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>
30 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
33 * The "cpufreq driver" - the arch- or hardware-dependend low
34 * level driver of CPUFreq support, and its spinlock. This lock
35 * also protects the cpufreq_cpu_data array.
37 static struct cpufreq_driver *cpufreq_driver;
38 static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
39 static DEFINE_SPINLOCK(cpufreq_driver_lock);
41 /* internal prototypes */
42 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
43 static void handle_update(void *data);
46 * Two notifier lists: the "policy" list is involved in the
47 * validation process for a new CPU frequency policy; the
48 * "transition" list for kernel code that needs to handle
49 * changes to devices when the CPU clock speed changes.
50 * The mutex locks both lists.
52 static struct notifier_block *cpufreq_policy_notifier_list;
53 static struct notifier_block *cpufreq_transition_notifier_list;
54 static DECLARE_RWSEM (cpufreq_notifier_rwsem);
57 static LIST_HEAD(cpufreq_governor_list);
58 static DECLARE_MUTEX (cpufreq_governor_sem);
60 struct cpufreq_policy * cpufreq_cpu_get(unsigned int cpu)
62 struct cpufreq_policy *data;
68 /* get the cpufreq driver */
69 spin_lock_irqsave(&cpufreq_driver_lock, flags);
74 if (!try_module_get(cpufreq_driver->owner))
79 data = cpufreq_cpu_data[cpu];
82 goto err_out_put_module;
84 if (!kobject_get(&data->kobj))
85 goto err_out_put_module;
88 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
93 module_put(cpufreq_driver->owner);
95 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
99 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
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 on frequency transition
233 * This function calls the transition notifiers and the "adjust_jiffies" function. It is called
234 * twice on all CPU frequency changes that have external effects.
236 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
238 BUG_ON(irqs_disabled());
240 freqs->flags = cpufreq_driver->flags;
241 dprintk("notification %u of frequency transition to %u kHz\n", state, freqs->new);
243 down_read(&cpufreq_notifier_rwsem);
245 case CPUFREQ_PRECHANGE:
246 /* detect if the driver reported a value as "old frequency" which
247 * is not equal to what the cpufreq core thinks is "old frequency".
249 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
250 if ((likely(cpufreq_cpu_data[freqs->cpu])) &&
251 (likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu)) &&
252 (likely(cpufreq_cpu_data[freqs->cpu]->cur)) &&
253 (unlikely(freqs->old != cpufreq_cpu_data[freqs->cpu]->cur)))
255 dprintk(KERN_WARNING "Warning: CPU frequency is %u, "
256 "cpufreq assumed %u kHz.\n", freqs->old, cpufreq_cpu_data[freqs->cpu]->cur);
257 freqs->old = cpufreq_cpu_data[freqs->cpu]->cur;
260 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_PRECHANGE, freqs);
261 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
263 case CPUFREQ_POSTCHANGE:
264 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
265 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_POSTCHANGE, freqs);
266 if ((likely(cpufreq_cpu_data[freqs->cpu])) &&
267 (likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu)))
268 cpufreq_cpu_data[freqs->cpu]->cur = freqs->new;
271 up_read(&cpufreq_notifier_rwsem);
273 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
277 /*********************************************************************
279 *********************************************************************/
282 * cpufreq_parse_governor - parse a governor string
284 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
285 struct cpufreq_governor **governor)
289 if (cpufreq_driver->setpolicy) {
290 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
291 *policy = CPUFREQ_POLICY_PERFORMANCE;
293 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
294 *policy = CPUFREQ_POLICY_POWERSAVE;
299 struct cpufreq_governor *t;
300 down(&cpufreq_governor_sem);
301 if (!cpufreq_driver || !cpufreq_driver->target)
303 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
304 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
306 up(&cpufreq_governor_sem);
311 up(&cpufreq_governor_sem);
315 EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
318 /* drivers/base/cpu.c */
319 extern struct sysdev_class cpu_sysdev_class;
323 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
325 * Write out information from cpufreq_driver->policy[cpu]; object must be
329 #define show_one(file_name, object) \
330 static ssize_t show_##file_name \
331 (struct cpufreq_policy * policy, char *buf) \
333 return sprintf (buf, "%u\n", policy->object); \
336 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
337 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
338 show_one(scaling_min_freq, min);
339 show_one(scaling_max_freq, max);
340 show_one(scaling_cur_freq, cur);
343 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
345 #define store_one(file_name, object) \
346 static ssize_t store_##file_name \
347 (struct cpufreq_policy * policy, const char *buf, size_t count) \
349 unsigned int ret = -EINVAL; \
350 struct cpufreq_policy new_policy; \
352 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
356 ret = sscanf (buf, "%u", &new_policy.object); \
360 ret = cpufreq_set_policy(&new_policy); \
362 return ret ? ret : count; \
365 store_one(scaling_min_freq,min);
366 store_one(scaling_max_freq,max);
369 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
371 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
373 unsigned int cur_freq = cpufreq_get(policy->cpu);
375 return sprintf(buf, "<unknown>");
376 return sprintf(buf, "%u\n", cur_freq);
381 * show_scaling_governor - show the current policy for the specified CPU
383 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
385 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
386 return sprintf(buf, "powersave\n");
387 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
388 return sprintf(buf, "performance\n");
389 else if (policy->governor)
390 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
396 * store_scaling_governor - store policy for the specified CPU
398 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
399 const char *buf, size_t count)
401 unsigned int ret = -EINVAL;
402 char str_governor[16];
403 struct cpufreq_policy new_policy;
405 ret = cpufreq_get_policy(&new_policy, policy->cpu);
409 ret = sscanf (buf, "%15s", str_governor);
413 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
416 ret = cpufreq_set_policy(&new_policy);
418 return ret ? ret : count;
422 * show_scaling_driver - show the cpufreq driver currently loaded
424 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
426 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
430 * show_scaling_available_governors - show the available CPUfreq governors
432 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
436 struct cpufreq_governor *t;
438 if (!cpufreq_driver->target) {
439 i += sprintf(buf, "performance powersave");
443 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
444 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
446 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
449 i += sprintf(&buf[i], "\n");
453 * show_affected_cpus - show the CPUs affected by each transition
455 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
460 for_each_cpu_mask(cpu, policy->cpus) {
462 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
463 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
464 if (i >= (PAGE_SIZE - 5))
467 i += sprintf(&buf[i], "\n");
472 #define define_one_ro(_name) \
473 static struct freq_attr _name = \
474 __ATTR(_name, 0444, show_##_name, NULL)
476 #define define_one_ro0400(_name) \
477 static struct freq_attr _name = \
478 __ATTR(_name, 0400, show_##_name, NULL)
480 #define define_one_rw(_name) \
481 static struct freq_attr _name = \
482 __ATTR(_name, 0644, show_##_name, store_##_name)
484 define_one_ro0400(cpuinfo_cur_freq);
485 define_one_ro(cpuinfo_min_freq);
486 define_one_ro(cpuinfo_max_freq);
487 define_one_ro(scaling_available_governors);
488 define_one_ro(scaling_driver);
489 define_one_ro(scaling_cur_freq);
490 define_one_ro(affected_cpus);
491 define_one_rw(scaling_min_freq);
492 define_one_rw(scaling_max_freq);
493 define_one_rw(scaling_governor);
495 static struct attribute * default_attrs[] = {
496 &cpuinfo_min_freq.attr,
497 &cpuinfo_max_freq.attr,
498 &scaling_min_freq.attr,
499 &scaling_max_freq.attr,
501 &scaling_governor.attr,
502 &scaling_driver.attr,
503 &scaling_available_governors.attr,
507 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
508 #define to_attr(a) container_of(a,struct freq_attr,attr)
510 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
512 struct cpufreq_policy * policy = to_policy(kobj);
513 struct freq_attr * fattr = to_attr(attr);
515 policy = cpufreq_cpu_get(policy->cpu);
518 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
519 cpufreq_cpu_put(policy);
523 static ssize_t store(struct kobject * kobj, struct attribute * attr,
524 const char * buf, size_t count)
526 struct cpufreq_policy * policy = to_policy(kobj);
527 struct freq_attr * fattr = to_attr(attr);
529 policy = cpufreq_cpu_get(policy->cpu);
532 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
533 cpufreq_cpu_put(policy);
537 static void cpufreq_sysfs_release(struct kobject * kobj)
539 struct cpufreq_policy * policy = to_policy(kobj);
540 dprintk("last reference is dropped\n");
541 complete(&policy->kobj_unregister);
544 static struct sysfs_ops sysfs_ops = {
549 static struct kobj_type ktype_cpufreq = {
550 .sysfs_ops = &sysfs_ops,
551 .default_attrs = default_attrs,
552 .release = cpufreq_sysfs_release,
557 * cpufreq_add_dev - add a CPU device
559 * Adds the cpufreq interface for a CPU device.
561 static int cpufreq_add_dev (struct sys_device * sys_dev)
563 unsigned int cpu = sys_dev->id;
565 struct cpufreq_policy new_policy;
566 struct cpufreq_policy *policy;
567 struct freq_attr **drv_attr;
571 if (cpu_is_offline(cpu))
574 cpufreq_debug_disable_ratelimit();
575 dprintk("adding CPU %u\n", cpu);
578 /* check whether a different CPU already registered this
579 * CPU because it is in the same boat. */
580 policy = cpufreq_cpu_get(cpu);
581 if (unlikely(policy)) {
582 dprintk("CPU already managed, adding link\n");
583 sysfs_create_link(&sys_dev->kobj, &policy->kobj, "cpufreq");
584 cpufreq_debug_enable_ratelimit();
589 if (!try_module_get(cpufreq_driver->owner)) {
594 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
601 policy->cpus = cpumask_of_cpu(cpu);
603 init_MUTEX_LOCKED(&policy->lock);
604 init_completion(&policy->kobj_unregister);
605 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
607 /* call driver. From then on the cpufreq must be able
608 * to accept all calls to ->verify and ->setpolicy for this CPU
610 ret = cpufreq_driver->init(policy);
612 dprintk("initialization failed\n");
616 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
618 /* prepare interface data */
619 policy->kobj.parent = &sys_dev->kobj;
620 policy->kobj.ktype = &ktype_cpufreq;
621 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
623 ret = kobject_register(&policy->kobj);
625 goto err_out_driver_exit;
627 /* set up files for this cpu device */
628 drv_attr = cpufreq_driver->attr;
629 while ((drv_attr) && (*drv_attr)) {
630 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
633 if (cpufreq_driver->get)
634 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
635 if (cpufreq_driver->target)
636 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
638 spin_lock_irqsave(&cpufreq_driver_lock, flags);
639 for_each_cpu_mask(j, policy->cpus)
640 cpufreq_cpu_data[j] = policy;
641 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
642 policy->governor = NULL; /* to assure that the starting sequence is
643 * run in cpufreq_set_policy */
646 /* set default policy */
648 ret = cpufreq_set_policy(&new_policy);
650 dprintk("setting policy failed\n");
651 goto err_out_unregister;
654 module_put(cpufreq_driver->owner);
655 dprintk("initialization complete\n");
656 cpufreq_debug_enable_ratelimit();
662 spin_lock_irqsave(&cpufreq_driver_lock, flags);
663 for_each_cpu_mask(j, policy->cpus)
664 cpufreq_cpu_data[j] = NULL;
665 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
667 kobject_unregister(&policy->kobj);
668 wait_for_completion(&policy->kobj_unregister);
671 if (cpufreq_driver->exit)
672 cpufreq_driver->exit(policy);
678 module_put(cpufreq_driver->owner);
680 cpufreq_debug_enable_ratelimit();
686 * cpufreq_remove_dev - remove a CPU device
688 * Removes the cpufreq interface for a CPU device.
690 static int cpufreq_remove_dev (struct sys_device * sys_dev)
692 unsigned int cpu = sys_dev->id;
694 struct cpufreq_policy *data;
696 struct sys_device *cpu_sys_dev;
700 cpufreq_debug_disable_ratelimit();
701 dprintk("unregistering CPU %u\n", cpu);
703 spin_lock_irqsave(&cpufreq_driver_lock, flags);
704 data = cpufreq_cpu_data[cpu];
707 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
708 cpufreq_debug_enable_ratelimit();
711 cpufreq_cpu_data[cpu] = NULL;
715 /* if this isn't the CPU which is the parent of the kobj, we
716 * only need to unlink, put and exit
718 if (unlikely(cpu != data->cpu)) {
719 dprintk("removing link\n");
720 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
721 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
722 cpufreq_cpu_put(data);
723 cpufreq_debug_enable_ratelimit();
729 if (!kobject_get(&data->kobj)) {
730 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
731 cpufreq_debug_enable_ratelimit();
736 /* if we have other CPUs still registered, we need to unlink them,
737 * or else wait_for_completion below will lock up. Clean the
738 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
741 if (unlikely(cpus_weight(data->cpus) > 1)) {
742 for_each_cpu_mask(j, data->cpus) {
745 cpufreq_cpu_data[j] = NULL;
749 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
751 if (unlikely(cpus_weight(data->cpus) > 1)) {
752 for_each_cpu_mask(j, data->cpus) {
755 dprintk("removing link for cpu %u\n", j);
756 cpu_sys_dev = get_cpu_sysdev(j);
757 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
758 cpufreq_cpu_put(data);
762 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
766 if (cpufreq_driver->target)
767 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
770 kobject_unregister(&data->kobj);
772 kobject_put(&data->kobj);
774 /* we need to make sure that the underlying kobj is actually
775 * not referenced anymore by anybody before we proceed with
778 dprintk("waiting for dropping of refcount\n");
779 wait_for_completion(&data->kobj_unregister);
780 dprintk("wait complete\n");
782 if (cpufreq_driver->exit)
783 cpufreq_driver->exit(data);
787 cpufreq_debug_enable_ratelimit();
793 static void handle_update(void *data)
795 unsigned int cpu = (unsigned int)(long)data;
796 dprintk("handle_update for cpu %u called\n", cpu);
797 cpufreq_update_policy(cpu);
801 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
803 * @old_freq: CPU frequency the kernel thinks the CPU runs at
804 * @new_freq: CPU frequency the CPU actually runs at
806 * We adjust to current frequency first, and need to clean up later. So either call
807 * to cpufreq_update_policy() or schedule handle_update()).
809 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
811 struct cpufreq_freqs freqs;
813 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
814 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
817 freqs.old = old_freq;
818 freqs.new = new_freq;
819 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
820 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
825 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
828 * This is the last known freq, without actually getting it from the driver.
829 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
831 unsigned int cpufreq_quick_get(unsigned int cpu)
833 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
834 unsigned int ret = 0;
840 cpufreq_cpu_put(policy);
845 EXPORT_SYMBOL(cpufreq_quick_get);
849 * cpufreq_get - get the current CPU frequency (in kHz)
852 * Get the CPU current (static) CPU frequency
854 unsigned int cpufreq_get(unsigned int cpu)
856 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
857 unsigned int ret = 0;
862 if (!cpufreq_driver->get)
867 ret = cpufreq_driver->get(cpu);
869 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS))
871 /* verify no discrepancy between actual and saved value exists */
872 if (unlikely(ret != policy->cur)) {
873 cpufreq_out_of_sync(cpu, policy->cur, ret);
874 schedule_work(&policy->update);
881 cpufreq_cpu_put(policy);
885 EXPORT_SYMBOL(cpufreq_get);
889 * cpufreq_suspend - let the low level driver prepare for suspend
892 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
894 int cpu = sysdev->id;
895 unsigned int ret = 0;
896 unsigned int cur_freq = 0;
897 struct cpufreq_policy *cpu_policy;
899 dprintk("resuming cpu %u\n", cpu);
901 if (!cpu_online(cpu))
904 /* we may be lax here as interrupts are off. Nonetheless
905 * we need to grab the correct cpu policy, as to check
906 * whether we really run on this CPU.
909 cpu_policy = cpufreq_cpu_get(cpu);
913 /* only handle each CPU group once */
914 if (unlikely(cpu_policy->cpu != cpu)) {
915 cpufreq_cpu_put(cpu_policy);
919 if (cpufreq_driver->suspend) {
920 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
922 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
923 "step on CPU %u\n", cpu_policy->cpu);
924 cpufreq_cpu_put(cpu_policy);
930 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
933 if (cpufreq_driver->get)
934 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
936 if (!cur_freq || !cpu_policy->cur) {
937 printk(KERN_ERR "cpufreq: suspend failed to assert current "
938 "frequency is what timing core thinks it is.\n");
942 if (unlikely(cur_freq != cpu_policy->cur)) {
943 struct cpufreq_freqs freqs;
945 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
946 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
947 "cpufreq assumed %u kHz.\n",
948 cur_freq, cpu_policy->cur);
951 freqs.old = cpu_policy->cur;
952 freqs.new = cur_freq;
954 notifier_call_chain(&cpufreq_transition_notifier_list,
955 CPUFREQ_SUSPENDCHANGE, &freqs);
956 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
958 cpu_policy->cur = cur_freq;
962 cpufreq_cpu_put(cpu_policy);
967 * cpufreq_resume - restore proper CPU frequency handling after resume
969 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
970 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
971 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
974 static int cpufreq_resume(struct sys_device * sysdev)
976 int cpu = sysdev->id;
977 unsigned int ret = 0;
978 struct cpufreq_policy *cpu_policy;
980 dprintk("resuming cpu %u\n", cpu);
982 if (!cpu_online(cpu))
985 /* we may be lax here as interrupts are off. Nonetheless
986 * we need to grab the correct cpu policy, as to check
987 * whether we really run on this CPU.
990 cpu_policy = cpufreq_cpu_get(cpu);
994 /* only handle each CPU group once */
995 if (unlikely(cpu_policy->cpu != cpu)) {
996 cpufreq_cpu_put(cpu_policy);
1000 if (cpufreq_driver->resume) {
1001 ret = cpufreq_driver->resume(cpu_policy);
1003 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1004 "step on CPU %u\n", cpu_policy->cpu);
1005 cpufreq_cpu_put(cpu_policy);
1010 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1011 unsigned int cur_freq = 0;
1013 if (cpufreq_driver->get)
1014 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1016 if (!cur_freq || !cpu_policy->cur) {
1017 printk(KERN_ERR "cpufreq: resume failed to assert "
1018 "current frequency is what timing core "
1023 if (unlikely(cur_freq != cpu_policy->cur)) {
1024 struct cpufreq_freqs freqs;
1026 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1027 dprintk(KERN_WARNING "Warning: CPU frequency"
1028 "is %u, cpufreq assumed %u kHz.\n",
1029 cur_freq, cpu_policy->cur);
1032 freqs.old = cpu_policy->cur;
1033 freqs.new = cur_freq;
1035 notifier_call_chain(&cpufreq_transition_notifier_list,
1036 CPUFREQ_RESUMECHANGE, &freqs);
1037 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1039 cpu_policy->cur = cur_freq;
1044 schedule_work(&cpu_policy->update);
1045 cpufreq_cpu_put(cpu_policy);
1049 static struct sysdev_driver cpufreq_sysdev_driver = {
1050 .add = cpufreq_add_dev,
1051 .remove = cpufreq_remove_dev,
1052 .suspend = cpufreq_suspend,
1053 .resume = cpufreq_resume,
1057 /*********************************************************************
1058 * NOTIFIER LISTS INTERFACE *
1059 *********************************************************************/
1062 * cpufreq_register_notifier - register a driver with cpufreq
1063 * @nb: notifier function to register
1064 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1066 * Add a driver to one of two lists: either a list of drivers that
1067 * are notified about clock rate changes (once before and once after
1068 * the transition), or a list of drivers that are notified about
1069 * changes in cpufreq policy.
1071 * This function may sleep, and has the same return conditions as
1072 * notifier_chain_register.
1074 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1078 down_write(&cpufreq_notifier_rwsem);
1080 case CPUFREQ_TRANSITION_NOTIFIER:
1081 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
1083 case CPUFREQ_POLICY_NOTIFIER:
1084 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
1089 up_write(&cpufreq_notifier_rwsem);
1093 EXPORT_SYMBOL(cpufreq_register_notifier);
1097 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1098 * @nb: notifier block to be unregistered
1099 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1101 * Remove a driver from the CPU frequency notifier list.
1103 * This function may sleep, and has the same return conditions as
1104 * notifier_chain_unregister.
1106 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1110 down_write(&cpufreq_notifier_rwsem);
1112 case CPUFREQ_TRANSITION_NOTIFIER:
1113 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
1115 case CPUFREQ_POLICY_NOTIFIER:
1116 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
1121 up_write(&cpufreq_notifier_rwsem);
1125 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1128 /*********************************************************************
1130 *********************************************************************/
1133 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1134 unsigned int target_freq,
1135 unsigned int relation)
1137 int retval = -EINVAL;
1140 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1141 target_freq, relation);
1142 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1143 retval = cpufreq_driver->target(policy, target_freq, relation);
1145 unlock_cpu_hotplug();
1149 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1151 int cpufreq_driver_target(struct cpufreq_policy *policy,
1152 unsigned int target_freq,
1153 unsigned int relation)
1157 policy = cpufreq_cpu_get(policy->cpu);
1161 down(&policy->lock);
1163 ret = __cpufreq_driver_target(policy, target_freq, relation);
1167 cpufreq_cpu_put(policy);
1171 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1174 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1178 if (!try_module_get(policy->governor->owner))
1181 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1182 ret = policy->governor->governor(policy, event);
1184 /* we keep one module reference alive for each CPU governed by this CPU */
1185 if ((event != CPUFREQ_GOV_START) || ret)
1186 module_put(policy->governor->owner);
1187 if ((event == CPUFREQ_GOV_STOP) && !ret)
1188 module_put(policy->governor->owner);
1194 int cpufreq_governor(unsigned int cpu, unsigned int event)
1197 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1202 down(&policy->lock);
1203 ret = __cpufreq_governor(policy, event);
1206 cpufreq_cpu_put(policy);
1210 EXPORT_SYMBOL_GPL(cpufreq_governor);
1213 int cpufreq_register_governor(struct cpufreq_governor *governor)
1215 struct cpufreq_governor *t;
1220 down(&cpufreq_governor_sem);
1222 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1223 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1224 up(&cpufreq_governor_sem);
1228 list_add(&governor->governor_list, &cpufreq_governor_list);
1230 up(&cpufreq_governor_sem);
1234 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1237 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1242 down(&cpufreq_governor_sem);
1243 list_del(&governor->governor_list);
1244 up(&cpufreq_governor_sem);
1247 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1251 /*********************************************************************
1252 * POLICY INTERFACE *
1253 *********************************************************************/
1256 * cpufreq_get_policy - get the current cpufreq_policy
1257 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1259 * Reads the current cpufreq policy.
1261 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1263 struct cpufreq_policy *cpu_policy;
1267 cpu_policy = cpufreq_cpu_get(cpu);
1271 down(&cpu_policy->lock);
1272 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1273 up(&cpu_policy->lock);
1275 cpufreq_cpu_put(cpu_policy);
1279 EXPORT_SYMBOL(cpufreq_get_policy);
1282 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1286 cpufreq_debug_disable_ratelimit();
1287 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1288 policy->min, policy->max);
1290 memcpy(&policy->cpuinfo,
1292 sizeof(struct cpufreq_cpuinfo));
1294 /* verify the cpu speed can be set within this limit */
1295 ret = cpufreq_driver->verify(policy);
1299 down_read(&cpufreq_notifier_rwsem);
1301 /* adjust if necessary - all reasons */
1302 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
1305 /* adjust if necessary - hardware incompatibility*/
1306 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
1309 /* verify the cpu speed can be set within this limit,
1310 which might be different to the first one */
1311 ret = cpufreq_driver->verify(policy);
1313 up_read(&cpufreq_notifier_rwsem);
1317 /* notification of the new policy */
1318 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
1321 up_read(&cpufreq_notifier_rwsem);
1323 data->min = policy->min;
1324 data->max = policy->max;
1326 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1328 if (cpufreq_driver->setpolicy) {
1329 data->policy = policy->policy;
1330 dprintk("setting range\n");
1331 ret = cpufreq_driver->setpolicy(policy);
1333 if (policy->governor != data->governor) {
1334 /* save old, working values */
1335 struct cpufreq_governor *old_gov = data->governor;
1337 dprintk("governor switch\n");
1339 /* end old governor */
1341 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1343 /* start new governor */
1344 data->governor = policy->governor;
1345 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1346 /* new governor failed, so re-start old one */
1347 dprintk("starting governor %s failed\n", data->governor->name);
1349 data->governor = old_gov;
1350 __cpufreq_governor(data, CPUFREQ_GOV_START);
1355 /* might be a policy change, too, so fall through */
1357 dprintk("governor: change or update limits\n");
1358 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1362 cpufreq_debug_enable_ratelimit();
1367 * cpufreq_set_policy - set a new CPUFreq policy
1368 * @policy: policy to be set.
1370 * Sets a new CPU frequency and voltage scaling policy.
1372 int cpufreq_set_policy(struct cpufreq_policy *policy)
1375 struct cpufreq_policy *data;
1380 data = cpufreq_cpu_get(policy->cpu);
1387 ret = __cpufreq_set_policy(data, policy);
1388 data->user_policy.min = data->min;
1389 data->user_policy.max = data->max;
1390 data->user_policy.policy = data->policy;
1391 data->user_policy.governor = data->governor;
1394 cpufreq_cpu_put(data);
1398 EXPORT_SYMBOL(cpufreq_set_policy);
1402 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1403 * @cpu: CPU which shall be re-evaluated
1405 * Usefull for policy notifiers which have different necessities
1406 * at different times.
1408 int cpufreq_update_policy(unsigned int cpu)
1410 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1411 struct cpufreq_policy policy;
1419 dprintk("updating policy for CPU %u\n", cpu);
1422 sizeof(struct cpufreq_policy));
1423 policy.min = data->user_policy.min;
1424 policy.max = data->user_policy.max;
1425 policy.policy = data->user_policy.policy;
1426 policy.governor = data->user_policy.governor;
1428 ret = __cpufreq_set_policy(data, &policy);
1432 cpufreq_cpu_put(data);
1435 EXPORT_SYMBOL(cpufreq_update_policy);
1437 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1438 unsigned long action, void *hcpu)
1440 unsigned int cpu = (unsigned long)hcpu;
1441 struct cpufreq_policy *policy;
1442 struct sys_device *sys_dev;
1444 sys_dev = get_cpu_sysdev(cpu);
1449 cpufreq_add_dev(sys_dev);
1451 case CPU_DOWN_PREPARE:
1453 * We attempt to put this cpu in lowest frequency
1454 * possible before going down. This will permit
1455 * hardware-managed P-State to switch other related
1456 * threads to min or higher speeds if possible.
1458 policy = cpufreq_cpu_data[cpu];
1460 cpufreq_driver_target(policy, policy->min,
1461 CPUFREQ_RELATION_H);
1465 cpufreq_remove_dev(sys_dev);
1472 static struct notifier_block cpufreq_cpu_notifier =
1474 .notifier_call = cpufreq_cpu_callback,
1477 /*********************************************************************
1478 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1479 *********************************************************************/
1482 * cpufreq_register_driver - register a CPU Frequency driver
1483 * @driver_data: A struct cpufreq_driver containing the values#
1484 * submitted by the CPU Frequency driver.
1486 * Registers a CPU Frequency driver to this core code. This code
1487 * returns zero on success, -EBUSY when another driver got here first
1488 * (and isn't unregistered in the meantime).
1491 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1493 unsigned long flags;
1496 if (!driver_data || !driver_data->verify || !driver_data->init ||
1497 ((!driver_data->setpolicy) && (!driver_data->target)))
1500 dprintk("trying to register driver %s\n", driver_data->name);
1502 if (driver_data->setpolicy)
1503 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1505 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1506 if (cpufreq_driver) {
1507 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1510 cpufreq_driver = driver_data;
1511 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1513 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1515 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1519 /* check for at least one working CPU */
1520 for (i=0; i<NR_CPUS; i++)
1521 if (cpufreq_cpu_data[i])
1524 /* if all ->init() calls failed, unregister */
1526 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1527 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1529 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1530 cpufreq_driver = NULL;
1531 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1536 register_cpu_notifier(&cpufreq_cpu_notifier);
1537 dprintk("driver %s up and running\n", driver_data->name);
1538 cpufreq_debug_enable_ratelimit();
1543 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1547 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1549 * Unregister the current CPUFreq driver. Only call this if you have
1550 * the right to do so, i.e. if you have succeeded in initialising before!
1551 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1552 * currently not initialised.
1554 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1556 unsigned long flags;
1558 cpufreq_debug_disable_ratelimit();
1560 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1561 cpufreq_debug_enable_ratelimit();
1565 dprintk("unregistering driver %s\n", driver->name);
1567 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1568 unregister_cpu_notifier(&cpufreq_cpu_notifier);
1570 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1571 cpufreq_driver = NULL;
1572 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1576 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);