hibernate.c 27.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
Linus Torvalds's avatar
Linus Torvalds committed
3 4 5
 *
 * Copyright (c) 2003 Patrick Mochel
 * Copyright (c) 2003 Open Source Development Lab
Pavel Machek's avatar
Pavel Machek committed
6
 * Copyright (c) 2004 Pavel Machek <pavel@ucw.cz>
7
 * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
8
 * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12
 *
 * This file is released under the GPLv2.
 */

13 14
#define pr_fmt(fmt) "PM: " fmt

15
#include <linux/export.h>
Linus Torvalds's avatar
Linus Torvalds committed
16 17 18 19 20
#include <linux/suspend.h>
#include <linux/syscalls.h>
#include <linux/reboot.h>
#include <linux/string.h>
#include <linux/device.h>
21
#include <linux/async.h>
Linus Torvalds's avatar
Linus Torvalds committed
22 23
#include <linux/delay.h>
#include <linux/fs.h>
24
#include <linux/mount.h>
25
#include <linux/pm.h>
26
#include <linux/nmi.h>
27
#include <linux/console.h>
28
#include <linux/cpu.h>
29
#include <linux/freezer.h>
30
#include <linux/gfp.h>
31
#include <linux/syscore_ops.h>
32 33
#include <linux/ctype.h>
#include <linux/genhd.h>
34
#include <linux/ktime.h>
35
#include <trace/events/power.h>
36

Linus Torvalds's avatar
Linus Torvalds committed
37 38 39
#include "power.h"


40 41
static int nocompress;
static int noresume;
42
static int nohibernate;
43
static int resume_wait;
44
static unsigned int resume_delay;
45
static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds's avatar
Linus Torvalds committed
46
dev_t swsusp_resume_device;
47
sector_t swsusp_resume_block;
48
__visible int in_suspend __nosavedata;
Linus Torvalds's avatar
Linus Torvalds committed
49

50 51 52 53 54
enum {
	HIBERNATION_INVALID,
	HIBERNATION_PLATFORM,
	HIBERNATION_SHUTDOWN,
	HIBERNATION_REBOOT,
55 56 57
#ifdef CONFIG_SUSPEND
	HIBERNATION_SUSPEND,
#endif
58
	HIBERNATION_TEST_RESUME,
59 60 61 62 63 64 65 66
	/* keep last */
	__HIBERNATION_AFTER_LAST
};
#define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
#define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)

static int hibernation_mode = HIBERNATION_SHUTDOWN;

67
bool freezer_test_done;
68

69
static const struct platform_hibernation_ops *hibernation_ops;
70

71 72 73 74 75
bool hibernation_available(void)
{
	return (nohibernate == 0);
}

76
/**
77 78
 * hibernation_set_ops - Set the global hibernate operations.
 * @ops: Hibernation operations to use in subsequent hibernation transitions.
79
 */
80
void hibernation_set_ops(const struct platform_hibernation_ops *ops)
81
{
82 83
	if (ops && !(ops->begin && ops->end &&  ops->pre_snapshot
	    && ops->prepare && ops->finish && ops->enter && ops->pre_restore
84
	    && ops->restore_cleanup && ops->leave)) {
85 86 87
		WARN_ON(1);
		return;
	}
88
	lock_system_sleep();
89 90 91 92 93 94
	hibernation_ops = ops;
	if (ops)
		hibernation_mode = HIBERNATION_PLATFORM;
	else if (hibernation_mode == HIBERNATION_PLATFORM)
		hibernation_mode = HIBERNATION_SHUTDOWN;

95
	unlock_system_sleep();
96
}
97
EXPORT_SYMBOL_GPL(hibernation_set_ops);
98

99 100 101 102 103 104 105 106
static bool entering_platform_hibernation;

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

107 108 109
#ifdef CONFIG_PM_DEBUG
static void hibernation_debug_sleep(void)
{
110
	pr_info("hibernation debug: Waiting for 5 seconds.\n");
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	mdelay(5000);
}

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_test(int level) { return 0; }
#endif /* !CONFIG_PM_DEBUG */

