i915_request.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. IGT_TIMEOUT(end_time);
  283. struct i915_request *request;
  284. unsigned long n, prime;
  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.base, 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, err == 0);
  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 = ctx->ppgtt ? &ctx->ppgtt->base : &i915->ggtt.base;
  457. struct drm_i915_gem_object *obj;
  458. const int gen = INTEL_GEN(i915);
  459. struct i915_vma *vma;
  460. u32 *cmd;
  461. int err;
  462. obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
  463. if (IS_ERR(obj))
  464. return ERR_CAST(obj);
  465. vma = i915_vma_instance(obj, vm, NULL);
  466. if (IS_ERR(vma)) {
  467. err = PTR_ERR(vma);
  468. goto err;
  469. }
  470. err = i915_vma_pin(vma, 0, 0, PIN_USER);
  471. if (err)
  472. goto err;
  473. err = i915_gem_object_set_to_wc_domain(obj, true);
  474. if (err)
  475. goto err;
  476. cmd = i915_gem_object_pin_map(obj, I915_MAP_WC);
  477. if (IS_ERR(cmd)) {
  478. err = PTR_ERR(cmd);
  479. goto err;
  480. }
  481. if (gen >= 8) {
  482. *cmd++ = MI_BATCH_BUFFER_START | 1 << 8 | 1;
  483. *cmd++ = lower_32_bits(vma->node.start);
  484. *cmd++ = upper_32_bits(vma->node.start);
  485. } else if (gen >= 6) {
  486. *cmd++ = MI_BATCH_BUFFER_START | 1 << 8;
  487. *cmd++ = lower_32_bits(vma->node.start);
  488. } else if (gen >= 4) {
  489. *cmd++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
  490. *cmd++ = lower_32_bits(vma->node.start);
  491. } else {
  492. *cmd++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | 1;
  493. *cmd++ = lower_32_bits(vma->node.start);
  494. }
  495. *cmd++ = MI_BATCH_BUFFER_END; /* terminate early in case of error */
  496. i915_gem_chipset_flush(i915);
  497. i915_gem_object_unpin_map(obj);
  498. return vma;
  499. err:
  500. i915_gem_object_put(obj);
  501. return ERR_PTR(err);
  502. }
  503. static int recursive_batch_resolve(struct i915_vma *batch)
  504. {
  505. u32 *cmd;
  506. cmd = i915_gem_object_pin_map(batch->obj, I915_MAP_WC);
  507. if (IS_ERR(cmd))
  508. return PTR_ERR(cmd);
  509. *cmd = MI_BATCH_BUFFER_END;
  510. i915_gem_chipset_flush(batch->vm->i915);
  511. i915_gem_object_unpin_map(batch->obj);
  512. return 0;
  513. }
  514. static int live_all_engines(void *arg)
  515. {
  516. struct drm_i915_private *i915 = arg;
  517. struct intel_engine_cs *engine;
  518. struct i915_request *request[I915_NUM_ENGINES];
  519. struct i915_vma *batch;
  520. struct live_test t;
  521. unsigned int id;
  522. int err;
  523. /* Check we can submit requests to all engines simultaneously. We
  524. * send a recursive batch to each engine - checking that we don't
  525. * block doing so, and that they don't complete too soon.
  526. */
  527. mutex_lock(&i915->drm.struct_mutex);
  528. err = begin_live_test(&t, i915, __func__, "");
  529. if (err)
  530. goto out_unlock;
  531. batch = recursive_batch(i915);
  532. if (IS_ERR(batch)) {
  533. err = PTR_ERR(batch);
  534. pr_err("%s: Unable to create batch, err=%d\n", __func__, err);
  535. goto out_unlock;
  536. }
  537. for_each_engine(engine, i915, id) {
  538. request[id] = i915_request_alloc(engine, i915->kernel_context);
  539. if (IS_ERR(request[id])) {
  540. err = PTR_ERR(request[id]);
  541. pr_err("%s: Request allocation failed with err=%d\n",
  542. __func__, err);
  543. goto out_request;
  544. }
  545. err = engine->emit_bb_start(request[id],
  546. batch->node.start,
  547. batch->node.size,
  548. 0);
  549. GEM_BUG_ON(err);
  550. request[id]->batch = batch;
  551. if (!i915_gem_object_has_active_reference(batch->obj)) {
  552. i915_gem_object_get(batch->obj);
  553. i915_gem_object_set_active_reference(batch->obj);
  554. }
  555. i915_vma_move_to_active(batch, request[id], 0);
  556. i915_request_get(request[id]);
  557. i915_request_add(request[id]);
  558. }
  559. for_each_engine(engine, i915, id) {
  560. if (i915_request_completed(request[id])) {
  561. pr_err("%s(%s): request completed too early!\n",
  562. __func__, engine->name);
  563. err = -EINVAL;
  564. goto out_request;
  565. }
  566. }
  567. err = recursive_batch_resolve(batch);
  568. if (err) {
  569. pr_err("%s: failed to resolve batch, err=%d\n", __func__, err);
  570. goto out_request;
  571. }
  572. for_each_engine(engine, i915, id) {
  573. long timeout;
  574. timeout = i915_request_wait(request[id],
  575. I915_WAIT_LOCKED,
  576. MAX_SCHEDULE_TIMEOUT);
  577. if (timeout < 0) {
  578. err = timeout;
  579. pr_err("%s: error waiting for request on %s, err=%d\n",
  580. __func__, engine->name, err);
  581. goto out_request;
  582. }
  583. GEM_BUG_ON(!i915_request_completed(request[id]));
  584. i915_request_put(request[id]);
  585. request[id] = NULL;
  586. }
  587. err = end_live_test(&t);
  588. out_request:
  589. for_each_engine(engine, i915, id)
  590. if (request[id])
  591. i915_request_put(request[id]);
  592. i915_vma_unpin(batch);
  593. i915_vma_put(batch);
  594. out_unlock:
  595. mutex_unlock(&i915->drm.struct_mutex);
  596. return err;
  597. }
  598. static int live_sequential_engines(void *arg)
  599. {
  600. struct drm_i915_private *i915 = arg;
  601. struct i915_request *request[I915_NUM_ENGINES] = {};
  602. struct i915_request *prev = NULL;
  603. struct intel_engine_cs *engine;
  604. struct live_test t;
  605. unsigned int id;
  606. int err;
  607. /* Check we can submit requests to all engines sequentially, such
  608. * that each successive request waits for the earlier ones. This
  609. * tests that we don't execute requests out of order, even though
  610. * they are running on independent engines.
  611. */
  612. mutex_lock(&i915->drm.struct_mutex);
  613. err = begin_live_test(&t, i915, __func__, "");
  614. if (err)
  615. goto out_unlock;
  616. for_each_engine(engine, i915, id) {
  617. struct i915_vma *batch;
  618. batch = recursive_batch(i915);
  619. if (IS_ERR(batch)) {
  620. err = PTR_ERR(batch);
  621. pr_err("%s: Unable to create batch for %s, err=%d\n",
  622. __func__, engine->name, err);
  623. goto out_unlock;
  624. }
  625. request[id] = i915_request_alloc(engine, i915->kernel_context);
  626. if (IS_ERR(request[id])) {
  627. err = PTR_ERR(request[id]);
  628. pr_err("%s: Request allocation failed for %s with err=%d\n",
  629. __func__, engine->name, err);
  630. goto out_request;
  631. }
  632. if (prev) {
  633. err = i915_request_await_dma_fence(request[id],
  634. &prev->fence);
  635. if (err) {
  636. i915_request_add(request[id]);
  637. pr_err("%s: Request await failed for %s with err=%d\n",
  638. __func__, engine->name, err);
  639. goto out_request;
  640. }
  641. }
  642. err = engine->emit_bb_start(request[id],
  643. batch->node.start,
  644. batch->node.size,
  645. 0);
  646. GEM_BUG_ON(err);
  647. request[id]->batch = batch;
  648. i915_vma_move_to_active(batch, request[id], 0);
  649. i915_gem_object_set_active_reference(batch->obj);
  650. i915_vma_get(batch);
  651. i915_request_get(request[id]);
  652. i915_request_add(request[id]);
  653. prev = request[id];
  654. }
  655. for_each_engine(engine, i915, id) {
  656. long timeout;
  657. if (i915_request_completed(request[id])) {
  658. pr_err("%s(%s): request completed too early!\n",
  659. __func__, engine->name);
  660. err = -EINVAL;
  661. goto out_request;
  662. }
  663. err = recursive_batch_resolve(request[id]->batch);
  664. if (err) {
  665. pr_err("%s: failed to resolve batch, err=%d\n",
  666. __func__, err);
  667. goto out_request;
  668. }
  669. timeout = i915_request_wait(request[id],
  670. I915_WAIT_LOCKED,
  671. MAX_SCHEDULE_TIMEOUT);
  672. if (timeout < 0) {
  673. err = timeout;
  674. pr_err("%s: error waiting for request on %s, err=%d\n",
  675. __func__, engine->name, err);
  676. goto out_request;
  677. }
  678. GEM_BUG_ON(!i915_request_completed(request[id]));
  679. }
  680. err = end_live_test(&t);
  681. out_request:
  682. for_each_engine(engine, i915, id) {
  683. u32 *cmd;
  684. if (!request[id])
  685. break;
  686. cmd = i915_gem_object_pin_map(request[id]->batch->obj,
  687. I915_MAP_WC);
  688. if (!IS_ERR(cmd)) {
  689. *cmd = MI_BATCH_BUFFER_END;
  690. i915_gem_chipset_flush(i915);
  691. i915_gem_object_unpin_map(request[id]->batch->obj);
  692. }
  693. i915_vma_put(request[id]->batch);
  694. i915_request_put(request[id]);
  695. }
  696. out_unlock:
  697. mutex_unlock(&i915->drm.struct_mutex);
  698. return err;
  699. }
  700. int i915_request_live_selftests(struct drm_i915_private *i915)
  701. {
  702. static const struct i915_subtest tests[] = {
  703. SUBTEST(live_nop_request),
  704. SUBTEST(live_all_engines),
  705. SUBTEST(live_sequential_engines),
  706. SUBTEST(live_empty_request),
  707. };
  708. return i915_subtests(tests, i915);
  709. }