| Rafael J. Wysocki | a9d7052 | 2009-06-10 01:27:12 +0200 | [diff] [blame] | 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 |  | 
 | 9 | #include <linux/init.h> | 
 | 10 | #include <linux/rtc.h> | 
 | 11 |  | 
 | 12 | #include "power.h" | 
 | 13 |  | 
 | 14 | /* | 
 | 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. | 
 | 18 |  * | 
 | 19 |  * The time it takes is system-specific though, so when we test this | 
 | 20 |  * during system bootup we allow a LOT of time. | 
 | 21 |  */ | 
 | 22 | #define TEST_SUSPEND_SECONDS	5 | 
 | 23 |  | 
 | 24 | static unsigned long suspend_test_start_time; | 
 | 25 |  | 
 | 26 | void suspend_test_start(void) | 
 | 27 | { | 
 | 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... | 
 | 31 | 	 */ | 
 | 32 | 	suspend_test_start_time = jiffies; | 
 | 33 | } | 
 | 34 |  | 
 | 35 | void suspend_test_finish(const char *label) | 
 | 36 | { | 
 | 37 | 	long nj = jiffies - suspend_test_start_time; | 
 | 38 | 	unsigned msec; | 
 | 39 |  | 
 | 40 | 	msec = jiffies_to_msecs(abs(nj)); | 
 | 41 | 	pr_info("PM: %s took %d.%03d seconds\n", label, | 
 | 42 | 			msec / 1000, msec % 1000); | 
 | 43 |  | 
 | 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! | 
 | 47 | 	 * | 
 | 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... | 
 | 51 | 	 */ | 
 | 52 | 	WARN(msec > (TEST_SUSPEND_SECONDS * 1000), "Component: %s\n", label); | 
 | 53 | } | 
 | 54 |  | 
 | 55 | /* | 
 | 56 |  * To test system suspend, we need a hands-off mechanism to resume the | 
 | 57 |  * system.  RTCs wake alarms are a common self-contained mechanism. | 
 | 58 |  */ | 
 | 59 |  | 
 | 60 | static void __init test_wakealarm(struct rtc_device *rtc, suspend_state_t state) | 
 | 61 | { | 
 | 62 | 	static char err_readtime[] __initdata = | 
 | 63 | 		KERN_ERR "PM: can't read %s time, err %d\n"; | 
 | 64 | 	static char err_wakealarm [] __initdata = | 
 | 65 | 		KERN_ERR "PM: can't set %s wakealarm, err %d\n"; | 
 | 66 | 	static char err_suspend[] __initdata = | 
 | 67 | 		KERN_ERR "PM: suspend test failed, error %d\n"; | 
 | 68 | 	static char info_test[] __initdata = | 
 | 69 | 		KERN_INFO "PM: test RTC wakeup from '%s' suspend\n"; | 
 | 70 |  | 
 | 71 | 	unsigned long		now; | 
 | 72 | 	struct rtc_wkalrm	alm; | 
 | 73 | 	int			status; | 
 | 74 |  | 
 | 75 | 	/* this may fail if the RTC hasn't been initialized */ | 
 | 76 | 	status = rtc_read_time(rtc, &alm.time); | 
 | 77 | 	if (status < 0) { | 
 | 78 | 		printk(err_readtime, dev_name(&rtc->dev), status); | 
 | 79 | 		return; | 
 | 80 | 	} | 
 | 81 | 	rtc_tm_to_time(&alm.time, &now); | 
 | 82 |  | 
 | 83 | 	memset(&alm, 0, sizeof alm); | 
 | 84 | 	rtc_time_to_tm(now + TEST_SUSPEND_SECONDS, &alm.time); | 
 | 85 | 	alm.enabled = true; | 
 | 86 |  | 
 | 87 | 	status = rtc_set_alarm(rtc, &alm); | 
 | 88 | 	if (status < 0) { | 
 | 89 | 		printk(err_wakealarm, dev_name(&rtc->dev), status); | 
 | 90 | 		return; | 
 | 91 | 	} | 
 | 92 |  | 
 | 93 | 	if (state == PM_SUSPEND_MEM) { | 
 | 94 | 		printk(info_test, pm_states[state]); | 
 | 95 | 		status = pm_suspend(state); | 
 | 96 | 		if (status == -ENODEV) | 
 | 97 | 			state = PM_SUSPEND_STANDBY; | 
 | 98 | 	} | 
 | 99 | 	if (state == PM_SUSPEND_STANDBY) { | 
 | 100 | 		printk(info_test, pm_states[state]); | 
 | 101 | 		status = pm_suspend(state); | 
 | 102 | 	} | 
 | 103 | 	if (status < 0) | 
 | 104 | 		printk(err_suspend, status); | 
 | 105 |  | 
 | 106 | 	/* Some platforms can't detect that the alarm triggered the | 
 | 107 | 	 * wakeup, or (accordingly) disable it after it afterwards. | 
 | 108 | 	 * It's supposed to give oneshot behavior; cope. | 
 | 109 | 	 */ | 
 | 110 | 	alm.enabled = false; | 
 | 111 | 	rtc_set_alarm(rtc, &alm); | 
 | 112 | } | 
 | 113 |  | 
 | 114 | static int __init has_wakealarm(struct device *dev, void *name_ptr) | 
 | 115 | { | 
 | 116 | 	struct rtc_device *candidate = to_rtc_device(dev); | 
 | 117 |  | 
 | 118 | 	if (!candidate->ops->set_alarm) | 
 | 119 | 		return 0; | 
 | 120 | 	if (!device_may_wakeup(candidate->dev.parent)) | 
 | 121 | 		return 0; | 
 | 122 |  | 
 | 123 | 	*(const char **)name_ptr = dev_name(dev); | 
 | 124 | 	return 1; | 
 | 125 | } | 
 | 126 |  | 
 | 127 | /* | 
 | 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. | 
 | 131 |  */ | 
 | 132 | static suspend_state_t test_state __initdata = PM_SUSPEND_ON; | 
 | 133 |  | 
 | 134 | static char warn_bad_state[] __initdata = | 
 | 135 | 	KERN_WARNING "PM: can't test '%s' suspend state\n"; | 
 | 136 |  | 
 | 137 | static int __init setup_test_suspend(char *value) | 
 | 138 | { | 
 | 139 | 	unsigned i; | 
 | 140 |  | 
 | 141 | 	/* "=mem" ==> "mem" */ | 
 | 142 | 	value++; | 
 | 143 | 	for (i = 0; i < PM_SUSPEND_MAX; i++) { | 
 | 144 | 		if (!pm_states[i]) | 
 | 145 | 			continue; | 
 | 146 | 		if (strcmp(pm_states[i], value) != 0) | 
 | 147 | 			continue; | 
 | 148 | 		test_state = (__force suspend_state_t) i; | 
 | 149 | 		return 0; | 
 | 150 | 	} | 
 | 151 | 	printk(warn_bad_state, value); | 
 | 152 | 	return 0; | 
 | 153 | } | 
 | 154 | __setup("test_suspend", setup_test_suspend); | 
 | 155 |  | 
 | 156 | static int __init test_suspend(void) | 
 | 157 | { | 
 | 158 | 	static char		warn_no_rtc[] __initdata = | 
 | 159 | 		KERN_WARNING "PM: no wakealarm-capable RTC driver is ready\n"; | 
 | 160 |  | 
 | 161 | 	char			*pony = NULL; | 
 | 162 | 	struct rtc_device	*rtc = NULL; | 
 | 163 |  | 
 | 164 | 	/* PM is initialized by now; is that state testable? */ | 
 | 165 | 	if (test_state == PM_SUSPEND_ON) | 
 | 166 | 		goto done; | 
 | 167 | 	if (!valid_state(test_state)) { | 
 | 168 | 		printk(warn_bad_state, pm_states[test_state]); | 
 | 169 | 		goto done; | 
 | 170 | 	} | 
 | 171 |  | 
 | 172 | 	/* RTCs have initialized by now too ... can we use one? */ | 
 | 173 | 	class_find_device(rtc_class, NULL, &pony, has_wakealarm); | 
 | 174 | 	if (pony) | 
 | 175 | 		rtc = rtc_class_open(pony); | 
 | 176 | 	if (!rtc) { | 
 | 177 | 		printk(warn_no_rtc); | 
 | 178 | 		goto done; | 
 | 179 | 	} | 
 | 180 |  | 
 | 181 | 	/* go for it */ | 
 | 182 | 	test_wakealarm(rtc, test_state); | 
 | 183 | 	rtc_class_close(rtc); | 
 | 184 | done: | 
 | 185 | 	return 0; | 
 | 186 | } | 
 | 187 | late_initcall(test_suspend); |