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>
19 #include <linux/mount.h>
21 #include <linux/console.h>
22 #include <linux/cpu.h>
27 static int noresume = 0;
28 char resume_file[256] = CONFIG_PM_STD_PARTITION;
29 dev_t swsusp_resume_device;
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
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)
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);
52 case PM_DISK_SHUTDOWN:
60 /* Valid image is on the disk, if we continue we risk serious data corruption
62 printk(KERN_CRIT "Please power me down manually\n");
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)
80 error = disable_nonboot_cpus();
84 if (freeze_processes()) {
89 /* Free memory before shutting down devices. */
90 if (!(error = swsusp_shrink_memory()))
95 enable_nonboot_cpus();
100 static void unprepare_processes(void)
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)
121 error = prepare_processes();
126 error = device_suspend(PMSG_FREEZE);
129 printk("Some devices failed to suspend\n");
130 unprepare_processes();
134 pr_debug("PM: snapshotting memory.\n");
136 if ((error = swsusp_suspend()))
142 pr_debug("PM: writing image.\n");
143 error = swsusp_write();
145 power_down(pm_disk_mode);
148 unprepare_processes();
152 pr_debug("PM: Image restored successfully.\n");
158 unprepare_processes();
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
175 static int software_resume(void)
180 if (!swsusp_resume_device) {
181 if (!strlen(resume_file)) {
185 swsusp_resume_device = name_to_dev_t(resume_file);
186 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
188 pr_debug("swsusp: Resume From Partition %d:%d\n",
189 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
194 * FIXME: If noresume is specified, we need to find the partition
195 * and reset it back to normal swap space.
201 pr_debug("PM: Checking swsusp image.\n");
203 if ((error = swsusp_check()))
206 pr_debug("PM: Preparing processes for restore.\n");
208 if ((error = prepare_processes())) {
213 pr_debug("PM: Reading swsusp image.\n");
215 if ((error = swsusp_read())) {
220 pr_debug("PM: Preparing devices for restore.\n");
223 if ((error = device_suspend(PMSG_PRETHAW))) {
225 printk("Some devices failed to suspend\n");
232 pr_debug("PM: Restoring saved image.\n");
234 pr_debug("PM: Restore failed, recovering.n");
238 unprepare_processes();
240 /* For success case, the suspend path will release the lock */
242 pr_debug("PM: Resume from disk failed.\n");
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
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
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)
295 suspend_disk_method_t mode = 0;
297 p = memchr(buf, '\n', n);
298 len = p ? p - buf : n;
301 for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
302 if (!strncmp(buf, pm_disk_modes[i], len)) {
308 if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT)
311 if (pm_ops && pm_ops->enter &&
312 (mode == pm_ops->pm_disk_mode))
320 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
321 pm_disk_modes[mode]);
323 return error ? error : n;
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;
340 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
343 res = MKDEV(maj,min);
344 if (maj != MAJOR(res) || min != MINOR(res))
348 swsusp_resume_device = res;
350 printk("Attempting manual resume\n");
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)
369 if (sscanf(buf, "%lu", &size) == 1) {
377 power_attr(image_size);
379 static struct attribute * g[] = {
382 &image_size_attr.attr,
387 static struct attribute_group attr_group = {
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)
405 strncpy( resume_file, str, 255 );
409 static int __init noresume_setup(char *str)
415 __setup("noresume", noresume_setup);
416 __setup("resume=", resume_setup);