i915_gem_request.c 37 KB

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