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