blob: 3d4d1f8aac5c3aa126dd1045f4d2eda48907c29e [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>
Jean Pihet91ff4cb2011-08-25 15:35:41 +020044
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +010045#include "power.h"
Jean Pihet91ff4cb2011-08-25 15:35:41 +020046
47static DEFINE_MUTEX(dev_pm_qos_mtx);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +020048
Jean Pihetb66213c2011-08-25 15:35:47 +020049static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
50
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +020051/**
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020052 * __dev_pm_qos_flags - Check PM QoS flags for a given device.
53 * @dev: Device to check the PM QoS flags for.
54 * @mask: Flags to check against.
55 *
56 * This routine must be called with dev->power.lock held.
57 */
58enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask)
59{
60 struct dev_pm_qos *qos = dev->power.qos;
61 struct pm_qos_flags *pqf;
62 s32 val;
63
64 if (!qos)
65 return PM_QOS_FLAGS_UNDEFINED;
66
67 pqf = &qos->flags;
68 if (list_empty(&pqf->list))
69 return PM_QOS_FLAGS_UNDEFINED;
70
71 val = pqf->effective_flags & mask;
72 if (val)
73 return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME;
74
75 return PM_QOS_FLAGS_NONE;
76}
77
78/**
79 * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
80 * @dev: Device to check the PM QoS flags for.
81 * @mask: Flags to check against.
82 */
83enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask)
84{
85 unsigned long irqflags;
86 enum pm_qos_flags_status ret;
87
88 spin_lock_irqsave(&dev->power.lock, irqflags);
89 ret = __dev_pm_qos_flags(dev, mask);
90 spin_unlock_irqrestore(&dev->power.lock, irqflags);
91
92 return ret;
93}
Lan Tianyu68027712013-01-23 04:26:28 +080094EXPORT_SYMBOL_GPL(dev_pm_qos_flags);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +020095
96/**
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +010097 * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
98 * @dev: Device to get the PM QoS constraint value for.
99 *
100 * This routine must be called with dev->power.lock held.
101 */
102s32 __dev_pm_qos_read_value(struct device *dev)
103{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200104 return dev->power.qos ? pm_qos_read_value(&dev->power.qos->latency) : 0;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100105}
106
107/**
108 * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200109 * @dev: Device to get the PM QoS constraint value for.
110 */
111s32 dev_pm_qos_read_value(struct device *dev)
112{
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200113 unsigned long flags;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100114 s32 ret;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200115
116 spin_lock_irqsave(&dev->power.lock, flags);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100117 ret = __dev_pm_qos_read_value(dev);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200118 spin_unlock_irqrestore(&dev->power.lock, flags);
119
120 return ret;
121}
122
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200123/**
124 * apply_constraint - Add/modify/remove device PM QoS request.
125 * @req: Constraint request to apply
126 * @action: Action to perform (add/update/remove).
127 * @value: Value to assign to the QoS request.
Jean Pihetb66213c2011-08-25 15:35:47 +0200128 *
129 * Internal function to update the constraints list using the PM QoS core
130 * code and if needed call the per-device and the global notification
131 * callbacks
132 */
133static int apply_constraint(struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200134 enum pm_qos_req_action action, s32 value)
Jean Pihetb66213c2011-08-25 15:35:47 +0200135{
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200136 struct dev_pm_qos *qos = req->dev->power.qos;
137 int ret;
Jean Pihetb66213c2011-08-25 15:35:47 +0200138
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200139 switch(req->type) {
140 case DEV_PM_QOS_LATENCY:
141 ret = pm_qos_update_target(&qos->latency, &req->data.pnode,
142 action, value);
143 if (ret) {
144 value = pm_qos_read_value(&qos->latency);
145 blocking_notifier_call_chain(&dev_pm_notifiers,
146 (unsigned long)value,
147 req);
148 }
149 break;
150 case DEV_PM_QOS_FLAGS:
151 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
152 action, value);
153 break;
154 default:
155 ret = -EINVAL;
Jean Pihetb66213c2011-08-25 15:35:47 +0200156 }
157
158 return ret;
159}
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200160
161/*
162 * dev_pm_qos_constraints_allocate
163 * @dev: device to allocate data for
164 *
165 * Called at the first call to add_request, for constraint data allocation
166 * Must be called with the dev_pm_qos_mtx mutex held
167 */
168static int dev_pm_qos_constraints_allocate(struct device *dev)
169{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200170 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200171 struct pm_qos_constraints *c;
172 struct blocking_notifier_head *n;
173
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200174 qos = kzalloc(sizeof(*qos), GFP_KERNEL);
175 if (!qos)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200176 return -ENOMEM;
177
178 n = kzalloc(sizeof(*n), GFP_KERNEL);
179 if (!n) {
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200180 kfree(qos);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200181 return -ENOMEM;
182 }
183 BLOCKING_INIT_NOTIFIER_HEAD(n);
184
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200185 c = &qos->latency;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200186 plist_head_init(&c->list);
187 c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
188 c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
189 c->type = PM_QOS_MIN;
190 c->notifiers = n;
191
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200192 INIT_LIST_HEAD(&qos->flags.list);
193
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200194 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200195 dev->power.qos = qos;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200196 spin_unlock_irq(&dev->power.lock);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200197
198 return 0;
199}
200
201/**
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200202 * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200203 * @dev: target device
204 *
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200205 * Called from the device PM subsystem during device insertion under
206 * device_pm_lock().
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200207 */
208void dev_pm_qos_constraints_init(struct device *dev)
209{
210 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200211 dev->power.qos = NULL;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200212 dev->power.power_state = PMSG_ON;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200213 mutex_unlock(&dev_pm_qos_mtx);
214}
215
216/**
217 * dev_pm_qos_constraints_destroy
218 * @dev: target device
219 *
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200220 * Called from the device PM subsystem on device removal under device_pm_lock().
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200221 */
222void dev_pm_qos_constraints_destroy(struct device *dev)
223{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200224 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200225 struct dev_pm_qos_request *req, *tmp;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200226 struct pm_qos_constraints *c;
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100227 struct pm_qos_flags *f;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200228
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100229 /*
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100230 * If the device's PM QoS resume latency limit or PM QoS flags have been
231 * exposed to user space, they have to be hidden at this point.
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100232 */
233 dev_pm_qos_hide_latency_limit(dev);
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100234 dev_pm_qos_hide_flags(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100235
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200236 mutex_lock(&dev_pm_qos_mtx);
237
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200238 dev->power.power_state = PMSG_INVALID;
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200239 qos = dev->power.qos;
240 if (!qos)
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200241 goto out;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200242
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100243 /* Flush the constraints lists for the device. */
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200244 c = &qos->latency;
Rafael J. Wysocki021c8702012-10-23 01:09:00 +0200245 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200246 /*
247 * Update constraints list and call the notification
248 * callbacks if needed
249 */
250 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
251 memset(req, 0, sizeof(*req));
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200252 }
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100253 f = &qos->flags;
254 list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
255 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
256 memset(req, 0, sizeof(*req));
257 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200258
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200259 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200260 dev->power.qos = NULL;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200261 spin_unlock_irq(&dev->power.lock);
262
263 kfree(c->notifiers);
Lan,Tianyu9eaee2c2012-11-01 22:45:30 +0100264 kfree(qos);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200265
266 out:
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200267 mutex_unlock(&dev_pm_qos_mtx);
268}
269
270/**
271 * dev_pm_qos_add_request - inserts new qos request into the list
272 * @dev: target device for the constraint
273 * @req: pointer to a preallocated handle
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200274 * @type: type of the request
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200275 * @value: defines the qos request
276 *
277 * This function inserts a new entry in the device constraints list of
278 * requested qos performance characteristics. It recomputes the aggregate
279 * QoS expectations of parameters and initializes the dev_pm_qos_request
280 * handle. Caller needs to save this handle for later use in updates and
281 * removal.
282 *
283 * Returns 1 if the aggregated constraint value has changed,
284 * 0 if the aggregated constraint value has not changed,
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200285 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
286 * to allocate for data structures, -ENODEV if the device has just been removed
287 * from the system.
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100288 *
289 * Callers should ensure that the target device is not RPM_SUSPENDED before
290 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200291 */
292int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200293 enum dev_pm_qos_req_type type, s32 value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200294{
295 int ret = 0;
296
297 if (!dev || !req) /*guard against callers passing in null */
298 return -EINVAL;
299
Guennadi Liakhovetskiaf4c7202011-11-10 00:44:18 +0100300 if (WARN(dev_pm_qos_request_active(req),
301 "%s() called for already added request\n", __func__))
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200302 return -EINVAL;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200303
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200304 req->dev = dev;
305
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200306 mutex_lock(&dev_pm_qos_mtx);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200307
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200308 if (!dev->power.qos) {
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200309 if (dev->power.power_state.event == PM_EVENT_INVALID) {
310 /* The device has been removed from the system. */
311 req->dev = NULL;
312 ret = -ENODEV;
313 goto out;
314 } else {
315 /*
316 * Allocate the constraints data on the first call to
317 * add_request, i.e. only if the data is not already
318 * allocated and if the device has not been removed.
319 */
320 ret = dev_pm_qos_constraints_allocate(dev);
321 }
322 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200323
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200324 if (!ret) {
325 req->type = type;
Jean Pihetb66213c2011-08-25 15:35:47 +0200326 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200327 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200328
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200329 out:
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200330 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200331
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200332 return ret;
333}
334EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
335
336/**
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200337 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
338 * @req : PM QoS request to modify.
339 * @new_value: New value to request.
340 */
341static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
342 s32 new_value)
343{
344 s32 curr_value;
345 int ret = 0;
346
347 if (!req->dev->power.qos)
348 return -ENODEV;
349
350 switch(req->type) {
351 case DEV_PM_QOS_LATENCY:
352 curr_value = req->data.pnode.prio;
353 break;
354 case DEV_PM_QOS_FLAGS:
355 curr_value = req->data.flr.flags;
356 break;
357 default:
358 return -EINVAL;
359 }
360
361 if (curr_value != new_value)
362 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
363
364 return ret;
365}
366
367/**
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200368 * dev_pm_qos_update_request - modifies an existing qos request
369 * @req : handle to list element holding a dev_pm_qos request to use
370 * @new_value: defines the qos request
371 *
372 * Updates an existing dev PM qos request along with updating the
373 * target value.
374 *
375 * Attempts are made to make this code callable on hot code paths.
376 *
377 * Returns 1 if the aggregated constraint value has changed,
378 * 0 if the aggregated constraint value has not changed,
379 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
380 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100381 *
382 * Callers should ensure that the target device is not RPM_SUSPENDED before
383 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200384 */
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200385int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200386{
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200387 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200388
389 if (!req) /*guard against callers passing in null */
390 return -EINVAL;
391
Guennadi Liakhovetskiaf4c7202011-11-10 00:44:18 +0100392 if (WARN(!dev_pm_qos_request_active(req),
393 "%s() called for unknown object\n", __func__))
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200394 return -EINVAL;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200395
396 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysockif9652872012-10-30 20:00:30 +0100397 ret = __dev_pm_qos_update_request(req, new_value);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200398 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200399
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200400 return ret;
401}
402EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
403
404/**
405 * dev_pm_qos_remove_request - modifies an existing qos request
406 * @req: handle to request list element
407 *
408 * Will remove pm qos request from the list of constraints and
409 * recompute the current target value. Call this on slow code paths.
410 *
411 * Returns 1 if the aggregated constraint value has changed,
412 * 0 if the aggregated constraint value has not changed,
413 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
414 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100415 *
416 * Callers should ensure that the target device is not RPM_SUSPENDED before
417 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200418 */
419int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
420{
421 int ret = 0;
422
423 if (!req) /*guard against callers passing in null */
424 return -EINVAL;
425
Guennadi Liakhovetskiaf4c7202011-11-10 00:44:18 +0100426 if (WARN(!dev_pm_qos_request_active(req),
427 "%s() called for unknown object\n", __func__))
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200428 return -EINVAL;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200429
430 mutex_lock(&dev_pm_qos_mtx);
431
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200432 if (req->dev->power.qos) {
Jean Pihetb66213c2011-08-25 15:35:47 +0200433 ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
434 PM_QOS_DEFAULT_VALUE);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200435 memset(req, 0, sizeof(*req));
436 } else {
437 /* Return if the device has been removed */
438 ret = -ENODEV;
439 }
440
441 mutex_unlock(&dev_pm_qos_mtx);
442 return ret;
443}
444EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
445
446/**
447 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
448 * of per-device PM QoS constraints
449 *
450 * @dev: target device for the constraint
451 * @notifier: notifier block managed by caller.
452 *
453 * Will register the notifier into a notification chain that gets called
454 * upon changes to the target value for the device.
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200455 *
456 * If the device's constraints object doesn't exist when this routine is called,
457 * it will be created (or error code will be returned if that fails).
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200458 */
459int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
460{
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200461 int ret = 0;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200462
463 mutex_lock(&dev_pm_qos_mtx);
464
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200465 if (!dev->power.qos)
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200466 ret = dev->power.power_state.event != PM_EVENT_INVALID ?
467 dev_pm_qos_constraints_allocate(dev) : -ENODEV;
468
469 if (!ret)
470 ret = blocking_notifier_chain_register(
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200471 dev->power.qos->latency.notifiers, notifier);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200472
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200473 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200474 return ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200475}
476EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
477
478/**
479 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
480 * of per-device PM QoS constraints
481 *
482 * @dev: target device for the constraint
483 * @notifier: notifier block to be removed.
484 *
485 * Will remove the notifier from the notification chain that gets called
486 * upon changes to the target value.
487 */
488int dev_pm_qos_remove_notifier(struct device *dev,
489 struct notifier_block *notifier)
490{
491 int retval = 0;
492
493 mutex_lock(&dev_pm_qos_mtx);
494
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200495 /* Silently return if the constraints object is not present. */
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200496 if (dev->power.qos)
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200497 retval = blocking_notifier_chain_unregister(
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200498 dev->power.qos->latency.notifiers,
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200499 notifier);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200500
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200501 mutex_unlock(&dev_pm_qos_mtx);
502 return retval;
503}
504EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
Jean Pihetb66213c2011-08-25 15:35:47 +0200505
506/**
507 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
508 * target value of the PM QoS constraints for any device
509 *
510 * @notifier: notifier block managed by caller.
511 *
512 * Will register the notifier into a notification chain that gets called
513 * upon changes to the target value for any device.
514 */
515int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
516{
517 return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
518}
519EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
520
521/**
522 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
523 * target value of PM QoS constraints for any device
524 *
525 * @notifier: notifier block to be removed.
526 *
527 * Will remove the notifier from the notification chain that gets called
528 * upon changes to the target value for any device.
529 */
530int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
531{
532 return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
533}
534EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100535
536/**
537 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
538 * @dev: Device whose ancestor to add the request for.
539 * @req: Pointer to the preallocated handle.
540 * @value: Constraint latency value.
541 */
542int dev_pm_qos_add_ancestor_request(struct device *dev,
543 struct dev_pm_qos_request *req, s32 value)
544{
545 struct device *ancestor = dev->parent;
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100546 int ret = -ENODEV;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100547
548 while (ancestor && !ancestor->power.ignore_children)
549 ancestor = ancestor->parent;
550
551 if (ancestor)
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100552 ret = dev_pm_qos_add_request(ancestor, req,
553 DEV_PM_QOS_LATENCY, value);
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100554
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100555 if (ret < 0)
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100556 req->dev = NULL;
557
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100558 return ret;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100559}
560EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100561
562#ifdef CONFIG_PM_RUNTIME
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200563static void __dev_pm_qos_drop_user_request(struct device *dev,
564 enum dev_pm_qos_req_type type)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100565{
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200566 switch(type) {
567 case DEV_PM_QOS_LATENCY:
568 dev_pm_qos_remove_request(dev->power.qos->latency_req);
569 dev->power.qos->latency_req = NULL;
570 break;
571 case DEV_PM_QOS_FLAGS:
572 dev_pm_qos_remove_request(dev->power.qos->flags_req);
573 dev->power.qos->flags_req = NULL;
574 break;
575 }
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100576}
577
578/**
579 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
580 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
581 * @value: Initial value of the latency limit.
582 */
583int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
584{
585 struct dev_pm_qos_request *req;
586 int ret;
587
588 if (!device_is_registered(dev) || value < 0)
589 return -EINVAL;
590
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200591 if (dev->power.qos && dev->power.qos->latency_req)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100592 return -EEXIST;
593
594 req = kzalloc(sizeof(*req), GFP_KERNEL);
595 if (!req)
596 return -ENOMEM;
597
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200598 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY, value);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100599 if (ret < 0)
600 return ret;
601
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200602 dev->power.qos->latency_req = req;
603 ret = pm_qos_sysfs_add_latency(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100604 if (ret)
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200605 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100606
607 return ret;
608}
609EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
610
611/**
612 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
613 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
614 */
615void dev_pm_qos_hide_latency_limit(struct device *dev)
616{
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200617 if (dev->power.qos && dev->power.qos->latency_req) {
618 pm_qos_sysfs_remove_latency(dev);
619 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100620 }
621}
622EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200623
624/**
625 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
626 * @dev: Device whose PM QoS flags are to be exposed to user space.
627 * @val: Initial values of the flags.
628 */
629int dev_pm_qos_expose_flags(struct device *dev, s32 val)
630{
631 struct dev_pm_qos_request *req;
632 int ret;
633
634 if (!device_is_registered(dev))
635 return -EINVAL;
636
637 if (dev->power.qos && dev->power.qos->flags_req)
638 return -EEXIST;
639
640 req = kzalloc(sizeof(*req), GFP_KERNEL);
641 if (!req)
642 return -ENOMEM;
643
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800644 pm_runtime_get_sync(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200645 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
646 if (ret < 0)
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800647 goto fail;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200648
649 dev->power.qos->flags_req = req;
650 ret = pm_qos_sysfs_add_flags(dev);
651 if (ret)
652 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
653
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800654fail:
655 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200656 return ret;
657}
658EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
659
660/**
661 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
662 * @dev: Device whose PM QoS flags are to be hidden from user space.
663 */
664void dev_pm_qos_hide_flags(struct device *dev)
665{
666 if (dev->power.qos && dev->power.qos->flags_req) {
667 pm_qos_sysfs_remove_flags(dev);
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800668 pm_runtime_get_sync(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200669 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800670 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200671 }
672}
673EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
674
675/**
676 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
677 * @dev: Device to update the PM QoS flags request for.
678 * @mask: Flags to set/clear.
679 * @set: Whether to set or clear the flags (true means set).
680 */
681int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
682{
683 s32 value;
684 int ret;
685
686 if (!dev->power.qos || !dev->power.qos->flags_req)
687 return -EINVAL;
688
689 pm_runtime_get_sync(dev);
690 mutex_lock(&dev_pm_qos_mtx);
691
692 value = dev_pm_qos_requested_flags(dev);
693 if (set)
694 value |= mask;
695 else
696 value &= ~mask;
697
698 ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
699
700 mutex_unlock(&dev_pm_qos_mtx);
701 pm_runtime_put(dev);
702
703 return ret;
704}
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100705#endif /* CONFIG_PM_RUNTIME */