]>
Commit | Line | Data |
---|---|---|
91ff4cb8 JP |
1 | /* |
2 | * Devices PM QoS constraints management | |
3 | * | |
4 | * Copyright (C) 2011 Texas Instruments, Inc. | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * | |
11 | * This module exposes the interface to kernel space for specifying | |
12 | * per-device PM QoS dependencies. It provides infrastructure for registration | |
13 | * of: | |
14 | * | |
15 | * Dependents on a QoS value : register requests | |
16 | * Watchers of QoS value : get notified when target QoS value changes | |
17 | * | |
18 | * This QoS design is best effort based. Dependents register their QoS needs. | |
19 | * Watchers register to keep track of the current QoS needs of the system. | |
d08d1b27 VK |
20 | * Watchers can register a per-device notification callback using the |
21 | * dev_pm_qos_*_notifier API. The notification chain data is stored in the | |
22 | * per-device constraint data struct. | |
91ff4cb8 JP |
23 | * |
24 | * Note about the per-device constraint data struct allocation: | |
25 | * . The per-device constraints data struct ptr is tored into the device | |
26 | * dev_pm_info. | |
27 | * . To minimize the data usage by the per-device constraints, the data struct | |
28 | * is only allocated at the first call to dev_pm_qos_add_request. | |
29 | * . The data is later free'd when the device is removed from the system. | |
91ff4cb8 JP |
30 | * . A global mutex protects the constraints users from the data being |
31 | * allocated and free'd. | |
32 | */ | |
33 | ||
34 | #include <linux/pm_qos.h> | |
35 | #include <linux/spinlock.h> | |
36 | #include <linux/slab.h> | |
37 | #include <linux/device.h> | |
38 | #include <linux/mutex.h> | |
1b6bc32f | 39 | #include <linux/export.h> |
e39473d0 | 40 | #include <linux/pm_runtime.h> |
37530f2b | 41 | #include <linux/err.h> |
96d9d0b5 | 42 | #include <trace/events/power.h> |
91ff4cb8 | 43 | |
85dc0b8a | 44 | #include "power.h" |
91ff4cb8 JP |
45 | |
46 | static DEFINE_MUTEX(dev_pm_qos_mtx); | |
0f703069 | 47 | static DEFINE_MUTEX(dev_pm_qos_sysfs_mtx); |
1a9a9152 | 48 | |
ae0fb4b7 RW |
49 | /** |
50 | * __dev_pm_qos_flags - Check PM QoS flags for a given device. | |
51 | * @dev: Device to check the PM QoS flags for. | |
52 | * @mask: Flags to check against. | |
53 | * | |
54 | * This routine must be called with dev->power.lock held. | |
55 | */ | |
56 | enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask) | |
57 | { | |
58 | struct dev_pm_qos *qos = dev->power.qos; | |
59 | struct pm_qos_flags *pqf; | |
60 | s32 val; | |
61 | ||
f90b8ad8 KK |
62 | lockdep_assert_held(&dev->power.lock); |
63 | ||
37530f2b | 64 | if (IS_ERR_OR_NULL(qos)) |
ae0fb4b7 RW |
65 | return PM_QOS_FLAGS_UNDEFINED; |
66 | ||
67 | pqf = &qos->flags; | |
68 | if (list_empty(&pqf->list)) | |
69 | return PM_QOS_FLAGS_UNDEFINED; | |
70 | ||
71 | val = pqf->effective_flags & mask; | |
72 | if (val) | |
73 | return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME; | |
74 | ||
75 | return PM_QOS_FLAGS_NONE; | |
76 | } | |
77 | ||
78 | /** | |
79 | * dev_pm_qos_flags - Check PM QoS flags for a given device (locked). | |
80 | * @dev: Device to check the PM QoS flags for. | |
81 | * @mask: Flags to check against. | |
82 | */ | |
83 | enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask) | |
84 | { | |
85 | unsigned long irqflags; | |
86 | enum pm_qos_flags_status ret; | |
87 | ||
88 | spin_lock_irqsave(&dev->power.lock, irqflags); | |
89 | ret = __dev_pm_qos_flags(dev, mask); | |
90 | spin_unlock_irqrestore(&dev->power.lock, irqflags); | |
91 | ||
92 | return ret; | |
93 | } | |
6802771b | 94 | EXPORT_SYMBOL_GPL(dev_pm_qos_flags); |
ae0fb4b7 | 95 | |
1a9a9152 | 96 | /** |
00dc9ad1 RW |
97 | * __dev_pm_qos_read_value - Get PM QoS constraint for a given device. |
98 | * @dev: Device to get the PM QoS constraint value for. | |
99 | * | |
100 | * This routine must be called with dev->power.lock held. | |
101 | */ | |
102 | s32 __dev_pm_qos_read_value(struct device *dev) | |
103 | { | |
f90b8ad8 KK |
104 | lockdep_assert_held(&dev->power.lock); |
105 | ||
6dbf5cea | 106 | return dev_pm_qos_raw_read_value(dev); |
00dc9ad1 RW |
107 | } |
108 | ||
109 | /** | |
110 | * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked). | |
1a9a9152 RW |
111 | * @dev: Device to get the PM QoS constraint value for. |
112 | */ | |
113 | s32 dev_pm_qos_read_value(struct device *dev) | |
114 | { | |
1a9a9152 | 115 | unsigned long flags; |
00dc9ad1 | 116 | s32 ret; |
1a9a9152 RW |
117 | |
118 | spin_lock_irqsave(&dev->power.lock, flags); | |
00dc9ad1 | 119 | ret = __dev_pm_qos_read_value(dev); |
1a9a9152 RW |
120 | spin_unlock_irqrestore(&dev->power.lock, flags); |
121 | ||
122 | return ret; | |
123 | } | |
124 | ||
ae0fb4b7 RW |
125 | /** |
126 | * apply_constraint - Add/modify/remove device PM QoS request. | |
127 | * @req: Constraint request to apply | |
128 | * @action: Action to perform (add/update/remove). | |
129 | * @value: Value to assign to the QoS request. | |
b66213cd JP |
130 | * |
131 | * Internal function to update the constraints list using the PM QoS core | |
d08d1b27 | 132 | * code and if needed call the per-device callbacks. |
b66213cd JP |
133 | */ |
134 | static int apply_constraint(struct dev_pm_qos_request *req, | |
ae0fb4b7 | 135 | enum pm_qos_req_action action, s32 value) |
b66213cd | 136 | { |
ae0fb4b7 RW |
137 | struct dev_pm_qos *qos = req->dev->power.qos; |
138 | int ret; | |
b66213cd | 139 | |
ae0fb4b7 | 140 | switch(req->type) { |
b02f6695 RW |
141 | case DEV_PM_QOS_RESUME_LATENCY: |
142 | ret = pm_qos_update_target(&qos->resume_latency, | |
143 | &req->data.pnode, action, value); | |
ae0fb4b7 | 144 | break; |
2d984ad1 RW |
145 | case DEV_PM_QOS_LATENCY_TOLERANCE: |
146 | ret = pm_qos_update_target(&qos->latency_tolerance, | |
147 | &req->data.pnode, action, value); | |
148 | if (ret) { | |
149 | value = pm_qos_read_value(&qos->latency_tolerance); | |
150 | req->dev->power.set_latency_tolerance(req->dev, value); | |
151 | } | |
152 | break; | |
ae0fb4b7 RW |
153 | case DEV_PM_QOS_FLAGS: |
154 | ret = pm_qos_update_flags(&qos->flags, &req->data.flr, | |
155 | action, value); | |
156 | break; | |
157 | default: | |
158 | ret = -EINVAL; | |
b66213cd JP |
159 | } |
160 | ||
161 | return ret; | |
162 | } | |
91ff4cb8 JP |
163 | |
164 | /* | |
165 | * dev_pm_qos_constraints_allocate | |
166 | * @dev: device to allocate data for | |
167 | * | |
168 | * Called at the first call to add_request, for constraint data allocation | |
169 | * Must be called with the dev_pm_qos_mtx mutex held | |
170 | */ | |
171 | static int dev_pm_qos_constraints_allocate(struct device *dev) | |
172 | { | |
5f986c59 | 173 | struct dev_pm_qos *qos; |
91ff4cb8 JP |
174 | struct pm_qos_constraints *c; |
175 | struct blocking_notifier_head *n; | |
176 | ||
5f986c59 RW |
177 | qos = kzalloc(sizeof(*qos), GFP_KERNEL); |
178 | if (!qos) | |
91ff4cb8 JP |
179 | return -ENOMEM; |
180 | ||
181 | n = kzalloc(sizeof(*n), GFP_KERNEL); | |
182 | if (!n) { | |
5f986c59 | 183 | kfree(qos); |
91ff4cb8 JP |
184 | return -ENOMEM; |
185 | } | |
186 | BLOCKING_INIT_NOTIFIER_HEAD(n); | |
187 | ||
b02f6695 | 188 | c = &qos->resume_latency; |
1a9a9152 | 189 | plist_head_init(&c->list); |
b02f6695 RW |
190 | c->target_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE; |
191 | c->default_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE; | |
d5919dcc | 192 | c->no_constraint_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE; |
1a9a9152 RW |
193 | c->type = PM_QOS_MIN; |
194 | c->notifiers = n; | |
195 | ||
2d984ad1 RW |
196 | c = &qos->latency_tolerance; |
197 | plist_head_init(&c->list); | |
198 | c->target_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE; | |
199 | c->default_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE; | |
200 | c->no_constraint_value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT; | |
201 | c->type = PM_QOS_MIN; | |
202 | ||
ae0fb4b7 RW |
203 | INIT_LIST_HEAD(&qos->flags.list); |
204 | ||
1a9a9152 | 205 | spin_lock_irq(&dev->power.lock); |
5f986c59 | 206 | dev->power.qos = qos; |
1a9a9152 | 207 | spin_unlock_irq(&dev->power.lock); |
91ff4cb8 JP |
208 | |
209 | return 0; | |
210 | } | |
211 | ||
37530f2b RW |
212 | static void __dev_pm_qos_hide_latency_limit(struct device *dev); |
213 | static void __dev_pm_qos_hide_flags(struct device *dev); | |
91ff4cb8 JP |
214 | |
215 | /** | |
216 | * dev_pm_qos_constraints_destroy | |
217 | * @dev: target device | |
218 | * | |
1a9a9152 | 219 | * Called from the device PM subsystem on device removal under device_pm_lock(). |
91ff4cb8 JP |
220 | */ |
221 | void dev_pm_qos_constraints_destroy(struct device *dev) | |
222 | { | |
5f986c59 | 223 | struct dev_pm_qos *qos; |
91ff4cb8 | 224 | struct dev_pm_qos_request *req, *tmp; |
1a9a9152 | 225 | struct pm_qos_constraints *c; |
35546bd4 | 226 | struct pm_qos_flags *f; |
91ff4cb8 | 227 | |
0f703069 | 228 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
37530f2b | 229 | |
85dc0b8a | 230 | /* |
35546bd4 RW |
231 | * If the device's PM QoS resume latency limit or PM QoS flags have been |
232 | * exposed to user space, they have to be hidden at this point. | |
85dc0b8a | 233 | */ |
b02f6695 | 234 | pm_qos_sysfs_remove_resume_latency(dev); |
0f703069 RW |
235 | pm_qos_sysfs_remove_flags(dev); |
236 | ||
237 | mutex_lock(&dev_pm_qos_mtx); | |
238 | ||
37530f2b RW |
239 | __dev_pm_qos_hide_latency_limit(dev); |
240 | __dev_pm_qos_hide_flags(dev); | |
91ff4cb8 | 241 | |
5f986c59 RW |
242 | qos = dev->power.qos; |
243 | if (!qos) | |
1a9a9152 | 244 | goto out; |
91ff4cb8 | 245 | |
35546bd4 | 246 | /* Flush the constraints lists for the device. */ |
b02f6695 | 247 | c = &qos->resume_latency; |
021c870b | 248 | plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) { |
1a9a9152 RW |
249 | /* |
250 | * Update constraints list and call the notification | |
251 | * callbacks if needed | |
252 | */ | |
253 | apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE); | |
254 | memset(req, 0, sizeof(*req)); | |
91ff4cb8 | 255 | } |
2d984ad1 RW |
256 | c = &qos->latency_tolerance; |
257 | plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) { | |
258 | apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE); | |
259 | memset(req, 0, sizeof(*req)); | |
260 | } | |
35546bd4 RW |
261 | f = &qos->flags; |
262 | list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) { | |
263 | apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE); | |
264 | memset(req, 0, sizeof(*req)); | |
91ff4cb8 | 265 | } |
91ff4cb8 | 266 | |
1a9a9152 | 267 | spin_lock_irq(&dev->power.lock); |
37530f2b | 268 | dev->power.qos = ERR_PTR(-ENODEV); |
1a9a9152 RW |
269 | spin_unlock_irq(&dev->power.lock); |
270 | ||
e84b4a84 | 271 | kfree(qos->resume_latency.notifiers); |
9eaee2cd | 272 | kfree(qos); |
1a9a9152 RW |
273 | |
274 | out: | |
91ff4cb8 | 275 | mutex_unlock(&dev_pm_qos_mtx); |
0f703069 RW |
276 | |
277 | mutex_unlock(&dev_pm_qos_sysfs_mtx); | |
91ff4cb8 JP |
278 | } |
279 | ||
41ba8bd0 JS |
280 | static bool dev_pm_qos_invalid_req_type(struct device *dev, |
281 | enum dev_pm_qos_req_type type) | |
2d984ad1 | 282 | { |
41ba8bd0 JS |
283 | return type == DEV_PM_QOS_LATENCY_TOLERANCE && |
284 | !dev->power.set_latency_tolerance; | |
2d984ad1 RW |
285 | } |
286 | ||
287 | static int __dev_pm_qos_add_request(struct device *dev, | |
288 | struct dev_pm_qos_request *req, | |
289 | enum dev_pm_qos_req_type type, s32 value) | |
290 | { | |
291 | int ret = 0; | |
292 | ||
41ba8bd0 | 293 | if (!dev || !req || dev_pm_qos_invalid_req_type(dev, type)) |
2d984ad1 RW |
294 | return -EINVAL; |
295 | ||
296 | if (WARN(dev_pm_qos_request_active(req), | |
297 | "%s() called for already added request\n", __func__)) | |
298 | return -EINVAL; | |
299 | ||
300 | if (IS_ERR(dev->power.qos)) | |
301 | ret = -ENODEV; | |
302 | else if (!dev->power.qos) | |
303 | ret = dev_pm_qos_constraints_allocate(dev); | |
304 | ||
305 | trace_dev_pm_qos_add_request(dev_name(dev), type, value); | |
306 | if (!ret) { | |
307 | req->dev = dev; | |
308 | req->type = type; | |
309 | ret = apply_constraint(req, PM_QOS_ADD_REQ, value); | |
310 | } | |
311 | return ret; | |
312 | } | |
313 | ||
91ff4cb8 JP |
314 | /** |
315 | * dev_pm_qos_add_request - inserts new qos request into the list | |
316 | * @dev: target device for the constraint | |
317 | * @req: pointer to a preallocated handle | |
ae0fb4b7 | 318 | * @type: type of the request |
91ff4cb8 JP |
319 | * @value: defines the qos request |
320 | * | |
321 | * This function inserts a new entry in the device constraints list of | |
322 | * requested qos performance characteristics. It recomputes the aggregate | |
323 | * QoS expectations of parameters and initializes the dev_pm_qos_request | |
324 | * handle. Caller needs to save this handle for later use in updates and | |
325 | * removal. | |
326 | * | |
327 | * Returns 1 if the aggregated constraint value has changed, | |
328 | * 0 if the aggregated constraint value has not changed, | |
1a9a9152 RW |
329 | * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory |
330 | * to allocate for data structures, -ENODEV if the device has just been removed | |
331 | * from the system. | |
436ede89 RW |
332 | * |
333 | * Callers should ensure that the target device is not RPM_SUSPENDED before | |
334 | * using this function for requests of type DEV_PM_QOS_FLAGS. | |
91ff4cb8 JP |
335 | */ |
336 | int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req, | |
ae0fb4b7 | 337 | enum dev_pm_qos_req_type type, s32 value) |
91ff4cb8 | 338 | { |
2d984ad1 | 339 | int ret; |
91ff4cb8 | 340 | |
1a9a9152 | 341 | mutex_lock(&dev_pm_qos_mtx); |
2d984ad1 | 342 | ret = __dev_pm_qos_add_request(dev, req, type, value); |
91ff4cb8 JP |
343 | mutex_unlock(&dev_pm_qos_mtx); |
344 | return ret; | |
345 | } | |
346 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_request); | |
347 | ||
e39473d0 RW |
348 | /** |
349 | * __dev_pm_qos_update_request - Modify an existing device PM QoS request. | |
350 | * @req : PM QoS request to modify. | |
351 | * @new_value: New value to request. | |
352 | */ | |
353 | static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req, | |
354 | s32 new_value) | |
355 | { | |
356 | s32 curr_value; | |
357 | int ret = 0; | |
358 | ||
b81ea1b5 RW |
359 | if (!req) /*guard against callers passing in null */ |
360 | return -EINVAL; | |
361 | ||
362 | if (WARN(!dev_pm_qos_request_active(req), | |
363 | "%s() called for unknown object\n", __func__)) | |
364 | return -EINVAL; | |
365 | ||
37530f2b | 366 | if (IS_ERR_OR_NULL(req->dev->power.qos)) |
e39473d0 RW |
367 | return -ENODEV; |
368 | ||
369 | switch(req->type) { | |
b02f6695 | 370 | case DEV_PM_QOS_RESUME_LATENCY: |
2d984ad1 | 371 | case DEV_PM_QOS_LATENCY_TOLERANCE: |
e39473d0 RW |
372 | curr_value = req->data.pnode.prio; |
373 | break; | |
374 | case DEV_PM_QOS_FLAGS: | |
375 | curr_value = req->data.flr.flags; | |
376 | break; | |
377 | default: | |
378 | return -EINVAL; | |
379 | } | |
380 | ||
96d9d0b5 S |
381 | trace_dev_pm_qos_update_request(dev_name(req->dev), req->type, |
382 | new_value); | |
e39473d0 RW |
383 | if (curr_value != new_value) |
384 | ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value); | |
385 | ||
386 | return ret; | |
387 | } | |
388 | ||
91ff4cb8 JP |
389 | /** |
390 | * dev_pm_qos_update_request - modifies an existing qos request | |
391 | * @req : handle to list element holding a dev_pm_qos request to use | |
392 | * @new_value: defines the qos request | |
393 | * | |
394 | * Updates an existing dev PM qos request along with updating the | |
395 | * target value. | |
396 | * | |
397 | * Attempts are made to make this code callable on hot code paths. | |
398 | * | |
399 | * Returns 1 if the aggregated constraint value has changed, | |
400 | * 0 if the aggregated constraint value has not changed, | |
401 | * -EINVAL in case of wrong parameters, -ENODEV if the device has been | |
402 | * removed from the system | |
436ede89 RW |
403 | * |
404 | * Callers should ensure that the target device is not RPM_SUSPENDED before | |
405 | * using this function for requests of type DEV_PM_QOS_FLAGS. | |
91ff4cb8 | 406 | */ |
e39473d0 | 407 | int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value) |
91ff4cb8 | 408 | { |
e39473d0 | 409 | int ret; |
91ff4cb8 | 410 | |
b81ea1b5 RW |
411 | mutex_lock(&dev_pm_qos_mtx); |
412 | ret = __dev_pm_qos_update_request(req, new_value); | |
413 | mutex_unlock(&dev_pm_qos_mtx); | |
414 | return ret; | |
415 | } | |
416 | EXPORT_SYMBOL_GPL(dev_pm_qos_update_request); | |
417 | ||
418 | static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req) | |
419 | { | |
37530f2b | 420 | int ret; |
b81ea1b5 | 421 | |
91ff4cb8 JP |
422 | if (!req) /*guard against callers passing in null */ |
423 | return -EINVAL; | |
424 | ||
af4c720e GL |
425 | if (WARN(!dev_pm_qos_request_active(req), |
426 | "%s() called for unknown object\n", __func__)) | |
91ff4cb8 | 427 | return -EINVAL; |
91ff4cb8 | 428 | |
37530f2b RW |
429 | if (IS_ERR_OR_NULL(req->dev->power.qos)) |
430 | return -ENODEV; | |
431 | ||
96d9d0b5 S |
432 | trace_dev_pm_qos_remove_request(dev_name(req->dev), req->type, |
433 | PM_QOS_DEFAULT_VALUE); | |
37530f2b RW |
434 | ret = apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE); |
435 | memset(req, 0, sizeof(*req)); | |
91ff4cb8 JP |
436 | return ret; |
437 | } | |
91ff4cb8 JP |
438 | |
439 | /** | |
440 | * dev_pm_qos_remove_request - modifies an existing qos request | |
441 | * @req: handle to request list element | |
442 | * | |
443 | * Will remove pm qos request from the list of constraints and | |
444 | * recompute the current target value. Call this on slow code paths. | |
445 | * | |
446 | * Returns 1 if the aggregated constraint value has changed, | |
447 | * 0 if the aggregated constraint value has not changed, | |
448 | * -EINVAL in case of wrong parameters, -ENODEV if the device has been | |
449 | * removed from the system | |
436ede89 RW |
450 | * |
451 | * Callers should ensure that the target device is not RPM_SUSPENDED before | |
452 | * using this function for requests of type DEV_PM_QOS_FLAGS. | |
91ff4cb8 JP |
453 | */ |
454 | int dev_pm_qos_remove_request(struct dev_pm_qos_request *req) | |
455 | { | |
b81ea1b5 | 456 | int ret; |
91ff4cb8 JP |
457 | |
458 | mutex_lock(&dev_pm_qos_mtx); | |
b81ea1b5 | 459 | ret = __dev_pm_qos_remove_request(req); |
91ff4cb8 JP |
460 | mutex_unlock(&dev_pm_qos_mtx); |
461 | return ret; | |
462 | } | |
463 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request); | |
464 | ||
465 | /** | |
466 | * dev_pm_qos_add_notifier - sets notification entry for changes to target value | |
467 | * of per-device PM QoS constraints | |
468 | * | |
469 | * @dev: target device for the constraint | |
470 | * @notifier: notifier block managed by caller. | |
471 | * | |
472 | * Will register the notifier into a notification chain that gets called | |
473 | * upon changes to the target value for the device. | |
23e0fc5a RW |
474 | * |
475 | * If the device's constraints object doesn't exist when this routine is called, | |
476 | * it will be created (or error code will be returned if that fails). | |
91ff4cb8 JP |
477 | */ |
478 | int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier) | |
479 | { | |
23e0fc5a | 480 | int ret = 0; |
91ff4cb8 JP |
481 | |
482 | mutex_lock(&dev_pm_qos_mtx); | |
483 | ||
37530f2b RW |
484 | if (IS_ERR(dev->power.qos)) |
485 | ret = -ENODEV; | |
486 | else if (!dev->power.qos) | |
487 | ret = dev_pm_qos_constraints_allocate(dev); | |
23e0fc5a RW |
488 | |
489 | if (!ret) | |
b02f6695 RW |
490 | ret = blocking_notifier_chain_register(dev->power.qos->resume_latency.notifiers, |
491 | notifier); | |
91ff4cb8 | 492 | |
91ff4cb8 | 493 | mutex_unlock(&dev_pm_qos_mtx); |
23e0fc5a | 494 | return ret; |
91ff4cb8 JP |
495 | } |
496 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier); | |
497 | ||
498 | /** | |
499 | * dev_pm_qos_remove_notifier - deletes notification for changes to target value | |
500 | * of per-device PM QoS constraints | |
501 | * | |
502 | * @dev: target device for the constraint | |
503 | * @notifier: notifier block to be removed. | |
504 | * | |
505 | * Will remove the notifier from the notification chain that gets called | |
506 | * upon changes to the target value. | |
507 | */ | |
508 | int dev_pm_qos_remove_notifier(struct device *dev, | |
509 | struct notifier_block *notifier) | |
510 | { | |
511 | int retval = 0; | |
512 | ||
513 | mutex_lock(&dev_pm_qos_mtx); | |
514 | ||
1a9a9152 | 515 | /* Silently return if the constraints object is not present. */ |
37530f2b | 516 | if (!IS_ERR_OR_NULL(dev->power.qos)) |
b02f6695 RW |
517 | retval = blocking_notifier_chain_unregister(dev->power.qos->resume_latency.notifiers, |
518 | notifier); | |
91ff4cb8 | 519 | |
91ff4cb8 JP |
520 | mutex_unlock(&dev_pm_qos_mtx); |
521 | return retval; | |
522 | } | |
523 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier); | |
b66213cd | 524 | |
40a5f8be RW |
525 | /** |
526 | * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor. | |
527 | * @dev: Device whose ancestor to add the request for. | |
528 | * @req: Pointer to the preallocated handle. | |
71d821fd | 529 | * @type: Type of the request. |
40a5f8be RW |
530 | * @value: Constraint latency value. |
531 | */ | |
532 | int dev_pm_qos_add_ancestor_request(struct device *dev, | |
71d821fd RW |
533 | struct dev_pm_qos_request *req, |
534 | enum dev_pm_qos_req_type type, s32 value) | |
40a5f8be RW |
535 | { |
536 | struct device *ancestor = dev->parent; | |
4ce47802 | 537 | int ret = -ENODEV; |
40a5f8be | 538 | |
71d821fd RW |
539 | switch (type) { |
540 | case DEV_PM_QOS_RESUME_LATENCY: | |
541 | while (ancestor && !ancestor->power.ignore_children) | |
542 | ancestor = ancestor->parent; | |
40a5f8be | 543 | |
71d821fd RW |
544 | break; |
545 | case DEV_PM_QOS_LATENCY_TOLERANCE: | |
546 | while (ancestor && !ancestor->power.set_latency_tolerance) | |
547 | ancestor = ancestor->parent; | |
548 | ||
549 | break; | |
550 | default: | |
551 | ancestor = NULL; | |
552 | } | |
40a5f8be | 553 | if (ancestor) |
71d821fd | 554 | ret = dev_pm_qos_add_request(ancestor, req, type, value); |
40a5f8be | 555 | |
4ce47802 | 556 | if (ret < 0) |
40a5f8be RW |
557 | req->dev = NULL; |
558 | ||
4ce47802 | 559 | return ret; |
40a5f8be RW |
560 | } |
561 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request); | |
85dc0b8a | 562 | |
e39473d0 RW |
563 | static void __dev_pm_qos_drop_user_request(struct device *dev, |
564 | enum dev_pm_qos_req_type type) | |
85dc0b8a | 565 | { |
b81ea1b5 RW |
566 | struct dev_pm_qos_request *req = NULL; |
567 | ||
e39473d0 | 568 | switch(type) { |
b02f6695 RW |
569 | case DEV_PM_QOS_RESUME_LATENCY: |
570 | req = dev->power.qos->resume_latency_req; | |
571 | dev->power.qos->resume_latency_req = NULL; | |
e39473d0 | 572 | break; |
2d984ad1 RW |
573 | case DEV_PM_QOS_LATENCY_TOLERANCE: |
574 | req = dev->power.qos->latency_tolerance_req; | |
575 | dev->power.qos->latency_tolerance_req = NULL; | |
576 | break; | |
e39473d0 | 577 | case DEV_PM_QOS_FLAGS: |
b81ea1b5 | 578 | req = dev->power.qos->flags_req; |
e39473d0 RW |
579 | dev->power.qos->flags_req = NULL; |
580 | break; | |
581 | } | |
b81ea1b5 RW |
582 | __dev_pm_qos_remove_request(req); |
583 | kfree(req); | |
85dc0b8a RW |
584 | } |
585 | ||
0f703069 RW |
586 | static void dev_pm_qos_drop_user_request(struct device *dev, |
587 | enum dev_pm_qos_req_type type) | |
588 | { | |
589 | mutex_lock(&dev_pm_qos_mtx); | |
590 | __dev_pm_qos_drop_user_request(dev, type); | |
591 | mutex_unlock(&dev_pm_qos_mtx); | |
592 | } | |
593 | ||
85dc0b8a RW |
594 | /** |
595 | * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space. | |
596 | * @dev: Device whose PM QoS latency limit is to be exposed to user space. | |
597 | * @value: Initial value of the latency limit. | |
598 | */ | |
599 | int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value) | |
600 | { | |
601 | struct dev_pm_qos_request *req; | |
602 | int ret; | |
603 | ||
604 | if (!device_is_registered(dev) || value < 0) | |
605 | return -EINVAL; | |
606 | ||
85dc0b8a RW |
607 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
608 | if (!req) | |
609 | return -ENOMEM; | |
610 | ||
b02f6695 | 611 | ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_RESUME_LATENCY, value); |
b81ea1b5 RW |
612 | if (ret < 0) { |
613 | kfree(req); | |
85dc0b8a | 614 | return ret; |
b81ea1b5 RW |
615 | } |
616 | ||
0f703069 RW |
617 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
618 | ||
b81ea1b5 RW |
619 | mutex_lock(&dev_pm_qos_mtx); |
620 | ||
37530f2b | 621 | if (IS_ERR_OR_NULL(dev->power.qos)) |
b81ea1b5 | 622 | ret = -ENODEV; |
b02f6695 | 623 | else if (dev->power.qos->resume_latency_req) |
b81ea1b5 RW |
624 | ret = -EEXIST; |
625 | ||
626 | if (ret < 0) { | |
627 | __dev_pm_qos_remove_request(req); | |
628 | kfree(req); | |
0f703069 | 629 | mutex_unlock(&dev_pm_qos_mtx); |
b81ea1b5 RW |
630 | goto out; |
631 | } | |
b02f6695 | 632 | dev->power.qos->resume_latency_req = req; |
0f703069 RW |
633 | |
634 | mutex_unlock(&dev_pm_qos_mtx); | |
635 | ||
b02f6695 | 636 | ret = pm_qos_sysfs_add_resume_latency(dev); |
85dc0b8a | 637 | if (ret) |
b02f6695 | 638 | dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY); |
85dc0b8a | 639 | |
b81ea1b5 | 640 | out: |
0f703069 | 641 | mutex_unlock(&dev_pm_qos_sysfs_mtx); |
85dc0b8a RW |
642 | return ret; |
643 | } | |
644 | EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit); | |
645 | ||
37530f2b RW |
646 | static void __dev_pm_qos_hide_latency_limit(struct device *dev) |
647 | { | |
b02f6695 RW |
648 | if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->resume_latency_req) |
649 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY); | |
37530f2b RW |
650 | } |
651 | ||
85dc0b8a RW |
652 | /** |
653 | * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space. | |
654 | * @dev: Device whose PM QoS latency limit is to be hidden from user space. | |
655 | */ | |
656 | void dev_pm_qos_hide_latency_limit(struct device *dev) | |
657 | { | |
0f703069 RW |
658 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
659 | ||
b02f6695 | 660 | pm_qos_sysfs_remove_resume_latency(dev); |
0f703069 | 661 | |
b81ea1b5 | 662 | mutex_lock(&dev_pm_qos_mtx); |
37530f2b | 663 | __dev_pm_qos_hide_latency_limit(dev); |
b81ea1b5 | 664 | mutex_unlock(&dev_pm_qos_mtx); |
0f703069 RW |
665 | |
666 | mutex_unlock(&dev_pm_qos_sysfs_mtx); | |
85dc0b8a RW |
667 | } |
668 | EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit); | |
e39473d0 RW |
669 | |
670 | /** | |
671 | * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space. | |
672 | * @dev: Device whose PM QoS flags are to be exposed to user space. | |
673 | * @val: Initial values of the flags. | |
674 | */ | |
675 | int dev_pm_qos_expose_flags(struct device *dev, s32 val) | |
676 | { | |
677 | struct dev_pm_qos_request *req; | |
678 | int ret; | |
679 | ||
680 | if (!device_is_registered(dev)) | |
681 | return -EINVAL; | |
682 | ||
e39473d0 RW |
683 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
684 | if (!req) | |
685 | return -ENOMEM; | |
686 | ||
687 | ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val); | |
b81ea1b5 RW |
688 | if (ret < 0) { |
689 | kfree(req); | |
690 | return ret; | |
691 | } | |
692 | ||
693 | pm_runtime_get_sync(dev); | |
0f703069 RW |
694 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
695 | ||
b81ea1b5 RW |
696 | mutex_lock(&dev_pm_qos_mtx); |
697 | ||
37530f2b | 698 | if (IS_ERR_OR_NULL(dev->power.qos)) |
b81ea1b5 RW |
699 | ret = -ENODEV; |
700 | else if (dev->power.qos->flags_req) | |
701 | ret = -EEXIST; | |
702 | ||
703 | if (ret < 0) { | |
704 | __dev_pm_qos_remove_request(req); | |
705 | kfree(req); | |
0f703069 | 706 | mutex_unlock(&dev_pm_qos_mtx); |
b81ea1b5 RW |
707 | goto out; |
708 | } | |
e39473d0 | 709 | dev->power.qos->flags_req = req; |
0f703069 RW |
710 | |
711 | mutex_unlock(&dev_pm_qos_mtx); | |
712 | ||
e39473d0 RW |
713 | ret = pm_qos_sysfs_add_flags(dev); |
714 | if (ret) | |
0f703069 | 715 | dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS); |
e39473d0 | 716 | |
b81ea1b5 | 717 | out: |
0f703069 | 718 | mutex_unlock(&dev_pm_qos_sysfs_mtx); |
7e4d6844 | 719 | pm_runtime_put(dev); |
e39473d0 RW |
720 | return ret; |
721 | } | |
722 | EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags); | |
723 | ||
37530f2b RW |
724 | static void __dev_pm_qos_hide_flags(struct device *dev) |
725 | { | |
0f703069 | 726 | if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req) |
37530f2b | 727 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS); |
37530f2b RW |
728 | } |
729 | ||
e39473d0 RW |
730 | /** |
731 | * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space. | |
732 | * @dev: Device whose PM QoS flags are to be hidden from user space. | |
733 | */ | |
734 | void dev_pm_qos_hide_flags(struct device *dev) | |
735 | { | |
b81ea1b5 | 736 | pm_runtime_get_sync(dev); |
0f703069 RW |
737 | mutex_lock(&dev_pm_qos_sysfs_mtx); |
738 | ||
739 | pm_qos_sysfs_remove_flags(dev); | |
740 | ||
b81ea1b5 | 741 | mutex_lock(&dev_pm_qos_mtx); |
37530f2b | 742 | __dev_pm_qos_hide_flags(dev); |
b81ea1b5 | 743 | mutex_unlock(&dev_pm_qos_mtx); |
0f703069 RW |
744 | |
745 | mutex_unlock(&dev_pm_qos_sysfs_mtx); | |
b81ea1b5 | 746 | pm_runtime_put(dev); |
e39473d0 RW |
747 | } |
748 | EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags); | |
749 | ||
750 | /** | |
751 | * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space. | |
752 | * @dev: Device to update the PM QoS flags request for. | |
753 | * @mask: Flags to set/clear. | |
754 | * @set: Whether to set or clear the flags (true means set). | |
755 | */ | |
756 | int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set) | |
757 | { | |
758 | s32 value; | |
759 | int ret; | |
760 | ||
e39473d0 RW |
761 | pm_runtime_get_sync(dev); |
762 | mutex_lock(&dev_pm_qos_mtx); | |
763 | ||
37530f2b | 764 | if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) { |
b81ea1b5 RW |
765 | ret = -EINVAL; |
766 | goto out; | |
767 | } | |
768 | ||
e39473d0 RW |
769 | value = dev_pm_qos_requested_flags(dev); |
770 | if (set) | |
771 | value |= mask; | |
772 | else | |
773 | value &= ~mask; | |
774 | ||
775 | ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value); | |
776 | ||
b81ea1b5 | 777 | out: |
e39473d0 RW |
778 | mutex_unlock(&dev_pm_qos_mtx); |
779 | pm_runtime_put(dev); | |
e39473d0 RW |
780 | return ret; |
781 | } | |
2d984ad1 RW |
782 | |
783 | /** | |
784 | * dev_pm_qos_get_user_latency_tolerance - Get user space latency tolerance. | |
785 | * @dev: Device to obtain the user space latency tolerance for. | |
786 | */ | |
787 | s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev) | |
788 | { | |
789 | s32 ret; | |
790 | ||
791 | mutex_lock(&dev_pm_qos_mtx); | |
792 | ret = IS_ERR_OR_NULL(dev->power.qos) | |
793 | || !dev->power.qos->latency_tolerance_req ? | |
794 | PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT : | |
795 | dev->power.qos->latency_tolerance_req->data.pnode.prio; | |
796 | mutex_unlock(&dev_pm_qos_mtx); | |
797 | return ret; | |
798 | } | |
799 | ||
800 | /** | |
801 | * dev_pm_qos_update_user_latency_tolerance - Update user space latency tolerance. | |
802 | * @dev: Device to update the user space latency tolerance for. | |
803 | * @val: New user space latency tolerance for @dev (negative values disable). | |
804 | */ | |
805 | int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val) | |
806 | { | |
807 | int ret; | |
808 | ||
809 | mutex_lock(&dev_pm_qos_mtx); | |
810 | ||
811 | if (IS_ERR_OR_NULL(dev->power.qos) | |
812 | || !dev->power.qos->latency_tolerance_req) { | |
813 | struct dev_pm_qos_request *req; | |
814 | ||
815 | if (val < 0) { | |
80a6f7c7 AL |
816 | if (val == PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT) |
817 | ret = 0; | |
818 | else | |
819 | ret = -EINVAL; | |
2d984ad1 RW |
820 | goto out; |
821 | } | |
822 | req = kzalloc(sizeof(*req), GFP_KERNEL); | |
823 | if (!req) { | |
824 | ret = -ENOMEM; | |
825 | goto out; | |
826 | } | |
827 | ret = __dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY_TOLERANCE, val); | |
828 | if (ret < 0) { | |
829 | kfree(req); | |
830 | goto out; | |
831 | } | |
832 | dev->power.qos->latency_tolerance_req = req; | |
833 | } else { | |
834 | if (val < 0) { | |
835 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY_TOLERANCE); | |
836 | ret = 0; | |
837 | } else { | |
838 | ret = __dev_pm_qos_update_request(dev->power.qos->latency_tolerance_req, val); | |
839 | } | |
840 | } | |
841 | ||
842 | out: | |
843 | mutex_unlock(&dev_pm_qos_mtx); | |
844 | return ret; | |
845 | } | |
034e7906 | 846 | EXPORT_SYMBOL_GPL(dev_pm_qos_update_user_latency_tolerance); |
13b2c4a0 MW |
847 | |
848 | /** | |
849 | * dev_pm_qos_expose_latency_tolerance - Expose latency tolerance to userspace | |
850 | * @dev: Device whose latency tolerance to expose | |
851 | */ | |
852 | int dev_pm_qos_expose_latency_tolerance(struct device *dev) | |
853 | { | |
854 | int ret; | |
855 | ||
856 | if (!dev->power.set_latency_tolerance) | |
857 | return -EINVAL; | |
858 | ||
859 | mutex_lock(&dev_pm_qos_sysfs_mtx); | |
860 | ret = pm_qos_sysfs_add_latency_tolerance(dev); | |
861 | mutex_unlock(&dev_pm_qos_sysfs_mtx); | |
862 | ||
863 | return ret; | |
864 | } | |
865 | EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_tolerance); | |
866 | ||
867 | /** | |
868 | * dev_pm_qos_hide_latency_tolerance - Hide latency tolerance from userspace | |
869 | * @dev: Device whose latency tolerance to hide | |
870 | */ | |
871 | void dev_pm_qos_hide_latency_tolerance(struct device *dev) | |
872 | { | |
873 | mutex_lock(&dev_pm_qos_sysfs_mtx); | |
874 | pm_qos_sysfs_remove_latency_tolerance(dev); | |
875 | mutex_unlock(&dev_pm_qos_sysfs_mtx); | |
876 | ||
877 | /* Remove the request from user space now */ | |
878 | pm_runtime_get_sync(dev); | |
879 | dev_pm_qos_update_user_latency_tolerance(dev, | |
880 | PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT); | |
881 | pm_runtime_put(dev); | |
882 | } | |
883 | EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_tolerance); |