xref: /openbmc/linux/include/linux/pm_qos.h (revision 814d51f8)
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 };
97 
98 
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,
104 	DEV_PM_QOS_FLAGS,
105 };
106 
107 struct dev_pm_qos_request {
108 	enum dev_pm_qos_req_type type;
109 	union {
110 		struct plist_node pnode;
111 		struct pm_qos_flags_request flr;
112 		struct freq_qos_request freq;
113 	} data;
114 	struct device *dev;
115 };
116 
117 struct dev_pm_qos {
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;
125 };
126 
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 */
132 };
133 
dev_pm_qos_request_active(struct dev_pm_qos_request * req)134 static inline int dev_pm_qos_request_active(struct dev_pm_qos_request *req)
135 {
136 	return req->dev != NULL;
137 }
138 
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);
145 
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);
152 #else
cpu_latency_qos_limit(void)153 static inline s32 cpu_latency_qos_limit(void) { return INT_MAX; }
cpu_latency_qos_request_active(struct pm_qos_request * req)154 static inline bool cpu_latency_qos_request_active(struct pm_qos_request *req)
155 {
156 	return false;
157 }
cpu_latency_qos_add_request(struct pm_qos_request * req,s32 value)158 static inline void cpu_latency_qos_add_request(struct pm_qos_request *req,
159 					       s32 value) {}
cpu_latency_qos_update_request(struct pm_qos_request * req,s32 new_value)160 static inline void cpu_latency_qos_update_request(struct pm_qos_request *req,
161 						  s32 new_value) {}
cpu_latency_qos_remove_request(struct pm_qos_request * req)162 static inline void cpu_latency_qos_remove_request(struct pm_qos_request *req) {}
163 #endif
164 
165 #ifdef CONFIG_PM
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);
194 
dev_pm_qos_requested_resume_latency(struct device * dev)195 static inline s32 dev_pm_qos_requested_resume_latency(struct device *dev)
196 {
197 	return dev->power.qos->resume_latency_req->data.pnode.prio;
198 }
199 
dev_pm_qos_requested_flags(struct device * dev)200 static inline s32 dev_pm_qos_requested_flags(struct device *dev)
201 {
202 	return dev->power.qos->flags_req->data.flr.flags;
203 }
204 
dev_pm_qos_raw_resume_latency(struct device * dev)205 static inline s32 dev_pm_qos_raw_resume_latency(struct device *dev)
206 {
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);
210 }
211 #else
__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_flags(struct device * dev,s32 mask)215 static inline enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev,
216 							s32 mask)
217 			{ return PM_QOS_FLAGS_UNDEFINED; }
__dev_pm_qos_resume_latency(struct device * dev)218 static inline s32 __dev_pm_qos_resume_latency(struct device *dev)
219 			{ return PM_QOS_RESUME_LATENCY_NO_CONSTRAINT; }
dev_pm_qos_read_value(struct device * dev,enum dev_pm_qos_req_type type)220 static inline s32 dev_pm_qos_read_value(struct device *dev,
221 					enum dev_pm_qos_req_type type)
222 {
223 	switch (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;
230 	default:
231 		WARN_ON(1);
232 		return 0;
233 	}
234 }
235 
dev_pm_qos_add_request(struct device * dev,struct dev_pm_qos_request * req,enum dev_pm_qos_req_type type,s32 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,
239 					 s32 value)
240 			{ return 0; }
dev_pm_qos_update_request(struct dev_pm_qos_request * req,s32 new_value)241 static inline int dev_pm_qos_update_request(struct dev_pm_qos_request *req,
242 					    s32 new_value)
243 			{ return 0; }
dev_pm_qos_remove_request(struct dev_pm_qos_request * req)244 static inline int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
245 			{ return 0; }
dev_pm_qos_add_notifier(struct device * dev,struct notifier_block * notifier,enum dev_pm_qos_req_type type)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)
249 			{ return 0; }
dev_pm_qos_remove_notifier(struct device * dev,struct notifier_block * notifier,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)
253 			{ return 0; }
dev_pm_qos_constraints_init(struct device * dev)254 static inline void dev_pm_qos_constraints_init(struct device *dev)
255 {
256 	dev->power.power_state = PMSG_ON;
257 }
dev_pm_qos_constraints_destroy(struct device * dev)258 static inline void dev_pm_qos_constraints_destroy(struct device *dev)
259 {
260 	dev->power.power_state = PMSG_INVALID;
261 }
dev_pm_qos_add_ancestor_request(struct device * dev,struct dev_pm_qos_request * req,enum dev_pm_qos_req_type type,s32 value)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,
265 						  s32 value)
266 			{ return 0; }
dev_pm_qos_expose_latency_limit(struct device * dev,s32 value)267 static inline int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
268 			{ return 0; }
dev_pm_qos_hide_latency_limit(struct device * dev)269 static inline void dev_pm_qos_hide_latency_limit(struct device *dev) {}
dev_pm_qos_expose_flags(struct device * dev,s32 value)270 static inline int dev_pm_qos_expose_flags(struct device *dev, s32 value)
271 			{ return 0; }
dev_pm_qos_hide_flags(struct device * dev)272 static inline void dev_pm_qos_hide_flags(struct device *dev) {}
dev_pm_qos_update_flags(struct device * dev,s32 m,bool set)273 static inline int dev_pm_qos_update_flags(struct device *dev, s32 m, bool set)
274 			{ return 0; }
dev_pm_qos_get_user_latency_tolerance(struct device * dev)275 static inline s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev)
276 			{ return PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT; }
dev_pm_qos_update_user_latency_tolerance(struct device * dev,s32 val)277 static inline int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val)
278 			{ return 0; }
dev_pm_qos_expose_latency_tolerance(struct device * dev)279 static inline int dev_pm_qos_expose_latency_tolerance(struct device *dev)
280 			{ return 0; }
dev_pm_qos_hide_latency_tolerance(struct device * dev)281 static inline void dev_pm_qos_hide_latency_tolerance(struct device *dev) {}
282 
dev_pm_qos_requested_resume_latency(struct device * dev)283 static inline s32 dev_pm_qos_requested_resume_latency(struct device *dev)
284 {
285 	return PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
286 }
dev_pm_qos_requested_flags(struct device * dev)287 static inline s32 dev_pm_qos_requested_flags(struct device *dev) { return 0; }
dev_pm_qos_raw_resume_latency(struct device * dev)288 static inline s32 dev_pm_qos_raw_resume_latency(struct device *dev)
289 {
290 	return PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
291 }
292 #endif
293 
freq_qos_request_active(struct freq_qos_request * req)294 static inline int freq_qos_request_active(struct freq_qos_request *req)
295 {
296 	return !IS_ERR_OR_NULL(req->qos);
297 }
298 
299 void freq_constraints_init(struct freq_constraints *qos);
300 
301 s32 freq_qos_read_value(struct freq_constraints *qos,
302 			enum freq_qos_req_type type);
303 
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);
311 
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);
318 
319 #endif
320