Merge remote-tracking branch 'pm/linux-next'
[linux-2.6/next.git] / drivers / base / power / qos.c
blob8d0b81151c14bb6a287a976272008a077741169f
1 /*
2 * Devices PM QoS constraints management
4 * Copyright (C) 2011 Texas Instruments, Inc.
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.
11 * This module exposes the interface to kernel space for specifying
12 * per-device PM QoS dependencies. It provides infrastructure for registration
13 * of:
15 * Dependents on a QoS value : register requests
16 * Watchers of QoS value : get notified when target QoS value changes
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.
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 * . The constraints_state variable from dev_pm_info tracks the data struct
34 * allocation state:
35 * DEV_PM_QOS_NO_DEVICE: No device present or device removed, no data
36 * allocated,
37 * DEV_PM_QOS_DEVICE_PRESENT: Device present, data not allocated and will be
38 * allocated at the first call to dev_pm_qos_add_request,
39 * DEV_PM_QOS_ALLOCATED: Device present, data allocated. The per-device
40 * PM QoS constraints framework is operational and constraints can be
41 * added, updated or removed using the dev_pm_qos_* API.
42 * . A global mutex protects the constraints users from the data being
43 * allocated and free'd.
46 #include <linux/pm_qos.h>
47 #include <linux/spinlock.h>
48 #include <linux/slab.h>
49 #include <linux/device.h>
50 #include <linux/mutex.h>
53 static DEFINE_MUTEX(dev_pm_qos_mtx);
54 static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
57 * apply_constraint
58 * @req: constraint request to apply
59 * @action: action to perform add/update/remove, of type enum pm_qos_req_action
60 * @value: defines the qos request
62 * Internal function to update the constraints list using the PM QoS core
63 * code and if needed call the per-device and the global notification
64 * callbacks
66 static int apply_constraint(struct dev_pm_qos_request *req,
67 enum pm_qos_req_action action, int value)
69 int ret, curr_value;
71 ret = pm_qos_update_target(req->dev->power.constraints,
72 &req->node, action, value);
74 if (ret) {
75 /* Call the global callbacks if needed */
76 curr_value = pm_qos_read_value(req->dev->power.constraints);
77 blocking_notifier_call_chain(&dev_pm_notifiers,
78 (unsigned long)curr_value,
79 req);
82 return ret;
86 * dev_pm_qos_constraints_allocate
87 * @dev: device to allocate data for
89 * Called at the first call to add_request, for constraint data allocation
90 * Must be called with the dev_pm_qos_mtx mutex held
92 static int dev_pm_qos_constraints_allocate(struct device *dev)
94 struct pm_qos_constraints *c;
95 struct blocking_notifier_head *n;
97 c = kzalloc(sizeof(*c), GFP_KERNEL);
98 if (!c)
99 return -ENOMEM;
101 n = kzalloc(sizeof(*n), GFP_KERNEL);
102 if (!n) {
103 kfree(c);
104 return -ENOMEM;
106 BLOCKING_INIT_NOTIFIER_HEAD(n);
108 dev->power.constraints = c;
109 plist_head_init(&dev->power.constraints->list);
110 dev->power.constraints->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
111 dev->power.constraints->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
112 dev->power.constraints->type = PM_QOS_MIN;
113 dev->power.constraints->notifiers = n;
114 dev->power.constraints_state = DEV_PM_QOS_ALLOCATED;
116 return 0;
120 * dev_pm_qos_constraints_init
121 * @dev: target device
123 * Called from the device PM subsystem at device insertion
125 void dev_pm_qos_constraints_init(struct device *dev)
127 mutex_lock(&dev_pm_qos_mtx);
128 dev->power.constraints_state = DEV_PM_QOS_DEVICE_PRESENT;
129 mutex_unlock(&dev_pm_qos_mtx);
133 * dev_pm_qos_constraints_destroy
134 * @dev: target device
136 * Called from the device PM subsystem at device removal
138 void dev_pm_qos_constraints_destroy(struct device *dev)
140 struct dev_pm_qos_request *req, *tmp;
142 mutex_lock(&dev_pm_qos_mtx);
144 if (dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) {
145 /* Flush the constraints list for the device */
146 plist_for_each_entry_safe(req, tmp,
147 &dev->power.constraints->list,
148 node) {
150 * Update constraints list and call the notification
151 * callbacks if needed
153 apply_constraint(req, PM_QOS_REMOVE_REQ,
154 PM_QOS_DEFAULT_VALUE);
155 memset(req, 0, sizeof(*req));
158 kfree(dev->power.constraints->notifiers);
159 kfree(dev->power.constraints);
160 dev->power.constraints = NULL;
162 dev->power.constraints_state = DEV_PM_QOS_NO_DEVICE;
164 mutex_unlock(&dev_pm_qos_mtx);
168 * dev_pm_qos_add_request - inserts new qos request into the list
169 * @dev: target device for the constraint
170 * @req: pointer to a preallocated handle
171 * @value: defines the qos request
173 * This function inserts a new entry in the device constraints list of
174 * requested qos performance characteristics. It recomputes the aggregate
175 * QoS expectations of parameters and initializes the dev_pm_qos_request
176 * handle. Caller needs to save this handle for later use in updates and
177 * removal.
179 * Returns 1 if the aggregated constraint value has changed,
180 * 0 if the aggregated constraint value has not changed,
181 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
182 * removed from the system
184 int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
185 s32 value)
187 int ret = 0;
189 if (!dev || !req) /*guard against callers passing in null */
190 return -EINVAL;
192 if (dev_pm_qos_request_active(req)) {
193 WARN(1, KERN_ERR "dev_pm_qos_add_request() called for already "
194 "added request\n");
195 return -EINVAL;
198 mutex_lock(&dev_pm_qos_mtx);
199 req->dev = dev;
201 /* Return if the device has been removed */
202 if (req->dev->power.constraints_state == DEV_PM_QOS_NO_DEVICE) {
203 ret = -ENODEV;
204 goto out;
208 * Allocate the constraints data on the first call to add_request,
209 * i.e. only if the data is not already allocated and if the device has
210 * not been removed
212 if (dev->power.constraints_state == DEV_PM_QOS_DEVICE_PRESENT)
213 ret = dev_pm_qos_constraints_allocate(dev);
215 if (!ret)
216 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
218 out:
219 mutex_unlock(&dev_pm_qos_mtx);
220 return ret;
222 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
225 * dev_pm_qos_update_request - modifies an existing qos request
226 * @req : handle to list element holding a dev_pm_qos request to use
227 * @new_value: defines the qos request
229 * Updates an existing dev PM qos request along with updating the
230 * target value.
232 * Attempts are made to make this code callable on hot code paths.
234 * Returns 1 if the aggregated constraint value has changed,
235 * 0 if the aggregated constraint value has not changed,
236 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
237 * removed from the system
239 int dev_pm_qos_update_request(struct dev_pm_qos_request *req,
240 s32 new_value)
242 int ret = 0;
244 if (!req) /*guard against callers passing in null */
245 return -EINVAL;
247 if (!dev_pm_qos_request_active(req)) {
248 WARN(1, KERN_ERR "dev_pm_qos_update_request() called for "
249 "unknown object\n");
250 return -EINVAL;
253 mutex_lock(&dev_pm_qos_mtx);
255 if (req->dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) {
256 if (new_value != req->node.prio)
257 ret = apply_constraint(req, PM_QOS_UPDATE_REQ,
258 new_value);
259 } else {
260 /* Return if the device has been removed */
261 ret = -ENODEV;
264 mutex_unlock(&dev_pm_qos_mtx);
265 return ret;
267 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
270 * dev_pm_qos_remove_request - modifies an existing qos request
271 * @req: handle to request list element
273 * Will remove pm qos request from the list of constraints and
274 * recompute the current target value. Call this on slow code paths.
276 * Returns 1 if the aggregated constraint value has changed,
277 * 0 if the aggregated constraint value has not changed,
278 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
279 * removed from the system
281 int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
283 int ret = 0;
285 if (!req) /*guard against callers passing in null */
286 return -EINVAL;
288 if (!dev_pm_qos_request_active(req)) {
289 WARN(1, KERN_ERR "dev_pm_qos_remove_request() called for "
290 "unknown object\n");
291 return -EINVAL;
294 mutex_lock(&dev_pm_qos_mtx);
296 if (req->dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) {
297 ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
298 PM_QOS_DEFAULT_VALUE);
299 memset(req, 0, sizeof(*req));
300 } else {
301 /* Return if the device has been removed */
302 ret = -ENODEV;
305 mutex_unlock(&dev_pm_qos_mtx);
306 return ret;
308 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
311 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
312 * of per-device PM QoS constraints
314 * @dev: target device for the constraint
315 * @notifier: notifier block managed by caller.
317 * Will register the notifier into a notification chain that gets called
318 * upon changes to the target value for the device.
320 int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
322 int retval = 0;
324 mutex_lock(&dev_pm_qos_mtx);
326 /* Silently return if the device has been removed */
327 if (dev->power.constraints_state != DEV_PM_QOS_ALLOCATED)
328 goto out;
330 retval = blocking_notifier_chain_register(
331 dev->power.constraints->notifiers,
332 notifier);
334 out:
335 mutex_unlock(&dev_pm_qos_mtx);
336 return retval;
338 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
341 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
342 * of per-device PM QoS constraints
344 * @dev: target device for the constraint
345 * @notifier: notifier block to be removed.
347 * Will remove the notifier from the notification chain that gets called
348 * upon changes to the target value.
350 int dev_pm_qos_remove_notifier(struct device *dev,
351 struct notifier_block *notifier)
353 int retval = 0;
355 mutex_lock(&dev_pm_qos_mtx);
357 /* Silently return if the device has been removed */
358 if (dev->power.constraints_state != DEV_PM_QOS_ALLOCATED)
359 goto out;
361 retval = blocking_notifier_chain_unregister(
362 dev->power.constraints->notifiers,
363 notifier);
365 out:
366 mutex_unlock(&dev_pm_qos_mtx);
367 return retval;
369 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
372 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
373 * target value of the PM QoS constraints for any device
375 * @notifier: notifier block managed by caller.
377 * Will register the notifier into a notification chain that gets called
378 * upon changes to the target value for any device.
380 int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
382 return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
384 EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
387 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
388 * target value of PM QoS constraints for any device
390 * @notifier: notifier block to be removed.
392 * Will remove the notifier from the notification chain that gets called
393 * upon changes to the target value for any device.
395 int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
397 return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
399 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);