1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright © 2016 Intel Corporation
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun * Software.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*4882a593Smuzhiyun * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*4882a593Smuzhiyun * IN THE SOFTWARE.
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #include <linux/compiler.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define selftest(name, func) __idx_##name,
27*4882a593Smuzhiyun enum {
28*4882a593Smuzhiyun #include TESTS
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun #undef selftest
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #define selftest(n, f) [__idx_##n] = { .name = #n, .func = f },
33*4882a593Smuzhiyun static struct drm_selftest {
34*4882a593Smuzhiyun bool enabled;
35*4882a593Smuzhiyun const char *name;
36*4882a593Smuzhiyun int (*func)(void *);
37*4882a593Smuzhiyun } selftests[] = {
38*4882a593Smuzhiyun #include TESTS
39*4882a593Smuzhiyun };
40*4882a593Smuzhiyun #undef selftest
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* Embed the line number into the parameter name so that we can order tests */
43*4882a593Smuzhiyun #define param(n) __PASTE(igt__, __PASTE(__PASTE(__LINE__, __), n))
44*4882a593Smuzhiyun #define selftest_0(n, func, id) \
45*4882a593Smuzhiyun module_param_named(id, selftests[__idx_##n].enabled, bool, 0400);
46*4882a593Smuzhiyun #define selftest(n, func) selftest_0(n, func, param(n))
47*4882a593Smuzhiyun #include TESTS
48*4882a593Smuzhiyun #undef selftest
49*4882a593Smuzhiyun
set_default_test_all(struct drm_selftest * st,unsigned long count)50*4882a593Smuzhiyun static void set_default_test_all(struct drm_selftest *st, unsigned long count)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun unsigned long i;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun for (i = 0; i < count; i++)
55*4882a593Smuzhiyun if (st[i].enabled)
56*4882a593Smuzhiyun return;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun for (i = 0; i < count; i++)
59*4882a593Smuzhiyun st[i].enabled = true;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
run_selftests(struct drm_selftest * st,unsigned long count,void * data)62*4882a593Smuzhiyun static int run_selftests(struct drm_selftest *st,
63*4882a593Smuzhiyun unsigned long count,
64*4882a593Smuzhiyun void *data)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun int err = 0;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun set_default_test_all(st, count);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* Tests are listed in natural order in drm_*_selftests.h */
71*4882a593Smuzhiyun for (; count--; st++) {
72*4882a593Smuzhiyun if (!st->enabled)
73*4882a593Smuzhiyun continue;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun pr_debug("drm: Running %s\n", st->name);
76*4882a593Smuzhiyun err = st->func(data);
77*4882a593Smuzhiyun if (err)
78*4882a593Smuzhiyun break;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun if (WARN(err > 0 || err == -ENOTTY,
82*4882a593Smuzhiyun "%s returned %d, conflicting with selftest's magic values!\n",
83*4882a593Smuzhiyun st->name, err))
84*4882a593Smuzhiyun err = -1;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun rcu_barrier();
87*4882a593Smuzhiyun return err;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun static int __maybe_unused
__drm_subtests(const char * caller,const struct drm_subtest * st,int count,void * data)91*4882a593Smuzhiyun __drm_subtests(const char *caller,
92*4882a593Smuzhiyun const struct drm_subtest *st,
93*4882a593Smuzhiyun int count,
94*4882a593Smuzhiyun void *data)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun int err;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun for (; count--; st++) {
99*4882a593Smuzhiyun pr_debug("Running %s/%s\n", caller, st->name);
100*4882a593Smuzhiyun err = st->func(data);
101*4882a593Smuzhiyun if (err) {
102*4882a593Smuzhiyun pr_err("%s: %s failed with error %d\n",
103*4882a593Smuzhiyun caller, st->name, err);
104*4882a593Smuzhiyun return err;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun return 0;
109*4882a593Smuzhiyun }
110