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