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