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);
96 * __dev_pm_qos_read_value - Get PM QoS constraint for a given device.
97 * @dev: Device to get the PM QoS constraint value for.
99 * This routine must be called with dev->power.lock held.
101 s32 __dev_pm_qos_read_value(struct device *dev)
103 return dev->power.qos ? pm_qos_read_value(&dev->power.qos->latency) : 0;
107 * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked).
108 * @dev: Device to get the PM QoS constraint value for.
110 s32 dev_pm_qos_read_value(struct device *dev)
115 spin_lock_irqsave(&dev->power.lock, flags);
116 ret = __dev_pm_qos_read_value(dev);
117 spin_unlock_irqrestore(&dev->power.lock, flags);
123 * apply_constraint - Add/modify/remove device PM QoS request.
124 * @req: Constraint request to apply
125 * @action: Action to perform (add/update/remove).
126 * @value: Value to assign to the QoS request.
128 * Internal function to update the constraints list using the PM QoS core
129 * code and if needed call the per-device and the global notification
132 static int apply_constraint(struct dev_pm_qos_request *req,
133 enum pm_qos_req_action action, s32 value)
135 struct dev_pm_qos *qos = req->dev->power.qos;
139 case DEV_PM_QOS_LATENCY:
140 ret = pm_qos_update_target(&qos->latency, &req->data.pnode,
143 value = pm_qos_read_value(&qos->latency);
144 blocking_notifier_call_chain(&dev_pm_notifiers,
145 (unsigned long)value,
149 case DEV_PM_QOS_FLAGS:
150 ret = pm_qos_update_flags(&qos->flags, &req->data.flr,
161 * dev_pm_qos_constraints_allocate
162 * @dev: device to allocate data for
164 * Called at the first call to add_request, for constraint data allocation
165 * Must be called with the dev_pm_qos_mtx mutex held
167 static int dev_pm_qos_constraints_allocate(struct device *dev)
169 struct dev_pm_qos *qos;
170 struct pm_qos_constraints *c;
171 struct blocking_notifier_head *n;
173 qos = kzalloc(sizeof(*qos), GFP_KERNEL);
177 n = kzalloc(sizeof(*n), GFP_KERNEL);
182 BLOCKING_INIT_NOTIFIER_HEAD(n);
185 plist_head_init(&c->list);
186 c->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
187 c->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE;
188 c->type = PM_QOS_MIN;
191 INIT_LIST_HEAD(&qos->flags.list);
193 spin_lock_irq(&dev->power.lock);
194 dev->power.qos = qos;
195 spin_unlock_irq(&dev->power.lock);
201 * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer.
202 * @dev: target device
204 * Called from the device PM subsystem during device insertion under
207 void dev_pm_qos_constraints_init(struct device *dev)
209 mutex_lock(&dev_pm_qos_mtx);
210 dev->power.qos = NULL;
211 dev->power.power_state = PMSG_ON;
212 mutex_unlock(&dev_pm_qos_mtx);
216 * dev_pm_qos_constraints_destroy
217 * @dev: target device
219 * Called from the device PM subsystem on device removal under device_pm_lock().
221 void dev_pm_qos_constraints_destroy(struct device *dev)
223 struct dev_pm_qos *qos;
224 struct dev_pm_qos_request *req, *tmp;
225 struct pm_qos_constraints *c;
226 struct pm_qos_flags *f;
229 * If the device's PM QoS resume latency limit or PM QoS flags have been
230 * exposed to user space, they have to be hidden at this point.
232 dev_pm_qos_hide_latency_limit(dev);
233 dev_pm_qos_hide_flags(dev);
235 mutex_lock(&dev_pm_qos_mtx);
237 dev->power.power_state = PMSG_INVALID;
238 qos = dev->power.qos;
242 /* Flush the constraints lists for the device. */
244 plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) {
246 * Update constraints list and call the notification
247 * callbacks if needed
249 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
250 memset(req, 0, sizeof(*req));
253 list_for_each_entry_safe(req, tmp, &f->list, data.flr.node) {
254 apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE);
255 memset(req, 0, sizeof(*req));
258 spin_lock_irq(&dev->power.lock);
259 dev->power.qos = NULL;
260 spin_unlock_irq(&dev->power.lock);
266 mutex_unlock(&dev_pm_qos_mtx);
270 * dev_pm_qos_add_request - inserts new qos request into the list
271 * @dev: target device for the constraint
272 * @req: pointer to a preallocated handle
273 * @type: type of the request
274 * @value: defines the qos request
276 * This function inserts a new entry in the device constraints list of
277 * requested qos performance characteristics. It recomputes the aggregate
278 * QoS expectations of parameters and initializes the dev_pm_qos_request
279 * handle. Caller needs to save this handle for later use in updates and
282 * Returns 1 if the aggregated constraint value has changed,
283 * 0 if the aggregated constraint value has not changed,
284 * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory
285 * to allocate for data structures, -ENODEV if the device has just been removed
288 * Callers should ensure that the target device is not RPM_SUSPENDED before
289 * using this function for requests of type DEV_PM_QOS_FLAGS.
291 int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req,
292 enum dev_pm_qos_req_type type, s32 value)
296 if (!dev || !req) /*guard against callers passing in null */
299 if (WARN(dev_pm_qos_request_active(req),
300 "%s() called for already added request\n", __func__))
305 mutex_lock(&dev_pm_qos_mtx);
307 if (!dev->power.qos) {
308 if (dev->power.power_state.event == PM_EVENT_INVALID) {
309 /* The device has been removed from the system. */
315 * Allocate the constraints data on the first call to
316 * add_request, i.e. only if the data is not already
317 * allocated and if the device has not been removed.
319 ret = dev_pm_qos_constraints_allocate(dev);
325 ret = apply_constraint(req, PM_QOS_ADD_REQ, value);
329 mutex_unlock(&dev_pm_qos_mtx);
333 EXPORT_SYMBOL_GPL(dev_pm_qos_add_request);
336 * __dev_pm_qos_update_request - Modify an existing device PM QoS request.
337 * @req : PM QoS request to modify.
338 * @new_value: New value to request.
340 static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req,
346 if (!req->dev->power.qos)
350 case DEV_PM_QOS_LATENCY:
351 curr_value = req->data.pnode.prio;
353 case DEV_PM_QOS_FLAGS:
354 curr_value = req->data.flr.flags;
360 if (curr_value != new_value)
361 ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value);
367 * dev_pm_qos_update_request - modifies an existing qos request
368 * @req : handle to list element holding a dev_pm_qos request to use
369 * @new_value: defines the qos request
371 * Updates an existing dev PM qos request along with updating the
374 * Attempts are made to make this code callable on hot code paths.
376 * Returns 1 if the aggregated constraint value has changed,
377 * 0 if the aggregated constraint value has not changed,
378 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
379 * removed from the system
381 * Callers should ensure that the target device is not RPM_SUSPENDED before
382 * using this function for requests of type DEV_PM_QOS_FLAGS.
384 int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value)
388 if (!req) /*guard against callers passing in null */
391 if (WARN(!dev_pm_qos_request_active(req),
392 "%s() called for unknown object\n", __func__))
395 mutex_lock(&dev_pm_qos_mtx);
396 ret = __dev_pm_qos_update_request(req, new_value);
397 mutex_unlock(&dev_pm_qos_mtx);
401 EXPORT_SYMBOL_GPL(dev_pm_qos_update_request);
404 * dev_pm_qos_remove_request - modifies an existing qos request
405 * @req: handle to request list element
407 * Will remove pm qos request from the list of constraints and
408 * recompute the current target value. Call this on slow code paths.
410 * Returns 1 if the aggregated constraint value has changed,
411 * 0 if the aggregated constraint value has not changed,
412 * -EINVAL in case of wrong parameters, -ENODEV if the device has been
413 * removed from the system
415 * Callers should ensure that the target device is not RPM_SUSPENDED before
416 * using this function for requests of type DEV_PM_QOS_FLAGS.
418 int dev_pm_qos_remove_request(struct dev_pm_qos_request *req)
422 if (!req) /*guard against callers passing in null */
425 if (WARN(!dev_pm_qos_request_active(req),
426 "%s() called for unknown object\n", __func__))
429 mutex_lock(&dev_pm_qos_mtx);
431 if (req->dev->power.qos) {
432 ret = apply_constraint(req, PM_QOS_REMOVE_REQ,
433 PM_QOS_DEFAULT_VALUE);
434 memset(req, 0, sizeof(*req));
436 /* Return if the device has been removed */
440 mutex_unlock(&dev_pm_qos_mtx);
443 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request);
446 * dev_pm_qos_add_notifier - sets notification entry for changes to target value
447 * of per-device PM QoS constraints
449 * @dev: target device for the constraint
450 * @notifier: notifier block managed by caller.
452 * Will register the notifier into a notification chain that gets called
453 * upon changes to the target value for the device.
455 * If the device's constraints object doesn't exist when this routine is called,
456 * it will be created (or error code will be returned if that fails).
458 int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier)
462 mutex_lock(&dev_pm_qos_mtx);
465 ret = dev->power.power_state.event != PM_EVENT_INVALID ?
466 dev_pm_qos_constraints_allocate(dev) : -ENODEV;
469 ret = blocking_notifier_chain_register(
470 dev->power.qos->latency.notifiers, notifier);
472 mutex_unlock(&dev_pm_qos_mtx);
475 EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier);
478 * dev_pm_qos_remove_notifier - deletes notification for changes to target value
479 * of per-device PM QoS constraints
481 * @dev: target device for the constraint
482 * @notifier: notifier block to be removed.
484 * Will remove the notifier from the notification chain that gets called
485 * upon changes to the target value.
487 int dev_pm_qos_remove_notifier(struct device *dev,
488 struct notifier_block *notifier)
492 mutex_lock(&dev_pm_qos_mtx);
494 /* Silently return if the constraints object is not present. */
496 retval = blocking_notifier_chain_unregister(
497 dev->power.qos->latency.notifiers,
500 mutex_unlock(&dev_pm_qos_mtx);
503 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier);
506 * dev_pm_qos_add_global_notifier - sets notification entry for changes to
507 * target value of the PM QoS constraints for any device
509 * @notifier: notifier block managed by caller.
511 * Will register the notifier into a notification chain that gets called
512 * upon changes to the target value for any device.
514 int dev_pm_qos_add_global_notifier(struct notifier_block *notifier)
516 return blocking_notifier_chain_register(&dev_pm_notifiers, notifier);
518 EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier);
521 * dev_pm_qos_remove_global_notifier - deletes notification for changes to
522 * target value of PM QoS constraints for any device
524 * @notifier: notifier block to be removed.
526 * Will remove the notifier from the notification chain that gets called
527 * upon changes to the target value for any device.
529 int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier)
531 return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier);
533 EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier);
536 * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor.
537 * @dev: Device whose ancestor to add the request for.
538 * @req: Pointer to the preallocated handle.
539 * @value: Constraint latency value.
541 int dev_pm_qos_add_ancestor_request(struct device *dev,
542 struct dev_pm_qos_request *req, s32 value)
544 struct device *ancestor = dev->parent;
547 while (ancestor && !ancestor->power.ignore_children)
548 ancestor = ancestor->parent;
551 error = dev_pm_qos_add_request(ancestor, req,
552 DEV_PM_QOS_LATENCY, value);
559 EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request);
561 #ifdef CONFIG_PM_RUNTIME
562 static void __dev_pm_qos_drop_user_request(struct device *dev,
563 enum dev_pm_qos_req_type type)
566 case DEV_PM_QOS_LATENCY:
567 dev_pm_qos_remove_request(dev->power.qos->latency_req);
568 dev->power.qos->latency_req = NULL;
570 case DEV_PM_QOS_FLAGS:
571 dev_pm_qos_remove_request(dev->power.qos->flags_req);
572 dev->power.qos->flags_req = NULL;
578 * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space.
579 * @dev: Device whose PM QoS latency limit is to be exposed to user space.
580 * @value: Initial value of the latency limit.
582 int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value)
584 struct dev_pm_qos_request *req;
587 if (!device_is_registered(dev) || value < 0)
590 if (dev->power.qos && dev->power.qos->latency_req)
593 req = kzalloc(sizeof(*req), GFP_KERNEL);
597 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY, value);
601 dev->power.qos->latency_req = req;
602 ret = pm_qos_sysfs_add_latency(dev);
604 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
608 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit);
611 * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space.
612 * @dev: Device whose PM QoS latency limit is to be hidden from user space.
614 void dev_pm_qos_hide_latency_limit(struct device *dev)
616 if (dev->power.qos && dev->power.qos->latency_req) {
617 pm_qos_sysfs_remove_latency(dev);
618 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY);
621 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit);
624 * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space.
625 * @dev: Device whose PM QoS flags are to be exposed to user space.
626 * @val: Initial values of the flags.
628 int dev_pm_qos_expose_flags(struct device *dev, s32 val)
630 struct dev_pm_qos_request *req;
633 if (!device_is_registered(dev))
636 if (dev->power.qos && dev->power.qos->flags_req)
639 req = kzalloc(sizeof(*req), GFP_KERNEL);
643 pm_runtime_get_sync(dev);
644 ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val);
648 dev->power.qos->flags_req = req;
649 ret = pm_qos_sysfs_add_flags(dev);
651 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
657 EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags);
660 * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space.
661 * @dev: Device whose PM QoS flags are to be hidden from user space.
663 void dev_pm_qos_hide_flags(struct device *dev)
665 if (dev->power.qos && dev->power.qos->flags_req) {
666 pm_qos_sysfs_remove_flags(dev);
667 pm_runtime_get_sync(dev);
668 __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS);
672 EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags);
675 * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space.
676 * @dev: Device to update the PM QoS flags request for.
677 * @mask: Flags to set/clear.
678 * @set: Whether to set or clear the flags (true means set).
680 int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set)
685 if (!dev->power.qos || !dev->power.qos->flags_req)
688 pm_runtime_get_sync(dev);
689 mutex_lock(&dev_pm_qos_mtx);
691 value = dev_pm_qos_requested_flags(dev);
697 ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value);
699 mutex_unlock(&dev_pm_qos_mtx);
704 #endif /* CONFIG_PM_RUNTIME */