Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Devices PM QoS constraints management |
| 3 | * |
| 4 | * Copyright (C) 2011 Texas Instruments, Inc. |
| 5 | * |
| 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. |
| 9 | * |
| 10 | * |
| 11 | * This module exposes the interface to kernel space for specifying |
| 12 | * per-device PM QoS dependencies. It provides infrastructure for registration |
| 13 | * of: |
| 14 | * |
| 15 | * Dependents on a QoS value : register requests |
| 16 | * Watchers of QoS value : get notified when target QoS value changes |
| 17 | * |
| 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. |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 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. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 26 | * |
| 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. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 33 | * . A global mutex protects the constraints users from the data being |
| 34 | * allocated and free'd. |
| 35 | */ |
| 36 | |
| 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> |
Paul Gortmaker | 1b6bc32 | 2011-05-27 07:12:15 -0400 | [diff] [blame] | 42 | #include <linux/export.h> |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 43 | #include <linux/pm_runtime.h> |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 44 | |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 45 | #include "power.h" |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 46 | |
| 47 | static DEFINE_MUTEX(dev_pm_qos_mtx); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 48 | |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 49 | static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers); |
| 50 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 51 | /** |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 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. |
| 55 | * |
| 56 | * This routine must be called with dev->power.lock held. |
| 57 | */ |
| 58 | enum pm_qos_flags_status __dev_pm_qos_flags(struct device *dev, s32 mask) |
| 59 | { |
| 60 | struct dev_pm_qos *qos = dev->power.qos; |
| 61 | struct pm_qos_flags *pqf; |
| 62 | s32 val; |
| 63 | |
| 64 | if (!qos) |
| 65 | return PM_QOS_FLAGS_UNDEFINED; |
| 66 | |
| 67 | pqf = &qos->flags; |
| 68 | if (list_empty(&pqf->list)) |
| 69 | return PM_QOS_FLAGS_UNDEFINED; |
| 70 | |
| 71 | val = pqf->effective_flags & mask; |
| 72 | if (val) |
| 73 | return (val == mask) ? PM_QOS_FLAGS_ALL : PM_QOS_FLAGS_SOME; |
| 74 | |
| 75 | return PM_QOS_FLAGS_NONE; |
| 76 | } |
| 77 | |
| 78 | /** |
| 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. |
| 82 | */ |
| 83 | enum pm_qos_flags_status dev_pm_qos_flags(struct device *dev, s32 mask) |
| 84 | { |
| 85 | unsigned long irqflags; |
| 86 | enum pm_qos_flags_status ret; |
| 87 | |
| 88 | spin_lock_irqsave(&dev->power.lock, irqflags); |
| 89 | ret = __dev_pm_qos_flags(dev, mask); |
| 90 | spin_unlock_irqrestore(&dev->power.lock, irqflags); |
| 91 | |
| 92 | return ret; |
| 93 | } |
Lan Tianyu | 6802771 | 2013-01-23 04:26:28 +0800 | [diff] [blame] | 94 | EXPORT_SYMBOL_GPL(dev_pm_qos_flags); |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 95 | |
| 96 | /** |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 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. |
| 99 | * |
| 100 | * This routine must be called with dev->power.lock held. |
| 101 | */ |
| 102 | s32 __dev_pm_qos_read_value(struct device *dev) |
| 103 | { |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 104 | return dev->power.qos ? pm_qos_read_value(&dev->power.qos->latency) : 0; |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * dev_pm_qos_read_value - Get PM QoS constraint for a given device (locked). |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 109 | * @dev: Device to get the PM QoS constraint value for. |
| 110 | */ |
| 111 | s32 dev_pm_qos_read_value(struct device *dev) |
| 112 | { |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 113 | unsigned long flags; |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 114 | s32 ret; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 115 | |
| 116 | spin_lock_irqsave(&dev->power.lock, flags); |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 117 | ret = __dev_pm_qos_read_value(dev); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 118 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 123 | /** |
| 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. |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 128 | * |
| 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 |
| 131 | * callbacks |
| 132 | */ |
| 133 | static int apply_constraint(struct dev_pm_qos_request *req, |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 134 | enum pm_qos_req_action action, s32 value) |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 135 | { |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 136 | struct dev_pm_qos *qos = req->dev->power.qos; |
| 137 | int ret; |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 138 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 139 | switch(req->type) { |
| 140 | case DEV_PM_QOS_LATENCY: |
| 141 | ret = pm_qos_update_target(&qos->latency, &req->data.pnode, |
| 142 | action, value); |
| 143 | if (ret) { |
| 144 | value = pm_qos_read_value(&qos->latency); |
| 145 | blocking_notifier_call_chain(&dev_pm_notifiers, |
| 146 | (unsigned long)value, |
| 147 | req); |
| 148 | } |
| 149 | break; |
| 150 | case DEV_PM_QOS_FLAGS: |
| 151 | ret = pm_qos_update_flags(&qos->flags, &req->data.flr, |
| 152 | action, value); |
| 153 | break; |
| 154 | default: |
| 155 | ret = -EINVAL; |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return ret; |
| 159 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * dev_pm_qos_constraints_allocate |
| 163 | * @dev: device to allocate data for |
| 164 | * |
| 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 |
| 167 | */ |
| 168 | static int dev_pm_qos_constraints_allocate(struct device *dev) |
| 169 | { |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 170 | struct dev_pm_qos *qos; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 171 | struct pm_qos_constraints *c; |
| 172 | struct blocking_notifier_head *n; |
| 173 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 174 | qos = kzalloc(sizeof(*qos), GFP_KERNEL); |
| 175 | if (!qos) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 176 | return -ENOMEM; |
| 177 | |
| 178 | n = kzalloc(sizeof(*n), GFP_KERNEL); |
| 179 | if (!n) { |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 180 | kfree(qos); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 181 | return -ENOMEM; |
| 182 | } |
| 183 | BLOCKING_INIT_NOTIFIER_HEAD(n); |
| 184 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 185 | c = &qos->latency; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 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; |
| 190 | c->notifiers = n; |
| 191 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 192 | INIT_LIST_HEAD(&qos->flags.list); |
| 193 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 194 | spin_lock_irq(&dev->power.lock); |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 195 | dev->power.qos = qos; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 196 | spin_unlock_irq(&dev->power.lock); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /** |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 202 | * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 203 | * @dev: target device |
| 204 | * |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 205 | * Called from the device PM subsystem during device insertion under |
| 206 | * device_pm_lock(). |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 207 | */ |
| 208 | void dev_pm_qos_constraints_init(struct device *dev) |
| 209 | { |
| 210 | mutex_lock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 211 | dev->power.qos = NULL; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 212 | dev->power.power_state = PMSG_ON; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 213 | mutex_unlock(&dev_pm_qos_mtx); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * dev_pm_qos_constraints_destroy |
| 218 | * @dev: target device |
| 219 | * |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 220 | * Called from the device PM subsystem on device removal under device_pm_lock(). |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 221 | */ |
| 222 | void dev_pm_qos_constraints_destroy(struct device *dev) |
| 223 | { |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 224 | struct dev_pm_qos *qos; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 225 | struct dev_pm_qos_request *req, *tmp; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 226 | struct pm_qos_constraints *c; |
Rafael J. Wysocki | 35546bd | 2012-11-24 10:10:51 +0100 | [diff] [blame] | 227 | struct pm_qos_flags *f; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 228 | |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 229 | /* |
Rafael J. Wysocki | 35546bd | 2012-11-24 10:10:51 +0100 | [diff] [blame] | 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. |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 232 | */ |
| 233 | dev_pm_qos_hide_latency_limit(dev); |
Rafael J. Wysocki | 35546bd | 2012-11-24 10:10:51 +0100 | [diff] [blame] | 234 | dev_pm_qos_hide_flags(dev); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 235 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 236 | mutex_lock(&dev_pm_qos_mtx); |
| 237 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 238 | dev->power.power_state = PMSG_INVALID; |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 239 | qos = dev->power.qos; |
| 240 | if (!qos) |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 241 | goto out; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 242 | |
Rafael J. Wysocki | 35546bd | 2012-11-24 10:10:51 +0100 | [diff] [blame] | 243 | /* Flush the constraints lists for the device. */ |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 244 | c = &qos->latency; |
Rafael J. Wysocki | 021c870 | 2012-10-23 01:09:00 +0200 | [diff] [blame] | 245 | plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) { |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 246 | /* |
| 247 | * Update constraints list and call the notification |
| 248 | * callbacks if needed |
| 249 | */ |
| 250 | apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE); |
| 251 | memset(req, 0, sizeof(*req)); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 252 | } |
Rafael J. Wysocki | 35546bd | 2012-11-24 10:10:51 +0100 | [diff] [blame] | 253 | f = &qos->flags; |
| 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)); |
| 257 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 258 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 259 | spin_lock_irq(&dev->power.lock); |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 260 | dev->power.qos = NULL; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 261 | spin_unlock_irq(&dev->power.lock); |
| 262 | |
| 263 | kfree(c->notifiers); |
Lan,Tianyu | 9eaee2c | 2012-11-01 22:45:30 +0100 | [diff] [blame] | 264 | kfree(qos); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 265 | |
| 266 | out: |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 267 | mutex_unlock(&dev_pm_qos_mtx); |
| 268 | } |
| 269 | |
| 270 | /** |
| 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 |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 274 | * @type: type of the request |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 275 | * @value: defines the qos request |
| 276 | * |
| 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 |
| 281 | * removal. |
| 282 | * |
| 283 | * Returns 1 if the aggregated constraint value has changed, |
| 284 | * 0 if the aggregated constraint value has not changed, |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 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 |
| 287 | * from the system. |
Rafael J. Wysocki | 436ede8 | 2012-11-02 13:10:09 +0100 | [diff] [blame] | 288 | * |
| 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. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 291 | */ |
| 292 | int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req, |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 293 | enum dev_pm_qos_req_type type, s32 value) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 294 | { |
| 295 | int ret = 0; |
| 296 | |
| 297 | if (!dev || !req) /*guard against callers passing in null */ |
| 298 | return -EINVAL; |
| 299 | |
Guennadi Liakhovetski | af4c720 | 2011-11-10 00:44:18 +0100 | [diff] [blame] | 300 | if (WARN(dev_pm_qos_request_active(req), |
| 301 | "%s() called for already added request\n", __func__)) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 302 | return -EINVAL; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 303 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 304 | req->dev = dev; |
| 305 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 306 | mutex_lock(&dev_pm_qos_mtx); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 307 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 308 | if (!dev->power.qos) { |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 309 | if (dev->power.power_state.event == PM_EVENT_INVALID) { |
| 310 | /* The device has been removed from the system. */ |
| 311 | req->dev = NULL; |
| 312 | ret = -ENODEV; |
| 313 | goto out; |
| 314 | } else { |
| 315 | /* |
| 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. |
| 319 | */ |
| 320 | ret = dev_pm_qos_constraints_allocate(dev); |
| 321 | } |
| 322 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 323 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 324 | if (!ret) { |
| 325 | req->type = type; |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 326 | ret = apply_constraint(req, PM_QOS_ADD_REQ, value); |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 327 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 328 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 329 | out: |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 330 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 331 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 332 | return ret; |
| 333 | } |
| 334 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_request); |
| 335 | |
| 336 | /** |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 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. |
| 340 | */ |
| 341 | static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req, |
| 342 | s32 new_value) |
| 343 | { |
| 344 | s32 curr_value; |
| 345 | int ret = 0; |
| 346 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 347 | if (!req) /*guard against callers passing in null */ |
| 348 | return -EINVAL; |
| 349 | |
| 350 | if (WARN(!dev_pm_qos_request_active(req), |
| 351 | "%s() called for unknown object\n", __func__)) |
| 352 | return -EINVAL; |
| 353 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 354 | if (!req->dev->power.qos) |
| 355 | return -ENODEV; |
| 356 | |
| 357 | switch(req->type) { |
| 358 | case DEV_PM_QOS_LATENCY: |
| 359 | curr_value = req->data.pnode.prio; |
| 360 | break; |
| 361 | case DEV_PM_QOS_FLAGS: |
| 362 | curr_value = req->data.flr.flags; |
| 363 | break; |
| 364 | default: |
| 365 | return -EINVAL; |
| 366 | } |
| 367 | |
| 368 | if (curr_value != new_value) |
| 369 | ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value); |
| 370 | |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | /** |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 375 | * dev_pm_qos_update_request - modifies an existing qos request |
| 376 | * @req : handle to list element holding a dev_pm_qos request to use |
| 377 | * @new_value: defines the qos request |
| 378 | * |
| 379 | * Updates an existing dev PM qos request along with updating the |
| 380 | * target value. |
| 381 | * |
| 382 | * Attempts are made to make this code callable on hot code paths. |
| 383 | * |
| 384 | * Returns 1 if the aggregated constraint value has changed, |
| 385 | * 0 if the aggregated constraint value has not changed, |
| 386 | * -EINVAL in case of wrong parameters, -ENODEV if the device has been |
| 387 | * removed from the system |
Rafael J. Wysocki | 436ede8 | 2012-11-02 13:10:09 +0100 | [diff] [blame] | 388 | * |
| 389 | * Callers should ensure that the target device is not RPM_SUSPENDED before |
| 390 | * using this function for requests of type DEV_PM_QOS_FLAGS. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 391 | */ |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 392 | int dev_pm_qos_update_request(struct dev_pm_qos_request *req, s32 new_value) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 393 | { |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 394 | int ret; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 395 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 396 | mutex_lock(&dev_pm_qos_mtx); |
| 397 | ret = __dev_pm_qos_update_request(req, new_value); |
| 398 | mutex_unlock(&dev_pm_qos_mtx); |
| 399 | return ret; |
| 400 | } |
| 401 | EXPORT_SYMBOL_GPL(dev_pm_qos_update_request); |
| 402 | |
| 403 | static int __dev_pm_qos_remove_request(struct dev_pm_qos_request *req) |
| 404 | { |
| 405 | int ret = 0; |
| 406 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 407 | if (!req) /*guard against callers passing in null */ |
| 408 | return -EINVAL; |
| 409 | |
Guennadi Liakhovetski | af4c720 | 2011-11-10 00:44:18 +0100 | [diff] [blame] | 410 | if (WARN(!dev_pm_qos_request_active(req), |
| 411 | "%s() called for unknown object\n", __func__)) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 412 | return -EINVAL; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 413 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 414 | if (req->dev->power.qos) { |
| 415 | ret = apply_constraint(req, PM_QOS_REMOVE_REQ, |
| 416 | PM_QOS_DEFAULT_VALUE); |
| 417 | memset(req, 0, sizeof(*req)); |
| 418 | } else { |
| 419 | ret = -ENODEV; |
| 420 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 421 | return ret; |
| 422 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 423 | |
| 424 | /** |
| 425 | * dev_pm_qos_remove_request - modifies an existing qos request |
| 426 | * @req: handle to request list element |
| 427 | * |
| 428 | * Will remove pm qos request from the list of constraints and |
| 429 | * recompute the current target value. Call this on slow code paths. |
| 430 | * |
| 431 | * Returns 1 if the aggregated constraint value has changed, |
| 432 | * 0 if the aggregated constraint value has not changed, |
| 433 | * -EINVAL in case of wrong parameters, -ENODEV if the device has been |
| 434 | * removed from the system |
Rafael J. Wysocki | 436ede8 | 2012-11-02 13:10:09 +0100 | [diff] [blame] | 435 | * |
| 436 | * Callers should ensure that the target device is not RPM_SUSPENDED before |
| 437 | * using this function for requests of type DEV_PM_QOS_FLAGS. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 438 | */ |
| 439 | int dev_pm_qos_remove_request(struct dev_pm_qos_request *req) |
| 440 | { |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 441 | int ret; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 442 | |
| 443 | mutex_lock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 444 | ret = __dev_pm_qos_remove_request(req); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 445 | mutex_unlock(&dev_pm_qos_mtx); |
| 446 | return ret; |
| 447 | } |
| 448 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request); |
| 449 | |
| 450 | /** |
| 451 | * dev_pm_qos_add_notifier - sets notification entry for changes to target value |
| 452 | * of per-device PM QoS constraints |
| 453 | * |
| 454 | * @dev: target device for the constraint |
| 455 | * @notifier: notifier block managed by caller. |
| 456 | * |
| 457 | * Will register the notifier into a notification chain that gets called |
| 458 | * upon changes to the target value for the device. |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 459 | * |
| 460 | * If the device's constraints object doesn't exist when this routine is called, |
| 461 | * it will be created (or error code will be returned if that fails). |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 462 | */ |
| 463 | int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier) |
| 464 | { |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 465 | int ret = 0; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 466 | |
| 467 | mutex_lock(&dev_pm_qos_mtx); |
| 468 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 469 | if (!dev->power.qos) |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 470 | ret = dev->power.power_state.event != PM_EVENT_INVALID ? |
| 471 | dev_pm_qos_constraints_allocate(dev) : -ENODEV; |
| 472 | |
| 473 | if (!ret) |
| 474 | ret = blocking_notifier_chain_register( |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 475 | dev->power.qos->latency.notifiers, notifier); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 476 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 477 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 478 | return ret; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 479 | } |
| 480 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier); |
| 481 | |
| 482 | /** |
| 483 | * dev_pm_qos_remove_notifier - deletes notification for changes to target value |
| 484 | * of per-device PM QoS constraints |
| 485 | * |
| 486 | * @dev: target device for the constraint |
| 487 | * @notifier: notifier block to be removed. |
| 488 | * |
| 489 | * Will remove the notifier from the notification chain that gets called |
| 490 | * upon changes to the target value. |
| 491 | */ |
| 492 | int dev_pm_qos_remove_notifier(struct device *dev, |
| 493 | struct notifier_block *notifier) |
| 494 | { |
| 495 | int retval = 0; |
| 496 | |
| 497 | mutex_lock(&dev_pm_qos_mtx); |
| 498 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 499 | /* Silently return if the constraints object is not present. */ |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 500 | if (dev->power.qos) |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 501 | retval = blocking_notifier_chain_unregister( |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 502 | dev->power.qos->latency.notifiers, |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 503 | notifier); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 504 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 505 | mutex_unlock(&dev_pm_qos_mtx); |
| 506 | return retval; |
| 507 | } |
| 508 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier); |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 509 | |
| 510 | /** |
| 511 | * dev_pm_qos_add_global_notifier - sets notification entry for changes to |
| 512 | * target value of the PM QoS constraints for any device |
| 513 | * |
| 514 | * @notifier: notifier block managed by caller. |
| 515 | * |
| 516 | * Will register the notifier into a notification chain that gets called |
| 517 | * upon changes to the target value for any device. |
| 518 | */ |
| 519 | int dev_pm_qos_add_global_notifier(struct notifier_block *notifier) |
| 520 | { |
| 521 | return blocking_notifier_chain_register(&dev_pm_notifiers, notifier); |
| 522 | } |
| 523 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier); |
| 524 | |
| 525 | /** |
| 526 | * dev_pm_qos_remove_global_notifier - deletes notification for changes to |
| 527 | * target value of PM QoS constraints for any device |
| 528 | * |
| 529 | * @notifier: notifier block to be removed. |
| 530 | * |
| 531 | * Will remove the notifier from the notification chain that gets called |
| 532 | * upon changes to the target value for any device. |
| 533 | */ |
| 534 | int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier) |
| 535 | { |
| 536 | return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier); |
| 537 | } |
| 538 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier); |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 539 | |
| 540 | /** |
| 541 | * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor. |
| 542 | * @dev: Device whose ancestor to add the request for. |
| 543 | * @req: Pointer to the preallocated handle. |
| 544 | * @value: Constraint latency value. |
| 545 | */ |
| 546 | int dev_pm_qos_add_ancestor_request(struct device *dev, |
| 547 | struct dev_pm_qos_request *req, s32 value) |
| 548 | { |
| 549 | struct device *ancestor = dev->parent; |
Rafael J. Wysocki | 4ce4780 | 2012-12-18 14:07:49 +0100 | [diff] [blame] | 550 | int ret = -ENODEV; |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 551 | |
| 552 | while (ancestor && !ancestor->power.ignore_children) |
| 553 | ancestor = ancestor->parent; |
| 554 | |
| 555 | if (ancestor) |
Rafael J. Wysocki | 4ce4780 | 2012-12-18 14:07:49 +0100 | [diff] [blame] | 556 | ret = dev_pm_qos_add_request(ancestor, req, |
| 557 | DEV_PM_QOS_LATENCY, value); |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 558 | |
Rafael J. Wysocki | 4ce4780 | 2012-12-18 14:07:49 +0100 | [diff] [blame] | 559 | if (ret < 0) |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 560 | req->dev = NULL; |
| 561 | |
Rafael J. Wysocki | 4ce4780 | 2012-12-18 14:07:49 +0100 | [diff] [blame] | 562 | return ret; |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 563 | } |
| 564 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 565 | |
| 566 | #ifdef CONFIG_PM_RUNTIME |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 567 | static void __dev_pm_qos_drop_user_request(struct device *dev, |
| 568 | enum dev_pm_qos_req_type type) |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 569 | { |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 570 | struct dev_pm_qos_request *req = NULL; |
| 571 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 572 | switch(type) { |
| 573 | case DEV_PM_QOS_LATENCY: |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 574 | req = dev->power.qos->latency_req; |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 575 | dev->power.qos->latency_req = NULL; |
| 576 | break; |
| 577 | case DEV_PM_QOS_FLAGS: |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 578 | req = dev->power.qos->flags_req; |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 579 | dev->power.qos->flags_req = NULL; |
| 580 | break; |
| 581 | } |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 582 | __dev_pm_qos_remove_request(req); |
| 583 | kfree(req); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | /** |
| 587 | * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space. |
| 588 | * @dev: Device whose PM QoS latency limit is to be exposed to user space. |
| 589 | * @value: Initial value of the latency limit. |
| 590 | */ |
| 591 | int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value) |
| 592 | { |
| 593 | struct dev_pm_qos_request *req; |
| 594 | int ret; |
| 595 | |
| 596 | if (!device_is_registered(dev) || value < 0) |
| 597 | return -EINVAL; |
| 598 | |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 599 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 600 | if (!req) |
| 601 | return -ENOMEM; |
| 602 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 603 | ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY, value); |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 604 | if (ret < 0) { |
| 605 | kfree(req); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 606 | return ret; |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 607 | } |
| 608 | |
| 609 | mutex_lock(&dev_pm_qos_mtx); |
| 610 | |
| 611 | if (!dev->power.qos) |
| 612 | ret = -ENODEV; |
| 613 | else if (dev->power.qos->latency_req) |
| 614 | ret = -EEXIST; |
| 615 | |
| 616 | if (ret < 0) { |
| 617 | __dev_pm_qos_remove_request(req); |
| 618 | kfree(req); |
| 619 | goto out; |
| 620 | } |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 621 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 622 | dev->power.qos->latency_req = req; |
| 623 | ret = pm_qos_sysfs_add_latency(dev); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 624 | if (ret) |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 625 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 626 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 627 | out: |
| 628 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 629 | return ret; |
| 630 | } |
| 631 | EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit); |
| 632 | |
| 633 | /** |
| 634 | * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space. |
| 635 | * @dev: Device whose PM QoS latency limit is to be hidden from user space. |
| 636 | */ |
| 637 | void dev_pm_qos_hide_latency_limit(struct device *dev) |
| 638 | { |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 639 | mutex_lock(&dev_pm_qos_mtx); |
| 640 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 641 | if (dev->power.qos && dev->power.qos->latency_req) { |
| 642 | pm_qos_sysfs_remove_latency(dev); |
| 643 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 644 | } |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 645 | |
| 646 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 647 | } |
| 648 | EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 649 | |
| 650 | /** |
| 651 | * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space. |
| 652 | * @dev: Device whose PM QoS flags are to be exposed to user space. |
| 653 | * @val: Initial values of the flags. |
| 654 | */ |
| 655 | int dev_pm_qos_expose_flags(struct device *dev, s32 val) |
| 656 | { |
| 657 | struct dev_pm_qos_request *req; |
| 658 | int ret; |
| 659 | |
| 660 | if (!device_is_registered(dev)) |
| 661 | return -EINVAL; |
| 662 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 663 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 664 | if (!req) |
| 665 | return -ENOMEM; |
| 666 | |
| 667 | ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val); |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 668 | if (ret < 0) { |
| 669 | kfree(req); |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | pm_runtime_get_sync(dev); |
| 674 | mutex_lock(&dev_pm_qos_mtx); |
| 675 | |
| 676 | if (!dev->power.qos) |
| 677 | ret = -ENODEV; |
| 678 | else if (dev->power.qos->flags_req) |
| 679 | ret = -EEXIST; |
| 680 | |
| 681 | if (ret < 0) { |
| 682 | __dev_pm_qos_remove_request(req); |
| 683 | kfree(req); |
| 684 | goto out; |
| 685 | } |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 686 | |
| 687 | dev->power.qos->flags_req = req; |
| 688 | ret = pm_qos_sysfs_add_flags(dev); |
| 689 | if (ret) |
| 690 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS); |
| 691 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 692 | out: |
| 693 | mutex_unlock(&dev_pm_qos_mtx); |
Lan Tianyu | 7e4d684 | 2012-11-08 11:14:08 +0800 | [diff] [blame] | 694 | pm_runtime_put(dev); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 695 | return ret; |
| 696 | } |
| 697 | EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags); |
| 698 | |
| 699 | /** |
| 700 | * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space. |
| 701 | * @dev: Device whose PM QoS flags are to be hidden from user space. |
| 702 | */ |
| 703 | void dev_pm_qos_hide_flags(struct device *dev) |
| 704 | { |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 705 | pm_runtime_get_sync(dev); |
| 706 | mutex_lock(&dev_pm_qos_mtx); |
| 707 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 708 | if (dev->power.qos && dev->power.qos->flags_req) { |
| 709 | pm_qos_sysfs_remove_flags(dev); |
| 710 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS); |
| 711 | } |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 712 | |
| 713 | mutex_unlock(&dev_pm_qos_mtx); |
| 714 | pm_runtime_put(dev); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 715 | } |
| 716 | EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags); |
| 717 | |
| 718 | /** |
| 719 | * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space. |
| 720 | * @dev: Device to update the PM QoS flags request for. |
| 721 | * @mask: Flags to set/clear. |
| 722 | * @set: Whether to set or clear the flags (true means set). |
| 723 | */ |
| 724 | int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set) |
| 725 | { |
| 726 | s32 value; |
| 727 | int ret; |
| 728 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 729 | pm_runtime_get_sync(dev); |
| 730 | mutex_lock(&dev_pm_qos_mtx); |
| 731 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 732 | if (!dev->power.qos || !dev->power.qos->flags_req) { |
| 733 | ret = -EINVAL; |
| 734 | goto out; |
| 735 | } |
| 736 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 737 | value = dev_pm_qos_requested_flags(dev); |
| 738 | if (set) |
| 739 | value |= mask; |
| 740 | else |
| 741 | value &= ~mask; |
| 742 | |
| 743 | ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value); |
| 744 | |
Rafael J. Wysocki | b81ea1b | 2013-03-03 22:48:14 +0100 | [diff] [blame^] | 745 | out: |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 746 | mutex_unlock(&dev_pm_qos_mtx); |
| 747 | pm_runtime_put(dev); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 748 | return ret; |
| 749 | } |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 750 | #endif /* CONFIG_PM_RUNTIME */ |