hibernate.c 21.3 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
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.
Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
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

Linus Torvalds's avatar
Linus Torvalds committed
28
29
30
31
#include "power.h"


static int noresume = 0;
32
static char resume_file[256] = CONFIG_PM_STD_PARTITION;
Linus Torvalds's avatar
Linus Torvalds committed
33
dev_t swsusp_resume_device;
34
sector_t swsusp_resume_block;
Linus Torvalds's avatar
Linus Torvalds committed
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
219
	/* At this point, dpm_suspend_start() has been called, but *not*
	 * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
220
221
222
223
	 * 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.
	 */
224
	error = dpm_suspend_noirq(PMSG_FREEZE);
225
	if (error) {
226
227
		printk(KERN_ERR "PM: Some devices failed to power down, "
			"aborting hibernation\n");
228
		return error;
229
	}
230

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

240
241
	local_irq_disable();

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

249
250
251
252
	if (hibernation_test(TEST_CORE))
		goto Power_up;

	in_suspend = 1;
253
254
255
	save_processor_state();
	error = swsusp_arch_suspend();
	if (error)
256
257
		printk(KERN_ERR "PM: Error %d creating hibernation image\n",
			error);
258
259
260
261
	/* Restore control flow magically appears here */
	restore_processor_state();
	if (!in_suspend)
		platform_leave(platform_mode);
262

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

269
 Enable_irqs:
270
271
	local_irq_enable();

272
273
274
275
276
277
 Enable_cpus:
	enable_nonboot_cpus();

 Platform_finish:
	platform_finish(platform_mode);

278
	dpm_resume_noirq(in_suspend ?
279
		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
280

281
282
283
	return error;
}

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

