disk.c 21.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * kernel/power/disk.c - Suspend-to-disk support.
 *
 * Copyright (c) 2003 Patrick Mochel
 * Copyright (c) 2003 Open Source Development Lab
 * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
 *
 * 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;
L
Linus Torvalds 已提交
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
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;

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

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

58
void hibernation_set_ops(struct platform_hibernation_ops *ops)
59
{
60 61
	if (ops && !(ops->begin && ops->end &&  ops->pre_snapshot
	    && ops->prepare && ops->finish && ops->enter && ops->pre_restore
62
	    && ops->restore_cleanup)) {
63 64 65 66 67 68 69 70 71 72 73 74 75
		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);
}

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

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

84 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
#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 */

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

118
static int platform_begin(int platform_mode)
119 120
{
	return (platform_mode && hibernation_ops) ?
121 122 123 124 125 126 127 128 129 130 131 132
		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();
133
}
134

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

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

146 147 148 149 150 151 152 153 154 155 156
/**
 *	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();
}

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

162
static void platform_finish(int platform_mode)
163
{
164
	if (platform_mode && hibernation_ops)
165
		hibernation_ops->finish();
166 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
/**
 *	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();
}

193 194 195 196 197 198 199 200 201 202 203
/**
 *	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();
}

204 205 206 207 208 209
/**
 *	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.
 */

