suspend_test.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. static u32 test_repeat_count_max = 1;
  22. static u32 test_repeat_count_current;
  23. void suspend_test_start(void)
  24. {
  25. /* FIXME Use better timebase than "jiffies", ideally a clocksource.
  26. * What we want is a hardware counter that will work correctly even
  27. * during the irqs-are-off stages of the suspend/resume cycle...
  28. */
  29. suspend_test_start_time = jiffies;
  30. }
  31. void suspend_test_finish(const char *label)
  32. {
  33. long nj = jiffies - suspend_test_start_time;
  34. unsigned msec;
  35. msec = jiffies_to_msecs(abs(nj));
  36. pr_info("PM: %s took %d.%03d seconds\n", label,
  37. msec / 1000, msec % 1000);
  38. /* Warning on suspend means the RTC alarm period needs to be
  39. * larger -- the system was sooo slooowwww to suspend that the
  40. * alarm (should have) fired before the system went to sleep!
  41. *
  42. * Warning on either suspend or resume also means the system
  43. * has some performance issues. The stack dump of a WARN_ON
  44. * is more likely to get the right attention than a printk...
  45. */
  46. WARN(msec > (TEST_SUSPEND_SECONDS * 1000),
  47. "Component: %s, time: %u\n", label, msec);
  48. }
  49. /*
  50. * To test system suspend, we need a hands-off mechanism to resume the
  51. * system. RTCs wake alarms are a common self-contained mechanism.
  52. */
  53. static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
  54. {
  55. static char err_readtime[] __initdata =
  56. KERN_ERR "PM: can't read %s time, err %d\n";
  57. static char err_wakealarm [] __initdata =
  58. KERN_ERR "PM: can't set %s wakealarm, err %d\n";
  59. static char err_suspend[] __initdata =
  60. KERN_ERR "PM: suspend test failed, error %d\n";
  61. static char info_test[] __initdata =
  62. KERN_INFO "PM: test RTC wakeup from '%s' suspend\n";
  63. unsigned long now;
  64. struct rtc_wkalrm alm;
  65. int status;
  66. /* this may fail if the RTC hasn't been initialized */
  67. repeat:
  68. status = rtc_read_time(rtc, &alm.time);
  69. if (status < 0) {
  70. printk(err_readtime, dev_name(&rtc->dev), status);
  71. return;
  72. }
  73. rtc_tm_to_time(&alm.time, &now);
  74. memset(&alm, 0, sizeof alm);
  75. rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
  76. alm.enabled = true;
  77. status = rtc_set_alarm(rtc, &alm);
  78. if (status < 0) {
  79. printk(err_wakealarm, dev_name(&rtc->dev), status);
  80. return;
  81. }
  82. if (state == PM_SUSPEND_MEM) {
  83. printk(info_test, pm_states[state]);
  84. status = pm_suspend(state);
  85. if (status == -ENODEV)
  86. state = PM_SUSPEND_STANDBY;
  87. }
  88. if (state == PM_SUSPEND_STANDBY) {
  89. printk(info_test, pm_states[state]);
  90. status = pm_suspend(state);
  91. if (status < 0)
  92. state = PM_SUSPEND_FREEZE;
  93. }
  94. if (state == PM_SUSPEND_FREEZE) {
  95. printk(info_test, pm_states[state]);
  96. status = pm_suspend(state);
  97. }
  98. if (status < 0)
  99. printk(err_suspend, status);
  100. test_repeat_count_current++;
  101. if (test_repeat_count_current < test_repeat_count_max)
  102. goto repeat;
  103. /* Some platforms can't detect that the alarm triggered the
  104. * wakeup, or (accordingly) disable it after it afterwards.
  105. * It's supposed to give oneshot behavior; cope.
  106. */
  107. alm.enabled = false;
  108. rtc_set_alarm(rtc, &alm);
  109. }
  110. static int __init has_wakealarm(struct device *dev, const void *data)
  111. {
  112. struct rtc_device *candidate = to_rtc_device(dev);
  113. if (!candidate->ops->set_alarm)
  114. return 0;
  115. if (!device_may_wakeup(candidate->dev.parent))
  116. return 0;
  117. return 1;
  118. }
  119. /*
  120. * Kernel options like "test_suspend=mem" force suspend/resume sanity tests
  121. * at startup time. They're normally disabled, for faster boot and because
  122. * we can't know which states really work on this particular system.
  123. */
  124. static const char *test_state_label __initdata;
  125. static char warn_bad_state[] __initdata =
  126. KERN_WARNING "PM: can't test '%s' suspend state\n";
  127. static int __init setup_test_suspend(char *value)
  128. {
  129. int i;
  130. char *repeat;
  131. char *suspend_type;
  132. /* example : "=mem[,N]" ==> "mem[,N]" */
  133. value++;
  134. suspend_type = strsep(&value, ",");
  135. if (!suspend_type)
  136. return 0;
  137. repeat = strsep(&value, ",");
  138. if (repeat) {
  139. if (kstrtou32(repeat, 0, &test_repeat_count_max))
  140. return 0;
  141. }
  142. for (i = 0; pm_labels[i]; i++)
  143. if (!strcmp(pm_labels[i], suspend_type)) {
  144. test_state_label = pm_labels[i];
  145. return 0;
  146. }
  147. printk(warn_bad_state, suspend_type);
  148. return 0;
  149. }
  150. __setup("test_suspend", setup_test_suspend);
  151. static int __init test_suspend(void)
  152. {
  153. static char warn_no_rtc[] __initdata =
  154. KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n";
  155. struct rtc_device *rtc = NULL;
  156. struct device *dev;
  157. suspend_state_t test_state;
  158. /* PM is initialized by now; is that state testable? */
  159. if (!test_state_label)
  160. return 0;
  161. for (test_state = PM_SUSPEND_MIN; test_state < PM_SUSPEND_MAX; test_state++) {
  162. const char *state_label = pm_states[test_state];
  163. if (state_label && !strcmp(test_state_label, state_label))
  164. break;
  165. }
  166. if (test_state == PM_SUSPEND_MAX) {
  167. printk(warn_bad_state, test_state_label);
  168. return 0;
  169. }
  170. /* RTCs have initialized by now too ... can we use one? */
  171. dev = class_find_device(rtc_class, NULL, NULL, has_wakealarm);
  172. if (dev) {
  173. rtc = rtc_class_open(dev_name(dev));
  174. put_device(dev);
  175. }
  176. if (!rtc) {
  177. printk(warn_no_rtc);
  178. return 0;
  179. }
  180. /* go for it */
  181. test_wakealarm(rtc, test_state);
  182. rtc_class_close(rtc);
  183. return 0;
  184. }
  185. late_initcall(test_suspend);