126
/**
127 128
 * platform_begin - Call platform to start hibernation.
 * @platform_mode: Whether or not to use the platform driver.
129
 */
130
static int platform_begin(int platform_mode)
131 132
{
	return (platform_mode && hibernation_ops) ?
133 134 135 136
		hibernation_ops->begin() : 0;
}

/**
137 138
 * platform_end - Call platform to finish transition to the working state.
 * @platform_mode: Whether or not to use the platform driver.
139 140 141 142 143
 */
static void platform_end(int platform_mode)
{
	if (platform_mode && hibernation_ops)
		hibernation_ops->end();
144
}
145

146
/**
147 148 149 150 151
 * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
 * @platform_mode: Whether or not to use the platform driver.
 *
 * Use the platform driver to prepare the system for creating a hibernate image,
 * if so configured, and return an error code if that fails.
152 153
 */

154
static int platform_pre_snapshot(int platform_mode)
155
{
156
	return (platform_mode && hibernation_ops) ?
157
		hibernation_ops->pre_snapshot() : 0;
158
}
159

160
/**
161 162 163 164 165 166 167
 * platform_leave - Call platform to prepare a transition to the working state.
 * @platform_mode: Whether or not to use the platform driver.
 *
 * Use the platform driver prepare to prepare the machine for switching to the
 * normal mode of operation.
 *
 * This routine is called on one CPU with interrupts disabled.
168 169 170 171 172 173 174
 */
static void platform_leave(int platform_mode)
{
	if (platform_mode && hibernation_ops)
		hibernation_ops->leave();
}

175
/**
176 177 178 179 180 181 182
 * platform_finish - Call platform to switch the system to the working state.
 * @platform_mode: Whether or not to use the platform driver.
 *
 * Use the platform driver to switch the machine to the normal mode of
 * operation.
 *
 * This routine must be called after platform_prepare().
183
 */
184
static void platform_finish(int platform_mode)
185
{
186
	if (platform_mode && hibernation_ops)
187
		hibernation_ops->finish();
188 189
}

190
/**
191 192 193 194 195 196 197 198
 * platform_pre_restore - Prepare for hibernate image restoration.
 * @platform_mode: Whether or not to use the platform driver.
 *
 * Use the platform driver to prepare the system for resume from a hibernation
 * image.
 *
 * If the restore fails after this function has been called,
 * platform_restore_cleanup() must be called.
199 200 201 202 203 204 205 206
 */
static int platform_pre_restore(int platform_mode)
{
	return (platform_mode && hibernation_ops) ?
		hibernation_ops->pre_restore() : 0;
}

/**
207 208 209 210 211 212 213 214 215
 * platform_restore_cleanup - Switch to the working state after failing restore.
 * @platform_mode: Whether or not to use the platform driver.
 *
 * Use the platform driver to switch the system 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().
216 217 218 219 220 221 222
 */
static void platform_restore_cleanup(int platform_mode)
{
	if (platform_mode && hibernation_ops)
		hibernation_ops->restore_cleanup();
}

223
/**
224 225
 * platform_recover - Recover from a failure to suspend devices.
 * @platform_mode: Whether or not to use the platform driver.
226 227 228 229 230 231 232
 */
static void platform_recover(int platform_mode)
{
	if (platform_mode && hibernation_ops && hibernation_ops->recover)
		hibernation_ops->recover();
}

233
/**
234 235 236 237 238
 * swsusp_show_speed - Print time elapsed between two events during hibernation.
 * @start: Starting event.
 * @stop: Final event.
 * @nr_pages: Number of memory pages processed between @start and @stop.
 * @msg: Additional diagnostic message to print.
239
 */
240 241
void swsusp_show_speed(ktime_t start, ktime_t stop,
		      unsigned nr_pages, char *msg)
