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