2 * kernel/power/disk.c - 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>
8 * 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 <asm/suspend.h>
30 static int noresume
= 0;
31 static char resume_file
[256] = CONFIG_PM_STD_PARTITION
;
32 dev_t swsusp_resume_device
;
33 sector_t swsusp_resume_block
;
43 __HIBERNATION_AFTER_LAST
45 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
46 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
48 static int hibernation_mode
= HIBERNATION_SHUTDOWN
;
50 static struct platform_hibernation_ops
*hibernation_ops
;
53 * hibernation_set_ops - set the global hibernate operations
54 * @ops: the hibernation operations to use in subsequent hibernation transitions
57 void hibernation_set_ops(struct platform_hibernation_ops
*ops
)
59 if (ops
&& !(ops
->begin
&& ops
->end
&& ops
->pre_snapshot
60 && ops
->prepare
&& ops
->finish
&& ops
->enter
&& ops
->pre_restore
61 && ops
->restore_cleanup
)) {
65 mutex_lock(&pm_mutex
);
66 hibernation_ops
= ops
;
68 hibernation_mode
= HIBERNATION_PLATFORM
;
69 else if (hibernation_mode
== HIBERNATION_PLATFORM
)
70 hibernation_mode
= HIBERNATION_SHUTDOWN
;
72 mutex_unlock(&pm_mutex
);
75 static bool entering_platform_hibernation
;
77 bool system_entering_hibernation(void)
79 return entering_platform_hibernation
;
81 EXPORT_SYMBOL(system_entering_hibernation
);
83 #ifdef CONFIG_PM_DEBUG
84 static void hibernation_debug_sleep(void)
86 printk(KERN_INFO
"hibernation debug: Waiting for 5 seconds.\n");
90 static int hibernation_testmode(int mode
)
92 if (hibernation_mode
== mode
) {
93 hibernation_debug_sleep();
99 static int hibernation_test(int level
)
101 if (pm_test_level
== level
) {
102 hibernation_debug_sleep();
107 #else /* !CONFIG_PM_DEBUG */
108 static int hibernation_testmode(int mode
) { return 0; }
109 static int hibernation_test(int level
) { return 0; }
110 #endif /* !CONFIG_PM_DEBUG */
113 * platform_begin - tell the platform driver that we're starting
117 static int platform_begin(int platform_mode
)
119 return (platform_mode
&& hibernation_ops
) ?
120 hibernation_ops
->begin() : 0;
124 * platform_end - tell the platform driver that we've entered the
128 static void platform_end(int platform_mode
)
130 if (platform_mode
&& hibernation_ops
)
131 hibernation_ops
->end();
135 * platform_pre_snapshot - prepare the machine for hibernation using the
136 * platform driver if so configured and return an error code if it fails
139 static int platform_pre_snapshot(int platform_mode
)
141 return (platform_mode
&& hibernation_ops
) ?
142 hibernation_ops
->pre_snapshot() : 0;
146 * platform_leave - prepare the machine for switching to the normal mode
147 * of operation using the platform driver (called with interrupts disabled)
150 static void platform_leave(int platform_mode
)
152 if (platform_mode
&& hibernation_ops
)
153 hibernation_ops
->leave();
157 * platform_finish - switch the machine to the normal mode of operation
158 * using the platform driver (must be called after platform_prepare())
161 static void platform_finish(int platform_mode
)
163 if (platform_mode
&& hibernation_ops
)
164 hibernation_ops
->finish();
168 * platform_pre_restore - prepare the platform for the restoration from a
169 * hibernation image. If the restore fails after this function has been
170 * called, platform_restore_cleanup() must be called.
173 static int platform_pre_restore(int platform_mode
)
175 return (platform_mode
&& hibernation_ops
) ?
176 hibernation_ops
->pre_restore() : 0;
180 * platform_restore_cleanup - switch the platform to the normal mode of
181 * operation after a failing restore. If platform_pre_restore() has been
182 * called before the failing restore, this function must be called too,
183 * regardless of the result of platform_pre_restore().
186 static void platform_restore_cleanup(int platform_mode
)
188 if (platform_mode
&& hibernation_ops
)
189 hibernation_ops
->restore_cleanup();
193 * platform_recover - recover the platform from a failure to suspend
197 static void platform_recover(int platform_mode
)
199 if (platform_mode
&& hibernation_ops
&& hibernation_ops
->recover
)
200 hibernation_ops
->recover();
204 * create_image - freeze devices that need to be frozen with interrupts
205 * off, create the hibernation image and thaw those devices. Control
206 * reappears in this routine after a restore.
209 static int create_image(int platform_mode
)
213 error
= arch_prepare_suspend();
219 /* At this point, device_suspend() has been called, but *not*
220 * device_power_down(). We *must* call device_power_down() now.
221 * Otherwise, drivers for some devices (e.g. interrupt controllers)
222 * become desynchronized with the actual state of the hardware
223 * at resume time, and evil weirdness ensues.
225 error
= device_power_down(PMSG_FREEZE
);
227 printk(KERN_ERR
"PM: Some devices failed to power down, "
228 "aborting hibernation\n");
232 error
= platform_pre_snapshot(platform_mode
);
233 if (error
|| hibernation_test(TEST_PLATFORM
))
234 goto Platform_finish
;
236 error
= disable_nonboot_cpus();
237 if (error
|| hibernation_test(TEST_CPUS
)
238 || hibernation_testmode(HIBERNATION_TEST
))
243 sysdev_suspend(PMSG_FREEZE
);
245 printk(KERN_ERR
"PM: Some devices failed to power down, "
246 "aborting hibernation\n");
250 if (hibernation_test(TEST_CORE
))
254 save_processor_state();
255 error
= swsusp_arch_suspend();
257 printk(KERN_ERR
"PM: Error %d creating hibernation image\n",
259 /* Restore control flow magically appears here */
260 restore_processor_state();
262 platform_leave(platform_mode
);
266 /* NOTE: device_power_up() is just a resume() for devices
267 * that suspended with irqs off ... no overall powerup.
274 enable_nonboot_cpus();
277 platform_finish(platform_mode
);
279 device_power_up(in_suspend
?
280 (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
);
289 * hibernation_snapshot - quiesce devices and create the hibernation
291 * @platform_mode - if set, use the platform driver, if available, to
292 * prepare the platform firmware for the power transition.
294 * Must be called with pm_mutex held
297 int hibernation_snapshot(int platform_mode
)
301 error
= platform_begin(platform_mode
);
305 /* Free memory before shutting down devices. */
306 error
= swsusp_shrink_memory();
311 error
= device_suspend(PMSG_FREEZE
);
313 goto Recover_platform
;
315 if (hibernation_test(TEST_DEVICES
))
316 goto Recover_platform
;
318 error
= create_image(platform_mode
);
319 /* Control returns here after successful restore */
322 device_resume(in_suspend
?
323 (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
);
326 platform_end(platform_mode
);
330 platform_recover(platform_mode
);
335 * resume_target_kernel - prepare devices that need to be suspended with
336 * interrupts off, restore the contents of highmem that have not been
337 * restored yet from the image and run the low level code that will restore
338 * the remaining contents of memory and switch to the just restored target
342 static int resume_target_kernel(bool platform_mode
)
348 error
= device_power_down(PMSG_QUIESCE
);
350 printk(KERN_ERR
"PM: Some devices failed to power down, "
351 "aborting resume\n");
355 error
= platform_pre_restore(platform_mode
);
359 error
= disable_nonboot_cpus();
365 error
= sysdev_suspend(PMSG_QUIESCE
);
369 /* We'll ignore saved state, but this gets preempt count (etc) right */
370 save_processor_state();
371 error
= restore_highmem();
373 error
= swsusp_arch_resume();
375 * The code below is only ever reached in case of a failure.
376 * Otherwise execution continues at place where
377 * swsusp_arch_suspend() was called
380 /* This call to restore_highmem() undos the previous one */
384 * The only reason why swsusp_arch_resume() can fail is memory being
385 * very tight, so we have to free it as soon as we can to avoid
386 * subsequent failures
389 restore_processor_state();
390 touch_softlockup_watchdog();
398 enable_nonboot_cpus();
401 platform_restore_cleanup(platform_mode
);
403 device_power_up(PMSG_RECOVER
);
412 * hibernation_restore - quiesce devices and restore the hibernation
413 * snapshot image. If successful, control returns in hibernation_snaphot()
414 * @platform_mode - if set, use the platform driver, if available, to
415 * prepare the platform firmware for the transition.
417 * Must be called with pm_mutex held
420 int hibernation_restore(int platform_mode
)
424 pm_prepare_console();
426 error
= device_suspend(PMSG_QUIESCE
);
428 error
= resume_target_kernel(platform_mode
);
429 device_resume(PMSG_RECOVER
);
432 pm_restore_console();
437 * hibernation_platform_enter - enter the hibernation state using the
438 * platform driver (if available)
441 int hibernation_platform_enter(void)
445 if (!hibernation_ops
)
449 * We have cancelled the power transition by running
450 * hibernation_ops->finish() before saving the image, so we should let
451 * the firmware know that we're going to enter the sleep state after all
453 error
= hibernation_ops
->begin();
457 entering_platform_hibernation
= true;
459 error
= device_suspend(PMSG_HIBERNATE
);
461 if (hibernation_ops
->recover
)
462 hibernation_ops
->recover();
468 error
= device_power_down(PMSG_HIBERNATE
);
472 error
= hibernation_ops
->prepare();
474 goto Platofrm_finish
;
476 error
= disable_nonboot_cpus();
478 goto Platofrm_finish
;
481 sysdev_suspend(PMSG_HIBERNATE
);
482 hibernation_ops
->enter();
483 /* We should never get here */
487 * We don't need to reenable the nonboot CPUs or resume consoles, since
488 * the system is going to be halted anyway.
491 hibernation_ops
->finish();
493 device_power_up(PMSG_RESTORE
);
499 entering_platform_hibernation
= false;
500 device_resume(PMSG_RESTORE
);
504 hibernation_ops
->end();
510 * power_down - Shut the machine down for hibernation.
512 * Use the platform driver, if configured so; otherwise try
513 * to power off or reboot.
516 static void power_down(void)
518 switch (hibernation_mode
) {
519 case HIBERNATION_TEST
:
520 case HIBERNATION_TESTPROC
:
522 case HIBERNATION_REBOOT
:
523 kernel_restart(NULL
);
525 case HIBERNATION_PLATFORM
:
526 hibernation_platform_enter();
527 case HIBERNATION_SHUTDOWN
:
533 * Valid image is on the disk, if we continue we risk serious data
534 * corruption after resume.
536 printk(KERN_CRIT
"PM: Please power down manually\n");
540 static int prepare_processes(void)
544 if (freeze_processes()) {
552 * hibernate - The granpappy of the built-in hibernation management
559 mutex_lock(&pm_mutex
);
560 /* The snapshot device should not be opened while we're running */
561 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
566 pm_prepare_console();
567 error
= pm_notifier_call_chain(PM_HIBERNATION_PREPARE
);
571 error
= usermodehelper_disable();
575 /* Allocate memory management structures */
576 error
= create_basic_memory_bitmaps();
580 printk(KERN_INFO
"PM: Syncing filesystems ... ");
584 error
= prepare_processes();
588 if (hibernation_test(TEST_FREEZER
))
591 if (hibernation_testmode(HIBERNATION_TESTPROC
))
594 error
= hibernation_snapshot(hibernation_mode
== HIBERNATION_PLATFORM
);
595 if (in_suspend
&& !error
) {
596 unsigned int flags
= 0;
598 if (hibernation_mode
== HIBERNATION_PLATFORM
)
599 flags
|= SF_PLATFORM_MODE
;
600 pr_debug("PM: writing image.\n");
601 error
= swsusp_write(flags
);
606 pr_debug("PM: Image restored successfully.\n");
612 free_basic_memory_bitmaps();
613 usermodehelper_enable();
615 pm_notifier_call_chain(PM_POST_HIBERNATION
);
616 pm_restore_console();
617 atomic_inc(&snapshot_device_available
);
619 mutex_unlock(&pm_mutex
);
625 * software_resume - Resume from a saved image.
627 * Called as a late_initcall (so all devices are discovered and
628 * initialized), we call swsusp to see if we have a saved image or not.
629 * If so, we quiesce devices, the restore the saved image. We will
630 * return above (in hibernate() ) if everything goes well.
631 * Otherwise, we fail gracefully and return to the normally
636 static int software_resume(void)
642 * If the user said "noresume".. bail out early.
648 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
649 * is configured into the kernel. Since the regular hibernate
650 * trigger path is via sysfs which takes a buffer mutex before
651 * calling hibernate functions (which take pm_mutex) this can
652 * cause lockdep to complain about a possible ABBA deadlock
653 * which cannot happen since we're in the boot code here and
654 * sysfs can't be invoked yet. Therefore, we use a subclass
655 * here to avoid lockdep complaining.
657 mutex_lock_nested(&pm_mutex
, SINGLE_DEPTH_NESTING
);
658 if (!swsusp_resume_device
) {
659 if (!strlen(resume_file
)) {
660 mutex_unlock(&pm_mutex
);
664 * Some device discovery might still be in progress; we need
665 * to wait for this to finish.
667 wait_for_device_probe();
668 swsusp_resume_device
= name_to_dev_t(resume_file
);
669 pr_debug("PM: Resume from partition %s\n", resume_file
);
671 pr_debug("PM: Resume from partition %d:%d\n",
672 MAJOR(swsusp_resume_device
),
673 MINOR(swsusp_resume_device
));
678 * FIXME: If noresume is specified, we need to find the
679 * partition and reset it back to normal swap space.
681 mutex_unlock(&pm_mutex
);
685 pr_debug("PM: Checking hibernation image.\n");
686 error
= swsusp_check();
690 /* The snapshot device should not be opened while we're running */
691 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
696 pm_prepare_console();
697 error
= pm_notifier_call_chain(PM_RESTORE_PREPARE
);
701 error
= usermodehelper_disable();
705 error
= create_basic_memory_bitmaps();
709 pr_debug("PM: Preparing processes for restore.\n");
710 error
= prepare_processes();
712 swsusp_close(FMODE_READ
);
716 pr_debug("PM: Reading hibernation image.\n");
718 error
= swsusp_read(&flags
);
720 hibernation_restore(flags
& SF_PLATFORM_MODE
);
722 printk(KERN_ERR
"PM: Restore failed, recovering.\n");
726 free_basic_memory_bitmaps();
727 usermodehelper_enable();
729 pm_notifier_call_chain(PM_POST_RESTORE
);
730 pm_restore_console();
731 atomic_inc(&snapshot_device_available
);
732 /* For success case, the suspend path will release the lock */
734 mutex_unlock(&pm_mutex
);
735 pr_debug("PM: Resume from disk failed.\n");
739 late_initcall(software_resume
);
742 static const char * const hibernation_modes
[] = {
743 [HIBERNATION_PLATFORM
] = "platform",
744 [HIBERNATION_SHUTDOWN
] = "shutdown",
745 [HIBERNATION_REBOOT
] = "reboot",
746 [HIBERNATION_TEST
] = "test",
747 [HIBERNATION_TESTPROC
] = "testproc",
751 * disk - Control hibernation mode
753 * Suspend-to-disk can be handled in several ways. We have a few options
754 * for putting the system to sleep - using the platform driver (e.g. ACPI
755 * or other hibernation_ops), powering off the system or rebooting the
756 * system (for testing) as well as the two test modes.
758 * The system can support 'platform', and that is known a priori (and
759 * encoded by the presence of hibernation_ops). However, the user may
760 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
761 * test modes, 'test' or 'testproc'.
763 * show() will display what the mode is currently set to.
764 * store() will accept one of
772 * It will only change to 'platform' if the system
773 * supports it (as determined by having hibernation_ops).
776 static ssize_t
disk_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
782 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
783 if (!hibernation_modes
[i
])
786 case HIBERNATION_SHUTDOWN
:
787 case HIBERNATION_REBOOT
:
788 case HIBERNATION_TEST
:
789 case HIBERNATION_TESTPROC
:
791 case HIBERNATION_PLATFORM
:
794 /* not a valid mode, continue with loop */
797 if (i
== hibernation_mode
)
798 buf
+= sprintf(buf
, "[%s] ", hibernation_modes
[i
]);
800 buf
+= sprintf(buf
, "%s ", hibernation_modes
[i
]);
802 buf
+= sprintf(buf
, "\n");
807 static ssize_t
disk_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
808 const char *buf
, size_t n
)
814 int mode
= HIBERNATION_INVALID
;
816 p
= memchr(buf
, '\n', n
);
817 len
= p
? p
- buf
: n
;
819 mutex_lock(&pm_mutex
);
820 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
821 if (len
== strlen(hibernation_modes
[i
])
822 && !strncmp(buf
, hibernation_modes
[i
], len
)) {
827 if (mode
!= HIBERNATION_INVALID
) {
829 case HIBERNATION_SHUTDOWN
:
830 case HIBERNATION_REBOOT
:
831 case HIBERNATION_TEST
:
832 case HIBERNATION_TESTPROC
:
833 hibernation_mode
= mode
;
835 case HIBERNATION_PLATFORM
:
837 hibernation_mode
= mode
;
845 pr_debug("PM: Hibernation mode set to '%s'\n",
846 hibernation_modes
[mode
]);
847 mutex_unlock(&pm_mutex
);
848 return error
? error
: n
;
853 static ssize_t
resume_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
856 return sprintf(buf
,"%d:%d\n", MAJOR(swsusp_resume_device
),
857 MINOR(swsusp_resume_device
));
860 static ssize_t
resume_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
861 const char *buf
, size_t n
)
863 unsigned int maj
, min
;
867 if (sscanf(buf
, "%u:%u", &maj
, &min
) != 2)
870 res
= MKDEV(maj
,min
);
871 if (maj
!= MAJOR(res
) || min
!= MINOR(res
))
874 mutex_lock(&pm_mutex
);
875 swsusp_resume_device
= res
;
876 mutex_unlock(&pm_mutex
);
877 printk(KERN_INFO
"PM: Starting manual resume from disk\n");
887 static ssize_t
image_size_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
890 return sprintf(buf
, "%lu\n", image_size
);
893 static ssize_t
image_size_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
894 const char *buf
, size_t n
)
898 if (sscanf(buf
, "%lu", &size
) == 1) {
906 power_attr(image_size
);
908 static struct attribute
* g
[] = {
911 &image_size_attr
.attr
,
916 static struct attribute_group attr_group
= {
921 static int __init
pm_disk_init(void)
923 return sysfs_create_group(power_kobj
, &attr_group
);
926 core_initcall(pm_disk_init
);
929 static int __init
resume_setup(char *str
)
934 strncpy( resume_file
, str
, 255 );
938 static int __init
resume_offset_setup(char *str
)
940 unsigned long long offset
;
945 if (sscanf(str
, "%llu", &offset
) == 1)
946 swsusp_resume_block
= offset
;
951 static int __init
noresume_setup(char *str
)
957 __setup("noresume", noresume_setup
);
958 __setup("resume_offset=", resume_offset_setup
);
959 __setup("resume=", resume_setup
);