blob: 6799c423777dca2def6fcbe2c13ac9e454a5d73f [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>
24#include <linux/suspend.h>
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +010025#include <linux/syscore_ops.h>
Jean Pihet938cfed2011-01-05 19:49:01 +010026#include <trace/events/power.h>
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020027
28#include "power.h"
29
30const char *const pm_states[PM_SUSPEND_MAX] = {
Arve Hjønnevågb0dc3432008-10-09 19:17:11 -070031#ifdef CONFIG_EARLYSUSPEND
32 [PM_SUSPEND_ON] = "on",
33#endif
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
40/**
41 * suspend_set_ops - Set the global suspend method table.
42 * @ops: Pointer to ops structure.
43 */
Lionel Debroux2f55ac02010-11-16 14:14:02 +010044void suspend_set_ops(const struct platform_suspend_ops *ops)
Rafael J. Wysockia9d70522009-06-10 01:27:12 +020045{
46 mutex_lock(&pm_mutex);
47 suspend_ops = ops;
48 mutex_unlock(&pm_mutex);
49}
50
51bool valid_state(suspend_state_t state)
52{
53 /*
54 * All states need lowlevel support and need to be valid to the lowlevel
55 * implementation, no valid callback implies that none are valid.
56 */
57 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
58}
59
60/**
61 * suspend_valid_only_mem - generic memory-only valid callback
62 *
63 * Platform drivers that implement mem suspend only and only need
64 * to check for that in their .valid callback can use this instead
65 * of rolling their own .valid callback.
66 */
67int suspend_valid_only_mem(suspend_state_t state)
68{
69 return state == PM_SUSPEND_MEM;
70}
71
72static int suspend_test(int level)
73{
74#ifdef CONFIG_PM_DEBUG
75 if (pm_test_level == level) {
76 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
77 mdelay(5000);
78 return 1;
79 }
80#endif /* !CONFIG_PM_DEBUG */
81 return 0;
82}
83
84/**
85 * suspend_prepare - Do prep work before entering low-power state.
86 *
87 * This is common code that is called for each state that we're entering.
88 * Run suspend notifiers, allocate a console and stop all processes.
89 */
90static int suspend_prepare(void)
91{
92 int error;
93
94 if (!suspend_ops || !suspend_ops->enter)
95 return -EPERM;
96
97 pm_prepare_console();
98
99 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
100 if (error)
101 goto Finish;
102
103 error = usermodehelper_disable();
104 if (error)
105 goto Finish;
106
107 error = suspend_freeze_processes();
108 if (!error)
109 return 0;
110
111 suspend_thaw_processes();
112 usermodehelper_enable();
113 Finish:
114 pm_notifier_call_chain(PM_POST_SUSPEND);
115 pm_restore_console();
116 return error;
117}
118
119/* default implementation */
120void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
121{
122 local_irq_disable();
123}
124
125/* default implementation */
126void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
127{
128 local_irq_enable();
129}
130
131/**
132 * suspend_enter - enter the desired system sleep state.
133 * @state: state to enter
134 *
135 * This function should be called after devices have been suspended.
136 */
137static int suspend_enter(suspend_state_t state)
138{
139 int error;
140
141 if (suspend_ops->prepare) {
142 error = suspend_ops->prepare();
143 if (error)
Rafael J. Wysockice441012010-07-07 23:43:45 +0200144 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200145 }
146
147 error = dpm_suspend_noirq(PMSG_SUSPEND);
148 if (error) {
149 printk(KERN_ERR "PM: Some devices failed to power down\n");
Rafael J. Wysockice441012010-07-07 23:43:45 +0200150 goto Platform_finish;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200151 }
152
153 if (suspend_ops->prepare_late) {
154 error = suspend_ops->prepare_late();
155 if (error)
Rafael J. Wysockice441012010-07-07 23:43:45 +0200156 goto Platform_wake;
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200157 }
158
159 if (suspend_test(TEST_PLATFORM))
160 goto Platform_wake;
161
162 error = disable_nonboot_cpus();
163 if (error || suspend_test(TEST_CPUS))
164 goto Enable_cpus;
165
166 arch_suspend_disable_irqs();
167 BUG_ON(!irqs_disabled());
168
Rafael J. Wysocki2e711c02011-04-26 19:15:07 +0200169 error = syscore_suspend();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200170 if (!error) {
Rafael J. Wysockia2867e02010-12-03 22:58:31 +0100171 if (!(suspend_test(TEST_CORE) || pm_wakeup_pending())) {
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200172 error = suspend_ops->enter(state);
Rafael J. Wysockic125e962010-07-05 22:43:53 +0200173 events_check_enabled = false;
174 }
Rafael J. Wysocki40dc1662011-03-15 00:43:46 +0100175 syscore_resume();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200176 }
177
178 arch_suspend_enable_irqs();
179 BUG_ON(irqs_disabled());
180
181 Enable_cpus:
182 enable_nonboot_cpus();
183
184 Platform_wake:
185 if (suspend_ops->wake)
186 suspend_ops->wake();
187
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200188 dpm_resume_noirq(PMSG_RESUME);
189
Rafael J. Wysockice441012010-07-07 23:43:45 +0200190 Platform_finish:
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200191 if (suspend_ops->finish)
192 suspend_ops->finish();
193
194 return error;
195}
196
197/**
198 * suspend_devices_and_enter - suspend devices and enter the desired system
199 * sleep state.
200 * @state: state to enter
201 */
202int suspend_devices_and_enter(suspend_state_t state)
203{
204 int error;
205
206 if (!suspend_ops)
207 return -ENOSYS;
208
Jean Pihet938cfed2011-01-05 19:49:01 +0100209 trace_machine_suspend(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200210 if (suspend_ops->begin) {
211 error = suspend_ops->begin(state);
212 if (error)
213 goto Close;
214 }
215 suspend_console();
216 suspend_test_start();
217 error = dpm_suspend_start(PMSG_SUSPEND);
218 if (error) {
219 printk(KERN_ERR "PM: Some devices failed to suspend\n");
220 goto Recover_platform;
221 }
222 suspend_test_finish("suspend devices");
223 if (suspend_test(TEST_DEVICES))
224 goto Recover_platform;
225
MyungJoo Ham3c431932011-04-22 22:00:54 +0200226 error = suspend_enter(state);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200227
228 Resume_devices:
229 suspend_test_start();
230 dpm_resume_end(PMSG_RESUME);
231 suspend_test_finish("resume devices");
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200232 resume_console();
233 Close:
234 if (suspend_ops->end)
235 suspend_ops->end();
Jean Pihet938cfed2011-01-05 19:49:01 +0100236 trace_machine_suspend(PWR_EVENT_EXIT);
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200237 return error;
238
239 Recover_platform:
240 if (suspend_ops->recover)
241 suspend_ops->recover();
242 goto Resume_devices;
243}
244
245/**
246 * suspend_finish - Do final work before exiting suspend sequence.
247 *
248 * Call platform code to clean up, restart processes, and free the
249 * console that we've allocated. This is not called for suspend-to-disk.
250 */
251static void suspend_finish(void)
252{
253 suspend_thaw_processes();
254 usermodehelper_enable();
255 pm_notifier_call_chain(PM_POST_SUSPEND);
256 pm_restore_console();
257}
258
259/**
260 * enter_state - Do common work of entering low-power state.
261 * @state: pm_state structure for state we're entering.
262 *
263 * Make sure we're the only ones trying to enter a sleep state. Fail
264 * if someone has beat us to it, since we don't want anything weird to
265 * happen when we wake up.
266 * Then, do the setup for suspend, enter the state, and cleaup (after
267 * we've woken up).
268 */
269int enter_state(suspend_state_t state)
270{
271 int error;
272
273 if (!valid_state(state))
274 return -ENODEV;
275
276 if (!mutex_trylock(&pm_mutex))
277 return -EBUSY;
278
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200279 sys_sync();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200280
281 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
282 error = suspend_prepare();
283 if (error)
284 goto Unlock;
285
286 if (suspend_test(TEST_FREEZER))
287 goto Finish;
288
289 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200290 pm_restrict_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200291 error = suspend_devices_and_enter(state);
Rafael J. Wysocki87186472011-05-10 21:09:53 +0200292 pm_restore_gfp_mask();
Rafael J. Wysockia9d70522009-06-10 01:27:12 +0200293
294 Finish:
295 pr_debug("PM: Finishing wakeup.\n");
296 suspend_finish();
297 Unlock:
298 mutex_unlock(&pm_mutex);
299 return error;
300}
301
302/**
303 * pm_suspend - Externally visible function for suspending system.
304 * @state: Enumerated value of state to enter.
305 *
306 * Determine whether or not value is within range, get state
307 * structure, and enter (above).
308 */
309int pm_suspend(suspend_state_t state)
310{
311 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
312 return enter_state(state);
313 return -EINVAL;
314}
315EXPORT_SYMBOL(pm_suspend);