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