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