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>
31 #include <linux/syscore_ops.h>
33 #include <trace/events/power.h>
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 int off __read_mostly;
130 int cpufreq_disabled(void)
134 void disable_cpufreq(void)
138 static LIST_HEAD(cpufreq_governor_list);
139 static DEFINE_MUTEX(cpufreq_governor_mutex);
141 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
143 struct cpufreq_policy *data;
146 if (cpu >= nr_cpu_ids)
149 /* get the cpufreq driver */
150 spin_lock_irqsave(&cpufreq_driver_lock, flags);
155 if (!try_module_get(cpufreq_driver->owner))
160 data = per_cpu(cpufreq_cpu_data, cpu);
163 goto err_out_put_module;
165 if (!kobject_get(&data->kobj))
166 goto err_out_put_module;
168 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
172 module_put(cpufreq_driver->owner);
174 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
178 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
181 void cpufreq_cpu_put(struct cpufreq_policy *data)
183 kobject_put(&data->kobj);
184 module_put(cpufreq_driver->owner);
186 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
189 /*********************************************************************
190 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
191 *********************************************************************/
194 * adjust_jiffies - adjust the system "loops_per_jiffy"
196 * This function alters the system "loops_per_jiffy" for the clock
197 * speed change. Note that loops_per_jiffy cannot be updated on SMP
198 * systems as each CPU might be scaled differently. So, use the arch
199 * per-CPU loops_per_jiffy value wherever possible.
202 static unsigned long l_p_j_ref;
203 static unsigned int l_p_j_ref_freq;
205 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
207 if (ci->flags & CPUFREQ_CONST_LOOPS)
210 if (!l_p_j_ref_freq) {
211 l_p_j_ref = loops_per_jiffy;
212 l_p_j_ref_freq = ci->old;
213 pr_debug("saving %lu as reference value for loops_per_jiffy; "
214 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
216 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
217 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
218 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
219 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
221 pr_debug("scaling loops_per_jiffy to %lu "
222 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
226 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
234 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
235 * on frequency transition.
237 * This function calls the transition notifiers and the "adjust_jiffies"
238 * function. It is called twice on all CPU frequency changes that have
241 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
243 struct cpufreq_policy *policy;
245 BUG_ON(irqs_disabled());
247 freqs->flags = cpufreq_driver->flags;
248 pr_debug("notification %u of frequency transition to %u kHz\n",
251 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
254 case CPUFREQ_PRECHANGE:
255 /* detect if the driver reported a value as "old frequency"
256 * which is not equal to what the cpufreq core thinks is
259 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
260 if ((policy) && (policy->cpu == freqs->cpu) &&
261 (policy->cur) && (policy->cur != freqs->old)) {
262 pr_debug("Warning: CPU frequency is"
263 " %u, cpufreq assumed %u kHz.\n",
264 freqs->old, policy->cur);
265 freqs->old = policy->cur;
268 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
269 CPUFREQ_PRECHANGE, freqs);
270 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
273 case CPUFREQ_POSTCHANGE:
274 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
275 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
276 (unsigned long)freqs->cpu);
277 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
278 trace_cpu_frequency(freqs->new, freqs->cpu);
279 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
280 CPUFREQ_POSTCHANGE, freqs);
281 if (likely(policy) && likely(policy->cpu == freqs->cpu))
282 policy->cur = freqs->new;
286 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
290 /*********************************************************************
292 *********************************************************************/
294 static struct cpufreq_governor *__find_governor(const char *str_governor)
296 struct cpufreq_governor *t;
298 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
299 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
306 * cpufreq_parse_governor - parse a governor string
308 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
309 struct cpufreq_governor **governor)
316 if (cpufreq_driver->setpolicy) {
317 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
318 *policy = CPUFREQ_POLICY_PERFORMANCE;
320 } else if (!strnicmp(str_governor, "powersave",
322 *policy = CPUFREQ_POLICY_POWERSAVE;
325 } else if (cpufreq_driver->target) {
326 struct cpufreq_governor *t;
328 mutex_lock(&cpufreq_governor_mutex);
330 t = __find_governor(str_governor);
335 mutex_unlock(&cpufreq_governor_mutex);
336 ret = request_module("cpufreq_%s", str_governor);
337 mutex_lock(&cpufreq_governor_mutex);
340 t = __find_governor(str_governor);
348 mutex_unlock(&cpufreq_governor_mutex);
356 * cpufreq_per_cpu_attr_read() / show_##file_name() -
357 * print out cpufreq information
359 * Write out information from cpufreq_driver->policy[cpu]; object must be
363 #define show_one(file_name, object) \
364 static ssize_t show_##file_name \
365 (struct cpufreq_policy *policy, char *buf) \
367 return sprintf(buf, "%u\n", policy->object); \
370 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
371 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
372 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
373 show_one(scaling_min_freq, min);
374 show_one(scaling_max_freq, max);
375 show_one(scaling_cur_freq, cur);
377 static int __cpufreq_set_policy(struct cpufreq_policy *data,
378 struct cpufreq_policy *policy);
381 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
383 #define store_one(file_name, object) \
384 static ssize_t store_##file_name \
385 (struct cpufreq_policy *policy, const char *buf, size_t count) \
387 unsigned int ret = -EINVAL; \
388 struct cpufreq_policy new_policy; \
390 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
394 ret = sscanf(buf, "%u", &new_policy.object); \
398 ret = __cpufreq_set_policy(policy, &new_policy); \
399 policy->user_policy.object = policy->object; \
401 return ret ? ret : count; \
404 store_one(scaling_min_freq, min);
405 store_one(scaling_max_freq, max);
408 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
410 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
413 unsigned int cur_freq = __cpufreq_get(policy->cpu);
415 return sprintf(buf, "<unknown>");
416 return sprintf(buf, "%u\n", cur_freq);
421 * show_scaling_governor - show the current policy for the specified CPU
423 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
425 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
426 return sprintf(buf, "powersave\n");
427 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
428 return sprintf(buf, "performance\n");
429 else if (policy->governor)
430 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
431 policy->governor->name);
437 * store_scaling_governor - store policy for the specified CPU
439 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
440 const char *buf, size_t count)
442 unsigned int ret = -EINVAL;
443 char str_governor[16];
444 struct cpufreq_policy new_policy;
446 ret = cpufreq_get_policy(&new_policy, policy->cpu);
450 ret = sscanf(buf, "%15s", str_governor);
454 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
455 &new_policy.governor))
458 /* Do not use cpufreq_set_policy here or the user_policy.max
459 will be wrongly overridden */
460 ret = __cpufreq_set_policy(policy, &new_policy);
462 policy->user_policy.policy = policy->policy;
463 policy->user_policy.governor = policy->governor;
472 * show_scaling_driver - show the cpufreq driver currently loaded
474 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
476 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
480 * show_scaling_available_governors - show the available CPUfreq governors
482 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
486 struct cpufreq_governor *t;
488 if (!cpufreq_driver->target) {
489 i += sprintf(buf, "performance powersave");
493 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
494 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
495 - (CPUFREQ_NAME_LEN + 2)))
497 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
500 i += sprintf(&buf[i], "\n");
504 static ssize_t show_cpus(const struct cpumask *mask, char *buf)
509 for_each_cpu(cpu, mask) {
511 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
512 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
513 if (i >= (PAGE_SIZE - 5))
516 i += sprintf(&buf[i], "\n");
521 * show_related_cpus - show the CPUs affected by each transition even if
522 * hw coordination is in use
524 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
526 if (cpumask_empty(policy->related_cpus))
527 return show_cpus(policy->cpus, buf);
528 return show_cpus(policy->related_cpus, buf);
532 * show_affected_cpus - show the CPUs affected by each transition
534 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
536 return show_cpus(policy->cpus, buf);
539 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
540 const char *buf, size_t count)
542 unsigned int freq = 0;
545 if (!policy->governor || !policy->governor->store_setspeed)
548 ret = sscanf(buf, "%u", &freq);
552 policy->governor->store_setspeed(policy, freq);
557 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
559 if (!policy->governor || !policy->governor->show_setspeed)
560 return sprintf(buf, "<unsupported>\n");
562 return policy->governor->show_setspeed(policy, buf);
566 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
568 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
572 if (cpufreq_driver->bios_limit) {
573 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
575 return sprintf(buf, "%u\n", limit);
577 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
580 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
581 cpufreq_freq_attr_ro(cpuinfo_min_freq);
582 cpufreq_freq_attr_ro(cpuinfo_max_freq);
583 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
584 cpufreq_freq_attr_ro(scaling_available_governors);
585 cpufreq_freq_attr_ro(scaling_driver);
586 cpufreq_freq_attr_ro(scaling_cur_freq);
587 cpufreq_freq_attr_ro(bios_limit);
588 cpufreq_freq_attr_ro(related_cpus);
589 cpufreq_freq_attr_ro(affected_cpus);
590 cpufreq_freq_attr_rw(scaling_min_freq);
591 cpufreq_freq_attr_rw(scaling_max_freq);
592 cpufreq_freq_attr_rw(scaling_governor);
593 cpufreq_freq_attr_rw(scaling_setspeed);
595 static struct attribute *default_attrs[] = {
596 &cpuinfo_min_freq.attr,
597 &cpuinfo_max_freq.attr,
598 &cpuinfo_transition_latency.attr,
599 &scaling_min_freq.attr,
600 &scaling_max_freq.attr,
603 &scaling_governor.attr,
604 &scaling_driver.attr,
605 &scaling_available_governors.attr,
606 &scaling_setspeed.attr,
610 struct kobject *cpufreq_global_kobject;
611 EXPORT_SYMBOL(cpufreq_global_kobject);
613 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
614 #define to_attr(a) container_of(a, struct freq_attr, attr)
616 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
618 struct cpufreq_policy *policy = to_policy(kobj);
619 struct freq_attr *fattr = to_attr(attr);
620 ssize_t ret = -EINVAL;
621 policy = cpufreq_cpu_get(policy->cpu);
625 if (lock_policy_rwsem_read(policy->cpu) < 0)
629 ret = fattr->show(policy, buf);
633 unlock_policy_rwsem_read(policy->cpu);
635 cpufreq_cpu_put(policy);
640 static ssize_t store(struct kobject *kobj, struct attribute *attr,
641 const char *buf, size_t count)
643 struct cpufreq_policy *policy = to_policy(kobj);
644 struct freq_attr *fattr = to_attr(attr);
645 ssize_t ret = -EINVAL;
646 policy = cpufreq_cpu_get(policy->cpu);
650 if (lock_policy_rwsem_write(policy->cpu) < 0)
654 ret = fattr->store(policy, buf, count);
658 unlock_policy_rwsem_write(policy->cpu);
660 cpufreq_cpu_put(policy);
665 static void cpufreq_sysfs_release(struct kobject *kobj)
667 struct cpufreq_policy *policy = to_policy(kobj);
668 pr_debug("last reference is dropped\n");
669 complete(&policy->kobj_unregister);
672 static const struct sysfs_ops sysfs_ops = {
677 static struct kobj_type ktype_cpufreq = {
678 .sysfs_ops = &sysfs_ops,
679 .default_attrs = default_attrs,
680 .release = cpufreq_sysfs_release,
687 * Positive: When we have a managed CPU and the sysfs got symlinked
689 static int cpufreq_add_dev_policy(unsigned int cpu,
690 struct cpufreq_policy *policy,
697 #ifdef CONFIG_HOTPLUG_CPU
698 struct cpufreq_governor *gov;
700 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
702 policy->governor = gov;
703 pr_debug("Restoring governor %s for cpu %d\n",
704 policy->governor->name, cpu);
708 for_each_cpu(j, policy->cpus) {
709 struct cpufreq_policy *managed_policy;
714 /* Check for existing affected CPUs.
715 * They may not be aware of it due to CPU Hotplug.
716 * cpufreq_cpu_put is called when the device is removed
717 * in __cpufreq_remove_dev()
719 managed_policy = cpufreq_cpu_get(j);
720 if (unlikely(managed_policy)) {
722 /* Set proper policy_cpu */
723 unlock_policy_rwsem_write(cpu);
724 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
726 if (lock_policy_rwsem_write(cpu) < 0) {
727 /* Should not go through policy unlock path */
728 if (cpufreq_driver->exit)
729 cpufreq_driver->exit(policy);
730 cpufreq_cpu_put(managed_policy);
734 spin_lock_irqsave(&cpufreq_driver_lock, flags);
735 cpumask_copy(managed_policy->cpus, policy->cpus);
736 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
737 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
739 pr_debug("CPU already managed, adding link\n");
740 ret = sysfs_create_link(&dev->kobj,
741 &managed_policy->kobj,
744 cpufreq_cpu_put(managed_policy);
746 * Success. We only needed to be added to the mask.
747 * Call driver->exit() because only the cpu parent of
748 * the kobj needed to call init().
750 if (cpufreq_driver->exit)
751 cpufreq_driver->exit(policy);
764 /* symlink affected CPUs */
765 static int cpufreq_add_dev_symlink(unsigned int cpu,
766 struct cpufreq_policy *policy)
771 for_each_cpu(j, policy->cpus) {
772 struct cpufreq_policy *managed_policy;
773 struct device *cpu_dev;
780 pr_debug("CPU %u already managed, adding link\n", j);
781 managed_policy = cpufreq_cpu_get(cpu);
782 cpu_dev = get_cpu_device(j);
783 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
786 cpufreq_cpu_put(managed_policy);
793 static int cpufreq_add_dev_interface(unsigned int cpu,
794 struct cpufreq_policy *policy,
797 struct cpufreq_policy new_policy;
798 struct freq_attr **drv_attr;
803 /* prepare interface data */
804 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
805 &dev->kobj, "cpufreq");
809 /* set up files for this cpu device */
810 drv_attr = cpufreq_driver->attr;
811 while ((drv_attr) && (*drv_attr)) {
812 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
814 goto err_out_kobj_put;
817 if (cpufreq_driver->get) {
818 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
820 goto err_out_kobj_put;
822 if (cpufreq_driver->target) {
823 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
825 goto err_out_kobj_put;
827 if (cpufreq_driver->bios_limit) {
828 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
830 goto err_out_kobj_put;
833 spin_lock_irqsave(&cpufreq_driver_lock, flags);
834 for_each_cpu(j, policy->cpus) {
837 per_cpu(cpufreq_cpu_data, j) = policy;
838 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
840 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
842 ret = cpufreq_add_dev_symlink(cpu, policy);
844 goto err_out_kobj_put;
846 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
847 /* assure that the starting sequence is run in __cpufreq_set_policy */
848 policy->governor = NULL;
850 /* set default policy */
851 ret = __cpufreq_set_policy(policy, &new_policy);
852 policy->user_policy.policy = policy->policy;
853 policy->user_policy.governor = policy->governor;
856 pr_debug("setting policy failed\n");
857 if (cpufreq_driver->exit)
858 cpufreq_driver->exit(policy);
863 kobject_put(&policy->kobj);
864 wait_for_completion(&policy->kobj_unregister);
870 * cpufreq_add_dev - add a CPU device
872 * Adds the cpufreq interface for a CPU device.
874 * The Oracle says: try running cpufreq registration/unregistration concurrently
875 * with with cpu hotplugging and all hell will break loose. Tried to clean this
876 * mess up, but more thorough testing is needed. - Mathieu
878 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
880 unsigned int cpu = dev->id;
881 int ret = 0, found = 0;
882 struct cpufreq_policy *policy;
885 #ifdef CONFIG_HOTPLUG_CPU
889 if (cpu_is_offline(cpu))
892 pr_debug("adding CPU %u\n", cpu);
895 /* check whether a different CPU already registered this
896 * CPU because it is in the same boat. */
897 policy = cpufreq_cpu_get(cpu);
898 if (unlikely(policy)) {
899 cpufreq_cpu_put(policy);
904 if (!try_module_get(cpufreq_driver->owner)) {
910 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
914 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
915 goto err_free_policy;
917 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
918 goto err_free_cpumask;
921 cpumask_copy(policy->cpus, cpumask_of(cpu));
923 /* Initially set CPU itself as the policy_cpu */
924 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
925 ret = (lock_policy_rwsem_write(cpu) < 0);
928 init_completion(&policy->kobj_unregister);
929 INIT_WORK(&policy->update, handle_update);
931 /* Set governor before ->init, so that driver could check it */
932 #ifdef CONFIG_HOTPLUG_CPU
933 for_each_online_cpu(sibling) {
934 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
935 if (cp && cp->governor &&
936 (cpumask_test_cpu(cpu, cp->related_cpus))) {
937 policy->governor = cp->governor;
944 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
945 /* call driver. From then on the cpufreq must be able
946 * to accept all calls to ->verify and ->setpolicy for this CPU
948 ret = cpufreq_driver->init(policy);
950 pr_debug("initialization failed\n");
951 goto err_unlock_policy;
953 policy->user_policy.min = policy->min;
954 policy->user_policy.max = policy->max;
956 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
957 CPUFREQ_START, policy);
959 ret = cpufreq_add_dev_policy(cpu, policy, dev);
962 /* This is a managed cpu, symlink created,
965 goto err_unlock_policy;
968 ret = cpufreq_add_dev_interface(cpu, policy, dev);
970 goto err_out_unregister;
972 unlock_policy_rwsem_write(cpu);
974 kobject_uevent(&policy->kobj, KOBJ_ADD);
975 module_put(cpufreq_driver->owner);
976 pr_debug("initialization complete\n");
982 spin_lock_irqsave(&cpufreq_driver_lock, flags);
983 for_each_cpu(j, policy->cpus)
984 per_cpu(cpufreq_cpu_data, j) = NULL;
985 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
987 kobject_put(&policy->kobj);
988 wait_for_completion(&policy->kobj_unregister);
991 unlock_policy_rwsem_write(cpu);
992 free_cpumask_var(policy->related_cpus);
994 free_cpumask_var(policy->cpus);
998 module_put(cpufreq_driver->owner);
1005 * __cpufreq_remove_dev - remove a CPU device
1007 * Removes the cpufreq interface for a CPU device.
1008 * Caller should already have policy_rwsem in write mode for this CPU.
1009 * This routine frees the rwsem before returning.
1011 static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1013 unsigned int cpu = dev->id;
1014 unsigned long flags;
1015 struct cpufreq_policy *data;
1016 struct kobject *kobj;
1017 struct completion *cmp;
1019 struct device *cpu_dev;
1023 pr_debug("unregistering CPU %u\n", cpu);
1025 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1026 data = per_cpu(cpufreq_cpu_data, cpu);
1029 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1030 unlock_policy_rwsem_write(cpu);
1033 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1037 /* if this isn't the CPU which is the parent of the kobj, we
1038 * only need to unlink, put and exit
1040 if (unlikely(cpu != data->cpu)) {
1041 pr_debug("removing link\n");
1042 cpumask_clear_cpu(cpu, data->cpus);
1043 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1045 cpufreq_cpu_put(data);
1046 unlock_policy_rwsem_write(cpu);
1047 sysfs_remove_link(kobj, "cpufreq");
1054 #ifdef CONFIG_HOTPLUG_CPU
1055 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1059 /* if we have other CPUs still registered, we need to unlink them,
1060 * or else wait_for_completion below will lock up. Clean the
1061 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1062 * the sysfs links afterwards.
1064 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1065 for_each_cpu(j, data->cpus) {
1068 per_cpu(cpufreq_cpu_data, j) = NULL;
1072 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1074 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1075 for_each_cpu(j, data->cpus) {
1078 pr_debug("removing link for cpu %u\n", j);
1079 #ifdef CONFIG_HOTPLUG_CPU
1080 strncpy(per_cpu(cpufreq_cpu_governor, j),
1081 data->governor->name, CPUFREQ_NAME_LEN);
1083 cpu_dev = get_cpu_device(j);
1084 kobj = &cpu_dev->kobj;
1085 unlock_policy_rwsem_write(cpu);
1086 sysfs_remove_link(kobj, "cpufreq");
1087 lock_policy_rwsem_write(cpu);
1088 cpufreq_cpu_put(data);
1092 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1095 if (cpufreq_driver->target)
1096 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1099 cmp = &data->kobj_unregister;
1100 unlock_policy_rwsem_write(cpu);
1103 /* we need to make sure that the underlying kobj is actually
1104 * not referenced anymore by anybody before we proceed with
1107 pr_debug("waiting for dropping of refcount\n");
1108 wait_for_completion(cmp);
1109 pr_debug("wait complete\n");
1111 lock_policy_rwsem_write(cpu);
1112 if (cpufreq_driver->exit)
1113 cpufreq_driver->exit(data);
1114 unlock_policy_rwsem_write(cpu);
1116 #ifdef CONFIG_HOTPLUG_CPU
1117 /* when the CPU which is the parent of the kobj is hotplugged
1118 * offline, check for siblings, and create cpufreq sysfs interface
1121 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1122 /* first sibling now owns the new sysfs dir */
1123 cpumask_clear_cpu(cpu, data->cpus);
1124 cpufreq_add_dev(get_cpu_device(cpumask_first(data->cpus)), NULL);
1126 /* finally remove our own symlink */
1127 lock_policy_rwsem_write(cpu);
1128 __cpufreq_remove_dev(dev, sif);
1132 free_cpumask_var(data->related_cpus);
1133 free_cpumask_var(data->cpus);
1140 static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1142 unsigned int cpu = dev->id;
1145 if (cpu_is_offline(cpu))
1148 if (unlikely(lock_policy_rwsem_write(cpu)))
1151 retval = __cpufreq_remove_dev(dev, sif);
1156 static void handle_update(struct work_struct *work)
1158 struct cpufreq_policy *policy =
1159 container_of(work, struct cpufreq_policy, update);
1160 unsigned int cpu = policy->cpu;
1161 pr_debug("handle_update for cpu %u called\n", cpu);
1162 cpufreq_update_policy(cpu);
1166 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1168 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1169 * @new_freq: CPU frequency the CPU actually runs at
1171 * We adjust to current frequency first, and need to clean up later.
1172 * So either call to cpufreq_update_policy() or schedule handle_update()).
1174 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1175 unsigned int new_freq)
1177 struct cpufreq_freqs freqs;
1179 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1180 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1183 freqs.old = old_freq;
1184 freqs.new = new_freq;
1185 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1186 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1191 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1194 * This is the last known freq, without actually getting it from the driver.
1195 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1197 unsigned int cpufreq_quick_get(unsigned int cpu)
1199 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1200 unsigned int ret_freq = 0;
1203 ret_freq = policy->cur;
1204 cpufreq_cpu_put(policy);
1209 EXPORT_SYMBOL(cpufreq_quick_get);
1212 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1215 * Just return the max possible frequency for a given CPU.
1217 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1219 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1220 unsigned int ret_freq = 0;
1223 ret_freq = policy->max;
1224 cpufreq_cpu_put(policy);
1229 EXPORT_SYMBOL(cpufreq_quick_get_max);
1232 static unsigned int __cpufreq_get(unsigned int cpu)
1234 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1235 unsigned int ret_freq = 0;
1237 if (!cpufreq_driver->get)
1240 ret_freq = cpufreq_driver->get(cpu);
1242 if (ret_freq && policy->cur &&
1243 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1244 /* verify no discrepancy between actual and
1245 saved value exists */
1246 if (unlikely(ret_freq != policy->cur)) {
1247 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1248 schedule_work(&policy->update);
1256 * cpufreq_get - get the current CPU frequency (in kHz)
1259 * Get the CPU current (static) CPU frequency
1261 unsigned int cpufreq_get(unsigned int cpu)
1263 unsigned int ret_freq = 0;
1264 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1269 if (unlikely(lock_policy_rwsem_read(cpu)))
1272 ret_freq = __cpufreq_get(cpu);
1274 unlock_policy_rwsem_read(cpu);
1277 cpufreq_cpu_put(policy);
1281 EXPORT_SYMBOL(cpufreq_get);
1283 static struct subsys_interface cpufreq_interface = {
1285 .subsys = &cpu_subsys,
1286 .add_dev = cpufreq_add_dev,
1287 .remove_dev = cpufreq_remove_dev,
1292 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1294 * This function is only executed for the boot processor. The other CPUs
1295 * have been put offline by means of CPU hotplug.
1297 static int cpufreq_bp_suspend(void)
1301 int cpu = smp_processor_id();
1302 struct cpufreq_policy *cpu_policy;
1304 pr_debug("suspending cpu %u\n", cpu);
1306 /* If there's no policy for the boot CPU, we have nothing to do. */
1307 cpu_policy = cpufreq_cpu_get(cpu);
1311 if (cpufreq_driver->suspend) {
1312 ret = cpufreq_driver->suspend(cpu_policy);
1314 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1315 "step on CPU %u\n", cpu_policy->cpu);
1318 cpufreq_cpu_put(cpu_policy);
1323 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1325 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1326 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1327 * restored. It will verify that the current freq is in sync with
1328 * what we believe it to be. This is a bit later than when it
1329 * should be, but nonethteless it's better than calling
1330 * cpufreq_driver->get() here which might re-enable interrupts...
1332 * This function is only executed for the boot CPU. The other CPUs have not
1333 * been turned on yet.
1335 static void cpufreq_bp_resume(void)
1339 int cpu = smp_processor_id();
1340 struct cpufreq_policy *cpu_policy;
1342 pr_debug("resuming cpu %u\n", cpu);
1344 /* If there's no policy for the boot CPU, we have nothing to do. */
1345 cpu_policy = cpufreq_cpu_get(cpu);
1349 if (cpufreq_driver->resume) {
1350 ret = cpufreq_driver->resume(cpu_policy);
1352 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1353 "step on CPU %u\n", cpu_policy->cpu);
1358 schedule_work(&cpu_policy->update);
1361 cpufreq_cpu_put(cpu_policy);
1364 static struct syscore_ops cpufreq_syscore_ops = {
1365 .suspend = cpufreq_bp_suspend,
1366 .resume = cpufreq_bp_resume,
1370 /*********************************************************************
1371 * NOTIFIER LISTS INTERFACE *
1372 *********************************************************************/
1375 * cpufreq_register_notifier - register a driver with cpufreq
1376 * @nb: notifier function to register
1377 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1379 * Add a driver to one of two lists: either a list of drivers that
1380 * are notified about clock rate changes (once before and once after
1381 * the transition), or a list of drivers that are notified about
1382 * changes in cpufreq policy.
1384 * This function may sleep, and has the same return conditions as
1385 * blocking_notifier_chain_register.
1387 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1391 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1394 case CPUFREQ_TRANSITION_NOTIFIER:
1395 ret = srcu_notifier_chain_register(
1396 &cpufreq_transition_notifier_list, nb);
1398 case CPUFREQ_POLICY_NOTIFIER:
1399 ret = blocking_notifier_chain_register(
1400 &cpufreq_policy_notifier_list, nb);
1408 EXPORT_SYMBOL(cpufreq_register_notifier);
1412 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1413 * @nb: notifier block to be unregistered
1414 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1416 * Remove a driver from the CPU frequency notifier list.
1418 * This function may sleep, and has the same return conditions as
1419 * blocking_notifier_chain_unregister.
1421 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1426 case CPUFREQ_TRANSITION_NOTIFIER:
1427 ret = srcu_notifier_chain_unregister(
1428 &cpufreq_transition_notifier_list, nb);
1430 case CPUFREQ_POLICY_NOTIFIER:
1431 ret = blocking_notifier_chain_unregister(
1432 &cpufreq_policy_notifier_list, nb);
1440 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1443 /*********************************************************************
1445 *********************************************************************/
1448 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1449 unsigned int target_freq,
1450 unsigned int relation)
1452 int retval = -EINVAL;
1454 if (cpufreq_disabled())
1457 pr_debug("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1458 target_freq, relation);
1459 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1460 retval = cpufreq_driver->target(policy, target_freq, relation);
1464 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1466 int cpufreq_driver_target(struct cpufreq_policy *policy,
1467 unsigned int target_freq,
1468 unsigned int relation)
1472 policy = cpufreq_cpu_get(policy->cpu);
1476 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1479 ret = __cpufreq_driver_target(policy, target_freq, relation);
1481 unlock_policy_rwsem_write(policy->cpu);
1484 cpufreq_cpu_put(policy);
1488 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1490 int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
1494 policy = cpufreq_cpu_get(policy->cpu);
1498 if (cpu_online(cpu) && cpufreq_driver->getavg)
1499 ret = cpufreq_driver->getavg(policy, cpu);
1501 cpufreq_cpu_put(policy);
1504 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
1507 * when "event" is CPUFREQ_GOV_LIMITS
1510 static int __cpufreq_governor(struct cpufreq_policy *policy,
1515 /* Only must be defined when default governor is known to have latency
1516 restrictions, like e.g. conservative or ondemand.
1517 That this is the case is already ensured in Kconfig
1519 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1520 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1522 struct cpufreq_governor *gov = NULL;
1525 if (policy->governor->max_transition_latency &&
1526 policy->cpuinfo.transition_latency >
1527 policy->governor->max_transition_latency) {
1531 printk(KERN_WARNING "%s governor failed, too long"
1532 " transition latency of HW, fallback"
1533 " to %s governor\n",
1534 policy->governor->name,
1536 policy->governor = gov;
1540 if (!try_module_get(policy->governor->owner))
1543 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1544 policy->cpu, event);
1545 ret = policy->governor->governor(policy, event);
1547 /* we keep one module reference alive for
1548 each CPU governed by this CPU */
1549 if ((event != CPUFREQ_GOV_START) || ret)
1550 module_put(policy->governor->owner);
1551 if ((event == CPUFREQ_GOV_STOP) && !ret)
1552 module_put(policy->governor->owner);
1558 int cpufreq_register_governor(struct cpufreq_governor *governor)
1565 if (cpufreq_disabled())
1568 mutex_lock(&cpufreq_governor_mutex);
1571 if (__find_governor(governor->name) == NULL) {
1573 list_add(&governor->governor_list, &cpufreq_governor_list);
1576 mutex_unlock(&cpufreq_governor_mutex);
1579 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1582 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1584 #ifdef CONFIG_HOTPLUG_CPU
1591 if (cpufreq_disabled())
1594 #ifdef CONFIG_HOTPLUG_CPU
1595 for_each_present_cpu(cpu) {
1596 if (cpu_online(cpu))
1598 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1599 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1603 mutex_lock(&cpufreq_governor_mutex);
1604 list_del(&governor->governor_list);
1605 mutex_unlock(&cpufreq_governor_mutex);
1608 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1612 /*********************************************************************
1613 * POLICY INTERFACE *
1614 *********************************************************************/
1617 * cpufreq_get_policy - get the current cpufreq_policy
1618 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1621 * Reads the current cpufreq policy.
1623 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1625 struct cpufreq_policy *cpu_policy;
1629 cpu_policy = cpufreq_cpu_get(cpu);
1633 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1635 cpufreq_cpu_put(cpu_policy);
1638 EXPORT_SYMBOL(cpufreq_get_policy);
1642 * data : current policy.
1643 * policy : policy to be set.
1645 static int __cpufreq_set_policy(struct cpufreq_policy *data,
1646 struct cpufreq_policy *policy)
1650 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1651 policy->min, policy->max);
1653 memcpy(&policy->cpuinfo, &data->cpuinfo,
1654 sizeof(struct cpufreq_cpuinfo));
1656 if (policy->min > data->max || policy->max < data->min) {
1661 /* verify the cpu speed can be set within this limit */
1662 ret = cpufreq_driver->verify(policy);
1666 /* adjust if necessary - all reasons */
1667 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1668 CPUFREQ_ADJUST, policy);
1670 /* adjust if necessary - hardware incompatibility*/
1671 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1672 CPUFREQ_INCOMPATIBLE, policy);
1674 /* verify the cpu speed can be set within this limit,
1675 which might be different to the first one */
1676 ret = cpufreq_driver->verify(policy);
1680 /* notification of the new policy */
1681 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1682 CPUFREQ_NOTIFY, policy);
1684 data->min = policy->min;
1685 data->max = policy->max;
1687 pr_debug("new min and max freqs are %u - %u kHz\n",
1688 data->min, data->max);
1690 if (cpufreq_driver->setpolicy) {
1691 data->policy = policy->policy;
1692 pr_debug("setting range\n");
1693 ret = cpufreq_driver->setpolicy(policy);
1695 if (policy->governor != data->governor) {
1696 /* save old, working values */
1697 struct cpufreq_governor *old_gov = data->governor;
1699 pr_debug("governor switch\n");
1701 /* end old governor */
1703 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1705 /* start new governor */
1706 data->governor = policy->governor;
1707 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1708 /* new governor failed, so re-start old one */
1709 pr_debug("starting governor %s failed\n",
1710 data->governor->name);
1712 data->governor = old_gov;
1713 __cpufreq_governor(data,
1719 /* might be a policy change, too, so fall through */
1721 pr_debug("governor: change or update limits\n");
1722 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1730 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1731 * @cpu: CPU which shall be re-evaluated
1733 * Useful for policy notifiers which have different necessities
1734 * at different times.
1736 int cpufreq_update_policy(unsigned int cpu)
1738 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1739 struct cpufreq_policy policy;
1747 if (unlikely(lock_policy_rwsem_write(cpu))) {
1752 pr_debug("updating policy for CPU %u\n", cpu);
1753 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1754 policy.min = data->user_policy.min;
1755 policy.max = data->user_policy.max;
1756 policy.policy = data->user_policy.policy;
1757 policy.governor = data->user_policy.governor;
1759 /* BIOS might change freq behind our back
1760 -> ask driver for current freq and notify governors about a change */
1761 if (cpufreq_driver->get) {
1762 policy.cur = cpufreq_driver->get(cpu);
1764 pr_debug("Driver did not initialize current freq");
1765 data->cur = policy.cur;
1767 if (data->cur != policy.cur)
1768 cpufreq_out_of_sync(cpu, data->cur,
1773 ret = __cpufreq_set_policy(data, &policy);
1775 unlock_policy_rwsem_write(cpu);
1778 cpufreq_cpu_put(data);
1782 EXPORT_SYMBOL(cpufreq_update_policy);
1784 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1785 unsigned long action, void *hcpu)
1787 unsigned int cpu = (unsigned long)hcpu;
1790 dev = get_cpu_device(cpu);
1794 case CPU_ONLINE_FROZEN:
1795 cpufreq_add_dev(dev, NULL);
1797 case CPU_DOWN_PREPARE:
1798 case CPU_DOWN_PREPARE_FROZEN:
1799 if (unlikely(lock_policy_rwsem_write(cpu)))
1802 __cpufreq_remove_dev(dev, NULL);
1804 case CPU_DOWN_FAILED:
1805 case CPU_DOWN_FAILED_FROZEN:
1806 cpufreq_add_dev(dev, NULL);
1813 static struct notifier_block __refdata cpufreq_cpu_notifier = {
1814 .notifier_call = cpufreq_cpu_callback,
1817 /*********************************************************************
1818 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1819 *********************************************************************/
1822 * cpufreq_register_driver - register a CPU Frequency driver
1823 * @driver_data: A struct cpufreq_driver containing the values#
1824 * submitted by the CPU Frequency driver.
1826 * Registers a CPU Frequency driver to this core code. This code
1827 * returns zero on success, -EBUSY when another driver got here first
1828 * (and isn't unregistered in the meantime).
1831 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1833 unsigned long flags;
1836 if (cpufreq_disabled())
1839 if (!driver_data || !driver_data->verify || !driver_data->init ||
1840 ((!driver_data->setpolicy) && (!driver_data->target)))
1843 pr_debug("trying to register driver %s\n", driver_data->name);
1845 if (driver_data->setpolicy)
1846 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1848 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1849 if (cpufreq_driver) {
1850 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1853 cpufreq_driver = driver_data;
1854 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1856 ret = subsys_interface_register(&cpufreq_interface);
1858 goto err_null_driver;
1860 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1864 /* check for at least one working CPU */
1865 for (i = 0; i < nr_cpu_ids; i++)
1866 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
1871 /* if all ->init() calls failed, unregister */
1873 pr_debug("no CPU initialized for driver %s\n",
1879 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1880 pr_debug("driver %s up and running\n", driver_data->name);
1884 subsys_interface_unregister(&cpufreq_interface);
1886 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1887 cpufreq_driver = NULL;
1888 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1891 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1895 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1897 * Unregister the current CPUFreq driver. Only call this if you have
1898 * the right to do so, i.e. if you have succeeded in initialising before!
1899 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1900 * currently not initialised.
1902 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1904 unsigned long flags;
1906 if (!cpufreq_driver || (driver != cpufreq_driver))
1909 pr_debug("unregistering driver %s\n", driver->name);
1911 subsys_interface_unregister(&cpufreq_interface);
1912 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1914 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1915 cpufreq_driver = NULL;
1916 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1920 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
1922 static int __init cpufreq_core_init(void)
1926 if (cpufreq_disabled())
1929 for_each_possible_cpu(cpu) {
1930 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1931 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1934 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
1935 BUG_ON(!cpufreq_global_kobject);
1936 register_syscore_ops(&cpufreq_syscore_ops);
1940 core_initcall(cpufreq_core_init);