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, "cpufreq-core", msg)
35 * The "cpufreq driver" - the arch- or hardware-dependend low
36 * level driver of CPUFreq support, and its spinlock. This lock
37 * also protects the cpufreq_cpu_data array.
39 static struct cpufreq_driver *cpufreq_driver;
40 static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
41 static DEFINE_SPINLOCK(cpufreq_driver_lock);
43 /* internal prototypes */
44 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
45 static void handle_update(void *data);
48 * Two notifier lists: the "policy" list is involved in the
49 * validation process for a new CPU frequency policy; the
50 * "transition" list for kernel code that needs to handle
51 * changes to devices when the CPU clock speed changes.
52 * The mutex locks both lists.
54 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
55 static BLOCKING_NOTIFIER_HEAD(cpufreq_transition_notifier_list);
58 static LIST_HEAD(cpufreq_governor_list);
59 static DEFINE_MUTEX (cpufreq_governor_mutex);
61 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
63 struct cpufreq_policy *data;
69 /* get the cpufreq driver */
70 spin_lock_irqsave(&cpufreq_driver_lock, flags);
75 if (!try_module_get(cpufreq_driver->owner))
80 data = cpufreq_cpu_data[cpu];
83 goto err_out_put_module;
85 if (!kobject_get(&data->kobj))
86 goto err_out_put_module;
88 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
92 module_put(cpufreq_driver->owner);
94 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
98 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
101 void cpufreq_cpu_put(struct cpufreq_policy *data)
103 kobject_put(&data->kobj);
104 module_put(cpufreq_driver->owner);
106 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
109 /*********************************************************************
110 * UNIFIED DEBUG HELPERS *
111 *********************************************************************/
112 #ifdef CONFIG_CPU_FREQ_DEBUG
114 /* what part(s) of the CPUfreq subsystem are debugged? */
115 static unsigned int debug;
117 /* is the debug output ratelimit'ed using printk_ratelimit? User can
118 * set or modify this value.
120 static unsigned int debug_ratelimit = 1;
122 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
123 * loading of a cpufreq driver, temporarily disabled when a new policy
124 * is set, and disabled upon cpufreq driver removal
126 static unsigned int disable_ratelimit = 1;
127 static DEFINE_SPINLOCK(disable_ratelimit_lock);
129 static void cpufreq_debug_enable_ratelimit(void)
133 spin_lock_irqsave(&disable_ratelimit_lock, flags);
134 if (disable_ratelimit)
136 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
139 static void cpufreq_debug_disable_ratelimit(void)
143 spin_lock_irqsave(&disable_ratelimit_lock, flags);
145 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
148 void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
157 spin_lock_irqsave(&disable_ratelimit_lock, flags);
158 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
159 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
162 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
164 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
167 len += vsnprintf(&s[len], (256 - len), fmt, args);
175 EXPORT_SYMBOL(cpufreq_debug_printk);
178 module_param(debug, uint, 0644);
179 MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
181 module_param(debug_ratelimit, uint, 0644);
182 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
184 #else /* !CONFIG_CPU_FREQ_DEBUG */
186 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
187 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
189 #endif /* CONFIG_CPU_FREQ_DEBUG */
192 /*********************************************************************
193 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
194 *********************************************************************/
197 * adjust_jiffies - adjust the system "loops_per_jiffy"
199 * This function alters the system "loops_per_jiffy" for the clock
200 * speed change. Note that loops_per_jiffy cannot be updated on SMP
201 * systems as each CPU might be scaled differently. So, use the arch
202 * per-CPU loops_per_jiffy value wherever possible.
205 static unsigned long l_p_j_ref;
206 static unsigned int l_p_j_ref_freq;
208 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
210 if (ci->flags & CPUFREQ_CONST_LOOPS)
213 if (!l_p_j_ref_freq) {
214 l_p_j_ref = loops_per_jiffy;
215 l_p_j_ref_freq = ci->old;
216 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
218 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
219 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
220 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
221 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
222 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
226 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
231 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
232 * on frequency transition.
234 * This function calls the transition notifiers and the "adjust_jiffies"
235 * function. It is called twice on all CPU frequency changes that have
238 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
240 struct cpufreq_policy *policy;
242 BUG_ON(irqs_disabled());
244 freqs->flags = cpufreq_driver->flags;
245 dprintk("notification %u of frequency transition to %u kHz\n",
248 policy = cpufreq_cpu_data[freqs->cpu];
251 case CPUFREQ_PRECHANGE:
252 /* detect if the driver reported a value as "old frequency"
253 * which is not equal to what the cpufreq core thinks is
256 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
257 if ((policy) && (policy->cpu == freqs->cpu) &&
258 (policy->cur) && (policy->cur != freqs->old)) {
259 dprintk("Warning: CPU frequency is"
260 " %u, cpufreq assumed %u kHz.\n",
261 freqs->old, policy->cur);
262 freqs->old = policy->cur;
265 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
266 CPUFREQ_PRECHANGE, freqs);
267 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
270 case CPUFREQ_POSTCHANGE:
271 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
272 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
273 CPUFREQ_POSTCHANGE, freqs);
274 if (likely(policy) && likely(policy->cpu == freqs->cpu))
275 policy->cur = freqs->new;
279 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
283 /*********************************************************************
285 *********************************************************************/
288 * cpufreq_parse_governor - parse a governor string
290 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
291 struct cpufreq_governor **governor)
295 if (cpufreq_driver->setpolicy) {
296 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
297 *policy = CPUFREQ_POLICY_PERFORMANCE;
299 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
300 *policy = CPUFREQ_POLICY_POWERSAVE;
305 struct cpufreq_governor *t;
306 mutex_lock(&cpufreq_governor_mutex);
307 if (!cpufreq_driver || !cpufreq_driver->target)
309 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
310 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
312 mutex_unlock(&cpufreq_governor_mutex);
317 mutex_unlock(&cpufreq_governor_mutex);
323 /* drivers/base/cpu.c */
324 extern struct sysdev_class cpu_sysdev_class;
328 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
330 * Write out information from cpufreq_driver->policy[cpu]; object must be
334 #define show_one(file_name, object) \
335 static ssize_t show_##file_name \
336 (struct cpufreq_policy * policy, char *buf) \
338 return sprintf (buf, "%u\n", policy->object); \
341 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
342 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
343 show_one(scaling_min_freq, min);
344 show_one(scaling_max_freq, max);
345 show_one(scaling_cur_freq, cur);
347 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
350 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
352 #define store_one(file_name, object) \
353 static ssize_t store_##file_name \
354 (struct cpufreq_policy * policy, const char *buf, size_t count) \
356 unsigned int ret = -EINVAL; \
357 struct cpufreq_policy new_policy; \
359 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
363 ret = sscanf (buf, "%u", &new_policy.object); \
367 mutex_lock(&policy->lock); \
368 ret = __cpufreq_set_policy(policy, &new_policy); \
369 policy->user_policy.object = policy->object; \
370 mutex_unlock(&policy->lock); \
372 return ret ? ret : count; \
375 store_one(scaling_min_freq,min);
376 store_one(scaling_max_freq,max);
379 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
381 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
383 unsigned int cur_freq = cpufreq_get(policy->cpu);
385 return sprintf(buf, "<unknown>");
386 return sprintf(buf, "%u\n", cur_freq);
391 * show_scaling_governor - show the current policy for the specified CPU
393 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
395 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
396 return sprintf(buf, "powersave\n");
397 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
398 return sprintf(buf, "performance\n");
399 else if (policy->governor)
400 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
406 * store_scaling_governor - store policy for the specified CPU
408 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
409 const char *buf, size_t count)
411 unsigned int ret = -EINVAL;
412 char str_governor[16];
413 struct cpufreq_policy new_policy;
415 ret = cpufreq_get_policy(&new_policy, policy->cpu);
419 ret = sscanf (buf, "%15s", str_governor);
423 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
426 /* Do not use cpufreq_set_policy here or the user_policy.max
427 will be wrongly overridden */
428 mutex_lock(&policy->lock);
429 ret = __cpufreq_set_policy(policy, &new_policy);
431 policy->user_policy.policy = policy->policy;
432 policy->user_policy.governor = policy->governor;
433 mutex_unlock(&policy->lock);
435 return ret ? ret : count;
439 * show_scaling_driver - show the cpufreq driver currently loaded
441 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
443 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
447 * show_scaling_available_governors - show the available CPUfreq governors
449 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
453 struct cpufreq_governor *t;
455 if (!cpufreq_driver->target) {
456 i += sprintf(buf, "performance powersave");
460 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
461 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
463 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
466 i += sprintf(&buf[i], "\n");
470 * show_affected_cpus - show the CPUs affected by each transition
472 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
477 for_each_cpu_mask(cpu, policy->cpus) {
479 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
480 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
481 if (i >= (PAGE_SIZE - 5))
484 i += sprintf(&buf[i], "\n");
489 #define define_one_ro(_name) \
490 static struct freq_attr _name = \
491 __ATTR(_name, 0444, show_##_name, NULL)
493 #define define_one_ro0400(_name) \
494 static struct freq_attr _name = \
495 __ATTR(_name, 0400, show_##_name, NULL)
497 #define define_one_rw(_name) \
498 static struct freq_attr _name = \
499 __ATTR(_name, 0644, show_##_name, store_##_name)
501 define_one_ro0400(cpuinfo_cur_freq);
502 define_one_ro(cpuinfo_min_freq);
503 define_one_ro(cpuinfo_max_freq);
504 define_one_ro(scaling_available_governors);
505 define_one_ro(scaling_driver);
506 define_one_ro(scaling_cur_freq);
507 define_one_ro(affected_cpus);
508 define_one_rw(scaling_min_freq);
509 define_one_rw(scaling_max_freq);
510 define_one_rw(scaling_governor);
512 static struct attribute * default_attrs[] = {
513 &cpuinfo_min_freq.attr,
514 &cpuinfo_max_freq.attr,
515 &scaling_min_freq.attr,
516 &scaling_max_freq.attr,
518 &scaling_governor.attr,
519 &scaling_driver.attr,
520 &scaling_available_governors.attr,
524 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
525 #define to_attr(a) container_of(a,struct freq_attr,attr)
527 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
529 struct cpufreq_policy * policy = to_policy(kobj);
530 struct freq_attr * fattr = to_attr(attr);
532 policy = cpufreq_cpu_get(policy->cpu);
535 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
536 cpufreq_cpu_put(policy);
540 static ssize_t store(struct kobject * kobj, struct attribute * attr,
541 const char * buf, size_t count)
543 struct cpufreq_policy * policy = to_policy(kobj);
544 struct freq_attr * fattr = to_attr(attr);
546 policy = cpufreq_cpu_get(policy->cpu);
549 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
550 cpufreq_cpu_put(policy);
554 static void cpufreq_sysfs_release(struct kobject * kobj)
556 struct cpufreq_policy * policy = to_policy(kobj);
557 dprintk("last reference is dropped\n");
558 complete(&policy->kobj_unregister);
561 static struct sysfs_ops sysfs_ops = {
566 static struct kobj_type ktype_cpufreq = {
567 .sysfs_ops = &sysfs_ops,
568 .default_attrs = default_attrs,
569 .release = cpufreq_sysfs_release,
574 * cpufreq_add_dev - add a CPU device
576 * Adds the cpufreq interface for a CPU device.
578 static int cpufreq_add_dev (struct sys_device * sys_dev)
580 unsigned int cpu = sys_dev->id;
582 struct cpufreq_policy new_policy;
583 struct cpufreq_policy *policy;
584 struct freq_attr **drv_attr;
585 struct sys_device *cpu_sys_dev;
589 struct cpufreq_policy *managed_policy;
592 if (cpu_is_offline(cpu))
595 cpufreq_debug_disable_ratelimit();
596 dprintk("adding CPU %u\n", cpu);
599 /* check whether a different CPU already registered this
600 * CPU because it is in the same boat. */
601 policy = cpufreq_cpu_get(cpu);
602 if (unlikely(policy)) {
603 cpufreq_cpu_put(policy);
604 cpufreq_debug_enable_ratelimit();
609 if (!try_module_get(cpufreq_driver->owner)) {
614 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
621 policy->cpus = cpumask_of_cpu(cpu);
623 mutex_init(&policy->lock);
624 mutex_lock(&policy->lock);
625 init_completion(&policy->kobj_unregister);
626 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
628 /* call driver. From then on the cpufreq must be able
629 * to accept all calls to ->verify and ->setpolicy for this CPU
631 ret = cpufreq_driver->init(policy);
633 dprintk("initialization failed\n");
634 mutex_unlock(&policy->lock);
639 for_each_cpu_mask(j, policy->cpus) {
643 /* check for existing affected CPUs. They may not be aware
644 * of it due to CPU Hotplug.
646 managed_policy = cpufreq_cpu_get(j);
647 if (unlikely(managed_policy)) {
648 spin_lock_irqsave(&cpufreq_driver_lock, flags);
649 managed_policy->cpus = policy->cpus;
650 cpufreq_cpu_data[cpu] = managed_policy;
651 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
653 dprintk("CPU already managed, adding link\n");
654 sysfs_create_link(&sys_dev->kobj,
655 &managed_policy->kobj, "cpufreq");
657 cpufreq_debug_enable_ratelimit();
658 mutex_unlock(&policy->lock);
660 goto err_out_driver_exit; /* call driver->exit() */
664 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
666 /* prepare interface data */
667 policy->kobj.parent = &sys_dev->kobj;
668 policy->kobj.ktype = &ktype_cpufreq;
669 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
671 ret = kobject_register(&policy->kobj);
673 mutex_unlock(&policy->lock);
674 goto err_out_driver_exit;
676 /* set up files for this cpu device */
677 drv_attr = cpufreq_driver->attr;
678 while ((drv_attr) && (*drv_attr)) {
679 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
682 if (cpufreq_driver->get)
683 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
684 if (cpufreq_driver->target)
685 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
687 spin_lock_irqsave(&cpufreq_driver_lock, flags);
688 for_each_cpu_mask(j, policy->cpus)
689 cpufreq_cpu_data[j] = policy;
690 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
692 /* symlink affected CPUs */
693 for_each_cpu_mask(j, policy->cpus) {
699 dprintk("CPU %u already managed, adding link\n", j);
700 cpufreq_cpu_get(cpu);
701 cpu_sys_dev = get_cpu_sysdev(j);
702 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
706 policy->governor = NULL; /* to assure that the starting sequence is
707 * run in cpufreq_set_policy */
708 mutex_unlock(&policy->lock);
710 /* set default policy */
711 ret = cpufreq_set_policy(&new_policy);
713 dprintk("setting policy failed\n");
714 goto err_out_unregister;
717 module_put(cpufreq_driver->owner);
718 dprintk("initialization complete\n");
719 cpufreq_debug_enable_ratelimit();
725 spin_lock_irqsave(&cpufreq_driver_lock, flags);
726 for_each_cpu_mask(j, policy->cpus)
727 cpufreq_cpu_data[j] = NULL;
728 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
730 kobject_unregister(&policy->kobj);
731 wait_for_completion(&policy->kobj_unregister);
734 if (cpufreq_driver->exit)
735 cpufreq_driver->exit(policy);
741 module_put(cpufreq_driver->owner);
743 cpufreq_debug_enable_ratelimit();
749 * cpufreq_remove_dev - remove a CPU device
751 * Removes the cpufreq interface for a CPU device.
753 static int cpufreq_remove_dev (struct sys_device * sys_dev)
755 unsigned int cpu = sys_dev->id;
757 struct cpufreq_policy *data;
759 struct sys_device *cpu_sys_dev;
763 cpufreq_debug_disable_ratelimit();
764 dprintk("unregistering CPU %u\n", cpu);
766 spin_lock_irqsave(&cpufreq_driver_lock, flags);
767 data = cpufreq_cpu_data[cpu];
770 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
771 cpufreq_debug_enable_ratelimit();
774 cpufreq_cpu_data[cpu] = NULL;
778 /* if this isn't the CPU which is the parent of the kobj, we
779 * only need to unlink, put and exit
781 if (unlikely(cpu != data->cpu)) {
782 dprintk("removing link\n");
783 cpu_clear(cpu, data->cpus);
784 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
785 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
786 cpufreq_cpu_put(data);
787 cpufreq_debug_enable_ratelimit();
793 if (!kobject_get(&data->kobj)) {
794 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
795 cpufreq_debug_enable_ratelimit();
800 /* if we have other CPUs still registered, we need to unlink them,
801 * or else wait_for_completion below will lock up. Clean the
802 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
805 if (unlikely(cpus_weight(data->cpus) > 1)) {
806 for_each_cpu_mask(j, data->cpus) {
809 cpufreq_cpu_data[j] = NULL;
813 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
815 if (unlikely(cpus_weight(data->cpus) > 1)) {
816 for_each_cpu_mask(j, data->cpus) {
819 dprintk("removing link for cpu %u\n", j);
820 cpu_sys_dev = get_cpu_sysdev(j);
821 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
822 cpufreq_cpu_put(data);
826 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
829 mutex_lock(&data->lock);
830 if (cpufreq_driver->target)
831 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
832 mutex_unlock(&data->lock);
834 kobject_unregister(&data->kobj);
836 kobject_put(&data->kobj);
838 /* we need to make sure that the underlying kobj is actually
839 * not referenced anymore by anybody before we proceed with
842 dprintk("waiting for dropping of refcount\n");
843 wait_for_completion(&data->kobj_unregister);
844 dprintk("wait complete\n");
846 if (cpufreq_driver->exit)
847 cpufreq_driver->exit(data);
851 cpufreq_debug_enable_ratelimit();
856 static void handle_update(void *data)
858 unsigned int cpu = (unsigned int)(long)data;
859 dprintk("handle_update for cpu %u called\n", cpu);
860 cpufreq_update_policy(cpu);
864 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
866 * @old_freq: CPU frequency the kernel thinks the CPU runs at
867 * @new_freq: CPU frequency the CPU actually runs at
869 * We adjust to current frequency first, and need to clean up later. So either call
870 * to cpufreq_update_policy() or schedule handle_update()).
872 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
874 struct cpufreq_freqs freqs;
876 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
877 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
880 freqs.old = old_freq;
881 freqs.new = new_freq;
882 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
883 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
888 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
891 * This is the last known freq, without actually getting it from the driver.
892 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
894 unsigned int cpufreq_quick_get(unsigned int cpu)
896 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
897 unsigned int ret = 0;
900 mutex_lock(&policy->lock);
902 mutex_unlock(&policy->lock);
903 cpufreq_cpu_put(policy);
908 EXPORT_SYMBOL(cpufreq_quick_get);
912 * cpufreq_get - get the current CPU frequency (in kHz)
915 * Get the CPU current (static) CPU frequency
917 unsigned int cpufreq_get(unsigned int cpu)
919 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
920 unsigned int ret = 0;
925 if (!cpufreq_driver->get)
928 mutex_lock(&policy->lock);
930 ret = cpufreq_driver->get(cpu);
932 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
933 /* verify no discrepancy between actual and saved value exists */
934 if (unlikely(ret != policy->cur)) {
935 cpufreq_out_of_sync(cpu, policy->cur, ret);
936 schedule_work(&policy->update);
940 mutex_unlock(&policy->lock);
943 cpufreq_cpu_put(policy);
947 EXPORT_SYMBOL(cpufreq_get);
951 * cpufreq_suspend - let the low level driver prepare for suspend
954 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
956 int cpu = sysdev->id;
957 unsigned int ret = 0;
958 unsigned int cur_freq = 0;
959 struct cpufreq_policy *cpu_policy;
961 dprintk("resuming cpu %u\n", cpu);
963 if (!cpu_online(cpu))
966 /* we may be lax here as interrupts are off. Nonetheless
967 * we need to grab the correct cpu policy, as to check
968 * whether we really run on this CPU.
971 cpu_policy = cpufreq_cpu_get(cpu);
975 /* only handle each CPU group once */
976 if (unlikely(cpu_policy->cpu != cpu)) {
977 cpufreq_cpu_put(cpu_policy);
981 if (cpufreq_driver->suspend) {
982 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
984 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
985 "step on CPU %u\n", cpu_policy->cpu);
986 cpufreq_cpu_put(cpu_policy);
992 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
995 if (cpufreq_driver->get)
996 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
998 if (!cur_freq || !cpu_policy->cur) {
999 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1000 "frequency is what timing core thinks it is.\n");
1004 if (unlikely(cur_freq != cpu_policy->cur)) {
1005 struct cpufreq_freqs freqs;
1007 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1008 dprintk("Warning: CPU frequency is %u, "
1009 "cpufreq assumed %u kHz.\n",
1010 cur_freq, cpu_policy->cur);
1013 freqs.old = cpu_policy->cur;
1014 freqs.new = cur_freq;
1016 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
1017 CPUFREQ_SUSPENDCHANGE, &freqs);
1018 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1020 cpu_policy->cur = cur_freq;
1024 cpufreq_cpu_put(cpu_policy);
1029 * cpufreq_resume - restore proper CPU frequency handling after resume
1031 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1032 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
1033 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1036 static int cpufreq_resume(struct sys_device * sysdev)
1038 int cpu = sysdev->id;
1039 unsigned int ret = 0;
1040 struct cpufreq_policy *cpu_policy;
1042 dprintk("resuming cpu %u\n", cpu);
1044 if (!cpu_online(cpu))
1047 /* we may be lax here as interrupts are off. Nonetheless
1048 * we need to grab the correct cpu policy, as to check
1049 * whether we really run on this CPU.
1052 cpu_policy = cpufreq_cpu_get(cpu);
1056 /* only handle each CPU group once */
1057 if (unlikely(cpu_policy->cpu != cpu)) {
1058 cpufreq_cpu_put(cpu_policy);
1062 if (cpufreq_driver->resume) {
1063 ret = cpufreq_driver->resume(cpu_policy);
1065 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1066 "step on CPU %u\n", cpu_policy->cpu);
1067 cpufreq_cpu_put(cpu_policy);
1072 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1073 unsigned int cur_freq = 0;
1075 if (cpufreq_driver->get)
1076 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1078 if (!cur_freq || !cpu_policy->cur) {
1079 printk(KERN_ERR "cpufreq: resume failed to assert "
1080 "current frequency is what timing core "
1085 if (unlikely(cur_freq != cpu_policy->cur)) {
1086 struct cpufreq_freqs freqs;
1088 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1089 dprintk("Warning: CPU frequency"
1090 "is %u, cpufreq assumed %u kHz.\n",
1091 cur_freq, cpu_policy->cur);
1094 freqs.old = cpu_policy->cur;
1095 freqs.new = cur_freq;
1097 blocking_notifier_call_chain(
1098 &cpufreq_transition_notifier_list,
1099 CPUFREQ_RESUMECHANGE, &freqs);
1100 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1102 cpu_policy->cur = cur_freq;
1107 schedule_work(&cpu_policy->update);
1108 cpufreq_cpu_put(cpu_policy);
1112 static struct sysdev_driver cpufreq_sysdev_driver = {
1113 .add = cpufreq_add_dev,
1114 .remove = cpufreq_remove_dev,
1115 .suspend = cpufreq_suspend,
1116 .resume = cpufreq_resume,
1120 /*********************************************************************
1121 * NOTIFIER LISTS INTERFACE *
1122 *********************************************************************/
1125 * cpufreq_register_notifier - register a driver with cpufreq
1126 * @nb: notifier function to register
1127 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1129 * Add a driver to one of two lists: either a list of drivers that
1130 * are notified about clock rate changes (once before and once after
1131 * the transition), or a list of drivers that are notified about
1132 * changes in cpufreq policy.
1134 * This function may sleep, and has the same return conditions as
1135 * blocking_notifier_chain_register.
1137 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1142 case CPUFREQ_TRANSITION_NOTIFIER:
1143 ret = blocking_notifier_chain_register(
1144 &cpufreq_transition_notifier_list, nb);
1146 case CPUFREQ_POLICY_NOTIFIER:
1147 ret = blocking_notifier_chain_register(
1148 &cpufreq_policy_notifier_list, nb);
1156 EXPORT_SYMBOL(cpufreq_register_notifier);
1160 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1161 * @nb: notifier block to be unregistered
1162 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1164 * Remove a driver from the CPU frequency notifier list.
1166 * This function may sleep, and has the same return conditions as
1167 * blocking_notifier_chain_unregister.
1169 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1174 case CPUFREQ_TRANSITION_NOTIFIER:
1175 ret = blocking_notifier_chain_unregister(
1176 &cpufreq_transition_notifier_list, nb);
1178 case CPUFREQ_POLICY_NOTIFIER:
1179 ret = blocking_notifier_chain_unregister(
1180 &cpufreq_policy_notifier_list, nb);
1188 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1191 /*********************************************************************
1193 *********************************************************************/
1196 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1197 unsigned int target_freq,
1198 unsigned int relation)
1200 int retval = -EINVAL;
1203 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1204 target_freq, relation);
1205 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1206 retval = cpufreq_driver->target(policy, target_freq, relation);
1208 unlock_cpu_hotplug();
1212 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1214 int cpufreq_driver_target(struct cpufreq_policy *policy,
1215 unsigned int target_freq,
1216 unsigned int relation)
1220 policy = cpufreq_cpu_get(policy->cpu);
1224 mutex_lock(&policy->lock);
1226 ret = __cpufreq_driver_target(policy, target_freq, relation);
1228 mutex_unlock(&policy->lock);
1230 cpufreq_cpu_put(policy);
1233 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1236 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1240 if (!try_module_get(policy->governor->owner))
1243 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1244 ret = policy->governor->governor(policy, event);
1246 /* we keep one module reference alive for each CPU governed by this CPU */
1247 if ((event != CPUFREQ_GOV_START) || ret)
1248 module_put(policy->governor->owner);
1249 if ((event == CPUFREQ_GOV_STOP) && !ret)
1250 module_put(policy->governor->owner);
1256 int cpufreq_governor(unsigned int cpu, unsigned int event)
1259 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1264 mutex_lock(&policy->lock);
1265 ret = __cpufreq_governor(policy, event);
1266 mutex_unlock(&policy->lock);
1268 cpufreq_cpu_put(policy);
1271 EXPORT_SYMBOL_GPL(cpufreq_governor);
1274 int cpufreq_register_governor(struct cpufreq_governor *governor)
1276 struct cpufreq_governor *t;
1281 mutex_lock(&cpufreq_governor_mutex);
1283 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1284 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1285 mutex_unlock(&cpufreq_governor_mutex);
1289 list_add(&governor->governor_list, &cpufreq_governor_list);
1291 mutex_unlock(&cpufreq_governor_mutex);
1294 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1297 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1302 mutex_lock(&cpufreq_governor_mutex);
1303 list_del(&governor->governor_list);
1304 mutex_unlock(&cpufreq_governor_mutex);
1307 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1311 /*********************************************************************
1312 * POLICY INTERFACE *
1313 *********************************************************************/
1316 * cpufreq_get_policy - get the current cpufreq_policy
1317 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1319 * Reads the current cpufreq policy.
1321 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1323 struct cpufreq_policy *cpu_policy;
1327 cpu_policy = cpufreq_cpu_get(cpu);
1331 mutex_lock(&cpu_policy->lock);
1332 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1333 mutex_unlock(&cpu_policy->lock);
1335 cpufreq_cpu_put(cpu_policy);
1338 EXPORT_SYMBOL(cpufreq_get_policy);
1341 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1345 cpufreq_debug_disable_ratelimit();
1346 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1347 policy->min, policy->max);
1349 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
1351 /* verify the cpu speed can be set within this limit */
1352 ret = cpufreq_driver->verify(policy);
1356 /* adjust if necessary - all reasons */
1357 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1358 CPUFREQ_ADJUST, policy);
1360 /* adjust if necessary - hardware incompatibility*/
1361 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1362 CPUFREQ_INCOMPATIBLE, policy);
1364 /* verify the cpu speed can be set within this limit,
1365 which might be different to the first one */
1366 ret = cpufreq_driver->verify(policy);
1370 /* notification of the new policy */
1371 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1372 CPUFREQ_NOTIFY, policy);
1374 data->min = policy->min;
1375 data->max = policy->max;
1377 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1379 if (cpufreq_driver->setpolicy) {
1380 data->policy = policy->policy;
1381 dprintk("setting range\n");
1382 ret = cpufreq_driver->setpolicy(policy);
1384 if (policy->governor != data->governor) {
1385 /* save old, working values */
1386 struct cpufreq_governor *old_gov = data->governor;
1388 dprintk("governor switch\n");
1390 /* end old governor */
1392 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1394 /* start new governor */
1395 data->governor = policy->governor;
1396 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1397 /* new governor failed, so re-start old one */
1398 dprintk("starting governor %s failed\n", data->governor->name);
1400 data->governor = old_gov;
1401 __cpufreq_governor(data, CPUFREQ_GOV_START);
1406 /* might be a policy change, too, so fall through */
1408 dprintk("governor: change or update limits\n");
1409 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1413 cpufreq_debug_enable_ratelimit();
1418 * cpufreq_set_policy - set a new CPUFreq policy
1419 * @policy: policy to be set.
1421 * Sets a new CPU frequency and voltage scaling policy.
1423 int cpufreq_set_policy(struct cpufreq_policy *policy)
1426 struct cpufreq_policy *data;
1431 data = cpufreq_cpu_get(policy->cpu);
1436 mutex_lock(&data->lock);
1438 ret = __cpufreq_set_policy(data, policy);
1439 data->user_policy.min = data->min;
1440 data->user_policy.max = data->max;
1441 data->user_policy.policy = data->policy;
1442 data->user_policy.governor = data->governor;
1444 mutex_unlock(&data->lock);
1445 cpufreq_cpu_put(data);
1449 EXPORT_SYMBOL(cpufreq_set_policy);
1453 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1454 * @cpu: CPU which shall be re-evaluated
1456 * Usefull for policy notifiers which have different necessities
1457 * at different times.
1459 int cpufreq_update_policy(unsigned int cpu)
1461 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1462 struct cpufreq_policy policy;
1468 mutex_lock(&data->lock);
1470 dprintk("updating policy for CPU %u\n", cpu);
1471 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1472 policy.min = data->user_policy.min;
1473 policy.max = data->user_policy.max;
1474 policy.policy = data->user_policy.policy;
1475 policy.governor = data->user_policy.governor;
1477 /* BIOS might change freq behind our back
1478 -> ask driver for current freq and notify governors about a change */
1479 if (cpufreq_driver->get) {
1480 policy.cur = cpufreq_driver->get(cpu);
1482 dprintk("Driver did not initialize current freq");
1483 data->cur = policy.cur;
1485 if (data->cur != policy.cur)
1486 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1490 ret = __cpufreq_set_policy(data, &policy);
1492 mutex_unlock(&data->lock);
1494 cpufreq_cpu_put(data);
1497 EXPORT_SYMBOL(cpufreq_update_policy);
1499 #ifdef CONFIG_HOTPLUG_CPU
1500 static int cpufreq_cpu_callback(struct notifier_block *nfb,
1501 unsigned long action, void *hcpu)
1503 unsigned int cpu = (unsigned long)hcpu;
1504 struct cpufreq_policy *policy;
1505 struct sys_device *sys_dev;
1507 sys_dev = get_cpu_sysdev(cpu);
1512 cpufreq_add_dev(sys_dev);
1514 case CPU_DOWN_PREPARE:
1516 * We attempt to put this cpu in lowest frequency
1517 * possible before going down. This will permit
1518 * hardware-managed P-State to switch other related
1519 * threads to min or higher speeds if possible.
1521 policy = cpufreq_cpu_data[cpu];
1523 cpufreq_driver_target(policy, policy->min,
1524 CPUFREQ_RELATION_H);
1528 cpufreq_remove_dev(sys_dev);
1535 static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
1537 .notifier_call = cpufreq_cpu_callback,
1539 #endif /* CONFIG_HOTPLUG_CPU */
1541 /*********************************************************************
1542 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1543 *********************************************************************/
1546 * cpufreq_register_driver - register a CPU Frequency driver
1547 * @driver_data: A struct cpufreq_driver containing the values#
1548 * submitted by the CPU Frequency driver.
1550 * Registers a CPU Frequency driver to this core code. This code
1551 * returns zero on success, -EBUSY when another driver got here first
1552 * (and isn't unregistered in the meantime).
1555 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1557 unsigned long flags;
1560 if (!driver_data || !driver_data->verify || !driver_data->init ||
1561 ((!driver_data->setpolicy) && (!driver_data->target)))
1564 dprintk("trying to register driver %s\n", driver_data->name);
1566 if (driver_data->setpolicy)
1567 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1569 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1570 if (cpufreq_driver) {
1571 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1574 cpufreq_driver = driver_data;
1575 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1577 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1579 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1583 /* check for at least one working CPU */
1584 for (i=0; i<NR_CPUS; i++)
1585 if (cpufreq_cpu_data[i])
1588 /* if all ->init() calls failed, unregister */
1590 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1591 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1593 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1594 cpufreq_driver = NULL;
1595 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1600 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1601 dprintk("driver %s up and running\n", driver_data->name);
1602 cpufreq_debug_enable_ratelimit();
1607 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1611 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1613 * Unregister the current CPUFreq driver. Only call this if you have
1614 * the right to do so, i.e. if you have succeeded in initialising before!
1615 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1616 * currently not initialised.
1618 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1620 unsigned long flags;
1622 cpufreq_debug_disable_ratelimit();
1624 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1625 cpufreq_debug_enable_ratelimit();
1629 dprintk("unregistering driver %s\n", driver->name);
1631 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1632 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1634 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1635 cpufreq_driver = NULL;
1636 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1640 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);