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