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/config.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/notifier.h>
23 #include <linux/cpufreq.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/spinlock.h>
27 #include <linux/device.h>
28 #include <linux/slab.h>
29 #include <linux/cpu.h>
30 #include <linux/completion.h>
31 #include <linux/mutex.h>
33 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
36 * The "cpufreq driver" - the arch- or hardware-dependend 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, unsigned int event);
46 static void handle_update(void *data);
49 * Two notifier lists: the "policy" list is involved in the
50 * validation process for a new CPU frequency policy; the
51 * "transition" list for kernel code that needs to handle
52 * changes to devices when the CPU clock speed changes.
53 * The mutex locks both lists.
55 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
56 static BLOCKING_NOTIFIER_HEAD(cpufreq_transition_notifier_list);
59 static LIST_HEAD(cpufreq_governor_list);
60 static DEFINE_MUTEX (cpufreq_governor_mutex);
62 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
64 struct cpufreq_policy *data;
70 /* get the cpufreq driver */
71 spin_lock_irqsave(&cpufreq_driver_lock, flags);
76 if (!try_module_get(cpufreq_driver->owner))
81 data = cpufreq_cpu_data[cpu];
84 goto err_out_put_module;
86 if (!kobject_get(&data->kobj))
87 goto err_out_put_module;
89 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
93 module_put(cpufreq_driver->owner);
95 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
99 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
102 void cpufreq_cpu_put(struct cpufreq_policy *data)
104 kobject_put(&data->kobj);
105 module_put(cpufreq_driver->owner);
107 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
110 /*********************************************************************
111 * UNIFIED DEBUG HELPERS *
112 *********************************************************************/
113 #ifdef CONFIG_CPU_FREQ_DEBUG
115 /* what part(s) of the CPUfreq subsystem are debugged? */
116 static unsigned int debug;
118 /* is the debug output ratelimit'ed using printk_ratelimit? User can
119 * set or modify this value.
121 static unsigned int debug_ratelimit = 1;
123 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
124 * loading of a cpufreq driver, temporarily disabled when a new policy
125 * is set, and disabled upon cpufreq driver removal
127 static unsigned int disable_ratelimit = 1;
128 static DEFINE_SPINLOCK(disable_ratelimit_lock);
130 static void cpufreq_debug_enable_ratelimit(void)
134 spin_lock_irqsave(&disable_ratelimit_lock, flags);
135 if (disable_ratelimit)
137 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
140 static void cpufreq_debug_disable_ratelimit(void)
144 spin_lock_irqsave(&disable_ratelimit_lock, flags);
146 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
149 void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
158 spin_lock_irqsave(&disable_ratelimit_lock, flags);
159 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
160 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
163 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
165 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
168 len += vsnprintf(&s[len], (256 - len), fmt, args);
176 EXPORT_SYMBOL(cpufreq_debug_printk);
179 module_param(debug, uint, 0644);
180 MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
182 module_param(debug_ratelimit, uint, 0644);
183 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
185 #else /* !CONFIG_CPU_FREQ_DEBUG */
187 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
188 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
190 #endif /* CONFIG_CPU_FREQ_DEBUG */
193 /*********************************************************************
194 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
195 *********************************************************************/
198 * adjust_jiffies - adjust the system "loops_per_jiffy"
200 * This function alters the system "loops_per_jiffy" for the clock
201 * speed change. Note that loops_per_jiffy cannot be updated on SMP
202 * systems as each CPU might be scaled differently. So, use the arch
203 * per-CPU loops_per_jiffy value wherever possible.
206 static unsigned long l_p_j_ref;
207 static unsigned int l_p_j_ref_freq;
209 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
211 if (ci->flags & CPUFREQ_CONST_LOOPS)
214 if (!l_p_j_ref_freq) {
215 l_p_j_ref = loops_per_jiffy;
216 l_p_j_ref_freq = ci->old;
217 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
219 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
220 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
221 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
222 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
223 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
227 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
232 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
233 * on frequency transition.
235 * This function calls the transition notifiers and the "adjust_jiffies"
236 * function. It is called twice on all CPU frequency changes that have
239 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
241 struct cpufreq_policy *policy;
243 BUG_ON(irqs_disabled());
245 freqs->flags = cpufreq_driver->flags;
246 dprintk("notification %u of frequency transition to %u kHz\n",
249 policy = cpufreq_cpu_data[freqs->cpu];
252 case CPUFREQ_PRECHANGE:
253 /* detect if the driver reported a value as "old frequency"
254 * which is not equal to what the cpufreq core thinks is
257 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
258 if ((policy) && (policy->cpu == freqs->cpu) &&
259 (policy->cur) && (policy->cur != freqs->old)) {
260 dprintk("Warning: CPU frequency is"
261 " %u, cpufreq assumed %u kHz.\n",
262 freqs->old, policy->cur);
263 freqs->old = policy->cur;
266 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
267 CPUFREQ_PRECHANGE, freqs);
268 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
271 case CPUFREQ_POSTCHANGE:
272 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
273 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
274 CPUFREQ_POSTCHANGE, freqs);
275 if (likely(policy) && likely(policy->cpu == freqs->cpu))
276 policy->cur = freqs->new;
280 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
284 /*********************************************************************
286 *********************************************************************/
289 * cpufreq_parse_governor - parse a governor string
291 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
292 struct cpufreq_governor **governor)
296 if (cpufreq_driver->setpolicy) {
297 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
298 *policy = CPUFREQ_POLICY_PERFORMANCE;
300 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
301 *policy = CPUFREQ_POLICY_POWERSAVE;
306 struct cpufreq_governor *t;
307 mutex_lock(&cpufreq_governor_mutex);
308 if (!cpufreq_driver || !cpufreq_driver->target)
310 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
311 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
313 mutex_unlock(&cpufreq_governor_mutex);
318 mutex_unlock(&cpufreq_governor_mutex);
324 /* drivers/base/cpu.c */
325 extern struct sysdev_class cpu_sysdev_class;
329 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
331 * Write out information from cpufreq_driver->policy[cpu]; object must be
335 #define show_one(file_name, object) \
336 static ssize_t show_##file_name \
337 (struct cpufreq_policy * policy, char *buf) \
339 return sprintf (buf, "%u\n", policy->object); \
342 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
343 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
344 show_one(scaling_min_freq, min);
345 show_one(scaling_max_freq, max);
346 show_one(scaling_cur_freq, cur);
348 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
351 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
353 #define store_one(file_name, object) \
354 static ssize_t store_##file_name \
355 (struct cpufreq_policy * policy, const char *buf, size_t count) \
357 unsigned int ret = -EINVAL; \
358 struct cpufreq_policy new_policy; \
360 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
364 ret = sscanf (buf, "%u", &new_policy.object); \
368 mutex_lock(&policy->lock); \
369 ret = __cpufreq_set_policy(policy, &new_policy); \
370 policy->user_policy.object = policy->object; \
371 mutex_unlock(&policy->lock); \
373 return ret ? ret : count; \
376 store_one(scaling_min_freq,min);
377 store_one(scaling_max_freq,max);
380 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
382 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
384 unsigned int cur_freq = cpufreq_get(policy->cpu);
386 return sprintf(buf, "<unknown>");
387 return sprintf(buf, "%u\n", cur_freq);
392 * show_scaling_governor - show the current policy for the specified CPU
394 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
396 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
397 return sprintf(buf, "powersave\n");
398 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
399 return sprintf(buf, "performance\n");
400 else if (policy->governor)
401 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
407 * store_scaling_governor - store policy for the specified CPU
409 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
410 const char *buf, size_t count)
412 unsigned int ret = -EINVAL;
413 char str_governor[16];
414 struct cpufreq_policy new_policy;
416 ret = cpufreq_get_policy(&new_policy, policy->cpu);
420 ret = sscanf (buf, "%15s", str_governor);
424 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
427 /* Do not use cpufreq_set_policy here or the user_policy.max
428 will be wrongly overridden */
429 mutex_lock(&policy->lock);
430 ret = __cpufreq_set_policy(policy, &new_policy);
432 policy->user_policy.policy = policy->policy;
433 policy->user_policy.governor = policy->governor;
434 mutex_unlock(&policy->lock);
436 return ret ? ret : count;
440 * show_scaling_driver - show the cpufreq driver currently loaded
442 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
444 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
448 * show_scaling_available_governors - show the available CPUfreq governors
450 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
454 struct cpufreq_governor *t;
456 if (!cpufreq_driver->target) {
457 i += sprintf(buf, "performance powersave");
461 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
462 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
464 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
467 i += sprintf(&buf[i], "\n");
471 * show_affected_cpus - show the CPUs affected by each transition
473 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
478 for_each_cpu_mask(cpu, policy->cpus) {
480 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
481 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
482 if (i >= (PAGE_SIZE - 5))
485 i += sprintf(&buf[i], "\n");
490 #define define_one_ro(_name) \
491 static struct freq_attr _name = \
492 __ATTR(_name, 0444, show_##_name, NULL)
494 #define define_one_ro0400(_name) \
495 static struct freq_attr _name = \
496 __ATTR(_name, 0400, show_##_name, NULL)
498 #define define_one_rw(_name) \
499 static struct freq_attr _name = \
500 __ATTR(_name, 0644, show_##_name, store_##_name)
502 define_one_ro0400(cpuinfo_cur_freq);
503 define_one_ro(cpuinfo_min_freq);
504 define_one_ro(cpuinfo_max_freq);
505 define_one_ro(scaling_available_governors);
506 define_one_ro(scaling_driver);
507 define_one_ro(scaling_cur_freq);
508 define_one_ro(affected_cpus);
509 define_one_rw(scaling_min_freq);
510 define_one_rw(scaling_max_freq);
511 define_one_rw(scaling_governor);
513 static struct attribute * default_attrs[] = {
514 &cpuinfo_min_freq.attr,
515 &cpuinfo_max_freq.attr,
516 &scaling_min_freq.attr,
517 &scaling_max_freq.attr,
519 &scaling_governor.attr,
520 &scaling_driver.attr,
521 &scaling_available_governors.attr,
525 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
526 #define to_attr(a) container_of(a,struct freq_attr,attr)
528 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
530 struct cpufreq_policy * policy = to_policy(kobj);
531 struct freq_attr * fattr = to_attr(attr);
533 policy = cpufreq_cpu_get(policy->cpu);
536 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
537 cpufreq_cpu_put(policy);
541 static ssize_t store(struct kobject * kobj, struct attribute * attr,
542 const char * buf, size_t count)
544 struct cpufreq_policy * policy = to_policy(kobj);
545 struct freq_attr * fattr = to_attr(attr);
547 policy = cpufreq_cpu_get(policy->cpu);
550 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
551 cpufreq_cpu_put(policy);
555 static void cpufreq_sysfs_release(struct kobject * kobj)
557 struct cpufreq_policy * policy = to_policy(kobj);
558 dprintk("last reference is dropped\n");
559 complete(&policy->kobj_unregister);
562 static struct sysfs_ops sysfs_ops = {
567 static struct kobj_type ktype_cpufreq = {
568 .sysfs_ops = &sysfs_ops,
569 .default_attrs = default_attrs,
570 .release = cpufreq_sysfs_release,
575 * cpufreq_add_dev - add a CPU device
577 * Adds the cpufreq interface for a CPU device.
579 static int cpufreq_add_dev (struct sys_device * sys_dev)
581 unsigned int cpu = sys_dev->id;
583 struct cpufreq_policy new_policy;
584 struct cpufreq_policy *policy;
585 struct freq_attr **drv_attr;
586 struct sys_device *cpu_sys_dev;
590 struct cpufreq_policy *managed_policy;
593 if (cpu_is_offline(cpu))
596 cpufreq_debug_disable_ratelimit();
597 dprintk("adding CPU %u\n", cpu);
600 /* check whether a different CPU already registered this
601 * CPU because it is in the same boat. */
602 policy = cpufreq_cpu_get(cpu);
603 if (unlikely(policy)) {
604 cpufreq_cpu_put(policy);
605 cpufreq_debug_enable_ratelimit();
610 if (!try_module_get(cpufreq_driver->owner)) {
615 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
622 policy->cpus = cpumask_of_cpu(cpu);
624 mutex_init(&policy->lock);
625 mutex_lock(&policy->lock);
626 init_completion(&policy->kobj_unregister);
627 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
629 /* call driver. From then on the cpufreq must be able
630 * to accept all calls to ->verify and ->setpolicy for this CPU
632 ret = cpufreq_driver->init(policy);
634 dprintk("initialization failed\n");
635 mutex_unlock(&policy->lock);
640 for_each_cpu_mask(j, policy->cpus) {
644 /* check for existing affected CPUs. They may not be aware
645 * of it due to CPU Hotplug.
647 managed_policy = cpufreq_cpu_get(j);
648 if (unlikely(managed_policy)) {
649 spin_lock_irqsave(&cpufreq_driver_lock, flags);
650 managed_policy->cpus = policy->cpus;
651 cpufreq_cpu_data[cpu] = managed_policy;
652 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
654 dprintk("CPU already managed, adding link\n");
655 sysfs_create_link(&sys_dev->kobj,
656 &managed_policy->kobj, "cpufreq");
658 cpufreq_debug_enable_ratelimit();
659 mutex_unlock(&policy->lock);
661 goto err_out_driver_exit; /* call driver->exit() */
665 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
667 /* prepare interface data */
668 policy->kobj.parent = &sys_dev->kobj;
669 policy->kobj.ktype = &ktype_cpufreq;
670 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
672 ret = kobject_register(&policy->kobj);
674 mutex_unlock(&policy->lock);
675 goto err_out_driver_exit;
677 /* set up files for this cpu device */
678 drv_attr = cpufreq_driver->attr;
679 while ((drv_attr) && (*drv_attr)) {
680 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
683 if (cpufreq_driver->get)
684 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
685 if (cpufreq_driver->target)
686 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
688 spin_lock_irqsave(&cpufreq_driver_lock, flags);
689 for_each_cpu_mask(j, policy->cpus)
690 cpufreq_cpu_data[j] = policy;
691 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
693 /* symlink affected CPUs */
694 for_each_cpu_mask(j, policy->cpus) {
700 dprintk("CPU %u already managed, adding link\n", j);
701 cpufreq_cpu_get(cpu);
702 cpu_sys_dev = get_cpu_sysdev(j);
703 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
707 policy->governor = NULL; /* to assure that the starting sequence is
708 * run in cpufreq_set_policy */
709 mutex_unlock(&policy->lock);
711 /* set default policy */
712 ret = cpufreq_set_policy(&new_policy);
714 dprintk("setting policy failed\n");
715 goto err_out_unregister;
718 module_put(cpufreq_driver->owner);
719 dprintk("initialization complete\n");
720 cpufreq_debug_enable_ratelimit();
726 spin_lock_irqsave(&cpufreq_driver_lock, flags);
727 for_each_cpu_mask(j, policy->cpus)
728 cpufreq_cpu_data[j] = NULL;
729 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
731 kobject_unregister(&policy->kobj);
732 wait_for_completion(&policy->kobj_unregister);
735 if (cpufreq_driver->exit)
736 cpufreq_driver->exit(policy);
742 module_put(cpufreq_driver->owner);
744 cpufreq_debug_enable_ratelimit();
750 * cpufreq_remove_dev - remove a CPU device
752 * Removes the cpufreq interface for a CPU device.
754 static int cpufreq_remove_dev (struct sys_device * sys_dev)
756 unsigned int cpu = sys_dev->id;
758 struct cpufreq_policy *data;
760 struct sys_device *cpu_sys_dev;
764 cpufreq_debug_disable_ratelimit();
765 dprintk("unregistering CPU %u\n", cpu);
767 spin_lock_irqsave(&cpufreq_driver_lock, flags);
768 data = cpufreq_cpu_data[cpu];
771 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
772 cpufreq_debug_enable_ratelimit();
775 cpufreq_cpu_data[cpu] = NULL;
779 /* if this isn't the CPU which is the parent of the kobj, we
780 * only need to unlink, put and exit
782 if (unlikely(cpu != data->cpu)) {
783 dprintk("removing link\n");
784 cpu_clear(cpu, data->cpus);
785 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
786 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
787 cpufreq_cpu_put(data);
788 cpufreq_debug_enable_ratelimit();
794 if (!kobject_get(&data->kobj)) {
795 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
796 cpufreq_debug_enable_ratelimit();
801 /* if we have other CPUs still registered, we need to unlink them,
802 * or else wait_for_completion below will lock up. Clean the
803 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
806 if (unlikely(cpus_weight(data->cpus) > 1)) {
807 for_each_cpu_mask(j, data->cpus) {
810 cpufreq_cpu_data[j] = NULL;
814 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
816 if (unlikely(cpus_weight(data->cpus) > 1)) {
817 for_each_cpu_mask(j, data->cpus) {
820 dprintk("removing link for cpu %u\n", j);
821 cpu_sys_dev = get_cpu_sysdev(j);
822 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
823 cpufreq_cpu_put(data);
827 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
830 mutex_lock(&data->lock);
831 if (cpufreq_driver->target)
832 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
833 mutex_unlock(&data->lock);
835 kobject_unregister(&data->kobj);
837 kobject_put(&data->kobj);
839 /* we need to make sure that the underlying kobj is actually
840 * not referenced anymore by anybody before we proceed with
843 dprintk("waiting for dropping of refcount\n");
844 wait_for_completion(&data->kobj_unregister);
845 dprintk("wait complete\n");
847 if (cpufreq_driver->exit)
848 cpufreq_driver->exit(data);
852 cpufreq_debug_enable_ratelimit();
857 static void handle_update(void *data)
859 unsigned int cpu = (unsigned int)(long)data;
860 dprintk("handle_update for cpu %u called\n", cpu);
861 cpufreq_update_policy(cpu);
865 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
867 * @old_freq: CPU frequency the kernel thinks the CPU runs at
868 * @new_freq: CPU frequency the CPU actually runs at
870 * We adjust to current frequency first, and need to clean up later. So either call
871 * to cpufreq_update_policy() or schedule handle_update()).
873 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
875 struct cpufreq_freqs freqs;
877 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
878 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
881 freqs.old = old_freq;
882 freqs.new = new_freq;
883 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
884 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
889 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
892 * This is the last known freq, without actually getting it from the driver.
893 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
895 unsigned int cpufreq_quick_get(unsigned int cpu)
897 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
898 unsigned int ret = 0;
901 mutex_lock(&policy->lock);
903 mutex_unlock(&policy->lock);
904 cpufreq_cpu_put(policy);
909 EXPORT_SYMBOL(cpufreq_quick_get);
913 * cpufreq_get - get the current CPU frequency (in kHz)
916 * Get the CPU current (static) CPU frequency
918 unsigned int cpufreq_get(unsigned int cpu)
920 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
921 unsigned int ret = 0;
926 if (!cpufreq_driver->get)
929 mutex_lock(&policy->lock);
931 ret = cpufreq_driver->get(cpu);
933 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
934 /* verify no discrepancy between actual and saved value exists */
935 if (unlikely(ret != policy->cur)) {
936 cpufreq_out_of_sync(cpu, policy->cur, ret);
937 schedule_work(&policy->update);
941 mutex_unlock(&policy->lock);
944 cpufreq_cpu_put(policy);
948 EXPORT_SYMBOL(cpufreq_get);
952 * cpufreq_suspend - let the low level driver prepare for suspend
955 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
957 int cpu = sysdev->id;
958 unsigned int ret = 0;
959 unsigned int cur_freq = 0;
960 struct cpufreq_policy *cpu_policy;
962 dprintk("resuming cpu %u\n", cpu);
964 if (!cpu_online(cpu))
967 /* we may be lax here as interrupts are off. Nonetheless
968 * we need to grab the correct cpu policy, as to check
969 * whether we really run on this CPU.
972 cpu_policy = cpufreq_cpu_get(cpu);
976 /* only handle each CPU group once */
977 if (unlikely(cpu_policy->cpu != cpu)) {
978 cpufreq_cpu_put(cpu_policy);
982 if (cpufreq_driver->suspend) {
983 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
985 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
986 "step on CPU %u\n", cpu_policy->cpu);
987 cpufreq_cpu_put(cpu_policy);
993 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
996 if (cpufreq_driver->get)
997 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
999 if (!cur_freq || !cpu_policy->cur) {
1000 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1001 "frequency is what timing core thinks it is.\n");
1005 if (unlikely(cur_freq != cpu_policy->cur)) {
1006 struct cpufreq_freqs freqs;
1008 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1009 dprintk("Warning: CPU frequency is %u, "
1010 "cpufreq assumed %u kHz.\n",
1011 cur_freq, cpu_policy->cur);
1014 freqs.old = cpu_policy->cur;
1015 freqs.new = cur_freq;
1017 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
1018 CPUFREQ_SUSPENDCHANGE, &freqs);
1019 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1021 cpu_policy->cur = cur_freq;
1025 cpufreq_cpu_put(cpu_policy);
1030 * cpufreq_resume - restore proper CPU frequency handling after resume
1032 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1033 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
1034 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1037 static int cpufreq_resume(struct sys_device * sysdev)
1039 int cpu = sysdev->id;
1040 unsigned int ret = 0;
1041 struct cpufreq_policy *cpu_policy;
1043 dprintk("resuming cpu %u\n", cpu);
1045 if (!cpu_online(cpu))
1048 /* we may be lax here as interrupts are off. Nonetheless
1049 * we need to grab the correct cpu policy, as to check
1050 * whether we really run on this CPU.
1053 cpu_policy = cpufreq_cpu_get(cpu);
1057 /* only handle each CPU group once */
1058 if (unlikely(cpu_policy->cpu != cpu)) {
1059 cpufreq_cpu_put(cpu_policy);
1063 if (cpufreq_driver->resume) {
1064 ret = cpufreq_driver->resume(cpu_policy);
1066 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1067 "step on CPU %u\n", cpu_policy->cpu);
1068 cpufreq_cpu_put(cpu_policy);
1073 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1074 unsigned int cur_freq = 0;
1076 if (cpufreq_driver->get)
1077 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1079 if (!cur_freq || !cpu_policy->cur) {
1080 printk(KERN_ERR "cpufreq: resume failed to assert "
1081 "current frequency is what timing core "
1086 if (unlikely(cur_freq != cpu_policy->cur)) {
1087 struct cpufreq_freqs freqs;
1089 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1090 dprintk("Warning: CPU frequency"
1091 "is %u, cpufreq assumed %u kHz.\n",
1092 cur_freq, cpu_policy->cur);
1095 freqs.old = cpu_policy->cur;
1096 freqs.new = cur_freq;
1098 blocking_notifier_call_chain(
1099 &cpufreq_transition_notifier_list,
1100 CPUFREQ_RESUMECHANGE, &freqs);
1101 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1103 cpu_policy->cur = cur_freq;
1108 schedule_work(&cpu_policy->update);
1109 cpufreq_cpu_put(cpu_policy);
1113 static struct sysdev_driver cpufreq_sysdev_driver = {
1114 .add = cpufreq_add_dev,
1115 .remove = cpufreq_remove_dev,
1116 .suspend = cpufreq_suspend,
1117 .resume = cpufreq_resume,
1121 /*********************************************************************
1122 * NOTIFIER LISTS INTERFACE *
1123 *********************************************************************/
1126 * cpufreq_register_notifier - register a driver with cpufreq
1127 * @nb: notifier function to register
1128 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1130 * Add a driver to one of two lists: either a list of drivers that
1131 * are notified about clock rate changes (once before and once after
1132 * the transition), or a list of drivers that are notified about
1133 * changes in cpufreq policy.
1135 * This function may sleep, and has the same return conditions as
1136 * blocking_notifier_chain_register.
1138 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1143 case CPUFREQ_TRANSITION_NOTIFIER:
1144 ret = blocking_notifier_chain_register(
1145 &cpufreq_transition_notifier_list, nb);
1147 case CPUFREQ_POLICY_NOTIFIER:
1148 ret = blocking_notifier_chain_register(
1149 &cpufreq_policy_notifier_list, nb);
1157 EXPORT_SYMBOL(cpufreq_register_notifier);
1161 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1162 * @nb: notifier block to be unregistered
1163 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1165 * Remove a driver from the CPU frequency notifier list.
1167 * This function may sleep, and has the same return conditions as
1168 * blocking_notifier_chain_unregister.
1170 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1175 case CPUFREQ_TRANSITION_NOTIFIER:
1176 ret = blocking_notifier_chain_unregister(
1177 &cpufreq_transition_notifier_list, nb);
1179 case CPUFREQ_POLICY_NOTIFIER:
1180 ret = blocking_notifier_chain_unregister(
1181 &cpufreq_policy_notifier_list, nb);
1189 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1192 /*********************************************************************
1194 *********************************************************************/
1197 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1198 unsigned int target_freq,
1199 unsigned int relation)
1201 int retval = -EINVAL;
1204 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1205 target_freq, relation);
1206 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1207 retval = cpufreq_driver->target(policy, target_freq, relation);
1209 unlock_cpu_hotplug();
1213 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1215 int cpufreq_driver_target(struct cpufreq_policy *policy,
1216 unsigned int target_freq,
1217 unsigned int relation)
1221 policy = cpufreq_cpu_get(policy->cpu);
1225 mutex_lock(&policy->lock);
1227 ret = __cpufreq_driver_target(policy, target_freq, relation);
1229 mutex_unlock(&policy->lock);
1231 cpufreq_cpu_put(policy);
1234 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1237 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1241 if (!try_module_get(policy->governor->owner))
1244 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1245 ret = policy->governor->governor(policy, event);
1247 /* we keep one module reference alive for each CPU governed by this CPU */
1248 if ((event != CPUFREQ_GOV_START) || ret)
1249 module_put(policy->governor->owner);
1250 if ((event == CPUFREQ_GOV_STOP) && !ret)
1251 module_put(policy->governor->owner);
1257 int cpufreq_governor(unsigned int cpu, unsigned int event)
1260 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1265 mutex_lock(&policy->lock);
1266 ret = __cpufreq_governor(policy, event);
1267 mutex_unlock(&policy->lock);
1269 cpufreq_cpu_put(policy);
1272 EXPORT_SYMBOL_GPL(cpufreq_governor);
1275 int cpufreq_register_governor(struct cpufreq_governor *governor)
1277 struct cpufreq_governor *t;
1282 mutex_lock(&cpufreq_governor_mutex);
1284 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1285 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1286 mutex_unlock(&cpufreq_governor_mutex);
1290 list_add(&governor->governor_list, &cpufreq_governor_list);
1292 mutex_unlock(&cpufreq_governor_mutex);
1295 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1298 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1303 mutex_lock(&cpufreq_governor_mutex);
1304 list_del(&governor->governor_list);
1305 mutex_unlock(&cpufreq_governor_mutex);
1308 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1312 /*********************************************************************
1313 * POLICY INTERFACE *
1314 *********************************************************************/
1317 * cpufreq_get_policy - get the current cpufreq_policy
1318 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1320 * Reads the current cpufreq policy.
1322 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1324 struct cpufreq_policy *cpu_policy;
1328 cpu_policy = cpufreq_cpu_get(cpu);
1332 mutex_lock(&cpu_policy->lock);
1333 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1334 mutex_unlock(&cpu_policy->lock);
1336 cpufreq_cpu_put(cpu_policy);
1339 EXPORT_SYMBOL(cpufreq_get_policy);
1342 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1346 cpufreq_debug_disable_ratelimit();
1347 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1348 policy->min, policy->max);
1350 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
1352 /* verify the cpu speed can be set within this limit */
1353 ret = cpufreq_driver->verify(policy);
1357 /* adjust if necessary - all reasons */
1358 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1359 CPUFREQ_ADJUST, policy);
1361 /* adjust if necessary - hardware incompatibility*/
1362 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1363 CPUFREQ_INCOMPATIBLE, policy);
1365 /* verify the cpu speed can be set within this limit,
1366 which might be different to the first one */
1367 ret = cpufreq_driver->verify(policy);
1371 /* notification of the new policy */
1372 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1373 CPUFREQ_NOTIFY, policy);
1375 data->min = policy->min;
1376 data->max = policy->max;
1378 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1380 if (cpufreq_driver->setpolicy) {
1381 data->policy = policy->policy;
1382 dprintk("setting range\n");
1383 ret = cpufreq_driver->setpolicy(policy);
1385 if (policy->governor != data->governor) {
1386 /* save old, working values */
1387 struct cpufreq_governor *old_gov = data->governor;
1389 dprintk("governor switch\n");
1391 /* end old governor */
1393 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1395 /* start new governor */
1396 data->governor = policy->governor;
1397 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1398 /* new governor failed, so re-start old one */
1399 dprintk("starting governor %s failed\n", data->governor->name);
1401 data->governor = old_gov;
1402 __cpufreq_governor(data, CPUFREQ_GOV_START);
1407 /* might be a policy change, too, so fall through */
1409 dprintk("governor: change or update limits\n");
1410 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1414 cpufreq_debug_enable_ratelimit();
1419 * cpufreq_set_policy - set a new CPUFreq policy
1420 * @policy: policy to be set.
1422 * Sets a new CPU frequency and voltage scaling policy.
1424 int cpufreq_set_policy(struct cpufreq_policy *policy)
1427 struct cpufreq_policy *data;
1432 data = cpufreq_cpu_get(policy->cpu);
1437 mutex_lock(&data->lock);
1439 ret = __cpufreq_set_policy(data, policy);
1440 data->user_policy.min = data->min;
1441 data->user_policy.max = data->max;
1442 data->user_policy.policy = data->policy;
1443 data->user_policy.governor = data->governor;
1445 mutex_unlock(&data->lock);
1446 cpufreq_cpu_put(data);
1450 EXPORT_SYMBOL(cpufreq_set_policy);
1454 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1455 * @cpu: CPU which shall be re-evaluated
1457 * Usefull for policy notifiers which have different necessities
1458 * at different times.
1460 int cpufreq_update_policy(unsigned int cpu)
1462 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1463 struct cpufreq_policy policy;
1469 mutex_lock(&data->lock);
1471 dprintk("updating policy for CPU %u\n", cpu);
1472 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1473 policy.min = data->user_policy.min;
1474 policy.max = data->user_policy.max;
1475 policy.policy = data->user_policy.policy;
1476 policy.governor = data->user_policy.governor;
1478 /* BIOS might change freq behind our back
1479 -> ask driver for current freq and notify governors about a change */
1480 if (cpufreq_driver->get) {
1481 policy.cur = cpufreq_driver->get(cpu);
1483 dprintk("Driver did not initialize current freq");
1484 data->cur = policy.cur;
1486 if (data->cur != policy.cur)
1487 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1491 ret = __cpufreq_set_policy(data, &policy);
1493 mutex_unlock(&data->lock);
1495 cpufreq_cpu_put(data);
1498 EXPORT_SYMBOL(cpufreq_update_policy);
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 cpufreq_cpu_notifier =
1537 .notifier_call = cpufreq_cpu_callback,
1540 /*********************************************************************
1541 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1542 *********************************************************************/
1545 * cpufreq_register_driver - register a CPU Frequency driver
1546 * @driver_data: A struct cpufreq_driver containing the values#
1547 * submitted by the CPU Frequency driver.
1549 * Registers a CPU Frequency driver to this core code. This code
1550 * returns zero on success, -EBUSY when another driver got here first
1551 * (and isn't unregistered in the meantime).
1554 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1556 unsigned long flags;
1559 if (!driver_data || !driver_data->verify || !driver_data->init ||
1560 ((!driver_data->setpolicy) && (!driver_data->target)))
1563 dprintk("trying to register driver %s\n", driver_data->name);
1565 if (driver_data->setpolicy)
1566 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1568 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1569 if (cpufreq_driver) {
1570 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1573 cpufreq_driver = driver_data;
1574 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1576 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1578 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1582 /* check for at least one working CPU */
1583 for (i=0; i<NR_CPUS; i++)
1584 if (cpufreq_cpu_data[i])
1587 /* if all ->init() calls failed, unregister */
1589 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1590 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1592 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1593 cpufreq_driver = NULL;
1594 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1599 register_cpu_notifier(&cpufreq_cpu_notifier);
1600 dprintk("driver %s up and running\n", driver_data->name);
1601 cpufreq_debug_enable_ratelimit();
1606 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1610 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1612 * Unregister the current CPUFreq driver. Only call this if you have
1613 * the right to do so, i.e. if you have succeeded in initialising before!
1614 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1615 * currently not initialised.
1617 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1619 unsigned long flags;
1621 cpufreq_debug_disable_ratelimit();
1623 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1624 cpufreq_debug_enable_ratelimit();
1628 dprintk("unregistering driver %s\n", driver->name);
1630 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1631 unregister_cpu_notifier(&cpufreq_cpu_notifier);
1633 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1634 cpufreq_driver = NULL;
1635 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1639 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);