i915_request.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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_put(&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,
  239. I915_WAIT_LOCKED,
  240. MAX_SCHEDULE_TIMEOUT);
  241. if (err) {
  242. pr_err("%s(%s): failed to idle before, with err=%d!",
  243. func, name, err);
  244. return err;
  245. }
  246. i915->gpu_error.missed_irq_rings = 0;
  247. t->reset_count = i915_reset_count(&i915->gpu_error);
  248. return 0;
  249. }
  250. static int end_live_test(struct live_test *t)
  251. {
  252. struct drm_i915_private *i915 = t->i915;
  253. i915_retire_requests(i915);
  254. if (wait_for(intel_engines_are_idle(i915), 10)) {
  255. pr_err("%s(%s): GPU not idle\n", t->func, t->name);
  256. return -EIO;
  257. }
  258. if (t->reset_count != i915_reset_count(&i915->gpu_error)) {
  259. pr_err("%s(%s): GPU was reset %d times!\n",
  260. t->func, t->name,
  261. i915_reset_count(&i915->gpu_error) - t->reset_count);
  262. return -EIO;
  263. }
  264. if (i915->gpu_error.missed_irq_rings) {
  265. pr_err("%s(%s): Missed interrupts on engines %lx\n",
  266. t->func, t->name, i915->gpu_error.missed_irq_rings);
  267. return -EIO;
  268. }
  269. return 0;
  270. }
  271. static int live_nop_request(void *arg)
  272. {
  273. struct drm_i915_private *i915 = arg;
  274. struct intel_engine_cs *engine;
  275. struct live_test t;
  276. unsigned int id;
  277. int err = -ENODEV;
  278. /* Submit various sized batches of empty requests, to each engine
  279. * (individually), and wait for the batch to complete. We can check
  280. * the overhead of submitting requests to the hardware.
  281. */
  282. mutex_lock(&i915->drm.struct_mutex);
  283. for_each_engine(engine, i915, id) {
  284. struct i915_request *request = NULL;
  285. unsigned long n, prime;
  286. IGT_TIMEOUT(end_time);
  287. ktime_t times[2] = {};
  288. err = begin_live_test(&t, i915, __func__, engine->name);
  289. if (err)
  290. goto out_unlock;
  291. for_each_prime_number_from(prime, 1, 8192) {
  292. times[1] = ktime_get_raw();
  293. for (n = 0; n < prime; n++) {
  294. request = i915_request_alloc(engine,
  295. i915->kernel_context);
  296. if (IS_ERR(request)) {
  297. err = PTR_ERR(request);
  298. goto out_unlock;
  299. }
  300. /* This space is left intentionally blank.
  301. *
  302. * We do not actually want to perform any
  303. * action with this request, we just want
  304. * to measure the latency in allocation
  305. * and submission of our breadcrumbs -
  306. * ensuring that the bare request is sufficient
  307. * for the system to work (i.e. proper HEAD
  308. * tracking of the rings, interrupt handling,
  309. * etc). It also gives us the lowest bounds
  310. * for latency.
  311. */
  312. i915_request_add(request);
  313. }
  314. i915_request_wait(request,
  315. I915_WAIT_LOCKED,
  316. MAX_SCHEDULE_TIMEOUT);
  317. times[1] = ktime_sub(ktime_get_raw(), times[1]);
  318. if (prime == 1)
  319. times[0] = times[1];
  320. if (__igt_timeout(end_time, NULL))
  321. break;
  322. }
  323. err = end_live_test(&t);
  324. if (err)
  325. goto out_unlock;
  326. pr_info("Request latencies on %s: 1 = %lluns, %lu = %lluns\n",
  327. engine->name,
  328. ktime_to_ns(times[0]),
  329. prime, div64_u64(ktime_to_ns(times[1]), prime));
  330. }
  331. out_unlock:
  332. mutex_unlock(&i915->drm.struct_mutex);
  333. return err;
  334. }
  335. static struct i915_vma *empty_batch(struct drm_i915_private *i915)
  336. {
  337. struct drm_i915_gem_object *obj;
  338. struct i915_vma *vma;
  339. u32 *cmd;
  340. int err;
  341. obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
  342. if (IS_ERR(obj))
  343. return ERR_CAST(obj);
  344. cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
  345. if (IS_ERR(cmd)) {
  346. err = PTR_ERR(cmd);
  347. goto err;
  348. }
  349. *cmd = MI_BATCH_BUFFER_END;
  350. i915_gem_chipset_flush(i915);
  351. i915_gem_object_unpin_map(obj);
  352. err = i915_gem_object_set_to_gtt_domain(obj, false);
  353. if (err)
  354. goto err;
  355. vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
  356. if (IS_ERR(vma)) {
  357. err = PTR_ERR(vma);
  358. goto err;
  359. }
  360. err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_GLOBAL);
  361. if (err)
  362. goto err;
  363. return vma;
  364. err:
  365. i915_gem_object_put(obj);
  366. return ERR_PTR(err);
  367. }
  368. static struct i915_request *
  369. empty_request(struct intel_engine_cs *engine,
  370. struct i915_vma *batch)
  371. {
  372. struct i915_request *request;
  373. int err;
  374. request = i915_request_alloc(engine, engine->i915->kernel_context);
  375. if (IS_ERR(request))
  376. return request;
  377. err = engine->emit_bb_start(request,
  378. batch->node.start,
  379. batch->node.size,
  380. I915_DISPATCH_SECURE);
  381. if (err)
  382. goto out_request;
  383. out_request:
  384. i915_request_add(request);
  385. return err ? ERR_PTR(err) : request;
  386. }
  387. static int live_empty_request(void *arg)
  388. {
  389. struct drm_i915_private *i915 = arg;
  390. struct intel_engine_cs *engine;
  391. struct live_test t;
  392. struct i915_vma *batch;
  393. unsigned int id;
  394. int err = 0;
  395. /* Submit various sized batches of empty requests, to each engine
  396. * (individually), and wait for the batch to complete. We can check
  397. * the overhead of submitting requests to the hardware.
  398. */
  399. mutex_lock(&i915->drm.struct_mutex);
  400. batch = empty_batch(i915);
  401. if (IS_ERR(batch)) {
  402. err = PTR_ERR(batch);
  403. goto out_unlock;
  404. }
  405. for_each_engine(engine, i915, id) {
  406. IGT_TIMEOUT(end_time);
  407. struct i915_request *request;
  408. unsigned long n, prime;
  409. ktime_t times[2] = {};
  410. err = begin_live_test(&t, i915, __func__, engine->name);
  411. if (err)
  412. goto out_batch;
  413. /* Warmup / preload */
  414. request = empty_request(engine, batch);
  415. if (IS_ERR(request)) {
  416. err = PTR_ERR(request);
  417. goto out_batch;
  418. }
  419. i915_request_wait(request,
  420. I915_WAIT_LOCKED,
  421. MAX_SCHEDULE_TIMEOUT);
  422. for_each_prime_number_from(prime, 1, 8192) {
  423. times[1] = ktime_get_raw();
  424. for (n = 0; n < prime; n++) {
  425. request = empty_request(engine, batch);
  426. if (IS_ERR(request)) {
  427. err = PTR_ERR(request);
  428. goto out_batch;
  429. }
  430. }
  431. i915_request_wait(request,
  432. I915_WAIT_LOCKED,
  433. MAX_SCHEDULE_TIMEOUT);
  434. times[1] = ktime_sub(ktime_get_raw(), times[1]);
  435. if (prime == 1)
  436. times[0] = times[1];
  437. if (__igt_timeout(end_time, NULL))
  438. break;
  439. }
  440. err = end_live_test(&t);
  441. if (err)
  442. goto out_batch;
  443. pr_info("Batch latencies on %s: 1 = %lluns, %lu = %lluns\n",
  444. engine->name,
  445. ktime_to_ns(times[0]),
  446. prime, div64_u64(ktime_to_ns(times[1]), prime));
  447. }
  448. out_batch:
  449. i915_vma_unpin(batch);
  450. i915_vma_put(batch);
  451. out_unlock:
  452. mutex_unlock(&i915->drm.struct_mutex);
  453. return err;
  454. }
  455. static struct i915_vma *recursive_batch(struct drm_i915_private *i915)
  456. {
  457. struct i915_gem_context *ctx = i915->kernel_context;
  458. struct i915_address_space *vm =
  459. ctx->ppgtt ? &ctx->ppgtt->vm : &i915->ggtt.vm;
  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_wc_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 {
  492. *cmd++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
  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. err = i915_vma_move_to_active(batch, request[id], 0);
  556. GEM_BUG_ON(err);
  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. err = i915_vma_move_to_active(batch, request[id], 0);
  650. GEM_BUG_ON(err);
  651. i915_gem_object_set_active_reference(batch->obj);
  652. i915_vma_get(batch);
  653. i915_request_get(request[id]);
  654. i915_request_add(request[id]);
  655. prev = request[id];
  656. }
  657. for_each_engine(engine, i915, id) {
  658. long timeout;
  659. if (i915_request_completed(request[id])) {
  660. pr_err("%s(%s): request completed too early!\n",
  661. __func__, engine->name);
  662. err = -EINVAL;
  663. goto out_request;
  664. }
  665. err = recursive_batch_resolve(request[id]->batch);
  666. if (err) {
  667. pr_err("%s: failed to resolve batch, err=%d\n",
  668. __func__, err);
  669. goto out_request;
  670. }
  671. timeout = i915_request_wait(request[id],
  672. I915_WAIT_LOCKED,
  673. MAX_SCHEDULE_TIMEOUT);
  674. if (timeout < 0) {
  675. err = timeout;
  676. pr_err("%s: error waiting for request on %s, err=%d\n",
  677. __func__, engine->name, err);
  678. goto out_request;
  679. }
  680. GEM_BUG_ON(!i915_request_completed(request[id]));
  681. }
  682. err = end_live_test(&t);
  683. out_request:
  684. for_each_engine(engine, i915, id) {
  685. u32 *cmd;
  686. if (!request[id])
  687. break;
  688. cmd = i915_gem_object_pin_map(request[id]->batch->obj,
  689. I915_MAP_WC);
  690. if (!IS_ERR(cmd)) {
  691. *cmd = MI_BATCH_BUFFER_END;
  692. i915_gem_chipset_flush(i915);
  693. i915_gem_object_unpin_map(request[id]->batch->obj);
  694. }
  695. i915_vma_put(request[id]->batch);
  696. i915_request_put(request[id]);
  697. }
  698. out_unlock:
  699. mutex_unlock(&i915->drm.struct_mutex);
  700. return err;
  701. }
  702. int i915_request_live_selftests(struct drm_i915_private *i915)
  703. {
  704. static const struct i915_subtest tests[] = {
  705. SUBTEST(live_nop_request),
  706. SUBTEST(live_all_engines),
  707. SUBTEST(live_sequential_engines),
  708. SUBTEST(live_empty_request),
  709. };
  710. if (i915_terminally_wedged(&i915->gpu_error))
  711. return 0;
  712. return i915_subtests(tests, i915);
  713. }