i915_gem_request.c 35 KB

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