1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/cpufreq/cpufreq.c
5 * Copyright (C) 2001 Russell King
6 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
9 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
10 * Added handling for CPU hotplug
11 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
12 * Fix handling for CPU hotplug -- affected CPUs
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/cpu.h>
18 #include <linux/cpufreq.h>
19 #include <linux/cpu_cooling.h>
20 #include <linux/delay.h>
21 #include <linux/device.h>
22 #include <linux/init.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/slab.h>
27 #include <linux/suspend.h>
28 #include <linux/syscore_ops.h>
29 #include <linux/tick.h>
30 #include <trace/events/power.h>
32 static LIST_HEAD(cpufreq_policy_list);
34 /* Macros to iterate over CPU policies */
35 #define for_each_suitable_policy(__policy, __active) \
36 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
37 if ((__active) == !policy_is_inactive(__policy))
39 #define for_each_active_policy(__policy) \
40 for_each_suitable_policy(__policy, true)
41 #define for_each_inactive_policy(__policy) \
42 for_each_suitable_policy(__policy, false)
44 #define for_each_policy(__policy) \
45 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
47 /* Iterate over governors */
48 static LIST_HEAD(cpufreq_governor_list);
49 #define for_each_governor(__governor) \
50 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
53 * The "cpufreq driver" - the arch- or hardware-dependent low
54 * level driver of CPUFreq support, and its spinlock. This lock
55 * also protects the cpufreq_cpu_data array.
57 static struct cpufreq_driver *cpufreq_driver;
58 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
59 static DEFINE_RWLOCK(cpufreq_driver_lock);
61 /* Flag to suspend/resume CPUFreq governors */
62 static bool cpufreq_suspended;
64 static inline bool has_target(void)
66 return cpufreq_driver->target_index || cpufreq_driver->target;
69 /* internal prototypes */
70 static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
71 static int cpufreq_init_governor(struct cpufreq_policy *policy);
72 static void cpufreq_exit_governor(struct cpufreq_policy *policy);
73 static int cpufreq_start_governor(struct cpufreq_policy *policy);
74 static void cpufreq_stop_governor(struct cpufreq_policy *policy);
75 static void cpufreq_governor_limits(struct cpufreq_policy *policy);
78 * Two notifier lists: the "policy" list is involved in the
79 * validation process for a new CPU frequency policy; the
80 * "transition" list for kernel code that needs to handle
81 * changes to devices when the CPU clock speed changes.
82 * The mutex locks both lists.
84 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
85 SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
87 static int off __read_mostly;
88 static int cpufreq_disabled(void)
92 void disable_cpufreq(void)
96 static DEFINE_MUTEX(cpufreq_governor_mutex);
98 bool have_governor_per_policy(void)
100 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
102 EXPORT_SYMBOL_GPL(have_governor_per_policy);
104 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
106 if (have_governor_per_policy())
107 return &policy->kobj;
109 return cpufreq_global_kobject;
111 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
113 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
119 cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
121 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
122 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
123 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
124 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
125 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
126 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
128 idle_time = cur_wall_time - busy_time;
130 *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
132 return div_u64(idle_time, NSEC_PER_USEC);
135 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
137 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
139 if (idle_time == -1ULL)
140 return get_cpu_idle_time_jiffy(cpu, wall);
142 idle_time += get_cpu_iowait_time_us(cpu, wall);
146 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
148 __weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
149 unsigned long max_freq)
152 EXPORT_SYMBOL_GPL(arch_set_freq_scale);
155 * This is a generic cpufreq init() routine which can be used by cpufreq
156 * drivers of SMP systems. It will do following:
157 * - validate & show freq table passed
158 * - set policies transition latency
159 * - policy->cpus with all possible CPUs
161 int cpufreq_generic_init(struct cpufreq_policy *policy,
162 struct cpufreq_frequency_table *table,
163 unsigned int transition_latency)
165 policy->freq_table = table;
166 policy->cpuinfo.transition_latency = transition_latency;
169 * The driver only supports the SMP configuration where all processors
170 * share the clock and voltage and clock.
172 cpumask_setall(policy->cpus);
176 EXPORT_SYMBOL_GPL(cpufreq_generic_init);
178 struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
180 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
182 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
184 EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
186 unsigned int cpufreq_generic_get(unsigned int cpu)
188 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
190 if (!policy || IS_ERR(policy->clk)) {
191 pr_err("%s: No %s associated to cpu: %d\n",
192 __func__, policy ? "clk" : "policy", cpu);
196 return clk_get_rate(policy->clk) / 1000;
198 EXPORT_SYMBOL_GPL(cpufreq_generic_get);
201 * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
202 * @cpu: CPU to find the policy for.
204 * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
205 * the kobject reference counter of that policy. Return a valid policy on
206 * success or NULL on failure.
208 * The policy returned by this function has to be released with the help of
209 * cpufreq_cpu_put() to balance its kobject reference counter properly.
211 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
213 struct cpufreq_policy *policy = NULL;
216 if (WARN_ON(cpu >= nr_cpu_ids))
219 /* get the cpufreq driver */
220 read_lock_irqsave(&cpufreq_driver_lock, flags);
222 if (cpufreq_driver) {
224 policy = cpufreq_cpu_get_raw(cpu);
226 kobject_get(&policy->kobj);
229 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
233 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
236 * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
237 * @policy: cpufreq policy returned by cpufreq_cpu_get().
239 void cpufreq_cpu_put(struct cpufreq_policy *policy)
241 kobject_put(&policy->kobj);
243 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
246 * cpufreq_cpu_release - Unlock a policy and decrement its usage counter.
247 * @policy: cpufreq policy returned by cpufreq_cpu_acquire().
249 void cpufreq_cpu_release(struct cpufreq_policy *policy)
251 if (WARN_ON(!policy))
254 lockdep_assert_held(&policy->rwsem);
256 up_write(&policy->rwsem);
258 cpufreq_cpu_put(policy);
262 * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it.
263 * @cpu: CPU to find the policy for.
265 * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and
266 * if the policy returned by it is not NULL, acquire its rwsem for writing.
267 * Return the policy if it is active or release it and return NULL otherwise.
269 * The policy returned by this function has to be released with the help of
270 * cpufreq_cpu_release() in order to release its rwsem and balance its usage
273 struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu)
275 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
280 down_write(&policy->rwsem);
282 if (policy_is_inactive(policy)) {
283 cpufreq_cpu_release(policy);
290 /*********************************************************************
291 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
292 *********************************************************************/
295 * adjust_jiffies - adjust the system "loops_per_jiffy"
297 * This function alters the system "loops_per_jiffy" for the clock
298 * speed change. Note that loops_per_jiffy cannot be updated on SMP
299 * systems as each CPU might be scaled differently. So, use the arch
300 * per-CPU loops_per_jiffy value wherever possible.
302 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
305 static unsigned long l_p_j_ref;
306 static unsigned int l_p_j_ref_freq;
308 if (ci->flags & CPUFREQ_CONST_LOOPS)
311 if (!l_p_j_ref_freq) {
312 l_p_j_ref = loops_per_jiffy;
313 l_p_j_ref_freq = ci->old;
314 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
315 l_p_j_ref, l_p_j_ref_freq);
317 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
318 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
320 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
321 loops_per_jiffy, ci->new);
327 * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies.
328 * @policy: cpufreq policy to enable fast frequency switching for.
329 * @freqs: contain details of the frequency update.
330 * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
332 * This function calls the transition notifiers and the "adjust_jiffies"
333 * function. It is called twice on all CPU frequency changes that have
336 static void cpufreq_notify_transition(struct cpufreq_policy *policy,
337 struct cpufreq_freqs *freqs,
342 BUG_ON(irqs_disabled());
344 if (cpufreq_disabled())
347 freqs->policy = policy;
348 freqs->flags = cpufreq_driver->flags;
349 pr_debug("notification %u of frequency transition to %u kHz\n",
353 case CPUFREQ_PRECHANGE:
355 * Detect if the driver reported a value as "old frequency"
356 * which is not equal to what the cpufreq core thinks is
359 if (policy->cur && policy->cur != freqs->old) {
360 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
361 freqs->old, policy->cur);
362 freqs->old = policy->cur;
365 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
366 CPUFREQ_PRECHANGE, freqs);
368 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
371 case CPUFREQ_POSTCHANGE:
372 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
373 pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
374 cpumask_pr_args(policy->cpus));
376 for_each_cpu(cpu, policy->cpus)
377 trace_cpu_frequency(freqs->new, cpu);
379 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
380 CPUFREQ_POSTCHANGE, freqs);
382 cpufreq_stats_record_transition(policy, freqs->new);
383 policy->cur = freqs->new;
387 /* Do post notifications when there are chances that transition has failed */
388 static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
389 struct cpufreq_freqs *freqs, int transition_failed)
391 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
392 if (!transition_failed)
395 swap(freqs->old, freqs->new);
396 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
397 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
400 void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
401 struct cpufreq_freqs *freqs)
405 * Catch double invocations of _begin() which lead to self-deadlock.
406 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
407 * doesn't invoke _begin() on their behalf, and hence the chances of
408 * double invocations are very low. Moreover, there are scenarios
409 * where these checks can emit false-positive warnings in these
410 * drivers; so we avoid that by skipping them altogether.
412 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
413 && current == policy->transition_task);
416 wait_event(policy->transition_wait, !policy->transition_ongoing);
418 spin_lock(&policy->transition_lock);
420 if (unlikely(policy->transition_ongoing)) {
421 spin_unlock(&policy->transition_lock);
425 policy->transition_ongoing = true;
426 policy->transition_task = current;
428 spin_unlock(&policy->transition_lock);
430 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
432 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
434 void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
435 struct cpufreq_freqs *freqs, int transition_failed)
437 if (WARN_ON(!policy->transition_ongoing))
440 cpufreq_notify_post_transition(policy, freqs, transition_failed);
442 policy->transition_ongoing = false;
443 policy->transition_task = NULL;
445 wake_up(&policy->transition_wait);
447 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
450 * Fast frequency switching status count. Positive means "enabled", negative
451 * means "disabled" and 0 means "not decided yet".
453 static int cpufreq_fast_switch_count;
454 static DEFINE_MUTEX(cpufreq_fast_switch_lock);
456 static void cpufreq_list_transition_notifiers(void)
458 struct notifier_block *nb;
460 pr_info("Registered transition notifiers:\n");
462 mutex_lock(&cpufreq_transition_notifier_list.mutex);
464 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
465 pr_info("%pS\n", nb->notifier_call);
467 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
471 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
472 * @policy: cpufreq policy to enable fast frequency switching for.
474 * Try to enable fast frequency switching for @policy.
476 * The attempt will fail if there is at least one transition notifier registered
477 * at this point, as fast frequency switching is quite fundamentally at odds
478 * with transition notifiers. Thus if successful, it will make registration of
479 * transition notifiers fail going forward.
481 void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
483 lockdep_assert_held(&policy->rwsem);
485 if (!policy->fast_switch_possible)
488 mutex_lock(&cpufreq_fast_switch_lock);
489 if (cpufreq_fast_switch_count >= 0) {
490 cpufreq_fast_switch_count++;
491 policy->fast_switch_enabled = true;
493 pr_warn("CPU%u: Fast frequency switching not enabled\n",
495 cpufreq_list_transition_notifiers();
497 mutex_unlock(&cpufreq_fast_switch_lock);
499 EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
502 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
503 * @policy: cpufreq policy to disable fast frequency switching for.
505 void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
507 mutex_lock(&cpufreq_fast_switch_lock);
508 if (policy->fast_switch_enabled) {
509 policy->fast_switch_enabled = false;
510 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
511 cpufreq_fast_switch_count--;
513 mutex_unlock(&cpufreq_fast_switch_lock);
515 EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
518 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
520 * @target_freq: target frequency to resolve.
522 * The target to driver frequency mapping is cached in the policy.
524 * Return: Lowest driver-supported frequency greater than or equal to the
525 * given target_freq, subject to policy (min/max) and driver limitations.
527 unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
528 unsigned int target_freq)
530 target_freq = clamp_val(target_freq, policy->min, policy->max);
531 policy->cached_target_freq = target_freq;
533 if (cpufreq_driver->target_index) {
536 idx = cpufreq_frequency_table_target(policy, target_freq,
538 policy->cached_resolved_idx = idx;
539 return policy->freq_table[idx].frequency;
542 if (cpufreq_driver->resolve_freq)
543 return cpufreq_driver->resolve_freq(policy, target_freq);
547 EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
549 unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
551 unsigned int latency;
553 if (policy->transition_delay_us)
554 return policy->transition_delay_us;
556 latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
559 * For platforms that can change the frequency very fast (< 10
560 * us), the above formula gives a decent transition delay. But
561 * for platforms where transition_latency is in milliseconds, it
562 * ends up giving unrealistic values.
564 * Cap the default transition delay to 10 ms, which seems to be
565 * a reasonable amount of time after which we should reevaluate
568 return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
571 return LATENCY_MULTIPLIER;
573 EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
575 /*********************************************************************
577 *********************************************************************/
578 static ssize_t show_boost(struct kobject *kobj,
579 struct kobj_attribute *attr, char *buf)
581 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
584 static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
585 const char *buf, size_t count)
589 ret = sscanf(buf, "%d", &enable);
590 if (ret != 1 || enable < 0 || enable > 1)
593 if (cpufreq_boost_trigger_state(enable)) {
594 pr_err("%s: Cannot %s BOOST!\n",
595 __func__, enable ? "enable" : "disable");
599 pr_debug("%s: cpufreq BOOST %s\n",
600 __func__, enable ? "enabled" : "disabled");
604 define_one_global_rw(boost);
606 static struct cpufreq_governor *find_governor(const char *str_governor)
608 struct cpufreq_governor *t;
611 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
617 static int cpufreq_parse_policy(char *str_governor,
618 struct cpufreq_policy *policy)
620 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
621 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
624 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
625 policy->policy = CPUFREQ_POLICY_POWERSAVE;
632 * cpufreq_parse_governor - parse a governor string only for has_target()
634 static int cpufreq_parse_governor(char *str_governor,
635 struct cpufreq_policy *policy)
637 struct cpufreq_governor *t;
639 mutex_lock(&cpufreq_governor_mutex);
641 t = find_governor(str_governor);
645 mutex_unlock(&cpufreq_governor_mutex);
647 ret = request_module("cpufreq_%s", str_governor);
651 mutex_lock(&cpufreq_governor_mutex);
653 t = find_governor(str_governor);
655 if (t && !try_module_get(t->owner))
658 mutex_unlock(&cpufreq_governor_mutex);
661 policy->governor = t;
669 * cpufreq_per_cpu_attr_read() / show_##file_name() -
670 * print out cpufreq information
672 * Write out information from cpufreq_driver->policy[cpu]; object must be
676 #define show_one(file_name, object) \
677 static ssize_t show_##file_name \
678 (struct cpufreq_policy *policy, char *buf) \
680 return sprintf(buf, "%u\n", policy->object); \
683 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
684 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
685 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
686 show_one(scaling_min_freq, min);
687 show_one(scaling_max_freq, max);
689 __weak unsigned int arch_freq_get_on_cpu(int cpu)
694 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
699 freq = arch_freq_get_on_cpu(policy->cpu);
701 ret = sprintf(buf, "%u\n", freq);
702 else if (cpufreq_driver && cpufreq_driver->setpolicy &&
704 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
706 ret = sprintf(buf, "%u\n", policy->cur);
711 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
713 #define store_one(file_name, object) \
714 static ssize_t store_##file_name \
715 (struct cpufreq_policy *policy, const char *buf, size_t count) \
718 struct cpufreq_policy new_policy; \
720 memcpy(&new_policy, policy, sizeof(*policy)); \
721 new_policy.min = policy->user_policy.min; \
722 new_policy.max = policy->user_policy.max; \
724 ret = sscanf(buf, "%u", &new_policy.object); \
728 temp = new_policy.object; \
729 ret = cpufreq_set_policy(policy, &new_policy); \
731 policy->user_policy.object = temp; \
733 return ret ? ret : count; \
736 store_one(scaling_min_freq, min);
737 store_one(scaling_max_freq, max);
740 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
742 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
745 unsigned int cur_freq = __cpufreq_get(policy);
748 return sprintf(buf, "%u\n", cur_freq);
750 return sprintf(buf, "<unknown>\n");
754 * show_scaling_governor - show the current policy for the specified CPU
756 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
758 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
759 return sprintf(buf, "powersave\n");
760 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
761 return sprintf(buf, "performance\n");
762 else if (policy->governor)
763 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
764 policy->governor->name);
769 * store_scaling_governor - store policy for the specified CPU
771 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
772 const char *buf, size_t count)
775 char str_governor[16];
776 struct cpufreq_policy new_policy;
778 memcpy(&new_policy, policy, sizeof(*policy));
780 ret = sscanf(buf, "%15s", str_governor);
784 if (cpufreq_driver->setpolicy) {
785 if (cpufreq_parse_policy(str_governor, &new_policy))
788 if (cpufreq_parse_governor(str_governor, &new_policy))
792 ret = cpufreq_set_policy(policy, &new_policy);
794 if (new_policy.governor)
795 module_put(new_policy.governor->owner);
797 return ret ? ret : count;
801 * show_scaling_driver - show the cpufreq driver currently loaded
803 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
805 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
809 * show_scaling_available_governors - show the available CPUfreq governors
811 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
815 struct cpufreq_governor *t;
818 i += sprintf(buf, "performance powersave");
822 for_each_governor(t) {
823 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
824 - (CPUFREQ_NAME_LEN + 2)))
826 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
829 i += sprintf(&buf[i], "\n");
833 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
838 for_each_cpu(cpu, mask) {
840 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
841 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
842 if (i >= (PAGE_SIZE - 5))
845 i += sprintf(&buf[i], "\n");
848 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
851 * show_related_cpus - show the CPUs affected by each transition even if
852 * hw coordination is in use
854 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
856 return cpufreq_show_cpus(policy->related_cpus, buf);
860 * show_affected_cpus - show the CPUs affected by each transition
862 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
864 return cpufreq_show_cpus(policy->cpus, buf);
867 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
868 const char *buf, size_t count)
870 unsigned int freq = 0;
873 if (!policy->governor || !policy->governor->store_setspeed)
876 ret = sscanf(buf, "%u", &freq);
880 policy->governor->store_setspeed(policy, freq);
885 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
887 if (!policy->governor || !policy->governor->show_setspeed)
888 return sprintf(buf, "<unsupported>\n");
890 return policy->governor->show_setspeed(policy, buf);
894 * show_bios_limit - show the current cpufreq HW/BIOS limitation
896 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
900 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
902 return sprintf(buf, "%u\n", limit);
903 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
906 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
907 cpufreq_freq_attr_ro(cpuinfo_min_freq);
908 cpufreq_freq_attr_ro(cpuinfo_max_freq);
909 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
910 cpufreq_freq_attr_ro(scaling_available_governors);
911 cpufreq_freq_attr_ro(scaling_driver);
912 cpufreq_freq_attr_ro(scaling_cur_freq);
913 cpufreq_freq_attr_ro(bios_limit);
914 cpufreq_freq_attr_ro(related_cpus);
915 cpufreq_freq_attr_ro(affected_cpus);
916 cpufreq_freq_attr_rw(scaling_min_freq);
917 cpufreq_freq_attr_rw(scaling_max_freq);
918 cpufreq_freq_attr_rw(scaling_governor);
919 cpufreq_freq_attr_rw(scaling_setspeed);
921 static struct attribute *default_attrs[] = {
922 &cpuinfo_min_freq.attr,
923 &cpuinfo_max_freq.attr,
924 &cpuinfo_transition_latency.attr,
925 &scaling_min_freq.attr,
926 &scaling_max_freq.attr,
929 &scaling_governor.attr,
930 &scaling_driver.attr,
931 &scaling_available_governors.attr,
932 &scaling_setspeed.attr,
936 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
937 #define to_attr(a) container_of(a, struct freq_attr, attr)
939 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
941 struct cpufreq_policy *policy = to_policy(kobj);
942 struct freq_attr *fattr = to_attr(attr);
945 down_read(&policy->rwsem);
946 ret = fattr->show(policy, buf);
947 up_read(&policy->rwsem);
952 static ssize_t store(struct kobject *kobj, struct attribute *attr,
953 const char *buf, size_t count)
955 struct cpufreq_policy *policy = to_policy(kobj);
956 struct freq_attr *fattr = to_attr(attr);
957 ssize_t ret = -EINVAL;
960 * cpus_read_trylock() is used here to work around a circular lock
961 * dependency problem with respect to the cpufreq_register_driver().
963 if (!cpus_read_trylock())
966 if (cpu_online(policy->cpu)) {
967 down_write(&policy->rwsem);
968 ret = fattr->store(policy, buf, count);
969 up_write(&policy->rwsem);
977 static void cpufreq_sysfs_release(struct kobject *kobj)
979 struct cpufreq_policy *policy = to_policy(kobj);
980 pr_debug("last reference is dropped\n");
981 complete(&policy->kobj_unregister);
984 static const struct sysfs_ops sysfs_ops = {
989 static struct kobj_type ktype_cpufreq = {
990 .sysfs_ops = &sysfs_ops,
991 .default_attrs = default_attrs,
992 .release = cpufreq_sysfs_release,
995 static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
997 struct device *dev = get_cpu_device(cpu);
1002 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1005 dev_dbg(dev, "%s: Adding symlink\n", __func__);
1006 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
1007 dev_err(dev, "cpufreq symlink creation failed\n");
1010 static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
1013 dev_dbg(dev, "%s: Removing symlink\n", __func__);
1014 sysfs_remove_link(&dev->kobj, "cpufreq");
1017 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
1019 struct freq_attr **drv_attr;
1022 /* set up files for this cpu device */
1023 drv_attr = cpufreq_driver->attr;
1024 while (drv_attr && *drv_attr) {
1025 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1030 if (cpufreq_driver->get) {
1031 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1036 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1040 if (cpufreq_driver->bios_limit) {
1041 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1049 __weak struct cpufreq_governor *cpufreq_default_governor(void)
1054 static int cpufreq_init_policy(struct cpufreq_policy *policy)
1056 struct cpufreq_governor *gov = NULL, *def_gov = NULL;
1057 struct cpufreq_policy new_policy;
1059 memcpy(&new_policy, policy, sizeof(*policy));
1061 def_gov = cpufreq_default_governor();
1065 * Update governor of new_policy to the governor used before
1068 gov = find_governor(policy->last_governor);
1070 pr_debug("Restoring governor %s for cpu %d\n",
1071 policy->governor->name, policy->cpu);
1077 new_policy.governor = gov;
1079 /* Use the default policy if there is no last_policy. */
1080 if (policy->last_policy) {
1081 new_policy.policy = policy->last_policy;
1085 cpufreq_parse_policy(def_gov->name, &new_policy);
1089 return cpufreq_set_policy(policy, &new_policy);
1092 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1096 /* Has this CPU been taken care of already? */
1097 if (cpumask_test_cpu(cpu, policy->cpus))
1100 down_write(&policy->rwsem);
1102 cpufreq_stop_governor(policy);
1104 cpumask_set_cpu(cpu, policy->cpus);
1107 ret = cpufreq_start_governor(policy);
1109 pr_err("%s: Failed to start governor\n", __func__);
1111 up_write(&policy->rwsem);
1115 static void refresh_frequency_limits(struct cpufreq_policy *policy)
1117 struct cpufreq_policy new_policy = *policy;
1119 pr_debug("updating policy for CPU %u\n", policy->cpu);
1121 new_policy.min = policy->user_policy.min;
1122 new_policy.max = policy->user_policy.max;
1124 cpufreq_set_policy(policy, &new_policy);
1127 static void handle_update(struct work_struct *work)
1129 struct cpufreq_policy *policy =
1130 container_of(work, struct cpufreq_policy, update);
1132 pr_debug("handle_update for cpu %u called\n", policy->cpu);
1133 refresh_frequency_limits(policy);
1136 static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1138 struct cpufreq_policy *policy;
1141 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1145 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1146 goto err_free_policy;
1148 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1149 goto err_free_cpumask;
1151 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1152 goto err_free_rcpumask;
1154 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1155 cpufreq_global_kobject, "policy%u", cpu);
1157 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1159 * The entire policy object will be freed below, but the extra
1160 * memory allocated for the kobject name needs to be freed by
1161 * releasing the kobject.
1163 kobject_put(&policy->kobj);
1164 goto err_free_real_cpus;
1167 INIT_LIST_HEAD(&policy->policy_list);
1168 init_rwsem(&policy->rwsem);
1169 spin_lock_init(&policy->transition_lock);
1170 init_waitqueue_head(&policy->transition_wait);
1171 init_completion(&policy->kobj_unregister);
1172 INIT_WORK(&policy->update, handle_update);
1178 free_cpumask_var(policy->real_cpus);
1180 free_cpumask_var(policy->related_cpus);
1182 free_cpumask_var(policy->cpus);
1189 static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
1191 struct kobject *kobj;
1192 struct completion *cmp;
1194 down_write(&policy->rwsem);
1195 cpufreq_stats_free_table(policy);
1196 kobj = &policy->kobj;
1197 cmp = &policy->kobj_unregister;
1198 up_write(&policy->rwsem);
1202 * We need to make sure that the underlying kobj is
1203 * actually not referenced anymore by anybody before we
1204 * proceed with unloading.
1206 pr_debug("waiting for dropping of refcount\n");
1207 wait_for_completion(cmp);
1208 pr_debug("wait complete\n");
1211 static void cpufreq_policy_free(struct cpufreq_policy *policy)
1213 unsigned long flags;
1216 /* Remove policy from list */
1217 write_lock_irqsave(&cpufreq_driver_lock, flags);
1218 list_del(&policy->policy_list);
1220 for_each_cpu(cpu, policy->related_cpus)
1221 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1222 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1224 cpufreq_policy_put_kobj(policy);
1225 free_cpumask_var(policy->real_cpus);
1226 free_cpumask_var(policy->related_cpus);
1227 free_cpumask_var(policy->cpus);
1231 static int cpufreq_online(unsigned int cpu)
1233 struct cpufreq_policy *policy;
1235 unsigned long flags;
1239 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
1241 /* Check if this CPU already has a policy to manage it */
1242 policy = per_cpu(cpufreq_cpu_data, cpu);
1244 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
1245 if (!policy_is_inactive(policy))
1246 return cpufreq_add_policy_cpu(policy, cpu);
1248 /* This is the only online CPU for the policy. Start over. */
1250 down_write(&policy->rwsem);
1252 policy->governor = NULL;
1253 up_write(&policy->rwsem);
1256 policy = cpufreq_policy_alloc(cpu);
1261 if (!new_policy && cpufreq_driver->online) {
1262 ret = cpufreq_driver->online(policy);
1264 pr_debug("%s: %d: initialization failed\n", __func__,
1266 goto out_exit_policy;
1269 /* Recover policy->cpus using related_cpus */
1270 cpumask_copy(policy->cpus, policy->related_cpus);
1272 cpumask_copy(policy->cpus, cpumask_of(cpu));
1275 * Call driver. From then on the cpufreq must be able
1276 * to accept all calls to ->verify and ->setpolicy for this CPU.
1278 ret = cpufreq_driver->init(policy);
1280 pr_debug("%s: %d: initialization failed\n", __func__,
1282 goto out_free_policy;
1285 ret = cpufreq_table_validate_and_sort(policy);
1287 goto out_exit_policy;
1289 /* related_cpus should at least include policy->cpus. */
1290 cpumask_copy(policy->related_cpus, policy->cpus);
1293 down_write(&policy->rwsem);
1295 * affected cpus must always be the one, which are online. We aren't
1296 * managing offline cpus here.
1298 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1301 policy->user_policy.min = policy->min;
1302 policy->user_policy.max = policy->max;
1304 for_each_cpu(j, policy->related_cpus) {
1305 per_cpu(cpufreq_cpu_data, j) = policy;
1306 add_cpu_dev_symlink(policy, j);
1309 policy->min = policy->user_policy.min;
1310 policy->max = policy->user_policy.max;
1313 if (cpufreq_driver->get && has_target()) {
1314 policy->cur = cpufreq_driver->get(policy->cpu);
1316 pr_err("%s: ->get() failed\n", __func__);
1317 goto out_destroy_policy;
1322 * Sometimes boot loaders set CPU frequency to a value outside of
1323 * frequency table present with cpufreq core. In such cases CPU might be
1324 * unstable if it has to run on that frequency for long duration of time
1325 * and so its better to set it to a frequency which is specified in
1326 * freq-table. This also makes cpufreq stats inconsistent as
1327 * cpufreq-stats would fail to register because current frequency of CPU
1328 * isn't found in freq-table.
1330 * Because we don't want this change to effect boot process badly, we go
1331 * for the next freq which is >= policy->cur ('cur' must be set by now,
1332 * otherwise we will end up setting freq to lowest of the table as 'cur'
1333 * is initialized to zero).
1335 * We are passing target-freq as "policy->cur - 1" otherwise
1336 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1337 * equal to target-freq.
1339 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1341 /* Are we running at unknown frequency ? */
1342 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1343 if (ret == -EINVAL) {
1344 /* Warn user and fix it */
1345 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1346 __func__, policy->cpu, policy->cur);
1347 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1348 CPUFREQ_RELATION_L);
1351 * Reaching here after boot in a few seconds may not
1352 * mean that system will remain stable at "unknown"
1353 * frequency for longer duration. Hence, a BUG_ON().
1356 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1357 __func__, policy->cpu, policy->cur);
1362 ret = cpufreq_add_dev_interface(policy);
1364 goto out_destroy_policy;
1366 cpufreq_stats_create_table(policy);
1368 write_lock_irqsave(&cpufreq_driver_lock, flags);
1369 list_add(&policy->policy_list, &cpufreq_policy_list);
1370 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1373 ret = cpufreq_init_policy(policy);
1375 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1376 __func__, cpu, ret);
1377 goto out_destroy_policy;
1380 up_write(&policy->rwsem);
1382 kobject_uevent(&policy->kobj, KOBJ_ADD);
1384 /* Callback for handling stuff after policy is ready */
1385 if (cpufreq_driver->ready)
1386 cpufreq_driver->ready(policy);
1388 if (cpufreq_thermal_control_enabled(cpufreq_driver))
1389 policy->cdev = of_cpufreq_cooling_register(policy);
1391 pr_debug("initialization complete\n");
1396 for_each_cpu(j, policy->real_cpus)
1397 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1399 up_write(&policy->rwsem);
1402 if (cpufreq_driver->exit)
1403 cpufreq_driver->exit(policy);
1406 cpufreq_policy_free(policy);
1411 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1413 * @sif: Subsystem interface structure pointer (not used)
1415 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1417 struct cpufreq_policy *policy;
1418 unsigned cpu = dev->id;
1421 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1423 if (cpu_online(cpu)) {
1424 ret = cpufreq_online(cpu);
1429 /* Create sysfs link on CPU registration */
1430 policy = per_cpu(cpufreq_cpu_data, cpu);
1432 add_cpu_dev_symlink(policy, cpu);
1437 static int cpufreq_offline(unsigned int cpu)
1439 struct cpufreq_policy *policy;
1442 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1444 policy = cpufreq_cpu_get_raw(cpu);
1446 pr_debug("%s: No cpu_data found\n", __func__);
1450 down_write(&policy->rwsem);
1452 cpufreq_stop_governor(policy);
1454 cpumask_clear_cpu(cpu, policy->cpus);
1456 if (policy_is_inactive(policy)) {
1458 strncpy(policy->last_governor, policy->governor->name,
1461 policy->last_policy = policy->policy;
1462 } else if (cpu == policy->cpu) {
1463 /* Nominate new CPU */
1464 policy->cpu = cpumask_any(policy->cpus);
1467 /* Start governor again for active policy */
1468 if (!policy_is_inactive(policy)) {
1470 ret = cpufreq_start_governor(policy);
1472 pr_err("%s: Failed to start governor\n", __func__);
1478 if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
1479 cpufreq_cooling_unregister(policy->cdev);
1480 policy->cdev = NULL;
1483 if (cpufreq_driver->stop_cpu)
1484 cpufreq_driver->stop_cpu(policy);
1487 cpufreq_exit_governor(policy);
1490 * Perform the ->offline() during light-weight tear-down, as
1491 * that allows fast recovery when the CPU comes back.
1493 if (cpufreq_driver->offline) {
1494 cpufreq_driver->offline(policy);
1495 } else if (cpufreq_driver->exit) {
1496 cpufreq_driver->exit(policy);
1497 policy->freq_table = NULL;
1501 up_write(&policy->rwsem);
1506 * cpufreq_remove_dev - remove a CPU device
1508 * Removes the cpufreq interface for a CPU device.
1510 static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1512 unsigned int cpu = dev->id;
1513 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1518 if (cpu_online(cpu))
1519 cpufreq_offline(cpu);
1521 cpumask_clear_cpu(cpu, policy->real_cpus);
1522 remove_cpu_dev_symlink(policy, dev);
1524 if (cpumask_empty(policy->real_cpus)) {
1525 /* We did light-weight exit earlier, do full tear down now */
1526 if (cpufreq_driver->offline)
1527 cpufreq_driver->exit(policy);
1529 cpufreq_policy_free(policy);
1534 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1536 * @policy: policy managing CPUs
1537 * @new_freq: CPU frequency the CPU actually runs at
1539 * We adjust to current frequency first, and need to clean up later.
1540 * So either call to cpufreq_update_policy() or schedule handle_update()).
1542 static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1543 unsigned int new_freq)
1545 struct cpufreq_freqs freqs;
1547 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1548 policy->cur, new_freq);
1550 freqs.old = policy->cur;
1551 freqs.new = new_freq;
1553 cpufreq_freq_transition_begin(policy, &freqs);
1554 cpufreq_freq_transition_end(policy, &freqs, 0);
1557 static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update)
1559 unsigned int new_freq;
1561 new_freq = cpufreq_driver->get(policy->cpu);
1566 * If fast frequency switching is used with the given policy, the check
1567 * against policy->cur is pointless, so skip it in that case.
1569 if (policy->fast_switch_enabled || !has_target())
1572 if (policy->cur != new_freq) {
1573 cpufreq_out_of_sync(policy, new_freq);
1575 schedule_work(&policy->update);
1582 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1585 * This is the last known freq, without actually getting it from the driver.
1586 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1588 unsigned int cpufreq_quick_get(unsigned int cpu)
1590 struct cpufreq_policy *policy;
1591 unsigned int ret_freq = 0;
1592 unsigned long flags;
1594 read_lock_irqsave(&cpufreq_driver_lock, flags);
1596 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1597 ret_freq = cpufreq_driver->get(cpu);
1598 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1602 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1604 policy = cpufreq_cpu_get(cpu);
1606 ret_freq = policy->cur;
1607 cpufreq_cpu_put(policy);
1612 EXPORT_SYMBOL(cpufreq_quick_get);
1615 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1618 * Just return the max possible frequency for a given CPU.
1620 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1622 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1623 unsigned int ret_freq = 0;
1626 ret_freq = policy->max;
1627 cpufreq_cpu_put(policy);
1632 EXPORT_SYMBOL(cpufreq_quick_get_max);
1634 static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
1636 if (unlikely(policy_is_inactive(policy)))
1639 return cpufreq_verify_current_freq(policy, true);
1643 * cpufreq_get - get the current CPU frequency (in kHz)
1646 * Get the CPU current (static) CPU frequency
1648 unsigned int cpufreq_get(unsigned int cpu)
1650 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1651 unsigned int ret_freq = 0;
1654 down_read(&policy->rwsem);
1655 if (cpufreq_driver->get)
1656 ret_freq = __cpufreq_get(policy);
1657 up_read(&policy->rwsem);
1659 cpufreq_cpu_put(policy);
1664 EXPORT_SYMBOL(cpufreq_get);
1666 static struct subsys_interface cpufreq_interface = {
1668 .subsys = &cpu_subsys,
1669 .add_dev = cpufreq_add_dev,
1670 .remove_dev = cpufreq_remove_dev,
1674 * In case platform wants some specific frequency to be configured
1677 int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1681 if (!policy->suspend_freq) {
1682 pr_debug("%s: suspend_freq not defined\n", __func__);
1686 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1687 policy->suspend_freq);
1689 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1690 CPUFREQ_RELATION_H);
1692 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1693 __func__, policy->suspend_freq, ret);
1697 EXPORT_SYMBOL(cpufreq_generic_suspend);
1700 * cpufreq_suspend() - Suspend CPUFreq governors
1702 * Called during system wide Suspend/Hibernate cycles for suspending governors
1703 * as some platforms can't change frequency after this point in suspend cycle.
1704 * Because some of the devices (like: i2c, regulators, etc) they use for
1705 * changing frequency are suspended quickly after this point.
1707 void cpufreq_suspend(void)
1709 struct cpufreq_policy *policy;
1711 if (!cpufreq_driver)
1714 if (!has_target() && !cpufreq_driver->suspend)
1717 pr_debug("%s: Suspending Governors\n", __func__);
1719 for_each_active_policy(policy) {
1721 down_write(&policy->rwsem);
1722 cpufreq_stop_governor(policy);
1723 up_write(&policy->rwsem);
1726 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
1727 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1732 cpufreq_suspended = true;
1736 * cpufreq_resume() - Resume CPUFreq governors
1738 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1739 * are suspended with cpufreq_suspend().
1741 void cpufreq_resume(void)
1743 struct cpufreq_policy *policy;
1746 if (!cpufreq_driver)
1749 if (unlikely(!cpufreq_suspended))
1752 cpufreq_suspended = false;
1754 if (!has_target() && !cpufreq_driver->resume)
1757 pr_debug("%s: Resuming Governors\n", __func__);
1759 for_each_active_policy(policy) {
1760 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
1761 pr_err("%s: Failed to resume driver: %p\n", __func__,
1763 } else if (has_target()) {
1764 down_write(&policy->rwsem);
1765 ret = cpufreq_start_governor(policy);
1766 up_write(&policy->rwsem);
1769 pr_err("%s: Failed to start governor for policy: %p\n",
1776 * cpufreq_get_current_driver - return current driver's name
1778 * Return the name string of the currently loaded cpufreq driver
1781 const char *cpufreq_get_current_driver(void)
1784 return cpufreq_driver->name;
1788 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1791 * cpufreq_get_driver_data - return current driver data
1793 * Return the private data of the currently loaded cpufreq
1794 * driver, or NULL if no cpufreq driver is loaded.
1796 void *cpufreq_get_driver_data(void)
1799 return cpufreq_driver->driver_data;
1803 EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1805 /*********************************************************************
1806 * NOTIFIER LISTS INTERFACE *
1807 *********************************************************************/
1810 * cpufreq_register_notifier - register a driver with cpufreq
1811 * @nb: notifier function to register
1812 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1814 * Add a driver to one of two lists: either a list of drivers that
1815 * are notified about clock rate changes (once before and once after
1816 * the transition), or a list of drivers that are notified about
1817 * changes in cpufreq policy.
1819 * This function may sleep, and has the same return conditions as
1820 * blocking_notifier_chain_register.
1822 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1826 if (cpufreq_disabled())
1830 case CPUFREQ_TRANSITION_NOTIFIER:
1831 mutex_lock(&cpufreq_fast_switch_lock);
1833 if (cpufreq_fast_switch_count > 0) {
1834 mutex_unlock(&cpufreq_fast_switch_lock);
1837 ret = srcu_notifier_chain_register(
1838 &cpufreq_transition_notifier_list, nb);
1840 cpufreq_fast_switch_count--;
1842 mutex_unlock(&cpufreq_fast_switch_lock);
1844 case CPUFREQ_POLICY_NOTIFIER:
1845 ret = blocking_notifier_chain_register(
1846 &cpufreq_policy_notifier_list, nb);
1854 EXPORT_SYMBOL(cpufreq_register_notifier);
1857 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1858 * @nb: notifier block to be unregistered
1859 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1861 * Remove a driver from the CPU frequency notifier list.
1863 * This function may sleep, and has the same return conditions as
1864 * blocking_notifier_chain_unregister.
1866 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1870 if (cpufreq_disabled())
1874 case CPUFREQ_TRANSITION_NOTIFIER:
1875 mutex_lock(&cpufreq_fast_switch_lock);
1877 ret = srcu_notifier_chain_unregister(
1878 &cpufreq_transition_notifier_list, nb);
1879 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1880 cpufreq_fast_switch_count++;
1882 mutex_unlock(&cpufreq_fast_switch_lock);
1884 case CPUFREQ_POLICY_NOTIFIER:
1885 ret = blocking_notifier_chain_unregister(
1886 &cpufreq_policy_notifier_list, nb);
1894 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1897 /*********************************************************************
1899 *********************************************************************/
1902 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1903 * @policy: cpufreq policy to switch the frequency for.
1904 * @target_freq: New frequency to set (may be approximate).
1906 * Carry out a fast frequency switch without sleeping.
1908 * The driver's ->fast_switch() callback invoked by this function must be
1909 * suitable for being called from within RCU-sched read-side critical sections
1910 * and it is expected to select the minimum available frequency greater than or
1911 * equal to @target_freq (CPUFREQ_RELATION_L).
1913 * This function must not be called if policy->fast_switch_enabled is unset.
1915 * Governors calling this function must guarantee that it will never be invoked
1916 * twice in parallel for the same policy and that it will never be called in
1917 * parallel with either ->target() or ->target_index() for the same policy.
1919 * Returns the actual frequency set for the CPU.
1921 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
1922 * error condition, the hardware configuration must be preserved.
1924 unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1925 unsigned int target_freq)
1927 target_freq = clamp_val(target_freq, policy->min, policy->max);
1929 return cpufreq_driver->fast_switch(policy, target_freq);
1931 EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1933 /* Must set freqs->new to intermediate frequency */
1934 static int __target_intermediate(struct cpufreq_policy *policy,
1935 struct cpufreq_freqs *freqs, int index)
1939 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1941 /* We don't need to switch to intermediate freq */
1945 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1946 __func__, policy->cpu, freqs->old, freqs->new);
1948 cpufreq_freq_transition_begin(policy, freqs);
1949 ret = cpufreq_driver->target_intermediate(policy, index);
1950 cpufreq_freq_transition_end(policy, freqs, ret);
1953 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1959 static int __target_index(struct cpufreq_policy *policy, int index)
1961 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1962 unsigned int intermediate_freq = 0;
1963 unsigned int newfreq = policy->freq_table[index].frequency;
1964 int retval = -EINVAL;
1967 if (newfreq == policy->cur)
1970 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1972 /* Handle switching to intermediate frequency */
1973 if (cpufreq_driver->get_intermediate) {
1974 retval = __target_intermediate(policy, &freqs, index);
1978 intermediate_freq = freqs.new;
1979 /* Set old freq to intermediate */
1980 if (intermediate_freq)
1981 freqs.old = freqs.new;
1984 freqs.new = newfreq;
1985 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1986 __func__, policy->cpu, freqs.old, freqs.new);
1988 cpufreq_freq_transition_begin(policy, &freqs);
1991 retval = cpufreq_driver->target_index(policy, index);
1993 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1997 cpufreq_freq_transition_end(policy, &freqs, retval);
2000 * Failed after setting to intermediate freq? Driver should have
2001 * reverted back to initial frequency and so should we. Check
2002 * here for intermediate_freq instead of get_intermediate, in
2003 * case we haven't switched to intermediate freq at all.
2005 if (unlikely(retval && intermediate_freq)) {
2006 freqs.old = intermediate_freq;
2007 freqs.new = policy->restore_freq;
2008 cpufreq_freq_transition_begin(policy, &freqs);
2009 cpufreq_freq_transition_end(policy, &freqs, 0);
2016 int __cpufreq_driver_target(struct cpufreq_policy *policy,
2017 unsigned int target_freq,
2018 unsigned int relation)
2020 unsigned int old_target_freq = target_freq;
2023 if (cpufreq_disabled())
2026 /* Make sure that target_freq is within supported range */
2027 target_freq = clamp_val(target_freq, policy->min, policy->max);
2029 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
2030 policy->cpu, target_freq, relation, old_target_freq);
2033 * This might look like a redundant call as we are checking it again
2034 * after finding index. But it is left intentionally for cases where
2035 * exactly same freq is called again and so we can save on few function
2038 if (target_freq == policy->cur)
2041 /* Save last value to restore later on errors */
2042 policy->restore_freq = policy->cur;
2044 if (cpufreq_driver->target)
2045 return cpufreq_driver->target(policy, target_freq, relation);
2047 if (!cpufreq_driver->target_index)
2050 index = cpufreq_frequency_table_target(policy, target_freq, relation);
2052 return __target_index(policy, index);
2054 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2056 int cpufreq_driver_target(struct cpufreq_policy *policy,
2057 unsigned int target_freq,
2058 unsigned int relation)
2062 down_write(&policy->rwsem);
2064 ret = __cpufreq_driver_target(policy, target_freq, relation);
2066 up_write(&policy->rwsem);
2070 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2072 __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2077 static int cpufreq_init_governor(struct cpufreq_policy *policy)
2081 /* Don't start any governor operations if we are entering suspend */
2082 if (cpufreq_suspended)
2085 * Governor might not be initiated here if ACPI _PPC changed
2086 * notification happened, so check it.
2088 if (!policy->governor)
2091 /* Platform doesn't want dynamic frequency switching ? */
2092 if (policy->governor->dynamic_switching &&
2093 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
2094 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2097 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
2098 policy->governor->name, gov->name);
2099 policy->governor = gov;
2105 if (!try_module_get(policy->governor->owner))
2108 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2110 if (policy->governor->init) {
2111 ret = policy->governor->init(policy);
2113 module_put(policy->governor->owner);
2121 static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2123 if (cpufreq_suspended || !policy->governor)
2126 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2128 if (policy->governor->exit)
2129 policy->governor->exit(policy);
2131 module_put(policy->governor->owner);
2134 static int cpufreq_start_governor(struct cpufreq_policy *policy)
2138 if (cpufreq_suspended)
2141 if (!policy->governor)
2144 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2146 if (cpufreq_driver->get)
2147 cpufreq_verify_current_freq(policy, false);
2149 if (policy->governor->start) {
2150 ret = policy->governor->start(policy);
2155 if (policy->governor->limits)
2156 policy->governor->limits(policy);
2161 static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2163 if (cpufreq_suspended || !policy->governor)
2166 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2168 if (policy->governor->stop)
2169 policy->governor->stop(policy);
2172 static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2174 if (cpufreq_suspended || !policy->governor)
2177 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2179 if (policy->governor->limits)
2180 policy->governor->limits(policy);
2183 int cpufreq_register_governor(struct cpufreq_governor *governor)
2190 if (cpufreq_disabled())
2193 mutex_lock(&cpufreq_governor_mutex);
2196 if (!find_governor(governor->name)) {
2198 list_add(&governor->governor_list, &cpufreq_governor_list);
2201 mutex_unlock(&cpufreq_governor_mutex);
2204 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2206 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2208 struct cpufreq_policy *policy;
2209 unsigned long flags;
2214 if (cpufreq_disabled())
2217 /* clear last_governor for all inactive policies */
2218 read_lock_irqsave(&cpufreq_driver_lock, flags);
2219 for_each_inactive_policy(policy) {
2220 if (!strcmp(policy->last_governor, governor->name)) {
2221 policy->governor = NULL;
2222 strcpy(policy->last_governor, "\0");
2225 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2227 mutex_lock(&cpufreq_governor_mutex);
2228 list_del(&governor->governor_list);
2229 mutex_unlock(&cpufreq_governor_mutex);
2231 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2234 /*********************************************************************
2235 * POLICY INTERFACE *
2236 *********************************************************************/
2239 * cpufreq_get_policy - get the current cpufreq_policy
2240 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2243 * Reads the current cpufreq policy.
2245 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2247 struct cpufreq_policy *cpu_policy;
2251 cpu_policy = cpufreq_cpu_get(cpu);
2255 memcpy(policy, cpu_policy, sizeof(*policy));
2257 cpufreq_cpu_put(cpu_policy);
2260 EXPORT_SYMBOL(cpufreq_get_policy);
2263 * cpufreq_set_policy - Modify cpufreq policy parameters.
2264 * @policy: Policy object to modify.
2265 * @new_policy: New policy data.
2267 * Pass @new_policy to the cpufreq driver's ->verify() callback, run the
2268 * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to
2269 * the driver's ->verify() callback again and run the notifiers for it again
2270 * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters
2271 * of @new_policy to @policy and either invoke the driver's ->setpolicy()
2272 * callback (if present) or carry out a governor update for @policy. That is,
2273 * run the current governor's ->limits() callback (if the governor field in
2274 * @new_policy points to the same object as the one in @policy) or replace the
2275 * governor for @policy with the new one stored in @new_policy.
2277 * The cpuinfo part of @policy is not updated by this function.
2279 int cpufreq_set_policy(struct cpufreq_policy *policy,
2280 struct cpufreq_policy *new_policy)
2282 struct cpufreq_governor *old_gov;
2285 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2286 new_policy->cpu, new_policy->min, new_policy->max);
2288 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2291 * This check works well when we store new min/max freq attributes,
2292 * because new_policy is a copy of policy with one field updated.
2294 if (new_policy->min > new_policy->max)
2297 /* verify the cpu speed can be set within this limit */
2298 ret = cpufreq_driver->verify(new_policy);
2302 /* adjust if necessary - all reasons */
2303 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2304 CPUFREQ_ADJUST, new_policy);
2307 * verify the cpu speed can be set within this limit, which might be
2308 * different to the first one
2310 ret = cpufreq_driver->verify(new_policy);
2314 /* notification of the new policy */
2315 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2316 CPUFREQ_NOTIFY, new_policy);
2318 policy->min = new_policy->min;
2319 policy->max = new_policy->max;
2320 trace_cpu_frequency_limits(policy);
2322 policy->cached_target_freq = UINT_MAX;
2324 pr_debug("new min and max freqs are %u - %u kHz\n",
2325 policy->min, policy->max);
2327 if (cpufreq_driver->setpolicy) {
2328 policy->policy = new_policy->policy;
2329 pr_debug("setting range\n");
2330 return cpufreq_driver->setpolicy(policy);
2333 if (new_policy->governor == policy->governor) {
2334 pr_debug("governor limits update\n");
2335 cpufreq_governor_limits(policy);
2339 pr_debug("governor switch\n");
2341 /* save old, working values */
2342 old_gov = policy->governor;
2343 /* end old governor */
2345 cpufreq_stop_governor(policy);
2346 cpufreq_exit_governor(policy);
2349 /* start new governor */
2350 policy->governor = new_policy->governor;
2351 ret = cpufreq_init_governor(policy);
2353 ret = cpufreq_start_governor(policy);
2355 pr_debug("governor change\n");
2356 sched_cpufreq_governor_change(policy, old_gov);
2359 cpufreq_exit_governor(policy);
2362 /* new governor failed, so re-start old one */
2363 pr_debug("starting governor %s failed\n", policy->governor->name);
2365 policy->governor = old_gov;
2366 if (cpufreq_init_governor(policy))
2367 policy->governor = NULL;
2369 cpufreq_start_governor(policy);
2376 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2377 * @cpu: CPU to re-evaluate the policy for.
2379 * Update the current frequency for the cpufreq policy of @cpu and use
2380 * cpufreq_set_policy() to re-apply the min and max limits saved in the
2381 * user_policy sub-structure of that policy, which triggers the evaluation
2382 * of policy notifiers and the cpufreq driver's ->verify() callback for the
2383 * policy in question, among other things.
2385 void cpufreq_update_policy(unsigned int cpu)
2387 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
2393 * BIOS might change freq behind our back
2394 * -> ask driver for current freq and notify governors about a change
2396 if (cpufreq_driver->get && has_target() &&
2397 (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
2400 refresh_frequency_limits(policy);
2403 cpufreq_cpu_release(policy);
2405 EXPORT_SYMBOL(cpufreq_update_policy);
2408 * cpufreq_update_limits - Update policy limits for a given CPU.
2409 * @cpu: CPU to update the policy limits for.
2411 * Invoke the driver's ->update_limits callback if present or call
2412 * cpufreq_update_policy() for @cpu.
2414 void cpufreq_update_limits(unsigned int cpu)
2416 if (cpufreq_driver->update_limits)
2417 cpufreq_driver->update_limits(cpu);
2419 cpufreq_update_policy(cpu);
2421 EXPORT_SYMBOL_GPL(cpufreq_update_limits);
2423 /*********************************************************************
2425 *********************************************************************/
2426 static int cpufreq_boost_set_sw(int state)
2428 struct cpufreq_policy *policy;
2431 for_each_active_policy(policy) {
2432 if (!policy->freq_table)
2435 ret = cpufreq_frequency_table_cpuinfo(policy,
2436 policy->freq_table);
2438 pr_err("%s: Policy frequency update failed\n",
2443 down_write(&policy->rwsem);
2444 policy->user_policy.max = policy->max;
2445 cpufreq_governor_limits(policy);
2446 up_write(&policy->rwsem);
2452 int cpufreq_boost_trigger_state(int state)
2454 unsigned long flags;
2457 if (cpufreq_driver->boost_enabled == state)
2460 write_lock_irqsave(&cpufreq_driver_lock, flags);
2461 cpufreq_driver->boost_enabled = state;
2462 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2464 ret = cpufreq_driver->set_boost(state);
2466 write_lock_irqsave(&cpufreq_driver_lock, flags);
2467 cpufreq_driver->boost_enabled = !state;
2468 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2470 pr_err("%s: Cannot %s BOOST\n",
2471 __func__, state ? "enable" : "disable");
2477 static bool cpufreq_boost_supported(void)
2479 return cpufreq_driver->set_boost;
2482 static int create_boost_sysfs_file(void)
2486 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
2488 pr_err("%s: cannot register global BOOST sysfs file\n",
2494 static void remove_boost_sysfs_file(void)
2496 if (cpufreq_boost_supported())
2497 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
2500 int cpufreq_enable_boost_support(void)
2502 if (!cpufreq_driver)
2505 if (cpufreq_boost_supported())
2508 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2510 /* This will get removed on driver unregister */
2511 return create_boost_sysfs_file();
2513 EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2515 int cpufreq_boost_enabled(void)
2517 return cpufreq_driver->boost_enabled;
2519 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2521 /*********************************************************************
2522 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2523 *********************************************************************/
2524 static enum cpuhp_state hp_online;
2526 static int cpuhp_cpufreq_online(unsigned int cpu)
2528 cpufreq_online(cpu);
2533 static int cpuhp_cpufreq_offline(unsigned int cpu)
2535 cpufreq_offline(cpu);
2541 * cpufreq_register_driver - register a CPU Frequency driver
2542 * @driver_data: A struct cpufreq_driver containing the values#
2543 * submitted by the CPU Frequency driver.
2545 * Registers a CPU Frequency driver to this core code. This code
2546 * returns zero on success, -EEXIST when another driver got here first
2547 * (and isn't unregistered in the meantime).
2550 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2552 unsigned long flags;
2555 if (cpufreq_disabled())
2558 if (!driver_data || !driver_data->verify || !driver_data->init ||
2559 !(driver_data->setpolicy || driver_data->target_index ||
2560 driver_data->target) ||
2561 (driver_data->setpolicy && (driver_data->target_index ||
2562 driver_data->target)) ||
2563 (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
2564 (!driver_data->online != !driver_data->offline))
2567 pr_debug("trying to register driver %s\n", driver_data->name);
2569 /* Protect against concurrent CPU online/offline. */
2572 write_lock_irqsave(&cpufreq_driver_lock, flags);
2573 if (cpufreq_driver) {
2574 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2578 cpufreq_driver = driver_data;
2579 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2581 if (driver_data->setpolicy)
2582 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2584 if (cpufreq_boost_supported()) {
2585 ret = create_boost_sysfs_file();
2587 goto err_null_driver;
2590 ret = subsys_interface_register(&cpufreq_interface);
2592 goto err_boost_unreg;
2594 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2595 list_empty(&cpufreq_policy_list)) {
2596 /* if all ->init() calls failed, unregister */
2598 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2603 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2605 cpuhp_cpufreq_online,
2606 cpuhp_cpufreq_offline);
2612 pr_debug("driver %s up and running\n", driver_data->name);
2616 subsys_interface_unregister(&cpufreq_interface);
2618 remove_boost_sysfs_file();
2620 write_lock_irqsave(&cpufreq_driver_lock, flags);
2621 cpufreq_driver = NULL;
2622 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2627 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2630 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2632 * Unregister the current CPUFreq driver. Only call this if you have
2633 * the right to do so, i.e. if you have succeeded in initialising before!
2634 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2635 * currently not initialised.
2637 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2639 unsigned long flags;
2641 if (!cpufreq_driver || (driver != cpufreq_driver))
2644 pr_debug("unregistering driver %s\n", driver->name);
2646 /* Protect against concurrent cpu hotplug */
2648 subsys_interface_unregister(&cpufreq_interface);
2649 remove_boost_sysfs_file();
2650 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
2652 write_lock_irqsave(&cpufreq_driver_lock, flags);
2654 cpufreq_driver = NULL;
2656 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2661 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2664 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2665 * or mutexes when secondary CPUs are halted.
2667 static struct syscore_ops cpufreq_syscore_ops = {
2668 .shutdown = cpufreq_suspend,
2671 struct kobject *cpufreq_global_kobject;
2672 EXPORT_SYMBOL(cpufreq_global_kobject);
2674 static int __init cpufreq_core_init(void)
2676 if (cpufreq_disabled())
2679 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
2680 BUG_ON(!cpufreq_global_kobject);
2682 register_syscore_ops(&cpufreq_syscore_ops);
2686 module_param(off, int, 0444);
2687 core_initcall(cpufreq_core_init);