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