blob: 4ca9a33ff62020e63d15219ce9f097611ebf6507 [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>
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020031
32#include "power.h"
33
Rafael J. Wysocki62109b42014-09-03 01:21:03 +020034const char *pm_labels[] = { "mem", "standby", "freeze", NULL };
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +020035const char *pm_states[PM_SUSPEND_MAX];
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020036
Lionel Debroux2f55ac02010-11-16 14:14:02 +010037static const struct platform_suspend_ops *suspend_ops;
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020038static const struct platform_freeze_ops *freeze_ops;
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010039static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head);
40static bool suspend_freeze_wake;
41
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020042void freeze_set_ops(const struct platform_freeze_ops *ops)
43{
44 lock_system_sleep();
45 freeze_ops = ops;
46 unlock_system_sleep();
47}
48
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010049static void freeze_begin(void)
50{
51 suspend_freeze_wake = false;
52}
53
54static void freeze_enter(void)
55{
Rafael J. Wysockia6220fc2014-05-05 00:51:54 +020056 cpuidle_use_deepest_state(true);
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020057 cpuidle_resume();
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010058 wait_event(suspend_freeze_wait_head, suspend_freeze_wake);
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020059 cpuidle_pause();
Rafael J. Wysockia6220fc2014-05-05 00:51:54 +020060 cpuidle_use_deepest_state(false);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +010061}
62
63void freeze_wake(void)
64{
65 suspend_freeze_wake = true;
66 wake_up(&suspend_freeze_wait_head);
67}
68EXPORT_SYMBOL_GPL(freeze_wake);
69
Rafael J. Wysocki43e83172014-05-26 13:40:53 +020070static bool valid_state(suspend_state_t state)
71{
72 /*
73 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
74 * support and need to be valid to the low level
75 * implementation, no valid callback implies that none are valid.
76 */
77 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
78}
79
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +020080/*
81 * If this is set, the "mem" label always corresponds to the deepest sleep state
82 * available, the "standby" label corresponds to the second deepest sleep state
83 * available (if any), and the "freeze" label corresponds to the remaining
84 * available sleep state (if there is one).
85 */
86static bool relative_states;
87
88static int __init sleep_states_setup(char *str)
89{
90 relative_states = !strncmp(str, "1", 1);
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +020091 pm_states[PM_SUSPEND_FREEZE] = pm_labels[relative_states ? 0 : 2];
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +020092 return 1;
93}
94
95__setup("relative_sleep_states=", sleep_states_setup);
96
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020097/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +010098 * suspend_set_ops - Set the global suspend method table.
99 * @ops: Suspend operations to use.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200100 */
Lionel Debroux2f55ac02010-11-16 14:14:02 +0100101void suspend_set_ops(const struct platform_suspend_ops *ops)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200102{
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200103 suspend_state_t i;
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200104 int j = 0;
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200105
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100106 lock_system_sleep();
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200107
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200108 suspend_ops = ops;
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200109 for (i = PM_SUSPEND_MEM; i >= PM_SUSPEND_STANDBY; i--)
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200110 if (valid_state(i)) {
111 pm_states[i] = pm_labels[j++];
112 } else if (!relative_states) {
113 pm_states[i] = NULL;
114 j++;
115 }
Rafael J. Wysocki0399d4d2014-05-26 13:40:59 +0200116
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200117 pm_states[PM_SUSPEND_FREEZE] = pm_labels[j];
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200118
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +0100119 unlock_system_sleep();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200120}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200121EXPORT_SYMBOL_GPL(suspend_set_ops);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200122
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200123/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100124 * suspend_valid_only_mem - Generic memory-only valid callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200125 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100126 * Platform drivers that implement mem suspend only and only need to check for
127 * that in their .valid() callback can use this instead of rolling their own
128 * .valid() callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200129 */
130int suspend_valid_only_mem(suspend_state_t state)
131{
132 return state == PM_SUSPEND_MEM;
133}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200134EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200135
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200136static bool sleep_state_supported(suspend_state_t state)
137{
138 return state == PM_SUSPEND_FREEZE || (suspend_ops && suspend_ops->enter);
139}
140
141static int platform_suspend_prepare(suspend_state_t state)
142{
143 return state != PM_SUSPEND_FREEZE && suspend_ops->prepare ?
144 suspend_ops->prepare() : 0;
145}
146
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200147static int platform_suspend_prepare_late(suspend_state_t state)
148{
149 return state == PM_SUSPEND_FREEZE && freeze_ops->prepare ?
150 freeze_ops->prepare() : 0;
151}
152
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200153static int platform_suspend_prepare_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200154{
155 return state != PM_SUSPEND_FREEZE && suspend_ops->prepare_late ?
156 suspend_ops->prepare_late() : 0;
157}
158
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200159static void platform_resume_noirq(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200160{
161 if (state != PM_SUSPEND_FREEZE && suspend_ops->wake)
162 suspend_ops->wake();
163}
164
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200165static void platform_resume_early(suspend_state_t state)
166{
167 if (state == PM_SUSPEND_FREEZE && freeze_ops->restore)
168 freeze_ops->restore();
169}
170
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200171static void platform_resume_finish(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200172{
173 if (state != PM_SUSPEND_FREEZE && suspend_ops->finish)
174 suspend_ops->finish();
175}
176
177static int platform_suspend_begin(suspend_state_t state)
178{
179 if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->begin)
180 return freeze_ops->begin();
181 else if (suspend_ops->begin)
182 return suspend_ops->begin(state);
183 else
184 return 0;
185}
186
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200187static void platform_resume_end(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200188{
189 if (state == PM_SUSPEND_FREEZE && freeze_ops && freeze_ops->end)
190 freeze_ops->end();
191 else if (suspend_ops->end)
192 suspend_ops->end();
193}
194
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200195static void platform_recover(suspend_state_t state)
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200196{
197 if (state != PM_SUSPEND_FREEZE && suspend_ops->recover)
198 suspend_ops->recover();
199}
200
201static bool platform_suspend_again(suspend_state_t state)
202{
203 return state != PM_SUSPEND_FREEZE && suspend_ops->suspend_again ?
204 suspend_ops->suspend_again() : false;
205}
206
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200207static int suspend_test(int level)
208{
209#ifdef CONFIG_PM_DEBUG
210 if (pm_test_level == level) {
211 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
212 mdelay(5000);
213 return 1;
214 }
215#endif /* !CONFIG_PM_DEBUG */
216 return 0;
217}
218
219/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100220 * suspend_prepare - Prepare for entering system sleep state.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200221 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100222 * Common code run for every system sleep state that can be entered (except for
223 * hibernation). Run suspend notifiers, allocate the "suspend" console and
224 * freeze processes.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200225 */
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100226static int suspend_prepare(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200227{
228 int error;
229
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200230 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200231 return -EPERM;
232
233 pm_prepare_console();
234
235 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
236 if (error)
237 goto Finish;
238
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700239 trace_suspend_resume(TPS("freeze_processes"), 0, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200240 error = suspend_freeze_processes();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700241 trace_suspend_resume(TPS("freeze_processes"), 0, false);
Tejun Heo03afed82011-11-21 12:32:24 -0800242 if (!error)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200243 return 0;
244
Tejun Heo03afed82011-11-21 12:32:24 -0800245 suspend_stats.failed_freeze++;
246 dpm_save_failed_step(SUSPEND_FREEZE);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200247 Finish:
248 pm_notifier_call_chain(PM_POST_SUSPEND);
249 pm_restore_console();
250 return error;
251}
252
253/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700254void __weak arch_suspend_disable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200255{
256 local_irq_disable();
257}
258
259/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700260void __weak arch_suspend_enable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200261{
262 local_irq_enable();
263}
264
265/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100266 * suspend_enter - Make the system enter the given sleep state.
267 * @state: System sleep state to enter.
268 * @wakeup: Returns information that the sleep state should not be re-entered.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200269 *
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200270 * This function should be called after devices have been suspended.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200271 */
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200272static int suspend_enter(suspend_state_t state, bool *wakeup)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200273{
274 int error;
275
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200276 error = platform_suspend_prepare(state);
277 if (error)
278 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200279
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200280 error = dpm_suspend_late(PMSG_SUSPEND);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200281 if (error) {
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200282 printk(KERN_ERR "PM: late suspend of devices failed\n");
Rafael J. Wysockice441012010-07-07 23:43:45 +0200283 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200284 }
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200285 error = platform_suspend_prepare_late(state);
286 if (error)
287 goto Devices_early_resume;
288
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200289 error = dpm_suspend_noirq(PMSG_SUSPEND);
290 if (error) {
291 printk(KERN_ERR "PM: noirq suspend of devices failed\n");
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200292 goto Platform_early_resume;
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200293 }
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200294 error = platform_suspend_prepare_noirq(state);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200295 if (error)
296 goto Platform_wake;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200297
Zhang Rui08605ac2013-03-27 03:36:10 +0000298 if (suspend_test(TEST_PLATFORM))
299 goto Platform_wake;
300
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100301 /*
302 * PM_SUSPEND_FREEZE equals
303 * frozen processes + suspended devices + idle processors.
304 * Thus we should invoke freeze_enter() soon after
305 * all the devices are suspended.
306 */
307 if (state == PM_SUSPEND_FREEZE) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700308 trace_suspend_resume(TPS("machine_suspend"), state, true);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100309 freeze_enter();
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700310 trace_suspend_resume(TPS("machine_suspend"), state, false);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100311 goto Platform_wake;
312 }
313
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200314 error = disable_nonboot_cpus();
315 if (error || suspend_test(TEST_CPUS))
316 goto Enable_cpus;
317
318 arch_suspend_disable_irqs();
319 BUG_ON(!irqs_disabled());
320
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200321 error = syscore_suspend();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200322 if (!error) {
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200323 *wakeup = pm_wakeup_pending();
324 if (!(suspend_test(TEST_CORE) || *wakeup)) {
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700325 trace_suspend_resume(TPS("machine_suspend"),
326 state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200327 error = suspend_ops->enter(state);
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700328 trace_suspend_resume(TPS("machine_suspend"),
329 state, false);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200330 events_check_enabled = false;
331 }
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100332 syscore_resume();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200333 }
334
335 arch_suspend_enable_irqs();
336 BUG_ON(irqs_disabled());
337
338 Enable_cpus:
339 enable_nonboot_cpus();
340
341 Platform_wake:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200342 platform_resume_noirq(state);
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200343 dpm_resume_noirq(PMSG_RESUME);
344
Rafael J. Wysockia8d46b92014-09-30 02:29:01 +0200345 Platform_early_resume:
346 platform_resume_early(state);
347
Rafael J. Wysocki2a8a8ce2014-09-30 02:21:34 +0200348 Devices_early_resume:
349 dpm_resume_early(PMSG_RESUME);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200350
Rafael J. Wysockice441012010-07-07 23:43:45 +0200351 Platform_finish:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200352 platform_resume_finish(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200353 return error;
354}
355
356/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100357 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
358 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200359 */
360int suspend_devices_and_enter(suspend_state_t state)
361{
362 int error;
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200363 bool wakeup = false;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200364
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200365 if (!sleep_state_supported(state))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200366 return -ENOSYS;
367
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200368 error = platform_suspend_begin(state);
369 if (error)
370 goto Close;
371
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200372 suspend_console();
373 suspend_test_start();
374 error = dpm_suspend_start(PMSG_SUSPEND);
375 if (error) {
Bernie Thompson9350de062013-06-01 00:47:43 +0000376 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200377 goto Recover_platform;
378 }
379 suspend_test_finish("suspend devices");
380 if (suspend_test(TEST_DEVICES))
381 goto Recover_platform;
382
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200383 do {
384 error = suspend_enter(state, &wakeup);
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200385 } while (!error && !wakeup && platform_suspend_again(state));
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200386
387 Resume_devices:
388 suspend_test_start();
389 dpm_resume_end(PMSG_RESUME);
390 suspend_test_finish("resume devices");
Todd E Brandt0cadc702014-09-19 14:07:12 -0700391 trace_suspend_resume(TPS("resume_console"), state, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200392 resume_console();
Todd E Brandt0cadc702014-09-19 14:07:12 -0700393 trace_suspend_resume(TPS("resume_console"), state, false);
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +0200394
Rafael J. Wysocki8490fdf2014-07-23 00:57:53 +0200395 Close:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200396 platform_resume_end(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200397 return error;
398
399 Recover_platform:
Rafael J. Wysockiebc3e412014-09-30 02:22:24 +0200400 platform_recover(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200401 goto Resume_devices;
402}
403
404/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100405 * suspend_finish - Clean up before finishing the suspend sequence.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200406 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100407 * Call platform code to clean up, restart processes, and free the console that
408 * we've allocated. This routine is not called for hibernation.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200409 */
410static void suspend_finish(void)
411{
412 suspend_thaw_processes();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200413 pm_notifier_call_chain(PM_POST_SUSPEND);
414 pm_restore_console();
415}
416
417/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100418 * enter_state - Do common work needed to enter system sleep state.
419 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200420 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100421 * Make sure that no one else is trying to put the system into a sleep state.
422 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
423 * system enter the given sleep state and clean up after wakeup.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200424 */
Rafael J. Wysocki93e1ee42012-02-13 16:29:24 +0100425static int enter_state(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200426{
427 int error;
428
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700429 trace_suspend_resume(TPS("suspend_enter"), state, true);
Rafael J. Wysocki43e83172014-05-26 13:40:53 +0200430 if (state == PM_SUSPEND_FREEZE) {
431#ifdef CONFIG_PM_DEBUG
432 if (pm_test_level != TEST_NONE && pm_test_level <= TEST_CPUS) {
433 pr_warning("PM: Unsupported test mode for freeze state,"
434 "please choose none/freezer/devices/platform.\n");
435 return -EAGAIN;
436 }
437#endif
438 } else if (!valid_state(state)) {
439 return -EINVAL;
440 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200441 if (!mutex_trylock(&pm_mutex))
442 return -EBUSY;
443
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100444 if (state == PM_SUSPEND_FREEZE)
445 freeze_begin();
446
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700447 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200448 printk(KERN_INFO "PM: Syncing filesystems ... ");
449 sys_sync();
450 printk("done.\n");
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700451 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200452
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200453 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Zhang Rui7e73c5ae2013-02-06 13:00:36 +0100454 error = suspend_prepare(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200455 if (error)
456 goto Unlock;
457
458 if (suspend_test(TEST_FREEZER))
459 goto Finish;
460
Todd E Brandtbb3632c2014-06-06 05:40:17 -0700461 trace_suspend_resume(TPS("suspend_enter"), state, false);
Rafael J. Wysockid431cbc2014-07-15 22:02:11 +0200462 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200463 pm_restrict_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200464 error = suspend_devices_and_enter(state);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200465 pm_restore_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200466
467 Finish:
468 pr_debug("PM: Finishing wakeup.\n");
469 suspend_finish();
470 Unlock:
471 mutex_unlock(&pm_mutex);
472 return error;
473}
474
475/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100476 * pm_suspend - Externally visible function for suspending the system.
477 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200478 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100479 * Check if the value of @state represents one of the supported states,
480 * execute enter_state() and update system suspend statistics.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200481 */
482int pm_suspend(suspend_state_t state)
483{
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100484 int error;
485
486 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
487 return -EINVAL;
488
489 error = enter_state(state);
490 if (error) {
491 suspend_stats.fail++;
492 dpm_save_failed_errno(error);
493 } else {
494 suspend_stats.success++;
ShuoX Liu2a77c462011-08-10 23:01:26 +0200495 }
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100496 return error;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200497}
498EXPORT_SYMBOL(pm_suspend);