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