Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/base/power/main.c - Where the driver meets power management. |
| 3 | * |
| 4 | * Copyright (c) 2003 Patrick Mochel |
| 5 | * Copyright (c) 2003 Open Source Development Lab |
| 6 | * |
| 7 | * This file is released under the GPLv2 |
| 8 | * |
| 9 | * |
| 10 | * The driver model core calls device_pm_add() when a device is registered. |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 11 | * This will initialize the embedded device_pm_info object in the device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * and add it to the list of power-controlled devices. sysfs entries for |
| 13 | * controlling device power management will also be added. |
| 14 | * |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 15 | * A separate list is used for keeping track of power info, because the power |
| 16 | * domain dependencies may differ from the ancestral dependencies that the |
| 17 | * subsystem list maintains. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/device.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 21 | #include <linux/kallsyms.h> |
Paul Gortmaker | 1b6bc32 | 2011-05-27 07:12:15 -0400 | [diff] [blame] | 22 | #include <linux/export.h> |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 24 | #include <linux/pm.h> |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 25 | #include <linux/pm_runtime.h> |
Zhonghui Fu | 431d452 | 2015-03-18 15:54:27 +0100 | [diff] [blame] | 26 | #include <linux/pm-trace.h> |
Tony Lindgren | 4990d4f | 2015-05-18 15:40:29 -0700 | [diff] [blame^] | 27 | #include <linux/pm_wakeirq.h> |
Rafael J. Wysocki | 2ed8d2b3 | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 28 | #include <linux/interrupt.h> |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 29 | #include <linux/sched.h> |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 30 | #include <linux/async.h> |
Rafael J. Wysocki | 1e75227 | 2010-12-03 22:58:05 +0100 | [diff] [blame] | 31 | #include <linux/suspend.h> |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 32 | #include <trace/events/power.h> |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 33 | #include <linux/cpufreq.h> |
Preeti U Murthy | 8651f97 | 2012-07-09 10:12:56 +0200 | [diff] [blame] | 34 | #include <linux/cpuidle.h> |
Benoit Goby | 70fea60 | 2013-10-17 10:48:46 -0700 | [diff] [blame] | 35 | #include <linux/timer.h> |
| 36 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 37 | #include "../base.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include "power.h" |
| 39 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 40 | typedef int (*pm_callback_t)(struct device *); |
| 41 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 42 | /* |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 43 | * The entries in the dpm_list list are in a depth first order, simply |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 44 | * because children are guaranteed to be discovered after parents, and |
| 45 | * are inserted at the back of the list on discovery. |
| 46 | * |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 47 | * Since device_pm_add() may be called with a device lock held, |
| 48 | * we must never try to acquire a device lock while holding |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 49 | * dpm_list_mutex. |
| 50 | */ |
| 51 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 52 | LIST_HEAD(dpm_list); |
Sachin Kamat | 7664e96 | 2012-07-17 22:38:08 +0200 | [diff] [blame] | 53 | static LIST_HEAD(dpm_prepared_list); |
| 54 | static LIST_HEAD(dpm_suspended_list); |
| 55 | static LIST_HEAD(dpm_late_early_list); |
| 56 | static LIST_HEAD(dpm_noirq_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 58 | struct suspend_stats suspend_stats; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 59 | static DEFINE_MUTEX(dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 60 | static pm_message_t pm_transition; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 62 | static int async_error; |
| 63 | |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 64 | static char *pm_verb(int event) |
| 65 | { |
| 66 | switch (event) { |
| 67 | case PM_EVENT_SUSPEND: |
| 68 | return "suspend"; |
| 69 | case PM_EVENT_RESUME: |
| 70 | return "resume"; |
| 71 | case PM_EVENT_FREEZE: |
| 72 | return "freeze"; |
| 73 | case PM_EVENT_QUIESCE: |
| 74 | return "quiesce"; |
| 75 | case PM_EVENT_HIBERNATE: |
| 76 | return "hibernate"; |
| 77 | case PM_EVENT_THAW: |
| 78 | return "thaw"; |
| 79 | case PM_EVENT_RESTORE: |
| 80 | return "restore"; |
| 81 | case PM_EVENT_RECOVER: |
| 82 | return "recover"; |
| 83 | default: |
| 84 | return "(unknown PM event)"; |
| 85 | } |
| 86 | } |
| 87 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 88 | /** |
Rafael J. Wysocki | e91c11b | 2012-08-06 01:44:28 +0200 | [diff] [blame] | 89 | * device_pm_sleep_init - Initialize system suspend-related device fields. |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 90 | * @dev: Device object being initialized. |
| 91 | */ |
Rafael J. Wysocki | e91c11b | 2012-08-06 01:44:28 +0200 | [diff] [blame] | 92 | void device_pm_sleep_init(struct device *dev) |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 93 | { |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 94 | dev->power.is_prepared = false; |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 95 | dev->power.is_suspended = false; |
Liu, Chuansheng | 3d2699b | 2014-02-18 10:28:44 +0800 | [diff] [blame] | 96 | dev->power.is_noirq_suspended = false; |
| 97 | dev->power.is_late_suspended = false; |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 98 | init_completion(&dev->power.completion); |
Colin Cross | 152e1d5 | 2010-09-03 01:24:07 +0200 | [diff] [blame] | 99 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 100 | dev->power.wakeup = NULL; |
Rafael J. Wysocki | 22110fa | 2011-04-26 11:33:09 +0200 | [diff] [blame] | 101 | INIT_LIST_HEAD(&dev->power.entry); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 105 | * device_pm_lock - Lock the list of active devices used by the PM core. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 106 | */ |
| 107 | void device_pm_lock(void) |
| 108 | { |
| 109 | mutex_lock(&dpm_list_mtx); |
| 110 | } |
| 111 | |
| 112 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 113 | * device_pm_unlock - Unlock the list of active devices used by the PM core. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 114 | */ |
| 115 | void device_pm_unlock(void) |
| 116 | { |
| 117 | mutex_unlock(&dpm_list_mtx); |
| 118 | } |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 119 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 120 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 121 | * device_pm_add - Add a device to the PM core's list of active devices. |
| 122 | * @dev: Device to add to the list. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 123 | */ |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 124 | void device_pm_add(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | pr_debug("PM: Adding info for %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 127 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 128 | mutex_lock(&dpm_list_mtx); |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 129 | if (dev->parent && dev->parent->power.is_prepared) |
Rafael J. Wysocki | b64959e | 2010-12-16 17:11:45 +0100 | [diff] [blame] | 130 | dev_warn(dev, "parent %s should not be sleeping\n", |
| 131 | dev_name(dev->parent)); |
Alan Stern | 3b98aea | 2008-08-07 13:06:12 -0400 | [diff] [blame] | 132 | list_add_tail(&dev->power.entry, &dpm_list); |
Rafael J. Wysocki | 1a9a915 | 2011-09-29 22:29:44 +0200 | [diff] [blame] | 133 | mutex_unlock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 136 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 137 | * device_pm_remove - Remove a device from the PM core's list of active devices. |
| 138 | * @dev: Device to be removed from the list. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 139 | */ |
Rafael J. Wysocki | 9cddad7 | 2007-06-13 15:53:34 +0200 | [diff] [blame] | 140 | void device_pm_remove(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | { |
| 142 | pr_debug("PM: Removing info for %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 143 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 144 | complete_all(&dev->power.completion); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 145 | mutex_lock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | list_del_init(&dev->power.entry); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 147 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 074037e | 2010-09-22 22:09:10 +0200 | [diff] [blame] | 148 | device_wakeup_disable(dev); |
Rafael J. Wysocki | 5e928f7 | 2009-08-18 23:38:32 +0200 | [diff] [blame] | 149 | pm_runtime_remove(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 152 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 153 | * device_pm_move_before - Move device in the PM core's list of active devices. |
| 154 | * @deva: Device to move in dpm_list. |
| 155 | * @devb: Device @deva should come before. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 156 | */ |
| 157 | void device_pm_move_before(struct device *deva, struct device *devb) |
| 158 | { |
| 159 | pr_debug("PM: Moving %s:%s before %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 160 | deva->bus ? deva->bus->name : "No Bus", dev_name(deva), |
| 161 | devb->bus ? devb->bus->name : "No Bus", dev_name(devb)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 162 | /* Delete deva from dpm_list and reinsert before devb. */ |
| 163 | list_move_tail(&deva->power.entry, &devb->power.entry); |
| 164 | } |
| 165 | |
| 166 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 167 | * device_pm_move_after - Move device in the PM core's list of active devices. |
| 168 | * @deva: Device to move in dpm_list. |
| 169 | * @devb: Device @deva should come after. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 170 | */ |
| 171 | void device_pm_move_after(struct device *deva, struct device *devb) |
| 172 | { |
| 173 | pr_debug("PM: Moving %s:%s after %s:%s\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 174 | deva->bus ? deva->bus->name : "No Bus", dev_name(deva), |
| 175 | devb->bus ? devb->bus->name : "No Bus", dev_name(devb)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 176 | /* Delete deva from dpm_list and reinsert after devb. */ |
| 177 | list_move(&deva->power.entry, &devb->power.entry); |
| 178 | } |
| 179 | |
| 180 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 181 | * device_pm_move_last - Move device to end of the PM core's list of devices. |
| 182 | * @dev: Device to move in dpm_list. |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 183 | */ |
| 184 | void device_pm_move_last(struct device *dev) |
| 185 | { |
| 186 | pr_debug("PM: Moving %s:%s to end of list\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 187 | dev->bus ? dev->bus->name : "No Bus", dev_name(dev)); |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 188 | list_move_tail(&dev->power.entry, &dpm_list); |
| 189 | } |
| 190 | |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 191 | static ktime_t initcall_debug_start(struct device *dev) |
| 192 | { |
| 193 | ktime_t calltime = ktime_set(0, 0); |
| 194 | |
Rafael J. Wysocki | b2df1d4 | 2012-06-21 00:19:33 +0200 | [diff] [blame] | 195 | if (pm_print_times_enabled) { |
Rafael J. Wysocki | 0c6aebe | 2011-12-03 00:23:43 +0100 | [diff] [blame] | 196 | pr_info("calling %s+ @ %i, parent: %s\n", |
| 197 | dev_name(dev), task_pid_nr(current), |
| 198 | dev->parent ? dev_name(dev->parent) : "none"); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 199 | calltime = ktime_get(); |
| 200 | } |
| 201 | |
| 202 | return calltime; |
| 203 | } |
| 204 | |
| 205 | static void initcall_debug_report(struct device *dev, ktime_t calltime, |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 206 | int error, pm_message_t state, char *info) |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 207 | { |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 208 | ktime_t rettime; |
| 209 | s64 nsecs; |
| 210 | |
| 211 | rettime = ktime_get(); |
| 212 | nsecs = (s64) ktime_to_ns(ktime_sub(rettime, calltime)); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 213 | |
Rafael J. Wysocki | b2df1d4 | 2012-06-21 00:19:33 +0200 | [diff] [blame] | 214 | if (pm_print_times_enabled) { |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 215 | pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev), |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 216 | error, (unsigned long long)nsecs >> 10); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Cornelia Huck | ffa6a70 | 2009-03-04 12:44:00 +0100 | [diff] [blame] | 220 | /** |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 221 | * dpm_wait - Wait for a PM operation to complete. |
| 222 | * @dev: Device to wait for. |
| 223 | * @async: If unset, wait only if the device's power.async_suspend flag is set. |
| 224 | */ |
| 225 | static void dpm_wait(struct device *dev, bool async) |
| 226 | { |
| 227 | if (!dev) |
| 228 | return; |
| 229 | |
Rafael J. Wysocki | 0e06b4a | 2010-01-23 22:25:15 +0100 | [diff] [blame] | 230 | if (async || (pm_async_enabled && dev->power.async_suspend)) |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 231 | wait_for_completion(&dev->power.completion); |
| 232 | } |
| 233 | |
| 234 | static int dpm_wait_fn(struct device *dev, void *async_ptr) |
| 235 | { |
| 236 | dpm_wait(dev, *((bool *)async_ptr)); |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static void dpm_wait_for_children(struct device *dev, bool async) |
| 241 | { |
| 242 | device_for_each_child(dev, &async, dpm_wait_fn); |
| 243 | } |
| 244 | |
| 245 | /** |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 246 | * pm_op - Return the PM operation appropriate for given PM event. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 247 | * @ops: PM operations to choose from. |
| 248 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 249 | */ |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 250 | static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 251 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 252 | switch (state.event) { |
| 253 | #ifdef CONFIG_SUSPEND |
| 254 | case PM_EVENT_SUSPEND: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 255 | return ops->suspend; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 256 | case PM_EVENT_RESUME: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 257 | return ops->resume; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 258 | #endif /* CONFIG_SUSPEND */ |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 259 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 260 | case PM_EVENT_FREEZE: |
| 261 | case PM_EVENT_QUIESCE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 262 | return ops->freeze; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 263 | case PM_EVENT_HIBERNATE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 264 | return ops->poweroff; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 265 | case PM_EVENT_THAW: |
| 266 | case PM_EVENT_RECOVER: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 267 | return ops->thaw; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 268 | break; |
| 269 | case PM_EVENT_RESTORE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 270 | return ops->restore; |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 271 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 272 | } |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 273 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 274 | return NULL; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /** |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 278 | * pm_late_early_op - Return the PM operation appropriate for given PM event. |
| 279 | * @ops: PM operations to choose from. |
| 280 | * @state: PM transition of the system being carried out. |
| 281 | * |
| 282 | * Runtime PM is disabled for @dev while this function is being executed. |
| 283 | */ |
| 284 | static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops, |
| 285 | pm_message_t state) |
| 286 | { |
| 287 | switch (state.event) { |
| 288 | #ifdef CONFIG_SUSPEND |
| 289 | case PM_EVENT_SUSPEND: |
| 290 | return ops->suspend_late; |
| 291 | case PM_EVENT_RESUME: |
| 292 | return ops->resume_early; |
| 293 | #endif /* CONFIG_SUSPEND */ |
| 294 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
| 295 | case PM_EVENT_FREEZE: |
| 296 | case PM_EVENT_QUIESCE: |
| 297 | return ops->freeze_late; |
| 298 | case PM_EVENT_HIBERNATE: |
| 299 | return ops->poweroff_late; |
| 300 | case PM_EVENT_THAW: |
| 301 | case PM_EVENT_RECOVER: |
| 302 | return ops->thaw_early; |
| 303 | case PM_EVENT_RESTORE: |
| 304 | return ops->restore_early; |
| 305 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
| 306 | } |
| 307 | |
| 308 | return NULL; |
| 309 | } |
| 310 | |
| 311 | /** |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 312 | * pm_noirq_op - Return the PM operation appropriate for given PM event. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 313 | * @ops: PM operations to choose from. |
| 314 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 315 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 316 | * The driver of @dev will not receive interrupts while this function is being |
| 317 | * executed. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 318 | */ |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 319 | static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 320 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 321 | switch (state.event) { |
| 322 | #ifdef CONFIG_SUSPEND |
| 323 | case PM_EVENT_SUSPEND: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 324 | return ops->suspend_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 325 | case PM_EVENT_RESUME: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 326 | return ops->resume_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 327 | #endif /* CONFIG_SUSPEND */ |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 328 | #ifdef CONFIG_HIBERNATE_CALLBACKS |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 329 | case PM_EVENT_FREEZE: |
| 330 | case PM_EVENT_QUIESCE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 331 | return ops->freeze_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 332 | case PM_EVENT_HIBERNATE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 333 | return ops->poweroff_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 334 | case PM_EVENT_THAW: |
| 335 | case PM_EVENT_RECOVER: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 336 | return ops->thaw_noirq; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 337 | case PM_EVENT_RESTORE: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 338 | return ops->restore_noirq; |
Rafael J. Wysocki | 1f112ce | 2011-04-11 22:54:42 +0200 | [diff] [blame] | 339 | #endif /* CONFIG_HIBERNATE_CALLBACKS */ |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 340 | } |
Arjan van de Ven | f251177 | 2009-12-13 20:29:01 +0100 | [diff] [blame] | 341 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 342 | return NULL; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 343 | } |
| 344 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 345 | static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info) |
| 346 | { |
| 347 | dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event), |
| 348 | ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ? |
| 349 | ", may wakeup" : ""); |
| 350 | } |
| 351 | |
| 352 | static void pm_dev_err(struct device *dev, pm_message_t state, char *info, |
| 353 | int error) |
| 354 | { |
| 355 | printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 356 | dev_name(dev), pm_verb(state.event), info, error); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 359 | static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info) |
| 360 | { |
| 361 | ktime_t calltime; |
Kevin Cernekee | 0702d9ee | 2010-09-20 22:32:10 +0200 | [diff] [blame] | 362 | u64 usecs64; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 363 | int usecs; |
| 364 | |
| 365 | calltime = ktime_get(); |
| 366 | usecs64 = ktime_to_ns(ktime_sub(calltime, starttime)); |
| 367 | do_div(usecs64, NSEC_PER_USEC); |
| 368 | usecs = usecs64; |
| 369 | if (usecs == 0) |
| 370 | usecs = 1; |
| 371 | pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n", |
| 372 | info ?: "", info ? " " : "", pm_verb(state.event), |
| 373 | usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC); |
| 374 | } |
| 375 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 376 | static int dpm_run_callback(pm_callback_t cb, struct device *dev, |
| 377 | pm_message_t state, char *info) |
| 378 | { |
| 379 | ktime_t calltime; |
| 380 | int error; |
| 381 | |
| 382 | if (!cb) |
| 383 | return 0; |
| 384 | |
| 385 | calltime = initcall_debug_start(dev); |
| 386 | |
| 387 | pm_dev_dbg(dev, state, info); |
Todd E Brandt | e8bca47 | 2014-06-10 07:31:22 -0700 | [diff] [blame] | 388 | trace_device_pm_callback_start(dev, info, state.event); |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 389 | error = cb(dev); |
Todd E Brandt | e8bca47 | 2014-06-10 07:31:22 -0700 | [diff] [blame] | 390 | trace_device_pm_callback_end(dev, error); |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 391 | suspend_report_result(cb, error); |
| 392 | |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 393 | initcall_debug_report(dev, calltime, error, state, info); |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 394 | |
| 395 | return error; |
| 396 | } |
| 397 | |
Benoit Goby | 70fea60 | 2013-10-17 10:48:46 -0700 | [diff] [blame] | 398 | #ifdef CONFIG_DPM_WATCHDOG |
| 399 | struct dpm_watchdog { |
| 400 | struct device *dev; |
| 401 | struct task_struct *tsk; |
| 402 | struct timer_list timer; |
| 403 | }; |
| 404 | |
| 405 | #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \ |
| 406 | struct dpm_watchdog wd |
| 407 | |
| 408 | /** |
| 409 | * dpm_watchdog_handler - Driver suspend / resume watchdog handler. |
| 410 | * @data: Watchdog object address. |
| 411 | * |
| 412 | * Called when a driver has timed out suspending or resuming. |
| 413 | * There's not much we can do here to recover so panic() to |
| 414 | * capture a crash-dump in pstore. |
| 415 | */ |
| 416 | static void dpm_watchdog_handler(unsigned long data) |
| 417 | { |
| 418 | struct dpm_watchdog *wd = (void *)data; |
| 419 | |
| 420 | dev_emerg(wd->dev, "**** DPM device timeout ****\n"); |
| 421 | show_stack(wd->tsk, NULL); |
| 422 | panic("%s %s: unrecoverable failure\n", |
| 423 | dev_driver_string(wd->dev), dev_name(wd->dev)); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * dpm_watchdog_set - Enable pm watchdog for given device. |
| 428 | * @wd: Watchdog. Must be allocated on the stack. |
| 429 | * @dev: Device to handle. |
| 430 | */ |
| 431 | static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev) |
| 432 | { |
| 433 | struct timer_list *timer = &wd->timer; |
| 434 | |
| 435 | wd->dev = dev; |
| 436 | wd->tsk = current; |
| 437 | |
| 438 | init_timer_on_stack(timer); |
| 439 | /* use same timeout value for both suspend and resume */ |
| 440 | timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT; |
| 441 | timer->function = dpm_watchdog_handler; |
| 442 | timer->data = (unsigned long)wd; |
| 443 | add_timer(timer); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * dpm_watchdog_clear - Disable suspend/resume watchdog. |
| 448 | * @wd: Watchdog to disable. |
| 449 | */ |
| 450 | static void dpm_watchdog_clear(struct dpm_watchdog *wd) |
| 451 | { |
| 452 | struct timer_list *timer = &wd->timer; |
| 453 | |
| 454 | del_timer_sync(timer); |
| 455 | destroy_timer_on_stack(timer); |
| 456 | } |
| 457 | #else |
| 458 | #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) |
| 459 | #define dpm_watchdog_set(x, y) |
| 460 | #define dpm_watchdog_clear(x) |
| 461 | #endif |
| 462 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 463 | /*------------------------- Resume routines -------------------------*/ |
| 464 | |
| 465 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 466 | * device_resume_noirq - Execute an "early resume" callback for given device. |
| 467 | * @dev: Device to handle. |
| 468 | * @state: PM transition of the system being carried out. |
Randy Dunlap | 58c256a | 2014-07-27 16:17:15 -0700 | [diff] [blame] | 469 | * @async: If true, the device is being resumed asynchronously. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 470 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 471 | * The driver of @dev will not receive interrupts while this function is being |
| 472 | * executed. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 473 | */ |
Liu, Chuansheng | 76569fa | 2014-02-18 10:28:45 +0800 | [diff] [blame] | 474 | static int device_resume_noirq(struct device *dev, pm_message_t state, bool async) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 475 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 476 | pm_callback_t callback = NULL; |
| 477 | char *info = NULL; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 478 | int error = 0; |
| 479 | |
| 480 | TRACE_DEVICE(dev); |
| 481 | TRACE_RESUME(0); |
| 482 | |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 483 | if (dev->power.syscore || dev->power.direct_complete) |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 484 | goto Out; |
| 485 | |
Liu, Chuansheng | 3d2699b | 2014-02-18 10:28:44 +0800 | [diff] [blame] | 486 | if (!dev->power.is_noirq_suspended) |
| 487 | goto Out; |
| 488 | |
Liu, Chuansheng | 76569fa | 2014-02-18 10:28:45 +0800 | [diff] [blame] | 489 | dpm_wait(dev->parent, async); |
| 490 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 491 | if (dev->pm_domain) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 492 | info = "noirq power domain "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 493 | callback = pm_noirq_op(&dev->pm_domain->ops, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 494 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 495 | info = "noirq type "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 496 | callback = pm_noirq_op(dev->type->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 497 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 498 | info = "noirq class "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 499 | callback = pm_noirq_op(dev->class->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 500 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 501 | info = "noirq bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 502 | callback = pm_noirq_op(dev->bus->pm, state); |
Dominik Brodowski | e7176a3 | 2010-03-15 21:43:11 +0100 | [diff] [blame] | 503 | } |
| 504 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 505 | if (!callback && dev->driver && dev->driver->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 506 | info = "noirq driver "; |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 507 | callback = pm_noirq_op(dev->driver->pm, state); |
| 508 | } |
| 509 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 510 | error = dpm_run_callback(callback, dev, state, info); |
Liu, Chuansheng | 3d2699b | 2014-02-18 10:28:44 +0800 | [diff] [blame] | 511 | dev->power.is_noirq_suspended = false; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 512 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 513 | Out: |
Liu, Chuansheng | 76569fa | 2014-02-18 10:28:45 +0800 | [diff] [blame] | 514 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 515 | TRACE_RESUME(error); |
| 516 | return error; |
| 517 | } |
| 518 | |
Liu, Chuansheng | 76569fa | 2014-02-18 10:28:45 +0800 | [diff] [blame] | 519 | static bool is_async(struct device *dev) |
| 520 | { |
| 521 | return dev->power.async_suspend && pm_async_enabled |
| 522 | && !pm_trace_is_enabled(); |
| 523 | } |
| 524 | |
| 525 | static void async_resume_noirq(void *data, async_cookie_t cookie) |
| 526 | { |
| 527 | struct device *dev = (struct device *)data; |
| 528 | int error; |
| 529 | |
| 530 | error = device_resume_noirq(dev, pm_transition, true); |
| 531 | if (error) |
| 532 | pm_dev_err(dev, pm_transition, " async", error); |
| 533 | |
| 534 | put_device(dev); |
| 535 | } |
| 536 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 537 | /** |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 538 | * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 539 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 540 | * |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 541 | * Call the "noirq" resume handlers for all devices in dpm_noirq_list and |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 542 | * enable device drivers to receive interrupts. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 543 | */ |
Rafael J. Wysocki | 2a8a8ce | 2014-09-30 02:21:34 +0200 | [diff] [blame] | 544 | void dpm_resume_noirq(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 545 | { |
Liu, Chuansheng | 76569fa | 2014-02-18 10:28:45 +0800 | [diff] [blame] | 546 | struct device *dev; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 547 | ktime_t starttime = ktime_get(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 548 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 549 | trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true); |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 550 | mutex_lock(&dpm_list_mtx); |
Liu, Chuansheng | 76569fa | 2014-02-18 10:28:45 +0800 | [diff] [blame] | 551 | pm_transition = state; |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 552 | |
Liu, Chuansheng | 76569fa | 2014-02-18 10:28:45 +0800 | [diff] [blame] | 553 | /* |
| 554 | * Advanced the async threads upfront, |
| 555 | * in case the starting of async threads is |
| 556 | * delayed by non-async resuming devices. |
| 557 | */ |
| 558 | list_for_each_entry(dev, &dpm_noirq_list, power.entry) { |
| 559 | reinit_completion(&dev->power.completion); |
| 560 | if (is_async(dev)) { |
| 561 | get_device(dev); |
| 562 | async_schedule(async_resume_noirq, dev); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | while (!list_empty(&dpm_noirq_list)) { |
| 567 | dev = to_device(dpm_noirq_list.next); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 568 | get_device(dev); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 569 | list_move_tail(&dev->power.entry, &dpm_late_early_list); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 570 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 571 | |
Liu, Chuansheng | 76569fa | 2014-02-18 10:28:45 +0800 | [diff] [blame] | 572 | if (!is_async(dev)) { |
| 573 | int error; |
| 574 | |
| 575 | error = device_resume_noirq(dev, state, false); |
| 576 | if (error) { |
| 577 | suspend_stats.failed_resume_noirq++; |
| 578 | dpm_save_failed_step(SUSPEND_RESUME_NOIRQ); |
| 579 | dpm_save_failed_dev(dev_name(dev)); |
| 580 | pm_dev_err(dev, state, " noirq", error); |
| 581 | } |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | mutex_lock(&dpm_list_mtx); |
| 585 | put_device(dev); |
| 586 | } |
| 587 | mutex_unlock(&dpm_list_mtx); |
Liu, Chuansheng | 76569fa | 2014-02-18 10:28:45 +0800 | [diff] [blame] | 588 | async_synchronize_full(); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 589 | dpm_show_time(starttime, state, "noirq"); |
| 590 | resume_device_irqs(); |
Tony Lindgren | 4990d4f | 2015-05-18 15:40:29 -0700 | [diff] [blame^] | 591 | device_wakeup_disarm_wake_irqs(); |
Preeti U Murthy | 8651f97 | 2012-07-09 10:12:56 +0200 | [diff] [blame] | 592 | cpuidle_resume(); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 593 | trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /** |
| 597 | * device_resume_early - Execute an "early resume" callback for given device. |
| 598 | * @dev: Device to handle. |
| 599 | * @state: PM transition of the system being carried out. |
Randy Dunlap | 58c256a | 2014-07-27 16:17:15 -0700 | [diff] [blame] | 600 | * @async: If true, the device is being resumed asynchronously. |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 601 | * |
| 602 | * Runtime PM is disabled for @dev while this function is being executed. |
| 603 | */ |
Liu, Chuansheng | 9e5e791 | 2014-02-18 10:28:46 +0800 | [diff] [blame] | 604 | static int device_resume_early(struct device *dev, pm_message_t state, bool async) |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 605 | { |
| 606 | pm_callback_t callback = NULL; |
| 607 | char *info = NULL; |
| 608 | int error = 0; |
| 609 | |
| 610 | TRACE_DEVICE(dev); |
| 611 | TRACE_RESUME(0); |
| 612 | |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 613 | if (dev->power.syscore || dev->power.direct_complete) |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 614 | goto Out; |
| 615 | |
Liu, Chuansheng | 3d2699b | 2014-02-18 10:28:44 +0800 | [diff] [blame] | 616 | if (!dev->power.is_late_suspended) |
| 617 | goto Out; |
| 618 | |
Liu, Chuansheng | 9e5e791 | 2014-02-18 10:28:46 +0800 | [diff] [blame] | 619 | dpm_wait(dev->parent, async); |
| 620 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 621 | if (dev->pm_domain) { |
| 622 | info = "early power domain "; |
| 623 | callback = pm_late_early_op(&dev->pm_domain->ops, state); |
| 624 | } else if (dev->type && dev->type->pm) { |
| 625 | info = "early type "; |
| 626 | callback = pm_late_early_op(dev->type->pm, state); |
| 627 | } else if (dev->class && dev->class->pm) { |
| 628 | info = "early class "; |
| 629 | callback = pm_late_early_op(dev->class->pm, state); |
| 630 | } else if (dev->bus && dev->bus->pm) { |
| 631 | info = "early bus "; |
| 632 | callback = pm_late_early_op(dev->bus->pm, state); |
| 633 | } |
| 634 | |
| 635 | if (!callback && dev->driver && dev->driver->pm) { |
| 636 | info = "early driver "; |
| 637 | callback = pm_late_early_op(dev->driver->pm, state); |
| 638 | } |
| 639 | |
| 640 | error = dpm_run_callback(callback, dev, state, info); |
Liu, Chuansheng | 3d2699b | 2014-02-18 10:28:44 +0800 | [diff] [blame] | 641 | dev->power.is_late_suspended = false; |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 642 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 643 | Out: |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 644 | TRACE_RESUME(error); |
Rafael J. Wysocki | 9f6d8f6 | 2012-12-22 23:59:01 +0100 | [diff] [blame] | 645 | |
| 646 | pm_runtime_enable(dev); |
Liu, Chuansheng | 9e5e791 | 2014-02-18 10:28:46 +0800 | [diff] [blame] | 647 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 648 | return error; |
| 649 | } |
| 650 | |
Liu, Chuansheng | 9e5e791 | 2014-02-18 10:28:46 +0800 | [diff] [blame] | 651 | static void async_resume_early(void *data, async_cookie_t cookie) |
| 652 | { |
| 653 | struct device *dev = (struct device *)data; |
| 654 | int error; |
| 655 | |
| 656 | error = device_resume_early(dev, pm_transition, true); |
| 657 | if (error) |
| 658 | pm_dev_err(dev, pm_transition, " async", error); |
| 659 | |
| 660 | put_device(dev); |
| 661 | } |
| 662 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 663 | /** |
| 664 | * dpm_resume_early - Execute "early resume" callbacks for all devices. |
| 665 | * @state: PM transition of the system being carried out. |
| 666 | */ |
Rafael J. Wysocki | 2a8a8ce | 2014-09-30 02:21:34 +0200 | [diff] [blame] | 667 | void dpm_resume_early(pm_message_t state) |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 668 | { |
Liu, Chuansheng | 9e5e791 | 2014-02-18 10:28:46 +0800 | [diff] [blame] | 669 | struct device *dev; |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 670 | ktime_t starttime = ktime_get(); |
| 671 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 672 | trace_suspend_resume(TPS("dpm_resume_early"), state.event, true); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 673 | mutex_lock(&dpm_list_mtx); |
Liu, Chuansheng | 9e5e791 | 2014-02-18 10:28:46 +0800 | [diff] [blame] | 674 | pm_transition = state; |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 675 | |
Liu, Chuansheng | 9e5e791 | 2014-02-18 10:28:46 +0800 | [diff] [blame] | 676 | /* |
| 677 | * Advanced the async threads upfront, |
| 678 | * in case the starting of async threads is |
| 679 | * delayed by non-async resuming devices. |
| 680 | */ |
| 681 | list_for_each_entry(dev, &dpm_late_early_list, power.entry) { |
| 682 | reinit_completion(&dev->power.completion); |
| 683 | if (is_async(dev)) { |
| 684 | get_device(dev); |
| 685 | async_schedule(async_resume_early, dev); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | while (!list_empty(&dpm_late_early_list)) { |
| 690 | dev = to_device(dpm_late_early_list.next); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 691 | get_device(dev); |
| 692 | list_move_tail(&dev->power.entry, &dpm_suspended_list); |
| 693 | mutex_unlock(&dpm_list_mtx); |
| 694 | |
Liu, Chuansheng | 9e5e791 | 2014-02-18 10:28:46 +0800 | [diff] [blame] | 695 | if (!is_async(dev)) { |
| 696 | int error; |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 697 | |
Liu, Chuansheng | 9e5e791 | 2014-02-18 10:28:46 +0800 | [diff] [blame] | 698 | error = device_resume_early(dev, state, false); |
| 699 | if (error) { |
| 700 | suspend_stats.failed_resume_early++; |
| 701 | dpm_save_failed_step(SUSPEND_RESUME_EARLY); |
| 702 | dpm_save_failed_dev(dev_name(dev)); |
| 703 | pm_dev_err(dev, state, " early", error); |
| 704 | } |
| 705 | } |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 706 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 707 | put_device(dev); |
| 708 | } |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 709 | mutex_unlock(&dpm_list_mtx); |
Liu, Chuansheng | 9e5e791 | 2014-02-18 10:28:46 +0800 | [diff] [blame] | 710 | async_synchronize_full(); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 711 | dpm_show_time(starttime, state, "early"); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 712 | trace_suspend_resume(TPS("dpm_resume_early"), state.event, false); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 713 | } |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 714 | |
| 715 | /** |
| 716 | * dpm_resume_start - Execute "noirq" and "early" device callbacks. |
| 717 | * @state: PM transition of the system being carried out. |
| 718 | */ |
| 719 | void dpm_resume_start(pm_message_t state) |
| 720 | { |
| 721 | dpm_resume_noirq(state); |
| 722 | dpm_resume_early(state); |
| 723 | } |
| 724 | EXPORT_SYMBOL_GPL(dpm_resume_start); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 725 | |
| 726 | /** |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 727 | * device_resume - Execute "resume" callbacks for given device. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 728 | * @dev: Device to handle. |
| 729 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 730 | * @async: If true, the device is being resumed asynchronously. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 731 | */ |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 732 | static int device_resume(struct device *dev, pm_message_t state, bool async) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 733 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 734 | pm_callback_t callback = NULL; |
| 735 | char *info = NULL; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 736 | int error = 0; |
Benoit Goby | 70fea60 | 2013-10-17 10:48:46 -0700 | [diff] [blame] | 737 | DECLARE_DPM_WATCHDOG_ON_STACK(wd); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 738 | |
| 739 | TRACE_DEVICE(dev); |
| 740 | TRACE_RESUME(0); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 741 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 742 | if (dev->power.syscore) |
| 743 | goto Complete; |
| 744 | |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 745 | if (dev->power.direct_complete) { |
| 746 | /* Match the pm_runtime_disable() in __device_suspend(). */ |
| 747 | pm_runtime_enable(dev); |
| 748 | goto Complete; |
| 749 | } |
| 750 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 751 | dpm_wait(dev->parent, async); |
Benoit Goby | 70fea60 | 2013-10-17 10:48:46 -0700 | [diff] [blame] | 752 | dpm_watchdog_set(&wd, dev); |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 753 | device_lock(dev); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 754 | |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 755 | /* |
| 756 | * This is a fib. But we'll allow new children to be added below |
| 757 | * a resumed device, even if the device hasn't been completed yet. |
| 758 | */ |
| 759 | dev->power.is_prepared = false; |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 760 | |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 761 | if (!dev->power.is_suspended) |
| 762 | goto Unlock; |
| 763 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 764 | if (dev->pm_domain) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 765 | info = "power domain "; |
| 766 | callback = pm_op(&dev->pm_domain->ops, state); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 767 | goto Driver; |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 768 | } |
| 769 | |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 770 | if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 771 | info = "type "; |
| 772 | callback = pm_op(dev->type->pm, state); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 773 | goto Driver; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | if (dev->class) { |
| 777 | if (dev->class->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 778 | info = "class "; |
| 779 | callback = pm_op(dev->class->pm, state); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 780 | goto Driver; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 781 | } else if (dev->class->resume) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 782 | info = "legacy class "; |
| 783 | callback = dev->class->resume; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 784 | goto End; |
| 785 | } |
| 786 | } |
| 787 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 788 | if (dev->bus) { |
| 789 | if (dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 790 | info = "bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 791 | callback = pm_op(dev->bus->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 792 | } else if (dev->bus->resume) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 793 | info = "legacy bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 794 | callback = dev->bus->resume; |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 795 | goto End; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 796 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 797 | } |
| 798 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 799 | Driver: |
| 800 | if (!callback && dev->driver && dev->driver->pm) { |
| 801 | info = "driver "; |
| 802 | callback = pm_op(dev->driver->pm, state); |
| 803 | } |
| 804 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 805 | End: |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 806 | error = dpm_run_callback(callback, dev, state, info); |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 807 | dev->power.is_suspended = false; |
| 808 | |
| 809 | Unlock: |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 810 | device_unlock(dev); |
Benoit Goby | 70fea60 | 2013-10-17 10:48:46 -0700 | [diff] [blame] | 811 | dpm_watchdog_clear(&wd); |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 812 | |
| 813 | Complete: |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 814 | complete_all(&dev->power.completion); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 815 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 816 | TRACE_RESUME(error); |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 817 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 818 | return error; |
| 819 | } |
| 820 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 821 | static void async_resume(void *data, async_cookie_t cookie) |
| 822 | { |
| 823 | struct device *dev = (struct device *)data; |
| 824 | int error; |
| 825 | |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 826 | error = device_resume(dev, pm_transition, true); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 827 | if (error) |
| 828 | pm_dev_err(dev, pm_transition, " async", error); |
| 829 | put_device(dev); |
| 830 | } |
| 831 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 832 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 833 | * dpm_resume - Execute "resume" callbacks for non-sysdev devices. |
| 834 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 835 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 836 | * Execute the appropriate "resume" callback for all devices whose status |
| 837 | * indicates that they are suspended. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 838 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 839 | void dpm_resume(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 840 | { |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 841 | struct device *dev; |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 842 | ktime_t starttime = ktime_get(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 843 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 844 | trace_suspend_resume(TPS("dpm_resume"), state.event, true); |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 845 | might_sleep(); |
| 846 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 847 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 848 | pm_transition = state; |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 849 | async_error = 0; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 850 | |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 851 | list_for_each_entry(dev, &dpm_suspended_list, power.entry) { |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 852 | reinit_completion(&dev->power.completion); |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 853 | if (is_async(dev)) { |
| 854 | get_device(dev); |
| 855 | async_schedule(async_resume, dev); |
| 856 | } |
| 857 | } |
| 858 | |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 859 | while (!list_empty(&dpm_suspended_list)) { |
| 860 | dev = to_device(dpm_suspended_list.next); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 861 | get_device(dev); |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 862 | if (!is_async(dev)) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 863 | int error; |
| 864 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 865 | mutex_unlock(&dpm_list_mtx); |
| 866 | |
Rafael J. Wysocki | 97df8c1 | 2010-01-23 22:25:31 +0100 | [diff] [blame] | 867 | error = device_resume(dev, state, false); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 868 | if (error) { |
| 869 | suspend_stats.failed_resume++; |
| 870 | dpm_save_failed_step(SUSPEND_RESUME); |
| 871 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 872 | pm_dev_err(dev, state, "", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 873 | } |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 874 | |
| 875 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 876 | } |
| 877 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 878 | list_move_tail(&dev->power.entry, &dpm_prepared_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 879 | put_device(dev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 880 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 881 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 882 | async_synchronize_full(); |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 883 | dpm_show_time(starttime, state, NULL); |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 884 | |
| 885 | cpufreq_resume(); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 886 | trace_suspend_resume(TPS("dpm_resume"), state.event, false); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 890 | * device_complete - Complete a PM transition for given device. |
| 891 | * @dev: Device to handle. |
| 892 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 893 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 894 | static void device_complete(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 895 | { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 896 | void (*callback)(struct device *) = NULL; |
| 897 | char *info = NULL; |
| 898 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 899 | if (dev->power.syscore) |
| 900 | return; |
| 901 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 902 | device_lock(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 903 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 904 | if (dev->pm_domain) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 905 | info = "completing power domain "; |
| 906 | callback = dev->pm_domain->ops.complete; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 907 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 908 | info = "completing type "; |
| 909 | callback = dev->type->pm->complete; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 910 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 911 | info = "completing class "; |
| 912 | callback = dev->class->pm->complete; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 913 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 914 | info = "completing bus "; |
| 915 | callback = dev->bus->pm->complete; |
| 916 | } |
| 917 | |
| 918 | if (!callback && dev->driver && dev->driver->pm) { |
| 919 | info = "completing driver "; |
| 920 | callback = dev->driver->pm->complete; |
| 921 | } |
| 922 | |
| 923 | if (callback) { |
| 924 | pm_dev_dbg(dev, state, info); |
Todd E Brandt | e8bca47 | 2014-06-10 07:31:22 -0700 | [diff] [blame] | 925 | trace_device_pm_callback_start(dev, info, state.event); |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 926 | callback(dev); |
Todd E Brandt | e8bca47 | 2014-06-10 07:31:22 -0700 | [diff] [blame] | 927 | trace_device_pm_callback_end(dev, 0); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 928 | } |
| 929 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 930 | device_unlock(dev); |
Alan Stern | 88d2613 | 2012-09-19 21:59:02 +0200 | [diff] [blame] | 931 | |
Ulf Hansson | af93933 | 2013-04-12 09:41:06 +0000 | [diff] [blame] | 932 | pm_runtime_put(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 936 | * dpm_complete - Complete a PM transition for all non-sysdev devices. |
| 937 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 938 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 939 | * Execute the ->complete() callbacks for all devices whose PM status is not |
| 940 | * DPM_ON (this allows new devices to be registered). |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 941 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 942 | void dpm_complete(pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 943 | { |
| 944 | struct list_head list; |
| 945 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 946 | trace_suspend_resume(TPS("dpm_complete"), state.event, true); |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 947 | might_sleep(); |
| 948 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 949 | INIT_LIST_HEAD(&list); |
| 950 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 951 | while (!list_empty(&dpm_prepared_list)) { |
| 952 | struct device *dev = to_device(dpm_prepared_list.prev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 953 | |
| 954 | get_device(dev); |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 955 | dev->power.is_prepared = false; |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 956 | list_move(&dev->power.entry, &list); |
| 957 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 958 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 959 | device_complete(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 960 | |
Rafael J. Wysocki | 5b219a5 | 2010-12-16 00:51:08 +0100 | [diff] [blame] | 961 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 962 | put_device(dev); |
| 963 | } |
| 964 | list_splice(&list, &dpm_list); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 965 | mutex_unlock(&dpm_list_mtx); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 966 | trace_suspend_resume(TPS("dpm_complete"), state.event, false); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 967 | } |
| 968 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 969 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 970 | * dpm_resume_end - Execute "resume" callbacks and complete system transition. |
| 971 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 972 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 973 | * Execute "resume" callbacks for all devices and complete the PM transition of |
| 974 | * the system. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 975 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 976 | void dpm_resume_end(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 977 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 978 | dpm_resume(state); |
| 979 | dpm_complete(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 980 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 981 | EXPORT_SYMBOL_GPL(dpm_resume_end); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 982 | |
| 983 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 984 | /*------------------------- Suspend routines -------------------------*/ |
| 985 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 986 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 987 | * resume_event - Return a "resume" message for given "suspend" sleep state. |
| 988 | * @sleep_state: PM message representing a sleep state. |
| 989 | * |
| 990 | * Return a PM message representing the resume event corresponding to given |
| 991 | * sleep state. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 992 | */ |
| 993 | static pm_message_t resume_event(pm_message_t sleep_state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 994 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 995 | switch (sleep_state.event) { |
| 996 | case PM_EVENT_SUSPEND: |
| 997 | return PMSG_RESUME; |
| 998 | case PM_EVENT_FREEZE: |
| 999 | case PM_EVENT_QUIESCE: |
| 1000 | return PMSG_RECOVER; |
| 1001 | case PM_EVENT_HIBERNATE: |
| 1002 | return PMSG_RESTORE; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1003 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1004 | return PMSG_ON; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1008 | * device_suspend_noirq - Execute a "late suspend" callback for given device. |
| 1009 | * @dev: Device to handle. |
| 1010 | * @state: PM transition of the system being carried out. |
Randy Dunlap | 58c256a | 2014-07-27 16:17:15 -0700 | [diff] [blame] | 1011 | * @async: If true, the device is being suspended asynchronously. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1012 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1013 | * The driver of @dev will not receive interrupts while this function is being |
| 1014 | * executed. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1015 | */ |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1016 | static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1017 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1018 | pm_callback_t callback = NULL; |
| 1019 | char *info = NULL; |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1020 | int error = 0; |
| 1021 | |
Zhonghui Fu | 431d452 | 2015-03-18 15:54:27 +0100 | [diff] [blame] | 1022 | TRACE_DEVICE(dev); |
| 1023 | TRACE_SUSPEND(0); |
| 1024 | |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1025 | if (async_error) |
| 1026 | goto Complete; |
| 1027 | |
| 1028 | if (pm_wakeup_pending()) { |
| 1029 | async_error = -EBUSY; |
| 1030 | goto Complete; |
| 1031 | } |
Dominik Brodowski | e7176a3 | 2010-03-15 21:43:11 +0100 | [diff] [blame] | 1032 | |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 1033 | if (dev->power.syscore || dev->power.direct_complete) |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1034 | goto Complete; |
| 1035 | |
| 1036 | dpm_wait_for_children(dev, async); |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 1037 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 1038 | if (dev->pm_domain) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1039 | info = "noirq power domain "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1040 | callback = pm_noirq_op(&dev->pm_domain->ops, state); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1041 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1042 | info = "noirq type "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1043 | callback = pm_noirq_op(dev->type->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1044 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1045 | info = "noirq class "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1046 | callback = pm_noirq_op(dev->class->pm, state); |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1047 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1048 | info = "noirq bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1049 | callback = pm_noirq_op(dev->bus->pm, state); |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 1050 | } |
| 1051 | |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1052 | if (!callback && dev->driver && dev->driver->pm) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1053 | info = "noirq driver "; |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1054 | callback = pm_noirq_op(dev->driver->pm, state); |
| 1055 | } |
| 1056 | |
Liu, Chuansheng | 3d2699b | 2014-02-18 10:28:44 +0800 | [diff] [blame] | 1057 | error = dpm_run_callback(callback, dev, state, info); |
| 1058 | if (!error) |
| 1059 | dev->power.is_noirq_suspended = true; |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1060 | else |
| 1061 | async_error = error; |
Liu, Chuansheng | 3d2699b | 2014-02-18 10:28:44 +0800 | [diff] [blame] | 1062 | |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1063 | Complete: |
| 1064 | complete_all(&dev->power.completion); |
Zhonghui Fu | 431d452 | 2015-03-18 15:54:27 +0100 | [diff] [blame] | 1065 | TRACE_SUSPEND(error); |
Liu, Chuansheng | 3d2699b | 2014-02-18 10:28:44 +0800 | [diff] [blame] | 1066 | return error; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1067 | } |
| 1068 | |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1069 | static void async_suspend_noirq(void *data, async_cookie_t cookie) |
| 1070 | { |
| 1071 | struct device *dev = (struct device *)data; |
| 1072 | int error; |
| 1073 | |
| 1074 | error = __device_suspend_noirq(dev, pm_transition, true); |
| 1075 | if (error) { |
| 1076 | dpm_save_failed_dev(dev_name(dev)); |
| 1077 | pm_dev_err(dev, pm_transition, " async", error); |
| 1078 | } |
| 1079 | |
| 1080 | put_device(dev); |
| 1081 | } |
| 1082 | |
| 1083 | static int device_suspend_noirq(struct device *dev) |
| 1084 | { |
| 1085 | reinit_completion(&dev->power.completion); |
| 1086 | |
Zhonghui Fu | 431d452 | 2015-03-18 15:54:27 +0100 | [diff] [blame] | 1087 | if (is_async(dev)) { |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1088 | get_device(dev); |
| 1089 | async_schedule(async_suspend_noirq, dev); |
| 1090 | return 0; |
| 1091 | } |
| 1092 | return __device_suspend_noirq(dev, pm_transition, false); |
| 1093 | } |
| 1094 | |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1095 | /** |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1096 | * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices. |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1097 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1098 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1099 | * Prevent device drivers from receiving interrupts and call the "noirq" suspend |
| 1100 | * handlers for all non-sysdev devices. |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1101 | */ |
Rafael J. Wysocki | 2a8a8ce | 2014-09-30 02:21:34 +0200 | [diff] [blame] | 1102 | int dpm_suspend_noirq(pm_message_t state) |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1103 | { |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 1104 | ktime_t starttime = ktime_get(); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1105 | int error = 0; |
| 1106 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 1107 | trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true); |
Preeti U Murthy | 8651f97 | 2012-07-09 10:12:56 +0200 | [diff] [blame] | 1108 | cpuidle_pause(); |
Tony Lindgren | 4990d4f | 2015-05-18 15:40:29 -0700 | [diff] [blame^] | 1109 | device_wakeup_arm_wake_irqs(); |
Rafael J. Wysocki | 2ed8d2b3 | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 1110 | suspend_device_irqs(); |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 1111 | mutex_lock(&dpm_list_mtx); |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1112 | pm_transition = state; |
| 1113 | async_error = 0; |
| 1114 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1115 | while (!list_empty(&dpm_late_early_list)) { |
| 1116 | struct device *dev = to_device(dpm_late_early_list.prev); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 1117 | |
| 1118 | get_device(dev); |
| 1119 | mutex_unlock(&dpm_list_mtx); |
| 1120 | |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1121 | error = device_suspend_noirq(dev); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 1122 | |
| 1123 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1124 | if (error) { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1125 | pm_dev_err(dev, state, " noirq", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1126 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 1127 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1128 | break; |
| 1129 | } |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 1130 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 1131 | list_move(&dev->power.entry, &dpm_noirq_list); |
Rafael J. Wysocki | d08a5ac | 2010-11-11 01:50:53 +0100 | [diff] [blame] | 1132 | put_device(dev); |
Rafael J. Wysocki | 52d136c | 2012-04-29 22:52:19 +0200 | [diff] [blame] | 1133 | |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1134 | if (async_error) |
Rafael J. Wysocki | 52d136c | 2012-04-29 22:52:19 +0200 | [diff] [blame] | 1135 | break; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1136 | } |
Rafael J. Wysocki | 32bdfac | 2009-05-24 21:15:07 +0200 | [diff] [blame] | 1137 | mutex_unlock(&dpm_list_mtx); |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1138 | async_synchronize_full(); |
| 1139 | if (!error) |
| 1140 | error = async_error; |
| 1141 | |
| 1142 | if (error) { |
| 1143 | suspend_stats.failed_suspend_noirq++; |
| 1144 | dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ); |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1145 | dpm_resume_noirq(resume_event(state)); |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1146 | } else { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1147 | dpm_show_time(starttime, state, "noirq"); |
Liu, Chuansheng | 28b6fd6 | 2014-02-18 10:28:47 +0800 | [diff] [blame] | 1148 | } |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 1149 | trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1150 | return error; |
| 1151 | } |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1152 | |
| 1153 | /** |
| 1154 | * device_suspend_late - Execute a "late suspend" callback for given device. |
| 1155 | * @dev: Device to handle. |
| 1156 | * @state: PM transition of the system being carried out. |
Randy Dunlap | 58c256a | 2014-07-27 16:17:15 -0700 | [diff] [blame] | 1157 | * @async: If true, the device is being suspended asynchronously. |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1158 | * |
| 1159 | * Runtime PM is disabled for @dev while this function is being executed. |
| 1160 | */ |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1161 | static int __device_suspend_late(struct device *dev, pm_message_t state, bool async) |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1162 | { |
| 1163 | pm_callback_t callback = NULL; |
| 1164 | char *info = NULL; |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1165 | int error = 0; |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1166 | |
Zhonghui Fu | 431d452 | 2015-03-18 15:54:27 +0100 | [diff] [blame] | 1167 | TRACE_DEVICE(dev); |
| 1168 | TRACE_SUSPEND(0); |
| 1169 | |
Rafael J. Wysocki | 9f6d8f6 | 2012-12-22 23:59:01 +0100 | [diff] [blame] | 1170 | __pm_runtime_disable(dev, false); |
| 1171 | |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1172 | if (async_error) |
| 1173 | goto Complete; |
| 1174 | |
| 1175 | if (pm_wakeup_pending()) { |
| 1176 | async_error = -EBUSY; |
| 1177 | goto Complete; |
| 1178 | } |
| 1179 | |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 1180 | if (dev->power.syscore || dev->power.direct_complete) |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1181 | goto Complete; |
| 1182 | |
| 1183 | dpm_wait_for_children(dev, async); |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 1184 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1185 | if (dev->pm_domain) { |
| 1186 | info = "late power domain "; |
| 1187 | callback = pm_late_early_op(&dev->pm_domain->ops, state); |
| 1188 | } else if (dev->type && dev->type->pm) { |
| 1189 | info = "late type "; |
| 1190 | callback = pm_late_early_op(dev->type->pm, state); |
| 1191 | } else if (dev->class && dev->class->pm) { |
| 1192 | info = "late class "; |
| 1193 | callback = pm_late_early_op(dev->class->pm, state); |
| 1194 | } else if (dev->bus && dev->bus->pm) { |
| 1195 | info = "late bus "; |
| 1196 | callback = pm_late_early_op(dev->bus->pm, state); |
| 1197 | } |
| 1198 | |
| 1199 | if (!callback && dev->driver && dev->driver->pm) { |
| 1200 | info = "late driver "; |
| 1201 | callback = pm_late_early_op(dev->driver->pm, state); |
| 1202 | } |
| 1203 | |
Liu, Chuansheng | 3d2699b | 2014-02-18 10:28:44 +0800 | [diff] [blame] | 1204 | error = dpm_run_callback(callback, dev, state, info); |
| 1205 | if (!error) |
| 1206 | dev->power.is_late_suspended = true; |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1207 | else |
| 1208 | async_error = error; |
Liu, Chuansheng | 3d2699b | 2014-02-18 10:28:44 +0800 | [diff] [blame] | 1209 | |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1210 | Complete: |
Zhonghui Fu | 431d452 | 2015-03-18 15:54:27 +0100 | [diff] [blame] | 1211 | TRACE_SUSPEND(error); |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1212 | complete_all(&dev->power.completion); |
Liu, Chuansheng | 3d2699b | 2014-02-18 10:28:44 +0800 | [diff] [blame] | 1213 | return error; |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1216 | static void async_suspend_late(void *data, async_cookie_t cookie) |
| 1217 | { |
| 1218 | struct device *dev = (struct device *)data; |
| 1219 | int error; |
| 1220 | |
| 1221 | error = __device_suspend_late(dev, pm_transition, true); |
| 1222 | if (error) { |
| 1223 | dpm_save_failed_dev(dev_name(dev)); |
| 1224 | pm_dev_err(dev, pm_transition, " async", error); |
| 1225 | } |
| 1226 | put_device(dev); |
| 1227 | } |
| 1228 | |
| 1229 | static int device_suspend_late(struct device *dev) |
| 1230 | { |
| 1231 | reinit_completion(&dev->power.completion); |
| 1232 | |
Zhonghui Fu | 431d452 | 2015-03-18 15:54:27 +0100 | [diff] [blame] | 1233 | if (is_async(dev)) { |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1234 | get_device(dev); |
| 1235 | async_schedule(async_suspend_late, dev); |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | return __device_suspend_late(dev, pm_transition, false); |
| 1240 | } |
| 1241 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1242 | /** |
| 1243 | * dpm_suspend_late - Execute "late suspend" callbacks for all devices. |
| 1244 | * @state: PM transition of the system being carried out. |
| 1245 | */ |
Rafael J. Wysocki | 2a8a8ce | 2014-09-30 02:21:34 +0200 | [diff] [blame] | 1246 | int dpm_suspend_late(pm_message_t state) |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1247 | { |
| 1248 | ktime_t starttime = ktime_get(); |
| 1249 | int error = 0; |
| 1250 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 1251 | trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1252 | mutex_lock(&dpm_list_mtx); |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1253 | pm_transition = state; |
| 1254 | async_error = 0; |
| 1255 | |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1256 | while (!list_empty(&dpm_suspended_list)) { |
| 1257 | struct device *dev = to_device(dpm_suspended_list.prev); |
| 1258 | |
| 1259 | get_device(dev); |
| 1260 | mutex_unlock(&dpm_list_mtx); |
| 1261 | |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1262 | error = device_suspend_late(dev); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1263 | |
| 1264 | mutex_lock(&dpm_list_mtx); |
| 1265 | if (error) { |
| 1266 | pm_dev_err(dev, state, " late", error); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1267 | dpm_save_failed_dev(dev_name(dev)); |
| 1268 | put_device(dev); |
| 1269 | break; |
| 1270 | } |
| 1271 | if (!list_empty(&dev->power.entry)) |
| 1272 | list_move(&dev->power.entry, &dpm_late_early_list); |
| 1273 | put_device(dev); |
Rafael J. Wysocki | 52d136c | 2012-04-29 22:52:19 +0200 | [diff] [blame] | 1274 | |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1275 | if (async_error) |
Rafael J. Wysocki | 52d136c | 2012-04-29 22:52:19 +0200 | [diff] [blame] | 1276 | break; |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1277 | } |
| 1278 | mutex_unlock(&dpm_list_mtx); |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1279 | async_synchronize_full(); |
Imre Deak | 246ef76 | 2014-10-24 20:29:09 +0300 | [diff] [blame] | 1280 | if (!error) |
| 1281 | error = async_error; |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1282 | if (error) { |
| 1283 | suspend_stats.failed_suspend_late++; |
| 1284 | dpm_save_failed_step(SUSPEND_SUSPEND_LATE); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1285 | dpm_resume_early(resume_event(state)); |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1286 | } else { |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1287 | dpm_show_time(starttime, state, "late"); |
Liu, Chuansheng | de377b3 | 2014-02-18 10:28:48 +0800 | [diff] [blame] | 1288 | } |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 1289 | trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false); |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1290 | return error; |
| 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks. |
| 1295 | * @state: PM transition of the system being carried out. |
| 1296 | */ |
| 1297 | int dpm_suspend_end(pm_message_t state) |
| 1298 | { |
| 1299 | int error = dpm_suspend_late(state); |
Colin Cross | 064b021 | 2012-07-19 10:38:06 +0200 | [diff] [blame] | 1300 | if (error) |
| 1301 | return error; |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1302 | |
Colin Cross | 064b021 | 2012-07-19 10:38:06 +0200 | [diff] [blame] | 1303 | error = dpm_suspend_noirq(state); |
| 1304 | if (error) { |
Feng Hong | 997a031 | 2012-09-19 14:16:00 +0200 | [diff] [blame] | 1305 | dpm_resume_early(resume_event(state)); |
Colin Cross | 064b021 | 2012-07-19 10:38:06 +0200 | [diff] [blame] | 1306 | return error; |
| 1307 | } |
| 1308 | |
| 1309 | return 0; |
Rafael J. Wysocki | cf579df | 2012-01-29 20:38:29 +0100 | [diff] [blame] | 1310 | } |
| 1311 | EXPORT_SYMBOL_GPL(dpm_suspend_end); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1312 | |
| 1313 | /** |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1314 | * legacy_suspend - Execute a legacy (bus or class) suspend callback for device. |
Randy Dunlap | 0a88422 | 2010-01-08 14:42:57 -0800 | [diff] [blame] | 1315 | * @dev: Device to suspend. |
| 1316 | * @state: PM transition of the system being carried out. |
| 1317 | * @cb: Suspend callback to execute. |
Randy Dunlap | 58c256a | 2014-07-27 16:17:15 -0700 | [diff] [blame] | 1318 | * @info: string description of caller. |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1319 | */ |
| 1320 | static int legacy_suspend(struct device *dev, pm_message_t state, |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 1321 | int (*cb)(struct device *dev, pm_message_t state), |
| 1322 | char *info) |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1323 | { |
| 1324 | int error; |
| 1325 | ktime_t calltime; |
| 1326 | |
| 1327 | calltime = initcall_debug_start(dev); |
| 1328 | |
Todd E Brandt | e8bca47 | 2014-06-10 07:31:22 -0700 | [diff] [blame] | 1329 | trace_device_pm_callback_start(dev, info, state.event); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1330 | error = cb(dev, state); |
Todd E Brandt | e8bca47 | 2014-06-10 07:31:22 -0700 | [diff] [blame] | 1331 | trace_device_pm_callback_end(dev, error); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1332 | suspend_report_result(cb, error); |
| 1333 | |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 1334 | initcall_debug_report(dev, calltime, error, state, info); |
Rafael J. Wysocki | 875ab0b | 2009-12-18 01:57:31 +0100 | [diff] [blame] | 1335 | |
| 1336 | return error; |
| 1337 | } |
| 1338 | |
| 1339 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1340 | * device_suspend - Execute "suspend" callbacks for given device. |
| 1341 | * @dev: Device to handle. |
| 1342 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1343 | * @async: If true, the device is being suspended asynchronously. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1344 | */ |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1345 | static int __device_suspend(struct device *dev, pm_message_t state, bool async) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1346 | { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1347 | pm_callback_t callback = NULL; |
| 1348 | char *info = NULL; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1349 | int error = 0; |
Benoit Goby | 70fea60 | 2013-10-17 10:48:46 -0700 | [diff] [blame] | 1350 | DECLARE_DPM_WATCHDOG_ON_STACK(wd); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1351 | |
Zhonghui Fu | 431d452 | 2015-03-18 15:54:27 +0100 | [diff] [blame] | 1352 | TRACE_DEVICE(dev); |
| 1353 | TRACE_SUSPEND(0); |
| 1354 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1355 | dpm_wait_for_children(dev, async); |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 1356 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1357 | if (async_error) |
Mandeep Singh Baines | 1f758b2 | 2012-06-24 23:31:09 +0200 | [diff] [blame] | 1358 | goto Complete; |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1359 | |
Alan Stern | 88d2613 | 2012-09-19 21:59:02 +0200 | [diff] [blame] | 1360 | /* |
| 1361 | * If a device configured to wake up the system from sleep states |
| 1362 | * has been suspended at run time and there's a resume request pending |
| 1363 | * for it, this is equivalent to the device signaling wakeup, so the |
| 1364 | * system suspend operation should be aborted. |
| 1365 | */ |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1366 | if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) |
| 1367 | pm_wakeup_event(dev, 0); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1368 | |
Rafael J. Wysocki | d83f905 | 2010-12-03 23:14:26 +0100 | [diff] [blame] | 1369 | if (pm_wakeup_pending()) { |
| 1370 | async_error = -EBUSY; |
Mandeep Singh Baines | 1f758b2 | 2012-06-24 23:31:09 +0200 | [diff] [blame] | 1371 | goto Complete; |
Rafael J. Wysocki | d83f905 | 2010-12-03 23:14:26 +0100 | [diff] [blame] | 1372 | } |
| 1373 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 1374 | if (dev->power.syscore) |
| 1375 | goto Complete; |
| 1376 | |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 1377 | if (dev->power.direct_complete) { |
| 1378 | if (pm_runtime_status_suspended(dev)) { |
| 1379 | pm_runtime_disable(dev); |
| 1380 | if (pm_runtime_suspended_if_enabled(dev)) |
| 1381 | goto Complete; |
| 1382 | |
| 1383 | pm_runtime_enable(dev); |
| 1384 | } |
| 1385 | dev->power.direct_complete = false; |
| 1386 | } |
| 1387 | |
Benoit Goby | 70fea60 | 2013-10-17 10:48:46 -0700 | [diff] [blame] | 1388 | dpm_watchdog_set(&wd, dev); |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1389 | device_lock(dev); |
| 1390 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 1391 | if (dev->pm_domain) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1392 | info = "power domain "; |
| 1393 | callback = pm_op(&dev->pm_domain->ops, state); |
| 1394 | goto Run; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1395 | } |
| 1396 | |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1397 | if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1398 | info = "type "; |
| 1399 | callback = pm_op(dev->type->pm, state); |
| 1400 | goto Run; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1401 | } |
| 1402 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1403 | if (dev->class) { |
| 1404 | if (dev->class->pm) { |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1405 | info = "class "; |
| 1406 | callback = pm_op(dev->class->pm, state); |
| 1407 | goto Run; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1408 | } else if (dev->class->suspend) { |
| 1409 | pm_dev_dbg(dev, state, "legacy class "); |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 1410 | error = legacy_suspend(dev, state, dev->class->suspend, |
| 1411 | "legacy class "); |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1412 | goto End; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1413 | } |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1414 | } |
| 1415 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1416 | if (dev->bus) { |
| 1417 | if (dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1418 | info = "bus "; |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1419 | callback = pm_op(dev->bus->pm, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1420 | } else if (dev->bus->suspend) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1421 | pm_dev_dbg(dev, state, "legacy bus "); |
Shuah Khan | 5364467 | 2013-07-26 13:30:20 -0600 | [diff] [blame] | 1422 | error = legacy_suspend(dev, state, dev->bus->suspend, |
| 1423 | "legacy bus "); |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1424 | goto End; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1425 | } |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 1426 | } |
| 1427 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1428 | Run: |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1429 | if (!callback && dev->driver && dev->driver->pm) { |
| 1430 | info = "driver "; |
| 1431 | callback = pm_op(dev->driver->pm, state); |
| 1432 | } |
| 1433 | |
Rafael J. Wysocki | 9cf519d | 2011-12-18 00:34:01 +0100 | [diff] [blame] | 1434 | error = dpm_run_callback(callback, dev, state, info); |
| 1435 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1436 | End: |
Rafael J. Wysocki | 4ca46ff | 2011-10-16 23:34:36 +0200 | [diff] [blame] | 1437 | if (!error) { |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 1438 | struct device *parent = dev->parent; |
| 1439 | |
Rafael J. Wysocki | 4ca46ff | 2011-10-16 23:34:36 +0200 | [diff] [blame] | 1440 | dev->power.is_suspended = true; |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 1441 | if (parent) { |
| 1442 | spin_lock_irq(&parent->power.lock); |
| 1443 | |
| 1444 | dev->parent->power.direct_complete = false; |
| 1445 | if (dev->power.wakeup_path |
| 1446 | && !dev->parent->power.ignore_children) |
| 1447 | dev->parent->power.wakeup_path = true; |
| 1448 | |
| 1449 | spin_unlock_irq(&parent->power.lock); |
| 1450 | } |
Rafael J. Wysocki | 4ca46ff | 2011-10-16 23:34:36 +0200 | [diff] [blame] | 1451 | } |
Alan Stern | 6d0e0e8 | 2011-06-18 22:42:09 +0200 | [diff] [blame] | 1452 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 1453 | device_unlock(dev); |
Benoit Goby | 70fea60 | 2013-10-17 10:48:46 -0700 | [diff] [blame] | 1454 | dpm_watchdog_clear(&wd); |
Mandeep Singh Baines | 1f758b2 | 2012-06-24 23:31:09 +0200 | [diff] [blame] | 1455 | |
| 1456 | Complete: |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1457 | complete_all(&dev->power.completion); |
Alan Stern | 88d2613 | 2012-09-19 21:59:02 +0200 | [diff] [blame] | 1458 | if (error) |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1459 | async_error = error; |
| 1460 | |
Zhonghui Fu | 431d452 | 2015-03-18 15:54:27 +0100 | [diff] [blame] | 1461 | TRACE_SUSPEND(error); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1462 | return error; |
| 1463 | } |
| 1464 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1465 | static void async_suspend(void *data, async_cookie_t cookie) |
| 1466 | { |
| 1467 | struct device *dev = (struct device *)data; |
| 1468 | int error; |
| 1469 | |
| 1470 | error = __device_suspend(dev, pm_transition, true); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1471 | if (error) { |
| 1472 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1473 | pm_dev_err(dev, pm_transition, " async", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1474 | } |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1475 | |
| 1476 | put_device(dev); |
| 1477 | } |
| 1478 | |
| 1479 | static int device_suspend(struct device *dev) |
| 1480 | { |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 1481 | reinit_completion(&dev->power.completion); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1482 | |
Zhonghui Fu | 431d452 | 2015-03-18 15:54:27 +0100 | [diff] [blame] | 1483 | if (is_async(dev)) { |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1484 | get_device(dev); |
| 1485 | async_schedule(async_suspend, dev); |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
| 1489 | return __device_suspend(dev, pm_transition, false); |
| 1490 | } |
| 1491 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1492 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1493 | * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices. |
| 1494 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1495 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1496 | int dpm_suspend(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1497 | { |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 1498 | ktime_t starttime = ktime_get(); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1499 | int error = 0; |
| 1500 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 1501 | trace_suspend_resume(TPS("dpm_suspend"), state.event, true); |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1502 | might_sleep(); |
| 1503 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1504 | cpufreq_suspend(); |
| 1505 | |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1506 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1507 | pm_transition = state; |
| 1508 | async_error = 0; |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 1509 | while (!list_empty(&dpm_prepared_list)) { |
| 1510 | struct device *dev = to_device(dpm_prepared_list.prev); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1511 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1512 | get_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1513 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1514 | |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1515 | error = device_suspend(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1516 | |
Alan Stern | 1b3cbec | 2008-02-29 11:50:22 -0500 | [diff] [blame] | 1517 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1518 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1519 | pm_dev_err(dev, state, "", error); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1520 | dpm_save_failed_dev(dev_name(dev)); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1521 | put_device(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1522 | break; |
| 1523 | } |
Rafael J. Wysocki | 7a8d37a | 2008-02-25 00:35:04 +0100 | [diff] [blame] | 1524 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 1525 | list_move(&dev->power.entry, &dpm_suspended_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1526 | put_device(dev); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1527 | if (async_error) |
| 1528 | break; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1529 | } |
| 1530 | mutex_unlock(&dpm_list_mtx); |
Rafael J. Wysocki | 5af84b8 | 2010-01-23 22:23:32 +0100 | [diff] [blame] | 1531 | async_synchronize_full(); |
| 1532 | if (!error) |
| 1533 | error = async_error; |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1534 | if (error) { |
| 1535 | suspend_stats.failed_suspend++; |
| 1536 | dpm_save_failed_step(SUSPEND_SUSPEND); |
| 1537 | } else |
Rafael J. Wysocki | ecf762b | 2009-12-18 01:57:47 +0100 | [diff] [blame] | 1538 | dpm_show_time(starttime, state, NULL); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 1539 | trace_suspend_resume(TPS("dpm_suspend"), state.event, false); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1540 | return error; |
| 1541 | } |
| 1542 | |
| 1543 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1544 | * device_prepare - Prepare a device for system power transition. |
| 1545 | * @dev: Device to handle. |
| 1546 | * @state: PM transition of the system being carried out. |
| 1547 | * |
| 1548 | * Execute the ->prepare() callback(s) for given device. No new children of the |
| 1549 | * device may be registered after this function has returned. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1550 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1551 | static int device_prepare(struct device *dev, pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1552 | { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1553 | int (*callback)(struct device *) = NULL; |
| 1554 | char *info = NULL; |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 1555 | int ret = 0; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1556 | |
Rafael J. Wysocki | dbf3741 | 2012-08-06 01:46:39 +0200 | [diff] [blame] | 1557 | if (dev->power.syscore) |
| 1558 | return 0; |
| 1559 | |
Alan Stern | 88d2613 | 2012-09-19 21:59:02 +0200 | [diff] [blame] | 1560 | /* |
| 1561 | * If a device's parent goes into runtime suspend at the wrong time, |
| 1562 | * it won't be possible to resume the device. To prevent this we |
| 1563 | * block runtime suspend here, during the prepare phase, and allow |
| 1564 | * it again during the complete phase. |
| 1565 | */ |
| 1566 | pm_runtime_get_noresume(dev); |
| 1567 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 1568 | device_lock(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1569 | |
Rafael J. Wysocki | 4ca46ff | 2011-10-16 23:34:36 +0200 | [diff] [blame] | 1570 | dev->power.wakeup_path = device_may_wakeup(dev); |
| 1571 | |
Rafael J. Wysocki | 564b905 | 2011-06-23 01:52:55 +0200 | [diff] [blame] | 1572 | if (dev->pm_domain) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1573 | info = "preparing power domain "; |
| 1574 | callback = dev->pm_domain->ops.prepare; |
Rafael J. Wysocki | 4d27e9d | 2011-04-29 00:35:50 +0200 | [diff] [blame] | 1575 | } else if (dev->type && dev->type->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1576 | info = "preparing type "; |
| 1577 | callback = dev->type->pm->prepare; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1578 | } else if (dev->class && dev->class->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1579 | info = "preparing class "; |
| 1580 | callback = dev->class->pm->prepare; |
Rafael J. Wysocki | 9659cc0 | 2011-02-18 23:20:21 +0100 | [diff] [blame] | 1581 | } else if (dev->bus && dev->bus->pm) { |
Rafael J. Wysocki | 35cd133 | 2011-12-18 00:34:13 +0100 | [diff] [blame] | 1582 | info = "preparing bus "; |
| 1583 | callback = dev->bus->pm->prepare; |
| 1584 | } |
| 1585 | |
| 1586 | if (!callback && dev->driver && dev->driver->pm) { |
| 1587 | info = "preparing driver "; |
| 1588 | callback = dev->driver->pm->prepare; |
| 1589 | } |
| 1590 | |
Todd E Brandt | e8bca47 | 2014-06-10 07:31:22 -0700 | [diff] [blame] | 1591 | if (callback) { |
| 1592 | trace_device_pm_callback_start(dev, info, state.event); |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 1593 | ret = callback(dev); |
Todd E Brandt | e8bca47 | 2014-06-10 07:31:22 -0700 | [diff] [blame] | 1594 | trace_device_pm_callback_end(dev, ret); |
| 1595 | } |
Rafael J. Wysocki | 7538e3d | 2011-02-16 21:53:17 +0100 | [diff] [blame] | 1596 | |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 1597 | device_unlock(dev); |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1598 | |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 1599 | if (ret < 0) { |
| 1600 | suspend_report_result(callback, ret); |
Ulf Hansson | aa1b9f1 | 2013-11-13 15:45:03 +0100 | [diff] [blame] | 1601 | pm_runtime_put(dev); |
Rafael J. Wysocki | aae4518 | 2014-05-16 02:46:50 +0200 | [diff] [blame] | 1602 | return ret; |
| 1603 | } |
| 1604 | /* |
| 1605 | * A positive return value from ->prepare() means "this device appears |
| 1606 | * to be runtime-suspended and its state is fine, so if it really is |
| 1607 | * runtime-suspended, you can leave it in that state provided that you |
| 1608 | * will do the same thing with all of its descendants". This only |
| 1609 | * applies to suspend transitions, however. |
| 1610 | */ |
| 1611 | spin_lock_irq(&dev->power.lock); |
| 1612 | dev->power.direct_complete = ret > 0 && state.event == PM_EVENT_SUSPEND; |
| 1613 | spin_unlock_irq(&dev->power.lock); |
| 1614 | return 0; |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1618 | * dpm_prepare - Prepare all non-sysdev devices for a system PM transition. |
| 1619 | * @state: PM transition of the system being carried out. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1620 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1621 | * Execute the ->prepare() callback(s) for all devices. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1622 | */ |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1623 | int dpm_prepare(pm_message_t state) |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1624 | { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1625 | int error = 0; |
| 1626 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 1627 | trace_suspend_resume(TPS("dpm_prepare"), state.event, true); |
Rafael J. Wysocki | 91e7c75 | 2011-05-17 23:26:00 +0200 | [diff] [blame] | 1628 | might_sleep(); |
| 1629 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1630 | mutex_lock(&dpm_list_mtx); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1631 | while (!list_empty(&dpm_list)) { |
| 1632 | struct device *dev = to_device(dpm_list.next); |
| 1633 | |
| 1634 | get_device(dev); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1635 | mutex_unlock(&dpm_list_mtx); |
| 1636 | |
Rafael J. Wysocki | 1e2ef05 | 2011-07-06 10:51:58 +0200 | [diff] [blame] | 1637 | error = device_prepare(dev, state); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1638 | |
| 1639 | mutex_lock(&dpm_list_mtx); |
| 1640 | if (error) { |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1641 | if (error == -EAGAIN) { |
| 1642 | put_device(dev); |
Sebastian Ott | 886a7a3 | 2009-07-08 13:26:05 +0200 | [diff] [blame] | 1643 | error = 0; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1644 | continue; |
| 1645 | } |
Rafael J. Wysocki | 1e75227 | 2010-12-03 22:58:05 +0100 | [diff] [blame] | 1646 | printk(KERN_INFO "PM: Device %s not prepared " |
| 1647 | "for power transition: code %d\n", |
Rafael J. Wysocki | 5c1a07a | 2010-12-24 15:03:34 +0100 | [diff] [blame] | 1648 | dev_name(dev), error); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1649 | put_device(dev); |
| 1650 | break; |
| 1651 | } |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 1652 | dev->power.is_prepared = true; |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1653 | if (!list_empty(&dev->power.entry)) |
Rafael J. Wysocki | 8a43a9a | 2010-12-16 00:50:30 +0100 | [diff] [blame] | 1654 | list_move_tail(&dev->power.entry, &dpm_prepared_list); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1655 | put_device(dev); |
| 1656 | } |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1657 | mutex_unlock(&dpm_list_mtx); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 1658 | trace_suspend_resume(TPS("dpm_prepare"), state.event, false); |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1659 | return error; |
| 1660 | } |
| 1661 | |
| 1662 | /** |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1663 | * dpm_suspend_start - Prepare devices for PM transition and suspend them. |
| 1664 | * @state: PM transition of the system being carried out. |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1665 | * |
Rafael J. Wysocki | 20d652d | 2009-08-20 20:25:52 +0200 | [diff] [blame] | 1666 | * Prepare all non-sysdev devices for system PM transition and execute "suspend" |
| 1667 | * callbacks for them. |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1668 | */ |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1669 | int dpm_suspend_start(pm_message_t state) |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1670 | { |
Rafael J. Wysocki | 775b64d | 2008-01-12 20:40:46 +0100 | [diff] [blame] | 1671 | int error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1672 | |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1673 | error = dpm_prepare(state); |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 1674 | if (error) { |
| 1675 | suspend_stats.failed_prepare++; |
| 1676 | dpm_save_failed_step(SUSPEND_PREPARE); |
| 1677 | } else |
Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 1678 | error = dpm_suspend(state); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1679 | return error; |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1680 | } |
Alan Stern | d161630 | 2009-05-24 22:05:42 +0200 | [diff] [blame] | 1681 | EXPORT_SYMBOL_GPL(dpm_suspend_start); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1682 | |
| 1683 | void __suspend_report_result(const char *function, void *fn, int ret) |
| 1684 | { |
Bjorn Helgaas | c80cfb0 | 2008-10-15 22:01:35 -0700 | [diff] [blame] | 1685 | if (ret) |
| 1686 | printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret); |
Alan Stern | cd59abf | 2007-09-21 15:36:56 -0400 | [diff] [blame] | 1687 | } |
| 1688 | EXPORT_SYMBOL_GPL(__suspend_report_result); |
Rafael J. Wysocki | f8824ce | 2010-01-27 23:47:38 +0100 | [diff] [blame] | 1689 | |
| 1690 | /** |
| 1691 | * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete. |
| 1692 | * @dev: Device to wait for. |
| 1693 | * @subordinate: Device that needs to wait for @dev. |
| 1694 | */ |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1695 | int device_pm_wait_for_dev(struct device *subordinate, struct device *dev) |
Rafael J. Wysocki | f8824ce | 2010-01-27 23:47:38 +0100 | [diff] [blame] | 1696 | { |
| 1697 | dpm_wait(dev, subordinate->power.async_suspend); |
Rafael J. Wysocki | 098dff7 | 2010-09-22 22:10:57 +0200 | [diff] [blame] | 1698 | return async_error; |
Rafael J. Wysocki | f8824ce | 2010-01-27 23:47:38 +0100 | [diff] [blame] | 1699 | } |
| 1700 | EXPORT_SYMBOL_GPL(device_pm_wait_for_dev); |
Ming Lei | dfe3212 | 2012-08-17 22:06:59 +0800 | [diff] [blame] | 1701 | |
| 1702 | /** |
| 1703 | * dpm_for_each_dev - device iterator. |
| 1704 | * @data: data for the callback. |
| 1705 | * @fn: function to be called for each device. |
| 1706 | * |
| 1707 | * Iterate over devices in dpm_list, and call @fn for each device, |
| 1708 | * passing it @data. |
| 1709 | */ |
| 1710 | void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *)) |
| 1711 | { |
| 1712 | struct device *dev; |
| 1713 | |
| 1714 | if (!fn) |
| 1715 | return; |
| 1716 | |
| 1717 | device_pm_lock(); |
| 1718 | list_for_each_entry(dev, &dpm_list, power.entry) |
| 1719 | fn(dev, data); |
| 1720 | device_pm_unlock(); |
| 1721 | } |
| 1722 | EXPORT_SYMBOL_GPL(dpm_for_each_dev); |