i915_gem_request.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/prime_numbers.h>
  25. #include "../i915_selftest.h"
  26. #include "mock_gem_device.h"
  27. static int igt_add_request(void *arg)
  28. {
  29. struct drm_i915_private *i915 = arg;
  30. struct drm_i915_gem_request *request;
  31. int err = -ENOMEM;
  32. /* Basic preliminary test to create a request and let it loose! */
  33. mutex_lock(&i915->drm.struct_mutex);
  34. request = mock_request(i915->engine[RCS],
  35. i915->kernel_context,
  36. HZ / 10);
  37. if (!request)
  38. goto out_unlock;
  39. i915_add_request(request);
  40. err = 0;
  41. out_unlock:
  42. mutex_unlock(&i915->drm.struct_mutex);
  43. return err;
  44. }
  45. static int igt_wait_request(void *arg)
  46. {
  47. const long T = HZ / 4;
  48. struct drm_i915_private *i915 = arg;
  49. struct drm_i915_gem_request *request;
  50. int err = -EINVAL;
  51. /* Submit a request, then wait upon it */
  52. mutex_lock(&i915->drm.struct_mutex);
  53. request = mock_request(i915->engine[RCS], i915->kernel_context, T);
  54. if (!request) {
  55. err = -ENOMEM;
  56. goto out_unlock;
  57. }
  58. if (i915_wait_request(request, I915_WAIT_LOCKED, 0) != -ETIME) {
  59. pr_err("request wait (busy query) succeeded (expected timeout before submit!)\n");
  60. goto out_unlock;
  61. }
  62. if (i915_wait_request(request, I915_WAIT_LOCKED, T) != -ETIME) {
  63. pr_err("request wait succeeded (expected timeout before submit!)\n");
  64. goto out_unlock;
  65. }
  66. if (i915_gem_request_completed(request)) {
  67. pr_err("request completed before submit!!\n");
  68. goto out_unlock;
  69. }
  70. i915_add_request(request);
  71. if (i915_wait_request(request, I915_WAIT_LOCKED, 0) != -ETIME) {
  72. pr_err("request wait (busy query) succeeded (expected timeout after submit!)\n");
  73. goto out_unlock;
  74. }
  75. if (i915_gem_request_completed(request)) {
  76. pr_err("request completed immediately!\n");
  77. goto out_unlock;
  78. }
  79. if (i915_wait_request(request, I915_WAIT_LOCKED, T / 2) != -ETIME) {
  80. pr_err("request wait succeeded (expected timeout!)\n");
  81. goto out_unlock;
  82. }
  83. if (i915_wait_request(request, I915_WAIT_LOCKED, T) == -ETIME) {
  84. pr_err("request wait timed out!\n");
  85. goto out_unlock;
  86. }
  87. if (!i915_gem_request_completed(request)) {
  88. pr_err("request not complete after waiting!\n");
  89. goto out_unlock;
  90. }
  91. if (i915_wait_request(request, I915_WAIT_LOCKED, T) == -ETIME) {
  92. pr_err("request wait timed out when already complete!\n");
  93. goto out_unlock;
  94. }
  95. err = 0;
  96. out_unlock:
  97. mock_device_flush(i915);
  98. mutex_unlock(&i915->drm.struct_mutex);
  99. return err;
  100. }
  101. static int igt_fence_wait(void *arg)
  102. {
  103. const long T = HZ / 4;
  104. struct drm_i915_private *i915 = arg;
  105. struct drm_i915_gem_request *request;
  106. int err = -EINVAL;
  107. /* Submit a request, treat it as a fence and wait upon it */
  108. mutex_lock(&i915->drm.struct_mutex);
  109. request = mock_request(i915->engine[RCS], i915->kernel_context, T);
  110. if (!request) {
  111. err = -ENOMEM;
  112. goto out_locked;
  113. }
  114. mutex_unlock(&i915->drm.struct_mutex); /* safe as we are single user */
  115. if (dma_fence_wait_timeout(&request->fence, false, T) != -ETIME) {
  116. pr_err("fence wait success before submit (expected timeout)!\n");
  117. goto out_device;
  118. }
  119. mutex_lock(&i915->drm.struct_mutex);
  120. i915_add_request(request);
  121. mutex_unlock(&i915->drm.struct_mutex);
  122. if (dma_fence_is_signaled(&request->fence)) {
  123. pr_err("fence signaled immediately!\n");
  124. goto out_device;
  125. }
  126. if (dma_fence_wait_timeout(&request->fence, false, T / 2) != -ETIME) {
  127. pr_err("fence wait success after submit (expected timeout)!\n");
  128. goto out_device;
  129. }
  130. if (dma_fence_wait_timeout(&request->fence, false, T) <= 0) {
  131. pr_err("fence wait timed out (expected success)!\n");
  132. goto out_device;
  133. }
  134. if (!dma_fence_is_signaled(&request->fence)) {
  135. pr_err("fence unsignaled after waiting!\n");
  136. goto out_device;
  137. }
  138. if (dma_fence_wait_timeout(&request->fence, false, T) <= 0) {
  139. pr_err("fence wait timed out when complete (expected success)!\n");
  140. goto out_device;
  141. }
  142. err = 0;
  143. out_device:
  144. mutex_lock(&i915->drm.struct_mutex);
  145. out_locked:
  146. mock_device_flush(i915);
  147. mutex_unlock(&i915->drm.struct_mutex);
  148. return err;
  149. }
  150. int i915_gem_request_mock_selftests(void)
  151. {
  152. static const struct i915_subtest tests[] = {
  153. SUBTEST(igt_add_request),
  154. SUBTEST(igt_wait_request),
  155. SUBTEST(igt_fence_wait),
  156. };
  157. struct drm_i915_private *i915;
  158. int err;
  159. i915 = mock_gem_device();
  160. if (!i915)
  161. return -ENOMEM;
  162. err = i915_subtests(tests, i915);
  163. drm_dev_unref(&i915->drm);
  164. return err;
  165. }
  166. struct live_test {
  167. struct drm_i915_private *i915;
  168. const char *func;
  169. const char *name;
  170. unsigned int reset_count;
  171. };
  172. static int begin_live_test(struct live_test *t,
  173. struct drm_i915_private *i915,
  174. const char *func,
  175. const char *name)
  176. {
  177. int err;
  178. t->i915 = i915;
  179. t->func = func;
  180. t->name = name;
  181. err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
  182. if (err) {
  183. pr_err("%s(%s): failed to idle before, with err=%d!",
  184. func, name, err);
  185. return err;
  186. }
  187. i915_gem_retire_requests(i915);
  188. i915->gpu_error.missed_irq_rings = 0;
  189. t->reset_count = i915_reset_count(&i915->gpu_error);
  190. return 0;
  191. }
  192. static int end_live_test(struct live_test *t)
  193. {
  194. struct drm_i915_private *i915 = t->i915;
  195. if (wait_for(intel_execlists_idle(i915), 1)) {
  196. pr_err("%s(%s): GPU not idle\n", t->func, t->name);
  197. return -EIO;
  198. }
  199. if (t->reset_count != i915_reset_count(&i915->gpu_error)) {
  200. pr_err("%s(%s): GPU was reset %d times!\n",
  201. t->func, t->name,
  202. i915_reset_count(&i915->gpu_error) - t->reset_count);
  203. return -EIO;
  204. }
  205. if (i915->gpu_error.missed_irq_rings) {
  206. pr_err("%s(%s): Missed interrupts on engines %lx\n",
  207. t->func, t->name, i915->gpu_error.missed_irq_rings);
  208. return -EIO;
  209. }
  210. return 0;
  211. }
  212. static int live_nop_request(void *arg)
  213. {
  214. struct drm_i915_private *i915 = arg;
  215. struct intel_engine_cs *engine;
  216. struct live_test t;
  217. unsigned int id;
  218. int err;
  219. /* Submit various sized batches of empty requests, to each engine
  220. * (individually), and wait for the batch to complete. We can check
  221. * the overhead of submitting requests to the hardware.
  222. */
  223. mutex_lock(&i915->drm.struct_mutex);
  224. for_each_engine(engine, i915, id) {
  225. IGT_TIMEOUT(end_time);
  226. struct drm_i915_gem_request *request;
  227. unsigned long n, prime;
  228. ktime_t times[2] = {};
  229. err = begin_live_test(&t, i915, __func__, engine->name);
  230. if (err)
  231. goto out_unlock;
  232. for_each_prime_number_from(prime, 1, 8192) {
  233. times[1] = ktime_get_raw();
  234. for (n = 0; n < prime; n++) {
  235. request = i915_gem_request_alloc(engine,
  236. i915->kernel_context);
  237. if (IS_ERR(request)) {
  238. err = PTR_ERR(request);
  239. goto out_unlock;
  240. }
  241. /* This space is left intentionally blank.
  242. *
  243. * We do not actually want to perform any
  244. * action with this request, we just want
  245. * to measure the latency in allocation
  246. * and submission of our breadcrumbs -
  247. * ensuring that the bare request is sufficient
  248. * for the system to work (i.e. proper HEAD
  249. * tracking of the rings, interrupt handling,
  250. * etc). It also gives us the lowest bounds
  251. * for latency.
  252. */
  253. i915_add_request(request);
  254. }
  255. i915_wait_request(request,
  256. I915_WAIT_LOCKED,
  257. MAX_SCHEDULE_TIMEOUT);
  258. times[1] = ktime_sub(ktime_get_raw(), times[1]);
  259. if (prime == 1)
  260. times[0] = times[1];
  261. if (__igt_timeout(end_time, NULL))
  262. break;
  263. }
  264. err = end_live_test(&t);
  265. if (err)
  266. goto out_unlock;
  267. pr_info("Request latencies on %s: 1 = %lluns, %lu = %lluns\n",
  268. engine->name,
  269. ktime_to_ns(times[0]),
  270. prime, div64_u64(ktime_to_ns(times[1]), prime));
  271. }
  272. out_unlock:
  273. mutex_unlock(&i915->drm.struct_mutex);
  274. return err;
  275. }
  276. int i915_gem_request_live_selftests(struct drm_i915_private *i915)
  277. {
  278. static const struct i915_subtest tests[] = {
  279. SUBTEST(live_nop_request),
  280. };
  281. return i915_subtests(tests, i915);
  282. }