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