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
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
24 #include <linux/random.h>
26 #include "gt/intel_gt_pm.h"
28 #include "i915_selftest.h"
30 #include "igt_flush_test.h"
32 struct i915_selftest i915_selftest __read_mostly
= {
36 int i915_mock_sanitycheck(void)
38 pr_info(DRIVER_NAME
": %s() - ok!\n", __func__
);
42 int i915_live_sanitycheck(struct drm_i915_private
*i915
)
44 pr_info("%s: %s() - ok!\n", i915
->drm
.driver
->name
, __func__
);
49 #define selftest(name, func) mock_##name,
50 #include "i915_mock_selftests.h"
55 #define selftest(name, func) live_##name,
56 #include "i915_live_selftests.h"
61 #define selftest(name, func) perf_##name,
62 #include "i915_perf_selftests.h"
71 int (*live
)(struct drm_i915_private
*);
75 #define selftest(n, f) [mock_##n] = { .name = #n, { .mock = f } },
76 static struct selftest mock_selftests
[] = {
77 #include "i915_mock_selftests.h"
81 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } },
82 static struct selftest live_selftests
[] = {
83 #include "i915_live_selftests.h"
87 #define selftest(n, f) [perf_##n] = { .name = #n, { .live = f } },
88 static struct selftest perf_selftests
[] = {
89 #include "i915_perf_selftests.h"
93 /* Embed the line number into the parameter name so that we can order tests */
94 #define selftest(n, func) selftest_0(n, func, param(n))
95 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __mock_##n))
96 #define selftest_0(n, func, id) \
97 module_param_named(id, mock_selftests[mock_##n].enabled, bool, 0400);
98 #include "i915_mock_selftests.h"
102 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __live_##n))
103 #define selftest_0(n, func, id) \
104 module_param_named(id, live_selftests[live_##n].enabled, bool, 0400);
105 #include "i915_live_selftests.h"
109 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __perf_##n))
110 #define selftest_0(n, func, id) \
111 module_param_named(id, perf_selftests[perf_##n].enabled, bool, 0400);
112 #include "i915_perf_selftests.h"
117 static void set_default_test_all(struct selftest
*st
, unsigned int count
)
121 for (i
= 0; i
< count
; i
++)
125 for (i
= 0; i
< count
; i
++)
126 st
[i
].enabled
= true;
129 static int __run_selftests(const char *name
,
136 while (!i915_selftest
.random_seed
)
137 i915_selftest
.random_seed
= get_random_int();
139 i915_selftest
.timeout_jiffies
=
140 i915_selftest
.timeout_ms
?
141 msecs_to_jiffies_timeout(i915_selftest
.timeout_ms
) :
142 MAX_SCHEDULE_TIMEOUT
;
144 set_default_test_all(st
, count
);
146 pr_info(DRIVER_NAME
": Performing %s selftests with st_random_seed=0x%x st_timeout=%u\n",
147 name
, i915_selftest
.random_seed
, i915_selftest
.timeout_ms
);
149 /* Tests are listed in order in i915_*_selftests.h */
150 for (; count
--; st
++) {
155 if (signal_pending(current
))
158 pr_info(DRIVER_NAME
": Running %s\n", st
->name
);
160 err
= st
->live(data
);
163 if (err
== -EINTR
&& !signal_pending(current
))
169 if (WARN(err
> 0 || err
== -ENOTTY
,
170 "%s returned %d, conflicting with selftest's magic values!\n",
177 #define run_selftests(x, data) \
178 __run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
180 int i915_mock_selftests(void)
184 if (!i915_selftest
.mock
)
187 err
= run_selftests(mock
, NULL
);
189 i915_selftest
.mock
= err
;
193 if (i915_selftest
.mock
< 0) {
194 i915_selftest
.mock
= -ENOTTY
;
201 int i915_live_selftests(struct pci_dev
*pdev
)
205 if (!i915_selftest
.live
)
208 err
= run_selftests(live
, pdev_to_i915(pdev
));
210 i915_selftest
.live
= err
;
214 if (i915_selftest
.live
< 0) {
215 i915_selftest
.live
= -ENOTTY
;
222 int i915_perf_selftests(struct pci_dev
*pdev
)
226 if (!i915_selftest
.perf
)
229 err
= run_selftests(perf
, pdev_to_i915(pdev
));
231 i915_selftest
.perf
= err
;
235 if (i915_selftest
.perf
< 0) {
236 i915_selftest
.perf
= -ENOTTY
;
243 static bool apply_subtest_filter(const char *caller
, const char *name
)
245 char *filter
, *sep
, *tok
;
248 filter
= kstrdup(i915_selftest
.filter
, GFP_KERNEL
);
249 for (sep
= filter
; (tok
= strsep(&sep
, ","));) {
261 sl
= strchr(tok
, '/');
264 if (strcmp(tok
, caller
)) {
272 if (strcmp(tok
, name
)) {
286 int __i915_nop_setup(void *data
)
291 int __i915_nop_teardown(int err
, void *data
)
296 int __i915_live_setup(void *data
)
298 struct drm_i915_private
*i915
= data
;
300 /* The selftests expect an idle system */
301 if (intel_gt_pm_wait_for_idle(&i915
->gt
))
304 return intel_gt_terminally_wedged(&i915
->gt
);
307 int __i915_live_teardown(int err
, void *data
)
309 struct drm_i915_private
*i915
= data
;
311 if (igt_flush_test(i915
))
314 i915_gem_drain_freed_objects(i915
);
319 int __intel_gt_live_setup(void *data
)
321 struct intel_gt
*gt
= data
;
323 /* The selftests expect an idle system */
324 if (intel_gt_pm_wait_for_idle(gt
))
327 return intel_gt_terminally_wedged(gt
);
330 int __intel_gt_live_teardown(int err
, void *data
)
332 struct intel_gt
*gt
= data
;
334 if (igt_flush_test(gt
->i915
))
337 i915_gem_drain_freed_objects(gt
->i915
);
342 int __i915_subtests(const char *caller
,
343 int (*setup
)(void *data
),
344 int (*teardown
)(int err
, void *data
),
345 const struct i915_subtest
*st
,
351 for (; count
--; st
++) {
353 if (signal_pending(current
))
356 if (!apply_subtest_filter(caller
, st
->name
))
361 pr_err(DRIVER_NAME
"/%s: setup failed for %s\n",
366 pr_info(DRIVER_NAME
": Running %s/%s\n", caller
, st
->name
);
367 GEM_TRACE("Running %s/%s\n", caller
, st
->name
);
369 err
= teardown(st
->func(data
), data
);
370 if (err
&& err
!= -EINTR
) {
371 pr_err(DRIVER_NAME
"/%s: %s failed with error %d\n",
372 caller
, st
->name
, err
);
380 bool __igt_timeout(unsigned long timeout
, const char *fmt
, ...)
384 if (!signal_pending(current
)) {
386 if (time_before(jiffies
, timeout
))
399 void igt_hexdump(const void *buf
, size_t len
)
401 const size_t rowsize
= 8 * sizeof(u32
);
402 const void *prev
= NULL
;
406 for (pos
= 0; pos
< len
; pos
+= rowsize
) {
409 if (prev
&& !memcmp(prev
, buf
+ pos
, rowsize
)) {
417 WARN_ON_ONCE(hex_dump_to_buffer(buf
+ pos
, len
- pos
,
418 rowsize
, sizeof(u32
),
420 false) >= sizeof(line
));
421 pr_info("[%04zx] %s\n", pos
, line
);
428 module_param_named(st_random_seed
, i915_selftest
.random_seed
, uint
, 0400);
429 module_param_named(st_timeout
, i915_selftest
.timeout_ms
, uint
, 0400);
430 module_param_named(st_filter
, i915_selftest
.filter
, charp
, 0400);
432 module_param_named_unsafe(mock_selftests
, i915_selftest
.mock
, int, 0400);
433 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)");
435 module_param_named_unsafe(live_selftests
, i915_selftest
.live
, int, 0400);
436 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)");
438 module_param_named_unsafe(perf_selftests
, i915_selftest
.perf
, int, 0400);
439 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)");