1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Definitions related to Power Management Quality of Service (PM QoS).
5 * Copyright (C) 2020 Intel Corporation
8 * Mark Gross <mgross@linux.intel.com>
9 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
12 #ifndef _LINUX_PM_QOS_H
13 #define _LINUX_PM_QOS_H
15 #include <linux/plist.h>
16 #include <linux/notifier.h>
17 #include <linux/device.h>
19 enum pm_qos_flags_status {
20 PM_QOS_FLAGS_UNDEFINED = -1,
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)
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)
39 #define PM_QOS_FLAG_NO_POWER_OFF (1 << 0)
43 PM_QOS_MAX, /* return the largest value */
44 PM_QOS_MIN, /* return the smallest value */
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
52 struct pm_qos_constraints {
53 struct plist_head list;
54 s32 target_value; /* Do not change to 64 bit */
56 s32 no_constraint_value;
57 enum pm_qos_type type;
58 struct blocking_notifier_head *notifiers;
61 struct pm_qos_request {
62 struct plist_node node;
63 struct pm_qos_constraints *qos;
66 struct pm_qos_flags_request {
67 struct list_head node;
68 s32 flags; /* Do not change to 64 bit */
72 struct list_head list;
73 s32 effective_flags; /* Do not change to 64 bit */
77 #define FREQ_QOS_MIN_DEFAULT_VALUE 0
78 #define FREQ_QOS_MAX_DEFAULT_VALUE S32_MAX
80 enum freq_qos_req_type {
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;
92 struct freq_qos_request {
93 enum freq_qos_req_type type;
94 struct plist_node pnode;
95 struct freq_constraints *qos;
99 enum dev_pm_qos_req_type {
100 DEV_PM_QOS_RESUME_LATENCY = 1,
101 DEV_PM_QOS_LATENCY_TOLERANCE,
102 DEV_PM_QOS_MIN_FREQUENCY,
103 DEV_PM_QOS_MAX_FREQUENCY,
107 struct dev_pm_qos_request {
108 enum dev_pm_qos_req_type type;
110 struct plist_node pnode;
111 struct pm_qos_flags_request flr;
112 struct freq_qos_request freq;
118 struct pm_qos_constraints resume_latency;
119 struct pm_qos_constraints latency_tolerance;
120 struct freq_constraints freq;
121 struct pm_qos_flags flags;
122 struct dev_pm_qos_request *resume_latency_req;
123 struct dev_pm_qos_request *latency_tolerance_req;
124 struct dev_pm_qos_request *flags_req;
127 /* Action requested to pm_qos_update_target */
128 enum pm_qos_req_action {
129 PM_QOS_ADD_REQ, /* Add a new request */
130 PM_QOS_UPDATE_REQ, /* Update an existing request */
131 PM_QOS_REMOVE_REQ /* Remove an existing request */
134 static inline int dev_pm_qos_request_active(struct dev_pm_qos_request *req)
136 return req->dev != NULL;
139 s32 pm_qos_read_value(struct pm_qos_constraints *c);
140 int pm_qos_update_target(struct pm_qos_constraints *c, struct plist_node *node,
141 enum pm_qos_req_action action, int value);
142 bool pm_qos_update_flags(struct pm_qos_flags *pqf,
143 struct pm_qos_flags_request *req,
144 enum pm_qos_req_action action, s32 val);
146 #ifdef CONFIG_CPU_IDLE
147 s32 cpu_latency_qos_limit(void);
148 bool cpu_latency_qos_request_active(struct pm_qos_request *req);
149 void cpu_latency_qos_add_request(struct pm_qos_request *req, s32 value);
150 void cpu_latency_qos_update_request(struct pm_qos_request *req, s32 new_value);
151 void cpu_latency_qos_remove_request(struct pm_qos_request *req);
153 static inline s32 cpu_latency_qos_limit(void) { return INT_MAX; }
154 static inline bool cpu_latency_qos_request_active(struct pm_qos_request *req)
158 static inline void cpu_latency_qos_add_request(struct pm_qos_request *req,
160 static inline void cpu_latency_qos_update_request(struct pm_qos_request *req,
162 static inline void cpu_latency_qos_remove_request(struct pm_qos_request *req) {}
166 enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask);
167 enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask);
168 s32 __dev_pm_qos_resume_latency(struct device *dev);
169 s32 dev_pm_qos_read_value(struct device *dev, enum dev_pm_qos_req_type type);
170 int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
171 enum dev_pm_qos_req_type type, s32 value);
172 int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value);
173 int dev_pm_qos_remove_request(struct dev_pm_qos_request *req);
174 int dev_pm_qos_add_notifier(struct device *dev,
175 struct notifier_block *notifier,
176 enum dev_pm_qos_req_type type);
177 int dev_pm_qos_remove_notifier(struct device *dev,
178 struct notifier_block *notifier,
179 enum dev_pm_qos_req_type type);
180 void dev_pm_qos_constraints_init(struct device *dev);
181 void dev_pm_qos_constraints_destroy(struct device *dev);
182 int dev_pm_qos_add_ancestor_request(struct device *dev,
183 struct dev_pm_qos_request *req,
184 enum dev_pm_qos_req_type type, s32 value);
185 int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value);
186 void dev_pm_qos_hide_latency_limit(struct device *dev);
187 int dev_pm_qos_expose_flags(struct device *dev, s32 value);
188 void dev_pm_qos_hide_flags(struct device *dev);
189 int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set);
190 s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev);
191 int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val);
192 int dev_pm_qos_expose_latency_tolerance(struct device *dev);
193 void dev_pm_qos_hide_latency_tolerance(struct device *dev);
195 static inline s32 dev_pm_qos_requested_resume_latency(struct device *dev)
197 return dev->power.qos->resume_latency_req->data.pnode.prio;
200 static inline s32 dev_pm_qos_requested_flags(struct device *dev)
202 return dev->power.qos->flags_req->data.flr.flags;
205 static inline s32 dev_pm_qos_raw_resume_latency(struct device *dev)
207 return IS_ERR_OR_NULL(dev->power.qos) ?
208 PM_QOS_RESUME_LATENCY_NO_CONSTRAINT :
209 pm_qos_read_value(&dev->power.qos->resume_latency);
212 static inline enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev,
214 { return PM_QOS_FLAGS_UNDEFINED; }
215 static inline enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev,
217 { return PM_QOS_FLAGS_UNDEFINED; }
218 static inline s32 __dev_pm_qos_resume_latency(struct device *dev)
219 { return PM_QOS_RESUME_LATENCY_NO_CONSTRAINT; }
220 static inline s32 dev_pm_qos_read_value(struct device *dev,
221 enum dev_pm_qos_req_type type)
224 case DEV_PM_QOS_RESUME_LATENCY:
225 return PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
226 case DEV_PM_QOS_MIN_FREQUENCY:
227 return PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE;
228 case DEV_PM_QOS_MAX_FREQUENCY:
229 return PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
236 static inline int dev_pm_qos_add_request(struct device *dev,
237 struct dev_pm_qos_request *req,
238 enum dev_pm_qos_req_type type,
241 static inline int dev_pm_qos_update_request(struct dev_pm_qos_request *req,
244 static inline int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
246 static inline int dev_pm_qos_add_notifier(struct device *dev,
247 struct notifier_block *notifier,
248 enum dev_pm_qos_req_type type)
250 static inline int dev_pm_qos_remove_notifier(struct device *dev,
251 struct notifier_block *notifier,
252 enum dev_pm_qos_req_type type)
254 static inline void dev_pm_qos_constraints_init(struct device *dev)
256 dev->power.power_state = PMSG_ON;
258 static inline void dev_pm_qos_constraints_destroy(struct device *dev)
260 dev->power.power_state = PMSG_INVALID;
262 static inline int dev_pm_qos_add_ancestor_request(struct device *dev,
263 struct dev_pm_qos_request *req,
264 enum dev_pm_qos_req_type type,
267 static inline int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
269 static inline void dev_pm_qos_hide_latency_limit(struct device *dev) {}
270 static inline int dev_pm_qos_expose_flags(struct device *dev, s32 value)
272 static inline void dev_pm_qos_hide_flags(struct device *dev) {}
273 static inline int dev_pm_qos_update_flags(struct device *dev, s32 m, bool set)
275 static inline s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev)
276 { return PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT; }
277 static inline int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val)
279 static inline int dev_pm_qos_expose_latency_tolerance(struct device *dev)
281 static inline void dev_pm_qos_hide_latency_tolerance(struct device *dev) {}
283 static inline s32 dev_pm_qos_requested_resume_latency(struct device *dev)
285 return PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
287 static inline s32 dev_pm_qos_requested_flags(struct device *dev) { return 0; }
288 static inline s32 dev_pm_qos_raw_resume_latency(struct device *dev)
290 return PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
294 static inline int freq_qos_request_active(struct freq_qos_request *req)
296 return !IS_ERR_OR_NULL(req->qos);
299 void freq_constraints_init(struct freq_constraints *qos);
301 s32 freq_qos_read_value(struct freq_constraints *qos,
302 enum freq_qos_req_type type);
304 int freq_qos_add_request(struct freq_constraints *qos,
305 struct freq_qos_request *req,
306 enum freq_qos_req_type type, s32 value);
307 int freq_qos_update_request(struct freq_qos_request *req, s32 new_value);
308 int freq_qos_remove_request(struct freq_qos_request *req);
309 int freq_qos_apply(struct freq_qos_request *req,
310 enum pm_qos_req_action action, s32 value);
312 int freq_qos_add_notifier(struct freq_constraints *qos,
313 enum freq_qos_req_type type,
314 struct notifier_block *notifier);
315 int freq_qos_remove_notifier(struct freq_constraints *qos,
316 enum freq_qos_req_type type,
317 struct notifier_block *notifier);