i915_gem_request.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include <linux/prefetch.h>
  25. #include <linux/dma-fence-array.h>
  26. #include <linux/sched.h>
  27. #include <linux/sched/clock.h>
  28. #include <linux/sched/signal.h>
  29. #include "i915_drv.h"
  30. static const char *i915_fence_get_driver_name(struct dma_fence *fence)
  31. {
  32. return "i915";
  33. }
  34. static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
  35. {
  36. /* The timeline struct (as part of the ppgtt underneath a context)
  37. * may be freed when the request is no longer in use by the GPU.
  38. * We could extend the life of a context to beyond that of all
  39. * fences, possibly keeping the hw resource around indefinitely,
  40. * or we just give them a false name. Since
  41. * dma_fence_ops.get_timeline_name is a debug feature, the occasional
  42. * lie seems justifiable.
  43. */
  44. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  45. return "signaled";
  46. return to_request(fence)->timeline->common->name;
  47. }
  48. static bool i915_fence_signaled(struct dma_fence *fence)
  49. {
  50. return i915_gem_request_completed(to_request(fence));
  51. }
  52. static bool i915_fence_enable_signaling(struct dma_fence *fence)
  53. {
  54. if (i915_fence_signaled(fence))
  55. return false;
  56. intel_engine_enable_signaling(to_request(fence));
  57. return true;
  58. }
  59. static signed long i915_fence_wait(struct dma_fence *fence,
  60. bool interruptible,
  61. signed long timeout)
  62. {
  63. return i915_wait_request(to_request(fence), interruptible, timeout);
  64. }
  65. static void i915_fence_release(struct dma_fence *fence)
  66. {
  67. struct drm_i915_gem_request *req = to_request(fence);
  68. /* The request is put onto a RCU freelist (i.e. the address
  69. * is immediately reused), mark the fences as being freed now.
  70. * Otherwise the debugobjects for the fences are only marked as
  71. * freed when the slab cache itself is freed, and so we would get
  72. * caught trying to reuse dead objects.
  73. */
  74. i915_sw_fence_fini(&req->submit);
  75. kmem_cache_free(req->i915->requests, req);
  76. }
  77. const struct dma_fence_ops i915_fence_ops = {
  78. .get_driver_name = i915_fence_get_driver_name,
  79. .get_timeline_name = i915_fence_get_timeline_name,
  80. .enable_signaling = i915_fence_enable_signaling,
  81. .signaled = i915_fence_signaled,
  82. .wait = i915_fence_wait,
  83. .release = i915_fence_release,
  84. };
  85. static inline void
  86. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  87. {
  88. struct drm_i915_file_private *file_priv;
  89. file_priv = request->file_priv;
  90. if (!file_priv)
  91. return;
  92. spin_lock(&file_priv->mm.lock);
  93. if (request->file_priv) {
  94. list_del(&request->client_link);
  95. request->file_priv = NULL;
  96. }
  97. spin_unlock(&file_priv->mm.lock);
  98. }
  99. static struct i915_dependency *
  100. i915_dependency_alloc(struct drm_i915_private *i915)
  101. {
  102. return kmem_cache_alloc(i915->dependencies, GFP_KERNEL);
  103. }
  104. static void
  105. i915_dependency_free(struct drm_i915_private *i915,
  106. struct i915_dependency *dep)
  107. {
  108. kmem_cache_free(i915->dependencies, dep);
  109. }
  110. static void
  111. __i915_priotree_add_dependency(struct i915_priotree *pt,
  112. struct i915_priotree *signal,
  113. struct i915_dependency *dep,
  114. unsigned long flags)
  115. {
  116. INIT_LIST_HEAD(&dep->dfs_link);
  117. list_add(&dep->wait_link, &signal->waiters_list);
  118. list_add(&dep->signal_link, &pt->signalers_list);
  119. dep->signaler = signal;
  120. dep->flags = flags;
  121. }
  122. static int
  123. i915_priotree_add_dependency(struct drm_i915_private *i915,
  124. struct i915_priotree *pt,
  125. struct i915_priotree *signal)
  126. {
  127. struct i915_dependency *dep;
  128. dep = i915_dependency_alloc(i915);
  129. if (!dep)
  130. return -ENOMEM;
  131. __i915_priotree_add_dependency(pt, signal, dep, I915_DEPENDENCY_ALLOC);
  132. return 0;
  133. }
  134. static void
  135. i915_priotree_fini(struct drm_i915_private *i915, struct i915_priotree *pt)
  136. {
  137. struct i915_dependency *dep, *next;
  138. GEM_BUG_ON(!RB_EMPTY_NODE(&pt->node));
  139. /* Everyone we depended upon (the fences we wait to be signaled)
  140. * should retire before us and remove themselves from our list.
  141. * However, retirement is run independently on each timeline and
  142. * so we may be called out-of-order.
  143. */
  144. list_for_each_entry_safe(dep, next, &pt->signalers_list, signal_link) {
  145. list_del(&dep->wait_link);
  146. if (dep->flags & I915_DEPENDENCY_ALLOC)
  147. i915_dependency_free(i915, dep);
  148. }
  149. /* Remove ourselves from everyone who depends upon us */
  150. list_for_each_entry_safe(dep, next, &pt->waiters_list, wait_link) {
  151. list_del(&dep->signal_link);
  152. if (dep->flags & I915_DEPENDENCY_ALLOC)
  153. i915_dependency_free(i915, dep);
  154. }
  155. }
  156. static void
  157. i915_priotree_init(struct i915_priotree *pt)
  158. {
  159. INIT_LIST_HEAD(&pt->signalers_list);
  160. INIT_LIST_HEAD(&pt->waiters_list);
  161. RB_CLEAR_NODE(&pt->node);
  162. pt->priority = INT_MIN;
  163. }
  164. static int reset_all_global_seqno(struct drm_i915_private *i915, u32 seqno)
  165. {
  166. struct i915_gem_timeline *timeline = &i915->gt.global_timeline;
  167. struct intel_engine_cs *engine;
  168. enum intel_engine_id id;
  169. int ret;
  170. /* Carefully retire all requests without writing to the rings */
  171. ret = i915_gem_wait_for_idle(i915,
  172. I915_WAIT_INTERRUPTIBLE |
  173. I915_WAIT_LOCKED);
  174. if (ret)
  175. return ret;
  176. /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
  177. for_each_engine(engine, i915, id) {
  178. struct intel_timeline *tl = &timeline->engine[id];
  179. if (wait_for(intel_engine_is_idle(engine), 50))
  180. return -EBUSY;
  181. if (!i915_seqno_passed(seqno, tl->seqno)) {
  182. /* spin until threads are complete */
  183. while (intel_breadcrumbs_busy(engine))
  184. cond_resched();
  185. }
  186. /* Finally reset hw state */
  187. tl->seqno = seqno;
  188. intel_engine_init_global_seqno(engine, seqno);
  189. }
  190. list_for_each_entry(timeline, &i915->gt.timelines, link) {
  191. for_each_engine(engine, i915, id) {
  192. struct intel_timeline *tl = &timeline->engine[id];
  193. memset(tl->sync_seqno, 0, sizeof(tl->sync_seqno));
  194. }
  195. }
  196. return 0;
  197. }
  198. int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
  199. {
  200. struct drm_i915_private *dev_priv = to_i915(dev);
  201. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  202. if (seqno == 0)
  203. return -EINVAL;
  204. /* HWS page needs to be set less than what we
  205. * will inject to ring
  206. */
  207. return reset_all_global_seqno(dev_priv, seqno - 1);
  208. }
  209. static int reserve_seqno(struct intel_engine_cs *engine)
  210. {
  211. u32 active = ++engine->timeline->inflight_seqnos;
  212. u32 seqno = engine->timeline->seqno;
  213. int ret;
  214. /* Reservation is fine until we need to wrap around */
  215. if (likely(!add_overflows(seqno, active)))
  216. return 0;
  217. ret = reset_all_global_seqno(engine->i915, 0);
  218. if (ret) {
  219. engine->timeline->inflight_seqnos--;
  220. return ret;
  221. }
  222. return 0;
  223. }
  224. static void unreserve_seqno(struct intel_engine_cs *engine)
  225. {
  226. GEM_BUG_ON(!engine->timeline->inflight_seqnos);
  227. engine->timeline->inflight_seqnos--;
  228. }
  229. void i915_gem_retire_noop(struct i915_gem_active *active,
  230. struct drm_i915_gem_request *request)
  231. {
  232. /* Space left intentionally blank */
  233. }
  234. static void i915_gem_request_retire(struct drm_i915_gem_request *request)
  235. {
  236. struct intel_engine_cs *engine = request->engine;
  237. struct i915_gem_active *active, *next;
  238. lockdep_assert_held(&request->i915->drm.struct_mutex);
  239. GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit));
  240. GEM_BUG_ON(!i915_gem_request_completed(request));
  241. GEM_BUG_ON(!request->i915->gt.active_requests);
  242. trace_i915_gem_request_retire(request);
  243. spin_lock_irq(&engine->timeline->lock);
  244. list_del_init(&request->link);
  245. spin_unlock_irq(&engine->timeline->lock);
  246. /* We know the GPU must have read the request to have
  247. * sent us the seqno + interrupt, so use the position
  248. * of tail of the request to update the last known position
  249. * of the GPU head.
  250. *
  251. * Note this requires that we are always called in request
  252. * completion order.
  253. */
  254. list_del(&request->ring_link);
  255. request->ring->head = request->postfix;
  256. if (!--request->i915->gt.active_requests) {
  257. GEM_BUG_ON(!request->i915->gt.awake);
  258. mod_delayed_work(request->i915->wq,
  259. &request->i915->gt.idle_work,
  260. msecs_to_jiffies(100));
  261. }
  262. unreserve_seqno(request->engine);
  263. /* Walk through the active list, calling retire on each. This allows
  264. * objects to track their GPU activity and mark themselves as idle
  265. * when their *last* active request is completed (updating state
  266. * tracking lists for eviction, active references for GEM, etc).
  267. *
  268. * As the ->retire() may free the node, we decouple it first and
  269. * pass along the auxiliary information (to avoid dereferencing
  270. * the node after the callback).
  271. */
  272. list_for_each_entry_safe(active, next, &request->active_list, link) {
  273. /* In microbenchmarks or focusing upon time inside the kernel,
  274. * we may spend an inordinate amount of time simply handling
  275. * the retirement of requests and processing their callbacks.
  276. * Of which, this loop itself is particularly hot due to the
  277. * cache misses when jumping around the list of i915_gem_active.
  278. * So we try to keep this loop as streamlined as possible and
  279. * also prefetch the next i915_gem_active to try and hide
  280. * the likely cache miss.
  281. */
  282. prefetchw(next);
  283. INIT_LIST_HEAD(&active->link);
  284. RCU_INIT_POINTER(active->request, NULL);
  285. active->retire(active, request);
  286. }
  287. i915_gem_request_remove_from_client(request);
  288. /* Retirement decays the ban score as it is a sign of ctx progress */
  289. if (request->ctx->ban_score > 0)
  290. request->ctx->ban_score--;
  291. /* The backing object for the context is done after switching to the
  292. * *next* context. Therefore we cannot retire the previous context until
  293. * the next context has already started running. However, since we
  294. * cannot take the required locks at i915_gem_request_submit() we
  295. * defer the unpinning of the active context to now, retirement of
  296. * the subsequent request.
  297. */
  298. if (engine->last_retired_context)
  299. engine->context_unpin(engine, engine->last_retired_context);
  300. engine->last_retired_context = request->ctx;
  301. dma_fence_signal(&request->fence);
  302. i915_priotree_fini(request->i915, &request->priotree);
  303. i915_gem_request_put(request);
  304. }
  305. void i915_gem_request_retire_upto(struct drm_i915_gem_request *req)
  306. {
  307. struct intel_engine_cs *engine = req->engine;
  308. struct drm_i915_gem_request *tmp;
  309. lockdep_assert_held(&req->i915->drm.struct_mutex);
  310. GEM_BUG_ON(!i915_gem_request_completed(req));
  311. if (list_empty(&req->link))
  312. return;
  313. do {
  314. tmp = list_first_entry(&engine->timeline->requests,
  315. typeof(*tmp), link);
  316. i915_gem_request_retire(tmp);
  317. } while (tmp != req);
  318. }
  319. static u32 timeline_get_seqno(struct intel_timeline *tl)
  320. {
  321. return ++tl->seqno;
  322. }
  323. void __i915_gem_request_submit(struct drm_i915_gem_request *request)
  324. {
  325. struct intel_engine_cs *engine = request->engine;
  326. struct intel_timeline *timeline;
  327. u32 seqno;
  328. GEM_BUG_ON(!irqs_disabled());
  329. lockdep_assert_held(&engine->timeline->lock);
  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. seqno = timeline_get_seqno(timeline);
  335. GEM_BUG_ON(!seqno);
  336. GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
  337. /* We may be recursing from the signal callback of another i915 fence */
  338. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  339. request->global_seqno = seqno;
  340. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  341. intel_engine_enable_signaling(request);
  342. spin_unlock(&request->lock);
  343. engine->emit_breadcrumb(request,
  344. request->ring->vaddr + request->postfix);
  345. spin_lock(&request->timeline->lock);
  346. list_move_tail(&request->link, &timeline->requests);
  347. spin_unlock(&request->timeline->lock);
  348. wake_up_all(&request->execute);
  349. }
  350. void i915_gem_request_submit(struct drm_i915_gem_request *request)
  351. {
  352. struct intel_engine_cs *engine = request->engine;
  353. unsigned long flags;
  354. /* Will be called from irq-context when using foreign fences. */
  355. spin_lock_irqsave(&engine->timeline->lock, flags);
  356. __i915_gem_request_submit(request);
  357. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  358. }
  359. void __i915_gem_request_unsubmit(struct drm_i915_gem_request *request)
  360. {
  361. struct intel_engine_cs *engine = request->engine;
  362. struct intel_timeline *timeline;
  363. GEM_BUG_ON(!irqs_disabled());
  364. lockdep_assert_held(&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_handoff(&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, req);
  899. restart:
  900. do {
  901. set_current_state(state);
  902. if (intel_wait_update_request(&wait, req))
  903. break;
  904. if (flags & I915_WAIT_LOCKED &&
  905. __i915_wait_request_check_and_reset(req))
  906. continue;
  907. if (signal_pending_state(state, current)) {
  908. timeout = -ERESTARTSYS;
  909. goto complete;
  910. }
  911. if (!timeout) {
  912. timeout = -ETIME;
  913. goto complete;
  914. }
  915. timeout = io_schedule_timeout(timeout);
  916. } while (1);
  917. GEM_BUG_ON(!intel_wait_has_seqno(&wait));
  918. GEM_BUG_ON(!i915_sw_fence_signaled(&req->submit));
  919. /* Optimistic short spin before touching IRQs */
  920. if (i915_spin_request(req, state, 5))
  921. goto complete;
  922. set_current_state(state);
  923. if (intel_engine_add_wait(req->engine, &wait))
  924. /* In order to check that we haven't missed the interrupt
  925. * as we enabled it, we need to kick ourselves to do a
  926. * coherent check on the seqno before we sleep.
  927. */
  928. goto wakeup;
  929. if (flags & I915_WAIT_LOCKED)
  930. __i915_wait_request_check_and_reset(req);
  931. for (;;) {
  932. if (signal_pending_state(state, current)) {
  933. timeout = -ERESTARTSYS;
  934. break;
  935. }
  936. if (!timeout) {
  937. timeout = -ETIME;
  938. break;
  939. }
  940. timeout = io_schedule_timeout(timeout);
  941. if (intel_wait_complete(&wait) &&
  942. intel_wait_check_request(&wait, req))
  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_wait_request_check_and_reset(req))
  965. continue;
  966. /* Only spin if we know the GPU is processing this request */
  967. if (i915_spin_request(req, state, 2))
  968. break;
  969. if (!intel_wait_check_request(&wait, req)) {
  970. intel_engine_remove_wait(req->engine, &wait);
  971. goto restart;
  972. }
  973. }
  974. intel_engine_remove_wait(req->engine, &wait);
  975. complete:
  976. __set_current_state(TASK_RUNNING);
  977. if (flags & I915_WAIT_LOCKED)
  978. remove_wait_queue(errq, &reset);
  979. remove_wait_queue(&req->execute, &exec);
  980. trace_i915_gem_request_wait_end(req);
  981. return timeout;
  982. }
  983. static void engine_retire_requests(struct intel_engine_cs *engine)
  984. {
  985. struct drm_i915_gem_request *request, *next;
  986. u32 seqno = intel_engine_get_seqno(engine);
  987. LIST_HEAD(retire);
  988. spin_lock_irq(&engine->timeline->lock);
  989. list_for_each_entry_safe(request, next,
  990. &engine->timeline->requests, link) {
  991. if (!i915_seqno_passed(seqno, request->global_seqno))
  992. break;
  993. list_move_tail(&request->link, &retire);
  994. }
  995. spin_unlock_irq(&engine->timeline->lock);
  996. list_for_each_entry_safe(request, next, &retire, link)
  997. i915_gem_request_retire(request);
  998. }
  999. void i915_gem_retire_requests(struct drm_i915_private *dev_priv)
  1000. {
  1001. struct intel_engine_cs *engine;
  1002. enum intel_engine_id id;
  1003. lockdep_assert_held(&dev_priv->drm.struct_mutex);
  1004. if (!dev_priv->gt.active_requests)
  1005. return;
  1006. for_each_engine(engine, dev_priv, id)
  1007. engine_retire_requests(engine);
  1008. }
  1009. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1010. #include "selftests/mock_request.c"
  1011. #include "selftests/i915_gem_request.c"
  1012. #endif