[SCSI] hpsa: allow modifying device queue depth.
[linux-2.6/next.git] / kernel / power / suspend.c
blob6f10dfc2d3e9f79a0a57cd73b8a70ad2cc90d22b
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>
19 #include "power.h"
21 const char *const pm_states[PM_SUSPEND_MAX] = {
22 [PM_SUSPEND_STANDBY] = "standby",
23 [PM_SUSPEND_MEM] = "mem",
26 static struct platform_suspend_ops *suspend_ops;
28 /**
29 * suspend_set_ops - Set the global suspend method table.
30 * @ops: Pointer to ops structure.
32 void suspend_set_ops(struct platform_suspend_ops *ops)
34 mutex_lock(&pm_mutex);
35 suspend_ops = ops;
36 mutex_unlock(&pm_mutex);
39 bool valid_state(suspend_state_t state)
42 * All states need lowlevel support and need to be valid to the lowlevel
43 * implementation, no valid callback implies that none are valid.
45 return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
48 /**
49 * suspend_valid_only_mem - generic memory-only valid callback
51 * Platform drivers that implement mem suspend only and only need
52 * to check for that in their .valid callback can use this instead
53 * of rolling their own .valid callback.
55 int suspend_valid_only_mem(suspend_state_t state)
57 return state == PM_SUSPEND_MEM;
60 static int suspend_test(int level)
62 #ifdef CONFIG_PM_DEBUG
63 if (pm_test_level == level) {
64 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n");
65 mdelay(5000);
66 return 1;
68 #endif /* !CONFIG_PM_DEBUG */
69 return 0;
72 /**
73 * suspend_prepare - Do prep work before entering low-power state.
75 * This is common code that is called for each state that we're entering.
76 * Run suspend notifiers, allocate a console and stop all processes.
78 static int suspend_prepare(void)
80 int error;
82 if (!suspend_ops || !suspend_ops->enter)
83 return -EPERM;
85 pm_prepare_console();
87 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
88 if (error)
89 goto Finish;
91 error = usermodehelper_disable();
92 if (error)
93 goto Finish;
95 error = suspend_freeze_processes();
96 if (!error)
97 return 0;
99 suspend_thaw_processes();
100 usermodehelper_enable();
101 Finish:
102 pm_notifier_call_chain(PM_POST_SUSPEND);
103 pm_restore_console();
104 return error;
107 /* default implementation */
108 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
110 local_irq_disable();
113 /* default implementation */
114 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
116 local_irq_enable();
120 * suspend_enter - enter the desired system sleep state.
121 * @state: state to enter
123 * This function should be called after devices have been suspended.
125 static int suspend_enter(suspend_state_t state)
127 int error;
129 if (suspend_ops->prepare) {
130 error = suspend_ops->prepare();
131 if (error)
132 return error;
135 error = dpm_suspend_noirq(PMSG_SUSPEND);
136 if (error) {
137 printk(KERN_ERR "PM: Some devices failed to power down\n");
138 goto Platfrom_finish;
141 if (suspend_ops->prepare_late) {
142 error = suspend_ops->prepare_late();
143 if (error)
144 goto Power_up_devices;
147 if (suspend_test(TEST_PLATFORM))
148 goto Platform_wake;
150 error = disable_nonboot_cpus();
151 if (error || suspend_test(TEST_CPUS))
152 goto Enable_cpus;
154 arch_suspend_disable_irqs();
155 BUG_ON(!irqs_disabled());
157 error = sysdev_suspend(PMSG_SUSPEND);
158 if (!error) {
159 if (!suspend_test(TEST_CORE))
160 error = suspend_ops->enter(state);
161 sysdev_resume();
164 arch_suspend_enable_irqs();
165 BUG_ON(irqs_disabled());
167 Enable_cpus:
168 enable_nonboot_cpus();
170 Platform_wake:
171 if (suspend_ops->wake)
172 suspend_ops->wake();
174 Power_up_devices:
175 dpm_resume_noirq(PMSG_RESUME);
177 Platfrom_finish:
178 if (suspend_ops->finish)
179 suspend_ops->finish();
181 return error;
185 * suspend_devices_and_enter - suspend devices and enter the desired system
186 * sleep state.
187 * @state: state to enter
189 int suspend_devices_and_enter(suspend_state_t state)
191 int error;
193 if (!suspend_ops)
194 return -ENOSYS;
196 if (suspend_ops->begin) {
197 error = suspend_ops->begin(state);
198 if (error)
199 goto Close;
201 suspend_console();
202 suspend_test_start();
203 error = dpm_suspend_start(PMSG_SUSPEND);
204 if (error) {
205 printk(KERN_ERR "PM: Some devices failed to suspend\n");
206 goto Recover_platform;
208 suspend_test_finish("suspend devices");
209 if (suspend_test(TEST_DEVICES))
210 goto Recover_platform;
212 suspend_enter(state);
214 Resume_devices:
215 suspend_test_start();
216 dpm_resume_end(PMSG_RESUME);
217 suspend_test_finish("resume devices");
218 resume_console();
219 Close:
220 if (suspend_ops->end)
221 suspend_ops->end();
222 return error;
224 Recover_platform:
225 if (suspend_ops->recover)
226 suspend_ops->recover();
227 goto Resume_devices;
231 * suspend_finish - Do final work before exiting suspend sequence.
233 * Call platform code to clean up, restart processes, and free the
234 * console that we've allocated. This is not called for suspend-to-disk.
236 static void suspend_finish(void)
238 suspend_thaw_processes();
239 usermodehelper_enable();
240 pm_notifier_call_chain(PM_POST_SUSPEND);
241 pm_restore_console();
245 * enter_state - Do common work of entering low-power state.
246 * @state: pm_state structure for state we're entering.
248 * Make sure we're the only ones trying to enter a sleep state. Fail
249 * if someone has beat us to it, since we don't want anything weird to
250 * happen when we wake up.
251 * Then, do the setup for suspend, enter the state, and cleaup (after
252 * we've woken up).
254 int enter_state(suspend_state_t state)
256 int error;
258 if (!valid_state(state))
259 return -ENODEV;
261 if (!mutex_trylock(&pm_mutex))
262 return -EBUSY;
264 printk(KERN_INFO "PM: Syncing filesystems ... ");
265 sys_sync();
266 printk("done.\n");
268 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
269 error = suspend_prepare();
270 if (error)
271 goto Unlock;
273 if (suspend_test(TEST_FREEZER))
274 goto Finish;
276 pr_debug("PM: Entering %s sleep\n", pm_states[state]);
277 error = suspend_devices_and_enter(state);
279 Finish:
280 pr_debug("PM: Finishing wakeup.\n");
281 suspend_finish();
282 Unlock:
283 mutex_unlock(&pm_mutex);
284 return error;
288 * pm_suspend - Externally visible function for suspending system.
289 * @state: Enumerated value of state to enter.
291 * Determine whether or not value is within range, get state
292 * structure, and enter (above).
294 int pm_suspend(suspend_state_t state)
296 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
297 return enter_state(state);
298 return -EINVAL;
300 EXPORT_SYMBOL(pm_suspend);