powerpc/pmac: Add missing unlocks in error path
[linux-2.6/next.git] / kernel / power / suspend.c
blob56e7dbb8b996db295b4fc7cdf1b5f11a0409cb0c
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>
20 #include "power.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;
29 /**
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);
36 suspend_ops = ops;
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);
49 /**
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");
66 mdelay(5000);
67 return 1;
69 #endif /* !CONFIG_PM_DEBUG */
70 return 0;
73 /**
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)
81 int error;
83 if (!suspend_ops || !suspend_ops->enter)
84 return -EPERM;
86 pm_prepare_console();
88 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
89 if (error)
90 goto Finish;
92 error = usermodehelper_disable();
93 if (error)
94 goto Finish;
96 error = suspend_freeze_processes();
97 if (!error)
98 return 0;
100 suspend_thaw_processes();
101 usermodehelper_enable();
102 Finish:
103 pm_notifier_call_chain(PM_POST_SUSPEND);
104 pm_restore_console();
105 return error;
108 /* default implementation */
109 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
111 local_irq_disable();
114 /* default implementation */
115 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
117 local_irq_enable();
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)
128 int error;
130 if (suspend_ops->prepare) {
131 error = suspend_ops->prepare();
132 if (error)
133 return error;
136 error = dpm_suspend_noirq(PMSG_SUSPEND);
137 if (error) {
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();
144 if (error)
145 goto Power_up_devices;
148 if (suspend_test(TEST_PLATFORM))
149 goto Platform_wake;
151 error = disable_nonboot_cpus();
152 if (error || suspend_test(TEST_CPUS))
153 goto Enable_cpus;
155 arch_suspend_disable_irqs();
156 BUG_ON(!irqs_disabled());
158 error = sysdev_suspend(PMSG_SUSPEND);
159 if (!error) {
160 if (!suspend_test(TEST_CORE))
161 error = suspend_ops->enter(state);
162 sysdev_resume();
165 arch_suspend_enable_irqs();
166 BUG_ON(irqs_disabled());
168 Enable_cpus:
169 enable_nonboot_cpus();
171 Platform_wake:
172 if (suspend_ops->wake)
173 suspend_ops->wake();
175 Power_up_devices:
176 dpm_resume_noirq(PMSG_RESUME);
178 Platfrom_finish:
179 if (suspend_ops->finish)
180 suspend_ops->finish();
182 return error;
186 * suspend_devices_and_enter - suspend devices and enter the desired system
187 * sleep state.
188 * @state: state to enter
190 int suspend_devices_and_enter(suspend_state_t state)
192 int error;
193 gfp_t saved_mask;
195 if (!suspend_ops)
196 return -ENOSYS;
198 if (suspend_ops->begin) {
199 error = suspend_ops->begin(state);
200 if (error)
201 goto Close;
203 suspend_console();
204 saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
205 suspend_test_start();
206 error = dpm_suspend_start(PMSG_SUSPEND);
207 if (error) {
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);
217 Resume_devices:
218 suspend_test_start();
219 dpm_resume_end(PMSG_RESUME);
220 suspend_test_finish("resume devices");
221 set_gfp_allowed_mask(saved_mask);
222 resume_console();
223 Close:
224 if (suspend_ops->end)
225 suspend_ops->end();
226 return error;
228 Recover_platform:
229 if (suspend_ops->recover)
230 suspend_ops->recover();
231 goto Resume_devices;
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
256 * we've woken up).
258 int enter_state(suspend_state_t state)
260 int error;
262 if (!valid_state(state))
263 return -ENODEV;
265 if (!mutex_trylock(&pm_mutex))
266 return -EBUSY;
268 printk(KERN_INFO "PM: Syncing filesystems ... ");
269 sys_sync();
270 printk("done.\n");
272 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
273 error = suspend_prepare();
274 if (error)
275 goto Unlock;
277 if (suspend_test(TEST_FREEZER))
278 goto Finish;
280 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
281 error = suspend_devices_and_enter(state);
283 Finish:
284 pr_debug("PM: Finishing wakeup.\n");
285 suspend_finish();
286 Unlock:
287 mutex_unlock(&pm_mutex);
288 return error;
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);
302 return -EINVAL;
304 EXPORT_SYMBOL(pm_suspend);