Hibernation: Introduce begin() and end() callbacks
[linux-2.6/sactl.git] / kernel / power / disk.c
blob53c22d9cf577ecdb90eb4c3b97149bd4bdff0277
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->begin && ops->end && ops->pre_snapshot
58 && ops->prepare && ops->finish && 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_begin - tell the platform driver that we're starting
104 * hibernation
107 static int platform_begin(int platform_mode)
109 return (platform_mode && hibernation_ops) ?
110 hibernation_ops->begin() : 0;
114 * platform_end - tell the platform driver that we've entered the
115 * working state
118 static void platform_end(int platform_mode)
120 if (platform_mode && hibernation_ops)
121 hibernation_ops->end();
125 * platform_pre_snapshot - prepare the machine for hibernation using the
126 * platform driver if so configured and return an error code if it fails
129 static int platform_pre_snapshot(int platform_mode)
131 return (platform_mode && hibernation_ops) ?
132 hibernation_ops->pre_snapshot() : 0;
136 * platform_leave - prepare the machine for switching to the normal mode
137 * of operation using the platform driver (called with interrupts disabled)
140 static void platform_leave(int platform_mode)
142 if (platform_mode && hibernation_ops)
143 hibernation_ops->leave();
147 * platform_finish - switch the machine to the normal mode of operation
148 * using the platform driver (must be called after platform_prepare())
151 static void platform_finish(int platform_mode)
153 if (platform_mode && hibernation_ops)
154 hibernation_ops->finish();
158 * platform_pre_restore - prepare the platform for the restoration from a
159 * hibernation image. If the restore fails after this function has been
160 * called, platform_restore_cleanup() must be called.
163 static int platform_pre_restore(int platform_mode)
165 return (platform_mode && hibernation_ops) ?
166 hibernation_ops->pre_restore() : 0;
170 * platform_restore_cleanup - switch the platform to the normal mode of
171 * operation after a failing restore. If platform_pre_restore() has been
172 * called before the failing restore, this function must be called too,
173 * regardless of the result of platform_pre_restore().
176 static void platform_restore_cleanup(int platform_mode)
178 if (platform_mode && hibernation_ops)
179 hibernation_ops->restore_cleanup();
183 * create_image - freeze devices that need to be frozen with interrupts
184 * off, create the hibernation image and thaw those devices. Control
185 * reappears in this routine after a restore.
188 int create_image(int platform_mode)
190 int error;
192 error = arch_prepare_suspend();
193 if (error)
194 return error;
196 local_irq_disable();
197 /* At this point, device_suspend() has been called, but *not*
198 * device_power_down(). We *must* call device_power_down() now.
199 * Otherwise, drivers for some devices (e.g. interrupt controllers)
200 * become desynchronized with the actual state of the hardware
201 * at resume time, and evil weirdness ensues.
203 error = device_power_down(PMSG_FREEZE);
204 if (error) {
205 printk(KERN_ERR "PM: Some devices failed to power down, "
206 "aborting hibernation\n");
207 goto Enable_irqs;
210 if (hibernation_test(TEST_CORE))
211 goto Power_up;
213 in_suspend = 1;
214 save_processor_state();
215 error = swsusp_arch_suspend();
216 if (error)
217 printk(KERN_ERR "PM: Error %d creating hibernation image\n",
218 error);
219 /* Restore control flow magically appears here */
220 restore_processor_state();
221 if (!in_suspend)
222 platform_leave(platform_mode);
223 Power_up:
224 /* NOTE: device_power_up() is just a resume() for devices
225 * that suspended with irqs off ... no overall powerup.
227 device_power_up();
228 Enable_irqs:
229 local_irq_enable();
230 return error;
234 * hibernation_snapshot - quiesce devices and create the hibernation
235 * snapshot image.
236 * @platform_mode - if set, use the platform driver, if available, to
237 * prepare the platform frimware for the power transition.
239 * Must be called with pm_mutex held
242 int hibernation_snapshot(int platform_mode)
244 int error;
246 /* Free memory before shutting down devices. */
247 error = swsusp_shrink_memory();
248 if (error)
249 return error;
251 error = platform_begin(platform_mode);
252 if (error)
253 goto Close;
255 suspend_console();
256 error = device_suspend(PMSG_FREEZE);
257 if (error)
258 goto Resume_console;
260 if (hibernation_test(TEST_DEVICES))
261 goto Resume_devices;
263 error = platform_pre_snapshot(platform_mode);
264 if (error || hibernation_test(TEST_PLATFORM))
265 goto Finish;
267 error = disable_nonboot_cpus();
268 if (!error) {
269 if (hibernation_test(TEST_CPUS))
270 goto Enable_cpus;
272 if (hibernation_testmode(HIBERNATION_TEST))
273 goto Enable_cpus;
275 error = create_image(platform_mode);
276 /* Control returns here after successful restore */
278 Enable_cpus:
279 enable_nonboot_cpus();
280 Finish:
281 platform_finish(platform_mode);
282 Resume_devices:
283 device_resume();
284 Resume_console:
285 resume_console();
286 Close:
287 platform_end(platform_mode);
288 return error;
292 * resume_target_kernel - prepare devices that need to be suspended with
293 * interrupts off, restore the contents of highmem that have not been
294 * restored yet from the image and run the low level code that will restore
295 * the remaining contents of memory and switch to the just restored target
296 * kernel.
299 static int resume_target_kernel(void)
301 int error;
303 local_irq_disable();
304 error = device_power_down(PMSG_PRETHAW);
305 if (error) {
306 printk(KERN_ERR "PM: Some devices failed to power down, "
307 "aborting resume\n");
308 goto Enable_irqs;
310 /* We'll ignore saved state, but this gets preempt count (etc) right */
311 save_processor_state();
312 error = restore_highmem();
313 if (!error) {
314 error = swsusp_arch_resume();
316 * The code below is only ever reached in case of a failure.
317 * Otherwise execution continues at place where
318 * swsusp_arch_suspend() was called
320 BUG_ON(!error);
321 /* This call to restore_highmem() undos the previous one */
322 restore_highmem();
325 * The only reason why swsusp_arch_resume() can fail is memory being
326 * very tight, so we have to free it as soon as we can to avoid
327 * subsequent failures
329 swsusp_free();
330 restore_processor_state();
331 touch_softlockup_watchdog();
332 device_power_up();
333 Enable_irqs:
334 local_irq_enable();
335 return error;
339 * hibernation_restore - quiesce devices and restore the hibernation
340 * snapshot image. If successful, control returns in hibernation_snaphot()
341 * @platform_mode - if set, use the platform driver, if available, to
342 * prepare the platform frimware for the transition.
344 * Must be called with pm_mutex held
347 int hibernation_restore(int platform_mode)
349 int error;
351 pm_prepare_console();
352 suspend_console();
353 error = device_suspend(PMSG_PRETHAW);
354 if (error)
355 goto Finish;
357 error = platform_pre_restore(platform_mode);
358 if (!error) {
359 error = disable_nonboot_cpus();
360 if (!error)
361 error = resume_target_kernel();
362 enable_nonboot_cpus();
364 platform_restore_cleanup(platform_mode);
365 device_resume();
366 Finish:
367 resume_console();
368 pm_restore_console();
369 return error;
373 * hibernation_platform_enter - enter the hibernation state using the
374 * platform driver (if available)
377 int hibernation_platform_enter(void)
379 int error;
381 if (!hibernation_ops)
382 return -ENOSYS;
385 * We have cancelled the power transition by running
386 * hibernation_ops->finish() before saving the image, so we should let
387 * the firmware know that we're going to enter the sleep state after all
389 error = hibernation_ops->begin();
390 if (error)
391 goto Close;
393 suspend_console();
394 error = device_suspend(PMSG_SUSPEND);
395 if (error)
396 goto Resume_console;
398 error = hibernation_ops->prepare();
399 if (error)
400 goto Resume_devices;
402 error = disable_nonboot_cpus();
403 if (error)
404 goto Finish;
406 local_irq_disable();
407 error = device_power_down(PMSG_SUSPEND);
408 if (!error) {
409 hibernation_ops->enter();
410 /* We should never get here */
411 while (1);
413 local_irq_enable();
416 * We don't need to reenable the nonboot CPUs or resume consoles, since
417 * the system is going to be halted anyway.
419 Finish:
420 hibernation_ops->finish();
421 Resume_devices:
422 device_resume();
423 Resume_console:
424 resume_console();
425 Close:
426 hibernation_ops->end();
427 return error;
431 * power_down - Shut the machine down for hibernation.
433 * Use the platform driver, if configured so; otherwise try
434 * to power off or reboot.
437 static void power_down(void)
439 switch (hibernation_mode) {
440 case HIBERNATION_TEST:
441 case HIBERNATION_TESTPROC:
442 break;
443 case HIBERNATION_REBOOT:
444 kernel_restart(NULL);
445 break;
446 case HIBERNATION_PLATFORM:
447 hibernation_platform_enter();
448 case HIBERNATION_SHUTDOWN:
449 kernel_power_off();
450 break;
452 kernel_halt();
454 * Valid image is on the disk, if we continue we risk serious data
455 * corruption after resume.
457 printk(KERN_CRIT "PM: Please power down manually\n");
458 while(1);
461 static void unprepare_processes(void)
463 thaw_processes();
464 pm_restore_console();
467 static int prepare_processes(void)
469 int error = 0;
471 pm_prepare_console();
472 if (freeze_processes()) {
473 error = -EBUSY;
474 unprepare_processes();
476 return error;
480 * hibernate - The granpappy of the built-in hibernation management
483 int hibernate(void)
485 int error;
487 mutex_lock(&pm_mutex);
488 /* The snapshot device should not be opened while we're running */
489 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
490 error = -EBUSY;
491 goto Unlock;
494 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
495 if (error)
496 goto Exit;
498 /* Allocate memory management structures */
499 error = create_basic_memory_bitmaps();
500 if (error)
501 goto Exit;
503 printk(KERN_INFO "PM: Syncing filesystems ... ");
504 sys_sync();
505 printk("done.\n");
507 error = prepare_processes();
508 if (error)
509 goto Finish;
511 if (hibernation_test(TEST_FREEZER))
512 goto Thaw;
514 if (hibernation_testmode(HIBERNATION_TESTPROC))
515 goto Thaw;
517 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
518 if (in_suspend && !error) {
519 unsigned int flags = 0;
521 if (hibernation_mode == HIBERNATION_PLATFORM)
522 flags |= SF_PLATFORM_MODE;
523 pr_debug("PM: writing image.\n");
524 error = swsusp_write(flags);
525 swsusp_free();
526 if (!error)
527 power_down();
528 } else {
529 pr_debug("PM: Image restored successfully.\n");
530 swsusp_free();
532 Thaw:
533 unprepare_processes();
534 Finish:
535 free_basic_memory_bitmaps();
536 Exit:
537 pm_notifier_call_chain(PM_POST_HIBERNATION);
538 atomic_inc(&snapshot_device_available);
539 Unlock:
540 mutex_unlock(&pm_mutex);
541 return error;
546 * software_resume - Resume from a saved image.
548 * Called as a late_initcall (so all devices are discovered and
549 * initialized), we call swsusp to see if we have a saved image or not.
550 * If so, we quiesce devices, the restore the saved image. We will
551 * return above (in hibernate() ) if everything goes well.
552 * Otherwise, we fail gracefully and return to the normally
553 * scheduled program.
557 static int software_resume(void)
559 int error;
560 unsigned int flags;
563 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
564 * is configured into the kernel. Since the regular hibernate
565 * trigger path is via sysfs which takes a buffer mutex before
566 * calling hibernate functions (which take pm_mutex) this can
567 * cause lockdep to complain about a possible ABBA deadlock
568 * which cannot happen since we're in the boot code here and
569 * sysfs can't be invoked yet. Therefore, we use a subclass
570 * here to avoid lockdep complaining.
572 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
573 if (!swsusp_resume_device) {
574 if (!strlen(resume_file)) {
575 mutex_unlock(&pm_mutex);
576 return -ENOENT;
578 swsusp_resume_device = name_to_dev_t(resume_file);
579 pr_debug("PM: Resume from partition %s\n", resume_file);
580 } else {
581 pr_debug("PM: Resume from partition %d:%d\n",
582 MAJOR(swsusp_resume_device),
583 MINOR(swsusp_resume_device));
586 if (noresume) {
588 * FIXME: If noresume is specified, we need to find the
589 * partition and reset it back to normal swap space.
591 mutex_unlock(&pm_mutex);
592 return 0;
595 pr_debug("PM: Checking hibernation image.\n");
596 error = swsusp_check();
597 if (error)
598 goto Unlock;
600 /* The snapshot device should not be opened while we're running */
601 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
602 error = -EBUSY;
603 goto Unlock;
606 error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
607 if (error)
608 goto Finish;
610 error = create_basic_memory_bitmaps();
611 if (error)
612 goto Finish;
614 pr_debug("PM: Preparing processes for restore.\n");
615 error = prepare_processes();
616 if (error) {
617 swsusp_close();
618 goto Done;
621 pr_debug("PM: Reading hibernation image.\n");
623 error = swsusp_read(&flags);
624 if (!error)
625 hibernation_restore(flags & SF_PLATFORM_MODE);
627 printk(KERN_ERR "PM: Restore failed, recovering.\n");
628 swsusp_free();
629 unprepare_processes();
630 Done:
631 free_basic_memory_bitmaps();
632 Finish:
633 pm_notifier_call_chain(PM_POST_RESTORE);
634 atomic_inc(&snapshot_device_available);
635 /* For success case, the suspend path will release the lock */
636 Unlock:
637 mutex_unlock(&pm_mutex);
638 pr_debug("PM: Resume from disk failed.\n");
639 return error;
642 late_initcall(software_resume);
645 static const char * const hibernation_modes[] = {
646 [HIBERNATION_PLATFORM] = "platform",
647 [HIBERNATION_SHUTDOWN] = "shutdown",
648 [HIBERNATION_REBOOT] = "reboot",
649 [HIBERNATION_TEST] = "test",
650 [HIBERNATION_TESTPROC] = "testproc",
654 * disk - Control hibernation mode
656 * Suspend-to-disk can be handled in several ways. We have a few options
657 * for putting the system to sleep - using the platform driver (e.g. ACPI
658 * or other hibernation_ops), powering off the system or rebooting the
659 * system (for testing) as well as the two test modes.
661 * The system can support 'platform', and that is known a priori (and
662 * encoded by the presence of hibernation_ops). However, the user may
663 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
664 * test modes, 'test' or 'testproc'.
666 * show() will display what the mode is currently set to.
667 * store() will accept one of
669 * 'platform'
670 * 'shutdown'
671 * 'reboot'
672 * 'test'
673 * 'testproc'
675 * It will only change to 'platform' if the system
676 * supports it (as determined by having hibernation_ops).
679 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
680 char *buf)
682 int i;
683 char *start = buf;
685 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
686 if (!hibernation_modes[i])
687 continue;
688 switch (i) {
689 case HIBERNATION_SHUTDOWN:
690 case HIBERNATION_REBOOT:
691 case HIBERNATION_TEST:
692 case HIBERNATION_TESTPROC:
693 break;
694 case HIBERNATION_PLATFORM:
695 if (hibernation_ops)
696 break;
697 /* not a valid mode, continue with loop */
698 continue;
700 if (i == hibernation_mode)
701 buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
702 else
703 buf += sprintf(buf, "%s ", hibernation_modes[i]);
705 buf += sprintf(buf, "\n");
706 return buf-start;
710 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
711 const char *buf, size_t n)
713 int error = 0;
714 int i;
715 int len;
716 char *p;
717 int mode = HIBERNATION_INVALID;
719 p = memchr(buf, '\n', n);
720 len = p ? p - buf : n;
722 mutex_lock(&pm_mutex);
723 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
724 if (len == strlen(hibernation_modes[i])
725 && !strncmp(buf, hibernation_modes[i], len)) {
726 mode = i;
727 break;
730 if (mode != HIBERNATION_INVALID) {
731 switch (mode) {
732 case HIBERNATION_SHUTDOWN:
733 case HIBERNATION_REBOOT:
734 case HIBERNATION_TEST:
735 case HIBERNATION_TESTPROC:
736 hibernation_mode = mode;
737 break;
738 case HIBERNATION_PLATFORM:
739 if (hibernation_ops)
740 hibernation_mode = mode;
741 else
742 error = -EINVAL;
744 } else
745 error = -EINVAL;
747 if (!error)
748 pr_debug("PM: Hibernation mode set to '%s'\n",
749 hibernation_modes[mode]);
750 mutex_unlock(&pm_mutex);
751 return error ? error : n;
754 power_attr(disk);
756 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
757 char *buf)
759 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
760 MINOR(swsusp_resume_device));
763 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
764 const char *buf, size_t n)
766 unsigned int maj, min;
767 dev_t res;
768 int ret = -EINVAL;
770 if (sscanf(buf, "%u:%u", &maj, &min) != 2)
771 goto out;
773 res = MKDEV(maj,min);
774 if (maj != MAJOR(res) || min != MINOR(res))
775 goto out;
777 mutex_lock(&pm_mutex);
778 swsusp_resume_device = res;
779 mutex_unlock(&pm_mutex);
780 printk(KERN_INFO "PM: Starting manual resume from disk\n");
781 noresume = 0;
782 software_resume();
783 ret = n;
784 out:
785 return ret;
788 power_attr(resume);
790 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
791 char *buf)
793 return sprintf(buf, "%lu\n", image_size);
796 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
797 const char *buf, size_t n)
799 unsigned long size;
801 if (sscanf(buf, "%lu", &size) == 1) {
802 image_size = size;
803 return n;
806 return -EINVAL;
809 power_attr(image_size);
811 static struct attribute * g[] = {
812 &disk_attr.attr,
813 &resume_attr.attr,
814 &image_size_attr.attr,
815 NULL,
819 static struct attribute_group attr_group = {
820 .attrs = g,
824 static int __init pm_disk_init(void)
826 return sysfs_create_group(power_kobj, &attr_group);
829 core_initcall(pm_disk_init);
832 static int __init resume_setup(char *str)
834 if (noresume)
835 return 1;
837 strncpy( resume_file, str, 255 );
838 return 1;
841 static int __init resume_offset_setup(char *str)
843 unsigned long long offset;
845 if (noresume)
846 return 1;
848 if (sscanf(str, "%llu", &offset) == 1)
849 swsusp_resume_block = offset;
851 return 1;
854 static int __init noresume_setup(char *str)
856 noresume = 1;
857 return 1;
860 __setup("noresume", noresume_setup);
861 __setup("resume_offset=", resume_offset_setup);
862 __setup("resume=", resume_setup);