ACPI: S4: Use "platform" rather than "shutdown" mode by default
[linux-2.6.git] / kernel / power / disk.c
blobae6bbc903b7d1993577375a7b7e77faf184c33a9
1 /*
2 * kernel/power/disk.c - Suspend-to-disk support.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
6 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
8 * This file is released under the GPLv2.
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>
24 #include "power.h"
27 static int noresume = 0;
28 char resume_file[256] = CONFIG_PM_STD_PARTITION;
29 dev_t swsusp_resume_device;
31 /**
32 * power_down - Shut machine down for hibernate.
33 * @mode: Suspend-to-disk mode
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.
41 static void power_down(suspend_disk_method_t mode)
43 int error = 0;
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;
52 case PM_DISK_SHUTDOWN:
53 kernel_power_off();
54 break;
55 case PM_DISK_REBOOT:
56 kernel_restart(NULL);
57 break;
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);
66 static inline void platform_finish(void)
68 if (pm_disk_mode == PM_DISK_PLATFORM) {
69 if (pm_ops && pm_ops->finish)
70 pm_ops->finish(PM_SUSPEND_DISK);
74 static int prepare_processes(void)
76 int error;
78 pm_prepare_console();
80 error = disable_nonboot_cpus();
81 if (error)
82 goto enable_cpus;
84 if (freeze_processes()) {
85 error = -EBUSY;
86 goto thaw;
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;
100 static void unprepare_processes(void)
102 platform_finish();
103 thaw_processes();
104 enable_nonboot_cpus();
105 pm_restore_console();
109 * pm_suspend_disk - The granpappy of hibernation power management.
111 * If we're going through the firmware, then get it over with quickly.
113 * If not, then call swsusp to do its thing, then figure out how
114 * to power down the system.
117 int pm_suspend_disk(void)
119 int error;
121 error = prepare_processes();
122 if (error)
123 return error;
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;
134 pr_debug("PM: snapshotting memory.\n");
135 in_suspend = 1;
136 if ((error = swsusp_suspend()))
137 goto Done;
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;
151 } else
152 pr_debug("PM: Image restored successfully.\n");
154 swsusp_free();
155 Done:
156 device_resume();
157 resume_console();
158 unprepare_processes();
159 return error;
164 * software_resume - Resume from a saved image.
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.
175 static int software_resume(void)
177 int error;
179 down(&pm_sem);
180 if (!swsusp_resume_device) {
181 if (!strlen(resume_file)) {
182 up(&pm_sem);
183 return -ENOENT;
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));
192 if (noresume) {
194 * FIXME: If noresume is specified, we need to find the partition
195 * and reset it back to normal swap space.
197 up(&pm_sem);
198 return 0;
201 pr_debug("PM: Checking swsusp image.\n");
203 if ((error = swsusp_check()))
204 goto Done;
206 pr_debug("PM: Preparing processes for restore.\n");
208 if ((error = prepare_processes())) {
209 swsusp_close();
210 goto Done;
213 pr_debug("PM: Reading swsusp image.\n");
215 if ((error = swsusp_read())) {
216 swsusp_free();
217 goto Thaw;
220 pr_debug("PM: Preparing devices for restore.\n");
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;
230 mb();
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;
246 late_initcall(software_resume);
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",
257 * disk - Control suspend-to-disk mode
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).
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.
271 * show() will display what the mode is currently set to.
272 * store() will accept one of
274 * 'firmware'
275 * 'platform'
276 * 'shutdown'
277 * 'reboot'
279 * It will only change to 'firmware' or 'platform' if the system
280 * supports it (as determined from pm_ops->pm_disk_mode).
283 static ssize_t disk_show(struct subsystem * subsys, char * buf)
285 return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]);
289 static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
291 int error = 0;
292 int i;
293 int len;
294 char *p;
295 suspend_disk_method_t mode = 0;
297 p = memchr(buf, '\n', n);
298 len = p ? p - buf : n;
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;
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;
317 } else
318 error = -EINVAL;
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;
326 power_attr(disk);
328 static ssize_t resume_show(struct subsystem * subsys, char *buf)
330 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
331 MINOR(swsusp_resume_device));
334 static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
336 unsigned int maj, min;
337 dev_t res;
338 int ret = -EINVAL;
340 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
341 goto out;
343 res = MKDEV(maj,min);
344 if (maj != MAJOR(res) || min != MINOR(res))
345 goto out;
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;
358 power_attr(resume);
360 static ssize_t image_size_show(struct subsystem * subsys, char *buf)
362 return sprintf(buf, "%lu\n", image_size);
365 static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n)
367 unsigned long size;
369 if (sscanf(buf, "%lu", &size) == 1) {
370 image_size = size;
371 return n;
374 return -EINVAL;
377 power_attr(image_size);
379 static struct attribute * g[] = {
380 &disk_attr.attr,
381 &resume_attr.attr,
382 &image_size_attr.attr,
383 NULL,
387 static struct attribute_group attr_group = {
388 .attrs = g,
392 static int __init pm_disk_init(void)
394 return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
397 core_initcall(pm_disk_init);
400 static int __init resume_setup(char *str)
402 if (noresume)
403 return 1;
405 strncpy( resume_file, str, 255 );
406 return 1;
409 static int __init noresume_setup(char *str)
411 noresume = 1;
412 return 1;
415 __setup("noresume", noresume_setup);
416 __setup("resume=", resume_setup);