hibernate.c 22.4 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
L
Linus Torvalds 已提交
3 4 5 6
 *
 * Copyright (c) 2003 Patrick Mochel
 * Copyright (c) 2003 Open Source Development Lab
 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
7
 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15 16
 *
 * This file is released under the GPLv2.
 */

#include <linux/suspend.h>
#include <linux/syscalls.h>
#include <linux/reboot.h>
#include <linux/string.h>
#include <linux/device.h>
17
#include <linux/kmod.h>
L
Linus Torvalds 已提交
18 19
#include <linux/delay.h>
#include <linux/fs.h>
20
#include <linux/mount.h>
21
#include <linux/pm.h>
22
#include <linux/console.h>
23
#include <linux/cpu.h>
24
#include <linux/freezer.h>
25
#include <scsi/scsi_scan.h>
26
#include <asm/suspend.h>
27

L
Linus Torvalds 已提交
28 29 30 31
#include "power.h"


static int noresume = 0;
32
static char resume_file[256] = CONFIG_PM_STD_PARTITION;
L
Linus Torvalds 已提交
33
dev_t swsusp_resume_device;
34
sector_t swsusp_resume_block;
35
int in_suspend __nosavedata = 0;
L
Linus Torvalds 已提交
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
enum {
	HIBERNATION_INVALID,
	HIBERNATION_PLATFORM,
	HIBERNATION_TEST,
	HIBERNATION_TESTPROC,
	HIBERNATION_SHUTDOWN,
	HIBERNATION_REBOOT,
	/* keep last */
	__HIBERNATION_AFTER_LAST
};
#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)

static int hibernation_mode = HIBERNATION_SHUTDOWN;

52
static struct platform_hibernation_ops *hibernation_ops;
53 54 55 56 57 58

/**
 * hibernation_set_ops - set the global hibernate operations
 * @ops: the hibernation operations to use in subsequent hibernation transitions
 */

59
void hibernation_set_ops(struct platform_hibernation_ops *ops)
60
{
61 62
	if (ops && !(ops->begin && ops->end &&  ops->pre_snapshot
	    && ops->prepare && ops->finish && ops->enter && ops->pre_restore
63
	    && ops->restore_cleanup)) {
64 65 66 67 68 69 70 71 72 73 74 75 76
		WARN_ON(1);
		return;
	}
	mutex_lock(&pm_mutex);
	hibernation_ops = ops;
	if (ops)
		hibernation_mode = HIBERNATION_PLATFORM;
	else if (hibernation_mode == HIBERNATION_PLATFORM)
		hibernation_mode = HIBERNATION_SHUTDOWN;

	mutex_unlock(&pm_mutex);
}

77 78 79 80 81 82 83 84
static bool entering_platform_hibernation;

bool system_entering_hibernation(void)
{
	return entering_platform_hibernation;
}
EXPORT_SYMBOL(system_entering_hibernation);

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#ifdef CONFIG_PM_DEBUG
static void hibernation_debug_sleep(void)
{
	printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
	mdelay(5000);
}

static int hibernation_testmode(int mode)
{
	if (hibernation_mode == mode) {
		hibernation_debug_sleep();
		return 1;
	}
	return 0;
}

static int hibernation_test(int level)
{
	if (pm_test_level == level) {
		hibernation_debug_sleep();
		return 1;
	}
	return 0;
}
#else /* !CONFIG_PM_DEBUG */
static int hibernation_testmode(int mode) { return 0; }
static int hibernation_test(int level) { return 0; }
#endif /* !CONFIG_PM_DEBUG */

114
/**
115
 *	platform_begin - tell the platform driver that we're starting
116 117 118
 *	hibernation
 */

119
static int platform_begin(int platform_mode)
120 121
{
	return (platform_mode && hibernation_ops) ?
122 123 124 125 126 127 128 129 130 131 132 133
		hibernation_ops->begin() : 0;
}

/**
 *	platform_end - tell the platform driver that we've entered the
 *	working state
 */

static void platform_end(int platform_mode)
{
	if (platform_mode && hibernation_ops)
		hibernation_ops->end();
134
}
135

