blob: 274371a71ed08f1c31d1ce44e89552aad0e86d07 [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>
Jean Pihet938cfed2011-01-05 19:49:01 +010029#include <trace/events/power.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070030#include <linux/compiler.h>
Brian Norris1d4a9c12015-02-22 21:16:49 -080031#include <linux/moduleparam.h>
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020032
33#include "power.h"
34
Rafael J. Wysocki62109b42014-09-03 01:21:03 +020035const char *pm_labels[] = { "mem", "standby", "freeze", NULL };
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +020036const char *pm_states[PM_SUSPEND_MAX];
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020037
Lionel Debroux2f55ac02010-11-16 14:14:02 +010038static const struct platform_suspend_ops *suspend_ops;
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020039static const struct platform_freeze_ops *freeze_ops;
Zhang Rui7e73c5a2013-02-06 13:00:36 +010040static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head);
Rafael J. Wysocki38106312015-02-12 23:33:15 +010041
42enum freeze_state __read_mostly suspend_freeze_state;
43static DEFINE_SPINLOCK(suspend_freeze_lock);
Zhang Rui7e73c5a2013-02-06 13:00:36 +010044
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020045void freeze_set_ops(const struct platform_freeze_ops *ops)
46{
47 lock_system_sleep();
48 freeze_ops = ops;
49 unlock_system_sleep();
50}
51
Zhang Rui7e73c5a2013-02-06 13:00:36 +010052static void freeze_begin(void)
53{
Rafael J. Wysocki38106312015-02-12 23:33:15 +010054 suspend_freeze_state = FREEZE_STATE_NONE;
Zhang Rui7e73c5a2013-02-06 13:00:36 +010055}
56
57static void freeze_enter(void)
58{
Rafael J. Wysocki38106312015-02-12 23:33:15 +010059 spin_lock_irq(&suspend_freeze_lock);
60 if (pm_wakeup_pending())
61 goto out;
62
63 suspend_freeze_state = FREEZE_STATE_ENTER;
64 spin_unlock_irq(&suspend_freeze_lock);
65
66 get_online_cpus();
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020067 cpuidle_resume();
Rafael J. Wysocki38106312015-02-12 23:33:15 +010068
69 /* Push all the CPUs into the idle loop. */
70 wake_up_all_idle_cpus();
71 pr_debug("PM: suspend-to-idle\n");
72 /* Make the current CPU wait so it can enter the idle loop too. */
73 wait_event(suspend_freeze_wait_head,
74 suspend_freeze_state == FREEZE_STATE_WAKE);
75 pr_debug("PM: resume from suspend-to-idle\n");
76
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020077 cpuidle_pause();
Rafael J. Wysocki38106312015-02-12 23:33:15 +010078 put_online_cpus();
79
80 spin_lock_irq(&suspend_freeze_lock);
81
82 out:
83 suspend_freeze_state = FREEZE_STATE_NONE;
84 spin_unlock_irq(&suspend_freeze_lock);
Zhang Rui7e73c5a2013-02-06 13:00:36 +010085}
86
87void freeze_wake(void)
88{
Rafael J. Wysocki38106312015-02-12 23:33:15 +010089 unsigned long flags;
90
91 spin_lock_irqsave(&suspend_freeze_lock, flags);
92 if (suspend_freeze_state > FREEZE_STATE_NONE) {
93 suspend_freeze_state = FREEZE_STATE_WAKE;
94 wake_up(&suspend_freeze_wait_head);
95 }
96 spin_unlock_irqrestore(&suspend_freeze_lock, flags);
Zhang Rui7e73c5a2013-02-06 13:00:36 +010097}
98EXPORT_SYMBOL_GPL(freeze_wake);
99
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200100static bool valid_state(suspend_state_t state)
101{
102 /*
103 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
104 * support and need to be valid to the low level
105 * implementation, no valid callback implies that none are valid.
106 */
107 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
108}
109
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200110/*
111 * If this is set, the "mem" label always corresponds to the deepest sleep state
112 * available, the "standby" label corresponds to the second deepest sleep state
113 * available (if any), and the "freeze" label corresponds to the remaining
114 * available sleep state (if there is one).
115 */
116static bool relative_states;
117
118static int __init sleep_states_setup(char *str)
119{
120 relative_states = !strncmp(str, "1", 1);
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200121 pm_states[PM_SUSPEND_FREEZE] = pm_labels[relative_states ? 0 : 2];
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200122 return 1;
123}
124
125__setup("relative_sleep_states=", sleep_states_setup);
126
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200127/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100128 * suspend_set_ops - Set the global suspend method table.
129 * @ops: Suspend operations to use.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200130 */
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100131void suspend_set_ops(const struct platform_suspend_ops *ops)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200132{
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200133 suspend_state_t i;
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200134 int j = 0;
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200135
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100136 lock_system_sleep();
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200137
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200138 suspend_ops = ops;
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200139 for (i = PM_SUSPEND_MEM; i >= PM_SUSPEND_STANDBY; i--)
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200140 if (valid_state(i)) {
141 pm_states[i] = pm_labels[j++];
142 } else if (!relative_states) {
143 pm_states[i] = NULL;
144 j++;
145 }
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200146
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200147 pm_states[PM_SUSPEND_FREEZE] = pm_labels[j];
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200148
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100149 unlock_system_sleep();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200150}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200151EXPORT_SYMBOL_GPL(suspend_set_ops);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200152
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200153/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100154 * suspend_valid_only_mem - Generic memory-only valid callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200155 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100156 * Platform drivers that implement mem suspend only and only need to check for
157 * that in their .valid() callback can use this instead of rolling their own
158 * .valid() callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200159 */
160int suspend_valid_only_mem(suspend_state_t state)
161{
162 return state == PM_SUSPEND_MEM;
163}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200164EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200165
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200166static bool sleep_state_supported(suspend_state_t state)
167{
168 return state == PM_SUSPEND_FREEZE || (suspend_ops && suspend_ops->enter);
169}
170
171static int platform_suspend_prepare(suspend_state_t state)
172{
173 return state != PM_SUSPEND_FREEZE && suspend_ops->prepare ?
174 suspend_ops->prepare() : 0;
175}
176
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200177static int platform_suspend_prepare_late(suspend_state_t state)
178{
Dmitry Eremin-Solenikov403b9632014-11-08 19:17:13 +0300179 return state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->prepare ?
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200180 freeze_ops->prepare() : 0;
181}
182
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200183static int platform_suspend_prepare_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200184{
185 return state != PM_SUSPEND_FREEZE && suspend_ops->prepare_late ?
186 suspend_ops->prepare_late() : 0;
187}
188
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200189static void platform_resume_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200190{
191 if (state != PM_SUSPEND_FREEZE && suspend_ops->wake)
192 suspend_ops->wake();
193}
194
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200195static void platform_resume_early(suspend_state_t state)
196{
Dmitry Eremin-Solenikov403b9632014-11-08 19:17:13 +0300197 if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->restore)
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200198 freeze_ops->restore();
199}
200
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200201static void platform_resume_finish(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200202{
203 if (state != PM_SUSPEND_FREEZE && suspend_ops->finish)
204 suspend_ops->finish();
205}
206
207static int platform_suspend_begin(suspend_state_t state)
208{
209 if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->begin)
210 return freeze_ops->begin();
211 else if (suspend_ops->begin)
212 return suspend_ops->begin(state);
213 else
214 return 0;
215}
216
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200217static void platform_resume_end(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200218{
219 if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->end)
220 freeze_ops->end();
221 else if (suspend_ops->end)
222 suspend_ops->end();
223}
224
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200225static void platform_recover(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200226{
227 if (state != PM_SUSPEND_FREEZE && suspend_ops->recover)
228 suspend_ops->recover();
229}
230
231static bool platform_suspend_again(suspend_state_t state)
232{
233 return state != PM_SUSPEND_FREEZE && suspend_ops->suspend_again ?
234 suspend_ops->suspend_again() : false;
235}
236
Brian Norris1d4a9c12015-02-22 21:16:49 -0800237#ifdef CONFIG_PM_DEBUG
238static unsigned int pm_test_delay = 5;
239module_param(pm_test_delay, uint, 0644);
240MODULE_PARM_DESC(pm_test_delay,
241 "Number of seconds to wait before resuming from suspend test");
242#endif
243
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200244static int suspend_test(int level)
245{
246#ifdef CONFIG_PM_DEBUG
247 if (pm_test_level == level) {
Brian Norris1d4a9c12015-02-22 21:16:49 -0800248 printk(KERN_INFO "suspend debug: Waiting for %d second(s).\n",
249 pm_test_delay);
250 mdelay(pm_test_delay * 1000);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200251 return 1;
252 }
253#endif /* !CONFIG_PM_DEBUG */
254 return 0;
255}
256
257/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100258 * suspend_prepare - Prepare for entering system sleep state.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200259 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100260 * Common code run for every system sleep state that can be entered (except for
261 * hibernation). Run suspend notifiers, allocate the "suspend" console and
262 * freeze processes.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200263 */
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100264static int suspend_prepare(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200265{
266 int error;
267
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200268 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200269 return -EPERM;
270
271 pm_prepare_console();
272
273 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
274 if (error)
275 goto Finish;
276
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700277 trace_suspend_resume(TPS("freeze_processes"), 0, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200278 error = suspend_freeze_processes();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700279 trace_suspend_resume(TPS("freeze_processes"), 0, false);
Tejun Heo03afed82011-11-21 12:32:24 -0800280 if (!error)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200281 return 0;
282
Tejun Heo03afed82011-11-21 12:32:24 -0800283 suspend_stats.failed_freeze++;
284 dpm_save_failed_step(SUSPEND_FREEZE);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200285 Finish:
286 pm_notifier_call_chain(PM_POST_SUSPEND);
287 pm_restore_console();
288 return error;
289}
290
291/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700292void __weak arch_suspend_disable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200293{
294 local_irq_disable();
295}
296
297/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700298void __weak arch_suspend_enable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200299{
300 local_irq_enable();
301}
302
303/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100304 * suspend_enter - Make the system enter the given sleep state.
305 * @state: System sleep state to enter.
306 * @wakeup: Returns information that the sleep state should not be re-entered.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200307 *
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200308 * This function should be called after devices have been suspended.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200309 */
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200310static int suspend_enter(suspend_state_t state, bool *wakeup)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200311{
312 int error;
313
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200314 error = platform_suspend_prepare(state);
315 if (error)
316 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200317
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200318 error = dpm_suspend_late(PMSG_SUSPEND);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200319 if (error) {
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200320 printk(KERN_ERR "PM: late suspend of devices failed\n");
Rafael J. Wysockice441012010-07-07 23:43:45 +0200321 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200322 }
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200323 error = platform_suspend_prepare_late(state);
324 if (error)
325 goto Devices_early_resume;
326
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200327 error = dpm_suspend_noirq(PMSG_SUSPEND);
328 if (error) {
329 printk(KERN_ERR "PM: noirq suspend of devices failed\n");
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200330 goto Platform_early_resume;
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200331 }
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200332 error = platform_suspend_prepare_noirq(state);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200333 if (error)
334 goto Platform_wake;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200335
Zhang Rui08605ac2013-03-27 03:36:10 +0000336 if (suspend_test(TEST_PLATFORM))
337 goto Platform_wake;
338
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100339 /*
340 * PM_SUSPEND_FREEZE equals
341 * frozen processes + suspended devices + idle processors.
342 * Thus we should invoke freeze_enter() soon after
343 * all the devices are suspended.
344 */
345 if (state == PM_SUSPEND_FREEZE) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700346 trace_suspend_resume(TPS("machine_suspend"), state, true);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100347 freeze_enter();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700348 trace_suspend_resume(TPS("machine_suspend"), state, false);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100349 goto Platform_wake;
350 }
351
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200352 error = disable_nonboot_cpus();
353 if (error || suspend_test(TEST_CPUS))
354 goto Enable_cpus;
355
356 arch_suspend_disable_irqs();
357 BUG_ON(!irqs_disabled());
358
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200359 error = syscore_suspend();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200360 if (!error) {
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200361 *wakeup = pm_wakeup_pending();
362 if (!(suspend_test(TEST_CORE) || *wakeup)) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700363 trace_suspend_resume(TPS("machine_suspend"),
364 state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200365 error = suspend_ops->enter(state);
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700366 trace_suspend_resume(TPS("machine_suspend"),
367 state, false);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200368 events_check_enabled = false;
369 }
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100370 syscore_resume();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200371 }
372
373 arch_suspend_enable_irqs();
374 BUG_ON(irqs_disabled());
375
376 Enable_cpus:
377 enable_nonboot_cpus();
378
379 Platform_wake:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200380 platform_resume_noirq(state);
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200381 dpm_resume_noirq(PMSG_RESUME);
382
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200383 Platform_early_resume:
384 platform_resume_early(state);
385
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200386 Devices_early_resume:
387 dpm_resume_early(PMSG_RESUME);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200388
Rafael J. Wysockice441012010-07-07 23:43:45 +0200389 Platform_finish:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200390 platform_resume_finish(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200391 return error;
392}
393
394/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100395 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
396 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200397 */
398int suspend_devices_and_enter(suspend_state_t state)
399{
400 int error;
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200401 bool wakeup = false;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200402
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200403 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200404 return -ENOSYS;
405
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200406 error = platform_suspend_begin(state);
407 if (error)
408 goto Close;
409
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200410 suspend_console();
411 suspend_test_start();
412 error = dpm_suspend_start(PMSG_SUSPEND);
413 if (error) {
Bernie Thompson9350de062013-06-01 00:47:43 +0000414 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200415 goto Recover_platform;
416 }
417 suspend_test_finish("suspend devices");
418 if (suspend_test(TEST_DEVICES))
419 goto Recover_platform;
420
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200421 do {
422 error = suspend_enter(state, &wakeup);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200423 } while (!error && !wakeup && platform_suspend_again(state));
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200424
425 Resume_devices:
426 suspend_test_start();
427 dpm_resume_end(PMSG_RESUME);
428 suspend_test_finish("resume devices");
Todd E Brandt0cadc702014-09-19 14:07:12 -0700429 trace_suspend_resume(TPS("resume_console"), state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200430 resume_console();
Todd E Brandt0cadc702014-09-19 14:07:12 -0700431 trace_suspend_resume(TPS("resume_console"), state, false);
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +0200432
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200433 Close:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200434 platform_resume_end(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200435 return error;
436
437 Recover_platform:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200438 platform_recover(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200439 goto Resume_devices;
440}
441
442/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100443 * suspend_finish - Clean up before finishing the suspend sequence.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200444 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100445 * Call platform code to clean up, restart processes, and free the console that
446 * we've allocated. This routine is not called for hibernation.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200447 */
448static void suspend_finish(void)
449{
450 suspend_thaw_processes();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200451 pm_notifier_call_chain(PM_POST_SUSPEND);
452 pm_restore_console();
453}
454
455/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100456 * enter_state - Do common work needed to enter system sleep state.
457 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200458 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100459 * Make sure that no one else is trying to put the system into a sleep state.
460 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
461 * system enter the given sleep state and clean up after wakeup.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200462 */
Rafael J. Wysocki93e1ee42012-02-13 16:29:24 +0100463static int enter_state(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200464{
465 int error;
466
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700467 trace_suspend_resume(TPS("suspend_enter"), state, true);
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200468 if (state == PM_SUSPEND_FREEZE) {
469#ifdef CONFIG_PM_DEBUG
470 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
Rafael J. Wysockia9215042015-05-05 23:42:25 +0200471 pr_warning("PM: Unsupported test mode for suspend to idle,"
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200472 "please choose none/freezer/devices/platform.\n");
473 return -EAGAIN;
474 }
475#endif
476 } else if (!valid_state(state)) {
477 return -EINVAL;
478 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200479 if (!mutex_trylock(&pm_mutex))
480 return -EBUSY;
481
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100482 if (state == PM_SUSPEND_FREEZE)
483 freeze_begin();
484
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700485 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200486 printk(KERN_INFO "PM: Syncing filesystems ... ");
487 sys_sync();
488 printk("done.\n");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700489 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200490
Rafael J. Wysockia9215042015-05-05 23:42:25 +0200491 pr_debug("PM: Preparing system for sleep (%s)\n", pm_states[state]);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100492 error = suspend_prepare(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200493 if (error)
494 goto Unlock;
495
496 if (suspend_test(TEST_FREEZER))
497 goto Finish;
498
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700499 trace_suspend_resume(TPS("suspend_enter"), state, false);
Rafael J. Wysockia9215042015-05-05 23:42:25 +0200500 pr_debug("PM: Suspending system (%s)\n", pm_states[state]);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200501 pm_restrict_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200502 error = suspend_devices_and_enter(state);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200503 pm_restore_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200504
505 Finish:
506 pr_debug("PM: Finishing wakeup.\n");
507 suspend_finish();
508 Unlock:
509 mutex_unlock(&pm_mutex);
510 return error;
511}
512
513/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100514 * pm_suspend - Externally visible function for suspending the system.
515 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200516 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100517 * Check if the value of @state represents one of the supported states,
518 * execute enter_state() and update system suspend statistics.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200519 */
520int pm_suspend(suspend_state_t state)
521{
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100522 int error;
523
524 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
525 return -EINVAL;
526
527 error = enter_state(state);
528 if (error) {
529 suspend_stats.fail++;
530 dpm_save_failed_errno(error);
531 } else {
532 suspend_stats.success++;
ShuoX Liu2a77c462011-08-10 23:01:26 +0200533 }
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100534 return error;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200535}
536EXPORT_SYMBOL(pm_suspend);