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