| 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> | 
 | 17 | #include <linux/delay.h> | 
 | 18 | #include <linux/fs.h> | 
| Andrew Morton | d53d9f1 | 2005-07-12 13:58:07 -0700 | [diff] [blame] | 19 | #include <linux/mount.h> | 
 | 20 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "power.h" | 
 | 22 |  | 
 | 23 |  | 
 | 24 | extern suspend_disk_method_t pm_disk_mode; | 
 | 25 | extern struct pm_ops * pm_ops; | 
 | 26 |  | 
 | 27 | extern int swsusp_suspend(void); | 
 | 28 | extern int swsusp_write(void); | 
 | 29 | extern int swsusp_check(void); | 
 | 30 | extern int swsusp_read(void); | 
 | 31 | extern void swsusp_close(void); | 
 | 32 | extern int swsusp_resume(void); | 
 | 33 | extern int swsusp_free(void); | 
 | 34 |  | 
 | 35 |  | 
 | 36 | static int noresume = 0; | 
 | 37 | char resume_file[256] = CONFIG_PM_STD_PARTITION; | 
 | 38 | dev_t swsusp_resume_device; | 
 | 39 |  | 
 | 40 | /** | 
 | 41 |  *	power_down - Shut machine down for hibernate. | 
 | 42 |  *	@mode:		Suspend-to-disk mode | 
 | 43 |  * | 
 | 44 |  *	Use the platform driver, if configured so, and return gracefully if it | 
 | 45 |  *	fails. | 
 | 46 |  *	Otherwise, try to power off and reboot. If they fail, halt the machine, | 
 | 47 |  *	there ain't no turning back. | 
 | 48 |  */ | 
 | 49 |  | 
 | 50 | static void power_down(suspend_disk_method_t mode) | 
 | 51 | { | 
 | 52 | 	unsigned long flags; | 
 | 53 | 	int error = 0; | 
 | 54 |  | 
 | 55 | 	local_irq_save(flags); | 
 | 56 | 	switch(mode) { | 
 | 57 | 	case PM_DISK_PLATFORM: | 
 | 58 |  		device_shutdown(); | 
 | 59 | 		error = pm_ops->enter(PM_SUSPEND_DISK); | 
 | 60 | 		break; | 
 | 61 | 	case PM_DISK_SHUTDOWN: | 
| Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 62 | 		kernel_power_off(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | 		break; | 
 | 64 | 	case PM_DISK_REBOOT: | 
| Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 65 | 		kernel_restart(NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | 		break; | 
 | 67 | 	} | 
| Eric W. Biederman | fdde86a | 2005-07-26 12:01:17 -0600 | [diff] [blame] | 68 | 	kernel_halt(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | 	/* Valid image is on the disk, if we continue we risk serious data corruption | 
 | 70 | 	   after resume. */ | 
 | 71 | 	printk(KERN_CRIT "Please power me down manually\n"); | 
 | 72 | 	while(1); | 
 | 73 | } | 
 | 74 |  | 
 | 75 |  | 
 | 76 | static int in_suspend __nosavedata = 0; | 
 | 77 |  | 
 | 78 |  | 
 | 79 | /** | 
 | 80 |  *	free_some_memory -  Try to free as much memory as possible | 
 | 81 |  * | 
 | 82 |  *	... but do not OOM-kill anyone | 
 | 83 |  * | 
 | 84 |  *	Notice: all userland should be stopped at this point, or | 
 | 85 |  *	livelock is possible. | 
 | 86 |  */ | 
 | 87 |  | 
 | 88 | static void free_some_memory(void) | 
 | 89 | { | 
 | 90 | 	unsigned int i = 0; | 
 | 91 | 	unsigned int tmp; | 
 | 92 | 	unsigned long pages = 0; | 
 | 93 | 	char *p = "-\\|/"; | 
 | 94 |  | 
 | 95 | 	printk("Freeing memory...  "); | 
 | 96 | 	while ((tmp = shrink_all_memory(10000))) { | 
 | 97 | 		pages += tmp; | 
 | 98 | 		printk("\b%c", p[i]); | 
 | 99 | 		i++; | 
 | 100 | 		if (i > 3) | 
 | 101 | 			i = 0; | 
 | 102 | 	} | 
 | 103 | 	printk("\bdone (%li pages freed)\n", pages); | 
 | 104 | } | 
 | 105 |  | 
 | 106 |  | 
 | 107 | static inline void platform_finish(void) | 
 | 108 | { | 
 | 109 | 	if (pm_disk_mode == PM_DISK_PLATFORM) { | 
 | 110 | 		if (pm_ops && pm_ops->finish) | 
 | 111 | 			pm_ops->finish(PM_SUSPEND_DISK); | 
 | 112 | 	} | 
 | 113 | } | 
 | 114 |  | 
 | 115 | static void finish(void) | 
 | 116 | { | 
 | 117 | 	device_resume(); | 
 | 118 | 	platform_finish(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | 	thaw_processes(); | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 120 | 	enable_nonboot_cpus(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | 	pm_restore_console(); | 
 | 122 | } | 
 | 123 |  | 
 | 124 |  | 
 | 125 | static int prepare_processes(void) | 
 | 126 | { | 
 | 127 | 	int error; | 
 | 128 |  | 
 | 129 | 	pm_prepare_console(); | 
 | 130 |  | 
 | 131 | 	sys_sync(); | 
 | 132 |  | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 133 | 	disable_nonboot_cpus(); | 
 | 134 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | 	if (freeze_processes()) { | 
 | 136 | 		error = -EBUSY; | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 137 | 		goto thaw; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | 	} | 
 | 139 |  | 
 | 140 | 	if (pm_disk_mode == PM_DISK_PLATFORM) { | 
 | 141 | 		if (pm_ops && pm_ops->prepare) { | 
 | 142 | 			if ((error = pm_ops->prepare(PM_SUSPEND_DISK))) | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 143 | 				goto thaw; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | 		} | 
 | 145 | 	} | 
 | 146 |  | 
 | 147 | 	/* Free memory before shutting down devices. */ | 
 | 148 | 	free_some_memory(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | 	return 0; | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 150 | thaw: | 
 | 151 | 	thaw_processes(); | 
 | 152 | 	enable_nonboot_cpus(); | 
 | 153 | 	pm_restore_console(); | 
 | 154 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } | 
 | 156 |  | 
 | 157 | static void unprepare_processes(void) | 
 | 158 | { | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 159 | 	platform_finish(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | 	thaw_processes(); | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 161 | 	enable_nonboot_cpus(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | 	pm_restore_console(); | 
 | 163 | } | 
 | 164 |  | 
 | 165 | static int prepare_devices(void) | 
 | 166 | { | 
 | 167 | 	int error; | 
 | 168 |  | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 169 | 	if ((error = device_suspend(PMSG_FREEZE))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | 		printk("Some devices failed to suspend\n"); | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 171 | 	return error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } | 
 | 173 |  | 
 | 174 | /** | 
 | 175 |  *	pm_suspend_disk - The granpappy of power management. | 
 | 176 |  * | 
 | 177 |  *	If we're going through the firmware, then get it over with quickly. | 
 | 178 |  * | 
 | 179 |  *	If not, then call swsusp to do its thing, then figure out how | 
 | 180 |  *	to power down the system. | 
 | 181 |  */ | 
 | 182 |  | 
 | 183 | int pm_suspend_disk(void) | 
 | 184 | { | 
 | 185 | 	int error; | 
 | 186 |  | 
 | 187 | 	error = prepare_processes(); | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 188 | 	if (error) | 
 | 189 | 		return error; | 
 | 190 | 	error = prepare_devices(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 |  | 
 | 192 | 	if (error) { | 
 | 193 | 		unprepare_processes(); | 
 | 194 | 		return error; | 
 | 195 | 	} | 
 | 196 |  | 
 | 197 | 	pr_debug("PM: Attempting to suspend to disk.\n"); | 
 | 198 | 	if (pm_disk_mode == PM_DISK_FIRMWARE) | 
 | 199 | 		return pm_ops->enter(PM_SUSPEND_DISK); | 
 | 200 |  | 
 | 201 | 	pr_debug("PM: snapshotting memory.\n"); | 
 | 202 | 	in_suspend = 1; | 
 | 203 | 	if ((error = swsusp_suspend())) | 
 | 204 | 		goto Done; | 
 | 205 |  | 
 | 206 | 	if (in_suspend) { | 
 | 207 | 		pr_debug("PM: writing image.\n"); | 
 | 208 | 		error = swsusp_write(); | 
 | 209 | 		if (!error) | 
 | 210 | 			power_down(pm_disk_mode); | 
 | 211 | 	} else | 
 | 212 | 		pr_debug("PM: Image restored successfully.\n"); | 
 | 213 | 	swsusp_free(); | 
 | 214 |  Done: | 
 | 215 | 	finish(); | 
 | 216 | 	return error; | 
 | 217 | } | 
 | 218 |  | 
 | 219 |  | 
 | 220 | /** | 
 | 221 |  *	software_resume - Resume from a saved image. | 
 | 222 |  * | 
 | 223 |  *	Called as a late_initcall (so all devices are discovered and | 
 | 224 |  *	initialized), we call swsusp to see if we have a saved image or not. | 
 | 225 |  *	If so, we quiesce devices, the restore the saved image. We will | 
 | 226 |  *	return above (in pm_suspend_disk() ) if everything goes well. | 
 | 227 |  *	Otherwise, we fail gracefully and return to the normally | 
 | 228 |  *	scheduled program. | 
 | 229 |  * | 
 | 230 |  */ | 
 | 231 |  | 
 | 232 | static int software_resume(void) | 
 | 233 | { | 
 | 234 | 	int error; | 
 | 235 |  | 
| Pavel Machek | 3efa147 | 2005-07-07 17:56:43 -0700 | [diff] [blame] | 236 | 	if (!swsusp_resume_device) { | 
 | 237 | 		if (!strlen(resume_file)) | 
 | 238 | 			return -ENOENT; | 
 | 239 | 		swsusp_resume_device = name_to_dev_t(resume_file); | 
 | 240 | 		pr_debug("swsusp: Resume From Partition %s\n", resume_file); | 
 | 241 | 	} else { | 
 | 242 | 		pr_debug("swsusp: Resume From Partition %d:%d\n", | 
 | 243 | 			 MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device)); | 
 | 244 | 	} | 
 | 245 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | 	if (noresume) { | 
 | 247 | 		/** | 
 | 248 | 		 * FIXME: If noresume is specified, we need to find the partition | 
 | 249 | 		 * and reset it back to normal swap space. | 
 | 250 | 		 */ | 
 | 251 | 		return 0; | 
 | 252 | 	} | 
 | 253 |  | 
 | 254 | 	pr_debug("PM: Checking swsusp image.\n"); | 
 | 255 |  | 
 | 256 | 	if ((error = swsusp_check())) | 
 | 257 | 		goto Done; | 
 | 258 |  | 
 | 259 | 	pr_debug("PM: Preparing processes for restore.\n"); | 
 | 260 |  | 
 | 261 | 	if ((error = prepare_processes())) { | 
 | 262 | 		swsusp_close(); | 
| Li Shaohua | 5a72e04 | 2005-06-25 14:55:06 -0700 | [diff] [blame] | 263 | 		goto Done; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | 	} | 
 | 265 |  | 
 | 266 | 	pr_debug("PM: Reading swsusp image.\n"); | 
 | 267 |  | 
 | 268 | 	if ((error = swsusp_read())) | 
 | 269 | 		goto Cleanup; | 
 | 270 |  | 
 | 271 | 	pr_debug("PM: Preparing devices for restore.\n"); | 
 | 272 |  | 
 | 273 | 	if ((error = prepare_devices())) | 
 | 274 | 		goto Free; | 
 | 275 |  | 
 | 276 | 	mb(); | 
 | 277 |  | 
 | 278 | 	pr_debug("PM: Restoring saved image.\n"); | 
 | 279 | 	swsusp_resume(); | 
 | 280 | 	pr_debug("PM: Restore failed, recovering.n"); | 
 | 281 | 	finish(); | 
 | 282 |  Free: | 
 | 283 | 	swsusp_free(); | 
 | 284 |  Cleanup: | 
 | 285 | 	unprepare_processes(); | 
 | 286 |  Done: | 
 | 287 | 	pr_debug("PM: Resume from disk failed.\n"); | 
 | 288 | 	return 0; | 
 | 289 | } | 
 | 290 |  | 
 | 291 | late_initcall(software_resume); | 
 | 292 |  | 
 | 293 |  | 
 | 294 | static char * pm_disk_modes[] = { | 
 | 295 | 	[PM_DISK_FIRMWARE]	= "firmware", | 
 | 296 | 	[PM_DISK_PLATFORM]	= "platform", | 
 | 297 | 	[PM_DISK_SHUTDOWN]	= "shutdown", | 
 | 298 | 	[PM_DISK_REBOOT]	= "reboot", | 
 | 299 | }; | 
 | 300 |  | 
 | 301 | /** | 
 | 302 |  *	disk - Control suspend-to-disk mode | 
 | 303 |  * | 
 | 304 |  *	Suspend-to-disk can be handled in several ways. The greatest | 
 | 305 |  *	distinction is who writes memory to disk - the firmware or the OS. | 
 | 306 |  *	If the firmware does it, we assume that it also handles suspending | 
 | 307 |  *	the system. | 
 | 308 |  *	If the OS does it, then we have three options for putting the system | 
 | 309 |  *	to sleep - using the platform driver (e.g. ACPI or other PM registers), | 
 | 310 |  *	powering off the system or rebooting the system (for testing). | 
 | 311 |  * | 
 | 312 |  *	The system will support either 'firmware' or 'platform', and that is | 
 | 313 |  *	known a priori (and encoded in pm_ops). But, the user may choose | 
 | 314 |  *	'shutdown' or 'reboot' as alternatives. | 
 | 315 |  * | 
 | 316 |  *	show() will display what the mode is currently set to. | 
 | 317 |  *	store() will accept one of | 
 | 318 |  * | 
 | 319 |  *	'firmware' | 
 | 320 |  *	'platform' | 
 | 321 |  *	'shutdown' | 
 | 322 |  *	'reboot' | 
 | 323 |  * | 
 | 324 |  *	It will only change to 'firmware' or 'platform' if the system | 
 | 325 |  *	supports it (as determined from pm_ops->pm_disk_mode). | 
 | 326 |  */ | 
 | 327 |  | 
 | 328 | static ssize_t disk_show(struct subsystem * subsys, char * buf) | 
 | 329 | { | 
 | 330 | 	return sprintf(buf, "%s\n", pm_disk_modes[pm_disk_mode]); | 
 | 331 | } | 
 | 332 |  | 
 | 333 |  | 
 | 334 | static ssize_t disk_store(struct subsystem * s, const char * buf, size_t n) | 
 | 335 | { | 
 | 336 | 	int error = 0; | 
 | 337 | 	int i; | 
 | 338 | 	int len; | 
 | 339 | 	char *p; | 
 | 340 | 	suspend_disk_method_t mode = 0; | 
 | 341 |  | 
 | 342 | 	p = memchr(buf, '\n', n); | 
 | 343 | 	len = p ? p - buf : n; | 
 | 344 |  | 
 | 345 | 	down(&pm_sem); | 
 | 346 | 	for (i = PM_DISK_FIRMWARE; i < PM_DISK_MAX; i++) { | 
 | 347 | 		if (!strncmp(buf, pm_disk_modes[i], len)) { | 
 | 348 | 			mode = i; | 
 | 349 | 			break; | 
 | 350 | 		} | 
 | 351 | 	} | 
 | 352 | 	if (mode) { | 
 | 353 | 		if (mode == PM_DISK_SHUTDOWN || mode == PM_DISK_REBOOT) | 
 | 354 | 			pm_disk_mode = mode; | 
 | 355 | 		else { | 
 | 356 | 			if (pm_ops && pm_ops->enter && | 
 | 357 | 			    (mode == pm_ops->pm_disk_mode)) | 
 | 358 | 				pm_disk_mode = mode; | 
 | 359 | 			else | 
 | 360 | 				error = -EINVAL; | 
 | 361 | 		} | 
 | 362 | 	} else | 
 | 363 | 		error = -EINVAL; | 
 | 364 |  | 
 | 365 | 	pr_debug("PM: suspend-to-disk mode set to '%s'\n", | 
 | 366 | 		 pm_disk_modes[mode]); | 
 | 367 | 	up(&pm_sem); | 
 | 368 | 	return error ? error : n; | 
 | 369 | } | 
 | 370 |  | 
 | 371 | power_attr(disk); | 
 | 372 |  | 
 | 373 | static ssize_t resume_show(struct subsystem * subsys, char *buf) | 
 | 374 | { | 
 | 375 | 	return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), | 
 | 376 | 		       MINOR(swsusp_resume_device)); | 
 | 377 | } | 
 | 378 |  | 
 | 379 | static ssize_t resume_store(struct subsystem * subsys, const char * buf, size_t n) | 
 | 380 | { | 
 | 381 | 	int len; | 
 | 382 | 	char *p; | 
 | 383 | 	unsigned int maj, min; | 
 | 384 | 	int error = -EINVAL; | 
 | 385 | 	dev_t res; | 
 | 386 |  | 
 | 387 | 	p = memchr(buf, '\n', n); | 
 | 388 | 	len = p ? p - buf : n; | 
 | 389 |  | 
 | 390 | 	if (sscanf(buf, "%u:%u", &maj, &min) == 2) { | 
 | 391 | 		res = MKDEV(maj,min); | 
 | 392 | 		if (maj == MAJOR(res) && min == MINOR(res)) { | 
 | 393 | 			swsusp_resume_device = res; | 
 | 394 | 			printk("Attempting manual resume\n"); | 
 | 395 | 			noresume = 0; | 
 | 396 | 			software_resume(); | 
 | 397 | 		} | 
 | 398 | 	} | 
 | 399 |  | 
 | 400 | 	return error >= 0 ? n : error; | 
 | 401 | } | 
 | 402 |  | 
 | 403 | power_attr(resume); | 
 | 404 |  | 
 | 405 | static struct attribute * g[] = { | 
 | 406 | 	&disk_attr.attr, | 
 | 407 | 	&resume_attr.attr, | 
 | 408 | 	NULL, | 
 | 409 | }; | 
 | 410 |  | 
 | 411 |  | 
 | 412 | static struct attribute_group attr_group = { | 
 | 413 | 	.attrs = g, | 
 | 414 | }; | 
 | 415 |  | 
 | 416 |  | 
 | 417 | static int __init pm_disk_init(void) | 
 | 418 | { | 
 | 419 | 	return sysfs_create_group(&power_subsys.kset.kobj,&attr_group); | 
 | 420 | } | 
 | 421 |  | 
 | 422 | core_initcall(pm_disk_init); | 
 | 423 |  | 
 | 424 |  | 
 | 425 | static int __init resume_setup(char *str) | 
 | 426 | { | 
 | 427 | 	if (noresume) | 
 | 428 | 		return 1; | 
 | 429 |  | 
 | 430 | 	strncpy( resume_file, str, 255 ); | 
 | 431 | 	return 1; | 
 | 432 | } | 
 | 433 |  | 
 | 434 | static int __init noresume_setup(char *str) | 
 | 435 | { | 
 | 436 | 	noresume = 1; | 
 | 437 | 	return 1; | 
 | 438 | } | 
 | 439 |  | 
 | 440 | __setup("noresume", noresume_setup); | 
 | 441 | __setup("resume=", resume_setup); |