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 #ifdef CONFIG_EARLYSUSPEND
23 [PM_SUSPEND_ON
] = "on",
25 [PM_SUSPEND_STANDBY
] = "standby",
26 [PM_SUSPEND_MEM
] = "mem",
29 static struct platform_suspend_ops
*suspend_ops
;
32 * suspend_set_ops - Set the global suspend method table.
33 * @ops: Pointer to ops structure.
35 void suspend_set_ops(struct platform_suspend_ops
*ops
)
37 mutex_lock(&pm_mutex
);
39 mutex_unlock(&pm_mutex
);
42 bool valid_state(suspend_state_t state
)
45 * All states need lowlevel support and need to be valid to the lowlevel
46 * implementation, no valid callback implies that none are valid.
48 return suspend_ops
&& suspend_ops
->valid
&& suspend_ops
->valid(state
);
52 * suspend_valid_only_mem - generic memory-only valid callback
54 * Platform drivers that implement mem suspend only and only need
55 * to check for that in their .valid callback can use this instead
56 * of rolling their own .valid callback.
58 int suspend_valid_only_mem(suspend_state_t state
)
60 return state
== PM_SUSPEND_MEM
;
63 static int suspend_test(int level
)
65 #ifdef CONFIG_PM_DEBUG
66 if (pm_test_level
== level
) {
67 printk(KERN_INFO
"suspend debug: Waiting for 5 seconds.\n");
71 #endif /* !CONFIG_PM_DEBUG */
76 * suspend_prepare - Do prep work before entering low-power state.
78 * This is common code that is called for each state that we're entering.
79 * Run suspend notifiers, allocate a console and stop all processes.
81 static int suspend_prepare(void)
85 if (!suspend_ops
|| !suspend_ops
->enter
)
90 error
= pm_notifier_call_chain(PM_SUSPEND_PREPARE
);
94 error
= usermodehelper_disable();
98 error
= suspend_freeze_processes();
102 suspend_thaw_processes();
103 usermodehelper_enable();
105 pm_notifier_call_chain(PM_POST_SUSPEND
);
106 pm_restore_console();
110 /* default implementation */
111 void __attribute__ ((weak
)) arch_suspend_disable_irqs(void)
116 /* default implementation */
117 void __attribute__ ((weak
)) arch_suspend_enable_irqs(void)
123 * suspend_enter - enter the desired system sleep state.
124 * @state: state to enter
126 * This function should be called after devices have been suspended.
128 static int suspend_enter(suspend_state_t state
)
132 if (suspend_ops
->prepare
) {
133 error
= suspend_ops
->prepare();
138 error
= dpm_suspend_noirq(PMSG_SUSPEND
);
140 printk(KERN_ERR
"PM: Some devices failed to power down\n");
141 goto Platfrom_finish
;
144 if (suspend_ops
->prepare_late
) {
145 error
= suspend_ops
->prepare_late();
147 goto Power_up_devices
;
150 if (suspend_test(TEST_PLATFORM
))
153 error
= disable_nonboot_cpus();
154 if (error
|| suspend_test(TEST_CPUS
))
157 arch_suspend_disable_irqs();
158 BUG_ON(!irqs_disabled());
160 error
= sysdev_suspend(PMSG_SUSPEND
);
162 if (!suspend_test(TEST_CORE
))
163 error
= suspend_ops
->enter(state
);
167 arch_suspend_enable_irqs();
168 BUG_ON(irqs_disabled());
171 enable_nonboot_cpus();
174 if (suspend_ops
->wake
)
178 dpm_resume_noirq(PMSG_RESUME
);
181 if (suspend_ops
->finish
)
182 suspend_ops
->finish();
188 * suspend_devices_and_enter - suspend devices and enter the desired system
190 * @state: state to enter
192 int suspend_devices_and_enter(suspend_state_t state
)
199 if (suspend_ops
->begin
) {
200 error
= suspend_ops
->begin(state
);
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");
223 if (suspend_ops
->end
)
228 if (suspend_ops
->recover
)
229 suspend_ops
->recover();
234 * suspend_finish - Do final work before exiting suspend sequence.
236 * Call platform code to clean up, restart processes, and free the
237 * console that we've allocated. This is not called for suspend-to-disk.
239 static void suspend_finish(void)
241 suspend_thaw_processes();
242 usermodehelper_enable();
243 pm_notifier_call_chain(PM_POST_SUSPEND
);
244 pm_restore_console();
248 * enter_state - Do common work of entering low-power state.
249 * @state: pm_state structure for state we're entering.
251 * Make sure we're the only ones trying to enter a sleep state. Fail
252 * if someone has beat us to it, since we don't want anything weird to
253 * happen when we wake up.
254 * Then, do the setup for suspend, enter the state, and cleaup (after
257 int enter_state(suspend_state_t state
)
261 if (!valid_state(state
))
264 if (!mutex_trylock(&pm_mutex
))
267 printk(KERN_INFO
"PM: Syncing filesystems ... ");
271 pr_debug("PM: Preparing system for %s sleep\n", pm_states
[state
]);
272 error
= suspend_prepare();
276 if (suspend_test(TEST_FREEZER
))
279 pr_debug("PM: Entering %s sleep\n", pm_states
[state
]);
280 error
= suspend_devices_and_enter(state
);
283 pr_debug("PM: Finishing wakeup.\n");
286 mutex_unlock(&pm_mutex
);
291 * pm_suspend - Externally visible function for suspending system.
292 * @state: Enumerated value of state to enter.
294 * Determine whether or not value is within range, get state
295 * structure, and enter (above).
297 int pm_suspend(suspend_state_t state
)
299 if (state
> PM_SUSPEND_ON
&& state
<= PM_SUSPEND_MAX
)
300 return enter_state(state
);
303 EXPORT_SYMBOL(pm_suspend
);