intel_lrc.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  1. /*
  2. * Copyright © 2014 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. * Authors:
  24. * Ben Widawsky <ben@bwidawsk.net>
  25. * Michel Thierry <michel.thierry@intel.com>
  26. * Thomas Daniel <thomas.daniel@intel.com>
  27. * Oscar Mateo <oscar.mateo@intel.com>
  28. *
  29. */
  30. /**
  31. * DOC: Logical Rings, Logical Ring Contexts and Execlists
  32. *
  33. * Motivation:
  34. * GEN8 brings an expansion of the HW contexts: "Logical Ring Contexts".
  35. * These expanded contexts enable a number of new abilities, especially
  36. * "Execlists" (also implemented in this file).
  37. *
  38. * One of the main differences with the legacy HW contexts is that logical
  39. * ring contexts incorporate many more things to the context's state, like
  40. * PDPs or ringbuffer control registers:
  41. *
  42. * The reason why PDPs are included in the context is straightforward: as
  43. * PPGTTs (per-process GTTs) are actually per-context, having the PDPs
  44. * contained there mean you don't need to do a ppgtt->switch_mm yourself,
  45. * instead, the GPU will do it for you on the context switch.
  46. *
  47. * But, what about the ringbuffer control registers (head, tail, etc..)?
  48. * shouldn't we just need a set of those per engine command streamer? This is
  49. * where the name "Logical Rings" starts to make sense: by virtualizing the
  50. * rings, the engine cs shifts to a new "ring buffer" with every context
  51. * switch. When you want to submit a workload to the GPU you: A) choose your
  52. * context, B) find its appropriate virtualized ring, C) write commands to it
  53. * and then, finally, D) tell the GPU to switch to that context.
  54. *
  55. * Instead of the legacy MI_SET_CONTEXT, the way you tell the GPU to switch
  56. * to a contexts is via a context execution list, ergo "Execlists".
  57. *
  58. * LRC implementation:
  59. * Regarding the creation of contexts, we have:
  60. *
  61. * - One global default context.
  62. * - One local default context for each opened fd.
  63. * - One local extra context for each context create ioctl call.
  64. *
  65. * Now that ringbuffers belong per-context (and not per-engine, like before)
  66. * and that contexts are uniquely tied to a given engine (and not reusable,
  67. * like before) we need:
  68. *
  69. * - One ringbuffer per-engine inside each context.
  70. * - One backing object per-engine inside each context.
  71. *
  72. * The global default context starts its life with these new objects fully
  73. * allocated and populated. The local default context for each opened fd is
  74. * more complex, because we don't know at creation time which engine is going
  75. * to use them. To handle this, we have implemented a deferred creation of LR
  76. * contexts:
  77. *
  78. * The local context starts its life as a hollow or blank holder, that only
  79. * gets populated for a given engine once we receive an execbuffer. If later
  80. * on we receive another execbuffer ioctl for the same context but a different
  81. * engine, we allocate/populate a new ringbuffer and context backing object and
  82. * so on.
  83. *
  84. * Finally, regarding local contexts created using the ioctl call: as they are
  85. * only allowed with the render ring, we can allocate & populate them right
  86. * away (no need to defer anything, at least for now).
  87. *
  88. * Execlists implementation:
  89. * Execlists are the new method by which, on gen8+ hardware, workloads are
  90. * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
  91. * This method works as follows:
  92. *
  93. * When a request is committed, its commands (the BB start and any leading or
  94. * trailing commands, like the seqno breadcrumbs) are placed in the ringbuffer
  95. * for the appropriate context. The tail pointer in the hardware context is not
  96. * updated at this time, but instead, kept by the driver in the ringbuffer
  97. * structure. A structure representing this request is added to a request queue
  98. * for the appropriate engine: this structure contains a copy of the context's
  99. * tail after the request was written to the ring buffer and a pointer to the
  100. * context itself.
  101. *
  102. * If the engine's request queue was empty before the request was added, the
  103. * queue is processed immediately. Otherwise the queue will be processed during
  104. * a context switch interrupt. In any case, elements on the queue will get sent
  105. * (in pairs) to the GPU's ExecLists Submit Port (ELSP, for short) with a
  106. * globally unique 20-bits submission ID.
  107. *
  108. * When execution of a request completes, the GPU updates the context status
  109. * buffer with a context complete event and generates a context switch interrupt.
  110. * During the interrupt handling, the driver examines the events in the buffer:
  111. * for each context complete event, if the announced ID matches that on the head
  112. * of the request queue, then that request is retired and removed from the queue.
  113. *
  114. * After processing, if any requests were retired and the queue is not empty
  115. * then a new execution list can be submitted. The two requests at the front of
  116. * the queue are next to be submitted but since a context may not occur twice in
  117. * an execution list, if subsequent requests have the same ID as the first then
  118. * the two requests must be combined. This is done simply by discarding requests
  119. * at the head of the queue until either only one requests is left (in which case
  120. * we use a NULL second context) or the first two requests have unique IDs.
  121. *
  122. * By always executing the first two requests in the queue the driver ensures
  123. * that the GPU is kept as busy as possible. In the case where a single context
  124. * completes but a second context is still executing, the request for this second
  125. * context will be at the head of the queue when we remove the first one. This
  126. * request will then be resubmitted along with a new request for a different context,
  127. * which will cause the hardware to continue executing the second request and queue
  128. * the new request (the GPU detects the condition of a context getting preempted
  129. * with the same context and optimizes the context switch flow by not doing
  130. * preemption, but just sampling the new tail pointer).
  131. *
  132. */
  133. #include <linux/interrupt.h>
  134. #include <drm/drmP.h>
  135. #include <drm/i915_drm.h>
  136. #include "i915_drv.h"
  137. #include "intel_mocs.h"
  138. #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
  139. #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
  140. #define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
  141. #define RING_EXECLIST_QFULL (1 << 0x2)
  142. #define RING_EXECLIST1_VALID (1 << 0x3)
  143. #define RING_EXECLIST0_VALID (1 << 0x4)
  144. #define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
  145. #define RING_EXECLIST1_ACTIVE (1 << 0x11)
  146. #define RING_EXECLIST0_ACTIVE (1 << 0x12)
  147. #define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
  148. #define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
  149. #define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
  150. #define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
  151. #define GEN8_CTX_STATUS_COMPLETE (1 << 4)
  152. #define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
  153. #define GEN8_CTX_STATUS_COMPLETED_MASK \
  154. (GEN8_CTX_STATUS_ACTIVE_IDLE | \
  155. GEN8_CTX_STATUS_PREEMPTED | \
  156. GEN8_CTX_STATUS_ELEMENT_SWITCH)
  157. #define CTX_LRI_HEADER_0 0x01
  158. #define CTX_CONTEXT_CONTROL 0x02
  159. #define CTX_RING_HEAD 0x04
  160. #define CTX_RING_TAIL 0x06
  161. #define CTX_RING_BUFFER_START 0x08
  162. #define CTX_RING_BUFFER_CONTROL 0x0a
  163. #define CTX_BB_HEAD_U 0x0c
  164. #define CTX_BB_HEAD_L 0x0e
  165. #define CTX_BB_STATE 0x10
  166. #define CTX_SECOND_BB_HEAD_U 0x12
  167. #define CTX_SECOND_BB_HEAD_L 0x14
  168. #define CTX_SECOND_BB_STATE 0x16
  169. #define CTX_BB_PER_CTX_PTR 0x18
  170. #define CTX_RCS_INDIRECT_CTX 0x1a
  171. #define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
  172. #define CTX_LRI_HEADER_1 0x21
  173. #define CTX_CTX_TIMESTAMP 0x22
  174. #define CTX_PDP3_UDW 0x24
  175. #define CTX_PDP3_LDW 0x26
  176. #define CTX_PDP2_UDW 0x28
  177. #define CTX_PDP2_LDW 0x2a
  178. #define CTX_PDP1_UDW 0x2c
  179. #define CTX_PDP1_LDW 0x2e
  180. #define CTX_PDP0_UDW 0x30
  181. #define CTX_PDP0_LDW 0x32
  182. #define CTX_LRI_HEADER_2 0x41
  183. #define CTX_R_PWR_CLK_STATE 0x42
  184. #define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
  185. #define ASSIGN_CTX_REG(reg_state, pos, reg, val) do { \
  186. (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
  187. (reg_state)[(pos)+1] = (val); \
  188. } while (0)
  189. #define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \
  190. const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
  191. reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
  192. reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
  193. } while (0)
  194. #define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
  195. reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \
  196. reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \
  197. } while (0)
  198. #define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
  199. #define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26
  200. /* Typical size of the average request (2 pipecontrols and a MI_BB) */
  201. #define EXECLISTS_REQUEST_SIZE 64 /* bytes */
  202. #define WA_TAIL_DWORDS 2
  203. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  204. struct intel_engine_cs *engine);
  205. static void execlists_init_reg_state(u32 *reg_state,
  206. struct i915_gem_context *ctx,
  207. struct intel_engine_cs *engine,
  208. struct intel_ring *ring);
  209. /**
  210. * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
  211. * @dev_priv: i915 device private
  212. * @enable_execlists: value of i915.enable_execlists module parameter.
  213. *
  214. * Only certain platforms support Execlists (the prerequisites being
  215. * support for Logical Ring Contexts and Aliasing PPGTT or better).
  216. *
  217. * Return: 1 if Execlists is supported and has to be enabled.
  218. */
  219. int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists)
  220. {
  221. /* On platforms with execlist available, vGPU will only
  222. * support execlist mode, no ring buffer mode.
  223. */
  224. if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv))
  225. return 1;
  226. if (INTEL_GEN(dev_priv) >= 9)
  227. return 1;
  228. if (enable_execlists == 0)
  229. return 0;
  230. if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) &&
  231. USES_PPGTT(dev_priv) &&
  232. i915.use_mmio_flip >= 0)
  233. return 1;
  234. return 0;
  235. }
  236. /**
  237. * intel_lr_context_descriptor_update() - calculate & cache the descriptor
  238. * descriptor for a pinned context
  239. * @ctx: Context to work on
  240. * @engine: Engine the descriptor will be used with
  241. *
  242. * The context descriptor encodes various attributes of a context,
  243. * including its GTT address and some flags. Because it's fairly
  244. * expensive to calculate, we'll just do it once and cache the result,
  245. * which remains valid until the context is unpinned.
  246. *
  247. * This is what a descriptor looks like, from LSB to MSB::
  248. *
  249. * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
  250. * bits 12-31: LRCA, GTT address of (the HWSP of) this context
  251. * bits 32-52: ctx ID, a globally unique tag
  252. * bits 53-54: mbz, reserved for use by hardware
  253. * bits 55-63: group ID, currently unused and set to 0
  254. */
  255. static void
  256. intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
  257. struct intel_engine_cs *engine)
  258. {
  259. struct intel_context *ce = &ctx->engine[engine->id];
  260. u64 desc;
  261. BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
  262. desc = ctx->desc_template; /* bits 0-11 */
  263. desc |= i915_ggtt_offset(ce->state) + LRC_PPHWSP_PN * PAGE_SIZE;
  264. /* bits 12-31 */
  265. desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
  266. ce->lrc_desc = desc;
  267. }
  268. uint64_t intel_lr_context_descriptor(struct i915_gem_context *ctx,
  269. struct intel_engine_cs *engine)
  270. {
  271. return ctx->engine[engine->id].lrc_desc;
  272. }
  273. static inline void
  274. execlists_context_status_change(struct drm_i915_gem_request *rq,
  275. unsigned long status)
  276. {
  277. /*
  278. * Only used when GVT-g is enabled now. When GVT-g is disabled,
  279. * The compiler should eliminate this function as dead-code.
  280. */
  281. if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
  282. return;
  283. atomic_notifier_call_chain(&rq->ctx->status_notifier, status, rq);
  284. }
  285. static void
  286. execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
  287. {
  288. ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
  289. ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
  290. ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
  291. ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
  292. }
  293. static u64 execlists_update_context(struct drm_i915_gem_request *rq)
  294. {
  295. struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
  296. struct i915_hw_ppgtt *ppgtt =
  297. rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
  298. u32 *reg_state = ce->lrc_reg_state;
  299. reg_state[CTX_RING_TAIL+1] = rq->tail;
  300. /* True 32b PPGTT with dynamic page allocation: update PDP
  301. * registers and point the unallocated PDPs to scratch page.
  302. * PML4 is allocated during ppgtt init, so this is not needed
  303. * in 48-bit mode.
  304. */
  305. if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
  306. execlists_update_context_pdps(ppgtt, reg_state);
  307. return ce->lrc_desc;
  308. }
  309. static void execlists_submit_ports(struct intel_engine_cs *engine)
  310. {
  311. struct drm_i915_private *dev_priv = engine->i915;
  312. struct execlist_port *port = engine->execlist_port;
  313. u32 __iomem *elsp =
  314. dev_priv->regs + i915_mmio_reg_offset(RING_ELSP(engine));
  315. u64 desc[2];
  316. GEM_BUG_ON(port[0].count > 1);
  317. if (!port[0].count)
  318. execlists_context_status_change(port[0].request,
  319. INTEL_CONTEXT_SCHEDULE_IN);
  320. desc[0] = execlists_update_context(port[0].request);
  321. GEM_DEBUG_EXEC(port[0].context_id = upper_32_bits(desc[0]));
  322. port[0].count++;
  323. if (port[1].request) {
  324. GEM_BUG_ON(port[1].count);
  325. execlists_context_status_change(port[1].request,
  326. INTEL_CONTEXT_SCHEDULE_IN);
  327. desc[1] = execlists_update_context(port[1].request);
  328. GEM_DEBUG_EXEC(port[1].context_id = upper_32_bits(desc[1]));
  329. port[1].count = 1;
  330. } else {
  331. desc[1] = 0;
  332. }
  333. GEM_BUG_ON(desc[0] == desc[1]);
  334. /* You must always write both descriptors in the order below. */
  335. writel(upper_32_bits(desc[1]), elsp);
  336. writel(lower_32_bits(desc[1]), elsp);
  337. writel(upper_32_bits(desc[0]), elsp);
  338. /* The context is automatically loaded after the following */
  339. writel(lower_32_bits(desc[0]), elsp);
  340. }
  341. static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
  342. {
  343. return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
  344. i915_gem_context_force_single_submission(ctx));
  345. }
  346. static bool can_merge_ctx(const struct i915_gem_context *prev,
  347. const struct i915_gem_context *next)
  348. {
  349. if (prev != next)
  350. return false;
  351. if (ctx_single_port_submission(prev))
  352. return false;
  353. return true;
  354. }
  355. static void execlists_dequeue(struct intel_engine_cs *engine)
  356. {
  357. struct drm_i915_gem_request *last;
  358. struct execlist_port *port = engine->execlist_port;
  359. unsigned long flags;
  360. struct rb_node *rb;
  361. bool submit = false;
  362. last = port->request;
  363. if (last)
  364. /* WaIdleLiteRestore:bdw,skl
  365. * Apply the wa NOOPs to prevent ring:HEAD == req:TAIL
  366. * as we resubmit the request. See gen8_emit_breadcrumb()
  367. * for where we prepare the padding after the end of the
  368. * request.
  369. */
  370. last->tail = last->wa_tail;
  371. GEM_BUG_ON(port[1].request);
  372. /* Hardware submission is through 2 ports. Conceptually each port
  373. * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
  374. * static for a context, and unique to each, so we only execute
  375. * requests belonging to a single context from each ring. RING_HEAD
  376. * is maintained by the CS in the context image, it marks the place
  377. * where it got up to last time, and through RING_TAIL we tell the CS
  378. * where we want to execute up to this time.
  379. *
  380. * In this list the requests are in order of execution. Consecutive
  381. * requests from the same context are adjacent in the ringbuffer. We
  382. * can combine these requests into a single RING_TAIL update:
  383. *
  384. * RING_HEAD...req1...req2
  385. * ^- RING_TAIL
  386. * since to execute req2 the CS must first execute req1.
  387. *
  388. * Our goal then is to point each port to the end of a consecutive
  389. * sequence of requests as being the most optimal (fewest wake ups
  390. * and context switches) submission.
  391. */
  392. spin_lock_irqsave(&engine->timeline->lock, flags);
  393. rb = engine->execlist_first;
  394. while (rb) {
  395. struct drm_i915_gem_request *cursor =
  396. rb_entry(rb, typeof(*cursor), priotree.node);
  397. /* Can we combine this request with the current port? It has to
  398. * be the same context/ringbuffer and not have any exceptions
  399. * (e.g. GVT saying never to combine contexts).
  400. *
  401. * If we can combine the requests, we can execute both by
  402. * updating the RING_TAIL to point to the end of the second
  403. * request, and so we never need to tell the hardware about
  404. * the first.
  405. */
  406. if (last && !can_merge_ctx(cursor->ctx, last->ctx)) {
  407. /* If we are on the second port and cannot combine
  408. * this request with the last, then we are done.
  409. */
  410. if (port != engine->execlist_port)
  411. break;
  412. /* If GVT overrides us we only ever submit port[0],
  413. * leaving port[1] empty. Note that we also have
  414. * to be careful that we don't queue the same
  415. * context (even though a different request) to
  416. * the second port.
  417. */
  418. if (ctx_single_port_submission(last->ctx) ||
  419. ctx_single_port_submission(cursor->ctx))
  420. break;
  421. GEM_BUG_ON(last->ctx == cursor->ctx);
  422. i915_gem_request_assign(&port->request, last);
  423. port++;
  424. }
  425. rb = rb_next(rb);
  426. rb_erase(&cursor->priotree.node, &engine->execlist_queue);
  427. RB_CLEAR_NODE(&cursor->priotree.node);
  428. cursor->priotree.priority = INT_MAX;
  429. __i915_gem_request_submit(cursor);
  430. last = cursor;
  431. submit = true;
  432. }
  433. if (submit) {
  434. i915_gem_request_assign(&port->request, last);
  435. engine->execlist_first = rb;
  436. }
  437. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  438. if (submit)
  439. execlists_submit_ports(engine);
  440. }
  441. static bool execlists_elsp_idle(struct intel_engine_cs *engine)
  442. {
  443. return !engine->execlist_port[0].request;
  444. }
  445. /**
  446. * intel_execlists_idle() - Determine if all engine submission ports are idle
  447. * @dev_priv: i915 device private
  448. *
  449. * Return true if there are no requests pending on any of the submission ports
  450. * of any engines.
  451. */
  452. bool intel_execlists_idle(struct drm_i915_private *dev_priv)
  453. {
  454. struct intel_engine_cs *engine;
  455. enum intel_engine_id id;
  456. if (!i915.enable_execlists)
  457. return true;
  458. for_each_engine(engine, dev_priv, id) {
  459. /* Interrupt/tasklet pending? */
  460. if (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted))
  461. return false;
  462. /* Both ports drained, no more ELSP submission? */
  463. if (!execlists_elsp_idle(engine))
  464. return false;
  465. }
  466. return true;
  467. }
  468. static bool execlists_elsp_ready(const struct intel_engine_cs *engine)
  469. {
  470. const struct execlist_port *port = engine->execlist_port;
  471. return port[0].count + port[1].count < 2;
  472. }
  473. /*
  474. * Check the unread Context Status Buffers and manage the submission of new
  475. * contexts to the ELSP accordingly.
  476. */
  477. static void intel_lrc_irq_handler(unsigned long data)
  478. {
  479. struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
  480. struct execlist_port *port = engine->execlist_port;
  481. struct drm_i915_private *dev_priv = engine->i915;
  482. intel_uncore_forcewake_get(dev_priv, engine->fw_domains);
  483. while (test_and_clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
  484. u32 __iomem *csb_mmio =
  485. dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine));
  486. u32 __iomem *buf =
  487. dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0));
  488. unsigned int csb, head, tail;
  489. csb = readl(csb_mmio);
  490. head = GEN8_CSB_READ_PTR(csb);
  491. tail = GEN8_CSB_WRITE_PTR(csb);
  492. if (head == tail)
  493. break;
  494. if (tail < head)
  495. tail += GEN8_CSB_ENTRIES;
  496. do {
  497. unsigned int idx = ++head % GEN8_CSB_ENTRIES;
  498. unsigned int status = readl(buf + 2 * idx);
  499. /* We are flying near dragons again.
  500. *
  501. * We hold a reference to the request in execlist_port[]
  502. * but no more than that. We are operating in softirq
  503. * context and so cannot hold any mutex or sleep. That
  504. * prevents us stopping the requests we are processing
  505. * in port[] from being retired simultaneously (the
  506. * breadcrumb will be complete before we see the
  507. * context-switch). As we only hold the reference to the
  508. * request, any pointer chasing underneath the request
  509. * is subject to a potential use-after-free. Thus we
  510. * store all of the bookkeeping within port[] as
  511. * required, and avoid using unguarded pointers beneath
  512. * request itself. The same applies to the atomic
  513. * status notifier.
  514. */
  515. if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
  516. continue;
  517. /* Check the context/desc id for this event matches */
  518. GEM_DEBUG_BUG_ON(readl(buf + 2 * idx + 1) !=
  519. port[0].context_id);
  520. GEM_BUG_ON(port[0].count == 0);
  521. if (--port[0].count == 0) {
  522. GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
  523. execlists_context_status_change(port[0].request,
  524. INTEL_CONTEXT_SCHEDULE_OUT);
  525. i915_gem_request_put(port[0].request);
  526. port[0] = port[1];
  527. memset(&port[1], 0, sizeof(port[1]));
  528. }
  529. GEM_BUG_ON(port[0].count == 0 &&
  530. !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
  531. } while (head < tail);
  532. writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK,
  533. GEN8_CSB_WRITE_PTR(csb) << 8),
  534. csb_mmio);
  535. }
  536. if (execlists_elsp_ready(engine))
  537. execlists_dequeue(engine);
  538. intel_uncore_forcewake_put(dev_priv, engine->fw_domains);
  539. }
  540. static bool insert_request(struct i915_priotree *pt, struct rb_root *root)
  541. {
  542. struct rb_node **p, *rb;
  543. bool first = true;
  544. /* most positive priority is scheduled first, equal priorities fifo */
  545. rb = NULL;
  546. p = &root->rb_node;
  547. while (*p) {
  548. struct i915_priotree *pos;
  549. rb = *p;
  550. pos = rb_entry(rb, typeof(*pos), node);
  551. if (pt->priority > pos->priority) {
  552. p = &rb->rb_left;
  553. } else {
  554. p = &rb->rb_right;
  555. first = false;
  556. }
  557. }
  558. rb_link_node(&pt->node, rb, p);
  559. rb_insert_color(&pt->node, root);
  560. return first;
  561. }
  562. static void execlists_submit_request(struct drm_i915_gem_request *request)
  563. {
  564. struct intel_engine_cs *engine = request->engine;
  565. unsigned long flags;
  566. /* Will be called from irq-context when using foreign fences. */
  567. spin_lock_irqsave(&engine->timeline->lock, flags);
  568. if (insert_request(&request->priotree, &engine->execlist_queue)) {
  569. engine->execlist_first = &request->priotree.node;
  570. if (execlists_elsp_ready(engine))
  571. tasklet_hi_schedule(&engine->irq_tasklet);
  572. }
  573. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  574. }
  575. static struct intel_engine_cs *
  576. pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
  577. {
  578. struct intel_engine_cs *engine;
  579. engine = container_of(pt,
  580. struct drm_i915_gem_request,
  581. priotree)->engine;
  582. if (engine != locked) {
  583. if (locked)
  584. spin_unlock_irq(&locked->timeline->lock);
  585. spin_lock_irq(&engine->timeline->lock);
  586. }
  587. return engine;
  588. }
  589. static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
  590. {
  591. struct intel_engine_cs *engine = NULL;
  592. struct i915_dependency *dep, *p;
  593. struct i915_dependency stack;
  594. LIST_HEAD(dfs);
  595. if (prio <= READ_ONCE(request->priotree.priority))
  596. return;
  597. /* Need BKL in order to use the temporary link inside i915_dependency */
  598. lockdep_assert_held(&request->i915->drm.struct_mutex);
  599. stack.signaler = &request->priotree;
  600. list_add(&stack.dfs_link, &dfs);
  601. /* Recursively bump all dependent priorities to match the new request.
  602. *
  603. * A naive approach would be to use recursion:
  604. * static void update_priorities(struct i915_priotree *pt, prio) {
  605. * list_for_each_entry(dep, &pt->signalers_list, signal_link)
  606. * update_priorities(dep->signal, prio)
  607. * insert_request(pt);
  608. * }
  609. * but that may have unlimited recursion depth and so runs a very
  610. * real risk of overunning the kernel stack. Instead, we build
  611. * a flat list of all dependencies starting with the current request.
  612. * As we walk the list of dependencies, we add all of its dependencies
  613. * to the end of the list (this may include an already visited
  614. * request) and continue to walk onwards onto the new dependencies. The
  615. * end result is a topological list of requests in reverse order, the
  616. * last element in the list is the request we must execute first.
  617. */
  618. list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
  619. struct i915_priotree *pt = dep->signaler;
  620. list_for_each_entry(p, &pt->signalers_list, signal_link)
  621. if (prio > READ_ONCE(p->signaler->priority))
  622. list_move_tail(&p->dfs_link, &dfs);
  623. list_safe_reset_next(dep, p, dfs_link);
  624. if (!RB_EMPTY_NODE(&pt->node))
  625. continue;
  626. engine = pt_lock_engine(pt, engine);
  627. /* If it is not already in the rbtree, we can update the
  628. * priority inplace and skip over it (and its dependencies)
  629. * if it is referenced *again* as we descend the dfs.
  630. */
  631. if (prio > pt->priority && RB_EMPTY_NODE(&pt->node)) {
  632. pt->priority = prio;
  633. list_del_init(&dep->dfs_link);
  634. }
  635. }
  636. /* Fifo and depth-first replacement ensure our deps execute before us */
  637. list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
  638. struct i915_priotree *pt = dep->signaler;
  639. INIT_LIST_HEAD(&dep->dfs_link);
  640. engine = pt_lock_engine(pt, engine);
  641. if (prio <= pt->priority)
  642. continue;
  643. GEM_BUG_ON(RB_EMPTY_NODE(&pt->node));
  644. pt->priority = prio;
  645. rb_erase(&pt->node, &engine->execlist_queue);
  646. if (insert_request(pt, &engine->execlist_queue))
  647. engine->execlist_first = &pt->node;
  648. }
  649. if (engine)
  650. spin_unlock_irq(&engine->timeline->lock);
  651. /* XXX Do we need to preempt to make room for us and our deps? */
  652. }
  653. static int execlists_context_pin(struct intel_engine_cs *engine,
  654. struct i915_gem_context *ctx)
  655. {
  656. struct intel_context *ce = &ctx->engine[engine->id];
  657. unsigned int flags;
  658. void *vaddr;
  659. int ret;
  660. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  661. if (ce->pin_count++)
  662. return 0;
  663. if (!ce->state) {
  664. ret = execlists_context_deferred_alloc(ctx, engine);
  665. if (ret)
  666. goto err;
  667. }
  668. GEM_BUG_ON(!ce->state);
  669. flags = PIN_GLOBAL | PIN_HIGH;
  670. if (ctx->ggtt_offset_bias)
  671. flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
  672. ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN, flags);
  673. if (ret)
  674. goto err;
  675. vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
  676. if (IS_ERR(vaddr)) {
  677. ret = PTR_ERR(vaddr);
  678. goto unpin_vma;
  679. }
  680. ret = intel_ring_pin(ce->ring, ctx->ggtt_offset_bias);
  681. if (ret)
  682. goto unpin_map;
  683. intel_lr_context_descriptor_update(ctx, engine);
  684. ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  685. ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
  686. i915_ggtt_offset(ce->ring->vma);
  687. ce->state->obj->mm.dirty = true;
  688. i915_gem_context_get(ctx);
  689. return 0;
  690. unpin_map:
  691. i915_gem_object_unpin_map(ce->state->obj);
  692. unpin_vma:
  693. __i915_vma_unpin(ce->state);
  694. err:
  695. ce->pin_count = 0;
  696. return ret;
  697. }
  698. static void execlists_context_unpin(struct intel_engine_cs *engine,
  699. struct i915_gem_context *ctx)
  700. {
  701. struct intel_context *ce = &ctx->engine[engine->id];
  702. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  703. GEM_BUG_ON(ce->pin_count == 0);
  704. if (--ce->pin_count)
  705. return;
  706. intel_ring_unpin(ce->ring);
  707. i915_gem_object_unpin_map(ce->state->obj);
  708. i915_vma_unpin(ce->state);
  709. i915_gem_context_put(ctx);
  710. }
  711. static int execlists_request_alloc(struct drm_i915_gem_request *request)
  712. {
  713. struct intel_engine_cs *engine = request->engine;
  714. struct intel_context *ce = &request->ctx->engine[engine->id];
  715. u32 *cs;
  716. int ret;
  717. GEM_BUG_ON(!ce->pin_count);
  718. /* Flush enough space to reduce the likelihood of waiting after
  719. * we start building the request - in which case we will just
  720. * have to repeat work.
  721. */
  722. request->reserved_space += EXECLISTS_REQUEST_SIZE;
  723. GEM_BUG_ON(!ce->ring);
  724. request->ring = ce->ring;
  725. if (i915.enable_guc_submission) {
  726. /*
  727. * Check that the GuC has space for the request before
  728. * going any further, as the i915_add_request() call
  729. * later on mustn't fail ...
  730. */
  731. ret = i915_guc_wq_reserve(request);
  732. if (ret)
  733. goto err;
  734. }
  735. cs = intel_ring_begin(request, 0);
  736. if (IS_ERR(cs)) {
  737. ret = PTR_ERR(cs);
  738. goto err_unreserve;
  739. }
  740. if (!ce->initialised) {
  741. ret = engine->init_context(request);
  742. if (ret)
  743. goto err_unreserve;
  744. ce->initialised = true;
  745. }
  746. /* Note that after this point, we have committed to using
  747. * this request as it is being used to both track the
  748. * state of engine initialisation and liveness of the
  749. * golden renderstate above. Think twice before you try
  750. * to cancel/unwind this request now.
  751. */
  752. request->reserved_space -= EXECLISTS_REQUEST_SIZE;
  753. return 0;
  754. err_unreserve:
  755. if (i915.enable_guc_submission)
  756. i915_guc_wq_unreserve(request);
  757. err:
  758. return ret;
  759. }
  760. #define wa_ctx_emit(batch, index, cmd) \
  761. do { \
  762. int __index = (index)++; \
  763. if (WARN_ON(__index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
  764. return -ENOSPC; \
  765. } \
  766. batch[__index] = (cmd); \
  767. } while (0)
  768. #define wa_ctx_emit_reg(batch, index, reg) \
  769. wa_ctx_emit((batch), (index), i915_mmio_reg_offset(reg))
  770. /*
  771. * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
  772. * PIPE_CONTROL instruction. This is required for the flush to happen correctly
  773. * but there is a slight complication as this is applied in WA batch where the
  774. * values are only initialized once so we cannot take register value at the
  775. * beginning and reuse it further; hence we save its value to memory, upload a
  776. * constant value with bit21 set and then we restore it back with the saved value.
  777. * To simplify the WA, a constant value is formed by using the default value
  778. * of this register. This shouldn't be a problem because we are only modifying
  779. * it for a short period and this batch in non-premptible. We can ofcourse
  780. * use additional instructions that read the actual value of the register
  781. * at that time and set our bit of interest but it makes the WA complicated.
  782. *
  783. * This WA is also required for Gen9 so extracting as a function avoids
  784. * code duplication.
  785. */
  786. static inline int gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine,
  787. uint32_t *batch,
  788. uint32_t index)
  789. {
  790. uint32_t l3sqc4_flush = (0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES);
  791. wa_ctx_emit(batch, index, (MI_STORE_REGISTER_MEM_GEN8 |
  792. MI_SRM_LRM_GLOBAL_GTT));
  793. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  794. wa_ctx_emit(batch, index, i915_ggtt_offset(engine->scratch) + 256);
  795. wa_ctx_emit(batch, index, 0);
  796. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  797. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  798. wa_ctx_emit(batch, index, l3sqc4_flush);
  799. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  800. wa_ctx_emit(batch, index, (PIPE_CONTROL_CS_STALL |
  801. PIPE_CONTROL_DC_FLUSH_ENABLE));
  802. wa_ctx_emit(batch, index, 0);
  803. wa_ctx_emit(batch, index, 0);
  804. wa_ctx_emit(batch, index, 0);
  805. wa_ctx_emit(batch, index, 0);
  806. wa_ctx_emit(batch, index, (MI_LOAD_REGISTER_MEM_GEN8 |
  807. MI_SRM_LRM_GLOBAL_GTT));
  808. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  809. wa_ctx_emit(batch, index, i915_ggtt_offset(engine->scratch) + 256);
  810. wa_ctx_emit(batch, index, 0);
  811. return index;
  812. }
  813. static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
  814. uint32_t offset,
  815. uint32_t start_alignment)
  816. {
  817. return wa_ctx->offset = ALIGN(offset, start_alignment);
  818. }
  819. static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
  820. uint32_t offset,
  821. uint32_t size_alignment)
  822. {
  823. wa_ctx->size = offset - wa_ctx->offset;
  824. WARN(wa_ctx->size % size_alignment,
  825. "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
  826. wa_ctx->size, size_alignment);
  827. return 0;
  828. }
  829. /*
  830. * Typically we only have one indirect_ctx and per_ctx batch buffer which are
  831. * initialized at the beginning and shared across all contexts but this field
  832. * helps us to have multiple batches at different offsets and select them based
  833. * on a criteria. At the moment this batch always start at the beginning of the page
  834. * and at this point we don't have multiple wa_ctx batch buffers.
  835. *
  836. * The number of WA applied are not known at the beginning; we use this field
  837. * to return the no of DWORDS written.
  838. *
  839. * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
  840. * so it adds NOOPs as padding to make it cacheline aligned.
  841. * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
  842. * makes a complete batch buffer.
  843. */
  844. static int gen8_init_indirectctx_bb(struct intel_engine_cs *engine,
  845. struct i915_wa_ctx_bb *wa_ctx,
  846. uint32_t *batch,
  847. uint32_t *offset)
  848. {
  849. uint32_t scratch_addr;
  850. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  851. /* WaDisableCtxRestoreArbitration:bdw,chv */
  852. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  853. /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
  854. if (IS_BROADWELL(engine->i915)) {
  855. int rc = gen8_emit_flush_coherentl3_wa(engine, batch, index);
  856. if (rc < 0)
  857. return rc;
  858. index = rc;
  859. }
  860. /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
  861. /* Actual scratch location is at 128 bytes offset */
  862. scratch_addr = i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  863. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  864. wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
  865. PIPE_CONTROL_GLOBAL_GTT_IVB |
  866. PIPE_CONTROL_CS_STALL |
  867. PIPE_CONTROL_QW_WRITE));
  868. wa_ctx_emit(batch, index, scratch_addr);
  869. wa_ctx_emit(batch, index, 0);
  870. wa_ctx_emit(batch, index, 0);
  871. wa_ctx_emit(batch, index, 0);
  872. /* Pad to end of cacheline */
  873. while (index % CACHELINE_DWORDS)
  874. wa_ctx_emit(batch, index, MI_NOOP);
  875. /*
  876. * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
  877. * execution depends on the length specified in terms of cache lines
  878. * in the register CTX_RCS_INDIRECT_CTX
  879. */
  880. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  881. }
  882. /*
  883. * This batch is started immediately after indirect_ctx batch. Since we ensure
  884. * that indirect_ctx ends on a cacheline this batch is aligned automatically.
  885. *
  886. * The number of DWORDS written are returned using this field.
  887. *
  888. * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
  889. * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
  890. */
  891. static int gen8_init_perctx_bb(struct intel_engine_cs *engine,
  892. struct i915_wa_ctx_bb *wa_ctx,
  893. uint32_t *batch,
  894. uint32_t *offset)
  895. {
  896. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  897. /* WaDisableCtxRestoreArbitration:bdw,chv */
  898. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  899. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  900. return wa_ctx_end(wa_ctx, *offset = index, 1);
  901. }
  902. static int gen9_init_indirectctx_bb(struct intel_engine_cs *engine,
  903. struct i915_wa_ctx_bb *wa_ctx,
  904. uint32_t *batch,
  905. uint32_t *offset)
  906. {
  907. int ret;
  908. struct drm_i915_private *dev_priv = engine->i915;
  909. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  910. /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
  911. ret = gen8_emit_flush_coherentl3_wa(engine, batch, index);
  912. if (ret < 0)
  913. return ret;
  914. index = ret;
  915. /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
  916. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  917. wa_ctx_emit_reg(batch, index, COMMON_SLICE_CHICKEN2);
  918. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(
  919. GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE));
  920. wa_ctx_emit(batch, index, MI_NOOP);
  921. /* WaClearSlmSpaceAtContextSwitch:kbl */
  922. /* Actual scratch location is at 128 bytes offset */
  923. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_A0)) {
  924. u32 scratch_addr =
  925. i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  926. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  927. wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
  928. PIPE_CONTROL_GLOBAL_GTT_IVB |
  929. PIPE_CONTROL_CS_STALL |
  930. PIPE_CONTROL_QW_WRITE));
  931. wa_ctx_emit(batch, index, scratch_addr);
  932. wa_ctx_emit(batch, index, 0);
  933. wa_ctx_emit(batch, index, 0);
  934. wa_ctx_emit(batch, index, 0);
  935. }
  936. /* WaMediaPoolStateCmdInWABB:bxt,glk */
  937. if (HAS_POOLED_EU(engine->i915)) {
  938. /*
  939. * EU pool configuration is setup along with golden context
  940. * during context initialization. This value depends on
  941. * device type (2x6 or 3x6) and needs to be updated based
  942. * on which subslice is disabled especially for 2x6
  943. * devices, however it is safe to load default
  944. * configuration of 3x6 device instead of masking off
  945. * corresponding bits because HW ignores bits of a disabled
  946. * subslice and drops down to appropriate config. Please
  947. * see render_state_setup() in i915_gem_render_state.c for
  948. * possible configurations, to avoid duplication they are
  949. * not shown here again.
  950. */
  951. u32 eu_pool_config = 0x00777000;
  952. wa_ctx_emit(batch, index, GEN9_MEDIA_POOL_STATE);
  953. wa_ctx_emit(batch, index, GEN9_MEDIA_POOL_ENABLE);
  954. wa_ctx_emit(batch, index, eu_pool_config);
  955. wa_ctx_emit(batch, index, 0);
  956. wa_ctx_emit(batch, index, 0);
  957. wa_ctx_emit(batch, index, 0);
  958. }
  959. /* Pad to end of cacheline */
  960. while (index % CACHELINE_DWORDS)
  961. wa_ctx_emit(batch, index, MI_NOOP);
  962. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  963. }
  964. static int gen9_init_perctx_bb(struct intel_engine_cs *engine,
  965. struct i915_wa_ctx_bb *wa_ctx,
  966. uint32_t *batch,
  967. uint32_t *offset)
  968. {
  969. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  970. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  971. return wa_ctx_end(wa_ctx, *offset = index, 1);
  972. }
  973. static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *engine, u32 size)
  974. {
  975. struct drm_i915_gem_object *obj;
  976. struct i915_vma *vma;
  977. int err;
  978. obj = i915_gem_object_create(engine->i915, PAGE_ALIGN(size));
  979. if (IS_ERR(obj))
  980. return PTR_ERR(obj);
  981. vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
  982. if (IS_ERR(vma)) {
  983. err = PTR_ERR(vma);
  984. goto err;
  985. }
  986. err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
  987. if (err)
  988. goto err;
  989. engine->wa_ctx.vma = vma;
  990. return 0;
  991. err:
  992. i915_gem_object_put(obj);
  993. return err;
  994. }
  995. static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *engine)
  996. {
  997. i915_vma_unpin_and_release(&engine->wa_ctx.vma);
  998. }
  999. static int intel_init_workaround_bb(struct intel_engine_cs *engine)
  1000. {
  1001. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1002. uint32_t *batch;
  1003. uint32_t offset;
  1004. struct page *page;
  1005. int ret;
  1006. WARN_ON(engine->id != RCS);
  1007. /* update this when WA for higher Gen are added */
  1008. if (INTEL_GEN(engine->i915) > 9) {
  1009. DRM_ERROR("WA batch buffer is not initialized for Gen%d\n",
  1010. INTEL_GEN(engine->i915));
  1011. return 0;
  1012. }
  1013. /* some WA perform writes to scratch page, ensure it is valid */
  1014. if (!engine->scratch) {
  1015. DRM_ERROR("scratch page not allocated for %s\n", engine->name);
  1016. return -EINVAL;
  1017. }
  1018. ret = lrc_setup_wa_ctx_obj(engine, PAGE_SIZE);
  1019. if (ret) {
  1020. DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
  1021. return ret;
  1022. }
  1023. page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
  1024. batch = kmap_atomic(page);
  1025. offset = 0;
  1026. if (IS_GEN8(engine->i915)) {
  1027. ret = gen8_init_indirectctx_bb(engine,
  1028. &wa_ctx->indirect_ctx,
  1029. batch,
  1030. &offset);
  1031. if (ret)
  1032. goto out;
  1033. ret = gen8_init_perctx_bb(engine,
  1034. &wa_ctx->per_ctx,
  1035. batch,
  1036. &offset);
  1037. if (ret)
  1038. goto out;
  1039. } else if (IS_GEN9(engine->i915)) {
  1040. ret = gen9_init_indirectctx_bb(engine,
  1041. &wa_ctx->indirect_ctx,
  1042. batch,
  1043. &offset);
  1044. if (ret)
  1045. goto out;
  1046. ret = gen9_init_perctx_bb(engine,
  1047. &wa_ctx->per_ctx,
  1048. batch,
  1049. &offset);
  1050. if (ret)
  1051. goto out;
  1052. }
  1053. out:
  1054. kunmap_atomic(batch);
  1055. if (ret)
  1056. lrc_destroy_wa_ctx_obj(engine);
  1057. return ret;
  1058. }
  1059. static u32 port_seqno(struct execlist_port *port)
  1060. {
  1061. return port->request ? port->request->global_seqno : 0;
  1062. }
  1063. static int gen8_init_common_ring(struct intel_engine_cs *engine)
  1064. {
  1065. struct drm_i915_private *dev_priv = engine->i915;
  1066. int ret;
  1067. ret = intel_mocs_init_engine(engine);
  1068. if (ret)
  1069. return ret;
  1070. intel_engine_reset_breadcrumbs(engine);
  1071. intel_engine_init_hangcheck(engine);
  1072. I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
  1073. I915_WRITE(RING_MODE_GEN7(engine),
  1074. _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
  1075. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  1076. I915_WRITE(RING_HWS_PGA(engine->mmio_base),
  1077. engine->status_page.ggtt_offset);
  1078. POSTING_READ(RING_HWS_PGA(engine->mmio_base));
  1079. DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
  1080. /* After a GPU reset, we may have requests to replay */
  1081. clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
  1082. if (!execlists_elsp_idle(engine)) {
  1083. DRM_DEBUG_DRIVER("Restarting %s from requests [0x%x, 0x%x]\n",
  1084. engine->name,
  1085. port_seqno(&engine->execlist_port[0]),
  1086. port_seqno(&engine->execlist_port[1]));
  1087. engine->execlist_port[0].count = 0;
  1088. engine->execlist_port[1].count = 0;
  1089. execlists_submit_ports(engine);
  1090. }
  1091. return 0;
  1092. }
  1093. static int gen8_init_render_ring(struct intel_engine_cs *engine)
  1094. {
  1095. struct drm_i915_private *dev_priv = engine->i915;
  1096. int ret;
  1097. ret = gen8_init_common_ring(engine);
  1098. if (ret)
  1099. return ret;
  1100. /* We need to disable the AsyncFlip performance optimisations in order
  1101. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  1102. * programmed to '1' on all products.
  1103. *
  1104. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  1105. */
  1106. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  1107. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  1108. return init_workarounds_ring(engine);
  1109. }
  1110. static int gen9_init_render_ring(struct intel_engine_cs *engine)
  1111. {
  1112. int ret;
  1113. ret = gen8_init_common_ring(engine);
  1114. if (ret)
  1115. return ret;
  1116. return init_workarounds_ring(engine);
  1117. }
  1118. static void reset_common_ring(struct intel_engine_cs *engine,
  1119. struct drm_i915_gem_request *request)
  1120. {
  1121. struct execlist_port *port = engine->execlist_port;
  1122. struct intel_context *ce;
  1123. /* If the request was innocent, we leave the request in the ELSP
  1124. * and will try to replay it on restarting. The context image may
  1125. * have been corrupted by the reset, in which case we may have
  1126. * to service a new GPU hang, but more likely we can continue on
  1127. * without impact.
  1128. *
  1129. * If the request was guilty, we presume the context is corrupt
  1130. * and have to at least restore the RING register in the context
  1131. * image back to the expected values to skip over the guilty request.
  1132. */
  1133. if (!request || request->fence.error != -EIO)
  1134. return;
  1135. /* We want a simple context + ring to execute the breadcrumb update.
  1136. * We cannot rely on the context being intact across the GPU hang,
  1137. * so clear it and rebuild just what we need for the breadcrumb.
  1138. * All pending requests for this context will be zapped, and any
  1139. * future request will be after userspace has had the opportunity
  1140. * to recreate its own state.
  1141. */
  1142. ce = &request->ctx->engine[engine->id];
  1143. execlists_init_reg_state(ce->lrc_reg_state,
  1144. request->ctx, engine, ce->ring);
  1145. /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
  1146. ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
  1147. i915_ggtt_offset(ce->ring->vma);
  1148. ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
  1149. request->ring->head = request->postfix;
  1150. request->ring->last_retired_head = -1;
  1151. intel_ring_update_space(request->ring);
  1152. if (i915.enable_guc_submission)
  1153. return;
  1154. /* Catch up with any missed context-switch interrupts */
  1155. if (request->ctx != port[0].request->ctx) {
  1156. i915_gem_request_put(port[0].request);
  1157. port[0] = port[1];
  1158. memset(&port[1], 0, sizeof(port[1]));
  1159. }
  1160. GEM_BUG_ON(request->ctx != port[0].request->ctx);
  1161. /* Reset WaIdleLiteRestore:bdw,skl as well */
  1162. request->tail = request->wa_tail - WA_TAIL_DWORDS * sizeof(u32);
  1163. }
  1164. static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
  1165. {
  1166. struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
  1167. struct intel_engine_cs *engine = req->engine;
  1168. const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
  1169. u32 *cs;
  1170. int i;
  1171. cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
  1172. if (IS_ERR(cs))
  1173. return PTR_ERR(cs);
  1174. *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
  1175. for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
  1176. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  1177. *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
  1178. *cs++ = upper_32_bits(pd_daddr);
  1179. *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
  1180. *cs++ = lower_32_bits(pd_daddr);
  1181. }
  1182. *cs++ = MI_NOOP;
  1183. intel_ring_advance(req, cs);
  1184. return 0;
  1185. }
  1186. static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
  1187. u64 offset, u32 len,
  1188. unsigned int dispatch_flags)
  1189. {
  1190. bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
  1191. u32 *cs;
  1192. int ret;
  1193. /* Don't rely in hw updating PDPs, specially in lite-restore.
  1194. * Ideally, we should set Force PD Restore in ctx descriptor,
  1195. * but we can't. Force Restore would be a second option, but
  1196. * it is unsafe in case of lite-restore (because the ctx is
  1197. * not idle). PML4 is allocated during ppgtt init so this is
  1198. * not needed in 48-bit.*/
  1199. if (req->ctx->ppgtt &&
  1200. (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings)) {
  1201. if (!i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
  1202. !intel_vgpu_active(req->i915)) {
  1203. ret = intel_logical_ring_emit_pdps(req);
  1204. if (ret)
  1205. return ret;
  1206. }
  1207. req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
  1208. }
  1209. cs = intel_ring_begin(req, 4);
  1210. if (IS_ERR(cs))
  1211. return PTR_ERR(cs);
  1212. /* FIXME(BDW): Address space and security selectors. */
  1213. *cs++ = MI_BATCH_BUFFER_START_GEN8 | (ppgtt << 8) | (dispatch_flags &
  1214. I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
  1215. *cs++ = lower_32_bits(offset);
  1216. *cs++ = upper_32_bits(offset);
  1217. *cs++ = MI_NOOP;
  1218. intel_ring_advance(req, cs);
  1219. return 0;
  1220. }
  1221. static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
  1222. {
  1223. struct drm_i915_private *dev_priv = engine->i915;
  1224. I915_WRITE_IMR(engine,
  1225. ~(engine->irq_enable_mask | engine->irq_keep_mask));
  1226. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1227. }
  1228. static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
  1229. {
  1230. struct drm_i915_private *dev_priv = engine->i915;
  1231. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  1232. }
  1233. static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
  1234. {
  1235. u32 cmd, *cs;
  1236. cs = intel_ring_begin(request, 4);
  1237. if (IS_ERR(cs))
  1238. return PTR_ERR(cs);
  1239. cmd = MI_FLUSH_DW + 1;
  1240. /* We always require a command barrier so that subsequent
  1241. * commands, such as breadcrumb interrupts, are strictly ordered
  1242. * wrt the contents of the write cache being flushed to memory
  1243. * (and thus being coherent from the CPU).
  1244. */
  1245. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1246. if (mode & EMIT_INVALIDATE) {
  1247. cmd |= MI_INVALIDATE_TLB;
  1248. if (request->engine->id == VCS)
  1249. cmd |= MI_INVALIDATE_BSD;
  1250. }
  1251. *cs++ = cmd;
  1252. *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
  1253. *cs++ = 0; /* upper addr */
  1254. *cs++ = 0; /* value */
  1255. intel_ring_advance(request, cs);
  1256. return 0;
  1257. }
  1258. static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
  1259. u32 mode)
  1260. {
  1261. struct intel_engine_cs *engine = request->engine;
  1262. u32 scratch_addr =
  1263. i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  1264. bool vf_flush_wa = false, dc_flush_wa = false;
  1265. u32 *cs, flags = 0;
  1266. int len;
  1267. flags |= PIPE_CONTROL_CS_STALL;
  1268. if (mode & EMIT_FLUSH) {
  1269. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  1270. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  1271. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  1272. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  1273. }
  1274. if (mode & EMIT_INVALIDATE) {
  1275. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  1276. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  1277. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  1278. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1279. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  1280. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  1281. flags |= PIPE_CONTROL_QW_WRITE;
  1282. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1283. /*
  1284. * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
  1285. * pipe control.
  1286. */
  1287. if (IS_GEN9(request->i915))
  1288. vf_flush_wa = true;
  1289. /* WaForGAMHang:kbl */
  1290. if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
  1291. dc_flush_wa = true;
  1292. }
  1293. len = 6;
  1294. if (vf_flush_wa)
  1295. len += 6;
  1296. if (dc_flush_wa)
  1297. len += 12;
  1298. cs = intel_ring_begin(request, len);
  1299. if (IS_ERR(cs))
  1300. return PTR_ERR(cs);
  1301. if (vf_flush_wa) {
  1302. *cs++ = GFX_OP_PIPE_CONTROL(6);
  1303. *cs++ = 0;
  1304. *cs++ = 0;
  1305. *cs++ = 0;
  1306. *cs++ = 0;
  1307. *cs++ = 0;
  1308. }
  1309. if (dc_flush_wa) {
  1310. *cs++ = GFX_OP_PIPE_CONTROL(6);
  1311. *cs++ = PIPE_CONTROL_DC_FLUSH_ENABLE;
  1312. *cs++ = 0;
  1313. *cs++ = 0;
  1314. *cs++ = 0;
  1315. *cs++ = 0;
  1316. }
  1317. *cs++ = GFX_OP_PIPE_CONTROL(6);
  1318. *cs++ = flags;
  1319. *cs++ = scratch_addr;
  1320. *cs++ = 0;
  1321. *cs++ = 0;
  1322. *cs++ = 0;
  1323. if (dc_flush_wa) {
  1324. *cs++ = GFX_OP_PIPE_CONTROL(6);
  1325. *cs++ = PIPE_CONTROL_CS_STALL;
  1326. *cs++ = 0;
  1327. *cs++ = 0;
  1328. *cs++ = 0;
  1329. *cs++ = 0;
  1330. }
  1331. intel_ring_advance(request, cs);
  1332. return 0;
  1333. }
  1334. /*
  1335. * Reserve space for 2 NOOPs at the end of each request to be
  1336. * used as a workaround for not being allowed to do lite
  1337. * restore with HEAD==TAIL (WaIdleLiteRestore).
  1338. */
  1339. static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
  1340. {
  1341. *cs++ = MI_NOOP;
  1342. *cs++ = MI_NOOP;
  1343. request->wa_tail = intel_ring_offset(request, cs);
  1344. }
  1345. static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
  1346. {
  1347. /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
  1348. BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
  1349. *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
  1350. *cs++ = intel_hws_seqno_address(request->engine) | MI_FLUSH_DW_USE_GTT;
  1351. *cs++ = 0;
  1352. *cs++ = request->global_seqno;
  1353. *cs++ = MI_USER_INTERRUPT;
  1354. *cs++ = MI_NOOP;
  1355. request->tail = intel_ring_offset(request, cs);
  1356. gen8_emit_wa_tail(request, cs);
  1357. }
  1358. static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
  1359. static void gen8_emit_breadcrumb_render(struct drm_i915_gem_request *request,
  1360. u32 *cs)
  1361. {
  1362. /* We're using qword write, seqno should be aligned to 8 bytes. */
  1363. BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
  1364. /* w/a for post sync ops following a GPGPU operation we
  1365. * need a prior CS_STALL, which is emitted by the flush
  1366. * following the batch.
  1367. */
  1368. *cs++ = GFX_OP_PIPE_CONTROL(6);
  1369. *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
  1370. PIPE_CONTROL_QW_WRITE;
  1371. *cs++ = intel_hws_seqno_address(request->engine);
  1372. *cs++ = 0;
  1373. *cs++ = request->global_seqno;
  1374. /* We're thrashing one dword of HWS. */
  1375. *cs++ = 0;
  1376. *cs++ = MI_USER_INTERRUPT;
  1377. *cs++ = MI_NOOP;
  1378. request->tail = intel_ring_offset(request, cs);
  1379. gen8_emit_wa_tail(request, cs);
  1380. }
  1381. static const int gen8_emit_breadcrumb_render_sz = 8 + WA_TAIL_DWORDS;
  1382. static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
  1383. {
  1384. int ret;
  1385. ret = intel_ring_workarounds_emit(req);
  1386. if (ret)
  1387. return ret;
  1388. ret = intel_rcs_context_init_mocs(req);
  1389. /*
  1390. * Failing to program the MOCS is non-fatal.The system will not
  1391. * run at peak performance. So generate an error and carry on.
  1392. */
  1393. if (ret)
  1394. DRM_ERROR("MOCS failed to program: expect performance issues.\n");
  1395. return i915_gem_render_state_emit(req);
  1396. }
  1397. /**
  1398. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1399. * @engine: Engine Command Streamer.
  1400. */
  1401. void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
  1402. {
  1403. struct drm_i915_private *dev_priv;
  1404. /*
  1405. * Tasklet cannot be active at this point due intel_mark_active/idle
  1406. * so this is just for documentation.
  1407. */
  1408. if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->irq_tasklet.state)))
  1409. tasklet_kill(&engine->irq_tasklet);
  1410. dev_priv = engine->i915;
  1411. if (engine->buffer) {
  1412. WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1413. }
  1414. if (engine->cleanup)
  1415. engine->cleanup(engine);
  1416. if (engine->status_page.vma) {
  1417. i915_gem_object_unpin_map(engine->status_page.vma->obj);
  1418. engine->status_page.vma = NULL;
  1419. }
  1420. intel_engine_cleanup_common(engine);
  1421. lrc_destroy_wa_ctx_obj(engine);
  1422. engine->i915 = NULL;
  1423. dev_priv->engine[engine->id] = NULL;
  1424. kfree(engine);
  1425. }
  1426. void intel_execlists_enable_submission(struct drm_i915_private *dev_priv)
  1427. {
  1428. struct intel_engine_cs *engine;
  1429. enum intel_engine_id id;
  1430. for_each_engine(engine, dev_priv, id) {
  1431. engine->submit_request = execlists_submit_request;
  1432. engine->schedule = execlists_schedule;
  1433. }
  1434. }
  1435. static void
  1436. logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  1437. {
  1438. /* Default vfuncs which can be overriden by each engine. */
  1439. engine->init_hw = gen8_init_common_ring;
  1440. engine->reset_hw = reset_common_ring;
  1441. engine->context_pin = execlists_context_pin;
  1442. engine->context_unpin = execlists_context_unpin;
  1443. engine->request_alloc = execlists_request_alloc;
  1444. engine->emit_flush = gen8_emit_flush;
  1445. engine->emit_breadcrumb = gen8_emit_breadcrumb;
  1446. engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
  1447. engine->submit_request = execlists_submit_request;
  1448. engine->schedule = execlists_schedule;
  1449. engine->irq_enable = gen8_logical_ring_enable_irq;
  1450. engine->irq_disable = gen8_logical_ring_disable_irq;
  1451. engine->emit_bb_start = gen8_emit_bb_start;
  1452. }
  1453. static inline void
  1454. logical_ring_default_irqs(struct intel_engine_cs *engine)
  1455. {
  1456. unsigned shift = engine->irq_shift;
  1457. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
  1458. engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
  1459. }
  1460. static int
  1461. lrc_setup_hws(struct intel_engine_cs *engine, struct i915_vma *vma)
  1462. {
  1463. const int hws_offset = LRC_PPHWSP_PN * PAGE_SIZE;
  1464. void *hws;
  1465. /* The HWSP is part of the default context object in LRC mode. */
  1466. hws = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  1467. if (IS_ERR(hws))
  1468. return PTR_ERR(hws);
  1469. engine->status_page.page_addr = hws + hws_offset;
  1470. engine->status_page.ggtt_offset = i915_ggtt_offset(vma) + hws_offset;
  1471. engine->status_page.vma = vma;
  1472. return 0;
  1473. }
  1474. static void
  1475. logical_ring_setup(struct intel_engine_cs *engine)
  1476. {
  1477. struct drm_i915_private *dev_priv = engine->i915;
  1478. enum forcewake_domains fw_domains;
  1479. intel_engine_setup_common(engine);
  1480. /* Intentionally left blank. */
  1481. engine->buffer = NULL;
  1482. fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
  1483. RING_ELSP(engine),
  1484. FW_REG_WRITE);
  1485. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1486. RING_CONTEXT_STATUS_PTR(engine),
  1487. FW_REG_READ | FW_REG_WRITE);
  1488. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1489. RING_CONTEXT_STATUS_BUF_BASE(engine),
  1490. FW_REG_READ);
  1491. engine->fw_domains = fw_domains;
  1492. tasklet_init(&engine->irq_tasklet,
  1493. intel_lrc_irq_handler, (unsigned long)engine);
  1494. logical_ring_default_vfuncs(engine);
  1495. logical_ring_default_irqs(engine);
  1496. }
  1497. static int
  1498. logical_ring_init(struct intel_engine_cs *engine)
  1499. {
  1500. struct i915_gem_context *dctx = engine->i915->kernel_context;
  1501. int ret;
  1502. ret = intel_engine_init_common(engine);
  1503. if (ret)
  1504. goto error;
  1505. /* And setup the hardware status page. */
  1506. ret = lrc_setup_hws(engine, dctx->engine[engine->id].state);
  1507. if (ret) {
  1508. DRM_ERROR("Failed to set up hws %s: %d\n", engine->name, ret);
  1509. goto error;
  1510. }
  1511. return 0;
  1512. error:
  1513. intel_logical_ring_cleanup(engine);
  1514. return ret;
  1515. }
  1516. int logical_render_ring_init(struct intel_engine_cs *engine)
  1517. {
  1518. struct drm_i915_private *dev_priv = engine->i915;
  1519. int ret;
  1520. logical_ring_setup(engine);
  1521. if (HAS_L3_DPF(dev_priv))
  1522. engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1523. /* Override some for render ring. */
  1524. if (INTEL_GEN(dev_priv) >= 9)
  1525. engine->init_hw = gen9_init_render_ring;
  1526. else
  1527. engine->init_hw = gen8_init_render_ring;
  1528. engine->init_context = gen8_init_rcs_context;
  1529. engine->emit_flush = gen8_emit_flush_render;
  1530. engine->emit_breadcrumb = gen8_emit_breadcrumb_render;
  1531. engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_render_sz;
  1532. ret = intel_engine_create_scratch(engine, PAGE_SIZE);
  1533. if (ret)
  1534. return ret;
  1535. ret = intel_init_workaround_bb(engine);
  1536. if (ret) {
  1537. /*
  1538. * We continue even if we fail to initialize WA batch
  1539. * because we only expect rare glitches but nothing
  1540. * critical to prevent us from using GPU
  1541. */
  1542. DRM_ERROR("WA batch buffer initialization failed: %d\n",
  1543. ret);
  1544. }
  1545. return logical_ring_init(engine);
  1546. }
  1547. int logical_xcs_ring_init(struct intel_engine_cs *engine)
  1548. {
  1549. logical_ring_setup(engine);
  1550. return logical_ring_init(engine);
  1551. }
  1552. static u32
  1553. make_rpcs(struct drm_i915_private *dev_priv)
  1554. {
  1555. u32 rpcs = 0;
  1556. /*
  1557. * No explicit RPCS request is needed to ensure full
  1558. * slice/subslice/EU enablement prior to Gen9.
  1559. */
  1560. if (INTEL_GEN(dev_priv) < 9)
  1561. return 0;
  1562. /*
  1563. * Starting in Gen9, render power gating can leave
  1564. * slice/subslice/EU in a partially enabled state. We
  1565. * must make an explicit request through RPCS for full
  1566. * enablement.
  1567. */
  1568. if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
  1569. rpcs |= GEN8_RPCS_S_CNT_ENABLE;
  1570. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
  1571. GEN8_RPCS_S_CNT_SHIFT;
  1572. rpcs |= GEN8_RPCS_ENABLE;
  1573. }
  1574. if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
  1575. rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
  1576. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
  1577. GEN8_RPCS_SS_CNT_SHIFT;
  1578. rpcs |= GEN8_RPCS_ENABLE;
  1579. }
  1580. if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
  1581. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  1582. GEN8_RPCS_EU_MIN_SHIFT;
  1583. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  1584. GEN8_RPCS_EU_MAX_SHIFT;
  1585. rpcs |= GEN8_RPCS_ENABLE;
  1586. }
  1587. return rpcs;
  1588. }
  1589. static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
  1590. {
  1591. u32 indirect_ctx_offset;
  1592. switch (INTEL_GEN(engine->i915)) {
  1593. default:
  1594. MISSING_CASE(INTEL_GEN(engine->i915));
  1595. /* fall through */
  1596. case 9:
  1597. indirect_ctx_offset =
  1598. GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1599. break;
  1600. case 8:
  1601. indirect_ctx_offset =
  1602. GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1603. break;
  1604. }
  1605. return indirect_ctx_offset;
  1606. }
  1607. static void execlists_init_reg_state(u32 *reg_state,
  1608. struct i915_gem_context *ctx,
  1609. struct intel_engine_cs *engine,
  1610. struct intel_ring *ring)
  1611. {
  1612. struct drm_i915_private *dev_priv = engine->i915;
  1613. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
  1614. /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
  1615. * commands followed by (reg, value) pairs. The values we are setting here are
  1616. * only for the first context restore: on a subsequent save, the GPU will
  1617. * recreate this batchbuffer with new values (including all the missing
  1618. * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
  1619. reg_state[CTX_LRI_HEADER_0] =
  1620. MI_LOAD_REGISTER_IMM(engine->id == RCS ? 14 : 11) | MI_LRI_FORCE_POSTED;
  1621. ASSIGN_CTX_REG(reg_state, CTX_CONTEXT_CONTROL,
  1622. RING_CONTEXT_CONTROL(engine),
  1623. _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
  1624. CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
  1625. (HAS_RESOURCE_STREAMER(dev_priv) ?
  1626. CTX_CTRL_RS_CTX_ENABLE : 0)));
  1627. ASSIGN_CTX_REG(reg_state, CTX_RING_HEAD, RING_HEAD(engine->mmio_base),
  1628. 0);
  1629. ASSIGN_CTX_REG(reg_state, CTX_RING_TAIL, RING_TAIL(engine->mmio_base),
  1630. 0);
  1631. ASSIGN_CTX_REG(reg_state, CTX_RING_BUFFER_START,
  1632. RING_START(engine->mmio_base), 0);
  1633. ASSIGN_CTX_REG(reg_state, CTX_RING_BUFFER_CONTROL,
  1634. RING_CTL(engine->mmio_base),
  1635. RING_CTL_SIZE(ring->size) | RING_VALID);
  1636. ASSIGN_CTX_REG(reg_state, CTX_BB_HEAD_U,
  1637. RING_BBADDR_UDW(engine->mmio_base), 0);
  1638. ASSIGN_CTX_REG(reg_state, CTX_BB_HEAD_L,
  1639. RING_BBADDR(engine->mmio_base), 0);
  1640. ASSIGN_CTX_REG(reg_state, CTX_BB_STATE,
  1641. RING_BBSTATE(engine->mmio_base),
  1642. RING_BB_PPGTT);
  1643. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_HEAD_U,
  1644. RING_SBBADDR_UDW(engine->mmio_base), 0);
  1645. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_HEAD_L,
  1646. RING_SBBADDR(engine->mmio_base), 0);
  1647. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_STATE,
  1648. RING_SBBSTATE(engine->mmio_base), 0);
  1649. if (engine->id == RCS) {
  1650. ASSIGN_CTX_REG(reg_state, CTX_BB_PER_CTX_PTR,
  1651. RING_BB_PER_CTX_PTR(engine->mmio_base), 0);
  1652. ASSIGN_CTX_REG(reg_state, CTX_RCS_INDIRECT_CTX,
  1653. RING_INDIRECT_CTX(engine->mmio_base), 0);
  1654. ASSIGN_CTX_REG(reg_state, CTX_RCS_INDIRECT_CTX_OFFSET,
  1655. RING_INDIRECT_CTX_OFFSET(engine->mmio_base), 0);
  1656. if (engine->wa_ctx.vma) {
  1657. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1658. u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
  1659. reg_state[CTX_RCS_INDIRECT_CTX+1] =
  1660. (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
  1661. (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
  1662. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
  1663. intel_lr_indirect_ctx_offset(engine) << 6;
  1664. reg_state[CTX_BB_PER_CTX_PTR+1] =
  1665. (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
  1666. 0x01;
  1667. }
  1668. }
  1669. reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
  1670. ASSIGN_CTX_REG(reg_state, CTX_CTX_TIMESTAMP,
  1671. RING_CTX_TIMESTAMP(engine->mmio_base), 0);
  1672. /* PDP values well be assigned later if needed */
  1673. ASSIGN_CTX_REG(reg_state, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3),
  1674. 0);
  1675. ASSIGN_CTX_REG(reg_state, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3),
  1676. 0);
  1677. ASSIGN_CTX_REG(reg_state, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2),
  1678. 0);
  1679. ASSIGN_CTX_REG(reg_state, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2),
  1680. 0);
  1681. ASSIGN_CTX_REG(reg_state, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1),
  1682. 0);
  1683. ASSIGN_CTX_REG(reg_state, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1),
  1684. 0);
  1685. ASSIGN_CTX_REG(reg_state, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0),
  1686. 0);
  1687. ASSIGN_CTX_REG(reg_state, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0),
  1688. 0);
  1689. if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
  1690. /* 64b PPGTT (48bit canonical)
  1691. * PDP0_DESCRIPTOR contains the base address to PML4 and
  1692. * other PDP Descriptors are ignored.
  1693. */
  1694. ASSIGN_CTX_PML4(ppgtt, reg_state);
  1695. }
  1696. if (engine->id == RCS) {
  1697. reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  1698. ASSIGN_CTX_REG(reg_state, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
  1699. make_rpcs(dev_priv));
  1700. }
  1701. }
  1702. static int
  1703. populate_lr_context(struct i915_gem_context *ctx,
  1704. struct drm_i915_gem_object *ctx_obj,
  1705. struct intel_engine_cs *engine,
  1706. struct intel_ring *ring)
  1707. {
  1708. void *vaddr;
  1709. int ret;
  1710. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1711. if (ret) {
  1712. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1713. return ret;
  1714. }
  1715. vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
  1716. if (IS_ERR(vaddr)) {
  1717. ret = PTR_ERR(vaddr);
  1718. DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
  1719. return ret;
  1720. }
  1721. ctx_obj->mm.dirty = true;
  1722. /* The second page of the context object contains some fields which must
  1723. * be set up prior to the first execution. */
  1724. execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
  1725. ctx, engine, ring);
  1726. i915_gem_object_unpin_map(ctx_obj);
  1727. return 0;
  1728. }
  1729. /**
  1730. * intel_lr_context_size() - return the size of the context for an engine
  1731. * @engine: which engine to find the context size for
  1732. *
  1733. * Each engine may require a different amount of space for a context image,
  1734. * so when allocating (or copying) an image, this function can be used to
  1735. * find the right size for the specific engine.
  1736. *
  1737. * Return: size (in bytes) of an engine-specific context image
  1738. *
  1739. * Note: this size includes the HWSP, which is part of the context image
  1740. * in LRC mode, but does not include the "shared data page" used with
  1741. * GuC submission. The caller should account for this if using the GuC.
  1742. */
  1743. uint32_t intel_lr_context_size(struct intel_engine_cs *engine)
  1744. {
  1745. int ret = 0;
  1746. WARN_ON(INTEL_GEN(engine->i915) < 8);
  1747. switch (engine->id) {
  1748. case RCS:
  1749. if (INTEL_GEN(engine->i915) >= 9)
  1750. ret = GEN9_LR_CONTEXT_RENDER_SIZE;
  1751. else
  1752. ret = GEN8_LR_CONTEXT_RENDER_SIZE;
  1753. break;
  1754. case VCS:
  1755. case BCS:
  1756. case VECS:
  1757. case VCS2:
  1758. ret = GEN8_LR_CONTEXT_OTHER_SIZE;
  1759. break;
  1760. }
  1761. return ret;
  1762. }
  1763. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  1764. struct intel_engine_cs *engine)
  1765. {
  1766. struct drm_i915_gem_object *ctx_obj;
  1767. struct intel_context *ce = &ctx->engine[engine->id];
  1768. struct i915_vma *vma;
  1769. uint32_t context_size;
  1770. struct intel_ring *ring;
  1771. int ret;
  1772. WARN_ON(ce->state);
  1773. context_size = round_up(intel_lr_context_size(engine),
  1774. I915_GTT_PAGE_SIZE);
  1775. /* One extra page as the sharing data between driver and GuC */
  1776. context_size += PAGE_SIZE * LRC_PPHWSP_PN;
  1777. ctx_obj = i915_gem_object_create(ctx->i915, context_size);
  1778. if (IS_ERR(ctx_obj)) {
  1779. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
  1780. return PTR_ERR(ctx_obj);
  1781. }
  1782. vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
  1783. if (IS_ERR(vma)) {
  1784. ret = PTR_ERR(vma);
  1785. goto error_deref_obj;
  1786. }
  1787. ring = intel_engine_create_ring(engine, ctx->ring_size);
  1788. if (IS_ERR(ring)) {
  1789. ret = PTR_ERR(ring);
  1790. goto error_deref_obj;
  1791. }
  1792. ret = populate_lr_context(ctx, ctx_obj, engine, ring);
  1793. if (ret) {
  1794. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  1795. goto error_ring_free;
  1796. }
  1797. ce->ring = ring;
  1798. ce->state = vma;
  1799. ce->initialised = engine->init_context == NULL;
  1800. return 0;
  1801. error_ring_free:
  1802. intel_ring_free(ring);
  1803. error_deref_obj:
  1804. i915_gem_object_put(ctx_obj);
  1805. return ret;
  1806. }
  1807. void intel_lr_context_resume(struct drm_i915_private *dev_priv)
  1808. {
  1809. struct intel_engine_cs *engine;
  1810. struct i915_gem_context *ctx;
  1811. enum intel_engine_id id;
  1812. /* Because we emit WA_TAIL_DWORDS there may be a disparity
  1813. * between our bookkeeping in ce->ring->head and ce->ring->tail and
  1814. * that stored in context. As we only write new commands from
  1815. * ce->ring->tail onwards, everything before that is junk. If the GPU
  1816. * starts reading from its RING_HEAD from the context, it may try to
  1817. * execute that junk and die.
  1818. *
  1819. * So to avoid that we reset the context images upon resume. For
  1820. * simplicity, we just zero everything out.
  1821. */
  1822. list_for_each_entry(ctx, &dev_priv->context_list, link) {
  1823. for_each_engine(engine, dev_priv, id) {
  1824. struct intel_context *ce = &ctx->engine[engine->id];
  1825. u32 *reg;
  1826. if (!ce->state)
  1827. continue;
  1828. reg = i915_gem_object_pin_map(ce->state->obj,
  1829. I915_MAP_WB);
  1830. if (WARN_ON(IS_ERR(reg)))
  1831. continue;
  1832. reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
  1833. reg[CTX_RING_HEAD+1] = 0;
  1834. reg[CTX_RING_TAIL+1] = 0;
  1835. ce->state->obj->mm.dirty = true;
  1836. i915_gem_object_unpin_map(ce->state->obj);
  1837. ce->ring->head = ce->ring->tail = 0;
  1838. ce->ring->last_retired_head = -1;
  1839. intel_ring_update_space(ce->ring);
  1840. }
  1841. }
  1842. }