blob: d21349544ce5b8131b0ca217ec5618af7beca346 [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}
94
95/**
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +010096 * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
97 * @dev: Device to get the PM QoS constraint value for.
98 *
99 * This routine must be called with dev->power.lock held.
100 */
101s32 __dev_pm_qos_read_value(struct device *dev)
102{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200103 return dev->power.qos ? pm_qos_read_value(&dev->power.qos->latency) : 0;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100104}
105
106/**
107 * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200108 * @dev: Device to get the PM QoS constraint value for.
109 */
110s32 dev_pm_qos_read_value(struct device *dev)
111{
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200112 unsigned long flags;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100113 s32 ret;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200114
115 spin_lock_irqsave(&dev->power.lock, flags);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100116 ret = __dev_pm_qos_read_value(dev);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200117 spin_unlock_irqrestore(&dev->power.lock, flags);
118
119 return ret;
120}
121
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200122/**
123 * apply_constraint - Add/modify/remove device PM QoS request.
124 * @req: Constraint request to apply
125 * @action: Action to perform (add/update/remove).
126 * @value: Value to assign to the QoS request.
Jean Pihetb66213c2011-08-25 15:35:47 +0200127 *
128 * Internal function to update the constraints list using the PM QoS core
129 * code and if needed call the per-device and the global notification
130 * callbacks
131 */
132static int apply_constraint(struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200133 enum pm_qos_req_action action, s32 value)
Jean Pihetb66213c2011-08-25 15:35:47 +0200134{
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200135 struct dev_pm_qos *qos = req->dev->power.qos;
136 int ret;
Jean Pihetb66213c2011-08-25 15:35:47 +0200137
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200138 switch(req->type) {
139 case DEV_PM_QOS_LATENCY:
140 ret = pm_qos_update_target(&qos->latency, &req->data.pnode,
141 action, value);
142 if (ret) {
143 value = pm_qos_read_value(&qos->latency);
144 blocking_notifier_call_chain(&dev_pm_notifiers,
145 (unsigned long)value,
146 req);
147 }
148 break;
149 case DEV_PM_QOS_FLAGS:
150 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
151 action, value);
152 break;
153 default:
154 ret = -EINVAL;
Jean Pihetb66213c2011-08-25 15:35:47 +0200155 }
156
157 return ret;
158}
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200159
160/*
161 * dev_pm_qos_constraints_allocate
162 * @dev: device to allocate data for
163 *
164 * Called at the first call to add_request, for constraint data allocation
165 * Must be called with the dev_pm_qos_mtx mutex held
166 */
167static int dev_pm_qos_constraints_allocate(struct device *dev)
168{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200169 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200170 struct pm_qos_constraints *c;
171 struct blocking_notifier_head *n;
172
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200173 qos = kzalloc(sizeof(*qos), GFP_KERNEL);
174 if (!qos)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200175 return -ENOMEM;
176
177 n = kzalloc(sizeof(*n), GFP_KERNEL);
178 if (!n) {
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200179 kfree(qos);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200180 return -ENOMEM;
181 }
182 BLOCKING_INIT_NOTIFIER_HEAD(n);
183
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200184 c = &qos->latency;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200185 plist_head_init(&c->list);
186 c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
187 c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
188 c->type = PM_QOS_MIN;
189 c->notifiers = n;
190
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200191 INIT_LIST_HEAD(&qos->flags.list);
192
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200193 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200194 dev->power.qos = qos;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200195 spin_unlock_irq(&dev->power.lock);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200196
197 return 0;
198}
199
200/**
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200201 * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200202 * @dev: target device
203 *
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200204 * Called from the device PM subsystem during device insertion under
205 * device_pm_lock().
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200206 */
207void dev_pm_qos_constraints_init(struct device *dev)
208{
209 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200210 dev->power.qos = NULL;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200211 dev->power.power_state = PMSG_ON;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200212 mutex_unlock(&dev_pm_qos_mtx);
213}
214
215/**
216 * dev_pm_qos_constraints_destroy
217 * @dev: target device
218 *
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200219 * Called from the device PM subsystem on device removal under device_pm_lock().
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200220 */
221void dev_pm_qos_constraints_destroy(struct device *dev)
222{
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200223 struct dev_pm_qos *qos;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200224 struct dev_pm_qos_request *req, *tmp;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200225 struct pm_qos_constraints *c;
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100226 struct pm_qos_flags *f;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200227
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100228 /*
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100229 * If the device's PM QoS resume latency limit or PM QoS flags have been
230 * exposed to user space, they have to be hidden at this point.
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100231 */
232 dev_pm_qos_hide_latency_limit(dev);
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100233 dev_pm_qos_hide_flags(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100234
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200235 mutex_lock(&dev_pm_qos_mtx);
236
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200237 dev->power.power_state = PMSG_INVALID;
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200238 qos = dev->power.qos;
239 if (!qos)
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200240 goto out;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200241
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100242 /* Flush the constraints lists for the device. */
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200243 c = &qos->latency;
Rafael J. Wysocki021c8702012-10-23 01:09:00 +0200244 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200245 /*
246 * Update constraints list and call the notification
247 * callbacks if needed
248 */
249 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
250 memset(req, 0, sizeof(*req));
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200251 }
Rafael J. Wysocki35546bd2012-11-24 10:10:51 +0100252 f = &qos->flags;
253 list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
254 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
255 memset(req, 0, sizeof(*req));
256 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200257
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200258 spin_lock_irq(&dev->power.lock);
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200259 dev->power.qos = NULL;
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200260 spin_unlock_irq(&dev->power.lock);
261
262 kfree(c->notifiers);
Lan,Tianyu9eaee2c2012-11-01 22:45:30 +0100263 kfree(qos);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200264
265 out:
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200266 mutex_unlock(&dev_pm_qos_mtx);
267}
268
269/**
270 * dev_pm_qos_add_request - inserts new qos request into the list
271 * @dev: target device for the constraint
272 * @req: pointer to a preallocated handle
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200273 * @type: type of the request
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200274 * @value: defines the qos request
275 *
276 * This function inserts a new entry in the device constraints list of
277 * requested qos performance characteristics. It recomputes the aggregate
278 * QoS expectations of parameters and initializes the dev_pm_qos_request
279 * handle. Caller needs to save this handle for later use in updates and
280 * removal.
281 *
282 * Returns 1 if the aggregated constraint value has changed,
283 * 0 if the aggregated constraint value has not changed,
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200284 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
285 * to allocate for data structures, -ENODEV if the device has just been removed
286 * from the system.
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100287 *
288 * Callers should ensure that the target device is not RPM_SUSPENDED before
289 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200290 */
291int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200292 enum dev_pm_qos_req_type type, s32 value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200293{
294 int ret = 0;
295
296 if (!dev || !req) /*guard against callers passing in null */
297 return -EINVAL;
298
Guennadi Liakhovetskiaf4c7202011-11-10 00:44:18 +0100299 if (WARN(dev_pm_qos_request_active(req),
300 "%s() called for already added request\n", __func__))
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200301 return -EINVAL;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200302
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200303 req->dev = dev;
304
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200305 mutex_lock(&dev_pm_qos_mtx);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200306
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200307 if (!dev->power.qos) {
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200308 if (dev->power.power_state.event == PM_EVENT_INVALID) {
309 /* The device has been removed from the system. */
310 req->dev = NULL;
311 ret = -ENODEV;
312 goto out;
313 } else {
314 /*
315 * Allocate the constraints data on the first call to
316 * add_request, i.e. only if the data is not already
317 * allocated and if the device has not been removed.
318 */
319 ret = dev_pm_qos_constraints_allocate(dev);
320 }
321 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200322
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200323 if (!ret) {
324 req->type = type;
Jean Pihetb66213c2011-08-25 15:35:47 +0200325 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200326 }
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200327
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200328 out:
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200329 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200330
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200331 return ret;
332}
333EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
334
335/**
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200336 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
337 * @req : PM QoS request to modify.
338 * @new_value: New value to request.
339 */
340static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
341 s32 new_value)
342{
343 s32 curr_value;
344 int ret = 0;
345
346 if (!req->dev->power.qos)
347 return -ENODEV;
348
349 switch(req->type) {
350 case DEV_PM_QOS_LATENCY:
351 curr_value = req->data.pnode.prio;
352 break;
353 case DEV_PM_QOS_FLAGS:
354 curr_value = req->data.flr.flags;
355 break;
356 default:
357 return -EINVAL;
358 }
359
360 if (curr_value != new_value)
361 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
362
363 return ret;
364}
365
366/**
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200367 * dev_pm_qos_update_request - modifies an existing qos request
368 * @req : handle to list element holding a dev_pm_qos request to use
369 * @new_value: defines the qos request
370 *
371 * Updates an existing dev PM qos request along with updating the
372 * target value.
373 *
374 * Attempts are made to make this code callable on hot code paths.
375 *
376 * Returns 1 if the aggregated constraint value has changed,
377 * 0 if the aggregated constraint value has not changed,
378 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
379 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100380 *
381 * Callers should ensure that the target device is not RPM_SUSPENDED before
382 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200383 */
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200384int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200385{
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200386 int ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200387
388 if (!req) /*guard against callers passing in null */
389 return -EINVAL;
390
Guennadi Liakhovetskiaf4c7202011-11-10 00:44:18 +0100391 if (WARN(!dev_pm_qos_request_active(req),
392 "%s() called for unknown object\n", __func__))
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200393 return -EINVAL;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200394
395 mutex_lock(&dev_pm_qos_mtx);
Rafael J. Wysockif9652872012-10-30 20:00:30 +0100396 ret = __dev_pm_qos_update_request(req, new_value);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200397 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200398
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200399 return ret;
400}
401EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
402
403/**
404 * dev_pm_qos_remove_request - modifies an existing qos request
405 * @req: handle to request list element
406 *
407 * Will remove pm qos request from the list of constraints and
408 * recompute the current target value. Call this on slow code paths.
409 *
410 * Returns 1 if the aggregated constraint value has changed,
411 * 0 if the aggregated constraint value has not changed,
412 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
413 * removed from the system
Rafael J. Wysocki436ede82012-11-02 13:10:09 +0100414 *
415 * Callers should ensure that the target device is not RPM_SUSPENDED before
416 * using this function for requests of type DEV_PM_QOS_FLAGS.
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200417 */
418int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
419{
420 int ret = 0;
421
422 if (!req) /*guard against callers passing in null */
423 return -EINVAL;
424
Guennadi Liakhovetskiaf4c7202011-11-10 00:44:18 +0100425 if (WARN(!dev_pm_qos_request_active(req),
426 "%s() called for unknown object\n", __func__))
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200427 return -EINVAL;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200428
429 mutex_lock(&dev_pm_qos_mtx);
430
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200431 if (req->dev->power.qos) {
Jean Pihetb66213c2011-08-25 15:35:47 +0200432 ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
433 PM_QOS_DEFAULT_VALUE);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200434 memset(req, 0, sizeof(*req));
435 } else {
436 /* Return if the device has been removed */
437 ret = -ENODEV;
438 }
439
440 mutex_unlock(&dev_pm_qos_mtx);
441 return ret;
442}
443EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
444
445/**
446 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
447 * of per-device PM QoS constraints
448 *
449 * @dev: target device for the constraint
450 * @notifier: notifier block managed by caller.
451 *
452 * Will register the notifier into a notification chain that gets called
453 * upon changes to the target value for the device.
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200454 *
455 * If the device's constraints object doesn't exist when this routine is called,
456 * it will be created (or error code will be returned if that fails).
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200457 */
458int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
459{
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200460 int ret = 0;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200461
462 mutex_lock(&dev_pm_qos_mtx);
463
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200464 if (!dev->power.qos)
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200465 ret = dev->power.power_state.event != PM_EVENT_INVALID ?
466 dev_pm_qos_constraints_allocate(dev) : -ENODEV;
467
468 if (!ret)
469 ret = blocking_notifier_chain_register(
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200470 dev->power.qos->latency.notifiers, notifier);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200471
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200472 mutex_unlock(&dev_pm_qos_mtx);
Rafael J. Wysocki23e0fc52012-04-29 22:54:47 +0200473 return ret;
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200474}
475EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
476
477/**
478 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
479 * of per-device PM QoS constraints
480 *
481 * @dev: target device for the constraint
482 * @notifier: notifier block to be removed.
483 *
484 * Will remove the notifier from the notification chain that gets called
485 * upon changes to the target value.
486 */
487int dev_pm_qos_remove_notifier(struct device *dev,
488 struct notifier_block *notifier)
489{
490 int retval = 0;
491
492 mutex_lock(&dev_pm_qos_mtx);
493
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200494 /* Silently return if the constraints object is not present. */
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200495 if (dev->power.qos)
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200496 retval = blocking_notifier_chain_unregister(
Rafael J. Wysocki5f986c52012-10-23 01:07:27 +0200497 dev->power.qos->latency.notifiers,
Rafael J. Wysocki1a9a9152011-09-29 22:29:44 +0200498 notifier);
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200499
Jean Pihet91ff4cb2011-08-25 15:35:41 +0200500 mutex_unlock(&dev_pm_qos_mtx);
501 return retval;
502}
503EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
Jean Pihetb66213c2011-08-25 15:35:47 +0200504
505/**
506 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
507 * target value of the PM QoS constraints for any device
508 *
509 * @notifier: notifier block managed by caller.
510 *
511 * Will register the notifier into a notification chain that gets called
512 * upon changes to the target value for any device.
513 */
514int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
515{
516 return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
517}
518EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
519
520/**
521 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
522 * target value of PM QoS constraints for any device
523 *
524 * @notifier: notifier block to be removed.
525 *
526 * Will remove the notifier from the notification chain that gets called
527 * upon changes to the target value for any device.
528 */
529int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
530{
531 return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
532}
533EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100534
535/**
536 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
537 * @dev: Device whose ancestor to add the request for.
538 * @req: Pointer to the preallocated handle.
539 * @value: Constraint latency value.
540 */
541int dev_pm_qos_add_ancestor_request(struct device *dev,
542 struct dev_pm_qos_request *req, s32 value)
543{
544 struct device *ancestor = dev->parent;
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100545 int ret = -ENODEV;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100546
547 while (ancestor && !ancestor->power.ignore_children)
548 ancestor = ancestor->parent;
549
550 if (ancestor)
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100551 ret = dev_pm_qos_add_request(ancestor, req,
552 DEV_PM_QOS_LATENCY, value);
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100553
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100554 if (ret < 0)
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100555 req->dev = NULL;
556
Rafael J. Wysocki4ce47802012-12-18 14:07:49 +0100557 return ret;
Rafael J. Wysocki40a5f8b2011-12-23 01:23:52 +0100558}
559EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100560
561#ifdef CONFIG_PM_RUNTIME
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200562static void __dev_pm_qos_drop_user_request(struct device *dev,
563 enum dev_pm_qos_req_type type)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100564{
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200565 switch(type) {
566 case DEV_PM_QOS_LATENCY:
567 dev_pm_qos_remove_request(dev->power.qos->latency_req);
568 dev->power.qos->latency_req = NULL;
569 break;
570 case DEV_PM_QOS_FLAGS:
571 dev_pm_qos_remove_request(dev->power.qos->flags_req);
572 dev->power.qos->flags_req = NULL;
573 break;
574 }
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100575}
576
577/**
578 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
579 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
580 * @value: Initial value of the latency limit.
581 */
582int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
583{
584 struct dev_pm_qos_request *req;
585 int ret;
586
587 if (!device_is_registered(dev) || value < 0)
588 return -EINVAL;
589
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200590 if (dev->power.qos && dev->power.qos->latency_req)
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100591 return -EEXIST;
592
593 req = kzalloc(sizeof(*req), GFP_KERNEL);
594 if (!req)
595 return -ENOMEM;
596
Rafael J. Wysockiae0fb4b2012-10-23 01:09:12 +0200597 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY, value);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100598 if (ret < 0)
599 return ret;
600
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200601 dev->power.qos->latency_req = req;
602 ret = pm_qos_sysfs_add_latency(dev);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100603 if (ret)
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200604 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100605
606 return ret;
607}
608EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
609
610/**
611 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
612 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
613 */
614void dev_pm_qos_hide_latency_limit(struct device *dev)
615{
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200616 if (dev->power.qos && dev->power.qos->latency_req) {
617 pm_qos_sysfs_remove_latency(dev);
618 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100619 }
620}
621EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200622
623/**
624 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
625 * @dev: Device whose PM QoS flags are to be exposed to user space.
626 * @val: Initial values of the flags.
627 */
628int dev_pm_qos_expose_flags(struct device *dev, s32 val)
629{
630 struct dev_pm_qos_request *req;
631 int ret;
632
633 if (!device_is_registered(dev))
634 return -EINVAL;
635
636 if (dev->power.qos && dev->power.qos->flags_req)
637 return -EEXIST;
638
639 req = kzalloc(sizeof(*req), GFP_KERNEL);
640 if (!req)
641 return -ENOMEM;
642
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800643 pm_runtime_get_sync(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200644 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
645 if (ret < 0)
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800646 goto fail;
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200647
648 dev->power.qos->flags_req = req;
649 ret = pm_qos_sysfs_add_flags(dev);
650 if (ret)
651 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
652
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800653fail:
654 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200655 return ret;
656}
657EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
658
659/**
660 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
661 * @dev: Device whose PM QoS flags are to be hidden from user space.
662 */
663void dev_pm_qos_hide_flags(struct device *dev)
664{
665 if (dev->power.qos && dev->power.qos->flags_req) {
666 pm_qos_sysfs_remove_flags(dev);
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800667 pm_runtime_get_sync(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200668 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
Lan Tianyu7e4d6842012-11-08 11:14:08 +0800669 pm_runtime_put(dev);
Rafael J. Wysockie39473d2012-10-24 02:08:18 +0200670 }
671}
672EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
673
674/**
675 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
676 * @dev: Device to update the PM QoS flags request for.
677 * @mask: Flags to set/clear.
678 * @set: Whether to set or clear the flags (true means set).
679 */
680int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
681{
682 s32 value;
683 int ret;
684
685 if (!dev->power.qos || !dev->power.qos->flags_req)
686 return -EINVAL;
687
688 pm_runtime_get_sync(dev);
689 mutex_lock(&dev_pm_qos_mtx);
690
691 value = dev_pm_qos_requested_flags(dev);
692 if (set)
693 value |= mask;
694 else
695 value &= ~mask;
696
697 ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
698
699 mutex_unlock(&dev_pm_qos_mtx);
700 pm_runtime_put(dev);
701
702 return ret;
703}
Rafael J. Wysocki85dc0b82012-03-13 01:01:39 +0100704#endif /* CONFIG_PM_RUNTIME */