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