i915_gem_request.c 39 KB

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