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/syscalls.h>
18 #include <linux/gfp.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
23 #include <linux/slab.h>
24 #include <linux/export.h>
25 #include <linux/suspend.h>
26 #include <linux/syscore_ops.h>
27 #include <linux/ftrace.h>
28 #include <trace/events/power.h>
32 const char *const pm_states
[PM_SUSPEND_MAX
] = {
33 [PM_SUSPEND_FREEZE
] = "freeze",
34 [PM_SUSPEND_STANDBY
] = "standby",
35 [PM_SUSPEND_MEM
] = "mem",
38 static const struct platform_suspend_ops
*suspend_ops
;
40 static bool need_suspend_ops(suspend_state_t state
)
42 return !!(state
> PM_SUSPEND_FREEZE
);
45 static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head
);
46 static bool suspend_freeze_wake
;
48 static void freeze_begin(void)
50 suspend_freeze_wake
= false;
53 static void freeze_enter(void)
55 wait_event(suspend_freeze_wait_head
, suspend_freeze_wake
);
58 void freeze_wake(void)
60 suspend_freeze_wake
= true;
61 wake_up(&suspend_freeze_wait_head
);
63 EXPORT_SYMBOL_GPL(freeze_wake
);
66 * suspend_set_ops - Set the global suspend method table.
67 * @ops: Suspend operations to use.
69 void suspend_set_ops(const struct platform_suspend_ops
*ops
)
73 unlock_system_sleep();
75 EXPORT_SYMBOL_GPL(suspend_set_ops
);
77 bool valid_state(suspend_state_t state
)
79 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");
94 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEMORY states need lowlevel
95 * support and need to be valid to the lowlevel
96 * implementation, no valid callback implies that none are valid.
98 return suspend_ops
&& suspend_ops
->valid
&& suspend_ops
->valid(state
);
102 * suspend_valid_only_mem - Generic memory-only valid callback.
104 * 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
108 int suspend_valid_only_mem(suspend_state_t state
)
110 return state
== PM_SUSPEND_MEM
;
112 EXPORT_SYMBOL_GPL(suspend_valid_only_mem
);
114 static int suspend_test(int level
)
116 #ifdef CONFIG_PM_DEBUG
117 if (pm_test_level
== level
) {
118 printk(KERN_INFO
"suspend debug: Waiting for 5 seconds.\n");
122 #endif /* !CONFIG_PM_DEBUG */
127 * suspend_prepare - Prepare for entering system sleep state.
129 * Common code run for every system sleep state that can be entered (except for
130 * hibernation). Run suspend notifiers, allocate the "suspend" console and
133 static int suspend_prepare(suspend_state_t state
)
137 if (need_suspend_ops(state
) && (!suspend_ops
|| !suspend_ops
->enter
))
140 pm_prepare_console();
142 error
= pm_notifier_call_chain(PM_SUSPEND_PREPARE
);
146 error
= suspend_freeze_processes();
150 suspend_stats
.failed_freeze
++;
151 dpm_save_failed_step(SUSPEND_FREEZE
);
153 pm_notifier_call_chain(PM_POST_SUSPEND
);
154 pm_restore_console();
158 /* default implementation */
159 void __attribute__ ((weak
)) arch_suspend_disable_irqs(void)
164 /* default implementation */
165 void __attribute__ ((weak
)) arch_suspend_enable_irqs(void)
171 * 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.
175 * This function should be called after devices have been suspended.
177 static int suspend_enter(suspend_state_t state
, bool *wakeup
)
181 if (need_suspend_ops(state
) && suspend_ops
->prepare
) {
182 error
= suspend_ops
->prepare();
184 goto Platform_finish
;
187 error
= dpm_suspend_end(PMSG_SUSPEND
);
189 printk(KERN_ERR
"PM: Some devices failed to power down\n");
190 goto Platform_finish
;
193 if (need_suspend_ops(state
) && suspend_ops
->prepare_late
) {
194 error
= suspend_ops
->prepare_late();
199 if (suspend_test(TEST_PLATFORM
))
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.
208 if (state
== PM_SUSPEND_FREEZE
) {
214 error
= disable_nonboot_cpus();
215 if (error
|| suspend_test(TEST_CPUS
))
218 arch_suspend_disable_irqs();
219 BUG_ON(!irqs_disabled());
221 error
= syscore_suspend();
223 *wakeup
= pm_wakeup_pending();
224 if (!(suspend_test(TEST_CORE
) || *wakeup
)) {
225 error
= suspend_ops
->enter(state
);
226 events_check_enabled
= false;
231 arch_suspend_enable_irqs();
232 BUG_ON(irqs_disabled());
235 enable_nonboot_cpus();
239 if (need_suspend_ops(state
) && suspend_ops
->wake
)
242 dpm_resume_start(PMSG_RESUME
);
245 if (need_suspend_ops(state
) && suspend_ops
->finish
)
246 suspend_ops
->finish();
252 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
253 * @state: System sleep state to enter.
255 int suspend_devices_and_enter(suspend_state_t state
)
260 if (need_suspend_ops(state
) && !suspend_ops
)
263 trace_machine_suspend(state
);
264 if (need_suspend_ops(state
) && suspend_ops
->begin
) {
265 error
= suspend_ops
->begin(state
);
270 suspend_test_start();
271 error
= dpm_suspend_start(PMSG_SUSPEND
);
273 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
274 goto Recover_platform
;
276 suspend_test_finish("suspend devices");
277 if (suspend_test(TEST_DEVICES
))
278 goto Recover_platform
;
281 error
= suspend_enter(state
, &wakeup
);
282 } while (!error
&& !wakeup
&& need_suspend_ops(state
)
283 && suspend_ops
->suspend_again
&& suspend_ops
->suspend_again());
286 suspend_test_start();
287 dpm_resume_end(PMSG_RESUME
);
288 suspend_test_finish("resume devices");
291 if (need_suspend_ops(state
) && suspend_ops
->end
)
293 trace_machine_suspend(PWR_EVENT_EXIT
);
297 if (need_suspend_ops(state
) && suspend_ops
->recover
)
298 suspend_ops
->recover();
303 * suspend_finish - Clean up before finishing the suspend sequence.
305 * Call platform code to clean up, restart processes, and free the console that
306 * we've allocated. This routine is not called for hibernation.
308 static void suspend_finish(void)
310 suspend_thaw_processes();
311 pm_notifier_call_chain(PM_POST_SUSPEND
);
312 pm_restore_console();
316 * enter_state - Do common work needed to enter system sleep state.
317 * @state: System sleep state to enter.
319 * 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.
323 static int enter_state(suspend_state_t state
)
327 if (!valid_state(state
))
330 if (!mutex_trylock(&pm_mutex
))
333 if (state
== PM_SUSPEND_FREEZE
)
336 printk(KERN_INFO
"PM: Syncing filesystems ... ");
340 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
341 error
= suspend_prepare(state
);
345 if (suspend_test(TEST_FREEZER
))
348 pr_debug("PM: Entering %s sleep\n", pm_states
[state
]);
349 pm_restrict_gfp_mask();
350 error
= suspend_devices_and_enter(state
);
351 pm_restore_gfp_mask();
354 pr_debug("PM: Finishing wakeup.\n");
357 mutex_unlock(&pm_mutex
);
362 * pm_suspend - Externally visible function for suspending the system.
363 * @state: System sleep state to enter.
365 * Check if the value of @state represents one of the supported states,
366 * execute enter_state() and update system suspend statistics.
368 int pm_suspend(suspend_state_t state
)
372 if (state
<= PM_SUSPEND_ON
|| state
>= PM_SUSPEND_MAX
)
375 error
= enter_state(state
);
377 suspend_stats
.fail
++;
378 dpm_save_failed_errno(error
);
380 suspend_stats
.success
++;
384 EXPORT_SYMBOL(pm_suspend
);