xref: /linux/kernel/power/hibernate.c (revision b5c00a3a412857d6f07970984068c450429e051c)
1 /*
2  * kernel/power/hibernate.c - Hibernation (a.k.a suspend-to-disk) support.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  * Copyright (c) 2004 Pavel Machek <pavel@suse.cz>
7  * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
8  *
9  * This file is released under the GPLv2.
10  */
11 
12 #include <linux/suspend.h>
13 #include <linux/syscalls.h>
14 #include <linux/reboot.h>
15 #include <linux/string.h>
16 #include <linux/device.h>
17 #include <linux/kmod.h>
18 #include <linux/delay.h>
19 #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 
28 #include "power.h"
29 
30 
31 static int noresume = 0;
32 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
33 dev_t swsusp_resume_device;
34 sector_t swsusp_resume_block;
35 int in_suspend __nosavedata = 0;
36 
37 enum {
38 	HIBERNATION_INVALID,
39 	HIBERNATION_PLATFORM,
40 	HIBERNATION_TEST,
41 	HIBERNATION_TESTPROC,
42 	HIBERNATION_SHUTDOWN,
43 	HIBERNATION_REBOOT,
44 	/* keep last */
45 	__HIBERNATION_AFTER_LAST
46 };
47 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
48 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
49 
50 static int hibernation_mode = HIBERNATION_SHUTDOWN;
51 
52 static struct platform_hibernation_ops *hibernation_ops;
53 
54 /**
55  * hibernation_set_ops - set the global hibernate operations
56  * @ops: the hibernation operations to use in subsequent hibernation transitions
57  */
58 
59 void hibernation_set_ops(struct platform_hibernation_ops *ops)
60 {
61 	if (ops && !(ops->begin && ops->end &&  ops->pre_snapshot
62 	    && ops->prepare && ops->finish && ops->enter && ops->pre_restore
63 	    && ops->restore_cleanup)) {
64 		WARN_ON(1);
65 		return;
66 	}
67 	mutex_lock(&pm_mutex);
68 	hibernation_ops = ops;
69 	if (ops)
70 		hibernation_mode = HIBERNATION_PLATFORM;
71 	else if (hibernation_mode == HIBERNATION_PLATFORM)
72 		hibernation_mode = HIBERNATION_SHUTDOWN;
73 
74 	mutex_unlock(&pm_mutex);
75 }
76 
77 static bool entering_platform_hibernation;
78 
79 bool system_entering_hibernation(void)
80 {
81 	return entering_platform_hibernation;
82 }
83 EXPORT_SYMBOL(system_entering_hibernation);
84 
85 #ifdef CONFIG_PM_DEBUG
86 static void hibernation_debug_sleep(void)
87 {
88 	printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
89 	mdelay(5000);
90 }
91 
92 static int hibernation_testmode(int mode)
93 {
94 	if (hibernation_mode == mode) {
95 		hibernation_debug_sleep();
96 		return 1;
97 	}
98 	return 0;
99 }
100 
101 static int hibernation_test(int level)
102 {
103 	if (pm_test_level == level) {
104 		hibernation_debug_sleep();
105 		return 1;
106 	}
107 	return 0;
108 }
109 #else /* !CONFIG_PM_DEBUG */
110 static int hibernation_testmode(int mode) { return 0; }
111 static int hibernation_test(int level) { return 0; }
112 #endif /* !CONFIG_PM_DEBUG */
113 
114 /**
115  *	platform_begin - tell the platform driver that we're starting
116  *	hibernation
117  */
118 
119 static int platform_begin(int platform_mode)
120 {
121 	return (platform_mode && hibernation_ops) ?
122 		hibernation_ops->begin() : 0;
123 }
124 
125 /**
126  *	platform_end - tell the platform driver that we've entered the
127  *	working state
128  */
129 
130 static void platform_end(int platform_mode)
131 {
132 	if (platform_mode && hibernation_ops)
133 		hibernation_ops->end();
134 }
135 
136 /**
137  *	platform_pre_snapshot - prepare the machine for hibernation using the
138  *	platform driver if so configured and return an error code if it fails
139  */
140 
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  *	platform_leave - prepare the machine for switching to the normal mode
149  *	of operation using the platform driver (called with interrupts disabled)
150  */
151 
152 static void platform_leave(int platform_mode)
153 {
154 	if (platform_mode && hibernation_ops)
155 		hibernation_ops->leave();
156 }
157 
158 /**
159  *	platform_finish - switch the machine to the normal mode of operation
160  *	using the platform driver (must be called after platform_prepare())
161  */
162 
163 static void platform_finish(int platform_mode)
164 {
165 	if (platform_mode && hibernation_ops)
166 		hibernation_ops->finish();
167 }
168 
169 /**
170  *	platform_pre_restore - prepare the platform for the restoration from a
171  *	hibernation image.  If the restore fails after this function has been
172  *	called, platform_restore_cleanup() must be called.
173  */
174 
175 static int platform_pre_restore(int platform_mode)
176 {
177 	return (platform_mode && hibernation_ops) ?
178 		hibernation_ops->pre_restore() : 0;
179 }
180 
181 /**
182  *	platform_restore_cleanup - switch the platform to the normal mode of
183  *	operation after a failing restore.  If platform_pre_restore() has been
184  *	called before the failing restore, this function must be called too,
185  *	regardless of the result of platform_pre_restore().
186  */
187 
188 static void platform_restore_cleanup(int platform_mode)
189 {
190 	if (platform_mode && hibernation_ops)
191 		hibernation_ops->restore_cleanup();
192 }
193 
194 /**
195  *	platform_recover - recover the platform from a failure to suspend
196  *	devices.
197  */
198 
199 static void platform_recover(int platform_mode)
200 {
201 	if (platform_mode && hibernation_ops && hibernation_ops->recover)
202 		hibernation_ops->recover();
203 }
204 
205 /**
206  *	swsusp_show_speed - print the time elapsed between two events.
207  *	@start: Starting event.
208  *	@stop: Final event.
209  *	@nr_pages -	number of pages processed between @start and @stop
210  *	@msg -		introductory message to print
211  */
212 
213 void swsusp_show_speed(struct timeval *start, struct timeval *stop,
214 			unsigned nr_pages, char *msg)
215 {
216 	s64 elapsed_centisecs64;
217 	int centisecs;
218 	int k;
219 	int kps;
220 
221 	elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
222 	do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
223 	centisecs = elapsed_centisecs64;
224 	if (centisecs == 0)
225 		centisecs = 1;	/* avoid div-by-zero */
226 	k = nr_pages * (PAGE_SIZE / 1024);
227 	kps = (k * 100) / centisecs;
228 	printk(KERN_INFO "PM: %s %d kbytes in %d.%02d seconds (%d.%02d MB/s)\n",
229 			msg, k,
230 			centisecs / 100, centisecs % 100,
231 			kps / 1000, (kps % 1000) / 10);
232 }
233 
234 /**
235  *	create_image - freeze devices that need to be frozen with interrupts
236  *	off, create the hibernation image and thaw those devices.  Control
237  *	reappears in this routine after a restore.
238  */
239 
240 static int create_image(int platform_mode)
241 {
242 	int error;
243 
244 	error = arch_prepare_suspend();
245 	if (error)
246 		return error;
247 
248 	/* At this point, dpm_suspend_start() has been called, but *not*
249 	 * dpm_suspend_noirq(). We *must* call dpm_suspend_noirq() now.
250 	 * Otherwise, drivers for some devices (e.g. interrupt controllers)
251 	 * become desynchronized with the actual state of the hardware
252 	 * at resume time, and evil weirdness ensues.
253 	 */
254 	error = dpm_suspend_noirq(PMSG_FREEZE);
255 	if (error) {
256 		printk(KERN_ERR "PM: Some devices failed to power down, "
257 			"aborting hibernation\n");
258 		return error;
259 	}
260 
261 	error = platform_pre_snapshot(platform_mode);
262 	if (error || hibernation_test(TEST_PLATFORM))
263 		goto Platform_finish;
264 
265 	error = disable_nonboot_cpus();
266 	if (error || hibernation_test(TEST_CPUS)
267 	    || hibernation_testmode(HIBERNATION_TEST))
268 		goto Enable_cpus;
269 
270 	local_irq_disable();
271 
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 	if (hibernation_test(TEST_CORE))
280 		goto Power_up;
281 
282 	in_suspend = 1;
283 	save_processor_state();
284 	error = swsusp_arch_suspend();
285 	if (error)
286 		printk(KERN_ERR "PM: Error %d creating hibernation image\n",
287 			error);
288 	/* Restore control flow magically appears here */
289 	restore_processor_state();
290 	if (!in_suspend)
291 		platform_leave(platform_mode);
292 
293  Power_up:
294 	sysdev_resume();
295 	/* NOTE:  dpm_resume_noirq() is just a resume() for devices
296 	 * that suspended with irqs off ... no overall powerup.
297 	 */
298 
299  Enable_irqs:
300 	local_irq_enable();
301 
302  Enable_cpus:
303 	enable_nonboot_cpus();
304 
305  Platform_finish:
306 	platform_finish(platform_mode);
307 
308 	dpm_resume_noirq(in_suspend ?
309 		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
310 
311 	return error;
312 }
313 
314 /**
315  *	hibernation_snapshot - quiesce devices and create the hibernation
316  *	snapshot image.
317  *	@platform_mode - if set, use the platform driver, if available, to
318  *			 prepare the platform firmware for the power transition.
319  *
320  *	Must be called with pm_mutex held
321  */
322 
323 int hibernation_snapshot(int platform_mode)
324 {
325 	int error;
326 
327 	error = platform_begin(platform_mode);
328 	if (error)
329 		return error;
330 
331 	/* Preallocate image memory before shutting down devices. */
332 	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 	error = create_image(platform_mode);
345 	/* Control returns here after successful restore */
346 
347  Resume_devices:
348 	/* We may need to release the preallocated image pages here. */
349 	if (error || !in_suspend)
350 		swsusp_free();
351 
352 	dpm_resume_end(in_suspend ?
353 		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
354 	resume_console();
355  Close:
356 	platform_end(platform_mode);
357 	return error;
358 
359  Recover_platform:
360 	platform_recover(platform_mode);
361 	goto Resume_devices;
362 }
363 
364 /**
365  *	resume_target_kernel - prepare devices that need to be suspended with
366  *	interrupts off, restore the contents of highmem that have not been
367  *	restored yet from the image and run the low level code that will restore
368  *	the remaining contents of memory and switch to the just restored target
369  *	kernel.
370  */
371 
372 static int resume_target_kernel(bool platform_mode)
373 {
374 	int error;
375 
376 	error = dpm_suspend_noirq(PMSG_QUIESCE);
377 	if (error) {
378 		printk(KERN_ERR "PM: Some devices failed to power down, "
379 			"aborting resume\n");
380 		return error;
381 	}
382 
383 	error = platform_pre_restore(platform_mode);
384 	if (error)
385 		goto Cleanup;
386 
387 	error = disable_nonboot_cpus();
388 	if (error)
389 		goto Enable_cpus;
390 
391 	local_irq_disable();
392 
393 	error = sysdev_suspend(PMSG_QUIESCE);
394 	if (error)
395 		goto Enable_irqs;
396 
397 	/* We'll ignore saved state, but this gets preempt count (etc) right */
398 	save_processor_state();
399 	error = restore_highmem();
400 	if (!error) {
401 		error = swsusp_arch_resume();
402 		/*
403 		 * The code below is only ever reached in case of a failure.
404 		 * Otherwise execution continues at place where
405 		 * swsusp_arch_suspend() was called
406 		 */
407 		BUG_ON(!error);
408 		/* This call to restore_highmem() undos the previous one */
409 		restore_highmem();
410 	}
411 	/*
412 	 * The only reason why swsusp_arch_resume() can fail is memory being
413 	 * very tight, so we have to free it as soon as we can to avoid
414 	 * subsequent failures
415 	 */
416 	swsusp_free();
417 	restore_processor_state();
418 	touch_softlockup_watchdog();
419 
420 	sysdev_resume();
421 
422  Enable_irqs:
423 	local_irq_enable();
424 
425  Enable_cpus:
426 	enable_nonboot_cpus();
427 
428  Cleanup:
429 	platform_restore_cleanup(platform_mode);
430 
431 	dpm_resume_noirq(PMSG_RECOVER);
432 
433 	return error;
434 }
435 
436 /**
437  *	hibernation_restore - quiesce devices and restore the hibernation
438  *	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  *	Must be called with pm_mutex held
443  */
444 
445 int hibernation_restore(int platform_mode)
446 {
447 	int error;
448 
449 	pm_prepare_console();
450 	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 	resume_console();
457 	pm_restore_console();
458 	return error;
459 }
460 
461 /**
462  *	hibernation_platform_enter - enter the hibernation state using the
463  *	platform driver (if available)
464  */
465 
466 int hibernation_platform_enter(void)
467 {
468 	int error;
469 
470 	if (!hibernation_ops)
471 		return -ENOSYS;
472 
473 	/*
474 	 * We have cancelled the power transition by running
475 	 * hibernation_ops->finish() before saving the image, so we should let
476 	 * the firmware know that we're going to enter the sleep state after all
477 	 */
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 	if (error) {
486 		if (hibernation_ops->recover)
487 			hibernation_ops->recover();
488 		goto Resume_devices;
489 	}
490 
491 	error = dpm_suspend_noirq(PMSG_HIBERNATE);
492 	if (error)
493 		goto Resume_devices;
494 
495 	error = hibernation_ops->prepare();
496 	if (error)
497 		goto Platform_finish;
498 
499 	error = disable_nonboot_cpus();
500 	if (error)
501 		goto Platform_finish;
502 
503 	local_irq_disable();
504 	sysdev_suspend(PMSG_HIBERNATE);
505 	hibernation_ops->enter();
506 	/* We should never get here */
507 	while (1);
508 
509 	/*
510 	 * We don't need to reenable the nonboot CPUs or resume consoles, since
511 	 * the system is going to be halted anyway.
512 	 */
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  Close:
524 	hibernation_ops->end();
525 
526 	return error;
527 }
528 
529 /**
530  *	power_down - Shut the machine down for hibernation.
531  *
532  *	Use the platform driver, if configured so; otherwise try
533  *	to power off or reboot.
534  */
535 
536 static void power_down(void)
537 {
538 	switch (hibernation_mode) {
539 	case HIBERNATION_TEST:
540 	case HIBERNATION_TESTPROC:
541 		break;
542 	case HIBERNATION_REBOOT:
543 		kernel_restart(NULL);
544 		break;
545 	case HIBERNATION_PLATFORM:
546 		hibernation_platform_enter();
547 	case HIBERNATION_SHUTDOWN:
548 		kernel_power_off();
549 		break;
550 	}
551 	kernel_halt();
552 	/*
553 	 * Valid image is on the disk, if we continue we risk serious data
554 	 * corruption after resume.
555 	 */
556 	printk(KERN_CRIT "PM: Please power down manually\n");
557 	while(1);
558 }
559 
560 static int prepare_processes(void)
561 {
562 	int error = 0;
563 
564 	if (freeze_processes()) {
565 		error = -EBUSY;
566 		thaw_processes();
567 	}
568 	return error;
569 }
570 
571 /**
572  *	hibernate - The granpappy of the built-in hibernation management
573  */
574 
575 int hibernate(void)
576 {
577 	int error;
578 
579 	mutex_lock(&pm_mutex);
580 	/* The snapshot device should not be opened while we're running */
581 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
582 		error = -EBUSY;
583 		goto Unlock;
584 	}
585 
586 	pm_prepare_console();
587 	error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
588 	if (error)
589 		goto Exit;
590 
591 	error = usermodehelper_disable();
592 	if (error)
593 		goto Exit;
594 
595 	/* Allocate memory management structures */
596 	error = create_basic_memory_bitmaps();
597 	if (error)
598 		goto Exit;
599 
600 	printk(KERN_INFO "PM: Syncing filesystems ... ");
601 	sys_sync();
602 	printk("done.\n");
603 
604 	error = prepare_processes();
605 	if (error)
606 		goto Finish;
607 
608 	if (hibernation_test(TEST_FREEZER))
609 		goto Thaw;
610 
611 	if (hibernation_testmode(HIBERNATION_TESTPROC))
612 		goto Thaw;
613 
614 	error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
615 	if (error)
616 		goto Thaw;
617 
618 	if (in_suspend) {
619 		unsigned int flags = 0;
620 
621 		if (hibernation_mode == HIBERNATION_PLATFORM)
622 			flags |= SF_PLATFORM_MODE;
623 		pr_debug("PM: writing image.\n");
624 		error = swsusp_write(flags);
625 		swsusp_free();
626 		if (!error)
627 			power_down();
628 	} else {
629 		pr_debug("PM: Image restored successfully.\n");
630 	}
631 
632  Thaw:
633 	thaw_processes();
634  Finish:
635 	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  Unlock:
642 	mutex_unlock(&pm_mutex);
643 	return error;
644 }
645 
646 
647 /**
648  *	software_resume - Resume from a saved image.
649  *
650  *	Called as a late_initcall (so all devices are discovered and
651  *	initialized), we call swsusp to see if we have a saved image or not.
652  *	If so, we quiesce devices, the restore the saved image. We will
653  *	return above (in hibernate() ) if everything goes well.
654  *	Otherwise, we fail gracefully and return to the normally
655  *	scheduled program.
656  *
657  */
658 
659 static int software_resume(void)
660 {
661 	int error;
662 	unsigned int flags;
663 
664 	/*
665 	 * If the user said "noresume".. bail out early.
666 	 */
667 	if (noresume)
668 		return 0;
669 
670 	/*
671 	 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
672 	 * is configured into the kernel. Since the regular hibernate
673 	 * trigger path is via sysfs which takes a buffer mutex before
674 	 * calling hibernate functions (which take pm_mutex) this can
675 	 * cause lockdep to complain about a possible ABBA deadlock
676 	 * which cannot happen since we're in the boot code here and
677 	 * sysfs can't be invoked yet. Therefore, we use a subclass
678 	 * here to avoid lockdep complaining.
679 	 */
680 	mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
681 
682 	if (swsusp_resume_device)
683 		goto Check_image;
684 
685 	if (!strlen(resume_file)) {
686 		error = -ENOENT;
687 		goto Unlock;
688 	}
689 
690 	pr_debug("PM: Checking image partition %s\n", resume_file);
691 
692 	/* Check if the device is there */
693 	swsusp_resume_device = name_to_dev_t(resume_file);
694 	if (!swsusp_resume_device) {
695 		/*
696 		 * Some device discovery might still be in progress; we need
697 		 * to wait for this to finish.
698 		 */
699 		wait_for_device_probe();
700 		/*
701 		 * We can't depend on SCSI devices being available after loading
702 		 * one of their modules until scsi_complete_async_scans() is
703 		 * called and the resume device usually is a SCSI one.
704 		 */
705 		scsi_complete_async_scans();
706 
707 		swsusp_resume_device = name_to_dev_t(resume_file);
708 		if (!swsusp_resume_device) {
709 			error = -ENODEV;
710 			goto Unlock;
711 		}
712 	}
713 
714  Check_image:
715 	pr_debug("PM: Resume from partition %d:%d\n",
716 		MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
717 
718 	pr_debug("PM: Checking hibernation image.\n");
719 	error = swsusp_check();
720 	if (error)
721 		goto Unlock;
722 
723 	/* The snapshot device should not be opened while we're running */
724 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
725 		error = -EBUSY;
726 		swsusp_close(FMODE_READ);
727 		goto Unlock;
728 	}
729 
730 	pm_prepare_console();
731 	error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
732 	if (error)
733 		goto close_finish;
734 
735 	error = usermodehelper_disable();
736 	if (error)
737 		goto close_finish;
738 
739 	error = create_basic_memory_bitmaps();
740 	if (error)
741 		goto close_finish;
742 
743 	pr_debug("PM: Preparing processes for restore.\n");
744 	error = prepare_processes();
745 	if (error) {
746 		swsusp_close(FMODE_READ);
747 		goto Done;
748 	}
749 
750 	pr_debug("PM: Reading hibernation image.\n");
751 
752 	error = swsusp_read(&flags);
753 	swsusp_close(FMODE_READ);
754 	if (!error)
755 		hibernation_restore(flags & SF_PLATFORM_MODE);
756 
757 	printk(KERN_ERR "PM: Restore failed, recovering.\n");
758 	swsusp_free();
759 	thaw_processes();
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);
770 	pr_debug("PM: Resume from disk failed.\n");
771 	return error;
772 close_finish:
773 	swsusp_close(FMODE_READ);
774 	goto Finish;
775 }
776 
777 late_initcall(software_resume);
778 
779 
780 static const char * const hibernation_modes[] = {
781 	[HIBERNATION_PLATFORM]	= "platform",
782 	[HIBERNATION_SHUTDOWN]	= "shutdown",
783 	[HIBERNATION_REBOOT]	= "reboot",
784 	[HIBERNATION_TEST]	= "test",
785 	[HIBERNATION_TESTPROC]	= "testproc",
786 };
787 
788 /**
789  *	disk - Control hibernation mode
790  *
791  *	Suspend-to-disk can be handled in several ways. We have a few options
792  *	for putting the system to sleep - using the platform driver (e.g. ACPI
793  *	or other hibernation_ops), powering off the system or rebooting the
794  *	system (for testing) as well as the two test modes.
795  *
796  *	The system can support 'platform', and that is known a priori (and
797  *	encoded by the presence of hibernation_ops). However, the user may
798  *	choose 'shutdown' or 'reboot' as alternatives, as well as one fo the
799  *	test modes, 'test' or 'testproc'.
800  *
801  *	show() will display what the mode is currently set to.
802  *	store() will accept one of
803  *
804  *	'platform'
805  *	'shutdown'
806  *	'reboot'
807  *	'test'
808  *	'testproc'
809  *
810  *	It will only change to 'platform' if the system
811  *	supports it (as determined by having hibernation_ops).
812  */
813 
814 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
815 			 char *buf)
816 {
817 	int i;
818 	char *start = buf;
819 
820 	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
821 		if (!hibernation_modes[i])
822 			continue;
823 		switch (i) {
824 		case HIBERNATION_SHUTDOWN:
825 		case HIBERNATION_REBOOT:
826 		case HIBERNATION_TEST:
827 		case HIBERNATION_TESTPROC:
828 			break;
829 		case HIBERNATION_PLATFORM:
830 			if (hibernation_ops)
831 				break;
832 			/* not a valid mode, continue with loop */
833 			continue;
834 		}
835 		if (i == hibernation_mode)
836 			buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
837 		else
838 			buf += sprintf(buf, "%s ", hibernation_modes[i]);
839 	}
840 	buf += sprintf(buf, "\n");
841 	return buf-start;
842 }
843 
844 
845 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
846 			  const char *buf, size_t n)
847 {
848 	int error = 0;
849 	int i;
850 	int len;
851 	char *p;
852 	int mode = HIBERNATION_INVALID;
853 
854 	p = memchr(buf, '\n', n);
855 	len = p ? p - buf : n;
856 
857 	mutex_lock(&pm_mutex);
858 	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
859 		if (len == strlen(hibernation_modes[i])
860 		    && !strncmp(buf, hibernation_modes[i], len)) {
861 			mode = i;
862 			break;
863 		}
864 	}
865 	if (mode != HIBERNATION_INVALID) {
866 		switch (mode) {
867 		case HIBERNATION_SHUTDOWN:
868 		case HIBERNATION_REBOOT:
869 		case HIBERNATION_TEST:
870 		case HIBERNATION_TESTPROC:
871 			hibernation_mode = mode;
872 			break;
873 		case HIBERNATION_PLATFORM:
874 			if (hibernation_ops)
875 				hibernation_mode = mode;
876 			else
877 				error = -EINVAL;
878 		}
879 	} else
880 		error = -EINVAL;
881 
882 	if (!error)
883 		pr_debug("PM: Hibernation mode set to '%s'\n",
884 			 hibernation_modes[mode]);
885 	mutex_unlock(&pm_mutex);
886 	return error ? error : n;
887 }
888 
889 power_attr(disk);
890 
891 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
892 			   char *buf)
893 {
894 	return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
895 		       MINOR(swsusp_resume_device));
896 }
897 
898 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
899 			    const char *buf, size_t n)
900 {
901 	unsigned int maj, min;
902 	dev_t res;
903 	int ret = -EINVAL;
904 
905 	if (sscanf(buf, "%u:%u", &maj, &min) != 2)
906 		goto out;
907 
908 	res = MKDEV(maj,min);
909 	if (maj != MAJOR(res) || min != MINOR(res))
910 		goto out;
911 
912 	mutex_lock(&pm_mutex);
913 	swsusp_resume_device = res;
914 	mutex_unlock(&pm_mutex);
915 	printk(KERN_INFO "PM: Starting manual resume from disk\n");
916 	noresume = 0;
917 	software_resume();
918 	ret = n;
919  out:
920 	return ret;
921 }
922 
923 power_attr(resume);
924 
925 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
926 			       char *buf)
927 {
928 	return sprintf(buf, "%lu\n", image_size);
929 }
930 
931 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
932 				const char *buf, size_t n)
933 {
934 	unsigned long size;
935 
936 	if (sscanf(buf, "%lu", &size) == 1) {
937 		image_size = size;
938 		return n;
939 	}
940 
941 	return -EINVAL;
942 }
943 
944 power_attr(image_size);
945 
946 static struct attribute * g[] = {
947 	&disk_attr.attr,
948 	&resume_attr.attr,
949 	&image_size_attr.attr,
950 	NULL,
951 };
952 
953 
954 static struct attribute_group attr_group = {
955 	.attrs = g,
956 };
957 
958 
959 static int __init pm_disk_init(void)
960 {
961 	return sysfs_create_group(power_kobj, &attr_group);
962 }
963 
964 core_initcall(pm_disk_init);
965 
966 
967 static int __init resume_setup(char *str)
968 {
969 	if (noresume)
970 		return 1;
971 
972 	strncpy( resume_file, str, 255 );
973 	return 1;
974 }
975 
976 static int __init resume_offset_setup(char *str)
977 {
978 	unsigned long long offset;
979 
980 	if (noresume)
981 		return 1;
982 
983 	if (sscanf(str, "%llu", &offset) == 1)
984 		swsusp_resume_block = offset;
985 
986 	return 1;
987 }
988 
989 static int __init noresume_setup(char *str)
990 {
991 	noresume = 1;
992 	return 1;
993 }
994 
995 __setup("noresume", noresume_setup);
996 __setup("resume_offset=", resume_offset_setup);
997 __setup("resume=", resume_setup);
998