i915_request.c 41 KB

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