int hibernation_snapshot(int platform_mode)
{
295
	int error;
296

297
	error = platform_begin(platform_mode);
298
	if (error)
299
		return error;
300

301
302
	/* Free memory before shutting down devices. */
	error = swsusp_shrink_memory();
303
	if (error)
304
		goto Close;
305

306
	suspend_console();
307
	error = dpm_suspend_start(PMSG_FREEZE);
308
	if (error)
309
		goto Recover_platform;
310

311
	if (hibernation_test(TEST_DEVICES))
312
		goto Recover_platform;
313

314
315
	error = create_image(platform_mode);
	/* Control returns here after successful restore */
316
317

 Resume_devices:
318
	dpm_resume_end(in_suspend ?
319
		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
320
	resume_console();
321
322
 Close:
	platform_end(platform_mode);
323
	return error;
324
325
326
327

 Recover_platform:
	platform_recover(platform_mode);
	goto Resume_devices;
328
329
}

330
331
332
333
334
335
336
337
/**
 *	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.
 */

338
static int resume_target_kernel(bool platform_mode)
339
340
341
{
	int error;

342
	error = dpm_suspend_noirq(PMSG_QUIESCE);
343
	if (error) {
344
		printk(KERN_ERR "PM: Some devices failed to power down, "
345
			"aborting resume\n");
346
		return error;
347
	}
348

349
350
351
352
353
354
355
356
	error = platform_pre_restore(platform_mode);
	if (error)
		goto Cleanup;

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

357
358
	local_irq_disable();

359
360
361
362
	error = sysdev_suspend(PMSG_QUIESCE);
	if (error)
		goto Enable_irqs;

363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
	/* 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();
385

386
	sysdev_resume();
387

388
 Enable_irqs:
389
	local_irq_enable();
390

391
392
393
394
395
396
 Enable_cpus:
	enable_nonboot_cpus();

 Cleanup:
	platform_restore_cleanup(platform_mode);

397
	dpm_resume_noirq(PMSG_RECOVER);
398

399
400
401
	return error;
}

402
403
404
/**
 *	hibernation_restore - quiesce devices and restore the hibernation
 *	snapshot image.  If successful, control returns in hibernation_snaphot()
405
 *	@platform_mode - if set, use the platform driver, if available, to
406
 *			 prepare the platform firmware for the transition.
407
408
409
410
 *
 *	Must be called with pm_mutex held
 */

411
int hibernation_restore(int platform_mode)
412
{
413
	int error;
414
415
416

	pm_prepare_console();
	suspend_console();
417
	error = dpm_suspend_start(PMSG_QUIESCE);
418
	if (!error) {
419
		error = resume_target_kernel(platform_mode);
420
		dpm_resume_end(PMSG_RECOVER);
421
	}
422
423
424
425
426
427
428
429
430
431
432
433
	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)
{
434
	int error;
435

436
437
438
439
440
441
442
443
	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
	 */
444
	error = hibernation_ops->begin();
445
	if (error)
446
		goto Close;
447

448
	entering_platform_hibernation = true;
449
	suspend_console();
450
	error = dpm_suspend_start(PMSG_HIBERNATE);
451
452
453
454
455
	if (error) {
		if (hibernation_ops->recover)
			hibernation_ops->recover();
		goto Resume_devices;
	}
456

457
	error = dpm_suspend_noirq(PMSG_HIBERNATE);
458
	if (error)
459
		goto Resume_devices;
460

461
462
	error = hibernation_ops->prepare();
	if (error)
463
		goto Platform_finish;
464
465
466

	error = disable_nonboot_cpus();
	if (error)
467
		goto Platform_finish;
468

469
470
471
472
473
	local_irq_disable();
	sysdev_suspend(PMSG_HIBERNATE);
	hibernation_ops->enter();
	/* We should never get here */
	while (1);
474
475
476
477
478

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

482
	dpm_suspend_noirq(PMSG_RESTORE);
483

484
 Resume_devices:
485
	entering_platform_hibernation = false;
486
	dpm_resume_end(PMSG_RESTORE);
487
	resume_console();
488

489
490
 Close:
	hibernation_ops->end();
491

492
	return error;
493
494
}

Linus Torvalds's avatar
Linus Torvalds committed
495
/**
496
 *	power_down - Shut the machine down for hibernation.
Linus Torvalds's avatar
Linus Torvalds committed
497
 *
498
499
 *	Use the platform driver, if configured so; otherwise try
 *	to power off or reboot.
Linus Torvalds's avatar
Linus Torvalds committed
500
501
 */

502
static void power_down(void)
Linus Torvalds's avatar
Linus Torvalds committed
503
{
504
505
506
	switch (hibernation_mode) {
	case HIBERNATION_TEST:
	case HIBERNATION_TESTPROC:
507
		break;
508
	case HIBERNATION_REBOOT:
509
		kernel_restart(NULL);
Linus Torvalds's avatar
Linus Torvalds committed
510
		break;
511
	case HIBERNATION_PLATFORM:
512
		hibernation_platform_enter();
513
514
515
	case HIBERNATION_SHUTDOWN:
		kernel_power_off();
		break;
Linus Torvalds's avatar
Linus Torvalds committed
516
	}
517
	kernel_halt();
518
519
520
521
	/*
	 * Valid image is on the disk, if we continue we risk serious data
	 * corruption after resume.
	 */
522
	printk(KERN_CRIT "PM: Please power down manually\n");
Linus Torvalds's avatar
Linus Torvalds committed
523
524
525
526
527
	while(1);
}

static int prepare_processes(void)
{
Rafael J. Wysocki's avatar
Rafael J. Wysocki committed
528
	int error = 0;
Linus Torvalds's avatar
Linus Torvalds committed
529
530
531

	if (freeze_processes()) {
		error = -EBUSY;
532
		thaw_processes();
Linus Torvalds's avatar
Linus Torvalds committed
533
	}
534
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
535
536
537
}

/**
538
 *	hibernate - The granpappy of the built-in hibernation management
Linus Torvalds's avatar
Linus Torvalds committed
539
540
 */

541
int hibernate(void)
Linus Torvalds's avatar
Linus Torvalds committed
542
543
544
{
	int error;

545
	mutex_lock(&pm_mutex);
546
	/* The snapshot device should not be opened while we're running */
547
548
549
550
551
	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
		error = -EBUSY;
		goto Unlock;
	}

552
	pm_prepare_console();
553
554
555
	error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
	if (error)
		goto Exit;
556

557
558
559
560
	error = usermodehelper_disable();
	if (error)
		goto Exit;

561
562
563
564
565
	/* Allocate memory management structures */
	error = create_basic_memory_bitmaps();
	if (error)
		goto Exit;

566
	printk(KERN_INFO "PM: Syncing filesystems ... ");
567
568
569
	sys_sync();
	printk("done.\n");

Linus Torvalds's avatar
Linus Torvalds committed
570
	error = prepare_processes();
571
	if (error)
572
		goto Finish;
Linus Torvalds's avatar
Linus Torvalds committed
573

574
	if (hibernation_test(TEST_FREEZER))
575
		goto Thaw;
576
577
578
579

	if (hibernation_testmode(HIBERNATION_TESTPROC))
		goto Thaw;

580
581
	error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
	if (in_suspend && !error) {
582
583
584
585
		unsigned int flags = 0;

		if (hibernation_mode == HIBERNATION_PLATFORM)
			flags |= SF_PLATFORM_MODE;
Linus Torvalds's avatar
Linus Torvalds committed
586
		pr_debug("PM: writing image.\n");
587
		error = swsusp_write(flags);
588
		swsusp_free();
Linus Torvalds's avatar
Linus Torvalds committed
589
		if (!error)
590
			power_down();
Rafael J. Wysocki's avatar
Rafael J. Wysocki committed
591
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
592
		pr_debug("PM: Image restored successfully.\n");
593
		swsusp_free();
Rafael J. Wysocki's avatar
Rafael J. Wysocki committed
594
595
	}
 Thaw:
596
	thaw_processes();
597
598
 Finish:
	free_basic_memory_bitmaps();
599
	usermodehelper_enable();
600
 Exit:
601
	pm_notifier_call_chain(PM_POST_HIBERNATION);
602
	pm_restore_console();
603
	atomic_inc(&snapshot_device_available);
604
605
 Unlock:
	mutex_unlock(&pm_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
606
607
608
609
610
611
612
613
614
615
	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
616
 *	return above (in hibernate() ) if everything goes well.
Linus Torvalds's avatar
Linus Torvalds committed
617
618
619
620
621
622
623
624
 *	Otherwise, we fail gracefully and return to the normally
 *	scheduled program.
 *
 */

static int software_resume(void)
{
	int error;
625
	unsigned int flags;
Linus Torvalds's avatar
Linus Torvalds committed
626

627
628
629
630
631
632
	/*
	 * If the user said "noresume".. bail out early.
	 */
	if (noresume)
		return 0;

Johannes Berg's avatar
Johannes Berg committed
633
634
635
636
637
638
639
640
641
642
643
	/*
	 * 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);
644
645
646
647
648
649
650
651
652
653
654
655
656

	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);
657
	if (!swsusp_resume_device) {
658
659
660
661
662
		/*
		 * Some device discovery might still be in progress; we need
		 * to wait for this to finish.
		 */
		wait_for_device_probe();
663
664
665
666
667
668
669
		/*
		 * 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();

670
		swsusp_resume_device = name_to_dev_t(resume_file);
671
672
673
674
		if (!swsusp_resume_device) {
			error = -ENODEV;
			goto Unlock;
		}
675
676
	}

677
678
679
 Check_image:
	pr_debug("PM: Resume from partition %d:%d\n",
		MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
Linus Torvalds's avatar
Linus Torvalds committed
680

681
	pr_debug("PM: Checking hibernation image.\n");
682
683
	error = swsusp_check();
	if (error)
684
		goto Unlock;
Linus Torvalds's avatar
Linus Torvalds committed
685

686
687
688
689
690
691
	/* The snapshot device should not be opened while we're running */
	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
		error = -EBUSY;
		goto Unlock;
	}

692
	pm_prepare_console();
693
694
695
696
	error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
	if (error)
		goto Finish;

697
698
699
700
	error = usermodehelper_disable();
	if (error)
		goto Finish;

701
702
	error = create_basic_memory_bitmaps();
	if (error)
703
		goto Finish;
Linus Torvalds's avatar
Linus Torvalds committed
704

705
	pr_debug("PM: Preparing processes for restore.\n");
706
707
	error = prepare_processes();
	if (error) {
708
		swsusp_close(FMODE_READ);
709
		goto Done;
Linus Torvalds's avatar
Linus Torvalds committed
710
711
	}

712
	pr_debug("PM: Reading hibernation image.\n");
Linus Torvalds's avatar
Linus Torvalds committed
713

714
	error = swsusp_read(&flags);
715
	if (!error)
716
		hibernation_restore(flags & SF_PLATFORM_MODE);
Linus Torvalds's avatar
Linus Torvalds committed
717

718
	printk(KERN_ERR "PM: Restore failed, recovering.\n");
719
	swsusp_free();
720
	thaw_processes();
Linus Torvalds's avatar
Linus Torvalds committed
721
 Done:
722
	free_basic_memory_bitmaps();
723
	usermodehelper_enable();
724
 Finish:
725
	pm_notifier_call_chain(PM_POST_RESTORE);
726
	pm_restore_console();
727
	atomic_inc(&snapshot_device_available);
728
	/* For success case, the suspend path will release the lock */
729
 Unlock:
730
	mutex_unlock(&pm_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
731
	pr_debug("PM: Resume from disk failed.\n");
732
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
733
734
735
736
737
}

late_initcall(software_resume);


738
739
740
741
742
743
static const char * const hibernation_modes[] = {
	[HIBERNATION_PLATFORM]	= "platform",
	[HIBERNATION_SHUTDOWN]	= "shutdown",
	[HIBERNATION_REBOOT]	= "reboot",
	[HIBERNATION_TEST]	= "test",
	[HIBERNATION_TESTPROC]	= "testproc",
Linus Torvalds's avatar
Linus Torvalds committed
744
745
746
};

/**
747
 *	disk - Control hibernation mode
Linus Torvalds's avatar
Linus Torvalds committed
748
 *
749
750
 *	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
751
752
 *	or other hibernation_ops), powering off the system or rebooting the
 *	system (for testing) as well as the two test modes.
Linus Torvalds's avatar
Linus Torvalds committed
753
 *
754
 *	The system can support 'platform', and that is known a priori (and
755
756
757
 *	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'.
Linus Torvalds's avatar
Linus Torvalds committed
758
759
760
761
762
763
764
 *
 *	show() will display what the mode is currently set to.
 *	store() will accept one of
 *
 *	'platform'
 *	'shutdown'
 *	'reboot'
765
766
 *	'test'
 *	'testproc'
Linus Torvalds's avatar
Linus Torvalds committed
767
 *
768
 *	It will only change to 'platform' if the system
769
 *	supports it (as determined by having hibernation_ops).
Linus Torvalds's avatar
Linus Torvalds committed
770
771
 */

772
773
static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
			 char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
774
{
775
776
777
	int i;
	char *start = buf;

778
779
	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
		if (!hibernation_modes[i])
780
781
			continue;
		switch (i) {
782
783
784
785
		case HIBERNATION_SHUTDOWN:
		case HIBERNATION_REBOOT:
		case HIBERNATION_TEST:
		case HIBERNATION_TESTPROC:
786
			break;
787
788
		case HIBERNATION_PLATFORM:
			if (hibernation_ops)
789
790
791
792
				break;
			/* not a valid mode, continue with loop */
			continue;
		}
793
794
		if (i == hibernation_mode)
			buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
795
		else
796
			buf += sprintf(buf, "%s ", hibernation_modes[i]);
797
798
799
	}
	buf += sprintf(buf, "\n");
	return buf-start;
Linus Torvalds's avatar
Linus Torvalds committed
800
801
802
}


803
804
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
805
806
807
808
809
{
	int error = 0;
	int i;
	int len;
	char *p;
810
	int mode = HIBERNATION_INVALID;
Linus Torvalds's avatar
Linus Torvalds committed
811
812
813
814

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

815
	mutex_lock(&pm_mutex);
816
	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
817
818
		if (len == strlen(hibernation_modes[i])
		    && !strncmp(buf, hibernation_modes[i], len)) {
Linus Torvalds's avatar
Linus Torvalds committed
819
820
821
822
			mode = i;
			break;
		}
	}
823
	if (mode != HIBERNATION_INVALID) {
824
		switch (mode) {
825
826
827
828
829
		case HIBERNATION_SHUTDOWN:
		case HIBERNATION_REBOOT:
		case HIBERNATION_TEST:
		case HIBERNATION_TESTPROC:
			hibernation_mode = mode;
830
			break;
831
832
833
		case HIBERNATION_PLATFORM:
			if (hibernation_ops)
				hibernation_mode = mode;
Linus Torvalds's avatar
Linus Torvalds committed
834
835
836
			else
				error = -EINVAL;
		}
837
	} else
Linus Torvalds's avatar
Linus Torvalds committed
838
839
		error = -EINVAL;

840
	if (!error)
841
		pr_debug("PM: Hibernation mode set to '%s'\n",
842
			 hibernation_modes[mode]);
843
	mutex_unlock(&pm_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
844
845
846
847
848
	return error ? error : n;
}

power_attr(disk);

849
850
static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
			   char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
851
852
853
854
855
{
	return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
		       MINOR(swsusp_resume_device));
}

856
857
static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
			    const char *buf, size_t n)
