blob: ef908c134b34f6d37542f07c6044639381c203bd [file] [log] [blame]
Rafael J. Wysockia9d70522009-06-10 01:27:12 +02001/*
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. Wysocki142bce72017-07-21 14:50:48 +020011#define pr_fmt(fmt) "PM: " fmt
12
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020013#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. Wysockif3f12532014-04-20 23:43:01 +020019#include <linux/cpuidle.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/gfp.h>
Matthew Garrettdd4c4f12010-05-28 16:32:14 -040021#include <linux/io.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
24#include <linux/mm.h>
25#include <linux/slab.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040026#include <linux/export.h>
Matthew Garrettdd4c4f12010-05-28 16:32:14 -040027#include <linux/suspend.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010028#include <linux/syscore_ops.h>
Sebastian Andrzej Siewior9c8cd6b2018-05-25 11:46:47 +020029#include <linux/swait.h>
Srivatsa S. Bhat443772d42012-06-16 15:30:45 +020030#include <linux/ftrace.h>
Jean Pihet938cfed2011-01-05 19:49:01 +010031#include <trace/events/power.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070032#include <linux/compiler.h>
Brian Norris1d4a9c12015-02-22 21:16:49 -080033#include <linux/moduleparam.h>
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020034
35#include "power.h"
36
Rafael J. Wysocki406e7932016-11-21 22:45:40 +010037const char * const pm_labels[] = {
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +020038 [PM_SUSPEND_TO_IDLE] = "freeze",
Rafael J. Wysocki406e7932016-11-21 22:45:40 +010039 [PM_SUSPEND_STANDBY] = "standby",
40 [PM_SUSPEND_MEM] = "mem",
41};
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +020042const char *pm_states[PM_SUSPEND_MAX];
Rafael J. Wysocki406e7932016-11-21 22:45:40 +010043static const char * const mem_sleep_labels[] = {
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +020044 [PM_SUSPEND_TO_IDLE] = "s2idle",
Rafael J. Wysocki406e7932016-11-21 22:45:40 +010045 [PM_SUSPEND_STANDBY] = "shallow",
46 [PM_SUSPEND_MEM] = "deep",
47};
48const char *mem_sleep_states[PM_SUSPEND_MAX];
49
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +020050suspend_state_t mem_sleep_current = PM_SUSPEND_TO_IDLE;
Rafael J. Wysockie870c6c82017-07-31 23:43:18 +020051suspend_state_t mem_sleep_default = PM_SUSPEND_MAX;
Florian Fainellibd8c9ba2017-07-17 17:19:25 -070052suspend_state_t pm_suspend_target_state;
53EXPORT_SYMBOL_GPL(pm_suspend_target_state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020054
Rafael J. Wysockief25ba02015-10-07 00:49:34 +020055unsigned int pm_suspend_global_flags;
56EXPORT_SYMBOL_GPL(pm_suspend_global_flags);
57
Lionel Debroux2f55ac02010-11-16 14:14:02 +010058static const struct platform_suspend_ops *suspend_ops;
Rafael J. Wysocki23d58552017-08-10 00:15:30 +020059static const struct platform_s2idle_ops *s2idle_ops;
Sebastian Andrzej Siewior9c8cd6b2018-05-25 11:46:47 +020060static DECLARE_SWAIT_QUEUE_HEAD(s2idle_wait_head);
Rafael J. Wysocki38106312015-02-12 23:33:15 +010061
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020062enum s2idle_states __read_mostly s2idle_state;
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +020063static DEFINE_RAW_SPINLOCK(s2idle_lock);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010064
Daniel Drake684bec12018-10-01 15:55:22 -070065bool pm_suspend_via_s2idle(void)
66{
67 return mem_sleep_current == PM_SUSPEND_TO_IDLE;
68}
69EXPORT_SYMBOL_GPL(pm_suspend_via_s2idle);
70
Rafael J. Wysocki23d58552017-08-10 00:15:30 +020071void s2idle_set_ops(const struct platform_s2idle_ops *ops)
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020072{
73 lock_system_sleep();
Rafael J. Wysocki23d58552017-08-10 00:15:30 +020074 s2idle_ops = ops;
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020075 unlock_system_sleep();
76}
77
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020078static void s2idle_begin(void)
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010079{
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020080 s2idle_state = S2IDLE_STATE_NONE;
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010081}
82
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020083static void s2idle_enter(void)
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010084{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +020085 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, true);
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +020086
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +020087 raw_spin_lock_irq(&s2idle_lock);
Rafael J. Wysocki38106312015-02-12 23:33:15 +010088 if (pm_wakeup_pending())
89 goto out;
90
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +020091 s2idle_state = S2IDLE_STATE_ENTER;
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +020092 raw_spin_unlock_irq(&s2idle_lock);
Rafael J. Wysocki38106312015-02-12 23:33:15 +010093
94 get_online_cpus();
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020095 cpuidle_resume();
Rafael J. Wysocki38106312015-02-12 23:33:15 +010096
97 /* Push all the CPUs into the idle loop. */
98 wake_up_all_idle_cpus();
Rafael J. Wysocki38106312015-02-12 23:33:15 +010099 /* Make the current CPU wait so it can enter the idle loop too. */
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200100 swait_event_exclusive(s2idle_wait_head,
Sebastian Andrzej Siewior9c8cd6b2018-05-25 11:46:47 +0200101 s2idle_state == S2IDLE_STATE_WAKE);
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100102
Rafael J. Wysockif3f12532014-04-20 23:43:01 +0200103 cpuidle_pause();
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100104 put_online_cpus();
105
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200106 raw_spin_lock_irq(&s2idle_lock);
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100107
108 out:
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200109 s2idle_state = S2IDLE_STATE_NONE;
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200110 raw_spin_unlock_irq(&s2idle_lock);
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200111
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200112 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE, false);
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200113}
114
115static void s2idle_loop(void)
116{
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200117 pm_pr_dbg("suspend-to-idle\n");
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200118
Rafael J. Wysocki9a3ebe32017-07-21 02:12:10 +0200119 for (;;) {
120 int error;
121
122 dpm_noirq_begin();
123
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200124 /*
125 * Suspend-to-idle equals
126 * frozen processes + suspended devices + idle processors.
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200127 * Thus s2idle_enter() should be called right after
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200128 * all devices have been suspended.
Rafael J. Wysocki87cbde82017-09-28 01:45:10 +0200129 *
130 * Wakeups during the noirq suspend of devices may be spurious,
131 * so prevent them from terminating the loop right away.
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200132 */
Rafael J. Wysocki9a3ebe32017-07-21 02:12:10 +0200133 error = dpm_noirq_suspend_devices(PMSG_SUSPEND);
134 if (!error)
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200135 s2idle_enter();
Rafael J. Wysocki87cbde82017-09-28 01:45:10 +0200136 else if (error == -EBUSY && pm_wakeup_pending())
137 error = 0;
Rafael J. Wysocki9a3ebe32017-07-21 02:12:10 +0200138
Rafael J. Wysocki87cbde82017-09-28 01:45:10 +0200139 if (!error && s2idle_ops && s2idle_ops->wake)
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200140 s2idle_ops->wake();
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200141
Rafael J. Wysocki87cbde82017-09-28 01:45:10 +0200142 dpm_noirq_resume_devices(PMSG_RESUME);
143
Rafael J. Wysocki9a3ebe32017-07-21 02:12:10 +0200144 dpm_noirq_end();
145
Rafael J. Wysocki87cbde82017-09-28 01:45:10 +0200146 if (error)
147 break;
148
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200149 if (s2idle_ops && s2idle_ops->sync)
150 s2idle_ops->sync();
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200151
152 if (pm_wakeup_pending())
153 break;
154
155 pm_wakeup_clear(false);
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200156 }
Rafael J. Wysocki33e4f802017-06-12 22:56:34 +0200157
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200158 pm_pr_dbg("resume from suspend-to-idle\n");
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100159}
160
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200161void s2idle_wake(void)
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100162{
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100163 unsigned long flags;
164
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200165 raw_spin_lock_irqsave(&s2idle_lock, flags);
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200166 if (s2idle_state > S2IDLE_STATE_NONE) {
167 s2idle_state = S2IDLE_STATE_WAKE;
Peter Zijlstrab3dae102018-06-12 10:34:52 +0200168 swake_up_one(&s2idle_wait_head);
Rafael J. Wysocki38106312015-02-12 23:33:15 +0100169 }
Sebastian Andrzej Siewior62fc00a2018-05-25 11:46:48 +0200170 raw_spin_unlock_irqrestore(&s2idle_lock, flags);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100171}
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200172EXPORT_SYMBOL_GPL(s2idle_wake);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100173
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200174static bool valid_state(suspend_state_t state)
175{
176 /*
177 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
178 * support and need to be valid to the low level
179 * implementation, no valid callback implies that none are valid.
180 */
181 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
182}
183
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100184void __init pm_states_init(void)
185{
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100186 /* "mem" and "freeze" are always present in /sys/power/state. */
187 pm_states[PM_SUSPEND_MEM] = pm_labels[PM_SUSPEND_MEM];
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200188 pm_states[PM_SUSPEND_TO_IDLE] = pm_labels[PM_SUSPEND_TO_IDLE];
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100189 /*
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100190 * Suspend-to-idle should be supported even without any suspend_ops,
191 * initialize mem_sleep_states[] accordingly here.
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100192 */
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200193 mem_sleep_states[PM_SUSPEND_TO_IDLE] = mem_sleep_labels[PM_SUSPEND_TO_IDLE];
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100194}
195
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100196static int __init mem_sleep_default_setup(char *str)
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200197{
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100198 suspend_state_t state;
199
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200200 for (state = PM_SUSPEND_TO_IDLE; state <= PM_SUSPEND_MEM; state++)
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100201 if (mem_sleep_labels[state] &&
202 !strcmp(str, mem_sleep_labels[state])) {
203 mem_sleep_default = state;
204 break;
205 }
206
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200207 return 1;
208}
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100209__setup("mem_sleep_default=", mem_sleep_default_setup);
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200210
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200211/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100212 * suspend_set_ops - Set the global suspend method table.
213 * @ops: Suspend operations to use.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200214 */
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100215void suspend_set_ops(const struct platform_suspend_ops *ops)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200216{
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100217 lock_system_sleep();
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200218
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200219 suspend_ops = ops;
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200220
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100221 if (valid_state(PM_SUSPEND_STANDBY)) {
222 mem_sleep_states[PM_SUSPEND_STANDBY] = mem_sleep_labels[PM_SUSPEND_STANDBY];
223 pm_states[PM_SUSPEND_STANDBY] = pm_labels[PM_SUSPEND_STANDBY];
224 if (mem_sleep_default == PM_SUSPEND_STANDBY)
225 mem_sleep_current = PM_SUSPEND_STANDBY;
226 }
227 if (valid_state(PM_SUSPEND_MEM)) {
228 mem_sleep_states[PM_SUSPEND_MEM] = mem_sleep_labels[PM_SUSPEND_MEM];
Rafael J. Wysockie870c6c82017-07-31 23:43:18 +0200229 if (mem_sleep_default >= PM_SUSPEND_MEM)
Rafael J. Wysocki406e7932016-11-21 22:45:40 +0100230 mem_sleep_current = PM_SUSPEND_MEM;
231 }
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200232
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100233 unlock_system_sleep();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200234}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200235EXPORT_SYMBOL_GPL(suspend_set_ops);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200236
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200237/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100238 * suspend_valid_only_mem - Generic memory-only valid callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200239 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100240 * Platform drivers that implement mem suspend only and only need to check for
241 * that in their .valid() callback can use this instead of rolling their own
242 * .valid() callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200243 */
244int suspend_valid_only_mem(suspend_state_t state)
245{
246 return state == PM_SUSPEND_MEM;
247}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200248EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200249
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200250static bool sleep_state_supported(suspend_state_t state)
251{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200252 return state == PM_SUSPEND_TO_IDLE || (suspend_ops && suspend_ops->enter);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200253}
254
255static int platform_suspend_prepare(suspend_state_t state)
256{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200257 return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare ?
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200258 suspend_ops->prepare() : 0;
259}
260
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200261static int platform_suspend_prepare_late(suspend_state_t state)
262{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200263 return state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->prepare ?
264 s2idle_ops->prepare() : 0;
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200265}
266
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200267static int platform_suspend_prepare_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200268{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200269 return state != PM_SUSPEND_TO_IDLE && suspend_ops->prepare_late ?
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200270 suspend_ops->prepare_late() : 0;
271}
272
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200273static void platform_resume_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200274{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200275 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->wake)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200276 suspend_ops->wake();
277}
278
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200279static void platform_resume_early(suspend_state_t state)
280{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200281 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->restore)
282 s2idle_ops->restore();
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200283}
284
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200285static void platform_resume_finish(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200286{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200287 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->finish)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200288 suspend_ops->finish();
289}
290
291static int platform_suspend_begin(suspend_state_t state)
292{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200293 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->begin)
294 return s2idle_ops->begin();
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100295 else if (suspend_ops && suspend_ops->begin)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200296 return suspend_ops->begin(state);
297 else
298 return 0;
299}
300
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200301static void platform_resume_end(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200302{
Rafael J. Wysocki23d58552017-08-10 00:15:30 +0200303 if (state == PM_SUSPEND_TO_IDLE && s2idle_ops && s2idle_ops->end)
304 s2idle_ops->end();
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100305 else if (suspend_ops && suspend_ops->end)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200306 suspend_ops->end();
307}
308
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200309static void platform_recover(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200310{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200311 if (state != PM_SUSPEND_TO_IDLE && suspend_ops->recover)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200312 suspend_ops->recover();
313}
314
315static bool platform_suspend_again(suspend_state_t state)
316{
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200317 return state != PM_SUSPEND_TO_IDLE && suspend_ops->suspend_again ?
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200318 suspend_ops->suspend_again() : false;
319}
320
Brian Norris1d4a9c12015-02-22 21:16:49 -0800321#ifdef CONFIG_PM_DEBUG
322static unsigned int pm_test_delay = 5;
323module_param(pm_test_delay, uint, 0644);
324MODULE_PARM_DESC(pm_test_delay,
325 "Number of seconds to wait before resuming from suspend test");
326#endif
327
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200328static int suspend_test(int level)
329{
330#ifdef CONFIG_PM_DEBUG
331 if (pm_test_level == level) {
saurabh22e09b32015-10-28 08:54:01 +0530332 pr_info("suspend debug: Waiting for %d second(s).\n",
Brian Norris1d4a9c12015-02-22 21:16:49 -0800333 pm_test_delay);
334 mdelay(pm_test_delay * 1000);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200335 return 1;
336 }
337#endif /* !CONFIG_PM_DEBUG */
338 return 0;
339}
340
341/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100342 * suspend_prepare - Prepare for entering system sleep state.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200343 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100344 * Common code run for every system sleep state that can be entered (except for
345 * hibernation). Run suspend notifiers, allocate the "suspend" console and
346 * freeze processes.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200347 */
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100348static int suspend_prepare(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200349{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700350 int error, nr_calls = 0;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200351
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200352 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200353 return -EPERM;
354
355 pm_prepare_console();
356
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700357 error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls);
358 if (error) {
359 nr_calls--;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200360 goto Finish;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700361 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200362
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700363 trace_suspend_resume(TPS("freeze_processes"), 0, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200364 error = suspend_freeze_processes();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700365 trace_suspend_resume(TPS("freeze_processes"), 0, false);
Tejun Heo03afed82011-11-21 12:32:24 -0800366 if (!error)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200367 return 0;
368
Tejun Heo03afed82011-11-21 12:32:24 -0800369 suspend_stats.failed_freeze++;
370 dpm_save_failed_step(SUSPEND_FREEZE);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200371 Finish:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700372 __pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200373 pm_restore_console();
374 return error;
375}
376
377/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700378void __weak arch_suspend_disable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200379{
380 local_irq_disable();
381}
382
383/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700384void __weak arch_suspend_enable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200385{
386 local_irq_enable();
387}
388
389/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100390 * suspend_enter - Make the system enter the given sleep state.
391 * @state: System sleep state to enter.
392 * @wakeup: Returns information that the sleep state should not be re-entered.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200393 *
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200394 * This function should be called after devices have been suspended.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200395 */
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200396static int suspend_enter(suspend_state_t state, bool *wakeup)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200397{
398 int error;
399
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200400 error = platform_suspend_prepare(state);
401 if (error)
402 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200403
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200404 error = dpm_suspend_late(PMSG_SUSPEND);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200405 if (error) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200406 pr_err("late suspend of devices failed\n");
Rafael J. Wysockice441012010-07-07 23:43:45 +0200407 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200408 }
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200409 error = platform_suspend_prepare_late(state);
410 if (error)
411 goto Devices_early_resume;
412
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200413 if (state == PM_SUSPEND_TO_IDLE && pm_test_level != TEST_PLATFORM) {
Rafael J. Wysocki8e6bcd92017-07-21 02:07:54 +0200414 s2idle_loop();
415 goto Platform_early_resume;
416 }
417
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200418 error = dpm_suspend_noirq(PMSG_SUSPEND);
419 if (error) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200420 pr_err("noirq suspend of devices failed\n");
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200421 goto Platform_early_resume;
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200422 }
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200423 error = platform_suspend_prepare_noirq(state);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200424 if (error)
425 goto Platform_wake;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200426
Zhang Rui08605ac2013-03-27 03:36:10 +0000427 if (suspend_test(TEST_PLATFORM))
428 goto Platform_wake;
429
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000430 error = suspend_disable_secondary_cpus();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200431 if (error || suspend_test(TEST_CPUS))
432 goto Enable_cpus;
433
434 arch_suspend_disable_irqs();
435 BUG_ON(!irqs_disabled());
436
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200437 system_state = SYSTEM_SUSPEND;
438
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200439 error = syscore_suspend();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200440 if (!error) {
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200441 *wakeup = pm_wakeup_pending();
442 if (!(suspend_test(TEST_CORE) || *wakeup)) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700443 trace_suspend_resume(TPS("machine_suspend"),
444 state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200445 error = suspend_ops->enter(state);
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700446 trace_suspend_resume(TPS("machine_suspend"),
447 state, false);
Ruchi Kandoi67176732015-05-07 19:07:42 -0700448 } else if (*wakeup) {
449 error = -EBUSY;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200450 }
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100451 syscore_resume();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200452 }
453
Thomas Gleixnerc1a957d2018-05-25 17:54:41 +0200454 system_state = SYSTEM_RUNNING;
455
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200456 arch_suspend_enable_irqs();
457 BUG_ON(irqs_disabled());
458
459 Enable_cpus:
Nicholas Piggin2f1a6fb2019-04-11 13:34:45 +1000460 suspend_enable_secondary_cpus();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200461
462 Platform_wake:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200463 platform_resume_noirq(state);
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200464 dpm_resume_noirq(PMSG_RESUME);
465
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200466 Platform_early_resume:
467 platform_resume_early(state);
468
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200469 Devices_early_resume:
470 dpm_resume_early(PMSG_RESUME);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200471
Rafael J. Wysockice441012010-07-07 23:43:45 +0200472 Platform_finish:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200473 platform_resume_finish(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200474 return error;
475}
476
477/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100478 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
479 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200480 */
481int suspend_devices_and_enter(suspend_state_t state)
482{
483 int error;
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200484 bool wakeup = false;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200485
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200486 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200487 return -ENOSYS;
488
Florian Fainellibd8c9ba2017-07-17 17:19:25 -0700489 pm_suspend_target_state = state;
490
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200491 error = platform_suspend_begin(state);
492 if (error)
493 goto Close;
494
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200495 suspend_console();
496 suspend_test_start();
497 error = dpm_suspend_start(PMSG_SUSPEND);
498 if (error) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200499 pr_err("Some devices failed to suspend, or early wake event detected\n");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200500 goto Recover_platform;
501 }
502 suspend_test_finish("suspend devices");
503 if (suspend_test(TEST_DEVICES))
504 goto Recover_platform;
505
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200506 do {
507 error = suspend_enter(state, &wakeup);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200508 } while (!error && !wakeup && platform_suspend_again(state));
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200509
510 Resume_devices:
511 suspend_test_start();
512 dpm_resume_end(PMSG_RESUME);
513 suspend_test_finish("resume devices");
Todd E Brandt0cadc702014-09-19 14:07:12 -0700514 trace_suspend_resume(TPS("resume_console"), state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200515 resume_console();
Todd E Brandt0cadc702014-09-19 14:07:12 -0700516 trace_suspend_resume(TPS("resume_console"), state, false);
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +0200517
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200518 Close:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200519 platform_resume_end(state);
Florian Fainellibd8c9ba2017-07-17 17:19:25 -0700520 pm_suspend_target_state = PM_SUSPEND_ON;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200521 return error;
522
523 Recover_platform:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200524 platform_recover(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200525 goto Resume_devices;
526}
527
528/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100529 * suspend_finish - Clean up before finishing the suspend sequence.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200530 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100531 * Call platform code to clean up, restart processes, and free the console that
532 * we've allocated. This routine is not called for hibernation.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200533 */
534static void suspend_finish(void)
535{
536 suspend_thaw_processes();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200537 pm_notifier_call_chain(PM_POST_SUSPEND);
538 pm_restore_console();
539}
540
541/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100542 * enter_state - Do common work needed to enter system sleep state.
543 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200544 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100545 * Make sure that no one else is trying to put the system into a sleep state.
546 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
547 * system enter the given sleep state and clean up after wakeup.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200548 */
Rafael J. Wysocki93e1ee42012-02-13 16:29:24 +0100549static int enter_state(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200550{
551 int error;
552
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700553 trace_suspend_resume(TPS("suspend_enter"), state, true);
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200554 if (state == PM_SUSPEND_TO_IDLE) {
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200555#ifdef CONFIG_PM_DEBUG
556 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200557 pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200558 return -EAGAIN;
559 }
560#endif
561 } else if (!valid_state(state)) {
562 return -EINVAL;
563 }
Pingfan Liu55f25032018-07-31 16:51:32 +0800564 if (!mutex_trylock(&system_transition_mutex))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200565 return -EBUSY;
566
Rafael J. Wysocki690cbb92017-08-10 00:13:07 +0200567 if (state == PM_SUSPEND_TO_IDLE)
Rafael J. Wysockif02f4f92017-08-10 00:13:56 +0200568 s2idle_begin();
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100569
Harry Panb5dee312019-02-25 20:36:41 +0800570 if (!IS_ENABLED(CONFIG_SUSPEND_SKIP_SYNC)) {
571 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
572 ksys_sync_helper();
573 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
574 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200575
Rafael J. Wysockibebcdae2017-07-21 14:49:51 +0200576 pm_pr_dbg("Preparing system for sleep (%s)\n", mem_sleep_labels[state]);
Rafael J. Wysockief25ba02015-10-07 00:49:34 +0200577 pm_suspend_clear_flags();
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100578 error = suspend_prepare(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200579 if (error)
580 goto Unlock;
581
582 if (suspend_test(TEST_FREEZER))
583 goto Finish;
584
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700585 trace_suspend_resume(TPS("suspend_enter"), state, false);
Rafael J. Wysockibebcdae2017-07-21 14:49:51 +0200586 pm_pr_dbg("Suspending system (%s)\n", mem_sleep_labels[state]);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200587 pm_restrict_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200588 error = suspend_devices_and_enter(state);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200589 pm_restore_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200590
591 Finish:
Rajat Jain95b982b2017-10-31 14:44:24 -0700592 events_check_enabled = false;
Rafael J. Wysocki8d8b2442017-07-19 02:38:44 +0200593 pm_pr_dbg("Finishing wakeup.\n");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200594 suspend_finish();
595 Unlock:
Pingfan Liu55f25032018-07-31 16:51:32 +0800596 mutex_unlock(&system_transition_mutex);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200597 return error;
598}
599
600/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100601 * pm_suspend - Externally visible function for suspending the system.
602 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200603 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100604 * Check if the value of @state represents one of the supported states,
605 * execute enter_state() and update system suspend statistics.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200606 */
607int pm_suspend(suspend_state_t state)
608{
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100609 int error;
610
611 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
612 return -EINVAL;
613
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200614 pr_info("suspend entry (%s)\n", mem_sleep_labels[state]);
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100615 error = enter_state(state);
616 if (error) {
617 suspend_stats.fail++;
618 dpm_save_failed_errno(error);
619 } else {
620 suspend_stats.success++;
ShuoX Liu2a77c462011-08-10 23:01:26 +0200621 }
Rafael J. Wysocki142bce72017-07-21 14:50:48 +0200622 pr_info("suspend exit\n");
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100623 return error;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200624}
625EXPORT_SYMBOL(pm_suspend);