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