i915_gem_request.c 34 KB

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