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