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/compiler.h>
25
26#define selftest(name, func) __idx_##name,
27enum {
28#include TESTS
29};
30#undef selftest
31
32#define selftest(n, f) [__idx_##n] = { .name = #n, .func = f },
33static struct drm_selftest {
34	bool enabled;
35	const char *name;
36	int (*func)(void *);
37} selftests[] = {
38#include TESTS
39};
40#undef selftest
41
42/* Embed the line number into the parameter name so that we can order tests */
43#define param(n) __PASTE(igt__, __PASTE(__PASTE(__LINE__, __), n))
44#define selftest_0(n, func, id) \
45module_param_named(id, selftests[__idx_##n].enabled, bool, 0400);
46#define selftest(n, func) selftest_0(n, func, param(n))
47#include TESTS
48#undef selftest
49
50static void set_default_test_all(struct drm_selftest *st, unsigned long count)
51{
52	unsigned long i;
53
54	for (i = 0; i < count; i++)
55		if (st[i].enabled)
56			return;
57
58	for (i = 0; i < count; i++)
59		st[i].enabled = true;
60}
61
62static int run_selftests(struct drm_selftest *st,
63			 unsigned long count,
64			 void *data)
65{
66	int err = 0;
67
68	set_default_test_all(st, count);
69
70	/* Tests are listed in natural order in drm_*_selftests.h */
71	for (; count--; st++) {
72		if (!st->enabled)
73			continue;
74
75		pr_debug("drm: Running %s\n", st->name);
76		err = st->func(data);
77		if (err)
78			break;
79	}
80
81	if (WARN(err > 0 || err == -ENOTTY,
82		 "%s returned %d, conflicting with selftest's magic values!\n",
83		 st->name, err))
84		err = -1;
85
86	rcu_barrier();
87	return err;
88}
89
90static int __maybe_unused
91__drm_subtests(const char *caller,
92	       const struct drm_subtest *st,
93	       int count,
94	       void *data)
95{
96	int err;
97
98	for (; count--; st++) {
99		pr_debug("Running %s/%s\n", caller, st->name);
100		err = st->func(data);
101		if (err) {
102			pr_err("%s: %s failed with error %d\n",
103			       caller, st->name, err);
104			return err;
105		}
106	}
107
108	return 0;
109}
110