Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1 | /* |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 2 | * drivers/base/power/runtime.c - Helper functions for device runtime PM |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 5 | * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu> |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 6 | * |
| 7 | * This file is released under the GPLv2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/pm_runtime.h> |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 12 | #include "power.h" |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 13 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 14 | static int rpm_resume(struct device *dev, int rpmflags); |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 15 | static int rpm_suspend(struct device *dev, int rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 16 | |
| 17 | /** |
Alan Stern | 4769373 | 2010-09-25 23:34:46 +0200 | [diff] [blame] | 18 | * update_pm_runtime_accounting - Update the time accounting of power states |
| 19 | * @dev: Device to update the accounting for |
| 20 | * |
| 21 | * In order to be able to have time accounting of the various power states |
| 22 | * (as used by programs such as PowerTOP to show the effectiveness of runtime |
| 23 | * PM), we need to track the time spent in each state. |
| 24 | * update_pm_runtime_accounting must be called each time before the |
| 25 | * runtime_status field is updated, to account the time in the old state |
| 26 | * correctly. |
| 27 | */ |
| 28 | void update_pm_runtime_accounting(struct device *dev) |
| 29 | { |
| 30 | unsigned long now = jiffies; |
| 31 | int delta; |
| 32 | |
| 33 | delta = now - dev->power.accounting_timestamp; |
| 34 | |
| 35 | if (delta < 0) |
| 36 | delta = 0; |
| 37 | |
| 38 | dev->power.accounting_timestamp = now; |
| 39 | |
| 40 | if (dev->power.disable_depth > 0) |
| 41 | return; |
| 42 | |
| 43 | if (dev->power.runtime_status == RPM_SUSPENDED) |
| 44 | dev->power.suspended_jiffies += delta; |
| 45 | else |
| 46 | dev->power.active_jiffies += delta; |
| 47 | } |
| 48 | |
| 49 | static void __update_runtime_status(struct device *dev, enum rpm_status status) |
| 50 | { |
| 51 | update_pm_runtime_accounting(dev); |
| 52 | dev->power.runtime_status = status; |
| 53 | } |
| 54 | |
| 55 | /** |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 56 | * pm_runtime_deactivate_timer - Deactivate given device's suspend timer. |
| 57 | * @dev: Device to handle. |
| 58 | */ |
| 59 | static void pm_runtime_deactivate_timer(struct device *dev) |
| 60 | { |
| 61 | if (dev->power.timer_expires > 0) { |
| 62 | del_timer(&dev->power.suspend_timer); |
| 63 | dev->power.timer_expires = 0; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests. |
| 69 | * @dev: Device to handle. |
| 70 | */ |
| 71 | static void pm_runtime_cancel_pending(struct device *dev) |
| 72 | { |
| 73 | pm_runtime_deactivate_timer(dev); |
| 74 | /* |
| 75 | * In case there's a request pending, make sure its work function will |
| 76 | * return without doing anything. |
| 77 | */ |
| 78 | dev->power.request = RPM_REQ_NONE; |
| 79 | } |
| 80 | |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 81 | /* |
| 82 | * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time. |
| 83 | * @dev: Device to handle. |
| 84 | * |
| 85 | * Compute the autosuspend-delay expiration time based on the device's |
| 86 | * power.last_busy time. If the delay has already expired or is disabled |
| 87 | * (negative) or the power.use_autosuspend flag isn't set, return 0. |
| 88 | * Otherwise return the expiration time in jiffies (adjusted to be nonzero). |
| 89 | * |
| 90 | * This function may be called either with or without dev->power.lock held. |
| 91 | * Either way it can be racy, since power.last_busy may be updated at any time. |
| 92 | */ |
| 93 | unsigned long pm_runtime_autosuspend_expiration(struct device *dev) |
| 94 | { |
| 95 | int autosuspend_delay; |
| 96 | long elapsed; |
| 97 | unsigned long last_busy; |
| 98 | unsigned long expires = 0; |
| 99 | |
| 100 | if (!dev->power.use_autosuspend) |
| 101 | goto out; |
| 102 | |
| 103 | autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay); |
| 104 | if (autosuspend_delay < 0) |
| 105 | goto out; |
| 106 | |
| 107 | last_busy = ACCESS_ONCE(dev->power.last_busy); |
| 108 | elapsed = jiffies - last_busy; |
| 109 | if (elapsed < 0) |
| 110 | goto out; /* jiffies has wrapped around. */ |
| 111 | |
| 112 | /* |
| 113 | * If the autosuspend_delay is >= 1 second, align the timer by rounding |
| 114 | * up to the nearest second. |
| 115 | */ |
| 116 | expires = last_busy + msecs_to_jiffies(autosuspend_delay); |
| 117 | if (autosuspend_delay >= 1000) |
| 118 | expires = round_jiffies(expires); |
| 119 | expires += !expires; |
| 120 | if (elapsed >= expires - last_busy) |
| 121 | expires = 0; /* Already expired. */ |
| 122 | |
| 123 | out: |
| 124 | return expires; |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration); |
| 127 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 128 | /** |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 129 | * rpm_check_suspend_allowed - Test whether a device may be suspended. |
| 130 | * @dev: Device to test. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 131 | */ |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 132 | static int rpm_check_suspend_allowed(struct device *dev) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 133 | { |
| 134 | int retval = 0; |
| 135 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 136 | if (dev->power.runtime_error) |
| 137 | retval = -EINVAL; |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 138 | else if (dev->power.disable_depth > 0) |
| 139 | retval = -EACCES; |
| 140 | else if (atomic_read(&dev->power.usage_count) > 0) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 141 | retval = -EAGAIN; |
| 142 | else if (!pm_children_suspended(dev)) |
| 143 | retval = -EBUSY; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 144 | |
| 145 | /* Pending resume requests take precedence over suspends. */ |
| 146 | else if ((dev->power.deferred_resume |
Kevin Winchester | 78ca7c3 | 2010-10-29 15:29:55 +0200 | [diff] [blame] | 147 | && dev->power.runtime_status == RPM_SUSPENDING) |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 148 | || (dev->power.request_pending |
| 149 | && dev->power.request == RPM_REQ_RESUME)) |
| 150 | retval = -EAGAIN; |
| 151 | else if (dev->power.runtime_status == RPM_SUSPENDED) |
| 152 | retval = 1; |
| 153 | |
| 154 | return retval; |
| 155 | } |
| 156 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 157 | /** |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame^] | 158 | * __rpm_callback - Run a given runtime PM callback for a given device. |
| 159 | * @cb: Runtime PM callback to run. |
| 160 | * @dev: Device to run the callback for. |
| 161 | */ |
| 162 | static int __rpm_callback(int (*cb)(struct device *), struct device *dev) |
| 163 | __releases(&dev->power.lock) __acquires(&dev->power.lock) |
| 164 | { |
| 165 | int retval; |
| 166 | |
| 167 | if (dev->power.irq_safe) |
| 168 | spin_unlock(&dev->power.lock); |
| 169 | else |
| 170 | spin_unlock_irq(&dev->power.lock); |
| 171 | |
| 172 | retval = cb(dev); |
| 173 | |
| 174 | if (dev->power.irq_safe) |
| 175 | spin_lock(&dev->power.lock); |
| 176 | else |
| 177 | spin_lock_irq(&dev->power.lock); |
| 178 | |
| 179 | return retval; |
| 180 | } |
| 181 | |
| 182 | /** |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 183 | * rpm_idle - Notify device bus type if the device can be suspended. |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 184 | * @dev: Device to notify the bus type about. |
| 185 | * @rpmflags: Flag bits. |
| 186 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 187 | * Check if the device's runtime PM status allows it to be suspended. If |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 188 | * another idle notification has been started earlier, return immediately. If |
| 189 | * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise |
| 190 | * run the ->runtime_idle() callback directly. |
| 191 | * |
| 192 | * This function must be called under dev->power.lock with interrupts disabled. |
| 193 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 194 | static int rpm_idle(struct device *dev, int rpmflags) |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 195 | { |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 196 | int (*callback)(struct device *); |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 197 | int retval; |
| 198 | |
| 199 | retval = rpm_check_suspend_allowed(dev); |
| 200 | if (retval < 0) |
| 201 | ; /* Conditions are wrong. */ |
| 202 | |
| 203 | /* Idle notifications are allowed only in the RPM_ACTIVE state. */ |
| 204 | else if (dev->power.runtime_status != RPM_ACTIVE) |
| 205 | retval = -EAGAIN; |
| 206 | |
| 207 | /* |
| 208 | * Any pending request other than an idle notification takes |
| 209 | * precedence over us, except that the timer may be running. |
| 210 | */ |
| 211 | else if (dev->power.request_pending && |
| 212 | dev->power.request > RPM_REQ_IDLE) |
| 213 | retval = -EAGAIN; |
| 214 | |
| 215 | /* Act as though RPM_NOWAIT is always set. */ |
| 216 | else if (dev->power.idle_notification) |
| 217 | retval = -EINPROGRESS; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 218 | if (retval) |
| 219 | goto out; |
| 220 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 221 | /* Pending requests need to be canceled. */ |
| 222 | dev->power.request = RPM_REQ_NONE; |
| 223 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 224 | if (dev->power.no_callbacks) { |
| 225 | /* Assume ->runtime_idle() callback would have suspended. */ |
| 226 | retval = rpm_suspend(dev, rpmflags); |
| 227 | goto out; |
| 228 | } |
| 229 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 230 | /* Carry out an asynchronous or a synchronous idle notification. */ |
| 231 | if (rpmflags & RPM_ASYNC) { |
| 232 | dev->power.request = RPM_REQ_IDLE; |
| 233 | if (!dev->power.request_pending) { |
| 234 | dev->power.request_pending = true; |
| 235 | queue_work(pm_wq, &dev->power.work); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 236 | } |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 237 | goto out; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | dev->power.idle_notification = true; |
| 241 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 242 | if (dev->pm_domain) |
| 243 | callback = dev->pm_domain->ops.runtime_idle; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 244 | else if (dev->type && dev->type->pm) |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 245 | callback = dev->type->pm->runtime_idle; |
| 246 | else if (dev->class && dev->class->pm) |
| 247 | callback = dev->class->pm->runtime_idle; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 248 | else if (dev->bus && dev->bus->pm) |
| 249 | callback = dev->bus->pm->runtime_idle; |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 250 | else |
| 251 | callback = NULL; |
| 252 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame^] | 253 | if (callback) |
| 254 | __rpm_callback(callback, dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 255 | |
| 256 | dev->power.idle_notification = false; |
| 257 | wake_up_all(&dev->power.wait_queue); |
| 258 | |
| 259 | out: |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 260 | return retval; |
| 261 | } |
| 262 | |
| 263 | /** |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 264 | * rpm_callback - Run a given runtime PM callback for a given device. |
| 265 | * @cb: Runtime PM callback to run. |
| 266 | * @dev: Device to run the callback for. |
| 267 | */ |
| 268 | static int rpm_callback(int (*cb)(struct device *), struct device *dev) |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 269 | { |
| 270 | int retval; |
| 271 | |
| 272 | if (!cb) |
| 273 | return -ENOSYS; |
| 274 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame^] | 275 | retval = __rpm_callback(cb, dev); |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 276 | |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 277 | dev->power.runtime_error = retval; |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 278 | return retval != -EACCES ? retval : -EIO; |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 282 | * rpm_suspend - Carry out runtime suspend of given device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 283 | * @dev: Device to suspend. |
Alan Stern | 3f9af05 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 284 | * @rpmflags: Flag bits. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 285 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 286 | * Check if the device's runtime PM status allows it to be suspended. If |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 287 | * another suspend has been started earlier, either return immediately or wait |
| 288 | * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a |
| 289 | * pending idle notification. If the RPM_ASYNC flag is set then queue a |
| 290 | * suspend request; otherwise run the ->runtime_suspend() callback directly. |
| 291 | * If a deferred resume was requested while the callback was running then carry |
| 292 | * it out; otherwise send an idle notification for the device (if the suspend |
| 293 | * failed) or for its parent (if the suspend succeeded). |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 294 | * |
| 295 | * This function must be called under dev->power.lock with interrupts disabled. |
| 296 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 297 | static int rpm_suspend(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 298 | __releases(&dev->power.lock) __acquires(&dev->power.lock) |
| 299 | { |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 300 | int (*callback)(struct device *); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 301 | struct device *parent = NULL; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 302 | int retval; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 303 | |
Alan Stern | 3f9af05 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 304 | dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 305 | |
| 306 | repeat: |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 307 | retval = rpm_check_suspend_allowed(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 308 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 309 | if (retval < 0) |
| 310 | ; /* Conditions are wrong. */ |
| 311 | |
| 312 | /* Synchronous suspends are not allowed in the RPM_RESUMING state. */ |
| 313 | else if (dev->power.runtime_status == RPM_RESUMING && |
| 314 | !(rpmflags & RPM_ASYNC)) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 315 | retval = -EAGAIN; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 316 | if (retval) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 317 | goto out; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 318 | |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 319 | /* If the autosuspend_delay time hasn't expired yet, reschedule. */ |
| 320 | if ((rpmflags & RPM_AUTO) |
| 321 | && dev->power.runtime_status != RPM_SUSPENDING) { |
| 322 | unsigned long expires = pm_runtime_autosuspend_expiration(dev); |
| 323 | |
| 324 | if (expires != 0) { |
| 325 | /* Pending requests need to be canceled. */ |
| 326 | dev->power.request = RPM_REQ_NONE; |
| 327 | |
| 328 | /* |
| 329 | * Optimization: If the timer is already running and is |
| 330 | * set to expire at or before the autosuspend delay, |
| 331 | * avoid the overhead of resetting it. Just let it |
| 332 | * expire; pm_suspend_timer_fn() will take care of the |
| 333 | * rest. |
| 334 | */ |
| 335 | if (!(dev->power.timer_expires && time_before_eq( |
| 336 | dev->power.timer_expires, expires))) { |
| 337 | dev->power.timer_expires = expires; |
| 338 | mod_timer(&dev->power.suspend_timer, expires); |
| 339 | } |
| 340 | dev->power.timer_autosuspends = 1; |
| 341 | goto out; |
| 342 | } |
| 343 | } |
| 344 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 345 | /* Other scheduled or pending requests need to be canceled. */ |
| 346 | pm_runtime_cancel_pending(dev); |
| 347 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 348 | if (dev->power.runtime_status == RPM_SUSPENDING) { |
| 349 | DEFINE_WAIT(wait); |
| 350 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 351 | if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 352 | retval = -EINPROGRESS; |
| 353 | goto out; |
| 354 | } |
| 355 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame^] | 356 | if (dev->power.irq_safe) { |
| 357 | spin_unlock(&dev->power.lock); |
| 358 | |
| 359 | cpu_relax(); |
| 360 | |
| 361 | spin_lock(&dev->power.lock); |
| 362 | goto repeat; |
| 363 | } |
| 364 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 365 | /* Wait for the other suspend running in parallel with us. */ |
| 366 | for (;;) { |
| 367 | prepare_to_wait(&dev->power.wait_queue, &wait, |
| 368 | TASK_UNINTERRUPTIBLE); |
| 369 | if (dev->power.runtime_status != RPM_SUSPENDING) |
| 370 | break; |
| 371 | |
| 372 | spin_unlock_irq(&dev->power.lock); |
| 373 | |
| 374 | schedule(); |
| 375 | |
| 376 | spin_lock_irq(&dev->power.lock); |
| 377 | } |
| 378 | finish_wait(&dev->power.wait_queue, &wait); |
| 379 | goto repeat; |
| 380 | } |
| 381 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 382 | dev->power.deferred_resume = false; |
| 383 | if (dev->power.no_callbacks) |
| 384 | goto no_callback; /* Assume success. */ |
| 385 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 386 | /* Carry out an asynchronous or a synchronous suspend. */ |
| 387 | if (rpmflags & RPM_ASYNC) { |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 388 | dev->power.request = (rpmflags & RPM_AUTO) ? |
| 389 | RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 390 | if (!dev->power.request_pending) { |
| 391 | dev->power.request_pending = true; |
| 392 | queue_work(pm_wq, &dev->power.work); |
| 393 | } |
| 394 | goto out; |
| 395 | } |
| 396 | |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 397 | __update_runtime_status(dev, RPM_SUSPENDING); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 398 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 399 | if (dev->pm_domain) |
| 400 | callback = dev->pm_domain->ops.runtime_suspend; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 401 | else if (dev->type && dev->type->pm) |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 402 | callback = dev->type->pm->runtime_suspend; |
| 403 | else if (dev->class && dev->class->pm) |
| 404 | callback = dev->class->pm->runtime_suspend; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 405 | else if (dev->bus && dev->bus->pm) |
| 406 | callback = dev->bus->pm->runtime_suspend; |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 407 | else |
| 408 | callback = NULL; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 409 | |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 410 | retval = rpm_callback(callback, dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 411 | if (retval) { |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 412 | __update_runtime_status(dev, RPM_ACTIVE); |
ShuoX Liu | 2cffff1 | 2011-07-08 20:53:55 +0200 | [diff] [blame] | 413 | dev->power.deferred_resume = false; |
Rafael J. Wysocki | f71648d | 2010-10-11 01:02:27 +0200 | [diff] [blame] | 414 | if (retval == -EAGAIN || retval == -EBUSY) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 415 | dev->power.runtime_error = 0; |
Rafael J. Wysocki | f71648d | 2010-10-11 01:02:27 +0200 | [diff] [blame] | 416 | else |
Alan Stern | 240c733 | 2010-03-23 00:50:07 +0100 | [diff] [blame] | 417 | pm_runtime_cancel_pending(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 418 | } else { |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 419 | no_callback: |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 420 | __update_runtime_status(dev, RPM_SUSPENDED); |
Alan Stern | 240c733 | 2010-03-23 00:50:07 +0100 | [diff] [blame] | 421 | pm_runtime_deactivate_timer(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 422 | |
| 423 | if (dev->parent) { |
| 424 | parent = dev->parent; |
| 425 | atomic_add_unless(&parent->power.child_count, -1, 0); |
| 426 | } |
| 427 | } |
| 428 | wake_up_all(&dev->power.wait_queue); |
| 429 | |
| 430 | if (dev->power.deferred_resume) { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 431 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 432 | retval = -EAGAIN; |
| 433 | goto out; |
| 434 | } |
| 435 | |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 436 | /* Maybe the parent is now able to suspend. */ |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 437 | if (parent && !parent->power.ignore_children && !dev->power.irq_safe) { |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 438 | spin_unlock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 439 | |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 440 | spin_lock(&parent->power.lock); |
| 441 | rpm_idle(parent, RPM_ASYNC); |
| 442 | spin_unlock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 443 | |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 444 | spin_lock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | out: |
Alan Stern | 3f9af05 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 448 | dev_dbg(dev, "%s returns %d\n", __func__, retval); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 449 | |
| 450 | return retval; |
| 451 | } |
| 452 | |
| 453 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 454 | * rpm_resume - Carry out runtime resume of given device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 455 | * @dev: Device to resume. |
Alan Stern | 3f9af05 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 456 | * @rpmflags: Flag bits. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 457 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 458 | * Check if the device's runtime PM status allows it to be resumed. Cancel |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 459 | * any scheduled or pending requests. If another resume has been started |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 460 | * earlier, either return immediately or wait for it to finish, depending on the |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 461 | * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in |
| 462 | * parallel with this function, either tell the other process to resume after |
| 463 | * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC |
| 464 | * flag is set then queue a resume request; otherwise run the |
| 465 | * ->runtime_resume() callback directly. Queue an idle notification for the |
| 466 | * device if the resume succeeded. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 467 | * |
| 468 | * This function must be called under dev->power.lock with interrupts disabled. |
| 469 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 470 | static int rpm_resume(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 471 | __releases(&dev->power.lock) __acquires(&dev->power.lock) |
| 472 | { |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 473 | int (*callback)(struct device *); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 474 | struct device *parent = NULL; |
| 475 | int retval = 0; |
| 476 | |
Alan Stern | 3f9af05 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 477 | dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 478 | |
| 479 | repeat: |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 480 | if (dev->power.runtime_error) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 481 | retval = -EINVAL; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 482 | else if (dev->power.disable_depth > 0) |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 483 | retval = -EACCES; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 484 | if (retval) |
| 485 | goto out; |
| 486 | |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 487 | /* |
| 488 | * Other scheduled or pending requests need to be canceled. Small |
| 489 | * optimization: If an autosuspend timer is running, leave it running |
| 490 | * rather than cancelling it now only to restart it again in the near |
| 491 | * future. |
| 492 | */ |
| 493 | dev->power.request = RPM_REQ_NONE; |
| 494 | if (!dev->power.timer_autosuspends) |
| 495 | pm_runtime_deactivate_timer(dev); |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 496 | |
| 497 | if (dev->power.runtime_status == RPM_ACTIVE) { |
| 498 | retval = 1; |
| 499 | goto out; |
| 500 | } |
| 501 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 502 | if (dev->power.runtime_status == RPM_RESUMING |
| 503 | || dev->power.runtime_status == RPM_SUSPENDING) { |
| 504 | DEFINE_WAIT(wait); |
| 505 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 506 | if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 507 | if (dev->power.runtime_status == RPM_SUSPENDING) |
| 508 | dev->power.deferred_resume = true; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 509 | else |
| 510 | retval = -EINPROGRESS; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 511 | goto out; |
| 512 | } |
| 513 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame^] | 514 | if (dev->power.irq_safe) { |
| 515 | spin_unlock(&dev->power.lock); |
| 516 | |
| 517 | cpu_relax(); |
| 518 | |
| 519 | spin_lock(&dev->power.lock); |
| 520 | goto repeat; |
| 521 | } |
| 522 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 523 | /* Wait for the operation carried out in parallel with us. */ |
| 524 | for (;;) { |
| 525 | prepare_to_wait(&dev->power.wait_queue, &wait, |
| 526 | TASK_UNINTERRUPTIBLE); |
| 527 | if (dev->power.runtime_status != RPM_RESUMING |
| 528 | && dev->power.runtime_status != RPM_SUSPENDING) |
| 529 | break; |
| 530 | |
| 531 | spin_unlock_irq(&dev->power.lock); |
| 532 | |
| 533 | schedule(); |
| 534 | |
| 535 | spin_lock_irq(&dev->power.lock); |
| 536 | } |
| 537 | finish_wait(&dev->power.wait_queue, &wait); |
| 538 | goto repeat; |
| 539 | } |
| 540 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 541 | /* |
| 542 | * See if we can skip waking up the parent. This is safe only if |
| 543 | * power.no_callbacks is set, because otherwise we don't know whether |
| 544 | * the resume will actually succeed. |
| 545 | */ |
| 546 | if (dev->power.no_callbacks && !parent && dev->parent) { |
Ming Lei | d63be5f | 2010-10-22 23:48:14 +0200 | [diff] [blame] | 547 | spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING); |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 548 | if (dev->parent->power.disable_depth > 0 |
| 549 | || dev->parent->power.ignore_children |
| 550 | || dev->parent->power.runtime_status == RPM_ACTIVE) { |
| 551 | atomic_inc(&dev->parent->power.child_count); |
| 552 | spin_unlock(&dev->parent->power.lock); |
| 553 | goto no_callback; /* Assume success. */ |
| 554 | } |
| 555 | spin_unlock(&dev->parent->power.lock); |
| 556 | } |
| 557 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 558 | /* Carry out an asynchronous or a synchronous resume. */ |
| 559 | if (rpmflags & RPM_ASYNC) { |
| 560 | dev->power.request = RPM_REQ_RESUME; |
| 561 | if (!dev->power.request_pending) { |
| 562 | dev->power.request_pending = true; |
| 563 | queue_work(pm_wq, &dev->power.work); |
| 564 | } |
| 565 | retval = 0; |
| 566 | goto out; |
| 567 | } |
| 568 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 569 | if (!parent && dev->parent) { |
| 570 | /* |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 571 | * Increment the parent's usage counter and resume it if |
| 572 | * necessary. Not needed if dev is irq-safe; then the |
| 573 | * parent is permanently resumed. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 574 | */ |
| 575 | parent = dev->parent; |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 576 | if (dev->power.irq_safe) |
| 577 | goto skip_parent; |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 578 | spin_unlock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 579 | |
| 580 | pm_runtime_get_noresume(parent); |
| 581 | |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 582 | spin_lock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 583 | /* |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 584 | * We can resume if the parent's runtime PM is disabled or it |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 585 | * is set to ignore children. |
| 586 | */ |
| 587 | if (!parent->power.disable_depth |
| 588 | && !parent->power.ignore_children) { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 589 | rpm_resume(parent, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 590 | if (parent->power.runtime_status != RPM_ACTIVE) |
| 591 | retval = -EBUSY; |
| 592 | } |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 593 | spin_unlock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 594 | |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 595 | spin_lock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 596 | if (retval) |
| 597 | goto out; |
| 598 | goto repeat; |
| 599 | } |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 600 | skip_parent: |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 601 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 602 | if (dev->power.no_callbacks) |
| 603 | goto no_callback; /* Assume success. */ |
| 604 | |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 605 | __update_runtime_status(dev, RPM_RESUMING); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 606 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 607 | if (dev->pm_domain) |
| 608 | callback = dev->pm_domain->ops.runtime_resume; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 609 | else if (dev->type && dev->type->pm) |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 610 | callback = dev->type->pm->runtime_resume; |
| 611 | else if (dev->class && dev->class->pm) |
| 612 | callback = dev->class->pm->runtime_resume; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 613 | else if (dev->bus && dev->bus->pm) |
| 614 | callback = dev->bus->pm->runtime_resume; |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 615 | else |
| 616 | callback = NULL; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 617 | |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 618 | retval = rpm_callback(callback, dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 619 | if (retval) { |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 620 | __update_runtime_status(dev, RPM_SUSPENDED); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 621 | pm_runtime_cancel_pending(dev); |
| 622 | } else { |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 623 | no_callback: |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 624 | __update_runtime_status(dev, RPM_ACTIVE); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 625 | if (parent) |
| 626 | atomic_inc(&parent->power.child_count); |
| 627 | } |
| 628 | wake_up_all(&dev->power.wait_queue); |
| 629 | |
| 630 | if (!retval) |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 631 | rpm_idle(dev, RPM_ASYNC); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 632 | |
| 633 | out: |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 634 | if (parent && !dev->power.irq_safe) { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 635 | spin_unlock_irq(&dev->power.lock); |
| 636 | |
| 637 | pm_runtime_put(parent); |
| 638 | |
| 639 | spin_lock_irq(&dev->power.lock); |
| 640 | } |
| 641 | |
Alan Stern | 3f9af05 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 642 | dev_dbg(dev, "%s returns %d\n", __func__, retval); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 643 | |
| 644 | return retval; |
| 645 | } |
| 646 | |
| 647 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 648 | * pm_runtime_work - Universal runtime PM work function. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 649 | * @work: Work structure used for scheduling the execution of this function. |
| 650 | * |
| 651 | * Use @work to get the device object the work is to be done for, determine what |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 652 | * is to be done and execute the appropriate runtime PM function. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 653 | */ |
| 654 | static void pm_runtime_work(struct work_struct *work) |
| 655 | { |
| 656 | struct device *dev = container_of(work, struct device, power.work); |
| 657 | enum rpm_request req; |
| 658 | |
| 659 | spin_lock_irq(&dev->power.lock); |
| 660 | |
| 661 | if (!dev->power.request_pending) |
| 662 | goto out; |
| 663 | |
| 664 | req = dev->power.request; |
| 665 | dev->power.request = RPM_REQ_NONE; |
| 666 | dev->power.request_pending = false; |
| 667 | |
| 668 | switch (req) { |
| 669 | case RPM_REQ_NONE: |
| 670 | break; |
| 671 | case RPM_REQ_IDLE: |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 672 | rpm_idle(dev, RPM_NOWAIT); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 673 | break; |
| 674 | case RPM_REQ_SUSPEND: |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 675 | rpm_suspend(dev, RPM_NOWAIT); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 676 | break; |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 677 | case RPM_REQ_AUTOSUSPEND: |
| 678 | rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO); |
| 679 | break; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 680 | case RPM_REQ_RESUME: |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 681 | rpm_resume(dev, RPM_NOWAIT); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 682 | break; |
| 683 | } |
| 684 | |
| 685 | out: |
| 686 | spin_unlock_irq(&dev->power.lock); |
| 687 | } |
| 688 | |
| 689 | /** |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 690 | * pm_suspend_timer_fn - Timer function for pm_schedule_suspend(). |
| 691 | * @data: Device pointer passed by pm_schedule_suspend(). |
| 692 | * |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 693 | * Check if the time is right and queue a suspend request. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 694 | */ |
| 695 | static void pm_suspend_timer_fn(unsigned long data) |
| 696 | { |
| 697 | struct device *dev = (struct device *)data; |
| 698 | unsigned long flags; |
| 699 | unsigned long expires; |
| 700 | |
| 701 | spin_lock_irqsave(&dev->power.lock, flags); |
| 702 | |
| 703 | expires = dev->power.timer_expires; |
| 704 | /* If 'expire' is after 'jiffies' we've been called too early. */ |
| 705 | if (expires > 0 && !time_after(expires, jiffies)) { |
| 706 | dev->power.timer_expires = 0; |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 707 | rpm_suspend(dev, dev->power.timer_autosuspends ? |
| 708 | (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * pm_schedule_suspend - Set up a timer to submit a suspend request in future. |
| 716 | * @dev: Device to suspend. |
| 717 | * @delay: Time to wait before submitting a suspend request, in milliseconds. |
| 718 | */ |
| 719 | int pm_schedule_suspend(struct device *dev, unsigned int delay) |
| 720 | { |
| 721 | unsigned long flags; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 722 | int retval; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 723 | |
| 724 | spin_lock_irqsave(&dev->power.lock, flags); |
| 725 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 726 | if (!delay) { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 727 | retval = rpm_suspend(dev, RPM_ASYNC); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 728 | goto out; |
| 729 | } |
| 730 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 731 | retval = rpm_check_suspend_allowed(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 732 | if (retval) |
| 733 | goto out; |
| 734 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 735 | /* Other scheduled or pending requests need to be canceled. */ |
| 736 | pm_runtime_cancel_pending(dev); |
| 737 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 738 | dev->power.timer_expires = jiffies + msecs_to_jiffies(delay); |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 739 | dev->power.timer_expires += !dev->power.timer_expires; |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 740 | dev->power.timer_autosuspends = 0; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 741 | mod_timer(&dev->power.suspend_timer, dev->power.timer_expires); |
| 742 | |
| 743 | out: |
| 744 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 745 | |
| 746 | return retval; |
| 747 | } |
| 748 | EXPORT_SYMBOL_GPL(pm_schedule_suspend); |
| 749 | |
| 750 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 751 | * __pm_runtime_idle - Entry point for runtime idle operations. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 752 | * @dev: Device to send idle notification for. |
| 753 | * @rpmflags: Flag bits. |
| 754 | * |
| 755 | * If the RPM_GET_PUT flag is set, decrement the device's usage count and |
| 756 | * return immediately if it is larger than zero. Then carry out an idle |
| 757 | * notification, either synchronous or asynchronous. |
| 758 | * |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 759 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
| 760 | * or if pm_runtime_irq_safe() has been called. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 761 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 762 | int __pm_runtime_idle(struct device *dev, int rpmflags) |
| 763 | { |
| 764 | unsigned long flags; |
| 765 | int retval; |
| 766 | |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 767 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); |
| 768 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 769 | if (rpmflags & RPM_GET_PUT) { |
| 770 | if (!atomic_dec_and_test(&dev->power.usage_count)) |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | spin_lock_irqsave(&dev->power.lock, flags); |
| 775 | retval = rpm_idle(dev, rpmflags); |
| 776 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 777 | |
| 778 | return retval; |
| 779 | } |
| 780 | EXPORT_SYMBOL_GPL(__pm_runtime_idle); |
| 781 | |
| 782 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 783 | * __pm_runtime_suspend - Entry point for runtime put/suspend operations. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 784 | * @dev: Device to suspend. |
| 785 | * @rpmflags: Flag bits. |
| 786 | * |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 787 | * If the RPM_GET_PUT flag is set, decrement the device's usage count and |
| 788 | * return immediately if it is larger than zero. Then carry out a suspend, |
| 789 | * either synchronous or asynchronous. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 790 | * |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 791 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
| 792 | * or if pm_runtime_irq_safe() has been called. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 793 | */ |
| 794 | int __pm_runtime_suspend(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 795 | { |
| 796 | unsigned long flags; |
| 797 | int retval; |
| 798 | |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 799 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); |
| 800 | |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 801 | if (rpmflags & RPM_GET_PUT) { |
| 802 | if (!atomic_dec_and_test(&dev->power.usage_count)) |
| 803 | return 0; |
| 804 | } |
| 805 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 806 | spin_lock_irqsave(&dev->power.lock, flags); |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 807 | retval = rpm_suspend(dev, rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 808 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 809 | |
| 810 | return retval; |
| 811 | } |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 812 | EXPORT_SYMBOL_GPL(__pm_runtime_suspend); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 813 | |
| 814 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 815 | * __pm_runtime_resume - Entry point for runtime resume operations. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 816 | * @dev: Device to resume. |
Alan Stern | 3f9af05 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 817 | * @rpmflags: Flag bits. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 818 | * |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 819 | * If the RPM_GET_PUT flag is set, increment the device's usage count. Then |
| 820 | * carry out a resume, either synchronous or asynchronous. |
| 821 | * |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 822 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
| 823 | * or if pm_runtime_irq_safe() has been called. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 824 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 825 | int __pm_runtime_resume(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 826 | { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 827 | unsigned long flags; |
Alan Stern | 1d531c1 | 2009-12-13 20:28:30 +0100 | [diff] [blame] | 828 | int retval; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 829 | |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 830 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); |
| 831 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 832 | if (rpmflags & RPM_GET_PUT) |
| 833 | atomic_inc(&dev->power.usage_count); |
| 834 | |
| 835 | spin_lock_irqsave(&dev->power.lock, flags); |
| 836 | retval = rpm_resume(dev, rpmflags); |
| 837 | spin_unlock_irqrestore(&dev->power.lock, flags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 838 | |
| 839 | return retval; |
| 840 | } |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 841 | EXPORT_SYMBOL_GPL(__pm_runtime_resume); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 842 | |
| 843 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 844 | * __pm_runtime_set_status - Set runtime PM status of a device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 845 | * @dev: Device to handle. |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 846 | * @status: New runtime PM status of the device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 847 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 848 | * If runtime PM of the device is disabled or its power.runtime_error field is |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 849 | * different from zero, the status may be changed either to RPM_ACTIVE, or to |
| 850 | * RPM_SUSPENDED, as long as that reflects the actual state of the device. |
| 851 | * However, if the device has a parent and the parent is not active, and the |
| 852 | * parent's power.ignore_children flag is unset, the device's status cannot be |
| 853 | * set to RPM_ACTIVE, so -EBUSY is returned in that case. |
| 854 | * |
| 855 | * If successful, __pm_runtime_set_status() clears the power.runtime_error field |
| 856 | * and the device parent's counter of unsuspended children is modified to |
| 857 | * reflect the new status. If the new status is RPM_SUSPENDED, an idle |
| 858 | * notification request for the parent is submitted. |
| 859 | */ |
| 860 | int __pm_runtime_set_status(struct device *dev, unsigned int status) |
| 861 | { |
| 862 | struct device *parent = dev->parent; |
| 863 | unsigned long flags; |
| 864 | bool notify_parent = false; |
| 865 | int error = 0; |
| 866 | |
| 867 | if (status != RPM_ACTIVE && status != RPM_SUSPENDED) |
| 868 | return -EINVAL; |
| 869 | |
| 870 | spin_lock_irqsave(&dev->power.lock, flags); |
| 871 | |
| 872 | if (!dev->power.runtime_error && !dev->power.disable_depth) { |
| 873 | error = -EAGAIN; |
| 874 | goto out; |
| 875 | } |
| 876 | |
| 877 | if (dev->power.runtime_status == status) |
| 878 | goto out_set; |
| 879 | |
| 880 | if (status == RPM_SUSPENDED) { |
| 881 | /* It always is possible to set the status to 'suspended'. */ |
| 882 | if (parent) { |
| 883 | atomic_add_unless(&parent->power.child_count, -1, 0); |
| 884 | notify_parent = !parent->power.ignore_children; |
| 885 | } |
| 886 | goto out_set; |
| 887 | } |
| 888 | |
| 889 | if (parent) { |
Rafael J. Wysocki | bab636b | 2009-12-03 20:21:21 +0100 | [diff] [blame] | 890 | spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 891 | |
| 892 | /* |
| 893 | * It is invalid to put an active child under a parent that is |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 894 | * not active, has runtime PM enabled and the |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 895 | * 'power.ignore_children' flag unset. |
| 896 | */ |
| 897 | if (!parent->power.disable_depth |
| 898 | && !parent->power.ignore_children |
Rafael J. Wysocki | 965c4ac | 2009-12-03 21:04:41 +0100 | [diff] [blame] | 899 | && parent->power.runtime_status != RPM_ACTIVE) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 900 | error = -EBUSY; |
Rafael J. Wysocki | 965c4ac | 2009-12-03 21:04:41 +0100 | [diff] [blame] | 901 | else if (dev->power.runtime_status == RPM_SUSPENDED) |
| 902 | atomic_inc(&parent->power.child_count); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 903 | |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 904 | spin_unlock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 905 | |
| 906 | if (error) |
| 907 | goto out; |
| 908 | } |
| 909 | |
| 910 | out_set: |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 911 | __update_runtime_status(dev, status); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 912 | dev->power.runtime_error = 0; |
| 913 | out: |
| 914 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 915 | |
| 916 | if (notify_parent) |
| 917 | pm_request_idle(parent); |
| 918 | |
| 919 | return error; |
| 920 | } |
| 921 | EXPORT_SYMBOL_GPL(__pm_runtime_set_status); |
| 922 | |
| 923 | /** |
| 924 | * __pm_runtime_barrier - Cancel pending requests and wait for completions. |
| 925 | * @dev: Device to handle. |
| 926 | * |
| 927 | * Flush all pending requests for the device from pm_wq and wait for all |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 928 | * runtime PM operations involving the device in progress to complete. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 929 | * |
| 930 | * Should be called under dev->power.lock with interrupts disabled. |
| 931 | */ |
| 932 | static void __pm_runtime_barrier(struct device *dev) |
| 933 | { |
| 934 | pm_runtime_deactivate_timer(dev); |
| 935 | |
| 936 | if (dev->power.request_pending) { |
| 937 | dev->power.request = RPM_REQ_NONE; |
| 938 | spin_unlock_irq(&dev->power.lock); |
| 939 | |
| 940 | cancel_work_sync(&dev->power.work); |
| 941 | |
| 942 | spin_lock_irq(&dev->power.lock); |
| 943 | dev->power.request_pending = false; |
| 944 | } |
| 945 | |
| 946 | if (dev->power.runtime_status == RPM_SUSPENDING |
| 947 | || dev->power.runtime_status == RPM_RESUMING |
| 948 | || dev->power.idle_notification) { |
| 949 | DEFINE_WAIT(wait); |
| 950 | |
| 951 | /* Suspend, wake-up or idle notification in progress. */ |
| 952 | for (;;) { |
| 953 | prepare_to_wait(&dev->power.wait_queue, &wait, |
| 954 | TASK_UNINTERRUPTIBLE); |
| 955 | if (dev->power.runtime_status != RPM_SUSPENDING |
| 956 | && dev->power.runtime_status != RPM_RESUMING |
| 957 | && !dev->power.idle_notification) |
| 958 | break; |
| 959 | spin_unlock_irq(&dev->power.lock); |
| 960 | |
| 961 | schedule(); |
| 962 | |
| 963 | spin_lock_irq(&dev->power.lock); |
| 964 | } |
| 965 | finish_wait(&dev->power.wait_queue, &wait); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * pm_runtime_barrier - Flush pending requests and wait for completions. |
| 971 | * @dev: Device to handle. |
| 972 | * |
| 973 | * Prevent the device from being suspended by incrementing its usage counter and |
| 974 | * if there's a pending resume request for the device, wake the device up. |
| 975 | * Next, make sure that all pending requests for the device have been flushed |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 976 | * from pm_wq and wait for all runtime PM operations involving the device in |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 977 | * progress to complete. |
| 978 | * |
| 979 | * Return value: |
| 980 | * 1, if there was a resume request pending and the device had to be woken up, |
| 981 | * 0, otherwise |
| 982 | */ |
| 983 | int pm_runtime_barrier(struct device *dev) |
| 984 | { |
| 985 | int retval = 0; |
| 986 | |
| 987 | pm_runtime_get_noresume(dev); |
| 988 | spin_lock_irq(&dev->power.lock); |
| 989 | |
| 990 | if (dev->power.request_pending |
| 991 | && dev->power.request == RPM_REQ_RESUME) { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 992 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 993 | retval = 1; |
| 994 | } |
| 995 | |
| 996 | __pm_runtime_barrier(dev); |
| 997 | |
| 998 | spin_unlock_irq(&dev->power.lock); |
| 999 | pm_runtime_put_noidle(dev); |
| 1000 | |
| 1001 | return retval; |
| 1002 | } |
| 1003 | EXPORT_SYMBOL_GPL(pm_runtime_barrier); |
| 1004 | |
| 1005 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1006 | * __pm_runtime_disable - Disable runtime PM of a device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1007 | * @dev: Device to handle. |
| 1008 | * @check_resume: If set, check if there's a resume request for the device. |
| 1009 | * |
| 1010 | * Increment power.disable_depth for the device and if was zero previously, |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1011 | * cancel all pending runtime PM requests for the device and wait for all |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1012 | * operations in progress to complete. The device can be either active or |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1013 | * suspended after its runtime PM has been disabled. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1014 | * |
| 1015 | * If @check_resume is set and there's a resume request pending when |
| 1016 | * __pm_runtime_disable() is called and power.disable_depth is zero, the |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1017 | * function will wake up the device before disabling its runtime PM. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1018 | */ |
| 1019 | void __pm_runtime_disable(struct device *dev, bool check_resume) |
| 1020 | { |
| 1021 | spin_lock_irq(&dev->power.lock); |
| 1022 | |
| 1023 | if (dev->power.disable_depth > 0) { |
| 1024 | dev->power.disable_depth++; |
| 1025 | goto out; |
| 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * Wake up the device if there's a resume request pending, because that |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1030 | * means there probably is some I/O to process and disabling runtime PM |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1031 | * shouldn't prevent the device from processing the I/O. |
| 1032 | */ |
| 1033 | if (check_resume && dev->power.request_pending |
| 1034 | && dev->power.request == RPM_REQ_RESUME) { |
| 1035 | /* |
| 1036 | * Prevent suspends and idle notifications from being carried |
| 1037 | * out after we have woken up the device. |
| 1038 | */ |
| 1039 | pm_runtime_get_noresume(dev); |
| 1040 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1041 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1042 | |
| 1043 | pm_runtime_put_noidle(dev); |
| 1044 | } |
| 1045 | |
| 1046 | if (!dev->power.disable_depth++) |
| 1047 | __pm_runtime_barrier(dev); |
| 1048 | |
| 1049 | out: |
| 1050 | spin_unlock_irq(&dev->power.lock); |
| 1051 | } |
| 1052 | EXPORT_SYMBOL_GPL(__pm_runtime_disable); |
| 1053 | |
| 1054 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1055 | * pm_runtime_enable - Enable runtime PM of a device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1056 | * @dev: Device to handle. |
| 1057 | */ |
| 1058 | void pm_runtime_enable(struct device *dev) |
| 1059 | { |
| 1060 | unsigned long flags; |
| 1061 | |
| 1062 | spin_lock_irqsave(&dev->power.lock, flags); |
| 1063 | |
| 1064 | if (dev->power.disable_depth > 0) |
| 1065 | dev->power.disable_depth--; |
| 1066 | else |
| 1067 | dev_warn(dev, "Unbalanced %s!\n", __func__); |
| 1068 | |
| 1069 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 1070 | } |
| 1071 | EXPORT_SYMBOL_GPL(pm_runtime_enable); |
| 1072 | |
| 1073 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1074 | * pm_runtime_forbid - Block runtime PM of a device. |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1075 | * @dev: Device to handle. |
| 1076 | * |
| 1077 | * Increase the device's usage count and clear its power.runtime_auto flag, |
| 1078 | * so that it cannot be suspended at run time until pm_runtime_allow() is called |
| 1079 | * for it. |
| 1080 | */ |
| 1081 | void pm_runtime_forbid(struct device *dev) |
| 1082 | { |
| 1083 | spin_lock_irq(&dev->power.lock); |
| 1084 | if (!dev->power.runtime_auto) |
| 1085 | goto out; |
| 1086 | |
| 1087 | dev->power.runtime_auto = false; |
| 1088 | atomic_inc(&dev->power.usage_count); |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1089 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1090 | |
| 1091 | out: |
| 1092 | spin_unlock_irq(&dev->power.lock); |
| 1093 | } |
| 1094 | EXPORT_SYMBOL_GPL(pm_runtime_forbid); |
| 1095 | |
| 1096 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1097 | * pm_runtime_allow - Unblock runtime PM of a device. |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1098 | * @dev: Device to handle. |
| 1099 | * |
| 1100 | * Decrease the device's usage count and set its power.runtime_auto flag. |
| 1101 | */ |
| 1102 | void pm_runtime_allow(struct device *dev) |
| 1103 | { |
| 1104 | spin_lock_irq(&dev->power.lock); |
| 1105 | if (dev->power.runtime_auto) |
| 1106 | goto out; |
| 1107 | |
| 1108 | dev->power.runtime_auto = true; |
| 1109 | if (atomic_dec_and_test(&dev->power.usage_count)) |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 1110 | rpm_idle(dev, RPM_AUTO); |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1111 | |
| 1112 | out: |
| 1113 | spin_unlock_irq(&dev->power.lock); |
| 1114 | } |
| 1115 | EXPORT_SYMBOL_GPL(pm_runtime_allow); |
| 1116 | |
| 1117 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1118 | * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device. |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 1119 | * @dev: Device to handle. |
| 1120 | * |
| 1121 | * Set the power.no_callbacks flag, which tells the PM core that this |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1122 | * device is power-managed through its parent and has no runtime PM |
| 1123 | * callbacks of its own. The runtime sysfs attributes will be removed. |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 1124 | */ |
| 1125 | void pm_runtime_no_callbacks(struct device *dev) |
| 1126 | { |
| 1127 | spin_lock_irq(&dev->power.lock); |
| 1128 | dev->power.no_callbacks = 1; |
| 1129 | spin_unlock_irq(&dev->power.lock); |
| 1130 | if (device_is_registered(dev)) |
| 1131 | rpm_sysfs_remove(dev); |
| 1132 | } |
| 1133 | EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks); |
| 1134 | |
| 1135 | /** |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 1136 | * pm_runtime_irq_safe - Leave interrupts disabled during callbacks. |
| 1137 | * @dev: Device to handle |
| 1138 | * |
| 1139 | * Set the power.irq_safe flag, which tells the PM core that the |
| 1140 | * ->runtime_suspend() and ->runtime_resume() callbacks for this device should |
| 1141 | * always be invoked with the spinlock held and interrupts disabled. It also |
| 1142 | * causes the parent's usage counter to be permanently incremented, preventing |
| 1143 | * the parent from runtime suspending -- otherwise an irq-safe child might have |
| 1144 | * to wait for a non-irq-safe parent. |
| 1145 | */ |
| 1146 | void pm_runtime_irq_safe(struct device *dev) |
| 1147 | { |
| 1148 | if (dev->parent) |
| 1149 | pm_runtime_get_sync(dev->parent); |
| 1150 | spin_lock_irq(&dev->power.lock); |
| 1151 | dev->power.irq_safe = 1; |
| 1152 | spin_unlock_irq(&dev->power.lock); |
| 1153 | } |
| 1154 | EXPORT_SYMBOL_GPL(pm_runtime_irq_safe); |
| 1155 | |
| 1156 | /** |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 1157 | * update_autosuspend - Handle a change to a device's autosuspend settings. |
| 1158 | * @dev: Device to handle. |
| 1159 | * @old_delay: The former autosuspend_delay value. |
| 1160 | * @old_use: The former use_autosuspend value. |
| 1161 | * |
| 1162 | * Prevent runtime suspend if the new delay is negative and use_autosuspend is |
| 1163 | * set; otherwise allow it. Send an idle notification if suspends are allowed. |
| 1164 | * |
| 1165 | * This function must be called under dev->power.lock with interrupts disabled. |
| 1166 | */ |
| 1167 | static void update_autosuspend(struct device *dev, int old_delay, int old_use) |
| 1168 | { |
| 1169 | int delay = dev->power.autosuspend_delay; |
| 1170 | |
| 1171 | /* Should runtime suspend be prevented now? */ |
| 1172 | if (dev->power.use_autosuspend && delay < 0) { |
| 1173 | |
| 1174 | /* If it used to be allowed then prevent it. */ |
| 1175 | if (!old_use || old_delay >= 0) { |
| 1176 | atomic_inc(&dev->power.usage_count); |
| 1177 | rpm_resume(dev, 0); |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | /* Runtime suspend should be allowed now. */ |
| 1182 | else { |
| 1183 | |
| 1184 | /* If it used to be prevented then allow it. */ |
| 1185 | if (old_use && old_delay < 0) |
| 1186 | atomic_dec(&dev->power.usage_count); |
| 1187 | |
| 1188 | /* Maybe we can autosuspend now. */ |
| 1189 | rpm_idle(dev, RPM_AUTO); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | /** |
| 1194 | * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value. |
| 1195 | * @dev: Device to handle. |
| 1196 | * @delay: Value of the new delay in milliseconds. |
| 1197 | * |
| 1198 | * Set the device's power.autosuspend_delay value. If it changes to negative |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1199 | * and the power.use_autosuspend flag is set, prevent runtime suspends. If it |
| 1200 | * changes the other way, allow runtime suspends. |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 1201 | */ |
| 1202 | void pm_runtime_set_autosuspend_delay(struct device *dev, int delay) |
| 1203 | { |
| 1204 | int old_delay, old_use; |
| 1205 | |
| 1206 | spin_lock_irq(&dev->power.lock); |
| 1207 | old_delay = dev->power.autosuspend_delay; |
| 1208 | old_use = dev->power.use_autosuspend; |
| 1209 | dev->power.autosuspend_delay = delay; |
| 1210 | update_autosuspend(dev, old_delay, old_use); |
| 1211 | spin_unlock_irq(&dev->power.lock); |
| 1212 | } |
| 1213 | EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay); |
| 1214 | |
| 1215 | /** |
| 1216 | * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag. |
| 1217 | * @dev: Device to handle. |
| 1218 | * @use: New value for use_autosuspend. |
| 1219 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1220 | * Set the device's power.use_autosuspend flag, and allow or prevent runtime |
Alan Stern | 15bcb91 | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 1221 | * suspends as needed. |
| 1222 | */ |
| 1223 | void __pm_runtime_use_autosuspend(struct device *dev, bool use) |
| 1224 | { |
| 1225 | int old_delay, old_use; |
| 1226 | |
| 1227 | spin_lock_irq(&dev->power.lock); |
| 1228 | old_delay = dev->power.autosuspend_delay; |
| 1229 | old_use = dev->power.use_autosuspend; |
| 1230 | dev->power.use_autosuspend = use; |
| 1231 | update_autosuspend(dev, old_delay, old_use); |
| 1232 | spin_unlock_irq(&dev->power.lock); |
| 1233 | } |
| 1234 | EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend); |
| 1235 | |
| 1236 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1237 | * pm_runtime_init - Initialize runtime PM fields in given device object. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1238 | * @dev: Device object to initialize. |
| 1239 | */ |
| 1240 | void pm_runtime_init(struct device *dev) |
| 1241 | { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1242 | dev->power.runtime_status = RPM_SUSPENDED; |
| 1243 | dev->power.idle_notification = false; |
| 1244 | |
| 1245 | dev->power.disable_depth = 1; |
| 1246 | atomic_set(&dev->power.usage_count, 0); |
| 1247 | |
| 1248 | dev->power.runtime_error = 0; |
| 1249 | |
| 1250 | atomic_set(&dev->power.child_count, 0); |
| 1251 | pm_suspend_ignore_children(dev, false); |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1252 | dev->power.runtime_auto = true; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1253 | |
| 1254 | dev->power.request_pending = false; |
| 1255 | dev->power.request = RPM_REQ_NONE; |
| 1256 | dev->power.deferred_resume = false; |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 1257 | dev->power.accounting_timestamp = jiffies; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1258 | INIT_WORK(&dev->power.work, pm_runtime_work); |
| 1259 | |
| 1260 | dev->power.timer_expires = 0; |
| 1261 | setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn, |
| 1262 | (unsigned long)dev); |
| 1263 | |
| 1264 | init_waitqueue_head(&dev->power.wait_queue); |
| 1265 | } |
| 1266 | |
| 1267 | /** |
| 1268 | * pm_runtime_remove - Prepare for removing a device from device hierarchy. |
| 1269 | * @dev: Device object being removed from device hierarchy. |
| 1270 | */ |
| 1271 | void pm_runtime_remove(struct device *dev) |
| 1272 | { |
| 1273 | __pm_runtime_disable(dev, false); |
| 1274 | |
| 1275 | /* Change the status back to 'suspended' to match the initial status. */ |
| 1276 | if (dev->power.runtime_status == RPM_ACTIVE) |
| 1277 | pm_runtime_set_suspended(dev); |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 1278 | if (dev->power.irq_safe && dev->parent) |
| 1279 | pm_runtime_put_sync(dev->parent); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1280 | } |