1 // SPDX-License-Identifier: GPL-2.0
3 * Devices PM QoS constraints management
5 * Copyright (C) 2011 Texas Instruments, Inc.
7 * This module exposes the interface to kernel space for specifying
8 * per-device PM QoS dependencies. It provides infrastructure for registration
11 * Dependents on a QoS value : register requests
12 * Watchers of QoS value : get notified when target QoS value changes
14 * This QoS design is best effort based. Dependents register their QoS needs.
15 * Watchers register to keep track of the current QoS needs of the system.
16 * Watchers can register a per-device notification callback using the
17 * dev_pm_qos_*_notifier API. The notification chain data is stored in the
18 * per-device constraint data struct.
20 * Note about the per-device constraint data struct allocation:
21 * . The per-device constraints data struct ptr is stored into the device
23 * . To minimize the data usage by the per-device constraints, the data struct
24 * is only allocated at the first call to dev_pm_qos_add_request.
25 * . The data is later free'd when the device is removed from the system.
26 * . A global mutex protects the constraints users from the data being
27 * allocated and free'd.
30 #include <linux/pm_qos.h>
31 #include <linux/spinlock.h>
32 #include <linux/slab.h>
33 #include <linux/device.h>
34 #include <linux/mutex.h>
35 #include <linux/export.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/err.h>
38 #include <trace/events/power.h>
42 static DEFINE_MUTEX(dev_pm_qos_mtx
);
43 static DEFINE_MUTEX(dev_pm_qos_sysfs_mtx
);
46 * __dev_pm_qos_flags - Check PM QoS flags for a given device.
47 * @dev: Device to check the PM QoS flags for.
48 * @mask: Flags to check against.
50 * This routine must be called with dev->power.lock held.
52 enum pm_qos_flags_status
__dev_pm_qos_flags(struct device
*dev
, s32 mask
)
54 struct dev_pm_qos
*qos
= dev
->power
.qos
;
55 struct pm_qos_flags
*pqf
;
58 lockdep_assert_held(&dev
->power
.lock
);
60 if (IS_ERR_OR_NULL(qos
))
61 return PM_QOS_FLAGS_UNDEFINED
;
64 if (list_empty(&pqf
->list
))
65 return PM_QOS_FLAGS_UNDEFINED
;
67 val
= pqf
->effective_flags
& mask
;
69 return (val
== mask
) ? PM_QOS_FLAGS_ALL
: PM_QOS_FLAGS_SOME
;
71 return PM_QOS_FLAGS_NONE
;
75 * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
76 * @dev: Device to check the PM QoS flags for.
77 * @mask: Flags to check against.
79 enum pm_qos_flags_status
dev_pm_qos_flags(struct device
*dev
, s32 mask
)
81 unsigned long irqflags
;
82 enum pm_qos_flags_status ret
;
84 spin_lock_irqsave(&dev
->power
.lock
, irqflags
);
85 ret
= __dev_pm_qos_flags(dev
, mask
);
86 spin_unlock_irqrestore(&dev
->power
.lock
, irqflags
);
90 EXPORT_SYMBOL_GPL(dev_pm_qos_flags
);
93 * __dev_pm_qos_resume_latency - Get resume latency constraint for a given device.
94 * @dev: Device to get the PM QoS constraint value for.
96 * This routine must be called with dev->power.lock held.
98 s32
__dev_pm_qos_resume_latency(struct device
*dev
)
100 lockdep_assert_held(&dev
->power
.lock
);
102 return dev_pm_qos_raw_resume_latency(dev
);
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.
108 * @type: QoS request type.
110 s32
dev_pm_qos_read_value(struct device
*dev
, enum dev_pm_qos_req_type type
)
112 struct dev_pm_qos
*qos
= dev
->power
.qos
;
116 spin_lock_irqsave(&dev
->power
.lock
, flags
);
119 case DEV_PM_QOS_RESUME_LATENCY
:
120 ret
= IS_ERR_OR_NULL(qos
) ? PM_QOS_RESUME_LATENCY_NO_CONSTRAINT
121 : pm_qos_read_value(&qos
->resume_latency
);
123 case DEV_PM_QOS_MIN_FREQUENCY
:
124 ret
= IS_ERR_OR_NULL(qos
) ? PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE
125 : freq_qos_read_value(&qos
->freq
, FREQ_QOS_MIN
);
127 case DEV_PM_QOS_MAX_FREQUENCY
:
128 ret
= IS_ERR_OR_NULL(qos
) ? PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE
129 : freq_qos_read_value(&qos
->freq
, FREQ_QOS_MAX
);
136 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
140 EXPORT_SYMBOL_GPL(dev_pm_qos_read_value
);
143 * apply_constraint - Add/modify/remove device PM QoS request.
144 * @req: Constraint request to apply
145 * @action: Action to perform (add/update/remove).
146 * @value: Value to assign to the QoS request.
148 * Internal function to update the constraints list using the PM QoS core
149 * code and if needed call the per-device callbacks.
151 static int apply_constraint(struct dev_pm_qos_request
*req
,
152 enum pm_qos_req_action action
, s32 value
)
154 struct dev_pm_qos
*qos
= req
->dev
->power
.qos
;
158 case DEV_PM_QOS_RESUME_LATENCY
:
159 if (WARN_ON(action
!= PM_QOS_REMOVE_REQ
&& value
< 0))
162 ret
= pm_qos_update_target(&qos
->resume_latency
,
163 &req
->data
.pnode
, action
, value
);
165 case DEV_PM_QOS_LATENCY_TOLERANCE
:
166 ret
= pm_qos_update_target(&qos
->latency_tolerance
,
167 &req
->data
.pnode
, action
, value
);
169 value
= pm_qos_read_value(&qos
->latency_tolerance
);
170 req
->dev
->power
.set_latency_tolerance(req
->dev
, value
);
173 case DEV_PM_QOS_MIN_FREQUENCY
:
174 case DEV_PM_QOS_MAX_FREQUENCY
:
175 ret
= freq_qos_apply(&req
->data
.freq
, action
, value
);
177 case DEV_PM_QOS_FLAGS
:
178 ret
= pm_qos_update_flags(&qos
->flags
, &req
->data
.flr
,
189 * dev_pm_qos_constraints_allocate
190 * @dev: device to allocate data for
192 * Called at the first call to add_request, for constraint data allocation
193 * Must be called with the dev_pm_qos_mtx mutex held
195 static int dev_pm_qos_constraints_allocate(struct device
*dev
)
197 struct dev_pm_qos
*qos
;
198 struct pm_qos_constraints
*c
;
199 struct blocking_notifier_head
*n
;
201 qos
= kzalloc(sizeof(*qos
), GFP_KERNEL
);
205 n
= kcalloc(3, sizeof(*n
), GFP_KERNEL
);
211 c
= &qos
->resume_latency
;
212 plist_head_init(&c
->list
);
213 c
->target_value
= PM_QOS_RESUME_LATENCY_DEFAULT_VALUE
;
214 c
->default_value
= PM_QOS_RESUME_LATENCY_DEFAULT_VALUE
;
215 c
->no_constraint_value
= PM_QOS_RESUME_LATENCY_NO_CONSTRAINT
;
216 c
->type
= PM_QOS_MIN
;
218 BLOCKING_INIT_NOTIFIER_HEAD(n
);
220 c
= &qos
->latency_tolerance
;
221 plist_head_init(&c
->list
);
222 c
->target_value
= PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE
;
223 c
->default_value
= PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE
;
224 c
->no_constraint_value
= PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT
;
225 c
->type
= PM_QOS_MIN
;
227 freq_constraints_init(&qos
->freq
);
229 INIT_LIST_HEAD(&qos
->flags
.list
);
231 spin_lock_irq(&dev
->power
.lock
);
232 dev
->power
.qos
= qos
;
233 spin_unlock_irq(&dev
->power
.lock
);
238 static void __dev_pm_qos_hide_latency_limit(struct device
*dev
);
239 static void __dev_pm_qos_hide_flags(struct device
*dev
);
242 * dev_pm_qos_constraints_destroy
243 * @dev: target device
245 * Called from the device PM subsystem on device removal under device_pm_lock().
247 void dev_pm_qos_constraints_destroy(struct device
*dev
)
249 struct dev_pm_qos
*qos
;
250 struct dev_pm_qos_request
*req
, *tmp
;
251 struct pm_qos_constraints
*c
;
252 struct pm_qos_flags
*f
;
254 mutex_lock(&dev_pm_qos_sysfs_mtx
);
257 * If the device's PM QoS resume latency limit or PM QoS flags have been
258 * exposed to user space, they have to be hidden at this point.
260 pm_qos_sysfs_remove_resume_latency(dev
);
261 pm_qos_sysfs_remove_flags(dev
);
263 mutex_lock(&dev_pm_qos_mtx
);
265 __dev_pm_qos_hide_latency_limit(dev
);
266 __dev_pm_qos_hide_flags(dev
);
268 qos
= dev
->power
.qos
;
272 /* Flush the constraints lists for the device. */
273 c
= &qos
->resume_latency
;
274 plist_for_each_entry_safe(req
, tmp
, &c
->list
, data
.pnode
) {
276 * Update constraints list and call the notification
277 * callbacks if needed
279 apply_constraint(req
, PM_QOS_REMOVE_REQ
, PM_QOS_DEFAULT_VALUE
);
280 memset(req
, 0, sizeof(*req
));
283 c
= &qos
->latency_tolerance
;
284 plist_for_each_entry_safe(req
, tmp
, &c
->list
, data
.pnode
) {
285 apply_constraint(req
, PM_QOS_REMOVE_REQ
, PM_QOS_DEFAULT_VALUE
);
286 memset(req
, 0, sizeof(*req
));
289 c
= &qos
->freq
.min_freq
;
290 plist_for_each_entry_safe(req
, tmp
, &c
->list
, data
.freq
.pnode
) {
291 apply_constraint(req
, PM_QOS_REMOVE_REQ
,
292 PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE
);
293 memset(req
, 0, sizeof(*req
));
296 c
= &qos
->freq
.max_freq
;
297 plist_for_each_entry_safe(req
, tmp
, &c
->list
, data
.freq
.pnode
) {
298 apply_constraint(req
, PM_QOS_REMOVE_REQ
,
299 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE
);
300 memset(req
, 0, sizeof(*req
));
304 list_for_each_entry_safe(req
, tmp
, &f
->list
, data
.flr
.node
) {
305 apply_constraint(req
, PM_QOS_REMOVE_REQ
, PM_QOS_DEFAULT_VALUE
);
306 memset(req
, 0, sizeof(*req
));
309 spin_lock_irq(&dev
->power
.lock
);
310 dev
->power
.qos
= ERR_PTR(-ENODEV
);
311 spin_unlock_irq(&dev
->power
.lock
);
313 kfree(qos
->resume_latency
.notifiers
);
317 mutex_unlock(&dev_pm_qos_mtx
);
319 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
322 static bool dev_pm_qos_invalid_req_type(struct device
*dev
,
323 enum dev_pm_qos_req_type type
)
325 return type
== DEV_PM_QOS_LATENCY_TOLERANCE
&&
326 !dev
->power
.set_latency_tolerance
;
329 static int __dev_pm_qos_add_request(struct device
*dev
,
330 struct dev_pm_qos_request
*req
,
331 enum dev_pm_qos_req_type type
, s32 value
)
335 if (!dev
|| !req
|| dev_pm_qos_invalid_req_type(dev
, type
))
338 if (WARN(dev_pm_qos_request_active(req
),
339 "%s() called for already added request\n", __func__
))
342 if (IS_ERR(dev
->power
.qos
))
344 else if (!dev
->power
.qos
)
345 ret
= dev_pm_qos_constraints_allocate(dev
);
347 trace_dev_pm_qos_add_request(dev_name(dev
), type
, value
);
353 if (req
->type
== DEV_PM_QOS_MIN_FREQUENCY
)
354 ret
= freq_qos_add_request(&dev
->power
.qos
->freq
,
356 FREQ_QOS_MIN
, value
);
357 else if (req
->type
== DEV_PM_QOS_MAX_FREQUENCY
)
358 ret
= freq_qos_add_request(&dev
->power
.qos
->freq
,
360 FREQ_QOS_MAX
, value
);
362 ret
= apply_constraint(req
, PM_QOS_ADD_REQ
, value
);
368 * dev_pm_qos_add_request - inserts new qos request into the list
369 * @dev: target device for the constraint
370 * @req: pointer to a preallocated handle
371 * @type: type of the request
372 * @value: defines the qos request
374 * This function inserts a new entry in the device constraints list of
375 * requested qos performance characteristics. It recomputes the aggregate
376 * QoS expectations of parameters and initializes the dev_pm_qos_request
377 * handle. Caller needs to save this handle for later use in updates and
380 * Returns 1 if the aggregated constraint value has changed,
381 * 0 if the aggregated constraint value has not changed,
382 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
383 * to allocate for data structures, -ENODEV if the device has just been removed
386 * Callers should ensure that the target device is not RPM_SUSPENDED before
387 * using this function for requests of type DEV_PM_QOS_FLAGS.
389 int dev_pm_qos_add_request(struct device
*dev
, struct dev_pm_qos_request
*req
,
390 enum dev_pm_qos_req_type type
, s32 value
)
394 mutex_lock(&dev_pm_qos_mtx
);
395 ret
= __dev_pm_qos_add_request(dev
, req
, type
, value
);
396 mutex_unlock(&dev_pm_qos_mtx
);
399 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request
);
402 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
403 * @req : PM QoS request to modify.
404 * @new_value: New value to request.
406 static int __dev_pm_qos_update_request(struct dev_pm_qos_request
*req
,
412 if (!req
) /*guard against callers passing in null */
415 if (WARN(!dev_pm_qos_request_active(req
),
416 "%s() called for unknown object\n", __func__
))
419 if (IS_ERR_OR_NULL(req
->dev
->power
.qos
))
423 case DEV_PM_QOS_RESUME_LATENCY
:
424 case DEV_PM_QOS_LATENCY_TOLERANCE
:
425 curr_value
= req
->data
.pnode
.prio
;
427 case DEV_PM_QOS_MIN_FREQUENCY
:
428 case DEV_PM_QOS_MAX_FREQUENCY
:
429 curr_value
= req
->data
.freq
.pnode
.prio
;
431 case DEV_PM_QOS_FLAGS
:
432 curr_value
= req
->data
.flr
.flags
;
438 trace_dev_pm_qos_update_request(dev_name(req
->dev
), req
->type
,
440 if (curr_value
!= new_value
)
441 ret
= apply_constraint(req
, PM_QOS_UPDATE_REQ
, new_value
);
447 * dev_pm_qos_update_request - modifies an existing qos request
448 * @req : handle to list element holding a dev_pm_qos request to use
449 * @new_value: defines the qos request
451 * Updates an existing dev PM qos request along with updating the
454 * Attempts are made to make this code callable on hot code paths.
456 * Returns 1 if the aggregated constraint value has changed,
457 * 0 if the aggregated constraint value has not changed,
458 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
459 * removed from the system
461 * Callers should ensure that the target device is not RPM_SUSPENDED before
462 * using this function for requests of type DEV_PM_QOS_FLAGS.
464 int dev_pm_qos_update_request(struct dev_pm_qos_request
*req
, s32 new_value
)
468 mutex_lock(&dev_pm_qos_mtx
);
469 ret
= __dev_pm_qos_update_request(req
, new_value
);
470 mutex_unlock(&dev_pm_qos_mtx
);
473 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request
);
475 static int __dev_pm_qos_remove_request(struct dev_pm_qos_request
*req
)
479 if (!req
) /*guard against callers passing in null */
482 if (WARN(!dev_pm_qos_request_active(req
),
483 "%s() called for unknown object\n", __func__
))
486 if (IS_ERR_OR_NULL(req
->dev
->power
.qos
))
489 trace_dev_pm_qos_remove_request(dev_name(req
->dev
), req
->type
,
490 PM_QOS_DEFAULT_VALUE
);
491 ret
= apply_constraint(req
, PM_QOS_REMOVE_REQ
, PM_QOS_DEFAULT_VALUE
);
492 memset(req
, 0, sizeof(*req
));
497 * dev_pm_qos_remove_request - modifies an existing qos request
498 * @req: handle to request list element
500 * Will remove pm qos request from the list of constraints and
501 * recompute the current target value. Call this on slow code paths.
503 * Returns 1 if the aggregated constraint value has changed,
504 * 0 if the aggregated constraint value has not changed,
505 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
506 * removed from the system
508 * Callers should ensure that the target device is not RPM_SUSPENDED before
509 * using this function for requests of type DEV_PM_QOS_FLAGS.
511 int dev_pm_qos_remove_request(struct dev_pm_qos_request
*req
)
515 mutex_lock(&dev_pm_qos_mtx
);
516 ret
= __dev_pm_qos_remove_request(req
);
517 mutex_unlock(&dev_pm_qos_mtx
);
520 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request
);
523 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
524 * of per-device PM QoS constraints
526 * @dev: target device for the constraint
527 * @notifier: notifier block managed by caller.
528 * @type: request type.
530 * Will register the notifier into a notification chain that gets called
531 * upon changes to the target value for the device.
533 * If the device's constraints object doesn't exist when this routine is called,
534 * it will be created (or error code will be returned if that fails).
536 int dev_pm_qos_add_notifier(struct device
*dev
, struct notifier_block
*notifier
,
537 enum dev_pm_qos_req_type type
)
541 mutex_lock(&dev_pm_qos_mtx
);
543 if (IS_ERR(dev
->power
.qos
))
545 else if (!dev
->power
.qos
)
546 ret
= dev_pm_qos_constraints_allocate(dev
);
552 case DEV_PM_QOS_RESUME_LATENCY
:
553 ret
= blocking_notifier_chain_register(dev
->power
.qos
->resume_latency
.notifiers
,
556 case DEV_PM_QOS_MIN_FREQUENCY
:
557 ret
= freq_qos_add_notifier(&dev
->power
.qos
->freq
,
558 FREQ_QOS_MIN
, notifier
);
560 case DEV_PM_QOS_MAX_FREQUENCY
:
561 ret
= freq_qos_add_notifier(&dev
->power
.qos
->freq
,
562 FREQ_QOS_MAX
, notifier
);
570 mutex_unlock(&dev_pm_qos_mtx
);
573 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier
);
576 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
577 * of per-device PM QoS constraints
579 * @dev: target device for the constraint
580 * @notifier: notifier block to be removed.
581 * @type: request type.
583 * Will remove the notifier from the notification chain that gets called
584 * upon changes to the target value.
586 int dev_pm_qos_remove_notifier(struct device
*dev
,
587 struct notifier_block
*notifier
,
588 enum dev_pm_qos_req_type type
)
592 mutex_lock(&dev_pm_qos_mtx
);
594 /* Silently return if the constraints object is not present. */
595 if (IS_ERR_OR_NULL(dev
->power
.qos
))
599 case DEV_PM_QOS_RESUME_LATENCY
:
600 ret
= blocking_notifier_chain_unregister(dev
->power
.qos
->resume_latency
.notifiers
,
603 case DEV_PM_QOS_MIN_FREQUENCY
:
604 ret
= freq_qos_remove_notifier(&dev
->power
.qos
->freq
,
605 FREQ_QOS_MIN
, notifier
);
607 case DEV_PM_QOS_MAX_FREQUENCY
:
608 ret
= freq_qos_remove_notifier(&dev
->power
.qos
->freq
,
609 FREQ_QOS_MAX
, notifier
);
617 mutex_unlock(&dev_pm_qos_mtx
);
620 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier
);
623 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
624 * @dev: Device whose ancestor to add the request for.
625 * @req: Pointer to the preallocated handle.
626 * @type: Type of the request.
627 * @value: Constraint latency value.
629 int dev_pm_qos_add_ancestor_request(struct device
*dev
,
630 struct dev_pm_qos_request
*req
,
631 enum dev_pm_qos_req_type type
, s32 value
)
633 struct device
*ancestor
= dev
->parent
;
637 case DEV_PM_QOS_RESUME_LATENCY
:
638 while (ancestor
&& !ancestor
->power
.ignore_children
)
639 ancestor
= ancestor
->parent
;
642 case DEV_PM_QOS_LATENCY_TOLERANCE
:
643 while (ancestor
&& !ancestor
->power
.set_latency_tolerance
)
644 ancestor
= ancestor
->parent
;
651 ret
= dev_pm_qos_add_request(ancestor
, req
, type
, value
);
658 EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request
);
660 static void __dev_pm_qos_drop_user_request(struct device
*dev
,
661 enum dev_pm_qos_req_type type
)
663 struct dev_pm_qos_request
*req
= NULL
;
666 case DEV_PM_QOS_RESUME_LATENCY
:
667 req
= dev
->power
.qos
->resume_latency_req
;
668 dev
->power
.qos
->resume_latency_req
= NULL
;
670 case DEV_PM_QOS_LATENCY_TOLERANCE
:
671 req
= dev
->power
.qos
->latency_tolerance_req
;
672 dev
->power
.qos
->latency_tolerance_req
= NULL
;
674 case DEV_PM_QOS_FLAGS
:
675 req
= dev
->power
.qos
->flags_req
;
676 dev
->power
.qos
->flags_req
= NULL
;
682 __dev_pm_qos_remove_request(req
);
686 static void dev_pm_qos_drop_user_request(struct device
*dev
,
687 enum dev_pm_qos_req_type type
)
689 mutex_lock(&dev_pm_qos_mtx
);
690 __dev_pm_qos_drop_user_request(dev
, type
);
691 mutex_unlock(&dev_pm_qos_mtx
);
695 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
696 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
697 * @value: Initial value of the latency limit.
699 int dev_pm_qos_expose_latency_limit(struct device
*dev
, s32 value
)
701 struct dev_pm_qos_request
*req
;
704 if (!device_is_registered(dev
) || value
< 0)
707 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
711 ret
= dev_pm_qos_add_request(dev
, req
, DEV_PM_QOS_RESUME_LATENCY
, value
);
717 mutex_lock(&dev_pm_qos_sysfs_mtx
);
719 mutex_lock(&dev_pm_qos_mtx
);
721 if (IS_ERR_OR_NULL(dev
->power
.qos
))
723 else if (dev
->power
.qos
->resume_latency_req
)
727 __dev_pm_qos_remove_request(req
);
729 mutex_unlock(&dev_pm_qos_mtx
);
732 dev
->power
.qos
->resume_latency_req
= req
;
734 mutex_unlock(&dev_pm_qos_mtx
);
736 ret
= pm_qos_sysfs_add_resume_latency(dev
);
738 dev_pm_qos_drop_user_request(dev
, DEV_PM_QOS_RESUME_LATENCY
);
741 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
744 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit
);
746 static void __dev_pm_qos_hide_latency_limit(struct device
*dev
)
748 if (!IS_ERR_OR_NULL(dev
->power
.qos
) && dev
->power
.qos
->resume_latency_req
)
749 __dev_pm_qos_drop_user_request(dev
, DEV_PM_QOS_RESUME_LATENCY
);
753 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
754 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
756 void dev_pm_qos_hide_latency_limit(struct device
*dev
)
758 mutex_lock(&dev_pm_qos_sysfs_mtx
);
760 pm_qos_sysfs_remove_resume_latency(dev
);
762 mutex_lock(&dev_pm_qos_mtx
);
763 __dev_pm_qos_hide_latency_limit(dev
);
764 mutex_unlock(&dev_pm_qos_mtx
);
766 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
768 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit
);
771 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
772 * @dev: Device whose PM QoS flags are to be exposed to user space.
773 * @val: Initial values of the flags.
775 int dev_pm_qos_expose_flags(struct device
*dev
, s32 val
)
777 struct dev_pm_qos_request
*req
;
780 if (!device_is_registered(dev
))
783 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
787 ret
= dev_pm_qos_add_request(dev
, req
, DEV_PM_QOS_FLAGS
, val
);
793 pm_runtime_get_sync(dev
);
794 mutex_lock(&dev_pm_qos_sysfs_mtx
);
796 mutex_lock(&dev_pm_qos_mtx
);
798 if (IS_ERR_OR_NULL(dev
->power
.qos
))
800 else if (dev
->power
.qos
->flags_req
)
804 __dev_pm_qos_remove_request(req
);
806 mutex_unlock(&dev_pm_qos_mtx
);
809 dev
->power
.qos
->flags_req
= req
;
811 mutex_unlock(&dev_pm_qos_mtx
);
813 ret
= pm_qos_sysfs_add_flags(dev
);
815 dev_pm_qos_drop_user_request(dev
, DEV_PM_QOS_FLAGS
);
818 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
822 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags
);
824 static void __dev_pm_qos_hide_flags(struct device
*dev
)
826 if (!IS_ERR_OR_NULL(dev
->power
.qos
) && dev
->power
.qos
->flags_req
)
827 __dev_pm_qos_drop_user_request(dev
, DEV_PM_QOS_FLAGS
);
831 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
832 * @dev: Device whose PM QoS flags are to be hidden from user space.
834 void dev_pm_qos_hide_flags(struct device
*dev
)
836 pm_runtime_get_sync(dev
);
837 mutex_lock(&dev_pm_qos_sysfs_mtx
);
839 pm_qos_sysfs_remove_flags(dev
);
841 mutex_lock(&dev_pm_qos_mtx
);
842 __dev_pm_qos_hide_flags(dev
);
843 mutex_unlock(&dev_pm_qos_mtx
);
845 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
848 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags
);
851 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
852 * @dev: Device to update the PM QoS flags request for.
853 * @mask: Flags to set/clear.
854 * @set: Whether to set or clear the flags (true means set).
856 int dev_pm_qos_update_flags(struct device
*dev
, s32 mask
, bool set
)
861 pm_runtime_get_sync(dev
);
862 mutex_lock(&dev_pm_qos_mtx
);
864 if (IS_ERR_OR_NULL(dev
->power
.qos
) || !dev
->power
.qos
->flags_req
) {
869 value
= dev_pm_qos_requested_flags(dev
);
875 ret
= __dev_pm_qos_update_request(dev
->power
.qos
->flags_req
, value
);
878 mutex_unlock(&dev_pm_qos_mtx
);
884 * dev_pm_qos_get_user_latency_tolerance - Get user space latency tolerance.
885 * @dev: Device to obtain the user space latency tolerance for.
887 s32
dev_pm_qos_get_user_latency_tolerance(struct device
*dev
)
891 mutex_lock(&dev_pm_qos_mtx
);
892 ret
= IS_ERR_OR_NULL(dev
->power
.qos
)
893 || !dev
->power
.qos
->latency_tolerance_req
?
894 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT
:
895 dev
->power
.qos
->latency_tolerance_req
->data
.pnode
.prio
;
896 mutex_unlock(&dev_pm_qos_mtx
);
901 * dev_pm_qos_update_user_latency_tolerance - Update user space latency tolerance.
902 * @dev: Device to update the user space latency tolerance for.
903 * @val: New user space latency tolerance for @dev (negative values disable).
905 int dev_pm_qos_update_user_latency_tolerance(struct device
*dev
, s32 val
)
909 mutex_lock(&dev_pm_qos_mtx
);
911 if (IS_ERR_OR_NULL(dev
->power
.qos
)
912 || !dev
->power
.qos
->latency_tolerance_req
) {
913 struct dev_pm_qos_request
*req
;
916 if (val
== PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT
)
922 req
= kzalloc(sizeof(*req
), GFP_KERNEL
);
927 ret
= __dev_pm_qos_add_request(dev
, req
, DEV_PM_QOS_LATENCY_TOLERANCE
, val
);
932 dev
->power
.qos
->latency_tolerance_req
= req
;
935 __dev_pm_qos_drop_user_request(dev
, DEV_PM_QOS_LATENCY_TOLERANCE
);
938 ret
= __dev_pm_qos_update_request(dev
->power
.qos
->latency_tolerance_req
, val
);
943 mutex_unlock(&dev_pm_qos_mtx
);
946 EXPORT_SYMBOL_GPL(dev_pm_qos_update_user_latency_tolerance
);
949 * dev_pm_qos_expose_latency_tolerance - Expose latency tolerance to userspace
950 * @dev: Device whose latency tolerance to expose
952 int dev_pm_qos_expose_latency_tolerance(struct device
*dev
)
956 if (!dev
->power
.set_latency_tolerance
)
959 mutex_lock(&dev_pm_qos_sysfs_mtx
);
960 ret
= pm_qos_sysfs_add_latency_tolerance(dev
);
961 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
965 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_tolerance
);
968 * dev_pm_qos_hide_latency_tolerance - Hide latency tolerance from userspace
969 * @dev: Device whose latency tolerance to hide
971 void dev_pm_qos_hide_latency_tolerance(struct device
*dev
)
973 mutex_lock(&dev_pm_qos_sysfs_mtx
);
974 pm_qos_sysfs_remove_latency_tolerance(dev
);
975 mutex_unlock(&dev_pm_qos_sysfs_mtx
);
977 /* Remove the request from user space now */
978 pm_runtime_get_sync(dev
);
979 dev_pm_qos_update_user_latency_tolerance(dev
,
980 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT
);
983 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_tolerance
);