| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * kernel/power/disk.c - Suspend-to-disk support. | 
 | 3 |  * | 
 | 4 |  * Copyright (c) 2003 Patrick Mochel | 
 | 5 |  * Copyright (c) 2003 Open Source Development Lab | 
 | 6 |  * Copyright (c) 2004 Pavel Machek <pavel@suse.cz> | 
 | 7 |  * | 
 | 8 |  * This file is released under the GPLv2. | 
 | 9 |  * | 
 | 10 |  */ | 
 | 11 |  | 
 | 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> | 
| Rafael J. Wysocki | 1bfcf13 | 2008-10-15 22:01:21 -0700 | [diff] [blame] | 17 | #include <linux/kmod.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/delay.h> | 
 | 19 | #include <linux/fs.h> | 
| Andrew Morton | d53d9f1 | 2005-07-12 13:58:07 -0700 | [diff] [blame] | 20 | #include <linux/mount.h> | 
| Eric W. Biederman | 88d10bb | 2005-09-22 21:43:46 -0700 | [diff] [blame] | 21 | #include <linux/pm.h> | 
| Rafael J. Wysocki | 97c7801 | 2006-10-11 01:20:45 -0700 | [diff] [blame] | 22 | #include <linux/console.h> | 
| Rafael J. Wysocki | e3920fb | 2006-09-25 23:32:48 -0700 | [diff] [blame] | 23 | #include <linux/cpu.h> | 
| Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 24 | #include <linux/freezer.h> | 
| Magnus Damm | a8af789 | 2009-03-31 15:23:37 -0700 | [diff] [blame] | 25 | #include <asm/suspend.h> | 
| Andrew Morton | d53d9f1 | 2005-07-12 13:58:07 -0700 | [diff] [blame] | 26 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include "power.h" | 
 | 28 |  | 
 | 29 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | static int noresume = 0; | 
| Adrian Bunk | 47a460d | 2008-02-04 22:30:06 -0800 | [diff] [blame] | 31 | static char resume_file[256] = CONFIG_PM_STD_PARTITION; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | dev_t swsusp_resume_device; | 
| Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 33 | sector_t swsusp_resume_block; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 |  | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 35 | enum { | 
 | 36 | 	HIBERNATION_INVALID, | 
 | 37 | 	HIBERNATION_PLATFORM, | 
 | 38 | 	HIBERNATION_TEST, | 
 | 39 | 	HIBERNATION_TESTPROC, | 
 | 40 | 	HIBERNATION_SHUTDOWN, | 
 | 41 | 	HIBERNATION_REBOOT, | 
 | 42 | 	/* keep last */ | 
 | 43 | 	__HIBERNATION_AFTER_LAST | 
 | 44 | }; | 
 | 45 | #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1) | 
 | 46 | #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1) | 
 | 47 |  | 
 | 48 | static int hibernation_mode = HIBERNATION_SHUTDOWN; | 
 | 49 |  | 
| Rafael J. Wysocki | b3dac3b | 2007-10-18 03:04:43 -0700 | [diff] [blame] | 50 | static struct platform_hibernation_ops *hibernation_ops; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 51 |  | 
 | 52 | /** | 
 | 53 |  * hibernation_set_ops - set the global hibernate operations | 
 | 54 |  * @ops: the hibernation operations to use in subsequent hibernation transitions | 
 | 55 |  */ | 
 | 56 |  | 
| Rafael J. Wysocki | b3dac3b | 2007-10-18 03:04:43 -0700 | [diff] [blame] | 57 | void hibernation_set_ops(struct platform_hibernation_ops *ops) | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 58 | { | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 59 | 	if (ops && !(ops->begin && ops->end &&  ops->pre_snapshot | 
 | 60 | 	    && ops->prepare && ops->finish && ops->enter && ops->pre_restore | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 61 | 	    && ops->restore_cleanup)) { | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 62 | 		WARN_ON(1); | 
 | 63 | 		return; | 
 | 64 | 	} | 
 | 65 | 	mutex_lock(&pm_mutex); | 
 | 66 | 	hibernation_ops = ops; | 
 | 67 | 	if (ops) | 
 | 68 | 		hibernation_mode = HIBERNATION_PLATFORM; | 
 | 69 | 	else if (hibernation_mode == HIBERNATION_PLATFORM) | 
 | 70 | 		hibernation_mode = HIBERNATION_SHUTDOWN; | 
 | 71 |  | 
 | 72 | 	mutex_unlock(&pm_mutex); | 
 | 73 | } | 
 | 74 |  | 
| Rafael J. Wysocki | abfe2d7 | 2009-01-19 20:54:54 +0100 | [diff] [blame] | 75 | static bool entering_platform_hibernation; | 
 | 76 |  | 
 | 77 | bool system_entering_hibernation(void) | 
 | 78 | { | 
 | 79 | 	return entering_platform_hibernation; | 
 | 80 | } | 
 | 81 | EXPORT_SYMBOL(system_entering_hibernation); | 
 | 82 |  | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 83 | #ifdef CONFIG_PM_DEBUG | 
 | 84 | static void hibernation_debug_sleep(void) | 
 | 85 | { | 
 | 86 | 	printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n"); | 
 | 87 | 	mdelay(5000); | 
 | 88 | } | 
 | 89 |  | 
 | 90 | static int hibernation_testmode(int mode) | 
 | 91 | { | 
 | 92 | 	if (hibernation_mode == mode) { | 
 | 93 | 		hibernation_debug_sleep(); | 
 | 94 | 		return 1; | 
 | 95 | 	} | 
 | 96 | 	return 0; | 
 | 97 | } | 
 | 98 |  | 
 | 99 | static int hibernation_test(int level) | 
 | 100 | { | 
 | 101 | 	if (pm_test_level == level) { | 
 | 102 | 		hibernation_debug_sleep(); | 
 | 103 | 		return 1; | 
 | 104 | 	} | 
 | 105 | 	return 0; | 
 | 106 | } | 
 | 107 | #else /* !CONFIG_PM_DEBUG */ | 
 | 108 | static int hibernation_testmode(int mode) { return 0; } | 
 | 109 | static int hibernation_test(int level) { return 0; } | 
 | 110 | #endif /* !CONFIG_PM_DEBUG */ | 
 | 111 |  | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 112 | /** | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 113 |  *	platform_begin - tell the platform driver that we're starting | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 114 |  *	hibernation | 
 | 115 |  */ | 
 | 116 |  | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 117 | static int platform_begin(int platform_mode) | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 118 | { | 
 | 119 | 	return (platform_mode && hibernation_ops) ? | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 120 | 		hibernation_ops->begin() : 0; | 
 | 121 | } | 
 | 122 |  | 
 | 123 | /** | 
 | 124 |  *	platform_end - tell the platform driver that we've entered the | 
 | 125 |  *	working state | 
 | 126 |  */ | 
 | 127 |  | 
 | 128 | static void platform_end(int platform_mode) | 
 | 129 | { | 
 | 130 | 	if (platform_mode && hibernation_ops) | 
 | 131 | 		hibernation_ops->end(); | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 132 | } | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 133 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | /** | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 135 |  *	platform_pre_snapshot - prepare the machine for hibernation using the | 
| Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 136 |  *	platform driver if so configured and return an error code if it fails | 
 | 137 |  */ | 
 | 138 |  | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 139 | static int platform_pre_snapshot(int platform_mode) | 
| Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 140 | { | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 141 | 	return (platform_mode && hibernation_ops) ? | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 142 | 		hibernation_ops->pre_snapshot() : 0; | 
| Stefan Seyfried | 8a05aac | 2006-12-06 20:34:21 -0800 | [diff] [blame] | 143 | } | 
 | 144 |  | 
 | 145 | /** | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 146 |  *	platform_leave - prepare the machine for switching to the normal mode | 
 | 147 |  *	of operation using the platform driver (called with interrupts disabled) | 
 | 148 |  */ | 
 | 149 |  | 
 | 150 | static void platform_leave(int platform_mode) | 
 | 151 | { | 
 | 152 | 	if (platform_mode && hibernation_ops) | 
 | 153 | 		hibernation_ops->leave(); | 
 | 154 | } | 
 | 155 |  | 
 | 156 | /** | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 157 |  *	platform_finish - switch the machine to the normal mode of operation | 
 | 158 |  *	using the platform driver (must be called after platform_prepare()) | 
 | 159 |  */ | 
 | 160 |  | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 161 | static void platform_finish(int platform_mode) | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 162 | { | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 163 | 	if (platform_mode && hibernation_ops) | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 164 | 		hibernation_ops->finish(); | 
 | 165 | } | 
 | 166 |  | 
 | 167 | /** | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 168 |  *	platform_pre_restore - prepare the platform for the restoration from a | 
 | 169 |  *	hibernation image.  If the restore fails after this function has been | 
 | 170 |  *	called, platform_restore_cleanup() must be called. | 
 | 171 |  */ | 
 | 172 |  | 
 | 173 | static int platform_pre_restore(int platform_mode) | 
 | 174 | { | 
 | 175 | 	return (platform_mode && hibernation_ops) ? | 
 | 176 | 		hibernation_ops->pre_restore() : 0; | 
 | 177 | } | 
 | 178 |  | 
 | 179 | /** | 
 | 180 |  *	platform_restore_cleanup - switch the platform to the normal mode of | 
 | 181 |  *	operation after a failing restore.  If platform_pre_restore() has been | 
 | 182 |  *	called before the failing restore, this function must be called too, | 
 | 183 |  *	regardless of the result of platform_pre_restore(). | 
 | 184 |  */ | 
 | 185 |  | 
 | 186 | static void platform_restore_cleanup(int platform_mode) | 
 | 187 | { | 
 | 188 | 	if (platform_mode && hibernation_ops) | 
 | 189 | 		hibernation_ops->restore_cleanup(); | 
 | 190 | } | 
 | 191 |  | 
 | 192 | /** | 
| Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 193 |  *	platform_recover - recover the platform from a failure to suspend | 
 | 194 |  *	devices. | 
 | 195 |  */ | 
 | 196 |  | 
 | 197 | static void platform_recover(int platform_mode) | 
 | 198 | { | 
 | 199 | 	if (platform_mode && hibernation_ops && hibernation_ops->recover) | 
 | 200 | 		hibernation_ops->recover(); | 
 | 201 | } | 
 | 202 |  | 
 | 203 | /** | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 204 |  *	create_image - freeze devices that need to be frozen with interrupts | 
 | 205 |  *	off, create the hibernation image and thaw those devices.  Control | 
 | 206 |  *	reappears in this routine after a restore. | 
 | 207 |  */ | 
 | 208 |  | 
