suspend_test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * kernel/power/suspend_test.c - Suspend to RAM and standby test facility.
  3. *
  4. * Copyright (c) 2009 Pavel Machek <pavel@ucw.cz>
  5. *
  6. * This file is released under the GPLv2.
  7. */
  8. #include <linux/init.h>
  9. #include <linux/rtc.h>
  10. #include "power.h"
  11. /*
  12. * We test the system suspend code by setting an RTC wakealarm a short
  13. * time in the future, then suspending. Suspending the devices won't
  14. * normally take long ... some systems only need a few milliseconds.
  15. *
  16. * The time it takes is system-specific though, so when we test this
  17. * during system bootup we allow a LOT of time.
  18. */
  19. #define TEST_SUSPEND_SECONDS 5
  20. static unsigned long suspend_test_start_time;
  21. void suspend_test_start(void)
  22. {
  23. /* FIXME Use better timebase than "jiffies", ideally a clocksource.
  24. * What we want is a hardware counter that will work correctly even
  25. * during the irqs-are-off stages of the suspend/resume cycle...
  26. */
  27. suspend_test_start_time = jiffies;
  28. }
  29. void suspend_test_finish(const char *label)
  30. {
  31. long nj = jiffies - suspend_test_start_time;
  32. unsigned msec;
  33. msec = jiffies_to_msecs(abs(nj));
  34. pr_info("PM: %s took %d.%03d seconds\n", label,
  35. msec / 1000, msec % 1000);
  36. /* Warning on suspend means the RTC alarm period needs to be
  37. * larger -- the system was sooo slooowwww to suspend that the
  38. * alarm (should have) fired before the system went to sleep!
  39. *
  40. * Warning on either suspend or resume also means the system
  41. * has some performance issues. The stack dump of a WARN_ON
  42. * is more likely to get the right attention than a printk...
  43. */
  44. WARN(msec > (TEST_SUSPEND_SECONDS * 1000), "Component: %s\n", label);
  45. }
  46. /*
  47. * To test system suspend, we need a hands-off mechanism to resume the
  48. * system. RTCs wake alarms are a common self-contained mechanism.
  49. */
  50. static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
  51. {
  52. static char err_readtime[] __initdata =
  53. KERN_ERR "PM: can't read %s time, err %d\n";
  54. static char err_wakealarm [] __initdata =
  55. KERN_ERR "PM: can't set %s wakealarm, err %d\n";
  56. static char err_suspend[] __initdata =
  57. KERN_ERR "PM: suspend test failed, error %d\n";
  58. static char info_test[] __initdata =
  59. KERN_INFO "PM: test RTC wakeup from '%s' suspend\n";
  60. unsigned long now;
  61. struct rtc_wkalrm alm;
  62. int status;
  63. /* this may fail if the RTC hasn't been initialized */
  64. status = rtc_read_time(rtc, &alm.time);
  65. if (status < 0) {
  66. printk(err_readtime, dev_name(&rtc->dev), status);
  67. return;
  68. }
  69. rtc_tm_to_time(&alm.time, &now);
  70. memset(&alm, 0, sizeof alm);
  71. rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
  72. alm.enabled = true;
  73. status = rtc_set_alarm(rtc, &alm);
  74. if (status < 0) {
  75. printk(err_wakealarm, dev_name(&rtc->dev), status);
  76. return;
  77. }
  78. if (state == PM_SUSPEND_MEM) {
  79. printk(info_test, pm_states[state]);
  80. status = pm_suspend(state);
  81. if (status == -ENODEV)
  82. state = PM_SUSPEND_STANDBY;
  83. }
  84. if (state == PM_SUSPEND_STANDBY) {
  85. printk(info_test, pm_states[state]);
  86. status = pm_suspend(state);
  87. }
  88. if (status < 0)
  89. printk(err_suspend, status);
  90. /* Some platforms can't detect that the alarm triggered the
  91. * wakeup, or (accordingly) disable it after it afterwards.
  92. * It's supposed to give oneshot behavior; cope.
  93. */
  94. alm.enabled = false;
  95. rtc_set_alarm(rtc, &alm);
  96. }
  97. static int __init has_wakealarm(struct device *dev, void *name_ptr)
  98. {
  99. struct rtc_device *candidate = to_rtc_device(dev);
  100. if (!candidate->ops->set_alarm)
  101. return 0;
  102. if (!device_may_wakeup(candidate->dev.parent))
  103. return 0;
  104. *(const char **)name_ptr = dev_name(dev);
  105. return 1;
  106. }
  107. /*
  108. * Kernel options like "test_suspend=mem" force suspend/resume sanity tests
  109. * at startup time. They're normally disabled, for faster boot and because
  110. * we can't know which states really work on this particular system.
  111. */
  112. static suspend_state_t test_state __initdata = PM_SUSPEND_ON;
  113. static char warn_bad_state[] __initdata =
  114. KERN_WARNING "PM: can't test '%s' suspend state\n";
  115. static int __init setup_test_suspend(char *value)
  116. {
  117. unsigned i;
  118. /* "=mem" ==> "mem" */
  119. value++;
  120. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  121. if (!pm_states[i])
  122. continue;
  123. if (strcmp(pm_states[i], value) != 0)
  124. continue;
  125. test_state = (__force suspend_state_t) i;
  126. return 0;
  127. }
  128. printk(warn_bad_state, value);
  129. return 0;
  130. }
  131. __setup("test_suspend", setup_test_suspend);
  132. static int __init test_suspend(void)
  133. {
  134. static char warn_no_rtc[] __initdata =
  135. KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n";
  136. char *pony = NULL;
  137. struct rtc_device *rtc = NULL;
  138. /* PM is initialized by now; is that state testable? */
  139. if (test_state == PM_SUSPEND_ON)
  140. goto done;
  141. if (!valid_state(test_state)) {
  142. printk(warn_bad_state, pm_states[test_state]);
  143. goto done;
  144. }
  145. /* RTCs have initialized by now too ... can we use one? */
  146. class_find_device(rtc_class, NULL, &pony, has_wakealarm);
  147. if (pony)
  148. rtc = rtc_class_open(pony);
  149. if (!rtc) {
  150. printk(warn_no_rtc);
  151. goto done;
  152. }
  153. /* go for it */
  154. test_wakealarm(rtc, test_state);
  155. rtc_class_close(rtc);
  156. done:
  157. return 0;
  158. }
  159. late_initcall(test_suspend);