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 lock_cpu_hotplug(); \
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); \
372 unlock_cpu_hotplug(); \
374 return ret ? ret : count; \
377 store_one(scaling_min_freq,min);
378 store_one(scaling_max_freq,max);
381 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
383 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
385 unsigned int cur_freq = cpufreq_get(policy->cpu);
387 return sprintf(buf, "<unknown>");
388 return sprintf(buf, "%u\n", cur_freq);
393 * show_scaling_governor - show the current policy for the specified CPU
395 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
397 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
398 return sprintf(buf, "powersave\n");
399 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
400 return sprintf(buf, "performance\n");
401 else if (policy->governor)
402 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
408 * store_scaling_governor - store policy for the specified CPU
410 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
411 const char *buf, size_t count)
413 unsigned int ret = -EINVAL;
414 char str_governor[16];
415 struct cpufreq_policy new_policy;
417 ret = cpufreq_get_policy(&new_policy, policy->cpu);
421 ret = sscanf (buf, "%15s", str_governor);
425 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
430 /* Do not use cpufreq_set_policy here or the user_policy.max
431 will be wrongly overridden */
432 mutex_lock(&policy->lock);
433 ret = __cpufreq_set_policy(policy, &new_policy);
435 policy->user_policy.policy = policy->policy;
436 policy->user_policy.governor = policy->governor;
437 mutex_unlock(&policy->lock);
439 unlock_cpu_hotplug();
441 return ret ? ret : count;
445 * show_scaling_driver - show the cpufreq driver currently loaded
447 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
449 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
453 * show_scaling_available_governors - show the available CPUfreq governors
455 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
459 struct cpufreq_governor *t;
461 if (!cpufreq_driver->target) {
462 i += sprintf(buf, "performance powersave");
466 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
467 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
469 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
472 i += sprintf(&buf[i], "\n");
476 * show_affected_cpus - show the CPUs affected by each transition
478 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
483 for_each_cpu_mask(cpu, policy->cpus) {
485 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
486 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
487 if (i >= (PAGE_SIZE - 5))
490 i += sprintf(&buf[i], "\n");
495 #define define_one_ro(_name) \
496 static struct freq_attr _name = \
497 __ATTR(_name, 0444, show_##_name, NULL)
499 #define define_one_ro0400(_name) \
500 static struct freq_attr _name = \
501 __ATTR(_name, 0400, show_##_name, NULL)
503 #define define_one_rw(_name) \
504 static struct freq_attr _name = \
505 __ATTR(_name, 0644, show_##_name, store_##_name)
507 define_one_ro0400(cpuinfo_cur_freq);
508 define_one_ro(cpuinfo_min_freq);
509 define_one_ro(cpuinfo_max_freq);
510 define_one_ro(scaling_available_governors);
511 define_one_ro(scaling_driver);
512 define_one_ro(scaling_cur_freq);
513 define_one_ro(affected_cpus);
514 define_one_rw(scaling_min_freq);
515 define_one_rw(scaling_max_freq);
516 define_one_rw(scaling_governor);
518 static struct attribute * default_attrs[] = {
519 &cpuinfo_min_freq.attr,
520 &cpuinfo_max_freq.attr,
521 &scaling_min_freq.attr,
522 &scaling_max_freq.attr,
524 &scaling_governor.attr,
525 &scaling_driver.attr,
526 &scaling_available_governors.attr,
530 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
531 #define to_attr(a) container_of(a,struct freq_attr,attr)
533 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
535 struct cpufreq_policy * policy = to_policy(kobj);
536 struct freq_attr * fattr = to_attr(attr);
538 policy = cpufreq_cpu_get(policy->cpu);
541 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
542 cpufreq_cpu_put(policy);
546 static ssize_t store(struct kobject * kobj, struct attribute * attr,
547 const char * buf, size_t count)
549 struct cpufreq_policy * policy = to_policy(kobj);
550 struct freq_attr * fattr = to_attr(attr);
552 policy = cpufreq_cpu_get(policy->cpu);
555 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
556 cpufreq_cpu_put(policy);
560 static void cpufreq_sysfs_release(struct kobject * kobj)
562 struct cpufreq_policy * policy = to_policy(kobj);
563 dprintk("last reference is dropped\n");
564 complete(&policy->kobj_unregister);
567 static struct sysfs_ops sysfs_ops = {
572 static struct kobj_type ktype_cpufreq = {
573 .sysfs_ops = &sysfs_ops,
574 .default_attrs = default_attrs,
575 .release = cpufreq_sysfs_release,
580 * cpufreq_add_dev - add a CPU device
582 * Adds the cpufreq interface for a CPU device.
584 static int cpufreq_add_dev (struct sys_device * sys_dev)
586 unsigned int cpu = sys_dev->id;
588 struct cpufreq_policy new_policy;
589 struct cpufreq_policy *policy;
590 struct freq_attr **drv_attr;
591 struct sys_device *cpu_sys_dev;
595 struct cpufreq_policy *managed_policy;
598 if (cpu_is_offline(cpu))
601 cpufreq_debug_disable_ratelimit();
602 dprintk("adding CPU %u\n", cpu);
605 /* check whether a different CPU already registered this
606 * CPU because it is in the same boat. */
607 policy = cpufreq_cpu_get(cpu);
608 if (unlikely(policy)) {
609 cpufreq_cpu_put(policy);
610 cpufreq_debug_enable_ratelimit();
615 if (!try_module_get(cpufreq_driver->owner)) {
620 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
627 policy->cpus = cpumask_of_cpu(cpu);
629 mutex_init(&policy->lock);
630 mutex_lock(&policy->lock);
631 init_completion(&policy->kobj_unregister);
632 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
634 /* call driver. From then on the cpufreq must be able
635 * to accept all calls to ->verify and ->setpolicy for this CPU
637 ret = cpufreq_driver->init(policy);
639 dprintk("initialization failed\n");
640 mutex_unlock(&policy->lock);
645 for_each_cpu_mask(j, policy->cpus) {
649 /* check for existing affected CPUs. They may not be aware
650 * of it due to CPU Hotplug.
652 managed_policy = cpufreq_cpu_get(j);
653 if (unlikely(managed_policy)) {
654 spin_lock_irqsave(&cpufreq_driver_lock, flags);
655 managed_policy->cpus = policy->cpus;
656 cpufreq_cpu_data[cpu] = managed_policy;
657 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
659 dprintk("CPU already managed, adding link\n");
660 sysfs_create_link(&sys_dev->kobj,
661 &managed_policy->kobj, "cpufreq");
663 cpufreq_debug_enable_ratelimit();
664 mutex_unlock(&policy->lock);
666 goto err_out_driver_exit; /* call driver->exit() */
670 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
672 /* prepare interface data */
673 policy->kobj.parent = &sys_dev->kobj;
674 policy->kobj.ktype = &ktype_cpufreq;
675 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
677 ret = kobject_register(&policy->kobj);
679 mutex_unlock(&policy->lock);
680 goto err_out_driver_exit;
682 /* set up files for this cpu device */
683 drv_attr = cpufreq_driver->attr;
684 while ((drv_attr) && (*drv_attr)) {
685 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
688 if (cpufreq_driver->get)
689 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
690 if (cpufreq_driver->target)
691 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
693 spin_lock_irqsave(&cpufreq_driver_lock, flags);
694 for_each_cpu_mask(j, policy->cpus)
695 cpufreq_cpu_data[j] = policy;
696 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
698 /* symlink affected CPUs */
699 for_each_cpu_mask(j, policy->cpus) {
705 dprintk("CPU %u already managed, adding link\n", j);
706 cpufreq_cpu_get(cpu);
707 cpu_sys_dev = get_cpu_sysdev(j);
708 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
712 policy->governor = NULL; /* to assure that the starting sequence is
713 * run in cpufreq_set_policy */
714 mutex_unlock(&policy->lock);
716 /* set default policy */
717 ret = cpufreq_set_policy(&new_policy);
719 dprintk("setting policy failed\n");
720 goto err_out_unregister;
723 module_put(cpufreq_driver->owner);
724 dprintk("initialization complete\n");
725 cpufreq_debug_enable_ratelimit();
731 spin_lock_irqsave(&cpufreq_driver_lock, flags);
732 for_each_cpu_mask(j, policy->cpus)
733 cpufreq_cpu_data[j] = NULL;
734 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
736 kobject_unregister(&policy->kobj);
737 wait_for_completion(&policy->kobj_unregister);
740 if (cpufreq_driver->exit)
741 cpufreq_driver->exit(policy);
747 module_put(cpufreq_driver->owner);
749 cpufreq_debug_enable_ratelimit();
755 * cpufreq_remove_dev - remove a CPU device
757 * Removes the cpufreq interface for a CPU device.
759 static int cpufreq_remove_dev (struct sys_device * sys_dev)
761 unsigned int cpu = sys_dev->id;
763 struct cpufreq_policy *data;
765 struct sys_device *cpu_sys_dev;
769 cpufreq_debug_disable_ratelimit();
770 dprintk("unregistering CPU %u\n", cpu);
772 spin_lock_irqsave(&cpufreq_driver_lock, flags);
773 data = cpufreq_cpu_data[cpu];
776 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
777 cpufreq_debug_enable_ratelimit();
780 cpufreq_cpu_data[cpu] = NULL;
784 /* if this isn't the CPU which is the parent of the kobj, we
785 * only need to unlink, put and exit
787 if (unlikely(cpu != data->cpu)) {
788 dprintk("removing link\n");
789 cpu_clear(cpu, data->cpus);
790 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
791 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
792 cpufreq_cpu_put(data);
793 cpufreq_debug_enable_ratelimit();
799 if (!kobject_get(&data->kobj)) {
800 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
801 cpufreq_debug_enable_ratelimit();
806 /* if we have other CPUs still registered, we need to unlink them,
807 * or else wait_for_completion below will lock up. Clean the
808 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
811 if (unlikely(cpus_weight(data->cpus) > 1)) {
812 for_each_cpu_mask(j, data->cpus) {
815 cpufreq_cpu_data[j] = NULL;
819 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
821 if (unlikely(cpus_weight(data->cpus) > 1)) {
822 for_each_cpu_mask(j, data->cpus) {
825 dprintk("removing link for cpu %u\n", j);
826 cpu_sys_dev = get_cpu_sysdev(j);
827 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
828 cpufreq_cpu_put(data);
832 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
835 mutex_lock(&data->lock);
836 if (cpufreq_driver->target)
837 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
838 mutex_unlock(&data->lock);
840 kobject_unregister(&data->kobj);
842 kobject_put(&data->kobj);
844 /* we need to make sure that the underlying kobj is actually
845 * not referenced anymore by anybody before we proceed with
848 dprintk("waiting for dropping of refcount\n");
849 wait_for_completion(&data->kobj_unregister);
850 dprintk("wait complete\n");
852 if (cpufreq_driver->exit)
853 cpufreq_driver->exit(data);
857 cpufreq_debug_enable_ratelimit();
862 static void handle_update(void *data)
864 unsigned int cpu = (unsigned int)(long)data;
865 dprintk("handle_update for cpu %u called\n", cpu);
866 cpufreq_update_policy(cpu);
870 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
872 * @old_freq: CPU frequency the kernel thinks the CPU runs at
873 * @new_freq: CPU frequency the CPU actually runs at
875 * We adjust to current frequency first, and need to clean up later. So either call
876 * to cpufreq_update_policy() or schedule handle_update()).
878 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
880 struct cpufreq_freqs freqs;
882 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
883 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
886 freqs.old = old_freq;
887 freqs.new = new_freq;
888 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
889 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
894 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
897 * This is the last known freq, without actually getting it from the driver.
898 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
900 unsigned int cpufreq_quick_get(unsigned int cpu)
902 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
903 unsigned int ret = 0;
906 mutex_lock(&policy->lock);
908 mutex_unlock(&policy->lock);
909 cpufreq_cpu_put(policy);
914 EXPORT_SYMBOL(cpufreq_quick_get);
918 * cpufreq_get - get the current CPU frequency (in kHz)
921 * Get the CPU current (static) CPU frequency
923 unsigned int cpufreq_get(unsigned int cpu)
925 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
926 unsigned int ret = 0;
931 if (!cpufreq_driver->get)
934 mutex_lock(&policy->lock);
936 ret = cpufreq_driver->get(cpu);
938 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
939 /* verify no discrepancy between actual and saved value exists */
940 if (unlikely(ret != policy->cur)) {
941 cpufreq_out_of_sync(cpu, policy->cur, ret);
942 schedule_work(&policy->update);
946 mutex_unlock(&policy->lock);
949 cpufreq_cpu_put(policy);
953 EXPORT_SYMBOL(cpufreq_get);
957 * cpufreq_suspend - let the low level driver prepare for suspend
960 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
962 int cpu = sysdev->id;
963 unsigned int ret = 0;
964 unsigned int cur_freq = 0;
965 struct cpufreq_policy *cpu_policy;
967 dprintk("resuming cpu %u\n", cpu);
969 if (!cpu_online(cpu))
972 /* we may be lax here as interrupts are off. Nonetheless
973 * we need to grab the correct cpu policy, as to check
974 * whether we really run on this CPU.
977 cpu_policy = cpufreq_cpu_get(cpu);
981 /* only handle each CPU group once */
982 if (unlikely(cpu_policy->cpu != cpu)) {
983 cpufreq_cpu_put(cpu_policy);
987 if (cpufreq_driver->suspend) {
988 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
990 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
991 "step on CPU %u\n", cpu_policy->cpu);
992 cpufreq_cpu_put(cpu_policy);
998 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
1001 if (cpufreq_driver->get)
1002 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1004 if (!cur_freq || !cpu_policy->cur) {
1005 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1006 "frequency is what timing core thinks it is.\n");
1010 if (unlikely(cur_freq != cpu_policy->cur)) {
1011 struct cpufreq_freqs freqs;
1013 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1014 dprintk("Warning: CPU frequency is %u, "
1015 "cpufreq assumed %u kHz.\n",
1016 cur_freq, cpu_policy->cur);
1019 freqs.old = cpu_policy->cur;
1020 freqs.new = cur_freq;
1022 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
1023 CPUFREQ_SUSPENDCHANGE, &freqs);
1024 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1026 cpu_policy->cur = cur_freq;
1030 cpufreq_cpu_put(cpu_policy);
1035 * cpufreq_resume - restore proper CPU frequency handling after resume
1037 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1038 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
1039 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1042 static int cpufreq_resume(struct sys_device * sysdev)
1044 int cpu = sysdev->id;
1045 unsigned int ret = 0;
1046 struct cpufreq_policy *cpu_policy;
1048 dprintk("resuming cpu %u\n", cpu);
1050 if (!cpu_online(cpu))
1053 /* we may be lax here as interrupts are off. Nonetheless
1054 * we need to grab the correct cpu policy, as to check
1055 * whether we really run on this CPU.
1058 cpu_policy = cpufreq_cpu_get(cpu);
1062 /* only handle each CPU group once */
1063 if (unlikely(cpu_policy->cpu != cpu)) {
1064 cpufreq_cpu_put(cpu_policy);
1068 if (cpufreq_driver->resume) {
1069 ret = cpufreq_driver->resume(cpu_policy);
1071 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1072 "step on CPU %u\n", cpu_policy->cpu);
1073 cpufreq_cpu_put(cpu_policy);
1078 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1079 unsigned int cur_freq = 0;
1081 if (cpufreq_driver->get)
1082 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1084 if (!cur_freq || !cpu_policy->cur) {
1085 printk(KERN_ERR "cpufreq: resume failed to assert "
1086 "current frequency is what timing core "
1091 if (unlikely(cur_freq != cpu_policy->cur)) {
1092 struct cpufreq_freqs freqs;
1094 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1095 dprintk("Warning: CPU frequency"
1096 "is %u, cpufreq assumed %u kHz.\n",
1097 cur_freq, cpu_policy->cur);
1100 freqs.old = cpu_policy->cur;
1101 freqs.new = cur_freq;
1103 blocking_notifier_call_chain(
1104 &cpufreq_transition_notifier_list,
1105 CPUFREQ_RESUMECHANGE, &freqs);
1106 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1108 cpu_policy->cur = cur_freq;
1113 schedule_work(&cpu_policy->update);
1114 cpufreq_cpu_put(cpu_policy);
1118 static struct sysdev_driver cpufreq_sysdev_driver = {
1119 .add = cpufreq_add_dev,
1120 .remove = cpufreq_remove_dev,
1121 .suspend = cpufreq_suspend,
1122 .resume = cpufreq_resume,
1126 /*********************************************************************
1127 * NOTIFIER LISTS INTERFACE *
1128 *********************************************************************/
1131 * cpufreq_register_notifier - register a driver with cpufreq
1132 * @nb: notifier function to register
1133 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1135 * Add a driver to one of two lists: either a list of drivers that
1136 * are notified about clock rate changes (once before and once after
1137 * the transition), or a list of drivers that are notified about
1138 * changes in cpufreq policy.
1140 * This function may sleep, and has the same return conditions as
1141 * blocking_notifier_chain_register.
1143 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1148 case CPUFREQ_TRANSITION_NOTIFIER:
1149 ret = blocking_notifier_chain_register(
1150 &cpufreq_transition_notifier_list, nb);
1152 case CPUFREQ_POLICY_NOTIFIER:
1153 ret = blocking_notifier_chain_register(
1154 &cpufreq_policy_notifier_list, nb);
1162 EXPORT_SYMBOL(cpufreq_register_notifier);
1166 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1167 * @nb: notifier block to be unregistered
1168 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1170 * Remove a driver from the CPU frequency notifier list.
1172 * This function may sleep, and has the same return conditions as
1173 * blocking_notifier_chain_unregister.
1175 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1180 case CPUFREQ_TRANSITION_NOTIFIER:
1181 ret = blocking_notifier_chain_unregister(
1182 &cpufreq_transition_notifier_list, nb);
1184 case CPUFREQ_POLICY_NOTIFIER:
1185 ret = blocking_notifier_chain_unregister(
1186 &cpufreq_policy_notifier_list, nb);
1194 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1197 /*********************************************************************
1199 *********************************************************************/
1202 /* Must be called with lock_cpu_hotplug held */
1203 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1204 unsigned int target_freq,
1205 unsigned int relation)
1207 int retval = -EINVAL;
1209 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1210 target_freq, relation);
1211 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1212 retval = cpufreq_driver->target(policy, target_freq, relation);
1216 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1218 int cpufreq_driver_target(struct cpufreq_policy *policy,
1219 unsigned int target_freq,
1220 unsigned int relation)
1224 policy = cpufreq_cpu_get(policy->cpu);
1229 mutex_lock(&policy->lock);
1231 ret = __cpufreq_driver_target(policy, target_freq, relation);
1233 mutex_unlock(&policy->lock);
1234 unlock_cpu_hotplug();
1236 cpufreq_cpu_put(policy);
1239 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1242 * Locking: Must be called with the lock_cpu_hotplug() lock held
1243 * when "event" is CPUFREQ_GOV_LIMITS
1246 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1250 if (!try_module_get(policy->governor->owner))
1253 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1254 ret = policy->governor->governor(policy, event);
1256 /* we keep one module reference alive for each CPU governed by this CPU */
1257 if ((event != CPUFREQ_GOV_START) || ret)
1258 module_put(policy->governor->owner);
1259 if ((event == CPUFREQ_GOV_STOP) && !ret)
1260 module_put(policy->governor->owner);
1266 int cpufreq_register_governor(struct cpufreq_governor *governor)
1268 struct cpufreq_governor *t;
1273 mutex_lock(&cpufreq_governor_mutex);
1275 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1276 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1277 mutex_unlock(&cpufreq_governor_mutex);
1281 list_add(&governor->governor_list, &cpufreq_governor_list);
1283 mutex_unlock(&cpufreq_governor_mutex);
1286 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1289 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1294 mutex_lock(&cpufreq_governor_mutex);
1295 list_del(&governor->governor_list);
1296 mutex_unlock(&cpufreq_governor_mutex);
1299 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1303 /*********************************************************************
1304 * POLICY INTERFACE *
1305 *********************************************************************/
1308 * cpufreq_get_policy - get the current cpufreq_policy
1309 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1311 * Reads the current cpufreq policy.
1313 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1315 struct cpufreq_policy *cpu_policy;
1319 cpu_policy = cpufreq_cpu_get(cpu);
1323 mutex_lock(&cpu_policy->lock);
1324 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1325 mutex_unlock(&cpu_policy->lock);
1327 cpufreq_cpu_put(cpu_policy);
1330 EXPORT_SYMBOL(cpufreq_get_policy);
1334 * Locking: Must be called with the lock_cpu_hotplug() lock held
1336 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1340 cpufreq_debug_disable_ratelimit();
1341 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1342 policy->min, policy->max);
1344 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
1346 /* verify the cpu speed can be set within this limit */
1347 ret = cpufreq_driver->verify(policy);
1351 /* adjust if necessary - all reasons */
1352 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1353 CPUFREQ_ADJUST, policy);
1355 /* adjust if necessary - hardware incompatibility*/
1356 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1357 CPUFREQ_INCOMPATIBLE, policy);
1359 /* verify the cpu speed can be set within this limit,
1360 which might be different to the first one */
1361 ret = cpufreq_driver->verify(policy);
1365 /* notification of the new policy */
1366 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1367 CPUFREQ_NOTIFY, policy);
1369 data->min = policy->min;
1370 data->max = policy->max;
1372 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1374 if (cpufreq_driver->setpolicy) {
1375 data->policy = policy->policy;
1376 dprintk("setting range\n");
1377 ret = cpufreq_driver->setpolicy(policy);
1379 if (policy->governor != data->governor) {
1380 /* save old, working values */
1381 struct cpufreq_governor *old_gov = data->governor;
1383 dprintk("governor switch\n");
1385 /* end old governor */
1387 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1389 /* start new governor */
1390 data->governor = policy->governor;
1391 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1392 /* new governor failed, so re-start old one */
1393 dprintk("starting governor %s failed\n", data->governor->name);
1395 data->governor = old_gov;
1396 __cpufreq_governor(data, CPUFREQ_GOV_START);
1401 /* might be a policy change, too, so fall through */
1403 dprintk("governor: change or update limits\n");
1404 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1408 cpufreq_debug_enable_ratelimit();
1413 * cpufreq_set_policy - set a new CPUFreq policy
1414 * @policy: policy to be set.
1416 * Sets a new CPU frequency and voltage scaling policy.
1418 int cpufreq_set_policy(struct cpufreq_policy *policy)
1421 struct cpufreq_policy *data;
1426 data = cpufreq_cpu_get(policy->cpu);
1433 mutex_lock(&data->lock);
1435 ret = __cpufreq_set_policy(data, policy);
1436 data->user_policy.min = data->min;
1437 data->user_policy.max = data->max;
1438 data->user_policy.policy = data->policy;
1439 data->user_policy.governor = data->governor;
1441 mutex_unlock(&data->lock);
1443 unlock_cpu_hotplug();
1444 cpufreq_cpu_put(data);
1448 EXPORT_SYMBOL(cpufreq_set_policy);
1452 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1453 * @cpu: CPU which shall be re-evaluated
1455 * Usefull for policy notifiers which have different necessities
1456 * at different times.
1458 int cpufreq_update_policy(unsigned int cpu)
1460 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1461 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);
1493 unlock_cpu_hotplug();
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);