i915_gem_request.c 34 KB

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