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_info(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 static bool apply_subtest_filter(const char *caller, const char *name) 201 { 202 char *filter, *sep, *tok; 203 bool result = true; 204 205 filter = kstrdup(i915_selftest.filter, GFP_KERNEL); 206 for (sep = filter; (tok = strsep(&sep, ","));) { 207 bool allow = true; 208 char *sl; 209 210 if (*tok == '!') { 211 allow = false; 212 tok++; 213 } 214 215 if (*tok == '\0') 216 continue; 217 218 sl = strchr(tok, '/'); 219 if (sl) { 220 *sl++ = '\0'; 221 if (strcmp(tok, caller)) { 222 if (allow) 223 result = false; 224 continue; 225 } 226 tok = sl; 227 } 228 229 if (strcmp(tok, name)) { 230 if (allow) 231 result = false; 232 continue; 233 } 234 235 result = allow; 236 break; 237 } 238 kfree(filter); 239 240 return result; 241 } 242 243 int __i915_subtests(const char *caller, 244 const struct i915_subtest *st, 245 unsigned int count, 246 void *data) 247 { 248 int err; 249 250 for (; count--; st++) { 251 cond_resched(); 252 if (signal_pending(current)) 253 return -EINTR; 254 255 if (!apply_subtest_filter(caller, st->name)) 256 continue; 257 258 pr_info(DRIVER_NAME ": Running %s/%s\n", caller, st->name); 259 GEM_TRACE("Running %s/%s\n", caller, st->name); 260 261 err = st->func(data); 262 if (err && err != -EINTR) { 263 pr_err(DRIVER_NAME "/%s: %s failed with error %d\n", 264 caller, st->name, err); 265 return err; 266 } 267 } 268 269 return 0; 270 } 271 272 bool __igt_timeout(unsigned long timeout, const char *fmt, ...) 273 { 274 va_list va; 275 276 if (!signal_pending(current)) { 277 cond_resched(); 278 if (time_before(jiffies, timeout)) 279 return false; 280 } 281 282 if (fmt) { 283 va_start(va, fmt); 284 vprintk(fmt, va); 285 va_end(va); 286 } 287 288 return true; 289 } 290 291 module_param_named(st_random_seed, i915_selftest.random_seed, uint, 0400); 292 module_param_named(st_timeout, i915_selftest.timeout_ms, uint, 0400); 293 module_param_named(st_filter, i915_selftest.filter, charp, 0400); 294 295 module_param_named_unsafe(mock_selftests, i915_selftest.mock, int, 0400); 296 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)"); 297 298 module_param_named_unsafe(live_selftests, i915_selftest.live, int, 0400); 299 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)"); 300