Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/klassert/ipsec...
[deliverable/linux.git] / kernel / power / suspend.c
1 /*
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/cpuidle.h>
18 #include <linux/syscalls.h>
19 #include <linux/gfp.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/mm.h>
24 #include <linux/slab.h>
25 #include <linux/export.h>
26 #include <linux/suspend.h>
27 #include <linux/syscore_ops.h>
28 #include <linux/ftrace.h>
29 #include <trace/events/power.h>
30 #include <linux/compiler.h>
31
32 #include "power.h"
33
34 const char *const pm_states[PM_SUSPEND_MAX] = {
35 [PM_SUSPEND_FREEZE] = "freeze",
36 [PM_SUSPEND_STANDBY] = "standby",
37 [PM_SUSPEND_MEM] = "mem",
38 };
39
40 static const struct platform_suspend_ops *suspend_ops;
41
42 static bool need_suspend_ops(suspend_state_t state)
43 {
44 return state > PM_SUSPEND_FREEZE;
45 }
46
47 static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head);
48 static bool suspend_freeze_wake;
49
50 static void freeze_begin(void)
51 {
52 suspend_freeze_wake = false;
53 }
54
55 static void freeze_enter(void)
56 {
57 cpuidle_resume();
58 wait_event(suspend_freeze_wait_head, suspend_freeze_wake);
59 cpuidle_pause();
60 }
61
62 void freeze_wake(void)
63 {
64 suspend_freeze_wake = true;
65 wake_up(&suspend_freeze_wait_head);
66 }
67 EXPORT_SYMBOL_GPL(freeze_wake);
68
69 /**
70 * suspend_set_ops - Set the global suspend method table.
71 * @ops: Suspend operations to use.
72 */
73 void suspend_set_ops(const struct platform_suspend_ops *ops)
74 {
75 lock_system_sleep();
76 suspend_ops = ops;
77 unlock_system_sleep();
78 }
79 EXPORT_SYMBOL_GPL(suspend_set_ops);
80
81 bool valid_state(suspend_state_t state)
82 {
83 if (state == PM_SUSPEND_FREEZE) {
84 #ifdef CONFIG_PM_DEBUG
85 if (pm_test_level != TEST_NONE &&
86 pm_test_level != TEST_FREEZER &&
87 pm_test_level != TEST_DEVICES &&
88 pm_test_level != TEST_PLATFORM) {
89 printk(KERN_WARNING "Unsupported pm_test mode for "
90 "freeze state, please choose "
91 "none/freezer/devices/platform.\n");
92 return false;
93 }
94 #endif
95 return true;
96 }
97 /*
98 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEMORY states need lowlevel
99 * support and need to be valid to the lowlevel
100 * implementation, no valid callback implies that none are valid.
101 */
102 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
103 }
104
105 /**
106 * suspend_valid_only_mem - Generic memory-only valid callback.
107 *
108 * Platform drivers that implement mem suspend only and only need to check for
109 * that in their .valid() callback can use this instead of rolling their own
110 * .valid() callback.
111 */
112 int suspend_valid_only_mem(suspend_state_t state)
113 {
114 return state == PM_SUSPEND_MEM;
115 }
116 EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
117
118 static int suspend_test(int level)
119 {
120 #ifdef CONFIG_PM_DEBUG
121 if (pm_test_level == level) {
122 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
123 mdelay(5000);
124 return 1;
125 }
126 #endif /* !CONFIG_PM_DEBUG */
127 return 0;
128 }
129
130 /**
131 * suspend_prepare - Prepare for entering system sleep state.
132 *
133 * Common code run for every system sleep state that can be entered (except for
134 * hibernation). Run suspend notifiers, allocate the "suspend" console and
135 * freeze processes.
136 */
137 static int suspend_prepare(suspend_state_t state)
138 {
139 int error;
140
141 if (need_suspend_ops(state) && (!suspend_ops || !suspend_ops->enter))
142 return -EPERM;
143
144 pm_prepare_console();
145
146 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
147 if (error)
148 goto Finish;
149
150 error = suspend_freeze_processes();
151 if (!error)
152 return 0;
153
154 suspend_stats.failed_freeze++;
155 dpm_save_failed_step(SUSPEND_FREEZE);
156 Finish:
157 pm_notifier_call_chain(PM_POST_SUSPEND);
158 pm_restore_console();
159 return error;
160 }
161
162 /* default implementation */
163 void __weak arch_suspend_disable_irqs(void)
164 {
165 local_irq_disable();
166 }
167
168 /* default implementation */
169 void __weak arch_suspend_enable_irqs(void)
170 {
171 local_irq_enable();
172 }
173
174 /**
175 * suspend_enter - Make the system enter the given sleep state.
176 * @state: System sleep state to enter.
177 * @wakeup: Returns information that the sleep state should not be re-entered.
178 *
179 * This function should be called after devices have been suspended.
180 */
181 static int suspend_enter(suspend_state_t state, bool *wakeup)
182 {
183 int error;
184
185 if (need_suspend_ops(state) && suspend_ops->prepare) {
186 error = suspend_ops->prepare();
187 if (error)
188 goto Platform_finish;
189 }
190
191 error = dpm_suspend_end(PMSG_SUSPEND);
192 if (error) {
193 printk(KERN_ERR "PM: Some devices failed to power down\n");
194 goto Platform_finish;
195 }
196
197 if (need_suspend_ops(state) && suspend_ops->prepare_late) {
198 error = suspend_ops->prepare_late();
199 if (error)
200 goto Platform_wake;
201 }
202
203 if (suspend_test(TEST_PLATFORM))
204 goto Platform_wake;
205
206 /*
207 * PM_SUSPEND_FREEZE equals
208 * frozen processes + suspended devices + idle processors.
209 * Thus we should invoke freeze_enter() soon after
210 * all the devices are suspended.
211 */
212 if (state == PM_SUSPEND_FREEZE) {
213 freeze_enter();
214 goto Platform_wake;
215 }
216
217 ftrace_stop();
218 error = disable_nonboot_cpus();
219 if (error || suspend_test(TEST_CPUS))
220 goto Enable_cpus;
221
222 arch_suspend_disable_irqs();
223 BUG_ON(!irqs_disabled());
224
225 error = syscore_suspend();
226 if (!error) {
227 *wakeup = pm_wakeup_pending();
228 if (!(suspend_test(TEST_CORE) || *wakeup)) {
229 error = suspend_ops->enter(state);
230 events_check_enabled = false;
231 }
232 syscore_resume();
233 }
234
235 arch_suspend_enable_irqs();
236 BUG_ON(irqs_disabled());
237
238 Enable_cpus:
239 enable_nonboot_cpus();
240 ftrace_start();
241
242 Platform_wake:
243 if (need_suspend_ops(state) && suspend_ops->wake)
244 suspend_ops->wake();
245
246 dpm_resume_start(PMSG_RESUME);
247
248 Platform_finish:
249 if (need_suspend_ops(state) && suspend_ops->finish)
250 suspend_ops->finish();
251
252 return error;
253 }
254
255 /**
256 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
257 * @state: System sleep state to enter.
258 */
259 int suspend_devices_and_enter(suspend_state_t state)
260 {
261 int error;
262 bool wakeup = false;
263
264 if (need_suspend_ops(state) && !suspend_ops)
265 return -ENOSYS;
266
267 trace_machine_suspend(state);
268 if (need_suspend_ops(state) && suspend_ops->begin) {
269 error = suspend_ops->begin(state);
270 if (error)
271 goto Close;
272 }
273 suspend_console();
274 suspend_test_start();
275 error = dpm_suspend_start(PMSG_SUSPEND);
276 if (error) {
277 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
278 goto Recover_platform;
279 }
280 suspend_test_finish("suspend devices");
281 if (suspend_test(TEST_DEVICES))
282 goto Recover_platform;
283
284 do {
285 error = suspend_enter(state, &wakeup);
286 } while (!error && !wakeup && need_suspend_ops(state)
287 && suspend_ops->suspend_again && suspend_ops->suspend_again());
288
289 Resume_devices:
290 suspend_test_start();
291 dpm_resume_end(PMSG_RESUME);
292 suspend_test_finish("resume devices");
293 resume_console();
294 Close:
295 if (need_suspend_ops(state) && suspend_ops->end)
296 suspend_ops->end();
297 trace_machine_suspend(PWR_EVENT_EXIT);
298 return error;
299
300 Recover_platform:
301 if (need_suspend_ops(state) && suspend_ops->recover)
302 suspend_ops->recover();
303 goto Resume_devices;
304 }
305
306 /**
307 * suspend_finish - Clean up before finishing the suspend sequence.
308 *
309 * Call platform code to clean up, restart processes, and free the console that
310 * we've allocated. This routine is not called for hibernation.
311 */
312 static void suspend_finish(void)
313 {
314 suspend_thaw_processes();
315 pm_notifier_call_chain(PM_POST_SUSPEND);
316 pm_restore_console();
317 }
318
319 /**
320 * enter_state - Do common work needed to enter system sleep state.
321 * @state: System sleep state to enter.
322 *
323 * Make sure that no one else is trying to put the system into a sleep state.
324 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
325 * system enter the given sleep state and clean up after wakeup.
326 */
327 static int enter_state(suspend_state_t state)
328 {
329 int error;
330
331 if (!valid_state(state))
332 return -ENODEV;
333
334 if (!mutex_trylock(&pm_mutex))
335 return -EBUSY;
336
337 if (state == PM_SUSPEND_FREEZE)
338 freeze_begin();
339
340 printk(KERN_INFO "PM: Syncing filesystems ... ");
341 sys_sync();
342 printk("done.\n");
343
344 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
345 error = suspend_prepare(state);
346 if (error)
347 goto Unlock;
348
349 if (suspend_test(TEST_FREEZER))
350 goto Finish;
351
352 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
353 pm_restrict_gfp_mask();
354 error = suspend_devices_and_enter(state);
355 pm_restore_gfp_mask();
356
357 Finish:
358 pr_debug("PM: Finishing wakeup.\n");
359 suspend_finish();
360 Unlock:
361 mutex_unlock(&pm_mutex);
362 return error;
363 }
364
365 /**
366 * pm_suspend - Externally visible function for suspending the system.
367 * @state: System sleep state to enter.
368 *
369 * Check if the value of @state represents one of the supported states,
370 * execute enter_state() and update system suspend statistics.
371 */
372 int pm_suspend(suspend_state_t state)
373 {
374 int error;
375
376 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
377 return -EINVAL;
378
379 error = enter_state(state);
380 if (error) {
381 suspend_stats.fail++;
382 dpm_save_failed_errno(error);
383 } else {
384 suspend_stats.success++;
385 }
386 return error;
387 }
388 EXPORT_SYMBOL(pm_suspend);
This page took 0.037411 seconds and 5 git commands to generate.