i915_request.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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. GEM_TRACE("marking %s as inactive\n", ring->timeline->name);
  274. tail = READ_ONCE(request->tail);
  275. list_del(&ring->active_link);
  276. } else {
  277. tail = request->postfix;
  278. }
  279. list_del_init(&request->ring_link);
  280. ring->head = tail;
  281. }
  282. static void free_capture_list(struct i915_request *request)
  283. {
  284. struct i915_capture_list *capture;
  285. capture = request->capture_list;
  286. while (capture) {
  287. struct i915_capture_list *next = capture->next;
  288. kfree(capture);
  289. capture = next;
  290. }
  291. }
  292. static void __retire_engine_request(struct intel_engine_cs *engine,
  293. struct i915_request *rq)
  294. {
  295. GEM_TRACE("%s(%s) fence %llx:%d, global=%d, current %d\n",
  296. __func__, engine->name,
  297. rq->fence.context, rq->fence.seqno,
  298. rq->global_seqno,
  299. intel_engine_get_seqno(engine));
  300. GEM_BUG_ON(!i915_request_completed(rq));
  301. local_irq_disable();
  302. spin_lock(&engine->timeline.lock);
  303. GEM_BUG_ON(!list_is_first(&rq->link, &engine->timeline.requests));
  304. list_del_init(&rq->link);
  305. spin_unlock(&engine->timeline.lock);
  306. spin_lock(&rq->lock);
  307. if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
  308. dma_fence_signal_locked(&rq->fence);
  309. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &rq->fence.flags))
  310. intel_engine_cancel_signaling(rq);
  311. if (rq->waitboost) {
  312. GEM_BUG_ON(!atomic_read(&rq->i915->gt_pm.rps.num_waiters));
  313. atomic_dec(&rq->i915->gt_pm.rps.num_waiters);
  314. }
  315. spin_unlock(&rq->lock);
  316. local_irq_enable();
  317. /*
  318. * The backing object for the context is done after switching to the
  319. * *next* context. Therefore we cannot retire the previous context until
  320. * the next context has already started running. However, since we
  321. * cannot take the required locks at i915_request_submit() we
  322. * defer the unpinning of the active context to now, retirement of
  323. * the subsequent request.
  324. */
  325. if (engine->last_retired_context)
  326. intel_context_unpin(engine->last_retired_context);
  327. engine->last_retired_context = rq->hw_context;
  328. }
  329. static void __retire_engine_upto(struct intel_engine_cs *engine,
  330. struct i915_request *rq)
  331. {
  332. struct i915_request *tmp;
  333. if (list_empty(&rq->link))
  334. return;
  335. do {
  336. tmp = list_first_entry(&engine->timeline.requests,
  337. typeof(*tmp), link);
  338. GEM_BUG_ON(tmp->engine != engine);
  339. __retire_engine_request(engine, tmp);
  340. } while (tmp != rq);
  341. }
  342. static void i915_request_retire(struct i915_request *request)
  343. {
  344. struct i915_gem_active *active, *next;
  345. GEM_TRACE("%s fence %llx:%d, global=%d, current %d\n",
  346. request->engine->name,
  347. request->fence.context, request->fence.seqno,
  348. request->global_seqno,
  349. intel_engine_get_seqno(request->engine));
  350. lockdep_assert_held(&request->i915->drm.struct_mutex);
  351. GEM_BUG_ON(!i915_sw_fence_signaled(&request->submit));
  352. GEM_BUG_ON(!i915_request_completed(request));
  353. trace_i915_request_retire(request);
  354. advance_ring(request);
  355. free_capture_list(request);
  356. /*
  357. * Walk through the active list, calling retire on each. This allows
  358. * objects to track their GPU activity and mark themselves as idle
  359. * when their *last* active request is completed (updating state
  360. * tracking lists for eviction, active references for GEM, etc).
  361. *
  362. * As the ->retire() may free the node, we decouple it first and
  363. * pass along the auxiliary information (to avoid dereferencing
  364. * the node after the callback).
  365. */
  366. list_for_each_entry_safe(active, next, &request->active_list, link) {
  367. /*
  368. * In microbenchmarks or focusing upon time inside the kernel,
  369. * we may spend an inordinate amount of time simply handling
  370. * the retirement of requests and processing their callbacks.
  371. * Of which, this loop itself is particularly hot due to the
  372. * cache misses when jumping around the list of i915_gem_active.
  373. * So we try to keep this loop as streamlined as possible and
  374. * also prefetch the next i915_gem_active to try and hide
  375. * the likely cache miss.
  376. */
  377. prefetchw(next);
  378. INIT_LIST_HEAD(&active->link);
  379. RCU_INIT_POINTER(active->request, NULL);
  380. active->retire(active, request);
  381. }
  382. i915_request_remove_from_client(request);
  383. /* Retirement decays the ban score as it is a sign of ctx progress */
  384. atomic_dec_if_positive(&request->gem_context->ban_score);
  385. intel_context_unpin(request->hw_context);
  386. __retire_engine_upto(request->engine, request);
  387. unreserve_gt(request->i915);
  388. i915_sched_node_fini(request->i915, &request->sched);
  389. i915_request_put(request);
  390. }
  391. void i915_request_retire_upto(struct i915_request *rq)
  392. {
  393. struct intel_ring *ring = rq->ring;
  394. struct i915_request *tmp;
  395. GEM_TRACE("%s fence %llx:%d, global=%d, current %d\n",
  396. rq->engine->name,
  397. rq->fence.context, rq->fence.seqno,
  398. rq->global_seqno,
  399. intel_engine_get_seqno(rq->engine));
  400. lockdep_assert_held(&rq->i915->drm.struct_mutex);
  401. GEM_BUG_ON(!i915_request_completed(rq));
  402. if (list_empty(&rq->ring_link))
  403. return;
  404. do {
  405. tmp = list_first_entry(&ring->request_list,
  406. typeof(*tmp), ring_link);
  407. i915_request_retire(tmp);
  408. } while (tmp != rq);
  409. }
  410. static u32 timeline_get_seqno(struct i915_timeline *tl)
  411. {
  412. return ++tl->seqno;
  413. }
  414. static void move_to_timeline(struct i915_request *request,
  415. struct i915_timeline *timeline)
  416. {
  417. GEM_BUG_ON(request->timeline == &request->engine->timeline);
  418. lockdep_assert_held(&request->engine->timeline.lock);
  419. spin_lock_nested(&request->timeline->lock, SINGLE_DEPTH_NESTING);
  420. list_move_tail(&request->link, &timeline->requests);
  421. spin_unlock(&request->timeline->lock);
  422. }
  423. void __i915_request_submit(struct i915_request *request)
  424. {
  425. struct intel_engine_cs *engine = request->engine;
  426. u32 seqno;
  427. GEM_TRACE("%s fence %llx:%d -> global=%d, current %d\n",
  428. engine->name,
  429. request->fence.context, request->fence.seqno,
  430. engine->timeline.seqno + 1,
  431. intel_engine_get_seqno(engine));
  432. GEM_BUG_ON(!irqs_disabled());
  433. lockdep_assert_held(&engine->timeline.lock);
  434. GEM_BUG_ON(request->global_seqno);
  435. seqno = timeline_get_seqno(&engine->timeline);
  436. GEM_BUG_ON(!seqno);
  437. GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine), seqno));
  438. /* We may be recursing from the signal callback of another i915 fence */
  439. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  440. request->global_seqno = seqno;
  441. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  442. intel_engine_enable_signaling(request, false);
  443. spin_unlock(&request->lock);
  444. engine->emit_breadcrumb(request,
  445. request->ring->vaddr + request->postfix);
  446. /* Transfer from per-context onto the global per-engine timeline */
  447. move_to_timeline(request, &engine->timeline);
  448. trace_i915_request_execute(request);
  449. wake_up_all(&request->execute);
  450. }
  451. void i915_request_submit(struct i915_request *request)
  452. {
  453. struct intel_engine_cs *engine = request->engine;
  454. unsigned long flags;
  455. /* Will be called from irq-context when using foreign fences. */
  456. spin_lock_irqsave(&engine->timeline.lock, flags);
  457. __i915_request_submit(request);
  458. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  459. }
  460. void __i915_request_unsubmit(struct i915_request *request)
  461. {
  462. struct intel_engine_cs *engine = request->engine;
  463. GEM_TRACE("%s fence %llx:%d <- global=%d, current %d\n",
  464. engine->name,
  465. request->fence.context, request->fence.seqno,
  466. request->global_seqno,
  467. intel_engine_get_seqno(engine));
  468. GEM_BUG_ON(!irqs_disabled());
  469. lockdep_assert_held(&engine->timeline.lock);
  470. /*
  471. * Only unwind in reverse order, required so that the per-context list
  472. * is kept in seqno/ring order.
  473. */
  474. GEM_BUG_ON(!request->global_seqno);
  475. GEM_BUG_ON(request->global_seqno != engine->timeline.seqno);
  476. GEM_BUG_ON(i915_seqno_passed(intel_engine_get_seqno(engine),
  477. request->global_seqno));
  478. engine->timeline.seqno--;
  479. /* We may be recursing from the signal callback of another i915 fence */
  480. spin_lock_nested(&request->lock, SINGLE_DEPTH_NESTING);
  481. request->global_seqno = 0;
  482. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  483. intel_engine_cancel_signaling(request);
  484. spin_unlock(&request->lock);
  485. /* Transfer back from the global per-engine timeline to per-context */
  486. move_to_timeline(request, request->timeline);
  487. /*
  488. * We don't need to wake_up any waiters on request->execute, they
  489. * will get woken by any other event or us re-adding this request
  490. * to the engine timeline (__i915_request_submit()). The waiters
  491. * should be quite adapt at finding that the request now has a new
  492. * global_seqno to the one they went to sleep on.
  493. */
  494. }
  495. void i915_request_unsubmit(struct i915_request *request)
  496. {
  497. struct intel_engine_cs *engine = request->engine;
  498. unsigned long flags;
  499. /* Will be called from irq-context when using foreign fences. */
  500. spin_lock_irqsave(&engine->timeline.lock, flags);
  501. __i915_request_unsubmit(request);
  502. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  503. }
  504. static int __i915_sw_fence_call
  505. submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
  506. {
  507. struct i915_request *request =
  508. container_of(fence, typeof(*request), submit);
  509. switch (state) {
  510. case FENCE_COMPLETE:
  511. trace_i915_request_submit(request);
  512. /*
  513. * We need to serialize use of the submit_request() callback
  514. * with its hotplugging performed during an emergency
  515. * i915_gem_set_wedged(). We use the RCU mechanism to mark the
  516. * critical section in order to force i915_gem_set_wedged() to
  517. * wait until the submit_request() is completed before
  518. * proceeding.
  519. */
  520. rcu_read_lock();
  521. request->engine->submit_request(request);
  522. rcu_read_unlock();
  523. break;
  524. case FENCE_FREE:
  525. i915_request_put(request);
  526. break;
  527. }
  528. return NOTIFY_DONE;
  529. }
  530. /**
  531. * i915_request_alloc - allocate a request structure
  532. *
  533. * @engine: engine that we wish to issue the request on.
  534. * @ctx: context that the request will be associated with.
  535. *
  536. * Returns a pointer to the allocated request if successful,
  537. * or an error code if not.
  538. */
  539. struct i915_request *
  540. i915_request_alloc(struct intel_engine_cs *engine, struct i915_gem_context *ctx)
  541. {
  542. struct drm_i915_private *i915 = engine->i915;
  543. struct i915_request *rq;
  544. struct intel_context *ce;
  545. int ret;
  546. lockdep_assert_held(&i915->drm.struct_mutex);
  547. /*
  548. * Preempt contexts are reserved for exclusive use to inject a
  549. * preemption context switch. They are never to be used for any trivial
  550. * request!
  551. */
  552. GEM_BUG_ON(ctx == i915->preempt_context);
  553. /*
  554. * ABI: Before userspace accesses the GPU (e.g. execbuffer), report
  555. * EIO if the GPU is already wedged.
  556. */
  557. if (i915_terminally_wedged(&i915->gpu_error))
  558. return ERR_PTR(-EIO);
  559. /*
  560. * Pinning the contexts may generate requests in order to acquire
  561. * GGTT space, so do this first before we reserve a seqno for
  562. * ourselves.
  563. */
  564. ce = intel_context_pin(ctx, engine);
  565. if (IS_ERR(ce))
  566. return ERR_CAST(ce);
  567. ret = reserve_gt(i915);
  568. if (ret)
  569. goto err_unpin;
  570. ret = intel_ring_wait_for_space(ce->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(&ce->ring->request_list, typeof(*rq), ring_link);
  575. if (!list_is_last(&rq->ring_link, &ce->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->gem_context = ctx;
  636. rq->hw_context = ce;
  637. rq->ring = ce->ring;
  638. rq->timeline = ce->ring->timeline;
  639. GEM_BUG_ON(rq->timeline == &engine->timeline);
  640. spin_lock_init(&rq->lock);
  641. dma_fence_init(&rq->fence,
  642. &i915_fence_ops,
  643. &rq->lock,
  644. rq->timeline->fence_context,
  645. timeline_get_seqno(rq->timeline));
  646. /* We bump the ref for the fence chain */
  647. i915_sw_fence_init(&i915_request_get(rq)->submit, submit_notify);
  648. init_waitqueue_head(&rq->execute);
  649. i915_sched_node_init(&rq->sched);
  650. /* No zalloc, must clear what we need by hand */
  651. rq->global_seqno = 0;
  652. rq->signaling.wait.seqno = 0;
  653. rq->file_priv = NULL;
  654. rq->batch = NULL;
  655. rq->capture_list = NULL;
  656. rq->waitboost = false;
  657. /*
  658. * Reserve space in the ring buffer for all the commands required to
  659. * eventually emit this request. This is to guarantee that the
  660. * i915_request_add() call can't fail. Note that the reserve may need
  661. * to be redone if the request is not actually submitted straight
  662. * away, e.g. because a GPU scheduler has deferred it.
  663. */
  664. rq->reserved_space = MIN_SPACE_FOR_ADD_REQUEST;
  665. GEM_BUG_ON(rq->reserved_space < engine->emit_breadcrumb_sz);
  666. /*
  667. * Record the position of the start of the request so that
  668. * should we detect the updated seqno part-way through the
  669. * GPU processing the request, we never over-estimate the
  670. * position of the head.
  671. */
  672. rq->head = rq->ring->emit;
  673. /* Unconditionally invalidate GPU caches and TLBs. */
  674. ret = engine->emit_flush(rq, EMIT_INVALIDATE);
  675. if (ret)
  676. goto err_unwind;
  677. ret = engine->request_alloc(rq);
  678. if (ret)
  679. goto err_unwind;
  680. /* Keep a second pin for the dual retirement along engine and ring */
  681. __intel_context_pin(ce);
  682. rq->infix = rq->ring->emit; /* end of header; start of user payload */
  683. /* Check that we didn't interrupt ourselves with a new request */
  684. GEM_BUG_ON(rq->timeline->seqno != rq->fence.seqno);
  685. return rq;
  686. err_unwind:
  687. ce->ring->emit = rq->head;
  688. /* Make sure we didn't add ourselves to external state before freeing */
  689. GEM_BUG_ON(!list_empty(&rq->active_list));
  690. GEM_BUG_ON(!list_empty(&rq->sched.signalers_list));
  691. GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
  692. kmem_cache_free(i915->requests, rq);
  693. err_unreserve:
  694. unreserve_gt(i915);
  695. err_unpin:
  696. intel_context_unpin(ce);
  697. return ERR_PTR(ret);
  698. }
  699. static int
  700. i915_request_await_request(struct i915_request *to, struct i915_request *from)
  701. {
  702. int ret;
  703. GEM_BUG_ON(to == from);
  704. GEM_BUG_ON(to->timeline == from->timeline);
  705. if (i915_request_completed(from))
  706. return 0;
  707. if (to->engine->schedule) {
  708. ret = i915_sched_node_add_dependency(to->i915,
  709. &to->sched,
  710. &from->sched);
  711. if (ret < 0)
  712. return ret;
  713. }
  714. if (to->engine == from->engine) {
  715. ret = i915_sw_fence_await_sw_fence_gfp(&to->submit,
  716. &from->submit,
  717. I915_FENCE_GFP);
  718. return ret < 0 ? ret : 0;
  719. }
  720. if (to->engine->semaphore.sync_to) {
  721. u32 seqno;
  722. GEM_BUG_ON(!from->engine->semaphore.signal);
  723. seqno = i915_request_global_seqno(from);
  724. if (!seqno)
  725. goto await_dma_fence;
  726. if (seqno <= to->timeline->global_sync[from->engine->id])
  727. return 0;
  728. trace_i915_gem_ring_sync_to(to, from);
  729. ret = to->engine->semaphore.sync_to(to, from);
  730. if (ret)
  731. return ret;
  732. to->timeline->global_sync[from->engine->id] = seqno;
  733. return 0;
  734. }
  735. await_dma_fence:
  736. ret = i915_sw_fence_await_dma_fence(&to->submit,
  737. &from->fence, 0,
  738. I915_FENCE_GFP);
  739. return ret < 0 ? ret : 0;
  740. }
  741. int
  742. i915_request_await_dma_fence(struct i915_request *rq, struct dma_fence *fence)
  743. {
  744. struct dma_fence **child = &fence;
  745. unsigned int nchild = 1;
  746. int ret;
  747. /*
  748. * Note that if the fence-array was created in signal-on-any mode,
  749. * we should *not* decompose it into its individual fences. However,
  750. * we don't currently store which mode the fence-array is operating
  751. * in. Fortunately, the only user of signal-on-any is private to
  752. * amdgpu and we should not see any incoming fence-array from
  753. * sync-file being in signal-on-any mode.
  754. */
  755. if (dma_fence_is_array(fence)) {
  756. struct dma_fence_array *array = to_dma_fence_array(fence);
  757. child = array->fences;
  758. nchild = array->num_fences;
  759. GEM_BUG_ON(!nchild);
  760. }
  761. do {
  762. fence = *child++;
  763. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  764. continue;
  765. /*
  766. * Requests on the same timeline are explicitly ordered, along
  767. * with their dependencies, by i915_request_add() which ensures
  768. * that requests are submitted in-order through each ring.
  769. */
  770. if (fence->context == rq->fence.context)
  771. continue;
  772. /* Squash repeated waits to the same timelines */
  773. if (fence->context != rq->i915->mm.unordered_timeline &&
  774. i915_timeline_sync_is_later(rq->timeline, fence))
  775. continue;
  776. if (dma_fence_is_i915(fence))
  777. ret = i915_request_await_request(rq, to_request(fence));
  778. else
  779. ret = i915_sw_fence_await_dma_fence(&rq->submit, fence,
  780. I915_FENCE_TIMEOUT,
  781. I915_FENCE_GFP);
  782. if (ret < 0)
  783. return ret;
  784. /* Record the latest fence used against each timeline */
  785. if (fence->context != rq->i915->mm.unordered_timeline)
  786. i915_timeline_sync_set(rq->timeline, fence);
  787. } while (--nchild);
  788. return 0;
  789. }
  790. /**
  791. * i915_request_await_object - set this request to (async) wait upon a bo
  792. * @to: request we are wishing to use
  793. * @obj: object which may be in use on another ring.
  794. * @write: whether the wait is on behalf of a writer
  795. *
  796. * This code is meant to abstract object synchronization with the GPU.
  797. * Conceptually we serialise writes between engines inside the GPU.
  798. * We only allow one engine to write into a buffer at any time, but
  799. * multiple readers. To ensure each has a coherent view of memory, we must:
  800. *
  801. * - If there is an outstanding write request to the object, the new
  802. * request must wait for it to complete (either CPU or in hw, requests
  803. * on the same ring will be naturally ordered).
  804. *
  805. * - If we are a write request (pending_write_domain is set), the new
  806. * request must wait for outstanding read requests to complete.
  807. *
  808. * Returns 0 if successful, else propagates up the lower layer error.
  809. */
  810. int
  811. i915_request_await_object(struct i915_request *to,
  812. struct drm_i915_gem_object *obj,
  813. bool write)
  814. {
  815. struct dma_fence *excl;
  816. int ret = 0;
  817. if (write) {
  818. struct dma_fence **shared;
  819. unsigned int count, i;
  820. ret = reservation_object_get_fences_rcu(obj->resv,
  821. &excl, &count, &shared);
  822. if (ret)
  823. return ret;
  824. for (i = 0; i < count; i++) {
  825. ret = i915_request_await_dma_fence(to, shared[i]);
  826. if (ret)
  827. break;
  828. dma_fence_put(shared[i]);
  829. }
  830. for (; i < count; i++)
  831. dma_fence_put(shared[i]);
  832. kfree(shared);
  833. } else {
  834. excl = reservation_object_get_excl_rcu(obj->resv);
  835. }
  836. if (excl) {
  837. if (ret == 0)
  838. ret = i915_request_await_dma_fence(to, excl);
  839. dma_fence_put(excl);
  840. }
  841. return ret;
  842. }
  843. /*
  844. * NB: This function is not allowed to fail. Doing so would mean the the
  845. * request is not being tracked for completion but the work itself is
  846. * going to happen on the hardware. This would be a Bad Thing(tm).
  847. */
  848. void i915_request_add(struct i915_request *request)
  849. {
  850. struct intel_engine_cs *engine = request->engine;
  851. struct i915_timeline *timeline = request->timeline;
  852. struct intel_ring *ring = request->ring;
  853. struct i915_request *prev;
  854. u32 *cs;
  855. GEM_TRACE("%s fence %llx:%d\n",
  856. engine->name, request->fence.context, request->fence.seqno);
  857. lockdep_assert_held(&request->i915->drm.struct_mutex);
  858. trace_i915_request_add(request);
  859. /*
  860. * Make sure that no request gazumped us - if it was allocated after
  861. * our i915_request_alloc() and called __i915_request_add() before
  862. * us, the timeline will hold its seqno which is later than ours.
  863. */
  864. GEM_BUG_ON(timeline->seqno != request->fence.seqno);
  865. /*
  866. * To ensure that this call will not fail, space for its emissions
  867. * should already have been reserved in the ring buffer. Let the ring
  868. * know that it is time to use that space up.
  869. */
  870. request->reserved_space = 0;
  871. engine->emit_flush(request, EMIT_FLUSH);
  872. /*
  873. * Record the position of the start of the breadcrumb so that
  874. * should we detect the updated seqno part-way through the
  875. * GPU processing the request, we never over-estimate the
  876. * position of the ring's HEAD.
  877. */
  878. cs = intel_ring_begin(request, engine->emit_breadcrumb_sz);
  879. GEM_BUG_ON(IS_ERR(cs));
  880. request->postfix = intel_ring_offset(request, cs);
  881. /*
  882. * Seal the request and mark it as pending execution. Note that
  883. * we may inspect this state, without holding any locks, during
  884. * hangcheck. Hence we apply the barrier to ensure that we do not
  885. * see a more recent value in the hws than we are tracking.
  886. */
  887. prev = i915_gem_active_raw(&timeline->last_request,
  888. &request->i915->drm.struct_mutex);
  889. if (prev && !i915_request_completed(prev)) {
  890. i915_sw_fence_await_sw_fence(&request->submit, &prev->submit,
  891. &request->submitq);
  892. if (engine->schedule)
  893. __i915_sched_node_add_dependency(&request->sched,
  894. &prev->sched,
  895. &request->dep,
  896. 0);
  897. }
  898. spin_lock_irq(&timeline->lock);
  899. list_add_tail(&request->link, &timeline->requests);
  900. spin_unlock_irq(&timeline->lock);
  901. GEM_BUG_ON(timeline->seqno != request->fence.seqno);
  902. i915_gem_active_set(&timeline->last_request, request);
  903. list_add_tail(&request->ring_link, &ring->request_list);
  904. if (list_is_first(&request->ring_link, &ring->request_list)) {
  905. GEM_TRACE("marking %s as active\n", ring->timeline->name);
  906. list_add(&ring->active_link, &request->i915->gt.active_rings);
  907. }
  908. request->emitted_jiffies = jiffies;
  909. /*
  910. * Let the backend know a new request has arrived that may need
  911. * to adjust the existing execution schedule due to a high priority
  912. * request - i.e. we may want to preempt the current request in order
  913. * to run a high priority dependency chain *before* we can execute this
  914. * request.
  915. *
  916. * This is called before the request is ready to run so that we can
  917. * decide whether to preempt the entire chain so that it is ready to
  918. * run at the earliest possible convenience.
  919. */
  920. local_bh_disable();
  921. rcu_read_lock(); /* RCU serialisation for set-wedged protection */
  922. if (engine->schedule)
  923. engine->schedule(request, &request->gem_context->sched);
  924. rcu_read_unlock();
  925. i915_sw_fence_commit(&request->submit);
  926. local_bh_enable(); /* Kick the execlists tasklet if just scheduled */
  927. /*
  928. * In typical scenarios, we do not expect the previous request on
  929. * the timeline to be still tracked by timeline->last_request if it
  930. * has been completed. If the completed request is still here, that
  931. * implies that request retirement is a long way behind submission,
  932. * suggesting that we haven't been retiring frequently enough from
  933. * the combination of retire-before-alloc, waiters and the background
  934. * retirement worker. So if the last request on this timeline was
  935. * already completed, do a catch up pass, flushing the retirement queue
  936. * up to this client. Since we have now moved the heaviest operations
  937. * during retirement onto secondary workers, such as freeing objects
  938. * or contexts, retiring a bunch of requests is mostly list management
  939. * (and cache misses), and so we should not be overly penalizing this
  940. * client by performing excess work, though we may still performing
  941. * work on behalf of others -- but instead we should benefit from
  942. * improved resource management. (Well, that's the theory at least.)
  943. */
  944. if (prev && i915_request_completed(prev))
  945. i915_request_retire_upto(prev);
  946. }
  947. static unsigned long local_clock_us(unsigned int *cpu)
  948. {
  949. unsigned long t;
  950. /*
  951. * Cheaply and approximately convert from nanoseconds to microseconds.
  952. * The result and subsequent calculations are also defined in the same
  953. * approximate microseconds units. The principal source of timing
  954. * error here is from the simple truncation.
  955. *
  956. * Note that local_clock() is only defined wrt to the current CPU;
  957. * the comparisons are no longer valid if we switch CPUs. Instead of
  958. * blocking preemption for the entire busywait, we can detect the CPU
  959. * switch and use that as indicator of system load and a reason to
  960. * stop busywaiting, see busywait_stop().
  961. */
  962. *cpu = get_cpu();
  963. t = local_clock() >> 10;
  964. put_cpu();
  965. return t;
  966. }
  967. static bool busywait_stop(unsigned long timeout, unsigned int cpu)
  968. {
  969. unsigned int this_cpu;
  970. if (time_after(local_clock_us(&this_cpu), timeout))
  971. return true;
  972. return this_cpu != cpu;
  973. }
  974. static bool __i915_spin_request(const struct i915_request *rq,
  975. u32 seqno, int state, unsigned long timeout_us)
  976. {
  977. struct intel_engine_cs *engine = rq->engine;
  978. unsigned int irq, cpu;
  979. GEM_BUG_ON(!seqno);
  980. /*
  981. * Only wait for the request if we know it is likely to complete.
  982. *
  983. * We don't track the timestamps around requests, nor the average
  984. * request length, so we do not have a good indicator that this
  985. * request will complete within the timeout. What we do know is the
  986. * order in which requests are executed by the engine and so we can
  987. * tell if the request has started. If the request hasn't started yet,
  988. * it is a fair assumption that it will not complete within our
  989. * relatively short timeout.
  990. */
  991. if (!i915_seqno_passed(intel_engine_get_seqno(engine), seqno - 1))
  992. return false;
  993. /*
  994. * When waiting for high frequency requests, e.g. during synchronous
  995. * rendering split between the CPU and GPU, the finite amount of time
  996. * required to set up the irq and wait upon it limits the response
  997. * rate. By busywaiting on the request completion for a short while we
  998. * can service the high frequency waits as quick as possible. However,
  999. * if it is a slow request, we want to sleep as quickly as possible.
  1000. * The tradeoff between waiting and sleeping is roughly the time it
  1001. * takes to sleep on a request, on the order of a microsecond.
  1002. */
  1003. irq = atomic_read(&engine->irq_count);
  1004. timeout_us += local_clock_us(&cpu);
  1005. do {
  1006. if (i915_seqno_passed(intel_engine_get_seqno(engine), seqno))
  1007. return seqno == i915_request_global_seqno(rq);
  1008. /*
  1009. * Seqno are meant to be ordered *before* the interrupt. If
  1010. * we see an interrupt without a corresponding seqno advance,
  1011. * assume we won't see one in the near future but require
  1012. * the engine->seqno_barrier() to fixup coherency.
  1013. */
  1014. if (atomic_read(&engine->irq_count) != irq)
  1015. break;
  1016. if (signal_pending_state(state, current))
  1017. break;
  1018. if (busywait_stop(timeout_us, cpu))
  1019. break;
  1020. cpu_relax();
  1021. } while (!need_resched());
  1022. return false;
  1023. }
  1024. static bool __i915_wait_request_check_and_reset(struct i915_request *request)
  1025. {
  1026. struct i915_gpu_error *error = &request->i915->gpu_error;
  1027. if (likely(!i915_reset_handoff(error)))
  1028. return false;
  1029. __set_current_state(TASK_RUNNING);
  1030. i915_reset(request->i915, error->stalled_mask, error->reason);
  1031. return true;
  1032. }
  1033. /**
  1034. * i915_request_wait - wait until execution of request has finished
  1035. * @rq: the request to wait upon
  1036. * @flags: how to wait
  1037. * @timeout: how long to wait in jiffies
  1038. *
  1039. * i915_request_wait() waits for the request to be completed, for a
  1040. * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
  1041. * unbounded wait).
  1042. *
  1043. * If the caller holds the struct_mutex, the caller must pass I915_WAIT_LOCKED
  1044. * in via the flags, and vice versa if the struct_mutex is not held, the caller
  1045. * must not specify that the wait is locked.
  1046. *
  1047. * Returns the remaining time (in jiffies) if the request completed, which may
  1048. * be zero or -ETIME if the request is unfinished after the timeout expires.
  1049. * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
  1050. * pending before the request completes.
  1051. */
  1052. long i915_request_wait(struct i915_request *rq,
  1053. unsigned int flags,
  1054. long timeout)
  1055. {
  1056. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  1057. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  1058. wait_queue_head_t *errq = &rq->i915->gpu_error.wait_queue;
  1059. DEFINE_WAIT_FUNC(reset, default_wake_function);
  1060. DEFINE_WAIT_FUNC(exec, default_wake_function);
  1061. struct intel_wait wait;
  1062. might_sleep();
  1063. #if IS_ENABLED(CONFIG_LOCKDEP)
  1064. GEM_BUG_ON(debug_locks &&
  1065. !!lockdep_is_held(&rq->i915->drm.struct_mutex) !=
  1066. !!(flags & I915_WAIT_LOCKED));
  1067. #endif
  1068. GEM_BUG_ON(timeout < 0);
  1069. if (i915_request_completed(rq))
  1070. return timeout;
  1071. if (!timeout)
  1072. return -ETIME;
  1073. trace_i915_request_wait_begin(rq, flags);
  1074. add_wait_queue(&rq->execute, &exec);
  1075. if (flags & I915_WAIT_LOCKED)
  1076. add_wait_queue(errq, &reset);
  1077. intel_wait_init(&wait, rq);
  1078. restart:
  1079. do {
  1080. set_current_state(state);
  1081. if (intel_wait_update_request(&wait, rq))
  1082. break;
  1083. if (flags & I915_WAIT_LOCKED &&
  1084. __i915_wait_request_check_and_reset(rq))
  1085. continue;
  1086. if (signal_pending_state(state, current)) {
  1087. timeout = -ERESTARTSYS;
  1088. goto complete;
  1089. }
  1090. if (!timeout) {
  1091. timeout = -ETIME;
  1092. goto complete;
  1093. }
  1094. timeout = io_schedule_timeout(timeout);
  1095. } while (1);
  1096. GEM_BUG_ON(!intel_wait_has_seqno(&wait));
  1097. GEM_BUG_ON(!i915_sw_fence_signaled(&rq->submit));
  1098. /* Optimistic short spin before touching IRQs */
  1099. if (__i915_spin_request(rq, wait.seqno, state, 5))
  1100. goto complete;
  1101. set_current_state(state);
  1102. if (intel_engine_add_wait(rq->engine, &wait))
  1103. /*
  1104. * In order to check that we haven't missed the interrupt
  1105. * as we enabled it, we need to kick ourselves to do a
  1106. * coherent check on the seqno before we sleep.
  1107. */
  1108. goto wakeup;
  1109. if (flags & I915_WAIT_LOCKED)
  1110. __i915_wait_request_check_and_reset(rq);
  1111. for (;;) {
  1112. if (signal_pending_state(state, current)) {
  1113. timeout = -ERESTARTSYS;
  1114. break;
  1115. }
  1116. if (!timeout) {
  1117. timeout = -ETIME;
  1118. break;
  1119. }
  1120. timeout = io_schedule_timeout(timeout);
  1121. if (intel_wait_complete(&wait) &&
  1122. intel_wait_check_request(&wait, rq))
  1123. break;
  1124. set_current_state(state);
  1125. wakeup:
  1126. /*
  1127. * Carefully check if the request is complete, giving time
  1128. * for the seqno to be visible following the interrupt.
  1129. * We also have to check in case we are kicked by the GPU
  1130. * reset in order to drop the struct_mutex.
  1131. */
  1132. if (__i915_request_irq_complete(rq))
  1133. break;
  1134. /*
  1135. * If the GPU is hung, and we hold the lock, reset the GPU
  1136. * and then check for completion. On a full reset, the engine's
  1137. * HW seqno will be advanced passed us and we are complete.
  1138. * If we do a partial reset, we have to wait for the GPU to
  1139. * resume and update the breadcrumb.
  1140. *
  1141. * If we don't hold the mutex, we can just wait for the worker
  1142. * to come along and update the breadcrumb (either directly
  1143. * itself, or indirectly by recovering the GPU).
  1144. */
  1145. if (flags & I915_WAIT_LOCKED &&
  1146. __i915_wait_request_check_and_reset(rq))
  1147. continue;
  1148. /* Only spin if we know the GPU is processing this request */
  1149. if (__i915_spin_request(rq, wait.seqno, state, 2))
  1150. break;
  1151. if (!intel_wait_check_request(&wait, rq)) {
  1152. intel_engine_remove_wait(rq->engine, &wait);
  1153. goto restart;
  1154. }
  1155. }
  1156. intel_engine_remove_wait(rq->engine, &wait);
  1157. complete:
  1158. __set_current_state(TASK_RUNNING);
  1159. if (flags & I915_WAIT_LOCKED)
  1160. remove_wait_queue(errq, &reset);
  1161. remove_wait_queue(&rq->execute, &exec);
  1162. trace_i915_request_wait_end(rq);
  1163. return timeout;
  1164. }
  1165. static void ring_retire_requests(struct intel_ring *ring)
  1166. {
  1167. struct i915_request *request, *next;
  1168. list_for_each_entry_safe(request, next,
  1169. &ring->request_list, ring_link) {
  1170. if (!i915_request_completed(request))
  1171. break;
  1172. i915_request_retire(request);
  1173. }
  1174. }
  1175. void i915_retire_requests(struct drm_i915_private *i915)
  1176. {
  1177. struct intel_ring *ring, *tmp;
  1178. lockdep_assert_held(&i915->drm.struct_mutex);
  1179. if (!i915->gt.active_requests)
  1180. return;
  1181. list_for_each_entry_safe(ring, tmp, &i915->gt.active_rings, active_link)
  1182. ring_retire_requests(ring);
  1183. }
  1184. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1185. #include "selftests/mock_request.c"
  1186. #include "selftests/i915_request.c"
  1187. #endif