i915_gem_request.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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 <linux/sched.h>
  27. #include <linux/sched/clock.h>
  28. #include <linux/sched/signal.h>
  29. #include "i915_drv.h"
  30. static const char *i915_fence_get_driver_name(struct dma_fence *fence)
  31. {
  32. return "i915";
  33. }
  34. static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
  35. {
  36. /* The timeline struct (as part of the ppgtt underneath a context)
  37. * may be freed when the request is no longer in use by the GPU.
  38. * We could extend the life of a context to beyond that of all
  39. * fences, possibly keeping the hw resource around indefinitely,
  40. * or we just give them a false name. Since
  41. * dma_fence_ops.get_timeline_name is a debug feature, the occasional
  42. * lie seems justifiable.
  43. */
  44. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  45. return "signaled";
  46. return to_request(fence)->timeline->common->name;
  47. }
  48. static bool i915_fence_signaled(struct dma_fence *fence)
  49. {
  50. return i915_gem_request_completed(to_request(fence));
  51. }
  52. static bool i915_fence_enable_signaling(struct dma_fence *fence)
  53. {
  54. if (i915_fence_signaled(fence))
  55. return false;
  56. intel_engine_enable_signaling(to_request(fence));
  57. return true;
  58. }
  59. static signed long i915_fence_wait(struct dma_fence *fence,
  60. bool interruptible,
  61. signed long timeout)
  62. {
  63. return i915_wait_request(to_request(fence), interruptible, timeout);
  64. }
  65. static void i915_fence_release(struct dma_fence *fence)
  66. {
  67. struct drm_i915_gem_request *req = to_request(fence);
  68. /* The request is put onto a RCU freelist (i.e. the address
  69. * is immediately reused), mark the fences as being freed now.
  70. * Otherwise the debugobjects for the fences are only marked as
  71. * freed when the slab cache itself is freed, and so we would get
  72. * caught trying to reuse dead objects.
  73. */
  74. i915_sw_fence_fini(&req->submit);
  75. kmem_cache_free(req->i915->requests, req);
  76. }
  77. const struct dma_fence_ops i915_fence_ops = {
  78. .get_driver_name = i915_fence_get_driver_name,
  79. .get_timeline_name = i915_fence_get_timeline_name,
  80. .enable_signaling = i915_fence_enable_signaling,
  81. .signaled = i915_fence_signaled,
  82. .wait = i915_fence_wait,
  83. .release = i915_fence_release,
  84. };
  85. static inline void
  86. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  87. {
  88. struct drm_i915_file_private *file_priv;
  89. file_priv = request->file_priv;
  90. if (!file_priv)
  91. return;
  92. spin_lock(&file_priv->mm.lock);
  93. if (request->file_priv) {
  94. list_del(&request->client_link);
  95. request->file_priv = NULL;
  96. }
  97. spin_unlock(&file_priv->mm.lock);
  98. }
  99. static struct i915_dependency *
  100. i915_dependency_alloc(struct drm_i915_private *i915)
  101. {
  102. return kmem_cache_alloc(i915->dependencies, GFP_KERNEL);
  103. }
  104. static void
  105. i915_dependency_free(struct drm_i915_private *i915,
  106. struct i915_dependency *dep)
  107. {
  108. kmem_cache_free(i915->dependencies, dep);
  109. }
  110. static void
  111. __i915_priotree_add_dependency(struct i915_priotree *pt,
  112. struct i915_priotree *signal,
  113. struct i915_dependency *dep,
  114. unsigned long flags)
  115. {
  116. INIT_LIST_HEAD(&dep->dfs_link);
  117. list_add(&dep->wait_link, &signal->waiters_list);
  118. list_add(&dep->signal_link, &pt->signalers_list);
  119. dep->signaler = signal;
  120. dep->flags = flags;
  121. }
  122. static int
  123. i915_priotree_add_dependency(struct drm_i915_private *i915,
  124. struct i915_priotree *pt,
  125. struct i915_priotree *signal)
  126. {
  127. struct i915_dependency *dep;
  128. dep = i915_dependency_alloc(i915);
  129. if (!dep)
  130. return -ENOMEM;
  131. __i915_priotree_add_dependency(pt, signal, dep, I915_DEPENDENCY_ALLOC);
  132. return 0;
  133. }
  134. static void
  135. i915_priotree_fini(struct drm_i915_private *i915, struct i915_priotree *pt)
  136. {
  137. struct i915_dependency *dep, *next;
  138. GEM_BUG_ON(!RB_EMPTY_NODE(&pt->node));
  139. /* Everyone we depended upon (the fences we wait to be signaled)
  140. * should retire before us and remove themselves from our list.
  141. * However, retirement is run independently on each timeline and
  142. * so we may be called out-of-order.
  143. */
  144. list_for_each_entry_safe(dep, next, &pt->signalers_list, signal_link) {
  145. list_del(&dep->wait_link);
  146. if (dep->flags & I915_DEPENDENCY_ALLOC)
  147. i915_dependency_free(i915, dep);
  148. }
  149. /* Remove ourselves from everyone who depends upon us */
  150. list_for_each_entry_safe(dep, next, &pt->waiters_list, wait_link) {
  151. list_del(&dep->signal_link);
  152. if (dep->flags & I915_DEPENDENCY_ALLOC)
  153. i915_dependency_free(i915, dep);
  154. }
  155. }
  156. static void
  157. i915_priotree_init(struct i915_priotree *pt)
  158. {
  159. INIT_LIST_HEAD(&pt->signalers_list);
  160. INIT_LIST_HEAD(&pt->waiters_list);
  161. RB_CLEAR_NODE(&pt->node);
  162. pt->priority = INT_MIN;
  163. }
  164. static int reset_all_global_seqno(struct drm_i915_private *i915, u32 seqno)
  165. {
  166. struct intel_engine_cs *engine;
  167. enum intel_engine_id id;
  168. int ret;
  169. /* Carefully retire all requests without writing to the rings */
  170. ret = i915_gem_wait_for_idle(i915,
  171. I915_WAIT_INTERRUPTIBLE |
  172. I915_WAIT_LOCKED);
  173. if (ret)
  174. return ret;
  175. /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
  176. for_each_engine(engine, i915, id) {
  177. struct i915_gem_timeline *timeline;
  178. struct intel_timeline *tl = engine->timeline;
  179. if (!i915_seqno_passed(seqno, tl->seqno)) {
  180. /* spin until threads are complete */
  181. while (intel_breadcrumbs_busy(engine))
  182. cond_resched();
  183. }
  184. /* Finally reset hw state */
  185. tl->seqno = seqno;
  186. intel_engine_init_global_seqno(engine, seqno);
  187. list_for_each_entry(timeline, &i915->gt.timelines, link)
  188. memset(timeline->engine[id].sync_seqno, 0,
  189. sizeof(timeline->engine[id].sync_seqno));
  190. }
  191. return 0;
  192. }
  193. int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
  194. {
  195. struct drm_i915_private *dev_priv = to_i915(dev);
  196. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  197. if (seqno == 0)
  198. return -EINVAL;
  199. /* HWS page needs to be set less than what we
  200. * will inject to ring
  201. */
  202. return reset_all_global_seqno(dev_priv, seqno - 1);
  203. }
  204. static int reserve_seqno(struct intel_engine_cs *engine)
  205. {
  206. u32 active = ++engine->timeline->inflight_seqnos;
  207. u32 seqno = engine->timeline->seqno;
  208. int ret;
  209. /* Reservation is fine until we need to wrap around */
  210. if (likely(!add_overflows(seqno, active)))
  211. return 0;
  212. ret = reset_all_global_seqno(engine->i915, 0);
  213. if (ret) {
  214. engine->timeline->inflight_seqnos--;
  215. return ret;
  216. }
  217. return 0;
  218. }
  219. static void unreserve_seqno(struct intel_engine_cs *engine)
  220. {
  221. GEM_BUG_ON(!engine->timeline->inflight_seqnos);
  222. engine->timeline->inflight_seqnos--;
  223. }
  224. void i915_gem_retire_noop(struct i915_gem_active *active,
  225. struct drm_i915_gem_request *request)
  226. {
  227. /* Space left intentionally blank */
  228. }
  229. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  230. {
  231. struct intel_engine_cs *engine = request->engine;
  232. struct i915_gem_active *active, *next;
  233. lockdep_assert_held(&request->i915->drm.struct_mutex);
  234. GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit));
  235. GEM_BUG_ON(!i915_gem_request_completed(request));
  236. GEM_BUG_ON(!request->i915->gt.active_requests);
  237. trace_i915_gem_request_retire(request);
  238. spin_lock_irq(&engine->timeline->lock);
  239. list_del_init(&request->link);
  240. spin_unlock_irq(&engine->timeline->lock);
  241. /* We know the GPU must have read the request to have
  242. * sent us the seqno + interrupt, so use the position
  243. * of tail of the request to update the last known position
  244. * of the GPU head.
  245. *
  246. * Note this requires that we are always called in request
  247. * completion order.
  248. */
  249. list_del(&request->ring_link);
  250. request->ring->head = request->postfix;
  251. if (!--request->i915->gt.active_requests) {
  252. GEM_BUG_ON(!request->i915->gt.awake);
  253. mod_delayed_work(request->i915->wq,
  254. &request->i915->gt.idle_work,
  255. msecs_to_jiffies(100));
  256. }
  257. unreserve_seqno(request->engine);
  258. /* Walk through the active list, calling retire on each. This allows
  259. * objects to track their GPU activity and mark themselves as idle
  260. * when their *last* active request is completed (updating state
  261. * tracking lists for eviction, active references for GEM, etc).
  262. *
  263. * As the ->retire() may free the node, we decouple it first and
  264. * pass along the auxiliary information (to avoid dereferencing
  265. * the node after the callback).
  266. */
  267. list_for_each_entry_safe(active, next, &request->active_list, link) {
  268. /* In microbenchmarks or focusing upon time inside the kernel,
  269. * we may spend an inordinate amount of time simply handling
  270. * the retirement of requests and processing their callbacks.
  271. * Of which, this loop itself is particularly hot due to the
  272. * cache misses when jumping around the list of i915_gem_active.
  273. * So we try to keep this loop as streamlined as possible and
  274. * also prefetch the next i915_gem_active to try and hide
  275. * the likely cache miss.
  276. */
  277. prefetchw(next);
  278. INIT_LIST_HEAD(&active->link);
  279. RCU_INIT_POINTER(active->request, NULL);
  280. active->retire(active, request);
  281. }
  282. i915_gem_request_remove_from_client(request);
  283. /* Retirement decays the ban score as it is a sign of ctx progress */
  284. if (request->ctx->ban_score > 0)
  285. request->ctx->ban_score--;
  286. /* The backing object for the context is done after switching to the
  287. * *next* context. Therefore we cannot retire the previous context until
  288. * the next context has already started running. However, since we
  289. * cannot take the required locks at i915_gem_request_submit() we
  290. * defer the unpinning of the active context to now, retirement of
  291. * the subsequent request.
  292. */
  293. if (engine->last_retired_context)
  294. engine->context_unpin(engine, engine->last_retired_context);
  295. engine->last_retired_context = request->ctx;
  296. dma_fence_signal(&request->fence);
  297. i915_priotree_fini(request->i915, &request->priotree);
  298. i915_gem_request_put(request);
  299. }
  300. void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
  301. {
  302. struct intel_engine_cs *engine = req->engine;
  303. struct drm_i915_gem_request *tmp;
  304. lockdep_assert_held(&req->i915->drm.struct_mutex);
  305. GEM_BUG_ON(!i915_gem_request_completed(req));
  306. if (list_empty(&req->link))
  307. return;
  308. do {
  309. tmp = list_first_entry(&engine->timeline->requests,
  310. typeof(*tmp), link);
  311. i915_gem_request_retire(tmp);
  312. } while (tmp != req);
  313. }
  314. static u32 timeline_get_seqno(struct intel_timeline *tl)
  315. {
  316. return ++tl->seqno;
  317. }
  318. void __i915_gem_request_submit(struct drm_i915_gem_request *request)
  319. {
  320. struct intel_engine_cs *engine = request->engine;
  321. struct intel_timeline *timeline;
  322. u32 seqno;
  323. GEM_BUG_ON(!irqs_disabled());
  324. lockdep_assert_held(&engine->timeline->lock);
  325. trace_i915_gem_request_execute(request);
  326. /* Transfer from per-context onto the global per-engine timeline */
  327. timeline = engine->timeline;
  328. GEM_BUG_ON(timeline == request->timeline);
  329. seqno = timeline_get_seqno(timeline);
  330. GEM_BUG_ON(!seqno);
  331. GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
  332. /* We may be recursing from the signal callback of another i915 fence */
  333. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  334. request->global_seqno = seqno;
  335. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  336. intel_engine_enable_signaling(request);
  337. spin_unlock(&request->lock);
  338. engine->emit_breadcrumb(request,
  339. request->ring->vaddr + request->postfix);
  340. spin_lock(&request->timeline->lock);
  341. list_move_tail(&request->link, &timeline->requests);
  342. spin_unlock(&request->timeline->lock);
  343. wake_up_all(&request->execute);
  344. }
  345. void i915_gem_request_submit(struct drm_i915_gem_request *request)
  346. {
  347. struct intel_engine_cs *engine = request->engine;
  348. unsigned long flags;
  349. /* Will be called from irq-context when using foreign fences. */
  350. spin_lock_irqsave(&engine->timeline->lock, flags);
  351. __i915_gem_request_submit(request);
  352. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  353. }
  354. void __i915_gem_request_unsubmit(struct drm_i915_gem_request *request)
  355. {
  356. struct intel_engine_cs *engine = request->engine;
  357. struct intel_timeline *timeline;
  358. GEM_BUG_ON(!irqs_disabled());
  359. lockdep_assert_held(&engine->timeline->lock);
  360. /* Only unwind in reverse order, required so that the per-context list
  361. * is kept in seqno/ring order.
  362. */
  363. GEM_BUG_ON(request->global_seqno != engine->timeline->seqno);
  364. engine->timeline->seqno--;
  365. /* We may be recursing from the signal callback of another i915 fence */
  366. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  367. request->global_seqno = 0;
  368. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  369. intel_engine_cancel_signaling(request);
  370. spin_unlock(&request->lock);
  371. /* Transfer back from the global per-engine timeline to per-context */
  372. timeline = request->timeline;
  373. GEM_BUG_ON(timeline == engine->timeline);
  374. spin_lock(&timeline->lock);
  375. list_move(&request->link, &timeline->requests);
  376. spin_unlock(&timeline->lock);
  377. /* We don't need to wake_up any waiters on request->execute, they
  378. * will get woken by any other event or us re-adding this request
  379. * to the engine timeline (__i915_gem_request_submit()). The waiters
  380. * should be quite adapt at finding that the request now has a new
  381. * global_seqno to the one they went to sleep on.
  382. */
  383. }
  384. void i915_gem_request_unsubmit(struct drm_i915_gem_request *request)
  385. {
  386. struct intel_engine_cs *engine = request->engine;
  387. unsigned long flags;
  388. /* Will be called from irq-context when using foreign fences. */
  389. spin_lock_irqsave(&engine->timeline->lock, flags);
  390. __i915_gem_request_unsubmit(request);
  391. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  392. }
  393. static int __i915_sw_fence_call
  394. submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
  395. {
  396. struct drm_i915_gem_request *request =
  397. container_of(fence, typeof(*request), submit);
  398. switch (state) {
  399. case FENCE_COMPLETE:
  400. trace_i915_gem_request_submit(request);
  401. request->engine->submit_request(request);
  402. break;
  403. case FENCE_FREE:
  404. i915_gem_request_put(request);
  405. break;
  406. }
  407. return NOTIFY_DONE;
  408. }
  409. /**
  410. * i915_gem_request_alloc - allocate a request structure
  411. *
  412. * @engine: engine that we wish to issue the request on.
  413. * @ctx: context that the request will be associated with.
  414. * This can be NULL if the request is not directly related to
  415. * any specific user context, in which case this function will
  416. * choose an appropriate context to use.
  417. *
  418. * Returns a pointer to the allocated request if successful,
  419. * or an error code if not.
  420. */
  421. struct drm_i915_gem_request *
  422. i915_gem_request_alloc(struct intel_engine_cs *engine,
  423. struct i915_gem_context *ctx)
  424. {
  425. struct drm_i915_private *dev_priv = engine->i915;
  426. struct drm_i915_gem_request *req;
  427. int ret;
  428. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  429. /* ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  430. * EIO if the GPU is already wedged.
  431. */
  432. if (i915_terminally_wedged(&dev_priv->gpu_error))
  433. return ERR_PTR(-EIO);
  434. /* Pinning the contexts may generate requests in order to acquire
  435. * GGTT space, so do this first before we reserve a seqno for
  436. * ourselves.
  437. */
  438. ret = engine->context_pin(engine, ctx);
  439. if (ret)
  440. return ERR_PTR(ret);
  441. ret = reserve_seqno(engine);
  442. if (ret)
  443. goto err_unpin;
  444. /* Move the oldest request to the slab-cache (if not in use!) */
  445. req = list_first_entry_or_null(&engine->timeline->requests,
  446. typeof(*req), link);
  447. if (req && i915_gem_request_completed(req))
  448. i915_gem_request_retire(req);
  449. /* Beware: Dragons be flying overhead.
  450. *
  451. * We use RCU to look up requests in flight. The lookups may
  452. * race with the request being allocated from the slab freelist.
  453. * That is the request we are writing to here, may be in the process
  454. * of being read by __i915_gem_active_get_rcu(). As such,
  455. * we have to be very careful when overwriting the contents. During
  456. * the RCU lookup, we change chase the request->engine pointer,
  457. * read the request->global_seqno and increment the reference count.
  458. *
  459. * The reference count is incremented atomically. If it is zero,
  460. * the lookup knows the request is unallocated and complete. Otherwise,
  461. * it is either still in use, or has been reallocated and reset
  462. * with dma_fence_init(). This increment is safe for release as we
  463. * check that the request we have a reference to and matches the active
  464. * request.
  465. *
  466. * Before we increment the refcount, we chase the request->engine
  467. * pointer. We must not call kmem_cache_zalloc() or else we set
  468. * that pointer to NULL and cause a crash during the lookup. If
  469. * we see the request is completed (based on the value of the
  470. * old engine and seqno), the lookup is complete and reports NULL.
  471. * If we decide the request is not completed (new engine or seqno),
  472. * then we grab a reference and double check that it is still the
  473. * active request - which it won't be and restart the lookup.
  474. *
  475. * Do not use kmem_cache_zalloc() here!
  476. */
  477. req = kmem_cache_alloc(dev_priv->requests, GFP_KERNEL);
  478. if (!req) {
  479. ret = -ENOMEM;
  480. goto err_unreserve;
  481. }
  482. req->timeline = i915_gem_context_lookup_timeline(ctx, engine);
  483. GEM_BUG_ON(req->timeline == engine->timeline);
  484. spin_lock_init(&req->lock);
  485. dma_fence_init(&req->fence,
  486. &i915_fence_ops,
  487. &req->lock,
  488. req->timeline->fence_context,
  489. timeline_get_seqno(req->timeline));
  490. /* We bump the ref for the fence chain */
  491. i915_sw_fence_init(&i915_gem_request_get(req)->submit, submit_notify);
  492. init_waitqueue_head(&req->execute);
  493. i915_priotree_init(&req->priotree);
  494. INIT_LIST_HEAD(&req->active_list);
  495. req->i915 = dev_priv;
  496. req->engine = engine;
  497. req->ctx = ctx;
  498. /* No zalloc, must clear what we need by hand */
  499. req->global_seqno = 0;
  500. req->file_priv = NULL;
  501. req->batch = NULL;
  502. /*
  503. * Reserve space in the ring buffer for all the commands required to
  504. * eventually emit this request. This is to guarantee that the
  505. * i915_add_request() call can't fail. Note that the reserve may need
  506. * to be redone if the request is not actually submitted straight
  507. * away, e.g. because a GPU scheduler has deferred it.
  508. */
  509. req->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  510. GEM_BUG_ON(req->reserved_space < engine->emit_breadcrumb_sz);
  511. ret = engine->request_alloc(req);
  512. if (ret)
  513. goto err_ctx;
  514. /* Record the position of the start of the request so that
  515. * should we detect the updated seqno part-way through the
  516. * GPU processing the request, we never over-estimate the
  517. * position of the head.
  518. */
  519. req->head = req->ring->emit;
  520. /* Check that we didn't interrupt ourselves with a new request */
  521. GEM_BUG_ON(req->timeline->seqno != req->fence.seqno);
  522. return req;
  523. err_ctx:
  524. /* Make sure we didn't add ourselves to external state before freeing */
  525. GEM_BUG_ON(!list_empty(&req->active_list));
  526. GEM_BUG_ON(!list_empty(&req->priotree.signalers_list));
  527. GEM_BUG_ON(!list_empty(&req->priotree.waiters_list));
  528. kmem_cache_free(dev_priv->requests, req);
  529. err_unreserve:
  530. unreserve_seqno(engine);
  531. err_unpin:
  532. engine->context_unpin(engine, ctx);
  533. return ERR_PTR(ret);
  534. }
  535. static int
  536. i915_gem_request_await_request(struct drm_i915_gem_request *to,
  537. struct drm_i915_gem_request *from)
  538. {
  539. u32 seqno;
  540. int ret;
  541. GEM_BUG_ON(to == from);
  542. if (i915_gem_request_completed(from))
  543. return 0;
  544. if (to->engine->schedule) {
  545. ret = i915_priotree_add_dependency(to->i915,
  546. &to->priotree,
  547. &from->priotree);
  548. if (ret < 0)
  549. return ret;
  550. }
  551. if (to->timeline == from->timeline)
  552. return 0;
  553. if (to->engine == from->engine) {
  554. ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
  555. &from->submit,
  556. GFP_KERNEL);
  557. return ret < 0 ? ret : 0;
  558. }
  559. seqno = i915_gem_request_global_seqno(from);
  560. if (!seqno) {
  561. ret = i915_sw_fence_await_dma_fence(&to->submit,
  562. &from->fence, 0,
  563. GFP_KERNEL);
  564. return ret < 0 ? ret : 0;
  565. }
  566. if (seqno <= to->timeline->sync_seqno[from->engine->id])
  567. return 0;
  568. trace_i915_gem_ring_sync_to(to, from);
  569. if (!i915.semaphores) {
  570. if (!i915_spin_request(from, TASK_INTERRUPTIBLE, 2)) {
  571. ret = i915_sw_fence_await_dma_fence(&to->submit,
  572. &from->fence, 0,
  573. GFP_KERNEL);
  574. if (ret < 0)
  575. return ret;
  576. }
  577. } else {
  578. ret = to->engine->semaphore.sync_to(to, from);
  579. if (ret)
  580. return ret;
  581. }
  582. to->timeline->sync_seqno[from->engine->id] = seqno;
  583. return 0;
  584. }
  585. int
  586. i915_gem_request_await_dma_fence(struct drm_i915_gem_request *req,
  587. struct dma_fence *fence)
  588. {
  589. struct dma_fence_array *array;
  590. int ret;
  591. int i;
  592. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  593. return 0;
  594. if (dma_fence_is_i915(fence))
  595. return i915_gem_request_await_request(req, to_request(fence));
  596. if (!dma_fence_is_array(fence)) {
  597. ret = i915_sw_fence_await_dma_fence(&req->submit,
  598. fence, I915_FENCE_TIMEOUT,
  599. GFP_KERNEL);
  600. return ret < 0 ? ret : 0;
  601. }
  602. /* Note that if the fence-array was created in signal-on-any mode,
  603. * we should *not* decompose it into its individual fences. However,
  604. * we don't currently store which mode the fence-array is operating
  605. * in. Fortunately, the only user of signal-on-any is private to
  606. * amdgpu and we should not see any incoming fence-array from
  607. * sync-file being in signal-on-any mode.
  608. */
  609. array = to_dma_fence_array(fence);
  610. for (i = 0; i < array->num_fences; i++) {
  611. struct dma_fence *child = array->fences[i];
  612. if (dma_fence_is_i915(child))
  613. ret = i915_gem_request_await_request(req,
  614. to_request(child));
  615. else
  616. ret = i915_sw_fence_await_dma_fence(&req->submit,
  617. child, I915_FENCE_TIMEOUT,
  618. GFP_KERNEL);
  619. if (ret < 0)
  620. return ret;
  621. }
  622. return 0;
  623. }
  624. /**
  625. * i915_gem_request_await_object - set this request to (async) wait upon a bo
  626. *
  627. * @to: request we are wishing to use
  628. * @obj: object which may be in use on another ring.
  629. *
  630. * This code is meant to abstract object synchronization with the GPU.
  631. * Conceptually we serialise writes between engines inside the GPU.
  632. * We only allow one engine to write into a buffer at any time, but
  633. * multiple readers. To ensure each has a coherent view of memory, we must:
  634. *
  635. * - If there is an outstanding write request to the object, the new
  636. * request must wait for it to complete (either CPU or in hw, requests
  637. * on the same ring will be naturally ordered).
  638. *
  639. * - If we are a write request (pending_write_domain is set), the new
  640. * request must wait for outstanding read requests to complete.
  641. *
  642. * Returns 0 if successful, else propagates up the lower layer error.
  643. */
  644. int
  645. i915_gem_request_await_object(struct drm_i915_gem_request *to,
  646. struct drm_i915_gem_object *obj,
  647. bool write)
  648. {
  649. struct dma_fence *excl;
  650. int ret = 0;
  651. if (write) {
  652. struct dma_fence **shared;
  653. unsigned int count, i;
  654. ret = reservation_object_get_fences_rcu(obj->resv,
  655. &excl, &count, &shared);
  656. if (ret)
  657. return ret;
  658. for (i = 0; i < count; i++) {
  659. ret = i915_gem_request_await_dma_fence(to, shared[i]);
  660. if (ret)
  661. break;
  662. dma_fence_put(shared[i]);
  663. }
  664. for (; i < count; i++)
  665. dma_fence_put(shared[i]);
  666. kfree(shared);
  667. } else {
  668. excl = reservation_object_get_excl_rcu(obj->resv);
  669. }
  670. if (excl) {
  671. if (ret == 0)
  672. ret = i915_gem_request_await_dma_fence(to, excl);
  673. dma_fence_put(excl);
  674. }
  675. return ret;
  676. }
  677. static void i915_gem_mark_busy(const struct intel_engine_cs *engine)
  678. {
  679. struct drm_i915_private *dev_priv = engine->i915;
  680. if (dev_priv->gt.awake)
  681. return;
  682. GEM_BUG_ON(!dev_priv->gt.active_requests);
  683. intel_runtime_pm_get_noresume(dev_priv);
  684. dev_priv->gt.awake = true;
  685. intel_enable_gt_powersave(dev_priv);
  686. i915_update_gfx_val(dev_priv);
  687. if (INTEL_GEN(dev_priv) >= 6)
  688. gen6_rps_busy(dev_priv);
  689. queue_delayed_work(dev_priv->wq,
  690. &dev_priv->gt.retire_work,
  691. round_jiffies_up_relative(HZ));
  692. }
  693. /*
  694. * NB: This function is not allowed to fail. Doing so would mean the the
  695. * request is not being tracked for completion but the work itself is
  696. * going to happen on the hardware. This would be a Bad Thing(tm).
  697. */
  698. void __i915_add_request(struct drm_i915_gem_request *request, bool flush_caches)
  699. {
  700. struct intel_engine_cs *engine = request->engine;
  701. struct intel_ring *ring = request->ring;
  702. struct intel_timeline *timeline = request->timeline;
  703. struct drm_i915_gem_request *prev;
  704. u32 *cs;
  705. int err;
  706. lockdep_assert_held(&request->i915->drm.struct_mutex);
  707. trace_i915_gem_request_add(request);
  708. /* Make sure that no request gazumped us - if it was allocated after
  709. * our i915_gem_request_alloc() and called __i915_add_request() before
  710. * us, the timeline will hold its seqno which is later than ours.
  711. */
  712. GEM_BUG_ON(timeline->seqno != request->fence.seqno);
  713. /*
  714. * To ensure that this call will not fail, space for its emissions
  715. * should already have been reserved in the ring buffer. Let the ring
  716. * know that it is time to use that space up.
  717. */
  718. request->reserved_space = 0;
  719. /*
  720. * Emit any outstanding flushes - execbuf can fail to emit the flush
  721. * after having emitted the batchbuffer command. Hence we need to fix
  722. * things up similar to emitting the lazy request. The difference here
  723. * is that the flush _must_ happen before the next request, no matter
  724. * what.
  725. */
  726. if (flush_caches) {
  727. err = engine->emit_flush(request, EMIT_FLUSH);
  728. /* Not allowed to fail! */
  729. WARN(err, "engine->emit_flush() failed: %d!\n", err);
  730. }
  731. /* Record the position of the start of the breadcrumb so that
  732. * should we detect the updated seqno part-way through the
  733. * GPU processing the request, we never over-estimate the
  734. * position of the ring's HEAD.
  735. */
  736. cs = intel_ring_begin(request, engine->emit_breadcrumb_sz);
  737. GEM_BUG_ON(IS_ERR(cs));
  738. request->postfix = intel_ring_offset(request, cs);
  739. /* Seal the request and mark it as pending execution. Note that
  740. * we may inspect this state, without holding any locks, during
  741. * hangcheck. Hence we apply the barrier to ensure that we do not
  742. * see a more recent value in the hws than we are tracking.
  743. */
  744. prev = i915_gem_active_raw(&timeline->last_request,
  745. &request->i915->drm.struct_mutex);
  746. if (prev) {
  747. i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
  748. &request->submitq);
  749. if (engine->schedule)
  750. __i915_priotree_add_dependency(&request->priotree,
  751. &prev->priotree,
  752. &request->dep,
  753. 0);
  754. }
  755. spin_lock_irq(&timeline->lock);
  756. list_add_tail(&request->link, &timeline->requests);
  757. spin_unlock_irq(&timeline->lock);
  758. GEM_BUG_ON(timeline->seqno != request->fence.seqno);
  759. i915_gem_active_set(&timeline->last_request, request);
  760. list_add_tail(&request->ring_link, &ring->request_list);
  761. request->emitted_jiffies = jiffies;
  762. if (!request->i915->gt.active_requests++)
  763. i915_gem_mark_busy(engine);
  764. /* Let the backend know a new request has arrived that may need
  765. * to adjust the existing execution schedule due to a high priority
  766. * request - i.e. we may want to preempt the current request in order
  767. * to run a high priority dependency chain *before* we can execute this
  768. * request.
  769. *
  770. * This is called before the request is ready to run so that we can
  771. * decide whether to preempt the entire chain so that it is ready to
  772. * run at the earliest possible convenience.
  773. */
  774. if (engine->schedule)
  775. engine->schedule(request, request->ctx->priority);
  776. local_bh_disable();
  777. i915_sw_fence_commit(&request->submit);
  778. local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
  779. }
  780. static unsigned long local_clock_us(unsigned int *cpu)
  781. {
  782. unsigned long t;
  783. /* Cheaply and approximately convert from nanoseconds to microseconds.
  784. * The result and subsequent calculations are also defined in the same
  785. * approximate microseconds units. The principal source of timing
  786. * error here is from the simple truncation.
  787. *
  788. * Note that local_clock() is only defined wrt to the current CPU;
  789. * the comparisons are no longer valid if we switch CPUs. Instead of
  790. * blocking preemption for the entire busywait, we can detect the CPU
  791. * switch and use that as indicator of system load and a reason to
  792. * stop busywaiting, see busywait_stop().
  793. */
  794. *cpu = get_cpu();
  795. t = local_clock() >> 10;
  796. put_cpu();
  797. return t;
  798. }
  799. static bool busywait_stop(unsigned long timeout, unsigned int cpu)
  800. {
  801. unsigned int this_cpu;
  802. if (time_after(local_clock_us(&this_cpu), timeout))
  803. return true;
  804. return this_cpu != cpu;
  805. }
  806. bool __i915_spin_request(const struct drm_i915_gem_request *req,
  807. u32 seqno, int state, unsigned long timeout_us)
  808. {
  809. struct intel_engine_cs *engine = req->engine;
  810. unsigned int irq, cpu;
  811. /* When waiting for high frequency requests, e.g. during synchronous
  812. * rendering split between the CPU and GPU, the finite amount of time
  813. * required to set up the irq and wait upon it limits the response
  814. * rate. By busywaiting on the request completion for a short while we
  815. * can service the high frequency waits as quick as possible. However,
  816. * if it is a slow request, we want to sleep as quickly as possible.
  817. * The tradeoff between waiting and sleeping is roughly the time it
  818. * takes to sleep on a request, on the order of a microsecond.
  819. */
  820. irq = atomic_read(&engine->irq_count);
  821. timeout_us += local_clock_us(&cpu);
  822. do {
  823. if (seqno != i915_gem_request_global_seqno(req))
  824. break;
  825. if (i915_seqno_passed(intel_engine_get_seqno(req->engine),
  826. seqno))
  827. return true;
  828. /* Seqno are meant to be ordered *before* the interrupt. If
  829. * we see an interrupt without a corresponding seqno advance,
  830. * assume we won't see one in the near future but require
  831. * the engine->seqno_barrier() to fixup coherency.
  832. */
  833. if (atomic_read(&engine->irq_count) != irq)
  834. break;
  835. if (signal_pending_state(state, current))
  836. break;
  837. if (busywait_stop(timeout_us, cpu))
  838. break;
  839. cpu_relax();
  840. } while (!need_resched());
  841. return false;
  842. }
  843. static bool __i915_wait_request_check_and_reset(struct drm_i915_gem_request *request)
  844. {
  845. if (likely(!i915_reset_handoff(&request->i915->gpu_error)))
  846. return false;
  847. __set_current_state(TASK_RUNNING);
  848. i915_reset(request->i915);
  849. return true;
  850. }
  851. /**
  852. * i915_wait_request - wait until execution of request has finished
  853. * @req: the request to wait upon
  854. * @flags: how to wait
  855. * @timeout: how long to wait in jiffies
  856. *
  857. * i915_wait_request() waits for the request to be completed, for a
  858. * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
  859. * unbounded wait).
  860. *
  861. * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
  862. * in via the flags, and vice versa if the struct_mutex is not held, the caller
  863. * must not specify that the wait is locked.
  864. *
  865. * Returns the remaining time (in jiffies) if the request completed, which may
  866. * be zero or -ETIME if the request is unfinished after the timeout expires.
  867. * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
  868. * pending before the request completes.
  869. */
  870. long i915_wait_request(struct drm_i915_gem_request *req,
  871. unsigned int flags,
  872. long timeout)
  873. {
  874. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  875. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  876. wait_queue_head_t *errq = &req->i915->gpu_error.wait_queue;
  877. DEFINE_WAIT_FUNC(reset, default_wake_function);
  878. DEFINE_WAIT_FUNC(exec, default_wake_function);
  879. struct intel_wait wait;
  880. might_sleep();
  881. #if IS_ENABLED(CONFIG_LOCKDEP)
  882. GEM_BUG_ON(debug_locks &&
  883. !!lockdep_is_held(&req->i915->drm.struct_mutex) !=
  884. !!(flags & I915_WAIT_LOCKED));
  885. #endif
  886. GEM_BUG_ON(timeout < 0);
  887. if (i915_gem_request_completed(req))
  888. return timeout;
  889. if (!timeout)
  890. return -ETIME;
  891. trace_i915_gem_request_wait_begin(req, flags);
  892. add_wait_queue(&req->execute, &exec);
  893. if (flags & I915_WAIT_LOCKED)
  894. add_wait_queue(errq, &reset);
  895. intel_wait_init(&wait, req);
  896. restart:
  897. do {
  898. set_current_state(state);
  899. if (intel_wait_update_request(&wait, req))
  900. break;
  901. if (flags & I915_WAIT_LOCKED &&
  902. __i915_wait_request_check_and_reset(req))
  903. continue;
  904. if (signal_pending_state(state, current)) {
  905. timeout = -ERESTARTSYS;
  906. goto complete;
  907. }
  908. if (!timeout) {
  909. timeout = -ETIME;
  910. goto complete;
  911. }
  912. timeout = io_schedule_timeout(timeout);
  913. } while (1);
  914. GEM_BUG_ON(!intel_wait_has_seqno(&wait));
  915. GEM_BUG_ON(!i915_sw_fence_signaled(&req->submit));
  916. /* Optimistic short spin before touching IRQs */
  917. if (i915_spin_request(req, state, 5))
  918. goto complete;
  919. set_current_state(state);
  920. if (intel_engine_add_wait(req->engine, &wait))
  921. /* In order to check that we haven't missed the interrupt
  922. * as we enabled it, we need to kick ourselves to do a
  923. * coherent check on the seqno before we sleep.
  924. */
  925. goto wakeup;
  926. if (flags & I915_WAIT_LOCKED)
  927. __i915_wait_request_check_and_reset(req);
  928. for (;;) {
  929. if (signal_pending_state(state, current)) {
  930. timeout = -ERESTARTSYS;
  931. break;
  932. }
  933. if (!timeout) {
  934. timeout = -ETIME;
  935. break;
  936. }
  937. timeout = io_schedule_timeout(timeout);
  938. if (intel_wait_complete(&wait) &&
  939. intel_wait_check_request(&wait, req))
  940. break;
  941. set_current_state(state);
  942. wakeup:
  943. /* Carefully check if the request is complete, giving time
  944. * for the seqno to be visible following the interrupt.
  945. * We also have to check in case we are kicked by the GPU
  946. * reset in order to drop the struct_mutex.
  947. */
  948. if (__i915_request_irq_complete(req))
  949. break;
  950. /* If the GPU is hung, and we hold the lock, reset the GPU
  951. * and then check for completion. On a full reset, the engine's
  952. * HW seqno will be advanced passed us and we are complete.
  953. * If we do a partial reset, we have to wait for the GPU to
  954. * resume and update the breadcrumb.
  955. *
  956. * If we don't hold the mutex, we can just wait for the worker
  957. * to come along and update the breadcrumb (either directly
  958. * itself, or indirectly by recovering the GPU).
  959. */
  960. if (flags & I915_WAIT_LOCKED &&
  961. __i915_wait_request_check_and_reset(req))
  962. continue;
  963. /* Only spin if we know the GPU is processing this request */
  964. if (i915_spin_request(req, state, 2))
  965. break;
  966. if (!intel_wait_check_request(&wait, req)) {
  967. intel_engine_remove_wait(req->engine, &wait);
  968. goto restart;
  969. }
  970. }
  971. intel_engine_remove_wait(req->engine, &wait);
  972. complete:
  973. __set_current_state(TASK_RUNNING);
  974. if (flags & I915_WAIT_LOCKED)
  975. remove_wait_queue(errq, &reset);
  976. remove_wait_queue(&req->execute, &exec);
  977. trace_i915_gem_request_wait_end(req);
  978. return timeout;
  979. }
  980. static void engine_retire_requests(struct intel_engine_cs *engine)
  981. {
  982. struct drm_i915_gem_request *request, *next;
  983. u32 seqno = intel_engine_get_seqno(engine);
  984. LIST_HEAD(retire);
  985. spin_lock_irq(&engine->timeline->lock);
  986. list_for_each_entry_safe(request, next,
  987. &engine->timeline->requests, link) {
  988. if (!i915_seqno_passed(seqno, request->global_seqno))
  989. break;
  990. list_move_tail(&request->link, &retire);
  991. }
  992. spin_unlock_irq(&engine->timeline->lock);
  993. list_for_each_entry_safe(request, next, &retire, link)
  994. i915_gem_request_retire(request);
  995. }
  996. void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
  997. {
  998. struct intel_engine_cs *engine;
  999. enum intel_engine_id id;
  1000. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  1001. if (!dev_priv->gt.active_requests)
  1002. return;
  1003. for_each_engine(engine, dev_priv, id)
  1004. engine_retire_requests(engine);
  1005. }
  1006. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1007. #include "selftests/mock_request.c"
  1008. #include "selftests/i915_gem_request.c"
  1009. #endif