blob: 80cc1f0f502b8591cd4cc41bc1f916b9e76a2076 [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
Rafael J. Wysockia6137342019-05-27 12:45:18 +020064/**
Rafael J. Wysocki0b385a02019-06-18 10:18:28 +020065 * pm_suspend_default_s2idle - Check if suspend-to-idle is the default suspend.
Rafael J. Wysockia6137342019-05-27 12:45:18 +020066 *
67 * Return 'true' if suspend-to-idle has been selected as the default system
68 * suspend method.
69 */
Rafael J. Wysocki0b385a02019-06-18 10:18:28 +020070bool pm_suspend_default_s2idle(void)
Daniel Drake684bec12018-10-01 15:55:22 -070071{
72 return mem_sleep_current == PM_SUSPEND_TO_IDLE;
73}
Rafael J. Wysocki0b385a02019-06-18 10:18:28 +020074EXPORT_SYMBOL_GPL(pm_suspend_default_s2idle);
Daniel Drake684bec12018-10-01 15:55:22 -070075
Rafael J. Wysocki23d58552017-08-10 00:15:30 +020076void s2idle_set_ops(const struct platform_s2idle_ops *ops)
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020077{
78 lock_system_sleep();
Rafael J. Wysocki23d58552017-08-10 00:15:30 +020079 s2idle_ops = ops;
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020080 unlock_system_sleep();
81}
82
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020083static void s2idle_begin(void)
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010084{
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020085 s2idle_state = S2IDLE_STATE_NONE;
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010086}
87
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020088static void s2idle_enter(void)
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010089{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +020090 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, true);
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +020091
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +020092 raw_spin_lock_irq(&s2idle_lock);
Rafael J. Wysocki38106312015-02-12 23:33:15 +010093 if (pm_wakeup_pending())
94 goto out;
95
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020096 s2idle_state = S2IDLE_STATE_ENTER;
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +020097 raw_spin_unlock_irq(&s2idle_lock);
Rafael J. Wysocki38106312015-02-12 23:33:15 +010098
Sebastian Andrzej Siewiord2c8cce2021-08-03 16:16:13 +020099 cpus_read_lock();
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100100
101 /* Push all the CPUs into the idle loop. */
102 wake_up_all_idle_cpus();
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100103 /* Make the current CPU wait so it can enter the idle loop too. */
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200104 swait_event_exclusive(s2idle_wait_head,
Sebastian Andrzej Siewior9c8cd6b2018-05-25 11:46:47 +0200105 s2idle_state == S2IDLE_STATE_WAKE);
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100106
Sebastian Andrzej Siewiord2c8cce2021-08-03 16:16:13 +0200107 cpus_read_unlock();
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100108
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200109 raw_spin_lock_irq(&s2idle_lock);
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100110
111 out:
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200112 s2idle_state = S2IDLE_STATE_NONE;
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200113 raw_spin_unlock_irq(&s2idle_lock);
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200114
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200115 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, false);
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200116}
117
118static void s2idle_loop(void)
119{
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200120 pm_pr_dbg("suspend-to-idle\n");
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200121
Rafael J. Wysocki56b99182019-07-15 23:52:03 +0200122 /*
123 * Suspend-to-idle equals:
124 * frozen processes + suspended devices + idle processors.
125 * Thus s2idle_enter() should be called right after all devices have
126 * been suspended.
127 *
128 * Wakeups during the noirq suspend of devices may be spurious, so try
129 * to avoid them upfront.
130 */
Rafael J. Wysocki9a3ebe32017-07-21 02:12:10 +0200131 for (;;) {
Rafael J. Wysockie3728b52020-02-11 10:11:02 +0100132 if (s2idle_ops && s2idle_ops->wake) {
133 if (s2idle_ops->wake())
134 break;
135 } else if (pm_wakeup_pending()) {
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200136 break;
Rafael J. Wysockie3728b52020-02-11 10:11:02 +0100137 }
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200138
139 pm_wakeup_clear(false);
Rafael J. Wysocki56b99182019-07-15 23:52:03 +0200140
141 s2idle_enter();
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200142 }
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200143
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200144 pm_pr_dbg("resume from suspend-to-idle\n");
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100145}
146
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200147void s2idle_wake(void)
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100148{
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100149 unsigned long flags;
150
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200151 raw_spin_lock_irqsave(&s2idle_lock, flags);
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200152 if (s2idle_state > S2IDLE_STATE_NONE) {
153 s2idle_state = S2IDLE_STATE_WAKE;
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200154 swake_up_one(&s2idle_wait_head);
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100155 }
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200156 raw_spin_unlock_irqrestore(&s2idle_lock, flags);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100157}
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200158EXPORT_SYMBOL_GPL(s2idle_wake);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100159
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200160static bool valid_state(suspend_state_t state)
161{
162 /*
Rafael J. Wysocki9f6abfc2021-10-22 17:53:43 +0200163 * The PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states require low-level
164 * support and need to be valid to the low-level implementation.
165 *
166 * No ->valid() or ->enter() callback implies that none are valid.
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200167 */
Rafael J. Wysocki9f6abfc2021-10-22 17:53:43 +0200168 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state) &&
169 suspend_ops->enter;
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200170}
171
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100172void __init pm_states_init(void)
173{
Rafael J. Wysockic1bfc592021-10-19 20:55:04 +0200174 /* "mem" and "freeze" are always present in /sys/power/state. */
175 pm_states[PM_SUSPEND_MEM] = pm_labels[PM_SUSPEND_MEM];
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200176 pm_states[PM_SUSPEND_TO_IDLE] = pm_labels[PM_SUSPEND_TO_IDLE];
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100177 /*
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100178 * Suspend-to-idle should be supported even without any suspend_ops,
179 * initialize mem_sleep_states[] accordingly here.
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100180 */
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200181 mem_sleep_states[PM_SUSPEND_TO_IDLE] = mem_sleep_labels[PM_SUSPEND_TO_IDLE];
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100182}
183
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100184static int __init mem_sleep_default_setup(char *str)
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200185{
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100186 suspend_state_t state;
187
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200188 for (state = PM_SUSPEND_TO_IDLE; state <= PM_SUSPEND_MEM; state++)
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100189 if (mem_sleep_labels[state] &&
190 !strcmp(str, mem_sleep_labels[state])) {
191 mem_sleep_default = state;
192 break;
193 }
194
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200195 return 1;
196}
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100197__setup("mem_sleep_default=", mem_sleep_default_setup);
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200198
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200199/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100200 * suspend_set_ops - Set the global suspend method table.
201 * @ops: Suspend operations to use.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200202 */
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100203void suspend_set_ops(const struct platform_suspend_ops *ops)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200204{
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100205 lock_system_sleep();
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200206
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200207 suspend_ops = ops;
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200208
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100209 if (valid_state(PM_SUSPEND_STANDBY)) {
210 mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY];
211 pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY];
212 if (mem_sleep_default == PM_SUSPEND_STANDBY)
213 mem_sleep_current = PM_SUSPEND_STANDBY;
214 }
215 if (valid_state(PM_SUSPEND_MEM)) {
216 mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM];
Rafael J. Wysockie870c6c82017-07-31 23:43:18 +0200217 if (mem_sleep_default >= PM_SUSPEND_MEM)
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100218 mem_sleep_current = PM_SUSPEND_MEM;
219 }
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200220
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100221 unlock_system_sleep();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200222}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200223EXPORT_SYMBOL_GPL(suspend_set_ops);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200224
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200225/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100226 * suspend_valid_only_mem - Generic memory-only valid callback.
Alex Shiab150c32020-11-13 16:58:10 +0800227 * @state: Target system sleep state.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200228 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100229 * Platform drivers that implement mem suspend only and only need to check for
230 * that in their .valid() callback can use this instead of rolling their own
231 * .valid() callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200232 */
233int suspend_valid_only_mem(suspend_state_t state)
234{
235 return state == PM_SUSPEND_MEM;
236}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200237EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200238
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200239static bool sleep_state_supported(suspend_state_t state)
240{
Rafael J. Wysocki9f6abfc2021-10-22 17:53:43 +0200241 return state == PM_SUSPEND_TO_IDLE || valid_state(state);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200242}
243
244static int platform_suspend_prepare(suspend_state_t state)
245{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200246 return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare ?
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200247 suspend_ops->prepare() : 0;
248}
249
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200250static int platform_suspend_prepare_late(suspend_state_t state)
251{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200252 return state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->prepare ?
253 s2idle_ops->prepare() : 0;
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200254}
255
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200256static int platform_suspend_prepare_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200257{
Rafael J. Wysocki11f26632019-08-10 13:18:06 +0200258 if (state == PM_SUSPEND_TO_IDLE)
259 return s2idle_ops && s2idle_ops->prepare_late ?
260 s2idle_ops->prepare_late() : 0;
261
Rafael J. Wysockiac9eafb2019-08-01 19:31:10 +0200262 return suspend_ops->prepare_late ? suspend_ops->prepare_late() : 0;
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200263}
264
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200265static void platform_resume_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200266{
Rafael J. Wysockiac9eafb2019-08-01 19:31:10 +0200267 if (state == PM_SUSPEND_TO_IDLE) {
268 if (s2idle_ops && s2idle_ops->restore_early)
269 s2idle_ops->restore_early();
Rafael J. Wysocki11f26632019-08-10 13:18:06 +0200270 } else if (suspend_ops->wake) {
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200271 suspend_ops->wake();
Rafael J. Wysocki11f26632019-08-10 13:18:06 +0200272 }
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200273}
274
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200275static void platform_resume_early(suspend_state_t state)
276{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200277 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->restore)
278 s2idle_ops->restore();
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200279}
280
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200281static void platform_resume_finish(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200282{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200283 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->finish)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200284 suspend_ops->finish();
285}
286
287static int platform_suspend_begin(suspend_state_t state)
288{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200289 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->begin)
290 return s2idle_ops->begin();
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100291 else if (suspend_ops && suspend_ops->begin)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200292 return suspend_ops->begin(state);
293 else
294 return 0;
295}
296
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200297static void platform_resume_end(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200298{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200299 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->end)
300 s2idle_ops->end();
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100301 else if (suspend_ops && suspend_ops->end)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200302 suspend_ops->end();
303}
304
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200305static void platform_recover(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200306{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200307 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->recover)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200308 suspend_ops->recover();
309}
310
311static bool platform_suspend_again(suspend_state_t state)
312{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200313 return state != PM_SUSPEND_TO_IDLE && suspend_ops->suspend_again ?
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200314 suspend_ops->suspend_again() : false;
315}
316
Brian Norris1d4a9c12015-02-22 21:16:49 -0800317#ifdef CONFIG_PM_DEBUG
318static unsigned int pm_test_delay = 5;
319module_param(pm_test_delay, uint, 0644);
320MODULE_PARM_DESC(pm_test_delay,
321 "Number of seconds to wait before resuming from suspend test");
322#endif
323
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200324static int suspend_test(int level)
325{
326#ifdef CONFIG_PM_DEBUG
327 if (pm_test_level == level) {
saurabh22e09b32015-10-28 08:54:01 +0530328 pr_info("suspend debug: Waiting for %d second(s).\n",
Brian Norris1d4a9c12015-02-22 21:16:49 -0800329 pm_test_delay);
330 mdelay(pm_test_delay * 1000);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200331 return 1;
332 }
333#endif /* !CONFIG_PM_DEBUG */
334 return 0;
335}
336
337/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100338 * suspend_prepare - Prepare for entering system sleep state.
Alex Shiab150c32020-11-13 16:58:10 +0800339 * @state: Target system sleep state.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200340 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100341 * Common code run for every system sleep state that can be entered (except for
342 * hibernation). Run suspend notifiers, allocate the "suspend" console and
343 * freeze processes.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200344 */
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100345static int suspend_prepare(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200346{
Peter Zijlstra70d93292020-08-18 15:57:36 +0200347 int error;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200348
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200349 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200350 return -EPERM;
351
352 pm_prepare_console();
353
Peter Zijlstra70d93292020-08-18 15:57:36 +0200354 error = pm_notifier_call_chain_robust(PM_SUSPEND_PREPARE, PM_POST_SUSPEND);
355 if (error)
356 goto Restore;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200357
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700358 trace_suspend_resume(TPS("freeze_processes"), 0, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200359 error = suspend_freeze_processes();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700360 trace_suspend_resume(TPS("freeze_processes"), 0, false);
Tejun Heo03afed82011-11-21 12:32:24 -0800361 if (!error)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200362 return 0;
363
Tejun Heo03afed82011-11-21 12:32:24 -0800364 suspend_stats.failed_freeze++;
365 dpm_save_failed_step(SUSPEND_FREEZE);
Peter Zijlstra70d93292020-08-18 15:57:36 +0200366 pm_notifier_call_chain(PM_POST_SUSPEND);
367 Restore:
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200368 pm_restore_console();
369 return error;
370}
371
372/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700373void __weak arch_suspend_disable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200374{
375 local_irq_disable();
376}
377
378/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700379void __weak arch_suspend_enable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200380{
381 local_irq_enable();
382}
383
384/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100385 * suspend_enter - Make the system enter the given sleep state.
386 * @state: System sleep state to enter.
387 * @wakeup: Returns information that the sleep state should not be re-entered.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200388 *
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200389 * This function should be called after devices have been suspended.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200390 */
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200391static int suspend_enter(suspend_state_t state, bool *wakeup)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200392{
393 int error;
394
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200395 error = platform_suspend_prepare(state);
396 if (error)
397 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200398
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200399 error = dpm_suspend_late(PMSG_SUSPEND);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200400 if (error) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200401 pr_err("late suspend of devices failed\n");
Rafael J. Wysockice441012010-07-07 23:43:45 +0200402 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200403 }
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200404 error = platform_suspend_prepare_late(state);
405 if (error)
406 goto Devices_early_resume;
407
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200408 error = dpm_suspend_noirq(PMSG_SUSPEND);
409 if (error) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200410 pr_err("noirq suspend of devices failed\n");
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200411 goto Platform_early_resume;
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200412 }
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200413 error = platform_suspend_prepare_noirq(state);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200414 if (error)
415 goto Platform_wake;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200416
Zhang Rui08605ac2013-03-27 03:36:10 +0000417 if (suspend_test(TEST_PLATFORM))
418 goto Platform_wake;
419
Rafael J. Wysocki8eb0fd32019-07-15 23:52:12 +0200420 if (state == PM_SUSPEND_TO_IDLE) {
421 s2idle_loop();
422 goto Platform_wake;
423 }
424
Rafael J. Wysocki23f62d72021-10-22 18:07:47 +0200425 error = pm_sleep_disable_secondary_cpus();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200426 if (error || suspend_test(TEST_CPUS))
427 goto Enable_cpus;
428
429 arch_suspend_disable_irqs();
430 BUG_ON(!irqs_disabled());
431
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200432 system_state = SYSTEM_SUSPEND;
433
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200434 error = syscore_suspend();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200435 if (!error) {
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200436 *wakeup = pm_wakeup_pending();
437 if (!(suspend_test(TEST_CORE) || *wakeup)) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700438 trace_suspend_resume(TPS("machine_suspend"),
439 state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200440 error = suspend_ops->enter(state);
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700441 trace_suspend_resume(TPS("machine_suspend"),
442 state, false);
Ruchi Kandoi67176732015-05-07 19:07:42 -0700443 } else if (*wakeup) {
444 error = -EBUSY;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200445 }
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100446 syscore_resume();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200447 }
448
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200449 system_state = SYSTEM_RUNNING;
450
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200451 arch_suspend_enable_irqs();
452 BUG_ON(irqs_disabled());
453
454 Enable_cpus:
Rafael J. Wysocki23f62d72021-10-22 18:07:47 +0200455 pm_sleep_enable_secondary_cpus();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200456
457 Platform_wake:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200458 platform_resume_noirq(state);
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200459 dpm_resume_noirq(PMSG_RESUME);
460
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200461 Platform_early_resume:
462 platform_resume_early(state);
463
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200464 Devices_early_resume:
465 dpm_resume_early(PMSG_RESUME);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200466
Rafael J. Wysockice441012010-07-07 23:43:45 +0200467 Platform_finish:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200468 platform_resume_finish(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200469 return error;
470}
471
472/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100473 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
474 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200475 */
476int suspend_devices_and_enter(suspend_state_t state)
477{
478 int error;
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200479 bool wakeup = false;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200480
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200481 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200482 return -ENOSYS;
483
Florian Fainellibd8c9ba2017-07-17 17:19:25 -0700484 pm_suspend_target_state = state;
485
Rafael J. Wysocki471a7392019-06-26 00:20:23 +0200486 if (state == PM_SUSPEND_TO_IDLE)
487 pm_set_suspend_no_platform();
488
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200489 error = platform_suspend_begin(state);
490 if (error)
491 goto Close;
492
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200493 suspend_console();
494 suspend_test_start();
495 error = dpm_suspend_start(PMSG_SUSPEND);
496 if (error) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200497 pr_err("Some devices failed to suspend, or early wake event detected\n");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200498 goto Recover_platform;
499 }
500 suspend_test_finish("suspend devices");
501 if (suspend_test(TEST_DEVICES))
502 goto Recover_platform;
503
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200504 do {
505 error = suspend_enter(state, &wakeup);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200506 } while (!error && !wakeup && platform_suspend_again(state));
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200507
508 Resume_devices:
509 suspend_test_start();
510 dpm_resume_end(PMSG_RESUME);
511 suspend_test_finish("resume devices");
Todd E Brandt0cadc702014-09-19 14:07:12 -0700512 trace_suspend_resume(TPS("resume_console"), state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200513 resume_console();
Todd E Brandt0cadc702014-09-19 14:07:12 -0700514 trace_suspend_resume(TPS("resume_console"), state, false);
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +0200515
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200516 Close:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200517 platform_resume_end(state);
Florian Fainellibd8c9ba2017-07-17 17:19:25 -0700518 pm_suspend_target_state = PM_SUSPEND_ON;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200519 return error;
520
521 Recover_platform:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200522 platform_recover(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200523 goto Resume_devices;
524}
525
526/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100527 * suspend_finish - Clean up before finishing the suspend sequence.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200528 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100529 * Call platform code to clean up, restart processes, and free the console that
530 * we've allocated. This routine is not called for hibernation.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200531 */
532static void suspend_finish(void)
533{
534 suspend_thaw_processes();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200535 pm_notifier_call_chain(PM_POST_SUSPEND);
536 pm_restore_console();
537}
538
539/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100540 * enter_state - Do common work needed to enter system sleep state.
541 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200542 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100543 * Make sure that no one else is trying to put the system into a sleep state.
544 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
545 * system enter the given sleep state and clean up after wakeup.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200546 */
Rafael J. Wysocki93e1ee42012-02-13 16:29:24 +0100547static int enter_state(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200548{
549 int error;
550
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700551 trace_suspend_resume(TPS("suspend_enter"), state, true);
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200552 if (state == PM_SUSPEND_TO_IDLE) {
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200553#ifdef CONFIG_PM_DEBUG
554 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200555 pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200556 return -EAGAIN;
557 }
558#endif
559 } else if (!valid_state(state)) {
560 return -EINVAL;
561 }
Pingfan Liu55f25032018-07-31 16:51:32 +0800562 if (!mutex_trylock(&system_transition_mutex))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200563 return -EBUSY;
564
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200565 if (state == PM_SUSPEND_TO_IDLE)
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200566 s2idle_begin();
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100567
Jonas Meurerc052bf82020-01-16 12:53:54 +0100568 if (sync_on_suspend_enabled) {
Harry Panb5dee312019-02-25 20:36:41 +0800569 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
570 ksys_sync_helper();
571 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
572 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200573
Rafael J. Wysockibebcdae2017-07-21 14:49:51 +0200574 pm_pr_dbg("Preparing system for sleep (%s)\n", mem_sleep_labels[state]);
Rafael J. Wysockief25ba02015-10-07 00:49:34 +0200575 pm_suspend_clear_flags();
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100576 error = suspend_prepare(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200577 if (error)
578 goto Unlock;
579
580 if (suspend_test(TEST_FREEZER))
581 goto Finish;
582
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700583 trace_suspend_resume(TPS("suspend_enter"), state, false);
Rafael J. Wysockibebcdae2017-07-21 14:49:51 +0200584 pm_pr_dbg("Suspending system (%s)\n", mem_sleep_labels[state]);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200585 pm_restrict_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200586 error = suspend_devices_and_enter(state);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200587 pm_restore_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200588
589 Finish:
Rajat Jain95b982b2017-10-31 14:44:24 -0700590 events_check_enabled = false;
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200591 pm_pr_dbg("Finishing wakeup.\n");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200592 suspend_finish();
593 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +0800594 mutex_unlock(&system_transition_mutex);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200595 return error;
596}
597
598/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100599 * pm_suspend - Externally visible function for suspending the system.
600 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200601 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100602 * Check if the value of @state represents one of the supported states,
603 * execute enter_state() and update system suspend statistics.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200604 */
605int pm_suspend(suspend_state_t state)
606{
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100607 int error;
608
609 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
610 return -EINVAL;
611
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200612 pr_info("suspend entry (%s)\n", mem_sleep_labels[state]);
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100613 error = enter_state(state);
614 if (error) {
615 suspend_stats.fail++;
616 dpm_save_failed_errno(error);
617 } else {
618 suspend_stats.success++;
ShuoX Liu2a77c462011-08-10 23:01:26 +0200619 }
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200620 pr_info("suspend exit\n");
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100621 return error;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200622}
623EXPORT_SYMBOL(pm_suspend);