blob: 7f3646e459cbacd399c6ac7520dd1e37aa2bcb10 [file] [log] [blame]
Jean Pihet91ff4cb2011-08-25 15:35:41 +02001/*
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.
Jean Pihetb66213c2011-08-25 15:35:47 +020020 * Watchers can register different types of notification callbacks:
21 * . a per-device notification callback using the dev_pm_qos_*_notifier API.
22 * The notification chain data is stored in the per-device constraint
23 * data struct.
24 * . a system-wide notification callback using the dev_pm_qos_*_global_notifier
25 * API. The notification chain data is stored in a static variable.
Jean Pihet91ff4cb2011-08-25 15:35:41 +020026 *
27 * Note about the per-device constraint data struct allocation:
28 * . The per-device constraints data struct ptr is tored into the device
29 * dev_pm_info.
30 * . To minimize the data usage by the per-device constraints, the data struct
31 * is only allocated at the first call to dev_pm_qos_add_request.
32 * . The data is later free'd when the device is removed from the system.
Jean Pihet91ff4cb2011-08-25 15:35:41 +020033 * . A global mutex protects the constraints users from the data being
34 * allocated and free'd.
35 */
36
37#include <linux/pm_qos.h>
38#include <linux/spinlock.h>
39#include <linux/slab.h>
40#include <linux/device.h>
41#include <linux/mutex.h>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040042#include <linux/export.h>
Rafael J. Wysockie39473d2012-10-24 02:08:18 +020043#include <linux/pm_runtime.h>
Rafael J. Wysocki37530f22013-03-04 14:22:57 +010044#include <linux/err.h>
Sahara96d9d0b2013-06-21 11:12:30 +090045#include <trace/events/power.h>
Jean Pihet91ff4cb2011-08-25 15:35:41 +020046
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +010047#include "power.h"
Jean Pihet91ff4cb2011-08-25 15:35:41 +020048
49static DEFINE_MUTEX(dev_pm_qos_mtx);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +020050static DEFINE_MUTEX(dev_pm_qos_sysfs_mtx);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +020051
Jean Pihetb66213c2011-08-25 15:35:47 +020052static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
53
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +020054/**
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020055 * __dev_pm_qos_flags - Check PM QoS flags for a given device.
56 * @dev: Device to check the PM QoS flags for.
57 * @mask: Flags to check against.
58 *
59 * This routine must be called with dev->power.lock held.
60 */
61enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask)
62{
63 struct dev_pm_qos *qos = dev->power.qos;
64 struct pm_qos_flags *pqf;
65 s32 val;
66
Krzysztof Kozlowskif90b8ad2015-01-09 09:27:58 +010067 lockdep_assert_held(&dev->power.lock);
68
Rafael J. Wysocki37530f22013-03-04 14:22:57 +010069 if (IS_ERR_OR_NULL(qos))
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020070 return PM_QOS_FLAGS_UNDEFINED;
71
72 pqf = &qos->flags;
73 if (list_empty(&pqf->list))
74 return PM_QOS_FLAGS_UNDEFINED;
75
76 val = pqf->effective_flags & mask;
77 if (val)
78 return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME;
79
80 return PM_QOS_FLAGS_NONE;
81}
82
83/**
84 * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
85 * @dev: Device to check the PM QoS flags for.
86 * @mask: Flags to check against.
87 */
88enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask)
89{
90 unsigned long irqflags;
91 enum pm_qos_flags_status ret;
92
93 spin_lock_irqsave(&dev->power.lock, irqflags);
94 ret = __dev_pm_qos_flags(dev, mask);
95 spin_unlock_irqrestore(&dev->power.lock, irqflags);
96
97 return ret;
98}
Lan Tianyu6802771b2013-01-23 04:26:28 +080099EXPORT_SYMBOL_GPL(dev_pm_qos_flags);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200100
101/**
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100102 * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
103 * @dev: Device to get the PM QoS constraint value for.
104 *
105 * This routine must be called with dev->power.lock held.
106 */
107s32 __dev_pm_qos_read_value(struct device *dev)
108{
Krzysztof Kozlowskif90b8ad2015-01-09 09:27:58 +0100109 lockdep_assert_held(&dev->power.lock);
110
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100111 return IS_ERR_OR_NULL(dev->power.qos) ?
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100112 0 : pm_qos_read_value(&dev->power.qos->resume_latency);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100113}
114
115/**
116 * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200117 * @dev: Device to get the PM QoS constraint value for.
118 */
119s32 dev_pm_qos_read_value(struct device *dev)
120{
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200121 unsigned long flags;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100122 s32 ret;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200123
124 spin_lock_irqsave(&dev->power.lock, flags);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100125 ret = __dev_pm_qos_read_value(dev);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200126 spin_unlock_irqrestore(&dev->power.lock, flags);
127
128 return ret;
129}
130
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200131/**
132 * apply_constraint - Add/modify/remove device PM QoS request.
133 * @req: Constraint request to apply
134 * @action: Action to perform (add/update/remove).
135 * @value: Value to assign to the QoS request.
Jean Pihetb66213c2011-08-25 15:35:47 +0200136 *
137 * Internal function to update the constraints list using the PM QoS core
138 * code and if needed call the per-device and the global notification
139 * callbacks
140 */
141static int apply_constraint(struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200142 enum pm_qos_req_action action, s32 value)
Jean Pihetb66213c2011-08-25 15:35:47 +0200143{
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200144 struct dev_pm_qos *qos = req->dev->power.qos;
145 int ret;
Jean Pihetb66213c2011-08-25 15:35:47 +0200146
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200147 switch(req->type) {
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100148 case DEV_PM_QOS_RESUME_LATENCY:
149 ret = pm_qos_update_target(&qos->resume_latency,
150 &req->data.pnode, action, value);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200151 if (ret) {
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100152 value = pm_qos_read_value(&qos->resume_latency);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200153 blocking_notifier_call_chain(&dev_pm_notifiers,
154 (unsigned long)value,
155 req);
156 }
157 break;
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100158 case DEV_PM_QOS_LATENCY_TOLERANCE:
159 ret = pm_qos_update_target(&qos->latency_tolerance,
160 &req->data.pnode, action, value);
161 if (ret) {
162 value = pm_qos_read_value(&qos->latency_tolerance);
163 req->dev->power.set_latency_tolerance(req->dev, value);
164 }
165 break;
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200166 case DEV_PM_QOS_FLAGS:
167 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
168 action, value);
169 break;
170 default:
171 ret = -EINVAL;
Jean Pihetb66213c2011-08-25 15:35:47 +0200172 }
173
174 return ret;
175}
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200176
177/*
178 * dev_pm_qos_constraints_allocate
179 * @dev: device to allocate data for
180 *
181 * Called at the first call to add_request, for constraint data allocation
182 * Must be called with the dev_pm_qos_mtx mutex held
183 */
184static int dev_pm_qos_constraints_allocate(struct device *dev)
185{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200186 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200187 struct pm_qos_constraints *c;
188 struct blocking_notifier_head *n;
189
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200190 qos = kzalloc(sizeof(*qos), GFP_KERNEL);
191 if (!qos)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200192 return -ENOMEM;
193
194 n = kzalloc(sizeof(*n), GFP_KERNEL);
195 if (!n) {
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200196 kfree(qos);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200197 return -ENOMEM;
198 }
199 BLOCKING_INIT_NOTIFIER_HEAD(n);
200
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100201 c = &qos->resume_latency;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200202 plist_head_init(&c->list);
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100203 c->target_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
204 c->default_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
Rafael J. Wysocki327adae2014-02-11 00:35:29 +0100205 c->no_constraint_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200206 c->type = PM_QOS_MIN;
207 c->notifiers = n;
208
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100209 c = &qos->latency_tolerance;
210 plist_head_init(&c->list);
211 c->target_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
212 c->default_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
213 c->no_constraint_value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
214 c->type = PM_QOS_MIN;
215
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200216 INIT_LIST_HEAD(&qos->flags.list);
217
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200218 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200219 dev->power.qos = qos;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200220 spin_unlock_irq(&dev->power.lock);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200221
222 return 0;
223}
224
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100225static void __dev_pm_qos_hide_latency_limit(struct device *dev);
226static void __dev_pm_qos_hide_flags(struct device *dev);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200227
228/**
229 * dev_pm_qos_constraints_destroy
230 * @dev: target device
231 *
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200232 * Called from the device PM subsystem on device removal under device_pm_lock().
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200233 */
234void dev_pm_qos_constraints_destroy(struct device *dev)
235{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200236 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200237 struct dev_pm_qos_request *req, *tmp;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200238 struct pm_qos_constraints *c;
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100239 struct pm_qos_flags *f;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200240
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200241 mutex_lock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100242
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100243 /*
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100244 * If the device's PM QoS resume latency limit or PM QoS flags have been
245 * exposed to user space, they have to be hidden at this point.
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100246 */
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100247 pm_qos_sysfs_remove_resume_latency(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200248 pm_qos_sysfs_remove_flags(dev);
249
250 mutex_lock(&dev_pm_qos_mtx);
251
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100252 __dev_pm_qos_hide_latency_limit(dev);
253 __dev_pm_qos_hide_flags(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100254
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200255 qos = dev->power.qos;
256 if (!qos)
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200257 goto out;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200258
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100259 /* Flush the constraints lists for the device. */
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100260 c = &qos->resume_latency;
Rafael J. Wysocki021c8702012-10-23 01:09:00 +0200261 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200262 /*
263 * Update constraints list and call the notification
264 * callbacks if needed
265 */
266 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
267 memset(req, 0, sizeof(*req));
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200268 }
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100269 c = &qos->latency_tolerance;
270 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
271 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
272 memset(req, 0, sizeof(*req));
273 }
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100274 f = &qos->flags;
275 list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
276 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
277 memset(req, 0, sizeof(*req));
278 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200279
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200280 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100281 dev->power.qos = ERR_PTR(-ENODEV);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200282 spin_unlock_irq(&dev->power.lock);
283
284 kfree(c->notifiers);
Lan,Tianyu9eaee2c2012-11-01 22:45:30 +0100285 kfree(qos);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200286
287 out:
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200288 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200289
290 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200291}
292
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100293static bool dev_pm_qos_invalid_request(struct device *dev,
294 struct dev_pm_qos_request *req)
295{
296 return !req || (req->type == DEV_PM_QOS_LATENCY_TOLERANCE
297 && !dev->power.set_latency_tolerance);
298}
299
300static int __dev_pm_qos_add_request(struct device *dev,
301 struct dev_pm_qos_request *req,
302 enum dev_pm_qos_req_type type, s32 value)
303{
304 int ret = 0;
305
306 if (!dev || dev_pm_qos_invalid_request(dev, req))
307 return -EINVAL;
308
309 if (WARN(dev_pm_qos_request_active(req),
310 "%s() called for already added request\n", __func__))
311 return -EINVAL;
312
313 if (IS_ERR(dev->power.qos))
314 ret = -ENODEV;
315 else if (!dev->power.qos)
316 ret = dev_pm_qos_constraints_allocate(dev);
317
318 trace_dev_pm_qos_add_request(dev_name(dev), type, value);
319 if (!ret) {
320 req->dev = dev;
321 req->type = type;
322 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
323 }
324 return ret;
325}
326
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200327/**
328 * dev_pm_qos_add_request - inserts new qos request into the list
329 * @dev: target device for the constraint
330 * @req: pointer to a preallocated handle
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200331 * @type: type of the request
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200332 * @value: defines the qos request
333 *
334 * This function inserts a new entry in the device constraints list of
335 * requested qos performance characteristics. It recomputes the aggregate
336 * QoS expectations of parameters and initializes the dev_pm_qos_request
337 * handle. Caller needs to save this handle for later use in updates and
338 * removal.
339 *
340 * Returns 1 if the aggregated constraint value has changed,
341 * 0 if the aggregated constraint value has not changed,
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200342 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
343 * to allocate for data structures, -ENODEV if the device has just been removed
344 * from the system.
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100345 *
346 * Callers should ensure that the target device is not RPM_SUSPENDED before
347 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200348 */
349int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200350 enum dev_pm_qos_req_type type, s32 value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200351{
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100352 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200353
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200354 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100355 ret = __dev_pm_qos_add_request(dev, req, type, value);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200356 mutex_unlock(&dev_pm_qos_mtx);
357 return ret;
358}
359EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
360
361/**
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200362 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
363 * @req : PM QoS request to modify.
364 * @new_value: New value to request.
365 */
366static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
367 s32 new_value)
368{
369 s32 curr_value;
370 int ret = 0;
371
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100372 if (!req) /*guard against callers passing in null */
373 return -EINVAL;
374
375 if (WARN(!dev_pm_qos_request_active(req),
376 "%s() called for unknown object\n", __func__))
377 return -EINVAL;
378
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100379 if (IS_ERR_OR_NULL(req->dev->power.qos))
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200380 return -ENODEV;
381
382 switch(req->type) {
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100383 case DEV_PM_QOS_RESUME_LATENCY:
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100384 case DEV_PM_QOS_LATENCY_TOLERANCE:
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200385 curr_value = req->data.pnode.prio;
386 break;
387 case DEV_PM_QOS_FLAGS:
388 curr_value = req->data.flr.flags;
389 break;
390 default:
391 return -EINVAL;
392 }
393
Sahara96d9d0b2013-06-21 11:12:30 +0900394 trace_dev_pm_qos_update_request(dev_name(req->dev), req->type,
395 new_value);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200396 if (curr_value != new_value)
397 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
398
399 return ret;
400}
401
402/**
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200403 * dev_pm_qos_update_request - modifies an existing qos request
404 * @req : handle to list element holding a dev_pm_qos request to use
405 * @new_value: defines the qos request
406 *
407 * Updates an existing dev PM qos request along with updating the
408 * target value.
409 *
410 * Attempts are made to make this code callable on hot code paths.
411 *
412 * Returns 1 if the aggregated constraint value has changed,
413 * 0 if the aggregated constraint value has not changed,
414 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
415 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100416 *
417 * Callers should ensure that the target device is not RPM_SUSPENDED before
418 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200419 */
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200420int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200421{
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200422 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200423
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100424 mutex_lock(&dev_pm_qos_mtx);
425 ret = __dev_pm_qos_update_request(req, new_value);
426 mutex_unlock(&dev_pm_qos_mtx);
427 return ret;
428}
429EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
430
431static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
432{
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100433 int ret;
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100434
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200435 if (!req) /*guard against callers passing in null */
436 return -EINVAL;
437
Guennadi Liakhovetskiaf4c7202011-11-10 00:44:18 +0100438 if (WARN(!dev_pm_qos_request_active(req),
439 "%s() called for unknown object\n", __func__))
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200440 return -EINVAL;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200441
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100442 if (IS_ERR_OR_NULL(req->dev->power.qos))
443 return -ENODEV;
444
Sahara96d9d0b2013-06-21 11:12:30 +0900445 trace_dev_pm_qos_remove_request(dev_name(req->dev), req->type,
446 PM_QOS_DEFAULT_VALUE);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100447 ret = apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
448 memset(req, 0, sizeof(*req));
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200449 return ret;
450}
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200451
452/**
453 * dev_pm_qos_remove_request - modifies an existing qos request
454 * @req: handle to request list element
455 *
456 * Will remove pm qos request from the list of constraints and
457 * recompute the current target value. Call this on slow code paths.
458 *
459 * Returns 1 if the aggregated constraint value has changed,
460 * 0 if the aggregated constraint value has not changed,
461 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
462 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100463 *
464 * Callers should ensure that the target device is not RPM_SUSPENDED before
465 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200466 */
467int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
468{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100469 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200470
471 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100472 ret = __dev_pm_qos_remove_request(req);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200473 mutex_unlock(&dev_pm_qos_mtx);
474 return ret;
475}
476EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
477
478/**
479 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
480 * of per-device PM QoS constraints
481 *
482 * @dev: target device for the constraint
483 * @notifier: notifier block managed by caller.
484 *
485 * Will register the notifier into a notification chain that gets called
486 * upon changes to the target value for the device.
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200487 *
488 * If the device's constraints object doesn't exist when this routine is called,
489 * it will be created (or error code will be returned if that fails).
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200490 */
491int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
492{
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200493 int ret = 0;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200494
495 mutex_lock(&dev_pm_qos_mtx);
496
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100497 if (IS_ERR(dev->power.qos))
498 ret = -ENODEV;
499 else if (!dev->power.qos)
500 ret = dev_pm_qos_constraints_allocate(dev);
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200501
502 if (!ret)
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100503 ret = blocking_notifier_chain_register(dev->power.qos->resume_latency.notifiers,
504 notifier);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200505
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200506 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200507 return ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200508}
509EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
510
511/**
512 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
513 * of per-device PM QoS constraints
514 *
515 * @dev: target device for the constraint
516 * @notifier: notifier block to be removed.
517 *
518 * Will remove the notifier from the notification chain that gets called
519 * upon changes to the target value.
520 */
521int dev_pm_qos_remove_notifier(struct device *dev,
522 struct notifier_block *notifier)
523{
524 int retval = 0;
525
526 mutex_lock(&dev_pm_qos_mtx);
527
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200528 /* Silently return if the constraints object is not present. */
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100529 if (!IS_ERR_OR_NULL(dev->power.qos))
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100530 retval = blocking_notifier_chain_unregister(dev->power.qos->resume_latency.notifiers,
531 notifier);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200532
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200533 mutex_unlock(&dev_pm_qos_mtx);
534 return retval;
535}
536EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
Jean Pihetb66213c2011-08-25 15:35:47 +0200537
538/**
539 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
540 * target value of the PM QoS constraints for any device
541 *
542 * @notifier: notifier block managed by caller.
543 *
544 * Will register the notifier into a notification chain that gets called
545 * upon changes to the target value for any device.
546 */
547int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
548{
549 return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
550}
551EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
552
553/**
554 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
555 * target value of PM QoS constraints for any device
556 *
557 * @notifier: notifier block to be removed.
558 *
559 * Will remove the notifier from the notification chain that gets called
560 * upon changes to the target value for any device.
561 */
562int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
563{
564 return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
565}
566EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100567
568/**
569 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
570 * @dev: Device whose ancestor to add the request for.
571 * @req: Pointer to the preallocated handle.
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100572 * @type: Type of the request.
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100573 * @value: Constraint latency value.
574 */
575int dev_pm_qos_add_ancestor_request(struct device *dev,
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100576 struct dev_pm_qos_request *req,
577 enum dev_pm_qos_req_type type, s32 value)
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100578{
579 struct device *ancestor = dev->parent;
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100580 int ret = -ENODEV;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100581
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100582 switch (type) {
583 case DEV_PM_QOS_RESUME_LATENCY:
584 while (ancestor && !ancestor->power.ignore_children)
585 ancestor = ancestor->parent;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100586
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100587 break;
588 case DEV_PM_QOS_LATENCY_TOLERANCE:
589 while (ancestor && !ancestor->power.set_latency_tolerance)
590 ancestor = ancestor->parent;
591
592 break;
593 default:
594 ancestor = NULL;
595 }
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100596 if (ancestor)
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100597 ret = dev_pm_qos_add_request(ancestor, req, type, value);
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100598
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100599 if (ret < 0)
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100600 req->dev = NULL;
601
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100602 return ret;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100603}
604EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100605
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200606static void __dev_pm_qos_drop_user_request(struct device *dev,
607 enum dev_pm_qos_req_type type)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100608{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100609 struct dev_pm_qos_request *req = NULL;
610
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200611 switch(type) {
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100612 case DEV_PM_QOS_RESUME_LATENCY:
613 req = dev->power.qos->resume_latency_req;
614 dev->power.qos->resume_latency_req = NULL;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200615 break;
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100616 case DEV_PM_QOS_LATENCY_TOLERANCE:
617 req = dev->power.qos->latency_tolerance_req;
618 dev->power.qos->latency_tolerance_req = NULL;
619 break;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200620 case DEV_PM_QOS_FLAGS:
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100621 req = dev->power.qos->flags_req;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200622 dev->power.qos->flags_req = NULL;
623 break;
624 }
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100625 __dev_pm_qos_remove_request(req);
626 kfree(req);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100627}
628
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200629static void dev_pm_qos_drop_user_request(struct device *dev,
630 enum dev_pm_qos_req_type type)
631{
632 mutex_lock(&dev_pm_qos_mtx);
633 __dev_pm_qos_drop_user_request(dev, type);
634 mutex_unlock(&dev_pm_qos_mtx);
635}
636
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100637/**
638 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
639 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
640 * @value: Initial value of the latency limit.
641 */
642int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
643{
644 struct dev_pm_qos_request *req;
645 int ret;
646
647 if (!device_is_registered(dev) || value < 0)
648 return -EINVAL;
649
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100650 req = kzalloc(sizeof(*req), GFP_KERNEL);
651 if (!req)
652 return -ENOMEM;
653
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100654 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_RESUME_LATENCY, value);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100655 if (ret < 0) {
656 kfree(req);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100657 return ret;
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100658 }
659
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200660 mutex_lock(&dev_pm_qos_sysfs_mtx);
661
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100662 mutex_lock(&dev_pm_qos_mtx);
663
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100664 if (IS_ERR_OR_NULL(dev->power.qos))
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100665 ret = -ENODEV;
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100666 else if (dev->power.qos->resume_latency_req)
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100667 ret = -EEXIST;
668
669 if (ret < 0) {
670 __dev_pm_qos_remove_request(req);
671 kfree(req);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200672 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100673 goto out;
674 }
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100675 dev->power.qos->resume_latency_req = req;
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200676
677 mutex_unlock(&dev_pm_qos_mtx);
678
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100679 ret = pm_qos_sysfs_add_resume_latency(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100680 if (ret)
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100681 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100682
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100683 out:
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200684 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100685 return ret;
686}
687EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
688
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100689static void __dev_pm_qos_hide_latency_limit(struct device *dev)
690{
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100691 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->resume_latency_req)
692 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100693}
694
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100695/**
696 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
697 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
698 */
699void dev_pm_qos_hide_latency_limit(struct device *dev)
700{
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200701 mutex_lock(&dev_pm_qos_sysfs_mtx);
702
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100703 pm_qos_sysfs_remove_resume_latency(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200704
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100705 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100706 __dev_pm_qos_hide_latency_limit(dev);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100707 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200708
709 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100710}
711EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200712
713/**
714 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
715 * @dev: Device whose PM QoS flags are to be exposed to user space.
716 * @val: Initial values of the flags.
717 */
718int dev_pm_qos_expose_flags(struct device *dev, s32 val)
719{
720 struct dev_pm_qos_request *req;
721 int ret;
722
723 if (!device_is_registered(dev))
724 return -EINVAL;
725
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200726 req = kzalloc(sizeof(*req), GFP_KERNEL);
727 if (!req)
728 return -ENOMEM;
729
730 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100731 if (ret < 0) {
732 kfree(req);
733 return ret;
734 }
735
736 pm_runtime_get_sync(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200737 mutex_lock(&dev_pm_qos_sysfs_mtx);
738
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100739 mutex_lock(&dev_pm_qos_mtx);
740
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100741 if (IS_ERR_OR_NULL(dev->power.qos))
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100742 ret = -ENODEV;
743 else if (dev->power.qos->flags_req)
744 ret = -EEXIST;
745
746 if (ret < 0) {
747 __dev_pm_qos_remove_request(req);
748 kfree(req);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200749 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100750 goto out;
751 }
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200752 dev->power.qos->flags_req = req;
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200753
754 mutex_unlock(&dev_pm_qos_mtx);
755
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200756 ret = pm_qos_sysfs_add_flags(dev);
757 if (ret)
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200758 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200759
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100760 out:
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200761 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800762 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200763 return ret;
764}
765EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
766
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100767static void __dev_pm_qos_hide_flags(struct device *dev)
768{
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200769 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req)
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100770 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100771}
772
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200773/**
774 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
775 * @dev: Device whose PM QoS flags are to be hidden from user space.
776 */
777void dev_pm_qos_hide_flags(struct device *dev)
778{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100779 pm_runtime_get_sync(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200780 mutex_lock(&dev_pm_qos_sysfs_mtx);
781
782 pm_qos_sysfs_remove_flags(dev);
783
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100784 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100785 __dev_pm_qos_hide_flags(dev);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100786 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200787
788 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100789 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200790}
791EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
792
793/**
794 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
795 * @dev: Device to update the PM QoS flags request for.
796 * @mask: Flags to set/clear.
797 * @set: Whether to set or clear the flags (true means set).
798 */
799int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
800{
801 s32 value;
802 int ret;
803
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200804 pm_runtime_get_sync(dev);
805 mutex_lock(&dev_pm_qos_mtx);
806
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100807 if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) {
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100808 ret = -EINVAL;
809 goto out;
810 }
811
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200812 value = dev_pm_qos_requested_flags(dev);
813 if (set)
814 value |= mask;
815 else
816 value &= ~mask;
817
818 ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
819
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100820 out:
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200821 mutex_unlock(&dev_pm_qos_mtx);
822 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200823 return ret;
824}
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100825
826/**
827 * dev_pm_qos_get_user_latency_tolerance - Get user space latency tolerance.
828 * @dev: Device to obtain the user space latency tolerance for.
829 */
830s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev)
831{
832 s32 ret;
833
834 mutex_lock(&dev_pm_qos_mtx);
835 ret = IS_ERR_OR_NULL(dev->power.qos)
836 || !dev->power.qos->latency_tolerance_req ?
837 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT :
838 dev->power.qos->latency_tolerance_req->data.pnode.prio;
839 mutex_unlock(&dev_pm_qos_mtx);
840 return ret;
841}
842
843/**
844 * dev_pm_qos_update_user_latency_tolerance - Update user space latency tolerance.
845 * @dev: Device to update the user space latency tolerance for.
846 * @val: New user space latency tolerance for @dev (negative values disable).
847 */
848int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val)
849{
850 int ret;
851
852 mutex_lock(&dev_pm_qos_mtx);
853
854 if (IS_ERR_OR_NULL(dev->power.qos)
855 || !dev->power.qos->latency_tolerance_req) {
856 struct dev_pm_qos_request *req;
857
858 if (val < 0) {
859 ret = -EINVAL;
860 goto out;
861 }
862 req = kzalloc(sizeof(*req), GFP_KERNEL);
863 if (!req) {
864 ret = -ENOMEM;
865 goto out;
866 }
867 ret = __dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY_TOLERANCE, val);
868 if (ret < 0) {
869 kfree(req);
870 goto out;
871 }
872 dev->power.qos->latency_tolerance_req = req;
873 } else {
874 if (val < 0) {
875 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY_TOLERANCE);
876 ret = 0;
877 } else {
878 ret = __dev_pm_qos_update_request(dev->power.qos->latency_tolerance_req, val);
879 }
880 }
881
882 out:
883 mutex_unlock(&dev_pm_qos_mtx);
884 return ret;
885}
Mika Westerberg13b2c4a2015-07-27 18:03:56 +0300886
887/**
888 * dev_pm_qos_expose_latency_tolerance - Expose latency tolerance to userspace
889 * @dev: Device whose latency tolerance to expose
890 */
891int dev_pm_qos_expose_latency_tolerance(struct device *dev)
892{
893 int ret;
894
895 if (!dev->power.set_latency_tolerance)
896 return -EINVAL;
897
898 mutex_lock(&dev_pm_qos_sysfs_mtx);
899 ret = pm_qos_sysfs_add_latency_tolerance(dev);
900 mutex_unlock(&dev_pm_qos_sysfs_mtx);
901
902 return ret;
903}
904EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_tolerance);
905
906/**
907 * dev_pm_qos_hide_latency_tolerance - Hide latency tolerance from userspace
908 * @dev: Device whose latency tolerance to hide
909 */
910void dev_pm_qos_hide_latency_tolerance(struct device *dev)
911{
912 mutex_lock(&dev_pm_qos_sysfs_mtx);
913 pm_qos_sysfs_remove_latency_tolerance(dev);
914 mutex_unlock(&dev_pm_qos_sysfs_mtx);
915
916 /* Remove the request from user space now */
917 pm_runtime_get_sync(dev);
918 dev_pm_qos_update_user_latency_tolerance(dev,
919 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT);
920 pm_runtime_put(dev);
921}
922EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_tolerance);