xref: /linux-6.15/include/linux/cpufreq.h (revision b454cc66)
1 /*
2  *  linux/include/linux/cpufreq.h
3  *
4  *  Copyright (C) 2001 Russell King
5  *            (C) 2002 - 2003 Dominik Brodowski <[email protected]>
6  *
7  *
8  * $Id: cpufreq.h,v 1.36 2003/01/20 17:31:48 db Exp $
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14 #ifndef _LINUX_CPUFREQ_H
15 #define _LINUX_CPUFREQ_H
16 
17 #include <linux/mutex.h>
18 #include <linux/notifier.h>
19 #include <linux/threads.h>
20 #include <linux/device.h>
21 #include <linux/kobject.h>
22 #include <linux/sysfs.h>
23 #include <linux/completion.h>
24 #include <linux/workqueue.h>
25 #include <linux/cpumask.h>
26 #include <asm/div64.h>
27 
28 #define CPUFREQ_NAME_LEN 16
29 
30 
31 /*********************************************************************
32  *                     CPUFREQ NOTIFIER INTERFACE                    *
33  *********************************************************************/
34 
35 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
36 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
37 
38 #define CPUFREQ_TRANSITION_NOTIFIER	(0)
39 #define CPUFREQ_POLICY_NOTIFIER		(1)
40 
41 
42 /* if (cpufreq_driver->target) exists, the ->governor decides what frequency
43  * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
44  * two generic policies are available:
45  */
46 
47 #define CPUFREQ_POLICY_POWERSAVE	(1)
48 #define CPUFREQ_POLICY_PERFORMANCE	(2)
49 
50 /* Frequency values here are CPU kHz so that hardware which doesn't run
51  * with some frequencies can complain without having to guess what per
52  * cent / per mille means.
53  * Maximum transition latency is in nanoseconds - if it's unknown,
54  * CPUFREQ_ETERNAL shall be used.
55  */
56 
57 struct cpufreq_governor;
58 
59 #define CPUFREQ_ETERNAL			(-1)
60 struct cpufreq_cpuinfo {
61 	unsigned int		max_freq;
62 	unsigned int		min_freq;
63 	unsigned int		transition_latency; /* in 10^(-9) s = nanoseconds */
64 };
65 
66 struct cpufreq_real_policy {
67 	unsigned int		min;    /* in kHz */
68 	unsigned int		max;    /* in kHz */
69         unsigned int		policy; /* see above */
70 	struct cpufreq_governor	*governor; /* see below */
71 };
72 
73 struct cpufreq_policy {
74 	cpumask_t		cpus;	/* affected CPUs */
75 	unsigned int		shared_type; /* ANY or ALL affected CPUs
76 						should set cpufreq */
77 	unsigned int		cpu;    /* cpu nr of registered CPU */
78 	struct cpufreq_cpuinfo	cpuinfo;/* see above */
79 
80 	unsigned int		min;    /* in kHz */
81 	unsigned int		max;    /* in kHz */
82 	unsigned int		cur;    /* in kHz, only needed if cpufreq
83 					 * governors are used */
84         unsigned int		policy; /* see above */
85 	struct cpufreq_governor	*governor; /* see below */
86 
87  	struct mutex		lock;   /* CPU ->setpolicy or ->target may
88 					   only be called once a time */
89 
90 	struct work_struct	update; /* if update_policy() needs to be
91 					 * called, but you're in IRQ context */
92 
93 	struct cpufreq_real_policy	user_policy;
94 
95 	struct kobject		kobj;
96 	struct completion	kobj_unregister;
97 };
98 
99 #define CPUFREQ_ADJUST		(0)
100 #define CPUFREQ_INCOMPATIBLE	(1)
101 #define CPUFREQ_NOTIFY		(2)
102 
103 #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */
104 #define CPUFREQ_SHARED_TYPE_HW	 (1) /* HW does needed coordination */
105 #define CPUFREQ_SHARED_TYPE_ALL	 (2) /* All dependent CPUs should set freq */
106 #define CPUFREQ_SHARED_TYPE_ANY	 (3) /* Freq can be set from any dependent CPU*/
107 
108 /******************** cpufreq transition notifiers *******************/
109 
110 #define CPUFREQ_PRECHANGE	(0)
111 #define CPUFREQ_POSTCHANGE	(1)
112 #define CPUFREQ_RESUMECHANGE	(8)
113 #define CPUFREQ_SUSPENDCHANGE	(9)
114 
115 struct cpufreq_freqs {
116 	unsigned int cpu;	/* cpu nr */
117 	unsigned int old;
118 	unsigned int new;
119 	u8 flags;		/* flags of cpufreq_driver, see below. */
120 };
121 
122 
123 /**
124  * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch safe)
125  * @old:   old value
126  * @div:   divisor
127  * @mult:  multiplier
128  *
129  *
130  *    new = old * mult / div
131  */
132 static inline unsigned long cpufreq_scale(unsigned long old, u_int div, u_int mult)
133 {
134 #if BITS_PER_LONG == 32
135 
136 	u64 result = ((u64) old) * ((u64) mult);
137 	do_div(result, div);
138 	return (unsigned long) result;
139 
140 #elif BITS_PER_LONG == 64
141 
142 	unsigned long result = old * ((u64) mult);
143 	result /= div;
144 	return result;
145 
146 #endif
147 };
148 
149 /*********************************************************************
150  *                          CPUFREQ GOVERNORS                        *
151  *********************************************************************/
152 
153 #define CPUFREQ_GOV_START  1
154 #define CPUFREQ_GOV_STOP   2
155 #define CPUFREQ_GOV_LIMITS 3
156 
157 struct cpufreq_governor {
158 	char	name[CPUFREQ_NAME_LEN];
159 	int 	(*governor)	(struct cpufreq_policy *policy,
160 				 unsigned int event);
161 	struct list_head	governor_list;
162 	struct module		*owner;
163 };
164 
165 /* pass a target to the cpufreq driver
166  */
167 extern int cpufreq_driver_target(struct cpufreq_policy *policy,
168 				 unsigned int target_freq,
169 				 unsigned int relation);
170 extern int __cpufreq_driver_target(struct cpufreq_policy *policy,
171 				   unsigned int target_freq,
172 				   unsigned int relation);
173 
174 
175 extern int cpufreq_driver_getavg(struct cpufreq_policy *policy);
176 
177 int cpufreq_register_governor(struct cpufreq_governor *governor);
178 void cpufreq_unregister_governor(struct cpufreq_governor *governor);
179 
180 
181 /*********************************************************************
182  *                      CPUFREQ DRIVER INTERFACE                     *
183  *********************************************************************/
184 
185 #define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
186 #define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */
187 
188 struct freq_attr;
189 
190 struct cpufreq_driver {
191 	struct module           *owner;
192 	char			name[CPUFREQ_NAME_LEN];
193 	u8			flags;
194 
195 	/* needed by all drivers */
196 	int	(*init)		(struct cpufreq_policy *policy);
197 	int	(*verify)	(struct cpufreq_policy *policy);
198 
199 	/* define one out of two */
200 	int	(*setpolicy)	(struct cpufreq_policy *policy);
201 	int	(*target)	(struct cpufreq_policy *policy,
202 				 unsigned int target_freq,
203 				 unsigned int relation);
204 
205 	/* should be defined, if possible */
206 	unsigned int	(*get)	(unsigned int cpu);
207 
208 	/* optional */
209 	unsigned int (*getavg)	(unsigned int cpu);
210 	int	(*exit)		(struct cpufreq_policy *policy);
211 	int	(*suspend)	(struct cpufreq_policy *policy, pm_message_t pmsg);
212 	int	(*resume)	(struct cpufreq_policy *policy);
213 	struct freq_attr	**attr;
214 };
215 
216 /* flags */
217 
218 #define CPUFREQ_STICKY		0x01	/* the driver isn't removed even if
219 					 * all ->init() calls failed */
220 #define CPUFREQ_CONST_LOOPS 	0x02	/* loops_per_jiffy or other kernel
221 					 * "constants" aren't affected by
222 					 * frequency transitions */
223 #define CPUFREQ_PM_NO_WARN	0x04	/* don't warn on suspend/resume speed
224 					 * mismatches */
225 
226 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
227 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
228 
229 
230 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state);
231 
232 
233 static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned int min, unsigned int max)
234 {
235 	if (policy->min < min)
236 		policy->min = min;
237 	if (policy->max < min)
238 		policy->max = min;
239 	if (policy->min > max)
240 		policy->min = max;
241 	if (policy->max > max)
242 		policy->max = max;
243 	if (policy->min > policy->max)
244 		policy->min = policy->max;
245 	return;
246 }
247 
248 struct freq_attr {
249 	struct attribute attr;
250 	ssize_t (*show)(struct cpufreq_policy *, char *);
251 	ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
252 };
253 
254 
255 /*********************************************************************
256  *                        CPUFREQ 2.6. INTERFACE                     *
257  *********************************************************************/
258 int cpufreq_set_policy(struct cpufreq_policy *policy);
259 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
260 int cpufreq_update_policy(unsigned int cpu);
261 
262 /* query the current CPU frequency (in kHz). If zero, cpufreq couldn't detect it */
263 unsigned int cpufreq_get(unsigned int cpu);
264 
265 /* query the last known CPU freq (in kHz). If zero, cpufreq couldn't detect it */
266 #ifdef CONFIG_CPU_FREQ
267 unsigned int cpufreq_quick_get(unsigned int cpu);
268 #else
269 static inline unsigned int cpufreq_quick_get(unsigned int cpu)
270 {
271 	return 0;
272 }
273 #endif
274 
275 
276 /*********************************************************************
277  *                       CPUFREQ DEFAULT GOVERNOR                    *
278  *********************************************************************/
279 
280 
281 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
282 extern struct cpufreq_governor cpufreq_gov_performance;
283 #define CPUFREQ_DEFAULT_GOVERNOR	&cpufreq_gov_performance
284 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE)
285 extern struct cpufreq_governor cpufreq_gov_userspace;
286 #define CPUFREQ_DEFAULT_GOVERNOR	&cpufreq_gov_userspace
287 #endif
288 
289 
290 /*********************************************************************
291  *                     FREQUENCY TABLE HELPERS                       *
292  *********************************************************************/
293 
294 #define CPUFREQ_ENTRY_INVALID ~0
295 #define CPUFREQ_TABLE_END     ~1
296 
297 struct cpufreq_frequency_table {
298 	unsigned int	index;     /* any */
299 	unsigned int	frequency; /* kHz - doesn't need to be in ascending
300 				    * order */
301 };
302 
303 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
304 				    struct cpufreq_frequency_table *table);
305 
306 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
307 				   struct cpufreq_frequency_table *table);
308 
309 int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
310 				   struct cpufreq_frequency_table *table,
311 				   unsigned int target_freq,
312 				   unsigned int relation,
313 				   unsigned int *index);
314 
315 /* the following 3 funtions are for cpufreq core use only */
316 struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu);
317 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
318 void   cpufreq_cpu_put (struct cpufreq_policy *data);
319 
320 /* the following are really really optional */
321 extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
322 
323 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
324 				      unsigned int cpu);
325 
326 void cpufreq_frequency_table_put_attr(unsigned int cpu);
327 
328 
329 /*********************************************************************
330  *                     UNIFIED DEBUG HELPERS                         *
331  *********************************************************************/
332 
333 #define CPUFREQ_DEBUG_CORE	1
334 #define CPUFREQ_DEBUG_DRIVER	2
335 #define CPUFREQ_DEBUG_GOVERNOR	4
336 
337 #ifdef CONFIG_CPU_FREQ_DEBUG
338 
339 extern void cpufreq_debug_printk(unsigned int type, const char *prefix,
340 				 const char *fmt, ...);
341 
342 #else
343 
344 #define cpufreq_debug_printk(msg...) do { } while(0)
345 
346 #endif /* CONFIG_CPU_FREQ_DEBUG */
347 
348 #endif /* _LINUX_CPUFREQ_H */
349