136
/**
137
 *	platform_pre_snapshot - prepare the machine for hibernation using the
138 139 140
 *	platform driver if so configured and return an error code if it fails
 */

141
static int platform_pre_snapshot(int platform_mode)
142
{
143
	return (platform_mode && hibernation_ops) ?
144
		hibernation_ops->pre_snapshot() : 0;
145
}
146

147 148 149 150 151 152 153 154 155 156 157
/**
 *	platform_leave - prepare the machine for switching to the normal mode
 *	of operation using the platform driver (called with interrupts disabled)
 */

static void platform_leave(int platform_mode)
{
	if (platform_mode && hibernation_ops)
		hibernation_ops->leave();
}

158 159 160 161 162
/**
 *	platform_finish - switch the machine to the normal mode of operation
 *	using the platform driver (must be called after platform_prepare())
 */

163
static void platform_finish(int platform_mode)
164
{
165
	if (platform_mode && hibernation_ops)
166
		hibernation_ops->finish();
167 168
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
/**
 *	platform_pre_restore - prepare the platform for the restoration from a
 *	hibernation image.  If the restore fails after this function has been
 *	called, platform_restore_cleanup() must be called.
 */

static int platform_pre_restore(int platform_mode)
{
	return (platform_mode && hibernation_ops) ?
		hibernation_ops->pre_restore() : 0;
}

/**
 *	platform_restore_cleanup - switch the platform to the normal mode of
 *	operation after a failing restore.  If platform_pre_restore() has been
 *	called before the failing restore, this function must be called too,
 *	regardless of the result of platform_pre_restore().
 */

static void platform_restore_cleanup(int platform_mode)
{
	if (platform_mode && hibernation_ops)
		hibernation_ops->restore_cleanup();
}

194 195 196 197 198 199 200 201 202 203 204
/**
 *	platform_recover - recover the platform from a failure to suspend
 *	devices.
 */

static void platform_recover(int platform_mode)
{
	if (platform_mode && hibernation_ops && hibernation_ops->recover)
		hibernation_ops->recover();
}

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
/**
 *	swsusp_show_speed - print the time elapsed between two events.
 *	@start: Starting event.
 *	@stop: Final event.
 *	@nr_pages -	number of pages processed between @start and @stop
 *	@msg -		introductory message to print
 */

void swsusp_show_speed(struct timeval *start, struct timeval *stop,
			unsigned nr_pages, char *msg)
{
	s64 elapsed_centisecs64;
	int centisecs;
	int k;
	int kps;

	elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
	do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
	centisecs = elapsed_centisecs64;
	if (centisecs == 0)
		centisecs = 1;	/* avoid div-by-zero */
	k = nr_pages * (PAGE_SIZE / 1024);
	kps = (k * 100) / centisecs;
	printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
			msg, k,
			centisecs / 100, centisecs % 100,
			kps / 1000, (kps % 1000) / 10);
}

234 235 236 237 238 239
/**
 *	create_image - freeze devices that need to be frozen with interrupts
 *	off, create the hibernation image and thaw those devices.  Control
 *	reappears in this routine after a restore.
 */

