i915_gem_request.c 37 KB

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