1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_PM_QOS_H
3 #define _LINUX_PM_QOS_H
4 /* interface for the pm_qos_power infrastructure of the linux kernel.
5 *
6 * Mark Gross <mgross@linux.intel.com>
7 */
8 #include <linux/plist.h>
9 #include <linux/notifier.h>
10 #include <linux/device.h>
11 #include <linux/workqueue.h>
12
13 enum {
14 PM_QOS_RESERVED = 0,
15 PM_QOS_CPU_DMA_LATENCY,
16
17 /* insert new class ID */
18 PM_QOS_NUM_CLASSES,
19 };
20
21 enum pm_qos_flags_status {
22 PM_QOS_FLAGS_UNDEFINED = -1,
23 PM_QOS_FLAGS_NONE,
24 PM_QOS_FLAGS_SOME,
25 PM_QOS_FLAGS_ALL,
26 };
27
28 #define PM_QOS_DEFAULT_VALUE (-1)
29 #define PM_QOS_LATENCY_ANY S32_MAX
30 #define PM_QOS_LATENCY_ANY_NS ((s64)PM_QOS_LATENCY_ANY * NSEC_PER_USEC)
31
32 #define PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE (2000 * USEC_PER_SEC)
33 #define PM_QOS_RESUME_LATENCY_DEFAULT_VALUE PM_QOS_LATENCY_ANY
34 #define PM_QOS_RESUME_LATENCY_NO_CONSTRAINT PM_QOS_LATENCY_ANY
35 #define PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS PM_QOS_LATENCY_ANY_NS
36 #define PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE 0
37 #define PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE 0
38 #define PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE FREQ_QOS_MAX_DEFAULT_VALUE
39 #define PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT (-1)
40
41 #define PM_QOS_FLAG_NO_POWER_OFF (1 << 0)
42
43 struct pm_qos_request {
44 struct plist_node node;
45 int pm_qos_class;
46 struct delayed_work work; /* for pm_qos_update_request_timeout */
47 };
48
49 struct pm_qos_flags_request {
50 struct list_head node;
51 s32 flags; /* Do not change to 64 bit */
52 };
53
54 enum pm_qos_type {
55 PM_QOS_UNITIALIZED,
56 PM_QOS_MAX, /* return the largest value */
57 PM_QOS_MIN, /* return the smallest value */
58 PM_QOS_SUM /* return the sum */
59 };
60
61 /*
62 * Note: The lockless read path depends on the CPU accessing target_value
63 * or effective_flags atomically. Atomic access is only guaranteed on all CPU
64 * types linux supports for 32 bit quantites
65 */
66 struct pm_qos_constraints {
67 struct plist_head list;
68 s32 target_value; /* Do not change to 64 bit */
69 s32 default_value;
70 s32 no_constraint_value;
71 enum pm_qos_type type;
72 struct blocking_notifier_head *notifiers;
73 };
74
75 struct pm_qos_flags {
76 struct list_head list;
77 s32 effective_flags; /* Do not change to 64 bit */
78 };
79
80
81 #define FREQ_QOS_MIN_DEFAULT_VALUE 0
82 #define FREQ_QOS_MAX_DEFAULT_VALUE S32_MAX
83
84 enum freq_qos_req_type {
85 FREQ_QOS_MIN = 1,
86 FREQ_QOS_MAX,
87 };
88
89 struct freq_constraints {
90 struct pm_qos_constraints min_freq;
91 struct blocking_notifier_head min_freq_notifiers;
92 struct pm_qos_constraints max_freq;
93 struct blocking_notifier_head max_freq_notifiers;
94 };
95
96 struct freq_qos_request {
97 enum freq_qos_req_type type;
98 struct plist_node pnode;
99 struct freq_constraints *qos;
100 };
101
102
103 enum dev_pm_qos_req_type {
104 DEV_PM_QOS_RESUME_LATENCY = 1,
105 DEV_PM_QOS_LATENCY_TOLERANCE,
106 DEV_PM_QOS_MIN_FREQUENCY,
107 DEV_PM_QOS_MAX_FREQUENCY,
108 DEV_PM_QOS_FLAGS,
109 };
110
111 struct dev_pm_qos_request {
112 enum dev_pm_qos_req_type type;
113 union {
114 struct plist_node pnode;
115 struct pm_qos_flags_request flr;
116 struct freq_qos_request freq;
117 } data;
118 struct device *dev;
119 };
120
121 struct dev_pm_qos {
122 struct pm_qos_constraints resume_latency;
123 struct pm_qos_constraints latency_tolerance;
124 struct freq_constraints freq;
125 struct pm_qos_flags flags;
126 struct dev_pm_qos_request *resume_latency_req;
127 struct dev_pm_qos_request *latency_tolerance_req;
128 struct dev_pm_qos_request *flags_req;
129 };
130
131 /* Action requested to pm_qos_update_target */
132 enum pm_qos_req_action {
133 PM_QOS_ADD_REQ, /* Add a new request */
134 PM_QOS_UPDATE_REQ, /* Update an existing request */
135 PM_QOS_REMOVE_REQ /* Remove an existing request */
136 };
137
dev_pm_qos_request_active(struct dev_pm_qos_request * req)138 static inline int dev_pm_qos_request_active(struct dev_pm_qos_request *req)
139 {
140 return req->dev != NULL;
141 }
142
143 int pm_qos_update_target(struct pm_qos_constraints *c, struct plist_node *node,
144 enum pm_qos_req_action action, int value);
145 bool pm_qos_update_flags(struct pm_qos_flags *pqf,
146 struct pm_qos_flags_request *req,
147 enum pm_qos_req_action action, s32 val);
148 void pm_qos_add_request(struct pm_qos_request *req, int pm_qos_class,
149 s32 value);
150 void pm_qos_update_request(struct pm_qos_request *req,
151 s32 new_value);
152 void pm_qos_update_request_timeout(struct pm_qos_request *req,
153 s32 new_value, unsigned long timeout_us);
154 void pm_qos_remove_request(struct pm_qos_request *req);
155
156 int pm_qos_request(int pm_qos_class);
157 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier);
158 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier);
159 int pm_qos_request_active(struct pm_qos_request *req);
160 s32 pm_qos_read_value(struct pm_qos_constraints *c);
161
162 #ifdef CONFIG_PM
163 enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask);
164 enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask);
165 s32 __dev_pm_qos_resume_latency(struct device *dev);
166 s32 dev_pm_qos_read_value(struct device *dev, enum dev_pm_qos_req_type type);
167 int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
168 enum dev_pm_qos_req_type type, s32 value);
169 int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value);
170 int dev_pm_qos_remove_request(struct dev_pm_qos_request *req);
171 int dev_pm_qos_add_notifier(struct device *dev,
172 struct notifier_block *notifier,
173 enum dev_pm_qos_req_type type);
174 int dev_pm_qos_remove_notifier(struct device *dev,
175 struct notifier_block *notifier,
176 enum dev_pm_qos_req_type type);
177 void dev_pm_qos_constraints_init(struct device *dev);
178 void dev_pm_qos_constraints_destroy(struct device *dev);
179 int dev_pm_qos_add_ancestor_request(struct device *dev,
180 struct dev_pm_qos_request *req,
181 enum dev_pm_qos_req_type type, s32 value);
182 int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value);
183 void dev_pm_qos_hide_latency_limit(struct device *dev);
184 int dev_pm_qos_expose_flags(struct device *dev, s32 value);
185 void dev_pm_qos_hide_flags(struct device *dev);
186 int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set);
187 s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev);
188 int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val);
189 int dev_pm_qos_expose_latency_tolerance(struct device *dev);
190 void dev_pm_qos_hide_latency_tolerance(struct device *dev);
191
dev_pm_qos_requested_resume_latency(struct device * dev)192 static inline s32 dev_pm_qos_requested_resume_latency(struct device *dev)
193 {
194 return dev->power.qos->resume_latency_req->data.pnode.prio;
195 }
196
dev_pm_qos_requested_flags(struct device * dev)197 static inline s32 dev_pm_qos_requested_flags(struct device *dev)
198 {
199 return dev->power.qos->flags_req->data.flr.flags;
200 }
201
dev_pm_qos_raw_resume_latency(struct device * dev)202 static inline s32 dev_pm_qos_raw_resume_latency(struct device *dev)
203 {
204 return IS_ERR_OR_NULL(dev->power.qos) ?
205 PM_QOS_RESUME_LATENCY_NO_CONSTRAINT :
206 pm_qos_read_value(&dev->power.qos->resume_latency);
207 }
208 #else
__dev_pm_qos_flags(struct device * dev,s32 mask)209 static inline enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev,
210 s32 mask)
211 { return PM_QOS_FLAGS_UNDEFINED; }
dev_pm_qos_flags(struct device * dev,s32 mask)212 static inline enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev,
213 s32 mask)
214 { return PM_QOS_FLAGS_UNDEFINED; }
__dev_pm_qos_resume_latency(struct device * dev)215 static inline s32 __dev_pm_qos_resume_latency(struct device *dev)
216 { return PM_QOS_RESUME_LATENCY_NO_CONSTRAINT; }
dev_pm_qos_read_value(struct device * dev,enum dev_pm_qos_req_type type)217 static inline s32 dev_pm_qos_read_value(struct device *dev,
218 enum dev_pm_qos_req_type type)
219 {
220 switch (type) {
221 case DEV_PM_QOS_RESUME_LATENCY:
222 return PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
223 case DEV_PM_QOS_MIN_FREQUENCY:
224 return PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE;
225 case DEV_PM_QOS_MAX_FREQUENCY:
226 return PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
227 default:
228 WARN_ON(1);
229 return 0;
230 }
231 }
232
dev_pm_qos_add_request(struct device * dev,struct dev_pm_qos_request * req,enum dev_pm_qos_req_type type,s32 value)233 static inline int dev_pm_qos_add_request(struct device *dev,
234 struct dev_pm_qos_request *req,
235 enum dev_pm_qos_req_type type,
236 s32 value)
237 { return 0; }
dev_pm_qos_update_request(struct dev_pm_qos_request * req,s32 new_value)238 static inline int dev_pm_qos_update_request(struct dev_pm_qos_request *req,
239 s32 new_value)
240 { return 0; }
dev_pm_qos_remove_request(struct dev_pm_qos_request * req)241 static inline int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
242 { return 0; }
dev_pm_qos_add_notifier(struct device * dev,struct notifier_block * notifier,enum dev_pm_qos_req_type type)243 static inline int dev_pm_qos_add_notifier(struct device *dev,
244 struct notifier_block *notifier,
245 enum dev_pm_qos_req_type type)
246 { return 0; }
dev_pm_qos_remove_notifier(struct device * dev,struct notifier_block * notifier,enum dev_pm_qos_req_type type)247 static inline int dev_pm_qos_remove_notifier(struct device *dev,
248 struct notifier_block *notifier,
249 enum dev_pm_qos_req_type type)
250 { return 0; }
dev_pm_qos_constraints_init(struct device * dev)251 static inline void dev_pm_qos_constraints_init(struct device *dev)
252 {
253 dev->power.power_state = PMSG_ON;
254 }
dev_pm_qos_constraints_destroy(struct device * dev)255 static inline void dev_pm_qos_constraints_destroy(struct device *dev)
256 {
257 dev->power.power_state = PMSG_INVALID;
258 }
dev_pm_qos_add_ancestor_request(struct device * dev,struct dev_pm_qos_request * req,enum dev_pm_qos_req_type type,s32 value)259 static inline int dev_pm_qos_add_ancestor_request(struct device *dev,
260 struct dev_pm_qos_request *req,
261 enum dev_pm_qos_req_type type,
262 s32 value)
263 { return 0; }
dev_pm_qos_expose_latency_limit(struct device * dev,s32 value)264 static inline int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
265 { return 0; }
dev_pm_qos_hide_latency_limit(struct device * dev)266 static inline void dev_pm_qos_hide_latency_limit(struct device *dev) {}
dev_pm_qos_expose_flags(struct device * dev,s32 value)267 static inline int dev_pm_qos_expose_flags(struct device *dev, s32 value)
268 { return 0; }
dev_pm_qos_hide_flags(struct device * dev)269 static inline void dev_pm_qos_hide_flags(struct device *dev) {}
dev_pm_qos_update_flags(struct device * dev,s32 m,bool set)270 static inline int dev_pm_qos_update_flags(struct device *dev, s32 m, bool set)
271 { return 0; }
dev_pm_qos_get_user_latency_tolerance(struct device * dev)272 static inline s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev)
273 { return PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT; }
dev_pm_qos_update_user_latency_tolerance(struct device * dev,s32 val)274 static inline int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val)
275 { return 0; }
dev_pm_qos_expose_latency_tolerance(struct device * dev)276 static inline int dev_pm_qos_expose_latency_tolerance(struct device *dev)
277 { return 0; }
dev_pm_qos_hide_latency_tolerance(struct device * dev)278 static inline void dev_pm_qos_hide_latency_tolerance(struct device *dev) {}
279
dev_pm_qos_requested_resume_latency(struct device * dev)280 static inline s32 dev_pm_qos_requested_resume_latency(struct device *dev)
281 {
282 return PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
283 }
dev_pm_qos_requested_flags(struct device * dev)284 static inline s32 dev_pm_qos_requested_flags(struct device *dev) { return 0; }
dev_pm_qos_raw_resume_latency(struct device * dev)285 static inline s32 dev_pm_qos_raw_resume_latency(struct device *dev)
286 {
287 return PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
288 }
289 #endif
290
freq_qos_request_active(struct freq_qos_request * req)291 static inline int freq_qos_request_active(struct freq_qos_request *req)
292 {
293 return !IS_ERR_OR_NULL(req->qos);
294 }
295
296 void freq_constraints_init(struct freq_constraints *qos);
297
298 s32 freq_qos_read_value(struct freq_constraints *qos,
299 enum freq_qos_req_type type);
300
301 int freq_qos_add_request(struct freq_constraints *qos,
302 struct freq_qos_request *req,
303 enum freq_qos_req_type type, s32 value);
304 int freq_qos_update_request(struct freq_qos_request *req, s32 new_value);
305 int freq_qos_remove_request(struct freq_qos_request *req);
306 int freq_qos_apply(struct freq_qos_request *req,
307 enum pm_qos_req_action action, s32 value);
308
309 int freq_qos_add_notifier(struct freq_constraints *qos,
310 enum freq_qos_req_type type,
311 struct notifier_block *notifier);
312 int freq_qos_remove_notifier(struct freq_constraints *qos,
313 enum freq_qos_req_type type,
314 struct notifier_block *notifier);
315
316 #endif
317