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
58 && ops
->pre_restore
&& ops
->restore_cleanup
)) {
62 mutex_lock(&pm_mutex
);
63 hibernation_ops
= ops
;
65 hibernation_mode
= HIBERNATION_PLATFORM
;
66 else if (hibernation_mode
== HIBERNATION_PLATFORM
)
67 hibernation_mode
= HIBERNATION_SHUTDOWN
;
69 mutex_unlock(&pm_mutex
);
74 * platform_prepare - prepare the machine for hibernation using the
75 * platform driver if so configured and return an error code if it fails
78 static int platform_prepare(int platform_mode
)
80 return (platform_mode
&& hibernation_ops
) ?
81 hibernation_ops
->prepare() : 0;
85 * platform_finish - switch the machine to the normal mode of operation
86 * using the platform driver (must be called after platform_prepare())
89 static void platform_finish(int platform_mode
)
91 if (platform_mode
&& hibernation_ops
)
92 hibernation_ops
->finish();
96 * platform_pre_restore - prepare the platform for the restoration from a
97 * hibernation image. If the restore fails after this function has been
98 * called, platform_restore_cleanup() must be called.
101 static int platform_pre_restore(int platform_mode
)
103 return (platform_mode
&& hibernation_ops
) ?
104 hibernation_ops
->pre_restore() : 0;
108 * platform_restore_cleanup - switch the platform to the normal mode of
109 * operation after a failing restore. If platform_pre_restore() has been
110 * called before the failing restore, this function must be called too,
111 * regardless of the result of platform_pre_restore().
114 static void platform_restore_cleanup(int platform_mode
)
116 if (platform_mode
&& hibernation_ops
)
117 hibernation_ops
->restore_cleanup();
121 * hibernation_snapshot - quiesce devices and create the hibernation
123 * @platform_mode - if set, use the platform driver, if available, to
124 * prepare the platform frimware for the power transition.
126 * Must be called with pm_mutex held
129 int hibernation_snapshot(int platform_mode
)
133 /* Free memory before shutting down devices. */
134 error
= swsusp_shrink_memory();
139 error
= device_suspend(PMSG_FREEZE
);
143 error
= platform_prepare(platform_mode
);
147 error
= disable_nonboot_cpus();
149 if (hibernation_mode
!= HIBERNATION_TEST
) {
151 error
= swsusp_suspend();
152 /* Control returns here after successful restore */
154 printk("swsusp debug: Waiting for 5 seconds.\n");
158 enable_nonboot_cpus();
160 platform_finish(platform_mode
);
168 * hibernation_restore - quiesce devices and restore the hibernation
169 * snapshot image. If successful, control returns in hibernation_snaphot()
170 * @platform_mode - if set, use the platform driver, if available, to
171 * prepare the platform frimware for the transition.
173 * Must be called with pm_mutex held
176 int hibernation_restore(int platform_mode
)
180 pm_prepare_console();
182 error
= device_suspend(PMSG_PRETHAW
);
186 error
= platform_pre_restore(platform_mode
);
188 error
= disable_nonboot_cpus();
190 error
= swsusp_resume();
191 enable_nonboot_cpus();
193 platform_restore_cleanup(platform_mode
);
197 pm_restore_console();
202 * hibernation_platform_enter - enter the hibernation state using the
203 * platform driver (if available)
206 int hibernation_platform_enter(void)
210 if (hibernation_ops
) {
211 kernel_shutdown_prepare(SYSTEM_SUSPEND_DISK
);
213 * We have cancelled the power transition by running
214 * hibernation_ops->finish() before saving the image, so we
215 * should let the firmware know that we're going to enter the
216 * sleep state after all
218 error
= hibernation_ops
->prepare();
221 error
= hibernation_ops
->enter();
229 * power_down - Shut the machine down for hibernation.
231 * Use the platform driver, if configured so; otherwise try
232 * to power off or reboot.
235 static void power_down(void)
237 switch (hibernation_mode
) {
238 case HIBERNATION_TEST
:
239 case HIBERNATION_TESTPROC
:
241 case HIBERNATION_SHUTDOWN
:
244 case HIBERNATION_REBOOT
:
245 kernel_restart(NULL
);
247 case HIBERNATION_PLATFORM
:
248 hibernation_platform_enter();
252 * Valid image is on the disk, if we continue we risk serious data
253 * corruption after resume.
255 printk(KERN_CRIT
"Please power me down manually\n");
259 static void unprepare_processes(void)
262 pm_restore_console();
265 static int prepare_processes(void)
269 pm_prepare_console();
270 if (freeze_processes()) {
272 unprepare_processes();
278 * hibernate - The granpappy of the built-in hibernation management
285 mutex_lock(&pm_mutex
);
286 /* The snapshot device should not be opened while we're running */
287 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
292 error
= pm_notifier_call_chain(PM_HIBERNATION_PREPARE
);
296 /* Allocate memory management structures */
297 error
= create_basic_memory_bitmaps();
301 error
= prepare_processes();
305 if (hibernation_mode
== HIBERNATION_TESTPROC
) {
306 printk("swsusp debug: Waiting for 5 seconds.\n");
310 error
= hibernation_snapshot(hibernation_mode
== HIBERNATION_PLATFORM
);
311 if (in_suspend
&& !error
) {
312 unsigned int flags
= 0;
314 if (hibernation_mode
== HIBERNATION_PLATFORM
)
315 flags
|= SF_PLATFORM_MODE
;
316 pr_debug("PM: writing image.\n");
317 error
= swsusp_write(flags
);
322 pr_debug("PM: Image restored successfully.\n");
326 unprepare_processes();
328 free_basic_memory_bitmaps();
330 pm_notifier_call_chain(PM_POST_HIBERNATION
);
331 atomic_inc(&snapshot_device_available
);
333 mutex_unlock(&pm_mutex
);
339 * software_resume - Resume from a saved image.
341 * Called as a late_initcall (so all devices are discovered and
342 * initialized), we call swsusp to see if we have a saved image or not.
343 * If so, we quiesce devices, the restore the saved image. We will
344 * return above (in hibernate() ) if everything goes well.
345 * Otherwise, we fail gracefully and return to the normally
350 static int software_resume(void)
355 mutex_lock(&pm_mutex
);
356 if (!swsusp_resume_device
) {
357 if (!strlen(resume_file
)) {
358 mutex_unlock(&pm_mutex
);
361 swsusp_resume_device
= name_to_dev_t(resume_file
);
362 pr_debug("swsusp: Resume From Partition %s\n", resume_file
);
364 pr_debug("swsusp: Resume From Partition %d:%d\n",
365 MAJOR(swsusp_resume_device
), MINOR(swsusp_resume_device
));
370 * FIXME: If noresume is specified, we need to find the partition
371 * and reset it back to normal swap space.
373 mutex_unlock(&pm_mutex
);
377 pr_debug("PM: Checking swsusp image.\n");
378 error
= swsusp_check();
382 /* The snapshot device should not be opened while we're running */
383 if (!atomic_add_unless(&snapshot_device_available
, -1, 0)) {
388 error
= create_basic_memory_bitmaps();
392 pr_debug("PM: Preparing processes for restore.\n");
393 error
= prepare_processes();
399 pr_debug("PM: Reading swsusp image.\n");
401 error
= swsusp_read(&flags
);
403 hibernation_restore(flags
& SF_PLATFORM_MODE
);
405 printk(KERN_ERR
"PM: Restore failed, recovering.\n");
407 unprepare_processes();
409 free_basic_memory_bitmaps();
411 atomic_inc(&snapshot_device_available
);
412 /* For success case, the suspend path will release the lock */
414 mutex_unlock(&pm_mutex
);
415 pr_debug("PM: Resume from disk failed.\n");
419 late_initcall(software_resume
);
422 static const char * const hibernation_modes
[] = {
423 [HIBERNATION_PLATFORM
] = "platform",
424 [HIBERNATION_SHUTDOWN
] = "shutdown",
425 [HIBERNATION_REBOOT
] = "reboot",
426 [HIBERNATION_TEST
] = "test",
427 [HIBERNATION_TESTPROC
] = "testproc",
431 * disk - Control hibernation mode
433 * Suspend-to-disk can be handled in several ways. We have a few options
434 * for putting the system to sleep - using the platform driver (e.g. ACPI
435 * or other hibernation_ops), powering off the system or rebooting the
436 * system (for testing) as well as the two test modes.
438 * The system can support 'platform', and that is known a priori (and
439 * encoded by the presence of hibernation_ops). However, the user may
440 * choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
441 * test modes, 'test' or 'testproc'.
443 * show() will display what the mode is currently set to.
444 * store() will accept one of
452 * It will only change to 'platform' if the system
453 * supports it (as determined by having hibernation_ops).
456 static ssize_t
disk_show(struct kset
*kset
, char *buf
)
461 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
462 if (!hibernation_modes
[i
])
465 case HIBERNATION_SHUTDOWN
:
466 case HIBERNATION_REBOOT
:
467 case HIBERNATION_TEST
:
468 case HIBERNATION_TESTPROC
:
470 case HIBERNATION_PLATFORM
:
473 /* not a valid mode, continue with loop */
476 if (i
== hibernation_mode
)
477 buf
+= sprintf(buf
, "[%s] ", hibernation_modes
[i
]);
479 buf
+= sprintf(buf
, "%s ", hibernation_modes
[i
]);
481 buf
+= sprintf(buf
, "\n");
486 static ssize_t
disk_store(struct kset
*kset
, const char *buf
, size_t n
)
492 int mode
= HIBERNATION_INVALID
;
494 p
= memchr(buf
, '\n', n
);
495 len
= p
? p
- buf
: n
;
497 mutex_lock(&pm_mutex
);
498 for (i
= HIBERNATION_FIRST
; i
<= HIBERNATION_MAX
; i
++) {
499 if (len
== strlen(hibernation_modes
[i
])
500 && !strncmp(buf
, hibernation_modes
[i
], len
)) {
505 if (mode
!= HIBERNATION_INVALID
) {
507 case HIBERNATION_SHUTDOWN
:
508 case HIBERNATION_REBOOT
:
509 case HIBERNATION_TEST
:
510 case HIBERNATION_TESTPROC
:
511 hibernation_mode
= mode
;
513 case HIBERNATION_PLATFORM
:
515 hibernation_mode
= mode
;
523 pr_debug("PM: suspend-to-disk mode set to '%s'\n",
524 hibernation_modes
[mode
]);
525 mutex_unlock(&pm_mutex
);
526 return error
? error
: n
;
531 static ssize_t
resume_show(struct kset
*kset
, char *buf
)
533 return sprintf(buf
,"%d:%d\n", MAJOR(swsusp_resume_device
),
534 MINOR(swsusp_resume_device
));
537 static ssize_t
resume_store(struct kset
*kset
, const char *buf
, size_t n
)
539 unsigned int maj
, min
;
543 if (sscanf(buf
, "%u:%u", &maj
, &min
) != 2)
546 res
= MKDEV(maj
,min
);
547 if (maj
!= MAJOR(res
) || min
!= MINOR(res
))
550 mutex_lock(&pm_mutex
);
551 swsusp_resume_device
= res
;
552 mutex_unlock(&pm_mutex
);
553 printk("Attempting manual resume\n");
563 static ssize_t
image_size_show(struct kset
*kset
, char *buf
)
565 return sprintf(buf
, "%lu\n", image_size
);
568 static ssize_t
image_size_store(struct kset
*kset
, const char *buf
, size_t n
)
572 if (sscanf(buf
, "%lu", &size
) == 1) {
580 power_attr(image_size
);
582 static struct attribute
* g
[] = {
585 &image_size_attr
.attr
,
590 static struct attribute_group attr_group
= {
595 static int __init
pm_disk_init(void)
597 return sysfs_create_group(&power_subsys
.kobj
, &attr_group
);
600 core_initcall(pm_disk_init
);
603 static int __init
resume_setup(char *str
)
608 strncpy( resume_file
, str
, 255 );
612 static int __init
resume_offset_setup(char *str
)
614 unsigned long long offset
;
619 if (sscanf(str
, "%llu", &offset
) == 1)
620 swsusp_resume_block
= offset
;
625 static int __init
noresume_setup(char *str
)
631 __setup("noresume", noresume_setup
);
632 __setup("resume_offset=", resume_offset_setup
);
633 __setup("resume=", resume_setup
);