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/delay.h>
21 #include <linux/mount.h>
23 #include <linux/console.h>
24 #include <linux/cpu.h>
25 #include <linux/freezer.h>
26 #include <linux/gfp.h>
27 #include <linux/syscore_ops.h>
28 #include <scsi/scsi_scan.h>
33 static int nocompress
;
35 static int resume_wait
;
36 static int resume_delay
;
37 static char resume_file
[256] = CONFIG_PM_STD_PARTITION
;
38 dev_t swsusp_resume_device
;
39 sector_t swsusp_resume_block
;
40 int in_suspend __nosavedata
;
48 __HIBERNATION_AFTER_LAST
50 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
51 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
53 static int hibernation_mode
= HIBERNATION_SHUTDOWN
;
55 bool freezer_test_done
;
57 static const struct platform_hibernation_ops
*hibernation_ops
;
60 * hibernation_set_ops - Set the global hibernate operations.
61 * @ops: Hibernation operations to use in subsequent hibernation transitions.
63 void hibernation_set_ops(const struct platform_hibernation_ops
*ops
)
65 if (ops
&& !(ops
->begin
&& ops
->end
&& ops
->pre_snapshot
66 && ops
->prepare
&& ops
->finish
&& ops
->enter
&& ops
->pre_restore
67 && ops
->restore_cleanup
&& ops
->leave
)) {
72 hibernation_ops
= ops
;
74 hibernation_mode
= HIBERNATION_PLATFORM
;
75 else if (hibernation_mode
== HIBERNATION_PLATFORM
)
76 hibernation_mode
= HIBERNATION_SHUTDOWN
;
78 unlock_system_sleep();
81 static bool entering_platform_hibernation
;
83 bool system_entering_hibernation(void)
85 return entering_platform_hibernation
;
87 EXPORT_SYMBOL(system_entering_hibernation
);
89 #ifdef CONFIG_PM_DEBUG
90 static void hibernation_debug_sleep(void)
92 printk(KERN_INFO
"hibernation debug: Waiting for 5 seconds.\n");
96 static int hibernation_test(int level
)
98 if (pm_test_level
== level
) {
99 hibernation_debug_sleep();
104 #else /* !CONFIG_PM_DEBUG */
105 static int hibernation_test(int level
) { return 0; }
106 #endif /* !CONFIG_PM_DEBUG */
109 * platform_begin - Call platform to start hibernation.
110 * @platform_mode: Whether or not to use the platform driver.
112 static int platform_begin(int platform_mode
)
114 return (platform_mode
&& hibernation_ops
) ?
115 hibernation_ops
->begin() : 0;
119 * platform_end - Call platform to finish transition to the working state.
120 * @platform_mode: Whether or not to use the platform driver.
122 static void platform_end(int platform_mode
)
124 if (platform_mode
&& hibernation_ops
)
125 hibernation_ops
->end();
129 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
130 * @platform_mode: Whether or not to use the platform driver.
132 * Use the platform driver to prepare the system for creating a hibernate image,
133 * if so configured, and return an error code if that fails.
136 static int platform_pre_snapshot(int platform_mode
)
138 return (platform_mode
&& hibernation_ops
) ?
139 hibernation_ops
->pre_snapshot() : 0;
143 * platform_leave - Call platform to prepare a transition to the working state.
144 * @platform_mode: Whether or not to use the platform driver.
146 * Use the platform driver prepare to prepare the machine for switching to the
147 * normal mode of operation.
149 * This routine is called on one CPU with interrupts disabled.
151 static void platform_leave(int platform_mode
)
153 if (platform_mode
&& hibernation_ops
)
154 hibernation_ops
->leave();
158 * platform_finish - Call platform to switch the system to the working state.
159 * @platform_mode: Whether or not to use the platform driver.
161 * Use the platform driver to switch the machine to the normal mode of
164 * This routine must be called after platform_prepare().
166 static void platform_finish(int platform_mode
)
168 if (platform_mode
&& hibernation_ops
)
169 hibernation_ops
->finish();
173 * platform_pre_restore - Prepare for hibernate image restoration.
174 * @platform_mode: Whether or not to use the platform driver.
176 * Use the platform driver to prepare the system for resume from a hibernation
179 * If the restore fails after this function has been called,
180 * platform_restore_cleanup() must be called.
182 static int platform_pre_restore(int platform_mode
)
184 return (platform_mode
&& hibernation_ops
) ?
185 hibernation_ops
->pre_restore() : 0;
189 * platform_restore_cleanup - Switch to the working state after failing restore.
190 * @platform_mode: Whether or not to use the platform driver.
192 * Use the platform driver to switch the system to the normal mode of operation
193 * after a failing restore.
195 * If platform_pre_restore() has been called before the failing restore, this
196 * function must be called too, regardless of the result of
197 * platform_pre_restore().
199 static void platform_restore_cleanup(int platform_mode
)
201 if (platform_mode
&& hibernation_ops
)
202 hibernation_ops
->restore_cleanup();
206 * platform_recover - Recover from a failure to suspend devices.
207 * @platform_mode: Whether or not to use the platform driver.
209 static void platform_recover(int platform_mode
)
211 if (platform_mode
&& hibernation_ops
&& hibernation_ops
->recover
)
212 hibernation_ops
->recover();
216 * swsusp_show_speed - Print time elapsed between two events during hibernation.
217 * @start: Starting event.
218 * @stop: Final event.
219 * @nr_pages: Number of memory pages processed between @start and @stop.
220 * @msg: Additional diagnostic message to print.
222 void swsusp_show_speed(struct timeval
*start
, struct timeval
*stop
,
223 unsigned nr_pages
, char *msg
)
225 s64 elapsed_centisecs64
;
230 elapsed_centisecs64
= timeval_to_ns(stop
) - timeval_to_ns(start
);
231 do_div(elapsed_centisecs64
, NSEC_PER_SEC
/ 100);
232 centisecs
= elapsed_centisecs64
;
234 centisecs
= 1; /* avoid div-by-zero */
235 k
= nr_pages
* (PAGE_SIZE
/ 1024);
236 kps
= (k
* 100) / centisecs
;
237 printk(KERN_INFO
"PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
239 centisecs
/ 100, centisecs
% 100,
240 kps
/ 1000, (kps
% 1000) / 10);
244 * create_image - Create a hibernation image.
245 * @platform_mode: Whether or not to use the platform driver.
247 * Execute device drivers' .freeze_noirq() callbacks, create a hibernation image
248 * and execute the drivers' .thaw_noirq() callbacks.
250 * Control reappears in this routine after the subsequent restore.
252 static int create_image(int platform_mode
)
256 error
= dpm_suspend_noirq(PMSG_FREEZE
);
258 printk(KERN_ERR
"PM: Some devices failed to power down, "
259 "aborting hibernation\n");
263 error
= platform_pre_snapshot(platform_mode
);
264 if (error
|| hibernation_test(TEST_PLATFORM
))
265 goto Platform_finish
;
267 error
= disable_nonboot_cpus();
268 if (error
|| hibernation_test(TEST_CPUS
))
273 error
= syscore_suspend();
275 printk(KERN_ERR
"PM: Some system devices failed to power down, "
276 "aborting hibernation\n");
280 if (hibernation_test(TEST_CORE
) || pm_wakeup_pending())
284 save_processor_state();
285 error
= swsusp_arch_suspend();
287 printk(KERN_ERR
"PM: Error %d creating hibernation image\n",
289 /* Restore control flow magically appears here */
290 restore_processor_state();
292 events_check_enabled
= false;
293 platform_leave(platform_mode
);
303 enable_nonboot_cpus();
306 platform_finish(platform_mode
);
308 dpm_resume_noirq(in_suspend
?
309 (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
);
315 * hibernation_snapshot - Quiesce devices and create a hibernation image.
316 * @platform_mode: If set, use platform driver to prepare for the transition.
318 * This routine must be called with pm_mutex held.
320 int hibernation_snapshot(int platform_mode
)
325 error
= platform_begin(platform_mode
);
329 /* Preallocate image memory before shutting down devices. */
330 error
= hibernate_preallocate_memory();
334 error
= freeze_kernel_threads();
338 if (hibernation_test(TEST_FREEZER
)) {
341 * Indicate to the caller that we are returning due to a
342 * successful freezer test.
344 freezer_test_done
= true;
348 error
= dpm_prepare(PMSG_FREEZE
);
350 dpm_complete(PMSG_RECOVER
);
355 pm_restrict_gfp_mask();
357 error
= dpm_suspend(PMSG_FREEZE
);
359 if (error
|| hibernation_test(TEST_DEVICES
))
360 platform_recover(platform_mode
);
362 error
= create_image(platform_mode
);
365 * In the case that we call create_image() above, the control
366 * returns here (1) after the image has been created or the
367 * image creation has failed and (2) after a successful restore.
370 /* We may need to release the preallocated image pages here. */
371 if (error
|| !in_suspend
)
374 msg
= in_suspend
? (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
;
377 if (error
|| !in_suspend
)
378 pm_restore_gfp_mask();
384 platform_end(platform_mode
);
393 * resume_target_kernel - Restore system state from a hibernation image.
394 * @platform_mode: Whether or not to use the platform driver.
396 * Execute device drivers' .freeze_noirq() callbacks, restore the contents of
397 * highmem that have not been restored yet from the image and run the low-level
398 * code that will restore the remaining contents of memory and switch to the
399 * just restored target kernel.
401 static int resume_target_kernel(bool platform_mode
)
405 error
= dpm_suspend_noirq(PMSG_QUIESCE
);
407 printk(KERN_ERR
"PM: Some devices failed to power down, "
408 "aborting resume\n");
412 error
= platform_pre_restore(platform_mode
);
416 error
= disable_nonboot_cpus();
422 error
= syscore_suspend();
426 save_processor_state();
427 error
= restore_highmem();
429 error
= swsusp_arch_resume();
431 * The code below is only ever reached in case of a failure.
432 * Otherwise, execution continues at the place where
433 * swsusp_arch_suspend() was called.
437 * This call to restore_highmem() reverts the changes made by
443 * The only reason why swsusp_arch_resume() can fail is memory being
444 * very tight, so we have to free it as soon as we can to avoid
445 * subsequent failures.
448 restore_processor_state();
449 touch_softlockup_watchdog();
457 enable_nonboot_cpus();
460 platform_restore_cleanup(platform_mode
);
462 dpm_resume_noirq(PMSG_RECOVER
);
468 * hibernation_restore - Quiesce devices and restore from a hibernation image.
469 * @platform_mode: If set, use platform driver to prepare for the transition.
471 * This routine must be called with pm_mutex held. If it is successful, control
472 * reappears in the restored target kernel in hibernation_snapshot().
474 int hibernation_restore(int platform_mode
)
478 pm_prepare_console();
480 pm_restrict_gfp_mask();
481 error
= dpm_suspend_start(PMSG_QUIESCE
);
483 error
= resume_target_kernel(platform_mode
);
484 dpm_resume_end(PMSG_RECOVER
);
486 pm_restore_gfp_mask();
488 pm_restore_console();
493 * hibernation_platform_enter - Power off the system using the platform driver.
495 int hibernation_platform_enter(void)
499 if (!hibernation_ops
)
503 * We have cancelled the power transition by running
504 * hibernation_ops->finish() before saving the image, so we should let
505 * the firmware know that we're going to enter the sleep state after all
507 error
= hibernation_ops
->begin();
511 entering_platform_hibernation
= true;
513 error
= dpm_suspend_start(PMSG_HIBERNATE
);
515 if (hibernation_ops
->recover
)
516 hibernation_ops
->recover();
520 error
= dpm_suspend_noirq(PMSG_HIBERNATE
);
524 error
= hibernation_ops
->prepare();
526 goto Platform_finish
;
528 error
= disable_nonboot_cpus();
530 goto Platform_finish
;
534 if (pm_wakeup_pending()) {
539 hibernation_ops
->enter();
540 /* We should never get here */
546 enable_nonboot_cpus();
549 hibernation_ops
->finish();
551 dpm_resume_noirq(PMSG_RESTORE
);
554 entering_platform_hibernation
= false;
555 dpm_resume_end(PMSG_RESTORE
);
559 hibernation_ops
->end();
565 * power_down - Shut the machine down for hibernation.
567 * Use the platform driver, if configured, to put the system into the sleep
568 * state corresponding to hibernation, or try to power it off or reboot,
569 * depending on the value of hibernation_mode.
571 static void power_down(void)
573 switch (hibernation_mode
) {
574 case HIBERNATION_REBOOT
:
575 kernel_restart(NULL
);
577 case HIBERNATION_PLATFORM
:
578 hibernation_platform_enter();
579 case HIBERNATION_SHUTDOWN
:
585 * Valid image is on the disk, if we continue we risk serious data
586 * corruption after resume.
588 printk(KERN_CRIT
"PM: Please power down manually\n");
593 * hibernate - Carry out system hibernation, including saving the image.
600 /* The snapshot device should not be opened while we're running */
601 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
606 pm_prepare_console();
607 error
= pm_notifier_call_chain(PM_HIBERNATION_PREPARE
);
611 /* Allocate memory management structures */
612 error
= create_basic_memory_bitmaps();
616 printk(KERN_INFO
"PM: Syncing filesystems ... ");
620 error
= freeze_processes();
624 error
= hibernation_snapshot(hibernation_mode
== HIBERNATION_PLATFORM
);
627 if (freezer_test_done
) {
628 freezer_test_done
= false;
633 unsigned int flags
= 0;
635 if (hibernation_mode
== HIBERNATION_PLATFORM
)
636 flags
|= SF_PLATFORM_MODE
;
638 flags
|= SF_NOCOMPRESS_MODE
;
640 flags
|= SF_CRC32_MODE
;
642 pr_debug("PM: writing image.\n");
643 error
= swsusp_write(flags
);
648 pm_restore_gfp_mask();
650 pr_debug("PM: Image restored successfully.\n");
656 free_basic_memory_bitmaps();
658 pm_notifier_call_chain(PM_POST_HIBERNATION
);
659 pm_restore_console();
660 atomic_inc(&snapshot_device_available
);
662 unlock_system_sleep();
668 * software_resume - Resume from a saved hibernation image.
670 * This routine is called as a late initcall, when all devices have been
671 * discovered and initialized already.
673 * The image reading code is called to see if there is a hibernation image
674 * available for reading. If that is the case, devices are quiesced and the
675 * contents of memory is restored from the saved image.
677 * If this is successful, control reappears in the restored target kernel in
678 * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine
679 * attempts to recover gracefully and make the kernel return to the normal mode
682 static int software_resume(void)
688 * If the user said "noresume".. bail out early.
694 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
695 * is configured into the kernel. Since the regular hibernate
696 * trigger path is via sysfs which takes a buffer mutex before
697 * calling hibernate functions (which take pm_mutex) this can
698 * cause lockdep to complain about a possible ABBA deadlock
699 * which cannot happen since we're in the boot code here and
700 * sysfs can't be invoked yet. Therefore, we use a subclass
701 * here to avoid lockdep complaining.
703 mutex_lock_nested(&pm_mutex
, SINGLE_DEPTH_NESTING
);
705 if (swsusp_resume_device
)
708 if (!strlen(resume_file
)) {
713 pr_debug("PM: Checking hibernation image partition %s\n", resume_file
);
716 printk(KERN_INFO
"Waiting %dsec before reading resume device...\n",
718 ssleep(resume_delay
);
721 /* Check if the device is there */
722 swsusp_resume_device
= name_to_dev_t(resume_file
);
723 if (!swsusp_resume_device
) {
725 * Some device discovery might still be in progress; we need
726 * to wait for this to finish.
728 wait_for_device_probe();
731 while ((swsusp_resume_device
= name_to_dev_t(resume_file
)) == 0)
733 async_synchronize_full();
737 * We can't depend on SCSI devices being available after loading
738 * one of their modules until scsi_complete_async_scans() is
739 * called and the resume device usually is a SCSI one.
741 scsi_complete_async_scans();
743 swsusp_resume_device
= name_to_dev_t(resume_file
);
744 if (!swsusp_resume_device
) {
751 pr_debug("PM: Hibernation image partition %d:%d present\n",
752 MAJOR(swsusp_resume_device
), MINOR(swsusp_resume_device
));
754 pr_debug("PM: Looking for hibernation image.\n");
755 error
= swsusp_check();
759 /* The snapshot device should not be opened while we're running */
760 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
762 swsusp_close(FMODE_READ
);
766 pm_prepare_console();
767 error
= pm_notifier_call_chain(PM_RESTORE_PREPARE
);
771 error
= create_basic_memory_bitmaps();
775 pr_debug("PM: Preparing processes for restore.\n");
776 error
= freeze_processes();
778 swsusp_close(FMODE_READ
);
782 pr_debug("PM: Loading hibernation image.\n");
784 error
= swsusp_read(&flags
);
785 swsusp_close(FMODE_READ
);
787 hibernation_restore(flags
& SF_PLATFORM_MODE
);
789 printk(KERN_ERR
"PM: Failed to load hibernation image, recovering.\n");
793 free_basic_memory_bitmaps();
795 pm_notifier_call_chain(PM_POST_RESTORE
);
796 pm_restore_console();
797 atomic_inc(&snapshot_device_available
);
798 /* For success case, the suspend path will release the lock */
800 mutex_unlock(&pm_mutex
);
801 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
804 swsusp_close(FMODE_READ
);
808 late_initcall(software_resume
);
811 static const char * const hibernation_modes
[] = {
812 [HIBERNATION_PLATFORM
] = "platform",
813 [HIBERNATION_SHUTDOWN
] = "shutdown",
814 [HIBERNATION_REBOOT
] = "reboot",
818 * /sys/power/disk - Control hibernation mode.
820 * Hibernation can be handled in several ways. There are a few different ways
821 * to put the system into the sleep state: using the platform driver (e.g. ACPI
822 * or other hibernation_ops), powering it off or rebooting it (for testing
825 * The sysfs file /sys/power/disk provides an interface for selecting the
826 * hibernation mode to use. Reading from this file causes the available modes
827 * to be printed. There are 3 modes that can be supported:
833 * If a platform hibernation driver is in use, 'platform' will be supported
834 * and will be used by default. Otherwise, 'shutdown' will be used by default.
835 * The selected option (i.e. the one corresponding to the current value of
836 * hibernation_mode) is enclosed by a square bracket.
838 * To select a given hibernation mode it is necessary to write the mode's
839 * string representation (as returned by reading from /sys/power/disk) back
840 * into /sys/power/disk.
843 static ssize_t
disk_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
849 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
850 if (!hibernation_modes
[i
])
853 case HIBERNATION_SHUTDOWN
:
854 case HIBERNATION_REBOOT
:
856 case HIBERNATION_PLATFORM
:
859 /* not a valid mode, continue with loop */
862 if (i
== hibernation_mode
)
863 buf
+= sprintf(buf
, "[%s] ", hibernation_modes
[i
]);
865 buf
+= sprintf(buf
, "%s ", hibernation_modes
[i
]);
867 buf
+= sprintf(buf
, "\n");
871 static ssize_t
disk_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
872 const char *buf
, size_t n
)
878 int mode
= HIBERNATION_INVALID
;
880 p
= memchr(buf
, '\n', n
);
881 len
= p
? p
- buf
: n
;
884 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
885 if (len
== strlen(hibernation_modes
[i
])
886 && !strncmp(buf
, hibernation_modes
[i
], len
)) {
891 if (mode
!= HIBERNATION_INVALID
) {
893 case HIBERNATION_SHUTDOWN
:
894 case HIBERNATION_REBOOT
:
895 hibernation_mode
= mode
;
897 case HIBERNATION_PLATFORM
:
899 hibernation_mode
= mode
;
907 pr_debug("PM: Hibernation mode set to '%s'\n",
908 hibernation_modes
[mode
]);
909 unlock_system_sleep();
910 return error
? error
: n
;
915 static ssize_t
resume_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
918 return sprintf(buf
,"%d:%d\n", MAJOR(swsusp_resume_device
),
919 MINOR(swsusp_resume_device
));
922 static ssize_t
resume_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
923 const char *buf
, size_t n
)
925 unsigned int maj
, min
;
929 if (sscanf(buf
, "%u:%u", &maj
, &min
) != 2)
932 res
= MKDEV(maj
,min
);
933 if (maj
!= MAJOR(res
) || min
!= MINOR(res
))
937 swsusp_resume_device
= res
;
938 unlock_system_sleep();
939 printk(KERN_INFO
"PM: Starting manual resume from disk\n");
949 static ssize_t
image_size_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
952 return sprintf(buf
, "%lu\n", image_size
);
955 static ssize_t
image_size_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
956 const char *buf
, size_t n
)
960 if (sscanf(buf
, "%lu", &size
) == 1) {
968 power_attr(image_size
);
970 static ssize_t
reserved_size_show(struct kobject
*kobj
,
971 struct kobj_attribute
*attr
, char *buf
)
973 return sprintf(buf
, "%lu\n", reserved_size
);
976 static ssize_t
reserved_size_store(struct kobject
*kobj
,
977 struct kobj_attribute
*attr
,
978 const char *buf
, size_t n
)
982 if (sscanf(buf
, "%lu", &size
) == 1) {
983 reserved_size
= size
;
990 power_attr(reserved_size
);
992 static struct attribute
* g
[] = {
995 &image_size_attr
.attr
,
996 &reserved_size_attr
.attr
,
1001 static struct attribute_group attr_group
= {
1006 static int __init
pm_disk_init(void)
1008 return sysfs_create_group(power_kobj
, &attr_group
);
1011 core_initcall(pm_disk_init
);
1014 static int __init
resume_setup(char *str
)
1019 strncpy( resume_file
, str
, 255 );
1023 static int __init
resume_offset_setup(char *str
)
1025 unsigned long long offset
;
1030 if (sscanf(str
, "%llu", &offset
) == 1)
1031 swsusp_resume_block
= offset
;
1036 static int __init
hibernate_setup(char *str
)
1038 if (!strncmp(str
, "noresume", 8))
1040 else if (!strncmp(str
, "nocompress", 10))
1045 static int __init
noresume_setup(char *str
)
1051 static int __init
resumewait_setup(char *str
)
1057 static int __init
resumedelay_setup(char *str
)
1059 resume_delay
= simple_strtoul(str
, NULL
, 0);
1063 __setup("noresume", noresume_setup
);
1064 __setup("resume_offset=", resume_offset_setup
);
1065 __setup("resume=", resume_setup
);
1066 __setup("hibernate=", hibernate_setup
);
1067 __setup("resumewait", resumewait_setup
);
1068 __setup("resumedelay=", resumedelay_setup
);