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 <asm/cputime.h>
21 #include <linux/kernel.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/notifier.h>
26 #include <linux/cpufreq.h>
27 #include <linux/delay.h>
28 #include <linux/interrupt.h>
29 #include <linux/spinlock.h>
30 #include <linux/tick.h>
31 #include <linux/device.h>
32 #include <linux/slab.h>
33 #include <linux/cpu.h>
34 #include <linux/completion.h>
35 #include <linux/mutex.h>
36 #include <linux/syscore_ops.h>
38 #include <trace/events/power.h>
41 * The "cpufreq driver" - the arch- or hardware-dependent low
42 * level driver of CPUFreq support, and its spinlock. This lock
43 * also protects the cpufreq_cpu_data array.
45 static struct cpufreq_driver *cpufreq_driver;
46 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
47 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
48 static DEFINE_RWLOCK(cpufreq_driver_lock);
49 static DEFINE_MUTEX(cpufreq_governor_lock);
51 #ifdef CONFIG_HOTPLUG_CPU
52 /* This one keeps track of the previously set governor of a removed CPU */
53 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
57 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
58 * all cpufreq/hotplug/workqueue/etc related lock issues.
60 * The rules for this semaphore:
61 * - Any routine that wants to read from the policy structure will
62 * do a down_read on this semaphore.
63 * - Any routine that will write to the policy structure and/or may take away
64 * the policy altogether (eg. CPU hotplug), will hold this lock in write
65 * mode before doing so.
68 * - Governor routines that can be called in cpufreq hotplug path should not
69 * take this sem as top level hotplug notifier handler takes this.
70 * - Lock should not be held across
71 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
73 static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
74 static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
76 #define lock_policy_rwsem(mode, cpu) \
77 static int lock_policy_rwsem_##mode(int cpu) \
79 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
80 BUG_ON(policy_cpu == -1); \
81 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
86 lock_policy_rwsem(read, cpu);
87 lock_policy_rwsem(write, cpu);
89 #define unlock_policy_rwsem(mode, cpu) \
90 static void unlock_policy_rwsem_##mode(int cpu) \
92 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
93 BUG_ON(policy_cpu == -1); \
94 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
97 unlock_policy_rwsem(read, cpu);
98 unlock_policy_rwsem(write, cpu);
100 /* internal prototypes */
101 static int __cpufreq_governor(struct cpufreq_policy *policy,
103 static unsigned int __cpufreq_get(unsigned int cpu);
104 static void handle_update(struct work_struct *work);
107 * Two notifier lists: the "policy" list is involved in the
108 * validation process for a new CPU frequency policy; the
109 * "transition" list for kernel code that needs to handle
110 * changes to devices when the CPU clock speed changes.
111 * The mutex locks both lists.
113 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
114 static struct srcu_notifier_head cpufreq_transition_notifier_list;
116 static bool init_cpufreq_transition_notifier_list_called;
117 static int __init init_cpufreq_transition_notifier_list(void)
119 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
120 init_cpufreq_transition_notifier_list_called = true;
123 pure_initcall(init_cpufreq_transition_notifier_list);
125 static int off __read_mostly;
126 static int cpufreq_disabled(void)
130 void disable_cpufreq(void)
134 static LIST_HEAD(cpufreq_governor_list);
135 static DEFINE_MUTEX(cpufreq_governor_mutex);
137 bool have_governor_per_policy(void)
139 return cpufreq_driver->have_governor_per_policy;
141 EXPORT_SYMBOL_GPL(have_governor_per_policy);
143 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
145 if (have_governor_per_policy())
146 return &policy->kobj;
148 return cpufreq_global_kobject;
150 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
152 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
158 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
160 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
164 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
165 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
167 idle_time = cur_wall_time - busy_time;
169 *wall = cputime_to_usecs(cur_wall_time);
171 return cputime_to_usecs(idle_time);
174 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
176 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
178 if (idle_time == -1ULL)
179 return get_cpu_idle_time_jiffy(cpu, wall);
181 idle_time += get_cpu_iowait_time_us(cpu, wall);
185 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
187 static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
189 struct cpufreq_policy *data;
192 if (cpu >= nr_cpu_ids)
195 /* get the cpufreq driver */
196 read_lock_irqsave(&cpufreq_driver_lock, flags);
201 if (!try_module_get(cpufreq_driver->owner))
205 data = per_cpu(cpufreq_cpu_data, cpu);
208 goto err_out_put_module;
210 if (!sysfs && !kobject_get(&data->kobj))
211 goto err_out_put_module;
213 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
217 module_put(cpufreq_driver->owner);
219 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
224 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
226 if (cpufreq_disabled())
229 return __cpufreq_cpu_get(cpu, false);
231 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
233 static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
235 return __cpufreq_cpu_get(cpu, true);
238 static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
241 kobject_put(&data->kobj);
242 module_put(cpufreq_driver->owner);
245 void cpufreq_cpu_put(struct cpufreq_policy *data)
247 if (cpufreq_disabled())
250 __cpufreq_cpu_put(data, false);
252 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
254 static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
256 __cpufreq_cpu_put(data, true);
259 /*********************************************************************
260 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
261 *********************************************************************/
264 * adjust_jiffies - adjust the system "loops_per_jiffy"
266 * This function alters the system "loops_per_jiffy" for the clock
267 * speed change. Note that loops_per_jiffy cannot be updated on SMP
268 * systems as each CPU might be scaled differently. So, use the arch
269 * per-CPU loops_per_jiffy value wherever possible.
272 static unsigned long l_p_j_ref;
273 static unsigned int l_p_j_ref_freq;
275 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
277 if (ci->flags & CPUFREQ_CONST_LOOPS)
280 if (!l_p_j_ref_freq) {
281 l_p_j_ref = loops_per_jiffy;
282 l_p_j_ref_freq = ci->old;
283 pr_debug("saving %lu as reference value for loops_per_jiffy; "
284 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
286 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
287 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
288 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
290 pr_debug("scaling loops_per_jiffy to %lu "
291 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
295 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
301 static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
302 struct cpufreq_freqs *freqs, unsigned int state)
304 BUG_ON(irqs_disabled());
306 if (cpufreq_disabled())
309 freqs->flags = cpufreq_driver->flags;
310 pr_debug("notification %u of frequency transition to %u kHz\n",
315 case CPUFREQ_PRECHANGE:
316 if (WARN(policy->transition_ongoing ==
317 cpumask_weight(policy->cpus),
318 "In middle of another frequency transition\n"))
321 policy->transition_ongoing++;
323 /* detect if the driver reported a value as "old frequency"
324 * which is not equal to what the cpufreq core thinks is
327 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
328 if ((policy) && (policy->cpu == freqs->cpu) &&
329 (policy->cur) && (policy->cur != freqs->old)) {
330 pr_debug("Warning: CPU frequency is"
331 " %u, cpufreq assumed %u kHz.\n",
332 freqs->old, policy->cur);
333 freqs->old = policy->cur;
336 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
337 CPUFREQ_PRECHANGE, freqs);
338 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
341 case CPUFREQ_POSTCHANGE:
342 if (WARN(!policy->transition_ongoing,
343 "No frequency transition in progress\n"))
346 policy->transition_ongoing--;
348 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
349 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
350 (unsigned long)freqs->cpu);
351 trace_cpu_frequency(freqs->new, freqs->cpu);
352 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
353 CPUFREQ_POSTCHANGE, freqs);
354 if (likely(policy) && likely(policy->cpu == freqs->cpu))
355 policy->cur = freqs->new;
361 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
362 * on frequency transition.
364 * This function calls the transition notifiers and the "adjust_jiffies"
365 * function. It is called twice on all CPU frequency changes that have
368 void cpufreq_notify_transition(struct cpufreq_policy *policy,
369 struct cpufreq_freqs *freqs, unsigned int state)
371 for_each_cpu(freqs->cpu, policy->cpus)
372 __cpufreq_notify_transition(policy, freqs, state);
374 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
377 /*********************************************************************
379 *********************************************************************/
381 static struct cpufreq_governor *__find_governor(const char *str_governor)
383 struct cpufreq_governor *t;
385 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
386 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
393 * cpufreq_parse_governor - parse a governor string
395 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
396 struct cpufreq_governor **governor)
403 if (cpufreq_driver->setpolicy) {
404 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
405 *policy = CPUFREQ_POLICY_PERFORMANCE;
407 } else if (!strnicmp(str_governor, "powersave",
409 *policy = CPUFREQ_POLICY_POWERSAVE;
412 } else if (cpufreq_driver->target) {
413 struct cpufreq_governor *t;
415 mutex_lock(&cpufreq_governor_mutex);
417 t = __find_governor(str_governor);
422 mutex_unlock(&cpufreq_governor_mutex);
423 ret = request_module("cpufreq_%s", str_governor);
424 mutex_lock(&cpufreq_governor_mutex);
427 t = __find_governor(str_governor);
435 mutex_unlock(&cpufreq_governor_mutex);
442 * cpufreq_per_cpu_attr_read() / show_##file_name() -
443 * print out cpufreq information
445 * Write out information from cpufreq_driver->policy[cpu]; object must be
449 #define show_one(file_name, object) \
450 static ssize_t show_##file_name \
451 (struct cpufreq_policy *policy, char *buf) \
453 return sprintf(buf, "%u\n", policy->object); \
456 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
457 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
458 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
459 show_one(scaling_min_freq, min);
460 show_one(scaling_max_freq, max);
461 show_one(scaling_cur_freq, cur);
463 static int __cpufreq_set_policy(struct cpufreq_policy *data,
464 struct cpufreq_policy *policy);
467 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
469 #define store_one(file_name, object) \
470 static ssize_t store_##file_name \
471 (struct cpufreq_policy *policy, const char *buf, size_t count) \
474 struct cpufreq_policy new_policy; \
476 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
480 ret = sscanf(buf, "%u", &new_policy.object); \
484 ret = __cpufreq_set_policy(policy, &new_policy); \
485 policy->user_policy.object = policy->object; \
487 return ret ? ret : count; \
490 store_one(scaling_min_freq, min);
491 store_one(scaling_max_freq, max);
494 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
496 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
499 unsigned int cur_freq = __cpufreq_get(policy->cpu);
501 return sprintf(buf, "<unknown>");
502 return sprintf(buf, "%u\n", cur_freq);
506 * show_scaling_governor - show the current policy for the specified CPU
508 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
510 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
511 return sprintf(buf, "powersave\n");
512 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
513 return sprintf(buf, "performance\n");
514 else if (policy->governor)
515 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
516 policy->governor->name);
521 * store_scaling_governor - store policy for the specified CPU
523 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
524 const char *buf, size_t count)
527 char str_governor[16];
528 struct cpufreq_policy new_policy;
530 ret = cpufreq_get_policy(&new_policy, policy->cpu);
534 ret = sscanf(buf, "%15s", str_governor);
538 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
539 &new_policy.governor))
543 * Do not use cpufreq_set_policy here or the user_policy.max
544 * will be wrongly overridden
546 ret = __cpufreq_set_policy(policy, &new_policy);
548 policy->user_policy.policy = policy->policy;
549 policy->user_policy.governor = policy->governor;
558 * show_scaling_driver - show the cpufreq driver currently loaded
560 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
562 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
566 * show_scaling_available_governors - show the available CPUfreq governors
568 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
572 struct cpufreq_governor *t;
574 if (!cpufreq_driver->target) {
575 i += sprintf(buf, "performance powersave");
579 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
580 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
581 - (CPUFREQ_NAME_LEN + 2)))
583 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
586 i += sprintf(&buf[i], "\n");
590 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
595 for_each_cpu(cpu, mask) {
597 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
598 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
599 if (i >= (PAGE_SIZE - 5))
602 i += sprintf(&buf[i], "\n");
605 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
608 * show_related_cpus - show the CPUs affected by each transition even if
609 * hw coordination is in use
611 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
613 return cpufreq_show_cpus(policy->related_cpus, buf);
617 * show_affected_cpus - show the CPUs affected by each transition
619 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
621 return cpufreq_show_cpus(policy->cpus, buf);
624 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
625 const char *buf, size_t count)
627 unsigned int freq = 0;
630 if (!policy->governor || !policy->governor->store_setspeed)
633 ret = sscanf(buf, "%u", &freq);
637 policy->governor->store_setspeed(policy, freq);
642 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
644 if (!policy->governor || !policy->governor->show_setspeed)
645 return sprintf(buf, "<unsupported>\n");
647 return policy->governor->show_setspeed(policy, buf);
651 * show_bios_limit - show the current cpufreq HW/BIOS limitation
653 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
657 if (cpufreq_driver->bios_limit) {
658 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
660 return sprintf(buf, "%u\n", limit);
662 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
665 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
666 cpufreq_freq_attr_ro(cpuinfo_min_freq);
667 cpufreq_freq_attr_ro(cpuinfo_max_freq);
668 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
669 cpufreq_freq_attr_ro(scaling_available_governors);
670 cpufreq_freq_attr_ro(scaling_driver);
671 cpufreq_freq_attr_ro(scaling_cur_freq);
672 cpufreq_freq_attr_ro(bios_limit);
673 cpufreq_freq_attr_ro(related_cpus);
674 cpufreq_freq_attr_ro(affected_cpus);
675 cpufreq_freq_attr_rw(scaling_min_freq);
676 cpufreq_freq_attr_rw(scaling_max_freq);
677 cpufreq_freq_attr_rw(scaling_governor);
678 cpufreq_freq_attr_rw(scaling_setspeed);
680 static struct attribute *default_attrs[] = {
681 &cpuinfo_min_freq.attr,
682 &cpuinfo_max_freq.attr,
683 &cpuinfo_transition_latency.attr,
684 &scaling_min_freq.attr,
685 &scaling_max_freq.attr,
688 &scaling_governor.attr,
689 &scaling_driver.attr,
690 &scaling_available_governors.attr,
691 &scaling_setspeed.attr,
695 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
696 #define to_attr(a) container_of(a, struct freq_attr, attr)
698 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
700 struct cpufreq_policy *policy = to_policy(kobj);
701 struct freq_attr *fattr = to_attr(attr);
702 ssize_t ret = -EINVAL;
703 policy = cpufreq_cpu_get_sysfs(policy->cpu);
707 if (lock_policy_rwsem_read(policy->cpu) < 0)
711 ret = fattr->show(policy, buf);
715 unlock_policy_rwsem_read(policy->cpu);
717 cpufreq_cpu_put_sysfs(policy);
722 static ssize_t store(struct kobject *kobj, struct attribute *attr,
723 const char *buf, size_t count)
725 struct cpufreq_policy *policy = to_policy(kobj);
726 struct freq_attr *fattr = to_attr(attr);
727 ssize_t ret = -EINVAL;
728 policy = cpufreq_cpu_get_sysfs(policy->cpu);
732 if (lock_policy_rwsem_write(policy->cpu) < 0)
736 ret = fattr->store(policy, buf, count);
740 unlock_policy_rwsem_write(policy->cpu);
742 cpufreq_cpu_put_sysfs(policy);
747 static void cpufreq_sysfs_release(struct kobject *kobj)
749 struct cpufreq_policy *policy = to_policy(kobj);
750 pr_debug("last reference is dropped\n");
751 complete(&policy->kobj_unregister);
754 static const struct sysfs_ops sysfs_ops = {
759 static struct kobj_type ktype_cpufreq = {
760 .sysfs_ops = &sysfs_ops,
761 .default_attrs = default_attrs,
762 .release = cpufreq_sysfs_release,
765 struct kobject *cpufreq_global_kobject;
766 EXPORT_SYMBOL(cpufreq_global_kobject);
768 static int cpufreq_global_kobject_usage;
770 int cpufreq_get_global_kobject(void)
772 if (!cpufreq_global_kobject_usage++)
773 return kobject_add(cpufreq_global_kobject,
774 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
778 EXPORT_SYMBOL(cpufreq_get_global_kobject);
780 void cpufreq_put_global_kobject(void)
782 if (!--cpufreq_global_kobject_usage)
783 kobject_del(cpufreq_global_kobject);
785 EXPORT_SYMBOL(cpufreq_put_global_kobject);
787 int cpufreq_sysfs_create_file(const struct attribute *attr)
789 int ret = cpufreq_get_global_kobject();
792 ret = sysfs_create_file(cpufreq_global_kobject, attr);
794 cpufreq_put_global_kobject();
799 EXPORT_SYMBOL(cpufreq_sysfs_create_file);
801 void cpufreq_sysfs_remove_file(const struct attribute *attr)
803 sysfs_remove_file(cpufreq_global_kobject, attr);
804 cpufreq_put_global_kobject();
806 EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
808 /* symlink affected CPUs */
809 static int cpufreq_add_dev_symlink(unsigned int cpu,
810 struct cpufreq_policy *policy)
815 for_each_cpu(j, policy->cpus) {
816 struct cpufreq_policy *managed_policy;
817 struct device *cpu_dev;
822 pr_debug("CPU %u already managed, adding link\n", j);
823 managed_policy = cpufreq_cpu_get(cpu);
824 cpu_dev = get_cpu_device(j);
825 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
828 cpufreq_cpu_put(managed_policy);
835 static int cpufreq_add_dev_interface(unsigned int cpu,
836 struct cpufreq_policy *policy,
839 struct freq_attr **drv_attr;
842 /* prepare interface data */
843 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
844 &dev->kobj, "cpufreq");
848 /* set up files for this cpu device */
849 drv_attr = cpufreq_driver->attr;
850 while ((drv_attr) && (*drv_attr)) {
851 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
853 goto err_out_kobj_put;
856 if (cpufreq_driver->get) {
857 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
859 goto err_out_kobj_put;
861 if (cpufreq_driver->target) {
862 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
864 goto err_out_kobj_put;
866 if (cpufreq_driver->bios_limit) {
867 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
869 goto err_out_kobj_put;
872 ret = cpufreq_add_dev_symlink(cpu, policy);
874 goto err_out_kobj_put;
879 kobject_put(&policy->kobj);
880 wait_for_completion(&policy->kobj_unregister);
884 static void cpufreq_init_policy(struct cpufreq_policy *policy)
886 struct cpufreq_policy new_policy;
889 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
890 /* assure that the starting sequence is run in __cpufreq_set_policy */
891 policy->governor = NULL;
893 /* set default policy */
894 ret = __cpufreq_set_policy(policy, &new_policy);
895 policy->user_policy.policy = policy->policy;
896 policy->user_policy.governor = policy->governor;
899 pr_debug("setting policy failed\n");
900 if (cpufreq_driver->exit)
901 cpufreq_driver->exit(policy);
905 #ifdef CONFIG_HOTPLUG_CPU
906 static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
907 struct device *dev, bool frozen)
909 struct cpufreq_policy *policy;
910 int ret = 0, has_target = !!cpufreq_driver->target;
913 policy = cpufreq_cpu_get(sibling);
917 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
919 lock_policy_rwsem_write(sibling);
921 write_lock_irqsave(&cpufreq_driver_lock, flags);
923 cpumask_set_cpu(cpu, policy->cpus);
924 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
925 per_cpu(cpufreq_cpu_data, cpu) = policy;
926 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
928 unlock_policy_rwsem_write(sibling);
931 __cpufreq_governor(policy, CPUFREQ_GOV_START);
932 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
935 /* Don't touch sysfs links during light-weight init */
937 /* Drop the extra refcount that we took above */
938 cpufreq_cpu_put(policy);
942 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
944 cpufreq_cpu_put(policy);
950 static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
952 struct cpufreq_policy *policy;
955 write_lock_irqsave(&cpufreq_driver_lock, flags);
957 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
959 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
964 static struct cpufreq_policy *cpufreq_policy_alloc(void)
966 struct cpufreq_policy *policy;
968 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
972 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
973 goto err_free_policy;
975 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
976 goto err_free_cpumask;
981 free_cpumask_var(policy->cpus);
988 static void cpufreq_policy_free(struct cpufreq_policy *policy)
990 free_cpumask_var(policy->related_cpus);
991 free_cpumask_var(policy->cpus);
995 static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
998 unsigned int j, cpu = dev->id;
1000 struct cpufreq_policy *policy;
1001 unsigned long flags;
1002 #ifdef CONFIG_HOTPLUG_CPU
1003 struct cpufreq_governor *gov;
1007 if (cpu_is_offline(cpu))
1010 pr_debug("adding CPU %u\n", cpu);
1013 /* check whether a different CPU already registered this
1014 * CPU because it is in the same boat. */
1015 policy = cpufreq_cpu_get(cpu);
1016 if (unlikely(policy)) {
1017 cpufreq_cpu_put(policy);
1021 #ifdef CONFIG_HOTPLUG_CPU
1022 /* Check if this cpu was hot-unplugged earlier and has siblings */
1023 read_lock_irqsave(&cpufreq_driver_lock, flags);
1024 for_each_online_cpu(sibling) {
1025 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
1026 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
1027 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1028 return cpufreq_add_policy_cpu(cpu, sibling, dev,
1032 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1036 if (!try_module_get(cpufreq_driver->owner)) {
1042 /* Restore the saved policy when doing light-weight init */
1043 policy = cpufreq_policy_restore(cpu);
1045 policy = cpufreq_policy_alloc();
1051 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1052 cpumask_copy(policy->cpus, cpumask_of(cpu));
1054 /* Initially set CPU itself as the policy_cpu */
1055 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
1057 init_completion(&policy->kobj_unregister);
1058 INIT_WORK(&policy->update, handle_update);
1060 /* call driver. From then on the cpufreq must be able
1061 * to accept all calls to ->verify and ->setpolicy for this CPU
1063 ret = cpufreq_driver->init(policy);
1065 pr_debug("initialization failed\n");
1066 goto err_set_policy_cpu;
1069 /* related cpus should atleast have policy->cpus */
1070 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1073 * affected cpus must always be the one, which are online. We aren't
1074 * managing offline cpus here.
1076 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1078 policy->user_policy.min = policy->min;
1079 policy->user_policy.max = policy->max;
1081 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1082 CPUFREQ_START, policy);
1084 #ifdef CONFIG_HOTPLUG_CPU
1085 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1087 policy->governor = gov;
1088 pr_debug("Restoring governor %s for cpu %d\n",
1089 policy->governor->name, cpu);
1093 write_lock_irqsave(&cpufreq_driver_lock, flags);
1094 for_each_cpu(j, policy->cpus) {
1095 per_cpu(cpufreq_cpu_data, j) = policy;
1096 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
1098 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1101 ret = cpufreq_add_dev_interface(cpu, policy, dev);
1103 goto err_out_unregister;
1106 cpufreq_init_policy(policy);
1108 kobject_uevent(&policy->kobj, KOBJ_ADD);
1109 module_put(cpufreq_driver->owner);
1110 pr_debug("initialization complete\n");
1115 write_lock_irqsave(&cpufreq_driver_lock, flags);
1116 for_each_cpu(j, policy->cpus) {
1117 per_cpu(cpufreq_cpu_data, j) = NULL;
1119 per_cpu(cpufreq_policy_cpu, j) = -1;
1121 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1123 kobject_put(&policy->kobj);
1124 wait_for_completion(&policy->kobj_unregister);
1127 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1128 cpufreq_policy_free(policy);
1130 module_put(cpufreq_driver->owner);
1136 * cpufreq_add_dev - add a CPU device
1138 * Adds the cpufreq interface for a CPU device.
1140 * The Oracle says: try running cpufreq registration/unregistration concurrently
1141 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1142 * mess up, but more thorough testing is needed. - Mathieu
1144 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1146 return __cpufreq_add_dev(dev, sif, false);
1149 static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1153 policy->last_cpu = policy->cpu;
1156 for_each_cpu(j, policy->cpus)
1157 per_cpu(cpufreq_policy_cpu, j) = cpu;
1159 #ifdef CONFIG_CPU_FREQ_TABLE
1160 cpufreq_frequency_table_update_policy_cpu(policy);
1162 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1163 CPUFREQ_UPDATE_POLICY_CPU, policy);
1166 static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *data,
1167 unsigned int old_cpu, bool frozen)
1169 struct device *cpu_dev;
1170 unsigned long flags;
1173 /* first sibling now owns the new sysfs dir */
1174 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
1176 /* Don't touch sysfs files during light-weight tear-down */
1180 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1181 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1183 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1185 WARN_ON(lock_policy_rwsem_write(old_cpu));
1186 cpumask_set_cpu(old_cpu, data->cpus);
1188 write_lock_irqsave(&cpufreq_driver_lock, flags);
1189 per_cpu(cpufreq_cpu_data, old_cpu) = data;
1190 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1192 unlock_policy_rwsem_write(old_cpu);
1194 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1204 * __cpufreq_remove_dev - remove a CPU device
1206 * Removes the cpufreq interface for a CPU device.
1207 * Caller should already have policy_rwsem in write mode for this CPU.
1208 * This routine frees the rwsem before returning.
1210 static int __cpufreq_remove_dev(struct device *dev,
1211 struct subsys_interface *sif, bool frozen)
1213 unsigned int cpu = dev->id, cpus;
1215 unsigned long flags;
1216 struct cpufreq_policy *data;
1217 struct kobject *kobj;
1218 struct completion *cmp;
1220 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1222 write_lock_irqsave(&cpufreq_driver_lock, flags);
1224 data = per_cpu(cpufreq_cpu_data, cpu);
1225 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1227 /* Save the policy somewhere when doing a light-weight tear-down */
1229 per_cpu(cpufreq_cpu_data_fallback, cpu) = data;
1231 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1234 pr_debug("%s: No cpu_data found\n", __func__);
1238 if (cpufreq_driver->target)
1239 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1241 #ifdef CONFIG_HOTPLUG_CPU
1242 if (!cpufreq_driver->setpolicy)
1243 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1244 data->governor->name, CPUFREQ_NAME_LEN);
1247 WARN_ON(lock_policy_rwsem_write(cpu));
1248 cpus = cpumask_weight(data->cpus);
1251 cpumask_clear_cpu(cpu, data->cpus);
1252 unlock_policy_rwsem_write(cpu);
1254 if (cpu != data->cpu && !frozen) {
1255 sysfs_remove_link(&dev->kobj, "cpufreq");
1256 } else if (cpus > 1) {
1258 new_cpu = cpufreq_nominate_new_policy_cpu(data, cpu, frozen);
1260 WARN_ON(lock_policy_rwsem_write(cpu));
1261 update_policy_cpu(data, new_cpu);
1262 unlock_policy_rwsem_write(cpu);
1265 pr_debug("%s: policy Kobject moved to cpu: %d "
1266 "from: %d\n",__func__, new_cpu, cpu);
1271 /* If cpu is last user of policy, free policy */
1273 if (cpufreq_driver->target)
1274 __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
1277 lock_policy_rwsem_read(cpu);
1279 cmp = &data->kobj_unregister;
1280 unlock_policy_rwsem_read(cpu);
1284 * We need to make sure that the underlying kobj is
1285 * actually not referenced anymore by anybody before we
1286 * proceed with unloading.
1288 pr_debug("waiting for dropping of refcount\n");
1289 wait_for_completion(cmp);
1290 pr_debug("wait complete\n");
1294 * Perform the ->exit() even during light-weight tear-down,
1295 * since this is a core component, and is essential for the
1296 * subsequent light-weight ->init() to succeed.
1298 if (cpufreq_driver->exit)
1299 cpufreq_driver->exit(data);
1302 cpufreq_policy_free(data);
1306 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1307 cpufreq_cpu_put(data);
1310 if (cpufreq_driver->target) {
1311 __cpufreq_governor(data, CPUFREQ_GOV_START);
1312 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1316 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1320 static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1322 unsigned int cpu = dev->id;
1325 if (cpu_is_offline(cpu))
1328 retval = __cpufreq_remove_dev(dev, sif, false);
1332 static void handle_update(struct work_struct *work)
1334 struct cpufreq_policy *policy =
1335 container_of(work, struct cpufreq_policy, update);
1336 unsigned int cpu = policy->cpu;
1337 pr_debug("handle_update for cpu %u called\n", cpu);
1338 cpufreq_update_policy(cpu);
1342 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1345 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1346 * @new_freq: CPU frequency the CPU actually runs at
1348 * We adjust to current frequency first, and need to clean up later.
1349 * So either call to cpufreq_update_policy() or schedule handle_update()).
1351 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1352 unsigned int new_freq)
1354 struct cpufreq_policy *policy;
1355 struct cpufreq_freqs freqs;
1356 unsigned long flags;
1358 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1359 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1361 freqs.old = old_freq;
1362 freqs.new = new_freq;
1364 read_lock_irqsave(&cpufreq_driver_lock, flags);
1365 policy = per_cpu(cpufreq_cpu_data, cpu);
1366 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1368 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1369 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
1373 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1376 * This is the last known freq, without actually getting it from the driver.
1377 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1379 unsigned int cpufreq_quick_get(unsigned int cpu)
1381 struct cpufreq_policy *policy;
1382 unsigned int ret_freq = 0;
1384 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1385 return cpufreq_driver->get(cpu);
1387 policy = cpufreq_cpu_get(cpu);
1389 ret_freq = policy->cur;
1390 cpufreq_cpu_put(policy);
1395 EXPORT_SYMBOL(cpufreq_quick_get);
1398 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1401 * Just return the max possible frequency for a given CPU.
1403 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1405 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1406 unsigned int ret_freq = 0;
1409 ret_freq = policy->max;
1410 cpufreq_cpu_put(policy);
1415 EXPORT_SYMBOL(cpufreq_quick_get_max);
1417 static unsigned int __cpufreq_get(unsigned int cpu)
1419 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1420 unsigned int ret_freq = 0;
1422 if (!cpufreq_driver->get)
1425 ret_freq = cpufreq_driver->get(cpu);
1427 if (ret_freq && policy->cur &&
1428 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1429 /* verify no discrepancy between actual and
1430 saved value exists */
1431 if (unlikely(ret_freq != policy->cur)) {
1432 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1433 schedule_work(&policy->update);
1441 * cpufreq_get - get the current CPU frequency (in kHz)
1444 * Get the CPU current (static) CPU frequency
1446 unsigned int cpufreq_get(unsigned int cpu)
1448 unsigned int ret_freq = 0;
1449 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1454 if (unlikely(lock_policy_rwsem_read(cpu)))
1457 ret_freq = __cpufreq_get(cpu);
1459 unlock_policy_rwsem_read(cpu);
1462 cpufreq_cpu_put(policy);
1466 EXPORT_SYMBOL(cpufreq_get);
1468 static struct subsys_interface cpufreq_interface = {
1470 .subsys = &cpu_subsys,
1471 .add_dev = cpufreq_add_dev,
1472 .remove_dev = cpufreq_remove_dev,
1476 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1478 * This function is only executed for the boot processor. The other CPUs
1479 * have been put offline by means of CPU hotplug.
1481 static int cpufreq_bp_suspend(void)
1485 int cpu = smp_processor_id();
1486 struct cpufreq_policy *cpu_policy;
1488 pr_debug("suspending cpu %u\n", cpu);
1490 /* If there's no policy for the boot CPU, we have nothing to do. */
1491 cpu_policy = cpufreq_cpu_get(cpu);
1495 if (cpufreq_driver->suspend) {
1496 ret = cpufreq_driver->suspend(cpu_policy);
1498 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1499 "step on CPU %u\n", cpu_policy->cpu);
1502 cpufreq_cpu_put(cpu_policy);
1507 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1509 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1510 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1511 * restored. It will verify that the current freq is in sync with
1512 * what we believe it to be. This is a bit later than when it
1513 * should be, but nonethteless it's better than calling
1514 * cpufreq_driver->get() here which might re-enable interrupts...
1516 * This function is only executed for the boot CPU. The other CPUs have not
1517 * been turned on yet.
1519 static void cpufreq_bp_resume(void)
1523 int cpu = smp_processor_id();
1524 struct cpufreq_policy *cpu_policy;
1526 pr_debug("resuming cpu %u\n", cpu);
1528 /* If there's no policy for the boot CPU, we have nothing to do. */
1529 cpu_policy = cpufreq_cpu_get(cpu);
1533 if (cpufreq_driver->resume) {
1534 ret = cpufreq_driver->resume(cpu_policy);
1536 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1537 "step on CPU %u\n", cpu_policy->cpu);
1542 schedule_work(&cpu_policy->update);
1545 cpufreq_cpu_put(cpu_policy);
1548 static struct syscore_ops cpufreq_syscore_ops = {
1549 .suspend = cpufreq_bp_suspend,
1550 .resume = cpufreq_bp_resume,
1554 * cpufreq_get_current_driver - return current driver's name
1556 * Return the name string of the currently loaded cpufreq driver
1559 const char *cpufreq_get_current_driver(void)
1562 return cpufreq_driver->name;
1566 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1568 /*********************************************************************
1569 * NOTIFIER LISTS INTERFACE *
1570 *********************************************************************/
1573 * cpufreq_register_notifier - register a driver with cpufreq
1574 * @nb: notifier function to register
1575 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1577 * Add a driver to one of two lists: either a list of drivers that
1578 * are notified about clock rate changes (once before and once after
1579 * the transition), or a list of drivers that are notified about
1580 * changes in cpufreq policy.
1582 * This function may sleep, and has the same return conditions as
1583 * blocking_notifier_chain_register.
1585 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1589 if (cpufreq_disabled())
1592 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1595 case CPUFREQ_TRANSITION_NOTIFIER:
1596 ret = srcu_notifier_chain_register(
1597 &cpufreq_transition_notifier_list, nb);
1599 case CPUFREQ_POLICY_NOTIFIER:
1600 ret = blocking_notifier_chain_register(
1601 &cpufreq_policy_notifier_list, nb);
1609 EXPORT_SYMBOL(cpufreq_register_notifier);
1612 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1613 * @nb: notifier block to be unregistered
1614 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1616 * Remove a driver from the CPU frequency notifier list.
1618 * This function may sleep, and has the same return conditions as
1619 * blocking_notifier_chain_unregister.
1621 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1625 if (cpufreq_disabled())
1629 case CPUFREQ_TRANSITION_NOTIFIER:
1630 ret = srcu_notifier_chain_unregister(
1631 &cpufreq_transition_notifier_list, nb);
1633 case CPUFREQ_POLICY_NOTIFIER:
1634 ret = blocking_notifier_chain_unregister(
1635 &cpufreq_policy_notifier_list, nb);
1643 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1646 /*********************************************************************
1648 *********************************************************************/
1650 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1651 unsigned int target_freq,
1652 unsigned int relation)
1654 int retval = -EINVAL;
1655 unsigned int old_target_freq = target_freq;
1657 if (cpufreq_disabled())
1659 if (policy->transition_ongoing)
1662 /* Make sure that target_freq is within supported range */
1663 if (target_freq > policy->max)
1664 target_freq = policy->max;
1665 if (target_freq < policy->min)
1666 target_freq = policy->min;
1668 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1669 policy->cpu, target_freq, relation, old_target_freq);
1671 if (target_freq == policy->cur)
1674 if (cpufreq_driver->target)
1675 retval = cpufreq_driver->target(policy, target_freq, relation);
1679 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1681 int cpufreq_driver_target(struct cpufreq_policy *policy,
1682 unsigned int target_freq,
1683 unsigned int relation)
1687 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1690 ret = __cpufreq_driver_target(policy, target_freq, relation);
1692 unlock_policy_rwsem_write(policy->cpu);
1697 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1699 int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
1701 if (cpufreq_disabled())
1704 if (!cpufreq_driver->getavg)
1707 return cpufreq_driver->getavg(policy, cpu);
1709 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
1712 * when "event" is CPUFREQ_GOV_LIMITS
1715 static int __cpufreq_governor(struct cpufreq_policy *policy,
1720 /* Only must be defined when default governor is known to have latency
1721 restrictions, like e.g. conservative or ondemand.
1722 That this is the case is already ensured in Kconfig
1724 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1725 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1727 struct cpufreq_governor *gov = NULL;
1730 if (policy->governor->max_transition_latency &&
1731 policy->cpuinfo.transition_latency >
1732 policy->governor->max_transition_latency) {
1736 printk(KERN_WARNING "%s governor failed, too long"
1737 " transition latency of HW, fallback"
1738 " to %s governor\n",
1739 policy->governor->name,
1741 policy->governor = gov;
1745 if (!try_module_get(policy->governor->owner))
1748 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1749 policy->cpu, event);
1751 mutex_lock(&cpufreq_governor_lock);
1752 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1753 (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1754 mutex_unlock(&cpufreq_governor_lock);
1758 if (event == CPUFREQ_GOV_STOP)
1759 policy->governor_enabled = false;
1760 else if (event == CPUFREQ_GOV_START)
1761 policy->governor_enabled = true;
1763 mutex_unlock(&cpufreq_governor_lock);
1765 ret = policy->governor->governor(policy, event);
1768 if (event == CPUFREQ_GOV_POLICY_INIT)
1769 policy->governor->initialized++;
1770 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1771 policy->governor->initialized--;
1773 /* Restore original values */
1774 mutex_lock(&cpufreq_governor_lock);
1775 if (event == CPUFREQ_GOV_STOP)
1776 policy->governor_enabled = true;
1777 else if (event == CPUFREQ_GOV_START)
1778 policy->governor_enabled = false;
1779 mutex_unlock(&cpufreq_governor_lock);
1782 /* we keep one module reference alive for
1783 each CPU governed by this CPU */
1784 if ((event != CPUFREQ_GOV_START) || ret)
1785 module_put(policy->governor->owner);
1786 if ((event == CPUFREQ_GOV_STOP) && !ret)
1787 module_put(policy->governor->owner);
1792 int cpufreq_register_governor(struct cpufreq_governor *governor)
1799 if (cpufreq_disabled())
1802 mutex_lock(&cpufreq_governor_mutex);
1804 governor->initialized = 0;
1806 if (__find_governor(governor->name) == NULL) {
1808 list_add(&governor->governor_list, &cpufreq_governor_list);
1811 mutex_unlock(&cpufreq_governor_mutex);
1814 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1816 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1818 #ifdef CONFIG_HOTPLUG_CPU
1825 if (cpufreq_disabled())
1828 #ifdef CONFIG_HOTPLUG_CPU
1829 for_each_present_cpu(cpu) {
1830 if (cpu_online(cpu))
1832 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1833 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1837 mutex_lock(&cpufreq_governor_mutex);
1838 list_del(&governor->governor_list);
1839 mutex_unlock(&cpufreq_governor_mutex);
1842 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1845 /*********************************************************************
1846 * POLICY INTERFACE *
1847 *********************************************************************/
1850 * cpufreq_get_policy - get the current cpufreq_policy
1851 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1854 * Reads the current cpufreq policy.
1856 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1858 struct cpufreq_policy *cpu_policy;
1862 cpu_policy = cpufreq_cpu_get(cpu);
1866 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1868 cpufreq_cpu_put(cpu_policy);
1871 EXPORT_SYMBOL(cpufreq_get_policy);
1874 * data : current policy.
1875 * policy : policy to be set.
1877 static int __cpufreq_set_policy(struct cpufreq_policy *data,
1878 struct cpufreq_policy *policy)
1880 int ret = 0, failed = 1;
1882 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1883 policy->min, policy->max);
1885 memcpy(&policy->cpuinfo, &data->cpuinfo,
1886 sizeof(struct cpufreq_cpuinfo));
1888 if (policy->min > data->max || policy->max < data->min) {
1893 /* verify the cpu speed can be set within this limit */
1894 ret = cpufreq_driver->verify(policy);
1898 /* adjust if necessary - all reasons */
1899 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1900 CPUFREQ_ADJUST, policy);
1902 /* adjust if necessary - hardware incompatibility*/
1903 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1904 CPUFREQ_INCOMPATIBLE, policy);
1907 * verify the cpu speed can be set within this limit, which might be
1908 * different to the first one
1910 ret = cpufreq_driver->verify(policy);
1914 /* notification of the new policy */
1915 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1916 CPUFREQ_NOTIFY, policy);
1918 data->min = policy->min;
1919 data->max = policy->max;
1921 pr_debug("new min and max freqs are %u - %u kHz\n",
1922 data->min, data->max);
1924 if (cpufreq_driver->setpolicy) {
1925 data->policy = policy->policy;
1926 pr_debug("setting range\n");
1927 ret = cpufreq_driver->setpolicy(policy);
1929 if (policy->governor != data->governor) {
1930 /* save old, working values */
1931 struct cpufreq_governor *old_gov = data->governor;
1933 pr_debug("governor switch\n");
1935 /* end old governor */
1936 if (data->governor) {
1937 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1938 unlock_policy_rwsem_write(policy->cpu);
1939 __cpufreq_governor(data,
1940 CPUFREQ_GOV_POLICY_EXIT);
1941 lock_policy_rwsem_write(policy->cpu);
1944 /* start new governor */
1945 data->governor = policy->governor;
1946 if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
1947 if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1950 unlock_policy_rwsem_write(policy->cpu);
1951 __cpufreq_governor(data,
1952 CPUFREQ_GOV_POLICY_EXIT);
1953 lock_policy_rwsem_write(policy->cpu);
1958 /* new governor failed, so re-start old one */
1959 pr_debug("starting governor %s failed\n",
1960 data->governor->name);
1962 data->governor = old_gov;
1963 __cpufreq_governor(data,
1964 CPUFREQ_GOV_POLICY_INIT);
1965 __cpufreq_governor(data,
1971 /* might be a policy change, too, so fall through */
1973 pr_debug("governor: change or update limits\n");
1974 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1982 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1983 * @cpu: CPU which shall be re-evaluated
1985 * Useful for policy notifiers which have different necessities
1986 * at different times.
1988 int cpufreq_update_policy(unsigned int cpu)
1990 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1991 struct cpufreq_policy policy;
1999 if (unlikely(lock_policy_rwsem_write(cpu))) {
2004 pr_debug("updating policy for CPU %u\n", cpu);
2005 memcpy(&policy, data, sizeof(struct cpufreq_policy));
2006 policy.min = data->user_policy.min;
2007 policy.max = data->user_policy.max;
2008 policy.policy = data->user_policy.policy;
2009 policy.governor = data->user_policy.governor;
2012 * BIOS might change freq behind our back
2013 * -> ask driver for current freq and notify governors about a change
2015 if (cpufreq_driver->get) {
2016 policy.cur = cpufreq_driver->get(cpu);
2018 pr_debug("Driver did not initialize current freq");
2019 data->cur = policy.cur;
2021 if (data->cur != policy.cur && cpufreq_driver->target)
2022 cpufreq_out_of_sync(cpu, data->cur,
2027 ret = __cpufreq_set_policy(data, &policy);
2029 unlock_policy_rwsem_write(cpu);
2032 cpufreq_cpu_put(data);
2036 EXPORT_SYMBOL(cpufreq_update_policy);
2038 static int cpufreq_cpu_callback(struct notifier_block *nfb,
2039 unsigned long action, void *hcpu)
2041 unsigned int cpu = (unsigned long)hcpu;
2044 dev = get_cpu_device(cpu);
2048 case CPU_ONLINE_FROZEN:
2049 cpufreq_add_dev(dev, NULL);
2050 cpufreq_update_policy(cpu);
2052 case CPU_DOWN_PREPARE:
2053 case CPU_DOWN_PREPARE_FROZEN:
2054 __cpufreq_remove_dev(dev, NULL, false);
2056 case CPU_DOWN_FAILED:
2057 case CPU_DOWN_FAILED_FROZEN:
2058 cpufreq_add_dev(dev, NULL);
2065 static struct notifier_block __refdata cpufreq_cpu_notifier = {
2066 .notifier_call = cpufreq_cpu_callback,
2069 /*********************************************************************
2070 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2071 *********************************************************************/
2074 * cpufreq_register_driver - register a CPU Frequency driver
2075 * @driver_data: A struct cpufreq_driver containing the values#
2076 * submitted by the CPU Frequency driver.
2078 * Registers a CPU Frequency driver to this core code. This code
2079 * returns zero on success, -EBUSY when another driver got here first
2080 * (and isn't unregistered in the meantime).
2083 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2085 unsigned long flags;
2088 if (cpufreq_disabled())
2091 if (!driver_data || !driver_data->verify || !driver_data->init ||
2092 ((!driver_data->setpolicy) && (!driver_data->target)))
2095 pr_debug("trying to register driver %s\n", driver_data->name);
2097 if (driver_data->setpolicy)
2098 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2100 write_lock_irqsave(&cpufreq_driver_lock, flags);
2101 if (cpufreq_driver) {
2102 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2105 cpufreq_driver = driver_data;
2106 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2108 ret = subsys_interface_register(&cpufreq_interface);
2110 goto err_null_driver;
2112 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
2116 /* check for at least one working CPU */
2117 for (i = 0; i < nr_cpu_ids; i++)
2118 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
2123 /* if all ->init() calls failed, unregister */
2125 pr_debug("no CPU initialized for driver %s\n",
2131 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2132 pr_debug("driver %s up and running\n", driver_data->name);
2136 subsys_interface_unregister(&cpufreq_interface);
2138 write_lock_irqsave(&cpufreq_driver_lock, flags);
2139 cpufreq_driver = NULL;
2140 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2143 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2146 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2148 * Unregister the current CPUFreq driver. Only call this if you have
2149 * the right to do so, i.e. if you have succeeded in initialising before!
2150 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2151 * currently not initialised.
2153 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2155 unsigned long flags;
2157 if (!cpufreq_driver || (driver != cpufreq_driver))
2160 pr_debug("unregistering driver %s\n", driver->name);
2162 subsys_interface_unregister(&cpufreq_interface);
2163 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2165 write_lock_irqsave(&cpufreq_driver_lock, flags);
2166 cpufreq_driver = NULL;
2167 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2171 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2173 static int __init cpufreq_core_init(void)
2177 if (cpufreq_disabled())
2180 for_each_possible_cpu(cpu) {
2181 per_cpu(cpufreq_policy_cpu, cpu) = -1;
2182 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2185 cpufreq_global_kobject = kobject_create();
2186 BUG_ON(!cpufreq_global_kobject);
2187 register_syscore_ops(&cpufreq_syscore_ops);
2191 core_initcall(cpufreq_core_init);