2 * kernel/power/hibernate.c - Hibernation (a.k.a 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@ucw.cz>
7 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
9 * This file is released under the GPLv2.
12 #include <linux/export.h>
13 #include <linux/suspend.h>
14 #include <linux/syscalls.h>
15 #include <linux/reboot.h>
16 #include <linux/string.h>
17 #include <linux/device.h>
18 #include <linux/async.h>
19 #include <linux/kmod.h>
20 #include <linux/delay.h>
22 #include <linux/mount.h>
24 #include <linux/console.h>
25 #include <linux/cpu.h>
26 #include <linux/freezer.h>
27 #include <linux/gfp.h>
28 #include <linux/syscore_ops.h>
29 #include <scsi/scsi_scan.h>
34 static int nocompress;
36 static int resume_wait;
37 static int resume_delay;
38 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
39 dev_t swsusp_resume_device;
40 sector_t swsusp_resume_block;
41 int in_suspend __nosavedata;
51 __HIBERNATION_AFTER_LAST
53 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
54 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
56 static int hibernation_mode = HIBERNATION_SHUTDOWN;
58 static bool freezer_test_done;
60 static const struct platform_hibernation_ops *hibernation_ops;
63 * hibernation_set_ops - Set the global hibernate operations.
64 * @ops: Hibernation operations to use in subsequent hibernation transitions.
66 void hibernation_set_ops(const struct platform_hibernation_ops *ops)
68 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
69 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
70 && ops->restore_cleanup && ops->leave)) {
74 mutex_lock(&pm_mutex);
75 hibernation_ops = ops;
77 hibernation_mode = HIBERNATION_PLATFORM;
78 else if (hibernation_mode == HIBERNATION_PLATFORM)
79 hibernation_mode = HIBERNATION_SHUTDOWN;
81 mutex_unlock(&pm_mutex);
84 static bool entering_platform_hibernation;
86 bool system_entering_hibernation(void)
88 return entering_platform_hibernation;
90 EXPORT_SYMBOL(system_entering_hibernation);
92 #ifdef CONFIG_PM_DEBUG
93 static void hibernation_debug_sleep(void)
95 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
99 static int hibernation_testmode(int mode)
101 if (hibernation_mode == mode) {
102 hibernation_debug_sleep();
108 static int hibernation_test(int level)
110 if (pm_test_level == level) {
111 hibernation_debug_sleep();
116 #else /* !CONFIG_PM_DEBUG */
117 static int hibernation_testmode(int mode) { return 0; }
118 static int hibernation_test(int level) { return 0; }
119 #endif /* !CONFIG_PM_DEBUG */
122 * platform_begin - Call platform to start hibernation.
123 * @platform_mode: Whether or not to use the platform driver.
125 static int platform_begin(int platform_mode)
127 return (platform_mode && hibernation_ops) ?
128 hibernation_ops->begin() : 0;
132 * platform_end - Call platform to finish transition to the working state.
133 * @platform_mode: Whether or not to use the platform driver.
135 static void platform_end(int platform_mode)
137 if (platform_mode && hibernation_ops)
138 hibernation_ops->end();
142 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
143 * @platform_mode: Whether or not to use the platform driver.
145 * Use the platform driver to prepare the system for creating a hibernate image,
146 * if so configured, and return an error code if that fails.
149 static int platform_pre_snapshot(int platform_mode)
151 return (platform_mode && hibernation_ops) ?
152 hibernation_ops->pre_snapshot() : 0;
156 * platform_leave - Call platform to prepare a transition to the working state.
157 * @platform_mode: Whether or not to use the platform driver.
159 * Use the platform driver prepare to prepare the machine for switching to the
160 * normal mode of operation.
162 * This routine is called on one CPU with interrupts disabled.
164 static void platform_leave(int platform_mode)
166 if (platform_mode && hibernation_ops)
167 hibernation_ops->leave();
171 * platform_finish - Call platform to switch the system to the working state.
172 * @platform_mode: Whether or not to use the platform driver.
174 * Use the platform driver to switch the machine to the normal mode of
177 * This routine must be called after platform_prepare().
179 static void platform_finish(int platform_mode)
181 if (platform_mode && hibernation_ops)
182 hibernation_ops->finish();
186 * platform_pre_restore - Prepare for hibernate image restoration.
187 * @platform_mode: Whether or not to use the platform driver.
189 * Use the platform driver to prepare the system for resume from a hibernation
192 * If the restore fails after this function has been called,
193 * platform_restore_cleanup() must be called.
195 static int platform_pre_restore(int platform_mode)
197 return (platform_mode && hibernation_ops) ?
198 hibernation_ops->pre_restore() : 0;
202 * platform_restore_cleanup - Switch to the working state after failing restore.
203 * @platform_mode: Whether or not to use the platform driver.
205 * Use the platform driver to switch the system to the normal mode of operation
206 * after a failing restore.
208 * If platform_pre_restore() has been called before the failing restore, this
209 * function must be called too, regardless of the result of
210 * platform_pre_restore().
212 static void platform_restore_cleanup(int platform_mode)
214 if (platform_mode && hibernation_ops)
215 hibernation_ops->restore_cleanup();
219 * platform_recover - Recover from a failure to suspend devices.
220 * @platform_mode: Whether or not to use the platform driver.
222 static void platform_recover(int platform_mode)
224 if (platform_mode && hibernation_ops && hibernation_ops->recover)
225 hibernation_ops->recover();
229 * swsusp_show_speed - Print time elapsed between two events during hibernation.
230 * @start: Starting event.
231 * @stop: Final event.
232 * @nr_pages: Number of memory pages processed between @start and @stop.
233 * @msg: Additional diagnostic message to print.
235 void swsusp_show_speed(struct timeval *start, struct timeval *stop,
236 unsigned nr_pages, char *msg)
238 s64 elapsed_centisecs64;
243 elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
244 do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
245 centisecs = elapsed_centisecs64;
247 centisecs = 1; /* avoid div-by-zero */
248 k = nr_pages * (PAGE_SIZE / 1024);
249 kps = (k * 100) / centisecs;
250 printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
252 centisecs / 100, centisecs % 100,
253 kps / 1000, (kps % 1000) / 10);
257 * create_image - Create a hibernation image.
258 * @platform_mode: Whether or not to use the platform driver.
260 * Execute device drivers' .freeze_noirq() callbacks, create a hibernation image
261 * and execute the drivers' .thaw_noirq() callbacks.
263 * Control reappears in this routine after the subsequent restore.
265 static int create_image(int platform_mode)
269 error = dpm_suspend_noirq(PMSG_FREEZE);
271 printk(KERN_ERR "PM: Some devices failed to power down, "
272 "aborting hibernation\n");
276 error = platform_pre_snapshot(platform_mode);
277 if (error || hibernation_test(TEST_PLATFORM))
278 goto Platform_finish;
280 error = disable_nonboot_cpus();
281 if (error || hibernation_test(TEST_CPUS)
282 || hibernation_testmode(HIBERNATION_TEST))
287 error = syscore_suspend();
289 printk(KERN_ERR "PM: Some system devices failed to power down, "
290 "aborting hibernation\n");
294 if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
298 save_processor_state();
299 error = swsusp_arch_suspend();
301 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
303 /* Restore control flow magically appears here */
304 restore_processor_state();
306 events_check_enabled = false;
307 platform_leave(platform_mode);
317 enable_nonboot_cpus();
320 platform_finish(platform_mode);
322 dpm_resume_noirq(in_suspend ?
323 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
329 * hibernation_snapshot - Quiesce devices and create a hibernation image.
330 * @platform_mode: If set, use platform driver to prepare for the transition.
332 * This routine must be called with pm_mutex held.
334 int hibernation_snapshot(int platform_mode)
336 pm_message_t msg = PMSG_RECOVER;
339 error = platform_begin(platform_mode);
343 /* Preallocate image memory before shutting down devices. */
344 error = hibernate_preallocate_memory();
348 error = freeze_kernel_threads();
352 if (hibernation_test(TEST_FREEZER) ||
353 hibernation_testmode(HIBERNATION_TESTPROC)) {
356 * Indicate to the caller that we are returning due to a
357 * successful freezer test.
359 freezer_test_done = true;
363 error = dpm_prepare(PMSG_FREEZE);
365 goto Complete_devices;
368 pm_restrict_gfp_mask();
369 error = dpm_suspend(PMSG_FREEZE);
371 goto Recover_platform;
373 if (hibernation_test(TEST_DEVICES))
374 goto Recover_platform;
376 error = create_image(platform_mode);
378 * Control returns here (1) after the image has been created or the
379 * image creation has failed and (2) after a successful restore.
383 /* We may need to release the preallocated image pages here. */
384 if (error || !in_suspend)
387 msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
390 if (error || !in_suspend)
391 pm_restore_gfp_mask();
399 platform_end(platform_mode);
403 platform_recover(platform_mode);
408 * resume_target_kernel - Restore system state from a hibernation image.
409 * @platform_mode: Whether or not to use the platform driver.
411 * Execute device drivers' .freeze_noirq() callbacks, restore the contents of
412 * highmem that have not been restored yet from the image and run the low-level
413 * code that will restore the remaining contents of memory and switch to the
414 * just restored target kernel.
416 static int resume_target_kernel(bool platform_mode)
420 error = dpm_suspend_noirq(PMSG_QUIESCE);
422 printk(KERN_ERR "PM: Some devices failed to power down, "
423 "aborting resume\n");
427 error = platform_pre_restore(platform_mode);
431 error = disable_nonboot_cpus();
437 error = syscore_suspend();
441 save_processor_state();
442 error = restore_highmem();
444 error = swsusp_arch_resume();
446 * The code below is only ever reached in case of a failure.
447 * Otherwise, execution continues at the place where
448 * swsusp_arch_suspend() was called.
452 * This call to restore_highmem() reverts the changes made by
458 * The only reason why swsusp_arch_resume() can fail is memory being
459 * very tight, so we have to free it as soon as we can to avoid
460 * subsequent failures.
463 restore_processor_state();
464 touch_softlockup_watchdog();
472 enable_nonboot_cpus();
475 platform_restore_cleanup(platform_mode);
477 dpm_resume_noirq(PMSG_RECOVER);
483 * hibernation_restore - Quiesce devices and restore from a hibernation image.
484 * @platform_mode: If set, use platform driver to prepare for the transition.
486 * This routine must be called with pm_mutex held. If it is successful, control
487 * reappears in the restored target kernel in hibernation_snapshot().
489 int hibernation_restore(int platform_mode)
493 pm_prepare_console();
495 pm_restrict_gfp_mask();
496 error = dpm_suspend_start(PMSG_QUIESCE);
498 error = resume_target_kernel(platform_mode);
499 dpm_resume_end(PMSG_RECOVER);
501 pm_restore_gfp_mask();
503 pm_restore_console();
508 * hibernation_platform_enter - Power off the system using the platform driver.
510 int hibernation_platform_enter(void)
514 if (!hibernation_ops)
518 * We have cancelled the power transition by running
519 * hibernation_ops->finish() before saving the image, so we should let
520 * the firmware know that we're going to enter the sleep state after all
522 error = hibernation_ops->begin();
526 entering_platform_hibernation = true;
528 error = dpm_suspend_start(PMSG_HIBERNATE);
530 if (hibernation_ops->recover)
531 hibernation_ops->recover();
535 error = dpm_suspend_noirq(PMSG_HIBERNATE);
539 error = hibernation_ops->prepare();
541 goto Platform_finish;
543 error = disable_nonboot_cpus();
545 goto Platform_finish;
549 if (pm_wakeup_pending()) {
554 hibernation_ops->enter();
555 /* We should never get here */
561 enable_nonboot_cpus();
564 hibernation_ops->finish();
566 dpm_resume_noirq(PMSG_RESTORE);
569 entering_platform_hibernation = false;
570 dpm_resume_end(PMSG_RESTORE);
574 hibernation_ops->end();
580 * power_down - Shut the machine down for hibernation.
582 * Use the platform driver, if configured, to put the system into the sleep
583 * state corresponding to hibernation, or try to power it off or reboot,
584 * depending on the value of hibernation_mode.
586 static void power_down(void)
588 switch (hibernation_mode) {
589 case HIBERNATION_TEST:
590 case HIBERNATION_TESTPROC:
592 case HIBERNATION_REBOOT:
593 kernel_restart(NULL);
595 case HIBERNATION_PLATFORM:
596 hibernation_platform_enter();
597 case HIBERNATION_SHUTDOWN:
603 * Valid image is on the disk, if we continue we risk serious data
604 * corruption after resume.
606 printk(KERN_CRIT "PM: Please power down manually\n");
610 static int prepare_processes(void)
614 if (freeze_processes()) {
622 * hibernate - Carry out system hibernation, including saving the image.
628 mutex_lock(&pm_mutex);
629 /* The snapshot device should not be opened while we're running */
630 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
635 pm_prepare_console();
636 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
640 error = usermodehelper_disable();
644 /* Allocate memory management structures */
645 error = create_basic_memory_bitmaps();
649 printk(KERN_INFO "PM: Syncing filesystems ... ");
653 error = prepare_processes();
657 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
660 if (freezer_test_done) {
661 freezer_test_done = false;
666 unsigned int flags = 0;
668 if (hibernation_mode == HIBERNATION_PLATFORM)
669 flags |= SF_PLATFORM_MODE;
671 flags |= SF_NOCOMPRESS_MODE;
673 flags |= SF_CRC32_MODE;
675 pr_debug("PM: writing image.\n");
676 error = swsusp_write(flags);
681 pm_restore_gfp_mask();
683 pr_debug("PM: Image restored successfully.\n");
689 free_basic_memory_bitmaps();
690 usermodehelper_enable();
692 pm_notifier_call_chain(PM_POST_HIBERNATION);
693 pm_restore_console();
694 atomic_inc(&snapshot_device_available);
696 mutex_unlock(&pm_mutex);
702 * software_resume - Resume from a saved hibernation image.
704 * This routine is called as a late initcall, when all devices have been
705 * discovered and initialized already.
707 * The image reading code is called to see if there is a hibernation image
708 * available for reading. If that is the case, devices are quiesced and the
709 * contents of memory is restored from the saved image.
711 * If this is successful, control reappears in the restored target kernel in
712 * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
713 * attempts to recover gracefully and make the kernel return to the normal mode
716 static int software_resume(void)
722 * If the user said "noresume".. bail out early.
728 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
729 * is configured into the kernel. Since the regular hibernate
730 * trigger path is via sysfs which takes a buffer mutex before
731 * calling hibernate functions (which take pm_mutex) this can
732 * cause lockdep to complain about a possible ABBA deadlock
733 * which cannot happen since we're in the boot code here and
734 * sysfs can't be invoked yet. Therefore, we use a subclass
735 * here to avoid lockdep complaining.
737 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
739 if (swsusp_resume_device)
742 if (!strlen(resume_file)) {
747 pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
750 printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
752 ssleep(resume_delay);
755 /* Check if the device is there */
756 swsusp_resume_device = name_to_dev_t(resume_file);
757 if (!swsusp_resume_device) {
759 * Some device discovery might still be in progress; we need
760 * to wait for this to finish.
762 wait_for_device_probe();
765 while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
767 async_synchronize_full();
771 * We can't depend on SCSI devices being available after loading
772 * one of their modules until scsi_complete_async_scans() is
773 * called and the resume device usually is a SCSI one.
775 scsi_complete_async_scans();
777 swsusp_resume_device = name_to_dev_t(resume_file);
778 if (!swsusp_resume_device) {
785 pr_debug("PM: Hibernation image partition %d:%d present\n",
786 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
788 pr_debug("PM: Looking for hibernation image.\n");
789 error = swsusp_check();
793 /* The snapshot device should not be opened while we're running */
794 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
796 swsusp_close(FMODE_READ);
800 pm_prepare_console();
801 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
805 error = usermodehelper_disable();
809 error = create_basic_memory_bitmaps();
813 pr_debug("PM: Preparing processes for restore.\n");
814 error = prepare_processes();
816 swsusp_close(FMODE_READ);
820 pr_debug("PM: Loading hibernation image.\n");
822 error = swsusp_read(&flags);
823 swsusp_close(FMODE_READ);
825 hibernation_restore(flags & SF_PLATFORM_MODE);
827 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
831 free_basic_memory_bitmaps();
832 usermodehelper_enable();
834 pm_notifier_call_chain(PM_POST_RESTORE);
835 pm_restore_console();
836 atomic_inc(&snapshot_device_available);
837 /* For success case, the suspend path will release the lock */
839 mutex_unlock(&pm_mutex);
840 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
843 swsusp_close(FMODE_READ);
847 late_initcall(software_resume);
850 static const char * const hibernation_modes[] = {
851 [HIBERNATION_PLATFORM] = "platform",
852 [HIBERNATION_SHUTDOWN] = "shutdown",
853 [HIBERNATION_REBOOT] = "reboot",
854 [HIBERNATION_TEST] = "test",
855 [HIBERNATION_TESTPROC] = "testproc",
859 * /sys/power/disk - Control hibernation mode.
861 * Hibernation can be handled in several ways. There are a few different ways
862 * to put the system into the sleep state: using the platform driver (e.g. ACPI
863 * or other hibernation_ops), powering it off or rebooting it (for testing
864 * mostly), or using one of the two available test modes.
866 * The sysfs file /sys/power/disk provides an interface for selecting the
867 * hibernation mode to use. Reading from this file causes the available modes
868 * to be printed. There are 5 modes that can be supported:
876 * If a platform hibernation driver is in use, 'platform' will be supported
877 * and will be used by default. Otherwise, 'shutdown' will be used by default.
878 * The selected option (i.e. the one corresponding to the current value of
879 * hibernation_mode) is enclosed by a square bracket.
881 * To select a given hibernation mode it is necessary to write the mode's
882 * string representation (as returned by reading from /sys/power/disk) back
883 * into /sys/power/disk.
886 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
892 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
893 if (!hibernation_modes[i])
896 case HIBERNATION_SHUTDOWN:
897 case HIBERNATION_REBOOT:
898 case HIBERNATION_TEST:
899 case HIBERNATION_TESTPROC:
901 case HIBERNATION_PLATFORM:
904 /* not a valid mode, continue with loop */
907 if (i == hibernation_mode)
908 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
910 buf += sprintf(buf, "%s ", hibernation_modes[i]);
912 buf += sprintf(buf, "\n");
916 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
917 const char *buf, size_t n)
923 int mode = HIBERNATION_INVALID;
925 p = memchr(buf, '\n', n);
926 len = p ? p - buf : n;
928 mutex_lock(&pm_mutex);
929 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
930 if (len == strlen(hibernation_modes[i])
931 && !strncmp(buf, hibernation_modes[i], len)) {
936 if (mode != HIBERNATION_INVALID) {
938 case HIBERNATION_SHUTDOWN:
939 case HIBERNATION_REBOOT:
940 case HIBERNATION_TEST:
941 case HIBERNATION_TESTPROC:
942 hibernation_mode = mode;
944 case HIBERNATION_PLATFORM:
946 hibernation_mode = mode;
954 pr_debug("PM: Hibernation mode set to '%s'\n",
955 hibernation_modes[mode]);
956 mutex_unlock(&pm_mutex);
957 return error ? error : n;
962 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
965 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
966 MINOR(swsusp_resume_device));
969 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
970 const char *buf, size_t n)
972 unsigned int maj, min;
976 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
979 res = MKDEV(maj,min);
980 if (maj != MAJOR(res) || min != MINOR(res))
983 mutex_lock(&pm_mutex);
984 swsusp_resume_device = res;
985 mutex_unlock(&pm_mutex);
986 printk(KERN_INFO "PM: Starting manual resume from disk\n");
996 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
999 return sprintf(buf, "%lu\n", image_size);
1002 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1003 const char *buf, size_t n)
1007 if (sscanf(buf, "%lu", &size) == 1) {
1015 power_attr(image_size);
1017 static ssize_t reserved_size_show(struct kobject *kobj,
1018 struct kobj_attribute *attr, char *buf)
1020 return sprintf(buf, "%lu\n", reserved_size);
1023 static ssize_t reserved_size_store(struct kobject *kobj,
1024 struct kobj_attribute *attr,
1025 const char *buf, size_t n)
1029 if (sscanf(buf, "%lu", &size) == 1) {
1030 reserved_size = size;
1037 power_attr(reserved_size);
1039 static struct attribute * g[] = {
1042 &image_size_attr.attr,
1043 &reserved_size_attr.attr,
1048 static struct attribute_group attr_group = {
1053 static int __init pm_disk_init(void)
1055 return sysfs_create_group(power_kobj, &attr_group);
1058 core_initcall(pm_disk_init);
1061 static int __init resume_setup(char *str)
1066 strncpy( resume_file, str, 255 );
1070 static int __init resume_offset_setup(char *str)
1072 unsigned long long offset;
1077 if (sscanf(str, "%llu", &offset) == 1)
1078 swsusp_resume_block = offset;
1083 static int __init hibernate_setup(char *str)
1085 if (!strncmp(str, "noresume", 8))
1087 else if (!strncmp(str, "nocompress", 10))
1092 static int __init noresume_setup(char *str)
1098 static int __init resumewait_setup(char *str)
1104 static int __init resumedelay_setup(char *str)
1106 resume_delay = simple_strtoul(str, NULL, 0);
1110 __setup("noresume", noresume_setup);
1111 __setup("resume_offset=", resume_offset_setup);
1112 __setup("resume=", resume_setup);
1113 __setup("hibernate=", hibernate_setup);
1114 __setup("resumewait", resumewait_setup);
1115 __setup("resumedelay=", resumedelay_setup);