i915_gem_request.c 34 KB

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