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 : pm_qos_read_value(&qos->min_frequency);
127 case DEV_PM_QOS_MAX_FREQUENCY:
128 ret = IS_ERR_OR_NULL(qos) ? PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE
129 : pm_qos_read_value(&qos->max_frequency);
136 spin_unlock_irqrestore(&dev->power.lock, flags);
142 * apply_constraint - Add/modify/remove device PM QoS request.
143 * @req: Constraint request to apply
144 * @action: Action to perform (add/update/remove).
145 * @value: Value to assign to the QoS request.
147 * Internal function to update the constraints list using the PM QoS core
148 * code and if needed call the per-device callbacks.
150 static int apply_constraint(struct dev_pm_qos_request *req,
151 enum pm_qos_req_action action, s32 value)
153 struct dev_pm_qos *qos = req->dev->power.qos;
157 case DEV_PM_QOS_RESUME_LATENCY:
158 if (WARN_ON(action != PM_QOS_REMOVE_REQ && value < 0))
161 ret = pm_qos_update_target(&qos->resume_latency,
162 &req->data.pnode, action, value);
164 case DEV_PM_QOS_LATENCY_TOLERANCE:
165 ret = pm_qos_update_target(&qos->latency_tolerance,
166 &req->data.pnode, action, value);
168 value = pm_qos_read_value(&qos->latency_tolerance);
169 req->dev->power.set_latency_tolerance(req->dev, value);
172 case DEV_PM_QOS_MIN_FREQUENCY:
173 ret = pm_qos_update_target(&qos->min_frequency,
174 &req->data.pnode, action, value);
176 case DEV_PM_QOS_MAX_FREQUENCY:
177 ret = pm_qos_update_target(&qos->max_frequency,
178 &req->data.pnode, action, value);
180 case DEV_PM_QOS_FLAGS:
181 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
192 * dev_pm_qos_constraints_allocate
193 * @dev: device to allocate data for
195 * Called at the first call to add_request, for constraint data allocation
196 * Must be called with the dev_pm_qos_mtx mutex held
198 static int dev_pm_qos_constraints_allocate(struct device *dev)
200 struct dev_pm_qos *qos;
201 struct pm_qos_constraints *c;
202 struct blocking_notifier_head *n;
204 qos = kzalloc(sizeof(*qos), GFP_KERNEL);
208 n = kzalloc(3 * sizeof(*n), GFP_KERNEL);
214 c = &qos->resume_latency;
215 plist_head_init(&c->list);
216 c->target_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
217 c->default_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
218 c->no_constraint_value = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
219 c->type = PM_QOS_MIN;
221 BLOCKING_INIT_NOTIFIER_HEAD(n);
223 c = &qos->latency_tolerance;
224 plist_head_init(&c->list);
225 c->target_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
226 c->default_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
227 c->no_constraint_value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
228 c->type = PM_QOS_MIN;
230 c = &qos->min_frequency;
231 plist_head_init(&c->list);
232 c->target_value = PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE;
233 c->default_value = PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE;
234 c->no_constraint_value = PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE;
235 c->type = PM_QOS_MAX;
237 BLOCKING_INIT_NOTIFIER_HEAD(n);
239 c = &qos->max_frequency;
240 plist_head_init(&c->list);
241 c->target_value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
242 c->default_value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
243 c->no_constraint_value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
244 c->type = PM_QOS_MIN;
246 BLOCKING_INIT_NOTIFIER_HEAD(n);
248 INIT_LIST_HEAD(&qos->flags.list);
250 spin_lock_irq(&dev->power.lock);
251 dev->power.qos = qos;
252 spin_unlock_irq(&dev->power.lock);
257 static void __dev_pm_qos_hide_latency_limit(struct device *dev);
258 static void __dev_pm_qos_hide_flags(struct device *dev);
261 * dev_pm_qos_constraints_destroy
262 * @dev: target device
264 * Called from the device PM subsystem on device removal under device_pm_lock().
266 void dev_pm_qos_constraints_destroy(struct device *dev)
268 struct dev_pm_qos *qos;
269 struct dev_pm_qos_request *req, *tmp;
270 struct pm_qos_constraints *c;
271 struct pm_qos_flags *f;
273 mutex_lock(&dev_pm_qos_sysfs_mtx);
276 * If the device's PM QoS resume latency limit or PM QoS flags have been
277 * exposed to user space, they have to be hidden at this point.
279 pm_qos_sysfs_remove_resume_latency(dev);
280 pm_qos_sysfs_remove_flags(dev);
282 mutex_lock(&dev_pm_qos_mtx);
284 __dev_pm_qos_hide_latency_limit(dev);
285 __dev_pm_qos_hide_flags(dev);
287 qos = dev->power.qos;
291 /* Flush the constraints lists for the device. */
292 c = &qos->resume_latency;
293 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
295 * Update constraints list and call the notification
296 * callbacks if needed
298 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
299 memset(req, 0, sizeof(*req));
302 c = &qos->latency_tolerance;
303 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
304 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
305 memset(req, 0, sizeof(*req));
308 c = &qos->min_frequency;
309 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
310 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_MIN_FREQUENCY_DEFAULT_VALUE);
311 memset(req, 0, sizeof(*req));
314 c = &qos->max_frequency;
315 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
316 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
317 memset(req, 0, sizeof(*req));
321 list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
322 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
323 memset(req, 0, sizeof(*req));
326 spin_lock_irq(&dev->power.lock);
327 dev->power.qos = ERR_PTR(-ENODEV);
328 spin_unlock_irq(&dev->power.lock);
330 kfree(qos->resume_latency.notifiers);
334 mutex_unlock(&dev_pm_qos_mtx);
336 mutex_unlock(&dev_pm_qos_sysfs_mtx);
339 static bool dev_pm_qos_invalid_req_type(struct device *dev,
340 enum dev_pm_qos_req_type type)
342 return type == DEV_PM_QOS_LATENCY_TOLERANCE &&
343 !dev->power.set_latency_tolerance;
346 static int __dev_pm_qos_add_request(struct device *dev,
347 struct dev_pm_qos_request *req,
348 enum dev_pm_qos_req_type type, s32 value)
352 if (!dev || !req || dev_pm_qos_invalid_req_type(dev, type))
355 if (WARN(dev_pm_qos_request_active(req),
356 "%s() called for already added request\n", __func__))
359 if (IS_ERR(dev->power.qos))
361 else if (!dev->power.qos)
362 ret = dev_pm_qos_constraints_allocate(dev);
364 trace_dev_pm_qos_add_request(dev_name(dev), type, value);
368 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
374 * dev_pm_qos_add_request - inserts new qos request into the list
375 * @dev: target device for the constraint
376 * @req: pointer to a preallocated handle
377 * @type: type of the request
378 * @value: defines the qos request
380 * This function inserts a new entry in the device constraints list of
381 * requested qos performance characteristics. It recomputes the aggregate
382 * QoS expectations of parameters and initializes the dev_pm_qos_request
383 * handle. Caller needs to save this handle for later use in updates and
386 * Returns 1 if the aggregated constraint value has changed,
387 * 0 if the aggregated constraint value has not changed,
388 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
389 * to allocate for data structures, -ENODEV if the device has just been removed
392 * Callers should ensure that the target device is not RPM_SUSPENDED before
393 * using this function for requests of type DEV_PM_QOS_FLAGS.
395 int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
396 enum dev_pm_qos_req_type type, s32 value)
400 mutex_lock(&dev_pm_qos_mtx);
401 ret = __dev_pm_qos_add_request(dev, req, type, value);
402 mutex_unlock(&dev_pm_qos_mtx);
405 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
408 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
409 * @req : PM QoS request to modify.
410 * @new_value: New value to request.
412 static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
418 if (!req) /*guard against callers passing in null */
421 if (WARN(!dev_pm_qos_request_active(req),
422 "%s() called for unknown object\n", __func__))
425 if (IS_ERR_OR_NULL(req->dev->power.qos))
429 case DEV_PM_QOS_RESUME_LATENCY:
430 case DEV_PM_QOS_LATENCY_TOLERANCE:
431 case DEV_PM_QOS_MIN_FREQUENCY:
432 case DEV_PM_QOS_MAX_FREQUENCY:
433 curr_value = req->data.pnode.prio;
435 case DEV_PM_QOS_FLAGS:
436 curr_value = req->data.flr.flags;
442 trace_dev_pm_qos_update_request(dev_name(req->dev), req->type,
444 if (curr_value != new_value)
445 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
451 * dev_pm_qos_update_request - modifies an existing qos request
452 * @req : handle to list element holding a dev_pm_qos request to use
453 * @new_value: defines the qos request
455 * Updates an existing dev PM qos request along with updating the
458 * Attempts are made to make this code callable on hot code paths.
460 * Returns 1 if the aggregated constraint value has changed,
461 * 0 if the aggregated constraint value has not changed,
462 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
463 * removed from the system
465 * Callers should ensure that the target device is not RPM_SUSPENDED before
466 * using this function for requests of type DEV_PM_QOS_FLAGS.
468 int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
472 mutex_lock(&dev_pm_qos_mtx);
473 ret = __dev_pm_qos_update_request(req, new_value);
474 mutex_unlock(&dev_pm_qos_mtx);
477 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
479 static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
483 if (!req) /*guard against callers passing in null */
486 if (WARN(!dev_pm_qos_request_active(req),
487 "%s() called for unknown object\n", __func__))
490 if (IS_ERR_OR_NULL(req->dev->power.qos))
493 trace_dev_pm_qos_remove_request(dev_name(req->dev), req->type,
494 PM_QOS_DEFAULT_VALUE);
495 ret = apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
496 memset(req, 0, sizeof(*req));
501 * dev_pm_qos_remove_request - modifies an existing qos request
502 * @req: handle to request list element
504 * Will remove pm qos request from the list of constraints and
505 * recompute the current target value. Call this on slow code paths.
507 * Returns 1 if the aggregated constraint value has changed,
508 * 0 if the aggregated constraint value has not changed,
509 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
510 * removed from the system
512 * Callers should ensure that the target device is not RPM_SUSPENDED before
513 * using this function for requests of type DEV_PM_QOS_FLAGS.
515 int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
519 mutex_lock(&dev_pm_qos_mtx);
520 ret = __dev_pm_qos_remove_request(req);
521 mutex_unlock(&dev_pm_qos_mtx);
524 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
527 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
528 * of per-device PM QoS constraints
530 * @dev: target device for the constraint
531 * @notifier: notifier block managed by caller.
532 * @type: request type.
534 * Will register the notifier into a notification chain that gets called
535 * upon changes to the target value for the device.
537 * If the device's constraints object doesn't exist when this routine is called,
538 * it will be created (or error code will be returned if that fails).
540 int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier,
541 enum dev_pm_qos_req_type type)
545 mutex_lock(&dev_pm_qos_mtx);
547 if (IS_ERR(dev->power.qos))
549 else if (!dev->power.qos)
550 ret = dev_pm_qos_constraints_allocate(dev);
556 case DEV_PM_QOS_RESUME_LATENCY:
557 ret = blocking_notifier_chain_register(dev->power.qos->resume_latency.notifiers,
560 case DEV_PM_QOS_MIN_FREQUENCY:
561 ret = blocking_notifier_chain_register(dev->power.qos->min_frequency.notifiers,
564 case DEV_PM_QOS_MAX_FREQUENCY:
565 ret = blocking_notifier_chain_register(dev->power.qos->max_frequency.notifiers,
574 mutex_unlock(&dev_pm_qos_mtx);
577 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
580 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
581 * of per-device PM QoS constraints
583 * @dev: target device for the constraint
584 * @notifier: notifier block to be removed.
585 * @type: request type.
587 * Will remove the notifier from the notification chain that gets called
588 * upon changes to the target value.
590 int dev_pm_qos_remove_notifier(struct device *dev,
591 struct notifier_block *notifier,
592 enum dev_pm_qos_req_type type)
596 mutex_lock(&dev_pm_qos_mtx);
598 /* Silently return if the constraints object is not present. */
599 if (IS_ERR_OR_NULL(dev->power.qos))
603 case DEV_PM_QOS_RESUME_LATENCY:
604 ret = blocking_notifier_chain_unregister(dev->power.qos->resume_latency.notifiers,
607 case DEV_PM_QOS_MIN_FREQUENCY:
608 ret = blocking_notifier_chain_unregister(dev->power.qos->min_frequency.notifiers,
611 case DEV_PM_QOS_MAX_FREQUENCY:
612 ret = blocking_notifier_chain_unregister(dev->power.qos->max_frequency.notifiers,
621 mutex_unlock(&dev_pm_qos_mtx);
624 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
627 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
628 * @dev: Device whose ancestor to add the request for.
629 * @req: Pointer to the preallocated handle.
630 * @type: Type of the request.
631 * @value: Constraint latency value.
633 int dev_pm_qos_add_ancestor_request(struct device *dev,
634 struct dev_pm_qos_request *req,
635 enum dev_pm_qos_req_type type, s32 value)
637 struct device *ancestor = dev->parent;
641 case DEV_PM_QOS_RESUME_LATENCY:
642 while (ancestor && !ancestor->power.ignore_children)
643 ancestor = ancestor->parent;
646 case DEV_PM_QOS_LATENCY_TOLERANCE:
647 while (ancestor && !ancestor->power.set_latency_tolerance)
648 ancestor = ancestor->parent;
655 ret = dev_pm_qos_add_request(ancestor, req, type, value);
662 EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
664 static void __dev_pm_qos_drop_user_request(struct device *dev,
665 enum dev_pm_qos_req_type type)
667 struct dev_pm_qos_request *req = NULL;
670 case DEV_PM_QOS_RESUME_LATENCY:
671 req = dev->power.qos->resume_latency_req;
672 dev->power.qos->resume_latency_req = NULL;
674 case DEV_PM_QOS_LATENCY_TOLERANCE:
675 req = dev->power.qos->latency_tolerance_req;
676 dev->power.qos->latency_tolerance_req = NULL;
678 case DEV_PM_QOS_FLAGS:
679 req = dev->power.qos->flags_req;
680 dev->power.qos->flags_req = NULL;
686 __dev_pm_qos_remove_request(req);
690 static void dev_pm_qos_drop_user_request(struct device *dev,
691 enum dev_pm_qos_req_type type)
693 mutex_lock(&dev_pm_qos_mtx);
694 __dev_pm_qos_drop_user_request(dev, type);
695 mutex_unlock(&dev_pm_qos_mtx);
699 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
700 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
701 * @value: Initial value of the latency limit.
703 int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
705 struct dev_pm_qos_request *req;
708 if (!device_is_registered(dev) || value < 0)
711 req = kzalloc(sizeof(*req), GFP_KERNEL);
715 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_RESUME_LATENCY, value);
721 mutex_lock(&dev_pm_qos_sysfs_mtx);
723 mutex_lock(&dev_pm_qos_mtx);
725 if (IS_ERR_OR_NULL(dev->power.qos))
727 else if (dev->power.qos->resume_latency_req)
731 __dev_pm_qos_remove_request(req);
733 mutex_unlock(&dev_pm_qos_mtx);
736 dev->power.qos->resume_latency_req = req;
738 mutex_unlock(&dev_pm_qos_mtx);
740 ret = pm_qos_sysfs_add_resume_latency(dev);
742 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
745 mutex_unlock(&dev_pm_qos_sysfs_mtx);
748 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
750 static void __dev_pm_qos_hide_latency_limit(struct device *dev)
752 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->resume_latency_req)
753 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
757 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
758 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
760 void dev_pm_qos_hide_latency_limit(struct device *dev)
762 mutex_lock(&dev_pm_qos_sysfs_mtx);
764 pm_qos_sysfs_remove_resume_latency(dev);
766 mutex_lock(&dev_pm_qos_mtx);
767 __dev_pm_qos_hide_latency_limit(dev);
768 mutex_unlock(&dev_pm_qos_mtx);
770 mutex_unlock(&dev_pm_qos_sysfs_mtx);
772 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
775 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
776 * @dev: Device whose PM QoS flags are to be exposed to user space.
777 * @val: Initial values of the flags.
779 int dev_pm_qos_expose_flags(struct device *dev, s32 val)
781 struct dev_pm_qos_request *req;
784 if (!device_is_registered(dev))
787 req = kzalloc(sizeof(*req), GFP_KERNEL);
791 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
797 pm_runtime_get_sync(dev);
798 mutex_lock(&dev_pm_qos_sysfs_mtx);
800 mutex_lock(&dev_pm_qos_mtx);
802 if (IS_ERR_OR_NULL(dev->power.qos))
804 else if (dev->power.qos->flags_req)
808 __dev_pm_qos_remove_request(req);
810 mutex_unlock(&dev_pm_qos_mtx);
813 dev->power.qos->flags_req = req;
815 mutex_unlock(&dev_pm_qos_mtx);
817 ret = pm_qos_sysfs_add_flags(dev);
819 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
822 mutex_unlock(&dev_pm_qos_sysfs_mtx);
826 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
828 static void __dev_pm_qos_hide_flags(struct device *dev)
830 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req)
831 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
835 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
836 * @dev: Device whose PM QoS flags are to be hidden from user space.
838 void dev_pm_qos_hide_flags(struct device *dev)
840 pm_runtime_get_sync(dev);
841 mutex_lock(&dev_pm_qos_sysfs_mtx);
843 pm_qos_sysfs_remove_flags(dev);
845 mutex_lock(&dev_pm_qos_mtx);
846 __dev_pm_qos_hide_flags(dev);
847 mutex_unlock(&dev_pm_qos_mtx);
849 mutex_unlock(&dev_pm_qos_sysfs_mtx);
852 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
855 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
856 * @dev: Device to update the PM QoS flags request for.
857 * @mask: Flags to set/clear.
858 * @set: Whether to set or clear the flags (true means set).
860 int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
865 pm_runtime_get_sync(dev);
866 mutex_lock(&dev_pm_qos_mtx);
868 if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) {
873 value = dev_pm_qos_requested_flags(dev);
879 ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
882 mutex_unlock(&dev_pm_qos_mtx);
888 * dev_pm_qos_get_user_latency_tolerance - Get user space latency tolerance.
889 * @dev: Device to obtain the user space latency tolerance for.
891 s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev)
895 mutex_lock(&dev_pm_qos_mtx);
896 ret = IS_ERR_OR_NULL(dev->power.qos)
897 || !dev->power.qos->latency_tolerance_req ?
898 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT :
899 dev->power.qos->latency_tolerance_req->data.pnode.prio;
900 mutex_unlock(&dev_pm_qos_mtx);
905 * dev_pm_qos_update_user_latency_tolerance - Update user space latency tolerance.
906 * @dev: Device to update the user space latency tolerance for.
907 * @val: New user space latency tolerance for @dev (negative values disable).
909 int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val)
913 mutex_lock(&dev_pm_qos_mtx);
915 if (IS_ERR_OR_NULL(dev->power.qos)
916 || !dev->power.qos->latency_tolerance_req) {
917 struct dev_pm_qos_request *req;
920 if (val == PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT)
926 req = kzalloc(sizeof(*req), GFP_KERNEL);
931 ret = __dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY_TOLERANCE, val);
936 dev->power.qos->latency_tolerance_req = req;
939 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY_TOLERANCE);
942 ret = __dev_pm_qos_update_request(dev->power.qos->latency_tolerance_req, val);
947 mutex_unlock(&dev_pm_qos_mtx);
950 EXPORT_SYMBOL_GPL(dev_pm_qos_update_user_latency_tolerance);
953 * dev_pm_qos_expose_latency_tolerance - Expose latency tolerance to userspace
954 * @dev: Device whose latency tolerance to expose
956 int dev_pm_qos_expose_latency_tolerance(struct device *dev)
960 if (!dev->power.set_latency_tolerance)
963 mutex_lock(&dev_pm_qos_sysfs_mtx);
964 ret = pm_qos_sysfs_add_latency_tolerance(dev);
965 mutex_unlock(&dev_pm_qos_sysfs_mtx);
969 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_tolerance);
972 * dev_pm_qos_hide_latency_tolerance - Hide latency tolerance from userspace
973 * @dev: Device whose latency tolerance to hide
975 void dev_pm_qos_hide_latency_tolerance(struct device *dev)
977 mutex_lock(&dev_pm_qos_sysfs_mtx);
978 pm_qos_sysfs_remove_latency_tolerance(dev);
979 mutex_unlock(&dev_pm_qos_sysfs_mtx);
981 /* Remove the request from user space now */
982 pm_runtime_get_sync(dev);
983 dev_pm_qos_update_user_latency_tolerance(dev,
984 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT);
987 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_tolerance);