i915_gem_request.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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_check_wedge(struct drm_i915_private *dev_priv)
  258. {
  259. struct i915_gpu_error *error = &dev_priv->gpu_error;
  260. if (i915_terminally_wedged(error))
  261. return -EIO;
  262. if (i915_reset_in_progress(error)) {
  263. /* Non-interruptible callers can't handle -EAGAIN, hence return
  264. * -EIO unconditionally for these.
  265. */
  266. if (!dev_priv->mm.interruptible)
  267. return -EIO;
  268. return -EAGAIN;
  269. }
  270. return 0;
  271. }
  272. static int i915_gem_init_global_seqno(struct drm_i915_private *i915, u32 seqno)
  273. {
  274. struct i915_gem_timeline *timeline = &i915->gt.global_timeline;
  275. struct intel_engine_cs *engine;
  276. enum intel_engine_id id;
  277. int ret;
  278. /* Carefully retire all requests without writing to the rings */
  279. ret = i915_gem_wait_for_idle(i915,
  280. I915_WAIT_INTERRUPTIBLE |
  281. I915_WAIT_LOCKED);
  282. if (ret)
  283. return ret;
  284. i915_gem_retire_requests(i915);
  285. GEM_BUG_ON(i915->gt.active_requests > 1);
  286. /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
  287. if (!i915_seqno_passed(seqno, atomic_read(&timeline->seqno))) {
  288. while (intel_breadcrumbs_busy(i915))
  289. cond_resched(); /* spin until threads are complete */
  290. }
  291. atomic_set(&timeline->seqno, seqno);
  292. /* Finally reset hw state */
  293. for_each_engine(engine, i915, id)
  294. intel_engine_init_global_seqno(engine, seqno);
  295. list_for_each_entry(timeline, &i915->gt.timelines, link) {
  296. for_each_engine(engine, i915, id) {
  297. struct intel_timeline *tl = &timeline->engine[id];
  298. memset(tl->sync_seqno, 0, sizeof(tl->sync_seqno));
  299. }
  300. }
  301. return 0;
  302. }
  303. int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
  304. {
  305. struct drm_i915_private *dev_priv = to_i915(dev);
  306. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  307. if (seqno == 0)
  308. return -EINVAL;
  309. /* HWS page needs to be set less than what we
  310. * will inject to ring
  311. */
  312. return i915_gem_init_global_seqno(dev_priv, seqno - 1);
  313. }
  314. static int reserve_global_seqno(struct drm_i915_private *i915)
  315. {
  316. u32 active_requests = ++i915->gt.active_requests;
  317. u32 seqno = atomic_read(&i915->gt.global_timeline.seqno);
  318. int ret;
  319. /* Reservation is fine until we need to wrap around */
  320. if (likely(seqno + active_requests > seqno))
  321. return 0;
  322. ret = i915_gem_init_global_seqno(i915, 0);
  323. if (ret) {
  324. i915->gt.active_requests--;
  325. return ret;
  326. }
  327. return 0;
  328. }
  329. static u32 __timeline_get_seqno(struct i915_gem_timeline *tl)
  330. {
  331. /* seqno only incremented under a mutex */
  332. return ++tl->seqno.counter;
  333. }
  334. static u32 timeline_get_seqno(struct i915_gem_timeline *tl)
  335. {
  336. return atomic_inc_return(&tl->seqno);
  337. }
  338. void __i915_gem_request_submit(struct drm_i915_gem_request *request)
  339. {
  340. struct intel_engine_cs *engine = request->engine;
  341. struct intel_timeline *timeline;
  342. u32 seqno;
  343. /* Transfer from per-context onto the global per-engine timeline */
  344. timeline = engine->timeline;
  345. GEM_BUG_ON(timeline == request->timeline);
  346. assert_spin_locked(&timeline->lock);
  347. seqno = timeline_get_seqno(timeline->common);
  348. GEM_BUG_ON(!seqno);
  349. GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
  350. GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno, seqno));
  351. request->previous_seqno = timeline->last_submitted_seqno;
  352. timeline->last_submitted_seqno = seqno;
  353. /* We may be recursing from the signal callback of another i915 fence */
  354. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  355. request->global_seqno = seqno;
  356. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  357. intel_engine_enable_signaling(request);
  358. spin_unlock(&request->lock);
  359. GEM_BUG_ON(!request->global_seqno);
  360. engine->emit_breadcrumb(request,
  361. request->ring->vaddr + request->postfix);
  362. spin_lock(&request->timeline->lock);
  363. list_move_tail(&request->link, &timeline->requests);
  364. spin_unlock(&request->timeline->lock);
  365. i915_sw_fence_commit(&request->execute);
  366. }
  367. void i915_gem_request_submit(struct drm_i915_gem_request *request)
  368. {
  369. struct intel_engine_cs *engine = request->engine;
  370. unsigned long flags;
  371. /* Will be called from irq-context when using foreign fences. */
  372. spin_lock_irqsave(&engine->timeline->lock, flags);
  373. __i915_gem_request_submit(request);
  374. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  375. }
  376. static int __i915_sw_fence_call
  377. submit_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), submit);
  381. switch (state) {
  382. case FENCE_COMPLETE:
  383. request->engine->submit_request(request);
  384. break;
  385. case FENCE_FREE:
  386. i915_gem_request_put(request);
  387. break;
  388. }
  389. return NOTIFY_DONE;
  390. }
  391. static int __i915_sw_fence_call
  392. execute_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
  393. {
  394. struct drm_i915_gem_request *request =
  395. container_of(fence, typeof(*request), execute);
  396. switch (state) {
  397. case FENCE_COMPLETE:
  398. break;
  399. case FENCE_FREE:
  400. i915_gem_request_put(request);
  401. break;
  402. }
  403. return NOTIFY_DONE;
  404. }
  405. /**
  406. * i915_gem_request_alloc - allocate a request structure
  407. *
  408. * @engine: engine that we wish to issue the request on.
  409. * @ctx: context that the request will be associated with.
  410. * This can be NULL if the request is not directly related to
  411. * any specific user context, in which case this function will
  412. * choose an appropriate context to use.
  413. *
  414. * Returns a pointer to the allocated request if successful,
  415. * or an error code if not.
  416. */
  417. struct drm_i915_gem_request *
  418. i915_gem_request_alloc(struct intel_engine_cs *engine,
  419. struct i915_gem_context *ctx)
  420. {
  421. struct drm_i915_private *dev_priv = engine->i915;
  422. struct drm_i915_gem_request *req;
  423. int ret;
  424. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  425. /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  426. * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
  427. * and restart.
  428. */
  429. ret = i915_gem_check_wedge(dev_priv);
  430. if (ret)
  431. return ERR_PTR(ret);
  432. /* Pinning the contexts may generate requests in order to acquire
  433. * GGTT space, so do this first before we reserve a seqno for
  434. * ourselves.
  435. */
  436. ret = engine->context_pin(engine, ctx);
  437. if (ret)
  438. return ERR_PTR(ret);
  439. ret = reserve_global_seqno(dev_priv);
  440. if (ret)
  441. goto err_unpin;
  442. /* Move the oldest request to the slab-cache (if not in use!) */
  443. req = list_first_entry_or_null(&engine->timeline->requests,
  444. typeof(*req), link);
  445. if (req && __i915_gem_request_completed(req))
  446. i915_gem_request_retire(req);
  447. /* Beware: Dragons be flying overhead.
  448. *
  449. * We use RCU to look up requests in flight. The lookups may
  450. * race with the request being allocated from the slab freelist.
  451. * That is the request we are writing to here, may be in the process
  452. * of being read by __i915_gem_active_get_rcu(). As such,
  453. * we have to be very careful when overwriting the contents. During
  454. * the RCU lookup, we change chase the request->engine pointer,
  455. * read the request->global_seqno and increment the reference count.
  456. *
  457. * The reference count is incremented atomically. If it is zero,
  458. * the lookup knows the request is unallocated and complete. Otherwise,
  459. * it is either still in use, or has been reallocated and reset
  460. * with dma_fence_init(). This increment is safe for release as we
  461. * check that the request we have a reference to and matches the active
  462. * request.
  463. *
  464. * Before we increment the refcount, we chase the request->engine
  465. * pointer. We must not call kmem_cache_zalloc() or else we set
  466. * that pointer to NULL and cause a crash during the lookup. If
  467. * we see the request is completed (based on the value of the
  468. * old engine and seqno), the lookup is complete and reports NULL.
  469. * If we decide the request is not completed (new engine or seqno),
  470. * then we grab a reference and double check that it is still the
  471. * active request - which it won't be and restart the lookup.
  472. *
  473. * Do not use kmem_cache_zalloc() here!
  474. */
  475. req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
  476. if (!req) {
  477. ret = -ENOMEM;
  478. goto err_unreserve;
  479. }
  480. req->timeline = i915_gem_context_lookup_timeline(ctx, engine);
  481. GEM_BUG_ON(req->timeline == engine->timeline);
  482. spin_lock_init(&req->lock);
  483. dma_fence_init(&req->fence,
  484. &i915_fence_ops,
  485. &req->lock,
  486. req->timeline->fence_context,
  487. __timeline_get_seqno(req->timeline->common));
  488. /* We bump the ref for the fence chain */
  489. i915_sw_fence_init(&i915_gem_request_get(req)->submit, submit_notify);
  490. i915_sw_fence_init(&i915_gem_request_get(req)->execute, execute_notify);
  491. /* Ensure that the execute fence completes after the submit fence -
  492. * as we complete the execute fence from within the submit fence
  493. * callback, its completion would otherwise be visible first.
  494. */
  495. i915_sw_fence_await_sw_fence(&req->execute, &req->submit, &req->execq);
  496. i915_priotree_init(&req->priotree);
  497. INIT_LIST_HEAD(&req->active_list);
  498. req->i915 = dev_priv;
  499. req->engine = engine;
  500. req->ctx = ctx;
  501. /* No zalloc, must clear what we need by hand */
  502. req->global_seqno = 0;
  503. req->file_priv = NULL;
  504. req->batch = NULL;
  505. /*
  506. * Reserve space in the ring buffer for all the commands required to
  507. * eventually emit this request. This is to guarantee that the
  508. * i915_add_request() call can't fail. Note that the reserve may need
  509. * to be redone if the request is not actually submitted straight
  510. * away, e.g. because a GPU scheduler has deferred it.
  511. */
  512. req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  513. GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
  514. ret = engine->request_alloc(req);
  515. if (ret)
  516. goto err_ctx;
  517. /* Record the position of the start of the request so that
  518. * should we detect the updated seqno part-way through the
  519. * GPU processing the request, we never over-estimate the
  520. * position of the head.
  521. */
  522. req->head = req->ring->tail;
  523. return req;
  524. err_ctx:
  525. /* Make sure we didn't add ourselves to external state before freeing */
  526. GEM_BUG_ON(!list_empty(&req->active_list));
  527. GEM_BUG_ON(!list_empty(&req->priotree.signalers_list));
  528. GEM_BUG_ON(!list_empty(&req->priotree.waiters_list));
  529. kmem_cache_free(dev_priv->requests, req);
  530. err_unreserve:
  531. dev_priv->gt.active_requests--;
  532. err_unpin:
  533. engine->context_unpin(engine, ctx);
  534. return ERR_PTR(ret);
  535. }
  536. static int
  537. i915_gem_request_await_request(struct drm_i915_gem_request *to,
  538. struct drm_i915_gem_request *from)
  539. {
  540. int ret;
  541. GEM_BUG_ON(to == from);
  542. if (to->engine->schedule) {
  543. ret = i915_priotree_add_dependency(to->i915,
  544. &to->priotree,
  545. &from->priotree);
  546. if (ret < 0)
  547. return ret;
  548. }
  549. if (to->timeline == from->timeline)
  550. return 0;
  551. if (to->engine == from->engine) {
  552. ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
  553. &from->submit,
  554. GFP_KERNEL);
  555. return ret < 0 ? ret : 0;
  556. }
  557. if (!from->global_seqno) {
  558. ret = i915_sw_fence_await_dma_fence(&to->submit,
  559. &from->fence, 0,
  560. GFP_KERNEL);
  561. return ret < 0 ? ret : 0;
  562. }
  563. if (from->global_seqno <= to->timeline->sync_seqno[from->engine->id])
  564. return 0;
  565. trace_i915_gem_ring_sync_to(to, from);
  566. if (!i915.semaphores) {
  567. if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
  568. ret = i915_sw_fence_await_dma_fence(&to->submit,
  569. &from->fence, 0,
  570. GFP_KERNEL);
  571. if (ret < 0)
  572. return ret;
  573. }
  574. } else {
  575. ret = to->engine->semaphore.sync_to(to, from);
  576. if (ret)
  577. return ret;
  578. }
  579. to->timeline->sync_seqno[from->engine->id] = from->global_seqno;
  580. return 0;
  581. }
  582. int
  583. i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
  584. struct dma_fence *fence)
  585. {
  586. struct dma_fence_array *array;
  587. int ret;
  588. int i;
  589. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  590. return 0;
  591. if (dma_fence_is_i915(fence))
  592. return i915_gem_request_await_request(req, to_request(fence));
  593. if (!dma_fence_is_array(fence)) {
  594. ret = i915_sw_fence_await_dma_fence(&req->submit,
  595. fence, I915_FENCE_TIMEOUT,
  596. GFP_KERNEL);
  597. return ret < 0 ? ret : 0;
  598. }
  599. /* Note that if the fence-array was created in signal-on-any mode,
  600. * we should *not* decompose it into its individual fences. However,
  601. * we don't currently store which mode the fence-array is operating
  602. * in. Fortunately, the only user of signal-on-any is private to
  603. * amdgpu and we should not see any incoming fence-array from
  604. * sync-file being in signal-on-any mode.
  605. */
  606. array = to_dma_fence_array(fence);
  607. for (i = 0; i < array->num_fences; i++) {
  608. struct dma_fence *child = array->fences[i];
  609. if (dma_fence_is_i915(child))
  610. ret = i915_gem_request_await_request(req,
  611. to_request(child));
  612. else
  613. ret = i915_sw_fence_await_dma_fence(&req->submit,
  614. child, I915_FENCE_TIMEOUT,
  615. GFP_KERNEL);
  616. if (ret < 0)
  617. return ret;
  618. }
  619. return 0;
  620. }
  621. /**
  622. * i915_gem_request_await_object - set this request to (async) wait upon a bo
  623. *
  624. * @to: request we are wishing to use
  625. * @obj: object which may be in use on another ring.
  626. *
  627. * This code is meant to abstract object synchronization with the GPU.
  628. * Conceptually we serialise writes between engines inside the GPU.
  629. * We only allow one engine to write into a buffer at any time, but
  630. * multiple readers. To ensure each has a coherent view of memory, we must:
  631. *
  632. * - If there is an outstanding write request to the object, the new
  633. * request must wait for it to complete (either CPU or in hw, requests
  634. * on the same ring will be naturally ordered).
  635. *
  636. * - If we are a write request (pending_write_domain is set), the new
  637. * request must wait for outstanding read requests to complete.
  638. *
  639. * Returns 0 if successful, else propagates up the lower layer error.
  640. */
  641. int
  642. i915_gem_request_await_object(struct drm_i915_gem_request *to,
  643. struct drm_i915_gem_object *obj,
  644. bool write)
  645. {
  646. struct dma_fence *excl;
  647. int ret = 0;
  648. if (write) {
  649. struct dma_fence **shared;
  650. unsigned int count, i;
  651. ret = reservation_object_get_fences_rcu(obj->resv,
  652. &excl, &count, &shared);
  653. if (ret)
  654. return ret;
  655. for (i = 0; i < count; i++) {
  656. ret = i915_gem_request_await_dma_fence(to, shared[i]);
  657. if (ret)
  658. break;
  659. dma_fence_put(shared[i]);
  660. }
  661. for (; i < count; i++)
  662. dma_fence_put(shared[i]);
  663. kfree(shared);
  664. } else {
  665. excl = reservation_object_get_excl_rcu(obj->resv);
  666. }
  667. if (excl) {
  668. if (ret == 0)
  669. ret = i915_gem_request_await_dma_fence(to, excl);
  670. dma_fence_put(excl);
  671. }
  672. return ret;
  673. }
  674. static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
  675. {
  676. struct drm_i915_private *dev_priv = engine->i915;
  677. if (dev_priv->gt.awake)
  678. return;
  679. GEM_BUG_ON(!dev_priv->gt.active_requests);
  680. intel_runtime_pm_get_noresume(dev_priv);
  681. dev_priv->gt.awake = true;
  682. intel_enable_gt_powersave(dev_priv);
  683. i915_update_gfx_val(dev_priv);
  684. if (INTEL_GEN(dev_priv) >= 6)
  685. gen6_rps_busy(dev_priv);
  686. queue_delayed_work(dev_priv->wq,
  687. &dev_priv->gt.retire_work,
  688. round_jiffies_up_relative(HZ));
  689. }
  690. /*
  691. * NB: This function is not allowed to fail. Doing so would mean the the
  692. * request is not being tracked for completion but the work itself is
  693. * going to happen on the hardware. This would be a Bad Thing(tm).
  694. */
  695. void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
  696. {
  697. struct intel_engine_cs *engine = request->engine;
  698. struct intel_ring *ring = request->ring;
  699. struct intel_timeline *timeline = request->timeline;
  700. struct drm_i915_gem_request *prev;
  701. int err;
  702. lockdep_assert_held(&request->i915->drm.struct_mutex);
  703. trace_i915_gem_request_add(request);
  704. /*
  705. * To ensure that this call will not fail, space for its emissions
  706. * should already have been reserved in the ring buffer. Let the ring
  707. * know that it is time to use that space up.
  708. */
  709. request->reserved_space = 0;
  710. /*
  711. * Emit any outstanding flushes - execbuf can fail to emit the flush
  712. * after having emitted the batchbuffer command. Hence we need to fix
  713. * things up similar to emitting the lazy request. The difference here
  714. * is that the flush _must_ happen before the next request, no matter
  715. * what.
  716. */
  717. if (flush_caches) {
  718. err = engine->emit_flush(request, EMIT_FLUSH);
  719. /* Not allowed to fail! */
  720. WARN(err, "engine->emit_flush() failed: %d!\n", err);
  721. }
  722. /* Record the position of the start of the breadcrumb so that
  723. * should we detect the updated seqno part-way through the
  724. * GPU processing the request, we never over-estimate the
  725. * position of the ring's HEAD.
  726. */
  727. err = intel_ring_begin(request, engine->emit_breadcrumb_sz);
  728. GEM_BUG_ON(err);
  729. request->postfix = ring->tail;
  730. ring->tail += engine->emit_breadcrumb_sz * sizeof(u32);
  731. /* Seal the request and mark it as pending execution. Note that
  732. * we may inspect this state, without holding any locks, during
  733. * hangcheck. Hence we apply the barrier to ensure that we do not
  734. * see a more recent value in the hws than we are tracking.
  735. */
  736. prev = i915_gem_active_raw(&timeline->last_request,
  737. &request->i915->drm.struct_mutex);
  738. if (prev) {
  739. i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
  740. &request->submitq);
  741. if (engine->schedule)
  742. __i915_priotree_add_dependency(&request->priotree,
  743. &prev->priotree,
  744. &request->dep,
  745. 0);
  746. }
  747. spin_lock_irq(&timeline->lock);
  748. list_add_tail(&request->link, &timeline->requests);
  749. spin_unlock_irq(&timeline->lock);
  750. GEM_BUG_ON(i915_seqno_passed(timeline->last_submitted_seqno,
  751. request->fence.seqno));
  752. timeline->last_submitted_seqno = request->fence.seqno;
  753. i915_gem_active_set(&timeline->last_request, request);
  754. list_add_tail(&request->ring_link, &ring->request_list);
  755. request->emitted_jiffies = jiffies;
  756. i915_gem_mark_busy(engine);
  757. /* Let the backend know a new request has arrived that may need
  758. * to adjust the existing execution schedule due to a high priority
  759. * request - i.e. we may want to preempt the current request in order
  760. * to run a high priority dependency chain *before* we can execute this
  761. * request.
  762. *
  763. * This is called before the request is ready to run so that we can
  764. * decide whether to preempt the entire chain so that it is ready to
  765. * run at the earliest possible convenience.
  766. */
  767. if (engine->schedule)
  768. engine->schedule(request, request->ctx->priority);
  769. local_bh_disable();
  770. i915_sw_fence_commit(&request->submit);
  771. local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
  772. }
  773. static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
  774. {
  775. unsigned long flags;
  776. spin_lock_irqsave(&q->lock, flags);
  777. if (list_empty(&wait->task_list))
  778. __add_wait_queue(q, wait);
  779. spin_unlock_irqrestore(&q->lock, flags);
  780. }
  781. static unsigned long local_clock_us(unsigned int *cpu)
  782. {
  783. unsigned long t;
  784. /* Cheaply and approximately convert from nanoseconds to microseconds.
  785. * The result and subsequent calculations are also defined in the same
  786. * approximate microseconds units. The principal source of timing
  787. * error here is from the simple truncation.
  788. *
  789. * Note that local_clock() is only defined wrt to the current CPU;
  790. * the comparisons are no longer valid if we switch CPUs. Instead of
  791. * blocking preemption for the entire busywait, we can detect the CPU
  792. * switch and use that as indicator of system load and a reason to
  793. * stop busywaiting, see busywait_stop().
  794. */
  795. *cpu = get_cpu();
  796. t = local_clock() >> 10;
  797. put_cpu();
  798. return t;
  799. }
  800. static bool busywait_stop(unsigned long timeout, unsigned int cpu)
  801. {
  802. unsigned int this_cpu;
  803. if (time_after(local_clock_us(&this_cpu), timeout))
  804. return true;
  805. return this_cpu != cpu;
  806. }
  807. bool __i915_spin_request(const struct drm_i915_gem_request *req,
  808. int state, unsigned long timeout_us)
  809. {
  810. unsigned int cpu;
  811. /* When waiting for high frequency requests, e.g. during synchronous
  812. * rendering split between the CPU and GPU, the finite amount of time
  813. * required to set up the irq and wait upon it limits the response
  814. * rate. By busywaiting on the request completion for a short while we
  815. * can service the high frequency waits as quick as possible. However,
  816. * if it is a slow request, we want to sleep as quickly as possible.
  817. * The tradeoff between waiting and sleeping is roughly the time it
  818. * takes to sleep on a request, on the order of a microsecond.
  819. */
  820. timeout_us += local_clock_us(&cpu);
  821. do {
  822. if (__i915_gem_request_completed(req))
  823. return true;
  824. if (signal_pending_state(state, current))
  825. break;
  826. if (busywait_stop(timeout_us, cpu))
  827. break;
  828. cpu_relax_lowlatency();
  829. } while (!need_resched());
  830. return false;
  831. }
  832. static long
  833. __i915_request_wait_for_execute(struct drm_i915_gem_request *request,
  834. unsigned int flags,
  835. long timeout)
  836. {
  837. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  838. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  839. wait_queue_head_t *q = &request->i915->gpu_error.wait_queue;
  840. DEFINE_WAIT(reset);
  841. DEFINE_WAIT(wait);
  842. if (flags & I915_WAIT_LOCKED)
  843. add_wait_queue(q, &reset);
  844. do {
  845. prepare_to_wait(&request->execute.wait, &wait, state);
  846. if (i915_sw_fence_done(&request->execute))
  847. break;
  848. if (flags & I915_WAIT_LOCKED &&
  849. i915_reset_in_progress(&request->i915->gpu_error)) {
  850. __set_current_state(TASK_RUNNING);
  851. i915_reset(request->i915);
  852. reset_wait_queue(q, &reset);
  853. continue;
  854. }
  855. if (signal_pending_state(state, current)) {
  856. timeout = -ERESTARTSYS;
  857. break;
  858. }
  859. timeout = io_schedule_timeout(timeout);
  860. } while (timeout);
  861. finish_wait(&request->execute.wait, &wait);
  862. if (flags & I915_WAIT_LOCKED)
  863. remove_wait_queue(q, &reset);
  864. return timeout;
  865. }
  866. /**
  867. * i915_wait_request - wait until execution of request has finished
  868. * @req: the request to wait upon
  869. * @flags: how to wait
  870. * @timeout: how long to wait in jiffies
  871. *
  872. * i915_wait_request() waits for the request to be completed, for a
  873. * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
  874. * unbounded wait).
  875. *
  876. * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
  877. * in via the flags, and vice versa if the struct_mutex is not held, the caller
  878. * must not specify that the wait is locked.
  879. *
  880. * Returns the remaining time (in jiffies) if the request completed, which may
  881. * be zero or -ETIME if the request is unfinished after the timeout expires.
  882. * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
  883. * pending before the request completes.
  884. */
  885. long i915_wait_request(struct drm_i915_gem_request *req,
  886. unsigned int flags,
  887. long timeout)
  888. {
  889. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  890. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  891. DEFINE_WAIT(reset);
  892. struct intel_wait wait;
  893. might_sleep();
  894. #if IS_ENABLED(CONFIG_LOCKDEP)
  895. GEM_BUG_ON(debug_locks &&
  896. !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
  897. !!(flags & I915_WAIT_LOCKED));
  898. #endif
  899. GEM_BUG_ON(timeout < 0);
  900. if (i915_gem_request_completed(req))
  901. return timeout;
  902. if (!timeout)
  903. return -ETIME;
  904. trace_i915_gem_request_wait_begin(req);
  905. if (!i915_sw_fence_done(&req->execute)) {
  906. timeout = __i915_request_wait_for_execute(req, flags, timeout);
  907. if (timeout < 0)
  908. goto complete;
  909. GEM_BUG_ON(!i915_sw_fence_done(&req->execute));
  910. }
  911. GEM_BUG_ON(!i915_sw_fence_done(&req->submit));
  912. GEM_BUG_ON(!req->global_seqno);
  913. /* Optimistic short spin before touching IRQs */
  914. if (i915_spin_request(req, state, 5))
  915. goto complete;
  916. set_current_state(state);
  917. if (flags & I915_WAIT_LOCKED)
  918. add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  919. intel_wait_init(&wait, req->global_seqno);
  920. if (intel_engine_add_wait(req->engine, &wait))
  921. /* In order to check that we haven't missed the interrupt
  922. * as we enabled it, we need to kick ourselves to do a
  923. * coherent check on the seqno before we sleep.
  924. */
  925. goto wakeup;
  926. for (;;) {
  927. if (signal_pending_state(state, current)) {
  928. timeout = -ERESTARTSYS;
  929. break;
  930. }
  931. if (!timeout) {
  932. timeout = -ETIME;
  933. break;
  934. }
  935. timeout = io_schedule_timeout(timeout);
  936. if (intel_wait_complete(&wait))
  937. break;
  938. set_current_state(state);
  939. wakeup:
  940. /* Carefully check if the request is complete, giving time
  941. * for the seqno to be visible following the interrupt.
  942. * We also have to check in case we are kicked by the GPU
  943. * reset in order to drop the struct_mutex.
  944. */
  945. if (__i915_request_irq_complete(req))
  946. break;
  947. /* If the GPU is hung, and we hold the lock, reset the GPU
  948. * and then check for completion. On a full reset, the engine's
  949. * HW seqno will be advanced passed us and we are complete.
  950. * If we do a partial reset, we have to wait for the GPU to
  951. * resume and update the breadcrumb.
  952. *
  953. * If we don't hold the mutex, we can just wait for the worker
  954. * to come along and update the breadcrumb (either directly
  955. * itself, or indirectly by recovering the GPU).
  956. */
  957. if (flags & I915_WAIT_LOCKED &&
  958. i915_reset_in_progress(&req->i915->gpu_error)) {
  959. __set_current_state(TASK_RUNNING);
  960. i915_reset(req->i915);
  961. reset_wait_queue(&req->i915->gpu_error.wait_queue,
  962. &reset);
  963. continue;
  964. }
  965. /* Only spin if we know the GPU is processing this request */
  966. if (i915_spin_request(req, state, 2))
  967. break;
  968. }
  969. intel_engine_remove_wait(req->engine, &wait);
  970. if (flags & I915_WAIT_LOCKED)
  971. remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  972. __set_current_state(TASK_RUNNING);
  973. complete:
  974. trace_i915_gem_request_wait_end(req);
  975. return timeout;
  976. }
  977. static void engine_retire_requests(struct intel_engine_cs *engine)
  978. {
  979. struct drm_i915_gem_request *request, *next;
  980. list_for_each_entry_safe(request, next,
  981. &engine->timeline->requests, link) {
  982. if (!__i915_gem_request_completed(request))
  983. return;
  984. i915_gem_request_retire(request);
  985. }
  986. }
  987. void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
  988. {
  989. struct intel_engine_cs *engine;
  990. enum intel_engine_id id;
  991. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  992. if (!dev_priv->gt.active_requests)
  993. return;
  994. for_each_engine(engine, dev_priv, id)
  995. engine_retire_requests(engine);
  996. }