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