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 #define pr_fmt(fmt) "PM: " fmt
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/console.h>
18 #include <linux/cpu.h>
19 #include <linux/cpuidle.h>
20 #include <linux/syscalls.h>
21 #include <linux/gfp.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
26 #include <linux/slab.h>
27 #include <linux/export.h>
28 #include <linux/suspend.h>
29 #include <linux/syscore_ops.h>
30 #include <linux/ftrace.h>
31 #include <trace/events/power.h>
32 #include <linux/compiler.h>
33 #include <linux/moduleparam.h>
37 const char * const pm_labels
[] = {
38 [PM_SUSPEND_TO_IDLE
] = "freeze",
39 [PM_SUSPEND_STANDBY
] = "standby",
40 [PM_SUSPEND_MEM
] = "mem",
42 const char *pm_states
[PM_SUSPEND_MAX
];
43 static const char * const mem_sleep_labels
[] = {
44 [PM_SUSPEND_TO_IDLE
] = "s2idle",
45 [PM_SUSPEND_STANDBY
] = "shallow",
46 [PM_SUSPEND_MEM
] = "deep",
48 const char *mem_sleep_states
[PM_SUSPEND_MAX
];
50 suspend_state_t mem_sleep_current
= PM_SUSPEND_TO_IDLE
;
51 suspend_state_t mem_sleep_default
= PM_SUSPEND_MAX
;
52 suspend_state_t pm_suspend_target_state
;
53 EXPORT_SYMBOL_GPL(pm_suspend_target_state
);
55 unsigned int pm_suspend_global_flags
;
56 EXPORT_SYMBOL_GPL(pm_suspend_global_flags
);
58 static const struct platform_suspend_ops
*suspend_ops
;
59 static const struct platform_s2idle_ops
*s2idle_ops
;
60 static DECLARE_WAIT_QUEUE_HEAD(s2idle_wait_head
);
62 enum s2idle_states __read_mostly s2idle_state
;
63 static DEFINE_SPINLOCK(s2idle_lock
);
65 void s2idle_set_ops(const struct platform_s2idle_ops
*ops
)
69 unlock_system_sleep();
72 static void s2idle_begin(void)
74 s2idle_state
= S2IDLE_STATE_NONE
;
77 static void s2idle_enter(void)
79 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE
, true);
81 spin_lock_irq(&s2idle_lock
);
82 if (pm_wakeup_pending())
85 s2idle_state
= S2IDLE_STATE_ENTER
;
86 spin_unlock_irq(&s2idle_lock
);
91 /* Push all the CPUs into the idle loop. */
92 wake_up_all_idle_cpus();
93 /* Make the current CPU wait so it can enter the idle loop too. */
94 wait_event(s2idle_wait_head
,
95 s2idle_state
== S2IDLE_STATE_WAKE
);
100 spin_lock_irq(&s2idle_lock
);
103 s2idle_state
= S2IDLE_STATE_NONE
;
104 spin_unlock_irq(&s2idle_lock
);
106 trace_suspend_resume(TPS("machine_suspend"), PM_SUSPEND_TO_IDLE
, false);
109 static void s2idle_loop(void)
111 pm_pr_dbg("suspend-to-idle\n");
119 * Suspend-to-idle equals
120 * frozen processes + suspended devices + idle processors.
121 * Thus s2idle_enter() should be called right after
122 * all devices have been suspended.
124 error
= dpm_noirq_suspend_devices(PMSG_SUSPEND
);
128 dpm_noirq_resume_devices(PMSG_RESUME
);
129 if (error
&& (error
!= -EBUSY
|| !pm_wakeup_pending())) {
134 if (s2idle_ops
&& s2idle_ops
->wake
)
139 if (s2idle_ops
&& s2idle_ops
->sync
)
142 if (pm_wakeup_pending())
145 pm_wakeup_clear(false);
148 pm_pr_dbg("resume from suspend-to-idle\n");
151 void s2idle_wake(void)
155 spin_lock_irqsave(&s2idle_lock
, flags
);
156 if (s2idle_state
> S2IDLE_STATE_NONE
) {
157 s2idle_state
= S2IDLE_STATE_WAKE
;
158 wake_up(&s2idle_wait_head
);
160 spin_unlock_irqrestore(&s2idle_lock
, flags
);
162 EXPORT_SYMBOL_GPL(s2idle_wake
);
164 static bool valid_state(suspend_state_t state
)
167 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
168 * support and need to be valid to the low level
169 * implementation, no valid callback implies that none are valid.
171 return suspend_ops
&& suspend_ops
->valid
&& suspend_ops
->valid(state
);
174 void __init
pm_states_init(void)
176 /* "mem" and "freeze" are always present in /sys/power/state. */
177 pm_states
[PM_SUSPEND_MEM
] = pm_labels
[PM_SUSPEND_MEM
];
178 pm_states
[PM_SUSPEND_TO_IDLE
] = pm_labels
[PM_SUSPEND_TO_IDLE
];
180 * Suspend-to-idle should be supported even without any suspend_ops,
181 * initialize mem_sleep_states[] accordingly here.
183 mem_sleep_states
[PM_SUSPEND_TO_IDLE
] = mem_sleep_labels
[PM_SUSPEND_TO_IDLE
];
186 static int __init
mem_sleep_default_setup(char *str
)
188 suspend_state_t state
;
190 for (state
= PM_SUSPEND_TO_IDLE
; state
<= PM_SUSPEND_MEM
; state
++)
191 if (mem_sleep_labels
[state
] &&
192 !strcmp(str
, mem_sleep_labels
[state
])) {
193 mem_sleep_default
= state
;
199 __setup("mem_sleep_default=", mem_sleep_default_setup
);
202 * suspend_set_ops - Set the global suspend method table.
203 * @ops: Suspend operations to use.
205 void suspend_set_ops(const struct platform_suspend_ops
*ops
)
211 if (valid_state(PM_SUSPEND_STANDBY
)) {
212 mem_sleep_states
[PM_SUSPEND_STANDBY
] = mem_sleep_labels
[PM_SUSPEND_STANDBY
];
213 pm_states
[PM_SUSPEND_STANDBY
] = pm_labels
[PM_SUSPEND_STANDBY
];
214 if (mem_sleep_default
== PM_SUSPEND_STANDBY
)
215 mem_sleep_current
= PM_SUSPEND_STANDBY
;
217 if (valid_state(PM_SUSPEND_MEM
)) {
218 mem_sleep_states
[PM_SUSPEND_MEM
] = mem_sleep_labels
[PM_SUSPEND_MEM
];
219 if (mem_sleep_default
>= PM_SUSPEND_MEM
)
220 mem_sleep_current
= PM_SUSPEND_MEM
;
223 unlock_system_sleep();
225 EXPORT_SYMBOL_GPL(suspend_set_ops
);
228 * suspend_valid_only_mem - Generic memory-only valid callback.
230 * Platform drivers that implement mem suspend only and only need to check for
231 * that in their .valid() callback can use this instead of rolling their own
234 int suspend_valid_only_mem(suspend_state_t state
)
236 return state
== PM_SUSPEND_MEM
;
238 EXPORT_SYMBOL_GPL(suspend_valid_only_mem
);
240 static bool sleep_state_supported(suspend_state_t state
)
242 return state
== PM_SUSPEND_TO_IDLE
|| (suspend_ops
&& suspend_ops
->enter
);
245 static int platform_suspend_prepare(suspend_state_t state
)
247 return state
!= PM_SUSPEND_TO_IDLE
&& suspend_ops
->prepare
?
248 suspend_ops
->prepare() : 0;
251 static int platform_suspend_prepare_late(suspend_state_t state
)
253 return state
== PM_SUSPEND_TO_IDLE
&& s2idle_ops
&& s2idle_ops
->prepare
?
254 s2idle_ops
->prepare() : 0;
257 static int platform_suspend_prepare_noirq(suspend_state_t state
)
259 return state
!= PM_SUSPEND_TO_IDLE
&& suspend_ops
->prepare_late
?
260 suspend_ops
->prepare_late() : 0;
263 static void platform_resume_noirq(suspend_state_t state
)
265 if (state
!= PM_SUSPEND_TO_IDLE
&& suspend_ops
->wake
)
269 static void platform_resume_early(suspend_state_t state
)
271 if (state
== PM_SUSPEND_TO_IDLE
&& s2idle_ops
&& s2idle_ops
->restore
)
272 s2idle_ops
->restore();
275 static void platform_resume_finish(suspend_state_t state
)
277 if (state
!= PM_SUSPEND_TO_IDLE
&& suspend_ops
->finish
)
278 suspend_ops
->finish();
281 static int platform_suspend_begin(suspend_state_t state
)
283 if (state
== PM_SUSPEND_TO_IDLE
&& s2idle_ops
&& s2idle_ops
->begin
)
284 return s2idle_ops
->begin();
285 else if (suspend_ops
&& suspend_ops
->begin
)
286 return suspend_ops
->begin(state
);
291 static void platform_resume_end(suspend_state_t state
)
293 if (state
== PM_SUSPEND_TO_IDLE
&& s2idle_ops
&& s2idle_ops
->end
)
295 else if (suspend_ops
&& suspend_ops
->end
)
299 static void platform_recover(suspend_state_t state
)
301 if (state
!= PM_SUSPEND_TO_IDLE
&& suspend_ops
->recover
)
302 suspend_ops
->recover();
305 static bool platform_suspend_again(suspend_state_t state
)
307 return state
!= PM_SUSPEND_TO_IDLE
&& suspend_ops
->suspend_again
?
308 suspend_ops
->suspend_again() : false;
311 #ifdef CONFIG_PM_DEBUG
312 static unsigned int pm_test_delay
= 5;
313 module_param(pm_test_delay
, uint
, 0644);
314 MODULE_PARM_DESC(pm_test_delay
,
315 "Number of seconds to wait before resuming from suspend test");
318 static int suspend_test(int level
)
320 #ifdef CONFIG_PM_DEBUG
321 if (pm_test_level
== level
) {
322 pr_info("suspend debug: Waiting for %d second(s).\n",
324 mdelay(pm_test_delay
* 1000);
327 #endif /* !CONFIG_PM_DEBUG */
332 * suspend_prepare - Prepare for entering system sleep state.
334 * Common code run for every system sleep state that can be entered (except for
335 * hibernation). Run suspend notifiers, allocate the "suspend" console and
338 static int suspend_prepare(suspend_state_t state
)
340 int error
, nr_calls
= 0;
342 if (!sleep_state_supported(state
))
345 pm_prepare_console();
347 error
= __pm_notifier_call_chain(PM_SUSPEND_PREPARE
, -1, &nr_calls
);
353 trace_suspend_resume(TPS("freeze_processes"), 0, true);
354 error
= suspend_freeze_processes();
355 trace_suspend_resume(TPS("freeze_processes"), 0, false);
359 suspend_stats
.failed_freeze
++;
360 dpm_save_failed_step(SUSPEND_FREEZE
);
362 __pm_notifier_call_chain(PM_POST_SUSPEND
, nr_calls
, NULL
);
363 pm_restore_console();
367 /* default implementation */
368 void __weak
arch_suspend_disable_irqs(void)
373 /* default implementation */
374 void __weak
arch_suspend_enable_irqs(void)
380 * suspend_enter - Make the system enter the given sleep state.
381 * @state: System sleep state to enter.
382 * @wakeup: Returns information that the sleep state should not be re-entered.
384 * This function should be called after devices have been suspended.
386 static int suspend_enter(suspend_state_t state
, bool *wakeup
)
390 error
= platform_suspend_prepare(state
);
392 goto Platform_finish
;
394 error
= dpm_suspend_late(PMSG_SUSPEND
);
396 pr_err("late suspend of devices failed\n");
397 goto Platform_finish
;
399 error
= platform_suspend_prepare_late(state
);
401 goto Devices_early_resume
;
403 if (state
== PM_SUSPEND_TO_IDLE
&& pm_test_level
!= TEST_PLATFORM
) {
405 goto Platform_early_resume
;
408 error
= dpm_suspend_noirq(PMSG_SUSPEND
);
410 pr_err("noirq suspend of devices failed\n");
411 goto Platform_early_resume
;
413 error
= platform_suspend_prepare_noirq(state
);
417 if (suspend_test(TEST_PLATFORM
))
420 error
= disable_nonboot_cpus();
421 if (error
|| suspend_test(TEST_CPUS
))
424 arch_suspend_disable_irqs();
425 BUG_ON(!irqs_disabled());
427 error
= syscore_suspend();
429 *wakeup
= pm_wakeup_pending();
430 if (!(suspend_test(TEST_CORE
) || *wakeup
)) {
431 trace_suspend_resume(TPS("machine_suspend"),
433 error
= suspend_ops
->enter(state
);
434 trace_suspend_resume(TPS("machine_suspend"),
436 events_check_enabled
= false;
437 } else if (*wakeup
) {
443 arch_suspend_enable_irqs();
444 BUG_ON(irqs_disabled());
447 enable_nonboot_cpus();
450 platform_resume_noirq(state
);
451 dpm_resume_noirq(PMSG_RESUME
);
453 Platform_early_resume
:
454 platform_resume_early(state
);
456 Devices_early_resume
:
457 dpm_resume_early(PMSG_RESUME
);
460 platform_resume_finish(state
);
465 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
466 * @state: System sleep state to enter.
468 int suspend_devices_and_enter(suspend_state_t state
)
473 if (!sleep_state_supported(state
))
476 pm_suspend_target_state
= state
;
478 error
= platform_suspend_begin(state
);
483 suspend_test_start();
484 error
= dpm_suspend_start(PMSG_SUSPEND
);
486 pr_err("Some devices failed to suspend, or early wake event detected\n");
487 goto Recover_platform
;
489 suspend_test_finish("suspend devices");
490 if (suspend_test(TEST_DEVICES
))
491 goto Recover_platform
;
494 error
= suspend_enter(state
, &wakeup
);
495 } while (!error
&& !wakeup
&& platform_suspend_again(state
));
498 suspend_test_start();
499 dpm_resume_end(PMSG_RESUME
);
500 suspend_test_finish("resume devices");
501 trace_suspend_resume(TPS("resume_console"), state
, true);
503 trace_suspend_resume(TPS("resume_console"), state
, false);
506 platform_resume_end(state
);
507 pm_suspend_target_state
= PM_SUSPEND_ON
;
511 platform_recover(state
);
516 * suspend_finish - Clean up before finishing the suspend sequence.
518 * Call platform code to clean up, restart processes, and free the console that
519 * we've allocated. This routine is not called for hibernation.
521 static void suspend_finish(void)
523 suspend_thaw_processes();
524 pm_notifier_call_chain(PM_POST_SUSPEND
);
525 pm_restore_console();
529 * enter_state - Do common work needed to enter system sleep state.
530 * @state: System sleep state to enter.
532 * Make sure that no one else is trying to put the system into a sleep state.
533 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
534 * system enter the given sleep state and clean up after wakeup.
536 static int enter_state(suspend_state_t state
)
540 trace_suspend_resume(TPS("suspend_enter"), state
, true);
541 if (state
== PM_SUSPEND_TO_IDLE
) {
542 #ifdef CONFIG_PM_DEBUG
543 if (pm_test_level
!= TEST_NONE
&& pm_test_level
<= TEST_CPUS
) {
544 pr_warn("Unsupported test mode for suspend to idle, please choose none/freezer/devices/platform.\n");
548 } else if (!valid_state(state
)) {
551 if (!mutex_trylock(&pm_mutex
))
554 if (state
== PM_SUSPEND_TO_IDLE
)
557 #ifndef CONFIG_SUSPEND_SKIP_SYNC
558 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
559 pr_info("Syncing filesystems ... ");
562 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
565 pm_pr_dbg("Preparing system for sleep (%s)\n", mem_sleep_labels
[state
]);
566 pm_suspend_clear_flags();
567 error
= suspend_prepare(state
);
571 if (suspend_test(TEST_FREEZER
))
574 trace_suspend_resume(TPS("suspend_enter"), state
, false);
575 pm_pr_dbg("Suspending system (%s)\n", mem_sleep_labels
[state
]);
576 pm_restrict_gfp_mask();
577 error
= suspend_devices_and_enter(state
);
578 pm_restore_gfp_mask();
581 pm_pr_dbg("Finishing wakeup.\n");
584 mutex_unlock(&pm_mutex
);
589 * pm_suspend - Externally visible function for suspending the system.
590 * @state: System sleep state to enter.
592 * Check if the value of @state represents one of the supported states,
593 * execute enter_state() and update system suspend statistics.
595 int pm_suspend(suspend_state_t state
)
599 if (state
<= PM_SUSPEND_ON
|| state
>= PM_SUSPEND_MAX
)
602 pr_info("suspend entry (%s)\n", mem_sleep_labels
[state
]);
603 error
= enter_state(state
);
605 suspend_stats
.fail
++;
606 dpm_save_failed_errno(error
);
608 suspend_stats
.success
++;
610 pr_info("suspend exit\n");
613 EXPORT_SYMBOL(pm_suspend
);