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_RWLOCK(cpufreq_driver_lock);
48 static DEFINE_MUTEX(cpufreq_governor_lock);
50 #ifdef CONFIG_HOTPLUG_CPU
51 /* This one keeps track of the previously set governor of a removed CPU */
52 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
56 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
57 * all cpufreq/hotplug/workqueue/etc related lock issues.
59 * The rules for this semaphore:
60 * - Any routine that wants to read from the policy structure will
61 * do a down_read on this semaphore.
62 * - Any routine that will write to the policy structure and/or may take away
63 * the policy altogether (eg. CPU hotplug), will hold this lock in write
64 * mode before doing so.
67 * - Governor routines that can be called in cpufreq hotplug path should not
68 * take this sem as top level hotplug notifier handler takes this.
69 * - Lock should not be held across
70 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
72 static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
73 static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
75 #define lock_policy_rwsem(mode, cpu) \
76 static int lock_policy_rwsem_##mode(int cpu) \
78 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
79 BUG_ON(policy_cpu == -1); \
80 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
85 lock_policy_rwsem(read, cpu);
86 lock_policy_rwsem(write, cpu);
88 #define unlock_policy_rwsem(mode, cpu) \
89 static void unlock_policy_rwsem_##mode(int cpu) \
91 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
92 BUG_ON(policy_cpu == -1); \
93 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
96 unlock_policy_rwsem(read, cpu);
97 unlock_policy_rwsem(write, cpu);
99 /* internal prototypes */
100 static int __cpufreq_governor(struct cpufreq_policy *policy,
102 static unsigned int __cpufreq_get(unsigned int cpu);
103 static void handle_update(struct work_struct *work);
106 * Two notifier lists: the "policy" list is involved in the
107 * validation process for a new CPU frequency policy; the
108 * "transition" list for kernel code that needs to handle
109 * changes to devices when the CPU clock speed changes.
110 * The mutex locks both lists.
112 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
113 static struct srcu_notifier_head cpufreq_transition_notifier_list;
115 static bool init_cpufreq_transition_notifier_list_called;
116 static int __init init_cpufreq_transition_notifier_list(void)
118 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
119 init_cpufreq_transition_notifier_list_called = true;
122 pure_initcall(init_cpufreq_transition_notifier_list);
124 static int off __read_mostly;
125 static int cpufreq_disabled(void)
129 void disable_cpufreq(void)
133 static LIST_HEAD(cpufreq_governor_list);
134 static DEFINE_MUTEX(cpufreq_governor_mutex);
136 bool have_governor_per_policy(void)
138 return cpufreq_driver->have_governor_per_policy;
140 EXPORT_SYMBOL_GPL(have_governor_per_policy);
142 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
144 if (have_governor_per_policy())
145 return &policy->kobj;
147 return cpufreq_global_kobject;
149 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
151 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
157 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
159 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
164 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
166 idle_time = cur_wall_time - busy_time;
168 *wall = cputime_to_usecs(cur_wall_time);
170 return cputime_to_usecs(idle_time);
173 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
175 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
177 if (idle_time == -1ULL)
178 return get_cpu_idle_time_jiffy(cpu, wall);
180 idle_time += get_cpu_iowait_time_us(cpu, wall);
184 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
186 static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
188 struct cpufreq_policy *data;
191 if (cpu >= nr_cpu_ids)
194 /* get the cpufreq driver */
195 read_lock_irqsave(&cpufreq_driver_lock, flags);
200 if (!try_module_get(cpufreq_driver->owner))
204 data = per_cpu(cpufreq_cpu_data, cpu);
207 goto err_out_put_module;
209 if (!sysfs && !kobject_get(&data->kobj))
210 goto err_out_put_module;
212 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
216 module_put(cpufreq_driver->owner);
218 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
223 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
225 if (cpufreq_disabled())
228 return __cpufreq_cpu_get(cpu, false);
230 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
232 static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
234 return __cpufreq_cpu_get(cpu, true);
237 static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
240 kobject_put(&data->kobj);
241 module_put(cpufreq_driver->owner);
244 void cpufreq_cpu_put(struct cpufreq_policy *data)
246 if (cpufreq_disabled())
249 __cpufreq_cpu_put(data, false);
251 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
253 static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
255 __cpufreq_cpu_put(data, true);
258 /*********************************************************************
259 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
260 *********************************************************************/
263 * adjust_jiffies - adjust the system "loops_per_jiffy"
265 * This function alters the system "loops_per_jiffy" for the clock
266 * speed change. Note that loops_per_jiffy cannot be updated on SMP
267 * systems as each CPU might be scaled differently. So, use the arch
268 * per-CPU loops_per_jiffy value wherever possible.
271 static unsigned long l_p_j_ref;
272 static unsigned int l_p_j_ref_freq;
274 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
276 if (ci->flags & CPUFREQ_CONST_LOOPS)
279 if (!l_p_j_ref_freq) {
280 l_p_j_ref = loops_per_jiffy;
281 l_p_j_ref_freq = ci->old;
282 pr_debug("saving %lu as reference value for loops_per_jiffy; "
283 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
285 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
286 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
287 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
289 pr_debug("scaling loops_per_jiffy to %lu "
290 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
294 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
300 static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
301 struct cpufreq_freqs *freqs, unsigned int state)
303 BUG_ON(irqs_disabled());
305 if (cpufreq_disabled())
308 freqs->flags = cpufreq_driver->flags;
309 pr_debug("notification %u of frequency transition to %u kHz\n",
314 case CPUFREQ_PRECHANGE:
315 if (WARN(policy->transition_ongoing,
316 "In middle of another frequency transition\n"))
319 policy->transition_ongoing = true;
321 /* detect if the driver reported a value as "old frequency"
322 * which is not equal to what the cpufreq core thinks is
325 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
326 if ((policy) && (policy->cpu == freqs->cpu) &&
327 (policy->cur) && (policy->cur != freqs->old)) {
328 pr_debug("Warning: CPU frequency is"
329 " %u, cpufreq assumed %u kHz.\n",
330 freqs->old, policy->cur);
331 freqs->old = policy->cur;
334 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
335 CPUFREQ_PRECHANGE, freqs);
336 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
339 case CPUFREQ_POSTCHANGE:
340 if (WARN(!policy->transition_ongoing,
341 "No frequency transition in progress\n"))
344 policy->transition_ongoing = false;
346 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
347 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
348 (unsigned long)freqs->cpu);
349 trace_cpu_frequency(freqs->new, freqs->cpu);
350 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
351 CPUFREQ_POSTCHANGE, freqs);
352 if (likely(policy) && likely(policy->cpu == freqs->cpu))
353 policy->cur = freqs->new;
359 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
360 * on frequency transition.
362 * This function calls the transition notifiers and the "adjust_jiffies"
363 * function. It is called twice on all CPU frequency changes that have
366 void cpufreq_notify_transition(struct cpufreq_policy *policy,
367 struct cpufreq_freqs *freqs, unsigned int state)
369 for_each_cpu(freqs->cpu, policy->cpus)
370 __cpufreq_notify_transition(policy, freqs, state);
372 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
375 /*********************************************************************
377 *********************************************************************/
379 static struct cpufreq_governor *__find_governor(const char *str_governor)
381 struct cpufreq_governor *t;
383 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
384 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
391 * cpufreq_parse_governor - parse a governor string
393 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
394 struct cpufreq_governor **governor)
401 if (cpufreq_driver->setpolicy) {
402 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
403 *policy = CPUFREQ_POLICY_PERFORMANCE;
405 } else if (!strnicmp(str_governor, "powersave",
407 *policy = CPUFREQ_POLICY_POWERSAVE;
410 } else if (cpufreq_driver->target) {
411 struct cpufreq_governor *t;
413 mutex_lock(&cpufreq_governor_mutex);
415 t = __find_governor(str_governor);
420 mutex_unlock(&cpufreq_governor_mutex);
421 ret = request_module("cpufreq_%s", str_governor);
422 mutex_lock(&cpufreq_governor_mutex);
425 t = __find_governor(str_governor);
433 mutex_unlock(&cpufreq_governor_mutex);
440 * cpufreq_per_cpu_attr_read() / show_##file_name() -
441 * print out cpufreq information
443 * Write out information from cpufreq_driver->policy[cpu]; object must be
447 #define show_one(file_name, object) \
448 static ssize_t show_##file_name \
449 (struct cpufreq_policy *policy, char *buf) \
451 return sprintf(buf, "%u\n", policy->object); \
454 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
455 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
456 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
457 show_one(scaling_min_freq, min);
458 show_one(scaling_max_freq, max);
459 show_one(scaling_cur_freq, cur);
461 static int __cpufreq_set_policy(struct cpufreq_policy *data,
462 struct cpufreq_policy *policy);
465 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
467 #define store_one(file_name, object) \
468 static ssize_t store_##file_name \
469 (struct cpufreq_policy *policy, const char *buf, size_t count) \
472 struct cpufreq_policy new_policy; \
474 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
478 ret = sscanf(buf, "%u", &new_policy.object); \
482 ret = __cpufreq_set_policy(policy, &new_policy); \
483 policy->user_policy.object = policy->object; \
485 return ret ? ret : count; \
488 store_one(scaling_min_freq, min);
489 store_one(scaling_max_freq, max);
492 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
494 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
497 unsigned int cur_freq = __cpufreq_get(policy->cpu);
499 return sprintf(buf, "<unknown>");
500 return sprintf(buf, "%u\n", cur_freq);
504 * show_scaling_governor - show the current policy for the specified CPU
506 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
508 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
509 return sprintf(buf, "powersave\n");
510 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
511 return sprintf(buf, "performance\n");
512 else if (policy->governor)
513 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
514 policy->governor->name);
519 * store_scaling_governor - store policy for the specified CPU
521 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
522 const char *buf, size_t count)
525 char str_governor[16];
526 struct cpufreq_policy new_policy;
528 ret = cpufreq_get_policy(&new_policy, policy->cpu);
532 ret = sscanf(buf, "%15s", str_governor);
536 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
537 &new_policy.governor))
541 * Do not use cpufreq_set_policy here or the user_policy.max
542 * will be wrongly overridden
544 ret = __cpufreq_set_policy(policy, &new_policy);
546 policy->user_policy.policy = policy->policy;
547 policy->user_policy.governor = policy->governor;
556 * show_scaling_driver - show the cpufreq driver currently loaded
558 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
560 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
564 * show_scaling_available_governors - show the available CPUfreq governors
566 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
570 struct cpufreq_governor *t;
572 if (!cpufreq_driver->target) {
573 i += sprintf(buf, "performance powersave");
577 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
578 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
579 - (CPUFREQ_NAME_LEN + 2)))
581 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
584 i += sprintf(&buf[i], "\n");
588 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
593 for_each_cpu(cpu, mask) {
595 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
596 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
597 if (i >= (PAGE_SIZE - 5))
600 i += sprintf(&buf[i], "\n");
603 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
606 * show_related_cpus - show the CPUs affected by each transition even if
607 * hw coordination is in use
609 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
611 return cpufreq_show_cpus(policy->related_cpus, buf);
615 * show_affected_cpus - show the CPUs affected by each transition
617 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
619 return cpufreq_show_cpus(policy->cpus, buf);
622 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
623 const char *buf, size_t count)
625 unsigned int freq = 0;
628 if (!policy->governor || !policy->governor->store_setspeed)
631 ret = sscanf(buf, "%u", &freq);
635 policy->governor->store_setspeed(policy, freq);
640 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
642 if (!policy->governor || !policy->governor->show_setspeed)
643 return sprintf(buf, "<unsupported>\n");
645 return policy->governor->show_setspeed(policy, buf);
649 * show_bios_limit - show the current cpufreq HW/BIOS limitation
651 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
655 if (cpufreq_driver->bios_limit) {
656 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
658 return sprintf(buf, "%u\n", limit);
660 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
663 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
664 cpufreq_freq_attr_ro(cpuinfo_min_freq);
665 cpufreq_freq_attr_ro(cpuinfo_max_freq);
666 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
667 cpufreq_freq_attr_ro(scaling_available_governors);
668 cpufreq_freq_attr_ro(scaling_driver);
669 cpufreq_freq_attr_ro(scaling_cur_freq);
670 cpufreq_freq_attr_ro(bios_limit);
671 cpufreq_freq_attr_ro(related_cpus);
672 cpufreq_freq_attr_ro(affected_cpus);
673 cpufreq_freq_attr_rw(scaling_min_freq);
674 cpufreq_freq_attr_rw(scaling_max_freq);
675 cpufreq_freq_attr_rw(scaling_governor);
676 cpufreq_freq_attr_rw(scaling_setspeed);
678 static struct attribute *default_attrs[] = {
679 &cpuinfo_min_freq.attr,
680 &cpuinfo_max_freq.attr,
681 &cpuinfo_transition_latency.attr,
682 &scaling_min_freq.attr,
683 &scaling_max_freq.attr,
686 &scaling_governor.attr,
687 &scaling_driver.attr,
688 &scaling_available_governors.attr,
689 &scaling_setspeed.attr,
693 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
694 #define to_attr(a) container_of(a, struct freq_attr, attr)
696 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
698 struct cpufreq_policy *policy = to_policy(kobj);
699 struct freq_attr *fattr = to_attr(attr);
700 ssize_t ret = -EINVAL;
701 policy = cpufreq_cpu_get_sysfs(policy->cpu);
705 if (lock_policy_rwsem_read(policy->cpu) < 0)
709 ret = fattr->show(policy, buf);
713 unlock_policy_rwsem_read(policy->cpu);
715 cpufreq_cpu_put_sysfs(policy);
720 static ssize_t store(struct kobject *kobj, struct attribute *attr,
721 const char *buf, size_t count)
723 struct cpufreq_policy *policy = to_policy(kobj);
724 struct freq_attr *fattr = to_attr(attr);
725 ssize_t ret = -EINVAL;
726 policy = cpufreq_cpu_get_sysfs(policy->cpu);
730 if (lock_policy_rwsem_write(policy->cpu) < 0)
734 ret = fattr->store(policy, buf, count);
738 unlock_policy_rwsem_write(policy->cpu);
740 cpufreq_cpu_put_sysfs(policy);
745 static void cpufreq_sysfs_release(struct kobject *kobj)
747 struct cpufreq_policy *policy = to_policy(kobj);
748 pr_debug("last reference is dropped\n");
749 complete(&policy->kobj_unregister);
752 static const struct sysfs_ops sysfs_ops = {
757 static struct kobj_type ktype_cpufreq = {
758 .sysfs_ops = &sysfs_ops,
759 .default_attrs = default_attrs,
760 .release = cpufreq_sysfs_release,
763 struct kobject *cpufreq_global_kobject;
764 EXPORT_SYMBOL(cpufreq_global_kobject);
766 static int cpufreq_global_kobject_usage;
768 int cpufreq_get_global_kobject(void)
770 if (!cpufreq_global_kobject_usage++)
771 return kobject_add(cpufreq_global_kobject,
772 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
776 EXPORT_SYMBOL(cpufreq_get_global_kobject);
778 void cpufreq_put_global_kobject(void)
780 if (!--cpufreq_global_kobject_usage)
781 kobject_del(cpufreq_global_kobject);
783 EXPORT_SYMBOL(cpufreq_put_global_kobject);
785 int cpufreq_sysfs_create_file(const struct attribute *attr)
787 int ret = cpufreq_get_global_kobject();
790 ret = sysfs_create_file(cpufreq_global_kobject, attr);
792 cpufreq_put_global_kobject();
797 EXPORT_SYMBOL(cpufreq_sysfs_create_file);
799 void cpufreq_sysfs_remove_file(const struct attribute *attr)
801 sysfs_remove_file(cpufreq_global_kobject, attr);
802 cpufreq_put_global_kobject();
804 EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
806 /* symlink affected CPUs */
807 static int cpufreq_add_dev_symlink(unsigned int cpu,
808 struct cpufreq_policy *policy)
813 for_each_cpu(j, policy->cpus) {
814 struct cpufreq_policy *managed_policy;
815 struct device *cpu_dev;
820 pr_debug("CPU %u already managed, adding link\n", j);
821 managed_policy = cpufreq_cpu_get(cpu);
822 cpu_dev = get_cpu_device(j);
823 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
826 cpufreq_cpu_put(managed_policy);
833 static int cpufreq_add_dev_interface(unsigned int cpu,
834 struct cpufreq_policy *policy,
837 struct cpufreq_policy new_policy;
838 struct freq_attr **drv_attr;
843 /* prepare interface data */
844 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
845 &dev->kobj, "cpufreq");
849 /* set up files for this cpu device */
850 drv_attr = cpufreq_driver->attr;
851 while ((drv_attr) && (*drv_attr)) {
852 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
854 goto err_out_kobj_put;
857 if (cpufreq_driver->get) {
858 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
860 goto err_out_kobj_put;
862 if (cpufreq_driver->target) {
863 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
865 goto err_out_kobj_put;
867 if (cpufreq_driver->bios_limit) {
868 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
870 goto err_out_kobj_put;
873 write_lock_irqsave(&cpufreq_driver_lock, flags);
874 for_each_cpu(j, policy->cpus) {
875 per_cpu(cpufreq_cpu_data, j) = policy;
876 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
878 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
880 ret = cpufreq_add_dev_symlink(cpu, policy);
882 goto err_out_kobj_put;
884 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
885 /* assure that the starting sequence is run in __cpufreq_set_policy */
886 policy->governor = NULL;
888 /* set default policy */
889 ret = __cpufreq_set_policy(policy, &new_policy);
890 policy->user_policy.policy = policy->policy;
891 policy->user_policy.governor = policy->governor;
894 pr_debug("setting policy failed\n");
895 if (cpufreq_driver->exit)
896 cpufreq_driver->exit(policy);
901 kobject_put(&policy->kobj);
902 wait_for_completion(&policy->kobj_unregister);
906 #ifdef CONFIG_HOTPLUG_CPU
907 static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
910 struct cpufreq_policy *policy;
911 int ret = 0, has_target = !!cpufreq_driver->target;
914 policy = cpufreq_cpu_get(sibling);
918 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
920 lock_policy_rwsem_write(sibling);
922 write_lock_irqsave(&cpufreq_driver_lock, flags);
924 cpumask_set_cpu(cpu, policy->cpus);
925 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
926 per_cpu(cpufreq_cpu_data, cpu) = policy;
927 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
929 unlock_policy_rwsem_write(sibling);
932 __cpufreq_governor(policy, CPUFREQ_GOV_START);
933 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
936 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
938 cpufreq_cpu_put(policy);
947 * cpufreq_add_dev - add a CPU device
949 * Adds the cpufreq interface for a CPU device.
951 * The Oracle says: try running cpufreq registration/unregistration concurrently
952 * with with cpu hotplugging and all hell will break loose. Tried to clean this
953 * mess up, but more thorough testing is needed. - Mathieu
955 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
957 unsigned int j, cpu = dev->id;
959 struct cpufreq_policy *policy;
961 #ifdef CONFIG_HOTPLUG_CPU
962 struct cpufreq_governor *gov;
966 if (cpu_is_offline(cpu))
969 pr_debug("adding CPU %u\n", cpu);
972 /* check whether a different CPU already registered this
973 * CPU because it is in the same boat. */
974 policy = cpufreq_cpu_get(cpu);
975 if (unlikely(policy)) {
976 cpufreq_cpu_put(policy);
980 #ifdef CONFIG_HOTPLUG_CPU
981 /* Check if this cpu was hot-unplugged earlier and has siblings */
982 read_lock_irqsave(&cpufreq_driver_lock, flags);
983 for_each_online_cpu(sibling) {
984 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
985 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
986 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
987 return cpufreq_add_policy_cpu(cpu, sibling, dev);
990 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
994 if (!try_module_get(cpufreq_driver->owner)) {
999 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
1003 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1004 goto err_free_policy;
1006 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1007 goto err_free_cpumask;
1010 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1011 cpumask_copy(policy->cpus, cpumask_of(cpu));
1013 /* Initially set CPU itself as the policy_cpu */
1014 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
1016 init_completion(&policy->kobj_unregister);
1017 INIT_WORK(&policy->update, handle_update);
1019 /* call driver. From then on the cpufreq must be able
1020 * to accept all calls to ->verify and ->setpolicy for this CPU
1022 ret = cpufreq_driver->init(policy);
1024 pr_debug("initialization failed\n");
1025 goto err_set_policy_cpu;
1028 /* related cpus should atleast have policy->cpus */
1029 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1032 * affected cpus must always be the one, which are online. We aren't
1033 * managing offline cpus here.
1035 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1037 policy->user_policy.min = policy->min;
1038 policy->user_policy.max = policy->max;
1040 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1041 CPUFREQ_START, policy);
1043 #ifdef CONFIG_HOTPLUG_CPU
1044 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1046 policy->governor = gov;
1047 pr_debug("Restoring governor %s for cpu %d\n",
1048 policy->governor->name, cpu);
1052 ret = cpufreq_add_dev_interface(cpu, policy, dev);
1054 goto err_out_unregister;
1056 kobject_uevent(&policy->kobj, KOBJ_ADD);
1057 module_put(cpufreq_driver->owner);
1058 pr_debug("initialization complete\n");
1063 write_lock_irqsave(&cpufreq_driver_lock, flags);
1064 for_each_cpu(j, policy->cpus)
1065 per_cpu(cpufreq_cpu_data, j) = NULL;
1066 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1068 kobject_put(&policy->kobj);
1069 wait_for_completion(&policy->kobj_unregister);
1072 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1073 free_cpumask_var(policy->related_cpus);
1075 free_cpumask_var(policy->cpus);
1079 module_put(cpufreq_driver->owner);
1084 static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1088 policy->last_cpu = policy->cpu;
1091 for_each_cpu(j, policy->cpus)
1092 per_cpu(cpufreq_policy_cpu, j) = cpu;
1094 #ifdef CONFIG_CPU_FREQ_TABLE
1095 cpufreq_frequency_table_update_policy_cpu(policy);
1097 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1098 CPUFREQ_UPDATE_POLICY_CPU, policy);
1102 * __cpufreq_remove_dev - remove a CPU device
1104 * Removes the cpufreq interface for a CPU device.
1105 * Caller should already have policy_rwsem in write mode for this CPU.
1106 * This routine frees the rwsem before returning.
1108 static int __cpufreq_remove_dev(struct device *dev,
1109 struct subsys_interface *sif)
1111 unsigned int cpu = dev->id, ret, cpus;
1112 unsigned long flags;
1113 struct cpufreq_policy *data;
1114 struct kobject *kobj;
1115 struct completion *cmp;
1116 struct device *cpu_dev;
1118 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1120 write_lock_irqsave(&cpufreq_driver_lock, flags);
1122 data = per_cpu(cpufreq_cpu_data, cpu);
1123 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1125 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1128 pr_debug("%s: No cpu_data found\n", __func__);
1132 if (cpufreq_driver->target)
1133 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1135 #ifdef CONFIG_HOTPLUG_CPU
1136 if (!cpufreq_driver->setpolicy)
1137 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1138 data->governor->name, CPUFREQ_NAME_LEN);
1141 WARN_ON(lock_policy_rwsem_write(cpu));
1142 cpus = cpumask_weight(data->cpus);
1145 cpumask_clear_cpu(cpu, data->cpus);
1146 unlock_policy_rwsem_write(cpu);
1148 if (cpu != data->cpu) {
1149 sysfs_remove_link(&dev->kobj, "cpufreq");
1150 } else if (cpus > 1) {
1151 /* first sibling now owns the new sysfs dir */
1152 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
1153 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1154 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1156 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1158 WARN_ON(lock_policy_rwsem_write(cpu));
1159 cpumask_set_cpu(cpu, data->cpus);
1161 write_lock_irqsave(&cpufreq_driver_lock, flags);
1162 per_cpu(cpufreq_cpu_data, cpu) = data;
1163 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1165 unlock_policy_rwsem_write(cpu);
1167 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1172 WARN_ON(lock_policy_rwsem_write(cpu));
1173 update_policy_cpu(data, cpu_dev->id);
1174 unlock_policy_rwsem_write(cpu);
1175 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1176 __func__, cpu_dev->id, cpu);
1179 if ((cpus == 1) && (cpufreq_driver->target))
1180 __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
1182 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1183 cpufreq_cpu_put(data);
1185 /* If cpu is last user of policy, free policy */
1187 lock_policy_rwsem_read(cpu);
1189 cmp = &data->kobj_unregister;
1190 unlock_policy_rwsem_read(cpu);
1193 /* we need to make sure that the underlying kobj is actually
1194 * not referenced anymore by anybody before we proceed with
1197 pr_debug("waiting for dropping of refcount\n");
1198 wait_for_completion(cmp);
1199 pr_debug("wait complete\n");
1201 if (cpufreq_driver->exit)
1202 cpufreq_driver->exit(data);
1204 free_cpumask_var(data->related_cpus);
1205 free_cpumask_var(data->cpus);
1207 } else if (cpufreq_driver->target) {
1208 __cpufreq_governor(data, CPUFREQ_GOV_START);
1209 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1212 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1216 static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1218 unsigned int cpu = dev->id;
1221 if (cpu_is_offline(cpu))
1224 retval = __cpufreq_remove_dev(dev, sif);
1228 static void handle_update(struct work_struct *work)
1230 struct cpufreq_policy *policy =
1231 container_of(work, struct cpufreq_policy, update);
1232 unsigned int cpu = policy->cpu;
1233 pr_debug("handle_update for cpu %u called\n", cpu);
1234 cpufreq_update_policy(cpu);
1238 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1241 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1242 * @new_freq: CPU frequency the CPU actually runs at
1244 * We adjust to current frequency first, and need to clean up later.
1245 * So either call to cpufreq_update_policy() or schedule handle_update()).
1247 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1248 unsigned int new_freq)
1250 struct cpufreq_policy *policy;
1251 struct cpufreq_freqs freqs;
1252 unsigned long flags;
1254 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1255 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1257 freqs.old = old_freq;
1258 freqs.new = new_freq;
1260 read_lock_irqsave(&cpufreq_driver_lock, flags);
1261 policy = per_cpu(cpufreq_cpu_data, cpu);
1262 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1264 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1265 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
1269 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1272 * This is the last known freq, without actually getting it from the driver.
1273 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1275 unsigned int cpufreq_quick_get(unsigned int cpu)
1277 struct cpufreq_policy *policy;
1278 unsigned int ret_freq = 0;
1280 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1281 return cpufreq_driver->get(cpu);
1283 policy = cpufreq_cpu_get(cpu);
1285 ret_freq = policy->cur;
1286 cpufreq_cpu_put(policy);
1291 EXPORT_SYMBOL(cpufreq_quick_get);
1294 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1297 * Just return the max possible frequency for a given CPU.
1299 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1301 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1302 unsigned int ret_freq = 0;
1305 ret_freq = policy->max;
1306 cpufreq_cpu_put(policy);
1311 EXPORT_SYMBOL(cpufreq_quick_get_max);
1313 static unsigned int __cpufreq_get(unsigned int cpu)
1315 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1316 unsigned int ret_freq = 0;
1318 if (!cpufreq_driver->get)
1321 ret_freq = cpufreq_driver->get(cpu);
1323 if (ret_freq && policy->cur &&
1324 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1325 /* verify no discrepancy between actual and
1326 saved value exists */
1327 if (unlikely(ret_freq != policy->cur)) {
1328 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1329 schedule_work(&policy->update);
1337 * cpufreq_get - get the current CPU frequency (in kHz)
1340 * Get the CPU current (static) CPU frequency
1342 unsigned int cpufreq_get(unsigned int cpu)
1344 unsigned int ret_freq = 0;
1345 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1350 if (unlikely(lock_policy_rwsem_read(cpu)))
1353 ret_freq = __cpufreq_get(cpu);
1355 unlock_policy_rwsem_read(cpu);
1358 cpufreq_cpu_put(policy);
1362 EXPORT_SYMBOL(cpufreq_get);
1364 static struct subsys_interface cpufreq_interface = {
1366 .subsys = &cpu_subsys,
1367 .add_dev = cpufreq_add_dev,
1368 .remove_dev = cpufreq_remove_dev,
1372 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1374 * This function is only executed for the boot processor. The other CPUs
1375 * have been put offline by means of CPU hotplug.
1377 static int cpufreq_bp_suspend(void)
1381 int cpu = smp_processor_id();
1382 struct cpufreq_policy *cpu_policy;
1384 pr_debug("suspending cpu %u\n", cpu);
1386 /* If there's no policy for the boot CPU, we have nothing to do. */
1387 cpu_policy = cpufreq_cpu_get(cpu);
1391 if (cpufreq_driver->suspend) {
1392 ret = cpufreq_driver->suspend(cpu_policy);
1394 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1395 "step on CPU %u\n", cpu_policy->cpu);
1398 cpufreq_cpu_put(cpu_policy);
1403 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1405 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1406 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1407 * restored. It will verify that the current freq is in sync with
1408 * what we believe it to be. This is a bit later than when it
1409 * should be, but nonethteless it's better than calling
1410 * cpufreq_driver->get() here which might re-enable interrupts...
1412 * This function is only executed for the boot CPU. The other CPUs have not
1413 * been turned on yet.
1415 static void cpufreq_bp_resume(void)
1419 int cpu = smp_processor_id();
1420 struct cpufreq_policy *cpu_policy;
1422 pr_debug("resuming cpu %u\n", cpu);
1424 /* If there's no policy for the boot CPU, we have nothing to do. */
1425 cpu_policy = cpufreq_cpu_get(cpu);
1429 if (cpufreq_driver->resume) {
1430 ret = cpufreq_driver->resume(cpu_policy);
1432 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1433 "step on CPU %u\n", cpu_policy->cpu);
1438 schedule_work(&cpu_policy->update);
1441 cpufreq_cpu_put(cpu_policy);
1444 static struct syscore_ops cpufreq_syscore_ops = {
1445 .suspend = cpufreq_bp_suspend,
1446 .resume = cpufreq_bp_resume,
1450 * cpufreq_get_current_driver - return current driver's name
1452 * Return the name string of the currently loaded cpufreq driver
1455 const char *cpufreq_get_current_driver(void)
1458 return cpufreq_driver->name;
1462 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1464 /*********************************************************************
1465 * NOTIFIER LISTS INTERFACE *
1466 *********************************************************************/
1469 * cpufreq_register_notifier - register a driver with cpufreq
1470 * @nb: notifier function to register
1471 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1473 * Add a driver to one of two lists: either a list of drivers that
1474 * are notified about clock rate changes (once before and once after
1475 * the transition), or a list of drivers that are notified about
1476 * changes in cpufreq policy.
1478 * This function may sleep, and has the same return conditions as
1479 * blocking_notifier_chain_register.
1481 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1485 if (cpufreq_disabled())
1488 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1491 case CPUFREQ_TRANSITION_NOTIFIER:
1492 ret = srcu_notifier_chain_register(
1493 &cpufreq_transition_notifier_list, nb);
1495 case CPUFREQ_POLICY_NOTIFIER:
1496 ret = blocking_notifier_chain_register(
1497 &cpufreq_policy_notifier_list, nb);
1505 EXPORT_SYMBOL(cpufreq_register_notifier);
1508 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1509 * @nb: notifier block to be unregistered
1510 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1512 * Remove a driver from the CPU frequency notifier list.
1514 * This function may sleep, and has the same return conditions as
1515 * blocking_notifier_chain_unregister.
1517 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1521 if (cpufreq_disabled())
1525 case CPUFREQ_TRANSITION_NOTIFIER:
1526 ret = srcu_notifier_chain_unregister(
1527 &cpufreq_transition_notifier_list, nb);
1529 case CPUFREQ_POLICY_NOTIFIER:
1530 ret = blocking_notifier_chain_unregister(
1531 &cpufreq_policy_notifier_list, nb);
1539 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1542 /*********************************************************************
1544 *********************************************************************/
1546 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1547 unsigned int target_freq,
1548 unsigned int relation)
1550 int retval = -EINVAL;
1551 unsigned int old_target_freq = target_freq;
1553 if (cpufreq_disabled())
1555 if (policy->transition_ongoing)
1558 /* Make sure that target_freq is within supported range */
1559 if (target_freq > policy->max)
1560 target_freq = policy->max;
1561 if (target_freq < policy->min)
1562 target_freq = policy->min;
1564 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1565 policy->cpu, target_freq, relation, old_target_freq);
1567 if (target_freq == policy->cur)
1570 if (cpufreq_driver->target)
1571 retval = cpufreq_driver->target(policy, target_freq, relation);
1575 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1577 int cpufreq_driver_target(struct cpufreq_policy *policy,
1578 unsigned int target_freq,
1579 unsigned int relation)
1583 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1586 ret = __cpufreq_driver_target(policy, target_freq, relation);
1588 unlock_policy_rwsem_write(policy->cpu);
1593 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1595 int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
1597 if (cpufreq_disabled())
1600 if (!cpufreq_driver->getavg)
1603 return cpufreq_driver->getavg(policy, cpu);
1605 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
1608 * when "event" is CPUFREQ_GOV_LIMITS
1611 static int __cpufreq_governor(struct cpufreq_policy *policy,
1616 /* Only must be defined when default governor is known to have latency
1617 restrictions, like e.g. conservative or ondemand.
1618 That this is the case is already ensured in Kconfig
1620 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1621 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1623 struct cpufreq_governor *gov = NULL;
1626 if (policy->governor->max_transition_latency &&
1627 policy->cpuinfo.transition_latency >
1628 policy->governor->max_transition_latency) {
1632 printk(KERN_WARNING "%s governor failed, too long"
1633 " transition latency of HW, fallback"
1634 " to %s governor\n",
1635 policy->governor->name,
1637 policy->governor = gov;
1641 if (!try_module_get(policy->governor->owner))
1644 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1645 policy->cpu, event);
1647 mutex_lock(&cpufreq_governor_lock);
1648 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1649 (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1650 mutex_unlock(&cpufreq_governor_lock);
1654 if (event == CPUFREQ_GOV_STOP)
1655 policy->governor_enabled = false;
1656 else if (event == CPUFREQ_GOV_START)
1657 policy->governor_enabled = true;
1659 mutex_unlock(&cpufreq_governor_lock);
1661 ret = policy->governor->governor(policy, event);
1664 if (event == CPUFREQ_GOV_POLICY_INIT)
1665 policy->governor->initialized++;
1666 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1667 policy->governor->initialized--;
1669 /* Restore original values */
1670 mutex_lock(&cpufreq_governor_lock);
1671 if (event == CPUFREQ_GOV_STOP)
1672 policy->governor_enabled = true;
1673 else if (event == CPUFREQ_GOV_START)
1674 policy->governor_enabled = false;
1675 mutex_unlock(&cpufreq_governor_lock);
1678 /* we keep one module reference alive for
1679 each CPU governed by this CPU */
1680 if ((event != CPUFREQ_GOV_START) || ret)
1681 module_put(policy->governor->owner);
1682 if ((event == CPUFREQ_GOV_STOP) && !ret)
1683 module_put(policy->governor->owner);
1688 int cpufreq_register_governor(struct cpufreq_governor *governor)
1695 if (cpufreq_disabled())
1698 mutex_lock(&cpufreq_governor_mutex);
1700 governor->initialized = 0;
1702 if (__find_governor(governor->name) == NULL) {
1704 list_add(&governor->governor_list, &cpufreq_governor_list);
1707 mutex_unlock(&cpufreq_governor_mutex);
1710 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1712 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1714 #ifdef CONFIG_HOTPLUG_CPU
1721 if (cpufreq_disabled())
1724 #ifdef CONFIG_HOTPLUG_CPU
1725 for_each_present_cpu(cpu) {
1726 if (cpu_online(cpu))
1728 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1729 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1733 mutex_lock(&cpufreq_governor_mutex);
1734 list_del(&governor->governor_list);
1735 mutex_unlock(&cpufreq_governor_mutex);
1738 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1741 /*********************************************************************
1742 * POLICY INTERFACE *
1743 *********************************************************************/
1746 * cpufreq_get_policy - get the current cpufreq_policy
1747 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1750 * Reads the current cpufreq policy.
1752 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1754 struct cpufreq_policy *cpu_policy;
1758 cpu_policy = cpufreq_cpu_get(cpu);
1762 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1764 cpufreq_cpu_put(cpu_policy);
1767 EXPORT_SYMBOL(cpufreq_get_policy);
1770 * data : current policy.
1771 * policy : policy to be set.
1773 static int __cpufreq_set_policy(struct cpufreq_policy *data,
1774 struct cpufreq_policy *policy)
1776 int ret = 0, failed = 1;
1778 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1779 policy->min, policy->max);
1781 memcpy(&policy->cpuinfo, &data->cpuinfo,
1782 sizeof(struct cpufreq_cpuinfo));
1784 if (policy->min > data->max || policy->max < data->min) {
1789 /* verify the cpu speed can be set within this limit */
1790 ret = cpufreq_driver->verify(policy);
1794 /* adjust if necessary - all reasons */
1795 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1796 CPUFREQ_ADJUST, policy);
1798 /* adjust if necessary - hardware incompatibility*/
1799 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1800 CPUFREQ_INCOMPATIBLE, policy);
1803 * verify the cpu speed can be set within this limit, which might be
1804 * different to the first one
1806 ret = cpufreq_driver->verify(policy);
1810 /* notification of the new policy */
1811 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1812 CPUFREQ_NOTIFY, policy);
1814 data->min = policy->min;
1815 data->max = policy->max;
1817 pr_debug("new min and max freqs are %u - %u kHz\n",
1818 data->min, data->max);
1820 if (cpufreq_driver->setpolicy) {
1821 data->policy = policy->policy;
1822 pr_debug("setting range\n");
1823 ret = cpufreq_driver->setpolicy(policy);
1825 if (policy->governor != data->governor) {
1826 /* save old, working values */
1827 struct cpufreq_governor *old_gov = data->governor;
1829 pr_debug("governor switch\n");
1831 /* end old governor */
1832 if (data->governor) {
1833 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1834 unlock_policy_rwsem_write(policy->cpu);
1835 __cpufreq_governor(data,
1836 CPUFREQ_GOV_POLICY_EXIT);
1837 lock_policy_rwsem_write(policy->cpu);
1840 /* start new governor */
1841 data->governor = policy->governor;
1842 if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
1843 if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1846 unlock_policy_rwsem_write(policy->cpu);
1847 __cpufreq_governor(data,
1848 CPUFREQ_GOV_POLICY_EXIT);
1849 lock_policy_rwsem_write(policy->cpu);
1854 /* new governor failed, so re-start old one */
1855 pr_debug("starting governor %s failed\n",
1856 data->governor->name);
1858 data->governor = old_gov;
1859 __cpufreq_governor(data,
1860 CPUFREQ_GOV_POLICY_INIT);
1861 __cpufreq_governor(data,
1867 /* might be a policy change, too, so fall through */
1869 pr_debug("governor: change or update limits\n");
1870 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1878 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1879 * @cpu: CPU which shall be re-evaluated
1881 * Useful for policy notifiers which have different necessities
1882 * at different times.
1884 int cpufreq_update_policy(unsigned int cpu)
1886 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1887 struct cpufreq_policy policy;
1895 if (unlikely(lock_policy_rwsem_write(cpu))) {
1900 pr_debug("updating policy for CPU %u\n", cpu);
1901 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1902 policy.min = data->user_policy.min;
1903 policy.max = data->user_policy.max;
1904 policy.policy = data->user_policy.policy;
1905 policy.governor = data->user_policy.governor;
1908 * BIOS might change freq behind our back
1909 * -> ask driver for current freq and notify governors about a change
1911 if (cpufreq_driver->get) {
1912 policy.cur = cpufreq_driver->get(cpu);
1914 pr_debug("Driver did not initialize current freq");
1915 data->cur = policy.cur;
1917 if (data->cur != policy.cur && cpufreq_driver->target)
1918 cpufreq_out_of_sync(cpu, data->cur,
1923 ret = __cpufreq_set_policy(data, &policy);
1925 unlock_policy_rwsem_write(cpu);
1928 cpufreq_cpu_put(data);
1932 EXPORT_SYMBOL(cpufreq_update_policy);
1934 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1935 unsigned long action, void *hcpu)
1937 unsigned int cpu = (unsigned long)hcpu;
1940 dev = get_cpu_device(cpu);
1944 cpufreq_add_dev(dev, NULL);
1946 case CPU_DOWN_PREPARE:
1947 case CPU_UP_CANCELED_FROZEN:
1948 __cpufreq_remove_dev(dev, NULL);
1950 case CPU_DOWN_FAILED:
1951 cpufreq_add_dev(dev, NULL);
1958 static struct notifier_block __refdata cpufreq_cpu_notifier = {
1959 .notifier_call = cpufreq_cpu_callback,
1962 /*********************************************************************
1963 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1964 *********************************************************************/
1967 * cpufreq_register_driver - register a CPU Frequency driver
1968 * @driver_data: A struct cpufreq_driver containing the values#
1969 * submitted by the CPU Frequency driver.
1971 * Registers a CPU Frequency driver to this core code. This code
1972 * returns zero on success, -EBUSY when another driver got here first
1973 * (and isn't unregistered in the meantime).
1976 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1978 unsigned long flags;
1981 if (cpufreq_disabled())
1984 if (!driver_data || !driver_data->verify || !driver_data->init ||
1985 ((!driver_data->setpolicy) && (!driver_data->target)))
1988 pr_debug("trying to register driver %s\n", driver_data->name);
1990 if (driver_data->setpolicy)
1991 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1993 write_lock_irqsave(&cpufreq_driver_lock, flags);
1994 if (cpufreq_driver) {
1995 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1998 cpufreq_driver = driver_data;
1999 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2001 ret = subsys_interface_register(&cpufreq_interface);
2003 goto err_null_driver;
2005 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
2009 /* check for at least one working CPU */
2010 for (i = 0; i < nr_cpu_ids; i++)
2011 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
2016 /* if all ->init() calls failed, unregister */
2018 pr_debug("no CPU initialized for driver %s\n",
2024 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2025 pr_debug("driver %s up and running\n", driver_data->name);
2029 subsys_interface_unregister(&cpufreq_interface);
2031 write_lock_irqsave(&cpufreq_driver_lock, flags);
2032 cpufreq_driver = NULL;
2033 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2036 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2039 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2041 * Unregister the current CPUFreq driver. Only call this if you have
2042 * the right to do so, i.e. if you have succeeded in initialising before!
2043 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2044 * currently not initialised.
2046 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2048 unsigned long flags;
2050 if (!cpufreq_driver || (driver != cpufreq_driver))
2053 pr_debug("unregistering driver %s\n", driver->name);
2055 subsys_interface_unregister(&cpufreq_interface);
2056 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2058 write_lock_irqsave(&cpufreq_driver_lock, flags);
2059 cpufreq_driver = NULL;
2060 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2064 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2066 static int __init cpufreq_core_init(void)
2070 if (cpufreq_disabled())
2073 for_each_possible_cpu(cpu) {
2074 per_cpu(cpufreq_policy_cpu, cpu) = -1;
2075 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2078 cpufreq_global_kobject = kobject_create();
2079 BUG_ON(!cpufreq_global_kobject);
2080 register_syscore_ops(&cpufreq_syscore_ops);
2084 core_initcall(cpufreq_core_init);