2 * kernel/power/suspend_test.c - Suspend to RAM and standby test facility.
4 * Copyright (c) 2009 Pavel Machek <pavel@ucw.cz>
6 * This file is released under the GPLv2.
9 #include <linux/init.h>
10 #include <linux/rtc.h>
15 * We test the system suspend code by setting an RTC wakealarm a short
16 * time in the future, then suspending. Suspending the devices won't
17 * normally take long ... some systems only need a few milliseconds.
19 * The time it takes is system-specific though, so when we test this
20 * during system bootup we allow a LOT of time.
22 #define TEST_SUSPEND_SECONDS 10
24 static unsigned long suspend_test_start_time;
26 void suspend_test_start(void)
28 /* FIXME Use better timebase than "jiffies", ideally a clocksource.
29 * What we want is a hardware counter that will work correctly even
30 * during the irqs-are-off stages of the suspend/resume cycle...
32 suspend_test_start_time = jiffies;
35 void suspend_test_finish(const char *label)
37 long nj = jiffies - suspend_test_start_time;
40 msec = jiffies_to_msecs(abs(nj));
41 pr_info("PM: %s took %d.%03d seconds\n", label,
42 msec / 1000, msec % 1000);
44 /* Warning on suspend means the RTC alarm period needs to be
45 * larger -- the system was sooo slooowwww to suspend that the
46 * alarm (should have) fired before the system went to sleep!
48 * Warning on either suspend or resume also means the system
49 * has some performance issues. The stack dump of a WARN_ON
50 * is more likely to get the right attention than a printk...
52 WARN(msec > (TEST_SUSPEND_SECONDS * 1000),
53 "Component: %s, time: %u\n", label, msec);
57 * To test system suspend, we need a hands-off mechanism to resume the
58 * system. RTCs wake alarms are a common self-contained mechanism.
61 static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state)
63 static char err_readtime[] __initdata =
64 KERN_ERR "PM: can't read %s time, err %d\n";
65 static char err_wakealarm [] __initdata =
66 KERN_ERR "PM: can't set %s wakealarm, err %d\n";
67 static char err_suspend[] __initdata =
68 KERN_ERR "PM: suspend test failed, error %d\n";
69 static char info_test[] __initdata =
70 KERN_INFO "PM: test RTC wakeup from '%s' suspend\n";
73 struct rtc_wkalrm alm;
76 /* this may fail if the RTC hasn't been initialized */
77 status = rtc_read_time(rtc, &alm.time);
79 printk(err_readtime, dev_name(&rtc->dev), status);
82 rtc_tm_to_time(&alm.time, &now);
84 memset(&alm, 0, sizeof alm);
85 rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time);
88 status = rtc_set_alarm(rtc, &alm);
90 printk(err_wakealarm, dev_name(&rtc->dev), status);
94 if (state == PM_SUSPEND_MEM) {
95 printk(info_test, pm_states[state]);
96 status = pm_suspend(state);
97 if (status == -ENODEV)
98 state = PM_SUSPEND_STANDBY;
100 if (state == PM_SUSPEND_STANDBY) {
101 printk(info_test, pm_states[state]);
102 status = pm_suspend(state);
105 printk(err_suspend, status);
107 /* Some platforms can't detect that the alarm triggered the
108 * wakeup, or (accordingly) disable it after it afterwards.
109 * It's supposed to give oneshot behavior; cope.
112 rtc_set_alarm(rtc, &alm);
115 static int __init has_wakealarm(struct device *dev, const void *data)
117 struct rtc_device *candidate = to_rtc_device(dev);
119 if (!candidate->ops->set_alarm)
121 if (!device_may_wakeup(candidate->dev.parent))
128 * Kernel options like "test_suspend=mem" force suspend/resume sanity tests
129 * at startup time. They're normally disabled, for faster boot and because
130 * we can't know which states really work on this particular system.
132 static const char *test_state_label __initdata;
134 static char warn_bad_state[] __initdata =
135 KERN_WARNING "PM: can't test '%s' suspend state\n";
137 static int __init setup_test_suspend(char *value)
141 /* "=mem" ==> "mem" */
143 for (i = 0; pm_labels[i]; i++)
144 if (!strcmp(pm_labels[i], value)) {
145 test_state_label = pm_labels[i];
149 printk(warn_bad_state, value);
152 __setup("test_suspend", setup_test_suspend);
154 static int __init test_suspend(void)
156 static char warn_no_rtc[] __initdata =
157 KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n";
159 struct rtc_device *rtc = NULL;
161 suspend_state_t test_state;
163 /* PM is initialized by now; is that state testable? */
164 if (!test_state_label)
167 for (test_state = PM_SUSPEND_MIN; test_state < PM_SUSPEND_MAX; test_state++) {
168 const char *state_label = pm_states[test_state];
170 if (state_label && !strcmp(test_state_label, state_label))
173 if (test_state == PM_SUSPEND_MAX) {
174 printk(warn_bad_state, test_state_label);
178 /* RTCs have initialized by now too ... can we use one? */
179 dev = class_find_device(rtc_class, NULL, NULL, has_wakealarm);
181 rtc = rtc_class_open(dev_name(dev));
188 test_wakealarm(rtc, test_state);
189 rtc_class_close(rtc);
192 late_initcall(test_suspend);