Skip to content

Commit 62c552c

Browse files
bsmojverrjwysocki
authored andcommitted
PM / Hibernate: Enable suspend to both for in-kernel hibernation.
It is often useful to suspend to memory after hibernation image has been written to disk. If the battery runs out or power is otherwise lost, the computer will resume from the hibernated image. If not, it will resume from memory and hibernation image will be discarded. Signed-off-by: Bojan Smojver <bojan@rexursive.com> Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
1 parent 6887a41 commit 62c552c

4 files changed

Lines changed: 72 additions & 0 deletions

File tree

‎Documentation/power/swsusp.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ echo shutdown > /sys/power/disk; echo disk > /sys/power/state
3333

3434
echo platform > /sys/power/disk; echo disk > /sys/power/state
3535

36+
. If you would like to write hibernation image to swap and then suspend
37+
to RAM (provided your platform supports it), you can try
38+
39+
echo suspend > /sys/power/disk; echo disk > /sys/power/state
40+
3641
. If you have SATA disks, you'll need recent kernels with SATA suspend
3742
support. For suspend and resume to work, make sure your disk drivers
3843
are built into kernel -- not modules. [There's way to make

‎kernel/power/hibernate.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Copyright (c) 2003 Open Source Development Lab
66
* Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
77
* Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
8+
* Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
89
*
910
* This file is released under the GPLv2.
1011
*/
@@ -46,6 +47,9 @@ enum {
4647
HIBERNATION_PLATFORM,
4748
HIBERNATION_SHUTDOWN,
4849
HIBERNATION_REBOOT,
50+
#ifdef CONFIG_SUSPEND
51+
HIBERNATION_SUSPEND,
52+
#endif
4953
/* keep last */
5054
__HIBERNATION_AFTER_LAST
5155
};
@@ -574,6 +578,10 @@ int hibernation_platform_enter(void)
574578
*/
575579
static void power_down(void)
576580
{
581+
#ifdef CONFIG_SUSPEND
582+
int error;
583+
#endif
584+
577585
switch (hibernation_mode) {
578586
case HIBERNATION_REBOOT:
579587
kernel_restart(NULL);
@@ -583,6 +591,25 @@ static void power_down(void)
583591
case HIBERNATION_SHUTDOWN:
584592
kernel_power_off();
585593
break;
594+
#ifdef CONFIG_SUSPEND
595+
case HIBERNATION_SUSPEND:
596+
error = suspend_devices_and_enter(PM_SUSPEND_MEM);
597+
if (error) {
598+
if (hibernation_ops)
599+
hibernation_mode = HIBERNATION_PLATFORM;
600+
else
601+
hibernation_mode = HIBERNATION_SHUTDOWN;
602+
power_down();
603+
}
604+
/*
605+
* Restore swap signature.
606+
*/
607+
error = swsusp_unmark();
608+
if (error)
609+
printk(KERN_ERR "PM: Swap will be unusable! "
610+
"Try swapon -a.\n");
611+
return;
612+
#endif
586613
}
587614
kernel_halt();
588615
/*
@@ -827,6 +854,9 @@ static const char * const hibernation_modes[] = {
827854
[HIBERNATION_PLATFORM] = "platform",
828855
[HIBERNATION_SHUTDOWN] = "shutdown",
829856
[HIBERNATION_REBOOT] = "reboot",
857+
#ifdef CONFIG_SUSPEND
858+
[HIBERNATION_SUSPEND] = "suspend",
859+
#endif
830860
};
831861

832862
/*
@@ -867,6 +897,9 @@ static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
867897
switch (i) {
868898
case HIBERNATION_SHUTDOWN:
869899
case HIBERNATION_REBOOT:
900+
#ifdef CONFIG_SUSPEND
901+
case HIBERNATION_SUSPEND:
902+
#endif
870903
break;
871904
case HIBERNATION_PLATFORM:
872905
if (hibernation_ops)
@@ -907,6 +940,9 @@ static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
907940
switch (mode) {
908941
case HIBERNATION_SHUTDOWN:
909942
case HIBERNATION_REBOOT:
943+
#ifdef CONFIG_SUSPEND
944+
case HIBERNATION_SUSPEND:
945+
#endif
910946
hibernation_mode = mode;
911947
break;
912948
case HIBERNATION_PLATFORM:

‎kernel/power/power.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ extern void swsusp_free(void);
156156
extern int swsusp_read(unsigned int *flags_p);
157157
extern int swsusp_write(unsigned int flags);
158158
extern void swsusp_close(fmode_t);
159+
#ifdef CONFIG_SUSPEND
160+
extern int swsusp_unmark(void);
161+
#endif
159162

160163
/* kernel/power/block_io.c */
161164
extern struct block_device *hib_resume_bdev;

‎kernel/power/swap.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,34 @@ void swsusp_close(fmode_t mode)
14721472
blkdev_put(hib_resume_bdev, mode);
14731473
}
14741474

1475+
/**
1476+
* swsusp_unmark - Unmark swsusp signature in the resume device
1477+
*/
1478+
1479+
#ifdef CONFIG_SUSPEND
1480+
int swsusp_unmark(void)
1481+
{
1482+
int error;
1483+
1484+
hib_bio_read_page(swsusp_resume_block, swsusp_header, NULL);
1485+
if (!memcmp(HIBERNATE_SIG,swsusp_header->sig, 10)) {
1486+
memcpy(swsusp_header->sig,swsusp_header->orig_sig, 10);
1487+
error = hib_bio_write_page(swsusp_resume_block,
1488+
swsusp_header, NULL);
1489+
} else {
1490+
printk(KERN_ERR "PM: Cannot find swsusp signature!\n");
1491+
error = -ENODEV;
1492+
}
1493+
1494+
/*
1495+
* We just returned from suspend, we don't need the image any more.
1496+
*/
1497+
free_all_swap_pages(root_swap);
1498+
1499+
return error;
1500+
}
1501+
#endif
1502+
14751503
static int swsusp_header_init(void)
14761504
{
14771505
swsusp_header = (struct swsusp_header*) __get_free_page(GFP_KERNEL);

0 commit comments

Comments
 (0)