210
static int create_image(int platform_mode)
211 212 213 214 215 216 217
{
	int error;

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

218
	device_pm_lock();
219

220 221 222 223 224 225 226 227
	/* At this point, device_suspend() has been called, but *not*
	 * device_power_down(). We *must* call device_power_down() now.
	 * 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.
	 */
	error = device_power_down(PMSG_FREEZE);
	if (error) {
R
Rafael J. Wysocki 已提交
228 229
		printk(KERN_ERR "PM: Some devices failed to power down, "
			"aborting hibernation\n");
230
		goto Unlock;
231
	}
232

233 234 235 236 237 238 239 240 241
	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;

242 243
	local_irq_disable();

244 245 246 247
	sysdev_suspend(PMSG_FREEZE);
	if (error) {
		printk(KERN_ERR "PM: Some devices failed to power down, "
			"aborting hibernation\n");
248
		goto Enable_irqs;
249
	}
250

251 252 253 254
	if (hibernation_test(TEST_CORE))
		goto Power_up;

	in_suspend = 1;
255 256 257
	save_processor_state();
	error = swsusp_arch_suspend();
	if (error)
R
Rafael J. Wysocki 已提交
258 259
		printk(KERN_ERR "PM: Error %d creating hibernation image\n",
			error);
260 261 262 263
	/* Restore control flow magically appears here */
	restore_processor_state();
	if (!in_suspend)
		platform_leave(platform_mode);
264

265
 Power_up:
266
	sysdev_resume();
267 268 269
	/* NOTE:  device_power_up() is just a resume() for devices
	 * that suspended with irqs off ... no overall powerup.
	 */
270

271
 Enable_irqs:
272 273
	local_irq_enable();

274 275 276 277 278 279
 Enable_cpus:
	enable_nonboot_cpus();

 Platform_finish:
	platform_finish(platform_mode);

280 281
	device_power_up(in_suspend ?
		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
282 283

 Unlock:
284
	device_pm_unlock();
285

286 287 288
	return error;
}

289 290 291 292
/**
 *	hibernation_snapshot - quiesce devices and create the hibernation
 *	snapshot image.
 *	@platform_mode - if set, use the platform driver, if available, to
293
 *			 prepare the platform firmware for the power transition.
294 295 296 297 298 299
 *
 *	Must be called with pm_mutex held
 */

int hibernation_snapshot(int platform_mode)
{
300
	int error;
301

302
	error = platform_begin(platform_mode);
303
	if (error)
304
		return error;
305

306 307
	/* Free memory before shutting down devices. */
	error = swsusp_shrink_memory();
308
	if (error)
309
		goto Close;
310

311 312
	suspend_console();
	error = device_suspend(PMSG_FREEZE);
313
	if (error)
314
		goto Recover_platform;
315

316
	if (hibernation_test(TEST_DEVICES))
317
		goto Recover_platform;
318

319 320
	error = create_image(platform_mode);
	/* Control returns here after successful restore */
321 322

 Resume_devices:
323 324
	device_resume(in_suspend ?
		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
325
	resume_console();
326 327
 Close:
	platform_end(platform_mode);
328
	return error;
329 330 331 332

 Recover_platform:
	platform_recover(platform_mode);
	goto Resume_devices;
333 334
}

335 336 337 338 339 340 341 342
/**
 *	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.
 */

343
static int resume_target_kernel(bool platform_mode)
344 345 346
{
	int error;

347
	device_pm_lock();
348

349
	error = device_power_down(PMSG_QUIESCE);
350
	if (error) {
R
Rafael J. Wysocki 已提交
351
		printk(KERN_ERR "PM: Some devices failed to power down, "
352
			"aborting resume\n");
353
		goto Unlock;
354
	}
355

356 357 358 359 360 361 362 363
	error = platform_pre_restore(platform_mode);
	if (error)
		goto Cleanup;

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

364 365
	local_irq_disable();

366 367 368 369
	error = sysdev_suspend(PMSG_QUIESCE);
	if (error)
		goto Enable_irqs;

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
	/* 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();
392

393
	sysdev_resume();
394

395
 Enable_irqs:
396
	local_irq_enable();
397

398 399 400 401 402 403
 Enable_cpus:
	enable_nonboot_cpus();

 Cleanup:
	platform_restore_cleanup(platform_mode);

404 405 406
	device_power_up(PMSG_RECOVER);

 Unlock:
407
	device_pm_unlock();
408

409 410 411
	return error;
}

412 413 414
/**
 *	hibernation_restore - quiesce devices and restore the hibernation
 *	snapshot image.  If successful, control returns in hibernation_snaphot()
415
 *	@platform_mode - if set, use the platform driver, if available, to
416
 *			 prepare the platform firmware for the transition.
417 418 419 420
 *
 *	Must be called with pm_mutex held
 */

421
int hibernation_restore(int platform_mode)
422
{
423
	int error;
424 425 426

	pm_prepare_console();
	suspend_console();
427
	error = device_suspend(PMSG_QUIESCE);
428
	if (!error) {
429 430
		error = resume_target_kernel(platform_mode);
		device_resume(PMSG_RECOVER);
431
	}
432 433 434 435 436 437 438 439 440 441 442 443
	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)
{
444
	int error;
445

446 447 448 449 450 451 452 453
	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
	 */
454
	error = hibernation_ops->begin();
455
	if (error)
456
		goto Close;
457

458
	entering_platform_hibernation = true;
459
	suspend_console();
460
	error = device_suspend(PMSG_HIBERNATE);
461 462 463 464 465
	if (error) {
		if (hibernation_ops->recover)
			hibernation_ops->recover();
		goto Resume_devices;
	}
466

467 468 469 470 471 472
	device_pm_lock();

	error = device_power_down(PMSG_HIBERNATE);
	if (error)
		goto Unlock;

473 474
	error = hibernation_ops->prepare();
	if (error)
475
		goto Platofrm_finish;
476 477 478

	error = disable_nonboot_cpus();
	if (error)
479
		goto Platofrm_finish;
480

481 482 483 484 485
	local_irq_disable();
	sysdev_suspend(PMSG_HIBERNATE);
	hibernation_ops->enter();
	/* We should never get here */
	while (1);
486 487 488 489 490

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

494 495 496 497 498
	device_power_up(PMSG_RESTORE);

 Unlock:
	device_pm_unlock();

499
 Resume_devices:
500
	entering_platform_hibernation = false;
501
	device_resume(PMSG_RESTORE);
502
	resume_console();
503

504 505
 Close:
	hibernation_ops->end();
506

507
	return error;
508 509
}

L
Linus Torvalds 已提交
510
/**
511
 *	power_down - Shut the machine down for hibernation.
L
Linus Torvalds 已提交
512
 *
513 514
 *	Use the platform driver, if configured so; otherwise try
 *	to power off or reboot.
L
Linus Torvalds 已提交
515 516
 */

517
static void power_down(void)
L
Linus Torvalds 已提交
518
{
519 520 521
	switch (hibernation_mode) {
	case HIBERNATION_TEST:
	case HIBERNATION_TESTPROC:
522
		break;
523
	case HIBERNATION_REBOOT:
524
		kernel_restart(NULL);
L
Linus Torvalds 已提交
525
		break;
526
	case HIBERNATION_PLATFORM:
527
		hibernation_platform_enter();
528 529 530
	case HIBERNATION_SHUTDOWN:
		kernel_power_off();
		break;
L
Linus Torvalds 已提交
531
	}
532
	kernel_halt();
533 534 535 536
	/*
	 * Valid image is on the disk, if we continue we risk serious data
	 * corruption after resume.
	 */
R
Rafael J. Wysocki 已提交
537
	printk(KERN_CRIT "PM: Please power down manually\n");
L
Linus Torvalds 已提交
538 539 540 541 542
	while(1);
}

static int prepare_processes(void)
{
R
Rafael J. Wysocki 已提交
543
	int error = 0;
L
Linus Torvalds 已提交
544 545 546

	if (freeze_processes()) {
		error = -EBUSY;
547
		thaw_processes();
L
Linus Torvalds 已提交
548
	}
L
Li Shaohua 已提交
549
	return error;
L
Linus Torvalds 已提交
550 551 552
}

/**
553
 *	hibernate - The granpappy of the built-in hibernation management
L
Linus Torvalds 已提交
554 555
 */

556
int hibernate(void)
L
Linus Torvalds 已提交
557 558 559
{
	int error;

560
	mutex_lock(&pm_mutex);
561
	/* The snapshot device should not be opened while we're running */
562 563 564 565 566
	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
		error = -EBUSY;
		goto Unlock;
	}

567
	pm_prepare_console();
568 569 570
	error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
	if (error)
		goto Exit;
571

572 573 574 575
	error = usermodehelper_disable();
	if (error)
		goto Exit;

576 577 578 579 580
	/* Allocate memory management structures */
	error = create_basic_memory_bitmaps();
	if (error)
		goto Exit;

R
Rafael J. Wysocki 已提交
581
	printk(KERN_INFO "PM: Syncing filesystems ... ");
582 583 584
	sys_sync();
	printk("done.\n");

L
Linus Torvalds 已提交
585
	error = prepare_processes();
L
Li Shaohua 已提交
586
	if (error)
587
		goto Finish;
L
Linus Torvalds 已提交
588

589
	if (hibernation_test(TEST_FREEZER))
590
		goto Thaw;
591 592 593 594

	if (hibernation_testmode(HIBERNATION_TESTPROC))
		goto Thaw;

595 596
	error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
	if (in_suspend && !error) {
597 598 599 600
		unsigned int flags = 0;

		if (hibernation_mode == HIBERNATION_PLATFORM)
			flags |= SF_PLATFORM_MODE;
L
Linus Torvalds 已提交
601
		pr_debug("PM: writing image.\n");
602
		error = swsusp_write(flags);
603
		swsusp_free();
L
Linus Torvalds 已提交
604
		if (!error)
605
			power_down();
R
Rafael J. Wysocki 已提交
606
	} else {
L
Linus Torvalds 已提交
607
		pr_debug("PM: Image restored successfully.\n");
608
		swsusp_free();
R
Rafael J. Wysocki 已提交
609 610
	}
 Thaw:
611
	thaw_processes();
612 613
 Finish:
	free_basic_memory_bitmaps();
614
	usermodehelper_enable();
615
 Exit:
616
	pm_notifier_call_chain(PM_POST_HIBERNATION);
617
	pm_restore_console();
618
	atomic_inc(&snapshot_device_available);
619 620
 Unlock:
	mutex_unlock(&pm_mutex);
L
Linus Torvalds 已提交
621 622 623 624 625 626 627 628 629 630
	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
631
 *	return above (in hibernate() ) if everything goes well.
L
Linus Torvalds 已提交
632 633 634 635 636 637 638 639
 *	Otherwise, we fail gracefully and return to the normally
 *	scheduled program.
 *
 */

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

642 643 644 645 646 647
	/*
	 * If the user said "noresume".. bail out early.
	 */
	if (noresume)
		return 0;

J
Johannes Berg 已提交
648 649 650 651 652 653 654 655 656 657 658
	/*
	 * 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);
659 660 661 662 663 664 665 666 667 668 669 670 671

	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);
672
	if (!swsusp_resume_device) {
673 674 675 676 677
		/*
		 * Some device discovery might still be in progress; we need
		 * to wait for this to finish.
		 */
		wait_for_device_probe();
678 679 680 681 682 683 684
		/*
		 * 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();

685
		swsusp_resume_device = name_to_dev_t(resume_file);
686 687 688 689
		if (!swsusp_resume_device) {
			error = -ENODEV;
			goto Unlock;
		}
690 691
	}

692 693 694
 Check_image:
	pr_debug("PM: Resume from partition %d:%d\n",
		MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
L
Linus Torvalds 已提交
695

R
Rafael J. Wysocki 已提交
696
	pr_debug("PM: Checking hibernation image.\n");
697 698
	error = swsusp_check();
	if (error)
699
		goto Unlock;
L
Linus Torvalds 已提交
700

701 702 703 704 705 706
	/* The snapshot device should not be opened while we're running */
	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
		error = -EBUSY;
		goto Unlock;
	}

707
	pm_prepare_console();
708 709 710 711
	error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
	if (error)
		goto Finish;

712 713 714 715
	error = usermodehelper_disable();
	if (error)
		goto Finish;

716 717
	error = create_basic_memory_bitmaps();
	if (error)
718
		goto Finish;
L
Linus Torvalds 已提交
719

720
	pr_debug("PM: Preparing processes for restore.\n");
721 722
	error = prepare_processes();
	if (error) {
723
		swsusp_close(FMODE_READ);
L
Li Shaohua 已提交
724
		goto Done;
L
Linus Torvalds 已提交
725 726
	}

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

729
	error = swsusp_read(&flags);
730
	if (!error)
731
		hibernation_restore(flags & SF_PLATFORM_MODE);
L
Linus Torvalds 已提交
732

733
	printk(KERN_ERR "PM: Restore failed, recovering.\n");
734
	swsusp_free();
735
	thaw_processes();
L
Linus Torvalds 已提交
736
 Done:
737
	free_basic_memory_bitmaps();
738
	usermodehelper_enable();
739
 Finish:
740
	pm_notifier_call_chain(PM_POST_RESTORE);
741
	pm_restore_console();
742
	atomic_inc(&snapshot_device_available);
743
	/* For success case, the suspend path will release the lock */
744
 Unlock:
745
	mutex_unlock(&pm_mutex);
L
Linus Torvalds 已提交
746
	pr_debug("PM: Resume from disk failed.\n");
747
	return error;
L
Linus Torvalds 已提交
748 749 750 751 752
}

late_initcall(software_resume);


753 754 755 756 757 758
static const char * const hibernation_modes[] = {
	[HIBERNATION_PLATFORM]	= "platform",
	[HIBERNATION_SHUTDOWN]	= "shutdown",
	[HIBERNATION_REBOOT]	= "reboot",
	[HIBERNATION_TEST]	= "test",
	[HIBERNATION_TESTPROC]	= "testproc",
L
Linus Torvalds 已提交
759 760 761
};

/**
762
 *	disk - Control hibernation mode
L
Linus Torvalds 已提交
763
 *
764 765
 *	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
766 767
 *	or other hibernation_ops), powering off the system or rebooting the
 *	system (for testing) as well as the two test modes.
L
Linus Torvalds 已提交
768
 *
769
 *	The system can support 'platform', and that is known a priori (and
770 771 772
 *	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 已提交
773 774 775 776 777 778 779
 *
 *	show() will display what the mode is currently set to.
 *	store() will accept one of
 *
 *	'platform'
 *	'shutdown'
 *	'reboot'
780 781
 *	'test'
 *	'testproc'
L
Linus Torvalds 已提交
782
 *
783
 *	It will only change to 'platform' if the system
784
 *	supports it (as determined by having hibernation_ops).
L
Linus Torvalds 已提交
785 786
 */

787 788
static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
			 char *buf)
