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 | |
Ingo Molnar | 5b3cc15 | 2017-02-02 20:43:54 +0100 | [diff] [blame] | 10 | #include <linux/sched/mm.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> |
Tony Lindgren | 4990d4f | 2015-05-18 15:40:29 -0700 | [diff] [blame] | 13 | #include <linux/pm_wakeirq.h> |
Ming Lei | c3dc2f1 | 2011-09-27 22:54:41 +0200 | [diff] [blame] | 14 | #include <trace/events/rpm.h> |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 15 | |
| 16 | #include "../base.h" |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 17 | #include "power.h" |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 18 | |
Andrzej Hajda | dbcd2d7 | 2014-10-17 12:58:02 +0200 | [diff] [blame] | 19 | typedef int (*pm_callback_t)(struct device *); |
Ulf Hansson | 5f59df7 | 2014-03-01 11:56:04 +0100 | [diff] [blame] | 20 | |
Andrzej Hajda | dbcd2d7 | 2014-10-17 12:58:02 +0200 | [diff] [blame] | 21 | static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset) |
Ulf Hansson | 5f59df7 | 2014-03-01 11:56:04 +0100 | [diff] [blame] | 22 | { |
Andrzej Hajda | dbcd2d7 | 2014-10-17 12:58:02 +0200 | [diff] [blame] | 23 | pm_callback_t cb; |
| 24 | const struct dev_pm_ops *ops; |
| 25 | |
| 26 | if (dev->pm_domain) |
| 27 | ops = &dev->pm_domain->ops; |
| 28 | else if (dev->type && dev->type->pm) |
| 29 | ops = dev->type->pm; |
| 30 | else if (dev->class && dev->class->pm) |
| 31 | ops = dev->class->pm; |
| 32 | else if (dev->bus && dev->bus->pm) |
| 33 | ops = dev->bus->pm; |
| 34 | else |
| 35 | ops = NULL; |
| 36 | |
| 37 | if (ops) |
| 38 | cb = *(pm_callback_t *)((void *)ops + cb_offset); |
| 39 | else |
| 40 | cb = NULL; |
| 41 | |
| 42 | if (!cb && dev->driver && dev->driver->pm) |
| 43 | cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset); |
| 44 | |
| 45 | return cb; |
Ulf Hansson | 5f59df7 | 2014-03-01 11:56:04 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Andrzej Hajda | dbcd2d7 | 2014-10-17 12:58:02 +0200 | [diff] [blame] | 48 | #define RPM_GET_CALLBACK(dev, callback) \ |
| 49 | __rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback)) |
Ulf Hansson | 5f59df7 | 2014-03-01 11:56:04 +0100 | [diff] [blame] | 50 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 51 | static int rpm_resume(struct device *dev, int rpmflags); |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 52 | static int rpm_suspend(struct device *dev, int rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 53 | |
| 54 | /** |
Alan Stern | 4769373 | 2010-09-25 23:34:46 +0200 | [diff] [blame] | 55 | * update_pm_runtime_accounting - Update the time accounting of power states |
| 56 | * @dev: Device to update the accounting for |
| 57 | * |
| 58 | * In order to be able to have time accounting of the various power states |
| 59 | * (as used by programs such as PowerTOP to show the effectiveness of runtime |
| 60 | * PM), we need to track the time spent in each state. |
| 61 | * update_pm_runtime_accounting must be called each time before the |
| 62 | * runtime_status field is updated, to account the time in the old state |
| 63 | * correctly. |
| 64 | */ |
| 65 | void update_pm_runtime_accounting(struct device *dev) |
| 66 | { |
| 67 | unsigned long now = jiffies; |
venu byravarasu | def0c0a3 | 2011-11-03 10:12:14 +0100 | [diff] [blame] | 68 | unsigned long delta; |
Alan Stern | 4769373 | 2010-09-25 23:34:46 +0200 | [diff] [blame] | 69 | |
| 70 | delta = now - dev->power.accounting_timestamp; |
| 71 | |
Alan Stern | 4769373 | 2010-09-25 23:34:46 +0200 | [diff] [blame] | 72 | dev->power.accounting_timestamp = now; |
| 73 | |
| 74 | if (dev->power.disable_depth > 0) |
| 75 | return; |
| 76 | |
| 77 | if (dev->power.runtime_status == RPM_SUSPENDED) |
| 78 | dev->power.suspended_jiffies += delta; |
| 79 | else |
| 80 | dev->power.active_jiffies += delta; |
| 81 | } |
| 82 | |
| 83 | static void __update_runtime_status(struct device *dev, enum rpm_status status) |
| 84 | { |
| 85 | update_pm_runtime_accounting(dev); |
| 86 | dev->power.runtime_status = status; |
| 87 | } |
| 88 | |
| 89 | /** |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 90 | * pm_runtime_deactivate_timer - Deactivate given device's suspend timer. |
| 91 | * @dev: Device to handle. |
| 92 | */ |
| 93 | static void pm_runtime_deactivate_timer(struct device *dev) |
| 94 | { |
| 95 | if (dev->power.timer_expires > 0) { |
| 96 | del_timer(&dev->power.suspend_timer); |
| 97 | dev->power.timer_expires = 0; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests. |
| 103 | * @dev: Device to handle. |
| 104 | */ |
| 105 | static void pm_runtime_cancel_pending(struct device *dev) |
| 106 | { |
| 107 | pm_runtime_deactivate_timer(dev); |
| 108 | /* |
| 109 | * In case there's a request pending, make sure its work function will |
| 110 | * return without doing anything. |
| 111 | */ |
| 112 | dev->power.request = RPM_REQ_NONE; |
| 113 | } |
| 114 | |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 115 | /* |
| 116 | * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time. |
| 117 | * @dev: Device to handle. |
| 118 | * |
| 119 | * Compute the autosuspend-delay expiration time based on the device's |
| 120 | * power.last_busy time. If the delay has already expired or is disabled |
| 121 | * (negative) or the power.use_autosuspend flag isn't set, return 0. |
| 122 | * Otherwise return the expiration time in jiffies (adjusted to be nonzero). |
| 123 | * |
| 124 | * This function may be called either with or without dev->power.lock held. |
| 125 | * Either way it can be racy, since power.last_busy may be updated at any time. |
| 126 | */ |
| 127 | unsigned long pm_runtime_autosuspend_expiration(struct device *dev) |
| 128 | { |
| 129 | int autosuspend_delay; |
| 130 | long elapsed; |
| 131 | unsigned long last_busy; |
| 132 | unsigned long expires = 0; |
| 133 | |
| 134 | if (!dev->power.use_autosuspend) |
| 135 | goto out; |
| 136 | |
| 137 | autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay); |
| 138 | if (autosuspend_delay < 0) |
| 139 | goto out; |
| 140 | |
| 141 | last_busy = ACCESS_ONCE(dev->power.last_busy); |
| 142 | elapsed = jiffies - last_busy; |
| 143 | if (elapsed < 0) |
| 144 | goto out; /* jiffies has wrapped around. */ |
| 145 | |
| 146 | /* |
| 147 | * If the autosuspend_delay is >= 1 second, align the timer by rounding |
| 148 | * up to the nearest second. |
| 149 | */ |
| 150 | expires = last_busy + msecs_to_jiffies(autosuspend_delay); |
| 151 | if (autosuspend_delay >= 1000) |
| 152 | expires = round_jiffies(expires); |
| 153 | expires += !expires; |
| 154 | if (elapsed >= expires - last_busy) |
| 155 | expires = 0; /* Already expired. */ |
| 156 | |
| 157 | out: |
| 158 | return expires; |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration); |
| 161 | |
Ming Lei | e823407 | 2013-02-22 16:34:11 -0800 | [diff] [blame] | 162 | static int dev_memalloc_noio(struct device *dev, void *data) |
| 163 | { |
| 164 | return dev->power.memalloc_noio; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag. |
| 169 | * @dev: Device to handle. |
| 170 | * @enable: True for setting the flag and False for clearing the flag. |
| 171 | * |
| 172 | * Set the flag for all devices in the path from the device to the |
| 173 | * root device in the device tree if @enable is true, otherwise clear |
| 174 | * the flag for devices in the path whose siblings don't set the flag. |
| 175 | * |
| 176 | * The function should only be called by block device, or network |
| 177 | * device driver for solving the deadlock problem during runtime |
| 178 | * resume/suspend: |
| 179 | * |
| 180 | * If memory allocation with GFP_KERNEL is called inside runtime |
| 181 | * resume/suspend callback of any one of its ancestors(or the |
| 182 | * block device itself), the deadlock may be triggered inside the |
| 183 | * memory allocation since it might not complete until the block |
| 184 | * device becomes active and the involed page I/O finishes. The |
| 185 | * situation is pointed out first by Alan Stern. Network device |
| 186 | * are involved in iSCSI kind of situation. |
| 187 | * |
| 188 | * The lock of dev_hotplug_mutex is held in the function for handling |
| 189 | * hotplug race because pm_runtime_set_memalloc_noio() may be called |
| 190 | * in async probe(). |
| 191 | * |
| 192 | * The function should be called between device_add() and device_del() |
| 193 | * on the affected device(block/network device). |
| 194 | */ |
| 195 | void pm_runtime_set_memalloc_noio(struct device *dev, bool enable) |
| 196 | { |
| 197 | static DEFINE_MUTEX(dev_hotplug_mutex); |
| 198 | |
| 199 | mutex_lock(&dev_hotplug_mutex); |
| 200 | for (;;) { |
| 201 | bool enabled; |
| 202 | |
| 203 | /* hold power lock since bitfield is not SMP-safe. */ |
| 204 | spin_lock_irq(&dev->power.lock); |
| 205 | enabled = dev->power.memalloc_noio; |
| 206 | dev->power.memalloc_noio = enable; |
| 207 | spin_unlock_irq(&dev->power.lock); |
| 208 | |
| 209 | /* |
| 210 | * not need to enable ancestors any more if the device |
| 211 | * has been enabled. |
| 212 | */ |
| 213 | if (enabled && enable) |
| 214 | break; |
| 215 | |
| 216 | dev = dev->parent; |
| 217 | |
| 218 | /* |
| 219 | * clear flag of the parent device only if all the |
| 220 | * children don't set the flag because ancestor's |
| 221 | * flag was set by any one of the descendants. |
| 222 | */ |
| 223 | if (!dev || (!enable && |
| 224 | device_for_each_child(dev, NULL, |
| 225 | dev_memalloc_noio))) |
| 226 | break; |
| 227 | } |
| 228 | mutex_unlock(&dev_hotplug_mutex); |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio); |
| 231 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 232 | /** |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 233 | * rpm_check_suspend_allowed - Test whether a device may be suspended. |
| 234 | * @dev: Device to test. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 235 | */ |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 236 | static int rpm_check_suspend_allowed(struct device *dev) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 237 | { |
| 238 | int retval = 0; |
| 239 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 240 | if (dev->power.runtime_error) |
| 241 | retval = -EINVAL; |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 242 | else if (dev->power.disable_depth > 0) |
| 243 | retval = -EACCES; |
| 244 | else if (atomic_read(&dev->power.usage_count) > 0) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 245 | retval = -EAGAIN; |
Ulf Hansson | 62006c1 | 2016-10-17 20:16:58 +0200 | [diff] [blame] | 246 | else if (!dev->power.ignore_children && |
| 247 | atomic_read(&dev->power.child_count)) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 248 | retval = -EBUSY; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 249 | |
| 250 | /* Pending resume requests take precedence over suspends. */ |
| 251 | else if ((dev->power.deferred_resume |
Kevin Winchester | 78ca7c3 | 2010-10-29 15:29:55 +0200 | [diff] [blame] | 252 | && dev->power.runtime_status == RPM_SUSPENDING) |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 253 | || (dev->power.request_pending |
| 254 | && dev->power.request == RPM_REQ_RESUME)) |
| 255 | retval = -EAGAIN; |
Rafael J. Wysocki | d5919dc | 2017-10-31 18:26:15 +0100 | [diff] [blame] | 256 | else if (__dev_pm_qos_read_value(dev) < 0) |
Rafael J. Wysocki | 55d7ec4 | 2012-08-15 21:32:04 +0200 | [diff] [blame] | 257 | retval = -EPERM; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 258 | else if (dev->power.runtime_status == RPM_SUSPENDED) |
| 259 | retval = 1; |
| 260 | |
| 261 | return retval; |
| 262 | } |
| 263 | |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 264 | static int rpm_get_suppliers(struct device *dev) |
| 265 | { |
| 266 | struct device_link *link; |
| 267 | |
| 268 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) { |
| 269 | int retval; |
| 270 | |
| 271 | if (!(link->flags & DL_FLAG_PM_RUNTIME)) |
| 272 | continue; |
| 273 | |
| 274 | if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND || |
| 275 | link->rpm_active) |
| 276 | continue; |
| 277 | |
| 278 | retval = pm_runtime_get_sync(link->supplier); |
Rafael J. Wysocki | 0b028b0 | 2017-12-01 14:58:34 +0100 | [diff] [blame] | 279 | /* Ignore suppliers with disabled runtime PM. */ |
| 280 | if (retval < 0 && retval != -EACCES) { |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 281 | pm_runtime_put_noidle(link->supplier); |
| 282 | return retval; |
| 283 | } |
| 284 | link->rpm_active = true; |
| 285 | } |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static void rpm_put_suppliers(struct device *dev) |
| 290 | { |
| 291 | struct device_link *link; |
| 292 | |
| 293 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) |
| 294 | if (link->rpm_active && |
| 295 | READ_ONCE(link->status) != DL_STATE_SUPPLIER_UNBIND) { |
| 296 | pm_runtime_put(link->supplier); |
| 297 | link->rpm_active = false; |
| 298 | } |
| 299 | } |
| 300 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 301 | /** |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 302 | * __rpm_callback - Run a given runtime PM callback for a given device. |
| 303 | * @cb: Runtime PM callback to run. |
| 304 | * @dev: Device to run the callback for. |
| 305 | */ |
| 306 | static int __rpm_callback(int (*cb)(struct device *), struct device *dev) |
| 307 | __releases(&dev->power.lock) __acquires(&dev->power.lock) |
| 308 | { |
Rafael J. Wysocki | c70b3e2 | 2021-02-25 19:23:27 +0100 | [diff] [blame] | 309 | int retval, idx; |
Rafael J. Wysocki | 866f290 | 2021-03-19 15:47:25 +0100 | [diff] [blame] | 310 | bool use_links = dev->power.links_count > 0; |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 311 | |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 312 | if (dev->power.irq_safe) { |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 313 | spin_unlock(&dev->power.lock); |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 314 | } else { |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 315 | spin_unlock_irq(&dev->power.lock); |
| 316 | |
Rafael J. Wysocki | 866f290 | 2021-03-19 15:47:25 +0100 | [diff] [blame] | 317 | /* |
| 318 | * Resume suppliers if necessary. |
| 319 | * |
| 320 | * The device's runtime PM status cannot change until this |
| 321 | * routine returns, so it is safe to read the status outside of |
| 322 | * the lock. |
| 323 | */ |
| 324 | if (use_links && dev->power.runtime_status == RPM_RESUMING) { |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 325 | idx = device_links_read_lock(); |
| 326 | |
| 327 | retval = rpm_get_suppliers(dev); |
| 328 | if (retval) |
| 329 | goto fail; |
| 330 | |
| 331 | device_links_read_unlock(idx); |
| 332 | } |
| 333 | } |
| 334 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 335 | retval = cb(dev); |
| 336 | |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 337 | if (dev->power.irq_safe) { |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 338 | spin_lock(&dev->power.lock); |
Rafael J. Wysocki | 866f290 | 2021-03-19 15:47:25 +0100 | [diff] [blame] | 339 | } else { |
| 340 | /* |
| 341 | * If the device is suspending and the callback has returned |
| 342 | * success, drop the usage counters of the suppliers that have |
| 343 | * been reference counted on its resume. |
| 344 | * |
| 345 | * Do that if resume fails too. |
| 346 | */ |
| 347 | if (use_links |
| 348 | && ((dev->power.runtime_status == RPM_SUSPENDING && !retval) |
| 349 | || (dev->power.runtime_status == RPM_RESUMING && retval))) { |
| 350 | idx = device_links_read_lock(); |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 351 | |
Rafael J. Wysocki | 866f290 | 2021-03-19 15:47:25 +0100 | [diff] [blame] | 352 | fail: |
| 353 | rpm_put_suppliers(dev); |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 354 | |
Rafael J. Wysocki | 866f290 | 2021-03-19 15:47:25 +0100 | [diff] [blame] | 355 | device_links_read_unlock(idx); |
| 356 | } |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 357 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 358 | spin_lock_irq(&dev->power.lock); |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 359 | } |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 360 | |
| 361 | return retval; |
| 362 | } |
| 363 | |
| 364 | /** |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 365 | * rpm_idle - Notify device bus type if the device can be suspended. |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 366 | * @dev: Device to notify the bus type about. |
| 367 | * @rpmflags: Flag bits. |
| 368 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 369 | * 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] | 370 | * another idle notification has been started earlier, return immediately. If |
| 371 | * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise |
Ulf Hansson | d66e6db | 2013-10-15 22:25:08 +0200 | [diff] [blame] | 372 | * run the ->runtime_idle() callback directly. If the ->runtime_idle callback |
| 373 | * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag. |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 374 | * |
| 375 | * This function must be called under dev->power.lock with interrupts disabled. |
| 376 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 377 | static int rpm_idle(struct device *dev, int rpmflags) |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 378 | { |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 379 | int (*callback)(struct device *); |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 380 | int retval; |
| 381 | |
Paul E. McKenney | d7737ce | 2016-04-26 13:03:51 -0700 | [diff] [blame] | 382 | trace_rpm_idle_rcuidle(dev, rpmflags); |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 383 | retval = rpm_check_suspend_allowed(dev); |
| 384 | if (retval < 0) |
| 385 | ; /* Conditions are wrong. */ |
| 386 | |
| 387 | /* Idle notifications are allowed only in the RPM_ACTIVE state. */ |
| 388 | else if (dev->power.runtime_status != RPM_ACTIVE) |
| 389 | retval = -EAGAIN; |
| 390 | |
| 391 | /* |
| 392 | * Any pending request other than an idle notification takes |
| 393 | * precedence over us, except that the timer may be running. |
| 394 | */ |
| 395 | else if (dev->power.request_pending && |
| 396 | dev->power.request > RPM_REQ_IDLE) |
| 397 | retval = -EAGAIN; |
| 398 | |
| 399 | /* Act as though RPM_NOWAIT is always set. */ |
| 400 | else if (dev->power.idle_notification) |
| 401 | retval = -EINPROGRESS; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 402 | if (retval) |
| 403 | goto out; |
| 404 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 405 | /* Pending requests need to be canceled. */ |
| 406 | dev->power.request = RPM_REQ_NONE; |
| 407 | |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 408 | if (dev->power.no_callbacks) |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 409 | goto out; |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 410 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 411 | /* Carry out an asynchronous or a synchronous idle notification. */ |
| 412 | if (rpmflags & RPM_ASYNC) { |
| 413 | dev->power.request = RPM_REQ_IDLE; |
| 414 | if (!dev->power.request_pending) { |
| 415 | dev->power.request_pending = true; |
| 416 | queue_work(pm_wq, &dev->power.work); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 417 | } |
Paul E. McKenney | d7737ce | 2016-04-26 13:03:51 -0700 | [diff] [blame] | 418 | trace_rpm_return_int_rcuidle(dev, _THIS_IP_, 0); |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 419 | return 0; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | dev->power.idle_notification = true; |
| 423 | |
Andrzej Hajda | dbcd2d7 | 2014-10-17 12:58:02 +0200 | [diff] [blame] | 424 | callback = RPM_GET_CALLBACK(dev, runtime_idle); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 425 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 426 | if (callback) |
Rafael J. Wysocki | 45f0a85 | 2013-06-03 21:49:52 +0200 | [diff] [blame] | 427 | retval = __rpm_callback(callback, dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 428 | |
| 429 | dev->power.idle_notification = false; |
| 430 | wake_up_all(&dev->power.wait_queue); |
| 431 | |
| 432 | out: |
Paul E. McKenney | d7737ce | 2016-04-26 13:03:51 -0700 | [diff] [blame] | 433 | trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval); |
Ulf Hansson | d66e6db | 2013-10-15 22:25:08 +0200 | [diff] [blame] | 434 | return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 438 | * rpm_callback - Run a given runtime PM callback for a given device. |
| 439 | * @cb: Runtime PM callback to run. |
| 440 | * @dev: Device to run the callback for. |
| 441 | */ |
| 442 | static int rpm_callback(int (*cb)(struct device *), struct device *dev) |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 443 | { |
| 444 | int retval; |
| 445 | |
| 446 | if (!cb) |
| 447 | return -ENOSYS; |
| 448 | |
Ming Lei | db88175 | 2013-02-22 16:34:19 -0800 | [diff] [blame] | 449 | if (dev->power.memalloc_noio) { |
| 450 | unsigned int noio_flag; |
| 451 | |
| 452 | /* |
| 453 | * Deadlock might be caused if memory allocation with |
| 454 | * GFP_KERNEL happens inside runtime_suspend and |
| 455 | * runtime_resume callbacks of one block device's |
| 456 | * ancestor or the block device itself. Network |
| 457 | * device might be thought as part of iSCSI block |
| 458 | * device, so network device and its ancestor should |
| 459 | * be marked as memalloc_noio too. |
| 460 | */ |
| 461 | noio_flag = memalloc_noio_save(); |
| 462 | retval = __rpm_callback(cb, dev); |
| 463 | memalloc_noio_restore(noio_flag); |
| 464 | } else { |
| 465 | retval = __rpm_callback(cb, dev); |
| 466 | } |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 467 | |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 468 | dev->power.runtime_error = retval; |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 469 | return retval != -EACCES ? retval : -EIO; |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 473 | * rpm_suspend - Carry out runtime suspend of given device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 474 | * @dev: Device to suspend. |
Alan Stern | 3f9af05 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 475 | * @rpmflags: Flag bits. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 476 | * |
Ming Lei | 47d8f0b | 2011-10-12 11:53:32 +0800 | [diff] [blame] | 477 | * Check if the device's runtime PM status allows it to be suspended. |
| 478 | * Cancel a pending idle notification, autosuspend or suspend. If |
| 479 | * another suspend has been started earlier, either return immediately |
| 480 | * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC |
| 481 | * 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] | 482 | * otherwise run the ->runtime_suspend() callback directly. When |
| 483 | * ->runtime_suspend succeeded, if a deferred resume was requested while |
| 484 | * the callback was running then carry it out, otherwise send an idle |
| 485 | * notification for its parent (if the suspend succeeded and both |
| 486 | * 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] | 487 | * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO |
| 488 | * flag is set and the next autosuspend-delay expiration time is in the |
| 489 | * future, schedule another autosuspend attempt. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 490 | * |
| 491 | * This function must be called under dev->power.lock with interrupts disabled. |
| 492 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 493 | static int rpm_suspend(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 494 | __releases(&dev->power.lock) __acquires(&dev->power.lock) |
| 495 | { |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 496 | int (*callback)(struct device *); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 497 | struct device *parent = NULL; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 498 | int retval; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 499 | |
Paul E. McKenney | 7789357 | 2016-04-26 10:42:25 -0700 | [diff] [blame] | 500 | trace_rpm_suspend_rcuidle(dev, rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 501 | |
| 502 | repeat: |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 503 | retval = rpm_check_suspend_allowed(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 504 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 505 | if (retval < 0) |
| 506 | ; /* Conditions are wrong. */ |
| 507 | |
| 508 | /* Synchronous suspends are not allowed in the RPM_RESUMING state. */ |
| 509 | else if (dev->power.runtime_status == RPM_RESUMING && |
| 510 | !(rpmflags & RPM_ASYNC)) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 511 | retval = -EAGAIN; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 512 | if (retval) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 513 | goto out; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 514 | |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 515 | /* If the autosuspend_delay time hasn't expired yet, reschedule. */ |
| 516 | if ((rpmflags & RPM_AUTO) |
| 517 | && dev->power.runtime_status != RPM_SUSPENDING) { |
| 518 | unsigned long expires = pm_runtime_autosuspend_expiration(dev); |
| 519 | |
| 520 | if (expires != 0) { |
| 521 | /* Pending requests need to be canceled. */ |
| 522 | dev->power.request = RPM_REQ_NONE; |
| 523 | |
| 524 | /* |
| 525 | * Optimization: If the timer is already running and is |
| 526 | * set to expire at or before the autosuspend delay, |
| 527 | * avoid the overhead of resetting it. Just let it |
| 528 | * expire; pm_suspend_timer_fn() will take care of the |
| 529 | * rest. |
| 530 | */ |
| 531 | if (!(dev->power.timer_expires && time_before_eq( |
| 532 | dev->power.timer_expires, expires))) { |
| 533 | dev->power.timer_expires = expires; |
| 534 | mod_timer(&dev->power.suspend_timer, expires); |
| 535 | } |
| 536 | dev->power.timer_autosuspends = 1; |
| 537 | goto out; |
| 538 | } |
| 539 | } |
| 540 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 541 | /* Other scheduled or pending requests need to be canceled. */ |
| 542 | pm_runtime_cancel_pending(dev); |
| 543 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 544 | if (dev->power.runtime_status == RPM_SUSPENDING) { |
| 545 | DEFINE_WAIT(wait); |
| 546 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 547 | if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 548 | retval = -EINPROGRESS; |
| 549 | goto out; |
| 550 | } |
| 551 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 552 | if (dev->power.irq_safe) { |
| 553 | spin_unlock(&dev->power.lock); |
| 554 | |
| 555 | cpu_relax(); |
| 556 | |
| 557 | spin_lock(&dev->power.lock); |
| 558 | goto repeat; |
| 559 | } |
| 560 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 561 | /* Wait for the other suspend running in parallel with us. */ |
| 562 | for (;;) { |
| 563 | prepare_to_wait(&dev->power.wait_queue, &wait, |
| 564 | TASK_UNINTERRUPTIBLE); |
| 565 | if (dev->power.runtime_status != RPM_SUSPENDING) |
| 566 | break; |
| 567 | |
| 568 | spin_unlock_irq(&dev->power.lock); |
| 569 | |
| 570 | schedule(); |
| 571 | |
| 572 | spin_lock_irq(&dev->power.lock); |
| 573 | } |
| 574 | finish_wait(&dev->power.wait_queue, &wait); |
| 575 | goto repeat; |
| 576 | } |
| 577 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 578 | if (dev->power.no_callbacks) |
| 579 | goto no_callback; /* Assume success. */ |
| 580 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 581 | /* Carry out an asynchronous or a synchronous suspend. */ |
| 582 | if (rpmflags & RPM_ASYNC) { |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 583 | dev->power.request = (rpmflags & RPM_AUTO) ? |
| 584 | RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 585 | if (!dev->power.request_pending) { |
| 586 | dev->power.request_pending = true; |
| 587 | queue_work(pm_wq, &dev->power.work); |
| 588 | } |
| 589 | goto out; |
| 590 | } |
| 591 | |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 592 | __update_runtime_status(dev, RPM_SUSPENDING); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 593 | |
Andrzej Hajda | dbcd2d7 | 2014-10-17 12:58:02 +0200 | [diff] [blame] | 594 | callback = RPM_GET_CALLBACK(dev, runtime_suspend); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 595 | |
Tony Lindgren | bed5703 | 2016-12-05 16:38:16 -0800 | [diff] [blame] | 596 | dev_pm_enable_wake_irq_check(dev, true); |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 597 | retval = rpm_callback(callback, dev); |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 598 | if (retval) |
| 599 | goto fail; |
Alan Stern | 886486b | 2011-11-03 23:39:18 +0100 | [diff] [blame] | 600 | |
Chunfeng Yun | b54ef49 | 2024-04-17 12:24:53 -0400 | [diff] [blame] | 601 | dev_pm_enable_wake_irq_complete(dev); |
| 602 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 603 | no_callback: |
Ming Lei | 857b36c | 2011-10-12 22:59:33 +0200 | [diff] [blame] | 604 | __update_runtime_status(dev, RPM_SUSPENDED); |
| 605 | pm_runtime_deactivate_timer(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 606 | |
Ming Lei | 857b36c | 2011-10-12 22:59:33 +0200 | [diff] [blame] | 607 | if (dev->parent) { |
| 608 | parent = dev->parent; |
| 609 | atomic_add_unless(&parent->power.child_count, -1, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 610 | } |
| 611 | wake_up_all(&dev->power.wait_queue); |
| 612 | |
| 613 | if (dev->power.deferred_resume) { |
Rafael J. Wysocki | 58a34de | 2012-08-15 21:31:55 +0200 | [diff] [blame] | 614 | dev->power.deferred_resume = false; |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 615 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 616 | retval = -EAGAIN; |
| 617 | goto out; |
| 618 | } |
| 619 | |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 620 | /* Maybe the parent is now able to suspend. */ |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 621 | if (parent && !parent->power.ignore_children && !dev->power.irq_safe) { |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 622 | spin_unlock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 623 | |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 624 | spin_lock(&parent->power.lock); |
| 625 | rpm_idle(parent, RPM_ASYNC); |
| 626 | spin_unlock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 627 | |
Alan Stern | c3810c8 | 2011-01-25 20:50:07 +0100 | [diff] [blame] | 628 | spin_lock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | out: |
Paul E. McKenney | 7789357 | 2016-04-26 10:42:25 -0700 | [diff] [blame] | 632 | trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 633 | |
| 634 | return retval; |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 635 | |
| 636 | fail: |
Chunfeng Yun | b54ef49 | 2024-04-17 12:24:53 -0400 | [diff] [blame] | 637 | dev_pm_disable_wake_irq_check(dev, true); |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 638 | __update_runtime_status(dev, RPM_ACTIVE); |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 639 | dev->power.deferred_resume = false; |
Alan Stern | f2791d7 | 2012-03-26 22:46:52 +0200 | [diff] [blame] | 640 | wake_up_all(&dev->power.wait_queue); |
| 641 | |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 642 | if (retval == -EAGAIN || retval == -EBUSY) { |
| 643 | dev->power.runtime_error = 0; |
| 644 | |
| 645 | /* |
| 646 | * If the callback routine failed an autosuspend, and |
| 647 | * if the last_busy time has been updated so that there |
| 648 | * is a new autosuspend expiration time, automatically |
| 649 | * reschedule another autosuspend. |
| 650 | */ |
| 651 | if ((rpmflags & RPM_AUTO) && |
| 652 | pm_runtime_autosuspend_expiration(dev) != 0) |
| 653 | goto repeat; |
| 654 | } else { |
| 655 | pm_runtime_cancel_pending(dev); |
| 656 | } |
Rafael J. Wysocki | 00dc9ad | 2011-12-01 00:01:31 +0100 | [diff] [blame] | 657 | goto out; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 661 | * rpm_resume - Carry out runtime resume of given device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 662 | * @dev: Device to resume. |
Alan Stern | 3f9af05 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 663 | * @rpmflags: Flag bits. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 664 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 665 | * 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] | 666 | * any scheduled or pending requests. If another resume has been started |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 667 | * 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] | 668 | * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in |
| 669 | * parallel with this function, either tell the other process to resume after |
| 670 | * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC |
| 671 | * flag is set then queue a resume request; otherwise run the |
| 672 | * ->runtime_resume() callback directly. Queue an idle notification for the |
| 673 | * device if the resume succeeded. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 674 | * |
| 675 | * This function must be called under dev->power.lock with interrupts disabled. |
| 676 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 677 | static int rpm_resume(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 678 | __releases(&dev->power.lock) __acquires(&dev->power.lock) |
| 679 | { |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 680 | int (*callback)(struct device *); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 681 | struct device *parent = NULL; |
| 682 | int retval = 0; |
| 683 | |
Paul E. McKenney | d44c950 | 2016-04-26 13:38:55 -0700 | [diff] [blame] | 684 | trace_rpm_resume_rcuidle(dev, rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 685 | |
| 686 | repeat: |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 687 | if (dev->power.runtime_error) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 688 | retval = -EINVAL; |
Kevin Hilman | 6f3c77b | 2012-09-21 22:47:34 +0000 | [diff] [blame] | 689 | else if (dev->power.disable_depth == 1 && dev->power.is_suspended |
| 690 | && dev->power.runtime_status == RPM_ACTIVE) |
| 691 | retval = 1; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 692 | else if (dev->power.disable_depth > 0) |
Rafael J. Wysocki | 632e270 | 2011-07-01 22:29:15 +0200 | [diff] [blame] | 693 | retval = -EACCES; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 694 | if (retval) |
| 695 | goto out; |
| 696 | |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 697 | /* |
| 698 | * Other scheduled or pending requests need to be canceled. Small |
| 699 | * optimization: If an autosuspend timer is running, leave it running |
| 700 | * rather than cancelling it now only to restart it again in the near |
| 701 | * future. |
| 702 | */ |
| 703 | dev->power.request = RPM_REQ_NONE; |
| 704 | if (!dev->power.timer_autosuspends) |
| 705 | pm_runtime_deactivate_timer(dev); |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 706 | |
| 707 | if (dev->power.runtime_status == RPM_ACTIVE) { |
| 708 | retval = 1; |
| 709 | goto out; |
| 710 | } |
| 711 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 712 | if (dev->power.runtime_status == RPM_RESUMING |
| 713 | || dev->power.runtime_status == RPM_SUSPENDING) { |
| 714 | DEFINE_WAIT(wait); |
| 715 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 716 | if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 717 | if (dev->power.runtime_status == RPM_SUSPENDING) |
| 718 | dev->power.deferred_resume = true; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 719 | else |
| 720 | retval = -EINPROGRESS; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 721 | goto out; |
| 722 | } |
| 723 | |
Rafael J. Wysocki | ad3c36a | 2011-09-27 21:54:52 +0200 | [diff] [blame] | 724 | if (dev->power.irq_safe) { |
| 725 | spin_unlock(&dev->power.lock); |
| 726 | |
| 727 | cpu_relax(); |
| 728 | |
| 729 | spin_lock(&dev->power.lock); |
| 730 | goto repeat; |
| 731 | } |
| 732 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 733 | /* Wait for the operation carried out in parallel with us. */ |
| 734 | for (;;) { |
| 735 | prepare_to_wait(&dev->power.wait_queue, &wait, |
| 736 | TASK_UNINTERRUPTIBLE); |
| 737 | if (dev->power.runtime_status != RPM_RESUMING |
| 738 | && dev->power.runtime_status != RPM_SUSPENDING) |
| 739 | break; |
| 740 | |
| 741 | spin_unlock_irq(&dev->power.lock); |
| 742 | |
| 743 | schedule(); |
| 744 | |
| 745 | spin_lock_irq(&dev->power.lock); |
| 746 | } |
| 747 | finish_wait(&dev->power.wait_queue, &wait); |
| 748 | goto repeat; |
| 749 | } |
| 750 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 751 | /* |
| 752 | * See if we can skip waking up the parent. This is safe only if |
| 753 | * power.no_callbacks is set, because otherwise we don't know whether |
| 754 | * the resume will actually succeed. |
| 755 | */ |
| 756 | if (dev->power.no_callbacks && !parent && dev->parent) { |
Ming Lei | d63be5f | 2010-10-22 23:48:14 +0200 | [diff] [blame] | 757 | spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING); |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 758 | if (dev->parent->power.disable_depth > 0 |
| 759 | || dev->parent->power.ignore_children |
| 760 | || dev->parent->power.runtime_status == RPM_ACTIVE) { |
| 761 | atomic_inc(&dev->parent->power.child_count); |
| 762 | spin_unlock(&dev->parent->power.lock); |
Rafael J. Wysocki | 7f321c2 | 2012-08-15 21:31:45 +0200 | [diff] [blame] | 763 | retval = 1; |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 764 | goto no_callback; /* Assume success. */ |
| 765 | } |
| 766 | spin_unlock(&dev->parent->power.lock); |
| 767 | } |
| 768 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 769 | /* Carry out an asynchronous or a synchronous resume. */ |
| 770 | if (rpmflags & RPM_ASYNC) { |
| 771 | dev->power.request = RPM_REQ_RESUME; |
| 772 | if (!dev->power.request_pending) { |
| 773 | dev->power.request_pending = true; |
| 774 | queue_work(pm_wq, &dev->power.work); |
| 775 | } |
| 776 | retval = 0; |
| 777 | goto out; |
| 778 | } |
| 779 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 780 | if (!parent && dev->parent) { |
| 781 | /* |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 782 | * Increment the parent's usage counter and resume it if |
| 783 | * necessary. Not needed if dev is irq-safe; then the |
| 784 | * parent is permanently resumed. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 785 | */ |
| 786 | parent = dev->parent; |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 787 | if (dev->power.irq_safe) |
| 788 | goto skip_parent; |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 789 | spin_unlock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 790 | |
| 791 | pm_runtime_get_noresume(parent); |
| 792 | |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 793 | spin_lock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 794 | /* |
Ulf Hansson | 216ef0b | 2016-10-17 20:16:59 +0200 | [diff] [blame] | 795 | * Resume the parent if it has runtime PM enabled and not been |
| 796 | * set to ignore its children. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 797 | */ |
| 798 | if (!parent->power.disable_depth |
| 799 | && !parent->power.ignore_children) { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 800 | rpm_resume(parent, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 801 | if (parent->power.runtime_status != RPM_ACTIVE) |
| 802 | retval = -EBUSY; |
| 803 | } |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 804 | spin_unlock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 805 | |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 806 | spin_lock(&dev->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 807 | if (retval) |
| 808 | goto out; |
| 809 | goto repeat; |
| 810 | } |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 811 | skip_parent: |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 812 | |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 813 | if (dev->power.no_callbacks) |
| 814 | goto no_callback; /* Assume success. */ |
| 815 | |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 816 | __update_runtime_status(dev, RPM_RESUMING); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 817 | |
Andrzej Hajda | dbcd2d7 | 2014-10-17 12:58:02 +0200 | [diff] [blame] | 818 | callback = RPM_GET_CALLBACK(dev, runtime_resume); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 819 | |
Chunfeng Yun | b54ef49 | 2024-04-17 12:24:53 -0400 | [diff] [blame] | 820 | dev_pm_disable_wake_irq_check(dev, false); |
Rafael J. Wysocki | 71c6312 | 2010-10-04 22:08:01 +0200 | [diff] [blame] | 821 | retval = rpm_callback(callback, dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 822 | if (retval) { |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 823 | __update_runtime_status(dev, RPM_SUSPENDED); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 824 | pm_runtime_cancel_pending(dev); |
Tony Lindgren | bed5703 | 2016-12-05 16:38:16 -0800 | [diff] [blame] | 825 | dev_pm_enable_wake_irq_check(dev, false); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 826 | } else { |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 827 | no_callback: |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 828 | __update_runtime_status(dev, RPM_ACTIVE); |
Tony Lindgren | 56f487c | 2015-05-13 16:36:32 -0700 | [diff] [blame] | 829 | pm_runtime_mark_last_busy(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 830 | if (parent) |
| 831 | atomic_inc(&parent->power.child_count); |
| 832 | } |
| 833 | wake_up_all(&dev->power.wait_queue); |
| 834 | |
Rafael J. Wysocki | 7f321c2 | 2012-08-15 21:31:45 +0200 | [diff] [blame] | 835 | if (retval >= 0) |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 836 | rpm_idle(dev, RPM_ASYNC); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 837 | |
| 838 | out: |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 839 | if (parent && !dev->power.irq_safe) { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 840 | spin_unlock_irq(&dev->power.lock); |
| 841 | |
| 842 | pm_runtime_put(parent); |
| 843 | |
| 844 | spin_lock_irq(&dev->power.lock); |
| 845 | } |
| 846 | |
Paul E. McKenney | d44c950 | 2016-04-26 13:38:55 -0700 | [diff] [blame] | 847 | trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 848 | |
| 849 | return retval; |
| 850 | } |
| 851 | |
| 852 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 853 | * pm_runtime_work - Universal runtime PM work function. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 854 | * @work: Work structure used for scheduling the execution of this function. |
| 855 | * |
| 856 | * 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] | 857 | * is to be done and execute the appropriate runtime PM function. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 858 | */ |
| 859 | static void pm_runtime_work(struct work_struct *work) |
| 860 | { |
| 861 | struct device *dev = container_of(work, struct device, power.work); |
| 862 | enum rpm_request req; |
| 863 | |
| 864 | spin_lock_irq(&dev->power.lock); |
| 865 | |
| 866 | if (!dev->power.request_pending) |
| 867 | goto out; |
| 868 | |
| 869 | req = dev->power.request; |
| 870 | dev->power.request = RPM_REQ_NONE; |
| 871 | dev->power.request_pending = false; |
| 872 | |
| 873 | switch (req) { |
| 874 | case RPM_REQ_NONE: |
| 875 | break; |
| 876 | case RPM_REQ_IDLE: |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 877 | rpm_idle(dev, RPM_NOWAIT); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 878 | break; |
| 879 | case RPM_REQ_SUSPEND: |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 880 | rpm_suspend(dev, RPM_NOWAIT); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 881 | break; |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 882 | case RPM_REQ_AUTOSUSPEND: |
| 883 | rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO); |
| 884 | break; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 885 | case RPM_REQ_RESUME: |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 886 | rpm_resume(dev, RPM_NOWAIT); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 887 | break; |
| 888 | } |
| 889 | |
| 890 | out: |
| 891 | spin_unlock_irq(&dev->power.lock); |
| 892 | } |
| 893 | |
| 894 | /** |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 895 | * pm_suspend_timer_fn - Timer function for pm_schedule_suspend(). |
| 896 | * @data: Device pointer passed by pm_schedule_suspend(). |
| 897 | * |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 898 | * Check if the time is right and queue a suspend request. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 899 | */ |
| 900 | static void pm_suspend_timer_fn(unsigned long data) |
| 901 | { |
| 902 | struct device *dev = (struct device *)data; |
| 903 | unsigned long flags; |
| 904 | unsigned long expires; |
| 905 | |
| 906 | spin_lock_irqsave(&dev->power.lock, flags); |
| 907 | |
| 908 | expires = dev->power.timer_expires; |
| 909 | /* If 'expire' is after 'jiffies' we've been called too early. */ |
| 910 | if (expires > 0 && !time_after(expires, jiffies)) { |
| 911 | dev->power.timer_expires = 0; |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 912 | rpm_suspend(dev, dev->power.timer_autosuspends ? |
| 913 | (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * pm_schedule_suspend - Set up a timer to submit a suspend request in future. |
| 921 | * @dev: Device to suspend. |
| 922 | * @delay: Time to wait before submitting a suspend request, in milliseconds. |
| 923 | */ |
| 924 | int pm_schedule_suspend(struct device *dev, unsigned int delay) |
| 925 | { |
| 926 | unsigned long flags; |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 927 | int retval; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 928 | |
| 929 | spin_lock_irqsave(&dev->power.lock, flags); |
| 930 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 931 | if (!delay) { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 932 | retval = rpm_suspend(dev, RPM_ASYNC); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 933 | goto out; |
| 934 | } |
| 935 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 936 | retval = rpm_check_suspend_allowed(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 937 | if (retval) |
| 938 | goto out; |
| 939 | |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 940 | /* Other scheduled or pending requests need to be canceled. */ |
| 941 | pm_runtime_cancel_pending(dev); |
| 942 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 943 | dev->power.timer_expires = jiffies + msecs_to_jiffies(delay); |
Alan Stern | 1bfee5b | 2010-09-25 23:35:00 +0200 | [diff] [blame] | 944 | dev->power.timer_expires += !dev->power.timer_expires; |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 945 | dev->power.timer_autosuspends = 0; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 946 | mod_timer(&dev->power.suspend_timer, dev->power.timer_expires); |
| 947 | |
| 948 | out: |
| 949 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 950 | |
| 951 | return retval; |
| 952 | } |
| 953 | EXPORT_SYMBOL_GPL(pm_schedule_suspend); |
| 954 | |
| 955 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 956 | * __pm_runtime_idle - Entry point for runtime idle operations. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 957 | * @dev: Device to send idle notification for. |
| 958 | * @rpmflags: Flag bits. |
| 959 | * |
| 960 | * If the RPM_GET_PUT flag is set, decrement the device's usage count and |
| 961 | * return immediately if it is larger than zero. Then carry out an idle |
| 962 | * notification, either synchronous or asynchronous. |
| 963 | * |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 964 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
| 965 | * or if pm_runtime_irq_safe() has been called. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 966 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 967 | int __pm_runtime_idle(struct device *dev, int rpmflags) |
| 968 | { |
| 969 | unsigned long flags; |
| 970 | int retval; |
| 971 | |
| 972 | if (rpmflags & RPM_GET_PUT) { |
| 973 | if (!atomic_dec_and_test(&dev->power.usage_count)) |
| 974 | return 0; |
| 975 | } |
| 976 | |
Rafael J. Wysocki | a9306a6 | 2017-02-04 00:44:36 +0100 | [diff] [blame] | 977 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); |
| 978 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 979 | spin_lock_irqsave(&dev->power.lock, flags); |
| 980 | retval = rpm_idle(dev, rpmflags); |
| 981 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 982 | |
| 983 | return retval; |
| 984 | } |
| 985 | EXPORT_SYMBOL_GPL(__pm_runtime_idle); |
| 986 | |
| 987 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 988 | * __pm_runtime_suspend - Entry point for runtime put/suspend operations. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 989 | * @dev: Device to suspend. |
| 990 | * @rpmflags: Flag bits. |
| 991 | * |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 992 | * If the RPM_GET_PUT flag is set, decrement the device's usage count and |
| 993 | * return immediately if it is larger than zero. Then carry out a suspend, |
| 994 | * either synchronous or asynchronous. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 995 | * |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 996 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
| 997 | * or if pm_runtime_irq_safe() has been called. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 998 | */ |
| 999 | int __pm_runtime_suspend(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1000 | { |
| 1001 | unsigned long flags; |
| 1002 | int retval; |
| 1003 | |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 1004 | if (rpmflags & RPM_GET_PUT) { |
| 1005 | if (!atomic_dec_and_test(&dev->power.usage_count)) |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
Rafael J. Wysocki | a9306a6 | 2017-02-04 00:44:36 +0100 | [diff] [blame] | 1009 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); |
| 1010 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1011 | spin_lock_irqsave(&dev->power.lock, flags); |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1012 | retval = rpm_suspend(dev, rpmflags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1013 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 1014 | |
| 1015 | return retval; |
| 1016 | } |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1017 | EXPORT_SYMBOL_GPL(__pm_runtime_suspend); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1018 | |
| 1019 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1020 | * __pm_runtime_resume - Entry point for runtime resume operations. |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1021 | * @dev: Device to resume. |
Alan Stern | 3f9af05 | 2010-09-25 23:34:54 +0200 | [diff] [blame] | 1022 | * @rpmflags: Flag bits. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1023 | * |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1024 | * If the RPM_GET_PUT flag is set, increment the device's usage count. Then |
| 1025 | * carry out a resume, either synchronous or asynchronous. |
| 1026 | * |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 1027 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
| 1028 | * or if pm_runtime_irq_safe() has been called. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1029 | */ |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1030 | int __pm_runtime_resume(struct device *dev, int rpmflags) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1031 | { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1032 | unsigned long flags; |
Alan Stern | 1d531c1 | 2009-12-13 20:28:30 +0100 | [diff] [blame] | 1033 | int retval; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1034 | |
Rafael J. Wysocki | a9306a6 | 2017-02-04 00:44:36 +0100 | [diff] [blame] | 1035 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe && |
| 1036 | dev->power.runtime_status != RPM_ACTIVE); |
Colin Cross | 311aab7 | 2011-08-08 23:39:36 +0200 | [diff] [blame] | 1037 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1038 | if (rpmflags & RPM_GET_PUT) |
| 1039 | atomic_inc(&dev->power.usage_count); |
| 1040 | |
| 1041 | spin_lock_irqsave(&dev->power.lock, flags); |
| 1042 | retval = rpm_resume(dev, rpmflags); |
| 1043 | spin_unlock_irqrestore(&dev->power.lock, flags); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1044 | |
| 1045 | return retval; |
| 1046 | } |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1047 | EXPORT_SYMBOL_GPL(__pm_runtime_resume); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1048 | |
| 1049 | /** |
Rafael J. Wysocki | a436b6a | 2015-12-17 02:54:26 +0100 | [diff] [blame] | 1050 | * pm_runtime_get_if_in_use - Conditionally bump up the device's usage counter. |
| 1051 | * @dev: Device to handle. |
| 1052 | * |
| 1053 | * Return -EINVAL if runtime PM is disabled for the device. |
| 1054 | * |
| 1055 | * If that's not the case and if the device's runtime PM status is RPM_ACTIVE |
| 1056 | * and the runtime PM usage counter is nonzero, increment the counter and |
| 1057 | * return 1. Otherwise return 0 without changing the counter. |
| 1058 | */ |
| 1059 | int pm_runtime_get_if_in_use(struct device *dev) |
| 1060 | { |
| 1061 | unsigned long flags; |
| 1062 | int retval; |
| 1063 | |
| 1064 | spin_lock_irqsave(&dev->power.lock, flags); |
| 1065 | retval = dev->power.disable_depth > 0 ? -EINVAL : |
| 1066 | dev->power.runtime_status == RPM_ACTIVE |
| 1067 | && atomic_inc_not_zero(&dev->power.usage_count); |
| 1068 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 1069 | return retval; |
| 1070 | } |
| 1071 | EXPORT_SYMBOL_GPL(pm_runtime_get_if_in_use); |
| 1072 | |
| 1073 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1074 | * __pm_runtime_set_status - Set runtime PM status of a device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1075 | * @dev: Device to handle. |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1076 | * @status: New runtime PM status of the device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1077 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1078 | * 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] | 1079 | * different from zero, the status may be changed either to RPM_ACTIVE, or to |
| 1080 | * RPM_SUSPENDED, as long as that reflects the actual state of the device. |
| 1081 | * However, if the device has a parent and the parent is not active, and the |
| 1082 | * parent's power.ignore_children flag is unset, the device's status cannot be |
| 1083 | * set to RPM_ACTIVE, so -EBUSY is returned in that case. |
| 1084 | * |
| 1085 | * If successful, __pm_runtime_set_status() clears the power.runtime_error field |
| 1086 | * and the device parent's counter of unsuspended children is modified to |
| 1087 | * reflect the new status. If the new status is RPM_SUSPENDED, an idle |
| 1088 | * notification request for the parent is submitted. |
| 1089 | */ |
| 1090 | int __pm_runtime_set_status(struct device *dev, unsigned int status) |
| 1091 | { |
| 1092 | struct device *parent = dev->parent; |
| 1093 | unsigned long flags; |
| 1094 | bool notify_parent = false; |
| 1095 | int error = 0; |
| 1096 | |
| 1097 | if (status != RPM_ACTIVE && status != RPM_SUSPENDED) |
| 1098 | return -EINVAL; |
| 1099 | |
| 1100 | spin_lock_irqsave(&dev->power.lock, flags); |
| 1101 | |
| 1102 | if (!dev->power.runtime_error && !dev->power.disable_depth) { |
| 1103 | error = -EAGAIN; |
| 1104 | goto out; |
| 1105 | } |
| 1106 | |
| 1107 | if (dev->power.runtime_status == status) |
| 1108 | goto out_set; |
| 1109 | |
| 1110 | if (status == RPM_SUSPENDED) { |
Ulf Hansson | a8636c8 | 2016-10-17 20:17:01 +0200 | [diff] [blame] | 1111 | /* |
| 1112 | * It is invalid to suspend a device with an active child, |
| 1113 | * unless it has been set to ignore its children. |
| 1114 | */ |
| 1115 | if (!dev->power.ignore_children && |
| 1116 | atomic_read(&dev->power.child_count)) { |
| 1117 | dev_err(dev, "runtime PM trying to suspend device but active child\n"); |
| 1118 | error = -EBUSY; |
| 1119 | goto out; |
| 1120 | } |
| 1121 | |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1122 | if (parent) { |
| 1123 | atomic_add_unless(&parent->power.child_count, -1, 0); |
| 1124 | notify_parent = !parent->power.ignore_children; |
| 1125 | } |
| 1126 | goto out_set; |
| 1127 | } |
| 1128 | |
| 1129 | if (parent) { |
Rafael J. Wysocki | bab636b | 2009-12-03 20:21:21 +0100 | [diff] [blame] | 1130 | spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1131 | |
| 1132 | /* |
| 1133 | * 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] | 1134 | * not active, has runtime PM enabled and the |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1135 | * 'power.ignore_children' flag unset. |
| 1136 | */ |
| 1137 | if (!parent->power.disable_depth |
| 1138 | && !parent->power.ignore_children |
Linus Walleij | 71723f9 | 2016-06-20 11:14:26 +0200 | [diff] [blame] | 1139 | && parent->power.runtime_status != RPM_ACTIVE) { |
| 1140 | dev_err(dev, "runtime PM trying to activate child device %s but parent (%s) is not active\n", |
| 1141 | dev_name(dev), |
| 1142 | dev_name(parent)); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1143 | error = -EBUSY; |
Linus Walleij | 71723f9 | 2016-06-20 11:14:26 +0200 | [diff] [blame] | 1144 | } else if (dev->power.runtime_status == RPM_SUSPENDED) { |
Rafael J. Wysocki | 965c4ac | 2009-12-03 21:04:41 +0100 | [diff] [blame] | 1145 | atomic_inc(&parent->power.child_count); |
Linus Walleij | 71723f9 | 2016-06-20 11:14:26 +0200 | [diff] [blame] | 1146 | } |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1147 | |
Alan Stern | 862f89b | 2009-11-25 01:06:37 +0100 | [diff] [blame] | 1148 | spin_unlock(&parent->power.lock); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1149 | |
| 1150 | if (error) |
| 1151 | goto out; |
| 1152 | } |
| 1153 | |
| 1154 | out_set: |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 1155 | __update_runtime_status(dev, status); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1156 | dev->power.runtime_error = 0; |
| 1157 | out: |
| 1158 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 1159 | |
| 1160 | if (notify_parent) |
| 1161 | pm_request_idle(parent); |
| 1162 | |
| 1163 | return error; |
| 1164 | } |
| 1165 | EXPORT_SYMBOL_GPL(__pm_runtime_set_status); |
| 1166 | |
| 1167 | /** |
| 1168 | * __pm_runtime_barrier - Cancel pending requests and wait for completions. |
| 1169 | * @dev: Device to handle. |
| 1170 | * |
| 1171 | * 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] | 1172 | * runtime PM operations involving the device in progress to complete. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1173 | * |
| 1174 | * Should be called under dev->power.lock with interrupts disabled. |
| 1175 | */ |
| 1176 | static void __pm_runtime_barrier(struct device *dev) |
| 1177 | { |
| 1178 | pm_runtime_deactivate_timer(dev); |
| 1179 | |
| 1180 | if (dev->power.request_pending) { |
| 1181 | dev->power.request = RPM_REQ_NONE; |
| 1182 | spin_unlock_irq(&dev->power.lock); |
| 1183 | |
| 1184 | cancel_work_sync(&dev->power.work); |
| 1185 | |
| 1186 | spin_lock_irq(&dev->power.lock); |
| 1187 | dev->power.request_pending = false; |
| 1188 | } |
| 1189 | |
| 1190 | if (dev->power.runtime_status == RPM_SUSPENDING |
| 1191 | || dev->power.runtime_status == RPM_RESUMING |
| 1192 | || dev->power.idle_notification) { |
| 1193 | DEFINE_WAIT(wait); |
| 1194 | |
| 1195 | /* Suspend, wake-up or idle notification in progress. */ |
| 1196 | for (;;) { |
| 1197 | prepare_to_wait(&dev->power.wait_queue, &wait, |
| 1198 | TASK_UNINTERRUPTIBLE); |
| 1199 | if (dev->power.runtime_status != RPM_SUSPENDING |
| 1200 | && dev->power.runtime_status != RPM_RESUMING |
| 1201 | && !dev->power.idle_notification) |
| 1202 | break; |
| 1203 | spin_unlock_irq(&dev->power.lock); |
| 1204 | |
| 1205 | schedule(); |
| 1206 | |
| 1207 | spin_lock_irq(&dev->power.lock); |
| 1208 | } |
| 1209 | finish_wait(&dev->power.wait_queue, &wait); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * pm_runtime_barrier - Flush pending requests and wait for completions. |
| 1215 | * @dev: Device to handle. |
| 1216 | * |
| 1217 | * Prevent the device from being suspended by incrementing its usage counter and |
| 1218 | * if there's a pending resume request for the device, wake the device up. |
| 1219 | * 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] | 1220 | * 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] | 1221 | * progress to complete. |
| 1222 | * |
| 1223 | * Return value: |
| 1224 | * 1, if there was a resume request pending and the device had to be woken up, |
| 1225 | * 0, otherwise |
| 1226 | */ |
| 1227 | int pm_runtime_barrier(struct device *dev) |
| 1228 | { |
| 1229 | int retval = 0; |
| 1230 | |
| 1231 | pm_runtime_get_noresume(dev); |
| 1232 | spin_lock_irq(&dev->power.lock); |
| 1233 | |
| 1234 | if (dev->power.request_pending |
| 1235 | && dev->power.request == RPM_REQ_RESUME) { |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1236 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1237 | retval = 1; |
| 1238 | } |
| 1239 | |
| 1240 | __pm_runtime_barrier(dev); |
| 1241 | |
| 1242 | spin_unlock_irq(&dev->power.lock); |
| 1243 | pm_runtime_put_noidle(dev); |
| 1244 | |
| 1245 | return retval; |
| 1246 | } |
| 1247 | EXPORT_SYMBOL_GPL(pm_runtime_barrier); |
| 1248 | |
| 1249 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1250 | * __pm_runtime_disable - Disable runtime PM of a device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1251 | * @dev: Device to handle. |
| 1252 | * @check_resume: If set, check if there's a resume request for the device. |
| 1253 | * |
Geert Uytterhoeven | 7b60894 | 2014-03-11 11:23:40 +0100 | [diff] [blame] | 1254 | * Increment power.disable_depth for the device and if it was zero previously, |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1255 | * 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] | 1256 | * 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] | 1257 | * suspended after its runtime PM has been disabled. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1258 | * |
| 1259 | * If @check_resume is set and there's a resume request pending when |
| 1260 | * __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] | 1261 | * function will wake up the device before disabling its runtime PM. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1262 | */ |
| 1263 | void __pm_runtime_disable(struct device *dev, bool check_resume) |
| 1264 | { |
| 1265 | spin_lock_irq(&dev->power.lock); |
| 1266 | |
| 1267 | if (dev->power.disable_depth > 0) { |
| 1268 | dev->power.disable_depth++; |
| 1269 | goto out; |
| 1270 | } |
| 1271 | |
| 1272 | /* |
| 1273 | * 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] | 1274 | * 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] | 1275 | * shouldn't prevent the device from processing the I/O. |
| 1276 | */ |
| 1277 | if (check_resume && dev->power.request_pending |
| 1278 | && dev->power.request == RPM_REQ_RESUME) { |
| 1279 | /* |
| 1280 | * Prevent suspends and idle notifications from being carried |
| 1281 | * out after we have woken up the device. |
| 1282 | */ |
| 1283 | pm_runtime_get_noresume(dev); |
| 1284 | |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1285 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1286 | |
| 1287 | pm_runtime_put_noidle(dev); |
| 1288 | } |
| 1289 | |
| 1290 | if (!dev->power.disable_depth++) |
| 1291 | __pm_runtime_barrier(dev); |
| 1292 | |
| 1293 | out: |
| 1294 | spin_unlock_irq(&dev->power.lock); |
| 1295 | } |
| 1296 | EXPORT_SYMBOL_GPL(__pm_runtime_disable); |
| 1297 | |
| 1298 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1299 | * pm_runtime_enable - Enable runtime PM of a device. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1300 | * @dev: Device to handle. |
| 1301 | */ |
| 1302 | void pm_runtime_enable(struct device *dev) |
| 1303 | { |
| 1304 | unsigned long flags; |
| 1305 | |
| 1306 | spin_lock_irqsave(&dev->power.lock, flags); |
| 1307 | |
| 1308 | if (dev->power.disable_depth > 0) |
| 1309 | dev->power.disable_depth--; |
| 1310 | else |
| 1311 | dev_warn(dev, "Unbalanced %s!\n", __func__); |
| 1312 | |
| 1313 | spin_unlock_irqrestore(&dev->power.lock, flags); |
| 1314 | } |
| 1315 | EXPORT_SYMBOL_GPL(pm_runtime_enable); |
| 1316 | |
| 1317 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1318 | * pm_runtime_forbid - Block runtime PM of a device. |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1319 | * @dev: Device to handle. |
| 1320 | * |
| 1321 | * Increase the device's usage count and clear its power.runtime_auto flag, |
| 1322 | * so that it cannot be suspended at run time until pm_runtime_allow() is called |
| 1323 | * for it. |
| 1324 | */ |
| 1325 | void pm_runtime_forbid(struct device *dev) |
| 1326 | { |
| 1327 | spin_lock_irq(&dev->power.lock); |
| 1328 | if (!dev->power.runtime_auto) |
| 1329 | goto out; |
| 1330 | |
| 1331 | dev->power.runtime_auto = false; |
| 1332 | atomic_inc(&dev->power.usage_count); |
Alan Stern | 140a6c9 | 2010-09-25 23:35:07 +0200 | [diff] [blame] | 1333 | rpm_resume(dev, 0); |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1334 | |
| 1335 | out: |
| 1336 | spin_unlock_irq(&dev->power.lock); |
| 1337 | } |
| 1338 | EXPORT_SYMBOL_GPL(pm_runtime_forbid); |
| 1339 | |
| 1340 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1341 | * pm_runtime_allow - Unblock runtime PM of a device. |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1342 | * @dev: Device to handle. |
| 1343 | * |
| 1344 | * Decrease the device's usage count and set its power.runtime_auto flag. |
| 1345 | */ |
| 1346 | void pm_runtime_allow(struct device *dev) |
| 1347 | { |
| 1348 | spin_lock_irq(&dev->power.lock); |
| 1349 | if (dev->power.runtime_auto) |
| 1350 | goto out; |
| 1351 | |
| 1352 | dev->power.runtime_auto = true; |
| 1353 | if (atomic_dec_and_test(&dev->power.usage_count)) |
Rafael J. Wysocki | fe7450b | 2016-06-29 02:53:48 +0200 | [diff] [blame] | 1354 | rpm_idle(dev, RPM_AUTO | RPM_ASYNC); |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1355 | |
| 1356 | out: |
| 1357 | spin_unlock_irq(&dev->power.lock); |
| 1358 | } |
| 1359 | EXPORT_SYMBOL_GPL(pm_runtime_allow); |
| 1360 | |
| 1361 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1362 | * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device. |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 1363 | * @dev: Device to handle. |
| 1364 | * |
| 1365 | * 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] | 1366 | * device is power-managed through its parent and has no runtime PM |
| 1367 | * callbacks of its own. The runtime sysfs attributes will be removed. |
Alan Stern | 7490e44 | 2010-09-25 23:35:15 +0200 | [diff] [blame] | 1368 | */ |
| 1369 | void pm_runtime_no_callbacks(struct device *dev) |
| 1370 | { |
| 1371 | spin_lock_irq(&dev->power.lock); |
| 1372 | dev->power.no_callbacks = 1; |
| 1373 | spin_unlock_irq(&dev->power.lock); |
| 1374 | if (device_is_registered(dev)) |
| 1375 | rpm_sysfs_remove(dev); |
| 1376 | } |
| 1377 | EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks); |
| 1378 | |
| 1379 | /** |
Alan Stern | c7b61de | 2010-12-01 00:14:42 +0100 | [diff] [blame] | 1380 | * pm_runtime_irq_safe - Leave interrupts disabled during callbacks. |
| 1381 | * @dev: Device to handle |
| 1382 | * |
| 1383 | * Set the power.irq_safe flag, which tells the PM core that the |
| 1384 | * ->runtime_suspend() and ->runtime_resume() callbacks for this device should |
| 1385 | * always be invoked with the spinlock held and interrupts disabled. It also |
| 1386 | * causes the parent's usage counter to be permanently incremented, preventing |
| 1387 | * the parent from runtime suspending -- otherwise an irq-safe child might have |
| 1388 | * to wait for a non-irq-safe parent. |
| 1389 | */ |
| 1390 | void pm_runtime_irq_safe(struct device *dev) |
| 1391 | { |
| 1392 | if (dev->parent) |
| 1393 | pm_runtime_get_sync(dev->parent); |
| 1394 | spin_lock_irq(&dev->power.lock); |
| 1395 | dev->power.irq_safe = 1; |
| 1396 | spin_unlock_irq(&dev->power.lock); |
| 1397 | } |
| 1398 | EXPORT_SYMBOL_GPL(pm_runtime_irq_safe); |
| 1399 | |
| 1400 | /** |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 1401 | * update_autosuspend - Handle a change to a device's autosuspend settings. |
| 1402 | * @dev: Device to handle. |
| 1403 | * @old_delay: The former autosuspend_delay value. |
| 1404 | * @old_use: The former use_autosuspend value. |
| 1405 | * |
| 1406 | * Prevent runtime suspend if the new delay is negative and use_autosuspend is |
| 1407 | * set; otherwise allow it. Send an idle notification if suspends are allowed. |
| 1408 | * |
| 1409 | * This function must be called under dev->power.lock with interrupts disabled. |
| 1410 | */ |
| 1411 | static void update_autosuspend(struct device *dev, int old_delay, int old_use) |
| 1412 | { |
| 1413 | int delay = dev->power.autosuspend_delay; |
| 1414 | |
| 1415 | /* Should runtime suspend be prevented now? */ |
| 1416 | if (dev->power.use_autosuspend && delay < 0) { |
| 1417 | |
| 1418 | /* If it used to be allowed then prevent it. */ |
| 1419 | if (!old_use || old_delay >= 0) { |
| 1420 | atomic_inc(&dev->power.usage_count); |
| 1421 | rpm_resume(dev, 0); |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | /* Runtime suspend should be allowed now. */ |
| 1426 | else { |
| 1427 | |
| 1428 | /* If it used to be prevented then allow it. */ |
| 1429 | if (old_use && old_delay < 0) |
| 1430 | atomic_dec(&dev->power.usage_count); |
| 1431 | |
| 1432 | /* Maybe we can autosuspend now. */ |
| 1433 | rpm_idle(dev, RPM_AUTO); |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | /** |
| 1438 | * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value. |
| 1439 | * @dev: Device to handle. |
| 1440 | * @delay: Value of the new delay in milliseconds. |
| 1441 | * |
| 1442 | * 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] | 1443 | * and the power.use_autosuspend flag is set, prevent runtime suspends. If it |
| 1444 | * changes the other way, allow runtime suspends. |
Alan Stern | 15bcb91d | 2010-09-25 23:35:21 +0200 | [diff] [blame] | 1445 | */ |
| 1446 | void pm_runtime_set_autosuspend_delay(struct device *dev, int delay) |
| 1447 | { |
| 1448 | int old_delay, old_use; |
| 1449 | |
| 1450 | spin_lock_irq(&dev->power.lock); |
| 1451 | old_delay = dev->power.autosuspend_delay; |
| 1452 | old_use = dev->power.use_autosuspend; |
| 1453 | dev->power.autosuspend_delay = delay; |
| 1454 | update_autosuspend(dev, old_delay, old_use); |
| 1455 | spin_unlock_irq(&dev->power.lock); |
| 1456 | } |
| 1457 | EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay); |
| 1458 | |
| 1459 | /** |
| 1460 | * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag. |
| 1461 | * @dev: Device to handle. |
| 1462 | * @use: New value for use_autosuspend. |
| 1463 | * |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1464 | * 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] | 1465 | * suspends as needed. |
| 1466 | */ |
| 1467 | void __pm_runtime_use_autosuspend(struct device *dev, bool use) |
| 1468 | { |
| 1469 | int old_delay, old_use; |
| 1470 | |
| 1471 | spin_lock_irq(&dev->power.lock); |
| 1472 | old_delay = dev->power.autosuspend_delay; |
| 1473 | old_use = dev->power.use_autosuspend; |
| 1474 | dev->power.use_autosuspend = use; |
| 1475 | update_autosuspend(dev, old_delay, old_use); |
| 1476 | spin_unlock_irq(&dev->power.lock); |
| 1477 | } |
| 1478 | EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend); |
| 1479 | |
| 1480 | /** |
Rafael J. Wysocki | 62052ab | 2011-07-06 10:52:13 +0200 | [diff] [blame] | 1481 | * pm_runtime_init - Initialize runtime PM fields in given device object. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1482 | * @dev: Device object to initialize. |
| 1483 | */ |
| 1484 | void pm_runtime_init(struct device *dev) |
| 1485 | { |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1486 | dev->power.runtime_status = RPM_SUSPENDED; |
| 1487 | dev->power.idle_notification = false; |
| 1488 | |
| 1489 | dev->power.disable_depth = 1; |
| 1490 | atomic_set(&dev->power.usage_count, 0); |
| 1491 | |
| 1492 | dev->power.runtime_error = 0; |
| 1493 | |
| 1494 | atomic_set(&dev->power.child_count, 0); |
| 1495 | pm_suspend_ignore_children(dev, false); |
Rafael J. Wysocki | 5382363 | 2010-01-23 22:02:51 +0100 | [diff] [blame] | 1496 | dev->power.runtime_auto = true; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1497 | |
| 1498 | dev->power.request_pending = false; |
| 1499 | dev->power.request = RPM_REQ_NONE; |
| 1500 | dev->power.deferred_resume = false; |
Arjan van de Ven | 8d4b9d1 | 2010-07-19 02:01:06 +0200 | [diff] [blame] | 1501 | dev->power.accounting_timestamp = jiffies; |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1502 | INIT_WORK(&dev->power.work, pm_runtime_work); |
| 1503 | |
| 1504 | dev->power.timer_expires = 0; |
| 1505 | setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn, |
| 1506 | (unsigned long)dev); |
| 1507 | |
| 1508 | init_waitqueue_head(&dev->power.wait_queue); |
| 1509 | } |
| 1510 | |
| 1511 | /** |
Ulf Hansson | 5de85b9 | 2015-11-18 11:48:39 +0100 | [diff] [blame] | 1512 | * pm_runtime_reinit - Re-initialize runtime PM fields in given device object. |
| 1513 | * @dev: Device object to re-initialize. |
| 1514 | */ |
| 1515 | void pm_runtime_reinit(struct device *dev) |
| 1516 | { |
| 1517 | if (!pm_runtime_enabled(dev)) { |
| 1518 | if (dev->power.runtime_status == RPM_ACTIVE) |
| 1519 | pm_runtime_set_suspended(dev); |
| 1520 | if (dev->power.irq_safe) { |
| 1521 | spin_lock_irq(&dev->power.lock); |
| 1522 | dev->power.irq_safe = 0; |
| 1523 | spin_unlock_irq(&dev->power.lock); |
| 1524 | if (dev->parent) |
| 1525 | pm_runtime_put(dev->parent); |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | /** |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1531 | * pm_runtime_remove - Prepare for removing a device from device hierarchy. |
| 1532 | * @dev: Device object being removed from device hierarchy. |
| 1533 | */ |
| 1534 | void pm_runtime_remove(struct device *dev) |
| 1535 | { |
| 1536 | __pm_runtime_disable(dev, false); |
Ulf Hansson | 5de85b9 | 2015-11-18 11:48:39 +0100 | [diff] [blame] | 1537 | pm_runtime_reinit(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 1538 | } |
Ulf Hansson | 37f2041 | 2014-03-01 11:56:05 +0100 | [diff] [blame] | 1539 | |
| 1540 | /** |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 1541 | * pm_runtime_clean_up_links - Prepare links to consumers for driver removal. |
| 1542 | * @dev: Device whose driver is going to be removed. |
| 1543 | * |
| 1544 | * Check links from this device to any consumers and if any of them have active |
| 1545 | * runtime PM references to the device, drop the usage counter of the device |
| 1546 | * (once per link). |
| 1547 | * |
| 1548 | * Links with the DL_FLAG_STATELESS flag set are ignored. |
| 1549 | * |
| 1550 | * Since the device is guaranteed to be runtime-active at the point this is |
| 1551 | * called, nothing else needs to be done here. |
| 1552 | * |
| 1553 | * Moreover, this is called after device_links_busy() has returned 'false', so |
| 1554 | * the status of each link is guaranteed to be DL_STATE_SUPPLIER_UNBIND and |
| 1555 | * therefore rpm_active can't be manipulated concurrently. |
| 1556 | */ |
| 1557 | void pm_runtime_clean_up_links(struct device *dev) |
| 1558 | { |
| 1559 | struct device_link *link; |
| 1560 | int idx; |
| 1561 | |
| 1562 | idx = device_links_read_lock(); |
| 1563 | |
| 1564 | list_for_each_entry_rcu(link, &dev->links.consumers, s_node) { |
| 1565 | if (link->flags & DL_FLAG_STATELESS) |
| 1566 | continue; |
| 1567 | |
| 1568 | if (link->rpm_active) { |
| 1569 | pm_runtime_put_noidle(dev); |
| 1570 | link->rpm_active = false; |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | device_links_read_unlock(idx); |
| 1575 | } |
| 1576 | |
| 1577 | /** |
| 1578 | * pm_runtime_get_suppliers - Resume and reference-count supplier devices. |
| 1579 | * @dev: Consumer device. |
| 1580 | */ |
| 1581 | void pm_runtime_get_suppliers(struct device *dev) |
| 1582 | { |
| 1583 | struct device_link *link; |
| 1584 | int idx; |
| 1585 | |
| 1586 | idx = device_links_read_lock(); |
| 1587 | |
| 1588 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) |
| 1589 | if (link->flags & DL_FLAG_PM_RUNTIME) |
| 1590 | pm_runtime_get_sync(link->supplier); |
| 1591 | |
| 1592 | device_links_read_unlock(idx); |
| 1593 | } |
| 1594 | |
| 1595 | /** |
| 1596 | * pm_runtime_put_suppliers - Drop references to supplier devices. |
| 1597 | * @dev: Consumer device. |
| 1598 | */ |
| 1599 | void pm_runtime_put_suppliers(struct device *dev) |
| 1600 | { |
| 1601 | struct device_link *link; |
| 1602 | int idx; |
| 1603 | |
| 1604 | idx = device_links_read_lock(); |
| 1605 | |
| 1606 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) |
| 1607 | if (link->flags & DL_FLAG_PM_RUNTIME) |
| 1608 | pm_runtime_put(link->supplier); |
| 1609 | |
| 1610 | device_links_read_unlock(idx); |
| 1611 | } |
| 1612 | |
Rafael J. Wysocki | baa8809 | 2016-10-30 17:32:43 +0100 | [diff] [blame] | 1613 | void pm_runtime_new_link(struct device *dev) |
| 1614 | { |
| 1615 | spin_lock_irq(&dev->power.lock); |
| 1616 | dev->power.links_count++; |
| 1617 | spin_unlock_irq(&dev->power.lock); |
| 1618 | } |
| 1619 | |
| 1620 | void pm_runtime_drop_link(struct device *dev) |
| 1621 | { |
| 1622 | spin_lock_irq(&dev->power.lock); |
| 1623 | WARN_ON(dev->power.links_count == 0); |
| 1624 | dev->power.links_count--; |
| 1625 | spin_unlock_irq(&dev->power.lock); |
| 1626 | } |
| 1627 | |
Rafael J. Wysocki | 21d5c57 | 2016-10-30 17:32:31 +0100 | [diff] [blame] | 1628 | /** |
Ulf Hansson | 37f2041 | 2014-03-01 11:56:05 +0100 | [diff] [blame] | 1629 | * pm_runtime_force_suspend - Force a device into suspend state if needed. |
| 1630 | * @dev: Device to suspend. |
| 1631 | * |
| 1632 | * Disable runtime PM so we safely can check the device's runtime PM status and |
| 1633 | * if it is active, invoke it's .runtime_suspend callback to bring it into |
| 1634 | * suspend state. Keep runtime PM disabled to preserve the state unless we |
| 1635 | * encounter errors. |
| 1636 | * |
| 1637 | * Typically this function may be invoked from a system suspend callback to make |
| 1638 | * sure the device is put into low power state. |
| 1639 | */ |
| 1640 | int pm_runtime_force_suspend(struct device *dev) |
| 1641 | { |
| 1642 | int (*callback)(struct device *); |
| 1643 | int ret = 0; |
| 1644 | |
| 1645 | pm_runtime_disable(dev); |
Ulf Hansson | 37f2041 | 2014-03-01 11:56:05 +0100 | [diff] [blame] | 1646 | if (pm_runtime_status_suspended(dev)) |
| 1647 | return 0; |
| 1648 | |
Andrzej Hajda | dbcd2d7 | 2014-10-17 12:58:02 +0200 | [diff] [blame] | 1649 | callback = RPM_GET_CALLBACK(dev, runtime_suspend); |
Ulf Hansson | 37f2041 | 2014-03-01 11:56:05 +0100 | [diff] [blame] | 1650 | |
| 1651 | if (!callback) { |
| 1652 | ret = -ENOSYS; |
| 1653 | goto err; |
| 1654 | } |
| 1655 | |
| 1656 | ret = callback(dev); |
| 1657 | if (ret) |
| 1658 | goto err; |
| 1659 | |
Ulf Hansson | 1d9174f | 2016-10-13 16:58:54 +0200 | [diff] [blame] | 1660 | /* |
| 1661 | * Increase the runtime PM usage count for the device's parent, in case |
| 1662 | * when we find the device being used when system suspend was invoked. |
| 1663 | * This informs pm_runtime_force_resume() to resume the parent |
| 1664 | * immediately, which is needed to be able to resume its children, |
| 1665 | * when not deferring the resume to be managed via runtime PM. |
| 1666 | */ |
| 1667 | if (dev->parent && atomic_read(&dev->power.usage_count) > 1) |
| 1668 | pm_runtime_get_noresume(dev->parent); |
| 1669 | |
Ulf Hansson | 37f2041 | 2014-03-01 11:56:05 +0100 | [diff] [blame] | 1670 | pm_runtime_set_suspended(dev); |
| 1671 | return 0; |
| 1672 | err: |
| 1673 | pm_runtime_enable(dev); |
| 1674 | return ret; |
| 1675 | } |
| 1676 | EXPORT_SYMBOL_GPL(pm_runtime_force_suspend); |
| 1677 | |
| 1678 | /** |
Ulf Hansson | 1d9174f | 2016-10-13 16:58:54 +0200 | [diff] [blame] | 1679 | * pm_runtime_force_resume - Force a device into resume state if needed. |
Ulf Hansson | 37f2041 | 2014-03-01 11:56:05 +0100 | [diff] [blame] | 1680 | * @dev: Device to resume. |
| 1681 | * |
| 1682 | * Prior invoking this function we expect the user to have brought the device |
| 1683 | * into low power state by a call to pm_runtime_force_suspend(). Here we reverse |
Ulf Hansson | 1d9174f | 2016-10-13 16:58:54 +0200 | [diff] [blame] | 1684 | * those actions and brings the device into full power, if it is expected to be |
| 1685 | * used on system resume. To distinguish that, we check whether the runtime PM |
| 1686 | * usage count is greater than 1 (the PM core increases the usage count in the |
| 1687 | * system PM prepare phase), as that indicates a real user (such as a subsystem, |
| 1688 | * driver, userspace, etc.) is using it. If that is the case, the device is |
| 1689 | * expected to be used on system resume as well, so then we resume it. In the |
| 1690 | * other case, we defer the resume to be managed via runtime PM. |
Ulf Hansson | 37f2041 | 2014-03-01 11:56:05 +0100 | [diff] [blame] | 1691 | * |
Ulf Hansson | 1d9174f | 2016-10-13 16:58:54 +0200 | [diff] [blame] | 1692 | * Typically this function may be invoked from a system resume callback. |
Ulf Hansson | 37f2041 | 2014-03-01 11:56:05 +0100 | [diff] [blame] | 1693 | */ |
| 1694 | int pm_runtime_force_resume(struct device *dev) |
| 1695 | { |
| 1696 | int (*callback)(struct device *); |
| 1697 | int ret = 0; |
| 1698 | |
Andrzej Hajda | dbcd2d7 | 2014-10-17 12:58:02 +0200 | [diff] [blame] | 1699 | callback = RPM_GET_CALLBACK(dev, runtime_resume); |
Ulf Hansson | 37f2041 | 2014-03-01 11:56:05 +0100 | [diff] [blame] | 1700 | |
| 1701 | if (!callback) { |
| 1702 | ret = -ENOSYS; |
| 1703 | goto out; |
| 1704 | } |
| 1705 | |
Ulf Hansson | 9f5b527 | 2016-05-30 11:33:12 +0200 | [diff] [blame] | 1706 | if (!pm_runtime_status_suspended(dev)) |
| 1707 | goto out; |
| 1708 | |
Ulf Hansson | 1d9174f | 2016-10-13 16:58:54 +0200 | [diff] [blame] | 1709 | /* |
| 1710 | * Decrease the parent's runtime PM usage count, if we increased it |
| 1711 | * during system suspend in pm_runtime_force_suspend(). |
| 1712 | */ |
| 1713 | if (atomic_read(&dev->power.usage_count) > 1) { |
| 1714 | if (dev->parent) |
| 1715 | pm_runtime_put_noidle(dev->parent); |
| 1716 | } else { |
| 1717 | goto out; |
| 1718 | } |
| 1719 | |
Ulf Hansson | 0ae3aee | 2016-04-08 13:10:23 +0200 | [diff] [blame] | 1720 | ret = pm_runtime_set_active(dev); |
Ulf Hansson | 37f2041 | 2014-03-01 11:56:05 +0100 | [diff] [blame] | 1721 | if (ret) |
| 1722 | goto out; |
| 1723 | |
Ulf Hansson | 0ae3aee | 2016-04-08 13:10:23 +0200 | [diff] [blame] | 1724 | ret = callback(dev); |
| 1725 | if (ret) { |
| 1726 | pm_runtime_set_suspended(dev); |
| 1727 | goto out; |
| 1728 | } |
| 1729 | |
Ulf Hansson | 37f2041 | 2014-03-01 11:56:05 +0100 | [diff] [blame] | 1730 | pm_runtime_mark_last_busy(dev); |
| 1731 | out: |
| 1732 | pm_runtime_enable(dev); |
| 1733 | return ret; |
| 1734 | } |
| 1735 | EXPORT_SYMBOL_GPL(pm_runtime_force_resume); |