i915_request.c 42 KB

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