Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / selftests / i915_selftest.c
blob1bc11c09faef53182e66d3ab2900dc7317e2e137
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 "gt/intel_gt_pm.h"
27 #include "i915_drv.h"
28 #include "i915_selftest.h"
30 #include "igt_flush_test.h"
32 struct i915_selftest i915_selftest __read_mostly = {
33 .timeout_ms = 500,
36 int i915_mock_sanitycheck(void)
38 pr_info(DRIVER_NAME ": %s() - ok!\n", __func__);
39 return 0;
42 int i915_live_sanitycheck(struct drm_i915_private *i915)
44 pr_info("%s: %s() - ok!\n", i915->drm.driver->name, __func__);
45 return 0;
48 enum {
49 #define selftest(name, func) mock_##name,
50 #include "i915_mock_selftests.h"
51 #undef selftest
54 enum {
55 #define selftest(name, func) live_##name,
56 #include "i915_live_selftests.h"
57 #undef selftest
60 enum {
61 #define selftest(name, func) perf_##name,
62 #include "i915_perf_selftests.h"
63 #undef selftest
66 struct selftest {
67 bool enabled;
68 const char *name;
69 union {
70 int (*mock)(void);
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"
79 #undef selftest
81 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } },
82 static struct selftest live_selftests[] = {
83 #include "i915_live_selftests.h"
85 #undef selftest
87 #define selftest(n, f) [perf_##n] = { .name = #n, { .live = f } },
88 static struct selftest perf_selftests[] = {
89 #include "i915_perf_selftests.h"
91 #undef selftest
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"
99 #undef selftest_0
100 #undef param
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"
106 #undef selftest_0
107 #undef param
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"
113 #undef selftest_0
114 #undef param
115 #undef selftest
117 static void set_default_test_all(struct selftest *st, unsigned int count)
119 unsigned int i;
121 for (i = 0; i < count; i++)
122 if (st[i].enabled)
123 return;
125 for (i = 0; i < count; i++)
126 st[i].enabled = true;
129 static int __run_selftests(const char *name,
130 struct selftest *st,
131 unsigned int count,
132 void *data)
134 int err = 0;
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++) {
151 if (!st->enabled)
152 continue;
154 cond_resched();
155 if (signal_pending(current))
156 return -EINTR;
158 pr_info(DRIVER_NAME ": Running %s\n", st->name);
159 if (data)
160 err = st->live(data);
161 else
162 err = st->mock();
163 if (err == -EINTR && !signal_pending(current))
164 err = 0;
165 if (err)
166 break;
169 if (WARN(err > 0 || err == -ENOTTY,
170 "%s returned %d, conflicting with selftest's magic values!\n",
171 st->name, err))
172 err = -1;
174 return err;
177 #define run_selftests(x, data) \
178 __run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
180 int i915_mock_selftests(void)
182 int err;
184 if (!i915_selftest.mock)
185 return 0;
187 err = run_selftests(mock, NULL);
188 if (err) {
189 i915_selftest.mock = err;
190 return err;
193 if (i915_selftest.mock < 0) {
194 i915_selftest.mock = -ENOTTY;
195 return 1;
198 return 0;
201 int i915_live_selftests(struct pci_dev *pdev)
203 int err;
205 if (!i915_selftest.live)
206 return 0;
208 err = run_selftests(live, pdev_to_i915(pdev));
209 if (err) {
210 i915_selftest.live = err;
211 return err;
214 if (i915_selftest.live < 0) {
215 i915_selftest.live = -ENOTTY;
216 return 1;
219 return 0;
222 int i915_perf_selftests(struct pci_dev *pdev)
224 int err;
226 if (!i915_selftest.perf)
227 return 0;
229 err = run_selftests(perf, pdev_to_i915(pdev));
230 if (err) {
231 i915_selftest.perf = err;
232 return err;
235 if (i915_selftest.perf < 0) {
236 i915_selftest.perf = -ENOTTY;
237 return 1;
240 return 0;
243 static bool apply_subtest_filter(const char *caller, const char *name)
245 char *filter, *sep, *tok;
246 bool result = true;
248 filter = kstrdup(i915_selftest.filter, GFP_KERNEL);
249 for (sep = filter; (tok = strsep(&sep, ","));) {
250 bool allow = true;
251 char *sl;
253 if (*tok == '!') {
254 allow = false;
255 tok++;
258 if (*tok == '\0')
259 continue;
261 sl = strchr(tok, '/');
262 if (sl) {
263 *sl++ = '\0';
264 if (strcmp(tok, caller)) {
265 if (allow)
266 result = false;
267 continue;
269 tok = sl;
272 if (strcmp(tok, name)) {
273 if (allow)
274 result = false;
275 continue;
278 result = allow;
279 break;
281 kfree(filter);
283 return result;
286 int __i915_nop_setup(void *data)
288 return 0;
291 int __i915_nop_teardown(int err, void *data)
293 return err;
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))
302 return -EIO;
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))
312 err = -EIO;
314 i915_gem_drain_freed_objects(i915);
316 return err;
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))
325 return -EIO;
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))
335 err = -EIO;
337 i915_gem_drain_freed_objects(gt->i915);
339 return err;
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,
346 unsigned int count,
347 void *data)
349 int err;
351 for (; count--; st++) {
352 cond_resched();
353 if (signal_pending(current))
354 return -EINTR;
356 if (!apply_subtest_filter(caller, st->name))
357 continue;
359 err = setup(data);
360 if (err) {
361 pr_err(DRIVER_NAME "/%s: setup failed for %s\n",
362 caller, st->name);
363 return err;
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);
373 return err;
377 return 0;
380 bool __igt_timeout(unsigned long timeout, const char *fmt, ...)
382 va_list va;
384 if (!signal_pending(current)) {
385 cond_resched();
386 if (time_before(jiffies, timeout))
387 return false;
390 if (fmt) {
391 va_start(va, fmt);
392 vprintk(fmt, va);
393 va_end(va);
396 return true;
399 void igt_hexdump(const void *buf, size_t len)
401 const size_t rowsize = 8 * sizeof(u32);
402 const void *prev = NULL;
403 bool skip = false;
404 size_t pos;
406 for (pos = 0; pos < len; pos += rowsize) {
407 char line[128];
409 if (prev && !memcmp(prev, buf + pos, rowsize)) {
410 if (!skip) {
411 pr_info("*\n");
412 skip = true;
414 continue;
417 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
418 rowsize, sizeof(u32),
419 line, sizeof(line),
420 false) >= sizeof(line));
421 pr_info("[%04zx] %s\n", pos, line);
423 prev = buf + pos;
424 skip = false;
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)");