i915_request.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315
  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 <drm/drm_print.h>
  33. #include "gem/i915_gem_context.h"
  34. #include "gt/intel_breadcrumbs.h"
  35. #include "gt/intel_context.h"
  36. #include "gt/intel_engine.h"
  37. #include "gt/intel_engine_heartbeat.h"
  38. #include "gt/intel_engine_regs.h"
  39. #include "gt/intel_gpu_commands.h"
  40. #include "gt/intel_reset.h"
  41. #include "gt/intel_ring.h"
  42. #include "gt/intel_rps.h"
  43. #include "i915_active.h"
  44. #include "i915_config.h"
  45. #include "i915_deps.h"
  46. #include "i915_driver.h"
  47. #include "i915_drv.h"
  48. #include "i915_trace.h"
  49. struct execute_cb {
  50. struct irq_work work;
  51. struct i915_sw_fence *fence;
  52. };
  53. static struct kmem_cache *slab_requests;
  54. static struct kmem_cache *slab_execute_cbs;
  55. static const char *i915_fence_get_driver_name(struct dma_fence *fence)
  56. {
  57. return dev_name(to_request(fence)->i915->drm.dev);
  58. }
  59. static const char *i915_fence_get_timeline_name(struct dma_fence *fence)
  60. {
  61. const struct i915_gem_context *ctx;
  62. /*
  63. * The timeline struct (as part of the ppgtt underneath a context)
  64. * may be freed when the request is no longer in use by the GPU.
  65. * We could extend the life of a context to beyond that of all
  66. * fences, possibly keeping the hw resource around indefinitely,
  67. * or we just give them a false name. Since
  68. * dma_fence_ops.get_timeline_name is a debug feature, the occasional
  69. * lie seems justifiable.
  70. */
  71. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  72. return "signaled";
  73. ctx = i915_request_gem_context(to_request(fence));
  74. if (!ctx)
  75. return "[" DRIVER_NAME "]";
  76. return ctx->name;
  77. }
  78. static bool i915_fence_signaled(struct dma_fence *fence)
  79. {
  80. return i915_request_completed(to_request(fence));
  81. }
  82. static bool i915_fence_enable_signaling(struct dma_fence *fence)
  83. {
  84. return i915_request_enable_breadcrumb(to_request(fence));
  85. }
  86. static signed long i915_fence_wait(struct dma_fence *fence,
  87. bool interruptible,
  88. signed long timeout)
  89. {
  90. return i915_request_wait_timeout(to_request(fence),
  91. interruptible | I915_WAIT_PRIORITY,
  92. timeout);
  93. }
  94. struct kmem_cache *i915_request_slab_cache(void)
  95. {
  96. return slab_requests;
  97. }
  98. static void i915_fence_release(struct dma_fence *fence)
  99. {
  100. struct i915_request *rq = to_request(fence);
  101. GEM_BUG_ON(rq->guc_prio != GUC_PRIO_INIT &&
  102. rq->guc_prio != GUC_PRIO_FINI);
  103. i915_request_free_capture_list(fetch_and_zero(&rq->capture_list));
  104. if (rq->batch_res) {
  105. i915_vma_resource_put(rq->batch_res);
  106. rq->batch_res = NULL;
  107. }
  108. /*
  109. * The request is put onto a RCU freelist (i.e. the address
  110. * is immediately reused), mark the fences as being freed now.
  111. * Otherwise the debugobjects for the fences are only marked as
  112. * freed when the slab cache itself is freed, and so we would get
  113. * caught trying to reuse dead objects.
  114. */
  115. i915_sw_fence_fini(&rq->submit);
  116. i915_sw_fence_fini(&rq->semaphore);
  117. /*
  118. * Keep one request on each engine for reserved use under mempressure.
  119. *
  120. * We do not hold a reference to the engine here and so have to be
  121. * very careful in what rq->engine we poke. The virtual engine is
  122. * referenced via the rq->context and we released that ref during
  123. * i915_request_retire(), ergo we must not dereference a virtual
  124. * engine here. Not that we would want to, as the only consumer of
  125. * the reserved engine->request_pool is the power management parking,
  126. * which must-not-fail, and that is only run on the physical engines.
  127. *
  128. * Since the request must have been executed to be have completed,
  129. * we know that it will have been processed by the HW and will
  130. * not be unsubmitted again, so rq->engine and rq->execution_mask
  131. * at this point is stable. rq->execution_mask will be a single
  132. * bit if the last and _only_ engine it could execution on was a
  133. * physical engine, if it's multiple bits then it started on and
  134. * could still be on a virtual engine. Thus if the mask is not a
  135. * power-of-two we assume that rq->engine may still be a virtual
  136. * engine and so a dangling invalid pointer that we cannot dereference
  137. *
  138. * For example, consider the flow of a bonded request through a virtual
  139. * engine. The request is created with a wide engine mask (all engines
  140. * that we might execute on). On processing the bond, the request mask
  141. * is reduced to one or more engines. If the request is subsequently
  142. * bound to a single engine, it will then be constrained to only
  143. * execute on that engine and never returned to the virtual engine
  144. * after timeslicing away, see __unwind_incomplete_requests(). Thus we
  145. * know that if the rq->execution_mask is a single bit, rq->engine
  146. * can be a physical engine with the exact corresponding mask.
  147. */
  148. if (is_power_of_2(rq->execution_mask) &&
  149. !cmpxchg(&rq->engine->request_pool, NULL, rq))
  150. return;
  151. kmem_cache_free(slab_requests, rq);
  152. }
  153. const struct dma_fence_ops i915_fence_ops = {
  154. .get_driver_name = i915_fence_get_driver_name,
  155. .get_timeline_name = i915_fence_get_timeline_name,
  156. .enable_signaling = i915_fence_enable_signaling,
  157. .signaled = i915_fence_signaled,
  158. .wait = i915_fence_wait,
  159. .release = i915_fence_release,
  160. };
  161. static void irq_execute_cb(struct irq_work *wrk)
  162. {
  163. struct execute_cb *cb = container_of(wrk, typeof(*cb), work);
  164. i915_sw_fence_complete(cb->fence);
  165. kmem_cache_free(slab_execute_cbs, cb);
  166. }
  167. static __always_inline void
  168. __notify_execute_cb(struct i915_request *rq, bool (*fn)(struct irq_work *wrk))
  169. {
  170. struct execute_cb *cb, *cn;
  171. if (llist_empty(&rq->execute_cb))
  172. return;
  173. llist_for_each_entry_safe(cb, cn,
  174. llist_del_all(&rq->execute_cb),
  175. work.node.llist)
  176. fn(&cb->work);
  177. }
  178. static void __notify_execute_cb_irq(struct i915_request *rq)
  179. {
  180. __notify_execute_cb(rq, irq_work_queue);
  181. }
  182. static bool irq_work_imm(struct irq_work *wrk)
  183. {
  184. wrk->func(wrk);
  185. return false;
  186. }
  187. void i915_request_notify_execute_cb_imm(struct i915_request *rq)
  188. {
  189. __notify_execute_cb(rq, irq_work_imm);
  190. }
  191. static void __i915_request_fill(struct i915_request *rq, u8 val)
  192. {
  193. void *vaddr = rq->ring->vaddr;
  194. u32 head;
  195. head = rq->infix;
  196. if (rq->postfix < head) {
  197. memset(vaddr + head, val, rq->ring->size - head);
  198. head = 0;
  199. }
  200. memset(vaddr + head, val, rq->postfix - head);
  201. }
  202. /**
  203. * i915_request_active_engine
  204. * @rq: request to inspect
  205. * @active: pointer in which to return the active engine
  206. *
  207. * Fills the currently active engine to the @active pointer if the request
  208. * is active and still not completed.
  209. *
  210. * Returns true if request was active or false otherwise.
  211. */
  212. bool
  213. i915_request_active_engine(struct i915_request *rq,
  214. struct intel_engine_cs **active)
  215. {
  216. struct intel_engine_cs *engine, *locked;
  217. bool ret = false;
  218. /*
  219. * Serialise with __i915_request_submit() so that it sees
  220. * is-banned?, or we know the request is already inflight.
  221. *
  222. * Note that rq->engine is unstable, and so we double
  223. * check that we have acquired the lock on the final engine.
  224. */
  225. locked = READ_ONCE(rq->engine);
  226. spin_lock_irq(&locked->sched_engine->lock);
  227. while (unlikely(locked != (engine = READ_ONCE(rq->engine)))) {
  228. spin_unlock(&locked->sched_engine->lock);
  229. locked = engine;
  230. spin_lock(&locked->sched_engine->lock);
  231. }
  232. if (i915_request_is_active(rq)) {
  233. if (!__i915_request_is_complete(rq))
  234. *active = locked;
  235. ret = true;
  236. }
  237. spin_unlock_irq(&locked->sched_engine->lock);
  238. return ret;
  239. }
  240. static enum hrtimer_restart __rq_watchdog_expired(struct hrtimer *hrtimer)
  241. {
  242. struct i915_request *rq =
  243. container_of(hrtimer, struct i915_request, watchdog.timer);
  244. struct intel_gt *gt = rq->engine->gt;
  245. if (!i915_request_completed(rq)) {
  246. if (llist_add(&rq->watchdog.link, &gt->watchdog.list))
  247. queue_work(gt->i915->unordered_wq, &gt->watchdog.work);
  248. } else {
  249. i915_request_put(rq);
  250. }
  251. return HRTIMER_NORESTART;
  252. }
  253. static void __rq_init_watchdog(struct i915_request *rq)
  254. {
  255. struct i915_request_watchdog *wdg = &rq->watchdog;
  256. hrtimer_setup(&wdg->timer, __rq_watchdog_expired, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  257. }
  258. static void __rq_arm_watchdog(struct i915_request *rq)
  259. {
  260. struct i915_request_watchdog *wdg = &rq->watchdog;
  261. struct intel_context *ce = rq->context;
  262. if (!ce->watchdog.timeout_us)
  263. return;
  264. i915_request_get(rq);
  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 (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[], overwriting
  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(fence->context),
  1126. I915_FENCE_GFP);
  1127. }
  1128. static int
  1129. i915_request_await_external(struct i915_request *rq, struct dma_fence *fence)
  1130. {
  1131. struct dma_fence *iter;
  1132. int err = 0;
  1133. if (!to_dma_fence_chain(fence))
  1134. return __i915_request_await_external(rq, fence);
  1135. dma_fence_chain_for_each(iter, fence) {
  1136. struct dma_fence_chain *chain = to_dma_fence_chain(iter);
  1137. if (!dma_fence_is_i915(chain->fence)) {
  1138. err = __i915_request_await_external(rq, iter);
  1139. break;
  1140. }
  1141. err = i915_request_await_dma_fence(rq, chain->fence);
  1142. if (err < 0)
  1143. break;
  1144. }
  1145. dma_fence_put(iter);
  1146. return err;
  1147. }
  1148. static inline bool is_parallel_rq(struct i915_request *rq)
  1149. {
  1150. return intel_context_is_parallel(rq->context);
  1151. }
  1152. static inline struct intel_context *request_to_parent(struct i915_request *rq)
  1153. {
  1154. return intel_context_to_parent(rq->context);
  1155. }
  1156. static bool is_same_parallel_context(struct i915_request *to,
  1157. struct i915_request *from)
  1158. {
  1159. if (is_parallel_rq(to))
  1160. return request_to_parent(to) == request_to_parent(from);
  1161. return false;
  1162. }
  1163. int
  1164. i915_request_await_execution(struct i915_request *rq,
  1165. struct dma_fence *fence)
  1166. {
  1167. struct dma_fence **child = &fence;
  1168. unsigned int nchild = 1;
  1169. int ret;
  1170. if (dma_fence_is_array(fence)) {
  1171. struct dma_fence_array *array = to_dma_fence_array(fence);
  1172. /* XXX Error for signal-on-any fence arrays */
  1173. child = array->fences;
  1174. nchild = array->num_fences;
  1175. GEM_BUG_ON(!nchild);
  1176. }
  1177. do {
  1178. fence = *child++;
  1179. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  1180. continue;
  1181. if (fence->context == rq->fence.context)
  1182. continue;
  1183. /*
  1184. * We don't squash repeated fence dependencies here as we
  1185. * want to run our callback in all cases.
  1186. */
  1187. if (dma_fence_is_i915(fence)) {
  1188. if (is_same_parallel_context(rq, to_request(fence)))
  1189. continue;
  1190. ret = __i915_request_await_execution(rq,
  1191. to_request(fence));
  1192. } else {
  1193. ret = i915_request_await_external(rq, fence);
  1194. }
  1195. if (ret < 0)
  1196. return ret;
  1197. } while (--nchild);
  1198. return 0;
  1199. }
  1200. static int
  1201. await_request_submit(struct i915_request *to, struct i915_request *from)
  1202. {
  1203. /*
  1204. * If we are waiting on a virtual engine, then it may be
  1205. * constrained to execute on a single engine *prior* to submission.
  1206. * When it is submitted, it will be first submitted to the virtual
  1207. * engine and then passed to the physical engine. We cannot allow
  1208. * the waiter to be submitted immediately to the physical engine
  1209. * as it may then bypass the virtual request.
  1210. */
  1211. if (to->engine == READ_ONCE(from->engine))
  1212. return i915_sw_fence_await_sw_fence_gfp(&to->submit,
  1213. &from->submit,
  1214. I915_FENCE_GFP);
  1215. else
  1216. return __i915_request_await_execution(to, from);
  1217. }
  1218. static int
  1219. i915_request_await_request(struct i915_request *to, struct i915_request *from)
  1220. {
  1221. int ret;
  1222. GEM_BUG_ON(to == from);
  1223. GEM_BUG_ON(to->timeline == from->timeline);
  1224. if (i915_request_completed(from)) {
  1225. i915_sw_fence_set_error_once(&to->submit, from->fence.error);
  1226. return 0;
  1227. }
  1228. if (to->engine->sched_engine->schedule) {
  1229. ret = i915_sched_node_add_dependency(&to->sched,
  1230. &from->sched,
  1231. I915_DEPENDENCY_EXTERNAL);
  1232. if (ret < 0)
  1233. return ret;
  1234. }
  1235. if (!intel_engine_uses_guc(to->engine) &&
  1236. is_power_of_2(to->execution_mask | READ_ONCE(from->execution_mask)))
  1237. ret = await_request_submit(to, from);
  1238. else
  1239. ret = emit_semaphore_wait(to, from, I915_FENCE_GFP);
  1240. if (ret < 0)
  1241. return ret;
  1242. return 0;
  1243. }
  1244. int
  1245. i915_request_await_dma_fence(struct i915_request *rq, struct dma_fence *fence)
  1246. {
  1247. struct dma_fence **child = &fence;
  1248. unsigned int nchild = 1;
  1249. int ret;
  1250. /*
  1251. * Note that if the fence-array was created in signal-on-any mode,
  1252. * we should *not* decompose it into its individual fences. However,
  1253. * we don't currently store which mode the fence-array is operating
  1254. * in. Fortunately, the only user of signal-on-any is private to
  1255. * amdgpu and we should not see any incoming fence-array from
  1256. * sync-file being in signal-on-any mode.
  1257. */
  1258. if (dma_fence_is_array(fence)) {
  1259. struct dma_fence_array *array = to_dma_fence_array(fence);
  1260. child = array->fences;
  1261. nchild = array->num_fences;
  1262. GEM_BUG_ON(!nchild);
  1263. }
  1264. do {
  1265. fence = *child++;
  1266. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
  1267. continue;
  1268. /*
  1269. * Requests on the same timeline are explicitly ordered, along
  1270. * with their dependencies, by i915_request_add() which ensures
  1271. * that requests are submitted in-order through each ring.
  1272. */
  1273. if (fence->context == rq->fence.context)
  1274. continue;
  1275. /* Squash repeated waits to the same timelines */
  1276. if (fence->context &&
  1277. intel_timeline_sync_is_later(i915_request_timeline(rq),
  1278. fence))
  1279. continue;
  1280. if (dma_fence_is_i915(fence)) {
  1281. if (is_same_parallel_context(rq, to_request(fence)))
  1282. continue;
  1283. ret = i915_request_await_request(rq, to_request(fence));
  1284. } else {
  1285. ret = i915_request_await_external(rq, fence);
  1286. }
  1287. if (ret < 0)
  1288. return ret;
  1289. /* Record the latest fence used against each timeline */
  1290. if (fence->context)
  1291. intel_timeline_sync_set(i915_request_timeline(rq),
  1292. fence);
  1293. } while (--nchild);
  1294. return 0;
  1295. }
  1296. /**
  1297. * i915_request_await_deps - set this request to (async) wait upon a struct
  1298. * i915_deps dma_fence collection
  1299. * @rq: request we are wishing to use
  1300. * @deps: The struct i915_deps containing the dependencies.
  1301. *
  1302. * Returns 0 if successful, negative error code on error.
  1303. */
  1304. int i915_request_await_deps(struct i915_request *rq, const struct i915_deps *deps)
  1305. {
  1306. int i, err;
  1307. for (i = 0; i < deps->num_deps; ++i) {
  1308. err = i915_request_await_dma_fence(rq, deps->fences[i]);
  1309. if (err)
  1310. return err;
  1311. }
  1312. return 0;
  1313. }
  1314. /**
  1315. * i915_request_await_object - set this request to (async) wait upon a bo
  1316. * @to: request we are wishing to use
  1317. * @obj: object which may be in use on another ring.
  1318. * @write: whether the wait is on behalf of a writer
  1319. *
  1320. * This code is meant to abstract object synchronization with the GPU.
  1321. * Conceptually we serialise writes between engines inside the GPU.
  1322. * We only allow one engine to write into a buffer at any time, but
  1323. * multiple readers. To ensure each has a coherent view of memory, we must:
  1324. *
  1325. * - If there is an outstanding write request to the object, the new
  1326. * request must wait for it to complete (either CPU or in hw, requests
  1327. * on the same ring will be naturally ordered).
  1328. *
  1329. * - If we are a write request (pending_write_domain is set), the new
  1330. * request must wait for outstanding read requests to complete.
  1331. *
  1332. * Returns 0 if successful, else propagates up the lower layer error.
  1333. */
  1334. int
  1335. i915_request_await_object(struct i915_request *to,
  1336. struct drm_i915_gem_object *obj,
  1337. bool write)
  1338. {
  1339. struct dma_resv_iter cursor;
  1340. struct dma_fence *fence;
  1341. int ret = 0;
  1342. dma_resv_for_each_fence(&cursor, obj->base.resv,
  1343. dma_resv_usage_rw(write), fence) {
  1344. ret = i915_request_await_dma_fence(to, fence);
  1345. if (ret)
  1346. break;
  1347. }
  1348. return ret;
  1349. }
  1350. static void i915_request_await_huc(struct i915_request *rq)
  1351. {
  1352. struct intel_huc *huc = &rq->context->engine->gt->uc.huc;
  1353. /* don't stall kernel submissions! */
  1354. if (!rcu_access_pointer(rq->context->gem_context))
  1355. return;
  1356. if (intel_huc_wait_required(huc))
  1357. i915_sw_fence_await_sw_fence(&rq->submit,
  1358. &huc->delayed_load.fence,
  1359. &rq->hucq);
  1360. }
  1361. static struct i915_request *
  1362. __i915_request_ensure_parallel_ordering(struct i915_request *rq,
  1363. struct intel_timeline *timeline)
  1364. {
  1365. struct i915_request *prev;
  1366. GEM_BUG_ON(!is_parallel_rq(rq));
  1367. prev = request_to_parent(rq)->parallel.last_rq;
  1368. if (prev) {
  1369. if (!__i915_request_is_complete(prev)) {
  1370. i915_sw_fence_await_sw_fence(&rq->submit,
  1371. &prev->submit,
  1372. &rq->submitq);
  1373. if (rq->engine->sched_engine->schedule)
  1374. __i915_sched_node_add_dependency(&rq->sched,
  1375. &prev->sched,
  1376. &rq->dep,
  1377. 0);
  1378. }
  1379. i915_request_put(prev);
  1380. }
  1381. request_to_parent(rq)->parallel.last_rq = i915_request_get(rq);
  1382. /*
  1383. * Users have to put a reference potentially got by
  1384. * __i915_active_fence_set() to the returned request
  1385. * when no longer needed
  1386. */
  1387. return to_request(__i915_active_fence_set(&timeline->last_request,
  1388. &rq->fence));
  1389. }
  1390. static struct i915_request *
  1391. __i915_request_ensure_ordering(struct i915_request *rq,
  1392. struct intel_timeline *timeline)
  1393. {
  1394. struct i915_request *prev;
  1395. GEM_BUG_ON(is_parallel_rq(rq));
  1396. prev = to_request(__i915_active_fence_set(&timeline->last_request,
  1397. &rq->fence));
  1398. if (prev && !__i915_request_is_complete(prev)) {
  1399. bool uses_guc = intel_engine_uses_guc(rq->engine);
  1400. bool pow2 = is_power_of_2(READ_ONCE(prev->engine)->mask |
  1401. rq->engine->mask);
  1402. bool same_context = prev->context == rq->context;
  1403. /*
  1404. * The requests are supposed to be kept in order. However,
  1405. * we need to be wary in case the timeline->last_request
  1406. * is used as a barrier for external modification to this
  1407. * context.
  1408. */
  1409. GEM_BUG_ON(same_context &&
  1410. i915_seqno_passed(prev->fence.seqno,
  1411. rq->fence.seqno));
  1412. if ((same_context && uses_guc) || (!uses_guc && pow2))
  1413. i915_sw_fence_await_sw_fence(&rq->submit,
  1414. &prev->submit,
  1415. &rq->submitq);
  1416. else
  1417. __i915_sw_fence_await_dma_fence(&rq->submit,
  1418. &prev->fence,
  1419. &rq->dmaq);
  1420. if (rq->engine->sched_engine->schedule)
  1421. __i915_sched_node_add_dependency(&rq->sched,
  1422. &prev->sched,
  1423. &rq->dep,
  1424. 0);
  1425. }
  1426. /*
  1427. * Users have to put the reference to prev potentially got
  1428. * by __i915_active_fence_set() when no longer needed
  1429. */
  1430. return prev;
  1431. }
  1432. static struct i915_request *
  1433. __i915_request_add_to_timeline(struct i915_request *rq)
  1434. {
  1435. struct intel_timeline *timeline = i915_request_timeline(rq);
  1436. struct i915_request *prev;
  1437. /*
  1438. * Media workloads may require HuC, so stall them until HuC loading is
  1439. * complete. Note that HuC not being loaded when a user submission
  1440. * arrives can only happen when HuC is loaded via GSC and in that case
  1441. * we still expect the window between us starting to accept submissions
  1442. * and HuC loading completion to be small (a few hundred ms).
  1443. */
  1444. if (rq->engine->class == VIDEO_DECODE_CLASS)
  1445. i915_request_await_huc(rq);
  1446. /*
  1447. * Dependency tracking and request ordering along the timeline
  1448. * is special cased so that we can eliminate redundant ordering
  1449. * operations while building the request (we know that the timeline
  1450. * itself is ordered, and here we guarantee it).
  1451. *
  1452. * As we know we will need to emit tracking along the timeline,
  1453. * we embed the hooks into our request struct -- at the cost of
  1454. * having to have specialised no-allocation interfaces (which will
  1455. * be beneficial elsewhere).
  1456. *
  1457. * A second benefit to open-coding i915_request_await_request is
  1458. * that we can apply a slight variant of the rules specialised
  1459. * for timelines that jump between engines (such as virtual engines).
  1460. * If we consider the case of virtual engine, we must emit a dma-fence
  1461. * to prevent scheduling of the second request until the first is
  1462. * complete (to maximise our greedy late load balancing) and this
  1463. * precludes optimising to use semaphores serialisation of a single
  1464. * timeline across engines.
  1465. *
  1466. * We do not order parallel submission requests on the timeline as each
  1467. * parallel submission context has its own timeline and the ordering
  1468. * rules for parallel requests are that they must be submitted in the
  1469. * order received from the execbuf IOCTL. So rather than using the
  1470. * timeline we store a pointer to last request submitted in the
  1471. * relationship in the gem context and insert a submission fence
  1472. * between that request and request passed into this function or
  1473. * alternatively we use completion fence if gem context has a single
  1474. * timeline and this is the first submission of an execbuf IOCTL.
  1475. */
  1476. if (likely(!is_parallel_rq(rq)))
  1477. prev = __i915_request_ensure_ordering(rq, timeline);
  1478. else
  1479. prev = __i915_request_ensure_parallel_ordering(rq, timeline);
  1480. if (prev)
  1481. i915_request_put(prev);
  1482. /*
  1483. * Make sure that no request gazumped us - if it was allocated after
  1484. * our i915_request_alloc() and called __i915_request_add() before
  1485. * us, the timeline will hold its seqno which is later than ours.
  1486. */
  1487. GEM_BUG_ON(timeline->seqno != rq->fence.seqno);
  1488. return prev;
  1489. }
  1490. /*
  1491. * NB: This function is not allowed to fail. Doing so would mean the the
  1492. * request is not being tracked for completion but the work itself is
  1493. * going to happen on the hardware. This would be a Bad Thing(tm).
  1494. */
  1495. struct i915_request *__i915_request_commit(struct i915_request *rq)
  1496. {
  1497. struct intel_engine_cs *engine = rq->engine;
  1498. struct intel_ring *ring = rq->ring;
  1499. u32 *cs;
  1500. RQ_TRACE(rq, "\n");
  1501. /*
  1502. * To ensure that this call will not fail, space for its emissions
  1503. * should already have been reserved in the ring buffer. Let the ring
  1504. * know that it is time to use that space up.
  1505. */
  1506. GEM_BUG_ON(rq->reserved_space > ring->space);
  1507. rq->reserved_space = 0;
  1508. rq->emitted_jiffies = jiffies;
  1509. /*
  1510. * Record the position of the start of the breadcrumb so that
  1511. * should we detect the updated seqno part-way through the
  1512. * GPU processing the request, we never over-estimate the
  1513. * position of the ring's HEAD.
  1514. */
  1515. cs = intel_ring_begin(rq, engine->emit_fini_breadcrumb_dw);
  1516. GEM_BUG_ON(IS_ERR(cs));
  1517. rq->postfix = intel_ring_offset(rq, cs);
  1518. return __i915_request_add_to_timeline(rq);
  1519. }
  1520. void __i915_request_queue_bh(struct i915_request *rq)
  1521. {
  1522. i915_sw_fence_commit(&rq->semaphore);
  1523. i915_sw_fence_commit(&rq->submit);
  1524. }
  1525. void __i915_request_queue(struct i915_request *rq,
  1526. const struct i915_sched_attr *attr)
  1527. {
  1528. /*
  1529. * Let the backend know a new request has arrived that may need
  1530. * to adjust the existing execution schedule due to a high priority
  1531. * request - i.e. we may want to preempt the current request in order
  1532. * to run a high priority dependency chain *before* we can execute this
  1533. * request.
  1534. *
  1535. * This is called before the request is ready to run so that we can
  1536. * decide whether to preempt the entire chain so that it is ready to
  1537. * run at the earliest possible convenience.
  1538. */
  1539. if (attr && rq->engine->sched_engine->schedule)
  1540. rq->engine->sched_engine->schedule(rq, attr);
  1541. local_bh_disable();
  1542. __i915_request_queue_bh(rq);
  1543. local_bh_enable(); /* kick tasklets */
  1544. }
  1545. void i915_request_add(struct i915_request *rq)
  1546. {
  1547. struct intel_timeline * const tl = i915_request_timeline(rq);
  1548. struct i915_sched_attr attr = {};
  1549. struct i915_gem_context *ctx;
  1550. lockdep_assert_held(&tl->mutex);
  1551. lockdep_unpin_lock(&tl->mutex, rq->cookie);
  1552. trace_i915_request_add(rq);
  1553. __i915_request_commit(rq);
  1554. /* XXX placeholder for selftests */
  1555. rcu_read_lock();
  1556. ctx = rcu_dereference(rq->context->gem_context);
  1557. if (ctx)
  1558. attr = ctx->sched;
  1559. rcu_read_unlock();
  1560. __i915_request_queue(rq, &attr);
  1561. mutex_unlock(&tl->mutex);
  1562. }
  1563. static unsigned long local_clock_ns(unsigned int *cpu)
  1564. {
  1565. unsigned long t;
  1566. /*
  1567. * Cheaply and approximately convert from nanoseconds to microseconds.
  1568. * The result and subsequent calculations are also defined in the same
  1569. * approximate microseconds units. The principal source of timing
  1570. * error here is from the simple truncation.
  1571. *
  1572. * Note that local_clock() is only defined wrt to the current CPU;
  1573. * the comparisons are no longer valid if we switch CPUs. Instead of
  1574. * blocking preemption for the entire busywait, we can detect the CPU
  1575. * switch and use that as indicator of system load and a reason to
  1576. * stop busywaiting, see busywait_stop().
  1577. */
  1578. *cpu = get_cpu();
  1579. t = local_clock();
  1580. put_cpu();
  1581. return t;
  1582. }
  1583. static bool busywait_stop(unsigned long timeout, unsigned int cpu)
  1584. {
  1585. unsigned int this_cpu;
  1586. if (time_after(local_clock_ns(&this_cpu), timeout))
  1587. return true;
  1588. return this_cpu != cpu;
  1589. }
  1590. static bool __i915_spin_request(struct i915_request * const rq, int state)
  1591. {
  1592. unsigned long timeout_ns;
  1593. unsigned int cpu;
  1594. /*
  1595. * Only wait for the request if we know it is likely to complete.
  1596. *
  1597. * We don't track the timestamps around requests, nor the average
  1598. * request length, so we do not have a good indicator that this
  1599. * request will complete within the timeout. What we do know is the
  1600. * order in which requests are executed by the context and so we can
  1601. * tell if the request has been started. If the request is not even
  1602. * running yet, it is a fair assumption that it will not complete
  1603. * within our relatively short timeout.
  1604. */
  1605. if (!i915_request_is_running(rq))
  1606. return false;
  1607. /*
  1608. * When waiting for high frequency requests, e.g. during synchronous
  1609. * rendering split between the CPU and GPU, the finite amount of time
  1610. * required to set up the irq and wait upon it limits the response
  1611. * rate. By busywaiting on the request completion for a short while we
  1612. * can service the high frequency waits as quick as possible. However,
  1613. * if it is a slow request, we want to sleep as quickly as possible.
  1614. * The tradeoff between waiting and sleeping is roughly the time it
  1615. * takes to sleep on a request, on the order of a microsecond.
  1616. */
  1617. timeout_ns = READ_ONCE(rq->engine->props.max_busywait_duration_ns);
  1618. timeout_ns += local_clock_ns(&cpu);
  1619. do {
  1620. if (dma_fence_is_signaled(&rq->fence))
  1621. return true;
  1622. if (signal_pending_state(state, current))
  1623. break;
  1624. if (busywait_stop(timeout_ns, cpu))
  1625. break;
  1626. cpu_relax();
  1627. } while (!need_resched());
  1628. return false;
  1629. }
  1630. struct request_wait {
  1631. struct dma_fence_cb cb;
  1632. struct task_struct *tsk;
  1633. };
  1634. static void request_wait_wake(struct dma_fence *fence, struct dma_fence_cb *cb)
  1635. {
  1636. struct request_wait *wait = container_of(cb, typeof(*wait), cb);
  1637. wake_up_process(fetch_and_zero(&wait->tsk));
  1638. }
  1639. /**
  1640. * i915_request_wait_timeout - wait until execution of request has finished
  1641. * @rq: the request to wait upon
  1642. * @flags: how to wait
  1643. * @timeout: how long to wait in jiffies
  1644. *
  1645. * i915_request_wait_timeout() waits for the request to be completed, for a
  1646. * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
  1647. * unbounded wait).
  1648. *
  1649. * Returns the remaining time (in jiffies) if the request completed, which may
  1650. * be zero if the request is unfinished after the timeout expires.
  1651. * If the timeout is 0, it will return 1 if the fence is signaled.
  1652. *
  1653. * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
  1654. * pending before the request completes.
  1655. *
  1656. * NOTE: This function has the same wait semantics as dma-fence.
  1657. */
  1658. long i915_request_wait_timeout(struct i915_request *rq,
  1659. unsigned int flags,
  1660. long timeout)
  1661. {
  1662. const int state = flags & I915_WAIT_INTERRUPTIBLE ?
  1663. TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;
  1664. struct request_wait wait;
  1665. might_sleep();
  1666. GEM_BUG_ON(timeout < 0);
  1667. if (dma_fence_is_signaled(&rq->fence))
  1668. return timeout ?: 1;
  1669. if (!timeout)
  1670. return -ETIME;
  1671. trace_i915_request_wait_begin(rq, flags);
  1672. /*
  1673. * We must never wait on the GPU while holding a lock as we
  1674. * may need to perform a GPU reset. So while we don't need to
  1675. * serialise wait/reset with an explicit lock, we do want
  1676. * lockdep to detect potential dependency cycles.
  1677. */
  1678. mutex_acquire(&rq->engine->gt->reset.mutex.dep_map, 0, 0, _THIS_IP_);
  1679. /*
  1680. * Optimistic spin before touching IRQs.
  1681. *
  1682. * We may use a rather large value here to offset the penalty of
  1683. * switching away from the active task. Frequently, the client will
  1684. * wait upon an old swapbuffer to throttle itself to remain within a
  1685. * frame of the gpu. If the client is running in lockstep with the gpu,
  1686. * then it should not be waiting long at all, and a sleep now will incur
  1687. * extra scheduler latency in producing the next frame. To try to
  1688. * avoid adding the cost of enabling/disabling the interrupt to the
  1689. * short wait, we first spin to see if the request would have completed
  1690. * in the time taken to setup the interrupt.
  1691. *
  1692. * We need upto 5us to enable the irq, and upto 20us to hide the
  1693. * scheduler latency of a context switch, ignoring the secondary
  1694. * impacts from a context switch such as cache eviction.
  1695. *
  1696. * The scheme used for low-latency IO is called "hybrid interrupt
  1697. * polling". The suggestion there is to sleep until just before you
  1698. * expect to be woken by the device interrupt and then poll for its
  1699. * completion. That requires having a good predictor for the request
  1700. * duration, which we currently lack.
  1701. */
  1702. if (CONFIG_DRM_I915_MAX_REQUEST_BUSYWAIT &&
  1703. __i915_spin_request(rq, state))
  1704. goto out;
  1705. /*
  1706. * This client is about to stall waiting for the GPU. In many cases
  1707. * this is undesirable and limits the throughput of the system, as
  1708. * many clients cannot continue processing user input/output whilst
  1709. * blocked. RPS autotuning may take tens of milliseconds to respond
  1710. * to the GPU load and thus incurs additional latency for the client.
  1711. * We can circumvent that by promoting the GPU frequency to maximum
  1712. * before we sleep. This makes the GPU throttle up much more quickly
  1713. * (good for benchmarks and user experience, e.g. window animations),
  1714. * but at a cost of spending more power processing the workload
  1715. * (bad for battery).
  1716. */
  1717. if (flags & I915_WAIT_PRIORITY && !i915_request_started(rq))
  1718. intel_rps_boost(rq);
  1719. wait.tsk = current;
  1720. if (dma_fence_add_callback(&rq->fence, &wait.cb, request_wait_wake))
  1721. goto out;
  1722. /*
  1723. * Flush the submission tasklet, but only if it may help this request.
  1724. *
  1725. * We sometimes experience some latency between the HW interrupts and
  1726. * tasklet execution (mostly due to ksoftirqd latency, but it can also
  1727. * be due to lazy CS events), so lets run the tasklet manually if there
  1728. * is a chance it may submit this request. If the request is not ready
  1729. * to run, as it is waiting for other fences to be signaled, flushing
  1730. * the tasklet is busy work without any advantage for this client.
  1731. *
  1732. * If the HW is being lazy, this is the last chance before we go to
  1733. * sleep to catch any pending events. We will check periodically in
  1734. * the heartbeat to flush the submission tasklets as a last resort
  1735. * for unhappy HW.
  1736. */
  1737. if (i915_request_is_ready(rq))
  1738. __intel_engine_flush_submission(rq->engine, false);
  1739. for (;;) {
  1740. set_current_state(state);
  1741. if (dma_fence_is_signaled(&rq->fence))
  1742. break;
  1743. if (signal_pending_state(state, current)) {
  1744. timeout = -ERESTARTSYS;
  1745. break;
  1746. }
  1747. if (!timeout) {
  1748. timeout = -ETIME;
  1749. break;
  1750. }
  1751. timeout = io_schedule_timeout(timeout);
  1752. }
  1753. __set_current_state(TASK_RUNNING);
  1754. if (READ_ONCE(wait.tsk))
  1755. dma_fence_remove_callback(&rq->fence, &wait.cb);
  1756. GEM_BUG_ON(!list_empty(&wait.cb.node));
  1757. out:
  1758. mutex_release(&rq->engine->gt->reset.mutex.dep_map, _THIS_IP_);
  1759. trace_i915_request_wait_end(rq);
  1760. return timeout;
  1761. }
  1762. /**
  1763. * i915_request_wait - wait until execution of request has finished
  1764. * @rq: the request to wait upon
  1765. * @flags: how to wait
  1766. * @timeout: how long to wait in jiffies
  1767. *
  1768. * i915_request_wait() waits for the request to be completed, for a
  1769. * maximum of @timeout jiffies (with MAX_SCHEDULE_TIMEOUT implying an
  1770. * unbounded wait).
  1771. *
  1772. * Returns the remaining time (in jiffies) if the request completed, which may
  1773. * be zero or -ETIME if the request is unfinished after the timeout expires.
  1774. * May return -EINTR is called with I915_WAIT_INTERRUPTIBLE and a signal is
  1775. * pending before the request completes.
  1776. *
  1777. * NOTE: This function behaves differently from dma-fence wait semantics for
  1778. * timeout = 0. It returns 0 on success, and -ETIME if not signaled.
  1779. */
  1780. long i915_request_wait(struct i915_request *rq,
  1781. unsigned int flags,
  1782. long timeout)
  1783. {
  1784. long ret = i915_request_wait_timeout(rq, flags, timeout);
  1785. if (!ret)
  1786. return -ETIME;
  1787. if (ret > 0 && !timeout)
  1788. return 0;
  1789. return ret;
  1790. }
  1791. static int print_sched_attr(const struct i915_sched_attr *attr,
  1792. char *buf, int x, int len)
  1793. {
  1794. if (attr->priority == I915_PRIORITY_INVALID)
  1795. return x;
  1796. x += snprintf(buf + x, len - x,
  1797. " prio=%d", attr->priority);
  1798. return x;
  1799. }
  1800. static char queue_status(const struct i915_request *rq)
  1801. {
  1802. if (i915_request_is_active(rq))
  1803. return 'E';
  1804. if (i915_request_is_ready(rq))
  1805. return intel_engine_is_virtual(rq->engine) ? 'V' : 'R';
  1806. return 'U';
  1807. }
  1808. static const char *run_status(const struct i915_request *rq)
  1809. {
  1810. if (__i915_request_is_complete(rq))
  1811. return "!";
  1812. if (__i915_request_has_started(rq))
  1813. return "*";
  1814. if (!i915_sw_fence_signaled(&rq->semaphore))
  1815. return "&";
  1816. return "";
  1817. }
  1818. static const char *fence_status(const struct i915_request *rq)
  1819. {
  1820. if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &rq->fence.flags))
  1821. return "+";
  1822. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &rq->fence.flags))
  1823. return "-";
  1824. return "";
  1825. }
  1826. void i915_request_show(struct drm_printer *m,
  1827. const struct i915_request *rq,
  1828. const char *prefix,
  1829. int indent)
  1830. {
  1831. const char __rcu *timeline;
  1832. char buf[80] = "";
  1833. int x = 0;
  1834. /*
  1835. * The prefix is used to show the queue status, for which we use
  1836. * the following flags:
  1837. *
  1838. * U [Unready]
  1839. * - initial status upon being submitted by the user
  1840. *
  1841. * - the request is not ready for execution as it is waiting
  1842. * for external fences
  1843. *
  1844. * R [Ready]
  1845. * - all fences the request was waiting on have been signaled,
  1846. * and the request is now ready for execution and will be
  1847. * in a backend queue
  1848. *
  1849. * - a ready request may still need to wait on semaphores
  1850. * [internal fences]
  1851. *
  1852. * V [Ready/virtual]
  1853. * - same as ready, but queued over multiple backends
  1854. *
  1855. * E [Executing]
  1856. * - the request has been transferred from the backend queue and
  1857. * submitted for execution on HW
  1858. *
  1859. * - a completed request may still be regarded as executing, its
  1860. * status may not be updated until it is retired and removed
  1861. * from the lists
  1862. */
  1863. x = print_sched_attr(&rq->sched.attr, buf, x, sizeof(buf));
  1864. rcu_read_lock();
  1865. timeline = dma_fence_timeline_name((struct dma_fence *)&rq->fence);
  1866. drm_printf(m, "%s%.*s%c %llx:%lld%s%s %s @ %dms: %s\n",
  1867. prefix, indent, " ",
  1868. queue_status(rq),
  1869. rq->fence.context, rq->fence.seqno,
  1870. run_status(rq),
  1871. fence_status(rq),
  1872. buf,
  1873. jiffies_to_msecs(jiffies - rq->emitted_jiffies),
  1874. rcu_dereference(timeline));
  1875. rcu_read_unlock();
  1876. }
  1877. static bool engine_match_ring(struct intel_engine_cs *engine, struct i915_request *rq)
  1878. {
  1879. u32 ring = ENGINE_READ(engine, RING_START);
  1880. return ring == i915_ggtt_offset(rq->ring->vma);
  1881. }
  1882. static bool match_ring(struct i915_request *rq)
  1883. {
  1884. struct intel_engine_cs *engine;
  1885. bool found;
  1886. int i;
  1887. if (!intel_engine_is_virtual(rq->engine))
  1888. return engine_match_ring(rq->engine, rq);
  1889. found = false;
  1890. i = 0;
  1891. while ((engine = intel_engine_get_sibling(rq->engine, i++))) {
  1892. found = engine_match_ring(engine, rq);
  1893. if (found)
  1894. break;
  1895. }
  1896. return found;
  1897. }
  1898. enum i915_request_state i915_test_request_state(struct i915_request *rq)
  1899. {
  1900. if (i915_request_completed(rq))
  1901. return I915_REQUEST_COMPLETE;
  1902. if (!i915_request_started(rq))
  1903. return I915_REQUEST_PENDING;
  1904. if (match_ring(rq))
  1905. return I915_REQUEST_ACTIVE;
  1906. return I915_REQUEST_QUEUED;
  1907. }
  1908. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  1909. #include "selftests/mock_request.c"
  1910. #include "selftests/i915_request.c"
  1911. #endif
  1912. void i915_request_module_exit(void)
  1913. {
  1914. kmem_cache_destroy(slab_execute_cbs);
  1915. kmem_cache_destroy(slab_requests);
  1916. }
  1917. int __init i915_request_module_init(void)
  1918. {
  1919. slab_requests =
  1920. kmem_cache_create("i915_request",
  1921. sizeof(struct i915_request),
  1922. __alignof__(struct i915_request),
  1923. SLAB_HWCACHE_ALIGN |
  1924. SLAB_RECLAIM_ACCOUNT |
  1925. SLAB_TYPESAFE_BY_RCU,
  1926. __i915_request_ctor);
  1927. if (!slab_requests)
  1928. return -ENOMEM;
  1929. slab_execute_cbs = KMEM_CACHE(execute_cb,
  1930. SLAB_HWCACHE_ALIGN |
  1931. SLAB_RECLAIM_ACCOUNT |
  1932. SLAB_TYPESAFE_BY_RCU);
  1933. if (!slab_execute_cbs)
  1934. goto err_requests;
  1935. return 0;
  1936. err_requests:
  1937. kmem_cache_destroy(slab_requests);
  1938. return -ENOMEM;
  1939. }