blob: 36b9eb4862cb96e2561321500982730e4ff1587f [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
Rafael J. Wysocki37530f22013-03-04 14:22:57 +010067 if (IS_ERR_OR_NULL(qos))
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020068 return PM_QOS_FLAGS_UNDEFINED;
69
70 pqf = &qos->flags;
71 if (list_empty(&pqf->list))
72 return PM_QOS_FLAGS_UNDEFINED;
73
74 val = pqf->effective_flags & mask;
75 if (val)
76 return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME;
77
78 return PM_QOS_FLAGS_NONE;
79}
80
81/**
82 * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
83 * @dev: Device to check the PM QoS flags for.
84 * @mask: Flags to check against.
85 */
86enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask)
87{
88 unsigned long irqflags;
89 enum pm_qos_flags_status ret;
90
91 spin_lock_irqsave(&dev->power.lock, irqflags);
92 ret = __dev_pm_qos_flags(dev, mask);
93 spin_unlock_irqrestore(&dev->power.lock, irqflags);
94
95 return ret;
96}
Lan Tianyu6802771b2013-01-23 04:26:28 +080097EXPORT_SYMBOL_GPL(dev_pm_qos_flags);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020098
99/**
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100100 * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
101 * @dev: Device to get the PM QoS constraint value for.
102 *
103 * This routine must be called with dev->power.lock held.
104 */
105s32 __dev_pm_qos_read_value(struct device *dev)
106{
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100107 return IS_ERR_OR_NULL(dev->power.qos) ?
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100108 0 : pm_qos_read_value(&dev->power.qos->resume_latency);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100109}
110
111/**
112 * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200113 * @dev: Device to get the PM QoS constraint value for.
114 */
115s32 dev_pm_qos_read_value(struct device *dev)
116{
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200117 unsigned long flags;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100118 s32 ret;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200119
120 spin_lock_irqsave(&dev->power.lock, flags);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100121 ret = __dev_pm_qos_read_value(dev);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200122 spin_unlock_irqrestore(&dev->power.lock, flags);
123
124 return ret;
125}
126
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200127/**
128 * apply_constraint - Add/modify/remove device PM QoS request.
129 * @req: Constraint request to apply
130 * @action: Action to perform (add/update/remove).
131 * @value: Value to assign to the QoS request.
Jean Pihetb66213c2011-08-25 15:35:47 +0200132 *
133 * Internal function to update the constraints list using the PM QoS core
134 * code and if needed call the per-device and the global notification
135 * callbacks
136 */
137static int apply_constraint(struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200138 enum pm_qos_req_action action, s32 value)
Jean Pihetb66213c2011-08-25 15:35:47 +0200139{
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200140 struct dev_pm_qos *qos = req->dev->power.qos;
141 int ret;
Jean Pihetb66213c2011-08-25 15:35:47 +0200142
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200143 switch(req->type) {
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100144 case DEV_PM_QOS_RESUME_LATENCY:
145 ret = pm_qos_update_target(&qos->resume_latency,
146 &req->data.pnode, action, value);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200147 if (ret) {
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100148 value = pm_qos_read_value(&qos->resume_latency);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200149 blocking_notifier_call_chain(&dev_pm_notifiers,
150 (unsigned long)value,
151 req);
152 }
153 break;
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100154 case DEV_PM_QOS_LATENCY_TOLERANCE:
155 ret = pm_qos_update_target(&qos->latency_tolerance,
156 &req->data.pnode, action, value);
157 if (ret) {
158 value = pm_qos_read_value(&qos->latency_tolerance);
159 req->dev->power.set_latency_tolerance(req->dev, value);
160 }
161 break;
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200162 case DEV_PM_QOS_FLAGS:
163 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
164 action, value);
165 break;
166 default:
167 ret = -EINVAL;
Jean Pihetb66213c2011-08-25 15:35:47 +0200168 }
169
170 return ret;
171}
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200172
173/*
174 * dev_pm_qos_constraints_allocate
175 * @dev: device to allocate data for
176 *
177 * Called at the first call to add_request, for constraint data allocation
178 * Must be called with the dev_pm_qos_mtx mutex held
179 */
180static int dev_pm_qos_constraints_allocate(struct device *dev)
181{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200182 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200183 struct pm_qos_constraints *c;
184 struct blocking_notifier_head *n;
185
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200186 qos = kzalloc(sizeof(*qos), GFP_KERNEL);
187 if (!qos)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200188 return -ENOMEM;
189
190 n = kzalloc(sizeof(*n), GFP_KERNEL);
191 if (!n) {
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200192 kfree(qos);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200193 return -ENOMEM;
194 }
195 BLOCKING_INIT_NOTIFIER_HEAD(n);
196
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100197 c = &qos->resume_latency;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200198 plist_head_init(&c->list);
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100199 c->target_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
200 c->default_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
Rafael J. Wysocki327adae2014-02-11 00:35:29 +0100201 c->no_constraint_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200202 c->type = PM_QOS_MIN;
203 c->notifiers = n;
204
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100205 c = &qos->latency_tolerance;
206 plist_head_init(&c->list);
207 c->target_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
208 c->default_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
209 c->no_constraint_value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
210 c->type = PM_QOS_MIN;
211
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200212 INIT_LIST_HEAD(&qos->flags.list);
213
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200214 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200215 dev->power.qos = qos;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200216 spin_unlock_irq(&dev->power.lock);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200217
218 return 0;
219}
220
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100221static void __dev_pm_qos_hide_latency_limit(struct device *dev);
222static void __dev_pm_qos_hide_flags(struct device *dev);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200223
224/**
225 * dev_pm_qos_constraints_destroy
226 * @dev: target device
227 *
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200228 * Called from the device PM subsystem on device removal under device_pm_lock().
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200229 */
230void dev_pm_qos_constraints_destroy(struct device *dev)
231{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200232 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200233 struct dev_pm_qos_request *req, *tmp;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200234 struct pm_qos_constraints *c;
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100235 struct pm_qos_flags *f;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200236
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200237 mutex_lock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100238
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100239 /*
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100240 * If the device's PM QoS resume latency limit or PM QoS flags have been
241 * exposed to user space, they have to be hidden at this point.
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100242 */
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100243 pm_qos_sysfs_remove_resume_latency(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200244 pm_qos_sysfs_remove_flags(dev);
245
246 mutex_lock(&dev_pm_qos_mtx);
247
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100248 __dev_pm_qos_hide_latency_limit(dev);
249 __dev_pm_qos_hide_flags(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100250
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200251 qos = dev->power.qos;
252 if (!qos)
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200253 goto out;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200254
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100255 /* Flush the constraints lists for the device. */
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100256 c = &qos->resume_latency;
Rafael J. Wysocki021c8702012-10-23 01:09:00 +0200257 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200258 /*
259 * Update constraints list and call the notification
260 * callbacks if needed
261 */
262 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
263 memset(req, 0, sizeof(*req));
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200264 }
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100265 c = &qos->latency_tolerance;
266 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
267 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
268 memset(req, 0, sizeof(*req));
269 }
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100270 f = &qos->flags;
271 list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
272 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
273 memset(req, 0, sizeof(*req));
274 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200275
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200276 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100277 dev->power.qos = ERR_PTR(-ENODEV);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200278 spin_unlock_irq(&dev->power.lock);
279
280 kfree(c->notifiers);
Lan,Tianyu9eaee2c2012-11-01 22:45:30 +0100281 kfree(qos);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200282
283 out:
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200284 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200285
286 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200287}
288
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100289static bool dev_pm_qos_invalid_request(struct device *dev,
290 struct dev_pm_qos_request *req)
291{
292 return !req || (req->type == DEV_PM_QOS_LATENCY_TOLERANCE
293 && !dev->power.set_latency_tolerance);
294}
295
296static int __dev_pm_qos_add_request(struct device *dev,
297 struct dev_pm_qos_request *req,
298 enum dev_pm_qos_req_type type, s32 value)
299{
300 int ret = 0;
301
302 if (!dev || dev_pm_qos_invalid_request(dev, req))
303 return -EINVAL;
304
305 if (WARN(dev_pm_qos_request_active(req),
306 "%s() called for already added request\n", __func__))
307 return -EINVAL;
308
309 if (IS_ERR(dev->power.qos))
310 ret = -ENODEV;
311 else if (!dev->power.qos)
312 ret = dev_pm_qos_constraints_allocate(dev);
313
314 trace_dev_pm_qos_add_request(dev_name(dev), type, value);
315 if (!ret) {
316 req->dev = dev;
317 req->type = type;
318 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
319 }
320 return ret;
321}
322
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200323/**
324 * dev_pm_qos_add_request - inserts new qos request into the list
325 * @dev: target device for the constraint
326 * @req: pointer to a preallocated handle
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200327 * @type: type of the request
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200328 * @value: defines the qos request
329 *
330 * This function inserts a new entry in the device constraints list of
331 * requested qos performance characteristics. It recomputes the aggregate
332 * QoS expectations of parameters and initializes the dev_pm_qos_request
333 * handle. Caller needs to save this handle for later use in updates and
334 * removal.
335 *
336 * Returns 1 if the aggregated constraint value has changed,
337 * 0 if the aggregated constraint value has not changed,
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200338 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
339 * to allocate for data structures, -ENODEV if the device has just been removed
340 * from the system.
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100341 *
342 * Callers should ensure that the target device is not RPM_SUSPENDED before
343 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200344 */
345int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200346 enum dev_pm_qos_req_type type, s32 value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200347{
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100348 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200349
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200350 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100351 ret = __dev_pm_qos_add_request(dev, req, type, value);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200352 mutex_unlock(&dev_pm_qos_mtx);
353 return ret;
354}
355EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
356
357/**
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200358 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
359 * @req : PM QoS request to modify.
360 * @new_value: New value to request.
361 */
362static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
363 s32 new_value)
364{
365 s32 curr_value;
366 int ret = 0;
367
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100368 if (!req) /*guard against callers passing in null */
369 return -EINVAL;
370
371 if (WARN(!dev_pm_qos_request_active(req),
372 "%s() called for unknown object\n", __func__))
373 return -EINVAL;
374
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100375 if (IS_ERR_OR_NULL(req->dev->power.qos))
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200376 return -ENODEV;
377
378 switch(req->type) {
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100379 case DEV_PM_QOS_RESUME_LATENCY:
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100380 case DEV_PM_QOS_LATENCY_TOLERANCE:
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200381 curr_value = req->data.pnode.prio;
382 break;
383 case DEV_PM_QOS_FLAGS:
384 curr_value = req->data.flr.flags;
385 break;
386 default:
387 return -EINVAL;
388 }
389
Sahara96d9d0b2013-06-21 11:12:30 +0900390 trace_dev_pm_qos_update_request(dev_name(req->dev), req->type,
391 new_value);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200392 if (curr_value != new_value)
393 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
394
395 return ret;
396}
397
398/**
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200399 * dev_pm_qos_update_request - modifies an existing qos request
400 * @req : handle to list element holding a dev_pm_qos request to use
401 * @new_value: defines the qos request
402 *
403 * Updates an existing dev PM qos request along with updating the
404 * target value.
405 *
406 * Attempts are made to make this code callable on hot code paths.
407 *
408 * Returns 1 if the aggregated constraint value has changed,
409 * 0 if the aggregated constraint value has not changed,
410 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
411 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100412 *
413 * Callers should ensure that the target device is not RPM_SUSPENDED before
414 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200415 */
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200416int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200417{
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200418 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200419
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100420 mutex_lock(&dev_pm_qos_mtx);
421 ret = __dev_pm_qos_update_request(req, new_value);
422 mutex_unlock(&dev_pm_qos_mtx);
423 return ret;
424}
425EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
426
427static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
428{
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100429 int ret;
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100430
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200431 if (!req) /*guard against callers passing in null */
432 return -EINVAL;
433
Guennadi Liakhovetskiaf4c7202011-11-10 00:44:18 +0100434 if (WARN(!dev_pm_qos_request_active(req),
435 "%s() called for unknown object\n", __func__))
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200436 return -EINVAL;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200437
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100438 if (IS_ERR_OR_NULL(req->dev->power.qos))
439 return -ENODEV;
440
Sahara96d9d0b2013-06-21 11:12:30 +0900441 trace_dev_pm_qos_remove_request(dev_name(req->dev), req->type,
442 PM_QOS_DEFAULT_VALUE);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100443 ret = apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
444 memset(req, 0, sizeof(*req));
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200445 return ret;
446}
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200447
448/**
449 * dev_pm_qos_remove_request - modifies an existing qos request
450 * @req: handle to request list element
451 *
452 * Will remove pm qos request from the list of constraints and
453 * recompute the current target value. Call this on slow code paths.
454 *
455 * Returns 1 if the aggregated constraint value has changed,
456 * 0 if the aggregated constraint value has not changed,
457 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
458 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100459 *
460 * Callers should ensure that the target device is not RPM_SUSPENDED before
461 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200462 */
463int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
464{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100465 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200466
467 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100468 ret = __dev_pm_qos_remove_request(req);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200469 mutex_unlock(&dev_pm_qos_mtx);
470 return ret;
471}
472EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
473
474/**
475 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
476 * of per-device PM QoS constraints
477 *
478 * @dev: target device for the constraint
479 * @notifier: notifier block managed by caller.
480 *
481 * Will register the notifier into a notification chain that gets called
482 * upon changes to the target value for the device.
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200483 *
484 * If the device's constraints object doesn't exist when this routine is called,
485 * it will be created (or error code will be returned if that fails).
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200486 */
487int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
488{
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200489 int ret = 0;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200490
491 mutex_lock(&dev_pm_qos_mtx);
492
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100493 if (IS_ERR(dev->power.qos))
494 ret = -ENODEV;
495 else if (!dev->power.qos)
496 ret = dev_pm_qos_constraints_allocate(dev);
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200497
498 if (!ret)
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100499 ret = blocking_notifier_chain_register(dev->power.qos->resume_latency.notifiers,
500 notifier);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200501
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200502 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200503 return ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200504}
505EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
506
507/**
508 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
509 * of per-device PM QoS constraints
510 *
511 * @dev: target device for the constraint
512 * @notifier: notifier block to be removed.
513 *
514 * Will remove the notifier from the notification chain that gets called
515 * upon changes to the target value.
516 */
517int dev_pm_qos_remove_notifier(struct device *dev,
518 struct notifier_block *notifier)
519{
520 int retval = 0;
521
522 mutex_lock(&dev_pm_qos_mtx);
523
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200524 /* Silently return if the constraints object is not present. */
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100525 if (!IS_ERR_OR_NULL(dev->power.qos))
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100526 retval = blocking_notifier_chain_unregister(dev->power.qos->resume_latency.notifiers,
527 notifier);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200528
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200529 mutex_unlock(&dev_pm_qos_mtx);
530 return retval;
531}
532EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
Jean Pihetb66213c2011-08-25 15:35:47 +0200533
534/**
535 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
536 * target value of the PM QoS constraints for any device
537 *
538 * @notifier: notifier block managed by caller.
539 *
540 * Will register the notifier into a notification chain that gets called
541 * upon changes to the target value for any device.
542 */
543int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
544{
545 return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
546}
547EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
548
549/**
550 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
551 * target value of PM QoS constraints for any device
552 *
553 * @notifier: notifier block to be removed.
554 *
555 * Will remove the notifier from the notification chain that gets called
556 * upon changes to the target value for any device.
557 */
558int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
559{
560 return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
561}
562EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100563
564/**
565 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
566 * @dev: Device whose ancestor to add the request for.
567 * @req: Pointer to the preallocated handle.
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100568 * @type: Type of the request.
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100569 * @value: Constraint latency value.
570 */
571int dev_pm_qos_add_ancestor_request(struct device *dev,
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100572 struct dev_pm_qos_request *req,
573 enum dev_pm_qos_req_type type, s32 value)
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100574{
575 struct device *ancestor = dev->parent;
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100576 int ret = -ENODEV;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100577
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100578 switch (type) {
579 case DEV_PM_QOS_RESUME_LATENCY:
580 while (ancestor && !ancestor->power.ignore_children)
581 ancestor = ancestor->parent;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100582
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100583 break;
584 case DEV_PM_QOS_LATENCY_TOLERANCE:
585 while (ancestor && !ancestor->power.set_latency_tolerance)
586 ancestor = ancestor->parent;
587
588 break;
589 default:
590 ancestor = NULL;
591 }
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100592 if (ancestor)
Rafael J. Wysocki71d821f2014-02-11 00:36:00 +0100593 ret = dev_pm_qos_add_request(ancestor, req, type, value);
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100594
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100595 if (ret < 0)
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100596 req->dev = NULL;
597
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100598 return ret;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100599}
600EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100601
602#ifdef CONFIG_PM_RUNTIME
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200603static void __dev_pm_qos_drop_user_request(struct device *dev,
604 enum dev_pm_qos_req_type type)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100605{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100606 struct dev_pm_qos_request *req = NULL;
607
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200608 switch(type) {
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100609 case DEV_PM_QOS_RESUME_LATENCY:
610 req = dev->power.qos->resume_latency_req;
611 dev->power.qos->resume_latency_req = NULL;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200612 break;
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100613 case DEV_PM_QOS_LATENCY_TOLERANCE:
614 req = dev->power.qos->latency_tolerance_req;
615 dev->power.qos->latency_tolerance_req = NULL;
616 break;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200617 case DEV_PM_QOS_FLAGS:
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100618 req = dev->power.qos->flags_req;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200619 dev->power.qos->flags_req = NULL;
620 break;
621 }
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100622 __dev_pm_qos_remove_request(req);
623 kfree(req);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100624}
625
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200626static void dev_pm_qos_drop_user_request(struct device *dev,
627 enum dev_pm_qos_req_type type)
628{
629 mutex_lock(&dev_pm_qos_mtx);
630 __dev_pm_qos_drop_user_request(dev, type);
631 mutex_unlock(&dev_pm_qos_mtx);
632}
633
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100634/**
635 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
636 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
637 * @value: Initial value of the latency limit.
638 */
639int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
640{
641 struct dev_pm_qos_request *req;
642 int ret;
643
644 if (!device_is_registered(dev) || value < 0)
645 return -EINVAL;
646
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100647 req = kzalloc(sizeof(*req), GFP_KERNEL);
648 if (!req)
649 return -ENOMEM;
650
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100651 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_RESUME_LATENCY, value);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100652 if (ret < 0) {
653 kfree(req);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100654 return ret;
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100655 }
656
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200657 mutex_lock(&dev_pm_qos_sysfs_mtx);
658
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100659 mutex_lock(&dev_pm_qos_mtx);
660
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100661 if (IS_ERR_OR_NULL(dev->power.qos))
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100662 ret = -ENODEV;
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100663 else if (dev->power.qos->resume_latency_req)
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100664 ret = -EEXIST;
665
666 if (ret < 0) {
667 __dev_pm_qos_remove_request(req);
668 kfree(req);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200669 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100670 goto out;
671 }
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100672 dev->power.qos->resume_latency_req = req;
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200673
674 mutex_unlock(&dev_pm_qos_mtx);
675
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100676 ret = pm_qos_sysfs_add_resume_latency(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100677 if (ret)
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100678 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100679
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100680 out:
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200681 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100682 return ret;
683}
684EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
685
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100686static void __dev_pm_qos_hide_latency_limit(struct device *dev)
687{
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100688 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->resume_latency_req)
689 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100690}
691
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100692/**
693 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
694 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
695 */
696void dev_pm_qos_hide_latency_limit(struct device *dev)
697{
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200698 mutex_lock(&dev_pm_qos_sysfs_mtx);
699
Rafael J. Wysockib02f6692014-02-11 00:35:23 +0100700 pm_qos_sysfs_remove_resume_latency(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200701
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100702 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100703 __dev_pm_qos_hide_latency_limit(dev);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100704 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200705
706 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100707}
708EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200709
710/**
711 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
712 * @dev: Device whose PM QoS flags are to be exposed to user space.
713 * @val: Initial values of the flags.
714 */
715int dev_pm_qos_expose_flags(struct device *dev, s32 val)
716{
717 struct dev_pm_qos_request *req;
718 int ret;
719
720 if (!device_is_registered(dev))
721 return -EINVAL;
722
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200723 req = kzalloc(sizeof(*req), GFP_KERNEL);
724 if (!req)
725 return -ENOMEM;
726
727 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100728 if (ret < 0) {
729 kfree(req);
730 return ret;
731 }
732
733 pm_runtime_get_sync(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200734 mutex_lock(&dev_pm_qos_sysfs_mtx);
735
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100736 mutex_lock(&dev_pm_qos_mtx);
737
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100738 if (IS_ERR_OR_NULL(dev->power.qos))
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100739 ret = -ENODEV;
740 else if (dev->power.qos->flags_req)
741 ret = -EEXIST;
742
743 if (ret < 0) {
744 __dev_pm_qos_remove_request(req);
745 kfree(req);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200746 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100747 goto out;
748 }
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200749 dev->power.qos->flags_req = req;
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200750
751 mutex_unlock(&dev_pm_qos_mtx);
752
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200753 ret = pm_qos_sysfs_add_flags(dev);
754 if (ret)
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200755 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200756
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100757 out:
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200758 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800759 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200760 return ret;
761}
762EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
763
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100764static void __dev_pm_qos_hide_flags(struct device *dev)
765{
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200766 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req)
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100767 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100768}
769
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200770/**
771 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
772 * @dev: Device whose PM QoS flags are to be hidden from user space.
773 */
774void dev_pm_qos_hide_flags(struct device *dev)
775{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100776 pm_runtime_get_sync(dev);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200777 mutex_lock(&dev_pm_qos_sysfs_mtx);
778
779 pm_qos_sysfs_remove_flags(dev);
780
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100781 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100782 __dev_pm_qos_hide_flags(dev);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100783 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki0f703062013-04-02 01:25:24 +0200784
785 mutex_unlock(&dev_pm_qos_sysfs_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100786 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200787}
788EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
789
790/**
791 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
792 * @dev: Device to update the PM QoS flags request for.
793 * @mask: Flags to set/clear.
794 * @set: Whether to set or clear the flags (true means set).
795 */
796int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
797{
798 s32 value;
799 int ret;
800
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200801 pm_runtime_get_sync(dev);
802 mutex_lock(&dev_pm_qos_mtx);
803
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100804 if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) {
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100805 ret = -EINVAL;
806 goto out;
807 }
808
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200809 value = dev_pm_qos_requested_flags(dev);
810 if (set)
811 value |= mask;
812 else
813 value &= ~mask;
814
815 ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
816
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100817 out:
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200818 mutex_unlock(&dev_pm_qos_mtx);
819 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200820 return ret;
821}
Rafael J. Wysocki2d984ad2014-02-11 00:35:38 +0100822
823/**
824 * dev_pm_qos_get_user_latency_tolerance - Get user space latency tolerance.
825 * @dev: Device to obtain the user space latency tolerance for.
826 */
827s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev)
828{
829 s32 ret;
830
831 mutex_lock(&dev_pm_qos_mtx);
832 ret = IS_ERR_OR_NULL(dev->power.qos)
833 || !dev->power.qos->latency_tolerance_req ?
834 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT :
835 dev->power.qos->latency_tolerance_req->data.pnode.prio;
836 mutex_unlock(&dev_pm_qos_mtx);
837 return ret;
838}
839
840/**
841 * dev_pm_qos_update_user_latency_tolerance - Update user space latency tolerance.
842 * @dev: Device to update the user space latency tolerance for.
843 * @val: New user space latency tolerance for @dev (negative values disable).
844 */
845int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val)
846{
847 int ret;
848
849 mutex_lock(&dev_pm_qos_mtx);
850
851 if (IS_ERR_OR_NULL(dev->power.qos)
852 || !dev->power.qos->latency_tolerance_req) {
853 struct dev_pm_qos_request *req;
854
855 if (val < 0) {
856 ret = -EINVAL;
857 goto out;
858 }
859 req = kzalloc(sizeof(*req), GFP_KERNEL);
860 if (!req) {
861 ret = -ENOMEM;
862 goto out;
863 }
864 ret = __dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY_TOLERANCE, val);
865 if (ret < 0) {
866 kfree(req);
867 goto out;
868 }
869 dev->power.qos->latency_tolerance_req = req;
870 } else {
871 if (val < 0) {
872 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY_TOLERANCE);
873 ret = 0;
874 } else {
875 ret = __dev_pm_qos_update_request(dev->power.qos->latency_tolerance_req, val);
876 }
877 }
878
879 out:
880 mutex_unlock(&dev_pm_qos_mtx);
881 return ret;
882}
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100883#else /* !CONFIG_PM_RUNTIME */
884static void __dev_pm_qos_hide_latency_limit(struct device *dev) {}
885static void __dev_pm_qos_hide_flags(struct device *dev) {}
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100886#endif /* CONFIG_PM_RUNTIME */