Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / gpu / drm / i915 / selftests / i915_selftest.c
blobaddc5a599c4adf65c3a4e122b39f1ca9aee67e8c
1 /*
2 * Copyright © 2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
24 #include <linux/random.h>
26 #include "../i915_drv.h"
27 #include "../i915_selftest.h"
29 struct i915_selftest i915_selftest __read_mostly = {
30 .timeout_ms = 1000,
33 int i915_mock_sanitycheck(void)
35 pr_info(DRIVER_NAME ": %s() - ok!\n", __func__);
36 return 0;
39 int i915_live_sanitycheck(struct drm_i915_private *i915)
41 pr_info("%s: %s() - ok!\n", i915->drm.driver->name, __func__);
42 return 0;
45 enum {
46 #define selftest(name, func) mock_##name,
47 #include "i915_mock_selftests.h"
48 #undef selftest
51 enum {
52 #define selftest(name, func) live_##name,
53 #include "i915_live_selftests.h"
54 #undef selftest
57 struct selftest {
58 bool enabled;
59 const char *name;
60 union {
61 int (*mock)(void);
62 int (*live)(struct drm_i915_private *);
66 #define selftest(n, f) [mock_##n] = { .name = #n, { .mock = f } },
67 static struct selftest mock_selftests[] = {
68 #include "i915_mock_selftests.h"
70 #undef selftest
72 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } },
73 static struct selftest live_selftests[] = {
74 #include "i915_live_selftests.h"
76 #undef selftest
78 /* Embed the line number into the parameter name so that we can order tests */
79 #define selftest(n, func) selftest_0(n, func, param(n))
80 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __mock_##n))
81 #define selftest_0(n, func, id) \
82 module_param_named(id, mock_selftests[mock_##n].enabled, bool, 0400);
83 #include "i915_mock_selftests.h"
84 #undef selftest_0
85 #undef param
87 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __live_##n))
88 #define selftest_0(n, func, id) \
89 module_param_named(id, live_selftests[live_##n].enabled, bool, 0400);
90 #include "i915_live_selftests.h"
91 #undef selftest_0
92 #undef param
93 #undef selftest
95 static void set_default_test_all(struct selftest *st, unsigned int count)
97 unsigned int i;
99 for (i = 0; i < count; i++)
100 if (st[i].enabled)
101 return;
103 for (i = 0; i < count; i++)
104 st[i].enabled = true;
107 static int __run_selftests(const char *name,
108 struct selftest *st,
109 unsigned int count,
110 void *data)
112 int err = 0;
114 while (!i915_selftest.random_seed)
115 i915_selftest.random_seed = get_random_int();
117 i915_selftest.timeout_jiffies =
118 i915_selftest.timeout_ms ?
119 msecs_to_jiffies_timeout(i915_selftest.timeout_ms) :
120 MAX_SCHEDULE_TIMEOUT;
122 set_default_test_all(st, count);
124 pr_info(DRIVER_NAME ": Performing %s selftests with st_random_seed=0x%x st_timeout=%u\n",
125 name, i915_selftest.random_seed, i915_selftest.timeout_ms);
127 /* Tests are listed in order in i915_*_selftests.h */
128 for (; count--; st++) {
129 if (!st->enabled)
130 continue;
132 cond_resched();
133 if (signal_pending(current))
134 return -EINTR;
136 pr_debug(DRIVER_NAME ": Running %s\n", st->name);
137 if (data)
138 err = st->live(data);
139 else
140 err = st->mock();
141 if (err == -EINTR && !signal_pending(current))
142 err = 0;
143 if (err)
144 break;
147 if (WARN(err > 0 || err == -ENOTTY,
148 "%s returned %d, conflicting with selftest's magic values!\n",
149 st->name, err))
150 err = -1;
152 return err;
155 #define run_selftests(x, data) \
156 __run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
158 int i915_mock_selftests(void)
160 int err;
162 if (!i915_selftest.mock)
163 return 0;
165 err = run_selftests(mock, NULL);
166 if (err) {
167 i915_selftest.mock = err;
168 return err;
171 if (i915_selftest.mock < 0) {
172 i915_selftest.mock = -ENOTTY;
173 return 1;
176 return 0;
179 int i915_live_selftests(struct pci_dev *pdev)
181 int err;
183 if (!i915_selftest.live)
184 return 0;
186 err = run_selftests(live, to_i915(pci_get_drvdata(pdev)));
187 if (err) {
188 i915_selftest.live = err;
189 return err;
192 if (i915_selftest.live < 0) {
193 i915_selftest.live = -ENOTTY;
194 return 1;
197 return 0;
200 int __i915_subtests(const char *caller,
201 const struct i915_subtest *st,
202 unsigned int count,
203 void *data)
205 int err;
207 for (; count--; st++) {
208 cond_resched();
209 if (signal_pending(current))
210 return -EINTR;
212 pr_debug(DRIVER_NAME ": Running %s/%s\n", caller, st->name);
213 err = st->func(data);
214 if (err && err != -EINTR) {
215 pr_err(DRIVER_NAME "/%s: %s failed with error %d\n",
216 caller, st->name, err);
217 return err;
221 return 0;
224 bool __igt_timeout(unsigned long timeout, const char *fmt, ...)
226 va_list va;
228 if (!signal_pending(current)) {
229 cond_resched();
230 if (time_before(jiffies, timeout))
231 return false;
234 if (fmt) {
235 va_start(va, fmt);
236 vprintk(fmt, va);
237 va_end(va);
240 return true;
243 module_param_named(st_random_seed, i915_selftest.random_seed, uint, 0400);
244 module_param_named(st_timeout, i915_selftest.timeout_ms, uint, 0400);
246 module_param_named_unsafe(mock_selftests, i915_selftest.mock, int, 0400);
247 MODULE_PARM_DESC(mock_selftests, "Run selftests before loading, using mock hardware (0:disabled [default], 1:run tests then load driver, -1:run tests then exit module)");
249 module_param_named_unsafe(live_selftests, i915_selftest.live, int, 0400);
250 MODULE_PARM_DESC(live_selftests, "Run selftests after driver initialisation on the live system (0:disabled [default], 1:run tests then continue, -1:run tests then exit module)");