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