i915_gem_request.c 35 KB

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