Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * kernel/power/suspend.c - Suspend to RAM and standby functionality. |
| 3 | * |
| 4 | * Copyright (c) 2003 Patrick Mochel |
| 5 | * Copyright (c) 2003 Open Source Development Lab |
| 6 | * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. |
| 7 | * |
| 8 | * This file is released under the GPLv2. |
| 9 | */ |
| 10 | |
Rafael J. Wysocki | 142bce7 | 2017-07-21 14:50:48 +0200 | [diff] [blame] | 11 | #define pr_fmt(fmt) "PM: " fmt |
| 12 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 13 | #include <linux/string.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/console.h> |
| 18 | #include <linux/cpu.h> |
Rafael J. Wysocki | f3f1253 | 2014-04-20 23:43:01 +0200 | [diff] [blame] | 19 | #include <linux/cpuidle.h> |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 20 | #include <linux/syscalls.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/gfp.h> |
Matthew Garrett | dd4c4f1 | 2010-05-28 16:32:14 -0400 | [diff] [blame] | 22 | #include <linux/io.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/mm.h> |
| 26 | #include <linux/slab.h> |
Paul Gortmaker | 6e5fdee | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Matthew Garrett | dd4c4f1 | 2010-05-28 16:32:14 -0400 | [diff] [blame] | 28 | #include <linux/suspend.h> |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 29 | #include <linux/syscore_ops.h> |
Srivatsa S. Bhat | 443772d4 | 2012-06-16 15:30:45 +0200 | [diff] [blame] | 30 | #include <linux/ftrace.h> |
Jean Pihet | 938cfed | 2011-01-05 19:49:01 +0100 | [diff] [blame] | 31 | #include <trace/events/power.h> |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 32 | #include <linux/compiler.h> |
Brian Norris | 1d4a9c1 | 2015-02-22 21:16:49 -0800 | [diff] [blame] | 33 | #include <linux/moduleparam.h> |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 34 | |
| 35 | #include "power.h" |
| 36 | |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 37 | const char * const pm_labels[] = { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 38 | [PM_SUSPEND_TO_IDLE] = "freeze", |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 39 | [PM_SUSPEND_STANDBY] = "standby", |
| 40 | [PM_SUSPEND_MEM] = "mem", |
| 41 | }; |
Rafael J. Wysocki | d431cbc | 2014-07-15 22:02:11 +0200 | [diff] [blame] | 42 | const char *pm_states[PM_SUSPEND_MAX]; |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 43 | static const char * const mem_sleep_labels[] = { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 44 | [PM_SUSPEND_TO_IDLE] = "s2idle", |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 45 | [PM_SUSPEND_STANDBY] = "shallow", |
| 46 | [PM_SUSPEND_MEM] = "deep", |
| 47 | }; |
| 48 | const char *mem_sleep_states[PM_SUSPEND_MAX]; |
| 49 | |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 50 | suspend_state_t mem_sleep_current = PM_SUSPEND_TO_IDLE; |
Rafael J. Wysocki | e870c6c8 | 2017-07-31 23:43:18 +0200 | [diff] [blame] | 51 | suspend_state_t mem_sleep_default = PM_SUSPEND_MAX; |
Florian Fainelli | bd8c9ba | 2017-07-17 17:19:25 -0700 | [diff] [blame] | 52 | suspend_state_t pm_suspend_target_state; |
| 53 | EXPORT_SYMBOL_GPL(pm_suspend_target_state); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 54 | |
Rafael J. Wysocki | ef25ba0 | 2015-10-07 00:49:34 +0200 | [diff] [blame] | 55 | unsigned int pm_suspend_global_flags; |
| 56 | EXPORT_SYMBOL_GPL(pm_suspend_global_flags); |
| 57 | |
Lionel Debroux | 2f55ac0 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 58 | static const struct platform_suspend_ops *suspend_ops; |
Rafael J. Wysocki | 1f0b638 | 2014-05-15 23:29:57 +0200 | [diff] [blame] | 59 | static const struct platform_freeze_ops *freeze_ops; |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 60 | static DECLARE_WAIT_QUEUE_HEAD(s2idle_wait_head); |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 61 | |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 62 | enum s2idle_states __read_mostly s2idle_state; |
| 63 | static DEFINE_SPINLOCK(s2idle_lock); |
Zhang Rui | 7e73c5ae | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 64 | |
Rafael J. Wysocki | 1f0b638 | 2014-05-15 23:29:57 +0200 | [diff] [blame] | 65 | void freeze_set_ops(const struct platform_freeze_ops *ops) |
| 66 | { |
| 67 | lock_system_sleep(); |
| 68 | freeze_ops = ops; |
| 69 | unlock_system_sleep(); |
| 70 | } |
| 71 | |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 72 | static void s2idle_begin(void) |
Zhang Rui | 7e73c5ae | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 73 | { |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 74 | s2idle_state = S2IDLE_STATE_NONE; |
Zhang Rui | 7e73c5ae | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 77 | static void s2idle_enter(void) |
Zhang Rui | 7e73c5ae | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 78 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 79 | trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, true); |
Rafael J. Wysocki | 33e4f80 | 2017-06-12 22:56:34 +0200 | [diff] [blame] | 80 | |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 81 | spin_lock_irq(&s2idle_lock); |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 82 | if (pm_wakeup_pending()) |
| 83 | goto out; |
| 84 | |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 85 | s2idle_state = S2IDLE_STATE_ENTER; |
| 86 | spin_unlock_irq(&s2idle_lock); |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 87 | |
| 88 | get_online_cpus(); |
Rafael J. Wysocki | f3f1253 | 2014-04-20 23:43:01 +0200 | [diff] [blame] | 89 | cpuidle_resume(); |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 90 | |
| 91 | /* Push all the CPUs into the idle loop. */ |
| 92 | wake_up_all_idle_cpus(); |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 93 | /* Make the current CPU wait so it can enter the idle loop too. */ |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 94 | wait_event(s2idle_wait_head, |
| 95 | s2idle_state == S2IDLE_STATE_WAKE); |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 96 | |
Rafael J. Wysocki | f3f1253 | 2014-04-20 23:43:01 +0200 | [diff] [blame] | 97 | cpuidle_pause(); |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 98 | put_online_cpus(); |
| 99 | |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 100 | spin_lock_irq(&s2idle_lock); |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 101 | |
| 102 | out: |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 103 | s2idle_state = S2IDLE_STATE_NONE; |
| 104 | spin_unlock_irq(&s2idle_lock); |
Rafael J. Wysocki | 33e4f80 | 2017-06-12 22:56:34 +0200 | [diff] [blame] | 105 | |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 106 | trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, false); |
Rafael J. Wysocki | 33e4f80 | 2017-06-12 22:56:34 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void s2idle_loop(void) |
| 110 | { |
Rafael J. Wysocki | 8d8b244 | 2017-07-19 02:38:44 +0200 | [diff] [blame] | 111 | pm_pr_dbg("suspend-to-idle\n"); |
Rafael J. Wysocki | 33e4f80 | 2017-06-12 22:56:34 +0200 | [diff] [blame] | 112 | |
Rafael J. Wysocki | 9a3ebe3 | 2017-07-21 02:12:10 +0200 | [diff] [blame] | 113 | for (;;) { |
| 114 | int error; |
| 115 | |
| 116 | dpm_noirq_begin(); |
| 117 | |
Rafael J. Wysocki | 8e6bcd9 | 2017-07-21 02:07:54 +0200 | [diff] [blame] | 118 | /* |
| 119 | * Suspend-to-idle equals |
| 120 | * frozen processes + suspended devices + idle processors. |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 121 | * Thus s2idle_enter() should be called right after |
Rafael J. Wysocki | 8e6bcd9 | 2017-07-21 02:07:54 +0200 | [diff] [blame] | 122 | * all devices have been suspended. |
| 123 | */ |
Rafael J. Wysocki | 9a3ebe3 | 2017-07-21 02:12:10 +0200 | [diff] [blame] | 124 | error = dpm_noirq_suspend_devices(PMSG_SUSPEND); |
| 125 | if (!error) |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 126 | s2idle_enter(); |
Rafael J. Wysocki | 9a3ebe3 | 2017-07-21 02:12:10 +0200 | [diff] [blame] | 127 | |
| 128 | dpm_noirq_resume_devices(PMSG_RESUME); |
| 129 | if (error && (error != -EBUSY || !pm_wakeup_pending())) { |
| 130 | dpm_noirq_end(); |
| 131 | break; |
| 132 | } |
Rafael J. Wysocki | 33e4f80 | 2017-06-12 22:56:34 +0200 | [diff] [blame] | 133 | |
| 134 | if (freeze_ops && freeze_ops->wake) |
| 135 | freeze_ops->wake(); |
| 136 | |
Rafael J. Wysocki | 9a3ebe3 | 2017-07-21 02:12:10 +0200 | [diff] [blame] | 137 | dpm_noirq_end(); |
| 138 | |
Rafael J. Wysocki | 33e4f80 | 2017-06-12 22:56:34 +0200 | [diff] [blame] | 139 | if (freeze_ops && freeze_ops->sync) |
| 140 | freeze_ops->sync(); |
| 141 | |
| 142 | if (pm_wakeup_pending()) |
| 143 | break; |
| 144 | |
| 145 | pm_wakeup_clear(false); |
Rafael J. Wysocki | 8e6bcd9 | 2017-07-21 02:07:54 +0200 | [diff] [blame] | 146 | } |
Rafael J. Wysocki | 33e4f80 | 2017-06-12 22:56:34 +0200 | [diff] [blame] | 147 | |
Rafael J. Wysocki | 8d8b244 | 2017-07-19 02:38:44 +0200 | [diff] [blame] | 148 | pm_pr_dbg("resume from suspend-to-idle\n"); |
Zhang Rui | 7e73c5ae | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 149 | } |
| 150 | |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 151 | void s2idle_wake(void) |
Zhang Rui | 7e73c5ae | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 152 | { |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 153 | unsigned long flags; |
| 154 | |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 155 | spin_lock_irqsave(&s2idle_lock, flags); |
| 156 | if (s2idle_state > S2IDLE_STATE_NONE) { |
| 157 | s2idle_state = S2IDLE_STATE_WAKE; |
| 158 | wake_up(&s2idle_wait_head); |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 159 | } |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 160 | spin_unlock_irqrestore(&s2idle_lock, flags); |
Zhang Rui | 7e73c5ae | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 161 | } |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 162 | EXPORT_SYMBOL_GPL(s2idle_wake); |
Zhang Rui | 7e73c5ae | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 163 | |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 164 | static bool valid_state(suspend_state_t state) |
| 165 | { |
| 166 | /* |
| 167 | * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level |
| 168 | * support and need to be valid to the low level |
| 169 | * implementation, no valid callback implies that none are valid. |
| 170 | */ |
| 171 | return suspend_ops && suspend_ops->valid && suspend_ops->valid(state); |
| 172 | } |
| 173 | |
Sudeep Holla | fa7fd6f | 2016-08-19 14:41:00 +0100 | [diff] [blame] | 174 | void __init pm_states_init(void) |
| 175 | { |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 176 | /* "mem" and "freeze" are always present in /sys/power/state. */ |
| 177 | pm_states[PM_SUSPEND_MEM] = pm_labels[PM_SUSPEND_MEM]; |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 178 | pm_states[PM_SUSPEND_TO_IDLE] = pm_labels[PM_SUSPEND_TO_IDLE]; |
Sudeep Holla | fa7fd6f | 2016-08-19 14:41:00 +0100 | [diff] [blame] | 179 | /* |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 180 | * Suspend-to-idle should be supported even without any suspend_ops, |
| 181 | * initialize mem_sleep_states[] accordingly here. |
Sudeep Holla | fa7fd6f | 2016-08-19 14:41:00 +0100 | [diff] [blame] | 182 | */ |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 183 | mem_sleep_states[PM_SUSPEND_TO_IDLE] = mem_sleep_labels[PM_SUSPEND_TO_IDLE]; |
Sudeep Holla | fa7fd6f | 2016-08-19 14:41:00 +0100 | [diff] [blame] | 184 | } |
| 185 | |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 186 | static int __init mem_sleep_default_setup(char *str) |
Rafael J. Wysocki | 0399d4d | 2014-05-26 13:40:59 +0200 | [diff] [blame] | 187 | { |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 188 | suspend_state_t state; |
| 189 | |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 190 | for (state = PM_SUSPEND_TO_IDLE; state <= PM_SUSPEND_MEM; state++) |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 191 | if (mem_sleep_labels[state] && |
| 192 | !strcmp(str, mem_sleep_labels[state])) { |
| 193 | mem_sleep_default = state; |
| 194 | break; |
| 195 | } |
| 196 | |
Rafael J. Wysocki | 0399d4d | 2014-05-26 13:40:59 +0200 | [diff] [blame] | 197 | return 1; |
| 198 | } |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 199 | __setup("mem_sleep_default=", mem_sleep_default_setup); |
Rafael J. Wysocki | 0399d4d | 2014-05-26 13:40:59 +0200 | [diff] [blame] | 200 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 201 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 202 | * suspend_set_ops - Set the global suspend method table. |
| 203 | * @ops: Suspend operations to use. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 204 | */ |
Lionel Debroux | 2f55ac0 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 205 | void suspend_set_ops(const struct platform_suspend_ops *ops) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 206 | { |
Srivatsa S. Bhat | bcda53f | 2011-12-07 22:29:54 +0100 | [diff] [blame] | 207 | lock_system_sleep(); |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 208 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 209 | suspend_ops = ops; |
Rafael J. Wysocki | 0399d4d | 2014-05-26 13:40:59 +0200 | [diff] [blame] | 210 | |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 211 | if (valid_state(PM_SUSPEND_STANDBY)) { |
| 212 | mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY]; |
| 213 | pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY]; |
| 214 | if (mem_sleep_default == PM_SUSPEND_STANDBY) |
| 215 | mem_sleep_current = PM_SUSPEND_STANDBY; |
| 216 | } |
| 217 | if (valid_state(PM_SUSPEND_MEM)) { |
| 218 | mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM]; |
Rafael J. Wysocki | e870c6c8 | 2017-07-31 23:43:18 +0200 | [diff] [blame] | 219 | if (mem_sleep_default >= PM_SUSPEND_MEM) |
Rafael J. Wysocki | 406e793 | 2016-11-21 22:45:40 +0100 | [diff] [blame] | 220 | mem_sleep_current = PM_SUSPEND_MEM; |
| 221 | } |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 222 | |
Srivatsa S. Bhat | bcda53f | 2011-12-07 22:29:54 +0100 | [diff] [blame] | 223 | unlock_system_sleep(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 224 | } |
Kevin Hilman | a5e4fd8 | 2011-06-27 01:01:07 +0200 | [diff] [blame] | 225 | EXPORT_SYMBOL_GPL(suspend_set_ops); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 226 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 227 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 228 | * suspend_valid_only_mem - Generic memory-only valid callback. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 229 | * |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 230 | * Platform drivers that implement mem suspend only and only need to check for |
| 231 | * that in their .valid() callback can use this instead of rolling their own |
| 232 | * .valid() callback. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 233 | */ |
| 234 | int suspend_valid_only_mem(suspend_state_t state) |
| 235 | { |
| 236 | return state == PM_SUSPEND_MEM; |
| 237 | } |
Kevin Hilman | a5e4fd8 | 2011-06-27 01:01:07 +0200 | [diff] [blame] | 238 | EXPORT_SYMBOL_GPL(suspend_valid_only_mem); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 239 | |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 240 | static bool sleep_state_supported(suspend_state_t state) |
| 241 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 242 | return state == PM_SUSPEND_TO_IDLE || (suspend_ops && suspend_ops->enter); |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static int platform_suspend_prepare(suspend_state_t state) |
| 246 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 247 | return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare ? |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 248 | suspend_ops->prepare() : 0; |
| 249 | } |
| 250 | |
Rafael J. Wysocki | a8d46b9 | 2014-09-30 02:29:01 +0200 | [diff] [blame] | 251 | static int platform_suspend_prepare_late(suspend_state_t state) |
| 252 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 253 | return state == PM_SUSPEND_TO_IDLE && freeze_ops && freeze_ops->prepare ? |
Rafael J. Wysocki | a8d46b9 | 2014-09-30 02:29:01 +0200 | [diff] [blame] | 254 | freeze_ops->prepare() : 0; |
| 255 | } |
| 256 | |
Rafael J. Wysocki | ebc3e41 | 2014-09-30 02:22:24 +0200 | [diff] [blame] | 257 | static int platform_suspend_prepare_noirq(suspend_state_t state) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 258 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 259 | return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare_late ? |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 260 | suspend_ops->prepare_late() : 0; |
| 261 | } |
| 262 | |
Rafael J. Wysocki | ebc3e41 | 2014-09-30 02:22:24 +0200 | [diff] [blame] | 263 | static void platform_resume_noirq(suspend_state_t state) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 264 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 265 | if (state != PM_SUSPEND_TO_IDLE && suspend_ops->wake) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 266 | suspend_ops->wake(); |
| 267 | } |
| 268 | |
Rafael J. Wysocki | a8d46b9 | 2014-09-30 02:29:01 +0200 | [diff] [blame] | 269 | static void platform_resume_early(suspend_state_t state) |
| 270 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 271 | if (state == PM_SUSPEND_TO_IDLE && freeze_ops && freeze_ops->restore) |
Rafael J. Wysocki | a8d46b9 | 2014-09-30 02:29:01 +0200 | [diff] [blame] | 272 | freeze_ops->restore(); |
| 273 | } |
| 274 | |
Rafael J. Wysocki | ebc3e41 | 2014-09-30 02:22:24 +0200 | [diff] [blame] | 275 | static void platform_resume_finish(suspend_state_t state) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 276 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 277 | if (state != PM_SUSPEND_TO_IDLE && suspend_ops->finish) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 278 | suspend_ops->finish(); |
| 279 | } |
| 280 | |
| 281 | static int platform_suspend_begin(suspend_state_t state) |
| 282 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 283 | if (state == PM_SUSPEND_TO_IDLE && freeze_ops && freeze_ops->begin) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 284 | return freeze_ops->begin(); |
Sudeep Holla | fa7fd6f | 2016-08-19 14:41:00 +0100 | [diff] [blame] | 285 | else if (suspend_ops && suspend_ops->begin) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 286 | return suspend_ops->begin(state); |
| 287 | else |
| 288 | return 0; |
| 289 | } |
| 290 | |
Rafael J. Wysocki | ebc3e41 | 2014-09-30 02:22:24 +0200 | [diff] [blame] | 291 | static void platform_resume_end(suspend_state_t state) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 292 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 293 | if (state == PM_SUSPEND_TO_IDLE && freeze_ops && freeze_ops->end) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 294 | freeze_ops->end(); |
Sudeep Holla | fa7fd6f | 2016-08-19 14:41:00 +0100 | [diff] [blame] | 295 | else if (suspend_ops && suspend_ops->end) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 296 | suspend_ops->end(); |
| 297 | } |
| 298 | |
Rafael J. Wysocki | ebc3e41 | 2014-09-30 02:22:24 +0200 | [diff] [blame] | 299 | static void platform_recover(suspend_state_t state) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 300 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 301 | if (state != PM_SUSPEND_TO_IDLE && suspend_ops->recover) |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 302 | suspend_ops->recover(); |
| 303 | } |
| 304 | |
| 305 | static bool platform_suspend_again(suspend_state_t state) |
| 306 | { |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 307 | return state != PM_SUSPEND_TO_IDLE && suspend_ops->suspend_again ? |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 308 | suspend_ops->suspend_again() : false; |
| 309 | } |
| 310 | |
Brian Norris | 1d4a9c1 | 2015-02-22 21:16:49 -0800 | [diff] [blame] | 311 | #ifdef CONFIG_PM_DEBUG |
| 312 | static unsigned int pm_test_delay = 5; |
| 313 | module_param(pm_test_delay, uint, 0644); |
| 314 | MODULE_PARM_DESC(pm_test_delay, |
| 315 | "Number of seconds to wait before resuming from suspend test"); |
| 316 | #endif |
| 317 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 318 | static int suspend_test(int level) |
| 319 | { |
| 320 | #ifdef CONFIG_PM_DEBUG |
| 321 | if (pm_test_level == level) { |
saurabh | 22e09b3 | 2015-10-28 08:54:01 +0530 | [diff] [blame] | 322 | pr_info("suspend debug: Waiting for %d second(s).\n", |
Brian Norris | 1d4a9c1 | 2015-02-22 21:16:49 -0800 | [diff] [blame] | 323 | pm_test_delay); |
| 324 | mdelay(pm_test_delay * 1000); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 325 | return 1; |
| 326 | } |
| 327 | #endif /* !CONFIG_PM_DEBUG */ |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 332 | * suspend_prepare - Prepare for entering system sleep state. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 333 | * |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 334 | * Common code run for every system sleep state that can be entered (except for |
| 335 | * hibernation). Run suspend notifiers, allocate the "suspend" console and |
| 336 | * freeze processes. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 337 | */ |
Zhang Rui | 7e73c5ae | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 338 | static int suspend_prepare(suspend_state_t state) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 339 | { |
Lianwei Wang | ea00f4f | 2016-06-19 23:52:27 -0700 | [diff] [blame] | 340 | int error, nr_calls = 0; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 341 | |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 342 | if (!sleep_state_supported(state)) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 343 | return -EPERM; |
| 344 | |
| 345 | pm_prepare_console(); |
| 346 | |
Lianwei Wang | ea00f4f | 2016-06-19 23:52:27 -0700 | [diff] [blame] | 347 | error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls); |
| 348 | if (error) { |
| 349 | nr_calls--; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 350 | goto Finish; |
Lianwei Wang | ea00f4f | 2016-06-19 23:52:27 -0700 | [diff] [blame] | 351 | } |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 352 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 353 | trace_suspend_resume(TPS("freeze_processes"), 0, true); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 354 | error = suspend_freeze_processes(); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 355 | trace_suspend_resume(TPS("freeze_processes"), 0, false); |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 356 | if (!error) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 357 | return 0; |
| 358 | |
Tejun Heo | 03afed8 | 2011-11-21 12:32:24 -0800 | [diff] [blame] | 359 | suspend_stats.failed_freeze++; |
| 360 | dpm_save_failed_step(SUSPEND_FREEZE); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 361 | Finish: |
Lianwei Wang | ea00f4f | 2016-06-19 23:52:27 -0700 | [diff] [blame] | 362 | __pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 363 | pm_restore_console(); |
| 364 | return error; |
| 365 | } |
| 366 | |
| 367 | /* default implementation */ |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 368 | void __weak arch_suspend_disable_irqs(void) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 369 | { |
| 370 | local_irq_disable(); |
| 371 | } |
| 372 | |
| 373 | /* default implementation */ |
Gideon Israel Dsouza | 52f5684c | 2014-04-07 15:39:20 -0700 | [diff] [blame] | 374 | void __weak arch_suspend_enable_irqs(void) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 375 | { |
| 376 | local_irq_enable(); |
| 377 | } |
| 378 | |
| 379 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 380 | * suspend_enter - Make the system enter the given sleep state. |
| 381 | * @state: System sleep state to enter. |
| 382 | * @wakeup: Returns information that the sleep state should not be re-entered. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 383 | * |
MyungJoo Ham | 3b5fe85 | 2011-06-12 15:57:05 +0200 | [diff] [blame] | 384 | * This function should be called after devices have been suspended. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 385 | */ |
MyungJoo Ham | 3b5fe85 | 2011-06-12 15:57:05 +0200 | [diff] [blame] | 386 | static int suspend_enter(suspend_state_t state, bool *wakeup) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 387 | { |
| 388 | int error; |
| 389 | |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 390 | error = platform_suspend_prepare(state); |
| 391 | if (error) |
| 392 | goto Platform_finish; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 393 | |
Rafael J. Wysocki | 2a8a8ce | 2014-09-30 02:21:34 +0200 | [diff] [blame] | 394 | error = dpm_suspend_late(PMSG_SUSPEND); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 395 | if (error) { |
Rafael J. Wysocki | 142bce7 | 2017-07-21 14:50:48 +0200 | [diff] [blame] | 396 | pr_err("late suspend of devices failed\n"); |
Rafael J. Wysocki | ce44101 | 2010-07-07 23:43:45 +0200 | [diff] [blame] | 397 | goto Platform_finish; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 398 | } |
Rafael J. Wysocki | a8d46b9 | 2014-09-30 02:29:01 +0200 | [diff] [blame] | 399 | error = platform_suspend_prepare_late(state); |
| 400 | if (error) |
| 401 | goto Devices_early_resume; |
| 402 | |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 403 | if (state == PM_SUSPEND_TO_IDLE && pm_test_level != TEST_PLATFORM) { |
Rafael J. Wysocki | 8e6bcd9 | 2017-07-21 02:07:54 +0200 | [diff] [blame] | 404 | s2idle_loop(); |
| 405 | goto Platform_early_resume; |
| 406 | } |
| 407 | |
Rafael J. Wysocki | 2a8a8ce | 2014-09-30 02:21:34 +0200 | [diff] [blame] | 408 | error = dpm_suspend_noirq(PMSG_SUSPEND); |
| 409 | if (error) { |
Rafael J. Wysocki | 142bce7 | 2017-07-21 14:50:48 +0200 | [diff] [blame] | 410 | pr_err("noirq suspend of devices failed\n"); |
Rafael J. Wysocki | a8d46b9 | 2014-09-30 02:29:01 +0200 | [diff] [blame] | 411 | goto Platform_early_resume; |
Rafael J. Wysocki | 2a8a8ce | 2014-09-30 02:21:34 +0200 | [diff] [blame] | 412 | } |
Rafael J. Wysocki | ebc3e41 | 2014-09-30 02:22:24 +0200 | [diff] [blame] | 413 | error = platform_suspend_prepare_noirq(state); |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 414 | if (error) |
| 415 | goto Platform_wake; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 416 | |
Zhang Rui | 08605ac | 2013-03-27 03:36:10 +0000 | [diff] [blame] | 417 | if (suspend_test(TEST_PLATFORM)) |
| 418 | goto Platform_wake; |
| 419 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 420 | error = disable_nonboot_cpus(); |
| 421 | if (error || suspend_test(TEST_CPUS)) |
| 422 | goto Enable_cpus; |
| 423 | |
| 424 | arch_suspend_disable_irqs(); |
| 425 | BUG_ON(!irqs_disabled()); |
| 426 | |
Rafael J. Wysocki | 2e711c0 | 2011-04-26 19:15:07 +0200 | [diff] [blame] | 427 | error = syscore_suspend(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 428 | if (!error) { |
MyungJoo Ham | 3b5fe85 | 2011-06-12 15:57:05 +0200 | [diff] [blame] | 429 | *wakeup = pm_wakeup_pending(); |
| 430 | if (!(suspend_test(TEST_CORE) || *wakeup)) { |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 431 | trace_suspend_resume(TPS("machine_suspend"), |
| 432 | state, true); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 433 | error = suspend_ops->enter(state); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 434 | trace_suspend_resume(TPS("machine_suspend"), |
| 435 | state, false); |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 436 | events_check_enabled = false; |
Ruchi Kandoi | 6717673 | 2015-05-07 19:07:42 -0700 | [diff] [blame] | 437 | } else if (*wakeup) { |
| 438 | error = -EBUSY; |
Rafael J. Wysocki | c125e96 | 2010-07-05 22:43:53 +0200 | [diff] [blame] | 439 | } |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 440 | syscore_resume(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | arch_suspend_enable_irqs(); |
| 444 | BUG_ON(irqs_disabled()); |
| 445 | |
| 446 | Enable_cpus: |
| 447 | enable_nonboot_cpus(); |
| 448 | |
| 449 | Platform_wake: |
Rafael J. Wysocki | ebc3e41 | 2014-09-30 02:22:24 +0200 | [diff] [blame] | 450 | platform_resume_noirq(state); |
Rafael J. Wysocki | 2a8a8ce | 2014-09-30 02:21:34 +0200 | [diff] [blame] | 451 | dpm_resume_noirq(PMSG_RESUME); |
| 452 | |
Rafael J. Wysocki | a8d46b9 | 2014-09-30 02:29:01 +0200 | [diff] [blame] | 453 | Platform_early_resume: |
| 454 | platform_resume_early(state); |
| 455 | |
Rafael J. Wysocki | 2a8a8ce | 2014-09-30 02:21:34 +0200 | [diff] [blame] | 456 | Devices_early_resume: |
| 457 | dpm_resume_early(PMSG_RESUME); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 458 | |
Rafael J. Wysocki | ce44101 | 2010-07-07 23:43:45 +0200 | [diff] [blame] | 459 | Platform_finish: |
Rafael J. Wysocki | ebc3e41 | 2014-09-30 02:22:24 +0200 | [diff] [blame] | 460 | platform_resume_finish(state); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 461 | return error; |
| 462 | } |
| 463 | |
| 464 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 465 | * suspend_devices_and_enter - Suspend devices and enter system sleep state. |
| 466 | * @state: System sleep state to enter. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 467 | */ |
| 468 | int suspend_devices_and_enter(suspend_state_t state) |
| 469 | { |
| 470 | int error; |
MyungJoo Ham | 3b5fe85 | 2011-06-12 15:57:05 +0200 | [diff] [blame] | 471 | bool wakeup = false; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 472 | |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 473 | if (!sleep_state_supported(state)) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 474 | return -ENOSYS; |
| 475 | |
Florian Fainelli | bd8c9ba | 2017-07-17 17:19:25 -0700 | [diff] [blame] | 476 | pm_suspend_target_state = state; |
| 477 | |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 478 | error = platform_suspend_begin(state); |
| 479 | if (error) |
| 480 | goto Close; |
| 481 | |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 482 | suspend_console(); |
| 483 | suspend_test_start(); |
| 484 | error = dpm_suspend_start(PMSG_SUSPEND); |
| 485 | if (error) { |
Rafael J. Wysocki | 142bce7 | 2017-07-21 14:50:48 +0200 | [diff] [blame] | 486 | pr_err("Some devices failed to suspend, or early wake event detected\n"); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 487 | goto Recover_platform; |
| 488 | } |
| 489 | suspend_test_finish("suspend devices"); |
| 490 | if (suspend_test(TEST_DEVICES)) |
| 491 | goto Recover_platform; |
| 492 | |
MyungJoo Ham | 3b5fe85 | 2011-06-12 15:57:05 +0200 | [diff] [blame] | 493 | do { |
| 494 | error = suspend_enter(state, &wakeup); |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 495 | } while (!error && !wakeup && platform_suspend_again(state)); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 496 | |
| 497 | Resume_devices: |
| 498 | suspend_test_start(); |
| 499 | dpm_resume_end(PMSG_RESUME); |
| 500 | suspend_test_finish("resume devices"); |
Todd E Brandt | 0cadc70 | 2014-09-19 14:07:12 -0700 | [diff] [blame] | 501 | trace_suspend_resume(TPS("resume_console"), state, true); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 502 | resume_console(); |
Todd E Brandt | 0cadc70 | 2014-09-19 14:07:12 -0700 | [diff] [blame] | 503 | trace_suspend_resume(TPS("resume_console"), state, false); |
Rafael J. Wysocki | 1f0b638 | 2014-05-15 23:29:57 +0200 | [diff] [blame] | 504 | |
Rafael J. Wysocki | 8490fdf | 2014-07-23 00:57:53 +0200 | [diff] [blame] | 505 | Close: |
Rafael J. Wysocki | ebc3e41 | 2014-09-30 02:22:24 +0200 | [diff] [blame] | 506 | platform_resume_end(state); |
Florian Fainelli | bd8c9ba | 2017-07-17 17:19:25 -0700 | [diff] [blame] | 507 | pm_suspend_target_state = PM_SUSPEND_ON; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 508 | return error; |
| 509 | |
| 510 | Recover_platform: |
Rafael J. Wysocki | ebc3e41 | 2014-09-30 02:22:24 +0200 | [diff] [blame] | 511 | platform_recover(state); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 512 | goto Resume_devices; |
| 513 | } |
| 514 | |
| 515 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 516 | * suspend_finish - Clean up before finishing the suspend sequence. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 517 | * |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 518 | * Call platform code to clean up, restart processes, and free the console that |
| 519 | * we've allocated. This routine is not called for hibernation. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 520 | */ |
| 521 | static void suspend_finish(void) |
| 522 | { |
| 523 | suspend_thaw_processes(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 524 | pm_notifier_call_chain(PM_POST_SUSPEND); |
| 525 | pm_restore_console(); |
| 526 | } |
| 527 | |
| 528 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 529 | * enter_state - Do common work needed to enter system sleep state. |
| 530 | * @state: System sleep state to enter. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 531 | * |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 532 | * Make sure that no one else is trying to put the system into a sleep state. |
| 533 | * Fail if that's not the case. Otherwise, prepare for system suspend, make the |
| 534 | * system enter the given sleep state and clean up after wakeup. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 535 | */ |
Rafael J. Wysocki | 93e1ee4 | 2012-02-13 16:29:24 +0100 | [diff] [blame] | 536 | static int enter_state(suspend_state_t state) |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 537 | { |
| 538 | int error; |
| 539 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 540 | trace_suspend_resume(TPS("suspend_enter"), state, true); |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 541 | if (state == PM_SUSPEND_TO_IDLE) { |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 542 | #ifdef CONFIG_PM_DEBUG |
| 543 | if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) { |
Rafael J. Wysocki | 142bce7 | 2017-07-21 14:50:48 +0200 | [diff] [blame] | 544 | pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n"); |
Rafael J. Wysocki | 43e8317 | 2014-05-26 13:40:53 +0200 | [diff] [blame] | 545 | return -EAGAIN; |
| 546 | } |
| 547 | #endif |
| 548 | } else if (!valid_state(state)) { |
| 549 | return -EINVAL; |
| 550 | } |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 551 | if (!mutex_trylock(&pm_mutex)) |
| 552 | return -EBUSY; |
| 553 | |
Rafael J. Wysocki | 690cbb9 | 2017-08-10 00:13:07 +0200 | [diff] [blame] | 554 | if (state == PM_SUSPEND_TO_IDLE) |
Rafael J. Wysocki | f02f4f9 | 2017-08-10 00:13:56 +0200 | [diff] [blame^] | 555 | s2idle_begin(); |
Zhang Rui | 7e73c5ae | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 556 | |
Len Brown | 2fd77ff | 2015-07-31 12:46:17 -0400 | [diff] [blame] | 557 | #ifndef CONFIG_SUSPEND_SKIP_SYNC |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 558 | trace_suspend_resume(TPS("sync_filesystems"), 0, true); |
Rafael J. Wysocki | 142bce7 | 2017-07-21 14:50:48 +0200 | [diff] [blame] | 559 | pr_info("Syncing filesystems ... "); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 560 | sys_sync(); |
Jon Hunter | 1adb469 | 2016-10-21 16:24:09 +0100 | [diff] [blame] | 561 | pr_cont("done.\n"); |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 562 | trace_suspend_resume(TPS("sync_filesystems"), 0, false); |
Len Brown | 2fd77ff | 2015-07-31 12:46:17 -0400 | [diff] [blame] | 563 | #endif |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 564 | |
Rafael J. Wysocki | bebcdae | 2017-07-21 14:49:51 +0200 | [diff] [blame] | 565 | pm_pr_dbg("Preparing system for sleep (%s)\n", mem_sleep_labels[state]); |
Rafael J. Wysocki | ef25ba0 | 2015-10-07 00:49:34 +0200 | [diff] [blame] | 566 | pm_suspend_clear_flags(); |
Zhang Rui | 7e73c5ae | 2013-02-06 13:00:36 +0100 | [diff] [blame] | 567 | error = suspend_prepare(state); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 568 | if (error) |
| 569 | goto Unlock; |
| 570 | |
| 571 | if (suspend_test(TEST_FREEZER)) |
| 572 | goto Finish; |
| 573 | |
Todd E Brandt | bb3632c | 2014-06-06 05:40:17 -0700 | [diff] [blame] | 574 | trace_suspend_resume(TPS("suspend_enter"), state, false); |
Rafael J. Wysocki | bebcdae | 2017-07-21 14:49:51 +0200 | [diff] [blame] | 575 | pm_pr_dbg("Suspending system (%s)\n", mem_sleep_labels[state]); |
Rafael J. Wysocki | 8718647 | 2011-05-10 21:09:53 +0200 | [diff] [blame] | 576 | pm_restrict_gfp_mask(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 577 | error = suspend_devices_and_enter(state); |
Rafael J. Wysocki | 8718647 | 2011-05-10 21:09:53 +0200 | [diff] [blame] | 578 | pm_restore_gfp_mask(); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 579 | |
| 580 | Finish: |
Rafael J. Wysocki | 8d8b244 | 2017-07-19 02:38:44 +0200 | [diff] [blame] | 581 | pm_pr_dbg("Finishing wakeup.\n"); |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 582 | suspend_finish(); |
| 583 | Unlock: |
| 584 | mutex_unlock(&pm_mutex); |
| 585 | return error; |
| 586 | } |
| 587 | |
| 588 | /** |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 589 | * pm_suspend - Externally visible function for suspending the system. |
| 590 | * @state: System sleep state to enter. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 591 | * |
Rafael J. Wysocki | 55ae451 | 2012-02-13 16:29:14 +0100 | [diff] [blame] | 592 | * Check if the value of @state represents one of the supported states, |
| 593 | * execute enter_state() and update system suspend statistics. |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 594 | */ |
| 595 | int pm_suspend(suspend_state_t state) |
| 596 | { |
Rafael J. Wysocki | bc25cf5 | 2012-02-13 16:29:33 +0100 | [diff] [blame] | 597 | int error; |
| 598 | |
| 599 | if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX) |
| 600 | return -EINVAL; |
| 601 | |
Rafael J. Wysocki | 142bce7 | 2017-07-21 14:50:48 +0200 | [diff] [blame] | 602 | pr_info("suspend entry (%s)\n", mem_sleep_labels[state]); |
Rafael J. Wysocki | bc25cf5 | 2012-02-13 16:29:33 +0100 | [diff] [blame] | 603 | error = enter_state(state); |
| 604 | if (error) { |
| 605 | suspend_stats.fail++; |
| 606 | dpm_save_failed_errno(error); |
| 607 | } else { |
| 608 | suspend_stats.success++; |
ShuoX Liu | 2a77c46 | 2011-08-10 23:01:26 +0200 | [diff] [blame] | 609 | } |
Rafael J. Wysocki | 142bce7 | 2017-07-21 14:50:48 +0200 | [diff] [blame] | 610 | pr_info("suspend exit\n"); |
Rafael J. Wysocki | bc25cf5 | 2012-02-13 16:29:33 +0100 | [diff] [blame] | 611 | return error; |
Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 612 | } |
| 613 | EXPORT_SYMBOL(pm_suspend); |