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 struct i915_selftest i915_selftest __read_mostly = { 30 .timeout_ms = 1000, 31 }; 32 33 int i915_mock_sanitycheck(void) 34 { 35 pr_info(DRIVER_NAME ": %s() - ok!\n", __func__); 36 return 0; 37 } 38 39 int i915_live_sanitycheck(struct drm_i915_private *i915) 40 { 41 pr_info("%s: %s() - ok!\n", i915->drm.driver->name, __func__); 42 return 0; 43 } 44 45 enum { 46 #define selftest(name, func) mock_##name, 47 #include "i915_mock_selftests.h" 48 #undef selftest 49 }; 50 51 enum { 52 #define selftest(name, func) live_##name, 53 #include "i915_live_selftests.h" 54 #undef selftest 55 }; 56 57 struct selftest { 58 bool enabled; 59 const char *name; 60 union { 61 int (*mock)(void); 62 int (*live)(struct drm_i915_private *); 63 }; 64 }; 65 66 #define selftest(n, f) [mock_##n] = { .name = #n, { .mock = f } }, 67 static struct selftest mock_selftests[] = { 68 #include "i915_mock_selftests.h" 69 }; 70 #undef selftest 71 72 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } }, 73 static struct selftest live_selftests[] = { 74 #include "i915_live_selftests.h" 75 }; 76 #undef selftest 77 78 /* Embed the line number into the parameter name so that we can order tests */ 79 #define selftest(n, func) selftest_0(n, func, param(n)) 80 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __mock_##n)) 81 #define selftest_0(n, func, id) \ 82 module_param_named(id, mock_selftests[mock_##n].enabled, bool, 0400); 83 #include "i915_mock_selftests.h" 84 #undef selftest_0 85 #undef param 86 87 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __live_##n)) 88 #define selftest_0(n, func, id) \ 89 module_param_named(id, live_selftests[live_##n].enabled, bool, 0400); 90 #include "i915_live_selftests.h" 91 #undef selftest_0 92 #undef param 93 #undef selftest 94 95 static void set_default_test_all(struct selftest *st, unsigned int count) 96 { 97 unsigned int i; 98 99 for (i = 0; i < count; i++) 100 if (st[i].enabled) 101 return; 102 103 for (i = 0; i < count; i++) 104 st[i].enabled = true; 105 } 106 107 static int __run_selftests(const char *name, 108 struct selftest *st, 109 unsigned int count, 110 void *data) 111 { 112 int err = 0; 113 114 while (!i915_selftest.random_seed) 115 i915_selftest.random_seed = get_random_int(); 116 117 i915_selftest.timeout_jiffies = 118 i915_selftest.timeout_ms ? 119 msecs_to_jiffies_timeout(i915_selftest.timeout_ms) : 120 MAX_SCHEDULE_TIMEOUT; 121 122 set_default_test_all(st, count); 123 124 pr_info(DRIVER_NAME ": Performing %s selftests with st_random_seed=0x%x st_timeout=%u\n", 125 name, i915_selftest.random_seed, i915_selftest.timeout_ms); 126 127 /* Tests are listed in order in i915_*_selftests.h */ 128 for (; count--; st++) { 129 if (!st->enabled) 130 continue; 131 132 cond_resched(); 133 if (signal_pending(current)) 134 return -EINTR; 135 136 pr_debug(DRIVER_NAME ": Running %s\n", st->name); 137 if (data) 138 err = st->live(data); 139 else 140 err = st->mock(); 141 if (err == -EINTR && !signal_pending(current)) 142 err = 0; 143 if (err) 144 break; 145 } 146 147 if (WARN(err > 0 || err == -ENOTTY, 148 "%s returned %d, conflicting with selftest's magic values!\n", 149 st->name, err)) 150 err = -1; 151 152 return err; 153 } 154 155 #define run_selftests(x, data) \ 156 __run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data) 157 158 int i915_mock_selftests(void) 159 { 160 int err; 161 162 if (!i915_selftest.mock) 163 return 0; 164 165 err = run_selftests(mock, NULL); 166 if (err) { 167 i915_selftest.mock = err; 168 return err; 169 } 170 171 if (i915_selftest.mock < 0) { 172 i915_selftest.mock = -ENOTTY; 173 return 1; 174 } 175 176 return 0; 177 } 178 179 int i915_live_selftests(struct pci_dev *pdev) 180 { 181 int err; 182 183 if (!i915_selftest.live) 184 return 0; 185 186 err = run_selftests(live, to_i915(pci_get_drvdata(pdev))); 187 if (err) { 188 i915_selftest.live = err; 189 return err; 190 } 191 192 if (i915_selftest.live < 0) { 193 i915_selftest.live = -ENOTTY; 194 return 1; 195 } 196 197 return 0; 198 } 199 200 int __i915_subtests(const char *caller, 201 const struct i915_subtest *st, 202 unsigned int count, 203 void *data) 204 { 205 int err; 206 207 for (; count--; st++) { 208 cond_resched(); 209 if (signal_pending(current)) 210 return -EINTR; 211 212 pr_debug(DRIVER_NAME ": Running %s/%s\n", caller, st->name); 213 GEM_TRACE("Running %s/%s\n", caller, st->name); 214 215 err = st->func(data); 216 if (err && err != -EINTR) { 217 pr_err(DRIVER_NAME "/%s: %s failed with error %d\n", 218 caller, st->name, err); 219 return err; 220 } 221 } 222 223 return 0; 224 } 225 226 bool __igt_timeout(unsigned long timeout, const char *fmt, ...) 227 { 228 va_list va; 229 230 if (!signal_pending(current)) { 231 cond_resched(); 232 if (time_before(jiffies, timeout)) 233 return false; 234 } 235 236 if (fmt) { 237 va_start(va, fmt); 238 vprintk(fmt, va); 239 va_end(va); 240 } 241 242 return true; 243 } 244 245 module_param_named(st_random_seed, i915_selftest.random_seed, uint, 0400); 246 module_param_named(st_timeout, i915_selftest.timeout_ms, uint, 0400); 247 248 module_param_named_unsafe(mock_selftests, i915_selftest.mock, int, 0400); 249 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)"); 250 251 module_param_named_unsafe(live_selftests, i915_selftest.live, int, 0400); 252 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)"); 253