xref: /linux/drivers/gpu/drm/i915/selftests/i915_selftest.c (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
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.h"
27 #include "gt/intel_gt_pm.h"
28 #include "gt/intel_gt_regs.h"
29 #include "gt/uc/intel_gsc_fw.h"
30 
31 #include "i915_driver.h"
32 #include "i915_drv.h"
33 #include "i915_jiffies.h"
34 #include "i915_selftest.h"
35 #include "i915_wait_util.h"
36 #include "igt_flush_test.h"
37 
38 struct i915_selftest i915_selftest __read_mostly = {
39 	.timeout_ms = 500,
40 };
41 
42 int i915_mock_sanitycheck(void)
43 {
44 	pr_info(DRIVER_NAME ": %s() - ok!\n", __func__);
45 	return 0;
46 }
47 
48 int i915_live_sanitycheck(struct drm_i915_private *i915)
49 {
50 	pr_info("%s: %s() - ok!\n", i915->drm.driver->name, __func__);
51 	return 0;
52 }
53 
54 enum {
55 #define selftest(name, func) mock_##name,
56 #include "i915_mock_selftests.h"
57 #undef selftest
58 };
59 
60 enum {
61 #define selftest(name, func) live_##name,
62 #include "i915_live_selftests.h"
63 #undef selftest
64 };
65 
66 enum {
67 #define selftest(name, func) perf_##name,
68 #include "i915_perf_selftests.h"
69 #undef selftest
70 };
71 
72 struct selftest {
73 	bool enabled;
74 	const char *name;
75 	union {
76 		int (*mock)(void);
77 		int (*live)(struct drm_i915_private *);
78 	};
79 };
80 
81 #define selftest(n, f) [mock_##n] = { .name = #n, { .mock = f } },
82 static struct selftest mock_selftests[] = {
83 #include "i915_mock_selftests.h"
84 };
85 #undef selftest
86 
87 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } },
88 static struct selftest live_selftests[] = {
89 #include "i915_live_selftests.h"
90 };
91 #undef selftest
92 
93 #define selftest(n, f) [perf_##n] = { .name = #n, { .live = f } },
94 static struct selftest perf_selftests[] = {
95 #include "i915_perf_selftests.h"
96 };
97 #undef selftest
98 
99 /* Embed the line number into the parameter name so that we can order tests */
100 #define selftest(n, func) selftest_0(n, func, param(n))
101 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __mock_##n))
102 #define selftest_0(n, func, id) \
103 module_param_named(id, mock_selftests[mock_##n].enabled, bool, 0400);
104 #include "i915_mock_selftests.h"
105 #undef selftest_0
106 #undef param
107 
108 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __live_##n))
109 #define selftest_0(n, func, id) \
110 module_param_named(id, live_selftests[live_##n].enabled, bool, 0400);
111 #include "i915_live_selftests.h"
112 #undef selftest_0
113 #undef param
114 
115 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __perf_##n))
116 #define selftest_0(n, func, id) \
117 module_param_named(id, perf_selftests[perf_##n].enabled, bool, 0400);
118 #include "i915_perf_selftests.h"
119 #undef selftest_0
120 #undef param
121 #undef selftest
122 
123 static void set_default_test_all(struct selftest *st, unsigned int count)
124 {
125 	unsigned int i;
126 
127 	for (i = 0; i < count; i++)
128 		if (st[i].enabled)
129 			return;
130 
131 	for (i = 0; i < count; i++)
132 		st[i].enabled = true;
133 }
134 
135 static bool
136 __gsc_proxy_init_progressing(struct intel_gsc_uc *gsc)
137 {
138 	return intel_gsc_uc_fw_proxy_get_status(gsc) == -EAGAIN;
139 }
140 
141 static void
142 __wait_gsc_proxy_completed(struct drm_i915_private *i915)
143 {
144 	bool need_to_wait = (IS_ENABLED(CONFIG_INTEL_MEI_GSC_PROXY) &&
145 			     i915->media_gt &&
146 			     HAS_ENGINE(i915->media_gt, GSC0) &&
147 			     intel_uc_fw_is_loadable(&i915->media_gt->uc.gsc.fw));
148 	/*
149 	 * The gsc proxy component depends on the kernel component driver load ordering
150 	 * and in corner cases (the first time after an IFWI flash), init-completion
151 	 * firmware flows take longer.
152 	 */
153 	unsigned long timeout_ms = 8000;
154 
155 	if (need_to_wait && wait_for(!__gsc_proxy_init_progressing(&i915->media_gt->uc.gsc),
156 				     timeout_ms))
157 		pr_warn(DRIVER_NAME "Timed out waiting for gsc_proxy_completion!\n");
158 }
159 
160 static void
161 __wait_gsc_huc_load_completed(struct drm_i915_private *i915)
162 {
163 	/* this only applies to DG2, so we only care about GT0 */
164 	struct intel_huc *huc = &to_gt(i915)->uc.huc;
165 	bool need_to_wait = (IS_ENABLED(CONFIG_INTEL_MEI_PXP) &&
166 			     intel_huc_wait_required(huc));
167 	/*
168 	 * The GSC and PXP mei bringup depends on the kernel boot ordering, so
169 	 * to account for the worst case scenario the HuC code waits for up to
170 	 * 10s for the GSC driver to load and then another 5s for the PXP
171 	 * component to bind before giving up, even though those steps normally
172 	 * complete in less than a second from the i915 load. We match that
173 	 * timeout here, but we expect to bail early due to the fence being
174 	 * signalled even in a failure case, as it is extremely unlikely that
175 	 * both components will use their full timeout.
176 	 */
177 	unsigned long timeout_ms = 15000;
178 
179 	if (need_to_wait &&
180 	    wait_for(i915_sw_fence_done(&huc->delayed_load.fence), timeout_ms))
181 		pr_warn(DRIVER_NAME "Timed out waiting for huc load via GSC!\n");
182 }
183 
184 static int __run_selftests(const char *name,
185 			   struct selftest *st,
186 			   unsigned int count,
187 			   void *data)
188 {
189 	int err = 0;
190 
191 	while (!i915_selftest.random_seed)
192 		i915_selftest.random_seed = get_random_u32();
193 
194 	i915_selftest.timeout_jiffies =
195 		i915_selftest.timeout_ms ?
196 		msecs_to_jiffies_timeout(i915_selftest.timeout_ms) :
197 		MAX_SCHEDULE_TIMEOUT;
198 
199 	set_default_test_all(st, count);
200 
201 	pr_info(DRIVER_NAME ": Performing %s selftests with st_random_seed=0x%x st_timeout=%u\n",
202 		name, i915_selftest.random_seed, i915_selftest.timeout_ms);
203 
204 	/* Tests are listed in order in i915_*_selftests.h */
205 	for (; count--; st++) {
206 		if (!st->enabled)
207 			continue;
208 
209 		cond_resched();
210 		if (signal_pending(current))
211 			return -EINTR;
212 
213 		pr_info(DRIVER_NAME ": Running %s\n", st->name);
214 		if (data)
215 			err = st->live(data);
216 		else
217 			err = st->mock();
218 		if (err == -EINTR && !signal_pending(current))
219 			err = 0;
220 		if (err)
221 			break;
222 	}
223 
224 	if (WARN(err > 0 || err == -ENOTTY,
225 		 "%s returned %d, conflicting with selftest's magic values!\n",
226 		 st->name, err))
227 		err = -1;
228 
229 	return err;
230 }
231 
232 #define run_selftests(x, data) \
233 	__run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
234 
235 int i915_mock_selftests(void)
236 {
237 	int err;
238 
239 	if (!i915_selftest.mock)
240 		return 0;
241 
242 	err = run_selftests(mock, NULL);
243 	if (err) {
244 		i915_selftest.mock = err;
245 		return 1;
246 	}
247 
248 	if (i915_selftest.mock < 0) {
249 		i915_selftest.mock = -ENOTTY;
250 		return 1;
251 	}
252 
253 	return 0;
254 }
255 
256 int i915_live_selftests(struct pci_dev *pdev)
257 {
258 	struct drm_i915_private *i915 = pdev_to_i915(pdev);
259 	struct intel_uncore *uncore = &i915->uncore;
260 	int err;
261 	u32 pg_enable;
262 	intel_wakeref_t wakeref;
263 
264 	if (!i915_selftest.live)
265 		return 0;
266 
267 	/*
268 	 * FIXME Disable render powergating, this is temporary wa and should be removed
269 	 * after fixing real cause of forcewake timeouts.
270 	 */
271 	with_intel_runtime_pm(uncore->rpm, wakeref) {
272 		if (IS_GFX_GT_IP_RANGE(to_gt(i915), IP_VER(12, 00), IP_VER(12, 74))) {
273 			pg_enable = intel_uncore_read(uncore, GEN9_PG_ENABLE);
274 			if (pg_enable & GEN9_RENDER_PG_ENABLE)
275 				intel_uncore_write_fw(uncore, GEN9_PG_ENABLE,
276 						      pg_enable & ~GEN9_RENDER_PG_ENABLE);
277 		}
278 	}
279 
280 	__wait_gsc_proxy_completed(i915);
281 	__wait_gsc_huc_load_completed(i915);
282 
283 	err = run_selftests(live, i915);
284 	if (err) {
285 		i915_selftest.live = err;
286 		return err;
287 	}
288 
289 	if (i915_selftest.live < 0) {
290 		i915_selftest.live = -ENOTTY;
291 		return 1;
292 	}
293 
294 	return 0;
295 }
296 
297 int i915_perf_selftests(struct pci_dev *pdev)
298 {
299 	struct drm_i915_private *i915 = pdev_to_i915(pdev);
300 	int err;
301 
302 	if (!i915_selftest.perf)
303 		return 0;
304 
305 	__wait_gsc_proxy_completed(i915);
306 	__wait_gsc_huc_load_completed(i915);
307 
308 	err = run_selftests(perf, i915);
309 	if (err) {
310 		i915_selftest.perf = err;
311 		return err;
312 	}
313 
314 	if (i915_selftest.perf < 0) {
315 		i915_selftest.perf = -ENOTTY;
316 		return 1;
317 	}
318 
319 	return 0;
320 }
321 
322 static bool apply_subtest_filter(const char *caller, const char *name)
323 {
324 	char *filter, *sep, *tok;
325 	bool result = true;
326 
327 	filter = kstrdup(i915_selftest.filter, GFP_KERNEL);
328 	for (sep = filter; (tok = strsep(&sep, ","));) {
329 		bool allow = true;
330 		char *sl;
331 
332 		if (*tok == '!') {
333 			allow = false;
334 			tok++;
335 		}
336 
337 		if (*tok == '\0')
338 			continue;
339 
340 		sl = strchr(tok, '/');
341 		if (sl) {
342 			*sl++ = '\0';
343 			if (strcmp(tok, caller)) {
344 				if (allow)
345 					result = false;
346 				continue;
347 			}
348 			tok = sl;
349 		}
350 
351 		if (strcmp(tok, name)) {
352 			if (allow)
353 				result = false;
354 			continue;
355 		}
356 
357 		result = allow;
358 		break;
359 	}
360 	kfree(filter);
361 
362 	return result;
363 }
364 
365 int __i915_nop_setup(void *data)
366 {
367 	return 0;
368 }
369 
370 int __i915_nop_teardown(int err, void *data)
371 {
372 	return err;
373 }
374 
375 int __i915_live_setup(void *data)
376 {
377 	struct drm_i915_private *i915 = data;
378 
379 	/* The selftests expect an idle system */
380 	if (intel_gt_pm_wait_for_idle(to_gt(i915)))
381 		return -EIO;
382 
383 	return intel_gt_terminally_wedged(to_gt(i915));
384 }
385 
386 int __i915_live_teardown(int err, void *data)
387 {
388 	struct drm_i915_private *i915 = data;
389 
390 	if (igt_flush_test(i915))
391 		err = -EIO;
392 
393 	i915_gem_drain_freed_objects(i915);
394 
395 	return err;
396 }
397 
398 int __intel_gt_live_setup(void *data)
399 {
400 	struct intel_gt *gt = data;
401 
402 	/* The selftests expect an idle system */
403 	if (intel_gt_pm_wait_for_idle(gt))
404 		return -EIO;
405 
406 	return intel_gt_terminally_wedged(gt);
407 }
408 
409 int __intel_gt_live_teardown(int err, void *data)
410 {
411 	struct intel_gt *gt = data;
412 
413 	if (igt_flush_test(gt->i915))
414 		err = -EIO;
415 
416 	i915_gem_drain_freed_objects(gt->i915);
417 
418 	return err;
419 }
420 
421 int __i915_subtests(const char *caller,
422 		    int (*setup)(void *data),
423 		    int (*teardown)(int err, void *data),
424 		    const struct i915_subtest *st,
425 		    unsigned int count,
426 		    void *data)
427 {
428 	int err;
429 
430 	for (; count--; st++) {
431 		cond_resched();
432 		if (signal_pending(current))
433 			return -EINTR;
434 
435 		if (!apply_subtest_filter(caller, st->name))
436 			continue;
437 
438 		err = setup(data);
439 		if (err) {
440 			pr_err(DRIVER_NAME "/%s: setup failed for %s\n",
441 			       caller, st->name);
442 			return err;
443 		}
444 
445 		pr_info(DRIVER_NAME ": Running %s/%s\n", caller, st->name);
446 		GEM_TRACE("Running %s/%s\n", caller, st->name);
447 
448 		err = teardown(st->func(data), data);
449 		if (err && err != -EINTR) {
450 			pr_err(DRIVER_NAME "/%s: %s failed with error %d\n",
451 			       caller, st->name, err);
452 			return err;
453 		}
454 	}
455 
456 	return 0;
457 }
458 
459 bool __igt_timeout(unsigned long timeout, const char *fmt, ...)
460 {
461 	va_list va;
462 
463 	if (!signal_pending(current)) {
464 		cond_resched();
465 		if (time_before(jiffies, timeout))
466 			return false;
467 	}
468 
469 	if (fmt) {
470 		va_start(va, fmt);
471 		vprintk(fmt, va);
472 		va_end(va);
473 	}
474 
475 	return true;
476 }
477 
478 void igt_hexdump(const void *buf, size_t len)
479 {
480 	const size_t rowsize = 8 * sizeof(u32);
481 	const void *prev = NULL;
482 	bool skip = false;
483 	size_t pos;
484 
485 	for (pos = 0; pos < len; pos += rowsize) {
486 		char line[128];
487 
488 		if (prev && !memcmp(prev, buf + pos, rowsize)) {
489 			if (!skip) {
490 				pr_info("*\n");
491 				skip = true;
492 			}
493 			continue;
494 		}
495 
496 		WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
497 						rowsize, sizeof(u32),
498 						line, sizeof(line),
499 						false) >= sizeof(line));
500 		pr_info("[%04zx] %s\n", pos, line);
501 
502 		prev = buf + pos;
503 		skip = false;
504 	}
505 }
506 
507 module_param_named(st_random_seed, i915_selftest.random_seed, uint, 0400);
508 module_param_named(st_timeout, i915_selftest.timeout_ms, uint, 0400);
509 module_param_named(st_filter, i915_selftest.filter, charp, 0400);
510 
511 module_param_named_unsafe(mock_selftests, i915_selftest.mock, int, 0400);
512 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)");
513 
514 module_param_named_unsafe(live_selftests, i915_selftest.live, int, 0400);
515 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)");
516 
517 module_param_named_unsafe(perf_selftests, i915_selftest.perf, int, 0400);
518 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)");
519