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 "../i915_drv.h"
27 #include "../i915_selftest.h"
29 #include "igt_flush_test.h"
31 struct i915_selftest i915_selftest __read_mostly = {
35 int i915_mock_sanitycheck(void)
37 pr_info(DRIVER_NAME ": %s() - ok!\n", __func__);
41 int i915_live_sanitycheck(struct drm_i915_private *i915)
43 pr_info("%s: %s() - ok!\n", i915->drm.driver->name, __func__);
48 #define selftest(name, func) mock_##name,
49 #include "i915_mock_selftests.h"
54 #define selftest(name, func) live_##name,
55 #include "i915_live_selftests.h"
64 int (*live)(struct drm_i915_private *);
68 #define selftest(n, f) [mock_##n] = { .name = #n, { .mock = f } },
69 static struct selftest mock_selftests[] = {
70 #include "i915_mock_selftests.h"
74 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } },
75 static struct selftest live_selftests[] = {
76 #include "i915_live_selftests.h"
80 /* Embed the line number into the parameter name so that we can order tests */
81 #define selftest(n, func) selftest_0(n, func, param(n))
82 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __mock_##n))
83 #define selftest_0(n, func, id) \
84 module_param_named(id, mock_selftests[mock_##n].enabled, bool, 0400);
85 #include "i915_mock_selftests.h"
89 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __live_##n))
90 #define selftest_0(n, func, id) \
91 module_param_named(id, live_selftests[live_##n].enabled, bool, 0400);
92 #include "i915_live_selftests.h"
97 static void set_default_test_all(struct selftest *st, unsigned int count)
101 for (i = 0; i < count; i++)
105 for (i = 0; i < count; i++)
106 st[i].enabled = true;
109 static int __run_selftests(const char *name,
116 while (!i915_selftest.random_seed)
117 i915_selftest.random_seed = get_random_int();
119 i915_selftest.timeout_jiffies =
120 i915_selftest.timeout_ms ?
121 msecs_to_jiffies_timeout(i915_selftest.timeout_ms) :
122 MAX_SCHEDULE_TIMEOUT;
124 set_default_test_all(st, count);
126 pr_info(DRIVER_NAME ": Performing %s selftests with st_random_seed=0x%x st_timeout=%u\n",
127 name, i915_selftest.random_seed, i915_selftest.timeout_ms);
129 /* Tests are listed in order in i915_*_selftests.h */
130 for (; count--; st++) {
135 if (signal_pending(current))
138 pr_info(DRIVER_NAME ": Running %s\n", st->name);
140 err = st->live(data);
143 if (err == -EINTR && !signal_pending(current))
149 if (WARN(err > 0 || err == -ENOTTY,
150 "%s returned %d, conflicting with selftest's magic values!\n",
157 #define run_selftests(x, data) \
158 __run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
160 int i915_mock_selftests(void)
164 if (!i915_selftest.mock)
167 err = run_selftests(mock, NULL);
169 i915_selftest.mock = err;
173 if (i915_selftest.mock < 0) {
174 i915_selftest.mock = -ENOTTY;
181 int i915_live_selftests(struct pci_dev *pdev)
185 if (!i915_selftest.live)
188 err = run_selftests(live, to_i915(pci_get_drvdata(pdev)));
190 i915_selftest.live = err;
194 if (i915_selftest.live < 0) {
195 i915_selftest.live = -ENOTTY;
202 static bool apply_subtest_filter(const char *caller, const char *name)
204 char *filter, *sep, *tok;
207 filter = kstrdup(i915_selftest.filter, GFP_KERNEL);
208 for (sep = filter; (tok = strsep(&sep, ","));) {
220 sl = strchr(tok, '/');
223 if (strcmp(tok, caller)) {
231 if (strcmp(tok, name)) {
245 int __i915_nop_setup(void *data)
250 int __i915_nop_teardown(int err, void *data)
255 int __i915_live_setup(void *data)
257 struct drm_i915_private *i915 = data;
259 return intel_gt_terminally_wedged(&i915->gt);
262 int __i915_live_teardown(int err, void *data)
264 struct drm_i915_private *i915 = data;
266 mutex_lock(&i915->drm.struct_mutex);
267 if (igt_flush_test(i915, I915_WAIT_LOCKED))
269 mutex_unlock(&i915->drm.struct_mutex);
271 i915_gem_drain_freed_objects(i915);
276 int __intel_gt_live_setup(void *data)
278 struct intel_gt *gt = data;
280 return intel_gt_terminally_wedged(gt);
283 int __intel_gt_live_teardown(int err, void *data)
285 struct intel_gt *gt = data;
287 mutex_lock(>->i915->drm.struct_mutex);
288 if (igt_flush_test(gt->i915, I915_WAIT_LOCKED))
290 mutex_unlock(>->i915->drm.struct_mutex);
292 i915_gem_drain_freed_objects(gt->i915);
297 int __i915_subtests(const char *caller,
298 int (*setup)(void *data),
299 int (*teardown)(int err, void *data),
300 const struct i915_subtest *st,
306 for (; count--; st++) {
308 if (signal_pending(current))
311 if (!apply_subtest_filter(caller, st->name))
316 pr_err(DRIVER_NAME "/%s: setup failed for %s\n",
321 pr_info(DRIVER_NAME ": Running %s/%s\n", caller, st->name);
322 GEM_TRACE("Running %s/%s\n", caller, st->name);
324 err = teardown(st->func(data), data);
325 if (err && err != -EINTR) {
326 pr_err(DRIVER_NAME "/%s: %s failed with error %d\n",
327 caller, st->name, err);
335 bool __igt_timeout(unsigned long timeout, const char *fmt, ...)
339 if (!signal_pending(current)) {
341 if (time_before(jiffies, timeout))
354 module_param_named(st_random_seed, i915_selftest.random_seed, uint, 0400);
355 module_param_named(st_timeout, i915_selftest.timeout_ms, uint, 0400);
356 module_param_named(st_filter, i915_selftest.filter, charp, 0400);
358 module_param_named_unsafe(mock_selftests, i915_selftest.mock, int, 0400);
359 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)");
361 module_param_named_unsafe(live_selftests, i915_selftest.live, int, 0400);
362 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)");