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