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