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 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 hibernation_ops *ops)
57 if (ops && !(ops->prepare && ops->enter && ops->finish
58 && ops->pre_restore && ops->restore_cleanup)) {
62 mutex_lock(&pm_mutex);
63 hibernation_ops = ops;
65 hibernation_mode = HIBERNATION_PLATFORM;
66 else if (hibernation_mode == HIBERNATION_PLATFORM)
67 hibernation_mode = HIBERNATION_SHUTDOWN;
69 mutex_unlock(&pm_mutex);
74 * platform_prepare - prepare the machine for hibernation using the
75 * platform driver if so configured and return an error code if it fails
78 static int platform_prepare(int platform_mode)
80 return (platform_mode && hibernation_ops) ?
81 hibernation_ops->prepare() : 0;
85 * platform_finish - switch the machine to the normal mode of operation
86 * using the platform driver (must be called after platform_prepare())
89 static void platform_finish(int platform_mode)
91 if (platform_mode && hibernation_ops)
92 hibernation_ops->finish();
96 * platform_pre_restore - prepare the platform for the restoration from a
97 * hibernation image. If the restore fails after this function has been
98 * called, platform_restore_cleanup() must be called.
101 static int platform_pre_restore(int platform_mode)
103 return (platform_mode && hibernation_ops) ?
104 hibernation_ops->pre_restore() : 0;
108 * platform_restore_cleanup - switch the platform to the normal mode of
109 * operation after a failing restore. If platform_pre_restore() has been
110 * called before the failing restore, this function must be called too,
111 * regardless of the result of platform_pre_restore().
114 static void platform_restore_cleanup(int platform_mode)
116 if (platform_mode && hibernation_ops)
117 hibernation_ops->restore_cleanup();
121 * hibernation_snapshot - quiesce devices and create the hibernation
123 * @platform_mode - if set, use the platform driver, if available, to
124 * prepare the platform frimware for the power transition.
126 * Must be called with pm_mutex held
129 int hibernation_snapshot(int platform_mode)
133 /* Free memory before shutting down devices. */
134 error = swsusp_shrink_memory();
139 error = device_suspend(PMSG_FREEZE);
143 error = platform_prepare(platform_mode);
147 error = disable_nonboot_cpus();
149 if (hibernation_mode != HIBERNATION_TEST) {
151 error = swsusp_suspend();
152 /* Control returns here after successful restore */
154 printk("swsusp debug: Waiting for 5 seconds.\n");
158 enable_nonboot_cpus();
160 platform_finish(platform_mode);
168 * hibernation_restore - quiesce devices and restore the hibernation
169 * snapshot image. If successful, control returns in hibernation_snaphot()
170 * @platform_mode - if set, use the platform driver, if available, to
171 * prepare the platform frimware for the transition.
173 * Must be called with pm_mutex held
176 int hibernation_restore(int platform_mode)
180 pm_prepare_console();
182 error = device_suspend(PMSG_PRETHAW);
186 error = platform_pre_restore(platform_mode);
188 error = disable_nonboot_cpus();
190 error = swsusp_resume();
191 enable_nonboot_cpus();
193 platform_restore_cleanup(platform_mode);
197 pm_restore_console();
202 * hibernation_platform_enter - enter the hibernation state using the
203 * platform driver (if available)
206 int hibernation_platform_enter(void)
210 if (hibernation_ops) {
211 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
213 * We have cancelled the power transition by running
214 * hibernation_ops->finish() before saving the image, so we
215 * should let the firmware know that we're going to enter the
216 * sleep state after all
218 error = hibernation_ops->prepare();
220 error = hibernation_ops->enter();
228 * power_down - Shut the machine down for hibernation.
230 * Use the platform driver, if configured so; otherwise try
231 * to power off or reboot.
234 static void power_down(void)
236 switch (hibernation_mode) {
237 case HIBERNATION_TEST:
238 case HIBERNATION_TESTPROC:
240 case HIBERNATION_SHUTDOWN:
243 case HIBERNATION_REBOOT:
244 kernel_restart(NULL);
246 case HIBERNATION_PLATFORM:
247 hibernation_platform_enter();
251 * Valid image is on the disk, if we continue we risk serious data
252 * corruption after resume.
254 printk(KERN_CRIT "Please power me down manually\n");
258 static void unprepare_processes(void)
261 pm_restore_console();
264 static int prepare_processes(void)
268 pm_prepare_console();
269 if (freeze_processes()) {
271 unprepare_processes();
277 * hibernate - The granpappy of the built-in hibernation management
284 mutex_lock(&pm_mutex);
285 /* The snapshot device should not be opened while we're running */
286 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
291 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
295 /* Allocate memory management structures */
296 error = create_basic_memory_bitmaps();
300 error = prepare_processes();
304 if (hibernation_mode == HIBERNATION_TESTPROC) {
305 printk("swsusp debug: Waiting for 5 seconds.\n");
309 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
310 if (in_suspend && !error) {
311 unsigned int flags = 0;
313 if (hibernation_mode == HIBERNATION_PLATFORM)
314 flags |= SF_PLATFORM_MODE;
315 pr_debug("PM: writing image.\n");
316 error = swsusp_write(flags);
321 pr_debug("PM: Image restored successfully.\n");
325 unprepare_processes();
327 free_basic_memory_bitmaps();
329 pm_notifier_call_chain(PM_POST_HIBERNATION);
330 atomic_inc(&snapshot_device_available);
332 mutex_unlock(&pm_mutex);
338 * software_resume - Resume from a saved image.
340 * Called as a late_initcall (so all devices are discovered and
341 * initialized), we call swsusp to see if we have a saved image or not.
342 * If so, we quiesce devices, the restore the saved image. We will
343 * return above (in hibernate() ) if everything goes well.
344 * Otherwise, we fail gracefully and return to the normally
349 static int software_resume(void)
354 mutex_lock(&pm_mutex);
355 if (!swsusp_resume_device) {
356 if (!strlen(resume_file)) {
357 mutex_unlock(&pm_mutex);
360 swsusp_resume_device = name_to_dev_t(resume_file);
361 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
363 pr_debug("swsusp: Resume From Partition %d:%d\n",
364 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
369 * FIXME: If noresume is specified, we need to find the partition
370 * and reset it back to normal swap space.
372 mutex_unlock(&pm_mutex);
376 pr_debug("PM: Checking swsusp image.\n");
377 error = swsusp_check();
381 /* The snapshot device should not be opened while we're running */
382 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
387 error = create_basic_memory_bitmaps();
391 pr_debug("PM: Preparing processes for restore.\n");
392 error = prepare_processes();
398 pr_debug("PM: Reading swsusp image.\n");
400 error = swsusp_read(&flags);
402 hibernation_restore(flags & SF_PLATFORM_MODE);
404 printk(KERN_ERR "PM: Restore failed, recovering.\n");
406 unprepare_processes();
408 free_basic_memory_bitmaps();
410 atomic_inc(&snapshot_device_available);
411 /* For success case, the suspend path will release the lock */
413 mutex_unlock(&pm_mutex);
414 pr_debug("PM: Resume from disk failed.\n");
418 late_initcall(software_resume);
421 static const char * const hibernation_modes[] = {
422 [HIBERNATION_PLATFORM] = "platform",
423 [HIBERNATION_SHUTDOWN] = "shutdown",
424 [HIBERNATION_REBOOT] = "reboot",
425 [HIBERNATION_TEST] = "test",
426 [HIBERNATION_TESTPROC] = "testproc",
430 * disk - Control hibernation mode
432 * Suspend-to-disk can be handled in several ways. We have a few options
433 * for putting the system to sleep - using the platform driver (e.g. ACPI
434 * or other hibernation_ops), powering off the system or rebooting the
435 * system (for testing) as well as the two test modes.
437 * The system can support 'platform', and that is known a priori (and
438 * encoded by the presence of hibernation_ops). However, the user may
439 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
440 * test modes, 'test' or 'testproc'.
442 * show() will display what the mode is currently set to.
443 * store() will accept one of
451 * It will only change to 'platform' if the system
452 * supports it (as determined by having hibernation_ops).
455 static ssize_t disk_show(struct kset *kset, char *buf)
460 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
461 if (!hibernation_modes[i])
464 case HIBERNATION_SHUTDOWN:
465 case HIBERNATION_REBOOT:
466 case HIBERNATION_TEST:
467 case HIBERNATION_TESTPROC:
469 case HIBERNATION_PLATFORM:
472 /* not a valid mode, continue with loop */
475 if (i == hibernation_mode)
476 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
478 buf += sprintf(buf, "%s ", hibernation_modes[i]);
480 buf += sprintf(buf, "\n");
485 static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
491 int mode = HIBERNATION_INVALID;
493 p = memchr(buf, '\n', n);
494 len = p ? p - buf : n;
496 mutex_lock(&pm_mutex);
497 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
498 if (len == strlen(hibernation_modes[i])
499 && !strncmp(buf, hibernation_modes[i], len)) {
504 if (mode != HIBERNATION_INVALID) {
506 case HIBERNATION_SHUTDOWN:
507 case HIBERNATION_REBOOT:
508 case HIBERNATION_TEST:
509 case HIBERNATION_TESTPROC:
510 hibernation_mode = mode;
512 case HIBERNATION_PLATFORM:
514 hibernation_mode = mode;
522 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
523 hibernation_modes[mode]);
524 mutex_unlock(&pm_mutex);
525 return error ? error : n;
530 static ssize_t resume_show(struct kset *kset, char *buf)
532 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
533 MINOR(swsusp_resume_device));
536 static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
538 unsigned int maj, min;
542 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
545 res = MKDEV(maj,min);
546 if (maj != MAJOR(res) || min != MINOR(res))
549 mutex_lock(&pm_mutex);
550 swsusp_resume_device = res;
551 mutex_unlock(&pm_mutex);
552 printk("Attempting manual resume\n");
562 static ssize_t image_size_show(struct kset *kset, char *buf)
564 return sprintf(buf, "%lu\n", image_size);
567 static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
571 if (sscanf(buf, "%lu", &size) == 1) {
579 power_attr(image_size);
581 static struct attribute * g[] = {
584 &image_size_attr.attr,
589 static struct attribute_group attr_group = {
594 static int __init pm_disk_init(void)
596 return sysfs_create_group(&power_subsys.kobj, &attr_group);
599 core_initcall(pm_disk_init);
602 static int __init resume_setup(char *str)
607 strncpy( resume_file, str, 255 );
611 static int __init resume_offset_setup(char *str)
613 unsigned long long offset;
618 if (sscanf(str, "%llu", &offset) == 1)
619 swsusp_resume_block = offset;
624 static int __init noresume_setup(char *str)
630 __setup("noresume", noresume_setup);
631 __setup("resume_offset=", resume_offset_setup);
632 __setup("resume=", resume_setup);