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>
42 #include <linux/export.h>
46 static DEFINE_MUTEX(dev_pm_qos_mtx
);
48 static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers
);
51 * __dev_pm_qos_flags - Check PM QoS flags for a given device.
52 * @dev: Device to check the PM QoS flags for.
53 * @mask: Flags to check against.
55 * This routine must be called with dev->power.lock held.
57 enum pm_qos_flags_status
__dev_pm_qos_flags(struct device
*dev
, s32 mask
)
59 struct dev_pm_qos
*qos
= dev
->power
.qos
;
60 struct pm_qos_flags
*pqf
;
64 return PM_QOS_FLAGS_UNDEFINED
;
67 if (list_empty(&pqf
->list
))
68 return PM_QOS_FLAGS_UNDEFINED
;
70 val
= pqf
->effective_flags
& mask
;
72 return (val
== mask
) ? PM_QOS_FLAGS_ALL
: PM_QOS_FLAGS_SOME
;
74 return PM_QOS_FLAGS_NONE
;
78 * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
79 * @dev: Device to check the PM QoS flags for.
80 * @mask: Flags to check against.
82 enum pm_qos_flags_status
dev_pm_qos_flags(struct device
*dev
, s32 mask
)
84 unsigned long irqflags
;
85 enum pm_qos_flags_status ret
;
87 spin_lock_irqsave(&dev
->power
.lock
, irqflags
);
88 ret
= __dev_pm_qos_flags(dev
, mask
);
89 spin_unlock_irqrestore(&dev
->power
.lock
, irqflags
);
95 * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
96 * @dev: Device to get the PM QoS constraint value for.
98 * This routine must be called with dev->power.lock held.
100 s32
__dev_pm_qos_read_value(struct device
*dev
)
102 return dev
->power
.qos
? pm_qos_read_value(&dev
->power
.qos
->latency
) : 0;
106 * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
107 * @dev: Device to get the PM QoS constraint value for.
109 s32
dev_pm_qos_read_value(struct device
*dev
)
114 spin_lock_irqsave(&dev
->power
.lock
, flags
);
115 ret
= __dev_pm_qos_read_value(dev
);
116 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
122 * apply_constraint - Add/modify/remove device PM QoS request.
123 * @req: Constraint request to apply
124 * @action: Action to perform (add/update/remove).
125 * @value: Value to assign to the QoS request.
127 * Internal function to update the constraints list using the PM QoS core
128 * code and if needed call the per-device and the global notification
131 static int apply_constraint(struct dev_pm_qos_request
*req
,
132 enum pm_qos_req_action action
, s32 value
)
134 struct dev_pm_qos
*qos
= req
->dev
->power
.qos
;
138 case DEV_PM_QOS_LATENCY
:
139 ret
= pm_qos_update_target(&qos
->latency
, &req
->data
.pnode
,
142 value
= pm_qos_read_value(&qos
->latency
);
143 blocking_notifier_call_chain(&dev_pm_notifiers
,
144 (unsigned long)value
,
148 case DEV_PM_QOS_FLAGS
:
149 ret
= pm_qos_update_flags(&qos
->flags
, &req
->data
.flr
,
160 * dev_pm_qos_constraints_allocate
161 * @dev: device to allocate data for
163 * Called at the first call to add_request, for constraint data allocation
164 * Must be called with the dev_pm_qos_mtx mutex held
166 static int dev_pm_qos_constraints_allocate(struct device
*dev
)
168 struct dev_pm_qos
*qos
;
169 struct pm_qos_constraints
*c
;
170 struct blocking_notifier_head
*n
;
172 qos
= kzalloc(sizeof(*qos
), GFP_KERNEL
);
176 n
= kzalloc(sizeof(*n
), GFP_KERNEL
);
181 BLOCKING_INIT_NOTIFIER_HEAD(n
);
184 plist_head_init(&c
->list
);
185 c
->target_value
= PM_QOS_DEV_LAT_DEFAULT_VALUE
;
186 c
->default_value
= PM_QOS_DEV_LAT_DEFAULT_VALUE
;
187 c
->type
= PM_QOS_MIN
;
190 INIT_LIST_HEAD(&qos
->flags
.list
);
192 spin_lock_irq(&dev
->power
.lock
);
193 dev
->power
.qos
= qos
;
194 spin_unlock_irq(&dev
->power
.lock
);
200 * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer.
201 * @dev: target device
203 * Called from the device PM subsystem during device insertion under
206 void dev_pm_qos_constraints_init(struct device
*dev
)
208 mutex_lock(&dev_pm_qos_mtx
);
209 dev
->power
.qos
= NULL
;
210 dev
->power
.power_state
= PMSG_ON
;
211 mutex_unlock(&dev_pm_qos_mtx
);
215 * dev_pm_qos_constraints_destroy
216 * @dev: target device
218 * Called from the device PM subsystem on device removal under device_pm_lock().
220 void dev_pm_qos_constraints_destroy(struct device
*dev
)
222 struct dev_pm_qos
*qos
;
223 struct dev_pm_qos_request
*req
, *tmp
;
224 struct pm_qos_constraints
*c
;
227 * If the device's PM QoS resume latency limit has been exposed to user
228 * space, it has to be hidden at this point.
230 dev_pm_qos_hide_latency_limit(dev
);
232 mutex_lock(&dev_pm_qos_mtx
);
234 dev
->power
.power_state
= PMSG_INVALID
;
235 qos
= dev
->power
.qos
;
240 /* Flush the constraints list for the device */
241 plist_for_each_entry_safe(req
, tmp
, &c
->list
, data
.pnode
) {
243 * Update constraints list and call the notification
244 * callbacks if needed
246 apply_constraint(req
, PM_QOS_REMOVE_REQ
, PM_QOS_DEFAULT_VALUE
);
247 memset(req
, 0, sizeof(*req
));
250 spin_lock_irq(&dev
->power
.lock
);
251 dev
->power
.qos
= NULL
;
252 spin_unlock_irq(&dev
->power
.lock
);
258 mutex_unlock(&dev_pm_qos_mtx
);
262 * dev_pm_qos_add_request - inserts new qos request into the list
263 * @dev: target device for the constraint
264 * @req: pointer to a preallocated handle
265 * @type: type of the request
266 * @value: defines the qos request
268 * This function inserts a new entry in the device constraints list of
269 * requested qos performance characteristics. It recomputes the aggregate
270 * QoS expectations of parameters and initializes the dev_pm_qos_request
271 * handle. Caller needs to save this handle for later use in updates and
274 * Returns 1 if the aggregated constraint value has changed,
275 * 0 if the aggregated constraint value has not changed,
276 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
277 * to allocate for data structures, -ENODEV if the device has just been removed
280 int dev_pm_qos_add_request(struct device
*dev
, struct dev_pm_qos_request
*req
,
281 enum dev_pm_qos_req_type type
, s32 value
)
285 if (!dev
|| !req
) /*guard against callers passing in null */
288 if (WARN(dev_pm_qos_request_active(req
),
289 "%s() called for already added request\n", __func__
))
294 mutex_lock(&dev_pm_qos_mtx
);
296 if (!dev
->power
.qos
) {
297 if (dev
->power
.power_state
.event
== PM_EVENT_INVALID
) {
298 /* The device has been removed from the system. */
304 * Allocate the constraints data on the first call to
305 * add_request, i.e. only if the data is not already
306 * allocated and if the device has not been removed.
308 ret
= dev_pm_qos_constraints_allocate(dev
);
314 ret
= apply_constraint(req
, PM_QOS_ADD_REQ
, value
);
318 mutex_unlock(&dev_pm_qos_mtx
);
322 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request
);
325 * dev_pm_qos_update_request - modifies an existing qos request
326 * @req : handle to list element holding a dev_pm_qos request to use
327 * @new_value: defines the qos request
329 * Updates an existing dev PM qos request along with updating the
332 * Attempts are made to make this code callable on hot code paths.
334 * Returns 1 if the aggregated constraint value has changed,
335 * 0 if the aggregated constraint value has not changed,
336 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
337 * removed from the system
339 int dev_pm_qos_update_request(struct dev_pm_qos_request
*req
,
345 if (!req
) /*guard against callers passing in null */
348 if (WARN(!dev_pm_qos_request_active(req
),
349 "%s() called for unknown object\n", __func__
))
352 mutex_lock(&dev_pm_qos_mtx
);
354 if (!req
->dev
->power
.qos
) {
360 case DEV_PM_QOS_LATENCY
:
361 curr_value
= req
->data
.pnode
.prio
;
363 case DEV_PM_QOS_FLAGS
:
364 curr_value
= req
->data
.flr
.flags
;
371 if (curr_value
!= new_value
)
372 ret
= apply_constraint(req
, PM_QOS_UPDATE_REQ
, new_value
);
375 mutex_unlock(&dev_pm_qos_mtx
);
378 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request
);
381 * dev_pm_qos_remove_request - modifies an existing qos request
382 * @req: handle to request list element
384 * Will remove pm qos request from the list of constraints and
385 * recompute the current target value. Call this on slow code paths.
387 * Returns 1 if the aggregated constraint value has changed,
388 * 0 if the aggregated constraint value has not changed,
389 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
390 * removed from the system
392 int dev_pm_qos_remove_request(struct dev_pm_qos_request
*req
)
396 if (!req
) /*guard against callers passing in null */
399 if (WARN(!dev_pm_qos_request_active(req
),
400 "%s() called for unknown object\n", __func__
))
403 mutex_lock(&dev_pm_qos_mtx
);
405 if (req
->dev
->power
.qos
) {
406 ret
= apply_constraint(req
, PM_QOS_REMOVE_REQ
,
407 PM_QOS_DEFAULT_VALUE
);
408 memset(req
, 0, sizeof(*req
));
410 /* Return if the device has been removed */
414 mutex_unlock(&dev_pm_qos_mtx
);
417 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request
);
420 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
421 * of per-device PM QoS constraints
423 * @dev: target device for the constraint
424 * @notifier: notifier block managed by caller.
426 * Will register the notifier into a notification chain that gets called
427 * upon changes to the target value for the device.
429 * If the device's constraints object doesn't exist when this routine is called,
430 * it will be created (or error code will be returned if that fails).
432 int dev_pm_qos_add_notifier(struct device
*dev
, struct notifier_block
*notifier
)
436 mutex_lock(&dev_pm_qos_mtx
);
439 ret
= dev
->power
.power_state
.event
!= PM_EVENT_INVALID
?
440 dev_pm_qos_constraints_allocate(dev
) : -ENODEV
;
443 ret
= blocking_notifier_chain_register(
444 dev
->power
.qos
->latency
.notifiers
, notifier
);
446 mutex_unlock(&dev_pm_qos_mtx
);
449 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier
);
452 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
453 * of per-device PM QoS constraints
455 * @dev: target device for the constraint
456 * @notifier: notifier block to be removed.
458 * Will remove the notifier from the notification chain that gets called
459 * upon changes to the target value.
461 int dev_pm_qos_remove_notifier(struct device
*dev
,
462 struct notifier_block
*notifier
)
466 mutex_lock(&dev_pm_qos_mtx
);
468 /* Silently return if the constraints object is not present. */
470 retval
= blocking_notifier_chain_unregister(
471 dev
->power
.qos
->latency
.notifiers
,
474 mutex_unlock(&dev_pm_qos_mtx
);
477 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier
);
480 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
481 * target value of the PM QoS constraints for any device
483 * @notifier: notifier block managed by caller.
485 * Will register the notifier into a notification chain that gets called
486 * upon changes to the target value for any device.
488 int dev_pm_qos_add_global_notifier(struct notifier_block
*notifier
)
490 return blocking_notifier_chain_register(&dev_pm_notifiers
, notifier
);
492 EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier
);
495 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
496 * target value of PM QoS constraints for any device
498 * @notifier: notifier block to be removed.
500 * Will remove the notifier from the notification chain that gets called
501 * upon changes to the target value for any device.
503 int dev_pm_qos_remove_global_notifier(struct notifier_block
*notifier
)
505 return blocking_notifier_chain_unregister(&dev_pm_notifiers
, notifier
);
507 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier
);
510 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
511 * @dev: Device whose ancestor to add the request for.
512 * @req: Pointer to the preallocated handle.
513 * @value: Constraint latency value.
515 int dev_pm_qos_add_ancestor_request(struct device
*dev
,
516 struct dev_pm_qos_request
*req
, s32 value
)
518 struct device
*ancestor
= dev
->parent
;
521 while (ancestor
&& !ancestor
->power
.ignore_children
)
522 ancestor
= ancestor
->parent
;
525 error
= dev_pm_qos_add_request(ancestor
, req
,
526 DEV_PM_QOS_LATENCY
, value
);
533 EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request
);
535 #ifdef CONFIG_PM_RUNTIME
536 static void __dev_pm_qos_drop_user_request(struct device
*dev
)
538 dev_pm_qos_remove_request(dev
->power
.pq_req
);
539 dev
->power
.pq_req
= NULL
;
543 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
544 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
545 * @value: Initial value of the latency limit.
547 int dev_pm_qos_expose_latency_limit(struct device
*dev
, s32 value
)
549 struct dev_pm_qos_request
*req
;
552 if (!device_is_registered(dev
) || value
< 0)
555 if (dev
->power
.pq_req
)
558 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
562 ret
= dev_pm_qos_add_request(dev
, req
, DEV_PM_QOS_LATENCY
, value
);
566 dev
->power
.pq_req
= req
;
567 ret
= pm_qos_sysfs_add(dev
);
569 __dev_pm_qos_drop_user_request(dev
);
573 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit
);
576 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
577 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
579 void dev_pm_qos_hide_latency_limit(struct device
*dev
)
581 if (dev
->power
.pq_req
) {
582 pm_qos_sysfs_remove(dev
);
583 __dev_pm_qos_drop_user_request(dev
);
586 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit
);
587 #endif /* CONFIG_PM_RUNTIME */