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 "../i915_drv.h"
27 #include "../i915_selftest.h"
28 
29 #include "igt_flush_test.h"
30 
31 struct i915_selftest i915_selftest __read_mostly = {
32 	.timeout_ms = 1000,
33 };
34 
i915_mock_sanitycheck(void)35 int i915_mock_sanitycheck(void)
36 {
37 	pr_info(DRIVER_NAME ": %s() - ok!\n", __func__);
38 	return 0;
39 }
40 
i915_live_sanitycheck(struct drm_i915_private * i915)41 int i915_live_sanitycheck(struct drm_i915_private *i915)
42 {
43 	pr_info("%s: %s() - ok!\n", i915->drm.driver->name, __func__);
44 	return 0;
45 }
46 
47 enum {
48 #define selftest(name, func) mock_##name,
49 #include "i915_mock_selftests.h"
50 #undef selftest
51 };
52 
53 enum {
54 #define selftest(name, func) live_##name,
55 #include "i915_live_selftests.h"
56 #undef selftest
57 };
58 
59 struct selftest {
60 	bool enabled;
61 	const char *name;
62 	union {
63 		int (*mock)(void);
64 		int (*live)(struct drm_i915_private *);
65 	};
66 };
67 
68 #define selftest(n, f) [mock_##n] = { .name = #n, { .mock = f } },
69 static struct selftest mock_selftests[] = {
70 #include "i915_mock_selftests.h"
71 };
72 #undef selftest
73 
74 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } },
75 static struct selftest live_selftests[] = {
76 #include "i915_live_selftests.h"
77 };
78 #undef selftest
79 
80 /* Embed the line number into the parameter name so that we can order tests */
81 #define selftest(n, func) selftest_0(n, func, param(n))
82 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __mock_##n))
83 #define selftest_0(n, func, id) \
84 module_param_named(id, mock_selftests[mock_##n].enabled, bool, 0400);
85 #include "i915_mock_selftests.h"
86 #undef selftest_0
87 #undef param
88 
89 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __live_##n))
90 #define selftest_0(n, func, id) \
91 module_param_named(id, live_selftests[live_##n].enabled, bool, 0400);
92 #include "i915_live_selftests.h"
93 #undef selftest_0
94 #undef param
95 #undef selftest
96 
set_default_test_all(struct selftest * st,unsigned int count)97 static void set_default_test_all(struct selftest *st, unsigned int count)
98 {
99 	unsigned int i;
100 
101 	for (i = 0; i < count; i++)
102 		if (st[i].enabled)
103 			return;
104 
105 	for (i = 0; i < count; i++)
106 		st[i].enabled = true;
107 }
108 
__run_selftests(const char * name,struct selftest * st,unsigned int count,void * data)109 static int __run_selftests(const char *name,
110 			   struct selftest *st,
111 			   unsigned int count,
112 			   void *data)
113 {
114 	int err = 0;
115 
116 	while (!i915_selftest.random_seed)
117 		i915_selftest.random_seed = get_random_int();
118 
119 	i915_selftest.timeout_jiffies =
120 		i915_selftest.timeout_ms ?
121 		msecs_to_jiffies_timeout(i915_selftest.timeout_ms) :
122 		MAX_SCHEDULE_TIMEOUT;
123 
124 	set_default_test_all(st, count);
125 
126 	pr_info(DRIVER_NAME ": Performing %s selftests with st_random_seed=0x%x st_timeout=%u\n",
127 		name, i915_selftest.random_seed, i915_selftest.timeout_ms);
128 
129 	/* Tests are listed in order in i915_*_selftests.h */
130 	for (; count--; st++) {
131 		if (!st->enabled)
132 			continue;
133 
134 		cond_resched();
135 		if (signal_pending(current))
136 			return -EINTR;
137 
138 		pr_info(DRIVER_NAME ": Running %s\n", st->name);
139 		if (data)
140 			err = st->live(data);
141 		else
142 			err = st->mock();
143 		if (err == -EINTR && !signal_pending(current))
144 			err = 0;
145 		if (err)
146 			break;
147 	}
148 
149 	if (WARN(err > 0 || err == -ENOTTY,
150 		 "%s returned %d, conflicting with selftest's magic values!\n",
151 		 st->name, err))
152 		err = -1;
153 
154 	return err;
155 }
156 
157 #define run_selftests(x, data) \
158 	__run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
159 
i915_mock_selftests(void)160 int i915_mock_selftests(void)
161 {
162 	int err;
163 
164 	if (!i915_selftest.mock)
165 		return 0;
166 
167 	err = run_selftests(mock, NULL);
168 	if (err) {
169 		i915_selftest.mock = err;
170 		return err;
171 	}
172 
173 	if (i915_selftest.mock < 0) {
174 		i915_selftest.mock = -ENOTTY;
175 		return 1;
176 	}
177 
178 	return 0;
179 }
180 
i915_live_selftests(struct pci_dev * pdev)181 int i915_live_selftests(struct pci_dev *pdev)
182 {
183 	int err;
184 
185 	if (!i915_selftest.live)
186 		return 0;
187 
188 	err = run_selftests(live, pdev_to_i915(pdev));
189 	if (err) {
190 		i915_selftest.live = err;
191 		return err;
192 	}
193 
194 	if (i915_selftest.live < 0) {
195 		i915_selftest.live = -ENOTTY;
196 		return 1;
197 	}
198 
199 	return 0;
200 }
201 
apply_subtest_filter(const char * caller,const char * name)202 static bool apply_subtest_filter(const char *caller, const char *name)
203 {
204 	char *filter, *sep, *tok;
205 	bool result = true;
206 
207 	filter = kstrdup(i915_selftest.filter, GFP_KERNEL);
208 	for (sep = filter; (tok = strsep(&sep, ","));) {
209 		bool allow = true;
210 		char *sl;
211 
212 		if (*tok == '!') {
213 			allow = false;
214 			tok++;
215 		}
216 
217 		if (*tok == '\0')
218 			continue;
219 
220 		sl = strchr(tok, '/');
221 		if (sl) {
222 			*sl++ = '\0';
223 			if (strcmp(tok, caller)) {
224 				if (allow)
225 					result = false;
226 				continue;
227 			}
228 			tok = sl;
229 		}
230 
231 		if (strcmp(tok, name)) {
232 			if (allow)
233 				result = false;
234 			continue;
235 		}
236 
237 		result = allow;
238 		break;
239 	}
240 	kfree(filter);
241 
242 	return result;
243 }
244 
__i915_nop_setup(void * data)245 int __i915_nop_setup(void *data)
246 {
247 	return 0;
248 }
249 
__i915_nop_teardown(int err,void * data)250 int __i915_nop_teardown(int err, void *data)
251 {
252 	return err;
253 }
254 
__i915_live_setup(void * data)255 int __i915_live_setup(void *data)
256 {
257 	struct drm_i915_private *i915 = data;
258 
259 	return intel_gt_terminally_wedged(&i915->gt);
260 }
261 
__i915_live_teardown(int err,void * data)262 int __i915_live_teardown(int err, void *data)
263 {
264 	struct drm_i915_private *i915 = data;
265 
266 	mutex_lock(&i915->drm.struct_mutex);
267 	if (igt_flush_test(i915, I915_WAIT_LOCKED))
268 		err = -EIO;
269 	mutex_unlock(&i915->drm.struct_mutex);
270 
271 	i915_gem_drain_freed_objects(i915);
272 
273 	return err;
274 }
275 
__intel_gt_live_setup(void * data)276 int __intel_gt_live_setup(void *data)
277 {
278 	struct intel_gt *gt = data;
279 
280 	return intel_gt_terminally_wedged(gt);
281 }
282 
__intel_gt_live_teardown(int err,void * data)283 int __intel_gt_live_teardown(int err, void *data)
284 {
285 	struct intel_gt *gt = data;
286 
287 	mutex_lock(&gt->i915->drm.struct_mutex);
288 	if (igt_flush_test(gt->i915, I915_WAIT_LOCKED))
289 		err = -EIO;
290 	mutex_unlock(&gt->i915->drm.struct_mutex);
291 
292 	i915_gem_drain_freed_objects(gt->i915);
293 
294 	return err;
295 }
296 
__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)297 int __i915_subtests(const char *caller,
298 		    int (*setup)(void *data),
299 		    int (*teardown)(int err, void *data),
300 		    const struct i915_subtest *st,
301 		    unsigned int count,
302 		    void *data)
303 {
304 	int err;
305 
306 	for (; count--; st++) {
307 		cond_resched();
308 		if (signal_pending(current))
309 			return -EINTR;
310 
311 		if (!apply_subtest_filter(caller, st->name))
312 			continue;
313 
314 		err = setup(data);
315 		if (err) {
316 			pr_err(DRIVER_NAME "/%s: setup failed for %s\n",
317 			       caller, st->name);
318 			return err;
319 		}
320 
321 		pr_info(DRIVER_NAME ": Running %s/%s\n", caller, st->name);
322 		GEM_TRACE("Running %s/%s\n", caller, st->name);
323 
324 		err = teardown(st->func(data), data);
325 		if (err && err != -EINTR) {
326 			pr_err(DRIVER_NAME "/%s: %s failed with error %d\n",
327 			       caller, st->name, err);
328 			return err;
329 		}
330 	}
331 
332 	return 0;
333 }
334 
__igt_timeout(unsigned long timeout,const char * fmt,...)335 bool __igt_timeout(unsigned long timeout, const char *fmt, ...)
336 {
337 	va_list va;
338 
339 	if (!signal_pending(current)) {
340 		cond_resched();
341 		if (time_before(jiffies, timeout))
342 			return false;
343 	}
344 
345 	if (fmt) {
346 		va_start(va, fmt);
347 		vprintk(fmt, va);
348 		va_end(va);
349 	}
350 
351 	return true;
352 }
353 
354 module_param_named(st_random_seed, i915_selftest.random_seed, uint, 0400);
355 module_param_named(st_timeout, i915_selftest.timeout_ms, uint, 0400);
356 module_param_named(st_filter, i915_selftest.filter, charp, 0400);
357 
358 module_param_named_unsafe(mock_selftests, i915_selftest.mock, int, 0400);
359 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 exit module)");
360 
361 module_param_named_unsafe(live_selftests, i915_selftest.live, int, 0400);
362 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)");
363