2 * linux/drivers/cpufreq/cpufreq.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
8 * Added handling for CPU hotplug
9 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/notifier.h>
22 #include <linux/cpufreq.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/device.h>
27 #include <linux/slab.h>
28 #include <linux/cpu.h>
29 #include <linux/completion.h>
30 #include <linux/mutex.h>
32 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
35 * The "cpufreq driver" - the arch- or hardware-dependend low
36 * level driver of CPUFreq support, and its spinlock. This lock
37 * also protects the cpufreq_cpu_data array.
39 static struct cpufreq_driver *cpufreq_driver;
40 static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
41 static DEFINE_SPINLOCK(cpufreq_driver_lock);
43 /* internal prototypes */
44 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
45 static void handle_update(void *data);
48 * Two notifier lists: the "policy" list is involved in the
49 * validation process for a new CPU frequency policy; the
50 * "transition" list for kernel code that needs to handle
51 * changes to devices when the CPU clock speed changes.
52 * The mutex locks both lists.
54 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
55 static BLOCKING_NOTIFIER_HEAD(cpufreq_transition_notifier_list);
58 static LIST_HEAD(cpufreq_governor_list);
59 static DEFINE_MUTEX (cpufreq_governor_mutex);
61 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
63 struct cpufreq_policy *data;
69 /* get the cpufreq driver */
70 spin_lock_irqsave(&cpufreq_driver_lock, flags);
75 if (!try_module_get(cpufreq_driver->owner))
80 data = cpufreq_cpu_data[cpu];
83 goto err_out_put_module;
85 if (!kobject_get(&data->kobj))
86 goto err_out_put_module;
88 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
92 module_put(cpufreq_driver->owner);
94 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
98 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
101 void cpufreq_cpu_put(struct cpufreq_policy *data)
103 kobject_put(&data->kobj);
104 module_put(cpufreq_driver->owner);
106 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
109 /*********************************************************************
110 * UNIFIED DEBUG HELPERS *
111 *********************************************************************/
112 #ifdef CONFIG_CPU_FREQ_DEBUG
114 /* what part(s) of the CPUfreq subsystem are debugged? */
115 static unsigned int debug;
117 /* is the debug output ratelimit'ed using printk_ratelimit? User can
118 * set or modify this value.
120 static unsigned int debug_ratelimit = 1;
122 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
123 * loading of a cpufreq driver, temporarily disabled when a new policy
124 * is set, and disabled upon cpufreq driver removal
126 static unsigned int disable_ratelimit = 1;
127 static DEFINE_SPINLOCK(disable_ratelimit_lock);
129 static void cpufreq_debug_enable_ratelimit(void)
133 spin_lock_irqsave(&disable_ratelimit_lock, flags);
134 if (disable_ratelimit)
136 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
139 static void cpufreq_debug_disable_ratelimit(void)
143 spin_lock_irqsave(&disable_ratelimit_lock, flags);
145 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
148 void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
157 spin_lock_irqsave(&disable_ratelimit_lock, flags);
158 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
159 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
162 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
164 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
167 len += vsnprintf(&s[len], (256 - len), fmt, args);
175 EXPORT_SYMBOL(cpufreq_debug_printk);
178 module_param(debug, uint, 0644);
179 MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
181 module_param(debug_ratelimit, uint, 0644);
182 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
184 #else /* !CONFIG_CPU_FREQ_DEBUG */
186 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
187 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
189 #endif /* CONFIG_CPU_FREQ_DEBUG */
192 /*********************************************************************
193 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
194 *********************************************************************/
197 * adjust_jiffies - adjust the system "loops_per_jiffy"
199 * This function alters the system "loops_per_jiffy" for the clock
200 * speed change. Note that loops_per_jiffy cannot be updated on SMP
201 * systems as each CPU might be scaled differently. So, use the arch
202 * per-CPU loops_per_jiffy value wherever possible.
205 static unsigned long l_p_j_ref;
206 static unsigned int l_p_j_ref_freq;
208 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
210 if (ci->flags & CPUFREQ_CONST_LOOPS)
213 if (!l_p_j_ref_freq) {
214 l_p_j_ref = loops_per_jiffy;
215 l_p_j_ref_freq = ci->old;
216 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
218 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
219 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
220 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
221 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
222 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
226 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
231 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
232 * on frequency transition.
234 * This function calls the transition notifiers and the "adjust_jiffies"
235 * function. It is called twice on all CPU frequency changes that have
238 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
240 struct cpufreq_policy *policy;
242 BUG_ON(irqs_disabled());
244 freqs->flags = cpufreq_driver->flags;
245 dprintk("notification %u of frequency transition to %u kHz\n",
248 policy = cpufreq_cpu_data[freqs->cpu];
251 case CPUFREQ_PRECHANGE:
252 /* detect if the driver reported a value as "old frequency"
253 * which is not equal to what the cpufreq core thinks is
256 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
257 if ((policy) && (policy->cpu == freqs->cpu) &&
258 (policy->cur) && (policy->cur != freqs->old)) {
259 dprintk("Warning: CPU frequency is"
260 " %u, cpufreq assumed %u kHz.\n",
261 freqs->old, policy->cur);
262 freqs->old = policy->cur;
265 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
266 CPUFREQ_PRECHANGE, freqs);
267 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
270 case CPUFREQ_POSTCHANGE:
271 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
272 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
273 CPUFREQ_POSTCHANGE, freqs);
274 if (likely(policy) && likely(policy->cpu == freqs->cpu))
275 policy->cur = freqs->new;
279 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
283 /*********************************************************************
285 *********************************************************************/
287 static struct cpufreq_governor *__find_governor(const char *str_governor)
289 struct cpufreq_governor *t;
291 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
292 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN))
299 * cpufreq_parse_governor - parse a governor string
301 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
302 struct cpufreq_governor **governor)
309 if (cpufreq_driver->setpolicy) {
310 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
311 *policy = CPUFREQ_POLICY_PERFORMANCE;
313 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
314 *policy = CPUFREQ_POLICY_POWERSAVE;
317 } else if (cpufreq_driver->target) {
318 struct cpufreq_governor *t;
320 mutex_lock(&cpufreq_governor_mutex);
322 t = __find_governor(str_governor);
329 mutex_unlock(&cpufreq_governor_mutex);
336 /* drivers/base/cpu.c */
337 extern struct sysdev_class cpu_sysdev_class;
341 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
343 * Write out information from cpufreq_driver->policy[cpu]; object must be
347 #define show_one(file_name, object) \
348 static ssize_t show_##file_name \
349 (struct cpufreq_policy * policy, char *buf) \
351 return sprintf (buf, "%u\n", policy->object); \
354 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
355 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
356 show_one(scaling_min_freq, min);
357 show_one(scaling_max_freq, max);
358 show_one(scaling_cur_freq, cur);
360 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
363 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
365 #define store_one(file_name, object) \
366 static ssize_t store_##file_name \
367 (struct cpufreq_policy * policy, const char *buf, size_t count) \
369 unsigned int ret = -EINVAL; \
370 struct cpufreq_policy new_policy; \
372 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
376 ret = sscanf (buf, "%u", &new_policy.object); \
380 lock_cpu_hotplug(); \
381 mutex_lock(&policy->lock); \
382 ret = __cpufreq_set_policy(policy, &new_policy); \
383 policy->user_policy.object = policy->object; \
384 mutex_unlock(&policy->lock); \
385 unlock_cpu_hotplug(); \
387 return ret ? ret : count; \
390 store_one(scaling_min_freq,min);
391 store_one(scaling_max_freq,max);
394 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
396 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
398 unsigned int cur_freq = cpufreq_get(policy->cpu);
400 return sprintf(buf, "<unknown>");
401 return sprintf(buf, "%u\n", cur_freq);
406 * show_scaling_governor - show the current policy for the specified CPU
408 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
410 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
411 return sprintf(buf, "powersave\n");
412 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
413 return sprintf(buf, "performance\n");
414 else if (policy->governor)
415 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
421 * store_scaling_governor - store policy for the specified CPU
423 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
424 const char *buf, size_t count)
426 unsigned int ret = -EINVAL;
427 char str_governor[16];
428 struct cpufreq_policy new_policy;
430 ret = cpufreq_get_policy(&new_policy, policy->cpu);
434 ret = sscanf (buf, "%15s", str_governor);
438 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
443 /* Do not use cpufreq_set_policy here or the user_policy.max
444 will be wrongly overridden */
445 mutex_lock(&policy->lock);
446 ret = __cpufreq_set_policy(policy, &new_policy);
448 policy->user_policy.policy = policy->policy;
449 policy->user_policy.governor = policy->governor;
450 mutex_unlock(&policy->lock);
452 unlock_cpu_hotplug();
454 return ret ? ret : count;
458 * show_scaling_driver - show the cpufreq driver currently loaded
460 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
462 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
466 * show_scaling_available_governors - show the available CPUfreq governors
468 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
472 struct cpufreq_governor *t;
474 if (!cpufreq_driver->target) {
475 i += sprintf(buf, "performance powersave");
479 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
480 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
482 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
485 i += sprintf(&buf[i], "\n");
489 * show_affected_cpus - show the CPUs affected by each transition
491 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
496 for_each_cpu_mask(cpu, policy->cpus) {
498 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
499 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
500 if (i >= (PAGE_SIZE - 5))
503 i += sprintf(&buf[i], "\n");
508 #define define_one_ro(_name) \
509 static struct freq_attr _name = \
510 __ATTR(_name, 0444, show_##_name, NULL)
512 #define define_one_ro0400(_name) \
513 static struct freq_attr _name = \
514 __ATTR(_name, 0400, show_##_name, NULL)
516 #define define_one_rw(_name) \
517 static struct freq_attr _name = \
518 __ATTR(_name, 0644, show_##_name, store_##_name)
520 define_one_ro0400(cpuinfo_cur_freq);
521 define_one_ro(cpuinfo_min_freq);
522 define_one_ro(cpuinfo_max_freq);
523 define_one_ro(scaling_available_governors);
524 define_one_ro(scaling_driver);
525 define_one_ro(scaling_cur_freq);
526 define_one_ro(affected_cpus);
527 define_one_rw(scaling_min_freq);
528 define_one_rw(scaling_max_freq);
529 define_one_rw(scaling_governor);
531 static struct attribute * default_attrs[] = {
532 &cpuinfo_min_freq.attr,
533 &cpuinfo_max_freq.attr,
534 &scaling_min_freq.attr,
535 &scaling_max_freq.attr,
537 &scaling_governor.attr,
538 &scaling_driver.attr,
539 &scaling_available_governors.attr,
543 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
544 #define to_attr(a) container_of(a,struct freq_attr,attr)
546 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
548 struct cpufreq_policy * policy = to_policy(kobj);
549 struct freq_attr * fattr = to_attr(attr);
551 policy = cpufreq_cpu_get(policy->cpu);
554 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
555 cpufreq_cpu_put(policy);
559 static ssize_t store(struct kobject * kobj, struct attribute * attr,
560 const char * buf, size_t count)
562 struct cpufreq_policy * policy = to_policy(kobj);
563 struct freq_attr * fattr = to_attr(attr);
565 policy = cpufreq_cpu_get(policy->cpu);
568 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
569 cpufreq_cpu_put(policy);
573 static void cpufreq_sysfs_release(struct kobject * kobj)
575 struct cpufreq_policy * policy = to_policy(kobj);
576 dprintk("last reference is dropped\n");
577 complete(&policy->kobj_unregister);
580 static struct sysfs_ops sysfs_ops = {
585 static struct kobj_type ktype_cpufreq = {
586 .sysfs_ops = &sysfs_ops,
587 .default_attrs = default_attrs,
588 .release = cpufreq_sysfs_release,
593 * cpufreq_add_dev - add a CPU device
595 * Adds the cpufreq interface for a CPU device.
597 static int cpufreq_add_dev (struct sys_device * sys_dev)
599 unsigned int cpu = sys_dev->id;
601 struct cpufreq_policy new_policy;
602 struct cpufreq_policy *policy;
603 struct freq_attr **drv_attr;
604 struct sys_device *cpu_sys_dev;
608 struct cpufreq_policy *managed_policy;
611 if (cpu_is_offline(cpu))
614 cpufreq_debug_disable_ratelimit();
615 dprintk("adding CPU %u\n", cpu);
618 /* check whether a different CPU already registered this
619 * CPU because it is in the same boat. */
620 policy = cpufreq_cpu_get(cpu);
621 if (unlikely(policy)) {
622 cpufreq_cpu_put(policy);
623 cpufreq_debug_enable_ratelimit();
628 if (!try_module_get(cpufreq_driver->owner)) {
633 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
640 policy->cpus = cpumask_of_cpu(cpu);
642 mutex_init(&policy->lock);
643 mutex_lock(&policy->lock);
644 init_completion(&policy->kobj_unregister);
645 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
647 /* call driver. From then on the cpufreq must be able
648 * to accept all calls to ->verify and ->setpolicy for this CPU
650 ret = cpufreq_driver->init(policy);
652 dprintk("initialization failed\n");
653 mutex_unlock(&policy->lock);
658 for_each_cpu_mask(j, policy->cpus) {
662 /* check for existing affected CPUs. They may not be aware
663 * of it due to CPU Hotplug.
665 managed_policy = cpufreq_cpu_get(j);
666 if (unlikely(managed_policy)) {
667 spin_lock_irqsave(&cpufreq_driver_lock, flags);
668 managed_policy->cpus = policy->cpus;
669 cpufreq_cpu_data[cpu] = managed_policy;
670 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
672 dprintk("CPU already managed, adding link\n");
673 sysfs_create_link(&sys_dev->kobj,
674 &managed_policy->kobj, "cpufreq");
676 cpufreq_debug_enable_ratelimit();
677 mutex_unlock(&policy->lock);
679 goto err_out_driver_exit; /* call driver->exit() */
683 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
685 /* prepare interface data */
686 policy->kobj.parent = &sys_dev->kobj;
687 policy->kobj.ktype = &ktype_cpufreq;
688 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
690 ret = kobject_register(&policy->kobj);
692 mutex_unlock(&policy->lock);
693 goto err_out_driver_exit;
695 /* set up files for this cpu device */
696 drv_attr = cpufreq_driver->attr;
697 while ((drv_attr) && (*drv_attr)) {
698 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
701 if (cpufreq_driver->get)
702 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
703 if (cpufreq_driver->target)
704 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
706 spin_lock_irqsave(&cpufreq_driver_lock, flags);
707 for_each_cpu_mask(j, policy->cpus)
708 cpufreq_cpu_data[j] = policy;
709 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
711 /* symlink affected CPUs */
712 for_each_cpu_mask(j, policy->cpus) {
718 dprintk("CPU %u already managed, adding link\n", j);
719 cpufreq_cpu_get(cpu);
720 cpu_sys_dev = get_cpu_sysdev(j);
721 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
725 policy->governor = NULL; /* to assure that the starting sequence is
726 * run in cpufreq_set_policy */
727 mutex_unlock(&policy->lock);
729 /* set default policy */
730 ret = cpufreq_set_policy(&new_policy);
732 dprintk("setting policy failed\n");
733 goto err_out_unregister;
736 module_put(cpufreq_driver->owner);
737 dprintk("initialization complete\n");
738 cpufreq_debug_enable_ratelimit();
744 spin_lock_irqsave(&cpufreq_driver_lock, flags);
745 for_each_cpu_mask(j, policy->cpus)
746 cpufreq_cpu_data[j] = NULL;
747 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
749 kobject_unregister(&policy->kobj);
750 wait_for_completion(&policy->kobj_unregister);
753 if (cpufreq_driver->exit)
754 cpufreq_driver->exit(policy);
760 module_put(cpufreq_driver->owner);
762 cpufreq_debug_enable_ratelimit();
768 * cpufreq_remove_dev - remove a CPU device
770 * Removes the cpufreq interface for a CPU device.
772 static int cpufreq_remove_dev (struct sys_device * sys_dev)
774 unsigned int cpu = sys_dev->id;
776 struct cpufreq_policy *data;
778 struct sys_device *cpu_sys_dev;
782 cpufreq_debug_disable_ratelimit();
783 dprintk("unregistering CPU %u\n", cpu);
785 spin_lock_irqsave(&cpufreq_driver_lock, flags);
786 data = cpufreq_cpu_data[cpu];
789 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
790 cpufreq_debug_enable_ratelimit();
793 cpufreq_cpu_data[cpu] = NULL;
797 /* if this isn't the CPU which is the parent of the kobj, we
798 * only need to unlink, put and exit
800 if (unlikely(cpu != data->cpu)) {
801 dprintk("removing link\n");
802 cpu_clear(cpu, data->cpus);
803 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
804 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
805 cpufreq_cpu_put(data);
806 cpufreq_debug_enable_ratelimit();
812 if (!kobject_get(&data->kobj)) {
813 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
814 cpufreq_debug_enable_ratelimit();
819 /* if we have other CPUs still registered, we need to unlink them,
820 * or else wait_for_completion below will lock up. Clean the
821 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
824 if (unlikely(cpus_weight(data->cpus) > 1)) {
825 for_each_cpu_mask(j, data->cpus) {
828 cpufreq_cpu_data[j] = NULL;
832 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
834 if (unlikely(cpus_weight(data->cpus) > 1)) {
835 for_each_cpu_mask(j, data->cpus) {
838 dprintk("removing link for cpu %u\n", j);
839 cpu_sys_dev = get_cpu_sysdev(j);
840 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
841 cpufreq_cpu_put(data);
845 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
848 mutex_lock(&data->lock);
849 if (cpufreq_driver->target)
850 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
851 mutex_unlock(&data->lock);
853 kobject_unregister(&data->kobj);
855 kobject_put(&data->kobj);
857 /* we need to make sure that the underlying kobj is actually
858 * not referenced anymore by anybody before we proceed with
861 dprintk("waiting for dropping of refcount\n");
862 wait_for_completion(&data->kobj_unregister);
863 dprintk("wait complete\n");
865 if (cpufreq_driver->exit)
866 cpufreq_driver->exit(data);
870 cpufreq_debug_enable_ratelimit();
875 static void handle_update(void *data)
877 unsigned int cpu = (unsigned int)(long)data;
878 dprintk("handle_update for cpu %u called\n", cpu);
879 cpufreq_update_policy(cpu);
883 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
885 * @old_freq: CPU frequency the kernel thinks the CPU runs at
886 * @new_freq: CPU frequency the CPU actually runs at
888 * We adjust to current frequency first, and need to clean up later. So either call
889 * to cpufreq_update_policy() or schedule handle_update()).
891 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
893 struct cpufreq_freqs freqs;
895 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
896 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
899 freqs.old = old_freq;
900 freqs.new = new_freq;
901 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
902 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
907 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
910 * This is the last known freq, without actually getting it from the driver.
911 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
913 unsigned int cpufreq_quick_get(unsigned int cpu)
915 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
916 unsigned int ret = 0;
919 mutex_lock(&policy->lock);
921 mutex_unlock(&policy->lock);
922 cpufreq_cpu_put(policy);
927 EXPORT_SYMBOL(cpufreq_quick_get);
931 * cpufreq_get - get the current CPU frequency (in kHz)
934 * Get the CPU current (static) CPU frequency
936 unsigned int cpufreq_get(unsigned int cpu)
938 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
939 unsigned int ret = 0;
944 if (!cpufreq_driver->get)
947 mutex_lock(&policy->lock);
949 ret = cpufreq_driver->get(cpu);
951 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
952 /* verify no discrepancy between actual and saved value exists */
953 if (unlikely(ret != policy->cur)) {
954 cpufreq_out_of_sync(cpu, policy->cur, ret);
955 schedule_work(&policy->update);
959 mutex_unlock(&policy->lock);
962 cpufreq_cpu_put(policy);
966 EXPORT_SYMBOL(cpufreq_get);
970 * cpufreq_suspend - let the low level driver prepare for suspend
973 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
975 int cpu = sysdev->id;
976 unsigned int ret = 0;
977 unsigned int cur_freq = 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->suspend) {
1001 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
1003 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1004 "step on CPU %u\n", cpu_policy->cpu);
1005 cpufreq_cpu_put(cpu_policy);
1011 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
1014 if (cpufreq_driver->get)
1015 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1017 if (!cur_freq || !cpu_policy->cur) {
1018 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1019 "frequency is what timing core thinks it is.\n");
1023 if (unlikely(cur_freq != cpu_policy->cur)) {
1024 struct cpufreq_freqs freqs;
1026 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1027 dprintk("Warning: CPU frequency is %u, "
1028 "cpufreq assumed %u kHz.\n",
1029 cur_freq, cpu_policy->cur);
1032 freqs.old = cpu_policy->cur;
1033 freqs.new = cur_freq;
1035 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
1036 CPUFREQ_SUSPENDCHANGE, &freqs);
1037 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1039 cpu_policy->cur = cur_freq;
1043 cpufreq_cpu_put(cpu_policy);
1048 * cpufreq_resume - restore proper CPU frequency handling after resume
1050 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1051 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
1052 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1055 static int cpufreq_resume(struct sys_device * sysdev)
1057 int cpu = sysdev->id;
1058 unsigned int ret = 0;
1059 struct cpufreq_policy *cpu_policy;
1061 dprintk("resuming cpu %u\n", cpu);
1063 if (!cpu_online(cpu))
1066 /* we may be lax here as interrupts are off. Nonetheless
1067 * we need to grab the correct cpu policy, as to check
1068 * whether we really run on this CPU.
1071 cpu_policy = cpufreq_cpu_get(cpu);
1075 /* only handle each CPU group once */
1076 if (unlikely(cpu_policy->cpu != cpu)) {
1077 cpufreq_cpu_put(cpu_policy);
1081 if (cpufreq_driver->resume) {
1082 ret = cpufreq_driver->resume(cpu_policy);
1084 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1085 "step on CPU %u\n", cpu_policy->cpu);
1086 cpufreq_cpu_put(cpu_policy);
1091 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1092 unsigned int cur_freq = 0;
1094 if (cpufreq_driver->get)
1095 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1097 if (!cur_freq || !cpu_policy->cur) {
1098 printk(KERN_ERR "cpufreq: resume failed to assert "
1099 "current frequency is what timing core "
1104 if (unlikely(cur_freq != cpu_policy->cur)) {
1105 struct cpufreq_freqs freqs;
1107 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1108 dprintk("Warning: CPU frequency"
1109 "is %u, cpufreq assumed %u kHz.\n",
1110 cur_freq, cpu_policy->cur);
1113 freqs.old = cpu_policy->cur;
1114 freqs.new = cur_freq;
1116 blocking_notifier_call_chain(
1117 &cpufreq_transition_notifier_list,
1118 CPUFREQ_RESUMECHANGE, &freqs);
1119 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1121 cpu_policy->cur = cur_freq;
1126 schedule_work(&cpu_policy->update);
1127 cpufreq_cpu_put(cpu_policy);
1131 static struct sysdev_driver cpufreq_sysdev_driver = {
1132 .add = cpufreq_add_dev,
1133 .remove = cpufreq_remove_dev,
1134 .suspend = cpufreq_suspend,
1135 .resume = cpufreq_resume,
1139 /*********************************************************************
1140 * NOTIFIER LISTS INTERFACE *
1141 *********************************************************************/
1144 * cpufreq_register_notifier - register a driver with cpufreq
1145 * @nb: notifier function to register
1146 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1148 * Add a driver to one of two lists: either a list of drivers that
1149 * are notified about clock rate changes (once before and once after
1150 * the transition), or a list of drivers that are notified about
1151 * changes in cpufreq policy.
1153 * This function may sleep, and has the same return conditions as
1154 * blocking_notifier_chain_register.
1156 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1161 case CPUFREQ_TRANSITION_NOTIFIER:
1162 ret = blocking_notifier_chain_register(
1163 &cpufreq_transition_notifier_list, nb);
1165 case CPUFREQ_POLICY_NOTIFIER:
1166 ret = blocking_notifier_chain_register(
1167 &cpufreq_policy_notifier_list, nb);
1175 EXPORT_SYMBOL(cpufreq_register_notifier);
1179 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1180 * @nb: notifier block to be unregistered
1181 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1183 * Remove a driver from the CPU frequency notifier list.
1185 * This function may sleep, and has the same return conditions as
1186 * blocking_notifier_chain_unregister.
1188 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1193 case CPUFREQ_TRANSITION_NOTIFIER:
1194 ret = blocking_notifier_chain_unregister(
1195 &cpufreq_transition_notifier_list, nb);
1197 case CPUFREQ_POLICY_NOTIFIER:
1198 ret = blocking_notifier_chain_unregister(
1199 &cpufreq_policy_notifier_list, nb);
1207 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1210 /*********************************************************************
1212 *********************************************************************/
1215 /* Must be called with lock_cpu_hotplug held */
1216 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1217 unsigned int target_freq,
1218 unsigned int relation)
1220 int retval = -EINVAL;
1222 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1223 target_freq, relation);
1224 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1225 retval = cpufreq_driver->target(policy, target_freq, relation);
1229 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1231 int cpufreq_driver_target(struct cpufreq_policy *policy,
1232 unsigned int target_freq,
1233 unsigned int relation)
1237 policy = cpufreq_cpu_get(policy->cpu);
1242 mutex_lock(&policy->lock);
1244 ret = __cpufreq_driver_target(policy, target_freq, relation);
1246 mutex_unlock(&policy->lock);
1247 unlock_cpu_hotplug();
1249 cpufreq_cpu_put(policy);
1252 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1255 * Locking: Must be called with the lock_cpu_hotplug() lock held
1256 * when "event" is CPUFREQ_GOV_LIMITS
1259 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1263 if (!try_module_get(policy->governor->owner))
1266 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1267 ret = policy->governor->governor(policy, event);
1269 /* we keep one module reference alive for each CPU governed by this CPU */
1270 if ((event != CPUFREQ_GOV_START) || ret)
1271 module_put(policy->governor->owner);
1272 if ((event == CPUFREQ_GOV_STOP) && !ret)
1273 module_put(policy->governor->owner);
1279 int cpufreq_register_governor(struct cpufreq_governor *governor)
1286 mutex_lock(&cpufreq_governor_mutex);
1289 if (__find_governor(governor->name) == NULL) {
1291 list_add(&governor->governor_list, &cpufreq_governor_list);
1294 mutex_unlock(&cpufreq_governor_mutex);
1297 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1300 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1305 mutex_lock(&cpufreq_governor_mutex);
1306 list_del(&governor->governor_list);
1307 mutex_unlock(&cpufreq_governor_mutex);
1310 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1314 /*********************************************************************
1315 * POLICY INTERFACE *
1316 *********************************************************************/
1319 * cpufreq_get_policy - get the current cpufreq_policy
1320 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1322 * Reads the current cpufreq policy.
1324 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1326 struct cpufreq_policy *cpu_policy;
1330 cpu_policy = cpufreq_cpu_get(cpu);
1334 mutex_lock(&cpu_policy->lock);
1335 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1336 mutex_unlock(&cpu_policy->lock);
1338 cpufreq_cpu_put(cpu_policy);
1341 EXPORT_SYMBOL(cpufreq_get_policy);
1345 * Locking: Must be called with the lock_cpu_hotplug() lock held
1347 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1351 cpufreq_debug_disable_ratelimit();
1352 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1353 policy->min, policy->max);
1355 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
1357 if (policy->min > data->min && policy->min > policy->max) {
1362 /* verify the cpu speed can be set within this limit */
1363 ret = cpufreq_driver->verify(policy);
1367 /* adjust if necessary - all reasons */
1368 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1369 CPUFREQ_ADJUST, policy);
1371 /* adjust if necessary - hardware incompatibility*/
1372 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1373 CPUFREQ_INCOMPATIBLE, policy);
1375 /* verify the cpu speed can be set within this limit,
1376 which might be different to the first one */
1377 ret = cpufreq_driver->verify(policy);
1381 /* notification of the new policy */
1382 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1383 CPUFREQ_NOTIFY, policy);
1385 data->min = policy->min;
1386 data->max = policy->max;
1388 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1390 if (cpufreq_driver->setpolicy) {
1391 data->policy = policy->policy;
1392 dprintk("setting range\n");
1393 ret = cpufreq_driver->setpolicy(policy);
1395 if (policy->governor != data->governor) {
1396 /* save old, working values */
1397 struct cpufreq_governor *old_gov = data->governor;
1399 dprintk("governor switch\n");
1401 /* end old governor */
1403 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1405 /* start new governor */
1406 data->governor = policy->governor;
1407 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1408 /* new governor failed, so re-start old one */
1409 dprintk("starting governor %s failed\n", data->governor->name);
1411 data->governor = old_gov;
1412 __cpufreq_governor(data, CPUFREQ_GOV_START);
1417 /* might be a policy change, too, so fall through */
1419 dprintk("governor: change or update limits\n");
1420 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1424 cpufreq_debug_enable_ratelimit();
1429 * cpufreq_set_policy - set a new CPUFreq policy
1430 * @policy: policy to be set.
1432 * Sets a new CPU frequency and voltage scaling policy.
1434 int cpufreq_set_policy(struct cpufreq_policy *policy)
1437 struct cpufreq_policy *data;
1442 data = cpufreq_cpu_get(policy->cpu);
1449 mutex_lock(&data->lock);
1451 ret = __cpufreq_set_policy(data, policy);
1452 data->user_policy.min = data->min;
1453 data->user_policy.max = data->max;
1454 data->user_policy.policy = data->policy;
1455 data->user_policy.governor = data->governor;
1457 mutex_unlock(&data->lock);
1459 unlock_cpu_hotplug();
1460 cpufreq_cpu_put(data);
1464 EXPORT_SYMBOL(cpufreq_set_policy);
1468 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1469 * @cpu: CPU which shall be re-evaluated
1471 * Usefull for policy notifiers which have different necessities
1472 * at different times.
1474 int cpufreq_update_policy(unsigned int cpu)
1476 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1477 struct cpufreq_policy policy;
1484 mutex_lock(&data->lock);
1486 dprintk("updating policy for CPU %u\n", cpu);
1487 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1488 policy.min = data->user_policy.min;
1489 policy.max = data->user_policy.max;
1490 policy.policy = data->user_policy.policy;
1491 policy.governor = data->user_policy.governor;
1493 /* BIOS might change freq behind our back
1494 -> ask driver for current freq and notify governors about a change */
1495 if (cpufreq_driver->get) {
1496 policy.cur = cpufreq_driver->get(cpu);
1498 dprintk("Driver did not initialize current freq");
1499 data->cur = policy.cur;
1501 if (data->cur != policy.cur)
1502 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1506 ret = __cpufreq_set_policy(data, &policy);
1508 mutex_unlock(&data->lock);
1509 unlock_cpu_hotplug();
1510 cpufreq_cpu_put(data);
1513 EXPORT_SYMBOL(cpufreq_update_policy);
1515 #ifdef CONFIG_HOTPLUG_CPU
1516 static int cpufreq_cpu_callback(struct notifier_block *nfb,
1517 unsigned long action, void *hcpu)
1519 unsigned int cpu = (unsigned long)hcpu;
1520 struct cpufreq_policy *policy;
1521 struct sys_device *sys_dev;
1523 sys_dev = get_cpu_sysdev(cpu);
1528 cpufreq_add_dev(sys_dev);
1530 case CPU_DOWN_PREPARE:
1532 * We attempt to put this cpu in lowest frequency
1533 * possible before going down. This will permit
1534 * hardware-managed P-State to switch other related
1535 * threads to min or higher speeds if possible.
1537 policy = cpufreq_cpu_data[cpu];
1539 cpufreq_driver_target(policy, policy->min,
1540 CPUFREQ_RELATION_H);
1544 cpufreq_remove_dev(sys_dev);
1551 static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
1553 .notifier_call = cpufreq_cpu_callback,
1555 #endif /* CONFIG_HOTPLUG_CPU */
1557 /*********************************************************************
1558 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1559 *********************************************************************/
1562 * cpufreq_register_driver - register a CPU Frequency driver
1563 * @driver_data: A struct cpufreq_driver containing the values#
1564 * submitted by the CPU Frequency driver.
1566 * Registers a CPU Frequency driver to this core code. This code
1567 * returns zero on success, -EBUSY when another driver got here first
1568 * (and isn't unregistered in the meantime).
1571 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1573 unsigned long flags;
1576 if (!driver_data || !driver_data->verify || !driver_data->init ||
1577 ((!driver_data->setpolicy) && (!driver_data->target)))
1580 dprintk("trying to register driver %s\n", driver_data->name);
1582 if (driver_data->setpolicy)
1583 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1585 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1586 if (cpufreq_driver) {
1587 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1590 cpufreq_driver = driver_data;
1591 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1593 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1595 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1599 /* check for at least one working CPU */
1600 for (i=0; i<NR_CPUS; i++)
1601 if (cpufreq_cpu_data[i])
1604 /* if all ->init() calls failed, unregister */
1606 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1607 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1609 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1610 cpufreq_driver = NULL;
1611 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1616 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1617 dprintk("driver %s up and running\n", driver_data->name);
1618 cpufreq_debug_enable_ratelimit();
1623 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1627 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1629 * Unregister the current CPUFreq driver. Only call this if you have
1630 * the right to do so, i.e. if you have succeeded in initialising before!
1631 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1632 * currently not initialised.
1634 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1636 unsigned long flags;
1638 cpufreq_debug_disable_ratelimit();
1640 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1641 cpufreq_debug_enable_ratelimit();
1645 dprintk("unregistering driver %s\n", driver->name);
1647 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1648 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1650 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1651 cpufreq_driver = NULL;
1652 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1656 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);