mm/pm: force GFP_NOIO during suspend/hibernation and resume
[linux-2.6.git] / kernel / power / suspend.c
1 /*
2  * kernel/power/suspend.c - Suspend to RAM and standby functionality.
3  *
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.
7  *
8  * This file is released under the GPLv2.
9  */
10
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
19 #include "power.h"
20
21 const char *const pm_states[PM_SUSPEND_MAX] = {
22         [PM_SUSPEND_STANDBY]    = "standby",
23         [PM_SUSPEND_MEM]        = "mem",
24 };
25
26 static struct platform_suspend_ops *suspend_ops;
27
28 /**
29  *      suspend_set_ops - Set the global suspend method table.
30  *      @ops:   Pointer to ops structure.
31  */
32 void suspend_set_ops(struct platform_suspend_ops *ops)
33 {
34         mutex_lock(&pm_mutex);
35         suspend_ops = ops;
36         mutex_unlock(&pm_mutex);
37 }
38
39 bool valid_state(suspend_state_t state)
40 {
41         /*
42          * All states need lowlevel support and need to be valid to the lowlevel
43          * implementation, no valid callback implies that none are valid.
44          */
45         return suspend_ops && suspend_ops->valid && suspend_ops->valid(state);
46 }
47
48 /**
49  * suspend_valid_only_mem - generic memory-only valid callback
50  *
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.
54  */
55 int suspend_valid_only_mem(suspend_state_t state)
56 {
57         return state == PM_SUSPEND_MEM;
58 }
59
60 static int suspend_test(int level)
61 {
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;
67         }
68 #endif /* !CONFIG_PM_DEBUG */
69         return 0;
70 }
71
72 /**
73  *      suspend_prepare - Do prep work before entering low-power state.
74  *
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.
77  */
78 static int suspend_prepare(void)
79 {
80         int error;
81
82         if (!suspend_ops || !suspend_ops->enter)
83                 return -EPERM;
84
85         pm_prepare_console();
86
87         error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
88         if (error)
89                 goto Finish;
90
91         error = usermodehelper_disable();
92         if (error)
93                 goto Finish;
94
95         error = suspend_freeze_processes();
96         if (!error)
97                 return 0;
98
99         suspend_thaw_processes();
100         usermodehelper_enable();
101  Finish:
102         pm_notifier_call_chain(PM_POST_SUSPEND);
103         pm_restore_console();
104         return error;
105 }
106
107 /* default implementation */
108 void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
109 {
110         local_irq_disable();
111 }
112
113 /* default implementation */
114 void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
115 {
116         local_irq_enable();
117 }
118
119 /**
120  *      suspend_enter - enter the desired system sleep state.
121  *      @state:         state to enter
122  *
123  *      This function should be called after devices have been suspended.
124  */
125 static int suspend_enter(suspend_state_t state)
126 {
127         int error;
128
129         if (suspend_ops->prepare) {
130                 error = suspend_ops->prepare();
131                 if (error)
132                         return error;
133         }
134
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;
139         }
140
141         if (suspend_ops->prepare_late) {
142                 error = suspend_ops->prepare_late();
143                 if (error)
144                         goto Power_up_devices;
145         }
146
147         if (suspend_test(TEST_PLATFORM))
148                 goto Platform_wake;
149
150         error = disable_nonboot_cpus();
151         if (error || suspend_test(TEST_CPUS))
152                 goto Enable_cpus;
153
154         arch_suspend_disable_irqs();
155         BUG_ON(!irqs_disabled());
156
157         error = sysdev_suspend(PMSG_SUSPEND);
158         if (!error) {
159                 if (!suspend_test(TEST_CORE))
160                         error = suspend_ops->enter(state);
161                 sysdev_resume();
162         }
163
164         arch_suspend_enable_irqs();
165         BUG_ON(irqs_disabled());
166
167  Enable_cpus:
168         enable_nonboot_cpus();
169
170  Platform_wake:
171         if (suspend_ops->wake)
172                 suspend_ops->wake();
173
174  Power_up_devices:
175         dpm_resume_noirq(PMSG_RESUME);
176
177  Platfrom_finish:
178         if (suspend_ops->finish)
179                 suspend_ops->finish();
180
181         return error;
182 }
183
184 /**
185  *      suspend_devices_and_enter - suspend devices and enter the desired system
186  *                                  sleep state.
187  *      @state:           state to enter
188  */
189 int suspend_devices_and_enter(suspend_state_t state)
190 {
191         int error;
192         gfp_t saved_mask;
193
194         if (!suspend_ops)
195                 return -ENOSYS;
196
197         if (suspend_ops->begin) {
198                 error = suspend_ops->begin(state);
199                 if (error)
200                         goto Close;
201         }
202         suspend_console();
203         saved_mask = clear_gfp_allowed_mask(GFP_IOFS);
204         suspend_test_start();
205         error = dpm_suspend_start(PMSG_SUSPEND);
206         if (error) {
207                 printk(KERN_ERR "PM: Some devices failed to suspend\n");
208                 goto Recover_platform;
209         }
210         suspend_test_finish("suspend devices");
211         if (suspend_test(TEST_DEVICES))
212                 goto Recover_platform;
213
214         suspend_enter(state);
215
216  Resume_devices:
217         suspend_test_start();
218         dpm_resume_end(PMSG_RESUME);
219         suspend_test_finish("resume devices");
220         set_gfp_allowed_mask(saved_mask);
221         resume_console();
222  Close:
223         if (suspend_ops->end)
224                 suspend_ops->end();
225         return error;
226
227  Recover_platform:
228         if (suspend_ops->recover)
229                 suspend_ops->recover();
230         goto Resume_devices;
231 }
232
233 /**
234  *      suspend_finish - Do final work before exiting suspend sequence.
235  *
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.
238  */
239 static void suspend_finish(void)
240 {
241         suspend_thaw_processes();
242         usermodehelper_enable();
243         pm_notifier_call_chain(PM_POST_SUSPEND);
244         pm_restore_console();
245 }
246
247 /**
248  *      enter_state - Do common work of entering low-power state.
249  *      @state:         pm_state structure for state we're entering.
250  *
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
255  *      we've woken up).
256  */
257 int enter_state(suspend_state_t state)
258 {
259         int error;
260
261         if (!valid_state(state))
262                 return -ENODEV;
263
264         if (!mutex_trylock(&pm_mutex))
265                 return -EBUSY;
266
267         printk(KERN_INFO "PM: Syncing filesystems ... ");
268         sys_sync();
269         printk("done.\n");
270
271         pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
272         error = suspend_prepare();
273         if (error)
274                 goto Unlock;
275
276         if (suspend_test(TEST_FREEZER))
277                 goto Finish;
278
279         pr_debug("PM: Entering %s sleep\n", pm_states[state]);
280         error = suspend_devices_and_enter(state);
281
282  Finish:
283         pr_debug("PM: Finishing wakeup.\n");
284         suspend_finish();
285  Unlock:
286         mutex_unlock(&pm_mutex);
287         return error;
288 }
289
290 /**
291  *      pm_suspend - Externally visible function for suspending system.
292  *      @state:         Enumerated value of state to enter.
293  *
294  *      Determine whether or not value is within range, get state
295  *      structure, and enter (above).
296  */
297 int pm_suspend(suspend_state_t state)
298 {
299         if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
300                 return enter_state(state);
301         return -EINVAL;
302 }
303 EXPORT_SYMBOL(pm_suspend);