blob: 73a905f83972048617b34bd22cf418df3527e8f4 [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
34const char *const pm_states[PM_SUSPEND_MAX] = {
Zhang Rui7e73c5a2013-02-06 13:00:36 +010035 [PM_SUSPEND_FREEZE] = "freeze",
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020036 [PM_SUSPEND_STANDBY] = "standby",
37 [PM_SUSPEND_MEM] = "mem",
38};
39
Lionel Debroux2f55ac02010-11-16 14:14:02 +010040static const struct platform_suspend_ops *suspend_ops;
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020041static const struct platform_freeze_ops *freeze_ops;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020042
Zhang Rui7e73c5a2013-02-06 13:00:36 +010043static bool need_suspend_ops(suspend_state_t state)
44{
Viresh Kumar7500d932014-03-10 19:31:51 +053045 return state > PM_SUSPEND_FREEZE;
Zhang Rui7e73c5a2013-02-06 13:00:36 +010046}
47
48static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head);
49static bool suspend_freeze_wake;
50
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +020051void freeze_set_ops(const struct platform_freeze_ops *ops)
52{
53 lock_system_sleep();
54 freeze_ops = ops;
55 unlock_system_sleep();
56}
57
Zhang Rui7e73c5a2013-02-06 13:00:36 +010058static void freeze_begin(void)
59{
60 suspend_freeze_wake = false;
61}
62
63static void freeze_enter(void)
64{
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020065 cpuidle_resume();
Zhang Rui7e73c5a2013-02-06 13:00:36 +010066 wait_event(suspend_freeze_wait_head, suspend_freeze_wake);
Rafael J. Wysockif3f12532014-04-20 23:43:01 +020067 cpuidle_pause();
Zhang Rui7e73c5a2013-02-06 13:00:36 +010068}
69
70void freeze_wake(void)
71{
72 suspend_freeze_wake = true;
73 wake_up(&suspend_freeze_wait_head);
74}
75EXPORT_SYMBOL_GPL(freeze_wake);
76
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020077/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +010078 * suspend_set_ops - Set the global suspend method table.
79 * @ops: Suspend operations to use.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020080 */
Lionel Debroux2f55ac02010-11-16 14:14:02 +010081void suspend_set_ops(const struct platform_suspend_ops *ops)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020082{
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010083 lock_system_sleep();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020084 suspend_ops = ops;
Srivatsa S. Bhatbcda53f2011-12-07 22:29:54 +010085 unlock_system_sleep();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020086}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +020087EXPORT_SYMBOL_GPL(suspend_set_ops);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020088
89bool valid_state(suspend_state_t state)
90{
Zhang Ruic26b78a2013-03-27 03:36:11 +000091 if (state == PM_SUSPEND_FREEZE) {
92#ifdef CONFIG_PM_DEBUG
93 if (pm_test_level != TEST_NONE &&
94 pm_test_level != TEST_FREEZER &&
95 pm_test_level != TEST_DEVICES &&
96 pm_test_level != TEST_PLATFORM) {
97 printk(KERN_WARNING "Unsupported pm_test mode for "
98 "freeze state, please choose "
99 "none/freezer/devices/platform.\n");
100 return false;
101 }
102#endif
103 return true;
104 }
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200105 /*
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100106 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEMORY states need lowlevel
107 * support and need to be valid to the lowlevel
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200108 * implementation, no valid callback implies that none are valid.
109 */
110 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
111}
112
113/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100114 * suspend_valid_only_mem - Generic memory-only valid callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200115 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100116 * Platform drivers that implement mem suspend only and only need to check for
117 * that in their .valid() callback can use this instead of rolling their own
118 * .valid() callback.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200119 */
120int suspend_valid_only_mem(suspend_state_t state)
121{
122 return state == PM_SUSPEND_MEM;
123}
Kevin Hilmana5e4fd82011-06-27 01:01:07 +0200124EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200125
126static int suspend_test(int level)
127{
128#ifdef CONFIG_PM_DEBUG
129 if (pm_test_level == level) {
130 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
131 mdelay(5000);
132 return 1;
133 }
134#endif /* !CONFIG_PM_DEBUG */
135 return 0;
136}
137
138/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100139 * suspend_prepare - Prepare for entering system sleep state.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200140 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100141 * Common code run for every system sleep state that can be entered (except for
142 * hibernation). Run suspend notifiers, allocate the "suspend" console and
143 * freeze processes.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200144 */
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100145static int suspend_prepare(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200146{
147 int error;
148
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100149 if (need_suspend_ops(state) && (!suspend_ops || !suspend_ops->enter))
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200150 return -EPERM;
151
152 pm_prepare_console();
153
154 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
155 if (error)
156 goto Finish;
157
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200158 error = suspend_freeze_processes();
Tejun Heo03afed82011-11-21 12:32:24 -0800159 if (!error)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200160 return 0;
161
Tejun Heo03afed82011-11-21 12:32:24 -0800162 suspend_stats.failed_freeze++;
163 dpm_save_failed_step(SUSPEND_FREEZE);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200164 Finish:
165 pm_notifier_call_chain(PM_POST_SUSPEND);
166 pm_restore_console();
167 return error;
168}
169
170/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700171void __weak arch_suspend_disable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200172{
173 local_irq_disable();
174}
175
176/* default implementation */
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -0700177void __weak arch_suspend_enable_irqs(void)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200178{
179 local_irq_enable();
180}
181
182/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100183 * suspend_enter - Make the system enter the given sleep state.
184 * @state: System sleep state to enter.
185 * @wakeup: Returns information that the sleep state should not be re-entered.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200186 *
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200187 * This function should be called after devices have been suspended.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200188 */
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200189static int suspend_enter(suspend_state_t state, bool *wakeup)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200190{
191 int error;
192
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100193 if (need_suspend_ops(state) && suspend_ops->prepare) {
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200194 error = suspend_ops->prepare();
195 if (error)
Rafael J. Wysockice441012010-07-07 23:43:45 +0200196 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200197 }
198
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100199 error = dpm_suspend_end(PMSG_SUSPEND);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200200 if (error) {
201 printk(KERN_ERR "PM: Some devices failed to power down\n");
Rafael J. Wysockice441012010-07-07 23:43:45 +0200202 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200203 }
204
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100205 if (need_suspend_ops(state) && suspend_ops->prepare_late) {
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200206 error = suspend_ops->prepare_late();
207 if (error)
Rafael J. Wysockice441012010-07-07 23:43:45 +0200208 goto Platform_wake;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200209 }
210
Zhang Rui08605ac2013-03-27 03:36:10 +0000211 if (suspend_test(TEST_PLATFORM))
212 goto Platform_wake;
213
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100214 /*
215 * PM_SUSPEND_FREEZE equals
216 * frozen processes + suspended devices + idle processors.
217 * Thus we should invoke freeze_enter() soon after
218 * all the devices are suspended.
219 */
220 if (state == PM_SUSPEND_FREEZE) {
221 freeze_enter();
222 goto Platform_wake;
223 }
224
Brandt, Todd E38312612013-07-11 07:44:35 +0000225 ftrace_stop();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200226 error = disable_nonboot_cpus();
227 if (error || suspend_test(TEST_CPUS))
228 goto Enable_cpus;
229
230 arch_suspend_disable_irqs();
231 BUG_ON(!irqs_disabled());
232
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200233 error = syscore_suspend();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200234 if (!error) {
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200235 *wakeup = pm_wakeup_pending();
236 if (!(suspend_test(TEST_CORE) || *wakeup)) {
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200237 error = suspend_ops->enter(state);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200238 events_check_enabled = false;
239 }
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100240 syscore_resume();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200241 }
242
243 arch_suspend_enable_irqs();
244 BUG_ON(irqs_disabled());
245
246 Enable_cpus:
247 enable_nonboot_cpus();
Brandt, Todd E38312612013-07-11 07:44:35 +0000248 ftrace_start();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200249
250 Platform_wake:
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100251 if (need_suspend_ops(state) && suspend_ops->wake)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200252 suspend_ops->wake();
253
Rafael J. Wysockicf579df2012-01-29 20:38:29 +0100254 dpm_resume_start(PMSG_RESUME);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200255
Rafael J. Wysockice441012010-07-07 23:43:45 +0200256 Platform_finish:
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100257 if (need_suspend_ops(state) && suspend_ops->finish)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200258 suspend_ops->finish();
259
260 return error;
261}
262
263/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100264 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
265 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200266 */
267int suspend_devices_and_enter(suspend_state_t state)
268{
269 int error;
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200270 bool wakeup = false;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200271
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100272 if (need_suspend_ops(state) && !suspend_ops)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200273 return -ENOSYS;
274
Jean Pihet938cfed2011-01-05 19:49:01 +0100275 trace_machine_suspend(state);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100276 if (need_suspend_ops(state) && suspend_ops->begin) {
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200277 error = suspend_ops->begin(state);
278 if (error)
279 goto Close;
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +0200280 } else if (state == PM_SUSPEND_FREEZE && freeze_ops->begin) {
281 error = freeze_ops->begin();
282 if (error)
283 goto Close;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200284 }
285 suspend_console();
286 suspend_test_start();
287 error = dpm_suspend_start(PMSG_SUSPEND);
288 if (error) {
Bernie Thompson9350de062013-06-01 00:47:43 +0000289 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200290 goto Recover_platform;
291 }
292 suspend_test_finish("suspend devices");
293 if (suspend_test(TEST_DEVICES))
294 goto Recover_platform;
295
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200296 do {
297 error = suspend_enter(state, &wakeup);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100298 } while (!error && !wakeup && need_suspend_ops(state)
MyungJoo Ham3b5fe852011-06-12 15:57:05 +0200299 && suspend_ops->suspend_again && suspend_ops->suspend_again());
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200300
301 Resume_devices:
302 suspend_test_start();
303 dpm_resume_end(PMSG_RESUME);
304 suspend_test_finish("resume devices");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200305 resume_console();
306 Close:
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100307 if (need_suspend_ops(state) && suspend_ops->end)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200308 suspend_ops->end();
Rafael J. Wysocki1f0b6382014-05-15 23:29:57 +0200309 else if (state == PM_SUSPEND_FREEZE && freeze_ops->end)
310 freeze_ops->end();
311
Jean Pihet938cfed2011-01-05 19:49:01 +0100312 trace_machine_suspend(PWR_EVENT_EXIT);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200313 return error;
314
315 Recover_platform:
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100316 if (need_suspend_ops(state) && suspend_ops->recover)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200317 suspend_ops->recover();
318 goto Resume_devices;
319}
320
321/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100322 * suspend_finish - Clean up before finishing the suspend sequence.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200323 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100324 * Call platform code to clean up, restart processes, and free the console that
325 * we've allocated. This routine is not called for hibernation.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200326 */
327static void suspend_finish(void)
328{
329 suspend_thaw_processes();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200330 pm_notifier_call_chain(PM_POST_SUSPEND);
331 pm_restore_console();
332}
333
334/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100335 * enter_state - Do common work needed to enter system sleep state.
336 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200337 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100338 * Make sure that no one else is trying to put the system into a sleep state.
339 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
340 * system enter the given sleep state and clean up after wakeup.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200341 */
Rafael J. Wysocki93e1ee42012-02-13 16:29:24 +0100342static int enter_state(suspend_state_t state)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200343{
344 int error;
345
346 if (!valid_state(state))
347 return -ENODEV;
348
349 if (!mutex_trylock(&pm_mutex))
350 return -EBUSY;
351
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100352 if (state == PM_SUSPEND_FREEZE)
353 freeze_begin();
354
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200355 printk(KERN_INFO "PM: Syncing filesystems ... ");
356 sys_sync();
357 printk("done.\n");
358
359 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
Zhang Rui7e73c5a2013-02-06 13:00:36 +0100360 error = suspend_prepare(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200361 if (error)
362 goto Unlock;
363
364 if (suspend_test(TEST_FREEZER))
365 goto Finish;
366
367 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200368 pm_restrict_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200369 error = suspend_devices_and_enter(state);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200370 pm_restore_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200371
372 Finish:
373 pr_debug("PM: Finishing wakeup.\n");
374 suspend_finish();
375 Unlock:
376 mutex_unlock(&pm_mutex);
377 return error;
378}
379
380/**
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100381 * pm_suspend - Externally visible function for suspending the system.
382 * @state: System sleep state to enter.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200383 *
Rafael J. Wysocki55ae4512012-02-13 16:29:14 +0100384 * Check if the value of @state represents one of the supported states,
385 * execute enter_state() and update system suspend statistics.
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200386 */
387int pm_suspend(suspend_state_t state)
388{
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100389 int error;
390
391 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
392 return -EINVAL;
393
394 error = enter_state(state);
395 if (error) {
396 suspend_stats.fail++;
397 dpm_save_failed_errno(error);
398 } else {
399 suspend_stats.success++;
ShuoX Liu2a77c462011-08-10 23:01:26 +0200400 }
Rafael J. Wysockibc25cf52012-02-13 16:29:33 +0100401 return error;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200402}
403EXPORT_SYMBOL(pm_suspend);