i915_gem_request.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /*
  2. * Copyright © 2008-2015 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/prefetch.h>
  25. #include <linux/dma-fence-array.h>
  26. #include <linux/sched.h>
  27. #include <linux/sched/clock.h>
  28. #include <linux/sched/signal.h>
  29. #include "i915_drv.h"
  30. static const char *i915_fence_get_driver_name(struct dma_fence *fence)
  31. {
  32. return "i915";
  33. }
  34. static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
  35. {
  36. /* The timeline struct (as part of the ppgtt underneath a context)
  37. * may be freed when the request is no longer in use by the GPU.
  38. * We could extend the life of a context to beyond that of all
  39. * fences, possibly keeping the hw resource around indefinitely,
  40. * or we just give them a false name. Since
  41. * dma_fence_ops.get_timeline_name is a debug feature, the occasional
  42. * lie seems justifiable.
  43. */
  44. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  45. return "signaled";
  46. return to_request(fence)->timeline->common->name;
  47. }
  48. static bool i915_fence_signaled(struct dma_fence *fence)
  49. {
  50. return i915_gem_request_completed(to_request(fence));
  51. }
  52. static bool i915_fence_enable_signaling(struct dma_fence *fence)
  53. {
  54. if (i915_fence_signaled(fence))
  55. return false;
  56. intel_engine_enable_signaling(to_request(fence), true);
  57. return !i915_fence_signaled(fence);
  58. }
  59. static signed long i915_fence_wait(struct dma_fence *fence,
  60. bool interruptible,
  61. signed long timeout)
  62. {
  63. return i915_wait_request(to_request(fence), interruptible, timeout);
  64. }
  65. static void i915_fence_release(struct dma_fence *fence)
  66. {
  67. struct drm_i915_gem_request *req = to_request(fence);
  68. /* The request is put onto a RCU freelist (i.e. the address
  69. * is immediately reused), mark the fences as being freed now.
  70. * Otherwise the debugobjects for the fences are only marked as
  71. * freed when the slab cache itself is freed, and so we would get
  72. * caught trying to reuse dead objects.
  73. */
  74. i915_sw_fence_fini(&req->submit);
  75. kmem_cache_free(req->i915->requests, req);
  76. }
  77. const struct dma_fence_ops i915_fence_ops = {
  78. .get_driver_name = i915_fence_get_driver_name,
  79. .get_timeline_name = i915_fence_get_timeline_name,
  80. .enable_signaling = i915_fence_enable_signaling,
  81. .signaled = i915_fence_signaled,
  82. .wait = i915_fence_wait,
  83. .release = i915_fence_release,
  84. };
  85. static inline void
  86. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  87. {
  88. struct drm_i915_file_private *file_priv;
  89. file_priv = request->file_priv;
  90. if (!file_priv)
  91. return;
  92. spin_lock(&file_priv->mm.lock);
  93. if (request->file_priv) {
  94. list_del(&request->client_link);
  95. request->file_priv = NULL;
  96. }
  97. spin_unlock(&file_priv->mm.lock);
  98. }
  99. static struct i915_dependency *
  100. i915_dependency_alloc(struct drm_i915_private *i915)
  101. {
  102. return kmem_cache_alloc(i915->dependencies, GFP_KERNEL);
  103. }
  104. static void
  105. i915_dependency_free(struct drm_i915_private *i915,
  106. struct i915_dependency *dep)
  107. {
  108. kmem_cache_free(i915->dependencies, dep);
  109. }
  110. static void
  111. __i915_priotree_add_dependency(struct i915_priotree *pt,
  112. struct i915_priotree *signal,
  113. struct i915_dependency *dep,
  114. unsigned long flags)
  115. {
  116. INIT_LIST_HEAD(&dep->dfs_link);
  117. list_add(&dep->wait_link, &signal->waiters_list);
  118. list_add(&dep->signal_link, &pt->signalers_list);
  119. dep->signaler = signal;
  120. dep->flags = flags;
  121. }
  122. static int
  123. i915_priotree_add_dependency(struct drm_i915_private *i915,
  124. struct i915_priotree *pt,
  125. struct i915_priotree *signal)
  126. {
  127. struct i915_dependency *dep;
  128. dep = i915_dependency_alloc(i915);
  129. if (!dep)
  130. return -ENOMEM;
  131. __i915_priotree_add_dependency(pt, signal, dep, I915_DEPENDENCY_ALLOC);
  132. return 0;
  133. }
  134. static void
  135. i915_priotree_fini(struct drm_i915_private *i915, struct i915_priotree *pt)
  136. {
  137. struct i915_dependency *dep, *next;
  138. GEM_BUG_ON(!list_empty(&pt->link));
  139. /* Everyone we depended upon (the fences we wait to be signaled)
  140. * should retire before us and remove themselves from our list.
  141. * However, retirement is run independently on each timeline and
  142. * so we may be called out-of-order.
  143. */
  144. list_for_each_entry_safe(dep, next, &pt->signalers_list, signal_link) {
  145. list_del(&dep->wait_link);
  146. if (dep->flags & I915_DEPENDENCY_ALLOC)
  147. i915_dependency_free(i915, dep);
  148. }
  149. /* Remove ourselves from everyone who depends upon us */
  150. list_for_each_entry_safe(dep, next, &pt->waiters_list, wait_link) {
  151. list_del(&dep->signal_link);
  152. if (dep->flags & I915_DEPENDENCY_ALLOC)
  153. i915_dependency_free(i915, dep);
  154. }
  155. }
  156. static void
  157. i915_priotree_init(struct i915_priotree *pt)
  158. {
  159. INIT_LIST_HEAD(&pt->signalers_list);
  160. INIT_LIST_HEAD(&pt->waiters_list);
  161. INIT_LIST_HEAD(&pt->link);
  162. pt->priority = I915_PRIORITY_INVALID;
  163. }
  164. static int reset_all_global_seqno(struct drm_i915_private *i915, u32 seqno)
  165. {
  166. struct intel_engine_cs *engine;
  167. enum intel_engine_id id;
  168. int ret;
  169. /* Carefully retire all requests without writing to the rings */
  170. ret = i915_gem_wait_for_idle(i915,
  171. I915_WAIT_INTERRUPTIBLE |
  172. I915_WAIT_LOCKED);
  173. if (ret)
  174. return ret;
  175. /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
  176. for_each_engine(engine, i915, id) {
  177. struct i915_gem_timeline *timeline;
  178. struct intel_timeline *tl = engine->timeline;
  179. if (!i915_seqno_passed(seqno, tl->seqno)) {
  180. /* spin until threads are complete */
  181. while (intel_breadcrumbs_busy(engine))
  182. cond_resched();
  183. }
  184. /* Check we are idle before we fiddle with hw state! */
  185. GEM_BUG_ON(!intel_engine_is_idle(engine));
  186. GEM_BUG_ON(i915_gem_active_isset(&engine->timeline->last_request));
  187. /* Finally reset hw state */
  188. intel_engine_init_global_seqno(engine, seqno);
  189. tl->seqno = seqno;
  190. list_for_each_entry(timeline, &i915->gt.timelines, link)
  191. memset(timeline->engine[id].global_sync, 0,
  192. sizeof(timeline->engine[id].global_sync));
  193. }
  194. return 0;
  195. }
  196. int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
  197. {
  198. struct drm_i915_private *dev_priv = to_i915(dev);
  199. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  200. if (seqno == 0)
  201. return -EINVAL;
  202. /* HWS page needs to be set less than what we
  203. * will inject to ring
  204. */
  205. return reset_all_global_seqno(dev_priv, seqno - 1);
  206. }
  207. static void mark_busy(struct drm_i915_private *i915)
  208. {
  209. if (i915->gt.awake)
  210. return;
  211. GEM_BUG_ON(!i915->gt.active_requests);
  212. intel_runtime_pm_get_noresume(i915);
  213. i915->gt.awake = true;
  214. intel_enable_gt_powersave(i915);
  215. i915_update_gfx_val(i915);
  216. if (INTEL_GEN(i915) >= 6)
  217. gen6_rps_busy(i915);
  218. intel_engines_unpark(i915);
  219. queue_delayed_work(i915->wq,
  220. &i915->gt.retire_work,
  221. round_jiffies_up_relative(HZ));
  222. }
  223. static int reserve_engine(struct intel_engine_cs *engine)
  224. {
  225. struct drm_i915_private *i915 = engine->i915;
  226. u32 active = ++engine->timeline->inflight_seqnos;
  227. u32 seqno = engine->timeline->seqno;
  228. int ret;
  229. /* Reservation is fine until we need to wrap around */
  230. if (unlikely(add_overflows(seqno, active))) {
  231. ret = reset_all_global_seqno(i915, 0);
  232. if (ret) {
  233. engine->timeline->inflight_seqnos--;
  234. return ret;
  235. }
  236. }
  237. if (!i915->gt.active_requests++)
  238. mark_busy(i915);
  239. return 0;
  240. }
  241. static void unreserve_engine(struct intel_engine_cs *engine)
  242. {
  243. struct drm_i915_private *i915 = engine->i915;
  244. if (!--i915->gt.active_requests) {
  245. /* Cancel the mark_busy() from our reserve_engine() */
  246. GEM_BUG_ON(!i915->gt.awake);
  247. mod_delayed_work(i915->wq,
  248. &i915->gt.idle_work,
  249. msecs_to_jiffies(100));
  250. }
  251. GEM_BUG_ON(!engine->timeline->inflight_seqnos);
  252. engine->timeline->inflight_seqnos--;
  253. }
  254. void i915_gem_retire_noop(struct i915_gem_active *active,
  255. struct drm_i915_gem_request *request)
  256. {
  257. /* Space left intentionally blank */
  258. }
  259. static void advance_ring(struct drm_i915_gem_request *request)
  260. {
  261. unsigned int tail;
  262. /* We know the GPU must have read the request to have
  263. * sent us the seqno + interrupt, so use the position
  264. * of tail of the request to update the last known position
  265. * of the GPU head.
  266. *
  267. * Note this requires that we are always called in request
  268. * completion order.
  269. */
  270. if (list_is_last(&request->ring_link, &request->ring->request_list)) {
  271. /* We may race here with execlists resubmitting this request
  272. * as we retire it. The resubmission will move the ring->tail
  273. * forwards (to request->wa_tail). We either read the
  274. * current value that was written to hw, or the value that
  275. * is just about to be. Either works, if we miss the last two
  276. * noops - they are safe to be replayed on a reset.
  277. */
  278. tail = READ_ONCE(request->ring->tail);
  279. } else {
  280. tail = request->postfix;
  281. }
  282. list_del(&request->ring_link);
  283. request->ring->head = tail;
  284. }
  285. static void free_capture_list(struct drm_i915_gem_request *request)
  286. {
  287. struct i915_gem_capture_list *capture;
  288. capture = request->capture_list;
  289. while (capture) {
  290. struct i915_gem_capture_list *next = capture->next;
  291. kfree(capture);
  292. capture = next;
  293. }
  294. }
  295. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  296. {
  297. struct intel_engine_cs *engine = request->engine;
  298. struct i915_gem_active *active, *next;
  299. lockdep_assert_held(&request->i915->drm.struct_mutex);
  300. GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit));
  301. GEM_BUG_ON(!i915_gem_request_completed(request));
  302. GEM_BUG_ON(!request->i915->gt.active_requests);
  303. trace_i915_gem_request_retire(request);
  304. spin_lock_irq(&engine->timeline->lock);
  305. list_del_init(&request->link);
  306. spin_unlock_irq(&engine->timeline->lock);
  307. unreserve_engine(request->engine);
  308. advance_ring(request);
  309. free_capture_list(request);
  310. /* Walk through the active list, calling retire on each. This allows
  311. * objects to track their GPU activity and mark themselves as idle
  312. * when their *last* active request is completed (updating state
  313. * tracking lists for eviction, active references for GEM, etc).
  314. *
  315. * As the ->retire() may free the node, we decouple it first and
  316. * pass along the auxiliary information (to avoid dereferencing
  317. * the node after the callback).
  318. */
  319. list_for_each_entry_safe(active, next, &request->active_list, link) {
  320. /* In microbenchmarks or focusing upon time inside the kernel,
  321. * we may spend an inordinate amount of time simply handling
  322. * the retirement of requests and processing their callbacks.
  323. * Of which, this loop itself is particularly hot due to the
  324. * cache misses when jumping around the list of i915_gem_active.
  325. * So we try to keep this loop as streamlined as possible and
  326. * also prefetch the next i915_gem_active to try and hide
  327. * the likely cache miss.
  328. */
  329. prefetchw(next);
  330. INIT_LIST_HEAD(&active->link);
  331. RCU_INIT_POINTER(active->request, NULL);
  332. active->retire(active, request);
  333. }
  334. i915_gem_request_remove_from_client(request);
  335. /* Retirement decays the ban score as it is a sign of ctx progress */
  336. atomic_dec_if_positive(&request->ctx->ban_score);
  337. /* The backing object for the context is done after switching to the
  338. * *next* context. Therefore we cannot retire the previous context until
  339. * the next context has already started running. However, since we
  340. * cannot take the required locks at i915_gem_request_submit() we
  341. * defer the unpinning of the active context to now, retirement of
  342. * the subsequent request.
  343. */
  344. if (engine->last_retired_context)
  345. engine->context_unpin(engine, engine->last_retired_context);
  346. engine->last_retired_context = request->ctx;
  347. spin_lock_irq(&request->lock);
  348. if (request->waitboost)
  349. atomic_dec(&request->i915->gt_pm.rps.num_waiters);
  350. dma_fence_signal_locked(&request->fence);
  351. spin_unlock_irq(&request->lock);
  352. i915_priotree_fini(request->i915, &request->priotree);
  353. i915_gem_request_put(request);
  354. }
  355. void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
  356. {
  357. struct intel_engine_cs *engine = req->engine;
  358. struct drm_i915_gem_request *tmp;
  359. lockdep_assert_held(&req->i915->drm.struct_mutex);
  360. GEM_BUG_ON(!i915_gem_request_completed(req));
  361. if (list_empty(&req->link))
  362. return;
  363. do {
  364. tmp = list_first_entry(&engine->timeline->requests,
  365. typeof(*tmp), link);
  366. i915_gem_request_retire(tmp);
  367. } while (tmp != req);
  368. }
  369. static u32 timeline_get_seqno(struct intel_timeline *tl)
  370. {
  371. return ++tl->seqno;
  372. }
  373. void __i915_gem_request_submit(struct drm_i915_gem_request *request)
  374. {
  375. struct intel_engine_cs *engine = request->engine;
  376. struct intel_timeline *timeline;
  377. u32 seqno;
  378. GEM_BUG_ON(!irqs_disabled());
  379. lockdep_assert_held(&engine->timeline->lock);
  380. trace_i915_gem_request_execute(request);
  381. /* Transfer from per-context onto the global per-engine timeline */
  382. timeline = engine->timeline;
  383. GEM_BUG_ON(timeline == request->timeline);
  384. seqno = timeline_get_seqno(timeline);
  385. GEM_BUG_ON(!seqno);
  386. GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
  387. /* We may be recursing from the signal callback of another i915 fence */
  388. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  389. request->global_seqno = seqno;
  390. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  391. intel_engine_enable_signaling(request, false);
  392. spin_unlock(&request->lock);
  393. engine->emit_breadcrumb(request,
  394. request->ring->vaddr + request->postfix);
  395. spin_lock(&request->timeline->lock);
  396. list_move_tail(&request->link, &timeline->requests);
  397. spin_unlock(&request->timeline->lock);
  398. wake_up_all(&request->execute);
  399. }
  400. void i915_gem_request_submit(struct drm_i915_gem_request *request)
  401. {
  402. struct intel_engine_cs *engine = request->engine;
  403. unsigned long flags;
  404. /* Will be called from irq-context when using foreign fences. */
  405. spin_lock_irqsave(&engine->timeline->lock, flags);
  406. __i915_gem_request_submit(request);
  407. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  408. }
  409. void __i915_gem_request_unsubmit(struct drm_i915_gem_request *request)
  410. {
  411. struct intel_engine_cs *engine = request->engine;
  412. struct intel_timeline *timeline;
  413. GEM_BUG_ON(!irqs_disabled());
  414. lockdep_assert_held(&engine->timeline->lock);
  415. /* Only unwind in reverse order, required so that the per-context list
  416. * is kept in seqno/ring order.
  417. */
  418. GEM_BUG_ON(request->global_seqno != engine->timeline->seqno);
  419. engine->timeline->seqno--;
  420. /* We may be recursing from the signal callback of another i915 fence */
  421. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  422. request->global_seqno = 0;
  423. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  424. intel_engine_cancel_signaling(request);
  425. spin_unlock(&request->lock);
  426. /* Transfer back from the global per-engine timeline to per-context */
  427. timeline = request->timeline;
  428. GEM_BUG_ON(timeline == engine->timeline);
  429. spin_lock(&timeline->lock);
  430. list_move(&request->link, &timeline->requests);
  431. spin_unlock(&timeline->lock);
  432. /* We don't need to wake_up any waiters on request->execute, they
  433. * will get woken by any other event or us re-adding this request
  434. * to the engine timeline (__i915_gem_request_submit()). The waiters
  435. * should be quite adapt at finding that the request now has a new
  436. * global_seqno to the one they went to sleep on.
  437. */
  438. }
  439. void i915_gem_request_unsubmit(struct drm_i915_gem_request *request)
  440. {
  441. struct intel_engine_cs *engine = request->engine;
  442. unsigned long flags;
  443. /* Will be called from irq-context when using foreign fences. */
  444. spin_lock_irqsave(&engine->timeline->lock, flags);
  445. __i915_gem_request_unsubmit(request);
  446. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  447. }
  448. static int __i915_sw_fence_call
  449. submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
  450. {
  451. struct drm_i915_gem_request *request =
  452. container_of(fence, typeof(*request), submit);
  453. switch (state) {
  454. case FENCE_COMPLETE:
  455. trace_i915_gem_request_submit(request);
  456. /*
  457. * We need to serialize use of the submit_request() callback with its
  458. * hotplugging performed during an emergency i915_gem_set_wedged().
  459. * We use the RCU mechanism to mark the critical section in order to
  460. * force i915_gem_set_wedged() to wait until the submit_request() is
  461. * completed before proceeding.
  462. */
  463. rcu_read_lock();
  464. request->engine->submit_request(request);
  465. rcu_read_unlock();
  466. break;
  467. case FENCE_FREE:
  468. i915_gem_request_put(request);
  469. break;
  470. }
  471. return NOTIFY_DONE;
  472. }
  473. /**
  474. * i915_gem_request_alloc - allocate a request structure
  475. *
  476. * @engine: engine that we wish to issue the request on.
  477. * @ctx: context that the request will be associated with.
  478. *
  479. * Returns a pointer to the allocated request if successful,
  480. * or an error code if not.
  481. */
  482. struct drm_i915_gem_request *
  483. i915_gem_request_alloc(struct intel_engine_cs *engine,
  484. struct i915_gem_context *ctx)
  485. {
  486. struct drm_i915_private *dev_priv = engine->i915;
  487. struct drm_i915_gem_request *req;
  488. struct intel_ring *ring;
  489. int ret;
  490. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  491. /*
  492. * Preempt contexts are reserved for exclusive use to inject a
  493. * preemption context switch. They are never to be used for any trivial
  494. * request!
  495. */
  496. GEM_BUG_ON(ctx == dev_priv->preempt_context);
  497. /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  498. * EIO if the GPU is already wedged.
  499. */
  500. if (i915_terminally_wedged(&dev_priv->gpu_error))
  501. return ERR_PTR(-EIO);
  502. /* Pinning the contexts may generate requests in order to acquire
  503. * GGTT space, so do this first before we reserve a seqno for
  504. * ourselves.
  505. */
  506. ring = engine->context_pin(engine, ctx);
  507. if (IS_ERR(ring))
  508. return ERR_CAST(ring);
  509. GEM_BUG_ON(!ring);
  510. ret = reserve_engine(engine);
  511. if (ret)
  512. goto err_unpin;
  513. /* Move the oldest request to the slab-cache (if not in use!) */
  514. req = list_first_entry_or_null(&engine->timeline->requests,
  515. typeof(*req), link);
  516. if (req && i915_gem_request_completed(req))
  517. i915_gem_request_retire(req);
  518. /* Beware: Dragons be flying overhead.
  519. *
  520. * We use RCU to look up requests in flight. The lookups may
  521. * race with the request being allocated from the slab freelist.
  522. * That is the request we are writing to here, may be in the process
  523. * of being read by __i915_gem_active_get_rcu(). As such,
  524. * we have to be very careful when overwriting the contents. During
  525. * the RCU lookup, we change chase the request->engine pointer,
  526. * read the request->global_seqno and increment the reference count.
  527. *
  528. * The reference count is incremented atomically. If it is zero,
  529. * the lookup knows the request is unallocated and complete. Otherwise,
  530. * it is either still in use, or has been reallocated and reset
  531. * with dma_fence_init(). This increment is safe for release as we
  532. * check that the request we have a reference to and matches the active
  533. * request.
  534. *
  535. * Before we increment the refcount, we chase the request->engine
  536. * pointer. We must not call kmem_cache_zalloc() or else we set
  537. * that pointer to NULL and cause a crash during the lookup. If
  538. * we see the request is completed (based on the value of the
  539. * old engine and seqno), the lookup is complete and reports NULL.
  540. * If we decide the request is not completed (new engine or seqno),
  541. * then we grab a reference and double check that it is still the
  542. * active request - which it won't be and restart the lookup.
  543. *
  544. * Do not use kmem_cache_zalloc() here!
  545. */
  546. req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
  547. if (!req) {
  548. ret = -ENOMEM;
  549. goto err_unreserve;
  550. }
  551. req->timeline = i915_gem_context_lookup_timeline(ctx, engine);
  552. GEM_BUG_ON(req->timeline == engine->timeline);
  553. spin_lock_init(&req->lock);
  554. dma_fence_init(&req->fence,
  555. &i915_fence_ops,
  556. &req->lock,
  557. req->timeline->fence_context,
  558. timeline_get_seqno(req->timeline));
  559. /* We bump the ref for the fence chain */
  560. i915_sw_fence_init(&i915_gem_request_get(req)->submit, submit_notify);
  561. init_waitqueue_head(&req->execute);
  562. i915_priotree_init(&req->priotree);
  563. INIT_LIST_HEAD(&req->active_list);
  564. req->i915 = dev_priv;
  565. req->engine = engine;
  566. req->ctx = ctx;
  567. req->ring = ring;
  568. /* No zalloc, must clear what we need by hand */
  569. req->global_seqno = 0;
  570. req->file_priv = NULL;
  571. req->batch = NULL;
  572. req->capture_list = NULL;
  573. req->waitboost = false;
  574. /*
  575. * Reserve space in the ring buffer for all the commands required to
  576. * eventually emit this request. This is to guarantee that the
  577. * i915_add_request() call can't fail. Note that the reserve may need
  578. * to be redone if the request is not actually submitted straight
  579. * away, e.g. because a GPU scheduler has deferred it.
  580. */
  581. req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  582. GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
  583. ret = engine->request_alloc(req);
  584. if (ret)
  585. goto err_ctx;
  586. /* Record the position of the start of the request so that
  587. * should we detect the updated seqno part-way through the
  588. * GPU processing the request, we never over-estimate the
  589. * position of the head.
  590. */
  591. req->head = req->ring->emit;
  592. /* Check that we didn't interrupt ourselves with a new request */
  593. GEM_BUG_ON(req->timeline->seqno != req->fence.seqno);
  594. return req;
  595. err_ctx:
  596. /* Make sure we didn't add ourselves to external state before freeing */
  597. GEM_BUG_ON(!list_empty(&req->active_list));
  598. GEM_BUG_ON(!list_empty(&req->priotree.signalers_list));
  599. GEM_BUG_ON(!list_empty(&req->priotree.waiters_list));
  600. kmem_cache_free(dev_priv->requests, req);
  601. err_unreserve:
  602. unreserve_engine(engine);
  603. err_unpin:
  604. engine->context_unpin(engine, ctx);
  605. return ERR_PTR(ret);
  606. }
  607. static int
  608. i915_gem_request_await_request(struct drm_i915_gem_request *to,
  609. struct drm_i915_gem_request *from)
  610. {
  611. int ret;
  612. GEM_BUG_ON(to == from);
  613. GEM_BUG_ON(to->timeline == from->timeline);
  614. if (i915_gem_request_completed(from))
  615. return 0;
  616. if (to->engine->schedule) {
  617. ret = i915_priotree_add_dependency(to->i915,
  618. &to->priotree,
  619. &from->priotree);
  620. if (ret < 0)
  621. return ret;
  622. }
  623. if (to->engine == from->engine) {
  624. ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
  625. &from->submit,
  626. GFP_KERNEL);
  627. return ret < 0 ? ret : 0;
  628. }
  629. if (to->engine->semaphore.sync_to) {
  630. u32 seqno;
  631. GEM_BUG_ON(!from->engine->semaphore.signal);
  632. seqno = i915_gem_request_global_seqno(from);
  633. if (!seqno)
  634. goto await_dma_fence;
  635. if (seqno <= to->timeline->global_sync[from->engine->id])
  636. return 0;
  637. trace_i915_gem_ring_sync_to(to, from);
  638. ret = to->engine->semaphore.sync_to(to, from);
  639. if (ret)
  640. return ret;
  641. to->timeline->global_sync[from->engine->id] = seqno;
  642. return 0;
  643. }
  644. await_dma_fence:
  645. ret = i915_sw_fence_await_dma_fence(&to->submit,
  646. &from->fence, 0,
  647. GFP_KERNEL);
  648. return ret < 0 ? ret : 0;
  649. }
  650. int
  651. i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
  652. struct dma_fence *fence)
  653. {
  654. struct dma_fence **child = &fence;
  655. unsigned int nchild = 1;
  656. int ret;
  657. /* Note that if the fence-array was created in signal-on-any mode,
  658. * we should *not* decompose it into its individual fences. However,
  659. * we don't currently store which mode the fence-array is operating
  660. * in. Fortunately, the only user of signal-on-any is private to
  661. * amdgpu and we should not see any incoming fence-array from
  662. * sync-file being in signal-on-any mode.
  663. */
  664. if (dma_fence_is_array(fence)) {
  665. struct dma_fence_array *array = to_dma_fence_array(fence);
  666. child = array->fences;
  667. nchild = array->num_fences;
  668. GEM_BUG_ON(!nchild);
  669. }
  670. do {
  671. fence = *child++;
  672. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  673. continue;
  674. /*
  675. * Requests on the same timeline are explicitly ordered, along
  676. * with their dependencies, by i915_add_request() which ensures
  677. * that requests are submitted in-order through each ring.
  678. */
  679. if (fence->context == req->fence.context)
  680. continue;
  681. /* Squash repeated waits to the same timelines */
  682. if (fence->context != req->i915->mm.unordered_timeline &&
  683. intel_timeline_sync_is_later(req->timeline, fence))
  684. continue;
  685. if (dma_fence_is_i915(fence))
  686. ret = i915_gem_request_await_request(req,
  687. to_request(fence));
  688. else
  689. ret = i915_sw_fence_await_dma_fence(&req->submit, fence,
  690. I915_FENCE_TIMEOUT,
  691. GFP_KERNEL);
  692. if (ret < 0)
  693. return ret;
  694. /* Record the latest fence used against each timeline */
  695. if (fence->context != req->i915->mm.unordered_timeline)
  696. intel_timeline_sync_set(req->timeline, fence);
  697. } while (--nchild);
  698. return 0;
  699. }
  700. /**
  701. * i915_gem_request_await_object - set this request to (async) wait upon a bo
  702. *
  703. * @to: request we are wishing to use
  704. * @obj: object which may be in use on another ring.
  705. *
  706. * This code is meant to abstract object synchronization with the GPU.
  707. * Conceptually we serialise writes between engines inside the GPU.
  708. * We only allow one engine to write into a buffer at any time, but
  709. * multiple readers. To ensure each has a coherent view of memory, we must:
  710. *
  711. * - If there is an outstanding write request to the object, the new
  712. * request must wait for it to complete (either CPU or in hw, requests
  713. * on the same ring will be naturally ordered).
  714. *
  715. * - If we are a write request (pending_write_domain is set), the new
  716. * request must wait for outstanding read requests to complete.
  717. *
  718. * Returns 0 if successful, else propagates up the lower layer error.
  719. */
  720. int
  721. i915_gem_request_await_object(struct drm_i915_gem_request *to,
  722. struct drm_i915_gem_object *obj,
  723. bool write)
  724. {
  725. struct dma_fence *excl;
  726. int ret = 0;
  727. if (write) {
  728. struct dma_fence **shared;
  729. unsigned int count, i;
  730. ret = reservation_object_get_fences_rcu(obj->resv,
  731. &excl, &count, &shared);
  732. if (ret)
  733. return ret;
  734. for (i = 0; i < count; i++) {
  735. ret = i915_gem_request_await_dma_fence(to, shared[i]);
  736. if (ret)
  737. break;
  738. dma_fence_put(shared[i]);
  739. }
  740. for (; i < count; i++)
  741. dma_fence_put(shared[i]);
  742. kfree(shared);
  743. } else {
  744. excl = reservation_object_get_excl_rcu(obj->resv);
  745. }
  746. if (excl) {
  747. if (ret == 0)
  748. ret = i915_gem_request_await_dma_fence(to, excl);
  749. dma_fence_put(excl);
  750. }
  751. return ret;
  752. }
  753. /*
  754. * NB: This function is not allowed to fail. Doing so would mean the the
  755. * request is not being tracked for completion but the work itself is
  756. * going to happen on the hardware. This would be a Bad Thing(tm).
  757. */
  758. void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
  759. {
  760. struct intel_engine_cs *engine = request->engine;
  761. struct intel_ring *ring = request->ring;
  762. struct intel_timeline *timeline = request->timeline;
  763. struct drm_i915_gem_request *prev;
  764. u32 *cs;
  765. int err;
  766. lockdep_assert_held(&request->i915->drm.struct_mutex);
  767. trace_i915_gem_request_add(request);
  768. /* Make sure that no request gazumped us - if it was allocated after
  769. * our i915_gem_request_alloc() and called __i915_add_request() before
  770. * us, the timeline will hold its seqno which is later than ours.
  771. */
  772. GEM_BUG_ON(timeline->seqno != request->fence.seqno);
  773. /*
  774. * To ensure that this call will not fail, space for its emissions
  775. * should already have been reserved in the ring buffer. Let the ring
  776. * know that it is time to use that space up.
  777. */
  778. request->reserved_space = 0;
  779. /*
  780. * Emit any outstanding flushes - execbuf can fail to emit the flush
  781. * after having emitted the batchbuffer command. Hence we need to fix
  782. * things up similar to emitting the lazy request. The difference here
  783. * is that the flush _must_ happen before the next request, no matter
  784. * what.
  785. */
  786. if (flush_caches) {
  787. err = engine->emit_flush(request, EMIT_FLUSH);
  788. /* Not allowed to fail! */
  789. WARN(err, "engine->emit_flush() failed: %d!\n", err);
  790. }
  791. /* Record the position of the start of the breadcrumb so that
  792. * should we detect the updated seqno part-way through the
  793. * GPU processing the request, we never over-estimate the
  794. * position of the ring's HEAD.
  795. */
  796. cs = intel_ring_begin(request, engine->emit_breadcrumb_sz);
  797. GEM_BUG_ON(IS_ERR(cs));
  798. request->postfix = intel_ring_offset(request, cs);
  799. /* Seal the request and mark it as pending execution. Note that
  800. * we may inspect this state, without holding any locks, during
  801. * hangcheck. Hence we apply the barrier to ensure that we do not
  802. * see a more recent value in the hws than we are tracking.
  803. */
  804. prev = i915_gem_active_raw(&timeline->last_request,
  805. &request->i915->drm.struct_mutex);
  806. if (prev) {
  807. i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
  808. &request->submitq);
  809. if (engine->schedule)
  810. __i915_priotree_add_dependency(&request->priotree,
  811. &prev->priotree,
  812. &request->dep,
  813. 0);
  814. }
  815. spin_lock_irq(&timeline->lock);
  816. list_add_tail(&request->link, &timeline->requests);
  817. spin_unlock_irq(&timeline->lock);
  818. GEM_BUG_ON(timeline->seqno != request->fence.seqno);
  819. i915_gem_active_set(&timeline->last_request, request);
  820. list_add_tail(&request->ring_link, &ring->request_list);
  821. request->emitted_jiffies = jiffies;
  822. /* Let the backend know a new request has arrived that may need
  823. * to adjust the existing execution schedule due to a high priority
  824. * request - i.e. we may want to preempt the current request in order
  825. * to run a high priority dependency chain *before* we can execute this
  826. * request.
  827. *
  828. * This is called before the request is ready to run so that we can
  829. * decide whether to preempt the entire chain so that it is ready to
  830. * run at the earliest possible convenience.
  831. */
  832. if (engine->schedule)
  833. engine->schedule(request, request->ctx->priority);
  834. local_bh_disable();
  835. i915_sw_fence_commit(&request->submit);
  836. local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
  837. }
  838. static unsigned long local_clock_us(unsigned int *cpu)
  839. {
  840. unsigned long t;
  841. /* Cheaply and approximately convert from nanoseconds to microseconds.
  842. * The result and subsequent calculations are also defined in the same
  843. * approximate microseconds units. The principal source of timing
  844. * error here is from the simple truncation.
  845. *
  846. * Note that local_clock() is only defined wrt to the current CPU;
  847. * the comparisons are no longer valid if we switch CPUs. Instead of
  848. * blocking preemption for the entire busywait, we can detect the CPU
  849. * switch and use that as indicator of system load and a reason to
  850. * stop busywaiting, see busywait_stop().
  851. */
  852. *cpu = get_cpu();
  853. t = local_clock() >> 10;
  854. put_cpu();
  855. return t;
  856. }
  857. static bool busywait_stop(unsigned long timeout, unsigned int cpu)
  858. {
  859. unsigned int this_cpu;
  860. if (time_after(local_clock_us(&this_cpu), timeout))
  861. return true;
  862. return this_cpu != cpu;
  863. }
  864. static bool __i915_spin_request(const struct drm_i915_gem_request *req,
  865. u32 seqno, int state, unsigned long timeout_us)
  866. {
  867. struct intel_engine_cs *engine = req->engine;
  868. unsigned int irq, cpu;
  869. GEM_BUG_ON(!seqno);
  870. /*
  871. * Only wait for the request if we know it is likely to complete.
  872. *
  873. * We don't track the timestamps around requests, nor the average
  874. * request length, so we do not have a good indicator that this
  875. * request will complete within the timeout. What we do know is the
  876. * order in which requests are executed by the engine and so we can
  877. * tell if the request has started. If the request hasn't started yet,
  878. * it is a fair assumption that it will not complete within our
  879. * relatively short timeout.
  880. */
  881. if (!i915_seqno_passed(intel_engine_get_seqno(engine), seqno - 1))
  882. return false;
  883. /* When waiting for high frequency requests, e.g. during synchronous
  884. * rendering split between the CPU and GPU, the finite amount of time
  885. * required to set up the irq and wait upon it limits the response
  886. * rate. By busywaiting on the request completion for a short while we
  887. * can service the high frequency waits as quick as possible. However,
  888. * if it is a slow request, we want to sleep as quickly as possible.
  889. * The tradeoff between waiting and sleeping is roughly the time it
  890. * takes to sleep on a request, on the order of a microsecond.
  891. */
  892. irq = atomic_read(&engine->irq_count);
  893. timeout_us += local_clock_us(&cpu);
  894. do {
  895. if (i915_seqno_passed(intel_engine_get_seqno(engine), seqno))
  896. return seqno == i915_gem_request_global_seqno(req);
  897. /* Seqno are meant to be ordered *before* the interrupt. If
  898. * we see an interrupt without a corresponding seqno advance,
  899. * assume we won't see one in the near future but require
  900. * the engine->seqno_barrier() to fixup coherency.
  901. */
  902. if (atomic_read(&engine->irq_count) != irq)
  903. break;
  904. if (signal_pending_state(state, current))
  905. break;
  906. if (busywait_stop(timeout_us, cpu))
  907. break;
  908. cpu_relax();
  909. } while (!need_resched());
  910. return false;
  911. }
  912. static bool __i915_wait_request_check_and_reset(struct drm_i915_gem_request *request)
  913. {
  914. if (likely(!i915_reset_handoff(&request->i915->gpu_error)))
  915. return false;
  916. __set_current_state(TASK_RUNNING);
  917. i915_reset(request->i915, 0);
  918. return true;
  919. }
  920. /**
  921. * i915_wait_request - wait until execution of request has finished
  922. * @req: the request to wait upon
  923. * @flags: how to wait
  924. * @timeout: how long to wait in jiffies
  925. *
  926. * i915_wait_request() waits for the request to be completed, for a
  927. * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
  928. * unbounded wait).
  929. *
  930. * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
  931. * in via the flags, and vice versa if the struct_mutex is not held, the caller
  932. * must not specify that the wait is locked.
  933. *
  934. * Returns the remaining time (in jiffies) if the request completed, which may
  935. * be zero or -ETIME if the request is unfinished after the timeout expires.
  936. * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
  937. * pending before the request completes.
  938. */
  939. long i915_wait_request(struct drm_i915_gem_request *req,
  940. unsigned int flags,
  941. long timeout)
  942. {
  943. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  944. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  945. wait_queue_head_t *errq = &req->i915->gpu_error.wait_queue;
  946. DEFINE_WAIT_FUNC(reset, default_wake_function);
  947. DEFINE_WAIT_FUNC(exec, default_wake_function);
  948. struct intel_wait wait;
  949. might_sleep();
  950. #if IS_ENABLED(CONFIG_LOCKDEP)
  951. GEM_BUG_ON(debug_locks &&
  952. !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
  953. !!(flags & I915_WAIT_LOCKED));
  954. #endif
  955. GEM_BUG_ON(timeout < 0);
  956. if (i915_gem_request_completed(req))
  957. return timeout;
  958. if (!timeout)
  959. return -ETIME;
  960. trace_i915_gem_request_wait_begin(req, flags);
  961. add_wait_queue(&req->execute, &exec);
  962. if (flags & I915_WAIT_LOCKED)
  963. add_wait_queue(errq, &reset);
  964. intel_wait_init(&wait, req);
  965. restart:
  966. do {
  967. set_current_state(state);
  968. if (intel_wait_update_request(&wait, req))
  969. break;
  970. if (flags & I915_WAIT_LOCKED &&
  971. __i915_wait_request_check_and_reset(req))
  972. continue;
  973. if (signal_pending_state(state, current)) {
  974. timeout = -ERESTARTSYS;
  975. goto complete;
  976. }
  977. if (!timeout) {
  978. timeout = -ETIME;
  979. goto complete;
  980. }
  981. timeout = io_schedule_timeout(timeout);
  982. } while (1);
  983. GEM_BUG_ON(!intel_wait_has_seqno(&wait));
  984. GEM_BUG_ON(!i915_sw_fence_signaled(&req->submit));
  985. /* Optimistic short spin before touching IRQs */
  986. if (__i915_spin_request(req, wait.seqno, state, 5))
  987. goto complete;
  988. set_current_state(state);
  989. if (intel_engine_add_wait(req->engine, &wait))
  990. /* In order to check that we haven't missed the interrupt
  991. * as we enabled it, we need to kick ourselves to do a
  992. * coherent check on the seqno before we sleep.
  993. */
  994. goto wakeup;
  995. if (flags & I915_WAIT_LOCKED)
  996. __i915_wait_request_check_and_reset(req);
  997. for (;;) {
  998. if (signal_pending_state(state, current)) {
  999. timeout = -ERESTARTSYS;
  1000. break;
  1001. }
  1002. if (!timeout) {
  1003. timeout = -ETIME;
  1004. break;
  1005. }
  1006. timeout = io_schedule_timeout(timeout);
  1007. if (intel_wait_complete(&wait) &&
  1008. intel_wait_check_request(&wait, req))
  1009. break;
  1010. set_current_state(state);
  1011. wakeup:
  1012. /* Carefully check if the request is complete, giving time
  1013. * for the seqno to be visible following the interrupt.
  1014. * We also have to check in case we are kicked by the GPU
  1015. * reset in order to drop the struct_mutex.
  1016. */
  1017. if (__i915_request_irq_complete(req))
  1018. break;
  1019. /* If the GPU is hung, and we hold the lock, reset the GPU
  1020. * and then check for completion. On a full reset, the engine's
  1021. * HW seqno will be advanced passed us and we are complete.
  1022. * If we do a partial reset, we have to wait for the GPU to
  1023. * resume and update the breadcrumb.
  1024. *
  1025. * If we don't hold the mutex, we can just wait for the worker
  1026. * to come along and update the breadcrumb (either directly
  1027. * itself, or indirectly by recovering the GPU).
  1028. */
  1029. if (flags & I915_WAIT_LOCKED &&
  1030. __i915_wait_request_check_and_reset(req))
  1031. continue;
  1032. /* Only spin if we know the GPU is processing this request */
  1033. if (__i915_spin_request(req, wait.seqno, state, 2))
  1034. break;
  1035. if (!intel_wait_check_request(&wait, req)) {
  1036. intel_engine_remove_wait(req->engine, &wait);
  1037. goto restart;
  1038. }
  1039. }
  1040. intel_engine_remove_wait(req->engine, &wait);
  1041. complete:
  1042. __set_current_state(TASK_RUNNING);
  1043. if (flags & I915_WAIT_LOCKED)
  1044. remove_wait_queue(errq, &reset);
  1045. remove_wait_queue(&req->execute, &exec);
  1046. trace_i915_gem_request_wait_end(req);
  1047. return timeout;
  1048. }
  1049. static void engine_retire_requests(struct intel_engine_cs *engine)
  1050. {
  1051. struct drm_i915_gem_request *request, *next;
  1052. u32 seqno = intel_engine_get_seqno(engine);
  1053. LIST_HEAD(retire);
  1054. spin_lock_irq(&engine->timeline->lock);
  1055. list_for_each_entry_safe(request, next,
  1056. &engine->timeline->requests, link) {
  1057. if (!i915_seqno_passed(seqno, request->global_seqno))
  1058. break;
  1059. list_move_tail(&request->link, &retire);
  1060. }
  1061. spin_unlock_irq(&engine->timeline->lock);
  1062. list_for_each_entry_safe(request, next, &retire, link)
  1063. i915_gem_request_retire(request);
  1064. }
  1065. void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
  1066. {
  1067. struct intel_engine_cs *engine;
  1068. enum intel_engine_id id;
  1069. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  1070. if (!dev_priv->gt.active_requests)
  1071. return;
  1072. for_each_engine(engine, dev_priv, id)
  1073. engine_retire_requests(engine);
  1074. }
  1075. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1076. #include "selftests/mock_request.c"
  1077. #include "selftests/i915_gem_request.c"
  1078. #endif