i915_request.c 20 KB

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