ACPI Thinkpad: We must always call va_end() after va_start() but do not do so in...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / power / suspend.c
blobb0f28dddc04e8e6a41c93d517073d5fa42d7d510
1 /*
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.
9 */
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>
19 #include <linux/io.h>
20 #include <linux/kernel.h>
21 #include <linux/list.h>
22 #include <linux/mm.h>
23 #include <linux/slab.h>
24 #include <linux/suspend.h>
26 #include "power.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;
35 /**
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);
42 suspend_ops = ops;
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);
55 /**
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");
72 mdelay(5000);
73 return 1;
75 #endif /* !CONFIG_PM_DEBUG */
76 return 0;
79 /**
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)
87 int error;
89 if (!suspend_ops || !suspend_ops->enter)
90 return -EPERM;
92 pm_prepare_console();
94 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
95 if (error)
96 goto Finish;
98 error = usermodehelper_disable();
99 if (error)
100 goto Finish;
102 error = suspend_freeze_processes();
103 if (!error)
104 return 0;
106 suspend_thaw_processes();
107 usermodehelper_enable();
108 Finish:
109 pm_notifier_call_chain(PM_POST_SUSPEND);
110 pm_restore_console();
111 return error;
114 /* default implementation */
115 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
117 local_irq_disable();
120 /* default implementation */
121 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
123 local_irq_enable();
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)
134 int error;
136 if (suspend_ops->prepare) {
137 error = suspend_ops->prepare();
138 if (error)
139 return error;
142 error = dpm_suspend_noirq(PMSG_SUSPEND);
143 if (error) {
144 printk(KERN_ERR "PM: Some devices failed to power down\n");
145 goto Platfrom_finish;
148 if (suspend_ops->prepare_late) {
149 error = suspend_ops->prepare_late();
150 if (error)
151 goto Power_up_devices;
154 if (suspend_test(TEST_PLATFORM))
155 goto Platform_wake;
157 error = disable_nonboot_cpus();
158 if (error || suspend_test(TEST_CPUS))
159 goto Enable_cpus;
161 arch_suspend_disable_irqs();
162 BUG_ON(!irqs_disabled());
164 error = sysdev_suspend(PMSG_SUSPEND);
165 if (!error) {
166 if (!suspend_test(TEST_CORE))
167 error = suspend_ops->enter(state);
168 sysdev_resume();
171 arch_suspend_enable_irqs();
172 BUG_ON(irqs_disabled());
174 Enable_cpus:
175 enable_nonboot_cpus();
177 Platform_wake:
178 if (suspend_ops->wake)
179 suspend_ops->wake();
181 Power_up_devices:
182 dpm_resume_noirq(PMSG_RESUME);
184 Platfrom_finish:
185 if (suspend_ops->finish)
186 suspend_ops->finish();
188 return error;
192 * suspend_devices_and_enter - suspend devices and enter the desired system
193 * sleep state.
194 * @state: state to enter
196 int suspend_devices_and_enter(suspend_state_t state)
198 int error;
200 if (!suspend_ops)
201 return -ENOSYS;
203 if (suspend_ops->begin) {
204 error = suspend_ops->begin(state);
205 if (error)
206 goto Close;
208 suspend_console();
209 pm_restrict_gfp_mask();
210 suspend_test_start();
211 error = dpm_suspend_start(PMSG_SUSPEND);
212 if (error) {
213 printk(KERN_ERR "PM: Some devices failed to suspend\n");
214 goto Recover_platform;
216 suspend_test_finish("suspend devices");
217 if (suspend_test(TEST_DEVICES))
218 goto Recover_platform;
220 suspend_enter(state);
222 Resume_devices:
223 suspend_test_start();
224 dpm_resume_end(PMSG_RESUME);
225 suspend_test_finish("resume devices");
226 pm_restore_gfp_mask();
227 resume_console();
228 Close:
229 if (suspend_ops->end)
230 suspend_ops->end();
231 return error;
233 Recover_platform:
234 if (suspend_ops->recover)
235 suspend_ops->recover();
236 goto Resume_devices;
240 * suspend_finish - Do final work before exiting suspend sequence.
242 * Call platform code to clean up, restart processes, and free the
243 * console that we've allocated. This is not called for suspend-to-disk.
245 static void suspend_finish(void)
247 suspend_thaw_processes();
248 usermodehelper_enable();
249 pm_notifier_call_chain(PM_POST_SUSPEND);
250 pm_restore_console();
254 * enter_state - Do common work of entering low-power state.
255 * @state: pm_state structure for state we're entering.
257 * Make sure we're the only ones trying to enter a sleep state. Fail
258 * if someone has beat us to it, since we don't want anything weird to
259 * happen when we wake up.
260 * Then, do the setup for suspend, enter the state, and cleaup (after
261 * we've woken up).
263 int enter_state(suspend_state_t state)
265 int error;
267 if (!valid_state(state))
268 return -ENODEV;
270 if (!mutex_trylock(&pm_mutex))
271 return -EBUSY;
273 printk(KERN_INFO "PM: Syncing filesystems ... ");
274 sys_sync();
275 printk("done.\n");
277 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
278 error = suspend_prepare();
279 if (error)
280 goto Unlock;
282 if (suspend_test(TEST_FREEZER))
283 goto Finish;
285 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
286 error = suspend_devices_and_enter(state);
288 Finish:
289 pr_debug("PM: Finishing wakeup.\n");
290 suspend_finish();
291 Unlock:
292 mutex_unlock(&pm_mutex);
293 return error;
297 * pm_suspend - Externally visible function for suspending system.
298 * @state: Enumerated value of state to enter.
300 * Determine whether or not value is within range, get state
301 * structure, and enter (above).
303 int pm_suspend(suspend_state_t state)
305 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
306 return enter_state(state);
307 return -EINVAL;
309 EXPORT_SYMBOL(pm_suspend);