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/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 <linux/gfp.h>
26 #include <scsi/scsi_scan.h>
27 #include <asm/suspend.h>
32 static int nocompress
= 0;
33 static int noresume
= 0;
34 static char resume_file
[256] = CONFIG_PM_STD_PARTITION
;
35 dev_t swsusp_resume_device
;
36 sector_t swsusp_resume_block
;
37 int in_suspend __nosavedata
= 0;
47 __HIBERNATION_AFTER_LAST
49 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
50 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
52 static int hibernation_mode
= HIBERNATION_SHUTDOWN
;
54 static const struct platform_hibernation_ops
*hibernation_ops
;
57 * hibernation_set_ops - set the global hibernate operations
58 * @ops: the hibernation operations to use in subsequent hibernation transitions
61 void hibernation_set_ops(const struct platform_hibernation_ops
*ops
)
63 if (ops
&& !(ops
->begin
&& ops
->end
&& ops
->pre_snapshot
64 && ops
->prepare
&& ops
->finish
&& ops
->enter
&& ops
->pre_restore
65 && ops
->restore_cleanup
&& ops
->leave
)) {
69 mutex_lock(&pm_mutex
);
70 hibernation_ops
= ops
;
72 hibernation_mode
= HIBERNATION_PLATFORM
;
73 else if (hibernation_mode
== HIBERNATION_PLATFORM
)
74 hibernation_mode
= HIBERNATION_SHUTDOWN
;
76 mutex_unlock(&pm_mutex
);
79 static bool entering_platform_hibernation
;
81 bool system_entering_hibernation(void)
83 return entering_platform_hibernation
;
85 EXPORT_SYMBOL(system_entering_hibernation
);
87 #ifdef CONFIG_PM_DEBUG
88 static void hibernation_debug_sleep(void)
90 printk(KERN_INFO
"hibernation debug: Waiting for 5 seconds.\n");
94 static int hibernation_testmode(int mode
)
96 if (hibernation_mode
== mode
) {
97 hibernation_debug_sleep();
103 static int hibernation_test(int level
)
105 if (pm_test_level
== level
) {
106 hibernation_debug_sleep();
111 #else /* !CONFIG_PM_DEBUG */
112 static int hibernation_testmode(int mode
) { return 0; }
113 static int hibernation_test(int level
) { return 0; }
114 #endif /* !CONFIG_PM_DEBUG */
117 * platform_begin - tell the platform driver that we're starting
121 static int platform_begin(int platform_mode
)
123 return (platform_mode
&& hibernation_ops
) ?
124 hibernation_ops
->begin() : 0;
128 * platform_end - tell the platform driver that we've entered the
132 static void platform_end(int platform_mode
)
134 if (platform_mode
&& hibernation_ops
)
135 hibernation_ops
->end();
139 * platform_pre_snapshot - prepare the machine for hibernation using the
140 * platform driver if so configured and return an error code if it fails
143 static int platform_pre_snapshot(int platform_mode
)
145 return (platform_mode
&& hibernation_ops
) ?
146 hibernation_ops
->pre_snapshot() : 0;
150 * platform_leave - prepare the machine for switching to the normal mode
151 * of operation using the platform driver (called with interrupts disabled)
154 static void platform_leave(int platform_mode
)
156 if (platform_mode
&& hibernation_ops
)
157 hibernation_ops
->leave();
161 * platform_finish - switch the machine to the normal mode of operation
162 * using the platform driver (must be called after platform_prepare())
165 static void platform_finish(int platform_mode
)
167 if (platform_mode
&& hibernation_ops
)
168 hibernation_ops
->finish();
172 * platform_pre_restore - prepare the platform for the restoration from a
173 * hibernation image. If the restore fails after this function has been
174 * called, platform_restore_cleanup() must be called.
177 static int platform_pre_restore(int platform_mode
)
179 return (platform_mode
&& hibernation_ops
) ?
180 hibernation_ops
->pre_restore() : 0;
184 * platform_restore_cleanup - switch the platform to the normal mode of
185 * operation after a failing restore. If platform_pre_restore() has been
186 * called before the failing restore, this function must be called too,
187 * regardless of the result of platform_pre_restore().
190 static void platform_restore_cleanup(int platform_mode
)
192 if (platform_mode
&& hibernation_ops
)
193 hibernation_ops
->restore_cleanup();
197 * platform_recover - recover the platform from a failure to suspend
201 static void platform_recover(int platform_mode
)
203 if (platform_mode
&& hibernation_ops
&& hibernation_ops
->recover
)
204 hibernation_ops
->recover();
208 * swsusp_show_speed - print the time elapsed between two events.
209 * @start: Starting event.
210 * @stop: Final event.
211 * @nr_pages - number of pages processed between @start and @stop
212 * @msg - introductory message to print
215 void swsusp_show_speed(struct timeval
*start
, struct timeval
*stop
,
216 unsigned nr_pages
, char *msg
)
218 s64 elapsed_centisecs64
;
223 elapsed_centisecs64
= timeval_to_ns(stop
) - timeval_to_ns(start
);
224 do_div(elapsed_centisecs64
, NSEC_PER_SEC
/ 100);
225 centisecs
= elapsed_centisecs64
;
227 centisecs
= 1; /* avoid div-by-zero */
228 k
= nr_pages
* (PAGE_SIZE
/ 1024);
229 kps
= (k
* 100) / centisecs
;
230 printk(KERN_INFO
"PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
232 centisecs
/ 100, centisecs
% 100,
233 kps
/ 1000, (kps
% 1000) / 10);
237 * create_image - freeze devices that need to be frozen with interrupts
238 * off, create the hibernation image and thaw those devices. Control
239 * reappears in this routine after a restore.
242 static int create_image(int platform_mode
)
246 error
= arch_prepare_suspend();
250 /* At this point, dpm_suspend_start() has been called, but *not*
251 * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
252 * Otherwise, drivers for some devices (e.g. interrupt controllers)
253 * become desynchronized with the actual state of the hardware
254 * at resume time, and evil weirdness ensues.
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
)
269 || hibernation_testmode(HIBERNATION_TEST
))
274 error
= sysdev_suspend(PMSG_FREEZE
);
276 printk(KERN_ERR
"PM: Some system devices failed to power down, "
277 "aborting hibernation\n");
281 if (hibernation_test(TEST_CORE
) || pm_wakeup_pending())
285 save_processor_state();
286 error
= swsusp_arch_suspend();
288 printk(KERN_ERR
"PM: Error %d creating hibernation image\n",
290 /* Restore control flow magically appears here */
291 restore_processor_state();
293 events_check_enabled
= false;
294 platform_leave(platform_mode
);
299 /* NOTE: dpm_resume_noirq() is just a resume() for devices
300 * that suspended with irqs off ... no overall powerup.
307 enable_nonboot_cpus();
310 platform_finish(platform_mode
);
312 dpm_resume_noirq(in_suspend
?
313 (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
);
319 * hibernation_snapshot - quiesce devices and create the hibernation
321 * @platform_mode - if set, use the platform driver, if available, to
322 * prepare the platform firmware for the power transition.
324 * Must be called with pm_mutex held
327 int hibernation_snapshot(int platform_mode
)
331 error
= platform_begin(platform_mode
);
335 /* Preallocate image memory before shutting down devices. */
336 error
= hibernate_preallocate_memory();
341 pm_restrict_gfp_mask();
342 error
= dpm_suspend_start(PMSG_FREEZE
);
344 goto Recover_platform
;
346 if (hibernation_test(TEST_DEVICES
))
347 goto Recover_platform
;
349 error
= create_image(platform_mode
);
351 * Control returns here (1) after the image has been created or the
352 * image creation has failed and (2) after a successful restore.
356 /* We may need to release the preallocated image pages here. */
357 if (error
|| !in_suspend
)
360 dpm_resume_end(in_suspend
?
361 (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
);
363 if (error
|| !in_suspend
)
364 pm_restore_gfp_mask();
368 platform_end(platform_mode
);
372 platform_recover(platform_mode
);
377 * resume_target_kernel - prepare devices that need to be suspended with
378 * interrupts off, restore the contents of highmem that have not been
379 * restored yet from the image and run the low level code that will restore
380 * the remaining contents of memory and switch to the just restored target
384 static int resume_target_kernel(bool platform_mode
)
388 error
= dpm_suspend_noirq(PMSG_QUIESCE
);
390 printk(KERN_ERR
"PM: Some devices failed to power down, "
391 "aborting resume\n");
395 error
= platform_pre_restore(platform_mode
);
399 error
= disable_nonboot_cpus();
405 error
= sysdev_suspend(PMSG_QUIESCE
);
409 /* We'll ignore saved state, but this gets preempt count (etc) right */
410 save_processor_state();
411 error
= restore_highmem();
413 error
= swsusp_arch_resume();
415 * The code below is only ever reached in case of a failure.
416 * Otherwise execution continues at place where
417 * swsusp_arch_suspend() was called
420 /* This call to restore_highmem() undos the previous one */
424 * The only reason why swsusp_arch_resume() can fail is memory being
425 * very tight, so we have to free it as soon as we can to avoid
426 * subsequent failures
429 restore_processor_state();
430 touch_softlockup_watchdog();
438 enable_nonboot_cpus();
441 platform_restore_cleanup(platform_mode
);
443 dpm_resume_noirq(PMSG_RECOVER
);
449 * hibernation_restore - quiesce devices and restore the hibernation
450 * snapshot image. If successful, control returns in hibernation_snaphot()
451 * @platform_mode - if set, use the platform driver, if available, to
452 * prepare the platform firmware for the transition.
454 * Must be called with pm_mutex held
457 int hibernation_restore(int platform_mode
)
461 pm_prepare_console();
463 pm_restrict_gfp_mask();
464 error
= dpm_suspend_start(PMSG_QUIESCE
);
466 error
= resume_target_kernel(platform_mode
);
467 dpm_resume_end(PMSG_RECOVER
);
469 pm_restore_gfp_mask();
471 pm_restore_console();
476 * hibernation_platform_enter - enter the hibernation state using the
477 * platform driver (if available)
480 int hibernation_platform_enter(void)
484 if (!hibernation_ops
)
488 * We have cancelled the power transition by running
489 * hibernation_ops->finish() before saving the image, so we should let
490 * the firmware know that we're going to enter the sleep state after all
492 error
= hibernation_ops
->begin();
496 entering_platform_hibernation
= true;
498 error
= dpm_suspend_start(PMSG_HIBERNATE
);
500 if (hibernation_ops
->recover
)
501 hibernation_ops
->recover();
505 error
= dpm_suspend_noirq(PMSG_HIBERNATE
);
509 error
= hibernation_ops
->prepare();
511 goto Platform_finish
;
513 error
= disable_nonboot_cpus();
515 goto Platform_finish
;
518 sysdev_suspend(PMSG_HIBERNATE
);
519 if (pm_wakeup_pending()) {
524 hibernation_ops
->enter();
525 /* We should never get here */
531 enable_nonboot_cpus();
534 hibernation_ops
->finish();
536 dpm_resume_noirq(PMSG_RESTORE
);
539 entering_platform_hibernation
= false;
540 dpm_resume_end(PMSG_RESTORE
);
544 hibernation_ops
->end();
550 * power_down - Shut the machine down for hibernation.
552 * Use the platform driver, if configured so; otherwise try
553 * to power off or reboot.
556 static void power_down(void)
558 switch (hibernation_mode
) {
559 case HIBERNATION_TEST
:
560 case HIBERNATION_TESTPROC
:
562 case HIBERNATION_REBOOT
:
563 kernel_restart(NULL
);
565 case HIBERNATION_PLATFORM
:
566 hibernation_platform_enter();
567 case HIBERNATION_SHUTDOWN
:
573 * Valid image is on the disk, if we continue we risk serious data
574 * corruption after resume.
576 printk(KERN_CRIT
"PM: Please power down manually\n");
580 static int prepare_processes(void)
584 if (freeze_processes()) {
592 * hibernate - The granpappy of the built-in hibernation management
599 mutex_lock(&pm_mutex
);
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 error
= usermodehelper_disable();
615 /* Allocate memory management structures */
616 error
= create_basic_memory_bitmaps();
620 printk(KERN_INFO
"PM: Syncing filesystems ... ");
624 error
= prepare_processes();
628 if (hibernation_test(TEST_FREEZER
))
631 if (hibernation_testmode(HIBERNATION_TESTPROC
))
634 error
= hibernation_snapshot(hibernation_mode
== HIBERNATION_PLATFORM
);
639 unsigned int flags
= 0;
641 if (hibernation_mode
== HIBERNATION_PLATFORM
)
642 flags
|= SF_PLATFORM_MODE
;
644 flags
|= SF_NOCOMPRESS_MODE
;
645 pr_debug("PM: writing image.\n");
646 error
= swsusp_write(flags
);
651 pm_restore_gfp_mask();
653 pr_debug("PM: Image restored successfully.\n");
659 free_basic_memory_bitmaps();
660 usermodehelper_enable();
662 pm_notifier_call_chain(PM_POST_HIBERNATION
);
663 pm_restore_console();
664 atomic_inc(&snapshot_device_available
);
666 mutex_unlock(&pm_mutex
);
672 * software_resume - Resume from a saved image.
674 * Called as a late_initcall (so all devices are discovered and
675 * initialized), we call swsusp to see if we have a saved image or not.
676 * If so, we quiesce devices, the restore the saved image. We will
677 * return above (in hibernate() ) if everything goes well.
678 * Otherwise, we fail gracefully and return to the normally
683 static int software_resume(void)
689 * If the user said "noresume".. bail out early.
695 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
696 * is configured into the kernel. Since the regular hibernate
697 * trigger path is via sysfs which takes a buffer mutex before
698 * calling hibernate functions (which take pm_mutex) this can
699 * cause lockdep to complain about a possible ABBA deadlock
700 * which cannot happen since we're in the boot code here and
701 * sysfs can't be invoked yet. Therefore, we use a subclass
702 * here to avoid lockdep complaining.
704 mutex_lock_nested(&pm_mutex
, SINGLE_DEPTH_NESTING
);
706 if (swsusp_resume_device
)
709 if (!strlen(resume_file
)) {
714 pr_debug("PM: Checking hibernation image partition %s\n", resume_file
);
716 /* Check if the device is there */
717 swsusp_resume_device
= name_to_dev_t(resume_file
);
718 if (!swsusp_resume_device
) {
720 * Some device discovery might still be in progress; we need
721 * to wait for this to finish.
723 wait_for_device_probe();
725 * We can't depend on SCSI devices being available after loading
726 * one of their modules until scsi_complete_async_scans() is
727 * called and the resume device usually is a SCSI one.
729 scsi_complete_async_scans();
731 swsusp_resume_device
= name_to_dev_t(resume_file
);
732 if (!swsusp_resume_device
) {
739 pr_debug("PM: Hibernation image partition %d:%d present\n",
740 MAJOR(swsusp_resume_device
), MINOR(swsusp_resume_device
));
742 pr_debug("PM: Looking for hibernation image.\n");
743 error
= swsusp_check();
747 /* The snapshot device should not be opened while we're running */
748 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
750 swsusp_close(FMODE_READ
);
754 pm_prepare_console();
755 error
= pm_notifier_call_chain(PM_RESTORE_PREPARE
);
759 error
= usermodehelper_disable();
763 error
= create_basic_memory_bitmaps();
767 pr_debug("PM: Preparing processes for restore.\n");
768 error
= prepare_processes();
770 swsusp_close(FMODE_READ
);
774 pr_debug("PM: Loading hibernation image.\n");
776 error
= swsusp_read(&flags
);
777 swsusp_close(FMODE_READ
);
779 hibernation_restore(flags
& SF_PLATFORM_MODE
);
781 printk(KERN_ERR
"PM: Failed to load hibernation image, recovering.\n");
785 free_basic_memory_bitmaps();
786 usermodehelper_enable();
788 pm_notifier_call_chain(PM_POST_RESTORE
);
789 pm_restore_console();
790 atomic_inc(&snapshot_device_available
);
791 /* For success case, the suspend path will release the lock */
793 mutex_unlock(&pm_mutex
);
794 pr_debug("PM: Hibernation image not present or could not be loaded.\n");
797 swsusp_close(FMODE_READ
);
801 late_initcall(software_resume
);
804 static const char * const hibernation_modes
[] = {
805 [HIBERNATION_PLATFORM
] = "platform",
806 [HIBERNATION_SHUTDOWN
] = "shutdown",
807 [HIBERNATION_REBOOT
] = "reboot",
808 [HIBERNATION_TEST
] = "test",
809 [HIBERNATION_TESTPROC
] = "testproc",
813 * disk - Control hibernation mode
815 * Suspend-to-disk can be handled in several ways. We have a few options
816 * for putting the system to sleep - using the platform driver (e.g. ACPI
817 * or other hibernation_ops), powering off the system or rebooting the
818 * system (for testing) as well as the two test modes.
820 * The system can support 'platform', and that is known a priori (and
821 * encoded by the presence of hibernation_ops). However, the user may
822 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
823 * test modes, 'test' or 'testproc'.
825 * show() will display what the mode is currently set to.
826 * store() will accept one of
834 * It will only change to 'platform' if the system
835 * supports it (as determined by having hibernation_ops).
838 static ssize_t
disk_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
844 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
845 if (!hibernation_modes
[i
])
848 case HIBERNATION_SHUTDOWN
:
849 case HIBERNATION_REBOOT
:
850 case HIBERNATION_TEST
:
851 case HIBERNATION_TESTPROC
:
853 case HIBERNATION_PLATFORM
:
856 /* not a valid mode, continue with loop */
859 if (i
== hibernation_mode
)
860 buf
+= sprintf(buf
, "[%s] ", hibernation_modes
[i
]);
862 buf
+= sprintf(buf
, "%s ", hibernation_modes
[i
]);
864 buf
+= sprintf(buf
, "\n");
869 static ssize_t
disk_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
870 const char *buf
, size_t n
)
876 int mode
= HIBERNATION_INVALID
;
878 p
= memchr(buf
, '\n', n
);
879 len
= p
? p
- buf
: n
;
881 mutex_lock(&pm_mutex
);
882 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
883 if (len
== strlen(hibernation_modes
[i
])
884 && !strncmp(buf
, hibernation_modes
[i
], len
)) {
889 if (mode
!= HIBERNATION_INVALID
) {
891 case HIBERNATION_SHUTDOWN
:
892 case HIBERNATION_REBOOT
:
893 case HIBERNATION_TEST
:
894 case HIBERNATION_TESTPROC
:
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 mutex_unlock(&pm_mutex
);
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
))
936 mutex_lock(&pm_mutex
);
937 swsusp_resume_device
= res
;
938 mutex_unlock(&pm_mutex
);
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 struct attribute
* g
[] = {
973 &image_size_attr
.attr
,
978 static struct attribute_group attr_group
= {
983 static int __init
pm_disk_init(void)
985 return sysfs_create_group(power_kobj
, &attr_group
);
988 core_initcall(pm_disk_init
);
991 static int __init
resume_setup(char *str
)
996 strncpy( resume_file
, str
, 255 );
1000 static int __init
resume_offset_setup(char *str
)
1002 unsigned long long offset
;
1007 if (sscanf(str
, "%llu", &offset
) == 1)
1008 swsusp_resume_block
= offset
;
1013 static int __init
hibernate_setup(char *str
)
1015 if (!strncmp(str
, "noresume", 8))
1017 else if (!strncmp(str
, "nocompress", 10))
1022 static int __init
noresume_setup(char *str
)
1028 __setup("noresume", noresume_setup
);
1029 __setup("resume_offset=", resume_offset_setup
);
1030 __setup("resume=", resume_setup
);
1031 __setup("hibernate=", hibernate_setup
);