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>
22 const char *const pm_states
[PM_SUSPEND_MAX
] = {
23 [PM_SUSPEND_STANDBY
] = "standby",
24 [PM_SUSPEND_MEM
] = "mem",
27 static struct platform_suspend_ops
*suspend_ops
;
30 * suspend_set_ops - Set the global suspend method table.
31 * @ops: Pointer to ops structure.
33 void suspend_set_ops(struct platform_suspend_ops
*ops
)
35 mutex_lock(&pm_mutex
);
37 mutex_unlock(&pm_mutex
);
40 bool valid_state(suspend_state_t state
)
43 * All states need lowlevel support and need to be valid to the lowlevel
44 * implementation, no valid callback implies that none are valid.
46 return suspend_ops
&& suspend_ops
->valid
&& suspend_ops
->valid(state
);
50 * suspend_valid_only_mem - generic memory-only valid callback
52 * Platform drivers that implement mem suspend only and only need
53 * to check for that in their .valid callback can use this instead
54 * of rolling their own .valid callback.
56 int suspend_valid_only_mem(suspend_state_t state
)
58 return state
== PM_SUSPEND_MEM
;
61 static int suspend_test(int level
)
63 #ifdef CONFIG_PM_DEBUG
64 if (pm_test_level
== level
) {
65 printk(KERN_INFO
"suspend debug: Waiting for 5 seconds.\n");
69 #endif /* !CONFIG_PM_DEBUG */
74 * suspend_prepare - Do prep work before entering low-power state.
76 * This is common code that is called for each state that we're entering.
77 * Run suspend notifiers, allocate a console and stop all processes.
79 static int suspend_prepare(void)
83 if (!suspend_ops
|| !suspend_ops
->enter
)
88 error
= pm_notifier_call_chain(PM_SUSPEND_PREPARE
);
92 error
= usermodehelper_disable();
96 error
= suspend_freeze_processes();
100 suspend_thaw_processes();
101 usermodehelper_enable();
103 pm_notifier_call_chain(PM_POST_SUSPEND
);
104 pm_restore_console();
108 /* default implementation */
109 void __attribute__ ((weak
)) arch_suspend_disable_irqs(void)
114 /* default implementation */
115 void __attribute__ ((weak
)) arch_suspend_enable_irqs(void)
121 * suspend_enter - enter the desired system sleep state.
122 * @state: state to enter
124 * This function should be called after devices have been suspended.
126 static int suspend_enter(suspend_state_t state
)
130 if (suspend_ops
->prepare
) {
131 error
= suspend_ops
->prepare();
136 error
= dpm_suspend_noirq(PMSG_SUSPEND
);
138 printk(KERN_ERR
"PM: Some devices failed to power down\n");
139 goto Platfrom_finish
;
142 if (suspend_ops
->prepare_late
) {
143 error
= suspend_ops
->prepare_late();
145 goto Power_up_devices
;
148 if (suspend_test(TEST_PLATFORM
))
151 error
= disable_nonboot_cpus();
152 if (error
|| suspend_test(TEST_CPUS
))
155 arch_suspend_disable_irqs();
156 BUG_ON(!irqs_disabled());
158 error
= sysdev_suspend(PMSG_SUSPEND
);
160 if (!suspend_test(TEST_CORE
))
161 error
= suspend_ops
->enter(state
);
165 arch_suspend_enable_irqs();
166 BUG_ON(irqs_disabled());
169 enable_nonboot_cpus();
172 if (suspend_ops
->wake
)
176 dpm_resume_noirq(PMSG_RESUME
);
179 if (suspend_ops
->finish
)
180 suspend_ops
->finish();
186 * suspend_devices_and_enter - suspend devices and enter the desired system
188 * @state: state to enter
190 int suspend_devices_and_enter(suspend_state_t state
)
198 if (suspend_ops
->begin
) {
199 error
= suspend_ops
->begin(state
);
204 saved_mask
= clear_gfp_allowed_mask(GFP_IOFS
);
205 suspend_test_start();
206 error
= dpm_suspend_start(PMSG_SUSPEND
);
208 printk(KERN_ERR
"PM: Some devices failed to suspend\n");
209 goto Recover_platform
;
211 suspend_test_finish("suspend devices");
212 if (suspend_test(TEST_DEVICES
))
213 goto Recover_platform
;
215 suspend_enter(state
);
218 suspend_test_start();
219 dpm_resume_end(PMSG_RESUME
);
220 suspend_test_finish("resume devices");
221 set_gfp_allowed_mask(saved_mask
);
224 if (suspend_ops
->end
)
229 if (suspend_ops
->recover
)
230 suspend_ops
->recover();
235 * suspend_finish - Do final work before exiting suspend sequence.
237 * Call platform code to clean up, restart processes, and free the
238 * console that we've allocated. This is not called for suspend-to-disk.
240 static void suspend_finish(void)
242 suspend_thaw_processes();
243 usermodehelper_enable();
244 pm_notifier_call_chain(PM_POST_SUSPEND
);
245 pm_restore_console();
249 * enter_state - Do common work of entering low-power state.
250 * @state: pm_state structure for state we're entering.
252 * Make sure we're the only ones trying to enter a sleep state. Fail
253 * if someone has beat us to it, since we don't want anything weird to
254 * happen when we wake up.
255 * Then, do the setup for suspend, enter the state, and cleaup (after
258 int enter_state(suspend_state_t state
)
262 if (!valid_state(state
))
265 if (!mutex_trylock(&pm_mutex
))
268 printk(KERN_INFO
"PM: Syncing filesystems ... ");
272 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
273 error
= suspend_prepare();
277 if (suspend_test(TEST_FREEZER
))
280 pr_debug("PM: Entering %s sleep\n", pm_states
[state
]);
281 error
= suspend_devices_and_enter(state
);
284 pr_debug("PM: Finishing wakeup.\n");
287 mutex_unlock(&pm_mutex
);
292 * pm_suspend - Externally visible function for suspending system.
293 * @state: Enumerated value of state to enter.
295 * Determine whether or not value is within range, get state
296 * structure, and enter (above).
298 int pm_suspend(suspend_state_t state
)
300 if (state
> PM_SUSPEND_ON
&& state
<= PM_SUSPEND_MAX
)
301 return enter_state(state
);
304 EXPORT_SYMBOL(pm_suspend
);