2 * linux/drivers/cpufreq/cpufreq.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
8 * Added handling for CPU hotplug
9 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/notifier.h>
22 #include <linux/cpufreq.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/device.h>
27 #include <linux/slab.h>
28 #include <linux/cpu.h>
29 #include <linux/completion.h>
30 #include <linux/mutex.h>
32 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
36 * The "cpufreq driver" - the arch- or hardware-dependent low
37 * level driver of CPUFreq support, and its spinlock. This lock
38 * also protects the cpufreq_cpu_data array.
40 static struct cpufreq_driver *cpufreq_driver;
41 static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
42 static DEFINE_SPINLOCK(cpufreq_driver_lock);
44 /* internal prototypes */
45 static int __cpufreq_governor(struct cpufreq_policy *policy,
47 static void handle_update(void *data);
50 * Two notifier lists: the "policy" list is involved in the
51 * validation process for a new CPU frequency policy; the
52 * "transition" list for kernel code that needs to handle
53 * changes to devices when the CPU clock speed changes.
54 * The mutex locks both lists.
56 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
57 static struct srcu_notifier_head cpufreq_transition_notifier_list;
59 static int __init init_cpufreq_transition_notifier_list(void)
61 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
64 core_initcall(init_cpufreq_transition_notifier_list);
66 static LIST_HEAD(cpufreq_governor_list);
67 static DEFINE_MUTEX (cpufreq_governor_mutex);
69 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
71 struct cpufreq_policy *data;
77 /* get the cpufreq driver */
78 spin_lock_irqsave(&cpufreq_driver_lock, flags);
83 if (!try_module_get(cpufreq_driver->owner))
88 data = cpufreq_cpu_data[cpu];
91 goto err_out_put_module;
93 if (!kobject_get(&data->kobj))
94 goto err_out_put_module;
96 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
100 module_put(cpufreq_driver->owner);
102 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
106 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
109 void cpufreq_cpu_put(struct cpufreq_policy *data)
111 kobject_put(&data->kobj);
112 module_put(cpufreq_driver->owner);
114 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
117 /*********************************************************************
118 * UNIFIED DEBUG HELPERS *
119 *********************************************************************/
120 #ifdef CONFIG_CPU_FREQ_DEBUG
122 /* what part(s) of the CPUfreq subsystem are debugged? */
123 static unsigned int debug;
125 /* is the debug output ratelimit'ed using printk_ratelimit? User can
126 * set or modify this value.
128 static unsigned int debug_ratelimit = 1;
130 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
131 * loading of a cpufreq driver, temporarily disabled when a new policy
132 * is set, and disabled upon cpufreq driver removal
134 static unsigned int disable_ratelimit = 1;
135 static DEFINE_SPINLOCK(disable_ratelimit_lock);
137 static void cpufreq_debug_enable_ratelimit(void)
141 spin_lock_irqsave(&disable_ratelimit_lock, flags);
142 if (disable_ratelimit)
144 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
147 static void cpufreq_debug_disable_ratelimit(void)
151 spin_lock_irqsave(&disable_ratelimit_lock, flags);
153 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
156 void cpufreq_debug_printk(unsigned int type, const char *prefix,
157 const char *fmt, ...)
166 spin_lock_irqsave(&disable_ratelimit_lock, flags);
167 if (!disable_ratelimit && debug_ratelimit
168 && !printk_ratelimit()) {
169 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
172 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
174 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
177 len += vsnprintf(&s[len], (256 - len), fmt, args);
185 EXPORT_SYMBOL(cpufreq_debug_printk);
188 module_param(debug, uint, 0644);
189 MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
190 " 2 to debug drivers, and 4 to debug governors.");
192 module_param(debug_ratelimit, uint, 0644);
193 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
194 " set to 0 to disable ratelimiting.");
196 #else /* !CONFIG_CPU_FREQ_DEBUG */
198 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
199 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
201 #endif /* CONFIG_CPU_FREQ_DEBUG */
204 /*********************************************************************
205 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
206 *********************************************************************/
209 * adjust_jiffies - adjust the system "loops_per_jiffy"
211 * This function alters the system "loops_per_jiffy" for the clock
212 * speed change. Note that loops_per_jiffy cannot be updated on SMP
213 * systems as each CPU might be scaled differently. So, use the arch
214 * per-CPU loops_per_jiffy value wherever possible.
217 static unsigned long l_p_j_ref;
218 static unsigned int l_p_j_ref_freq;
220 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
222 if (ci->flags & CPUFREQ_CONST_LOOPS)
225 if (!l_p_j_ref_freq) {
226 l_p_j_ref = loops_per_jiffy;
227 l_p_j_ref_freq = ci->old;
228 dprintk("saving %lu as reference value for loops_per_jiffy;"
229 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
231 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
232 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
233 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
234 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
236 dprintk("scaling loops_per_jiffy to %lu"
237 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
241 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
249 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
250 * on frequency transition.
252 * This function calls the transition notifiers and the "adjust_jiffies"
253 * function. It is called twice on all CPU frequency changes that have
256 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
258 struct cpufreq_policy *policy;
260 BUG_ON(irqs_disabled());
262 freqs->flags = cpufreq_driver->flags;
263 dprintk("notification %u of frequency transition to %u kHz\n",
266 policy = cpufreq_cpu_data[freqs->cpu];
269 case CPUFREQ_PRECHANGE:
270 /* detect if the driver reported a value as "old frequency"
271 * which is not equal to what the cpufreq core thinks is
274 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
275 if ((policy) && (policy->cpu == freqs->cpu) &&
276 (policy->cur) && (policy->cur != freqs->old)) {
277 dprintk("Warning: CPU frequency is"
278 " %u, cpufreq assumed %u kHz.\n",
279 freqs->old, policy->cur);
280 freqs->old = policy->cur;
283 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
284 CPUFREQ_PRECHANGE, freqs);
285 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
288 case CPUFREQ_POSTCHANGE:
289 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
290 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
291 CPUFREQ_POSTCHANGE, freqs);
292 if (likely(policy) && likely(policy->cpu == freqs->cpu))
293 policy->cur = freqs->new;
297 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
301 /*********************************************************************
303 *********************************************************************/
305 static struct cpufreq_governor *__find_governor(const char *str_governor)
307 struct cpufreq_governor *t;
309 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
310 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN))
317 * cpufreq_parse_governor - parse a governor string
319 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
320 struct cpufreq_governor **governor)
327 if (cpufreq_driver->setpolicy) {
328 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
329 *policy = CPUFREQ_POLICY_PERFORMANCE;
331 } else if (!strnicmp(str_governor, "powersave",
333 *policy = CPUFREQ_POLICY_POWERSAVE;
336 } else if (cpufreq_driver->target) {
337 struct cpufreq_governor *t;
339 mutex_lock(&cpufreq_governor_mutex);
341 t = __find_governor(str_governor);
344 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
350 mutex_unlock(&cpufreq_governor_mutex);
351 ret = request_module(name);
352 mutex_lock(&cpufreq_governor_mutex);
355 t = __find_governor(str_governor);
366 mutex_unlock(&cpufreq_governor_mutex);
373 /* drivers/base/cpu.c */
374 extern struct sysdev_class cpu_sysdev_class;
378 * cpufreq_per_cpu_attr_read() / show_##file_name() -
379 * print out cpufreq information
381 * Write out information from cpufreq_driver->policy[cpu]; object must be
385 #define show_one(file_name, object) \
386 static ssize_t show_##file_name \
387 (struct cpufreq_policy * policy, char *buf) \
389 return sprintf (buf, "%u\n", policy->object); \
392 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
393 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
394 show_one(scaling_min_freq, min);
395 show_one(scaling_max_freq, max);
396 show_one(scaling_cur_freq, cur);
398 static int __cpufreq_set_policy(struct cpufreq_policy *data,
399 struct cpufreq_policy *policy);
402 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
404 #define store_one(file_name, object) \
405 static ssize_t store_##file_name \
406 (struct cpufreq_policy * policy, const char *buf, size_t count) \
408 unsigned int ret = -EINVAL; \
409 struct cpufreq_policy new_policy; \
411 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
415 ret = sscanf (buf, "%u", &new_policy.object); \
419 lock_cpu_hotplug(); \
420 mutex_lock(&policy->lock); \
421 ret = __cpufreq_set_policy(policy, &new_policy); \
422 policy->user_policy.object = policy->object; \
423 mutex_unlock(&policy->lock); \
424 unlock_cpu_hotplug(); \
426 return ret ? ret : count; \
429 store_one(scaling_min_freq,min);
430 store_one(scaling_max_freq,max);
433 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
435 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy,
438 unsigned int cur_freq = cpufreq_get(policy->cpu);
440 return sprintf(buf, "<unknown>");
441 return sprintf(buf, "%u\n", cur_freq);
446 * show_scaling_governor - show the current policy for the specified CPU
448 static ssize_t show_scaling_governor (struct cpufreq_policy * policy,
451 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
452 return sprintf(buf, "powersave\n");
453 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
454 return sprintf(buf, "performance\n");
455 else if (policy->governor)
456 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
462 * store_scaling_governor - store policy for the specified CPU
464 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
465 const char *buf, size_t count)
467 unsigned int ret = -EINVAL;
468 char str_governor[16];
469 struct cpufreq_policy new_policy;
471 ret = cpufreq_get_policy(&new_policy, policy->cpu);
475 ret = sscanf (buf, "%15s", str_governor);
479 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
480 &new_policy.governor))
485 /* Do not use cpufreq_set_policy here or the user_policy.max
486 will be wrongly overridden */
487 mutex_lock(&policy->lock);
488 ret = __cpufreq_set_policy(policy, &new_policy);
490 policy->user_policy.policy = policy->policy;
491 policy->user_policy.governor = policy->governor;
492 mutex_unlock(&policy->lock);
494 unlock_cpu_hotplug();
503 * show_scaling_driver - show the cpufreq driver currently loaded
505 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
507 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
511 * show_scaling_available_governors - show the available CPUfreq governors
513 static ssize_t show_scaling_available_governors (struct cpufreq_policy *policy,
517 struct cpufreq_governor *t;
519 if (!cpufreq_driver->target) {
520 i += sprintf(buf, "performance powersave");
524 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
525 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
527 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
530 i += sprintf(&buf[i], "\n");
534 * show_affected_cpus - show the CPUs affected by each transition
536 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
541 for_each_cpu_mask(cpu, policy->cpus) {
543 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
544 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
545 if (i >= (PAGE_SIZE - 5))
548 i += sprintf(&buf[i], "\n");
553 #define define_one_ro(_name) \
554 static struct freq_attr _name = \
555 __ATTR(_name, 0444, show_##_name, NULL)
557 #define define_one_ro0400(_name) \
558 static struct freq_attr _name = \
559 __ATTR(_name, 0400, show_##_name, NULL)
561 #define define_one_rw(_name) \
562 static struct freq_attr _name = \
563 __ATTR(_name, 0644, show_##_name, store_##_name)
565 define_one_ro0400(cpuinfo_cur_freq);
566 define_one_ro(cpuinfo_min_freq);
567 define_one_ro(cpuinfo_max_freq);
568 define_one_ro(scaling_available_governors);
569 define_one_ro(scaling_driver);
570 define_one_ro(scaling_cur_freq);
571 define_one_ro(affected_cpus);
572 define_one_rw(scaling_min_freq);
573 define_one_rw(scaling_max_freq);
574 define_one_rw(scaling_governor);
576 static struct attribute * default_attrs[] = {
577 &cpuinfo_min_freq.attr,
578 &cpuinfo_max_freq.attr,
579 &scaling_min_freq.attr,
580 &scaling_max_freq.attr,
582 &scaling_governor.attr,
583 &scaling_driver.attr,
584 &scaling_available_governors.attr,
588 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
589 #define to_attr(a) container_of(a,struct freq_attr,attr)
591 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
593 struct cpufreq_policy * policy = to_policy(kobj);
594 struct freq_attr * fattr = to_attr(attr);
596 policy = cpufreq_cpu_get(policy->cpu);
600 ret = fattr->show(policy, buf);
604 cpufreq_cpu_put(policy);
608 static ssize_t store(struct kobject * kobj, struct attribute * attr,
609 const char * buf, size_t count)
611 struct cpufreq_policy * policy = to_policy(kobj);
612 struct freq_attr * fattr = to_attr(attr);
614 policy = cpufreq_cpu_get(policy->cpu);
618 ret = fattr->store(policy, buf, count);
622 cpufreq_cpu_put(policy);
626 static void cpufreq_sysfs_release(struct kobject * kobj)
628 struct cpufreq_policy * policy = to_policy(kobj);
629 dprintk("last reference is dropped\n");
630 complete(&policy->kobj_unregister);
633 static struct sysfs_ops sysfs_ops = {
638 static struct kobj_type ktype_cpufreq = {
639 .sysfs_ops = &sysfs_ops,
640 .default_attrs = default_attrs,
641 .release = cpufreq_sysfs_release,
646 * cpufreq_add_dev - add a CPU device
648 * Adds the cpufreq interface for a CPU device.
650 static int cpufreq_add_dev (struct sys_device * sys_dev)
652 unsigned int cpu = sys_dev->id;
654 struct cpufreq_policy new_policy;
655 struct cpufreq_policy *policy;
656 struct freq_attr **drv_attr;
657 struct sys_device *cpu_sys_dev;
661 struct cpufreq_policy *managed_policy;
664 if (cpu_is_offline(cpu))
667 cpufreq_debug_disable_ratelimit();
668 dprintk("adding CPU %u\n", cpu);
671 /* check whether a different CPU already registered this
672 * CPU because it is in the same boat. */
673 policy = cpufreq_cpu_get(cpu);
674 if (unlikely(policy)) {
675 cpufreq_cpu_put(policy);
676 cpufreq_debug_enable_ratelimit();
681 if (!try_module_get(cpufreq_driver->owner)) {
686 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
693 policy->cpus = cpumask_of_cpu(cpu);
695 mutex_init(&policy->lock);
696 mutex_lock(&policy->lock);
697 init_completion(&policy->kobj_unregister);
698 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
700 /* call driver. From then on the cpufreq must be able
701 * to accept all calls to ->verify and ->setpolicy for this CPU
703 ret = cpufreq_driver->init(policy);
705 dprintk("initialization failed\n");
706 mutex_unlock(&policy->lock);
711 for_each_cpu_mask(j, policy->cpus) {
715 /* check for existing affected CPUs. They may not be aware
716 * of it due to CPU Hotplug.
718 managed_policy = cpufreq_cpu_get(j);
719 if (unlikely(managed_policy)) {
720 spin_lock_irqsave(&cpufreq_driver_lock, flags);
721 managed_policy->cpus = policy->cpus;
722 cpufreq_cpu_data[cpu] = managed_policy;
723 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
725 dprintk("CPU already managed, adding link\n");
726 sysfs_create_link(&sys_dev->kobj,
727 &managed_policy->kobj, "cpufreq");
729 cpufreq_debug_enable_ratelimit();
730 mutex_unlock(&policy->lock);
732 goto err_out_driver_exit; /* call driver->exit() */
736 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
738 /* prepare interface data */
739 policy->kobj.parent = &sys_dev->kobj;
740 policy->kobj.ktype = &ktype_cpufreq;
741 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
743 ret = kobject_register(&policy->kobj);
745 mutex_unlock(&policy->lock);
746 goto err_out_driver_exit;
748 /* set up files for this cpu device */
749 drv_attr = cpufreq_driver->attr;
750 while ((drv_attr) && (*drv_attr)) {
751 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
754 if (cpufreq_driver->get)
755 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
756 if (cpufreq_driver->target)
757 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
759 spin_lock_irqsave(&cpufreq_driver_lock, flags);
760 for_each_cpu_mask(j, policy->cpus)
761 cpufreq_cpu_data[j] = policy;
762 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
764 /* symlink affected CPUs */
765 for_each_cpu_mask(j, policy->cpus) {
771 dprintk("CPU %u already managed, adding link\n", j);
772 cpufreq_cpu_get(cpu);
773 cpu_sys_dev = get_cpu_sysdev(j);
774 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
778 policy->governor = NULL; /* to assure that the starting sequence is
779 * run in cpufreq_set_policy */
780 mutex_unlock(&policy->lock);
782 /* set default policy */
783 ret = cpufreq_set_policy(&new_policy);
785 dprintk("setting policy failed\n");
786 goto err_out_unregister;
789 module_put(cpufreq_driver->owner);
790 dprintk("initialization complete\n");
791 cpufreq_debug_enable_ratelimit();
797 spin_lock_irqsave(&cpufreq_driver_lock, flags);
798 for_each_cpu_mask(j, policy->cpus)
799 cpufreq_cpu_data[j] = NULL;
800 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
802 kobject_unregister(&policy->kobj);
803 wait_for_completion(&policy->kobj_unregister);
806 if (cpufreq_driver->exit)
807 cpufreq_driver->exit(policy);
813 module_put(cpufreq_driver->owner);
815 cpufreq_debug_enable_ratelimit();
821 * cpufreq_remove_dev - remove a CPU device
823 * Removes the cpufreq interface for a CPU device.
825 static int cpufreq_remove_dev (struct sys_device * sys_dev)
827 unsigned int cpu = sys_dev->id;
829 struct cpufreq_policy *data;
831 struct sys_device *cpu_sys_dev;
835 cpufreq_debug_disable_ratelimit();
836 dprintk("unregistering CPU %u\n", cpu);
838 spin_lock_irqsave(&cpufreq_driver_lock, flags);
839 data = cpufreq_cpu_data[cpu];
842 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
843 cpufreq_debug_enable_ratelimit();
846 cpufreq_cpu_data[cpu] = NULL;
850 /* if this isn't the CPU which is the parent of the kobj, we
851 * only need to unlink, put and exit
853 if (unlikely(cpu != data->cpu)) {
854 dprintk("removing link\n");
855 cpu_clear(cpu, data->cpus);
856 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
857 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
858 cpufreq_cpu_put(data);
859 cpufreq_debug_enable_ratelimit();
865 if (!kobject_get(&data->kobj)) {
866 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
867 cpufreq_debug_enable_ratelimit();
872 /* if we have other CPUs still registered, we need to unlink them,
873 * or else wait_for_completion below will lock up. Clean the
874 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
877 if (unlikely(cpus_weight(data->cpus) > 1)) {
878 for_each_cpu_mask(j, data->cpus) {
881 cpufreq_cpu_data[j] = NULL;
885 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
887 if (unlikely(cpus_weight(data->cpus) > 1)) {
888 for_each_cpu_mask(j, data->cpus) {
891 dprintk("removing link for cpu %u\n", j);
892 cpu_sys_dev = get_cpu_sysdev(j);
893 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
894 cpufreq_cpu_put(data);
898 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
901 mutex_lock(&data->lock);
902 if (cpufreq_driver->target)
903 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
904 mutex_unlock(&data->lock);
906 kobject_unregister(&data->kobj);
908 kobject_put(&data->kobj);
910 /* we need to make sure that the underlying kobj is actually
911 * not referenced anymore by anybody before we proceed with
914 dprintk("waiting for dropping of refcount\n");
915 wait_for_completion(&data->kobj_unregister);
916 dprintk("wait complete\n");
918 if (cpufreq_driver->exit)
919 cpufreq_driver->exit(data);
923 cpufreq_debug_enable_ratelimit();
928 static void handle_update(void *data)
930 unsigned int cpu = (unsigned int)(long)data;
931 dprintk("handle_update for cpu %u called\n", cpu);
932 cpufreq_update_policy(cpu);
936 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
938 * @old_freq: CPU frequency the kernel thinks the CPU runs at
939 * @new_freq: CPU frequency the CPU actually runs at
941 * We adjust to current frequency first, and need to clean up later. So either call
942 * to cpufreq_update_policy() or schedule handle_update()).
944 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
945 unsigned int new_freq)
947 struct cpufreq_freqs freqs;
949 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
950 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
953 freqs.old = old_freq;
954 freqs.new = new_freq;
955 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
956 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
961 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
964 * This is the last known freq, without actually getting it from the driver.
965 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
967 unsigned int cpufreq_quick_get(unsigned int cpu)
969 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
970 unsigned int ret_freq = 0;
973 mutex_lock(&policy->lock);
974 ret_freq = policy->cur;
975 mutex_unlock(&policy->lock);
976 cpufreq_cpu_put(policy);
981 EXPORT_SYMBOL(cpufreq_quick_get);
985 * cpufreq_get - get the current CPU frequency (in kHz)
988 * Get the CPU current (static) CPU frequency
990 unsigned int cpufreq_get(unsigned int cpu)
992 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
993 unsigned int ret_freq = 0;
998 if (!cpufreq_driver->get)
1001 mutex_lock(&policy->lock);
1003 ret_freq = cpufreq_driver->get(cpu);
1005 if (ret_freq && policy->cur &&
1006 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1007 /* verify no discrepancy between actual and
1008 saved value exists */
1009 if (unlikely(ret_freq != policy->cur)) {
1010 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1011 schedule_work(&policy->update);
1015 mutex_unlock(&policy->lock);
1018 cpufreq_cpu_put(policy);
1022 EXPORT_SYMBOL(cpufreq_get);
1026 * cpufreq_suspend - let the low level driver prepare for suspend
1029 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
1031 int cpu = sysdev->id;
1033 unsigned int cur_freq = 0;
1034 struct cpufreq_policy *cpu_policy;
1036 dprintk("suspending cpu %u\n", cpu);
1038 if (!cpu_online(cpu))
1041 /* we may be lax here as interrupts are off. Nonetheless
1042 * we need to grab the correct cpu policy, as to check
1043 * whether we really run on this CPU.
1046 cpu_policy = cpufreq_cpu_get(cpu);
1050 /* only handle each CPU group once */
1051 if (unlikely(cpu_policy->cpu != cpu)) {
1052 cpufreq_cpu_put(cpu_policy);
1056 if (cpufreq_driver->suspend) {
1057 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
1059 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1060 "step on CPU %u\n", cpu_policy->cpu);
1061 cpufreq_cpu_put(cpu_policy);
1067 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
1070 if (cpufreq_driver->get)
1071 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1073 if (!cur_freq || !cpu_policy->cur) {
1074 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1075 "frequency is what timing core thinks it is.\n");
1079 if (unlikely(cur_freq != cpu_policy->cur)) {
1080 struct cpufreq_freqs freqs;
1082 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1083 dprintk("Warning: CPU frequency is %u, "
1084 "cpufreq assumed %u kHz.\n",
1085 cur_freq, cpu_policy->cur);
1088 freqs.old = cpu_policy->cur;
1089 freqs.new = cur_freq;
1091 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
1092 CPUFREQ_SUSPENDCHANGE, &freqs);
1093 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1095 cpu_policy->cur = cur_freq;
1099 cpufreq_cpu_put(cpu_policy);
1104 * cpufreq_resume - restore proper CPU frequency handling after resume
1106 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1107 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
1108 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1111 static int cpufreq_resume(struct sys_device * sysdev)
1113 int cpu = sysdev->id;
1115 struct cpufreq_policy *cpu_policy;
1117 dprintk("resuming cpu %u\n", cpu);
1119 if (!cpu_online(cpu))
1122 /* we may be lax here as interrupts are off. Nonetheless
1123 * we need to grab the correct cpu policy, as to check
1124 * whether we really run on this CPU.
1127 cpu_policy = cpufreq_cpu_get(cpu);
1131 /* only handle each CPU group once */
1132 if (unlikely(cpu_policy->cpu != cpu)) {
1133 cpufreq_cpu_put(cpu_policy);
1137 if (cpufreq_driver->resume) {
1138 ret = cpufreq_driver->resume(cpu_policy);
1140 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1141 "step on CPU %u\n", cpu_policy->cpu);
1142 cpufreq_cpu_put(cpu_policy);
1147 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1148 unsigned int cur_freq = 0;
1150 if (cpufreq_driver->get)
1151 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1153 if (!cur_freq || !cpu_policy->cur) {
1154 printk(KERN_ERR "cpufreq: resume failed to assert "
1155 "current frequency is what timing core "
1160 if (unlikely(cur_freq != cpu_policy->cur)) {
1161 struct cpufreq_freqs freqs;
1163 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1164 dprintk("Warning: CPU frequency"
1165 "is %u, cpufreq assumed %u kHz.\n",
1166 cur_freq, cpu_policy->cur);
1169 freqs.old = cpu_policy->cur;
1170 freqs.new = cur_freq;
1172 srcu_notifier_call_chain(
1173 &cpufreq_transition_notifier_list,
1174 CPUFREQ_RESUMECHANGE, &freqs);
1175 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1177 cpu_policy->cur = cur_freq;
1182 schedule_work(&cpu_policy->update);
1183 cpufreq_cpu_put(cpu_policy);
1187 static struct sysdev_driver cpufreq_sysdev_driver = {
1188 .add = cpufreq_add_dev,
1189 .remove = cpufreq_remove_dev,
1190 .suspend = cpufreq_suspend,
1191 .resume = cpufreq_resume,
1195 /*********************************************************************
1196 * NOTIFIER LISTS INTERFACE *
1197 *********************************************************************/
1200 * cpufreq_register_notifier - register a driver with cpufreq
1201 * @nb: notifier function to register
1202 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1204 * Add a driver to one of two lists: either a list of drivers that
1205 * are notified about clock rate changes (once before and once after
1206 * the transition), or a list of drivers that are notified about
1207 * changes in cpufreq policy.
1209 * This function may sleep, and has the same return conditions as
1210 * blocking_notifier_chain_register.
1212 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1217 case CPUFREQ_TRANSITION_NOTIFIER:
1218 ret = srcu_notifier_chain_register(
1219 &cpufreq_transition_notifier_list, nb);
1221 case CPUFREQ_POLICY_NOTIFIER:
1222 ret = blocking_notifier_chain_register(
1223 &cpufreq_policy_notifier_list, nb);
1231 EXPORT_SYMBOL(cpufreq_register_notifier);
1235 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1236 * @nb: notifier block to be unregistered
1237 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1239 * Remove a driver from the CPU frequency notifier list.
1241 * This function may sleep, and has the same return conditions as
1242 * blocking_notifier_chain_unregister.
1244 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1249 case CPUFREQ_TRANSITION_NOTIFIER:
1250 ret = srcu_notifier_chain_unregister(
1251 &cpufreq_transition_notifier_list, nb);
1253 case CPUFREQ_POLICY_NOTIFIER:
1254 ret = blocking_notifier_chain_unregister(
1255 &cpufreq_policy_notifier_list, nb);
1263 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1266 /*********************************************************************
1268 *********************************************************************/
1271 /* Must be called with lock_cpu_hotplug held */
1272 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1273 unsigned int target_freq,
1274 unsigned int relation)
1276 int retval = -EINVAL;
1278 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1279 target_freq, relation);
1280 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1281 retval = cpufreq_driver->target(policy, target_freq, relation);
1285 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1287 int cpufreq_driver_target(struct cpufreq_policy *policy,
1288 unsigned int target_freq,
1289 unsigned int relation)
1293 policy = cpufreq_cpu_get(policy->cpu);
1298 mutex_lock(&policy->lock);
1300 ret = __cpufreq_driver_target(policy, target_freq, relation);
1302 mutex_unlock(&policy->lock);
1303 unlock_cpu_hotplug();
1305 cpufreq_cpu_put(policy);
1308 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1310 int cpufreq_driver_getavg(struct cpufreq_policy *policy)
1314 policy = cpufreq_cpu_get(policy->cpu);
1318 mutex_lock(&policy->lock);
1320 if (cpu_online(policy->cpu) && cpufreq_driver->getavg)
1321 ret = cpufreq_driver->getavg(policy->cpu);
1323 mutex_unlock(&policy->lock);
1325 cpufreq_cpu_put(policy);
1328 EXPORT_SYMBOL_GPL(cpufreq_driver_getavg);
1331 * Locking: Must be called with the lock_cpu_hotplug() lock held
1332 * when "event" is CPUFREQ_GOV_LIMITS
1335 static int __cpufreq_governor(struct cpufreq_policy *policy,
1340 if (!try_module_get(policy->governor->owner))
1343 dprintk("__cpufreq_governor for CPU %u, event %u\n",
1344 policy->cpu, event);
1345 ret = policy->governor->governor(policy, event);
1347 /* we keep one module reference alive for
1348 each CPU governed by this CPU */
1349 if ((event != CPUFREQ_GOV_START) || ret)
1350 module_put(policy->governor->owner);
1351 if ((event == CPUFREQ_GOV_STOP) && !ret)
1352 module_put(policy->governor->owner);
1358 int cpufreq_register_governor(struct cpufreq_governor *governor)
1365 mutex_lock(&cpufreq_governor_mutex);
1368 if (__find_governor(governor->name) == NULL) {
1370 list_add(&governor->governor_list, &cpufreq_governor_list);
1373 mutex_unlock(&cpufreq_governor_mutex);
1376 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1379 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1384 mutex_lock(&cpufreq_governor_mutex);
1385 list_del(&governor->governor_list);
1386 mutex_unlock(&cpufreq_governor_mutex);
1389 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1393 /*********************************************************************
1394 * POLICY INTERFACE *
1395 *********************************************************************/
1398 * cpufreq_get_policy - get the current cpufreq_policy
1399 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1401 * Reads the current cpufreq policy.
1403 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1405 struct cpufreq_policy *cpu_policy;
1409 cpu_policy = cpufreq_cpu_get(cpu);
1413 mutex_lock(&cpu_policy->lock);
1414 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1415 mutex_unlock(&cpu_policy->lock);
1417 cpufreq_cpu_put(cpu_policy);
1420 EXPORT_SYMBOL(cpufreq_get_policy);
1424 * data : current policy.
1425 * policy : policy to be set.
1426 * Locking: Must be called with the lock_cpu_hotplug() lock held
1428 static int __cpufreq_set_policy(struct cpufreq_policy *data,
1429 struct cpufreq_policy *policy)
1433 cpufreq_debug_disable_ratelimit();
1434 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1435 policy->min, policy->max);
1437 memcpy(&policy->cpuinfo, &data->cpuinfo,
1438 sizeof(struct cpufreq_cpuinfo));
1440 if (policy->min > data->min && policy->min > policy->max) {
1445 /* verify the cpu speed can be set within this limit */
1446 ret = cpufreq_driver->verify(policy);
1450 /* adjust if necessary - all reasons */
1451 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1452 CPUFREQ_ADJUST, policy);
1454 /* adjust if necessary - hardware incompatibility*/
1455 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1456 CPUFREQ_INCOMPATIBLE, policy);
1458 /* verify the cpu speed can be set within this limit,
1459 which might be different to the first one */
1460 ret = cpufreq_driver->verify(policy);
1464 /* notification of the new policy */
1465 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1466 CPUFREQ_NOTIFY, policy);
1468 data->min = policy->min;
1469 data->max = policy->max;
1471 dprintk("new min and max freqs are %u - %u kHz\n",
1472 data->min, data->max);
1474 if (cpufreq_driver->setpolicy) {
1475 data->policy = policy->policy;
1476 dprintk("setting range\n");
1477 ret = cpufreq_driver->setpolicy(policy);
1479 if (policy->governor != data->governor) {
1480 /* save old, working values */
1481 struct cpufreq_governor *old_gov = data->governor;
1483 dprintk("governor switch\n");
1485 /* end old governor */
1487 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1489 /* start new governor */
1490 data->governor = policy->governor;
1491 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1492 /* new governor failed, so re-start old one */
1493 dprintk("starting governor %s failed\n",
1494 data->governor->name);
1496 data->governor = old_gov;
1497 __cpufreq_governor(data,
1503 /* might be a policy change, too, so fall through */
1505 dprintk("governor: change or update limits\n");
1506 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1510 cpufreq_debug_enable_ratelimit();
1515 * cpufreq_set_policy - set a new CPUFreq policy
1516 * @policy: policy to be set.
1518 * Sets a new CPU frequency and voltage scaling policy.
1520 int cpufreq_set_policy(struct cpufreq_policy *policy)
1523 struct cpufreq_policy *data;
1528 data = cpufreq_cpu_get(policy->cpu);
1535 mutex_lock(&data->lock);
1537 ret = __cpufreq_set_policy(data, policy);
1538 data->user_policy.min = data->min;
1539 data->user_policy.max = data->max;
1540 data->user_policy.policy = data->policy;
1541 data->user_policy.governor = data->governor;
1543 mutex_unlock(&data->lock);
1545 unlock_cpu_hotplug();
1546 cpufreq_cpu_put(data);
1550 EXPORT_SYMBOL(cpufreq_set_policy);
1554 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1555 * @cpu: CPU which shall be re-evaluated
1557 * Usefull for policy notifiers which have different necessities
1558 * at different times.
1560 int cpufreq_update_policy(unsigned int cpu)
1562 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1563 struct cpufreq_policy policy;
1570 mutex_lock(&data->lock);
1572 dprintk("updating policy for CPU %u\n", cpu);
1573 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1574 policy.min = data->user_policy.min;
1575 policy.max = data->user_policy.max;
1576 policy.policy = data->user_policy.policy;
1577 policy.governor = data->user_policy.governor;
1579 /* BIOS might change freq behind our back
1580 -> ask driver for current freq and notify governors about a change */
1581 if (cpufreq_driver->get) {
1582 policy.cur = cpufreq_driver->get(cpu);
1584 dprintk("Driver did not initialize current freq");
1585 data->cur = policy.cur;
1587 if (data->cur != policy.cur)
1588 cpufreq_out_of_sync(cpu, data->cur,
1593 ret = __cpufreq_set_policy(data, &policy);
1595 mutex_unlock(&data->lock);
1596 unlock_cpu_hotplug();
1597 cpufreq_cpu_put(data);
1600 EXPORT_SYMBOL(cpufreq_update_policy);
1602 #ifdef CONFIG_HOTPLUG_CPU
1603 static int cpufreq_cpu_callback(struct notifier_block *nfb,
1604 unsigned long action, void *hcpu)
1606 unsigned int cpu = (unsigned long)hcpu;
1607 struct cpufreq_policy *policy;
1608 struct sys_device *sys_dev;
1610 sys_dev = get_cpu_sysdev(cpu);
1615 cpufreq_add_dev(sys_dev);
1617 case CPU_DOWN_PREPARE:
1619 * We attempt to put this cpu in lowest frequency
1620 * possible before going down. This will permit
1621 * hardware-managed P-State to switch other related
1622 * threads to min or higher speeds if possible.
1624 policy = cpufreq_cpu_data[cpu];
1626 cpufreq_driver_target(policy, policy->min,
1627 CPUFREQ_RELATION_H);
1631 cpufreq_remove_dev(sys_dev);
1638 static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
1640 .notifier_call = cpufreq_cpu_callback,
1642 #endif /* CONFIG_HOTPLUG_CPU */
1644 /*********************************************************************
1645 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1646 *********************************************************************/
1649 * cpufreq_register_driver - register a CPU Frequency driver
1650 * @driver_data: A struct cpufreq_driver containing the values#
1651 * submitted by the CPU Frequency driver.
1653 * Registers a CPU Frequency driver to this core code. This code
1654 * returns zero on success, -EBUSY when another driver got here first
1655 * (and isn't unregistered in the meantime).
1658 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1660 unsigned long flags;
1663 if (!driver_data || !driver_data->verify || !driver_data->init ||
1664 ((!driver_data->setpolicy) && (!driver_data->target)))
1667 dprintk("trying to register driver %s\n", driver_data->name);
1669 if (driver_data->setpolicy)
1670 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1672 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1673 if (cpufreq_driver) {
1674 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1677 cpufreq_driver = driver_data;
1678 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1680 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1682 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1686 /* check for at least one working CPU */
1687 for (i=0; i<NR_CPUS; i++)
1688 if (cpufreq_cpu_data[i])
1691 /* if all ->init() calls failed, unregister */
1693 dprintk("no CPU initialized for driver %s\n",
1695 sysdev_driver_unregister(&cpu_sysdev_class,
1696 &cpufreq_sysdev_driver);
1698 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1699 cpufreq_driver = NULL;
1700 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1705 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1706 dprintk("driver %s up and running\n", driver_data->name);
1707 cpufreq_debug_enable_ratelimit();
1712 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1716 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1718 * Unregister the current CPUFreq driver. Only call this if you have
1719 * the right to do so, i.e. if you have succeeded in initialising before!
1720 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1721 * currently not initialised.
1723 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1725 unsigned long flags;
1727 cpufreq_debug_disable_ratelimit();
1729 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1730 cpufreq_debug_enable_ratelimit();
1734 dprintk("unregistering driver %s\n", driver->name);
1736 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1737 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1739 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1740 cpufreq_driver = NULL;
1741 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1745 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);