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>
24 #include <linux/ftrace.h>
29 static int noresume = 0;
30 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
31 dev_t swsusp_resume_device;
32 sector_t swsusp_resume_block;
42 __HIBERNATION_AFTER_LAST
44 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
45 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
47 static int hibernation_mode = HIBERNATION_SHUTDOWN;
49 static struct platform_hibernation_ops *hibernation_ops;
52 * hibernation_set_ops - set the global hibernate operations
53 * @ops: the hibernation operations to use in subsequent hibernation transitions
56 void hibernation_set_ops(struct platform_hibernation_ops *ops)
58 if (ops && !(ops->begin && ops->end && ops->pre_snapshot
59 && ops->prepare && ops->finish && ops->enter && ops->pre_restore
60 && ops->restore_cleanup)) {
64 mutex_lock(&pm_mutex);
65 hibernation_ops = ops;
67 hibernation_mode = HIBERNATION_PLATFORM;
68 else if (hibernation_mode == HIBERNATION_PLATFORM)
69 hibernation_mode = HIBERNATION_SHUTDOWN;
71 mutex_unlock(&pm_mutex);
74 #ifdef CONFIG_PM_DEBUG
75 static void hibernation_debug_sleep(void)
77 printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
81 static int hibernation_testmode(int mode)
83 if (hibernation_mode == mode) {
84 hibernation_debug_sleep();
90 static int hibernation_test(int level)
92 if (pm_test_level == level) {
93 hibernation_debug_sleep();
98 #else /* !CONFIG_PM_DEBUG */
99 static int hibernation_testmode(int mode) { return 0; }
100 static int hibernation_test(int level) { return 0; }
101 #endif /* !CONFIG_PM_DEBUG */
104 * platform_begin - tell the platform driver that we're starting
108 static int platform_begin(int platform_mode)
110 return (platform_mode && hibernation_ops) ?
111 hibernation_ops->begin() : 0;
115 * platform_end - tell the platform driver that we've entered the
119 static void platform_end(int platform_mode)
121 if (platform_mode && hibernation_ops)
122 hibernation_ops->end();
126 * platform_pre_snapshot - prepare the machine for hibernation using the
127 * platform driver if so configured and return an error code if it fails
130 static int platform_pre_snapshot(int platform_mode)
132 return (platform_mode && hibernation_ops) ?
133 hibernation_ops->pre_snapshot() : 0;
137 * platform_leave - prepare the machine for switching to the normal mode
138 * of operation using the platform driver (called with interrupts disabled)
141 static void platform_leave(int platform_mode)
143 if (platform_mode && hibernation_ops)
144 hibernation_ops->leave();
148 * platform_finish - switch the machine to the normal mode of operation
149 * using the platform driver (must be called after platform_prepare())
152 static void platform_finish(int platform_mode)
154 if (platform_mode && hibernation_ops)
155 hibernation_ops->finish();
159 * platform_pre_restore - prepare the platform for the restoration from a
160 * hibernation image. If the restore fails after this function has been
161 * called, platform_restore_cleanup() must be called.
164 static int platform_pre_restore(int platform_mode)
166 return (platform_mode && hibernation_ops) ?
167 hibernation_ops->pre_restore() : 0;
171 * platform_restore_cleanup - switch the platform to the normal mode of
172 * operation after a failing restore. If platform_pre_restore() has been
173 * called before the failing restore, this function must be called too,
174 * regardless of the result of platform_pre_restore().
177 static void platform_restore_cleanup(int platform_mode)
179 if (platform_mode && hibernation_ops)
180 hibernation_ops->restore_cleanup();
184 * platform_recover - recover the platform from a failure to suspend
188 static void platform_recover(int platform_mode)
190 if (platform_mode && hibernation_ops && hibernation_ops->recover)
191 hibernation_ops->recover();
195 * create_image - freeze devices that need to be frozen with interrupts
196 * off, create the hibernation image and thaw those devices. Control
197 * reappears in this routine after a restore.
200 static int create_image(int platform_mode)
204 error = arch_prepare_suspend();
210 /* At this point, device_suspend() has been called, but *not*
211 * device_power_down(). We *must* call device_power_down() now.
212 * Otherwise, drivers for some devices (e.g. interrupt controllers)
213 * become desynchronized with the actual state of the hardware
214 * at resume time, and evil weirdness ensues.
216 error = device_power_down(PMSG_FREEZE);
218 printk(KERN_ERR "PM: Some devices failed to power down, "
219 "aborting hibernation\n");
223 if (hibernation_test(TEST_CORE))
227 save_processor_state();
228 error = swsusp_arch_suspend();
230 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
232 /* Restore control flow magically appears here */
233 restore_processor_state();
235 platform_leave(platform_mode);
237 /* NOTE: device_power_up() is just a resume() for devices
238 * that suspended with irqs off ... no overall powerup.
240 device_power_up(in_suspend ?
241 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
249 * hibernation_snapshot - quiesce devices and create the hibernation
251 * @platform_mode - if set, use the platform driver, if available, to
252 * prepare the platform frimware for the power transition.
254 * Must be called with pm_mutex held
257 int hibernation_snapshot(int platform_mode)
259 int error, ftrace_save;
261 /* Free memory before shutting down devices. */
262 error = swsusp_shrink_memory();
266 error = platform_begin(platform_mode);
271 ftrace_save = __ftrace_enabled_save();
272 error = device_suspend(PMSG_FREEZE);
274 goto Recover_platform;
276 if (hibernation_test(TEST_DEVICES))
277 goto Recover_platform;
279 error = platform_pre_snapshot(platform_mode);
280 if (error || hibernation_test(TEST_PLATFORM))
283 error = disable_nonboot_cpus();
285 if (hibernation_test(TEST_CPUS))
288 if (hibernation_testmode(HIBERNATION_TEST))
291 error = create_image(platform_mode);
292 /* Control returns here after successful restore */
295 enable_nonboot_cpus();
297 platform_finish(platform_mode);
299 device_resume(in_suspend ?
300 (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
301 __ftrace_enabled_restore(ftrace_save);
304 platform_end(platform_mode);
308 platform_recover(platform_mode);
313 * resume_target_kernel - prepare devices that need to be suspended with
314 * interrupts off, restore the contents of highmem that have not been
315 * restored yet from the image and run the low level code that will restore
316 * the remaining contents of memory and switch to the just restored target
320 static int resume_target_kernel(void)
326 error = device_power_down(PMSG_QUIESCE);
328 printk(KERN_ERR "PM: Some devices failed to power down, "
329 "aborting resume\n");
332 /* We'll ignore saved state, but this gets preempt count (etc) right */
333 save_processor_state();
334 error = restore_highmem();
336 error = swsusp_arch_resume();
338 * The code below is only ever reached in case of a failure.
339 * Otherwise execution continues at place where
340 * swsusp_arch_suspend() was called
343 /* This call to restore_highmem() undos the previous one */
347 * The only reason why swsusp_arch_resume() can fail is memory being
348 * very tight, so we have to free it as soon as we can to avoid
349 * subsequent failures
352 restore_processor_state();
353 touch_softlockup_watchdog();
354 device_power_up(PMSG_RECOVER);
362 * hibernation_restore - quiesce devices and restore the hibernation
363 * snapshot image. If successful, control returns in hibernation_snaphot()
364 * @platform_mode - if set, use the platform driver, if available, to
365 * prepare the platform frimware for the transition.
367 * Must be called with pm_mutex held
370 int hibernation_restore(int platform_mode)
372 int error, ftrace_save;
374 pm_prepare_console();
376 ftrace_save = __ftrace_enabled_save();
377 error = device_suspend(PMSG_QUIESCE);
381 error = platform_pre_restore(platform_mode);
383 error = disable_nonboot_cpus();
385 error = resume_target_kernel();
386 enable_nonboot_cpus();
388 platform_restore_cleanup(platform_mode);
389 device_resume(PMSG_RECOVER);
391 __ftrace_enabled_restore(ftrace_save);
393 pm_restore_console();
398 * hibernation_platform_enter - enter the hibernation state using the
399 * platform driver (if available)
402 int hibernation_platform_enter(void)
404 int error, ftrace_save;
406 if (!hibernation_ops)
410 * We have cancelled the power transition by running
411 * hibernation_ops->finish() before saving the image, so we should let
412 * the firmware know that we're going to enter the sleep state after all
414 error = hibernation_ops->begin();
419 ftrace_save = __ftrace_enabled_save();
420 error = device_suspend(PMSG_HIBERNATE);
422 if (hibernation_ops->recover)
423 hibernation_ops->recover();
427 error = hibernation_ops->prepare();
431 error = disable_nonboot_cpus();
437 error = device_power_down(PMSG_HIBERNATE);
439 hibernation_ops->enter();
440 /* We should never get here */
447 * We don't need to reenable the nonboot CPUs or resume consoles, since
448 * the system is going to be halted anyway.
451 hibernation_ops->finish();
453 device_resume(PMSG_RESTORE);
454 __ftrace_enabled_restore(ftrace_save);
457 hibernation_ops->end();
462 * power_down - Shut the machine down for hibernation.
464 * Use the platform driver, if configured so; otherwise try
465 * to power off or reboot.
468 static void power_down(void)
470 switch (hibernation_mode) {
471 case HIBERNATION_TEST:
472 case HIBERNATION_TESTPROC:
474 case HIBERNATION_REBOOT:
475 kernel_restart(NULL);
477 case HIBERNATION_PLATFORM:
478 hibernation_platform_enter();
479 case HIBERNATION_SHUTDOWN:
485 * Valid image is on the disk, if we continue we risk serious data
486 * corruption after resume.
488 printk(KERN_CRIT "PM: Please power down manually\n");
492 static int prepare_processes(void)
496 if (freeze_processes()) {
504 * hibernate - The granpappy of the built-in hibernation management
511 mutex_lock(&pm_mutex);
512 /* The snapshot device should not be opened while we're running */
513 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
518 pm_prepare_console();
519 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
523 /* Allocate memory management structures */
524 error = create_basic_memory_bitmaps();
528 printk(KERN_INFO "PM: Syncing filesystems ... ");
532 error = prepare_processes();
536 if (hibernation_test(TEST_FREEZER))
539 if (hibernation_testmode(HIBERNATION_TESTPROC))
542 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
543 if (in_suspend && !error) {
544 unsigned int flags = 0;
546 if (hibernation_mode == HIBERNATION_PLATFORM)
547 flags |= SF_PLATFORM_MODE;
548 pr_debug("PM: writing image.\n");
549 error = swsusp_write(flags);
554 pr_debug("PM: Image restored successfully.\n");
560 free_basic_memory_bitmaps();
562 pm_notifier_call_chain(PM_POST_HIBERNATION);
563 pm_restore_console();
564 atomic_inc(&snapshot_device_available);
566 mutex_unlock(&pm_mutex);
572 * software_resume - Resume from a saved image.
574 * Called as a late_initcall (so all devices are discovered and
575 * initialized), we call swsusp to see if we have a saved image or not.
576 * If so, we quiesce devices, the restore the saved image. We will
577 * return above (in hibernate() ) if everything goes well.
578 * Otherwise, we fail gracefully and return to the normally
583 static int software_resume(void)
589 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
590 * is configured into the kernel. Since the regular hibernate
591 * trigger path is via sysfs which takes a buffer mutex before
592 * calling hibernate functions (which take pm_mutex) this can
593 * cause lockdep to complain about a possible ABBA deadlock
594 * which cannot happen since we're in the boot code here and
595 * sysfs can't be invoked yet. Therefore, we use a subclass
596 * here to avoid lockdep complaining.
598 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
599 if (!swsusp_resume_device) {
600 if (!strlen(resume_file)) {
601 mutex_unlock(&pm_mutex);
604 swsusp_resume_device = name_to_dev_t(resume_file);
605 pr_debug("PM: Resume from partition %s\n", resume_file);
607 pr_debug("PM: Resume from partition %d:%d\n",
608 MAJOR(swsusp_resume_device),
609 MINOR(swsusp_resume_device));
614 * FIXME: If noresume is specified, we need to find the
615 * partition and reset it back to normal swap space.
617 mutex_unlock(&pm_mutex);
621 pr_debug("PM: Checking hibernation image.\n");
622 error = swsusp_check();
626 /* The snapshot device should not be opened while we're running */
627 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
632 pm_prepare_console();
633 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
637 error = create_basic_memory_bitmaps();
641 pr_debug("PM: Preparing processes for restore.\n");
642 error = prepare_processes();
648 pr_debug("PM: Reading hibernation image.\n");
650 error = swsusp_read(&flags);
652 hibernation_restore(flags & SF_PLATFORM_MODE);
654 printk(KERN_ERR "PM: Restore failed, recovering.\n");
658 free_basic_memory_bitmaps();
660 pm_notifier_call_chain(PM_POST_RESTORE);
661 pm_restore_console();
662 atomic_inc(&snapshot_device_available);
663 /* For success case, the suspend path will release the lock */
665 mutex_unlock(&pm_mutex);
666 pr_debug("PM: Resume from disk failed.\n");
670 late_initcall(software_resume);
673 static const char * const hibernation_modes[] = {
674 [HIBERNATION_PLATFORM] = "platform",
675 [HIBERNATION_SHUTDOWN] = "shutdown",
676 [HIBERNATION_REBOOT] = "reboot",
677 [HIBERNATION_TEST] = "test",
678 [HIBERNATION_TESTPROC] = "testproc",
682 * disk - Control hibernation mode
684 * Suspend-to-disk can be handled in several ways. We have a few options
685 * for putting the system to sleep - using the platform driver (e.g. ACPI
686 * or other hibernation_ops), powering off the system or rebooting the
687 * system (for testing) as well as the two test modes.
689 * The system can support 'platform', and that is known a priori (and
690 * encoded by the presence of hibernation_ops). However, the user may
691 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
692 * test modes, 'test' or 'testproc'.
694 * show() will display what the mode is currently set to.
695 * store() will accept one of
703 * It will only change to 'platform' if the system
704 * supports it (as determined by having hibernation_ops).
707 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
713 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
714 if (!hibernation_modes[i])
717 case HIBERNATION_SHUTDOWN:
718 case HIBERNATION_REBOOT:
719 case HIBERNATION_TEST:
720 case HIBERNATION_TESTPROC:
722 case HIBERNATION_PLATFORM:
725 /* not a valid mode, continue with loop */
728 if (i == hibernation_mode)
729 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
731 buf += sprintf(buf, "%s ", hibernation_modes[i]);
733 buf += sprintf(buf, "\n");
738 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
739 const char *buf, size_t n)
745 int mode = HIBERNATION_INVALID;
747 p = memchr(buf, '\n', n);
748 len = p ? p - buf : n;
750 mutex_lock(&pm_mutex);
751 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
752 if (len == strlen(hibernation_modes[i])
753 && !strncmp(buf, hibernation_modes[i], len)) {
758 if (mode != HIBERNATION_INVALID) {
760 case HIBERNATION_SHUTDOWN:
761 case HIBERNATION_REBOOT:
762 case HIBERNATION_TEST:
763 case HIBERNATION_TESTPROC:
764 hibernation_mode = mode;
766 case HIBERNATION_PLATFORM:
768 hibernation_mode = mode;
776 pr_debug("PM: Hibernation mode set to '%s'\n",
777 hibernation_modes[mode]);
778 mutex_unlock(&pm_mutex);
779 return error ? error : n;
784 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
787 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
788 MINOR(swsusp_resume_device));
791 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
792 const char *buf, size_t n)
794 unsigned int maj, min;
798 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
801 res = MKDEV(maj,min);
802 if (maj != MAJOR(res) || min != MINOR(res))
805 mutex_lock(&pm_mutex);
806 swsusp_resume_device = res;
807 mutex_unlock(&pm_mutex);
808 printk(KERN_INFO "PM: Starting manual resume from disk\n");
818 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
821 return sprintf(buf, "%lu\n", image_size);
824 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
825 const char *buf, size_t n)
829 if (sscanf(buf, "%lu", &size) == 1) {
837 power_attr(image_size);
839 static struct attribute * g[] = {
842 &image_size_attr.attr,
847 static struct attribute_group attr_group = {
852 static int __init pm_disk_init(void)
854 return sysfs_create_group(power_kobj, &attr_group);
857 core_initcall(pm_disk_init);
860 static int __init resume_setup(char *str)
865 strncpy( resume_file, str, 255 );
869 static int __init resume_offset_setup(char *str)
871 unsigned long long offset;
876 if (sscanf(str, "%llu", &offset) == 1)
877 swsusp_resume_block = offset;
882 static int __init noresume_setup(char *str)
888 __setup("noresume", noresume_setup);
889 __setup("resume_offset=", resume_offset_setup);
890 __setup("resume=", resume_setup);