Merge tag 'pull-18-rc1-work.mount' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / include / linux / devfreq.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
a3c98b8b
MH
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>
a3c98b8b
MH
8 */
9
10#ifndef __LINUX_DEVFREQ_H__
11#define __LINUX_DEVFREQ_H__
12
13#include <linux/device.h>
14#include <linux/notifier.h>
e4db1c74 15#include <linux/pm_opp.h>
27dbc542 16#include <linux/pm_qos.h>
a3c98b8b 17
aa7c352f
CC
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
0fe3a664
CC
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
4dc3bab8
CC
32/* DEVFREQ work timers */
33enum devfreq_timer {
34 DEVFREQ_TIMER_DEFERRABLE = 0,
35 DEVFREQ_TIMER_DELAYED,
36 DEVFREQ_TIMER_NUM,
37};
38
a3c98b8b 39struct devfreq;
3ea6b700 40struct devfreq_governor;
a03dacb0 41struct devfreq_cpu_data;
1224451b 42struct thermal_cooling_device;
a3c98b8b
MH
43
44/**
45 * struct devfreq_dev_status - Data given from devfreq user device to
46 * governors. Represents the performance
47 * statistics.
e09651fc 48 * @total_time: The total time represented by this instance of
a3c98b8b 49 * devfreq_dev_status
e09651fc 50 * @busy_time: The time that the device was working among the
a3c98b8b 51 * total_time.
e09651fc
NM
52 * @current_frequency: The operating frequency.
53 * @private_data: An entry not specified by the devfreq framework.
a3c98b8b
MH
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 */
59struct devfreq_dev_status {
60 /* both since the last measure */
61 unsigned long total_time;
62 unsigned long busy_time;
63 unsigned long current_frequency;
1a51cfdc 64 void *private_data;
a3c98b8b
MH
65};
66
ab5f299f
MH
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
a3c98b8b
MH
75/**
76 * struct devfreq_dev_profile - Devfreq's user device profile
e09651fc 77 * @initial_freq: The operating frequency when devfreq_add_device() is
a3c98b8b 78 * called.
e09651fc 79 * @polling_ms: The polling interval in ms. 0 disables polling.
4dc3bab8 80 * @timer: Timer type is either deferrable or delayed timer.
e09651fc 81 * @target: The device should set its operating frequency at
a3c98b8b
MH
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.
ab5f299f
MH
86 * The "flags" parameter's possible values are
87 * explained above with "DEVFREQ_FLAG_*" macros.
e09651fc 88 * @get_dev_status: The device should provide the current performance
d54cdf3f
MH
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.
e09651fc 93 * @get_cur_freq: The device should provide the current frequency
7f98a905 94 * at which it is operating.
e09651fc 95 * @exit: An optional callback that is called when devfreq
a3c98b8b
MH
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.
416b46a2
CC
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.
1224451b
DL
103 *
104 * @is_cooling_device: A self-explanatory boolean giving the device a
105 * cooling effect property.
a3c98b8b
MH
106 */
107struct devfreq_dev_profile {
108 unsigned long initial_freq;
109 unsigned int polling_ms;
4dc3bab8 110 enum devfreq_timer timer;
1224451b 111 bool is_cooling_device;
a3c98b8b 112
ab5f299f 113 int (*target)(struct device *dev, unsigned long *freq, u32 flags);
a3c98b8b
MH
114 int (*get_dev_status)(struct device *dev,
115 struct devfreq_dev_status *stat);
7f98a905 116 int (*get_cur_freq)(struct device *dev, unsigned long *freq);
a3c98b8b 117 void (*exit)(struct device *dev);
e552bbaf 118
0ec09ac2 119 unsigned long *freq_table;
e552bbaf 120 unsigned int max_state;
a3c98b8b
MH
121};
122
1ebd0bc0
KK
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 */
130struct devfreq_stats {
131 unsigned int total_trans;
132 unsigned int *trans_table;
133 u64 *time_in_state;
134 u64 last_update;
135};
136
a3c98b8b
MH
137/**
138 * struct devfreq - Device devfreq structure
e09651fc 139 * @node: list node - contains the devices with devfreq that have been
a3c98b8b 140 * registered.
e09651fc
NM
141 * @lock: a mutex to protect accessing devfreq.
142 * @dev: device registered by devfreq class. dev.parent is the device
a3c98b8b 143 * using devfreq.
e09651fc
NM
144 * @profile: device-specific devfreq profile
145 * @governor: method how to choose frequency based on the usage.
26f9c7cc 146 * @opp_table: Reference to OPP table of dev.parent, if one exists.
e09651fc 147 * @nb: notifier block used to notify devfreq object that it should
a3c98b8b
MH
148 * reevaluate operable frequencies. Devfreq users may use
149 * devfreq.nb to the corresponding register notifier call chain.
e09651fc
NM
150 * @work: delayed work for load monitoring.
151 * @previous_freq: previously configured frequency value.
54cb5740 152 * @last_status: devfreq user device info, performance statistics
e09651fc 153 * @data: Private data of the governor. The devfreq framework does not
a3c98b8b 154 * touch this.
27dbc542
LC
155 * @user_min_freq_req: PM QoS minimum frequency request from user (via sysfs)
156 * @user_max_freq_req: PM QoS maximum frequency request from user (via sysfs)
f1d981ea
CC
157 * @scaling_min_freq: Limit minimum frequency requested by OPP interface
158 * @scaling_max_freq: Limit maximum frequency requested by OPP interface
e09651fc 159 * @stop_polling: devfreq polling status of a device.
83f8ca45
LL
160 * @suspend_freq: frequency of a device set during suspend phase.
161 * @resume_freq: frequency of a device set in resume phase.
162 * @suspend_count: suspend requests counter for a device.
1ebd0bc0 163 * @stats: Statistics of devfreq device behavior
0fe3a664 164 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
1224451b 165 * @cdev: Cooling device pointer if the devfreq has cooling property
05d7ae15
LC
166 * @nb_min: Notifier block for DEV_PM_QOS_MIN_FREQUENCY
167 * @nb_max: Notifier block for DEV_PM_QOS_MAX_FREQUENCY
a3c98b8b 168 *
54cb5740 169 * This structure stores the devfreq information for a given device.
a3c98b8b
MH
170 *
171 * Note that when a governor accesses entries in struct devfreq in its
172 * functions except for the context of callbacks defined in struct
173 * devfreq_governor, the governor should protect its access with the
174 * struct mutex lock in struct devfreq. A governor may use this mutex
7a51320e 175 * to protect its own private data in ``void *data`` as well.
a3c98b8b
MH
176 */
177struct devfreq {
178 struct list_head node;
179
180 struct mutex lock;
181 struct device dev;
182 struct devfreq_dev_profile *profile;
183 const struct devfreq_governor *governor;
26f9c7cc 184 struct opp_table *opp_table;
a3c98b8b 185 struct notifier_block nb;
7e6fdd4b 186 struct delayed_work work;
a3c98b8b 187
a3c98b8b 188 unsigned long previous_freq;
08e75e75 189 struct devfreq_dev_status last_status;
a3c98b8b
MH
190
191 void *data; /* private data for governors */
192
27dbc542
LC
193 struct dev_pm_qos_request user_min_freq_req;
194 struct dev_pm_qos_request user_max_freq_req;
f1d981ea
CC
195 unsigned long scaling_min_freq;
196 unsigned long scaling_max_freq;
7e6fdd4b 197 bool stop_polling;
e552bbaf 198
83f8ca45
LL
199 unsigned long suspend_freq;
200 unsigned long resume_freq;
201 atomic_t suspend_count;
202
1ebd0bc0
KK
203 /* information for device frequency transitions */
204 struct devfreq_stats stats;
0fe3a664
CC
205
206 struct srcu_notifier_head transition_notifier_list;
05d7ae15 207
1224451b
DL
208 /* Pointer to the cooling device if used for thermal mitigation */
209 struct thermal_cooling_device *cdev;
210
05d7ae15
LC
211 struct notifier_block nb_min;
212 struct notifier_block nb_max;
0fe3a664
CC
213};
214
215struct devfreq_freqs {
216 unsigned long old;
217 unsigned long new;
a3c98b8b
MH
218};
219
220#if defined(CONFIG_PM_DEVFREQ)
6d743493
CC
221struct devfreq *devfreq_add_device(struct device *dev,
222 struct devfreq_dev_profile *profile,
223 const char *governor_name,
224 void *data);
225int devfreq_remove_device(struct devfreq *devfreq);
226struct devfreq *devm_devfreq_add_device(struct device *dev,
227 struct devfreq_dev_profile *profile,
228 const char *governor_name,
229 void *data);
230void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq);
de9c7394 231
464ed18e 232/* Supposed to be called by PM callbacks */
6d743493
CC
233int devfreq_suspend_device(struct devfreq *devfreq);
234int devfreq_resume_device(struct devfreq *devfreq);
a3c98b8b 235
6d743493
CC
236void devfreq_suspend(void);
237void devfreq_resume(void);
59031956 238
3e2ac979 239/* update_devfreq() - Reevaluate the device and configure frequency */
6d743493 240int update_devfreq(struct devfreq *devfreq);
b596d895 241
a3c98b8b 242/* Helper functions for devfreq user device driver with OPP. */
6d743493
CC
243struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
244 unsigned long *freq, u32 flags);
245int devfreq_register_opp_notifier(struct device *dev,
246 struct devfreq *devfreq);
247int devfreq_unregister_opp_notifier(struct device *dev,
248 struct devfreq *devfreq);
249int devm_devfreq_register_opp_notifier(struct device *dev,
250 struct devfreq *devfreq);
251void devm_devfreq_unregister_opp_notifier(struct device *dev,
252 struct devfreq *devfreq);
253int devfreq_register_notifier(struct devfreq *devfreq,
254 struct notifier_block *nb,
255 unsigned int list);
256int devfreq_unregister_notifier(struct devfreq *devfreq,
257 struct notifier_block *nb,
258 unsigned int list);
259int devm_devfreq_register_notifier(struct device *dev,
0fe3a664
CC
260 struct devfreq *devfreq,
261 struct notifier_block *nb,
262 unsigned int list);
6d743493 263void devm_devfreq_unregister_notifier(struct device *dev,
0fe3a664
CC
264 struct devfreq *devfreq,
265 struct notifier_block *nb,
266 unsigned int list);
7b38b7b0 267struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node);
86d90fd9
CC
268struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
269 const char *phandle_name, int index);
8f510aeb 270
883d588e 271#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
ce26c5bb 272/**
7a51320e 273 * struct devfreq_simple_ondemand_data - ``void *data`` fed to struct devfreq
ce26c5bb 274 * and devfreq_add_device
e09651fc 275 * @upthreshold: If the load is over this value, the frequency jumps.
ce26c5bb 276 * Specify 0 to use the default. Valid value = 0 to 100.
e09651fc 277 * @downdifferential: If the load is under upthreshold - downdifferential,
ce26c5bb
MH
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 */
285struct devfreq_simple_ondemand_data {
286 unsigned int upthreshold;
287 unsigned int downdifferential;
288};
289#endif
290
99613311 291#if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
a03dacb0
SK
292enum devfreq_parent_dev_type {
293 DEVFREQ_PARENT_DEV,
294 CPUFREQ_PARENT_DEV,
295};
296
99613311 297/**
7a51320e 298 * struct devfreq_passive_data - ``void *data`` fed to struct devfreq
99613311
CC
299 * and devfreq_add_device
300 * @parent: the devfreq instance of parent device.
301 * @get_target_freq: Optional callback, Returns desired operating frequency
302 * for the device using passive governor. That is called
303 * when passive governor should decide the next frequency
304 * by using the new frequency of parent devfreq device
305 * using governors except for passive governor.
306 * If the devfreq device has the specific method to decide
307 * the next frequency, should use this callback.
a03dacb0
SK
308 * @parent_type: the parent type of the device.
309 * @this: the devfreq instance of own device.
310 * @nb: the notifier block for DEVFREQ_TRANSITION_NOTIFIER or
311 * CPUFREQ_TRANSITION_NOTIFIER list.
26984d9d 312 * @cpu_data_list: the list of cpu frequency data for all cpufreq_policy.
99613311
CC
313 *
314 * The devfreq_passive_data have to set the devfreq instance of parent
315 * device with governors except for the passive governor. But, don't need to
316 * initialize the 'this' and 'nb' field because the devfreq core will handle
317 * them.
318 */
319struct devfreq_passive_data {
320 /* Should set the devfreq instance of parent device */
321 struct devfreq *parent;
322
323 /* Optional callback to decide the next frequency of passvice device */
324 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
325
a03dacb0
SK
326 /* Should set the type of parent device */
327 enum devfreq_parent_dev_type parent_type;
328
99613311
CC
329 /* For passive governor's internal use. Don't need to set them */
330 struct devfreq *this;
331 struct notifier_block nb;
26984d9d 332 struct list_head cpu_data_list;
99613311
CC
333};
334#endif
335
a3c98b8b 336#else /* !CONFIG_PM_DEVFREQ */
5faaa035 337static inline struct devfreq *devfreq_add_device(struct device *dev,
6d743493
CC
338 struct devfreq_dev_profile *profile,
339 const char *governor_name,
340 void *data)
a3c98b8b 341{
8cd84092 342 return ERR_PTR(-ENOSYS);
a3c98b8b
MH
343}
344
5faaa035 345static inline int devfreq_remove_device(struct devfreq *devfreq)
a3c98b8b
MH
346{
347 return 0;
348}
349
8cd84092
CC
350static inline struct devfreq *devm_devfreq_add_device(struct device *dev,
351 struct devfreq_dev_profile *profile,
352 const char *governor_name,
353 void *data)
354{
355 return ERR_PTR(-ENOSYS);
356}
357
358static inline void devm_devfreq_remove_device(struct device *dev,
359 struct devfreq *devfreq)
360{
361}
362
5faaa035 363static inline int devfreq_suspend_device(struct devfreq *devfreq)
206c30cf
RV
364{
365 return 0;
366}
367
5faaa035 368static inline int devfreq_resume_device(struct devfreq *devfreq)
206c30cf
RV
369{
370 return 0;
371}
372
59031956
LL
373static inline void devfreq_suspend(void) {}
374static inline void devfreq_resume(void) {}
375
47d43ba7 376static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
6d743493 377 unsigned long *freq, u32 flags)
a3c98b8b 378{
5faaa035 379 return ERR_PTR(-EINVAL);
a3c98b8b
MH
380}
381
5faaa035 382static inline int devfreq_register_opp_notifier(struct device *dev,
6d743493 383 struct devfreq *devfreq)
a3c98b8b
MH
384{
385 return -EINVAL;
386}
387
5faaa035 388static inline int devfreq_unregister_opp_notifier(struct device *dev,
6d743493 389 struct devfreq *devfreq)
a3c98b8b
MH
390{
391 return -EINVAL;
392}
393
d5b040d0 394static inline int devm_devfreq_register_opp_notifier(struct device *dev,
6d743493 395 struct devfreq *devfreq)
d5b040d0
CC
396{
397 return -EINVAL;
398}
399
400static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
6d743493 401 struct devfreq *devfreq)
d5b040d0
CC
402{
403}
08e75e75 404
0fe3a664
CC
405static inline int devfreq_register_notifier(struct devfreq *devfreq,
406 struct notifier_block *nb,
407 unsigned int list)
408{
409 return 0;
410}
411
412static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
413 struct notifier_block *nb,
414 unsigned int list)
415{
416 return 0;
417}
418
419static inline int devm_devfreq_register_notifier(struct device *dev,
6d743493
CC
420 struct devfreq *devfreq,
421 struct notifier_block *nb,
422 unsigned int list)
0fe3a664
CC
423{
424 return 0;
425}
426
427static inline void devm_devfreq_unregister_notifier(struct device *dev,
6d743493
CC
428 struct devfreq *devfreq,
429 struct notifier_block *nb,
430 unsigned int list)
0fe3a664
CC
431{
432}
433
7b38b7b0
LC
434static inline struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node)
435{
436 return ERR_PTR(-ENODEV);
437}
438
8f510aeb 439static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
86d90fd9 440 const char *phandle_name, int index)
8f510aeb
CC
441{
442 return ERR_PTR(-ENODEV);
443}
444
08e75e75
JM
445static inline int devfreq_update_stats(struct devfreq *df)
446{
447 return -EINVAL;
448}
a3c98b8b
MH
449#endif /* CONFIG_PM_DEVFREQ */
450
451#endif /* __LINUX_DEVFREQ_H__ */