i915_gem_request.c 34 KB

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