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>
43 #include <linux/pm_runtime.h>
44 #include <linux/err.h>
45 #include <trace/events/power.h>
49 static DEFINE_MUTEX(dev_pm_qos_mtx
);
50 static DEFINE_MUTEX(dev_pm_qos_sysfs_mtx
);
52 static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers
);
55 * __dev_pm_qos_flags - Check PM QoS flags for a given device.
56 * @dev: Device to check the PM QoS flags for.
57 * @mask: Flags to check against.
59 * This routine must be called with dev->power.lock held.
61 enum pm_qos_flags_status
__dev_pm_qos_flags(struct device
*dev
, s32 mask
)
63 struct dev_pm_qos
*qos
= dev
->power
.qos
;
64 struct pm_qos_flags
*pqf
;
67 if (IS_ERR_OR_NULL(qos
))
68 return PM_QOS_FLAGS_UNDEFINED
;
71 if (list_empty(&pqf
->list
))
72 return PM_QOS_FLAGS_UNDEFINED
;
74 val
= pqf
->effective_flags
& mask
;
76 return (val
== mask
) ? PM_QOS_FLAGS_ALL
: PM_QOS_FLAGS_SOME
;
78 return PM_QOS_FLAGS_NONE
;
82 * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
83 * @dev: Device to check the PM QoS flags for.
84 * @mask: Flags to check against.
86 enum pm_qos_flags_status
dev_pm_qos_flags(struct device
*dev
, s32 mask
)
88 unsigned long irqflags
;
89 enum pm_qos_flags_status ret
;
91 spin_lock_irqsave(&dev
->power
.lock
, irqflags
);
92 ret
= __dev_pm_qos_flags(dev
, mask
);
93 spin_unlock_irqrestore(&dev
->power
.lock
, irqflags
);
97 EXPORT_SYMBOL_GPL(dev_pm_qos_flags
);
100 * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
101 * @dev: Device to get the PM QoS constraint value for.
103 * This routine must be called with dev->power.lock held.
105 s32
__dev_pm_qos_read_value(struct device
*dev
)
107 return IS_ERR_OR_NULL(dev
->power
.qos
) ?
108 0 : pm_qos_read_value(&dev
->power
.qos
->latency
);
112 * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
113 * @dev: Device to get the PM QoS constraint value for.
115 s32
dev_pm_qos_read_value(struct device
*dev
)
120 spin_lock_irqsave(&dev
->power
.lock
, flags
);
121 ret
= __dev_pm_qos_read_value(dev
);
122 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
128 * apply_constraint - Add/modify/remove device PM QoS request.
129 * @req: Constraint request to apply
130 * @action: Action to perform (add/update/remove).
131 * @value: Value to assign to the QoS request.
133 * Internal function to update the constraints list using the PM QoS core
134 * code and if needed call the per-device and the global notification
137 static int apply_constraint(struct dev_pm_qos_request
*req
,
138 enum pm_qos_req_action action
, s32 value
)
140 struct dev_pm_qos
*qos
= req
->dev
->power
.qos
;
144 case DEV_PM_QOS_LATENCY
:
145 ret
= pm_qos_update_target(&qos
->latency
, &req
->data
.pnode
,
148 value
= pm_qos_read_value(&qos
->latency
);
149 blocking_notifier_call_chain(&dev_pm_notifiers
,
150 (unsigned long)value
,
154 case DEV_PM_QOS_FLAGS
:
155 ret
= pm_qos_update_flags(&qos
->flags
, &req
->data
.flr
,
166 * dev_pm_qos_constraints_allocate
167 * @dev: device to allocate data for
169 * Called at the first call to add_request, for constraint data allocation
170 * Must be called with the dev_pm_qos_mtx mutex held
172 static int dev_pm_qos_constraints_allocate(struct device
*dev
)
174 struct dev_pm_qos
*qos
;
175 struct pm_qos_constraints
*c
;
176 struct blocking_notifier_head
*n
;
178 qos
= kzalloc(sizeof(*qos
), GFP_KERNEL
);
182 n
= kzalloc(sizeof(*n
), GFP_KERNEL
);
187 BLOCKING_INIT_NOTIFIER_HEAD(n
);
190 plist_head_init(&c
->list
);
191 c
->target_value
= PM_QOS_DEV_LAT_DEFAULT_VALUE
;
192 c
->default_value
= PM_QOS_DEV_LAT_DEFAULT_VALUE
;
193 c
->type
= PM_QOS_MIN
;
196 INIT_LIST_HEAD(&qos
->flags
.list
);
198 spin_lock_irq(&dev
->power
.lock
);
199 dev
->power
.qos
= qos
;
200 spin_unlock_irq(&dev
->power
.lock
);
205 static void __dev_pm_qos_hide_latency_limit(struct device
*dev
);
206 static void __dev_pm_qos_hide_flags(struct device
*dev
);
209 * dev_pm_qos_constraints_destroy
210 * @dev: target device
212 * Called from the device PM subsystem on device removal under device_pm_lock().
214 void dev_pm_qos_constraints_destroy(struct device
*dev
)
216 struct dev_pm_qos
*qos
;
217 struct dev_pm_qos_request
*req
, *tmp
;
218 struct pm_qos_constraints
*c
;
219 struct pm_qos_flags
*f
;
221 mutex_lock(&dev_pm_qos_sysfs_mtx
);
224 * If the device's PM QoS resume latency limit or PM QoS flags have been
225 * exposed to user space, they have to be hidden at this point.
227 pm_qos_sysfs_remove_latency(dev
);
228 pm_qos_sysfs_remove_flags(dev
);
230 mutex_lock(&dev_pm_qos_mtx
);
232 __dev_pm_qos_hide_latency_limit(dev
);
233 __dev_pm_qos_hide_flags(dev
);
235 qos
= dev
->power
.qos
;
239 /* Flush the constraints lists 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 list_for_each_entry_safe(req
, tmp
, &f
->list
, data
.flr
.node
) {
251 apply_constraint(req
, PM_QOS_REMOVE_REQ
, PM_QOS_DEFAULT_VALUE
);
252 memset(req
, 0, sizeof(*req
));
255 spin_lock_irq(&dev
->power
.lock
);
256 dev
->power
.qos
= ERR_PTR(-ENODEV
);
257 spin_unlock_irq(&dev
->power
.lock
);
263 mutex_unlock(&dev_pm_qos_mtx
);
265 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
269 * dev_pm_qos_add_request - inserts new qos request into the list
270 * @dev: target device for the constraint
271 * @req: pointer to a preallocated handle
272 * @type: type of the request
273 * @value: defines the qos request
275 * This function inserts a new entry in the device constraints list of
276 * requested qos performance characteristics. It recomputes the aggregate
277 * QoS expectations of parameters and initializes the dev_pm_qos_request
278 * handle. Caller needs to save this handle for later use in updates and
281 * Returns 1 if the aggregated constraint value has changed,
282 * 0 if the aggregated constraint value has not changed,
283 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
284 * to allocate for data structures, -ENODEV if the device has just been removed
287 * Callers should ensure that the target device is not RPM_SUSPENDED before
288 * using this function for requests of type DEV_PM_QOS_FLAGS.
290 int dev_pm_qos_add_request(struct device
*dev
, struct dev_pm_qos_request
*req
,
291 enum dev_pm_qos_req_type type
, s32 value
)
295 if (!dev
|| !req
) /*guard against callers passing in null */
298 if (WARN(dev_pm_qos_request_active(req
),
299 "%s() called for already added request\n", __func__
))
302 mutex_lock(&dev_pm_qos_mtx
);
304 if (IS_ERR(dev
->power
.qos
))
306 else if (!dev
->power
.qos
)
307 ret
= dev_pm_qos_constraints_allocate(dev
);
309 trace_dev_pm_qos_add_request(dev_name(dev
), type
, value
);
313 ret
= apply_constraint(req
, PM_QOS_ADD_REQ
, value
);
316 mutex_unlock(&dev_pm_qos_mtx
);
320 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request
);
323 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
324 * @req : PM QoS request to modify.
325 * @new_value: New value to request.
327 static int __dev_pm_qos_update_request(struct dev_pm_qos_request
*req
,
333 if (!req
) /*guard against callers passing in null */
336 if (WARN(!dev_pm_qos_request_active(req
),
337 "%s() called for unknown object\n", __func__
))
340 if (IS_ERR_OR_NULL(req
->dev
->power
.qos
))
344 case DEV_PM_QOS_LATENCY
:
345 curr_value
= req
->data
.pnode
.prio
;
347 case DEV_PM_QOS_FLAGS
:
348 curr_value
= req
->data
.flr
.flags
;
354 trace_dev_pm_qos_update_request(dev_name(req
->dev
), req
->type
,
356 if (curr_value
!= new_value
)
357 ret
= apply_constraint(req
, PM_QOS_UPDATE_REQ
, new_value
);
363 * dev_pm_qos_update_request - modifies an existing qos request
364 * @req : handle to list element holding a dev_pm_qos request to use
365 * @new_value: defines the qos request
367 * Updates an existing dev PM qos request along with updating the
370 * Attempts are made to make this code callable on hot code paths.
372 * Returns 1 if the aggregated constraint value has changed,
373 * 0 if the aggregated constraint value has not changed,
374 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
375 * removed from the system
377 * Callers should ensure that the target device is not RPM_SUSPENDED before
378 * using this function for requests of type DEV_PM_QOS_FLAGS.
380 int dev_pm_qos_update_request(struct dev_pm_qos_request
*req
, s32 new_value
)
384 mutex_lock(&dev_pm_qos_mtx
);
385 ret
= __dev_pm_qos_update_request(req
, new_value
);
386 mutex_unlock(&dev_pm_qos_mtx
);
389 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request
);
391 static int __dev_pm_qos_remove_request(struct dev_pm_qos_request
*req
)
395 if (!req
) /*guard against callers passing in null */
398 if (WARN(!dev_pm_qos_request_active(req
),
399 "%s() called for unknown object\n", __func__
))
402 if (IS_ERR_OR_NULL(req
->dev
->power
.qos
))
405 trace_dev_pm_qos_remove_request(dev_name(req
->dev
), req
->type
,
406 PM_QOS_DEFAULT_VALUE
);
407 ret
= apply_constraint(req
, PM_QOS_REMOVE_REQ
, PM_QOS_DEFAULT_VALUE
);
408 memset(req
, 0, sizeof(*req
));
413 * dev_pm_qos_remove_request - modifies an existing qos request
414 * @req: handle to request list element
416 * Will remove pm qos request from the list of constraints and
417 * recompute the current target value. Call this on slow code paths.
419 * Returns 1 if the aggregated constraint value has changed,
420 * 0 if the aggregated constraint value has not changed,
421 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
422 * removed from the system
424 * Callers should ensure that the target device is not RPM_SUSPENDED before
425 * using this function for requests of type DEV_PM_QOS_FLAGS.
427 int dev_pm_qos_remove_request(struct dev_pm_qos_request
*req
)
431 mutex_lock(&dev_pm_qos_mtx
);
432 ret
= __dev_pm_qos_remove_request(req
);
433 mutex_unlock(&dev_pm_qos_mtx
);
436 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request
);
439 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
440 * of per-device PM QoS constraints
442 * @dev: target device for the constraint
443 * @notifier: notifier block managed by caller.
445 * Will register the notifier into a notification chain that gets called
446 * upon changes to the target value for the device.
448 * If the device's constraints object doesn't exist when this routine is called,
449 * it will be created (or error code will be returned if that fails).
451 int dev_pm_qos_add_notifier(struct device
*dev
, struct notifier_block
*notifier
)
455 mutex_lock(&dev_pm_qos_mtx
);
457 if (IS_ERR(dev
->power
.qos
))
459 else if (!dev
->power
.qos
)
460 ret
= dev_pm_qos_constraints_allocate(dev
);
463 ret
= blocking_notifier_chain_register(
464 dev
->power
.qos
->latency
.notifiers
, notifier
);
466 mutex_unlock(&dev_pm_qos_mtx
);
469 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier
);
472 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
473 * of per-device PM QoS constraints
475 * @dev: target device for the constraint
476 * @notifier: notifier block to be removed.
478 * Will remove the notifier from the notification chain that gets called
479 * upon changes to the target value.
481 int dev_pm_qos_remove_notifier(struct device
*dev
,
482 struct notifier_block
*notifier
)
486 mutex_lock(&dev_pm_qos_mtx
);
488 /* Silently return if the constraints object is not present. */
489 if (!IS_ERR_OR_NULL(dev
->power
.qos
))
490 retval
= blocking_notifier_chain_unregister(
491 dev
->power
.qos
->latency
.notifiers
,
494 mutex_unlock(&dev_pm_qos_mtx
);
497 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier
);
500 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
501 * target value of the PM QoS constraints for any device
503 * @notifier: notifier block managed by caller.
505 * Will register the notifier into a notification chain that gets called
506 * upon changes to the target value for any device.
508 int dev_pm_qos_add_global_notifier(struct notifier_block
*notifier
)
510 return blocking_notifier_chain_register(&dev_pm_notifiers
, notifier
);
512 EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier
);
515 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
516 * target value of PM QoS constraints for any device
518 * @notifier: notifier block to be removed.
520 * Will remove the notifier from the notification chain that gets called
521 * upon changes to the target value for any device.
523 int dev_pm_qos_remove_global_notifier(struct notifier_block
*notifier
)
525 return blocking_notifier_chain_unregister(&dev_pm_notifiers
, notifier
);
527 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier
);
530 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
531 * @dev: Device whose ancestor to add the request for.
532 * @req: Pointer to the preallocated handle.
533 * @value: Constraint latency value.
535 int dev_pm_qos_add_ancestor_request(struct device
*dev
,
536 struct dev_pm_qos_request
*req
, s32 value
)
538 struct device
*ancestor
= dev
->parent
;
541 while (ancestor
&& !ancestor
->power
.ignore_children
)
542 ancestor
= ancestor
->parent
;
545 ret
= dev_pm_qos_add_request(ancestor
, req
,
546 DEV_PM_QOS_LATENCY
, value
);
553 EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request
);
555 #ifdef CONFIG_PM_RUNTIME
556 static void __dev_pm_qos_drop_user_request(struct device
*dev
,
557 enum dev_pm_qos_req_type type
)
559 struct dev_pm_qos_request
*req
= NULL
;
562 case DEV_PM_QOS_LATENCY
:
563 req
= dev
->power
.qos
->latency_req
;
564 dev
->power
.qos
->latency_req
= NULL
;
566 case DEV_PM_QOS_FLAGS
:
567 req
= dev
->power
.qos
->flags_req
;
568 dev
->power
.qos
->flags_req
= NULL
;
571 __dev_pm_qos_remove_request(req
);
575 static void dev_pm_qos_drop_user_request(struct device
*dev
,
576 enum dev_pm_qos_req_type type
)
578 mutex_lock(&dev_pm_qos_mtx
);
579 __dev_pm_qos_drop_user_request(dev
, type
);
580 mutex_unlock(&dev_pm_qos_mtx
);
584 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
585 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
586 * @value: Initial value of the latency limit.
588 int dev_pm_qos_expose_latency_limit(struct device
*dev
, s32 value
)
590 struct dev_pm_qos_request
*req
;
593 if (!device_is_registered(dev
) || value
< 0)
596 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
600 ret
= dev_pm_qos_add_request(dev
, req
, DEV_PM_QOS_LATENCY
, value
);
606 mutex_lock(&dev_pm_qos_sysfs_mtx
);
608 mutex_lock(&dev_pm_qos_mtx
);
610 if (IS_ERR_OR_NULL(dev
->power
.qos
))
612 else if (dev
->power
.qos
->latency_req
)
616 __dev_pm_qos_remove_request(req
);
618 mutex_unlock(&dev_pm_qos_mtx
);
621 dev
->power
.qos
->latency_req
= req
;
623 mutex_unlock(&dev_pm_qos_mtx
);
625 ret
= pm_qos_sysfs_add_latency(dev
);
627 dev_pm_qos_drop_user_request(dev
, DEV_PM_QOS_LATENCY
);
630 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
633 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit
);
635 static void __dev_pm_qos_hide_latency_limit(struct device
*dev
)
637 if (!IS_ERR_OR_NULL(dev
->power
.qos
) && dev
->power
.qos
->latency_req
)
638 __dev_pm_qos_drop_user_request(dev
, DEV_PM_QOS_LATENCY
);
642 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
643 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
645 void dev_pm_qos_hide_latency_limit(struct device
*dev
)
647 mutex_lock(&dev_pm_qos_sysfs_mtx
);
649 pm_qos_sysfs_remove_latency(dev
);
651 mutex_lock(&dev_pm_qos_mtx
);
652 __dev_pm_qos_hide_latency_limit(dev
);
653 mutex_unlock(&dev_pm_qos_mtx
);
655 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
657 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit
);
660 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
661 * @dev: Device whose PM QoS flags are to be exposed to user space.
662 * @val: Initial values of the flags.
664 int dev_pm_qos_expose_flags(struct device
*dev
, s32 val
)
666 struct dev_pm_qos_request
*req
;
669 if (!device_is_registered(dev
))
672 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
676 ret
= dev_pm_qos_add_request(dev
, req
, DEV_PM_QOS_FLAGS
, val
);
682 pm_runtime_get_sync(dev
);
683 mutex_lock(&dev_pm_qos_sysfs_mtx
);
685 mutex_lock(&dev_pm_qos_mtx
);
687 if (IS_ERR_OR_NULL(dev
->power
.qos
))
689 else if (dev
->power
.qos
->flags_req
)
693 __dev_pm_qos_remove_request(req
);
695 mutex_unlock(&dev_pm_qos_mtx
);
698 dev
->power
.qos
->flags_req
= req
;
700 mutex_unlock(&dev_pm_qos_mtx
);
702 ret
= pm_qos_sysfs_add_flags(dev
);
704 dev_pm_qos_drop_user_request(dev
, DEV_PM_QOS_FLAGS
);
707 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
711 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags
);
713 static void __dev_pm_qos_hide_flags(struct device
*dev
)
715 if (!IS_ERR_OR_NULL(dev
->power
.qos
) && dev
->power
.qos
->flags_req
)
716 __dev_pm_qos_drop_user_request(dev
, DEV_PM_QOS_FLAGS
);
720 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
721 * @dev: Device whose PM QoS flags are to be hidden from user space.
723 void dev_pm_qos_hide_flags(struct device
*dev
)
725 pm_runtime_get_sync(dev
);
726 mutex_lock(&dev_pm_qos_sysfs_mtx
);
728 pm_qos_sysfs_remove_flags(dev
);
730 mutex_lock(&dev_pm_qos_mtx
);
731 __dev_pm_qos_hide_flags(dev
);
732 mutex_unlock(&dev_pm_qos_mtx
);
734 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
737 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags
);
740 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
741 * @dev: Device to update the PM QoS flags request for.
742 * @mask: Flags to set/clear.
743 * @set: Whether to set or clear the flags (true means set).
745 int dev_pm_qos_update_flags(struct device
*dev
, s32 mask
, bool set
)
750 pm_runtime_get_sync(dev
);
751 mutex_lock(&dev_pm_qos_mtx
);
753 if (IS_ERR_OR_NULL(dev
->power
.qos
) || !dev
->power
.qos
->flags_req
) {
758 value
= dev_pm_qos_requested_flags(dev
);
764 ret
= __dev_pm_qos_update_request(dev
->power
.qos
->flags_req
, value
);
767 mutex_unlock(&dev_pm_qos_mtx
);
771 #else /* !CONFIG_PM_RUNTIME */
772 static void __dev_pm_qos_hide_latency_limit(struct device
*dev
) {}
773 static void __dev_pm_qos_hide_flags(struct device
*dev
) {}
774 #endif /* CONFIG_PM_RUNTIME */