240
static int create_image(int platform_mode)
241 242 243 244 245 246 247
{
	int error;

	error = arch_prepare_suspend();
	if (error)
		return error;

248 249
	/* At this point, dpm_suspend_start() has been called, but *not*
	 * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
250 251 252 253
	 * Otherwise, drivers for some devices (e.g. interrupt controllers)
	 * become desynchronized with the actual state of the hardware
	 * at resume time, and evil weirdness ensues.
	 */
254
	error = dpm_suspend_noirq(PMSG_FREEZE);
255
	if (error) {
R
Rafael J. Wysocki 已提交
256 257
		printk(KERN_ERR "PM: Some devices failed to power down, "
			"aborting hibernation\n");
258
		return error;
259
	}
260

261 262 263 264 265 266 267 268 269
	error = platform_pre_snapshot(platform_mode);
	if (error || hibernation_test(TEST_PLATFORM))
		goto Platform_finish;

	error = disable_nonboot_cpus();
	if (error || hibernation_test(TEST_CPUS)
	    || hibernation_testmode(HIBERNATION_TEST))
		goto Enable_cpus;

270 271
	local_irq_disable();

272
	error = sysdev_suspend(PMSG_FREEZE);
273
	if (error) {
274
		printk(KERN_ERR "PM: Some system devices failed to power down, "
275
			"aborting hibernation\n");
276
		goto Enable_irqs;
277
	}
278

279 280 281 282
	if (hibernation_test(TEST_CORE))
		goto Power_up;

	in_suspend = 1;
283 284 285
	save_processor_state();
	error = swsusp_arch_suspend();
	if (error)
R
Rafael J. Wysocki 已提交
286 287
		printk(KERN_ERR "PM: Error %d creating hibernation image\n",
			error);
288 289 290 291
	/* Restore control flow magically appears here */
	restore_processor_state();
	if (!in_suspend)
		platform_leave(platform_mode);
292

293
 Power_up:
294
	sysdev_resume();
295
	/* NOTE:  dpm_resume_noirq() is just a resume() for devices
296 297
	 * that suspended with irqs off ... no overall powerup.
	 */
298

299
 Enable_irqs:
300 301
	local_irq_enable();

302 303 304 305 306 307
 Enable_cpus:
	enable_nonboot_cpus();

 Platform_finish:
	platform_finish(platform_mode);

308
	dpm_resume_noirq(in_suspend ?
309
		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
310

311 312 313
	return error;
}

314 315 316 317
/**
 *	hibernation_snapshot - quiesce devices and create the hibernation
 *	snapshot image.
 *	@platform_mode - if set, use the platform driver, if available, to
318
 *			 prepare the platform firmware for the power transition.
319 320 321 322 323 324
 *
 *	Must be called with pm_mutex held
 */

int hibernation_snapshot(int platform_mode)
{
325
	int error;
326

327
	error = platform_begin(platform_mode);
328
	if (error)
329
		return error;
330

331 332
	/* Preallocate image memory before shutting down devices. */
	error = hibernate_preallocate_memory();
333
	if (error)
334
		goto Close;
335

336
	suspend_console();
337
	error = dpm_suspend_start(PMSG_FREEZE);
338
	if (error)
339
		goto Recover_platform;
340

341
	if (hibernation_test(TEST_DEVICES))
342
		goto Recover_platform;
343

344 345
	error = create_image(platform_mode);
	/* Control returns here after successful restore */
346 347

 Resume_devices:
348 349 350 351
	/* We may need to release the preallocated image pages here. */
	if (error || !in_suspend)
		swsusp_free();

352
	dpm_resume_end(in_suspend ?
353
		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
354
	resume_console();
355 356
 Close:
	platform_end(platform_mode);
357
	return error;
358 359 360 361

 Recover_platform:
	platform_recover(platform_mode);
	goto Resume_devices;
362 363
}

364 365 366 367 368 369 370 371
/**
 *	resume_target_kernel - prepare devices that need to be suspended with
 *	interrupts off, restore the contents of highmem that have not been
 *	restored yet from the image and run the low level code that will restore
 *	the remaining contents of memory and switch to the just restored target
 *	kernel.
 */

372
static int resume_target_kernel(bool platform_mode)
373 374 375
{
	int error;

376
	error = dpm_suspend_noirq(PMSG_QUIESCE);
377
	if (error) {
R
Rafael J. Wysocki 已提交
378
		printk(KERN_ERR "PM: Some devices failed to power down, "
379
			"aborting resume\n");
380
		return error;
381
	}
382

383 384 385 386 387 388 389 390
	error = platform_pre_restore(platform_mode);
	if (error)
		goto Cleanup;

	error = disable_nonboot_cpus();
	if (error)
		goto Enable_cpus;

391 392
	local_irq_disable();

393 394 395 396
	error = sysdev_suspend(PMSG_QUIESCE);
	if (error)
		goto Enable_irqs;

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	/* We'll ignore saved state, but this gets preempt count (etc) right */
	save_processor_state();
	error = restore_highmem();
	if (!error) {
		error = swsusp_arch_resume();
		/*
		 * The code below is only ever reached in case of a failure.
		 * Otherwise execution continues at place where
		 * swsusp_arch_suspend() was called
		 */
		BUG_ON(!error);
		/* This call to restore_highmem() undos the previous one */
		restore_highmem();
	}
	/*
	 * The only reason why swsusp_arch_resume() can fail is memory being
	 * very tight, so we have to free it as soon as we can to avoid
	 * subsequent failures
	 */
	swsusp_free();
	restore_processor_state();
	touch_softlockup_watchdog();
419

420
	sysdev_resume();
421

422
 Enable_irqs:
423
	local_irq_enable();
424

425 426 427 428 429 430
 Enable_cpus:
	enable_nonboot_cpus();

 Cleanup:
	platform_restore_cleanup(platform_mode);

431
	dpm_resume_noirq(PMSG_RECOVER);
432

433 434 435
	return error;
}

436 437 438
/**
 *	hibernation_restore - quiesce devices and restore the hibernation
 *	snapshot image.  If successful, control returns in hibernation_snaphot()
439
 *	@platform_mode - if set, use the platform driver, if available, to
440
 *			 prepare the platform firmware for the transition.
441 442 443 444
 *
 *	Must be called with pm_mutex held
 */

445
int hibernation_restore(int platform_mode)
446
{
447
	int error;
448 449 450

	pm_prepare_console();
	suspend_console();
451
	error = dpm_suspend_start(PMSG_QUIESCE);
452
	if (!error) {
453
		error = resume_target_kernel(platform_mode);
454
		dpm_resume_end(PMSG_RECOVER);
455
	}
456 457 458 459 460 461 462 463 464 465 466 467
	resume_console();
	pm_restore_console();
	return error;
}

/**
 *	hibernation_platform_enter - enter the hibernation state using the
 *	platform driver (if available)
 */

int hibernation_platform_enter(void)
{
468
	int error;
469

470 471 472 473 474 475 476 477
	if (!hibernation_ops)
		return -ENOSYS;

	/*
	 * We have cancelled the power transition by running
	 * hibernation_ops->finish() before saving the image, so we should let
	 * the firmware know that we're going to enter the sleep state after all
	 */
478
	error = hibernation_ops->begin();
479
	if (error)
480
		goto Close;
481

482
	entering_platform_hibernation = true;
483
	suspend_console();
484
	error = dpm_suspend_start(PMSG_HIBERNATE);
485 486 487 488 489
	if (error) {
		if (hibernation_ops->recover)
			hibernation_ops->recover();
		goto Resume_devices;
	}
490

491
	error = dpm_suspend_noirq(PMSG_HIBERNATE);
492
	if (error)
493
		goto Resume_devices;
494

495 496
	error = hibernation_ops->prepare();
	if (error)
497
		goto Platform_finish;
498 499 500

	error = disable_nonboot_cpus();
	if (error)
501
		goto Platform_finish;
502

503 504 505 506 507
	local_irq_disable();
	sysdev_suspend(PMSG_HIBERNATE);
	hibernation_ops->enter();
	/* We should never get here */
	while (1);
508 509 510 511 512

	/*
	 * We don't need to reenable the nonboot CPUs or resume consoles, since
	 * the system is going to be halted anyway.
	 */
513
 Platform_finish:
514
	hibernation_ops->finish();
515

516
	dpm_suspend_noirq(PMSG_RESTORE);
517

518
 Resume_devices:
519
	entering_platform_hibernation = false;
520
	dpm_resume_end(PMSG_RESTORE);
521
	resume_console();
522

523 524
 Close:
	hibernation_ops->end();
525

526
	return error;
527 528
}

L
Linus Torvalds 已提交
529
/**
530
 *	power_down - Shut the machine down for hibernation.
L
Linus Torvalds 已提交
531
 *
532 533
 *	Use the platform driver, if configured so; otherwise try
 *	to power off or reboot.
L
Linus Torvalds 已提交
534 535
 */

536
static void power_down(void)
L
Linus Torvalds 已提交
537
{
538 539 540
	switch (hibernation_mode) {
	case HIBERNATION_TEST:
	case HIBERNATION_TESTPROC:
541
		break;
542
	case HIBERNATION_REBOOT:
543
		kernel_restart(NULL);
L
Linus Torvalds 已提交
544
		break;
545
	case HIBERNATION_PLATFORM:
546
		hibernation_platform_enter();
547 548 549
	case HIBERNATION_SHUTDOWN:
		kernel_power_off();
		break;
L
Linus Torvalds 已提交
550
	}
551
	kernel_halt();
552 553 554 555
	/*
	 * Valid image is on the disk, if we continue we risk serious data
	 * corruption after resume.
	 */
R
Rafael J. Wysocki 已提交
556
	printk(KERN_CRIT "PM: Please power down manually\n");
L
Linus Torvalds 已提交
557 558 559 560 561
	while(1);
}

static int prepare_processes(void)
{
R
Rafael J. Wysocki 已提交
562
	int error = 0;
L
Linus Torvalds 已提交
563 564 565

	if (freeze_processes()) {
		error = -EBUSY;
566
		thaw_processes();
L
Linus Torvalds 已提交
567
	}
L
Li Shaohua 已提交
568
	return error;
L
Linus Torvalds 已提交
569 570 571
}

/**
572
 *	hibernate - The granpappy of the built-in hibernation management
L
Linus Torvalds 已提交
573 574
 */

575
int hibernate(void)
L
Linus Torvalds 已提交
576 577 578
{
	int error;

579
	mutex_lock(&pm_mutex);
580
	/* The snapshot device should not be opened while we're running */
581 582 583 584 585
	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
		error = -EBUSY;
		goto Unlock;
	}

586
	pm_prepare_console();
587 588 589
	error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
	if (error)
		goto Exit;
590

591 592 593 594
	error = usermodehelper_disable();
	if (error)
		goto Exit;

595 596 597 598 599
	/* Allocate memory management structures */
	error = create_basic_memory_bitmaps();
	if (error)
		goto Exit;

R
Rafael J. Wysocki 已提交
600
	printk(KERN_INFO "PM: Syncing filesystems ... ");
601 602 603
	sys_sync();
	printk("done.\n");

L
Linus Torvalds 已提交
604
	error = prepare_processes();
L
Li Shaohua 已提交
605
	if (error)
606
		goto Finish;
L
Linus Torvalds 已提交
607

608
	if (hibernation_test(TEST_FREEZER))
609
		goto Thaw;
610 611 612 613

	if (hibernation_testmode(HIBERNATION_TESTPROC))
		goto Thaw;

614
	error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
615 616 617 618
	if (error)
		goto Thaw;

	if (in_suspend) {
619 620 621 622
		unsigned int flags = 0;

		if (hibernation_mode == HIBERNATION_PLATFORM)
			flags |= SF_PLATFORM_MODE;
L
Linus Torvalds 已提交
623
		pr_debug("PM: writing image.\n");
624
		error = swsusp_write(flags);
625
		swsusp_free();
L
Linus Torvalds 已提交
626
		if (!error)
627
			power_down();
R
Rafael J. Wysocki 已提交
628
	} else {
L
Linus Torvalds 已提交
629
		pr_debug("PM: Image restored successfully.\n");
R
Rafael J. Wysocki 已提交
630
	}
631

R
Rafael J. Wysocki 已提交
632
 Thaw:
633
	thaw_processes();
634 635
 Finish:
	free_basic_memory_bitmaps();
636
	usermodehelper_enable();
637
 Exit:
638
	pm_notifier_call_chain(PM_POST_HIBERNATION);
639
	pm_restore_console();
640
	atomic_inc(&snapshot_device_available);
641 642
 Unlock:
	mutex_unlock(&pm_mutex);
L
Linus Torvalds 已提交
643 644 645 646 647 648 649 650 651 652
	return error;
}


/**
 *	software_resume - Resume from a saved image.
 *
 *	Called as a late_initcall (so all devices are discovered and
 *	initialized), we call swsusp to see if we have a saved image or not.
 *	If so, we quiesce devices, the restore the saved image. We will
653
 *	return above (in hibernate() ) if everything goes well.
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661
 *	Otherwise, we fail gracefully and return to the normally
 *	scheduled program.
 *
 */

static int software_resume(void)
{
	int error;
662
	unsigned int flags;
L
Linus Torvalds 已提交
663

664 665 666 667 668 669
	/*
	 * If the user said "noresume".. bail out early.
	 */
	if (noresume)
		return 0;

J
Johannes Berg 已提交
670 671 672 673 674 675 676 677 678 679 680
	/*
	 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
	 * is configured into the kernel. Since the regular hibernate
	 * trigger path is via sysfs which takes a buffer mutex before
	 * calling hibernate functions (which take pm_mutex) this can
	 * cause lockdep to complain about a possible ABBA deadlock
	 * which cannot happen since we're in the boot code here and
	 * sysfs can't be invoked yet. Therefore, we use a subclass
	 * here to avoid lockdep complaining.
	 */
	mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
681 682 683 684 685 686 687 688 689 690 691 692 693

	if (swsusp_resume_device)
		goto Check_image;

	if (!strlen(resume_file)) {
		error = -ENOENT;
		goto Unlock;
	}

	pr_debug("PM: Checking image partition %s\n", resume_file);

	/* Check if the device is there */
	swsusp_resume_device = name_to_dev_t(resume_file);
694
	if (!swsusp_resume_device) {
695 696 697 698 699
		/*
		 * Some device discovery might still be in progress; we need
		 * to wait for this to finish.
		 */
		wait_for_device_probe();
700 701 702 703 704 705 706
		/*
		 * We can't depend on SCSI devices being available after loading
		 * one of their modules until scsi_complete_async_scans() is
		 * called and the resume device usually is a SCSI one.
		 */
		scsi_complete_async_scans();

707
		swsusp_resume_device = name_to_dev_t(resume_file);
708 709 710 711
		if (!swsusp_resume_device) {
			error = -ENODEV;
			goto Unlock;
		}
712 713
	}

714 715 716
 Check_image:
	pr_debug("PM: Resume from partition %d:%d\n",
		MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
L
Linus Torvalds 已提交
717

R
Rafael J. Wysocki 已提交
718
	pr_debug("PM: Checking hibernation image.\n");
719 720
	error = swsusp_check();
	if (error)
721
		goto Unlock;
L
Linus Torvalds 已提交
722

723 724 725
	/* The snapshot device should not be opened while we're running */
	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
		error = -EBUSY;
J
Jiri Slaby 已提交
726
		swsusp_close(FMODE_READ);
727 728 729
		goto Unlock;
	}

730
	pm_prepare_console();
731 732
	error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
	if (error)
J
Jiri Slaby 已提交
733
		goto close_finish;
734

735 736
	error = usermodehelper_disable();
	if (error)
J
Jiri Slaby 已提交
737
		goto close_finish;
738

739 740
	error = create_basic_memory_bitmaps();
	if (error)
J
Jiri Slaby 已提交
741
		goto close_finish;
L
Linus Torvalds 已提交
742

743
	pr_debug("PM: Preparing processes for restore.\n");
744 745
	error = prepare_processes();
	if (error) {
746
		swsusp_close(FMODE_READ);
L
Li Shaohua 已提交
747
		goto Done;
L
Linus Torvalds 已提交
748 749
	}

R
Rafael J. Wysocki 已提交
750
	pr_debug("PM: Reading hibernation image.\n");
L
Linus Torvalds 已提交
751

752
	error = swsusp_read(&flags);
J
Jiri Slaby 已提交
753
	swsusp_close(FMODE_READ);
754
	if (!error)
755
		hibernation_restore(flags & SF_PLATFORM_MODE);
L
Linus Torvalds 已提交
756

757
	printk(KERN_ERR "PM: Restore failed, recovering.\n");
758
	swsusp_free();
759
	thaw_processes();
L
Linus Torvalds 已提交
760
 Done:
761
	free_basic_memory_bitmaps();
762
	usermodehelper_enable();
763
 Finish:
764
	pm_notifier_call_chain(PM_POST_RESTORE);
765
	pm_restore_console();
766
	atomic_inc(&snapshot_device_available);
767
	/* For success case, the suspend path will release the lock */
768
 Unlock:
769
	mutex_unlock(&pm_mutex);
L
Linus Torvalds 已提交
770
	pr_debug("PM: Resume from disk failed.\n");
771
	return error;
J
Jiri Slaby 已提交
772 773 774
close_finish:
	swsusp_close(FMODE_READ);
	goto Finish;
L
Linus Torvalds 已提交
775 776 777 778 779
}

late_initcall(software_resume);


780 781 782 783 784 785
static const char * const hibernation_modes[] = {
	[HIBERNATION_PLATFORM]	= "platform",
	[HIBERNATION_SHUTDOWN]	= "shutdown",
	[HIBERNATION_REBOOT]	= "reboot",
	[HIBERNATION_TEST]	= "test",
	[HIBERNATION_TESTPROC]	= "testproc",
L
Linus Torvalds 已提交
786 787 788
};

/**
789
 *	disk - Control hibernation mode
L
Linus Torvalds 已提交
790
 *
791 792
 *	Suspend-to-disk can be handled in several ways. We have a few options
 *	for putting the system to sleep - using the platform driver (e.g. ACPI
793 794
 *	or other hibernation_ops), powering off the system or rebooting the
 *	system (for testing) as well as the two test modes.
L
Linus Torvalds 已提交
795
 *
796
 *	The system can support 'platform', and that is known a priori (and
797 798 799
 *	encoded by the presence of hibernation_ops). However, the user may
 *	choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
 *	test modes, 'test' or 'testproc'.
L
Linus Torvalds 已提交
800 801 802 803 804 805 806
 *
 *	show() will display what the mode is currently set to.
 *	store() will accept one of
 *
 *	'platform'
 *	'shutdown'
 *	'reboot'
807 808
 *	'test'
 *	'testproc'
L
Linus Torvalds 已提交
809
 *
810
 *	It will only change to 'platform' if the system
811
 *	supports it (as determined by having hibernation_ops).
L
Linus Torvalds 已提交
812 813
 */

814 815
static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
			 char *buf)
