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