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 "gt/uc/intel_gsc_fw.h"
29 #include "i915_driver.h"
31 #include "i915_selftest.h"
33 #include "igt_flush_test.h"
35 struct i915_selftest i915_selftest __read_mostly = {
39 int i915_mock_sanitycheck(void)
41 pr_info(DRIVER_NAME ": %s() - ok!\n", __func__);
45 int i915_live_sanitycheck(struct drm_i915_private *i915)
47 pr_info("%s: %s() - ok!\n", i915->drm.driver->name, __func__);
52 #define selftest(name, func) mock_##name,
53 #include "i915_mock_selftests.h"
58 #define selftest(name, func) live_##name,
59 #include "i915_live_selftests.h"
64 #define selftest(name, func) perf_##name,
65 #include "i915_perf_selftests.h"
74 int (*live)(struct drm_i915_private *);
78 #define selftest(n, f) [mock_##n] = { .name = #n, { .mock = f } },
79 static struct selftest mock_selftests[] = {
80 #include "i915_mock_selftests.h"
84 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } },
85 static struct selftest live_selftests[] = {
86 #include "i915_live_selftests.h"
90 #define selftest(n, f) [perf_##n] = { .name = #n, { .live = f } },
91 static struct selftest perf_selftests[] = {
92 #include "i915_perf_selftests.h"
96 /* Embed the line number into the parameter name so that we can order tests */
97 #define selftest(n, func) selftest_0(n, func, param(n))
98 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __mock_##n))
99 #define selftest_0(n, func, id) \
100 module_param_named(id, mock_selftests[mock_##n].enabled, bool, 0400);
101 #include "i915_mock_selftests.h"
105 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __live_##n))
106 #define selftest_0(n, func, id) \
107 module_param_named(id, live_selftests[live_##n].enabled, bool, 0400);
108 #include "i915_live_selftests.h"
112 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __perf_##n))
113 #define selftest_0(n, func, id) \
114 module_param_named(id, perf_selftests[perf_##n].enabled, bool, 0400);
115 #include "i915_perf_selftests.h"
120 static void set_default_test_all(struct selftest *st, unsigned int count)
124 for (i = 0; i < count; i++)
128 for (i = 0; i < count; i++)
129 st[i].enabled = true;
133 __gsc_proxy_init_progressing(struct intel_gsc_uc *gsc)
135 return intel_gsc_uc_fw_proxy_get_status(gsc) == -EAGAIN;
139 __wait_gsc_proxy_completed(struct drm_i915_private *i915)
141 bool need_to_wait = (IS_ENABLED(CONFIG_INTEL_MEI_GSC_PROXY) &&
143 HAS_ENGINE(i915->media_gt, GSC0) &&
144 intel_uc_fw_is_loadable(&i915->media_gt->uc.gsc.fw));
146 * The gsc proxy component depends on the kernel component driver load ordering
147 * and in corner cases (the first time after an IFWI flash), init-completion
148 * firmware flows take longer.
150 unsigned long timeout_ms = 8000;
152 if (need_to_wait && wait_for(!__gsc_proxy_init_progressing(&i915->media_gt->uc.gsc),
154 pr_warn(DRIVER_NAME "Timed out waiting for gsc_proxy_completion!\n");
158 __wait_gsc_huc_load_completed(struct drm_i915_private *i915)
160 /* this only applies to DG2, so we only care about GT0 */
161 struct intel_huc *huc = &to_gt(i915)->uc.huc;
162 bool need_to_wait = (IS_ENABLED(CONFIG_INTEL_MEI_PXP) &&
163 intel_huc_wait_required(huc));
165 * The GSC and PXP mei bringup depends on the kernel boot ordering, so
166 * to account for the worst case scenario the HuC code waits for up to
167 * 10s for the GSC driver to load and then another 5s for the PXP
168 * component to bind before giving up, even though those steps normally
169 * complete in less than a second from the i915 load. We match that
170 * timeout here, but we expect to bail early due to the fence being
171 * signalled even in a failure case, as it is extremely unlikely that
172 * both components will use their full timeout.
174 unsigned long timeout_ms = 15000;
177 wait_for(i915_sw_fence_done(&huc->delayed_load.fence), timeout_ms))
178 pr_warn(DRIVER_NAME "Timed out waiting for huc load via GSC!\n");
181 static int __run_selftests(const char *name,
188 while (!i915_selftest.random_seed)
189 i915_selftest.random_seed = get_random_u32();
191 i915_selftest.timeout_jiffies =
192 i915_selftest.timeout_ms ?
193 msecs_to_jiffies_timeout(i915_selftest.timeout_ms) :
194 MAX_SCHEDULE_TIMEOUT;
196 set_default_test_all(st, count);
198 pr_info(DRIVER_NAME ": Performing %s selftests with st_random_seed=0x%x st_timeout=%u\n",
199 name, i915_selftest.random_seed, i915_selftest.timeout_ms);
201 /* Tests are listed in order in i915_*_selftests.h */
202 for (; count--; st++) {
207 if (signal_pending(current))
210 pr_info(DRIVER_NAME ": Running %s\n", st->name);
212 err = st->live(data);
215 if (err == -EINTR && !signal_pending(current))
221 if (WARN(err > 0 || err == -ENOTTY,
222 "%s returned %d, conflicting with selftest's magic values!\n",
229 #define run_selftests(x, data) \
230 __run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
232 int i915_mock_selftests(void)
236 if (!i915_selftest.mock)
239 err = run_selftests(mock, NULL);
241 i915_selftest.mock = err;
245 if (i915_selftest.mock < 0) {
246 i915_selftest.mock = -ENOTTY;
253 int i915_live_selftests(struct pci_dev *pdev)
255 struct drm_i915_private *i915 = pdev_to_i915(pdev);
258 if (!i915_selftest.live)
261 __wait_gsc_proxy_completed(i915);
262 __wait_gsc_huc_load_completed(i915);
264 err = run_selftests(live, i915);
266 i915_selftest.live = err;
270 if (i915_selftest.live < 0) {
271 i915_selftest.live = -ENOTTY;
278 int i915_perf_selftests(struct pci_dev *pdev)
280 struct drm_i915_private *i915 = pdev_to_i915(pdev);
283 if (!i915_selftest.perf)
286 __wait_gsc_proxy_completed(i915);
287 __wait_gsc_huc_load_completed(i915);
289 err = run_selftests(perf, i915);
291 i915_selftest.perf = err;
295 if (i915_selftest.perf < 0) {
296 i915_selftest.perf = -ENOTTY;
303 static bool apply_subtest_filter(const char *caller, const char *name)
305 char *filter, *sep, *tok;
308 filter = kstrdup(i915_selftest.filter, GFP_KERNEL);
309 for (sep = filter; (tok = strsep(&sep, ","));) {
321 sl = strchr(tok, '/');
324 if (strcmp(tok, caller)) {
332 if (strcmp(tok, name)) {
346 int __i915_nop_setup(void *data)
351 int __i915_nop_teardown(int err, void *data)
356 int __i915_live_setup(void *data)
358 struct drm_i915_private *i915 = data;
360 /* The selftests expect an idle system */
361 if (intel_gt_pm_wait_for_idle(to_gt(i915)))
364 return intel_gt_terminally_wedged(to_gt(i915));
367 int __i915_live_teardown(int err, void *data)
369 struct drm_i915_private *i915 = data;
371 if (igt_flush_test(i915))
374 i915_gem_drain_freed_objects(i915);
379 int __intel_gt_live_setup(void *data)
381 struct intel_gt *gt = data;
383 /* The selftests expect an idle system */
384 if (intel_gt_pm_wait_for_idle(gt))
387 return intel_gt_terminally_wedged(gt);
390 int __intel_gt_live_teardown(int err, void *data)
392 struct intel_gt *gt = data;
394 if (igt_flush_test(gt->i915))
397 i915_gem_drain_freed_objects(gt->i915);
402 int __i915_subtests(const char *caller,
403 int (*setup)(void *data),
404 int (*teardown)(int err, void *data),
405 const struct i915_subtest *st,
411 for (; count--; st++) {
413 if (signal_pending(current))
416 if (!apply_subtest_filter(caller, st->name))
421 pr_err(DRIVER_NAME "/%s: setup failed for %s\n",
426 pr_info(DRIVER_NAME ": Running %s/%s\n", caller, st->name);
427 GEM_TRACE("Running %s/%s\n", caller, st->name);
429 err = teardown(st->func(data), data);
430 if (err && err != -EINTR) {
431 pr_err(DRIVER_NAME "/%s: %s failed with error %d\n",
432 caller, st->name, err);
440 bool __igt_timeout(unsigned long timeout, const char *fmt, ...)
444 if (!signal_pending(current)) {
446 if (time_before(jiffies, timeout))
459 void igt_hexdump(const void *buf, size_t len)
461 const size_t rowsize = 8 * sizeof(u32);
462 const void *prev = NULL;
466 for (pos = 0; pos < len; pos += rowsize) {
469 if (prev && !memcmp(prev, buf + pos, rowsize)) {
477 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
478 rowsize, sizeof(u32),
480 false) >= sizeof(line));
481 pr_info("[%04zx] %s\n", pos, line);
488 module_param_named(st_random_seed, i915_selftest.random_seed, uint, 0400);
489 module_param_named(st_timeout, i915_selftest.timeout_ms, uint, 0400);
490 module_param_named(st_filter, i915_selftest.filter, charp, 0400);
492 module_param_named_unsafe(mock_selftests, i915_selftest.mock, int, 0400);
493 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)");
495 module_param_named_unsafe(live_selftests, i915_selftest.live, int, 0400);
496 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)");
498 module_param_named_unsafe(perf_selftests, i915_selftest.perf, int, 0400);
499 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)");