i915_gem_request.c 28 KB

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