1 /* 2 * kernel/power/main.c - PM subsystem core functionality. 3 * 4 * Copyright (c) 2003 Patrick Mochel 5 * Copyright (c) 2003 Open Source Development Lab 6 * 7 * This file is released under the GPLv2 8 * 9 */ 10 11 #include <linux/module.h> 12 #include <linux/suspend.h> 13 #include <linux/kobject.h> 14 #include <linux/string.h> 15 #include <linux/delay.h> 16 #include <linux/errno.h> 17 #include <linux/kmod.h> 18 #include <linux/init.h> 19 #include <linux/console.h> 20 #include <linux/cpu.h> 21 #include <linux/resume-trace.h> 22 #include <linux/freezer.h> 23 #include <linux/vmstat.h> 24 #include <linux/syscalls.h> 25 #include <linux/ftrace.h> 26 27 #include "power.h" 28 29 DEFINE_MUTEX(pm_mutex); 30 31 unsigned int pm_flags; 32 EXPORT_SYMBOL(pm_flags); 33 34 #ifdef CONFIG_PM_SLEEP 35 36 /* Routines for PM-transition notifications */ 37 38 static BLOCKING_NOTIFIER_HEAD(pm_chain_head); 39 40 int register_pm_notifier(struct notifier_block *nb) 41 { 42 return blocking_notifier_chain_register(&pm_chain_head, nb); 43 } 44 EXPORT_SYMBOL_GPL(register_pm_notifier); 45 46 int unregister_pm_notifier(struct notifier_block *nb) 47 { 48 return blocking_notifier_chain_unregister(&pm_chain_head, nb); 49 } 50 EXPORT_SYMBOL_GPL(unregister_pm_notifier); 51 52 int pm_notifier_call_chain(unsigned long val) 53 { 54 return (blocking_notifier_call_chain(&pm_chain_head, val, NULL) 55 == NOTIFY_BAD) ? -EINVAL : 0; 56 } 57 58 #ifdef CONFIG_PM_DEBUG 59 int pm_test_level = TEST_NONE; 60 61 static int suspend_test(int level) 62 { 63 if (pm_test_level == level) { 64 printk(KERN_INFO "suspend debug: Waiting for 5 seconds.\n"); 65 mdelay(5000); 66 return 1; 67 } 68 return 0; 69 } 70 71 static const char * const pm_tests[__TEST_AFTER_LAST] = { 72 [TEST_NONE] = "none", 73 [TEST_CORE] = "core", 74 [TEST_CPUS] = "processors", 75 [TEST_PLATFORM] = "platform", 76 [TEST_DEVICES] = "devices", 77 [TEST_FREEZER] = "freezer", 78 }; 79 80 static ssize_t pm_test_show(struct kobject *kobj, struct kobj_attribute *attr, 81 char *buf) 82 { 83 char *s = buf; 84 int level; 85 86 for (level = TEST_FIRST; level <= TEST_MAX; level++) 87 if (pm_tests[level]) { 88 if (level == pm_test_level) 89 s += sprintf(s, "[%s] ", pm_tests[level]); 90 else 91 s += sprintf(s, "%s ", pm_tests[level]); 92 } 93 94 if (s != buf) 95 /* convert the last space to a newline */ 96 *(s-1) = '\n'; 97 98 return (s - buf); 99 } 100 101 static ssize_t pm_test_store(struct kobject *kobj, struct kobj_attribute *attr, 102 const char *buf, size_t n) 103 { 104 const char * const *s; 105 int level; 106 char *p; 107 int len; 108 int error = -EINVAL; 109 110 p = memchr(buf, '\n', n); 111 len = p ? p - buf : n; 112 113 mutex_lock(&pm_mutex); 114 115 level = TEST_FIRST; 116 for (s = &pm_tests[level]; level <= TEST_MAX; s++, level++) 117 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) { 118 pm_test_level = level; 119 error = 0; 120 break; 121 } 122 123 mutex_unlock(&pm_mutex); 124 125 return error ? error : n; 126 } 127 128 power_attr(pm_test); 129 #else /* !CONFIG_PM_DEBUG */ 130 static inline int suspend_test(int level) { return 0; } 131 #endif /* !CONFIG_PM_DEBUG */ 132 133 #endif /* CONFIG_PM_SLEEP */ 134 135 #ifdef CONFIG_SUSPEND 136 137 #ifdef CONFIG_PM_TEST_SUSPEND 138 139 /* 140 * We test the system suspend code by setting an RTC wakealarm a short 141 * time in the future, then suspending. Suspending the devices won't 142 * normally take long ... some systems only need a few milliseconds. 143 * 144 * The time it takes is system-specific though, so when we test this 145 * during system bootup we allow a LOT of time. 146 */ 147 #define TEST_SUSPEND_SECONDS 5 148 149 static unsigned long suspend_test_start_time; 150 151 static void suspend_test_start(void) 152 { 153 /* FIXME Use better timebase than "jiffies", ideally a clocksource. 154 * What we want is a hardware counter that will work correctly even 155 * during the irqs-are-off stages of the suspend/resume cycle... 156 */ 157 suspend_test_start_time = jiffies; 158 } 159 160 static void suspend_test_finish(const char *label) 161 { 162 long nj = jiffies - suspend_test_start_time; 163 unsigned msec; 164 165 msec = jiffies_to_msecs(abs(nj)); 166 pr_info("PM: %s took %d.%03d seconds\n", label, 167 msec / 1000, msec % 1000); 168 169 /* Warning on suspend means the RTC alarm period needs to be 170 * larger -- the system was sooo slooowwww to suspend that the 171 * alarm (should have) fired before the system went to sleep! 172 * 173 * Warning on either suspend or resume also means the system 174 * has some performance issues. The stack dump of a WARN_ON 175 * is more likely to get the right attention than a printk... 176 */ 177 WARN_ON(msec > (TEST_SUSPEND_SECONDS * 1000)); 178 } 179 180 #else 181 182 static void suspend_test_start(void) 183 { 184 } 185 186 static void suspend_test_finish(const char *label) 187 { 188 } 189 190 #endif 191 192 /* This is just an arbitrary number */ 193 #define FREE_PAGE_NUMBER (100) 194 195 static struct platform_suspend_ops *suspend_ops; 196 197 /** 198 * suspend_set_ops - Set the global suspend method table. 199 * @ops: Pointer to ops structure. 200 */ 201 202 void suspend_set_ops(struct platform_suspend_ops *ops) 203 { 204 mutex_lock(&pm_mutex); 205 suspend_ops = ops; 206 mutex_unlock(&pm_mutex); 207 } 208 209 /** 210 * suspend_valid_only_mem - generic memory-only valid callback 211 * 212 * Platform drivers that implement mem suspend only and only need 213 * to check for that in their .valid callback can use this instead 214 * of rolling their own .valid callback. 215 */ 216 int suspend_valid_only_mem(suspend_state_t state) 217 { 218 return state == PM_SUSPEND_MEM; 219 } 220 221 /** 222 * suspend_prepare - Do prep work before entering low-power state. 223 * 224 * This is common code that is called for each state that we're entering. 225 * Run suspend notifiers, allocate a console and stop all processes. 226 */ 227 static int suspend_prepare(void) 228 { 229 int error; 230 unsigned int free_pages; 231 232 if (!suspend_ops || !suspend_ops->enter) 233 return -EPERM; 234 235 pm_prepare_console(); 236 237 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE); 238 if (error) 239 goto Finish; 240 241 error = usermodehelper_disable(); 242 if (error) 243 goto Finish; 244 245 if (suspend_freeze_processes()) { 246 error = -EAGAIN; 247 goto Thaw; 248 } 249 250 free_pages = global_page_state(NR_FREE_PAGES); 251 if (free_pages < FREE_PAGE_NUMBER) { 252 pr_debug("PM: free some memory\n"); 253 shrink_all_memory(FREE_PAGE_NUMBER - free_pages); 254 if (nr_free_pages() < FREE_PAGE_NUMBER) { 255 error = -ENOMEM; 256 printk(KERN_ERR "PM: No enough memory\n"); 257 } 258 } 259 if (!error) 260 return 0; 261 262 Thaw: 263 suspend_thaw_processes(); 264 usermodehelper_enable(); 265 Finish: 266 pm_notifier_call_chain(PM_POST_SUSPEND); 267 pm_restore_console(); 268 return error; 269 } 270 271 /* default implementation */ 272 void __attribute__ ((weak)) arch_suspend_disable_irqs(void) 273 { 274 local_irq_disable(); 275 } 276 277 /* default implementation */ 278 void __attribute__ ((weak)) arch_suspend_enable_irqs(void) 279 { 280 local_irq_enable(); 281 } 282 283 /** 284 * suspend_enter - enter the desired system sleep state. 285 * @state: state to enter 286 * 287 * This function should be called after devices have been suspended. 288 */ 289 static int suspend_enter(suspend_state_t state) 290 { 291 int error = 0; 292 293 device_pm_lock(); 294 arch_suspend_disable_irqs(); 295 BUG_ON(!irqs_disabled()); 296 297 if ((error = device_power_down(PMSG_SUSPEND))) { 298 printk(KERN_ERR "PM: Some devices failed to power down\n"); 299 goto Done; 300 } 301 302 if (!suspend_test(TEST_CORE)) 303 error = suspend_ops->enter(state); 304 305 device_power_up(PMSG_RESUME); 306 Done: 307 arch_suspend_enable_irqs(); 308 BUG_ON(irqs_disabled()); 309 device_pm_unlock(); 310 return error; 311 } 312 313 /** 314 * suspend_devices_and_enter - suspend devices and enter the desired system 315 * sleep state. 316 * @state: state to enter 317 */ 318 int suspend_devices_and_enter(suspend_state_t state) 319 { 320 int error, ftrace_save; 321 322 if (!suspend_ops) 323 return -ENOSYS; 324 325 if (suspend_ops->begin) { 326 error = suspend_ops->begin(state); 327 if (error) 328 goto Close; 329 } 330 suspend_console(); 331 ftrace_save = __ftrace_enabled_save(); 332 suspend_test_start(); 333 error = device_suspend(PMSG_SUSPEND); 334 if (error) { 335 printk(KERN_ERR "PM: Some devices failed to suspend\n"); 336 goto Recover_platform; 337 } 338 suspend_test_finish("suspend devices"); 339 if (suspend_test(TEST_DEVICES)) 340 goto Recover_platform; 341 342 if (suspend_ops->prepare) { 343 error = suspend_ops->prepare(); 344 if (error) 345 goto Resume_devices; 346 } 347 348 if (suspend_test(TEST_PLATFORM)) 349 goto Finish; 350 351 error = disable_nonboot_cpus(); 352 if (!error && !suspend_test(TEST_CPUS)) 353 suspend_enter(state); 354 355 enable_nonboot_cpus(); 356 Finish: 357 if (suspend_ops->finish) 358 suspend_ops->finish(); 359 Resume_devices: 360 suspend_test_start(); 361 device_resume(PMSG_RESUME); 362 suspend_test_finish("resume devices"); 363 __ftrace_enabled_restore(ftrace_save); 364 resume_console(); 365 Close: 366 if (suspend_ops->end) 367 suspend_ops->end(); 368 return error; 369 370 Recover_platform: 371 if (suspend_ops->recover) 372 suspend_ops->recover(); 373 goto Resume_devices; 374 } 375 376 /** 377 * suspend_finish - Do final work before exiting suspend sequence. 378 * 379 * Call platform code to clean up, restart processes, and free the 380 * console that we've allocated. This is not called for suspend-to-disk. 381 */ 382 static void suspend_finish(void) 383 { 384 suspend_thaw_processes(); 385 usermodehelper_enable(); 386 pm_notifier_call_chain(PM_POST_SUSPEND); 387 pm_restore_console(); 388 } 389 390 391 392 393 static const char * const pm_states[PM_SUSPEND_MAX] = { 394 [PM_SUSPEND_STANDBY] = "standby", 395 [PM_SUSPEND_MEM] = "mem", 396 }; 397 398 static inline int valid_state(suspend_state_t state) 399 { 400 /* All states need lowlevel support and need to be valid 401 * to the lowlevel implementation, no valid callback 402 * implies that none are valid. */ 403 if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state)) 404 return 0; 405 return 1; 406 } 407 408 409 /** 410 * enter_state - Do common work of entering low-power state. 411 * @state: pm_state structure for state we're entering. 412 * 413 * Make sure we're the only ones trying to enter a sleep state. Fail 414 * if someone has beat us to it, since we don't want anything weird to 415 * happen when we wake up. 416 * Then, do the setup for suspend, enter the state, and cleaup (after 417 * we've woken up). 418 */ 419 static int enter_state(suspend_state_t state) 420 { 421 int error; 422 423 if (!valid_state(state)) 424 return -ENODEV; 425 426 if (!mutex_trylock(&pm_mutex)) 427 return -EBUSY; 428 429 printk(KERN_INFO "PM: Syncing filesystems ... "); 430 sys_sync(); 431 printk("done.\n"); 432 433 pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]); 434 error = suspend_prepare(); 435 if (error) 436 goto Unlock; 437 438 if (suspend_test(TEST_FREEZER)) 439 goto Finish; 440 441 pr_debug("PM: Entering %s sleep\n", pm_states[state]); 442 error = suspend_devices_and_enter(state); 443 444 Finish: 445 pr_debug("PM: Finishing wakeup.\n"); 446 suspend_finish(); 447 Unlock: 448 mutex_unlock(&pm_mutex); 449 return error; 450 } 451 452 453 /** 454 * pm_suspend - Externally visible function for suspending system. 455 * @state: Enumerated value of state to enter. 456 * 457 * Determine whether or not value is within range, get state 458 * structure, and enter (above). 459 */ 460 461 int pm_suspend(suspend_state_t state) 462 { 463 if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX) 464 return enter_state(state); 465 return -EINVAL; 466 } 467 468 EXPORT_SYMBOL(pm_suspend); 469 470 #endif /* CONFIG_SUSPEND */ 471 472 struct kobject *power_kobj; 473 474 /** 475 * state - control system power state. 476 * 477 * show() returns what states are supported, which is hard-coded to 478 * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and 479 * 'disk' (Suspend-to-Disk). 480 * 481 * store() accepts one of those strings, translates it into the 482 * proper enumerated value, and initiates a suspend transition. 483 */ 484 485 static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, 486 char *buf) 487 { 488 char *s = buf; 489 #ifdef CONFIG_SUSPEND 490 int i; 491 492 for (i = 0; i < PM_SUSPEND_MAX; i++) { 493 if (pm_states[i] && valid_state(i)) 494 s += sprintf(s,"%s ", pm_states[i]); 495 } 496 #endif 497 #ifdef CONFIG_HIBERNATION 498 s += sprintf(s, "%s\n", "disk"); 499 #else 500 if (s != buf) 501 /* convert the last space to a newline */ 502 *(s-1) = '\n'; 503 #endif 504 return (s - buf); 505 } 506 507 static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr, 508 const char *buf, size_t n) 509 { 510 #ifdef CONFIG_SUSPEND 511 suspend_state_t state = PM_SUSPEND_STANDBY; 512 const char * const *s; 513 #endif 514 char *p; 515 int len; 516 int error = -EINVAL; 517 518 p = memchr(buf, '\n', n); 519 len = p ? p - buf : n; 520 521 /* First, check if we are requested to hibernate */ 522 if (len == 4 && !strncmp(buf, "disk", len)) { 523 error = hibernate(); 524 goto Exit; 525 } 526 527 #ifdef CONFIG_SUSPEND 528 for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) { 529 if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) 530 break; 531 } 532 if (state < PM_SUSPEND_MAX && *s) 533 error = enter_state(state); 534 #endif 535 536 Exit: 537 return error ? error : n; 538 } 539 540 power_attr(state); 541 542 #ifdef CONFIG_PM_TRACE 543 int pm_trace_enabled; 544 545 static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr, 546 char *buf) 547 { 548 return sprintf(buf, "%d\n", pm_trace_enabled); 549 } 550 551 static ssize_t 552 pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr, 553 const char *buf, size_t n) 554 { 555 int val; 556 557 if (sscanf(buf, "%d", &val) == 1) { 558 pm_trace_enabled = !!val; 559 return n; 560 } 561 return -EINVAL; 562 } 563 564 power_attr(pm_trace); 565 #endif /* CONFIG_PM_TRACE */ 566 567 static struct attribute * g[] = { 568 &state_attr.attr, 569 #ifdef CONFIG_PM_TRACE 570 &pm_trace_attr.attr, 571 #endif 572 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG) 573 &pm_test_attr.attr, 574 #endif 575 NULL, 576 }; 577 578 static struct attribute_group attr_group = { 579 .attrs = g, 580 }; 581 582 583 static int __init pm_init(void) 584 { 585 power_kobj = kobject_create_and_add("power", NULL); 586 if (!power_kobj) 587 return -ENOMEM; 588 return sysfs_create_group(power_kobj, &attr_group); 589 } 590 591 core_initcall(pm_init); 592 593 594 #ifdef CONFIG_PM_TEST_SUSPEND 595 596 #include <linux/rtc.h> 597 598 /* 599 * To test system suspend, we need a hands-off mechanism to resume the 600 * system. RTCs wake alarms are a common self-contained mechanism. 601 */ 602 603 static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state) 604 { 605 static char err_readtime[] __initdata = 606 KERN_ERR "PM: can't read %s time, err %d\n"; 607 static char err_wakealarm [] __initdata = 608 KERN_ERR "PM: can't set %s wakealarm, err %d\n"; 609 static char err_suspend[] __initdata = 610 KERN_ERR "PM: suspend test failed, error %d\n"; 611 static char info_test[] __initdata = 612 KERN_INFO "PM: test RTC wakeup from '%s' suspend\n"; 613 614 unsigned long now; 615 struct rtc_wkalrm alm; 616 int status; 617 618 /* this may fail if the RTC hasn't been initialized */ 619 status = rtc_read_time(rtc, &alm.time); 620 if (status < 0) { 621 printk(err_readtime, rtc->dev.bus_id, status); 622 return; 623 } 624 rtc_tm_to_time(&alm.time, &now); 625 626 memset(&alm, 0, sizeof alm); 627 rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time); 628 alm.enabled = true; 629 630 status = rtc_set_alarm(rtc, &alm); 631 if (status < 0) { 632 printk(err_wakealarm, rtc->dev.bus_id, status); 633 return; 634 } 635 636 if (state == PM_SUSPEND_MEM) { 637 printk(info_test, pm_states[state]); 638 status = pm_suspend(state); 639 if (status == -ENODEV) 640 state = PM_SUSPEND_STANDBY; 641 } 642 if (state == PM_SUSPEND_STANDBY) { 643 printk(info_test, pm_states[state]); 644 status = pm_suspend(state); 645 } 646 if (status < 0) 647 printk(err_suspend, status); 648 649 /* Some platforms can't detect that the alarm triggered the 650 * wakeup, or (accordingly) disable it after it afterwards. 651 * It's supposed to give oneshot behavior; cope. 652 */ 653 alm.enabled = false; 654 rtc_set_alarm(rtc, &alm); 655 } 656 657 static int __init has_wakealarm(struct device *dev, void *name_ptr) 658 { 659 struct rtc_device *candidate = to_rtc_device(dev); 660 661 if (!candidate->ops->set_alarm) 662 return 0; 663 if (!device_may_wakeup(candidate->dev.parent)) 664 return 0; 665 666 *(char **)name_ptr = dev->bus_id; 667 return 1; 668 } 669 670 /* 671 * Kernel options like "test_suspend=mem" force suspend/resume sanity tests 672 * at startup time. They're normally disabled, for faster boot and because 673 * we can't know which states really work on this particular system. 674 */ 675 static suspend_state_t test_state __initdata = PM_SUSPEND_ON; 676 677 static char warn_bad_state[] __initdata = 678 KERN_WARNING "PM: can't test '%s' suspend state\n"; 679 680 static int __init setup_test_suspend(char *value) 681 { 682 unsigned i; 683 684 /* "=mem" ==> "mem" */ 685 value++; 686 for (i = 0; i < PM_SUSPEND_MAX; i++) { 687 if (!pm_states[i]) 688 continue; 689 if (strcmp(pm_states[i], value) != 0) 690 continue; 691 test_state = (__force suspend_state_t) i; 692 return 0; 693 } 694 printk(warn_bad_state, value); 695 return 0; 696 } 697 __setup("test_suspend", setup_test_suspend); 698 699 static int __init test_suspend(void) 700 { 701 static char warn_no_rtc[] __initdata = 702 KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n"; 703 704 char *pony = NULL; 705 struct rtc_device *rtc = NULL; 706 707 /* PM is initialized by now; is that state testable? */ 708 if (test_state == PM_SUSPEND_ON) 709 goto done; 710 if (!valid_state(test_state)) { 711 printk(warn_bad_state, pm_states[test_state]); 712 goto done; 713 } 714 715 /* RTCs have initialized by now too ... can we use one? */ 716 class_find_device(rtc_class, NULL, &pony, has_wakealarm); 717 if (pony) 718 rtc = rtc_class_open(pony); 719 if (!rtc) { 720 printk(warn_no_rtc); 721 goto done; 722 } 723 724 /* go for it */ 725 test_wakealarm(rtc, test_state); 726 rtc_class_close(rtc); 727 done: 728 return 0; 729 } 730 late_initcall(test_suspend); 731 732 #endif /* CONFIG_PM_TEST_SUSPEND */ 733