i915_gem_request.c 33 KB

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