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
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/notifier.h>
21 #include <linux/cpufreq.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/spinlock.h>
25 #include <linux/device.h>
26 #include <linux/slab.h>
27 #include <linux/cpu.h>
28 #include <linux/completion.h>
29 #include <linux/mutex.h>
31 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
34 * The "cpufreq driver" - the arch- or hardware-dependend low
35 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
38 static struct cpufreq_driver *cpufreq_driver;
39 static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
40 static DEFINE_SPINLOCK(cpufreq_driver_lock);
42 /* internal prototypes */
43 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
44 static void handle_update(void *data);
47 * Two notifier lists: the "policy" list is involved in the
48 * validation process for a new CPU frequency policy; the
49 * "transition" list for kernel code that needs to handle
50 * changes to devices when the CPU clock speed changes.
51 * The mutex locks both lists.
53 static struct notifier_block *cpufreq_policy_notifier_list;
54 static struct notifier_block *cpufreq_transition_notifier_list;
55 static DECLARE_RWSEM (cpufreq_notifier_rwsem);
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 down_read(&cpufreq_notifier_rwsem);
250 policy = cpufreq_cpu_data[freqs->cpu];
253 case CPUFREQ_PRECHANGE:
254 /* detect if the driver reported a value as "old frequency"
255 * which is not equal to what the cpufreq core thinks is
258 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
259 if ((policy) && (policy->cpu == freqs->cpu) &&
260 (policy->cur) && (policy->cur != freqs->old)) {
261 dprintk(KERN_WARNING "Warning: CPU frequency is"
262 " %u, cpufreq assumed %u kHz.\n",
263 freqs->old, policy->cur);
264 freqs->old = policy->cur;
267 notifier_call_chain(&cpufreq_transition_notifier_list,
268 CPUFREQ_PRECHANGE, freqs);
269 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
272 case CPUFREQ_POSTCHANGE:
273 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
274 notifier_call_chain(&cpufreq_transition_notifier_list,
275 CPUFREQ_POSTCHANGE, freqs);
276 if (likely(policy) && likely(policy->cpu == freqs->cpu))
277 policy->cur = freqs->new;
280 up_read(&cpufreq_notifier_rwsem);
282 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
286 /*********************************************************************
288 *********************************************************************/
291 * cpufreq_parse_governor - parse a governor string
293 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
294 struct cpufreq_governor **governor)
298 if (cpufreq_driver->setpolicy) {
299 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
300 *policy = CPUFREQ_POLICY_PERFORMANCE;
302 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
303 *policy = CPUFREQ_POLICY_POWERSAVE;
308 struct cpufreq_governor *t;
309 mutex_lock(&cpufreq_governor_mutex);
310 if (!cpufreq_driver || !cpufreq_driver->target)
312 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
313 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
315 mutex_unlock(&cpufreq_governor_mutex);
320 mutex_unlock(&cpufreq_governor_mutex);
324 EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
327 /* drivers/base/cpu.c */
328 extern struct sysdev_class cpu_sysdev_class;
332 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
334 * Write out information from cpufreq_driver->policy[cpu]; object must be
338 #define show_one(file_name, object) \
339 static ssize_t show_##file_name \
340 (struct cpufreq_policy * policy, char *buf) \
342 return sprintf (buf, "%u\n", policy->object); \
345 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
346 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
347 show_one(scaling_min_freq, min);
348 show_one(scaling_max_freq, max);
349 show_one(scaling_cur_freq, cur);
352 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
354 #define store_one(file_name, object) \
355 static ssize_t store_##file_name \
356 (struct cpufreq_policy * policy, const char *buf, size_t count) \
358 unsigned int ret = -EINVAL; \
359 struct cpufreq_policy new_policy; \
361 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
365 ret = sscanf (buf, "%u", &new_policy.object); \
369 ret = cpufreq_set_policy(&new_policy); \
371 return ret ? ret : count; \
374 store_one(scaling_min_freq,min);
375 store_one(scaling_max_freq,max);
378 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
380 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
382 unsigned int cur_freq = cpufreq_get(policy->cpu);
384 return sprintf(buf, "<unknown>");
385 return sprintf(buf, "%u\n", cur_freq);
390 * show_scaling_governor - show the current policy for the specified CPU
392 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
394 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
395 return sprintf(buf, "powersave\n");
396 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
397 return sprintf(buf, "performance\n");
398 else if (policy->governor)
399 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
405 * store_scaling_governor - store policy for the specified CPU
407 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
408 const char *buf, size_t count)
410 unsigned int ret = -EINVAL;
411 char str_governor[16];
412 struct cpufreq_policy new_policy;
414 ret = cpufreq_get_policy(&new_policy, policy->cpu);
418 ret = sscanf (buf, "%15s", str_governor);
422 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
425 ret = cpufreq_set_policy(&new_policy);
426 return ret ? ret : count;
430 * show_scaling_driver - show the cpufreq driver currently loaded
432 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
434 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
438 * show_scaling_available_governors - show the available CPUfreq governors
440 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
444 struct cpufreq_governor *t;
446 if (!cpufreq_driver->target) {
447 i += sprintf(buf, "performance powersave");
451 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
452 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
454 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
457 i += sprintf(&buf[i], "\n");
461 * show_affected_cpus - show the CPUs affected by each transition
463 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
468 for_each_cpu_mask(cpu, policy->cpus) {
470 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
471 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
472 if (i >= (PAGE_SIZE - 5))
475 i += sprintf(&buf[i], "\n");
480 #define define_one_ro(_name) \
481 static struct freq_attr _name = \
482 __ATTR(_name, 0444, show_##_name, NULL)
484 #define define_one_ro0400(_name) \
485 static struct freq_attr _name = \
486 __ATTR(_name, 0400, show_##_name, NULL)
488 #define define_one_rw(_name) \
489 static struct freq_attr _name = \
490 __ATTR(_name, 0644, show_##_name, store_##_name)
492 define_one_ro0400(cpuinfo_cur_freq);
493 define_one_ro(cpuinfo_min_freq);
494 define_one_ro(cpuinfo_max_freq);
495 define_one_ro(scaling_available_governors);
496 define_one_ro(scaling_driver);
497 define_one_ro(scaling_cur_freq);
498 define_one_ro(affected_cpus);
499 define_one_rw(scaling_min_freq);
500 define_one_rw(scaling_max_freq);
501 define_one_rw(scaling_governor);
503 static struct attribute * default_attrs[] = {
504 &cpuinfo_min_freq.attr,
505 &cpuinfo_max_freq.attr,
506 &scaling_min_freq.attr,
507 &scaling_max_freq.attr,
509 &scaling_governor.attr,
510 &scaling_driver.attr,
511 &scaling_available_governors.attr,
515 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
516 #define to_attr(a) container_of(a,struct freq_attr,attr)
518 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
520 struct cpufreq_policy * policy = to_policy(kobj);
521 struct freq_attr * fattr = to_attr(attr);
523 policy = cpufreq_cpu_get(policy->cpu);
526 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
527 cpufreq_cpu_put(policy);
531 static ssize_t store(struct kobject * kobj, struct attribute * attr,
532 const char * buf, size_t count)
534 struct cpufreq_policy * policy = to_policy(kobj);
535 struct freq_attr * fattr = to_attr(attr);
537 policy = cpufreq_cpu_get(policy->cpu);
540 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
541 cpufreq_cpu_put(policy);
545 static void cpufreq_sysfs_release(struct kobject * kobj)
547 struct cpufreq_policy * policy = to_policy(kobj);
548 dprintk("last reference is dropped\n");
549 complete(&policy->kobj_unregister);
552 static struct sysfs_ops sysfs_ops = {
557 static struct kobj_type ktype_cpufreq = {
558 .sysfs_ops = &sysfs_ops,
559 .default_attrs = default_attrs,
560 .release = cpufreq_sysfs_release,
565 * cpufreq_add_dev - add a CPU device
567 * Adds the cpufreq interface for a CPU device.
569 static int cpufreq_add_dev (struct sys_device * sys_dev)
571 unsigned int cpu = sys_dev->id;
573 struct cpufreq_policy new_policy;
574 struct cpufreq_policy *policy;
575 struct freq_attr **drv_attr;
579 if (cpu_is_offline(cpu))
582 cpufreq_debug_disable_ratelimit();
583 dprintk("adding CPU %u\n", cpu);
586 /* check whether a different CPU already registered this
587 * CPU because it is in the same boat. */
588 policy = cpufreq_cpu_get(cpu);
589 if (unlikely(policy)) {
590 dprintk("CPU already managed, adding link\n");
591 sysfs_create_link(&sys_dev->kobj, &policy->kobj, "cpufreq");
592 cpufreq_debug_enable_ratelimit();
597 if (!try_module_get(cpufreq_driver->owner)) {
602 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
609 policy->cpus = cpumask_of_cpu(cpu);
611 mutex_init(&policy->lock);
612 mutex_lock(&policy->lock);
613 init_completion(&policy->kobj_unregister);
614 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
616 /* call driver. From then on the cpufreq must be able
617 * to accept all calls to ->verify and ->setpolicy for this CPU
619 ret = cpufreq_driver->init(policy);
621 dprintk("initialization failed\n");
622 mutex_unlock(&policy->lock);
626 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
628 /* prepare interface data */
629 policy->kobj.parent = &sys_dev->kobj;
630 policy->kobj.ktype = &ktype_cpufreq;
631 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
633 ret = kobject_register(&policy->kobj);
635 mutex_unlock(&policy->lock);
636 goto err_out_driver_exit;
638 /* set up files for this cpu device */
639 drv_attr = cpufreq_driver->attr;
640 while ((drv_attr) && (*drv_attr)) {
641 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
644 if (cpufreq_driver->get)
645 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
646 if (cpufreq_driver->target)
647 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
649 spin_lock_irqsave(&cpufreq_driver_lock, flags);
650 for_each_cpu_mask(j, policy->cpus)
651 cpufreq_cpu_data[j] = policy;
652 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
653 policy->governor = NULL; /* to assure that the starting sequence is
654 * run in cpufreq_set_policy */
655 mutex_unlock(&policy->lock);
657 /* set default policy */
659 ret = cpufreq_set_policy(&new_policy);
661 dprintk("setting policy failed\n");
662 goto err_out_unregister;
665 module_put(cpufreq_driver->owner);
666 dprintk("initialization complete\n");
667 cpufreq_debug_enable_ratelimit();
673 spin_lock_irqsave(&cpufreq_driver_lock, flags);
674 for_each_cpu_mask(j, policy->cpus)
675 cpufreq_cpu_data[j] = NULL;
676 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
678 kobject_unregister(&policy->kobj);
679 wait_for_completion(&policy->kobj_unregister);
682 if (cpufreq_driver->exit)
683 cpufreq_driver->exit(policy);
689 module_put(cpufreq_driver->owner);
691 cpufreq_debug_enable_ratelimit();
697 * cpufreq_remove_dev - remove a CPU device
699 * Removes the cpufreq interface for a CPU device.
701 static int cpufreq_remove_dev (struct sys_device * sys_dev)
703 unsigned int cpu = sys_dev->id;
705 struct cpufreq_policy *data;
707 struct sys_device *cpu_sys_dev;
711 cpufreq_debug_disable_ratelimit();
712 dprintk("unregistering CPU %u\n", cpu);
714 spin_lock_irqsave(&cpufreq_driver_lock, flags);
715 data = cpufreq_cpu_data[cpu];
718 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
719 cpufreq_debug_enable_ratelimit();
722 cpufreq_cpu_data[cpu] = NULL;
726 /* if this isn't the CPU which is the parent of the kobj, we
727 * only need to unlink, put and exit
729 if (unlikely(cpu != data->cpu)) {
730 dprintk("removing link\n");
731 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
732 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
733 cpufreq_cpu_put(data);
734 cpufreq_debug_enable_ratelimit();
740 if (!kobject_get(&data->kobj)) {
741 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
742 cpufreq_debug_enable_ratelimit();
747 /* if we have other CPUs still registered, we need to unlink them,
748 * or else wait_for_completion below will lock up. Clean the
749 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
752 if (unlikely(cpus_weight(data->cpus) > 1)) {
753 for_each_cpu_mask(j, data->cpus) {
756 cpufreq_cpu_data[j] = NULL;
760 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
762 if (unlikely(cpus_weight(data->cpus) > 1)) {
763 for_each_cpu_mask(j, data->cpus) {
766 dprintk("removing link for cpu %u\n", j);
767 cpu_sys_dev = get_cpu_sysdev(j);
768 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
769 cpufreq_cpu_put(data);
773 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
776 mutex_lock(&data->lock);
777 if (cpufreq_driver->target)
778 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
779 mutex_unlock(&data->lock);
781 kobject_unregister(&data->kobj);
783 kobject_put(&data->kobj);
785 /* we need to make sure that the underlying kobj is actually
786 * not referenced anymore by anybody before we proceed with
789 dprintk("waiting for dropping of refcount\n");
790 wait_for_completion(&data->kobj_unregister);
791 dprintk("wait complete\n");
793 if (cpufreq_driver->exit)
794 cpufreq_driver->exit(data);
798 cpufreq_debug_enable_ratelimit();
803 static void handle_update(void *data)
805 unsigned int cpu = (unsigned int)(long)data;
806 dprintk("handle_update for cpu %u called\n", cpu);
807 cpufreq_update_policy(cpu);
811 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
813 * @old_freq: CPU frequency the kernel thinks the CPU runs at
814 * @new_freq: CPU frequency the CPU actually runs at
816 * We adjust to current frequency first, and need to clean up later. So either call
817 * to cpufreq_update_policy() or schedule handle_update()).
819 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
821 struct cpufreq_freqs freqs;
823 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
824 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
827 freqs.old = old_freq;
828 freqs.new = new_freq;
829 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
830 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
835 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
838 * This is the last known freq, without actually getting it from the driver.
839 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
841 unsigned int cpufreq_quick_get(unsigned int cpu)
843 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
844 unsigned int ret = 0;
847 mutex_lock(&policy->lock);
849 mutex_unlock(&policy->lock);
850 cpufreq_cpu_put(policy);
855 EXPORT_SYMBOL(cpufreq_quick_get);
859 * cpufreq_get - get the current CPU frequency (in kHz)
862 * Get the CPU current (static) CPU frequency
864 unsigned int cpufreq_get(unsigned int cpu)
866 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
867 unsigned int ret = 0;
872 if (!cpufreq_driver->get)
875 mutex_lock(&policy->lock);
877 ret = cpufreq_driver->get(cpu);
879 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
880 /* verify no discrepancy between actual and saved value exists */
881 if (unlikely(ret != policy->cur)) {
882 cpufreq_out_of_sync(cpu, policy->cur, ret);
883 schedule_work(&policy->update);
887 mutex_unlock(&policy->lock);
890 cpufreq_cpu_put(policy);
894 EXPORT_SYMBOL(cpufreq_get);
898 * cpufreq_suspend - let the low level driver prepare for suspend
901 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
903 int cpu = sysdev->id;
904 unsigned int ret = 0;
905 unsigned int cur_freq = 0;
906 struct cpufreq_policy *cpu_policy;
908 dprintk("resuming cpu %u\n", cpu);
910 if (!cpu_online(cpu))
913 /* we may be lax here as interrupts are off. Nonetheless
914 * we need to grab the correct cpu policy, as to check
915 * whether we really run on this CPU.
918 cpu_policy = cpufreq_cpu_get(cpu);
922 /* only handle each CPU group once */
923 if (unlikely(cpu_policy->cpu != cpu)) {
924 cpufreq_cpu_put(cpu_policy);
928 if (cpufreq_driver->suspend) {
929 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
931 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
932 "step on CPU %u\n", cpu_policy->cpu);
933 cpufreq_cpu_put(cpu_policy);
939 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
942 if (cpufreq_driver->get)
943 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
945 if (!cur_freq || !cpu_policy->cur) {
946 printk(KERN_ERR "cpufreq: suspend failed to assert current "
947 "frequency is what timing core thinks it is.\n");
951 if (unlikely(cur_freq != cpu_policy->cur)) {
952 struct cpufreq_freqs freqs;
954 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
955 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
956 "cpufreq assumed %u kHz.\n",
957 cur_freq, cpu_policy->cur);
960 freqs.old = cpu_policy->cur;
961 freqs.new = cur_freq;
963 notifier_call_chain(&cpufreq_transition_notifier_list,
964 CPUFREQ_SUSPENDCHANGE, &freqs);
965 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
967 cpu_policy->cur = cur_freq;
971 cpufreq_cpu_put(cpu_policy);
976 * cpufreq_resume - restore proper CPU frequency handling after resume
978 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
979 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
980 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
983 static int cpufreq_resume(struct sys_device * sysdev)
985 int cpu = sysdev->id;
986 unsigned int ret = 0;
987 struct cpufreq_policy *cpu_policy;
989 dprintk("resuming cpu %u\n", cpu);
991 if (!cpu_online(cpu))
994 /* we may be lax here as interrupts are off. Nonetheless
995 * we need to grab the correct cpu policy, as to check
996 * whether we really run on this CPU.
999 cpu_policy = cpufreq_cpu_get(cpu);
1003 /* only handle each CPU group once */
1004 if (unlikely(cpu_policy->cpu != cpu)) {
1005 cpufreq_cpu_put(cpu_policy);
1009 if (cpufreq_driver->resume) {
1010 ret = cpufreq_driver->resume(cpu_policy);
1012 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1013 "step on CPU %u\n", cpu_policy->cpu);
1014 cpufreq_cpu_put(cpu_policy);
1019 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1020 unsigned int cur_freq = 0;
1022 if (cpufreq_driver->get)
1023 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1025 if (!cur_freq || !cpu_policy->cur) {
1026 printk(KERN_ERR "cpufreq: resume failed to assert "
1027 "current frequency is what timing core "
1032 if (unlikely(cur_freq != cpu_policy->cur)) {
1033 struct cpufreq_freqs freqs;
1035 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1036 dprintk(KERN_WARNING "Warning: CPU frequency"
1037 "is %u, cpufreq assumed %u kHz.\n",
1038 cur_freq, cpu_policy->cur);
1041 freqs.old = cpu_policy->cur;
1042 freqs.new = cur_freq;
1044 notifier_call_chain(&cpufreq_transition_notifier_list,
1045 CPUFREQ_RESUMECHANGE, &freqs);
1046 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1048 cpu_policy->cur = cur_freq;
1053 schedule_work(&cpu_policy->update);
1054 cpufreq_cpu_put(cpu_policy);
1058 static struct sysdev_driver cpufreq_sysdev_driver = {
1059 .add = cpufreq_add_dev,
1060 .remove = cpufreq_remove_dev,
1061 .suspend = cpufreq_suspend,
1062 .resume = cpufreq_resume,
1066 /*********************************************************************
1067 * NOTIFIER LISTS INTERFACE *
1068 *********************************************************************/
1071 * cpufreq_register_notifier - register a driver with cpufreq
1072 * @nb: notifier function to register
1073 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1075 * Add a driver to one of two lists: either a list of drivers that
1076 * are notified about clock rate changes (once before and once after
1077 * the transition), or a list of drivers that are notified about
1078 * changes in cpufreq policy.
1080 * This function may sleep, and has the same return conditions as
1081 * notifier_chain_register.
1083 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1087 down_write(&cpufreq_notifier_rwsem);
1089 case CPUFREQ_TRANSITION_NOTIFIER:
1090 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
1092 case CPUFREQ_POLICY_NOTIFIER:
1093 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
1098 up_write(&cpufreq_notifier_rwsem);
1102 EXPORT_SYMBOL(cpufreq_register_notifier);
1106 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1107 * @nb: notifier block to be unregistered
1108 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1110 * Remove a driver from the CPU frequency notifier list.
1112 * This function may sleep, and has the same return conditions as
1113 * notifier_chain_unregister.
1115 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1119 down_write(&cpufreq_notifier_rwsem);
1121 case CPUFREQ_TRANSITION_NOTIFIER:
1122 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
1124 case CPUFREQ_POLICY_NOTIFIER:
1125 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
1130 up_write(&cpufreq_notifier_rwsem);
1134 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1137 /*********************************************************************
1139 *********************************************************************/
1142 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1143 unsigned int target_freq,
1144 unsigned int relation)
1146 int retval = -EINVAL;
1149 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1150 target_freq, relation);
1151 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1152 retval = cpufreq_driver->target(policy, target_freq, relation);
1154 unlock_cpu_hotplug();
1158 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1160 int cpufreq_driver_target(struct cpufreq_policy *policy,
1161 unsigned int target_freq,
1162 unsigned int relation)
1166 policy = cpufreq_cpu_get(policy->cpu);
1170 mutex_lock(&policy->lock);
1172 ret = __cpufreq_driver_target(policy, target_freq, relation);
1174 mutex_unlock(&policy->lock);
1176 cpufreq_cpu_put(policy);
1179 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1182 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1186 if (!try_module_get(policy->governor->owner))
1189 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1190 ret = policy->governor->governor(policy, event);
1192 /* we keep one module reference alive for each CPU governed by this CPU */
1193 if ((event != CPUFREQ_GOV_START) || ret)
1194 module_put(policy->governor->owner);
1195 if ((event == CPUFREQ_GOV_STOP) && !ret)
1196 module_put(policy->governor->owner);
1202 int cpufreq_governor(unsigned int cpu, unsigned int event)
1205 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1210 mutex_lock(&policy->lock);
1211 ret = __cpufreq_governor(policy, event);
1212 mutex_unlock(&policy->lock);
1214 cpufreq_cpu_put(policy);
1217 EXPORT_SYMBOL_GPL(cpufreq_governor);
1220 int cpufreq_register_governor(struct cpufreq_governor *governor)
1222 struct cpufreq_governor *t;
1227 mutex_lock(&cpufreq_governor_mutex);
1229 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1230 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1231 mutex_unlock(&cpufreq_governor_mutex);
1235 list_add(&governor->governor_list, &cpufreq_governor_list);
1237 mutex_unlock(&cpufreq_governor_mutex);
1240 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1243 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1248 mutex_lock(&cpufreq_governor_mutex);
1249 list_del(&governor->governor_list);
1250 mutex_unlock(&cpufreq_governor_mutex);
1253 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1257 /*********************************************************************
1258 * POLICY INTERFACE *
1259 *********************************************************************/
1262 * cpufreq_get_policy - get the current cpufreq_policy
1263 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1265 * Reads the current cpufreq policy.
1267 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1269 struct cpufreq_policy *cpu_policy;
1273 cpu_policy = cpufreq_cpu_get(cpu);
1277 mutex_lock(&cpu_policy->lock);
1278 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1279 mutex_unlock(&cpu_policy->lock);
1281 cpufreq_cpu_put(cpu_policy);
1284 EXPORT_SYMBOL(cpufreq_get_policy);
1287 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1291 cpufreq_debug_disable_ratelimit();
1292 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1293 policy->min, policy->max);
1295 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
1297 /* verify the cpu speed can be set within this limit */
1298 ret = cpufreq_driver->verify(policy);
1302 down_read(&cpufreq_notifier_rwsem);
1304 /* adjust if necessary - all reasons */
1305 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
1308 /* adjust if necessary - hardware incompatibility*/
1309 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
1312 /* verify the cpu speed can be set within this limit,
1313 which might be different to the first one */
1314 ret = cpufreq_driver->verify(policy);
1316 up_read(&cpufreq_notifier_rwsem);
1320 /* notification of the new policy */
1321 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
1324 up_read(&cpufreq_notifier_rwsem);
1326 data->min = policy->min;
1327 data->max = policy->max;
1329 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1331 if (cpufreq_driver->setpolicy) {
1332 data->policy = policy->policy;
1333 dprintk("setting range\n");
1334 ret = cpufreq_driver->setpolicy(policy);
1336 if (policy->governor != data->governor) {
1337 /* save old, working values */
1338 struct cpufreq_governor *old_gov = data->governor;
1340 dprintk("governor switch\n");
1342 /* end old governor */
1344 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1346 /* start new governor */
1347 data->governor = policy->governor;
1348 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1349 /* new governor failed, so re-start old one */
1350 dprintk("starting governor %s failed\n", data->governor->name);
1352 data->governor = old_gov;
1353 __cpufreq_governor(data, CPUFREQ_GOV_START);
1358 /* might be a policy change, too, so fall through */
1360 dprintk("governor: change or update limits\n");
1361 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1365 cpufreq_debug_enable_ratelimit();
1370 * cpufreq_set_policy - set a new CPUFreq policy
1371 * @policy: policy to be set.
1373 * Sets a new CPU frequency and voltage scaling policy.
1375 int cpufreq_set_policy(struct cpufreq_policy *policy)
1378 struct cpufreq_policy *data;
1383 data = cpufreq_cpu_get(policy->cpu);
1388 mutex_lock(&data->lock);
1390 ret = __cpufreq_set_policy(data, policy);
1391 data->user_policy.min = data->min;
1392 data->user_policy.max = data->max;
1393 data->user_policy.policy = data->policy;
1394 data->user_policy.governor = data->governor;
1396 mutex_unlock(&data->lock);
1397 cpufreq_cpu_put(data);
1401 EXPORT_SYMBOL(cpufreq_set_policy);
1405 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1406 * @cpu: CPU which shall be re-evaluated
1408 * Usefull for policy notifiers which have different necessities
1409 * at different times.
1411 int cpufreq_update_policy(unsigned int cpu)
1413 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1414 struct cpufreq_policy policy;
1420 mutex_lock(&data->lock);
1422 dprintk("updating policy for CPU %u\n", cpu);
1423 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1424 policy.min = data->user_policy.min;
1425 policy.max = data->user_policy.max;
1426 policy.policy = data->user_policy.policy;
1427 policy.governor = data->user_policy.governor;
1429 /* BIOS might change freq behind our back
1430 -> ask driver for current freq and notify governors about a change */
1431 if (cpufreq_driver->get) {
1432 policy.cur = cpufreq_driver->get(cpu);
1434 dprintk("Driver did not initialize current freq");
1435 data->cur = policy.cur;
1437 if (data->cur != policy.cur)
1438 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1442 ret = __cpufreq_set_policy(data, &policy);
1444 mutex_unlock(&data->lock);
1446 cpufreq_cpu_put(data);
1449 EXPORT_SYMBOL(cpufreq_update_policy);
1451 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1452 unsigned long action, void *hcpu)
1454 unsigned int cpu = (unsigned long)hcpu;
1455 struct cpufreq_policy *policy;
1456 struct sys_device *sys_dev;
1458 sys_dev = get_cpu_sysdev(cpu);
1463 cpufreq_add_dev(sys_dev);
1465 case CPU_DOWN_PREPARE:
1467 * We attempt to put this cpu in lowest frequency
1468 * possible before going down. This will permit
1469 * hardware-managed P-State to switch other related
1470 * threads to min or higher speeds if possible.
1472 policy = cpufreq_cpu_data[cpu];
1474 cpufreq_driver_target(policy, policy->min,
1475 CPUFREQ_RELATION_H);
1479 cpufreq_remove_dev(sys_dev);
1486 static struct notifier_block cpufreq_cpu_notifier =
1488 .notifier_call = cpufreq_cpu_callback,
1491 /*********************************************************************
1492 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1493 *********************************************************************/
1496 * cpufreq_register_driver - register a CPU Frequency driver
1497 * @driver_data: A struct cpufreq_driver containing the values#
1498 * submitted by the CPU Frequency driver.
1500 * Registers a CPU Frequency driver to this core code. This code
1501 * returns zero on success, -EBUSY when another driver got here first
1502 * (and isn't unregistered in the meantime).
1505 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1507 unsigned long flags;
1510 if (!driver_data || !driver_data->verify || !driver_data->init ||
1511 ((!driver_data->setpolicy) && (!driver_data->target)))
1514 dprintk("trying to register driver %s\n", driver_data->name);
1516 if (driver_data->setpolicy)
1517 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1519 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1520 if (cpufreq_driver) {
1521 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1524 cpufreq_driver = driver_data;
1525 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1527 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1529 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1533 /* check for at least one working CPU */
1534 for (i=0; i<NR_CPUS; i++)
1535 if (cpufreq_cpu_data[i])
1538 /* if all ->init() calls failed, unregister */
1540 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1541 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1543 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1544 cpufreq_driver = NULL;
1545 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1550 register_cpu_notifier(&cpufreq_cpu_notifier);
1551 dprintk("driver %s up and running\n", driver_data->name);
1552 cpufreq_debug_enable_ratelimit();
1557 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1561 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1563 * Unregister the current CPUFreq driver. Only call this if you have
1564 * the right to do so, i.e. if you have succeeded in initialising before!
1565 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1566 * currently not initialised.
1568 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1570 unsigned long flags;
1572 cpufreq_debug_disable_ratelimit();
1574 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1575 cpufreq_debug_enable_ratelimit();
1579 dprintk("unregistering driver %s\n", driver->name);
1581 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1582 unregister_cpu_notifier(&cpufreq_cpu_notifier);
1584 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1585 cpufreq_driver = NULL;
1586 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1590 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);