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 - Return policy for a CPU and mark it as busy.
210 * @cpu: CPU to find the policy for.
212 * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
213 * the kobject reference counter of that policy. Return a valid policy on
214 * success or NULL on failure.
216 * The policy returned by this function has to be released with the help of
217 * cpufreq_cpu_put() to balance its kobject reference counter properly.
219 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
221 struct cpufreq_policy *policy = NULL;
224 if (WARN_ON(cpu >= nr_cpu_ids))
227 /* get the cpufreq driver */
228 read_lock_irqsave(&cpufreq_driver_lock, flags);
230 if (cpufreq_driver) {
232 policy = cpufreq_cpu_get_raw(cpu);
234 kobject_get(&policy->kobj);
237 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
241 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
244 * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
245 * @policy: cpufreq policy returned by cpufreq_cpu_get().
247 void cpufreq_cpu_put(struct cpufreq_policy *policy)
249 kobject_put(&policy->kobj);
251 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
253 /*********************************************************************
254 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
255 *********************************************************************/
258 * adjust_jiffies - adjust the system "loops_per_jiffy"
260 * This function alters the system "loops_per_jiffy" for the clock
261 * speed change. Note that loops_per_jiffy cannot be updated on SMP
262 * systems as each CPU might be scaled differently. So, use the arch
263 * per-CPU loops_per_jiffy value wherever possible.
265 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
268 static unsigned long l_p_j_ref;
269 static unsigned int l_p_j_ref_freq;
271 if (ci->flags & CPUFREQ_CONST_LOOPS)
274 if (!l_p_j_ref_freq) {
275 l_p_j_ref = loops_per_jiffy;
276 l_p_j_ref_freq = ci->old;
277 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
278 l_p_j_ref, l_p_j_ref_freq);
280 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
281 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
283 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
284 loops_per_jiffy, ci->new);
290 * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies.
291 * @policy: cpufreq policy to enable fast frequency switching for.
292 * @freqs: contain details of the frequency update.
293 * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
295 * This function calls the transition notifiers and the "adjust_jiffies"
296 * function. It is called twice on all CPU frequency changes that have
299 static void cpufreq_notify_transition(struct cpufreq_policy *policy,
300 struct cpufreq_freqs *freqs,
303 BUG_ON(irqs_disabled());
305 if (cpufreq_disabled())
308 freqs->flags = cpufreq_driver->flags;
309 pr_debug("notification %u of frequency transition to %u kHz\n",
313 case CPUFREQ_PRECHANGE:
315 * Detect if the driver reported a value as "old frequency"
316 * which is not equal to what the cpufreq core thinks is
319 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
320 if (policy->cur && (policy->cur != freqs->old)) {
321 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
322 freqs->old, policy->cur);
323 freqs->old = policy->cur;
327 for_each_cpu(freqs->cpu, policy->cpus) {
328 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
329 CPUFREQ_PRECHANGE, freqs);
332 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
335 case CPUFREQ_POSTCHANGE:
336 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
337 pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
338 cpumask_pr_args(policy->cpus));
340 for_each_cpu(freqs->cpu, policy->cpus) {
341 trace_cpu_frequency(freqs->new, freqs->cpu);
342 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
343 CPUFREQ_POSTCHANGE, freqs);
346 cpufreq_stats_record_transition(policy, freqs->new);
347 policy->cur = freqs->new;
351 /* Do post notifications when there are chances that transition has failed */
352 static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
353 struct cpufreq_freqs *freqs, int transition_failed)
355 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
356 if (!transition_failed)
359 swap(freqs->old, freqs->new);
360 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
361 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
364 void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
365 struct cpufreq_freqs *freqs)
369 * Catch double invocations of _begin() which lead to self-deadlock.
370 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
371 * doesn't invoke _begin() on their behalf, and hence the chances of
372 * double invocations are very low. Moreover, there are scenarios
373 * where these checks can emit false-positive warnings in these
374 * drivers; so we avoid that by skipping them altogether.
376 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
377 && current == policy->transition_task);
380 wait_event(policy->transition_wait, !policy->transition_ongoing);
382 spin_lock(&policy->transition_lock);
384 if (unlikely(policy->transition_ongoing)) {
385 spin_unlock(&policy->transition_lock);
389 policy->transition_ongoing = true;
390 policy->transition_task = current;
392 spin_unlock(&policy->transition_lock);
394 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
396 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
398 void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
399 struct cpufreq_freqs *freqs, int transition_failed)
401 if (WARN_ON(!policy->transition_ongoing))
404 cpufreq_notify_post_transition(policy, freqs, transition_failed);
406 policy->transition_ongoing = false;
407 policy->transition_task = NULL;
409 wake_up(&policy->transition_wait);
411 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
414 * Fast frequency switching status count. Positive means "enabled", negative
415 * means "disabled" and 0 means "not decided yet".
417 static int cpufreq_fast_switch_count;
418 static DEFINE_MUTEX(cpufreq_fast_switch_lock);
420 static void cpufreq_list_transition_notifiers(void)
422 struct notifier_block *nb;
424 pr_info("Registered transition notifiers:\n");
426 mutex_lock(&cpufreq_transition_notifier_list.mutex);
428 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
429 pr_info("%pF\n", nb->notifier_call);
431 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
435 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
436 * @policy: cpufreq policy to enable fast frequency switching for.
438 * Try to enable fast frequency switching for @policy.
440 * The attempt will fail if there is at least one transition notifier registered
441 * at this point, as fast frequency switching is quite fundamentally at odds
442 * with transition notifiers. Thus if successful, it will make registration of
443 * transition notifiers fail going forward.
445 void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
447 lockdep_assert_held(&policy->rwsem);
449 if (!policy->fast_switch_possible)
452 mutex_lock(&cpufreq_fast_switch_lock);
453 if (cpufreq_fast_switch_count >= 0) {
454 cpufreq_fast_switch_count++;
455 policy->fast_switch_enabled = true;
457 pr_warn("CPU%u: Fast frequency switching not enabled\n",
459 cpufreq_list_transition_notifiers();
461 mutex_unlock(&cpufreq_fast_switch_lock);
463 EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
466 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
467 * @policy: cpufreq policy to disable fast frequency switching for.
469 void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
471 mutex_lock(&cpufreq_fast_switch_lock);
472 if (policy->fast_switch_enabled) {
473 policy->fast_switch_enabled = false;
474 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
475 cpufreq_fast_switch_count--;
477 mutex_unlock(&cpufreq_fast_switch_lock);
479 EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
482 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
484 * @target_freq: target frequency to resolve.
486 * The target to driver frequency mapping is cached in the policy.
488 * Return: Lowest driver-supported frequency greater than or equal to the
489 * given target_freq, subject to policy (min/max) and driver limitations.
491 unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
492 unsigned int target_freq)
494 target_freq = clamp_val(target_freq, policy->min, policy->max);
495 policy->cached_target_freq = target_freq;
497 if (cpufreq_driver->target_index) {
500 idx = cpufreq_frequency_table_target(policy, target_freq,
502 policy->cached_resolved_idx = idx;
503 return policy->freq_table[idx].frequency;
506 if (cpufreq_driver->resolve_freq)
507 return cpufreq_driver->resolve_freq(policy, target_freq);
511 EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
513 unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
515 unsigned int latency;
517 if (policy->transition_delay_us)
518 return policy->transition_delay_us;
520 latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
523 * For platforms that can change the frequency very fast (< 10
524 * us), the above formula gives a decent transition delay. But
525 * for platforms where transition_latency is in milliseconds, it
526 * ends up giving unrealistic values.
528 * Cap the default transition delay to 10 ms, which seems to be
529 * a reasonable amount of time after which we should reevaluate
532 return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
535 return LATENCY_MULTIPLIER;
537 EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
539 /*********************************************************************
541 *********************************************************************/
542 static ssize_t show_boost(struct kobject *kobj,
543 struct kobj_attribute *attr, char *buf)
545 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
548 static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
549 const char *buf, size_t count)
553 ret = sscanf(buf, "%d", &enable);
554 if (ret != 1 || enable < 0 || enable > 1)
557 if (cpufreq_boost_trigger_state(enable)) {
558 pr_err("%s: Cannot %s BOOST!\n",
559 __func__, enable ? "enable" : "disable");
563 pr_debug("%s: cpufreq BOOST %s\n",
564 __func__, enable ? "enabled" : "disabled");
568 define_one_global_rw(boost);
570 static struct cpufreq_governor *find_governor(const char *str_governor)
572 struct cpufreq_governor *t;
575 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
582 * cpufreq_parse_governor - parse a governor string
584 static int cpufreq_parse_governor(char *str_governor,
585 struct cpufreq_policy *policy)
587 if (cpufreq_driver->setpolicy) {
588 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
589 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
593 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
594 policy->policy = CPUFREQ_POLICY_POWERSAVE;
598 struct cpufreq_governor *t;
600 mutex_lock(&cpufreq_governor_mutex);
602 t = find_governor(str_governor);
606 mutex_unlock(&cpufreq_governor_mutex);
608 ret = request_module("cpufreq_%s", str_governor);
612 mutex_lock(&cpufreq_governor_mutex);
614 t = find_governor(str_governor);
616 if (t && !try_module_get(t->owner))
619 mutex_unlock(&cpufreq_governor_mutex);
622 policy->governor = t;
631 * cpufreq_per_cpu_attr_read() / show_##file_name() -
632 * print out cpufreq information
634 * Write out information from cpufreq_driver->policy[cpu]; object must be
638 #define show_one(file_name, object) \
639 static ssize_t show_##file_name \
640 (struct cpufreq_policy *policy, char *buf) \
642 return sprintf(buf, "%u\n", policy->object); \
645 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
646 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
647 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
648 show_one(scaling_min_freq, min);
649 show_one(scaling_max_freq, max);
651 __weak unsigned int arch_freq_get_on_cpu(int cpu)
656 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
661 freq = arch_freq_get_on_cpu(policy->cpu);
663 ret = sprintf(buf, "%u\n", freq);
664 else if (cpufreq_driver && cpufreq_driver->setpolicy &&
666 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
668 ret = sprintf(buf, "%u\n", policy->cur);
672 static int cpufreq_set_policy(struct cpufreq_policy *policy,
673 struct cpufreq_policy *new_policy);
676 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
678 #define store_one(file_name, object) \
679 static ssize_t store_##file_name \
680 (struct cpufreq_policy *policy, const char *buf, size_t count) \
683 struct cpufreq_policy new_policy; \
685 memcpy(&new_policy, policy, sizeof(*policy)); \
686 new_policy.min = policy->user_policy.min; \
687 new_policy.max = policy->user_policy.max; \
689 ret = sscanf(buf, "%u", &new_policy.object); \
693 temp = new_policy.object; \
694 ret = cpufreq_set_policy(policy, &new_policy); \
696 policy->user_policy.object = temp; \
698 return ret ? ret : count; \
701 store_one(scaling_min_freq, min);
702 store_one(scaling_max_freq, max);
705 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
707 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
710 unsigned int cur_freq = __cpufreq_get(policy);
713 return sprintf(buf, "%u\n", cur_freq);
715 return sprintf(buf, "<unknown>\n");
719 * show_scaling_governor - show the current policy for the specified CPU
721 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
723 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
724 return sprintf(buf, "powersave\n");
725 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
726 return sprintf(buf, "performance\n");
727 else if (policy->governor)
728 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
729 policy->governor->name);
734 * store_scaling_governor - store policy for the specified CPU
736 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
737 const char *buf, size_t count)
740 char str_governor[16];
741 struct cpufreq_policy new_policy;
743 memcpy(&new_policy, policy, sizeof(*policy));
745 ret = sscanf(buf, "%15s", str_governor);
749 if (cpufreq_parse_governor(str_governor, &new_policy))
752 ret = cpufreq_set_policy(policy, &new_policy);
754 if (new_policy.governor)
755 module_put(new_policy.governor->owner);
757 return ret ? ret : count;
761 * show_scaling_driver - show the cpufreq driver currently loaded
763 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
765 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
769 * show_scaling_available_governors - show the available CPUfreq governors
771 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
775 struct cpufreq_governor *t;
778 i += sprintf(buf, "performance powersave");
782 for_each_governor(t) {
783 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
784 - (CPUFREQ_NAME_LEN + 2)))
786 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
789 i += sprintf(&buf[i], "\n");
793 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
798 for_each_cpu(cpu, mask) {
800 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
801 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
802 if (i >= (PAGE_SIZE - 5))
805 i += sprintf(&buf[i], "\n");
808 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
811 * show_related_cpus - show the CPUs affected by each transition even if
812 * hw coordination is in use
814 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
816 return cpufreq_show_cpus(policy->related_cpus, buf);
820 * show_affected_cpus - show the CPUs affected by each transition
822 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
824 return cpufreq_show_cpus(policy->cpus, buf);
827 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
828 const char *buf, size_t count)
830 unsigned int freq = 0;
833 if (!policy->governor || !policy->governor->store_setspeed)
836 ret = sscanf(buf, "%u", &freq);
840 policy->governor->store_setspeed(policy, freq);
845 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
847 if (!policy->governor || !policy->governor->show_setspeed)
848 return sprintf(buf, "<unsupported>\n");
850 return policy->governor->show_setspeed(policy, buf);
854 * show_bios_limit - show the current cpufreq HW/BIOS limitation
856 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
860 if (cpufreq_driver->bios_limit) {
861 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
863 return sprintf(buf, "%u\n", limit);
865 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
868 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
869 cpufreq_freq_attr_ro(cpuinfo_min_freq);
870 cpufreq_freq_attr_ro(cpuinfo_max_freq);
871 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
872 cpufreq_freq_attr_ro(scaling_available_governors);
873 cpufreq_freq_attr_ro(scaling_driver);
874 cpufreq_freq_attr_ro(scaling_cur_freq);
875 cpufreq_freq_attr_ro(bios_limit);
876 cpufreq_freq_attr_ro(related_cpus);
877 cpufreq_freq_attr_ro(affected_cpus);
878 cpufreq_freq_attr_rw(scaling_min_freq);
879 cpufreq_freq_attr_rw(scaling_max_freq);
880 cpufreq_freq_attr_rw(scaling_governor);
881 cpufreq_freq_attr_rw(scaling_setspeed);
883 static struct attribute *default_attrs[] = {
884 &cpuinfo_min_freq.attr,
885 &cpuinfo_max_freq.attr,
886 &cpuinfo_transition_latency.attr,
887 &scaling_min_freq.attr,
888 &scaling_max_freq.attr,
891 &scaling_governor.attr,
892 &scaling_driver.attr,
893 &scaling_available_governors.attr,
894 &scaling_setspeed.attr,
898 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
899 #define to_attr(a) container_of(a, struct freq_attr, attr)
901 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
903 struct cpufreq_policy *policy = to_policy(kobj);
904 struct freq_attr *fattr = to_attr(attr);
907 down_read(&policy->rwsem);
908 ret = fattr->show(policy, buf);
909 up_read(&policy->rwsem);
914 static ssize_t store(struct kobject *kobj, struct attribute *attr,
915 const char *buf, size_t count)
917 struct cpufreq_policy *policy = to_policy(kobj);
918 struct freq_attr *fattr = to_attr(attr);
919 ssize_t ret = -EINVAL;
922 * cpus_read_trylock() is used here to work around a circular lock
923 * dependency problem with respect to the cpufreq_register_driver().
925 if (!cpus_read_trylock())
928 if (cpu_online(policy->cpu)) {
929 down_write(&policy->rwsem);
930 ret = fattr->store(policy, buf, count);
931 up_write(&policy->rwsem);
939 static void cpufreq_sysfs_release(struct kobject *kobj)
941 struct cpufreq_policy *policy = to_policy(kobj);
942 pr_debug("last reference is dropped\n");
943 complete(&policy->kobj_unregister);
946 static const struct sysfs_ops sysfs_ops = {
951 static struct kobj_type ktype_cpufreq = {
952 .sysfs_ops = &sysfs_ops,
953 .default_attrs = default_attrs,
954 .release = cpufreq_sysfs_release,
957 static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
959 struct device *dev = get_cpu_device(cpu);
964 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
967 dev_dbg(dev, "%s: Adding symlink\n", __func__);
968 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
969 dev_err(dev, "cpufreq symlink creation failed\n");
972 static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
975 dev_dbg(dev, "%s: Removing symlink\n", __func__);
976 sysfs_remove_link(&dev->kobj, "cpufreq");
979 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
981 struct freq_attr **drv_attr;
984 /* set up files for this cpu device */
985 drv_attr = cpufreq_driver->attr;
986 while (drv_attr && *drv_attr) {
987 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
992 if (cpufreq_driver->get) {
993 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
998 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1002 if (cpufreq_driver->bios_limit) {
1003 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1011 __weak struct cpufreq_governor *cpufreq_default_governor(void)
1016 static int cpufreq_init_policy(struct cpufreq_policy *policy)
1018 struct cpufreq_governor *gov = NULL;
1019 struct cpufreq_policy new_policy;
1021 memcpy(&new_policy, policy, sizeof(*policy));
1023 /* Update governor of new_policy to the governor used before hotplug */
1024 gov = find_governor(policy->last_governor);
1026 pr_debug("Restoring governor %s for cpu %d\n",
1027 policy->governor->name, policy->cpu);
1029 gov = cpufreq_default_governor();
1034 new_policy.governor = gov;
1036 /* Use the default policy if there is no last_policy. */
1037 if (cpufreq_driver->setpolicy) {
1038 if (policy->last_policy)
1039 new_policy.policy = policy->last_policy;
1041 cpufreq_parse_governor(gov->name, &new_policy);
1043 /* set default policy */
1044 return cpufreq_set_policy(policy, &new_policy);
1047 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1051 /* Has this CPU been taken care of already? */
1052 if (cpumask_test_cpu(cpu, policy->cpus))
1055 down_write(&policy->rwsem);
1057 cpufreq_stop_governor(policy);
1059 cpumask_set_cpu(cpu, policy->cpus);
1062 ret = cpufreq_start_governor(policy);
1064 pr_err("%s: Failed to start governor\n", __func__);
1066 up_write(&policy->rwsem);
1070 static void handle_update(struct work_struct *work)
1072 struct cpufreq_policy *policy =
1073 container_of(work, struct cpufreq_policy, update);
1074 unsigned int cpu = policy->cpu;
1075 pr_debug("handle_update for cpu %u called\n", cpu);
1076 cpufreq_update_policy(cpu);
1079 static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1081 struct cpufreq_policy *policy;
1084 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1088 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1089 goto err_free_policy;
1091 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1092 goto err_free_cpumask;
1094 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1095 goto err_free_rcpumask;
1097 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1098 cpufreq_global_kobject, "policy%u", cpu);
1100 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1101 goto err_free_real_cpus;
1104 INIT_LIST_HEAD(&policy->policy_list);
1105 init_rwsem(&policy->rwsem);
1106 spin_lock_init(&policy->transition_lock);
1107 init_waitqueue_head(&policy->transition_wait);
1108 init_completion(&policy->kobj_unregister);
1109 INIT_WORK(&policy->update, handle_update);
1115 free_cpumask_var(policy->real_cpus);
1117 free_cpumask_var(policy->related_cpus);
1119 free_cpumask_var(policy->cpus);
1126 static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
1128 struct kobject *kobj;
1129 struct completion *cmp;
1131 down_write(&policy->rwsem);
1132 cpufreq_stats_free_table(policy);
1133 kobj = &policy->kobj;
1134 cmp = &policy->kobj_unregister;
1135 up_write(&policy->rwsem);
1139 * We need to make sure that the underlying kobj is
1140 * actually not referenced anymore by anybody before we
1141 * proceed with unloading.
1143 pr_debug("waiting for dropping of refcount\n");
1144 wait_for_completion(cmp);
1145 pr_debug("wait complete\n");
1148 static void cpufreq_policy_free(struct cpufreq_policy *policy)
1150 unsigned long flags;
1153 /* Remove policy from list */
1154 write_lock_irqsave(&cpufreq_driver_lock, flags);
1155 list_del(&policy->policy_list);
1157 for_each_cpu(cpu, policy->related_cpus)
1158 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1159 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1161 cpufreq_policy_put_kobj(policy);
1162 free_cpumask_var(policy->real_cpus);
1163 free_cpumask_var(policy->related_cpus);
1164 free_cpumask_var(policy->cpus);
1168 static int cpufreq_online(unsigned int cpu)
1170 struct cpufreq_policy *policy;
1172 unsigned long flags;
1176 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
1178 /* Check if this CPU already has a policy to manage it */
1179 policy = per_cpu(cpufreq_cpu_data, cpu);
1181 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
1182 if (!policy_is_inactive(policy))
1183 return cpufreq_add_policy_cpu(policy, cpu);
1185 /* This is the only online CPU for the policy. Start over. */
1187 down_write(&policy->rwsem);
1189 policy->governor = NULL;
1190 up_write(&policy->rwsem);
1193 policy = cpufreq_policy_alloc(cpu);
1198 if (!new_policy && cpufreq_driver->online) {
1199 ret = cpufreq_driver->online(policy);
1201 pr_debug("%s: %d: initialization failed\n", __func__,
1203 goto out_exit_policy;
1206 /* Recover policy->cpus using related_cpus */
1207 cpumask_copy(policy->cpus, policy->related_cpus);
1209 cpumask_copy(policy->cpus, cpumask_of(cpu));
1212 * Call driver. From then on the cpufreq must be able
1213 * to accept all calls to ->verify and ->setpolicy for this CPU.
1215 ret = cpufreq_driver->init(policy);
1217 pr_debug("%s: %d: initialization failed\n", __func__,
1219 goto out_free_policy;
1222 ret = cpufreq_table_validate_and_sort(policy);
1224 goto out_exit_policy;
1226 /* related_cpus should at least include policy->cpus. */
1227 cpumask_copy(policy->related_cpus, policy->cpus);
1230 down_write(&policy->rwsem);
1232 * affected cpus must always be the one, which are online. We aren't
1233 * managing offline cpus here.
1235 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1238 policy->user_policy.min = policy->min;
1239 policy->user_policy.max = policy->max;
1241 for_each_cpu(j, policy->related_cpus) {
1242 per_cpu(cpufreq_cpu_data, j) = policy;
1243 add_cpu_dev_symlink(policy, j);
1246 policy->min = policy->user_policy.min;
1247 policy->max = policy->user_policy.max;
1250 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1251 policy->cur = cpufreq_driver->get(policy->cpu);
1253 pr_err("%s: ->get() failed\n", __func__);
1254 goto out_destroy_policy;
1259 * Sometimes boot loaders set CPU frequency to a value outside of
1260 * frequency table present with cpufreq core. In such cases CPU might be
1261 * unstable if it has to run on that frequency for long duration of time
1262 * and so its better to set it to a frequency which is specified in
1263 * freq-table. This also makes cpufreq stats inconsistent as
1264 * cpufreq-stats would fail to register because current frequency of CPU
1265 * isn't found in freq-table.
1267 * Because we don't want this change to effect boot process badly, we go
1268 * for the next freq which is >= policy->cur ('cur' must be set by now,
1269 * otherwise we will end up setting freq to lowest of the table as 'cur'
1270 * is initialized to zero).
1272 * We are passing target-freq as "policy->cur - 1" otherwise
1273 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1274 * equal to target-freq.
1276 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1278 /* Are we running at unknown frequency ? */
1279 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1280 if (ret == -EINVAL) {
1281 /* Warn user and fix it */
1282 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1283 __func__, policy->cpu, policy->cur);
1284 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1285 CPUFREQ_RELATION_L);
1288 * Reaching here after boot in a few seconds may not
1289 * mean that system will remain stable at "unknown"
1290 * frequency for longer duration. Hence, a BUG_ON().
1293 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1294 __func__, policy->cpu, policy->cur);
1299 ret = cpufreq_add_dev_interface(policy);
1301 goto out_destroy_policy;
1303 cpufreq_stats_create_table(policy);
1305 write_lock_irqsave(&cpufreq_driver_lock, flags);
1306 list_add(&policy->policy_list, &cpufreq_policy_list);
1307 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1310 ret = cpufreq_init_policy(policy);
1312 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1313 __func__, cpu, ret);
1314 goto out_destroy_policy;
1317 up_write(&policy->rwsem);
1319 kobject_uevent(&policy->kobj, KOBJ_ADD);
1321 /* Callback for handling stuff after policy is ready */
1322 if (cpufreq_driver->ready)
1323 cpufreq_driver->ready(policy);
1325 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1326 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV)
1327 policy->cdev = of_cpufreq_cooling_register(policy);
1329 pr_debug("initialization complete\n");
1334 for_each_cpu(j, policy->real_cpus)
1335 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1337 up_write(&policy->rwsem);
1340 if (cpufreq_driver->exit)
1341 cpufreq_driver->exit(policy);
1344 cpufreq_policy_free(policy);
1349 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1351 * @sif: Subsystem interface structure pointer (not used)
1353 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1355 struct cpufreq_policy *policy;
1356 unsigned cpu = dev->id;
1359 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1361 if (cpu_online(cpu)) {
1362 ret = cpufreq_online(cpu);
1367 /* Create sysfs link on CPU registration */
1368 policy = per_cpu(cpufreq_cpu_data, cpu);
1370 add_cpu_dev_symlink(policy, cpu);
1375 static int cpufreq_offline(unsigned int cpu)
1377 struct cpufreq_policy *policy;
1380 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1382 policy = cpufreq_cpu_get_raw(cpu);
1384 pr_debug("%s: No cpu_data found\n", __func__);
1388 down_write(&policy->rwsem);
1390 cpufreq_stop_governor(policy);
1392 cpumask_clear_cpu(cpu, policy->cpus);
1394 if (policy_is_inactive(policy)) {
1396 strncpy(policy->last_governor, policy->governor->name,
1399 policy->last_policy = policy->policy;
1400 } else if (cpu == policy->cpu) {
1401 /* Nominate new CPU */
1402 policy->cpu = cpumask_any(policy->cpus);
1405 /* Start governor again for active policy */
1406 if (!policy_is_inactive(policy)) {
1408 ret = cpufreq_start_governor(policy);
1410 pr_err("%s: Failed to start governor\n", __func__);
1416 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1417 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) {
1418 cpufreq_cooling_unregister(policy->cdev);
1419 policy->cdev = NULL;
1422 if (cpufreq_driver->stop_cpu)
1423 cpufreq_driver->stop_cpu(policy);
1426 cpufreq_exit_governor(policy);
1429 * Perform the ->offline() during light-weight tear-down, as
1430 * that allows fast recovery when the CPU comes back.
1432 if (cpufreq_driver->offline) {
1433 cpufreq_driver->offline(policy);
1434 } else if (cpufreq_driver->exit) {
1435 cpufreq_driver->exit(policy);
1436 policy->freq_table = NULL;
1440 up_write(&policy->rwsem);
1445 * cpufreq_remove_dev - remove a CPU device
1447 * Removes the cpufreq interface for a CPU device.
1449 static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1451 unsigned int cpu = dev->id;
1452 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1457 if (cpu_online(cpu))
1458 cpufreq_offline(cpu);
1460 cpumask_clear_cpu(cpu, policy->real_cpus);
1461 remove_cpu_dev_symlink(policy, dev);
1463 if (cpumask_empty(policy->real_cpus)) {
1464 /* We did light-weight exit earlier, do full tear down now */
1465 if (cpufreq_driver->offline)
1466 cpufreq_driver->exit(policy);
1468 cpufreq_policy_free(policy);
1473 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1475 * @policy: policy managing CPUs
1476 * @new_freq: CPU frequency the CPU actually runs at
1478 * We adjust to current frequency first, and need to clean up later.
1479 * So either call to cpufreq_update_policy() or schedule handle_update()).
1481 static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1482 unsigned int new_freq)
1484 struct cpufreq_freqs freqs;
1486 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1487 policy->cur, new_freq);
1489 freqs.old = policy->cur;
1490 freqs.new = new_freq;
1492 cpufreq_freq_transition_begin(policy, &freqs);
1493 cpufreq_freq_transition_end(policy, &freqs, 0);
1497 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1500 * This is the last known freq, without actually getting it from the driver.
1501 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1503 unsigned int cpufreq_quick_get(unsigned int cpu)
1505 struct cpufreq_policy *policy;
1506 unsigned int ret_freq = 0;
1507 unsigned long flags;
1509 read_lock_irqsave(&cpufreq_driver_lock, flags);
1511 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1512 ret_freq = cpufreq_driver->get(cpu);
1513 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1517 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1519 policy = cpufreq_cpu_get(cpu);
1521 ret_freq = policy->cur;
1522 cpufreq_cpu_put(policy);
1527 EXPORT_SYMBOL(cpufreq_quick_get);
1530 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1533 * Just return the max possible frequency for a given CPU.
1535 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1537 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1538 unsigned int ret_freq = 0;
1541 ret_freq = policy->max;
1542 cpufreq_cpu_put(policy);
1547 EXPORT_SYMBOL(cpufreq_quick_get_max);
1549 static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
1551 unsigned int ret_freq = 0;
1553 if (unlikely(policy_is_inactive(policy)) || !cpufreq_driver->get)
1556 ret_freq = cpufreq_driver->get(policy->cpu);
1559 * If fast frequency switching is used with the given policy, the check
1560 * against policy->cur is pointless, so skip it in that case too.
1562 if (policy->fast_switch_enabled)
1565 if (ret_freq && policy->cur &&
1566 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1567 /* verify no discrepancy between actual and
1568 saved value exists */
1569 if (unlikely(ret_freq != policy->cur)) {
1570 cpufreq_out_of_sync(policy, ret_freq);
1571 schedule_work(&policy->update);
1579 * cpufreq_get - get the current CPU frequency (in kHz)
1582 * Get the CPU current (static) CPU frequency
1584 unsigned int cpufreq_get(unsigned int cpu)
1586 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1587 unsigned int ret_freq = 0;
1590 down_read(&policy->rwsem);
1591 ret_freq = __cpufreq_get(policy);
1592 up_read(&policy->rwsem);
1594 cpufreq_cpu_put(policy);
1599 EXPORT_SYMBOL(cpufreq_get);
1601 static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1603 unsigned int new_freq;
1605 new_freq = cpufreq_driver->get(policy->cpu);
1610 pr_debug("cpufreq: Driver did not initialize current freq\n");
1611 policy->cur = new_freq;
1612 } else if (policy->cur != new_freq && has_target()) {
1613 cpufreq_out_of_sync(policy, new_freq);
1619 static struct subsys_interface cpufreq_interface = {
1621 .subsys = &cpu_subsys,
1622 .add_dev = cpufreq_add_dev,
1623 .remove_dev = cpufreq_remove_dev,
1627 * In case platform wants some specific frequency to be configured
1630 int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1634 if (!policy->suspend_freq) {
1635 pr_debug("%s: suspend_freq not defined\n", __func__);
1639 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1640 policy->suspend_freq);
1642 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1643 CPUFREQ_RELATION_H);
1645 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1646 __func__, policy->suspend_freq, ret);
1650 EXPORT_SYMBOL(cpufreq_generic_suspend);
1653 * cpufreq_suspend() - Suspend CPUFreq governors
1655 * Called during system wide Suspend/Hibernate cycles for suspending governors
1656 * as some platforms can't change frequency after this point in suspend cycle.
1657 * Because some of the devices (like: i2c, regulators, etc) they use for
1658 * changing frequency are suspended quickly after this point.
1660 void cpufreq_suspend(void)
1662 struct cpufreq_policy *policy;
1664 if (!cpufreq_driver)
1667 if (!has_target() && !cpufreq_driver->suspend)
1670 pr_debug("%s: Suspending Governors\n", __func__);
1672 for_each_active_policy(policy) {
1674 down_write(&policy->rwsem);
1675 cpufreq_stop_governor(policy);
1676 up_write(&policy->rwsem);
1679 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
1680 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1685 cpufreq_suspended = true;
1689 * cpufreq_resume() - Resume CPUFreq governors
1691 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1692 * are suspended with cpufreq_suspend().
1694 void cpufreq_resume(void)
1696 struct cpufreq_policy *policy;
1699 if (!cpufreq_driver)
1702 if (unlikely(!cpufreq_suspended))
1705 cpufreq_suspended = false;
1707 if (!has_target() && !cpufreq_driver->resume)
1710 pr_debug("%s: Resuming Governors\n", __func__);
1712 for_each_active_policy(policy) {
1713 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
1714 pr_err("%s: Failed to resume driver: %p\n", __func__,
1716 } else if (has_target()) {
1717 down_write(&policy->rwsem);
1718 ret = cpufreq_start_governor(policy);
1719 up_write(&policy->rwsem);
1722 pr_err("%s: Failed to start governor for policy: %p\n",
1729 * cpufreq_get_current_driver - return current driver's name
1731 * Return the name string of the currently loaded cpufreq driver
1734 const char *cpufreq_get_current_driver(void)
1737 return cpufreq_driver->name;
1741 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1744 * cpufreq_get_driver_data - return current driver data
1746 * Return the private data of the currently loaded cpufreq
1747 * driver, or NULL if no cpufreq driver is loaded.
1749 void *cpufreq_get_driver_data(void)
1752 return cpufreq_driver->driver_data;
1756 EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1758 /*********************************************************************
1759 * NOTIFIER LISTS INTERFACE *
1760 *********************************************************************/
1763 * cpufreq_register_notifier - register a driver with cpufreq
1764 * @nb: notifier function to register
1765 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1767 * Add a driver to one of two lists: either a list of drivers that
1768 * are notified about clock rate changes (once before and once after
1769 * the transition), or a list of drivers that are notified about
1770 * changes in cpufreq policy.
1772 * This function may sleep, and has the same return conditions as
1773 * blocking_notifier_chain_register.
1775 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1779 if (cpufreq_disabled())
1783 case CPUFREQ_TRANSITION_NOTIFIER:
1784 mutex_lock(&cpufreq_fast_switch_lock);
1786 if (cpufreq_fast_switch_count > 0) {
1787 mutex_unlock(&cpufreq_fast_switch_lock);
1790 ret = srcu_notifier_chain_register(
1791 &cpufreq_transition_notifier_list, nb);
1793 cpufreq_fast_switch_count--;
1795 mutex_unlock(&cpufreq_fast_switch_lock);
1797 case CPUFREQ_POLICY_NOTIFIER:
1798 ret = blocking_notifier_chain_register(
1799 &cpufreq_policy_notifier_list, nb);
1807 EXPORT_SYMBOL(cpufreq_register_notifier);
1810 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1811 * @nb: notifier block to be unregistered
1812 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1814 * Remove a driver from the CPU frequency notifier list.
1816 * This function may sleep, and has the same return conditions as
1817 * blocking_notifier_chain_unregister.
1819 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1823 if (cpufreq_disabled())
1827 case CPUFREQ_TRANSITION_NOTIFIER:
1828 mutex_lock(&cpufreq_fast_switch_lock);
1830 ret = srcu_notifier_chain_unregister(
1831 &cpufreq_transition_notifier_list, nb);
1832 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1833 cpufreq_fast_switch_count++;
1835 mutex_unlock(&cpufreq_fast_switch_lock);
1837 case CPUFREQ_POLICY_NOTIFIER:
1838 ret = blocking_notifier_chain_unregister(
1839 &cpufreq_policy_notifier_list, nb);
1847 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1850 /*********************************************************************
1852 *********************************************************************/
1855 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1856 * @policy: cpufreq policy to switch the frequency for.
1857 * @target_freq: New frequency to set (may be approximate).
1859 * Carry out a fast frequency switch without sleeping.
1861 * The driver's ->fast_switch() callback invoked by this function must be
1862 * suitable for being called from within RCU-sched read-side critical sections
1863 * and it is expected to select the minimum available frequency greater than or
1864 * equal to @target_freq (CPUFREQ_RELATION_L).
1866 * This function must not be called if policy->fast_switch_enabled is unset.
1868 * Governors calling this function must guarantee that it will never be invoked
1869 * twice in parallel for the same policy and that it will never be called in
1870 * parallel with either ->target() or ->target_index() for the same policy.
1872 * Returns the actual frequency set for the CPU.
1874 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
1875 * error condition, the hardware configuration must be preserved.
1877 unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1878 unsigned int target_freq)
1880 target_freq = clamp_val(target_freq, policy->min, policy->max);
1882 return cpufreq_driver->fast_switch(policy, target_freq);
1884 EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1886 /* Must set freqs->new to intermediate frequency */
1887 static int __target_intermediate(struct cpufreq_policy *policy,
1888 struct cpufreq_freqs *freqs, int index)
1892 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1894 /* We don't need to switch to intermediate freq */
1898 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1899 __func__, policy->cpu, freqs->old, freqs->new);
1901 cpufreq_freq_transition_begin(policy, freqs);
1902 ret = cpufreq_driver->target_intermediate(policy, index);
1903 cpufreq_freq_transition_end(policy, freqs, ret);
1906 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1912 static int __target_index(struct cpufreq_policy *policy, int index)
1914 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1915 unsigned int intermediate_freq = 0;
1916 unsigned int newfreq = policy->freq_table[index].frequency;
1917 int retval = -EINVAL;
1920 if (newfreq == policy->cur)
1923 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1925 /* Handle switching to intermediate frequency */
1926 if (cpufreq_driver->get_intermediate) {
1927 retval = __target_intermediate(policy, &freqs, index);
1931 intermediate_freq = freqs.new;
1932 /* Set old freq to intermediate */
1933 if (intermediate_freq)
1934 freqs.old = freqs.new;
1937 freqs.new = newfreq;
1938 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1939 __func__, policy->cpu, freqs.old, freqs.new);
1941 cpufreq_freq_transition_begin(policy, &freqs);
1944 retval = cpufreq_driver->target_index(policy, index);
1946 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1950 cpufreq_freq_transition_end(policy, &freqs, retval);
1953 * Failed after setting to intermediate freq? Driver should have
1954 * reverted back to initial frequency and so should we. Check
1955 * here for intermediate_freq instead of get_intermediate, in
1956 * case we haven't switched to intermediate freq at all.
1958 if (unlikely(retval && intermediate_freq)) {
1959 freqs.old = intermediate_freq;
1960 freqs.new = policy->restore_freq;
1961 cpufreq_freq_transition_begin(policy, &freqs);
1962 cpufreq_freq_transition_end(policy, &freqs, 0);
1969 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1970 unsigned int target_freq,
1971 unsigned int relation)
1973 unsigned int old_target_freq = target_freq;
1976 if (cpufreq_disabled())
1979 /* Make sure that target_freq is within supported range */
1980 target_freq = clamp_val(target_freq, policy->min, policy->max);
1982 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1983 policy->cpu, target_freq, relation, old_target_freq);
1986 * This might look like a redundant call as we are checking it again
1987 * after finding index. But it is left intentionally for cases where
1988 * exactly same freq is called again and so we can save on few function
1991 if (target_freq == policy->cur)
1994 /* Save last value to restore later on errors */
1995 policy->restore_freq = policy->cur;
1997 if (cpufreq_driver->target)
1998 return cpufreq_driver->target(policy, target_freq, relation);
2000 if (!cpufreq_driver->target_index)
2003 index = cpufreq_frequency_table_target(policy, target_freq, relation);
2005 return __target_index(policy, index);
2007 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2009 int cpufreq_driver_target(struct cpufreq_policy *policy,
2010 unsigned int target_freq,
2011 unsigned int relation)
2015 down_write(&policy->rwsem);
2017 ret = __cpufreq_driver_target(policy, target_freq, relation);
2019 up_write(&policy->rwsem);
2023 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2025 __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2030 static int cpufreq_init_governor(struct cpufreq_policy *policy)
2034 /* Don't start any governor operations if we are entering suspend */
2035 if (cpufreq_suspended)
2038 * Governor might not be initiated here if ACPI _PPC changed
2039 * notification happened, so check it.
2041 if (!policy->governor)
2044 /* Platform doesn't want dynamic frequency switching ? */
2045 if (policy->governor->dynamic_switching &&
2046 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
2047 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2050 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
2051 policy->governor->name, gov->name);
2052 policy->governor = gov;
2058 if (!try_module_get(policy->governor->owner))
2061 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2063 if (policy->governor->init) {
2064 ret = policy->governor->init(policy);
2066 module_put(policy->governor->owner);
2074 static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2076 if (cpufreq_suspended || !policy->governor)
2079 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2081 if (policy->governor->exit)
2082 policy->governor->exit(policy);
2084 module_put(policy->governor->owner);
2087 static int cpufreq_start_governor(struct cpufreq_policy *policy)
2091 if (cpufreq_suspended)
2094 if (!policy->governor)
2097 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2099 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2100 cpufreq_update_current_freq(policy);
2102 if (policy->governor->start) {
2103 ret = policy->governor->start(policy);
2108 if (policy->governor->limits)
2109 policy->governor->limits(policy);
2114 static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2116 if (cpufreq_suspended || !policy->governor)
2119 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2121 if (policy->governor->stop)
2122 policy->governor->stop(policy);
2125 static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2127 if (cpufreq_suspended || !policy->governor)
2130 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2132 if (policy->governor->limits)
2133 policy->governor->limits(policy);
2136 int cpufreq_register_governor(struct cpufreq_governor *governor)
2143 if (cpufreq_disabled())
2146 mutex_lock(&cpufreq_governor_mutex);
2149 if (!find_governor(governor->name)) {
2151 list_add(&governor->governor_list, &cpufreq_governor_list);
2154 mutex_unlock(&cpufreq_governor_mutex);
2157 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2159 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2161 struct cpufreq_policy *policy;
2162 unsigned long flags;
2167 if (cpufreq_disabled())
2170 /* clear last_governor for all inactive policies */
2171 read_lock_irqsave(&cpufreq_driver_lock, flags);
2172 for_each_inactive_policy(policy) {
2173 if (!strcmp(policy->last_governor, governor->name)) {
2174 policy->governor = NULL;
2175 strcpy(policy->last_governor, "\0");
2178 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2180 mutex_lock(&cpufreq_governor_mutex);
2181 list_del(&governor->governor_list);
2182 mutex_unlock(&cpufreq_governor_mutex);
2184 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2187 /*********************************************************************
2188 * POLICY INTERFACE *
2189 *********************************************************************/
2192 * cpufreq_get_policy - get the current cpufreq_policy
2193 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2196 * Reads the current cpufreq policy.
2198 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2200 struct cpufreq_policy *cpu_policy;
2204 cpu_policy = cpufreq_cpu_get(cpu);
2208 memcpy(policy, cpu_policy, sizeof(*policy));
2210 cpufreq_cpu_put(cpu_policy);
2213 EXPORT_SYMBOL(cpufreq_get_policy);
2216 * cpufreq_set_policy - Modify cpufreq policy parameters.
2217 * @policy: Policy object to modify.
2218 * @new_policy: New policy data.
2220 * Pass @new_policy to the cpufreq driver's ->verify() callback, run the
2221 * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to
2222 * the driver's ->verify() callback again and run the notifiers for it again
2223 * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters
2224 * of @new_policy to @policy and either invoke the driver's ->setpolicy()
2225 * callback (if present) or carry out a governor update for @policy. That is,
2226 * run the current governor's ->limits() callback (if the governor field in
2227 * @new_policy points to the same object as the one in @policy) or replace the
2228 * governor for @policy with the new one stored in @new_policy.
2230 * The cpuinfo part of @policy is not updated by this function.
2232 static int cpufreq_set_policy(struct cpufreq_policy *policy,
2233 struct cpufreq_policy *new_policy)
2235 struct cpufreq_governor *old_gov;
2238 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2239 new_policy->cpu, new_policy->min, new_policy->max);
2241 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2244 * This check works well when we store new min/max freq attributes,
2245 * because new_policy is a copy of policy with one field updated.
2247 if (new_policy->min > new_policy->max)
2250 /* verify the cpu speed can be set within this limit */
2251 ret = cpufreq_driver->verify(new_policy);
2255 /* adjust if necessary - all reasons */
2256 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2257 CPUFREQ_ADJUST, new_policy);
2260 * verify the cpu speed can be set within this limit, which might be
2261 * different to the first one
2263 ret = cpufreq_driver->verify(new_policy);
2267 /* notification of the new policy */
2268 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2269 CPUFREQ_NOTIFY, new_policy);
2271 policy->min = new_policy->min;
2272 policy->max = new_policy->max;
2273 trace_cpu_frequency_limits(policy);
2275 policy->cached_target_freq = UINT_MAX;
2277 pr_debug("new min and max freqs are %u - %u kHz\n",
2278 policy->min, policy->max);
2280 if (cpufreq_driver->setpolicy) {
2281 policy->policy = new_policy->policy;
2282 pr_debug("setting range\n");
2283 return cpufreq_driver->setpolicy(policy);
2286 if (new_policy->governor == policy->governor) {
2287 pr_debug("governor limits update\n");
2288 cpufreq_governor_limits(policy);
2292 pr_debug("governor switch\n");
2294 /* save old, working values */
2295 old_gov = policy->governor;
2296 /* end old governor */
2298 cpufreq_stop_governor(policy);
2299 cpufreq_exit_governor(policy);
2302 /* start new governor */
2303 policy->governor = new_policy->governor;
2304 ret = cpufreq_init_governor(policy);
2306 ret = cpufreq_start_governor(policy);
2308 pr_debug("governor change\n");
2309 sched_cpufreq_governor_change(policy, old_gov);
2312 cpufreq_exit_governor(policy);
2315 /* new governor failed, so re-start old one */
2316 pr_debug("starting governor %s failed\n", policy->governor->name);
2318 policy->governor = old_gov;
2319 if (cpufreq_init_governor(policy))
2320 policy->governor = NULL;
2322 cpufreq_start_governor(policy);
2329 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2330 * @cpu: CPU to re-evaluate the policy for.
2332 * Update the current frequency for the cpufreq policy of @cpu and use
2333 * cpufreq_set_policy() to re-apply the min and max limits saved in the
2334 * user_policy sub-structure of that policy, which triggers the evaluation
2335 * of policy notifiers and the cpufreq driver's ->verify() callback for the
2336 * policy in question, among other things.
2338 void cpufreq_update_policy(unsigned int cpu)
2340 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2341 struct cpufreq_policy new_policy;
2346 down_write(&policy->rwsem);
2348 if (policy_is_inactive(policy))
2352 * BIOS might change freq behind our back
2353 * -> ask driver for current freq and notify governors about a change
2355 if (cpufreq_driver->get && !cpufreq_driver->setpolicy &&
2356 (cpufreq_suspended || WARN_ON(!cpufreq_update_current_freq(policy))))
2359 pr_debug("updating policy for CPU %u\n", cpu);
2360 memcpy(&new_policy, policy, sizeof(*policy));
2361 new_policy.min = policy->user_policy.min;
2362 new_policy.max = policy->user_policy.max;
2364 cpufreq_set_policy(policy, &new_policy);
2367 up_write(&policy->rwsem);
2369 cpufreq_cpu_put(policy);
2371 EXPORT_SYMBOL(cpufreq_update_policy);
2373 /*********************************************************************
2375 *********************************************************************/
2376 static int cpufreq_boost_set_sw(int state)
2378 struct cpufreq_policy *policy;
2381 for_each_active_policy(policy) {
2382 if (!policy->freq_table)
2385 ret = cpufreq_frequency_table_cpuinfo(policy,
2386 policy->freq_table);
2388 pr_err("%s: Policy frequency update failed\n",
2393 down_write(&policy->rwsem);
2394 policy->user_policy.max = policy->max;
2395 cpufreq_governor_limits(policy);
2396 up_write(&policy->rwsem);
2402 int cpufreq_boost_trigger_state(int state)
2404 unsigned long flags;
2407 if (cpufreq_driver->boost_enabled == state)
2410 write_lock_irqsave(&cpufreq_driver_lock, flags);
2411 cpufreq_driver->boost_enabled = state;
2412 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2414 ret = cpufreq_driver->set_boost(state);
2416 write_lock_irqsave(&cpufreq_driver_lock, flags);
2417 cpufreq_driver->boost_enabled = !state;
2418 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2420 pr_err("%s: Cannot %s BOOST\n",
2421 __func__, state ? "enable" : "disable");
2427 static bool cpufreq_boost_supported(void)
2429 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
2432 static int create_boost_sysfs_file(void)
2436 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
2438 pr_err("%s: cannot register global BOOST sysfs file\n",
2444 static void remove_boost_sysfs_file(void)
2446 if (cpufreq_boost_supported())
2447 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
2450 int cpufreq_enable_boost_support(void)
2452 if (!cpufreq_driver)
2455 if (cpufreq_boost_supported())
2458 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2460 /* This will get removed on driver unregister */
2461 return create_boost_sysfs_file();
2463 EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2465 int cpufreq_boost_enabled(void)
2467 return cpufreq_driver->boost_enabled;
2469 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2471 /*********************************************************************
2472 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2473 *********************************************************************/
2474 static enum cpuhp_state hp_online;
2476 static int cpuhp_cpufreq_online(unsigned int cpu)
2478 cpufreq_online(cpu);
2483 static int cpuhp_cpufreq_offline(unsigned int cpu)
2485 cpufreq_offline(cpu);
2491 * cpufreq_register_driver - register a CPU Frequency driver
2492 * @driver_data: A struct cpufreq_driver containing the values#
2493 * submitted by the CPU Frequency driver.
2495 * Registers a CPU Frequency driver to this core code. This code
2496 * returns zero on success, -EEXIST when another driver got here first
2497 * (and isn't unregistered in the meantime).
2500 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2502 unsigned long flags;
2505 if (cpufreq_disabled())
2508 if (!driver_data || !driver_data->verify || !driver_data->init ||
2509 !(driver_data->setpolicy || driver_data->target_index ||
2510 driver_data->target) ||
2511 (driver_data->setpolicy && (driver_data->target_index ||
2512 driver_data->target)) ||
2513 (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
2514 (!driver_data->online != !driver_data->offline))
2517 pr_debug("trying to register driver %s\n", driver_data->name);
2519 /* Protect against concurrent CPU online/offline. */
2522 write_lock_irqsave(&cpufreq_driver_lock, flags);
2523 if (cpufreq_driver) {
2524 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2528 cpufreq_driver = driver_data;
2529 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2531 if (driver_data->setpolicy)
2532 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2534 if (cpufreq_boost_supported()) {
2535 ret = create_boost_sysfs_file();
2537 goto err_null_driver;
2540 ret = subsys_interface_register(&cpufreq_interface);
2542 goto err_boost_unreg;
2544 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2545 list_empty(&cpufreq_policy_list)) {
2546 /* if all ->init() calls failed, unregister */
2548 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2553 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2555 cpuhp_cpufreq_online,
2556 cpuhp_cpufreq_offline);
2562 pr_debug("driver %s up and running\n", driver_data->name);
2566 subsys_interface_unregister(&cpufreq_interface);
2568 remove_boost_sysfs_file();
2570 write_lock_irqsave(&cpufreq_driver_lock, flags);
2571 cpufreq_driver = NULL;
2572 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2577 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2580 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2582 * Unregister the current CPUFreq driver. Only call this if you have
2583 * the right to do so, i.e. if you have succeeded in initialising before!
2584 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2585 * currently not initialised.
2587 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2589 unsigned long flags;
2591 if (!cpufreq_driver || (driver != cpufreq_driver))
2594 pr_debug("unregistering driver %s\n", driver->name);
2596 /* Protect against concurrent cpu hotplug */
2598 subsys_interface_unregister(&cpufreq_interface);
2599 remove_boost_sysfs_file();
2600 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
2602 write_lock_irqsave(&cpufreq_driver_lock, flags);
2604 cpufreq_driver = NULL;
2606 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2611 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2614 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2615 * or mutexes when secondary CPUs are halted.
2617 static struct syscore_ops cpufreq_syscore_ops = {
2618 .shutdown = cpufreq_suspend,
2621 struct kobject *cpufreq_global_kobject;
2622 EXPORT_SYMBOL(cpufreq_global_kobject);
2624 static int __init cpufreq_core_init(void)
2626 if (cpufreq_disabled())
2629 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
2630 BUG_ON(!cpufreq_global_kobject);
2632 register_syscore_ops(&cpufreq_syscore_ops);
2636 module_param(off, int, 0444);
2637 core_initcall(cpufreq_core_init);