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 <linux/ftrace.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 #ifdef CONFIG_PM_DEBUG
76 static void hibernation_debug_sleep(void)
78 printk(KERN_INFO
"hibernation debug: Waiting for 5 seconds.\n");
82 static int hibernation_testmode(int mode
)
84 if (hibernation_mode
== mode
) {
85 hibernation_debug_sleep();
91 static int hibernation_test(int level
)
93 if (pm_test_level
== level
) {
94 hibernation_debug_sleep();
99 #else /* !CONFIG_PM_DEBUG */
100 static int hibernation_testmode(int mode
) { return 0; }
101 static int hibernation_test(int level
) { return 0; }
102 #endif /* !CONFIG_PM_DEBUG */
105 * platform_begin - tell the platform driver that we're starting
109 static int platform_begin(int platform_mode
)
111 return (platform_mode
&& hibernation_ops
) ?
112 hibernation_ops
->begin() : 0;
116 * platform_end - tell the platform driver that we've entered the
120 static void platform_end(int platform_mode
)
122 if (platform_mode
&& hibernation_ops
)
123 hibernation_ops
->end();
127 * platform_pre_snapshot - prepare the machine for hibernation using the
128 * platform driver if so configured and return an error code if it fails
131 static int platform_pre_snapshot(int platform_mode
)
133 return (platform_mode
&& hibernation_ops
) ?
134 hibernation_ops
->pre_snapshot() : 0;
138 * platform_leave - prepare the machine for switching to the normal mode
139 * of operation using the platform driver (called with interrupts disabled)
142 static void platform_leave(int platform_mode
)
144 if (platform_mode
&& hibernation_ops
)
145 hibernation_ops
->leave();
149 * platform_finish - switch the machine to the normal mode of operation
150 * using the platform driver (must be called after platform_prepare())
153 static void platform_finish(int platform_mode
)
155 if (platform_mode
&& hibernation_ops
)
156 hibernation_ops
->finish();
160 * platform_pre_restore - prepare the platform for the restoration from a
161 * hibernation image. If the restore fails after this function has been
162 * called, platform_restore_cleanup() must be called.
165 static int platform_pre_restore(int platform_mode
)
167 return (platform_mode
&& hibernation_ops
) ?
168 hibernation_ops
->pre_restore() : 0;
172 * platform_restore_cleanup - switch the platform to the normal mode of
173 * operation after a failing restore. If platform_pre_restore() has been
174 * called before the failing restore, this function must be called too,
175 * regardless of the result of platform_pre_restore().
178 static void platform_restore_cleanup(int platform_mode
)
180 if (platform_mode
&& hibernation_ops
)
181 hibernation_ops
->restore_cleanup();
185 * platform_recover - recover the platform from a failure to suspend
189 static void platform_recover(int platform_mode
)
191 if (platform_mode
&& hibernation_ops
&& hibernation_ops
->recover
)
192 hibernation_ops
->recover();
196 * create_image - freeze devices that need to be frozen with interrupts
197 * off, create the hibernation image and thaw those devices. Control
198 * reappears in this routine after a restore.
201 static int create_image(int platform_mode
)
205 error
= arch_prepare_suspend();
211 /* At this point, device_suspend() has been called, but *not*
212 * device_power_down(). We *must* call device_power_down() now.
213 * Otherwise, drivers for some devices (e.g. interrupt controllers)
214 * become desynchronized with the actual state of the hardware
215 * at resume time, and evil weirdness ensues.
217 error
= device_power_down(PMSG_FREEZE
);
219 printk(KERN_ERR
"PM: Some devices failed to power down, "
220 "aborting hibernation\n");
224 if (hibernation_test(TEST_CORE
))
228 save_processor_state();
229 error
= swsusp_arch_suspend();
231 printk(KERN_ERR
"PM: Error %d creating hibernation image\n",
233 /* Restore control flow magically appears here */
234 restore_processor_state();
236 platform_leave(platform_mode
);
238 /* NOTE: device_power_up() is just a resume() for devices
239 * that suspended with irqs off ... no overall powerup.
241 device_power_up(in_suspend
?
242 (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
);
250 * hibernation_snapshot - quiesce devices and create the hibernation
252 * @platform_mode - if set, use the platform driver, if available, to
253 * prepare the platform frimware for the power transition.
255 * Must be called with pm_mutex held
258 int hibernation_snapshot(int platform_mode
)
260 int error
, ftrace_save
;
262 /* Free memory before shutting down devices. */
263 error
= swsusp_shrink_memory();
267 error
= platform_begin(platform_mode
);
272 ftrace_save
= __ftrace_enabled_save();
273 error
= device_suspend(PMSG_FREEZE
);
275 goto Recover_platform
;
277 if (hibernation_test(TEST_DEVICES
))
278 goto Recover_platform
;
280 error
= platform_pre_snapshot(platform_mode
);
281 if (error
|| hibernation_test(TEST_PLATFORM
))
284 error
= disable_nonboot_cpus();
286 if (hibernation_test(TEST_CPUS
))
289 if (hibernation_testmode(HIBERNATION_TEST
))
292 error
= create_image(platform_mode
);
293 /* Control returns here after successful restore */
296 enable_nonboot_cpus();
298 platform_finish(platform_mode
);
300 device_resume(in_suspend
?
301 (error
? PMSG_RECOVER
: PMSG_THAW
) : PMSG_RESTORE
);
302 __ftrace_enabled_restore(ftrace_save
);
305 platform_end(platform_mode
);
309 platform_recover(platform_mode
);
314 * resume_target_kernel - prepare devices that need to be suspended with
315 * interrupts off, restore the contents of highmem that have not been
316 * restored yet from the image and run the low level code that will restore
317 * the remaining contents of memory and switch to the just restored target
321 static int resume_target_kernel(void)
327 error
= device_power_down(PMSG_QUIESCE
);
329 printk(KERN_ERR
"PM: Some devices failed to power down, "
330 "aborting resume\n");
333 /* We'll ignore saved state, but this gets preempt count (etc) right */
334 save_processor_state();
335 error
= restore_highmem();
337 error
= swsusp_arch_resume();
339 * The code below is only ever reached in case of a failure.
340 * Otherwise execution continues at place where
341 * swsusp_arch_suspend() was called
344 /* This call to restore_highmem() undos the previous one */
348 * The only reason why swsusp_arch_resume() can fail is memory being
349 * very tight, so we have to free it as soon as we can to avoid
350 * subsequent failures
353 restore_processor_state();
354 touch_softlockup_watchdog();
355 device_power_up(PMSG_RECOVER
);
363 * hibernation_restore - quiesce devices and restore the hibernation
364 * snapshot image. If successful, control returns in hibernation_snaphot()
365 * @platform_mode - if set, use the platform driver, if available, to
366 * prepare the platform frimware for the transition.
368 * Must be called with pm_mutex held
371 int hibernation_restore(int platform_mode
)
373 int error
, ftrace_save
;
375 pm_prepare_console();
377 ftrace_save
= __ftrace_enabled_save();
378 error
= device_suspend(PMSG_QUIESCE
);
382 error
= platform_pre_restore(platform_mode
);
384 error
= disable_nonboot_cpus();
386 error
= resume_target_kernel();
387 enable_nonboot_cpus();
389 platform_restore_cleanup(platform_mode
);
390 device_resume(PMSG_RECOVER
);
392 __ftrace_enabled_restore(ftrace_save
);
394 pm_restore_console();
399 * hibernation_platform_enter - enter the hibernation state using the
400 * platform driver (if available)
403 int hibernation_platform_enter(void)
405 int error
, ftrace_save
;
407 if (!hibernation_ops
)
411 * We have cancelled the power transition by running
412 * hibernation_ops->finish() before saving the image, so we should let
413 * the firmware know that we're going to enter the sleep state after all
415 error
= hibernation_ops
->begin();
420 ftrace_save
= __ftrace_enabled_save();
421 error
= device_suspend(PMSG_HIBERNATE
);
423 if (hibernation_ops
->recover
)
424 hibernation_ops
->recover();
428 error
= hibernation_ops
->prepare();
432 error
= disable_nonboot_cpus();
438 error
= device_power_down(PMSG_HIBERNATE
);
440 hibernation_ops
->enter();
441 /* We should never get here */
448 * We don't need to reenable the nonboot CPUs or resume consoles, since
449 * the system is going to be halted anyway.
452 hibernation_ops
->finish();
454 device_resume(PMSG_RESTORE
);
455 __ftrace_enabled_restore(ftrace_save
);
458 hibernation_ops
->end();
463 * power_down - Shut the machine down for hibernation.
465 * Use the platform driver, if configured so; otherwise try
466 * to power off or reboot.
469 static void power_down(void)
471 switch (hibernation_mode
) {
472 case HIBERNATION_TEST
:
473 case HIBERNATION_TESTPROC
:
475 case HIBERNATION_REBOOT
:
476 kernel_restart(NULL
);
478 case HIBERNATION_PLATFORM
:
479 hibernation_platform_enter();
480 case HIBERNATION_SHUTDOWN
:
486 * Valid image is on the disk, if we continue we risk serious data
487 * corruption after resume.
489 printk(KERN_CRIT
"PM: Please power down manually\n");
493 static int prepare_processes(void)
497 if (freeze_processes()) {
505 * hibernate - The granpappy of the built-in hibernation management
512 mutex_lock(&pm_mutex
);
513 /* The snapshot device should not be opened while we're running */
514 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
519 pm_prepare_console();
520 error
= pm_notifier_call_chain(PM_HIBERNATION_PREPARE
);
524 error
= usermodehelper_disable();
528 /* Allocate memory management structures */
529 error
= create_basic_memory_bitmaps();
533 printk(KERN_INFO
"PM: Syncing filesystems ... ");
537 error
= prepare_processes();
541 if (hibernation_test(TEST_FREEZER
))
544 if (hibernation_testmode(HIBERNATION_TESTPROC
))
547 error
= hibernation_snapshot(hibernation_mode
== HIBERNATION_PLATFORM
);
548 if (in_suspend
&& !error
) {
549 unsigned int flags
= 0;
551 if (hibernation_mode
== HIBERNATION_PLATFORM
)
552 flags
|= SF_PLATFORM_MODE
;
553 pr_debug("PM: writing image.\n");
554 error
= swsusp_write(flags
);
559 pr_debug("PM: Image restored successfully.\n");
565 free_basic_memory_bitmaps();
566 usermodehelper_enable();
568 pm_notifier_call_chain(PM_POST_HIBERNATION
);
569 pm_restore_console();
570 atomic_inc(&snapshot_device_available
);
572 mutex_unlock(&pm_mutex
);
578 * software_resume - Resume from a saved image.
580 * Called as a late_initcall (so all devices are discovered and
581 * initialized), we call swsusp to see if we have a saved image or not.
582 * If so, we quiesce devices, the restore the saved image. We will
583 * return above (in hibernate() ) if everything goes well.
584 * Otherwise, we fail gracefully and return to the normally
589 static int software_resume(void)
595 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
596 * is configured into the kernel. Since the regular hibernate
597 * trigger path is via sysfs which takes a buffer mutex before
598 * calling hibernate functions (which take pm_mutex) this can
599 * cause lockdep to complain about a possible ABBA deadlock
600 * which cannot happen since we're in the boot code here and
601 * sysfs can't be invoked yet. Therefore, we use a subclass
602 * here to avoid lockdep complaining.
604 mutex_lock_nested(&pm_mutex
, SINGLE_DEPTH_NESTING
);
605 if (!swsusp_resume_device
) {
606 if (!strlen(resume_file
)) {
607 mutex_unlock(&pm_mutex
);
610 swsusp_resume_device
= name_to_dev_t(resume_file
);
611 pr_debug("PM: Resume from partition %s\n", resume_file
);
613 pr_debug("PM: Resume from partition %d:%d\n",
614 MAJOR(swsusp_resume_device
),
615 MINOR(swsusp_resume_device
));
620 * FIXME: If noresume is specified, we need to find the
621 * partition and reset it back to normal swap space.
623 mutex_unlock(&pm_mutex
);
627 pr_debug("PM: Checking hibernation image.\n");
628 error
= swsusp_check();
632 /* The snapshot device should not be opened while we're running */
633 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
638 pm_prepare_console();
639 error
= pm_notifier_call_chain(PM_RESTORE_PREPARE
);
643 error
= usermodehelper_disable();
647 error
= create_basic_memory_bitmaps();
651 pr_debug("PM: Preparing processes for restore.\n");
652 error
= prepare_processes();
658 pr_debug("PM: Reading hibernation image.\n");
660 error
= swsusp_read(&flags
);
662 hibernation_restore(flags
& SF_PLATFORM_MODE
);
664 printk(KERN_ERR
"PM: Restore failed, recovering.\n");
668 free_basic_memory_bitmaps();
669 usermodehelper_enable();
671 pm_notifier_call_chain(PM_POST_RESTORE
);
672 pm_restore_console();
673 atomic_inc(&snapshot_device_available
);
674 /* For success case, the suspend path will release the lock */
676 mutex_unlock(&pm_mutex
);
677 pr_debug("PM: Resume from disk failed.\n");
681 late_initcall(software_resume
);
684 static const char * const hibernation_modes
[] = {
685 [HIBERNATION_PLATFORM
] = "platform",
686 [HIBERNATION_SHUTDOWN
] = "shutdown",
687 [HIBERNATION_REBOOT
] = "reboot",
688 [HIBERNATION_TEST
] = "test",
689 [HIBERNATION_TESTPROC
] = "testproc",
693 * disk - Control hibernation mode
695 * Suspend-to-disk can be handled in several ways. We have a few options
696 * for putting the system to sleep - using the platform driver (e.g. ACPI
697 * or other hibernation_ops), powering off the system or rebooting the
698 * system (for testing) as well as the two test modes.
700 * The system can support 'platform', and that is known a priori (and
701 * encoded by the presence of hibernation_ops). However, the user may
702 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
703 * test modes, 'test' or 'testproc'.
705 * show() will display what the mode is currently set to.
706 * store() will accept one of
714 * It will only change to 'platform' if the system
715 * supports it (as determined by having hibernation_ops).
718 static ssize_t
disk_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
724 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
725 if (!hibernation_modes
[i
])
728 case HIBERNATION_SHUTDOWN
:
729 case HIBERNATION_REBOOT
:
730 case HIBERNATION_TEST
:
731 case HIBERNATION_TESTPROC
:
733 case HIBERNATION_PLATFORM
:
736 /* not a valid mode, continue with loop */
739 if (i
== hibernation_mode
)
740 buf
+= sprintf(buf
, "[%s] ", hibernation_modes
[i
]);
742 buf
+= sprintf(buf
, "%s ", hibernation_modes
[i
]);
744 buf
+= sprintf(buf
, "\n");
749 static ssize_t
disk_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
750 const char *buf
, size_t n
)
756 int mode
= HIBERNATION_INVALID
;
758 p
= memchr(buf
, '\n', n
);
759 len
= p
? p
- buf
: n
;
761 mutex_lock(&pm_mutex
);
762 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
763 if (len
== strlen(hibernation_modes
[i
])
764 && !strncmp(buf
, hibernation_modes
[i
], len
)) {
769 if (mode
!= HIBERNATION_INVALID
) {
771 case HIBERNATION_SHUTDOWN
:
772 case HIBERNATION_REBOOT
:
773 case HIBERNATION_TEST
:
774 case HIBERNATION_TESTPROC
:
775 hibernation_mode
= mode
;
777 case HIBERNATION_PLATFORM
:
779 hibernation_mode
= mode
;
787 pr_debug("PM: Hibernation mode set to '%s'\n",
788 hibernation_modes
[mode
]);
789 mutex_unlock(&pm_mutex
);
790 return error
? error
: n
;
795 static ssize_t
resume_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
798 return sprintf(buf
,"%d:%d\n", MAJOR(swsusp_resume_device
),
799 MINOR(swsusp_resume_device
));
802 static ssize_t
resume_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
803 const char *buf
, size_t n
)
805 unsigned int maj
, min
;
809 if (sscanf(buf
, "%u:%u", &maj
, &min
) != 2)
812 res
= MKDEV(maj
,min
);
813 if (maj
!= MAJOR(res
) || min
!= MINOR(res
))
816 mutex_lock(&pm_mutex
);
817 swsusp_resume_device
= res
;
818 mutex_unlock(&pm_mutex
);
819 printk(KERN_INFO
"PM: Starting manual resume from disk\n");
829 static ssize_t
image_size_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
832 return sprintf(buf
, "%lu\n", image_size
);
835 static ssize_t
image_size_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
836 const char *buf
, size_t n
)
840 if (sscanf(buf
, "%lu", &size
) == 1) {
848 power_attr(image_size
);
850 static struct attribute
* g
[] = {
853 &image_size_attr
.attr
,
858 static struct attribute_group attr_group
= {
863 static int __init
pm_disk_init(void)
865 return sysfs_create_group(power_kobj
, &attr_group
);
868 core_initcall(pm_disk_init
);
871 static int __init
resume_setup(char *str
)
876 strncpy( resume_file
, str
, 255 );
880 static int __init
resume_offset_setup(char *str
)
882 unsigned long long offset
;
887 if (sscanf(str
, "%llu", &offset
) == 1)
888 swsusp_resume_block
= offset
;
893 static int __init
noresume_setup(char *str
)
899 __setup("noresume", noresume_setup
);
900 __setup("resume_offset=", resume_offset_setup
);
901 __setup("resume=", resume_setup
);