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;
41 __HIBERNATION_AFTER_LAST
43 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
44 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
46 static int hibernation_mode = HIBERNATION_SHUTDOWN;
48 struct hibernation_ops *hibernation_ops;
51 * hibernation_set_ops - set the global hibernate operations
52 * @ops: the hibernation operations to use in subsequent hibernation transitions
55 void hibernation_set_ops(struct hibernation_ops *ops)
57 if (ops && !(ops->prepare && ops->enter && ops->finish)) {
61 mutex_lock(&pm_mutex);
62 hibernation_ops = ops;
64 hibernation_mode = HIBERNATION_PLATFORM;
65 else if (hibernation_mode == HIBERNATION_PLATFORM)
66 hibernation_mode = HIBERNATION_SHUTDOWN;
68 mutex_unlock(&pm_mutex);
73 * platform_prepare - prepare the machine for hibernation using the
74 * platform driver if so configured and return an error code if it fails
77 static int platform_prepare(void)
79 return (hibernation_mode == HIBERNATION_PLATFORM && hibernation_ops) ?
80 hibernation_ops->prepare() : 0;
84 * platform_finish - switch the machine to the normal mode of operation
85 * using the platform driver (must be called after platform_prepare())
88 static void platform_finish(void)
90 if (hibernation_mode == HIBERNATION_PLATFORM && hibernation_ops)
91 hibernation_ops->finish();
95 * power_down - Shut the machine down for hibernation.
97 * Use the platform driver, if configured so; otherwise try
98 * to power off or reboot.
101 static void power_down(void)
103 switch (hibernation_mode) {
104 case HIBERNATION_TEST:
105 case HIBERNATION_TESTPROC:
107 case HIBERNATION_SHUTDOWN:
110 case HIBERNATION_REBOOT:
111 kernel_restart(NULL);
113 case HIBERNATION_PLATFORM:
114 if (hibernation_ops) {
115 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
116 hibernation_ops->enter();
122 * Valid image is on the disk, if we continue we risk serious data
123 * corruption after resume.
125 printk(KERN_CRIT "Please power me down manually\n");
129 static void unprepare_processes(void)
132 pm_restore_console();
135 static int prepare_processes(void)
139 pm_prepare_console();
140 if (freeze_processes()) {
142 unprepare_processes();
148 * hibernate - The granpappy of the built-in hibernation management
155 /* The snapshot device should not be opened while we're running */
156 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
159 /* Allocate memory management structures */
160 error = create_basic_memory_bitmaps();
164 error = prepare_processes();
168 mutex_lock(&pm_mutex);
169 if (hibernation_mode == HIBERNATION_TESTPROC) {
170 printk("swsusp debug: Waiting for 5 seconds.\n");
175 /* Free memory before shutting down devices. */
176 error = swsusp_shrink_memory();
180 error = platform_prepare();
185 error = device_suspend(PMSG_FREEZE);
187 printk(KERN_ERR "PM: Some devices failed to suspend\n");
190 error = disable_nonboot_cpus();
194 if (hibernation_mode == HIBERNATION_TEST) {
195 printk("swsusp debug: Waiting for 5 seconds.\n");
200 pr_debug("PM: snapshotting memory.\n");
202 error = swsusp_suspend();
207 enable_nonboot_cpus();
211 pr_debug("PM: writing image.\n");
212 error = swsusp_write();
220 pr_debug("PM: Image restored successfully.\n");
225 enable_nonboot_cpus();
231 mutex_unlock(&pm_mutex);
232 unprepare_processes();
234 free_basic_memory_bitmaps();
236 atomic_inc(&snapshot_device_available);
242 * software_resume - Resume from a saved image.
244 * Called as a late_initcall (so all devices are discovered and
245 * initialized), we call swsusp to see if we have a saved image or not.
246 * If so, we quiesce devices, the restore the saved image. We will
247 * return above (in hibernate() ) if everything goes well.
248 * Otherwise, we fail gracefully and return to the normally
253 static int software_resume(void)
257 mutex_lock(&pm_mutex);
258 if (!swsusp_resume_device) {
259 if (!strlen(resume_file)) {
260 mutex_unlock(&pm_mutex);
263 swsusp_resume_device = name_to_dev_t(resume_file);
264 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
266 pr_debug("swsusp: Resume From Partition %d:%d\n",
267 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
272 * FIXME: If noresume is specified, we need to find the partition
273 * and reset it back to normal swap space.
275 mutex_unlock(&pm_mutex);
279 pr_debug("PM: Checking swsusp image.\n");
280 error = swsusp_check();
284 /* The snapshot device should not be opened while we're running */
285 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
290 error = create_basic_memory_bitmaps();
294 pr_debug("PM: Preparing processes for restore.\n");
295 error = prepare_processes();
301 pr_debug("PM: Reading swsusp image.\n");
303 error = swsusp_read();
309 pr_debug("PM: Preparing devices for restore.\n");
312 error = device_suspend(PMSG_PRETHAW);
316 error = disable_nonboot_cpus();
320 enable_nonboot_cpus();
326 printk(KERN_ERR "PM: Restore failed, recovering.\n");
327 unprepare_processes();
329 free_basic_memory_bitmaps();
331 atomic_inc(&snapshot_device_available);
332 /* For success case, the suspend path will release the lock */
334 mutex_unlock(&pm_mutex);
335 pr_debug("PM: Resume from disk failed.\n");
339 late_initcall(software_resume);
342 static const char * const hibernation_modes[] = {
343 [HIBERNATION_PLATFORM] = "platform",
344 [HIBERNATION_SHUTDOWN] = "shutdown",
345 [HIBERNATION_REBOOT] = "reboot",
346 [HIBERNATION_TEST] = "test",
347 [HIBERNATION_TESTPROC] = "testproc",
351 * disk - Control hibernation mode
353 * Suspend-to-disk can be handled in several ways. We have a few options
354 * for putting the system to sleep - using the platform driver (e.g. ACPI
355 * or other hibernation_ops), powering off the system or rebooting the
356 * system (for testing) as well as the two test modes.
358 * The system can support 'platform', and that is known a priori (and
359 * encoded by the presence of hibernation_ops). However, the user may
360 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
361 * test modes, 'test' or 'testproc'.
363 * show() will display what the mode is currently set to.
364 * store() will accept one of
372 * It will only change to 'platform' if the system
373 * supports it (as determined by having hibernation_ops).
376 static ssize_t disk_show(struct kset *kset, char *buf)
381 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
382 if (!hibernation_modes[i])
385 case HIBERNATION_SHUTDOWN:
386 case HIBERNATION_REBOOT:
387 case HIBERNATION_TEST:
388 case HIBERNATION_TESTPROC:
390 case HIBERNATION_PLATFORM:
393 /* not a valid mode, continue with loop */
396 if (i == hibernation_mode)
397 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
399 buf += sprintf(buf, "%s ", hibernation_modes[i]);
401 buf += sprintf(buf, "\n");
406 static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
412 int mode = HIBERNATION_INVALID;
414 p = memchr(buf, '\n', n);
415 len = p ? p - buf : n;
417 mutex_lock(&pm_mutex);
418 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
419 if (len == strlen(hibernation_modes[i])
420 && !strncmp(buf, hibernation_modes[i], len)) {
425 if (mode != HIBERNATION_INVALID) {
427 case HIBERNATION_SHUTDOWN:
428 case HIBERNATION_REBOOT:
429 case HIBERNATION_TEST:
430 case HIBERNATION_TESTPROC:
431 hibernation_mode = mode;
433 case HIBERNATION_PLATFORM:
435 hibernation_mode = mode;
443 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
444 hibernation_modes[mode]);
445 mutex_unlock(&pm_mutex);
446 return error ? error : n;
451 static ssize_t resume_show(struct kset *kset, char *buf)
453 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
454 MINOR(swsusp_resume_device));
457 static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
459 unsigned int maj, min;
463 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
466 res = MKDEV(maj,min);
467 if (maj != MAJOR(res) || min != MINOR(res))
470 mutex_lock(&pm_mutex);
471 swsusp_resume_device = res;
472 mutex_unlock(&pm_mutex);
473 printk("Attempting manual resume\n");
483 static ssize_t image_size_show(struct kset *kset, char *buf)
485 return sprintf(buf, "%lu\n", image_size);
488 static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
492 if (sscanf(buf, "%lu", &size) == 1) {
500 power_attr(image_size);
502 static struct attribute * g[] = {
505 &image_size_attr.attr,
510 static struct attribute_group attr_group = {
515 static int __init pm_disk_init(void)
517 return sysfs_create_group(&power_subsys.kobj, &attr_group);
520 core_initcall(pm_disk_init);
523 static int __init resume_setup(char *str)
528 strncpy( resume_file, str, 255 );
532 static int __init resume_offset_setup(char *str)
534 unsigned long long offset;
539 if (sscanf(str, "%llu", &offset) == 1)
540 swsusp_resume_block = offset;
545 static int __init noresume_setup(char *str)
551 __setup("noresume", noresume_setup);
552 __setup("resume_offset=", resume_offset_setup);
553 __setup("resume=", resume_setup);