i915_selftest.c 6.1 KB

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