i915_gem_request.c 28 KB

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