L
Linus Torvalds 已提交
789
{
790 791 792
	int i;
	char *start = buf;

793 794
	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
		if (!hibernation_modes[i])
795 796
			continue;
		switch (i) {
797 798 799 800
		case HIBERNATION_SHUTDOWN:
		case HIBERNATION_REBOOT:
		case HIBERNATION_TEST:
		case HIBERNATION_TESTPROC:
801
			break;
802 803
		case HIBERNATION_PLATFORM:
			if (hibernation_ops)
804 805 806 807
				break;
			/* not a valid mode, continue with loop */
			continue;
		}
808 809
		if (i == hibernation_mode)
			buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
810
		else
811
			buf += sprintf(buf, "%s ", hibernation_modes[i]);
812 813 814
	}
	buf += sprintf(buf, "\n");
	return buf-start;
L
Linus Torvalds 已提交
815 816 817
}


818 819
static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
			  const char *buf, size_t n)
L
Linus Torvalds 已提交
820 821 822 823 824
{
	int error = 0;
	int i;
	int len;
	char *p;
825
	int mode = HIBERNATION_INVALID;
L
Linus Torvalds 已提交
826 827 828 829

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

830
	mutex_lock(&pm_mutex);
831
	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
R
Rafael J. Wysocki 已提交
832 833
		if (len == strlen(hibernation_modes[i])
		    && !strncmp(buf, hibernation_modes[i], len)) {
L
Linus Torvalds 已提交
834 835 836 837
			mode = i;
			break;
		}
	}
