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
] = { .label
= "freeze", .state
= PM_SUSPEND_FREEZE
},
36 [PM_SUSPEND_STANDBY
] = { .label
= "standby", },
37 [PM_SUSPEND_MEM
] = { .label
= "mem", },
40 static const struct platform_suspend_ops
*suspend_ops
;
41 static const struct platform_freeze_ops
*freeze_ops
;
43 static bool need_suspend_ops(suspend_state_t state
)
45 return state
> PM_SUSPEND_FREEZE
;
48 static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head
);
49 static bool suspend_freeze_wake
;
51 void freeze_set_ops(const struct platform_freeze_ops
*ops
)
55 unlock_system_sleep();
58 static void freeze_begin(void)
60 suspend_freeze_wake
= false;
63 static void freeze_enter(void)
65 cpuidle_use_deepest_state(true);
67 wait_event(suspend_freeze_wait_head
, suspend_freeze_wake
);
69 cpuidle_use_deepest_state(false);
72 void freeze_wake(void)
74 suspend_freeze_wake
= true;
75 wake_up(&suspend_freeze_wait_head
);
77 EXPORT_SYMBOL_GPL(freeze_wake
);
79 static bool valid_state(suspend_state_t state
)
82 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
83 * support and need to be valid to the low level
84 * implementation, no valid callback implies that none are valid.
86 return suspend_ops
&& suspend_ops
->valid
&& suspend_ops
->valid(state
);
90 * If this is set, the "mem" label always corresponds to the deepest sleep state
91 * available, the "standby" label corresponds to the second deepest sleep state
92 * available (if any), and the "freeze" label corresponds to the remaining
93 * available sleep state (if there is one).
95 static bool relative_states
;
97 static int __init
sleep_states_setup(char *str
)
99 relative_states
= !strncmp(str
, "1", 1);
100 if (relative_states
) {
101 pm_states
[PM_SUSPEND_MEM
].state
= PM_SUSPEND_FREEZE
;
102 pm_states
[PM_SUSPEND_FREEZE
].state
= 0;
107 __setup("relative_sleep_states=", sleep_states_setup
);
110 * suspend_set_ops - Set the global suspend method table.
111 * @ops: Suspend operations to use.
113 void suspend_set_ops(const struct platform_suspend_ops
*ops
)
116 int j
= PM_SUSPEND_MAX
- 1;
121 for (i
= PM_SUSPEND_MEM
; i
>= PM_SUSPEND_STANDBY
; i
--)
123 pm_states
[j
--].state
= i
;
124 else if (!relative_states
)
125 pm_states
[j
--].state
= 0;
127 pm_states
[j
--].state
= PM_SUSPEND_FREEZE
;
128 while (j
>= PM_SUSPEND_MIN
)
129 pm_states
[j
--].state
= 0;
131 unlock_system_sleep();
133 EXPORT_SYMBOL_GPL(suspend_set_ops
);
136 * suspend_valid_only_mem - Generic memory-only valid callback.
138 * Platform drivers that implement mem suspend only and only need to check for
139 * that in their .valid() callback can use this instead of rolling their own
142 int suspend_valid_only_mem(suspend_state_t state
)
144 return state
== PM_SUSPEND_MEM
;
146 EXPORT_SYMBOL_GPL(suspend_valid_only_mem
);
148 static int suspend_test(int level
)
150 #ifdef CONFIG_PM_DEBUG
151 if (pm_test_level
== level
) {
152 printk(KERN_INFO
"suspend debug: Waiting for 5 seconds.\n");
156 #endif /* !CONFIG_PM_DEBUG */
161 * suspend_prepare - Prepare for entering system sleep state.
163 * Common code run for every system sleep state that can be entered (except for
164 * hibernation). Run suspend notifiers, allocate the "suspend" console and
167 static int suspend_prepare(suspend_state_t state
)
171 if (need_suspend_ops(state
) && (!suspend_ops
|| !suspend_ops
->enter
))
174 pm_prepare_console();
176 error
= pm_notifier_call_chain(PM_SUSPEND_PREPARE
);
180 error
= suspend_freeze_processes();
184 suspend_stats
.failed_freeze
++;
185 dpm_save_failed_step(SUSPEND_FREEZE
);
187 pm_notifier_call_chain(PM_POST_SUSPEND
);
188 pm_restore_console();
192 /* default implementation */
193 void __weak
arch_suspend_disable_irqs(void)
198 /* default implementation */
199 void __weak
arch_suspend_enable_irqs(void)
205 * suspend_enter - Make the system enter the given sleep state.
206 * @state: System sleep state to enter.
207 * @wakeup: Returns information that the sleep state should not be re-entered.
209 * This function should be called after devices have been suspended.
211 static int suspend_enter(suspend_state_t state
, bool *wakeup
)
215 if (need_suspend_ops(state
) && suspend_ops
->prepare
) {
216 error
= suspend_ops
->prepare();
218 goto Platform_finish
;
221 error
= dpm_suspend_end(PMSG_SUSPEND
);
223 printk(KERN_ERR
"PM: Some devices failed to power down\n");
224 goto Platform_finish
;
227 if (need_suspend_ops(state
) && suspend_ops
->prepare_late
) {
228 error
= suspend_ops
->prepare_late();
233 if (suspend_test(TEST_PLATFORM
))
237 * PM_SUSPEND_FREEZE equals
238 * frozen processes + suspended devices + idle processors.
239 * Thus we should invoke freeze_enter() soon after
240 * all the devices are suspended.
242 if (state
== PM_SUSPEND_FREEZE
) {
248 error
= disable_nonboot_cpus();
249 if (error
|| suspend_test(TEST_CPUS
))
252 arch_suspend_disable_irqs();
253 BUG_ON(!irqs_disabled());
255 error
= syscore_suspend();
257 *wakeup
= pm_wakeup_pending();
258 if (!(suspend_test(TEST_CORE
) || *wakeup
)) {
259 error
= suspend_ops
->enter(state
);
260 events_check_enabled
= false;
265 arch_suspend_enable_irqs();
266 BUG_ON(irqs_disabled());
269 enable_nonboot_cpus();
273 if (need_suspend_ops(state
) && suspend_ops
->wake
)
276 dpm_resume_start(PMSG_RESUME
);
279 if (need_suspend_ops(state
) && suspend_ops
->finish
)
280 suspend_ops
->finish();
286 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
287 * @state: System sleep state to enter.
289 int suspend_devices_and_enter(suspend_state_t state
)
294 if (need_suspend_ops(state
) && !suspend_ops
)
297 trace_machine_suspend(state
);
298 if (need_suspend_ops(state
) && suspend_ops
->begin
) {
299 error
= suspend_ops
->begin(state
);
302 } else if (state
== PM_SUSPEND_FREEZE
&& freeze_ops
->begin
) {
303 error
= freeze_ops
->begin();
308 suspend_test_start();
309 error
= dpm_suspend_start(PMSG_SUSPEND
);
311 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
312 goto Recover_platform
;
314 suspend_test_finish("suspend devices");
315 if (suspend_test(TEST_DEVICES
))
316 goto Recover_platform
;
319 error
= suspend_enter(state
, &wakeup
);
320 } while (!error
&& !wakeup
&& need_suspend_ops(state
)
321 && suspend_ops
->suspend_again
&& suspend_ops
->suspend_again());
324 suspend_test_start();
325 dpm_resume_end(PMSG_RESUME
);
326 suspend_test_finish("resume devices");
329 if (need_suspend_ops(state
) && suspend_ops
->end
)
331 else if (state
== PM_SUSPEND_FREEZE
&& freeze_ops
->end
)
334 trace_machine_suspend(PWR_EVENT_EXIT
);
338 if (need_suspend_ops(state
) && suspend_ops
->recover
)
339 suspend_ops
->recover();
344 * suspend_finish - Clean up before finishing the suspend sequence.
346 * Call platform code to clean up, restart processes, and free the console that
347 * we've allocated. This routine is not called for hibernation.
349 static void suspend_finish(void)
351 suspend_thaw_processes();
352 pm_notifier_call_chain(PM_POST_SUSPEND
);
353 pm_restore_console();
357 * enter_state - Do common work needed to enter system sleep state.
358 * @state: System sleep state to enter.
360 * Make sure that no one else is trying to put the system into a sleep state.
361 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
362 * system enter the given sleep state and clean up after wakeup.
364 static int enter_state(suspend_state_t state
)
368 if (state
== PM_SUSPEND_FREEZE
) {
369 #ifdef CONFIG_PM_DEBUG
370 if (pm_test_level
!= TEST_NONE
&& pm_test_level
<= TEST_CPUS
) {
371 pr_warning("PM: Unsupported test mode for freeze state,"
372 "please choose none/freezer/devices/platform.\n");
376 } else if (!valid_state(state
)) {
379 if (!mutex_trylock(&pm_mutex
))
382 if (state
== PM_SUSPEND_FREEZE
)
385 printk(KERN_INFO
"PM: Syncing filesystems ... ");
389 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
].label
);
390 error
= suspend_prepare(state
);
394 if (suspend_test(TEST_FREEZER
))
397 pr_debug("PM: Entering %s sleep\n", pm_states
[state
].label
);
398 pm_restrict_gfp_mask();
399 error
= suspend_devices_and_enter(state
);
400 pm_restore_gfp_mask();
403 pr_debug("PM: Finishing wakeup.\n");
406 mutex_unlock(&pm_mutex
);
411 * pm_suspend - Externally visible function for suspending the system.
412 * @state: System sleep state to enter.
414 * Check if the value of @state represents one of the supported states,
415 * execute enter_state() and update system suspend statistics.
417 int pm_suspend(suspend_state_t state
)
421 if (state
<= PM_SUSPEND_ON
|| state
>= PM_SUSPEND_MAX
)
424 error
= enter_state(state
);
426 suspend_stats
.fail
++;
427 dpm_save_failed_errno(error
);
429 suspend_stats
.success
++;
433 EXPORT_SYMBOL(pm_suspend
);