i915_gem_request.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. if (state != FENCE_COMPLETE)
  260. return NOTIFY_DONE;
  261. /* Will be called from irq-context when using foreign DMA fences */
  262. engine->timeline->last_submitted_seqno = request->fence.seqno;
  263. engine->emit_breadcrumb(request,
  264. request->ring->vaddr + request->postfix);
  265. engine->submit_request(request);
  266. return NOTIFY_DONE;
  267. }
  268. /**
  269. * i915_gem_request_alloc - allocate a request structure
  270. *
  271. * @engine: engine that we wish to issue the request on.
  272. * @ctx: context that the request will be associated with.
  273. * This can be NULL if the request is not directly related to
  274. * any specific user context, in which case this function will
  275. * choose an appropriate context to use.
  276. *
  277. * Returns a pointer to the allocated request if successful,
  278. * or an error code if not.
  279. */
  280. struct drm_i915_gem_request *
  281. i915_gem_request_alloc(struct intel_engine_cs *engine,
  282. struct i915_gem_context *ctx)
  283. {
  284. struct drm_i915_private *dev_priv = engine->i915;
  285. struct drm_i915_gem_request *req;
  286. u32 seqno;
  287. int ret;
  288. /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  289. * EIO if the GPU is already wedged, or EAGAIN to drop the struct_mutex
  290. * and restart.
  291. */
  292. ret = i915_gem_check_wedge(dev_priv);
  293. if (ret)
  294. return ERR_PTR(ret);
  295. /* Move the oldest request to the slab-cache (if not in use!) */
  296. req = list_first_entry_or_null(&engine->timeline->requests,
  297. typeof(*req), link);
  298. if (req && i915_gem_request_completed(req))
  299. i915_gem_request_retire(req);
  300. /* Beware: Dragons be flying overhead.
  301. *
  302. * We use RCU to look up requests in flight. The lookups may
  303. * race with the request being allocated from the slab freelist.
  304. * That is the request we are writing to here, may be in the process
  305. * of being read by __i915_gem_active_get_rcu(). As such,
  306. * we have to be very careful when overwriting the contents. During
  307. * the RCU lookup, we change chase the request->engine pointer,
  308. * read the request->global_seqno and increment the reference count.
  309. *
  310. * The reference count is incremented atomically. If it is zero,
  311. * the lookup knows the request is unallocated and complete. Otherwise,
  312. * it is either still in use, or has been reallocated and reset
  313. * with dma_fence_init(). This increment is safe for release as we
  314. * check that the request we have a reference to and matches the active
  315. * request.
  316. *
  317. * Before we increment the refcount, we chase the request->engine
  318. * pointer. We must not call kmem_cache_zalloc() or else we set
  319. * that pointer to NULL and cause a crash during the lookup. If
  320. * we see the request is completed (based on the value of the
  321. * old engine and seqno), the lookup is complete and reports NULL.
  322. * If we decide the request is not completed (new engine or seqno),
  323. * then we grab a reference and double check that it is still the
  324. * active request - which it won't be and restart the lookup.
  325. *
  326. * Do not use kmem_cache_zalloc() here!
  327. */
  328. req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
  329. if (!req)
  330. return ERR_PTR(-ENOMEM);
  331. ret = i915_gem_get_global_seqno(dev_priv, &seqno);
  332. if (ret)
  333. goto err;
  334. req->timeline = engine->timeline;
  335. spin_lock_init(&req->lock);
  336. dma_fence_init(&req->fence,
  337. &i915_fence_ops,
  338. &req->lock,
  339. req->timeline->fence_context,
  340. seqno);
  341. i915_sw_fence_init(&req->submit, submit_notify);
  342. INIT_LIST_HEAD(&req->active_list);
  343. req->i915 = dev_priv;
  344. req->engine = engine;
  345. req->global_seqno = seqno;
  346. req->ctx = i915_gem_context_get(ctx);
  347. /* No zalloc, must clear what we need by hand */
  348. req->previous_context = NULL;
  349. req->file_priv = NULL;
  350. req->batch = NULL;
  351. /*
  352. * Reserve space in the ring buffer for all the commands required to
  353. * eventually emit this request. This is to guarantee that the
  354. * i915_add_request() call can't fail. Note that the reserve may need
  355. * to be redone if the request is not actually submitted straight
  356. * away, e.g. because a GPU scheduler has deferred it.
  357. */
  358. req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  359. GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
  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. if (!from->global_seqno) {
  394. ret = i915_sw_fence_await_dma_fence(&to->submit,
  395. &from->fence, 0,
  396. GFP_KERNEL);
  397. return ret < 0 ? ret : 0;
  398. }
  399. idx = intel_engine_sync_index(from->engine, to->engine);
  400. if (from->global_seqno <= from->engine->semaphore.sync_seqno[idx])
  401. return 0;
  402. trace_i915_gem_ring_sync_to(to, from);
  403. if (!i915.semaphores) {
  404. if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
  405. ret = i915_sw_fence_await_dma_fence(&to->submit,
  406. &from->fence, 0,
  407. GFP_KERNEL);
  408. if (ret < 0)
  409. return ret;
  410. }
  411. } else {
  412. ret = to->engine->semaphore.sync_to(to, from);
  413. if (ret)
  414. return ret;
  415. }
  416. from->engine->semaphore.sync_seqno[idx] = from->global_seqno;
  417. return 0;
  418. }
  419. int
  420. i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
  421. struct dma_fence *fence)
  422. {
  423. struct dma_fence_array *array;
  424. int ret;
  425. int i;
  426. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  427. return 0;
  428. if (dma_fence_is_i915(fence))
  429. return i915_gem_request_await_request(req, to_request(fence));
  430. if (!dma_fence_is_array(fence)) {
  431. ret = i915_sw_fence_await_dma_fence(&req->submit,
  432. fence, I915_FENCE_TIMEOUT,
  433. GFP_KERNEL);
  434. return ret < 0 ? ret : 0;
  435. }
  436. /* Note that if the fence-array was created in signal-on-any mode,
  437. * we should *not* decompose it into its individual fences. However,
  438. * we don't currently store which mode the fence-array is operating
  439. * in. Fortunately, the only user of signal-on-any is private to
  440. * amdgpu and we should not see any incoming fence-array from
  441. * sync-file being in signal-on-any mode.
  442. */
  443. array = to_dma_fence_array(fence);
  444. for (i = 0; i < array->num_fences; i++) {
  445. struct dma_fence *child = array->fences[i];
  446. if (dma_fence_is_i915(child))
  447. ret = i915_gem_request_await_request(req,
  448. to_request(child));
  449. else
  450. ret = i915_sw_fence_await_dma_fence(&req->submit,
  451. child, I915_FENCE_TIMEOUT,
  452. GFP_KERNEL);
  453. if (ret < 0)
  454. return ret;
  455. }
  456. return 0;
  457. }
  458. /**
  459. * i915_gem_request_await_object - set this request to (async) wait upon a bo
  460. *
  461. * @to: request we are wishing to use
  462. * @obj: object which may be in use on another ring.
  463. *
  464. * This code is meant to abstract object synchronization with the GPU.
  465. * Conceptually we serialise writes between engines inside the GPU.
  466. * We only allow one engine to write into a buffer at any time, but
  467. * multiple readers. To ensure each has a coherent view of memory, we must:
  468. *
  469. * - If there is an outstanding write request to the object, the new
  470. * request must wait for it to complete (either CPU or in hw, requests
  471. * on the same ring will be naturally ordered).
  472. *
  473. * - If we are a write request (pending_write_domain is set), the new
  474. * request must wait for outstanding read requests to complete.
  475. *
  476. * Returns 0 if successful, else propagates up the lower layer error.
  477. */
  478. int
  479. i915_gem_request_await_object(struct drm_i915_gem_request *to,
  480. struct drm_i915_gem_object *obj,
  481. bool write)
  482. {
  483. struct dma_fence *excl;
  484. int ret = 0;
  485. if (write) {
  486. struct dma_fence **shared;
  487. unsigned int count, i;
  488. ret = reservation_object_get_fences_rcu(obj->resv,
  489. &excl, &count, &shared);
  490. if (ret)
  491. return ret;
  492. for (i = 0; i < count; i++) {
  493. ret = i915_gem_request_await_dma_fence(to, shared[i]);
  494. if (ret)
  495. break;
  496. dma_fence_put(shared[i]);
  497. }
  498. for (; i < count; i++)
  499. dma_fence_put(shared[i]);
  500. kfree(shared);
  501. } else {
  502. excl = reservation_object_get_excl_rcu(obj->resv);
  503. }
  504. if (excl) {
  505. if (ret == 0)
  506. ret = i915_gem_request_await_dma_fence(to, excl);
  507. dma_fence_put(excl);
  508. }
  509. return ret;
  510. }
  511. static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
  512. {
  513. struct drm_i915_private *dev_priv = engine->i915;
  514. dev_priv->gt.active_engines |= intel_engine_flag(engine);
  515. if (dev_priv->gt.awake)
  516. return;
  517. intel_runtime_pm_get_noresume(dev_priv);
  518. dev_priv->gt.awake = true;
  519. intel_enable_gt_powersave(dev_priv);
  520. i915_update_gfx_val(dev_priv);
  521. if (INTEL_GEN(dev_priv) >= 6)
  522. gen6_rps_busy(dev_priv);
  523. queue_delayed_work(dev_priv->wq,
  524. &dev_priv->gt.retire_work,
  525. round_jiffies_up_relative(HZ));
  526. }
  527. /*
  528. * NB: This function is not allowed to fail. Doing so would mean the the
  529. * request is not being tracked for completion but the work itself is
  530. * going to happen on the hardware. This would be a Bad Thing(tm).
  531. */
  532. void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
  533. {
  534. struct intel_engine_cs *engine = request->engine;
  535. struct intel_ring *ring = request->ring;
  536. struct intel_timeline *timeline = request->timeline;
  537. struct drm_i915_gem_request *prev;
  538. int err;
  539. lockdep_assert_held(&request->i915->drm.struct_mutex);
  540. trace_i915_gem_request_add(request);
  541. /*
  542. * To ensure that this call will not fail, space for its emissions
  543. * should already have been reserved in the ring buffer. Let the ring
  544. * know that it is time to use that space up.
  545. */
  546. request->reserved_space = 0;
  547. /*
  548. * Emit any outstanding flushes - execbuf can fail to emit the flush
  549. * after having emitted the batchbuffer command. Hence we need to fix
  550. * things up similar to emitting the lazy request. The difference here
  551. * is that the flush _must_ happen before the next request, no matter
  552. * what.
  553. */
  554. if (flush_caches) {
  555. err = engine->emit_flush(request, EMIT_FLUSH);
  556. /* Not allowed to fail! */
  557. WARN(err, "engine->emit_flush() failed: %d!\n", err);
  558. }
  559. /* Record the position of the start of the breadcrumb so that
  560. * should we detect the updated seqno part-way through the
  561. * GPU processing the request, we never over-estimate the
  562. * position of the ring's HEAD.
  563. */
  564. err = intel_ring_begin(request, engine->emit_breadcrumb_sz);
  565. GEM_BUG_ON(err);
  566. request->postfix = ring->tail;
  567. ring->tail += engine->emit_breadcrumb_sz * sizeof(u32);
  568. /* Seal the request and mark it as pending execution. Note that
  569. * we may inspect this state, without holding any locks, during
  570. * hangcheck. Hence we apply the barrier to ensure that we do not
  571. * see a more recent value in the hws than we are tracking.
  572. */
  573. prev = i915_gem_active_raw(&timeline->last_request,
  574. &request->i915->drm.struct_mutex);
  575. if (prev)
  576. i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
  577. &request->submitq);
  578. request->emitted_jiffies = jiffies;
  579. request->previous_seqno = timeline->last_pending_seqno;
  580. timeline->last_pending_seqno = request->fence.seqno;
  581. i915_gem_active_set(&timeline->last_request, request);
  582. list_add_tail(&request->link, &timeline->requests);
  583. list_add_tail(&request->ring_link, &ring->request_list);
  584. i915_gem_mark_busy(engine);
  585. local_bh_disable();
  586. i915_sw_fence_commit(&request->submit);
  587. local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
  588. }
  589. static void reset_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
  590. {
  591. unsigned long flags;
  592. spin_lock_irqsave(&q->lock, flags);
  593. if (list_empty(&wait->task_list))
  594. __add_wait_queue(q, wait);
  595. spin_unlock_irqrestore(&q->lock, flags);
  596. }
  597. static unsigned long local_clock_us(unsigned int *cpu)
  598. {
  599. unsigned long t;
  600. /* Cheaply and approximately convert from nanoseconds to microseconds.
  601. * The result and subsequent calculations are also defined in the same
  602. * approximate microseconds units. The principal source of timing
  603. * error here is from the simple truncation.
  604. *
  605. * Note that local_clock() is only defined wrt to the current CPU;
  606. * the comparisons are no longer valid if we switch CPUs. Instead of
  607. * blocking preemption for the entire busywait, we can detect the CPU
  608. * switch and use that as indicator of system load and a reason to
  609. * stop busywaiting, see busywait_stop().
  610. */
  611. *cpu = get_cpu();
  612. t = local_clock() >> 10;
  613. put_cpu();
  614. return t;
  615. }
  616. static bool busywait_stop(unsigned long timeout, unsigned int cpu)
  617. {
  618. unsigned int this_cpu;
  619. if (time_after(local_clock_us(&this_cpu), timeout))
  620. return true;
  621. return this_cpu != cpu;
  622. }
  623. bool __i915_spin_request(const struct drm_i915_gem_request *req,
  624. int state, unsigned long timeout_us)
  625. {
  626. unsigned int cpu;
  627. /* When waiting for high frequency requests, e.g. during synchronous
  628. * rendering split between the CPU and GPU, the finite amount of time
  629. * required to set up the irq and wait upon it limits the response
  630. * rate. By busywaiting on the request completion for a short while we
  631. * can service the high frequency waits as quick as possible. However,
  632. * if it is a slow request, we want to sleep as quickly as possible.
  633. * The tradeoff between waiting and sleeping is roughly the time it
  634. * takes to sleep on a request, on the order of a microsecond.
  635. */
  636. timeout_us += local_clock_us(&cpu);
  637. do {
  638. if (__i915_gem_request_completed(req))
  639. return true;
  640. if (signal_pending_state(state, current))
  641. break;
  642. if (busywait_stop(timeout_us, cpu))
  643. break;
  644. cpu_relax_lowlatency();
  645. } while (!need_resched());
  646. return false;
  647. }
  648. static long
  649. __i915_request_wait_for_submit(struct drm_i915_gem_request *request,
  650. unsigned int flags,
  651. long timeout)
  652. {
  653. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  654. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  655. wait_queue_head_t *q = &request->i915->gpu_error.wait_queue;
  656. DEFINE_WAIT(reset);
  657. DEFINE_WAIT(wait);
  658. if (flags & I915_WAIT_LOCKED)
  659. add_wait_queue(q, &reset);
  660. do {
  661. prepare_to_wait(&request->submit.wait, &wait, state);
  662. if (i915_sw_fence_done(&request->submit))
  663. break;
  664. if (flags & I915_WAIT_LOCKED &&
  665. i915_reset_in_progress(&request->i915->gpu_error)) {
  666. __set_current_state(TASK_RUNNING);
  667. i915_reset(request->i915);
  668. reset_wait_queue(q, &reset);
  669. continue;
  670. }
  671. if (signal_pending_state(state, current)) {
  672. timeout = -ERESTARTSYS;
  673. break;
  674. }
  675. timeout = io_schedule_timeout(timeout);
  676. } while (timeout);
  677. finish_wait(&request->submit.wait, &wait);
  678. if (flags & I915_WAIT_LOCKED)
  679. remove_wait_queue(q, &reset);
  680. return timeout;
  681. }
  682. /**
  683. * i915_wait_request - wait until execution of request has finished
  684. * @req: the request to wait upon
  685. * @flags: how to wait
  686. * @timeout: how long to wait in jiffies
  687. *
  688. * i915_wait_request() waits for the request to be completed, for a
  689. * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
  690. * unbounded wait).
  691. *
  692. * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
  693. * in via the flags, and vice versa if the struct_mutex is not held, the caller
  694. * must not specify that the wait is locked.
  695. *
  696. * Returns the remaining time (in jiffies) if the request completed, which may
  697. * be zero or -ETIME if the request is unfinished after the timeout expires.
  698. * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
  699. * pending before the request completes.
  700. */
  701. long i915_wait_request(struct drm_i915_gem_request *req,
  702. unsigned int flags,
  703. long timeout)
  704. {
  705. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  706. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  707. DEFINE_WAIT(reset);
  708. struct intel_wait wait;
  709. might_sleep();
  710. #if IS_ENABLED(CONFIG_LOCKDEP)
  711. GEM_BUG_ON(debug_locks &&
  712. !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
  713. !!(flags & I915_WAIT_LOCKED));
  714. #endif
  715. GEM_BUG_ON(timeout < 0);
  716. if (i915_gem_request_completed(req))
  717. return timeout;
  718. if (!timeout)
  719. return -ETIME;
  720. trace_i915_gem_request_wait_begin(req);
  721. if (!i915_sw_fence_done(&req->submit)) {
  722. timeout = __i915_request_wait_for_submit(req, flags, timeout);
  723. if (timeout < 0)
  724. goto complete;
  725. GEM_BUG_ON(!i915_sw_fence_done(&req->submit));
  726. }
  727. GEM_BUG_ON(!req->global_seqno);
  728. /* Optimistic short spin before touching IRQs */
  729. if (i915_spin_request(req, state, 5))
  730. goto complete;
  731. set_current_state(state);
  732. if (flags & I915_WAIT_LOCKED)
  733. add_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  734. intel_wait_init(&wait, req->global_seqno);
  735. if (intel_engine_add_wait(req->engine, &wait))
  736. /* In order to check that we haven't missed the interrupt
  737. * as we enabled it, we need to kick ourselves to do a
  738. * coherent check on the seqno before we sleep.
  739. */
  740. goto wakeup;
  741. for (;;) {
  742. if (signal_pending_state(state, current)) {
  743. timeout = -ERESTARTSYS;
  744. break;
  745. }
  746. if (!timeout) {
  747. timeout = -ETIME;
  748. break;
  749. }
  750. timeout = io_schedule_timeout(timeout);
  751. if (intel_wait_complete(&wait))
  752. break;
  753. set_current_state(state);
  754. wakeup:
  755. /* Carefully check if the request is complete, giving time
  756. * for the seqno to be visible following the interrupt.
  757. * We also have to check in case we are kicked by the GPU
  758. * reset in order to drop the struct_mutex.
  759. */
  760. if (__i915_request_irq_complete(req))
  761. break;
  762. /* If the GPU is hung, and we hold the lock, reset the GPU
  763. * and then check for completion. On a full reset, the engine's
  764. * HW seqno will be advanced passed us and we are complete.
  765. * If we do a partial reset, we have to wait for the GPU to
  766. * resume and update the breadcrumb.
  767. *
  768. * If we don't hold the mutex, we can just wait for the worker
  769. * to come along and update the breadcrumb (either directly
  770. * itself, or indirectly by recovering the GPU).
  771. */
  772. if (flags & I915_WAIT_LOCKED &&
  773. i915_reset_in_progress(&req->i915->gpu_error)) {
  774. __set_current_state(TASK_RUNNING);
  775. i915_reset(req->i915);
  776. reset_wait_queue(&req->i915->gpu_error.wait_queue,
  777. &reset);
  778. continue;
  779. }
  780. /* Only spin if we know the GPU is processing this request */
  781. if (i915_spin_request(req, state, 2))
  782. break;
  783. }
  784. intel_engine_remove_wait(req->engine, &wait);
  785. if (flags & I915_WAIT_LOCKED)
  786. remove_wait_queue(&req->i915->gpu_error.wait_queue, &reset);
  787. __set_current_state(TASK_RUNNING);
  788. complete:
  789. trace_i915_gem_request_wait_end(req);
  790. return timeout;
  791. }
  792. static bool engine_retire_requests(struct intel_engine_cs *engine)
  793. {
  794. struct drm_i915_gem_request *request, *next;
  795. list_for_each_entry_safe(request, next,
  796. &engine->timeline->requests, link) {
  797. if (!i915_gem_request_completed(request))
  798. return false;
  799. i915_gem_request_retire(request);
  800. }
  801. return true;
  802. }
  803. void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
  804. {
  805. struct intel_engine_cs *engine;
  806. unsigned int tmp;
  807. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  808. if (dev_priv->gt.active_engines == 0)
  809. return;
  810. GEM_BUG_ON(!dev_priv->gt.awake);
  811. for_each_engine_masked(engine, dev_priv, dev_priv->gt.active_engines, tmp)
  812. if (engine_retire_requests(engine))
  813. dev_priv->gt.active_engines &= ~intel_engine_flag(engine);
  814. if (dev_priv->gt.active_engines == 0)
  815. queue_delayed_work(dev_priv->wq,
  816. &dev_priv->gt.idle_work,
  817. msecs_to_jiffies(100));
  818. }