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/kmod.h>
18 #include <linux/delay.h>
20 #include <linux/mount.h>
22 #include <linux/console.h>
23 #include <linux/cpu.h>
24 #include <linux/freezer.h>
25 #include <scsi/scsi_scan.h>
26 #include <asm/suspend.h>
31 static int noresume = 0;
32 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
33 dev_t swsusp_resume_device;
34 sector_t swsusp_resume_block;
44 __HIBERNATION_AFTER_LAST
46 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
47 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
49 static int hibernation_mode = HIBERNATION_SHUTDOWN;
51 static struct platform_hibernation_ops *hibernation_ops;
54 * hibernation_set_ops - set the global hibernate operations
55 * @ops: the hibernation operations to use in subsequent hibernation transitions
58 void hibernation_set_ops(struct platform_hibernation_ops *ops)
60 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
61 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
62 && ops->restore_cleanup)) {
66 mutex_lock(&pm_mutex);
67 hibernation_ops = ops;
69 hibernation_mode = HIBERNATION_PLATFORM;
70 else if (hibernation_mode == HIBERNATION_PLATFORM)
71 hibernation_mode = HIBERNATION_SHUTDOWN;
73 mutex_unlock(&pm_mutex);
76 static bool entering_platform_hibernation;
78 bool system_entering_hibernation(void)
80 return entering_platform_hibernation;
82 EXPORT_SYMBOL(system_entering_hibernation);
84 #ifdef CONFIG_PM_DEBUG
85 static void hibernation_debug_sleep(void)
87 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
91 static int hibernation_testmode(int mode)
93 if (hibernation_mode == mode) {
94 hibernation_debug_sleep();
100 static int hibernation_test(int level)
102 if (pm_test_level == level) {
103 hibernation_debug_sleep();
108 #else /* !CONFIG_PM_DEBUG */
109 static int hibernation_testmode(int mode) { return 0; }
110 static int hibernation_test(int level) { return 0; }
111 #endif /* !CONFIG_PM_DEBUG */
114 * platform_begin - tell the platform driver that we're starting
118 static int platform_begin(int platform_mode)
120 return (platform_mode && hibernation_ops) ?
121 hibernation_ops->begin() : 0;
125 * platform_end - tell the platform driver that we've entered the
129 static void platform_end(int platform_mode)
131 if (platform_mode && hibernation_ops)
132 hibernation_ops->end();
136 * platform_pre_snapshot - prepare the machine for hibernation using the
137 * platform driver if so configured and return an error code if it fails
140 static int platform_pre_snapshot(int platform_mode)
142 return (platform_mode && hibernation_ops) ?
143 hibernation_ops->pre_snapshot() : 0;
147 * platform_leave - prepare the machine for switching to the normal mode
148 * of operation using the platform driver (called with interrupts disabled)
151 static void platform_leave(int platform_mode)
153 if (platform_mode && hibernation_ops)
154 hibernation_ops->leave();
158 * platform_finish - switch the machine to the normal mode of operation
159 * using the platform driver (must be called after platform_prepare())
162 static void platform_finish(int platform_mode)
164 if (platform_mode && hibernation_ops)
165 hibernation_ops->finish();
169 * platform_pre_restore - prepare the platform for the restoration from a
170 * hibernation image. If the restore fails after this function has been
171 * called, platform_restore_cleanup() must be called.
174 static int platform_pre_restore(int platform_mode)
176 return (platform_mode && hibernation_ops) ?
177 hibernation_ops->pre_restore() : 0;
181 * platform_restore_cleanup - switch the platform to the normal mode of
182 * operation after a failing restore. If platform_pre_restore() has been
183 * called before the failing restore, this function must be called too,
184 * regardless of the result of platform_pre_restore().
187 static void platform_restore_cleanup(int platform_mode)
189 if (platform_mode && hibernation_ops)
190 hibernation_ops->restore_cleanup();
194 * platform_recover - recover the platform from a failure to suspend
198 static void platform_recover(int platform_mode)
200 if (platform_mode && hibernation_ops && hibernation_ops->recover)
201 hibernation_ops->recover();
205 * create_image - freeze devices that need to be frozen with interrupts
206 * off, create the hibernation image and thaw those devices. Control
207 * reappears in this routine after a restore.
210 static int create_image(int platform_mode)
214 error = arch_prepare_suspend();
220 /* At this point, device_suspend() has been called, but *not*
221 * device_power_down(). We *must* call device_power_down() now.
222 * Otherwise, drivers for some devices (e.g. interrupt controllers)
223 * become desynchronized with the actual state of the hardware
224 * at resume time, and evil weirdness ensues.
226 error = device_power_down(PMSG_FREEZE);
228 printk(KERN_ERR "PM: Some devices failed to power down, "
229 "aborting hibernation\n");
233 error = platform_pre_snapshot(platform_mode);
234 if (error || hibernation_test(TEST_PLATFORM))
235 goto Platform_finish;
237 error = disable_nonboot_cpus();
238 if (error || hibernation_test(TEST_CPUS)
239 || hibernation_testmode(HIBERNATION_TEST))
244 sysdev_suspend(PMSG_FREEZE);
246 printk(KERN_ERR "PM: Some devices failed to power down, "
247 "aborting hibernation\n");
251 if (hibernation_test(TEST_CORE))
255 save_processor_state();
256 error = swsusp_arch_suspend();
258 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
260 /* Restore control flow magically appears here */
261 restore_processor_state();
263 platform_leave(platform_mode);
267 /* NOTE: device_power_up() is just a resume() for devices
268 * that suspended with irqs off ... no overall powerup.
275 enable_nonboot_cpus();
278 platform_finish(platform_mode);
280 device_power_up(in_suspend ?
281 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
290 * hibernation_snapshot - quiesce devices and create the hibernation
292 * @platform_mode - if set, use the platform driver, if available, to
293 * prepare the platform firmware for the power transition.
295 * Must be called with pm_mutex held
298 int hibernation_snapshot(int platform_mode)
302 error = platform_begin(platform_mode);
306 /* Free memory before shutting down devices. */
307 error = swsusp_shrink_memory();
312 error = device_suspend(PMSG_FREEZE);
314 goto Recover_platform;
316 if (hibernation_test(TEST_DEVICES))
317 goto Recover_platform;
319 error = create_image(platform_mode);
320 /* Control returns here after successful restore */
323 device_resume(in_suspend ?
324 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
327 platform_end(platform_mode);
331 platform_recover(platform_mode);
336 * resume_target_kernel - prepare devices that need to be suspended with
337 * interrupts off, restore the contents of highmem that have not been
338 * restored yet from the image and run the low level code that will restore
339 * the remaining contents of memory and switch to the just restored target
343 static int resume_target_kernel(bool platform_mode)
349 error = device_power_down(PMSG_QUIESCE);
351 printk(KERN_ERR "PM: Some devices failed to power down, "
352 "aborting resume\n");
356 error = platform_pre_restore(platform_mode);
360 error = disable_nonboot_cpus();
366 error = sysdev_suspend(PMSG_QUIESCE);
370 /* We'll ignore saved state, but this gets preempt count (etc) right */
371 save_processor_state();
372 error = restore_highmem();
374 error = swsusp_arch_resume();
376 * The code below is only ever reached in case of a failure.
377 * Otherwise execution continues at place where
378 * swsusp_arch_suspend() was called
381 /* This call to restore_highmem() undos the previous one */
385 * The only reason why swsusp_arch_resume() can fail is memory being
386 * very tight, so we have to free it as soon as we can to avoid
387 * subsequent failures
390 restore_processor_state();
391 touch_softlockup_watchdog();
399 enable_nonboot_cpus();
402 platform_restore_cleanup(platform_mode);
404 device_power_up(PMSG_RECOVER);
413 * hibernation_restore - quiesce devices and restore the hibernation
414 * snapshot image. If successful, control returns in hibernation_snaphot()
415 * @platform_mode - if set, use the platform driver, if available, to
416 * prepare the platform firmware for the transition.
418 * Must be called with pm_mutex held
421 int hibernation_restore(int platform_mode)
425 pm_prepare_console();
427 error = device_suspend(PMSG_QUIESCE);
429 error = resume_target_kernel(platform_mode);
430 device_resume(PMSG_RECOVER);
433 pm_restore_console();
438 * hibernation_platform_enter - enter the hibernation state using the
439 * platform driver (if available)
442 int hibernation_platform_enter(void)
446 if (!hibernation_ops)
450 * We have cancelled the power transition by running
451 * hibernation_ops->finish() before saving the image, so we should let
452 * the firmware know that we're going to enter the sleep state after all
454 error = hibernation_ops->begin();
458 entering_platform_hibernation = true;
460 error = device_suspend(PMSG_HIBERNATE);
462 if (hibernation_ops->recover)
463 hibernation_ops->recover();
469 error = device_power_down(PMSG_HIBERNATE);
473 error = hibernation_ops->prepare();
475 goto Platofrm_finish;
477 error = disable_nonboot_cpus();
479 goto Platofrm_finish;
482 sysdev_suspend(PMSG_HIBERNATE);
483 hibernation_ops->enter();
484 /* We should never get here */
488 * We don't need to reenable the nonboot CPUs or resume consoles, since
489 * the system is going to be halted anyway.
492 hibernation_ops->finish();
494 device_power_up(PMSG_RESTORE);
500 entering_platform_hibernation = false;
501 device_resume(PMSG_RESTORE);
505 hibernation_ops->end();
511 * power_down - Shut the machine down for hibernation.
513 * Use the platform driver, if configured so; otherwise try
514 * to power off or reboot.
517 static void power_down(void)
519 switch (hibernation_mode) {
520 case HIBERNATION_TEST:
521 case HIBERNATION_TESTPROC:
523 case HIBERNATION_REBOOT:
524 kernel_restart(NULL);
526 case HIBERNATION_PLATFORM:
527 hibernation_platform_enter();
528 case HIBERNATION_SHUTDOWN:
534 * Valid image is on the disk, if we continue we risk serious data
535 * corruption after resume.
537 printk(KERN_CRIT "PM: Please power down manually\n");
541 static int prepare_processes(void)
545 if (freeze_processes()) {
553 * hibernate - The granpappy of the built-in hibernation management
560 mutex_lock(&pm_mutex);
561 /* The snapshot device should not be opened while we're running */
562 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
567 pm_prepare_console();
568 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
572 error = usermodehelper_disable();
576 /* Allocate memory management structures */
577 error = create_basic_memory_bitmaps();
581 printk(KERN_INFO "PM: Syncing filesystems ... ");
585 error = prepare_processes();
589 if (hibernation_test(TEST_FREEZER))
592 if (hibernation_testmode(HIBERNATION_TESTPROC))
595 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
596 if (in_suspend && !error) {
597 unsigned int flags = 0;
599 if (hibernation_mode == HIBERNATION_PLATFORM)
600 flags |= SF_PLATFORM_MODE;
601 pr_debug("PM: writing image.\n");
602 error = swsusp_write(flags);
607 pr_debug("PM: Image restored successfully.\n");
613 free_basic_memory_bitmaps();
614 usermodehelper_enable();
616 pm_notifier_call_chain(PM_POST_HIBERNATION);
617 pm_restore_console();
618 atomic_inc(&snapshot_device_available);
620 mutex_unlock(&pm_mutex);
626 * software_resume - Resume from a saved image.
628 * Called as a late_initcall (so all devices are discovered and
629 * initialized), we call swsusp to see if we have a saved image or not.
630 * If so, we quiesce devices, the restore the saved image. We will
631 * return above (in hibernate() ) if everything goes well.
632 * Otherwise, we fail gracefully and return to the normally
637 static int software_resume(void)
643 * If the user said "noresume".. bail out early.
649 * We can't depend on SCSI devices being available after loading one of
650 * their modules if scsi_complete_async_scans() is not called and the
651 * resume device usually is a SCSI one.
653 scsi_complete_async_scans();
656 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
657 * is configured into the kernel. Since the regular hibernate
658 * trigger path is via sysfs which takes a buffer mutex before
659 * calling hibernate functions (which take pm_mutex) this can
660 * cause lockdep to complain about a possible ABBA deadlock
661 * which cannot happen since we're in the boot code here and
662 * sysfs can't be invoked yet. Therefore, we use a subclass
663 * here to avoid lockdep complaining.
665 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
666 if (!swsusp_resume_device) {
667 if (!strlen(resume_file)) {
668 mutex_unlock(&pm_mutex);
672 * Some device discovery might still be in progress; we need
673 * to wait for this to finish.
675 wait_for_device_probe();
676 swsusp_resume_device = name_to_dev_t(resume_file);
677 pr_debug("PM: Resume from partition %s\n", resume_file);
679 pr_debug("PM: Resume from partition %d:%d\n",
680 MAJOR(swsusp_resume_device),
681 MINOR(swsusp_resume_device));
686 * FIXME: If noresume is specified, we need to find the
687 * partition and reset it back to normal swap space.
689 mutex_unlock(&pm_mutex);
693 pr_debug("PM: Checking hibernation image.\n");
694 error = swsusp_check();
698 /* The snapshot device should not be opened while we're running */
699 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
704 pm_prepare_console();
705 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
709 error = usermodehelper_disable();
713 error = create_basic_memory_bitmaps();
717 pr_debug("PM: Preparing processes for restore.\n");
718 error = prepare_processes();
720 swsusp_close(FMODE_READ);
724 pr_debug("PM: Reading hibernation image.\n");
726 error = swsusp_read(&flags);
728 hibernation_restore(flags & SF_PLATFORM_MODE);
730 printk(KERN_ERR "PM: Restore failed, recovering.\n");
734 free_basic_memory_bitmaps();
735 usermodehelper_enable();
737 pm_notifier_call_chain(PM_POST_RESTORE);
738 pm_restore_console();
739 atomic_inc(&snapshot_device_available);
740 /* For success case, the suspend path will release the lock */
742 mutex_unlock(&pm_mutex);
743 pr_debug("PM: Resume from disk failed.\n");
747 late_initcall(software_resume);
750 static const char * const hibernation_modes[] = {
751 [HIBERNATION_PLATFORM] = "platform",
752 [HIBERNATION_SHUTDOWN] = "shutdown",
753 [HIBERNATION_REBOOT] = "reboot",
754 [HIBERNATION_TEST] = "test",
755 [HIBERNATION_TESTPROC] = "testproc",
759 * disk - Control hibernation mode
761 * Suspend-to-disk can be handled in several ways. We have a few options
762 * for putting the system to sleep - using the platform driver (e.g. ACPI
763 * or other hibernation_ops), powering off the system or rebooting the
764 * system (for testing) as well as the two test modes.
766 * The system can support 'platform', and that is known a priori (and
767 * encoded by the presence of hibernation_ops). However, the user may
768 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
769 * test modes, 'test' or 'testproc'.
771 * show() will display what the mode is currently set to.
772 * store() will accept one of
780 * It will only change to 'platform' if the system
781 * supports it (as determined by having hibernation_ops).
784 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
790 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
791 if (!hibernation_modes[i])
794 case HIBERNATION_SHUTDOWN:
795 case HIBERNATION_REBOOT:
796 case HIBERNATION_TEST:
797 case HIBERNATION_TESTPROC:
799 case HIBERNATION_PLATFORM:
802 /* not a valid mode, continue with loop */
805 if (i == hibernation_mode)
806 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
808 buf += sprintf(buf, "%s ", hibernation_modes[i]);
810 buf += sprintf(buf, "\n");
815 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
816 const char *buf, size_t n)
822 int mode = HIBERNATION_INVALID;
824 p = memchr(buf, '\n', n);
825 len = p ? p - buf : n;
827 mutex_lock(&pm_mutex);
828 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
829 if (len == strlen(hibernation_modes[i])
830 && !strncmp(buf, hibernation_modes[i], len)) {
835 if (mode != HIBERNATION_INVALID) {
837 case HIBERNATION_SHUTDOWN:
838 case HIBERNATION_REBOOT:
839 case HIBERNATION_TEST:
840 case HIBERNATION_TESTPROC:
841 hibernation_mode = mode;
843 case HIBERNATION_PLATFORM:
845 hibernation_mode = mode;
853 pr_debug("PM: Hibernation mode set to '%s'\n",
854 hibernation_modes[mode]);
855 mutex_unlock(&pm_mutex);
856 return error ? error : n;
861 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
864 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
865 MINOR(swsusp_resume_device));
868 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
869 const char *buf, size_t n)
871 unsigned int maj, min;
875 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
878 res = MKDEV(maj,min);
879 if (maj != MAJOR(res) || min != MINOR(res))
882 mutex_lock(&pm_mutex);
883 swsusp_resume_device = res;
884 mutex_unlock(&pm_mutex);
885 printk(KERN_INFO "PM: Starting manual resume from disk\n");
895 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
898 return sprintf(buf, "%lu\n", image_size);
901 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
902 const char *buf, size_t n)
906 if (sscanf(buf, "%lu", &size) == 1) {
914 power_attr(image_size);
916 static struct attribute * g[] = {
919 &image_size_attr.attr,
924 static struct attribute_group attr_group = {
929 static int __init pm_disk_init(void)
931 return sysfs_create_group(power_kobj, &attr_group);
934 core_initcall(pm_disk_init);
937 static int __init resume_setup(char *str)
942 strncpy( resume_file, str, 255 );
946 static int __init resume_offset_setup(char *str)
948 unsigned long long offset;
953 if (sscanf(str, "%llu", &offset) == 1)
954 swsusp_resume_block = offset;
959 static int __init noresume_setup(char *str)
965 __setup("noresume", noresume_setup);
966 __setup("resume_offset=", resume_offset_setup);
967 __setup("resume=", resume_setup);