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/cpu_cooling.h>
23 #include <linux/delay.h>
24 #include <linux/device.h>
25 #include <linux/init.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/module.h>
28 #include <linux/mutex.h>
29 #include <linux/slab.h>
30 #include <linux/suspend.h>
31 #include <linux/syscore_ops.h>
32 #include <linux/tick.h>
33 #include <trace/events/power.h>
35 static LIST_HEAD(cpufreq_policy_list);
37 static inline bool policy_is_inactive(struct cpufreq_policy *policy)
39 return cpumask_empty(policy->cpus);
42 /* Macros to iterate over CPU policies */
43 #define for_each_suitable_policy(__policy, __active) \
44 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
45 if ((__active) == !policy_is_inactive(__policy))
47 #define for_each_active_policy(__policy) \
48 for_each_suitable_policy(__policy, true)
49 #define for_each_inactive_policy(__policy) \
50 for_each_suitable_policy(__policy, false)
52 #define for_each_policy(__policy) \
53 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
55 /* Iterate over governors */
56 static LIST_HEAD(cpufreq_governor_list);
57 #define for_each_governor(__governor) \
58 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
61 * The "cpufreq driver" - the arch- or hardware-dependent low
62 * level driver of CPUFreq support, and its spinlock. This lock
63 * also protects the cpufreq_cpu_data array.
65 static struct cpufreq_driver *cpufreq_driver;
66 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
67 static DEFINE_RWLOCK(cpufreq_driver_lock);
69 /* Flag to suspend/resume CPUFreq governors */
70 static bool cpufreq_suspended;
72 static inline bool has_target(void)
74 return cpufreq_driver->target_index || cpufreq_driver->target;
77 /* internal prototypes */
78 static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
79 static int cpufreq_init_governor(struct cpufreq_policy *policy);
80 static void cpufreq_exit_governor(struct cpufreq_policy *policy);
81 static int cpufreq_start_governor(struct cpufreq_policy *policy);
82 static void cpufreq_stop_governor(struct cpufreq_policy *policy);
83 static void cpufreq_governor_limits(struct cpufreq_policy *policy);
86 * Two notifier lists: the "policy" list is involved in the
87 * validation process for a new CPU frequency policy; the
88 * "transition" list for kernel code that needs to handle
89 * changes to devices when the CPU clock speed changes.
90 * The mutex locks both lists.
92 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
93 SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
95 static int off __read_mostly;
96 static int cpufreq_disabled(void)
100 void disable_cpufreq(void)
104 static DEFINE_MUTEX(cpufreq_governor_mutex);
106 bool have_governor_per_policy(void)
108 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
110 EXPORT_SYMBOL_GPL(have_governor_per_policy);
112 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
114 if (have_governor_per_policy())
115 return &policy->kobj;
117 return cpufreq_global_kobject;
119 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
121 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
127 cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
129 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
130 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
131 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
132 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
133 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
134 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
136 idle_time = cur_wall_time - busy_time;
138 *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
140 return div_u64(idle_time, NSEC_PER_USEC);
143 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
145 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
147 if (idle_time == -1ULL)
148 return get_cpu_idle_time_jiffy(cpu, wall);
150 idle_time += get_cpu_iowait_time_us(cpu, wall);
154 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
156 __weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
157 unsigned long max_freq)
160 EXPORT_SYMBOL_GPL(arch_set_freq_scale);
163 * This is a generic cpufreq init() routine which can be used by cpufreq
164 * drivers of SMP systems. It will do following:
165 * - validate & show freq table passed
166 * - set policies transition latency
167 * - policy->cpus with all possible CPUs
169 int cpufreq_generic_init(struct cpufreq_policy *policy,
170 struct cpufreq_frequency_table *table,
171 unsigned int transition_latency)
173 policy->freq_table = table;
174 policy->cpuinfo.transition_latency = transition_latency;
177 * The driver only supports the SMP configuration where all processors
178 * share the clock and voltage and clock.
180 cpumask_setall(policy->cpus);
184 EXPORT_SYMBOL_GPL(cpufreq_generic_init);
186 struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
188 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
190 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
192 EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
194 unsigned int cpufreq_generic_get(unsigned int cpu)
196 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
198 if (!policy || IS_ERR(policy->clk)) {
199 pr_err("%s: No %s associated to cpu: %d\n",
200 __func__, policy ? "clk" : "policy", cpu);
204 return clk_get_rate(policy->clk) / 1000;
206 EXPORT_SYMBOL_GPL(cpufreq_generic_get);
209 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
211 * @cpu: cpu to find policy for.
213 * This returns policy for 'cpu', returns NULL if it doesn't exist.
214 * It also increments the kobject reference count to mark it busy and so would
215 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
216 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
217 * freed as that depends on the kobj count.
219 * Return: A valid policy on success, otherwise NULL on failure.
221 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
223 struct cpufreq_policy *policy = NULL;
226 if (WARN_ON(cpu >= nr_cpu_ids))
229 /* get the cpufreq driver */
230 read_lock_irqsave(&cpufreq_driver_lock, flags);
232 if (cpufreq_driver) {
234 policy = cpufreq_cpu_get_raw(cpu);
236 kobject_get(&policy->kobj);
239 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
243 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
246 * cpufreq_cpu_put: Decrements the usage count of a policy
248 * @policy: policy earlier returned by cpufreq_cpu_get().
250 * This decrements the kobject reference count incremented earlier by calling
253 void cpufreq_cpu_put(struct cpufreq_policy *policy)
255 kobject_put(&policy->kobj);
257 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
259 /*********************************************************************
260 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
261 *********************************************************************/
264 * adjust_jiffies - adjust the system "loops_per_jiffy"
266 * This function alters the system "loops_per_jiffy" for the clock
267 * speed change. Note that loops_per_jiffy cannot be updated on SMP
268 * systems as each CPU might be scaled differently. So, use the arch
269 * per-CPU loops_per_jiffy value wherever possible.
271 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
274 static unsigned long l_p_j_ref;
275 static unsigned int l_p_j_ref_freq;
277 if (ci->flags & CPUFREQ_CONST_LOOPS)
280 if (!l_p_j_ref_freq) {
281 l_p_j_ref = loops_per_jiffy;
282 l_p_j_ref_freq = ci->old;
283 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
284 l_p_j_ref, l_p_j_ref_freq);
286 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
287 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
289 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
290 loops_per_jiffy, ci->new);
296 * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies.
297 * @policy: cpufreq policy to enable fast frequency switching for.
298 * @freqs: contain details of the frequency update.
299 * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
301 * This function calls the transition notifiers and the "adjust_jiffies"
302 * function. It is called twice on all CPU frequency changes that have
305 static void cpufreq_notify_transition(struct cpufreq_policy *policy,
306 struct cpufreq_freqs *freqs,
309 BUG_ON(irqs_disabled());
311 if (cpufreq_disabled())
314 freqs->flags = cpufreq_driver->flags;
315 pr_debug("notification %u of frequency transition to %u kHz\n",
319 case CPUFREQ_PRECHANGE:
321 * Detect if the driver reported a value as "old frequency"
322 * which is not equal to what the cpufreq core thinks is
325 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
326 if (policy->cur && (policy->cur != freqs->old)) {
327 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
328 freqs->old, policy->cur);
329 freqs->old = policy->cur;
333 for_each_cpu(freqs->cpu, policy->cpus) {
334 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
335 CPUFREQ_PRECHANGE, freqs);
338 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
341 case CPUFREQ_POSTCHANGE:
342 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
343 pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
344 cpumask_pr_args(policy->cpus));
346 for_each_cpu(freqs->cpu, policy->cpus) {
347 trace_cpu_frequency(freqs->new, freqs->cpu);
348 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
349 CPUFREQ_POSTCHANGE, freqs);
352 cpufreq_stats_record_transition(policy, freqs->new);
353 policy->cur = freqs->new;
357 /* Do post notifications when there are chances that transition has failed */
358 static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
359 struct cpufreq_freqs *freqs, int transition_failed)
361 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
362 if (!transition_failed)
365 swap(freqs->old, freqs->new);
366 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
367 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
370 void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
371 struct cpufreq_freqs *freqs)
375 * Catch double invocations of _begin() which lead to self-deadlock.
376 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
377 * doesn't invoke _begin() on their behalf, and hence the chances of
378 * double invocations are very low. Moreover, there are scenarios
379 * where these checks can emit false-positive warnings in these
380 * drivers; so we avoid that by skipping them altogether.
382 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
383 && current == policy->transition_task);
386 wait_event(policy->transition_wait, !policy->transition_ongoing);
388 spin_lock(&policy->transition_lock);
390 if (unlikely(policy->transition_ongoing)) {
391 spin_unlock(&policy->transition_lock);
395 policy->transition_ongoing = true;
396 policy->transition_task = current;
398 spin_unlock(&policy->transition_lock);
400 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
402 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
404 void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
405 struct cpufreq_freqs *freqs, int transition_failed)
407 if (WARN_ON(!policy->transition_ongoing))
410 cpufreq_notify_post_transition(policy, freqs, transition_failed);
412 policy->transition_ongoing = false;
413 policy->transition_task = NULL;
415 wake_up(&policy->transition_wait);
417 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
420 * Fast frequency switching status count. Positive means "enabled", negative
421 * means "disabled" and 0 means "not decided yet".
423 static int cpufreq_fast_switch_count;
424 static DEFINE_MUTEX(cpufreq_fast_switch_lock);
426 static void cpufreq_list_transition_notifiers(void)
428 struct notifier_block *nb;
430 pr_info("Registered transition notifiers:\n");
432 mutex_lock(&cpufreq_transition_notifier_list.mutex);
434 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
435 pr_info("%pF\n", nb->notifier_call);
437 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
441 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
442 * @policy: cpufreq policy to enable fast frequency switching for.
444 * Try to enable fast frequency switching for @policy.
446 * The attempt will fail if there is at least one transition notifier registered
447 * at this point, as fast frequency switching is quite fundamentally at odds
448 * with transition notifiers. Thus if successful, it will make registration of
449 * transition notifiers fail going forward.
451 void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
453 lockdep_assert_held(&policy->rwsem);
455 if (!policy->fast_switch_possible)
458 mutex_lock(&cpufreq_fast_switch_lock);
459 if (cpufreq_fast_switch_count >= 0) {
460 cpufreq_fast_switch_count++;
461 policy->fast_switch_enabled = true;
463 pr_warn("CPU%u: Fast frequency switching not enabled\n",
465 cpufreq_list_transition_notifiers();
467 mutex_unlock(&cpufreq_fast_switch_lock);
469 EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
472 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
473 * @policy: cpufreq policy to disable fast frequency switching for.
475 void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
477 mutex_lock(&cpufreq_fast_switch_lock);
478 if (policy->fast_switch_enabled) {
479 policy->fast_switch_enabled = false;
480 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
481 cpufreq_fast_switch_count--;
483 mutex_unlock(&cpufreq_fast_switch_lock);
485 EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
488 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
490 * @target_freq: target frequency to resolve.
492 * The target to driver frequency mapping is cached in the policy.
494 * Return: Lowest driver-supported frequency greater than or equal to the
495 * given target_freq, subject to policy (min/max) and driver limitations.
497 unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
498 unsigned int target_freq)
500 target_freq = clamp_val(target_freq, policy->min, policy->max);
501 policy->cached_target_freq = target_freq;
503 if (cpufreq_driver->target_index) {
506 idx = cpufreq_frequency_table_target(policy, target_freq,
508 policy->cached_resolved_idx = idx;
509 return policy->freq_table[idx].frequency;
512 if (cpufreq_driver->resolve_freq)
513 return cpufreq_driver->resolve_freq(policy, target_freq);
517 EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
519 unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
521 unsigned int latency;
523 if (policy->transition_delay_us)
524 return policy->transition_delay_us;
526 latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
529 * For platforms that can change the frequency very fast (< 10
530 * us), the above formula gives a decent transition delay. But
531 * for platforms where transition_latency is in milliseconds, it
532 * ends up giving unrealistic values.
534 * Cap the default transition delay to 10 ms, which seems to be
535 * a reasonable amount of time after which we should reevaluate
538 return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
541 return LATENCY_MULTIPLIER;
543 EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
545 /*********************************************************************
547 *********************************************************************/
548 static ssize_t show_boost(struct kobject *kobj,
549 struct kobj_attribute *attr, char *buf)
551 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
554 static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
555 const char *buf, size_t count)
559 ret = sscanf(buf, "%d", &enable);
560 if (ret != 1 || enable < 0 || enable > 1)
563 if (cpufreq_boost_trigger_state(enable)) {
564 pr_err("%s: Cannot %s BOOST!\n",
565 __func__, enable ? "enable" : "disable");
569 pr_debug("%s: cpufreq BOOST %s\n",
570 __func__, enable ? "enabled" : "disabled");
574 define_one_global_rw(boost);
576 static struct cpufreq_governor *find_governor(const char *str_governor)
578 struct cpufreq_governor *t;
581 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
588 * cpufreq_parse_governor - parse a governor string
590 static int cpufreq_parse_governor(char *str_governor,
591 struct cpufreq_policy *policy)
593 if (cpufreq_driver->setpolicy) {
594 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
595 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
599 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
600 policy->policy = CPUFREQ_POLICY_POWERSAVE;
604 struct cpufreq_governor *t;
606 mutex_lock(&cpufreq_governor_mutex);
608 t = find_governor(str_governor);
612 mutex_unlock(&cpufreq_governor_mutex);
614 ret = request_module("cpufreq_%s", str_governor);
618 mutex_lock(&cpufreq_governor_mutex);
620 t = find_governor(str_governor);
622 if (t && !try_module_get(t->owner))
625 mutex_unlock(&cpufreq_governor_mutex);
628 policy->governor = t;
637 * cpufreq_per_cpu_attr_read() / show_##file_name() -
638 * print out cpufreq information
640 * Write out information from cpufreq_driver->policy[cpu]; object must be
644 #define show_one(file_name, object) \
645 static ssize_t show_##file_name \
646 (struct cpufreq_policy *policy, char *buf) \
648 return sprintf(buf, "%u\n", policy->object); \
651 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
652 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
653 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
654 show_one(scaling_min_freq, min);
655 show_one(scaling_max_freq, max);
657 __weak unsigned int arch_freq_get_on_cpu(int cpu)
662 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
667 freq = arch_freq_get_on_cpu(policy->cpu);
669 ret = sprintf(buf, "%u\n", freq);
670 else if (cpufreq_driver && cpufreq_driver->setpolicy &&
672 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
674 ret = sprintf(buf, "%u\n", policy->cur);
678 static int cpufreq_set_policy(struct cpufreq_policy *policy,
679 struct cpufreq_policy *new_policy);
682 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
684 #define store_one(file_name, object) \
685 static ssize_t store_##file_name \
686 (struct cpufreq_policy *policy, const char *buf, size_t count) \
689 struct cpufreq_policy new_policy; \
691 memcpy(&new_policy, policy, sizeof(*policy)); \
692 new_policy.min = policy->user_policy.min; \
693 new_policy.max = policy->user_policy.max; \
695 ret = sscanf(buf, "%u", &new_policy.object); \
699 temp = new_policy.object; \
700 ret = cpufreq_set_policy(policy, &new_policy); \
702 policy->user_policy.object = temp; \
704 return ret ? ret : count; \
707 store_one(scaling_min_freq, min);
708 store_one(scaling_max_freq, max);
711 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
713 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
716 unsigned int cur_freq = __cpufreq_get(policy);
719 return sprintf(buf, "%u\n", cur_freq);
721 return sprintf(buf, "<unknown>\n");
725 * show_scaling_governor - show the current policy for the specified CPU
727 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
729 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
730 return sprintf(buf, "powersave\n");
731 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
732 return sprintf(buf, "performance\n");
733 else if (policy->governor)
734 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
735 policy->governor->name);
740 * store_scaling_governor - store policy for the specified CPU
742 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
743 const char *buf, size_t count)
746 char str_governor[16];
747 struct cpufreq_policy new_policy;
749 memcpy(&new_policy, policy, sizeof(*policy));
751 ret = sscanf(buf, "%15s", str_governor);
755 if (cpufreq_parse_governor(str_governor, &new_policy))
758 ret = cpufreq_set_policy(policy, &new_policy);
760 if (new_policy.governor)
761 module_put(new_policy.governor->owner);
763 return ret ? ret : count;
767 * show_scaling_driver - show the cpufreq driver currently loaded
769 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
771 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
775 * show_scaling_available_governors - show the available CPUfreq governors
777 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
781 struct cpufreq_governor *t;
784 i += sprintf(buf, "performance powersave");
788 for_each_governor(t) {
789 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
790 - (CPUFREQ_NAME_LEN + 2)))
792 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
795 i += sprintf(&buf[i], "\n");
799 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
804 for_each_cpu(cpu, mask) {
806 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
807 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
808 if (i >= (PAGE_SIZE - 5))
811 i += sprintf(&buf[i], "\n");
814 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
817 * show_related_cpus - show the CPUs affected by each transition even if
818 * hw coordination is in use
820 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
822 return cpufreq_show_cpus(policy->related_cpus, buf);
826 * show_affected_cpus - show the CPUs affected by each transition
828 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
830 return cpufreq_show_cpus(policy->cpus, buf);
833 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
834 const char *buf, size_t count)
836 unsigned int freq = 0;
839 if (!policy->governor || !policy->governor->store_setspeed)
842 ret = sscanf(buf, "%u", &freq);
846 policy->governor->store_setspeed(policy, freq);
851 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
853 if (!policy->governor || !policy->governor->show_setspeed)
854 return sprintf(buf, "<unsupported>\n");
856 return policy->governor->show_setspeed(policy, buf);
860 * show_bios_limit - show the current cpufreq HW/BIOS limitation
862 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
866 if (cpufreq_driver->bios_limit) {
867 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
869 return sprintf(buf, "%u\n", limit);
871 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
874 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
875 cpufreq_freq_attr_ro(cpuinfo_min_freq);
876 cpufreq_freq_attr_ro(cpuinfo_max_freq);
877 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
878 cpufreq_freq_attr_ro(scaling_available_governors);
879 cpufreq_freq_attr_ro(scaling_driver);
880 cpufreq_freq_attr_ro(scaling_cur_freq);
881 cpufreq_freq_attr_ro(bios_limit);
882 cpufreq_freq_attr_ro(related_cpus);
883 cpufreq_freq_attr_ro(affected_cpus);
884 cpufreq_freq_attr_rw(scaling_min_freq);
885 cpufreq_freq_attr_rw(scaling_max_freq);
886 cpufreq_freq_attr_rw(scaling_governor);
887 cpufreq_freq_attr_rw(scaling_setspeed);
889 static struct attribute *default_attrs[] = {
890 &cpuinfo_min_freq.attr,
891 &cpuinfo_max_freq.attr,
892 &cpuinfo_transition_latency.attr,
893 &scaling_min_freq.attr,
894 &scaling_max_freq.attr,
897 &scaling_governor.attr,
898 &scaling_driver.attr,
899 &scaling_available_governors.attr,
900 &scaling_setspeed.attr,
904 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
905 #define to_attr(a) container_of(a, struct freq_attr, attr)
907 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
909 struct cpufreq_policy *policy = to_policy(kobj);
910 struct freq_attr *fattr = to_attr(attr);
913 down_read(&policy->rwsem);
914 ret = fattr->show(policy, buf);
915 up_read(&policy->rwsem);
920 static ssize_t store(struct kobject *kobj, struct attribute *attr,
921 const char *buf, size_t count)
923 struct cpufreq_policy *policy = to_policy(kobj);
924 struct freq_attr *fattr = to_attr(attr);
925 ssize_t ret = -EINVAL;
928 * cpus_read_trylock() is used here to work around a circular lock
929 * dependency problem with respect to the cpufreq_register_driver().
931 if (!cpus_read_trylock())
934 if (cpu_online(policy->cpu)) {
935 down_write(&policy->rwsem);
936 ret = fattr->store(policy, buf, count);
937 up_write(&policy->rwsem);
945 static void cpufreq_sysfs_release(struct kobject *kobj)
947 struct cpufreq_policy *policy = to_policy(kobj);
948 pr_debug("last reference is dropped\n");
949 complete(&policy->kobj_unregister);
952 static const struct sysfs_ops sysfs_ops = {
957 static struct kobj_type ktype_cpufreq = {
958 .sysfs_ops = &sysfs_ops,
959 .default_attrs = default_attrs,
960 .release = cpufreq_sysfs_release,
963 static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
965 struct device *dev = get_cpu_device(cpu);
970 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
973 dev_dbg(dev, "%s: Adding symlink\n", __func__);
974 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
975 dev_err(dev, "cpufreq symlink creation failed\n");
978 static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
981 dev_dbg(dev, "%s: Removing symlink\n", __func__);
982 sysfs_remove_link(&dev->kobj, "cpufreq");
985 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
987 struct freq_attr **drv_attr;
990 /* set up files for this cpu device */
991 drv_attr = cpufreq_driver->attr;
992 while (drv_attr && *drv_attr) {
993 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
998 if (cpufreq_driver->get) {
999 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1004 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1008 if (cpufreq_driver->bios_limit) {
1009 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1017 __weak struct cpufreq_governor *cpufreq_default_governor(void)
1022 static int cpufreq_init_policy(struct cpufreq_policy *policy)
1024 struct cpufreq_governor *gov = NULL;
1025 struct cpufreq_policy new_policy;
1027 memcpy(&new_policy, policy, sizeof(*policy));
1029 /* Update governor of new_policy to the governor used before hotplug */
1030 gov = find_governor(policy->last_governor);
1032 pr_debug("Restoring governor %s for cpu %d\n",
1033 policy->governor->name, policy->cpu);
1035 gov = cpufreq_default_governor();
1040 new_policy.governor = gov;
1042 /* Use the default policy if there is no last_policy. */
1043 if (cpufreq_driver->setpolicy) {
1044 if (policy->last_policy)
1045 new_policy.policy = policy->last_policy;
1047 cpufreq_parse_governor(gov->name, &new_policy);
1049 /* set default policy */
1050 return cpufreq_set_policy(policy, &new_policy);
1053 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1057 /* Has this CPU been taken care of already? */
1058 if (cpumask_test_cpu(cpu, policy->cpus))
1061 down_write(&policy->rwsem);
1063 cpufreq_stop_governor(policy);
1065 cpumask_set_cpu(cpu, policy->cpus);
1068 ret = cpufreq_start_governor(policy);
1070 pr_err("%s: Failed to start governor\n", __func__);
1072 up_write(&policy->rwsem);
1076 static void handle_update(struct work_struct *work)
1078 struct cpufreq_policy *policy =
1079 container_of(work, struct cpufreq_policy, update);
1080 unsigned int cpu = policy->cpu;
1081 pr_debug("handle_update for cpu %u called\n", cpu);
1082 cpufreq_update_policy(cpu);
1085 static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1087 struct cpufreq_policy *policy;
1090 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1094 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1095 goto err_free_policy;
1097 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1098 goto err_free_cpumask;
1100 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1101 goto err_free_rcpumask;
1103 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1104 cpufreq_global_kobject, "policy%u", cpu);
1106 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1107 goto err_free_real_cpus;
1110 INIT_LIST_HEAD(&policy->policy_list);
1111 init_rwsem(&policy->rwsem);
1112 spin_lock_init(&policy->transition_lock);
1113 init_waitqueue_head(&policy->transition_wait);
1114 init_completion(&policy->kobj_unregister);
1115 INIT_WORK(&policy->update, handle_update);
1121 free_cpumask_var(policy->real_cpus);
1123 free_cpumask_var(policy->related_cpus);
1125 free_cpumask_var(policy->cpus);
1132 static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
1134 struct kobject *kobj;
1135 struct completion *cmp;
1137 down_write(&policy->rwsem);
1138 cpufreq_stats_free_table(policy);
1139 kobj = &policy->kobj;
1140 cmp = &policy->kobj_unregister;
1141 up_write(&policy->rwsem);
1145 * We need to make sure that the underlying kobj is
1146 * actually not referenced anymore by anybody before we
1147 * proceed with unloading.
1149 pr_debug("waiting for dropping of refcount\n");
1150 wait_for_completion(cmp);
1151 pr_debug("wait complete\n");
1154 static void cpufreq_policy_free(struct cpufreq_policy *policy)
1156 unsigned long flags;
1159 /* Remove policy from list */
1160 write_lock_irqsave(&cpufreq_driver_lock, flags);
1161 list_del(&policy->policy_list);
1163 for_each_cpu(cpu, policy->related_cpus)
1164 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1165 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1167 cpufreq_policy_put_kobj(policy);
1168 free_cpumask_var(policy->real_cpus);
1169 free_cpumask_var(policy->related_cpus);
1170 free_cpumask_var(policy->cpus);
1174 static int cpufreq_online(unsigned int cpu)
1176 struct cpufreq_policy *policy;
1178 unsigned long flags;
1182 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
1184 /* Check if this CPU already has a policy to manage it */
1185 policy = per_cpu(cpufreq_cpu_data, cpu);
1187 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
1188 if (!policy_is_inactive(policy))
1189 return cpufreq_add_policy_cpu(policy, cpu);
1191 /* This is the only online CPU for the policy. Start over. */
1193 down_write(&policy->rwsem);
1195 policy->governor = NULL;
1196 up_write(&policy->rwsem);
1199 policy = cpufreq_policy_alloc(cpu);
1204 if (!new_policy && cpufreq_driver->online) {
1205 ret = cpufreq_driver->online(policy);
1207 pr_debug("%s: %d: initialization failed\n", __func__,
1209 goto out_exit_policy;
1212 /* Recover policy->cpus using related_cpus */
1213 cpumask_copy(policy->cpus, policy->related_cpus);
1215 cpumask_copy(policy->cpus, cpumask_of(cpu));
1218 * Call driver. From then on the cpufreq must be able
1219 * to accept all calls to ->verify and ->setpolicy for this CPU.
1221 ret = cpufreq_driver->init(policy);
1223 pr_debug("%s: %d: initialization failed\n", __func__,
1225 goto out_free_policy;
1228 ret = cpufreq_table_validate_and_sort(policy);
1230 goto out_exit_policy;
1232 /* related_cpus should at least include policy->cpus. */
1233 cpumask_copy(policy->related_cpus, policy->cpus);
1236 down_write(&policy->rwsem);
1238 * affected cpus must always be the one, which are online. We aren't
1239 * managing offline cpus here.
1241 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1244 policy->user_policy.min = policy->min;
1245 policy->user_policy.max = policy->max;
1247 for_each_cpu(j, policy->related_cpus) {
1248 per_cpu(cpufreq_cpu_data, j) = policy;
1249 add_cpu_dev_symlink(policy, j);
1252 policy->min = policy->user_policy.min;
1253 policy->max = policy->user_policy.max;
1256 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1257 policy->cur = cpufreq_driver->get(policy->cpu);
1259 pr_err("%s: ->get() failed\n", __func__);
1260 goto out_destroy_policy;
1265 * Sometimes boot loaders set CPU frequency to a value outside of
1266 * frequency table present with cpufreq core. In such cases CPU might be
1267 * unstable if it has to run on that frequency for long duration of time
1268 * and so its better to set it to a frequency which is specified in
1269 * freq-table. This also makes cpufreq stats inconsistent as
1270 * cpufreq-stats would fail to register because current frequency of CPU
1271 * isn't found in freq-table.
1273 * Because we don't want this change to effect boot process badly, we go
1274 * for the next freq which is >= policy->cur ('cur' must be set by now,
1275 * otherwise we will end up setting freq to lowest of the table as 'cur'
1276 * is initialized to zero).
1278 * We are passing target-freq as "policy->cur - 1" otherwise
1279 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1280 * equal to target-freq.
1282 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1284 /* Are we running at unknown frequency ? */
1285 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1286 if (ret == -EINVAL) {
1287 /* Warn user and fix it */
1288 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1289 __func__, policy->cpu, policy->cur);
1290 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1291 CPUFREQ_RELATION_L);
1294 * Reaching here after boot in a few seconds may not
1295 * mean that system will remain stable at "unknown"
1296 * frequency for longer duration. Hence, a BUG_ON().
1299 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1300 __func__, policy->cpu, policy->cur);
1305 ret = cpufreq_add_dev_interface(policy);
1307 goto out_destroy_policy;
1309 cpufreq_stats_create_table(policy);
1311 write_lock_irqsave(&cpufreq_driver_lock, flags);
1312 list_add(&policy->policy_list, &cpufreq_policy_list);
1313 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1316 ret = cpufreq_init_policy(policy);
1318 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1319 __func__, cpu, ret);
1320 goto out_destroy_policy;
1323 up_write(&policy->rwsem);
1325 kobject_uevent(&policy->kobj, KOBJ_ADD);
1327 /* Callback for handling stuff after policy is ready */
1328 if (cpufreq_driver->ready)
1329 cpufreq_driver->ready(policy);
1331 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1332 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV)
1333 policy->cdev = of_cpufreq_cooling_register(policy);
1335 pr_debug("initialization complete\n");
1340 for_each_cpu(j, policy->real_cpus)
1341 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1343 up_write(&policy->rwsem);
1346 if (cpufreq_driver->exit)
1347 cpufreq_driver->exit(policy);
1350 cpufreq_policy_free(policy);
1355 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1357 * @sif: Subsystem interface structure pointer (not used)
1359 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1361 struct cpufreq_policy *policy;
1362 unsigned cpu = dev->id;
1365 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1367 if (cpu_online(cpu)) {
1368 ret = cpufreq_online(cpu);
1373 /* Create sysfs link on CPU registration */
1374 policy = per_cpu(cpufreq_cpu_data, cpu);
1376 add_cpu_dev_symlink(policy, cpu);
1381 static int cpufreq_offline(unsigned int cpu)
1383 struct cpufreq_policy *policy;
1386 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1388 policy = cpufreq_cpu_get_raw(cpu);
1390 pr_debug("%s: No cpu_data found\n", __func__);
1394 down_write(&policy->rwsem);
1396 cpufreq_stop_governor(policy);
1398 cpumask_clear_cpu(cpu, policy->cpus);
1400 if (policy_is_inactive(policy)) {
1402 strncpy(policy->last_governor, policy->governor->name,
1405 policy->last_policy = policy->policy;
1406 } else if (cpu == policy->cpu) {
1407 /* Nominate new CPU */
1408 policy->cpu = cpumask_any(policy->cpus);
1411 /* Start governor again for active policy */
1412 if (!policy_is_inactive(policy)) {
1414 ret = cpufreq_start_governor(policy);
1416 pr_err("%s: Failed to start governor\n", __func__);
1422 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1423 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) {
1424 cpufreq_cooling_unregister(policy->cdev);
1425 policy->cdev = NULL;
1428 if (cpufreq_driver->stop_cpu)
1429 cpufreq_driver->stop_cpu(policy);
1432 cpufreq_exit_governor(policy);
1435 * Perform the ->offline() during light-weight tear-down, as
1436 * that allows fast recovery when the CPU comes back.
1438 if (cpufreq_driver->offline) {
1439 cpufreq_driver->offline(policy);
1440 } else if (cpufreq_driver->exit) {
1441 cpufreq_driver->exit(policy);
1442 policy->freq_table = NULL;
1446 up_write(&policy->rwsem);
1451 * cpufreq_remove_dev - remove a CPU device
1453 * Removes the cpufreq interface for a CPU device.
1455 static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1457 unsigned int cpu = dev->id;
1458 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1463 if (cpu_online(cpu))
1464 cpufreq_offline(cpu);
1466 cpumask_clear_cpu(cpu, policy->real_cpus);
1467 remove_cpu_dev_symlink(policy, dev);
1469 if (cpumask_empty(policy->real_cpus)) {
1470 /* We did light-weight exit earlier, do full tear down now */
1471 if (cpufreq_driver->offline)
1472 cpufreq_driver->exit(policy);
1474 cpufreq_policy_free(policy);
1479 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1481 * @policy: policy managing CPUs
1482 * @new_freq: CPU frequency the CPU actually runs at
1484 * We adjust to current frequency first, and need to clean up later.
1485 * So either call to cpufreq_update_policy() or schedule handle_update()).
1487 static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1488 unsigned int new_freq)
1490 struct cpufreq_freqs freqs;
1492 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1493 policy->cur, new_freq);
1495 freqs.old = policy->cur;
1496 freqs.new = new_freq;
1498 cpufreq_freq_transition_begin(policy, &freqs);
1499 cpufreq_freq_transition_end(policy, &freqs, 0);
1503 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1506 * This is the last known freq, without actually getting it from the driver.
1507 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1509 unsigned int cpufreq_quick_get(unsigned int cpu)
1511 struct cpufreq_policy *policy;
1512 unsigned int ret_freq = 0;
1513 unsigned long flags;
1515 read_lock_irqsave(&cpufreq_driver_lock, flags);
1517 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1518 ret_freq = cpufreq_driver->get(cpu);
1519 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1523 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1525 policy = cpufreq_cpu_get(cpu);
1527 ret_freq = policy->cur;
1528 cpufreq_cpu_put(policy);
1533 EXPORT_SYMBOL(cpufreq_quick_get);
1536 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1539 * Just return the max possible frequency for a given CPU.
1541 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1543 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1544 unsigned int ret_freq = 0;
1547 ret_freq = policy->max;
1548 cpufreq_cpu_put(policy);
1553 EXPORT_SYMBOL(cpufreq_quick_get_max);
1555 static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
1557 unsigned int ret_freq = 0;
1559 if (unlikely(policy_is_inactive(policy)) || !cpufreq_driver->get)
1562 ret_freq = cpufreq_driver->get(policy->cpu);
1565 * If fast frequency switching is used with the given policy, the check
1566 * against policy->cur is pointless, so skip it in that case too.
1568 if (policy->fast_switch_enabled)
1571 if (ret_freq && policy->cur &&
1572 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1573 /* verify no discrepancy between actual and
1574 saved value exists */
1575 if (unlikely(ret_freq != policy->cur)) {
1576 cpufreq_out_of_sync(policy, ret_freq);
1577 schedule_work(&policy->update);
1585 * cpufreq_get - get the current CPU frequency (in kHz)
1588 * Get the CPU current (static) CPU frequency
1590 unsigned int cpufreq_get(unsigned int cpu)
1592 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1593 unsigned int ret_freq = 0;
1596 down_read(&policy->rwsem);
1597 ret_freq = __cpufreq_get(policy);
1598 up_read(&policy->rwsem);
1600 cpufreq_cpu_put(policy);
1605 EXPORT_SYMBOL(cpufreq_get);
1607 static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1609 unsigned int new_freq;
1611 new_freq = cpufreq_driver->get(policy->cpu);
1616 pr_debug("cpufreq: Driver did not initialize current freq\n");
1617 policy->cur = new_freq;
1618 } else if (policy->cur != new_freq && has_target()) {
1619 cpufreq_out_of_sync(policy, new_freq);
1625 static struct subsys_interface cpufreq_interface = {
1627 .subsys = &cpu_subsys,
1628 .add_dev = cpufreq_add_dev,
1629 .remove_dev = cpufreq_remove_dev,
1633 * In case platform wants some specific frequency to be configured
1636 int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1640 if (!policy->suspend_freq) {
1641 pr_debug("%s: suspend_freq not defined\n", __func__);
1645 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1646 policy->suspend_freq);
1648 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1649 CPUFREQ_RELATION_H);
1651 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1652 __func__, policy->suspend_freq, ret);
1656 EXPORT_SYMBOL(cpufreq_generic_suspend);
1659 * cpufreq_suspend() - Suspend CPUFreq governors
1661 * Called during system wide Suspend/Hibernate cycles for suspending governors
1662 * as some platforms can't change frequency after this point in suspend cycle.
1663 * Because some of the devices (like: i2c, regulators, etc) they use for
1664 * changing frequency are suspended quickly after this point.
1666 void cpufreq_suspend(void)
1668 struct cpufreq_policy *policy;
1670 if (!cpufreq_driver)
1673 if (!has_target() && !cpufreq_driver->suspend)
1676 pr_debug("%s: Suspending Governors\n", __func__);
1678 for_each_active_policy(policy) {
1680 down_write(&policy->rwsem);
1681 cpufreq_stop_governor(policy);
1682 up_write(&policy->rwsem);
1685 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
1686 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1691 cpufreq_suspended = true;
1695 * cpufreq_resume() - Resume CPUFreq governors
1697 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1698 * are suspended with cpufreq_suspend().
1700 void cpufreq_resume(void)
1702 struct cpufreq_policy *policy;
1705 if (!cpufreq_driver)
1708 if (unlikely(!cpufreq_suspended))
1711 cpufreq_suspended = false;
1713 if (!has_target() && !cpufreq_driver->resume)
1716 pr_debug("%s: Resuming Governors\n", __func__);
1718 for_each_active_policy(policy) {
1719 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
1720 pr_err("%s: Failed to resume driver: %p\n", __func__,
1722 } else if (has_target()) {
1723 down_write(&policy->rwsem);
1724 ret = cpufreq_start_governor(policy);
1725 up_write(&policy->rwsem);
1728 pr_err("%s: Failed to start governor for policy: %p\n",
1735 * cpufreq_get_current_driver - return current driver's name
1737 * Return the name string of the currently loaded cpufreq driver
1740 const char *cpufreq_get_current_driver(void)
1743 return cpufreq_driver->name;
1747 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1750 * cpufreq_get_driver_data - return current driver data
1752 * Return the private data of the currently loaded cpufreq
1753 * driver, or NULL if no cpufreq driver is loaded.
1755 void *cpufreq_get_driver_data(void)
1758 return cpufreq_driver->driver_data;
1762 EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1764 /*********************************************************************
1765 * NOTIFIER LISTS INTERFACE *
1766 *********************************************************************/
1769 * cpufreq_register_notifier - register a driver with cpufreq
1770 * @nb: notifier function to register
1771 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1773 * Add a driver to one of two lists: either a list of drivers that
1774 * are notified about clock rate changes (once before and once after
1775 * the transition), or a list of drivers that are notified about
1776 * changes in cpufreq policy.
1778 * This function may sleep, and has the same return conditions as
1779 * blocking_notifier_chain_register.
1781 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1785 if (cpufreq_disabled())
1789 case CPUFREQ_TRANSITION_NOTIFIER:
1790 mutex_lock(&cpufreq_fast_switch_lock);
1792 if (cpufreq_fast_switch_count > 0) {
1793 mutex_unlock(&cpufreq_fast_switch_lock);
1796 ret = srcu_notifier_chain_register(
1797 &cpufreq_transition_notifier_list, nb);
1799 cpufreq_fast_switch_count--;
1801 mutex_unlock(&cpufreq_fast_switch_lock);
1803 case CPUFREQ_POLICY_NOTIFIER:
1804 ret = blocking_notifier_chain_register(
1805 &cpufreq_policy_notifier_list, nb);
1813 EXPORT_SYMBOL(cpufreq_register_notifier);
1816 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1817 * @nb: notifier block to be unregistered
1818 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1820 * Remove a driver from the CPU frequency notifier list.
1822 * This function may sleep, and has the same return conditions as
1823 * blocking_notifier_chain_unregister.
1825 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1829 if (cpufreq_disabled())
1833 case CPUFREQ_TRANSITION_NOTIFIER:
1834 mutex_lock(&cpufreq_fast_switch_lock);
1836 ret = srcu_notifier_chain_unregister(
1837 &cpufreq_transition_notifier_list, nb);
1838 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1839 cpufreq_fast_switch_count++;
1841 mutex_unlock(&cpufreq_fast_switch_lock);
1843 case CPUFREQ_POLICY_NOTIFIER:
1844 ret = blocking_notifier_chain_unregister(
1845 &cpufreq_policy_notifier_list, nb);
1853 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1856 /*********************************************************************
1858 *********************************************************************/
1861 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1862 * @policy: cpufreq policy to switch the frequency for.
1863 * @target_freq: New frequency to set (may be approximate).
1865 * Carry out a fast frequency switch without sleeping.
1867 * The driver's ->fast_switch() callback invoked by this function must be
1868 * suitable for being called from within RCU-sched read-side critical sections
1869 * and it is expected to select the minimum available frequency greater than or
1870 * equal to @target_freq (CPUFREQ_RELATION_L).
1872 * This function must not be called if policy->fast_switch_enabled is unset.
1874 * Governors calling this function must guarantee that it will never be invoked
1875 * twice in parallel for the same policy and that it will never be called in
1876 * parallel with either ->target() or ->target_index() for the same policy.
1878 * Returns the actual frequency set for the CPU.
1880 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
1881 * error condition, the hardware configuration must be preserved.
1883 unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1884 unsigned int target_freq)
1886 target_freq = clamp_val(target_freq, policy->min, policy->max);
1888 return cpufreq_driver->fast_switch(policy, target_freq);
1890 EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1892 /* Must set freqs->new to intermediate frequency */
1893 static int __target_intermediate(struct cpufreq_policy *policy,
1894 struct cpufreq_freqs *freqs, int index)
1898 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1900 /* We don't need to switch to intermediate freq */
1904 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1905 __func__, policy->cpu, freqs->old, freqs->new);
1907 cpufreq_freq_transition_begin(policy, freqs);
1908 ret = cpufreq_driver->target_intermediate(policy, index);
1909 cpufreq_freq_transition_end(policy, freqs, ret);
1912 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1918 static int __target_index(struct cpufreq_policy *policy, int index)
1920 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1921 unsigned int intermediate_freq = 0;
1922 unsigned int newfreq = policy->freq_table[index].frequency;
1923 int retval = -EINVAL;
1926 if (newfreq == policy->cur)
1929 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1931 /* Handle switching to intermediate frequency */
1932 if (cpufreq_driver->get_intermediate) {
1933 retval = __target_intermediate(policy, &freqs, index);
1937 intermediate_freq = freqs.new;
1938 /* Set old freq to intermediate */
1939 if (intermediate_freq)
1940 freqs.old = freqs.new;
1943 freqs.new = newfreq;
1944 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1945 __func__, policy->cpu, freqs.old, freqs.new);
1947 cpufreq_freq_transition_begin(policy, &freqs);
1950 retval = cpufreq_driver->target_index(policy, index);
1952 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1956 cpufreq_freq_transition_end(policy, &freqs, retval);
1959 * Failed after setting to intermediate freq? Driver should have
1960 * reverted back to initial frequency and so should we. Check
1961 * here for intermediate_freq instead of get_intermediate, in
1962 * case we haven't switched to intermediate freq at all.
1964 if (unlikely(retval && intermediate_freq)) {
1965 freqs.old = intermediate_freq;
1966 freqs.new = policy->restore_freq;
1967 cpufreq_freq_transition_begin(policy, &freqs);
1968 cpufreq_freq_transition_end(policy, &freqs, 0);
1975 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1976 unsigned int target_freq,
1977 unsigned int relation)
1979 unsigned int old_target_freq = target_freq;
1982 if (cpufreq_disabled())
1985 /* Make sure that target_freq is within supported range */
1986 target_freq = clamp_val(target_freq, policy->min, policy->max);
1988 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1989 policy->cpu, target_freq, relation, old_target_freq);
1992 * This might look like a redundant call as we are checking it again
1993 * after finding index. But it is left intentionally for cases where
1994 * exactly same freq is called again and so we can save on few function
1997 if (target_freq == policy->cur)
2000 /* Save last value to restore later on errors */
2001 policy->restore_freq = policy->cur;
2003 if (cpufreq_driver->target)
2004 return cpufreq_driver->target(policy, target_freq, relation);
2006 if (!cpufreq_driver->target_index)
2009 index = cpufreq_frequency_table_target(policy, target_freq, relation);
2011 return __target_index(policy, index);
2013 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2015 int cpufreq_driver_target(struct cpufreq_policy *policy,
2016 unsigned int target_freq,
2017 unsigned int relation)
2021 down_write(&policy->rwsem);
2023 ret = __cpufreq_driver_target(policy, target_freq, relation);
2025 up_write(&policy->rwsem);
2029 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2031 __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2036 static int cpufreq_init_governor(struct cpufreq_policy *policy)
2040 /* Don't start any governor operations if we are entering suspend */
2041 if (cpufreq_suspended)
2044 * Governor might not be initiated here if ACPI _PPC changed
2045 * notification happened, so check it.
2047 if (!policy->governor)
2050 /* Platform doesn't want dynamic frequency switching ? */
2051 if (policy->governor->dynamic_switching &&
2052 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
2053 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2056 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
2057 policy->governor->name, gov->name);
2058 policy->governor = gov;
2064 if (!try_module_get(policy->governor->owner))
2067 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2069 if (policy->governor->init) {
2070 ret = policy->governor->init(policy);
2072 module_put(policy->governor->owner);
2080 static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2082 if (cpufreq_suspended || !policy->governor)
2085 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2087 if (policy->governor->exit)
2088 policy->governor->exit(policy);
2090 module_put(policy->governor->owner);
2093 static int cpufreq_start_governor(struct cpufreq_policy *policy)
2097 if (cpufreq_suspended)
2100 if (!policy->governor)
2103 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2105 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2106 cpufreq_update_current_freq(policy);
2108 if (policy->governor->start) {
2109 ret = policy->governor->start(policy);
2114 if (policy->governor->limits)
2115 policy->governor->limits(policy);
2120 static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2122 if (cpufreq_suspended || !policy->governor)
2125 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2127 if (policy->governor->stop)
2128 policy->governor->stop(policy);
2131 static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2133 if (cpufreq_suspended || !policy->governor)
2136 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2138 if (policy->governor->limits)
2139 policy->governor->limits(policy);
2142 int cpufreq_register_governor(struct cpufreq_governor *governor)
2149 if (cpufreq_disabled())
2152 mutex_lock(&cpufreq_governor_mutex);
2155 if (!find_governor(governor->name)) {
2157 list_add(&governor->governor_list, &cpufreq_governor_list);
2160 mutex_unlock(&cpufreq_governor_mutex);
2163 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2165 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2167 struct cpufreq_policy *policy;
2168 unsigned long flags;
2173 if (cpufreq_disabled())
2176 /* clear last_governor for all inactive policies */
2177 read_lock_irqsave(&cpufreq_driver_lock, flags);
2178 for_each_inactive_policy(policy) {
2179 if (!strcmp(policy->last_governor, governor->name)) {
2180 policy->governor = NULL;
2181 strcpy(policy->last_governor, "\0");
2184 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2186 mutex_lock(&cpufreq_governor_mutex);
2187 list_del(&governor->governor_list);
2188 mutex_unlock(&cpufreq_governor_mutex);
2190 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2193 /*********************************************************************
2194 * POLICY INTERFACE *
2195 *********************************************************************/
2198 * cpufreq_get_policy - get the current cpufreq_policy
2199 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2202 * Reads the current cpufreq policy.
2204 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2206 struct cpufreq_policy *cpu_policy;
2210 cpu_policy = cpufreq_cpu_get(cpu);
2214 memcpy(policy, cpu_policy, sizeof(*policy));
2216 cpufreq_cpu_put(cpu_policy);
2219 EXPORT_SYMBOL(cpufreq_get_policy);
2222 * policy : current policy.
2223 * new_policy: policy to be set.
2225 static int cpufreq_set_policy(struct cpufreq_policy *policy,
2226 struct cpufreq_policy *new_policy)
2228 struct cpufreq_governor *old_gov;
2231 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2232 new_policy->cpu, new_policy->min, new_policy->max);
2234 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2237 * This check works well when we store new min/max freq attributes,
2238 * because new_policy is a copy of policy with one field updated.
2240 if (new_policy->min > new_policy->max)
2243 /* verify the cpu speed can be set within this limit */
2244 ret = cpufreq_driver->verify(new_policy);
2248 /* adjust if necessary - all reasons */
2249 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2250 CPUFREQ_ADJUST, new_policy);
2253 * verify the cpu speed can be set within this limit, which might be
2254 * different to the first one
2256 ret = cpufreq_driver->verify(new_policy);
2260 /* notification of the new policy */
2261 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2262 CPUFREQ_NOTIFY, new_policy);
2264 policy->min = new_policy->min;
2265 policy->max = new_policy->max;
2266 trace_cpu_frequency_limits(policy);
2268 policy->cached_target_freq = UINT_MAX;
2270 pr_debug("new min and max freqs are %u - %u kHz\n",
2271 policy->min, policy->max);
2273 if (cpufreq_driver->setpolicy) {
2274 policy->policy = new_policy->policy;
2275 pr_debug("setting range\n");
2276 return cpufreq_driver->setpolicy(new_policy);
2279 if (new_policy->governor == policy->governor) {
2280 pr_debug("cpufreq: governor limits update\n");
2281 cpufreq_governor_limits(policy);
2285 pr_debug("governor switch\n");
2287 /* save old, working values */
2288 old_gov = policy->governor;
2289 /* end old governor */
2291 cpufreq_stop_governor(policy);
2292 cpufreq_exit_governor(policy);
2295 /* start new governor */
2296 policy->governor = new_policy->governor;
2297 ret = cpufreq_init_governor(policy);
2299 ret = cpufreq_start_governor(policy);
2301 pr_debug("cpufreq: governor change\n");
2302 sched_cpufreq_governor_change(policy, old_gov);
2305 cpufreq_exit_governor(policy);
2308 /* new governor failed, so re-start old one */
2309 pr_debug("starting governor %s failed\n", policy->governor->name);
2311 policy->governor = old_gov;
2312 if (cpufreq_init_governor(policy))
2313 policy->governor = NULL;
2315 cpufreq_start_governor(policy);
2322 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2323 * @cpu: CPU which shall be re-evaluated
2325 * Useful for policy notifiers which have different necessities
2326 * at different times.
2328 void cpufreq_update_policy(unsigned int cpu)
2330 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2331 struct cpufreq_policy new_policy;
2336 down_write(&policy->rwsem);
2338 if (policy_is_inactive(policy))
2341 pr_debug("updating policy for CPU %u\n", cpu);
2342 memcpy(&new_policy, policy, sizeof(*policy));
2343 new_policy.min = policy->user_policy.min;
2344 new_policy.max = policy->user_policy.max;
2347 * BIOS might change freq behind our back
2348 * -> ask driver for current freq and notify governors about a change
2350 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
2351 if (cpufreq_suspended)
2354 new_policy.cur = cpufreq_update_current_freq(policy);
2355 if (WARN_ON(!new_policy.cur))
2359 cpufreq_set_policy(policy, &new_policy);
2362 up_write(&policy->rwsem);
2364 cpufreq_cpu_put(policy);
2366 EXPORT_SYMBOL(cpufreq_update_policy);
2368 /*********************************************************************
2370 *********************************************************************/
2371 static int cpufreq_boost_set_sw(int state)
2373 struct cpufreq_policy *policy;
2376 for_each_active_policy(policy) {
2377 if (!policy->freq_table)
2380 ret = cpufreq_frequency_table_cpuinfo(policy,
2381 policy->freq_table);
2383 pr_err("%s: Policy frequency update failed\n",
2388 down_write(&policy->rwsem);
2389 policy->user_policy.max = policy->max;
2390 cpufreq_governor_limits(policy);
2391 up_write(&policy->rwsem);
2397 int cpufreq_boost_trigger_state(int state)
2399 unsigned long flags;
2402 if (cpufreq_driver->boost_enabled == state)
2405 write_lock_irqsave(&cpufreq_driver_lock, flags);
2406 cpufreq_driver->boost_enabled = state;
2407 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2409 ret = cpufreq_driver->set_boost(state);
2411 write_lock_irqsave(&cpufreq_driver_lock, flags);
2412 cpufreq_driver->boost_enabled = !state;
2413 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2415 pr_err("%s: Cannot %s BOOST\n",
2416 __func__, state ? "enable" : "disable");
2422 static bool cpufreq_boost_supported(void)
2424 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
2427 static int create_boost_sysfs_file(void)
2431 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
2433 pr_err("%s: cannot register global BOOST sysfs file\n",
2439 static void remove_boost_sysfs_file(void)
2441 if (cpufreq_boost_supported())
2442 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
2445 int cpufreq_enable_boost_support(void)
2447 if (!cpufreq_driver)
2450 if (cpufreq_boost_supported())
2453 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2455 /* This will get removed on driver unregister */
2456 return create_boost_sysfs_file();
2458 EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2460 int cpufreq_boost_enabled(void)
2462 return cpufreq_driver->boost_enabled;
2464 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2466 /*********************************************************************
2467 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2468 *********************************************************************/
2469 static enum cpuhp_state hp_online;
2471 static int cpuhp_cpufreq_online(unsigned int cpu)
2473 cpufreq_online(cpu);
2478 static int cpuhp_cpufreq_offline(unsigned int cpu)
2480 cpufreq_offline(cpu);
2486 * cpufreq_register_driver - register a CPU Frequency driver
2487 * @driver_data: A struct cpufreq_driver containing the values#
2488 * submitted by the CPU Frequency driver.
2490 * Registers a CPU Frequency driver to this core code. This code
2491 * returns zero on success, -EEXIST when another driver got here first
2492 * (and isn't unregistered in the meantime).
2495 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2497 unsigned long flags;
2500 if (cpufreq_disabled())
2503 if (!driver_data || !driver_data->verify || !driver_data->init ||
2504 !(driver_data->setpolicy || driver_data->target_index ||
2505 driver_data->target) ||
2506 (driver_data->setpolicy && (driver_data->target_index ||
2507 driver_data->target)) ||
2508 (!!driver_data->get_intermediate != !!driver_data->target_intermediate) ||
2509 (!driver_data->online != !driver_data->offline))
2512 pr_debug("trying to register driver %s\n", driver_data->name);
2514 /* Protect against concurrent CPU online/offline. */
2517 write_lock_irqsave(&cpufreq_driver_lock, flags);
2518 if (cpufreq_driver) {
2519 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2523 cpufreq_driver = driver_data;
2524 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2526 if (driver_data->setpolicy)
2527 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2529 if (cpufreq_boost_supported()) {
2530 ret = create_boost_sysfs_file();
2532 goto err_null_driver;
2535 ret = subsys_interface_register(&cpufreq_interface);
2537 goto err_boost_unreg;
2539 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2540 list_empty(&cpufreq_policy_list)) {
2541 /* if all ->init() calls failed, unregister */
2543 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2548 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2550 cpuhp_cpufreq_online,
2551 cpuhp_cpufreq_offline);
2557 pr_debug("driver %s up and running\n", driver_data->name);
2561 subsys_interface_unregister(&cpufreq_interface);
2563 remove_boost_sysfs_file();
2565 write_lock_irqsave(&cpufreq_driver_lock, flags);
2566 cpufreq_driver = NULL;
2567 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2572 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2575 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2577 * Unregister the current CPUFreq driver. Only call this if you have
2578 * the right to do so, i.e. if you have succeeded in initialising before!
2579 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2580 * currently not initialised.
2582 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2584 unsigned long flags;
2586 if (!cpufreq_driver || (driver != cpufreq_driver))
2589 pr_debug("unregistering driver %s\n", driver->name);
2591 /* Protect against concurrent cpu hotplug */
2593 subsys_interface_unregister(&cpufreq_interface);
2594 remove_boost_sysfs_file();
2595 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
2597 write_lock_irqsave(&cpufreq_driver_lock, flags);
2599 cpufreq_driver = NULL;
2601 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2606 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2609 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2610 * or mutexes when secondary CPUs are halted.
2612 static struct syscore_ops cpufreq_syscore_ops = {
2613 .shutdown = cpufreq_suspend,
2616 struct kobject *cpufreq_global_kobject;
2617 EXPORT_SYMBOL(cpufreq_global_kobject);
2619 static int __init cpufreq_core_init(void)
2621 if (cpufreq_disabled())
2624 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
2625 BUG_ON(!cpufreq_global_kobject);
2627 register_syscore_ops(&cpufreq_syscore_ops);
2631 module_param(off, int, 0444);
2632 core_initcall(cpufreq_core_init);