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