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 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
->start
&& ops
->pre_snapshot
&& ops
->finish
58 && ops
->prepare
&& 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_start - tell the platform driver that we're starting
107 static int platform_start(int platform_mode
)
109 return (platform_mode
&& hibernation_ops
) ?
110 hibernation_ops
->start() : 0;
114 * platform_pre_snapshot - prepare the machine for hibernation using the
115 * platform driver if so configured and return an error code if it fails
118 static int platform_pre_snapshot(int platform_mode
)
120 return (platform_mode
&& hibernation_ops
) ?
121 hibernation_ops
->pre_snapshot() : 0;
125 * platform_leave - prepare the machine for switching to the normal mode
126 * of operation using the platform driver (called with interrupts disabled)
129 static void platform_leave(int platform_mode
)
131 if (platform_mode
&& hibernation_ops
)
132 hibernation_ops
->leave();
136 * platform_finish - switch the machine to the normal mode of operation
137 * using the platform driver (must be called after platform_prepare())
140 static void platform_finish(int platform_mode
)
142 if (platform_mode
&& hibernation_ops
)
143 hibernation_ops
->finish();
147 * platform_pre_restore - prepare the platform for the restoration from a
148 * hibernation image. If the restore fails after this function has been
149 * called, platform_restore_cleanup() must be called.
152 static int platform_pre_restore(int platform_mode
)
154 return (platform_mode
&& hibernation_ops
) ?
155 hibernation_ops
->pre_restore() : 0;
159 * platform_restore_cleanup - switch the platform to the normal mode of
160 * operation after a failing restore. If platform_pre_restore() has been
161 * called before the failing restore, this function must be called too,
162 * regardless of the result of platform_pre_restore().
165 static void platform_restore_cleanup(int platform_mode
)
167 if (platform_mode
&& hibernation_ops
)
168 hibernation_ops
->restore_cleanup();
172 * create_image - freeze devices that need to be frozen with interrupts
173 * off, create the hibernation image and thaw those devices. Control
174 * reappears in this routine after a restore.
177 int create_image(int platform_mode
)
181 error
= arch_prepare_suspend();
186 /* At this point, device_suspend() has been called, but *not*
187 * device_power_down(). We *must* call device_power_down() now.
188 * Otherwise, drivers for some devices (e.g. interrupt controllers)
189 * become desynchronized with the actual state of the hardware
190 * at resume time, and evil weirdness ensues.
192 error
= device_power_down(PMSG_FREEZE
);
194 printk(KERN_ERR
"PM: Some devices failed to power down, "
195 "aborting hibernation\n");
199 if (hibernation_test(TEST_CORE
))
203 save_processor_state();
204 error
= swsusp_arch_suspend();
206 printk(KERN_ERR
"PM: Error %d creating hibernation image\n",
208 /* Restore control flow magically appears here */
209 restore_processor_state();
211 platform_leave(platform_mode
);
213 /* NOTE: device_power_up() is just a resume() for devices
214 * that suspended with irqs off ... no overall powerup.
223 * hibernation_snapshot - quiesce devices and create the hibernation
225 * @platform_mode - if set, use the platform driver, if available, to
226 * prepare the platform frimware for the power transition.
228 * Must be called with pm_mutex held
231 int hibernation_snapshot(int platform_mode
)
235 /* Free memory before shutting down devices. */
236 error
= swsusp_shrink_memory();
240 error
= platform_start(platform_mode
);
245 error
= device_suspend(PMSG_FREEZE
);
249 if (hibernation_test(TEST_DEVICES
))
252 error
= platform_pre_snapshot(platform_mode
);
253 if (error
|| hibernation_test(TEST_PLATFORM
))
256 error
= disable_nonboot_cpus();
258 if (hibernation_test(TEST_CPUS
))
261 if (hibernation_testmode(HIBERNATION_TEST
))
264 error
= create_image(platform_mode
);
265 /* Control returns here after successful restore */
268 enable_nonboot_cpus();
270 platform_finish(platform_mode
);
279 * resume_target_kernel - prepare devices that need to be suspended with
280 * interrupts off, restore the contents of highmem that have not been
281 * restored yet from the image and run the low level code that will restore
282 * the remaining contents of memory and switch to the just restored target
286 static int resume_target_kernel(void)
291 error
= device_power_down(PMSG_PRETHAW
);
293 printk(KERN_ERR
"PM: Some devices failed to power down, "
294 "aborting resume\n");
297 /* We'll ignore saved state, but this gets preempt count (etc) right */
298 save_processor_state();
299 error
= restore_highmem();
301 error
= swsusp_arch_resume();
303 * The code below is only ever reached in case of a failure.
304 * Otherwise execution continues at place where
305 * swsusp_arch_suspend() was called
308 /* This call to restore_highmem() undos the previous one */
312 * The only reason why swsusp_arch_resume() can fail is memory being
313 * very tight, so we have to free it as soon as we can to avoid
314 * subsequent failures
317 restore_processor_state();
318 touch_softlockup_watchdog();
326 * hibernation_restore - quiesce devices and restore the hibernation
327 * snapshot image. If successful, control returns in hibernation_snaphot()
328 * @platform_mode - if set, use the platform driver, if available, to
329 * prepare the platform frimware for the transition.
331 * Must be called with pm_mutex held
334 int hibernation_restore(int platform_mode
)
338 pm_prepare_console();
340 error
= device_suspend(PMSG_PRETHAW
);
344 error
= platform_pre_restore(platform_mode
);
346 error
= disable_nonboot_cpus();
348 error
= resume_target_kernel();
349 enable_nonboot_cpus();
351 platform_restore_cleanup(platform_mode
);
355 pm_restore_console();
360 * hibernation_platform_enter - enter the hibernation state using the
361 * platform driver (if available)
364 int hibernation_platform_enter(void)
368 if (!hibernation_ops
)
372 * We have cancelled the power transition by running
373 * hibernation_ops->finish() before saving the image, so we should let
374 * the firmware know that we're going to enter the sleep state after all
376 error
= hibernation_ops
->start();
381 error
= device_suspend(PMSG_SUSPEND
);
385 error
= hibernation_ops
->prepare();
389 error
= disable_nonboot_cpus();
394 error
= device_power_down(PMSG_SUSPEND
);
396 hibernation_ops
->enter();
397 /* We should never get here */
403 * We don't need to reenable the nonboot CPUs or resume consoles, since
404 * the system is going to be halted anyway.
407 hibernation_ops
->finish();
416 * power_down - Shut the machine down for hibernation.
418 * Use the platform driver, if configured so; otherwise try
419 * to power off or reboot.
422 static void power_down(void)
424 switch (hibernation_mode
) {
425 case HIBERNATION_TEST
:
426 case HIBERNATION_TESTPROC
:
428 case HIBERNATION_REBOOT
:
429 kernel_restart(NULL
);
431 case HIBERNATION_PLATFORM
:
432 hibernation_platform_enter();
433 case HIBERNATION_SHUTDOWN
:
439 * Valid image is on the disk, if we continue we risk serious data
440 * corruption after resume.
442 printk(KERN_CRIT
"PM: Please power down manually\n");
446 static void unprepare_processes(void)
449 pm_restore_console();
452 static int prepare_processes(void)
456 pm_prepare_console();
457 if (freeze_processes()) {
459 unprepare_processes();
465 * hibernate - The granpappy of the built-in hibernation management
472 mutex_lock(&pm_mutex
);
473 /* The snapshot device should not be opened while we're running */
474 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
479 error
= pm_notifier_call_chain(PM_HIBERNATION_PREPARE
);
483 /* Allocate memory management structures */
484 error
= create_basic_memory_bitmaps();
488 printk(KERN_INFO
"PM: Syncing filesystems ... ");
492 error
= prepare_processes();
496 if (hibernation_test(TEST_FREEZER
))
499 if (hibernation_testmode(HIBERNATION_TESTPROC
))
502 error
= hibernation_snapshot(hibernation_mode
== HIBERNATION_PLATFORM
);
503 if (in_suspend
&& !error
) {
504 unsigned int flags
= 0;
506 if (hibernation_mode
== HIBERNATION_PLATFORM
)
507 flags
|= SF_PLATFORM_MODE
;
508 pr_debug("PM: writing image.\n");
509 error
= swsusp_write(flags
);
514 pr_debug("PM: Image restored successfully.\n");
518 unprepare_processes();
520 free_basic_memory_bitmaps();
522 pm_notifier_call_chain(PM_POST_HIBERNATION
);
523 atomic_inc(&snapshot_device_available
);
525 mutex_unlock(&pm_mutex
);
531 * software_resume - Resume from a saved image.
533 * Called as a late_initcall (so all devices are discovered and
534 * initialized), we call swsusp to see if we have a saved image or not.
535 * If so, we quiesce devices, the restore the saved image. We will
536 * return above (in hibernate() ) if everything goes well.
537 * Otherwise, we fail gracefully and return to the normally
542 static int software_resume(void)
548 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
549 * is configured into the kernel. Since the regular hibernate
550 * trigger path is via sysfs which takes a buffer mutex before
551 * calling hibernate functions (which take pm_mutex) this can
552 * cause lockdep to complain about a possible ABBA deadlock
553 * which cannot happen since we're in the boot code here and
554 * sysfs can't be invoked yet. Therefore, we use a subclass
555 * here to avoid lockdep complaining.
557 mutex_lock_nested(&pm_mutex
, SINGLE_DEPTH_NESTING
);
558 if (!swsusp_resume_device
) {
559 if (!strlen(resume_file
)) {
560 mutex_unlock(&pm_mutex
);
563 swsusp_resume_device
= name_to_dev_t(resume_file
);
564 pr_debug("PM: Resume from partition %s\n", resume_file
);
566 pr_debug("PM: Resume from partition %d:%d\n",
567 MAJOR(swsusp_resume_device
),
568 MINOR(swsusp_resume_device
));
573 * FIXME: If noresume is specified, we need to find the
574 * partition and reset it back to normal swap space.
576 mutex_unlock(&pm_mutex
);
580 pr_debug("PM: Checking hibernation image.\n");
581 error
= swsusp_check();
585 /* The snapshot device should not be opened while we're running */
586 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
591 error
= pm_notifier_call_chain(PM_RESTORE_PREPARE
);
595 error
= create_basic_memory_bitmaps();
599 pr_debug("PM: Preparing processes for restore.\n");
600 error
= prepare_processes();
606 pr_debug("PM: Reading hibernation image.\n");
608 error
= swsusp_read(&flags
);
610 hibernation_restore(flags
& SF_PLATFORM_MODE
);
612 printk(KERN_ERR
"PM: Restore failed, recovering.\n");
614 unprepare_processes();
616 free_basic_memory_bitmaps();
618 pm_notifier_call_chain(PM_POST_RESTORE
);
619 atomic_inc(&snapshot_device_available
);
620 /* For success case, the suspend path will release the lock */
622 mutex_unlock(&pm_mutex
);
623 pr_debug("PM: Resume from disk failed.\n");
627 late_initcall(software_resume
);
630 static const char * const hibernation_modes
[] = {
631 [HIBERNATION_PLATFORM
] = "platform",
632 [HIBERNATION_SHUTDOWN
] = "shutdown",
633 [HIBERNATION_REBOOT
] = "reboot",
634 [HIBERNATION_TEST
] = "test",
635 [HIBERNATION_TESTPROC
] = "testproc",
639 * disk - Control hibernation mode
641 * Suspend-to-disk can be handled in several ways. We have a few options
642 * for putting the system to sleep - using the platform driver (e.g. ACPI
643 * or other hibernation_ops), powering off the system or rebooting the
644 * system (for testing) as well as the two test modes.
646 * The system can support 'platform', and that is known a priori (and
647 * encoded by the presence of hibernation_ops). However, the user may
648 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
649 * test modes, 'test' or 'testproc'.
651 * show() will display what the mode is currently set to.
652 * store() will accept one of
660 * It will only change to 'platform' if the system
661 * supports it (as determined by having hibernation_ops).
664 static ssize_t
disk_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
670 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
671 if (!hibernation_modes
[i
])
674 case HIBERNATION_SHUTDOWN
:
675 case HIBERNATION_REBOOT
:
676 case HIBERNATION_TEST
:
677 case HIBERNATION_TESTPROC
:
679 case HIBERNATION_PLATFORM
:
682 /* not a valid mode, continue with loop */
685 if (i
== hibernation_mode
)
686 buf
+= sprintf(buf
, "[%s] ", hibernation_modes
[i
]);
688 buf
+= sprintf(buf
, "%s ", hibernation_modes
[i
]);
690 buf
+= sprintf(buf
, "\n");
695 static ssize_t
disk_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
696 const char *buf
, size_t n
)
702 int mode
= HIBERNATION_INVALID
;
704 p
= memchr(buf
, '\n', n
);
705 len
= p
? p
- buf
: n
;
707 mutex_lock(&pm_mutex
);
708 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
709 if (len
== strlen(hibernation_modes
[i
])
710 && !strncmp(buf
, hibernation_modes
[i
], len
)) {
715 if (mode
!= HIBERNATION_INVALID
) {
717 case HIBERNATION_SHUTDOWN
:
718 case HIBERNATION_REBOOT
:
719 case HIBERNATION_TEST
:
720 case HIBERNATION_TESTPROC
:
721 hibernation_mode
= mode
;
723 case HIBERNATION_PLATFORM
:
725 hibernation_mode
= mode
;
733 pr_debug("PM: Hibernation mode set to '%s'\n",
734 hibernation_modes
[mode
]);
735 mutex_unlock(&pm_mutex
);
736 return error
? error
: n
;
741 static ssize_t
resume_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
744 return sprintf(buf
,"%d:%d\n", MAJOR(swsusp_resume_device
),
745 MINOR(swsusp_resume_device
));
748 static ssize_t
resume_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
749 const char *buf
, size_t n
)
751 unsigned int maj
, min
;
755 if (sscanf(buf
, "%u:%u", &maj
, &min
) != 2)
758 res
= MKDEV(maj
,min
);
759 if (maj
!= MAJOR(res
) || min
!= MINOR(res
))
762 mutex_lock(&pm_mutex
);
763 swsusp_resume_device
= res
;
764 mutex_unlock(&pm_mutex
);
765 printk(KERN_INFO
"PM: Starting manual resume from disk\n");
775 static ssize_t
image_size_show(struct kobject
*kobj
, struct kobj_attribute
*attr
,
778 return sprintf(buf
, "%lu\n", image_size
);
781 static ssize_t
image_size_store(struct kobject
*kobj
, struct kobj_attribute
*attr
,
782 const char *buf
, size_t n
)
786 if (sscanf(buf
, "%lu", &size
) == 1) {
794 power_attr(image_size
);
796 static struct attribute
* g
[] = {
799 &image_size_attr
.attr
,
804 static struct attribute_group attr_group
= {
809 static int __init
pm_disk_init(void)
811 return sysfs_create_group(power_kobj
, &attr_group
);
814 core_initcall(pm_disk_init
);
817 static int __init
resume_setup(char *str
)
822 strncpy( resume_file
, str
, 255 );
826 static int __init
resume_offset_setup(char *str
)
828 unsigned long long offset
;
833 if (sscanf(str
, "%llu", &offset
) == 1)
834 swsusp_resume_block
= offset
;
839 static int __init
noresume_setup(char *str
)
845 __setup("noresume", noresume_setup
);
846 __setup("resume_offset=", resume_offset_setup
);
847 __setup("resume=", resume_setup
);