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@suse.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 <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
;
35 int in_suspend __nosavedata
= 0;
45 __HIBERNATION_AFTER_LAST
47 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
48 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
50 static int hibernation_mode
= HIBERNATION_SHUTDOWN
;
52 static struct platform_hibernation_ops
*hibernation_ops
;
55 * hibernation_set_ops - set the global hibernate operations
56 * @ops: the hibernation operations to use in subsequent hibernation transitions
59 void hibernation_set_ops(struct platform_hibernation_ops
*ops
)
61 if (ops
&& !(ops
->begin
&& ops
->end
&& ops
->pre_snapshot
62 && ops
->prepare
&& ops
->finish
&& ops
->enter
&& ops
->pre_restore
63 && ops
->restore_cleanup
)) {
67 mutex_lock(&pm_mutex
);
68 hibernation_ops
= ops
;
70 hibernation_mode
= HIBERNATION_PLATFORM
;
71 else if (hibernation_mode
== HIBERNATION_PLATFORM
)
72 hibernation_mode
= HIBERNATION_SHUTDOWN
;
74 mutex_unlock(&pm_mutex
);
77 static bool entering_platform_hibernation
;
79 bool system_entering_hibernation(void)
81 return entering_platform_hibernation
;
83 EXPORT_SYMBOL(system_entering_hibernation
);
85 #ifdef CONFIG_PM_DEBUG
86 static void hibernation_debug_sleep(void)
88 printk(KERN_INFO
"hibernation debug: Waiting for 5 seconds.\n");
92 static int hibernation_testmode(int mode
)
94 if (hibernation_mode
== mode
) {
95 hibernation_debug_sleep();
101 static int hibernation_test(int level
)
103 if (pm_test_level
== level
) {
104 hibernation_debug_sleep();
109 #else /* !CONFIG_PM_DEBUG */
110 static int hibernation_testmode(int mode
) { return 0; }
111 static int hibernation_test(int level
) { return 0; }
112 #endif /* !CONFIG_PM_DEBUG */
115 * platform_begin - tell the platform driver that we're starting
119 static int platform_begin(int platform_mode
)
121 return (platform_mode
&& hibernation_ops
) ?
122 hibernation_ops
->begin() : 0;
126 * platform_end - tell the platform driver that we've entered the
130 static void platform_end(int platform_mode
)
132 if (platform_mode
&& hibernation_ops
)
133 hibernation_ops
->end();
137 * platform_pre_snapshot - prepare the machine for hibernation using the
138 * platform driver if so configured and return an error code if it fails
141 static int platform_pre_snapshot(int platform_mode
)
143 return (platform_mode
&& hibernation_ops
) ?
144 hibernation_ops
->pre_snapshot() : 0;
148 * platform_leave - prepare the machine for switching to the normal mode
149 * of operation using the platform driver (called with interrupts disabled)
152 static void platform_leave(int platform_mode
)
154 if (platform_mode
&& hibernation_ops
)
155 hibernation_ops
->leave();
159 * platform_finish - switch the machine to the normal mode of operation
160 * using the platform driver (must be called after platform_prepare())
163 static void platform_finish(int platform_mode
)
165 if (platform_mode
&& hibernation_ops
)
166 hibernation_ops
->finish();
170 * platform_pre_restore - prepare the platform for the restoration from a
171 * hibernation image. If the restore fails after this function has been
172 * called, platform_restore_cleanup() must be called.
175 static int platform_pre_restore(int platform_mode
)
177 return (platform_mode
&& hibernation_ops
) ?
178 hibernation_ops
->pre_restore() : 0;
182 * platform_restore_cleanup - switch the platform to the normal mode of
183 * operation after a failing restore. If platform_pre_restore() has been
184 * called before the failing restore, this function must be called too,
185 * regardless of the result of platform_pre_restore().
188 static void platform_restore_cleanup(int platform_mode
)
190 if (platform_mode
&& hibernation_ops
)
191 hibernation_ops
->restore_cleanup();
195 * platform_recover - recover the platform from a failure to suspend
199 static void platform_recover(int platform_mode
)
201 if (platform_mode
&& hibernation_ops
&& hibernation_ops
->recover
)
202 hibernation_ops
->recover();
206 * swsusp_show_speed - print the time elapsed between two events.
207 * @start: Starting event.
208 * @stop: Final event.
209 * @nr_pages - number of pages processed between @start and @stop
210 * @msg - introductory message to print
213 void swsusp_show_speed(struct timeval
*start
, struct timeval
*stop
,
214 unsigned nr_pages
, char *msg
)
216 s64 elapsed_centisecs64
;
221 elapsed_centisecs64
= timeval_to_ns(stop
) - timeval_to_ns(start
);
222 do_div(elapsed_centisecs64
, NSEC_PER_SEC
/ 100);
223 centisecs
= elapsed_centisecs64
;
225 centisecs
= 1; /* avoid div-by-zero */
226 k
= nr_pages
* (PAGE_SIZE
/ 1024);
227 kps
= (k
* 100) / centisecs
;
228 printk(KERN_INFO
"PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
230 centisecs
/ 100, centisecs
% 100,
231 kps
/ 1000, (kps
% 1000) / 10);
235 * create_image - freeze devices that need to be frozen with interrupts
236 * off, create the hibernation image and thaw those devices. Control
237 * reappears in this routine after a restore.
240 static int create_image(int platform_mode
)
244 error
= arch_prepare_suspend();
248 /* At this point, dpm_suspend_start() has been called, but *not*
249 * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
250 * Otherwise, drivers for some devices (e.g. interrupt controllers)
251 * become desynchronized with the actual state of the hardware
252 * at resume time, and evil weirdness ensues.
254 error
= dpm_suspend_noirq(PMSG_FREEZE
);
256 printk(KERN_ERR
"PM: Some devices failed to power down, "
257 "aborting hibernation\n");
261 error
= platform_pre_snapshot(platform_mode
);
262 if (error
|| hibernation_test(TEST_PLATFORM
))
263 goto Platform_finish
;
265 error
= disable_nonboot_cpus();
266 if (error
|| hibernation_test(TEST_CPUS
)
267 || hibernation_testmode(HIBERNATION_TEST
))
272 error
= sysdev_suspend(PMSG_FREEZE
);
274 printk(KERN_ERR
"PM: Some system devices failed to power down, "
275 "aborting hibernation\n");
279 if (hibernation_test(TEST_CORE
))
283 save_processor_state();
284 error
= swsusp_arch_suspend();
286 printk(KERN_ERR
"PM: Error %d creating hibernation image\n",
288 /* Restore control flow magically appears here */
289 restore_processor_state();
291 platform_leave(platform_mode
);
295 /* NOTE: dpm_resume_noirq() is just a resume() for devices
296 * that suspended with irqs off ... no overall powerup.
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 the hibernation
317 * @platform_mode - if set, use the platform driver, if available, to
318 * prepare the platform firmware for the power transition.
320 * Must be called with pm_mutex held
323 int hibernation_snapshot(int platform_mode
)
328 error
= platform_begin(platform_mode
);
332 /* Preallocate image memory before shutting down devices. */
333 error
= hibernate_preallocate_memory();
338 saved_mask
= clear_gfp_allowed_mask(GFP_IOFS
);
339 error
= dpm_suspend_start(PMSG_FREEZE
);
341 goto Recover_platform
;
343 if (hibernation_test(TEST_DEVICES
))
344 goto Recover_platform
;
346 error
= create_image(platform_mode
);
347 /* Control returns here after successful restore */
350 /* We may need to release the preallocated image pages here. */
351 if (error
|| !in_suspend
)
354 dpm_resume_end(in_suspend
?
355 (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
);
356 set_gfp_allowed_mask(saved_mask
);
359 platform_end(platform_mode
);
363 platform_recover(platform_mode
);
368 * resume_target_kernel - prepare devices that need to be suspended with
369 * interrupts off, restore the contents of highmem that have not been
370 * restored yet from the image and run the low level code that will restore
371 * the remaining contents of memory and switch to the just restored target
375 static int resume_target_kernel(bool platform_mode
)
379 error
= dpm_suspend_noirq(PMSG_QUIESCE
);
381 printk(KERN_ERR
"PM: Some devices failed to power down, "
382 "aborting resume\n");
386 error
= platform_pre_restore(platform_mode
);
390 error
= disable_nonboot_cpus();
396 error
= sysdev_suspend(PMSG_QUIESCE
);
400 /* We'll ignore saved state, but this gets preempt count (etc) right */
401 save_processor_state();
402 error
= restore_highmem();
404 error
= swsusp_arch_resume();
406 * The code below is only ever reached in case of a failure.
407 * Otherwise execution continues at place where
408 * swsusp_arch_suspend() was called
411 /* This call to restore_highmem() undos the previous one */
415 * The only reason why swsusp_arch_resume() can fail is memory being
416 * very tight, so we have to free it as soon as we can to avoid
417 * subsequent failures
420 restore_processor_state();
421 touch_softlockup_watchdog();
429 enable_nonboot_cpus();
432 platform_restore_cleanup(platform_mode
);
434 dpm_resume_noirq(PMSG_RECOVER
);
440 * hibernation_restore - quiesce devices and restore the hibernation
441 * snapshot image. If successful, control returns in hibernation_snaphot()
442 * @platform_mode - if set, use the platform driver, if available, to
443 * prepare the platform firmware for the transition.
445 * Must be called with pm_mutex held
448 int hibernation_restore(int platform_mode
)
453 pm_prepare_console();
455 saved_mask
= clear_gfp_allowed_mask(GFP_IOFS
);
456 error
= dpm_suspend_start(PMSG_QUIESCE
);
458 error
= resume_target_kernel(platform_mode
);
459 dpm_resume_end(PMSG_RECOVER
);
461 set_gfp_allowed_mask(saved_mask
);
463 pm_restore_console();
468 * hibernation_platform_enter - enter the hibernation state using the
469 * platform driver (if available)
472 int hibernation_platform_enter(void)
477 if (!hibernation_ops
)
481 * We have cancelled the power transition by running
482 * hibernation_ops->finish() before saving the image, so we should let
483 * the firmware know that we're going to enter the sleep state after all
485 error
= hibernation_ops
->begin();
489 entering_platform_hibernation
= true;
491 saved_mask
= clear_gfp_allowed_mask(GFP_IOFS
);
492 error
= dpm_suspend_start(PMSG_HIBERNATE
);
494 if (hibernation_ops
->recover
)
495 hibernation_ops
->recover();
499 error
= dpm_suspend_noirq(PMSG_HIBERNATE
);
503 error
= hibernation_ops
->prepare();
505 goto Platform_finish
;
507 error
= disable_nonboot_cpus();
509 goto Platform_finish
;
512 sysdev_suspend(PMSG_HIBERNATE
);
513 hibernation_ops
->enter();
514 /* We should never get here */
518 * We don't need to reenable the nonboot CPUs or resume consoles, since
519 * the system is going to be halted anyway.
522 hibernation_ops
->finish();
524 dpm_suspend_noirq(PMSG_RESTORE
);
527 entering_platform_hibernation
= false;
528 dpm_resume_end(PMSG_RESTORE
);
529 set_gfp_allowed_mask(saved_mask
);
533 hibernation_ops
->end();
539 * power_down - Shut the machine down for hibernation.
541 * Use the platform driver, if configured so; otherwise try
542 * to power off or reboot.
545 static void power_down(void)
547 switch (hibernation_mode
) {
548 case HIBERNATION_TEST
:
549 case HIBERNATION_TESTPROC
:
551 case HIBERNATION_REBOOT
:
552 kernel_restart(NULL
);
554 case HIBERNATION_PLATFORM
:
555 hibernation_platform_enter();
556 case HIBERNATION_SHUTDOWN
:
562 * Valid image is on the disk, if we continue we risk serious data
563 * corruption after resume.
565 printk(KERN_CRIT
"PM: Please power down manually\n");
569 static int prepare_processes(void)
573 if (freeze_processes()) {
581 * hibernate - The granpappy of the built-in hibernation management
588 mutex_lock(&pm_mutex
);
589 /* The snapshot device should not be opened while we're running */
590 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
595 pm_prepare_console();
596 error
= pm_notifier_call_chain(PM_HIBERNATION_PREPARE
);
600 error
= usermodehelper_disable();
604 /* Allocate memory management structures */
605 error
= create_basic_memory_bitmaps();
609 printk(KERN_INFO
"PM: Syncing filesystems ... ");
613 error
= prepare_processes();
617 if (hibernation_test(TEST_FREEZER
))
620 if (hibernation_testmode(HIBERNATION_TESTPROC
))
623 error
= hibernation_snapshot(hibernation_mode
== HIBERNATION_PLATFORM
);
628 unsigned int flags
= 0;
630 if (hibernation_mode
== HIBERNATION_PLATFORM
)
631 flags
|= SF_PLATFORM_MODE
;
632 pr_debug("PM: writing image.\n");
633 error
= swsusp_write(flags
);
638 pr_debug("PM: Image restored successfully.\n");
644 free_basic_memory_bitmaps();
645 usermodehelper_enable();
647 pm_notifier_call_chain(PM_POST_HIBERNATION
);
648 pm_restore_console();
649 atomic_inc(&snapshot_device_available
);
651 mutex_unlock(&pm_mutex
);
657 * software_resume - Resume from a saved image.
659 * Called as a late_initcall (so all devices are discovered and
660 * initialized), we call swsusp to see if we have a saved image or not.
661 * If so, we quiesce devices, the restore the saved image. We will
662 * return above (in hibernate() ) if everything goes well.
663 * Otherwise, we fail gracefully and return to the normally
668 static int software_resume(void)
674 * If the user said "noresume".. bail out early.
680 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
681 * is configured into the kernel. Since the regular hibernate
682 * trigger path is via sysfs which takes a buffer mutex before
683 * calling hibernate functions (which take pm_mutex) this can
684 * cause lockdep to complain about a possible ABBA deadlock
685 * which cannot happen since we're in the boot code here and
686 * sysfs can't be invoked yet. Therefore, we use a subclass
687 * here to avoid lockdep complaining.
689 mutex_lock_nested(&pm_mutex
, SINGLE_DEPTH_NESTING
);
691 if (swsusp_resume_device
)
694 if (!strlen(resume_file
)) {
699 pr_debug("PM: Checking image partition %s\n", resume_file
);
701 /* Check if the device is there */
702 swsusp_resume_device
= name_to_dev_t(resume_file
);
703 if (!swsusp_resume_device
) {
705 * Some device discovery might still be in progress; we need
706 * to wait for this to finish.
708 wait_for_device_probe();
710 * We can't depend on SCSI devices being available after loading
711 * one of their modules until scsi_complete_async_scans() is
712 * called and the resume device usually is a SCSI one.
714 scsi_complete_async_scans();
716 swsusp_resume_device
= name_to_dev_t(resume_file
);
717 if (!swsusp_resume_device
) {
724 pr_debug("PM: Resume from partition %d:%d\n",
725 MAJOR(swsusp_resume_device
), MINOR(swsusp_resume_device
));
727 pr_debug("PM: Checking hibernation image.\n");
728 error
= swsusp_check();
732 /* The snapshot device should not be opened while we're running */
733 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
735 swsusp_close(FMODE_READ
);
739 pm_prepare_console();
740 error
= pm_notifier_call_chain(PM_RESTORE_PREPARE
);
744 error
= usermodehelper_disable();
748 error
= create_basic_memory_bitmaps();
752 pr_debug("PM: Preparing processes for restore.\n");
753 error
= prepare_processes();
755 swsusp_close(FMODE_READ
);
759 pr_debug("PM: Reading hibernation image.\n");
761 error
= swsusp_read(&flags
);
762 swsusp_close(FMODE_READ
);
764 hibernation_restore(flags
& SF_PLATFORM_MODE
);
766 printk(KERN_ERR
"PM: Restore failed, recovering.\n");
770 free_basic_memory_bitmaps();
771 usermodehelper_enable();
773 pm_notifier_call_chain(PM_POST_RESTORE
);
774 pm_restore_console();
775 atomic_inc(&snapshot_device_available
);
776 /* For success case, the suspend path will release the lock */
778 mutex_unlock(&pm_mutex
);
779 pr_debug("PM: Resume from disk failed.\n");
782 swsusp_close(FMODE_READ
);
786 late_initcall(software_resume
);
789 static const char * const hibernation_modes
[] = {
790 [HIBERNATION_PLATFORM
] = "platform",
791 [HIBERNATION_SHUTDOWN
] = "shutdown",
792 [HIBERNATION_REBOOT
] = "reboot",
793 [HIBERNATION_TEST
] = "test",
794 [HIBERNATION_TESTPROC
] = "testproc",
798 * disk - Control hibernation mode
800 * Suspend-to-disk can be handled in several ways. We have a few options
801 * for putting the system to sleep - using the platform driver (e.g. ACPI
802 * or other hibernation_ops), powering off the system or rebooting the
803 * system (for testing) as well as the two test modes.
805 * The system can support 'platform', and that is known a priori (and
806 * encoded by the presence of hibernation_ops). However, the user may
807 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
808 * test modes, 'test' or 'testproc'.
810 * show() will display what the mode is currently set to.
811 * store() will accept one of
819 * It will only change to 'platform' if the system
820 * supports it (as determined by having hibernation_ops).
823 static ssize_t
disk_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
829 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
830 if (!hibernation_modes
[i
])
833 case HIBERNATION_SHUTDOWN
:
834 case HIBERNATION_REBOOT
:
835 case HIBERNATION_TEST
:
836 case HIBERNATION_TESTPROC
:
838 case HIBERNATION_PLATFORM
:
841 /* not a valid mode, continue with loop */
844 if (i
== hibernation_mode
)
845 buf
+= sprintf(buf
, "[%s] ", hibernation_modes
[i
]);
847 buf
+= sprintf(buf
, "%s ", hibernation_modes
[i
]);
849 buf
+= sprintf(buf
, "\n");
854 static ssize_t
disk_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
855 const char *buf
, size_t n
)
861 int mode
= HIBERNATION_INVALID
;
863 p
= memchr(buf
, '\n', n
);
864 len
= p
? p
- buf
: n
;
866 mutex_lock(&pm_mutex
);
867 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
868 if (len
== strlen(hibernation_modes
[i
])
869 && !strncmp(buf
, hibernation_modes
[i
], len
)) {
874 if (mode
!= HIBERNATION_INVALID
) {
876 case HIBERNATION_SHUTDOWN
:
877 case HIBERNATION_REBOOT
:
878 case HIBERNATION_TEST
:
879 case HIBERNATION_TESTPROC
:
880 hibernation_mode
= mode
;
882 case HIBERNATION_PLATFORM
:
884 hibernation_mode
= mode
;
892 pr_debug("PM: Hibernation mode set to '%s'\n",
893 hibernation_modes
[mode
]);
894 mutex_unlock(&pm_mutex
);
895 return error
? error
: n
;
900 static ssize_t
resume_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
903 return sprintf(buf
,"%d:%d\n", MAJOR(swsusp_resume_device
),
904 MINOR(swsusp_resume_device
));
907 static ssize_t
resume_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
908 const char *buf
, size_t n
)
910 unsigned int maj
, min
;
914 if (sscanf(buf
, "%u:%u", &maj
, &min
) != 2)
917 res
= MKDEV(maj
,min
);
918 if (maj
!= MAJOR(res
) || min
!= MINOR(res
))
921 mutex_lock(&pm_mutex
);
922 swsusp_resume_device
= res
;
923 mutex_unlock(&pm_mutex
);
924 printk(KERN_INFO
"PM: Starting manual resume from disk\n");
934 static ssize_t
image_size_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
937 return sprintf(buf
, "%lu\n", image_size
);
940 static ssize_t
image_size_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
941 const char *buf
, size_t n
)
945 if (sscanf(buf
, "%lu", &size
) == 1) {
953 power_attr(image_size
);
955 static struct attribute
* g
[] = {
958 &image_size_attr
.attr
,
963 static struct attribute_group attr_group
= {
968 static int __init
pm_disk_init(void)
970 return sysfs_create_group(power_kobj
, &attr_group
);
973 core_initcall(pm_disk_init
);
976 static int __init
resume_setup(char *str
)
981 strncpy( resume_file
, str
, 255 );
985 static int __init
resume_offset_setup(char *str
)
987 unsigned long long offset
;
992 if (sscanf(str
, "%llu", &offset
) == 1)
993 swsusp_resume_block
= offset
;
998 static int __init
noresume_setup(char *str
)
1004 __setup("noresume", noresume_setup
);
1005 __setup("resume_offset=", resume_offset_setup
);
1006 __setup("resume=", resume_setup
);