1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/drivers/cpufreq/cpufreq.c
4 *
5 * Copyright (C) 2001 Russell King
6 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
8 *
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
13 */
14
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
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/string_choices.h>
29 #include <linux/suspend.h>
30 #include <linux/syscore_ops.h>
31 #include <linux/tick.h>
32 #include <linux/units.h>
33 #include <trace/events/power.h>
34
35 static LIST_HEAD(cpufreq_policy_list);
36
37 /* Macros to iterate over CPU policies */
38 #define for_each_suitable_policy(__policy, __active) \
39 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
40 if ((__active) == !policy_is_inactive(__policy))
41
42 #define for_each_active_policy(__policy) \
43 for_each_suitable_policy(__policy, true)
44 #define for_each_inactive_policy(__policy) \
45 for_each_suitable_policy(__policy, false)
46
47 /* Iterate over governors */
48 static LIST_HEAD(cpufreq_governor_list);
49 #define for_each_governor(__governor) \
50 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
51
52 static char default_governor[CPUFREQ_NAME_LEN];
53
54 /*
55 * The "cpufreq driver" - the arch- or hardware-dependent low
56 * level driver of CPUFreq support, and its spinlock. This lock
57 * also protects the cpufreq_cpu_data array.
58 */
59 static struct cpufreq_driver *cpufreq_driver;
60 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
61 static DEFINE_RWLOCK(cpufreq_driver_lock);
62
63 static DEFINE_STATIC_KEY_FALSE(cpufreq_freq_invariance);
cpufreq_supports_freq_invariance(void)64 bool cpufreq_supports_freq_invariance(void)
65 {
66 return static_branch_likely(&cpufreq_freq_invariance);
67 }
68
69 /* Flag to suspend/resume CPUFreq governors */
70 static bool cpufreq_suspended;
71
has_target(void)72 static inline bool has_target(void)
73 {
74 return cpufreq_driver->target_index || cpufreq_driver->target;
75 }
76
has_target_index(void)77 bool has_target_index(void)
78 {
79 return !!cpufreq_driver->target_index;
80 }
81
82 /* internal prototypes */
83 static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
84 static int cpufreq_init_governor(struct cpufreq_policy *policy);
85 static void cpufreq_exit_governor(struct cpufreq_policy *policy);
86 static void cpufreq_governor_limits(struct cpufreq_policy *policy);
87 static int cpufreq_set_policy(struct cpufreq_policy *policy,
88 struct cpufreq_governor *new_gov,
89 unsigned int new_pol);
90 static bool cpufreq_boost_supported(void);
91 static int cpufreq_boost_trigger_state(int state);
92
93 /*
94 * Two notifier lists: the "policy" list is involved in the
95 * validation process for a new CPU frequency policy; the
96 * "transition" list for kernel code that needs to handle
97 * changes to devices when the CPU clock speed changes.
98 * The mutex locks both lists.
99 */
100 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
101 SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
102
103 static int off __read_mostly;
cpufreq_disabled(void)104 static int cpufreq_disabled(void)
105 {
106 return off;
107 }
disable_cpufreq(void)108 void disable_cpufreq(void)
109 {
110 off = 1;
111 }
112 static DEFINE_MUTEX(cpufreq_governor_mutex);
113
have_governor_per_policy(void)114 bool have_governor_per_policy(void)
115 {
116 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
117 }
118 EXPORT_SYMBOL_GPL(have_governor_per_policy);
119
120 static struct kobject *cpufreq_global_kobject;
121
get_governor_parent_kobj(struct cpufreq_policy * policy)122 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
123 {
124 if (have_governor_per_policy())
125 return &policy->kobj;
126 else
127 return cpufreq_global_kobject;
128 }
129 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
130
get_cpu_idle_time_jiffy(unsigned int cpu,u64 * wall)131 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
132 {
133 struct kernel_cpustat kcpustat;
134 u64 cur_wall_time;
135 u64 idle_time;
136 u64 busy_time;
137
138 cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
139
140 kcpustat_cpu_fetch(&kcpustat, cpu);
141
142 busy_time = kcpustat.cpustat[CPUTIME_USER];
143 busy_time += kcpustat.cpustat[CPUTIME_SYSTEM];
144 busy_time += kcpustat.cpustat[CPUTIME_IRQ];
145 busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ];
146 busy_time += kcpustat.cpustat[CPUTIME_STEAL];
147 busy_time += kcpustat.cpustat[CPUTIME_NICE];
148
149 idle_time = cur_wall_time - busy_time;
150 if (wall)
151 *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
152
153 return div_u64(idle_time, NSEC_PER_USEC);
154 }
155
get_cpu_idle_time(unsigned int cpu,u64 * wall,int io_busy)156 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
157 {
158 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
159
160 if (idle_time == -1ULL)
161 return get_cpu_idle_time_jiffy(cpu, wall);
162 else if (!io_busy)
163 idle_time += get_cpu_iowait_time_us(cpu, wall);
164
165 return idle_time;
166 }
167 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
168
169 /*
170 * This is a generic cpufreq init() routine which can be used by cpufreq
171 * drivers of SMP systems. It will do following:
172 * - validate & show freq table passed
173 * - set policies transition latency
174 * - policy->cpus with all possible CPUs
175 */
cpufreq_generic_init(struct cpufreq_policy * policy,struct cpufreq_frequency_table * table,unsigned int transition_latency)176 void cpufreq_generic_init(struct cpufreq_policy *policy,
177 struct cpufreq_frequency_table *table,
178 unsigned int transition_latency)
179 {
180 policy->freq_table = table;
181 policy->cpuinfo.transition_latency = transition_latency;
182
183 /*
184 * The driver only supports the SMP configuration where all processors
185 * share the clock and voltage and clock.
186 */
187 cpumask_setall(policy->cpus);
188 }
189 EXPORT_SYMBOL_GPL(cpufreq_generic_init);
190
cpufreq_cpu_get_raw(unsigned int cpu)191 struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
192 {
193 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
194
195 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
196 }
197 EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
198
cpufreq_generic_get(unsigned int cpu)199 unsigned int cpufreq_generic_get(unsigned int cpu)
200 {
201 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
202
203 if (!policy || IS_ERR(policy->clk)) {
204 pr_err("%s: No %s associated to cpu: %d\n",
205 __func__, policy ? "clk" : "policy", cpu);
206 return 0;
207 }
208
209 return clk_get_rate(policy->clk) / 1000;
210 }
211 EXPORT_SYMBOL_GPL(cpufreq_generic_get);
212
213 /**
214 * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
215 * @cpu: CPU to find the policy for.
216 *
217 * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
218 * the kobject reference counter of that policy. Return a valid policy on
219 * success or NULL on failure.
220 *
221 * The policy returned by this function has to be released with the help of
222 * cpufreq_cpu_put() to balance its kobject reference counter properly.
223 */
cpufreq_cpu_get(unsigned int cpu)224 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
225 {
226 struct cpufreq_policy *policy = NULL;
227 unsigned long flags;
228
229 if (WARN_ON(cpu >= nr_cpu_ids))
230 return NULL;
231
232 /* get the cpufreq driver */
233 read_lock_irqsave(&cpufreq_driver_lock, flags);
234
235 if (cpufreq_driver) {
236 /* get the CPU */
237 policy = cpufreq_cpu_get_raw(cpu);
238 if (policy)
239 kobject_get(&policy->kobj);
240 }
241
242 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
243
244 return policy;
245 }
246 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
247
248 /**
249 * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
250 * @policy: cpufreq policy returned by cpufreq_cpu_get().
251 */
cpufreq_cpu_put(struct cpufreq_policy * policy)252 void cpufreq_cpu_put(struct cpufreq_policy *policy)
253 {
254 kobject_put(&policy->kobj);
255 }
256 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
257
258 /**
259 * cpufreq_cpu_release - Unlock a policy and decrement its usage counter.
260 * @policy: cpufreq policy returned by cpufreq_cpu_acquire().
261 */
cpufreq_cpu_release(struct cpufreq_policy * policy)262 void cpufreq_cpu_release(struct cpufreq_policy *policy)
263 {
264 if (WARN_ON(!policy))
265 return;
266
267 lockdep_assert_held(&policy->rwsem);
268
269 up_write(&policy->rwsem);
270
271 cpufreq_cpu_put(policy);
272 }
273
274 /**
275 * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it.
276 * @cpu: CPU to find the policy for.
277 *
278 * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and
279 * if the policy returned by it is not NULL, acquire its rwsem for writing.
280 * Return the policy if it is active or release it and return NULL otherwise.
281 *
282 * The policy returned by this function has to be released with the help of
283 * cpufreq_cpu_release() in order to release its rwsem and balance its usage
284 * counter properly.
285 */
cpufreq_cpu_acquire(unsigned int cpu)286 struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu)
287 {
288 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
289
290 if (!policy)
291 return NULL;
292
293 down_write(&policy->rwsem);
294
295 if (policy_is_inactive(policy)) {
296 cpufreq_cpu_release(policy);
297 return NULL;
298 }
299
300 return policy;
301 }
302
303 /*********************************************************************
304 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
305 *********************************************************************/
306
307 /**
308 * adjust_jiffies - Adjust the system "loops_per_jiffy".
309 * @val: CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
310 * @ci: Frequency change information.
311 *
312 * This function alters the system "loops_per_jiffy" for the clock
313 * speed change. Note that loops_per_jiffy cannot be updated on SMP
314 * systems as each CPU might be scaled differently. So, use the arch
315 * per-CPU loops_per_jiffy value wherever possible.
316 */
adjust_jiffies(unsigned long val,struct cpufreq_freqs * ci)317 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
318 {
319 #ifndef CONFIG_SMP
320 static unsigned long l_p_j_ref;
321 static unsigned int l_p_j_ref_freq;
322
323 if (ci->flags & CPUFREQ_CONST_LOOPS)
324 return;
325
326 if (!l_p_j_ref_freq) {
327 l_p_j_ref = loops_per_jiffy;
328 l_p_j_ref_freq = ci->old;
329 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
330 l_p_j_ref, l_p_j_ref_freq);
331 }
332 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
333 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
334 ci->new);
335 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
336 loops_per_jiffy, ci->new);
337 }
338 #endif
339 }
340
341 /**
342 * cpufreq_notify_transition - Notify frequency transition and adjust jiffies.
343 * @policy: cpufreq policy to enable fast frequency switching for.
344 * @freqs: contain details of the frequency update.
345 * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
346 *
347 * This function calls the transition notifiers and adjust_jiffies().
348 *
349 * It is called twice on all CPU frequency changes that have external effects.
350 */
cpufreq_notify_transition(struct cpufreq_policy * policy,struct cpufreq_freqs * freqs,unsigned int state)351 static void cpufreq_notify_transition(struct cpufreq_policy *policy,
352 struct cpufreq_freqs *freqs,
353 unsigned int state)
354 {
355 int cpu;
356
357 BUG_ON(irqs_disabled());
358
359 if (cpufreq_disabled())
360 return;
361
362 freqs->policy = policy;
363 freqs->flags = cpufreq_driver->flags;
364 pr_debug("notification %u of frequency transition to %u kHz\n",
365 state, freqs->new);
366
367 switch (state) {
368 case CPUFREQ_PRECHANGE:
369 /*
370 * Detect if the driver reported a value as "old frequency"
371 * which is not equal to what the cpufreq core thinks is
372 * "old frequency".
373 */
374 if (policy->cur && policy->cur != freqs->old) {
375 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
376 freqs->old, policy->cur);
377 freqs->old = policy->cur;
378 }
379
380 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
381 CPUFREQ_PRECHANGE, freqs);
382
383 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
384 break;
385
386 case CPUFREQ_POSTCHANGE:
387 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
388 pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
389 cpumask_pr_args(policy->cpus));
390
391 for_each_cpu(cpu, policy->cpus)
392 trace_cpu_frequency(freqs->new, cpu);
393
394 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
395 CPUFREQ_POSTCHANGE, freqs);
396
397 cpufreq_stats_record_transition(policy, freqs->new);
398 policy->cur = freqs->new;
399 }
400 }
401
402 /* Do post notifications when there are chances that transition has failed */
cpufreq_notify_post_transition(struct cpufreq_policy * policy,struct cpufreq_freqs * freqs,int transition_failed)403 static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
404 struct cpufreq_freqs *freqs, int transition_failed)
405 {
406 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
407 if (!transition_failed)
408 return;
409
410 swap(freqs->old, freqs->new);
411 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
412 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
413 }
414
cpufreq_freq_transition_begin(struct cpufreq_policy * policy,struct cpufreq_freqs * freqs)415 void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
416 struct cpufreq_freqs *freqs)
417 {
418
419 /*
420 * Catch double invocations of _begin() which lead to self-deadlock.
421 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
422 * doesn't invoke _begin() on their behalf, and hence the chances of
423 * double invocations are very low. Moreover, there are scenarios
424 * where these checks can emit false-positive warnings in these
425 * drivers; so we avoid that by skipping them altogether.
426 */
427 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
428 && current == policy->transition_task);
429
430 wait:
431 wait_event(policy->transition_wait, !policy->transition_ongoing);
432
433 spin_lock(&policy->transition_lock);
434
435 if (unlikely(policy->transition_ongoing)) {
436 spin_unlock(&policy->transition_lock);
437 goto wait;
438 }
439
440 policy->transition_ongoing = true;
441 policy->transition_task = current;
442
443 spin_unlock(&policy->transition_lock);
444
445 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
446 }
447 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
448
cpufreq_freq_transition_end(struct cpufreq_policy * policy,struct cpufreq_freqs * freqs,int transition_failed)449 void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
450 struct cpufreq_freqs *freqs, int transition_failed)
451 {
452 if (WARN_ON(!policy->transition_ongoing))
453 return;
454
455 cpufreq_notify_post_transition(policy, freqs, transition_failed);
456
457 arch_set_freq_scale(policy->related_cpus,
458 policy->cur,
459 arch_scale_freq_ref(policy->cpu));
460
461 spin_lock(&policy->transition_lock);
462 policy->transition_ongoing = false;
463 policy->transition_task = NULL;
464 spin_unlock(&policy->transition_lock);
465
466 wake_up(&policy->transition_wait);
467 }
468 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
469
470 /*
471 * Fast frequency switching status count. Positive means "enabled", negative
472 * means "disabled" and 0 means "not decided yet".
473 */
474 static int cpufreq_fast_switch_count;
475 static DEFINE_MUTEX(cpufreq_fast_switch_lock);
476
cpufreq_list_transition_notifiers(void)477 static void cpufreq_list_transition_notifiers(void)
478 {
479 struct notifier_block *nb;
480
481 pr_info("Registered transition notifiers:\n");
482
483 mutex_lock(&cpufreq_transition_notifier_list.mutex);
484
485 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
486 pr_info("%pS\n", nb->notifier_call);
487
488 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
489 }
490
491 /**
492 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
493 * @policy: cpufreq policy to enable fast frequency switching for.
494 *
495 * Try to enable fast frequency switching for @policy.
496 *
497 * The attempt will fail if there is at least one transition notifier registered
498 * at this point, as fast frequency switching is quite fundamentally at odds
499 * with transition notifiers. Thus if successful, it will make registration of
500 * transition notifiers fail going forward.
501 */
cpufreq_enable_fast_switch(struct cpufreq_policy * policy)502 void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
503 {
504 lockdep_assert_held(&policy->rwsem);
505
506 if (!policy->fast_switch_possible)
507 return;
508
509 mutex_lock(&cpufreq_fast_switch_lock);
510 if (cpufreq_fast_switch_count >= 0) {
511 cpufreq_fast_switch_count++;
512 policy->fast_switch_enabled = true;
513 } else {
514 pr_warn("CPU%u: Fast frequency switching not enabled\n",
515 policy->cpu);
516 cpufreq_list_transition_notifiers();
517 }
518 mutex_unlock(&cpufreq_fast_switch_lock);
519 }
520 EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
521
522 /**
523 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
524 * @policy: cpufreq policy to disable fast frequency switching for.
525 */
cpufreq_disable_fast_switch(struct cpufreq_policy * policy)526 void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
527 {
528 mutex_lock(&cpufreq_fast_switch_lock);
529 if (policy->fast_switch_enabled) {
530 policy->fast_switch_enabled = false;
531 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
532 cpufreq_fast_switch_count--;
533 }
534 mutex_unlock(&cpufreq_fast_switch_lock);
535 }
536 EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
537
__resolve_freq(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int min,unsigned int max,unsigned int relation)538 static unsigned int __resolve_freq(struct cpufreq_policy *policy,
539 unsigned int target_freq,
540 unsigned int min, unsigned int max,
541 unsigned int relation)
542 {
543 unsigned int idx;
544
545 target_freq = clamp_val(target_freq, min, max);
546
547 if (!policy->freq_table)
548 return target_freq;
549
550 idx = cpufreq_frequency_table_target(policy, target_freq, min, max, relation);
551 policy->cached_resolved_idx = idx;
552 policy->cached_target_freq = target_freq;
553 return policy->freq_table[idx].frequency;
554 }
555
556 /**
557 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
558 * one.
559 * @policy: associated policy to interrogate
560 * @target_freq: target frequency to resolve.
561 *
562 * The target to driver frequency mapping is cached in the policy.
563 *
564 * Return: Lowest driver-supported frequency greater than or equal to the
565 * given target_freq, subject to policy (min/max) and driver limitations.
566 */
cpufreq_driver_resolve_freq(struct cpufreq_policy * policy,unsigned int target_freq)567 unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
568 unsigned int target_freq)
569 {
570 unsigned int min = READ_ONCE(policy->min);
571 unsigned int max = READ_ONCE(policy->max);
572
573 /*
574 * If this function runs in parallel with cpufreq_set_policy(), it may
575 * read policy->min before the update and policy->max after the update
576 * or the other way around, so there is no ordering guarantee.
577 *
578 * Resolve this by always honoring the max (in case it comes from
579 * thermal throttling or similar).
580 */
581 if (unlikely(min > max))
582 min = max;
583
584 return __resolve_freq(policy, target_freq, min, max, CPUFREQ_RELATION_LE);
585 }
586 EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
587
cpufreq_policy_transition_delay_us(struct cpufreq_policy * policy)588 unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
589 {
590 unsigned int latency;
591
592 if (policy->transition_delay_us)
593 return policy->transition_delay_us;
594
595 latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
596 if (latency)
597 /* Give a 50% breathing room between updates */
598 return latency + (latency >> 1);
599
600 return USEC_PER_MSEC;
601 }
602 EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
603
604 /*********************************************************************
605 * SYSFS INTERFACE *
606 *********************************************************************/
show_boost(struct kobject * kobj,struct kobj_attribute * attr,char * buf)607 static ssize_t show_boost(struct kobject *kobj,
608 struct kobj_attribute *attr, char *buf)
609 {
610 return sysfs_emit(buf, "%d\n", cpufreq_driver->boost_enabled);
611 }
612
store_boost(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)613 static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
614 const char *buf, size_t count)
615 {
616 bool enable;
617
618 if (kstrtobool(buf, &enable))
619 return -EINVAL;
620
621 if (cpufreq_boost_trigger_state(enable)) {
622 pr_err("%s: Cannot %s BOOST!\n",
623 __func__, str_enable_disable(enable));
624 return -EINVAL;
625 }
626
627 pr_debug("%s: cpufreq BOOST %s\n",
628 __func__, str_enabled_disabled(enable));
629
630 return count;
631 }
632 define_one_global_rw(boost);
633
show_local_boost(struct cpufreq_policy * policy,char * buf)634 static ssize_t show_local_boost(struct cpufreq_policy *policy, char *buf)
635 {
636 return sysfs_emit(buf, "%d\n", policy->boost_enabled);
637 }
638
store_local_boost(struct cpufreq_policy * policy,const char * buf,size_t count)639 static ssize_t store_local_boost(struct cpufreq_policy *policy,
640 const char *buf, size_t count)
641 {
642 int ret;
643 bool enable;
644
645 if (kstrtobool(buf, &enable))
646 return -EINVAL;
647
648 if (!cpufreq_driver->boost_enabled)
649 return -EINVAL;
650
651 if (!policy->boost_supported)
652 return -EINVAL;
653
654 if (policy->boost_enabled == enable)
655 return count;
656
657 policy->boost_enabled = enable;
658
659 cpus_read_lock();
660 ret = cpufreq_driver->set_boost(policy, enable);
661 cpus_read_unlock();
662
663 if (ret) {
664 policy->boost_enabled = !policy->boost_enabled;
665 return ret;
666 }
667
668 return count;
669 }
670
671 static struct freq_attr local_boost = __ATTR(boost, 0644, show_local_boost, store_local_boost);
672
find_governor(const char * str_governor)673 static struct cpufreq_governor *find_governor(const char *str_governor)
674 {
675 struct cpufreq_governor *t;
676
677 for_each_governor(t)
678 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
679 return t;
680
681 return NULL;
682 }
683
get_governor(const char * str_governor)684 static struct cpufreq_governor *get_governor(const char *str_governor)
685 {
686 struct cpufreq_governor *t;
687
688 mutex_lock(&cpufreq_governor_mutex);
689 t = find_governor(str_governor);
690 if (!t)
691 goto unlock;
692
693 if (!try_module_get(t->owner))
694 t = NULL;
695
696 unlock:
697 mutex_unlock(&cpufreq_governor_mutex);
698
699 return t;
700 }
701
cpufreq_parse_policy(char * str_governor)702 static unsigned int cpufreq_parse_policy(char *str_governor)
703 {
704 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN))
705 return CPUFREQ_POLICY_PERFORMANCE;
706
707 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN))
708 return CPUFREQ_POLICY_POWERSAVE;
709
710 return CPUFREQ_POLICY_UNKNOWN;
711 }
712
713 /**
714 * cpufreq_parse_governor - parse a governor string only for has_target()
715 * @str_governor: Governor name.
716 */
cpufreq_parse_governor(char * str_governor)717 static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor)
718 {
719 struct cpufreq_governor *t;
720
721 t = get_governor(str_governor);
722 if (t)
723 return t;
724
725 if (request_module("cpufreq_%s", str_governor))
726 return NULL;
727
728 return get_governor(str_governor);
729 }
730
731 /*
732 * cpufreq_per_cpu_attr_read() / show_##file_name() -
733 * print out cpufreq information
734 *
735 * Write out information from cpufreq_driver->policy[cpu]; object must be
736 * "unsigned int".
737 */
738
739 #define show_one(file_name, object) \
740 static ssize_t show_##file_name \
741 (struct cpufreq_policy *policy, char *buf) \
742 { \
743 return sysfs_emit(buf, "%u\n", policy->object); \
744 }
745
746 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
747 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
748 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
749 show_one(scaling_min_freq, min);
750 show_one(scaling_max_freq, max);
751
arch_freq_get_on_cpu(int cpu)752 __weak int arch_freq_get_on_cpu(int cpu)
753 {
754 return -EOPNOTSUPP;
755 }
756
cpufreq_avg_freq_supported(struct cpufreq_policy * policy)757 static inline bool cpufreq_avg_freq_supported(struct cpufreq_policy *policy)
758 {
759 return arch_freq_get_on_cpu(policy->cpu) != -EOPNOTSUPP;
760 }
761
show_scaling_cur_freq(struct cpufreq_policy * policy,char * buf)762 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
763 {
764 ssize_t ret;
765 int freq;
766
767 freq = IS_ENABLED(CONFIG_CPUFREQ_ARCH_CUR_FREQ)
768 ? arch_freq_get_on_cpu(policy->cpu)
769 : 0;
770
771 if (freq > 0)
772 ret = sysfs_emit(buf, "%u\n", freq);
773 else if (cpufreq_driver->setpolicy && cpufreq_driver->get)
774 ret = sysfs_emit(buf, "%u\n", cpufreq_driver->get(policy->cpu));
775 else
776 ret = sysfs_emit(buf, "%u\n", policy->cur);
777 return ret;
778 }
779
780 /*
781 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
782 */
783 #define store_one(file_name, object) \
784 static ssize_t store_##file_name \
785 (struct cpufreq_policy *policy, const char *buf, size_t count) \
786 { \
787 unsigned long val; \
788 int ret; \
789 \
790 ret = kstrtoul(buf, 0, &val); \
791 if (ret) \
792 return ret; \
793 \
794 ret = freq_qos_update_request(policy->object##_freq_req, val);\
795 return ret >= 0 ? count : ret; \
796 }
797
798 store_one(scaling_min_freq, min);
799 store_one(scaling_max_freq, max);
800
801 /*
802 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
803 */
show_cpuinfo_cur_freq(struct cpufreq_policy * policy,char * buf)804 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
805 char *buf)
806 {
807 unsigned int cur_freq = __cpufreq_get(policy);
808
809 if (cur_freq)
810 return sysfs_emit(buf, "%u\n", cur_freq);
811
812 return sysfs_emit(buf, "<unknown>\n");
813 }
814
815 /*
816 * show_cpuinfo_avg_freq - average CPU frequency as detected by hardware
817 */
show_cpuinfo_avg_freq(struct cpufreq_policy * policy,char * buf)818 static ssize_t show_cpuinfo_avg_freq(struct cpufreq_policy *policy,
819 char *buf)
820 {
821 int avg_freq = arch_freq_get_on_cpu(policy->cpu);
822
823 if (avg_freq > 0)
824 return sysfs_emit(buf, "%u\n", avg_freq);
825 return avg_freq != 0 ? avg_freq : -EINVAL;
826 }
827
828 /*
829 * show_scaling_governor - show the current policy for the specified CPU
830 */
show_scaling_governor(struct cpufreq_policy * policy,char * buf)831 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
832 {
833 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
834 return sysfs_emit(buf, "powersave\n");
835 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
836 return sysfs_emit(buf, "performance\n");
837 else if (policy->governor)
838 return sysfs_emit(buf, "%s\n", policy->governor->name);
839 return -EINVAL;
840 }
841
842 /*
843 * store_scaling_governor - store policy for the specified CPU
844 */
store_scaling_governor(struct cpufreq_policy * policy,const char * buf,size_t count)845 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
846 const char *buf, size_t count)
847 {
848 char str_governor[16];
849 int ret;
850
851 ret = sscanf(buf, "%15s", str_governor);
852 if (ret != 1)
853 return -EINVAL;
854
855 if (cpufreq_driver->setpolicy) {
856 unsigned int new_pol;
857
858 new_pol = cpufreq_parse_policy(str_governor);
859 if (!new_pol)
860 return -EINVAL;
861
862 ret = cpufreq_set_policy(policy, NULL, new_pol);
863 } else {
864 struct cpufreq_governor *new_gov;
865
866 new_gov = cpufreq_parse_governor(str_governor);
867 if (!new_gov)
868 return -EINVAL;
869
870 ret = cpufreq_set_policy(policy, new_gov,
871 CPUFREQ_POLICY_UNKNOWN);
872
873 module_put(new_gov->owner);
874 }
875
876 return ret ? ret : count;
877 }
878
879 /*
880 * show_scaling_driver - show the cpufreq driver currently loaded
881 */
show_scaling_driver(struct cpufreq_policy * policy,char * buf)882 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
883 {
884 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
885 }
886
887 /*
888 * show_scaling_available_governors - show the available CPUfreq governors
889 */
show_scaling_available_governors(struct cpufreq_policy * policy,char * buf)890 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
891 char *buf)
892 {
893 ssize_t i = 0;
894 struct cpufreq_governor *t;
895
896 if (!has_target()) {
897 i += sysfs_emit(buf, "performance powersave");
898 goto out;
899 }
900
901 mutex_lock(&cpufreq_governor_mutex);
902 for_each_governor(t) {
903 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
904 - (CPUFREQ_NAME_LEN + 2)))
905 break;
906 i += sysfs_emit_at(buf, i, "%s ", t->name);
907 }
908 mutex_unlock(&cpufreq_governor_mutex);
909 out:
910 i += sysfs_emit_at(buf, i, "\n");
911 return i;
912 }
913
cpufreq_show_cpus(const struct cpumask * mask,char * buf)914 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
915 {
916 ssize_t i = 0;
917 unsigned int cpu;
918
919 for_each_cpu(cpu, mask) {
920 i += sysfs_emit_at(buf, i, "%u ", cpu);
921 if (i >= (PAGE_SIZE - 5))
922 break;
923 }
924
925 /* Remove the extra space at the end */
926 i--;
927
928 i += sysfs_emit_at(buf, i, "\n");
929 return i;
930 }
931 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
932
933 /*
934 * show_related_cpus - show the CPUs affected by each transition even if
935 * hw coordination is in use
936 */
show_related_cpus(struct cpufreq_policy * policy,char * buf)937 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
938 {
939 return cpufreq_show_cpus(policy->related_cpus, buf);
940 }
941
942 /*
943 * show_affected_cpus - show the CPUs affected by each transition
944 */
show_affected_cpus(struct cpufreq_policy * policy,char * buf)945 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
946 {
947 return cpufreq_show_cpus(policy->cpus, buf);
948 }
949
store_scaling_setspeed(struct cpufreq_policy * policy,const char * buf,size_t count)950 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
951 const char *buf, size_t count)
952 {
953 unsigned int freq = 0;
954 unsigned int ret;
955
956 if (!policy->governor || !policy->governor->store_setspeed)
957 return -EINVAL;
958
959 ret = sscanf(buf, "%u", &freq);
960 if (ret != 1)
961 return -EINVAL;
962
963 policy->governor->store_setspeed(policy, freq);
964
965 return count;
966 }
967
show_scaling_setspeed(struct cpufreq_policy * policy,char * buf)968 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
969 {
970 if (!policy->governor || !policy->governor->show_setspeed)
971 return sysfs_emit(buf, "<unsupported>\n");
972
973 return policy->governor->show_setspeed(policy, buf);
974 }
975
976 /*
977 * show_bios_limit - show the current cpufreq HW/BIOS limitation
978 */
show_bios_limit(struct cpufreq_policy * policy,char * buf)979 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
980 {
981 unsigned int limit;
982 int ret;
983 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
984 if (!ret)
985 return sysfs_emit(buf, "%u\n", limit);
986 return sysfs_emit(buf, "%u\n", policy->cpuinfo.max_freq);
987 }
988
989 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
990 cpufreq_freq_attr_ro(cpuinfo_avg_freq);
991 cpufreq_freq_attr_ro(cpuinfo_min_freq);
992 cpufreq_freq_attr_ro(cpuinfo_max_freq);
993 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
994 cpufreq_freq_attr_ro(scaling_available_governors);
995 cpufreq_freq_attr_ro(scaling_driver);
996 cpufreq_freq_attr_ro(scaling_cur_freq);
997 cpufreq_freq_attr_ro(bios_limit);
998 cpufreq_freq_attr_ro(related_cpus);
999 cpufreq_freq_attr_ro(affected_cpus);
1000 cpufreq_freq_attr_rw(scaling_min_freq);
1001 cpufreq_freq_attr_rw(scaling_max_freq);
1002 cpufreq_freq_attr_rw(scaling_governor);
1003 cpufreq_freq_attr_rw(scaling_setspeed);
1004
1005 static struct attribute *cpufreq_attrs[] = {
1006 &cpuinfo_min_freq.attr,
1007 &cpuinfo_max_freq.attr,
1008 &cpuinfo_transition_latency.attr,
1009 &scaling_min_freq.attr,
1010 &scaling_max_freq.attr,
1011 &affected_cpus.attr,
1012 &related_cpus.attr,
1013 &scaling_governor.attr,
1014 &scaling_driver.attr,
1015 &scaling_available_governors.attr,
1016 &scaling_setspeed.attr,
1017 NULL
1018 };
1019 ATTRIBUTE_GROUPS(cpufreq);
1020
1021 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
1022 #define to_attr(a) container_of(a, struct freq_attr, attr)
1023
show(struct kobject * kobj,struct attribute * attr,char * buf)1024 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
1025 {
1026 struct cpufreq_policy *policy = to_policy(kobj);
1027 struct freq_attr *fattr = to_attr(attr);
1028 ssize_t ret = -EBUSY;
1029
1030 if (!fattr->show)
1031 return -EIO;
1032
1033 down_read(&policy->rwsem);
1034 if (likely(!policy_is_inactive(policy)))
1035 ret = fattr->show(policy, buf);
1036 up_read(&policy->rwsem);
1037
1038 return ret;
1039 }
1040
store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)1041 static ssize_t store(struct kobject *kobj, struct attribute *attr,
1042 const char *buf, size_t count)
1043 {
1044 struct cpufreq_policy *policy = to_policy(kobj);
1045 struct freq_attr *fattr = to_attr(attr);
1046 ssize_t ret = -EBUSY;
1047
1048 if (!fattr->store)
1049 return -EIO;
1050
1051 down_write(&policy->rwsem);
1052 if (likely(!policy_is_inactive(policy)))
1053 ret = fattr->store(policy, buf, count);
1054 up_write(&policy->rwsem);
1055
1056 return ret;
1057 }
1058
cpufreq_sysfs_release(struct kobject * kobj)1059 static void cpufreq_sysfs_release(struct kobject *kobj)
1060 {
1061 struct cpufreq_policy *policy = to_policy(kobj);
1062 pr_debug("last reference is dropped\n");
1063 complete(&policy->kobj_unregister);
1064 }
1065
1066 static const struct sysfs_ops sysfs_ops = {
1067 .show = show,
1068 .store = store,
1069 };
1070
1071 static const struct kobj_type ktype_cpufreq = {
1072 .sysfs_ops = &sysfs_ops,
1073 .default_groups = cpufreq_groups,
1074 .release = cpufreq_sysfs_release,
1075 };
1076
add_cpu_dev_symlink(struct cpufreq_policy * policy,unsigned int cpu,struct device * dev)1077 static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu,
1078 struct device *dev)
1079 {
1080 if (unlikely(!dev))
1081 return;
1082
1083 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1084 return;
1085
1086 dev_dbg(dev, "%s: Adding symlink\n", __func__);
1087 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
1088 dev_err(dev, "cpufreq symlink creation failed\n");
1089 }
1090
remove_cpu_dev_symlink(struct cpufreq_policy * policy,int cpu,struct device * dev)1091 static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu,
1092 struct device *dev)
1093 {
1094 dev_dbg(dev, "%s: Removing symlink\n", __func__);
1095 sysfs_remove_link(&dev->kobj, "cpufreq");
1096 cpumask_clear_cpu(cpu, policy->real_cpus);
1097 }
1098
cpufreq_add_dev_interface(struct cpufreq_policy * policy)1099 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
1100 {
1101 struct freq_attr **drv_attr;
1102 int ret = 0;
1103
1104 /* Attributes that need freq_table */
1105 if (policy->freq_table) {
1106 ret = sysfs_create_file(&policy->kobj,
1107 &cpufreq_freq_attr_scaling_available_freqs.attr);
1108 if (ret)
1109 return ret;
1110
1111 if (cpufreq_boost_supported()) {
1112 ret = sysfs_create_file(&policy->kobj,
1113 &cpufreq_freq_attr_scaling_boost_freqs.attr);
1114 if (ret)
1115 return ret;
1116 }
1117 }
1118
1119 /* set up files for this cpu device */
1120 drv_attr = cpufreq_driver->attr;
1121 while (drv_attr && *drv_attr) {
1122 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1123 if (ret)
1124 return ret;
1125 drv_attr++;
1126 }
1127 if (cpufreq_driver->get) {
1128 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1129 if (ret)
1130 return ret;
1131 }
1132
1133 if (cpufreq_avg_freq_supported(policy)) {
1134 ret = sysfs_create_file(&policy->kobj, &cpuinfo_avg_freq.attr);
1135 if (ret)
1136 return ret;
1137 }
1138
1139 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1140 if (ret)
1141 return ret;
1142
1143 if (cpufreq_driver->bios_limit) {
1144 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1145 if (ret)
1146 return ret;
1147 }
1148
1149 if (cpufreq_boost_supported()) {
1150 ret = sysfs_create_file(&policy->kobj, &local_boost.attr);
1151 if (ret)
1152 return ret;
1153 }
1154
1155 return 0;
1156 }
1157
cpufreq_init_policy(struct cpufreq_policy * policy)1158 static int cpufreq_init_policy(struct cpufreq_policy *policy)
1159 {
1160 struct cpufreq_governor *gov = NULL;
1161 unsigned int pol = CPUFREQ_POLICY_UNKNOWN;
1162 int ret;
1163
1164 if (has_target()) {
1165 /* Update policy governor to the one used before hotplug. */
1166 gov = get_governor(policy->last_governor);
1167 if (gov) {
1168 pr_debug("Restoring governor %s for cpu %d\n",
1169 gov->name, policy->cpu);
1170 } else {
1171 gov = get_governor(default_governor);
1172 }
1173
1174 if (!gov) {
1175 gov = cpufreq_default_governor();
1176 __module_get(gov->owner);
1177 }
1178
1179 } else {
1180
1181 /* Use the default policy if there is no last_policy. */
1182 if (policy->last_policy) {
1183 pol = policy->last_policy;
1184 } else {
1185 pol = cpufreq_parse_policy(default_governor);
1186 /*
1187 * In case the default governor is neither "performance"
1188 * nor "powersave", fall back to the initial policy
1189 * value set by the driver.
1190 */
1191 if (pol == CPUFREQ_POLICY_UNKNOWN)
1192 pol = policy->policy;
1193 }
1194 if (pol != CPUFREQ_POLICY_PERFORMANCE &&
1195 pol != CPUFREQ_POLICY_POWERSAVE)
1196 return -ENODATA;
1197 }
1198
1199 ret = cpufreq_set_policy(policy, gov, pol);
1200 if (gov)
1201 module_put(gov->owner);
1202
1203 return ret;
1204 }
1205
cpufreq_add_policy_cpu(struct cpufreq_policy * policy,unsigned int cpu)1206 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1207 {
1208 int ret = 0;
1209
1210 /* Has this CPU been taken care of already? */
1211 if (cpumask_test_cpu(cpu, policy->cpus))
1212 return 0;
1213
1214 down_write(&policy->rwsem);
1215 if (has_target())
1216 cpufreq_stop_governor(policy);
1217
1218 cpumask_set_cpu(cpu, policy->cpus);
1219
1220 if (has_target()) {
1221 ret = cpufreq_start_governor(policy);
1222 if (ret)
1223 pr_err("%s: Failed to start governor\n", __func__);
1224 }
1225 up_write(&policy->rwsem);
1226 return ret;
1227 }
1228
refresh_frequency_limits(struct cpufreq_policy * policy)1229 void refresh_frequency_limits(struct cpufreq_policy *policy)
1230 {
1231 if (!policy_is_inactive(policy)) {
1232 pr_debug("updating policy for CPU %u\n", policy->cpu);
1233
1234 cpufreq_set_policy(policy, policy->governor, policy->policy);
1235 }
1236 }
1237 EXPORT_SYMBOL(refresh_frequency_limits);
1238
handle_update(struct work_struct * work)1239 static void handle_update(struct work_struct *work)
1240 {
1241 struct cpufreq_policy *policy =
1242 container_of(work, struct cpufreq_policy, update);
1243
1244 pr_debug("handle_update for cpu %u called\n", policy->cpu);
1245 down_write(&policy->rwsem);
1246 refresh_frequency_limits(policy);
1247 up_write(&policy->rwsem);
1248 }
1249
cpufreq_notifier_min(struct notifier_block * nb,unsigned long freq,void * data)1250 static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq,
1251 void *data)
1252 {
1253 struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min);
1254
1255 schedule_work(&policy->update);
1256 return 0;
1257 }
1258
cpufreq_notifier_max(struct notifier_block * nb,unsigned long freq,void * data)1259 static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq,
1260 void *data)
1261 {
1262 struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max);
1263
1264 schedule_work(&policy->update);
1265 return 0;
1266 }
1267
cpufreq_policy_put_kobj(struct cpufreq_policy * policy)1268 static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
1269 {
1270 struct kobject *kobj;
1271 struct completion *cmp;
1272
1273 down_write(&policy->rwsem);
1274 cpufreq_stats_free_table(policy);
1275 kobj = &policy->kobj;
1276 cmp = &policy->kobj_unregister;
1277 up_write(&policy->rwsem);
1278 kobject_put(kobj);
1279
1280 /*
1281 * We need to make sure that the underlying kobj is
1282 * actually not referenced anymore by anybody before we
1283 * proceed with unloading.
1284 */
1285 pr_debug("waiting for dropping of refcount\n");
1286 wait_for_completion(cmp);
1287 pr_debug("wait complete\n");
1288 }
1289
cpufreq_policy_alloc(unsigned int cpu)1290 static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1291 {
1292 struct cpufreq_policy *policy;
1293 struct device *dev = get_cpu_device(cpu);
1294 int ret;
1295
1296 if (!dev)
1297 return NULL;
1298
1299 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1300 if (!policy)
1301 return NULL;
1302
1303 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1304 goto err_free_policy;
1305
1306 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1307 goto err_free_cpumask;
1308
1309 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1310 goto err_free_rcpumask;
1311
1312 init_completion(&policy->kobj_unregister);
1313 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1314 cpufreq_global_kobject, "policy%u", cpu);
1315 if (ret) {
1316 dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret);
1317 /*
1318 * The entire policy object will be freed below, but the extra
1319 * memory allocated for the kobject name needs to be freed by
1320 * releasing the kobject.
1321 */
1322 kobject_put(&policy->kobj);
1323 goto err_free_real_cpus;
1324 }
1325
1326 freq_constraints_init(&policy->constraints);
1327
1328 policy->nb_min.notifier_call = cpufreq_notifier_min;
1329 policy->nb_max.notifier_call = cpufreq_notifier_max;
1330
1331 ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN,
1332 &policy->nb_min);
1333 if (ret) {
1334 dev_err(dev, "Failed to register MIN QoS notifier: %d (CPU%u)\n",
1335 ret, cpu);
1336 goto err_kobj_remove;
1337 }
1338
1339 ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX,
1340 &policy->nb_max);
1341 if (ret) {
1342 dev_err(dev, "Failed to register MAX QoS notifier: %d (CPU%u)\n",
1343 ret, cpu);
1344 goto err_min_qos_notifier;
1345 }
1346
1347 INIT_LIST_HEAD(&policy->policy_list);
1348 init_rwsem(&policy->rwsem);
1349 spin_lock_init(&policy->transition_lock);
1350 init_waitqueue_head(&policy->transition_wait);
1351 INIT_WORK(&policy->update, handle_update);
1352
1353 policy->cpu = cpu;
1354 return policy;
1355
1356 err_min_qos_notifier:
1357 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
1358 &policy->nb_min);
1359 err_kobj_remove:
1360 cpufreq_policy_put_kobj(policy);
1361 err_free_real_cpus:
1362 free_cpumask_var(policy->real_cpus);
1363 err_free_rcpumask:
1364 free_cpumask_var(policy->related_cpus);
1365 err_free_cpumask:
1366 free_cpumask_var(policy->cpus);
1367 err_free_policy:
1368 kfree(policy);
1369
1370 return NULL;
1371 }
1372
cpufreq_policy_free(struct cpufreq_policy * policy)1373 static void cpufreq_policy_free(struct cpufreq_policy *policy)
1374 {
1375 unsigned long flags;
1376 int cpu;
1377
1378 /*
1379 * The callers must ensure the policy is inactive by now, to avoid any
1380 * races with show()/store() callbacks.
1381 */
1382 if (unlikely(!policy_is_inactive(policy)))
1383 pr_warn("%s: Freeing active policy\n", __func__);
1384
1385 /* Remove policy from list */
1386 write_lock_irqsave(&cpufreq_driver_lock, flags);
1387 list_del(&policy->policy_list);
1388
1389 for_each_cpu(cpu, policy->related_cpus)
1390 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1391 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1392
1393 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX,
1394 &policy->nb_max);
1395 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
1396 &policy->nb_min);
1397
1398 /* Cancel any pending policy->update work before freeing the policy. */
1399 cancel_work_sync(&policy->update);
1400
1401 if (policy->max_freq_req) {
1402 /*
1403 * Remove max_freq_req after sending CPUFREQ_REMOVE_POLICY
1404 * notification, since CPUFREQ_CREATE_POLICY notification was
1405 * sent after adding max_freq_req earlier.
1406 */
1407 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1408 CPUFREQ_REMOVE_POLICY, policy);
1409 freq_qos_remove_request(policy->max_freq_req);
1410 }
1411
1412 freq_qos_remove_request(policy->min_freq_req);
1413 kfree(policy->min_freq_req);
1414
1415 cpufreq_policy_put_kobj(policy);
1416 free_cpumask_var(policy->real_cpus);
1417 free_cpumask_var(policy->related_cpus);
1418 free_cpumask_var(policy->cpus);
1419 kfree(policy);
1420 }
1421
cpufreq_online(unsigned int cpu)1422 static int cpufreq_online(unsigned int cpu)
1423 {
1424 struct cpufreq_policy *policy;
1425 bool new_policy;
1426 unsigned long flags;
1427 unsigned int j;
1428 int ret;
1429
1430 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
1431
1432 /* Check if this CPU already has a policy to manage it */
1433 policy = per_cpu(cpufreq_cpu_data, cpu);
1434 if (policy) {
1435 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
1436 if (!policy_is_inactive(policy))
1437 return cpufreq_add_policy_cpu(policy, cpu);
1438
1439 /* This is the only online CPU for the policy. Start over. */
1440 new_policy = false;
1441 down_write(&policy->rwsem);
1442 policy->cpu = cpu;
1443 policy->governor = NULL;
1444 } else {
1445 new_policy = true;
1446 policy = cpufreq_policy_alloc(cpu);
1447 if (!policy)
1448 return -ENOMEM;
1449 down_write(&policy->rwsem);
1450 }
1451
1452 if (!new_policy && cpufreq_driver->online) {
1453 /* Recover policy->cpus using related_cpus */
1454 cpumask_copy(policy->cpus, policy->related_cpus);
1455
1456 ret = cpufreq_driver->online(policy);
1457 if (ret) {
1458 pr_debug("%s: %d: initialization failed\n", __func__,
1459 __LINE__);
1460 goto out_exit_policy;
1461 }
1462 } else {
1463 cpumask_copy(policy->cpus, cpumask_of(cpu));
1464
1465 /*
1466 * Call driver. From then on the cpufreq must be able
1467 * to accept all calls to ->verify and ->setpolicy for this CPU.
1468 */
1469 ret = cpufreq_driver->init(policy);
1470 if (ret) {
1471 pr_debug("%s: %d: initialization failed\n", __func__,
1472 __LINE__);
1473 goto out_free_policy;
1474 }
1475
1476 /*
1477 * The initialization has succeeded and the policy is online.
1478 * If there is a problem with its frequency table, take it
1479 * offline and drop it.
1480 */
1481 ret = cpufreq_table_validate_and_sort(policy);
1482 if (ret)
1483 goto out_offline_policy;
1484
1485 /* related_cpus should at least include policy->cpus. */
1486 cpumask_copy(policy->related_cpus, policy->cpus);
1487 }
1488
1489 /*
1490 * affected cpus must always be the one, which are online. We aren't
1491 * managing offline cpus here.
1492 */
1493 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1494
1495 if (new_policy) {
1496 for_each_cpu(j, policy->related_cpus) {
1497 per_cpu(cpufreq_cpu_data, j) = policy;
1498 add_cpu_dev_symlink(policy, j, get_cpu_device(j));
1499 }
1500
1501 policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req),
1502 GFP_KERNEL);
1503 if (!policy->min_freq_req) {
1504 ret = -ENOMEM;
1505 goto out_destroy_policy;
1506 }
1507
1508 ret = freq_qos_add_request(&policy->constraints,
1509 policy->min_freq_req, FREQ_QOS_MIN,
1510 FREQ_QOS_MIN_DEFAULT_VALUE);
1511 if (ret < 0) {
1512 /*
1513 * So we don't call freq_qos_remove_request() for an
1514 * uninitialized request.
1515 */
1516 kfree(policy->min_freq_req);
1517 policy->min_freq_req = NULL;
1518 goto out_destroy_policy;
1519 }
1520
1521 /*
1522 * This must be initialized right here to avoid calling
1523 * freq_qos_remove_request() on uninitialized request in case
1524 * of errors.
1525 */
1526 policy->max_freq_req = policy->min_freq_req + 1;
1527
1528 ret = freq_qos_add_request(&policy->constraints,
1529 policy->max_freq_req, FREQ_QOS_MAX,
1530 FREQ_QOS_MAX_DEFAULT_VALUE);
1531 if (ret < 0) {
1532 policy->max_freq_req = NULL;
1533 goto out_destroy_policy;
1534 }
1535
1536 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1537 CPUFREQ_CREATE_POLICY, policy);
1538 } else {
1539 ret = freq_qos_update_request(policy->max_freq_req, policy->max);
1540 if (ret < 0)
1541 goto out_destroy_policy;
1542 }
1543
1544 if (cpufreq_driver->get && has_target()) {
1545 policy->cur = cpufreq_driver->get(policy->cpu);
1546 if (!policy->cur) {
1547 ret = -EIO;
1548 pr_err("%s: ->get() failed\n", __func__);
1549 goto out_destroy_policy;
1550 }
1551 }
1552
1553 /*
1554 * Sometimes boot loaders set CPU frequency to a value outside of
1555 * frequency table present with cpufreq core. In such cases CPU might be
1556 * unstable if it has to run on that frequency for long duration of time
1557 * and so its better to set it to a frequency which is specified in
1558 * freq-table. This also makes cpufreq stats inconsistent as
1559 * cpufreq-stats would fail to register because current frequency of CPU
1560 * isn't found in freq-table.
1561 *
1562 * Because we don't want this change to effect boot process badly, we go
1563 * for the next freq which is >= policy->cur ('cur' must be set by now,
1564 * otherwise we will end up setting freq to lowest of the table as 'cur'
1565 * is initialized to zero).
1566 *
1567 * We are passing target-freq as "policy->cur - 1" otherwise
1568 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1569 * equal to target-freq.
1570 */
1571 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1572 && has_target()) {
1573 unsigned int old_freq = policy->cur;
1574
1575 /* Are we running at unknown frequency ? */
1576 ret = cpufreq_frequency_table_get_index(policy, old_freq);
1577 if (ret == -EINVAL) {
1578 ret = __cpufreq_driver_target(policy, old_freq - 1,
1579 CPUFREQ_RELATION_L);
1580
1581 /*
1582 * Reaching here after boot in a few seconds may not
1583 * mean that system will remain stable at "unknown"
1584 * frequency for longer duration. Hence, a BUG_ON().
1585 */
1586 BUG_ON(ret);
1587 pr_info("%s: CPU%d: Running at unlisted initial frequency: %u kHz, changing to: %u kHz\n",
1588 __func__, policy->cpu, old_freq, policy->cur);
1589 }
1590 }
1591
1592 if (new_policy) {
1593 ret = cpufreq_add_dev_interface(policy);
1594 if (ret)
1595 goto out_destroy_policy;
1596
1597 cpufreq_stats_create_table(policy);
1598
1599 write_lock_irqsave(&cpufreq_driver_lock, flags);
1600 list_add(&policy->policy_list, &cpufreq_policy_list);
1601 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1602
1603 /*
1604 * Register with the energy model before
1605 * em_rebuild_sched_domains() is called, which will result
1606 * in rebuilding of the sched domains, which should only be done
1607 * once the energy model is properly initialized for the policy
1608 * first.
1609 *
1610 * Also, this should be called before the policy is registered
1611 * with cooling framework.
1612 */
1613 if (cpufreq_driver->register_em)
1614 cpufreq_driver->register_em(policy);
1615 }
1616
1617 ret = cpufreq_init_policy(policy);
1618 if (ret) {
1619 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1620 __func__, cpu, ret);
1621 goto out_destroy_policy;
1622 }
1623
1624 up_write(&policy->rwsem);
1625
1626 kobject_uevent(&policy->kobj, KOBJ_ADD);
1627
1628 /* Callback for handling stuff after policy is ready */
1629 if (cpufreq_driver->ready)
1630 cpufreq_driver->ready(policy);
1631
1632 /* Register cpufreq cooling only for a new policy */
1633 if (new_policy && cpufreq_thermal_control_enabled(cpufreq_driver))
1634 policy->cdev = of_cpufreq_cooling_register(policy);
1635
1636 /* Let the per-policy boost flag mirror the cpufreq_driver boost during init */
1637 if (cpufreq_driver->set_boost && policy->boost_supported &&
1638 policy->boost_enabled != cpufreq_boost_enabled()) {
1639 policy->boost_enabled = cpufreq_boost_enabled();
1640 ret = cpufreq_driver->set_boost(policy, policy->boost_enabled);
1641 if (ret) {
1642 /* If the set_boost fails, the online operation is not affected */
1643 pr_info("%s: CPU%d: Cannot %s BOOST\n", __func__, policy->cpu,
1644 str_enable_disable(policy->boost_enabled));
1645 policy->boost_enabled = !policy->boost_enabled;
1646 }
1647 }
1648
1649 pr_debug("initialization complete\n");
1650
1651 return 0;
1652
1653 out_destroy_policy:
1654 for_each_cpu(j, policy->real_cpus)
1655 remove_cpu_dev_symlink(policy, j, get_cpu_device(j));
1656
1657 out_offline_policy:
1658 if (cpufreq_driver->offline)
1659 cpufreq_driver->offline(policy);
1660
1661 out_exit_policy:
1662 if (cpufreq_driver->exit)
1663 cpufreq_driver->exit(policy);
1664
1665 out_free_policy:
1666 cpumask_clear(policy->cpus);
1667 up_write(&policy->rwsem);
1668
1669 cpufreq_policy_free(policy);
1670 return ret;
1671 }
1672
1673 /**
1674 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1675 * @dev: CPU device.
1676 * @sif: Subsystem interface structure pointer (not used)
1677 */
cpufreq_add_dev(struct device * dev,struct subsys_interface * sif)1678 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1679 {
1680 struct cpufreq_policy *policy;
1681 unsigned cpu = dev->id;
1682 int ret;
1683
1684 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1685
1686 if (cpu_online(cpu)) {
1687 ret = cpufreq_online(cpu);
1688 if (ret)
1689 return ret;
1690 }
1691
1692 /* Create sysfs link on CPU registration */
1693 policy = per_cpu(cpufreq_cpu_data, cpu);
1694 if (policy)
1695 add_cpu_dev_symlink(policy, cpu, dev);
1696
1697 return 0;
1698 }
1699
__cpufreq_offline(unsigned int cpu,struct cpufreq_policy * policy)1700 static void __cpufreq_offline(unsigned int cpu, struct cpufreq_policy *policy)
1701 {
1702 int ret;
1703
1704 if (has_target())
1705 cpufreq_stop_governor(policy);
1706
1707 cpumask_clear_cpu(cpu, policy->cpus);
1708
1709 if (!policy_is_inactive(policy)) {
1710 /* Nominate a new CPU if necessary. */
1711 if (cpu == policy->cpu)
1712 policy->cpu = cpumask_any(policy->cpus);
1713
1714 /* Start the governor again for the active policy. */
1715 if (has_target()) {
1716 ret = cpufreq_start_governor(policy);
1717 if (ret)
1718 pr_err("%s: Failed to start governor\n", __func__);
1719 }
1720
1721 return;
1722 }
1723
1724 if (has_target())
1725 strscpy(policy->last_governor, policy->governor->name,
1726 CPUFREQ_NAME_LEN);
1727 else
1728 policy->last_policy = policy->policy;
1729
1730 if (has_target())
1731 cpufreq_exit_governor(policy);
1732
1733 /*
1734 * Perform the ->offline() during light-weight tear-down, as
1735 * that allows fast recovery when the CPU comes back.
1736 */
1737 if (cpufreq_driver->offline) {
1738 cpufreq_driver->offline(policy);
1739 return;
1740 }
1741
1742 if (cpufreq_driver->exit)
1743 cpufreq_driver->exit(policy);
1744
1745 policy->freq_table = NULL;
1746 }
1747
cpufreq_offline(unsigned int cpu)1748 static int cpufreq_offline(unsigned int cpu)
1749 {
1750 struct cpufreq_policy *policy;
1751
1752 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1753
1754 policy = cpufreq_cpu_get_raw(cpu);
1755 if (!policy) {
1756 pr_debug("%s: No cpu_data found\n", __func__);
1757 return 0;
1758 }
1759
1760 down_write(&policy->rwsem);
1761
1762 __cpufreq_offline(cpu, policy);
1763
1764 up_write(&policy->rwsem);
1765 return 0;
1766 }
1767
1768 /*
1769 * cpufreq_remove_dev - remove a CPU device
1770 *
1771 * Removes the cpufreq interface for a CPU device.
1772 */
cpufreq_remove_dev(struct device * dev,struct subsys_interface * sif)1773 static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1774 {
1775 unsigned int cpu = dev->id;
1776 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1777
1778 if (!policy)
1779 return;
1780
1781 down_write(&policy->rwsem);
1782
1783 if (cpu_online(cpu))
1784 __cpufreq_offline(cpu, policy);
1785
1786 remove_cpu_dev_symlink(policy, cpu, dev);
1787
1788 if (!cpumask_empty(policy->real_cpus)) {
1789 up_write(&policy->rwsem);
1790 return;
1791 }
1792
1793 /*
1794 * Unregister cpufreq cooling once all the CPUs of the policy are
1795 * removed.
1796 */
1797 if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
1798 cpufreq_cooling_unregister(policy->cdev);
1799 policy->cdev = NULL;
1800 }
1801
1802 /* We did light-weight exit earlier, do full tear down now */
1803 if (cpufreq_driver->offline && cpufreq_driver->exit)
1804 cpufreq_driver->exit(policy);
1805
1806 up_write(&policy->rwsem);
1807
1808 cpufreq_policy_free(policy);
1809 }
1810
1811 /**
1812 * cpufreq_out_of_sync - Fix up actual and saved CPU frequency difference.
1813 * @policy: Policy managing CPUs.
1814 * @new_freq: New CPU frequency.
1815 *
1816 * Adjust to the current frequency first and clean up later by either calling
1817 * cpufreq_update_policy(), or scheduling handle_update().
1818 */
cpufreq_out_of_sync(struct cpufreq_policy * policy,unsigned int new_freq)1819 static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1820 unsigned int new_freq)
1821 {
1822 struct cpufreq_freqs freqs;
1823
1824 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1825 policy->cur, new_freq);
1826
1827 freqs.old = policy->cur;
1828 freqs.new = new_freq;
1829
1830 cpufreq_freq_transition_begin(policy, &freqs);
1831 cpufreq_freq_transition_end(policy, &freqs, 0);
1832 }
1833
cpufreq_verify_current_freq(struct cpufreq_policy * policy,bool update)1834 static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update)
1835 {
1836 unsigned int new_freq;
1837
1838 new_freq = cpufreq_driver->get(policy->cpu);
1839 if (!new_freq)
1840 return 0;
1841
1842 /*
1843 * If fast frequency switching is used with the given policy, the check
1844 * against policy->cur is pointless, so skip it in that case.
1845 */
1846 if (policy->fast_switch_enabled || !has_target())
1847 return new_freq;
1848
1849 if (policy->cur != new_freq) {
1850 /*
1851 * For some platforms, the frequency returned by hardware may be
1852 * slightly different from what is provided in the frequency
1853 * table, for example hardware may return 499 MHz instead of 500
1854 * MHz. In such cases it is better to avoid getting into
1855 * unnecessary frequency updates.
1856 */
1857 if (abs(policy->cur - new_freq) < KHZ_PER_MHZ)
1858 return policy->cur;
1859
1860 cpufreq_out_of_sync(policy, new_freq);
1861 if (update)
1862 schedule_work(&policy->update);
1863 }
1864
1865 return new_freq;
1866 }
1867
1868 /**
1869 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1870 * @cpu: CPU number
1871 *
1872 * This is the last known freq, without actually getting it from the driver.
1873 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1874 */
cpufreq_quick_get(unsigned int cpu)1875 unsigned int cpufreq_quick_get(unsigned int cpu)
1876 {
1877 struct cpufreq_policy *policy;
1878 unsigned int ret_freq = 0;
1879 unsigned long flags;
1880
1881 read_lock_irqsave(&cpufreq_driver_lock, flags);
1882
1883 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1884 ret_freq = cpufreq_driver->get(cpu);
1885 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1886 return ret_freq;
1887 }
1888
1889 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1890
1891 policy = cpufreq_cpu_get(cpu);
1892 if (policy) {
1893 ret_freq = policy->cur;
1894 cpufreq_cpu_put(policy);
1895 }
1896
1897 return ret_freq;
1898 }
1899 EXPORT_SYMBOL(cpufreq_quick_get);
1900
1901 /**
1902 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1903 * @cpu: CPU number
1904 *
1905 * Just return the max possible frequency for a given CPU.
1906 */
cpufreq_quick_get_max(unsigned int cpu)1907 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1908 {
1909 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1910 unsigned int ret_freq = 0;
1911
1912 if (policy) {
1913 ret_freq = policy->max;
1914 cpufreq_cpu_put(policy);
1915 }
1916
1917 return ret_freq;
1918 }
1919 EXPORT_SYMBOL(cpufreq_quick_get_max);
1920
1921 /**
1922 * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU
1923 * @cpu: CPU number
1924 *
1925 * The default return value is the max_freq field of cpuinfo.
1926 */
cpufreq_get_hw_max_freq(unsigned int cpu)1927 __weak unsigned int cpufreq_get_hw_max_freq(unsigned int cpu)
1928 {
1929 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1930 unsigned int ret_freq = 0;
1931
1932 if (policy) {
1933 ret_freq = policy->cpuinfo.max_freq;
1934 cpufreq_cpu_put(policy);
1935 }
1936
1937 return ret_freq;
1938 }
1939 EXPORT_SYMBOL(cpufreq_get_hw_max_freq);
1940
__cpufreq_get(struct cpufreq_policy * policy)1941 static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
1942 {
1943 if (unlikely(policy_is_inactive(policy)))
1944 return 0;
1945
1946 return cpufreq_verify_current_freq(policy, true);
1947 }
1948
1949 /**
1950 * cpufreq_get - get the current CPU frequency (in kHz)
1951 * @cpu: CPU number
1952 *
1953 * Get the CPU current (static) CPU frequency
1954 */
cpufreq_get(unsigned int cpu)1955 unsigned int cpufreq_get(unsigned int cpu)
1956 {
1957 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1958 unsigned int ret_freq = 0;
1959
1960 if (policy) {
1961 down_read(&policy->rwsem);
1962 if (cpufreq_driver->get)
1963 ret_freq = __cpufreq_get(policy);
1964 up_read(&policy->rwsem);
1965
1966 cpufreq_cpu_put(policy);
1967 }
1968
1969 return ret_freq;
1970 }
1971 EXPORT_SYMBOL(cpufreq_get);
1972
1973 static struct subsys_interface cpufreq_interface = {
1974 .name = "cpufreq",
1975 .subsys = &cpu_subsys,
1976 .add_dev = cpufreq_add_dev,
1977 .remove_dev = cpufreq_remove_dev,
1978 };
1979
1980 /*
1981 * In case platform wants some specific frequency to be configured
1982 * during suspend..
1983 */
cpufreq_generic_suspend(struct cpufreq_policy * policy)1984 int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1985 {
1986 int ret;
1987
1988 if (!policy->suspend_freq) {
1989 pr_debug("%s: suspend_freq not defined\n", __func__);
1990 return 0;
1991 }
1992
1993 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1994 policy->suspend_freq);
1995
1996 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1997 CPUFREQ_RELATION_H);
1998 if (ret)
1999 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
2000 __func__, policy->suspend_freq, ret);
2001
2002 return ret;
2003 }
2004 EXPORT_SYMBOL(cpufreq_generic_suspend);
2005
2006 /**
2007 * cpufreq_suspend() - Suspend CPUFreq governors.
2008 *
2009 * Called during system wide Suspend/Hibernate cycles for suspending governors
2010 * as some platforms can't change frequency after this point in suspend cycle.
2011 * Because some of the devices (like: i2c, regulators, etc) they use for
2012 * changing frequency are suspended quickly after this point.
2013 */
cpufreq_suspend(void)2014 void cpufreq_suspend(void)
2015 {
2016 struct cpufreq_policy *policy;
2017
2018 if (!cpufreq_driver)
2019 return;
2020
2021 if (!has_target() && !cpufreq_driver->suspend)
2022 goto suspend;
2023
2024 pr_debug("%s: Suspending Governors\n", __func__);
2025
2026 for_each_active_policy(policy) {
2027 if (has_target()) {
2028 down_write(&policy->rwsem);
2029 cpufreq_stop_governor(policy);
2030 up_write(&policy->rwsem);
2031 }
2032
2033 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
2034 pr_err("%s: Failed to suspend driver: %s\n", __func__,
2035 cpufreq_driver->name);
2036 }
2037
2038 suspend:
2039 cpufreq_suspended = true;
2040 }
2041
2042 /**
2043 * cpufreq_resume() - Resume CPUFreq governors.
2044 *
2045 * Called during system wide Suspend/Hibernate cycle for resuming governors that
2046 * are suspended with cpufreq_suspend().
2047 */
cpufreq_resume(void)2048 void cpufreq_resume(void)
2049 {
2050 struct cpufreq_policy *policy;
2051 int ret;
2052
2053 if (!cpufreq_driver)
2054 return;
2055
2056 if (unlikely(!cpufreq_suspended))
2057 return;
2058
2059 cpufreq_suspended = false;
2060
2061 if (!has_target() && !cpufreq_driver->resume)
2062 return;
2063
2064 pr_debug("%s: Resuming Governors\n", __func__);
2065
2066 for_each_active_policy(policy) {
2067 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
2068 pr_err("%s: Failed to resume driver: %s\n", __func__,
2069 cpufreq_driver->name);
2070 } else if (has_target()) {
2071 down_write(&policy->rwsem);
2072 ret = cpufreq_start_governor(policy);
2073 up_write(&policy->rwsem);
2074
2075 if (ret)
2076 pr_err("%s: Failed to start governor for CPU%u's policy\n",
2077 __func__, policy->cpu);
2078 }
2079 }
2080 }
2081
2082 /**
2083 * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones.
2084 * @flags: Flags to test against the current cpufreq driver's flags.
2085 *
2086 * Assumes that the driver is there, so callers must ensure that this is the
2087 * case.
2088 */
cpufreq_driver_test_flags(u16 flags)2089 bool cpufreq_driver_test_flags(u16 flags)
2090 {
2091 return !!(cpufreq_driver->flags & flags);
2092 }
2093
2094 /**
2095 * cpufreq_get_current_driver - Return the current driver's name.
2096 *
2097 * Return the name string of the currently registered cpufreq driver or NULL if
2098 * none.
2099 */
cpufreq_get_current_driver(void)2100 const char *cpufreq_get_current_driver(void)
2101 {
2102 if (cpufreq_driver)
2103 return cpufreq_driver->name;
2104
2105 return NULL;
2106 }
2107 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
2108
2109 /**
2110 * cpufreq_get_driver_data - Return current driver data.
2111 *
2112 * Return the private data of the currently registered cpufreq driver, or NULL
2113 * if no cpufreq driver has been registered.
2114 */
cpufreq_get_driver_data(void)2115 void *cpufreq_get_driver_data(void)
2116 {
2117 if (cpufreq_driver)
2118 return cpufreq_driver->driver_data;
2119
2120 return NULL;
2121 }
2122 EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
2123
2124 /*********************************************************************
2125 * NOTIFIER LISTS INTERFACE *
2126 *********************************************************************/
2127
2128 /**
2129 * cpufreq_register_notifier - Register a notifier with cpufreq.
2130 * @nb: notifier function to register.
2131 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER.
2132 *
2133 * Add a notifier to one of two lists: either a list of notifiers that run on
2134 * clock rate changes (once before and once after every transition), or a list
2135 * of notifiers that ron on cpufreq policy changes.
2136 *
2137 * This function may sleep and it has the same return values as
2138 * blocking_notifier_chain_register().
2139 */
cpufreq_register_notifier(struct notifier_block * nb,unsigned int list)2140 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
2141 {
2142 int ret;
2143
2144 if (cpufreq_disabled())
2145 return -EINVAL;
2146
2147 switch (list) {
2148 case CPUFREQ_TRANSITION_NOTIFIER:
2149 mutex_lock(&cpufreq_fast_switch_lock);
2150
2151 if (cpufreq_fast_switch_count > 0) {
2152 mutex_unlock(&cpufreq_fast_switch_lock);
2153 return -EBUSY;
2154 }
2155 ret = srcu_notifier_chain_register(
2156 &cpufreq_transition_notifier_list, nb);
2157 if (!ret)
2158 cpufreq_fast_switch_count--;
2159
2160 mutex_unlock(&cpufreq_fast_switch_lock);
2161 break;
2162 case CPUFREQ_POLICY_NOTIFIER:
2163 ret = blocking_notifier_chain_register(
2164 &cpufreq_policy_notifier_list, nb);
2165 break;
2166 default:
2167 ret = -EINVAL;
2168 }
2169
2170 return ret;
2171 }
2172 EXPORT_SYMBOL(cpufreq_register_notifier);
2173
2174 /**
2175 * cpufreq_unregister_notifier - Unregister a notifier from cpufreq.
2176 * @nb: notifier block to be unregistered.
2177 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER.
2178 *
2179 * Remove a notifier from one of the cpufreq notifier lists.
2180 *
2181 * This function may sleep and it has the same return values as
2182 * blocking_notifier_chain_unregister().
2183 */
cpufreq_unregister_notifier(struct notifier_block * nb,unsigned int list)2184 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
2185 {
2186 int ret;
2187
2188 if (cpufreq_disabled())
2189 return -EINVAL;
2190
2191 switch (list) {
2192 case CPUFREQ_TRANSITION_NOTIFIER:
2193 mutex_lock(&cpufreq_fast_switch_lock);
2194
2195 ret = srcu_notifier_chain_unregister(
2196 &cpufreq_transition_notifier_list, nb);
2197 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
2198 cpufreq_fast_switch_count++;
2199
2200 mutex_unlock(&cpufreq_fast_switch_lock);
2201 break;
2202 case CPUFREQ_POLICY_NOTIFIER:
2203 ret = blocking_notifier_chain_unregister(
2204 &cpufreq_policy_notifier_list, nb);
2205 break;
2206 default:
2207 ret = -EINVAL;
2208 }
2209
2210 return ret;
2211 }
2212 EXPORT_SYMBOL(cpufreq_unregister_notifier);
2213
2214
2215 /*********************************************************************
2216 * GOVERNORS *
2217 *********************************************************************/
2218
2219 /**
2220 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
2221 * @policy: cpufreq policy to switch the frequency for.
2222 * @target_freq: New frequency to set (may be approximate).
2223 *
2224 * Carry out a fast frequency switch without sleeping.
2225 *
2226 * The driver's ->fast_switch() callback invoked by this function must be
2227 * suitable for being called from within RCU-sched read-side critical sections
2228 * and it is expected to select the minimum available frequency greater than or
2229 * equal to @target_freq (CPUFREQ_RELATION_L).
2230 *
2231 * This function must not be called if policy->fast_switch_enabled is unset.
2232 *
2233 * Governors calling this function must guarantee that it will never be invoked
2234 * twice in parallel for the same policy and that it will never be called in
2235 * parallel with either ->target() or ->target_index() for the same policy.
2236 *
2237 * Returns the actual frequency set for the CPU.
2238 *
2239 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
2240 * error condition, the hardware configuration must be preserved.
2241 */
cpufreq_driver_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)2242 unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
2243 unsigned int target_freq)
2244 {
2245 unsigned int freq;
2246 int cpu;
2247
2248 target_freq = clamp_val(target_freq, policy->min, policy->max);
2249 freq = cpufreq_driver->fast_switch(policy, target_freq);
2250
2251 if (!freq)
2252 return 0;
2253
2254 policy->cur = freq;
2255 arch_set_freq_scale(policy->related_cpus, freq,
2256 arch_scale_freq_ref(policy->cpu));
2257 cpufreq_stats_record_transition(policy, freq);
2258
2259 if (trace_cpu_frequency_enabled()) {
2260 for_each_cpu(cpu, policy->cpus)
2261 trace_cpu_frequency(freq, cpu);
2262 }
2263
2264 return freq;
2265 }
2266 EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
2267
2268 /**
2269 * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go.
2270 * @cpu: Target CPU.
2271 * @min_perf: Minimum (required) performance level (units of @capacity).
2272 * @target_perf: Target (desired) performance level (units of @capacity).
2273 * @capacity: Capacity of the target CPU.
2274 *
2275 * Carry out a fast performance level switch of @cpu without sleeping.
2276 *
2277 * The driver's ->adjust_perf() callback invoked by this function must be
2278 * suitable for being called from within RCU-sched read-side critical sections
2279 * and it is expected to select a suitable performance level equal to or above
2280 * @min_perf and preferably equal to or below @target_perf.
2281 *
2282 * This function must not be called if policy->fast_switch_enabled is unset.
2283 *
2284 * Governors calling this function must guarantee that it will never be invoked
2285 * twice in parallel for the same CPU and that it will never be called in
2286 * parallel with either ->target() or ->target_index() or ->fast_switch() for
2287 * the same CPU.
2288 */
cpufreq_driver_adjust_perf(unsigned int cpu,unsigned long min_perf,unsigned long target_perf,unsigned long capacity)2289 void cpufreq_driver_adjust_perf(unsigned int cpu,
2290 unsigned long min_perf,
2291 unsigned long target_perf,
2292 unsigned long capacity)
2293 {
2294 cpufreq_driver->adjust_perf(cpu, min_perf, target_perf, capacity);
2295 }
2296
2297 /**
2298 * cpufreq_driver_has_adjust_perf - Check "direct fast switch" callback.
2299 *
2300 * Return 'true' if the ->adjust_perf callback is present for the
2301 * current driver or 'false' otherwise.
2302 */
cpufreq_driver_has_adjust_perf(void)2303 bool cpufreq_driver_has_adjust_perf(void)
2304 {
2305 return !!cpufreq_driver->adjust_perf;
2306 }
2307
2308 /* Must set freqs->new to intermediate frequency */
__target_intermediate(struct cpufreq_policy * policy,struct cpufreq_freqs * freqs,int index)2309 static int __target_intermediate(struct cpufreq_policy *policy,
2310 struct cpufreq_freqs *freqs, int index)
2311 {
2312 int ret;
2313
2314 freqs->new = cpufreq_driver->get_intermediate(policy, index);
2315
2316 /* We don't need to switch to intermediate freq */
2317 if (!freqs->new)
2318 return 0;
2319
2320 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
2321 __func__, policy->cpu, freqs->old, freqs->new);
2322
2323 cpufreq_freq_transition_begin(policy, freqs);
2324 ret = cpufreq_driver->target_intermediate(policy, index);
2325 cpufreq_freq_transition_end(policy, freqs, ret);
2326
2327 if (ret)
2328 pr_err("%s: Failed to change to intermediate frequency: %d\n",
2329 __func__, ret);
2330
2331 return ret;
2332 }
2333
__target_index(struct cpufreq_policy * policy,int index)2334 static int __target_index(struct cpufreq_policy *policy, int index)
2335 {
2336 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
2337 unsigned int restore_freq, intermediate_freq = 0;
2338 unsigned int newfreq = policy->freq_table[index].frequency;
2339 int retval = -EINVAL;
2340 bool notify;
2341
2342 if (newfreq == policy->cur)
2343 return 0;
2344
2345 /* Save last value to restore later on errors */
2346 restore_freq = policy->cur;
2347
2348 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
2349 if (notify) {
2350 /* Handle switching to intermediate frequency */
2351 if (cpufreq_driver->get_intermediate) {
2352 retval = __target_intermediate(policy, &freqs, index);
2353 if (retval)
2354 return retval;
2355
2356 intermediate_freq = freqs.new;
2357 /* Set old freq to intermediate */
2358 if (intermediate_freq)
2359 freqs.old = freqs.new;
2360 }
2361
2362 freqs.new = newfreq;
2363 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
2364 __func__, policy->cpu, freqs.old, freqs.new);
2365
2366 cpufreq_freq_transition_begin(policy, &freqs);
2367 }
2368
2369 retval = cpufreq_driver->target_index(policy, index);
2370 if (retval)
2371 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
2372 retval);
2373
2374 if (notify) {
2375 cpufreq_freq_transition_end(policy, &freqs, retval);
2376
2377 /*
2378 * Failed after setting to intermediate freq? Driver should have
2379 * reverted back to initial frequency and so should we. Check
2380 * here for intermediate_freq instead of get_intermediate, in
2381 * case we haven't switched to intermediate freq at all.
2382 */
2383 if (unlikely(retval && intermediate_freq)) {
2384 freqs.old = intermediate_freq;
2385 freqs.new = restore_freq;
2386 cpufreq_freq_transition_begin(policy, &freqs);
2387 cpufreq_freq_transition_end(policy, &freqs, 0);
2388 }
2389 }
2390
2391 return retval;
2392 }
2393
__cpufreq_driver_target(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int relation)2394 int __cpufreq_driver_target(struct cpufreq_policy *policy,
2395 unsigned int target_freq,
2396 unsigned int relation)
2397 {
2398 unsigned int old_target_freq = target_freq;
2399
2400 if (cpufreq_disabled())
2401 return -ENODEV;
2402
2403 target_freq = __resolve_freq(policy, target_freq, policy->min,
2404 policy->max, relation);
2405
2406 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
2407 policy->cpu, target_freq, relation, old_target_freq);
2408
2409 /*
2410 * This might look like a redundant call as we are checking it again
2411 * after finding index. But it is left intentionally for cases where
2412 * exactly same freq is called again and so we can save on few function
2413 * calls.
2414 */
2415 if (target_freq == policy->cur &&
2416 !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS))
2417 return 0;
2418
2419 if (cpufreq_driver->target) {
2420 /*
2421 * If the driver hasn't setup a single inefficient frequency,
2422 * it's unlikely it knows how to decode CPUFREQ_RELATION_E.
2423 */
2424 if (!policy->efficiencies_available)
2425 relation &= ~CPUFREQ_RELATION_E;
2426
2427 return cpufreq_driver->target(policy, target_freq, relation);
2428 }
2429
2430 if (!cpufreq_driver->target_index)
2431 return -EINVAL;
2432
2433 return __target_index(policy, policy->cached_resolved_idx);
2434 }
2435 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2436
cpufreq_driver_target(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int relation)2437 int cpufreq_driver_target(struct cpufreq_policy *policy,
2438 unsigned int target_freq,
2439 unsigned int relation)
2440 {
2441 int ret;
2442
2443 down_write(&policy->rwsem);
2444
2445 ret = __cpufreq_driver_target(policy, target_freq, relation);
2446
2447 up_write(&policy->rwsem);
2448
2449 return ret;
2450 }
2451 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2452
cpufreq_fallback_governor(void)2453 __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2454 {
2455 return NULL;
2456 }
2457
cpufreq_init_governor(struct cpufreq_policy * policy)2458 static int cpufreq_init_governor(struct cpufreq_policy *policy)
2459 {
2460 int ret;
2461
2462 /* Don't start any governor operations if we are entering suspend */
2463 if (cpufreq_suspended)
2464 return 0;
2465 /*
2466 * Governor might not be initiated here if ACPI _PPC changed
2467 * notification happened, so check it.
2468 */
2469 if (!policy->governor)
2470 return -EINVAL;
2471
2472 /* Platform doesn't want dynamic frequency switching ? */
2473 if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING &&
2474 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
2475 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2476
2477 if (gov) {
2478 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
2479 policy->governor->name, gov->name);
2480 policy->governor = gov;
2481 } else {
2482 return -EINVAL;
2483 }
2484 }
2485
2486 if (!try_module_get(policy->governor->owner))
2487 return -EINVAL;
2488
2489 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2490
2491 if (policy->governor->init) {
2492 ret = policy->governor->init(policy);
2493 if (ret) {
2494 module_put(policy->governor->owner);
2495 return ret;
2496 }
2497 }
2498
2499 policy->strict_target = !!(policy->governor->flags & CPUFREQ_GOV_STRICT_TARGET);
2500
2501 return 0;
2502 }
2503
cpufreq_exit_governor(struct cpufreq_policy * policy)2504 static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2505 {
2506 if (cpufreq_suspended || !policy->governor)
2507 return;
2508
2509 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2510
2511 if (policy->governor->exit)
2512 policy->governor->exit(policy);
2513
2514 module_put(policy->governor->owner);
2515 }
2516
cpufreq_start_governor(struct cpufreq_policy * policy)2517 int cpufreq_start_governor(struct cpufreq_policy *policy)
2518 {
2519 int ret;
2520
2521 if (cpufreq_suspended)
2522 return 0;
2523
2524 if (!policy->governor)
2525 return -EINVAL;
2526
2527 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2528
2529 if (cpufreq_driver->get)
2530 cpufreq_verify_current_freq(policy, false);
2531
2532 if (policy->governor->start) {
2533 ret = policy->governor->start(policy);
2534 if (ret)
2535 return ret;
2536 }
2537
2538 if (policy->governor->limits)
2539 policy->governor->limits(policy);
2540
2541 return 0;
2542 }
2543
cpufreq_stop_governor(struct cpufreq_policy * policy)2544 void cpufreq_stop_governor(struct cpufreq_policy *policy)
2545 {
2546 if (cpufreq_suspended || !policy->governor)
2547 return;
2548
2549 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2550
2551 if (policy->governor->stop)
2552 policy->governor->stop(policy);
2553 }
2554
cpufreq_governor_limits(struct cpufreq_policy * policy)2555 static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2556 {
2557 if (cpufreq_suspended || !policy->governor)
2558 return;
2559
2560 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2561
2562 if (policy->governor->limits)
2563 policy->governor->limits(policy);
2564 }
2565
cpufreq_register_governor(struct cpufreq_governor * governor)2566 int cpufreq_register_governor(struct cpufreq_governor *governor)
2567 {
2568 int err;
2569
2570 if (!governor)
2571 return -EINVAL;
2572
2573 if (cpufreq_disabled())
2574 return -ENODEV;
2575
2576 mutex_lock(&cpufreq_governor_mutex);
2577
2578 err = -EBUSY;
2579 if (!find_governor(governor->name)) {
2580 err = 0;
2581 list_add(&governor->governor_list, &cpufreq_governor_list);
2582 }
2583
2584 mutex_unlock(&cpufreq_governor_mutex);
2585 return err;
2586 }
2587 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2588
cpufreq_unregister_governor(struct cpufreq_governor * governor)2589 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2590 {
2591 struct cpufreq_policy *policy;
2592 unsigned long flags;
2593
2594 if (!governor)
2595 return;
2596
2597 if (cpufreq_disabled())
2598 return;
2599
2600 /* clear last_governor for all inactive policies */
2601 read_lock_irqsave(&cpufreq_driver_lock, flags);
2602 for_each_inactive_policy(policy) {
2603 if (!strcmp(policy->last_governor, governor->name)) {
2604 policy->governor = NULL;
2605 strcpy(policy->last_governor, "\0");
2606 }
2607 }
2608 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2609
2610 mutex_lock(&cpufreq_governor_mutex);
2611 list_del(&governor->governor_list);
2612 mutex_unlock(&cpufreq_governor_mutex);
2613 }
2614 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2615
2616
2617 /*********************************************************************
2618 * POLICY INTERFACE *
2619 *********************************************************************/
2620
2621 /**
2622 * cpufreq_get_policy - get the current cpufreq_policy
2623 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2624 * is written
2625 * @cpu: CPU to find the policy for
2626 *
2627 * Reads the current cpufreq policy.
2628 */
cpufreq_get_policy(struct cpufreq_policy * policy,unsigned int cpu)2629 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2630 {
2631 struct cpufreq_policy *cpu_policy;
2632 if (!policy)
2633 return -EINVAL;
2634
2635 cpu_policy = cpufreq_cpu_get(cpu);
2636 if (!cpu_policy)
2637 return -EINVAL;
2638
2639 memcpy(policy, cpu_policy, sizeof(*policy));
2640
2641 cpufreq_cpu_put(cpu_policy);
2642 return 0;
2643 }
2644 EXPORT_SYMBOL(cpufreq_get_policy);
2645
2646 DEFINE_PER_CPU(unsigned long, cpufreq_pressure);
2647
2648 /**
2649 * cpufreq_update_pressure() - Update cpufreq pressure for CPUs
2650 * @policy: cpufreq policy of the CPUs.
2651 *
2652 * Update the value of cpufreq pressure for all @cpus in the policy.
2653 */
cpufreq_update_pressure(struct cpufreq_policy * policy)2654 static void cpufreq_update_pressure(struct cpufreq_policy *policy)
2655 {
2656 unsigned long max_capacity, capped_freq, pressure;
2657 u32 max_freq;
2658 int cpu;
2659
2660 cpu = cpumask_first(policy->related_cpus);
2661 max_freq = arch_scale_freq_ref(cpu);
2662 capped_freq = policy->max;
2663
2664 /*
2665 * Handle properly the boost frequencies, which should simply clean
2666 * the cpufreq pressure value.
2667 */
2668 if (max_freq <= capped_freq) {
2669 pressure = 0;
2670 } else {
2671 max_capacity = arch_scale_cpu_capacity(cpu);
2672 pressure = max_capacity -
2673 mult_frac(max_capacity, capped_freq, max_freq);
2674 }
2675
2676 for_each_cpu(cpu, policy->related_cpus)
2677 WRITE_ONCE(per_cpu(cpufreq_pressure, cpu), pressure);
2678 }
2679
2680 /**
2681 * cpufreq_set_policy - Modify cpufreq policy parameters.
2682 * @policy: Policy object to modify.
2683 * @new_gov: Policy governor pointer.
2684 * @new_pol: Policy value (for drivers with built-in governors).
2685 *
2686 * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency
2687 * limits to be set for the policy, update @policy with the verified limits
2688 * values and either invoke the driver's ->setpolicy() callback (if present) or
2689 * carry out a governor update for @policy. That is, run the current governor's
2690 * ->limits() callback (if @new_gov points to the same object as the one in
2691 * @policy) or replace the governor for @policy with @new_gov.
2692 *
2693 * The cpuinfo part of @policy is not updated by this function.
2694 */
cpufreq_set_policy(struct cpufreq_policy * policy,struct cpufreq_governor * new_gov,unsigned int new_pol)2695 static int cpufreq_set_policy(struct cpufreq_policy *policy,
2696 struct cpufreq_governor *new_gov,
2697 unsigned int new_pol)
2698 {
2699 struct cpufreq_policy_data new_data;
2700 struct cpufreq_governor *old_gov;
2701 int ret;
2702
2703 memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2704 new_data.freq_table = policy->freq_table;
2705 new_data.cpu = policy->cpu;
2706 /*
2707 * PM QoS framework collects all the requests from users and provide us
2708 * the final aggregated value here.
2709 */
2710 new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
2711 new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
2712
2713 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2714 new_data.cpu, new_data.min, new_data.max);
2715
2716 /*
2717 * Verify that the CPU speed can be set within these limits and make sure
2718 * that min <= max.
2719 */
2720 ret = cpufreq_driver->verify(&new_data);
2721 if (ret)
2722 return ret;
2723
2724 /*
2725 * Resolve policy min/max to available frequencies. It ensures
2726 * no frequency resolution will neither overshoot the requested maximum
2727 * nor undershoot the requested minimum.
2728 *
2729 * Avoid storing intermediate values in policy->max or policy->min and
2730 * compiler optimizations around them because they may be accessed
2731 * concurrently by cpufreq_driver_resolve_freq() during the update.
2732 */
2733 WRITE_ONCE(policy->max, __resolve_freq(policy, new_data.max,
2734 new_data.min, new_data.max,
2735 CPUFREQ_RELATION_H));
2736 new_data.min = __resolve_freq(policy, new_data.min, new_data.min,
2737 new_data.max, CPUFREQ_RELATION_L);
2738 WRITE_ONCE(policy->min, new_data.min > policy->max ? policy->max : new_data.min);
2739
2740 trace_cpu_frequency_limits(policy);
2741
2742 cpufreq_update_pressure(policy);
2743
2744 policy->cached_target_freq = UINT_MAX;
2745
2746 pr_debug("new min and max freqs are %u - %u kHz\n",
2747 policy->min, policy->max);
2748
2749 if (cpufreq_driver->setpolicy) {
2750 policy->policy = new_pol;
2751 pr_debug("setting range\n");
2752 return cpufreq_driver->setpolicy(policy);
2753 }
2754
2755 if (new_gov == policy->governor) {
2756 pr_debug("governor limits update\n");
2757 cpufreq_governor_limits(policy);
2758 return 0;
2759 }
2760
2761 pr_debug("governor switch\n");
2762
2763 /* save old, working values */
2764 old_gov = policy->governor;
2765 /* end old governor */
2766 if (old_gov) {
2767 cpufreq_stop_governor(policy);
2768 cpufreq_exit_governor(policy);
2769 }
2770
2771 /* start new governor */
2772 policy->governor = new_gov;
2773 ret = cpufreq_init_governor(policy);
2774 if (!ret) {
2775 ret = cpufreq_start_governor(policy);
2776 if (!ret) {
2777 pr_debug("governor change\n");
2778 return 0;
2779 }
2780 cpufreq_exit_governor(policy);
2781 }
2782
2783 /* new governor failed, so re-start old one */
2784 pr_debug("starting governor %s failed\n", policy->governor->name);
2785 if (old_gov) {
2786 policy->governor = old_gov;
2787 if (cpufreq_init_governor(policy))
2788 policy->governor = NULL;
2789 else
2790 cpufreq_start_governor(policy);
2791 }
2792
2793 return ret;
2794 }
2795
2796 /**
2797 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2798 * @cpu: CPU to re-evaluate the policy for.
2799 *
2800 * Update the current frequency for the cpufreq policy of @cpu and use
2801 * cpufreq_set_policy() to re-apply the min and max limits, which triggers the
2802 * evaluation of policy notifiers and the cpufreq driver's ->verify() callback
2803 * for the policy in question, among other things.
2804 */
cpufreq_update_policy(unsigned int cpu)2805 void cpufreq_update_policy(unsigned int cpu)
2806 {
2807 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
2808
2809 if (!policy)
2810 return;
2811
2812 /*
2813 * BIOS might change freq behind our back
2814 * -> ask driver for current freq and notify governors about a change
2815 */
2816 if (cpufreq_driver->get && has_target() &&
2817 (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
2818 goto unlock;
2819
2820 refresh_frequency_limits(policy);
2821
2822 unlock:
2823 cpufreq_cpu_release(policy);
2824 }
2825 EXPORT_SYMBOL(cpufreq_update_policy);
2826
2827 /**
2828 * cpufreq_update_limits - Update policy limits for a given CPU.
2829 * @cpu: CPU to update the policy limits for.
2830 *
2831 * Invoke the driver's ->update_limits callback if present or call
2832 * cpufreq_update_policy() for @cpu.
2833 */
cpufreq_update_limits(unsigned int cpu)2834 void cpufreq_update_limits(unsigned int cpu)
2835 {
2836 struct cpufreq_policy *policy __free(put_cpufreq_policy);
2837
2838 policy = cpufreq_cpu_get(cpu);
2839 if (!policy)
2840 return;
2841
2842 if (cpufreq_driver->update_limits)
2843 cpufreq_driver->update_limits(cpu);
2844 else
2845 cpufreq_update_policy(cpu);
2846 }
2847 EXPORT_SYMBOL_GPL(cpufreq_update_limits);
2848
2849 /*********************************************************************
2850 * BOOST *
2851 *********************************************************************/
cpufreq_boost_set_sw(struct cpufreq_policy * policy,int state)2852 int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state)
2853 {
2854 int ret;
2855
2856 if (!policy->freq_table)
2857 return -ENXIO;
2858
2859 ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table);
2860 if (ret) {
2861 pr_err("%s: Policy frequency update failed\n", __func__);
2862 return ret;
2863 }
2864
2865 ret = freq_qos_update_request(policy->max_freq_req, policy->max);
2866 if (ret < 0)
2867 return ret;
2868
2869 return 0;
2870 }
2871 EXPORT_SYMBOL_GPL(cpufreq_boost_set_sw);
2872
cpufreq_boost_trigger_state(int state)2873 static int cpufreq_boost_trigger_state(int state)
2874 {
2875 struct cpufreq_policy *policy;
2876 unsigned long flags;
2877 int ret = 0;
2878
2879 if (cpufreq_driver->boost_enabled == state)
2880 return 0;
2881
2882 write_lock_irqsave(&cpufreq_driver_lock, flags);
2883 cpufreq_driver->boost_enabled = state;
2884 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2885
2886 cpus_read_lock();
2887 for_each_active_policy(policy) {
2888 if (!policy->boost_supported)
2889 continue;
2890
2891 policy->boost_enabled = state;
2892 ret = cpufreq_driver->set_boost(policy, state);
2893 if (ret) {
2894 policy->boost_enabled = !policy->boost_enabled;
2895 goto err_reset_state;
2896 }
2897 }
2898 cpus_read_unlock();
2899
2900 return 0;
2901
2902 err_reset_state:
2903 cpus_read_unlock();
2904
2905 write_lock_irqsave(&cpufreq_driver_lock, flags);
2906 cpufreq_driver->boost_enabled = !state;
2907 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2908
2909 pr_err("%s: Cannot %s BOOST\n",
2910 __func__, str_enable_disable(state));
2911
2912 return ret;
2913 }
2914
cpufreq_boost_supported(void)2915 static bool cpufreq_boost_supported(void)
2916 {
2917 return cpufreq_driver->set_boost;
2918 }
2919
create_boost_sysfs_file(void)2920 static int create_boost_sysfs_file(void)
2921 {
2922 int ret;
2923
2924 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
2925 if (ret)
2926 pr_err("%s: cannot register global BOOST sysfs file\n",
2927 __func__);
2928
2929 return ret;
2930 }
2931
remove_boost_sysfs_file(void)2932 static void remove_boost_sysfs_file(void)
2933 {
2934 if (cpufreq_boost_supported())
2935 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
2936 }
2937
cpufreq_boost_enabled(void)2938 bool cpufreq_boost_enabled(void)
2939 {
2940 return cpufreq_driver->boost_enabled;
2941 }
2942 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2943
2944 /*********************************************************************
2945 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2946 *********************************************************************/
2947 static enum cpuhp_state hp_online;
2948
cpuhp_cpufreq_online(unsigned int cpu)2949 static int cpuhp_cpufreq_online(unsigned int cpu)
2950 {
2951 cpufreq_online(cpu);
2952
2953 return 0;
2954 }
2955
cpuhp_cpufreq_offline(unsigned int cpu)2956 static int cpuhp_cpufreq_offline(unsigned int cpu)
2957 {
2958 cpufreq_offline(cpu);
2959
2960 return 0;
2961 }
2962
2963 /**
2964 * cpufreq_register_driver - register a CPU Frequency driver
2965 * @driver_data: A struct cpufreq_driver containing the values#
2966 * submitted by the CPU Frequency driver.
2967 *
2968 * Registers a CPU Frequency driver to this core code. This code
2969 * returns zero on success, -EEXIST when another driver got here first
2970 * (and isn't unregistered in the meantime).
2971 *
2972 */
cpufreq_register_driver(struct cpufreq_driver * driver_data)2973 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2974 {
2975 unsigned long flags;
2976 int ret;
2977
2978 if (cpufreq_disabled())
2979 return -ENODEV;
2980
2981 /*
2982 * The cpufreq core depends heavily on the availability of device
2983 * structure, make sure they are available before proceeding further.
2984 */
2985 if (!get_cpu_device(0))
2986 return -EPROBE_DEFER;
2987
2988 if (!driver_data || !driver_data->verify || !driver_data->init ||
2989 !(driver_data->setpolicy || driver_data->target_index ||
2990 driver_data->target) ||
2991 (driver_data->setpolicy && (driver_data->target_index ||
2992 driver_data->target)) ||
2993 (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
2994 (!driver_data->online != !driver_data->offline) ||
2995 (driver_data->adjust_perf && !driver_data->fast_switch))
2996 return -EINVAL;
2997
2998 pr_debug("trying to register driver %s\n", driver_data->name);
2999
3000 /* Protect against concurrent CPU online/offline. */
3001 cpus_read_lock();
3002
3003 write_lock_irqsave(&cpufreq_driver_lock, flags);
3004 if (cpufreq_driver) {
3005 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
3006 ret = -EEXIST;
3007 goto out;
3008 }
3009 cpufreq_driver = driver_data;
3010 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
3011
3012 /*
3013 * Mark support for the scheduler's frequency invariance engine for
3014 * drivers that implement target(), target_index() or fast_switch().
3015 */
3016 if (!cpufreq_driver->setpolicy) {
3017 static_branch_enable_cpuslocked(&cpufreq_freq_invariance);
3018 pr_debug("supports frequency invariance");
3019 }
3020
3021 if (driver_data->setpolicy)
3022 driver_data->flags |= CPUFREQ_CONST_LOOPS;
3023
3024 if (cpufreq_boost_supported()) {
3025 ret = create_boost_sysfs_file();
3026 if (ret)
3027 goto err_null_driver;
3028 }
3029
3030 ret = subsys_interface_register(&cpufreq_interface);
3031 if (ret)
3032 goto err_boost_unreg;
3033
3034 if (unlikely(list_empty(&cpufreq_policy_list))) {
3035 /* if all ->init() calls failed, unregister */
3036 ret = -ENODEV;
3037 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
3038 driver_data->name);
3039 goto err_if_unreg;
3040 }
3041
3042 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
3043 "cpufreq:online",
3044 cpuhp_cpufreq_online,
3045 cpuhp_cpufreq_offline);
3046 if (ret < 0)
3047 goto err_if_unreg;
3048 hp_online = ret;
3049 ret = 0;
3050
3051 pr_debug("driver %s up and running\n", driver_data->name);
3052 goto out;
3053
3054 err_if_unreg:
3055 subsys_interface_unregister(&cpufreq_interface);
3056 err_boost_unreg:
3057 remove_boost_sysfs_file();
3058 err_null_driver:
3059 write_lock_irqsave(&cpufreq_driver_lock, flags);
3060 cpufreq_driver = NULL;
3061 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
3062 out:
3063 cpus_read_unlock();
3064 return ret;
3065 }
3066 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
3067
3068 /*
3069 * cpufreq_unregister_driver - unregister the current CPUFreq driver
3070 *
3071 * Unregister the current CPUFreq driver. Only call this if you have
3072 * the right to do so, i.e. if you have succeeded in initialising before!
3073 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
3074 * currently not initialised.
3075 */
cpufreq_unregister_driver(struct cpufreq_driver * driver)3076 void cpufreq_unregister_driver(struct cpufreq_driver *driver)
3077 {
3078 unsigned long flags;
3079
3080 if (WARN_ON(!cpufreq_driver || (driver != cpufreq_driver)))
3081 return;
3082
3083 pr_debug("unregistering driver %s\n", driver->name);
3084
3085 /* Protect against concurrent cpu hotplug */
3086 cpus_read_lock();
3087 subsys_interface_unregister(&cpufreq_interface);
3088 remove_boost_sysfs_file();
3089 static_branch_disable_cpuslocked(&cpufreq_freq_invariance);
3090 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
3091
3092 write_lock_irqsave(&cpufreq_driver_lock, flags);
3093
3094 cpufreq_driver = NULL;
3095
3096 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
3097 cpus_read_unlock();
3098 }
3099 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
3100
cpufreq_core_init(void)3101 static int __init cpufreq_core_init(void)
3102 {
3103 struct cpufreq_governor *gov = cpufreq_default_governor();
3104 struct device *dev_root;
3105
3106 if (cpufreq_disabled())
3107 return -ENODEV;
3108
3109 dev_root = bus_get_dev_root(&cpu_subsys);
3110 if (dev_root) {
3111 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &dev_root->kobj);
3112 put_device(dev_root);
3113 }
3114 BUG_ON(!cpufreq_global_kobject);
3115
3116 if (!strlen(default_governor))
3117 strscpy(default_governor, gov->name, CPUFREQ_NAME_LEN);
3118
3119 return 0;
3120 }
3121 module_param(off, int, 0444);
3122 module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444);
3123 core_initcall(cpufreq_core_init);
3124