i915_gem_request.c 42 KB

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