i915_gem_request.c 33 KB

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