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/delay.h>
19 #include <linux/mount.h>
21 #include <linux/console.h>
22 #include <linux/cpu.h>
23 #include <linux/freezer.h>
28 static int noresume
= 0;
29 static char resume_file
[256] = CONFIG_PM_STD_PARTITION
;
30 dev_t swsusp_resume_device
;
31 sector_t swsusp_resume_block
;
41 __HIBERNATION_AFTER_LAST
43 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
44 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
46 static int hibernation_mode
= HIBERNATION_SHUTDOWN
;
48 static struct platform_hibernation_ops
*hibernation_ops
;
51 * hibernation_set_ops - set the global hibernate operations
52 * @ops: the hibernation operations to use in subsequent hibernation transitions
55 void hibernation_set_ops(struct platform_hibernation_ops
*ops
)
57 if (ops
&& !(ops
->begin
&& ops
->end
&& ops
->pre_snapshot
58 && ops
->prepare
&& ops
->finish
&& ops
->enter
&& ops
->pre_restore
59 && ops
->restore_cleanup
)) {
63 mutex_lock(&pm_mutex
);
64 hibernation_ops
= ops
;
66 hibernation_mode
= HIBERNATION_PLATFORM
;
67 else if (hibernation_mode
== HIBERNATION_PLATFORM
)
68 hibernation_mode
= HIBERNATION_SHUTDOWN
;
70 mutex_unlock(&pm_mutex
);
73 #ifdef CONFIG_PM_DEBUG
74 static void hibernation_debug_sleep(void)
76 printk(KERN_INFO
"hibernation debug: Waiting for 5 seconds.\n");
80 static int hibernation_testmode(int mode
)
82 if (hibernation_mode
== mode
) {
83 hibernation_debug_sleep();
89 static int hibernation_test(int level
)
91 if (pm_test_level
== level
) {
92 hibernation_debug_sleep();
97 #else /* !CONFIG_PM_DEBUG */
98 static int hibernation_testmode(int mode
) { return 0; }
99 static int hibernation_test(int level
) { return 0; }
100 #endif /* !CONFIG_PM_DEBUG */
103 * platform_begin - tell the platform driver that we're starting
107 static int platform_begin(int platform_mode
)
109 return (platform_mode
&& hibernation_ops
) ?
110 hibernation_ops
->begin() : 0;
114 * platform_end - tell the platform driver that we've entered the
118 static void platform_end(int platform_mode
)
120 if (platform_mode
&& hibernation_ops
)
121 hibernation_ops
->end();
125 * platform_pre_snapshot - prepare the machine for hibernation using the
126 * platform driver if so configured and return an error code if it fails
129 static int platform_pre_snapshot(int platform_mode
)
131 return (platform_mode
&& hibernation_ops
) ?
132 hibernation_ops
->pre_snapshot() : 0;
136 * platform_leave - prepare the machine for switching to the normal mode
137 * of operation using the platform driver (called with interrupts disabled)
140 static void platform_leave(int platform_mode
)
142 if (platform_mode
&& hibernation_ops
)
143 hibernation_ops
->leave();
147 * platform_finish - switch the machine to the normal mode of operation
148 * using the platform driver (must be called after platform_prepare())
151 static void platform_finish(int platform_mode
)
153 if (platform_mode
&& hibernation_ops
)
154 hibernation_ops
->finish();
158 * platform_pre_restore - prepare the platform for the restoration from a
159 * hibernation image. If the restore fails after this function has been
160 * called, platform_restore_cleanup() must be called.
163 static int platform_pre_restore(int platform_mode
)
165 return (platform_mode
&& hibernation_ops
) ?
166 hibernation_ops
->pre_restore() : 0;
170 * platform_restore_cleanup - switch the platform to the normal mode of
171 * operation after a failing restore. If platform_pre_restore() has been
172 * called before the failing restore, this function must be called too,
173 * regardless of the result of platform_pre_restore().
176 static void platform_restore_cleanup(int platform_mode
)
178 if (platform_mode
&& hibernation_ops
)
179 hibernation_ops
->restore_cleanup();
183 * create_image - freeze devices that need to be frozen with interrupts
184 * off, create the hibernation image and thaw those devices. Control
185 * reappears in this routine after a restore.
188 static int create_image(int platform_mode
)
192 error
= arch_prepare_suspend();
197 /* At this point, device_suspend() has been called, but *not*
198 * device_power_down(). We *must* call device_power_down() now.
199 * Otherwise, drivers for some devices (e.g. interrupt controllers)
200 * become desynchronized with the actual state of the hardware
201 * at resume time, and evil weirdness ensues.
203 error
= device_power_down(PMSG_FREEZE
);
205 printk(KERN_ERR
"PM: Some devices failed to power down, "
206 "aborting hibernation\n");
210 if (hibernation_test(TEST_CORE
))
214 save_processor_state();
215 error
= swsusp_arch_suspend();
217 printk(KERN_ERR
"PM: Error %d creating hibernation image\n",
219 /* Restore control flow magically appears here */
220 restore_processor_state();
222 platform_leave(platform_mode
);
224 /* NOTE: device_power_up() is just a resume() for devices
225 * that suspended with irqs off ... no overall powerup.
234 * hibernation_snapshot - quiesce devices and create the hibernation
236 * @platform_mode - if set, use the platform driver, if available, to
237 * prepare the platform frimware for the power transition.
239 * Must be called with pm_mutex held
242 int hibernation_snapshot(int platform_mode
)
246 /* Free memory before shutting down devices. */
247 error
= swsusp_shrink_memory();
251 error
= platform_begin(platform_mode
);
256 error
= device_suspend(PMSG_FREEZE
);
260 if (hibernation_test(TEST_DEVICES
))
263 error
= platform_pre_snapshot(platform_mode
);
264 if (error
|| hibernation_test(TEST_PLATFORM
))
267 error
= disable_nonboot_cpus();
269 if (hibernation_test(TEST_CPUS
))
272 if (hibernation_testmode(HIBERNATION_TEST
))
275 error
= create_image(platform_mode
);
276 /* Control returns here after successful restore */
279 enable_nonboot_cpus();
281 platform_finish(platform_mode
);
287 platform_end(platform_mode
);
292 * resume_target_kernel - prepare devices that need to be suspended with
293 * interrupts off, restore the contents of highmem that have not been
294 * restored yet from the image and run the low level code that will restore
295 * the remaining contents of memory and switch to the just restored target
299 static int resume_target_kernel(void)
304 error
= device_power_down(PMSG_PRETHAW
);
306 printk(KERN_ERR
"PM: Some devices failed to power down, "
307 "aborting resume\n");
310 /* We'll ignore saved state, but this gets preempt count (etc) right */
311 save_processor_state();
312 error
= restore_highmem();
314 error
= swsusp_arch_resume();
316 * The code below is only ever reached in case of a failure.
317 * Otherwise execution continues at place where
318 * swsusp_arch_suspend() was called
321 /* This call to restore_highmem() undos the previous one */
325 * The only reason why swsusp_arch_resume() can fail is memory being
326 * very tight, so we have to free it as soon as we can to avoid
327 * subsequent failures
330 restore_processor_state();
331 touch_softlockup_watchdog();
339 * hibernation_restore - quiesce devices and restore the hibernation
340 * snapshot image. If successful, control returns in hibernation_snaphot()
341 * @platform_mode - if set, use the platform driver, if available, to
342 * prepare the platform frimware for the transition.
344 * Must be called with pm_mutex held
347 int hibernation_restore(int platform_mode
)
351 pm_prepare_console();
353 error
= device_suspend(PMSG_PRETHAW
);
357 error
= platform_pre_restore(platform_mode
);
359 error
= disable_nonboot_cpus();
361 error
= resume_target_kernel();
362 enable_nonboot_cpus();
364 platform_restore_cleanup(platform_mode
);
368 pm_restore_console();
373 * hibernation_platform_enter - enter the hibernation state using the
374 * platform driver (if available)
377 int hibernation_platform_enter(void)
381 if (!hibernation_ops
)
385 * We have cancelled the power transition by running
386 * hibernation_ops->finish() before saving the image, so we should let
387 * the firmware know that we're going to enter the sleep state after all
389 error
= hibernation_ops
->begin();
394 <<<<<<< HEAD
:kernel
/power
/disk
.c
395 error
= device_suspend(PMSG_SUSPEND
);
397 error
= device_suspend(PMSG_HIBERNATE
);
398 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:kernel
/power
/disk
.c
402 error
= hibernation_ops
->prepare();
406 error
= disable_nonboot_cpus();
411 <<<<<<< HEAD
:kernel
/power
/disk
.c
412 error
= device_power_down(PMSG_SUSPEND
);
414 error
= device_power_down(PMSG_HIBERNATE
);
415 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:kernel
/power
/disk
.c
417 hibernation_ops
->enter();
418 /* We should never get here */
424 * We don't need to reenable the nonboot CPUs or resume consoles, since
425 * the system is going to be halted anyway.
428 hibernation_ops
->finish();
434 hibernation_ops
->end();
439 * power_down - Shut the machine down for hibernation.
441 * Use the platform driver, if configured so; otherwise try
442 * to power off or reboot.
445 static void power_down(void)
447 switch (hibernation_mode
) {
448 case HIBERNATION_TEST
:
449 case HIBERNATION_TESTPROC
:
451 case HIBERNATION_REBOOT
:
452 kernel_restart(NULL
);
454 case HIBERNATION_PLATFORM
:
455 hibernation_platform_enter();
456 case HIBERNATION_SHUTDOWN
:
462 * Valid image is on the disk, if we continue we risk serious data
463 * corruption after resume.
465 printk(KERN_CRIT
"PM: Please power down manually\n");
469 static int prepare_processes(void)
473 if (freeze_processes()) {
481 * hibernate - The granpappy of the built-in hibernation management
488 mutex_lock(&pm_mutex
);
489 /* The snapshot device should not be opened while we're running */
490 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
495 pm_prepare_console();
496 error
= pm_notifier_call_chain(PM_HIBERNATION_PREPARE
);
500 /* Allocate memory management structures */
501 error
= create_basic_memory_bitmaps();
505 printk(KERN_INFO
"PM: Syncing filesystems ... ");
509 error
= prepare_processes();
513 if (hibernation_test(TEST_FREEZER
))
516 if (hibernation_testmode(HIBERNATION_TESTPROC
))
519 error
= hibernation_snapshot(hibernation_mode
== HIBERNATION_PLATFORM
);
520 if (in_suspend
&& !error
) {
521 unsigned int flags
= 0;
523 if (hibernation_mode
== HIBERNATION_PLATFORM
)
524 flags
|= SF_PLATFORM_MODE
;
525 pr_debug("PM: writing image.\n");
526 error
= swsusp_write(flags
);
531 pr_debug("PM: Image restored successfully.\n");
537 free_basic_memory_bitmaps();
539 pm_notifier_call_chain(PM_POST_HIBERNATION
);
540 pm_restore_console();
541 atomic_inc(&snapshot_device_available
);
543 mutex_unlock(&pm_mutex
);
549 * software_resume - Resume from a saved image.
551 * Called as a late_initcall (so all devices are discovered and
552 * initialized), we call swsusp to see if we have a saved image or not.
553 * If so, we quiesce devices, the restore the saved image. We will
554 * return above (in hibernate() ) if everything goes well.
555 * Otherwise, we fail gracefully and return to the normally
560 static int software_resume(void)
566 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
567 * is configured into the kernel. Since the regular hibernate
568 * trigger path is via sysfs which takes a buffer mutex before
569 * calling hibernate functions (which take pm_mutex) this can
570 * cause lockdep to complain about a possible ABBA deadlock
571 * which cannot happen since we're in the boot code here and
572 * sysfs can't be invoked yet. Therefore, we use a subclass
573 * here to avoid lockdep complaining.
575 mutex_lock_nested(&pm_mutex
, SINGLE_DEPTH_NESTING
);
576 if (!swsusp_resume_device
) {
577 if (!strlen(resume_file
)) {
578 mutex_unlock(&pm_mutex
);
581 swsusp_resume_device
= name_to_dev_t(resume_file
);
582 pr_debug("PM: Resume from partition %s\n", resume_file
);
584 pr_debug("PM: Resume from partition %d:%d\n",
585 MAJOR(swsusp_resume_device
),
586 MINOR(swsusp_resume_device
));
591 * FIXME: If noresume is specified, we need to find the
592 * partition and reset it back to normal swap space.
594 mutex_unlock(&pm_mutex
);
598 pr_debug("PM: Checking hibernation image.\n");
599 error
= swsusp_check();
603 /* The snapshot device should not be opened while we're running */
604 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
609 pm_prepare_console();
610 error
= pm_notifier_call_chain(PM_RESTORE_PREPARE
);
614 error
= create_basic_memory_bitmaps();
618 pr_debug("PM: Preparing processes for restore.\n");
619 error
= prepare_processes();
625 pr_debug("PM: Reading hibernation image.\n");
627 error
= swsusp_read(&flags
);
629 hibernation_restore(flags
& SF_PLATFORM_MODE
);
631 printk(KERN_ERR
"PM: Restore failed, recovering.\n");
635 free_basic_memory_bitmaps();
637 pm_notifier_call_chain(PM_POST_RESTORE
);
638 pm_restore_console();
639 atomic_inc(&snapshot_device_available
);
640 /* For success case, the suspend path will release the lock */
642 mutex_unlock(&pm_mutex
);
643 pr_debug("PM: Resume from disk failed.\n");
647 late_initcall(software_resume
);
650 static const char * const hibernation_modes
[] = {
651 [HIBERNATION_PLATFORM
] = "platform",
652 [HIBERNATION_SHUTDOWN
] = "shutdown",
653 [HIBERNATION_REBOOT
] = "reboot",
654 [HIBERNATION_TEST
] = "test",
655 [HIBERNATION_TESTPROC
] = "testproc",
659 * disk - Control hibernation mode
661 * Suspend-to-disk can be handled in several ways. We have a few options
662 * for putting the system to sleep - using the platform driver (e.g. ACPI
663 * or other hibernation_ops), powering off the system or rebooting the
664 * system (for testing) as well as the two test modes.
666 * The system can support 'platform', and that is known a priori (and
667 * encoded by the presence of hibernation_ops). However, the user may
668 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
669 * test modes, 'test' or 'testproc'.
671 * show() will display what the mode is currently set to.
672 * store() will accept one of
680 * It will only change to 'platform' if the system
681 * supports it (as determined by having hibernation_ops).
684 static ssize_t
disk_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
690 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
691 if (!hibernation_modes
[i
])
694 case HIBERNATION_SHUTDOWN
:
695 case HIBERNATION_REBOOT
:
696 case HIBERNATION_TEST
:
697 case HIBERNATION_TESTPROC
:
699 case HIBERNATION_PLATFORM
:
702 /* not a valid mode, continue with loop */
705 if (i
== hibernation_mode
)
706 buf
+= sprintf(buf
, "[%s] ", hibernation_modes
[i
]);
708 buf
+= sprintf(buf
, "%s ", hibernation_modes
[i
]);
710 buf
+= sprintf(buf
, "\n");
715 static ssize_t
disk_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
716 const char *buf
, size_t n
)
722 int mode
= HIBERNATION_INVALID
;
724 p
= memchr(buf
, '\n', n
);
725 len
= p
? p
- buf
: n
;
727 mutex_lock(&pm_mutex
);
728 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
729 if (len
== strlen(hibernation_modes
[i
])
730 && !strncmp(buf
, hibernation_modes
[i
], len
)) {
735 if (mode
!= HIBERNATION_INVALID
) {
737 case HIBERNATION_SHUTDOWN
:
738 case HIBERNATION_REBOOT
:
739 case HIBERNATION_TEST
:
740 case HIBERNATION_TESTPROC
:
741 hibernation_mode
= mode
;
743 case HIBERNATION_PLATFORM
:
745 hibernation_mode
= mode
;
753 pr_debug("PM: Hibernation mode set to '%s'\n",
754 hibernation_modes
[mode
]);
755 mutex_unlock(&pm_mutex
);
756 return error
? error
: n
;
761 static ssize_t
resume_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
764 return sprintf(buf
,"%d:%d\n", MAJOR(swsusp_resume_device
),
765 MINOR(swsusp_resume_device
));
768 static ssize_t
resume_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
769 const char *buf
, size_t n
)
771 unsigned int maj
, min
;
775 if (sscanf(buf
, "%u:%u", &maj
, &min
) != 2)
778 res
= MKDEV(maj
,min
);
779 if (maj
!= MAJOR(res
) || min
!= MINOR(res
))
782 mutex_lock(&pm_mutex
);
783 swsusp_resume_device
= res
;
784 mutex_unlock(&pm_mutex
);
785 printk(KERN_INFO
"PM: Starting manual resume from disk\n");
795 static ssize_t
image_size_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
798 return sprintf(buf
, "%lu\n", image_size
);
801 static ssize_t
image_size_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
802 const char *buf
, size_t n
)
806 if (sscanf(buf
, "%lu", &size
) == 1) {
814 power_attr(image_size
);
816 static struct attribute
* g
[] = {
819 &image_size_attr
.attr
,
824 static struct attribute_group attr_group
= {
829 static int __init
pm_disk_init(void)
831 return sysfs_create_group(power_kobj
, &attr_group
);
834 core_initcall(pm_disk_init
);
837 static int __init
resume_setup(char *str
)
842 strncpy( resume_file
, str
, 255 );
846 static int __init
resume_offset_setup(char *str
)
848 unsigned long long offset
;
853 if (sscanf(str
, "%llu", &offset
) == 1)
854 swsusp_resume_block
= offset
;
859 static int __init
noresume_setup(char *str
)
865 __setup("noresume", noresume_setup
);
866 __setup("resume_offset=", resume_offset_setup
);
867 __setup("resume=", resume_setup
);