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