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