i915_gem_request.c 39 KB

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