blob: 2d0c99b3f34c85cfdb4dcacf2433a27303c09716 [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
11#include <linux/string.h>
12#include <linux/delay.h>
13#include <linux/errno.h>
14#include <linux/init.h>
15#include <linux/console.h>
16#include <linux/cpu.h>
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020017#include <linux/cpuidle.h>
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020018#include <linux/syscalls.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>
Srivatsa S. Bhat443772d42012-06-16 15:30:45 +020028#include <linux/ftrace.h>
Todd Poynorb9acbfe2012-05-29 17:33:56 -070029#include <linux/rtc.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>
Ruchi Kandoic425eda2014-10-29 10:36:27 -070033#include <linux/wakeup_reason.h>
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020034
35#include "power.h"
36
Rafael J. Wysocki62109b42014-09-03 01:21:03 +020037const char *pm_labels[] = { "mem", "standby", "freeze", NULL };
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +020038const char *pm_states[PM_SUSPEND_MAX];
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020039
Rafael J. Wysockief25ba02015-10-07 00:49:34 +020040unsigned int pm_suspend_global_flags;
41EXPORT_SYMBOL_GPL(pm_suspend_global_flags);
42
Lionel Debroux2f55ac02010-11-16 14:14:02 +010043static const struct platform_suspend_ops *suspend_ops;
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020044static const struct platform_freeze_ops *freeze_ops;
Zhang Rui7e73c5a2013-02-06 13:00:36 +010045static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head);
Rafael J. Wysocki38106312015-02-12 23:33:15 +010046
47enum freeze_state __read_mostly suspend_freeze_state;
48static DEFINE_SPINLOCK(suspend_freeze_lock);
Zhang Rui7e73c5a2013-02-06 13:00:36 +010049
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020050void freeze_set_ops(const struct platform_freeze_ops *ops)
51{
52 lock_system_sleep();
53 freeze_ops = ops;
54 unlock_system_sleep();
55}
56
Zhang Rui7e73c5a2013-02-06 13:00:36 +010057static void freeze_begin(void)
58{
Rafael J. Wysocki38106312015-02-12 23:33:15 +010059 suspend_freeze_state = FREEZE_STATE_NONE;
Zhang Rui7e73c5a2013-02-06 13:00:36 +010060}
61
62static void freeze_enter(void)
63{
Rafael J. Wysocki38106312015-02-12 23:33:15 +010064 spin_lock_irq(&suspend_freeze_lock);
65 if (pm_wakeup_pending())
66 goto out;
67
68 suspend_freeze_state = FREEZE_STATE_ENTER;
69 spin_unlock_irq(&suspend_freeze_lock);
70
71 get_online_cpus();
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020072 cpuidle_resume();
Rafael J. Wysocki38106312015-02-12 23:33:15 +010073
74 /* Push all the CPUs into the idle loop. */
75 wake_up_all_idle_cpus();
76 pr_debug("PM: suspend-to-idle\n");
77 /* Make the current CPU wait so it can enter the idle loop too. */
78 wait_event(suspend_freeze_wait_head,
79 suspend_freeze_state == FREEZE_STATE_WAKE);
80 pr_debug("PM: resume from suspend-to-idle\n");
81
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020082 cpuidle_pause();
Rafael J. Wysocki38106312015-02-12 23:33:15 +010083 put_online_cpus();
84
85 spin_lock_irq(&suspend_freeze_lock);
86
87 out:
88 suspend_freeze_state = FREEZE_STATE_NONE;
89 spin_unlock_irq(&suspend_freeze_lock);
Zhang Rui7e73c5a2013-02-06 13:00:36 +010090}
91
92void freeze_wake(void)
93{
Rafael J. Wysocki38106312015-02-12 23:33:15 +010094 unsigned long flags;
95
96 spin_lock_irqsave(&suspend_freeze_lock, flags);
97 if (suspend_freeze_state > FREEZE_STATE_NONE) {
98 suspend_freeze_state = FREEZE_STATE_WAKE;
99 wake_up(&suspend_freeze_wait_head);
100 }
101 spin_unlock_irqrestore(&suspend_freeze_lock, flags);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100102}
103EXPORT_SYMBOL_GPL(freeze_wake);
104
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200105static bool valid_state(suspend_state_t state)
106{
107 /*
108 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
109 * support and need to be valid to the low level
110 * implementation, no valid callback implies that none are valid.
111 */
112 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
113}
114
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200115/*
116 * If this is set, the "mem" label always corresponds to the deepest sleep state
117 * available, the "standby" label corresponds to the second deepest sleep state
118 * available (if any), and the "freeze" label corresponds to the remaining
119 * available sleep state (if there is one).
120 */
121static bool relative_states;
122
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100123void __init pm_states_init(void)
124{
125 /*
126 * freeze state should be supported even without any suspend_ops,
127 * initialize pm_states accordingly here
128 */
129 pm_states[PM_SUSPEND_FREEZE] = pm_labels[relative_states ? 0 : 2];
130}
131
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200132static int __init sleep_states_setup(char *str)
133{
134 relative_states = !strncmp(str, "1", 1);
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200135 return 1;
136}
137
138__setup("relative_sleep_states=", sleep_states_setup);
139
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200140/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100141 * suspend_set_ops - Set the global suspend method table.
142 * @ops: Suspend operations to use.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200143 */
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100144void suspend_set_ops(const struct platform_suspend_ops *ops)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200145{
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200146 suspend_state_t i;
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200147 int j = 0;
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200148
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100149 lock_system_sleep();
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200150
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200151 suspend_ops = ops;
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200152 for (i = PM_SUSPEND_MEM; i >= PM_SUSPEND_STANDBY; i--)
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200153 if (valid_state(i)) {
154 pm_states[i] = pm_labels[j++];
155 } else if (!relative_states) {
156 pm_states[i] = NULL;
157 j++;
158 }
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200159
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200160 pm_states[PM_SUSPEND_FREEZE] = pm_labels[j];
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200161
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100162 unlock_system_sleep();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200163}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200164EXPORT_SYMBOL_GPL(suspend_set_ops);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200165
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200166/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100167 * suspend_valid_only_mem - Generic memory-only valid callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200168 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100169 * Platform drivers that implement mem suspend only and only need to check for
170 * that in their .valid() callback can use this instead of rolling their own
171 * .valid() callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200172 */
173int suspend_valid_only_mem(suspend_state_t state)
174{
175 return state == PM_SUSPEND_MEM;
176}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200177EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200178
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200179static bool sleep_state_supported(suspend_state_t state)
180{
181 return state == PM_SUSPEND_FREEZE || (suspend_ops && suspend_ops->enter);
182}
183
184static int platform_suspend_prepare(suspend_state_t state)
185{
186 return state != PM_SUSPEND_FREEZE && suspend_ops->prepare ?
187 suspend_ops->prepare() : 0;
188}
189
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200190static int platform_suspend_prepare_late(suspend_state_t state)
191{
Dmitry Eremin-Solenikov403b9632014-11-08 19:17:13 +0300192 return state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->prepare ?
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200193 freeze_ops->prepare() : 0;
194}
195
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200196static int platform_suspend_prepare_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200197{
198 return state != PM_SUSPEND_FREEZE && suspend_ops->prepare_late ?
199 suspend_ops->prepare_late() : 0;
200}
201
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200202static void platform_resume_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200203{
204 if (state != PM_SUSPEND_FREEZE && suspend_ops->wake)
205 suspend_ops->wake();
206}
207
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200208static void platform_resume_early(suspend_state_t state)
209{
Dmitry Eremin-Solenikov403b9632014-11-08 19:17:13 +0300210 if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->restore)
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200211 freeze_ops->restore();
212}
213
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200214static void platform_resume_finish(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200215{
216 if (state != PM_SUSPEND_FREEZE && suspend_ops->finish)
217 suspend_ops->finish();
218}
219
220static int platform_suspend_begin(suspend_state_t state)
221{
222 if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->begin)
223 return freeze_ops->begin();
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100224 else if (suspend_ops && suspend_ops->begin)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200225 return suspend_ops->begin(state);
226 else
227 return 0;
228}
229
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200230static void platform_resume_end(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200231{
232 if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->end)
233 freeze_ops->end();
Sudeep Hollafa7fd6f2016-08-19 14:41:00 +0100234 else if (suspend_ops && suspend_ops->end)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200235 suspend_ops->end();
236}
237
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200238static void platform_recover(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200239{
240 if (state != PM_SUSPEND_FREEZE && suspend_ops->recover)
241 suspend_ops->recover();
242}
243
244static bool platform_suspend_again(suspend_state_t state)
245{
246 return state != PM_SUSPEND_FREEZE && suspend_ops->suspend_again ?
247 suspend_ops->suspend_again() : false;
248}
249
Brian Norris1d4a9c12015-02-22 21:16:49 -0800250#ifdef CONFIG_PM_DEBUG
251static unsigned int pm_test_delay = 5;
252module_param(pm_test_delay, uint, 0644);
253MODULE_PARM_DESC(pm_test_delay,
254 "Number of seconds to wait before resuming from suspend test");
255#endif
256
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200257static int suspend_test(int level)
258{
259#ifdef CONFIG_PM_DEBUG
260 if (pm_test_level == level) {
saurabh22e09b32015-10-28 08:54:01 +0530261 pr_info("suspend debug: Waiting for %d second(s).\n",
Brian Norris1d4a9c12015-02-22 21:16:49 -0800262 pm_test_delay);
263 mdelay(pm_test_delay * 1000);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200264 return 1;
265 }
266#endif /* !CONFIG_PM_DEBUG */
267 return 0;
268}
269
270/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100271 * suspend_prepare - Prepare for entering system sleep state.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200272 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100273 * Common code run for every system sleep state that can be entered (except for
274 * hibernation). Run suspend notifiers, allocate the "suspend" console and
275 * freeze processes.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200276 */
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100277static int suspend_prepare(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200278{
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700279 int error, nr_calls = 0;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200280
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200281 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200282 return -EPERM;
283
284 pm_prepare_console();
285
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700286 error = __pm_notifier_call_chain(PM_SUSPEND_PREPARE, -1, &nr_calls);
287 if (error) {
288 nr_calls--;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200289 goto Finish;
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700290 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200291
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700292 trace_suspend_resume(TPS("freeze_processes"), 0, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200293 error = suspend_freeze_processes();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700294 trace_suspend_resume(TPS("freeze_processes"), 0, false);
Tejun Heo03afed82011-11-21 12:32:24 -0800295 if (!error)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200296 return 0;
297
Tejun Heo03afed82011-11-21 12:32:24 -0800298 suspend_stats.failed_freeze++;
299 dpm_save_failed_step(SUSPEND_FREEZE);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200300 Finish:
Lianwei Wangea00f4f2016-06-19 23:52:27 -0700301 __pm_notifier_call_chain(PM_POST_SUSPEND, nr_calls, NULL);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200302 pm_restore_console();
303 return error;
304}
305
306/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700307void __weak arch_suspend_disable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200308{
309 local_irq_disable();
310}
311
312/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700313void __weak arch_suspend_enable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200314{
315 local_irq_enable();
316}
317
318/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100319 * suspend_enter - Make the system enter the given sleep state.
320 * @state: System sleep state to enter.
321 * @wakeup: Returns information that the sleep state should not be re-entered.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200322 *
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200323 * This function should be called after devices have been suspended.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200324 */
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200325static int suspend_enter(suspend_state_t state, bool *wakeup)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200326{
Ruchi Kandoic425eda2014-10-29 10:36:27 -0700327 char suspend_abort[MAX_SUSPEND_ABORT_LEN];
328 int error, last_dev;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200329
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200330 error = platform_suspend_prepare(state);
331 if (error)
332 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200333
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200334 error = dpm_suspend_late(PMSG_SUSPEND);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200335 if (error) {
Ruchi Kandoic425eda2014-10-29 10:36:27 -0700336 last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
337 last_dev %= REC_FAILED_NUM;
saurabh22e09b32015-10-28 08:54:01 +0530338 pr_err("PM: late suspend of devices failed\n");
Ruchi Kandoic425eda2014-10-29 10:36:27 -0700339 log_suspend_abort_reason("%s device failed to power down",
340 suspend_stats.failed_devs[last_dev]);
Rafael J. Wysockice441012010-07-07 23:43:45 +0200341 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200342 }
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200343 error = platform_suspend_prepare_late(state);
344 if (error)
345 goto Devices_early_resume;
346
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200347 error = dpm_suspend_noirq(PMSG_SUSPEND);
348 if (error) {
Ruchi Kandoic425eda2014-10-29 10:36:27 -0700349 last_dev = suspend_stats.last_failed_dev + REC_FAILED_NUM - 1;
350 last_dev %= REC_FAILED_NUM;
saurabh22e09b32015-10-28 08:54:01 +0530351 pr_err("PM: noirq suspend of devices failed\n");
Ruchi Kandoic425eda2014-10-29 10:36:27 -0700352 log_suspend_abort_reason("noirq suspend of %s device failed",
353 suspend_stats.failed_devs[last_dev]);
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200354 goto Platform_early_resume;
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200355 }
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200356 error = platform_suspend_prepare_noirq(state);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200357 if (error)
358 goto Platform_wake;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200359
Zhang Rui08605ac2013-03-27 03:36:10 +0000360 if (suspend_test(TEST_PLATFORM))
361 goto Platform_wake;
362
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100363 /*
364 * PM_SUSPEND_FREEZE equals
365 * frozen processes + suspended devices + idle processors.
366 * Thus we should invoke freeze_enter() soon after
367 * all the devices are suspended.
368 */
369 if (state == PM_SUSPEND_FREEZE) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700370 trace_suspend_resume(TPS("machine_suspend"), state, true);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100371 freeze_enter();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700372 trace_suspend_resume(TPS("machine_suspend"), state, false);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100373 goto Platform_wake;
374 }
375
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200376 error = disable_nonboot_cpus();
Ruchi Kandoic425eda2014-10-29 10:36:27 -0700377 if (error || suspend_test(TEST_CPUS)) {
378 log_suspend_abort_reason("Disabling non-boot cpus failed");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200379 goto Enable_cpus;
Ruchi Kandoic425eda2014-10-29 10:36:27 -0700380 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200381
382 arch_suspend_disable_irqs();
383 BUG_ON(!irqs_disabled());
384
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200385 error = syscore_suspend();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200386 if (!error) {
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200387 *wakeup = pm_wakeup_pending();
388 if (!(suspend_test(TEST_CORE) || *wakeup)) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700389 trace_suspend_resume(TPS("machine_suspend"),
390 state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200391 error = suspend_ops->enter(state);
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700392 trace_suspend_resume(TPS("machine_suspend"),
393 state, false);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200394 events_check_enabled = false;
Ruchi Kandoi67176732015-05-07 19:07:42 -0700395 } else if (*wakeup) {
Ruchi Kandoic425eda2014-10-29 10:36:27 -0700396 pm_get_active_wakeup_sources(suspend_abort,
397 MAX_SUSPEND_ABORT_LEN);
398 log_suspend_abort_reason(suspend_abort);
Ruchi Kandoi67176732015-05-07 19:07:42 -0700399 error = -EBUSY;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200400 }
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100401 syscore_resume();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200402 }
403
404 arch_suspend_enable_irqs();
405 BUG_ON(irqs_disabled());
406
407 Enable_cpus:
408 enable_nonboot_cpus();
409
410 Platform_wake:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200411 platform_resume_noirq(state);
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200412 dpm_resume_noirq(PMSG_RESUME);
413
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200414 Platform_early_resume:
415 platform_resume_early(state);
416
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200417 Devices_early_resume:
418 dpm_resume_early(PMSG_RESUME);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200419
Rafael J. Wysockice441012010-07-07 23:43:45 +0200420 Platform_finish:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200421 platform_resume_finish(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200422 return error;
423}
424
425/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100426 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
427 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200428 */
429int suspend_devices_and_enter(suspend_state_t state)
430{
431 int error;
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200432 bool wakeup = false;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200433
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200434 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200435 return -ENOSYS;
436
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200437 error = platform_suspend_begin(state);
438 if (error)
439 goto Close;
440
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200441 suspend_console();
442 suspend_test_start();
443 error = dpm_suspend_start(PMSG_SUSPEND);
444 if (error) {
Bernie Thompson9350de062013-06-01 00:47:43 +0000445 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
Ruchi Kandoic425eda2014-10-29 10:36:27 -0700446 log_suspend_abort_reason("Some devices failed to suspend, or early wake event detected");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200447 goto Recover_platform;
448 }
449 suspend_test_finish("suspend devices");
450 if (suspend_test(TEST_DEVICES))
451 goto Recover_platform;
452
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200453 do {
454 error = suspend_enter(state, &wakeup);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200455 } while (!error && !wakeup && platform_suspend_again(state));
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200456
457 Resume_devices:
458 suspend_test_start();
459 dpm_resume_end(PMSG_RESUME);
460 suspend_test_finish("resume devices");
Todd E Brandt0cadc702014-09-19 14:07:12 -0700461 trace_suspend_resume(TPS("resume_console"), state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200462 resume_console();
Todd E Brandt0cadc702014-09-19 14:07:12 -0700463 trace_suspend_resume(TPS("resume_console"), state, false);
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +0200464
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200465 Close:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200466 platform_resume_end(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200467 return error;
468
469 Recover_platform:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200470 platform_recover(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200471 goto Resume_devices;
472}
473
474/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100475 * suspend_finish - Clean up before finishing the suspend sequence.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200476 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100477 * Call platform code to clean up, restart processes, and free the console that
478 * we've allocated. This routine is not called for hibernation.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200479 */
480static void suspend_finish(void)
481{
482 suspend_thaw_processes();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200483 pm_notifier_call_chain(PM_POST_SUSPEND);
484 pm_restore_console();
485}
486
487/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100488 * enter_state - Do common work needed to enter system sleep state.
489 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200490 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100491 * Make sure that no one else is trying to put the system into a sleep state.
492 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
493 * system enter the given sleep state and clean up after wakeup.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200494 */
Rafael J. Wysocki93e1ee42012-02-13 16:29:24 +0100495static int enter_state(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200496{
497 int error;
498
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700499 trace_suspend_resume(TPS("suspend_enter"), state, true);
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200500 if (state == PM_SUSPEND_FREEZE) {
501#ifdef CONFIG_PM_DEBUG
502 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
Joe Perchesa395d6a2016-03-22 14:28:09 -0700503 pr_warn("PM: Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200504 return -EAGAIN;
505 }
506#endif
507 } else if (!valid_state(state)) {
508 return -EINVAL;
509 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200510 if (!mutex_trylock(&pm_mutex))
511 return -EBUSY;
512
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100513 if (state == PM_SUSPEND_FREEZE)
514 freeze_begin();
515
Len Brown2fd77ff2015-07-31 12:46:17 -0400516#ifndef CONFIG_SUSPEND_SKIP_SYNC
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700517 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
Jon Hunter1adb4692016-10-21 16:24:09 +0100518 pr_info("PM: Syncing filesystems ... ");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200519 sys_sync();
Jon Hunter1adb4692016-10-21 16:24:09 +0100520 pr_cont("done.\n");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700521 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
Len Brown2fd77ff2015-07-31 12:46:17 -0400522#endif
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200523
Rafael J. Wysockia9215042015-05-05 23:42:25 +0200524 pr_debug("PM: Preparing system for sleep (%s)\n", pm_states[state]);
Rafael J. Wysockief25ba02015-10-07 00:49:34 +0200525 pm_suspend_clear_flags();
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100526 error = suspend_prepare(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200527 if (error)
528 goto Unlock;
529
530 if (suspend_test(TEST_FREEZER))
531 goto Finish;
532
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700533 trace_suspend_resume(TPS("suspend_enter"), state, false);
Rafael J. Wysockia9215042015-05-05 23:42:25 +0200534 pr_debug("PM: Suspending system (%s)\n", pm_states[state]);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200535 pm_restrict_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200536 error = suspend_devices_and_enter(state);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200537 pm_restore_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200538
539 Finish:
540 pr_debug("PM: Finishing wakeup.\n");
541 suspend_finish();
542 Unlock:
543 mutex_unlock(&pm_mutex);
544 return error;
545}
546
Todd Poynorb9acbfe2012-05-29 17:33:56 -0700547static void pm_suspend_marker(char *annotation)
548{
549 struct timespec ts;
550 struct rtc_time tm;
551
552 getnstimeofday(&ts);
553 rtc_time_to_tm(ts.tv_sec, &tm);
554 pr_info("PM: suspend %s %d-%02d-%02d %02d:%02d:%02d.%09lu UTC\n",
555 annotation, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
556 tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
557}
558
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200559/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100560 * pm_suspend - Externally visible function for suspending the system.
561 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200562 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100563 * Check if the value of @state represents one of the supported states,
564 * execute enter_state() and update system suspend statistics.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200565 */
566int pm_suspend(suspend_state_t state)
567{
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100568 int error;
569
570 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
571 return -EINVAL;
572
Todd Poynorb9acbfe2012-05-29 17:33:56 -0700573 pm_suspend_marker("entry");
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100574 error = enter_state(state);
575 if (error) {
576 suspend_stats.fail++;
577 dpm_save_failed_errno(error);
578 } else {
579 suspend_stats.success++;
ShuoX Liu2a77c462011-08-10 23:01:26 +0200580 }
Todd Poynorb9acbfe2012-05-29 17:33:56 -0700581 pm_suspend_marker("exit");
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100582 return error;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200583}
584EXPORT_SYMBOL(pm_suspend);