Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright © 2016 Intel Corporation
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice (including the next
0012  * paragraph) shall be included in all copies or substantial portions of the
0013  * Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0020  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
0021  * IN THE SOFTWARE.
0022  */
0023 
0024 #include <linux/random.h>
0025 
0026 #include "gt/intel_gt_pm.h"
0027 #include "i915_driver.h"
0028 #include "i915_drv.h"
0029 #include "i915_selftest.h"
0030 
0031 #include "igt_flush_test.h"
0032 
0033 struct i915_selftest i915_selftest __read_mostly = {
0034     .timeout_ms = 500,
0035 };
0036 
0037 int i915_mock_sanitycheck(void)
0038 {
0039     pr_info(DRIVER_NAME ": %s() - ok!\n", __func__);
0040     return 0;
0041 }
0042 
0043 int i915_live_sanitycheck(struct drm_i915_private *i915)
0044 {
0045     pr_info("%s: %s() - ok!\n", i915->drm.driver->name, __func__);
0046     return 0;
0047 }
0048 
0049 enum {
0050 #define selftest(name, func) mock_##name,
0051 #include "i915_mock_selftests.h"
0052 #undef selftest
0053 };
0054 
0055 enum {
0056 #define selftest(name, func) live_##name,
0057 #include "i915_live_selftests.h"
0058 #undef selftest
0059 };
0060 
0061 enum {
0062 #define selftest(name, func) perf_##name,
0063 #include "i915_perf_selftests.h"
0064 #undef selftest
0065 };
0066 
0067 struct selftest {
0068     bool enabled;
0069     const char *name;
0070     union {
0071         int (*mock)(void);
0072         int (*live)(struct drm_i915_private *);
0073     };
0074 };
0075 
0076 #define selftest(n, f) [mock_##n] = { .name = #n, { .mock = f } },
0077 static struct selftest mock_selftests[] = {
0078 #include "i915_mock_selftests.h"
0079 };
0080 #undef selftest
0081 
0082 #define selftest(n, f) [live_##n] = { .name = #n, { .live = f } },
0083 static struct selftest live_selftests[] = {
0084 #include "i915_live_selftests.h"
0085 };
0086 #undef selftest
0087 
0088 #define selftest(n, f) [perf_##n] = { .name = #n, { .live = f } },
0089 static struct selftest perf_selftests[] = {
0090 #include "i915_perf_selftests.h"
0091 };
0092 #undef selftest
0093 
0094 /* Embed the line number into the parameter name so that we can order tests */
0095 #define selftest(n, func) selftest_0(n, func, param(n))
0096 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __mock_##n))
0097 #define selftest_0(n, func, id) \
0098 module_param_named(id, mock_selftests[mock_##n].enabled, bool, 0400);
0099 #include "i915_mock_selftests.h"
0100 #undef selftest_0
0101 #undef param
0102 
0103 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __live_##n))
0104 #define selftest_0(n, func, id) \
0105 module_param_named(id, live_selftests[live_##n].enabled, bool, 0400);
0106 #include "i915_live_selftests.h"
0107 #undef selftest_0
0108 #undef param
0109 
0110 #define param(n) __PASTE(igt__, __PASTE(__LINE__, __perf_##n))
0111 #define selftest_0(n, func, id) \
0112 module_param_named(id, perf_selftests[perf_##n].enabled, bool, 0400);
0113 #include "i915_perf_selftests.h"
0114 #undef selftest_0
0115 #undef param
0116 #undef selftest
0117 
0118 static void set_default_test_all(struct selftest *st, unsigned int count)
0119 {
0120     unsigned int i;
0121 
0122     for (i = 0; i < count; i++)
0123         if (st[i].enabled)
0124             return;
0125 
0126     for (i = 0; i < count; i++)
0127         st[i].enabled = true;
0128 }
0129 
0130 static int __run_selftests(const char *name,
0131                struct selftest *st,
0132                unsigned int count,
0133                void *data)
0134 {
0135     int err = 0;
0136 
0137     while (!i915_selftest.random_seed)
0138         i915_selftest.random_seed = get_random_int();
0139 
0140     i915_selftest.timeout_jiffies =
0141         i915_selftest.timeout_ms ?
0142         msecs_to_jiffies_timeout(i915_selftest.timeout_ms) :
0143         MAX_SCHEDULE_TIMEOUT;
0144 
0145     set_default_test_all(st, count);
0146 
0147     pr_info(DRIVER_NAME ": Performing %s selftests with st_random_seed=0x%x st_timeout=%u\n",
0148         name, i915_selftest.random_seed, i915_selftest.timeout_ms);
0149 
0150     /* Tests are listed in order in i915_*_selftests.h */
0151     for (; count--; st++) {
0152         if (!st->enabled)
0153             continue;
0154 
0155         cond_resched();
0156         if (signal_pending(current))
0157             return -EINTR;
0158 
0159         pr_info(DRIVER_NAME ": Running %s\n", st->name);
0160         if (data)
0161             err = st->live(data);
0162         else
0163             err = st->mock();
0164         if (err == -EINTR && !signal_pending(current))
0165             err = 0;
0166         if (err)
0167             break;
0168     }
0169 
0170     if (WARN(err > 0 || err == -ENOTTY,
0171          "%s returned %d, conflicting with selftest's magic values!\n",
0172          st->name, err))
0173         err = -1;
0174 
0175     return err;
0176 }
0177 
0178 #define run_selftests(x, data) \
0179     __run_selftests(#x, x##_selftests, ARRAY_SIZE(x##_selftests), data)
0180 
0181 int i915_mock_selftests(void)
0182 {
0183     int err;
0184 
0185     if (!i915_selftest.mock)
0186         return 0;
0187 
0188     err = run_selftests(mock, NULL);
0189     if (err) {
0190         i915_selftest.mock = err;
0191         return 1;
0192     }
0193 
0194     if (i915_selftest.mock < 0) {
0195         i915_selftest.mock = -ENOTTY;
0196         return 1;
0197     }
0198 
0199     return 0;
0200 }
0201 
0202 int i915_live_selftests(struct pci_dev *pdev)
0203 {
0204     int err;
0205 
0206     if (!i915_selftest.live)
0207         return 0;
0208 
0209     err = run_selftests(live, pdev_to_i915(pdev));
0210     if (err) {
0211         i915_selftest.live = err;
0212         return err;
0213     }
0214 
0215     if (i915_selftest.live < 0) {
0216         i915_selftest.live = -ENOTTY;
0217         return 1;
0218     }
0219 
0220     return 0;
0221 }
0222 
0223 int i915_perf_selftests(struct pci_dev *pdev)
0224 {
0225     int err;
0226 
0227     if (!i915_selftest.perf)
0228         return 0;
0229 
0230     err = run_selftests(perf, pdev_to_i915(pdev));
0231     if (err) {
0232         i915_selftest.perf = err;
0233         return err;
0234     }
0235 
0236     if (i915_selftest.perf < 0) {
0237         i915_selftest.perf = -ENOTTY;
0238         return 1;
0239     }
0240 
0241     return 0;
0242 }
0243 
0244 static bool apply_subtest_filter(const char *caller, const char *name)
0245 {
0246     char *filter, *sep, *tok;
0247     bool result = true;
0248 
0249     filter = kstrdup(i915_selftest.filter, GFP_KERNEL);
0250     for (sep = filter; (tok = strsep(&sep, ","));) {
0251         bool allow = true;
0252         char *sl;
0253 
0254         if (*tok == '!') {
0255             allow = false;
0256             tok++;
0257         }
0258 
0259         if (*tok == '\0')
0260             continue;
0261 
0262         sl = strchr(tok, '/');
0263         if (sl) {
0264             *sl++ = '\0';
0265             if (strcmp(tok, caller)) {
0266                 if (allow)
0267                     result = false;
0268                 continue;
0269             }
0270             tok = sl;
0271         }
0272 
0273         if (strcmp(tok, name)) {
0274             if (allow)
0275                 result = false;
0276             continue;
0277         }
0278 
0279         result = allow;
0280         break;
0281     }
0282     kfree(filter);
0283 
0284     return result;
0285 }
0286 
0287 int __i915_nop_setup(void *data)
0288 {
0289     return 0;
0290 }
0291 
0292 int __i915_nop_teardown(int err, void *data)
0293 {
0294     return err;
0295 }
0296 
0297 int __i915_live_setup(void *data)
0298 {
0299     struct drm_i915_private *i915 = data;
0300 
0301     /* The selftests expect an idle system */
0302     if (intel_gt_pm_wait_for_idle(to_gt(i915)))
0303         return -EIO;
0304 
0305     return intel_gt_terminally_wedged(to_gt(i915));
0306 }
0307 
0308 int __i915_live_teardown(int err, void *data)
0309 {
0310     struct drm_i915_private *i915 = data;
0311 
0312     if (igt_flush_test(i915))
0313         err = -EIO;
0314 
0315     i915_gem_drain_freed_objects(i915);
0316 
0317     return err;
0318 }
0319 
0320 int __intel_gt_live_setup(void *data)
0321 {
0322     struct intel_gt *gt = data;
0323 
0324     /* The selftests expect an idle system */
0325     if (intel_gt_pm_wait_for_idle(gt))
0326         return -EIO;
0327 
0328     return intel_gt_terminally_wedged(gt);
0329 }
0330 
0331 int __intel_gt_live_teardown(int err, void *data)
0332 {
0333     struct intel_gt *gt = data;
0334 
0335     if (igt_flush_test(gt->i915))
0336         err = -EIO;
0337 
0338     i915_gem_drain_freed_objects(gt->i915);
0339 
0340     return err;
0341 }
0342 
0343 int __i915_subtests(const char *caller,
0344             int (*setup)(void *data),
0345             int (*teardown)(int err, void *data),
0346             const struct i915_subtest *st,
0347             unsigned int count,
0348             void *data)
0349 {
0350     int err;
0351 
0352     for (; count--; st++) {
0353         cond_resched();
0354         if (signal_pending(current))
0355             return -EINTR;
0356 
0357         if (!apply_subtest_filter(caller, st->name))
0358             continue;
0359 
0360         err = setup(data);
0361         if (err) {
0362             pr_err(DRIVER_NAME "/%s: setup failed for %s\n",
0363                    caller, st->name);
0364             return err;
0365         }
0366 
0367         pr_info(DRIVER_NAME ": Running %s/%s\n", caller, st->name);
0368         GEM_TRACE("Running %s/%s\n", caller, st->name);
0369 
0370         err = teardown(st->func(data), data);
0371         if (err && err != -EINTR) {
0372             pr_err(DRIVER_NAME "/%s: %s failed with error %d\n",
0373                    caller, st->name, err);
0374             return err;
0375         }
0376     }
0377 
0378     return 0;
0379 }
0380 
0381 bool __igt_timeout(unsigned long timeout, const char *fmt, ...)
0382 {
0383     va_list va;
0384 
0385     if (!signal_pending(current)) {
0386         cond_resched();
0387         if (time_before(jiffies, timeout))
0388             return false;
0389     }
0390 
0391     if (fmt) {
0392         va_start(va, fmt);
0393         vprintk(fmt, va);
0394         va_end(va);
0395     }
0396 
0397     return true;
0398 }
0399 
0400 void igt_hexdump(const void *buf, size_t len)
0401 {
0402     const size_t rowsize = 8 * sizeof(u32);
0403     const void *prev = NULL;
0404     bool skip = false;
0405     size_t pos;
0406 
0407     for (pos = 0; pos < len; pos += rowsize) {
0408         char line[128];
0409 
0410         if (prev && !memcmp(prev, buf + pos, rowsize)) {
0411             if (!skip) {
0412                 pr_info("*\n");
0413                 skip = true;
0414             }
0415             continue;
0416         }
0417 
0418         WARN_ON_ONCE(hex_dump_to_buffer(buf + pos, len - pos,
0419                         rowsize, sizeof(u32),
0420                         line, sizeof(line),
0421                         false) >= sizeof(line));
0422         pr_info("[%04zx] %s\n", pos, line);
0423 
0424         prev = buf + pos;
0425         skip = false;
0426     }
0427 }
0428 
0429 module_param_named(st_random_seed, i915_selftest.random_seed, uint, 0400);
0430 module_param_named(st_timeout, i915_selftest.timeout_ms, uint, 0400);
0431 module_param_named(st_filter, i915_selftest.filter, charp, 0400);
0432 
0433 module_param_named_unsafe(mock_selftests, i915_selftest.mock, int, 0400);
0434 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 leave dummy module)");
0435 
0436 module_param_named_unsafe(live_selftests, i915_selftest.live, int, 0400);
0437 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)");
0438 
0439 module_param_named_unsafe(perf_selftests, i915_selftest.perf, int, 0400);
0440 MODULE_PARM_DESC(perf_selftests, "Run performance orientated selftests after driver initialisation on the live system (0:disabled [default], 1:run tests then continue, -1:run tests then exit module)");