Hibernation: New testing facility (rev. 2)
[linux-2.6/zen-sources.git] / kernel / power / disk.c
blob0866b163c6bb928716e75c876761040817dc4c24
1 /*
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>
18 #include <linux/fs.h>
19 #include <linux/mount.h>
20 #include <linux/pm.h>
21 #include <linux/console.h>
22 #include <linux/cpu.h>
23 #include <linux/freezer.h>
25 #include "power.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;
33 enum {
34 HIBERNATION_INVALID,
35 HIBERNATION_PLATFORM,
36 HIBERNATION_TEST,
37 HIBERNATION_TESTPROC,
38 HIBERNATION_SHUTDOWN,
39 HIBERNATION_REBOOT,
40 /* keep last */
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;
50 /**
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)) {
60 WARN_ON(1);
61 return;
63 mutex_lock(&pm_mutex);
64 hibernation_ops = ops;
65 if (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");
77 mdelay(5000);
80 static int hibernation_testmode(int mode)
82 if (hibernation_mode == mode) {
83 hibernation_debug_sleep();
84 return 1;
86 return 0;
89 static int hibernation_test(int level)
91 if (pm_test_level == level) {
92 hibernation_debug_sleep();
93 return 1;
95 return 0;
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
104 * hibernation
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)
179 int error;
181 error = arch_prepare_suspend();
182 if (error)
183 return error;
185 local_irq_disable();
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);
193 if (error) {
194 printk(KERN_ERR "Some devices failed to power down, "
195 KERN_ERR "aborting suspend\n");
196 goto Enable_irqs;
199 if (hibernation_test(TEST_CORE))
200 goto Power_up;
202 in_suspend = 1;
203 save_processor_state();
204 error = swsusp_arch_suspend();
205 if (error)
206 printk(KERN_ERR "Error %d while creating the image\n", error);
207 /* Restore control flow magically appears here */
208 restore_processor_state();
209 if (!in_suspend)
210 platform_leave(platform_mode);
211 Power_up:
212 /* NOTE: device_power_up() is just a resume() for devices
213 * that suspended with irqs off ... no overall powerup.
215 device_power_up();
216 Enable_irqs:
217 local_irq_enable();
218 return error;
222 * hibernation_snapshot - quiesce devices and create the hibernation
223 * snapshot image.
224 * @platform_mode - if set, use the platform driver, if available, to
225 * prepare the platform frimware for the power transition.
227 * Must be called with pm_mutex held
230 int hibernation_snapshot(int platform_mode)
232 int error;
234 /* Free memory before shutting down devices. */
235 error = swsusp_shrink_memory();
236 if (error)
237 return error;
239 error = platform_start(platform_mode);
240 if (error)
241 return error;
243 suspend_console();
244 error = device_suspend(PMSG_FREEZE);
245 if (error)
246 goto Resume_console;
248 if (hibernation_test(TEST_DEVICES))
249 goto Resume_devices;
251 error = platform_pre_snapshot(platform_mode);
252 if (error || hibernation_test(TEST_PLATFORM))
253 goto Finish;
255 error = disable_nonboot_cpus();
256 if (!error) {
257 if (hibernation_test(TEST_CPUS))
258 goto Enable_cpus;
260 if (hibernation_testmode(HIBERNATION_TEST))
261 goto Enable_cpus;
263 error = create_image(platform_mode);
264 /* Control returns here after successful restore */
266 Enable_cpus:
267 enable_nonboot_cpus();
268 Finish:
269 platform_finish(platform_mode);
270 Resume_devices:
271 device_resume();
272 Resume_console:
273 resume_console();
274 return error;
278 * hibernation_restore - quiesce devices and restore the hibernation
279 * snapshot image. If successful, control returns in hibernation_snaphot()
280 * @platform_mode - if set, use the platform driver, if available, to
281 * prepare the platform frimware for the transition.
283 * Must be called with pm_mutex held
286 int hibernation_restore(int platform_mode)
288 int error;
290 pm_prepare_console();
291 suspend_console();
292 error = device_suspend(PMSG_PRETHAW);
293 if (error)
294 goto Finish;
296 error = platform_pre_restore(platform_mode);
297 if (!error) {
298 error = disable_nonboot_cpus();
299 if (!error)
300 error = swsusp_resume();
301 enable_nonboot_cpus();
303 platform_restore_cleanup(platform_mode);
304 device_resume();
305 Finish:
306 resume_console();
307 pm_restore_console();
308 return error;
312 * hibernation_platform_enter - enter the hibernation state using the
313 * platform driver (if available)
316 int hibernation_platform_enter(void)
318 int error;
320 if (!hibernation_ops)
321 return -ENOSYS;
324 * We have cancelled the power transition by running
325 * hibernation_ops->finish() before saving the image, so we should let
326 * the firmware know that we're going to enter the sleep state after all
328 error = hibernation_ops->start();
329 if (error)
330 return error;
332 suspend_console();
333 error = device_suspend(PMSG_SUSPEND);
334 if (error)
335 goto Resume_console;
337 error = hibernation_ops->prepare();
338 if (error)
339 goto Resume_devices;
341 error = disable_nonboot_cpus();
342 if (error)
343 goto Finish;
345 local_irq_disable();
346 error = device_power_down(PMSG_SUSPEND);
347 if (!error) {
348 hibernation_ops->enter();
349 /* We should never get here */
350 while (1);
352 local_irq_enable();
355 * We don't need to reenable the nonboot CPUs or resume consoles, since
356 * the system is going to be halted anyway.
358 Finish:
359 hibernation_ops->finish();
360 Resume_devices:
361 device_resume();
362 Resume_console:
363 resume_console();
364 return error;
368 * power_down - Shut the machine down for hibernation.
370 * Use the platform driver, if configured so; otherwise try
371 * to power off or reboot.
374 static void power_down(void)
376 switch (hibernation_mode) {
377 case HIBERNATION_TEST:
378 case HIBERNATION_TESTPROC:
379 break;
380 case HIBERNATION_REBOOT:
381 kernel_restart(NULL);
382 break;
383 case HIBERNATION_PLATFORM:
384 hibernation_platform_enter();
385 case HIBERNATION_SHUTDOWN:
386 kernel_power_off();
387 break;
389 kernel_halt();
391 * Valid image is on the disk, if we continue we risk serious data
392 * corruption after resume.
394 printk(KERN_CRIT "Please power me down manually\n");
395 while(1);
398 static void unprepare_processes(void)
400 thaw_processes();
401 pm_restore_console();
404 static int prepare_processes(void)
406 int error = 0;
408 pm_prepare_console();
409 if (freeze_processes()) {
410 error = -EBUSY;
411 unprepare_processes();
413 return error;
417 * hibernate - The granpappy of the built-in hibernation management
420 int hibernate(void)
422 int error;
424 mutex_lock(&pm_mutex);
425 /* The snapshot device should not be opened while we're running */
426 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
427 error = -EBUSY;
428 goto Unlock;
431 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
432 if (error)
433 goto Exit;
435 /* Allocate memory management structures */
436 error = create_basic_memory_bitmaps();
437 if (error)
438 goto Exit;
440 printk("Syncing filesystems ... ");
441 sys_sync();
442 printk("done.\n");
444 error = prepare_processes();
445 if (error)
446 goto Finish;
448 if (hibernation_test(TEST_FREEZER))
449 goto Thaw;
451 if (hibernation_testmode(HIBERNATION_TESTPROC))
452 goto Thaw;
454 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
455 if (in_suspend && !error) {
456 unsigned int flags = 0;
458 if (hibernation_mode == HIBERNATION_PLATFORM)
459 flags |= SF_PLATFORM_MODE;
460 pr_debug("PM: writing image.\n");
461 error = swsusp_write(flags);
462 swsusp_free();
463 if (!error)
464 power_down();
465 } else {
466 pr_debug("PM: Image restored successfully.\n");
467 swsusp_free();
469 Thaw:
470 unprepare_processes();
471 Finish:
472 free_basic_memory_bitmaps();
473 Exit:
474 pm_notifier_call_chain(PM_POST_HIBERNATION);
475 atomic_inc(&snapshot_device_available);
476 Unlock:
477 mutex_unlock(&pm_mutex);
478 return error;
483 * software_resume - Resume from a saved image.
485 * Called as a late_initcall (so all devices are discovered and
486 * initialized), we call swsusp to see if we have a saved image or not.
487 * If so, we quiesce devices, the restore the saved image. We will
488 * return above (in hibernate() ) if everything goes well.
489 * Otherwise, we fail gracefully and return to the normally
490 * scheduled program.
494 static int software_resume(void)
496 int error;
497 unsigned int flags;
500 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
501 * is configured into the kernel. Since the regular hibernate
502 * trigger path is via sysfs which takes a buffer mutex before
503 * calling hibernate functions (which take pm_mutex) this can
504 * cause lockdep to complain about a possible ABBA deadlock
505 * which cannot happen since we're in the boot code here and
506 * sysfs can't be invoked yet. Therefore, we use a subclass
507 * here to avoid lockdep complaining.
509 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
510 if (!swsusp_resume_device) {
511 if (!strlen(resume_file)) {
512 mutex_unlock(&pm_mutex);
513 return -ENOENT;
515 swsusp_resume_device = name_to_dev_t(resume_file);
516 pr_debug("swsusp: Resume From Partition %s\n", resume_file);
517 } else {
518 pr_debug("swsusp: Resume From Partition %d:%d\n",
519 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
522 if (noresume) {
524 * FIXME: If noresume is specified, we need to find the partition
525 * and reset it back to normal swap space.
527 mutex_unlock(&pm_mutex);
528 return 0;
531 pr_debug("PM: Checking swsusp image.\n");
532 error = swsusp_check();
533 if (error)
534 goto Unlock;
536 /* The snapshot device should not be opened while we're running */
537 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
538 error = -EBUSY;
539 goto Unlock;
542 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
543 if (error)
544 goto Finish;
546 error = create_basic_memory_bitmaps();
547 if (error)
548 goto Finish;
550 pr_debug("PM: Preparing processes for restore.\n");
551 error = prepare_processes();
552 if (error) {
553 swsusp_close();
554 goto Done;
557 pr_debug("PM: Reading swsusp image.\n");
559 error = swsusp_read(&flags);
560 if (!error)
561 hibernation_restore(flags & SF_PLATFORM_MODE);
563 printk(KERN_ERR "PM: Restore failed, recovering.\n");
564 swsusp_free();
565 unprepare_processes();
566 Done:
567 free_basic_memory_bitmaps();
568 Finish:
569 pm_notifier_call_chain(PM_POST_RESTORE);
570 atomic_inc(&snapshot_device_available);
571 /* For success case, the suspend path will release the lock */
572 Unlock:
573 mutex_unlock(&pm_mutex);
574 pr_debug("PM: Resume from disk failed.\n");
575 return error;
578 late_initcall(software_resume);
581 static const char * const hibernation_modes[] = {
582 [HIBERNATION_PLATFORM] = "platform",
583 [HIBERNATION_SHUTDOWN] = "shutdown",
584 [HIBERNATION_REBOOT] = "reboot",
585 [HIBERNATION_TEST] = "test",
586 [HIBERNATION_TESTPROC] = "testproc",
590 * disk - Control hibernation mode
592 * Suspend-to-disk can be handled in several ways. We have a few options
593 * for putting the system to sleep - using the platform driver (e.g. ACPI
594 * or other hibernation_ops), powering off the system or rebooting the
595 * system (for testing) as well as the two test modes.
597 * The system can support 'platform', and that is known a priori (and
598 * encoded by the presence of hibernation_ops). However, the user may
599 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
600 * test modes, 'test' or 'testproc'.
602 * show() will display what the mode is currently set to.
603 * store() will accept one of
605 * 'platform'
606 * 'shutdown'
607 * 'reboot'
608 * 'test'
609 * 'testproc'
611 * It will only change to 'platform' if the system
612 * supports it (as determined by having hibernation_ops).
615 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
616 char *buf)
618 int i;
619 char *start = buf;
621 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
622 if (!hibernation_modes[i])
623 continue;
624 switch (i) {
625 case HIBERNATION_SHUTDOWN:
626 case HIBERNATION_REBOOT:
627 case HIBERNATION_TEST:
628 case HIBERNATION_TESTPROC:
629 break;
630 case HIBERNATION_PLATFORM:
631 if (hibernation_ops)
632 break;
633 /* not a valid mode, continue with loop */
634 continue;
636 if (i == hibernation_mode)
637 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
638 else
639 buf += sprintf(buf, "%s ", hibernation_modes[i]);
641 buf += sprintf(buf, "\n");
642 return buf-start;
646 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
647 const char *buf, size_t n)
649 int error = 0;
650 int i;
651 int len;
652 char *p;
653 int mode = HIBERNATION_INVALID;
655 p = memchr(buf, '\n', n);
656 len = p ? p - buf : n;
658 mutex_lock(&pm_mutex);
659 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
660 if (len == strlen(hibernation_modes[i])
661 && !strncmp(buf, hibernation_modes[i], len)) {
662 mode = i;
663 break;
666 if (mode != HIBERNATION_INVALID) {
667 switch (mode) {
668 case HIBERNATION_SHUTDOWN:
669 case HIBERNATION_REBOOT:
670 case HIBERNATION_TEST:
671 case HIBERNATION_TESTPROC:
672 hibernation_mode = mode;
673 break;
674 case HIBERNATION_PLATFORM:
675 if (hibernation_ops)
676 hibernation_mode = mode;
677 else
678 error = -EINVAL;
680 } else
681 error = -EINVAL;
683 if (!error)
684 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
685 hibernation_modes[mode]);
686 mutex_unlock(&pm_mutex);
687 return error ? error : n;
690 power_attr(disk);
692 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
693 char *buf)
695 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
696 MINOR(swsusp_resume_device));
699 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
700 const char *buf, size_t n)
702 unsigned int maj, min;
703 dev_t res;
704 int ret = -EINVAL;
706 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
707 goto out;
709 res = MKDEV(maj,min);
710 if (maj != MAJOR(res) || min != MINOR(res))
711 goto out;
713 mutex_lock(&pm_mutex);
714 swsusp_resume_device = res;
715 mutex_unlock(&pm_mutex);
716 printk("Attempting manual resume\n");
717 noresume = 0;
718 software_resume();
719 ret = n;
720 out:
721 return ret;
724 power_attr(resume);
726 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
727 char *buf)
729 return sprintf(buf, "%lu\n", image_size);
732 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
733 const char *buf, size_t n)
735 unsigned long size;
737 if (sscanf(buf, "%lu", &size) == 1) {
738 image_size = size;
739 return n;
742 return -EINVAL;
745 power_attr(image_size);
747 static struct attribute * g[] = {
748 &disk_attr.attr,
749 &resume_attr.attr,
750 &image_size_attr.attr,
751 NULL,
755 static struct attribute_group attr_group = {
756 .attrs = g,
760 static int __init pm_disk_init(void)
762 return sysfs_create_group(power_kobj, &attr_group);
765 core_initcall(pm_disk_init);
768 static int __init resume_setup(char *str)
770 if (noresume)
771 return 1;
773 strncpy( resume_file, str, 255 );
774 return 1;
777 static int __init resume_offset_setup(char *str)
779 unsigned long long offset;
781 if (noresume)
782 return 1;
784 if (sscanf(str, "%llu", &offset) == 1)
785 swsusp_resume_block = offset;
787 return 1;
790 static int __init noresume_setup(char *str)
792 noresume = 1;
793 return 1;
796 __setup("noresume", noresume_setup);
797 __setup("resume_offset=", resume_offset_setup);
798 __setup("resume=", resume_setup);