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