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 platform_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 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
)) {
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 printk("Syncing filesystems ... ");
320 error
= prepare_processes();
324 if (hibernation_mode
== HIBERNATION_TESTPROC
) {
325 printk("swsusp debug: Waiting for 5 seconds.\n");
329 error
= hibernation_snapshot(hibernation_mode
== HIBERNATION_PLATFORM
);
330 if (in_suspend
&& !error
) {
331 unsigned int flags
= 0;
333 if (hibernation_mode
== HIBERNATION_PLATFORM
)
334 flags
|= SF_PLATFORM_MODE
;
335 pr_debug("PM: writing image.\n");
336 error
= swsusp_write(flags
);
341 pr_debug("PM: Image restored successfully.\n");
345 unprepare_processes();
347 free_basic_memory_bitmaps();
349 pm_notifier_call_chain(PM_POST_HIBERNATION
);
350 atomic_inc(&snapshot_device_available
);
352 mutex_unlock(&pm_mutex
);
358 * software_resume - Resume from a saved image.
360 * Called as a late_initcall (so all devices are discovered and
361 * initialized), we call swsusp to see if we have a saved image or not.
362 * If so, we quiesce devices, the restore the saved image. We will
363 * return above (in hibernate() ) if everything goes well.
364 * Otherwise, we fail gracefully and return to the normally
369 static int software_resume(void)
374 mutex_lock(&pm_mutex
);
375 if (!swsusp_resume_device
) {
376 if (!strlen(resume_file
)) {
377 mutex_unlock(&pm_mutex
);
380 swsusp_resume_device
= name_to_dev_t(resume_file
);
381 pr_debug("swsusp: Resume From Partition %s\n", resume_file
);
383 pr_debug("swsusp: Resume From Partition %d:%d\n",
384 MAJOR(swsusp_resume_device
), MINOR(swsusp_resume_device
));
389 * FIXME: If noresume is specified, we need to find the partition
390 * and reset it back to normal swap space.
392 mutex_unlock(&pm_mutex
);
396 pr_debug("PM: Checking swsusp image.\n");
397 error
= swsusp_check();
401 /* The snapshot device should not be opened while we're running */
402 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
407 error
= create_basic_memory_bitmaps();
411 pr_debug("PM: Preparing processes for restore.\n");
412 error
= prepare_processes();
418 pr_debug("PM: Reading swsusp image.\n");
420 error
= swsusp_read(&flags
);
422 hibernation_restore(flags
& SF_PLATFORM_MODE
);
424 printk(KERN_ERR
"PM: Restore failed, recovering.\n");
426 unprepare_processes();
428 free_basic_memory_bitmaps();
430 atomic_inc(&snapshot_device_available
);
431 /* For success case, the suspend path will release the lock */
433 mutex_unlock(&pm_mutex
);
434 pr_debug("PM: Resume from disk failed.\n");
438 late_initcall(software_resume
);
441 static const char * const hibernation_modes
[] = {
442 [HIBERNATION_PLATFORM
] = "platform",
443 [HIBERNATION_SHUTDOWN
] = "shutdown",
444 [HIBERNATION_REBOOT
] = "reboot",
445 [HIBERNATION_TEST
] = "test",
446 [HIBERNATION_TESTPROC
] = "testproc",
450 * disk - Control hibernation mode
452 * Suspend-to-disk can be handled in several ways. We have a few options
453 * for putting the system to sleep - using the platform driver (e.g. ACPI
454 * or other hibernation_ops), powering off the system or rebooting the
455 * system (for testing) as well as the two test modes.
457 * The system can support 'platform', and that is known a priori (and
458 * encoded by the presence of hibernation_ops). However, the user may
459 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
460 * test modes, 'test' or 'testproc'.
462 * show() will display what the mode is currently set to.
463 * store() will accept one of
471 * It will only change to 'platform' if the system
472 * supports it (as determined by having hibernation_ops).
475 static ssize_t
disk_show(struct kset
*kset
, char *buf
)
480 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
481 if (!hibernation_modes
[i
])
484 case HIBERNATION_SHUTDOWN
:
485 case HIBERNATION_REBOOT
:
486 case HIBERNATION_TEST
:
487 case HIBERNATION_TESTPROC
:
489 case HIBERNATION_PLATFORM
:
492 /* not a valid mode, continue with loop */
495 if (i
== hibernation_mode
)
496 buf
+= sprintf(buf
, "[%s] ", hibernation_modes
[i
]);
498 buf
+= sprintf(buf
, "%s ", hibernation_modes
[i
]);
500 buf
+= sprintf(buf
, "\n");
505 static ssize_t
disk_store(struct kset
*kset
, const char *buf
, size_t n
)
511 int mode
= HIBERNATION_INVALID
;
513 p
= memchr(buf
, '\n', n
);
514 len
= p
? p
- buf
: n
;
516 mutex_lock(&pm_mutex
);
517 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
518 if (len
== strlen(hibernation_modes
[i
])
519 && !strncmp(buf
, hibernation_modes
[i
], len
)) {
524 if (mode
!= HIBERNATION_INVALID
) {
526 case HIBERNATION_SHUTDOWN
:
527 case HIBERNATION_REBOOT
:
528 case HIBERNATION_TEST
:
529 case HIBERNATION_TESTPROC
:
530 hibernation_mode
= mode
;
532 case HIBERNATION_PLATFORM
:
534 hibernation_mode
= mode
;
542 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
543 hibernation_modes
[mode
]);
544 mutex_unlock(&pm_mutex
);
545 return error
? error
: n
;
550 static ssize_t
resume_show(struct kset
*kset
, char *buf
)
552 return sprintf(buf
,"%d:%d\n", MAJOR(swsusp_resume_device
),
553 MINOR(swsusp_resume_device
));
556 static ssize_t
resume_store(struct kset
*kset
, const char *buf
, size_t n
)
558 unsigned int maj
, min
;
562 if (sscanf(buf
, "%u:%u", &maj
, &min
) != 2)
565 res
= MKDEV(maj
,min
);
566 if (maj
!= MAJOR(res
) || min
!= MINOR(res
))
569 mutex_lock(&pm_mutex
);
570 swsusp_resume_device
= res
;
571 mutex_unlock(&pm_mutex
);
572 printk("Attempting manual resume\n");
582 static ssize_t
image_size_show(struct kset
*kset
, char *buf
)
584 return sprintf(buf
, "%lu\n", image_size
);
587 static ssize_t
image_size_store(struct kset
*kset
, const char *buf
, size_t n
)
591 if (sscanf(buf
, "%lu", &size
) == 1) {
599 power_attr(image_size
);
601 static struct attribute
* g
[] = {
604 &image_size_attr
.attr
,
609 static struct attribute_group attr_group
= {
614 static int __init
pm_disk_init(void)
616 return sysfs_create_group(&power_subsys
.kobj
, &attr_group
);
619 core_initcall(pm_disk_init
);
622 static int __init
resume_setup(char *str
)
627 strncpy( resume_file
, str
, 255 );
631 static int __init
resume_offset_setup(char *str
)
633 unsigned long long offset
;
638 if (sscanf(str
, "%llu", &offset
) == 1)
639 swsusp_resume_block
= offset
;
644 static int __init
noresume_setup(char *str
)
650 __setup("noresume", noresume_setup
);
651 __setup("resume_offset=", resume_offset_setup
);
652 __setup("resume=", resume_setup
);