Linus Torvalds's avatar
Linus Torvalds committed
858
859
860
{
	unsigned int maj, min;
	dev_t res;
861
	int ret = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
862

863
864
	if (sscanf(buf, "%u:%u", &maj, &min) != 2)
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
865

866
867
868
	res = MKDEV(maj,min);
	if (maj != MAJOR(res) || min != MINOR(res))
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
869

870
	mutex_lock(&pm_mutex);
871
	swsusp_resume_device = res;
872
	mutex_unlock(&pm_mutex);
873
	printk(KERN_INFO "PM: Starting manual resume from disk\n");
874
875
876
	noresume = 0;
	software_resume();
	ret = n;
Rafael J. Wysocki's avatar
Rafael J. Wysocki committed
877
 out:
878
	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
879
880
881
882
}

power_attr(resume);

883
884
static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
			       char *buf)
885
{
886
	return sprintf(buf, "%lu\n", image_size);
887
888
}

889
890
static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
				const char *buf, size_t n)
891
{
892
	unsigned long size;
893

894
	if (sscanf(buf, "%lu", &size) == 1) {
895
896
897
898
899
900
901
902
903
		image_size = size;
		return n;
	}

	return -EINVAL;
}

power_attr(image_size);

Linus Torvalds's avatar
Linus Torvalds committed
904
905
906
static struct attribute * g[] = {
	&disk_attr.attr,
	&resume_attr.attr,
907
	&image_size_attr.attr,
Linus Torvalds's avatar
Linus Torvalds committed
908
909
910
911
912
913
914
915
916
917
918
	NULL,
};


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


static int __init pm_disk_init(void)
{
919
	return sysfs_create_group(power_kobj, &attr_group);
Linus Torvalds's avatar
Linus Torvalds committed
920
921
922
923
924
925
926
927
928
929
930
931
932
933
}

core_initcall(pm_disk_init);


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

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

934
935
936
937
938
939
940
941
942
943
944
945
946
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;
}

Linus Torvalds's avatar
Linus Torvalds committed
947
948
949
950
951
952
953
static int __init noresume_setup(char *str)
{
	noresume = 1;
	return 1;
}

__setup("noresume", noresume_setup);
954
__setup("resume_offset=", resume_offset_setup);
Linus Torvalds's avatar
Linus Torvalds committed
955
__setup("resume=", resume_setup);