2 * linux/drivers/cpufreq/cpufreq.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
8 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9 * Added handling for CPU hotplug
10 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/cpu.h>
21 #include <linux/cpufreq.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/init.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/slab.h>
29 #include <linux/syscore_ops.h>
30 #include <linux/tick.h>
31 #include <trace/events/power.h>
34 * The "cpufreq driver" - the arch- or hardware-dependent low
35 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
38 static struct cpufreq_driver *cpufreq_driver;
39 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
40 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
41 static DEFINE_RWLOCK(cpufreq_driver_lock);
42 static DEFINE_MUTEX(cpufreq_governor_lock);
43 static LIST_HEAD(cpufreq_policy_list);
45 #ifdef CONFIG_HOTPLUG_CPU
46 /* This one keeps track of the previously set governor of a removed CPU */
47 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
50 static inline bool has_target(void)
52 return cpufreq_driver->target_index || cpufreq_driver->target;
56 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
59 static DECLARE_RWSEM(cpufreq_rwsem);
61 /* internal prototypes */
62 static int __cpufreq_governor(struct cpufreq_policy *policy,
64 static unsigned int __cpufreq_get(unsigned int cpu);
65 static void handle_update(struct work_struct *work);
68 * Two notifier lists: the "policy" list is involved in the
69 * validation process for a new CPU frequency policy; the
70 * "transition" list for kernel code that needs to handle
71 * changes to devices when the CPU clock speed changes.
72 * The mutex locks both lists.
74 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
75 static struct srcu_notifier_head cpufreq_transition_notifier_list;
77 static bool init_cpufreq_transition_notifier_list_called;
78 static int __init init_cpufreq_transition_notifier_list(void)
80 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
81 init_cpufreq_transition_notifier_list_called = true;
84 pure_initcall(init_cpufreq_transition_notifier_list);
86 static int off __read_mostly;
87 static int cpufreq_disabled(void)
91 void disable_cpufreq(void)
95 static LIST_HEAD(cpufreq_governor_list);
96 static DEFINE_MUTEX(cpufreq_governor_mutex);
98 bool have_governor_per_policy(void)
100 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
102 EXPORT_SYMBOL_GPL(have_governor_per_policy);
104 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
106 if (have_governor_per_policy())
107 return &policy->kobj;
109 return cpufreq_global_kobject;
111 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
113 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
119 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
121 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
122 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
123 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
124 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
125 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
126 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
128 idle_time = cur_wall_time - busy_time;
130 *wall = cputime_to_usecs(cur_wall_time);
132 return cputime_to_usecs(idle_time);
135 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
137 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
139 if (idle_time == -1ULL)
140 return get_cpu_idle_time_jiffy(cpu, wall);
142 idle_time += get_cpu_iowait_time_us(cpu, wall);
146 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
149 * This is a generic cpufreq init() routine which can be used by cpufreq
150 * drivers of SMP systems. It will do following:
151 * - validate & show freq table passed
152 * - set policies transition latency
153 * - policy->cpus with all possible CPUs
155 int cpufreq_generic_init(struct cpufreq_policy *policy,
156 struct cpufreq_frequency_table *table,
157 unsigned int transition_latency)
161 ret = cpufreq_table_validate_and_show(policy, table);
163 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
167 policy->cpuinfo.transition_latency = transition_latency;
170 * The driver only supports the SMP configuartion where all processors
171 * share the clock and voltage and clock.
173 cpumask_setall(policy->cpus);
177 EXPORT_SYMBOL_GPL(cpufreq_generic_init);
179 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
181 struct cpufreq_policy *policy = NULL;
184 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
187 if (!down_read_trylock(&cpufreq_rwsem))
190 /* get the cpufreq driver */
191 read_lock_irqsave(&cpufreq_driver_lock, flags);
193 if (cpufreq_driver) {
195 policy = per_cpu(cpufreq_cpu_data, cpu);
197 kobject_get(&policy->kobj);
200 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
203 up_read(&cpufreq_rwsem);
207 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
209 void cpufreq_cpu_put(struct cpufreq_policy *policy)
211 if (cpufreq_disabled())
214 kobject_put(&policy->kobj);
215 up_read(&cpufreq_rwsem);
217 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
219 /*********************************************************************
220 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
221 *********************************************************************/
224 * adjust_jiffies - adjust the system "loops_per_jiffy"
226 * This function alters the system "loops_per_jiffy" for the clock
227 * speed change. Note that loops_per_jiffy cannot be updated on SMP
228 * systems as each CPU might be scaled differently. So, use the arch
229 * per-CPU loops_per_jiffy value wherever possible.
232 static unsigned long l_p_j_ref;
233 static unsigned int l_p_j_ref_freq;
235 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
237 if (ci->flags & CPUFREQ_CONST_LOOPS)
240 if (!l_p_j_ref_freq) {
241 l_p_j_ref = loops_per_jiffy;
242 l_p_j_ref_freq = ci->old;
243 pr_debug("saving %lu as reference value for loops_per_jiffy; "
244 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
246 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
247 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
248 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
250 pr_debug("scaling loops_per_jiffy to %lu "
251 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
255 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
261 static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
262 struct cpufreq_freqs *freqs, unsigned int state)
264 BUG_ON(irqs_disabled());
266 if (cpufreq_disabled())
269 freqs->flags = cpufreq_driver->flags;
270 pr_debug("notification %u of frequency transition to %u kHz\n",
275 case CPUFREQ_PRECHANGE:
276 /* detect if the driver reported a value as "old frequency"
277 * which is not equal to what the cpufreq core thinks is
280 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
281 if ((policy) && (policy->cpu == freqs->cpu) &&
282 (policy->cur) && (policy->cur != freqs->old)) {
283 pr_debug("Warning: CPU frequency is"
284 " %u, cpufreq assumed %u kHz.\n",
285 freqs->old, policy->cur);
286 freqs->old = policy->cur;
289 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
290 CPUFREQ_PRECHANGE, freqs);
291 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
294 case CPUFREQ_POSTCHANGE:
295 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
296 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
297 (unsigned long)freqs->cpu);
298 trace_cpu_frequency(freqs->new, freqs->cpu);
299 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
300 CPUFREQ_POSTCHANGE, freqs);
301 if (likely(policy) && likely(policy->cpu == freqs->cpu))
302 policy->cur = freqs->new;
308 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
309 * on frequency transition.
311 * This function calls the transition notifiers and the "adjust_jiffies"
312 * function. It is called twice on all CPU frequency changes that have
315 void cpufreq_notify_transition(struct cpufreq_policy *policy,
316 struct cpufreq_freqs *freqs, unsigned int state)
318 for_each_cpu(freqs->cpu, policy->cpus)
319 __cpufreq_notify_transition(policy, freqs, state);
321 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
324 /*********************************************************************
326 *********************************************************************/
328 static struct cpufreq_governor *__find_governor(const char *str_governor)
330 struct cpufreq_governor *t;
332 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
333 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
340 * cpufreq_parse_governor - parse a governor string
342 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
343 struct cpufreq_governor **governor)
350 if (cpufreq_driver->setpolicy) {
351 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
352 *policy = CPUFREQ_POLICY_PERFORMANCE;
354 } else if (!strnicmp(str_governor, "powersave",
356 *policy = CPUFREQ_POLICY_POWERSAVE;
359 } else if (has_target()) {
360 struct cpufreq_governor *t;
362 mutex_lock(&cpufreq_governor_mutex);
364 t = __find_governor(str_governor);
369 mutex_unlock(&cpufreq_governor_mutex);
370 ret = request_module("cpufreq_%s", str_governor);
371 mutex_lock(&cpufreq_governor_mutex);
374 t = __find_governor(str_governor);
382 mutex_unlock(&cpufreq_governor_mutex);
389 * cpufreq_per_cpu_attr_read() / show_##file_name() -
390 * print out cpufreq information
392 * Write out information from cpufreq_driver->policy[cpu]; object must be
396 #define show_one(file_name, object) \
397 static ssize_t show_##file_name \
398 (struct cpufreq_policy *policy, char *buf) \
400 return sprintf(buf, "%u\n", policy->object); \
403 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
404 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
405 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
406 show_one(scaling_min_freq, min);
407 show_one(scaling_max_freq, max);
408 show_one(scaling_cur_freq, cur);
410 static int cpufreq_set_policy(struct cpufreq_policy *policy,
411 struct cpufreq_policy *new_policy);
414 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
416 #define store_one(file_name, object) \
417 static ssize_t store_##file_name \
418 (struct cpufreq_policy *policy, const char *buf, size_t count) \
421 struct cpufreq_policy new_policy; \
423 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
427 ret = sscanf(buf, "%u", &new_policy.object); \
431 ret = cpufreq_set_policy(policy, &new_policy); \
432 policy->user_policy.object = policy->object; \
434 return ret ? ret : count; \
437 store_one(scaling_min_freq, min);
438 store_one(scaling_max_freq, max);
441 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
443 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
446 unsigned int cur_freq = __cpufreq_get(policy->cpu);
448 return sprintf(buf, "<unknown>");
449 return sprintf(buf, "%u\n", cur_freq);
453 * show_scaling_governor - show the current policy for the specified CPU
455 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
457 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
458 return sprintf(buf, "powersave\n");
459 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
460 return sprintf(buf, "performance\n");
461 else if (policy->governor)
462 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
463 policy->governor->name);
468 * store_scaling_governor - store policy for the specified CPU
470 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
471 const char *buf, size_t count)
474 char str_governor[16];
475 struct cpufreq_policy new_policy;
477 ret = cpufreq_get_policy(&new_policy, policy->cpu);
481 ret = sscanf(buf, "%15s", str_governor);
485 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
486 &new_policy.governor))
489 ret = cpufreq_set_policy(policy, &new_policy);
491 policy->user_policy.policy = policy->policy;
492 policy->user_policy.governor = policy->governor;
501 * show_scaling_driver - show the cpufreq driver currently loaded
503 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
505 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
509 * show_scaling_available_governors - show the available CPUfreq governors
511 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
515 struct cpufreq_governor *t;
518 i += sprintf(buf, "performance powersave");
522 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
523 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
524 - (CPUFREQ_NAME_LEN + 2)))
526 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
529 i += sprintf(&buf[i], "\n");
533 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
538 for_each_cpu(cpu, mask) {
540 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
541 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
542 if (i >= (PAGE_SIZE - 5))
545 i += sprintf(&buf[i], "\n");
548 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
551 * show_related_cpus - show the CPUs affected by each transition even if
552 * hw coordination is in use
554 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
556 return cpufreq_show_cpus(policy->related_cpus, buf);
560 * show_affected_cpus - show the CPUs affected by each transition
562 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
564 return cpufreq_show_cpus(policy->cpus, buf);
567 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
568 const char *buf, size_t count)
570 unsigned int freq = 0;
573 if (!policy->governor || !policy->governor->store_setspeed)
576 ret = sscanf(buf, "%u", &freq);
580 policy->governor->store_setspeed(policy, freq);
585 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
587 if (!policy->governor || !policy->governor->show_setspeed)
588 return sprintf(buf, "<unsupported>\n");
590 return policy->governor->show_setspeed(policy, buf);
594 * show_bios_limit - show the current cpufreq HW/BIOS limitation
596 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
600 if (cpufreq_driver->bios_limit) {
601 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
603 return sprintf(buf, "%u\n", limit);
605 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
608 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
609 cpufreq_freq_attr_ro(cpuinfo_min_freq);
610 cpufreq_freq_attr_ro(cpuinfo_max_freq);
611 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
612 cpufreq_freq_attr_ro(scaling_available_governors);
613 cpufreq_freq_attr_ro(scaling_driver);
614 cpufreq_freq_attr_ro(scaling_cur_freq);
615 cpufreq_freq_attr_ro(bios_limit);
616 cpufreq_freq_attr_ro(related_cpus);
617 cpufreq_freq_attr_ro(affected_cpus);
618 cpufreq_freq_attr_rw(scaling_min_freq);
619 cpufreq_freq_attr_rw(scaling_max_freq);
620 cpufreq_freq_attr_rw(scaling_governor);
621 cpufreq_freq_attr_rw(scaling_setspeed);
623 static struct attribute *default_attrs[] = {
624 &cpuinfo_min_freq.attr,
625 &cpuinfo_max_freq.attr,
626 &cpuinfo_transition_latency.attr,
627 &scaling_min_freq.attr,
628 &scaling_max_freq.attr,
631 &scaling_governor.attr,
632 &scaling_driver.attr,
633 &scaling_available_governors.attr,
634 &scaling_setspeed.attr,
638 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
639 #define to_attr(a) container_of(a, struct freq_attr, attr)
641 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
643 struct cpufreq_policy *policy = to_policy(kobj);
644 struct freq_attr *fattr = to_attr(attr);
647 if (!down_read_trylock(&cpufreq_rwsem))
650 down_read(&policy->rwsem);
653 ret = fattr->show(policy, buf);
657 up_read(&policy->rwsem);
658 up_read(&cpufreq_rwsem);
663 static ssize_t store(struct kobject *kobj, struct attribute *attr,
664 const char *buf, size_t count)
666 struct cpufreq_policy *policy = to_policy(kobj);
667 struct freq_attr *fattr = to_attr(attr);
668 ssize_t ret = -EINVAL;
672 if (!cpu_online(policy->cpu))
675 if (!down_read_trylock(&cpufreq_rwsem))
678 down_write(&policy->rwsem);
681 ret = fattr->store(policy, buf, count);
685 up_write(&policy->rwsem);
687 up_read(&cpufreq_rwsem);
694 static void cpufreq_sysfs_release(struct kobject *kobj)
696 struct cpufreq_policy *policy = to_policy(kobj);
697 pr_debug("last reference is dropped\n");
698 complete(&policy->kobj_unregister);
701 static const struct sysfs_ops sysfs_ops = {
706 static struct kobj_type ktype_cpufreq = {
707 .sysfs_ops = &sysfs_ops,
708 .default_attrs = default_attrs,
709 .release = cpufreq_sysfs_release,
712 struct kobject *cpufreq_global_kobject;
713 EXPORT_SYMBOL(cpufreq_global_kobject);
715 static int cpufreq_global_kobject_usage;
717 int cpufreq_get_global_kobject(void)
719 if (!cpufreq_global_kobject_usage++)
720 return kobject_add(cpufreq_global_kobject,
721 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
725 EXPORT_SYMBOL(cpufreq_get_global_kobject);
727 void cpufreq_put_global_kobject(void)
729 if (!--cpufreq_global_kobject_usage)
730 kobject_del(cpufreq_global_kobject);
732 EXPORT_SYMBOL(cpufreq_put_global_kobject);
734 int cpufreq_sysfs_create_file(const struct attribute *attr)
736 int ret = cpufreq_get_global_kobject();
739 ret = sysfs_create_file(cpufreq_global_kobject, attr);
741 cpufreq_put_global_kobject();
746 EXPORT_SYMBOL(cpufreq_sysfs_create_file);
748 void cpufreq_sysfs_remove_file(const struct attribute *attr)
750 sysfs_remove_file(cpufreq_global_kobject, attr);
751 cpufreq_put_global_kobject();
753 EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
755 /* symlink affected CPUs */
756 static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
761 for_each_cpu(j, policy->cpus) {
762 struct device *cpu_dev;
764 if (j == policy->cpu)
767 pr_debug("Adding link for CPU: %u\n", j);
768 cpu_dev = get_cpu_device(j);
769 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
777 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
780 struct freq_attr **drv_attr;
783 /* prepare interface data */
784 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
785 &dev->kobj, "cpufreq");
789 /* set up files for this cpu device */
790 drv_attr = cpufreq_driver->attr;
791 while ((drv_attr) && (*drv_attr)) {
792 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
794 goto err_out_kobj_put;
797 if (cpufreq_driver->get) {
798 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
800 goto err_out_kobj_put;
803 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
805 goto err_out_kobj_put;
807 if (cpufreq_driver->bios_limit) {
808 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
810 goto err_out_kobj_put;
813 ret = cpufreq_add_dev_symlink(policy);
815 goto err_out_kobj_put;
820 kobject_put(&policy->kobj);
821 wait_for_completion(&policy->kobj_unregister);
825 static void cpufreq_init_policy(struct cpufreq_policy *policy)
827 struct cpufreq_policy new_policy;
830 memcpy(&new_policy, policy, sizeof(*policy));
832 /* Use the default policy if its valid. */
833 if (cpufreq_driver->setpolicy)
834 cpufreq_parse_governor(policy->governor->name,
835 &new_policy.policy, NULL);
837 /* assure that the starting sequence is run in cpufreq_set_policy */
838 policy->governor = NULL;
840 /* set default policy */
841 ret = cpufreq_set_policy(policy, &new_policy);
843 pr_debug("setting policy failed\n");
844 if (cpufreq_driver->exit)
845 cpufreq_driver->exit(policy);
849 #ifdef CONFIG_HOTPLUG_CPU
850 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
851 unsigned int cpu, struct device *dev)
857 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
859 pr_err("%s: Failed to stop governor\n", __func__);
864 down_write(&policy->rwsem);
866 write_lock_irqsave(&cpufreq_driver_lock, flags);
868 cpumask_set_cpu(cpu, policy->cpus);
869 per_cpu(cpufreq_cpu_data, cpu) = policy;
870 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
872 up_write(&policy->rwsem);
875 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
876 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
877 pr_err("%s: Failed to start governor\n", __func__);
882 return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
886 static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
888 struct cpufreq_policy *policy;
891 read_lock_irqsave(&cpufreq_driver_lock, flags);
893 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
895 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
900 static struct cpufreq_policy *cpufreq_policy_alloc(void)
902 struct cpufreq_policy *policy;
904 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
908 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
909 goto err_free_policy;
911 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
912 goto err_free_cpumask;
914 INIT_LIST_HEAD(&policy->policy_list);
915 init_rwsem(&policy->rwsem);
920 free_cpumask_var(policy->cpus);
927 static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
929 struct kobject *kobj;
930 struct completion *cmp;
932 down_read(&policy->rwsem);
933 kobj = &policy->kobj;
934 cmp = &policy->kobj_unregister;
935 up_read(&policy->rwsem);
939 * We need to make sure that the underlying kobj is
940 * actually not referenced anymore by anybody before we
941 * proceed with unloading.
943 pr_debug("waiting for dropping of refcount\n");
944 wait_for_completion(cmp);
945 pr_debug("wait complete\n");
948 static void cpufreq_policy_free(struct cpufreq_policy *policy)
950 free_cpumask_var(policy->related_cpus);
951 free_cpumask_var(policy->cpus);
955 static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
957 if (WARN_ON(cpu == policy->cpu))
960 down_write(&policy->rwsem);
962 policy->last_cpu = policy->cpu;
965 up_write(&policy->rwsem);
967 cpufreq_frequency_table_update_policy_cpu(policy);
968 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
969 CPUFREQ_UPDATE_POLICY_CPU, policy);
972 static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
975 unsigned int j, cpu = dev->id;
977 struct cpufreq_policy *policy;
979 #ifdef CONFIG_HOTPLUG_CPU
980 struct cpufreq_policy *tpolicy;
981 struct cpufreq_governor *gov;
984 if (cpu_is_offline(cpu))
987 pr_debug("adding CPU %u\n", cpu);
990 /* check whether a different CPU already registered this
991 * CPU because it is in the same boat. */
992 policy = cpufreq_cpu_get(cpu);
993 if (unlikely(policy)) {
994 cpufreq_cpu_put(policy);
999 if (!down_read_trylock(&cpufreq_rwsem))
1002 #ifdef CONFIG_HOTPLUG_CPU
1003 /* Check if this cpu was hot-unplugged earlier and has siblings */
1004 read_lock_irqsave(&cpufreq_driver_lock, flags);
1005 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1006 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
1007 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1008 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
1009 up_read(&cpufreq_rwsem);
1013 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1017 * Restore the saved policy when doing light-weight init and fall back
1018 * to the full init if that fails.
1020 policy = frozen ? cpufreq_policy_restore(cpu) : NULL;
1023 policy = cpufreq_policy_alloc();
1029 * In the resume path, since we restore a saved policy, the assignment
1030 * to policy->cpu is like an update of the existing policy, rather than
1031 * the creation of a brand new one. So we need to perform this update
1032 * by invoking update_policy_cpu().
1034 if (frozen && cpu != policy->cpu)
1035 update_policy_cpu(policy, cpu);
1039 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1040 cpumask_copy(policy->cpus, cpumask_of(cpu));
1042 init_completion(&policy->kobj_unregister);
1043 INIT_WORK(&policy->update, handle_update);
1045 /* call driver. From then on the cpufreq must be able
1046 * to accept all calls to ->verify and ->setpolicy for this CPU
1048 ret = cpufreq_driver->init(policy);
1050 pr_debug("initialization failed\n");
1051 goto err_set_policy_cpu;
1054 if (cpufreq_driver->get) {
1055 policy->cur = cpufreq_driver->get(policy->cpu);
1057 pr_err("%s: ->get() failed\n", __func__);
1062 /* related cpus should atleast have policy->cpus */
1063 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1066 * affected cpus must always be the one, which are online. We aren't
1067 * managing offline cpus here.
1069 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1072 policy->user_policy.min = policy->min;
1073 policy->user_policy.max = policy->max;
1076 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1077 CPUFREQ_START, policy);
1079 #ifdef CONFIG_HOTPLUG_CPU
1080 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1082 policy->governor = gov;
1083 pr_debug("Restoring governor %s for cpu %d\n",
1084 policy->governor->name, cpu);
1088 write_lock_irqsave(&cpufreq_driver_lock, flags);
1089 for_each_cpu(j, policy->cpus)
1090 per_cpu(cpufreq_cpu_data, j) = policy;
1091 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1094 ret = cpufreq_add_dev_interface(policy, dev);
1096 goto err_out_unregister;
1099 write_lock_irqsave(&cpufreq_driver_lock, flags);
1100 list_add(&policy->policy_list, &cpufreq_policy_list);
1101 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1103 cpufreq_init_policy(policy);
1106 policy->user_policy.policy = policy->policy;
1107 policy->user_policy.governor = policy->governor;
1110 kobject_uevent(&policy->kobj, KOBJ_ADD);
1111 up_read(&cpufreq_rwsem);
1113 pr_debug("initialization complete\n");
1118 write_lock_irqsave(&cpufreq_driver_lock, flags);
1119 for_each_cpu(j, policy->cpus)
1120 per_cpu(cpufreq_cpu_data, j) = NULL;
1121 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1124 if (cpufreq_driver->exit)
1125 cpufreq_driver->exit(policy);
1128 /* Do not leave stale fallback data behind. */
1129 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
1130 cpufreq_policy_put_kobj(policy);
1132 cpufreq_policy_free(policy);
1135 up_read(&cpufreq_rwsem);
1141 * cpufreq_add_dev - add a CPU device
1143 * Adds the cpufreq interface for a CPU device.
1145 * The Oracle says: try running cpufreq registration/unregistration concurrently
1146 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1147 * mess up, but more thorough testing is needed. - Mathieu
1149 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1151 return __cpufreq_add_dev(dev, sif, false);
1154 static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
1155 unsigned int old_cpu)
1157 struct device *cpu_dev;
1160 /* first sibling now owns the new sysfs dir */
1161 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
1163 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1164 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1166 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1168 down_write(&policy->rwsem);
1169 cpumask_set_cpu(old_cpu, policy->cpus);
1170 up_write(&policy->rwsem);
1172 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1181 static int __cpufreq_remove_dev_prepare(struct device *dev,
1182 struct subsys_interface *sif,
1185 unsigned int cpu = dev->id, cpus;
1187 unsigned long flags;
1188 struct cpufreq_policy *policy;
1190 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1192 write_lock_irqsave(&cpufreq_driver_lock, flags);
1194 policy = per_cpu(cpufreq_cpu_data, cpu);
1196 /* Save the policy somewhere when doing a light-weight tear-down */
1198 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
1200 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1203 pr_debug("%s: No cpu_data found\n", __func__);
1208 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1210 pr_err("%s: Failed to stop governor\n", __func__);
1215 #ifdef CONFIG_HOTPLUG_CPU
1216 if (!cpufreq_driver->setpolicy)
1217 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1218 policy->governor->name, CPUFREQ_NAME_LEN);
1221 down_read(&policy->rwsem);
1222 cpus = cpumask_weight(policy->cpus);
1223 up_read(&policy->rwsem);
1225 if (cpu != policy->cpu) {
1227 sysfs_remove_link(&dev->kobj, "cpufreq");
1228 } else if (cpus > 1) {
1229 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
1231 update_policy_cpu(policy, new_cpu);
1234 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1235 __func__, new_cpu, cpu);
1243 static int __cpufreq_remove_dev_finish(struct device *dev,
1244 struct subsys_interface *sif,
1247 unsigned int cpu = dev->id, cpus;
1249 unsigned long flags;
1250 struct cpufreq_policy *policy;
1252 read_lock_irqsave(&cpufreq_driver_lock, flags);
1253 policy = per_cpu(cpufreq_cpu_data, cpu);
1254 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1257 pr_debug("%s: No cpu_data found\n", __func__);
1261 down_write(&policy->rwsem);
1262 cpus = cpumask_weight(policy->cpus);
1265 cpumask_clear_cpu(cpu, policy->cpus);
1266 up_write(&policy->rwsem);
1268 /* If cpu is last user of policy, free policy */
1271 ret = __cpufreq_governor(policy,
1272 CPUFREQ_GOV_POLICY_EXIT);
1274 pr_err("%s: Failed to exit governor\n",
1281 cpufreq_policy_put_kobj(policy);
1284 * Perform the ->exit() even during light-weight tear-down,
1285 * since this is a core component, and is essential for the
1286 * subsequent light-weight ->init() to succeed.
1288 if (cpufreq_driver->exit)
1289 cpufreq_driver->exit(policy);
1291 /* Remove policy from list of active policies */
1292 write_lock_irqsave(&cpufreq_driver_lock, flags);
1293 list_del(&policy->policy_list);
1294 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1297 cpufreq_policy_free(policy);
1300 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1301 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1302 pr_err("%s: Failed to start governor\n",
1309 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1314 * cpufreq_remove_dev - remove a CPU device
1316 * Removes the cpufreq interface for a CPU device.
1318 static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1320 unsigned int cpu = dev->id;
1323 if (cpu_is_offline(cpu))
1326 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1329 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1334 static void handle_update(struct work_struct *work)
1336 struct cpufreq_policy *policy =
1337 container_of(work, struct cpufreq_policy, update);
1338 unsigned int cpu = policy->cpu;
1339 pr_debug("handle_update for cpu %u called\n", cpu);
1340 cpufreq_update_policy(cpu);
1344 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1347 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1348 * @new_freq: CPU frequency the CPU actually runs at
1350 * We adjust to current frequency first, and need to clean up later.
1351 * So either call to cpufreq_update_policy() or schedule handle_update()).
1353 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1354 unsigned int new_freq)
1356 struct cpufreq_policy *policy;
1357 struct cpufreq_freqs freqs;
1358 unsigned long flags;
1360 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1361 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1363 freqs.old = old_freq;
1364 freqs.new = new_freq;
1366 read_lock_irqsave(&cpufreq_driver_lock, flags);
1367 policy = per_cpu(cpufreq_cpu_data, cpu);
1368 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1370 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1371 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
1375 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1378 * This is the last known freq, without actually getting it from the driver.
1379 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1381 unsigned int cpufreq_quick_get(unsigned int cpu)
1383 struct cpufreq_policy *policy;
1384 unsigned int ret_freq = 0;
1386 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1387 return cpufreq_driver->get(cpu);
1389 policy = cpufreq_cpu_get(cpu);
1391 ret_freq = policy->cur;
1392 cpufreq_cpu_put(policy);
1397 EXPORT_SYMBOL(cpufreq_quick_get);
1400 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1403 * Just return the max possible frequency for a given CPU.
1405 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1407 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1408 unsigned int ret_freq = 0;
1411 ret_freq = policy->max;
1412 cpufreq_cpu_put(policy);
1417 EXPORT_SYMBOL(cpufreq_quick_get_max);
1419 static unsigned int __cpufreq_get(unsigned int cpu)
1421 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1422 unsigned int ret_freq = 0;
1424 if (!cpufreq_driver->get)
1427 ret_freq = cpufreq_driver->get(cpu);
1429 if (ret_freq && policy->cur &&
1430 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1431 /* verify no discrepancy between actual and
1432 saved value exists */
1433 if (unlikely(ret_freq != policy->cur)) {
1434 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1435 schedule_work(&policy->update);
1443 * cpufreq_get - get the current CPU frequency (in kHz)
1446 * Get the CPU current (static) CPU frequency
1448 unsigned int cpufreq_get(unsigned int cpu)
1450 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1451 unsigned int ret_freq = 0;
1453 if (cpufreq_disabled() || !cpufreq_driver)
1458 if (!down_read_trylock(&cpufreq_rwsem))
1461 down_read(&policy->rwsem);
1463 ret_freq = __cpufreq_get(cpu);
1465 up_read(&policy->rwsem);
1466 up_read(&cpufreq_rwsem);
1470 EXPORT_SYMBOL(cpufreq_get);
1472 static struct subsys_interface cpufreq_interface = {
1474 .subsys = &cpu_subsys,
1475 .add_dev = cpufreq_add_dev,
1476 .remove_dev = cpufreq_remove_dev,
1480 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1482 * This function is only executed for the boot processor. The other CPUs
1483 * have been put offline by means of CPU hotplug.
1485 static int cpufreq_bp_suspend(void)
1489 int cpu = smp_processor_id();
1490 struct cpufreq_policy *policy;
1492 pr_debug("suspending cpu %u\n", cpu);
1494 /* If there's no policy for the boot CPU, we have nothing to do. */
1495 policy = cpufreq_cpu_get(cpu);
1499 if (cpufreq_driver->suspend) {
1500 ret = cpufreq_driver->suspend(policy);
1502 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1503 "step on CPU %u\n", policy->cpu);
1506 cpufreq_cpu_put(policy);
1511 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1513 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1514 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1515 * restored. It will verify that the current freq is in sync with
1516 * what we believe it to be. This is a bit later than when it
1517 * should be, but nonethteless it's better than calling
1518 * cpufreq_driver->get() here which might re-enable interrupts...
1520 * This function is only executed for the boot CPU. The other CPUs have not
1521 * been turned on yet.
1523 static void cpufreq_bp_resume(void)
1527 int cpu = smp_processor_id();
1528 struct cpufreq_policy *policy;
1530 pr_debug("resuming cpu %u\n", cpu);
1532 /* If there's no policy for the boot CPU, we have nothing to do. */
1533 policy = cpufreq_cpu_get(cpu);
1537 if (cpufreq_driver->resume) {
1538 ret = cpufreq_driver->resume(policy);
1540 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1541 "step on CPU %u\n", policy->cpu);
1546 schedule_work(&policy->update);
1549 cpufreq_cpu_put(policy);
1552 static struct syscore_ops cpufreq_syscore_ops = {
1553 .suspend = cpufreq_bp_suspend,
1554 .resume = cpufreq_bp_resume,
1558 * cpufreq_get_current_driver - return current driver's name
1560 * Return the name string of the currently loaded cpufreq driver
1563 const char *cpufreq_get_current_driver(void)
1566 return cpufreq_driver->name;
1570 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1572 /*********************************************************************
1573 * NOTIFIER LISTS INTERFACE *
1574 *********************************************************************/
1577 * cpufreq_register_notifier - register a driver with cpufreq
1578 * @nb: notifier function to register
1579 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1581 * Add a driver to one of two lists: either a list of drivers that
1582 * are notified about clock rate changes (once before and once after
1583 * the transition), or a list of drivers that are notified about
1584 * changes in cpufreq policy.
1586 * This function may sleep, and has the same return conditions as
1587 * blocking_notifier_chain_register.
1589 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1593 if (cpufreq_disabled())
1596 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1599 case CPUFREQ_TRANSITION_NOTIFIER:
1600 ret = srcu_notifier_chain_register(
1601 &cpufreq_transition_notifier_list, nb);
1603 case CPUFREQ_POLICY_NOTIFIER:
1604 ret = blocking_notifier_chain_register(
1605 &cpufreq_policy_notifier_list, nb);
1613 EXPORT_SYMBOL(cpufreq_register_notifier);
1616 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1617 * @nb: notifier block to be unregistered
1618 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1620 * Remove a driver from the CPU frequency notifier list.
1622 * This function may sleep, and has the same return conditions as
1623 * blocking_notifier_chain_unregister.
1625 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1629 if (cpufreq_disabled())
1633 case CPUFREQ_TRANSITION_NOTIFIER:
1634 ret = srcu_notifier_chain_unregister(
1635 &cpufreq_transition_notifier_list, nb);
1637 case CPUFREQ_POLICY_NOTIFIER:
1638 ret = blocking_notifier_chain_unregister(
1639 &cpufreq_policy_notifier_list, nb);
1647 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1650 /*********************************************************************
1652 *********************************************************************/
1654 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1655 unsigned int target_freq,
1656 unsigned int relation)
1658 int retval = -EINVAL;
1659 unsigned int old_target_freq = target_freq;
1661 if (cpufreq_disabled())
1664 /* Make sure that target_freq is within supported range */
1665 if (target_freq > policy->max)
1666 target_freq = policy->max;
1667 if (target_freq < policy->min)
1668 target_freq = policy->min;
1670 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1671 policy->cpu, target_freq, relation, old_target_freq);
1674 * This might look like a redundant call as we are checking it again
1675 * after finding index. But it is left intentionally for cases where
1676 * exactly same freq is called again and so we can save on few function
1679 if (target_freq == policy->cur)
1682 if (cpufreq_driver->target)
1683 retval = cpufreq_driver->target(policy, target_freq, relation);
1684 else if (cpufreq_driver->target_index) {
1685 struct cpufreq_frequency_table *freq_table;
1686 struct cpufreq_freqs freqs;
1690 freq_table = cpufreq_frequency_get_table(policy->cpu);
1691 if (unlikely(!freq_table)) {
1692 pr_err("%s: Unable to find freq_table\n", __func__);
1696 retval = cpufreq_frequency_table_target(policy, freq_table,
1697 target_freq, relation, &index);
1698 if (unlikely(retval)) {
1699 pr_err("%s: Unable to find matching freq\n", __func__);
1703 if (freq_table[index].frequency == policy->cur) {
1708 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1711 freqs.old = policy->cur;
1712 freqs.new = freq_table[index].frequency;
1715 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1716 __func__, policy->cpu, freqs.old,
1719 cpufreq_notify_transition(policy, &freqs,
1723 retval = cpufreq_driver->target_index(policy, index);
1725 pr_err("%s: Failed to change cpu frequency: %d\n",
1730 * Notify with old freq in case we failed to change
1734 freqs.new = freqs.old;
1736 cpufreq_notify_transition(policy, &freqs,
1737 CPUFREQ_POSTCHANGE);
1744 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1746 int cpufreq_driver_target(struct cpufreq_policy *policy,
1747 unsigned int target_freq,
1748 unsigned int relation)
1752 down_write(&policy->rwsem);
1754 ret = __cpufreq_driver_target(policy, target_freq, relation);
1756 up_write(&policy->rwsem);
1760 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1763 * when "event" is CPUFREQ_GOV_LIMITS
1766 static int __cpufreq_governor(struct cpufreq_policy *policy,
1771 /* Only must be defined when default governor is known to have latency
1772 restrictions, like e.g. conservative or ondemand.
1773 That this is the case is already ensured in Kconfig
1775 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1776 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1778 struct cpufreq_governor *gov = NULL;
1781 if (policy->governor->max_transition_latency &&
1782 policy->cpuinfo.transition_latency >
1783 policy->governor->max_transition_latency) {
1787 printk(KERN_WARNING "%s governor failed, too long"
1788 " transition latency of HW, fallback"
1789 " to %s governor\n",
1790 policy->governor->name,
1792 policy->governor = gov;
1796 if (event == CPUFREQ_GOV_POLICY_INIT)
1797 if (!try_module_get(policy->governor->owner))
1800 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1801 policy->cpu, event);
1803 mutex_lock(&cpufreq_governor_lock);
1804 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
1805 || (!policy->governor_enabled
1806 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
1807 mutex_unlock(&cpufreq_governor_lock);
1811 if (event == CPUFREQ_GOV_STOP)
1812 policy->governor_enabled = false;
1813 else if (event == CPUFREQ_GOV_START)
1814 policy->governor_enabled = true;
1816 mutex_unlock(&cpufreq_governor_lock);
1818 ret = policy->governor->governor(policy, event);
1821 if (event == CPUFREQ_GOV_POLICY_INIT)
1822 policy->governor->initialized++;
1823 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1824 policy->governor->initialized--;
1826 /* Restore original values */
1827 mutex_lock(&cpufreq_governor_lock);
1828 if (event == CPUFREQ_GOV_STOP)
1829 policy->governor_enabled = true;
1830 else if (event == CPUFREQ_GOV_START)
1831 policy->governor_enabled = false;
1832 mutex_unlock(&cpufreq_governor_lock);
1835 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1836 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
1837 module_put(policy->governor->owner);
1842 int cpufreq_register_governor(struct cpufreq_governor *governor)
1849 if (cpufreq_disabled())
1852 mutex_lock(&cpufreq_governor_mutex);
1854 governor->initialized = 0;
1856 if (__find_governor(governor->name) == NULL) {
1858 list_add(&governor->governor_list, &cpufreq_governor_list);
1861 mutex_unlock(&cpufreq_governor_mutex);
1864 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1866 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1868 #ifdef CONFIG_HOTPLUG_CPU
1875 if (cpufreq_disabled())
1878 #ifdef CONFIG_HOTPLUG_CPU
1879 for_each_present_cpu(cpu) {
1880 if (cpu_online(cpu))
1882 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1883 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1887 mutex_lock(&cpufreq_governor_mutex);
1888 list_del(&governor->governor_list);
1889 mutex_unlock(&cpufreq_governor_mutex);
1892 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1895 /*********************************************************************
1896 * POLICY INTERFACE *
1897 *********************************************************************/
1900 * cpufreq_get_policy - get the current cpufreq_policy
1901 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1904 * Reads the current cpufreq policy.
1906 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1908 struct cpufreq_policy *cpu_policy;
1912 cpu_policy = cpufreq_cpu_get(cpu);
1916 memcpy(policy, cpu_policy, sizeof(*policy));
1918 cpufreq_cpu_put(cpu_policy);
1921 EXPORT_SYMBOL(cpufreq_get_policy);
1924 * policy : current policy.
1925 * new_policy: policy to be set.
1927 static int cpufreq_set_policy(struct cpufreq_policy *policy,
1928 struct cpufreq_policy *new_policy)
1930 int ret = 0, failed = 1;
1932 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1933 new_policy->min, new_policy->max);
1935 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
1937 if (new_policy->min > policy->max || new_policy->max < policy->min) {
1942 /* verify the cpu speed can be set within this limit */
1943 ret = cpufreq_driver->verify(new_policy);
1947 /* adjust if necessary - all reasons */
1948 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1949 CPUFREQ_ADJUST, new_policy);
1951 /* adjust if necessary - hardware incompatibility*/
1952 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1953 CPUFREQ_INCOMPATIBLE, new_policy);
1956 * verify the cpu speed can be set within this limit, which might be
1957 * different to the first one
1959 ret = cpufreq_driver->verify(new_policy);
1963 /* notification of the new policy */
1964 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1965 CPUFREQ_NOTIFY, new_policy);
1967 policy->min = new_policy->min;
1968 policy->max = new_policy->max;
1970 pr_debug("new min and max freqs are %u - %u kHz\n",
1971 policy->min, policy->max);
1973 if (cpufreq_driver->setpolicy) {
1974 policy->policy = new_policy->policy;
1975 pr_debug("setting range\n");
1976 ret = cpufreq_driver->setpolicy(new_policy);
1978 if (new_policy->governor != policy->governor) {
1979 /* save old, working values */
1980 struct cpufreq_governor *old_gov = policy->governor;
1982 pr_debug("governor switch\n");
1984 /* end old governor */
1985 if (policy->governor) {
1986 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1987 up_write(&policy->rwsem);
1988 __cpufreq_governor(policy,
1989 CPUFREQ_GOV_POLICY_EXIT);
1990 down_write(&policy->rwsem);
1993 /* start new governor */
1994 policy->governor = new_policy->governor;
1995 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
1996 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
1999 up_write(&policy->rwsem);
2000 __cpufreq_governor(policy,
2001 CPUFREQ_GOV_POLICY_EXIT);
2002 down_write(&policy->rwsem);
2007 /* new governor failed, so re-start old one */
2008 pr_debug("starting governor %s failed\n",
2009 policy->governor->name);
2011 policy->governor = old_gov;
2012 __cpufreq_governor(policy,
2013 CPUFREQ_GOV_POLICY_INIT);
2014 __cpufreq_governor(policy,
2020 /* might be a policy change, too, so fall through */
2022 pr_debug("governor: change or update limits\n");
2023 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2031 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2032 * @cpu: CPU which shall be re-evaluated
2034 * Useful for policy notifiers which have different necessities
2035 * at different times.
2037 int cpufreq_update_policy(unsigned int cpu)
2039 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2040 struct cpufreq_policy new_policy;
2048 down_write(&policy->rwsem);
2050 pr_debug("updating policy for CPU %u\n", cpu);
2051 memcpy(&new_policy, policy, sizeof(*policy));
2052 new_policy.min = policy->user_policy.min;
2053 new_policy.max = policy->user_policy.max;
2054 new_policy.policy = policy->user_policy.policy;
2055 new_policy.governor = policy->user_policy.governor;
2058 * BIOS might change freq behind our back
2059 * -> ask driver for current freq and notify governors about a change
2061 if (cpufreq_driver->get) {
2062 new_policy.cur = cpufreq_driver->get(cpu);
2064 pr_debug("Driver did not initialize current freq");
2065 policy->cur = new_policy.cur;
2067 if (policy->cur != new_policy.cur && has_target())
2068 cpufreq_out_of_sync(cpu, policy->cur,
2073 ret = cpufreq_set_policy(policy, &new_policy);
2075 up_write(&policy->rwsem);
2077 cpufreq_cpu_put(policy);
2081 EXPORT_SYMBOL(cpufreq_update_policy);
2083 static int cpufreq_cpu_callback(struct notifier_block *nfb,
2084 unsigned long action, void *hcpu)
2086 unsigned int cpu = (unsigned long)hcpu;
2088 bool frozen = false;
2090 dev = get_cpu_device(cpu);
2093 if (action & CPU_TASKS_FROZEN)
2096 switch (action & ~CPU_TASKS_FROZEN) {
2098 __cpufreq_add_dev(dev, NULL, frozen);
2099 cpufreq_update_policy(cpu);
2102 case CPU_DOWN_PREPARE:
2103 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
2107 __cpufreq_remove_dev_finish(dev, NULL, frozen);
2110 case CPU_DOWN_FAILED:
2111 __cpufreq_add_dev(dev, NULL, frozen);
2118 static struct notifier_block __refdata cpufreq_cpu_notifier = {
2119 .notifier_call = cpufreq_cpu_callback,
2122 /*********************************************************************
2123 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2124 *********************************************************************/
2127 * cpufreq_register_driver - register a CPU Frequency driver
2128 * @driver_data: A struct cpufreq_driver containing the values#
2129 * submitted by the CPU Frequency driver.
2131 * Registers a CPU Frequency driver to this core code. This code
2132 * returns zero on success, -EBUSY when another driver got here first
2133 * (and isn't unregistered in the meantime).
2136 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2138 unsigned long flags;
2141 if (cpufreq_disabled())
2144 if (!driver_data || !driver_data->verify || !driver_data->init ||
2145 !(driver_data->setpolicy || driver_data->target_index ||
2146 driver_data->target))
2149 pr_debug("trying to register driver %s\n", driver_data->name);
2151 if (driver_data->setpolicy)
2152 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2154 write_lock_irqsave(&cpufreq_driver_lock, flags);
2155 if (cpufreq_driver) {
2156 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2159 cpufreq_driver = driver_data;
2160 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2162 ret = subsys_interface_register(&cpufreq_interface);
2164 goto err_null_driver;
2166 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
2170 /* check for at least one working CPU */
2171 for (i = 0; i < nr_cpu_ids; i++)
2172 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
2177 /* if all ->init() calls failed, unregister */
2179 pr_debug("no CPU initialized for driver %s\n",
2185 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2186 pr_debug("driver %s up and running\n", driver_data->name);
2190 subsys_interface_unregister(&cpufreq_interface);
2192 write_lock_irqsave(&cpufreq_driver_lock, flags);
2193 cpufreq_driver = NULL;
2194 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2197 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2200 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2202 * Unregister the current CPUFreq driver. Only call this if you have
2203 * the right to do so, i.e. if you have succeeded in initialising before!
2204 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2205 * currently not initialised.
2207 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2209 unsigned long flags;
2211 if (!cpufreq_driver || (driver != cpufreq_driver))
2214 pr_debug("unregistering driver %s\n", driver->name);
2216 subsys_interface_unregister(&cpufreq_interface);
2217 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2219 down_write(&cpufreq_rwsem);
2220 write_lock_irqsave(&cpufreq_driver_lock, flags);
2222 cpufreq_driver = NULL;
2224 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2225 up_write(&cpufreq_rwsem);
2229 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2231 static int __init cpufreq_core_init(void)
2233 if (cpufreq_disabled())
2236 cpufreq_global_kobject = kobject_create();
2237 BUG_ON(!cpufreq_global_kobject);
2238 register_syscore_ops(&cpufreq_syscore_ops);
2242 core_initcall(cpufreq_core_init);