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 represented by
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 int centisecs
= pm_time_elapsed(start
, stop
) / 10;
221 centisecs
= 1; /* avoid div-by-zero */
222 k
= nr_pages
* (PAGE_SIZE
/ 1024);
223 kps
= (k
* 100) / centisecs
;
224 printk(KERN_INFO
"PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
226 centisecs
/ 100, centisecs
% 100,
227 kps
/ 1000, (kps
% 1000) / 10);
231 * create_image - freeze devices that need to be frozen with interrupts
232 * off, create the hibernation image and thaw those devices. Control
233 * reappears in this routine after a restore.
236 static int create_image(int platform_mode
)
240 error
= arch_prepare_suspend();
244 /* At this point, dpm_suspend_start() has been called, but *not*
245 * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
246 * Otherwise, drivers for some devices (e.g. interrupt controllers)
247 * become desynchronized with the actual state of the hardware
248 * at resume time, and evil weirdness ensues.
250 error
= dpm_suspend_noirq(PMSG_FREEZE
);
252 printk(KERN_ERR
"PM: Some devices failed to power down, "
253 "aborting hibernation\n");
257 error
= platform_pre_snapshot(platform_mode
);
258 if (error
|| hibernation_test(TEST_PLATFORM
))
259 goto Platform_finish
;
261 error
= disable_nonboot_cpus();
262 if (error
|| hibernation_test(TEST_CPUS
)
263 || hibernation_testmode(HIBERNATION_TEST
))
268 error
= sysdev_suspend(PMSG_FREEZE
);
270 printk(KERN_ERR
"PM: Some system devices failed to power down, "
271 "aborting hibernation\n");
275 if (hibernation_test(TEST_CORE
))
279 save_processor_state();
280 error
= swsusp_arch_suspend();
282 printk(KERN_ERR
"PM: Error %d creating hibernation image\n",
284 /* Restore control flow magically appears here */
285 restore_processor_state();
287 platform_leave(platform_mode
);
291 /* NOTE: dpm_resume_noirq() is just a resume() for devices
292 * that suspended with irqs off ... no overall powerup.
299 enable_nonboot_cpus();
302 platform_finish(platform_mode
);
304 dpm_resume_noirq(in_suspend
?
305 (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
);
311 * hibernation_snapshot - quiesce devices and create the hibernation
313 * @platform_mode - if set, use the platform driver, if available, to
314 * prepare the platform firmware for the power transition.
316 * Must be called with pm_mutex held
319 int hibernation_snapshot(int platform_mode
)
323 error
= platform_begin(platform_mode
);
327 /* Preallocate image memory before shutting down devices. */
328 error
= hibernate_preallocate_memory();
333 error
= dpm_suspend_start(PMSG_FREEZE
);
335 goto Recover_platform
;
337 if (hibernation_test(TEST_DEVICES
))
338 goto Recover_platform
;
340 error
= create_image(platform_mode
);
341 /* Control returns here after successful restore */
344 /* We may need to release the preallocated image pages here. */
345 if (error
|| !in_suspend
)
348 dpm_resume_end(in_suspend
?
349 (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
);
352 platform_end(platform_mode
);
356 platform_recover(platform_mode
);
361 * resume_target_kernel - prepare devices that need to be suspended with
362 * interrupts off, restore the contents of highmem that have not been
363 * restored yet from the image and run the low level code that will restore
364 * the remaining contents of memory and switch to the just restored target
368 static int resume_target_kernel(bool platform_mode
)
372 error
= dpm_suspend_noirq(PMSG_QUIESCE
);
374 printk(KERN_ERR
"PM: Some devices failed to power down, "
375 "aborting resume\n");
379 error
= platform_pre_restore(platform_mode
);
383 error
= disable_nonboot_cpus();
389 error
= sysdev_suspend(PMSG_QUIESCE
);
393 /* We'll ignore saved state, but this gets preempt count (etc) right */
394 save_processor_state();
395 error
= restore_highmem();
397 error
= swsusp_arch_resume();
399 * The code below is only ever reached in case of a failure.
400 * Otherwise execution continues at place where
401 * swsusp_arch_suspend() was called
404 /* This call to restore_highmem() undos the previous one */
408 * The only reason why swsusp_arch_resume() can fail is memory being
409 * very tight, so we have to free it as soon as we can to avoid
410 * subsequent failures
413 restore_processor_state();
414 touch_softlockup_watchdog();
422 enable_nonboot_cpus();
425 platform_restore_cleanup(platform_mode
);
427 dpm_resume_noirq(PMSG_RECOVER
);
433 * hibernation_restore - quiesce devices and restore the hibernation
434 * snapshot image. If successful, control returns in hibernation_snaphot()
435 * @platform_mode - if set, use the platform driver, if available, to
436 * prepare the platform firmware for the transition.
438 * Must be called with pm_mutex held
441 int hibernation_restore(int platform_mode
)
445 pm_prepare_console();
447 error
= dpm_suspend_start(PMSG_QUIESCE
);
449 error
= resume_target_kernel(platform_mode
);
450 dpm_resume_end(PMSG_RECOVER
);
453 pm_restore_console();
458 * hibernation_platform_enter - enter the hibernation state using the
459 * platform driver (if available)
462 int hibernation_platform_enter(void)
466 if (!hibernation_ops
)
470 * We have cancelled the power transition by running
471 * hibernation_ops->finish() before saving the image, so we should let
472 * the firmware know that we're going to enter the sleep state after all
474 error
= hibernation_ops
->begin();
478 entering_platform_hibernation
= true;
480 error
= dpm_suspend_start(PMSG_HIBERNATE
);
482 if (hibernation_ops
->recover
)
483 hibernation_ops
->recover();
487 error
= dpm_suspend_noirq(PMSG_HIBERNATE
);
491 error
= hibernation_ops
->prepare();
493 goto Platform_finish
;
495 error
= disable_nonboot_cpus();
497 goto Platform_finish
;
500 sysdev_suspend(PMSG_HIBERNATE
);
501 hibernation_ops
->enter();
502 /* We should never get here */
506 * We don't need to reenable the nonboot CPUs or resume consoles, since
507 * the system is going to be halted anyway.
510 hibernation_ops
->finish();
512 dpm_suspend_noirq(PMSG_RESTORE
);
515 entering_platform_hibernation
= false;
516 dpm_resume_end(PMSG_RESTORE
);
520 hibernation_ops
->end();
526 * power_down - Shut the machine down for hibernation.
528 * Use the platform driver, if configured so; otherwise try
529 * to power off or reboot.
532 static void power_down(void)
534 switch (hibernation_mode
) {
535 case HIBERNATION_TEST
:
536 case HIBERNATION_TESTPROC
:
538 case HIBERNATION_REBOOT
:
539 kernel_restart(NULL
);
541 case HIBERNATION_PLATFORM
:
542 hibernation_platform_enter();
543 case HIBERNATION_SHUTDOWN
:
549 * Valid image is on the disk, if we continue we risk serious data
550 * corruption after resume.
552 printk(KERN_CRIT
"PM: Please power down manually\n");
556 static int prepare_processes(void)
560 if (freeze_processes()) {
568 * hibernate - The granpappy of the built-in hibernation management
575 mutex_lock(&pm_mutex
);
576 /* The snapshot device should not be opened while we're running */
577 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
582 pm_prepare_console();
583 error
= pm_notifier_call_chain(PM_HIBERNATION_PREPARE
);
587 error
= usermodehelper_disable();
591 /* Allocate memory management structures */
592 error
= create_basic_memory_bitmaps();
596 printk(KERN_INFO
"PM: Syncing filesystems ... ");
600 error
= prepare_processes();
604 if (hibernation_test(TEST_FREEZER
))
607 if (hibernation_testmode(HIBERNATION_TESTPROC
))
610 error
= hibernation_snapshot(hibernation_mode
== HIBERNATION_PLATFORM
);
615 unsigned int flags
= 0;
617 if (hibernation_mode
== HIBERNATION_PLATFORM
)
618 flags
|= SF_PLATFORM_MODE
;
619 pr_debug("PM: writing image.\n");
620 error
= swsusp_write(flags
);
625 pr_debug("PM: Image restored successfully.\n");
631 free_basic_memory_bitmaps();
632 usermodehelper_enable();
634 pm_notifier_call_chain(PM_POST_HIBERNATION
);
635 pm_restore_console();
636 atomic_inc(&snapshot_device_available
);
638 mutex_unlock(&pm_mutex
);
644 * software_resume - Resume from a saved image.
646 * Called as a late_initcall (so all devices are discovered and
647 * initialized), we call swsusp to see if we have a saved image or not.
648 * If so, we quiesce devices, the restore the saved image. We will
649 * return above (in hibernate() ) if everything goes well.
650 * Otherwise, we fail gracefully and return to the normally
655 static int software_resume(void)
661 * If the user said "noresume".. bail out early.
667 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
668 * is configured into the kernel. Since the regular hibernate
669 * trigger path is via sysfs which takes a buffer mutex before
670 * calling hibernate functions (which take pm_mutex) this can
671 * cause lockdep to complain about a possible ABBA deadlock
672 * which cannot happen since we're in the boot code here and
673 * sysfs can't be invoked yet. Therefore, we use a subclass
674 * here to avoid lockdep complaining.
676 mutex_lock_nested(&pm_mutex
, SINGLE_DEPTH_NESTING
);
678 if (swsusp_resume_device
)
681 if (!strlen(resume_file
)) {
686 pr_debug("PM: Checking image partition %s\n", resume_file
);
688 /* Check if the device is there */
689 swsusp_resume_device
= name_to_dev_t(resume_file
);
690 if (!swsusp_resume_device
) {
692 * Some device discovery might still be in progress; we need
693 * to wait for this to finish.
695 wait_for_device_probe();
697 * We can't depend on SCSI devices being available after loading
698 * one of their modules until scsi_complete_async_scans() is
699 * called and the resume device usually is a SCSI one.
701 scsi_complete_async_scans();
703 swsusp_resume_device
= name_to_dev_t(resume_file
);
704 if (!swsusp_resume_device
) {
711 pr_debug("PM: Resume from partition %d:%d\n",
712 MAJOR(swsusp_resume_device
), MINOR(swsusp_resume_device
));
714 pr_debug("PM: Checking hibernation image.\n");
715 error
= swsusp_check();
719 /* The snapshot device should not be opened while we're running */
720 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
722 swsusp_close(FMODE_READ
);
726 pm_prepare_console();
727 error
= pm_notifier_call_chain(PM_RESTORE_PREPARE
);
731 error
= usermodehelper_disable();
735 error
= create_basic_memory_bitmaps();
739 pr_debug("PM: Preparing processes for restore.\n");
740 error
= prepare_processes();
742 swsusp_close(FMODE_READ
);
746 pr_debug("PM: Reading hibernation image.\n");
748 error
= swsusp_read(&flags
);
749 swsusp_close(FMODE_READ
);
751 hibernation_restore(flags
& SF_PLATFORM_MODE
);
753 printk(KERN_ERR
"PM: Restore failed, recovering.\n");
757 free_basic_memory_bitmaps();
758 usermodehelper_enable();
760 pm_notifier_call_chain(PM_POST_RESTORE
);
761 pm_restore_console();
762 atomic_inc(&snapshot_device_available
);
763 /* For success case, the suspend path will release the lock */
765 mutex_unlock(&pm_mutex
);
766 pr_debug("PM: Resume from disk failed.\n");
769 swsusp_close(FMODE_READ
);
773 late_initcall(software_resume
);
776 static const char * const hibernation_modes
[] = {
777 [HIBERNATION_PLATFORM
] = "platform",
778 [HIBERNATION_SHUTDOWN
] = "shutdown",
779 [HIBERNATION_REBOOT
] = "reboot",
780 [HIBERNATION_TEST
] = "test",
781 [HIBERNATION_TESTPROC
] = "testproc",
785 * disk - Control hibernation mode
787 * Suspend-to-disk can be handled in several ways. We have a few options
788 * for putting the system to sleep - using the platform driver (e.g. ACPI
789 * or other hibernation_ops), powering off the system or rebooting the
790 * system (for testing) as well as the two test modes.
792 * The system can support 'platform', and that is known a priori (and
793 * encoded by the presence of hibernation_ops). However, the user may
794 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
795 * test modes, 'test' or 'testproc'.
797 * show() will display what the mode is currently set to.
798 * store() will accept one of
806 * It will only change to 'platform' if the system
807 * supports it (as determined by having hibernation_ops).
810 static ssize_t
disk_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
816 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
817 if (!hibernation_modes
[i
])
820 case HIBERNATION_SHUTDOWN
:
821 case HIBERNATION_REBOOT
:
822 case HIBERNATION_TEST
:
823 case HIBERNATION_TESTPROC
:
825 case HIBERNATION_PLATFORM
:
828 /* not a valid mode, continue with loop */
831 if (i
== hibernation_mode
)
832 buf
+= sprintf(buf
, "[%s] ", hibernation_modes
[i
]);
834 buf
+= sprintf(buf
, "%s ", hibernation_modes
[i
]);
836 buf
+= sprintf(buf
, "\n");
841 static ssize_t
disk_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
842 const char *buf
, size_t n
)
848 int mode
= HIBERNATION_INVALID
;
850 p
= memchr(buf
, '\n', n
);
851 len
= p
? p
- buf
: n
;
853 mutex_lock(&pm_mutex
);
854 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
855 if (len
== strlen(hibernation_modes
[i
])
856 && !strncmp(buf
, hibernation_modes
[i
], len
)) {
861 if (mode
!= HIBERNATION_INVALID
) {
863 case HIBERNATION_SHUTDOWN
:
864 case HIBERNATION_REBOOT
:
865 case HIBERNATION_TEST
:
866 case HIBERNATION_TESTPROC
:
867 hibernation_mode
= mode
;
869 case HIBERNATION_PLATFORM
:
871 hibernation_mode
= mode
;
879 pr_debug("PM: Hibernation mode set to '%s'\n",
880 hibernation_modes
[mode
]);
881 mutex_unlock(&pm_mutex
);
882 return error
? error
: n
;
887 static ssize_t
resume_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
890 return sprintf(buf
,"%d:%d\n", MAJOR(swsusp_resume_device
),
891 MINOR(swsusp_resume_device
));
894 static ssize_t
resume_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
895 const char *buf
, size_t n
)
897 unsigned int maj
, min
;
901 if (sscanf(buf
, "%u:%u", &maj
, &min
) != 2)
904 res
= MKDEV(maj
,min
);
905 if (maj
!= MAJOR(res
) || min
!= MINOR(res
))
908 mutex_lock(&pm_mutex
);
909 swsusp_resume_device
= res
;
910 mutex_unlock(&pm_mutex
);
911 printk(KERN_INFO
"PM: Starting manual resume from disk\n");
921 static ssize_t
image_size_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
924 return sprintf(buf
, "%lu\n", image_size
);
927 static ssize_t
image_size_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
928 const char *buf
, size_t n
)
932 if (sscanf(buf
, "%lu", &size
) == 1) {
940 power_attr(image_size
);
942 static struct attribute
* g
[] = {
945 &image_size_attr
.attr
,
950 static struct attribute_group attr_group
= {
955 static int __init
pm_disk_init(void)
957 return sysfs_create_group(power_kobj
, &attr_group
);
960 core_initcall(pm_disk_init
);
963 static int __init
resume_setup(char *str
)
968 strncpy( resume_file
, str
, 255 );
972 static int __init
resume_offset_setup(char *str
)
974 unsigned long long offset
;
979 if (sscanf(str
, "%llu", &offset
) == 1)
980 swsusp_resume_block
= offset
;
985 static int __init
noresume_setup(char *str
)
991 __setup("noresume", noresume_setup
);
992 __setup("resume_offset=", resume_offset_setup
);
993 __setup("resume=", resume_setup
);