i915_request.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /*
  2. * Copyright © 2008-2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include <linux/prefetch.h>
  25. #include <linux/dma-fence-array.h>
  26. #include <linux/sched.h>
  27. #include <linux/sched/clock.h>
  28. #include <linux/sched/signal.h>
  29. #include "i915_drv.h"
  30. static const char *i915_fence_get_driver_name(struct dma_fence *fence)
  31. {
  32. return "i915";
  33. }
  34. static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
  35. {
  36. /*
  37. * The timeline struct (as part of the ppgtt underneath a context)
  38. * may be freed when the request is no longer in use by the GPU.
  39. * We could extend the life of a context to beyond that of all
  40. * fences, possibly keeping the hw resource around indefinitely,
  41. * or we just give them a false name. Since
  42. * dma_fence_ops.get_timeline_name is a debug feature, the occasional
  43. * lie seems justifiable.
  44. */
  45. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  46. return "signaled";
  47. return to_request(fence)->timeline->name;
  48. }
  49. static bool i915_fence_signaled(struct dma_fence *fence)
  50. {
  51. return i915_request_completed(to_request(fence));
  52. }
  53. static bool i915_fence_enable_signaling(struct dma_fence *fence)
  54. {
  55. return intel_engine_enable_signaling(to_request(fence), true);
  56. }
  57. static signed long i915_fence_wait(struct dma_fence *fence,
  58. bool interruptible,
  59. signed long timeout)
  60. {
  61. return i915_request_wait(to_request(fence), interruptible, timeout);
  62. }
  63. static void i915_fence_release(struct dma_fence *fence)
  64. {
  65. struct i915_request *rq = to_request(fence);
  66. /*
  67. * The request is put onto a RCU freelist (i.e. the address
  68. * is immediately reused), mark the fences as being freed now.
  69. * Otherwise the debugobjects for the fences are only marked as
  70. * freed when the slab cache itself is freed, and so we would get
  71. * caught trying to reuse dead objects.
  72. */
  73. i915_sw_fence_fini(&rq->submit);
  74. kmem_cache_free(rq->i915->requests, rq);
  75. }
  76. const struct dma_fence_ops i915_fence_ops = {
  77. .get_driver_name = i915_fence_get_driver_name,
  78. .get_timeline_name = i915_fence_get_timeline_name,
  79. .enable_signaling = i915_fence_enable_signaling,
  80. .signaled = i915_fence_signaled,
  81. .wait = i915_fence_wait,
  82. .release = i915_fence_release,
  83. };
  84. static inline void
  85. i915_request_remove_from_client(struct i915_request *request)
  86. {
  87. struct drm_i915_file_private *file_priv;
  88. file_priv = request->file_priv;
  89. if (!file_priv)
  90. return;
  91. spin_lock(&file_priv->mm.lock);
  92. if (request->file_priv) {
  93. list_del(&request->client_link);
  94. request->file_priv = NULL;
  95. }
  96. spin_unlock(&file_priv->mm.lock);
  97. }
  98. static struct i915_dependency *
  99. i915_dependency_alloc(struct drm_i915_private *i915)
  100. {
  101. return kmem_cache_alloc(i915->dependencies, GFP_KERNEL);
  102. }
  103. static void
  104. i915_dependency_free(struct drm_i915_private *i915,
  105. struct i915_dependency *dep)
  106. {
  107. kmem_cache_free(i915->dependencies, dep);
  108. }
  109. static void
  110. __i915_sched_node_add_dependency(struct i915_sched_node *node,
  111. struct i915_sched_node *signal,
  112. struct i915_dependency *dep,
  113. unsigned long flags)
  114. {
  115. INIT_LIST_HEAD(&dep->dfs_link);
  116. list_add(&dep->wait_link, &signal->waiters_list);
  117. list_add(&dep->signal_link, &node->signalers_list);
  118. dep->signaler = signal;
  119. dep->flags = flags;
  120. }
  121. static int
  122. i915_sched_node_add_dependency(struct drm_i915_private *i915,
  123. struct i915_sched_node *node,
  124. struct i915_sched_node *signal)
  125. {
  126. struct i915_dependency *dep;
  127. dep = i915_dependency_alloc(i915);
  128. if (!dep)
  129. return -ENOMEM;
  130. __i915_sched_node_add_dependency(node, signal, dep,
  131. I915_DEPENDENCY_ALLOC);
  132. return 0;
  133. }
  134. static void
  135. i915_sched_node_fini(struct drm_i915_private *i915,
  136. struct i915_sched_node *node)
  137. {
  138. struct i915_dependency *dep, *tmp;
  139. GEM_BUG_ON(!list_empty(&node->link));
  140. /*
  141. * Everyone we depended upon (the fences we wait to be signaled)
  142. * should retire before us and remove themselves from our list.
  143. * However, retirement is run independently on each timeline and
  144. * so we may be called out-of-order.
  145. */
  146. list_for_each_entry_safe(dep, tmp, &node->signalers_list, signal_link) {
  147. GEM_BUG_ON(!i915_sched_node_signaled(dep->signaler));
  148. GEM_BUG_ON(!list_empty(&dep->dfs_link));
  149. list_del(&dep->wait_link);
  150. if (dep->flags & I915_DEPENDENCY_ALLOC)
  151. i915_dependency_free(i915, dep);
  152. }
  153. /* Remove ourselves from everyone who depends upon us */
  154. list_for_each_entry_safe(dep, tmp, &node->waiters_list, wait_link) {
  155. GEM_BUG_ON(dep->signaler != node);
  156. GEM_BUG_ON(!list_empty(&dep->dfs_link));
  157. list_del(&dep->signal_link);
  158. if (dep->flags & I915_DEPENDENCY_ALLOC)
  159. i915_dependency_free(i915, dep);
  160. }
  161. }
  162. static void
  163. i915_sched_node_init(struct i915_sched_node *node)
  164. {
  165. INIT_LIST_HEAD(&node->signalers_list);
  166. INIT_LIST_HEAD(&node->waiters_list);
  167. INIT_LIST_HEAD(&node->link);
  168. node->attr.priority = I915_PRIORITY_INVALID;
  169. }
  170. static int reset_all_global_seqno(struct drm_i915_private *i915, u32 seqno)
  171. {
  172. struct intel_engine_cs *engine;
  173. struct i915_timeline *timeline;
  174. enum intel_engine_id id;
  175. int ret;
  176. /* Carefully retire all requests without writing to the rings */
  177. ret = i915_gem_wait_for_idle(i915,
  178. I915_WAIT_INTERRUPTIBLE |
  179. I915_WAIT_LOCKED);
  180. if (ret)
  181. return ret;
  182. GEM_BUG_ON(i915->gt.active_requests);
  183. /* If the seqno wraps around, we need to clear the breadcrumb rbtree */
  184. for_each_engine(engine, i915, id) {
  185. GEM_TRACE("%s seqno %d (current %d) -> %d\n",
  186. engine->name,
  187. engine->timeline.seqno,
  188. intel_engine_get_seqno(engine),
  189. seqno);
  190. if (!i915_seqno_passed(seqno, engine->timeline.seqno)) {
  191. /* Flush any waiters before we reuse the seqno */
  192. intel_engine_disarm_breadcrumbs(engine);
  193. intel_engine_init_hangcheck(engine);
  194. GEM_BUG_ON(!list_empty(&engine->breadcrumbs.signals));
  195. }
  196. /* Check we are idle before we fiddle with hw state! */
  197. GEM_BUG_ON(!intel_engine_is_idle(engine));
  198. GEM_BUG_ON(i915_gem_active_isset(&engine->timeline.last_request));
  199. /* Finally reset hw state */
  200. intel_engine_init_global_seqno(engine, seqno);
  201. engine->timeline.seqno = seqno;
  202. }
  203. list_for_each_entry(timeline, &i915->gt.timelines, link)
  204. memset(timeline->global_sync, 0, sizeof(timeline->global_sync));
  205. i915->gt.request_serial = seqno;
  206. return 0;
  207. }
  208. int i915_gem_set_global_seqno(struct drm_device *dev, u32 seqno)
  209. {
  210. struct drm_i915_private *i915 = to_i915(dev);
  211. lockdep_assert_held(&i915->drm.struct_mutex);
  212. if (seqno == 0)
  213. return -EINVAL;
  214. /* HWS page needs to be set less than what we will inject to ring */
  215. return reset_all_global_seqno(i915, seqno - 1);
  216. }
  217. static int reserve_gt(struct drm_i915_private *i915)
  218. {
  219. int ret;
  220. /*
  221. * Reservation is fine until we may need to wrap around
  222. *
  223. * By incrementing the serial for every request, we know that no
  224. * individual engine may exceed that serial (as each is reset to 0
  225. * on any wrap). This protects even the most pessimistic of migrations
  226. * of every request from all engines onto just one.
  227. */
  228. while (unlikely(++i915->gt.request_serial == 0)) {
  229. ret = reset_all_global_seqno(i915, 0);
  230. if (ret) {
  231. i915->gt.request_serial--;
  232. return ret;
  233. }
  234. }
  235. if (!i915->gt.active_requests++)
  236. i915_gem_unpark(i915);
  237. return 0;
  238. }
  239. static void unreserve_gt(struct drm_i915_private *i915)
  240. {
  241. GEM_BUG_ON(!i915->gt.active_requests);
  242. if (!--i915->gt.active_requests)
  243. i915_gem_park(i915);
  244. }
  245. void i915_gem_retire_noop(struct i915_gem_active *active,
  246. struct i915_request *request)
  247. {
  248. /* Space left intentionally blank */
  249. }
  250. static void advance_ring(struct i915_request *request)
  251. {
  252. struct intel_ring *ring = request->ring;
  253. unsigned int tail;
  254. /*
  255. * We know the GPU must have read the request to have
  256. * sent us the seqno + interrupt, so use the position
  257. * of tail of the request to update the last known position
  258. * of the GPU head.
  259. *
  260. * Note this requires that we are always called in request
  261. * completion order.
  262. */
  263. GEM_BUG_ON(!list_is_first(&request->ring_link, &ring->request_list));
  264. if (list_is_last(&request->ring_link, &ring->request_list)) {
  265. /*
  266. * We may race here with execlists resubmitting this request
  267. * as we retire it. The resubmission will move the ring->tail
  268. * forwards (to request->wa_tail). We either read the
  269. * current value that was written to hw, or the value that
  270. * is just about to be. Either works, if we miss the last two
  271. * noops - they are safe to be replayed on a reset.
  272. */
  273. tail = READ_ONCE(request->tail);
  274. list_del(&ring->active_link);
  275. } else {
  276. tail = request->postfix;
  277. }
  278. list_del_init(&request->ring_link);
  279. ring->head = tail;
  280. }
  281. static void free_capture_list(struct i915_request *request)
  282. {
  283. struct i915_capture_list *capture;
  284. capture = request->capture_list;
  285. while (capture) {
  286. struct i915_capture_list *next = capture->next;
  287. kfree(capture);
  288. capture = next;
  289. }
  290. }
  291. static void __retire_engine_request(struct intel_engine_cs *engine,
  292. struct i915_request *rq)
  293. {
  294. GEM_TRACE("%s(%s) fence %llx:%d, global=%d, current %d\n",
  295. __func__, engine->name,
  296. rq->fence.context, rq->fence.seqno,
  297. rq->global_seqno,
  298. intel_engine_get_seqno(engine));
  299. GEM_BUG_ON(!i915_request_completed(rq));
  300. local_irq_disable();
  301. spin_lock(&engine->timeline.lock);
  302. GEM_BUG_ON(!list_is_first(&rq->link, &engine->timeline.requests));
  303. list_del_init(&rq->link);
  304. spin_unlock(&engine->timeline.lock);
  305. spin_lock(&rq->lock);
  306. if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
  307. dma_fence_signal_locked(&rq->fence);
  308. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &rq->fence.flags))
  309. intel_engine_cancel_signaling(rq);
  310. if (rq->waitboost) {
  311. GEM_BUG_ON(!atomic_read(&rq->i915->gt_pm.rps.num_waiters));
  312. atomic_dec(&rq->i915->gt_pm.rps.num_waiters);
  313. }
  314. spin_unlock(&rq->lock);
  315. local_irq_enable();
  316. /*
  317. * The backing object for the context is done after switching to the
  318. * *next* context. Therefore we cannot retire the previous context until
  319. * the next context has already started running. However, since we
  320. * cannot take the required locks at i915_request_submit() we
  321. * defer the unpinning of the active context to now, retirement of
  322. * the subsequent request.
  323. */
  324. if (engine->last_retired_context)
  325. intel_context_unpin(engine->last_retired_context, engine);
  326. engine->last_retired_context = rq->ctx;
  327. }
  328. static void __retire_engine_upto(struct intel_engine_cs *engine,
  329. struct i915_request *rq)
  330. {
  331. struct i915_request *tmp;
  332. if (list_empty(&rq->link))
  333. return;
  334. do {
  335. tmp = list_first_entry(&engine->timeline.requests,
  336. typeof(*tmp), link);
  337. GEM_BUG_ON(tmp->engine != engine);
  338. __retire_engine_request(engine, tmp);
  339. } while (tmp != rq);
  340. }
  341. static void i915_request_retire(struct i915_request *request)
  342. {
  343. struct i915_gem_active *active, *next;
  344. GEM_TRACE("%s fence %llx:%d, global=%d, current %d\n",
  345. request->engine->name,
  346. request->fence.context, request->fence.seqno,
  347. request->global_seqno,
  348. intel_engine_get_seqno(request->engine));
  349. lockdep_assert_held(&request->i915->drm.struct_mutex);
  350. GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit));
  351. GEM_BUG_ON(!i915_request_completed(request));
  352. trace_i915_request_retire(request);
  353. advance_ring(request);
  354. free_capture_list(request);
  355. /*
  356. * Walk through the active list, calling retire on each. This allows
  357. * objects to track their GPU activity and mark themselves as idle
  358. * when their *last* active request is completed (updating state
  359. * tracking lists for eviction, active references for GEM, etc).
  360. *
  361. * As the ->retire() may free the node, we decouple it first and
  362. * pass along the auxiliary information (to avoid dereferencing
  363. * the node after the callback).
  364. */
  365. list_for_each_entry_safe(active, next, &request->active_list, link) {
  366. /*
  367. * In microbenchmarks or focusing upon time inside the kernel,
  368. * we may spend an inordinate amount of time simply handling
  369. * the retirement of requests and processing their callbacks.
  370. * Of which, this loop itself is particularly hot due to the
  371. * cache misses when jumping around the list of i915_gem_active.
  372. * So we try to keep this loop as streamlined as possible and
  373. * also prefetch the next i915_gem_active to try and hide
  374. * the likely cache miss.
  375. */
  376. prefetchw(next);
  377. INIT_LIST_HEAD(&active->link);
  378. RCU_INIT_POINTER(active->request, NULL);
  379. active->retire(active, request);
  380. }
  381. i915_request_remove_from_client(request);
  382. /* Retirement decays the ban score as it is a sign of ctx progress */
  383. atomic_dec_if_positive(&request->ctx->ban_score);
  384. intel_context_unpin(request->ctx, request->engine);
  385. __retire_engine_upto(request->engine, request);
  386. unreserve_gt(request->i915);
  387. i915_sched_node_fini(request->i915, &request->sched);
  388. i915_request_put(request);
  389. }
  390. void i915_request_retire_upto(struct i915_request *rq)
  391. {
  392. struct intel_ring *ring = rq->ring;
  393. struct i915_request *tmp;
  394. GEM_TRACE("%s fence %llx:%d, global=%d, current %d\n",
  395. rq->engine->name,
  396. rq->fence.context, rq->fence.seqno,
  397. rq->global_seqno,
  398. intel_engine_get_seqno(rq->engine));
  399. lockdep_assert_held(&rq->i915->drm.struct_mutex);
  400. GEM_BUG_ON(!i915_request_completed(rq));
  401. if (list_empty(&rq->ring_link))
  402. return;
  403. do {
  404. tmp = list_first_entry(&ring->request_list,
  405. typeof(*tmp), ring_link);
  406. i915_request_retire(tmp);
  407. } while (tmp != rq);
  408. }
  409. static u32 timeline_get_seqno(struct i915_timeline *tl)
  410. {
  411. return ++tl->seqno;
  412. }
  413. static void move_to_timeline(struct i915_request *request,
  414. struct i915_timeline *timeline)
  415. {
  416. GEM_BUG_ON(request->timeline == &request->engine->timeline);
  417. lockdep_assert_held(&request->engine->timeline.lock);
  418. spin_lock_nested(&request->timeline->lock, SINGLE_DEPTH_NESTING);
  419. list_move_tail(&request->link, &timeline->requests);
  420. spin_unlock(&request->timeline->lock);
  421. }
  422. void __i915_request_submit(struct i915_request *request)
  423. {
  424. struct intel_engine_cs *engine = request->engine;
  425. u32 seqno;
  426. GEM_TRACE("%s fence %llx:%d -> global=%d, current %d\n",
  427. engine->name,
  428. request->fence.context, request->fence.seqno,
  429. engine->timeline.seqno + 1,
  430. intel_engine_get_seqno(engine));
  431. GEM_BUG_ON(!irqs_disabled());
  432. lockdep_assert_held(&engine->timeline.lock);
  433. GEM_BUG_ON(request->global_seqno);
  434. seqno = timeline_get_seqno(&engine->timeline);
  435. GEM_BUG_ON(!seqno);
  436. GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
  437. /* We may be recursing from the signal callback of another i915 fence */
  438. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  439. request->global_seqno = seqno;
  440. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  441. intel_engine_enable_signaling(request, false);
  442. spin_unlock(&request->lock);
  443. engine->emit_breadcrumb(request,
  444. request->ring->vaddr + request->postfix);
  445. /* Transfer from per-context onto the global per-engine timeline */
  446. move_to_timeline(request, &engine->timeline);
  447. trace_i915_request_execute(request);
  448. wake_up_all(&request->execute);
  449. }
  450. void i915_request_submit(struct i915_request *request)
  451. {
  452. struct intel_engine_cs *engine = request->engine;
  453. unsigned long flags;
  454. /* Will be called from irq-context when using foreign fences. */
  455. spin_lock_irqsave(&engine->timeline.lock, flags);
  456. __i915_request_submit(request);
  457. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  458. }
  459. void __i915_request_unsubmit(struct i915_request *request)
  460. {
  461. struct intel_engine_cs *engine = request->engine;
  462. GEM_TRACE("%s fence %llx:%d <- global=%d, current %d\n",
  463. engine->name,
  464. request->fence.context, request->fence.seqno,
  465. request->global_seqno,
  466. intel_engine_get_seqno(engine));
  467. GEM_BUG_ON(!irqs_disabled());
  468. lockdep_assert_held(&engine->timeline.lock);
  469. /*
  470. * Only unwind in reverse order, required so that the per-context list
  471. * is kept in seqno/ring order.
  472. */
  473. GEM_BUG_ON(!request->global_seqno);
  474. GEM_BUG_ON(request->global_seqno != engine->timeline.seqno);
  475. GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine),
  476. request->global_seqno));
  477. engine->timeline.seqno--;
  478. /* We may be recursing from the signal callback of another i915 fence */
  479. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  480. request->global_seqno = 0;
  481. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  482. intel_engine_cancel_signaling(request);
  483. spin_unlock(&request->lock);
  484. /* Transfer back from the global per-engine timeline to per-context */
  485. move_to_timeline(request, request->timeline);
  486. /*
  487. * We don't need to wake_up any waiters on request->execute, they
  488. * will get woken by any other event or us re-adding this request
  489. * to the engine timeline (__i915_request_submit()). The waiters
  490. * should be quite adapt at finding that the request now has a new
  491. * global_seqno to the one they went to sleep on.
  492. */
  493. }
  494. void i915_request_unsubmit(struct i915_request *request)
  495. {
  496. struct intel_engine_cs *engine = request->engine;
  497. unsigned long flags;
  498. /* Will be called from irq-context when using foreign fences. */
  499. spin_lock_irqsave(&engine->timeline.lock, flags);
  500. __i915_request_unsubmit(request);
  501. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  502. }
  503. static int __i915_sw_fence_call
  504. submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
  505. {
  506. struct i915_request *request =
  507. container_of(fence, typeof(*request), submit);
  508. switch (state) {
  509. case FENCE_COMPLETE:
  510. trace_i915_request_submit(request);
  511. /*
  512. * We need to serialize use of the submit_request() callback
  513. * with its hotplugging performed during an emergency
  514. * i915_gem_set_wedged(). We use the RCU mechanism to mark the
  515. * critical section in order to force i915_gem_set_wedged() to
  516. * wait until the submit_request() is completed before
  517. * proceeding.
  518. */
  519. rcu_read_lock();
  520. request->engine->submit_request(request);
  521. rcu_read_unlock();
  522. break;
  523. case FENCE_FREE:
  524. i915_request_put(request);
  525. break;
  526. }
  527. return NOTIFY_DONE;
  528. }
  529. /**
  530. * i915_request_alloc - allocate a request structure
  531. *
  532. * @engine: engine that we wish to issue the request on.
  533. * @ctx: context that the request will be associated with.
  534. *
  535. * Returns a pointer to the allocated request if successful,
  536. * or an error code if not.
  537. */
  538. struct i915_request *
  539. i915_request_alloc(struct intel_engine_cs *engine, struct i915_gem_context *ctx)
  540. {
  541. struct drm_i915_private *i915 = engine->i915;
  542. struct i915_request *rq;
  543. struct intel_ring *ring;
  544. int ret;
  545. lockdep_assert_held(&i915->drm.struct_mutex);
  546. /*
  547. * Preempt contexts are reserved for exclusive use to inject a
  548. * preemption context switch. They are never to be used for any trivial
  549. * request!
  550. */
  551. GEM_BUG_ON(ctx == i915->preempt_context);
  552. /*
  553. * ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  554. * EIO if the GPU is already wedged.
  555. */
  556. if (i915_terminally_wedged(&i915->gpu_error))
  557. return ERR_PTR(-EIO);
  558. /*
  559. * Pinning the contexts may generate requests in order to acquire
  560. * GGTT space, so do this first before we reserve a seqno for
  561. * ourselves.
  562. */
  563. ring = intel_context_pin(ctx, engine);
  564. if (IS_ERR(ring))
  565. return ERR_CAST(ring);
  566. GEM_BUG_ON(!ring);
  567. ret = reserve_gt(i915);
  568. if (ret)
  569. goto err_unpin;
  570. ret = intel_ring_wait_for_space(ring, MIN_SPACE_FOR_ADD_REQUEST);
  571. if (ret)
  572. goto err_unreserve;
  573. /* Move our oldest request to the slab-cache (if not in use!) */
  574. rq = list_first_entry(&ring->request_list, typeof(*rq), ring_link);
  575. if (!list_is_last(&rq->ring_link, &ring->request_list) &&
  576. i915_request_completed(rq))
  577. i915_request_retire(rq);
  578. /*
  579. * Beware: Dragons be flying overhead.
  580. *
  581. * We use RCU to look up requests in flight. The lookups may
  582. * race with the request being allocated from the slab freelist.
  583. * That is the request we are writing to here, may be in the process
  584. * of being read by __i915_gem_active_get_rcu(). As such,
  585. * we have to be very careful when overwriting the contents. During
  586. * the RCU lookup, we change chase the request->engine pointer,
  587. * read the request->global_seqno and increment the reference count.
  588. *
  589. * The reference count is incremented atomically. If it is zero,
  590. * the lookup knows the request is unallocated and complete. Otherwise,
  591. * it is either still in use, or has been reallocated and reset
  592. * with dma_fence_init(). This increment is safe for release as we
  593. * check that the request we have a reference to and matches the active
  594. * request.
  595. *
  596. * Before we increment the refcount, we chase the request->engine
  597. * pointer. We must not call kmem_cache_zalloc() or else we set
  598. * that pointer to NULL and cause a crash during the lookup. If
  599. * we see the request is completed (based on the value of the
  600. * old engine and seqno), the lookup is complete and reports NULL.
  601. * If we decide the request is not completed (new engine or seqno),
  602. * then we grab a reference and double check that it is still the
  603. * active request - which it won't be and restart the lookup.
  604. *
  605. * Do not use kmem_cache_zalloc() here!
  606. */
  607. rq = kmem_cache_alloc(i915->requests,
  608. GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
  609. if (unlikely(!rq)) {
  610. /* Ratelimit ourselves to prevent oom from malicious clients */
  611. ret = i915_gem_wait_for_idle(i915,
  612. I915_WAIT_LOCKED |
  613. I915_WAIT_INTERRUPTIBLE);
  614. if (ret)
  615. goto err_unreserve;
  616. /*
  617. * We've forced the client to stall and catch up with whatever
  618. * backlog there might have been. As we are assuming that we
  619. * caused the mempressure, now is an opportune time to
  620. * recover as much memory from the request pool as is possible.
  621. * Having already penalized the client to stall, we spend
  622. * a little extra time to re-optimise page allocation.
  623. */
  624. kmem_cache_shrink(i915->requests);
  625. rcu_barrier(); /* Recover the TYPESAFE_BY_RCU pages */
  626. rq = kmem_cache_alloc(i915->requests, GFP_KERNEL);
  627. if (!rq) {
  628. ret = -ENOMEM;
  629. goto err_unreserve;
  630. }
  631. }
  632. INIT_LIST_HEAD(&rq->active_list);
  633. rq->i915 = i915;
  634. rq->engine = engine;
  635. rq->ctx = ctx;
  636. rq->ring = ring;
  637. rq->timeline = ring->timeline;
  638. GEM_BUG_ON(rq->timeline == &engine->timeline);
  639. spin_lock_init(&rq->lock);
  640. dma_fence_init(&rq->fence,
  641. &i915_fence_ops,
  642. &rq->lock,
  643. rq->timeline->fence_context,
  644. timeline_get_seqno(rq->timeline));
  645. /* We bump the ref for the fence chain */
  646. i915_sw_fence_init(&i915_request_get(rq)->submit, submit_notify);
  647. init_waitqueue_head(&rq->execute);
  648. i915_sched_node_init(&rq->sched);
  649. /* No zalloc, must clear what we need by hand */
  650. rq->global_seqno = 0;
  651. rq->signaling.wait.seqno = 0;
  652. rq->file_priv = NULL;
  653. rq->batch = NULL;
  654. rq->capture_list = NULL;
  655. rq->waitboost = false;
  656. /*
  657. * Reserve space in the ring buffer for all the commands required to
  658. * eventually emit this request. This is to guarantee that the
  659. * i915_request_add() call can't fail. Note that the reserve may need
  660. * to be redone if the request is not actually submitted straight
  661. * away, e.g. because a GPU scheduler has deferred it.
  662. */
  663. rq->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  664. GEM_BUG_ON(rq->reserved_space < engine->emit_breadcrumb_sz);
  665. /*
  666. * Record the position of the start of the request so that
  667. * should we detect the updated seqno part-way through the
  668. * GPU processing the request, we never over-estimate the
  669. * position of the head.
  670. */
  671. rq->head = rq->ring->emit;
  672. /* Unconditionally invalidate GPU caches and TLBs. */
  673. ret = engine->emit_flush(rq, EMIT_INVALIDATE);
  674. if (ret)
  675. goto err_unwind;
  676. ret = engine->request_alloc(rq);
  677. if (ret)
  678. goto err_unwind;
  679. /* Keep a second pin for the dual retirement along engine and ring */
  680. __intel_context_pin(rq->ctx, engine);
  681. /* Check that we didn't interrupt ourselves with a new request */
  682. GEM_BUG_ON(rq->timeline->seqno != rq->fence.seqno);
  683. return rq;
  684. err_unwind:
  685. rq->ring->emit = rq->head;
  686. /* Make sure we didn't add ourselves to external state before freeing */
  687. GEM_BUG_ON(!list_empty(&rq->active_list));
  688. GEM_BUG_ON(!list_empty(&rq->sched.signalers_list));
  689. GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
  690. kmem_cache_free(i915->requests, rq);
  691. err_unreserve:
  692. unreserve_gt(i915);
  693. err_unpin:
  694. intel_context_unpin(ctx, engine);
  695. return ERR_PTR(ret);
  696. }
  697. static int
  698. i915_request_await_request(struct i915_request *to, struct i915_request *from)
  699. {
  700. int ret;
  701. GEM_BUG_ON(to == from);
  702. GEM_BUG_ON(to->timeline == from->timeline);
  703. if (i915_request_completed(from))
  704. return 0;
  705. if (to->engine->schedule) {
  706. ret = i915_sched_node_add_dependency(to->i915,
  707. &to->sched,
  708. &from->sched);
  709. if (ret < 0)
  710. return ret;
  711. }
  712. if (to->engine == from->engine) {
  713. ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
  714. &from->submit,
  715. I915_FENCE_GFP);
  716. return ret < 0 ? ret : 0;
  717. }
  718. if (to->engine->semaphore.sync_to) {
  719. u32 seqno;
  720. GEM_BUG_ON(!from->engine->semaphore.signal);
  721. seqno = i915_request_global_seqno(from);
  722. if (!seqno)
  723. goto await_dma_fence;
  724. if (seqno <= to->timeline->global_sync[from->engine->id])
  725. return 0;
  726. trace_i915_gem_ring_sync_to(to, from);
  727. ret = to->engine->semaphore.sync_to(to, from);
  728. if (ret)
  729. return ret;
  730. to->timeline->global_sync[from->engine->id] = seqno;
  731. return 0;
  732. }
  733. await_dma_fence:
  734. ret = i915_sw_fence_await_dma_fence(&to->submit,
  735. &from->fence, 0,
  736. I915_FENCE_GFP);
  737. return ret < 0 ? ret : 0;
  738. }
  739. int
  740. i915_request_await_dma_fence(struct i915_request *rq, struct dma_fence *fence)
  741. {
  742. struct dma_fence **child = &fence;
  743. unsigned int nchild = 1;
  744. int ret;
  745. /*
  746. * Note that if the fence-array was created in signal-on-any mode,
  747. * we should *not* decompose it into its individual fences. However,
  748. * we don't currently store which mode the fence-array is operating
  749. * in. Fortunately, the only user of signal-on-any is private to
  750. * amdgpu and we should not see any incoming fence-array from
  751. * sync-file being in signal-on-any mode.
  752. */
  753. if (dma_fence_is_array(fence)) {
  754. struct dma_fence_array *array = to_dma_fence_array(fence);
  755. child = array->fences;
  756. nchild = array->num_fences;
  757. GEM_BUG_ON(!nchild);
  758. }
  759. do {
  760. fence = *child++;
  761. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  762. continue;
  763. /*
  764. * Requests on the same timeline are explicitly ordered, along
  765. * with their dependencies, by i915_request_add() which ensures
  766. * that requests are submitted in-order through each ring.
  767. */
  768. if (fence->context == rq->fence.context)
  769. continue;
  770. /* Squash repeated waits to the same timelines */
  771. if (fence->context != rq->i915->mm.unordered_timeline &&
  772. i915_timeline_sync_is_later(rq->timeline, fence))
  773. continue;
  774. if (dma_fence_is_i915(fence))
  775. ret = i915_request_await_request(rq, to_request(fence));
  776. else
  777. ret = i915_sw_fence_await_dma_fence(&rq->submit, fence,
  778. I915_FENCE_TIMEOUT,
  779. I915_FENCE_GFP);
  780. if (ret < 0)
  781. return ret;
  782. /* Record the latest fence used against each timeline */
  783. if (fence->context != rq->i915->mm.unordered_timeline)
  784. i915_timeline_sync_set(rq->timeline, fence);
  785. } while (--nchild);
  786. return 0;
  787. }
  788. /**
  789. * i915_request_await_object - set this request to (async) wait upon a bo
  790. * @to: request we are wishing to use
  791. * @obj: object which may be in use on another ring.
  792. * @write: whether the wait is on behalf of a writer
  793. *
  794. * This code is meant to abstract object synchronization with the GPU.
  795. * Conceptually we serialise writes between engines inside the GPU.
  796. * We only allow one engine to write into a buffer at any time, but
  797. * multiple readers. To ensure each has a coherent view of memory, we must:
  798. *
  799. * - If there is an outstanding write request to the object, the new
  800. * request must wait for it to complete (either CPU or in hw, requests
  801. * on the same ring will be naturally ordered).
  802. *
  803. * - If we are a write request (pending_write_domain is set), the new
  804. * request must wait for outstanding read requests to complete.
  805. *
  806. * Returns 0 if successful, else propagates up the lower layer error.
  807. */
  808. int
  809. i915_request_await_object(struct i915_request *to,
  810. struct drm_i915_gem_object *obj,
  811. bool write)
  812. {
  813. struct dma_fence *excl;
  814. int ret = 0;
  815. if (write) {
  816. struct dma_fence **shared;
  817. unsigned int count, i;
  818. ret = reservation_object_get_fences_rcu(obj->resv,
  819. &excl, &count, &shared);
  820. if (ret)
  821. return ret;
  822. for (i = 0; i < count; i++) {
  823. ret = i915_request_await_dma_fence(to, shared[i]);
  824. if (ret)
  825. break;
  826. dma_fence_put(shared[i]);
  827. }
  828. for (; i < count; i++)
  829. dma_fence_put(shared[i]);
  830. kfree(shared);
  831. } else {
  832. excl = reservation_object_get_excl_rcu(obj->resv);
  833. }
  834. if (excl) {
  835. if (ret == 0)
  836. ret = i915_request_await_dma_fence(to, excl);
  837. dma_fence_put(excl);
  838. }
  839. return ret;
  840. }
  841. /*
  842. * NB: This function is not allowed to fail. Doing so would mean the the
  843. * request is not being tracked for completion but the work itself is
  844. * going to happen on the hardware. This would be a Bad Thing(tm).
  845. */
  846. void __i915_request_add(struct i915_request *request, bool flush_caches)
  847. {
  848. struct intel_engine_cs *engine = request->engine;
  849. struct intel_ring *ring = request->ring;
  850. struct i915_timeline *timeline = request->timeline;
  851. struct i915_request *prev;
  852. u32 *cs;
  853. int err;
  854. GEM_TRACE("%s fence %llx:%d\n",
  855. engine->name, request->fence.context, request->fence.seqno);
  856. lockdep_assert_held(&request->i915->drm.struct_mutex);
  857. trace_i915_request_add(request);
  858. /*
  859. * Make sure that no request gazumped us - if it was allocated after
  860. * our i915_request_alloc() and called __i915_request_add() before
  861. * us, the timeline will hold its seqno which is later than ours.
  862. */
  863. GEM_BUG_ON(timeline->seqno != request->fence.seqno);
  864. /*
  865. * To ensure that this call will not fail, space for its emissions
  866. * should already have been reserved in the ring buffer. Let the ring
  867. * know that it is time to use that space up.
  868. */
  869. request->reserved_space = 0;
  870. /*
  871. * Emit any outstanding flushes - execbuf can fail to emit the flush
  872. * after having emitted the batchbuffer command. Hence we need to fix
  873. * things up similar to emitting the lazy request. The difference here
  874. * is that the flush _must_ happen before the next request, no matter
  875. * what.
  876. */
  877. if (flush_caches) {
  878. err = engine->emit_flush(request, EMIT_FLUSH);
  879. /* Not allowed to fail! */
  880. WARN(err, "engine->emit_flush() failed: %d!\n", err);
  881. }
  882. /*
  883. * Record the position of the start of the breadcrumb so that
  884. * should we detect the updated seqno part-way through the
  885. * GPU processing the request, we never over-estimate the
  886. * position of the ring's HEAD.
  887. */
  888. cs = intel_ring_begin(request, engine->emit_breadcrumb_sz);
  889. GEM_BUG_ON(IS_ERR(cs));
  890. request->postfix = intel_ring_offset(request, cs);
  891. /*
  892. * Seal the request and mark it as pending execution. Note that
  893. * we may inspect this state, without holding any locks, during
  894. * hangcheck. Hence we apply the barrier to ensure that we do not
  895. * see a more recent value in the hws than we are tracking.
  896. */
  897. prev = i915_gem_active_raw(&timeline->last_request,
  898. &request->i915->drm.struct_mutex);
  899. if (prev && !i915_request_completed(prev)) {
  900. i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
  901. &request->submitq);
  902. if (engine->schedule)
  903. __i915_sched_node_add_dependency(&request->sched,
  904. &prev->sched,
  905. &request->dep,
  906. 0);
  907. }
  908. spin_lock_irq(&timeline->lock);
  909. list_add_tail(&request->link, &timeline->requests);
  910. spin_unlock_irq(&timeline->lock);
  911. GEM_BUG_ON(timeline->seqno != request->fence.seqno);
  912. i915_gem_active_set(&timeline->last_request, request);
  913. list_add_tail(&request->ring_link, &ring->request_list);
  914. if (list_is_first(&request->ring_link, &ring->request_list))
  915. list_add(&ring->active_link, &request->i915->gt.active_rings);
  916. request->emitted_jiffies = jiffies;
  917. /*
  918. * Let the backend know a new request has arrived that may need
  919. * to adjust the existing execution schedule due to a high priority
  920. * request - i.e. we may want to preempt the current request in order
  921. * to run a high priority dependency chain *before* we can execute this
  922. * request.
  923. *
  924. * This is called before the request is ready to run so that we can
  925. * decide whether to preempt the entire chain so that it is ready to
  926. * run at the earliest possible convenience.
  927. */
  928. local_bh_disable();
  929. rcu_read_lock(); /* RCU serialisation for set-wedged protection */
  930. if (engine->schedule)
  931. engine->schedule(request, &request->ctx->sched);
  932. rcu_read_unlock();
  933. i915_sw_fence_commit(&request->submit);
  934. local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
  935. /*
  936. * In typical scenarios, we do not expect the previous request on
  937. * the timeline to be still tracked by timeline->last_request if it
  938. * has been completed. If the completed request is still here, that
  939. * implies that request retirement is a long way behind submission,
  940. * suggesting that we haven't been retiring frequently enough from
  941. * the combination of retire-before-alloc, waiters and the background
  942. * retirement worker. So if the last request on this timeline was
  943. * already completed, do a catch up pass, flushing the retirement queue
  944. * up to this client. Since we have now moved the heaviest operations
  945. * during retirement onto secondary workers, such as freeing objects
  946. * or contexts, retiring a bunch of requests is mostly list management
  947. * (and cache misses), and so we should not be overly penalizing this
  948. * client by performing excess work, though we may still performing
  949. * work on behalf of others -- but instead we should benefit from
  950. * improved resource management. (Well, that's the theory at least.)
  951. */
  952. if (prev && i915_request_completed(prev))
  953. i915_request_retire_upto(prev);
  954. }
  955. static unsigned long local_clock_us(unsigned int *cpu)
  956. {
  957. unsigned long t;
  958. /*
  959. * Cheaply and approximately convert from nanoseconds to microseconds.
  960. * The result and subsequent calculations are also defined in the same
  961. * approximate microseconds units. The principal source of timing
  962. * error here is from the simple truncation.
  963. *
  964. * Note that local_clock() is only defined wrt to the current CPU;
  965. * the comparisons are no longer valid if we switch CPUs. Instead of
  966. * blocking preemption for the entire busywait, we can detect the CPU
  967. * switch and use that as indicator of system load and a reason to
  968. * stop busywaiting, see busywait_stop().
  969. */
  970. *cpu = get_cpu();
  971. t = local_clock() >> 10;
  972. put_cpu();
  973. return t;
  974. }
  975. static bool busywait_stop(unsigned long timeout, unsigned int cpu)
  976. {
  977. unsigned int this_cpu;
  978. if (time_after(local_clock_us(&this_cpu), timeout))
  979. return true;
  980. return this_cpu != cpu;
  981. }
  982. static bool __i915_spin_request(const struct i915_request *rq,
  983. u32 seqno, int state, unsigned long timeout_us)
  984. {
  985. struct intel_engine_cs *engine = rq->engine;
  986. unsigned int irq, cpu;
  987. GEM_BUG_ON(!seqno);
  988. /*
  989. * Only wait for the request if we know it is likely to complete.
  990. *
  991. * We don't track the timestamps around requests, nor the average
  992. * request length, so we do not have a good indicator that this
  993. * request will complete within the timeout. What we do know is the
  994. * order in which requests are executed by the engine and so we can
  995. * tell if the request has started. If the request hasn't started yet,
  996. * it is a fair assumption that it will not complete within our
  997. * relatively short timeout.
  998. */
  999. if (!i915_seqno_passed(intel_engine_get_seqno(engine), seqno - 1))
  1000. return false;
  1001. /*
  1002. * When waiting for high frequency requests, e.g. during synchronous
  1003. * rendering split between the CPU and GPU, the finite amount of time
  1004. * required to set up the irq and wait upon it limits the response
  1005. * rate. By busywaiting on the request completion for a short while we
  1006. * can service the high frequency waits as quick as possible. However,
  1007. * if it is a slow request, we want to sleep as quickly as possible.
  1008. * The tradeoff between waiting and sleeping is roughly the time it
  1009. * takes to sleep on a request, on the order of a microsecond.
  1010. */
  1011. irq = atomic_read(&engine->irq_count);
  1012. timeout_us += local_clock_us(&cpu);
  1013. do {
  1014. if (i915_seqno_passed(intel_engine_get_seqno(engine), seqno))
  1015. return seqno == i915_request_global_seqno(rq);
  1016. /*
  1017. * Seqno are meant to be ordered *before* the interrupt. If
  1018. * we see an interrupt without a corresponding seqno advance,
  1019. * assume we won't see one in the near future but require
  1020. * the engine->seqno_barrier() to fixup coherency.
  1021. */
  1022. if (atomic_read(&engine->irq_count) != irq)
  1023. break;
  1024. if (signal_pending_state(state, current))
  1025. break;
  1026. if (busywait_stop(timeout_us, cpu))
  1027. break;
  1028. cpu_relax();
  1029. } while (!need_resched());
  1030. return false;
  1031. }
  1032. static bool __i915_wait_request_check_and_reset(struct i915_request *request)
  1033. {
  1034. struct i915_gpu_error *error = &request->i915->gpu_error;
  1035. if (likely(!i915_reset_handoff(error)))
  1036. return false;
  1037. __set_current_state(TASK_RUNNING);
  1038. i915_reset(request->i915, error->stalled_mask, error->reason);
  1039. return true;
  1040. }
  1041. /**
  1042. * i915_request_wait - wait until execution of request has finished
  1043. * @rq: the request to wait upon
  1044. * @flags: how to wait
  1045. * @timeout: how long to wait in jiffies
  1046. *
  1047. * i915_request_wait() waits for the request to be completed, for a
  1048. * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
  1049. * unbounded wait).
  1050. *
  1051. * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
  1052. * in via the flags, and vice versa if the struct_mutex is not held, the caller
  1053. * must not specify that the wait is locked.
  1054. *
  1055. * Returns the remaining time (in jiffies) if the request completed, which may
  1056. * be zero or -ETIME if the request is unfinished after the timeout expires.
  1057. * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
  1058. * pending before the request completes.
  1059. */
  1060. long i915_request_wait(struct i915_request *rq,
  1061. unsigned int flags,
  1062. long timeout)
  1063. {
  1064. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  1065. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  1066. wait_queue_head_t *errq = &rq->i915->gpu_error.wait_queue;
  1067. DEFINE_WAIT_FUNC(reset, default_wake_function);
  1068. DEFINE_WAIT_FUNC(exec, default_wake_function);
  1069. struct intel_wait wait;
  1070. might_sleep();
  1071. #if IS_ENABLED(CONFIG_LOCKDEP)
  1072. GEM_BUG_ON(debug_locks &&
  1073. !!lockdep_is_held(&rq->i915->drm.struct_mutex) !=
  1074. !!(flags & I915_WAIT_LOCKED));
  1075. #endif
  1076. GEM_BUG_ON(timeout < 0);
  1077. if (i915_request_completed(rq))
  1078. return timeout;
  1079. if (!timeout)
  1080. return -ETIME;
  1081. trace_i915_request_wait_begin(rq, flags);
  1082. add_wait_queue(&rq->execute, &exec);
  1083. if (flags & I915_WAIT_LOCKED)
  1084. add_wait_queue(errq, &reset);
  1085. intel_wait_init(&wait, rq);
  1086. restart:
  1087. do {
  1088. set_current_state(state);
  1089. if (intel_wait_update_request(&wait, rq))
  1090. break;
  1091. if (flags & I915_WAIT_LOCKED &&
  1092. __i915_wait_request_check_and_reset(rq))
  1093. continue;
  1094. if (signal_pending_state(state, current)) {
  1095. timeout = -ERESTARTSYS;
  1096. goto complete;
  1097. }
  1098. if (!timeout) {
  1099. timeout = -ETIME;
  1100. goto complete;
  1101. }
  1102. timeout = io_schedule_timeout(timeout);
  1103. } while (1);
  1104. GEM_BUG_ON(!intel_wait_has_seqno(&wait));
  1105. GEM_BUG_ON(!i915_sw_fence_signaled(&rq->submit));
  1106. /* Optimistic short spin before touching IRQs */
  1107. if (__i915_spin_request(rq, wait.seqno, state, 5))
  1108. goto complete;
  1109. set_current_state(state);
  1110. if (intel_engine_add_wait(rq->engine, &wait))
  1111. /*
  1112. * In order to check that we haven't missed the interrupt
  1113. * as we enabled it, we need to kick ourselves to do a
  1114. * coherent check on the seqno before we sleep.
  1115. */
  1116. goto wakeup;
  1117. if (flags & I915_WAIT_LOCKED)
  1118. __i915_wait_request_check_and_reset(rq);
  1119. for (;;) {
  1120. if (signal_pending_state(state, current)) {
  1121. timeout = -ERESTARTSYS;
  1122. break;
  1123. }
  1124. if (!timeout) {
  1125. timeout = -ETIME;
  1126. break;
  1127. }
  1128. timeout = io_schedule_timeout(timeout);
  1129. if (intel_wait_complete(&wait) &&
  1130. intel_wait_check_request(&wait, rq))
  1131. break;
  1132. set_current_state(state);
  1133. wakeup:
  1134. /*
  1135. * Carefully check if the request is complete, giving time
  1136. * for the seqno to be visible following the interrupt.
  1137. * We also have to check in case we are kicked by the GPU
  1138. * reset in order to drop the struct_mutex.
  1139. */
  1140. if (__i915_request_irq_complete(rq))
  1141. break;
  1142. /*
  1143. * If the GPU is hung, and we hold the lock, reset the GPU
  1144. * and then check for completion. On a full reset, the engine's
  1145. * HW seqno will be advanced passed us and we are complete.
  1146. * If we do a partial reset, we have to wait for the GPU to
  1147. * resume and update the breadcrumb.
  1148. *
  1149. * If we don't hold the mutex, we can just wait for the worker
  1150. * to come along and update the breadcrumb (either directly
  1151. * itself, or indirectly by recovering the GPU).
  1152. */
  1153. if (flags & I915_WAIT_LOCKED &&
  1154. __i915_wait_request_check_and_reset(rq))
  1155. continue;
  1156. /* Only spin if we know the GPU is processing this request */
  1157. if (__i915_spin_request(rq, wait.seqno, state, 2))
  1158. break;
  1159. if (!intel_wait_check_request(&wait, rq)) {
  1160. intel_engine_remove_wait(rq->engine, &wait);
  1161. goto restart;
  1162. }
  1163. }
  1164. intel_engine_remove_wait(rq->engine, &wait);
  1165. complete:
  1166. __set_current_state(TASK_RUNNING);
  1167. if (flags & I915_WAIT_LOCKED)
  1168. remove_wait_queue(errq, &reset);
  1169. remove_wait_queue(&rq->execute, &exec);
  1170. trace_i915_request_wait_end(rq);
  1171. return timeout;
  1172. }
  1173. static void ring_retire_requests(struct intel_ring *ring)
  1174. {
  1175. struct i915_request *request, *next;
  1176. list_for_each_entry_safe(request, next,
  1177. &ring->request_list, ring_link) {
  1178. if (!i915_request_completed(request))
  1179. break;
  1180. i915_request_retire(request);
  1181. }
  1182. }
  1183. void i915_retire_requests(struct drm_i915_private *i915)
  1184. {
  1185. struct intel_ring *ring, *tmp;
  1186. lockdep_assert_held(&i915->drm.struct_mutex);
  1187. if (!i915->gt.active_requests)
  1188. return;
  1189. list_for_each_entry_safe(ring, tmp, &i915->gt.active_rings, active_link)
  1190. ring_retire_requests(ring);
  1191. }
  1192. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1193. #include "selftests/mock_request.c"
  1194. #include "selftests/i915_request.c"
  1195. #endif