i915_gem_request.c 35 KB

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