i915_gem_request.c 35 KB

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