usb: Netlogic: Use CPU_XLR in place of NLM_XLR
[zen-stable.git] / drivers / base / power / qos.c
blob30a94eadc200c7f89f7e2448126ad93cd6a6a2f2
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 * . A global mutex protects the constraints users from the data being
34 * allocated and free'd.
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 #include <linux/export.h>
45 static DEFINE_MUTEX(dev_pm_qos_mtx);
47 static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
49 /**
50 * dev_pm_qos_read_value - Get PM QoS constraint for a given device.
51 * @dev: Device to get the PM QoS constraint value for.
53 s32 dev_pm_qos_read_value(struct device *dev)
55 struct pm_qos_constraints *c;
56 unsigned long flags;
57 s32 ret = 0;
59 spin_lock_irqsave(&dev->power.lock, flags);
61 c = dev->power.constraints;
62 if (c)
63 ret = pm_qos_read_value(c);
65 spin_unlock_irqrestore(&dev->power.lock, flags);
67 return ret;
71 * apply_constraint
72 * @req: constraint request to apply
73 * @action: action to perform add/update/remove, of type enum pm_qos_req_action
74 * @value: defines the qos request
76 * Internal function to update the constraints list using the PM QoS core
77 * code and if needed call the per-device and the global notification
78 * callbacks
80 static int apply_constraint(struct dev_pm_qos_request *req,
81 enum pm_qos_req_action action, int value)
83 int ret, curr_value;
85 ret = pm_qos_update_target(req->dev->power.constraints,
86 &req->node, action, value);
88 if (ret) {
89 /* Call the global callbacks if needed */
90 curr_value = pm_qos_read_value(req->dev->power.constraints);
91 blocking_notifier_call_chain(&dev_pm_notifiers,
92 (unsigned long)curr_value,
93 req);
96 return ret;
100 * dev_pm_qos_constraints_allocate
101 * @dev: device to allocate data for
103 * Called at the first call to add_request, for constraint data allocation
104 * Must be called with the dev_pm_qos_mtx mutex held
106 static int dev_pm_qos_constraints_allocate(struct device *dev)
108 struct pm_qos_constraints *c;
109 struct blocking_notifier_head *n;
111 c = kzalloc(sizeof(*c), GFP_KERNEL);
112 if (!c)
113 return -ENOMEM;
115 n = kzalloc(sizeof(*n), GFP_KERNEL);
116 if (!n) {
117 kfree(c);
118 return -ENOMEM;
120 BLOCKING_INIT_NOTIFIER_HEAD(n);
122 plist_head_init(&c->list);
123 c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
124 c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
125 c->type = PM_QOS_MIN;
126 c->notifiers = n;
128 spin_lock_irq(&dev->power.lock);
129 dev->power.constraints = c;
130 spin_unlock_irq(&dev->power.lock);
132 return 0;
136 * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer.
137 * @dev: target device
139 * Called from the device PM subsystem during device insertion under
140 * device_pm_lock().
142 void dev_pm_qos_constraints_init(struct device *dev)
144 mutex_lock(&dev_pm_qos_mtx);
145 dev->power.constraints = NULL;
146 dev->power.power_state = PMSG_ON;
147 mutex_unlock(&dev_pm_qos_mtx);
151 * dev_pm_qos_constraints_destroy
152 * @dev: target device
154 * Called from the device PM subsystem on device removal under device_pm_lock().
156 void dev_pm_qos_constraints_destroy(struct device *dev)
158 struct dev_pm_qos_request *req, *tmp;
159 struct pm_qos_constraints *c;
161 mutex_lock(&dev_pm_qos_mtx);
163 dev->power.power_state = PMSG_INVALID;
164 c = dev->power.constraints;
165 if (!c)
166 goto out;
168 /* Flush the constraints list for the device */
169 plist_for_each_entry_safe(req, tmp, &c->list, node) {
171 * Update constraints list and call the notification
172 * callbacks if needed
174 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
175 memset(req, 0, sizeof(*req));
178 spin_lock_irq(&dev->power.lock);
179 dev->power.constraints = NULL;
180 spin_unlock_irq(&dev->power.lock);
182 kfree(c->notifiers);
183 kfree(c);
185 out:
186 mutex_unlock(&dev_pm_qos_mtx);
190 * dev_pm_qos_add_request - inserts new qos request into the list
191 * @dev: target device for the constraint
192 * @req: pointer to a preallocated handle
193 * @value: defines the qos request
195 * This function inserts a new entry in the device constraints list of
196 * requested qos performance characteristics. It recomputes the aggregate
197 * QoS expectations of parameters and initializes the dev_pm_qos_request
198 * handle. Caller needs to save this handle for later use in updates and
199 * removal.
201 * Returns 1 if the aggregated constraint value has changed,
202 * 0 if the aggregated constraint value has not changed,
203 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
204 * to allocate for data structures, -ENODEV if the device has just been removed
205 * from the system.
207 int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
208 s32 value)
210 int ret = 0;
212 if (!dev || !req) /*guard against callers passing in null */
213 return -EINVAL;
215 if (dev_pm_qos_request_active(req)) {
216 WARN(1, KERN_ERR "dev_pm_qos_add_request() called for already "
217 "added request\n");
218 return -EINVAL;
221 req->dev = dev;
223 mutex_lock(&dev_pm_qos_mtx);
225 if (!dev->power.constraints) {
226 if (dev->power.power_state.event == PM_EVENT_INVALID) {
227 /* The device has been removed from the system. */
228 req->dev = NULL;
229 ret = -ENODEV;
230 goto out;
231 } else {
233 * Allocate the constraints data on the first call to
234 * add_request, i.e. only if the data is not already
235 * allocated and if the device has not been removed.
237 ret = dev_pm_qos_constraints_allocate(dev);
241 if (!ret)
242 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
244 out:
245 mutex_unlock(&dev_pm_qos_mtx);
247 return ret;
249 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
252 * dev_pm_qos_update_request - modifies an existing qos request
253 * @req : handle to list element holding a dev_pm_qos request to use
254 * @new_value: defines the qos request
256 * Updates an existing dev PM qos request along with updating the
257 * target value.
259 * Attempts are made to make this code callable on hot code paths.
261 * Returns 1 if the aggregated constraint value has changed,
262 * 0 if the aggregated constraint value has not changed,
263 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
264 * removed from the system
266 int dev_pm_qos_update_request(struct dev_pm_qos_request *req,
267 s32 new_value)
269 int ret = 0;
271 if (!req) /*guard against callers passing in null */
272 return -EINVAL;
274 if (!dev_pm_qos_request_active(req)) {
275 WARN(1, KERN_ERR "dev_pm_qos_update_request() called for "
276 "unknown object\n");
277 return -EINVAL;
280 mutex_lock(&dev_pm_qos_mtx);
282 if (req->dev->power.constraints) {
283 if (new_value != req->node.prio)
284 ret = apply_constraint(req, PM_QOS_UPDATE_REQ,
285 new_value);
286 } else {
287 /* Return if the device has been removed */
288 ret = -ENODEV;
291 mutex_unlock(&dev_pm_qos_mtx);
292 return ret;
294 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
297 * dev_pm_qos_remove_request - modifies an existing qos request
298 * @req: handle to request list element
300 * Will remove pm qos request from the list of constraints and
301 * recompute the current target value. Call this on slow code paths.
303 * Returns 1 if the aggregated constraint value has changed,
304 * 0 if the aggregated constraint value has not changed,
305 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
306 * removed from the system
308 int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
310 int ret = 0;
312 if (!req) /*guard against callers passing in null */
313 return -EINVAL;
315 if (!dev_pm_qos_request_active(req)) {
316 WARN(1, KERN_ERR "dev_pm_qos_remove_request() called for "
317 "unknown object\n");
318 return -EINVAL;
321 mutex_lock(&dev_pm_qos_mtx);
323 if (req->dev->power.constraints) {
324 ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
325 PM_QOS_DEFAULT_VALUE);
326 memset(req, 0, sizeof(*req));
327 } else {
328 /* Return if the device has been removed */
329 ret = -ENODEV;
332 mutex_unlock(&dev_pm_qos_mtx);
333 return ret;
335 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
338 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
339 * of per-device PM QoS constraints
341 * @dev: target device for the constraint
342 * @notifier: notifier block managed by caller.
344 * Will register the notifier into a notification chain that gets called
345 * upon changes to the target value for the device.
347 int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
349 int retval = 0;
351 mutex_lock(&dev_pm_qos_mtx);
353 /* Silently return if the constraints object is not present. */
354 if (dev->power.constraints)
355 retval = blocking_notifier_chain_register(
356 dev->power.constraints->notifiers,
357 notifier);
359 mutex_unlock(&dev_pm_qos_mtx);
360 return retval;
362 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
365 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
366 * of per-device PM QoS constraints
368 * @dev: target device for the constraint
369 * @notifier: notifier block to be removed.
371 * Will remove the notifier from the notification chain that gets called
372 * upon changes to the target value.
374 int dev_pm_qos_remove_notifier(struct device *dev,
375 struct notifier_block *notifier)
377 int retval = 0;
379 mutex_lock(&dev_pm_qos_mtx);
381 /* Silently return if the constraints object is not present. */
382 if (dev->power.constraints)
383 retval = blocking_notifier_chain_unregister(
384 dev->power.constraints->notifiers,
385 notifier);
387 mutex_unlock(&dev_pm_qos_mtx);
388 return retval;
390 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
393 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
394 * target value of the PM QoS constraints for any device
396 * @notifier: notifier block managed by caller.
398 * Will register the notifier into a notification chain that gets called
399 * upon changes to the target value for any device.
401 int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
403 return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
405 EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
408 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
409 * target value of PM QoS constraints for any device
411 * @notifier: notifier block to be removed.
413 * Will remove the notifier from the notification chain that gets called
414 * upon changes to the target value for any device.
416 int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
418 return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
420 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);