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