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 <linux/rtc.h>
29 #include <trace/events/power.h>
30 #include <linux/wakeup_reason.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
;
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 wait_event(suspend_freeze_wait_head
, suspend_freeze_wake
);
60 void freeze_wake(void)
62 suspend_freeze_wake
= true;
63 wake_up(&suspend_freeze_wait_head
);
65 EXPORT_SYMBOL_GPL(freeze_wake
);
67 static bool valid_state(suspend_state_t state
)
70 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
71 * support and need to be valid to the low level
72 * implementation, no valid callback implies that none are valid.
74 return suspend_ops
&& suspend_ops
->valid
&& suspend_ops
->valid(state
);
78 * suspend_set_ops - Set the global suspend method table.
79 * @ops: Suspend operations to use.
81 void suspend_set_ops(const struct platform_suspend_ops
*ops
)
88 for (i
= PM_SUSPEND_STANDBY
; i
<= PM_SUSPEND_MEM
; i
++)
89 pm_states
[i
].state
= valid_state(i
) ? i
: 0;
91 unlock_system_sleep();
93 EXPORT_SYMBOL_GPL(suspend_set_ops
);
96 * suspend_valid_only_mem - Generic memory-only valid callback.
98 * Platform drivers that implement mem suspend only and only need to check for
99 * that in their .valid() callback can use this instead of rolling their own
102 int suspend_valid_only_mem(suspend_state_t state
)
104 return state
== PM_SUSPEND_MEM
;
106 EXPORT_SYMBOL_GPL(suspend_valid_only_mem
);
108 static int suspend_test(int level
)
110 #ifdef CONFIG_PM_DEBUG
111 if (pm_test_level
== level
) {
112 printk(KERN_INFO
"suspend debug: Waiting for 5 seconds.\n");
116 #endif /* !CONFIG_PM_DEBUG */
121 * suspend_prepare - Prepare for entering system sleep state.
123 * Common code run for every system sleep state that can be entered (except for
124 * hibernation). Run suspend notifiers, allocate the "suspend" console and
127 static int suspend_prepare(suspend_state_t state
)
131 if (need_suspend_ops(state
) && (!suspend_ops
|| !suspend_ops
->enter
))
134 pm_prepare_console();
136 error
= pm_notifier_call_chain(PM_SUSPEND_PREPARE
);
140 printk(KERN_INFO
"PM: Syncing filesystems ... ");
141 if (intr_sync(NULL
)) {
142 printk("canceled.\n");
148 error
= suspend_freeze_processes();
151 log_suspend_abort_reason("One or more tasks refusing to freeze");
152 suspend_stats
.failed_freeze
++;
153 dpm_save_failed_step(SUSPEND_FREEZE
);
155 pm_notifier_call_chain(PM_POST_SUSPEND
);
156 pm_restore_console();
160 /* default implementation */
161 void __attribute__ ((weak
)) arch_suspend_disable_irqs(void)
166 /* default implementation */
167 void __attribute__ ((weak
)) arch_suspend_enable_irqs(void)
173 * suspend_enter - Make the system enter the given sleep state.
174 * @state: System sleep state to enter.
175 * @wakeup: Returns information that the sleep state should not be re-entered.
177 * This function should be called after devices have been suspended.
179 static int suspend_enter(suspend_state_t state
, bool *wakeup
)
181 char suspend_abort
[MAX_SUSPEND_ABORT_LEN
];
184 if (need_suspend_ops(state
) && suspend_ops
->prepare
) {
185 error
= suspend_ops
->prepare();
187 goto Platform_finish
;
190 error
= dpm_suspend_end(PMSG_SUSPEND
);
192 last_dev
= suspend_stats
.last_failed_dev
+ REC_FAILED_NUM
- 1;
193 last_dev
%= REC_FAILED_NUM
;
194 printk(KERN_ERR
"PM: Some devices failed to power down\n");
195 log_suspend_abort_reason("%s device failed to power down",
196 suspend_stats
.failed_devs
[last_dev
]);
197 goto Platform_finish
;
200 if (need_suspend_ops(state
) && suspend_ops
->prepare_late
) {
201 error
= suspend_ops
->prepare_late();
206 if (suspend_test(TEST_PLATFORM
))
210 * PM_SUSPEND_FREEZE equals
211 * frozen processes + suspended devices + idle processors.
212 * Thus we should invoke freeze_enter() soon after
213 * all the devices are suspended.
215 if (state
== PM_SUSPEND_FREEZE
) {
220 error
= disable_nonboot_cpus();
221 if (error
|| suspend_test(TEST_CPUS
)) {
222 log_suspend_abort_reason("Disabling non-boot cpus failed");
226 arch_suspend_disable_irqs();
227 BUG_ON(!irqs_disabled());
229 error
= syscore_suspend();
231 *wakeup
= pm_wakeup_pending();
232 if (!(suspend_test(TEST_CORE
) || *wakeup
)) {
233 error
= suspend_ops
->enter(state
);
234 events_check_enabled
= false;
236 pm_get_active_wakeup_sources(suspend_abort
,
237 MAX_SUSPEND_ABORT_LEN
);
238 log_suspend_abort_reason(suspend_abort
);
243 arch_suspend_enable_irqs();
244 BUG_ON(irqs_disabled());
247 enable_nonboot_cpus();
250 if (need_suspend_ops(state
) && suspend_ops
->wake
)
253 dpm_resume_start(PMSG_RESUME
);
256 if (need_suspend_ops(state
) && suspend_ops
->finish
)
257 suspend_ops
->finish();
263 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
264 * @state: System sleep state to enter.
266 int suspend_devices_and_enter(suspend_state_t state
)
271 if (need_suspend_ops(state
) && !suspend_ops
)
274 trace_machine_suspend(state
);
275 if (need_suspend_ops(state
) && suspend_ops
->begin
) {
276 error
= suspend_ops
->begin(state
);
282 suspend_test_start();
283 error
= dpm_suspend_start(PMSG_SUSPEND
);
285 printk(KERN_ERR
"PM: Some devices failed to suspend\n");
286 log_suspend_abort_reason("Some devices failed to suspend");
287 goto Recover_platform
;
289 suspend_test_finish("suspend devices");
290 if (suspend_test(TEST_DEVICES
))
291 goto Recover_platform
;
294 error
= suspend_enter(state
, &wakeup
);
295 } while (!error
&& !wakeup
&& need_suspend_ops(state
)
296 && suspend_ops
->suspend_again
&& suspend_ops
->suspend_again());
299 suspend_test_start();
300 dpm_resume_end(PMSG_RESUME
);
301 suspend_test_finish("resume devices");
305 if (need_suspend_ops(state
) && suspend_ops
->end
)
307 trace_machine_suspend(PWR_EVENT_EXIT
);
311 if (need_suspend_ops(state
) && suspend_ops
->recover
)
312 suspend_ops
->recover();
317 * suspend_finish - Clean up before finishing the suspend sequence.
319 * Call platform code to clean up, restart processes, and free the console that
320 * we've allocated. This routine is not called for hibernation.
322 static void suspend_finish(void)
324 suspend_thaw_processes();
325 pm_notifier_call_chain(PM_POST_SUSPEND
);
326 pm_restore_console();
330 * enter_state - Do common work needed to enter system sleep state.
331 * @state: System sleep state to enter.
333 * Make sure that no one else is trying to put the system into a sleep state.
334 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
335 * system enter the given sleep state and clean up after wakeup.
337 static int enter_state(suspend_state_t state
)
341 if (state
== PM_SUSPEND_FREEZE
) {
342 #ifdef CONFIG_PM_DEBUG
343 if (pm_test_level
!= TEST_NONE
&& pm_test_level
<= TEST_CPUS
) {
344 pr_warning("PM: Unsupported test mode for freeze state,"
345 "please choose none/freezer/devices/platform.\n");
349 } else if (!valid_state(state
)) {
352 if (!mutex_trylock(&pm_mutex
))
355 if (state
== PM_SUSPEND_FREEZE
)
358 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
].label
);
359 error
= suspend_prepare(state
);
363 if (suspend_test(TEST_FREEZER
))
366 pr_debug("PM: Entering %s sleep\n", pm_states
[state
].label
);
367 pm_restrict_gfp_mask();
368 error
= suspend_devices_and_enter(state
);
369 pm_restore_gfp_mask();
372 pr_debug("PM: Finishing wakeup.\n");
375 mutex_unlock(&pm_mutex
);
379 static void pm_suspend_marker(char *annotation
)
385 rtc_time_to_tm(ts
.tv_sec
, &tm
);
386 pr_info("PM: suspend %s %d-%02d-%02d %02d:%02d:%02d.%09lu UTC\n",
387 annotation
, tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
,
388 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts
.tv_nsec
);
392 * pm_suspend - Externally visible function for suspending the system.
393 * @state: System sleep state to enter.
395 * Check if the value of @state represents one of the supported states,
396 * execute enter_state() and update system suspend statistics.
398 int pm_suspend(suspend_state_t state
)
402 if (state
<= PM_SUSPEND_ON
|| state
>= PM_SUSPEND_MAX
)
405 pm_suspend_marker("entry");
406 error
= enter_state(state
);
408 suspend_stats
.fail
++;
409 dpm_save_failed_errno(error
);
411 suspend_stats
.success
++;
413 pm_suspend_marker("exit");
416 EXPORT_SYMBOL(pm_suspend
);