xref: /linux/kernel/power/hibernate.c (revision a6e15a39048ec3229b9a53425f4384f55f6cc1b3)
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@ucw.cz>
7  * Copyright (c) 2009 Rafael J. Wysocki, Novell Inc.
8  * Copyright (C) 2012 Bojan Smojver <bojan@rexursive.com>
9  *
10  * This file is released under the GPLv2.
11  */
12 
13 #include <linux/export.h>
14 #include <linux/suspend.h>
15 #include <linux/syscalls.h>
16 #include <linux/reboot.h>
17 #include <linux/string.h>
18 #include <linux/device.h>
19 #include <linux/async.h>
20 #include <linux/delay.h>
21 #include <linux/fs.h>
22 #include <linux/mount.h>
23 #include <linux/pm.h>
24 #include <linux/console.h>
25 #include <linux/cpu.h>
26 #include <linux/freezer.h>
27 #include <linux/gfp.h>
28 #include <linux/syscore_ops.h>
29 #include <linux/ctype.h>
30 #include <linux/genhd.h>
31 #include <trace/events/power.h>
32 
33 #include "power.h"
34 
35 
36 static int nocompress;
37 static int noresume;
38 static int nohibernate;
39 static int resume_wait;
40 static unsigned int resume_delay;
41 static char resume_file[256] = CONFIG_PM_STD_PARTITION;
42 dev_t swsusp_resume_device;
43 sector_t swsusp_resume_block;
44 __visible int in_suspend __nosavedata;
45 
46 enum {
47 	HIBERNATION_INVALID,
48 	HIBERNATION_PLATFORM,
49 	HIBERNATION_SHUTDOWN,
50 	HIBERNATION_REBOOT,
51 #ifdef CONFIG_SUSPEND
52 	HIBERNATION_SUSPEND,
53 #endif
54 	/* keep last */
55 	__HIBERNATION_AFTER_LAST
56 };
57 #define HIBERNATION_MAX (__HIBERNATION_AFTER_LAST-1)
58 #define HIBERNATION_FIRST (HIBERNATION_INVALID + 1)
59 
60 static int hibernation_mode = HIBERNATION_SHUTDOWN;
61 
62 bool freezer_test_done;
63 
64 static const struct platform_hibernation_ops *hibernation_ops;
65 
66 bool hibernation_available(void)
67 {
68 	return (nohibernate == 0);
69 }
70 
71 /**
72  * hibernation_set_ops - Set the global hibernate operations.
73  * @ops: Hibernation operations to use in subsequent hibernation transitions.
74  */
75 void hibernation_set_ops(const struct platform_hibernation_ops *ops)
76 {
77 	if (ops && !(ops->begin && ops->end &&  ops->pre_snapshot
78 	    && ops->prepare && ops->finish && ops->enter && ops->pre_restore
79 	    && ops->restore_cleanup && ops->leave)) {
80 		WARN_ON(1);
81 		return;
82 	}
83 	lock_system_sleep();
84 	hibernation_ops = ops;
85 	if (ops)
86 		hibernation_mode = HIBERNATION_PLATFORM;
87 	else if (hibernation_mode == HIBERNATION_PLATFORM)
88 		hibernation_mode = HIBERNATION_SHUTDOWN;
89 
90 	unlock_system_sleep();
91 }
92 EXPORT_SYMBOL_GPL(hibernation_set_ops);
93 
94 static bool entering_platform_hibernation;
95 
96 bool system_entering_hibernation(void)
97 {
98 	return entering_platform_hibernation;
99 }
100 EXPORT_SYMBOL(system_entering_hibernation);
101 
102 #ifdef CONFIG_PM_DEBUG
103 static void hibernation_debug_sleep(void)
104 {
105 	printk(KERN_INFO "hibernation debug: Waiting for 5 seconds.\n");
106 	mdelay(5000);
107 }
108 
109 static int hibernation_test(int level)
110 {
111 	if (pm_test_level == level) {
112 		hibernation_debug_sleep();
113 		return 1;
114 	}
115 	return 0;
116 }
117 #else /* !CONFIG_PM_DEBUG */
118 static int hibernation_test(int level) { return 0; }
119 #endif /* !CONFIG_PM_DEBUG */
120 
121 /**
122  * platform_begin - Call platform to start hibernation.
123  * @platform_mode: Whether or not to use the platform driver.
124  */
125 static int platform_begin(int platform_mode)
126 {
127 	return (platform_mode && hibernation_ops) ?
128 		hibernation_ops->begin() : 0;
129 }
130 
131 /**
132  * platform_end - Call platform to finish transition to the working state.
133  * @platform_mode: Whether or not to use the platform driver.
134  */
135 static void platform_end(int platform_mode)
136 {
137 	if (platform_mode && hibernation_ops)
138 		hibernation_ops->end();
139 }
140 
141 /**
142  * platform_pre_snapshot - Call platform to prepare the machine for hibernation.
143  * @platform_mode: Whether or not to use the platform driver.
144  *
145  * Use the platform driver to prepare the system for creating a hibernate image,
146  * if so configured, and return an error code if that fails.
147  */
148 
149 static int platform_pre_snapshot(int platform_mode)
150 {
151 	return (platform_mode && hibernation_ops) ?
152 		hibernation_ops->pre_snapshot() : 0;
153 }
154 
155 /**
156  * platform_leave - Call platform to prepare a transition to the working state.
157  * @platform_mode: Whether or not to use the platform driver.
158  *
159  * Use the platform driver prepare to prepare the machine for switching to the
160  * normal mode of operation.
161  *
162  * This routine is called on one CPU with interrupts disabled.
163  */
164 static void platform_leave(int platform_mode)
165 {
166 	if (platform_mode && hibernation_ops)
167 		hibernation_ops->leave();
168 }
169 
170 /**
171  * platform_finish - Call platform to switch the system to the working state.
172  * @platform_mode: Whether or not to use the platform driver.
173  *
174  * Use the platform driver to switch the machine to the normal mode of
175  * operation.
176  *
177  * This routine must be called after platform_prepare().
178  */
179 static void platform_finish(int platform_mode)
180 {
181 	if (platform_mode && hibernation_ops)
182 		hibernation_ops->finish();
183 }
184 
185 /**
186  * platform_pre_restore - Prepare for hibernate image restoration.
187  * @platform_mode: Whether or not to use the platform driver.
188  *
189  * Use the platform driver to prepare the system for resume from a hibernation
190  * image.
191  *
192  * If the restore fails after this function has been called,
193  * platform_restore_cleanup() must be called.
194  */
195 static int platform_pre_restore(int platform_mode)
196 {
197 	return (platform_mode && hibernation_ops) ?
198 		hibernation_ops->pre_restore() : 0;
199 }
200 
201 /**
202  * platform_restore_cleanup - Switch to the working state after failing restore.
203  * @platform_mode: Whether or not to use the platform driver.
204  *
205  * Use the platform driver to switch the system to the normal mode of operation
206  * after a failing restore.
207  *
208  * If platform_pre_restore() has been called before the failing restore, this
209  * function must be called too, regardless of the result of
210  * platform_pre_restore().
211  */
212 static void platform_restore_cleanup(int platform_mode)
213 {
214 	if (platform_mode && hibernation_ops)
215 		hibernation_ops->restore_cleanup();
216 }
217 
218 /**
219  * platform_recover - Recover from a failure to suspend devices.
220  * @platform_mode: Whether or not to use the platform driver.
221  */
222 static void platform_recover(int platform_mode)
223 {
224 	if (platform_mode && hibernation_ops && hibernation_ops->recover)
225 		hibernation_ops->recover();
226 }
227 
228 /**
229  * swsusp_show_speed - Print time elapsed between two events during hibernation.
230  * @start: Starting event.
231  * @stop: Final event.
232  * @nr_pages: Number of memory pages processed between @start and @stop.
233  * @msg: Additional diagnostic message to print.
234  */
235 void swsusp_show_speed(struct timeval *start, struct timeval *stop,
236 			unsigned nr_pages, char *msg)
237 {
238 	u64 elapsed_centisecs64;
239 	unsigned int centisecs;
240 	unsigned int k;
241 	unsigned int kps;
242 
243 	elapsed_centisecs64 = timeval_to_ns(stop) - timeval_to_ns(start);
244 	/*
245 	 * If "(s64)elapsed_centisecs64 < 0", it will print long elapsed time,
246 	 * it is obvious enough for what went wrong.
247 	 */
248 	do_div(elapsed_centisecs64, NSEC_PER_SEC / 100);
249 	centisecs = elapsed_centisecs64;
250 	if (centisecs == 0)
251 		centisecs = 1;	/* avoid div-by-zero */
252 	k = nr_pages * (PAGE_SIZE / 1024);
253 	kps = (k * 100) / centisecs;
254 	printk(KERN_INFO "PM: %s %u kbytes in %u.%02u seconds (%u.%02u MB/s)\n",
255 			msg, k,
256 			centisecs / 100, centisecs % 100,
257 			kps / 1000, (kps % 1000) / 10);
258 }
259 
260 /**
261  * create_image - Create a hibernation image.
262  * @platform_mode: Whether or not to use the platform driver.
263  *
264  * Execute device drivers' "late" and "noirq" freeze callbacks, create a
265  * hibernation image and run the drivers' "noirq" and "early" thaw callbacks.
266  *
267  * Control reappears in this routine after the subsequent restore.
268  */
269 static int create_image(int platform_mode)
270 {
271 	int error;
272 
273 	error = dpm_suspend_end(PMSG_FREEZE);
274 	if (error) {
275 		printk(KERN_ERR "PM: Some devices failed to power down, "
276 			"aborting hibernation\n");
277 		return error;
278 	}
279 
280 	error = platform_pre_snapshot(platform_mode);
281 	if (error || hibernation_test(TEST_PLATFORM))
282 		goto Platform_finish;
283 
284 	error = disable_nonboot_cpus();
285 	if (error || hibernation_test(TEST_CPUS))
286 		goto Enable_cpus;
287 
288 	local_irq_disable();
289 
290 	error = syscore_suspend();
291 	if (error) {
292 		printk(KERN_ERR "PM: Some system devices failed to power down, "
293 			"aborting hibernation\n");
294 		goto Enable_irqs;
295 	}
296 
297 	if (hibernation_test(TEST_CORE) || pm_wakeup_pending())
298 		goto Power_up;
299 
300 	in_suspend = 1;
301 	save_processor_state();
302 	trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, true);
303 	error = swsusp_arch_suspend();
304 	trace_suspend_resume(TPS("machine_suspend"), PM_EVENT_HIBERNATE, false);
305 	if (error)
306 		printk(KERN_ERR "PM: Error %d creating hibernation image\n",
307 			error);
308 	/* Restore control flow magically appears here */
309 	restore_processor_state();
310 	if (!in_suspend)
311 		events_check_enabled = false;
312 
313 	platform_leave(platform_mode);
314 
315  Power_up:
316 	syscore_resume();
317 
318  Enable_irqs:
319 	local_irq_enable();
320 
321  Enable_cpus:
322 	enable_nonboot_cpus();
323 
324  Platform_finish:
325 	platform_finish(platform_mode);
326 
327 	dpm_resume_start(in_suspend ?
328 		(error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE);
329 
330 	return error;
331 }
332 
333 /**
334  * hibernation_snapshot - Quiesce devices and create a hibernation image.
335  * @platform_mode: If set, use platform driver to prepare for the transition.
336  *
337  * This routine must be called with pm_mutex held.
338  */
339 int hibernation_snapshot(int platform_mode)
340 {
341 	pm_message_t msg;
342 	int error;
343 
344 	error = platform_begin(platform_mode);
345 	if (error)
346 		goto Close;
347 
348 	/* Preallocate image memory before shutting down devices. */
349 	error = hibernate_preallocate_memory();
350 	if (error)
351 		goto Close;
352 
353 	error = freeze_kernel_threads();
354 	if (error)
355 		goto Cleanup;
356 
357 	if (hibernation_test(TEST_FREEZER)) {
358 
359 		/*
360 		 * Indicate to the caller that we are returning due to a
361 		 * successful freezer test.
362 		 */
363 		freezer_test_done = true;
364 		goto Thaw;
365 	}
366 
367 	error = dpm_prepare(PMSG_FREEZE);
368 	if (error) {
369 		dpm_complete(PMSG_RECOVER);
370 		goto Thaw;
371 	}
372 
373 	suspend_console();
374 	ftrace_stop();
375 	pm_restrict_gfp_mask();
376 
377 	error = dpm_suspend(PMSG_FREEZE);
378 
379 	if (error || hibernation_test(TEST_DEVICES))
380 		platform_recover(platform_mode);
381 	else
382 		error = create_image(platform_mode);
383 
384 	/*
385 	 * In the case that we call create_image() above, the control
386 	 * returns here (1) after the image has been created or the
387 	 * image creation has failed and (2) after a successful restore.
388 	 */
389 
390 	/* We may need to release the preallocated image pages here. */
391 	if (error || !in_suspend)
392 		swsusp_free();
393 
394 	msg = in_suspend ? (error ? PMSG_RECOVER : PMSG_THAW) : PMSG_RESTORE;
395 	dpm_resume(msg);
396 
397 	if (error || !in_suspend)
398 		pm_restore_gfp_mask();
399 
400 	ftrace_start();
401 	resume_console();
402 	dpm_complete(msg);
403 
404  Close:
405 	platform_end(platform_mode);
406 	return error;
407 
408  Thaw:
409 	thaw_kernel_threads();
410  Cleanup:
411 	swsusp_free();
412 	goto Close;
413 }
414 
415 /**
416  * resume_target_kernel - Restore system state from a hibernation image.
417  * @platform_mode: Whether or not to use the platform driver.
418  *
419  * Execute device drivers' "noirq" and "late" freeze callbacks, restore the
420  * contents of highmem that have not been restored yet from the image and run
421  * the low-level code that will restore the remaining contents of memory and
422  * switch to the just restored target kernel.
423  */
424 static int resume_target_kernel(bool platform_mode)
425 {
426 	int error;
427 
428 	error = dpm_suspend_end(PMSG_QUIESCE);
429 	if (error) {
430 		printk(KERN_ERR "PM: Some devices failed to power down, "
431 			"aborting resume\n");
432 		return error;
433 	}
434 
435 	error = platform_pre_restore(platform_mode);
436 	if (error)
437 		goto Cleanup;
438 
439 	error = disable_nonboot_cpus();
440 	if (error)
441 		goto Enable_cpus;
442 
443 	local_irq_disable();
444 
445 	error = syscore_suspend();
446 	if (error)
447 		goto Enable_irqs;
448 
449 	save_processor_state();
450 	error = restore_highmem();
451 	if (!error) {
452 		error = swsusp_arch_resume();
453 		/*
454 		 * The code below is only ever reached in case of a failure.
455 		 * Otherwise, execution continues at the place where
456 		 * swsusp_arch_suspend() was called.
457 		 */
458 		BUG_ON(!error);
459 		/*
460 		 * This call to restore_highmem() reverts the changes made by
461 		 * the previous one.
462 		 */
463 		restore_highmem();
464 	}
465 	/*
466 	 * The only reason why swsusp_arch_resume() can fail is memory being
467 	 * very tight, so we have to free it as soon as we can to avoid
468 	 * subsequent failures.
469 	 */
470 	swsusp_free();
471 	restore_processor_state();
472 	touch_softlockup_watchdog();
473 
474 	syscore_resume();
475 
476  Enable_irqs:
477 	local_irq_enable();
478 
479  Enable_cpus:
480 	enable_nonboot_cpus();
481 
482  Cleanup:
483 	platform_restore_cleanup(platform_mode);
484 
485 	dpm_resume_start(PMSG_RECOVER);
486 
487 	return error;
488 }
489 
490 /**
491  * hibernation_restore - Quiesce devices and restore from a hibernation image.
492  * @platform_mode: If set, use platform driver to prepare for the transition.
493  *
494  * This routine must be called with pm_mutex held.  If it is successful, control
495  * reappears in the restored target kernel in hibernation_snapshot().
496  */
497 int hibernation_restore(int platform_mode)
498 {
499 	int error;
500 
501 	pm_prepare_console();
502 	suspend_console();
503 	ftrace_stop();
504 	pm_restrict_gfp_mask();
505 	error = dpm_suspend_start(PMSG_QUIESCE);
506 	if (!error) {
507 		error = resume_target_kernel(platform_mode);
508 		dpm_resume_end(PMSG_RECOVER);
509 	}
510 	pm_restore_gfp_mask();
511 	ftrace_start();
512 	resume_console();
513 	pm_restore_console();
514 	return error;
515 }
516 
517 /**
518  * hibernation_platform_enter - Power off the system using the platform driver.
519  */
520 int hibernation_platform_enter(void)
521 {
522 	int error;
523 
524 	if (!hibernation_ops)
525 		return -ENOSYS;
526 
527 	/*
528 	 * We have cancelled the power transition by running
529 	 * hibernation_ops->finish() before saving the image, so we should let
530 	 * the firmware know that we're going to enter the sleep state after all
531 	 */
532 	error = hibernation_ops->begin();
533 	if (error)
534 		goto Close;
535 
536 	entering_platform_hibernation = true;
537 	suspend_console();
538 	ftrace_stop();
539 	error = dpm_suspend_start(PMSG_HIBERNATE);
540 	if (error) {
541 		if (hibernation_ops->recover)
542 			hibernation_ops->recover();
543 		goto Resume_devices;
544 	}
545 
546 	error = dpm_suspend_end(PMSG_HIBERNATE);
547 	if (error)
548 		goto Resume_devices;
549 
550 	error = hibernation_ops->prepare();
551 	if (error)
552 		goto Platform_finish;
553 
554 	error = disable_nonboot_cpus();
555 	if (error)
556 		goto Platform_finish;
557 
558 	local_irq_disable();
559 	syscore_suspend();
560 	if (pm_wakeup_pending()) {
561 		error = -EAGAIN;
562 		goto Power_up;
563 	}
564 
565 	hibernation_ops->enter();
566 	/* We should never get here */
567 	while (1);
568 
569  Power_up:
570 	syscore_resume();
571 	local_irq_enable();
572 	enable_nonboot_cpus();
573 
574  Platform_finish:
575 	hibernation_ops->finish();
576 
577 	dpm_resume_start(PMSG_RESTORE);
578 
579  Resume_devices:
580 	entering_platform_hibernation = false;
581 	dpm_resume_end(PMSG_RESTORE);
582 	ftrace_start();
583 	resume_console();
584 
585  Close:
586 	hibernation_ops->end();
587 
588 	return error;
589 }
590 
591 /**
592  * power_down - Shut the machine down for hibernation.
593  *
594  * Use the platform driver, if configured, to put the system into the sleep
595  * state corresponding to hibernation, or try to power it off or reboot,
596  * depending on the value of hibernation_mode.
597  */
598 static void power_down(void)
599 {
600 #ifdef CONFIG_SUSPEND
601 	int error;
602 #endif
603 
604 	switch (hibernation_mode) {
605 	case HIBERNATION_REBOOT:
606 		kernel_restart(NULL);
607 		break;
608 	case HIBERNATION_PLATFORM:
609 		hibernation_platform_enter();
610 	case HIBERNATION_SHUTDOWN:
611 		if (pm_power_off)
612 			kernel_power_off();
613 		break;
614 #ifdef CONFIG_SUSPEND
615 	case HIBERNATION_SUSPEND:
616 		error = suspend_devices_and_enter(PM_SUSPEND_MEM);
617 		if (error) {
618 			if (hibernation_ops)
619 				hibernation_mode = HIBERNATION_PLATFORM;
620 			else
621 				hibernation_mode = HIBERNATION_SHUTDOWN;
622 			power_down();
623 		}
624 		/*
625 		 * Restore swap signature.
626 		 */
627 		error = swsusp_unmark();
628 		if (error)
629 			printk(KERN_ERR "PM: Swap will be unusable! "
630 			                "Try swapon -a.\n");
631 		return;
632 #endif
633 	}
634 	kernel_halt();
635 	/*
636 	 * Valid image is on the disk, if we continue we risk serious data
637 	 * corruption after resume.
638 	 */
639 	printk(KERN_CRIT "PM: Please power down manually\n");
640 	while (1)
641 		cpu_relax();
642 }
643 
644 /**
645  * hibernate - Carry out system hibernation, including saving the image.
646  */
647 int hibernate(void)
648 {
649 	int error;
650 
651 	if (!hibernation_available()) {
652 		pr_debug("PM: Hibernation not available.\n");
653 		return -EPERM;
654 	}
655 
656 	lock_system_sleep();
657 	/* The snapshot device should not be opened while we're running */
658 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
659 		error = -EBUSY;
660 		goto Unlock;
661 	}
662 
663 	pm_prepare_console();
664 	error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE);
665 	if (error)
666 		goto Exit;
667 
668 	printk(KERN_INFO "PM: Syncing filesystems ... ");
669 	sys_sync();
670 	printk("done.\n");
671 
672 	error = freeze_processes();
673 	if (error)
674 		goto Exit;
675 
676 	lock_device_hotplug();
677 	/* Allocate memory management structures */
678 	error = create_basic_memory_bitmaps();
679 	if (error)
680 		goto Thaw;
681 
682 	error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM);
683 	if (error || freezer_test_done)
684 		goto Free_bitmaps;
685 
686 	if (in_suspend) {
687 		unsigned int flags = 0;
688 
689 		if (hibernation_mode == HIBERNATION_PLATFORM)
690 			flags |= SF_PLATFORM_MODE;
691 		if (nocompress)
692 			flags |= SF_NOCOMPRESS_MODE;
693 		else
694 		        flags |= SF_CRC32_MODE;
695 
696 		pr_debug("PM: writing image.\n");
697 		error = swsusp_write(flags);
698 		swsusp_free();
699 		if (!error)
700 			power_down();
701 		in_suspend = 0;
702 		pm_restore_gfp_mask();
703 	} else {
704 		pr_debug("PM: Image restored successfully.\n");
705 	}
706 
707  Free_bitmaps:
708 	free_basic_memory_bitmaps();
709  Thaw:
710 	unlock_device_hotplug();
711 	thaw_processes();
712 
713 	/* Don't bother checking whether freezer_test_done is true */
714 	freezer_test_done = false;
715  Exit:
716 	pm_notifier_call_chain(PM_POST_HIBERNATION);
717 	pm_restore_console();
718 	atomic_inc(&snapshot_device_available);
719  Unlock:
720 	unlock_system_sleep();
721 	return error;
722 }
723 
724 
725 /**
726  * software_resume - Resume from a saved hibernation image.
727  *
728  * This routine is called as a late initcall, when all devices have been
729  * discovered and initialized already.
730  *
731  * The image reading code is called to see if there is a hibernation image
732  * available for reading.  If that is the case, devices are quiesced and the
733  * contents of memory is restored from the saved image.
734  *
735  * If this is successful, control reappears in the restored target kernel in
736  * hibernation_snaphot() which returns to hibernate().  Otherwise, the routine
737  * attempts to recover gracefully and make the kernel return to the normal mode
738  * of operation.
739  */
740 static int software_resume(void)
741 {
742 	int error;
743 	unsigned int flags;
744 
745 	/*
746 	 * If the user said "noresume".. bail out early.
747 	 */
748 	if (noresume || !hibernation_available())
749 		return 0;
750 
751 	/*
752 	 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs
753 	 * is configured into the kernel. Since the regular hibernate
754 	 * trigger path is via sysfs which takes a buffer mutex before
755 	 * calling hibernate functions (which take pm_mutex) this can
756 	 * cause lockdep to complain about a possible ABBA deadlock
757 	 * which cannot happen since we're in the boot code here and
758 	 * sysfs can't be invoked yet. Therefore, we use a subclass
759 	 * here to avoid lockdep complaining.
760 	 */
761 	mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING);
762 
763 	if (swsusp_resume_device)
764 		goto Check_image;
765 
766 	if (!strlen(resume_file)) {
767 		error = -ENOENT;
768 		goto Unlock;
769 	}
770 
771 	pr_debug("PM: Checking hibernation image partition %s\n", resume_file);
772 
773 	if (resume_delay) {
774 		printk(KERN_INFO "Waiting %dsec before reading resume device...\n",
775 			resume_delay);
776 		ssleep(resume_delay);
777 	}
778 
779 	/* Check if the device is there */
780 	swsusp_resume_device = name_to_dev_t(resume_file);
781 
782 	/*
783 	 * name_to_dev_t is ineffective to verify parition if resume_file is in
784 	 * integer format. (e.g. major:minor)
785 	 */
786 	if (isdigit(resume_file[0]) && resume_wait) {
787 		int partno;
788 		while (!get_gendisk(swsusp_resume_device, &partno))
789 			msleep(10);
790 	}
791 
792 	if (!swsusp_resume_device) {
793 		/*
794 		 * Some device discovery might still be in progress; we need
795 		 * to wait for this to finish.
796 		 */
797 		wait_for_device_probe();
798 
799 		if (resume_wait) {
800 			while ((swsusp_resume_device = name_to_dev_t(resume_file)) == 0)
801 				msleep(10);
802 			async_synchronize_full();
803 		}
804 
805 		swsusp_resume_device = name_to_dev_t(resume_file);
806 		if (!swsusp_resume_device) {
807 			error = -ENODEV;
808 			goto Unlock;
809 		}
810 	}
811 
812  Check_image:
813 	pr_debug("PM: Hibernation image partition %d:%d present\n",
814 		MAJOR(swsusp_resume_device), MINOR(swsusp_resume_device));
815 
816 	pr_debug("PM: Looking for hibernation image.\n");
817 	error = swsusp_check();
818 	if (error)
819 		goto Unlock;
820 
821 	/* The snapshot device should not be opened while we're running */
822 	if (!atomic_add_unless(&snapshot_device_available, -1, 0)) {
823 		error = -EBUSY;
824 		swsusp_close(FMODE_READ);
825 		goto Unlock;
826 	}
827 
828 	pm_prepare_console();
829 	error = pm_notifier_call_chain(PM_RESTORE_PREPARE);
830 	if (error)
831 		goto Close_Finish;
832 
833 	pr_debug("PM: Preparing processes for restore.\n");
834 	error = freeze_processes();
835 	if (error)
836 		goto Close_Finish;
837 
838 	pr_debug("PM: Loading hibernation image.\n");
839 
840 	lock_device_hotplug();
841 	error = create_basic_memory_bitmaps();
842 	if (error)
843 		goto Thaw;
844 
845 	error = swsusp_read(&flags);
846 	swsusp_close(FMODE_READ);
847 	if (!error)
848 		hibernation_restore(flags & SF_PLATFORM_MODE);
849 
850 	printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n");
851 	swsusp_free();
852 	free_basic_memory_bitmaps();
853  Thaw:
854 	unlock_device_hotplug();
855 	thaw_processes();
856  Finish:
857 	pm_notifier_call_chain(PM_POST_RESTORE);
858 	pm_restore_console();
859 	atomic_inc(&snapshot_device_available);
860 	/* For success case, the suspend path will release the lock */
861  Unlock:
862 	mutex_unlock(&pm_mutex);
863 	pr_debug("PM: Hibernation image not present or could not be loaded.\n");
864 	return error;
865  Close_Finish:
866 	swsusp_close(FMODE_READ);
867 	goto Finish;
868 }
869 
870 late_initcall_sync(software_resume);
871 
872 
873 static const char * const hibernation_modes[] = {
874 	[HIBERNATION_PLATFORM]	= "platform",
875 	[HIBERNATION_SHUTDOWN]	= "shutdown",
876 	[HIBERNATION_REBOOT]	= "reboot",
877 #ifdef CONFIG_SUSPEND
878 	[HIBERNATION_SUSPEND]	= "suspend",
879 #endif
880 };
881 
882 /*
883  * /sys/power/disk - Control hibernation mode.
884  *
885  * Hibernation can be handled in several ways.  There are a few different ways
886  * to put the system into the sleep state: using the platform driver (e.g. ACPI
887  * or other hibernation_ops), powering it off or rebooting it (for testing
888  * mostly).
889  *
890  * The sysfs file /sys/power/disk provides an interface for selecting the
891  * hibernation mode to use.  Reading from this file causes the available modes
892  * to be printed.  There are 3 modes that can be supported:
893  *
894  *	'platform'
895  *	'shutdown'
896  *	'reboot'
897  *
898  * If a platform hibernation driver is in use, 'platform' will be supported
899  * and will be used by default.  Otherwise, 'shutdown' will be used by default.
900  * The selected option (i.e. the one corresponding to the current value of
901  * hibernation_mode) is enclosed by a square bracket.
902  *
903  * To select a given hibernation mode it is necessary to write the mode's
904  * string representation (as returned by reading from /sys/power/disk) back
905  * into /sys/power/disk.
906  */
907 
908 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr,
909 			 char *buf)
910 {
911 	int i;
912 	char *start = buf;
913 
914 	if (!hibernation_available())
915 		return sprintf(buf, "[disabled]\n");
916 
917 	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
918 		if (!hibernation_modes[i])
919 			continue;
920 		switch (i) {
921 		case HIBERNATION_SHUTDOWN:
922 		case HIBERNATION_REBOOT:
923 #ifdef CONFIG_SUSPEND
924 		case HIBERNATION_SUSPEND:
925 #endif
926 			break;
927 		case HIBERNATION_PLATFORM:
928 			if (hibernation_ops)
929 				break;
930 			/* not a valid mode, continue with loop */
931 			continue;
932 		}
933 		if (i == hibernation_mode)
934 			buf += sprintf(buf, "[%s] ", hibernation_modes[i]);
935 		else
936 			buf += sprintf(buf, "%s ", hibernation_modes[i]);
937 	}
938 	buf += sprintf(buf, "\n");
939 	return buf-start;
940 }
941 
942 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr,
943 			  const char *buf, size_t n)
944 {
945 	int error = 0;
946 	int i;
947 	int len;
948 	char *p;
949 	int mode = HIBERNATION_INVALID;
950 
951 	if (!hibernation_available())
952 		return -EPERM;
953 
954 	p = memchr(buf, '\n', n);
955 	len = p ? p - buf : n;
956 
957 	lock_system_sleep();
958 	for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) {
959 		if (len == strlen(hibernation_modes[i])
960 		    && !strncmp(buf, hibernation_modes[i], len)) {
961 			mode = i;
962 			break;
963 		}
964 	}
965 	if (mode != HIBERNATION_INVALID) {
966 		switch (mode) {
967 		case HIBERNATION_SHUTDOWN:
968 		case HIBERNATION_REBOOT:
969 #ifdef CONFIG_SUSPEND
970 		case HIBERNATION_SUSPEND:
971 #endif
972 			hibernation_mode = mode;
973 			break;
974 		case HIBERNATION_PLATFORM:
975 			if (hibernation_ops)
976 				hibernation_mode = mode;
977 			else
978 				error = -EINVAL;
979 		}
980 	} else
981 		error = -EINVAL;
982 
983 	if (!error)
984 		pr_debug("PM: Hibernation mode set to '%s'\n",
985 			 hibernation_modes[mode]);
986 	unlock_system_sleep();
987 	return error ? error : n;
988 }
989 
990 power_attr(disk);
991 
992 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr,
993 			   char *buf)
994 {
995 	return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device),
996 		       MINOR(swsusp_resume_device));
997 }
998 
999 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr,
1000 			    const char *buf, size_t n)
1001 {
1002 	dev_t res;
1003 	int len = n;
1004 	char *name;
1005 
1006 	if (len && buf[len-1] == '\n')
1007 		len--;
1008 	name = kstrndup(buf, len, GFP_KERNEL);
1009 	if (!name)
1010 		return -ENOMEM;
1011 
1012 	res = name_to_dev_t(name);
1013 	kfree(name);
1014 	if (!res)
1015 		return -EINVAL;
1016 
1017 	lock_system_sleep();
1018 	swsusp_resume_device = res;
1019 	unlock_system_sleep();
1020 	printk(KERN_INFO "PM: Starting manual resume from disk\n");
1021 	noresume = 0;
1022 	software_resume();
1023 	return n;
1024 }
1025 
1026 power_attr(resume);
1027 
1028 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr,
1029 			       char *buf)
1030 {
1031 	return sprintf(buf, "%lu\n", image_size);
1032 }
1033 
1034 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr,
1035 				const char *buf, size_t n)
1036 {
1037 	unsigned long size;
1038 
1039 	if (sscanf(buf, "%lu", &size) == 1) {
1040 		image_size = size;
1041 		return n;
1042 	}
1043 
1044 	return -EINVAL;
1045 }
1046 
1047 power_attr(image_size);
1048 
1049 static ssize_t reserved_size_show(struct kobject *kobj,
1050 				  struct kobj_attribute *attr, char *buf)
1051 {
1052 	return sprintf(buf, "%lu\n", reserved_size);
1053 }
1054 
1055 static ssize_t reserved_size_store(struct kobject *kobj,
1056 				   struct kobj_attribute *attr,
1057 				   const char *buf, size_t n)
1058 {
1059 	unsigned long size;
1060 
1061 	if (sscanf(buf, "%lu", &size) == 1) {
1062 		reserved_size = size;
1063 		return n;
1064 	}
1065 
1066 	return -EINVAL;
1067 }
1068 
1069 power_attr(reserved_size);
1070 
1071 static struct attribute * g[] = {
1072 	&disk_attr.attr,
1073 	&resume_attr.attr,
1074 	&image_size_attr.attr,
1075 	&reserved_size_attr.attr,
1076 	NULL,
1077 };
1078 
1079 
1080 static struct attribute_group attr_group = {
1081 	.attrs = g,
1082 };
1083 
1084 
1085 static int __init pm_disk_init(void)
1086 {
1087 	return sysfs_create_group(power_kobj, &attr_group);
1088 }
1089 
1090 core_initcall(pm_disk_init);
1091 
1092 
1093 static int __init resume_setup(char *str)
1094 {
1095 	if (noresume)
1096 		return 1;
1097 
1098 	strncpy( resume_file, str, 255 );
1099 	return 1;
1100 }
1101 
1102 static int __init resume_offset_setup(char *str)
1103 {
1104 	unsigned long long offset;
1105 
1106 	if (noresume)
1107 		return 1;
1108 
1109 	if (sscanf(str, "%llu", &offset) == 1)
1110 		swsusp_resume_block = offset;
1111 
1112 	return 1;
1113 }
1114 
1115 static int __init hibernate_setup(char *str)
1116 {
1117 	if (!strncmp(str, "noresume", 8))
1118 		noresume = 1;
1119 	else if (!strncmp(str, "nocompress", 10))
1120 		nocompress = 1;
1121 	else if (!strncmp(str, "no", 2)) {
1122 		noresume = 1;
1123 		nohibernate = 1;
1124 	}
1125 	return 1;
1126 }
1127 
1128 static int __init noresume_setup(char *str)
1129 {
1130 	noresume = 1;
1131 	return 1;
1132 }
1133 
1134 static int __init resumewait_setup(char *str)
1135 {
1136 	resume_wait = 1;
1137 	return 1;
1138 }
1139 
1140 static int __init resumedelay_setup(char *str)
1141 {
1142 	int rc = kstrtouint(str, 0, &resume_delay);
1143 
1144 	if (rc)
1145 		return rc;
1146 	return 1;
1147 }
1148 
1149 static int __init nohibernate_setup(char *str)
1150 {
1151 	noresume = 1;
1152 	nohibernate = 1;
1153 	return 1;
1154 }
1155 
1156 __setup("noresume", noresume_setup);
1157 __setup("resume_offset=", resume_offset_setup);
1158 __setup("resume=", resume_setup);
1159 __setup("hibernate=", hibernate_setup);
1160 __setup("resumewait", resumewait_setup);
1161 __setup("resumedelay=", resumedelay_setup);
1162 __setup("nohibernate", nohibernate_setup);
1163