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/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)
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
);
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)
96 void disable_cpufreq(void)
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
;
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
;
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
;
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
);
149 idle_time
+= get_cpu_iowait_time_us(cpu
, wall
);
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
);
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
;
221 if (WARN_ON(cpu
>= nr_cpu_ids
))
224 /* get the cpufreq driver */
225 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
227 if (cpufreq_driver
) {
229 policy
= cpufreq_cpu_get_raw(cpu
);
231 kobject_get(&policy
->kobj
);
234 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
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
))
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
278 struct cpufreq_policy
*cpufreq_cpu_acquire(unsigned int cpu
)
280 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
285 down_write(&policy
->rwsem
);
287 if (policy_is_inactive(policy
)) {
288 cpufreq_cpu_release(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
)
310 static unsigned long l_p_j_ref
;
311 static unsigned int l_p_j_ref_freq
;
313 if (ci
->flags
& CPUFREQ_CONST_LOOPS
)
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
,
325 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
326 loops_per_jiffy
, ci
->new);
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
341 static void cpufreq_notify_transition(struct cpufreq_policy
*policy
,
342 struct cpufreq_freqs
*freqs
,
347 BUG_ON(irqs_disabled());
349 if (cpufreq_disabled())
352 freqs
->policy
= policy
;
353 freqs
->flags
= cpufreq_driver
->flags
;
354 pr_debug("notification %u of frequency transition to %u kHz\n",
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
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
);
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
)
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
);
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
);
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
))
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
)
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;
498 pr_warn("CPU%u: Fast frequency switching not enabled\n",
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
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
) {
541 idx
= cpufreq_frequency_table_target(policy
, target_freq
,
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
);
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
;
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
573 return min(latency
* LATENCY_MULTIPLIER
, (unsigned int)10000);
576 return LATENCY_MULTIPLIER
;
578 EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us
);
580 /*********************************************************************
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
)
594 ret
= sscanf(buf
, "%d", &enable
);
595 if (ret
!= 1 || enable
< 0 || enable
> 1)
598 if (cpufreq_boost_trigger_state(enable
)) {
599 pr_err("%s: Cannot %s BOOST!\n",
600 __func__
, enable
? "enable" : "disable");
604 pr_debug("%s: cpufreq BOOST %s\n",
605 __func__
, enable
? "enabled" : "disabled");
609 define_one_global_rw(boost
);
611 static struct cpufreq_governor
*find_governor(const char *str_governor
)
613 struct cpufreq_governor
*t
;
616 if (!strncasecmp(str_governor
, t
->name
, CPUFREQ_NAME_LEN
))
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
);
647 mutex_unlock(&cpufreq_governor_mutex
);
649 ret
= request_module("cpufreq_%s", str_governor
);
653 mutex_lock(&cpufreq_governor_mutex
);
655 t
= find_governor(str_governor
);
657 if (t
&& !try_module_get(t
->owner
))
660 mutex_unlock(&cpufreq_governor_mutex
);
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
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
)
691 static ssize_t
show_scaling_cur_freq(struct cpufreq_policy
*policy
, char *buf
)
696 freq
= arch_freq_get_on_cpu(policy
->cpu
);
698 ret
= sprintf(buf
, "%u\n", freq
);
699 else if (cpufreq_driver
&& cpufreq_driver
->setpolicy
&&
701 ret
= sprintf(buf
, "%u\n", cpufreq_driver
->get(policy
->cpu
));
703 ret
= sprintf(buf
, "%u\n", policy
->cur
);
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) \
717 ret = sscanf(buf, "%lu", &val); \
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
,
734 unsigned int cur_freq
= __cpufreq_get(policy
);
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
);
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];
766 ret
= sscanf(buf
, "%15s", str_governor
);
770 if (cpufreq_driver
->setpolicy
) {
771 unsigned int new_pol
;
773 new_pol
= cpufreq_parse_policy(str_governor
);
777 ret
= cpufreq_set_policy(policy
, NULL
, new_pol
);
779 struct cpufreq_governor
*new_gov
;
781 new_gov
= cpufreq_parse_governor(str_governor
);
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
,
809 struct cpufreq_governor
*t
;
812 i
+= sprintf(buf
, "performance powersave");
816 for_each_governor(t
) {
817 if (i
>= (ssize_t
) ((PAGE_SIZE
/ sizeof(char))
818 - (CPUFREQ_NAME_LEN
+ 2)))
820 i
+= scnprintf(&buf
[i
], CPUFREQ_NAME_PLEN
, "%s ", t
->name
);
823 i
+= sprintf(&buf
[i
], "\n");
827 ssize_t
cpufreq_show_cpus(const struct cpumask
*mask
, char *buf
)
832 for_each_cpu(cpu
, mask
) {
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))
839 i
+= sprintf(&buf
[i
], "\n");
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;
867 if (!policy
->governor
|| !policy
->governor
->store_setspeed
)
870 ret
= sscanf(buf
, "%u", &freq
);
874 policy
->governor
->store_setspeed(policy
, freq
);
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
)
894 ret
= cpufreq_driver
->bios_limit(policy
->cpu
, &limit
);
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
,
923 &scaling_governor
.attr
,
924 &scaling_driver
.attr
,
925 &scaling_available_governors
.attr
,
926 &scaling_setspeed
.attr
,
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
);
942 down_read(&policy
->rwsem
);
943 ret
= fattr
->show(policy
, buf
);
944 up_read(&policy
->rwsem
);
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
;
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
*def_gov
= cpufreq_default_governor();
1057 struct cpufreq_governor
*gov
= NULL
;
1058 unsigned int pol
= CPUFREQ_POLICY_UNKNOWN
;
1061 /* Update policy governor to the one used before hotplug. */
1062 gov
= find_governor(policy
->last_governor
);
1064 pr_debug("Restoring governor %s for cpu %d\n",
1065 policy
->governor
->name
, policy
->cpu
);
1066 } else if (def_gov
) {
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
);
1082 return cpufreq_set_policy(policy
, gov
, pol
);
1085 static int cpufreq_add_policy_cpu(struct cpufreq_policy
*policy
, unsigned int cpu
)
1089 /* Has this CPU been taken care of already? */
1090 if (cpumask_test_cpu(cpu
, policy
->cpus
))
1093 down_write(&policy
->rwsem
);
1095 cpufreq_stop_governor(policy
);
1097 cpumask_set_cpu(cpu
, policy
->cpus
);
1100 ret
= cpufreq_start_governor(policy
);
1102 pr_err("%s: Failed to start governor\n", __func__
);
1104 up_write(&policy
->rwsem
);
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
,
1132 struct cpufreq_policy
*policy
= container_of(nb
, struct cpufreq_policy
, nb_min
);
1134 schedule_work(&policy
->update
);
1138 static int cpufreq_notifier_max(struct notifier_block
*nb
, unsigned long freq
,
1141 struct cpufreq_policy
*policy
= container_of(nb
, struct cpufreq_policy
, nb_max
);
1143 schedule_work(&policy
->update
);
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
);
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
);
1178 policy
= kzalloc(sizeof(*policy
), GFP_KERNEL
);
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
);
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
,
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
,
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
);
1235 err_min_qos_notifier
:
1236 freq_qos_remove_notifier(&policy
->constraints
, FREQ_QOS_MIN
,
1239 cpufreq_policy_put_kobj(policy
);
1241 free_cpumask_var(policy
->real_cpus
);
1243 free_cpumask_var(policy
->related_cpus
);
1245 free_cpumask_var(policy
->cpus
);
1252 static void cpufreq_policy_free(struct cpufreq_policy
*policy
)
1254 unsigned long flags
;
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
,
1267 freq_qos_remove_notifier(&policy
->constraints
, FREQ_QOS_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
);
1293 static int cpufreq_online(unsigned int cpu
)
1295 struct cpufreq_policy
*policy
;
1297 unsigned long flags
;
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
);
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. */
1312 down_write(&policy
->rwsem
);
1314 policy
->governor
= NULL
;
1315 up_write(&policy
->rwsem
);
1318 policy
= cpufreq_policy_alloc(cpu
);
1323 if (!new_policy
&& cpufreq_driver
->online
) {
1324 ret
= cpufreq_driver
->online(policy
);
1326 pr_debug("%s: %d: initialization failed\n", __func__
,
1328 goto out_exit_policy
;
1331 /* Recover policy->cpus using related_cpus */
1332 cpumask_copy(policy
->cpus
, policy
->related_cpus
);
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
);
1342 pr_debug("%s: %d: initialization failed\n", __func__
,
1344 goto out_free_policy
;
1347 ret
= cpufreq_table_validate_and_sort(policy
);
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
);
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
),
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
,
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
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
,
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
);
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
)
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().
1448 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1449 __func__
, policy
->cpu
, policy
->cur
);
1454 ret
= cpufreq_add_dev_interface(policy
);
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
);
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");
1488 for_each_cpu(j
, policy
->real_cpus
)
1489 remove_cpu_dev_symlink(policy
, get_cpu_device(j
));
1491 up_write(&policy
->rwsem
);
1494 if (cpufreq_driver
->exit
)
1495 cpufreq_driver
->exit(policy
);
1498 cpufreq_policy_free(policy
);
1503 * cpufreq_add_dev - the cpufreq interface for a 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
;
1513 dev_dbg(dev
, "%s: adding CPU%u\n", __func__
, cpu
);
1515 if (cpu_online(cpu
)) {
1516 ret
= cpufreq_online(cpu
);
1521 /* Create sysfs link on CPU registration */
1522 policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1524 add_cpu_dev_symlink(policy
, cpu
);
1529 static int cpufreq_offline(unsigned int cpu
)
1531 struct cpufreq_policy
*policy
;
1534 pr_debug("%s: unregistering CPU %u\n", __func__
, cpu
);
1536 policy
= cpufreq_cpu_get_raw(cpu
);
1538 pr_debug("%s: No cpu_data found\n", __func__
);
1542 down_write(&policy
->rwsem
);
1544 cpufreq_stop_governor(policy
);
1546 cpumask_clear_cpu(cpu
, policy
->cpus
);
1548 if (policy_is_inactive(policy
)) {
1550 strncpy(policy
->last_governor
, policy
->governor
->name
,
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
)) {
1562 ret
= cpufreq_start_governor(policy
);
1564 pr_err("%s: Failed to start governor\n", __func__
);
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
);
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
;
1593 up_write(&policy
->rwsem
);
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
);
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
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
);
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())
1664 if (policy
->cur
!= new_freq
) {
1665 cpufreq_out_of_sync(policy
, new_freq
);
1667 schedule_work(&policy
->update
);
1674 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
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
);
1694 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1696 policy
= cpufreq_cpu_get(cpu
);
1698 ret_freq
= policy
->cur
;
1699 cpufreq_cpu_put(policy
);
1704 EXPORT_SYMBOL(cpufreq_quick_get
);
1707 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
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;
1718 ret_freq
= policy
->max
;
1719 cpufreq_cpu_put(policy
);
1724 EXPORT_SYMBOL(cpufreq_quick_get_max
);
1726 static unsigned int __cpufreq_get(struct cpufreq_policy
*policy
)
1728 if (unlikely(policy_is_inactive(policy
)))
1731 return cpufreq_verify_current_freq(policy
, true);
1735 * cpufreq_get - get the current CPU frequency (in kHz)
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;
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
);
1756 EXPORT_SYMBOL(cpufreq_get
);
1758 static struct subsys_interface cpufreq_interface
= {
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
1769 int cpufreq_generic_suspend(struct cpufreq_policy
*policy
)
1773 if (!policy
->suspend_freq
) {
1774 pr_debug("%s: suspend_freq not defined\n", __func__
);
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
);
1784 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1785 __func__
, policy
->suspend_freq
, 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
)
1806 if (!has_target() && !cpufreq_driver
->suspend
)
1809 pr_debug("%s: Suspending Governors\n", __func__
);
1811 for_each_active_policy(policy
) {
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
);
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
;
1838 if (!cpufreq_driver
)
1841 if (unlikely(!cpufreq_suspended
))
1844 cpufreq_suspended
= false;
1846 if (!has_target() && !cpufreq_driver
->resume
)
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__
,
1855 } else if (has_target()) {
1856 down_write(&policy
->rwsem
);
1857 ret
= cpufreq_start_governor(policy
);
1858 up_write(&policy
->rwsem
);
1861 pr_err("%s: Failed to start governor for policy: %p\n",
1868 * cpufreq_get_current_driver - return current driver's name
1870 * Return the name string of the currently loaded cpufreq driver
1873 const char *cpufreq_get_current_driver(void)
1876 return cpufreq_driver
->name
;
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)
1891 return cpufreq_driver
->driver_data
;
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
)
1918 if (cpufreq_disabled())
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
);
1929 ret
= srcu_notifier_chain_register(
1930 &cpufreq_transition_notifier_list
, nb
);
1932 cpufreq_fast_switch_count
--;
1934 mutex_unlock(&cpufreq_fast_switch_lock
);
1936 case CPUFREQ_POLICY_NOTIFIER
:
1937 ret
= blocking_notifier_chain_register(
1938 &cpufreq_policy_notifier_list
, nb
);
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
)
1962 if (cpufreq_disabled())
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
);
1976 case CPUFREQ_POLICY_NOTIFIER
:
1977 ret
= blocking_notifier_chain_unregister(
1978 &cpufreq_policy_notifier_list
, nb
);
1986 EXPORT_SYMBOL(cpufreq_unregister_notifier
);
1989 /*********************************************************************
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
)
2031 freqs
->new = cpufreq_driver
->get_intermediate(policy
, index
);
2033 /* We don't need to switch to intermediate freq */
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
);
2045 pr_err("%s: Failed to change to intermediate frequency: %d\n",
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
;
2059 if (newfreq
== policy
->cur
)
2062 notify
= !(cpufreq_driver
->flags
& CPUFREQ_ASYNC_NOTIFICATION
);
2064 /* Handle switching to intermediate frequency */
2065 if (cpufreq_driver
->get_intermediate
) {
2066 retval
= __target_intermediate(policy
, &freqs
, index
);
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
);
2085 pr_err("%s: Failed to change cpu frequency: %d\n", __func__
,
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);
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
;
2115 if (cpufreq_disabled())
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
2130 if (target_freq
== policy
->cur
)
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
)
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
)
2154 down_write(&policy
->rwsem
);
2156 ret
= __cpufreq_driver_target(policy
, target_freq
, relation
);
2158 up_write(&policy
->rwsem
);
2162 EXPORT_SYMBOL_GPL(cpufreq_driver_target
);
2164 __weak
struct cpufreq_governor
*cpufreq_fallback_governor(void)
2169 static int cpufreq_init_governor(struct cpufreq_policy
*policy
)
2173 /* Don't start any governor operations if we are entering suspend */
2174 if (cpufreq_suspended
)
2177 * Governor might not be initiated here if ACPI _PPC changed
2178 * notification happened, so check it.
2180 if (!policy
->governor
)
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();
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
;
2197 if (!try_module_get(policy
->governor
->owner
))
2200 pr_debug("%s: for CPU %u\n", __func__
, policy
->cpu
);
2202 if (policy
->governor
->init
) {
2203 ret
= policy
->governor
->init(policy
);
2205 module_put(policy
->governor
->owner
);
2213 static void cpufreq_exit_governor(struct cpufreq_policy
*policy
)
2215 if (cpufreq_suspended
|| !policy
->governor
)
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
)
2230 if (cpufreq_suspended
)
2233 if (!policy
->governor
)
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
);
2247 if (policy
->governor
->limits
)
2248 policy
->governor
->limits(policy
);
2253 static void cpufreq_stop_governor(struct cpufreq_policy
*policy
)
2255 if (cpufreq_suspended
|| !policy
->governor
)
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
)
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
)
2282 if (cpufreq_disabled())
2285 mutex_lock(&cpufreq_governor_mutex
);
2288 if (!find_governor(governor
->name
)) {
2290 list_add(&governor
->governor_list
, &cpufreq_governor_list
);
2293 mutex_unlock(&cpufreq_governor_mutex
);
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
;
2306 if (cpufreq_disabled())
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
2335 * Reads the current cpufreq policy.
2337 int cpufreq_get_policy(struct cpufreq_policy
*policy
, unsigned int cpu
)
2339 struct cpufreq_policy
*cpu_policy
;
2343 cpu_policy
= cpufreq_cpu_get(cpu
);
2347 memcpy(policy
, cpu_policy
, sizeof(*policy
));
2349 cpufreq_cpu_put(cpu_policy
);
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
;
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
2394 ret
= cpufreq_driver
->verify(&new_data
);
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
);
2419 pr_debug("governor switch\n");
2421 /* save old, working values */
2422 old_gov
= policy
->governor
;
2423 /* end old governor */
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
);
2433 ret
= cpufreq_start_governor(policy
);
2435 pr_debug("governor change\n");
2436 sched_cpufreq_governor_change(policy
, old_gov
);
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
);
2445 policy
->governor
= old_gov
;
2446 if (cpufreq_init_governor(policy
))
2447 policy
->governor
= NULL
;
2449 cpufreq_start_governor(policy
);
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
);
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))))
2479 refresh_frequency_limits(policy
);
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
);
2498 cpufreq_update_policy(cpu
);
2500 EXPORT_SYMBOL_GPL(cpufreq_update_limits
);
2502 /*********************************************************************
2504 *********************************************************************/
2505 static int cpufreq_boost_set_sw(int state
)
2507 struct cpufreq_policy
*policy
;
2510 for_each_active_policy(policy
) {
2511 if (!policy
->freq_table
)
2514 ret
= cpufreq_frequency_table_cpuinfo(policy
,
2515 policy
->freq_table
);
2517 pr_err("%s: Policy frequency update failed\n",
2522 ret
= freq_qos_update_request(policy
->max_freq_req
, policy
->max
);
2530 int cpufreq_boost_trigger_state(int state
)
2532 unsigned long flags
;
2535 if (cpufreq_driver
->boost_enabled
== state
)
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
);
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");
2555 static bool cpufreq_boost_supported(void)
2557 return cpufreq_driver
->set_boost
;
2560 static int create_boost_sysfs_file(void)
2564 ret
= sysfs_create_file(cpufreq_global_kobject
, &boost
.attr
);
2566 pr_err("%s: cannot register global BOOST sysfs file\n",
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
)
2583 if (cpufreq_boost_supported())
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
);
2611 static int cpuhp_cpufreq_offline(unsigned int cpu
)
2613 cpufreq_offline(cpu
);
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
;
2633 if (cpufreq_disabled())
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
))
2652 pr_debug("trying to register driver %s\n", driver_data
->name
);
2654 /* Protect against concurrent CPU online/offline. */
2657 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2658 if (cpufreq_driver
) {
2659 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
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();
2672 goto err_null_driver
;
2675 ret
= subsys_interface_register(&cpufreq_interface
);
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 */
2683 pr_debug("%s: No CPU initialized for driver %s\n", __func__
,
2688 ret
= cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN
,
2690 cpuhp_cpufreq_online
,
2691 cpuhp_cpufreq_offline
);
2697 pr_debug("driver %s up and running\n", driver_data
->name
);
2701 subsys_interface_unregister(&cpufreq_interface
);
2703 remove_boost_sysfs_file();
2705 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2706 cpufreq_driver
= NULL
;
2707 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
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
))
2729 pr_debug("unregistering driver %s\n", driver
->name
);
2731 /* Protect against concurrent cpu hotplug */
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
);
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())
2756 cpufreq_global_kobject
= kobject_create_and_add("cpufreq", &cpu_subsys
.dev_root
->kobj
);
2757 BUG_ON(!cpufreq_global_kobject
);
2761 module_param(off
, int, 0444);
2762 core_initcall(cpufreq_core_init
);