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 | } |
| 94 | |
| 95 | /** |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 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. |
| 98 | * |
| 99 | * This routine must be called with dev->power.lock held. |
| 100 | */ |
| 101 | s32 __dev_pm_qos_read_value(struct device *dev) |
| 102 | { |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 103 | 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] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * 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] | 108 | * @dev: Device to get the PM QoS constraint value for. |
| 109 | */ |
| 110 | s32 dev_pm_qos_read_value(struct device *dev) |
| 111 | { |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 112 | unsigned long flags; |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 113 | s32 ret; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 114 | |
| 115 | spin_lock_irqsave(&dev->power.lock, flags); |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 116 | ret = __dev_pm_qos_read_value(dev); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 117 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 122 | /** |
| 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. |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 127 | * |
| 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 |
| 130 | * callbacks |
| 131 | */ |
| 132 | static int apply_constraint(struct dev_pm_qos_request *req, |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 133 | enum pm_qos_req_action action, s32 value) |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 134 | { |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 135 | struct dev_pm_qos *qos = req->dev->power.qos; |
| 136 | int ret; |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 137 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 138 | switch(req->type) { |
| 139 | case DEV_PM_QOS_LATENCY: |
| 140 | ret = pm_qos_update_target(&qos->latency, &req->data.pnode, |
| 141 | action, value); |
| 142 | if (ret) { |
| 143 | value = pm_qos_read_value(&qos->latency); |
| 144 | blocking_notifier_call_chain(&dev_pm_notifiers, |
| 145 | (unsigned long)value, |
| 146 | req); |
| 147 | } |
| 148 | break; |
| 149 | case DEV_PM_QOS_FLAGS: |
| 150 | ret = pm_qos_update_flags(&qos->flags, &req->data.flr, |
| 151 | action, value); |
| 152 | break; |
| 153 | default: |
| 154 | ret = -EINVAL; |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | return ret; |
| 158 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * dev_pm_qos_constraints_allocate |
| 162 | * @dev: device to allocate data for |
| 163 | * |
| 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 |
| 166 | */ |
| 167 | static int dev_pm_qos_constraints_allocate(struct device *dev) |
| 168 | { |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 169 | struct dev_pm_qos *qos; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 170 | struct pm_qos_constraints *c; |
| 171 | struct blocking_notifier_head *n; |
| 172 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 173 | qos = kzalloc(sizeof(*qos), GFP_KERNEL); |
| 174 | if (!qos) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 175 | return -ENOMEM; |
| 176 | |
| 177 | n = kzalloc(sizeof(*n), GFP_KERNEL); |
| 178 | if (!n) { |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 179 | kfree(qos); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 180 | return -ENOMEM; |
| 181 | } |
| 182 | BLOCKING_INIT_NOTIFIER_HEAD(n); |
| 183 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 184 | c = &qos->latency; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 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; |
| 189 | c->notifiers = n; |
| 190 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 191 | INIT_LIST_HEAD(&qos->flags.list); |
| 192 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 193 | spin_lock_irq(&dev->power.lock); |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 194 | dev->power.qos = qos; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 195 | spin_unlock_irq(&dev->power.lock); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /** |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 201 | * dev_pm_qos_constraints_init - Initalize device's PM QoS constraints pointer. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 202 | * @dev: target device |
| 203 | * |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 204 | * Called from the device PM subsystem during device insertion under |
| 205 | * device_pm_lock(). |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 206 | */ |
| 207 | void dev_pm_qos_constraints_init(struct device *dev) |
| 208 | { |
| 209 | mutex_lock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 210 | dev->power.qos = NULL; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 211 | dev->power.power_state = PMSG_ON; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 212 | mutex_unlock(&dev_pm_qos_mtx); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * dev_pm_qos_constraints_destroy |
| 217 | * @dev: target device |
| 218 | * |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 219 | * 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] | 220 | */ |
| 221 | void dev_pm_qos_constraints_destroy(struct device *dev) |
| 222 | { |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 223 | struct dev_pm_qos *qos; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 224 | struct dev_pm_qos_request *req, *tmp; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 225 | struct pm_qos_constraints *c; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 226 | |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 227 | /* |
| 228 | * If the device's PM QoS resume latency limit has been exposed to user |
| 229 | * space, it has to be hidden at this point. |
| 230 | */ |
| 231 | dev_pm_qos_hide_latency_limit(dev); |
| 232 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 233 | mutex_lock(&dev_pm_qos_mtx); |
| 234 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 235 | dev->power.power_state = PMSG_INVALID; |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 236 | qos = dev->power.qos; |
| 237 | if (!qos) |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 238 | goto out; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 239 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 240 | c = &qos->latency; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 241 | /* Flush the constraints list for the device */ |
Rafael J. Wysocki | 021c870 | 2012-10-23 01:09:00 +0200 | [diff] [blame] | 242 | plist_for_each_entry_safe(req, tmp, &c->list, data.pnode) { |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 243 | /* |
| 244 | * Update constraints list and call the notification |
| 245 | * callbacks if needed |
| 246 | */ |
| 247 | apply_constraint(req, PM_QOS_REMOVE_REQ, PM_QOS_DEFAULT_VALUE); |
| 248 | memset(req, 0, sizeof(*req)); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 249 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 250 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 251 | spin_lock_irq(&dev->power.lock); |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 252 | dev->power.qos = NULL; |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 253 | spin_unlock_irq(&dev->power.lock); |
| 254 | |
| 255 | kfree(c->notifiers); |
Lan,Tianyu | 9eaee2c | 2012-11-01 22:45:30 +0100 | [diff] [blame] | 256 | kfree(qos); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 257 | |
| 258 | out: |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 259 | mutex_unlock(&dev_pm_qos_mtx); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * dev_pm_qos_add_request - inserts new qos request into the list |
| 264 | * @dev: target device for the constraint |
| 265 | * @req: pointer to a preallocated handle |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 266 | * @type: type of the request |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 267 | * @value: defines the qos request |
| 268 | * |
| 269 | * This function inserts a new entry in the device constraints list of |
| 270 | * requested qos performance characteristics. It recomputes the aggregate |
| 271 | * QoS expectations of parameters and initializes the dev_pm_qos_request |
| 272 | * handle. Caller needs to save this handle for later use in updates and |
| 273 | * removal. |
| 274 | * |
| 275 | * Returns 1 if the aggregated constraint value has changed, |
| 276 | * 0 if the aggregated constraint value has not changed, |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 277 | * -EINVAL in case of wrong parameters, -ENOMEM if there's not enough memory |
| 278 | * to allocate for data structures, -ENODEV if the device has just been removed |
| 279 | * from the system. |
Rafael J. Wysocki | 436ede8 | 2012-11-02 13:10:09 +0100 | [diff] [blame^] | 280 | * |
| 281 | * Callers should ensure that the target device is not RPM_SUSPENDED before |
| 282 | * using this function for requests of type DEV_PM_QOS_FLAGS. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 283 | */ |
| 284 | 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] | 285 | enum dev_pm_qos_req_type type, s32 value) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 286 | { |
| 287 | int ret = 0; |
| 288 | |
| 289 | if (!dev || !req) /*guard against callers passing in null */ |
| 290 | return -EINVAL; |
| 291 | |
Guennadi Liakhovetski | af4c720 | 2011-11-10 00:44:18 +0100 | [diff] [blame] | 292 | if (WARN(dev_pm_qos_request_active(req), |
| 293 | "%s() called for already added request\n", __func__)) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 294 | return -EINVAL; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 295 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 296 | req->dev = dev; |
| 297 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 298 | mutex_lock(&dev_pm_qos_mtx); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 299 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 300 | if (!dev->power.qos) { |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 301 | if (dev->power.power_state.event == PM_EVENT_INVALID) { |
| 302 | /* The device has been removed from the system. */ |
| 303 | req->dev = NULL; |
| 304 | ret = -ENODEV; |
| 305 | goto out; |
| 306 | } else { |
| 307 | /* |
| 308 | * Allocate the constraints data on the first call to |
| 309 | * add_request, i.e. only if the data is not already |
| 310 | * allocated and if the device has not been removed. |
| 311 | */ |
| 312 | ret = dev_pm_qos_constraints_allocate(dev); |
| 313 | } |
| 314 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 315 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 316 | if (!ret) { |
| 317 | req->type = type; |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 318 | ret = apply_constraint(req, PM_QOS_ADD_REQ, value); |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 319 | } |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 320 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 321 | out: |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 322 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 323 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 324 | return ret; |
| 325 | } |
| 326 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_request); |
| 327 | |
| 328 | /** |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 329 | * __dev_pm_qos_update_request - Modify an existing device PM QoS request. |
| 330 | * @req : PM QoS request to modify. |
| 331 | * @new_value: New value to request. |
| 332 | */ |
| 333 | static int __dev_pm_qos_update_request(struct dev_pm_qos_request *req, |
| 334 | s32 new_value) |
| 335 | { |
| 336 | s32 curr_value; |
| 337 | int ret = 0; |
| 338 | |
| 339 | if (!req->dev->power.qos) |
| 340 | return -ENODEV; |
| 341 | |
| 342 | switch(req->type) { |
| 343 | case DEV_PM_QOS_LATENCY: |
| 344 | curr_value = req->data.pnode.prio; |
| 345 | break; |
| 346 | case DEV_PM_QOS_FLAGS: |
| 347 | curr_value = req->data.flr.flags; |
| 348 | break; |
| 349 | default: |
| 350 | return -EINVAL; |
| 351 | } |
| 352 | |
| 353 | if (curr_value != new_value) |
| 354 | ret = apply_constraint(req, PM_QOS_UPDATE_REQ, new_value); |
| 355 | |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | /** |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 360 | * dev_pm_qos_update_request - modifies an existing qos request |
| 361 | * @req : handle to list element holding a dev_pm_qos request to use |
| 362 | * @new_value: defines the qos request |
| 363 | * |
| 364 | * Updates an existing dev PM qos request along with updating the |
| 365 | * target value. |
| 366 | * |
| 367 | * Attempts are made to make this code callable on hot code paths. |
| 368 | * |
| 369 | * Returns 1 if the aggregated constraint value has changed, |
| 370 | * 0 if the aggregated constraint value has not changed, |
| 371 | * -EINVAL in case of wrong parameters, -ENODEV if the device has been |
| 372 | * removed from the system |
Rafael J. Wysocki | 436ede8 | 2012-11-02 13:10:09 +0100 | [diff] [blame^] | 373 | * |
| 374 | * Callers should ensure that the target device is not RPM_SUSPENDED before |
| 375 | * using this function for requests of type DEV_PM_QOS_FLAGS. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 376 | */ |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 377 | 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] | 378 | { |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 379 | int ret; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 380 | |
| 381 | if (!req) /*guard against callers passing in null */ |
| 382 | return -EINVAL; |
| 383 | |
Guennadi Liakhovetski | af4c720 | 2011-11-10 00:44:18 +0100 | [diff] [blame] | 384 | if (WARN(!dev_pm_qos_request_active(req), |
| 385 | "%s() called for unknown object\n", __func__)) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 386 | return -EINVAL; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 387 | |
| 388 | mutex_lock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | f965287 | 2012-10-30 20:00:30 +0100 | [diff] [blame] | 389 | ret = __dev_pm_qos_update_request(req, new_value); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 390 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 391 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 392 | return ret; |
| 393 | } |
| 394 | EXPORT_SYMBOL_GPL(dev_pm_qos_update_request); |
| 395 | |
| 396 | /** |
| 397 | * dev_pm_qos_remove_request - modifies an existing qos request |
| 398 | * @req: handle to request list element |
| 399 | * |
| 400 | * Will remove pm qos request from the list of constraints and |
| 401 | * recompute the current target value. Call this on slow code paths. |
| 402 | * |
| 403 | * Returns 1 if the aggregated constraint value has changed, |
| 404 | * 0 if the aggregated constraint value has not changed, |
| 405 | * -EINVAL in case of wrong parameters, -ENODEV if the device has been |
| 406 | * removed from the system |
Rafael J. Wysocki | 436ede8 | 2012-11-02 13:10:09 +0100 | [diff] [blame^] | 407 | * |
| 408 | * Callers should ensure that the target device is not RPM_SUSPENDED before |
| 409 | * using this function for requests of type DEV_PM_QOS_FLAGS. |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 410 | */ |
| 411 | int dev_pm_qos_remove_request(struct dev_pm_qos_request *req) |
| 412 | { |
| 413 | int ret = 0; |
| 414 | |
| 415 | if (!req) /*guard against callers passing in null */ |
| 416 | return -EINVAL; |
| 417 | |
Guennadi Liakhovetski | af4c720 | 2011-11-10 00:44:18 +0100 | [diff] [blame] | 418 | if (WARN(!dev_pm_qos_request_active(req), |
| 419 | "%s() called for unknown object\n", __func__)) |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 420 | return -EINVAL; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 421 | |
| 422 | mutex_lock(&dev_pm_qos_mtx); |
| 423 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 424 | if (req->dev->power.qos) { |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 425 | ret = apply_constraint(req, PM_QOS_REMOVE_REQ, |
| 426 | PM_QOS_DEFAULT_VALUE); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 427 | memset(req, 0, sizeof(*req)); |
| 428 | } else { |
| 429 | /* Return if the device has been removed */ |
| 430 | ret = -ENODEV; |
| 431 | } |
| 432 | |
| 433 | mutex_unlock(&dev_pm_qos_mtx); |
| 434 | return ret; |
| 435 | } |
| 436 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_request); |
| 437 | |
| 438 | /** |
| 439 | * dev_pm_qos_add_notifier - sets notification entry for changes to target value |
| 440 | * of per-device PM QoS constraints |
| 441 | * |
| 442 | * @dev: target device for the constraint |
| 443 | * @notifier: notifier block managed by caller. |
| 444 | * |
| 445 | * Will register the notifier into a notification chain that gets called |
| 446 | * upon changes to the target value for the device. |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 447 | * |
| 448 | * If the device's constraints object doesn't exist when this routine is called, |
| 449 | * 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] | 450 | */ |
| 451 | int dev_pm_qos_add_notifier(struct device *dev, struct notifier_block *notifier) |
| 452 | { |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 453 | int ret = 0; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 454 | |
| 455 | mutex_lock(&dev_pm_qos_mtx); |
| 456 | |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 457 | if (!dev->power.qos) |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 458 | ret = dev->power.power_state.event != PM_EVENT_INVALID ? |
| 459 | dev_pm_qos_constraints_allocate(dev) : -ENODEV; |
| 460 | |
| 461 | if (!ret) |
| 462 | ret = blocking_notifier_chain_register( |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 463 | dev->power.qos->latency.notifiers, notifier); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 464 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 465 | mutex_unlock(&dev_pm_qos_mtx); |
Rafael J. Wysocki | 23e0fc5 | 2012-04-29 22:54:47 +0200 | [diff] [blame] | 466 | return ret; |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 467 | } |
| 468 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_notifier); |
| 469 | |
| 470 | /** |
| 471 | * dev_pm_qos_remove_notifier - deletes notification for changes to target value |
| 472 | * of per-device PM QoS constraints |
| 473 | * |
| 474 | * @dev: target device for the constraint |
| 475 | * @notifier: notifier block to be removed. |
| 476 | * |
| 477 | * Will remove the notifier from the notification chain that gets called |
| 478 | * upon changes to the target value. |
| 479 | */ |
| 480 | int dev_pm_qos_remove_notifier(struct device *dev, |
| 481 | struct notifier_block *notifier) |
| 482 | { |
| 483 | int retval = 0; |
| 484 | |
| 485 | mutex_lock(&dev_pm_qos_mtx); |
| 486 | |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 487 | /* Silently return if the constraints object is not present. */ |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 488 | if (dev->power.qos) |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 489 | retval = blocking_notifier_chain_unregister( |
Rafael J. Wysocki | 5f986c5 | 2012-10-23 01:07:27 +0200 | [diff] [blame] | 490 | dev->power.qos->latency.notifiers, |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 491 | notifier); |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 492 | |
Jean Pihet | 91ff4cb | 2011-08-25 15:35:41 +0200 | [diff] [blame] | 493 | mutex_unlock(&dev_pm_qos_mtx); |
| 494 | return retval; |
| 495 | } |
| 496 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_notifier); |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 497 | |
| 498 | /** |
| 499 | * dev_pm_qos_add_global_notifier - sets notification entry for changes to |
| 500 | * target value of the PM QoS constraints for any device |
| 501 | * |
| 502 | * @notifier: notifier block managed by caller. |
| 503 | * |
| 504 | * Will register the notifier into a notification chain that gets called |
| 505 | * upon changes to the target value for any device. |
| 506 | */ |
| 507 | int dev_pm_qos_add_global_notifier(struct notifier_block *notifier) |
| 508 | { |
| 509 | return blocking_notifier_chain_register(&dev_pm_notifiers, notifier); |
| 510 | } |
| 511 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_global_notifier); |
| 512 | |
| 513 | /** |
| 514 | * dev_pm_qos_remove_global_notifier - deletes notification for changes to |
| 515 | * target value of PM QoS constraints for any device |
| 516 | * |
| 517 | * @notifier: notifier block to be removed. |
| 518 | * |
| 519 | * Will remove the notifier from the notification chain that gets called |
| 520 | * upon changes to the target value for any device. |
| 521 | */ |
| 522 | int dev_pm_qos_remove_global_notifier(struct notifier_block *notifier) |
| 523 | { |
| 524 | return blocking_notifier_chain_unregister(&dev_pm_notifiers, notifier); |
| 525 | } |
| 526 | EXPORT_SYMBOL_GPL(dev_pm_qos_remove_global_notifier); |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 527 | |
| 528 | /** |
| 529 | * dev_pm_qos_add_ancestor_request - Add PM QoS request for device's ancestor. |
| 530 | * @dev: Device whose ancestor to add the request for. |
| 531 | * @req: Pointer to the preallocated handle. |
| 532 | * @value: Constraint latency value. |
| 533 | */ |
| 534 | int dev_pm_qos_add_ancestor_request(struct device *dev, |
| 535 | struct dev_pm_qos_request *req, s32 value) |
| 536 | { |
| 537 | struct device *ancestor = dev->parent; |
| 538 | int error = -ENODEV; |
| 539 | |
| 540 | while (ancestor && !ancestor->power.ignore_children) |
| 541 | ancestor = ancestor->parent; |
| 542 | |
| 543 | if (ancestor) |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 544 | error = dev_pm_qos_add_request(ancestor, req, |
| 545 | DEV_PM_QOS_LATENCY, value); |
Rafael J. Wysocki | 40a5f8b | 2011-12-23 01:23:52 +0100 | [diff] [blame] | 546 | |
| 547 | if (error) |
| 548 | req->dev = NULL; |
| 549 | |
| 550 | return error; |
| 551 | } |
| 552 | EXPORT_SYMBOL_GPL(dev_pm_qos_add_ancestor_request); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 553 | |
| 554 | #ifdef CONFIG_PM_RUNTIME |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 555 | static void __dev_pm_qos_drop_user_request(struct device *dev, |
| 556 | enum dev_pm_qos_req_type type) |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 557 | { |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 558 | switch(type) { |
| 559 | case DEV_PM_QOS_LATENCY: |
| 560 | dev_pm_qos_remove_request(dev->power.qos->latency_req); |
| 561 | dev->power.qos->latency_req = NULL; |
| 562 | break; |
| 563 | case DEV_PM_QOS_FLAGS: |
| 564 | dev_pm_qos_remove_request(dev->power.qos->flags_req); |
| 565 | dev->power.qos->flags_req = NULL; |
| 566 | break; |
| 567 | } |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /** |
| 571 | * dev_pm_qos_expose_latency_limit - Expose PM QoS latency limit to user space. |
| 572 | * @dev: Device whose PM QoS latency limit is to be exposed to user space. |
| 573 | * @value: Initial value of the latency limit. |
| 574 | */ |
| 575 | int dev_pm_qos_expose_latency_limit(struct device *dev, s32 value) |
| 576 | { |
| 577 | struct dev_pm_qos_request *req; |
| 578 | int ret; |
| 579 | |
| 580 | if (!device_is_registered(dev) || value < 0) |
| 581 | return -EINVAL; |
| 582 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 583 | if (dev->power.qos && dev->power.qos->latency_req) |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 584 | return -EEXIST; |
| 585 | |
| 586 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 587 | if (!req) |
| 588 | return -ENOMEM; |
| 589 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 590 | ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_LATENCY, value); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 591 | if (ret < 0) |
| 592 | return ret; |
| 593 | |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 594 | dev->power.qos->latency_req = req; |
| 595 | ret = pm_qos_sysfs_add_latency(dev); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 596 | if (ret) |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 597 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 598 | |
| 599 | return ret; |
| 600 | } |
| 601 | EXPORT_SYMBOL_GPL(dev_pm_qos_expose_latency_limit); |
| 602 | |
| 603 | /** |
| 604 | * dev_pm_qos_hide_latency_limit - Hide PM QoS latency limit from user space. |
| 605 | * @dev: Device whose PM QoS latency limit is to be hidden from user space. |
| 606 | */ |
| 607 | void dev_pm_qos_hide_latency_limit(struct device *dev) |
| 608 | { |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 609 | if (dev->power.qos && dev->power.qos->latency_req) { |
| 610 | pm_qos_sysfs_remove_latency(dev); |
| 611 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_LATENCY); |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | EXPORT_SYMBOL_GPL(dev_pm_qos_hide_latency_limit); |
Rafael J. Wysocki | e39473d | 2012-10-24 02:08:18 +0200 | [diff] [blame] | 615 | |
| 616 | /** |
| 617 | * dev_pm_qos_expose_flags - Expose PM QoS flags of a device to user space. |
| 618 | * @dev: Device whose PM QoS flags are to be exposed to user space. |
| 619 | * @val: Initial values of the flags. |
| 620 | */ |
| 621 | int dev_pm_qos_expose_flags(struct device *dev, s32 val) |
| 622 | { |
| 623 | struct dev_pm_qos_request *req; |
| 624 | int ret; |
| 625 | |
| 626 | if (!device_is_registered(dev)) |
| 627 | return -EINVAL; |
| 628 | |
| 629 | if (dev->power.qos && dev->power.qos->flags_req) |
| 630 | return -EEXIST; |
| 631 | |
| 632 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 633 | if (!req) |
| 634 | return -ENOMEM; |
| 635 | |
| 636 | ret = dev_pm_qos_add_request(dev, req, DEV_PM_QOS_FLAGS, val); |
| 637 | if (ret < 0) |
| 638 | return ret; |
| 639 | |
| 640 | dev->power.qos->flags_req = req; |
| 641 | ret = pm_qos_sysfs_add_flags(dev); |
| 642 | if (ret) |
| 643 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS); |
| 644 | |
| 645 | return ret; |
| 646 | } |
| 647 | EXPORT_SYMBOL_GPL(dev_pm_qos_expose_flags); |
| 648 | |
| 649 | /** |
| 650 | * dev_pm_qos_hide_flags - Hide PM QoS flags of a device from user space. |
| 651 | * @dev: Device whose PM QoS flags are to be hidden from user space. |
| 652 | */ |
| 653 | void dev_pm_qos_hide_flags(struct device *dev) |
| 654 | { |
| 655 | if (dev->power.qos && dev->power.qos->flags_req) { |
| 656 | pm_qos_sysfs_remove_flags(dev); |
| 657 | __dev_pm_qos_drop_user_request(dev, DEV_PM_QOS_FLAGS); |
| 658 | } |
| 659 | } |
| 660 | EXPORT_SYMBOL_GPL(dev_pm_qos_hide_flags); |
| 661 | |
| 662 | /** |
| 663 | * dev_pm_qos_update_flags - Update PM QoS flags request owned by user space. |
| 664 | * @dev: Device to update the PM QoS flags request for. |
| 665 | * @mask: Flags to set/clear. |
| 666 | * @set: Whether to set or clear the flags (true means set). |
| 667 | */ |
| 668 | int dev_pm_qos_update_flags(struct device *dev, s32 mask, bool set) |
| 669 | { |
| 670 | s32 value; |
| 671 | int ret; |
| 672 | |
| 673 | if (!dev->power.qos || !dev->power.qos->flags_req) |
| 674 | return -EINVAL; |
| 675 | |
| 676 | pm_runtime_get_sync(dev); |
| 677 | mutex_lock(&dev_pm_qos_mtx); |
| 678 | |
| 679 | value = dev_pm_qos_requested_flags(dev); |
| 680 | if (set) |
| 681 | value |= mask; |
| 682 | else |
| 683 | value &= ~mask; |
| 684 | |
| 685 | ret = __dev_pm_qos_update_request(dev->power.qos->flags_req, value); |
| 686 | |
| 687 | mutex_unlock(&dev_pm_qos_mtx); |
| 688 | pm_runtime_put(dev); |
| 689 | |
| 690 | return ret; |
| 691 | } |
Rafael J. Wysocki | 85dc0b8 | 2012-03-13 01:01:39 +0100 | [diff] [blame] | 692 | #endif /* CONFIG_PM_RUNTIME */ |