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>
21 const char *const pm_states[PM_SUSPEND_MAX] = {
22 [PM_SUSPEND_STANDBY] = "standby",
23 [PM_SUSPEND_MEM] = "mem",
26 static struct platform_suspend_ops *suspend_ops;
29 * suspend_set_ops - Set the global suspend method table.
30 * @ops: Pointer to ops structure.
32 void suspend_set_ops(struct platform_suspend_ops *ops)
34 mutex_lock(&pm_mutex);
36 mutex_unlock(&pm_mutex);
39 bool valid_state(suspend_state_t state)
42 * All states need lowlevel support and need to be valid to the lowlevel
43 * implementation, no valid callback implies that none are valid.
45 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
49 * suspend_valid_only_mem - generic memory-only valid callback
51 * Platform drivers that implement mem suspend only and only need
52 * to check for that in their .valid callback can use this instead
53 * of rolling their own .valid callback.
55 int suspend_valid_only_mem(suspend_state_t state)
57 return state == PM_SUSPEND_MEM;
60 static int suspend_test(int level)
62 #ifdef CONFIG_PM_DEBUG
63 if (pm_test_level == level) {
64 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
68 #endif /* !CONFIG_PM_DEBUG */
73 * suspend_prepare - Do prep work before entering low-power state.
75 * This is common code that is called for each state that we're entering.
76 * Run suspend notifiers, allocate a console and stop all processes.
78 static int suspend_prepare(void)
82 if (!suspend_ops || !suspend_ops->enter)
87 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
91 error = usermodehelper_disable();
95 error = suspend_freeze_processes();
99 suspend_thaw_processes();
100 usermodehelper_enable();
102 pm_notifier_call_chain(PM_POST_SUSPEND);
103 pm_restore_console();
107 /* default implementation */
108 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
113 /* default implementation */
114 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
120 * suspend_enter - enter the desired system sleep state.
121 * @state: state to enter
123 * This function should be called after devices have been suspended.
125 static int suspend_enter(suspend_state_t state)
129 if (suspend_ops->prepare) {
130 error = suspend_ops->prepare();
135 error = dpm_suspend_noirq(PMSG_SUSPEND);
137 printk(KERN_ERR "PM: Some devices failed to power down\n");
138 goto Platfrom_finish;
141 if (suspend_ops->prepare_late) {
142 error = suspend_ops->prepare_late();
144 goto Power_up_devices;
147 if (suspend_test(TEST_PLATFORM))
150 error = disable_nonboot_cpus();
151 if (error || suspend_test(TEST_CPUS))
154 arch_suspend_disable_irqs();
155 BUG_ON(!irqs_disabled());
157 error = sysdev_suspend(PMSG_SUSPEND);
159 if (!suspend_test(TEST_CORE))
160 error = suspend_ops->enter(state);
164 arch_suspend_enable_irqs();
165 BUG_ON(irqs_disabled());
168 enable_nonboot_cpus();
171 if (suspend_ops->wake)
175 dpm_resume_noirq(PMSG_RESUME);
178 if (suspend_ops->finish)
179 suspend_ops->finish();
185 * suspend_devices_and_enter - suspend devices and enter the desired system
187 * @state: state to enter
189 int suspend_devices_and_enter(suspend_state_t state)
196 if (suspend_ops->begin) {
197 error = suspend_ops->begin(state);
202 suspend_test_start();
203 error = dpm_suspend_start(PMSG_SUSPEND);
205 printk(KERN_ERR "PM: Some devices failed to suspend\n");
206 goto Recover_platform;
208 suspend_test_finish("suspend devices");
209 if (suspend_test(TEST_DEVICES))
210 goto Recover_platform;
212 suspend_enter(state);
215 suspend_test_start();
216 dpm_resume_end(PMSG_RESUME);
217 suspend_test_finish("resume devices");
220 if (suspend_ops->end)
225 if (suspend_ops->recover)
226 suspend_ops->recover();
231 * suspend_finish - Do final work before exiting suspend sequence.
233 * Call platform code to clean up, restart processes, and free the
234 * console that we've allocated. This is not called for suspend-to-disk.
236 static void suspend_finish(void)
238 suspend_thaw_processes();
239 usermodehelper_enable();
240 pm_notifier_call_chain(PM_POST_SUSPEND);
241 pm_restore_console();
245 * enter_state - Do common work of entering low-power state.
246 * @state: pm_state structure for state we're entering.
248 * Make sure we're the only ones trying to enter a sleep state. Fail
249 * if someone has beat us to it, since we don't want anything weird to
250 * happen when we wake up.
251 * Then, do the setup for suspend, enter the state, and cleaup (after
254 int enter_state(suspend_state_t state)
258 if (!valid_state(state))
261 if (!mutex_trylock(&pm_mutex))
264 printk(KERN_INFO "PM: Syncing filesystems ... ");
268 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
269 error = suspend_prepare();
273 if (suspend_test(TEST_FREEZER))
276 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
277 error = suspend_devices_and_enter(state);
280 pr_debug("PM: Finishing wakeup.\n");
283 mutex_unlock(&pm_mutex);
288 * pm_suspend - Externally visible function for suspending system.
289 * @state: Enumerated value of state to enter.
291 * Determine whether or not value is within range, get state
292 * structure, and enter (above).
294 int pm_suspend(suspend_state_t state)
296 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
297 return enter_state(state);
300 EXPORT_SYMBOL(pm_suspend);