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 *pm_labels
[] = { "mem", "standby", "freeze", NULL
};
35 const char *pm_states
[PM_SUSPEND_MAX
];
37 static const struct platform_suspend_ops
*suspend_ops
;
38 static const struct platform_freeze_ops
*freeze_ops
;
39 static DECLARE_WAIT_QUEUE_HEAD(suspend_freeze_wait_head
);
40 static bool suspend_freeze_wake
;
42 void freeze_set_ops(const struct platform_freeze_ops
*ops
)
46 unlock_system_sleep();
49 static void freeze_begin(void)
51 suspend_freeze_wake
= false;
54 static void freeze_enter(void)
56 cpuidle_use_deepest_state(true);
58 wait_event(suspend_freeze_wait_head
, suspend_freeze_wake
);
60 cpuidle_use_deepest_state(false);
63 void freeze_wake(void)
65 suspend_freeze_wake
= true;
66 wake_up(&suspend_freeze_wait_head
);
68 EXPORT_SYMBOL_GPL(freeze_wake
);
70 static bool valid_state(suspend_state_t state
)
73 * PM_SUSPEND_STANDBY and PM_SUSPEND_MEM states need low level
74 * support and need to be valid to the low level
75 * implementation, no valid callback implies that none are valid.
77 return suspend_ops
&& suspend_ops
->valid
&& suspend_ops
->valid(state
);
81 * If this is set, the "mem" label always corresponds to the deepest sleep state
82 * available, the "standby" label corresponds to the second deepest sleep state
83 * available (if any), and the "freeze" label corresponds to the remaining
84 * available sleep state (if there is one).
86 static bool relative_states
;
88 static int __init
sleep_states_setup(char *str
)
90 relative_states
= !strncmp(str
, "1", 1);
91 pm_states
[PM_SUSPEND_FREEZE
] = pm_labels
[relative_states
? 0 : 2];
95 __setup("relative_sleep_states=", sleep_states_setup
);
98 * suspend_set_ops - Set the global suspend method table.
99 * @ops: Suspend operations to use.
101 void suspend_set_ops(const struct platform_suspend_ops
*ops
)
109 for (i
= PM_SUSPEND_MEM
; i
>= PM_SUSPEND_STANDBY
; i
--)
110 if (valid_state(i
)) {
111 pm_states
[i
] = pm_labels
[j
++];
112 } else if (!relative_states
) {
117 pm_states
[PM_SUSPEND_FREEZE
] = pm_labels
[j
];
119 unlock_system_sleep();
121 EXPORT_SYMBOL_GPL(suspend_set_ops
);
124 * suspend_valid_only_mem - Generic memory-only valid callback.
126 * Platform drivers that implement mem suspend only and only need to check for
127 * that in their .valid() callback can use this instead of rolling their own
130 int suspend_valid_only_mem(suspend_state_t state
)
132 return state
== PM_SUSPEND_MEM
;
134 EXPORT_SYMBOL_GPL(suspend_valid_only_mem
);
136 static bool sleep_state_supported(suspend_state_t state
)
138 return state
== PM_SUSPEND_FREEZE
|| (suspend_ops
&& suspend_ops
->enter
);
141 static int platform_suspend_prepare(suspend_state_t state
)
143 return state
!= PM_SUSPEND_FREEZE
&& suspend_ops
->prepare
?
144 suspend_ops
->prepare() : 0;
147 static int platform_suspend_prepare_late(suspend_state_t state
)
149 return state
== PM_SUSPEND_FREEZE
&& freeze_ops
->prepare
?
150 freeze_ops
->prepare() : 0;
153 static int platform_suspend_prepare_noirq(suspend_state_t state
)
155 return state
!= PM_SUSPEND_FREEZE
&& suspend_ops
->prepare_late
?
156 suspend_ops
->prepare_late() : 0;
159 static void platform_resume_noirq(suspend_state_t state
)
161 if (state
!= PM_SUSPEND_FREEZE
&& suspend_ops
->wake
)
165 static void platform_resume_early(suspend_state_t state
)
167 if (state
== PM_SUSPEND_FREEZE
&& freeze_ops
->restore
)
168 freeze_ops
->restore();
171 static void platform_resume_finish(suspend_state_t state
)
173 if (state
!= PM_SUSPEND_FREEZE
&& suspend_ops
->finish
)
174 suspend_ops
->finish();
177 static int platform_suspend_begin(suspend_state_t state
)
179 if (state
== PM_SUSPEND_FREEZE
&& freeze_ops
&& freeze_ops
->begin
)
180 return freeze_ops
->begin();
181 else if (suspend_ops
->begin
)
182 return suspend_ops
->begin(state
);
187 static void platform_resume_end(suspend_state_t state
)
189 if (state
== PM_SUSPEND_FREEZE
&& freeze_ops
&& freeze_ops
->end
)
191 else if (suspend_ops
->end
)
195 static void platform_recover(suspend_state_t state
)
197 if (state
!= PM_SUSPEND_FREEZE
&& suspend_ops
->recover
)
198 suspend_ops
->recover();
201 static bool platform_suspend_again(suspend_state_t state
)
203 return state
!= PM_SUSPEND_FREEZE
&& suspend_ops
->suspend_again
?
204 suspend_ops
->suspend_again() : false;
207 static int suspend_test(int level
)
209 #ifdef CONFIG_PM_DEBUG
210 if (pm_test_level
== level
) {
211 printk(KERN_INFO
"suspend debug: Waiting for 5 seconds.\n");
215 #endif /* !CONFIG_PM_DEBUG */
220 * suspend_prepare - Prepare for entering system sleep state.
222 * Common code run for every system sleep state that can be entered (except for
223 * hibernation). Run suspend notifiers, allocate the "suspend" console and
226 static int suspend_prepare(suspend_state_t state
)
230 if (!sleep_state_supported(state
))
233 pm_prepare_console();
235 error
= pm_notifier_call_chain(PM_SUSPEND_PREPARE
);
239 trace_suspend_resume(TPS("freeze_processes"), 0, true);
240 error
= suspend_freeze_processes();
241 trace_suspend_resume(TPS("freeze_processes"), 0, false);
245 suspend_stats
.failed_freeze
++;
246 dpm_save_failed_step(SUSPEND_FREEZE
);
248 pm_notifier_call_chain(PM_POST_SUSPEND
);
249 pm_restore_console();
253 /* default implementation */
254 void __weak
arch_suspend_disable_irqs(void)
259 /* default implementation */
260 void __weak
arch_suspend_enable_irqs(void)
266 * suspend_enter - Make the system enter the given sleep state.
267 * @state: System sleep state to enter.
268 * @wakeup: Returns information that the sleep state should not be re-entered.
270 * This function should be called after devices have been suspended.
272 static int suspend_enter(suspend_state_t state
, bool *wakeup
)
276 error
= platform_suspend_prepare(state
);
278 goto Platform_finish
;
280 error
= dpm_suspend_late(PMSG_SUSPEND
);
282 printk(KERN_ERR
"PM: late suspend of devices failed\n");
283 goto Platform_finish
;
285 error
= platform_suspend_prepare_late(state
);
287 goto Devices_early_resume
;
289 error
= dpm_suspend_noirq(PMSG_SUSPEND
);
291 printk(KERN_ERR
"PM: noirq suspend of devices failed\n");
292 goto Platform_early_resume
;
294 error
= platform_suspend_prepare_noirq(state
);
298 if (suspend_test(TEST_PLATFORM
))
302 * PM_SUSPEND_FREEZE equals
303 * frozen processes + suspended devices + idle processors.
304 * Thus we should invoke freeze_enter() soon after
305 * all the devices are suspended.
307 if (state
== PM_SUSPEND_FREEZE
) {
308 trace_suspend_resume(TPS("machine_suspend"), state
, true);
310 trace_suspend_resume(TPS("machine_suspend"), state
, false);
314 error
= disable_nonboot_cpus();
315 if (error
|| suspend_test(TEST_CPUS
))
318 arch_suspend_disable_irqs();
319 BUG_ON(!irqs_disabled());
321 error
= syscore_suspend();
323 *wakeup
= pm_wakeup_pending();
324 if (!(suspend_test(TEST_CORE
) || *wakeup
)) {
325 trace_suspend_resume(TPS("machine_suspend"),
327 error
= suspend_ops
->enter(state
);
328 trace_suspend_resume(TPS("machine_suspend"),
330 events_check_enabled
= false;
335 arch_suspend_enable_irqs();
336 BUG_ON(irqs_disabled());
339 enable_nonboot_cpus();
342 platform_resume_noirq(state
);
343 dpm_resume_noirq(PMSG_RESUME
);
345 Platform_early_resume
:
346 platform_resume_early(state
);
348 Devices_early_resume
:
349 dpm_resume_early(PMSG_RESUME
);
352 platform_resume_finish(state
);
357 * suspend_devices_and_enter - Suspend devices and enter system sleep state.
358 * @state: System sleep state to enter.
360 int suspend_devices_and_enter(suspend_state_t state
)
365 if (!sleep_state_supported(state
))
368 error
= platform_suspend_begin(state
);
373 suspend_test_start();
374 error
= dpm_suspend_start(PMSG_SUSPEND
);
376 pr_err("PM: Some devices failed to suspend, or early wake event detected\n");
377 goto Recover_platform
;
379 suspend_test_finish("suspend devices");
380 if (suspend_test(TEST_DEVICES
))
381 goto Recover_platform
;
384 error
= suspend_enter(state
, &wakeup
);
385 } while (!error
&& !wakeup
&& platform_suspend_again(state
));
388 suspend_test_start();
389 dpm_resume_end(PMSG_RESUME
);
390 suspend_test_finish("resume devices");
391 trace_suspend_resume(TPS("resume_console"), state
, true);
393 trace_suspend_resume(TPS("resume_console"), state
, false);
396 platform_resume_end(state
);
400 platform_recover(state
);
405 * suspend_finish - Clean up before finishing the suspend sequence.
407 * Call platform code to clean up, restart processes, and free the console that
408 * we've allocated. This routine is not called for hibernation.
410 static void suspend_finish(void)
412 suspend_thaw_processes();
413 pm_notifier_call_chain(PM_POST_SUSPEND
);
414 pm_restore_console();
418 * enter_state - Do common work needed to enter system sleep state.
419 * @state: System sleep state to enter.
421 * Make sure that no one else is trying to put the system into a sleep state.
422 * Fail if that's not the case. Otherwise, prepare for system suspend, make the
423 * system enter the given sleep state and clean up after wakeup.
425 static int enter_state(suspend_state_t state
)
429 trace_suspend_resume(TPS("suspend_enter"), state
, true);
430 if (state
== PM_SUSPEND_FREEZE
) {
431 #ifdef CONFIG_PM_DEBUG
432 if (pm_test_level
!= TEST_NONE
&& pm_test_level
<= TEST_CPUS
) {
433 pr_warning("PM: Unsupported test mode for freeze state,"
434 "please choose none/freezer/devices/platform.\n");
438 } else if (!valid_state(state
)) {
441 if (!mutex_trylock(&pm_mutex
))
444 if (state
== PM_SUSPEND_FREEZE
)
447 trace_suspend_resume(TPS("sync_filesystems"), 0, true);
448 printk(KERN_INFO
"PM: Syncing filesystems ... ");
451 trace_suspend_resume(TPS("sync_filesystems"), 0, false);
453 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
454 error
= suspend_prepare(state
);
458 if (suspend_test(TEST_FREEZER
))
461 trace_suspend_resume(TPS("suspend_enter"), state
, false);
462 pr_debug("PM: Entering %s sleep\n", pm_states
[state
]);
463 pm_restrict_gfp_mask();
464 error
= suspend_devices_and_enter(state
);
465 pm_restore_gfp_mask();
468 pr_debug("PM: Finishing wakeup.\n");
471 mutex_unlock(&pm_mutex
);
476 * pm_suspend - Externally visible function for suspending the system.
477 * @state: System sleep state to enter.
479 * Check if the value of @state represents one of the supported states,
480 * execute enter_state() and update system suspend statistics.
482 int pm_suspend(suspend_state_t state
)
486 if (state
<= PM_SUSPEND_ON
|| state
>= PM_SUSPEND_MAX
)
489 error
= enter_state(state
);
491 suspend_stats
.fail
++;
492 dpm_save_failed_errno(error
);
494 suspend_stats
.success
++;
498 EXPORT_SYMBOL(pm_suspend
);