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