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