i915_request.c 41 KB

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