blob: 3c57e206f3e89a2d16fbb706445cfbb315007f69 [file] [log] [blame]
Thomas Gleixner55716d22019-06-01 10:08:42 +02001// SPDX-License-Identifier: GPL-2.0-only
Rafael J. Wysockia9d70522009-06-10 01:27:12 +02002/*
3 * kernel/power/suspend.c - Suspend to RAM and standby functionality.
4 *
5 * Copyright (c) 2003 Patrick Mochel
6 * Copyright (c) 2003 Open Source Development Lab
7 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +02008 */
9
Rafael J. Wysocki142bce72017-07-21 14:50:48 +020010#define pr_fmt(fmt) "PM: " fmt
11
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020012#include <linux/string.h>
13#include <linux/delay.h>
14#include <linux/errno.h>
15#include <linux/init.h>
16#include <linux/console.h>
17#include <linux/cpu.h>
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020018#include <linux/cpuidle.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/gfp.h>
Matthew Garrettdd4c4f12010-05-28 16:32:14 -040020#include <linux/io.h>
21#include <linux/kernel.h>
22#include <linux/list.h>
23#include <linux/mm.h>
24#include <linux/slab.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040025#include <linux/export.h>
Matthew Garrettdd4c4f12010-05-28 16:32:14 -040026#include <linux/suspend.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010027#include <linux/syscore_ops.h>
Sebastian Andrzej Siewior9c8cd6b2018-05-25 11:46:47 +020028#include <linux/swait.h>
Srivatsa S. Bhat443772d42012-06-16 15:30:45 +020029#include <linux/ftrace.h>
Jean Pihet938cfed2011-01-05 19:49:01 +010030#include <trace/events/power.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070031#include <linux/compiler.h>
Brian Norris1d4a9c12015-02-22 21:16:49 -080032#include <linux/moduleparam.h>
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020033
34#include "power.h"
35
Rafael J. Wysocki406e7932016-11-21 22:45:40 +010036const char * const pm_labels[] = {
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +020037 [PM_SUSPEND_TO_IDLE] = "freeze",
Rafael J. Wysocki406e7932016-11-21 22:45:40 +010038 [PM_SUSPEND_STANDBY] = "standby",
39 [PM_SUSPEND_MEM] = "mem",
40};
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +020041const char *pm_states[PM_SUSPEND_MAX];
Rafael J. Wysocki406e7932016-11-21 22:45:40 +010042static const char * const mem_sleep_labels[] = {
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +020043 [PM_SUSPEND_TO_IDLE] = "s2idle",
Rafael J. Wysocki406e7932016-11-21 22:45:40 +010044 [PM_SUSPEND_STANDBY] = "shallow",
45 [PM_SUSPEND_MEM] = "deep",
46};
47const char *mem_sleep_states[PM_SUSPEND_MAX];
48
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +020049suspend_state_t mem_sleep_current = PM_SUSPEND_TO_IDLE;
Rafael J. Wysockie870c6c82017-07-31 23:43:18 +020050suspend_state_t mem_sleep_default = PM_SUSPEND_MAX;
Florian Fainellibd8c9ba2017-07-17 17:19:25 -070051suspend_state_t pm_suspend_target_state;
52EXPORT_SYMBOL_GPL(pm_suspend_target_state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020053
Rafael J. Wysockief25ba02015-10-07 00:49:34 +020054unsigned int pm_suspend_global_flags;
55EXPORT_SYMBOL_GPL(pm_suspend_global_flags);
56
Lionel Debroux2f55ac02010-11-16 14:14:02 +010057static const struct platform_suspend_ops *suspend_ops;
Rafael J. Wysocki23d58552017-08-10 00:15:30 +020058static const struct platform_s2idle_ops *s2idle_ops;
Sebastian Andrzej Siewior9c8cd6b2018-05-25 11:46:47 +020059static DECLARE_SWAIT_QUEUE_HEAD(s2idle_wait_head);
Rafael J. Wysocki38106312015-02-12 23:33:15 +010060
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020061enum s2idle_states __read_mostly s2idle_state;
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +020062static DEFINE_RAW_SPINLOCK(s2idle_lock);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010063
Daniel Drake684bec12018-10-01 15:55:22 -070064bool pm_suspend_via_s2idle(void)
65{
66 return mem_sleep_current == PM_SUSPEND_TO_IDLE;
67}
68EXPORT_SYMBOL_GPL(pm_suspend_via_s2idle);
69
Rafael J. Wysocki23d58552017-08-10 00:15:30 +020070void s2idle_set_ops(const struct platform_s2idle_ops *ops)
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020071{
72 lock_system_sleep();
Rafael J. Wysocki23d58552017-08-10 00:15:30 +020073 s2idle_ops = ops;
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020074 unlock_system_sleep();
75}
76
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020077static void s2idle_begin(void)
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010078{
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020079 s2idle_state = S2IDLE_STATE_NONE;
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010080}
81
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020082static void s2idle_enter(void)
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010083{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +020084 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, true);
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +020085
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +020086 raw_spin_lock_irq(&s2idle_lock);
Rafael J. Wysocki38106312015-02-12 23:33:15 +010087 if (pm_wakeup_pending())
88 goto out;
89
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020090 s2idle_state = S2IDLE_STATE_ENTER;
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +020091 raw_spin_unlock_irq(&s2idle_lock);
Rafael J. Wysocki38106312015-02-12 23:33:15 +010092
93 get_online_cpus();
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020094 cpuidle_resume();
Rafael J. Wysocki38106312015-02-12 23:33:15 +010095
96 /* Push all the CPUs into the idle loop. */
97 wake_up_all_idle_cpus();
Rafael J. Wysocki38106312015-02-12 23:33:15 +010098 /* Make the current CPU wait so it can enter the idle loop too. */
Peter Zijlstrab3dae102018-06-12 10:34:52 +020099 swait_event_exclusive(s2idle_wait_head,
Sebastian Andrzej Siewior9c8cd6b2018-05-25 11:46:47 +0200100 s2idle_state == S2IDLE_STATE_WAKE);
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100101
Rafael J. Wysockif3f12532014-04-20 23:43:01 +0200102 cpuidle_pause();
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100103 put_online_cpus();
104
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200105 raw_spin_lock_irq(&s2idle_lock);
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100106
107 out:
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200108 s2idle_state = S2IDLE_STATE_NONE;
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200109 raw_spin_unlock_irq(&s2idle_lock);
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200110
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200111 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, false);
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200112}
113
114static void s2idle_loop(void)
115{
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200116 pm_pr_dbg("suspend-to-idle\n");
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200117
Rafael J. Wysocki9a3ebe32017-07-21 02:12:10 +0200118 for (;;) {
119 int error;
120
121 dpm_noirq_begin();
122
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200123 /*
124 * Suspend-to-idle equals
125 * frozen processes + suspended devices + idle processors.
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200126 * Thus s2idle_enter() should be called right after
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200127 * all devices have been suspended.
Rafael J. Wysocki87cbde82017-09-28 01:45:10 +0200128 *
129 * Wakeups during the noirq suspend of devices may be spurious,
130 * so prevent them from terminating the loop right away.
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200131 */
Rafael J. Wysocki9a3ebe32017-07-21 02:12:10 +0200132 error = dpm_noirq_suspend_devices(PMSG_SUSPEND);
133 if (!error)
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200134 s2idle_enter();
Rafael J. Wysocki87cbde82017-09-28 01:45:10 +0200135 else if (error == -EBUSY && pm_wakeup_pending())
136 error = 0;
Rafael J. Wysocki9a3ebe32017-07-21 02:12:10 +0200137
Rafael J. Wysocki87cbde82017-09-28 01:45:10 +0200138 if (!error && s2idle_ops && s2idle_ops->wake)
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200139 s2idle_ops->wake();
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200140
Rafael J. Wysocki87cbde82017-09-28 01:45:10 +0200141 dpm_noirq_resume_devices(PMSG_RESUME);
142
Rafael J. Wysocki9a3ebe32017-07-21 02:12:10 +0200143 dpm_noirq_end();
144
Rafael J. Wysocki87cbde82017-09-28 01:45:10 +0200145 if (error)
146 break;
147
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200148 if (s2idle_ops && s2idle_ops->sync)
149 s2idle_ops->sync();
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200150
151 if (pm_wakeup_pending())
152 break;
153
154 pm_wakeup_clear(false);
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200155 }
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200156
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200157 pm_pr_dbg("resume from suspend-to-idle\n");
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100158}
159
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200160void s2idle_wake(void)
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100161{
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100162 unsigned long flags;
163
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200164 raw_spin_lock_irqsave(&s2idle_lock, flags);
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200165 if (s2idle_state > S2IDLE_STATE_NONE) {
166 s2idle_state = S2IDLE_STATE_WAKE;
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200167 swake_up_one(&s2idle_wait_head);
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100168 }
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200169 raw_spin_unlock_irqrestore(&s2idle_lock, flags);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100170}
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200171EXPORT_SYMBOL_GPL(s2idle_wake);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100172
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200173static bool valid_state(suspend_state_t state)
174{
175 /*
176 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
177 * support and need to be valid to the low level
178 * implementation, no valid callback implies that none are valid.
179 */
180 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
181}
182
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100183void __init pm_states_init(void)
184{
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100185 /* "mem" and "freeze" are always present in /sys/power/state. */
186 pm_states[PM_SUSPEND_MEM] = pm_labels[PM_SUSPEND_MEM];
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200187 pm_states[PM_SUSPEND_TO_IDLE] = pm_labels[PM_SUSPEND_TO_IDLE];
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100188 /*
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100189 * Suspend-to-idle should be supported even without any suspend_ops,
190 * initialize mem_sleep_states[] accordingly here.
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100191 */
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200192 mem_sleep_states[PM_SUSPEND_TO_IDLE] = mem_sleep_labels[PM_SUSPEND_TO_IDLE];
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100193}
194
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100195static int __init mem_sleep_default_setup(char *str)
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200196{
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100197 suspend_state_t state;
198
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200199 for (state = PM_SUSPEND_TO_IDLE; state <= PM_SUSPEND_MEM; state++)
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100200 if (mem_sleep_labels[state] &&
201 !strcmp(str, mem_sleep_labels[state])) {
202 mem_sleep_default = state;
203 break;
204 }
205
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200206 return 1;
207}
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100208__setup("mem_sleep_default=", mem_sleep_default_setup);
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200209
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200210/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100211 * suspend_set_ops - Set the global suspend method table.
212 * @ops: Suspend operations to use.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200213 */
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100214void suspend_set_ops(const struct platform_suspend_ops *ops)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200215{
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100216 lock_system_sleep();
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200217
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200218 suspend_ops = ops;
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200219
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100220 if (valid_state(PM_SUSPEND_STANDBY)) {
221 mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY];
222 pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY];
223 if (mem_sleep_default == PM_SUSPEND_STANDBY)
224 mem_sleep_current = PM_SUSPEND_STANDBY;
225 }
226 if (valid_state(PM_SUSPEND_MEM)) {
227 mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM];
Rafael J. Wysockie870c6c82017-07-31 23:43:18 +0200228 if (mem_sleep_default >= PM_SUSPEND_MEM)
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100229 mem_sleep_current = PM_SUSPEND_MEM;
230 }
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200231
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100232 unlock_system_sleep();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200233}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200234EXPORT_SYMBOL_GPL(suspend_set_ops);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200235
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200236/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100237 * suspend_valid_only_mem - Generic memory-only valid callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200238 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100239 * Platform drivers that implement mem suspend only and only need to check for
240 * that in their .valid() callback can use this instead of rolling their own
241 * .valid() callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200242 */
243int suspend_valid_only_mem(suspend_state_t state)
244{
245 return state == PM_SUSPEND_MEM;
246}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200247EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200248
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200249static bool sleep_state_supported(suspend_state_t state)
250{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200251 return state == PM_SUSPEND_TO_IDLE || (suspend_ops && suspend_ops->enter);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200252}
253
254static int platform_suspend_prepare(suspend_state_t state)
255{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200256 return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare ?
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200257 suspend_ops->prepare() : 0;
258}
259
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200260static int platform_suspend_prepare_late(suspend_state_t state)
261{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200262 return state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->prepare ?
263 s2idle_ops->prepare() : 0;
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200264}
265
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200266static int platform_suspend_prepare_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200267{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200268 return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare_late ?
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200269 suspend_ops->prepare_late() : 0;
270}
271
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200272static void platform_resume_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200273{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200274 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->wake)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200275 suspend_ops->wake();
276}
277
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200278static void platform_resume_early(suspend_state_t state)
279{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200280 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->restore)
281 s2idle_ops->restore();
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200282}
283
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200284static void platform_resume_finish(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200285{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200286 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->finish)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200287 suspend_ops->finish();
288}
289
290static int platform_suspend_begin(suspend_state_t state)
291{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200292 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->begin)
293 return s2idle_ops->begin();
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100294 else if (suspend_ops && suspend_ops->begin)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200295 return suspend_ops->begin(state);
296 else
297 return 0;
298}
299
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200300static void platform_resume_end(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200301{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200302 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->end)
303 s2idle_ops->end();
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100304 else if (suspend_ops && suspend_ops->end)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200305 suspend_ops->end();
306}
307
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200308static void platform_recover(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200309{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200310 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->recover)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200311 suspend_ops->recover();
312}
313
314static bool platform_suspend_again(suspend_state_t state)
315{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200316 return state != PM_SUSPEND_TO_IDLE && suspend_ops->suspend_again ?
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200317 suspend_ops->suspend_again() : false;
318}
319
Brian Norris1d4a9c12015-02-22 21:16:49 -0800320#ifdef CONFIG_PM_DEBUG
321static unsigned int pm_test_delay = 5;
322module_param(pm_test_delay, uint, 0644);
323MODULE_PARM_DESC(pm_test_delay,
324 "Number of seconds to wait before resuming from suspend test");
325#endif
326
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200327static int suspend_test(int level)
328{
329#ifdef CONFIG_PM_DEBUG
330 if (pm_test_level == level) {
saurabh22e09b32015-10-28 08:54:01 +0530331 pr_info("suspend debug: Waiting for %d second(s).\n",
Brian Norris1d4a9c12015-02-22 21:16:49 -0800332 pm_test_delay);
333 mdelay(pm_test_delay * 1000);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200334 return 1;
335 }
336#endif /* !CONFIG_PM_DEBUG */
337 return 0;
338}
339
340/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100341 * suspend_prepare - Prepare for entering system sleep state.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200342 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100343 * Common code run for every system sleep state that can be entered (except for
344 * hibernation). Run suspend notifiers, allocate the "suspend" console and
345 * freeze processes.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200346 */
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100347static int suspend_prepare(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200348{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700349 int error, nr_calls = 0;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200350
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200351 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200352 return -EPERM;
353
354 pm_prepare_console();
355
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700356 error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls);
357 if (error) {
358 nr_calls--;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200359 goto Finish;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700360 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200361
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700362 trace_suspend_resume(TPS("freeze_processes"), 0, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200363 error = suspend_freeze_processes();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700364 trace_suspend_resume(TPS("freeze_processes"), 0, false);
Tejun Heo03afed82011-11-21 12:32:24 -0800365 if (!error)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200366 return 0;
367
Tejun Heo03afed82011-11-21 12:32:24 -0800368 suspend_stats.failed_freeze++;
369 dpm_save_failed_step(SUSPEND_FREEZE);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200370 Finish:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700371 __pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200372 pm_restore_console();
373 return error;
374}
375
376/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700377void __weak arch_suspend_disable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200378{
379 local_irq_disable();
380}
381
382/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700383void __weak arch_suspend_enable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200384{
385 local_irq_enable();
386}
387
388/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100389 * suspend_enter - Make the system enter the given sleep state.
390 * @state: System sleep state to enter.
391 * @wakeup: Returns information that the sleep state should not be re-entered.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200392 *
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200393 * This function should be called after devices have been suspended.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200394 */
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200395static int suspend_enter(suspend_state_t state, bool *wakeup)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200396{
397 int error;
398
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200399 error = platform_suspend_prepare(state);
400 if (error)
401 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200402
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200403 error = dpm_suspend_late(PMSG_SUSPEND);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200404 if (error) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200405 pr_err("late suspend of devices failed\n");
Rafael J. Wysockice441012010-07-07 23:43:45 +0200406 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200407 }
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200408 error = platform_suspend_prepare_late(state);
409 if (error)
410 goto Devices_early_resume;
411
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200412 if (state == PM_SUSPEND_TO_IDLE && pm_test_level != TEST_PLATFORM) {
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200413 s2idle_loop();
414 goto Platform_early_resume;
415 }
416
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200417 error = dpm_suspend_noirq(PMSG_SUSPEND);
418 if (error) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200419 pr_err("noirq suspend of devices failed\n");
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200420 goto Platform_early_resume;
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200421 }
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200422 error = platform_suspend_prepare_noirq(state);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200423 if (error)
424 goto Platform_wake;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200425
Zhang Rui08605ac2013-03-27 03:36:10 +0000426 if (suspend_test(TEST_PLATFORM))
427 goto Platform_wake;
428
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000429 error = suspend_disable_secondary_cpus();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200430 if (error || suspend_test(TEST_CPUS))
431 goto Enable_cpus;
432
433 arch_suspend_disable_irqs();
434 BUG_ON(!irqs_disabled());
435
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200436 system_state = SYSTEM_SUSPEND;
437
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200438 error = syscore_suspend();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200439 if (!error) {
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200440 *wakeup = pm_wakeup_pending();
441 if (!(suspend_test(TEST_CORE) || *wakeup)) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700442 trace_suspend_resume(TPS("machine_suspend"),
443 state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200444 error = suspend_ops->enter(state);
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700445 trace_suspend_resume(TPS("machine_suspend"),
446 state, false);
Ruchi Kandoi67176732015-05-07 19:07:42 -0700447 } else if (*wakeup) {
448 error = -EBUSY;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200449 }
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100450 syscore_resume();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200451 }
452
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200453 system_state = SYSTEM_RUNNING;
454
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200455 arch_suspend_enable_irqs();
456 BUG_ON(irqs_disabled());
457
458 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000459 suspend_enable_secondary_cpus();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200460
461 Platform_wake:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200462 platform_resume_noirq(state);
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200463 dpm_resume_noirq(PMSG_RESUME);
464
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200465 Platform_early_resume:
466 platform_resume_early(state);
467
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200468 Devices_early_resume:
469 dpm_resume_early(PMSG_RESUME);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200470
Rafael J. Wysockice441012010-07-07 23:43:45 +0200471 Platform_finish:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200472 platform_resume_finish(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200473 return error;
474}
475
476/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100477 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
478 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200479 */
480int suspend_devices_and_enter(suspend_state_t state)
481{
482 int error;
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200483 bool wakeup = false;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200484
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200485 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200486 return -ENOSYS;
487
Florian Fainellibd8c9ba2017-07-17 17:19:25 -0700488 pm_suspend_target_state = state;
489
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200490 error = platform_suspend_begin(state);
491 if (error)
492 goto Close;
493
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200494 suspend_console();
495 suspend_test_start();
496 error = dpm_suspend_start(PMSG_SUSPEND);
497 if (error) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200498 pr_err("Some devices failed to suspend, or early wake event detected\n");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200499 goto Recover_platform;
500 }
501 suspend_test_finish("suspend devices");
502 if (suspend_test(TEST_DEVICES))
503 goto Recover_platform;
504
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200505 do {
506 error = suspend_enter(state, &wakeup);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200507 } while (!error && !wakeup && platform_suspend_again(state));
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200508
509 Resume_devices:
510 suspend_test_start();
511 dpm_resume_end(PMSG_RESUME);
512 suspend_test_finish("resume devices");
Todd E Brandt0cadc702014-09-19 14:07:12 -0700513 trace_suspend_resume(TPS("resume_console"), state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200514 resume_console();
Todd E Brandt0cadc702014-09-19 14:07:12 -0700515 trace_suspend_resume(TPS("resume_console"), state, false);
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +0200516
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200517 Close:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200518 platform_resume_end(state);
Florian Fainellibd8c9ba2017-07-17 17:19:25 -0700519 pm_suspend_target_state = PM_SUSPEND_ON;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200520 return error;
521
522 Recover_platform:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200523 platform_recover(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200524 goto Resume_devices;
525}
526
527/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100528 * suspend_finish - Clean up before finishing the suspend sequence.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200529 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100530 * Call platform code to clean up, restart processes, and free the console that
531 * we've allocated. This routine is not called for hibernation.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200532 */
533static void suspend_finish(void)
534{
535 suspend_thaw_processes();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200536 pm_notifier_call_chain(PM_POST_SUSPEND);
537 pm_restore_console();
538}
539
540/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100541 * enter_state - Do common work needed to enter system sleep state.
542 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200543 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100544 * Make sure that no one else is trying to put the system into a sleep state.
545 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
546 * system enter the given sleep state and clean up after wakeup.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200547 */
Rafael J. Wysocki93e1ee42012-02-13 16:29:24 +0100548static int enter_state(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200549{
550 int error;
551
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700552 trace_suspend_resume(TPS("suspend_enter"), state, true);
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200553 if (state == PM_SUSPEND_TO_IDLE) {
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200554#ifdef CONFIG_PM_DEBUG
555 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200556 pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200557 return -EAGAIN;
558 }
559#endif
560 } else if (!valid_state(state)) {
561 return -EINVAL;
562 }
Pingfan Liu55f25032018-07-31 16:51:32 +0800563 if (!mutex_trylock(&system_transition_mutex))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200564 return -EBUSY;
565
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200566 if (state == PM_SUSPEND_TO_IDLE)
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200567 s2idle_begin();
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100568
Harry Panb5dee312019-02-25 20:36:41 +0800569 if (!IS_ENABLED(CONFIG_SUSPEND_SKIP_SYNC)) {
570 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
571 ksys_sync_helper();
572 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
573 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200574
Rafael J. Wysockibebcdae2017-07-21 14:49:51 +0200575 pm_pr_dbg("Preparing system for sleep (%s)\n", mem_sleep_labels[state]);
Rafael J. Wysockief25ba02015-10-07 00:49:34 +0200576 pm_suspend_clear_flags();
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100577 error = suspend_prepare(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200578 if (error)
579 goto Unlock;
580
581 if (suspend_test(TEST_FREEZER))
582 goto Finish;
583
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700584 trace_suspend_resume(TPS("suspend_enter"), state, false);
Rafael J. Wysockibebcdae2017-07-21 14:49:51 +0200585 pm_pr_dbg("Suspending system (%s)\n", mem_sleep_labels[state]);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200586 pm_restrict_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200587 error = suspend_devices_and_enter(state);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200588 pm_restore_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200589
590 Finish:
Rajat Jain95b982b2017-10-31 14:44:24 -0700591 events_check_enabled = false;
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200592 pm_pr_dbg("Finishing wakeup.\n");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200593 suspend_finish();
594 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +0800595 mutex_unlock(&system_transition_mutex);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200596 return error;
597}
598
599/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100600 * pm_suspend - Externally visible function for suspending the system.
601 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200602 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100603 * Check if the value of @state represents one of the supported states,
604 * execute enter_state() and update system suspend statistics.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200605 */
606int pm_suspend(suspend_state_t state)
607{
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100608 int error;
609
610 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
611 return -EINVAL;
612
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200613 pr_info("suspend entry (%s)\n", mem_sleep_labels[state]);
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100614 error = enter_state(state);
615 if (error) {
616 suspend_stats.fail++;
617 dpm_save_failed_errno(error);
618 } else {
619 suspend_stats.success++;
ShuoX Liu2a77c462011-08-10 23:01:26 +0200620 }
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200621 pr_info("suspend exit\n");
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100622 return error;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200623}
624EXPORT_SYMBOL(pm_suspend);