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