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