]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/devfreq.h
Linux 4.11-rc6
[mirror_ubuntu-bionic-kernel.git] / include / linux / devfreq.h
CommitLineData
a3c98b8b
MH
1/*
2 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
3 * for Non-CPU Devices.
4 *
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#ifndef __LINUX_DEVFREQ_H__
14#define __LINUX_DEVFREQ_H__
15
16#include <linux/device.h>
17#include <linux/notifier.h>
e4db1c74 18#include <linux/pm_opp.h>
a3c98b8b
MH
19
20#define DEVFREQ_NAME_LEN 16
21
0fe3a664
CC
22/* DEVFREQ notifier interface */
23#define DEVFREQ_TRANSITION_NOTIFIER (0)
24
25/* Transition notifiers of DEVFREQ_TRANSITION_NOTIFIER */
26#define DEVFREQ_PRECHANGE (0)
27#define DEVFREQ_POSTCHANGE (1)
28
a3c98b8b
MH
29struct devfreq;
30
31/**
32 * struct devfreq_dev_status - Data given from devfreq user device to
33 * governors. Represents the performance
34 * statistics.
e09651fc 35 * @total_time: The total time represented by this instance of
a3c98b8b 36 * devfreq_dev_status
e09651fc 37 * @busy_time: The time that the device was working among the
a3c98b8b 38 * total_time.
e09651fc
NM
39 * @current_frequency: The operating frequency.
40 * @private_data: An entry not specified by the devfreq framework.
a3c98b8b
MH
41 * A device and a specific governor may have their
42 * own protocol with private_data. However, because
43 * this is governor-specific, a governor using this
44 * will be only compatible with devices aware of it.
45 */
46struct devfreq_dev_status {
47 /* both since the last measure */
48 unsigned long total_time;
49 unsigned long busy_time;
50 unsigned long current_frequency;
1a51cfdc 51 void *private_data;
a3c98b8b
MH
52};
53
ab5f299f
MH
54/*
55 * The resulting frequency should be at most this. (this bound is the
56 * least upper bound; thus, the resulting freq should be lower or same)
57 * If the flag is not set, the resulting frequency should be at most the
58 * bound (greatest lower bound)
59 */
60#define DEVFREQ_FLAG_LEAST_UPPER_BOUND 0x1
61
a3c98b8b
MH
62/**
63 * struct devfreq_dev_profile - Devfreq's user device profile
e09651fc 64 * @initial_freq: The operating frequency when devfreq_add_device() is
a3c98b8b 65 * called.
e09651fc
NM
66 * @polling_ms: The polling interval in ms. 0 disables polling.
67 * @target: The device should set its operating frequency at
a3c98b8b
MH
68 * freq or lowest-upper-than-freq value. If freq is
69 * higher than any operable frequency, set maximum.
70 * Before returning, target function should set
71 * freq at the current frequency.
ab5f299f
MH
72 * The "flags" parameter's possible values are
73 * explained above with "DEVFREQ_FLAG_*" macros.
e09651fc 74 * @get_dev_status: The device should provide the current performance
d54cdf3f
MH
75 * status to devfreq. Governors are recommended not to
76 * use this directly. Instead, governors are recommended
77 * to use devfreq_update_stats() along with
78 * devfreq.last_status.
e09651fc 79 * @get_cur_freq: The device should provide the current frequency
7f98a905 80 * at which it is operating.
e09651fc 81 * @exit: An optional callback that is called when devfreq
a3c98b8b
MH
82 * is removing the devfreq object due to error or
83 * from devfreq_remove_device() call. If the user
84 * has registered devfreq->nb at a notifier-head,
85 * this is the time to unregister it.
e552bbaf
JL
86 * @freq_table: Optional list of frequencies to support statistics.
87 * @max_state: The size of freq_table.
a3c98b8b
MH
88 */
89struct devfreq_dev_profile {
90 unsigned long initial_freq;
91 unsigned int polling_ms;
92
ab5f299f 93 int (*target)(struct device *dev, unsigned long *freq, u32 flags);
a3c98b8b
MH
94 int (*get_dev_status)(struct device *dev,
95 struct devfreq_dev_status *stat);
7f98a905 96 int (*get_cur_freq)(struct device *dev, unsigned long *freq);
a3c98b8b 97 void (*exit)(struct device *dev);
e552bbaf 98
0ec09ac2 99 unsigned long *freq_table;
e552bbaf 100 unsigned int max_state;
a3c98b8b
MH
101};
102
103/**
104 * struct devfreq_governor - Devfreq policy governor
3aa173b8 105 * @node: list node - contains registered devfreq governors
e09651fc 106 * @name: Governor's name
bcf23c79
CC
107 * @immutable: Immutable flag for governor. If the value is 1,
108 * this govenror is never changeable to other governor.
e09651fc 109 * @get_target_freq: Returns desired operating frequency for the device.
a3c98b8b
MH
110 * Basically, get_target_freq will run
111 * devfreq_dev_profile.get_dev_status() to get the
112 * status of the device (load = busy_time / total_time).
113 * If no_central_polling is set, this callback is called
114 * only with update_devfreq() notified by OPP.
e09651fc 115 * @event_handler: Callback for devfreq core framework to notify events
7e6fdd4b
RV
116 * to governors. Events include per device governor
117 * init and exit, opp changes out of devfreq, suspend
118 * and resume of per device devfreq during device idle.
a3c98b8b
MH
119 *
120 * Note that the callbacks are called with devfreq->lock locked by devfreq.
121 */
122struct devfreq_governor {
3aa173b8
NM
123 struct list_head node;
124
a3c98b8b 125 const char name[DEVFREQ_NAME_LEN];
bcf23c79 126 const unsigned int immutable;
a3c98b8b 127 int (*get_target_freq)(struct devfreq *this, unsigned long *freq);
7e6fdd4b
RV
128 int (*event_handler)(struct devfreq *devfreq,
129 unsigned int event, void *data);
a3c98b8b
MH
130};
131
132/**
133 * struct devfreq - Device devfreq structure
e09651fc 134 * @node: list node - contains the devices with devfreq that have been
a3c98b8b 135 * registered.
e09651fc
NM
136 * @lock: a mutex to protect accessing devfreq.
137 * @dev: device registered by devfreq class. dev.parent is the device
a3c98b8b 138 * using devfreq.
e09651fc
NM
139 * @profile: device-specific devfreq profile
140 * @governor: method how to choose frequency based on the usage.
1b5c1be2 141 * @governor_name: devfreq governor name for use with this devfreq
e09651fc 142 * @nb: notifier block used to notify devfreq object that it should
a3c98b8b
MH
143 * reevaluate operable frequencies. Devfreq users may use
144 * devfreq.nb to the corresponding register notifier call chain.
e09651fc
NM
145 * @work: delayed work for load monitoring.
146 * @previous_freq: previously configured frequency value.
147 * @data: Private data of the governor. The devfreq framework does not
a3c98b8b 148 * touch this.
e09651fc
NM
149 * @min_freq: Limit minimum frequency requested by user (0: none)
150 * @max_freq: Limit maximum frequency requested by user (0: none)
151 * @stop_polling: devfreq polling status of a device.
e552bbaf
JL
152 * @total_trans: Number of devfreq transitions
153 * @trans_table: Statistics of devfreq transitions
154 * @time_in_state: Statistics of devfreq states
155 * @last_stat_updated: The last time stat updated
0fe3a664 156 * @transition_notifier_list: list head of DEVFREQ_TRANSITION_NOTIFIER notifier
a3c98b8b
MH
157 *
158 * This structure stores the devfreq information for a give device.
159 *
160 * Note that when a governor accesses entries in struct devfreq in its
161 * functions except for the context of callbacks defined in struct
162 * devfreq_governor, the governor should protect its access with the
163 * struct mutex lock in struct devfreq. A governor may use this mutex
164 * to protect its own private data in void *data as well.
165 */
166struct devfreq {
167 struct list_head node;
168
169 struct mutex lock;
170 struct device dev;
171 struct devfreq_dev_profile *profile;
172 const struct devfreq_governor *governor;
1b5c1be2 173 char governor_name[DEVFREQ_NAME_LEN];
a3c98b8b 174 struct notifier_block nb;
7e6fdd4b 175 struct delayed_work work;
a3c98b8b 176
a3c98b8b 177 unsigned long previous_freq;
08e75e75 178 struct devfreq_dev_status last_status;
a3c98b8b
MH
179
180 void *data; /* private data for governors */
181
6530b9de
MH
182 unsigned long min_freq;
183 unsigned long max_freq;
7e6fdd4b 184 bool stop_polling;
e552bbaf 185
0585123e 186 /* information for device frequency transition */
e552bbaf
JL
187 unsigned int total_trans;
188 unsigned int *trans_table;
189 unsigned long *time_in_state;
190 unsigned long last_stat_updated;
0fe3a664
CC
191
192 struct srcu_notifier_head transition_notifier_list;
193};
194
195struct devfreq_freqs {
196 unsigned long old;
197 unsigned long new;
a3c98b8b
MH
198};
199
200#if defined(CONFIG_PM_DEVFREQ)
201extern struct devfreq *devfreq_add_device(struct device *dev,
202 struct devfreq_dev_profile *profile,
1b5c1be2 203 const char *governor_name,
a3c98b8b
MH
204 void *data);
205extern int devfreq_remove_device(struct devfreq *devfreq);
8cd84092
CC
206extern struct devfreq *devm_devfreq_add_device(struct device *dev,
207 struct devfreq_dev_profile *profile,
208 const char *governor_name,
209 void *data);
210extern void devm_devfreq_remove_device(struct device *dev,
211 struct devfreq *devfreq);
de9c7394 212
464ed18e 213/* Supposed to be called by PM callbacks */
206c30cf
RV
214extern int devfreq_suspend_device(struct devfreq *devfreq);
215extern int devfreq_resume_device(struct devfreq *devfreq);
a3c98b8b
MH
216
217/* Helper functions for devfreq user device driver with OPP. */
47d43ba7 218extern struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
ab5f299f 219 unsigned long *freq, u32 flags);
a3c98b8b
MH
220extern int devfreq_register_opp_notifier(struct device *dev,
221 struct devfreq *devfreq);
222extern int devfreq_unregister_opp_notifier(struct device *dev,
223 struct devfreq *devfreq);
d5b040d0
CC
224extern int devm_devfreq_register_opp_notifier(struct device *dev,
225 struct devfreq *devfreq);
226extern void devm_devfreq_unregister_opp_notifier(struct device *dev,
227 struct devfreq *devfreq);
0fe3a664
CC
228extern int devfreq_register_notifier(struct devfreq *devfreq,
229 struct notifier_block *nb,
230 unsigned int list);
231extern int devfreq_unregister_notifier(struct devfreq *devfreq,
232 struct notifier_block *nb,
233 unsigned int list);
234extern int devm_devfreq_register_notifier(struct device *dev,
235 struct devfreq *devfreq,
236 struct notifier_block *nb,
237 unsigned int list);
238extern void devm_devfreq_unregister_notifier(struct device *dev,
239 struct devfreq *devfreq,
240 struct notifier_block *nb,
241 unsigned int list);
8f510aeb
CC
242extern struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
243 int index);
244
08e75e75
JM
245/**
246 * devfreq_update_stats() - update the last_status pointer in struct devfreq
247 * @df: the devfreq instance whose status needs updating
d54cdf3f
MH
248 *
249 * Governors are recommended to use this function along with last_status,
250 * which allows other entities to reuse the last_status without affecting
251 * the values fetched later by governors.
08e75e75
JM
252 */
253static inline int devfreq_update_stats(struct devfreq *df)
254{
255 return df->profile->get_dev_status(df->dev.parent, &df->last_status);
256}
257
883d588e 258#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
ce26c5bb
MH
259/**
260 * struct devfreq_simple_ondemand_data - void *data fed to struct devfreq
261 * and devfreq_add_device
e09651fc 262 * @upthreshold: If the load is over this value, the frequency jumps.
ce26c5bb 263 * Specify 0 to use the default. Valid value = 0 to 100.
e09651fc 264 * @downdifferential: If the load is under upthreshold - downdifferential,
ce26c5bb
MH
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 */
272struct devfreq_simple_ondemand_data {
273 unsigned int upthreshold;
274 unsigned int downdifferential;
275};
276#endif
277
99613311
CC
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 */
298struct 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
a3c98b8b 311#else /* !CONFIG_PM_DEVFREQ */
5faaa035 312static inline struct devfreq *devfreq_add_device(struct device *dev,
a3c98b8b 313 struct devfreq_dev_profile *profile,
1b5c1be2 314 const char *governor_name,
a95e1f5d 315 void *data)
a3c98b8b 316{
8cd84092 317 return ERR_PTR(-ENOSYS);
a3c98b8b
MH
318}
319
5faaa035 320static inline int devfreq_remove_device(struct devfreq *devfreq)
a3c98b8b
MH
321{
322 return 0;
323}
324
8cd84092
CC
325static 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
333static inline void devm_devfreq_remove_device(struct device *dev,
334 struct devfreq *devfreq)
335{
336}
337
5faaa035 338static inline int devfreq_suspend_device(struct devfreq *devfreq)
206c30cf
RV
339{
340 return 0;
341}
342
5faaa035 343static inline int devfreq_resume_device(struct devfreq *devfreq)
206c30cf
RV
344{
345 return 0;
346}
347
47d43ba7 348static inline struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
ab5f299f 349 unsigned long *freq, u32 flags)
a3c98b8b 350{
5faaa035 351 return ERR_PTR(-EINVAL);
a3c98b8b
MH
352}
353
5faaa035 354static inline int devfreq_register_opp_notifier(struct device *dev,
a3c98b8b
MH
355 struct devfreq *devfreq)
356{
357 return -EINVAL;
358}
359
5faaa035 360static inline int devfreq_unregister_opp_notifier(struct device *dev,
a3c98b8b
MH
361 struct devfreq *devfreq)
362{
363 return -EINVAL;
364}
365
d5b040d0
CC
366static inline int devm_devfreq_register_opp_notifier(struct device *dev,
367 struct devfreq *devfreq)
368{
369 return -EINVAL;
370}
371
372static inline void devm_devfreq_unregister_opp_notifier(struct device *dev,
373 struct devfreq *devfreq)
374{
375}
08e75e75 376
0fe3a664
CC
377static inline int devfreq_register_notifier(struct devfreq *devfreq,
378 struct notifier_block *nb,
379 unsigned int list)
380{
381 return 0;
382}
383
384static inline int devfreq_unregister_notifier(struct devfreq *devfreq,
385 struct notifier_block *nb,
386 unsigned int list)
387{
388 return 0;
389}
390
391static inline int devm_devfreq_register_notifier(struct device *dev,
392 struct devfreq *devfreq,
393 struct notifier_block *nb,
394 unsigned int list)
395{
396 return 0;
397}
398
399static inline void devm_devfreq_unregister_notifier(struct device *dev,
400 struct devfreq *devfreq,
401 struct notifier_block *nb,
402 unsigned int list)
403{
404}
405
8f510aeb
CC
406static inline struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev,
407 int index)
408{
409 return ERR_PTR(-ENODEV);
410}
411
08e75e75
JM
412static inline int devfreq_update_stats(struct devfreq *df)
413{
414 return -EINVAL;
415}
a3c98b8b
MH
416#endif /* CONFIG_PM_DEVFREQ */
417
418#endif /* __LINUX_DEVFREQ_H__ */