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