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