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 
39 int i915_mock_sanitycheck(void)
40 {
41 	pr_info(DRIVER_NAME ": %s() - ok!\n", __func__);
42 	return 0;
43 }
44 
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 
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
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
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 int __run_selftests(const char *name,
158 			   struct selftest *st,
159 			   unsigned int count,
160 			   void *data)
161 {
162 	int err = 0;
163 
164 	while (!i915_selftest.random_seed)
165 		i915_selftest.random_seed = get_random_u32();
166 
167 	i915_selftest.timeout_jiffies =
168 		i915_selftest.timeout_ms ?
169 		msecs_to_jiffies_timeout(i915_selftest.timeout_ms) :
170 		MAX_SCHEDULE_TIMEOUT;
171 
172 	set_default_test_all(st, count);
173 
174 	pr_info(DRIVER_NAME ": Performing %s selftests with st_random_seed=0x%x st_timeout=%u\n",
175 		name, i915_selftest.random_seed, i915_selftest.timeout_ms);
176 
177 	/* Tests are listed in order in i915_*_selftests.h */
178 	for (; count--; st++) {
179 		if (!st->enabled)
180 			continue;
181 
182 		cond_resched();
183 		if (signal_pending(current))
184 			return -EINTR;
185 
186 		pr_info(DRIVER_NAME ": Running %s\n", st->name);
187 		if (data)
188 			err = st->live(data);
189 		else
190 			err = st->mock();
191 		if (err == -EINTR && !signal_pending(current))
192 			err = 0;
193 		if (err)
194 			break;
195 	}
196 
197 	if (WARN(err > 0 || err == -ENOTTY,
198 		 "%s returned %d, conflicting with selftest's magic values!\n",
199 		 st->name, err))
200 		err = -1;
201 
202 	return err;
203 }
204 
205 #define run_selftests(x, data) \
206 	__run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
207 
208 int i915_mock_selftests(void)
209 {
210 	int err;
211 
212 	if (!i915_selftest.mock)
213 		return 0;
214 
215 	err = run_selftests(mock, NULL);
216 	if (err) {
217 		i915_selftest.mock = err;
218 		return 1;
219 	}
220 
221 	if (i915_selftest.mock < 0) {
222 		i915_selftest.mock = -ENOTTY;
223 		return 1;
224 	}
225 
226 	return 0;
227 }
228 
229 int i915_live_selftests(struct pci_dev *pdev)
230 {
231 	int err;
232 
233 	if (!i915_selftest.live)
234 		return 0;
235 
236 	__wait_gsc_proxy_completed(pdev_to_i915(pdev));
237 
238 	err = run_selftests(live, pdev_to_i915(pdev));
239 	if (err) {
240 		i915_selftest.live = err;
241 		return err;
242 	}
243 
244 	if (i915_selftest.live < 0) {
245 		i915_selftest.live = -ENOTTY;
246 		return 1;
247 	}
248 
249 	return 0;
250 }
251 
252 int i915_perf_selftests(struct pci_dev *pdev)
253 {
254 	int err;
255 
256 	if (!i915_selftest.perf)
257 		return 0;
258 
259 	__wait_gsc_proxy_completed(pdev_to_i915(pdev));
260 
261 	err = run_selftests(perf, pdev_to_i915(pdev));
262 	if (err) {
263 		i915_selftest.perf = err;
264 		return err;
265 	}
266 
267 	if (i915_selftest.perf < 0) {
268 		i915_selftest.perf = -ENOTTY;
269 		return 1;
270 	}
271 
272 	return 0;
273 }
274 
275 static bool apply_subtest_filter(const char *caller, const char *name)
276 {
277 	char *filter, *sep, *tok;
278 	bool result = true;
279 
280 	filter = kstrdup(i915_selftest.filter, GFP_KERNEL);
281 	for (sep = filter; (tok = strsep(&sep, ","));) {
282 		bool allow = true;
283 		char *sl;
284 
285 		if (*tok == '!') {
286 			allow = false;
287 			tok++;
288 		}
289 
290 		if (*tok == '\0')
291 			continue;
292 
293 		sl = strchr(tok, '/');
294 		if (sl) {
295 			*sl++ = '\0';
296 			if (strcmp(tok, caller)) {
297 				if (allow)
298 					result = false;
299 				continue;
300 			}
301 			tok = sl;
302 		}
303 
304 		if (strcmp(tok, name)) {
305 			if (allow)
306 				result = false;
307 			continue;
308 		}
309 
310 		result = allow;
311 		break;
312 	}
313 	kfree(filter);
314 
315 	return result;
316 }
317 
318 int __i915_nop_setup(void *data)
319 {
320 	return 0;
321 }
322 
323 int __i915_nop_teardown(int err, void *data)
324 {
325 	return err;
326 }
327 
328 int __i915_live_setup(void *data)
329 {
330 	struct drm_i915_private *i915 = data;
331 
332 	/* The selftests expect an idle system */
333 	if (intel_gt_pm_wait_for_idle(to_gt(i915)))
334 		return -EIO;
335 
336 	return intel_gt_terminally_wedged(to_gt(i915));
337 }
338 
339 int __i915_live_teardown(int err, void *data)
340 {
341 	struct drm_i915_private *i915 = data;
342 
343 	if (igt_flush_test(i915))
344 		err = -EIO;
345 
346 	i915_gem_drain_freed_objects(i915);
347 
348 	return err;
349 }
350 
351 int __intel_gt_live_setup(void *data)
352 {
353 	struct intel_gt *gt = data;
354 
355 	/* The selftests expect an idle system */
356 	if (intel_gt_pm_wait_for_idle(gt))
357 		return -EIO;
358 
359 	return intel_gt_terminally_wedged(gt);
360 }
361 
362 int __intel_gt_live_teardown(int err, void *data)
363 {
364 	struct intel_gt *gt = data;
365 
366 	if (igt_flush_test(gt->i915))
367 		err = -EIO;
368 
369 	i915_gem_drain_freed_objects(gt->i915);
370 
371 	return err;
372 }
373 
374 int __i915_subtests(const char *caller,
375 		    int (*setup)(void *data),
376 		    int (*teardown)(int err, void *data),
377 		    const struct i915_subtest *st,
378 		    unsigned int count,
379 		    void *data)
380 {
381 	int err;
382 
383 	for (; count--; st++) {
384 		cond_resched();
385 		if (signal_pending(current))
386 			return -EINTR;
387 
388 		if (!apply_subtest_filter(caller, st->name))
389 			continue;
390 
391 		err = setup(data);
392 		if (err) {
393 			pr_err(DRIVER_NAME "/%s: setup failed for %s\n",
394 			       caller, st->name);
395 			return err;
396 		}
397 
398 		pr_info(DRIVER_NAME ": Running %s/%s\n", caller, st->name);
399 		GEM_TRACE("Running %s/%s\n", caller, st->name);
400 
401 		err = teardown(st->func(data), data);
402 		if (err && err != -EINTR) {
403 			pr_err(DRIVER_NAME "/%s: %s failed with error %d\n",
404 			       caller, st->name, err);
405 			return err;
406 		}
407 	}
408 
409 	return 0;
410 }
411 
412 bool __igt_timeout(unsigned long timeout, const char *fmt, ...)
413 {
414 	va_list va;
415 
416 	if (!signal_pending(current)) {
417 		cond_resched();
418 		if (time_before(jiffies, timeout))
419 			return false;
420 	}
421 
422 	if (fmt) {
423 		va_start(va, fmt);
424 		vprintk(fmt, va);
425 		va_end(va);
426 	}
427 
428 	return true;
429 }
430 
431 void igt_hexdump(const void *buf, size_t len)
432 {
433 	const size_t rowsize = 8 * sizeof(u32);
434 	const void *prev = NULL;
435 	bool skip = false;
436 	size_t pos;
437 
438 	for (pos = 0; pos < len; pos += rowsize) {
439 		char line[128];
440 
441 		if (prev && !memcmp(prev, buf + pos, rowsize)) {
442 			if (!skip) {
443 				pr_info("*\n");
444 				skip = true;
445 			}
446 			continue;
447 		}
448 
449 		WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
450 						rowsize, sizeof(u32),
451 						line, sizeof(line),
452 						false) >= sizeof(line));
453 		pr_info("[%04zx] %s\n", pos, line);
454 
455 		prev = buf + pos;
456 		skip = false;
457 	}
458 }
459 
460 module_param_named(st_random_seed, i915_selftest.random_seed, uint, 0400);
461 module_param_named(st_timeout, i915_selftest.timeout_ms, uint, 0400);
462 module_param_named(st_filter, i915_selftest.filter, charp, 0400);
463 
464 module_param_named_unsafe(mock_selftests, i915_selftest.mock, int, 0400);
465 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)");
466 
467 module_param_named_unsafe(live_selftests, i915_selftest.live, int, 0400);
468 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)");
469 
470 module_param_named_unsafe(perf_selftests, i915_selftest.perf, int, 0400);
471 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)");
472