xref: /linux-6.15/include/linux/devfreq.h (revision 1ebd0bc0)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
4  *	    for Non-CPU Devices.
5  *
6  * Copyright (C) 2011 Samsung Electronics
7  *	MyungJoo Ham <[email protected]>
8  */
9 
10 #ifndef __LINUX_DEVFREQ_H__
11 #define __LINUX_DEVFREQ_H__
12 
13 #include <linux/device.h>
14 #include <linux/notifier.h>
15 #include <linux/pm_opp.h>
16 #include <linux/pm_qos.h>
17 
18 #define DEVFREQ_NAME_LEN 16
19 
20 /* DEVFREQ governor name */
21 #define DEVFREQ_GOV_SIMPLE_ONDEMAND	"simple_ondemand"
22 #define DEVFREQ_GOV_PERFORMANCE		"performance"
23 #define DEVFREQ_GOV_POWERSAVE		"powersave"
24 #define DEVFREQ_GOV_USERSPACE		"userspace"
25 #define DEVFREQ_GOV_PASSIVE		"passive"
26 
27 /* DEVFREQ notifier interface */
28 #define DEVFREQ_TRANSITION_NOTIFIER	(0)
29 
30 /* Transition notifiers of DEVFREQ_TRANSITION_NOTIFIER */
31 #define	DEVFREQ_PRECHANGE		(0)
32 #define DEVFREQ_POSTCHANGE		(1)
33 
34 struct devfreq;
35 struct devfreq_governor;
36 
37 /**
38  * struct devfreq_dev_status - Data given from devfreq user device to
39  *			     governors. Represents the performance
40  *			     statistics.
41  * @total_time:		The total time represented by this instance of
42  *			devfreq_dev_status
43  * @busy_time:		The time that the device was working among the
44  *			total_time.
45  * @current_frequency:	The operating frequency.
46  * @private_data:	An entry not specified by the devfreq framework.
47  *			A device and a specific governor may have their
48  *			own protocol with private_data. However, because
49  *			this is governor-specific, a governor using this
50  *			will be only compatible with devices aware of it.
51  */
52 struct devfreq_dev_status {
53 	/* both since the last measure */
54 	unsigned long total_time;
55 	unsigned long busy_time;
56 	unsigned long current_frequency;
57 	void *private_data;
58 };
59 
60 /*
61  * The resulting frequency should be at most this. (this bound is the
62  * least upper bound; thus, the resulting freq should be lower or same)
63  * If the flag is not set, the resulting frequency should be at most the
64  * bound (greatest lower bound)
65  */
66 #define DEVFREQ_FLAG_LEAST_UPPER_BOUND		0x1
67 
68 /**
69  * struct devfreq_dev_profile - Devfreq's user device profile
70  * @initial_freq:	The operating frequency when devfreq_add_device() is
71  *			called.
72  * @polling_ms:		The polling interval in ms. 0 disables polling.
73  * @target:		The device should set its operating frequency at
74  *			freq or lowest-upper-than-freq value. If freq is
75  *			higher than any operable frequency, set maximum.
76  *			Before returning, target function should set
77  *			freq at the current frequency.
78  *			The "flags" parameter's possible values are
79  *			explained above with "DEVFREQ_FLAG_*" macros.
80  * @get_dev_status:	The device should provide the current performance
81  *			status to devfreq. Governors are recommended not to
82  *			use this directly. Instead, governors are recommended
83  *			to use devfreq_update_stats() along with
84  *			devfreq.last_status.
85  * @get_cur_freq:	The device should provide the current frequency
86  *			at which it is operating.
87  * @exit:		An optional callback that is called when devfreq
88  *			is removing the devfreq object due to error or
89  *			from devfreq_remove_device() call. If the user
90  *			has registered devfreq->nb at a notifier-head,
91  *			this is the time to unregister it.
92  * @freq_table:		Optional list of frequencies to support statistics
93  *			and freq_table must be generated in ascending order.
94  * @max_state:		The size of freq_table.
95  */
96 struct devfreq_dev_profile {
97 	unsigned long initial_freq;
98 	unsigned int polling_ms;
99 
100 	int (*target)(struct device *dev, unsigned long *freq, u32 flags);
101 	int (*get_dev_status)(struct device *dev,
102 			      struct devfreq_dev_status *stat);
103 	int (*get_cur_freq)(struct device *dev, unsigned long *freq);
104 	void (*exit)(struct device *dev);
105 
106 	unsigned long *freq_table;
107 	unsigned int max_state;
108 };
109 
110 /**
111  * struct devfreq_stats - Statistics of devfreq device behavior
112  * @total_trans:	Number of devfreq transitions.
113  * @trans_table:	Statistics of devfreq transitions.
114  * @time_in_state:	Statistics of devfreq states.
115  * @last_update:	The last time stats were updated.
116  */
117 struct devfreq_stats {
118 	unsigned int total_trans;
119 	unsigned int *trans_table;
120 	u64 *time_in_state;
121 	u64 last_update;
122 };
123 
124 /**
125  * struct devfreq - Device devfreq structure
126  * @node:	list node - contains the devices with devfreq that have been
127  *		registered.
128  * @lock:	a mutex to protect accessing devfreq.
129  * @dev:	device registered by devfreq class. dev.parent is the device
130  *		using devfreq.
131  * @profile:	device-specific devfreq profile
132  * @governor:	method how to choose frequency based on the usage.
133  * @governor_name:	devfreq governor name for use with this devfreq
134  * @nb:		notifier block used to notify devfreq object that it should
135  *		reevaluate operable frequencies. Devfreq users may use
136  *		devfreq.nb to the corresponding register notifier call chain.
137  * @work:	delayed work for load monitoring.
138  * @previous_freq:	previously configured frequency value.
139  * @data:	Private data of the governor. The devfreq framework does not
140  *		touch this.
141  * @user_min_freq_req:	PM QoS minimum frequency request from user (via sysfs)
142  * @user_max_freq_req:	PM QoS maximum frequency request from user (via sysfs)
143  * @scaling_min_freq:	Limit minimum frequency requested by OPP interface
144  * @scaling_max_freq:	Limit maximum frequency requested by OPP interface
145  * @stop_polling:	 devfreq polling status of a device.
146  * @suspend_freq:	 frequency of a device set during suspend phase.
147  * @resume_freq:	 frequency of a device set in resume phase.
148  * @suspend_count:	 suspend requests counter for a device.
149  * @stats:	Statistics of devfreq device behavior
150  * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
151  * @nb_min:		Notifier block for DEV_PM_QOS_MIN_FREQUENCY
152  * @nb_max:		Notifier block for DEV_PM_QOS_MAX_FREQUENCY
153  *
154  * This structure stores the devfreq information for a give device.
155  *
156  * Note that when a governor accesses entries in struct devfreq in its
157  * functions except for the context of callbacks defined in struct
158  * devfreq_governor, the governor should protect its access with the
159  * struct mutex lock in struct devfreq. A governor may use this mutex
160  * to protect its own private data in void *data as well.
161  */
162 struct devfreq {
163 	struct list_head node;
164 
165 	struct mutex lock;
166 	struct device dev;
167 	struct devfreq_dev_profile *profile;
168 	const struct devfreq_governor *governor;
169 	char governor_name[DEVFREQ_NAME_LEN];
170 	struct notifier_block nb;
171 	struct delayed_work work;
172 
173 	unsigned long previous_freq;
174 	struct devfreq_dev_status last_status;
175 
176 	void *data; /* private data for governors */
177 
178 	struct dev_pm_qos_request user_min_freq_req;
179 	struct dev_pm_qos_request user_max_freq_req;
180 	unsigned long scaling_min_freq;
181 	unsigned long scaling_max_freq;
182 	bool stop_polling;
183 
184 	unsigned long suspend_freq;
185 	unsigned long resume_freq;
186 	atomic_t suspend_count;
187 
188 	/* information for device frequency transitions */
189 	struct devfreq_stats stats;
190 
191 	struct srcu_notifier_head transition_notifier_list;
192 
193 	struct notifier_block nb_min;
194 	struct notifier_block nb_max;
195 };
196 
197 struct devfreq_freqs {
198 	unsigned long old;
199 	unsigned long new;
200 };
201 
202 #if defined(CONFIG_PM_DEVFREQ)
203 extern struct devfreq *devfreq_add_device(struct device *dev,
204 				  struct devfreq_dev_profile *profile,
205 				  const char *governor_name,
206 				  void *data);
207 extern int devfreq_remove_device(struct devfreq *devfreq);
208 extern struct devfreq *devm_devfreq_add_device(struct device *dev,
209 				  struct devfreq_dev_profile *profile,
210 				  const char *governor_name,
211 				  void *data);
212 extern void devm_devfreq_remove_device(struct device *dev,
213 				  struct devfreq *devfreq);
214 
215 /* Supposed to be called by PM callbacks */
216 extern int devfreq_suspend_device(struct devfreq *devfreq);
217 extern int devfreq_resume_device(struct devfreq *devfreq);
218 
219 extern void devfreq_suspend(void);
220 extern void devfreq_resume(void);
221 
222 /**
223  * update_devfreq() - Reevaluate the device and configure frequency
224  * @devfreq:	the devfreq device
225  *
226  * Note: devfreq->lock must be held
227  */
228 extern int update_devfreq(struct devfreq *devfreq);
229 
230 /* Helper functions for devfreq user device driver with OPP. */
231 extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
232 					   unsigned long *freq, u32 flags);
233 extern int devfreq_register_opp_notifier(struct device *dev,
234 					 struct devfreq *devfreq);
235 extern int devfreq_unregister_opp_notifier(struct device *dev,
236 					   struct devfreq *devfreq);
237 extern int devm_devfreq_register_opp_notifier(struct device *dev,
238 					      struct devfreq *devfreq);
239 extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
240 						struct devfreq *devfreq);
241 extern int devfreq_register_notifier(struct devfreq *devfreq,
242 					struct notifier_block *nb,
243 					unsigned int list);
244 extern int devfreq_unregister_notifier(struct devfreq *devfreq,
245 					struct notifier_block *nb,
246 					unsigned int list);
247 extern int devm_devfreq_register_notifier(struct device *dev,
248 				struct devfreq *devfreq,
249 				struct notifier_block *nb,
250 				unsigned int list);
251 extern void devm_devfreq_unregister_notifier(struct device *dev,
252 				struct devfreq *devfreq,
253 				struct notifier_block *nb,
254 				unsigned int list);
255 extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
256 						int index);
257 
258 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
259 /**
260  * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
261  *	and devfreq_add_device
262  * @upthreshold:	If the load is over this value, the frequency jumps.
263  *			Specify 0 to use the default. Valid value = 0 to 100.
264  * @downdifferential:	If the load is under upthreshold - downdifferential,
265  *			the governor may consider slowing the frequency down.
266  *			Specify 0 to use the default. Valid value = 0 to 100.
267  *			downdifferential < upthreshold must hold.
268  *
269  * If the fed devfreq_simple_ondemand_data pointer is NULL to the governor,
270  * the governor uses the default values.
271  */
272 struct devfreq_simple_ondemand_data {
273 	unsigned int upthreshold;
274 	unsigned int downdifferential;
275 };
276 #endif
277 
278 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
279 /**
280  * struct devfreq_passive_data - void *data fed to struct devfreq
281  *	and devfreq_add_device
282  * @parent:	the devfreq instance of parent device.
283  * @get_target_freq:	Optional callback, Returns desired operating frequency
284  *			for the device using passive governor. That is called
285  *			when passive governor should decide the next frequency
286  *			by using the new frequency of parent devfreq device
287  *			using governors except for passive governor.
288  *			If the devfreq device has the specific method to decide
289  *			the next frequency, should use this callback.
290  * @this:	the devfreq instance of own device.
291  * @nb:		the notifier block for DEVFREQ_TRANSITION_NOTIFIER list
292  *
293  * The devfreq_passive_data have to set the devfreq instance of parent
294  * device with governors except for the passive governor. But, don't need to
295  * initialize the 'this' and 'nb' field because the devfreq core will handle
296  * them.
297  */
298 struct devfreq_passive_data {
299 	/* Should set the devfreq instance of parent device */
300 	struct devfreq *parent;
301 
302 	/* Optional callback to decide the next frequency of passvice device */
303 	int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
304 
305 	/* For passive governor's internal use. Don't need to set them */
306 	struct devfreq *this;
307 	struct notifier_block nb;
308 };
309 #endif
310 
311 #else /* !CONFIG_PM_DEVFREQ */
312 static inline struct devfreq *devfreq_add_device(struct device *dev,
313 					  struct devfreq_dev_profile *profile,
314 					  const char *governor_name,
315 					  void *data)
316 {
317 	return ERR_PTR(-ENOSYS);
318 }
319 
320 static inline int devfreq_remove_device(struct devfreq *devfreq)
321 {
322 	return 0;
323 }
324 
325 static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
326 					struct devfreq_dev_profile *profile,
327 					const char *governor_name,
328 					void *data)
329 {
330 	return ERR_PTR(-ENOSYS);
331 }
332 
333 static inline void devm_devfreq_remove_device(struct device *dev,
334 					struct devfreq *devfreq)
335 {
336 }
337 
338 static inline int devfreq_suspend_device(struct devfreq *devfreq)
339 {
340 	return 0;
341 }
342 
343 static inline int devfreq_resume_device(struct devfreq *devfreq)
344 {
345 	return 0;
346 }
347 
348 static inline void devfreq_suspend(void) {}
349 static inline void devfreq_resume(void) {}
350 
351 static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
352 					   unsigned long *freq, u32 flags)
353 {
354 	return ERR_PTR(-EINVAL);
355 }
356 
357 static inline int devfreq_register_opp_notifier(struct device *dev,
358 					 struct devfreq *devfreq)
359 {
360 	return -EINVAL;
361 }
362 
363 static inline int devfreq_unregister_opp_notifier(struct device *dev,
364 					   struct devfreq *devfreq)
365 {
366 	return -EINVAL;
367 }
368 
369 static inline int devm_devfreq_register_opp_notifier(struct device *dev,
370 						     struct devfreq *devfreq)
371 {
372 	return -EINVAL;
373 }
374 
375 static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
376 							struct devfreq *devfreq)
377 {
378 }
379 
380 static inline int devfreq_register_notifier(struct devfreq *devfreq,
381 					struct notifier_block *nb,
382 					unsigned int list)
383 {
384 	return 0;
385 }
386 
387 static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
388 					struct notifier_block *nb,
389 					unsigned int list)
390 {
391 	return 0;
392 }
393 
394 static inline int devm_devfreq_register_notifier(struct device *dev,
395 				struct devfreq *devfreq,
396 				struct notifier_block *nb,
397 				unsigned int list)
398 {
399 	return 0;
400 }
401 
402 static inline void devm_devfreq_unregister_notifier(struct device *dev,
403 				struct devfreq *devfreq,
404 				struct notifier_block *nb,
405 				unsigned int list)
406 {
407 }
408 
409 static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
410 							int index)
411 {
412 	return ERR_PTR(-ENODEV);
413 }
414 
415 static inline int devfreq_update_stats(struct devfreq *df)
416 {
417 	return -EINVAL;
418 }
419 #endif /* CONFIG_PM_DEVFREQ */
420 
421 #endif /* __LINUX_DEVFREQ_H__ */
422