vserver 1.9.3
[linux-2.6.git] / kernel / power / disk.c
1 /*
2  * kernel/power/disk.c - Suspend-to-disk support.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  *
7  * This file is released under the GPLv2.
8  *
9  */
10
11 #include <linux/suspend.h>
12 #include <linux/syscalls.h>
13 #include <linux/reboot.h>
14 #include <linux/string.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
17 #include <linux/fs.h>
18 #include "power.h"
19
20
21 extern u32 pm_disk_mode;
22 extern struct pm_ops * pm_ops;
23
24 extern int swsusp_suspend(void);
25 extern int swsusp_write(void);
26 extern int swsusp_read(void);
27 extern int swsusp_resume(void);
28 extern int swsusp_free(void);
29
30
31 static int noresume = 0;
32 char resume_file[256] = CONFIG_PM_STD_PARTITION;
33
34 /**
35  *      power_down - Shut machine down for hibernate.
36  *      @mode:          Suspend-to-disk mode
37  *
38  *      Use the platform driver, if configured so, and return gracefully if it
39  *      fails.
40  *      Otherwise, try to power off and reboot. If they fail, halt the machine,
41  *      there ain't no turning back.
42  */
43
44 static int power_down(u32 mode)
45 {
46         unsigned long flags;
47         int error = 0;
48
49         local_irq_save(flags);
50         switch(mode) {
51         case PM_DISK_PLATFORM:
52                 device_power_down(PM_SUSPEND_DISK);
53                 error = pm_ops->enter(PM_SUSPEND_DISK);
54                 break;
55         case PM_DISK_SHUTDOWN:
56                 printk("Powering off system\n");
57                 device_shutdown();
58                 machine_power_off();
59                 break;
60         case PM_DISK_REBOOT:
61                 device_shutdown();
62                 machine_restart(NULL);
63                 break;
64         }
65         machine_halt();
66         /* Valid image is on the disk, if we continue we risk serious data corruption
67            after resume. */
68         printk(KERN_CRIT "Please power me down manually\n");
69         while(1);
70         return 0;
71 }
72
73
74 static int in_suspend __nosavedata = 0;
75
76
77 /**
78  *      free_some_memory -  Try to free as much memory as possible
79  *
80  *      ... but do not OOM-kill anyone
81  *
82  *      Notice: all userland should be stopped at this point, or
83  *      livelock is possible.
84  */
85
86 static void free_some_memory(void)
87 {
88         printk("Freeing memory: ");
89         while (shrink_all_memory(10000))
90                 printk(".");
91         printk("|\n");
92 }
93
94
95 static inline void platform_finish(void)
96 {
97         if (pm_disk_mode == PM_DISK_PLATFORM) {
98                 if (pm_ops && pm_ops->finish)
99                         pm_ops->finish(PM_SUSPEND_DISK);
100         }
101 }
102
103 static void finish(void)
104 {
105         device_resume();
106         platform_finish();
107         enable_nonboot_cpus();
108         thaw_processes();
109         pm_restore_console();
110 }
111
112
113 static int prepare(void)
114 {
115         int error;
116
117         pm_prepare_console();
118
119         sys_sync();
120         if (freeze_processes()) {
121                 error = -EBUSY;
122                 goto Thaw;
123         }
124
125         if (pm_disk_mode == PM_DISK_PLATFORM) {
126                 if (pm_ops && pm_ops->prepare) {
127                         if ((error = pm_ops->prepare(PM_SUSPEND_DISK)))
128                                 goto Thaw;
129                 }
130         }
131
132         /* Free memory before shutting down devices. */
133         free_some_memory();
134
135         disable_nonboot_cpus();
136         if ((error = device_suspend(PM_SUSPEND_DISK)))
137                 goto Finish;
138
139         return 0;
140  Finish:
141         platform_finish();
142  Thaw:
143         enable_nonboot_cpus();
144         thaw_processes();
145         pm_restore_console();
146         return error;
147 }
148
149
150 /**
151  *      pm_suspend_disk - The granpappy of power management.
152  *
153  *      If we're going through the firmware, then get it over with quickly.
154  *
155  *      If not, then call pmdis to do it's thing, then figure out how
156  *      to power down the system.
157  */
158
159 int pm_suspend_disk(void)
160 {
161         int error;
162
163         if ((error = prepare()))
164                 return error;
165
166         pr_debug("PM: Attempting to suspend to disk.\n");
167         if (pm_disk_mode == PM_DISK_FIRMWARE)
168                 return pm_ops->enter(PM_SUSPEND_DISK);
169
170         pr_debug("PM: snapshotting memory.\n");
171         in_suspend = 1;
172         if ((error = swsusp_suspend()))
173                 goto Done;
174
175         if (in_suspend) {
176                 pr_debug("PM: writing image.\n");
177
178                 /*
179                  * FIXME: Leftover from swsusp. Are they necessary?
180                  */
181                 mb();
182                 barrier();
183
184                 error = swsusp_write();
185                 if (!error) {
186                         error = power_down(pm_disk_mode);
187                         pr_debug("PM: Power down failed.\n");
188                 }
189         } else
190                 pr_debug("PM: Image restored successfully.\n");
191         swsusp_free();
192  Done:
193         finish();
194         return error;
195 }
196
197
198 /**
199  *      software_resume - Resume from a saved image.
200  *
201  *      Called as a late_initcall (so all devices are discovered and
202  *      initialized), we call pmdisk to see if we have a saved image or not.
203  *      If so, we quiesce devices, the restore the saved image. We will
204  *      return above (in pm_suspend_disk() ) if everything goes well.
205  *      Otherwise, we fail gracefully and return to the normally
206  *      scheduled program.
207  *
208  */
209
210 static int software_resume(void)
211 {
212         int error;
213
214         if (noresume) {
215                 /**
216                  * FIXME: If noresume is specified, we need to find the partition
217                  * and reset it back to normal swap space.
218                  */
219                 return 0;
220         }
221
222         pr_debug("PM: Reading pmdisk image.\n");
223
224         if ((error = swsusp_read()))
225                 goto Done;
226
227         pr_debug("PM: Preparing system for restore.\n");
228
229         if ((error = prepare()))
230                 goto Free;
231
232         barrier();
233         mb();
234
235         pr_debug("PM: Restoring saved image.\n");
236         swsusp_resume();
237         pr_debug("PM: Restore failed, recovering.n");
238         finish();
239  Free:
240         swsusp_free();
241  Done:
242         pr_debug("PM: Resume from disk failed.\n");
243         return 0;
244 }
245
246 late_initcall(software_resume);
247
248
249 static char * pm_disk_modes[] = {
250         [PM_DISK_FIRMWARE]      = "firmware",
251         [PM_DISK_PLATFORM]      = "platform",
252         [PM_DISK_SHUTDOWN]      = "shutdown",
253         [PM_DISK_REBOOT]        = "reboot",
254 };
255
256 /**
257  *      disk - Control suspend-to-disk mode
258  *
259  *      Suspend-to-disk can be handled in several ways. The greatest
260  *      distinction is who writes memory to disk - the firmware or the OS.
261  *      If the firmware does it, we assume that it also handles suspending
262  *      the system.
263  *      If the OS does it, then we have three options for putting the system
264  *      to sleep - using the platform driver (e.g. ACPI or other PM registers),
265  *      powering off the system or rebooting the system (for testing).
266  *
267  *      The system will support either 'firmware' or 'platform', and that is
268  *      known a priori (and encoded in pm_ops). But, the user may choose
269  *      'shutdown' or 'reboot' as alternatives.
270  *
271  *      show() will display what the mode is currently set to.
272  *      store() will accept one of
273  *
274  *      'firmware'
275  *      'platform'
276  *      'shutdown'
277  *      'reboot'
278  *
279  *      It will only change to 'firmware' or 'platform' if the system
280  *      supports it (as determined from pm_ops->pm_disk_mode).
281  */
282
283 static ssize_t disk_show(struct subsystem * subsys, char * buf)
284 {
285         return sprintf(buf,"%s\n",pm_disk_modes[pm_disk_mode]);
286 }
287
288
289 static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
290 {
291         int error = 0;
292         int i;
293         int len;
294         char *p;
295         u32 mode = 0;
296
297         p = memchr(buf, '\n', n);
298         len = p ? p - buf : n;
299
300         down(&pm_sem);
301         for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
302                 if (!strncmp(buf, pm_disk_modes[i], len)) {
303                         mode = i;
304                         break;
305                 }
306         }
307         if (mode) {
308                 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT)
309                         pm_disk_mode = mode;
310                 else {
311                         if (pm_ops && pm_ops->enter &&
312                             (mode == pm_ops->pm_disk_mode))
313                                 pm_disk_mode = mode;
314                         else
315                                 error = -EINVAL;
316                 }
317         } else
318                 error = -EINVAL;
319
320         pr_debug("PM: suspend-to-disk mode set to '%s'\n",
321                  pm_disk_modes[mode]);
322         up(&pm_sem);
323         return error ? error : n;
324 }
325
326 power_attr(disk);
327
328 static struct attribute * g[] = {
329         &disk_attr.attr,
330         NULL,
331 };
332
333
334 static struct attribute_group attr_group = {
335         .attrs = g,
336 };
337
338
339 static int __init pm_disk_init(void)
340 {
341         return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
342 }
343
344 core_initcall(pm_disk_init);
345
346
347 static int __init resume_setup(char *str)
348 {
349         if (noresume)
350                 return 1;
351
352         strncpy( resume_file, str, 255 );
353         return 1;
354 }
355
356 static int __init noresume_setup(char *str)
357 {
358         noresume = 1;
359         return 1;
360 }
361
362 __setup("noresume", noresume_setup);
363 __setup("resume=", resume_setup);