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