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