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);
42 /* internal prototypes */
43 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
44 static void handle_update(void *data);
45 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci);
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 struct notifier_block *cpufreq_policy_notifier_list;
55 static struct notifier_block *cpufreq_transition_notifier_list;
56 static DECLARE_RWSEM (cpufreq_notifier_rwsem);
59 static LIST_HEAD(cpufreq_governor_list);
60 static DECLARE_MUTEX (cpufreq_governor_sem);
62 struct cpufreq_policy * cpufreq_cpu_get(unsigned int cpu)
64 struct cpufreq_policy *data;
70 /* get the cpufreq driver */
71 spin_lock_irqsave(&cpufreq_driver_lock, flags);
76 if (!try_module_get(cpufreq_driver->owner))
81 data = cpufreq_cpu_data[cpu];
84 goto err_out_put_module;
86 if (!kobject_get(&data->kobj))
87 goto err_out_put_module;
90 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
95 module_put(cpufreq_driver->owner);
97 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
101 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
103 void cpufreq_cpu_put(struct cpufreq_policy *data)
105 kobject_put(&data->kobj);
106 module_put(cpufreq_driver->owner);
108 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
111 /*********************************************************************
112 * UNIFIED DEBUG HELPERS *
113 *********************************************************************/
114 #ifdef CONFIG_CPU_FREQ_DEBUG
116 /* what part(s) of the CPUfreq subsystem are debugged? */
117 static unsigned int debug;
119 /* is the debug output ratelimit'ed using printk_ratelimit? User can
120 * set or modify this value.
122 static unsigned int debug_ratelimit = 1;
124 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
125 * loading of a cpufreq driver, temporarily disabled when a new policy
126 * is set, and disabled upon cpufreq driver removal
128 static unsigned int disable_ratelimit = 1;
129 static DEFINE_SPINLOCK(disable_ratelimit_lock);
131 static inline void cpufreq_debug_enable_ratelimit(void)
135 spin_lock_irqsave(&disable_ratelimit_lock, flags);
136 if (disable_ratelimit)
138 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
141 static inline void cpufreq_debug_disable_ratelimit(void)
145 spin_lock_irqsave(&disable_ratelimit_lock, flags);
147 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
150 void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
159 spin_lock_irqsave(&disable_ratelimit_lock, flags);
160 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
161 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
164 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
166 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
169 len += vsnprintf(&s[len], (256 - len), fmt, args);
177 EXPORT_SYMBOL(cpufreq_debug_printk);
180 module_param(debug, uint, 0644);
181 MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
183 module_param(debug_ratelimit, uint, 0644);
184 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
186 #else /* !CONFIG_CPU_FREQ_DEBUG */
188 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
189 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
191 #endif /* CONFIG_CPU_FREQ_DEBUG */
194 /*********************************************************************
195 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
196 *********************************************************************/
199 * adjust_jiffies - adjust the system "loops_per_jiffy"
201 * This function alters the system "loops_per_jiffy" for the clock
202 * speed change. Note that loops_per_jiffy cannot be updated on SMP
203 * systems as each CPU might be scaled differently. So, use the arch
204 * per-CPU loops_per_jiffy value wherever possible.
207 static unsigned long l_p_j_ref;
208 static unsigned int l_p_j_ref_freq;
210 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
212 if (ci->flags & CPUFREQ_CONST_LOOPS)
215 if (!l_p_j_ref_freq) {
216 l_p_j_ref = loops_per_jiffy;
217 l_p_j_ref_freq = ci->old;
218 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
220 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
221 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
222 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
223 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
224 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
228 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
233 * cpufreq_notify_transition - call notifier chain and adjust_jiffies on frequency transition
235 * This function calls the transition notifiers and the "adjust_jiffies" function. It is called
236 * twice on all CPU frequency changes that have external effects.
238 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
240 BUG_ON(irqs_disabled());
242 freqs->flags = cpufreq_driver->flags;
243 dprintk("notification %u of frequency transition to %u kHz\n", state, freqs->new);
245 down_read(&cpufreq_notifier_rwsem);
247 case CPUFREQ_PRECHANGE:
248 /* detect if the driver reported a value as "old frequency" which
249 * is not equal to what the cpufreq core thinks is "old frequency".
251 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
252 if ((likely(cpufreq_cpu_data[freqs->cpu])) &&
253 (likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu)) &&
254 (likely(cpufreq_cpu_data[freqs->cpu]->cur)) &&
255 (unlikely(freqs->old != cpufreq_cpu_data[freqs->cpu]->cur)))
257 dprintk(KERN_WARNING "Warning: CPU frequency is %u, "
258 "cpufreq assumed %u kHz.\n", freqs->old, cpufreq_cpu_data[freqs->cpu]->cur);
259 freqs->old = cpufreq_cpu_data[freqs->cpu]->cur;
262 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_PRECHANGE, freqs);
263 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
265 case CPUFREQ_POSTCHANGE:
266 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
267 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_POSTCHANGE, freqs);
268 if ((likely(cpufreq_cpu_data[freqs->cpu])) &&
269 (likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu)))
270 cpufreq_cpu_data[freqs->cpu]->cur = freqs->new;
273 up_read(&cpufreq_notifier_rwsem);
275 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
279 /*********************************************************************
281 *********************************************************************/
284 * cpufreq_parse_governor - parse a governor string
286 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
287 struct cpufreq_governor **governor)
291 if (cpufreq_driver->setpolicy) {
292 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
293 *policy = CPUFREQ_POLICY_PERFORMANCE;
295 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
296 *policy = CPUFREQ_POLICY_POWERSAVE;
301 struct cpufreq_governor *t;
302 down(&cpufreq_governor_sem);
303 if (!cpufreq_driver || !cpufreq_driver->target)
305 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
306 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
308 up(&cpufreq_governor_sem);
313 up(&cpufreq_governor_sem);
317 EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
320 /* drivers/base/cpu.c */
321 extern struct sysdev_class cpu_sysdev_class;
325 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
327 * Write out information from cpufreq_driver->policy[cpu]; object must be
331 #define show_one(file_name, object) \
332 static ssize_t show_##file_name \
333 (struct cpufreq_policy * policy, char *buf) \
335 return sprintf (buf, "%u\n", policy->object); \
338 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
339 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
340 show_one(scaling_min_freq, min);
341 show_one(scaling_max_freq, max);
342 show_one(scaling_cur_freq, cur);
345 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
347 #define store_one(file_name, object) \
348 static ssize_t store_##file_name \
349 (struct cpufreq_policy * policy, const char *buf, size_t count) \
351 unsigned int ret = -EINVAL; \
352 struct cpufreq_policy new_policy; \
354 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
358 ret = sscanf (buf, "%u", &new_policy.object); \
362 ret = cpufreq_set_policy(&new_policy); \
364 return ret ? ret : count; \
367 store_one(scaling_min_freq,min);
368 store_one(scaling_max_freq,max);
371 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
373 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
375 unsigned int cur_freq = cpufreq_get(policy->cpu);
377 return sprintf(buf, "<unknown>");
378 return sprintf(buf, "%u\n", cur_freq);
383 * show_scaling_governor - show the current policy for the specified CPU
385 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
387 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
388 return sprintf(buf, "powersave\n");
389 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
390 return sprintf(buf, "performance\n");
391 else if (policy->governor)
392 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
398 * store_scaling_governor - store policy for the specified CPU
400 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
401 const char *buf, size_t count)
403 unsigned int ret = -EINVAL;
404 char str_governor[16];
405 struct cpufreq_policy new_policy;
407 ret = cpufreq_get_policy(&new_policy, policy->cpu);
411 ret = sscanf (buf, "%15s", str_governor);
415 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
418 ret = cpufreq_set_policy(&new_policy);
420 return ret ? ret : count;
424 * show_scaling_driver - show the cpufreq driver currently loaded
426 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
428 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
432 * show_scaling_available_governors - show the available CPUfreq governors
434 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
438 struct cpufreq_governor *t;
440 if (!cpufreq_driver->target) {
441 i += sprintf(buf, "performance powersave");
445 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
446 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
448 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
451 i += sprintf(&buf[i], "\n");
455 * show_affected_cpus - show the CPUs affected by each transition
457 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
462 for_each_cpu_mask(cpu, policy->cpus) {
464 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
465 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
466 if (i >= (PAGE_SIZE - 5))
469 i += sprintf(&buf[i], "\n");
474 #define define_one_ro(_name) \
475 static struct freq_attr _name = \
476 __ATTR(_name, 0444, show_##_name, NULL)
478 #define define_one_ro0400(_name) \
479 static struct freq_attr _name = \
480 __ATTR(_name, 0400, show_##_name, NULL)
482 #define define_one_rw(_name) \
483 static struct freq_attr _name = \
484 __ATTR(_name, 0644, show_##_name, store_##_name)
486 define_one_ro0400(cpuinfo_cur_freq);
487 define_one_ro(cpuinfo_min_freq);
488 define_one_ro(cpuinfo_max_freq);
489 define_one_ro(scaling_available_governors);
490 define_one_ro(scaling_driver);
491 define_one_ro(scaling_cur_freq);
492 define_one_ro(affected_cpus);
493 define_one_rw(scaling_min_freq);
494 define_one_rw(scaling_max_freq);
495 define_one_rw(scaling_governor);
497 static struct attribute * default_attrs[] = {
498 &cpuinfo_min_freq.attr,
499 &cpuinfo_max_freq.attr,
500 &scaling_min_freq.attr,
501 &scaling_max_freq.attr,
503 &scaling_governor.attr,
504 &scaling_driver.attr,
505 &scaling_available_governors.attr,
509 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
510 #define to_attr(a) container_of(a,struct freq_attr,attr)
512 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
514 struct cpufreq_policy * policy = to_policy(kobj);
515 struct freq_attr * fattr = to_attr(attr);
517 policy = cpufreq_cpu_get(policy->cpu);
520 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
521 cpufreq_cpu_put(policy);
525 static ssize_t store(struct kobject * kobj, struct attribute * attr,
526 const char * buf, size_t count)
528 struct cpufreq_policy * policy = to_policy(kobj);
529 struct freq_attr * fattr = to_attr(attr);
531 policy = cpufreq_cpu_get(policy->cpu);
534 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
535 cpufreq_cpu_put(policy);
539 static void cpufreq_sysfs_release(struct kobject * kobj)
541 struct cpufreq_policy * policy = to_policy(kobj);
542 dprintk("last reference is dropped\n");
543 complete(&policy->kobj_unregister);
546 static struct sysfs_ops sysfs_ops = {
551 static struct kobj_type ktype_cpufreq = {
552 .sysfs_ops = &sysfs_ops,
553 .default_attrs = default_attrs,
554 .release = cpufreq_sysfs_release,
559 * cpufreq_add_dev - add a CPU device
561 * Adds the cpufreq interface for a CPU device.
563 static int cpufreq_add_dev (struct sys_device * sys_dev)
565 unsigned int cpu = sys_dev->id;
567 struct cpufreq_policy new_policy;
568 struct cpufreq_policy *policy;
569 struct freq_attr **drv_attr;
573 if (cpu_is_offline(cpu))
576 cpufreq_debug_disable_ratelimit();
577 dprintk("adding CPU %u\n", cpu);
580 /* check whether a different CPU already registered this
581 * CPU because it is in the same boat. */
582 policy = cpufreq_cpu_get(cpu);
583 if (unlikely(policy)) {
584 dprintk("CPU already managed, adding link\n");
585 sysfs_create_link(&sys_dev->kobj, &policy->kobj, "cpufreq");
586 cpufreq_debug_enable_ratelimit();
591 if (!try_module_get(cpufreq_driver->owner)) {
596 policy = kmalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
601 memset(policy, 0, sizeof(struct cpufreq_policy));
604 policy->cpus = cpumask_of_cpu(cpu);
606 init_MUTEX_LOCKED(&policy->lock);
607 init_completion(&policy->kobj_unregister);
608 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
610 /* call driver. From then on the cpufreq must be able
611 * to accept all calls to ->verify and ->setpolicy for this CPU
613 ret = cpufreq_driver->init(policy);
615 dprintk("initialization failed\n");
619 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
621 /* prepare interface data */
622 policy->kobj.parent = &sys_dev->kobj;
623 policy->kobj.ktype = &ktype_cpufreq;
624 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
626 ret = kobject_register(&policy->kobj);
628 goto err_out_driver_exit;
630 /* set up files for this cpu device */
631 drv_attr = cpufreq_driver->attr;
632 while ((drv_attr) && (*drv_attr)) {
633 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
636 if (cpufreq_driver->get)
637 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
638 if (cpufreq_driver->target)
639 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
641 spin_lock_irqsave(&cpufreq_driver_lock, flags);
642 for_each_cpu_mask(j, policy->cpus)
643 cpufreq_cpu_data[j] = policy;
644 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
645 policy->governor = NULL; /* to assure that the starting sequence is
646 * run in cpufreq_set_policy */
649 /* set default policy */
651 ret = cpufreq_set_policy(&new_policy);
653 dprintk("setting policy failed\n");
654 goto err_out_unregister;
657 module_put(cpufreq_driver->owner);
658 dprintk("initialization complete\n");
659 cpufreq_debug_enable_ratelimit();
665 spin_lock_irqsave(&cpufreq_driver_lock, flags);
666 for_each_cpu_mask(j, policy->cpus)
667 cpufreq_cpu_data[j] = NULL;
668 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
670 kobject_unregister(&policy->kobj);
671 wait_for_completion(&policy->kobj_unregister);
674 if (cpufreq_driver->exit)
675 cpufreq_driver->exit(policy);
681 module_put(cpufreq_driver->owner);
683 cpufreq_debug_enable_ratelimit();
689 * cpufreq_remove_dev - remove a CPU device
691 * Removes the cpufreq interface for a CPU device.
693 static int cpufreq_remove_dev (struct sys_device * sys_dev)
695 unsigned int cpu = sys_dev->id;
697 struct cpufreq_policy *data;
698 struct sys_device *cpu_sys_dev;
703 cpufreq_debug_disable_ratelimit();
704 dprintk("unregistering CPU %u\n", cpu);
706 spin_lock_irqsave(&cpufreq_driver_lock, flags);
707 data = cpufreq_cpu_data[cpu];
710 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
711 cpufreq_debug_enable_ratelimit();
714 cpufreq_cpu_data[cpu] = NULL;
718 /* if this isn't the CPU which is the parent of the kobj, we
719 * only need to unlink, put and exit
721 if (unlikely(cpu != data->cpu)) {
722 dprintk("removing link\n");
723 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
724 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
725 cpufreq_cpu_put(data);
726 cpufreq_debug_enable_ratelimit();
732 if (!kobject_get(&data->kobj)) {
733 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
734 cpufreq_debug_enable_ratelimit();
739 /* if we have other CPUs still registered, we need to unlink them,
740 * or else wait_for_completion below will lock up. Clean the
741 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
744 if (unlikely(cpus_weight(data->cpus) > 1)) {
745 for_each_cpu_mask(j, data->cpus) {
748 cpufreq_cpu_data[j] = NULL;
752 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
754 if (unlikely(cpus_weight(data->cpus) > 1)) {
755 for_each_cpu_mask(j, data->cpus) {
758 dprintk("removing link for cpu %u\n", j);
759 cpu_sys_dev = get_cpu_sysdev(j);
760 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
761 cpufreq_cpu_put(data);
765 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
769 if (cpufreq_driver->target)
770 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
773 kobject_unregister(&data->kobj);
775 kobject_put(&data->kobj);
777 /* we need to make sure that the underlying kobj is actually
778 * not referenced anymore by anybody before we proceed with
781 dprintk("waiting for dropping of refcount\n");
782 wait_for_completion(&data->kobj_unregister);
783 dprintk("wait complete\n");
785 if (cpufreq_driver->exit)
786 cpufreq_driver->exit(data);
790 cpufreq_debug_enable_ratelimit();
796 static void handle_update(void *data)
798 unsigned int cpu = (unsigned int)(long)data;
799 dprintk("handle_update for cpu %u called\n", cpu);
800 cpufreq_update_policy(cpu);
804 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
806 * @old_freq: CPU frequency the kernel thinks the CPU runs at
807 * @new_freq: CPU frequency the CPU actually runs at
809 * We adjust to current frequency first, and need to clean up later. So either call
810 * to cpufreq_update_policy() or schedule handle_update()).
812 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
814 struct cpufreq_freqs freqs;
816 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
817 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
820 freqs.old = old_freq;
821 freqs.new = new_freq;
822 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
823 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
828 * cpufreq_get - get the current CPU frequency (in kHz)
831 * Get the CPU current (static) CPU frequency
833 unsigned int cpufreq_get(unsigned int cpu)
835 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
836 unsigned int ret = 0;
841 if (!cpufreq_driver->get)
846 ret = cpufreq_driver->get(cpu);
848 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS))
850 /* verify no discrepancy between actual and saved value exists */
851 if (unlikely(ret != policy->cur)) {
852 cpufreq_out_of_sync(cpu, policy->cur, ret);
853 schedule_work(&policy->update);
860 cpufreq_cpu_put(policy);
864 EXPORT_SYMBOL(cpufreq_get);
868 * cpufreq_suspend - let the low level driver prepare for suspend
871 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
873 int cpu = sysdev->id;
874 unsigned int ret = 0;
875 unsigned int cur_freq = 0;
876 struct cpufreq_policy *cpu_policy;
878 dprintk("resuming cpu %u\n", cpu);
880 if (!cpu_online(cpu))
883 /* we may be lax here as interrupts are off. Nonetheless
884 * we need to grab the correct cpu policy, as to check
885 * whether we really run on this CPU.
888 cpu_policy = cpufreq_cpu_get(cpu);
892 /* only handle each CPU group once */
893 if (unlikely(cpu_policy->cpu != cpu)) {
894 cpufreq_cpu_put(cpu_policy);
898 if (cpufreq_driver->suspend) {
899 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
901 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
902 "step on CPU %u\n", cpu_policy->cpu);
903 cpufreq_cpu_put(cpu_policy);
909 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
912 if (cpufreq_driver->get)
913 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
915 if (!cur_freq || !cpu_policy->cur) {
916 printk(KERN_ERR "cpufreq: suspend failed to assert current "
917 "frequency is what timing core thinks it is.\n");
921 if (unlikely(cur_freq != cpu_policy->cur)) {
922 struct cpufreq_freqs freqs;
924 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
925 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
926 "cpufreq assumed %u kHz.\n",
927 cur_freq, cpu_policy->cur);
930 freqs.old = cpu_policy->cur;
931 freqs.new = cur_freq;
933 notifier_call_chain(&cpufreq_transition_notifier_list,
934 CPUFREQ_SUSPENDCHANGE, &freqs);
935 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
937 cpu_policy->cur = cur_freq;
941 cpufreq_cpu_put(cpu_policy);
946 * cpufreq_resume - restore proper CPU frequency handling after resume
948 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
949 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
950 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
953 static int cpufreq_resume(struct sys_device * sysdev)
955 int cpu = sysdev->id;
956 unsigned int ret = 0;
957 struct cpufreq_policy *cpu_policy;
959 dprintk("resuming cpu %u\n", cpu);
961 if (!cpu_online(cpu))
964 /* we may be lax here as interrupts are off. Nonetheless
965 * we need to grab the correct cpu policy, as to check
966 * whether we really run on this CPU.
969 cpu_policy = cpufreq_cpu_get(cpu);
973 /* only handle each CPU group once */
974 if (unlikely(cpu_policy->cpu != cpu)) {
975 cpufreq_cpu_put(cpu_policy);
979 if (cpufreq_driver->resume) {
980 ret = cpufreq_driver->resume(cpu_policy);
982 printk(KERN_ERR "cpufreq: resume failed in ->resume "
983 "step on CPU %u\n", cpu_policy->cpu);
984 cpufreq_cpu_put(cpu_policy);
989 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
990 unsigned int cur_freq = 0;
992 if (cpufreq_driver->get)
993 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
995 if (!cur_freq || !cpu_policy->cur) {
996 printk(KERN_ERR "cpufreq: resume failed to assert "
997 "current frequency is what timing core "
1002 if (unlikely(cur_freq != cpu_policy->cur)) {
1003 struct cpufreq_freqs freqs;
1005 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1006 dprintk(KERN_WARNING "Warning: CPU frequency"
1007 "is %u, cpufreq assumed %u kHz.\n",
1008 cur_freq, cpu_policy->cur);
1011 freqs.old = cpu_policy->cur;
1012 freqs.new = cur_freq;
1014 notifier_call_chain(&cpufreq_transition_notifier_list,
1015 CPUFREQ_RESUMECHANGE, &freqs);
1016 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1018 cpu_policy->cur = cur_freq;
1023 schedule_work(&cpu_policy->update);
1024 cpufreq_cpu_put(cpu_policy);
1028 static struct sysdev_driver cpufreq_sysdev_driver = {
1029 .add = cpufreq_add_dev,
1030 .remove = cpufreq_remove_dev,
1031 .suspend = cpufreq_suspend,
1032 .resume = cpufreq_resume,
1036 /*********************************************************************
1037 * NOTIFIER LISTS INTERFACE *
1038 *********************************************************************/
1041 * cpufreq_register_notifier - register a driver with cpufreq
1042 * @nb: notifier function to register
1043 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1045 * Add a driver to one of two lists: either a list of drivers that
1046 * are notified about clock rate changes (once before and once after
1047 * the transition), or a list of drivers that are notified about
1048 * changes in cpufreq policy.
1050 * This function may sleep, and has the same return conditions as
1051 * notifier_chain_register.
1053 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1057 down_write(&cpufreq_notifier_rwsem);
1059 case CPUFREQ_TRANSITION_NOTIFIER:
1060 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
1062 case CPUFREQ_POLICY_NOTIFIER:
1063 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
1068 up_write(&cpufreq_notifier_rwsem);
1072 EXPORT_SYMBOL(cpufreq_register_notifier);
1076 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1077 * @nb: notifier block to be unregistered
1078 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1080 * Remove a driver from the CPU frequency notifier list.
1082 * This function may sleep, and has the same return conditions as
1083 * notifier_chain_unregister.
1085 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1089 down_write(&cpufreq_notifier_rwsem);
1091 case CPUFREQ_TRANSITION_NOTIFIER:
1092 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
1094 case CPUFREQ_POLICY_NOTIFIER:
1095 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
1100 up_write(&cpufreq_notifier_rwsem);
1104 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1107 /*********************************************************************
1109 *********************************************************************/
1112 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1113 unsigned int target_freq,
1114 unsigned int relation)
1116 int retval = -EINVAL;
1119 * Converted the lock_cpu_hotplug to preempt_disable()
1120 * and preempt_enable(). This is a bit kludgy and relies on how cpu
1121 * hotplug works. All we need is a guarantee that cpu hotplug won't make
1122 * progress on any cpu. Once we do preempt_disable(), this would ensure
1123 * that hotplug threads don't get onto this cpu, thereby delaying
1124 * the cpu remove process.
1126 * We removed the lock_cpu_hotplug since we need to call this function
1127 * via cpu hotplug callbacks, which result in locking the cpu hotplug
1128 * thread itself. Agree this is not very clean, cpufreq community
1129 * could improve this if required. - Ashok Raj <ashok.raj@intel.com>
1132 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1133 target_freq, relation);
1134 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1135 retval = cpufreq_driver->target(policy, target_freq, relation);
1139 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1141 int cpufreq_driver_target(struct cpufreq_policy *policy,
1142 unsigned int target_freq,
1143 unsigned int relation)
1147 policy = cpufreq_cpu_get(policy->cpu);
1151 down(&policy->lock);
1153 ret = __cpufreq_driver_target(policy, target_freq, relation);
1157 cpufreq_cpu_put(policy);
1161 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1164 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1168 if (!try_module_get(policy->governor->owner))
1171 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1172 ret = policy->governor->governor(policy, event);
1174 /* we keep one module reference alive for each CPU governed by this CPU */
1175 if ((event != CPUFREQ_GOV_START) || ret)
1176 module_put(policy->governor->owner);
1177 if ((event == CPUFREQ_GOV_STOP) && !ret)
1178 module_put(policy->governor->owner);
1184 int cpufreq_governor(unsigned int cpu, unsigned int event)
1187 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1192 down(&policy->lock);
1193 ret = __cpufreq_governor(policy, event);
1196 cpufreq_cpu_put(policy);
1200 EXPORT_SYMBOL_GPL(cpufreq_governor);
1203 int cpufreq_register_governor(struct cpufreq_governor *governor)
1205 struct cpufreq_governor *t;
1210 down(&cpufreq_governor_sem);
1212 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1213 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1214 up(&cpufreq_governor_sem);
1218 list_add(&governor->governor_list, &cpufreq_governor_list);
1220 up(&cpufreq_governor_sem);
1224 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1227 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1232 down(&cpufreq_governor_sem);
1233 list_del(&governor->governor_list);
1234 up(&cpufreq_governor_sem);
1237 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1241 /*********************************************************************
1242 * POLICY INTERFACE *
1243 *********************************************************************/
1246 * cpufreq_get_policy - get the current cpufreq_policy
1247 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1249 * Reads the current cpufreq policy.
1251 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1253 struct cpufreq_policy *cpu_policy;
1257 cpu_policy = cpufreq_cpu_get(cpu);
1261 down(&cpu_policy->lock);
1262 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1263 up(&cpu_policy->lock);
1265 cpufreq_cpu_put(cpu_policy);
1269 EXPORT_SYMBOL(cpufreq_get_policy);
1272 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1276 cpufreq_debug_disable_ratelimit();
1277 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1278 policy->min, policy->max);
1280 memcpy(&policy->cpuinfo,
1282 sizeof(struct cpufreq_cpuinfo));
1284 /* verify the cpu speed can be set within this limit */
1285 ret = cpufreq_driver->verify(policy);
1289 down_read(&cpufreq_notifier_rwsem);
1291 /* adjust if necessary - all reasons */
1292 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
1295 /* adjust if necessary - hardware incompatibility*/
1296 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
1299 /* verify the cpu speed can be set within this limit,
1300 which might be different to the first one */
1301 ret = cpufreq_driver->verify(policy);
1303 up_read(&cpufreq_notifier_rwsem);
1307 /* notification of the new policy */
1308 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
1311 up_read(&cpufreq_notifier_rwsem);
1313 data->min = policy->min;
1314 data->max = policy->max;
1316 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1318 if (cpufreq_driver->setpolicy) {
1319 data->policy = policy->policy;
1320 dprintk("setting range\n");
1321 ret = cpufreq_driver->setpolicy(policy);
1323 if (policy->governor != data->governor) {
1324 /* save old, working values */
1325 struct cpufreq_governor *old_gov = data->governor;
1327 dprintk("governor switch\n");
1329 /* end old governor */
1331 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1333 /* start new governor */
1334 data->governor = policy->governor;
1335 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1336 /* new governor failed, so re-start old one */
1337 dprintk("starting governor %s failed\n", data->governor->name);
1339 data->governor = old_gov;
1340 __cpufreq_governor(data, CPUFREQ_GOV_START);
1345 /* might be a policy change, too, so fall through */
1347 dprintk("governor: change or update limits\n");
1348 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1352 cpufreq_debug_enable_ratelimit();
1357 * cpufreq_set_policy - set a new CPUFreq policy
1358 * @policy: policy to be set.
1360 * Sets a new CPU frequency and voltage scaling policy.
1362 int cpufreq_set_policy(struct cpufreq_policy *policy)
1365 struct cpufreq_policy *data;
1370 data = cpufreq_cpu_get(policy->cpu);
1377 ret = __cpufreq_set_policy(data, policy);
1378 data->user_policy.min = data->min;
1379 data->user_policy.max = data->max;
1380 data->user_policy.policy = data->policy;
1381 data->user_policy.governor = data->governor;
1384 cpufreq_cpu_put(data);
1388 EXPORT_SYMBOL(cpufreq_set_policy);
1392 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1393 * @cpu: CPU which shall be re-evaluated
1395 * Usefull for policy notifiers which have different necessities
1396 * at different times.
1398 int cpufreq_update_policy(unsigned int cpu)
1400 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1401 struct cpufreq_policy policy;
1409 dprintk("updating policy for CPU %u\n", cpu);
1412 sizeof(struct cpufreq_policy));
1413 policy.min = data->user_policy.min;
1414 policy.max = data->user_policy.max;
1415 policy.policy = data->user_policy.policy;
1416 policy.governor = data->user_policy.governor;
1418 ret = __cpufreq_set_policy(data, &policy);
1422 cpufreq_cpu_put(data);
1425 EXPORT_SYMBOL(cpufreq_update_policy);
1427 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1428 unsigned long action, void *hcpu)
1430 unsigned int cpu = (unsigned long)hcpu;
1431 struct cpufreq_policy *policy;
1432 struct sys_device *sys_dev;
1434 sys_dev = get_cpu_sysdev(cpu);
1439 cpufreq_add_dev(sys_dev);
1441 case CPU_DOWN_PREPARE:
1443 * We attempt to put this cpu in lowest frequency
1444 * possible before going down. This will permit
1445 * hardware-managed P-State to switch other related
1446 * threads to min or higher speeds if possible.
1448 policy = cpufreq_cpu_data[cpu];
1450 cpufreq_driver_target(policy, policy->min,
1451 CPUFREQ_RELATION_H);
1455 cpufreq_remove_dev(sys_dev);
1462 static struct notifier_block cpufreq_cpu_notifier =
1464 .notifier_call = cpufreq_cpu_callback,
1467 /*********************************************************************
1468 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1469 *********************************************************************/
1472 * cpufreq_register_driver - register a CPU Frequency driver
1473 * @driver_data: A struct cpufreq_driver containing the values#
1474 * submitted by the CPU Frequency driver.
1476 * Registers a CPU Frequency driver to this core code. This code
1477 * returns zero on success, -EBUSY when another driver got here first
1478 * (and isn't unregistered in the meantime).
1481 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1483 unsigned long flags;
1486 if (!driver_data || !driver_data->verify || !driver_data->init ||
1487 ((!driver_data->setpolicy) && (!driver_data->target)))
1490 dprintk("trying to register driver %s\n", driver_data->name);
1492 if (driver_data->setpolicy)
1493 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1495 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1496 if (cpufreq_driver) {
1497 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1500 cpufreq_driver = driver_data;
1501 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1503 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1505 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1509 /* check for at least one working CPU */
1510 for (i=0; i<NR_CPUS; i++)
1511 if (cpufreq_cpu_data[i])
1514 /* if all ->init() calls failed, unregister */
1516 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1517 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1519 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1520 cpufreq_driver = NULL;
1521 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1526 register_cpu_notifier(&cpufreq_cpu_notifier);
1527 dprintk("driver %s up and running\n", driver_data->name);
1528 cpufreq_debug_enable_ratelimit();
1533 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1537 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1539 * Unregister the current CPUFreq driver. Only call this if you have
1540 * the right to do so, i.e. if you have succeeded in initialising before!
1541 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1542 * currently not initialised.
1544 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1546 unsigned long flags;
1548 cpufreq_debug_disable_ratelimit();
1550 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1551 cpufreq_debug_enable_ratelimit();
1555 dprintk("unregistering driver %s\n", driver->name);
1557 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1558 unregister_cpu_notifier(&cpufreq_cpu_notifier);
1560 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1561 cpufreq_driver = NULL;
1562 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1566 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);