i915_gem_request.c 33 KB

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