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->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_finish - switch the machine to the normal mode of operation
97 * using the platform driver (must be called after platform_prepare())
100 static void platform_finish(int platform_mode)
102 if (platform_mode && hibernation_ops)
103 hibernation_ops->finish();
107 * platform_pre_restore - prepare the platform for the restoration from a
108 * hibernation image. If the restore fails after this function has been
109 * called, platform_restore_cleanup() must be called.
112 static int platform_pre_restore(int platform_mode)
114 return (platform_mode && hibernation_ops) ?
115 hibernation_ops->pre_restore() : 0;
119 * platform_restore_cleanup - switch the platform to the normal mode of
120 * operation after a failing restore. If platform_pre_restore() has been
121 * called before the failing restore, this function must be called too,
122 * regardless of the result of platform_pre_restore().
125 static void platform_restore_cleanup(int platform_mode)
127 if (platform_mode && hibernation_ops)
128 hibernation_ops->restore_cleanup();
132 * hibernation_snapshot - quiesce devices and create the hibernation
134 * @platform_mode - if set, use the platform driver, if available, to
135 * prepare the platform frimware for the power transition.
137 * Must be called with pm_mutex held
140 int hibernation_snapshot(int platform_mode)
144 /* Free memory before shutting down devices. */
145 error = swsusp_shrink_memory();
149 error = platform_start(platform_mode);
154 error = device_suspend(PMSG_FREEZE);
158 error = platform_pre_snapshot(platform_mode);
162 error = disable_nonboot_cpus();
164 if (hibernation_mode != HIBERNATION_TEST) {
166 error = swsusp_suspend();
167 /* Control returns here after successful restore */
169 printk("swsusp debug: Waiting for 5 seconds.\n");
173 enable_nonboot_cpus();
175 platform_finish(platform_mode);
183 * hibernation_restore - quiesce devices and restore the hibernation
184 * snapshot image. If successful, control returns in hibernation_snaphot()
185 * @platform_mode - if set, use the platform driver, if available, to
186 * prepare the platform frimware for the transition.
188 * Must be called with pm_mutex held
191 int hibernation_restore(int platform_mode)
195 pm_prepare_console();
197 error = device_suspend(PMSG_PRETHAW);
201 error = platform_pre_restore(platform_mode);
203 error = disable_nonboot_cpus();
205 error = swsusp_resume();
206 enable_nonboot_cpus();
208 platform_restore_cleanup(platform_mode);
212 pm_restore_console();
217 * hibernation_platform_enter - enter the hibernation state using the
218 * platform driver (if available)
221 int hibernation_platform_enter(void)
225 if (hibernation_ops) {
226 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
228 * We have cancelled the power transition by running
229 * hibernation_ops->finish() before saving the image, so we
230 * should let the firmware know that we're going to enter the
231 * sleep state after all
233 error = hibernation_ops->prepare();
236 error = hibernation_ops->enter();
244 * power_down - Shut the machine down for hibernation.
246 * Use the platform driver, if configured so; otherwise try
247 * to power off or reboot.
250 static void power_down(void)
252 switch (hibernation_mode) {
253 case HIBERNATION_TEST:
254 case HIBERNATION_TESTPROC:
256 case HIBERNATION_SHUTDOWN:
259 case HIBERNATION_REBOOT:
260 kernel_restart(NULL);
262 case HIBERNATION_PLATFORM:
263 hibernation_platform_enter();
267 * Valid image is on the disk, if we continue we risk serious data
268 * corruption after resume.
270 printk(KERN_CRIT "Please power me down manually\n");
274 static void unprepare_processes(void)
277 pm_restore_console();
280 static int prepare_processes(void)
284 pm_prepare_console();
285 if (freeze_processes()) {
287 unprepare_processes();
293 * hibernate - The granpappy of the built-in hibernation management
300 mutex_lock(&pm_mutex);
301 /* The snapshot device should not be opened while we're running */
302 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
307 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
311 /* Allocate memory management structures */
312 error = create_basic_memory_bitmaps();
316 error = prepare_processes();
320 if (hibernation_mode == HIBERNATION_TESTPROC) {
321 printk("swsusp debug: Waiting for 5 seconds.\n");
325 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
326 if (in_suspend && !error) {
327 unsigned int flags = 0;
329 if (hibernation_mode == HIBERNATION_PLATFORM)
330 flags |= SF_PLATFORM_MODE;
331 pr_debug("PM: writing image.\n");
332 error = swsusp_write(flags);
337 pr_debug("PM: Image restored successfully.\n");
341 unprepare_processes();
343 free_basic_memory_bitmaps();
345 pm_notifier_call_chain(PM_POST_HIBERNATION);
346 atomic_inc(&snapshot_device_available);
348 mutex_unlock(&pm_mutex);
354 * software_resume - Resume from a saved image.
356 * Called as a late_initcall (so all devices are discovered and
357 * initialized), we call swsusp to see if we have a saved image or not.
358 * If so, we quiesce devices, the restore the saved image. We will
359 * return above (in hibernate() ) if everything goes well.
360 * Otherwise, we fail gracefully and return to the normally
365 static int software_resume(void)
370 mutex_lock(&pm_mutex);
371 if (!swsusp_resume_device) {
372 if (!strlen(resume_file)) {
373 mutex_unlock(&pm_mutex);
376 swsusp_resume_device = name_to_dev_t(resume_file);
377 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
379 pr_debug("swsusp: Resume From Partition %d:%d\n",
380 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
385 * FIXME: If noresume is specified, we need to find the partition
386 * and reset it back to normal swap space.
388 mutex_unlock(&pm_mutex);
392 pr_debug("PM: Checking swsusp image.\n");
393 error = swsusp_check();
397 /* The snapshot device should not be opened while we're running */
398 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
403 error = create_basic_memory_bitmaps();
407 pr_debug("PM: Preparing processes for restore.\n");
408 error = prepare_processes();
414 pr_debug("PM: Reading swsusp image.\n");
416 error = swsusp_read(&flags);
418 hibernation_restore(flags & SF_PLATFORM_MODE);
420 printk(KERN_ERR "PM: Restore failed, recovering.\n");
422 unprepare_processes();
424 free_basic_memory_bitmaps();
426 atomic_inc(&snapshot_device_available);
427 /* For success case, the suspend path will release the lock */
429 mutex_unlock(&pm_mutex);
430 pr_debug("PM: Resume from disk failed.\n");
434 late_initcall(software_resume);
437 static const char * const hibernation_modes[] = {
438 [HIBERNATION_PLATFORM] = "platform",
439 [HIBERNATION_SHUTDOWN] = "shutdown",
440 [HIBERNATION_REBOOT] = "reboot",
441 [HIBERNATION_TEST] = "test",
442 [HIBERNATION_TESTPROC] = "testproc",
446 * disk - Control hibernation mode
448 * Suspend-to-disk can be handled in several ways. We have a few options
449 * for putting the system to sleep - using the platform driver (e.g. ACPI
450 * or other hibernation_ops), powering off the system or rebooting the
451 * system (for testing) as well as the two test modes.
453 * The system can support 'platform', and that is known a priori (and
454 * encoded by the presence of hibernation_ops). However, the user may
455 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
456 * test modes, 'test' or 'testproc'.
458 * show() will display what the mode is currently set to.
459 * store() will accept one of
467 * It will only change to 'platform' if the system
468 * supports it (as determined by having hibernation_ops).
471 static ssize_t disk_show(struct kset *kset, char *buf)
476 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
477 if (!hibernation_modes[i])
480 case HIBERNATION_SHUTDOWN:
481 case HIBERNATION_REBOOT:
482 case HIBERNATION_TEST:
483 case HIBERNATION_TESTPROC:
485 case HIBERNATION_PLATFORM:
488 /* not a valid mode, continue with loop */
491 if (i == hibernation_mode)
492 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
494 buf += sprintf(buf, "%s ", hibernation_modes[i]);
496 buf += sprintf(buf, "\n");
501 static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
507 int mode = HIBERNATION_INVALID;
509 p = memchr(buf, '\n', n);
510 len = p ? p - buf : n;
512 mutex_lock(&pm_mutex);
513 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
514 if (len == strlen(hibernation_modes[i])
515 && !strncmp(buf, hibernation_modes[i], len)) {
520 if (mode != HIBERNATION_INVALID) {
522 case HIBERNATION_SHUTDOWN:
523 case HIBERNATION_REBOOT:
524 case HIBERNATION_TEST:
525 case HIBERNATION_TESTPROC:
526 hibernation_mode = mode;
528 case HIBERNATION_PLATFORM:
530 hibernation_mode = mode;
538 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
539 hibernation_modes[mode]);
540 mutex_unlock(&pm_mutex);
541 return error ? error : n;
546 static ssize_t resume_show(struct kset *kset, char *buf)
548 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
549 MINOR(swsusp_resume_device));
552 static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
554 unsigned int maj, min;
558 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
561 res = MKDEV(maj,min);
562 if (maj != MAJOR(res) || min != MINOR(res))
565 mutex_lock(&pm_mutex);
566 swsusp_resume_device = res;
567 mutex_unlock(&pm_mutex);
568 printk("Attempting manual resume\n");
578 static ssize_t image_size_show(struct kset *kset, char *buf)
580 return sprintf(buf, "%lu\n", image_size);
583 static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
587 if (sscanf(buf, "%lu", &size) == 1) {
595 power_attr(image_size);
597 static struct attribute * g[] = {
600 &image_size_attr.attr,
605 static struct attribute_group attr_group = {
610 static int __init pm_disk_init(void)
612 return sysfs_create_group(&power_subsys.kobj, &attr_group);
615 core_initcall(pm_disk_init);
618 static int __init resume_setup(char *str)
623 strncpy( resume_file, str, 255 );
627 static int __init resume_offset_setup(char *str)
629 unsigned long long offset;
634 if (sscanf(str, "%llu", &offset) == 1)
635 swsusp_resume_block = offset;
640 static int __init noresume_setup(char *str)
646 __setup("noresume", noresume_setup);
647 __setup("resume_offset=", resume_offset_setup);
648 __setup("resume=", resume_setup);