treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / cpufreq / cpufreq.c
blob4adac3a8c2656b43d3a19d34e63bd486c885038c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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/pm_qos.h>
27 #include <linux/slab.h>
28 #include <linux/suspend.h>
29 #include <linux/syscore_ops.h>
30 #include <linux/tick.h>
31 #include <trace/events/power.h>
33 static LIST_HEAD(cpufreq_policy_list);
35 /* Macros to iterate over CPU policies */
36 #define for_each_suitable_policy(__policy, __active) \
37 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
38 if ((__active) == !policy_is_inactive(__policy))
40 #define for_each_active_policy(__policy) \
41 for_each_suitable_policy(__policy, true)
42 #define for_each_inactive_policy(__policy) \
43 for_each_suitable_policy(__policy, false)
45 #define for_each_policy(__policy) \
46 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
48 /* Iterate over governors */
49 static LIST_HEAD(cpufreq_governor_list);
50 #define for_each_governor(__governor) \
51 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
53 /**
54 * The "cpufreq driver" - the arch- or hardware-dependent low
55 * level driver of CPUFreq support, and its spinlock. This lock
56 * also protects the cpufreq_cpu_data array.
58 static struct cpufreq_driver *cpufreq_driver;
59 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
60 static DEFINE_RWLOCK(cpufreq_driver_lock);
62 /* Flag to suspend/resume CPUFreq governors */
63 static bool cpufreq_suspended;
65 static inline bool has_target(void)
67 return cpufreq_driver->target_index || cpufreq_driver->target;
70 /* internal prototypes */
71 static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
72 static int cpufreq_init_governor(struct cpufreq_policy *policy);
73 static void cpufreq_exit_governor(struct cpufreq_policy *policy);
74 static int cpufreq_start_governor(struct cpufreq_policy *policy);
75 static void cpufreq_stop_governor(struct cpufreq_policy *policy);
76 static void cpufreq_governor_limits(struct cpufreq_policy *policy);
77 static int cpufreq_set_policy(struct cpufreq_policy *policy,
78 struct cpufreq_governor *new_gov,
79 unsigned int new_pol);
81 /**
82 * Two notifier lists: the "policy" list is involved in the
83 * validation process for a new CPU frequency policy; the
84 * "transition" list for kernel code that needs to handle
85 * changes to devices when the CPU clock speed changes.
86 * The mutex locks both lists.
88 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
89 SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
91 static int off __read_mostly;
92 static int cpufreq_disabled(void)
94 return off;
96 void disable_cpufreq(void)
98 off = 1;
100 static DEFINE_MUTEX(cpufreq_governor_mutex);
102 bool have_governor_per_policy(void)
104 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
106 EXPORT_SYMBOL_GPL(have_governor_per_policy);
108 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
110 if (have_governor_per_policy())
111 return &policy->kobj;
112 else
113 return cpufreq_global_kobject;
115 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
117 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
119 struct kernel_cpustat kcpustat;
120 u64 cur_wall_time;
121 u64 idle_time;
122 u64 busy_time;
124 cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
126 kcpustat_cpu_fetch(&kcpustat, cpu);
128 busy_time = kcpustat.cpustat[CPUTIME_USER];
129 busy_time += kcpustat.cpustat[CPUTIME_SYSTEM];
130 busy_time += kcpustat.cpustat[CPUTIME_IRQ];
131 busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ];
132 busy_time += kcpustat.cpustat[CPUTIME_STEAL];
133 busy_time += kcpustat.cpustat[CPUTIME_NICE];
135 idle_time = cur_wall_time - busy_time;
136 if (wall)
137 *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
139 return div_u64(idle_time, NSEC_PER_USEC);
142 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
144 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
146 if (idle_time == -1ULL)
147 return get_cpu_idle_time_jiffy(cpu, wall);
148 else if (!io_busy)
149 idle_time += get_cpu_iowait_time_us(cpu, wall);
151 return idle_time;
153 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
155 __weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
156 unsigned long max_freq)
159 EXPORT_SYMBOL_GPL(arch_set_freq_scale);
162 * This is a generic cpufreq init() routine which can be used by cpufreq
163 * drivers of SMP systems. It will do following:
164 * - validate & show freq table passed
165 * - set policies transition latency
166 * - policy->cpus with all possible CPUs
168 void cpufreq_generic_init(struct cpufreq_policy *policy,
169 struct cpufreq_frequency_table *table,
170 unsigned int transition_latency)
172 policy->freq_table = table;
173 policy->cpuinfo.transition_latency = transition_latency;
176 * The driver only supports the SMP configuration where all processors
177 * share the clock and voltage and clock.
179 cpumask_setall(policy->cpus);
181 EXPORT_SYMBOL_GPL(cpufreq_generic_init);
183 struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
185 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
187 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
189 EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
191 unsigned int cpufreq_generic_get(unsigned int cpu)
193 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
195 if (!policy || IS_ERR(policy->clk)) {
196 pr_err("%s: No %s associated to cpu: %d\n",
197 __func__, policy ? "clk" : "policy", cpu);
198 return 0;
201 return clk_get_rate(policy->clk) / 1000;
203 EXPORT_SYMBOL_GPL(cpufreq_generic_get);
206 * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
207 * @cpu: CPU to find the policy for.
209 * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
210 * the kobject reference counter of that policy. Return a valid policy on
211 * success or NULL on failure.
213 * The policy returned by this function has to be released with the help of
214 * cpufreq_cpu_put() to balance its kobject reference counter properly.
216 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
218 struct cpufreq_policy *policy = NULL;
219 unsigned long flags;
221 if (WARN_ON(cpu >= nr_cpu_ids))
222 return NULL;
224 /* get the cpufreq driver */
225 read_lock_irqsave(&cpufreq_driver_lock, flags);
227 if (cpufreq_driver) {
228 /* get the CPU */
229 policy = cpufreq_cpu_get_raw(cpu);
230 if (policy)
231 kobject_get(&policy->kobj);
234 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
236 return policy;
238 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
241 * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
242 * @policy: cpufreq policy returned by cpufreq_cpu_get().
244 void cpufreq_cpu_put(struct cpufreq_policy *policy)
246 kobject_put(&policy->kobj);
248 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
251 * cpufreq_cpu_release - Unlock a policy and decrement its usage counter.
252 * @policy: cpufreq policy returned by cpufreq_cpu_acquire().
254 void cpufreq_cpu_release(struct cpufreq_policy *policy)
256 if (WARN_ON(!policy))
257 return;
259 lockdep_assert_held(&policy->rwsem);
261 up_write(&policy->rwsem);
263 cpufreq_cpu_put(policy);
267 * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it.
268 * @cpu: CPU to find the policy for.
270 * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and
271 * if the policy returned by it is not NULL, acquire its rwsem for writing.
272 * Return the policy if it is active or release it and return NULL otherwise.
274 * The policy returned by this function has to be released with the help of
275 * cpufreq_cpu_release() in order to release its rwsem and balance its usage
276 * counter properly.
278 struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu)
280 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
282 if (!policy)
283 return NULL;
285 down_write(&policy->rwsem);
287 if (policy_is_inactive(policy)) {
288 cpufreq_cpu_release(policy);
289 return NULL;
292 return policy;
295 /*********************************************************************
296 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
297 *********************************************************************/
300 * adjust_jiffies - adjust the system "loops_per_jiffy"
302 * This function alters the system "loops_per_jiffy" for the clock
303 * speed change. Note that loops_per_jiffy cannot be updated on SMP
304 * systems as each CPU might be scaled differently. So, use the arch
305 * per-CPU loops_per_jiffy value wherever possible.
307 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
309 #ifndef CONFIG_SMP
310 static unsigned long l_p_j_ref;
311 static unsigned int l_p_j_ref_freq;
313 if (ci->flags & CPUFREQ_CONST_LOOPS)
314 return;
316 if (!l_p_j_ref_freq) {
317 l_p_j_ref = loops_per_jiffy;
318 l_p_j_ref_freq = ci->old;
319 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
320 l_p_j_ref, l_p_j_ref_freq);
322 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
323 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
324 ci->new);
325 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
326 loops_per_jiffy, ci->new);
328 #endif
332 * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies.
333 * @policy: cpufreq policy to enable fast frequency switching for.
334 * @freqs: contain details of the frequency update.
335 * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
337 * This function calls the transition notifiers and the "adjust_jiffies"
338 * function. It is called twice on all CPU frequency changes that have
339 * external effects.
341 static void cpufreq_notify_transition(struct cpufreq_policy *policy,
342 struct cpufreq_freqs *freqs,
343 unsigned int state)
345 int cpu;
347 BUG_ON(irqs_disabled());
349 if (cpufreq_disabled())
350 return;
352 freqs->policy = policy;
353 freqs->flags = cpufreq_driver->flags;
354 pr_debug("notification %u of frequency transition to %u kHz\n",
355 state, freqs->new);
357 switch (state) {
358 case CPUFREQ_PRECHANGE:
360 * Detect if the driver reported a value as "old frequency"
361 * which is not equal to what the cpufreq core thinks is
362 * "old frequency".
364 if (policy->cur && policy->cur != freqs->old) {
365 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
366 freqs->old, policy->cur);
367 freqs->old = policy->cur;
370 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
371 CPUFREQ_PRECHANGE, freqs);
373 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
374 break;
376 case CPUFREQ_POSTCHANGE:
377 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
378 pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
379 cpumask_pr_args(policy->cpus));
381 for_each_cpu(cpu, policy->cpus)
382 trace_cpu_frequency(freqs->new, cpu);
384 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
385 CPUFREQ_POSTCHANGE, freqs);
387 cpufreq_stats_record_transition(policy, freqs->new);
388 policy->cur = freqs->new;
392 /* Do post notifications when there are chances that transition has failed */
393 static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
394 struct cpufreq_freqs *freqs, int transition_failed)
396 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
397 if (!transition_failed)
398 return;
400 swap(freqs->old, freqs->new);
401 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
402 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
405 void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
406 struct cpufreq_freqs *freqs)
410 * Catch double invocations of _begin() which lead to self-deadlock.
411 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
412 * doesn't invoke _begin() on their behalf, and hence the chances of
413 * double invocations are very low. Moreover, there are scenarios
414 * where these checks can emit false-positive warnings in these
415 * drivers; so we avoid that by skipping them altogether.
417 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
418 && current == policy->transition_task);
420 wait:
421 wait_event(policy->transition_wait, !policy->transition_ongoing);
423 spin_lock(&policy->transition_lock);
425 if (unlikely(policy->transition_ongoing)) {
426 spin_unlock(&policy->transition_lock);
427 goto wait;
430 policy->transition_ongoing = true;
431 policy->transition_task = current;
433 spin_unlock(&policy->transition_lock);
435 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
437 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
439 void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
440 struct cpufreq_freqs *freqs, int transition_failed)
442 if (WARN_ON(!policy->transition_ongoing))
443 return;
445 cpufreq_notify_post_transition(policy, freqs, transition_failed);
447 policy->transition_ongoing = false;
448 policy->transition_task = NULL;
450 wake_up(&policy->transition_wait);
452 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
455 * Fast frequency switching status count. Positive means "enabled", negative
456 * means "disabled" and 0 means "not decided yet".
458 static int cpufreq_fast_switch_count;
459 static DEFINE_MUTEX(cpufreq_fast_switch_lock);
461 static void cpufreq_list_transition_notifiers(void)
463 struct notifier_block *nb;
465 pr_info("Registered transition notifiers:\n");
467 mutex_lock(&cpufreq_transition_notifier_list.mutex);
469 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
470 pr_info("%pS\n", nb->notifier_call);
472 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
476 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
477 * @policy: cpufreq policy to enable fast frequency switching for.
479 * Try to enable fast frequency switching for @policy.
481 * The attempt will fail if there is at least one transition notifier registered
482 * at this point, as fast frequency switching is quite fundamentally at odds
483 * with transition notifiers. Thus if successful, it will make registration of
484 * transition notifiers fail going forward.
486 void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
488 lockdep_assert_held(&policy->rwsem);
490 if (!policy->fast_switch_possible)
491 return;
493 mutex_lock(&cpufreq_fast_switch_lock);
494 if (cpufreq_fast_switch_count >= 0) {
495 cpufreq_fast_switch_count++;
496 policy->fast_switch_enabled = true;
497 } else {
498 pr_warn("CPU%u: Fast frequency switching not enabled\n",
499 policy->cpu);
500 cpufreq_list_transition_notifiers();
502 mutex_unlock(&cpufreq_fast_switch_lock);
504 EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
507 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
508 * @policy: cpufreq policy to disable fast frequency switching for.
510 void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
512 mutex_lock(&cpufreq_fast_switch_lock);
513 if (policy->fast_switch_enabled) {
514 policy->fast_switch_enabled = false;
515 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
516 cpufreq_fast_switch_count--;
518 mutex_unlock(&cpufreq_fast_switch_lock);
520 EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
523 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
524 * one.
525 * @target_freq: target frequency to resolve.
527 * The target to driver frequency mapping is cached in the policy.
529 * Return: Lowest driver-supported frequency greater than or equal to the
530 * given target_freq, subject to policy (min/max) and driver limitations.
532 unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
533 unsigned int target_freq)
535 target_freq = clamp_val(target_freq, policy->min, policy->max);
536 policy->cached_target_freq = target_freq;
538 if (cpufreq_driver->target_index) {
539 int idx;
541 idx = cpufreq_frequency_table_target(policy, target_freq,
542 CPUFREQ_RELATION_L);
543 policy->cached_resolved_idx = idx;
544 return policy->freq_table[idx].frequency;
547 if (cpufreq_driver->resolve_freq)
548 return cpufreq_driver->resolve_freq(policy, target_freq);
550 return target_freq;
552 EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
554 unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
556 unsigned int latency;
558 if (policy->transition_delay_us)
559 return policy->transition_delay_us;
561 latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
562 if (latency) {
564 * For platforms that can change the frequency very fast (< 10
565 * us), the above formula gives a decent transition delay. But
566 * for platforms where transition_latency is in milliseconds, it
567 * ends up giving unrealistic values.
569 * Cap the default transition delay to 10 ms, which seems to be
570 * a reasonable amount of time after which we should reevaluate
571 * the frequency.
573 return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
576 return LATENCY_MULTIPLIER;
578 EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
580 /*********************************************************************
581 * SYSFS INTERFACE *
582 *********************************************************************/
583 static ssize_t show_boost(struct kobject *kobj,
584 struct kobj_attribute *attr, char *buf)
586 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
589 static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
590 const char *buf, size_t count)
592 int ret, enable;
594 ret = sscanf(buf, "%d", &enable);
595 if (ret != 1 || enable < 0 || enable > 1)
596 return -EINVAL;
598 if (cpufreq_boost_trigger_state(enable)) {
599 pr_err("%s: Cannot %s BOOST!\n",
600 __func__, enable ? "enable" : "disable");
601 return -EINVAL;
604 pr_debug("%s: cpufreq BOOST %s\n",
605 __func__, enable ? "enabled" : "disabled");
607 return count;
609 define_one_global_rw(boost);
611 static struct cpufreq_governor *find_governor(const char *str_governor)
613 struct cpufreq_governor *t;
615 for_each_governor(t)
616 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
617 return t;
619 return NULL;
622 static unsigned int cpufreq_parse_policy(char *str_governor)
624 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN))
625 return CPUFREQ_POLICY_PERFORMANCE;
627 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN))
628 return CPUFREQ_POLICY_POWERSAVE;
630 return CPUFREQ_POLICY_UNKNOWN;
634 * cpufreq_parse_governor - parse a governor string only for has_target()
635 * @str_governor: Governor name.
637 static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor)
639 struct cpufreq_governor *t;
641 mutex_lock(&cpufreq_governor_mutex);
643 t = find_governor(str_governor);
644 if (!t) {
645 int ret;
647 mutex_unlock(&cpufreq_governor_mutex);
649 ret = request_module("cpufreq_%s", str_governor);
650 if (ret)
651 return NULL;
653 mutex_lock(&cpufreq_governor_mutex);
655 t = find_governor(str_governor);
657 if (t && !try_module_get(t->owner))
658 t = NULL;
660 mutex_unlock(&cpufreq_governor_mutex);
662 return t;
666 * cpufreq_per_cpu_attr_read() / show_##file_name() -
667 * print out cpufreq information
669 * Write out information from cpufreq_driver->policy[cpu]; object must be
670 * "unsigned int".
673 #define show_one(file_name, object) \
674 static ssize_t show_##file_name \
675 (struct cpufreq_policy *policy, char *buf) \
677 return sprintf(buf, "%u\n", policy->object); \
680 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
681 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
682 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
683 show_one(scaling_min_freq, min);
684 show_one(scaling_max_freq, max);
686 __weak unsigned int arch_freq_get_on_cpu(int cpu)
688 return 0;
691 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
693 ssize_t ret;
694 unsigned int freq;
696 freq = arch_freq_get_on_cpu(policy->cpu);
697 if (freq)
698 ret = sprintf(buf, "%u\n", freq);
699 else if (cpufreq_driver && cpufreq_driver->setpolicy &&
700 cpufreq_driver->get)
701 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
702 else
703 ret = sprintf(buf, "%u\n", policy->cur);
704 return ret;
708 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
710 #define store_one(file_name, object) \
711 static ssize_t store_##file_name \
712 (struct cpufreq_policy *policy, const char *buf, size_t count) \
714 unsigned long val; \
715 int ret; \
717 ret = sscanf(buf, "%lu", &val); \
718 if (ret != 1) \
719 return -EINVAL; \
721 ret = freq_qos_update_request(policy->object##_freq_req, val);\
722 return ret >= 0 ? count : ret; \
725 store_one(scaling_min_freq, min);
726 store_one(scaling_max_freq, max);
729 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
731 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
732 char *buf)
734 unsigned int cur_freq = __cpufreq_get(policy);
736 if (cur_freq)
737 return sprintf(buf, "%u\n", cur_freq);
739 return sprintf(buf, "<unknown>\n");
743 * show_scaling_governor - show the current policy for the specified CPU
745 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
747 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
748 return sprintf(buf, "powersave\n");
749 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
750 return sprintf(buf, "performance\n");
751 else if (policy->governor)
752 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
753 policy->governor->name);
754 return -EINVAL;
758 * store_scaling_governor - store policy for the specified CPU
760 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
761 const char *buf, size_t count)
763 char str_governor[16];
764 int ret;
766 ret = sscanf(buf, "%15s", str_governor);
767 if (ret != 1)
768 return -EINVAL;
770 if (cpufreq_driver->setpolicy) {
771 unsigned int new_pol;
773 new_pol = cpufreq_parse_policy(str_governor);
774 if (!new_pol)
775 return -EINVAL;
777 ret = cpufreq_set_policy(policy, NULL, new_pol);
778 } else {
779 struct cpufreq_governor *new_gov;
781 new_gov = cpufreq_parse_governor(str_governor);
782 if (!new_gov)
783 return -EINVAL;
785 ret = cpufreq_set_policy(policy, new_gov,
786 CPUFREQ_POLICY_UNKNOWN);
788 module_put(new_gov->owner);
791 return ret ? ret : count;
795 * show_scaling_driver - show the cpufreq driver currently loaded
797 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
799 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
803 * show_scaling_available_governors - show the available CPUfreq governors
805 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
806 char *buf)
808 ssize_t i = 0;
809 struct cpufreq_governor *t;
811 if (!has_target()) {
812 i += sprintf(buf, "performance powersave");
813 goto out;
816 for_each_governor(t) {
817 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
818 - (CPUFREQ_NAME_LEN + 2)))
819 goto out;
820 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
822 out:
823 i += sprintf(&buf[i], "\n");
824 return i;
827 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
829 ssize_t i = 0;
830 unsigned int cpu;
832 for_each_cpu(cpu, mask) {
833 if (i)
834 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
835 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
836 if (i >= (PAGE_SIZE - 5))
837 break;
839 i += sprintf(&buf[i], "\n");
840 return i;
842 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
845 * show_related_cpus - show the CPUs affected by each transition even if
846 * hw coordination is in use
848 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
850 return cpufreq_show_cpus(policy->related_cpus, buf);
854 * show_affected_cpus - show the CPUs affected by each transition
856 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
858 return cpufreq_show_cpus(policy->cpus, buf);
861 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
862 const char *buf, size_t count)
864 unsigned int freq = 0;
865 unsigned int ret;
867 if (!policy->governor || !policy->governor->store_setspeed)
868 return -EINVAL;
870 ret = sscanf(buf, "%u", &freq);
871 if (ret != 1)
872 return -EINVAL;
874 policy->governor->store_setspeed(policy, freq);
876 return count;
879 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
881 if (!policy->governor || !policy->governor->show_setspeed)
882 return sprintf(buf, "<unsupported>\n");
884 return policy->governor->show_setspeed(policy, buf);
888 * show_bios_limit - show the current cpufreq HW/BIOS limitation
890 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
892 unsigned int limit;
893 int ret;
894 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
895 if (!ret)
896 return sprintf(buf, "%u\n", limit);
897 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
900 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
901 cpufreq_freq_attr_ro(cpuinfo_min_freq);
902 cpufreq_freq_attr_ro(cpuinfo_max_freq);
903 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
904 cpufreq_freq_attr_ro(scaling_available_governors);
905 cpufreq_freq_attr_ro(scaling_driver);
906 cpufreq_freq_attr_ro(scaling_cur_freq);
907 cpufreq_freq_attr_ro(bios_limit);
908 cpufreq_freq_attr_ro(related_cpus);
909 cpufreq_freq_attr_ro(affected_cpus);
910 cpufreq_freq_attr_rw(scaling_min_freq);
911 cpufreq_freq_attr_rw(scaling_max_freq);
912 cpufreq_freq_attr_rw(scaling_governor);
913 cpufreq_freq_attr_rw(scaling_setspeed);
915 static struct attribute *default_attrs[] = {
916 &cpuinfo_min_freq.attr,
917 &cpuinfo_max_freq.attr,
918 &cpuinfo_transition_latency.attr,
919 &scaling_min_freq.attr,
920 &scaling_max_freq.attr,
921 &affected_cpus.attr,
922 &related_cpus.attr,
923 &scaling_governor.attr,
924 &scaling_driver.attr,
925 &scaling_available_governors.attr,
926 &scaling_setspeed.attr,
927 NULL
930 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
931 #define to_attr(a) container_of(a, struct freq_attr, attr)
933 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
935 struct cpufreq_policy *policy = to_policy(kobj);
936 struct freq_attr *fattr = to_attr(attr);
937 ssize_t ret;
939 if (!fattr->show)
940 return -EIO;
942 down_read(&policy->rwsem);
943 ret = fattr->show(policy, buf);
944 up_read(&policy->rwsem);
946 return ret;
949 static ssize_t store(struct kobject *kobj, struct attribute *attr,
950 const char *buf, size_t count)
952 struct cpufreq_policy *policy = to_policy(kobj);
953 struct freq_attr *fattr = to_attr(attr);
954 ssize_t ret = -EINVAL;
956 if (!fattr->store)
957 return -EIO;
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())
964 return -EBUSY;
966 if (cpu_online(policy->cpu)) {
967 down_write(&policy->rwsem);
968 ret = fattr->store(policy, buf, count);
969 up_write(&policy->rwsem);
972 cpus_read_unlock();
974 return ret;
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 = {
985 .show = show,
986 .store = store,
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);
999 if (unlikely(!dev))
1000 return;
1002 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1003 return;
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,
1011 struct device *dev)
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;
1020 int ret = 0;
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));
1026 if (ret)
1027 return ret;
1028 drv_attr++;
1030 if (cpufreq_driver->get) {
1031 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1032 if (ret)
1033 return ret;
1036 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1037 if (ret)
1038 return ret;
1040 if (cpufreq_driver->bios_limit) {
1041 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1042 if (ret)
1043 return ret;
1046 return 0;
1049 __weak struct cpufreq_governor *cpufreq_default_governor(void)
1051 return NULL;
1054 static int cpufreq_init_policy(struct cpufreq_policy *policy)
1056 struct cpufreq_governor *def_gov = cpufreq_default_governor();
1057 struct cpufreq_governor *gov = NULL;
1058 unsigned int pol = CPUFREQ_POLICY_UNKNOWN;
1060 if (has_target()) {
1061 /* Update policy governor to the one used before hotplug. */
1062 gov = find_governor(policy->last_governor);
1063 if (gov) {
1064 pr_debug("Restoring governor %s for cpu %d\n",
1065 policy->governor->name, policy->cpu);
1066 } else if (def_gov) {
1067 gov = def_gov;
1068 } else {
1069 return -ENODATA;
1071 } else {
1072 /* Use the default policy if there is no last_policy. */
1073 if (policy->last_policy) {
1074 pol = policy->last_policy;
1075 } else if (def_gov) {
1076 pol = cpufreq_parse_policy(def_gov->name);
1077 } else {
1078 return -ENODATA;
1082 return cpufreq_set_policy(policy, gov, pol);
1085 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1087 int ret = 0;
1089 /* Has this CPU been taken care of already? */
1090 if (cpumask_test_cpu(cpu, policy->cpus))
1091 return 0;
1093 down_write(&policy->rwsem);
1094 if (has_target())
1095 cpufreq_stop_governor(policy);
1097 cpumask_set_cpu(cpu, policy->cpus);
1099 if (has_target()) {
1100 ret = cpufreq_start_governor(policy);
1101 if (ret)
1102 pr_err("%s: Failed to start governor\n", __func__);
1104 up_write(&policy->rwsem);
1105 return ret;
1108 void refresh_frequency_limits(struct cpufreq_policy *policy)
1110 if (!policy_is_inactive(policy)) {
1111 pr_debug("updating policy for CPU %u\n", policy->cpu);
1113 cpufreq_set_policy(policy, policy->governor, policy->policy);
1116 EXPORT_SYMBOL(refresh_frequency_limits);
1118 static void handle_update(struct work_struct *work)
1120 struct cpufreq_policy *policy =
1121 container_of(work, struct cpufreq_policy, update);
1123 pr_debug("handle_update for cpu %u called\n", policy->cpu);
1124 down_write(&policy->rwsem);
1125 refresh_frequency_limits(policy);
1126 up_write(&policy->rwsem);
1129 static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq,
1130 void *data)
1132 struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min);
1134 schedule_work(&policy->update);
1135 return 0;
1138 static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq,
1139 void *data)
1141 struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max);
1143 schedule_work(&policy->update);
1144 return 0;
1147 static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
1149 struct kobject *kobj;
1150 struct completion *cmp;
1152 down_write(&policy->rwsem);
1153 cpufreq_stats_free_table(policy);
1154 kobj = &policy->kobj;
1155 cmp = &policy->kobj_unregister;
1156 up_write(&policy->rwsem);
1157 kobject_put(kobj);
1160 * We need to make sure that the underlying kobj is
1161 * actually not referenced anymore by anybody before we
1162 * proceed with unloading.
1164 pr_debug("waiting for dropping of refcount\n");
1165 wait_for_completion(cmp);
1166 pr_debug("wait complete\n");
1169 static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1171 struct cpufreq_policy *policy;
1172 struct device *dev = get_cpu_device(cpu);
1173 int ret;
1175 if (!dev)
1176 return NULL;
1178 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1179 if (!policy)
1180 return NULL;
1182 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1183 goto err_free_policy;
1185 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1186 goto err_free_cpumask;
1188 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1189 goto err_free_rcpumask;
1191 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1192 cpufreq_global_kobject, "policy%u", cpu);
1193 if (ret) {
1194 dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret);
1196 * The entire policy object will be freed below, but the extra
1197 * memory allocated for the kobject name needs to be freed by
1198 * releasing the kobject.
1200 kobject_put(&policy->kobj);
1201 goto err_free_real_cpus;
1204 freq_constraints_init(&policy->constraints);
1206 policy->nb_min.notifier_call = cpufreq_notifier_min;
1207 policy->nb_max.notifier_call = cpufreq_notifier_max;
1209 ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN,
1210 &policy->nb_min);
1211 if (ret) {
1212 dev_err(dev, "Failed to register MIN QoS notifier: %d (%*pbl)\n",
1213 ret, cpumask_pr_args(policy->cpus));
1214 goto err_kobj_remove;
1217 ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX,
1218 &policy->nb_max);
1219 if (ret) {
1220 dev_err(dev, "Failed to register MAX QoS notifier: %d (%*pbl)\n",
1221 ret, cpumask_pr_args(policy->cpus));
1222 goto err_min_qos_notifier;
1225 INIT_LIST_HEAD(&policy->policy_list);
1226 init_rwsem(&policy->rwsem);
1227 spin_lock_init(&policy->transition_lock);
1228 init_waitqueue_head(&policy->transition_wait);
1229 init_completion(&policy->kobj_unregister);
1230 INIT_WORK(&policy->update, handle_update);
1232 policy->cpu = cpu;
1233 return policy;
1235 err_min_qos_notifier:
1236 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
1237 &policy->nb_min);
1238 err_kobj_remove:
1239 cpufreq_policy_put_kobj(policy);
1240 err_free_real_cpus:
1241 free_cpumask_var(policy->real_cpus);
1242 err_free_rcpumask:
1243 free_cpumask_var(policy->related_cpus);
1244 err_free_cpumask:
1245 free_cpumask_var(policy->cpus);
1246 err_free_policy:
1247 kfree(policy);
1249 return NULL;
1252 static void cpufreq_policy_free(struct cpufreq_policy *policy)
1254 unsigned long flags;
1255 int cpu;
1257 /* Remove policy from list */
1258 write_lock_irqsave(&cpufreq_driver_lock, flags);
1259 list_del(&policy->policy_list);
1261 for_each_cpu(cpu, policy->related_cpus)
1262 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1263 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1265 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX,
1266 &policy->nb_max);
1267 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
1268 &policy->nb_min);
1270 /* Cancel any pending policy->update work before freeing the policy. */
1271 cancel_work_sync(&policy->update);
1273 if (policy->max_freq_req) {
1275 * CPUFREQ_CREATE_POLICY notification is sent only after
1276 * successfully adding max_freq_req request.
1278 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1279 CPUFREQ_REMOVE_POLICY, policy);
1280 freq_qos_remove_request(policy->max_freq_req);
1283 freq_qos_remove_request(policy->min_freq_req);
1284 kfree(policy->min_freq_req);
1286 cpufreq_policy_put_kobj(policy);
1287 free_cpumask_var(policy->real_cpus);
1288 free_cpumask_var(policy->related_cpus);
1289 free_cpumask_var(policy->cpus);
1290 kfree(policy);
1293 static int cpufreq_online(unsigned int cpu)
1295 struct cpufreq_policy *policy;
1296 bool new_policy;
1297 unsigned long flags;
1298 unsigned int j;
1299 int ret;
1301 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
1303 /* Check if this CPU already has a policy to manage it */
1304 policy = per_cpu(cpufreq_cpu_data, cpu);
1305 if (policy) {
1306 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
1307 if (!policy_is_inactive(policy))
1308 return cpufreq_add_policy_cpu(policy, cpu);
1310 /* This is the only online CPU for the policy. Start over. */
1311 new_policy = false;
1312 down_write(&policy->rwsem);
1313 policy->cpu = cpu;
1314 policy->governor = NULL;
1315 up_write(&policy->rwsem);
1316 } else {
1317 new_policy = true;
1318 policy = cpufreq_policy_alloc(cpu);
1319 if (!policy)
1320 return -ENOMEM;
1323 if (!new_policy && cpufreq_driver->online) {
1324 ret = cpufreq_driver->online(policy);
1325 if (ret) {
1326 pr_debug("%s: %d: initialization failed\n", __func__,
1327 __LINE__);
1328 goto out_exit_policy;
1331 /* Recover policy->cpus using related_cpus */
1332 cpumask_copy(policy->cpus, policy->related_cpus);
1333 } else {
1334 cpumask_copy(policy->cpus, cpumask_of(cpu));
1337 * Call driver. From then on the cpufreq must be able
1338 * to accept all calls to ->verify and ->setpolicy for this CPU.
1340 ret = cpufreq_driver->init(policy);
1341 if (ret) {
1342 pr_debug("%s: %d: initialization failed\n", __func__,
1343 __LINE__);
1344 goto out_free_policy;
1347 ret = cpufreq_table_validate_and_sort(policy);
1348 if (ret)
1349 goto out_exit_policy;
1351 /* related_cpus should at least include policy->cpus. */
1352 cpumask_copy(policy->related_cpus, policy->cpus);
1355 down_write(&policy->rwsem);
1357 * affected cpus must always be the one, which are online. We aren't
1358 * managing offline cpus here.
1360 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1362 if (new_policy) {
1363 for_each_cpu(j, policy->related_cpus) {
1364 per_cpu(cpufreq_cpu_data, j) = policy;
1365 add_cpu_dev_symlink(policy, j);
1368 policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req),
1369 GFP_KERNEL);
1370 if (!policy->min_freq_req)
1371 goto out_destroy_policy;
1373 ret = freq_qos_add_request(&policy->constraints,
1374 policy->min_freq_req, FREQ_QOS_MIN,
1375 policy->min);
1376 if (ret < 0) {
1378 * So we don't call freq_qos_remove_request() for an
1379 * uninitialized request.
1381 kfree(policy->min_freq_req);
1382 policy->min_freq_req = NULL;
1383 goto out_destroy_policy;
1387 * This must be initialized right here to avoid calling
1388 * freq_qos_remove_request() on uninitialized request in case
1389 * of errors.
1391 policy->max_freq_req = policy->min_freq_req + 1;
1393 ret = freq_qos_add_request(&policy->constraints,
1394 policy->max_freq_req, FREQ_QOS_MAX,
1395 policy->max);
1396 if (ret < 0) {
1397 policy->max_freq_req = NULL;
1398 goto out_destroy_policy;
1401 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1402 CPUFREQ_CREATE_POLICY, policy);
1405 if (cpufreq_driver->get && has_target()) {
1406 policy->cur = cpufreq_driver->get(policy->cpu);
1407 if (!policy->cur) {
1408 pr_err("%s: ->get() failed\n", __func__);
1409 goto out_destroy_policy;
1414 * Sometimes boot loaders set CPU frequency to a value outside of
1415 * frequency table present with cpufreq core. In such cases CPU might be
1416 * unstable if it has to run on that frequency for long duration of time
1417 * and so its better to set it to a frequency which is specified in
1418 * freq-table. This also makes cpufreq stats inconsistent as
1419 * cpufreq-stats would fail to register because current frequency of CPU
1420 * isn't found in freq-table.
1422 * Because we don't want this change to effect boot process badly, we go
1423 * for the next freq which is >= policy->cur ('cur' must be set by now,
1424 * otherwise we will end up setting freq to lowest of the table as 'cur'
1425 * is initialized to zero).
1427 * We are passing target-freq as "policy->cur - 1" otherwise
1428 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1429 * equal to target-freq.
1431 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1432 && has_target()) {
1433 /* Are we running at unknown frequency ? */
1434 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1435 if (ret == -EINVAL) {
1436 /* Warn user and fix it */
1437 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1438 __func__, policy->cpu, policy->cur);
1439 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1440 CPUFREQ_RELATION_L);
1443 * Reaching here after boot in a few seconds may not
1444 * mean that system will remain stable at "unknown"
1445 * frequency for longer duration. Hence, a BUG_ON().
1447 BUG_ON(ret);
1448 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1449 __func__, policy->cpu, policy->cur);
1453 if (new_policy) {
1454 ret = cpufreq_add_dev_interface(policy);
1455 if (ret)
1456 goto out_destroy_policy;
1458 cpufreq_stats_create_table(policy);
1460 write_lock_irqsave(&cpufreq_driver_lock, flags);
1461 list_add(&policy->policy_list, &cpufreq_policy_list);
1462 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1465 ret = cpufreq_init_policy(policy);
1466 if (ret) {
1467 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1468 __func__, cpu, ret);
1469 goto out_destroy_policy;
1472 up_write(&policy->rwsem);
1474 kobject_uevent(&policy->kobj, KOBJ_ADD);
1476 /* Callback for handling stuff after policy is ready */
1477 if (cpufreq_driver->ready)
1478 cpufreq_driver->ready(policy);
1480 if (cpufreq_thermal_control_enabled(cpufreq_driver))
1481 policy->cdev = of_cpufreq_cooling_register(policy);
1483 pr_debug("initialization complete\n");
1485 return 0;
1487 out_destroy_policy:
1488 for_each_cpu(j, policy->real_cpus)
1489 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1491 up_write(&policy->rwsem);
1493 out_exit_policy:
1494 if (cpufreq_driver->exit)
1495 cpufreq_driver->exit(policy);
1497 out_free_policy:
1498 cpufreq_policy_free(policy);
1499 return ret;
1503 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1504 * @dev: CPU device.
1505 * @sif: Subsystem interface structure pointer (not used)
1507 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1509 struct cpufreq_policy *policy;
1510 unsigned cpu = dev->id;
1511 int ret;
1513 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1515 if (cpu_online(cpu)) {
1516 ret = cpufreq_online(cpu);
1517 if (ret)
1518 return ret;
1521 /* Create sysfs link on CPU registration */
1522 policy = per_cpu(cpufreq_cpu_data, cpu);
1523 if (policy)
1524 add_cpu_dev_symlink(policy, cpu);
1526 return 0;
1529 static int cpufreq_offline(unsigned int cpu)
1531 struct cpufreq_policy *policy;
1532 int ret;
1534 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1536 policy = cpufreq_cpu_get_raw(cpu);
1537 if (!policy) {
1538 pr_debug("%s: No cpu_data found\n", __func__);
1539 return 0;
1542 down_write(&policy->rwsem);
1543 if (has_target())
1544 cpufreq_stop_governor(policy);
1546 cpumask_clear_cpu(cpu, policy->cpus);
1548 if (policy_is_inactive(policy)) {
1549 if (has_target())
1550 strncpy(policy->last_governor, policy->governor->name,
1551 CPUFREQ_NAME_LEN);
1552 else
1553 policy->last_policy = policy->policy;
1554 } else if (cpu == policy->cpu) {
1555 /* Nominate new CPU */
1556 policy->cpu = cpumask_any(policy->cpus);
1559 /* Start governor again for active policy */
1560 if (!policy_is_inactive(policy)) {
1561 if (has_target()) {
1562 ret = cpufreq_start_governor(policy);
1563 if (ret)
1564 pr_err("%s: Failed to start governor\n", __func__);
1567 goto unlock;
1570 if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
1571 cpufreq_cooling_unregister(policy->cdev);
1572 policy->cdev = NULL;
1575 if (cpufreq_driver->stop_cpu)
1576 cpufreq_driver->stop_cpu(policy);
1578 if (has_target())
1579 cpufreq_exit_governor(policy);
1582 * Perform the ->offline() during light-weight tear-down, as
1583 * that allows fast recovery when the CPU comes back.
1585 if (cpufreq_driver->offline) {
1586 cpufreq_driver->offline(policy);
1587 } else if (cpufreq_driver->exit) {
1588 cpufreq_driver->exit(policy);
1589 policy->freq_table = NULL;
1592 unlock:
1593 up_write(&policy->rwsem);
1594 return 0;
1598 * cpufreq_remove_dev - remove a CPU device
1600 * Removes the cpufreq interface for a CPU device.
1602 static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1604 unsigned int cpu = dev->id;
1605 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1607 if (!policy)
1608 return;
1610 if (cpu_online(cpu))
1611 cpufreq_offline(cpu);
1613 cpumask_clear_cpu(cpu, policy->real_cpus);
1614 remove_cpu_dev_symlink(policy, dev);
1616 if (cpumask_empty(policy->real_cpus)) {
1617 /* We did light-weight exit earlier, do full tear down now */
1618 if (cpufreq_driver->offline)
1619 cpufreq_driver->exit(policy);
1621 cpufreq_policy_free(policy);
1626 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1627 * in deep trouble.
1628 * @policy: policy managing CPUs
1629 * @new_freq: CPU frequency the CPU actually runs at
1631 * We adjust to current frequency first, and need to clean up later.
1632 * So either call to cpufreq_update_policy() or schedule handle_update()).
1634 static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1635 unsigned int new_freq)
1637 struct cpufreq_freqs freqs;
1639 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1640 policy->cur, new_freq);
1642 freqs.old = policy->cur;
1643 freqs.new = new_freq;
1645 cpufreq_freq_transition_begin(policy, &freqs);
1646 cpufreq_freq_transition_end(policy, &freqs, 0);
1649 static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update)
1651 unsigned int new_freq;
1653 new_freq = cpufreq_driver->get(policy->cpu);
1654 if (!new_freq)
1655 return 0;
1658 * If fast frequency switching is used with the given policy, the check
1659 * against policy->cur is pointless, so skip it in that case.
1661 if (policy->fast_switch_enabled || !has_target())
1662 return new_freq;
1664 if (policy->cur != new_freq) {
1665 cpufreq_out_of_sync(policy, new_freq);
1666 if (update)
1667 schedule_work(&policy->update);
1670 return new_freq;
1674 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1675 * @cpu: CPU number
1677 * This is the last known freq, without actually getting it from the driver.
1678 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1680 unsigned int cpufreq_quick_get(unsigned int cpu)
1682 struct cpufreq_policy *policy;
1683 unsigned int ret_freq = 0;
1684 unsigned long flags;
1686 read_lock_irqsave(&cpufreq_driver_lock, flags);
1688 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1689 ret_freq = cpufreq_driver->get(cpu);
1690 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1691 return ret_freq;
1694 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1696 policy = cpufreq_cpu_get(cpu);
1697 if (policy) {
1698 ret_freq = policy->cur;
1699 cpufreq_cpu_put(policy);
1702 return ret_freq;
1704 EXPORT_SYMBOL(cpufreq_quick_get);
1707 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1708 * @cpu: CPU number
1710 * Just return the max possible frequency for a given CPU.
1712 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1714 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1715 unsigned int ret_freq = 0;
1717 if (policy) {
1718 ret_freq = policy->max;
1719 cpufreq_cpu_put(policy);
1722 return ret_freq;
1724 EXPORT_SYMBOL(cpufreq_quick_get_max);
1726 static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
1728 if (unlikely(policy_is_inactive(policy)))
1729 return 0;
1731 return cpufreq_verify_current_freq(policy, true);
1735 * cpufreq_get - get the current CPU frequency (in kHz)
1736 * @cpu: CPU number
1738 * Get the CPU current (static) CPU frequency
1740 unsigned int cpufreq_get(unsigned int cpu)
1742 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1743 unsigned int ret_freq = 0;
1745 if (policy) {
1746 down_read(&policy->rwsem);
1747 if (cpufreq_driver->get)
1748 ret_freq = __cpufreq_get(policy);
1749 up_read(&policy->rwsem);
1751 cpufreq_cpu_put(policy);
1754 return ret_freq;
1756 EXPORT_SYMBOL(cpufreq_get);
1758 static struct subsys_interface cpufreq_interface = {
1759 .name = "cpufreq",
1760 .subsys = &cpu_subsys,
1761 .add_dev = cpufreq_add_dev,
1762 .remove_dev = cpufreq_remove_dev,
1766 * In case platform wants some specific frequency to be configured
1767 * during suspend..
1769 int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1771 int ret;
1773 if (!policy->suspend_freq) {
1774 pr_debug("%s: suspend_freq not defined\n", __func__);
1775 return 0;
1778 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1779 policy->suspend_freq);
1781 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1782 CPUFREQ_RELATION_H);
1783 if (ret)
1784 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1785 __func__, policy->suspend_freq, ret);
1787 return ret;
1789 EXPORT_SYMBOL(cpufreq_generic_suspend);
1792 * cpufreq_suspend() - Suspend CPUFreq governors
1794 * Called during system wide Suspend/Hibernate cycles for suspending governors
1795 * as some platforms can't change frequency after this point in suspend cycle.
1796 * Because some of the devices (like: i2c, regulators, etc) they use for
1797 * changing frequency are suspended quickly after this point.
1799 void cpufreq_suspend(void)
1801 struct cpufreq_policy *policy;
1803 if (!cpufreq_driver)
1804 return;
1806 if (!has_target() && !cpufreq_driver->suspend)
1807 goto suspend;
1809 pr_debug("%s: Suspending Governors\n", __func__);
1811 for_each_active_policy(policy) {
1812 if (has_target()) {
1813 down_write(&policy->rwsem);
1814 cpufreq_stop_governor(policy);
1815 up_write(&policy->rwsem);
1818 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
1819 pr_err("%s: Failed to suspend driver: %s\n", __func__,
1820 cpufreq_driver->name);
1823 suspend:
1824 cpufreq_suspended = true;
1828 * cpufreq_resume() - Resume CPUFreq governors
1830 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1831 * are suspended with cpufreq_suspend().
1833 void cpufreq_resume(void)
1835 struct cpufreq_policy *policy;
1836 int ret;
1838 if (!cpufreq_driver)
1839 return;
1841 if (unlikely(!cpufreq_suspended))
1842 return;
1844 cpufreq_suspended = false;
1846 if (!has_target() && !cpufreq_driver->resume)
1847 return;
1849 pr_debug("%s: Resuming Governors\n", __func__);
1851 for_each_active_policy(policy) {
1852 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
1853 pr_err("%s: Failed to resume driver: %p\n", __func__,
1854 policy);
1855 } else if (has_target()) {
1856 down_write(&policy->rwsem);
1857 ret = cpufreq_start_governor(policy);
1858 up_write(&policy->rwsem);
1860 if (ret)
1861 pr_err("%s: Failed to start governor for policy: %p\n",
1862 __func__, policy);
1868 * cpufreq_get_current_driver - return current driver's name
1870 * Return the name string of the currently loaded cpufreq driver
1871 * or NULL, if none.
1873 const char *cpufreq_get_current_driver(void)
1875 if (cpufreq_driver)
1876 return cpufreq_driver->name;
1878 return NULL;
1880 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1883 * cpufreq_get_driver_data - return current driver data
1885 * Return the private data of the currently loaded cpufreq
1886 * driver, or NULL if no cpufreq driver is loaded.
1888 void *cpufreq_get_driver_data(void)
1890 if (cpufreq_driver)
1891 return cpufreq_driver->driver_data;
1893 return NULL;
1895 EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1897 /*********************************************************************
1898 * NOTIFIER LISTS INTERFACE *
1899 *********************************************************************/
1902 * cpufreq_register_notifier - register a driver with cpufreq
1903 * @nb: notifier function to register
1904 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1906 * Add a driver to one of two lists: either a list of drivers that
1907 * are notified about clock rate changes (once before and once after
1908 * the transition), or a list of drivers that are notified about
1909 * changes in cpufreq policy.
1911 * This function may sleep, and has the same return conditions as
1912 * blocking_notifier_chain_register.
1914 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1916 int ret;
1918 if (cpufreq_disabled())
1919 return -EINVAL;
1921 switch (list) {
1922 case CPUFREQ_TRANSITION_NOTIFIER:
1923 mutex_lock(&cpufreq_fast_switch_lock);
1925 if (cpufreq_fast_switch_count > 0) {
1926 mutex_unlock(&cpufreq_fast_switch_lock);
1927 return -EBUSY;
1929 ret = srcu_notifier_chain_register(
1930 &cpufreq_transition_notifier_list, nb);
1931 if (!ret)
1932 cpufreq_fast_switch_count--;
1934 mutex_unlock(&cpufreq_fast_switch_lock);
1935 break;
1936 case CPUFREQ_POLICY_NOTIFIER:
1937 ret = blocking_notifier_chain_register(
1938 &cpufreq_policy_notifier_list, nb);
1939 break;
1940 default:
1941 ret = -EINVAL;
1944 return ret;
1946 EXPORT_SYMBOL(cpufreq_register_notifier);
1949 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1950 * @nb: notifier block to be unregistered
1951 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1953 * Remove a driver from the CPU frequency notifier list.
1955 * This function may sleep, and has the same return conditions as
1956 * blocking_notifier_chain_unregister.
1958 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1960 int ret;
1962 if (cpufreq_disabled())
1963 return -EINVAL;
1965 switch (list) {
1966 case CPUFREQ_TRANSITION_NOTIFIER:
1967 mutex_lock(&cpufreq_fast_switch_lock);
1969 ret = srcu_notifier_chain_unregister(
1970 &cpufreq_transition_notifier_list, nb);
1971 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1972 cpufreq_fast_switch_count++;
1974 mutex_unlock(&cpufreq_fast_switch_lock);
1975 break;
1976 case CPUFREQ_POLICY_NOTIFIER:
1977 ret = blocking_notifier_chain_unregister(
1978 &cpufreq_policy_notifier_list, nb);
1979 break;
1980 default:
1981 ret = -EINVAL;
1984 return ret;
1986 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1989 /*********************************************************************
1990 * GOVERNORS *
1991 *********************************************************************/
1994 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1995 * @policy: cpufreq policy to switch the frequency for.
1996 * @target_freq: New frequency to set (may be approximate).
1998 * Carry out a fast frequency switch without sleeping.
2000 * The driver's ->fast_switch() callback invoked by this function must be
2001 * suitable for being called from within RCU-sched read-side critical sections
2002 * and it is expected to select the minimum available frequency greater than or
2003 * equal to @target_freq (CPUFREQ_RELATION_L).
2005 * This function must not be called if policy->fast_switch_enabled is unset.
2007 * Governors calling this function must guarantee that it will never be invoked
2008 * twice in parallel for the same policy and that it will never be called in
2009 * parallel with either ->target() or ->target_index() for the same policy.
2011 * Returns the actual frequency set for the CPU.
2013 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
2014 * error condition, the hardware configuration must be preserved.
2016 unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
2017 unsigned int target_freq)
2019 target_freq = clamp_val(target_freq, policy->min, policy->max);
2021 return cpufreq_driver->fast_switch(policy, target_freq);
2023 EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
2025 /* Must set freqs->new to intermediate frequency */
2026 static int __target_intermediate(struct cpufreq_policy *policy,
2027 struct cpufreq_freqs *freqs, int index)
2029 int ret;
2031 freqs->new = cpufreq_driver->get_intermediate(policy, index);
2033 /* We don't need to switch to intermediate freq */
2034 if (!freqs->new)
2035 return 0;
2037 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
2038 __func__, policy->cpu, freqs->old, freqs->new);
2040 cpufreq_freq_transition_begin(policy, freqs);
2041 ret = cpufreq_driver->target_intermediate(policy, index);
2042 cpufreq_freq_transition_end(policy, freqs, ret);
2044 if (ret)
2045 pr_err("%s: Failed to change to intermediate frequency: %d\n",
2046 __func__, ret);
2048 return ret;
2051 static int __target_index(struct cpufreq_policy *policy, int index)
2053 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
2054 unsigned int intermediate_freq = 0;
2055 unsigned int newfreq = policy->freq_table[index].frequency;
2056 int retval = -EINVAL;
2057 bool notify;
2059 if (newfreq == policy->cur)
2060 return 0;
2062 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
2063 if (notify) {
2064 /* Handle switching to intermediate frequency */
2065 if (cpufreq_driver->get_intermediate) {
2066 retval = __target_intermediate(policy, &freqs, index);
2067 if (retval)
2068 return retval;
2070 intermediate_freq = freqs.new;
2071 /* Set old freq to intermediate */
2072 if (intermediate_freq)
2073 freqs.old = freqs.new;
2076 freqs.new = newfreq;
2077 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
2078 __func__, policy->cpu, freqs.old, freqs.new);
2080 cpufreq_freq_transition_begin(policy, &freqs);
2083 retval = cpufreq_driver->target_index(policy, index);
2084 if (retval)
2085 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
2086 retval);
2088 if (notify) {
2089 cpufreq_freq_transition_end(policy, &freqs, retval);
2092 * Failed after setting to intermediate freq? Driver should have
2093 * reverted back to initial frequency and so should we. Check
2094 * here for intermediate_freq instead of get_intermediate, in
2095 * case we haven't switched to intermediate freq at all.
2097 if (unlikely(retval && intermediate_freq)) {
2098 freqs.old = intermediate_freq;
2099 freqs.new = policy->restore_freq;
2100 cpufreq_freq_transition_begin(policy, &freqs);
2101 cpufreq_freq_transition_end(policy, &freqs, 0);
2105 return retval;
2108 int __cpufreq_driver_target(struct cpufreq_policy *policy,
2109 unsigned int target_freq,
2110 unsigned int relation)
2112 unsigned int old_target_freq = target_freq;
2113 int index;
2115 if (cpufreq_disabled())
2116 return -ENODEV;
2118 /* Make sure that target_freq is within supported range */
2119 target_freq = clamp_val(target_freq, policy->min, policy->max);
2121 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
2122 policy->cpu, target_freq, relation, old_target_freq);
2125 * This might look like a redundant call as we are checking it again
2126 * after finding index. But it is left intentionally for cases where
2127 * exactly same freq is called again and so we can save on few function
2128 * calls.
2130 if (target_freq == policy->cur)
2131 return 0;
2133 /* Save last value to restore later on errors */
2134 policy->restore_freq = policy->cur;
2136 if (cpufreq_driver->target)
2137 return cpufreq_driver->target(policy, target_freq, relation);
2139 if (!cpufreq_driver->target_index)
2140 return -EINVAL;
2142 index = cpufreq_frequency_table_target(policy, target_freq, relation);
2144 return __target_index(policy, index);
2146 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2148 int cpufreq_driver_target(struct cpufreq_policy *policy,
2149 unsigned int target_freq,
2150 unsigned int relation)
2152 int ret;
2154 down_write(&policy->rwsem);
2156 ret = __cpufreq_driver_target(policy, target_freq, relation);
2158 up_write(&policy->rwsem);
2160 return ret;
2162 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2164 __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2166 return NULL;
2169 static int cpufreq_init_governor(struct cpufreq_policy *policy)
2171 int ret;
2173 /* Don't start any governor operations if we are entering suspend */
2174 if (cpufreq_suspended)
2175 return 0;
2177 * Governor might not be initiated here if ACPI _PPC changed
2178 * notification happened, so check it.
2180 if (!policy->governor)
2181 return -EINVAL;
2183 /* Platform doesn't want dynamic frequency switching ? */
2184 if (policy->governor->dynamic_switching &&
2185 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
2186 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2188 if (gov) {
2189 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
2190 policy->governor->name, gov->name);
2191 policy->governor = gov;
2192 } else {
2193 return -EINVAL;
2197 if (!try_module_get(policy->governor->owner))
2198 return -EINVAL;
2200 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2202 if (policy->governor->init) {
2203 ret = policy->governor->init(policy);
2204 if (ret) {
2205 module_put(policy->governor->owner);
2206 return ret;
2210 return 0;
2213 static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2215 if (cpufreq_suspended || !policy->governor)
2216 return;
2218 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2220 if (policy->governor->exit)
2221 policy->governor->exit(policy);
2223 module_put(policy->governor->owner);
2226 static int cpufreq_start_governor(struct cpufreq_policy *policy)
2228 int ret;
2230 if (cpufreq_suspended)
2231 return 0;
2233 if (!policy->governor)
2234 return -EINVAL;
2236 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2238 if (cpufreq_driver->get)
2239 cpufreq_verify_current_freq(policy, false);
2241 if (policy->governor->start) {
2242 ret = policy->governor->start(policy);
2243 if (ret)
2244 return ret;
2247 if (policy->governor->limits)
2248 policy->governor->limits(policy);
2250 return 0;
2253 static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2255 if (cpufreq_suspended || !policy->governor)
2256 return;
2258 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2260 if (policy->governor->stop)
2261 policy->governor->stop(policy);
2264 static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2266 if (cpufreq_suspended || !policy->governor)
2267 return;
2269 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2271 if (policy->governor->limits)
2272 policy->governor->limits(policy);
2275 int cpufreq_register_governor(struct cpufreq_governor *governor)
2277 int err;
2279 if (!governor)
2280 return -EINVAL;
2282 if (cpufreq_disabled())
2283 return -ENODEV;
2285 mutex_lock(&cpufreq_governor_mutex);
2287 err = -EBUSY;
2288 if (!find_governor(governor->name)) {
2289 err = 0;
2290 list_add(&governor->governor_list, &cpufreq_governor_list);
2293 mutex_unlock(&cpufreq_governor_mutex);
2294 return err;
2296 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2298 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2300 struct cpufreq_policy *policy;
2301 unsigned long flags;
2303 if (!governor)
2304 return;
2306 if (cpufreq_disabled())
2307 return;
2309 /* clear last_governor for all inactive policies */
2310 read_lock_irqsave(&cpufreq_driver_lock, flags);
2311 for_each_inactive_policy(policy) {
2312 if (!strcmp(policy->last_governor, governor->name)) {
2313 policy->governor = NULL;
2314 strcpy(policy->last_governor, "\0");
2317 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2319 mutex_lock(&cpufreq_governor_mutex);
2320 list_del(&governor->governor_list);
2321 mutex_unlock(&cpufreq_governor_mutex);
2323 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2326 /*********************************************************************
2327 * POLICY INTERFACE *
2328 *********************************************************************/
2331 * cpufreq_get_policy - get the current cpufreq_policy
2332 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2333 * is written
2335 * Reads the current cpufreq policy.
2337 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2339 struct cpufreq_policy *cpu_policy;
2340 if (!policy)
2341 return -EINVAL;
2343 cpu_policy = cpufreq_cpu_get(cpu);
2344 if (!cpu_policy)
2345 return -EINVAL;
2347 memcpy(policy, cpu_policy, sizeof(*policy));
2349 cpufreq_cpu_put(cpu_policy);
2350 return 0;
2352 EXPORT_SYMBOL(cpufreq_get_policy);
2355 * cpufreq_set_policy - Modify cpufreq policy parameters.
2356 * @policy: Policy object to modify.
2357 * @new_gov: Policy governor pointer.
2358 * @new_pol: Policy value (for drivers with built-in governors).
2360 * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency
2361 * limits to be set for the policy, update @policy with the verified limits
2362 * values and either invoke the driver's ->setpolicy() callback (if present) or
2363 * carry out a governor update for @policy. That is, run the current governor's
2364 * ->limits() callback (if @new_gov points to the same object as the one in
2365 * @policy) or replace the governor for @policy with @new_gov.
2367 * The cpuinfo part of @policy is not updated by this function.
2369 static int cpufreq_set_policy(struct cpufreq_policy *policy,
2370 struct cpufreq_governor *new_gov,
2371 unsigned int new_pol)
2373 struct cpufreq_policy_data new_data;
2374 struct cpufreq_governor *old_gov;
2375 int ret;
2377 memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2378 new_data.freq_table = policy->freq_table;
2379 new_data.cpu = policy->cpu;
2381 * PM QoS framework collects all the requests from users and provide us
2382 * the final aggregated value here.
2384 new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
2385 new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
2387 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2388 new_data.cpu, new_data.min, new_data.max);
2391 * Verify that the CPU speed can be set within these limits and make sure
2392 * that min <= max.
2394 ret = cpufreq_driver->verify(&new_data);
2395 if (ret)
2396 return ret;
2398 policy->min = new_data.min;
2399 policy->max = new_data.max;
2400 trace_cpu_frequency_limits(policy);
2402 policy->cached_target_freq = UINT_MAX;
2404 pr_debug("new min and max freqs are %u - %u kHz\n",
2405 policy->min, policy->max);
2407 if (cpufreq_driver->setpolicy) {
2408 policy->policy = new_pol;
2409 pr_debug("setting range\n");
2410 return cpufreq_driver->setpolicy(policy);
2413 if (new_gov == policy->governor) {
2414 pr_debug("governor limits update\n");
2415 cpufreq_governor_limits(policy);
2416 return 0;
2419 pr_debug("governor switch\n");
2421 /* save old, working values */
2422 old_gov = policy->governor;
2423 /* end old governor */
2424 if (old_gov) {
2425 cpufreq_stop_governor(policy);
2426 cpufreq_exit_governor(policy);
2429 /* start new governor */
2430 policy->governor = new_gov;
2431 ret = cpufreq_init_governor(policy);
2432 if (!ret) {
2433 ret = cpufreq_start_governor(policy);
2434 if (!ret) {
2435 pr_debug("governor change\n");
2436 sched_cpufreq_governor_change(policy, old_gov);
2437 return 0;
2439 cpufreq_exit_governor(policy);
2442 /* new governor failed, so re-start old one */
2443 pr_debug("starting governor %s failed\n", policy->governor->name);
2444 if (old_gov) {
2445 policy->governor = old_gov;
2446 if (cpufreq_init_governor(policy))
2447 policy->governor = NULL;
2448 else
2449 cpufreq_start_governor(policy);
2452 return ret;
2456 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2457 * @cpu: CPU to re-evaluate the policy for.
2459 * Update the current frequency for the cpufreq policy of @cpu and use
2460 * cpufreq_set_policy() to re-apply the min and max limits, which triggers the
2461 * evaluation of policy notifiers and the cpufreq driver's ->verify() callback
2462 * for the policy in question, among other things.
2464 void cpufreq_update_policy(unsigned int cpu)
2466 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
2468 if (!policy)
2469 return;
2472 * BIOS might change freq behind our back
2473 * -> ask driver for current freq and notify governors about a change
2475 if (cpufreq_driver->get && has_target() &&
2476 (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
2477 goto unlock;
2479 refresh_frequency_limits(policy);
2481 unlock:
2482 cpufreq_cpu_release(policy);
2484 EXPORT_SYMBOL(cpufreq_update_policy);
2487 * cpufreq_update_limits - Update policy limits for a given CPU.
2488 * @cpu: CPU to update the policy limits for.
2490 * Invoke the driver's ->update_limits callback if present or call
2491 * cpufreq_update_policy() for @cpu.
2493 void cpufreq_update_limits(unsigned int cpu)
2495 if (cpufreq_driver->update_limits)
2496 cpufreq_driver->update_limits(cpu);
2497 else
2498 cpufreq_update_policy(cpu);
2500 EXPORT_SYMBOL_GPL(cpufreq_update_limits);
2502 /*********************************************************************
2503 * BOOST *
2504 *********************************************************************/
2505 static int cpufreq_boost_set_sw(int state)
2507 struct cpufreq_policy *policy;
2508 int ret = -EINVAL;
2510 for_each_active_policy(policy) {
2511 if (!policy->freq_table)
2512 continue;
2514 ret = cpufreq_frequency_table_cpuinfo(policy,
2515 policy->freq_table);
2516 if (ret) {
2517 pr_err("%s: Policy frequency update failed\n",
2518 __func__);
2519 break;
2522 ret = freq_qos_update_request(policy->max_freq_req, policy->max);
2523 if (ret < 0)
2524 break;
2527 return ret;
2530 int cpufreq_boost_trigger_state(int state)
2532 unsigned long flags;
2533 int ret = 0;
2535 if (cpufreq_driver->boost_enabled == state)
2536 return 0;
2538 write_lock_irqsave(&cpufreq_driver_lock, flags);
2539 cpufreq_driver->boost_enabled = state;
2540 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2542 ret = cpufreq_driver->set_boost(state);
2543 if (ret) {
2544 write_lock_irqsave(&cpufreq_driver_lock, flags);
2545 cpufreq_driver->boost_enabled = !state;
2546 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2548 pr_err("%s: Cannot %s BOOST\n",
2549 __func__, state ? "enable" : "disable");
2552 return ret;
2555 static bool cpufreq_boost_supported(void)
2557 return cpufreq_driver->set_boost;
2560 static int create_boost_sysfs_file(void)
2562 int ret;
2564 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
2565 if (ret)
2566 pr_err("%s: cannot register global BOOST sysfs file\n",
2567 __func__);
2569 return ret;
2572 static void remove_boost_sysfs_file(void)
2574 if (cpufreq_boost_supported())
2575 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
2578 int cpufreq_enable_boost_support(void)
2580 if (!cpufreq_driver)
2581 return -EINVAL;
2583 if (cpufreq_boost_supported())
2584 return 0;
2586 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2588 /* This will get removed on driver unregister */
2589 return create_boost_sysfs_file();
2591 EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2593 int cpufreq_boost_enabled(void)
2595 return cpufreq_driver->boost_enabled;
2597 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2599 /*********************************************************************
2600 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2601 *********************************************************************/
2602 static enum cpuhp_state hp_online;
2604 static int cpuhp_cpufreq_online(unsigned int cpu)
2606 cpufreq_online(cpu);
2608 return 0;
2611 static int cpuhp_cpufreq_offline(unsigned int cpu)
2613 cpufreq_offline(cpu);
2615 return 0;
2619 * cpufreq_register_driver - register a CPU Frequency driver
2620 * @driver_data: A struct cpufreq_driver containing the values#
2621 * submitted by the CPU Frequency driver.
2623 * Registers a CPU Frequency driver to this core code. This code
2624 * returns zero on success, -EEXIST when another driver got here first
2625 * (and isn't unregistered in the meantime).
2628 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2630 unsigned long flags;
2631 int ret;
2633 if (cpufreq_disabled())
2634 return -ENODEV;
2637 * The cpufreq core depends heavily on the availability of device
2638 * structure, make sure they are available before proceeding further.
2640 if (!get_cpu_device(0))
2641 return -EPROBE_DEFER;
2643 if (!driver_data || !driver_data->verify || !driver_data->init ||
2644 !(driver_data->setpolicy || driver_data->target_index ||
2645 driver_data->target) ||
2646 (driver_data->setpolicy && (driver_data->target_index ||
2647 driver_data->target)) ||
2648 (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
2649 (!driver_data->online != !driver_data->offline))
2650 return -EINVAL;
2652 pr_debug("trying to register driver %s\n", driver_data->name);
2654 /* Protect against concurrent CPU online/offline. */
2655 cpus_read_lock();
2657 write_lock_irqsave(&cpufreq_driver_lock, flags);
2658 if (cpufreq_driver) {
2659 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2660 ret = -EEXIST;
2661 goto out;
2663 cpufreq_driver = driver_data;
2664 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2666 if (driver_data->setpolicy)
2667 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2669 if (cpufreq_boost_supported()) {
2670 ret = create_boost_sysfs_file();
2671 if (ret)
2672 goto err_null_driver;
2675 ret = subsys_interface_register(&cpufreq_interface);
2676 if (ret)
2677 goto err_boost_unreg;
2679 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2680 list_empty(&cpufreq_policy_list)) {
2681 /* if all ->init() calls failed, unregister */
2682 ret = -ENODEV;
2683 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2684 driver_data->name);
2685 goto err_if_unreg;
2688 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2689 "cpufreq:online",
2690 cpuhp_cpufreq_online,
2691 cpuhp_cpufreq_offline);
2692 if (ret < 0)
2693 goto err_if_unreg;
2694 hp_online = ret;
2695 ret = 0;
2697 pr_debug("driver %s up and running\n", driver_data->name);
2698 goto out;
2700 err_if_unreg:
2701 subsys_interface_unregister(&cpufreq_interface);
2702 err_boost_unreg:
2703 remove_boost_sysfs_file();
2704 err_null_driver:
2705 write_lock_irqsave(&cpufreq_driver_lock, flags);
2706 cpufreq_driver = NULL;
2707 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2708 out:
2709 cpus_read_unlock();
2710 return ret;
2712 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2715 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2717 * Unregister the current CPUFreq driver. Only call this if you have
2718 * the right to do so, i.e. if you have succeeded in initialising before!
2719 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2720 * currently not initialised.
2722 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2724 unsigned long flags;
2726 if (!cpufreq_driver || (driver != cpufreq_driver))
2727 return -EINVAL;
2729 pr_debug("unregistering driver %s\n", driver->name);
2731 /* Protect against concurrent cpu hotplug */
2732 cpus_read_lock();
2733 subsys_interface_unregister(&cpufreq_interface);
2734 remove_boost_sysfs_file();
2735 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
2737 write_lock_irqsave(&cpufreq_driver_lock, flags);
2739 cpufreq_driver = NULL;
2741 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2742 cpus_read_unlock();
2744 return 0;
2746 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2748 struct kobject *cpufreq_global_kobject;
2749 EXPORT_SYMBOL(cpufreq_global_kobject);
2751 static int __init cpufreq_core_init(void)
2753 if (cpufreq_disabled())
2754 return -ENODEV;
2756 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
2757 BUG_ON(!cpufreq_global_kobject);
2759 return 0;
2761 module_param(off, int, 0444);
2762 core_initcall(cpufreq_core_init);