242
{
243
	ktime_t diff;
244 245 246 247
	u64 elapsed_centisecs64;
	unsigned int centisecs;
	unsigned int k;
	unsigned int kps;
248

249 250
	diff = ktime_sub(stop, start);
	elapsed_centisecs64 = ktime_divns(diff, 10*NSEC_PER_MSEC);
251 252 253 254 255
	centisecs = elapsed_centisecs64;
	if (centisecs == 0)
		centisecs = 1;	/* avoid div-by-zero */
	k = nr_pages * (PAGE_SIZE / 1024);
	kps = (k * 100) / centisecs;
256 257 258
	pr_info("%s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
		msg, k, centisecs / 100, centisecs % 100, kps / 1000,
		(kps % 1000) / 10);
259 260
}

261
/**
262 263 264
 * create_image - Create a hibernation image.
 * @platform_mode: Whether or not to use the platform driver.
 *
265 266
 * Execute device drivers' "late" and "noirq" freeze callbacks, create a
 * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
267 268
 *
 * Control reappears in this routine after the subsequent restore.
269
 */
270
static int create_image(int platform_mode)
271 272 273
{
	int error;

274
	error = dpm_suspend_end(PMSG_FREEZE);
275
	if (error) {
276
		pr_err("Some devices failed to power down, aborting hibernation\n");
277
		return error;
278
	}
279

280 281 282 283 284
	error = platform_pre_snapshot(platform_mode);
	if (error || hibernation_test(TEST_PLATFORM))
		goto Platform_finish;

	error = disable_nonboot_cpus();
285
	if (error || hibernation_test(TEST_CPUS))
286 287
		goto Enable_cpus;

288
	local_irq_disable();
289
	hard_cond_local_irq_disable();
290

291
	error = syscore_suspend();
292
	if (error) {
293
		pr_err("Some system devices failed to power down, aborting hibernation\n");
294
		goto Enable_irqs;
295
	}
296

297
	if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
298 299 300
		goto Power_up;

	in_suspend = 1;
301
	save_processor_state();
302
	trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
303
	error = swsusp_arch_suspend();
304 305
	/* Restore control flow magically appears here */
	restore_processor_state();
306
	trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
307
	if (error)
308 309
		pr_err("Error %d creating hibernation image\n", error);

310
	if (!in_suspend) {
311
		events_check_enabled = false;
312 313
		clear_free_pages();
	}
314 315

	platform_leave(platform_mode);
316

317
 Power_up:
318
	syscore_resume();
319

320
 Enable_irqs:
321 322
	local_irq_enable();

323 324 325 326 327 328
 Enable_cpus:
	enable_nonboot_cpus();

 Platform_finish:
	platform_finish(platform_mode);

329
	dpm_resume_start(in_suspend ?
330
		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
331

332 333 334
	return error;
}

335
/**
336 337
 * hibernation_snapshot - Quiesce devices and create a hibernation image.
 * @platform_mode: If set, use platform driver to prepare for the transition.
338
 *
339
 * This routine must be called with pm_mutex held.
340 341 342
 */
int hibernation_snapshot(int platform_mode)
{
343
	pm_message_t msg;
344
	int error;
345

346
	pm_suspend_clear_flags();
347
	error = platform_begin(platform_mode);
348
	if (error)
349
		goto Close;
350

351 352
	/* Preallocate image memory before shutting down devices. */
	error = hibernate_preallocate_memory();
353 354 355 356 357
	if (error)
		goto Close;

	error = freeze_kernel_threads();
	if (error)
358
		goto Cleanup;
359

360
	if (hibernation_test(TEST_FREEZER)) {
361 362 363 364 365 366

		/*
		 * Indicate to the caller that we are returning due to a
		 * successful freezer test.
		 */
		freezer_test_done = true;
367
		goto Thaw;
368 369
	}

370
	error = dpm_prepare(PMSG_FREEZE);
371
	if (error) {
372
		dpm_complete(PMSG_RECOVER);
373
		goto Thaw;
374
	}
375

376
	suspend_console();
377
	pm_restrict_gfp_mask();
378

379
	error = dpm_suspend(PMSG_FREEZE);
380

381 382 383 384
	if (error || hibernation_test(TEST_DEVICES))
		platform_recover(platform_mode);
	else
		error = create_image(platform_mode);
385

386
	/*
387 388
	 * In the case that we call create_image() above, the control
	 * returns here (1) after the image has been created or the
389 390
	 * image creation has failed and (2) after a successful restore.
	 */
391

392 393 394 395
	/* We may need to release the preallocated image pages here. */
	if (error || !in_suspend)
		swsusp_free();

396 397
	msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
	dpm_resume(msg);
398 399 400 401

	if (error || !in_suspend)
		pm_restore_gfp_mask();

402
	resume_console();
403 404
	dpm_complete(msg);

405 406
 Close:
	platform_end(platform_mode);
407
	return error;
408

409 410
 Thaw:
	thaw_kernel_threads();
411 412 413
 Cleanup:
	swsusp_free();
	goto Close;
414 415
}

416 417 418 419 420
int __weak hibernate_resume_nonboot_cpu_disable(void)
{
	return disable_nonboot_cpus();
}

421
/**
422 423 424
 * resume_target_kernel - Restore system state from a hibernation image.
 * @platform_mode: Whether or not to use the platform driver.
 *
425 426 427 428
 * Execute device drivers' "noirq" and "late" freeze callbacks, 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.
429
 */
430
static int resume_target_kernel(bool platform_mode)
431 432 433
{
	int error;

434
	error = dpm_suspend_end(PMSG_QUIESCE);
435
	if (error) {
436
		pr_err("Some devices failed to power down, aborting resume\n");
437
		return error;
438
	}
439

440 441 442 443
	error = platform_pre_restore(platform_mode);
	if (error)
		goto Cleanup;

444
	error = hibernate_resume_nonboot_cpu_disable();
445 446 447
	if (error)
		goto Enable_cpus;

448
	local_irq_disable();
449
	hard_cond_local_irq_disable();
450

451
	error = syscore_suspend();
452 453 454
	if (error)
		goto Enable_irqs;

455 456 457 458 459 460
	save_processor_state();
	error = restore_highmem();
	if (!error) {
		error = swsusp_arch_resume();
		/*
		 * The code below is only ever reached in case of a failure.
461 462
		 * Otherwise, execution continues at the place where
		 * swsusp_arch_suspend() was called.
463 464
		 */
		BUG_ON(!error);
465 466 467 468
		/*
		 * This call to restore_highmem() reverts the changes made by
		 * the previous one.
		 */
469 470 471 472 473
		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
474
	 * subsequent failures.
475 476 477 478
	 */
	swsusp_free();
	restore_processor_state();
	touch_softlockup_watchdog();
479

480
	syscore_resume();
481

482
 Enable_irqs:
483
	local_irq_enable();
484

485 486 487 488 489 490
 Enable_cpus:
	enable_nonboot_cpus();

 Cleanup:
	platform_restore_cleanup(platform_mode);

491
	dpm_resume_start(PMSG_RECOVER);
492

493 494 495
	return error;
}

496
/**
497 498
 * hibernation_restore - Quiesce devices and restore from a hibernation image.
 * @platform_mode: If set, use platform driver to prepare for the transition.
499
 *
500
 * This routine must be called with pm_mutex held.  If it is successful, control
501
 * reappears in the restored target kernel in hibernation_snapshot().
502
 */
503
int hibernation_restore(int platform_mode)
504
{
505
	int error;
506 507 508

	pm_prepare_console();
	suspend_console();
509
	pm_restrict_gfp_mask();
510
	error = dpm_suspend_start(PMSG_QUIESCE);
511
	if (!error) {
512
		error = resume_target_kernel(platform_mode);
513 514 515 516 517 518
		/*
		 * The above should either succeed and jump to the new kernel,
		 * or return with an error. Otherwise things are just
		 * undefined, so let's be paranoid.
		 */
		BUG_ON(!error);
519
	}
520
	dpm_resume_end(PMSG_RECOVER);
521
	pm_restore_gfp_mask();
522 523 524 525 526 527
	resume_console();
	pm_restore_console();
	return error;
}

/**
528
 * hibernation_platform_enter - Power off the system using the platform driver.
529 530 531
 */
int hibernation_platform_enter(void)
{
532
	int error;
533

534 535 536 537 538 539 540 541
	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
	 */
542
	error = hibernation_ops->begin();
543
	if (error)
544
		goto Close;
545

546
	entering_platform_hibernation = true;
547
	suspend_console();
548
	error = dpm_suspend_start(PMSG_HIBERNATE);
549 550 551 552 553
	if (error) {
		if (hibernation_ops->recover)
			hibernation_ops->recover();
		goto Resume_devices;
	}
554

555
	error = dpm_suspend_end(PMSG_HIBERNATE);
556
	if (error)
557
		goto Resume_devices;
558

559 560
	error = hibernation_ops->prepare();
	if (error)
561
		goto Platform_finish;
562 563 564

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

567
	local_irq_disable();
568
	hard_cond_local_irq_disable();
569
	syscore_suspend();
570
	if (pm_wakeup_pending()) {
571 572 573 574
		error = -EAGAIN;
		goto Power_up;
	}

575 576 577
	hibernation_ops->enter();
	/* We should never get here */
	while (1);
578

579
 Power_up:
580
	syscore_resume();
581
	local_irq_enable();
582 583

 Enable_cpus:
584 585
	enable_nonboot_cpus();

586
 Platform_finish:
587
	hibernation_ops->finish();
588

589
	dpm_resume_start(PMSG_RESTORE);
590

591
 Resume_devices:
592
	entering_platform_hibernation = false;
593
	dpm_resume_end(PMSG_RESTORE);
594
	resume_console();
595

596 597
 Close:
	hibernation_ops->end();
598

599
	return error;
600 601
}

Linus Torvalds's avatar
Linus Torvalds committed
602
/**
603
 * power_down - Shut the machine down for hibernation.
Linus Torvalds's avatar
Linus Torvalds committed
604
 *
605 606 607
 * Use the platform driver, if configured, to put the system into the sleep
 * state corresponding to hibernation, or try to power it off or reboot,
 * depending on the value of hibernation_mode.
Linus Torvalds's avatar
Linus Torvalds committed
608
 */
609
static void power_down(void)
Linus Torvalds's avatar
Linus Torvalds committed
610
{
611 612
#ifdef CONFIG_SUSPEND
	int error;
613 614 615 616 617 618 619 620 621 622 623

	if (hibernation_mode == HIBERNATION_SUSPEND) {
		error = suspend_devices_and_enter(PM_SUSPEND_MEM);
		if (error) {
			hibernation_mode = hibernation_ops ?
						HIBERNATION_PLATFORM :
						HIBERNATION_SHUTDOWN;
		} else {
			/* Restore swap signature. */
			error = swsusp_unmark();
			if (error)
624
				pr_err("Swap will be unusable! Try swapon -a.\n");
625 626 627 628

			return;
		}
	}
629 630
#endif

631 632
	switch (hibernation_mode) {
	case HIBERNATION_REBOOT:
633
		kernel_restart(NULL);
Linus Torvalds's avatar
Linus Torvalds committed
634
		break;
635
	case HIBERNATION_PLATFORM:
636
		hibernation_platform_enter();
637
	case HIBERNATION_SHUTDOWN:
638 639
		if (pm_power_off)
			kernel_power_off();
640
		break;
Linus Torvalds's avatar
Linus Torvalds committed
641
	}
642
	kernel_halt();
643 644 645 646
	/*
	 * Valid image is on the disk, if we continue we risk serious data
	 * corruption after resume.
	 */
647
	pr_crit("Power down manually\n");
648 649
	while (1)
		cpu_relax();
Linus Torvalds's avatar
Linus Torvalds committed
650 651
}

652 653 654 655 656
static int load_image_and_restore(void)
{
	int error;
	unsigned int flags;

657
	pm_pr_dbg("Loading hibernation image.\n");
658 659 660 661 662 663 664 665 666 667 668

	lock_device_hotplug();
	error = create_basic_memory_bitmaps();
	if (error)
		goto Unlock;

	error = swsusp_read(&flags);
	swsusp_close(FMODE_READ);
	if (!error)
		hibernation_restore(flags & SF_PLATFORM_MODE);

669
	pr_err("Failed to load hibernation image, recovering.\n");
670 671 672 673 674 675 676 677
	swsusp_free();
	free_basic_memory_bitmaps();
 Unlock:
	unlock_device_hotplug();

	return error;
}

Linus Torvalds's avatar
Linus Torvalds committed
678
/**
679
 * hibernate - Carry out system hibernation, including saving the image.
Linus Torvalds's avatar
Linus Torvalds committed
680
 */
681
int hibernate(void)
Linus Torvalds's avatar
Linus Torvalds committed
682
{
683
	int error, nr_calls = 0;
684
	bool snapshot_test = false;
Linus Torvalds's avatar
Linus Torvalds committed
685

686
	if (!hibernation_available()) {
687
		pm_pr_dbg("Hibernation not available.\n");
688 689 690
		return -EPERM;
	}

691
	lock_system_sleep();
692
	/* The snapshot device should not be opened while we're running */
693 694 695 696 697
	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
		error = -EBUSY;
		goto Unlock;
	}

698
	pr_info("hibernation entry\n");
699
	pm_prepare_console();
700 701 702
	error = __pm_notifier_call_chain(PM_HIBERNATION_PREPARE, -1, &nr_calls);
	if (error) {
		nr_calls--;
703
		goto Exit;
704
	}
705

706
	pr_info("Syncing filesystems ... \n");
707
	sys_sync();
708
	pr_info("done.\n");
709

710
	error = freeze_processes();
711
	if (error)
712 713
		goto Exit;

714
	lock_device_hotplug();
715 716 717 718
	/* Allocate memory management structures */
	error = create_basic_memory_bitmaps();
	if (error)
		goto Thaw;
Linus Torvalds's avatar
Linus Torvalds committed
719

720
	error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
721
	if (error || freezer_test_done)
722
		goto Free_bitmaps;
723 724

	if (in_suspend) {
725 726 727 728
		unsigned int flags = 0;

		if (hibernation_mode == HIBERNATION_PLATFORM)
			flags |= SF_PLATFORM_MODE;
729 730
		if (nocompress)
			flags |= SF_NOCOMPRESS_MODE;
731 732 733
		else
		        flags |= SF_CRC32_MODE;

734
		pm_pr_dbg("Writing image.\n");
735
		error = swsusp_write(flags);
736
		swsusp_free();
737 738 739 740 741 742
		if (!error) {
			if (hibernation_mode == HIBERNATION_TEST_RESUME)
				snapshot_test = true;
			else
				power_down();
		}
743
		in_suspend = 0;
744
		pm_restore_gfp_mask();
745
	} else {
746
		pm_pr_dbg("Image restored successfully.\n");
747
	}
748

749 750
 Free_bitmaps:
	free_basic_memory_bitmaps();
751
 Thaw:
752
	unlock_device_hotplug();
753
	if (snapshot_test) {
754
		pm_pr_dbg("Checking hibernation image\n");
755 756 757 758
		error = swsusp_check();
		if (!error)
			error = load_image_and_restore();
	}
759
	thaw_processes();
760 761 762

	/* Don't bother checking whether freezer_test_done is true */
	freezer_test_done = false;
763
 Exit:
764
	__pm_notifier_call_chain(PM_POST_HIBERNATION, nr_calls, NULL);
765
	pm_restore_console();
766
	atomic_inc(&snapshot_device_available);
767
 Unlock:
768
	unlock_system_sleep();
769 770
	pr_info("hibernation exit\n");

Linus Torvalds's avatar
Linus Torvalds committed
771 772 773 774 775
	return error;
}


/**
776 777 778 779
 * software_resume - Resume from a saved hibernation image.
 *
 * This routine is called as a late initcall, when all devices have been
 * discovered and initialized already.
Linus Torvalds's avatar
Linus Torvalds committed
780
 *
781 782 783
 * The image reading code is called to see if there is a hibernation image
 * available for reading.  If that is the case, devices are quiesced and the
 * contents of memory is restored from the saved image.
Linus Torvalds's avatar
Linus Torvalds committed
784
 *
785
 * If this is successful, control reappears in the restored target kernel in
786
 * hibernation_snapshot() which returns to hibernate().  Otherwise, the routine
787 788
 * attempts to recover gracefully and make the kernel return to the normal mode
 * of operation.
Linus Torvalds's avatar
Linus Torvalds committed
789 790 791
 */
static int software_resume(void)
{
792
	int error, nr_calls = 0;
Linus Torvalds's avatar
Linus Torvalds committed
793

794 795 796
	/*
	 * If the user said "noresume".. bail out early.
	 */
797
	if (noresume || !hibernation_available())
798 799
		return 0;

800 801 802 803 804 805 806 807 808 809 810
	/*
	 * 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);
811 812 813 814 815 816 817 818 819

	if (swsusp_resume_device)
		goto Check_image;

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

820
	pm_pr_dbg("Checking hibernation image partition %s\n", resume_file);
821

822
	if (resume_delay) {
823
		pr_info("Waiting %dsec before reading resume device ...\n",
824 825 826 827
			resume_delay);
		ssleep(resume_delay);
	}

828 829
	/* Check if the device is there */
	swsusp_resume_device = name_to_dev_t(resume_file);
830 831 832 833 834 835 836 837 838 839 840

	/*
	 * name_to_dev_t is ineffective to verify parition if resume_file is in
	 * integer format. (e.g. major:minor)
	 */
	if (isdigit(resume_file[0]) && resume_wait) {
		int partno;
		while (!get_gendisk(swsusp_resume_device, &partno))
			msleep(10);
	}

841
	if (!swsusp_resume_device) {
842 843 844 845 846
		/*
		 * Some device discovery might still be in progress; we need
		 * to wait for this to finish.
		 */
		wait_for_device_probe();
847 848 849 850 851 852 853

		if (resume_wait) {
			while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
				msleep(10);
			async_synchronize_full();
		}

854
		swsusp_resume_device = name_to_dev_t(resume_file);
855 856 857 858
		if (!swsusp_resume_device) {
			error = -ENODEV;
			goto Unlock;
		}
859 860
	}

861
 Check_image:
862
	pm_pr_dbg("Hibernation image partition %d:%d present\n",
863
		MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds's avatar
Linus Torvalds committed
864

865
	pm_pr_dbg("Looking for hibernation image.\n");
866 867
	error = swsusp_check();
	if (error)
868
		goto Unlock;
Linus Torvalds's avatar
Linus Torvalds committed
869

870 871 872
	/* The snapshot device should not be opened while we're running */
	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
		error = -EBUSY;
873
		swsusp_close(FMODE_READ);
874 875 876
		goto Unlock;
	}

877
	pr_info("resume from hibernation\n");
878
	pm_prepare_console();
879 880 881
	error = __pm_notifier_call_chain(PM_RESTORE_PREPARE, -1, &nr_calls);
	if (error) {
		nr_calls--;
882
		goto Close_Finish;
883
	}
884

885
	pm_pr_dbg("Preparing processes for restore.\n");
886
	error = freeze_processes();
887 888
	if (error)
		goto Close_Finish;
889
	error = load_image_and_restore();
890
	thaw_processes();
891
 Finish:
892
	__pm_notifier_call_chain(PM_POST_RESTORE, nr_calls, NULL);
893
	pm_restore_console();
894
	pr_info("resume from hibernation failed (%d)\n", error);
895
	atomic_inc(&snapshot_device_available);
896
	/* For success case, the suspend path will release the lock */
897
 Unlock:
898
	mutex_unlock(&pm_mutex);
899
	pm_pr_dbg("Hibernation image not present or could not be loaded.\n");
900
	return error;
901
 Close_Finish:
902 903
	swsusp_close(FMODE_READ);
	goto Finish;
Linus Torvalds's avatar
Linus Torvalds committed
904 905
}

906
late_initcall_sync(software_resume);
Linus Torvalds's avatar
Linus Torvalds committed
907 908


909 910 911 912
static const char * const hibernation_modes[] = {
	[HIBERNATION_PLATFORM]	= "platform",
	[HIBERNATION_SHUTDOWN]	= "shutdown",
	[HIBERNATION_REBOOT]	= "reboot",
913 914 915
#ifdef CONFIG_SUSPEND
	[HIBERNATION_SUSPEND]	= "suspend",
#endif
916
	[HIBERNATION_TEST_RESUME]	= "test_resume",
Linus Torvalds's avatar
Linus Torvalds committed
917 918
};

919 920
/*
 * /sys/power/disk - Control hibernation mode.
Linus Torvalds's avatar
Linus Torvalds committed
921
 *
922 923 924
 * Hibernation can be handled in several ways.  There are a few different ways
 * to put the system into the sleep state: using the platform driver (e.g. ACPI
 * or other hibernation_ops), powering it off or rebooting it (for testing
925
 * mostly).
Linus Torvalds's avatar
Linus Torvalds committed
926
 *
927 928
 * The sysfs file /sys/power/disk provides an interface for selecting the
 * hibernation mode to use.  Reading from this file causes the available modes
929
 * to be printed.  There are 3 modes that can be supported:
Linus Torvalds's avatar
Linus Torvalds committed
930 931 932 933 934
 *
 *	'platform'
 *	'shutdown'
 *	'reboot'
 *
935 936 937 938 939 940 941 942
 * If a platform hibernation driver is in use, 'platform' will be supported
 * and will be used by default.  Otherwise, 'shutdown' will be used by default.
 * The selected option (i.e. the one corresponding to the current value of
 * hibernation_mode) is enclosed by a square bracket.
 *
 * To select a given hibernation mode it is necessary to write the mode's
 * string representation (as returned by reading from /sys/power/disk) back
 * into /sys/power/disk.
Linus Torvalds's avatar
Linus Torvalds committed
943 944
 */

945 946
static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
			 char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
947
{
948 949 950
	int i;
	char *start = buf;

951 952 953
	if (!hibernation_available())
		return sprintf(buf, "[disabled]\n");

954 955
	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
		if (!hibernation_modes[i])
956 957
			continue;
		switch (i) {
958 959
		case HIBERNATION_SHUTDOWN:
		case HIBERNATION_REBOOT:
960 961 962
#ifdef CONFIG_SUSPEND
		case HIBERNATION_SUSPEND:
#endif
963
		case HIBERNATION_TEST_RESUME:
964
			break;
965 966
		case HIBERNATION_PLATFORM:
			if (hibernation_ops)
967 968 969 970
				break;
			/* not a valid mode, continue with loop */
			continue;
		}
971 972
		if (i == hibernation_mode)
			buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
973
		else
974
			buf += sprintf(buf, "%s ", hibernation_modes[i]);
975 976 977
	}
	buf += sprintf(buf, "\n");
	return buf-start;
Linus Torvalds's avatar
Linus Torvalds committed
978 979
}

980 981
static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
			  const char *buf, size_t n)
Linus Torvalds's avatar
Linus Torvalds committed
982 983 984 985 986
{
	int error = 0;
	int i;
	int len;
	char *p;
987
	int mode = HIBERNATION_INVALID;
Linus Torvalds's avatar
Linus Torvalds committed
988

989 990 991
	if (!hibernation_available())
		return -EPERM;

Linus Torvalds's avatar
Linus Torvalds committed
992 993 994
	p