1 /*
2 * Copyright © 2016 Intel Corporation
3 *
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:
10 *
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.
14 *
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.
22 */
23
24 #include <linux/random.h>
25
26 #include "gt/intel_gt_pm.h"
27 #include "gt/uc/intel_gsc_fw.h"
28
29 #include "i915_driver.h"
30 #include "i915_drv.h"
31 #include "i915_selftest.h"
32
33 #include "igt_flush_test.h"
34
35 struct i915_selftest i915_selftest __read_mostly = {
36 .timeout_ms = 500,
37 };
38
i915_mock_sanitycheck(void)39 int i915_mock_sanitycheck(void)
40 {
41 pr_info(DRIVER_NAME ": %s() - ok!\n", __func__);
42 return 0;
43 }
44
i915_live_sanitycheck(struct drm_i915_private * i915)45 int i915_live_sanitycheck(struct drm_i915_private *i915)
46 {
47 pr_info("%s: %s() - ok!\n", i915->drm.driver->name, __func__);
48 return 0;
49 }
50
51 enum {
52 #define selftest(name, func) mock_##name,
53 #include "i915_mock_selftests.h"
54 #undef selftest
55 };
56
57 enum {
58 #define selftest(name, func) live_##name,
59 #include "i915_live_selftests.h"
60 #undef selftest
61 };
62
63 enum {
64 #define selftest(name, func) perf_##name,
65 #include "i915_perf_selftests.h"
66 #undef selftest
67 };
68
69 struct selftest {
70 bool enabled;
71 const char *name;
72 union {
73 int (*mock)(void);
74 int (*live)(struct drm_i915_private *);
75 };
76 };
77
78 #define selftest(n, f) [mock_##n] = { .name = #n, { .mock = f } },
79 static struct selftest mock_selftests[] = {
80 #include "i915_mock_selftests.h"
81 };
82 #undef selftest
83
84 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } },
85 static struct selftest live_selftests[] = {
86 #include "i915_live_selftests.h"
87 };
88 #undef selftest
89
90 #define selftest(n, f) [perf_##n] = { .name = #n, { .live = f } },
91 static struct selftest perf_selftests[] = {
92 #include "i915_perf_selftests.h"
93 };
94 #undef selftest
95
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"
102 #undef selftest_0
103 #undef param
104
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"
109 #undef selftest_0
110 #undef param
111
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"
116 #undef selftest_0
117 #undef param
118 #undef selftest
119
set_default_test_all(struct selftest * st,unsigned int count)120 static void set_default_test_all(struct selftest *st, unsigned int count)
121 {
122 unsigned int i;
123
124 for (i = 0; i < count; i++)
125 if (st[i].enabled)
126 return;
127
128 for (i = 0; i < count; i++)
129 st[i].enabled = true;
130 }
131
132 static bool
__gsc_proxy_init_progressing(struct intel_gsc_uc * gsc)133 __gsc_proxy_init_progressing(struct intel_gsc_uc *gsc)
134 {
135 return intel_gsc_uc_fw_proxy_get_status(gsc) == -EAGAIN;
136 }
137
138 static void
__wait_gsc_proxy_completed(struct drm_i915_private * i915)139 __wait_gsc_proxy_completed(struct drm_i915_private *i915)
140 {
141 bool need_to_wait = (IS_ENABLED(CONFIG_INTEL_MEI_GSC_PROXY) &&
142 i915->media_gt &&
143 HAS_ENGINE(i915->media_gt, GSC0) &&
144 intel_uc_fw_is_loadable(&i915->media_gt->uc.gsc.fw));
145 /*
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.
149 */
150 unsigned long timeout_ms = 8000;
151
152 if (need_to_wait && wait_for(!__gsc_proxy_init_progressing(&i915->media_gt->uc.gsc),
153 timeout_ms))
154 pr_warn(DRIVER_NAME "Timed out waiting for gsc_proxy_completion!\n");
155 }
156
157 static void
__wait_gsc_huc_load_completed(struct drm_i915_private * i915)158 __wait_gsc_huc_load_completed(struct drm_i915_private *i915)
159 {
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));
164 /*
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.
173 */
174 unsigned long timeout_ms = 15000;
175
176 if (need_to_wait &&
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");
179 }
180
__run_selftests(const char * name,struct selftest * st,unsigned int count,void * data)181 static int __run_selftests(const char *name,
182 struct selftest *st,
183 unsigned int count,
184 void *data)
185 {
186 int err = 0;
187
188 while (!i915_selftest.random_seed)
189 i915_selftest.random_seed = get_random_u32();
190
191 i915_selftest.timeout_jiffies =
192 i915_selftest.timeout_ms ?
193 msecs_to_jiffies_timeout(i915_selftest.timeout_ms) :
194 MAX_SCHEDULE_TIMEOUT;
195
196 set_default_test_all(st, count);
197
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);
200
201 /* Tests are listed in order in i915_*_selftests.h */
202 for (; count--; st++) {
203 if (!st->enabled)
204 continue;
205
206 cond_resched();
207 if (signal_pending(current))
208 return -EINTR;
209
210 pr_info(DRIVER_NAME ": Running %s\n", st->name);
211 if (data)
212 err = st->live(data);
213 else
214 err = st->mock();
215 if (err == -EINTR && !signal_pending(current))
216 err = 0;
217 if (err)
218 break;
219 }
220
221 if (WARN(err > 0 || err == -ENOTTY,
222 "%s returned %d, conflicting with selftest's magic values!\n",
223 st->name, err))
224 err = -1;
225
226 return err;
227 }
228
229 #define run_selftests(x, data) \
230 __run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
231
i915_mock_selftests(void)232 int i915_mock_selftests(void)
233 {
234 int err;
235
236 if (!i915_selftest.mock)
237 return 0;
238
239 err = run_selftests(mock, NULL);
240 if (err) {
241 i915_selftest.mock = err;
242 return 1;
243 }
244
245 if (i915_selftest.mock < 0) {
246 i915_selftest.mock = -ENOTTY;
247 return 1;
248 }
249
250 return 0;
251 }
252
i915_live_selftests(struct pci_dev * pdev)253 int i915_live_selftests(struct pci_dev *pdev)
254 {
255 struct drm_i915_private *i915 = pdev_to_i915(pdev);
256 int err;
257
258 if (!i915_selftest.live)
259 return 0;
260
261 __wait_gsc_proxy_completed(i915);
262 __wait_gsc_huc_load_completed(i915);
263
264 err = run_selftests(live, i915);
265 if (err) {
266 i915_selftest.live = err;
267 return err;
268 }
269
270 if (i915_selftest.live < 0) {
271 i915_selftest.live = -ENOTTY;
272 return 1;
273 }
274
275 return 0;
276 }
277
i915_perf_selftests(struct pci_dev * pdev)278 int i915_perf_selftests(struct pci_dev *pdev)
279 {
280 struct drm_i915_private *i915 = pdev_to_i915(pdev);
281 int err;
282
283 if (!i915_selftest.perf)
284 return 0;
285
286 __wait_gsc_proxy_completed(i915);
287 __wait_gsc_huc_load_completed(i915);
288
289 err = run_selftests(perf, i915);
290 if (err) {
291 i915_selftest.perf = err;
292 return err;
293 }
294
295 if (i915_selftest.perf < 0) {
296 i915_selftest.perf = -ENOTTY;
297 return 1;
298 }
299
300 return 0;
301 }
302
apply_subtest_filter(const char * caller,const char * name)303 static bool apply_subtest_filter(const char *caller, const char *name)
304 {
305 char *filter, *sep, *tok;
306 bool result = true;
307
308 filter = kstrdup(i915_selftest.filter, GFP_KERNEL);
309 for (sep = filter; (tok = strsep(&sep, ","));) {
310 bool allow = true;
311 char *sl;
312
313 if (*tok == '!') {
314 allow = false;
315 tok++;
316 }
317
318 if (*tok == '\0')
319 continue;
320
321 sl = strchr(tok, '/');
322 if (sl) {
323 *sl++ = '\0';
324 if (strcmp(tok, caller)) {
325 if (allow)
326 result = false;
327 continue;
328 }
329 tok = sl;
330 }
331
332 if (strcmp(tok, name)) {
333 if (allow)
334 result = false;
335 continue;
336 }
337
338 result = allow;
339 break;
340 }
341 kfree(filter);
342
343 return result;
344 }
345
__i915_nop_setup(void * data)346 int __i915_nop_setup(void *data)
347 {
348 return 0;
349 }
350
__i915_nop_teardown(int err,void * data)351 int __i915_nop_teardown(int err, void *data)
352 {
353 return err;
354 }
355
__i915_live_setup(void * data)356 int __i915_live_setup(void *data)
357 {
358 struct drm_i915_private *i915 = data;
359
360 /* The selftests expect an idle system */
361 if (intel_gt_pm_wait_for_idle(to_gt(i915)))
362 return -EIO;
363
364 return intel_gt_terminally_wedged(to_gt(i915));
365 }
366
__i915_live_teardown(int err,void * data)367 int __i915_live_teardown(int err, void *data)
368 {
369 struct drm_i915_private *i915 = data;
370
371 if (igt_flush_test(i915))
372 err = -EIO;
373
374 i915_gem_drain_freed_objects(i915);
375
376 return err;
377 }
378
__intel_gt_live_setup(void * data)379 int __intel_gt_live_setup(void *data)
380 {
381 struct intel_gt *gt = data;
382
383 /* The selftests expect an idle system */
384 if (intel_gt_pm_wait_for_idle(gt))
385 return -EIO;
386
387 return intel_gt_terminally_wedged(gt);
388 }
389
__intel_gt_live_teardown(int err,void * data)390 int __intel_gt_live_teardown(int err, void *data)
391 {
392 struct intel_gt *gt = data;
393
394 if (igt_flush_test(gt->i915))
395 err = -EIO;
396
397 i915_gem_drain_freed_objects(gt->i915);
398
399 return err;
400 }
401
__i915_subtests(const char * caller,int (* setup)(void * data),int (* teardown)(int err,void * data),const struct i915_subtest * st,unsigned int count,void * data)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,
406 unsigned int count,
407 void *data)
408 {
409 int err;
410
411 for (; count--; st++) {
412 cond_resched();
413 if (signal_pending(current))
414 return -EINTR;
415
416 if (!apply_subtest_filter(caller, st->name))
417 continue;
418
419 err = setup(data);
420 if (err) {
421 pr_err(DRIVER_NAME "/%s: setup failed for %s\n",
422 caller, st->name);
423 return err;
424 }
425
426 pr_info(DRIVER_NAME ": Running %s/%s\n", caller, st->name);
427 GEM_TRACE("Running %s/%s\n", caller, st->name);
428
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);
433 return err;
434 }
435 }
436
437 return 0;
438 }
439
__igt_timeout(unsigned long timeout,const char * fmt,...)440 bool __igt_timeout(unsigned long timeout, const char *fmt, ...)
441 {
442 va_list va;
443
444 if (!signal_pending(current)) {
445 cond_resched();
446 if (time_before(jiffies, timeout))
447 return false;
448 }
449
450 if (fmt) {
451 va_start(va, fmt);
452 vprintk(fmt, va);
453 va_end(va);
454 }
455
456 return true;
457 }
458
igt_hexdump(const void * buf,size_t len)459 void igt_hexdump(const void *buf, size_t len)
460 {
461 const size_t rowsize = 8 * sizeof(u32);
462 const void *prev = NULL;
463 bool skip = false;
464 size_t pos;
465
466 for (pos = 0; pos < len; pos += rowsize) {
467 char line[128];
468
469 if (prev && !memcmp(prev, buf + pos, rowsize)) {
470 if (!skip) {
471 pr_info("*\n");
472 skip = true;
473 }
474 continue;
475 }
476
477 WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
478 rowsize, sizeof(u32),
479 line, sizeof(line),
480 false) >= sizeof(line));
481 pr_info("[%04zx] %s\n", pos, line);
482
483 prev = buf + pos;
484 skip = false;
485 }
486 }
487
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);
491
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)");
494
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)");
497
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)");
500