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);
74 * platform_start - tell the platform driver that we're starting
78 static int platform_start(int platform_mode)
80 return (platform_mode && hibernation_ops) ?
81 hibernation_ops->start() : 0;
85 * platform_pre_snapshot - prepare the machine for hibernation using the
86 * platform driver if so configured and return an error code if it fails
89 static int platform_pre_snapshot(int platform_mode)
91 return (platform_mode && hibernation_ops) ?
92 hibernation_ops->pre_snapshot() : 0;
96 * platform_leave - prepare the machine for switching to the normal mode
97 * of operation using the platform driver (called with interrupts disabled)
100 static void platform_leave(int platform_mode)
102 if (platform_mode && hibernation_ops)
103 hibernation_ops->leave();
107 * platform_finish - switch the machine to the normal mode of operation
108 * using the platform driver (must be called after platform_prepare())
111 static void platform_finish(int platform_mode)
113 if (platform_mode && hibernation_ops)
114 hibernation_ops->finish();
118 * platform_pre_restore - prepare the platform for the restoration from a
119 * hibernation image. If the restore fails after this function has been
120 * called, platform_restore_cleanup() must be called.
123 static int platform_pre_restore(int platform_mode)
125 return (platform_mode && hibernation_ops) ?
126 hibernation_ops->pre_restore() : 0;
130 * platform_restore_cleanup - switch the platform to the normal mode of
131 * operation after a failing restore. If platform_pre_restore() has been
132 * called before the failing restore, this function must be called too,
133 * regardless of the result of platform_pre_restore().
136 static void platform_restore_cleanup(int platform_mode)
138 if (platform_mode && hibernation_ops)
139 hibernation_ops->restore_cleanup();
143 * create_image - freeze devices that need to be frozen with interrupts
144 * off, create the hibernation image and thaw those devices. Control
145 * reappears in this routine after a restore.
148 int create_image(int platform_mode)
152 error = arch_prepare_suspend();
157 /* At this point, device_suspend() has been called, but *not*
158 * device_power_down(). We *must* call device_power_down() now.
159 * Otherwise, drivers for some devices (e.g. interrupt controllers)
160 * become desynchronized with the actual state of the hardware
161 * at resume time, and evil weirdness ensues.
163 error = device_power_down(PMSG_FREEZE);
165 printk(KERN_ERR "Some devices failed to power down, "
166 KERN_ERR "aborting suspend\n");
170 save_processor_state();
171 error = swsusp_arch_suspend();
173 printk(KERN_ERR "Error %d while creating the image\n", error);
174 /* Restore control flow magically appears here */
175 restore_processor_state();
177 platform_leave(platform_mode);
178 /* NOTE: device_power_up() is just a resume() for devices
179 * that suspended with irqs off ... no overall powerup.
188 * hibernation_snapshot - quiesce devices and create the hibernation
190 * @platform_mode - if set, use the platform driver, if available, to
191 * prepare the platform frimware for the power transition.
193 * Must be called with pm_mutex held
196 int hibernation_snapshot(int platform_mode)
200 /* Free memory before shutting down devices. */
201 error = swsusp_shrink_memory();
205 error = platform_start(platform_mode);
210 error = device_suspend(PMSG_FREEZE);
214 error = platform_pre_snapshot(platform_mode);
218 error = disable_nonboot_cpus();
220 if (hibernation_mode != HIBERNATION_TEST) {
222 error = create_image(platform_mode);
223 /* Control returns here after successful restore */
225 printk("swsusp debug: Waiting for 5 seconds.\n");
229 enable_nonboot_cpus();
231 platform_finish(platform_mode);
239 * hibernation_restore - quiesce devices and restore the hibernation
240 * snapshot image. If successful, control returns in hibernation_snaphot()
241 * @platform_mode - if set, use the platform driver, if available, to
242 * prepare the platform frimware for the transition.
244 * Must be called with pm_mutex held
247 int hibernation_restore(int platform_mode)
251 pm_prepare_console();
253 error = device_suspend(PMSG_PRETHAW);
257 error = platform_pre_restore(platform_mode);
259 error = disable_nonboot_cpus();
261 error = swsusp_resume();
262 enable_nonboot_cpus();
264 platform_restore_cleanup(platform_mode);
268 pm_restore_console();
273 * hibernation_platform_enter - enter the hibernation state using the
274 * platform driver (if available)
277 int hibernation_platform_enter(void)
281 if (hibernation_ops) {
282 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
284 * We have cancelled the power transition by running
285 * hibernation_ops->finish() before saving the image, so we
286 * should let the firmware know that we're going to enter the
287 * sleep state after all
289 error = hibernation_ops->prepare();
292 error = hibernation_ops->enter();
300 * power_down - Shut the machine down for hibernation.
302 * Use the platform driver, if configured so; otherwise try
303 * to power off or reboot.
306 static void power_down(void)
308 switch (hibernation_mode) {
309 case HIBERNATION_TEST:
310 case HIBERNATION_TESTPROC:
312 case HIBERNATION_SHUTDOWN:
315 case HIBERNATION_REBOOT:
316 kernel_restart(NULL);
318 case HIBERNATION_PLATFORM:
319 hibernation_platform_enter();
323 * Valid image is on the disk, if we continue we risk serious data
324 * corruption after resume.
326 printk(KERN_CRIT "Please power me down manually\n");
330 static void unprepare_processes(void)
333 pm_restore_console();
336 static int prepare_processes(void)
340 pm_prepare_console();
341 if (freeze_processes()) {
343 unprepare_processes();
349 * hibernate - The granpappy of the built-in hibernation management
356 mutex_lock(&pm_mutex);
357 /* The snapshot device should not be opened while we're running */
358 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
363 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
367 /* Allocate memory management structures */
368 error = create_basic_memory_bitmaps();
372 printk("Syncing filesystems ... ");
376 error = prepare_processes();
380 if (hibernation_mode == HIBERNATION_TESTPROC) {
381 printk("swsusp debug: Waiting for 5 seconds.\n");
385 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
386 if (in_suspend && !error) {
387 unsigned int flags = 0;
389 if (hibernation_mode == HIBERNATION_PLATFORM)
390 flags |= SF_PLATFORM_MODE;
391 pr_debug("PM: writing image.\n");
392 error = swsusp_write(flags);
397 pr_debug("PM: Image restored successfully.\n");
401 unprepare_processes();
403 free_basic_memory_bitmaps();
405 pm_notifier_call_chain(PM_POST_HIBERNATION);
406 atomic_inc(&snapshot_device_available);
408 mutex_unlock(&pm_mutex);
414 * software_resume - Resume from a saved image.
416 * Called as a late_initcall (so all devices are discovered and
417 * initialized), we call swsusp to see if we have a saved image or not.
418 * If so, we quiesce devices, the restore the saved image. We will
419 * return above (in hibernate() ) if everything goes well.
420 * Otherwise, we fail gracefully and return to the normally
425 static int software_resume(void)
430 mutex_lock(&pm_mutex);
431 if (!swsusp_resume_device) {
432 if (!strlen(resume_file)) {
433 mutex_unlock(&pm_mutex);
436 swsusp_resume_device = name_to_dev_t(resume_file);
437 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
439 pr_debug("swsusp: Resume From Partition %d:%d\n",
440 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
445 * FIXME: If noresume is specified, we need to find the partition
446 * and reset it back to normal swap space.
448 mutex_unlock(&pm_mutex);
452 pr_debug("PM: Checking swsusp image.\n");
453 error = swsusp_check();
457 /* The snapshot device should not be opened while we're running */
458 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
463 error = create_basic_memory_bitmaps();
467 pr_debug("PM: Preparing processes for restore.\n");
468 error = prepare_processes();
474 pr_debug("PM: Reading swsusp image.\n");
476 error = swsusp_read(&flags);
478 hibernation_restore(flags & SF_PLATFORM_MODE);
480 printk(KERN_ERR "PM: Restore failed, recovering.\n");
482 unprepare_processes();
484 free_basic_memory_bitmaps();
486 atomic_inc(&snapshot_device_available);
487 /* For success case, the suspend path will release the lock */
489 mutex_unlock(&pm_mutex);
490 pr_debug("PM: Resume from disk failed.\n");
494 late_initcall(software_resume);
497 static const char * const hibernation_modes[] = {
498 [HIBERNATION_PLATFORM] = "platform",
499 [HIBERNATION_SHUTDOWN] = "shutdown",
500 [HIBERNATION_REBOOT] = "reboot",
501 [HIBERNATION_TEST] = "test",
502 [HIBERNATION_TESTPROC] = "testproc",
506 * disk - Control hibernation mode
508 * Suspend-to-disk can be handled in several ways. We have a few options
509 * for putting the system to sleep - using the platform driver (e.g. ACPI
510 * or other hibernation_ops), powering off the system or rebooting the
511 * system (for testing) as well as the two test modes.
513 * The system can support 'platform', and that is known a priori (and
514 * encoded by the presence of hibernation_ops). However, the user may
515 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
516 * test modes, 'test' or 'testproc'.
518 * show() will display what the mode is currently set to.
519 * store() will accept one of
527 * It will only change to 'platform' if the system
528 * supports it (as determined by having hibernation_ops).
531 static ssize_t disk_show(struct kset *kset, char *buf)
536 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
537 if (!hibernation_modes[i])
540 case HIBERNATION_SHUTDOWN:
541 case HIBERNATION_REBOOT:
542 case HIBERNATION_TEST:
543 case HIBERNATION_TESTPROC:
545 case HIBERNATION_PLATFORM:
548 /* not a valid mode, continue with loop */
551 if (i == hibernation_mode)
552 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
554 buf += sprintf(buf, "%s ", hibernation_modes[i]);
556 buf += sprintf(buf, "\n");
561 static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
567 int mode = HIBERNATION_INVALID;
569 p = memchr(buf, '\n', n);
570 len = p ? p - buf : n;
572 mutex_lock(&pm_mutex);
573 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
574 if (len == strlen(hibernation_modes[i])
575 && !strncmp(buf, hibernation_modes[i], len)) {
580 if (mode != HIBERNATION_INVALID) {
582 case HIBERNATION_SHUTDOWN:
583 case HIBERNATION_REBOOT:
584 case HIBERNATION_TEST:
585 case HIBERNATION_TESTPROC:
586 hibernation_mode = mode;
588 case HIBERNATION_PLATFORM:
590 hibernation_mode = mode;
598 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
599 hibernation_modes[mode]);
600 mutex_unlock(&pm_mutex);
601 return error ? error : n;
606 static ssize_t resume_show(struct kset *kset, char *buf)
608 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
609 MINOR(swsusp_resume_device));
612 static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
614 unsigned int maj, min;
618 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
621 res = MKDEV(maj,min);
622 if (maj != MAJOR(res) || min != MINOR(res))
625 mutex_lock(&pm_mutex);
626 swsusp_resume_device = res;
627 mutex_unlock(&pm_mutex);
628 printk("Attempting manual resume\n");
638 static ssize_t image_size_show(struct kset *kset, char *buf)
640 return sprintf(buf, "%lu\n", image_size);
643 static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
647 if (sscanf(buf, "%lu", &size) == 1) {
655 power_attr(image_size);
657 static struct attribute * g[] = {
660 &image_size_attr.attr,
665 static struct attribute_group attr_group = {
670 static int __init pm_disk_init(void)
672 return sysfs_create_group(&power_subsys.kobj, &attr_group);
675 core_initcall(pm_disk_init);
678 static int __init resume_setup(char *str)
683 strncpy( resume_file, str, 255 );
687 static int __init resume_offset_setup(char *str)
689 unsigned long long offset;
694 if (sscanf(str, "%llu", &offset) == 1)
695 swsusp_resume_block = offset;
700 static int __init noresume_setup(char *str)
706 __setup("noresume", noresume_setup);
707 __setup("resume_offset=", resume_offset_setup);
708 __setup("resume=", resume_setup);