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