838
	if (mode != HIBERNATION_INVALID) {
839
		switch (mode) {
840 841 842 843 844
		case HIBERNATION_SHUTDOWN:
		case HIBERNATION_REBOOT:
		case HIBERNATION_TEST:
		case HIBERNATION_TESTPROC:
			hibernation_mode = mode;
845
			break;
846 847 848
		case HIBERNATION_PLATFORM:
			if (hibernation_ops)
				hibernation_mode = mode;
L
Linus Torvalds 已提交
849 850 851
			else
				error = -EINVAL;
		}
852
	} else
L
Linus Torvalds 已提交
853 854
		error = -EINVAL;

855
	if (!error)
R
Rafael J. Wysocki 已提交
856
		pr_debug("PM: Hibernation mode set to '%s'\n",
857
			 hibernation_modes[mode]);
858
	mutex_unlock(&pm_mutex);
L
Linus Torvalds 已提交
859 860 861 862 863
	return error ? error : n;
}

power_attr(disk);

864 865
static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
			   char *buf)
L
Linus Torvalds 已提交
866 867 868 869 870
{
	return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
		       MINOR(swsusp_resume_device));
}

871 872
static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
			    const char *buf, size_t n)
L
Linus Torvalds 已提交
873 874 875
{
	unsigned int maj, min;
	dev_t res;
876
	int ret = -EINVAL;
L
Linus Torvalds 已提交
877

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

881 882 883
	res = MKDEV(maj,min);
	if (maj != MAJOR(res) || min != MINOR(res))
		goto out;
L
Linus Torvalds 已提交
884

885
	mutex_lock(&pm_mutex);
886
	swsusp_resume_device = res;
887
	mutex_unlock(&pm_mutex);
R
Rafael J. Wysocki 已提交
888
	printk(KERN_INFO "PM: Starting manual resume from disk\n");
889 890 891
	noresume = 0;
	software_resume();
	ret = n;
R
Rafael J. Wysocki 已提交
892
 out:
893
	return ret;
L
Linus Torvalds 已提交
894 895 896 897
}

power_attr(resume);

898 899
static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
			       char *buf)
900
{
901
	return sprintf(buf, "%lu\n", image_size);
902 903
}

904 905
static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
				const char *buf, size_t n)
906
{
907
	unsigned long size;
908

909
	if (sscanf(buf, "%lu", &size) == 1) {
910 911 912 913 914 915 916 917 918
		image_size = size;
		return n;
	}

	return -EINVAL;
}

power_attr(image_size);

L
Linus Torvalds 已提交
919 920 921
static struct attribute * g[] = {
	&disk_attr.attr,
	&resume_attr.attr,
922
	&image_size_attr.attr,
L
Linus Torvalds 已提交
923 924 925 926 927 928 929 930 931 932 933
	NULL,
};


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


static int __init pm_disk_init(void)
{
934
	return sysfs_create_group(power_kobj, &attr_group);
L
Linus Torvalds 已提交
935 936 937 938 939 940 941 942 943 944 945 946 947 948
}

core_initcall(pm_disk_init);


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

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

949 950 951 952 953 954 955 956 957 958 959 960 961
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 已提交
962 963 964 965 966 967 968
static int __init noresume_setup(char *str)
{
	noresume = 1;
	return 1;
}

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