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