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. |
| 33 | * . The constraints_state variable from dev_pm_info tracks the data struct |
| 34 | * allocation state: |
| 35 | * DEV_PM_QOS_NO_DEVICE: No device present or device removed, no data |
| 36 | * allocated, |
| 37 | * DEV_PM_QOS_DEVICE_PRESENT: Device present, data not allocated and will be |
| 38 | * allocated at the first call to dev_pm_qos_add_request, |
| 39 | * DEV_PM_QOS_ALLOCATED: Device present, data allocated. The per-device |
| 40 | * PM QoS constraints framework is operational and constraints can be |
| 41 | * added, updated or removed using the dev_pm_qos_* API. |
| 42 | * . A global mutex protects the constraints users from the data being |
| 43 | * allocated and free'd. |
| 44 | */ |
| 45 | |
| 46 | #include <linux/pm_qos.h> |
| 47 | #include <linux/spinlock.h> |
| 48 | #include <linux/slab.h> |
| 49 | #include <linux/device.h> |
| 50 | #include <linux/mutex.h> |
| 51 | |
| 52 | |
| 53 | static DEFINE_MUTEX(dev_pm_qos_mtx); |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame^] | 54 | static BLOCKING_NOTIFIER_HEAD(dev_pm_notifiers); |
| 55 | |
| 56 | /* |
| 57 | * apply_constraint |
| 58 | * @req: constraint request to apply |
| 59 | * @action: action to perform add/update/remove, of type enum pm_qos_req_action |
| 60 | * @value: defines the qos request |
| 61 | * |
| 62 | * Internal function to update the constraints list using the PM QoS core |
| 63 | * code and if needed call the per-device and the global notification |
| 64 | * callbacks |
| 65 | */ |
| 66 | static int apply_constraint(struct dev_pm_qos_request *req, |
| 67 | enum pm_qos_req_action action, int value) |
| 68 | { |
| 69 | int ret, curr_value; |
| 70 | |
| 71 | ret = pm_qos_update_target(req->dev->power.constraints, |
| 72 | &req->node, action, value); |
| 73 | |
| 74 | if (ret) { |
| 75 | /* Call the global callbacks if needed */ |
| 76 | curr_value = pm_qos_read_value(req->dev->power.constraints); |
| 77 | blocking_notifier_call_chain(&dev_pm_notifiers, |
| 78 | (unsigned long)curr_value, |
| 79 | req); |
| 80 | } |
| 81 | |
| 82 | return ret; |
| 83 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * dev_pm_qos_constraints_allocate |
| 87 | * @dev: device to allocate data for |
| 88 | * |
| 89 | * Called at the first call to add_request, for constraint data allocation |
| 90 | * Must be called with the dev_pm_qos_mtx mutex held |
| 91 | */ |
| 92 | static int dev_pm_qos_constraints_allocate(struct device *dev) |
| 93 | { |
| 94 | struct pm_qos_constraints *c; |
| 95 | struct blocking_notifier_head *n; |
| 96 | |
| 97 | c = kzalloc(sizeof(*c), GFP_KERNEL); |
| 98 | if (!c) |
| 99 | return -ENOMEM; |
| 100 | |
| 101 | n = kzalloc(sizeof(*n), GFP_KERNEL); |
| 102 | if (!n) { |
| 103 | kfree(c); |
| 104 | return -ENOMEM; |
| 105 | } |
| 106 | BLOCKING_INIT_NOTIFIER_HEAD(n); |
| 107 | |
| 108 | dev->power.constraints = c; |
| 109 | plist_head_init(&dev->power.constraints->list); |
| 110 | dev->power.constraints->target_value = PM_QOS_DEV_LAT_DEFAULT_VALUE; |
| 111 | dev->power.constraints->default_value = PM_QOS_DEV_LAT_DEFAULT_VALUE; |
| 112 | dev->power.constraints->type = PM_QOS_MIN; |
| 113 | dev->power.constraints->notifiers = n; |
| 114 | dev->power.constraints_state = DEV_PM_QOS_ALLOCATED; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * dev_pm_qos_constraints_init |
| 121 | * @dev: target device |
| 122 | * |
| 123 | * Called from the device PM subsystem at device insertion |
| 124 | */ |
| 125 | void dev_pm_qos_constraints_init(struct device *dev) |
| 126 | { |
| 127 | mutex_lock(&dev_pm_qos_mtx); |
| 128 | dev->power.constraints_state = DEV_PM_QOS_DEVICE_PRESENT; |
| 129 | mutex_unlock(&dev_pm_qos_mtx); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * dev_pm_qos_constraints_destroy |
| 134 | * @dev: target device |
| 135 | * |
| 136 | * Called from the device PM subsystem at device removal |
| 137 | */ |
| 138 | void dev_pm_qos_constraints_destroy(struct device *dev) |
| 139 | { |
| 140 | struct dev_pm_qos_request *req, *tmp; |
| 141 | |
| 142 | mutex_lock(&dev_pm_qos_mtx); |
| 143 | |
| 144 | if (dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) { |
| 145 | /* Flush the constraints list for the device */ |
| 146 | plist_for_each_entry_safe(req, tmp, |
| 147 | &dev->power.constraints->list, |
| 148 | node) { |
| 149 | /* |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame^] | 150 | * Update constraints list and call the notification |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 151 | * callbacks if needed |
| 152 | */ |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame^] | 153 | apply_constraint(req, PM_QOS_REMOVE_REQ, |
| 154 | PM_QOS_DEFAULT_VALUE); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 155 | memset(req, 0, sizeof(*req)); |
| 156 | } |
| 157 | |
| 158 | kfree(dev->power.constraints->notifiers); |
| 159 | kfree(dev->power.constraints); |
| 160 | dev->power.constraints = NULL; |
| 161 | } |
| 162 | dev->power.constraints_state = DEV_PM_QOS_NO_DEVICE; |
| 163 | |
| 164 | mutex_unlock(&dev_pm_qos_mtx); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * dev_pm_qos_add_request - inserts new qos request into the list |
| 169 | * @dev: target device for the constraint |
| 170 | * @req: pointer to a preallocated handle |
| 171 | * @value: defines the qos request |
| 172 | * |
| 173 | * This function inserts a new entry in the device constraints list of |
| 174 | * requested qos performance characteristics. It recomputes the aggregate |
| 175 | * QoS expectations of parameters and initializes the dev_pm_qos_request |
| 176 | * handle. Caller needs to save this handle for later use in updates and |
| 177 | * removal. |
| 178 | * |
| 179 | * Returns 1 if the aggregated constraint value has changed, |
| 180 | * 0 if the aggregated constraint value has not changed, |
| 181 | * -EINVAL in case of wrong parameters, -ENODEV if the device has been |
| 182 | * removed from the system |
| 183 | */ |
| 184 | int dev_pm_qos_add_request(struct device *dev, struct dev_pm_qos_request *req, |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame^] | 185 | s32 value) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 186 | { |
| 187 | int ret = 0; |
| 188 | |
| 189 | if (!dev || !req) /*guard against callers passing in null */ |
| 190 | return -EINVAL; |
| 191 | |
| 192 | if (dev_pm_qos_request_active(req)) { |
| 193 | WARN(1, KERN_ERR "dev_pm_qos_add_request() called for already " |
| 194 | "added request\n"); |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | mutex_lock(&dev_pm_qos_mtx); |
| 199 | req->dev = dev; |
| 200 | |
| 201 | /* Return if the device has been removed */ |
| 202 | if (req->dev->power.constraints_state == DEV_PM_QOS_NO_DEVICE) { |
| 203 | ret = -ENODEV; |
| 204 | goto out; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Allocate the constraints data on the first call to add_request, |
| 209 | * i.e. only if the data is not already allocated and if the device has |
| 210 | * not been removed |
| 211 | */ |
| 212 | if (dev->power.constraints_state == DEV_PM_QOS_DEVICE_PRESENT) |
| 213 | ret = dev_pm_qos_constraints_allocate(dev); |
| 214 | |
| 215 | if (!ret) |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame^] | 216 | ret = apply_constraint(req, PM_QOS_ADD_REQ, value); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 217 | |
| 218 | out: |
| 219 | mutex_unlock(&dev_pm_qos_mtx); |
| 220 | return ret; |
| 221 | } |
| 222 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_request); |
| 223 | |
| 224 | /** |
| 225 | * dev_pm_qos_update_request - modifies an existing qos request |
| 226 | * @req : handle to list element holding a dev_pm_qos request to use |
| 227 | * @new_value: defines the qos request |
| 228 | * |
| 229 | * Updates an existing dev PM qos request along with updating the |
| 230 | * target value. |
| 231 | * |
| 232 | * Attempts are made to make this code callable on hot code paths. |
| 233 | * |
| 234 | * Returns 1 if the aggregated constraint value has changed, |
| 235 | * 0 if the aggregated constraint value has not changed, |
| 236 | * -EINVAL in case of wrong parameters, -ENODEV if the device has been |
| 237 | * removed from the system |
| 238 | */ |
| 239 | int dev_pm_qos_update_request(struct dev_pm_qos_request *req, |
| 240 | s32 new_value) |
| 241 | { |
| 242 | int ret = 0; |
| 243 | |
| 244 | if (!req) /*guard against callers passing in null */ |
| 245 | return -EINVAL; |
| 246 | |
| 247 | if (!dev_pm_qos_request_active(req)) { |
| 248 | WARN(1, KERN_ERR "dev_pm_qos_update_request() called for " |
| 249 | "unknown object\n"); |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | |
| 253 | mutex_lock(&dev_pm_qos_mtx); |
| 254 | |
| 255 | if (req->dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) { |
| 256 | if (new_value != req->node.prio) |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame^] | 257 | ret = apply_constraint(req, PM_QOS_UPDATE_REQ, |
| 258 | new_value); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 259 | } else { |
| 260 | /* Return if the device has been removed */ |
| 261 | ret = -ENODEV; |
| 262 | } |
| 263 | |
| 264 | mutex_unlock(&dev_pm_qos_mtx); |
| 265 | return ret; |
| 266 | } |
| 267 | EXPORT_SYMBOL_GPL(dev_pm_qos_update_request); |
| 268 | |
| 269 | /** |
| 270 | * dev_pm_qos_remove_request - modifies an existing qos request |
| 271 | * @req: handle to request list element |
| 272 | * |
| 273 | * Will remove pm qos request from the list of constraints and |
| 274 | * recompute the current target value. Call this on slow code paths. |
| 275 | * |
| 276 | * Returns 1 if the aggregated constraint value has changed, |
| 277 | * 0 if the aggregated constraint value has not changed, |
| 278 | * -EINVAL in case of wrong parameters, -ENODEV if the device has been |
| 279 | * removed from the system |
| 280 | */ |
| 281 | int dev_pm_qos_remove_request(struct dev_pm_qos_request *req) |
| 282 | { |
| 283 | int ret = 0; |
| 284 | |
| 285 | if (!req) /*guard against callers passing in null */ |
| 286 | return -EINVAL; |
| 287 | |
| 288 | if (!dev_pm_qos_request_active(req)) { |
| 289 | WARN(1, KERN_ERR "dev_pm_qos_remove_request() called for " |
| 290 | "unknown object\n"); |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
| 294 | mutex_lock(&dev_pm_qos_mtx); |
| 295 | |
| 296 | if (req->dev->power.constraints_state == DEV_PM_QOS_ALLOCATED) { |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame^] | 297 | ret = apply_constraint(req, PM_QOS_REMOVE_REQ, |
| 298 | PM_QOS_DEFAULT_VALUE); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 299 | memset(req, 0, sizeof(*req)); |
| 300 | } else { |
| 301 | /* Return if the device has been removed */ |
| 302 | ret = -ENODEV; |
| 303 | } |
| 304 | |
| 305 | mutex_unlock(&dev_pm_qos_mtx); |
| 306 | return ret; |
| 307 | } |
| 308 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request); |
| 309 | |
| 310 | /** |
| 311 | * dev_pm_qos_add_notifier - sets notification entry for changes to target value |
| 312 | * of per-device PM QoS constraints |
| 313 | * |
| 314 | * @dev: target device for the constraint |
| 315 | * @notifier: notifier block managed by caller. |
| 316 | * |
| 317 | * Will register the notifier into a notification chain that gets called |
| 318 | * upon changes to the target value for the device. |
| 319 | */ |
| 320 | int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier) |
| 321 | { |
| 322 | int retval = 0; |
| 323 | |
| 324 | mutex_lock(&dev_pm_qos_mtx); |
| 325 | |
| 326 | /* Silently return if the device has been removed */ |
| 327 | if (dev->power.constraints_state != DEV_PM_QOS_ALLOCATED) |
| 328 | goto out; |
| 329 | |
| 330 | retval = blocking_notifier_chain_register( |
| 331 | dev->power.constraints->notifiers, |
| 332 | notifier); |
| 333 | |
| 334 | out: |
| 335 | mutex_unlock(&dev_pm_qos_mtx); |
| 336 | return retval; |
| 337 | } |
| 338 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier); |
| 339 | |
| 340 | /** |
| 341 | * dev_pm_qos_remove_notifier - deletes notification for changes to target value |
| 342 | * of per-device PM QoS constraints |
| 343 | * |
| 344 | * @dev: target device for the constraint |
| 345 | * @notifier: notifier block to be removed. |
| 346 | * |
| 347 | * Will remove the notifier from the notification chain that gets called |
| 348 | * upon changes to the target value. |
| 349 | */ |
| 350 | int dev_pm_qos_remove_notifier(struct device *dev, |
| 351 | struct notifier_block *notifier) |
| 352 | { |
| 353 | int retval = 0; |
| 354 | |
| 355 | mutex_lock(&dev_pm_qos_mtx); |
| 356 | |
| 357 | /* Silently return if the device has been removed */ |
| 358 | if (dev->power.constraints_state != DEV_PM_QOS_ALLOCATED) |
| 359 | goto out; |
| 360 | |
| 361 | retval = blocking_notifier_chain_unregister( |
| 362 | dev->power.constraints->notifiers, |
| 363 | notifier); |
| 364 | |
| 365 | out: |
| 366 | mutex_unlock(&dev_pm_qos_mtx); |
| 367 | return retval; |
| 368 | } |
| 369 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier); |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame^] | 370 | |
| 371 | /** |
| 372 | * dev_pm_qos_add_global_notifier - sets notification entry for changes to |
| 373 | * target value of the PM QoS constraints for any device |
| 374 | * |
| 375 | * @notifier: notifier block managed by caller. |
| 376 | * |
| 377 | * Will register the notifier into a notification chain that gets called |
| 378 | * upon changes to the target value for any device. |
| 379 | */ |
| 380 | int dev_pm_qos_add_global_notifier(struct notifier_block *notifier) |
| 381 | { |
| 382 | return blocking_notifier_chain_register(&dev_pm_notifiers, notifier); |
| 383 | } |
| 384 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier); |
| 385 | |
| 386 | /** |
| 387 | * dev_pm_qos_remove_global_notifier - deletes notification for changes to |
| 388 | * target value of PM QoS constraints for any device |
| 389 | * |
| 390 | * @notifier: notifier block to be removed. |
| 391 | * |
| 392 | * Will remove the notifier from the notification chain that gets called |
| 393 | * upon changes to the target value for any device. |
| 394 | */ |
| 395 | int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier) |
| 396 | { |
| 397 | return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier); |
| 398 | } |
| 399 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier); |