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