L
Linus Torvalds 已提交
816
{
817 818 819
	int i;
	char *start = buf;

820 821
	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
		if (!hibernation_modes[i])
822 823
			continue;
		switch (i) {
824 825 826 827
		case HIBERNATION_SHUTDOWN:
		case HIBERNATION_REBOOT:
		case HIBERNATION_TEST:
		case HIBERNATION_TESTPROC:
828
			break;
829 830
		case HIBERNATION_PLATFORM:
			if (hibernation_ops)
831 832 833 834
				break;
			/* not a valid mode, continue with loop */
			continue;
		}
835 836
		if (i == hibernation_mode)
			buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
837
		else
838
			buf += sprintf(buf, "%s ", hibernation_modes[i]);
839 840 841
	}
	buf += sprintf(buf, "\n");
	return buf-start;
L
Linus Torvalds 已提交
842 843 844
}


845 846
static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
			  const char *buf, size_t n)
L
Linus Torvalds 已提交
847 848 849 850 851
{
	int error = 0;
	int i;
	int len;
	char *p;
852
	int mode = HIBERNATION_INVALID;
L
Linus Torvalds 已提交
853 854 855 856

	p = memchr(buf, '\n', n);
	len = p ? p - buf : n;

857
	mutex_lock(&pm_mutex);
858
	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
R
Rafael J. Wysocki 已提交
859 860
		if (len == strlen(hibernation_modes[i])
		    && !strncmp(buf, hibernation_modes[i], len)) {
L
Linus Torvalds 已提交
861 862 863 864
			mode = i;
			break;
		}
	}
