xref: /linux-6.15/include/linux/cpufreq.h (revision 5e8d780d)
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_ALL	(0) /* All dependent CPUs should set freq */
104 #define CPUFREQ_SHARED_TYPE_ANY	(1) /* Freq can be set from any dependent CPU */
105 
106 /******************** cpufreq transition notifiers *******************/
107 
108 #define CPUFREQ_PRECHANGE	(0)
109 #define CPUFREQ_POSTCHANGE	(1)
110 #define CPUFREQ_RESUMECHANGE	(8)
111 #define CPUFREQ_SUSPENDCHANGE	(9)
112 
113 struct cpufreq_freqs {
114 	unsigned int cpu;	/* cpu nr */
115 	unsigned int old;
116 	unsigned int new;
117 	u8 flags;		/* flags of cpufreq_driver, see below. */
118 };
119 
120 
121 /**
122  * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch safe)
123  * @old:   old value
124  * @div:   divisor
125  * @mult:  multiplier
126  *
127  *
128  *    new = old * mult / div
129  */
130 static inline unsigned long cpufreq_scale(unsigned long old, u_int div, u_int mult)
131 {
132 #if BITS_PER_LONG == 32
133 
134 	u64 result = ((u64) old) * ((u64) mult);
135 	do_div(result, div);
136 	return (unsigned long) result;
137 
138 #elif BITS_PER_LONG == 64
139 
140 	unsigned long result = old * ((u64) mult);
141 	result /= div;
142 	return result;
143 
144 #endif
145 };
146 
147 /*********************************************************************
148  *                          CPUFREQ GOVERNORS                        *
149  *********************************************************************/
150 
151 #define CPUFREQ_GOV_START  1
152 #define CPUFREQ_GOV_STOP   2
153 #define CPUFREQ_GOV_LIMITS 3
154 
155 struct cpufreq_governor {
156 	char	name[CPUFREQ_NAME_LEN];
157 	int 	(*governor)	(struct cpufreq_policy *policy,
158 				 unsigned int event);
159 	struct list_head	governor_list;
160 	struct module		*owner;
161 };
162 
163 /* pass a target to the cpufreq driver
164  */
165 extern int cpufreq_driver_target(struct cpufreq_policy *policy,
166 				 unsigned int target_freq,
167 				 unsigned int relation);
168 extern int __cpufreq_driver_target(struct cpufreq_policy *policy,
169 				   unsigned int target_freq,
170 				   unsigned int relation);
171 
172 
173 /* pass an event to the cpufreq governor */
174 int cpufreq_governor(unsigned int cpu, unsigned int event);
175 
176 int cpufreq_register_governor(struct cpufreq_governor *governor);
177 void cpufreq_unregister_governor(struct cpufreq_governor *governor);
178 
179 
180 /*********************************************************************
181  *                      CPUFREQ DRIVER INTERFACE                     *
182  *********************************************************************/
183 
184 #define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
185 #define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */
186 
187 struct freq_attr;
188 
189 struct cpufreq_driver {
190 	struct module           *owner;
191 	char			name[CPUFREQ_NAME_LEN];
192 	u8			flags;
193 
194 	/* needed by all drivers */
195 	int	(*init)		(struct cpufreq_policy *policy);
196 	int	(*verify)	(struct cpufreq_policy *policy);
197 
198 	/* define one out of two */
199 	int	(*setpolicy)	(struct cpufreq_policy *policy);
200 	int	(*target)	(struct cpufreq_policy *policy,
201 				 unsigned int target_freq,
202 				 unsigned int relation);
203 
204 	/* should be defined, if possible */
205 	unsigned int	(*get)	(unsigned int cpu);
206 
207 	/* optional */
208 	int	(*exit)		(struct cpufreq_policy *policy);
209 	int	(*suspend)	(struct cpufreq_policy *policy, pm_message_t pmsg);
210 	int	(*resume)	(struct cpufreq_policy *policy);
211 	struct freq_attr	**attr;
212 };
213 
214 /* flags */
215 
216 #define CPUFREQ_STICKY		0x01	/* the driver isn't removed even if
217 					 * all ->init() calls failed */
218 #define CPUFREQ_CONST_LOOPS 	0x02	/* loops_per_jiffy or other kernel
219 					 * "constants" aren't affected by
220 					 * frequency transitions */
221 #define CPUFREQ_PM_NO_WARN	0x04	/* don't warn on suspend/resume speed
222 					 * mismatches */
223 
224 int cpufreq_register_driver(struct cpufreq_driver *driver_data);
225 int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
226 
227 
228 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state);
229 
230 
231 static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned int min, unsigned int max)
232 {
233 	if (policy->min < min)
234 		policy->min = min;
235 	if (policy->max < min)
236 		policy->max = min;
237 	if (policy->min > max)
238 		policy->min = max;
239 	if (policy->max > max)
240 		policy->max = max;
241 	if (policy->min > policy->max)
242 		policy->min = policy->max;
243 	return;
244 }
245 
246 struct freq_attr {
247 	struct attribute attr;
248 	ssize_t (*show)(struct cpufreq_policy *, char *);
249 	ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
250 };
251 
252 
253 /*********************************************************************
254  *                        CPUFREQ 2.6. INTERFACE                     *
255  *********************************************************************/
256 int cpufreq_set_policy(struct cpufreq_policy *policy);
257 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
258 int cpufreq_update_policy(unsigned int cpu);
259 
260 /* query the current CPU frequency (in kHz). If zero, cpufreq couldn't detect it */
261 unsigned int cpufreq_get(unsigned int cpu);
262 
263 /* query the last known CPU freq (in kHz). If zero, cpufreq couldn't detect it */
264 #ifdef CONFIG_CPU_FREQ
265 unsigned int cpufreq_quick_get(unsigned int cpu);
266 #else
267 static inline unsigned int cpufreq_quick_get(unsigned int cpu)
268 {
269 	return 0;
270 }
271 #endif
272 
273 
274 /*********************************************************************
275  *                       CPUFREQ DEFAULT GOVERNOR                    *
276  *********************************************************************/
277 
278 
279 #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
280 extern struct cpufreq_governor cpufreq_gov_performance;
281 #define CPUFREQ_DEFAULT_GOVERNOR	&cpufreq_gov_performance
282 #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE)
283 extern struct cpufreq_governor cpufreq_gov_userspace;
284 #define CPUFREQ_DEFAULT_GOVERNOR	&cpufreq_gov_userspace
285 #endif
286 
287 
288 /*********************************************************************
289  *                     FREQUENCY TABLE HELPERS                       *
290  *********************************************************************/
291 
292 #define CPUFREQ_ENTRY_INVALID ~0
293 #define CPUFREQ_TABLE_END     ~1
294 
295 struct cpufreq_frequency_table {
296 	unsigned int	index;     /* any */
297 	unsigned int	frequency; /* kHz - doesn't need to be in ascending
298 				    * order */
299 };
300 
301 int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
302 				    struct cpufreq_frequency_table *table);
303 
304 int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
305 				   struct cpufreq_frequency_table *table);
306 
307 int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
308 				   struct cpufreq_frequency_table *table,
309 				   unsigned int target_freq,
310 				   unsigned int relation,
311 				   unsigned int *index);
312 
313 /* the following 3 funtions are for cpufreq core use only */
314 struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu);
315 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
316 void   cpufreq_cpu_put (struct cpufreq_policy *data);
317 
318 /* the following are really really optional */
319 extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
320 
321 void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
322 				      unsigned int cpu);
323 
324 void cpufreq_frequency_table_put_attr(unsigned int cpu);
325 
326 
327 /*********************************************************************
328  *                     UNIFIED DEBUG HELPERS                         *
329  *********************************************************************/
330 
331 #define CPUFREQ_DEBUG_CORE	1
332 #define CPUFREQ_DEBUG_DRIVER	2
333 #define CPUFREQ_DEBUG_GOVERNOR	4
334 
335 #ifdef CONFIG_CPU_FREQ_DEBUG
336 
337 extern void cpufreq_debug_printk(unsigned int type, const char *prefix,
338 				 const char *fmt, ...);
339 
340 #else
341 
342 #define cpufreq_debug_printk(msg...) do { } while(0)
343 
344 #endif /* CONFIG_CPU_FREQ_DEBUG */
345 
346 #endif /* _LINUX_CPUFREQ_H */
347