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>
47 static DEFINE_MUTEX(dev_pm_qos_mtx);
49 static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers);
52 * __dev_pm_qos_flags - Check PM QoS flags for a given device.
53 * @dev: Device to check the PM QoS flags for.
54 * @mask: Flags to check against.
56 * This routine must be called with dev->power.lock held.
58 enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask)
60 struct dev_pm_qos *qos = dev->power.qos;
61 struct pm_qos_flags *pqf;
65 return PM_QOS_FLAGS_UNDEFINED;
68 if (list_empty(&pqf->list))
69 return PM_QOS_FLAGS_UNDEFINED;
71 val = pqf->effective_flags & mask;
73 return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME;
75 return PM_QOS_FLAGS_NONE;
79 * dev_pm_qos_flags - Check PM QoS flags for a given device (locked).
80 * @dev: Device to check the PM QoS flags for.
81 * @mask: Flags to check against.
83 enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask)
85 unsigned long irqflags;
86 enum pm_qos_flags_status ret;
88 spin_lock_irqsave(&dev->power.lock, irqflags);
89 ret = __dev_pm_qos_flags(dev, mask);
90 spin_unlock_irqrestore(&dev->power.lock, irqflags);
94 EXPORT_SYMBOL_GPL(dev_pm_qos_flags);
97 * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
98 * @dev: Device to get the PM QoS constraint value for.
100 * This routine must be called with dev->power.lock held.
102 s32 __dev_pm_qos_read_value(struct device *dev)
104 return dev->power.qos ? pm_qos_read_value(&dev->power.qos->latency) : 0;
108 * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
109 * @dev: Device to get the PM QoS constraint value for.
111 s32 dev_pm_qos_read_value(struct device *dev)
116 spin_lock_irqsave(&dev->power.lock, flags);
117 ret = __dev_pm_qos_read_value(dev);
118 spin_unlock_irqrestore(&dev->power.lock, flags);
124 * apply_constraint - Add/modify/remove device PM QoS request.
125 * @req: Constraint request to apply
126 * @action: Action to perform (add/update/remove).
127 * @value: Value to assign to the QoS request.
129 * Internal function to update the constraints list using the PM QoS core
130 * code and if needed call the per-device and the global notification
133 static int apply_constraint(struct dev_pm_qos_request *req,
134 enum pm_qos_req_action action, s32 value)
136 struct dev_pm_qos *qos = req->dev->power.qos;
140 case DEV_PM_QOS_LATENCY:
141 ret = pm_qos_update_target(&qos->latency, &req->data.pnode,
144 value = pm_qos_read_value(&qos->latency);
145 blocking_notifier_call_chain(&dev_pm_notifiers,
146 (unsigned long)value,
150 case DEV_PM_QOS_FLAGS:
151 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
162 * dev_pm_qos_constraints_allocate
163 * @dev: device to allocate data for
165 * Called at the first call to add_request, for constraint data allocation
166 * Must be called with the dev_pm_qos_mtx mutex held
168 static int dev_pm_qos_constraints_allocate(struct device *dev)
170 struct dev_pm_qos *qos;
171 struct pm_qos_constraints *c;
172 struct blocking_notifier_head *n;
174 qos = kzalloc(sizeof(*qos), GFP_KERNEL);
178 n = kzalloc(sizeof(*n), GFP_KERNEL);
183 BLOCKING_INIT_NOTIFIER_HEAD(n);
186 plist_head_init(&c->list);
187 c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
188 c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
189 c->type = PM_QOS_MIN;
192 INIT_LIST_HEAD(&qos->flags.list);
194 spin_lock_irq(&dev->power.lock);
195 dev->power.qos = qos;
196 spin_unlock_irq(&dev->power.lock);
202 * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer.
203 * @dev: target device
205 * Called from the device PM subsystem during device insertion under
208 void dev_pm_qos_constraints_init(struct device *dev)
210 mutex_lock(&dev_pm_qos_mtx);
211 dev->power.qos = NULL;
212 dev->power.power_state = PMSG_ON;
213 mutex_unlock(&dev_pm_qos_mtx);
217 * dev_pm_qos_constraints_destroy
218 * @dev: target device
220 * Called from the device PM subsystem on device removal under device_pm_lock().
222 void dev_pm_qos_constraints_destroy(struct device *dev)
224 struct dev_pm_qos *qos;
225 struct dev_pm_qos_request *req, *tmp;
226 struct pm_qos_constraints *c;
227 struct pm_qos_flags *f;
230 * If the device's PM QoS resume latency limit or PM QoS flags have been
231 * exposed to user space, they have to be hidden at this point.
233 dev_pm_qos_hide_latency_limit(dev);
234 dev_pm_qos_hide_flags(dev);
236 mutex_lock(&dev_pm_qos_mtx);
238 dev->power.power_state = PMSG_INVALID;
239 qos = dev->power.qos;
243 /* Flush the constraints lists for the device. */
245 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
247 * Update constraints list and call the notification
248 * callbacks if needed
250 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
251 memset(req, 0, sizeof(*req));
254 list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
255 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
256 memset(req, 0, sizeof(*req));
259 spin_lock_irq(&dev->power.lock);
260 dev->power.qos = NULL;
261 spin_unlock_irq(&dev->power.lock);
267 mutex_unlock(&dev_pm_qos_mtx);
271 * dev_pm_qos_add_request - inserts new qos request into the list
272 * @dev: target device for the constraint
273 * @req: pointer to a preallocated handle
274 * @type: type of the request
275 * @value: defines the qos request
277 * This function inserts a new entry in the device constraints list of
278 * requested qos performance characteristics. It recomputes the aggregate
279 * QoS expectations of parameters and initializes the dev_pm_qos_request
280 * handle. Caller needs to save this handle for later use in updates and
283 * Returns 1 if the aggregated constraint value has changed,
284 * 0 if the aggregated constraint value has not changed,
285 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
286 * to allocate for data structures, -ENODEV if the device has just been removed
289 * Callers should ensure that the target device is not RPM_SUSPENDED before
290 * using this function for requests of type DEV_PM_QOS_FLAGS.
292 int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
293 enum dev_pm_qos_req_type type, s32 value)
297 if (!dev || !req) /*guard against callers passing in null */
300 if (WARN(dev_pm_qos_request_active(req),
301 "%s() called for already added request\n", __func__))
306 mutex_lock(&dev_pm_qos_mtx);
308 if (!dev->power.qos) {
309 if (dev->power.power_state.event == PM_EVENT_INVALID) {
310 /* The device has been removed from the system. */
316 * Allocate the constraints data on the first call to
317 * add_request, i.e. only if the data is not already
318 * allocated and if the device has not been removed.
320 ret = dev_pm_qos_constraints_allocate(dev);
326 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
330 mutex_unlock(&dev_pm_qos_mtx);
334 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
337 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
338 * @req : PM QoS request to modify.
339 * @new_value: New value to request.
341 static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
347 if (!req->dev->power.qos)
351 case DEV_PM_QOS_LATENCY:
352 curr_value = req->data.pnode.prio;
354 case DEV_PM_QOS_FLAGS:
355 curr_value = req->data.flr.flags;
361 if (curr_value != new_value)
362 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
368 * dev_pm_qos_update_request - modifies an existing qos request
369 * @req : handle to list element holding a dev_pm_qos request to use
370 * @new_value: defines the qos request
372 * Updates an existing dev PM qos request along with updating the
375 * Attempts are made to make this code callable on hot code paths.
377 * Returns 1 if the aggregated constraint value has changed,
378 * 0 if the aggregated constraint value has not changed,
379 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
380 * removed from the system
382 * Callers should ensure that the target device is not RPM_SUSPENDED before
383 * using this function for requests of type DEV_PM_QOS_FLAGS.
385 int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
389 if (!req) /*guard against callers passing in null */
392 if (WARN(!dev_pm_qos_request_active(req),
393 "%s() called for unknown object\n", __func__))
396 mutex_lock(&dev_pm_qos_mtx);
397 ret = __dev_pm_qos_update_request(req, new_value);
398 mutex_unlock(&dev_pm_qos_mtx);
402 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
405 * dev_pm_qos_remove_request - modifies an existing qos request
406 * @req: handle to request list element
408 * Will remove pm qos request from the list of constraints and
409 * recompute the current target value. Call this on slow 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_remove_request(struct dev_pm_qos_request *req)
423 if (!req) /*guard against callers passing in null */
426 if (WARN(!dev_pm_qos_request_active(req),
427 "%s() called for unknown object\n", __func__))
430 mutex_lock(&dev_pm_qos_mtx);
432 if (req->dev->power.qos) {
433 ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
434 PM_QOS_DEFAULT_VALUE);
435 memset(req, 0, sizeof(*req));
437 /* Return if the device has been removed */
441 mutex_unlock(&dev_pm_qos_mtx);
444 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
447 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
448 * of per-device PM QoS constraints
450 * @dev: target device for the constraint
451 * @notifier: notifier block managed by caller.
453 * Will register the notifier into a notification chain that gets called
454 * upon changes to the target value for the device.
456 * If the device's constraints object doesn't exist when this routine is called,
457 * it will be created (or error code will be returned if that fails).
459 int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
463 mutex_lock(&dev_pm_qos_mtx);
466 ret = dev->power.power_state.event != PM_EVENT_INVALID ?
467 dev_pm_qos_constraints_allocate(dev) : -ENODEV;
470 ret = blocking_notifier_chain_register(
471 dev->power.qos->latency.notifiers, notifier);
473 mutex_unlock(&dev_pm_qos_mtx);
476 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
479 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
480 * of per-device PM QoS constraints
482 * @dev: target device for the constraint
483 * @notifier: notifier block to be removed.
485 * Will remove the notifier from the notification chain that gets called
486 * upon changes to the target value.
488 int dev_pm_qos_remove_notifier(struct device *dev,
489 struct notifier_block *notifier)
493 mutex_lock(&dev_pm_qos_mtx);
495 /* Silently return if the constraints object is not present. */
497 retval = blocking_notifier_chain_unregister(
498 dev->power.qos->latency.notifiers,
501 mutex_unlock(&dev_pm_qos_mtx);
504 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
507 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
508 * target value of the PM QoS constraints for any device
510 * @notifier: notifier block managed by caller.
512 * Will register the notifier into a notification chain that gets called
513 * upon changes to the target value for any device.
515 int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
517 return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
519 EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
522 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
523 * target value of PM QoS constraints for any device
525 * @notifier: notifier block to be removed.
527 * Will remove the notifier from the notification chain that gets called
528 * upon changes to the target value for any device.
530 int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
532 return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
534 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
537 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
538 * @dev: Device whose ancestor to add the request for.
539 * @req: Pointer to the preallocated handle.
540 * @value: Constraint latency value.
542 int dev_pm_qos_add_ancestor_request(struct device *dev,
543 struct dev_pm_qos_request *req, s32 value)
545 struct device *ancestor = dev->parent;
548 while (ancestor && !ancestor->power.ignore_children)
549 ancestor = ancestor->parent;
552 ret = dev_pm_qos_add_request(ancestor, req,
553 DEV_PM_QOS_LATENCY, value);
560 EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
562 #ifdef CONFIG_PM_RUNTIME
563 static void __dev_pm_qos_drop_user_request(struct device *dev,
564 enum dev_pm_qos_req_type type)
567 case DEV_PM_QOS_LATENCY:
568 dev_pm_qos_remove_request(dev->power.qos->latency_req);
569 dev->power.qos->latency_req = NULL;
571 case DEV_PM_QOS_FLAGS:
572 dev_pm_qos_remove_request(dev->power.qos->flags_req);
573 dev->power.qos->flags_req = NULL;
579 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
580 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
581 * @value: Initial value of the latency limit.
583 int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
585 struct dev_pm_qos_request *req;
588 if (!device_is_registered(dev) || value < 0)
591 if (dev->power.qos && dev->power.qos->latency_req)
594 req = kzalloc(sizeof(*req), GFP_KERNEL);
598 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY, value);
602 dev->power.qos->latency_req = req;
603 ret = pm_qos_sysfs_add_latency(dev);
605 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
609 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
612 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
613 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
615 void dev_pm_qos_hide_latency_limit(struct device *dev)
617 if (dev->power.qos && dev->power.qos->latency_req) {
618 pm_qos_sysfs_remove_latency(dev);
619 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
622 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
625 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
626 * @dev: Device whose PM QoS flags are to be exposed to user space.
627 * @val: Initial values of the flags.
629 int dev_pm_qos_expose_flags(struct device *dev, s32 val)
631 struct dev_pm_qos_request *req;
634 if (!device_is_registered(dev))
637 if (dev->power.qos && dev->power.qos->flags_req)
640 req = kzalloc(sizeof(*req), GFP_KERNEL);
644 pm_runtime_get_sync(dev);
645 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
649 dev->power.qos->flags_req = req;
650 ret = pm_qos_sysfs_add_flags(dev);
652 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
658 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
661 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
662 * @dev: Device whose PM QoS flags are to be hidden from user space.
664 void dev_pm_qos_hide_flags(struct device *dev)
666 if (dev->power.qos && dev->power.qos->flags_req) {
667 pm_qos_sysfs_remove_flags(dev);
668 pm_runtime_get_sync(dev);
669 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
673 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
676 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
677 * @dev: Device to update the PM QoS flags request for.
678 * @mask: Flags to set/clear.
679 * @set: Whether to set or clear the flags (true means set).
681 int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
686 if (!dev->power.qos || !dev->power.qos->flags_req)
689 pm_runtime_get_sync(dev);
690 mutex_lock(&dev_pm_qos_mtx);
692 value = dev_pm_qos_requested_flags(dev);
698 ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
700 mutex_unlock(&dev_pm_qos_mtx);
705 #endif /* CONFIG_PM_RUNTIME */