Revert "PM QoS: Add global notification mechanism for device constraints"
[linux-2.6/next.git] / drivers / base / power / qos.c
blobcc4c541398aa39120b6654296dac5f83b6cd0a75
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.
21 * Note about the per-device constraint data struct allocation:
22 * . The per-device constraints data struct ptr is tored into the device
23 * dev_pm_info.
24 * . To minimize the data usage by the per-device constraints, the data struct
25 * is only allocated at the first call to dev_pm_qos_add_request.
26 * . The data is later free'd when the device is removed from the system.
27 * . The constraints_state variable from dev_pm_info tracks the data struct
28 * allocation state:
29 * DEV_PM_QOS_NO_DEVICE: No device present or device removed, no data
30 * allocated,
31 * DEV_PM_QOS_DEVICE_PRESENT: Device present, data not allocated and will be
32 * allocated at the first call to dev_pm_qos_add_request,
33 * DEV_PM_QOS_ALLOCATED: Device present, data allocated. The per-device
34 * PM QoS constraints framework is operational and constraints can be
35 * added, updated or removed using the dev_pm_qos_* API.
36 * . A global mutex protects the constraints users from the data being
37 * allocated and free'd.
40 #include <linux/pm_qos.h>
41 #include <linux/spinlock.h>
42 #include <linux/slab.h>
43 #include <linux/device.h>
44 #include <linux/mutex.h>
47 static DEFINE_MUTEX(dev_pm_qos_mtx);
50 * dev_pm_qos_constraints_allocate
51 * @dev: device to allocate data for
53 * Called at the first call to add_request, for constraint data allocation
54 * Must be called with the dev_pm_qos_mtx mutex held
56 static int dev_pm_qos_constraints_allocate(struct device *dev)
58 struct pm_qos_constraints *c;
59 struct blocking_notifier_head *n;
61 c = kzalloc(sizeof(*c), GFP_KERNEL);
62 if (!c)
63 return -ENOMEM;
65 n = kzalloc(sizeof(*n), GFP_KERNEL);
66 if (!n) {
67 kfree(c);
68 return -ENOMEM;
70 BLOCKING_INIT_NOTIFIER_HEAD(n);
72 dev->power.constraints = c;
73 plist_head_init(&dev->power.constraints->list);
74 dev->power.constraints->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
75 dev->power.constraints->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
76 dev->power.constraints->type = PM_QOS_MIN;
77 dev->power.constraints->notifiers = n;
78 dev->power.constraints_state = DEV_PM_QOS_ALLOCATED;
80 return 0;
83 /**
84 * dev_pm_qos_constraints_init
85 * @dev: target device
87 * Called from the device PM subsystem at device insertion
89 void dev_pm_qos_constraints_init(struct device *dev)
91 mutex_lock(&dev_pm_qos_mtx);
92 dev->power.constraints_state = DEV_PM_QOS_DEVICE_PRESENT;
93 mutex_unlock(&dev_pm_qos_mtx);
96 /**
97 * dev_pm_qos_constraints_destroy
98 * @dev: target device
100 * Called from the device PM subsystem at device removal
102 void dev_pm_qos_constraints_destroy(struct device *dev)
104 struct dev_pm_qos_request *req, *tmp;
106 mutex_lock(&dev_pm_qos_mtx);
108 if (dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) {
109 /* Flush the constraints list for the device */
110 plist_for_each_entry_safe(req, tmp,
111 &dev->power.constraints->list,
112 node) {
114 * Update constraints list and call the per-device
115 * callbacks if needed
117 pm_qos_update_target(req->dev->power.constraints,
118 &req->node, PM_QOS_REMOVE_REQ,
119 PM_QOS_DEFAULT_VALUE);
120 memset(req, 0, sizeof(*req));
123 kfree(dev->power.constraints->notifiers);
124 kfree(dev->power.constraints);
125 dev->power.constraints = NULL;
127 dev->power.constraints_state = DEV_PM_QOS_NO_DEVICE;
129 mutex_unlock(&dev_pm_qos_mtx);
133 * dev_pm_qos_add_request - inserts new qos request into the list
134 * @dev: target device for the constraint
135 * @req: pointer to a preallocated handle
136 * @value: defines the qos request
138 * This function inserts a new entry in the device constraints list of
139 * requested qos performance characteristics. It recomputes the aggregate
140 * QoS expectations of parameters and initializes the dev_pm_qos_request
141 * handle. Caller needs to save this handle for later use in updates and
142 * removal.
144 * Returns 1 if the aggregated constraint value has changed,
145 * 0 if the aggregated constraint value has not changed,
146 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
147 * removed from the system
149 int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
150 s32 value)
152 int ret = 0;
154 if (!dev || !req) /*guard against callers passing in null */
155 return -EINVAL;
157 if (dev_pm_qos_request_active(req)) {
158 WARN(1, KERN_ERR "dev_pm_qos_add_request() called for already "
159 "added request\n");
160 return -EINVAL;
163 mutex_lock(&dev_pm_qos_mtx);
164 req->dev = dev;
166 /* Return if the device has been removed */
167 if (req->dev->power.constraints_state == DEV_PM_QOS_NO_DEVICE) {
168 ret = -ENODEV;
169 goto out;
173 * Allocate the constraints data on the first call to add_request,
174 * i.e. only if the data is not already allocated and if the device has
175 * not been removed
177 if (dev->power.constraints_state == DEV_PM_QOS_DEVICE_PRESENT)
178 ret = dev_pm_qos_constraints_allocate(dev);
180 if (!ret)
181 ret = pm_qos_update_target(dev->power.constraints, &req->node,
182 PM_QOS_ADD_REQ, value);
184 out:
185 mutex_unlock(&dev_pm_qos_mtx);
186 return ret;
188 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
191 * dev_pm_qos_update_request - modifies an existing qos request
192 * @req : handle to list element holding a dev_pm_qos request to use
193 * @new_value: defines the qos request
195 * Updates an existing dev PM qos request along with updating the
196 * target value.
198 * Attempts are made to make this code callable on hot code paths.
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, -ENODEV if the device has been
203 * removed from the system
205 int dev_pm_qos_update_request(struct dev_pm_qos_request *req,
206 s32 new_value)
208 int ret = 0;
210 if (!req) /*guard against callers passing in null */
211 return -EINVAL;
213 if (!dev_pm_qos_request_active(req)) {
214 WARN(1, KERN_ERR "dev_pm_qos_update_request() called for "
215 "unknown object\n");
216 return -EINVAL;
219 mutex_lock(&dev_pm_qos_mtx);
221 if (req->dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) {
222 if (new_value != req->node.prio)
223 ret = pm_qos_update_target(req->dev->power.constraints,
224 &req->node,
225 PM_QOS_UPDATE_REQ,
226 new_value);
227 } else {
228 /* Return if the device has been removed */
229 ret = -ENODEV;
232 mutex_unlock(&dev_pm_qos_mtx);
233 return ret;
235 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
238 * dev_pm_qos_remove_request - modifies an existing qos request
239 * @req: handle to request list element
241 * Will remove pm qos request from the list of constraints and
242 * recompute the current target value. Call this on slow code paths.
244 * Returns 1 if the aggregated constraint value has changed,
245 * 0 if the aggregated constraint value has not changed,
246 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
247 * removed from the system
249 int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
251 int ret = 0;
253 if (!req) /*guard against callers passing in null */
254 return -EINVAL;
256 if (!dev_pm_qos_request_active(req)) {
257 WARN(1, KERN_ERR "dev_pm_qos_remove_request() called for "
258 "unknown object\n");
259 return -EINVAL;
262 mutex_lock(&dev_pm_qos_mtx);
264 if (req->dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) {
265 ret = pm_qos_update_target(req->dev->power.constraints,
266 &req->node, PM_QOS_REMOVE_REQ,
267 PM_QOS_DEFAULT_VALUE);
268 memset(req, 0, sizeof(*req));
269 } else {
270 /* Return if the device has been removed */
271 ret = -ENODEV;
274 mutex_unlock(&dev_pm_qos_mtx);
275 return ret;
277 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
280 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
281 * of per-device PM QoS constraints
283 * @dev: target device for the constraint
284 * @notifier: notifier block managed by caller.
286 * Will register the notifier into a notification chain that gets called
287 * upon changes to the target value for the device.
289 int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
291 int retval = 0;
293 mutex_lock(&dev_pm_qos_mtx);
295 /* Silently return if the device has been removed */
296 if (dev->power.constraints_state != DEV_PM_QOS_ALLOCATED)
297 goto out;
299 retval = blocking_notifier_chain_register(
300 dev->power.constraints->notifiers,
301 notifier);
303 out:
304 mutex_unlock(&dev_pm_qos_mtx);
305 return retval;
307 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
310 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
311 * of per-device PM QoS constraints
313 * @dev: target device for the constraint
314 * @notifier: notifier block to be removed.
316 * Will remove the notifier from the notification chain that gets called
317 * upon changes to the target value.
319 int dev_pm_qos_remove_notifier(struct device *dev,
320 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_unregister(
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_remove_notifier);