865
	if (mode != HIBERNATION_INVALID) {
866
		switch (mode) {
867 868 869 870 871
		case HIBERNATION_SHUTDOWN:
		case HIBERNATION_REBOOT:
		case HIBERNATION_TEST:
		case HIBERNATION_TESTPROC:
			hibernation_mode = mode;
872
			break;
873 874 875
		case HIBERNATION_PLATFORM:
			if (hibernation_ops)
				hibernation_mode = mode;
L
Linus Torvalds 已提交
876 877 878
			else
				error = -EINVAL;
		}
879
	} else
L
Linus Torvalds 已提交
880 881
		error = -EINVAL;

882
	if (!error)
R
Rafael J. Wysocki 已提交
883
		pr_debug("PM: Hibernation mode set to '%s'\n",
884
			 hibernation_modes[mode]);
885
	mutex_unlock(&pm_mutex);
L
Linus Torvalds 已提交
886 887 888 889 890
	return error ? error : n;
}

power_attr(disk);

891 892
static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
			   char *buf)
L
Linus Torvalds 已提交
893 894 895 896 897
{
	return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
		       MINOR(swsusp_resume_device));
}

898 899
static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
			    const char *buf, size_t n)
L
Linus Torvalds 已提交
900 901 902
{
	unsigned int maj, min;
	dev_t res;
903
	int ret = -EINVAL;
L
Linus Torvalds 已提交
904

905 906
	if (sscanf(buf, "%u:%u", &maj, &min) != 2)
		goto out;
L
Linus Torvalds 已提交
907

908 909 910
	res = MKDEV(maj,min);
	if (maj != MAJOR(res) || min != MINOR(res))
		goto out;
L
Linus Torvalds 已提交
911

912
	mutex_lock(&pm_mutex);
913
	swsusp_resume_device = res;
914
	mutex_unlock(&pm_mutex);
R
Rafael J. Wysocki 已提交
915
	printk(KERN_INFO "PM: Starting manual resume from disk\n");
916 917 918
	noresume = 0;
	software_resume();
	ret = n;
R
Rafael J. Wysocki 已提交
919
 out:
920
	return ret;
L
Linus Torvalds 已提交
921 922 923 924
}

