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 const char *const pm_states
[PM_SUSPEND_MAX
] = {
35 [PM_SUSPEND_FREEZE
] = "freeze",
36 [PM_SUSPEND_STANDBY
] = "standby",
37 [PM_SUSPEND_MEM
] = "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)
58 wait_event(suspend_freeze_wait_head
, suspend_freeze_wake
);
62 void freeze_wake(void)
64 suspend_freeze_wake
= true;
65 wake_up(&suspend_freeze_wait_head
);
67 EXPORT_SYMBOL_GPL(freeze_wake
);
70 * suspend_set_ops - Set the global suspend method table.
71 * @ops: Suspend operations to use.
73 void suspend_set_ops(const struct platform_suspend_ops
*ops
)
77 unlock_system_sleep();
79 EXPORT_SYMBOL_GPL(suspend_set_ops
);
81 bool valid_state(suspend_state_t state
)
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");
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.
102 return suspend_ops
&& suspend_ops
->valid
&& suspend_ops
->valid(state
);
106 * suspend_valid_only_mem - Generic memory-only valid callback.
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
112 int suspend_valid_only_mem(suspend_state_t state
)
114 return state
== PM_SUSPEND_MEM
;
116 EXPORT_SYMBOL_GPL(suspend_valid_only_mem
);
118 static int suspend_test(int level
)
120 #ifdef CONFIG_PM_DEBUG
121 if (pm_test_level
== level
) {
122 printk(KERN_INFO
"suspend debug: Waiting for 5 seconds.\n");
126 #endif /* !CONFIG_PM_DEBUG */
131 * suspend_prepare - Prepare for entering system sleep state.
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
137 static int suspend_prepare(suspend_state_t state
)
141 if (need_suspend_ops(state
) && (!suspend_ops
|| !suspend_ops
->enter
))
144 pm_prepare_console();
146 error
= pm_notifier_call_chain(PM_SUSPEND_PREPARE
);
150 error
= suspend_freeze_processes();
154 suspend_stats
.failed_freeze
++;
155 dpm_save_failed_step(SUSPEND_FREEZE
);
157 pm_notifier_call_chain(PM_POST_SUSPEND
);
158 pm_restore_console();
162 /* default implementation */
163 void __weak
arch_suspend_disable_irqs(void)
168 /* default implementation */
169 void __weak
arch_suspend_enable_irqs(void)
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.
179 * This function should be called after devices have been suspended.
181 static int suspend_enter(suspend_state_t state
, bool *wakeup
)
185 if (need_suspend_ops(state
) && suspend_ops
->prepare
) {
186 error
= suspend_ops
->prepare();
188 goto Platform_finish
;
191 error
= dpm_suspend_end(PMSG_SUSPEND
);
193 printk(KERN_ERR
"PM: Some devices failed to power down\n");
194 goto Platform_finish
;
197 if (need_suspend_ops(state
) && suspend_ops
->prepare_late
) {
198 error
= suspend_ops
->prepare_late();
203 if (suspend_test(TEST_PLATFORM
))
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.
212 if (state
== PM_SUSPEND_FREEZE
) {
218 error
= disable_nonboot_cpus();
219 if (error
|| suspend_test(TEST_CPUS
))
222 arch_suspend_disable_irqs();
223 BUG_ON(!irqs_disabled());
225 error
= syscore_suspend();
227 *wakeup
= pm_wakeup_pending();
228 if (!(suspend_test(TEST_CORE
) || *wakeup
)) {
229 error
= suspend_ops
->enter(state
);
230 events_check_enabled
= false;
235 arch_suspend_enable_irqs();
236 BUG_ON(irqs_disabled());
239 enable_nonboot_cpus();
243 if (need_suspend_ops(state
) && suspend_ops
->wake
)
246 dpm_resume_start(PMSG_RESUME
);
249 if (need_suspend_ops(state
) && suspend_ops
->finish
)
250 suspend_ops
->finish();
256 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
257 * @state: System sleep state to enter.
259 int suspend_devices_and_enter(suspend_state_t state
)
264 if (need_suspend_ops(state
) && !suspend_ops
)
267 trace_machine_suspend(state
);
268 if (need_suspend_ops(state
) && suspend_ops
->begin
) {
269 error
= suspend_ops
->begin(state
);
274 suspend_test_start();
275 error
= dpm_suspend_start(PMSG_SUSPEND
);
277 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
278 goto Recover_platform
;
280 suspend_test_finish("suspend devices");
281 if (suspend_test(TEST_DEVICES
))
282 goto Recover_platform
;
285 error
= suspend_enter(state
, &wakeup
);
286 } while (!error
&& !wakeup
&& need_suspend_ops(state
)
287 && suspend_ops
->suspend_again
&& suspend_ops
->suspend_again());
290 suspend_test_start();
291 dpm_resume_end(PMSG_RESUME
);
292 suspend_test_finish("resume devices");
295 if (need_suspend_ops(state
) && suspend_ops
->end
)
297 trace_machine_suspend(PWR_EVENT_EXIT
);
301 if (need_suspend_ops(state
) && suspend_ops
->recover
)
302 suspend_ops
->recover();
307 * suspend_finish - Clean up before finishing the suspend sequence.
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.
312 static void suspend_finish(void)
314 suspend_thaw_processes();
315 pm_notifier_call_chain(PM_POST_SUSPEND
);
316 pm_restore_console();
320 * enter_state - Do common work needed to enter system sleep state.
321 * @state: System sleep state to enter.
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.
327 static int enter_state(suspend_state_t state
)
331 if (!valid_state(state
))
334 if (!mutex_trylock(&pm_mutex
))
337 if (state
== PM_SUSPEND_FREEZE
)
340 printk(KERN_INFO
"PM: Syncing filesystems ... ");
344 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
345 error
= suspend_prepare(state
);
349 if (suspend_test(TEST_FREEZER
))
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();
358 pr_debug("PM: Finishing wakeup.\n");
361 mutex_unlock(&pm_mutex
);
366 * pm_suspend - Externally visible function for suspending the system.
367 * @state: System sleep state to enter.
369 * Check if the value of @state represents one of the supported states,
370 * execute enter_state() and update system suspend statistics.
372 int pm_suspend(suspend_state_t state
)
376 if (state
<= PM_SUSPEND_ON
|| state
>= PM_SUSPEND_MAX
)
379 error
= enter_state(state
);
381 suspend_stats
.fail
++;
382 dpm_save_failed_errno(error
);
384 suspend_stats
.success
++;
388 EXPORT_SYMBOL(pm_suspend
);
This page took 0.037609 seconds and 6 git commands to generate.