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, \
36 * The "cpufreq driver" - the arch- or hardware-dependent low
37 * level driver of CPUFreq support, and its spinlock. This lock
38 * also protects the cpufreq_cpu_data array.
40 static struct cpufreq_driver *cpufreq_driver;
41 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
42 #ifdef CONFIG_HOTPLUG_CPU
43 /* This one keeps track of the previously set governor of a removed CPU */
44 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
46 static DEFINE_SPINLOCK(cpufreq_driver_lock);
49 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
50 * all cpufreq/hotplug/workqueue/etc related lock issues.
52 * The rules for this semaphore:
53 * - Any routine that wants to read from the policy structure will
54 * do a down_read on this semaphore.
55 * - Any routine that will write to the policy structure and/or may take away
56 * the policy altogether (eg. CPU hotplug), will hold this lock in write
57 * mode before doing so.
60 * - All holders of the lock should check to make sure that the CPU they
61 * are concerned with are online after they get the lock.
62 * - Governor routines that can be called in cpufreq hotplug path should not
63 * take this sem as top level hotplug notifier handler takes this.
64 * - Lock should not be held across
65 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
67 static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
68 static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
70 #define lock_policy_rwsem(mode, cpu) \
71 static int lock_policy_rwsem_##mode \
74 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
75 BUG_ON(policy_cpu == -1); \
76 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
77 if (unlikely(!cpu_online(cpu))) { \
78 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
85 lock_policy_rwsem(read, cpu);
87 lock_policy_rwsem(write, cpu);
89 static void unlock_policy_rwsem_read(int cpu)
91 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
92 BUG_ON(policy_cpu == -1);
93 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
96 static void unlock_policy_rwsem_write(int cpu)
98 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
99 BUG_ON(policy_cpu == -1);
100 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
104 /* internal prototypes */
105 static int __cpufreq_governor(struct cpufreq_policy *policy,
107 static unsigned int __cpufreq_get(unsigned int cpu);
108 static void handle_update(struct work_struct *work);
111 * Two notifier lists: the "policy" list is involved in the
112 * validation process for a new CPU frequency policy; the
113 * "transition" list for kernel code that needs to handle
114 * changes to devices when the CPU clock speed changes.
115 * The mutex locks both lists.
117 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
118 static struct srcu_notifier_head cpufreq_transition_notifier_list;
120 static bool init_cpufreq_transition_notifier_list_called;
121 static int __init init_cpufreq_transition_notifier_list(void)
123 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
124 init_cpufreq_transition_notifier_list_called = true;
127 pure_initcall(init_cpufreq_transition_notifier_list);
129 static LIST_HEAD(cpufreq_governor_list);
130 static DEFINE_MUTEX(cpufreq_governor_mutex);
132 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
134 struct cpufreq_policy *data;
137 if (cpu >= nr_cpu_ids)
140 /* get the cpufreq driver */
141 spin_lock_irqsave(&cpufreq_driver_lock, flags);
146 if (!try_module_get(cpufreq_driver->owner))
151 data = per_cpu(cpufreq_cpu_data, cpu);
154 goto err_out_put_module;
156 if (!kobject_get(&data->kobj))
157 goto err_out_put_module;
159 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
163 module_put(cpufreq_driver->owner);
165 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
169 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
172 void cpufreq_cpu_put(struct cpufreq_policy *data)
174 kobject_put(&data->kobj);
175 module_put(cpufreq_driver->owner);
177 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
180 /*********************************************************************
181 * UNIFIED DEBUG HELPERS *
182 *********************************************************************/
183 #ifdef CONFIG_CPU_FREQ_DEBUG
185 /* what part(s) of the CPUfreq subsystem are debugged? */
186 static unsigned int debug;
188 /* is the debug output ratelimit'ed using printk_ratelimit? User can
189 * set or modify this value.
191 static unsigned int debug_ratelimit = 1;
193 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
194 * loading of a cpufreq driver, temporarily disabled when a new policy
195 * is set, and disabled upon cpufreq driver removal
197 static unsigned int disable_ratelimit = 1;
198 static DEFINE_SPINLOCK(disable_ratelimit_lock);
200 static void cpufreq_debug_enable_ratelimit(void)
204 spin_lock_irqsave(&disable_ratelimit_lock, flags);
205 if (disable_ratelimit)
207 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
210 static void cpufreq_debug_disable_ratelimit(void)
214 spin_lock_irqsave(&disable_ratelimit_lock, flags);
216 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
219 void cpufreq_debug_printk(unsigned int type, const char *prefix,
220 const char *fmt, ...)
229 spin_lock_irqsave(&disable_ratelimit_lock, flags);
230 if (!disable_ratelimit && debug_ratelimit
231 && !printk_ratelimit()) {
232 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
235 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
237 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
240 len += vsnprintf(&s[len], (256 - len), fmt, args);
248 EXPORT_SYMBOL(cpufreq_debug_printk);
251 module_param(debug, uint, 0644);
252 MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
253 " 2 to debug drivers, and 4 to debug governors.");
255 module_param(debug_ratelimit, uint, 0644);
256 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
257 " set to 0 to disable ratelimiting.");
259 #else /* !CONFIG_CPU_FREQ_DEBUG */
261 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
262 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
264 #endif /* CONFIG_CPU_FREQ_DEBUG */
267 /*********************************************************************
268 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
269 *********************************************************************/
272 * adjust_jiffies - adjust the system "loops_per_jiffy"
274 * This function alters the system "loops_per_jiffy" for the clock
275 * speed change. Note that loops_per_jiffy cannot be updated on SMP
276 * systems as each CPU might be scaled differently. So, use the arch
277 * per-CPU loops_per_jiffy value wherever possible.
280 static unsigned long l_p_j_ref;
281 static unsigned int l_p_j_ref_freq;
283 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
285 if (ci->flags & CPUFREQ_CONST_LOOPS)
288 if (!l_p_j_ref_freq) {
289 l_p_j_ref = loops_per_jiffy;
290 l_p_j_ref_freq = ci->old;
291 dprintk("saving %lu as reference value for loops_per_jiffy; "
292 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
294 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
295 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
296 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
297 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
299 dprintk("scaling loops_per_jiffy to %lu "
300 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
304 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
312 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
313 * on frequency transition.
315 * This function calls the transition notifiers and the "adjust_jiffies"
316 * function. It is called twice on all CPU frequency changes that have
319 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
321 struct cpufreq_policy *policy;
323 BUG_ON(irqs_disabled());
325 freqs->flags = cpufreq_driver->flags;
326 dprintk("notification %u of frequency transition to %u kHz\n",
329 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
332 case CPUFREQ_PRECHANGE:
333 /* detect if the driver reported a value as "old frequency"
334 * which is not equal to what the cpufreq core thinks is
337 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
338 if ((policy) && (policy->cpu == freqs->cpu) &&
339 (policy->cur) && (policy->cur != freqs->old)) {
340 dprintk("Warning: CPU frequency is"
341 " %u, cpufreq assumed %u kHz.\n",
342 freqs->old, policy->cur);
343 freqs->old = policy->cur;
346 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
347 CPUFREQ_PRECHANGE, freqs);
348 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
351 case CPUFREQ_POSTCHANGE:
352 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
353 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
354 CPUFREQ_POSTCHANGE, freqs);
355 if (likely(policy) && likely(policy->cpu == freqs->cpu))
356 policy->cur = freqs->new;
360 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
364 /*********************************************************************
366 *********************************************************************/
368 static struct cpufreq_governor *__find_governor(const char *str_governor)
370 struct cpufreq_governor *t;
372 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
373 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
380 * cpufreq_parse_governor - parse a governor string
382 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
383 struct cpufreq_governor **governor)
390 if (cpufreq_driver->setpolicy) {
391 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
392 *policy = CPUFREQ_POLICY_PERFORMANCE;
394 } else if (!strnicmp(str_governor, "powersave",
396 *policy = CPUFREQ_POLICY_POWERSAVE;
399 } else if (cpufreq_driver->target) {
400 struct cpufreq_governor *t;
402 mutex_lock(&cpufreq_governor_mutex);
404 t = __find_governor(str_governor);
407 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
413 mutex_unlock(&cpufreq_governor_mutex);
414 ret = request_module("%s", name);
415 mutex_lock(&cpufreq_governor_mutex);
418 t = __find_governor(str_governor);
429 mutex_unlock(&cpufreq_governor_mutex);
437 * cpufreq_per_cpu_attr_read() / show_##file_name() -
438 * print out cpufreq information
440 * Write out information from cpufreq_driver->policy[cpu]; object must be
444 #define show_one(file_name, object) \
445 static ssize_t show_##file_name \
446 (struct cpufreq_policy *policy, char *buf) \
448 return sprintf(buf, "%u\n", policy->object); \
451 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
452 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
453 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
454 show_one(scaling_min_freq, min);
455 show_one(scaling_max_freq, max);
456 show_one(scaling_cur_freq, cur);
458 static int __cpufreq_set_policy(struct cpufreq_policy *data,
459 struct cpufreq_policy *policy);
462 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
464 #define store_one(file_name, object) \
465 static ssize_t store_##file_name \
466 (struct cpufreq_policy *policy, const char *buf, size_t count) \
468 unsigned int ret = -EINVAL; \
469 struct cpufreq_policy new_policy; \
471 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
475 ret = sscanf(buf, "%u", &new_policy.object); \
479 ret = __cpufreq_set_policy(policy, &new_policy); \
480 policy->user_policy.object = policy->object; \
482 return ret ? ret : count; \
485 store_one(scaling_min_freq, min);
486 store_one(scaling_max_freq, max);
489 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
491 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
494 unsigned int cur_freq = __cpufreq_get(policy->cpu);
496 return sprintf(buf, "<unknown>");
497 return sprintf(buf, "%u\n", cur_freq);
502 * show_scaling_governor - show the current policy for the specified CPU
504 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
506 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
507 return sprintf(buf, "powersave\n");
508 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
509 return sprintf(buf, "performance\n");
510 else if (policy->governor)
511 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
512 policy->governor->name);
518 * store_scaling_governor - store policy for the specified CPU
520 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
521 const char *buf, size_t count)
523 unsigned int ret = -EINVAL;
524 char str_governor[16];
525 struct cpufreq_policy new_policy;
527 ret = cpufreq_get_policy(&new_policy, policy->cpu);
531 ret = sscanf(buf, "%15s", str_governor);
535 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
536 &new_policy.governor))
539 /* Do not use cpufreq_set_policy here or the user_policy.max
540 will be wrongly overridden */
541 ret = __cpufreq_set_policy(policy, &new_policy);
543 policy->user_policy.policy = policy->policy;
544 policy->user_policy.governor = policy->governor;
553 * show_scaling_driver - show the cpufreq driver currently loaded
555 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
557 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
561 * show_scaling_available_governors - show the available CPUfreq governors
563 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
567 struct cpufreq_governor *t;
569 if (!cpufreq_driver->target) {
570 i += sprintf(buf, "performance powersave");
574 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
575 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
576 - (CPUFREQ_NAME_LEN + 2)))
578 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
581 i += sprintf(&buf[i], "\n");
585 static ssize_t show_cpus(const struct cpumask *mask, char *buf)
590 for_each_cpu(cpu, mask) {
592 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
593 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
594 if (i >= (PAGE_SIZE - 5))
597 i += sprintf(&buf[i], "\n");
602 * show_related_cpus - show the CPUs affected by each transition even if
603 * hw coordination is in use
605 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
607 if (cpumask_empty(policy->related_cpus))
608 return show_cpus(policy->cpus, buf);
609 return show_cpus(policy->related_cpus, buf);
613 * show_affected_cpus - show the CPUs affected by each transition
615 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
617 return show_cpus(policy->cpus, buf);
620 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
621 const char *buf, size_t count)
623 unsigned int freq = 0;
626 if (!policy->governor || !policy->governor->store_setspeed)
629 ret = sscanf(buf, "%u", &freq);
633 policy->governor->store_setspeed(policy, freq);
638 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
640 if (!policy->governor || !policy->governor->show_setspeed)
641 return sprintf(buf, "<unsupported>\n");
643 return policy->governor->show_setspeed(policy, buf);
647 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
649 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
653 if (cpufreq_driver->bios_limit) {
654 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
656 return sprintf(buf, "%u\n", limit);
658 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
661 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
662 cpufreq_freq_attr_ro(cpuinfo_min_freq);
663 cpufreq_freq_attr_ro(cpuinfo_max_freq);
664 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
665 cpufreq_freq_attr_ro(scaling_available_governors);
666 cpufreq_freq_attr_ro(scaling_driver);
667 cpufreq_freq_attr_ro(scaling_cur_freq);
668 cpufreq_freq_attr_ro(bios_limit);
669 cpufreq_freq_attr_ro(related_cpus);
670 cpufreq_freq_attr_ro(affected_cpus);
671 cpufreq_freq_attr_rw(scaling_min_freq);
672 cpufreq_freq_attr_rw(scaling_max_freq);
673 cpufreq_freq_attr_rw(scaling_governor);
674 cpufreq_freq_attr_rw(scaling_setspeed);
676 static struct attribute *default_attrs[] = {
677 &cpuinfo_min_freq.attr,
678 &cpuinfo_max_freq.attr,
679 &cpuinfo_transition_latency.attr,
680 &scaling_min_freq.attr,
681 &scaling_max_freq.attr,
684 &scaling_governor.attr,
685 &scaling_driver.attr,
686 &scaling_available_governors.attr,
687 &scaling_setspeed.attr,
691 struct kobject *cpufreq_global_kobject;
692 EXPORT_SYMBOL(cpufreq_global_kobject);
694 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
695 #define to_attr(a) container_of(a, struct freq_attr, attr)
697 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
699 struct cpufreq_policy *policy = to_policy(kobj);
700 struct freq_attr *fattr = to_attr(attr);
701 ssize_t ret = -EINVAL;
702 policy = cpufreq_cpu_get(policy->cpu);
706 if (lock_policy_rwsem_read(policy->cpu) < 0)
710 ret = fattr->show(policy, buf);
714 unlock_policy_rwsem_read(policy->cpu);
716 cpufreq_cpu_put(policy);
721 static ssize_t store(struct kobject *kobj, struct attribute *attr,
722 const char *buf, size_t count)
724 struct cpufreq_policy *policy = to_policy(kobj);
725 struct freq_attr *fattr = to_attr(attr);
726 ssize_t ret = -EINVAL;
727 policy = cpufreq_cpu_get(policy->cpu);
731 if (lock_policy_rwsem_write(policy->cpu) < 0)
735 ret = fattr->store(policy, buf, count);
739 unlock_policy_rwsem_write(policy->cpu);
741 cpufreq_cpu_put(policy);
746 static void cpufreq_sysfs_release(struct kobject *kobj)
748 struct cpufreq_policy *policy = to_policy(kobj);
749 dprintk("last reference is dropped\n");
750 complete(&policy->kobj_unregister);
753 static const struct sysfs_ops sysfs_ops = {
758 static struct kobj_type ktype_cpufreq = {
759 .sysfs_ops = &sysfs_ops,
760 .default_attrs = default_attrs,
761 .release = cpufreq_sysfs_release,
768 * Positive: When we have a managed CPU and the sysfs got symlinked
770 static int cpufreq_add_dev_policy(unsigned int cpu,
771 struct cpufreq_policy *policy,
772 struct sys_device *sys_dev)
778 #ifdef CONFIG_HOTPLUG_CPU
779 struct cpufreq_governor *gov;
781 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
783 policy->governor = gov;
784 dprintk("Restoring governor %s for cpu %d\n",
785 policy->governor->name, cpu);
789 for_each_cpu(j, policy->cpus) {
790 struct cpufreq_policy *managed_policy;
795 /* Check for existing affected CPUs.
796 * They may not be aware of it due to CPU Hotplug.
797 * cpufreq_cpu_put is called when the device is removed
798 * in __cpufreq_remove_dev()
800 managed_policy = cpufreq_cpu_get(j);
801 if (unlikely(managed_policy)) {
803 /* Set proper policy_cpu */
804 unlock_policy_rwsem_write(cpu);
805 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
807 if (lock_policy_rwsem_write(cpu) < 0) {
808 /* Should not go through policy unlock path */
809 if (cpufreq_driver->exit)
810 cpufreq_driver->exit(policy);
811 cpufreq_cpu_put(managed_policy);
815 spin_lock_irqsave(&cpufreq_driver_lock, flags);
816 cpumask_copy(managed_policy->cpus, policy->cpus);
817 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
818 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
820 dprintk("CPU already managed, adding link\n");
821 ret = sysfs_create_link(&sys_dev->kobj,
822 &managed_policy->kobj,
825 cpufreq_cpu_put(managed_policy);
827 * Success. We only needed to be added to the mask.
828 * Call driver->exit() because only the cpu parent of
829 * the kobj needed to call init().
831 if (cpufreq_driver->exit)
832 cpufreq_driver->exit(policy);
845 /* symlink affected CPUs */
846 static int cpufreq_add_dev_symlink(unsigned int cpu,
847 struct cpufreq_policy *policy)
852 for_each_cpu(j, policy->cpus) {
853 struct cpufreq_policy *managed_policy;
854 struct sys_device *cpu_sys_dev;
861 dprintk("CPU %u already managed, adding link\n", j);
862 managed_policy = cpufreq_cpu_get(cpu);
863 cpu_sys_dev = get_cpu_sysdev(j);
864 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
867 cpufreq_cpu_put(managed_policy);
874 static int cpufreq_add_dev_interface(unsigned int cpu,
875 struct cpufreq_policy *policy,
876 struct sys_device *sys_dev)
878 struct cpufreq_policy new_policy;
879 struct freq_attr **drv_attr;
884 /* prepare interface data */
885 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
886 &sys_dev->kobj, "cpufreq");
890 /* set up files for this cpu device */
891 drv_attr = cpufreq_driver->attr;
892 while ((drv_attr) && (*drv_attr)) {
893 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
895 goto err_out_kobj_put;
898 if (cpufreq_driver->get) {
899 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
901 goto err_out_kobj_put;
903 if (cpufreq_driver->target) {
904 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
906 goto err_out_kobj_put;
908 if (cpufreq_driver->bios_limit) {
909 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
911 goto err_out_kobj_put;
914 spin_lock_irqsave(&cpufreq_driver_lock, flags);
915 for_each_cpu(j, policy->cpus) {
918 per_cpu(cpufreq_cpu_data, j) = policy;
919 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
921 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
923 ret = cpufreq_add_dev_symlink(cpu, policy);
925 goto err_out_kobj_put;
927 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
928 /* assure that the starting sequence is run in __cpufreq_set_policy */
929 policy->governor = NULL;
931 /* set default policy */
932 ret = __cpufreq_set_policy(policy, &new_policy);
933 policy->user_policy.policy = policy->policy;
934 policy->user_policy.governor = policy->governor;
937 dprintk("setting policy failed\n");
938 if (cpufreq_driver->exit)
939 cpufreq_driver->exit(policy);
944 kobject_put(&policy->kobj);
945 wait_for_completion(&policy->kobj_unregister);
951 * cpufreq_add_dev - add a CPU device
953 * Adds the cpufreq interface for a CPU device.
955 * The Oracle says: try running cpufreq registration/unregistration concurrently
956 * with with cpu hotplugging and all hell will break loose. Tried to clean this
957 * mess up, but more thorough testing is needed. - Mathieu
959 static int cpufreq_add_dev(struct sys_device *sys_dev)
961 unsigned int cpu = sys_dev->id;
962 int ret = 0, found = 0;
963 struct cpufreq_policy *policy;
966 #ifdef CONFIG_HOTPLUG_CPU
970 if (cpu_is_offline(cpu))
973 cpufreq_debug_disable_ratelimit();
974 dprintk("adding CPU %u\n", cpu);
977 /* check whether a different CPU already registered this
978 * CPU because it is in the same boat. */
979 policy = cpufreq_cpu_get(cpu);
980 if (unlikely(policy)) {
981 cpufreq_cpu_put(policy);
982 cpufreq_debug_enable_ratelimit();
987 if (!try_module_get(cpufreq_driver->owner)) {
993 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
997 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
998 goto err_free_policy;
1000 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1001 goto err_free_cpumask;
1004 cpumask_copy(policy->cpus, cpumask_of(cpu));
1006 /* Initially set CPU itself as the policy_cpu */
1007 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
1008 ret = (lock_policy_rwsem_write(cpu) < 0);
1011 init_completion(&policy->kobj_unregister);
1012 INIT_WORK(&policy->update, handle_update);
1014 /* Set governor before ->init, so that driver could check it */
1015 #ifdef CONFIG_HOTPLUG_CPU
1016 for_each_online_cpu(sibling) {
1017 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
1018 if (cp && cp->governor &&
1019 (cpumask_test_cpu(cpu, cp->related_cpus))) {
1020 policy->governor = cp->governor;
1027 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1028 /* call driver. From then on the cpufreq must be able
1029 * to accept all calls to ->verify and ->setpolicy for this CPU
1031 ret = cpufreq_driver->init(policy);
1033 dprintk("initialization failed\n");
1034 goto err_unlock_policy;
1036 policy->user_policy.min = policy->min;
1037 policy->user_policy.max = policy->max;
1039 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1040 CPUFREQ_START, policy);
1042 ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
1045 /* This is a managed cpu, symlink created,
1048 goto err_unlock_policy;
1051 ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
1053 goto err_out_unregister;
1055 unlock_policy_rwsem_write(cpu);
1057 kobject_uevent(&policy->kobj, KOBJ_ADD);
1058 module_put(cpufreq_driver->owner);
1059 dprintk("initialization complete\n");
1060 cpufreq_debug_enable_ratelimit();
1066 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1067 for_each_cpu(j, policy->cpus)
1068 per_cpu(cpufreq_cpu_data, j) = NULL;
1069 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1071 kobject_put(&policy->kobj);
1072 wait_for_completion(&policy->kobj_unregister);
1075 unlock_policy_rwsem_write(cpu);
1076 free_cpumask_var(policy->related_cpus);
1078 free_cpumask_var(policy->cpus);
1082 module_put(cpufreq_driver->owner);
1084 cpufreq_debug_enable_ratelimit();
1090 * __cpufreq_remove_dev - remove a CPU device
1092 * Removes the cpufreq interface for a CPU device.
1093 * Caller should already have policy_rwsem in write mode for this CPU.
1094 * This routine frees the rwsem before returning.
1096 static int __cpufreq_remove_dev(struct sys_device *sys_dev)
1098 unsigned int cpu = sys_dev->id;
1099 unsigned long flags;
1100 struct cpufreq_policy *data;
1101 struct kobject *kobj;
1102 struct completion *cmp;
1104 struct sys_device *cpu_sys_dev;
1108 cpufreq_debug_disable_ratelimit();
1109 dprintk("unregistering CPU %u\n", cpu);
1111 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1112 data = per_cpu(cpufreq_cpu_data, cpu);
1115 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1116 cpufreq_debug_enable_ratelimit();
1117 unlock_policy_rwsem_write(cpu);
1120 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1124 /* if this isn't the CPU which is the parent of the kobj, we
1125 * only need to unlink, put and exit
1127 if (unlikely(cpu != data->cpu)) {
1128 dprintk("removing link\n");
1129 cpumask_clear_cpu(cpu, data->cpus);
1130 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1131 kobj = &sys_dev->kobj;
1132 cpufreq_cpu_put(data);
1133 cpufreq_debug_enable_ratelimit();
1134 unlock_policy_rwsem_write(cpu);
1135 sysfs_remove_link(kobj, "cpufreq");
1142 #ifdef CONFIG_HOTPLUG_CPU
1143 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1147 /* if we have other CPUs still registered, we need to unlink them,
1148 * or else wait_for_completion below will lock up. Clean the
1149 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1150 * the sysfs links afterwards.
1152 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1153 for_each_cpu(j, data->cpus) {
1156 per_cpu(cpufreq_cpu_data, j) = NULL;
1160 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1162 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1163 for_each_cpu(j, data->cpus) {
1166 dprintk("removing link for cpu %u\n", j);
1167 #ifdef CONFIG_HOTPLUG_CPU
1168 strncpy(per_cpu(cpufreq_cpu_governor, j),
1169 data->governor->name, CPUFREQ_NAME_LEN);
1171 cpu_sys_dev = get_cpu_sysdev(j);
1172 kobj = &cpu_sys_dev->kobj;
1173 unlock_policy_rwsem_write(cpu);
1174 sysfs_remove_link(kobj, "cpufreq");
1175 lock_policy_rwsem_write(cpu);
1176 cpufreq_cpu_put(data);
1180 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1183 if (cpufreq_driver->target)
1184 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1187 cmp = &data->kobj_unregister;
1188 unlock_policy_rwsem_write(cpu);
1191 /* we need to make sure that the underlying kobj is actually
1192 * not referenced anymore by anybody before we proceed with
1195 dprintk("waiting for dropping of refcount\n");
1196 wait_for_completion(cmp);
1197 dprintk("wait complete\n");
1199 lock_policy_rwsem_write(cpu);
1200 if (cpufreq_driver->exit)
1201 cpufreq_driver->exit(data);
1202 unlock_policy_rwsem_write(cpu);
1204 free_cpumask_var(data->related_cpus);
1205 free_cpumask_var(data->cpus);
1207 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1209 cpufreq_debug_enable_ratelimit();
1214 static int cpufreq_remove_dev(struct sys_device *sys_dev)
1216 unsigned int cpu = sys_dev->id;
1219 if (cpu_is_offline(cpu))
1222 if (unlikely(lock_policy_rwsem_write(cpu)))
1225 retval = __cpufreq_remove_dev(sys_dev);
1230 static void handle_update(struct work_struct *work)
1232 struct cpufreq_policy *policy =
1233 container_of(work, struct cpufreq_policy, update);
1234 unsigned int cpu = policy->cpu;
1235 dprintk("handle_update for cpu %u called\n", cpu);
1236 cpufreq_update_policy(cpu);
1240 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1242 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1243 * @new_freq: CPU frequency the CPU actually runs at
1245 * We adjust to current frequency first, and need to clean up later.
1246 * So either call to cpufreq_update_policy() or schedule handle_update()).
1248 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1249 unsigned int new_freq)
1251 struct cpufreq_freqs freqs;
1253 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
1254 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1257 freqs.old = old_freq;
1258 freqs.new = new_freq;
1259 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1260 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1265 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1268 * This is the last known freq, without actually getting it from the driver.
1269 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1271 unsigned int cpufreq_quick_get(unsigned int cpu)
1273 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1274 unsigned int ret_freq = 0;
1277 ret_freq = policy->cur;
1278 cpufreq_cpu_put(policy);
1283 EXPORT_SYMBOL(cpufreq_quick_get);
1286 static unsigned int __cpufreq_get(unsigned int cpu)
1288 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1289 unsigned int ret_freq = 0;
1291 if (!cpufreq_driver->get)
1294 ret_freq = cpufreq_driver->get(cpu);
1296 if (ret_freq && policy->cur &&
1297 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1298 /* verify no discrepancy between actual and
1299 saved value exists */
1300 if (unlikely(ret_freq != policy->cur)) {
1301 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1302 schedule_work(&policy->update);
1310 * cpufreq_get - get the current CPU frequency (in kHz)
1313 * Get the CPU current (static) CPU frequency
1315 unsigned int cpufreq_get(unsigned int cpu)
1317 unsigned int ret_freq = 0;
1318 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1323 if (unlikely(lock_policy_rwsem_read(cpu)))
1326 ret_freq = __cpufreq_get(cpu);
1328 unlock_policy_rwsem_read(cpu);
1331 cpufreq_cpu_put(policy);
1335 EXPORT_SYMBOL(cpufreq_get);
1339 * cpufreq_suspend - let the low level driver prepare for suspend
1342 static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg)
1346 int cpu = sysdev->id;
1347 struct cpufreq_policy *cpu_policy;
1349 dprintk("suspending cpu %u\n", cpu);
1351 if (!cpu_online(cpu))
1354 /* we may be lax here as interrupts are off. Nonetheless
1355 * we need to grab the correct cpu policy, as to check
1356 * whether we really run on this CPU.
1359 cpu_policy = cpufreq_cpu_get(cpu);
1363 /* only handle each CPU group once */
1364 if (unlikely(cpu_policy->cpu != cpu))
1367 if (cpufreq_driver->suspend) {
1368 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
1370 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1371 "step on CPU %u\n", cpu_policy->cpu);
1375 cpufreq_cpu_put(cpu_policy);
1380 * cpufreq_resume - restore proper CPU frequency handling after resume
1382 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1383 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1384 * restored. It will verify that the current freq is in sync with
1385 * what we believe it to be. This is a bit later than when it
1386 * should be, but nonethteless it's better than calling
1387 * cpufreq_driver->get() here which might re-enable interrupts...
1389 static int cpufreq_resume(struct sys_device *sysdev)
1393 int cpu = sysdev->id;
1394 struct cpufreq_policy *cpu_policy;
1396 dprintk("resuming cpu %u\n", cpu);
1398 if (!cpu_online(cpu))
1401 /* we may be lax here as interrupts are off. Nonetheless
1402 * we need to grab the correct cpu policy, as to check
1403 * whether we really run on this CPU.
1406 cpu_policy = cpufreq_cpu_get(cpu);
1410 /* only handle each CPU group once */
1411 if (unlikely(cpu_policy->cpu != cpu))
1414 if (cpufreq_driver->resume) {
1415 ret = cpufreq_driver->resume(cpu_policy);
1417 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1418 "step on CPU %u\n", cpu_policy->cpu);
1423 schedule_work(&cpu_policy->update);
1426 cpufreq_cpu_put(cpu_policy);
1430 static struct sysdev_driver cpufreq_sysdev_driver = {
1431 .add = cpufreq_add_dev,
1432 .remove = cpufreq_remove_dev,
1433 .suspend = cpufreq_suspend,
1434 .resume = cpufreq_resume,
1438 /*********************************************************************
1439 * NOTIFIER LISTS INTERFACE *
1440 *********************************************************************/
1443 * cpufreq_register_notifier - register a driver with cpufreq
1444 * @nb: notifier function to register
1445 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1447 * Add a driver to one of two lists: either a list of drivers that
1448 * are notified about clock rate changes (once before and once after
1449 * the transition), or a list of drivers that are notified about
1450 * changes in cpufreq policy.
1452 * This function may sleep, and has the same return conditions as
1453 * blocking_notifier_chain_register.
1455 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1459 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1462 case CPUFREQ_TRANSITION_NOTIFIER:
1463 ret = srcu_notifier_chain_register(
1464 &cpufreq_transition_notifier_list, nb);
1466 case CPUFREQ_POLICY_NOTIFIER:
1467 ret = blocking_notifier_chain_register(
1468 &cpufreq_policy_notifier_list, nb);
1476 EXPORT_SYMBOL(cpufreq_register_notifier);
1480 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1481 * @nb: notifier block to be unregistered
1482 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1484 * Remove a driver from the CPU frequency notifier list.
1486 * This function may sleep, and has the same return conditions as
1487 * blocking_notifier_chain_unregister.
1489 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1494 case CPUFREQ_TRANSITION_NOTIFIER:
1495 ret = srcu_notifier_chain_unregister(
1496 &cpufreq_transition_notifier_list, nb);
1498 case CPUFREQ_POLICY_NOTIFIER:
1499 ret = blocking_notifier_chain_unregister(
1500 &cpufreq_policy_notifier_list, nb);
1508 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1511 /*********************************************************************
1513 *********************************************************************/
1516 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1517 unsigned int target_freq,
1518 unsigned int relation)
1520 int retval = -EINVAL;
1522 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1523 target_freq, relation);
1524 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1525 retval = cpufreq_driver->target(policy, target_freq, relation);
1529 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1531 int cpufreq_driver_target(struct cpufreq_policy *policy,
1532 unsigned int target_freq,
1533 unsigned int relation)
1537 policy = cpufreq_cpu_get(policy->cpu);
1541 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1544 ret = __cpufreq_driver_target(policy, target_freq, relation);
1546 unlock_policy_rwsem_write(policy->cpu);
1549 cpufreq_cpu_put(policy);
1553 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1555 int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
1559 policy = cpufreq_cpu_get(policy->cpu);
1563 if (cpu_online(cpu) && cpufreq_driver->getavg)
1564 ret = cpufreq_driver->getavg(policy, cpu);
1566 cpufreq_cpu_put(policy);
1569 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
1572 * when "event" is CPUFREQ_GOV_LIMITS
1575 static int __cpufreq_governor(struct cpufreq_policy *policy,
1580 /* Only must be defined when default governor is known to have latency
1581 restrictions, like e.g. conservative or ondemand.
1582 That this is the case is already ensured in Kconfig
1584 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1585 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1587 struct cpufreq_governor *gov = NULL;
1590 if (policy->governor->max_transition_latency &&
1591 policy->cpuinfo.transition_latency >
1592 policy->governor->max_transition_latency) {
1596 printk(KERN_WARNING "%s governor failed, too long"
1597 " transition latency of HW, fallback"
1598 " to %s governor\n",
1599 policy->governor->name,
1601 policy->governor = gov;
1605 if (!try_module_get(policy->governor->owner))
1608 dprintk("__cpufreq_governor for CPU %u, event %u\n",
1609 policy->cpu, event);
1610 ret = policy->governor->governor(policy, event);
1612 /* we keep one module reference alive for
1613 each CPU governed by this CPU */
1614 if ((event != CPUFREQ_GOV_START) || ret)
1615 module_put(policy->governor->owner);
1616 if ((event == CPUFREQ_GOV_STOP) && !ret)
1617 module_put(policy->governor->owner);
1623 int cpufreq_register_governor(struct cpufreq_governor *governor)
1630 mutex_lock(&cpufreq_governor_mutex);
1633 if (__find_governor(governor->name) == NULL) {
1635 list_add(&governor->governor_list, &cpufreq_governor_list);
1638 mutex_unlock(&cpufreq_governor_mutex);
1641 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1644 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1646 #ifdef CONFIG_HOTPLUG_CPU
1653 #ifdef CONFIG_HOTPLUG_CPU
1654 for_each_present_cpu(cpu) {
1655 if (cpu_online(cpu))
1657 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1658 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1662 mutex_lock(&cpufreq_governor_mutex);
1663 list_del(&governor->governor_list);
1664 mutex_unlock(&cpufreq_governor_mutex);
1667 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1671 /*********************************************************************
1672 * POLICY INTERFACE *
1673 *********************************************************************/
1676 * cpufreq_get_policy - get the current cpufreq_policy
1677 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1680 * Reads the current cpufreq policy.
1682 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1684 struct cpufreq_policy *cpu_policy;
1688 cpu_policy = cpufreq_cpu_get(cpu);
1692 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1694 cpufreq_cpu_put(cpu_policy);
1697 EXPORT_SYMBOL(cpufreq_get_policy);
1701 * data : current policy.
1702 * policy : policy to be set.
1704 static int __cpufreq_set_policy(struct cpufreq_policy *data,
1705 struct cpufreq_policy *policy)
1709 cpufreq_debug_disable_ratelimit();
1710 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1711 policy->min, policy->max);
1713 memcpy(&policy->cpuinfo, &data->cpuinfo,
1714 sizeof(struct cpufreq_cpuinfo));
1716 if (policy->min > data->max || policy->max < data->min) {
1721 /* verify the cpu speed can be set within this limit */
1722 ret = cpufreq_driver->verify(policy);
1726 /* adjust if necessary - all reasons */
1727 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1728 CPUFREQ_ADJUST, policy);
1730 /* adjust if necessary - hardware incompatibility*/
1731 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1732 CPUFREQ_INCOMPATIBLE, policy);
1734 /* verify the cpu speed can be set within this limit,
1735 which might be different to the first one */
1736 ret = cpufreq_driver->verify(policy);
1740 /* notification of the new policy */
1741 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1742 CPUFREQ_NOTIFY, policy);
1744 data->min = policy->min;
1745 data->max = policy->max;
1747 dprintk("new min and max freqs are %u - %u kHz\n",
1748 data->min, data->max);
1750 if (cpufreq_driver->setpolicy) {
1751 data->policy = policy->policy;
1752 dprintk("setting range\n");
1753 ret = cpufreq_driver->setpolicy(policy);
1755 if (policy->governor != data->governor) {
1756 /* save old, working values */
1757 struct cpufreq_governor *old_gov = data->governor;
1759 dprintk("governor switch\n");
1761 /* end old governor */
1763 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1765 /* start new governor */
1766 data->governor = policy->governor;
1767 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1768 /* new governor failed, so re-start old one */
1769 dprintk("starting governor %s failed\n",
1770 data->governor->name);
1772 data->governor = old_gov;
1773 __cpufreq_governor(data,
1779 /* might be a policy change, too, so fall through */
1781 dprintk("governor: change or update limits\n");
1782 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1786 cpufreq_debug_enable_ratelimit();
1791 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1792 * @cpu: CPU which shall be re-evaluated
1794 * Usefull for policy notifiers which have different necessities
1795 * at different times.
1797 int cpufreq_update_policy(unsigned int cpu)
1799 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1800 struct cpufreq_policy policy;
1808 if (unlikely(lock_policy_rwsem_write(cpu))) {
1813 dprintk("updating policy for CPU %u\n", cpu);
1814 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1815 policy.min = data->user_policy.min;
1816 policy.max = data->user_policy.max;
1817 policy.policy = data->user_policy.policy;
1818 policy.governor = data->user_policy.governor;
1820 /* BIOS might change freq behind our back
1821 -> ask driver for current freq and notify governors about a change */
1822 if (cpufreq_driver->get) {
1823 policy.cur = cpufreq_driver->get(cpu);
1825 dprintk("Driver did not initialize current freq");
1826 data->cur = policy.cur;
1828 if (data->cur != policy.cur)
1829 cpufreq_out_of_sync(cpu, data->cur,
1834 ret = __cpufreq_set_policy(data, &policy);
1836 unlock_policy_rwsem_write(cpu);
1839 cpufreq_cpu_put(data);
1843 EXPORT_SYMBOL(cpufreq_update_policy);
1845 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1846 unsigned long action, void *hcpu)
1848 unsigned int cpu = (unsigned long)hcpu;
1849 struct sys_device *sys_dev;
1851 sys_dev = get_cpu_sysdev(cpu);
1855 case CPU_ONLINE_FROZEN:
1856 cpufreq_add_dev(sys_dev);
1858 case CPU_DOWN_PREPARE:
1859 case CPU_DOWN_PREPARE_FROZEN:
1860 if (unlikely(lock_policy_rwsem_write(cpu)))
1863 __cpufreq_remove_dev(sys_dev);
1865 case CPU_DOWN_FAILED:
1866 case CPU_DOWN_FAILED_FROZEN:
1867 cpufreq_add_dev(sys_dev);
1874 static struct notifier_block __refdata cpufreq_cpu_notifier =
1876 .notifier_call = cpufreq_cpu_callback,
1879 /*********************************************************************
1880 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1881 *********************************************************************/
1884 * cpufreq_register_driver - register a CPU Frequency driver
1885 * @driver_data: A struct cpufreq_driver containing the values#
1886 * submitted by the CPU Frequency driver.
1888 * Registers a CPU Frequency driver to this core code. This code
1889 * returns zero on success, -EBUSY when another driver got here first
1890 * (and isn't unregistered in the meantime).
1893 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1895 unsigned long flags;
1898 if (!driver_data || !driver_data->verify || !driver_data->init ||
1899 ((!driver_data->setpolicy) && (!driver_data->target)))
1902 dprintk("trying to register driver %s\n", driver_data->name);
1904 if (driver_data->setpolicy)
1905 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1907 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1908 if (cpufreq_driver) {
1909 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1912 cpufreq_driver = driver_data;
1913 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1915 ret = sysdev_driver_register(&cpu_sysdev_class,
1916 &cpufreq_sysdev_driver);
1918 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1922 /* check for at least one working CPU */
1923 for (i = 0; i < nr_cpu_ids; i++)
1924 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
1929 /* if all ->init() calls failed, unregister */
1931 dprintk("no CPU initialized for driver %s\n",
1933 sysdev_driver_unregister(&cpu_sysdev_class,
1934 &cpufreq_sysdev_driver);
1936 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1937 cpufreq_driver = NULL;
1938 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1943 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1944 dprintk("driver %s up and running\n", driver_data->name);
1945 cpufreq_debug_enable_ratelimit();
1950 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1954 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1956 * Unregister the current CPUFreq driver. Only call this if you have
1957 * the right to do so, i.e. if you have succeeded in initialising before!
1958 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1959 * currently not initialised.
1961 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1963 unsigned long flags;
1965 cpufreq_debug_disable_ratelimit();
1967 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1968 cpufreq_debug_enable_ratelimit();
1972 dprintk("unregistering driver %s\n", driver->name);
1974 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1975 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1977 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1978 cpufreq_driver = NULL;
1979 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1983 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
1985 static int __init cpufreq_core_init(void)
1989 for_each_possible_cpu(cpu) {
1990 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1991 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1994 cpufreq_global_kobject = kobject_create_and_add("cpufreq",
1995 &cpu_sysdev_class.kset.kobj);
1996 BUG_ON(!cpufreq_global_kobject);
2000 core_initcall(cpufreq_core_init);