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/suspend.h>
30 #include <linux/syscore_ops.h>
31 #include <linux/tick.h>
32 #include <trace/events/power.h>
34 static LIST_HEAD(cpufreq_policy_list);
36 static inline bool policy_is_inactive(struct cpufreq_policy *policy)
38 return cpumask_empty(policy->cpus);
41 static bool suitable_policy(struct cpufreq_policy *policy, bool active)
43 return active == !policy_is_inactive(policy);
46 /* Finds Next Acive/Inactive policy */
47 static struct cpufreq_policy *next_policy(struct cpufreq_policy *policy,
51 policy = list_next_entry(policy, policy_list);
53 /* No more policies in the list */
54 if (&policy->policy_list == &cpufreq_policy_list)
56 } while (!suitable_policy(policy, active));
61 static struct cpufreq_policy *first_policy(bool active)
63 struct cpufreq_policy *policy;
65 /* No policies in the list */
66 if (list_empty(&cpufreq_policy_list))
69 policy = list_first_entry(&cpufreq_policy_list, typeof(*policy),
72 if (!suitable_policy(policy, active))
73 policy = next_policy(policy, active);
78 /* Macros to iterate over CPU policies */
79 #define for_each_suitable_policy(__policy, __active) \
80 for (__policy = first_policy(__active); \
82 __policy = next_policy(__policy, __active))
84 #define for_each_active_policy(__policy) \
85 for_each_suitable_policy(__policy, true)
86 #define for_each_inactive_policy(__policy) \
87 for_each_suitable_policy(__policy, false)
89 #define for_each_policy(__policy) \
90 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
92 /* Iterate over governors */
93 static LIST_HEAD(cpufreq_governor_list);
94 #define for_each_governor(__governor) \
95 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
98 * The "cpufreq driver" - the arch- or hardware-dependent low
99 * level driver of CPUFreq support, and its spinlock. This lock
100 * also protects the cpufreq_cpu_data array.
102 static struct cpufreq_driver *cpufreq_driver;
103 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
104 static DEFINE_RWLOCK(cpufreq_driver_lock);
105 DEFINE_MUTEX(cpufreq_governor_lock);
107 /* Flag to suspend/resume CPUFreq governors */
108 static bool cpufreq_suspended;
110 static inline bool has_target(void)
112 return cpufreq_driver->target_index || cpufreq_driver->target;
115 /* internal prototypes */
116 static int __cpufreq_governor(struct cpufreq_policy *policy,
118 static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
119 static void handle_update(struct work_struct *work);
122 * Two notifier lists: the "policy" list is involved in the
123 * validation process for a new CPU frequency policy; the
124 * "transition" list for kernel code that needs to handle
125 * changes to devices when the CPU clock speed changes.
126 * The mutex locks both lists.
128 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
129 static struct srcu_notifier_head cpufreq_transition_notifier_list;
131 static bool init_cpufreq_transition_notifier_list_called;
132 static int __init init_cpufreq_transition_notifier_list(void)
134 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
135 init_cpufreq_transition_notifier_list_called = true;
138 pure_initcall(init_cpufreq_transition_notifier_list);
140 static int off __read_mostly;
141 static int cpufreq_disabled(void)
145 void disable_cpufreq(void)
149 static DEFINE_MUTEX(cpufreq_governor_mutex);
151 bool have_governor_per_policy(void)
153 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
155 EXPORT_SYMBOL_GPL(have_governor_per_policy);
157 bool cpufreq_driver_is_slow(void)
159 return !(cpufreq_driver->flags & CPUFREQ_DRIVER_FAST);
161 EXPORT_SYMBOL_GPL(cpufreq_driver_is_slow);
163 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
165 if (have_governor_per_policy())
166 return &policy->kobj;
168 return cpufreq_global_kobject;
170 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
172 struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
174 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
176 return policy && !policy_is_inactive(policy) ?
177 policy->freq_table : NULL;
179 EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
181 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
187 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
189 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
190 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
191 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
192 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
193 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
194 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
196 idle_time = cur_wall_time - busy_time;
198 *wall = cputime_to_usecs(cur_wall_time);
200 return cputime_to_usecs(idle_time);
203 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
205 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
207 if (idle_time == -1ULL)
208 return get_cpu_idle_time_jiffy(cpu, wall);
210 idle_time += get_cpu_iowait_time_us(cpu, wall);
214 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
217 * This is a generic cpufreq init() routine which can be used by cpufreq
218 * drivers of SMP systems. It will do following:
219 * - validate & show freq table passed
220 * - set policies transition latency
221 * - policy->cpus with all possible CPUs
223 int cpufreq_generic_init(struct cpufreq_policy *policy,
224 struct cpufreq_frequency_table *table,
225 unsigned int transition_latency)
229 ret = cpufreq_table_validate_and_show(policy, table);
231 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
235 policy->cpuinfo.transition_latency = transition_latency;
238 * The driver only supports the SMP configuration where all processors
239 * share the clock and voltage and clock.
241 cpumask_setall(policy->cpus);
245 EXPORT_SYMBOL_GPL(cpufreq_generic_init);
247 struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
249 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
251 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
253 EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
255 unsigned int cpufreq_generic_get(unsigned int cpu)
257 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
259 if (!policy || IS_ERR(policy->clk)) {
260 pr_err("%s: No %s associated to cpu: %d\n",
261 __func__, policy ? "clk" : "policy", cpu);
265 return clk_get_rate(policy->clk) / 1000;
267 EXPORT_SYMBOL_GPL(cpufreq_generic_get);
270 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
272 * @cpu: cpu to find policy for.
274 * This returns policy for 'cpu', returns NULL if it doesn't exist.
275 * It also increments the kobject reference count to mark it busy and so would
276 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
277 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
278 * freed as that depends on the kobj count.
280 * Return: A valid policy on success, otherwise NULL on failure.
282 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
284 struct cpufreq_policy *policy = NULL;
287 if (WARN_ON(cpu >= nr_cpu_ids))
290 /* get the cpufreq driver */
291 read_lock_irqsave(&cpufreq_driver_lock, flags);
293 if (cpufreq_driver) {
295 policy = cpufreq_cpu_get_raw(cpu);
297 kobject_get(&policy->kobj);
300 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
304 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
307 * cpufreq_cpu_put: Decrements the usage count of a policy
309 * @policy: policy earlier returned by cpufreq_cpu_get().
311 * This decrements the kobject reference count incremented earlier by calling
314 void cpufreq_cpu_put(struct cpufreq_policy *policy)
316 kobject_put(&policy->kobj);
318 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
320 /*********************************************************************
321 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
322 *********************************************************************/
325 * adjust_jiffies - adjust the system "loops_per_jiffy"
327 * This function alters the system "loops_per_jiffy" for the clock
328 * speed change. Note that loops_per_jiffy cannot be updated on SMP
329 * systems as each CPU might be scaled differently. So, use the arch
330 * per-CPU loops_per_jiffy value wherever possible.
332 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
335 static unsigned long l_p_j_ref;
336 static unsigned int l_p_j_ref_freq;
338 if (ci->flags & CPUFREQ_CONST_LOOPS)
341 if (!l_p_j_ref_freq) {
342 l_p_j_ref = loops_per_jiffy;
343 l_p_j_ref_freq = ci->old;
344 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
345 l_p_j_ref, l_p_j_ref_freq);
347 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
348 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
350 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
351 loops_per_jiffy, ci->new);
356 /*********************************************************************
357 * FREQUENCY INVARIANT CPU CAPACITY *
358 *********************************************************************/
360 static DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
361 static DEFINE_PER_CPU(unsigned long, max_freq_scale) = SCHED_CAPACITY_SCALE;
364 scale_freq_capacity(struct cpufreq_policy *policy, struct cpufreq_freqs *freqs)
366 unsigned long cur = freqs ? freqs->new : policy->cur;
367 unsigned long scale = (cur << SCHED_CAPACITY_SHIFT) / policy->max;
368 struct cpufreq_cpuinfo *cpuinfo = &policy->cpuinfo;
371 pr_debug("cpus %*pbl cur/cur max freq %lu/%u kHz freq scale %lu\n",
372 cpumask_pr_args(policy->cpus), cur, policy->max, scale);
374 for_each_cpu(cpu, policy->cpus)
375 per_cpu(freq_scale, cpu) = scale;
380 scale = (policy->max << SCHED_CAPACITY_SHIFT) / cpuinfo->max_freq;
382 pr_debug("cpus %*pbl cur max/max freq %u/%u kHz max freq scale %lu\n",
383 cpumask_pr_args(policy->cpus), policy->max, cpuinfo->max_freq,
386 for_each_cpu(cpu, policy->cpus)
387 per_cpu(max_freq_scale, cpu) = scale;
390 unsigned long cpufreq_scale_freq_capacity(struct sched_domain *sd, int cpu)
392 return per_cpu(freq_scale, cpu);
395 unsigned long cpufreq_scale_max_freq_capacity(int cpu)
397 return per_cpu(max_freq_scale, cpu);
400 static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
401 struct cpufreq_freqs *freqs, unsigned int state)
403 BUG_ON(irqs_disabled());
405 if (cpufreq_disabled())
408 freqs->flags = cpufreq_driver->flags;
409 pr_debug("notification %u of frequency transition to %u kHz\n",
414 case CPUFREQ_PRECHANGE:
415 /* detect if the driver reported a value as "old frequency"
416 * which is not equal to what the cpufreq core thinks is
419 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
420 if ((policy) && (policy->cpu == freqs->cpu) &&
421 (policy->cur) && (policy->cur != freqs->old)) {
422 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
423 freqs->old, policy->cur);
424 freqs->old = policy->cur;
427 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
428 CPUFREQ_PRECHANGE, freqs);
429 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
432 case CPUFREQ_POSTCHANGE:
433 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
434 pr_debug("FREQ: %lu - CPU: %lu\n",
435 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
436 trace_cpu_frequency(freqs->new, freqs->cpu);
437 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
438 CPUFREQ_POSTCHANGE, freqs);
439 if (likely(policy) && likely(policy->cpu == freqs->cpu))
440 policy->cur = freqs->new;
446 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
447 * on frequency transition.
449 * This function calls the transition notifiers and the "adjust_jiffies"
450 * function. It is called twice on all CPU frequency changes that have
453 static void cpufreq_notify_transition(struct cpufreq_policy *policy,
454 struct cpufreq_freqs *freqs, unsigned int state)
456 for_each_cpu(freqs->cpu, policy->cpus)
457 __cpufreq_notify_transition(policy, freqs, state);
460 /* Do post notifications when there are chances that transition has failed */
461 static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
462 struct cpufreq_freqs *freqs, int transition_failed)
464 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
465 if (!transition_failed)
468 swap(freqs->old, freqs->new);
469 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
470 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
473 void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
474 struct cpufreq_freqs *freqs)
478 * Catch double invocations of _begin() which lead to self-deadlock.
479 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
480 * doesn't invoke _begin() on their behalf, and hence the chances of
481 * double invocations are very low. Moreover, there are scenarios
482 * where these checks can emit false-positive warnings in these
483 * drivers; so we avoid that by skipping them altogether.
485 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
486 && current == policy->transition_task);
489 wait_event(policy->transition_wait, !policy->transition_ongoing);
491 spin_lock(&policy->transition_lock);
493 if (unlikely(policy->transition_ongoing)) {
494 spin_unlock(&policy->transition_lock);
498 policy->transition_ongoing = true;
499 policy->transition_task = current;
501 spin_unlock(&policy->transition_lock);
503 scale_freq_capacity(policy, freqs);
505 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
507 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
509 void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
510 struct cpufreq_freqs *freqs, int transition_failed)
512 if (unlikely(WARN_ON(!policy->transition_ongoing)))
515 cpufreq_notify_post_transition(policy, freqs, transition_failed);
517 policy->transition_ongoing = false;
518 policy->transition_task = NULL;
520 wake_up(&policy->transition_wait);
522 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
525 /*********************************************************************
527 *********************************************************************/
528 static ssize_t show_boost(struct kobject *kobj,
529 struct attribute *attr, char *buf)
531 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
534 static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
535 const char *buf, size_t count)
539 ret = sscanf(buf, "%d", &enable);
540 if (ret != 1 || enable < 0 || enable > 1)
543 if (cpufreq_boost_trigger_state(enable)) {
544 pr_err("%s: Cannot %s BOOST!\n",
545 __func__, enable ? "enable" : "disable");
549 pr_debug("%s: cpufreq BOOST %s\n",
550 __func__, enable ? "enabled" : "disabled");
554 define_one_global_rw(boost);
556 static struct cpufreq_governor *find_governor(const char *str_governor)
558 struct cpufreq_governor *t;
561 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
568 * cpufreq_parse_governor - parse a governor string
570 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
571 struct cpufreq_governor **governor)
575 if (cpufreq_driver->setpolicy) {
576 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
577 *policy = CPUFREQ_POLICY_PERFORMANCE;
579 } else if (!strncasecmp(str_governor, "powersave",
581 *policy = CPUFREQ_POLICY_POWERSAVE;
585 struct cpufreq_governor *t;
587 mutex_lock(&cpufreq_governor_mutex);
589 t = find_governor(str_governor);
594 mutex_unlock(&cpufreq_governor_mutex);
595 ret = request_module("cpufreq_%s", str_governor);
596 mutex_lock(&cpufreq_governor_mutex);
599 t = find_governor(str_governor);
607 mutex_unlock(&cpufreq_governor_mutex);
613 * cpufreq_per_cpu_attr_read() / show_##file_name() -
614 * print out cpufreq information
616 * Write out information from cpufreq_driver->policy[cpu]; object must be
620 #define show_one(file_name, object) \
621 static ssize_t show_##file_name \
622 (struct cpufreq_policy *policy, char *buf) \
624 return sprintf(buf, "%u\n", policy->object); \
627 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
628 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
629 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
630 show_one(scaling_min_freq, min);
631 show_one(scaling_max_freq, max);
633 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
637 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
638 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
640 ret = sprintf(buf, "%u\n", policy->cur);
644 static int cpufreq_set_policy(struct cpufreq_policy *policy,
645 struct cpufreq_policy *new_policy);
648 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
650 #define store_one(file_name, object) \
651 static ssize_t store_##file_name \
652 (struct cpufreq_policy *policy, const char *buf, size_t count) \
655 struct cpufreq_policy new_policy; \
657 memcpy(&new_policy, policy, sizeof(*policy)); \
659 ret = sscanf(buf, "%u", &new_policy.object); \
663 temp = new_policy.object; \
664 ret = cpufreq_set_policy(policy, &new_policy); \
666 policy->user_policy.object = temp; \
668 return ret ? ret : count; \
671 store_one(scaling_min_freq, min);
672 store_one(scaling_max_freq, max);
675 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
677 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
680 unsigned int cur_freq = __cpufreq_get(policy);
682 return sprintf(buf, "<unknown>");
683 return sprintf(buf, "%u\n", cur_freq);
687 * show_scaling_governor - show the current policy for the specified CPU
689 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
691 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
692 return sprintf(buf, "powersave\n");
693 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
694 return sprintf(buf, "performance\n");
695 else if (policy->governor)
696 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
697 policy->governor->name);
702 * store_scaling_governor - store policy for the specified CPU
704 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
705 const char *buf, size_t count)
708 char str_governor[16];
709 struct cpufreq_policy new_policy;
711 memcpy(&new_policy, policy, sizeof(*policy));
713 ret = sscanf(buf, "%15s", str_governor);
717 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
718 &new_policy.governor))
721 ret = cpufreq_set_policy(policy, &new_policy);
722 return ret ? ret : count;
726 * show_scaling_driver - show the cpufreq driver currently loaded
728 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
730 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
734 * show_scaling_available_governors - show the available CPUfreq governors
736 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
740 struct cpufreq_governor *t;
743 i += sprintf(buf, "performance powersave");
747 for_each_governor(t) {
748 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
749 - (CPUFREQ_NAME_LEN + 2)))
751 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
754 i += sprintf(&buf[i], "\n");
758 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
763 for_each_cpu(cpu, mask) {
765 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
766 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
767 if (i >= (PAGE_SIZE - 5))
770 i += sprintf(&buf[i], "\n");
773 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
776 * show_related_cpus - show the CPUs affected by each transition even if
777 * hw coordination is in use
779 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
781 return cpufreq_show_cpus(policy->related_cpus, buf);
785 * show_affected_cpus - show the CPUs affected by each transition
787 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
789 return cpufreq_show_cpus(policy->cpus, buf);
792 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
793 const char *buf, size_t count)
795 unsigned int freq = 0;
798 if (!policy->governor || !policy->governor->store_setspeed)
801 ret = sscanf(buf, "%u", &freq);
805 policy->governor->store_setspeed(policy, freq);
810 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
812 if (!policy->governor || !policy->governor->show_setspeed)
813 return sprintf(buf, "<unsupported>\n");
815 return policy->governor->show_setspeed(policy, buf);
819 * show_bios_limit - show the current cpufreq HW/BIOS limitation
821 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
825 if (cpufreq_driver->bios_limit) {
826 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
828 return sprintf(buf, "%u\n", limit);
830 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
833 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
834 cpufreq_freq_attr_ro(cpuinfo_min_freq);
835 cpufreq_freq_attr_ro(cpuinfo_max_freq);
836 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
837 cpufreq_freq_attr_ro(scaling_available_governors);
838 cpufreq_freq_attr_ro(scaling_driver);
839 cpufreq_freq_attr_ro(scaling_cur_freq);
840 cpufreq_freq_attr_ro(bios_limit);
841 cpufreq_freq_attr_ro(related_cpus);
842 cpufreq_freq_attr_ro(affected_cpus);
843 cpufreq_freq_attr_rw(scaling_min_freq);
844 cpufreq_freq_attr_rw(scaling_max_freq);
845 cpufreq_freq_attr_rw(scaling_governor);
846 cpufreq_freq_attr_rw(scaling_setspeed);
848 static struct attribute *default_attrs[] = {
849 &cpuinfo_min_freq.attr,
850 &cpuinfo_max_freq.attr,
851 &cpuinfo_transition_latency.attr,
852 &scaling_min_freq.attr,
853 &scaling_max_freq.attr,
856 &scaling_governor.attr,
857 &scaling_driver.attr,
858 &scaling_available_governors.attr,
859 &scaling_setspeed.attr,
863 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
864 #define to_attr(a) container_of(a, struct freq_attr, attr)
866 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
868 struct cpufreq_policy *policy = to_policy(kobj);
869 struct freq_attr *fattr = to_attr(attr);
872 down_read(&policy->rwsem);
875 ret = fattr->show(policy, buf);
879 up_read(&policy->rwsem);
884 static ssize_t store(struct kobject *kobj, struct attribute *attr,
885 const char *buf, size_t count)
887 struct cpufreq_policy *policy = to_policy(kobj);
888 struct freq_attr *fattr = to_attr(attr);
889 ssize_t ret = -EINVAL;
893 if (!cpu_online(policy->cpu))
896 down_write(&policy->rwsem);
899 ret = fattr->store(policy, buf, count);
903 up_write(&policy->rwsem);
910 static void cpufreq_sysfs_release(struct kobject *kobj)
912 struct cpufreq_policy *policy = to_policy(kobj);
913 pr_debug("last reference is dropped\n");
914 complete(&policy->kobj_unregister);
917 static const struct sysfs_ops sysfs_ops = {
922 static struct kobj_type ktype_cpufreq = {
923 .sysfs_ops = &sysfs_ops,
924 .default_attrs = default_attrs,
925 .release = cpufreq_sysfs_release,
928 static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
930 struct device *cpu_dev;
932 pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu);
937 cpu_dev = get_cpu_device(cpu);
938 if (WARN_ON(!cpu_dev))
941 return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq");
944 static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
946 struct device *cpu_dev;
948 pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu);
950 cpu_dev = get_cpu_device(cpu);
951 if (WARN_ON(!cpu_dev))
954 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
957 /* Add/remove symlinks for all related CPUs */
958 static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
963 /* Some related CPUs might not be present (physically hotplugged) */
964 for_each_cpu(j, policy->real_cpus) {
965 ret = add_cpu_dev_symlink(policy, j);
973 static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy)
977 /* Some related CPUs might not be present (physically hotplugged) */
978 for_each_cpu(j, policy->real_cpus)
979 remove_cpu_dev_symlink(policy, j);
982 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
984 struct freq_attr **drv_attr;
987 /* set up files for this cpu device */
988 drv_attr = cpufreq_driver->attr;
989 while (drv_attr && *drv_attr) {
990 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
995 if (cpufreq_driver->get) {
996 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1001 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1005 if (cpufreq_driver->bios_limit) {
1006 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1011 return cpufreq_add_dev_symlink(policy);
1014 static int cpufreq_init_policy(struct cpufreq_policy *policy)
1016 struct cpufreq_governor *gov = NULL;
1017 struct cpufreq_policy new_policy;
1019 memcpy(&new_policy, policy, sizeof(*policy));
1021 /* Update governor of new_policy to the governor used before hotplug */
1022 gov = find_governor(policy->last_governor);
1024 pr_debug("Restoring governor %s for cpu %d\n",
1025 policy->governor->name, policy->cpu);
1027 gov = CPUFREQ_DEFAULT_GOVERNOR;
1029 new_policy.governor = gov;
1031 /* Use the default policy if there is no last_policy. */
1032 if (cpufreq_driver->setpolicy) {
1033 if (policy->last_policy)
1034 new_policy.policy = policy->last_policy;
1036 cpufreq_parse_governor(gov->name, &new_policy.policy,
1039 /* set default policy */
1040 return cpufreq_set_policy(policy, &new_policy);
1043 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1047 /* Has this CPU been taken care of already? */
1048 if (cpumask_test_cpu(cpu, policy->cpus))
1052 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1054 pr_err("%s: Failed to stop governor\n", __func__);
1059 down_write(&policy->rwsem);
1060 cpumask_set_cpu(cpu, policy->cpus);
1061 up_write(&policy->rwsem);
1064 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1066 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1069 pr_err("%s: Failed to start governor\n", __func__);
1077 static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1079 struct device *dev = get_cpu_device(cpu);
1080 struct cpufreq_policy *policy;
1085 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1089 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1090 goto err_free_policy;
1092 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1093 goto err_free_cpumask;
1095 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1096 goto err_free_rcpumask;
1098 kobject_init(&policy->kobj, &ktype_cpufreq);
1099 INIT_LIST_HEAD(&policy->policy_list);
1100 init_rwsem(&policy->rwsem);
1101 spin_lock_init(&policy->transition_lock);
1102 init_waitqueue_head(&policy->transition_wait);
1103 init_completion(&policy->kobj_unregister);
1104 INIT_WORK(&policy->update, handle_update);
1110 free_cpumask_var(policy->related_cpus);
1112 free_cpumask_var(policy->cpus);
1119 static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify)
1121 struct kobject *kobj;
1122 struct completion *cmp;
1125 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1126 CPUFREQ_REMOVE_POLICY, policy);
1128 down_write(&policy->rwsem);
1129 cpufreq_remove_dev_symlink(policy);
1130 kobj = &policy->kobj;
1131 cmp = &policy->kobj_unregister;
1132 up_write(&policy->rwsem);
1136 * We need to make sure that the underlying kobj is
1137 * actually not referenced anymore by anybody before we
1138 * proceed with unloading.
1140 pr_debug("waiting for dropping of refcount\n");
1141 wait_for_completion(cmp);
1142 pr_debug("wait complete\n");
1145 static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
1147 unsigned long flags;
1150 /* Remove policy from list */
1151 write_lock_irqsave(&cpufreq_driver_lock, flags);
1152 list_del(&policy->policy_list);
1154 for_each_cpu(cpu, policy->related_cpus)
1155 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1156 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1158 cpufreq_policy_put_kobj(policy, notify);
1159 free_cpumask_var(policy->real_cpus);
1160 free_cpumask_var(policy->related_cpus);
1161 free_cpumask_var(policy->cpus);
1165 static int cpufreq_online(unsigned int cpu)
1167 struct cpufreq_policy *policy;
1169 unsigned long flags;
1173 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
1175 /* Check if this CPU already has a policy to manage it */
1176 policy = per_cpu(cpufreq_cpu_data, cpu);
1178 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
1179 if (!policy_is_inactive(policy))
1180 return cpufreq_add_policy_cpu(policy, cpu);
1182 /* This is the only online CPU for the policy. Start over. */
1184 down_write(&policy->rwsem);
1186 policy->governor = NULL;
1187 up_write(&policy->rwsem);
1190 policy = cpufreq_policy_alloc(cpu);
1195 cpumask_copy(policy->cpus, cpumask_of(cpu));
1197 /* call driver. From then on the cpufreq must be able
1198 * to accept all calls to ->verify and ->setpolicy for this CPU
1200 ret = cpufreq_driver->init(policy);
1202 pr_debug("initialization failed\n");
1203 goto out_free_policy;
1206 down_write(&policy->rwsem);
1209 /* related_cpus should at least include policy->cpus. */
1210 cpumask_copy(policy->related_cpus, policy->cpus);
1211 /* Remember CPUs present at the policy creation time. */
1212 cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask);
1214 /* Name and add the kobject */
1215 ret = kobject_add(&policy->kobj, cpufreq_global_kobject,
1217 cpumask_first(policy->related_cpus));
1219 pr_err("%s: failed to add policy->kobj: %d\n", __func__,
1221 goto out_exit_policy;
1226 * affected cpus must always be the one, which are online. We aren't
1227 * managing offline cpus here.
1229 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1232 policy->user_policy.min = policy->min;
1233 policy->user_policy.max = policy->max;
1235 write_lock_irqsave(&cpufreq_driver_lock, flags);
1236 for_each_cpu(j, policy->related_cpus)
1237 per_cpu(cpufreq_cpu_data, j) = policy;
1238 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1241 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1242 policy->cur = cpufreq_driver->get(policy->cpu);
1244 pr_err("%s: ->get() failed\n", __func__);
1245 goto out_exit_policy;
1250 * Sometimes boot loaders set CPU frequency to a value outside of
1251 * frequency table present with cpufreq core. In such cases CPU might be
1252 * unstable if it has to run on that frequency for long duration of time
1253 * and so its better to set it to a frequency which is specified in
1254 * freq-table. This also makes cpufreq stats inconsistent as
1255 * cpufreq-stats would fail to register because current frequency of CPU
1256 * isn't found in freq-table.
1258 * Because we don't want this change to effect boot process badly, we go
1259 * for the next freq which is >= policy->cur ('cur' must be set by now,
1260 * otherwise we will end up setting freq to lowest of the table as 'cur'
1261 * is initialized to zero).
1263 * We are passing target-freq as "policy->cur - 1" otherwise
1264 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1265 * equal to target-freq.
1267 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1269 /* Are we running at unknown frequency ? */
1270 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1271 if (ret == -EINVAL) {
1272 /* Warn user and fix it */
1273 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1274 __func__, policy->cpu, policy->cur);
1275 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1276 CPUFREQ_RELATION_L);
1279 * Reaching here after boot in a few seconds may not
1280 * mean that system will remain stable at "unknown"
1281 * frequency for longer duration. Hence, a BUG_ON().
1284 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1285 __func__, policy->cpu, policy->cur);
1289 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1290 CPUFREQ_START, policy);
1293 ret = cpufreq_add_dev_interface(policy);
1295 goto out_exit_policy;
1296 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1297 CPUFREQ_CREATE_POLICY, policy);
1299 write_lock_irqsave(&cpufreq_driver_lock, flags);
1300 list_add(&policy->policy_list, &cpufreq_policy_list);
1301 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1304 ret = cpufreq_init_policy(policy);
1306 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1307 __func__, cpu, ret);
1308 /* cpufreq_policy_free() will notify based on this */
1310 goto out_exit_policy;
1313 up_write(&policy->rwsem);
1315 kobject_uevent(&policy->kobj, KOBJ_ADD);
1317 /* Callback for handling stuff after policy is ready */
1318 if (cpufreq_driver->ready)
1319 cpufreq_driver->ready(policy);
1321 pr_debug("initialization complete\n");
1326 up_write(&policy->rwsem);
1328 if (cpufreq_driver->exit)
1329 cpufreq_driver->exit(policy);
1331 cpufreq_policy_free(policy, !new_policy);
1336 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1338 * @sif: Subsystem interface structure pointer (not used)
1340 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1342 unsigned cpu = dev->id;
1345 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1347 if (cpu_online(cpu)) {
1348 ret = cpufreq_online(cpu);
1351 * A hotplug notifier will follow and we will handle it as CPU
1352 * online then. For now, just create the sysfs link, unless
1353 * there is no policy or the link is already present.
1355 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1357 ret = policy && !cpumask_test_and_set_cpu(cpu, policy->real_cpus)
1358 ? add_cpu_dev_symlink(policy, cpu) : 0;
1364 static void cpufreq_offline_prepare(unsigned int cpu)
1366 struct cpufreq_policy *policy;
1368 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1370 policy = cpufreq_cpu_get_raw(cpu);
1372 pr_debug("%s: No cpu_data found\n", __func__);
1377 int ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1379 pr_err("%s: Failed to stop governor\n", __func__);
1382 down_write(&policy->rwsem);
1383 cpumask_clear_cpu(cpu, policy->cpus);
1385 if (policy_is_inactive(policy)) {
1387 strncpy(policy->last_governor, policy->governor->name,
1390 policy->last_policy = policy->policy;
1391 } else if (cpu == policy->cpu) {
1392 /* Nominate new CPU */
1393 policy->cpu = cpumask_any(policy->cpus);
1395 up_write(&policy->rwsem);
1397 /* Start governor again for active policy */
1398 if (!policy_is_inactive(policy)) {
1400 int ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1402 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1405 pr_err("%s: Failed to start governor\n", __func__);
1407 } else if (cpufreq_driver->stop_cpu) {
1408 cpufreq_driver->stop_cpu(policy);
1412 static void cpufreq_offline_finish(unsigned int cpu)
1414 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1417 pr_debug("%s: No cpu_data found\n", __func__);
1421 /* Only proceed for inactive policies */
1422 if (!policy_is_inactive(policy))
1425 /* If cpu is last user of policy, free policy */
1427 int ret = __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
1429 pr_err("%s: Failed to exit governor\n", __func__);
1433 * Perform the ->exit() even during light-weight tear-down,
1434 * since this is a core component, and is essential for the
1435 * subsequent light-weight ->init() to succeed.
1437 if (cpufreq_driver->exit) {
1438 cpufreq_driver->exit(policy);
1439 policy->freq_table = NULL;
1444 * cpufreq_remove_dev - remove a CPU device
1446 * Removes the cpufreq interface for a CPU device.
1448 static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1450 unsigned int cpu = dev->id;
1451 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1456 if (cpu_online(cpu)) {
1457 cpufreq_offline_prepare(cpu);
1458 cpufreq_offline_finish(cpu);
1461 cpumask_clear_cpu(cpu, policy->real_cpus);
1462 remove_cpu_dev_symlink(policy, cpu);
1464 if (cpumask_empty(policy->real_cpus))
1465 cpufreq_policy_free(policy, true);
1468 static void handle_update(struct work_struct *work)
1470 struct cpufreq_policy *policy =
1471 container_of(work, struct cpufreq_policy, update);
1472 unsigned int cpu = policy->cpu;
1473 pr_debug("handle_update for cpu %u called\n", cpu);
1474 cpufreq_update_policy(cpu);
1478 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1480 * @policy: policy managing CPUs
1481 * @new_freq: CPU frequency the CPU actually runs at
1483 * We adjust to current frequency first, and need to clean up later.
1484 * So either call to cpufreq_update_policy() or schedule handle_update()).
1486 static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1487 unsigned int new_freq)
1489 struct cpufreq_freqs freqs;
1491 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1492 policy->cur, new_freq);
1494 freqs.old = policy->cur;
1495 freqs.new = new_freq;
1497 cpufreq_freq_transition_begin(policy, &freqs);
1498 cpufreq_freq_transition_end(policy, &freqs, 0);
1502 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1505 * This is the last known freq, without actually getting it from the driver.
1506 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1508 unsigned int cpufreq_quick_get(unsigned int cpu)
1510 struct cpufreq_policy *policy;
1511 unsigned int ret_freq = 0;
1513 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1514 return cpufreq_driver->get(cpu);
1516 policy = cpufreq_cpu_get(cpu);
1518 ret_freq = policy->cur;
1519 cpufreq_cpu_put(policy);
1524 EXPORT_SYMBOL(cpufreq_quick_get);
1527 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1530 * Just return the max possible frequency for a given CPU.
1532 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1534 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1535 unsigned int ret_freq = 0;
1538 ret_freq = policy->max;
1539 cpufreq_cpu_put(policy);
1544 EXPORT_SYMBOL(cpufreq_quick_get_max);
1546 static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
1548 unsigned int ret_freq = 0;
1550 if (!cpufreq_driver->get)
1553 ret_freq = cpufreq_driver->get(policy->cpu);
1555 /* Updating inactive policies is invalid, so avoid doing that. */
1556 if (unlikely(policy_is_inactive(policy)))
1559 if (ret_freq && policy->cur &&
1560 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1561 /* verify no discrepancy between actual and
1562 saved value exists */
1563 if (unlikely(ret_freq != policy->cur)) {
1564 cpufreq_out_of_sync(policy, ret_freq);
1565 schedule_work(&policy->update);
1573 * cpufreq_get - get the current CPU frequency (in kHz)
1576 * Get the CPU current (static) CPU frequency
1578 unsigned int cpufreq_get(unsigned int cpu)
1580 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1581 unsigned int ret_freq = 0;
1584 down_read(&policy->rwsem);
1585 ret_freq = __cpufreq_get(policy);
1586 up_read(&policy->rwsem);
1588 cpufreq_cpu_put(policy);
1593 EXPORT_SYMBOL(cpufreq_get);
1595 static struct subsys_interface cpufreq_interface = {
1597 .subsys = &cpu_subsys,
1598 .add_dev = cpufreq_add_dev,
1599 .remove_dev = cpufreq_remove_dev,
1603 * In case platform wants some specific frequency to be configured
1606 int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1610 if (!policy->suspend_freq) {
1611 pr_debug("%s: suspend_freq not defined\n", __func__);
1615 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1616 policy->suspend_freq);
1618 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1619 CPUFREQ_RELATION_H);
1621 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1622 __func__, policy->suspend_freq, ret);
1626 EXPORT_SYMBOL(cpufreq_generic_suspend);
1629 * cpufreq_suspend() - Suspend CPUFreq governors
1631 * Called during system wide Suspend/Hibernate cycles for suspending governors
1632 * as some platforms can't change frequency after this point in suspend cycle.
1633 * Because some of the devices (like: i2c, regulators, etc) they use for
1634 * changing frequency are suspended quickly after this point.
1636 void cpufreq_suspend(void)
1638 struct cpufreq_policy *policy;
1640 if (!cpufreq_driver)
1646 pr_debug("%s: Suspending Governors\n", __func__);
1648 for_each_active_policy(policy) {
1649 if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
1650 pr_err("%s: Failed to stop governor for policy: %p\n",
1652 else if (cpufreq_driver->suspend
1653 && cpufreq_driver->suspend(policy))
1654 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1659 cpufreq_suspended = true;
1663 * cpufreq_resume() - Resume CPUFreq governors
1665 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1666 * are suspended with cpufreq_suspend().
1668 void cpufreq_resume(void)
1670 struct cpufreq_policy *policy;
1672 if (!cpufreq_driver)
1675 cpufreq_suspended = false;
1680 pr_debug("%s: Resuming Governors\n", __func__);
1682 for_each_active_policy(policy) {
1683 if (cpufreq_driver->resume && cpufreq_driver->resume(policy))
1684 pr_err("%s: Failed to resume driver: %p\n", __func__,
1686 else if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
1687 || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
1688 pr_err("%s: Failed to start governor for policy: %p\n",
1693 * schedule call cpufreq_update_policy() for first-online CPU, as that
1694 * wouldn't be hotplugged-out on suspend. It will verify that the
1695 * current freq is in sync with what we believe it to be.
1697 policy = cpufreq_cpu_get_raw(cpumask_first(cpu_online_mask));
1698 if (WARN_ON(!policy))
1701 schedule_work(&policy->update);
1705 * cpufreq_get_current_driver - return current driver's name
1707 * Return the name string of the currently loaded cpufreq driver
1710 const char *cpufreq_get_current_driver(void)
1713 return cpufreq_driver->name;
1717 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1720 * cpufreq_get_driver_data - return current driver data
1722 * Return the private data of the currently loaded cpufreq
1723 * driver, or NULL if no cpufreq driver is loaded.
1725 void *cpufreq_get_driver_data(void)
1728 return cpufreq_driver->driver_data;
1732 EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1734 /*********************************************************************
1735 * NOTIFIER LISTS INTERFACE *
1736 *********************************************************************/
1739 * cpufreq_register_notifier - register a driver with cpufreq
1740 * @nb: notifier function to register
1741 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1743 * Add a driver to one of two lists: either a list of drivers that
1744 * are notified about clock rate changes (once before and once after
1745 * the transition), or a list of drivers that are notified about
1746 * changes in cpufreq policy.
1748 * This function may sleep, and has the same return conditions as
1749 * blocking_notifier_chain_register.
1751 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1755 if (cpufreq_disabled())
1758 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1761 case CPUFREQ_TRANSITION_NOTIFIER:
1762 ret = srcu_notifier_chain_register(
1763 &cpufreq_transition_notifier_list, nb);
1765 case CPUFREQ_POLICY_NOTIFIER:
1766 ret = blocking_notifier_chain_register(
1767 &cpufreq_policy_notifier_list, nb);
1775 EXPORT_SYMBOL(cpufreq_register_notifier);
1778 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1779 * @nb: notifier block to be unregistered
1780 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1782 * Remove a driver from the CPU frequency notifier list.
1784 * This function may sleep, and has the same return conditions as
1785 * blocking_notifier_chain_unregister.
1787 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1791 if (cpufreq_disabled())
1795 case CPUFREQ_TRANSITION_NOTIFIER:
1796 ret = srcu_notifier_chain_unregister(
1797 &cpufreq_transition_notifier_list, nb);
1799 case CPUFREQ_POLICY_NOTIFIER:
1800 ret = blocking_notifier_chain_unregister(
1801 &cpufreq_policy_notifier_list, nb);
1809 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1812 /*********************************************************************
1814 *********************************************************************/
1816 /* Must set freqs->new to intermediate frequency */
1817 static int __target_intermediate(struct cpufreq_policy *policy,
1818 struct cpufreq_freqs *freqs, int index)
1822 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1824 /* We don't need to switch to intermediate freq */
1828 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1829 __func__, policy->cpu, freqs->old, freqs->new);
1831 cpufreq_freq_transition_begin(policy, freqs);
1832 ret = cpufreq_driver->target_intermediate(policy, index);
1833 cpufreq_freq_transition_end(policy, freqs, ret);
1836 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1842 static int __target_index(struct cpufreq_policy *policy,
1843 struct cpufreq_frequency_table *freq_table, int index)
1845 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1846 unsigned int intermediate_freq = 0;
1847 int retval = -EINVAL;
1850 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1852 /* Handle switching to intermediate frequency */
1853 if (cpufreq_driver->get_intermediate) {
1854 retval = __target_intermediate(policy, &freqs, index);
1858 intermediate_freq = freqs.new;
1859 /* Set old freq to intermediate */
1860 if (intermediate_freq)
1861 freqs.old = freqs.new;
1864 freqs.new = freq_table[index].frequency;
1865 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1866 __func__, policy->cpu, freqs.old, freqs.new);
1868 cpufreq_freq_transition_begin(policy, &freqs);
1871 retval = cpufreq_driver->target_index(policy, index);
1873 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1877 cpufreq_freq_transition_end(policy, &freqs, retval);
1880 * Failed after setting to intermediate freq? Driver should have
1881 * reverted back to initial frequency and so should we. Check
1882 * here for intermediate_freq instead of get_intermediate, in
1883 * case we haven't switched to intermediate freq at all.
1885 if (unlikely(retval && intermediate_freq)) {
1886 freqs.old = intermediate_freq;
1887 freqs.new = policy->restore_freq;
1888 cpufreq_freq_transition_begin(policy, &freqs);
1889 cpufreq_freq_transition_end(policy, &freqs, 0);
1896 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1897 unsigned int target_freq,
1898 unsigned int relation)
1900 unsigned int old_target_freq = target_freq;
1901 int retval = -EINVAL;
1903 if (cpufreq_disabled())
1906 /* Make sure that target_freq is within supported range */
1907 if (target_freq > policy->max)
1908 target_freq = policy->max;
1909 if (target_freq < policy->min)
1910 target_freq = policy->min;
1912 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1913 policy->cpu, target_freq, relation, old_target_freq);
1916 * This might look like a redundant call as we are checking it again
1917 * after finding index. But it is left intentionally for cases where
1918 * exactly same freq is called again and so we can save on few function
1921 if (target_freq == policy->cur)
1924 /* Save last value to restore later on errors */
1925 policy->restore_freq = policy->cur;
1927 if (cpufreq_driver->target)
1928 retval = cpufreq_driver->target(policy, target_freq, relation);
1929 else if (cpufreq_driver->target_index) {
1930 struct cpufreq_frequency_table *freq_table;
1933 freq_table = cpufreq_frequency_get_table(policy->cpu);
1934 if (unlikely(!freq_table)) {
1935 pr_err("%s: Unable to find freq_table\n", __func__);
1939 retval = cpufreq_frequency_table_target(policy, freq_table,
1940 target_freq, relation, &index);
1941 if (unlikely(retval)) {
1942 pr_err("%s: Unable to find matching freq\n", __func__);
1946 if (freq_table[index].frequency == policy->cur) {
1951 retval = __target_index(policy, freq_table, index);
1957 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1959 int cpufreq_driver_target(struct cpufreq_policy *policy,
1960 unsigned int target_freq,
1961 unsigned int relation)
1965 down_write(&policy->rwsem);
1967 ret = __cpufreq_driver_target(policy, target_freq, relation);
1969 up_write(&policy->rwsem);
1973 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1975 static int __cpufreq_governor(struct cpufreq_policy *policy,
1980 /* Only must be defined when default governor is known to have latency
1981 restrictions, like e.g. conservative or ondemand.
1982 That this is the case is already ensured in Kconfig
1984 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1985 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1987 struct cpufreq_governor *gov = NULL;
1990 /* Don't start any governor operations if we are entering suspend */
1991 if (cpufreq_suspended)
1994 * Governor might not be initiated here if ACPI _PPC changed
1995 * notification happened, so check it.
1997 if (!policy->governor)
2000 if (policy->governor->max_transition_latency &&
2001 policy->cpuinfo.transition_latency >
2002 policy->governor->max_transition_latency) {
2006 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2007 policy->governor->name, gov->name);
2008 policy->governor = gov;
2012 if (event == CPUFREQ_GOV_POLICY_INIT)
2013 if (!try_module_get(policy->governor->owner))
2016 pr_debug("%s: for CPU %u, event %u\n", __func__, policy->cpu, event);
2018 mutex_lock(&cpufreq_governor_lock);
2019 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
2020 || (!policy->governor_enabled
2021 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
2022 mutex_unlock(&cpufreq_governor_lock);
2026 if (event == CPUFREQ_GOV_STOP)
2027 policy->governor_enabled = false;
2028 else if (event == CPUFREQ_GOV_START)
2029 policy->governor_enabled = true;
2031 mutex_unlock(&cpufreq_governor_lock);
2033 ret = policy->governor->governor(policy, event);
2036 if (event == CPUFREQ_GOV_POLICY_INIT)
2037 policy->governor->initialized++;
2038 else if (event == CPUFREQ_GOV_POLICY_EXIT)
2039 policy->governor->initialized--;
2041 /* Restore original values */
2042 mutex_lock(&cpufreq_governor_lock);
2043 if (event == CPUFREQ_GOV_STOP)
2044 policy->governor_enabled = true;
2045 else if (event == CPUFREQ_GOV_START)
2046 policy->governor_enabled = false;
2047 mutex_unlock(&cpufreq_governor_lock);
2050 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2051 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
2052 module_put(policy->governor->owner);
2057 int cpufreq_register_governor(struct cpufreq_governor *governor)
2064 if (cpufreq_disabled())
2067 mutex_lock(&cpufreq_governor_mutex);
2069 governor->initialized = 0;
2071 if (!find_governor(governor->name)) {
2073 list_add(&governor->governor_list, &cpufreq_governor_list);
2076 mutex_unlock(&cpufreq_governor_mutex);
2079 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2081 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2083 struct cpufreq_policy *policy;
2084 unsigned long flags;
2089 if (cpufreq_disabled())
2092 /* clear last_governor for all inactive policies */
2093 read_lock_irqsave(&cpufreq_driver_lock, flags);
2094 for_each_inactive_policy(policy) {
2095 if (!strcmp(policy->last_governor, governor->name)) {
2096 policy->governor = NULL;
2097 strcpy(policy->last_governor, "\0");
2100 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2102 mutex_lock(&cpufreq_governor_mutex);
2103 list_del(&governor->governor_list);
2104 mutex_unlock(&cpufreq_governor_mutex);
2107 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2110 /*********************************************************************
2111 * POLICY INTERFACE *
2112 *********************************************************************/
2115 * cpufreq_get_policy - get the current cpufreq_policy
2116 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2119 * Reads the current cpufreq policy.
2121 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2123 struct cpufreq_policy *cpu_policy;
2127 cpu_policy = cpufreq_cpu_get(cpu);
2131 memcpy(policy, cpu_policy, sizeof(*policy));
2133 cpufreq_cpu_put(cpu_policy);
2136 EXPORT_SYMBOL(cpufreq_get_policy);
2139 * policy : current policy.
2140 * new_policy: policy to be set.
2142 static int cpufreq_set_policy(struct cpufreq_policy *policy,
2143 struct cpufreq_policy *new_policy)
2145 struct cpufreq_governor *old_gov;
2148 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2149 new_policy->cpu, new_policy->min, new_policy->max);
2151 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2154 * This check works well when we store new min/max freq attributes,
2155 * because new_policy is a copy of policy with one field updated.
2157 if (new_policy->min > new_policy->max)
2160 /* verify the cpu speed can be set within this limit */
2161 ret = cpufreq_driver->verify(new_policy);
2165 /* adjust if necessary - all reasons */
2166 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2167 CPUFREQ_ADJUST, new_policy);
2170 * verify the cpu speed can be set within this limit, which might be
2171 * different to the first one
2173 ret = cpufreq_driver->verify(new_policy);
2177 /* notification of the new policy */
2178 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2179 CPUFREQ_NOTIFY, new_policy);
2181 scale_freq_capacity(new_policy, NULL);
2183 policy->min = new_policy->min;
2184 policy->max = new_policy->max;
2186 pr_debug("new min and max freqs are %u - %u kHz\n",
2187 policy->min, policy->max);
2189 if (cpufreq_driver->setpolicy) {
2190 policy->policy = new_policy->policy;
2191 pr_debug("setting range\n");
2192 return cpufreq_driver->setpolicy(new_policy);
2195 if (new_policy->governor == policy->governor)
2198 pr_debug("governor switch\n");
2200 /* save old, working values */
2201 old_gov = policy->governor;
2202 /* end old governor */
2204 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2206 /* This can happen due to race with other operations */
2207 pr_debug("%s: Failed to Stop Governor: %s (%d)\n",
2208 __func__, old_gov->name, ret);
2212 up_write(&policy->rwsem);
2213 ret = __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2214 down_write(&policy->rwsem);
2217 pr_err("%s: Failed to Exit Governor: %s (%d)\n",
2218 __func__, old_gov->name, ret);
2223 /* start new governor */
2224 policy->governor = new_policy->governor;
2225 ret = __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2227 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
2231 up_write(&policy->rwsem);
2232 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2233 down_write(&policy->rwsem);
2236 /* new governor failed, so re-start old one */
2237 pr_debug("starting governor %s failed\n", policy->governor->name);
2239 policy->governor = old_gov;
2240 if (__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT))
2241 policy->governor = NULL;
2243 __cpufreq_governor(policy, CPUFREQ_GOV_START);
2249 pr_debug("governor: change or update limits\n");
2250 return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2254 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2255 * @cpu: CPU which shall be re-evaluated
2257 * Useful for policy notifiers which have different necessities
2258 * at different times.
2260 int cpufreq_update_policy(unsigned int cpu)
2262 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2263 struct cpufreq_policy new_policy;
2269 down_write(&policy->rwsem);
2271 pr_debug("updating policy for CPU %u\n", cpu);
2272 memcpy(&new_policy, policy, sizeof(*policy));
2273 new_policy.min = policy->user_policy.min;
2274 new_policy.max = policy->user_policy.max;
2277 * BIOS might change freq behind our back
2278 * -> ask driver for current freq and notify governors about a change
2280 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
2281 new_policy.cur = cpufreq_driver->get(cpu);
2282 if (WARN_ON(!new_policy.cur)) {
2288 pr_debug("Driver did not initialize current freq\n");
2289 policy->cur = new_policy.cur;
2291 if (policy->cur != new_policy.cur && has_target())
2292 cpufreq_out_of_sync(policy, new_policy.cur);
2296 ret = cpufreq_set_policy(policy, &new_policy);
2299 up_write(&policy->rwsem);
2301 cpufreq_cpu_put(policy);
2304 EXPORT_SYMBOL(cpufreq_update_policy);
2306 static int cpufreq_cpu_callback(struct notifier_block *nfb,
2307 unsigned long action, void *hcpu)
2309 unsigned int cpu = (unsigned long)hcpu;
2311 switch (action & ~CPU_TASKS_FROZEN) {
2313 cpufreq_online(cpu);
2316 case CPU_DOWN_PREPARE:
2317 cpufreq_offline_prepare(cpu);
2321 cpufreq_offline_finish(cpu);
2324 case CPU_DOWN_FAILED:
2325 cpufreq_online(cpu);
2331 static struct notifier_block __refdata cpufreq_cpu_notifier = {
2332 .notifier_call = cpufreq_cpu_callback,
2335 /*********************************************************************
2337 *********************************************************************/
2338 static int cpufreq_boost_set_sw(int state)
2340 struct cpufreq_frequency_table *freq_table;
2341 struct cpufreq_policy *policy;
2344 for_each_active_policy(policy) {
2345 freq_table = cpufreq_frequency_get_table(policy->cpu);
2347 ret = cpufreq_frequency_table_cpuinfo(policy,
2350 pr_err("%s: Policy frequency update failed\n",
2354 policy->user_policy.max = policy->max;
2355 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2362 int cpufreq_boost_trigger_state(int state)
2364 unsigned long flags;
2367 if (cpufreq_driver->boost_enabled == state)
2370 write_lock_irqsave(&cpufreq_driver_lock, flags);
2371 cpufreq_driver->boost_enabled = state;
2372 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2374 ret = cpufreq_driver->set_boost(state);
2376 write_lock_irqsave(&cpufreq_driver_lock, flags);
2377 cpufreq_driver->boost_enabled = !state;
2378 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2380 pr_err("%s: Cannot %s BOOST\n",
2381 __func__, state ? "enable" : "disable");
2387 int cpufreq_boost_supported(void)
2389 if (likely(cpufreq_driver))
2390 return cpufreq_driver->boost_supported;
2394 EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2396 static int create_boost_sysfs_file(void)
2400 if (!cpufreq_boost_supported())
2404 * Check if driver provides function to enable boost -
2405 * if not, use cpufreq_boost_set_sw as default
2407 if (!cpufreq_driver->set_boost)
2408 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2410 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
2412 pr_err("%s: cannot register global BOOST sysfs file\n",
2418 static void remove_boost_sysfs_file(void)
2420 if (cpufreq_boost_supported())
2421 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
2424 int cpufreq_enable_boost_support(void)
2426 if (!cpufreq_driver)
2429 if (cpufreq_boost_supported())
2432 cpufreq_driver->boost_supported = true;
2434 /* This will get removed on driver unregister */
2435 return create_boost_sysfs_file();
2437 EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2439 int cpufreq_boost_enabled(void)
2441 return cpufreq_driver->boost_enabled;
2443 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2445 /*********************************************************************
2446 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2447 *********************************************************************/
2450 * cpufreq_register_driver - register a CPU Frequency driver
2451 * @driver_data: A struct cpufreq_driver containing the values#
2452 * submitted by the CPU Frequency driver.
2454 * Registers a CPU Frequency driver to this core code. This code
2455 * returns zero on success, -EBUSY when another driver got here first
2456 * (and isn't unregistered in the meantime).
2459 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2461 unsigned long flags;
2464 if (cpufreq_disabled())
2467 if (!driver_data || !driver_data->verify || !driver_data->init ||
2468 !(driver_data->setpolicy || driver_data->target_index ||
2469 driver_data->target) ||
2470 (driver_data->setpolicy && (driver_data->target_index ||
2471 driver_data->target)) ||
2472 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
2475 pr_debug("trying to register driver %s\n", driver_data->name);
2477 /* Protect against concurrent CPU online/offline. */
2480 write_lock_irqsave(&cpufreq_driver_lock, flags);
2481 if (cpufreq_driver) {
2482 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2486 cpufreq_driver = driver_data;
2487 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2489 if (driver_data->setpolicy)
2490 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2492 ret = create_boost_sysfs_file();
2494 goto err_null_driver;
2496 ret = subsys_interface_register(&cpufreq_interface);
2498 goto err_boost_unreg;
2500 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2501 list_empty(&cpufreq_policy_list)) {
2502 /* if all ->init() calls failed, unregister */
2503 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2508 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2509 pr_debug("driver %s up and running\n", driver_data->name);
2516 subsys_interface_unregister(&cpufreq_interface);
2518 remove_boost_sysfs_file();
2520 write_lock_irqsave(&cpufreq_driver_lock, flags);
2521 cpufreq_driver = NULL;
2522 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2525 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2528 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2530 * Unregister the current CPUFreq driver. Only call this if you have
2531 * the right to do so, i.e. if you have succeeded in initialising before!
2532 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2533 * currently not initialised.
2535 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2537 unsigned long flags;
2539 if (!cpufreq_driver || (driver != cpufreq_driver))
2542 pr_debug("unregistering driver %s\n", driver->name);
2544 /* Protect against concurrent cpu hotplug */
2546 subsys_interface_unregister(&cpufreq_interface);
2547 remove_boost_sysfs_file();
2548 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2550 write_lock_irqsave(&cpufreq_driver_lock, flags);
2552 cpufreq_driver = NULL;
2554 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2559 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2562 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2563 * or mutexes when secondary CPUs are halted.
2565 static struct syscore_ops cpufreq_syscore_ops = {
2566 .shutdown = cpufreq_suspend,
2569 struct kobject *cpufreq_global_kobject;
2570 EXPORT_SYMBOL(cpufreq_global_kobject);
2572 static int __init cpufreq_core_init(void)
2574 if (cpufreq_disabled())
2577 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
2578 BUG_ON(!cpufreq_global_kobject);
2580 register_syscore_ops(&cpufreq_syscore_ops);
2584 core_initcall(cpufreq_core_init);