xref: /linux-6.15/include/linux/cpufreq.h (revision e756bc56)
1 /*
2  * linux/include/linux/cpufreq.h
3  *
4  * Copyright (C) 2001 Russell King
5  *           (C) 2002 - 2003 Dominik Brodowski <[email protected]>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #ifndef _LINUX_CPUFREQ_H
12 #define _LINUX_CPUFREQ_H
13 
14 #include <linux/cpumask.h>
15 #include <linux/completion.h>
16 #include <linux/kobject.h>
17 #include <linux/notifier.h>
18 #include <linux/sysfs.h>
19 
20 /*********************************************************************
21  *                        CPUFREQ INTERFACE                          *
22  *********************************************************************/
23 /*
24  * Frequency values here are CPU kHz
25  *
26  * Maximum transition latency is in nanoseconds - if it's unknown,
27  * CPUFREQ_ETERNAL shall be used.
28  */
29 
30 #define CPUFREQ_ETERNAL			(-1)
31 #define CPUFREQ_NAME_LEN		16
32 /* Print length for names. Extra 1 space for accomodating '\n' in prints */
33 #define CPUFREQ_NAME_PLEN		(CPUFREQ_NAME_LEN + 1)
34 
35 struct cpufreq_governor;
36 
37 struct cpufreq_freqs {
38 	unsigned int cpu;	/* cpu nr */
39 	unsigned int old;
40 	unsigned int new;
41 	u8 flags;		/* flags of cpufreq_driver, see below. */
42 };
43 
44 struct cpufreq_cpuinfo {
45 	unsigned int		max_freq;
46 	unsigned int		min_freq;
47 
48 	/* in 10^(-9) s = nanoseconds */
49 	unsigned int		transition_latency;
50 };
51 
52 struct cpufreq_real_policy {
53 	unsigned int		min;    /* in kHz */
54 	unsigned int		max;    /* in kHz */
55 	unsigned int		policy; /* see above */
56 	struct cpufreq_governor	*governor; /* see below */
57 };
58 
59 struct cpufreq_policy {
60 	/* CPUs sharing clock, require sw coordination */
61 	cpumask_var_t		cpus;	/* Online CPUs only */
62 	cpumask_var_t		related_cpus; /* Online + Offline CPUs */
63 
64 	unsigned int		shared_type; /* ACPI: ANY or ALL affected CPUs
65 						should set cpufreq */
66 	unsigned int		cpu;    /* cpu nr of CPU managing this policy */
67 	unsigned int		last_cpu; /* cpu nr of previous CPU that managed
68 					   * this policy */
69 	struct cpufreq_cpuinfo	cpuinfo;/* see above */
70 
71 	unsigned int		min;    /* in kHz */
72 	unsigned int		max;    /* in kHz */
73 	unsigned int		cur;    /* in kHz, only needed if cpufreq
74 					 * governors are used */
75 	unsigned int		policy; /* see above */
76 	struct cpufreq_governor	*governor; /* see below */
77 	void			*governor_data;
78 	bool			governor_enabled; /* governor start/stop flag */
79 
80 	struct work_struct	update; /* if update_policy() needs to be
81 					 * called, but you're in IRQ context */
82 
83 	struct cpufreq_real_policy	user_policy;
84 
85 	struct list_head        policy_list;
86 	struct kobject		kobj;
87 	struct completion	kobj_unregister;
88 
89 	/*
90 	 * The rules for this semaphore:
91 	 * - Any routine that wants to read from the policy structure will
92 	 *   do a down_read on this semaphore.
93 	 * - Any routine that will write to the policy structure and/or may take away
94 	 *   the policy altogether (eg. CPU hotplug), will hold this lock in write
95 	 *   mode before doing so.
96 	 *
97 	 * Additional rules:
98 	 * - Lock should not be held across
99 	 *     __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
100 	 */
101 	struct rw_semaphore	rwsem;
102 };
103 
104 /* Only for ACPI */
105 #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */
106 #define CPUFREQ_SHARED_TYPE_HW	 (1) /* HW does needed coordination */
107 #define CPUFREQ_SHARED_TYPE_ALL	 (2) /* All dependent CPUs should set freq */
108 #define CPUFREQ_SHARED_TYPE_ANY	 (3) /* Freq can be set from any dependent CPU*/
109 
110 #ifdef CONFIG_CPU_FREQ
111 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
112 void cpufreq_cpu_put(struct cpufreq_policy *policy);
113 #else
114 static inline struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
115 {
116 	return NULL;
117 }
118 static inline void cpufreq_cpu_put(struct cpufreq_policy *policy) { }
119 #endif
120 
121 static inline bool policy_is_shared(struct cpufreq_policy *policy)
122 {
123 	return cpumask_weight(policy->cpus) > 1;
124 }
125 
126 /* /sys/devices/system/cpu/cpufreq: entry point for global variables */
127 extern struct kobject *cpufreq_global_kobject;
128 int cpufreq_get_global_kobject(void);
129 void cpufreq_put_global_kobject(void);
130 int cpufreq_sysfs_create_file(const struct attribute *attr);
131 void cpufreq_sysfs_remove_file(const struct attribute *attr);
132 
133 #ifdef CONFIG_CPU_FREQ
134 unsigned int cpufreq_get(unsigned int cpu);
135 unsigned int cpufreq_quick_get(unsigned int cpu);
136 unsigned int cpufreq_quick_get_max(unsigned int cpu);
137 void disable_cpufreq(void);
138 
139 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy);
140 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
141 int cpufreq_update_policy(unsigned int cpu);
142 bool have_governor_per_policy(void);
143 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy);
144 #else
145 static inline unsigned int cpufreq_get(unsigned int cpu)
146 {
147 	return 0;
148 }
149 static inline unsigned int cpufreq_quick_get(unsigned int cpu)
150 {
151 	return 0;
152 }
153 static inline unsigned int cpufreq_quick_get_max(unsigned int cpu)
154 {
155 	return 0;
156 }
157 static inline void disable_cpufreq(void) { }
158 #endif
159 
160 /*********************************************************************
161  *                      CPUFREQ DRIVER INTERFACE                     *
162  *********************************************************************/
163 
164 #define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
165 #define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */
166 
167 struct freq_attr {
168 	struct attribute attr;
169 	ssize_t (*show)(struct cpufreq_policy *, char *);
170 	ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
171 };
172 
173 #define cpufreq_freq_attr_ro(_name)		\
174 static struct freq_attr _name =			\
175 __ATTR(_name, 0444, show_##_name, NULL)
176 
177 #define cpufreq_freq_attr_ro_perm(_name, _perm)	\
178 static struct freq_attr _name =			\
179 __ATTR(_name, _perm, show_##_name, NULL)
180 
181 #define cpufreq_freq_attr_rw(_name)		\
182 static struct freq_attr _name =			\
183 __ATTR(_name, 0644, show_##_name, store_##_name)
184 
185 struct global_attr {
186 	struct attribute attr;
187 	ssize_t (*show)(struct kobject *kobj,
188 			struct attribute *attr, char *buf);
189 	ssize_t (*store)(struct kobject *a, struct attribute *b,
190 			 const char *c, size_t count);
191 };
192 
193 #define define_one_global_ro(_name)		\
194 static struct global_attr _name =		\
195 __ATTR(_name, 0444, show_##_name, NULL)
196 
197 #define define_one_global_rw(_name)		\
198 static struct global_attr _name =		\
199 __ATTR(_name, 0644, show_##_name, store_##_name)
200 
201 
202 struct cpufreq_driver {
203 	char			name[CPUFREQ_NAME_LEN];
204 	u8			flags;
205 
206 	/* needed by all drivers */
207 	int	(*init)		(struct cpufreq_policy *policy);
208 	int	(*verify)	(struct cpufreq_policy *policy);
209 
210 	/* define one out of two */
211 	int	(*setpolicy)	(struct cpufreq_policy *policy);
212 	int	(*target)	(struct cpufreq_policy *policy,	/* Deprecated */
213 				 unsigned int target_freq,
214 				 unsigned int relation);
215 	int	(*target_index)	(struct cpufreq_policy *policy,
216 				 unsigned int index);
217 
218 	/* should be defined, if possible */
219 	unsigned int	(*get)	(unsigned int cpu);
220 
221 	/* optional */
222 	int	(*bios_limit)	(int cpu, unsigned int *limit);
223 
224 	int	(*exit)		(struct cpufreq_policy *policy);
225 	int	(*suspend)	(struct cpufreq_policy *policy);
226 	int	(*resume)	(struct cpufreq_policy *policy);
227 	struct freq_attr	**attr;
228 };
229 
230 /* flags */
231 #define CPUFREQ_STICKY		(1 << 0)	/* driver isn't removed even if
232 						   all ->init() calls failed */
233 #define CPUFREQ_CONST_LOOPS	(1 << 1)	/* loops_per_jiffy or other
234 						   kernel "constants" aren't
235 						   affected by frequency
236 						   transitions */
237 #define CPUFREQ_PM_NO_WARN	(1 << 2)	/* don't warn on suspend/resume
238 						   speed mismatches */
239 
240 /*
241  * This should be set by platforms having multiple clock-domains, i.e.
242  * supporting multiple policies. With this sysfs directories of governor would
243  * be created in cpu/cpu<num>/cpufreq/ directory and so they can use the same
244  * governor with different tunables for different clusters.
245  */
246 #define CPUFREQ_HAVE_GOVERNOR_PER_POLICY (1 << 3)
247 
248 /*
249  * Driver will do POSTCHANGE notifications from outside of their ->target()
250  * routine and so must set cpufreq_driver->flags with this flag, so that core
251  * can handle them specially.
252  */
253 #define CPUFREQ_ASYNC_NOTIFICATION  (1 << 4)
254 
255 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
256 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
257 
258 const char *cpufreq_get_current_driver(void);
259 
260 static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy,
261 		unsigned int min, unsigned int max)
262 {
263 	if (policy->min < min)
264 		policy->min = min;
265 	if (policy->max < min)
266 		policy->max = min;
267 	if (policy->min > max)
268 		policy->min = max;
269 	if (policy->max > max)
270 		policy->max = max;
271 	if (policy->min > policy->max)
272 		policy->min = policy->max;
273 	return;
274 }
275 
276 static inline void
277 cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy)
278 {
279 	cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
280 			policy->cpuinfo.max_freq);
281 }
282 
283 /*********************************************************************
284  *                     CPUFREQ NOTIFIER INTERFACE                    *
285  *********************************************************************/
286 
287 #define CPUFREQ_TRANSITION_NOTIFIER	(0)
288 #define CPUFREQ_POLICY_NOTIFIER		(1)
289 
290 /* Transition notifiers */
291 #define CPUFREQ_PRECHANGE		(0)
292 #define CPUFREQ_POSTCHANGE		(1)
293 #define CPUFREQ_RESUMECHANGE		(8)
294 #define CPUFREQ_SUSPENDCHANGE		(9)
295 
296 /* Policy Notifiers  */
297 #define CPUFREQ_ADJUST			(0)
298 #define CPUFREQ_INCOMPATIBLE		(1)
299 #define CPUFREQ_NOTIFY			(2)
300 #define CPUFREQ_START			(3)
301 #define CPUFREQ_UPDATE_POLICY_CPU	(4)
302 
303 #ifdef CONFIG_CPU_FREQ
304 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
305 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
306 
307 void cpufreq_notify_transition(struct cpufreq_policy *policy,
308 		struct cpufreq_freqs *freqs, unsigned int state);
309 
310 #else /* CONFIG_CPU_FREQ */
311 static inline int cpufreq_register_notifier(struct notifier_block *nb,
312 						unsigned int list)
313 {
314 	return 0;
315 }
316 static inline int cpufreq_unregister_notifier(struct notifier_block *nb,
317 						unsigned int list)
318 {
319 	return 0;
320 }
321 #endif /* !CONFIG_CPU_FREQ */
322 
323 /**
324  * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch
325  * safe)
326  * @old:   old value
327  * @div:   divisor
328  * @mult:  multiplier
329  *
330  *
331  * new = old * mult / div
332  */
333 static inline unsigned long cpufreq_scale(unsigned long old, u_int div,
334 		u_int mult)
335 {
336 #if BITS_PER_LONG == 32
337 	u64 result = ((u64) old) * ((u64) mult);
338 	do_div(result, div);
339 	return (unsigned long) result;
340 
341 #elif BITS_PER_LONG == 64
342 	unsigned long result = old * ((u64) mult);
343 	result /= div;
344 	return result;
345 #endif
346 }
347 
348 /*********************************************************************
349  *                          CPUFREQ GOVERNORS                        *
350  *********************************************************************/
351 
352 /*
353  * If (cpufreq_driver->target) exists, the ->governor decides what frequency
354  * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
355  * two generic policies are available:
356  */
357 #define CPUFREQ_POLICY_POWERSAVE	(1)
358 #define CPUFREQ_POLICY_PERFORMANCE	(2)
359 
360 /* Governor Events */
361 #define CPUFREQ_GOV_START	1
362 #define CPUFREQ_GOV_STOP	2
363 #define CPUFREQ_GOV_LIMITS	3
364 #define CPUFREQ_GOV_POLICY_INIT	4
365 #define CPUFREQ_GOV_POLICY_EXIT	5
366 
367 struct cpufreq_governor {
368 	char	name[CPUFREQ_NAME_LEN];
369 	int	initialized;
370 	int	(*governor)	(struct cpufreq_policy *policy,
371 				 unsigned int event);
372 	ssize_t	(*show_setspeed)	(struct cpufreq_policy *policy,
373 					 char *buf);
374 	int	(*store_setspeed)	(struct cpufreq_policy *policy,
375 					 unsigned int freq);
376 	unsigned int max_transition_latency; /* HW must be able to switch to
377 			next freq faster than this value in nano secs or we
378 			will fallback to performance governor */
379 	struct list_head	governor_list;
380 	struct module		*owner;
381 };
382 
383 /* Pass a target to the cpufreq driver */
384 int cpufreq_driver_target(struct cpufreq_policy *policy,
385 				 unsigned int target_freq,
386 				 unsigned int relation);
387 int __cpufreq_driver_target(struct cpufreq_policy *policy,
388 				   unsigned int target_freq,
389 				   unsigned int relation);
390 int cpufreq_register_governor(struct cpufreq_governor *governor);
391 void cpufreq_unregister_governor(struct cpufreq_governor *governor);
392 
393 /* CPUFREQ DEFAULT GOVERNOR */
394 /*
395  * Performance governor is fallback governor if any other gov failed to auto
396  * load due latency restrictions
397  */
398 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
399 extern struct cpufreq_governor cpufreq_gov_performance;
400 #endif
401 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
402 #define CPUFREQ_DEFAULT_GOVERNOR	(&cpufreq_gov_performance)
403 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE)
404 extern struct cpufreq_governor cpufreq_gov_powersave;
405 #define CPUFREQ_DEFAULT_GOVERNOR	(&cpufreq_gov_powersave)
406 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE)
407 extern struct cpufreq_governor cpufreq_gov_userspace;
408 #define CPUFREQ_DEFAULT_GOVERNOR	(&cpufreq_gov_userspace)
409 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND)
410 extern struct cpufreq_governor cpufreq_gov_ondemand;
411 #define CPUFREQ_DEFAULT_GOVERNOR	(&cpufreq_gov_ondemand)
412 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE)
413 extern struct cpufreq_governor cpufreq_gov_conservative;
414 #define CPUFREQ_DEFAULT_GOVERNOR	(&cpufreq_gov_conservative)
415 #endif
416 
417 /*********************************************************************
418  *                     FREQUENCY TABLE HELPERS                       *
419  *********************************************************************/
420 
421 #define CPUFREQ_ENTRY_INVALID ~0
422 #define CPUFREQ_TABLE_END     ~1
423 
424 struct cpufreq_frequency_table {
425 	unsigned int	driver_data; /* driver specific data, not used by core */
426 	unsigned int	frequency; /* kHz - doesn't need to be in ascending
427 				    * order */
428 };
429 
430 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
431 				    struct cpufreq_frequency_table *table);
432 
433 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
434 				   struct cpufreq_frequency_table *table);
435 int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy);
436 
437 int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
438 				   struct cpufreq_frequency_table *table,
439 				   unsigned int target_freq,
440 				   unsigned int relation,
441 				   unsigned int *index);
442 
443 void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy);
444 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf);
445 
446 /* the following funtion is for cpufreq core use only */
447 struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu);
448 
449 /* the following are really really optional */
450 extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
451 extern struct freq_attr *cpufreq_generic_attr[];
452 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
453 				      unsigned int cpu);
454 void cpufreq_frequency_table_put_attr(unsigned int cpu);
455 int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
456 				      struct cpufreq_frequency_table *table);
457 
458 int cpufreq_generic_init(struct cpufreq_policy *policy,
459 		struct cpufreq_frequency_table *table,
460 		unsigned int transition_latency);
461 static inline int cpufreq_generic_exit(struct cpufreq_policy *policy)
462 {
463 	cpufreq_frequency_table_put_attr(policy->cpu);
464 	return 0;
465 }
466 
467 #endif /* _LINUX_CPUFREQ_H */
468