i915_request.c 42 KB

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