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
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
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
29 * DEV_PM_QOS_NO_DEVICE: No device present or device removed, no data
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
);
65 n
= kzalloc(sizeof(*n
), GFP_KERNEL
);
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
;
84 * dev_pm_qos_constraints_init
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
);
97 * dev_pm_qos_constraints_destroy
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
,
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
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
,
154 if (!dev
|| !req
) /*guard against callers passing in null */
157 if (dev_pm_qos_request_active(req
)) {
158 WARN(1, KERN_ERR
"dev_pm_qos_add_request() called for already "
163 mutex_lock(&dev_pm_qos_mtx
);
166 /* Return if the device has been removed */
167 if (req
->dev
->power
.constraints_state
== DEV_PM_QOS_NO_DEVICE
) {
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
177 if (dev
->power
.constraints_state
== DEV_PM_QOS_DEVICE_PRESENT
)
178 ret
= dev_pm_qos_constraints_allocate(dev
);
181 ret
= pm_qos_update_target(dev
->power
.constraints
, &req
->node
,
182 PM_QOS_ADD_REQ
, value
);
185 mutex_unlock(&dev_pm_qos_mtx
);
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
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
,
210 if (!req
) /*guard against callers passing in null */
213 if (!dev_pm_qos_request_active(req
)) {
214 WARN(1, KERN_ERR
"dev_pm_qos_update_request() called for "
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
,
228 /* Return if the device has been removed */
232 mutex_unlock(&dev_pm_qos_mtx
);
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
)
253 if (!req
) /*guard against callers passing in null */
256 if (!dev_pm_qos_request_active(req
)) {
257 WARN(1, KERN_ERR
"dev_pm_qos_remove_request() called for "
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
));
270 /* Return if the device has been removed */
274 mutex_unlock(&dev_pm_qos_mtx
);
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
)
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
)
299 retval
= blocking_notifier_chain_register(
300 dev
->power
.constraints
->notifiers
,
304 mutex_unlock(&dev_pm_qos_mtx
);
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
)
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
)
330 retval
= blocking_notifier_chain_unregister(
331 dev
->power
.constraints
->notifiers
,
335 mutex_unlock(&dev_pm_qos_mtx
);
338 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier
);