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>
23 #include <linux/freezer.h>
28 static int noresume = 0;
29 char resume_file[256] = CONFIG_PM_STD_PARTITION;
30 dev_t swsusp_resume_device;
31 sector_t swsusp_resume_block;
34 * platform_prepare - prepare the machine for hibernation using the
35 * platform driver if so configured and return an error code if it fails
38 static inline int platform_prepare(void)
42 switch (pm_disk_mode) {
44 case PM_DISK_TESTPROC:
45 case PM_DISK_SHUTDOWN:
49 if (pm_ops && pm_ops->prepare)
50 error = pm_ops->prepare(PM_SUSPEND_DISK);
56 * power_down - Shut machine down for hibernate.
58 * Use the platform driver, if configured so; otherwise try
59 * to power off or reboot.
62 static void power_down(void)
64 switch (pm_disk_mode) {
66 case PM_DISK_TESTPROC:
68 case PM_DISK_SHUTDOWN:
75 if (pm_ops && pm_ops->enter) {
76 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
77 pm_ops->enter(PM_SUSPEND_DISK);
83 * Valid image is on the disk, if we continue we risk serious data
84 * corruption after resume.
86 printk(KERN_CRIT "Please power me down manually\n");
90 static inline void platform_finish(void)
92 switch (pm_disk_mode) {
94 case PM_DISK_TESTPROC:
95 case PM_DISK_SHUTDOWN:
99 if (pm_ops && pm_ops->finish)
100 pm_ops->finish(PM_SUSPEND_DISK);
104 static void unprepare_processes(void)
107 pm_restore_console();
110 static int prepare_processes(void)
114 pm_prepare_console();
115 if (freeze_processes()) {
117 unprepare_processes();
123 * pm_suspend_disk - The granpappy of hibernation power management.
125 * If we're going through the firmware, then get it over with quickly.
127 * If not, then call swsusp to do its thing, then figure out how
128 * to power down the system.
131 int pm_suspend_disk(void)
135 error = prepare_processes();
139 if (pm_disk_mode == PM_DISK_TESTPROC) {
140 printk("swsusp debug: Waiting for 5 seconds.\n");
144 /* Free memory before shutting down devices. */
145 error = swsusp_shrink_memory();
149 error = platform_prepare();
154 error = device_suspend(PMSG_FREEZE);
156 printk(KERN_ERR "PM: Some devices failed to suspend\n");
159 error = disable_nonboot_cpus();
163 if (pm_disk_mode == PM_DISK_TEST) {
164 printk("swsusp debug: Waiting for 5 seconds.\n");
169 pr_debug("PM: snapshotting memory.\n");
171 error = swsusp_suspend();
176 enable_nonboot_cpus();
180 pr_debug("PM: writing image.\n");
181 error = swsusp_write();
189 pr_debug("PM: Image restored successfully.\n");
194 enable_nonboot_cpus();
200 unprepare_processes();
206 * software_resume - Resume from a saved image.
208 * Called as a late_initcall (so all devices are discovered and
209 * initialized), we call swsusp to see if we have a saved image or not.
210 * If so, we quiesce devices, the restore the saved image. We will
211 * return above (in pm_suspend_disk() ) if everything goes well.
212 * Otherwise, we fail gracefully and return to the normally
217 static int software_resume(void)
221 mutex_lock(&pm_mutex);
222 if (!swsusp_resume_device) {
223 if (!strlen(resume_file)) {
224 mutex_unlock(&pm_mutex);
227 swsusp_resume_device = name_to_dev_t(resume_file);
228 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
230 pr_debug("swsusp: Resume From Partition %d:%d\n",
231 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
236 * FIXME: If noresume is specified, we need to find the partition
237 * and reset it back to normal swap space.
239 mutex_unlock(&pm_mutex);
243 pr_debug("PM: Checking swsusp image.\n");
245 error = swsusp_check();
249 pr_debug("PM: Preparing processes for restore.\n");
251 error = prepare_processes();
257 pr_debug("PM: Reading swsusp image.\n");
259 error = swsusp_read();
265 pr_debug("PM: Preparing devices for restore.\n");
268 error = device_suspend(PMSG_PRETHAW);
272 error = disable_nonboot_cpus();
276 enable_nonboot_cpus();
282 printk(KERN_ERR "PM: Restore failed, recovering.\n");
283 unprepare_processes();
285 /* For success case, the suspend path will release the lock */
286 mutex_unlock(&pm_mutex);
287 pr_debug("PM: Resume from disk failed.\n");
291 late_initcall(software_resume);
294 static const char * const pm_disk_modes[] = {
295 [PM_DISK_FIRMWARE] = "firmware",
296 [PM_DISK_PLATFORM] = "platform",
297 [PM_DISK_SHUTDOWN] = "shutdown",
298 [PM_DISK_REBOOT] = "reboot",
299 [PM_DISK_TEST] = "test",
300 [PM_DISK_TESTPROC] = "testproc",
304 * disk - Control suspend-to-disk mode
306 * Suspend-to-disk can be handled in several ways. The greatest
307 * distinction is who writes memory to disk - the firmware or the OS.
308 * If the firmware does it, we assume that it also handles suspending
310 * If the OS does it, then we have three options for putting the system
311 * to sleep - using the platform driver (e.g. ACPI or other PM registers),
312 * powering off the system or rebooting the system (for testing).
314 * The system will support either 'firmware' or 'platform', and that is
315 * known a priori (and encoded in pm_ops). But, the user may choose
316 * 'shutdown' or 'reboot' as alternatives.
318 * show() will display what the mode is currently set to.
319 * store() will accept one of
326 * It will only change to 'firmware' or 'platform' if the system
327 * supports it (as determined from pm_ops->pm_disk_mode).
330 static ssize_t disk_show(struct subsystem * subsys, char * buf)
332 return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]);
336 static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n)
342 suspend_disk_method_t mode = 0;
344 p = memchr(buf, '\n', n);
345 len = p ? p - buf : n;
347 mutex_lock(&pm_mutex);
348 for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) {
349 if (!strncmp(buf, pm_disk_modes[i], len)) {
356 case PM_DISK_SHUTDOWN:
359 case PM_DISK_TESTPROC:
363 if (pm_ops && pm_ops->enter &&
364 (mode == pm_ops->pm_disk_mode))
373 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
374 pm_disk_modes[mode]);
375 mutex_unlock(&pm_mutex);
376 return error ? error : n;
381 static ssize_t resume_show(struct subsystem * subsys, char *buf)
383 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
384 MINOR(swsusp_resume_device));
387 static ssize_t resume_store(struct subsystem *subsys, const char *buf, size_t n)
389 unsigned int maj, min;
393 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
396 res = MKDEV(maj,min);
397 if (maj != MAJOR(res) || min != MINOR(res))
400 mutex_lock(&pm_mutex);
401 swsusp_resume_device = res;
402 mutex_unlock(&pm_mutex);
403 printk("Attempting manual resume\n");
413 static ssize_t image_size_show(struct subsystem * subsys, char *buf)
415 return sprintf(buf, "%lu\n", image_size);
418 static ssize_t image_size_store(struct subsystem * subsys, const char * buf, size_t n)
422 if (sscanf(buf, "%lu", &size) == 1) {
430 power_attr(image_size);
432 static struct attribute * g[] = {
435 &image_size_attr.attr,
440 static struct attribute_group attr_group = {
445 static int __init pm_disk_init(void)
447 return sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
450 core_initcall(pm_disk_init);
453 static int __init resume_setup(char *str)
458 strncpy( resume_file, str, 255 );
462 static int __init resume_offset_setup(char *str)
464 unsigned long long offset;
469 if (sscanf(str, "%llu", &offset) == 1)
470 swsusp_resume_block = offset;
475 static int __init noresume_setup(char *str)
481 __setup("noresume", noresume_setup);
482 __setup("resume_offset=", resume_offset_setup);
483 __setup("resume=", resume_setup);