power_attr(resume);

925 926
static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
			       char *buf)
927
{
928
	return sprintf(buf, "%lu\n", image_size);
929 930
}

931 932
static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
				const char *buf, size_t n)
933
{
934
	unsigned long size;
935

936
	if (sscanf(buf, "%lu", &size) == 1) {
937 938 939 940 941 942 943 944 945
		image_size = size;
		return n;
	}

	return -EINVAL;
}

power_attr(image_size);

L
Linus Torvalds 已提交
946 947 948
static struct attribute * g[] = {
	&disk_attr.attr,
	&resume_attr.attr,
949
	&image_size_attr.attr,
L
Linus Torvalds 已提交
950 951 952 953 954 955 956 957 958 959 960
	NULL,
};


static struct attribute_group attr_group = {
	.attrs = g,
};


static int __init pm_disk_init(void)
{
961
	return sysfs_create_group(power_kobj, &attr_group);
L
Linus Torvalds 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975
}

core_initcall(pm_disk_init);


static int __init resume_setup(char *str)
{
	if (noresume)
		return 1;

	strncpy( resume_file, str, 255 );
	return 1;
}

976 977 978 979 980 981 982 983 984 985 986 987 988
static int __init resume_offset_setup(char *str)
{
	unsigned long long offset;

	if (noresume)
		return 1;

	if (sscanf(str, "%llu", &offset) == 1)
		swsusp_resume_block = offset;

	return 1;
}

L
Linus Torvalds 已提交
989 990 991 992 993 994 995
static int __init noresume_setup(char *str)
{
	noresume = 1;
	return 1;
}

__setup("noresume", noresume_setup);
996
__setup("resume_offset=", resume_offset_setup);
L
Linus Torvalds 已提交
997
__setup("resume=", resume_setup);