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/suspend.h>
28 const char *const pm_states
[PM_SUSPEND_MAX
] = {
29 [PM_SUSPEND_STANDBY
] = "standby",
30 [PM_SUSPEND_MEM
] = "mem",
33 static struct platform_suspend_ops
*suspend_ops
;
36 * suspend_set_ops - Set the global suspend method table.
37 * @ops: Pointer to ops structure.
39 void suspend_set_ops(struct platform_suspend_ops
*ops
)
41 mutex_lock(&pm_mutex
);
43 mutex_unlock(&pm_mutex
);
46 bool valid_state(suspend_state_t state
)
49 * All states need lowlevel support and need to be valid to the lowlevel
50 * implementation, no valid callback implies that none are valid.
52 return suspend_ops
&& suspend_ops
->valid
&& suspend_ops
->valid(state
);
56 * suspend_valid_only_mem - generic memory-only valid callback
58 * Platform drivers that implement mem suspend only and only need
59 * to check for that in their .valid callback can use this instead
60 * of rolling their own .valid callback.
62 int suspend_valid_only_mem(suspend_state_t state
)
64 return state
== PM_SUSPEND_MEM
;
67 static int suspend_test(int level
)
69 #ifdef CONFIG_PM_DEBUG
70 if (pm_test_level
== level
) {
71 printk(KERN_INFO
"suspend debug: Waiting for 5 seconds.\n");
75 #endif /* !CONFIG_PM_DEBUG */
80 * suspend_prepare - Do prep work before entering low-power state.
82 * This is common code that is called for each state that we're entering.
83 * Run suspend notifiers, allocate a console and stop all processes.
85 static int suspend_prepare(void)
89 if (!suspend_ops
|| !suspend_ops
->enter
)
94 error
= pm_notifier_call_chain(PM_SUSPEND_PREPARE
);
98 error
= usermodehelper_disable();
102 error
= suspend_freeze_processes();
106 suspend_thaw_processes();
107 usermodehelper_enable();
109 pm_notifier_call_chain(PM_POST_SUSPEND
);
110 pm_restore_console();
114 /* default implementation */
115 void __attribute__ ((weak
)) arch_suspend_disable_irqs(void)
120 /* default implementation */
121 void __attribute__ ((weak
)) arch_suspend_enable_irqs(void)
127 * suspend_enter - enter the desired system sleep state.
128 * @state: state to enter
130 * This function should be called after devices have been suspended.
132 static int suspend_enter(suspend_state_t state
)
136 if (suspend_ops
->prepare
) {
137 error
= suspend_ops
->prepare();
139 goto Platform_finish
;
142 error
= dpm_suspend_noirq(PMSG_SUSPEND
);
144 printk(KERN_ERR
"PM: Some devices failed to power down\n");
145 goto Platform_finish
;
148 if (suspend_ops
->prepare_late
) {
149 error
= suspend_ops
->prepare_late();
154 if (suspend_test(TEST_PLATFORM
))
157 error
= disable_nonboot_cpus();
158 if (error
|| suspend_test(TEST_CPUS
))
161 arch_suspend_disable_irqs();
162 BUG_ON(!irqs_disabled());
164 error
= sysdev_suspend(PMSG_SUSPEND
);
166 if (!suspend_test(TEST_CORE
) && pm_check_wakeup_events()) {
167 error
= suspend_ops
->enter(state
);
168 events_check_enabled
= false;
173 arch_suspend_enable_irqs();
174 BUG_ON(irqs_disabled());
177 enable_nonboot_cpus();
180 if (suspend_ops
->wake
)
183 dpm_resume_noirq(PMSG_RESUME
);
186 if (suspend_ops
->finish
)
187 suspend_ops
->finish();
193 * suspend_devices_and_enter - suspend devices and enter the desired system
195 * @state: state to enter
197 int suspend_devices_and_enter(suspend_state_t state
)
205 if (suspend_ops
->begin
) {
206 error
= suspend_ops
->begin(state
);
211 saved_mask
= clear_gfp_allowed_mask(GFP_IOFS
);
212 suspend_test_start();
213 error
= dpm_suspend_start(PMSG_SUSPEND
);
215 printk(KERN_ERR
"PM: Some devices failed to suspend\n");
216 goto Recover_platform
;
218 suspend_test_finish("suspend devices");
219 if (suspend_test(TEST_DEVICES
))
220 goto Recover_platform
;
222 suspend_enter(state
);
225 suspend_test_start();
226 dpm_resume_end(PMSG_RESUME
);
227 suspend_test_finish("resume devices");
228 set_gfp_allowed_mask(saved_mask
);
231 if (suspend_ops
->end
)
236 if (suspend_ops
->recover
)
237 suspend_ops
->recover();
242 * suspend_finish - Do final work before exiting suspend sequence.
244 * Call platform code to clean up, restart processes, and free the
245 * console that we've allocated. This is not called for suspend-to-disk.
247 static void suspend_finish(void)
249 suspend_thaw_processes();
250 usermodehelper_enable();
251 pm_notifier_call_chain(PM_POST_SUSPEND
);
252 pm_restore_console();
256 * enter_state - Do common work of entering low-power state.
257 * @state: pm_state structure for state we're entering.
259 * Make sure we're the only ones trying to enter a sleep state. Fail
260 * if someone has beat us to it, since we don't want anything weird to
261 * happen when we wake up.
262 * Then, do the setup for suspend, enter the state, and cleaup (after
265 int enter_state(suspend_state_t state
)
269 if (!valid_state(state
))
272 if (!mutex_trylock(&pm_mutex
))
275 printk(KERN_INFO
"PM: Syncing filesystems ... ");
279 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
280 error
= suspend_prepare();
284 if (suspend_test(TEST_FREEZER
))
287 pr_debug("PM: Entering %s sleep\n", pm_states
[state
]);
288 error
= suspend_devices_and_enter(state
);
291 pr_debug("PM: Finishing wakeup.\n");
294 mutex_unlock(&pm_mutex
);
299 * pm_suspend - Externally visible function for suspending system.
300 * @state: Enumerated value of state to enter.
302 * Determine whether or not value is within range, get state
303 * structure, and enter (above).
305 int pm_suspend(suspend_state_t state
)
307 if (state
> PM_SUSPEND_ON
&& state
<= PM_SUSPEND_MAX
)
308 return enter_state(state
);
311 EXPORT_SYMBOL(pm_suspend
);