| Adrian Bunk | 47a460d | 2008-02-04 22:30:06 -0800 | [diff] [blame] | 209 | static int create_image(int platform_mode) | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 210 | { | 
 | 211 | 	int error; | 
 | 212 |  | 
 | 213 | 	error = arch_prepare_suspend(); | 
 | 214 | 	if (error) | 
 | 215 | 		return error; | 
 | 216 |  | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 217 | 	device_pm_lock(); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 218 |  | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 219 | 	/* At this point, device_suspend() has been called, but *not* | 
 | 220 | 	 * device_power_down(). We *must* call device_power_down() now. | 
 | 221 | 	 * Otherwise, drivers for some devices (e.g. interrupt controllers) | 
 | 222 | 	 * become desynchronized with the actual state of the hardware | 
 | 223 | 	 * at resume time, and evil weirdness ensues. | 
 | 224 | 	 */ | 
 | 225 | 	error = device_power_down(PMSG_FREEZE); | 
 | 226 | 	if (error) { | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 227 | 		printk(KERN_ERR "PM: Some devices failed to power down, " | 
 | 228 | 			"aborting hibernation\n"); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 229 | 		goto Unlock; | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 230 | 	} | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 231 |  | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 232 | 	error = platform_pre_snapshot(platform_mode); | 
 | 233 | 	if (error || hibernation_test(TEST_PLATFORM)) | 
 | 234 | 		goto Platform_finish; | 
 | 235 |  | 
 | 236 | 	error = disable_nonboot_cpus(); | 
 | 237 | 	if (error || hibernation_test(TEST_CPUS) | 
 | 238 | 	    || hibernation_testmode(HIBERNATION_TEST)) | 
 | 239 | 		goto Enable_cpus; | 
 | 240 |  | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 241 | 	local_irq_disable(); | 
 | 242 |  | 
| Rafael J. Wysocki | 770824b | 2009-02-22 18:38:50 +0100 | [diff] [blame] | 243 | 	sysdev_suspend(PMSG_FREEZE); | 
 | 244 | 	if (error) { | 
 | 245 | 		printk(KERN_ERR "PM: Some devices failed to power down, " | 
 | 246 | 			"aborting hibernation\n"); | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 247 | 		goto Enable_irqs; | 
| Rafael J. Wysocki | 770824b | 2009-02-22 18:38:50 +0100 | [diff] [blame] | 248 | 	} | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 249 |  | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 250 | 	if (hibernation_test(TEST_CORE)) | 
 | 251 | 		goto Power_up; | 
 | 252 |  | 
 | 253 | 	in_suspend = 1; | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 254 | 	save_processor_state(); | 
 | 255 | 	error = swsusp_arch_suspend(); | 
 | 256 | 	if (error) | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 257 | 		printk(KERN_ERR "PM: Error %d creating hibernation image\n", | 
 | 258 | 			error); | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 259 | 	/* Restore control flow magically appears here */ | 
 | 260 | 	restore_processor_state(); | 
 | 261 | 	if (!in_suspend) | 
 | 262 | 		platform_leave(platform_mode); | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 263 |  | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 264 |  Power_up: | 
| Rafael J. Wysocki | 770824b | 2009-02-22 18:38:50 +0100 | [diff] [blame] | 265 | 	sysdev_resume(); | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 266 | 	/* NOTE:  device_power_up() is just a resume() for devices | 
 | 267 | 	 * that suspended with irqs off ... no overall powerup. | 
 | 268 | 	 */ | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 269 |  | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 270 |  Enable_irqs: | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 271 | 	local_irq_enable(); | 
 | 272 |  | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 273 |  Enable_cpus: | 
 | 274 | 	enable_nonboot_cpus(); | 
 | 275 |  | 
 | 276 |  Platform_finish: | 
 | 277 | 	platform_finish(platform_mode); | 
 | 278 |  | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 279 | 	device_power_up(in_suspend ? | 
 | 280 | 		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 281 |  | 
 | 282 |  Unlock: | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 283 | 	device_pm_unlock(); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 284 |  | 
| Rafael J. Wysocki | c7e0831 | 2007-10-18 03:04:55 -0700 | [diff] [blame] | 285 | 	return error; | 
 | 286 | } | 
 | 287 |  | 
 | 288 | /** | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 289 |  *	hibernation_snapshot - quiesce devices and create the hibernation | 
 | 290 |  *	snapshot image. | 
 | 291 |  *	@platform_mode - if set, use the platform driver, if available, to | 
| Nick Andrew | 877d031 | 2009-01-26 11:06:57 +0100 | [diff] [blame] | 292 |  *			 prepare the platform firmware for the power transition. | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 293 |  * | 
 | 294 |  *	Must be called with pm_mutex held | 
 | 295 |  */ | 
 | 296 |  | 
 | 297 | int hibernation_snapshot(int platform_mode) | 
 | 298 | { | 
| Ingo Molnar | cbe2f5a | 2008-11-23 10:37:12 +0100 | [diff] [blame] | 299 | 	int error; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 300 |  | 
| Zhang Rui | 3fe0313 | 2008-10-26 20:50:26 +0100 | [diff] [blame] | 301 | 	error = platform_begin(platform_mode); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 302 | 	if (error) | 
| Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 303 | 		return error; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 304 |  | 
| Zhang Rui | 3fe0313 | 2008-10-26 20:50:26 +0100 | [diff] [blame] | 305 | 	/* Free memory before shutting down devices. */ | 
 | 306 | 	error = swsusp_shrink_memory(); | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 307 | 	if (error) | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 308 | 		goto Close; | 
| Rafael J. Wysocki | 74f270a | 2007-10-18 03:04:42 -0700 | [diff] [blame] | 309 |  | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 310 | 	suspend_console(); | 
 | 311 | 	error = device_suspend(PMSG_FREEZE); | 
 | 312 | 	if (error) | 
| Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 313 | 		goto Recover_platform; | 
| Rafael J. Wysocki | 10a1803 | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 314 |  | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 315 | 	if (hibernation_test(TEST_DEVICES)) | 
| Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 316 | 		goto Recover_platform; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 317 |  | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 318 | 	error = create_image(platform_mode); | 
 | 319 | 	/* Control returns here after successful restore */ | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 320 |  | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 321 |  Resume_devices: | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 322 | 	device_resume(in_suspend ? | 
 | 323 | 		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 324 | 	resume_console(); | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 325 |  Close: | 
 | 326 | 	platform_end(platform_mode); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 327 | 	return error; | 
| Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 328 |  | 
 | 329 |  Recover_platform: | 
 | 330 | 	platform_recover(platform_mode); | 
 | 331 | 	goto Resume_devices; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 332 | } | 
 | 333 |  | 
 | 334 | /** | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 335 |  *	resume_target_kernel - prepare devices that need to be suspended with | 
 | 336 |  *	interrupts off, restore the contents of highmem that have not been | 
 | 337 |  *	restored yet from the image and run the low level code that will restore | 
 | 338 |  *	the remaining contents of memory and switch to the just restored target | 
 | 339 |  *	kernel. | 
 | 340 |  */ | 
 | 341 |  | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 342 | static int resume_target_kernel(bool platform_mode) | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 343 | { | 
 | 344 | 	int error; | 
 | 345 |  | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 346 | 	device_pm_lock(); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 347 |  | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 348 | 	error = device_power_down(PMSG_QUIESCE); | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 349 | 	if (error) { | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 350 | 		printk(KERN_ERR "PM: Some devices failed to power down, " | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 351 | 			"aborting resume\n"); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 352 | 		goto Unlock; | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 353 | 	} | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 354 |  | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 355 | 	error = platform_pre_restore(platform_mode); | 
 | 356 | 	if (error) | 
 | 357 | 		goto Cleanup; | 
 | 358 |  | 
 | 359 | 	error = disable_nonboot_cpus(); | 
 | 360 | 	if (error) | 
 | 361 | 		goto Enable_cpus; | 
 | 362 |  | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 363 | 	local_irq_disable(); | 
 | 364 |  | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 365 | 	error = sysdev_suspend(PMSG_QUIESCE); | 
 | 366 | 	if (error) | 
 | 367 | 		goto Enable_irqs; | 
 | 368 |  | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 369 | 	/* We'll ignore saved state, but this gets preempt count (etc) right */ | 
 | 370 | 	save_processor_state(); | 
 | 371 | 	error = restore_highmem(); | 
 | 372 | 	if (!error) { | 
 | 373 | 		error = swsusp_arch_resume(); | 
 | 374 | 		/* | 
 | 375 | 		 * The code below is only ever reached in case of a failure. | 
 | 376 | 		 * Otherwise execution continues at place where | 
 | 377 | 		 * swsusp_arch_suspend() was called | 
 | 378 | 		 */ | 
 | 379 | 		BUG_ON(!error); | 
 | 380 | 		/* This call to restore_highmem() undos the previous one */ | 
 | 381 | 		restore_highmem(); | 
 | 382 | 	} | 
 | 383 | 	/* | 
 | 384 | 	 * The only reason why swsusp_arch_resume() can fail is memory being | 
 | 385 | 	 * very tight, so we have to free it as soon as we can to avoid | 
 | 386 | 	 * subsequent failures | 
 | 387 | 	 */ | 
 | 388 | 	swsusp_free(); | 
 | 389 | 	restore_processor_state(); | 
 | 390 | 	touch_softlockup_watchdog(); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 391 |  | 
| Rafael J. Wysocki | 770824b | 2009-02-22 18:38:50 +0100 | [diff] [blame] | 392 | 	sysdev_resume(); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 393 |  | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 394 |  Enable_irqs: | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 395 | 	local_irq_enable(); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 396 |  | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 397 |  Enable_cpus: | 
 | 398 | 	enable_nonboot_cpus(); | 
 | 399 |  | 
 | 400 |  Cleanup: | 
 | 401 | 	platform_restore_cleanup(platform_mode); | 
 | 402 |  | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 403 | 	device_power_up(PMSG_RECOVER); | 
 | 404 |  | 
 | 405 |  Unlock: | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 406 | 	device_pm_unlock(); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 407 |  | 
| Rafael J. Wysocki | 72df68c | 2007-12-08 02:04:21 +0100 | [diff] [blame] | 408 | 	return error; | 
 | 409 | } | 
 | 410 |  | 
 | 411 | /** | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 412 |  *	hibernation_restore - quiesce devices and restore the hibernation | 
 | 413 |  *	snapshot image.  If successful, control returns in hibernation_snaphot() | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 414 |  *	@platform_mode - if set, use the platform driver, if available, to | 
| Nick Andrew | 877d031 | 2009-01-26 11:06:57 +0100 | [diff] [blame] | 415 |  *			 prepare the platform firmware for the transition. | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 416 |  * | 
 | 417 |  *	Must be called with pm_mutex held | 
 | 418 |  */ | 
 | 419 |  | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 420 | int hibernation_restore(int platform_mode) | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 421 | { | 
| Ingo Molnar | cbe2f5a | 2008-11-23 10:37:12 +0100 | [diff] [blame] | 422 | 	int error; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 423 |  | 
 | 424 | 	pm_prepare_console(); | 
 | 425 | 	suspend_console(); | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 426 | 	error = device_suspend(PMSG_QUIESCE); | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 427 | 	if (!error) { | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 428 | 		error = resume_target_kernel(platform_mode); | 
 | 429 | 		device_resume(PMSG_RECOVER); | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 430 | 	} | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 431 | 	resume_console(); | 
 | 432 | 	pm_restore_console(); | 
 | 433 | 	return error; | 
 | 434 | } | 
 | 435 |  | 
 | 436 | /** | 
 | 437 |  *	hibernation_platform_enter - enter the hibernation state using the | 
 | 438 |  *	platform driver (if available) | 
 | 439 |  */ | 
 | 440 |  | 
 | 441 | int hibernation_platform_enter(void) | 
 | 442 | { | 
| Ingo Molnar | cbe2f5a | 2008-11-23 10:37:12 +0100 | [diff] [blame] | 443 | 	int error; | 
| Rafael J. Wysocki | b1457bc | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 444 |  | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 445 | 	if (!hibernation_ops) | 
 | 446 | 		return -ENOSYS; | 
 | 447 |  | 
 | 448 | 	/* | 
 | 449 | 	 * We have cancelled the power transition by running | 
 | 450 | 	 * hibernation_ops->finish() before saving the image, so we should let | 
 | 451 | 	 * the firmware know that we're going to enter the sleep state after all | 
 | 452 | 	 */ | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 453 | 	error = hibernation_ops->begin(); | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 454 | 	if (error) | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 455 | 		goto Close; | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 456 |  | 
| Rafael J. Wysocki | abfe2d7 | 2009-01-19 20:54:54 +0100 | [diff] [blame] | 457 | 	entering_platform_hibernation = true; | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 458 | 	suspend_console(); | 
| Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 459 | 	error = device_suspend(PMSG_HIBERNATE); | 
| Rafael J. Wysocki | d8f3de0 | 2008-06-12 23:24:06 +0200 | [diff] [blame] | 460 | 	if (error) { | 
 | 461 | 		if (hibernation_ops->recover) | 
 | 462 | 			hibernation_ops->recover(); | 
 | 463 | 		goto Resume_devices; | 
 | 464 | 	} | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 465 |  | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 466 | 	device_pm_lock(); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 467 |  | 
| Rafael J. Wysocki | 3a2d5b7 | 2008-02-23 19:13:25 +0100 | [diff] [blame] | 468 | 	error = device_power_down(PMSG_HIBERNATE); | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 469 | 	if (error) | 
 | 470 | 		goto Unlock; | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 471 |  | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 472 | 	error = hibernation_ops->prepare(); | 
 | 473 | 	if (error) | 
 | 474 | 		goto Platofrm_finish; | 
 | 475 |  | 
 | 476 | 	error = disable_nonboot_cpus(); | 
 | 477 | 	if (error) | 
 | 478 | 		goto Platofrm_finish; | 
 | 479 |  | 
 | 480 | 	local_irq_disable(); | 
 | 481 | 	sysdev_suspend(PMSG_HIBERNATE); | 
 | 482 | 	hibernation_ops->enter(); | 
 | 483 | 	/* We should never get here */ | 
 | 484 | 	while (1); | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 485 |  | 
 | 486 | 	/* | 
 | 487 | 	 * We don't need to reenable the nonboot CPUs or resume consoles, since | 
 | 488 | 	 * the system is going to be halted anyway. | 
 | 489 | 	 */ | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 490 |  Platofrm_finish: | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 491 | 	hibernation_ops->finish(); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 492 |  | 
| Rafael J. Wysocki | 4aecd67 | 2009-03-16 22:34:26 +0100 | [diff] [blame] | 493 | 	device_power_up(PMSG_RESTORE); | 
 | 494 |  | 
 | 495 |  Unlock: | 
 | 496 | 	device_pm_unlock(); | 
 | 497 |  | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 498 |  Resume_devices: | 
| Rafael J. Wysocki | abfe2d7 | 2009-01-19 20:54:54 +0100 | [diff] [blame] | 499 | 	entering_platform_hibernation = false; | 
| Rafael J. Wysocki | 1eede07 | 2008-05-20 23:00:01 +0200 | [diff] [blame] | 500 | 	device_resume(PMSG_RESTORE); | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 501 | 	resume_console(); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 502 |  | 
| Rafael J. Wysocki | caea99e | 2008-01-08 00:08:44 +0100 | [diff] [blame] | 503 |  Close: | 
 | 504 | 	hibernation_ops->end(); | 
| Rafael J. Wysocki | 2ed8d2b | 2009-03-16 22:34:06 +0100 | [diff] [blame] | 505 |  | 
| Rafael J. Wysocki | b1457bc | 2007-07-19 01:47:31 -0700 | [diff] [blame] | 506 | 	return error; | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 507 | } | 
 | 508 |  | 
 | 509 | /** | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 510 |  *	power_down - Shut the machine down for hibernation. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 |  * | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 512 |  *	Use the platform driver, if configured so; otherwise try | 
 | 513 |  *	to power off or reboot. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 |  */ | 
 | 515 |  | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 516 | static void power_down(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 518 | 	switch (hibernation_mode) { | 
 | 519 | 	case HIBERNATION_TEST: | 
 | 520 | 	case HIBERNATION_TESTPROC: | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 521 | 		break; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 522 | 	case HIBERNATION_REBOOT: | 
| Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 523 | 		kernel_restart(NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | 		break; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 525 | 	case HIBERNATION_PLATFORM: | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 526 | 		hibernation_platform_enter(); | 
| Rafael J. Wysocki | 9cd9a00 | 2007-10-18 03:04:56 -0700 | [diff] [blame] | 527 | 	case HIBERNATION_SHUTDOWN: | 
 | 528 | 		kernel_power_off(); | 
 | 529 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | 	} | 
| Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 531 | 	kernel_halt(); | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 532 | 	/* | 
 | 533 | 	 * Valid image is on the disk, if we continue we risk serious data | 
 | 534 | 	 * corruption after resume. | 
 | 535 | 	 */ | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 536 | 	printk(KERN_CRIT "PM: Please power down manually\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | 	while(1); | 
 | 538 | } | 
 | 539 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | static int prepare_processes(void) | 
 | 541 | { | 
| Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 542 | 	int error = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | 	if (freeze_processes()) { | 
 | 545 | 		error = -EBUSY; | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 546 | 		thaw_processes(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | 	} | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 548 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } | 
 | 550 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | /** | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 552 |  *	hibernate - The granpappy of the built-in hibernation management | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 |  */ | 
 | 554 |  | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 555 | int hibernate(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { | 
 | 557 | 	int error; | 
 | 558 |  | 
| Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 559 | 	mutex_lock(&pm_mutex); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 560 | 	/* The snapshot device should not be opened while we're running */ | 
| Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 561 | 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { | 
 | 562 | 		error = -EBUSY; | 
 | 563 | 		goto Unlock; | 
 | 564 | 	} | 
 | 565 |  | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 566 | 	pm_prepare_console(); | 
| Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 567 | 	error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE); | 
 | 568 | 	if (error) | 
 | 569 | 		goto Exit; | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 570 |  | 
| Rafael J. Wysocki | 1bfcf13 | 2008-10-15 22:01:21 -0700 | [diff] [blame] | 571 | 	error = usermodehelper_disable(); | 
 | 572 | 	if (error) | 
 | 573 | 		goto Exit; | 
 | 574 |  | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 575 | 	/* Allocate memory management structures */ | 
 | 576 | 	error = create_basic_memory_bitmaps(); | 
 | 577 | 	if (error) | 
 | 578 | 		goto Exit; | 
 | 579 |  | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 580 | 	printk(KERN_INFO "PM: Syncing filesystems ... "); | 
| Rafael J. Wysocki | 232b143 | 2007-10-18 03:04:44 -0700 | [diff] [blame] | 581 | 	sys_sync(); | 
 | 582 | 	printk("done.\n"); | 
 | 583 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | 	error = prepare_processes(); | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 585 | 	if (error) | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 586 | 		goto Finish; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 |  | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 588 | 	if (hibernation_test(TEST_FREEZER)) | 
| Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 589 | 		goto Thaw; | 
| Rafael J. Wysocki | 4cc7977 | 2007-11-19 23:42:31 +0100 | [diff] [blame] | 590 |  | 
 | 591 | 	if (hibernation_testmode(HIBERNATION_TESTPROC)) | 
 | 592 | 		goto Thaw; | 
 | 593 |  | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 594 | 	error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); | 
 | 595 | 	if (in_suspend && !error) { | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 596 | 		unsigned int flags = 0; | 
 | 597 |  | 
 | 598 | 		if (hibernation_mode == HIBERNATION_PLATFORM) | 
 | 599 | 			flags |= SF_PLATFORM_MODE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | 		pr_debug("PM: writing image.\n"); | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 601 | 		error = swsusp_write(flags); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 602 | 		swsusp_free(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | 		if (!error) | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 604 | 			power_down(); | 
| Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 605 | 	} else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | 		pr_debug("PM: Image restored successfully.\n"); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 607 | 		swsusp_free(); | 
| Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 608 | 	} | 
| Rafael J. Wysocki | b918f6e | 2006-11-02 22:07:19 -0800 | [diff] [blame] | 609 |  Thaw: | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 610 | 	thaw_processes(); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 611 |  Finish: | 
 | 612 | 	free_basic_memory_bitmaps(); | 
| Rafael J. Wysocki | 1bfcf13 | 2008-10-15 22:01:21 -0700 | [diff] [blame] | 613 | 	usermodehelper_enable(); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 614 |  Exit: | 
| Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 615 | 	pm_notifier_call_chain(PM_POST_HIBERNATION); | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 616 | 	pm_restore_console(); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 617 | 	atomic_inc(&snapshot_device_available); | 
| Rafael J. Wysocki | b10d911 | 2007-07-19 01:47:36 -0700 | [diff] [blame] | 618 |  Unlock: | 
 | 619 | 	mutex_unlock(&pm_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | 	return error; | 
 | 621 | } | 
 | 622 |  | 
 | 623 |  | 
 | 624 | /** | 
 | 625 |  *	software_resume - Resume from a saved image. | 
 | 626 |  * | 
 | 627 |  *	Called as a late_initcall (so all devices are discovered and | 
 | 628 |  *	initialized), we call swsusp to see if we have a saved image or not. | 
 | 629 |  *	If so, we quiesce devices, the restore the saved image. We will | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 630 |  *	return above (in hibernate() ) if everything goes well. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 |  *	Otherwise, we fail gracefully and return to the normally | 
 | 632 |  *	scheduled program. | 
 | 633 |  * | 
 | 634 |  */ | 
 | 635 |  | 
 | 636 | static int software_resume(void) | 
 | 637 | { | 
 | 638 | 	int error; | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 639 | 	unsigned int flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 |  | 
| Johannes Berg | 60a0d23 | 2007-11-14 17:00:16 -0800 | [diff] [blame] | 641 | 	/* | 
| Arjan van de Ven | eed3ee0 | 2009-02-14 02:00:19 +0100 | [diff] [blame] | 642 | 	 * If the user said "noresume".. bail out early. | 
 | 643 | 	 */ | 
 | 644 | 	if (noresume) | 
 | 645 | 		return 0; | 
 | 646 |  | 
 | 647 | 	/* | 
| Johannes Berg | 60a0d23 | 2007-11-14 17:00:16 -0800 | [diff] [blame] | 648 | 	 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs | 
 | 649 | 	 * is configured into the kernel. Since the regular hibernate | 
 | 650 | 	 * trigger path is via sysfs which takes a buffer mutex before | 
 | 651 | 	 * calling hibernate functions (which take pm_mutex) this can | 
 | 652 | 	 * cause lockdep to complain about a possible ABBA deadlock | 
 | 653 | 	 * which cannot happen since we're in the boot code here and | 
 | 654 | 	 * sysfs can't be invoked yet. Therefore, we use a subclass | 
 | 655 | 	 * here to avoid lockdep complaining. | 
 | 656 | 	 */ | 
 | 657 | 	mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING); | 
| Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 658 | 	if (!swsusp_resume_device) { | 
| Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 659 | 		if (!strlen(resume_file)) { | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 660 | 			mutex_unlock(&pm_mutex); | 
| Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 661 | 			return -ENOENT; | 
| Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 662 | 		} | 
| Arjan van de Ven | eed3ee0 | 2009-02-14 02:00:19 +0100 | [diff] [blame] | 663 | 		/* | 
 | 664 | 		 * Some device discovery might still be in progress; we need | 
 | 665 | 		 * to wait for this to finish. | 
 | 666 | 		 */ | 
 | 667 | 		wait_for_device_probe(); | 
| Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 668 | 		swsusp_resume_device = name_to_dev_t(resume_file); | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 669 | 		pr_debug("PM: Resume from partition %s\n", resume_file); | 
| Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 670 | 	} else { | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 671 | 		pr_debug("PM: Resume from partition %d:%d\n", | 
 | 672 | 				MAJOR(swsusp_resume_device), | 
 | 673 | 				MINOR(swsusp_resume_device)); | 
| Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 674 | 	} | 
 | 675 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | 	if (noresume) { | 
 | 677 | 		/** | 
| Rafael J. Wysocki | 9575809 | 2007-12-08 02:06:57 +0100 | [diff] [blame] | 678 | 		 * FIXME: If noresume is specified, we need to find the | 
 | 679 | 		 * partition and reset it back to normal swap space. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | 		 */ | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 681 | 		mutex_unlock(&pm_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | 		return 0; | 
 | 683 | 	} | 
 | 684 |  | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 685 | 	pr_debug("PM: Checking hibernation image.\n"); | 
| Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 686 | 	error = swsusp_check(); | 
 | 687 | 	if (error) | 
| Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 688 | 		goto Unlock; | 
 | 689 |  | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 690 | 	/* The snapshot device should not be opened while we're running */ | 
 | 691 | 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { | 
 | 692 | 		error = -EBUSY; | 
 | 693 | 		goto Unlock; | 
 | 694 | 	} | 
 | 695 |  | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 696 | 	pm_prepare_console(); | 
| Alan Stern | c3e94d8 | 2007-11-19 23:38:25 +0100 | [diff] [blame] | 697 | 	error = pm_notifier_call_chain(PM_RESTORE_PREPARE); | 
 | 698 | 	if (error) | 
 | 699 | 		goto Finish; | 
 | 700 |  | 
| Rafael J. Wysocki | 1bfcf13 | 2008-10-15 22:01:21 -0700 | [diff] [blame] | 701 | 	error = usermodehelper_disable(); | 
 | 702 | 	if (error) | 
 | 703 | 		goto Finish; | 
 | 704 |  | 
| Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 705 | 	error = create_basic_memory_bitmaps(); | 
 | 706 | 	if (error) | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 707 | 		goto Finish; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 |  | 
 | 709 | 	pr_debug("PM: Preparing processes for restore.\n"); | 
| Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 710 | 	error = prepare_processes(); | 
 | 711 | 	if (error) { | 
| Al Viro | c2dd0da | 2007-10-08 13:21:10 -0400 | [diff] [blame] | 712 | 		swsusp_close(FMODE_READ); | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 713 | 		goto Done; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | 	} | 
 | 715 |  | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 716 | 	pr_debug("PM: Reading hibernation image.\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 |  | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 718 | 	error = swsusp_read(&flags); | 
| Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 719 | 	if (!error) | 
| Rafael J. Wysocki | a634cc1 | 2007-07-19 01:47:30 -0700 | [diff] [blame] | 720 | 		hibernation_restore(flags & SF_PLATFORM_MODE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 |  | 
| Rafael J. Wysocki | ed746e3 | 2007-02-10 01:43:32 -0800 | [diff] [blame] | 722 | 	printk(KERN_ERR "PM: Restore failed, recovering.\n"); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 723 | 	swsusp_free(); | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 724 | 	thaw_processes(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 |  Done: | 
| Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 726 | 	free_basic_memory_bitmaps(); | 
| Rafael J. Wysocki | 1bfcf13 | 2008-10-15 22:01:21 -0700 | [diff] [blame] | 727 | 	usermodehelper_enable(); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 728 |  Finish: | 
| Alan Stern | c3e94d8 | 2007-11-19 23:38:25 +0100 | [diff] [blame] | 729 | 	pm_notifier_call_chain(PM_POST_RESTORE); | 
| Rafael J. Wysocki | 5a0a2f3 | 2008-01-11 01:25:21 +0100 | [diff] [blame] | 730 | 	pm_restore_console(); | 
| Rafael J. Wysocki | 0709db6 | 2007-05-06 14:50:45 -0700 | [diff] [blame] | 731 | 	atomic_inc(&snapshot_device_available); | 
| Shaohua Li | dd5d666 | 2005-09-03 15:57:04 -0700 | [diff] [blame] | 732 | 	/* For success case, the suspend path will release the lock */ | 
| Rafael J. Wysocki | 74dfd66 | 2007-05-06 14:50:43 -0700 | [diff] [blame] | 733 |  Unlock: | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 734 | 	mutex_unlock(&pm_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | 	pr_debug("PM: Resume from disk failed.\n"); | 
| Rafael J. Wysocki | 7777fab | 2007-07-19 01:47:29 -0700 | [diff] [blame] | 736 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } | 
 | 738 |  | 
 | 739 | late_initcall(software_resume); | 
 | 740 |  | 
 | 741 |  | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 742 | static const char * const hibernation_modes[] = { | 
 | 743 | 	[HIBERNATION_PLATFORM]	= "platform", | 
 | 744 | 	[HIBERNATION_SHUTDOWN]	= "shutdown", | 
 | 745 | 	[HIBERNATION_REBOOT]	= "reboot", | 
 | 746 | 	[HIBERNATION_TEST]	= "test", | 
 | 747 | 	[HIBERNATION_TESTPROC]	= "testproc", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | }; | 
 | 749 |  | 
 | 750 | /** | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 751 |  *	disk - Control hibernation mode | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 |  * | 
| Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 753 |  *	Suspend-to-disk can be handled in several ways. We have a few options | 
 | 754 |  *	for putting the system to sleep - using the platform driver (e.g. ACPI | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 755 |  *	or other hibernation_ops), powering off the system or rebooting the | 
 | 756 |  *	system (for testing) as well as the two test modes. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 |  * | 
| Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 758 |  *	The system can support 'platform', and that is known a priori (and | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 759 |  *	encoded by the presence of hibernation_ops). However, the user may | 
 | 760 |  *	choose 'shutdown' or 'reboot' as alternatives, as well as one fo the | 
 | 761 |  *	test modes, 'test' or 'testproc'. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 |  * | 
 | 763 |  *	show() will display what the mode is currently set to. | 
 | 764 |  *	store() will accept one of | 
 | 765 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 |  *	'platform' | 
 | 767 |  *	'shutdown' | 
 | 768 |  *	'reboot' | 
| Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 769 |  *	'test' | 
 | 770 |  *	'testproc' | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 |  * | 
| Johannes Berg | 11d77d0 | 2007-04-30 15:09:53 -0700 | [diff] [blame] | 772 |  *	It will only change to 'platform' if the system | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 773 |  *	supports it (as determined by having hibernation_ops). | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 |  */ | 
 | 775 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 776 | static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 777 | 			 char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 779 | 	int i; | 
 | 780 | 	char *start = buf; | 
 | 781 |  | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 782 | 	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { | 
 | 783 | 		if (!hibernation_modes[i]) | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 784 | 			continue; | 
 | 785 | 		switch (i) { | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 786 | 		case HIBERNATION_SHUTDOWN: | 
 | 787 | 		case HIBERNATION_REBOOT: | 
 | 788 | 		case HIBERNATION_TEST: | 
 | 789 | 		case HIBERNATION_TESTPROC: | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 790 | 			break; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 791 | 		case HIBERNATION_PLATFORM: | 
 | 792 | 			if (hibernation_ops) | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 793 | 				break; | 
 | 794 | 			/* not a valid mode, continue with loop */ | 
 | 795 | 			continue; | 
 | 796 | 		} | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 797 | 		if (i == hibernation_mode) | 
 | 798 | 			buf += sprintf(buf, "[%s] ", hibernation_modes[i]); | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 799 | 		else | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 800 | 			buf += sprintf(buf, "%s ", hibernation_modes[i]); | 
| Johannes Berg | f0ced9b | 2007-05-06 14:50:50 -0700 | [diff] [blame] | 801 | 	} | 
 | 802 | 	buf += sprintf(buf, "\n"); | 
 | 803 | 	return buf-start; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | } | 
 | 805 |  | 
 | 806 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 807 | static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 808 | 			  const char *buf, size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | { | 
 | 810 | 	int error = 0; | 
 | 811 | 	int i; | 
 | 812 | 	int len; | 
 | 813 | 	char *p; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 814 | 	int mode = HIBERNATION_INVALID; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 |  | 
 | 816 | 	p = memchr(buf, '\n', n); | 
 | 817 | 	len = p ? p - buf : n; | 
 | 818 |  | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 819 | 	mutex_lock(&pm_mutex); | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 820 | 	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { | 
| Rafael J. Wysocki | 8d98a69 | 2007-05-16 22:11:19 -0700 | [diff] [blame] | 821 | 		if (len == strlen(hibernation_modes[i]) | 
 | 822 | 		    && !strncmp(buf, hibernation_modes[i], len)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | 			mode = i; | 
 | 824 | 			break; | 
 | 825 | 		} | 
 | 826 | 	} | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 827 | 	if (mode != HIBERNATION_INVALID) { | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 828 | 		switch (mode) { | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 829 | 		case HIBERNATION_SHUTDOWN: | 
 | 830 | 		case HIBERNATION_REBOOT: | 
 | 831 | 		case HIBERNATION_TEST: | 
 | 832 | 		case HIBERNATION_TESTPROC: | 
 | 833 | 			hibernation_mode = mode; | 
| Johannes Berg | fe0c935 | 2007-04-30 15:09:51 -0700 | [diff] [blame] | 834 | 			break; | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 835 | 		case HIBERNATION_PLATFORM: | 
 | 836 | 			if (hibernation_ops) | 
 | 837 | 				hibernation_mode = mode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | 			else | 
 | 839 | 				error = -EINVAL; | 
 | 840 | 		} | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 841 | 	} else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | 		error = -EINVAL; | 
 | 843 |  | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 844 | 	if (!error) | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 845 | 		pr_debug("PM: Hibernation mode set to '%s'\n", | 
| Rafael J. Wysocki | a3d25c2 | 2007-05-09 02:33:18 -0700 | [diff] [blame] | 846 | 			 hibernation_modes[mode]); | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 847 | 	mutex_unlock(&pm_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | 	return error ? error : n; | 
 | 849 | } | 
 | 850 |  | 
 | 851 | power_attr(disk); | 
 | 852 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 853 | static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 854 | 			   char *buf) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | { | 
 | 856 | 	return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), | 
 | 857 | 		       MINOR(swsusp_resume_device)); | 
 | 858 | } | 
 | 859 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 860 | static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 861 | 			    const char *buf, size_t n) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | 	unsigned int maj, min; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | 	dev_t res; | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 865 | 	int ret = -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 |  | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 867 | 	if (sscanf(buf, "%u:%u", &maj, &min) != 2) | 
 | 868 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 |  | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 870 | 	res = MKDEV(maj,min); | 
 | 871 | 	if (maj != MAJOR(res) || min != MINOR(res)) | 
 | 872 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 |  | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 874 | 	mutex_lock(&pm_mutex); | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 875 | 	swsusp_resume_device = res; | 
| Stephen Hemminger | a6d7098 | 2006-12-06 20:34:35 -0800 | [diff] [blame] | 876 | 	mutex_unlock(&pm_mutex); | 
| Rafael J. Wysocki | 2397672 | 2007-12-08 02:09:43 +0100 | [diff] [blame] | 877 | 	printk(KERN_INFO "PM: Starting manual resume from disk\n"); | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 878 | 	noresume = 0; | 
 | 879 | 	software_resume(); | 
 | 880 | 	ret = n; | 
| Rafael J. Wysocki | 59a4933 | 2006-12-06 20:34:44 -0800 | [diff] [blame] | 881 |  out: | 
| Andrew Morton | a576219 | 2006-01-06 00:09:50 -0800 | [diff] [blame] | 882 | 	return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | } | 
 | 884 |  | 
 | 885 | power_attr(resume); | 
 | 886 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 887 | static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 888 | 			       char *buf) | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 889 | { | 
| Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 890 | 	return sprintf(buf, "%lu\n", image_size); | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 891 | } | 
 | 892 |  | 
| Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 893 | static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr, | 
 | 894 | 				const char *buf, size_t n) | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 895 | { | 
| Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 896 | 	unsigned long size; | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 897 |  | 
| Rafael J. Wysocki | 853609b | 2006-02-01 03:05:07 -0800 | [diff] [blame] | 898 | 	if (sscanf(buf, "%lu", &size) == 1) { | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 899 | 		image_size = size; | 
 | 900 | 		return n; | 
 | 901 | 	} | 
 | 902 |  | 
 | 903 | 	return -EINVAL; | 
 | 904 | } | 
 | 905 |  | 
 | 906 | power_attr(image_size); | 
 | 907 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | static struct attribute * g[] = { | 
 | 909 | 	&disk_attr.attr, | 
 | 910 | 	&resume_attr.attr, | 
| Rafael J. Wysocki | ca0aec0 | 2006-01-06 00:15:56 -0800 | [diff] [blame] | 911 | 	&image_size_attr.attr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | 	NULL, | 
 | 913 | }; | 
 | 914 |  | 
 | 915 |  | 
 | 916 | static struct attribute_group attr_group = { | 
 | 917 | 	.attrs = g, | 
 | 918 | }; | 
 | 919 |  | 
 | 920 |  | 
 | 921 | static int __init pm_disk_init(void) | 
 | 922 | { | 
| Greg Kroah-Hartman | d76e15f | 2007-11-27 11:28:26 -0800 | [diff] [blame] | 923 | 	return sysfs_create_group(power_kobj, &attr_group); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } | 
 | 925 |  | 
 | 926 | core_initcall(pm_disk_init); | 
 | 927 |  | 
 | 928 |  | 
 | 929 | static int __init resume_setup(char *str) | 
 | 930 | { | 
 | 931 | 	if (noresume) | 
 | 932 | 		return 1; | 
 | 933 |  | 
 | 934 | 	strncpy( resume_file, str, 255 ); | 
 | 935 | 	return 1; | 
 | 936 | } | 
 | 937 |  | 
| Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 938 | static int __init resume_offset_setup(char *str) | 
 | 939 | { | 
 | 940 | 	unsigned long long offset; | 
 | 941 |  | 
 | 942 | 	if (noresume) | 
 | 943 | 		return 1; | 
 | 944 |  | 
 | 945 | 	if (sscanf(str, "%llu", &offset) == 1) | 
 | 946 | 		swsusp_resume_block = offset; | 
 | 947 |  | 
 | 948 | 	return 1; | 
 | 949 | } | 
 | 950 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | static int __init noresume_setup(char *str) | 
 | 952 | { | 
 | 953 | 	noresume = 1; | 
 | 954 | 	return 1; | 
 | 955 | } | 
 | 956 |  | 
 | 957 | __setup("noresume", noresume_setup); | 
| Rafael J. Wysocki | 9a154d9 | 2006-12-06 20:34:12 -0800 | [diff] [blame] | 958 | __setup("resume_offset=", resume_offset_setup); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | __setup("resume=", resume_setup); |