blob: 53266b729fd9c785cc1f4461e0bc2a7c82aeb5a9 [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;
Ruchi Kandoi67176732015-05-07 19:07:42 -0700369 } else if (*wakeup) {
370 error = -EBUSY;
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200371 }
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100372 syscore_resume();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200373 }
374
375 arch_suspend_enable_irqs();
376 BUG_ON(irqs_disabled());
377
378 Enable_cpus:
379 enable_nonboot_cpus();
380
381 Platform_wake:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200382 platform_resume_noirq(state);
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200383 dpm_resume_noirq(PMSG_RESUME);
384
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200385 Platform_early_resume:
386 platform_resume_early(state);
387
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200388 Devices_early_resume:
389 dpm_resume_early(PMSG_RESUME);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200390
Rafael J. Wysockice441012010-07-07 23:43:45 +0200391 Platform_finish:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200392 platform_resume_finish(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200393 return error;
394}
395
396/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100397 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
398 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200399 */
400int suspend_devices_and_enter(suspend_state_t state)
401{
402 int error;
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200403 bool wakeup = false;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200404
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200405 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200406 return -ENOSYS;
407
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200408 error = platform_suspend_begin(state);
409 if (error)
410 goto Close;
411
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200412 suspend_console();
413 suspend_test_start();
414 error = dpm_suspend_start(PMSG_SUSPEND);
415 if (error) {
Bernie Thompson9350de062013-06-01 00:47:43 +0000416 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200417 goto Recover_platform;
418 }
419 suspend_test_finish("suspend devices");
420 if (suspend_test(TEST_DEVICES))
421 goto Recover_platform;
422
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200423 do {
424 error = suspend_enter(state, &wakeup);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200425 } while (!error && !wakeup && platform_suspend_again(state));
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200426
427 Resume_devices:
428 suspend_test_start();
429 dpm_resume_end(PMSG_RESUME);
430 suspend_test_finish("resume devices");
Todd E Brandt0cadc702014-09-19 14:07:12 -0700431 trace_suspend_resume(TPS("resume_console"), state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200432 resume_console();
Todd E Brandt0cadc702014-09-19 14:07:12 -0700433 trace_suspend_resume(TPS("resume_console"), state, false);
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +0200434
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200435 Close:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200436 platform_resume_end(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200437 return error;
438
439 Recover_platform:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200440 platform_recover(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200441 goto Resume_devices;
442}
443
444/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100445 * suspend_finish - Clean up before finishing the suspend sequence.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200446 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100447 * Call platform code to clean up, restart processes, and free the console that
448 * we've allocated. This routine is not called for hibernation.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200449 */
450static void suspend_finish(void)
451{
452 suspend_thaw_processes();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200453 pm_notifier_call_chain(PM_POST_SUSPEND);
454 pm_restore_console();
455}
456
457/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100458 * enter_state - Do common work needed to enter system sleep state.
459 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200460 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100461 * Make sure that no one else is trying to put the system into a sleep state.
462 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
463 * system enter the given sleep state and clean up after wakeup.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200464 */
Rafael J. Wysocki93e1ee42012-02-13 16:29:24 +0100465static int enter_state(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200466{
467 int error;
468
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700469 trace_suspend_resume(TPS("suspend_enter"), state, true);
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200470 if (state == PM_SUSPEND_FREEZE) {
471#ifdef CONFIG_PM_DEBUG
472 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
Rafael J. Wysockia9215042015-05-05 23:42:25 +0200473 pr_warning("PM: Unsupported test mode for suspend to idle,"
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200474 "please choose none/freezer/devices/platform.\n");
475 return -EAGAIN;
476 }
477#endif
478 } else if (!valid_state(state)) {
479 return -EINVAL;
480 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200481 if (!mutex_trylock(&pm_mutex))
482 return -EBUSY;
483
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100484 if (state == PM_SUSPEND_FREEZE)
485 freeze_begin();
486
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700487 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200488 printk(KERN_INFO "PM: Syncing filesystems ... ");
489 sys_sync();
490 printk("done.\n");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700491 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200492
Rafael J. Wysockia9215042015-05-05 23:42:25 +0200493 pr_debug("PM: Preparing system for sleep (%s)\n", pm_states[state]);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100494 error = suspend_prepare(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200495 if (error)
496 goto Unlock;
497
498 if (suspend_test(TEST_FREEZER))
499 goto Finish;
500
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700501 trace_suspend_resume(TPS("suspend_enter"), state, false);
Rafael J. Wysockia9215042015-05-05 23:42:25 +0200502 pr_debug("PM: Suspending system (%s)\n", pm_states[state]);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200503 pm_restrict_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200504 error = suspend_devices_and_enter(state);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200505 pm_restore_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200506
507 Finish:
508 pr_debug("PM: Finishing wakeup.\n");
509 suspend_finish();
510 Unlock:
511 mutex_unlock(&pm_mutex);
512 return error;
513}
514
515/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100516 * pm_suspend - Externally visible function for suspending the system.
517 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200518 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100519 * Check if the value of @state represents one of the supported states,
520 * execute enter_state() and update system suspend statistics.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200521 */
522int pm_suspend(suspend_state_t state)
523{
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100524 int error;
525
526 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
527 return -EINVAL;
528
529 error = enter_state(state);
530 if (error) {
531 suspend_stats.fail++;
532 dpm_save_failed_errno(error);
533 } else {
534 suspend_stats.success++;
ShuoX Liu2a77c462011-08-10 23:01:26 +0200535 }
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100536 return error;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200537}
538EXPORT_SYMBOL(pm_suspend);