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-dependent 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 *********************************************************************/
287 static struct cpufreq_governor *__find_governor(const char *str_governor)
289 struct cpufreq_governor *t;
291 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
292 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN))
299 * cpufreq_parse_governor - parse a governor string
301 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
302 struct cpufreq_governor **governor)
309 if (cpufreq_driver->setpolicy) {
310 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
311 *policy = CPUFREQ_POLICY_PERFORMANCE;
313 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
314 *policy = CPUFREQ_POLICY_POWERSAVE;
317 } else if (cpufreq_driver->target) {
318 struct cpufreq_governor *t;
320 mutex_lock(&cpufreq_governor_mutex);
322 t = __find_governor(str_governor);
325 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s", str_governor);
330 mutex_unlock(&cpufreq_governor_mutex);
331 ret = request_module(name);
332 mutex_lock(&cpufreq_governor_mutex);
335 t = __find_governor(str_governor);
346 mutex_unlock(&cpufreq_governor_mutex);
353 /* drivers/base/cpu.c */
354 extern struct sysdev_class cpu_sysdev_class;
358 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
360 * Write out information from cpufreq_driver->policy[cpu]; object must be
364 #define show_one(file_name, object) \
365 static ssize_t show_##file_name \
366 (struct cpufreq_policy * policy, char *buf) \
368 return sprintf (buf, "%u\n", policy->object); \
371 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
372 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
373 show_one(scaling_min_freq, min);
374 show_one(scaling_max_freq, max);
375 show_one(scaling_cur_freq, cur);
377 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
380 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
382 #define store_one(file_name, object) \
383 static ssize_t store_##file_name \
384 (struct cpufreq_policy * policy, const char *buf, size_t count) \
386 unsigned int ret = -EINVAL; \
387 struct cpufreq_policy new_policy; \
389 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
393 ret = sscanf (buf, "%u", &new_policy.object); \
397 lock_cpu_hotplug(); \
398 mutex_lock(&policy->lock); \
399 ret = __cpufreq_set_policy(policy, &new_policy); \
400 policy->user_policy.object = policy->object; \
401 mutex_unlock(&policy->lock); \
402 unlock_cpu_hotplug(); \
404 return ret ? ret : count; \
407 store_one(scaling_min_freq,min);
408 store_one(scaling_max_freq,max);
411 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
413 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
415 unsigned int cur_freq = cpufreq_get(policy->cpu);
417 return sprintf(buf, "<unknown>");
418 return sprintf(buf, "%u\n", cur_freq);
423 * show_scaling_governor - show the current policy for the specified CPU
425 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
427 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
428 return sprintf(buf, "powersave\n");
429 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
430 return sprintf(buf, "performance\n");
431 else if (policy->governor)
432 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
438 * store_scaling_governor - store policy for the specified CPU
440 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
441 const char *buf, size_t count)
443 unsigned int ret = -EINVAL;
444 char str_governor[16];
445 struct cpufreq_policy new_policy;
447 ret = cpufreq_get_policy(&new_policy, policy->cpu);
451 ret = sscanf (buf, "%15s", str_governor);
455 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
460 /* Do not use cpufreq_set_policy here or the user_policy.max
461 will be wrongly overridden */
462 mutex_lock(&policy->lock);
463 ret = __cpufreq_set_policy(policy, &new_policy);
465 policy->user_policy.policy = policy->policy;
466 policy->user_policy.governor = policy->governor;
467 mutex_unlock(&policy->lock);
469 unlock_cpu_hotplug();
471 return ret ? ret : count;
475 * show_scaling_driver - show the cpufreq driver currently loaded
477 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
479 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
483 * show_scaling_available_governors - show the available CPUfreq governors
485 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
489 struct cpufreq_governor *t;
491 if (!cpufreq_driver->target) {
492 i += sprintf(buf, "performance powersave");
496 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
497 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
499 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
502 i += sprintf(&buf[i], "\n");
506 * show_affected_cpus - show the CPUs affected by each transition
508 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
513 for_each_cpu_mask(cpu, policy->cpus) {
515 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
516 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
517 if (i >= (PAGE_SIZE - 5))
520 i += sprintf(&buf[i], "\n");
525 #define define_one_ro(_name) \
526 static struct freq_attr _name = \
527 __ATTR(_name, 0444, show_##_name, NULL)
529 #define define_one_ro0400(_name) \
530 static struct freq_attr _name = \
531 __ATTR(_name, 0400, show_##_name, NULL)
533 #define define_one_rw(_name) \
534 static struct freq_attr _name = \
535 __ATTR(_name, 0644, show_##_name, store_##_name)
537 define_one_ro0400(cpuinfo_cur_freq);
538 define_one_ro(cpuinfo_min_freq);
539 define_one_ro(cpuinfo_max_freq);
540 define_one_ro(scaling_available_governors);
541 define_one_ro(scaling_driver);
542 define_one_ro(scaling_cur_freq);
543 define_one_ro(affected_cpus);
544 define_one_rw(scaling_min_freq);
545 define_one_rw(scaling_max_freq);
546 define_one_rw(scaling_governor);
548 static struct attribute * default_attrs[] = {
549 &cpuinfo_min_freq.attr,
550 &cpuinfo_max_freq.attr,
551 &scaling_min_freq.attr,
552 &scaling_max_freq.attr,
554 &scaling_governor.attr,
555 &scaling_driver.attr,
556 &scaling_available_governors.attr,
560 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
561 #define to_attr(a) container_of(a,struct freq_attr,attr)
563 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
565 struct cpufreq_policy * policy = to_policy(kobj);
566 struct freq_attr * fattr = to_attr(attr);
568 policy = cpufreq_cpu_get(policy->cpu);
571 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
572 cpufreq_cpu_put(policy);
576 static ssize_t store(struct kobject * kobj, struct attribute * attr,
577 const char * buf, size_t count)
579 struct cpufreq_policy * policy = to_policy(kobj);
580 struct freq_attr * fattr = to_attr(attr);
582 policy = cpufreq_cpu_get(policy->cpu);
585 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
586 cpufreq_cpu_put(policy);
590 static void cpufreq_sysfs_release(struct kobject * kobj)
592 struct cpufreq_policy * policy = to_policy(kobj);
593 dprintk("last reference is dropped\n");
594 complete(&policy->kobj_unregister);
597 static struct sysfs_ops sysfs_ops = {
602 static struct kobj_type ktype_cpufreq = {
603 .sysfs_ops = &sysfs_ops,
604 .default_attrs = default_attrs,
605 .release = cpufreq_sysfs_release,
610 * cpufreq_add_dev - add a CPU device
612 * Adds the cpufreq interface for a CPU device.
614 static int cpufreq_add_dev (struct sys_device * sys_dev)
616 unsigned int cpu = sys_dev->id;
618 struct cpufreq_policy new_policy;
619 struct cpufreq_policy *policy;
620 struct freq_attr **drv_attr;
621 struct sys_device *cpu_sys_dev;
625 struct cpufreq_policy *managed_policy;
628 if (cpu_is_offline(cpu))
631 cpufreq_debug_disable_ratelimit();
632 dprintk("adding CPU %u\n", cpu);
635 /* check whether a different CPU already registered this
636 * CPU because it is in the same boat. */
637 policy = cpufreq_cpu_get(cpu);
638 if (unlikely(policy)) {
639 cpufreq_cpu_put(policy);
640 cpufreq_debug_enable_ratelimit();
645 if (!try_module_get(cpufreq_driver->owner)) {
650 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
657 policy->cpus = cpumask_of_cpu(cpu);
659 mutex_init(&policy->lock);
660 mutex_lock(&policy->lock);
661 init_completion(&policy->kobj_unregister);
662 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
664 /* call driver. From then on the cpufreq must be able
665 * to accept all calls to ->verify and ->setpolicy for this CPU
667 ret = cpufreq_driver->init(policy);
669 dprintk("initialization failed\n");
670 mutex_unlock(&policy->lock);
675 for_each_cpu_mask(j, policy->cpus) {
679 /* check for existing affected CPUs. They may not be aware
680 * of it due to CPU Hotplug.
682 managed_policy = cpufreq_cpu_get(j);
683 if (unlikely(managed_policy)) {
684 spin_lock_irqsave(&cpufreq_driver_lock, flags);
685 managed_policy->cpus = policy->cpus;
686 cpufreq_cpu_data[cpu] = managed_policy;
687 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
689 dprintk("CPU already managed, adding link\n");
690 sysfs_create_link(&sys_dev->kobj,
691 &managed_policy->kobj, "cpufreq");
693 cpufreq_debug_enable_ratelimit();
694 mutex_unlock(&policy->lock);
696 goto err_out_driver_exit; /* call driver->exit() */
700 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
702 /* prepare interface data */
703 policy->kobj.parent = &sys_dev->kobj;
704 policy->kobj.ktype = &ktype_cpufreq;
705 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
707 ret = kobject_register(&policy->kobj);
709 mutex_unlock(&policy->lock);
710 goto err_out_driver_exit;
712 /* set up files for this cpu device */
713 drv_attr = cpufreq_driver->attr;
714 while ((drv_attr) && (*drv_attr)) {
715 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
718 if (cpufreq_driver->get)
719 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
720 if (cpufreq_driver->target)
721 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
723 spin_lock_irqsave(&cpufreq_driver_lock, flags);
724 for_each_cpu_mask(j, policy->cpus)
725 cpufreq_cpu_data[j] = policy;
726 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
728 /* symlink affected CPUs */
729 for_each_cpu_mask(j, policy->cpus) {
735 dprintk("CPU %u already managed, adding link\n", j);
736 cpufreq_cpu_get(cpu);
737 cpu_sys_dev = get_cpu_sysdev(j);
738 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
742 policy->governor = NULL; /* to assure that the starting sequence is
743 * run in cpufreq_set_policy */
744 mutex_unlock(&policy->lock);
746 /* set default policy */
747 ret = cpufreq_set_policy(&new_policy);
749 dprintk("setting policy failed\n");
750 goto err_out_unregister;
753 module_put(cpufreq_driver->owner);
754 dprintk("initialization complete\n");
755 cpufreq_debug_enable_ratelimit();
761 spin_lock_irqsave(&cpufreq_driver_lock, flags);
762 for_each_cpu_mask(j, policy->cpus)
763 cpufreq_cpu_data[j] = NULL;
764 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
766 kobject_unregister(&policy->kobj);
767 wait_for_completion(&policy->kobj_unregister);
770 if (cpufreq_driver->exit)
771 cpufreq_driver->exit(policy);
777 module_put(cpufreq_driver->owner);
779 cpufreq_debug_enable_ratelimit();
785 * cpufreq_remove_dev - remove a CPU device
787 * Removes the cpufreq interface for a CPU device.
789 static int cpufreq_remove_dev (struct sys_device * sys_dev)
791 unsigned int cpu = sys_dev->id;
793 struct cpufreq_policy *data;
795 struct sys_device *cpu_sys_dev;
799 cpufreq_debug_disable_ratelimit();
800 dprintk("unregistering CPU %u\n", cpu);
802 spin_lock_irqsave(&cpufreq_driver_lock, flags);
803 data = cpufreq_cpu_data[cpu];
806 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
807 cpufreq_debug_enable_ratelimit();
810 cpufreq_cpu_data[cpu] = NULL;
814 /* if this isn't the CPU which is the parent of the kobj, we
815 * only need to unlink, put and exit
817 if (unlikely(cpu != data->cpu)) {
818 dprintk("removing link\n");
819 cpu_clear(cpu, data->cpus);
820 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
821 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
822 cpufreq_cpu_put(data);
823 cpufreq_debug_enable_ratelimit();
829 if (!kobject_get(&data->kobj)) {
830 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
831 cpufreq_debug_enable_ratelimit();
836 /* if we have other CPUs still registered, we need to unlink them,
837 * or else wait_for_completion below will lock up. Clean the
838 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
841 if (unlikely(cpus_weight(data->cpus) > 1)) {
842 for_each_cpu_mask(j, data->cpus) {
845 cpufreq_cpu_data[j] = NULL;
849 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
851 if (unlikely(cpus_weight(data->cpus) > 1)) {
852 for_each_cpu_mask(j, data->cpus) {
855 dprintk("removing link for cpu %u\n", j);
856 cpu_sys_dev = get_cpu_sysdev(j);
857 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
858 cpufreq_cpu_put(data);
862 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
865 mutex_lock(&data->lock);
866 if (cpufreq_driver->target)
867 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
868 mutex_unlock(&data->lock);
870 kobject_unregister(&data->kobj);
872 kobject_put(&data->kobj);
874 /* we need to make sure that the underlying kobj is actually
875 * not referenced anymore by anybody before we proceed with
878 dprintk("waiting for dropping of refcount\n");
879 wait_for_completion(&data->kobj_unregister);
880 dprintk("wait complete\n");
882 if (cpufreq_driver->exit)
883 cpufreq_driver->exit(data);
887 cpufreq_debug_enable_ratelimit();
892 static void handle_update(void *data)
894 unsigned int cpu = (unsigned int)(long)data;
895 dprintk("handle_update for cpu %u called\n", cpu);
896 cpufreq_update_policy(cpu);
900 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
902 * @old_freq: CPU frequency the kernel thinks the CPU runs at
903 * @new_freq: CPU frequency the CPU actually runs at
905 * We adjust to current frequency first, and need to clean up later. So either call
906 * to cpufreq_update_policy() or schedule handle_update()).
908 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
910 struct cpufreq_freqs freqs;
912 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
913 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
916 freqs.old = old_freq;
917 freqs.new = new_freq;
918 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
919 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
924 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
927 * This is the last known freq, without actually getting it from the driver.
928 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
930 unsigned int cpufreq_quick_get(unsigned int cpu)
932 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
933 unsigned int ret = 0;
936 mutex_lock(&policy->lock);
938 mutex_unlock(&policy->lock);
939 cpufreq_cpu_put(policy);
944 EXPORT_SYMBOL(cpufreq_quick_get);
948 * cpufreq_get - get the current CPU frequency (in kHz)
951 * Get the CPU current (static) CPU frequency
953 unsigned int cpufreq_get(unsigned int cpu)
955 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
956 unsigned int ret = 0;
961 if (!cpufreq_driver->get)
964 mutex_lock(&policy->lock);
966 ret = cpufreq_driver->get(cpu);
968 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
969 /* verify no discrepancy between actual and saved value exists */
970 if (unlikely(ret != policy->cur)) {
971 cpufreq_out_of_sync(cpu, policy->cur, ret);
972 schedule_work(&policy->update);
976 mutex_unlock(&policy->lock);
979 cpufreq_cpu_put(policy);
983 EXPORT_SYMBOL(cpufreq_get);
987 * cpufreq_suspend - let the low level driver prepare for suspend
990 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
992 int cpu = sysdev->id;
993 unsigned int ret = 0;
994 unsigned int cur_freq = 0;
995 struct cpufreq_policy *cpu_policy;
997 dprintk("suspending cpu %u\n", cpu);
999 if (!cpu_online(cpu))
1002 /* we may be lax here as interrupts are off. Nonetheless
1003 * we need to grab the correct cpu policy, as to check
1004 * whether we really run on this CPU.
1007 cpu_policy = cpufreq_cpu_get(cpu);
1011 /* only handle each CPU group once */
1012 if (unlikely(cpu_policy->cpu != cpu)) {
1013 cpufreq_cpu_put(cpu_policy);
1017 if (cpufreq_driver->suspend) {
1018 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
1020 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1021 "step on CPU %u\n", cpu_policy->cpu);
1022 cpufreq_cpu_put(cpu_policy);
1028 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
1031 if (cpufreq_driver->get)
1032 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1034 if (!cur_freq || !cpu_policy->cur) {
1035 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1036 "frequency is what timing core thinks it is.\n");
1040 if (unlikely(cur_freq != cpu_policy->cur)) {
1041 struct cpufreq_freqs freqs;
1043 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1044 dprintk("Warning: CPU frequency is %u, "
1045 "cpufreq assumed %u kHz.\n",
1046 cur_freq, cpu_policy->cur);
1049 freqs.old = cpu_policy->cur;
1050 freqs.new = cur_freq;
1052 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
1053 CPUFREQ_SUSPENDCHANGE, &freqs);
1054 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1056 cpu_policy->cur = cur_freq;
1060 cpufreq_cpu_put(cpu_policy);
1065 * cpufreq_resume - restore proper CPU frequency handling after resume
1067 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1068 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
1069 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1072 static int cpufreq_resume(struct sys_device * sysdev)
1074 int cpu = sysdev->id;
1075 unsigned int ret = 0;
1076 struct cpufreq_policy *cpu_policy;
1078 dprintk("resuming cpu %u\n", cpu);
1080 if (!cpu_online(cpu))
1083 /* we may be lax here as interrupts are off. Nonetheless
1084 * we need to grab the correct cpu policy, as to check
1085 * whether we really run on this CPU.
1088 cpu_policy = cpufreq_cpu_get(cpu);
1092 /* only handle each CPU group once */
1093 if (unlikely(cpu_policy->cpu != cpu)) {
1094 cpufreq_cpu_put(cpu_policy);
1098 if (cpufreq_driver->resume) {
1099 ret = cpufreq_driver->resume(cpu_policy);
1101 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1102 "step on CPU %u\n", cpu_policy->cpu);
1103 cpufreq_cpu_put(cpu_policy);
1108 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1109 unsigned int cur_freq = 0;
1111 if (cpufreq_driver->get)
1112 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1114 if (!cur_freq || !cpu_policy->cur) {
1115 printk(KERN_ERR "cpufreq: resume failed to assert "
1116 "current frequency is what timing core "
1121 if (unlikely(cur_freq != cpu_policy->cur)) {
1122 struct cpufreq_freqs freqs;
1124 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1125 dprintk("Warning: CPU frequency"
1126 "is %u, cpufreq assumed %u kHz.\n",
1127 cur_freq, cpu_policy->cur);
1130 freqs.old = cpu_policy->cur;
1131 freqs.new = cur_freq;
1133 blocking_notifier_call_chain(
1134 &cpufreq_transition_notifier_list,
1135 CPUFREQ_RESUMECHANGE, &freqs);
1136 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1138 cpu_policy->cur = cur_freq;
1143 schedule_work(&cpu_policy->update);
1144 cpufreq_cpu_put(cpu_policy);
1148 static struct sysdev_driver cpufreq_sysdev_driver = {
1149 .add = cpufreq_add_dev,
1150 .remove = cpufreq_remove_dev,
1151 .suspend = cpufreq_suspend,
1152 .resume = cpufreq_resume,
1156 /*********************************************************************
1157 * NOTIFIER LISTS INTERFACE *
1158 *********************************************************************/
1161 * cpufreq_register_notifier - register a driver with cpufreq
1162 * @nb: notifier function to register
1163 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1165 * Add a driver to one of two lists: either a list of drivers that
1166 * are notified about clock rate changes (once before and once after
1167 * the transition), or a list of drivers that are notified about
1168 * changes in cpufreq policy.
1170 * This function may sleep, and has the same return conditions as
1171 * blocking_notifier_chain_register.
1173 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1178 case CPUFREQ_TRANSITION_NOTIFIER:
1179 ret = blocking_notifier_chain_register(
1180 &cpufreq_transition_notifier_list, nb);
1182 case CPUFREQ_POLICY_NOTIFIER:
1183 ret = blocking_notifier_chain_register(
1184 &cpufreq_policy_notifier_list, nb);
1192 EXPORT_SYMBOL(cpufreq_register_notifier);
1196 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1197 * @nb: notifier block to be unregistered
1198 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1200 * Remove a driver from the CPU frequency notifier list.
1202 * This function may sleep, and has the same return conditions as
1203 * blocking_notifier_chain_unregister.
1205 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1210 case CPUFREQ_TRANSITION_NOTIFIER:
1211 ret = blocking_notifier_chain_unregister(
1212 &cpufreq_transition_notifier_list, nb);
1214 case CPUFREQ_POLICY_NOTIFIER:
1215 ret = blocking_notifier_chain_unregister(
1216 &cpufreq_policy_notifier_list, nb);
1224 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1227 /*********************************************************************
1229 *********************************************************************/
1232 /* Must be called with lock_cpu_hotplug held */
1233 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1234 unsigned int target_freq,
1235 unsigned int relation)
1237 int retval = -EINVAL;
1239 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1240 target_freq, relation);
1241 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1242 retval = cpufreq_driver->target(policy, target_freq, relation);
1246 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1248 int cpufreq_driver_target(struct cpufreq_policy *policy,
1249 unsigned int target_freq,
1250 unsigned int relation)
1254 policy = cpufreq_cpu_get(policy->cpu);
1259 mutex_lock(&policy->lock);
1261 ret = __cpufreq_driver_target(policy, target_freq, relation);
1263 mutex_unlock(&policy->lock);
1264 unlock_cpu_hotplug();
1266 cpufreq_cpu_put(policy);
1269 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1272 * Locking: Must be called with the lock_cpu_hotplug() lock held
1273 * when "event" is CPUFREQ_GOV_LIMITS
1276 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1280 if (!try_module_get(policy->governor->owner))
1283 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1284 ret = policy->governor->governor(policy, event);
1286 /* we keep one module reference alive for each CPU governed by this CPU */
1287 if ((event != CPUFREQ_GOV_START) || ret)
1288 module_put(policy->governor->owner);
1289 if ((event == CPUFREQ_GOV_STOP) && !ret)
1290 module_put(policy->governor->owner);
1296 int cpufreq_register_governor(struct cpufreq_governor *governor)
1303 mutex_lock(&cpufreq_governor_mutex);
1306 if (__find_governor(governor->name) == NULL) {
1308 list_add(&governor->governor_list, &cpufreq_governor_list);
1311 mutex_unlock(&cpufreq_governor_mutex);
1314 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1317 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1322 mutex_lock(&cpufreq_governor_mutex);
1323 list_del(&governor->governor_list);
1324 mutex_unlock(&cpufreq_governor_mutex);
1327 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1331 /*********************************************************************
1332 * POLICY INTERFACE *
1333 *********************************************************************/
1336 * cpufreq_get_policy - get the current cpufreq_policy
1337 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1339 * Reads the current cpufreq policy.
1341 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1343 struct cpufreq_policy *cpu_policy;
1347 cpu_policy = cpufreq_cpu_get(cpu);
1351 mutex_lock(&cpu_policy->lock);
1352 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1353 mutex_unlock(&cpu_policy->lock);
1355 cpufreq_cpu_put(cpu_policy);
1358 EXPORT_SYMBOL(cpufreq_get_policy);
1362 * Locking: Must be called with the lock_cpu_hotplug() lock held
1364 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1368 cpufreq_debug_disable_ratelimit();
1369 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1370 policy->min, policy->max);
1372 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
1374 if (policy->min > data->min && policy->min > policy->max) {
1379 /* verify the cpu speed can be set within this limit */
1380 ret = cpufreq_driver->verify(policy);
1384 /* adjust if necessary - all reasons */
1385 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1386 CPUFREQ_ADJUST, policy);
1388 /* adjust if necessary - hardware incompatibility*/
1389 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1390 CPUFREQ_INCOMPATIBLE, policy);
1392 /* verify the cpu speed can be set within this limit,
1393 which might be different to the first one */
1394 ret = cpufreq_driver->verify(policy);
1398 /* notification of the new policy */
1399 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1400 CPUFREQ_NOTIFY, policy);
1402 data->min = policy->min;
1403 data->max = policy->max;
1405 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1407 if (cpufreq_driver->setpolicy) {
1408 data->policy = policy->policy;
1409 dprintk("setting range\n");
1410 ret = cpufreq_driver->setpolicy(policy);
1412 if (policy->governor != data->governor) {
1413 /* save old, working values */
1414 struct cpufreq_governor *old_gov = data->governor;
1416 dprintk("governor switch\n");
1418 /* end old governor */
1420 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1422 /* start new governor */
1423 data->governor = policy->governor;
1424 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1425 /* new governor failed, so re-start old one */
1426 dprintk("starting governor %s failed\n", data->governor->name);
1428 data->governor = old_gov;
1429 __cpufreq_governor(data, CPUFREQ_GOV_START);
1434 /* might be a policy change, too, so fall through */
1436 dprintk("governor: change or update limits\n");
1437 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1441 cpufreq_debug_enable_ratelimit();
1446 * cpufreq_set_policy - set a new CPUFreq policy
1447 * @policy: policy to be set.
1449 * Sets a new CPU frequency and voltage scaling policy.
1451 int cpufreq_set_policy(struct cpufreq_policy *policy)
1454 struct cpufreq_policy *data;
1459 data = cpufreq_cpu_get(policy->cpu);
1466 mutex_lock(&data->lock);
1468 ret = __cpufreq_set_policy(data, policy);
1469 data->user_policy.min = data->min;
1470 data->user_policy.max = data->max;
1471 data->user_policy.policy = data->policy;
1472 data->user_policy.governor = data->governor;
1474 mutex_unlock(&data->lock);
1476 unlock_cpu_hotplug();
1477 cpufreq_cpu_put(data);
1481 EXPORT_SYMBOL(cpufreq_set_policy);
1485 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1486 * @cpu: CPU which shall be re-evaluated
1488 * Usefull for policy notifiers which have different necessities
1489 * at different times.
1491 int cpufreq_update_policy(unsigned int cpu)
1493 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1494 struct cpufreq_policy policy;
1501 mutex_lock(&data->lock);
1503 dprintk("updating policy for CPU %u\n", cpu);
1504 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1505 policy.min = data->user_policy.min;
1506 policy.max = data->user_policy.max;
1507 policy.policy = data->user_policy.policy;
1508 policy.governor = data->user_policy.governor;
1510 /* BIOS might change freq behind our back
1511 -> ask driver for current freq and notify governors about a change */
1512 if (cpufreq_driver->get) {
1513 policy.cur = cpufreq_driver->get(cpu);
1515 dprintk("Driver did not initialize current freq");
1516 data->cur = policy.cur;
1518 if (data->cur != policy.cur)
1519 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1523 ret = __cpufreq_set_policy(data, &policy);
1525 mutex_unlock(&data->lock);
1526 unlock_cpu_hotplug();
1527 cpufreq_cpu_put(data);
1530 EXPORT_SYMBOL(cpufreq_update_policy);
1532 #ifdef CONFIG_HOTPLUG_CPU
1533 static int cpufreq_cpu_callback(struct notifier_block *nfb,
1534 unsigned long action, void *hcpu)
1536 unsigned int cpu = (unsigned long)hcpu;
1537 struct cpufreq_policy *policy;
1538 struct sys_device *sys_dev;
1540 sys_dev = get_cpu_sysdev(cpu);
1545 cpufreq_add_dev(sys_dev);
1547 case CPU_DOWN_PREPARE:
1549 * We attempt to put this cpu in lowest frequency
1550 * possible before going down. This will permit
1551 * hardware-managed P-State to switch other related
1552 * threads to min or higher speeds if possible.
1554 policy = cpufreq_cpu_data[cpu];
1556 cpufreq_driver_target(policy, policy->min,
1557 CPUFREQ_RELATION_H);
1561 cpufreq_remove_dev(sys_dev);
1568 static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
1570 .notifier_call = cpufreq_cpu_callback,
1572 #endif /* CONFIG_HOTPLUG_CPU */
1574 /*********************************************************************
1575 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1576 *********************************************************************/
1579 * cpufreq_register_driver - register a CPU Frequency driver
1580 * @driver_data: A struct cpufreq_driver containing the values#
1581 * submitted by the CPU Frequency driver.
1583 * Registers a CPU Frequency driver to this core code. This code
1584 * returns zero on success, -EBUSY when another driver got here first
1585 * (and isn't unregistered in the meantime).
1588 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1590 unsigned long flags;
1593 if (!driver_data || !driver_data->verify || !driver_data->init ||
1594 ((!driver_data->setpolicy) && (!driver_data->target)))
1597 dprintk("trying to register driver %s\n", driver_data->name);
1599 if (driver_data->setpolicy)
1600 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1602 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1603 if (cpufreq_driver) {
1604 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1607 cpufreq_driver = driver_data;
1608 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1610 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1612 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1616 /* check for at least one working CPU */
1617 for (i=0; i<NR_CPUS; i++)
1618 if (cpufreq_cpu_data[i])
1621 /* if all ->init() calls failed, unregister */
1623 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1624 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1626 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1627 cpufreq_driver = NULL;
1628 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1633 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1634 dprintk("driver %s up and running\n", driver_data->name);
1635 cpufreq_debug_enable_ratelimit();
1640 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1644 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1646 * Unregister the current CPUFreq driver. Only call this if you have
1647 * the right to do so, i.e. if you have succeeded in initialising before!
1648 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1649 * currently not initialised.
1651 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1653 unsigned long flags;
1655 cpufreq_debug_disable_ratelimit();
1657 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1658 cpufreq_debug_enable_ratelimit();
1662 dprintk("unregistering driver %s\n", driver->name);
1664 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1665 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1667 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1668 cpufreq_driver = NULL;
1669 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1673 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);