2 * kernel/power/suspend.c - Suspend to RAM and standby functionality.
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.
8 * This file is released under the GPLv2.
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>
21 #include <linux/kernel.h>
22 #include <linux/list.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>
34 struct pm_sleep_state pm_states[PM_SUSPEND_MAX] = {
35 [PM_SUSPEND_FREEZE] = { "freeze", PM_SUSPEND_FREEZE },
36 [PM_SUSPEND_STANDBY] = { "standby", PM_SUSPEND_STANDBY },
37 [PM_SUSPEND_MEM] = { "mem", PM_SUSPEND_MEM },
40 static const struct platform_suspend_ops *suspend_ops;
42 static bool need_suspend_ops(suspend_state_t state)
44 return state > PM_SUSPEND_FREEZE;
47 static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head);
48 static bool suspend_freeze_wake;
50 static void freeze_begin(void)
52 suspend_freeze_wake = false;
55 static void freeze_enter(void)
57 cpuidle_use_deepest_state(true);
59 wait_event(suspend_freeze_wait_head, suspend_freeze_wake);
61 cpuidle_use_deepest_state(false);
64 void freeze_wake(void)
66 suspend_freeze_wake = true;
67 wake_up(&suspend_freeze_wait_head);
69 EXPORT_SYMBOL_GPL(freeze_wake);
72 * suspend_set_ops - Set the global suspend method table.
73 * @ops: Suspend operations to use.
75 void suspend_set_ops(const struct platform_suspend_ops *ops)
79 unlock_system_sleep();
81 EXPORT_SYMBOL_GPL(suspend_set_ops);
83 bool valid_state(suspend_state_t state)
85 if (state == PM_SUSPEND_FREEZE) {
86 #ifdef CONFIG_PM_DEBUG
87 if (pm_test_level != TEST_NONE &&
88 pm_test_level != TEST_FREEZER &&
89 pm_test_level != TEST_DEVICES &&
90 pm_test_level != TEST_PLATFORM) {
91 printk(KERN_WARNING "Unsupported pm_test mode for "
92 "freeze state, please choose "
93 "none/freezer/devices/platform.\n");
100 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEMORY states need lowlevel
101 * support and need to be valid to the lowlevel
102 * implementation, no valid callback implies that none are valid.
104 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
108 * suspend_valid_only_mem - Generic memory-only valid callback.
110 * Platform drivers that implement mem suspend only and only need to check for
111 * that in their .valid() callback can use this instead of rolling their own
114 int suspend_valid_only_mem(suspend_state_t state)
116 return state == PM_SUSPEND_MEM;
118 EXPORT_SYMBOL_GPL(suspend_valid_only_mem);
120 static int suspend_test(int level)
122 #ifdef CONFIG_PM_DEBUG
123 if (pm_test_level == level) {
124 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
128 #endif /* !CONFIG_PM_DEBUG */
133 * suspend_prepare - Prepare for entering system sleep state.
135 * Common code run for every system sleep state that can be entered (except for
136 * hibernation). Run suspend notifiers, allocate the "suspend" console and
139 static int suspend_prepare(suspend_state_t state)
143 if (need_suspend_ops(state) && (!suspend_ops || !suspend_ops->enter))
146 pm_prepare_console();
148 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
152 error = suspend_freeze_processes();
156 suspend_stats.failed_freeze++;
157 dpm_save_failed_step(SUSPEND_FREEZE);
159 pm_notifier_call_chain(PM_POST_SUSPEND);
160 pm_restore_console();
164 /* default implementation */
165 void __weak arch_suspend_disable_irqs(void)
170 /* default implementation */
171 void __weak arch_suspend_enable_irqs(void)
177 * suspend_enter - Make the system enter the given sleep state.
178 * @state: System sleep state to enter.
179 * @wakeup: Returns information that the sleep state should not be re-entered.
181 * This function should be called after devices have been suspended.
183 static int suspend_enter(suspend_state_t state, bool *wakeup)
187 if (need_suspend_ops(state) && suspend_ops->prepare) {
188 error = suspend_ops->prepare();
190 goto Platform_finish;
193 error = dpm_suspend_end(PMSG_SUSPEND);
195 printk(KERN_ERR "PM: Some devices failed to power down\n");
196 goto Platform_finish;
199 if (need_suspend_ops(state) && suspend_ops->prepare_late) {
200 error = suspend_ops->prepare_late();
205 if (suspend_test(TEST_PLATFORM))
209 * PM_SUSPEND_FREEZE equals
210 * frozen processes + suspended devices + idle processors.
211 * Thus we should invoke freeze_enter() soon after
212 * all the devices are suspended.
214 if (state == PM_SUSPEND_FREEZE) {
220 error = disable_nonboot_cpus();
221 if (error || suspend_test(TEST_CPUS))
224 arch_suspend_disable_irqs();
225 BUG_ON(!irqs_disabled());
227 error = syscore_suspend();
229 *wakeup = pm_wakeup_pending();
230 if (!(suspend_test(TEST_CORE) || *wakeup)) {
231 error = suspend_ops->enter(state);
232 events_check_enabled = false;
237 arch_suspend_enable_irqs();
238 BUG_ON(irqs_disabled());
241 enable_nonboot_cpus();
245 if (need_suspend_ops(state) && suspend_ops->wake)
248 dpm_resume_start(PMSG_RESUME);
251 if (need_suspend_ops(state) && suspend_ops->finish)
252 suspend_ops->finish();
258 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
259 * @state: System sleep state to enter.
261 int suspend_devices_and_enter(suspend_state_t state)
266 if (need_suspend_ops(state) && !suspend_ops)
269 trace_machine_suspend(state);
270 if (need_suspend_ops(state) && suspend_ops->begin) {
271 error = suspend_ops->begin(state);
276 suspend_test_start();
277 error = dpm_suspend_start(PMSG_SUSPEND);
279 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
280 goto Recover_platform;
282 suspend_test_finish("suspend devices");
283 if (suspend_test(TEST_DEVICES))
284 goto Recover_platform;
287 error = suspend_enter(state, &wakeup);
288 } while (!error && !wakeup && need_suspend_ops(state)
289 && suspend_ops->suspend_again && suspend_ops->suspend_again());
292 suspend_test_start();
293 dpm_resume_end(PMSG_RESUME);
294 suspend_test_finish("resume devices");
297 if (need_suspend_ops(state) && suspend_ops->end)
299 trace_machine_suspend(PWR_EVENT_EXIT);
303 if (need_suspend_ops(state) && suspend_ops->recover)
304 suspend_ops->recover();
309 * suspend_finish - Clean up before finishing the suspend sequence.
311 * Call platform code to clean up, restart processes, and free the console that
312 * we've allocated. This routine is not called for hibernation.
314 static void suspend_finish(void)
316 suspend_thaw_processes();
317 pm_notifier_call_chain(PM_POST_SUSPEND);
318 pm_restore_console();
322 * enter_state - Do common work needed to enter system sleep state.
323 * @state: System sleep state to enter.
325 * Make sure that no one else is trying to put the system into a sleep state.
326 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
327 * system enter the given sleep state and clean up after wakeup.
329 static int enter_state(suspend_state_t state)
333 if (!valid_state(state))
336 if (!mutex_trylock(&pm_mutex))
339 if (state == PM_SUSPEND_FREEZE)
342 printk(KERN_INFO "PM: Syncing filesystems ... ");
346 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state].label);
347 error = suspend_prepare(state);
351 if (suspend_test(TEST_FREEZER))
354 pr_debug("PM: Entering %s sleep\n", pm_states[state].label);
355 pm_restrict_gfp_mask();
356 error = suspend_devices_and_enter(state);
357 pm_restore_gfp_mask();
360 pr_debug("PM: Finishing wakeup.\n");
363 mutex_unlock(&pm_mutex);
368 * pm_suspend - Externally visible function for suspending the system.
369 * @state: System sleep state to enter.
371 * Check if the value of @state represents one of the supported states,
372 * execute enter_state() and update system suspend statistics.
374 int pm_suspend(suspend_state_t state)
378 if (state <= PM_SUSPEND_ON || state >= PM_SUSPEND_MAX)
381 error = enter_state(state);
383 suspend_stats.fail++;
384 dpm_save_failed_errno(error);
386 suspend_stats.success++;
390 EXPORT_SYMBOL(pm_suspend);