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"
27 #include "i915_driver.h"
29 #include "i915_selftest.h"
31 #include "igt_flush_test.h"
33 struct i915_selftest i915_selftest __read_mostly = {
37 int i915_mock_sanitycheck(void)
39 pr_info(DRIVER_NAME ": %s() - ok!\n", __func__);
43 int i915_live_sanitycheck(struct drm_i915_private *i915)
45 pr_info("%s: %s() - ok!\n", i915->drm.driver->name, __func__);
50 #define selftest(name, func) mock_##name,
51 #include "i915_mock_selftests.h"
56 #define selftest(name, func) live_##name,
57 #include "i915_live_selftests.h"
62 #define selftest(name, func) perf_##name,
63 #include "i915_perf_selftests.h"
72 int (*live)(struct drm_i915_private *);
76 #define selftest(n, f) [mock_##n] = { .name = #n, { .mock = f } },
77 static struct selftest mock_selftests[] = {
78 #include "i915_mock_selftests.h"
82 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } },
83 static struct selftest live_selftests[] = {
84 #include "i915_live_selftests.h"
88 #define selftest(n, f) [perf_##n] = { .name = #n, { .live = f } },
89 static struct selftest perf_selftests[] = {
90 #include "i915_perf_selftests.h"
94 /* Embed the line number into the parameter name so that we can order tests */
95 #define selftest(n, func) selftest_0(n, func, param(n))
96 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __mock_##n))
97 #define selftest_0(n, func, id) \
98 module_param_named(id, mock_selftests[mock_##n].enabled, bool, 0400);
99 #include "i915_mock_selftests.h"
103 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __live_##n))
104 #define selftest_0(n, func, id) \
105 module_param_named(id, live_selftests[live_##n].enabled, bool, 0400);
106 #include "i915_live_selftests.h"
110 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __perf_##n))
111 #define selftest_0(n, func, id) \
112 module_param_named(id, perf_selftests[perf_##n].enabled, bool, 0400);
113 #include "i915_perf_selftests.h"
118 static void set_default_test_all(struct selftest *st, unsigned int count)
122 for (i = 0; i < count; i++)
126 for (i = 0; i < count; i++)
127 st[i].enabled = true;
130 static int __run_selftests(const char *name,
137 while (!i915_selftest.random_seed)
138 i915_selftest.random_seed = get_random_u32();
140 i915_selftest.timeout_jiffies =
141 i915_selftest.timeout_ms ?
142 msecs_to_jiffies_timeout(i915_selftest.timeout_ms) :
143 MAX_SCHEDULE_TIMEOUT;
145 set_default_test_all(st, count);
147 pr_info(DRIVER_NAME ": Performing %s selftests with st_random_seed=0x%x st_timeout=%u\n",
148 name, i915_selftest.random_seed, i915_selftest.timeout_ms);
150 /* Tests are listed in order in i915_*_selftests.h */
151 for (; count--; st++) {
156 if (signal_pending(current))
159 pr_info(DRIVER_NAME ": Running %s\n", st->name);
161 err = st->live(data);
164 if (err == -EINTR && !signal_pending(current))
170 if (WARN(err > 0 || err == -ENOTTY,
171 "%s returned %d, conflicting with selftest's magic values!\n",
178 #define run_selftests(x, data) \
179 __run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
181 int i915_mock_selftests(void)
185 if (!i915_selftest.mock)
188 err = run_selftests(mock, NULL);
190 i915_selftest.mock = err;
194 if (i915_selftest.mock < 0) {
195 i915_selftest.mock = -ENOTTY;
202 int i915_live_selftests(struct pci_dev *pdev)
206 if (!i915_selftest.live)
209 err = run_selftests(live, pdev_to_i915(pdev));
211 i915_selftest.live = err;
215 if (i915_selftest.live < 0) {
216 i915_selftest.live = -ENOTTY;
223 int i915_perf_selftests(struct pci_dev *pdev)
227 if (!i915_selftest.perf)
230 err = run_selftests(perf, pdev_to_i915(pdev));
232 i915_selftest.perf = err;
236 if (i915_selftest.perf < 0) {
237 i915_selftest.perf = -ENOTTY;
244 static bool apply_subtest_filter(const char *caller, const char *name)
246 char *filter, *sep, *tok;
249 filter = kstrdup(i915_selftest.filter, GFP_KERNEL);
250 for (sep = filter; (tok = strsep(&sep, ","));) {
262 sl = strchr(tok, '/');
265 if (strcmp(tok, caller)) {
273 if (strcmp(tok, name)) {
287 int __i915_nop_setup(void *data)
292 int __i915_nop_teardown(int err, void *data)
297 int __i915_live_setup(void *data)
299 struct drm_i915_private *i915 = data;
301 /* The selftests expect an idle system */
302 if (intel_gt_pm_wait_for_idle(to_gt(i915)))
305 return intel_gt_terminally_wedged(to_gt(i915));
308 int __i915_live_teardown(int err, void *data)
310 struct drm_i915_private *i915 = data;
312 if (igt_flush_test(i915))
315 i915_gem_drain_freed_objects(i915);
320 int __intel_gt_live_setup(void *data)
322 struct intel_gt *gt = data;
324 /* The selftests expect an idle system */
325 if (intel_gt_pm_wait_for_idle(gt))
328 return intel_gt_terminally_wedged(gt);
331 int __intel_gt_live_teardown(int err, void *data)
333 struct intel_gt *gt = data;
335 if (igt_flush_test(gt->i915))
338 i915_gem_drain_freed_objects(gt->i915);
343 int __i915_subtests(const char *caller,
344 int (*setup)(void *data),
345 int (*teardown)(int err, void *data),
346 const struct i915_subtest *st,
352 for (; count--; st++) {
354 if (signal_pending(current))
357 if (!apply_subtest_filter(caller, st->name))
362 pr_err(DRIVER_NAME "/%s: setup failed for %s\n",
367 pr_info(DRIVER_NAME ": Running %s/%s\n", caller, st->name);
368 GEM_TRACE("Running %s/%s\n", caller, st->name);
370 err = teardown(st->func(data), data);
371 if (err && err != -EINTR) {
372 pr_err(DRIVER_NAME "/%s: %s failed with error %d\n",
373 caller, st->name, err);
381 bool __igt_timeout(unsigned long timeout, const char *fmt, ...)
385 if (!signal_pending(current)) {
387 if (time_before(jiffies, timeout))
400 void igt_hexdump(const void *buf, size_t len)
402 const size_t rowsize = 8 * sizeof(u32);
403 const void *prev = NULL;
407 for (pos = 0; pos < len; pos += rowsize) {
410 if (prev && !memcmp(prev, buf + pos, rowsize)) {
418 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
419 rowsize, sizeof(u32),
421 false) >= sizeof(line));
422 pr_info("[%04zx] %s\n", pos, line);
429 module_param_named(st_random_seed, i915_selftest.random_seed, uint, 0400);
430 module_param_named(st_timeout, i915_selftest.timeout_ms, uint, 0400);
431 module_param_named(st_filter, i915_selftest.filter, charp, 0400);
433 module_param_named_unsafe(mock_selftests, i915_selftest.mock, int, 0400);
434 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 leave dummy module)");
436 module_param_named_unsafe(live_selftests, i915_selftest.live, int, 0400);
437 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)");
439 module_param_named_unsafe(perf_selftests, i915_selftest.perf, int, 0400);
440 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)");