i915_gem_request.c 30 KB

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