2 * linux/drivers/cpufreq/cpufreq.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
8 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9 * Added handling for CPU hotplug
10 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/cpu.h>
21 #include <linux/cpufreq.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/init.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/slab.h>
29 #include <linux/syscore_ops.h>
30 #include <linux/tick.h>
31 #include <trace/events/power.h>
34 * The "cpufreq driver" - the arch- or hardware-dependent low
35 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
38 static struct cpufreq_driver *cpufreq_driver;
39 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
40 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
41 static DEFINE_RWLOCK(cpufreq_driver_lock);
42 static DEFINE_MUTEX(cpufreq_governor_lock);
44 #ifdef CONFIG_HOTPLUG_CPU
45 /* This one keeps track of the previously set governor of a removed CPU */
46 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
50 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
51 * all cpufreq/hotplug/workqueue/etc related lock issues.
53 * The rules for this semaphore:
54 * - Any routine that wants to read from the policy structure will
55 * do a down_read on this semaphore.
56 * - Any routine that will write to the policy structure and/or may take away
57 * the policy altogether (eg. CPU hotplug), will hold this lock in write
58 * mode before doing so.
61 * - Governor routines that can be called in cpufreq hotplug path should not
62 * take this sem as top level hotplug notifier handler takes this.
63 * - Lock should not be held across
64 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
66 static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
67 static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
69 #define lock_policy_rwsem(mode, cpu) \
70 static int lock_policy_rwsem_##mode(int cpu) \
72 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
73 BUG_ON(policy_cpu == -1); \
74 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
79 lock_policy_rwsem(read, cpu);
80 lock_policy_rwsem(write, cpu);
82 #define unlock_policy_rwsem(mode, cpu) \
83 static void unlock_policy_rwsem_##mode(int cpu) \
85 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
86 BUG_ON(policy_cpu == -1); \
87 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
90 unlock_policy_rwsem(read, cpu);
91 unlock_policy_rwsem(write, cpu);
93 /* internal prototypes */
94 static int __cpufreq_governor(struct cpufreq_policy *policy,
96 static unsigned int __cpufreq_get(unsigned int cpu);
97 static void handle_update(struct work_struct *work);
100 * Two notifier lists: the "policy" list is involved in the
101 * validation process for a new CPU frequency policy; the
102 * "transition" list for kernel code that needs to handle
103 * changes to devices when the CPU clock speed changes.
104 * The mutex locks both lists.
106 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
107 static struct srcu_notifier_head cpufreq_transition_notifier_list;
109 static bool init_cpufreq_transition_notifier_list_called;
110 static int __init init_cpufreq_transition_notifier_list(void)
112 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
113 init_cpufreq_transition_notifier_list_called = true;
116 pure_initcall(init_cpufreq_transition_notifier_list);
118 static int off __read_mostly;
119 static int cpufreq_disabled(void)
123 void disable_cpufreq(void)
127 static LIST_HEAD(cpufreq_governor_list);
128 static DEFINE_MUTEX(cpufreq_governor_mutex);
130 bool have_governor_per_policy(void)
132 return cpufreq_driver->have_governor_per_policy;
134 EXPORT_SYMBOL_GPL(have_governor_per_policy);
136 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
138 if (have_governor_per_policy())
139 return &policy->kobj;
141 return cpufreq_global_kobject;
143 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
145 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
151 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
153 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
154 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
155 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
156 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
157 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
158 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
160 idle_time = cur_wall_time - busy_time;
162 *wall = cputime_to_usecs(cur_wall_time);
164 return cputime_to_usecs(idle_time);
167 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
169 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
171 if (idle_time == -1ULL)
172 return get_cpu_idle_time_jiffy(cpu, wall);
174 idle_time += get_cpu_iowait_time_us(cpu, wall);
178 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
180 static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
182 struct cpufreq_policy *policy;
185 if (cpu >= nr_cpu_ids)
188 /* get the cpufreq driver */
189 read_lock_irqsave(&cpufreq_driver_lock, flags);
194 if (!try_module_get(cpufreq_driver->owner))
198 policy = per_cpu(cpufreq_cpu_data, cpu);
201 goto err_out_put_module;
203 if (!sysfs && !kobject_get(&policy->kobj))
204 goto err_out_put_module;
206 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
210 module_put(cpufreq_driver->owner);
212 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
217 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
219 if (cpufreq_disabled())
222 return __cpufreq_cpu_get(cpu, false);
224 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
226 static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
228 return __cpufreq_cpu_get(cpu, true);
231 static void __cpufreq_cpu_put(struct cpufreq_policy *policy, bool sysfs)
234 kobject_put(&policy->kobj);
235 module_put(cpufreq_driver->owner);
238 void cpufreq_cpu_put(struct cpufreq_policy *policy)
240 if (cpufreq_disabled())
243 __cpufreq_cpu_put(policy, false);
245 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
247 static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *policy)
249 __cpufreq_cpu_put(policy, true);
252 /*********************************************************************
253 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
254 *********************************************************************/
257 * adjust_jiffies - adjust the system "loops_per_jiffy"
259 * This function alters the system "loops_per_jiffy" for the clock
260 * speed change. Note that loops_per_jiffy cannot be updated on SMP
261 * systems as each CPU might be scaled differently. So, use the arch
262 * per-CPU loops_per_jiffy value wherever possible.
265 static unsigned long l_p_j_ref;
266 static unsigned int l_p_j_ref_freq;
268 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
270 if (ci->flags & CPUFREQ_CONST_LOOPS)
273 if (!l_p_j_ref_freq) {
274 l_p_j_ref = loops_per_jiffy;
275 l_p_j_ref_freq = ci->old;
276 pr_debug("saving %lu as reference value for loops_per_jiffy; "
277 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
279 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
280 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
281 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
283 pr_debug("scaling loops_per_jiffy to %lu "
284 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
288 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
294 static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
295 struct cpufreq_freqs *freqs, unsigned int state)
297 BUG_ON(irqs_disabled());
299 if (cpufreq_disabled())
302 freqs->flags = cpufreq_driver->flags;
303 pr_debug("notification %u of frequency transition to %u kHz\n",
308 case CPUFREQ_PRECHANGE:
309 if (WARN(policy->transition_ongoing ==
310 cpumask_weight(policy->cpus),
311 "In middle of another frequency transition\n"))
314 policy->transition_ongoing++;
316 /* detect if the driver reported a value as "old frequency"
317 * which is not equal to what the cpufreq core thinks is
320 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
321 if ((policy) && (policy->cpu == freqs->cpu) &&
322 (policy->cur) && (policy->cur != freqs->old)) {
323 pr_debug("Warning: CPU frequency is"
324 " %u, cpufreq assumed %u kHz.\n",
325 freqs->old, policy->cur);
326 freqs->old = policy->cur;
329 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
330 CPUFREQ_PRECHANGE, freqs);
331 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
334 case CPUFREQ_POSTCHANGE:
335 if (WARN(!policy->transition_ongoing,
336 "No frequency transition in progress\n"))
339 policy->transition_ongoing--;
341 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
342 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
343 (unsigned long)freqs->cpu);
344 trace_cpu_frequency(freqs->new, freqs->cpu);
345 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
346 CPUFREQ_POSTCHANGE, freqs);
347 if (likely(policy) && likely(policy->cpu == freqs->cpu))
348 policy->cur = freqs->new;
354 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
355 * on frequency transition.
357 * This function calls the transition notifiers and the "adjust_jiffies"
358 * function. It is called twice on all CPU frequency changes that have
361 void cpufreq_notify_transition(struct cpufreq_policy *policy,
362 struct cpufreq_freqs *freqs, unsigned int state)
364 for_each_cpu(freqs->cpu, policy->cpus)
365 __cpufreq_notify_transition(policy, freqs, state);
367 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
370 /*********************************************************************
372 *********************************************************************/
374 static struct cpufreq_governor *__find_governor(const char *str_governor)
376 struct cpufreq_governor *t;
378 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
379 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
386 * cpufreq_parse_governor - parse a governor string
388 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
389 struct cpufreq_governor **governor)
396 if (cpufreq_driver->setpolicy) {
397 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
398 *policy = CPUFREQ_POLICY_PERFORMANCE;
400 } else if (!strnicmp(str_governor, "powersave",
402 *policy = CPUFREQ_POLICY_POWERSAVE;
405 } else if (cpufreq_driver->target) {
406 struct cpufreq_governor *t;
408 mutex_lock(&cpufreq_governor_mutex);
410 t = __find_governor(str_governor);
415 mutex_unlock(&cpufreq_governor_mutex);
416 ret = request_module("cpufreq_%s", str_governor);
417 mutex_lock(&cpufreq_governor_mutex);
420 t = __find_governor(str_governor);
428 mutex_unlock(&cpufreq_governor_mutex);
435 * cpufreq_per_cpu_attr_read() / show_##file_name() -
436 * print out cpufreq information
438 * Write out information from cpufreq_driver->policy[cpu]; object must be
442 #define show_one(file_name, object) \
443 static ssize_t show_##file_name \
444 (struct cpufreq_policy *policy, char *buf) \
446 return sprintf(buf, "%u\n", policy->object); \
449 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
450 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
451 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
452 show_one(scaling_min_freq, min);
453 show_one(scaling_max_freq, max);
454 show_one(scaling_cur_freq, cur);
456 static int __cpufreq_set_policy(struct cpufreq_policy *policy,
457 struct cpufreq_policy *new_policy);
460 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
462 #define store_one(file_name, object) \
463 static ssize_t store_##file_name \
464 (struct cpufreq_policy *policy, const char *buf, size_t count) \
467 struct cpufreq_policy new_policy; \
469 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
473 ret = sscanf(buf, "%u", &new_policy.object); \
477 ret = __cpufreq_set_policy(policy, &new_policy); \
478 policy->user_policy.object = policy->object; \
480 return ret ? ret : count; \
483 store_one(scaling_min_freq, min);
484 store_one(scaling_max_freq, max);
487 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
489 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
492 unsigned int cur_freq = __cpufreq_get(policy->cpu);
494 return sprintf(buf, "<unknown>");
495 return sprintf(buf, "%u\n", cur_freq);
499 * show_scaling_governor - show the current policy for the specified CPU
501 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
503 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
504 return sprintf(buf, "powersave\n");
505 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
506 return sprintf(buf, "performance\n");
507 else if (policy->governor)
508 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
509 policy->governor->name);
514 * store_scaling_governor - store policy for the specified CPU
516 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
517 const char *buf, size_t count)
520 char str_governor[16];
521 struct cpufreq_policy new_policy;
523 ret = cpufreq_get_policy(&new_policy, policy->cpu);
527 ret = sscanf(buf, "%15s", str_governor);
531 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
532 &new_policy.governor))
536 * Do not use cpufreq_set_policy here or the user_policy.max
537 * will be wrongly overridden
539 ret = __cpufreq_set_policy(policy, &new_policy);
541 policy->user_policy.policy = policy->policy;
542 policy->user_policy.governor = policy->governor;
551 * show_scaling_driver - show the cpufreq driver currently loaded
553 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
555 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
559 * show_scaling_available_governors - show the available CPUfreq governors
561 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
565 struct cpufreq_governor *t;
567 if (!cpufreq_driver->target) {
568 i += sprintf(buf, "performance powersave");
572 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
573 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
574 - (CPUFREQ_NAME_LEN + 2)))
576 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
579 i += sprintf(&buf[i], "\n");
583 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
588 for_each_cpu(cpu, mask) {
590 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
591 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
592 if (i >= (PAGE_SIZE - 5))
595 i += sprintf(&buf[i], "\n");
598 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
601 * show_related_cpus - show the CPUs affected by each transition even if
602 * hw coordination is in use
604 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
606 return cpufreq_show_cpus(policy->related_cpus, buf);
610 * show_affected_cpus - show the CPUs affected by each transition
612 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
614 return cpufreq_show_cpus(policy->cpus, buf);
617 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
618 const char *buf, size_t count)
620 unsigned int freq = 0;
623 if (!policy->governor || !policy->governor->store_setspeed)
626 ret = sscanf(buf, "%u", &freq);
630 policy->governor->store_setspeed(policy, freq);
635 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
637 if (!policy->governor || !policy->governor->show_setspeed)
638 return sprintf(buf, "<unsupported>\n");
640 return policy->governor->show_setspeed(policy, buf);
644 * show_bios_limit - show the current cpufreq HW/BIOS limitation
646 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
650 if (cpufreq_driver->bios_limit) {
651 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
653 return sprintf(buf, "%u\n", limit);
655 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
658 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
659 cpufreq_freq_attr_ro(cpuinfo_min_freq);
660 cpufreq_freq_attr_ro(cpuinfo_max_freq);
661 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
662 cpufreq_freq_attr_ro(scaling_available_governors);
663 cpufreq_freq_attr_ro(scaling_driver);
664 cpufreq_freq_attr_ro(scaling_cur_freq);
665 cpufreq_freq_attr_ro(bios_limit);
666 cpufreq_freq_attr_ro(related_cpus);
667 cpufreq_freq_attr_ro(affected_cpus);
668 cpufreq_freq_attr_rw(scaling_min_freq);
669 cpufreq_freq_attr_rw(scaling_max_freq);
670 cpufreq_freq_attr_rw(scaling_governor);
671 cpufreq_freq_attr_rw(scaling_setspeed);
673 static struct attribute *default_attrs[] = {
674 &cpuinfo_min_freq.attr,
675 &cpuinfo_max_freq.attr,
676 &cpuinfo_transition_latency.attr,
677 &scaling_min_freq.attr,
678 &scaling_max_freq.attr,
681 &scaling_governor.attr,
682 &scaling_driver.attr,
683 &scaling_available_governors.attr,
684 &scaling_setspeed.attr,
688 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
689 #define to_attr(a) container_of(a, struct freq_attr, attr)
691 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
693 struct cpufreq_policy *policy = to_policy(kobj);
694 struct freq_attr *fattr = to_attr(attr);
695 ssize_t ret = -EINVAL;
696 policy = cpufreq_cpu_get_sysfs(policy->cpu);
700 if (lock_policy_rwsem_read(policy->cpu) < 0)
704 ret = fattr->show(policy, buf);
708 unlock_policy_rwsem_read(policy->cpu);
710 cpufreq_cpu_put_sysfs(policy);
715 static ssize_t store(struct kobject *kobj, struct attribute *attr,
716 const char *buf, size_t count)
718 struct cpufreq_policy *policy = to_policy(kobj);
719 struct freq_attr *fattr = to_attr(attr);
720 ssize_t ret = -EINVAL;
721 policy = cpufreq_cpu_get_sysfs(policy->cpu);
725 if (lock_policy_rwsem_write(policy->cpu) < 0)
729 ret = fattr->store(policy, buf, count);
733 unlock_policy_rwsem_write(policy->cpu);
735 cpufreq_cpu_put_sysfs(policy);
740 static void cpufreq_sysfs_release(struct kobject *kobj)
742 struct cpufreq_policy *policy = to_policy(kobj);
743 pr_debug("last reference is dropped\n");
744 complete(&policy->kobj_unregister);
747 static const struct sysfs_ops sysfs_ops = {
752 static struct kobj_type ktype_cpufreq = {
753 .sysfs_ops = &sysfs_ops,
754 .default_attrs = default_attrs,
755 .release = cpufreq_sysfs_release,
758 struct kobject *cpufreq_global_kobject;
759 EXPORT_SYMBOL(cpufreq_global_kobject);
761 static int cpufreq_global_kobject_usage;
763 int cpufreq_get_global_kobject(void)
765 if (!cpufreq_global_kobject_usage++)
766 return kobject_add(cpufreq_global_kobject,
767 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
771 EXPORT_SYMBOL(cpufreq_get_global_kobject);
773 void cpufreq_put_global_kobject(void)
775 if (!--cpufreq_global_kobject_usage)
776 kobject_del(cpufreq_global_kobject);
778 EXPORT_SYMBOL(cpufreq_put_global_kobject);
780 int cpufreq_sysfs_create_file(const struct attribute *attr)
782 int ret = cpufreq_get_global_kobject();
785 ret = sysfs_create_file(cpufreq_global_kobject, attr);
787 cpufreq_put_global_kobject();
792 EXPORT_SYMBOL(cpufreq_sysfs_create_file);
794 void cpufreq_sysfs_remove_file(const struct attribute *attr)
796 sysfs_remove_file(cpufreq_global_kobject, attr);
797 cpufreq_put_global_kobject();
799 EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
801 /* symlink affected CPUs */
802 static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
807 for_each_cpu(j, policy->cpus) {
808 struct device *cpu_dev;
810 if (j == policy->cpu)
813 pr_debug("Adding link for CPU: %u\n", j);
814 cpu_dev = get_cpu_device(j);
815 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
823 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
826 struct freq_attr **drv_attr;
829 /* prepare interface data */
830 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
831 &dev->kobj, "cpufreq");
835 /* set up files for this cpu device */
836 drv_attr = cpufreq_driver->attr;
837 while ((drv_attr) && (*drv_attr)) {
838 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
840 goto err_out_kobj_put;
843 if (cpufreq_driver->get) {
844 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
846 goto err_out_kobj_put;
848 if (cpufreq_driver->target) {
849 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
851 goto err_out_kobj_put;
853 if (cpufreq_driver->bios_limit) {
854 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
856 goto err_out_kobj_put;
859 ret = cpufreq_add_dev_symlink(policy);
861 goto err_out_kobj_put;
866 kobject_put(&policy->kobj);
867 wait_for_completion(&policy->kobj_unregister);
871 static void cpufreq_init_policy(struct cpufreq_policy *policy)
873 struct cpufreq_policy new_policy;
876 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
877 /* assure that the starting sequence is run in __cpufreq_set_policy */
878 policy->governor = NULL;
880 /* set default policy */
881 ret = __cpufreq_set_policy(policy, &new_policy);
882 policy->user_policy.policy = policy->policy;
883 policy->user_policy.governor = policy->governor;
886 pr_debug("setting policy failed\n");
887 if (cpufreq_driver->exit)
888 cpufreq_driver->exit(policy);
892 #ifdef CONFIG_HOTPLUG_CPU
893 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
894 unsigned int cpu, struct device *dev,
897 int ret = 0, has_target = !!cpufreq_driver->target;
901 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
903 lock_policy_rwsem_write(policy->cpu);
905 write_lock_irqsave(&cpufreq_driver_lock, flags);
907 cpumask_set_cpu(cpu, policy->cpus);
908 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
909 per_cpu(cpufreq_cpu_data, cpu) = policy;
910 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
912 unlock_policy_rwsem_write(policy->cpu);
915 __cpufreq_governor(policy, CPUFREQ_GOV_START);
916 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
919 /* Don't touch sysfs links during light-weight init */
921 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
927 static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
929 struct cpufreq_policy *policy;
932 write_lock_irqsave(&cpufreq_driver_lock, flags);
934 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
936 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
941 static struct cpufreq_policy *cpufreq_policy_alloc(void)
943 struct cpufreq_policy *policy;
945 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
949 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
950 goto err_free_policy;
952 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
953 goto err_free_cpumask;
958 free_cpumask_var(policy->cpus);
965 static void cpufreq_policy_free(struct cpufreq_policy *policy)
967 free_cpumask_var(policy->related_cpus);
968 free_cpumask_var(policy->cpus);
972 static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
975 unsigned int j, cpu = dev->id;
977 struct cpufreq_policy *policy;
979 #ifdef CONFIG_HOTPLUG_CPU
980 struct cpufreq_governor *gov;
984 if (cpu_is_offline(cpu))
987 pr_debug("adding CPU %u\n", cpu);
990 /* check whether a different CPU already registered this
991 * CPU because it is in the same boat. */
992 policy = cpufreq_cpu_get(cpu);
993 if (unlikely(policy)) {
994 cpufreq_cpu_put(policy);
998 #ifdef CONFIG_HOTPLUG_CPU
999 /* Check if this cpu was hot-unplugged earlier and has siblings */
1000 read_lock_irqsave(&cpufreq_driver_lock, flags);
1001 for_each_online_cpu(sibling) {
1002 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
1003 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
1004 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1005 return cpufreq_add_policy_cpu(cp, cpu, dev, frozen);
1008 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1012 if (!try_module_get(cpufreq_driver->owner)) {
1018 /* Restore the saved policy when doing light-weight init */
1019 policy = cpufreq_policy_restore(cpu);
1021 policy = cpufreq_policy_alloc();
1027 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1028 cpumask_copy(policy->cpus, cpumask_of(cpu));
1030 /* Initially set CPU itself as the policy_cpu */
1031 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
1033 init_completion(&policy->kobj_unregister);
1034 INIT_WORK(&policy->update, handle_update);
1036 /* call driver. From then on the cpufreq must be able
1037 * to accept all calls to ->verify and ->setpolicy for this CPU
1039 ret = cpufreq_driver->init(policy);
1041 pr_debug("initialization failed\n");
1042 goto err_set_policy_cpu;
1045 /* related cpus should atleast have policy->cpus */
1046 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1049 * affected cpus must always be the one, which are online. We aren't
1050 * managing offline cpus here.
1052 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1054 policy->user_policy.min = policy->min;
1055 policy->user_policy.max = policy->max;
1057 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1058 CPUFREQ_START, policy);
1060 #ifdef CONFIG_HOTPLUG_CPU
1061 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1063 policy->governor = gov;
1064 pr_debug("Restoring governor %s for cpu %d\n",
1065 policy->governor->name, cpu);
1069 write_lock_irqsave(&cpufreq_driver_lock, flags);
1070 for_each_cpu(j, policy->cpus) {
1071 per_cpu(cpufreq_cpu_data, j) = policy;
1072 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
1074 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1077 ret = cpufreq_add_dev_interface(policy, dev);
1079 goto err_out_unregister;
1082 cpufreq_init_policy(policy);
1084 kobject_uevent(&policy->kobj, KOBJ_ADD);
1085 module_put(cpufreq_driver->owner);
1086 pr_debug("initialization complete\n");
1091 write_lock_irqsave(&cpufreq_driver_lock, flags);
1092 for_each_cpu(j, policy->cpus) {
1093 per_cpu(cpufreq_cpu_data, j) = NULL;
1095 per_cpu(cpufreq_policy_cpu, j) = -1;
1097 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1100 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1101 cpufreq_policy_free(policy);
1103 module_put(cpufreq_driver->owner);
1109 * cpufreq_add_dev - add a CPU device
1111 * Adds the cpufreq interface for a CPU device.
1113 * The Oracle says: try running cpufreq registration/unregistration concurrently
1114 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1115 * mess up, but more thorough testing is needed. - Mathieu
1117 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1119 return __cpufreq_add_dev(dev, sif, false);
1122 static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1126 policy->last_cpu = policy->cpu;
1129 for_each_cpu(j, policy->cpus)
1130 per_cpu(cpufreq_policy_cpu, j) = cpu;
1132 #ifdef CONFIG_CPU_FREQ_TABLE
1133 cpufreq_frequency_table_update_policy_cpu(policy);
1135 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1136 CPUFREQ_UPDATE_POLICY_CPU, policy);
1139 static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
1140 unsigned int old_cpu, bool frozen)
1142 struct device *cpu_dev;
1143 unsigned long flags;
1146 /* first sibling now owns the new sysfs dir */
1147 cpu_dev = get_cpu_device(cpumask_first(policy->cpus));
1149 /* Don't touch sysfs files during light-weight tear-down */
1153 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1154 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1156 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1158 WARN_ON(lock_policy_rwsem_write(old_cpu));
1159 cpumask_set_cpu(old_cpu, policy->cpus);
1161 write_lock_irqsave(&cpufreq_driver_lock, flags);
1162 per_cpu(cpufreq_cpu_data, old_cpu) = policy;
1163 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1165 unlock_policy_rwsem_write(old_cpu);
1167 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1177 * __cpufreq_remove_dev - remove a CPU device
1179 * Removes the cpufreq interface for a CPU device.
1180 * Caller should already have policy_rwsem in write mode for this CPU.
1181 * This routine frees the rwsem before returning.
1183 static int __cpufreq_remove_dev(struct device *dev,
1184 struct subsys_interface *sif, bool frozen)
1186 unsigned int cpu = dev->id, cpus;
1188 unsigned long flags;
1189 struct cpufreq_policy *policy;
1190 struct kobject *kobj;
1191 struct completion *cmp;
1193 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1195 write_lock_irqsave(&cpufreq_driver_lock, flags);
1197 policy = per_cpu(cpufreq_cpu_data, cpu);
1198 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1200 /* Save the policy somewhere when doing a light-weight tear-down */
1202 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
1204 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1207 pr_debug("%s: No cpu_data found\n", __func__);
1211 if (cpufreq_driver->target)
1212 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1214 #ifdef CONFIG_HOTPLUG_CPU
1215 if (!cpufreq_driver->setpolicy)
1216 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1217 policy->governor->name, CPUFREQ_NAME_LEN);
1220 WARN_ON(lock_policy_rwsem_write(cpu));
1221 cpus = cpumask_weight(policy->cpus);
1224 cpumask_clear_cpu(cpu, policy->cpus);
1225 unlock_policy_rwsem_write(cpu);
1227 if (cpu != policy->cpu && !frozen) {
1228 sysfs_remove_link(&dev->kobj, "cpufreq");
1229 } else if (cpus > 1) {
1231 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
1233 WARN_ON(lock_policy_rwsem_write(cpu));
1234 update_policy_cpu(policy, new_cpu);
1235 unlock_policy_rwsem_write(cpu);
1238 pr_debug("%s: policy Kobject moved to cpu: %d "
1239 "from: %d\n",__func__, new_cpu, cpu);
1244 /* If cpu is last user of policy, free policy */
1246 if (cpufreq_driver->target)
1247 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
1250 lock_policy_rwsem_read(cpu);
1251 kobj = &policy->kobj;
1252 cmp = &policy->kobj_unregister;
1253 unlock_policy_rwsem_read(cpu);
1257 * We need to make sure that the underlying kobj is
1258 * actually not referenced anymore by anybody before we
1259 * proceed with unloading.
1261 pr_debug("waiting for dropping of refcount\n");
1262 wait_for_completion(cmp);
1263 pr_debug("wait complete\n");
1267 * Perform the ->exit() even during light-weight tear-down,
1268 * since this is a core component, and is essential for the
1269 * subsequent light-weight ->init() to succeed.
1271 if (cpufreq_driver->exit)
1272 cpufreq_driver->exit(policy);
1275 cpufreq_policy_free(policy);
1277 if (cpufreq_driver->target) {
1278 __cpufreq_governor(policy, CPUFREQ_GOV_START);
1279 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1283 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1287 static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1289 unsigned int cpu = dev->id;
1292 if (cpu_is_offline(cpu))
1295 retval = __cpufreq_remove_dev(dev, sif, false);
1299 static void handle_update(struct work_struct *work)
1301 struct cpufreq_policy *policy =
1302 container_of(work, struct cpufreq_policy, update);
1303 unsigned int cpu = policy->cpu;
1304 pr_debug("handle_update for cpu %u called\n", cpu);
1305 cpufreq_update_policy(cpu);
1309 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1312 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1313 * @new_freq: CPU frequency the CPU actually runs at
1315 * We adjust to current frequency first, and need to clean up later.
1316 * So either call to cpufreq_update_policy() or schedule handle_update()).
1318 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1319 unsigned int new_freq)
1321 struct cpufreq_policy *policy;
1322 struct cpufreq_freqs freqs;
1323 unsigned long flags;
1325 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1326 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1328 freqs.old = old_freq;
1329 freqs.new = new_freq;
1331 read_lock_irqsave(&cpufreq_driver_lock, flags);
1332 policy = per_cpu(cpufreq_cpu_data, cpu);
1333 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1335 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1336 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
1340 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1343 * This is the last known freq, without actually getting it from the driver.
1344 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1346 unsigned int cpufreq_quick_get(unsigned int cpu)
1348 struct cpufreq_policy *policy;
1349 unsigned int ret_freq = 0;
1351 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1352 return cpufreq_driver->get(cpu);
1354 policy = cpufreq_cpu_get(cpu);
1356 ret_freq = policy->cur;
1357 cpufreq_cpu_put(policy);
1362 EXPORT_SYMBOL(cpufreq_quick_get);
1365 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1368 * Just return the max possible frequency for a given CPU.
1370 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1372 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1373 unsigned int ret_freq = 0;
1376 ret_freq = policy->max;
1377 cpufreq_cpu_put(policy);
1382 EXPORT_SYMBOL(cpufreq_quick_get_max);
1384 static unsigned int __cpufreq_get(unsigned int cpu)
1386 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1387 unsigned int ret_freq = 0;
1389 if (!cpufreq_driver->get)
1392 ret_freq = cpufreq_driver->get(cpu);
1394 if (ret_freq && policy->cur &&
1395 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1396 /* verify no discrepancy between actual and
1397 saved value exists */
1398 if (unlikely(ret_freq != policy->cur)) {
1399 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1400 schedule_work(&policy->update);
1408 * cpufreq_get - get the current CPU frequency (in kHz)
1411 * Get the CPU current (static) CPU frequency
1413 unsigned int cpufreq_get(unsigned int cpu)
1415 unsigned int ret_freq = 0;
1416 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1421 if (unlikely(lock_policy_rwsem_read(cpu)))
1424 ret_freq = __cpufreq_get(cpu);
1426 unlock_policy_rwsem_read(cpu);
1429 cpufreq_cpu_put(policy);
1433 EXPORT_SYMBOL(cpufreq_get);
1435 static struct subsys_interface cpufreq_interface = {
1437 .subsys = &cpu_subsys,
1438 .add_dev = cpufreq_add_dev,
1439 .remove_dev = cpufreq_remove_dev,
1443 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1445 * This function is only executed for the boot processor. The other CPUs
1446 * have been put offline by means of CPU hotplug.
1448 static int cpufreq_bp_suspend(void)
1452 int cpu = smp_processor_id();
1453 struct cpufreq_policy *policy;
1455 pr_debug("suspending cpu %u\n", cpu);
1457 /* If there's no policy for the boot CPU, we have nothing to do. */
1458 policy = cpufreq_cpu_get(cpu);
1462 if (cpufreq_driver->suspend) {
1463 ret = cpufreq_driver->suspend(policy);
1465 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1466 "step on CPU %u\n", policy->cpu);
1469 cpufreq_cpu_put(policy);
1474 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1476 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1477 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1478 * restored. It will verify that the current freq is in sync with
1479 * what we believe it to be. This is a bit later than when it
1480 * should be, but nonethteless it's better than calling
1481 * cpufreq_driver->get() here which might re-enable interrupts...
1483 * This function is only executed for the boot CPU. The other CPUs have not
1484 * been turned on yet.
1486 static void cpufreq_bp_resume(void)
1490 int cpu = smp_processor_id();
1491 struct cpufreq_policy *policy;
1493 pr_debug("resuming cpu %u\n", cpu);
1495 /* If there's no policy for the boot CPU, we have nothing to do. */
1496 policy = cpufreq_cpu_get(cpu);
1500 if (cpufreq_driver->resume) {
1501 ret = cpufreq_driver->resume(policy);
1503 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1504 "step on CPU %u\n", policy->cpu);
1509 schedule_work(&policy->update);
1512 cpufreq_cpu_put(policy);
1515 static struct syscore_ops cpufreq_syscore_ops = {
1516 .suspend = cpufreq_bp_suspend,
1517 .resume = cpufreq_bp_resume,
1521 * cpufreq_get_current_driver - return current driver's name
1523 * Return the name string of the currently loaded cpufreq driver
1526 const char *cpufreq_get_current_driver(void)
1529 return cpufreq_driver->name;
1533 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1535 /*********************************************************************
1536 * NOTIFIER LISTS INTERFACE *
1537 *********************************************************************/
1540 * cpufreq_register_notifier - register a driver with cpufreq
1541 * @nb: notifier function to register
1542 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1544 * Add a driver to one of two lists: either a list of drivers that
1545 * are notified about clock rate changes (once before and once after
1546 * the transition), or a list of drivers that are notified about
1547 * changes in cpufreq policy.
1549 * This function may sleep, and has the same return conditions as
1550 * blocking_notifier_chain_register.
1552 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1556 if (cpufreq_disabled())
1559 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1562 case CPUFREQ_TRANSITION_NOTIFIER:
1563 ret = srcu_notifier_chain_register(
1564 &cpufreq_transition_notifier_list, nb);
1566 case CPUFREQ_POLICY_NOTIFIER:
1567 ret = blocking_notifier_chain_register(
1568 &cpufreq_policy_notifier_list, nb);
1576 EXPORT_SYMBOL(cpufreq_register_notifier);
1579 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1580 * @nb: notifier block to be unregistered
1581 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1583 * Remove a driver from the CPU frequency notifier list.
1585 * This function may sleep, and has the same return conditions as
1586 * blocking_notifier_chain_unregister.
1588 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1592 if (cpufreq_disabled())
1596 case CPUFREQ_TRANSITION_NOTIFIER:
1597 ret = srcu_notifier_chain_unregister(
1598 &cpufreq_transition_notifier_list, nb);
1600 case CPUFREQ_POLICY_NOTIFIER:
1601 ret = blocking_notifier_chain_unregister(
1602 &cpufreq_policy_notifier_list, nb);
1610 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1613 /*********************************************************************
1615 *********************************************************************/
1617 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1618 unsigned int target_freq,
1619 unsigned int relation)
1621 int retval = -EINVAL;
1622 unsigned int old_target_freq = target_freq;
1624 if (cpufreq_disabled())
1626 if (policy->transition_ongoing)
1629 /* Make sure that target_freq is within supported range */
1630 if (target_freq > policy->max)
1631 target_freq = policy->max;
1632 if (target_freq < policy->min)
1633 target_freq = policy->min;
1635 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1636 policy->cpu, target_freq, relation, old_target_freq);
1638 if (target_freq == policy->cur)
1641 if (cpufreq_driver->target)
1642 retval = cpufreq_driver->target(policy, target_freq, relation);
1646 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1648 int cpufreq_driver_target(struct cpufreq_policy *policy,
1649 unsigned int target_freq,
1650 unsigned int relation)
1654 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1657 ret = __cpufreq_driver_target(policy, target_freq, relation);
1659 unlock_policy_rwsem_write(policy->cpu);
1664 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1667 * when "event" is CPUFREQ_GOV_LIMITS
1670 static int __cpufreq_governor(struct cpufreq_policy *policy,
1675 /* Only must be defined when default governor is known to have latency
1676 restrictions, like e.g. conservative or ondemand.
1677 That this is the case is already ensured in Kconfig
1679 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1680 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1682 struct cpufreq_governor *gov = NULL;
1685 if (policy->governor->max_transition_latency &&
1686 policy->cpuinfo.transition_latency >
1687 policy->governor->max_transition_latency) {
1691 printk(KERN_WARNING "%s governor failed, too long"
1692 " transition latency of HW, fallback"
1693 " to %s governor\n",
1694 policy->governor->name,
1696 policy->governor = gov;
1700 if (!try_module_get(policy->governor->owner))
1703 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1704 policy->cpu, event);
1706 mutex_lock(&cpufreq_governor_lock);
1707 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1708 (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1709 mutex_unlock(&cpufreq_governor_lock);
1713 if (event == CPUFREQ_GOV_STOP)
1714 policy->governor_enabled = false;
1715 else if (event == CPUFREQ_GOV_START)
1716 policy->governor_enabled = true;
1718 mutex_unlock(&cpufreq_governor_lock);
1720 ret = policy->governor->governor(policy, event);
1723 if (event == CPUFREQ_GOV_POLICY_INIT)
1724 policy->governor->initialized++;
1725 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1726 policy->governor->initialized--;
1728 /* Restore original values */
1729 mutex_lock(&cpufreq_governor_lock);
1730 if (event == CPUFREQ_GOV_STOP)
1731 policy->governor_enabled = true;
1732 else if (event == CPUFREQ_GOV_START)
1733 policy->governor_enabled = false;
1734 mutex_unlock(&cpufreq_governor_lock);
1737 /* we keep one module reference alive for
1738 each CPU governed by this CPU */
1739 if ((event != CPUFREQ_GOV_START) || ret)
1740 module_put(policy->governor->owner);
1741 if ((event == CPUFREQ_GOV_STOP) && !ret)
1742 module_put(policy->governor->owner);
1747 int cpufreq_register_governor(struct cpufreq_governor *governor)
1754 if (cpufreq_disabled())
1757 mutex_lock(&cpufreq_governor_mutex);
1759 governor->initialized = 0;
1761 if (__find_governor(governor->name) == NULL) {
1763 list_add(&governor->governor_list, &cpufreq_governor_list);
1766 mutex_unlock(&cpufreq_governor_mutex);
1769 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1771 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1773 #ifdef CONFIG_HOTPLUG_CPU
1780 if (cpufreq_disabled())
1783 #ifdef CONFIG_HOTPLUG_CPU
1784 for_each_present_cpu(cpu) {
1785 if (cpu_online(cpu))
1787 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1788 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1792 mutex_lock(&cpufreq_governor_mutex);
1793 list_del(&governor->governor_list);
1794 mutex_unlock(&cpufreq_governor_mutex);
1797 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1800 /*********************************************************************
1801 * POLICY INTERFACE *
1802 *********************************************************************/
1805 * cpufreq_get_policy - get the current cpufreq_policy
1806 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1809 * Reads the current cpufreq policy.
1811 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1813 struct cpufreq_policy *cpu_policy;
1817 cpu_policy = cpufreq_cpu_get(cpu);
1821 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1823 cpufreq_cpu_put(cpu_policy);
1826 EXPORT_SYMBOL(cpufreq_get_policy);
1829 * data : current policy.
1830 * policy : policy to be set.
1832 static int __cpufreq_set_policy(struct cpufreq_policy *policy,
1833 struct cpufreq_policy *new_policy)
1835 int ret = 0, failed = 1;
1837 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1838 new_policy->min, new_policy->max);
1840 memcpy(&new_policy->cpuinfo, &policy->cpuinfo,
1841 sizeof(struct cpufreq_cpuinfo));
1843 if (new_policy->min > policy->max || new_policy->max < policy->min) {
1848 /* verify the cpu speed can be set within this limit */
1849 ret = cpufreq_driver->verify(new_policy);
1853 /* adjust if necessary - all reasons */
1854 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1855 CPUFREQ_ADJUST, new_policy);
1857 /* adjust if necessary - hardware incompatibility*/
1858 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1859 CPUFREQ_INCOMPATIBLE, new_policy);
1862 * verify the cpu speed can be set within this limit, which might be
1863 * different to the first one
1865 ret = cpufreq_driver->verify(new_policy);
1869 /* notification of the new policy */
1870 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1871 CPUFREQ_NOTIFY, new_policy);
1873 policy->min = new_policy->min;
1874 policy->max = new_policy->max;
1876 pr_debug("new min and max freqs are %u - %u kHz\n",
1877 policy->min, policy->max);
1879 if (cpufreq_driver->setpolicy) {
1880 policy->policy = new_policy->policy;
1881 pr_debug("setting range\n");
1882 ret = cpufreq_driver->setpolicy(new_policy);
1884 if (new_policy->governor != policy->governor) {
1885 /* save old, working values */
1886 struct cpufreq_governor *old_gov = policy->governor;
1888 pr_debug("governor switch\n");
1890 /* end old governor */
1891 if (policy->governor) {
1892 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1893 unlock_policy_rwsem_write(new_policy->cpu);
1894 __cpufreq_governor(policy,
1895 CPUFREQ_GOV_POLICY_EXIT);
1896 lock_policy_rwsem_write(new_policy->cpu);
1899 /* start new governor */
1900 policy->governor = new_policy->governor;
1901 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
1902 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
1905 unlock_policy_rwsem_write(new_policy->cpu);
1906 __cpufreq_governor(policy,
1907 CPUFREQ_GOV_POLICY_EXIT);
1908 lock_policy_rwsem_write(new_policy->cpu);
1913 /* new governor failed, so re-start old one */
1914 pr_debug("starting governor %s failed\n",
1915 policy->governor->name);
1917 policy->governor = old_gov;
1918 __cpufreq_governor(policy,
1919 CPUFREQ_GOV_POLICY_INIT);
1920 __cpufreq_governor(policy,
1926 /* might be a policy change, too, so fall through */
1928 pr_debug("governor: change or update limits\n");
1929 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1937 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1938 * @cpu: CPU which shall be re-evaluated
1940 * Useful for policy notifiers which have different necessities
1941 * at different times.
1943 int cpufreq_update_policy(unsigned int cpu)
1945 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1946 struct cpufreq_policy new_policy;
1954 if (unlikely(lock_policy_rwsem_write(cpu))) {
1959 pr_debug("updating policy for CPU %u\n", cpu);
1960 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
1961 new_policy.min = policy->user_policy.min;
1962 new_policy.max = policy->user_policy.max;
1963 new_policy.policy = policy->user_policy.policy;
1964 new_policy.governor = policy->user_policy.governor;
1967 * BIOS might change freq behind our back
1968 * -> ask driver for current freq and notify governors about a change
1970 if (cpufreq_driver->get) {
1971 new_policy.cur = cpufreq_driver->get(cpu);
1973 pr_debug("Driver did not initialize current freq");
1974 policy->cur = new_policy.cur;
1976 if (policy->cur != new_policy.cur && cpufreq_driver->target)
1977 cpufreq_out_of_sync(cpu, policy->cur,
1982 ret = __cpufreq_set_policy(policy, &new_policy);
1984 unlock_policy_rwsem_write(cpu);
1987 cpufreq_cpu_put(policy);
1991 EXPORT_SYMBOL(cpufreq_update_policy);
1993 static int cpufreq_cpu_callback(struct notifier_block *nfb,
1994 unsigned long action, void *hcpu)
1996 unsigned int cpu = (unsigned long)hcpu;
1998 bool frozen = false;
2000 dev = get_cpu_device(cpu);
2003 if (action & CPU_TASKS_FROZEN)
2006 switch (action & ~CPU_TASKS_FROZEN) {
2008 __cpufreq_add_dev(dev, NULL, frozen);
2009 cpufreq_update_policy(cpu);
2012 case CPU_DOWN_PREPARE:
2013 __cpufreq_remove_dev(dev, NULL, frozen);
2016 case CPU_DOWN_FAILED:
2017 __cpufreq_add_dev(dev, NULL, frozen);
2024 static struct notifier_block __refdata cpufreq_cpu_notifier = {
2025 .notifier_call = cpufreq_cpu_callback,
2028 /*********************************************************************
2029 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2030 *********************************************************************/
2033 * cpufreq_register_driver - register a CPU Frequency driver
2034 * @driver_data: A struct cpufreq_driver containing the values#
2035 * submitted by the CPU Frequency driver.
2037 * Registers a CPU Frequency driver to this core code. This code
2038 * returns zero on success, -EBUSY when another driver got here first
2039 * (and isn't unregistered in the meantime).
2042 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2044 unsigned long flags;
2047 if (cpufreq_disabled())
2050 if (!driver_data || !driver_data->verify || !driver_data->init ||
2051 ((!driver_data->setpolicy) && (!driver_data->target)))
2054 pr_debug("trying to register driver %s\n", driver_data->name);
2056 if (driver_data->setpolicy)
2057 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2059 write_lock_irqsave(&cpufreq_driver_lock, flags);
2060 if (cpufreq_driver) {
2061 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2064 cpufreq_driver = driver_data;
2065 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2067 ret = subsys_interface_register(&cpufreq_interface);
2069 goto err_null_driver;
2071 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
2075 /* check for at least one working CPU */
2076 for (i = 0; i < nr_cpu_ids; i++)
2077 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
2082 /* if all ->init() calls failed, unregister */
2084 pr_debug("no CPU initialized for driver %s\n",
2090 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2091 pr_debug("driver %s up and running\n", driver_data->name);
2095 subsys_interface_unregister(&cpufreq_interface);
2097 write_lock_irqsave(&cpufreq_driver_lock, flags);
2098 cpufreq_driver = NULL;
2099 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2102 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2105 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2107 * Unregister the current CPUFreq driver. Only call this if you have
2108 * the right to do so, i.e. if you have succeeded in initialising before!
2109 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2110 * currently not initialised.
2112 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2114 unsigned long flags;
2116 if (!cpufreq_driver || (driver != cpufreq_driver))
2119 pr_debug("unregistering driver %s\n", driver->name);
2121 subsys_interface_unregister(&cpufreq_interface);
2122 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2124 write_lock_irqsave(&cpufreq_driver_lock, flags);
2125 cpufreq_driver = NULL;
2126 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2130 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2132 static int __init cpufreq_core_init(void)
2136 if (cpufreq_disabled())
2139 for_each_possible_cpu(cpu) {
2140 per_cpu(cpufreq_policy_cpu, cpu) = -1;
2141 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2144 cpufreq_global_kobject = kobject_create();
2145 BUG_ON(!cpufreq_global_kobject);
2146 register_syscore_ops(&cpufreq_syscore_ops);
2150 core_initcall(cpufreq_core_init);