ACPI: S4: Use "platform" rather than "shutdown" mode by default
[cascardo/linux.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  * 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 #include <linux/console.h>
22 #include <linux/cpu.h>
23
24 #include "power.h"
25
26
27 static int noresume = 0;
28 char resume_file[256] = CONFIG_PM_STD_PARTITION;
29 dev_t swsusp_resume_device;
30
31 /**
32  *      power_down - Shut machine down for hibernate.
33  *      @mode:          Suspend-to-disk mode
34  *
35  *      Use the platform driver, if configured so, and return gracefully if it
36  *      fails.
37  *      Otherwise, try to power off and reboot. If they fail, halt the machine,
38  *      there ain't no turning back.
39  */
40
41 static void power_down(suspend_disk_method_t mode)
42 {
43         int error = 0;
44
45         switch(mode) {
46         case PM_DISK_PLATFORM:
47                 if (pm_ops && pm_ops->enter) {
48                         kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
49                         error = pm_ops->enter(PM_SUSPEND_DISK);
50                         break;
51                 }
52         case PM_DISK_SHUTDOWN:
53                 kernel_power_off();
54                 break;
55         case PM_DISK_REBOOT:
56                 kernel_restart(NULL);
57                 break;
58         }
59         kernel_halt();
60         /* Valid image is on the disk, if we continue we risk serious data corruption
61            after resume. */
62         printk(KERN_CRIT "Please power me down manually\n");
63         while(1);
64 }
65
66 static inline void platform_finish(void)
67 {
68         if (pm_disk_mode == PM_DISK_PLATFORM) {
69                 if (pm_ops && pm_ops->finish)
70                         pm_ops->finish(PM_SUSPEND_DISK);
71         }
72 }
73
74 static int prepare_processes(void)
75 {
76         int error;
77
78         pm_prepare_console();
79
80         error = disable_nonboot_cpus();
81         if (error)
82                 goto enable_cpus;
83
84         if (freeze_processes()) {
85                 error = -EBUSY;
86                 goto thaw;
87         }
88
89         /* Free memory before shutting down devices. */
90         if (!(error = swsusp_shrink_memory()))
91                 return 0;
92 thaw:
93         thaw_processes();
94 enable_cpus:
95         enable_nonboot_cpus();
96         pm_restore_console();
97         return error;
98 }
99
100 static void unprepare_processes(void)
101 {
102         platform_finish();
103         thaw_processes();
104         enable_nonboot_cpus();
105         pm_restore_console();
106 }
107
108 /**
109  *      pm_suspend_disk - The granpappy of hibernation power management.
110  *
111  *      If we're going through the firmware, then get it over with quickly.
112  *
113  *      If not, then call swsusp to do its thing, then figure out how
114  *      to power down the system.
115  */
116
117 int pm_suspend_disk(void)
118 {
119         int error;
120
121         error = prepare_processes();
122         if (error)
123                 return error;
124
125         suspend_console();
126         error = device_suspend(PMSG_FREEZE);
127         if (error) {
128                 resume_console();
129                 printk("Some devices failed to suspend\n");
130                 unprepare_processes();
131                 return error;
132         }
133
134         pr_debug("PM: snapshotting memory.\n");
135         in_suspend = 1;
136         if ((error = swsusp_suspend()))
137                 goto Done;
138
139         if (in_suspend) {
140                 device_resume();
141                 resume_console();
142                 pr_debug("PM: writing image.\n");
143                 error = swsusp_write();
144                 if (!error)
145                         power_down(pm_disk_mode);
146                 else {
147                         swsusp_free();
148                         unprepare_processes();
149                         return error;
150                 }
151         } else
152                 pr_debug("PM: Image restored successfully.\n");
153
154         swsusp_free();
155  Done:
156         device_resume();
157         resume_console();
158         unprepare_processes();
159         return error;
160 }
161
162
163 /**
164  *      software_resume - Resume from a saved image.
165  *
166  *      Called as a late_initcall (so all devices are discovered and
167  *      initialized), we call swsusp to see if we have a saved image or not.
168  *      If so, we quiesce devices, the restore the saved image. We will
169  *      return above (in pm_suspend_disk() ) if everything goes well.
170  *      Otherwise, we fail gracefully and return to the normally
171  *      scheduled program.
172  *
173  */
174
175 static int software_resume(void)
176 {
177         int error;
178
179         down(&pm_sem);
180         if (!swsusp_resume_device) {
181                 if (!strlen(resume_file)) {
182                         up(&pm_sem);
183                         return -ENOENT;
184                 }
185                 swsusp_resume_device = name_to_dev_t(resume_file);
186                 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
187         } else {
188                 pr_debug("swsusp: Resume From Partition %d:%d\n",
189                          MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
190         }
191
192         if (noresume) {
193                 /**
194                  * FIXME: If noresume is specified, we need to find the partition
195                  * and reset it back to normal swap space.
196                  */
197                 up(&pm_sem);
198                 return 0;
199         }
200
201         pr_debug("PM: Checking swsusp image.\n");
202
203         if ((error = swsusp_check()))
204                 goto Done;
205
206         pr_debug("PM: Preparing processes for restore.\n");
207
208         if ((error = prepare_processes())) {
209                 swsusp_close();
210                 goto Done;
211         }
212
213         pr_debug("PM: Reading swsusp image.\n");
214
215         if ((error = swsusp_read())) {
216                 swsusp_free();
217                 goto Thaw;
218         }
219
220         pr_debug("PM: Preparing devices for restore.\n");
221
222         suspend_console();
223         if ((error = device_suspend(PMSG_PRETHAW))) {
224                 resume_console();
225                 printk("Some devices failed to suspend\n");
226                 swsusp_free();
227                 goto Thaw;
228         }
229
230         mb();
231
232         pr_debug("PM: Restoring saved image.\n");
233         swsusp_resume();
234         pr_debug("PM: Restore failed, recovering.n");
235         device_resume();
236         resume_console();
237  Thaw:
238         unprepare_processes();
239  Done:
240         /* For success case, the suspend path will release the lock */
241         up(&pm_sem);
242         pr_debug("PM: Resume from disk failed.\n");
243         return 0;
244 }
245
246 late_initcall(software_resume);
247
248
249 static const char * const 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         suspend_disk_method_t 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 ssize_t resume_show(struct subsystem * subsys, char *buf)
329 {
330         return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
331                        MINOR(swsusp_resume_device));
332 }
333
334 static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
335 {
336         unsigned int maj, min;
337         dev_t res;
338         int ret = -EINVAL;
339
340         if (sscanf(buf, "%u:%u", &maj, &min) != 2)
341                 goto out;
342
343         res = MKDEV(maj,min);
344         if (maj != MAJOR(res) || min != MINOR(res))
345                 goto out;
346
347         down(&pm_sem);
348         swsusp_resume_device = res;
349         up(&pm_sem);
350         printk("Attempting manual resume\n");
351         noresume = 0;
352         software_resume();
353         ret = n;
354 out:
355         return ret;
356 }
357
358 power_attr(resume);
359
360 static ssize_t image_size_show(struct subsystem * subsys, char *buf)
361 {
362         return sprintf(buf, "%lu\n", image_size);
363 }
364
365 static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n)
366 {
367         unsigned long size;
368
369         if (sscanf(buf, "%lu", &size) == 1) {
370                 image_size = size;
371                 return n;
372         }
373
374         return -EINVAL;
375 }
376
377 power_attr(image_size);
378
379 static struct attribute * g[] = {
380         &disk_attr.attr,
381         &resume_attr.attr,
382         &image_size_attr.attr,
383         NULL,
384 };
385
386
387 static struct attribute_group attr_group = {
388         .attrs = g,
389 };
390
391
392 static int __init pm_disk_init(void)
393 {
394         return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
395 }
396
397 core_initcall(pm_disk_init);
398
399
400 static int __init resume_setup(char *str)
401 {
402         if (noresume)
403                 return 1;
404
405         strncpy( resume_file, str, 255 );
406         return 1;
407 }
408
409 static int __init noresume_setup(char *str)
410 {
411         noresume = 1;
412         return 1;
413 }
414
415 __setup("noresume", noresume_setup);
416 __setup("resume=", resume_setup);