xref: /linux-6.15/include/linux/cpufreq.h (revision b3e8701d)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * linux/include/linux/cpufreq.h
4  *
5  * Copyright (C) 2001 Russell King
6  *           (C) 2002 - 2003 Dominik Brodowski <[email protected]>
7  */
8 #ifndef _LINUX_CPUFREQ_H
9 #define _LINUX_CPUFREQ_H
10 
11 #include <linux/clk.h>
12 #include <linux/cpu.h>
13 #include <linux/cpumask.h>
14 #include <linux/completion.h>
15 #include <linux/kobject.h>
16 #include <linux/notifier.h>
17 #include <linux/of.h>
18 #include <linux/of_device.h>
19 #include <linux/pm_opp.h>
20 #include <linux/pm_qos.h>
21 #include <linux/spinlock.h>
22 #include <linux/sysfs.h>
23 
24 /*********************************************************************
25  *                        CPUFREQ INTERFACE                          *
26  *********************************************************************/
27 /*
28  * Frequency values here are CPU kHz
29  *
30  * Maximum transition latency is in nanoseconds - if it's unknown,
31  * CPUFREQ_ETERNAL shall be used.
32  */
33 
34 #define CPUFREQ_ETERNAL			(-1)
35 #define CPUFREQ_NAME_LEN		16
36 /* Print length for names. Extra 1 space for accommodating '\n' in prints */
37 #define CPUFREQ_NAME_PLEN		(CPUFREQ_NAME_LEN + 1)
38 
39 struct cpufreq_governor;
40 
41 enum cpufreq_table_sorting {
42 	CPUFREQ_TABLE_UNSORTED,
43 	CPUFREQ_TABLE_SORTED_ASCENDING,
44 	CPUFREQ_TABLE_SORTED_DESCENDING
45 };
46 
47 struct cpufreq_cpuinfo {
48 	unsigned int		max_freq;
49 	unsigned int		min_freq;
50 
51 	/* in 10^(-9) s = nanoseconds */
52 	unsigned int		transition_latency;
53 };
54 
55 struct cpufreq_policy {
56 	/* CPUs sharing clock, require sw coordination */
57 	cpumask_var_t		cpus;	/* Online CPUs only */
58 	cpumask_var_t		related_cpus; /* Online + Offline CPUs */
59 	cpumask_var_t		real_cpus; /* Related and present */
60 
61 	unsigned int		shared_type; /* ACPI: ANY or ALL affected CPUs
62 						should set cpufreq */
63 	unsigned int		cpu;    /* cpu managing this policy, must be online */
64 
65 	struct clk		*clk;
66 	struct cpufreq_cpuinfo	cpuinfo;/* see above */
67 
68 	unsigned int		min;    /* in kHz */
69 	unsigned int		max;    /* in kHz */
70 	unsigned int		cur;    /* in kHz, only needed if cpufreq
71 					 * governors are used */
72 	unsigned int		suspend_freq; /* freq to set during suspend */
73 
74 	unsigned int		policy; /* see above */
75 	unsigned int		last_policy; /* policy before unplug */
76 	struct cpufreq_governor	*governor; /* see below */
77 	void			*governor_data;
78 	char			last_governor[CPUFREQ_NAME_LEN]; /* last governor used */
79 
80 	struct work_struct	update; /* if update_policy() needs to be
81 					 * called, but you're in IRQ context */
82 
83 	struct freq_constraints	constraints;
84 	struct freq_qos_request	*min_freq_req;
85 	struct freq_qos_request	*max_freq_req;
86 
87 	struct cpufreq_frequency_table	*freq_table;
88 	enum cpufreq_table_sorting freq_table_sorted;
89 
90 	struct list_head        policy_list;
91 	struct kobject		kobj;
92 	struct completion	kobj_unregister;
93 
94 	/*
95 	 * The rules for this semaphore:
96 	 * - Any routine that wants to read from the policy structure will
97 	 *   do a down_read on this semaphore.
98 	 * - Any routine that will write to the policy structure and/or may take away
99 	 *   the policy altogether (eg. CPU hotplug), will hold this lock in write
100 	 *   mode before doing so.
101 	 */
102 	struct rw_semaphore	rwsem;
103 
104 	/*
105 	 * Fast switch flags:
106 	 * - fast_switch_possible should be set by the driver if it can
107 	 *   guarantee that frequency can be changed on any CPU sharing the
108 	 *   policy and that the change will affect all of the policy CPUs then.
109 	 * - fast_switch_enabled is to be set by governors that support fast
110 	 *   frequency switching with the help of cpufreq_enable_fast_switch().
111 	 */
112 	bool			fast_switch_possible;
113 	bool			fast_switch_enabled;
114 
115 	/*
116 	 * Set if the CPUFREQ_GOV_STRICT_TARGET flag is set for the current
117 	 * governor.
118 	 */
119 	bool			strict_target;
120 
121 	/*
122 	 * Set if inefficient frequencies were found in the frequency table.
123 	 * This indicates if the relation flag CPUFREQ_RELATION_E can be
124 	 * honored.
125 	 */
126 	bool			efficiencies_available;
127 
128 	/*
129 	 * Preferred average time interval between consecutive invocations of
130 	 * the driver to set the frequency for this policy.  To be set by the
131 	 * scaling driver (0, which is the default, means no preference).
132 	 */
133 	unsigned int		transition_delay_us;
134 
135 	/*
136 	 * Remote DVFS flag (Not added to the driver structure as we don't want
137 	 * to access another structure from scheduler hotpath).
138 	 *
139 	 * Should be set if CPUs can do DVFS on behalf of other CPUs from
140 	 * different cpufreq policies.
141 	 */
142 	bool			dvfs_possible_from_any_cpu;
143 
144 	 /* Cached frequency lookup from cpufreq_driver_resolve_freq. */
145 	unsigned int cached_target_freq;
146 	unsigned int cached_resolved_idx;
147 
148 	/* Synchronization for frequency transitions */
149 	bool			transition_ongoing; /* Tracks transition status */
150 	spinlock_t		transition_lock;
151 	wait_queue_head_t	transition_wait;
152 	struct task_struct	*transition_task; /* Task which is doing the transition */
153 
154 	/* cpufreq-stats */
155 	struct cpufreq_stats	*stats;
156 
157 	/* For cpufreq driver's internal use */
158 	void			*driver_data;
159 
160 	/* Pointer to the cooling device if used for thermal mitigation */
161 	struct thermal_cooling_device *cdev;
162 
163 	struct notifier_block nb_min;
164 	struct notifier_block nb_max;
165 };
166 
167 /*
168  * Used for passing new cpufreq policy data to the cpufreq driver's ->verify()
169  * callback for sanitization.  That callback is only expected to modify the min
170  * and max values, if necessary, and specifically it must not update the
171  * frequency table.
172  */
173 struct cpufreq_policy_data {
174 	struct cpufreq_cpuinfo		cpuinfo;
175 	struct cpufreq_frequency_table	*freq_table;
176 	unsigned int			cpu;
177 	unsigned int			min;    /* in kHz */
178 	unsigned int			max;    /* in kHz */
179 };
180 
181 struct cpufreq_freqs {
182 	struct cpufreq_policy *policy;
183 	unsigned int old;
184 	unsigned int new;
185 	u8 flags;		/* flags of cpufreq_driver, see below. */
186 };
187 
188 /* Only for ACPI */
189 #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */
190 #define CPUFREQ_SHARED_TYPE_HW	 (1) /* HW does needed coordination */
191 #define CPUFREQ_SHARED_TYPE_ALL	 (2) /* All dependent CPUs should set freq */
192 #define CPUFREQ_SHARED_TYPE_ANY	 (3) /* Freq can be set from any dependent CPU*/
193 
194 #ifdef CONFIG_CPU_FREQ
195 struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
196 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
197 void cpufreq_cpu_put(struct cpufreq_policy *policy);
198 #else
199 static inline struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
200 {
201 	return NULL;
202 }
203 static inline struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
204 {
205 	return NULL;
206 }
207 static inline void cpufreq_cpu_put(struct cpufreq_policy *policy) { }
208 #endif
209 
210 static inline bool policy_is_inactive(struct cpufreq_policy *policy)
211 {
212 	return cpumask_empty(policy->cpus);
213 }
214 
215 static inline bool policy_is_shared(struct cpufreq_policy *policy)
216 {
217 	return cpumask_weight(policy->cpus) > 1;
218 }
219 
220 #ifdef CONFIG_CPU_FREQ
221 unsigned int cpufreq_get(unsigned int cpu);
222 unsigned int cpufreq_quick_get(unsigned int cpu);
223 unsigned int cpufreq_quick_get_max(unsigned int cpu);
224 unsigned int cpufreq_get_hw_max_freq(unsigned int cpu);
225 void disable_cpufreq(void);
226 
227 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy);
228 
229 struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu);
230 void cpufreq_cpu_release(struct cpufreq_policy *policy);
231 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
232 void refresh_frequency_limits(struct cpufreq_policy *policy);
233 void cpufreq_update_policy(unsigned int cpu);
234 void cpufreq_update_limits(unsigned int cpu);
235 bool have_governor_per_policy(void);
236 bool cpufreq_supports_freq_invariance(void);
237 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy);
238 void cpufreq_enable_fast_switch(struct cpufreq_policy *policy);
239 void cpufreq_disable_fast_switch(struct cpufreq_policy *policy);
240 bool has_target_index(void);
241 #else
242 static inline unsigned int cpufreq_get(unsigned int cpu)
243 {
244 	return 0;
245 }
246 static inline unsigned int cpufreq_quick_get(unsigned int cpu)
247 {
248 	return 0;
249 }
250 static inline unsigned int cpufreq_quick_get_max(unsigned int cpu)
251 {
252 	return 0;
253 }
254 static inline unsigned int cpufreq_get_hw_max_freq(unsigned int cpu)
255 {
256 	return 0;
257 }
258 static inline bool cpufreq_supports_freq_invariance(void)
259 {
260 	return false;
261 }
262 static inline void disable_cpufreq(void) { }
263 #endif
264 
265 #ifdef CONFIG_CPU_FREQ_STAT
266 void cpufreq_stats_create_table(struct cpufreq_policy *policy);
267 void cpufreq_stats_free_table(struct cpufreq_policy *policy);
268 void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
269 				     unsigned int new_freq);
270 #else
271 static inline void cpufreq_stats_create_table(struct cpufreq_policy *policy) { }
272 static inline void cpufreq_stats_free_table(struct cpufreq_policy *policy) { }
273 static inline void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
274 						   unsigned int new_freq) { }
275 #endif /* CONFIG_CPU_FREQ_STAT */
276 
277 /*********************************************************************
278  *                      CPUFREQ DRIVER INTERFACE                     *
279  *********************************************************************/
280 
281 #define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
282 #define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */
283 #define CPUFREQ_RELATION_C 2  /* closest frequency to target */
284 /* relation flags */
285 #define CPUFREQ_RELATION_E BIT(2) /* Get if possible an efficient frequency */
286 
287 #define CPUFREQ_RELATION_LE (CPUFREQ_RELATION_L | CPUFREQ_RELATION_E)
288 #define CPUFREQ_RELATION_HE (CPUFREQ_RELATION_H | CPUFREQ_RELATION_E)
289 #define CPUFREQ_RELATION_CE (CPUFREQ_RELATION_C | CPUFREQ_RELATION_E)
290 
291 struct freq_attr {
292 	struct attribute attr;
293 	ssize_t (*show)(struct cpufreq_policy *, char *);
294 	ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
295 };
296 
297 #define cpufreq_freq_attr_ro(_name)		\
298 static struct freq_attr _name =			\
299 __ATTR(_name, 0444, show_##_name, NULL)
300 
301 #define cpufreq_freq_attr_ro_perm(_name, _perm)	\
302 static struct freq_attr _name =			\
303 __ATTR(_name, _perm, show_##_name, NULL)
304 
305 #define cpufreq_freq_attr_rw(_name)		\
306 static struct freq_attr _name =			\
307 __ATTR(_name, 0644, show_##_name, store_##_name)
308 
309 #define cpufreq_freq_attr_wo(_name)		\
310 static struct freq_attr _name =			\
311 __ATTR(_name, 0200, NULL, store_##_name)
312 
313 #define define_one_global_ro(_name)		\
314 static struct kobj_attribute _name =		\
315 __ATTR(_name, 0444, show_##_name, NULL)
316 
317 #define define_one_global_rw(_name)		\
318 static struct kobj_attribute _name =		\
319 __ATTR(_name, 0644, show_##_name, store_##_name)
320 
321 
322 struct cpufreq_driver {
323 	char		name[CPUFREQ_NAME_LEN];
324 	u16		flags;
325 	void		*driver_data;
326 
327 	/* needed by all drivers */
328 	int		(*init)(struct cpufreq_policy *policy);
329 	int		(*verify)(struct cpufreq_policy_data *policy);
330 
331 	/* define one out of two */
332 	int		(*setpolicy)(struct cpufreq_policy *policy);
333 
334 	int		(*target)(struct cpufreq_policy *policy,
335 				  unsigned int target_freq,
336 				  unsigned int relation);	/* Deprecated */
337 	int		(*target_index)(struct cpufreq_policy *policy,
338 					unsigned int index);
339 	unsigned int	(*fast_switch)(struct cpufreq_policy *policy,
340 				       unsigned int target_freq);
341 	/*
342 	 * ->fast_switch() replacement for drivers that use an internal
343 	 * representation of performance levels and can pass hints other than
344 	 * the target performance level to the hardware.
345 	 */
346 	void		(*adjust_perf)(unsigned int cpu,
347 				       unsigned long min_perf,
348 				       unsigned long target_perf,
349 				       unsigned long capacity);
350 
351 	/*
352 	 * Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION
353 	 * unset.
354 	 *
355 	 * get_intermediate should return a stable intermediate frequency
356 	 * platform wants to switch to and target_intermediate() should set CPU
357 	 * to that frequency, before jumping to the frequency corresponding
358 	 * to 'index'. Core will take care of sending notifications and driver
359 	 * doesn't have to handle them in target_intermediate() or
360 	 * target_index().
361 	 *
362 	 * Drivers can return '0' from get_intermediate() in case they don't
363 	 * wish to switch to intermediate frequency for some target frequency.
364 	 * In that case core will directly call ->target_index().
365 	 */
366 	unsigned int	(*get_intermediate)(struct cpufreq_policy *policy,
367 					    unsigned int index);
368 	int		(*target_intermediate)(struct cpufreq_policy *policy,
369 					       unsigned int index);
370 
371 	/* should be defined, if possible */
372 	unsigned int	(*get)(unsigned int cpu);
373 
374 	/* Called to update policy limits on firmware notifications. */
375 	void		(*update_limits)(unsigned int cpu);
376 
377 	/* optional */
378 	int		(*bios_limit)(int cpu, unsigned int *limit);
379 
380 	int		(*online)(struct cpufreq_policy *policy);
381 	int		(*offline)(struct cpufreq_policy *policy);
382 	int		(*exit)(struct cpufreq_policy *policy);
383 	int		(*suspend)(struct cpufreq_policy *policy);
384 	int		(*resume)(struct cpufreq_policy *policy);
385 
386 	/* Will be called after the driver is fully initialized */
387 	void		(*ready)(struct cpufreq_policy *policy);
388 
389 	struct freq_attr **attr;
390 
391 	/* platform specific boost support code */
392 	bool		boost_enabled;
393 	int		(*set_boost)(struct cpufreq_policy *policy, int state);
394 
395 	/*
396 	 * Set by drivers that want to register with the energy model after the
397 	 * policy is properly initialized, but before the governor is started.
398 	 */
399 	void		(*register_em)(struct cpufreq_policy *policy);
400 };
401 
402 /* flags */
403 
404 /*
405  * Set by drivers that need to update internal upper and lower boundaries along
406  * with the target frequency and so the core and governors should also invoke
407  * the diver if the target frequency does not change, but the policy min or max
408  * may have changed.
409  */
410 #define CPUFREQ_NEED_UPDATE_LIMITS		BIT(0)
411 
412 /* loops_per_jiffy or other kernel "constants" aren't affected by frequency transitions */
413 #define CPUFREQ_CONST_LOOPS			BIT(1)
414 
415 /*
416  * Set by drivers that want the core to automatically register the cpufreq
417  * driver as a thermal cooling device.
418  */
419 #define CPUFREQ_IS_COOLING_DEV			BIT(2)
420 
421 /*
422  * This should be set by platforms having multiple clock-domains, i.e.
423  * supporting multiple policies. With this sysfs directories of governor would
424  * be created in cpu/cpu<num>/cpufreq/ directory and so they can use the same
425  * governor with different tunables for different clusters.
426  */
427 #define CPUFREQ_HAVE_GOVERNOR_PER_POLICY	BIT(3)
428 
429 /*
430  * Driver will do POSTCHANGE notifications from outside of their ->target()
431  * routine and so must set cpufreq_driver->flags with this flag, so that core
432  * can handle them specially.
433  */
434 #define CPUFREQ_ASYNC_NOTIFICATION		BIT(4)
435 
436 /*
437  * Set by drivers which want cpufreq core to check if CPU is running at a
438  * frequency present in freq-table exposed by the driver. For these drivers if
439  * CPU is found running at an out of table freq, we will try to set it to a freq
440  * from the table. And if that fails, we will stop further boot process by
441  * issuing a BUG_ON().
442  */
443 #define CPUFREQ_NEED_INITIAL_FREQ_CHECK	BIT(5)
444 
445 /*
446  * Set by drivers to disallow use of governors with "dynamic_switching" flag
447  * set.
448  */
449 #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING	BIT(6)
450 
451 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
452 void cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
453 
454 bool cpufreq_driver_test_flags(u16 flags);
455 const char *cpufreq_get_current_driver(void);
456 void *cpufreq_get_driver_data(void);
457 
458 static inline int cpufreq_thermal_control_enabled(struct cpufreq_driver *drv)
459 {
460 	return IS_ENABLED(CONFIG_CPU_THERMAL) &&
461 		(drv->flags & CPUFREQ_IS_COOLING_DEV);
462 }
463 
464 static inline void cpufreq_verify_within_limits(struct cpufreq_policy_data *policy,
465 						unsigned int min,
466 						unsigned int max)
467 {
468 	if (policy->min < min)
469 		policy->min = min;
470 	if (policy->max < min)
471 		policy->max = min;
472 	if (policy->min > max)
473 		policy->min = max;
474 	if (policy->max > max)
475 		policy->max = max;
476 	if (policy->min > policy->max)
477 		policy->min = policy->max;
478 	return;
479 }
480 
481 static inline void
482 cpufreq_verify_within_cpu_limits(struct cpufreq_policy_data *policy)
483 {
484 	cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
485 				     policy->cpuinfo.max_freq);
486 }
487 
488 #ifdef CONFIG_CPU_FREQ
489 void cpufreq_suspend(void);
490 void cpufreq_resume(void);
491 int cpufreq_generic_suspend(struct cpufreq_policy *policy);
492 #else
493 static inline void cpufreq_suspend(void) {}
494 static inline void cpufreq_resume(void) {}
495 #endif
496 
497 /*********************************************************************
498  *                     CPUFREQ NOTIFIER INTERFACE                    *
499  *********************************************************************/
500 
501 #define CPUFREQ_TRANSITION_NOTIFIER	(0)
502 #define CPUFREQ_POLICY_NOTIFIER		(1)
503 
504 /* Transition notifiers */
505 #define CPUFREQ_PRECHANGE		(0)
506 #define CPUFREQ_POSTCHANGE		(1)
507 
508 /* Policy Notifiers  */
509 #define CPUFREQ_CREATE_POLICY		(0)
510 #define CPUFREQ_REMOVE_POLICY		(1)
511 
512 #ifdef CONFIG_CPU_FREQ
513 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
514 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
515 
516 void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
517 		struct cpufreq_freqs *freqs);
518 void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
519 		struct cpufreq_freqs *freqs, int transition_failed);
520 
521 #else /* CONFIG_CPU_FREQ */
522 static inline int cpufreq_register_notifier(struct notifier_block *nb,
523 						unsigned int list)
524 {
525 	return 0;
526 }
527 static inline int cpufreq_unregister_notifier(struct notifier_block *nb,
528 						unsigned int list)
529 {
530 	return 0;
531 }
532 #endif /* !CONFIG_CPU_FREQ */
533 
534 /**
535  * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch
536  * safe)
537  * @old:   old value
538  * @div:   divisor
539  * @mult:  multiplier
540  *
541  *
542  * new = old * mult / div
543  */
544 static inline unsigned long cpufreq_scale(unsigned long old, u_int div,
545 		u_int mult)
546 {
547 #if BITS_PER_LONG == 32
548 	u64 result = ((u64) old) * ((u64) mult);
549 	do_div(result, div);
550 	return (unsigned long) result;
551 
552 #elif BITS_PER_LONG == 64
553 	unsigned long result = old * ((u64) mult);
554 	result /= div;
555 	return result;
556 #endif
557 }
558 
559 /*********************************************************************
560  *                          CPUFREQ GOVERNORS                        *
561  *********************************************************************/
562 
563 #define CPUFREQ_POLICY_UNKNOWN		(0)
564 /*
565  * If (cpufreq_driver->target) exists, the ->governor decides what frequency
566  * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
567  * two generic policies are available:
568  */
569 #define CPUFREQ_POLICY_POWERSAVE	(1)
570 #define CPUFREQ_POLICY_PERFORMANCE	(2)
571 
572 /*
573  * The polling frequency depends on the capability of the processor. Default
574  * polling frequency is 1000 times the transition latency of the processor. The
575  * ondemand governor will work on any processor with transition latency <= 10ms,
576  * using appropriate sampling rate.
577  */
578 #define LATENCY_MULTIPLIER		(1000)
579 
580 struct cpufreq_governor {
581 	char	name[CPUFREQ_NAME_LEN];
582 	int	(*init)(struct cpufreq_policy *policy);
583 	void	(*exit)(struct cpufreq_policy *policy);
584 	int	(*start)(struct cpufreq_policy *policy);
585 	void	(*stop)(struct cpufreq_policy *policy);
586 	void	(*limits)(struct cpufreq_policy *policy);
587 	ssize_t	(*show_setspeed)	(struct cpufreq_policy *policy,
588 					 char *buf);
589 	int	(*store_setspeed)	(struct cpufreq_policy *policy,
590 					 unsigned int freq);
591 	struct list_head	governor_list;
592 	struct module		*owner;
593 	u8			flags;
594 };
595 
596 /* Governor flags */
597 
598 /* For governors which change frequency dynamically by themselves */
599 #define CPUFREQ_GOV_DYNAMIC_SWITCHING	BIT(0)
600 
601 /* For governors wanting the target frequency to be set exactly */
602 #define CPUFREQ_GOV_STRICT_TARGET	BIT(1)
603 
604 
605 /* Pass a target to the cpufreq driver */
606 unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
607 					unsigned int target_freq);
608 void cpufreq_driver_adjust_perf(unsigned int cpu,
609 				unsigned long min_perf,
610 				unsigned long target_perf,
611 				unsigned long capacity);
612 bool cpufreq_driver_has_adjust_perf(void);
613 int cpufreq_driver_target(struct cpufreq_policy *policy,
614 				 unsigned int target_freq,
615 				 unsigned int relation);
616 int __cpufreq_driver_target(struct cpufreq_policy *policy,
617 				   unsigned int target_freq,
618 				   unsigned int relation);
619 unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
620 					 unsigned int target_freq);
621 unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy);
622 int cpufreq_register_governor(struct cpufreq_governor *governor);
623 void cpufreq_unregister_governor(struct cpufreq_governor *governor);
624 int cpufreq_start_governor(struct cpufreq_policy *policy);
625 void cpufreq_stop_governor(struct cpufreq_policy *policy);
626 
627 #define cpufreq_governor_init(__governor)			\
628 static int __init __governor##_init(void)			\
629 {								\
630 	return cpufreq_register_governor(&__governor);	\
631 }								\
632 core_initcall(__governor##_init)
633 
634 #define cpufreq_governor_exit(__governor)			\
635 static void __exit __governor##_exit(void)			\
636 {								\
637 	return cpufreq_unregister_governor(&__governor);	\
638 }								\
639 module_exit(__governor##_exit)
640 
641 struct cpufreq_governor *cpufreq_default_governor(void);
642 struct cpufreq_governor *cpufreq_fallback_governor(void);
643 
644 static inline void cpufreq_policy_apply_limits(struct cpufreq_policy *policy)
645 {
646 	if (policy->max < policy->cur)
647 		__cpufreq_driver_target(policy, policy->max,
648 					CPUFREQ_RELATION_HE);
649 	else if (policy->min > policy->cur)
650 		__cpufreq_driver_target(policy, policy->min,
651 					CPUFREQ_RELATION_LE);
652 }
653 
654 /* Governor attribute set */
655 struct gov_attr_set {
656 	struct kobject kobj;
657 	struct list_head policy_list;
658 	struct mutex update_lock;
659 	int usage_count;
660 };
661 
662 /* sysfs ops for cpufreq governors */
663 extern const struct sysfs_ops governor_sysfs_ops;
664 
665 static inline struct gov_attr_set *to_gov_attr_set(struct kobject *kobj)
666 {
667 	return container_of(kobj, struct gov_attr_set, kobj);
668 }
669 
670 void gov_attr_set_init(struct gov_attr_set *attr_set, struct list_head *list_node);
671 void gov_attr_set_get(struct gov_attr_set *attr_set, struct list_head *list_node);
672 unsigned int gov_attr_set_put(struct gov_attr_set *attr_set, struct list_head *list_node);
673 
674 /* Governor sysfs attribute */
675 struct governor_attr {
676 	struct attribute attr;
677 	ssize_t (*show)(struct gov_attr_set *attr_set, char *buf);
678 	ssize_t (*store)(struct gov_attr_set *attr_set, const char *buf,
679 			 size_t count);
680 };
681 
682 /*********************************************************************
683  *                     FREQUENCY TABLE HELPERS                       *
684  *********************************************************************/
685 
686 /* Special Values of .frequency field */
687 #define CPUFREQ_ENTRY_INVALID		~0u
688 #define CPUFREQ_TABLE_END		~1u
689 /* Special Values of .flags field */
690 #define CPUFREQ_BOOST_FREQ		(1 << 0)
691 #define CPUFREQ_INEFFICIENT_FREQ	(1 << 1)
692 
693 struct cpufreq_frequency_table {
694 	unsigned int	flags;
695 	unsigned int	driver_data; /* driver specific data, not used by core */
696 	unsigned int	frequency; /* kHz - doesn't need to be in ascending
697 				    * order */
698 };
699 
700 #if defined(CONFIG_CPU_FREQ) && defined(CONFIG_PM_OPP)
701 int dev_pm_opp_init_cpufreq_table(struct device *dev,
702 				  struct cpufreq_frequency_table **table);
703 void dev_pm_opp_free_cpufreq_table(struct device *dev,
704 				   struct cpufreq_frequency_table **table);
705 #else
706 static inline int dev_pm_opp_init_cpufreq_table(struct device *dev,
707 						struct cpufreq_frequency_table
708 						**table)
709 {
710 	return -EINVAL;
711 }
712 
713 static inline void dev_pm_opp_free_cpufreq_table(struct device *dev,
714 						 struct cpufreq_frequency_table
715 						 **table)
716 {
717 }
718 #endif
719 
720 /*
721  * cpufreq_for_each_entry -	iterate over a cpufreq_frequency_table
722  * @pos:	the cpufreq_frequency_table * to use as a loop cursor.
723  * @table:	the cpufreq_frequency_table * to iterate over.
724  */
725 
726 #define cpufreq_for_each_entry(pos, table)	\
727 	for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++)
728 
729 /*
730  * cpufreq_for_each_entry_idx -	iterate over a cpufreq_frequency_table
731  *	with index
732  * @pos:	the cpufreq_frequency_table * to use as a loop cursor.
733  * @table:	the cpufreq_frequency_table * to iterate over.
734  * @idx:	the table entry currently being processed
735  */
736 
737 #define cpufreq_for_each_entry_idx(pos, table, idx)	\
738 	for (pos = table, idx = 0; pos->frequency != CPUFREQ_TABLE_END; \
739 		pos++, idx++)
740 
741 /*
742  * cpufreq_for_each_valid_entry -     iterate over a cpufreq_frequency_table
743  *	excluding CPUFREQ_ENTRY_INVALID frequencies.
744  * @pos:        the cpufreq_frequency_table * to use as a loop cursor.
745  * @table:      the cpufreq_frequency_table * to iterate over.
746  */
747 
748 #define cpufreq_for_each_valid_entry(pos, table)			\
749 	for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++)	\
750 		if (pos->frequency == CPUFREQ_ENTRY_INVALID)		\
751 			continue;					\
752 		else
753 
754 /*
755  * cpufreq_for_each_valid_entry_idx -     iterate with index over a cpufreq
756  *	frequency_table excluding CPUFREQ_ENTRY_INVALID frequencies.
757  * @pos:	the cpufreq_frequency_table * to use as a loop cursor.
758  * @table:	the cpufreq_frequency_table * to iterate over.
759  * @idx:	the table entry currently being processed
760  */
761 
762 #define cpufreq_for_each_valid_entry_idx(pos, table, idx)		\
763 	cpufreq_for_each_entry_idx(pos, table, idx)			\
764 		if (pos->frequency == CPUFREQ_ENTRY_INVALID)		\
765 			continue;					\
766 		else
767 
768 /**
769  * cpufreq_for_each_efficient_entry_idx - iterate with index over a cpufreq
770  *	frequency_table excluding CPUFREQ_ENTRY_INVALID and
771  *	CPUFREQ_INEFFICIENT_FREQ frequencies.
772  * @pos: the &struct cpufreq_frequency_table to use as a loop cursor.
773  * @table: the &struct cpufreq_frequency_table to iterate over.
774  * @idx: the table entry currently being processed.
775  * @efficiencies: set to true to only iterate over efficient frequencies.
776  */
777 
778 #define cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies)	\
779 	cpufreq_for_each_valid_entry_idx(pos, table, idx)			\
780 		if (efficiencies && (pos->flags & CPUFREQ_INEFFICIENT_FREQ))	\
781 			continue;						\
782 		else
783 
784 
785 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
786 				    struct cpufreq_frequency_table *table);
787 
788 int cpufreq_frequency_table_verify(struct cpufreq_policy_data *policy,
789 				   struct cpufreq_frequency_table *table);
790 int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy);
791 
792 int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
793 				 unsigned int target_freq,
794 				 unsigned int relation);
795 int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
796 		unsigned int freq);
797 
798 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf);
799 
800 #ifdef CONFIG_CPU_FREQ
801 int cpufreq_boost_trigger_state(int state);
802 int cpufreq_boost_enabled(void);
803 int cpufreq_enable_boost_support(void);
804 bool policy_has_boost_freq(struct cpufreq_policy *policy);
805 
806 /* Find lowest freq at or above target in a table in ascending order */
807 static inline int cpufreq_table_find_index_al(struct cpufreq_policy *policy,
808 					      unsigned int target_freq,
809 					      bool efficiencies)
810 {
811 	struct cpufreq_frequency_table *table = policy->freq_table;
812 	struct cpufreq_frequency_table *pos;
813 	unsigned int freq;
814 	int idx, best = -1;
815 
816 	cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) {
817 		freq = pos->frequency;
818 
819 		if (freq >= target_freq)
820 			return idx;
821 
822 		best = idx;
823 	}
824 
825 	return best;
826 }
827 
828 /* Find lowest freq at or above target in a table in descending order */
829 static inline int cpufreq_table_find_index_dl(struct cpufreq_policy *policy,
830 					      unsigned int target_freq,
831 					      bool efficiencies)
832 {
833 	struct cpufreq_frequency_table *table = policy->freq_table;
834 	struct cpufreq_frequency_table *pos;
835 	unsigned int freq;
836 	int idx, best = -1;
837 
838 	cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) {
839 		freq = pos->frequency;
840 
841 		if (freq == target_freq)
842 			return idx;
843 
844 		if (freq > target_freq) {
845 			best = idx;
846 			continue;
847 		}
848 
849 		/* No freq found above target_freq */
850 		if (best == -1)
851 			return idx;
852 
853 		return best;
854 	}
855 
856 	return best;
857 }
858 
859 /* Works only on sorted freq-tables */
860 static inline int cpufreq_table_find_index_l(struct cpufreq_policy *policy,
861 					     unsigned int target_freq,
862 					     bool efficiencies)
863 {
864 	target_freq = clamp_val(target_freq, policy->min, policy->max);
865 
866 	if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
867 		return cpufreq_table_find_index_al(policy, target_freq,
868 						   efficiencies);
869 	else
870 		return cpufreq_table_find_index_dl(policy, target_freq,
871 						   efficiencies);
872 }
873 
874 /* Find highest freq at or below target in a table in ascending order */
875 static inline int cpufreq_table_find_index_ah(struct cpufreq_policy *policy,
876 					      unsigned int target_freq,
877 					      bool efficiencies)
878 {
879 	struct cpufreq_frequency_table *table = policy->freq_table;
880 	struct cpufreq_frequency_table *pos;
881 	unsigned int freq;
882 	int idx, best = -1;
883 
884 	cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) {
885 		freq = pos->frequency;
886 
887 		if (freq == target_freq)
888 			return idx;
889 
890 		if (freq < target_freq) {
891 			best = idx;
892 			continue;
893 		}
894 
895 		/* No freq found below target_freq */
896 		if (best == -1)
897 			return idx;
898 
899 		return best;
900 	}
901 
902 	return best;
903 }
904 
905 /* Find highest freq at or below target in a table in descending order */
906 static inline int cpufreq_table_find_index_dh(struct cpufreq_policy *policy,
907 					      unsigned int target_freq,
908 					      bool efficiencies)
909 {
910 	struct cpufreq_frequency_table *table = policy->freq_table;
911 	struct cpufreq_frequency_table *pos;
912 	unsigned int freq;
913 	int idx, best = -1;
914 
915 	cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) {
916 		freq = pos->frequency;
917 
918 		if (freq <= target_freq)
919 			return idx;
920 
921 		best = idx;
922 	}
923 
924 	return best;
925 }
926 
927 /* Works only on sorted freq-tables */
928 static inline int cpufreq_table_find_index_h(struct cpufreq_policy *policy,
929 					     unsigned int target_freq,
930 					     bool efficiencies)
931 {
932 	target_freq = clamp_val(target_freq, policy->min, policy->max);
933 
934 	if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
935 		return cpufreq_table_find_index_ah(policy, target_freq,
936 						   efficiencies);
937 	else
938 		return cpufreq_table_find_index_dh(policy, target_freq,
939 						   efficiencies);
940 }
941 
942 /* Find closest freq to target in a table in ascending order */
943 static inline int cpufreq_table_find_index_ac(struct cpufreq_policy *policy,
944 					      unsigned int target_freq,
945 					      bool efficiencies)
946 {
947 	struct cpufreq_frequency_table *table = policy->freq_table;
948 	struct cpufreq_frequency_table *pos;
949 	unsigned int freq;
950 	int idx, best = -1;
951 
952 	cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) {
953 		freq = pos->frequency;
954 
955 		if (freq == target_freq)
956 			return idx;
957 
958 		if (freq < target_freq) {
959 			best = idx;
960 			continue;
961 		}
962 
963 		/* No freq found below target_freq */
964 		if (best == -1)
965 			return idx;
966 
967 		/* Choose the closest freq */
968 		if (target_freq - table[best].frequency > freq - target_freq)
969 			return idx;
970 
971 		return best;
972 	}
973 
974 	return best;
975 }
976 
977 /* Find closest freq to target in a table in descending order */
978 static inline int cpufreq_table_find_index_dc(struct cpufreq_policy *policy,
979 					      unsigned int target_freq,
980 					      bool efficiencies)
981 {
982 	struct cpufreq_frequency_table *table = policy->freq_table;
983 	struct cpufreq_frequency_table *pos;
984 	unsigned int freq;
985 	int idx, best = -1;
986 
987 	cpufreq_for_each_efficient_entry_idx(pos, table, idx, efficiencies) {
988 		freq = pos->frequency;
989 
990 		if (freq == target_freq)
991 			return idx;
992 
993 		if (freq > target_freq) {
994 			best = idx;
995 			continue;
996 		}
997 
998 		/* No freq found above target_freq */
999 		if (best == -1)
1000 			return idx;
1001 
1002 		/* Choose the closest freq */
1003 		if (table[best].frequency - target_freq > target_freq - freq)
1004 			return idx;
1005 
1006 		return best;
1007 	}
1008 
1009 	return best;
1010 }
1011 
1012 /* Works only on sorted freq-tables */
1013 static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy,
1014 					     unsigned int target_freq,
1015 					     bool efficiencies)
1016 {
1017 	target_freq = clamp_val(target_freq, policy->min, policy->max);
1018 
1019 	if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
1020 		return cpufreq_table_find_index_ac(policy, target_freq,
1021 						   efficiencies);
1022 	else
1023 		return cpufreq_table_find_index_dc(policy, target_freq,
1024 						   efficiencies);
1025 }
1026 
1027 static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
1028 						 unsigned int target_freq,
1029 						 unsigned int relation)
1030 {
1031 	bool efficiencies = policy->efficiencies_available &&
1032 			    (relation & CPUFREQ_RELATION_E);
1033 	int idx;
1034 
1035 	/* cpufreq_table_index_unsorted() has no use for this flag anyway */
1036 	relation &= ~CPUFREQ_RELATION_E;
1037 
1038 	if (unlikely(policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED))
1039 		return cpufreq_table_index_unsorted(policy, target_freq,
1040 						    relation);
1041 retry:
1042 	switch (relation) {
1043 	case CPUFREQ_RELATION_L:
1044 		idx = cpufreq_table_find_index_l(policy, target_freq,
1045 						 efficiencies);
1046 		break;
1047 	case CPUFREQ_RELATION_H:
1048 		idx = cpufreq_table_find_index_h(policy, target_freq,
1049 						 efficiencies);
1050 		break;
1051 	case CPUFREQ_RELATION_C:
1052 		idx = cpufreq_table_find_index_c(policy, target_freq,
1053 						 efficiencies);
1054 		break;
1055 	default:
1056 		WARN_ON_ONCE(1);
1057 		return 0;
1058 	}
1059 
1060 	if (idx < 0 && efficiencies) {
1061 		efficiencies = false;
1062 		goto retry;
1063 	}
1064 
1065 	return idx;
1066 }
1067 
1068 static inline int cpufreq_table_count_valid_entries(const struct cpufreq_policy *policy)
1069 {
1070 	struct cpufreq_frequency_table *pos;
1071 	int count = 0;
1072 
1073 	if (unlikely(!policy->freq_table))
1074 		return 0;
1075 
1076 	cpufreq_for_each_valid_entry(pos, policy->freq_table)
1077 		count++;
1078 
1079 	return count;
1080 }
1081 
1082 /**
1083  * cpufreq_table_set_inefficient() - Mark a frequency as inefficient
1084  * @policy:	the &struct cpufreq_policy containing the inefficient frequency
1085  * @frequency:	the inefficient frequency
1086  *
1087  * The &struct cpufreq_policy must use a sorted frequency table
1088  *
1089  * Return:	%0 on success or a negative errno code
1090  */
1091 
1092 static inline int
1093 cpufreq_table_set_inefficient(struct cpufreq_policy *policy,
1094 			      unsigned int frequency)
1095 {
1096 	struct cpufreq_frequency_table *pos;
1097 
1098 	/* Not supported */
1099 	if (policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED)
1100 		return -EINVAL;
1101 
1102 	cpufreq_for_each_valid_entry(pos, policy->freq_table) {
1103 		if (pos->frequency == frequency) {
1104 			pos->flags |= CPUFREQ_INEFFICIENT_FREQ;
1105 			policy->efficiencies_available = true;
1106 			return 0;
1107 		}
1108 	}
1109 
1110 	return -EINVAL;
1111 }
1112 
1113 static inline int parse_perf_domain(int cpu, const char *list_name,
1114 				    const char *cell_name,
1115 				    struct of_phandle_args *args)
1116 {
1117 	struct device_node *cpu_np;
1118 	int ret;
1119 
1120 	cpu_np = of_cpu_device_node_get(cpu);
1121 	if (!cpu_np)
1122 		return -ENODEV;
1123 
1124 	ret = of_parse_phandle_with_args(cpu_np, list_name, cell_name, 0,
1125 					 args);
1126 	if (ret < 0)
1127 		return ret;
1128 
1129 	of_node_put(cpu_np);
1130 
1131 	return 0;
1132 }
1133 
1134 static inline int of_perf_domain_get_sharing_cpumask(int pcpu, const char *list_name,
1135 						     const char *cell_name, struct cpumask *cpumask,
1136 						     struct of_phandle_args *pargs)
1137 {
1138 	int cpu, ret;
1139 	struct of_phandle_args args;
1140 
1141 	ret = parse_perf_domain(pcpu, list_name, cell_name, pargs);
1142 	if (ret < 0)
1143 		return ret;
1144 
1145 	cpumask_set_cpu(pcpu, cpumask);
1146 
1147 	for_each_possible_cpu(cpu) {
1148 		if (cpu == pcpu)
1149 			continue;
1150 
1151 		ret = parse_perf_domain(cpu, list_name, cell_name, &args);
1152 		if (ret < 0)
1153 			continue;
1154 
1155 		if (pargs->np == args.np && pargs->args_count == args.args_count &&
1156 		    !memcmp(pargs->args, args.args, sizeof(args.args[0]) * args.args_count))
1157 			cpumask_set_cpu(cpu, cpumask);
1158 
1159 		of_node_put(args.np);
1160 	}
1161 
1162 	return 0;
1163 }
1164 #else
1165 static inline int cpufreq_boost_trigger_state(int state)
1166 {
1167 	return 0;
1168 }
1169 static inline int cpufreq_boost_enabled(void)
1170 {
1171 	return 0;
1172 }
1173 
1174 static inline int cpufreq_enable_boost_support(void)
1175 {
1176 	return -EINVAL;
1177 }
1178 
1179 static inline bool policy_has_boost_freq(struct cpufreq_policy *policy)
1180 {
1181 	return false;
1182 }
1183 
1184 static inline int
1185 cpufreq_table_set_inefficient(struct cpufreq_policy *policy,
1186 			      unsigned int frequency)
1187 {
1188 	return -EINVAL;
1189 }
1190 
1191 static inline int of_perf_domain_get_sharing_cpumask(int pcpu, const char *list_name,
1192 						     const char *cell_name, struct cpumask *cpumask,
1193 						     struct of_phandle_args *pargs)
1194 {
1195 	return -EOPNOTSUPP;
1196 }
1197 #endif
1198 
1199 #if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
1200 void sched_cpufreq_governor_change(struct cpufreq_policy *policy,
1201 			struct cpufreq_governor *old_gov);
1202 #else
1203 static inline void sched_cpufreq_governor_change(struct cpufreq_policy *policy,
1204 			struct cpufreq_governor *old_gov) { }
1205 #endif
1206 
1207 extern unsigned int arch_freq_get_on_cpu(int cpu);
1208 
1209 #ifndef arch_set_freq_scale
1210 static __always_inline
1211 void arch_set_freq_scale(const struct cpumask *cpus,
1212 			 unsigned long cur_freq,
1213 			 unsigned long max_freq)
1214 {
1215 }
1216 #endif
1217 /* the following are really really optional */
1218 extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
1219 extern struct freq_attr cpufreq_freq_attr_scaling_boost_freqs;
1220 extern struct freq_attr *cpufreq_generic_attr[];
1221 int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy);
1222 
1223 unsigned int cpufreq_generic_get(unsigned int cpu);
1224 void cpufreq_generic_init(struct cpufreq_policy *policy,
1225 		struct cpufreq_frequency_table *table,
1226 		unsigned int transition_latency);
1227 
1228 static inline void cpufreq_register_em_with_opp(struct cpufreq_policy *policy)
1229 {
1230 	dev_pm_opp_of_register_em(get_cpu_device(policy->cpu),
1231 				  policy->related_cpus);
1232 }
1233 #endif /* _LINUX_CPUFREQ_H */
1234