blob: 5f74587ef258f4598e16c37bf9284d108c475ead [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>
Jean Pihet91ff4cb2011-08-25 15:35:41 +020045
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +010046#include "power.h"
Jean Pihet91ff4cb2011-08-25 15:35:41 +020047
48static DEFINE_MUTEX(dev_pm_qos_mtx);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +020049
Jean Pihetb66213c2011-08-25 15:35:47 +020050static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
51
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +020052/**
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020053 * __dev_pm_qos_flags - Check PM QoS flags for a given device.
54 * @dev: Device to check the PM QoS flags for.
55 * @mask: Flags to check against.
56 *
57 * This routine must be called with dev->power.lock held.
58 */
59enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask)
60{
61 struct dev_pm_qos *qos = dev->power.qos;
62 struct pm_qos_flags *pqf;
63 s32 val;
64
Rafael J. Wysocki37530f22013-03-04 14:22:57 +010065 if (IS_ERR_OR_NULL(qos))
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020066 return PM_QOS_FLAGS_UNDEFINED;
67
68 pqf = &qos->flags;
69 if (list_empty(&pqf->list))
70 return PM_QOS_FLAGS_UNDEFINED;
71
72 val = pqf->effective_flags & mask;
73 if (val)
74 return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME;
75
76 return PM_QOS_FLAGS_NONE;
77}
78
79/**
80 * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
81 * @dev: Device to check the PM QoS flags for.
82 * @mask: Flags to check against.
83 */
84enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask)
85{
86 unsigned long irqflags;
87 enum pm_qos_flags_status ret;
88
89 spin_lock_irqsave(&dev->power.lock, irqflags);
90 ret = __dev_pm_qos_flags(dev, mask);
91 spin_unlock_irqrestore(&dev->power.lock, irqflags);
92
93 return ret;
94}
Lan Tianyu68027712013-01-23 04:26:28 +080095EXPORT_SYMBOL_GPL(dev_pm_qos_flags);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020096
97/**
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +010098 * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
99 * @dev: Device to get the PM QoS constraint value for.
100 *
101 * This routine must be called with dev->power.lock held.
102 */
103s32 __dev_pm_qos_read_value(struct device *dev)
104{
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100105 return IS_ERR_OR_NULL(dev->power.qos) ?
106 0 : pm_qos_read_value(&dev->power.qos->latency);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100107}
108
109/**
110 * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200111 * @dev: Device to get the PM QoS constraint value for.
112 */
113s32 dev_pm_qos_read_value(struct device *dev)
114{
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200115 unsigned long flags;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100116 s32 ret;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200117
118 spin_lock_irqsave(&dev->power.lock, flags);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100119 ret = __dev_pm_qos_read_value(dev);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200120 spin_unlock_irqrestore(&dev->power.lock, flags);
121
122 return ret;
123}
124
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200125/**
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.
Jean Pihetb66213c2011-08-25 15:35:47 +0200130 *
131 * Internal function to update the constraints list using the PM QoS core
132 * code and if needed call the per-device and the global notification
133 * callbacks
134 */
135static int apply_constraint(struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200136 enum pm_qos_req_action action, s32 value)
Jean Pihetb66213c2011-08-25 15:35:47 +0200137{
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200138 struct dev_pm_qos *qos = req->dev->power.qos;
139 int ret;
Jean Pihetb66213c2011-08-25 15:35:47 +0200140
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200141 switch(req->type) {
142 case DEV_PM_QOS_LATENCY:
143 ret = pm_qos_update_target(&qos->latency, &req->data.pnode,
144 action, value);
145 if (ret) {
146 value = pm_qos_read_value(&qos->latency);
147 blocking_notifier_call_chain(&dev_pm_notifiers,
148 (unsigned long)value,
149 req);
150 }
151 break;
152 case DEV_PM_QOS_FLAGS:
153 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
154 action, value);
155 break;
156 default:
157 ret = -EINVAL;
Jean Pihetb66213c2011-08-25 15:35:47 +0200158 }
159
160 return ret;
161}
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200162
163/*
164 * dev_pm_qos_constraints_allocate
165 * @dev: device to allocate data for
166 *
167 * Called at the first call to add_request, for constraint data allocation
168 * Must be called with the dev_pm_qos_mtx mutex held
169 */
170static int dev_pm_qos_constraints_allocate(struct device *dev)
171{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200172 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200173 struct pm_qos_constraints *c;
174 struct blocking_notifier_head *n;
175
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200176 qos = kzalloc(sizeof(*qos), GFP_KERNEL);
177 if (!qos)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200178 return -ENOMEM;
179
180 n = kzalloc(sizeof(*n), GFP_KERNEL);
181 if (!n) {
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200182 kfree(qos);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200183 return -ENOMEM;
184 }
185 BLOCKING_INIT_NOTIFIER_HEAD(n);
186
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200187 c = &qos->latency;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200188 plist_head_init(&c->list);
189 c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
190 c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
191 c->type = PM_QOS_MIN;
192 c->notifiers = n;
193
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200194 INIT_LIST_HEAD(&qos->flags.list);
195
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200196 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200197 dev->power.qos = qos;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200198 spin_unlock_irq(&dev->power.lock);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200199
200 return 0;
201}
202
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100203static void __dev_pm_qos_hide_latency_limit(struct device *dev);
204static void __dev_pm_qos_hide_flags(struct device *dev);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200205
206/**
207 * dev_pm_qos_constraints_destroy
208 * @dev: target device
209 *
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200210 * Called from the device PM subsystem on device removal under device_pm_lock().
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200211 */
212void dev_pm_qos_constraints_destroy(struct device *dev)
213{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200214 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200215 struct dev_pm_qos_request *req, *tmp;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200216 struct pm_qos_constraints *c;
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100217 struct pm_qos_flags *f;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200218
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100219 mutex_lock(&dev_pm_qos_mtx);
220
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100221 /*
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100222 * If the device's PM QoS resume latency limit or PM QoS flags have been
223 * exposed to user space, they have to be hidden at this point.
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100224 */
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100225 __dev_pm_qos_hide_latency_limit(dev);
226 __dev_pm_qos_hide_flags(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100227
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200228 qos = dev->power.qos;
229 if (!qos)
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200230 goto out;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200231
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100232 /* Flush the constraints lists for the device. */
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200233 c = &qos->latency;
Rafael J. Wysocki021c8702012-10-23 01:09:00 +0200234 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200235 /*
236 * Update constraints list and call the notification
237 * callbacks if needed
238 */
239 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
240 memset(req, 0, sizeof(*req));
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200241 }
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100242 f = &qos->flags;
243 list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
244 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
245 memset(req, 0, sizeof(*req));
246 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200247
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200248 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100249 dev->power.qos = ERR_PTR(-ENODEV);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200250 spin_unlock_irq(&dev->power.lock);
251
252 kfree(c->notifiers);
Lan,Tianyu9eaee2c2012-11-01 22:45:30 +0100253 kfree(qos);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200254
255 out:
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200256 mutex_unlock(&dev_pm_qos_mtx);
257}
258
259/**
260 * dev_pm_qos_add_request - inserts new qos request into the list
261 * @dev: target device for the constraint
262 * @req: pointer to a preallocated handle
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200263 * @type: type of the request
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200264 * @value: defines the qos request
265 *
266 * This function inserts a new entry in the device constraints list of
267 * requested qos performance characteristics. It recomputes the aggregate
268 * QoS expectations of parameters and initializes the dev_pm_qos_request
269 * handle. Caller needs to save this handle for later use in updates and
270 * removal.
271 *
272 * Returns 1 if the aggregated constraint value has changed,
273 * 0 if the aggregated constraint value has not changed,
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200274 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
275 * to allocate for data structures, -ENODEV if the device has just been removed
276 * from the system.
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100277 *
278 * Callers should ensure that the target device is not RPM_SUSPENDED before
279 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200280 */
281int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200282 enum dev_pm_qos_req_type type, s32 value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200283{
284 int ret = 0;
285
286 if (!dev || !req) /*guard against callers passing in null */
287 return -EINVAL;
288
Guennadi Liakhovetskiaf4c7202011-11-10 00:44:18 +0100289 if (WARN(dev_pm_qos_request_active(req),
290 "%s() called for already added request\n", __func__))
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200291 return -EINVAL;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200292
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200293 mutex_lock(&dev_pm_qos_mtx);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200294
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100295 if (IS_ERR(dev->power.qos))
296 ret = -ENODEV;
297 else if (!dev->power.qos)
298 ret = dev_pm_qos_constraints_allocate(dev);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200299
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200300 if (!ret) {
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100301 req->dev = dev;
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200302 req->type = type;
Jean Pihetb66213c2011-08-25 15:35:47 +0200303 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200304 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200305
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200306 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200307
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200308 return ret;
309}
310EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
311
312/**
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200313 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
314 * @req : PM QoS request to modify.
315 * @new_value: New value to request.
316 */
317static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
318 s32 new_value)
319{
320 s32 curr_value;
321 int ret = 0;
322
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100323 if (!req) /*guard against callers passing in null */
324 return -EINVAL;
325
326 if (WARN(!dev_pm_qos_request_active(req),
327 "%s() called for unknown object\n", __func__))
328 return -EINVAL;
329
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100330 if (IS_ERR_OR_NULL(req->dev->power.qos))
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200331 return -ENODEV;
332
333 switch(req->type) {
334 case DEV_PM_QOS_LATENCY:
335 curr_value = req->data.pnode.prio;
336 break;
337 case DEV_PM_QOS_FLAGS:
338 curr_value = req->data.flr.flags;
339 break;
340 default:
341 return -EINVAL;
342 }
343
344 if (curr_value != new_value)
345 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
346
347 return ret;
348}
349
350/**
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200351 * dev_pm_qos_update_request - modifies an existing qos request
352 * @req : handle to list element holding a dev_pm_qos request to use
353 * @new_value: defines the qos request
354 *
355 * Updates an existing dev PM qos request along with updating the
356 * target value.
357 *
358 * Attempts are made to make this code callable on hot code paths.
359 *
360 * Returns 1 if the aggregated constraint value has changed,
361 * 0 if the aggregated constraint value has not changed,
362 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
363 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100364 *
365 * Callers should ensure that the target device is not RPM_SUSPENDED before
366 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200367 */
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200368int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200369{
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200370 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200371
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100372 mutex_lock(&dev_pm_qos_mtx);
373 ret = __dev_pm_qos_update_request(req, new_value);
374 mutex_unlock(&dev_pm_qos_mtx);
375 return ret;
376}
377EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
378
379static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
380{
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100381 int ret;
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100382
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200383 if (!req) /*guard against callers passing in null */
384 return -EINVAL;
385
Guennadi Liakhovetskiaf4c7202011-11-10 00:44:18 +0100386 if (WARN(!dev_pm_qos_request_active(req),
387 "%s() called for unknown object\n", __func__))
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200388 return -EINVAL;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200389
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100390 if (IS_ERR_OR_NULL(req->dev->power.qos))
391 return -ENODEV;
392
393 ret = apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
394 memset(req, 0, sizeof(*req));
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200395 return ret;
396}
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200397
398/**
399 * dev_pm_qos_remove_request - modifies an existing qos request
400 * @req: handle to request list element
401 *
402 * Will remove pm qos request from the list of constraints and
403 * recompute the current target value. Call this on slow code paths.
404 *
405 * Returns 1 if the aggregated constraint value has changed,
406 * 0 if the aggregated constraint value has not changed,
407 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
408 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100409 *
410 * Callers should ensure that the target device is not RPM_SUSPENDED before
411 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200412 */
413int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
414{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100415 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200416
417 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100418 ret = __dev_pm_qos_remove_request(req);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200419 mutex_unlock(&dev_pm_qos_mtx);
420 return ret;
421}
422EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
423
424/**
425 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
426 * of per-device PM QoS constraints
427 *
428 * @dev: target device for the constraint
429 * @notifier: notifier block managed by caller.
430 *
431 * Will register the notifier into a notification chain that gets called
432 * upon changes to the target value for the device.
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200433 *
434 * If the device's constraints object doesn't exist when this routine is called,
435 * it will be created (or error code will be returned if that fails).
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200436 */
437int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
438{
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200439 int ret = 0;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200440
441 mutex_lock(&dev_pm_qos_mtx);
442
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100443 if (IS_ERR(dev->power.qos))
444 ret = -ENODEV;
445 else if (!dev->power.qos)
446 ret = dev_pm_qos_constraints_allocate(dev);
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200447
448 if (!ret)
449 ret = blocking_notifier_chain_register(
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200450 dev->power.qos->latency.notifiers, notifier);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200451
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200452 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200453 return ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200454}
455EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
456
457/**
458 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
459 * of per-device PM QoS constraints
460 *
461 * @dev: target device for the constraint
462 * @notifier: notifier block to be removed.
463 *
464 * Will remove the notifier from the notification chain that gets called
465 * upon changes to the target value.
466 */
467int dev_pm_qos_remove_notifier(struct device *dev,
468 struct notifier_block *notifier)
469{
470 int retval = 0;
471
472 mutex_lock(&dev_pm_qos_mtx);
473
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200474 /* Silently return if the constraints object is not present. */
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100475 if (!IS_ERR_OR_NULL(dev->power.qos))
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200476 retval = blocking_notifier_chain_unregister(
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200477 dev->power.qos->latency.notifiers,
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200478 notifier);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200479
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200480 mutex_unlock(&dev_pm_qos_mtx);
481 return retval;
482}
483EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
Jean Pihetb66213c2011-08-25 15:35:47 +0200484
485/**
486 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
487 * target value of the PM QoS constraints for any device
488 *
489 * @notifier: notifier block managed by caller.
490 *
491 * Will register the notifier into a notification chain that gets called
492 * upon changes to the target value for any device.
493 */
494int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
495{
496 return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
497}
498EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
499
500/**
501 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
502 * target value of PM QoS constraints for any device
503 *
504 * @notifier: notifier block to be removed.
505 *
506 * Will remove the notifier from the notification chain that gets called
507 * upon changes to the target value for any device.
508 */
509int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
510{
511 return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
512}
513EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100514
515/**
516 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
517 * @dev: Device whose ancestor to add the request for.
518 * @req: Pointer to the preallocated handle.
519 * @value: Constraint latency value.
520 */
521int dev_pm_qos_add_ancestor_request(struct device *dev,
522 struct dev_pm_qos_request *req, s32 value)
523{
524 struct device *ancestor = dev->parent;
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100525 int ret = -ENODEV;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100526
527 while (ancestor && !ancestor->power.ignore_children)
528 ancestor = ancestor->parent;
529
530 if (ancestor)
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100531 ret = dev_pm_qos_add_request(ancestor, req,
532 DEV_PM_QOS_LATENCY, value);
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100533
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100534 if (ret < 0)
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100535 req->dev = NULL;
536
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100537 return ret;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100538}
539EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100540
541#ifdef CONFIG_PM_RUNTIME
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200542static void __dev_pm_qos_drop_user_request(struct device *dev,
543 enum dev_pm_qos_req_type type)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100544{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100545 struct dev_pm_qos_request *req = NULL;
546
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200547 switch(type) {
548 case DEV_PM_QOS_LATENCY:
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100549 req = dev->power.qos->latency_req;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200550 dev->power.qos->latency_req = NULL;
551 break;
552 case DEV_PM_QOS_FLAGS:
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100553 req = dev->power.qos->flags_req;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200554 dev->power.qos->flags_req = NULL;
555 break;
556 }
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100557 __dev_pm_qos_remove_request(req);
558 kfree(req);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100559}
560
561/**
562 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
563 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
564 * @value: Initial value of the latency limit.
565 */
566int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
567{
568 struct dev_pm_qos_request *req;
569 int ret;
570
571 if (!device_is_registered(dev) || value < 0)
572 return -EINVAL;
573
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100574 req = kzalloc(sizeof(*req), GFP_KERNEL);
575 if (!req)
576 return -ENOMEM;
577
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200578 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY, value);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100579 if (ret < 0) {
580 kfree(req);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100581 return ret;
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100582 }
583
584 mutex_lock(&dev_pm_qos_mtx);
585
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100586 if (IS_ERR_OR_NULL(dev->power.qos))
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100587 ret = -ENODEV;
588 else if (dev->power.qos->latency_req)
589 ret = -EEXIST;
590
591 if (ret < 0) {
592 __dev_pm_qos_remove_request(req);
593 kfree(req);
594 goto out;
595 }
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100596
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200597 dev->power.qos->latency_req = req;
598 ret = pm_qos_sysfs_add_latency(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100599 if (ret)
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200600 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100601
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100602 out:
603 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100604 return ret;
605}
606EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
607
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100608static void __dev_pm_qos_hide_latency_limit(struct device *dev)
609{
610 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->latency_req) {
611 pm_qos_sysfs_remove_latency(dev);
612 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
613 }
614}
615
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100616/**
617 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
618 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
619 */
620void dev_pm_qos_hide_latency_limit(struct device *dev)
621{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100622 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100623 __dev_pm_qos_hide_latency_limit(dev);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100624 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100625}
626EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200627
628/**
629 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
630 * @dev: Device whose PM QoS flags are to be exposed to user space.
631 * @val: Initial values of the flags.
632 */
633int dev_pm_qos_expose_flags(struct device *dev, s32 val)
634{
635 struct dev_pm_qos_request *req;
636 int ret;
637
638 if (!device_is_registered(dev))
639 return -EINVAL;
640
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200641 req = kzalloc(sizeof(*req), GFP_KERNEL);
642 if (!req)
643 return -ENOMEM;
644
645 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100646 if (ret < 0) {
647 kfree(req);
648 return ret;
649 }
650
651 pm_runtime_get_sync(dev);
652 mutex_lock(&dev_pm_qos_mtx);
653
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100654 if (IS_ERR_OR_NULL(dev->power.qos))
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100655 ret = -ENODEV;
656 else if (dev->power.qos->flags_req)
657 ret = -EEXIST;
658
659 if (ret < 0) {
660 __dev_pm_qos_remove_request(req);
661 kfree(req);
662 goto out;
663 }
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200664
665 dev->power.qos->flags_req = req;
666 ret = pm_qos_sysfs_add_flags(dev);
667 if (ret)
668 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
669
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100670 out:
671 mutex_unlock(&dev_pm_qos_mtx);
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800672 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200673 return ret;
674}
675EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
676
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100677static void __dev_pm_qos_hide_flags(struct device *dev)
678{
679 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req) {
680 pm_qos_sysfs_remove_flags(dev);
681 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
682 }
683}
684
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200685/**
686 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
687 * @dev: Device whose PM QoS flags are to be hidden from user space.
688 */
689void dev_pm_qos_hide_flags(struct device *dev)
690{
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100691 pm_runtime_get_sync(dev);
692 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100693 __dev_pm_qos_hide_flags(dev);
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100694 mutex_unlock(&dev_pm_qos_mtx);
695 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200696}
697EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
698
699/**
700 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
701 * @dev: Device to update the PM QoS flags request for.
702 * @mask: Flags to set/clear.
703 * @set: Whether to set or clear the flags (true means set).
704 */
705int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
706{
707 s32 value;
708 int ret;
709
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200710 pm_runtime_get_sync(dev);
711 mutex_lock(&dev_pm_qos_mtx);
712
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100713 if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) {
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100714 ret = -EINVAL;
715 goto out;
716 }
717
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200718 value = dev_pm_qos_requested_flags(dev);
719 if (set)
720 value |= mask;
721 else
722 value &= ~mask;
723
724 ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
725
Rafael J. Wysockib81ea1b2013-03-03 22:48:14 +0100726 out:
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200727 mutex_unlock(&dev_pm_qos_mtx);
728 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200729 return ret;
730}
Rafael J. Wysocki37530f22013-03-04 14:22:57 +0100731#else /* !CONFIG_PM_RUNTIME */
732static void __dev_pm_qos_hide_latency_limit(struct device *dev) {}
733static void __dev_pm_qos_hide_flags(struct device *dev) {}
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100734#endif /* CONFIG_PM_RUNTIME */