aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/base/power
diff options
context:
space:
mode:
authorRafael J. Wysocki <rjw@sisk.pl>2011-10-07 17:17:07 -0400
committerRafael J. Wysocki <rjw@sisk.pl>2011-10-07 17:17:07 -0400
commit9696cc90071e3660ec02a3728acdedb68afdce4c (patch)
tree99783d31dd42262c29346c92760f7896c3d81cb6 /drivers/base/power
parentc28b56b1d46b1bbb1be33c8f2632a88b0de1ef68 (diff)
parente3cba3243eb853a052613c804dea033bc4c9cf2d (diff)
Merge branch 'pm-qos' into pm-for-linus
* pm-qos: PM / QoS: Update Documentation for the pm_qos and dev_pm_qos frameworks PM / QoS: Add function dev_pm_qos_read_value() (v3) PM QoS: Add global notification mechanism for device constraints PM QoS: Implement per-device PM QoS constraints PM QoS: Generalize and export constraints management code PM QoS: Reorganize data structs PM QoS: Code reorganization PM QoS: Minor clean-ups PM QoS: Move and rename the implementation files
Diffstat (limited to 'drivers/base/power')
-rw-r--r--drivers/base/power/Makefile4
-rw-r--r--drivers/base/power/main.c3
-rw-r--r--drivers/base/power/power.h10
-rw-r--r--drivers/base/power/qos.c419
4 files changed, 433 insertions, 3 deletions
diff --git a/drivers/base/power/Makefile b/drivers/base/power/Makefile
index 6488ce12f586..81676dd17900 100644
--- a/drivers/base/power/Makefile
+++ b/drivers/base/power/Makefile
@@ -1,4 +1,4 @@
1obj-$(CONFIG_PM) += sysfs.o generic_ops.o common.o 1obj-$(CONFIG_PM) += sysfs.o generic_ops.o common.o qos.o
2obj-$(CONFIG_PM_SLEEP) += main.o wakeup.o 2obj-$(CONFIG_PM_SLEEP) += main.o wakeup.o
3obj-$(CONFIG_PM_RUNTIME) += runtime.o 3obj-$(CONFIG_PM_RUNTIME) += runtime.o
4obj-$(CONFIG_PM_TRACE_RTC) += trace.o 4obj-$(CONFIG_PM_TRACE_RTC) += trace.o
@@ -6,4 +6,4 @@ obj-$(CONFIG_PM_OPP) += opp.o
6obj-$(CONFIG_PM_GENERIC_DOMAINS) += domain.o 6obj-$(CONFIG_PM_GENERIC_DOMAINS) += domain.o
7obj-$(CONFIG_HAVE_CLK) += clock_ops.o 7obj-$(CONFIG_HAVE_CLK) += clock_ops.o
8 8
9ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG \ No newline at end of file 9ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG
diff --git a/drivers/base/power/main.c b/drivers/base/power/main.c
index a85459126bc6..c6291ab725a3 100644
--- a/drivers/base/power/main.c
+++ b/drivers/base/power/main.c
@@ -65,6 +65,7 @@ void device_pm_init(struct device *dev)
65 spin_lock_init(&dev->power.lock); 65 spin_lock_init(&dev->power.lock);
66 pm_runtime_init(dev); 66 pm_runtime_init(dev);
67 INIT_LIST_HEAD(&dev->power.entry); 67 INIT_LIST_HEAD(&dev->power.entry);
68 dev->power.power_state = PMSG_INVALID;
68} 69}
69 70
70/** 71/**
@@ -96,6 +97,7 @@ void device_pm_add(struct device *dev)
96 dev_warn(dev, "parent %s should not be sleeping\n", 97 dev_warn(dev, "parent %s should not be sleeping\n",
97 dev_name(dev->parent)); 98 dev_name(dev->parent));
98 list_add_tail(&dev->power.entry, &dpm_list); 99 list_add_tail(&dev->power.entry, &dpm_list);
100 dev_pm_qos_constraints_init(dev);
99 mutex_unlock(&dpm_list_mtx); 101 mutex_unlock(&dpm_list_mtx);
100} 102}
101 103
@@ -109,6 +111,7 @@ void device_pm_remove(struct device *dev)
109 dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); 111 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
110 complete_all(&dev->power.completion); 112 complete_all(&dev->power.completion);
111 mutex_lock(&dpm_list_mtx); 113 mutex_lock(&dpm_list_mtx);
114 dev_pm_qos_constraints_destroy(dev);
112 list_del_init(&dev->power.entry); 115 list_del_init(&dev->power.entry);
113 mutex_unlock(&dpm_list_mtx); 116 mutex_unlock(&dpm_list_mtx);
114 device_wakeup_disable(dev); 117 device_wakeup_disable(dev);
diff --git a/drivers/base/power/power.h b/drivers/base/power/power.h
index f2a25f18fde7..9bf62323aaf3 100644
--- a/drivers/base/power/power.h
+++ b/drivers/base/power/power.h
@@ -1,3 +1,5 @@
1#include <linux/pm_qos.h>
2
1#ifdef CONFIG_PM_RUNTIME 3#ifdef CONFIG_PM_RUNTIME
2 4
3extern void pm_runtime_init(struct device *dev); 5extern void pm_runtime_init(struct device *dev);
@@ -35,15 +37,21 @@ extern void device_pm_move_last(struct device *);
35static inline void device_pm_init(struct device *dev) 37static inline void device_pm_init(struct device *dev)
36{ 38{
37 spin_lock_init(&dev->power.lock); 39 spin_lock_init(&dev->power.lock);
40 dev->power.power_state = PMSG_INVALID;
38 pm_runtime_init(dev); 41 pm_runtime_init(dev);
39} 42}
40 43
44static inline void device_pm_add(struct device *dev)
45{
46 dev_pm_qos_constraints_init(dev);
47}
48
41static inline void device_pm_remove(struct device *dev) 49static inline void device_pm_remove(struct device *dev)
42{ 50{
51 dev_pm_qos_constraints_destroy(dev);
43 pm_runtime_remove(dev); 52 pm_runtime_remove(dev);
44} 53}
45 54
46static inline void device_pm_add(struct device *dev) {}
47static inline void device_pm_move_before(struct device *deva, 55static inline void device_pm_move_before(struct device *deva,
48 struct device *devb) {} 56 struct device *devb) {}
49static inline void device_pm_move_after(struct device *deva, 57static inline void device_pm_move_after(struct device *deva,
diff --git a/drivers/base/power/qos.c b/drivers/base/power/qos.c
new file mode 100644
index 000000000000..91e061417382
--- /dev/null
+++ b/drivers/base/power/qos.c
@@ -0,0 +1,419 @@
1/*
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.
20 * 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.
26 *
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.
33 * . 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>
42
43
44static DEFINE_MUTEX(dev_pm_qos_mtx);
45
46static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
47
48/**
49 * dev_pm_qos_read_value - Get PM QoS constraint for a given device.
50 * @dev: Device to get the PM QoS constraint value for.
51 */
52s32 dev_pm_qos_read_value(struct device *dev)
53{
54 struct pm_qos_constraints *c;
55 unsigned long flags;
56 s32 ret = 0;
57
58 spin_lock_irqsave(&dev->power.lock, flags);
59
60 c = dev->power.constraints;
61 if (c)
62 ret = pm_qos_read_value(c);
63
64 spin_unlock_irqrestore(&dev->power.lock, flags);
65
66 return ret;
67}
68
69/*
70 * apply_constraint
71 * @req: constraint request to apply
72 * @action: action to perform add/update/remove, of type enum pm_qos_req_action
73 * @value: defines the qos request
74 *
75 * Internal function to update the constraints list using the PM QoS core
76 * code and if needed call the per-device and the global notification
77 * callbacks
78 */
79static int apply_constraint(struct dev_pm_qos_request *req,
80 enum pm_qos_req_action action, int value)
81{
82 int ret, curr_value;
83
84 ret = pm_qos_update_target(req->dev->power.constraints,
85 &req->node, action, value);
86
87 if (ret) {
88 /* Call the global callbacks if needed */
89 curr_value = pm_qos_read_value(req->dev->power.constraints);
90 blocking_notifier_call_chain(&dev_pm_notifiers,
91 (unsigned long)curr_value,
92 req);
93 }
94
95 return ret;
96}
97
98/*
99 * dev_pm_qos_constraints_allocate
100 * @dev: device to allocate data for
101 *
102 * Called at the first call to add_request, for constraint data allocation
103 * Must be called with the dev_pm_qos_mtx mutex held
104 */
105static int dev_pm_qos_constraints_allocate(struct device *dev)
106{
107 struct pm_qos_constraints *c;
108 struct blocking_notifier_head *n;
109
110 c = kzalloc(sizeof(*c), GFP_KERNEL);
111 if (!c)
112 return -ENOMEM;
113
114 n = kzalloc(sizeof(*n), GFP_KERNEL);
115 if (!n) {
116 kfree(c);
117 return -ENOMEM;
118 }
119 BLOCKING_INIT_NOTIFIER_HEAD(n);
120
121 plist_head_init(&c->list);
122 c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
123 c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
124 c->type = PM_QOS_MIN;
125 c->notifiers = n;
126
127 spin_lock_irq(&dev->power.lock);
128 dev->power.constraints = c;
129 spin_unlock_irq(&dev->power.lock);
130
131 return 0;
132}
133
134/**
135 * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer.
136 * @dev: target device
137 *
138 * Called from the device PM subsystem during device insertion under
139 * device_pm_lock().
140 */
141void dev_pm_qos_constraints_init(struct device *dev)
142{
143 mutex_lock(&dev_pm_qos_mtx);
144 dev->power.constraints = NULL;
145 dev->power.power_state = PMSG_ON;
146 mutex_unlock(&dev_pm_qos_mtx);
147}
148
149/**
150 * dev_pm_qos_constraints_destroy
151 * @dev: target device
152 *
153 * Called from the device PM subsystem on device removal under device_pm_lock().
154 */
155void dev_pm_qos_constraints_destroy(struct device *dev)
156{
157 struct dev_pm_qos_request *req, *tmp;
158 struct pm_qos_constraints *c;
159
160 mutex_lock(&dev_pm_qos_mtx);
161
162 dev->power.power_state = PMSG_INVALID;
163 c = dev->power.constraints;
164 if (!c)
165 goto out;
166
167 /* Flush the constraints list for the device */
168 plist_for_each_entry_safe(req, tmp, &c->list, node) {
169 /*
170 * Update constraints list and call the notification
171 * callbacks if needed
172 */
173 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
174 memset(req, 0, sizeof(*req));
175 }
176
177 spin_lock_irq(&dev->power.lock);
178 dev->power.constraints = NULL;
179 spin_unlock_irq(&dev->power.lock);
180
181 kfree(c->notifiers);
182 kfree(c);
183
184 out:
185 mutex_unlock(&dev_pm_qos_mtx);
186}
187
188/**
189 * dev_pm_qos_add_request - inserts new qos request into the list
190 * @dev: target device for the constraint
191 * @req: pointer to a preallocated handle
192 * @value: defines the qos request
193 *
194 * This function inserts a new entry in the device constraints list of
195 * requested qos performance characteristics. It recomputes the aggregate
196 * QoS expectations of parameters and initializes the dev_pm_qos_request
197 * handle. Caller needs to save this handle for later use in updates and
198 * removal.
199 *
200 * Returns 1 if the aggregated constraint value has changed,
201 * 0 if the aggregated constraint value has not changed,
202 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
203 * to allocate for data structures, -ENODEV if the device has just been removed
204 * from the system.
205 */
206int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
207 s32 value)
208{
209 int ret = 0;
210
211 if (!dev || !req) /*guard against callers passing in null */
212 return -EINVAL;
213
214 if (dev_pm_qos_request_active(req)) {
215 WARN(1, KERN_ERR "dev_pm_qos_add_request() called for already "
216 "added request\n");
217 return -EINVAL;
218 }
219
220 req->dev = dev;
221
222 mutex_lock(&dev_pm_qos_mtx);
223
224 if (!dev->power.constraints) {
225 if (dev->power.power_state.event == PM_EVENT_INVALID) {
226 /* The device has been removed from the system. */
227 req->dev = NULL;
228 ret = -ENODEV;
229 goto out;
230 } else {
231 /*
232 * Allocate the constraints data on the first call to
233 * add_request, i.e. only if the data is not already
234 * allocated and if the device has not been removed.
235 */
236 ret = dev_pm_qos_constraints_allocate(dev);
237 }
238 }
239
240 if (!ret)
241 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
242
243 out:
244 mutex_unlock(&dev_pm_qos_mtx);
245
246 return ret;
247}
248EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
249
250/**
251 * dev_pm_qos_update_request - modifies an existing qos request
252 * @req : handle to list element holding a dev_pm_qos request to use
253 * @new_value: defines the qos request
254 *
255 * Updates an existing dev PM qos request along with updating the
256 * target value.
257 *
258 * Attempts are made to make this code callable on hot code paths.
259 *
260 * Returns 1 if the aggregated constraint value has changed,
261 * 0 if the aggregated constraint value has not changed,
262 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
263 * removed from the system
264 */
265int dev_pm_qos_update_request(struct dev_pm_qos_request *req,
266 s32 new_value)
267{
268 int ret = 0;
269
270 if (!req) /*guard against callers passing in null */
271 return -EINVAL;
272
273 if (!dev_pm_qos_request_active(req)) {
274 WARN(1, KERN_ERR "dev_pm_qos_update_request() called for "
275 "unknown object\n");
276 return -EINVAL;
277 }
278
279 mutex_lock(&dev_pm_qos_mtx);
280
281 if (req->dev->power.constraints) {
282 if (new_value != req->node.prio)
283 ret = apply_constraint(req, PM_QOS_UPDATE_REQ,
284 new_value);
285 } else {
286 /* Return if the device has been removed */
287 ret = -ENODEV;
288 }
289
290 mutex_unlock(&dev_pm_qos_mtx);
291 return ret;
292}
293EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
294
295/**
296 * dev_pm_qos_remove_request - modifies an existing qos request
297 * @req: handle to request list element
298 *
299 * Will remove pm qos request from the list of constraints and
300 * recompute the current target value. Call this on slow code paths.
301 *
302 * Returns 1 if the aggregated constraint value has changed,
303 * 0 if the aggregated constraint value has not changed,
304 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
305 * removed from the system
306 */
307int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
308{
309 int ret = 0;
310
311 if (!req) /*guard against callers passing in null */
312 return -EINVAL;
313
314 if (!dev_pm_qos_request_active(req)) {
315 WARN(1, KERN_ERR "dev_pm_qos_remove_request() called for "
316 "unknown object\n");
317 return -EINVAL;
318 }
319
320 mutex_lock(&dev_pm_qos_mtx);
321
322 if (req->dev->power.constraints) {
323 ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
324 PM_QOS_DEFAULT_VALUE);
325 memset(req, 0, sizeof(*req));
326 } else {
327 /* Return if the device has been removed */
328 ret = -ENODEV;
329 }
330
331 mutex_unlock(&dev_pm_qos_mtx);
332 return ret;
333}
334EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
335
336/**
337 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
338 * of per-device PM QoS constraints
339 *
340 * @dev: target device for the constraint
341 * @notifier: notifier block managed by caller.
342 *
343 * Will register the notifier into a notification chain that gets called
344 * upon changes to the target value for the device.
345 */
346int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
347{
348 int retval = 0;
349
350 mutex_lock(&dev_pm_qos_mtx);
351
352 /* Silently return if the constraints object is not present. */
353 if (dev->power.constraints)
354 retval = blocking_notifier_chain_register(
355 dev->power.constraints->notifiers,
356 notifier);
357
358 mutex_unlock(&dev_pm_qos_mtx);
359 return retval;
360}
361EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
362
363/**
364 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
365 * of per-device PM QoS constraints
366 *
367 * @dev: target device for the constraint
368 * @notifier: notifier block to be removed.
369 *
370 * Will remove the notifier from the notification chain that gets called
371 * upon changes to the target value.
372 */
373int dev_pm_qos_remove_notifier(struct device *dev,
374 struct notifier_block *notifier)
375{
376 int retval = 0;
377
378 mutex_lock(&dev_pm_qos_mtx);
379
380 /* Silently return if the constraints object is not present. */
381 if (dev->power.constraints)
382 retval = blocking_notifier_chain_unregister(
383 dev->power.constraints->notifiers,
384 notifier);
385
386 mutex_unlock(&dev_pm_qos_mtx);
387 return retval;
388}
389EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
390
391/**
392 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
393 * target value of the PM QoS constraints for any device
394 *
395 * @notifier: notifier block managed by caller.
396 *
397 * Will register the notifier into a notification chain that gets called
398 * upon changes to the target value for any device.
399 */
400int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
401{
402 return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
403}
404EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
405
406/**
407 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
408 * target value of PM QoS constraints for any device
409 *
410 * @notifier: notifier block to be removed.
411 *
412 * Will remove the notifier from the notification chain that gets called
413 * upon changes to the target value for any device.
414 */
415int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
416{
417 return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
418}
419EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);