i915_gem_request.c 39 KB

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