1 /* $NetBSD: i915_selftest.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $ */
2
3 /*
4 * Copyright © 2016 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 */
25
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: i915_selftest.c,v 1.2 2021/12/18 23:45:31 riastradh Exp $");
28
29 #include <linux/random.h>
30
31 #include "gt/intel_gt_pm.h"
32 #include "i915_drv.h"
33 #include "i915_selftest.h"
34
35 #include "igt_flush_test.h"
36
37 struct i915_selftest i915_selftest __read_mostly = {
38 .timeout_ms = 500,
39 };
40
i915_mock_sanitycheck(void)41 int i915_mock_sanitycheck(void)
42 {
43 pr_info(DRIVER_NAME ": %s() - ok!\n", __func__);
44 return 0;
45 }
46
i915_live_sanitycheck(struct drm_i915_private * i915)47 int i915_live_sanitycheck(struct drm_i915_private *i915)
48 {
49 pr_info("%s: %s() - ok!\n", i915->drm.driver->name, __func__);
50 return 0;
51 }
52
53 enum {
54 #define selftest(name, func) mock_##name,
55 #include "i915_mock_selftests.h"
56 #undef selftest
57 };
58
59 enum {
60 #define selftest(name, func) live_##name,
61 #include "i915_live_selftests.h"
62 #undef selftest
63 };
64
65 enum {
66 #define selftest(name, func) perf_##name,
67 #include "i915_perf_selftests.h"
68 #undef selftest
69 };
70
71 struct selftest {
72 bool enabled;
73 const char *name;
74 union {
75 int (*mock)(void);
76 int (*live)(struct drm_i915_private *);
77 };
78 };
79
80 #define selftest(n, f) [mock_##n] = { .name = #n, { .mock = f } },
81 static struct selftest mock_selftests[] = {
82 #include "i915_mock_selftests.h"
83 };
84 #undef selftest
85
86 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } },
87 static struct selftest live_selftests[] = {
88 #include "i915_live_selftests.h"
89 };
90 #undef selftest
91
92 #define selftest(n, f) [perf_##n] = { .name = #n, { .live = f } },
93 static struct selftest perf_selftests[] = {
94 #include "i915_perf_selftests.h"
95 };
96 #undef selftest
97
98 /* Embed the line number into the parameter name so that we can order tests */
99 #define selftest(n, func) selftest_0(n, func, param(n))
100 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __mock_##n))
101 #define selftest_0(n, func, id) \
102 module_param_named(id, mock_selftests[mock_##n].enabled, bool, 0400);
103 #include "i915_mock_selftests.h"
104 #undef selftest_0
105 #undef param
106
107 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __live_##n))
108 #define selftest_0(n, func, id) \
109 module_param_named(id, live_selftests[live_##n].enabled, bool, 0400);
110 #include "i915_live_selftests.h"
111 #undef selftest_0
112 #undef param
113
114 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __perf_##n))
115 #define selftest_0(n, func, id) \
116 module_param_named(id, perf_selftests[perf_##n].enabled, bool, 0400);
117 #include "i915_perf_selftests.h"
118 #undef selftest_0
119 #undef param
120 #undef selftest
121
set_default_test_all(struct selftest * st,unsigned int count)122 static void set_default_test_all(struct selftest *st, unsigned int count)
123 {
124 unsigned int i;
125
126 for (i = 0; i < count; i++)
127 if (st[i].enabled)
128 return;
129
130 for (i = 0; i < count; i++)
131 st[i].enabled = true;
132 }
133
__run_selftests(const char * name,struct selftest * st,unsigned int count,void * data)134 static int __run_selftests(const char *name,
135 struct selftest *st,
136 unsigned int count,
137 void *data)
138 {
139 int err = 0;
140
141 while (!i915_selftest.random_seed)
142 i915_selftest.random_seed = get_random_int();
143
144 i915_selftest.timeout_jiffies =
145 i915_selftest.timeout_ms ?
146 msecs_to_jiffies_timeout(i915_selftest.timeout_ms) :
147 MAX_SCHEDULE_TIMEOUT;
148
149 set_default_test_all(st, count);
150
151 pr_info(DRIVER_NAME ": Performing %s selftests with st_random_seed=0x%x st_timeout=%u\n",
152 name, i915_selftest.random_seed, i915_selftest.timeout_ms);
153
154 /* Tests are listed in order in i915_*_selftests.h */
155 for (; count--; st++) {
156 if (!st->enabled)
157 continue;
158
159 cond_resched();
160 if (signal_pending(current))
161 return -EINTR;
162
163 pr_info(DRIVER_NAME ": Running %s\n", st->name);
164 if (data)
165 err = st->live(data);
166 else
167 err = st->mock();
168 if (err == -EINTR && !signal_pending(current))
169 err = 0;
170 if (err)
171 break;
172 }
173
174 if (WARN(err > 0 || err == -ENOTTY,
175 "%s returned %d, conflicting with selftest's magic values!\n",
176 st->name, err))
177 err = -1;
178
179 return err;
180 }
181
182 #define run_selftests(x, data) \
183 __run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
184
i915_mock_selftests(void)185 int i915_mock_selftests(void)
186 {
187 int err;
188
189 if (!i915_selftest.mock)
190 return 0;
191
192 err = run_selftests(mock, NULL);
193 if (err) {
194 i915_selftest.mock = err;
195 return err;
196 }
197
198 if (i915_selftest.mock < 0) {
199 i915_selftest.mock = -ENOTTY;
200 return 1;
201 }
202
203 return 0;
204 }
205
i915_live_selftests(struct pci_dev * pdev)206 int i915_live_selftests(struct pci_dev *pdev)
207 {
208 int err;
209
210 if (!i915_selftest.live)
211 return 0;
212
213 err = run_selftests(live, pdev_to_i915(pdev));
214 if (err) {
215 i915_selftest.live = err;
216 return err;
217 }
218
219 if (i915_selftest.live < 0) {
220 i915_selftest.live = -ENOTTY;
221 return 1;
222 }
223
224 return 0;
225 }
226
i915_perf_selftests(struct pci_dev * pdev)227 int i915_perf_selftests(struct pci_dev *pdev)
228 {
229 int err;
230
231 if (!i915_selftest.perf)
232 return 0;
233
234 err = run_selftests(perf, pdev_to_i915(pdev));
235 if (err) {
236 i915_selftest.perf = err;
237 return err;
238 }
239
240 if (i915_selftest.perf < 0) {
241 i915_selftest.perf = -ENOTTY;
242 return 1;
243 }
244
245 return 0;
246 }
247
apply_subtest_filter(const char * caller,const char * name)248 static bool apply_subtest_filter(const char *caller, const char *name)
249 {
250 char *filter, *sep, *tok;
251 bool result = true;
252
253 filter = kstrdup(i915_selftest.filter, GFP_KERNEL);
254 for (sep = filter; (tok = strsep(&sep, ","));) {
255 bool allow = true;
256 char *sl;
257
258 if (*tok == '!') {
259 allow = false;
260 tok++;
261 }
262
263 if (*tok == '\0')
264 continue;
265
266 sl = strchr(tok, '/');
267 if (sl) {
268 *sl++ = '\0';
269 if (strcmp(tok, caller)) {
270 if (allow)
271 result = false;
272 continue;
273 }
274 tok = sl;
275 }
276
277 if (strcmp(tok, name)) {
278 if (allow)
279 result = false;
280 continue;
281 }
282
283 result = allow;
284 break;
285 }
286 kfree(filter);
287
288 return result;
289 }
290
__i915_nop_setup(void * data)291 int __i915_nop_setup(void *data)
292 {
293 return 0;
294 }
295
__i915_nop_teardown(int err,void * data)296 int __i915_nop_teardown(int err, void *data)
297 {
298 return err;
299 }
300
__i915_live_setup(void * data)301 int __i915_live_setup(void *data)
302 {
303 struct drm_i915_private *i915 = data;
304
305 /* The selftests expect an idle system */
306 if (intel_gt_pm_wait_for_idle(&i915->gt))
307 return -EIO;
308
309 return intel_gt_terminally_wedged(&i915->gt);
310 }
311
__i915_live_teardown(int err,void * data)312 int __i915_live_teardown(int err, void *data)
313 {
314 struct drm_i915_private *i915 = data;
315
316 if (igt_flush_test(i915))
317 err = -EIO;
318
319 i915_gem_drain_freed_objects(i915);
320
321 return err;
322 }
323
__intel_gt_live_setup(void * data)324 int __intel_gt_live_setup(void *data)
325 {
326 struct intel_gt *gt = data;
327
328 /* The selftests expect an idle system */
329 if (intel_gt_pm_wait_for_idle(gt))
330 return -EIO;
331
332 return intel_gt_terminally_wedged(gt);
333 }
334
__intel_gt_live_teardown(int err,void * data)335 int __intel_gt_live_teardown(int err, void *data)
336 {
337 struct intel_gt *gt = data;
338
339 if (igt_flush_test(gt->i915))
340 err = -EIO;
341
342 i915_gem_drain_freed_objects(gt->i915);
343
344 return err;
345 }
346
__i915_subtests(const char * caller,int (* setup)(void * data),int (* teardown)(int err,void * data),const struct i915_subtest * st,unsigned int count,void * data)347 int __i915_subtests(const char *caller,
348 int (*setup)(void *data),
349 int (*teardown)(int err, void *data),
350 const struct i915_subtest *st,
351 unsigned int count,
352 void *data)
353 {
354 int err;
355
356 for (; count--; st++) {
357 cond_resched();
358 if (signal_pending(current))
359 return -EINTR;
360
361 if (!apply_subtest_filter(caller, st->name))
362 continue;
363
364 err = setup(data);
365 if (err) {
366 pr_err(DRIVER_NAME "/%s: setup failed for %s\n",
367 caller, st->name);
368 return err;
369 }
370
371 pr_info(DRIVER_NAME ": Running %s/%s\n", caller, st->name);
372 GEM_TRACE("Running %s/%s\n", caller, st->name);
373
374 err = teardown(st->func(data), data);
375 if (err && err != -EINTR) {
376 pr_err(DRIVER_NAME "/%s: %s failed with error %d\n",
377 caller, st->name, err);
378 return err;
379 }
380 }
381
382 return 0;
383 }
384
__igt_timeout(unsigned long timeout,const char * fmt,...)385 bool __igt_timeout(unsigned long timeout, const char *fmt, ...)
386 {
387 va_list va;
388
389 if (!signal_pending(current)) {
390 cond_resched();
391 if (time_before(jiffies, timeout))
392 return false;
393 }
394
395 if (fmt) {
396 va_start(va, fmt);
397 vprintk(fmt, va);
398 va_end(va);
399 }
400
401 return true;
402 }
403
404 module_param_named(st_random_seed, i915_selftest.random_seed, uint, 0400);
405 module_param_named(st_timeout, i915_selftest.timeout_ms, uint, 0400);
406 module_param_named(st_filter, i915_selftest.filter, charp, 0400);
407
408 module_param_named_unsafe(mock_selftests, i915_selftest.mock, int, 0400);
409 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)");
410
411 module_param_named_unsafe(live_selftests, i915_selftest.live, int, 0400);
412 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)");
413
414 module_param_named_unsafe(perf_selftests, i915_selftest.perf, int, 0400);
415 MODULE_PARM_DESC(perf_selftests, "Run performance orientated selftests after driver initialisation on the live system (0:disabled [default], 1:run tests then continue, -1:run tests then exit module)");
416