i915_gem_request.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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_context.h"
  27. #include "mock_gem_device.h"
  28. static int igt_add_request(void *arg)
  29. {
  30. struct drm_i915_private *i915 = arg;
  31. struct drm_i915_gem_request *request;
  32. int err = -ENOMEM;
  33. /* Basic preliminary test to create a request and let it loose! */
  34. mutex_lock(&i915->drm.struct_mutex);
  35. request = mock_request(i915->engine[RCS],
  36. i915->kernel_context,
  37. HZ / 10);
  38. if (!request)
  39. goto out_unlock;
  40. i915_add_request(request);
  41. err = 0;
  42. out_unlock:
  43. mutex_unlock(&i915->drm.struct_mutex);
  44. return err;
  45. }
  46. static int igt_wait_request(void *arg)
  47. {
  48. const long T = HZ / 4;
  49. struct drm_i915_private *i915 = arg;
  50. struct drm_i915_gem_request *request;
  51. int err = -EINVAL;
  52. /* Submit a request, then wait upon it */
  53. mutex_lock(&i915->drm.struct_mutex);
  54. request = mock_request(i915->engine[RCS], i915->kernel_context, T);
  55. if (!request) {
  56. err = -ENOMEM;
  57. goto out_unlock;
  58. }
  59. if (i915_wait_request(request, I915_WAIT_LOCKED, 0) != -ETIME) {
  60. pr_err("request wait (busy query) succeeded (expected timeout before submit!)\n");
  61. goto out_unlock;
  62. }
  63. if (i915_wait_request(request, I915_WAIT_LOCKED, T) != -ETIME) {
  64. pr_err("request wait succeeded (expected timeout before submit!)\n");
  65. goto out_unlock;
  66. }
  67. if (i915_gem_request_completed(request)) {
  68. pr_err("request completed before submit!!\n");
  69. goto out_unlock;
  70. }
  71. i915_add_request(request);
  72. if (i915_wait_request(request, I915_WAIT_LOCKED, 0) != -ETIME) {
  73. pr_err("request wait (busy query) succeeded (expected timeout after submit!)\n");
  74. goto out_unlock;
  75. }
  76. if (i915_gem_request_completed(request)) {
  77. pr_err("request completed immediately!\n");
  78. goto out_unlock;
  79. }
  80. if (i915_wait_request(request, I915_WAIT_LOCKED, T / 2) != -ETIME) {
  81. pr_err("request wait succeeded (expected timeout!)\n");
  82. goto out_unlock;
  83. }
  84. if (i915_wait_request(request, I915_WAIT_LOCKED, T) == -ETIME) {
  85. pr_err("request wait timed out!\n");
  86. goto out_unlock;
  87. }
  88. if (!i915_gem_request_completed(request)) {
  89. pr_err("request not complete after waiting!\n");
  90. goto out_unlock;
  91. }
  92. if (i915_wait_request(request, I915_WAIT_LOCKED, T) == -ETIME) {
  93. pr_err("request wait timed out when already complete!\n");
  94. goto out_unlock;
  95. }
  96. err = 0;
  97. out_unlock:
  98. mock_device_flush(i915);
  99. mutex_unlock(&i915->drm.struct_mutex);
  100. return err;
  101. }
  102. static int igt_fence_wait(void *arg)
  103. {
  104. const long T = HZ / 4;
  105. struct drm_i915_private *i915 = arg;
  106. struct drm_i915_gem_request *request;
  107. int err = -EINVAL;
  108. /* Submit a request, treat it as a fence and wait upon it */
  109. mutex_lock(&i915->drm.struct_mutex);
  110. request = mock_request(i915->engine[RCS], i915->kernel_context, T);
  111. if (!request) {
  112. err = -ENOMEM;
  113. goto out_locked;
  114. }
  115. mutex_unlock(&i915->drm.struct_mutex); /* safe as we are single user */
  116. if (dma_fence_wait_timeout(&request->fence, false, T) != -ETIME) {
  117. pr_err("fence wait success before submit (expected timeout)!\n");
  118. goto out_device;
  119. }
  120. mutex_lock(&i915->drm.struct_mutex);
  121. i915_add_request(request);
  122. mutex_unlock(&i915->drm.struct_mutex);
  123. if (dma_fence_is_signaled(&request->fence)) {
  124. pr_err("fence signaled immediately!\n");
  125. goto out_device;
  126. }
  127. if (dma_fence_wait_timeout(&request->fence, false, T / 2) != -ETIME) {
  128. pr_err("fence wait success after submit (expected timeout)!\n");
  129. goto out_device;
  130. }
  131. if (dma_fence_wait_timeout(&request->fence, false, T) <= 0) {
  132. pr_err("fence wait timed out (expected success)!\n");
  133. goto out_device;
  134. }
  135. if (!dma_fence_is_signaled(&request->fence)) {
  136. pr_err("fence unsignaled after waiting!\n");
  137. goto out_device;
  138. }
  139. if (dma_fence_wait_timeout(&request->fence, false, T) <= 0) {
  140. pr_err("fence wait timed out when complete (expected success)!\n");
  141. goto out_device;
  142. }
  143. err = 0;
  144. out_device:
  145. mutex_lock(&i915->drm.struct_mutex);
  146. out_locked:
  147. mock_device_flush(i915);
  148. mutex_unlock(&i915->drm.struct_mutex);
  149. return err;
  150. }
  151. static int igt_request_rewind(void *arg)
  152. {
  153. struct drm_i915_private *i915 = arg;
  154. struct drm_i915_gem_request *request, *vip;
  155. struct i915_gem_context *ctx[2];
  156. int err = -EINVAL;
  157. mutex_lock(&i915->drm.struct_mutex);
  158. ctx[0] = mock_context(i915, "A");
  159. request = mock_request(i915->engine[RCS], ctx[0], 2 * HZ);
  160. if (!request) {
  161. err = -ENOMEM;
  162. goto err_context_0;
  163. }
  164. i915_gem_request_get(request);
  165. i915_add_request(request);
  166. ctx[1] = mock_context(i915, "B");
  167. vip = mock_request(i915->engine[RCS], ctx[1], 0);
  168. if (!vip) {
  169. err = -ENOMEM;
  170. goto err_context_1;
  171. }
  172. /* Simulate preemption by manual reordering */
  173. if (!mock_cancel_request(request)) {
  174. pr_err("failed to cancel request (already executed)!\n");
  175. i915_add_request(vip);
  176. goto err_context_1;
  177. }
  178. i915_gem_request_get(vip);
  179. i915_add_request(vip);
  180. request->engine->submit_request(request);
  181. mutex_unlock(&i915->drm.struct_mutex);
  182. if (i915_wait_request(vip, 0, HZ) == -ETIME) {
  183. pr_err("timed out waiting for high priority request, vip.seqno=%d, current seqno=%d\n",
  184. vip->global_seqno, intel_engine_get_seqno(i915->engine[RCS]));
  185. goto err;
  186. }
  187. if (i915_gem_request_completed(request)) {
  188. pr_err("low priority request already completed\n");
  189. goto err;
  190. }
  191. err = 0;
  192. err:
  193. i915_gem_request_put(vip);
  194. mutex_lock(&i915->drm.struct_mutex);
  195. err_context_1:
  196. mock_context_close(ctx[1]);
  197. i915_gem_request_put(request);
  198. err_context_0:
  199. mock_context_close(ctx[0]);
  200. mock_device_flush(i915);
  201. mutex_unlock(&i915->drm.struct_mutex);
  202. return err;
  203. }
  204. int i915_gem_request_mock_selftests(void)
  205. {
  206. static const struct i915_subtest tests[] = {
  207. SUBTEST(igt_add_request),
  208. SUBTEST(igt_wait_request),
  209. SUBTEST(igt_fence_wait),
  210. SUBTEST(igt_request_rewind),
  211. };
  212. struct drm_i915_private *i915;
  213. int err;
  214. i915 = mock_gem_device();
  215. if (!i915)
  216. return -ENOMEM;
  217. err = i915_subtests(tests, i915);
  218. drm_dev_unref(&i915->drm);
  219. return err;
  220. }
  221. struct live_test {
  222. struct drm_i915_private *i915;
  223. const char *func;
  224. const char *name;
  225. unsigned int reset_count;
  226. };
  227. static int begin_live_test(struct live_test *t,
  228. struct drm_i915_private *i915,
  229. const char *func,
  230. const char *name)
  231. {
  232. int err;
  233. t->i915 = i915;
  234. t->func = func;
  235. t->name = name;
  236. err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
  237. if (err) {
  238. pr_err("%s(%s): failed to idle before, with err=%d!",
  239. func, name, err);
  240. return err;
  241. }
  242. i915->gpu_error.missed_irq_rings = 0;
  243. t->reset_count = i915_reset_count(&i915->gpu_error);
  244. return 0;
  245. }
  246. static int end_live_test(struct live_test *t)
  247. {
  248. struct drm_i915_private *i915 = t->i915;
  249. if (wait_for(intel_engines_are_idle(i915), 1)) {
  250. pr_err("%s(%s): GPU not idle\n", t->func, t->name);
  251. return -EIO;
  252. }
  253. if (t->reset_count != i915_reset_count(&i915->gpu_error)) {
  254. pr_err("%s(%s): GPU was reset %d times!\n",
  255. t->func, t->name,
  256. i915_reset_count(&i915->gpu_error) - t->reset_count);
  257. return -EIO;
  258. }
  259. if (i915->gpu_error.missed_irq_rings) {
  260. pr_err("%s(%s): Missed interrupts on engines %lx\n",
  261. t->func, t->name, i915->gpu_error.missed_irq_rings);
  262. return -EIO;
  263. }
  264. return 0;
  265. }
  266. static int live_nop_request(void *arg)
  267. {
  268. struct drm_i915_private *i915 = arg;
  269. struct intel_engine_cs *engine;
  270. struct live_test t;
  271. unsigned int id;
  272. int err;
  273. /* Submit various sized batches of empty requests, to each engine
  274. * (individually), and wait for the batch to complete. We can check
  275. * the overhead of submitting requests to the hardware.
  276. */
  277. mutex_lock(&i915->drm.struct_mutex);
  278. for_each_engine(engine, i915, id) {
  279. IGT_TIMEOUT(end_time);
  280. struct drm_i915_gem_request *request;
  281. unsigned long n, prime;
  282. ktime_t times[2] = {};
  283. err = begin_live_test(&t, i915, __func__, engine->name);
  284. if (err)
  285. goto out_unlock;
  286. for_each_prime_number_from(prime, 1, 8192) {
  287. times[1] = ktime_get_raw();
  288. for (n = 0; n < prime; n++) {
  289. request = i915_gem_request_alloc(engine,
  290. i915->kernel_context);
  291. if (IS_ERR(request)) {
  292. err = PTR_ERR(request);
  293. goto out_unlock;
  294. }
  295. /* This space is left intentionally blank.
  296. *
  297. * We do not actually want to perform any
  298. * action with this request, we just want
  299. * to measure the latency in allocation
  300. * and submission of our breadcrumbs -
  301. * ensuring that the bare request is sufficient
  302. * for the system to work (i.e. proper HEAD
  303. * tracking of the rings, interrupt handling,
  304. * etc). It also gives us the lowest bounds
  305. * for latency.
  306. */
  307. i915_add_request(request);
  308. }
  309. i915_wait_request(request,
  310. I915_WAIT_LOCKED,
  311. MAX_SCHEDULE_TIMEOUT);
  312. times[1] = ktime_sub(ktime_get_raw(), times[1]);
  313. if (prime == 1)
  314. times[0] = times[1];
  315. if (__igt_timeout(end_time, NULL))
  316. break;
  317. }
  318. err = end_live_test(&t);
  319. if (err)
  320. goto out_unlock;
  321. pr_info("Request latencies on %s: 1 = %lluns, %lu = %lluns\n",
  322. engine->name,
  323. ktime_to_ns(times[0]),
  324. prime, div64_u64(ktime_to_ns(times[1]), prime));
  325. }
  326. out_unlock:
  327. mutex_unlock(&i915->drm.struct_mutex);
  328. return err;
  329. }
  330. static struct i915_vma *empty_batch(struct drm_i915_private *i915)
  331. {
  332. struct drm_i915_gem_object *obj;
  333. struct i915_vma *vma;
  334. u32 *cmd;
  335. int err;
  336. obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
  337. if (IS_ERR(obj))
  338. return ERR_CAST(obj);
  339. cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
  340. if (IS_ERR(cmd)) {
  341. err = PTR_ERR(cmd);
  342. goto err;
  343. }
  344. *cmd = MI_BATCH_BUFFER_END;
  345. i915_gem_object_unpin_map(obj);
  346. err = i915_gem_object_set_to_gtt_domain(obj, false);
  347. if (err)
  348. goto err;
  349. vma = i915_vma_instance(obj, &i915->ggtt.base, NULL);
  350. if (IS_ERR(vma)) {
  351. err = PTR_ERR(vma);
  352. goto err;
  353. }
  354. err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_GLOBAL);
  355. if (err)
  356. goto err;
  357. return vma;
  358. err:
  359. i915_gem_object_put(obj);
  360. return ERR_PTR(err);
  361. }
  362. static struct drm_i915_gem_request *
  363. empty_request(struct intel_engine_cs *engine,
  364. struct i915_vma *batch)
  365. {
  366. struct drm_i915_gem_request *request;
  367. int err;
  368. request = i915_gem_request_alloc(engine,
  369. engine->i915->kernel_context);
  370. if (IS_ERR(request))
  371. return request;
  372. err = engine->emit_flush(request, EMIT_INVALIDATE);
  373. if (err)
  374. goto out_request;
  375. err = i915_switch_context(request);
  376. if (err)
  377. goto out_request;
  378. err = engine->emit_bb_start(request,
  379. batch->node.start,
  380. batch->node.size,
  381. I915_DISPATCH_SECURE);
  382. if (err)
  383. goto out_request;
  384. out_request:
  385. __i915_add_request(request, err == 0);
  386. return err ? ERR_PTR(err) : request;
  387. }
  388. static int live_empty_request(void *arg)
  389. {
  390. struct drm_i915_private *i915 = arg;
  391. struct intel_engine_cs *engine;
  392. struct live_test t;
  393. struct i915_vma *batch;
  394. unsigned int id;
  395. int err = 0;
  396. /* Submit various sized batches of empty requests, to each engine
  397. * (individually), and wait for the batch to complete. We can check
  398. * the overhead of submitting requests to the hardware.
  399. */
  400. mutex_lock(&i915->drm.struct_mutex);
  401. batch = empty_batch(i915);
  402. if (IS_ERR(batch)) {
  403. err = PTR_ERR(batch);
  404. goto out_unlock;
  405. }
  406. for_each_engine(engine, i915, id) {
  407. IGT_TIMEOUT(end_time);
  408. struct drm_i915_gem_request *request;
  409. unsigned long n, prime;
  410. ktime_t times[2] = {};
  411. err = begin_live_test(&t, i915, __func__, engine->name);
  412. if (err)
  413. goto out_batch;
  414. /* Warmup / preload */
  415. request = empty_request(engine, batch);
  416. if (IS_ERR(request)) {
  417. err = PTR_ERR(request);
  418. goto out_batch;
  419. }
  420. i915_wait_request(request,
  421. I915_WAIT_LOCKED,
  422. MAX_SCHEDULE_TIMEOUT);
  423. for_each_prime_number_from(prime, 1, 8192) {
  424. times[1] = ktime_get_raw();
  425. for (n = 0; n < prime; n++) {
  426. request = empty_request(engine, batch);
  427. if (IS_ERR(request)) {
  428. err = PTR_ERR(request);
  429. goto out_batch;
  430. }
  431. }
  432. i915_wait_request(request,
  433. I915_WAIT_LOCKED,
  434. MAX_SCHEDULE_TIMEOUT);
  435. times[1] = ktime_sub(ktime_get_raw(), times[1]);
  436. if (prime == 1)
  437. times[0] = times[1];
  438. if (__igt_timeout(end_time, NULL))
  439. break;
  440. }
  441. err = end_live_test(&t);
  442. if (err)
  443. goto out_batch;
  444. pr_info("Batch latencies on %s: 1 = %lluns, %lu = %lluns\n",
  445. engine->name,
  446. ktime_to_ns(times[0]),
  447. prime, div64_u64(ktime_to_ns(times[1]), prime));
  448. }
  449. out_batch:
  450. i915_vma_unpin(batch);
  451. i915_vma_put(batch);
  452. out_unlock:
  453. mutex_unlock(&i915->drm.struct_mutex);
  454. return err;
  455. }
  456. static struct i915_vma *recursive_batch(struct drm_i915_private *i915)
  457. {
  458. struct i915_gem_context *ctx = i915->kernel_context;
  459. struct i915_address_space *vm = ctx->ppgtt ? &ctx->ppgtt->base : &i915->ggtt.base;
  460. struct drm_i915_gem_object *obj;
  461. const int gen = INTEL_GEN(i915);
  462. struct i915_vma *vma;
  463. u32 *cmd;
  464. int err;
  465. obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
  466. if (IS_ERR(obj))
  467. return ERR_CAST(obj);
  468. vma = i915_vma_instance(obj, vm, NULL);
  469. if (IS_ERR(vma)) {
  470. err = PTR_ERR(vma);
  471. goto err;
  472. }
  473. err = i915_vma_pin(vma, 0, 0, PIN_USER);
  474. if (err)
  475. goto err;
  476. err = i915_gem_object_set_to_gtt_domain(obj, true);
  477. if (err)
  478. goto err;
  479. cmd = i915_gem_object_pin_map(obj, I915_MAP_WC);
  480. if (IS_ERR(cmd)) {
  481. err = PTR_ERR(cmd);
  482. goto err;
  483. }
  484. if (gen >= 8) {
  485. *cmd++ = MI_BATCH_BUFFER_START | 1 << 8 | 1;
  486. *cmd++ = lower_32_bits(vma->node.start);
  487. *cmd++ = upper_32_bits(vma->node.start);
  488. } else if (gen >= 6) {
  489. *cmd++ = MI_BATCH_BUFFER_START | 1 << 8;
  490. *cmd++ = lower_32_bits(vma->node.start);
  491. } else if (gen >= 4) {
  492. *cmd++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
  493. *cmd++ = lower_32_bits(vma->node.start);
  494. } else {
  495. *cmd++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | 1;
  496. *cmd++ = lower_32_bits(vma->node.start);
  497. }
  498. *cmd++ = MI_BATCH_BUFFER_END; /* terminate early in case of error */
  499. wmb();
  500. i915_gem_object_unpin_map(obj);
  501. return vma;
  502. err:
  503. i915_gem_object_put(obj);
  504. return ERR_PTR(err);
  505. }
  506. static int recursive_batch_resolve(struct i915_vma *batch)
  507. {
  508. u32 *cmd;
  509. cmd = i915_gem_object_pin_map(batch->obj, I915_MAP_WC);
  510. if (IS_ERR(cmd))
  511. return PTR_ERR(cmd);
  512. *cmd = MI_BATCH_BUFFER_END;
  513. wmb();
  514. i915_gem_object_unpin_map(batch->obj);
  515. return 0;
  516. }
  517. static int live_all_engines(void *arg)
  518. {
  519. struct drm_i915_private *i915 = arg;
  520. struct intel_engine_cs *engine;
  521. struct drm_i915_gem_request *request[I915_NUM_ENGINES];
  522. struct i915_vma *batch;
  523. struct live_test t;
  524. unsigned int id;
  525. int err;
  526. /* Check we can submit requests to all engines simultaneously. We
  527. * send a recursive batch to each engine - checking that we don't
  528. * block doing so, and that they don't complete too soon.
  529. */
  530. mutex_lock(&i915->drm.struct_mutex);
  531. err = begin_live_test(&t, i915, __func__, "");
  532. if (err)
  533. goto out_unlock;
  534. batch = recursive_batch(i915);
  535. if (IS_ERR(batch)) {
  536. err = PTR_ERR(batch);
  537. pr_err("%s: Unable to create batch, err=%d\n", __func__, err);
  538. goto out_unlock;
  539. }
  540. for_each_engine(engine, i915, id) {
  541. request[id] = i915_gem_request_alloc(engine,
  542. i915->kernel_context);
  543. if (IS_ERR(request[id])) {
  544. err = PTR_ERR(request[id]);
  545. pr_err("%s: Request allocation failed with err=%d\n",
  546. __func__, err);
  547. goto out_request;
  548. }
  549. err = engine->emit_flush(request[id], EMIT_INVALIDATE);
  550. GEM_BUG_ON(err);
  551. err = i915_switch_context(request[id]);
  552. GEM_BUG_ON(err);
  553. err = engine->emit_bb_start(request[id],
  554. batch->node.start,
  555. batch->node.size,
  556. 0);
  557. GEM_BUG_ON(err);
  558. request[id]->batch = batch;
  559. if (!i915_gem_object_has_active_reference(batch->obj)) {
  560. i915_gem_object_get(batch->obj);
  561. i915_gem_object_set_active_reference(batch->obj);
  562. }
  563. i915_vma_move_to_active(batch, request[id], 0);
  564. i915_gem_request_get(request[id]);
  565. i915_add_request(request[id]);
  566. }
  567. for_each_engine(engine, i915, id) {
  568. if (i915_gem_request_completed(request[id])) {
  569. pr_err("%s(%s): request completed too early!\n",
  570. __func__, engine->name);
  571. err = -EINVAL;
  572. goto out_request;
  573. }
  574. }
  575. err = recursive_batch_resolve(batch);
  576. if (err) {
  577. pr_err("%s: failed to resolve batch, err=%d\n", __func__, err);
  578. goto out_request;
  579. }
  580. for_each_engine(engine, i915, id) {
  581. long timeout;
  582. timeout = i915_wait_request(request[id],
  583. I915_WAIT_LOCKED,
  584. MAX_SCHEDULE_TIMEOUT);
  585. if (timeout < 0) {
  586. err = timeout;
  587. pr_err("%s: error waiting for request on %s, err=%d\n",
  588. __func__, engine->name, err);
  589. goto out_request;
  590. }
  591. GEM_BUG_ON(!i915_gem_request_completed(request[id]));
  592. i915_gem_request_put(request[id]);
  593. request[id] = NULL;
  594. }
  595. err = end_live_test(&t);
  596. out_request:
  597. for_each_engine(engine, i915, id)
  598. if (request[id])
  599. i915_gem_request_put(request[id]);
  600. i915_vma_unpin(batch);
  601. i915_vma_put(batch);
  602. out_unlock:
  603. mutex_unlock(&i915->drm.struct_mutex);
  604. return err;
  605. }
  606. static int live_sequential_engines(void *arg)
  607. {
  608. struct drm_i915_private *i915 = arg;
  609. struct drm_i915_gem_request *request[I915_NUM_ENGINES] = {};
  610. struct drm_i915_gem_request *prev = NULL;
  611. struct intel_engine_cs *engine;
  612. struct live_test t;
  613. unsigned int id;
  614. int err;
  615. /* Check we can submit requests to all engines sequentially, such
  616. * that each successive request waits for the earlier ones. This
  617. * tests that we don't execute requests out of order, even though
  618. * they are running on independent engines.
  619. */
  620. mutex_lock(&i915->drm.struct_mutex);
  621. err = begin_live_test(&t, i915, __func__, "");
  622. if (err)
  623. goto out_unlock;
  624. for_each_engine(engine, i915, id) {
  625. struct i915_vma *batch;
  626. batch = recursive_batch(i915);
  627. if (IS_ERR(batch)) {
  628. err = PTR_ERR(batch);
  629. pr_err("%s: Unable to create batch for %s, err=%d\n",
  630. __func__, engine->name, err);
  631. goto out_unlock;
  632. }
  633. request[id] = i915_gem_request_alloc(engine,
  634. i915->kernel_context);
  635. if (IS_ERR(request[id])) {
  636. err = PTR_ERR(request[id]);
  637. pr_err("%s: Request allocation failed for %s with err=%d\n",
  638. __func__, engine->name, err);
  639. goto out_request;
  640. }
  641. if (prev) {
  642. err = i915_gem_request_await_dma_fence(request[id],
  643. &prev->fence);
  644. if (err) {
  645. i915_add_request(request[id]);
  646. pr_err("%s: Request await failed for %s with err=%d\n",
  647. __func__, engine->name, err);
  648. goto out_request;
  649. }
  650. }
  651. err = engine->emit_flush(request[id], EMIT_INVALIDATE);
  652. GEM_BUG_ON(err);
  653. err = i915_switch_context(request[id]);
  654. GEM_BUG_ON(err);
  655. err = engine->emit_bb_start(request[id],
  656. batch->node.start,
  657. batch->node.size,
  658. 0);
  659. GEM_BUG_ON(err);
  660. request[id]->batch = batch;
  661. i915_vma_move_to_active(batch, request[id], 0);
  662. i915_gem_object_set_active_reference(batch->obj);
  663. i915_vma_get(batch);
  664. i915_gem_request_get(request[id]);
  665. i915_add_request(request[id]);
  666. prev = request[id];
  667. }
  668. for_each_engine(engine, i915, id) {
  669. long timeout;
  670. if (i915_gem_request_completed(request[id])) {
  671. pr_err("%s(%s): request completed too early!\n",
  672. __func__, engine->name);
  673. err = -EINVAL;
  674. goto out_request;
  675. }
  676. err = recursive_batch_resolve(request[id]->batch);
  677. if (err) {
  678. pr_err("%s: failed to resolve batch, err=%d\n",
  679. __func__, err);
  680. goto out_request;
  681. }
  682. timeout = i915_wait_request(request[id],
  683. I915_WAIT_LOCKED,
  684. MAX_SCHEDULE_TIMEOUT);
  685. if (timeout < 0) {
  686. err = timeout;
  687. pr_err("%s: error waiting for request on %s, err=%d\n",
  688. __func__, engine->name, err);
  689. goto out_request;
  690. }
  691. GEM_BUG_ON(!i915_gem_request_completed(request[id]));
  692. }
  693. err = end_live_test(&t);
  694. out_request:
  695. for_each_engine(engine, i915, id) {
  696. u32 *cmd;
  697. if (!request[id])
  698. break;
  699. cmd = i915_gem_object_pin_map(request[id]->batch->obj,
  700. I915_MAP_WC);
  701. if (!IS_ERR(cmd)) {
  702. *cmd = MI_BATCH_BUFFER_END;
  703. wmb();
  704. i915_gem_object_unpin_map(request[id]->batch->obj);
  705. }
  706. i915_vma_put(request[id]->batch);
  707. i915_gem_request_put(request[id]);
  708. }
  709. out_unlock:
  710. mutex_unlock(&i915->drm.struct_mutex);
  711. return err;
  712. }
  713. int i915_gem_request_live_selftests(struct drm_i915_private *i915)
  714. {
  715. static const struct i915_subtest tests[] = {
  716. SUBTEST(live_nop_request),
  717. SUBTEST(live_all_engines),
  718. SUBTEST(live_sequential_engines),
  719. SUBTEST(live_empty_request),
  720. };
  721. return i915_subtests(tests, i915);
  722. }