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.
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
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
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>
44 static DEFINE_MUTEX(dev_pm_qos_mtx
);
46 static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers
);
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.
52 s32
dev_pm_qos_read_value(struct device
*dev
)
54 struct pm_qos_constraints
*c
;
58 spin_lock_irqsave(&dev
->power
.lock
, flags
);
60 c
= dev
->power
.constraints
;
62 ret
= pm_qos_read_value(c
);
64 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
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
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
79 static int apply_constraint(struct dev_pm_qos_request
*req
,
80 enum pm_qos_req_action action
, int value
)
84 ret
= pm_qos_update_target(req
->dev
->power
.constraints
,
85 &req
->node
, action
, value
);
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
,
99 * dev_pm_qos_constraints_allocate
100 * @dev: device to allocate data for
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
105 static int dev_pm_qos_constraints_allocate(struct device
*dev
)
107 struct pm_qos_constraints
*c
;
108 struct blocking_notifier_head
*n
;
110 c
= kzalloc(sizeof(*c
), GFP_KERNEL
);
114 n
= kzalloc(sizeof(*n
), GFP_KERNEL
);
119 BLOCKING_INIT_NOTIFIER_HEAD(n
);
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
;
127 spin_lock_irq(&dev
->power
.lock
);
128 dev
->power
.constraints
= c
;
129 spin_unlock_irq(&dev
->power
.lock
);
135 * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer.
136 * @dev: target device
138 * Called from the device PM subsystem during device insertion under
141 void dev_pm_qos_constraints_init(struct device
*dev
)
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
);
150 * dev_pm_qos_constraints_destroy
151 * @dev: target device
153 * Called from the device PM subsystem on device removal under device_pm_lock().
155 void dev_pm_qos_constraints_destroy(struct device
*dev
)
157 struct dev_pm_qos_request
*req
, *tmp
;
158 struct pm_qos_constraints
*c
;
160 mutex_lock(&dev_pm_qos_mtx
);
162 dev
->power
.power_state
= PMSG_INVALID
;
163 c
= dev
->power
.constraints
;
167 /* Flush the constraints list for the device */
168 plist_for_each_entry_safe(req
, tmp
, &c
->list
, node
) {
170 * Update constraints list and call the notification
171 * callbacks if needed
173 apply_constraint(req
, PM_QOS_REMOVE_REQ
, PM_QOS_DEFAULT_VALUE
);
174 memset(req
, 0, sizeof(*req
));
177 spin_lock_irq(&dev
->power
.lock
);
178 dev
->power
.constraints
= NULL
;
179 spin_unlock_irq(&dev
->power
.lock
);
185 mutex_unlock(&dev_pm_qos_mtx
);
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
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
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
206 int dev_pm_qos_add_request(struct device
*dev
, struct dev_pm_qos_request
*req
,
211 if (!dev
|| !req
) /*guard against callers passing in null */
214 if (dev_pm_qos_request_active(req
)) {
215 WARN(1, KERN_ERR
"dev_pm_qos_add_request() called for already "
222 mutex_lock(&dev_pm_qos_mtx
);
224 if (!dev
->power
.constraints
) {
225 if (dev
->power
.power_state
.event
== PM_EVENT_INVALID
) {
226 /* The device has been removed from the system. */
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.
236 ret
= dev_pm_qos_constraints_allocate(dev
);
241 ret
= apply_constraint(req
, PM_QOS_ADD_REQ
, value
);
244 mutex_unlock(&dev_pm_qos_mtx
);
248 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request
);
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
255 * Updates an existing dev PM qos request along with updating the
258 * Attempts are made to make this code callable on hot code paths.
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
265 int dev_pm_qos_update_request(struct dev_pm_qos_request
*req
,
270 if (!req
) /*guard against callers passing in null */
273 if (!dev_pm_qos_request_active(req
)) {
274 WARN(1, KERN_ERR
"dev_pm_qos_update_request() called for "
279 mutex_lock(&dev_pm_qos_mtx
);
281 if (req
->dev
->power
.constraints
) {
282 if (new_value
!= req
->node
.prio
)
283 ret
= apply_constraint(req
, PM_QOS_UPDATE_REQ
,
286 /* Return if the device has been removed */
290 mutex_unlock(&dev_pm_qos_mtx
);
293 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request
);
296 * dev_pm_qos_remove_request - modifies an existing qos request
297 * @req: handle to request list element
299 * Will remove pm qos request from the list of constraints and
300 * recompute the current target value. Call this on slow code paths.
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
307 int dev_pm_qos_remove_request(struct dev_pm_qos_request
*req
)
311 if (!req
) /*guard against callers passing in null */
314 if (!dev_pm_qos_request_active(req
)) {
315 WARN(1, KERN_ERR
"dev_pm_qos_remove_request() called for "
320 mutex_lock(&dev_pm_qos_mtx
);
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
));
327 /* Return if the device has been removed */
331 mutex_unlock(&dev_pm_qos_mtx
);
334 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request
);
337 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
338 * of per-device PM QoS constraints
340 * @dev: target device for the constraint
341 * @notifier: notifier block managed by caller.
343 * Will register the notifier into a notification chain that gets called
344 * upon changes to the target value for the device.
346 int dev_pm_qos_add_notifier(struct device
*dev
, struct notifier_block
*notifier
)
350 mutex_lock(&dev_pm_qos_mtx
);
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
,
358 mutex_unlock(&dev_pm_qos_mtx
);
361 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier
);
364 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
365 * of per-device PM QoS constraints
367 * @dev: target device for the constraint
368 * @notifier: notifier block to be removed.
370 * Will remove the notifier from the notification chain that gets called
371 * upon changes to the target value.
373 int dev_pm_qos_remove_notifier(struct device
*dev
,
374 struct notifier_block
*notifier
)
378 mutex_lock(&dev_pm_qos_mtx
);
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
,
386 mutex_unlock(&dev_pm_qos_mtx
);
389 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier
);
392 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
393 * target value of the PM QoS constraints for any device
395 * @notifier: notifier block managed by caller.
397 * Will register the notifier into a notification chain that gets called
398 * upon changes to the target value for any device.
400 int dev_pm_qos_add_global_notifier(struct notifier_block
*notifier
)
402 return blocking_notifier_chain_register(&dev_pm_notifiers
, notifier
);
404 EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier
);
407 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
408 * target value of PM QoS constraints for any device
410 * @notifier: notifier block to be removed.
412 * Will remove the notifier from the notification chain that gets called
413 * upon changes to the target value for any device.
415 int dev_pm_qos_remove_global_notifier(struct notifier_block
*notifier
)
417 return blocking_notifier_chain_unregister(&dev_pm_notifiers
, notifier
);
419 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier
);