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