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)) {
61 mutex_lock(&pm_mutex);
62 hibernation_ops = ops;
64 hibernation_mode = HIBERNATION_PLATFORM;
65 else if (hibernation_mode == HIBERNATION_PLATFORM)
66 hibernation_mode = HIBERNATION_SHUTDOWN;
68 mutex_unlock(&pm_mutex);
73 * platform_prepare - prepare the machine for hibernation using the
74 * platform driver if so configured and return an error code if it fails
77 static int platform_prepare(int platform_mode)
79 return (platform_mode && hibernation_ops) ?
80 hibernation_ops->prepare() : 0;
84 * platform_finish - switch the machine to the normal mode of operation
85 * using the platform driver (must be called after platform_prepare())
88 static void platform_finish(int platform_mode)
90 if (platform_mode && hibernation_ops)
91 hibernation_ops->finish();
95 * hibernation_snapshot - quiesce devices and create the hibernation
97 * @platform_mode - if set, use the platform driver, if available, to
98 * prepare the platform frimware for the power transition.
100 * Must be called with pm_mutex held
103 int hibernation_snapshot(int platform_mode)
107 /* Free memory before shutting down devices. */
108 error = swsusp_shrink_memory();
112 error = platform_prepare(platform_mode);
117 error = device_suspend(PMSG_FREEZE);
121 error = disable_nonboot_cpus();
123 if (hibernation_mode != HIBERNATION_TEST) {
125 error = swsusp_suspend();
126 /* Control returns here after successful restore */
128 printk("swsusp debug: Waiting for 5 seconds.\n");
132 enable_nonboot_cpus();
134 platform_finish(platform_mode);
142 * hibernation_restore - quiesce devices and restore the hibernation
143 * snapshot image. If successful, control returns in hibernation_snaphot()
145 * Must be called with pm_mutex held
148 int hibernation_restore(void)
152 pm_prepare_console();
154 error = device_suspend(PMSG_PRETHAW);
158 error = disable_nonboot_cpus();
160 error = swsusp_resume();
162 enable_nonboot_cpus();
166 pm_restore_console();
171 * hibernation_platform_enter - enter the hibernation state using the
172 * platform driver (if available)
175 int hibernation_platform_enter(void)
177 if (hibernation_ops) {
178 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK);
179 return hibernation_ops->enter();
186 * power_down - Shut the machine down for hibernation.
188 * Use the platform driver, if configured so; otherwise try
189 * to power off or reboot.
192 static void power_down(void)
194 switch (hibernation_mode) {
195 case HIBERNATION_TEST:
196 case HIBERNATION_TESTPROC:
198 case HIBERNATION_SHUTDOWN:
201 case HIBERNATION_REBOOT:
202 kernel_restart(NULL);
204 case HIBERNATION_PLATFORM:
205 hibernation_platform_enter();
209 * Valid image is on the disk, if we continue we risk serious data
210 * corruption after resume.
212 printk(KERN_CRIT "Please power me down manually\n");
216 static void unprepare_processes(void)
219 pm_restore_console();
222 static int prepare_processes(void)
226 pm_prepare_console();
227 if (freeze_processes()) {
229 unprepare_processes();
235 * hibernate - The granpappy of the built-in hibernation management
242 /* The snapshot device should not be opened while we're running */
243 if (!atomic_add_unless(&snapshot_device_available, -1, 0))
246 /* Allocate memory management structures */
247 error = create_basic_memory_bitmaps();
251 error = prepare_processes();
255 mutex_lock(&pm_mutex);
256 if (hibernation_mode == HIBERNATION_TESTPROC) {
257 printk("swsusp debug: Waiting for 5 seconds.\n");
261 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
262 if (in_suspend && !error) {
263 pr_debug("PM: writing image.\n");
264 error = swsusp_write();
269 pr_debug("PM: Image restored successfully.\n");
273 mutex_unlock(&pm_mutex);
274 unprepare_processes();
276 free_basic_memory_bitmaps();
278 atomic_inc(&snapshot_device_available);
284 * software_resume - Resume from a saved image.
286 * Called as a late_initcall (so all devices are discovered and
287 * initialized), we call swsusp to see if we have a saved image or not.
288 * If so, we quiesce devices, the restore the saved image. We will
289 * return above (in hibernate() ) if everything goes well.
290 * Otherwise, we fail gracefully and return to the normally
295 static int software_resume(void)
299 mutex_lock(&pm_mutex);
300 if (!swsusp_resume_device) {
301 if (!strlen(resume_file)) {
302 mutex_unlock(&pm_mutex);
305 swsusp_resume_device = name_to_dev_t(resume_file);
306 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
308 pr_debug("swsusp: Resume From Partition %d:%d\n",
309 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
314 * FIXME: If noresume is specified, we need to find the partition
315 * and reset it back to normal swap space.
317 mutex_unlock(&pm_mutex);
321 pr_debug("PM: Checking swsusp image.\n");
322 error = swsusp_check();
326 /* The snapshot device should not be opened while we're running */
327 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
332 error = create_basic_memory_bitmaps();
336 pr_debug("PM: Preparing processes for restore.\n");
337 error = prepare_processes();
343 pr_debug("PM: Reading swsusp image.\n");
345 error = swsusp_read();
347 hibernation_restore();
349 printk(KERN_ERR "PM: Restore failed, recovering.\n");
351 unprepare_processes();
353 free_basic_memory_bitmaps();
355 atomic_inc(&snapshot_device_available);
356 /* For success case, the suspend path will release the lock */
358 mutex_unlock(&pm_mutex);
359 pr_debug("PM: Resume from disk failed.\n");
363 late_initcall(software_resume);
366 static const char * const hibernation_modes[] = {
367 [HIBERNATION_PLATFORM] = "platform",
368 [HIBERNATION_SHUTDOWN] = "shutdown",
369 [HIBERNATION_REBOOT] = "reboot",
370 [HIBERNATION_TEST] = "test",
371 [HIBERNATION_TESTPROC] = "testproc",
375 * disk - Control hibernation mode
377 * Suspend-to-disk can be handled in several ways. We have a few options
378 * for putting the system to sleep - using the platform driver (e.g. ACPI
379 * or other hibernation_ops), powering off the system or rebooting the
380 * system (for testing) as well as the two test modes.
382 * The system can support 'platform', and that is known a priori (and
383 * encoded by the presence of hibernation_ops). However, the user may
384 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
385 * test modes, 'test' or 'testproc'.
387 * show() will display what the mode is currently set to.
388 * store() will accept one of
396 * It will only change to 'platform' if the system
397 * supports it (as determined by having hibernation_ops).
400 static ssize_t disk_show(struct kset *kset, char *buf)
405 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
406 if (!hibernation_modes[i])
409 case HIBERNATION_SHUTDOWN:
410 case HIBERNATION_REBOOT:
411 case HIBERNATION_TEST:
412 case HIBERNATION_TESTPROC:
414 case HIBERNATION_PLATFORM:
417 /* not a valid mode, continue with loop */
420 if (i == hibernation_mode)
421 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
423 buf += sprintf(buf, "%s ", hibernation_modes[i]);
425 buf += sprintf(buf, "\n");
430 static ssize_t disk_store(struct kset *kset, const char *buf, size_t n)
436 int mode = HIBERNATION_INVALID;
438 p = memchr(buf, '\n', n);
439 len = p ? p - buf : n;
441 mutex_lock(&pm_mutex);
442 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
443 if (len == strlen(hibernation_modes[i])
444 && !strncmp(buf, hibernation_modes[i], len)) {
449 if (mode != HIBERNATION_INVALID) {
451 case HIBERNATION_SHUTDOWN:
452 case HIBERNATION_REBOOT:
453 case HIBERNATION_TEST:
454 case HIBERNATION_TESTPROC:
455 hibernation_mode = mode;
457 case HIBERNATION_PLATFORM:
459 hibernation_mode = mode;
467 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
468 hibernation_modes[mode]);
469 mutex_unlock(&pm_mutex);
470 return error ? error : n;
475 static ssize_t resume_show(struct kset *kset, char *buf)
477 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
478 MINOR(swsusp_resume_device));
481 static ssize_t resume_store(struct kset *kset, const char *buf, size_t n)
483 unsigned int maj, min;
487 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
490 res = MKDEV(maj,min);
491 if (maj != MAJOR(res) || min != MINOR(res))
494 mutex_lock(&pm_mutex);
495 swsusp_resume_device = res;
496 mutex_unlock(&pm_mutex);
497 printk("Attempting manual resume\n");
507 static ssize_t image_size_show(struct kset *kset, char *buf)
509 return sprintf(buf, "%lu\n", image_size);
512 static ssize_t image_size_store(struct kset *kset, const char *buf, size_t n)
516 if (sscanf(buf, "%lu", &size) == 1) {
524 power_attr(image_size);
526 static struct attribute * g[] = {
529 &image_size_attr.attr,
534 static struct attribute_group attr_group = {
539 static int __init pm_disk_init(void)
541 return sysfs_create_group(&power_subsys.kobj, &attr_group);
544 core_initcall(pm_disk_init);
547 static int __init resume_setup(char *str)
552 strncpy( resume_file, str, 255 );
556 static int __init resume_offset_setup(char *str)
558 unsigned long long offset;
563 if (sscanf(str, "%llu", &offset) == 1)
564 swsusp_resume_block = offset;
569 static int __init noresume_setup(char *str)
575 __setup("noresume", noresume_setup);
576 __setup("resume_offset=", resume_offset_setup);
577 __setup("resume=", resume_setup);