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; 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(PMSG_RECOVER); 366 goto Cleanup; 367 } 368 369 suspend_console(); 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 resume_console(); 396 dpm_complete(msg); 397 398 Close: 399 platform_end(platform_mode); 400 return error; 401 402 Cleanup: 403 swsusp_free(); 404 goto Close; 405 } 406 407 /** 408 * resume_target_kernel - Restore system state from a hibernation image. 409 * @platform_mode: Whether or not to use the platform driver. 410 * 411 * Execute device drivers' .freeze_noirq() callbacks, restore the contents of 412 * highmem that have not been restored yet from the image and run the low-level 413 * code that will restore the remaining contents of memory and switch to the 414 * just restored target kernel. 415 */ 416 static int resume_target_kernel(bool platform_mode) 417 { 418 int error; 419 420 error = dpm_suspend_noirq(PMSG_QUIESCE); 421 if (error) { 422 printk(KERN_ERR "PM: Some devices failed to power down, " 423 "aborting resume\n"); 424 return error; 425 } 426 427 error = platform_pre_restore(platform_mode); 428 if (error) 429 goto Cleanup; 430 431 error = disable_nonboot_cpus(); 432 if (error) 433 goto Enable_cpus; 434 435 local_irq_disable(); 436 437 error = syscore_suspend(); 438 if (error) 439 goto Enable_irqs; 440 441 save_processor_state(); 442 error = restore_highmem(); 443 if (!error) { 444 error = swsusp_arch_resume(); 445 /* 446 * The code below is only ever reached in case of a failure. 447 * Otherwise, execution continues at the place where 448 * swsusp_arch_suspend() was called. 449 */ 450 BUG_ON(!error); 451 /* 452 * This call to restore_highmem() reverts the changes made by 453 * the previous one. 454 */ 455 restore_highmem(); 456 } 457 /* 458 * The only reason why swsusp_arch_resume() can fail is memory being 459 * very tight, so we have to free it as soon as we can to avoid 460 * subsequent failures. 461 */ 462 swsusp_free(); 463 restore_processor_state(); 464 touch_softlockup_watchdog(); 465 466 syscore_resume(); 467 468 Enable_irqs: 469 local_irq_enable(); 470 471 Enable_cpus: 472 enable_nonboot_cpus(); 473 474 Cleanup: 475 platform_restore_cleanup(platform_mode); 476 477 dpm_resume_noirq(PMSG_RECOVER); 478 479 return error; 480 } 481 482 /** 483 * hibernation_restore - Quiesce devices and restore from a hibernation image. 484 * @platform_mode: If set, use platform driver to prepare for the transition. 485 * 486 * This routine must be called with pm_mutex held. If it is successful, control 487 * reappears in the restored target kernel in hibernation_snapshot(). 488 */ 489 int hibernation_restore(int platform_mode) 490 { 491 int error; 492 493 pm_prepare_console(); 494 suspend_console(); 495 pm_restrict_gfp_mask(); 496 error = dpm_suspend_start(PMSG_QUIESCE); 497 if (!error) { 498 error = resume_target_kernel(platform_mode); 499 dpm_resume_end(PMSG_RECOVER); 500 } 501 pm_restore_gfp_mask(); 502 resume_console(); 503 pm_restore_console(); 504 return error; 505 } 506 507 /** 508 * hibernation_platform_enter - Power off the system using the platform driver. 509 */ 510 int hibernation_platform_enter(void) 511 { 512 int error; 513 514 if (!hibernation_ops) 515 return -ENOSYS; 516 517 /* 518 * We have cancelled the power transition by running 519 * hibernation_ops->finish() before saving the image, so we should let 520 * the firmware know that we're going to enter the sleep state after all 521 */ 522 error = hibernation_ops->begin(); 523 if (error) 524 goto Close; 525 526 entering_platform_hibernation = true; 527 suspend_console(); 528 error = dpm_suspend_start(PMSG_HIBERNATE); 529 if (error) { 530 if (hibernation_ops->recover) 531 hibernation_ops->recover(); 532 goto Resume_devices; 533 } 534 535 error = dpm_suspend_noirq(PMSG_HIBERNATE); 536 if (error) 537 goto Resume_devices; 538 539 error = hibernation_ops->prepare(); 540 if (error) 541 goto Platform_finish; 542 543 error = disable_nonboot_cpus(); 544 if (error) 545 goto Platform_finish; 546 547 local_irq_disable(); 548 syscore_suspend(); 549 if (pm_wakeup_pending()) { 550 error = -EAGAIN; 551 goto Power_up; 552 } 553 554 hibernation_ops->enter(); 555 /* We should never get here */ 556 while (1); 557 558 Power_up: 559 syscore_resume(); 560 local_irq_enable(); 561 enable_nonboot_cpus(); 562 563 Platform_finish: 564 hibernation_ops->finish(); 565 566 dpm_resume_noirq(PMSG_RESTORE); 567 568 Resume_devices: 569 entering_platform_hibernation = false; 570 dpm_resume_end(PMSG_RESTORE); 571 resume_console(); 572 573 Close: 574 hibernation_ops->end(); 575 576 return error; 577 } 578 579 /** 580 * power_down - Shut the machine down for hibernation. 581 * 582 * Use the platform driver, if configured, to put the system into the sleep 583 * state corresponding to hibernation, or try to power it off or reboot, 584 * depending on the value of hibernation_mode. 585 */ 586 static void power_down(void) 587 { 588 switch (hibernation_mode) { 589 case HIBERNATION_TEST: 590 case HIBERNATION_TESTPROC: 591 break; 592 case HIBERNATION_REBOOT: 593 kernel_restart(NULL); 594 break; 595 case HIBERNATION_PLATFORM: 596 hibernation_platform_enter(); 597 case HIBERNATION_SHUTDOWN: 598 kernel_power_off(); 599 break; 600 } 601 kernel_halt(); 602 /* 603 * Valid image is on the disk, if we continue we risk serious data 604 * corruption after resume. 605 */ 606 printk(KERN_CRIT "PM: Please power down manually\n"); 607 while(1); 608 } 609 610 static int prepare_processes(void) 611 { 612 int error = 0; 613 614 if (freeze_processes()) { 615 error = -EBUSY; 616 thaw_processes(); 617 } 618 return error; 619 } 620 621 /** 622 * hibernate - Carry out system hibernation, including saving the image. 623 */ 624 int hibernate(void) 625 { 626 int error; 627 628 mutex_lock(&pm_mutex); 629 /* The snapshot device should not be opened while we're running */ 630 if (!atomic_add_unless(&snapshot_device_available, -1, 0)) { 631 error = -EBUSY; 632 goto Unlock; 633 } 634 635 pm_prepare_console(); 636 error = pm_notifier_call_chain(PM_HIBERNATION_PREPARE); 637 if (error) 638 goto Exit; 639 640 error = usermodehelper_disable(); 641 if (error) 642 goto Exit; 643 644 /* Allocate memory management structures */ 645 error = create_basic_memory_bitmaps(); 646 if (error) 647 goto Exit; 648 649 printk(KERN_INFO "PM: Syncing filesystems ... "); 650 sys_sync(); 651 printk("done.\n"); 652 653 error = prepare_processes(); 654 if (error) 655 goto Finish; 656 657 error = hibernation_snapshot(hibernation_mode == HIBERNATION_PLATFORM); 658 if (error) 659 goto Thaw; 660 if (freezer_test_done) { 661 freezer_test_done = false; 662 goto Thaw; 663 } 664 665 if (in_suspend) { 666 unsigned int flags = 0; 667 668 if (hibernation_mode == HIBERNATION_PLATFORM) 669 flags |= SF_PLATFORM_MODE; 670 if (nocompress) 671 flags |= SF_NOCOMPRESS_MODE; 672 else 673 flags |= SF_CRC32_MODE; 674 675 pr_debug("PM: writing image.\n"); 676 error = swsusp_write(flags); 677 swsusp_free(); 678 if (!error) 679 power_down(); 680 in_suspend = 0; 681 pm_restore_gfp_mask(); 682 } else { 683 pr_debug("PM: Image restored successfully.\n"); 684 } 685 686 Thaw: 687 thaw_processes(); 688 Finish: 689 free_basic_memory_bitmaps(); 690 usermodehelper_enable(); 691 Exit: 692 pm_notifier_call_chain(PM_POST_HIBERNATION); 693 pm_restore_console(); 694 atomic_inc(&snapshot_device_available); 695 Unlock: 696 mutex_unlock(&pm_mutex); 697 return error; 698 } 699 700 701 /** 702 * software_resume - Resume from a saved hibernation image. 703 * 704 * This routine is called as a late initcall, when all devices have been 705 * discovered and initialized already. 706 * 707 * The image reading code is called to see if there is a hibernation image 708 * available for reading. If that is the case, devices are quiesced and the 709 * contents of memory is restored from the saved image. 710 * 711 * If this is successful, control reappears in the restored target kernel in 712 * hibernation_snaphot() which returns to hibernate(). Otherwise, the routine 713 * attempts to recover gracefully and make the kernel return to the normal mode 714 * of operation. 715 */ 716 static int software_resume(void) 717 { 718 int error; 719 unsigned int flags; 720 721 /* 722 * If the user said "noresume".. bail out early. 723 */ 724 if (noresume) 725 return 0; 726 727 /* 728 * name_to_dev_t() below takes a sysfs buffer mutex when sysfs 729 * is configured into the kernel. Since the regular hibernate 730 * trigger path is via sysfs which takes a buffer mutex before 731 * calling hibernate functions (which take pm_mutex) this can 732 * cause lockdep to complain about a possible ABBA deadlock 733 * which cannot happen since we're in the boot code here and 734 * sysfs can't be invoked yet. Therefore, we use a subclass 735 * here to avoid lockdep complaining. 736 */ 737 mutex_lock_nested(&pm_mutex, SINGLE_DEPTH_NESTING); 738 739 if (swsusp_resume_device) 740 goto Check_image; 741 742 if (!strlen(resume_file)) { 743 error = -ENOENT; 744 goto Unlock; 745 } 746 747 pr_debug("PM: Checking hibernation image partition %s\n", resume_file); 748 749 if (resume_delay) { 750 printk(KERN_INFO "Waiting %dsec before reading resume device...\n", 751 resume_delay); 752 ssleep(resume_delay); 753 } 754 755 /* Check if the device is there */ 756 swsusp_resume_device = name_to_dev_t(resume_file); 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 = usermodehelper_disable(); 806 if (error) 807 goto close_finish; 808 809 error = create_basic_memory_bitmaps(); 810 if (error) { 811 usermodehelper_enable(); 812 goto close_finish; 813 } 814 815 pr_debug("PM: Preparing processes for restore.\n"); 816 error = prepare_processes(); 817 if (error) { 818 swsusp_close(FMODE_READ); 819 goto Done; 820 } 821 822 pr_debug("PM: Loading hibernation image.\n"); 823 824 error = swsusp_read(&flags); 825 swsusp_close(FMODE_READ); 826 if (!error) 827 hibernation_restore(flags & SF_PLATFORM_MODE); 828 829 printk(KERN_ERR "PM: Failed to load hibernation image, recovering.\n"); 830 swsusp_free(); 831 thaw_processes(); 832 Done: 833 free_basic_memory_bitmaps(); 834 usermodehelper_enable(); 835 Finish: 836 pm_notifier_call_chain(PM_POST_RESTORE); 837 pm_restore_console(); 838 atomic_inc(&snapshot_device_available); 839 /* For success case, the suspend path will release the lock */ 840 Unlock: 841 mutex_unlock(&pm_mutex); 842 pr_debug("PM: Hibernation image not present or could not be loaded.\n"); 843 return error; 844 close_finish: 845 swsusp_close(FMODE_READ); 846 goto Finish; 847 } 848 849 late_initcall(software_resume); 850 851 852 static const char * const hibernation_modes[] = { 853 [HIBERNATION_PLATFORM] = "platform", 854 [HIBERNATION_SHUTDOWN] = "shutdown", 855 [HIBERNATION_REBOOT] = "reboot", 856 [HIBERNATION_TEST] = "test", 857 [HIBERNATION_TESTPROC] = "testproc", 858 }; 859 860 /* 861 * /sys/power/disk - Control hibernation mode. 862 * 863 * Hibernation can be handled in several ways. There are a few different ways 864 * to put the system into the sleep state: using the platform driver (e.g. ACPI 865 * or other hibernation_ops), powering it off or rebooting it (for testing 866 * mostly), or using one of the two available test modes. 867 * 868 * The sysfs file /sys/power/disk provides an interface for selecting the 869 * hibernation mode to use. Reading from this file causes the available modes 870 * to be printed. There are 5 modes that can be supported: 871 * 872 * 'platform' 873 * 'shutdown' 874 * 'reboot' 875 * 'test' 876 * 'testproc' 877 * 878 * If a platform hibernation driver is in use, 'platform' will be supported 879 * and will be used by default. Otherwise, 'shutdown' will be used by default. 880 * The selected option (i.e. the one corresponding to the current value of 881 * hibernation_mode) is enclosed by a square bracket. 882 * 883 * To select a given hibernation mode it is necessary to write the mode's 884 * string representation (as returned by reading from /sys/power/disk) back 885 * into /sys/power/disk. 886 */ 887 888 static ssize_t disk_show(struct kobject *kobj, struct kobj_attribute *attr, 889 char *buf) 890 { 891 int i; 892 char *start = buf; 893 894 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { 895 if (!hibernation_modes[i]) 896 continue; 897 switch (i) { 898 case HIBERNATION_SHUTDOWN: 899 case HIBERNATION_REBOOT: 900 case HIBERNATION_TEST: 901 case HIBERNATION_TESTPROC: 902 break; 903 case HIBERNATION_PLATFORM: 904 if (hibernation_ops) 905 break; 906 /* not a valid mode, continue with loop */ 907 continue; 908 } 909 if (i == hibernation_mode) 910 buf += sprintf(buf, "[%s] ", hibernation_modes[i]); 911 else 912 buf += sprintf(buf, "%s ", hibernation_modes[i]); 913 } 914 buf += sprintf(buf, "\n"); 915 return buf-start; 916 } 917 918 static ssize_t disk_store(struct kobject *kobj, struct kobj_attribute *attr, 919 const char *buf, size_t n) 920 { 921 int error = 0; 922 int i; 923 int len; 924 char *p; 925 int mode = HIBERNATION_INVALID; 926 927 p = memchr(buf, '\n', n); 928 len = p ? p - buf : n; 929 930 mutex_lock(&pm_mutex); 931 for (i = HIBERNATION_FIRST; i <= HIBERNATION_MAX; i++) { 932 if (len == strlen(hibernation_modes[i]) 933 && !strncmp(buf, hibernation_modes[i], len)) { 934 mode = i; 935 break; 936 } 937 } 938 if (mode != HIBERNATION_INVALID) { 939 switch (mode) { 940 case HIBERNATION_SHUTDOWN: 941 case HIBERNATION_REBOOT: 942 case HIBERNATION_TEST: 943 case HIBERNATION_TESTPROC: 944 hibernation_mode = mode; 945 break; 946 case HIBERNATION_PLATFORM: 947 if (hibernation_ops) 948 hibernation_mode = mode; 949 else 950 error = -EINVAL; 951 } 952 } else 953 error = -EINVAL; 954 955 if (!error) 956 pr_debug("PM: Hibernation mode set to '%s'\n", 957 hibernation_modes[mode]); 958 mutex_unlock(&pm_mutex); 959 return error ? error : n; 960 } 961 962 power_attr(disk); 963 964 static ssize_t resume_show(struct kobject *kobj, struct kobj_attribute *attr, 965 char *buf) 966 { 967 return sprintf(buf,"%d:%d\n", MAJOR(swsusp_resume_device), 968 MINOR(swsusp_resume_device)); 969 } 970 971 static ssize_t resume_store(struct kobject *kobj, struct kobj_attribute *attr, 972 const char *buf, size_t n) 973 { 974 unsigned int maj, min; 975 dev_t res; 976 int ret = -EINVAL; 977 978 if (sscanf(buf, "%u:%u", &maj, &min) != 2) 979 goto out; 980 981 res = MKDEV(maj,min); 982 if (maj != MAJOR(res) || min != MINOR(res)) 983 goto out; 984 985 mutex_lock(&pm_mutex); 986 swsusp_resume_device = res; 987 mutex_unlock(&pm_mutex); 988 printk(KERN_INFO "PM: Starting manual resume from disk\n"); 989 noresume = 0; 990 software_resume(); 991 ret = n; 992 out: 993 return ret; 994 } 995 996 power_attr(resume); 997 998 static ssize_t image_size_show(struct kobject *kobj, struct kobj_attribute *attr, 999 char *buf) 1000 { 1001 return sprintf(buf, "%lu\n", image_size); 1002 } 1003 1004 static ssize_t image_size_store(struct kobject *kobj, struct kobj_attribute *attr, 1005 const char *buf, size_t n) 1006 { 1007 unsigned long size; 1008 1009 if (sscanf(buf, "%lu", &size) == 1) { 1010 image_size = size; 1011 return n; 1012 } 1013 1014 return -EINVAL; 1015 } 1016 1017 power_attr(image_size); 1018 1019 static ssize_t reserved_size_show(struct kobject *kobj, 1020 struct kobj_attribute *attr, char *buf) 1021 { 1022 return sprintf(buf, "%lu\n", reserved_size); 1023 } 1024 1025 static ssize_t reserved_size_store(struct kobject *kobj, 1026 struct kobj_attribute *attr, 1027 const char *buf, size_t n) 1028 { 1029 unsigned long size; 1030 1031 if (sscanf(buf, "%lu", &size) == 1) { 1032 reserved_size = size; 1033 return n; 1034 } 1035 1036 return -EINVAL; 1037 } 1038 1039 power_attr(reserved_size); 1040 1041 static struct attribute * g[] = { 1042 &disk_attr.attr, 1043 &resume_attr.attr, 1044 &image_size_attr.attr, 1045 &reserved_size_attr.attr, 1046 NULL, 1047 }; 1048 1049 1050 static struct attribute_group attr_group = { 1051 .attrs = g, 1052 }; 1053 1054 1055 static int __init pm_disk_init(void) 1056 { 1057 return sysfs_create_group(power_kobj, &attr_group); 1058 } 1059 1060 core_initcall(pm_disk_init); 1061 1062 1063 static int __init resume_setup(char *str) 1064 { 1065 if (noresume) 1066 return 1; 1067 1068 strncpy( resume_file, str, 255 ); 1069 return 1; 1070 } 1071 1072 static int __init resume_offset_setup(char *str) 1073 { 1074 unsigned long long offset; 1075 1076 if (noresume) 1077 return 1; 1078 1079 if (sscanf(str, "%llu", &offset) == 1) 1080 swsusp_resume_block = offset; 1081 1082 return 1; 1083 } 1084 1085 static int __init hibernate_setup(char *str) 1086 { 1087 if (!strncmp(str, "noresume", 8)) 1088 noresume = 1; 1089 else if (!strncmp(str, "nocompress", 10)) 1090 nocompress = 1; 1091 return 1; 1092 } 1093 1094 static int __init noresume_setup(char *str) 1095 { 1096 noresume = 1; 1097 return 1; 1098 } 1099 1100 static int __init resumewait_setup(char *str) 1101 { 1102 resume_wait = 1; 1103 return 1; 1104 } 1105 1106 static int __init resumedelay_setup(char *str) 1107 { 1108 resume_delay = simple_strtoul(str, NULL, 0); 1109 return 1; 1110 } 1111 1112 __setup("noresume", noresume_setup); 1113 __setup("resume_offset=", resume_offset_setup); 1114 __setup("resume=", resume_setup); 1115 __setup("hibernate=", hibernate_setup); 1116 __setup("resumewait", resumewait_setup); 1117 __setup("resumedelay=", resumedelay_setup); 1118