cpuidle: menu: Avoid taking spinlock for accessing QoS values
[linux/fpc-iii.git] / drivers / base / power / qos.c
blob73142d0862403702aaee1bd5a16e5669e40907d9
1 /*
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
13 * of:
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
23 * data struct.
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
29 * dev_pm_info.
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>
47 #include "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);
54 /**
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;
65 s32 val;
67 lockdep_assert_held(&dev->power.lock);
69 if (IS_ERR_OR_NULL(qos))
70 return PM_QOS_FLAGS_UNDEFINED;
72 pqf = &qos->flags;
73 if (list_empty(&pqf->list))
74 return PM_QOS_FLAGS_UNDEFINED;
76 val = pqf->effective_flags & mask;
77 if (val)
78 return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME;
80 return PM_QOS_FLAGS_NONE;
83 /**
84 * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
85 * @dev: Device to check the PM QoS flags for.
86 * @mask: Flags to check against.
88 enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask)
90 unsigned long irqflags;
91 enum pm_qos_flags_status ret;
93 spin_lock_irqsave(&dev->power.lock, irqflags);
94 ret = __dev_pm_qos_flags(dev, mask);
95 spin_unlock_irqrestore(&dev->power.lock, irqflags);
97 return ret;
99 EXPORT_SYMBOL_GPL(dev_pm_qos_flags);
102 * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
103 * @dev: Device to get the PM QoS constraint value for.
105 * This routine must be called with dev->power.lock held.
107 s32 __dev_pm_qos_read_value(struct device *dev)
109 lockdep_assert_held(&dev->power.lock);
111 return dev_pm_qos_raw_read_value(dev);
115 * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
116 * @dev: Device to get the PM QoS constraint value for.
118 s32 dev_pm_qos_read_value(struct device *dev)
120 unsigned long flags;
121 s32 ret;
123 spin_lock_irqsave(&dev->power.lock, flags);
124 ret = __dev_pm_qos_read_value(dev);
125 spin_unlock_irqrestore(&dev->power.lock, flags);
127 return ret;
131 * apply_constraint - Add/modify/remove device PM QoS request.
132 * @req: Constraint request to apply
133 * @action: Action to perform (add/update/remove).
134 * @value: Value to assign to the QoS request.
136 * Internal function to update the constraints list using the PM QoS core
137 * code and if needed call the per-device and the global notification
138 * callbacks
140 static int apply_constraint(struct dev_pm_qos_request *req,
141 enum pm_qos_req_action action, s32 value)
143 struct dev_pm_qos *qos = req->dev->power.qos;
144 int ret;
146 switch(req->type) {
147 case DEV_PM_QOS_RESUME_LATENCY:
148 ret = pm_qos_update_target(&qos->resume_latency,
149 &req->data.pnode, action, value);
150 if (ret) {
151 value = pm_qos_read_value(&qos->resume_latency);
152 blocking_notifier_call_chain(&dev_pm_notifiers,
153 (unsigned long)value,
154 req);
156 break;
157 case DEV_PM_QOS_LATENCY_TOLERANCE:
158 ret = pm_qos_update_target(&qos->latency_tolerance,
159 &req->data.pnode, action, value);
160 if (ret) {
161 value = pm_qos_read_value(&qos->latency_tolerance);
162 req->dev->power.set_latency_tolerance(req->dev, value);
164 break;
165 case DEV_PM_QOS_FLAGS:
166 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
167 action, value);
168 break;
169 default:
170 ret = -EINVAL;
173 return ret;
177 * dev_pm_qos_constraints_allocate
178 * @dev: device to allocate data for
180 * Called at the first call to add_request, for constraint data allocation
181 * Must be called with the dev_pm_qos_mtx mutex held
183 static int dev_pm_qos_constraints_allocate(struct device *dev)
185 struct dev_pm_qos *qos;
186 struct pm_qos_constraints *c;
187 struct blocking_notifier_head *n;
189 qos = kzalloc(sizeof(*qos), GFP_KERNEL);
190 if (!qos)
191 return -ENOMEM;
193 n = kzalloc(sizeof(*n), GFP_KERNEL);
194 if (!n) {
195 kfree(qos);
196 return -ENOMEM;
198 BLOCKING_INIT_NOTIFIER_HEAD(n);
200 c = &qos->resume_latency;
201 plist_head_init(&c->list);
202 c->target_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
203 c->default_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
204 c->no_constraint_value = PM_QOS_RESUME_LATENCY_DEFAULT_VALUE;
205 c->type = PM_QOS_MIN;
206 c->notifiers = n;
208 c = &qos->latency_tolerance;
209 plist_head_init(&c->list);
210 c->target_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
211 c->default_value = PM_QOS_LATENCY_TOLERANCE_DEFAULT_VALUE;
212 c->no_constraint_value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
213 c->type = PM_QOS_MIN;
215 INIT_LIST_HEAD(&qos->flags.list);
217 spin_lock_irq(&dev->power.lock);
218 dev->power.qos = qos;
219 spin_unlock_irq(&dev->power.lock);
221 return 0;
224 static void __dev_pm_qos_hide_latency_limit(struct device *dev);
225 static void __dev_pm_qos_hide_flags(struct device *dev);
228 * dev_pm_qos_constraints_destroy
229 * @dev: target device
231 * Called from the device PM subsystem on device removal under device_pm_lock().
233 void dev_pm_qos_constraints_destroy(struct device *dev)
235 struct dev_pm_qos *qos;
236 struct dev_pm_qos_request *req, *tmp;
237 struct pm_qos_constraints *c;
238 struct pm_qos_flags *f;
240 mutex_lock(&dev_pm_qos_sysfs_mtx);
243 * If the device's PM QoS resume latency limit or PM QoS flags have been
244 * exposed to user space, they have to be hidden at this point.
246 pm_qos_sysfs_remove_resume_latency(dev);
247 pm_qos_sysfs_remove_flags(dev);
249 mutex_lock(&dev_pm_qos_mtx);
251 __dev_pm_qos_hide_latency_limit(dev);
252 __dev_pm_qos_hide_flags(dev);
254 qos = dev->power.qos;
255 if (!qos)
256 goto out;
258 /* Flush the constraints lists for the device. */
259 c = &qos->resume_latency;
260 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
262 * Update constraints list and call the notification
263 * callbacks if needed
265 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
266 memset(req, 0, sizeof(*req));
268 c = &qos->latency_tolerance;
269 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
270 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
271 memset(req, 0, sizeof(*req));
273 f = &qos->flags;
274 list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
275 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
276 memset(req, 0, sizeof(*req));
279 spin_lock_irq(&dev->power.lock);
280 dev->power.qos = ERR_PTR(-ENODEV);
281 spin_unlock_irq(&dev->power.lock);
283 kfree(c->notifiers);
284 kfree(qos);
286 out:
287 mutex_unlock(&dev_pm_qos_mtx);
289 mutex_unlock(&dev_pm_qos_sysfs_mtx);
292 static bool dev_pm_qos_invalid_request(struct device *dev,
293 struct dev_pm_qos_request *req)
295 return !req || (req->type == DEV_PM_QOS_LATENCY_TOLERANCE
296 && !dev->power.set_latency_tolerance);
299 static int __dev_pm_qos_add_request(struct device *dev,
300 struct dev_pm_qos_request *req,
301 enum dev_pm_qos_req_type type, s32 value)
303 int ret = 0;
305 if (!dev || dev_pm_qos_invalid_request(dev, req))
306 return -EINVAL;
308 if (WARN(dev_pm_qos_request_active(req),
309 "%s() called for already added request\n", __func__))
310 return -EINVAL;
312 if (IS_ERR(dev->power.qos))
313 ret = -ENODEV;
314 else if (!dev->power.qos)
315 ret = dev_pm_qos_constraints_allocate(dev);
317 trace_dev_pm_qos_add_request(dev_name(dev), type, value);
318 if (!ret) {
319 req->dev = dev;
320 req->type = type;
321 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
323 return ret;
327 * dev_pm_qos_add_request - inserts new qos request into the list
328 * @dev: target device for the constraint
329 * @req: pointer to a preallocated handle
330 * @type: type of the request
331 * @value: defines the qos request
333 * This function inserts a new entry in the device constraints list of
334 * requested qos performance characteristics. It recomputes the aggregate
335 * QoS expectations of parameters and initializes the dev_pm_qos_request
336 * handle. Caller needs to save this handle for later use in updates and
337 * removal.
339 * Returns 1 if the aggregated constraint value has changed,
340 * 0 if the aggregated constraint value has not changed,
341 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
342 * to allocate for data structures, -ENODEV if the device has just been removed
343 * from the system.
345 * Callers should ensure that the target device is not RPM_SUSPENDED before
346 * using this function for requests of type DEV_PM_QOS_FLAGS.
348 int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
349 enum dev_pm_qos_req_type type, s32 value)
351 int ret;
353 mutex_lock(&dev_pm_qos_mtx);
354 ret = __dev_pm_qos_add_request(dev, req, type, value);
355 mutex_unlock(&dev_pm_qos_mtx);
356 return ret;
358 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
361 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
362 * @req : PM QoS request to modify.
363 * @new_value: New value to request.
365 static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
366 s32 new_value)
368 s32 curr_value;
369 int ret = 0;
371 if (!req) /*guard against callers passing in null */
372 return -EINVAL;
374 if (WARN(!dev_pm_qos_request_active(req),
375 "%s() called for unknown object\n", __func__))
376 return -EINVAL;
378 if (IS_ERR_OR_NULL(req->dev->power.qos))
379 return -ENODEV;
381 switch(req->type) {
382 case DEV_PM_QOS_RESUME_LATENCY:
383 case DEV_PM_QOS_LATENCY_TOLERANCE:
384 curr_value = req->data.pnode.prio;
385 break;
386 case DEV_PM_QOS_FLAGS:
387 curr_value = req->data.flr.flags;
388 break;
389 default:
390 return -EINVAL;
393 trace_dev_pm_qos_update_request(dev_name(req->dev), req->type,
394 new_value);
395 if (curr_value != new_value)
396 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
398 return ret;
402 * dev_pm_qos_update_request - modifies an existing qos request
403 * @req : handle to list element holding a dev_pm_qos request to use
404 * @new_value: defines the qos request
406 * Updates an existing dev PM qos request along with updating the
407 * target value.
409 * Attempts are made to make this code callable on hot code paths.
411 * Returns 1 if the aggregated constraint value has changed,
412 * 0 if the aggregated constraint value has not changed,
413 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
414 * removed from the system
416 * Callers should ensure that the target device is not RPM_SUSPENDED before
417 * using this function for requests of type DEV_PM_QOS_FLAGS.
419 int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
421 int ret;
423 mutex_lock(&dev_pm_qos_mtx);
424 ret = __dev_pm_qos_update_request(req, new_value);
425 mutex_unlock(&dev_pm_qos_mtx);
426 return ret;
428 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
430 static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
432 int ret;
434 if (!req) /*guard against callers passing in null */
435 return -EINVAL;
437 if (WARN(!dev_pm_qos_request_active(req),
438 "%s() called for unknown object\n", __func__))
439 return -EINVAL;
441 if (IS_ERR_OR_NULL(req->dev->power.qos))
442 return -ENODEV;
444 trace_dev_pm_qos_remove_request(dev_name(req->dev), req->type,
445 PM_QOS_DEFAULT_VALUE);
446 ret = apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
447 memset(req, 0, sizeof(*req));
448 return ret;
452 * dev_pm_qos_remove_request - modifies an existing qos request
453 * @req: handle to request list element
455 * Will remove pm qos request from the list of constraints and
456 * recompute the current target value. Call this on slow code paths.
458 * Returns 1 if the aggregated constraint value has changed,
459 * 0 if the aggregated constraint value has not changed,
460 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
461 * removed from the system
463 * Callers should ensure that the target device is not RPM_SUSPENDED before
464 * using this function for requests of type DEV_PM_QOS_FLAGS.
466 int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
468 int ret;
470 mutex_lock(&dev_pm_qos_mtx);
471 ret = __dev_pm_qos_remove_request(req);
472 mutex_unlock(&dev_pm_qos_mtx);
473 return ret;
475 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
478 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
479 * of per-device PM QoS constraints
481 * @dev: target device for the constraint
482 * @notifier: notifier block managed by caller.
484 * Will register the notifier into a notification chain that gets called
485 * upon changes to the target value for the device.
487 * If the device's constraints object doesn't exist when this routine is called,
488 * it will be created (or error code will be returned if that fails).
490 int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
492 int ret = 0;
494 mutex_lock(&dev_pm_qos_mtx);
496 if (IS_ERR(dev->power.qos))
497 ret = -ENODEV;
498 else if (!dev->power.qos)
499 ret = dev_pm_qos_constraints_allocate(dev);
501 if (!ret)
502 ret = blocking_notifier_chain_register(dev->power.qos->resume_latency.notifiers,
503 notifier);
505 mutex_unlock(&dev_pm_qos_mtx);
506 return ret;
508 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
511 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
512 * of per-device PM QoS constraints
514 * @dev: target device for the constraint
515 * @notifier: notifier block to be removed.
517 * Will remove the notifier from the notification chain that gets called
518 * upon changes to the target value.
520 int dev_pm_qos_remove_notifier(struct device *dev,
521 struct notifier_block *notifier)
523 int retval = 0;
525 mutex_lock(&dev_pm_qos_mtx);
527 /* Silently return if the constraints object is not present. */
528 if (!IS_ERR_OR_NULL(dev->power.qos))
529 retval = blocking_notifier_chain_unregister(dev->power.qos->resume_latency.notifiers,
530 notifier);
532 mutex_unlock(&dev_pm_qos_mtx);
533 return retval;
535 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
538 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
539 * target value of the PM QoS constraints for any device
541 * @notifier: notifier block managed by caller.
543 * Will register the notifier into a notification chain that gets called
544 * upon changes to the target value for any device.
546 int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
548 return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
550 EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
553 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
554 * target value of PM QoS constraints for any device
556 * @notifier: notifier block to be removed.
558 * Will remove the notifier from the notification chain that gets called
559 * upon changes to the target value for any device.
561 int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
563 return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
565 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
568 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
569 * @dev: Device whose ancestor to add the request for.
570 * @req: Pointer to the preallocated handle.
571 * @type: Type of the request.
572 * @value: Constraint latency value.
574 int dev_pm_qos_add_ancestor_request(struct device *dev,
575 struct dev_pm_qos_request *req,
576 enum dev_pm_qos_req_type type, s32 value)
578 struct device *ancestor = dev->parent;
579 int ret = -ENODEV;
581 switch (type) {
582 case DEV_PM_QOS_RESUME_LATENCY:
583 while (ancestor && !ancestor->power.ignore_children)
584 ancestor = ancestor->parent;
586 break;
587 case DEV_PM_QOS_LATENCY_TOLERANCE:
588 while (ancestor && !ancestor->power.set_latency_tolerance)
589 ancestor = ancestor->parent;
591 break;
592 default:
593 ancestor = NULL;
595 if (ancestor)
596 ret = dev_pm_qos_add_request(ancestor, req, type, value);
598 if (ret < 0)
599 req->dev = NULL;
601 return ret;
603 EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
605 static void __dev_pm_qos_drop_user_request(struct device *dev,
606 enum dev_pm_qos_req_type type)
608 struct dev_pm_qos_request *req = NULL;
610 switch(type) {
611 case DEV_PM_QOS_RESUME_LATENCY:
612 req = dev->power.qos->resume_latency_req;
613 dev->power.qos->resume_latency_req = NULL;
614 break;
615 case DEV_PM_QOS_LATENCY_TOLERANCE:
616 req = dev->power.qos->latency_tolerance_req;
617 dev->power.qos->latency_tolerance_req = NULL;
618 break;
619 case DEV_PM_QOS_FLAGS:
620 req = dev->power.qos->flags_req;
621 dev->power.qos->flags_req = NULL;
622 break;
624 __dev_pm_qos_remove_request(req);
625 kfree(req);
628 static void dev_pm_qos_drop_user_request(struct device *dev,
629 enum dev_pm_qos_req_type type)
631 mutex_lock(&dev_pm_qos_mtx);
632 __dev_pm_qos_drop_user_request(dev, type);
633 mutex_unlock(&dev_pm_qos_mtx);
637 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
638 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
639 * @value: Initial value of the latency limit.
641 int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
643 struct dev_pm_qos_request *req;
644 int ret;
646 if (!device_is_registered(dev) || value < 0)
647 return -EINVAL;
649 req = kzalloc(sizeof(*req), GFP_KERNEL);
650 if (!req)
651 return -ENOMEM;
653 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_RESUME_LATENCY, value);
654 if (ret < 0) {
655 kfree(req);
656 return ret;
659 mutex_lock(&dev_pm_qos_sysfs_mtx);
661 mutex_lock(&dev_pm_qos_mtx);
663 if (IS_ERR_OR_NULL(dev->power.qos))
664 ret = -ENODEV;
665 else if (dev->power.qos->resume_latency_req)
666 ret = -EEXIST;
668 if (ret < 0) {
669 __dev_pm_qos_remove_request(req);
670 kfree(req);
671 mutex_unlock(&dev_pm_qos_mtx);
672 goto out;
674 dev->power.qos->resume_latency_req = req;
676 mutex_unlock(&dev_pm_qos_mtx);
678 ret = pm_qos_sysfs_add_resume_latency(dev);
679 if (ret)
680 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
682 out:
683 mutex_unlock(&dev_pm_qos_sysfs_mtx);
684 return ret;
686 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
688 static void __dev_pm_qos_hide_latency_limit(struct device *dev)
690 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->resume_latency_req)
691 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_RESUME_LATENCY);
695 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
696 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
698 void dev_pm_qos_hide_latency_limit(struct device *dev)
700 mutex_lock(&dev_pm_qos_sysfs_mtx);
702 pm_qos_sysfs_remove_resume_latency(dev);
704 mutex_lock(&dev_pm_qos_mtx);
705 __dev_pm_qos_hide_latency_limit(dev);
706 mutex_unlock(&dev_pm_qos_mtx);
708 mutex_unlock(&dev_pm_qos_sysfs_mtx);
710 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
713 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
714 * @dev: Device whose PM QoS flags are to be exposed to user space.
715 * @val: Initial values of the flags.
717 int dev_pm_qos_expose_flags(struct device *dev, s32 val)
719 struct dev_pm_qos_request *req;
720 int ret;
722 if (!device_is_registered(dev))
723 return -EINVAL;
725 req = kzalloc(sizeof(*req), GFP_KERNEL);
726 if (!req)
727 return -ENOMEM;
729 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
730 if (ret < 0) {
731 kfree(req);
732 return ret;
735 pm_runtime_get_sync(dev);
736 mutex_lock(&dev_pm_qos_sysfs_mtx);
738 mutex_lock(&dev_pm_qos_mtx);
740 if (IS_ERR_OR_NULL(dev->power.qos))
741 ret = -ENODEV;
742 else if (dev->power.qos->flags_req)
743 ret = -EEXIST;
745 if (ret < 0) {
746 __dev_pm_qos_remove_request(req);
747 kfree(req);
748 mutex_unlock(&dev_pm_qos_mtx);
749 goto out;
751 dev->power.qos->flags_req = req;
753 mutex_unlock(&dev_pm_qos_mtx);
755 ret = pm_qos_sysfs_add_flags(dev);
756 if (ret)
757 dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
759 out:
760 mutex_unlock(&dev_pm_qos_sysfs_mtx);
761 pm_runtime_put(dev);
762 return ret;
764 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
766 static void __dev_pm_qos_hide_flags(struct device *dev)
768 if (!IS_ERR_OR_NULL(dev->power.qos) && dev->power.qos->flags_req)
769 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
773 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
774 * @dev: Device whose PM QoS flags are to be hidden from user space.
776 void dev_pm_qos_hide_flags(struct device *dev)
778 pm_runtime_get_sync(dev);
779 mutex_lock(&dev_pm_qos_sysfs_mtx);
781 pm_qos_sysfs_remove_flags(dev);
783 mutex_lock(&dev_pm_qos_mtx);
784 __dev_pm_qos_hide_flags(dev);
785 mutex_unlock(&dev_pm_qos_mtx);
787 mutex_unlock(&dev_pm_qos_sysfs_mtx);
788 pm_runtime_put(dev);
790 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
793 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
794 * @dev: Device to update the PM QoS flags request for.
795 * @mask: Flags to set/clear.
796 * @set: Whether to set or clear the flags (true means set).
798 int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
800 s32 value;
801 int ret;
803 pm_runtime_get_sync(dev);
804 mutex_lock(&dev_pm_qos_mtx);
806 if (IS_ERR_OR_NULL(dev->power.qos) || !dev->power.qos->flags_req) {
807 ret = -EINVAL;
808 goto out;
811 value = dev_pm_qos_requested_flags(dev);
812 if (set)
813 value |= mask;
814 else
815 value &= ~mask;
817 ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
819 out:
820 mutex_unlock(&dev_pm_qos_mtx);
821 pm_runtime_put(dev);
822 return ret;
826 * dev_pm_qos_get_user_latency_tolerance - Get user space latency tolerance.
827 * @dev: Device to obtain the user space latency tolerance for.
829 s32 dev_pm_qos_get_user_latency_tolerance(struct device *dev)
831 s32 ret;
833 mutex_lock(&dev_pm_qos_mtx);
834 ret = IS_ERR_OR_NULL(dev->power.qos)
835 || !dev->power.qos->latency_tolerance_req ?
836 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT :
837 dev->power.qos->latency_tolerance_req->data.pnode.prio;
838 mutex_unlock(&dev_pm_qos_mtx);
839 return ret;
843 * dev_pm_qos_update_user_latency_tolerance - Update user space latency tolerance.
844 * @dev: Device to update the user space latency tolerance for.
845 * @val: New user space latency tolerance for @dev (negative values disable).
847 int dev_pm_qos_update_user_latency_tolerance(struct device *dev, s32 val)
849 int ret;
851 mutex_lock(&dev_pm_qos_mtx);
853 if (IS_ERR_OR_NULL(dev->power.qos)
854 || !dev->power.qos->latency_tolerance_req) {
855 struct dev_pm_qos_request *req;
857 if (val < 0) {
858 if (val == PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT)
859 ret = 0;
860 else
861 ret = -EINVAL;
862 goto out;
864 req = kzalloc(sizeof(*req), GFP_KERNEL);
865 if (!req) {
866 ret = -ENOMEM;
867 goto out;
869 ret = __dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY_TOLERANCE, val);
870 if (ret < 0) {
871 kfree(req);
872 goto out;
874 dev->power.qos->latency_tolerance_req = req;
875 } else {
876 if (val < 0) {
877 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY_TOLERANCE);
878 ret = 0;
879 } else {
880 ret = __dev_pm_qos_update_request(dev->power.qos->latency_tolerance_req, val);
884 out:
885 mutex_unlock(&dev_pm_qos_mtx);
886 return ret;
888 EXPORT_SYMBOL_GPL(dev_pm_qos_update_user_latency_tolerance);
891 * dev_pm_qos_expose_latency_tolerance - Expose latency tolerance to userspace
892 * @dev: Device whose latency tolerance to expose
894 int dev_pm_qos_expose_latency_tolerance(struct device *dev)
896 int ret;
898 if (!dev->power.set_latency_tolerance)
899 return -EINVAL;
901 mutex_lock(&dev_pm_qos_sysfs_mtx);
902 ret = pm_qos_sysfs_add_latency_tolerance(dev);
903 mutex_unlock(&dev_pm_qos_sysfs_mtx);
905 return ret;
907 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_tolerance);
910 * dev_pm_qos_hide_latency_tolerance - Hide latency tolerance from userspace
911 * @dev: Device whose latency tolerance to hide
913 void dev_pm_qos_hide_latency_tolerance(struct device *dev)
915 mutex_lock(&dev_pm_qos_sysfs_mtx);
916 pm_qos_sysfs_remove_latency_tolerance(dev);
917 mutex_unlock(&dev_pm_qos_sysfs_mtx);
919 /* Remove the request from user space now */
920 pm_runtime_get_sync(dev);
921 dev_pm_qos_update_user_latency_tolerance(dev,
922 PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT);
923 pm_runtime_put(dev);
925 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_tolerance);