i915_request.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311
  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/dma-fence-array.h>
  25. #include <linux/dma-fence-chain.h>
  26. #include <linux/irq_work.h>
  27. #include <linux/prefetch.h>
  28. #include <linux/sched.h>
  29. #include <linux/sched/clock.h>
  30. #include <linux/sched/signal.h>
  31. #include <linux/sched/mm.h>
  32. #include "gem/i915_gem_context.h"
  33. #include "gt/intel_breadcrumbs.h"
  34. #include "gt/intel_context.h"
  35. #include "gt/intel_engine.h"
  36. #include "gt/intel_engine_heartbeat.h"
  37. #include "gt/intel_engine_regs.h"
  38. #include "gt/intel_gpu_commands.h"
  39. #include "gt/intel_reset.h"
  40. #include "gt/intel_ring.h"
  41. #include "gt/intel_rps.h"
  42. #include "i915_active.h"
  43. #include "i915_config.h"
  44. #include "i915_deps.h"
  45. #include "i915_driver.h"
  46. #include "i915_drv.h"
  47. #include "i915_trace.h"
  48. struct execute_cb {
  49. struct irq_work work;
  50. struct i915_sw_fence *fence;
  51. };
  52. static struct kmem_cache *slab_requests;
  53. static struct kmem_cache *slab_execute_cbs;
  54. static const char *i915_fence_get_driver_name(struct dma_fence *fence)
  55. {
  56. return dev_name(to_request(fence)->i915->drm.dev);
  57. }
  58. static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
  59. {
  60. const struct i915_gem_context *ctx;
  61. /*
  62. * The timeline struct (as part of the ppgtt underneath a context)
  63. * may be freed when the request is no longer in use by the GPU.
  64. * We could extend the life of a context to beyond that of all
  65. * fences, possibly keeping the hw resource around indefinitely,
  66. * or we just give them a false name. Since
  67. * dma_fence_ops.get_timeline_name is a debug feature, the occasional
  68. * lie seems justifiable.
  69. */
  70. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  71. return "signaled";
  72. ctx = i915_request_gem_context(to_request(fence));
  73. if (!ctx)
  74. return "[" DRIVER_NAME "]";
  75. return ctx->name;
  76. }
  77. static bool i915_fence_signaled(struct dma_fence *fence)
  78. {
  79. return i915_request_completed(to_request(fence));
  80. }
  81. static bool i915_fence_enable_signaling(struct dma_fence *fence)
  82. {
  83. return i915_request_enable_breadcrumb(to_request(fence));
  84. }
  85. static signed long i915_fence_wait(struct dma_fence *fence,
  86. bool interruptible,
  87. signed long timeout)
  88. {
  89. return i915_request_wait_timeout(to_request(fence),
  90. interruptible | I915_WAIT_PRIORITY,
  91. timeout);
  92. }
  93. struct kmem_cache *i915_request_slab_cache(void)
  94. {
  95. return slab_requests;
  96. }
  97. static void i915_fence_release(struct dma_fence *fence)
  98. {
  99. struct i915_request *rq = to_request(fence);
  100. GEM_BUG_ON(rq->guc_prio != GUC_PRIO_INIT &&
  101. rq->guc_prio != GUC_PRIO_FINI);
  102. i915_request_free_capture_list(fetch_and_zero(&rq->capture_list));
  103. if (rq->batch_res) {
  104. i915_vma_resource_put(rq->batch_res);
  105. rq->batch_res = NULL;
  106. }
  107. /*
  108. * The request is put onto a RCU freelist (i.e. the address
  109. * is immediately reused), mark the fences as being freed now.
  110. * Otherwise the debugobjects for the fences are only marked as
  111. * freed when the slab cache itself is freed, and so we would get
  112. * caught trying to reuse dead objects.
  113. */
  114. i915_sw_fence_fini(&rq->submit);
  115. i915_sw_fence_fini(&rq->semaphore);
  116. /*
  117. * Keep one request on each engine for reserved use under mempressure.
  118. *
  119. * We do not hold a reference to the engine here and so have to be
  120. * very careful in what rq->engine we poke. The virtual engine is
  121. * referenced via the rq->context and we released that ref during
  122. * i915_request_retire(), ergo we must not dereference a virtual
  123. * engine here. Not that we would want to, as the only consumer of
  124. * the reserved engine->request_pool is the power management parking,
  125. * which must-not-fail, and that is only run on the physical engines.
  126. *
  127. * Since the request must have been executed to be have completed,
  128. * we know that it will have been processed by the HW and will
  129. * not be unsubmitted again, so rq->engine and rq->execution_mask
  130. * at this point is stable. rq->execution_mask will be a single
  131. * bit if the last and _only_ engine it could execution on was a
  132. * physical engine, if it's multiple bits then it started on and
  133. * could still be on a virtual engine. Thus if the mask is not a
  134. * power-of-two we assume that rq->engine may still be a virtual
  135. * engine and so a dangling invalid pointer that we cannot dereference
  136. *
  137. * For example, consider the flow of a bonded request through a virtual
  138. * engine. The request is created with a wide engine mask (all engines
  139. * that we might execute on). On processing the bond, the request mask
  140. * is reduced to one or more engines. If the request is subsequently
  141. * bound to a single engine, it will then be constrained to only
  142. * execute on that engine and never returned to the virtual engine
  143. * after timeslicing away, see __unwind_incomplete_requests(). Thus we
  144. * know that if the rq->execution_mask is a single bit, rq->engine
  145. * can be a physical engine with the exact corresponding mask.
  146. */
  147. if (is_power_of_2(rq->execution_mask) &&
  148. !cmpxchg(&rq->engine->request_pool, NULL, rq))
  149. return;
  150. kmem_cache_free(slab_requests, rq);
  151. }
  152. const struct dma_fence_ops i915_fence_ops = {
  153. .get_driver_name = i915_fence_get_driver_name,
  154. .get_timeline_name = i915_fence_get_timeline_name,
  155. .enable_signaling = i915_fence_enable_signaling,
  156. .signaled = i915_fence_signaled,
  157. .wait = i915_fence_wait,
  158. .release = i915_fence_release,
  159. };
  160. static void irq_execute_cb(struct irq_work *wrk)
  161. {
  162. struct execute_cb *cb = container_of(wrk, typeof(*cb), work);
  163. i915_sw_fence_complete(cb->fence);
  164. kmem_cache_free(slab_execute_cbs, cb);
  165. }
  166. static __always_inline void
  167. __notify_execute_cb(struct i915_request *rq, bool (*fn)(struct irq_work *wrk))
  168. {
  169. struct execute_cb *cb, *cn;
  170. if (llist_empty(&rq->execute_cb))
  171. return;
  172. llist_for_each_entry_safe(cb, cn,
  173. llist_del_all(&rq->execute_cb),
  174. work.node.llist)
  175. fn(&cb->work);
  176. }
  177. static void __notify_execute_cb_irq(struct i915_request *rq)
  178. {
  179. __notify_execute_cb(rq, irq_work_queue);
  180. }
  181. static bool irq_work_imm(struct irq_work *wrk)
  182. {
  183. wrk->func(wrk);
  184. return false;
  185. }
  186. void i915_request_notify_execute_cb_imm(struct i915_request *rq)
  187. {
  188. __notify_execute_cb(rq, irq_work_imm);
  189. }
  190. static void __i915_request_fill(struct i915_request *rq, u8 val)
  191. {
  192. void *vaddr = rq->ring->vaddr;
  193. u32 head;
  194. head = rq->infix;
  195. if (rq->postfix < head) {
  196. memset(vaddr + head, val, rq->ring->size - head);
  197. head = 0;
  198. }
  199. memset(vaddr + head, val, rq->postfix - head);
  200. }
  201. /**
  202. * i915_request_active_engine
  203. * @rq: request to inspect
  204. * @active: pointer in which to return the active engine
  205. *
  206. * Fills the currently active engine to the @active pointer if the request
  207. * is active and still not completed.
  208. *
  209. * Returns true if request was active or false otherwise.
  210. */
  211. bool
  212. i915_request_active_engine(struct i915_request *rq,
  213. struct intel_engine_cs **active)
  214. {
  215. struct intel_engine_cs *engine, *locked;
  216. bool ret = false;
  217. /*
  218. * Serialise with __i915_request_submit() so that it sees
  219. * is-banned?, or we know the request is already inflight.
  220. *
  221. * Note that rq->engine is unstable, and so we double
  222. * check that we have acquired the lock on the final engine.
  223. */
  224. locked = READ_ONCE(rq->engine);
  225. spin_lock_irq(&locked->sched_engine->lock);
  226. while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
  227. spin_unlock(&locked->sched_engine->lock);
  228. locked = engine;
  229. spin_lock(&locked->sched_engine->lock);
  230. }
  231. if (i915_request_is_active(rq)) {
  232. if (!__i915_request_is_complete(rq))
  233. *active = locked;
  234. ret = true;
  235. }
  236. spin_unlock_irq(&locked->sched_engine->lock);
  237. return ret;
  238. }
  239. static void __rq_init_watchdog(struct i915_request *rq)
  240. {
  241. rq->watchdog.timer.function = NULL;
  242. }
  243. static enum hrtimer_restart __rq_watchdog_expired(struct hrtimer *hrtimer)
  244. {
  245. struct i915_request *rq =
  246. container_of(hrtimer, struct i915_request, watchdog.timer);
  247. struct intel_gt *gt = rq->engine->gt;
  248. if (!i915_request_completed(rq)) {
  249. if (llist_add(&rq->watchdog.link, &gt->watchdog.list))
  250. queue_work(gt->i915->unordered_wq, &gt->watchdog.work);
  251. } else {
  252. i915_request_put(rq);
  253. }
  254. return HRTIMER_NORESTART;
  255. }
  256. static void __rq_arm_watchdog(struct i915_request *rq)
  257. {
  258. struct i915_request_watchdog *wdg = &rq->watchdog;
  259. struct intel_context *ce = rq->context;
  260. if (!ce->watchdog.timeout_us)
  261. return;
  262. i915_request_get(rq);
  263. hrtimer_init(&wdg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  264. wdg->timer.function = __rq_watchdog_expired;
  265. hrtimer_start_range_ns(&wdg->timer,
  266. ns_to_ktime(ce->watchdog.timeout_us *
  267. NSEC_PER_USEC),
  268. NSEC_PER_MSEC,
  269. HRTIMER_MODE_REL);
  270. }
  271. static void __rq_cancel_watchdog(struct i915_request *rq)
  272. {
  273. struct i915_request_watchdog *wdg = &rq->watchdog;
  274. if (wdg->timer.function && hrtimer_try_to_cancel(&wdg->timer) > 0)
  275. i915_request_put(rq);
  276. }
  277. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  278. /**
  279. * i915_request_free_capture_list - Free a capture list
  280. * @capture: Pointer to the first list item or NULL
  281. *
  282. */
  283. void i915_request_free_capture_list(struct i915_capture_list *capture)
  284. {
  285. while (capture) {
  286. struct i915_capture_list *next = capture->next;
  287. i915_vma_resource_put(capture->vma_res);
  288. kfree(capture);
  289. capture = next;
  290. }
  291. }
  292. #define assert_capture_list_is_null(_rq) GEM_BUG_ON((_rq)->capture_list)
  293. #define clear_capture_list(_rq) ((_rq)->capture_list = NULL)
  294. #else
  295. #define i915_request_free_capture_list(_a) do {} while (0)
  296. #define assert_capture_list_is_null(_a) do {} while (0)
  297. #define clear_capture_list(_rq) do {} while (0)
  298. #endif
  299. bool i915_request_retire(struct i915_request *rq)
  300. {
  301. if (!__i915_request_is_complete(rq))
  302. return false;
  303. RQ_TRACE(rq, "\n");
  304. GEM_BUG_ON(!i915_sw_fence_signaled(&rq->submit));
  305. trace_i915_request_retire(rq);
  306. i915_request_mark_complete(rq);
  307. __rq_cancel_watchdog(rq);
  308. /*
  309. * We know the GPU must have read the request to have
  310. * sent us the seqno + interrupt, so use the position
  311. * of tail of the request to update the last known position
  312. * of the GPU head.
  313. *
  314. * Note this requires that we are always called in request
  315. * completion order.
  316. */
  317. GEM_BUG_ON(!list_is_first(&rq->link,
  318. &i915_request_timeline(rq)->requests));
  319. if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
  320. /* Poison before we release our space in the ring */
  321. __i915_request_fill(rq, POISON_FREE);
  322. rq->ring->head = rq->postfix;
  323. if (!i915_request_signaled(rq)) {
  324. spin_lock_irq(&rq->lock);
  325. dma_fence_signal_locked(&rq->fence);
  326. spin_unlock_irq(&rq->lock);
  327. }
  328. if (test_and_set_bit(I915_FENCE_FLAG_BOOST, &rq->fence.flags))
  329. intel_rps_dec_waiters(&rq->engine->gt->rps);
  330. /*
  331. * We only loosely track inflight requests across preemption,
  332. * and so we may find ourselves attempting to retire a _completed_
  333. * request that we have removed from the HW and put back on a run
  334. * queue.
  335. *
  336. * As we set I915_FENCE_FLAG_ACTIVE on the request, this should be
  337. * after removing the breadcrumb and signaling it, so that we do not
  338. * inadvertently attach the breadcrumb to a completed request.
  339. */
  340. rq->engine->remove_active_request(rq);
  341. GEM_BUG_ON(!llist_empty(&rq->execute_cb));
  342. __list_del_entry(&rq->link); /* poison neither prev/next (RCU walks) */
  343. intel_context_exit(rq->context);
  344. intel_context_unpin(rq->context);
  345. i915_sched_node_fini(&rq->sched);
  346. i915_request_put(rq);
  347. return true;
  348. }
  349. void i915_request_retire_upto(struct i915_request *rq)
  350. {
  351. struct intel_timeline * const tl = i915_request_timeline(rq);
  352. struct i915_request *tmp;
  353. RQ_TRACE(rq, "\n");
  354. GEM_BUG_ON(!__i915_request_is_complete(rq));
  355. do {
  356. tmp = list_first_entry(&tl->requests, typeof(*tmp), link);
  357. GEM_BUG_ON(!i915_request_completed(tmp));
  358. } while (i915_request_retire(tmp) && tmp != rq);
  359. }
  360. static struct i915_request * const *
  361. __engine_active(struct intel_engine_cs *engine)
  362. {
  363. return READ_ONCE(engine->execlists.active);
  364. }
  365. static bool __request_in_flight(const struct i915_request *signal)
  366. {
  367. struct i915_request * const *port, *rq;
  368. bool inflight = false;
  369. if (!i915_request_is_ready(signal))
  370. return false;
  371. /*
  372. * Even if we have unwound the request, it may still be on
  373. * the GPU (preempt-to-busy). If that request is inside an
  374. * unpreemptible critical section, it will not be removed. Some
  375. * GPU functions may even be stuck waiting for the paired request
  376. * (__await_execution) to be submitted and cannot be preempted
  377. * until the bond is executing.
  378. *
  379. * As we know that there are always preemption points between
  380. * requests, we know that only the currently executing request
  381. * may be still active even though we have cleared the flag.
  382. * However, we can't rely on our tracking of ELSP[0] to know
  383. * which request is currently active and so maybe stuck, as
  384. * the tracking maybe an event behind. Instead assume that
  385. * if the context is still inflight, then it is still active
  386. * even if the active flag has been cleared.
  387. *
  388. * To further complicate matters, if there a pending promotion, the HW
  389. * may either perform a context switch to the second inflight execlists,
  390. * or it may switch to the pending set of execlists. In the case of the
  391. * latter, it may send the ACK and we process the event copying the
  392. * pending[] over top of inflight[], _overwriting_ our *active. Since
  393. * this implies the HW is arbitrating and not struck in *active, we do
  394. * not worry about complete accuracy, but we do require no read/write
  395. * tearing of the pointer [the read of the pointer must be valid, even
  396. * as the array is being overwritten, for which we require the writes
  397. * to avoid tearing.]
  398. *
  399. * Note that the read of *execlists->active may race with the promotion
  400. * of execlists->pending[] to execlists->inflight[], overwritting
  401. * the value at *execlists->active. This is fine. The promotion implies
  402. * that we received an ACK from the HW, and so the context is not
  403. * stuck -- if we do not see ourselves in *active, the inflight status
  404. * is valid. If instead we see ourselves being copied into *active,
  405. * we are inflight and may signal the callback.
  406. */
  407. if (!intel_context_inflight(signal->context))
  408. return false;
  409. rcu_read_lock();
  410. for (port = __engine_active(signal->engine);
  411. (rq = READ_ONCE(*port)); /* may race with promotion of pending[] */
  412. port++) {
  413. if (rq->context == signal->context) {
  414. inflight = i915_seqno_passed(rq->fence.seqno,
  415. signal->fence.seqno);
  416. break;
  417. }
  418. }
  419. rcu_read_unlock();
  420. return inflight;
  421. }
  422. static int
  423. __await_execution(struct i915_request *rq,
  424. struct i915_request *signal,
  425. gfp_t gfp)
  426. {
  427. struct execute_cb *cb;
  428. if (i915_request_is_active(signal))
  429. return 0;
  430. cb = kmem_cache_alloc(slab_execute_cbs, gfp);
  431. if (!cb)
  432. return -ENOMEM;
  433. cb->fence = &rq->submit;
  434. i915_sw_fence_await(cb->fence);
  435. init_irq_work(&cb->work, irq_execute_cb);
  436. /*
  437. * Register the callback first, then see if the signaler is already
  438. * active. This ensures that if we race with the
  439. * __notify_execute_cb from i915_request_submit() and we are not
  440. * included in that list, we get a second bite of the cherry and
  441. * execute it ourselves. After this point, a future
  442. * i915_request_submit() will notify us.
  443. *
  444. * In i915_request_retire() we set the ACTIVE bit on a completed
  445. * request (then flush the execute_cb). So by registering the
  446. * callback first, then checking the ACTIVE bit, we serialise with
  447. * the completed/retired request.
  448. */
  449. if (llist_add(&cb->work.node.llist, &signal->execute_cb)) {
  450. if (i915_request_is_active(signal) ||
  451. __request_in_flight(signal))
  452. i915_request_notify_execute_cb_imm(signal);
  453. }
  454. return 0;
  455. }
  456. static bool fatal_error(int error)
  457. {
  458. switch (error) {
  459. case 0: /* not an error! */
  460. case -EAGAIN: /* innocent victim of a GT reset (__i915_request_reset) */
  461. case -ETIMEDOUT: /* waiting for Godot (timer_i915_sw_fence_wake) */
  462. return false;
  463. default:
  464. return true;
  465. }
  466. }
  467. void __i915_request_skip(struct i915_request *rq)
  468. {
  469. GEM_BUG_ON(!fatal_error(rq->fence.error));
  470. if (rq->infix == rq->postfix)
  471. return;
  472. RQ_TRACE(rq, "error: %d\n", rq->fence.error);
  473. /*
  474. * As this request likely depends on state from the lost
  475. * context, clear out all the user operations leaving the
  476. * breadcrumb at the end (so we get the fence notifications).
  477. */
  478. __i915_request_fill(rq, 0);
  479. rq->infix = rq->postfix;
  480. }
  481. bool i915_request_set_error_once(struct i915_request *rq, int error)
  482. {
  483. int old;
  484. GEM_BUG_ON(!IS_ERR_VALUE((long)error));
  485. if (i915_request_signaled(rq))
  486. return false;
  487. old = READ_ONCE(rq->fence.error);
  488. do {
  489. if (fatal_error(old))
  490. return false;
  491. } while (!try_cmpxchg(&rq->fence.error, &old, error));
  492. return true;
  493. }
  494. struct i915_request *i915_request_mark_eio(struct i915_request *rq)
  495. {
  496. if (__i915_request_is_complete(rq))
  497. return NULL;
  498. GEM_BUG_ON(i915_request_signaled(rq));
  499. /* As soon as the request is completed, it may be retired */
  500. rq = i915_request_get(rq);
  501. i915_request_set_error_once(rq, -EIO);
  502. i915_request_mark_complete(rq);
  503. return rq;
  504. }
  505. bool __i915_request_submit(struct i915_request *request)
  506. {
  507. struct intel_engine_cs *engine = request->engine;
  508. bool result = false;
  509. RQ_TRACE(request, "\n");
  510. GEM_BUG_ON(!irqs_disabled());
  511. lockdep_assert_held(&engine->sched_engine->lock);
  512. /*
  513. * With the advent of preempt-to-busy, we frequently encounter
  514. * requests that we have unsubmitted from HW, but left running
  515. * until the next ack and so have completed in the meantime. On
  516. * resubmission of that completed request, we can skip
  517. * updating the payload, and execlists can even skip submitting
  518. * the request.
  519. *
  520. * We must remove the request from the caller's priority queue,
  521. * and the caller must only call us when the request is in their
  522. * priority queue, under the sched_engine->lock. This ensures that the
  523. * request has *not* yet been retired and we can safely move
  524. * the request into the engine->active.list where it will be
  525. * dropped upon retiring. (Otherwise if resubmit a *retired*
  526. * request, this would be a horrible use-after-free.)
  527. */
  528. if (__i915_request_is_complete(request)) {
  529. list_del_init(&request->sched.link);
  530. goto active;
  531. }
  532. if (unlikely(!intel_context_is_schedulable(request->context)))
  533. i915_request_set_error_once(request, -EIO);
  534. if (unlikely(fatal_error(request->fence.error)))
  535. __i915_request_skip(request);
  536. /*
  537. * Are we using semaphores when the gpu is already saturated?
  538. *
  539. * Using semaphores incurs a cost in having the GPU poll a
  540. * memory location, busywaiting for it to change. The continual
  541. * memory reads can have a noticeable impact on the rest of the
  542. * system with the extra bus traffic, stalling the cpu as it too
  543. * tries to access memory across the bus (perf stat -e bus-cycles).
  544. *
  545. * If we installed a semaphore on this request and we only submit
  546. * the request after the signaler completed, that indicates the
  547. * system is overloaded and using semaphores at this time only
  548. * increases the amount of work we are doing. If so, we disable
  549. * further use of semaphores until we are idle again, whence we
  550. * optimistically try again.
  551. */
  552. if (request->sched.semaphores &&
  553. i915_sw_fence_signaled(&request->semaphore))
  554. engine->saturated |= request->sched.semaphores;
  555. engine->emit_fini_breadcrumb(request,
  556. request->ring->vaddr + request->postfix);
  557. trace_i915_request_execute(request);
  558. if (engine->bump_serial)
  559. engine->bump_serial(engine);
  560. else
  561. engine->serial++;
  562. result = true;
  563. GEM_BUG_ON(test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
  564. engine->add_active_request(request);
  565. active:
  566. clear_bit(I915_FENCE_FLAG_PQUEUE, &request->fence.flags);
  567. set_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
  568. /*
  569. * XXX Rollback bonded-execution on __i915_request_unsubmit()?
  570. *
  571. * In the future, perhaps when we have an active time-slicing scheduler,
  572. * it will be interesting to unsubmit parallel execution and remove
  573. * busywaits from the GPU until their master is restarted. This is
  574. * quite hairy, we have to carefully rollback the fence and do a
  575. * preempt-to-idle cycle on the target engine, all the while the
  576. * master execute_cb may refire.
  577. */
  578. __notify_execute_cb_irq(request);
  579. /* We may be recursing from the signal callback of another i915 fence */
  580. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  581. i915_request_enable_breadcrumb(request);
  582. return result;
  583. }
  584. void i915_request_submit(struct i915_request *request)
  585. {
  586. struct intel_engine_cs *engine = request->engine;
  587. unsigned long flags;
  588. /* Will be called from irq-context when using foreign fences. */
  589. spin_lock_irqsave(&engine->sched_engine->lock, flags);
  590. __i915_request_submit(request);
  591. spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
  592. }
  593. void __i915_request_unsubmit(struct i915_request *request)
  594. {
  595. struct intel_engine_cs *engine = request->engine;
  596. /*
  597. * Only unwind in reverse order, required so that the per-context list
  598. * is kept in seqno/ring order.
  599. */
  600. RQ_TRACE(request, "\n");
  601. GEM_BUG_ON(!irqs_disabled());
  602. lockdep_assert_held(&engine->sched_engine->lock);
  603. /*
  604. * Before we remove this breadcrumb from the signal list, we have
  605. * to ensure that a concurrent dma_fence_enable_signaling() does not
  606. * attach itself. We first mark the request as no longer active and
  607. * make sure that is visible to other cores, and then remove the
  608. * breadcrumb if attached.
  609. */
  610. GEM_BUG_ON(!test_bit(I915_FENCE_FLAG_ACTIVE, &request->fence.flags));
  611. clear_bit_unlock(I915_FENCE_FLAG_ACTIVE, &request->fence.flags);
  612. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &request->fence.flags))
  613. i915_request_cancel_breadcrumb(request);
  614. /* We've already spun, don't charge on resubmitting. */
  615. if (request->sched.semaphores && __i915_request_has_started(request))
  616. request->sched.semaphores = 0;
  617. /*
  618. * We don't need to wake_up any waiters on request->execute, they
  619. * will get woken by any other event or us re-adding this request
  620. * to the engine timeline (__i915_request_submit()). The waiters
  621. * should be quite adapt at finding that the request now has a new
  622. * global_seqno to the one they went to sleep on.
  623. */
  624. }
  625. void i915_request_unsubmit(struct i915_request *request)
  626. {
  627. struct intel_engine_cs *engine = request->engine;
  628. unsigned long flags;
  629. /* Will be called from irq-context when using foreign fences. */
  630. spin_lock_irqsave(&engine->sched_engine->lock, flags);
  631. __i915_request_unsubmit(request);
  632. spin_unlock_irqrestore(&engine->sched_engine->lock, flags);
  633. }
  634. void i915_request_cancel(struct i915_request *rq, int error)
  635. {
  636. if (!i915_request_set_error_once(rq, error))
  637. return;
  638. set_bit(I915_FENCE_FLAG_SENTINEL, &rq->fence.flags);
  639. intel_context_cancel_request(rq->context, rq);
  640. }
  641. static int
  642. submit_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
  643. {
  644. struct i915_request *request =
  645. container_of(fence, typeof(*request), submit);
  646. switch (state) {
  647. case FENCE_COMPLETE:
  648. trace_i915_request_submit(request);
  649. if (unlikely(fence->error))
  650. i915_request_set_error_once(request, fence->error);
  651. else
  652. __rq_arm_watchdog(request);
  653. /*
  654. * We need to serialize use of the submit_request() callback
  655. * with its hotplugging performed during an emergency
  656. * i915_gem_set_wedged(). We use the RCU mechanism to mark the
  657. * critical section in order to force i915_gem_set_wedged() to
  658. * wait until the submit_request() is completed before
  659. * proceeding.
  660. */
  661. rcu_read_lock();
  662. request->engine->submit_request(request);
  663. rcu_read_unlock();
  664. break;
  665. case FENCE_FREE:
  666. i915_request_put(request);
  667. break;
  668. }
  669. return NOTIFY_DONE;
  670. }
  671. static int
  672. semaphore_notify(struct i915_sw_fence *fence, enum i915_sw_fence_notify state)
  673. {
  674. struct i915_request *rq = container_of(fence, typeof(*rq), semaphore);
  675. switch (state) {
  676. case FENCE_COMPLETE:
  677. break;
  678. case FENCE_FREE:
  679. i915_request_put(rq);
  680. break;
  681. }
  682. return NOTIFY_DONE;
  683. }
  684. static void retire_requests(struct intel_timeline *tl)
  685. {
  686. struct i915_request *rq, *rn;
  687. list_for_each_entry_safe(rq, rn, &tl->requests, link)
  688. if (!i915_request_retire(rq))
  689. break;
  690. }
  691. static noinline struct i915_request *
  692. request_alloc_slow(struct intel_timeline *tl,
  693. struct i915_request **rsvd,
  694. gfp_t gfp)
  695. {
  696. struct i915_request *rq;
  697. /* If we cannot wait, dip into our reserves */
  698. if (!gfpflags_allow_blocking(gfp)) {
  699. rq = xchg(rsvd, NULL);
  700. if (!rq) /* Use the normal failure path for one final WARN */
  701. goto out;
  702. return rq;
  703. }
  704. if (list_empty(&tl->requests))
  705. goto out;
  706. /* Move our oldest request to the slab-cache (if not in use!) */
  707. rq = list_first_entry(&tl->requests, typeof(*rq), link);
  708. i915_request_retire(rq);
  709. rq = kmem_cache_alloc(slab_requests,
  710. gfp | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
  711. if (rq)
  712. return rq;
  713. /* Ratelimit ourselves to prevent oom from malicious clients */
  714. rq = list_last_entry(&tl->requests, typeof(*rq), link);
  715. cond_synchronize_rcu(rq->rcustate);
  716. /* Retire our old requests in the hope that we free some */
  717. retire_requests(tl);
  718. out:
  719. return kmem_cache_alloc(slab_requests, gfp);
  720. }
  721. static void __i915_request_ctor(void *arg)
  722. {
  723. struct i915_request *rq = arg;
  724. spin_lock_init(&rq->lock);
  725. i915_sched_node_init(&rq->sched);
  726. i915_sw_fence_init(&rq->submit, submit_notify);
  727. i915_sw_fence_init(&rq->semaphore, semaphore_notify);
  728. clear_capture_list(rq);
  729. rq->batch_res = NULL;
  730. init_llist_head(&rq->execute_cb);
  731. }
  732. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  733. #define clear_batch_ptr(_rq) ((_rq)->batch = NULL)
  734. #else
  735. #define clear_batch_ptr(_a) do {} while (0)
  736. #endif
  737. struct i915_request *
  738. __i915_request_create(struct intel_context *ce, gfp_t gfp)
  739. {
  740. struct intel_timeline *tl = ce->timeline;
  741. struct i915_request *rq;
  742. u32 seqno;
  743. int ret;
  744. might_alloc(gfp);
  745. /* Check that the caller provided an already pinned context */
  746. __intel_context_pin(ce);
  747. /*
  748. * Beware: Dragons be flying overhead.
  749. *
  750. * We use RCU to look up requests in flight. The lookups may
  751. * race with the request being allocated from the slab freelist.
  752. * That is the request we are writing to here, may be in the process
  753. * of being read by __i915_active_request_get_rcu(). As such,
  754. * we have to be very careful when overwriting the contents. During
  755. * the RCU lookup, we change chase the request->engine pointer,
  756. * read the request->global_seqno and increment the reference count.
  757. *
  758. * The reference count is incremented atomically. If it is zero,
  759. * the lookup knows the request is unallocated and complete. Otherwise,
  760. * it is either still in use, or has been reallocated and reset
  761. * with dma_fence_init(). This increment is safe for release as we
  762. * check that the request we have a reference to and matches the active
  763. * request.
  764. *
  765. * Before we increment the refcount, we chase the request->engine
  766. * pointer. We must not call kmem_cache_zalloc() or else we set
  767. * that pointer to NULL and cause a crash during the lookup. If
  768. * we see the request is completed (based on the value of the
  769. * old engine and seqno), the lookup is complete and reports NULL.
  770. * If we decide the request is not completed (new engine or seqno),
  771. * then we grab a reference and double check that it is still the
  772. * active request - which it won't be and restart the lookup.
  773. *
  774. * Do not use kmem_cache_zalloc() here!
  775. */
  776. rq = kmem_cache_alloc(slab_requests,
  777. gfp | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
  778. if (unlikely(!rq)) {
  779. rq = request_alloc_slow(tl, &ce->engine->request_pool, gfp);
  780. if (!rq) {
  781. ret = -ENOMEM;
  782. goto err_unreserve;
  783. }
  784. }
  785. rq->context = ce;
  786. rq->engine = ce->engine;
  787. rq->ring = ce->ring;
  788. rq->execution_mask = ce->engine->mask;
  789. rq->i915 = ce->engine->i915;
  790. ret = intel_timeline_get_seqno(tl, rq, &seqno);
  791. if (ret)
  792. goto err_free;
  793. dma_fence_init(&rq->fence, &i915_fence_ops, &rq->lock,
  794. tl->fence_context, seqno);
  795. RCU_INIT_POINTER(rq->timeline, tl);
  796. rq->hwsp_seqno = tl->hwsp_seqno;
  797. GEM_BUG_ON(__i915_request_is_complete(rq));
  798. rq->rcustate = get_state_synchronize_rcu(); /* acts as smp_mb() */
  799. rq->guc_prio = GUC_PRIO_INIT;
  800. /* We bump the ref for the fence chain */
  801. i915_sw_fence_reinit(&i915_request_get(rq)->submit);
  802. i915_sw_fence_reinit(&i915_request_get(rq)->semaphore);
  803. i915_sched_node_reinit(&rq->sched);
  804. /* No zalloc, everything must be cleared after use */
  805. clear_batch_ptr(rq);
  806. __rq_init_watchdog(rq);
  807. assert_capture_list_is_null(rq);
  808. GEM_BUG_ON(!llist_empty(&rq->execute_cb));
  809. GEM_BUG_ON(rq->batch_res);
  810. /*
  811. * Reserve space in the ring buffer for all the commands required to
  812. * eventually emit this request. This is to guarantee that the
  813. * i915_request_add() call can't fail. Note that the reserve may need
  814. * to be redone if the request is not actually submitted straight
  815. * away, e.g. because a GPU scheduler has deferred it.
  816. *
  817. * Note that due to how we add reserved_space to intel_ring_begin()
  818. * we need to double our request to ensure that if we need to wrap
  819. * around inside i915_request_add() there is sufficient space at
  820. * the beginning of the ring as well.
  821. */
  822. rq->reserved_space =
  823. 2 * rq->engine->emit_fini_breadcrumb_dw * sizeof(u32);
  824. /*
  825. * Record the position of the start of the request so that
  826. * should we detect the updated seqno part-way through the
  827. * GPU processing the request, we never over-estimate the
  828. * position of the head.
  829. */
  830. rq->head = rq->ring->emit;
  831. ret = rq->engine->request_alloc(rq);
  832. if (ret)
  833. goto err_unwind;
  834. rq->infix = rq->ring->emit; /* end of header; start of user payload */
  835. intel_context_mark_active(ce);
  836. list_add_tail_rcu(&rq->link, &tl->requests);
  837. return rq;
  838. err_unwind:
  839. ce->ring->emit = rq->head;
  840. /* Make sure we didn't add ourselves to external state before freeing */
  841. GEM_BUG_ON(!list_empty(&rq->sched.signalers_list));
  842. GEM_BUG_ON(!list_empty(&rq->sched.waiters_list));
  843. err_free:
  844. kmem_cache_free(slab_requests, rq);
  845. err_unreserve:
  846. intel_context_unpin(ce);
  847. return ERR_PTR(ret);
  848. }
  849. struct i915_request *
  850. i915_request_create(struct intel_context *ce)
  851. {
  852. struct i915_request *rq;
  853. struct intel_timeline *tl;
  854. tl = intel_context_timeline_lock(ce);
  855. if (IS_ERR(tl))
  856. return ERR_CAST(tl);
  857. /* Move our oldest request to the slab-cache (if not in use!) */
  858. rq = list_first_entry(&tl->requests, typeof(*rq), link);
  859. if (!list_is_last(&rq->link, &tl->requests))
  860. i915_request_retire(rq);
  861. intel_context_enter(ce);
  862. rq = __i915_request_create(ce, GFP_KERNEL);
  863. intel_context_exit(ce); /* active reference transferred to request */
  864. if (IS_ERR(rq))
  865. goto err_unlock;
  866. /* Check that we do not interrupt ourselves with a new request */
  867. rq->cookie = lockdep_pin_lock(&tl->mutex);
  868. return rq;
  869. err_unlock:
  870. intel_context_timeline_unlock(tl);
  871. return rq;
  872. }
  873. static int
  874. i915_request_await_start(struct i915_request *rq, struct i915_request *signal)
  875. {
  876. struct dma_fence *fence;
  877. int err;
  878. if (i915_request_timeline(rq) == rcu_access_pointer(signal->timeline))
  879. return 0;
  880. if (i915_request_started(signal))
  881. return 0;
  882. /*
  883. * The caller holds a reference on @signal, but we do not serialise
  884. * against it being retired and removed from the lists.
  885. *
  886. * We do not hold a reference to the request before @signal, and
  887. * so must be very careful to ensure that it is not _recycled_ as
  888. * we follow the link backwards.
  889. */
  890. fence = NULL;
  891. rcu_read_lock();
  892. do {
  893. struct list_head *pos = READ_ONCE(signal->link.prev);
  894. struct i915_request *prev;
  895. /* Confirm signal has not been retired, the link is valid */
  896. if (unlikely(__i915_request_has_started(signal)))
  897. break;
  898. /* Is signal the earliest request on its timeline? */
  899. if (pos == &rcu_dereference(signal->timeline)->requests)
  900. break;
  901. /*
  902. * Peek at the request before us in the timeline. That
  903. * request will only be valid before it is retired, so
  904. * after acquiring a reference to it, confirm that it is
  905. * still part of the signaler's timeline.
  906. */
  907. prev = list_entry(pos, typeof(*prev), link);
  908. if (!i915_request_get_rcu(prev))
  909. break;
  910. /* After the strong barrier, confirm prev is still attached */
  911. if (unlikely(READ_ONCE(prev->link.next) != &signal->link)) {
  912. i915_request_put(prev);
  913. break;
  914. }
  915. fence = &prev->fence;
  916. } while (0);
  917. rcu_read_unlock();
  918. if (!fence)
  919. return 0;
  920. err = 0;
  921. if (!intel_timeline_sync_is_later(i915_request_timeline(rq), fence))
  922. err = i915_sw_fence_await_dma_fence(&rq->submit,
  923. fence, 0,
  924. I915_FENCE_GFP);
  925. dma_fence_put(fence);
  926. return err;
  927. }
  928. static intel_engine_mask_t
  929. already_busywaiting(struct i915_request *rq)
  930. {
  931. /*
  932. * Polling a semaphore causes bus traffic, delaying other users of
  933. * both the GPU and CPU. We want to limit the impact on others,
  934. * while taking advantage of early submission to reduce GPU
  935. * latency. Therefore we restrict ourselves to not using more
  936. * than one semaphore from each source, and not using a semaphore
  937. * if we have detected the engine is saturated (i.e. would not be
  938. * submitted early and cause bus traffic reading an already passed
  939. * semaphore).
  940. *
  941. * See the are-we-too-late? check in __i915_request_submit().
  942. */
  943. return rq->sched.semaphores | READ_ONCE(rq->engine->saturated);
  944. }
  945. static int
  946. __emit_semaphore_wait(struct i915_request *to,
  947. struct i915_request *from,
  948. u32 seqno)
  949. {
  950. const int has_token = GRAPHICS_VER(to->engine->i915) >= 12;
  951. u32 hwsp_offset;
  952. int len, err;
  953. u32 *cs;
  954. GEM_BUG_ON(GRAPHICS_VER(to->engine->i915) < 8);
  955. GEM_BUG_ON(i915_request_has_initial_breadcrumb(to));
  956. /* We need to pin the signaler's HWSP until we are finished reading. */
  957. err = intel_timeline_read_hwsp(from, to, &hwsp_offset);
  958. if (err)
  959. return err;
  960. len = 4;
  961. if (has_token)
  962. len += 2;
  963. cs = intel_ring_begin(to, len);
  964. if (IS_ERR(cs))
  965. return PTR_ERR(cs);
  966. /*
  967. * Using greater-than-or-equal here means we have to worry
  968. * about seqno wraparound. To side step that issue, we swap
  969. * the timeline HWSP upon wrapping, so that everyone listening
  970. * for the old (pre-wrap) values do not see the much smaller
  971. * (post-wrap) values than they were expecting (and so wait
  972. * forever).
  973. */
  974. *cs++ = (MI_SEMAPHORE_WAIT |
  975. MI_SEMAPHORE_GLOBAL_GTT |
  976. MI_SEMAPHORE_POLL |
  977. MI_SEMAPHORE_SAD_GTE_SDD) +
  978. has_token;
  979. *cs++ = seqno;
  980. *cs++ = hwsp_offset;
  981. *cs++ = 0;
  982. if (has_token) {
  983. *cs++ = 0;
  984. *cs++ = MI_NOOP;
  985. }
  986. intel_ring_advance(to, cs);
  987. return 0;
  988. }
  989. static bool
  990. can_use_semaphore_wait(struct i915_request *to, struct i915_request *from)
  991. {
  992. return to->engine->gt->ggtt == from->engine->gt->ggtt;
  993. }
  994. static int
  995. emit_semaphore_wait(struct i915_request *to,
  996. struct i915_request *from,
  997. gfp_t gfp)
  998. {
  999. const intel_engine_mask_t mask = READ_ONCE(from->engine)->mask;
  1000. struct i915_sw_fence *wait = &to->submit;
  1001. if (!can_use_semaphore_wait(to, from))
  1002. goto await_fence;
  1003. if (!intel_context_use_semaphores(to->context))
  1004. goto await_fence;
  1005. if (i915_request_has_initial_breadcrumb(to))
  1006. goto await_fence;
  1007. /*
  1008. * If this or its dependents are waiting on an external fence
  1009. * that may fail catastrophically, then we want to avoid using
  1010. * semaphores as they bypass the fence signaling metadata, and we
  1011. * lose the fence->error propagation.
  1012. */
  1013. if (from->sched.flags & I915_SCHED_HAS_EXTERNAL_CHAIN)
  1014. goto await_fence;
  1015. /* Just emit the first semaphore we see as request space is limited. */
  1016. if (already_busywaiting(to) & mask)
  1017. goto await_fence;
  1018. if (i915_request_await_start(to, from) < 0)
  1019. goto await_fence;
  1020. /* Only submit our spinner after the signaler is running! */
  1021. if (__await_execution(to, from, gfp))
  1022. goto await_fence;
  1023. if (__emit_semaphore_wait(to, from, from->fence.seqno))
  1024. goto await_fence;
  1025. to->sched.semaphores |= mask;
  1026. wait = &to->semaphore;
  1027. await_fence:
  1028. return i915_sw_fence_await_dma_fence(wait,
  1029. &from->fence, 0,
  1030. I915_FENCE_GFP);
  1031. }
  1032. static bool intel_timeline_sync_has_start(struct intel_timeline *tl,
  1033. struct dma_fence *fence)
  1034. {
  1035. return __intel_timeline_sync_is_later(tl,
  1036. fence->context,
  1037. fence->seqno - 1);
  1038. }
  1039. static int intel_timeline_sync_set_start(struct intel_timeline *tl,
  1040. const struct dma_fence *fence)
  1041. {
  1042. return __intel_timeline_sync_set(tl, fence->context, fence->seqno - 1);
  1043. }
  1044. static int
  1045. __i915_request_await_execution(struct i915_request *to,
  1046. struct i915_request *from)
  1047. {
  1048. int err;
  1049. GEM_BUG_ON(intel_context_is_barrier(from->context));
  1050. /* Submit both requests at the same time */
  1051. err = __await_execution(to, from, I915_FENCE_GFP);
  1052. if (err)
  1053. return err;
  1054. /* Squash repeated depenendices to the same timelines */
  1055. if (intel_timeline_sync_has_start(i915_request_timeline(to),
  1056. &from->fence))
  1057. return 0;
  1058. /*
  1059. * Wait until the start of this request.
  1060. *
  1061. * The execution cb fires when we submit the request to HW. But in
  1062. * many cases this may be long before the request itself is ready to
  1063. * run (consider that we submit 2 requests for the same context, where
  1064. * the request of interest is behind an indefinite spinner). So we hook
  1065. * up to both to reduce our queues and keep the execution lag minimised
  1066. * in the worst case, though we hope that the await_start is elided.
  1067. */
  1068. err = i915_request_await_start(to, from);
  1069. if (err < 0)
  1070. return err;
  1071. /*
  1072. * Ensure both start together [after all semaphores in signal]
  1073. *
  1074. * Now that we are queued to the HW at roughly the same time (thanks
  1075. * to the execute cb) and are ready to run at roughly the same time
  1076. * (thanks to the await start), our signaler may still be indefinitely
  1077. * delayed by waiting on a semaphore from a remote engine. If our
  1078. * signaler depends on a semaphore, so indirectly do we, and we do not
  1079. * want to start our payload until our signaler also starts theirs.
  1080. * So we wait.
  1081. *
  1082. * However, there is also a second condition for which we need to wait
  1083. * for the precise start of the signaler. Consider that the signaler
  1084. * was submitted in a chain of requests following another context
  1085. * (with just an ordinary intra-engine fence dependency between the
  1086. * two). In this case the signaler is queued to HW, but not for
  1087. * immediate execution, and so we must wait until it reaches the
  1088. * active slot.
  1089. */
  1090. if (can_use_semaphore_wait(to, from) &&
  1091. intel_engine_has_semaphores(to->engine) &&
  1092. !i915_request_has_initial_breadcrumb(to)) {
  1093. err = __emit_semaphore_wait(to, from, from->fence.seqno - 1);
  1094. if (err < 0)
  1095. return err;
  1096. }
  1097. /* Couple the dependency tree for PI on this exposed to->fence */
  1098. if (to->engine->sched_engine->schedule) {
  1099. err = i915_sched_node_add_dependency(&to->sched,
  1100. &from->sched,
  1101. I915_DEPENDENCY_WEAK);
  1102. if (err < 0)
  1103. return err;
  1104. }
  1105. return intel_timeline_sync_set_start(i915_request_timeline(to),
  1106. &from->fence);
  1107. }
  1108. static void mark_external(struct i915_request *rq)
  1109. {
  1110. /*
  1111. * The downside of using semaphores is that we lose metadata passing
  1112. * along the signaling chain. This is particularly nasty when we
  1113. * need to pass along a fatal error such as EFAULT or EDEADLK. For
  1114. * fatal errors we want to scrub the request before it is executed,
  1115. * which means that we cannot preload the request onto HW and have
  1116. * it wait upon a semaphore.
  1117. */
  1118. rq->sched.flags |= I915_SCHED_HAS_EXTERNAL_CHAIN;
  1119. }
  1120. static int
  1121. __i915_request_await_external(struct i915_request *rq, struct dma_fence *fence)
  1122. {
  1123. mark_external(rq);
  1124. return i915_sw_fence_await_dma_fence(&rq->submit, fence,
  1125. i915_fence_context_timeout(rq->i915,
  1126. fence->context),
  1127. I915_FENCE_GFP);
  1128. }
  1129. static int
  1130. i915_request_await_external(struct i915_request *rq, struct dma_fence *fence)
  1131. {
  1132. struct dma_fence *iter;
  1133. int err = 0;
  1134. if (!to_dma_fence_chain(fence))
  1135. return __i915_request_await_external(rq, fence);
  1136. dma_fence_chain_for_each(iter, fence) {
  1137. struct dma_fence_chain *chain = to_dma_fence_chain(iter);
  1138. if (!dma_fence_is_i915(chain->fence)) {
  1139. err = __i915_request_await_external(rq, iter);
  1140. break;
  1141. }
  1142. err = i915_request_await_dma_fence(rq, chain->fence);
  1143. if (err < 0)
  1144. break;
  1145. }
  1146. dma_fence_put(iter);
  1147. return err;
  1148. }
  1149. static inline bool is_parallel_rq(struct i915_request *rq)
  1150. {
  1151. return intel_context_is_parallel(rq->context);
  1152. }
  1153. static inline struct intel_context *request_to_parent(struct i915_request *rq)
  1154. {
  1155. return intel_context_to_parent(rq->context);
  1156. }
  1157. static bool is_same_parallel_context(struct i915_request *to,
  1158. struct i915_request *from)
  1159. {
  1160. if (is_parallel_rq(to))
  1161. return request_to_parent(to) == request_to_parent(from);
  1162. return false;
  1163. }
  1164. int
  1165. i915_request_await_execution(struct i915_request *rq,
  1166. struct dma_fence *fence)
  1167. {
  1168. struct dma_fence **child = &fence;
  1169. unsigned int nchild = 1;
  1170. int ret;
  1171. if (dma_fence_is_array(fence)) {
  1172. struct dma_fence_array *array = to_dma_fence_array(fence);
  1173. /* XXX Error for signal-on-any fence arrays */
  1174. child = array->fences;
  1175. nchild = array->num_fences;
  1176. GEM_BUG_ON(!nchild);
  1177. }
  1178. do {
  1179. fence = *child++;
  1180. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  1181. continue;
  1182. if (fence->context == rq->fence.context)
  1183. continue;
  1184. /*
  1185. * We don't squash repeated fence dependencies here as we
  1186. * want to run our callback in all cases.
  1187. */
  1188. if (dma_fence_is_i915(fence)) {
  1189. if (is_same_parallel_context(rq, to_request(fence)))
  1190. continue;
  1191. ret = __i915_request_await_execution(rq,
  1192. to_request(fence));
  1193. } else {
  1194. ret = i915_request_await_external(rq, fence);
  1195. }
  1196. if (ret < 0)
  1197. return ret;
  1198. } while (--nchild);
  1199. return 0;
  1200. }
  1201. static int
  1202. await_request_submit(struct i915_request *to, struct i915_request *from)
  1203. {
  1204. /*
  1205. * If we are waiting on a virtual engine, then it may be
  1206. * constrained to execute on a single engine *prior* to submission.
  1207. * When it is submitted, it will be first submitted to the virtual
  1208. * engine and then passed to the physical engine. We cannot allow
  1209. * the waiter to be submitted immediately to the physical engine
  1210. * as it may then bypass the virtual request.
  1211. */
  1212. if (to->engine == READ_ONCE(from->engine))
  1213. return i915_sw_fence_await_sw_fence_gfp(&to->submit,
  1214. &from->submit,
  1215. I915_FENCE_GFP);
  1216. else
  1217. return __i915_request_await_execution(to, from);
  1218. }
  1219. static int
  1220. i915_request_await_request(struct i915_request *to, struct i915_request *from)
  1221. {
  1222. int ret;
  1223. GEM_BUG_ON(to == from);
  1224. GEM_BUG_ON(to->timeline == from->timeline);
  1225. if (i915_request_completed(from)) {
  1226. i915_sw_fence_set_error_once(&to->submit, from->fence.error);
  1227. return 0;
  1228. }
  1229. if (to->engine->sched_engine->schedule) {
  1230. ret = i915_sched_node_add_dependency(&to->sched,
  1231. &from->sched,
  1232. I915_DEPENDENCY_EXTERNAL);
  1233. if (ret < 0)
  1234. return ret;
  1235. }
  1236. if (!intel_engine_uses_guc(to->engine) &&
  1237. is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
  1238. ret = await_request_submit(to, from);
  1239. else
  1240. ret = emit_semaphore_wait(to, from, I915_FENCE_GFP);
  1241. if (ret < 0)
  1242. return ret;
  1243. return 0;
  1244. }
  1245. int
  1246. i915_request_await_dma_fence(struct i915_request *rq, struct dma_fence *fence)
  1247. {
  1248. struct dma_fence **child = &fence;
  1249. unsigned int nchild = 1;
  1250. int ret;
  1251. /*
  1252. * Note that if the fence-array was created in signal-on-any mode,
  1253. * we should *not* decompose it into its individual fences. However,
  1254. * we don't currently store which mode the fence-array is operating
  1255. * in. Fortunately, the only user of signal-on-any is private to
  1256. * amdgpu and we should not see any incoming fence-array from
  1257. * sync-file being in signal-on-any mode.
  1258. */
  1259. if (dma_fence_is_array(fence)) {
  1260. struct dma_fence_array *array = to_dma_fence_array(fence);
  1261. child = array->fences;
  1262. nchild = array->num_fences;
  1263. GEM_BUG_ON(!nchild);
  1264. }
  1265. do {
  1266. fence = *child++;
  1267. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  1268. continue;
  1269. /*
  1270. * Requests on the same timeline are explicitly ordered, along
  1271. * with their dependencies, by i915_request_add() which ensures
  1272. * that requests are submitted in-order through each ring.
  1273. */
  1274. if (fence->context == rq->fence.context)
  1275. continue;
  1276. /* Squash repeated waits to the same timelines */
  1277. if (fence->context &&
  1278. intel_timeline_sync_is_later(i915_request_timeline(rq),
  1279. fence))
  1280. continue;
  1281. if (dma_fence_is_i915(fence)) {
  1282. if (is_same_parallel_context(rq, to_request(fence)))
  1283. continue;
  1284. ret = i915_request_await_request(rq, to_request(fence));
  1285. } else {
  1286. ret = i915_request_await_external(rq, fence);
  1287. }
  1288. if (ret < 0)
  1289. return ret;
  1290. /* Record the latest fence used against each timeline */
  1291. if (fence->context)
  1292. intel_timeline_sync_set(i915_request_timeline(rq),
  1293. fence);
  1294. } while (--nchild);
  1295. return 0;
  1296. }
  1297. /**
  1298. * i915_request_await_deps - set this request to (async) wait upon a struct
  1299. * i915_deps dma_fence collection
  1300. * @rq: request we are wishing to use
  1301. * @deps: The struct i915_deps containing the dependencies.
  1302. *
  1303. * Returns 0 if successful, negative error code on error.
  1304. */
  1305. int i915_request_await_deps(struct i915_request *rq, const struct i915_deps *deps)
  1306. {
  1307. int i, err;
  1308. for (i = 0; i < deps->num_deps; ++i) {
  1309. err = i915_request_await_dma_fence(rq, deps->fences[i]);
  1310. if (err)
  1311. return err;
  1312. }
  1313. return 0;
  1314. }
  1315. /**
  1316. * i915_request_await_object - set this request to (async) wait upon a bo
  1317. * @to: request we are wishing to use
  1318. * @obj: object which may be in use on another ring.
  1319. * @write: whether the wait is on behalf of a writer
  1320. *
  1321. * This code is meant to abstract object synchronization with the GPU.
  1322. * Conceptually we serialise writes between engines inside the GPU.
  1323. * We only allow one engine to write into a buffer at any time, but
  1324. * multiple readers. To ensure each has a coherent view of memory, we must:
  1325. *
  1326. * - If there is an outstanding write request to the object, the new
  1327. * request must wait for it to complete (either CPU or in hw, requests
  1328. * on the same ring will be naturally ordered).
  1329. *
  1330. * - If we are a write request (pending_write_domain is set), the new
  1331. * request must wait for outstanding read requests to complete.
  1332. *
  1333. * Returns 0 if successful, else propagates up the lower layer error.
  1334. */
  1335. int
  1336. i915_request_await_object(struct i915_request *to,
  1337. struct drm_i915_gem_object *obj,
  1338. bool write)
  1339. {
  1340. struct dma_resv_iter cursor;
  1341. struct dma_fence *fence;
  1342. int ret = 0;
  1343. dma_resv_for_each_fence(&cursor, obj->base.resv,
  1344. dma_resv_usage_rw(write), fence) {
  1345. ret = i915_request_await_dma_fence(to, fence);
  1346. if (ret)
  1347. break;
  1348. }
  1349. return ret;
  1350. }
  1351. static void i915_request_await_huc(struct i915_request *rq)
  1352. {
  1353. struct intel_huc *huc = &rq->context->engine->gt->uc.huc;
  1354. /* don't stall kernel submissions! */
  1355. if (!rcu_access_pointer(rq->context->gem_context))
  1356. return;
  1357. if (intel_huc_wait_required(huc))
  1358. i915_sw_fence_await_sw_fence(&rq->submit,
  1359. &huc->delayed_load.fence,
  1360. &rq->hucq);
  1361. }
  1362. static struct i915_request *
  1363. __i915_request_ensure_parallel_ordering(struct i915_request *rq,
  1364. struct intel_timeline *timeline)
  1365. {
  1366. struct i915_request *prev;
  1367. GEM_BUG_ON(!is_parallel_rq(rq));
  1368. prev = request_to_parent(rq)->parallel.last_rq;
  1369. if (prev) {
  1370. if (!__i915_request_is_complete(prev)) {
  1371. i915_sw_fence_await_sw_fence(&rq->submit,
  1372. &prev->submit,
  1373. &rq->submitq);
  1374. if (rq->engine->sched_engine->schedule)
  1375. __i915_sched_node_add_dependency(&rq->sched,
  1376. &prev->sched,
  1377. &rq->dep,
  1378. 0);
  1379. }
  1380. i915_request_put(prev);
  1381. }
  1382. request_to_parent(rq)->parallel.last_rq = i915_request_get(rq);
  1383. /*
  1384. * Users have to put a reference potentially got by
  1385. * __i915_active_fence_set() to the returned request
  1386. * when no longer needed
  1387. */
  1388. return to_request(__i915_active_fence_set(&timeline->last_request,
  1389. &rq->fence));
  1390. }
  1391. static struct i915_request *
  1392. __i915_request_ensure_ordering(struct i915_request *rq,
  1393. struct intel_timeline *timeline)
  1394. {
  1395. struct i915_request *prev;
  1396. GEM_BUG_ON(is_parallel_rq(rq));
  1397. prev = to_request(__i915_active_fence_set(&timeline->last_request,
  1398. &rq->fence));
  1399. if (prev && !__i915_request_is_complete(prev)) {
  1400. bool uses_guc = intel_engine_uses_guc(rq->engine);
  1401. bool pow2 = is_power_of_2(READ_ONCE(prev->engine)->mask |
  1402. rq->engine->mask);
  1403. bool same_context = prev->context == rq->context;
  1404. /*
  1405. * The requests are supposed to be kept in order. However,
  1406. * we need to be wary in case the timeline->last_request
  1407. * is used as a barrier for external modification to this
  1408. * context.
  1409. */
  1410. GEM_BUG_ON(same_context &&
  1411. i915_seqno_passed(prev->fence.seqno,
  1412. rq->fence.seqno));
  1413. if ((same_context && uses_guc) || (!uses_guc && pow2))
  1414. i915_sw_fence_await_sw_fence(&rq->submit,
  1415. &prev->submit,
  1416. &rq->submitq);
  1417. else
  1418. __i915_sw_fence_await_dma_fence(&rq->submit,
  1419. &prev->fence,
  1420. &rq->dmaq);
  1421. if (rq->engine->sched_engine->schedule)
  1422. __i915_sched_node_add_dependency(&rq->sched,
  1423. &prev->sched,
  1424. &rq->dep,
  1425. 0);
  1426. }
  1427. /*
  1428. * Users have to put the reference to prev potentially got
  1429. * by __i915_active_fence_set() when no longer needed
  1430. */
  1431. return prev;
  1432. }
  1433. static struct i915_request *
  1434. __i915_request_add_to_timeline(struct i915_request *rq)
  1435. {
  1436. struct intel_timeline *timeline = i915_request_timeline(rq);
  1437. struct i915_request *prev;
  1438. /*
  1439. * Media workloads may require HuC, so stall them until HuC loading is
  1440. * complete. Note that HuC not being loaded when a user submission
  1441. * arrives can only happen when HuC is loaded via GSC and in that case
  1442. * we still expect the window between us starting to accept submissions
  1443. * and HuC loading completion to be small (a few hundred ms).
  1444. */
  1445. if (rq->engine->class == VIDEO_DECODE_CLASS)
  1446. i915_request_await_huc(rq);
  1447. /*
  1448. * Dependency tracking and request ordering along the timeline
  1449. * is special cased so that we can eliminate redundant ordering
  1450. * operations while building the request (we know that the timeline
  1451. * itself is ordered, and here we guarantee it).
  1452. *
  1453. * As we know we will need to emit tracking along the timeline,
  1454. * we embed the hooks into our request struct -- at the cost of
  1455. * having to have specialised no-allocation interfaces (which will
  1456. * be beneficial elsewhere).
  1457. *
  1458. * A second benefit to open-coding i915_request_await_request is
  1459. * that we can apply a slight variant of the rules specialised
  1460. * for timelines that jump between engines (such as virtual engines).
  1461. * If we consider the case of virtual engine, we must emit a dma-fence
  1462. * to prevent scheduling of the second request until the first is
  1463. * complete (to maximise our greedy late load balancing) and this
  1464. * precludes optimising to use semaphores serialisation of a single
  1465. * timeline across engines.
  1466. *
  1467. * We do not order parallel submission requests on the timeline as each
  1468. * parallel submission context has its own timeline and the ordering
  1469. * rules for parallel requests are that they must be submitted in the
  1470. * order received from the execbuf IOCTL. So rather than using the
  1471. * timeline we store a pointer to last request submitted in the
  1472. * relationship in the gem context and insert a submission fence
  1473. * between that request and request passed into this function or
  1474. * alternatively we use completion fence if gem context has a single
  1475. * timeline and this is the first submission of an execbuf IOCTL.
  1476. */
  1477. if (likely(!is_parallel_rq(rq)))
  1478. prev = __i915_request_ensure_ordering(rq, timeline);
  1479. else
  1480. prev = __i915_request_ensure_parallel_ordering(rq, timeline);
  1481. if (prev)
  1482. i915_request_put(prev);
  1483. /*
  1484. * Make sure that no request gazumped us - if it was allocated after
  1485. * our i915_request_alloc() and called __i915_request_add() before
  1486. * us, the timeline will hold its seqno which is later than ours.
  1487. */
  1488. GEM_BUG_ON(timeline->seqno != rq->fence.seqno);
  1489. return prev;
  1490. }
  1491. /*
  1492. * NB: This function is not allowed to fail. Doing so would mean the the
  1493. * request is not being tracked for completion but the work itself is
  1494. * going to happen on the hardware. This would be a Bad Thing(tm).
  1495. */
  1496. struct i915_request *__i915_request_commit(struct i915_request *rq)
  1497. {
  1498. struct intel_engine_cs *engine = rq->engine;
  1499. struct intel_ring *ring = rq->ring;
  1500. u32 *cs;
  1501. RQ_TRACE(rq, "\n");
  1502. /*
  1503. * To ensure that this call will not fail, space for its emissions
  1504. * should already have been reserved in the ring buffer. Let the ring
  1505. * know that it is time to use that space up.
  1506. */
  1507. GEM_BUG_ON(rq->reserved_space > ring->space);
  1508. rq->reserved_space = 0;
  1509. rq->emitted_jiffies = jiffies;
  1510. /*
  1511. * Record the position of the start of the breadcrumb so that
  1512. * should we detect the updated seqno part-way through the
  1513. * GPU processing the request, we never over-estimate the
  1514. * position of the ring's HEAD.
  1515. */
  1516. cs = intel_ring_begin(rq, engine->emit_fini_breadcrumb_dw);
  1517. GEM_BUG_ON(IS_ERR(cs));
  1518. rq->postfix = intel_ring_offset(rq, cs);
  1519. return __i915_request_add_to_timeline(rq);
  1520. }
  1521. void __i915_request_queue_bh(struct i915_request *rq)
  1522. {
  1523. i915_sw_fence_commit(&rq->semaphore);
  1524. i915_sw_fence_commit(&rq->submit);
  1525. }
  1526. void __i915_request_queue(struct i915_request *rq,
  1527. const struct i915_sched_attr *attr)
  1528. {
  1529. /*
  1530. * Let the backend know a new request has arrived that may need
  1531. * to adjust the existing execution schedule due to a high priority
  1532. * request - i.e. we may want to preempt the current request in order
  1533. * to run a high priority dependency chain *before* we can execute this
  1534. * request.
  1535. *
  1536. * This is called before the request is ready to run so that we can
  1537. * decide whether to preempt the entire chain so that it is ready to
  1538. * run at the earliest possible convenience.
  1539. */
  1540. if (attr && rq->engine->sched_engine->schedule)
  1541. rq->engine->sched_engine->schedule(rq, attr);
  1542. local_bh_disable();
  1543. __i915_request_queue_bh(rq);
  1544. local_bh_enable(); /* kick tasklets */
  1545. }
  1546. void i915_request_add(struct i915_request *rq)
  1547. {
  1548. struct intel_timeline * const tl = i915_request_timeline(rq);
  1549. struct i915_sched_attr attr = {};
  1550. struct i915_gem_context *ctx;
  1551. lockdep_assert_held(&tl->mutex);
  1552. lockdep_unpin_lock(&tl->mutex, rq->cookie);
  1553. trace_i915_request_add(rq);
  1554. __i915_request_commit(rq);
  1555. /* XXX placeholder for selftests */
  1556. rcu_read_lock();
  1557. ctx = rcu_dereference(rq->context->gem_context);
  1558. if (ctx)
  1559. attr = ctx->sched;
  1560. rcu_read_unlock();
  1561. __i915_request_queue(rq, &attr);
  1562. mutex_unlock(&tl->mutex);
  1563. }
  1564. static unsigned long local_clock_ns(unsigned int *cpu)
  1565. {
  1566. unsigned long t;
  1567. /*
  1568. * Cheaply and approximately convert from nanoseconds to microseconds.
  1569. * The result and subsequent calculations are also defined in the same
  1570. * approximate microseconds units. The principal source of timing
  1571. * error here is from the simple truncation.
  1572. *
  1573. * Note that local_clock() is only defined wrt to the current CPU;
  1574. * the comparisons are no longer valid if we switch CPUs. Instead of
  1575. * blocking preemption for the entire busywait, we can detect the CPU
  1576. * switch and use that as indicator of system load and a reason to
  1577. * stop busywaiting, see busywait_stop().
  1578. */
  1579. *cpu = get_cpu();
  1580. t = local_clock();
  1581. put_cpu();
  1582. return t;
  1583. }
  1584. static bool busywait_stop(unsigned long timeout, unsigned int cpu)
  1585. {
  1586. unsigned int this_cpu;
  1587. if (time_after(local_clock_ns(&this_cpu), timeout))
  1588. return true;
  1589. return this_cpu != cpu;
  1590. }
  1591. static bool __i915_spin_request(struct i915_request * const rq, int state)
  1592. {
  1593. unsigned long timeout_ns;
  1594. unsigned int cpu;
  1595. /*
  1596. * Only wait for the request if we know it is likely to complete.
  1597. *
  1598. * We don't track the timestamps around requests, nor the average
  1599. * request length, so we do not have a good indicator that this
  1600. * request will complete within the timeout. What we do know is the
  1601. * order in which requests are executed by the context and so we can
  1602. * tell if the request has been started. If the request is not even
  1603. * running yet, it is a fair assumption that it will not complete
  1604. * within our relatively short timeout.
  1605. */
  1606. if (!i915_request_is_running(rq))
  1607. return false;
  1608. /*
  1609. * When waiting for high frequency requests, e.g. during synchronous
  1610. * rendering split between the CPU and GPU, the finite amount of time
  1611. * required to set up the irq and wait upon it limits the response
  1612. * rate. By busywaiting on the request completion for a short while we
  1613. * can service the high frequency waits as quick as possible. However,
  1614. * if it is a slow request, we want to sleep as quickly as possible.
  1615. * The tradeoff between waiting and sleeping is roughly the time it
  1616. * takes to sleep on a request, on the order of a microsecond.
  1617. */
  1618. timeout_ns = READ_ONCE(rq->engine->props.max_busywait_duration_ns);
  1619. timeout_ns += local_clock_ns(&cpu);
  1620. do {
  1621. if (dma_fence_is_signaled(&rq->fence))
  1622. return true;
  1623. if (signal_pending_state(state, current))
  1624. break;
  1625. if (busywait_stop(timeout_ns, cpu))
  1626. break;
  1627. cpu_relax();
  1628. } while (!need_resched());
  1629. return false;
  1630. }
  1631. struct request_wait {
  1632. struct dma_fence_cb cb;
  1633. struct task_struct *tsk;
  1634. };
  1635. static void request_wait_wake(struct dma_fence *fence, struct dma_fence_cb *cb)
  1636. {
  1637. struct request_wait *wait = container_of(cb, typeof(*wait), cb);
  1638. wake_up_process(fetch_and_zero(&wait->tsk));
  1639. }
  1640. /**
  1641. * i915_request_wait_timeout - wait until execution of request has finished
  1642. * @rq: the request to wait upon
  1643. * @flags: how to wait
  1644. * @timeout: how long to wait in jiffies
  1645. *
  1646. * i915_request_wait_timeout() waits for the request to be completed, for a
  1647. * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
  1648. * unbounded wait).
  1649. *
  1650. * Returns the remaining time (in jiffies) if the request completed, which may
  1651. * be zero if the request is unfinished after the timeout expires.
  1652. * If the timeout is 0, it will return 1 if the fence is signaled.
  1653. *
  1654. * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
  1655. * pending before the request completes.
  1656. *
  1657. * NOTE: This function has the same wait semantics as dma-fence.
  1658. */
  1659. long i915_request_wait_timeout(struct i915_request *rq,
  1660. unsigned int flags,
  1661. long timeout)
  1662. {
  1663. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  1664. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  1665. struct request_wait wait;
  1666. might_sleep();
  1667. GEM_BUG_ON(timeout < 0);
  1668. if (dma_fence_is_signaled(&rq->fence))
  1669. return timeout ?: 1;
  1670. if (!timeout)
  1671. return -ETIME;
  1672. trace_i915_request_wait_begin(rq, flags);
  1673. /*
  1674. * We must never wait on the GPU while holding a lock as we
  1675. * may need to perform a GPU reset. So while we don't need to
  1676. * serialise wait/reset with an explicit lock, we do want
  1677. * lockdep to detect potential dependency cycles.
  1678. */
  1679. mutex_acquire(&rq->engine->gt->reset.mutex.dep_map, 0, 0, _THIS_IP_);
  1680. /*
  1681. * Optimistic spin before touching IRQs.
  1682. *
  1683. * We may use a rather large value here to offset the penalty of
  1684. * switching away from the active task. Frequently, the client will
  1685. * wait upon an old swapbuffer to throttle itself to remain within a
  1686. * frame of the gpu. If the client is running in lockstep with the gpu,
  1687. * then it should not be waiting long at all, and a sleep now will incur
  1688. * extra scheduler latency in producing the next frame. To try to
  1689. * avoid adding the cost of enabling/disabling the interrupt to the
  1690. * short wait, we first spin to see if the request would have completed
  1691. * in the time taken to setup the interrupt.
  1692. *
  1693. * We need upto 5us to enable the irq, and upto 20us to hide the
  1694. * scheduler latency of a context switch, ignoring the secondary
  1695. * impacts from a context switch such as cache eviction.
  1696. *
  1697. * The scheme used for low-latency IO is called "hybrid interrupt
  1698. * polling". The suggestion there is to sleep until just before you
  1699. * expect to be woken by the device interrupt and then poll for its
  1700. * completion. That requires having a good predictor for the request
  1701. * duration, which we currently lack.
  1702. */
  1703. if (CONFIG_DRM_I915_MAX_REQUEST_BUSYWAIT &&
  1704. __i915_spin_request(rq, state))
  1705. goto out;
  1706. /*
  1707. * This client is about to stall waiting for the GPU. In many cases
  1708. * this is undesirable and limits the throughput of the system, as
  1709. * many clients cannot continue processing user input/output whilst
  1710. * blocked. RPS autotuning may take tens of milliseconds to respond
  1711. * to the GPU load and thus incurs additional latency for the client.
  1712. * We can circumvent that by promoting the GPU frequency to maximum
  1713. * before we sleep. This makes the GPU throttle up much more quickly
  1714. * (good for benchmarks and user experience, e.g. window animations),
  1715. * but at a cost of spending more power processing the workload
  1716. * (bad for battery).
  1717. */
  1718. if (flags & I915_WAIT_PRIORITY && !i915_request_started(rq))
  1719. intel_rps_boost(rq);
  1720. wait.tsk = current;
  1721. if (dma_fence_add_callback(&rq->fence, &wait.cb, request_wait_wake))
  1722. goto out;
  1723. /*
  1724. * Flush the submission tasklet, but only if it may help this request.
  1725. *
  1726. * We sometimes experience some latency between the HW interrupts and
  1727. * tasklet execution (mostly due to ksoftirqd latency, but it can also
  1728. * be due to lazy CS events), so lets run the tasklet manually if there
  1729. * is a chance it may submit this request. If the request is not ready
  1730. * to run, as it is waiting for other fences to be signaled, flushing
  1731. * the tasklet is busy work without any advantage for this client.
  1732. *
  1733. * If the HW is being lazy, this is the last chance before we go to
  1734. * sleep to catch any pending events. We will check periodically in
  1735. * the heartbeat to flush the submission tasklets as a last resort
  1736. * for unhappy HW.
  1737. */
  1738. if (i915_request_is_ready(rq))
  1739. __intel_engine_flush_submission(rq->engine, false);
  1740. for (;;) {
  1741. set_current_state(state);
  1742. if (dma_fence_is_signaled(&rq->fence))
  1743. break;
  1744. if (signal_pending_state(state, current)) {
  1745. timeout = -ERESTARTSYS;
  1746. break;
  1747. }
  1748. if (!timeout) {
  1749. timeout = -ETIME;
  1750. break;
  1751. }
  1752. timeout = io_schedule_timeout(timeout);
  1753. }
  1754. __set_current_state(TASK_RUNNING);
  1755. if (READ_ONCE(wait.tsk))
  1756. dma_fence_remove_callback(&rq->fence, &wait.cb);
  1757. GEM_BUG_ON(!list_empty(&wait.cb.node));
  1758. out:
  1759. mutex_release(&rq->engine->gt->reset.mutex.dep_map, _THIS_IP_);
  1760. trace_i915_request_wait_end(rq);
  1761. return timeout;
  1762. }
  1763. /**
  1764. * i915_request_wait - wait until execution of request has finished
  1765. * @rq: the request to wait upon
  1766. * @flags: how to wait
  1767. * @timeout: how long to wait in jiffies
  1768. *
  1769. * i915_request_wait() waits for the request to be completed, for a
  1770. * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
  1771. * unbounded wait).
  1772. *
  1773. * Returns the remaining time (in jiffies) if the request completed, which may
  1774. * be zero or -ETIME if the request is unfinished after the timeout expires.
  1775. * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
  1776. * pending before the request completes.
  1777. *
  1778. * NOTE: This function behaves differently from dma-fence wait semantics for
  1779. * timeout = 0. It returns 0 on success, and -ETIME if not signaled.
  1780. */
  1781. long i915_request_wait(struct i915_request *rq,
  1782. unsigned int flags,
  1783. long timeout)
  1784. {
  1785. long ret = i915_request_wait_timeout(rq, flags, timeout);
  1786. if (!ret)
  1787. return -ETIME;
  1788. if (ret > 0 && !timeout)
  1789. return 0;
  1790. return ret;
  1791. }
  1792. static int print_sched_attr(const struct i915_sched_attr *attr,
  1793. char *buf, int x, int len)
  1794. {
  1795. if (attr->priority == I915_PRIORITY_INVALID)
  1796. return x;
  1797. x += snprintf(buf + x, len - x,
  1798. " prio=%d", attr->priority);
  1799. return x;
  1800. }
  1801. static char queue_status(const struct i915_request *rq)
  1802. {
  1803. if (i915_request_is_active(rq))
  1804. return 'E';
  1805. if (i915_request_is_ready(rq))
  1806. return intel_engine_is_virtual(rq->engine) ? 'V' : 'R';
  1807. return 'U';
  1808. }
  1809. static const char *run_status(const struct i915_request *rq)
  1810. {
  1811. if (__i915_request_is_complete(rq))
  1812. return "!";
  1813. if (__i915_request_has_started(rq))
  1814. return "*";
  1815. if (!i915_sw_fence_signaled(&rq->semaphore))
  1816. return "&";
  1817. return "";
  1818. }
  1819. static const char *fence_status(const struct i915_request *rq)
  1820. {
  1821. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
  1822. return "+";
  1823. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &rq->fence.flags))
  1824. return "-";
  1825. return "";
  1826. }
  1827. void i915_request_show(struct drm_printer *m,
  1828. const struct i915_request *rq,
  1829. const char *prefix,
  1830. int indent)
  1831. {
  1832. const char *name = rq->fence.ops->get_timeline_name((struct dma_fence *)&rq->fence);
  1833. char buf[80] = "";
  1834. int x = 0;
  1835. /*
  1836. * The prefix is used to show the queue status, for which we use
  1837. * the following flags:
  1838. *
  1839. * U [Unready]
  1840. * - initial status upon being submitted by the user
  1841. *
  1842. * - the request is not ready for execution as it is waiting
  1843. * for external fences
  1844. *
  1845. * R [Ready]
  1846. * - all fences the request was waiting on have been signaled,
  1847. * and the request is now ready for execution and will be
  1848. * in a backend queue
  1849. *
  1850. * - a ready request may still need to wait on semaphores
  1851. * [internal fences]
  1852. *
  1853. * V [Ready/virtual]
  1854. * - same as ready, but queued over multiple backends
  1855. *
  1856. * E [Executing]
  1857. * - the request has been transferred from the backend queue and
  1858. * submitted for execution on HW
  1859. *
  1860. * - a completed request may still be regarded as executing, its
  1861. * status may not be updated until it is retired and removed
  1862. * from the lists
  1863. */
  1864. x = print_sched_attr(&rq->sched.attr, buf, x, sizeof(buf));
  1865. drm_printf(m, "%s%.*s%c %llx:%lld%s%s %s @ %dms: %s\n",
  1866. prefix, indent, " ",
  1867. queue_status(rq),
  1868. rq->fence.context, rq->fence.seqno,
  1869. run_status(rq),
  1870. fence_status(rq),
  1871. buf,
  1872. jiffies_to_msecs(jiffies - rq->emitted_jiffies),
  1873. name);
  1874. }
  1875. static bool engine_match_ring(struct intel_engine_cs *engine, struct i915_request *rq)
  1876. {
  1877. u32 ring = ENGINE_READ(engine, RING_START);
  1878. return ring == i915_ggtt_offset(rq->ring->vma);
  1879. }
  1880. static bool match_ring(struct i915_request *rq)
  1881. {
  1882. struct intel_engine_cs *engine;
  1883. bool found;
  1884. int i;
  1885. if (!intel_engine_is_virtual(rq->engine))
  1886. return engine_match_ring(rq->engine, rq);
  1887. found = false;
  1888. i = 0;
  1889. while ((engine = intel_engine_get_sibling(rq->engine, i++))) {
  1890. found = engine_match_ring(engine, rq);
  1891. if (found)
  1892. break;
  1893. }
  1894. return found;
  1895. }
  1896. enum i915_request_state i915_test_request_state(struct i915_request *rq)
  1897. {
  1898. if (i915_request_completed(rq))
  1899. return I915_REQUEST_COMPLETE;
  1900. if (!i915_request_started(rq))
  1901. return I915_REQUEST_PENDING;
  1902. if (match_ring(rq))
  1903. return I915_REQUEST_ACTIVE;
  1904. return I915_REQUEST_QUEUED;
  1905. }
  1906. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1907. #include "selftests/mock_request.c"
  1908. #include "selftests/i915_request.c"
  1909. #endif
  1910. void i915_request_module_exit(void)
  1911. {
  1912. kmem_cache_destroy(slab_execute_cbs);
  1913. kmem_cache_destroy(slab_requests);
  1914. }
  1915. int __init i915_request_module_init(void)
  1916. {
  1917. slab_requests =
  1918. kmem_cache_create("i915_request",
  1919. sizeof(struct i915_request),
  1920. __alignof__(struct i915_request),
  1921. SLAB_HWCACHE_ALIGN |
  1922. SLAB_RECLAIM_ACCOUNT |
  1923. SLAB_TYPESAFE_BY_RCU,
  1924. __i915_request_ctor);
  1925. if (!slab_requests)
  1926. return -ENOMEM;
  1927. slab_execute_cbs = KMEM_CACHE(execute_cb,
  1928. SLAB_HWCACHE_ALIGN |
  1929. SLAB_RECLAIM_ACCOUNT |
  1930. SLAB_TYPESAFE_BY_RCU);
  1931. if (!slab_execute_cbs)
  1932. goto err_requests;
  1933. return 0;
  1934. err_requests:
  1935. kmem_cache_destroy(slab_requests);
  1936. return -ENOMEM;
  1937. }