suspend_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 10
  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),
  45. "Component: %s, time: %u\n", label, msec);
  46. }
  47. /*
  48. * To test system suspend, we need a hands-off mechanism to resume the
  49. * system. RTCs wake alarms are a common self-contained mechanism.
  50. */
  51. static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
  52. {
  53. static char err_readtime[] __initdata =
  54. KERN_ERR "PM: can't read %s time, err %d\n";
  55. static char err_wakealarm [] __initdata =
  56. KERN_ERR "PM: can't set %s wakealarm, err %d\n";
  57. static char err_suspend[] __initdata =
  58. KERN_ERR "PM: suspend test failed, error %d\n";
  59. static char info_test[] __initdata =
  60. KERN_INFO "PM: test RTC wakeup from '%s' suspend\n";
  61. unsigned long now;
  62. struct rtc_wkalrm alm;
  63. int status;
  64. /* this may fail if the RTC hasn't been initialized */
  65. status = rtc_read_time(rtc, &alm.time);
  66. if (status < 0) {
  67. printk(err_readtime, dev_name(&rtc->dev), status);
  68. return;
  69. }
  70. rtc_tm_to_time(&alm.time, &now);
  71. memset(&alm, 0, sizeof alm);
  72. rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
  73. alm.enabled = true;
  74. status = rtc_set_alarm(rtc, &alm);
  75. if (status < 0) {
  76. printk(err_wakealarm, dev_name(&rtc->dev), status);
  77. return;
  78. }
  79. if (state == PM_SUSPEND_MEM) {
  80. printk(info_test, pm_states[state]);
  81. status = pm_suspend(state);
  82. if (status == -ENODEV)
  83. state = PM_SUSPEND_STANDBY;
  84. }
  85. if (state == PM_SUSPEND_STANDBY) {
  86. printk(info_test, pm_states[state]);
  87. status = pm_suspend(state);
  88. }
  89. if (status < 0)
  90. printk(err_suspend, status);
  91. /* Some platforms can't detect that the alarm triggered the
  92. * wakeup, or (accordingly) disable it after it afterwards.
  93. * It's supposed to give oneshot behavior; cope.
  94. */
  95. alm.enabled = false;
  96. rtc_set_alarm(rtc, &alm);
  97. }
  98. static int __init has_wakealarm(struct device *dev, void *name_ptr)
  99. {
  100. struct rtc_device *candidate = to_rtc_device(dev);
  101. if (!candidate->ops->set_alarm)
  102. return 0;
  103. if (!device_may_wakeup(candidate->dev.parent))
  104. return 0;
  105. *(const char **)name_ptr = dev_name(dev);
  106. return 1;
  107. }
  108. /*
  109. * Kernel options like "test_suspend=mem" force suspend/resume sanity tests
  110. * at startup time. They're normally disabled, for faster boot and because
  111. * we can't know which states really work on this particular system.
  112. */
  113. static suspend_state_t test_state __initdata = PM_SUSPEND_ON;
  114. static char warn_bad_state[] __initdata =
  115. KERN_WARNING "PM: can't test '%s' suspend state\n";
  116. static int __init setup_test_suspend(char *value)
  117. {
  118. unsigned i;
  119. /* "=mem" ==> "mem" */
  120. value++;
  121. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  122. if (!pm_states[i])
  123. continue;
  124. if (strcmp(pm_states[i], value) != 0)
  125. continue;
  126. test_state = (__force suspend_state_t) i;
  127. return 0;
  128. }
  129. printk(warn_bad_state, value);
  130. return 0;
  131. }
  132. __setup("test_suspend", setup_test_suspend);
  133. static int __init test_suspend(void)
  134. {
  135. static char warn_no_rtc[] __initdata =
  136. KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n";
  137. char *pony = NULL;
  138. struct rtc_device *rtc = NULL;
  139. /* PM is initialized by now; is that state testable? */
  140. if (test_state == PM_SUSPEND_ON)
  141. goto done;
  142. if (!valid_state(test_state)) {
  143. printk(warn_bad_state, pm_states[test_state]);
  144. goto done;
  145. }
  146. /* RTCs have initialized by now too ... can we use one? */
  147. class_find_device(rtc_class, NULL, &pony, has_wakealarm);
  148. if (pony)
  149. rtc = rtc_class_open(pony);
  150. if (!rtc) {
  151. printk(warn_no_rtc);
  152. goto done;
  153. }
  154. /* go for it */
  155. test_wakealarm(rtc, test_state);
  156. rtc_class_close(rtc);
  157. done:
  158. return 0;
  159. }
  160. late_initcall(test_suspend);