i915_request.c 41 KB

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