blob: 62ee437b5c7ea362de8573dd18fcee1ef6286221 [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>
17#include <linux/syscalls.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
Matthew Garrettdd4c4f12010-05-28 16:32:14 -040019#include <linux/io.h>
20#include <linux/kernel.h>
21#include <linux/list.h>
22#include <linux/mm.h>
23#include <linux/slab.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040024#include <linux/export.h>
Matthew Garrettdd4c4f12010-05-28 16:32:14 -040025#include <linux/suspend.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010026#include <linux/syscore_ops.h>
Srivatsa S. Bhat443772d42012-06-16 15:30:45 +020027#include <linux/ftrace.h>
Jean Pihet938cfed2011-01-05 19:49:01 +010028#include <trace/events/power.h>
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020029
30#include "power.h"
31
32const char *const pm_states[PM_SUSPEND_MAX] = {
Zhang Rui7e73c5a2013-02-06 13:00:36 +010033 [PM_SUSPEND_FREEZE] = "freeze",
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020034 [PM_SUSPEND_STANDBY] = "standby",
35 [PM_SUSPEND_MEM] = "mem",
36};
37
Lionel Debroux2f55ac02010-11-16 14:14:02 +010038static const struct platform_suspend_ops *suspend_ops;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020039
Zhang Rui7e73c5a2013-02-06 13:00:36 +010040static bool need_suspend_ops(suspend_state_t state)
41{
42 return !!(state > PM_SUSPEND_FREEZE);
43}
44
45static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head);
46static bool suspend_freeze_wake;
47
48static void freeze_begin(void)
49{
50 suspend_freeze_wake = false;
51}
52
53static void freeze_enter(void)
54{
55 wait_event(suspend_freeze_wait_head, suspend_freeze_wake);
56}
57
58void freeze_wake(void)
59{
60 suspend_freeze_wake = true;
61 wake_up(&suspend_freeze_wait_head);
62}
63EXPORT_SYMBOL_GPL(freeze_wake);
64
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020065/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +010066 * suspend_set_ops - Set the global suspend method table.
67 * @ops: Suspend operations to use.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020068 */
Lionel Debroux2f55ac02010-11-16 14:14:02 +010069void suspend_set_ops(const struct platform_suspend_ops *ops)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020070{
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010071 lock_system_sleep();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020072 suspend_ops = ops;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010073 unlock_system_sleep();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020074}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +020075EXPORT_SYMBOL_GPL(suspend_set_ops);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020076
77bool valid_state(suspend_state_t state)
78{
Zhang Ruic26b78a2013-03-27 03:36:11 +000079 if (state == PM_SUSPEND_FREEZE) {
80#ifdef CONFIG_PM_DEBUG
81 if (pm_test_level != TEST_NONE &&
82 pm_test_level != TEST_FREEZER &&
83 pm_test_level != TEST_DEVICES &&
84 pm_test_level != TEST_PLATFORM) {
85 printk(KERN_WARNING "Unsupported pm_test mode for "
86 "freeze state, please choose "
87 "none/freezer/devices/platform.\n");
88 return false;
89 }
90#endif
91 return true;
92 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020093 /*
Zhang Rui7e73c5a2013-02-06 13:00:36 +010094 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEMORY states need lowlevel
95 * support and need to be valid to the lowlevel
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020096 * implementation, no valid callback implies that none are valid.
97 */
98 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
99}
100
101/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100102 * suspend_valid_only_mem - Generic memory-only valid callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200103 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100104 * Platform drivers that implement mem suspend only and only need to check for
105 * that in their .valid() callback can use this instead of rolling their own
106 * .valid() callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200107 */
108int suspend_valid_only_mem(suspend_state_t state)
109{
110 return state == PM_SUSPEND_MEM;
111}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200112EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200113
114static int suspend_test(int level)
115{
116#ifdef CONFIG_PM_DEBUG
117 if (pm_test_level == level) {
118 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
119 mdelay(5000);
120 return 1;
121 }
122#endif /* !CONFIG_PM_DEBUG */
123 return 0;
124}
125
126/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100127 * suspend_prepare - Prepare for entering system sleep state.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200128 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100129 * Common code run for every system sleep state that can be entered (except for
130 * hibernation). Run suspend notifiers, allocate the "suspend" console and
131 * freeze processes.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200132 */
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100133static int suspend_prepare(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200134{
135 int error;
136
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100137 if (need_suspend_ops(state) && (!suspend_ops || !suspend_ops->enter))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200138 return -EPERM;
139
140 pm_prepare_console();
141
142 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
143 if (error)
144 goto Finish;
145
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200146 error = suspend_freeze_processes();
Tejun Heo03afed82011-11-21 12:32:24 -0800147 if (!error)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200148 return 0;
149
Tejun Heo03afed82011-11-21 12:32:24 -0800150 suspend_stats.failed_freeze++;
151 dpm_save_failed_step(SUSPEND_FREEZE);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200152 Finish:
153 pm_notifier_call_chain(PM_POST_SUSPEND);
154 pm_restore_console();
155 return error;
156}
157
158/* default implementation */
159void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
160{
161 local_irq_disable();
162}
163
164/* default implementation */
165void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
166{
167 local_irq_enable();
168}
169
170/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100171 * suspend_enter - Make the system enter the given sleep state.
172 * @state: System sleep state to enter.
173 * @wakeup: Returns information that the sleep state should not be re-entered.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200174 *
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200175 * This function should be called after devices have been suspended.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200176 */
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200177static int suspend_enter(suspend_state_t state, bool *wakeup)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200178{
179 int error;
180
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100181 if (need_suspend_ops(state) && suspend_ops->prepare) {
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200182 error = suspend_ops->prepare();
183 if (error)
Rafael J. Wysockice441012010-07-07 23:43:45 +0200184 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200185 }
186
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100187 error = dpm_suspend_end(PMSG_SUSPEND);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200188 if (error) {
189 printk(KERN_ERR "PM: Some devices failed to power down\n");
Rafael J. Wysockice441012010-07-07 23:43:45 +0200190 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200191 }
192
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100193 if (need_suspend_ops(state) && suspend_ops->prepare_late) {
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200194 error = suspend_ops->prepare_late();
195 if (error)
Rafael J. Wysockice441012010-07-07 23:43:45 +0200196 goto Platform_wake;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200197 }
198
Zhang Rui08605ac2013-03-27 03:36:10 +0000199 if (suspend_test(TEST_PLATFORM))
200 goto Platform_wake;
201
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100202 /*
203 * PM_SUSPEND_FREEZE equals
204 * frozen processes + suspended devices + idle processors.
205 * Thus we should invoke freeze_enter() soon after
206 * all the devices are suspended.
207 */
208 if (state == PM_SUSPEND_FREEZE) {
209 freeze_enter();
210 goto Platform_wake;
211 }
212
Brandt, Todd E38312612013-07-11 07:44:35 +0000213 ftrace_stop();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200214 error = disable_nonboot_cpus();
215 if (error || suspend_test(TEST_CPUS))
216 goto Enable_cpus;
217
218 arch_suspend_disable_irqs();
219 BUG_ON(!irqs_disabled());
220
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200221 error = syscore_suspend();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200222 if (!error) {
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200223 *wakeup = pm_wakeup_pending();
224 if (!(suspend_test(TEST_CORE) || *wakeup)) {
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200225 error = suspend_ops->enter(state);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200226 events_check_enabled = false;
227 }
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100228 syscore_resume();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200229 }
230
231 arch_suspend_enable_irqs();
232 BUG_ON(irqs_disabled());
233
234 Enable_cpus:
235 enable_nonboot_cpus();
Brandt, Todd E38312612013-07-11 07:44:35 +0000236 ftrace_start();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200237
238 Platform_wake:
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100239 if (need_suspend_ops(state) && suspend_ops->wake)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200240 suspend_ops->wake();
241
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100242 dpm_resume_start(PMSG_RESUME);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200243
Rafael J. Wysockice441012010-07-07 23:43:45 +0200244 Platform_finish:
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100245 if (need_suspend_ops(state) && suspend_ops->finish)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200246 suspend_ops->finish();
247
248 return error;
249}
250
251/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100252 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
253 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200254 */
255int suspend_devices_and_enter(suspend_state_t state)
256{
257 int error;
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200258 bool wakeup = false;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200259
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100260 if (need_suspend_ops(state) && !suspend_ops)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200261 return -ENOSYS;
262
Jean Pihet938cfed2011-01-05 19:49:01 +0100263 trace_machine_suspend(state);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100264 if (need_suspend_ops(state) && suspend_ops->begin) {
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200265 error = suspend_ops->begin(state);
266 if (error)
267 goto Close;
268 }
269 suspend_console();
270 suspend_test_start();
271 error = dpm_suspend_start(PMSG_SUSPEND);
272 if (error) {
Bernie Thompson9350de062013-06-01 00:47:43 +0000273 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200274 goto Recover_platform;
275 }
276 suspend_test_finish("suspend devices");
277 if (suspend_test(TEST_DEVICES))
278 goto Recover_platform;
279
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200280 do {
281 error = suspend_enter(state, &wakeup);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100282 } while (!error && !wakeup && need_suspend_ops(state)
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200283 && suspend_ops->suspend_again && suspend_ops->suspend_again());
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200284
285 Resume_devices:
286 suspend_test_start();
287 dpm_resume_end(PMSG_RESUME);
288 suspend_test_finish("resume devices");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200289 resume_console();
290 Close:
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100291 if (need_suspend_ops(state) && suspend_ops->end)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200292 suspend_ops->end();
Jean Pihet938cfed2011-01-05 19:49:01 +0100293 trace_machine_suspend(PWR_EVENT_EXIT);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200294 return error;
295
296 Recover_platform:
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100297 if (need_suspend_ops(state) && suspend_ops->recover)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200298 suspend_ops->recover();
299 goto Resume_devices;
300}
301
302/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100303 * suspend_finish - Clean up before finishing the suspend sequence.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200304 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100305 * Call platform code to clean up, restart processes, and free the console that
306 * we've allocated. This routine is not called for hibernation.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200307 */
308static void suspend_finish(void)
309{
310 suspend_thaw_processes();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200311 pm_notifier_call_chain(PM_POST_SUSPEND);
312 pm_restore_console();
313}
314
315/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100316 * enter_state - Do common work needed to enter system sleep state.
317 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200318 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100319 * Make sure that no one else is trying to put the system into a sleep state.
320 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
321 * system enter the given sleep state and clean up after wakeup.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200322 */
Rafael J. Wysocki93e1ee42012-02-13 16:29:24 +0100323static int enter_state(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200324{
325 int error;
326
327 if (!valid_state(state))
328 return -ENODEV;
329
330 if (!mutex_trylock(&pm_mutex))
331 return -EBUSY;
332
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100333 if (state == PM_SUSPEND_FREEZE)
334 freeze_begin();
335
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200336 printk(KERN_INFO "PM: Syncing filesystems ... ");
337 sys_sync();
338 printk("done.\n");
339
340 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100341 error = suspend_prepare(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200342 if (error)
343 goto Unlock;
344
345 if (suspend_test(TEST_FREEZER))
346 goto Finish;
347
348 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200349 pm_restrict_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200350 error = suspend_devices_and_enter(state);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200351 pm_restore_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200352
353 Finish:
354 pr_debug("PM: Finishing wakeup.\n");
355 suspend_finish();
356 Unlock:
357 mutex_unlock(&pm_mutex);
358 return error;
359}
360
361/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100362 * pm_suspend - Externally visible function for suspending the system.
363 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200364 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100365 * Check if the value of @state represents one of the supported states,
366 * execute enter_state() and update system suspend statistics.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200367 */
368int pm_suspend(suspend_state_t state)
369{
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100370 int error;
371
372 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
373 return -EINVAL;
374
375 error = enter_state(state);
376 if (error) {
377 suspend_stats.fail++;
378 dpm_save_failed_errno(error);
379 } else {
380 suspend_stats.success++;
ShuoX Liu2a77c462011-08-10 23:01:26 +0200381 }
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100382 return error;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200383}
384EXPORT_SYMBOL(pm_suspend);