i915_gem_request.c 29 KB

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