intel_lrc.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387
  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 CTX_LRI_HEADER_0 0x01
  154. #define CTX_CONTEXT_CONTROL 0x02
  155. #define CTX_RING_HEAD 0x04
  156. #define CTX_RING_TAIL 0x06
  157. #define CTX_RING_BUFFER_START 0x08
  158. #define CTX_RING_BUFFER_CONTROL 0x0a
  159. #define CTX_BB_HEAD_U 0x0c
  160. #define CTX_BB_HEAD_L 0x0e
  161. #define CTX_BB_STATE 0x10
  162. #define CTX_SECOND_BB_HEAD_U 0x12
  163. #define CTX_SECOND_BB_HEAD_L 0x14
  164. #define CTX_SECOND_BB_STATE 0x16
  165. #define CTX_BB_PER_CTX_PTR 0x18
  166. #define CTX_RCS_INDIRECT_CTX 0x1a
  167. #define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
  168. #define CTX_LRI_HEADER_1 0x21
  169. #define CTX_CTX_TIMESTAMP 0x22
  170. #define CTX_PDP3_UDW 0x24
  171. #define CTX_PDP3_LDW 0x26
  172. #define CTX_PDP2_UDW 0x28
  173. #define CTX_PDP2_LDW 0x2a
  174. #define CTX_PDP1_UDW 0x2c
  175. #define CTX_PDP1_LDW 0x2e
  176. #define CTX_PDP0_UDW 0x30
  177. #define CTX_PDP0_LDW 0x32
  178. #define CTX_LRI_HEADER_2 0x41
  179. #define CTX_R_PWR_CLK_STATE 0x42
  180. #define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
  181. #define GEN8_CTX_VALID (1<<0)
  182. #define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
  183. #define GEN8_CTX_FORCE_RESTORE (1<<2)
  184. #define GEN8_CTX_L3LLC_COHERENT (1<<5)
  185. #define GEN8_CTX_PRIVILEGE (1<<8)
  186. #define ASSIGN_CTX_REG(reg_state, pos, reg, val) do { \
  187. (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
  188. (reg_state)[(pos)+1] = (val); \
  189. } while (0)
  190. #define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \
  191. const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
  192. reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
  193. reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
  194. } while (0)
  195. #define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
  196. reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \
  197. reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \
  198. } while (0)
  199. enum {
  200. FAULT_AND_HANG = 0,
  201. FAULT_AND_HALT, /* Debug only */
  202. FAULT_AND_STREAM,
  203. FAULT_AND_CONTINUE /* Unsupported */
  204. };
  205. #define GEN8_CTX_ID_SHIFT 32
  206. #define GEN8_CTX_ID_WIDTH 21
  207. #define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
  208. #define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26
  209. /* Typical size of the average request (2 pipecontrols and a MI_BB) */
  210. #define EXECLISTS_REQUEST_SIZE 64 /* bytes */
  211. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  212. struct intel_engine_cs *engine);
  213. static int intel_lr_context_pin(struct i915_gem_context *ctx,
  214. struct intel_engine_cs *engine);
  215. /**
  216. * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
  217. * @dev_priv: i915 device private
  218. * @enable_execlists: value of i915.enable_execlists module parameter.
  219. *
  220. * Only certain platforms support Execlists (the prerequisites being
  221. * support for Logical Ring Contexts and Aliasing PPGTT or better).
  222. *
  223. * Return: 1 if Execlists is supported and has to be enabled.
  224. */
  225. int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists)
  226. {
  227. /* On platforms with execlist available, vGPU will only
  228. * support execlist mode, no ring buffer mode.
  229. */
  230. if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv))
  231. return 1;
  232. if (INTEL_GEN(dev_priv) >= 9)
  233. return 1;
  234. if (enable_execlists == 0)
  235. return 0;
  236. if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) &&
  237. USES_PPGTT(dev_priv) &&
  238. i915.use_mmio_flip >= 0)
  239. return 1;
  240. return 0;
  241. }
  242. static void
  243. logical_ring_init_platform_invariants(struct intel_engine_cs *engine)
  244. {
  245. struct drm_i915_private *dev_priv = engine->i915;
  246. if (IS_GEN8(dev_priv) || IS_GEN9(dev_priv))
  247. engine->idle_lite_restore_wa = ~0;
  248. engine->disable_lite_restore_wa = (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
  249. IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) &&
  250. (engine->id == VCS || engine->id == VCS2);
  251. engine->ctx_desc_template = GEN8_CTX_VALID;
  252. if (IS_GEN8(dev_priv))
  253. engine->ctx_desc_template |= GEN8_CTX_L3LLC_COHERENT;
  254. engine->ctx_desc_template |= GEN8_CTX_PRIVILEGE;
  255. /* TODO: WaDisableLiteRestore when we start using semaphore
  256. * signalling between Command Streamers */
  257. /* ring->ctx_desc_template |= GEN8_CTX_FORCE_RESTORE; */
  258. /* WaEnableForceRestoreInCtxtDescForVCS:skl */
  259. /* WaEnableForceRestoreInCtxtDescForVCS:bxt */
  260. if (engine->disable_lite_restore_wa)
  261. engine->ctx_desc_template |= GEN8_CTX_FORCE_RESTORE;
  262. }
  263. /**
  264. * intel_lr_context_descriptor_update() - calculate & cache the descriptor
  265. * descriptor for a pinned context
  266. * @ctx: Context to work on
  267. * @engine: Engine the descriptor will be used with
  268. *
  269. * The context descriptor encodes various attributes of a context,
  270. * including its GTT address and some flags. Because it's fairly
  271. * expensive to calculate, we'll just do it once and cache the result,
  272. * which remains valid until the context is unpinned.
  273. *
  274. * This is what a descriptor looks like, from LSB to MSB::
  275. *
  276. * bits 0-11: flags, GEN8_CTX_* (cached in ctx_desc_template)
  277. * bits 12-31: LRCA, GTT address of (the HWSP of) this context
  278. * bits 32-52: ctx ID, a globally unique tag
  279. * bits 53-54: mbz, reserved for use by hardware
  280. * bits 55-63: group ID, currently unused and set to 0
  281. */
  282. static void
  283. intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
  284. struct intel_engine_cs *engine)
  285. {
  286. struct intel_context *ce = &ctx->engine[engine->id];
  287. u64 desc;
  288. BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
  289. desc = ctx->desc_template; /* bits 3-4 */
  290. desc |= engine->ctx_desc_template; /* bits 0-11 */
  291. desc |= ce->lrc_vma->node.start + LRC_PPHWSP_PN * PAGE_SIZE;
  292. /* bits 12-31 */
  293. desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
  294. ce->lrc_desc = desc;
  295. }
  296. uint64_t intel_lr_context_descriptor(struct i915_gem_context *ctx,
  297. struct intel_engine_cs *engine)
  298. {
  299. return ctx->engine[engine->id].lrc_desc;
  300. }
  301. static void execlists_elsp_write(struct drm_i915_gem_request *rq0,
  302. struct drm_i915_gem_request *rq1)
  303. {
  304. struct intel_engine_cs *engine = rq0->engine;
  305. struct drm_i915_private *dev_priv = rq0->i915;
  306. uint64_t desc[2];
  307. if (rq1) {
  308. desc[1] = intel_lr_context_descriptor(rq1->ctx, rq1->engine);
  309. rq1->elsp_submitted++;
  310. } else {
  311. desc[1] = 0;
  312. }
  313. desc[0] = intel_lr_context_descriptor(rq0->ctx, rq0->engine);
  314. rq0->elsp_submitted++;
  315. /* You must always write both descriptors in the order below. */
  316. I915_WRITE_FW(RING_ELSP(engine), upper_32_bits(desc[1]));
  317. I915_WRITE_FW(RING_ELSP(engine), lower_32_bits(desc[1]));
  318. I915_WRITE_FW(RING_ELSP(engine), upper_32_bits(desc[0]));
  319. /* The context is automatically loaded after the following */
  320. I915_WRITE_FW(RING_ELSP(engine), lower_32_bits(desc[0]));
  321. /* ELSP is a wo register, use another nearby reg for posting */
  322. POSTING_READ_FW(RING_EXECLIST_STATUS_LO(engine));
  323. }
  324. static void
  325. execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
  326. {
  327. ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
  328. ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
  329. ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
  330. ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
  331. }
  332. static void execlists_update_context(struct drm_i915_gem_request *rq)
  333. {
  334. struct intel_engine_cs *engine = rq->engine;
  335. struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
  336. uint32_t *reg_state = rq->ctx->engine[engine->id].lrc_reg_state;
  337. reg_state[CTX_RING_TAIL+1] = intel_ring_offset(rq->ring, rq->tail);
  338. /* True 32b PPGTT with dynamic page allocation: update PDP
  339. * registers and point the unallocated PDPs to scratch page.
  340. * PML4 is allocated during ppgtt init, so this is not needed
  341. * in 48-bit mode.
  342. */
  343. if (ppgtt && !USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
  344. execlists_update_context_pdps(ppgtt, reg_state);
  345. }
  346. static void execlists_submit_requests(struct drm_i915_gem_request *rq0,
  347. struct drm_i915_gem_request *rq1)
  348. {
  349. struct drm_i915_private *dev_priv = rq0->i915;
  350. unsigned int fw_domains = rq0->engine->fw_domains;
  351. execlists_update_context(rq0);
  352. if (rq1)
  353. execlists_update_context(rq1);
  354. spin_lock_irq(&dev_priv->uncore.lock);
  355. intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
  356. execlists_elsp_write(rq0, rq1);
  357. intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
  358. spin_unlock_irq(&dev_priv->uncore.lock);
  359. }
  360. static inline void execlists_context_status_change(
  361. struct drm_i915_gem_request *rq,
  362. unsigned long status)
  363. {
  364. /*
  365. * Only used when GVT-g is enabled now. When GVT-g is disabled,
  366. * The compiler should eliminate this function as dead-code.
  367. */
  368. if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
  369. return;
  370. atomic_notifier_call_chain(&rq->ctx->status_notifier, status, rq);
  371. }
  372. static void execlists_context_unqueue(struct intel_engine_cs *engine)
  373. {
  374. struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
  375. struct drm_i915_gem_request *cursor, *tmp;
  376. assert_spin_locked(&engine->execlist_lock);
  377. /*
  378. * If irqs are not active generate a warning as batches that finish
  379. * without the irqs may get lost and a GPU Hang may occur.
  380. */
  381. WARN_ON(!intel_irqs_enabled(engine->i915));
  382. /* Try to read in pairs */
  383. list_for_each_entry_safe(cursor, tmp, &engine->execlist_queue,
  384. execlist_link) {
  385. if (!req0) {
  386. req0 = cursor;
  387. } else if (req0->ctx == cursor->ctx) {
  388. /* Same ctx: ignore first request, as second request
  389. * will update tail past first request's workload */
  390. cursor->elsp_submitted = req0->elsp_submitted;
  391. list_del(&req0->execlist_link);
  392. i915_gem_request_put(req0);
  393. req0 = cursor;
  394. } else {
  395. if (IS_ENABLED(CONFIG_DRM_I915_GVT)) {
  396. /*
  397. * req0 (after merged) ctx requires single
  398. * submission, stop picking
  399. */
  400. if (req0->ctx->execlists_force_single_submission)
  401. break;
  402. /*
  403. * req0 ctx doesn't require single submission,
  404. * but next req ctx requires, stop picking
  405. */
  406. if (cursor->ctx->execlists_force_single_submission)
  407. break;
  408. }
  409. req1 = cursor;
  410. WARN_ON(req1->elsp_submitted);
  411. break;
  412. }
  413. }
  414. if (unlikely(!req0))
  415. return;
  416. execlists_context_status_change(req0, INTEL_CONTEXT_SCHEDULE_IN);
  417. if (req1)
  418. execlists_context_status_change(req1,
  419. INTEL_CONTEXT_SCHEDULE_IN);
  420. if (req0->elsp_submitted & engine->idle_lite_restore_wa) {
  421. /*
  422. * WaIdleLiteRestore: make sure we never cause a lite restore
  423. * with HEAD==TAIL.
  424. *
  425. * Apply the wa NOOPS to prevent ring:HEAD == req:TAIL as we
  426. * resubmit the request. See gen8_emit_request() for where we
  427. * prepare the padding after the end of the request.
  428. */
  429. req0->tail += 8;
  430. req0->tail &= req0->ring->size - 1;
  431. }
  432. execlists_submit_requests(req0, req1);
  433. }
  434. static unsigned int
  435. execlists_check_remove_request(struct intel_engine_cs *engine, u32 ctx_id)
  436. {
  437. struct drm_i915_gem_request *head_req;
  438. assert_spin_locked(&engine->execlist_lock);
  439. head_req = list_first_entry_or_null(&engine->execlist_queue,
  440. struct drm_i915_gem_request,
  441. execlist_link);
  442. if (WARN_ON(!head_req || (head_req->ctx_hw_id != ctx_id)))
  443. return 0;
  444. WARN(head_req->elsp_submitted == 0, "Never submitted head request\n");
  445. if (--head_req->elsp_submitted > 0)
  446. return 0;
  447. execlists_context_status_change(head_req, INTEL_CONTEXT_SCHEDULE_OUT);
  448. list_del(&head_req->execlist_link);
  449. i915_gem_request_put(head_req);
  450. return 1;
  451. }
  452. static u32
  453. get_context_status(struct intel_engine_cs *engine, unsigned int read_pointer,
  454. u32 *context_id)
  455. {
  456. struct drm_i915_private *dev_priv = engine->i915;
  457. u32 status;
  458. read_pointer %= GEN8_CSB_ENTRIES;
  459. status = I915_READ_FW(RING_CONTEXT_STATUS_BUF_LO(engine, read_pointer));
  460. if (status & GEN8_CTX_STATUS_IDLE_ACTIVE)
  461. return 0;
  462. *context_id = I915_READ_FW(RING_CONTEXT_STATUS_BUF_HI(engine,
  463. read_pointer));
  464. return status;
  465. }
  466. /*
  467. * Check the unread Context Status Buffers and manage the submission of new
  468. * contexts to the ELSP accordingly.
  469. */
  470. static void intel_lrc_irq_handler(unsigned long data)
  471. {
  472. struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
  473. struct drm_i915_private *dev_priv = engine->i915;
  474. u32 status_pointer;
  475. unsigned int read_pointer, write_pointer;
  476. u32 csb[GEN8_CSB_ENTRIES][2];
  477. unsigned int csb_read = 0, i;
  478. unsigned int submit_contexts = 0;
  479. intel_uncore_forcewake_get(dev_priv, engine->fw_domains);
  480. status_pointer = I915_READ_FW(RING_CONTEXT_STATUS_PTR(engine));
  481. read_pointer = engine->next_context_status_buffer;
  482. write_pointer = GEN8_CSB_WRITE_PTR(status_pointer);
  483. if (read_pointer > write_pointer)
  484. write_pointer += GEN8_CSB_ENTRIES;
  485. while (read_pointer < write_pointer) {
  486. if (WARN_ON_ONCE(csb_read == GEN8_CSB_ENTRIES))
  487. break;
  488. csb[csb_read][0] = get_context_status(engine, ++read_pointer,
  489. &csb[csb_read][1]);
  490. csb_read++;
  491. }
  492. engine->next_context_status_buffer = write_pointer % GEN8_CSB_ENTRIES;
  493. /* Update the read pointer to the old write pointer. Manual ringbuffer
  494. * management ftw </sarcasm> */
  495. I915_WRITE_FW(RING_CONTEXT_STATUS_PTR(engine),
  496. _MASKED_FIELD(GEN8_CSB_READ_PTR_MASK,
  497. engine->next_context_status_buffer << 8));
  498. intel_uncore_forcewake_put(dev_priv, engine->fw_domains);
  499. spin_lock(&engine->execlist_lock);
  500. for (i = 0; i < csb_read; i++) {
  501. if (unlikely(csb[i][0] & GEN8_CTX_STATUS_PREEMPTED)) {
  502. if (csb[i][0] & GEN8_CTX_STATUS_LITE_RESTORE) {
  503. if (execlists_check_remove_request(engine, csb[i][1]))
  504. WARN(1, "Lite Restored request removed from queue\n");
  505. } else
  506. WARN(1, "Preemption without Lite Restore\n");
  507. }
  508. if (csb[i][0] & (GEN8_CTX_STATUS_ACTIVE_IDLE |
  509. GEN8_CTX_STATUS_ELEMENT_SWITCH))
  510. submit_contexts +=
  511. execlists_check_remove_request(engine, csb[i][1]);
  512. }
  513. if (submit_contexts) {
  514. if (!engine->disable_lite_restore_wa ||
  515. (csb[i][0] & GEN8_CTX_STATUS_ACTIVE_IDLE))
  516. execlists_context_unqueue(engine);
  517. }
  518. spin_unlock(&engine->execlist_lock);
  519. if (unlikely(submit_contexts > 2))
  520. DRM_ERROR("More than two context complete events?\n");
  521. }
  522. static void execlists_context_queue(struct drm_i915_gem_request *request)
  523. {
  524. struct intel_engine_cs *engine = request->engine;
  525. struct drm_i915_gem_request *cursor;
  526. int num_elements = 0;
  527. spin_lock_bh(&engine->execlist_lock);
  528. list_for_each_entry(cursor, &engine->execlist_queue, execlist_link)
  529. if (++num_elements > 2)
  530. break;
  531. if (num_elements > 2) {
  532. struct drm_i915_gem_request *tail_req;
  533. tail_req = list_last_entry(&engine->execlist_queue,
  534. struct drm_i915_gem_request,
  535. execlist_link);
  536. if (request->ctx == tail_req->ctx) {
  537. WARN(tail_req->elsp_submitted != 0,
  538. "More than 2 already-submitted reqs queued\n");
  539. list_del(&tail_req->execlist_link);
  540. i915_gem_request_put(tail_req);
  541. }
  542. }
  543. i915_gem_request_get(request);
  544. list_add_tail(&request->execlist_link, &engine->execlist_queue);
  545. request->ctx_hw_id = request->ctx->hw_id;
  546. if (num_elements == 0)
  547. execlists_context_unqueue(engine);
  548. spin_unlock_bh(&engine->execlist_lock);
  549. }
  550. static int execlists_move_to_gpu(struct drm_i915_gem_request *req,
  551. struct list_head *vmas)
  552. {
  553. const unsigned other_rings = ~intel_engine_flag(req->engine);
  554. struct i915_vma *vma;
  555. uint32_t flush_domains = 0;
  556. bool flush_chipset = false;
  557. int ret;
  558. list_for_each_entry(vma, vmas, exec_list) {
  559. struct drm_i915_gem_object *obj = vma->obj;
  560. if (obj->active & other_rings) {
  561. ret = i915_gem_object_sync(obj, req);
  562. if (ret)
  563. return ret;
  564. }
  565. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  566. flush_chipset |= i915_gem_clflush_object(obj, false);
  567. flush_domains |= obj->base.write_domain;
  568. }
  569. if (flush_domains & I915_GEM_DOMAIN_GTT)
  570. wmb();
  571. /* Unconditionally invalidate gpu caches and ensure that we do flush
  572. * any residual writes from the previous batch.
  573. */
  574. return req->engine->emit_flush(req, EMIT_INVALIDATE);
  575. }
  576. int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request)
  577. {
  578. struct intel_engine_cs *engine = request->engine;
  579. struct intel_context *ce = &request->ctx->engine[engine->id];
  580. int ret;
  581. /* Flush enough space to reduce the likelihood of waiting after
  582. * we start building the request - in which case we will just
  583. * have to repeat work.
  584. */
  585. request->reserved_space += EXECLISTS_REQUEST_SIZE;
  586. if (!ce->state) {
  587. ret = execlists_context_deferred_alloc(request->ctx, engine);
  588. if (ret)
  589. return ret;
  590. }
  591. request->ring = ce->ring;
  592. if (i915.enable_guc_submission) {
  593. /*
  594. * Check that the GuC has space for the request before
  595. * going any further, as the i915_add_request() call
  596. * later on mustn't fail ...
  597. */
  598. ret = i915_guc_wq_check_space(request);
  599. if (ret)
  600. return ret;
  601. }
  602. ret = intel_lr_context_pin(request->ctx, engine);
  603. if (ret)
  604. return ret;
  605. ret = intel_ring_begin(request, 0);
  606. if (ret)
  607. goto err_unpin;
  608. if (!ce->initialised) {
  609. ret = engine->init_context(request);
  610. if (ret)
  611. goto err_unpin;
  612. ce->initialised = true;
  613. }
  614. /* Note that after this point, we have committed to using
  615. * this request as it is being used to both track the
  616. * state of engine initialisation and liveness of the
  617. * golden renderstate above. Think twice before you try
  618. * to cancel/unwind this request now.
  619. */
  620. request->reserved_space -= EXECLISTS_REQUEST_SIZE;
  621. return 0;
  622. err_unpin:
  623. intel_lr_context_unpin(request->ctx, engine);
  624. return ret;
  625. }
  626. /*
  627. * intel_logical_ring_advance() - advance the tail and prepare for submission
  628. * @request: Request to advance the logical ringbuffer of.
  629. *
  630. * The tail is updated in our logical ringbuffer struct, not in the actual context. What
  631. * really happens during submission is that the context and current tail will be placed
  632. * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
  633. * point, the tail *inside* the context is updated and the ELSP written to.
  634. */
  635. static int
  636. intel_logical_ring_advance(struct drm_i915_gem_request *request)
  637. {
  638. struct intel_ring *ring = request->ring;
  639. struct intel_engine_cs *engine = request->engine;
  640. intel_ring_advance(ring);
  641. request->tail = ring->tail;
  642. /*
  643. * Here we add two extra NOOPs as padding to avoid
  644. * lite restore of a context with HEAD==TAIL.
  645. *
  646. * Caller must reserve WA_TAIL_DWORDS for us!
  647. */
  648. intel_ring_emit(ring, MI_NOOP);
  649. intel_ring_emit(ring, MI_NOOP);
  650. intel_ring_advance(ring);
  651. /* We keep the previous context alive until we retire the following
  652. * request. This ensures that any the context object is still pinned
  653. * for any residual writes the HW makes into it on the context switch
  654. * into the next object following the breadcrumb. Otherwise, we may
  655. * retire the context too early.
  656. */
  657. request->previous_context = engine->last_context;
  658. engine->last_context = request->ctx;
  659. return 0;
  660. }
  661. /**
  662. * intel_execlists_submission() - submit a batchbuffer for execution, Execlists style
  663. * @params: execbuffer call parameters.
  664. * @args: execbuffer call arguments.
  665. * @vmas: list of vmas.
  666. *
  667. * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
  668. * away the submission details of the execbuffer ioctl call.
  669. *
  670. * Return: non-zero if the submission fails.
  671. */
  672. int intel_execlists_submission(struct i915_execbuffer_params *params,
  673. struct drm_i915_gem_execbuffer2 *args,
  674. struct list_head *vmas)
  675. {
  676. struct drm_device *dev = params->dev;
  677. struct intel_engine_cs *engine = params->engine;
  678. struct drm_i915_private *dev_priv = to_i915(dev);
  679. struct intel_ring *ring = params->request->ring;
  680. u64 exec_start;
  681. int instp_mode;
  682. u32 instp_mask;
  683. int ret;
  684. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  685. instp_mask = I915_EXEC_CONSTANTS_MASK;
  686. switch (instp_mode) {
  687. case I915_EXEC_CONSTANTS_REL_GENERAL:
  688. case I915_EXEC_CONSTANTS_ABSOLUTE:
  689. case I915_EXEC_CONSTANTS_REL_SURFACE:
  690. if (instp_mode != 0 && engine->id != RCS) {
  691. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  692. return -EINVAL;
  693. }
  694. if (instp_mode != dev_priv->relative_constants_mode) {
  695. if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  696. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  697. return -EINVAL;
  698. }
  699. /* The HW changed the meaning on this bit on gen6 */
  700. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  701. }
  702. break;
  703. default:
  704. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  705. return -EINVAL;
  706. }
  707. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  708. DRM_DEBUG("sol reset is gen7 only\n");
  709. return -EINVAL;
  710. }
  711. ret = execlists_move_to_gpu(params->request, vmas);
  712. if (ret)
  713. return ret;
  714. if (engine->id == RCS &&
  715. instp_mode != dev_priv->relative_constants_mode) {
  716. ret = intel_ring_begin(params->request, 4);
  717. if (ret)
  718. return ret;
  719. intel_ring_emit(ring, MI_NOOP);
  720. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  721. intel_ring_emit_reg(ring, INSTPM);
  722. intel_ring_emit(ring, instp_mask << 16 | instp_mode);
  723. intel_ring_advance(ring);
  724. dev_priv->relative_constants_mode = instp_mode;
  725. }
  726. exec_start = params->batch_obj_vm_offset +
  727. args->batch_start_offset;
  728. ret = engine->emit_bb_start(params->request,
  729. exec_start, args->batch_len,
  730. params->dispatch_flags);
  731. if (ret)
  732. return ret;
  733. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  734. i915_gem_execbuffer_move_to_active(vmas, params->request);
  735. return 0;
  736. }
  737. void intel_execlists_cancel_requests(struct intel_engine_cs *engine)
  738. {
  739. struct drm_i915_gem_request *req, *tmp;
  740. LIST_HEAD(cancel_list);
  741. WARN_ON(!mutex_is_locked(&engine->i915->drm.struct_mutex));
  742. spin_lock_bh(&engine->execlist_lock);
  743. list_replace_init(&engine->execlist_queue, &cancel_list);
  744. spin_unlock_bh(&engine->execlist_lock);
  745. list_for_each_entry_safe(req, tmp, &cancel_list, execlist_link) {
  746. list_del(&req->execlist_link);
  747. i915_gem_request_put(req);
  748. }
  749. }
  750. void intel_logical_ring_stop(struct intel_engine_cs *engine)
  751. {
  752. struct drm_i915_private *dev_priv = engine->i915;
  753. int ret;
  754. if (!intel_engine_initialized(engine))
  755. return;
  756. ret = intel_engine_idle(engine);
  757. if (ret)
  758. DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
  759. engine->name, ret);
  760. /* TODO: Is this correct with Execlists enabled? */
  761. I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
  762. if (intel_wait_for_register(dev_priv,
  763. RING_MI_MODE(engine->mmio_base),
  764. MODE_IDLE, MODE_IDLE,
  765. 1000)) {
  766. DRM_ERROR("%s :timed out trying to stop ring\n", engine->name);
  767. return;
  768. }
  769. I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
  770. }
  771. static int intel_lr_context_pin(struct i915_gem_context *ctx,
  772. struct intel_engine_cs *engine)
  773. {
  774. struct drm_i915_private *dev_priv = ctx->i915;
  775. struct intel_context *ce = &ctx->engine[engine->id];
  776. void *vaddr;
  777. u32 *lrc_reg_state;
  778. int ret;
  779. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  780. if (ce->pin_count++)
  781. return 0;
  782. ret = i915_gem_obj_ggtt_pin(ce->state, GEN8_LR_CONTEXT_ALIGN,
  783. PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  784. if (ret)
  785. goto err;
  786. vaddr = i915_gem_object_pin_map(ce->state);
  787. if (IS_ERR(vaddr)) {
  788. ret = PTR_ERR(vaddr);
  789. goto unpin_ctx_obj;
  790. }
  791. lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  792. ret = intel_ring_pin(ce->ring);
  793. if (ret)
  794. goto unpin_map;
  795. ce->lrc_vma = i915_gem_obj_to_ggtt(ce->state);
  796. intel_lr_context_descriptor_update(ctx, engine);
  797. lrc_reg_state[CTX_RING_BUFFER_START+1] = ce->ring->vma->node.start;
  798. ce->lrc_reg_state = lrc_reg_state;
  799. ce->state->dirty = true;
  800. /* Invalidate GuC TLB. */
  801. if (i915.enable_guc_submission)
  802. I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
  803. i915_gem_context_get(ctx);
  804. return 0;
  805. unpin_map:
  806. i915_gem_object_unpin_map(ce->state);
  807. unpin_ctx_obj:
  808. i915_gem_object_ggtt_unpin(ce->state);
  809. err:
  810. ce->pin_count = 0;
  811. return ret;
  812. }
  813. void intel_lr_context_unpin(struct i915_gem_context *ctx,
  814. struct intel_engine_cs *engine)
  815. {
  816. struct intel_context *ce = &ctx->engine[engine->id];
  817. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  818. GEM_BUG_ON(ce->pin_count == 0);
  819. if (--ce->pin_count)
  820. return;
  821. intel_ring_unpin(ce->ring);
  822. i915_gem_object_unpin_map(ce->state);
  823. i915_gem_object_ggtt_unpin(ce->state);
  824. ce->lrc_vma = NULL;
  825. ce->lrc_desc = 0;
  826. ce->lrc_reg_state = NULL;
  827. i915_gem_context_put(ctx);
  828. }
  829. static int intel_logical_ring_workarounds_emit(struct drm_i915_gem_request *req)
  830. {
  831. int ret, i;
  832. struct intel_ring *ring = req->ring;
  833. struct i915_workarounds *w = &req->i915->workarounds;
  834. if (w->count == 0)
  835. return 0;
  836. ret = req->engine->emit_flush(req, EMIT_BARRIER);
  837. if (ret)
  838. return ret;
  839. ret = intel_ring_begin(req, w->count * 2 + 2);
  840. if (ret)
  841. return ret;
  842. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
  843. for (i = 0; i < w->count; i++) {
  844. intel_ring_emit_reg(ring, w->reg[i].addr);
  845. intel_ring_emit(ring, w->reg[i].value);
  846. }
  847. intel_ring_emit(ring, MI_NOOP);
  848. intel_ring_advance(ring);
  849. ret = req->engine->emit_flush(req, EMIT_BARRIER);
  850. if (ret)
  851. return ret;
  852. return 0;
  853. }
  854. #define wa_ctx_emit(batch, index, cmd) \
  855. do { \
  856. int __index = (index)++; \
  857. if (WARN_ON(__index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
  858. return -ENOSPC; \
  859. } \
  860. batch[__index] = (cmd); \
  861. } while (0)
  862. #define wa_ctx_emit_reg(batch, index, reg) \
  863. wa_ctx_emit((batch), (index), i915_mmio_reg_offset(reg))
  864. /*
  865. * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
  866. * PIPE_CONTROL instruction. This is required for the flush to happen correctly
  867. * but there is a slight complication as this is applied in WA batch where the
  868. * values are only initialized once so we cannot take register value at the
  869. * beginning and reuse it further; hence we save its value to memory, upload a
  870. * constant value with bit21 set and then we restore it back with the saved value.
  871. * To simplify the WA, a constant value is formed by using the default value
  872. * of this register. This shouldn't be a problem because we are only modifying
  873. * it for a short period and this batch in non-premptible. We can ofcourse
  874. * use additional instructions that read the actual value of the register
  875. * at that time and set our bit of interest but it makes the WA complicated.
  876. *
  877. * This WA is also required for Gen9 so extracting as a function avoids
  878. * code duplication.
  879. */
  880. static inline int gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine,
  881. uint32_t *batch,
  882. uint32_t index)
  883. {
  884. uint32_t l3sqc4_flush = (0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES);
  885. /*
  886. * WaDisableLSQCROPERFforOCL:skl,kbl
  887. * This WA is implemented in skl_init_clock_gating() but since
  888. * this batch updates GEN8_L3SQCREG4 with default value we need to
  889. * set this bit here to retain the WA during flush.
  890. */
  891. if (IS_SKL_REVID(engine->i915, 0, SKL_REVID_E0) ||
  892. IS_KBL_REVID(engine->i915, 0, KBL_REVID_E0))
  893. l3sqc4_flush |= GEN8_LQSC_RO_PERF_DIS;
  894. wa_ctx_emit(batch, index, (MI_STORE_REGISTER_MEM_GEN8 |
  895. MI_SRM_LRM_GLOBAL_GTT));
  896. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  897. wa_ctx_emit(batch, index, engine->scratch.gtt_offset + 256);
  898. wa_ctx_emit(batch, index, 0);
  899. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  900. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  901. wa_ctx_emit(batch, index, l3sqc4_flush);
  902. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  903. wa_ctx_emit(batch, index, (PIPE_CONTROL_CS_STALL |
  904. PIPE_CONTROL_DC_FLUSH_ENABLE));
  905. wa_ctx_emit(batch, index, 0);
  906. wa_ctx_emit(batch, index, 0);
  907. wa_ctx_emit(batch, index, 0);
  908. wa_ctx_emit(batch, index, 0);
  909. wa_ctx_emit(batch, index, (MI_LOAD_REGISTER_MEM_GEN8 |
  910. MI_SRM_LRM_GLOBAL_GTT));
  911. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  912. wa_ctx_emit(batch, index, engine->scratch.gtt_offset + 256);
  913. wa_ctx_emit(batch, index, 0);
  914. return index;
  915. }
  916. static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
  917. uint32_t offset,
  918. uint32_t start_alignment)
  919. {
  920. return wa_ctx->offset = ALIGN(offset, start_alignment);
  921. }
  922. static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
  923. uint32_t offset,
  924. uint32_t size_alignment)
  925. {
  926. wa_ctx->size = offset - wa_ctx->offset;
  927. WARN(wa_ctx->size % size_alignment,
  928. "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
  929. wa_ctx->size, size_alignment);
  930. return 0;
  931. }
  932. /*
  933. * Typically we only have one indirect_ctx and per_ctx batch buffer which are
  934. * initialized at the beginning and shared across all contexts but this field
  935. * helps us to have multiple batches at different offsets and select them based
  936. * on a criteria. At the moment this batch always start at the beginning of the page
  937. * and at this point we don't have multiple wa_ctx batch buffers.
  938. *
  939. * The number of WA applied are not known at the beginning; we use this field
  940. * to return the no of DWORDS written.
  941. *
  942. * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
  943. * so it adds NOOPs as padding to make it cacheline aligned.
  944. * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
  945. * makes a complete batch buffer.
  946. */
  947. static int gen8_init_indirectctx_bb(struct intel_engine_cs *engine,
  948. struct i915_wa_ctx_bb *wa_ctx,
  949. uint32_t *batch,
  950. uint32_t *offset)
  951. {
  952. uint32_t scratch_addr;
  953. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  954. /* WaDisableCtxRestoreArbitration:bdw,chv */
  955. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  956. /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
  957. if (IS_BROADWELL(engine->i915)) {
  958. int rc = gen8_emit_flush_coherentl3_wa(engine, batch, index);
  959. if (rc < 0)
  960. return rc;
  961. index = rc;
  962. }
  963. /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
  964. /* Actual scratch location is at 128 bytes offset */
  965. scratch_addr = engine->scratch.gtt_offset + 2*CACHELINE_BYTES;
  966. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  967. wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
  968. PIPE_CONTROL_GLOBAL_GTT_IVB |
  969. PIPE_CONTROL_CS_STALL |
  970. PIPE_CONTROL_QW_WRITE));
  971. wa_ctx_emit(batch, index, scratch_addr);
  972. wa_ctx_emit(batch, index, 0);
  973. wa_ctx_emit(batch, index, 0);
  974. wa_ctx_emit(batch, index, 0);
  975. /* Pad to end of cacheline */
  976. while (index % CACHELINE_DWORDS)
  977. wa_ctx_emit(batch, index, MI_NOOP);
  978. /*
  979. * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
  980. * execution depends on the length specified in terms of cache lines
  981. * in the register CTX_RCS_INDIRECT_CTX
  982. */
  983. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  984. }
  985. /*
  986. * This batch is started immediately after indirect_ctx batch. Since we ensure
  987. * that indirect_ctx ends on a cacheline this batch is aligned automatically.
  988. *
  989. * The number of DWORDS written are returned using this field.
  990. *
  991. * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
  992. * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
  993. */
  994. static int gen8_init_perctx_bb(struct intel_engine_cs *engine,
  995. struct i915_wa_ctx_bb *wa_ctx,
  996. uint32_t *batch,
  997. uint32_t *offset)
  998. {
  999. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1000. /* WaDisableCtxRestoreArbitration:bdw,chv */
  1001. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  1002. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  1003. return wa_ctx_end(wa_ctx, *offset = index, 1);
  1004. }
  1005. static int gen9_init_indirectctx_bb(struct intel_engine_cs *engine,
  1006. struct i915_wa_ctx_bb *wa_ctx,
  1007. uint32_t *batch,
  1008. uint32_t *offset)
  1009. {
  1010. int ret;
  1011. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1012. /* WaDisableCtxRestoreArbitration:skl,bxt */
  1013. if (IS_SKL_REVID(engine->i915, 0, SKL_REVID_D0) ||
  1014. IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1))
  1015. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  1016. /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt */
  1017. ret = gen8_emit_flush_coherentl3_wa(engine, batch, index);
  1018. if (ret < 0)
  1019. return ret;
  1020. index = ret;
  1021. /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl */
  1022. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  1023. wa_ctx_emit_reg(batch, index, COMMON_SLICE_CHICKEN2);
  1024. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(
  1025. GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE));
  1026. wa_ctx_emit(batch, index, MI_NOOP);
  1027. /* WaClearSlmSpaceAtContextSwitch:kbl */
  1028. /* Actual scratch location is at 128 bytes offset */
  1029. if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
  1030. uint32_t scratch_addr
  1031. = engine->scratch.gtt_offset + 2*CACHELINE_BYTES;
  1032. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  1033. wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
  1034. PIPE_CONTROL_GLOBAL_GTT_IVB |
  1035. PIPE_CONTROL_CS_STALL |
  1036. PIPE_CONTROL_QW_WRITE));
  1037. wa_ctx_emit(batch, index, scratch_addr);
  1038. wa_ctx_emit(batch, index, 0);
  1039. wa_ctx_emit(batch, index, 0);
  1040. wa_ctx_emit(batch, index, 0);
  1041. }
  1042. /* WaMediaPoolStateCmdInWABB:bxt */
  1043. if (HAS_POOLED_EU(engine->i915)) {
  1044. /*
  1045. * EU pool configuration is setup along with golden context
  1046. * during context initialization. This value depends on
  1047. * device type (2x6 or 3x6) and needs to be updated based
  1048. * on which subslice is disabled especially for 2x6
  1049. * devices, however it is safe to load default
  1050. * configuration of 3x6 device instead of masking off
  1051. * corresponding bits because HW ignores bits of a disabled
  1052. * subslice and drops down to appropriate config. Please
  1053. * see render_state_setup() in i915_gem_render_state.c for
  1054. * possible configurations, to avoid duplication they are
  1055. * not shown here again.
  1056. */
  1057. u32 eu_pool_config = 0x00777000;
  1058. wa_ctx_emit(batch, index, GEN9_MEDIA_POOL_STATE);
  1059. wa_ctx_emit(batch, index, GEN9_MEDIA_POOL_ENABLE);
  1060. wa_ctx_emit(batch, index, eu_pool_config);
  1061. wa_ctx_emit(batch, index, 0);
  1062. wa_ctx_emit(batch, index, 0);
  1063. wa_ctx_emit(batch, index, 0);
  1064. }
  1065. /* Pad to end of cacheline */
  1066. while (index % CACHELINE_DWORDS)
  1067. wa_ctx_emit(batch, index, MI_NOOP);
  1068. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  1069. }
  1070. static int gen9_init_perctx_bb(struct intel_engine_cs *engine,
  1071. struct i915_wa_ctx_bb *wa_ctx,
  1072. uint32_t *batch,
  1073. uint32_t *offset)
  1074. {
  1075. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1076. /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
  1077. if (IS_SKL_REVID(engine->i915, 0, SKL_REVID_B0) ||
  1078. IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1)) {
  1079. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  1080. wa_ctx_emit_reg(batch, index, GEN9_SLICE_COMMON_ECO_CHICKEN0);
  1081. wa_ctx_emit(batch, index,
  1082. _MASKED_BIT_ENABLE(DISABLE_PIXEL_MASK_CAMMING));
  1083. wa_ctx_emit(batch, index, MI_NOOP);
  1084. }
  1085. /* WaClearTdlStateAckDirtyBits:bxt */
  1086. if (IS_BXT_REVID(engine->i915, 0, BXT_REVID_B0)) {
  1087. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(4));
  1088. wa_ctx_emit_reg(batch, index, GEN8_STATE_ACK);
  1089. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  1090. wa_ctx_emit_reg(batch, index, GEN9_STATE_ACK_SLICE1);
  1091. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  1092. wa_ctx_emit_reg(batch, index, GEN9_STATE_ACK_SLICE2);
  1093. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  1094. wa_ctx_emit_reg(batch, index, GEN7_ROW_CHICKEN2);
  1095. /* dummy write to CS, mask bits are 0 to ensure the register is not modified */
  1096. wa_ctx_emit(batch, index, 0x0);
  1097. wa_ctx_emit(batch, index, MI_NOOP);
  1098. }
  1099. /* WaDisableCtxRestoreArbitration:skl,bxt */
  1100. if (IS_SKL_REVID(engine->i915, 0, SKL_REVID_D0) ||
  1101. IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1))
  1102. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  1103. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  1104. return wa_ctx_end(wa_ctx, *offset = index, 1);
  1105. }
  1106. static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *engine, u32 size)
  1107. {
  1108. int ret;
  1109. engine->wa_ctx.obj = i915_gem_object_create(&engine->i915->drm,
  1110. PAGE_ALIGN(size));
  1111. if (IS_ERR(engine->wa_ctx.obj)) {
  1112. DRM_DEBUG_DRIVER("alloc LRC WA ctx backing obj failed.\n");
  1113. ret = PTR_ERR(engine->wa_ctx.obj);
  1114. engine->wa_ctx.obj = NULL;
  1115. return ret;
  1116. }
  1117. ret = i915_gem_obj_ggtt_pin(engine->wa_ctx.obj, PAGE_SIZE, 0);
  1118. if (ret) {
  1119. DRM_DEBUG_DRIVER("pin LRC WA ctx backing obj failed: %d\n",
  1120. ret);
  1121. i915_gem_object_put(engine->wa_ctx.obj);
  1122. return ret;
  1123. }
  1124. return 0;
  1125. }
  1126. static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *engine)
  1127. {
  1128. if (engine->wa_ctx.obj) {
  1129. i915_gem_object_ggtt_unpin(engine->wa_ctx.obj);
  1130. i915_gem_object_put(engine->wa_ctx.obj);
  1131. engine->wa_ctx.obj = NULL;
  1132. }
  1133. }
  1134. static int intel_init_workaround_bb(struct intel_engine_cs *engine)
  1135. {
  1136. int ret;
  1137. uint32_t *batch;
  1138. uint32_t offset;
  1139. struct page *page;
  1140. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1141. WARN_ON(engine->id != RCS);
  1142. /* update this when WA for higher Gen are added */
  1143. if (INTEL_GEN(engine->i915) > 9) {
  1144. DRM_ERROR("WA batch buffer is not initialized for Gen%d\n",
  1145. INTEL_GEN(engine->i915));
  1146. return 0;
  1147. }
  1148. /* some WA perform writes to scratch page, ensure it is valid */
  1149. if (engine->scratch.obj == NULL) {
  1150. DRM_ERROR("scratch page not allocated for %s\n", engine->name);
  1151. return -EINVAL;
  1152. }
  1153. ret = lrc_setup_wa_ctx_obj(engine, PAGE_SIZE);
  1154. if (ret) {
  1155. DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
  1156. return ret;
  1157. }
  1158. page = i915_gem_object_get_dirty_page(wa_ctx->obj, 0);
  1159. batch = kmap_atomic(page);
  1160. offset = 0;
  1161. if (IS_GEN8(engine->i915)) {
  1162. ret = gen8_init_indirectctx_bb(engine,
  1163. &wa_ctx->indirect_ctx,
  1164. batch,
  1165. &offset);
  1166. if (ret)
  1167. goto out;
  1168. ret = gen8_init_perctx_bb(engine,
  1169. &wa_ctx->per_ctx,
  1170. batch,
  1171. &offset);
  1172. if (ret)
  1173. goto out;
  1174. } else if (IS_GEN9(engine->i915)) {
  1175. ret = gen9_init_indirectctx_bb(engine,
  1176. &wa_ctx->indirect_ctx,
  1177. batch,
  1178. &offset);
  1179. if (ret)
  1180. goto out;
  1181. ret = gen9_init_perctx_bb(engine,
  1182. &wa_ctx->per_ctx,
  1183. batch,
  1184. &offset);
  1185. if (ret)
  1186. goto out;
  1187. }
  1188. out:
  1189. kunmap_atomic(batch);
  1190. if (ret)
  1191. lrc_destroy_wa_ctx_obj(engine);
  1192. return ret;
  1193. }
  1194. static void lrc_init_hws(struct intel_engine_cs *engine)
  1195. {
  1196. struct drm_i915_private *dev_priv = engine->i915;
  1197. I915_WRITE(RING_HWS_PGA(engine->mmio_base),
  1198. (u32)engine->status_page.gfx_addr);
  1199. POSTING_READ(RING_HWS_PGA(engine->mmio_base));
  1200. }
  1201. static int gen8_init_common_ring(struct intel_engine_cs *engine)
  1202. {
  1203. struct drm_i915_private *dev_priv = engine->i915;
  1204. unsigned int next_context_status_buffer_hw;
  1205. lrc_init_hws(engine);
  1206. I915_WRITE_IMR(engine,
  1207. ~(engine->irq_enable_mask | engine->irq_keep_mask));
  1208. I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
  1209. I915_WRITE(RING_MODE_GEN7(engine),
  1210. _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
  1211. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  1212. POSTING_READ(RING_MODE_GEN7(engine));
  1213. /*
  1214. * Instead of resetting the Context Status Buffer (CSB) read pointer to
  1215. * zero, we need to read the write pointer from hardware and use its
  1216. * value because "this register is power context save restored".
  1217. * Effectively, these states have been observed:
  1218. *
  1219. * | Suspend-to-idle (freeze) | Suspend-to-RAM (mem) |
  1220. * BDW | CSB regs not reset | CSB regs reset |
  1221. * CHT | CSB regs not reset | CSB regs not reset |
  1222. * SKL | ? | ? |
  1223. * BXT | ? | ? |
  1224. */
  1225. next_context_status_buffer_hw =
  1226. GEN8_CSB_WRITE_PTR(I915_READ(RING_CONTEXT_STATUS_PTR(engine)));
  1227. /*
  1228. * When the CSB registers are reset (also after power-up / gpu reset),
  1229. * CSB write pointer is set to all 1's, which is not valid, use '5' in
  1230. * this special case, so the first element read is CSB[0].
  1231. */
  1232. if (next_context_status_buffer_hw == GEN8_CSB_PTR_MASK)
  1233. next_context_status_buffer_hw = (GEN8_CSB_ENTRIES - 1);
  1234. engine->next_context_status_buffer = next_context_status_buffer_hw;
  1235. DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
  1236. intel_engine_init_hangcheck(engine);
  1237. return intel_mocs_init_engine(engine);
  1238. }
  1239. static int gen8_init_render_ring(struct intel_engine_cs *engine)
  1240. {
  1241. struct drm_i915_private *dev_priv = engine->i915;
  1242. int ret;
  1243. ret = gen8_init_common_ring(engine);
  1244. if (ret)
  1245. return ret;
  1246. /* We need to disable the AsyncFlip performance optimisations in order
  1247. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  1248. * programmed to '1' on all products.
  1249. *
  1250. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  1251. */
  1252. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  1253. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  1254. return init_workarounds_ring(engine);
  1255. }
  1256. static int gen9_init_render_ring(struct intel_engine_cs *engine)
  1257. {
  1258. int ret;
  1259. ret = gen8_init_common_ring(engine);
  1260. if (ret)
  1261. return ret;
  1262. return init_workarounds_ring(engine);
  1263. }
  1264. static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
  1265. {
  1266. struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
  1267. struct intel_ring *ring = req->ring;
  1268. struct intel_engine_cs *engine = req->engine;
  1269. const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
  1270. int i, ret;
  1271. ret = intel_ring_begin(req, num_lri_cmds * 2 + 2);
  1272. if (ret)
  1273. return ret;
  1274. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(num_lri_cmds));
  1275. for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
  1276. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  1277. intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, i));
  1278. intel_ring_emit(ring, upper_32_bits(pd_daddr));
  1279. intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, i));
  1280. intel_ring_emit(ring, lower_32_bits(pd_daddr));
  1281. }
  1282. intel_ring_emit(ring, MI_NOOP);
  1283. intel_ring_advance(ring);
  1284. return 0;
  1285. }
  1286. static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
  1287. u64 offset, u32 len,
  1288. unsigned int dispatch_flags)
  1289. {
  1290. struct intel_ring *ring = req->ring;
  1291. bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
  1292. int ret;
  1293. /* Don't rely in hw updating PDPs, specially in lite-restore.
  1294. * Ideally, we should set Force PD Restore in ctx descriptor,
  1295. * but we can't. Force Restore would be a second option, but
  1296. * it is unsafe in case of lite-restore (because the ctx is
  1297. * not idle). PML4 is allocated during ppgtt init so this is
  1298. * not needed in 48-bit.*/
  1299. if (req->ctx->ppgtt &&
  1300. (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings)) {
  1301. if (!USES_FULL_48BIT_PPGTT(req->i915) &&
  1302. !intel_vgpu_active(req->i915)) {
  1303. ret = intel_logical_ring_emit_pdps(req);
  1304. if (ret)
  1305. return ret;
  1306. }
  1307. req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
  1308. }
  1309. ret = intel_ring_begin(req, 4);
  1310. if (ret)
  1311. return ret;
  1312. /* FIXME(BDW): Address space and security selectors. */
  1313. intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 |
  1314. (ppgtt<<8) |
  1315. (dispatch_flags & I915_DISPATCH_RS ?
  1316. MI_BATCH_RESOURCE_STREAMER : 0));
  1317. intel_ring_emit(ring, lower_32_bits(offset));
  1318. intel_ring_emit(ring, upper_32_bits(offset));
  1319. intel_ring_emit(ring, MI_NOOP);
  1320. intel_ring_advance(ring);
  1321. return 0;
  1322. }
  1323. static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
  1324. {
  1325. struct drm_i915_private *dev_priv = engine->i915;
  1326. I915_WRITE_IMR(engine,
  1327. ~(engine->irq_enable_mask | engine->irq_keep_mask));
  1328. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1329. }
  1330. static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
  1331. {
  1332. struct drm_i915_private *dev_priv = engine->i915;
  1333. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  1334. }
  1335. static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
  1336. {
  1337. struct intel_ring *ring = request->ring;
  1338. u32 cmd;
  1339. int ret;
  1340. ret = intel_ring_begin(request, 4);
  1341. if (ret)
  1342. return ret;
  1343. cmd = MI_FLUSH_DW + 1;
  1344. /* We always require a command barrier so that subsequent
  1345. * commands, such as breadcrumb interrupts, are strictly ordered
  1346. * wrt the contents of the write cache being flushed to memory
  1347. * (and thus being coherent from the CPU).
  1348. */
  1349. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1350. if (mode & EMIT_INVALIDATE) {
  1351. cmd |= MI_INVALIDATE_TLB;
  1352. if (request->engine->id == VCS)
  1353. cmd |= MI_INVALIDATE_BSD;
  1354. }
  1355. intel_ring_emit(ring, cmd);
  1356. intel_ring_emit(ring,
  1357. I915_GEM_HWS_SCRATCH_ADDR |
  1358. MI_FLUSH_DW_USE_GTT);
  1359. intel_ring_emit(ring, 0); /* upper addr */
  1360. intel_ring_emit(ring, 0); /* value */
  1361. intel_ring_advance(ring);
  1362. return 0;
  1363. }
  1364. static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
  1365. u32 mode)
  1366. {
  1367. struct intel_ring *ring = request->ring;
  1368. struct intel_engine_cs *engine = request->engine;
  1369. u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  1370. bool vf_flush_wa = false, dc_flush_wa = false;
  1371. u32 flags = 0;
  1372. int ret;
  1373. int len;
  1374. flags |= PIPE_CONTROL_CS_STALL;
  1375. if (mode & EMIT_FLUSH) {
  1376. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  1377. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  1378. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  1379. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  1380. }
  1381. if (mode & EMIT_INVALIDATE) {
  1382. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  1383. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  1384. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  1385. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1386. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  1387. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  1388. flags |= PIPE_CONTROL_QW_WRITE;
  1389. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1390. /*
  1391. * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
  1392. * pipe control.
  1393. */
  1394. if (IS_GEN9(request->i915))
  1395. vf_flush_wa = true;
  1396. /* WaForGAMHang:kbl */
  1397. if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
  1398. dc_flush_wa = true;
  1399. }
  1400. len = 6;
  1401. if (vf_flush_wa)
  1402. len += 6;
  1403. if (dc_flush_wa)
  1404. len += 12;
  1405. ret = intel_ring_begin(request, len);
  1406. if (ret)
  1407. return ret;
  1408. if (vf_flush_wa) {
  1409. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1410. intel_ring_emit(ring, 0);
  1411. intel_ring_emit(ring, 0);
  1412. intel_ring_emit(ring, 0);
  1413. intel_ring_emit(ring, 0);
  1414. intel_ring_emit(ring, 0);
  1415. }
  1416. if (dc_flush_wa) {
  1417. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1418. intel_ring_emit(ring, PIPE_CONTROL_DC_FLUSH_ENABLE);
  1419. intel_ring_emit(ring, 0);
  1420. intel_ring_emit(ring, 0);
  1421. intel_ring_emit(ring, 0);
  1422. intel_ring_emit(ring, 0);
  1423. }
  1424. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1425. intel_ring_emit(ring, flags);
  1426. intel_ring_emit(ring, scratch_addr);
  1427. intel_ring_emit(ring, 0);
  1428. intel_ring_emit(ring, 0);
  1429. intel_ring_emit(ring, 0);
  1430. if (dc_flush_wa) {
  1431. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1432. intel_ring_emit(ring, PIPE_CONTROL_CS_STALL);
  1433. intel_ring_emit(ring, 0);
  1434. intel_ring_emit(ring, 0);
  1435. intel_ring_emit(ring, 0);
  1436. intel_ring_emit(ring, 0);
  1437. }
  1438. intel_ring_advance(ring);
  1439. return 0;
  1440. }
  1441. static void bxt_a_seqno_barrier(struct intel_engine_cs *engine)
  1442. {
  1443. /*
  1444. * On BXT A steppings there is a HW coherency issue whereby the
  1445. * MI_STORE_DATA_IMM storing the completed request's seqno
  1446. * occasionally doesn't invalidate the CPU cache. Work around this by
  1447. * clflushing the corresponding cacheline whenever the caller wants
  1448. * the coherency to be guaranteed. Note that this cacheline is known
  1449. * to be clean at this point, since we only write it in
  1450. * bxt_a_set_seqno(), where we also do a clflush after the write. So
  1451. * this clflush in practice becomes an invalidate operation.
  1452. */
  1453. intel_flush_status_page(engine, I915_GEM_HWS_INDEX);
  1454. }
  1455. /*
  1456. * Reserve space for 2 NOOPs at the end of each request to be
  1457. * used as a workaround for not being allowed to do lite
  1458. * restore with HEAD==TAIL (WaIdleLiteRestore).
  1459. */
  1460. #define WA_TAIL_DWORDS 2
  1461. static int gen8_emit_request(struct drm_i915_gem_request *request)
  1462. {
  1463. struct intel_ring *ring = request->ring;
  1464. int ret;
  1465. ret = intel_ring_begin(request, 6 + WA_TAIL_DWORDS);
  1466. if (ret)
  1467. return ret;
  1468. /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
  1469. BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
  1470. intel_ring_emit(ring, (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW);
  1471. intel_ring_emit(ring,
  1472. intel_hws_seqno_address(request->engine) |
  1473. MI_FLUSH_DW_USE_GTT);
  1474. intel_ring_emit(ring, 0);
  1475. intel_ring_emit(ring, request->fence.seqno);
  1476. intel_ring_emit(ring, MI_USER_INTERRUPT);
  1477. intel_ring_emit(ring, MI_NOOP);
  1478. return intel_logical_ring_advance(request);
  1479. }
  1480. static int gen8_emit_request_render(struct drm_i915_gem_request *request)
  1481. {
  1482. struct intel_ring *ring = request->ring;
  1483. int ret;
  1484. ret = intel_ring_begin(request, 8 + WA_TAIL_DWORDS);
  1485. if (ret)
  1486. return ret;
  1487. /* We're using qword write, seqno should be aligned to 8 bytes. */
  1488. BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
  1489. /* w/a for post sync ops following a GPGPU operation we
  1490. * need a prior CS_STALL, which is emitted by the flush
  1491. * following the batch.
  1492. */
  1493. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1494. intel_ring_emit(ring,
  1495. (PIPE_CONTROL_GLOBAL_GTT_IVB |
  1496. PIPE_CONTROL_CS_STALL |
  1497. PIPE_CONTROL_QW_WRITE));
  1498. intel_ring_emit(ring, intel_hws_seqno_address(request->engine));
  1499. intel_ring_emit(ring, 0);
  1500. intel_ring_emit(ring, i915_gem_request_get_seqno(request));
  1501. /* We're thrashing one dword of HWS. */
  1502. intel_ring_emit(ring, 0);
  1503. intel_ring_emit(ring, MI_USER_INTERRUPT);
  1504. intel_ring_emit(ring, MI_NOOP);
  1505. return intel_logical_ring_advance(request);
  1506. }
  1507. static int intel_lr_context_render_state_init(struct drm_i915_gem_request *req)
  1508. {
  1509. struct render_state so;
  1510. int ret;
  1511. ret = i915_gem_render_state_prepare(req->engine, &so);
  1512. if (ret)
  1513. return ret;
  1514. if (so.rodata == NULL)
  1515. return 0;
  1516. ret = req->engine->emit_bb_start(req, so.ggtt_offset,
  1517. so.rodata->batch_items * 4,
  1518. I915_DISPATCH_SECURE);
  1519. if (ret)
  1520. goto out;
  1521. ret = req->engine->emit_bb_start(req,
  1522. (so.ggtt_offset + so.aux_batch_offset),
  1523. so.aux_batch_size,
  1524. I915_DISPATCH_SECURE);
  1525. if (ret)
  1526. goto out;
  1527. i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), req);
  1528. out:
  1529. i915_gem_render_state_fini(&so);
  1530. return ret;
  1531. }
  1532. static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
  1533. {
  1534. int ret;
  1535. ret = intel_logical_ring_workarounds_emit(req);
  1536. if (ret)
  1537. return ret;
  1538. ret = intel_rcs_context_init_mocs(req);
  1539. /*
  1540. * Failing to program the MOCS is non-fatal.The system will not
  1541. * run at peak performance. So generate an error and carry on.
  1542. */
  1543. if (ret)
  1544. DRM_ERROR("MOCS failed to program: expect performance issues.\n");
  1545. return intel_lr_context_render_state_init(req);
  1546. }
  1547. /**
  1548. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1549. * @engine: Engine Command Streamer.
  1550. */
  1551. void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
  1552. {
  1553. struct drm_i915_private *dev_priv;
  1554. if (!intel_engine_initialized(engine))
  1555. return;
  1556. /*
  1557. * Tasklet cannot be active at this point due intel_mark_active/idle
  1558. * so this is just for documentation.
  1559. */
  1560. if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->irq_tasklet.state)))
  1561. tasklet_kill(&engine->irq_tasklet);
  1562. dev_priv = engine->i915;
  1563. if (engine->buffer) {
  1564. intel_logical_ring_stop(engine);
  1565. WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1566. }
  1567. if (engine->cleanup)
  1568. engine->cleanup(engine);
  1569. intel_engine_cleanup_cmd_parser(engine);
  1570. i915_gem_batch_pool_fini(&engine->batch_pool);
  1571. intel_engine_fini_breadcrumbs(engine);
  1572. if (engine->status_page.obj) {
  1573. i915_gem_object_unpin_map(engine->status_page.obj);
  1574. engine->status_page.obj = NULL;
  1575. }
  1576. intel_lr_context_unpin(dev_priv->kernel_context, engine);
  1577. engine->idle_lite_restore_wa = 0;
  1578. engine->disable_lite_restore_wa = false;
  1579. engine->ctx_desc_template = 0;
  1580. lrc_destroy_wa_ctx_obj(engine);
  1581. engine->i915 = NULL;
  1582. }
  1583. void intel_execlists_enable_submission(struct drm_i915_private *dev_priv)
  1584. {
  1585. struct intel_engine_cs *engine;
  1586. for_each_engine(engine, dev_priv)
  1587. engine->submit_request = execlists_context_queue;
  1588. }
  1589. static void
  1590. logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  1591. {
  1592. /* Default vfuncs which can be overriden by each engine. */
  1593. engine->init_hw = gen8_init_common_ring;
  1594. engine->emit_flush = gen8_emit_flush;
  1595. engine->emit_request = gen8_emit_request;
  1596. engine->submit_request = execlists_context_queue;
  1597. engine->irq_enable = gen8_logical_ring_enable_irq;
  1598. engine->irq_disable = gen8_logical_ring_disable_irq;
  1599. engine->emit_bb_start = gen8_emit_bb_start;
  1600. if (IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1))
  1601. engine->irq_seqno_barrier = bxt_a_seqno_barrier;
  1602. }
  1603. static inline void
  1604. logical_ring_default_irqs(struct intel_engine_cs *engine)
  1605. {
  1606. unsigned shift = engine->irq_shift;
  1607. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
  1608. engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
  1609. }
  1610. static int
  1611. lrc_setup_hws(struct intel_engine_cs *engine,
  1612. struct drm_i915_gem_object *dctx_obj)
  1613. {
  1614. void *hws;
  1615. /* The HWSP is part of the default context object in LRC mode. */
  1616. engine->status_page.gfx_addr = i915_gem_obj_ggtt_offset(dctx_obj) +
  1617. LRC_PPHWSP_PN * PAGE_SIZE;
  1618. hws = i915_gem_object_pin_map(dctx_obj);
  1619. if (IS_ERR(hws))
  1620. return PTR_ERR(hws);
  1621. engine->status_page.page_addr = hws + LRC_PPHWSP_PN * PAGE_SIZE;
  1622. engine->status_page.obj = dctx_obj;
  1623. return 0;
  1624. }
  1625. static void
  1626. logical_ring_setup(struct intel_engine_cs *engine)
  1627. {
  1628. struct drm_i915_private *dev_priv = engine->i915;
  1629. enum forcewake_domains fw_domains;
  1630. intel_engine_setup_common(engine);
  1631. /* Intentionally left blank. */
  1632. engine->buffer = NULL;
  1633. fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
  1634. RING_ELSP(engine),
  1635. FW_REG_WRITE);
  1636. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1637. RING_CONTEXT_STATUS_PTR(engine),
  1638. FW_REG_READ | FW_REG_WRITE);
  1639. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1640. RING_CONTEXT_STATUS_BUF_BASE(engine),
  1641. FW_REG_READ);
  1642. engine->fw_domains = fw_domains;
  1643. tasklet_init(&engine->irq_tasklet,
  1644. intel_lrc_irq_handler, (unsigned long)engine);
  1645. logical_ring_init_platform_invariants(engine);
  1646. logical_ring_default_vfuncs(engine);
  1647. logical_ring_default_irqs(engine);
  1648. }
  1649. static int
  1650. logical_ring_init(struct intel_engine_cs *engine)
  1651. {
  1652. struct i915_gem_context *dctx = engine->i915->kernel_context;
  1653. int ret;
  1654. ret = intel_engine_init_common(engine);
  1655. if (ret)
  1656. goto error;
  1657. ret = execlists_context_deferred_alloc(dctx, engine);
  1658. if (ret)
  1659. goto error;
  1660. /* As this is the default context, always pin it */
  1661. ret = intel_lr_context_pin(dctx, engine);
  1662. if (ret) {
  1663. DRM_ERROR("Failed to pin context for %s: %d\n",
  1664. engine->name, ret);
  1665. goto error;
  1666. }
  1667. /* And setup the hardware status page. */
  1668. ret = lrc_setup_hws(engine, dctx->engine[engine->id].state);
  1669. if (ret) {
  1670. DRM_ERROR("Failed to set up hws %s: %d\n", engine->name, ret);
  1671. goto error;
  1672. }
  1673. return 0;
  1674. error:
  1675. intel_logical_ring_cleanup(engine);
  1676. return ret;
  1677. }
  1678. int logical_render_ring_init(struct intel_engine_cs *engine)
  1679. {
  1680. struct drm_i915_private *dev_priv = engine->i915;
  1681. int ret;
  1682. logical_ring_setup(engine);
  1683. if (HAS_L3_DPF(dev_priv))
  1684. engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1685. /* Override some for render ring. */
  1686. if (INTEL_GEN(dev_priv) >= 9)
  1687. engine->init_hw = gen9_init_render_ring;
  1688. else
  1689. engine->init_hw = gen8_init_render_ring;
  1690. engine->init_context = gen8_init_rcs_context;
  1691. engine->cleanup = intel_fini_pipe_control;
  1692. engine->emit_flush = gen8_emit_flush_render;
  1693. engine->emit_request = gen8_emit_request_render;
  1694. ret = intel_init_pipe_control(engine, 4096);
  1695. if (ret)
  1696. return ret;
  1697. ret = intel_init_workaround_bb(engine);
  1698. if (ret) {
  1699. /*
  1700. * We continue even if we fail to initialize WA batch
  1701. * because we only expect rare glitches but nothing
  1702. * critical to prevent us from using GPU
  1703. */
  1704. DRM_ERROR("WA batch buffer initialization failed: %d\n",
  1705. ret);
  1706. }
  1707. ret = logical_ring_init(engine);
  1708. if (ret) {
  1709. lrc_destroy_wa_ctx_obj(engine);
  1710. }
  1711. return ret;
  1712. }
  1713. int logical_xcs_ring_init(struct intel_engine_cs *engine)
  1714. {
  1715. logical_ring_setup(engine);
  1716. return logical_ring_init(engine);
  1717. }
  1718. static u32
  1719. make_rpcs(struct drm_i915_private *dev_priv)
  1720. {
  1721. u32 rpcs = 0;
  1722. /*
  1723. * No explicit RPCS request is needed to ensure full
  1724. * slice/subslice/EU enablement prior to Gen9.
  1725. */
  1726. if (INTEL_GEN(dev_priv) < 9)
  1727. return 0;
  1728. /*
  1729. * Starting in Gen9, render power gating can leave
  1730. * slice/subslice/EU in a partially enabled state. We
  1731. * must make an explicit request through RPCS for full
  1732. * enablement.
  1733. */
  1734. if (INTEL_INFO(dev_priv)->has_slice_pg) {
  1735. rpcs |= GEN8_RPCS_S_CNT_ENABLE;
  1736. rpcs |= INTEL_INFO(dev_priv)->slice_total <<
  1737. GEN8_RPCS_S_CNT_SHIFT;
  1738. rpcs |= GEN8_RPCS_ENABLE;
  1739. }
  1740. if (INTEL_INFO(dev_priv)->has_subslice_pg) {
  1741. rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
  1742. rpcs |= INTEL_INFO(dev_priv)->subslice_per_slice <<
  1743. GEN8_RPCS_SS_CNT_SHIFT;
  1744. rpcs |= GEN8_RPCS_ENABLE;
  1745. }
  1746. if (INTEL_INFO(dev_priv)->has_eu_pg) {
  1747. rpcs |= INTEL_INFO(dev_priv)->eu_per_subslice <<
  1748. GEN8_RPCS_EU_MIN_SHIFT;
  1749. rpcs |= INTEL_INFO(dev_priv)->eu_per_subslice <<
  1750. GEN8_RPCS_EU_MAX_SHIFT;
  1751. rpcs |= GEN8_RPCS_ENABLE;
  1752. }
  1753. return rpcs;
  1754. }
  1755. static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
  1756. {
  1757. u32 indirect_ctx_offset;
  1758. switch (INTEL_GEN(engine->i915)) {
  1759. default:
  1760. MISSING_CASE(INTEL_GEN(engine->i915));
  1761. /* fall through */
  1762. case 9:
  1763. indirect_ctx_offset =
  1764. GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1765. break;
  1766. case 8:
  1767. indirect_ctx_offset =
  1768. GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1769. break;
  1770. }
  1771. return indirect_ctx_offset;
  1772. }
  1773. static int
  1774. populate_lr_context(struct i915_gem_context *ctx,
  1775. struct drm_i915_gem_object *ctx_obj,
  1776. struct intel_engine_cs *engine,
  1777. struct intel_ring *ring)
  1778. {
  1779. struct drm_i915_private *dev_priv = ctx->i915;
  1780. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
  1781. void *vaddr;
  1782. u32 *reg_state;
  1783. int ret;
  1784. if (!ppgtt)
  1785. ppgtt = dev_priv->mm.aliasing_ppgtt;
  1786. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1787. if (ret) {
  1788. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1789. return ret;
  1790. }
  1791. vaddr = i915_gem_object_pin_map(ctx_obj);
  1792. if (IS_ERR(vaddr)) {
  1793. ret = PTR_ERR(vaddr);
  1794. DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
  1795. return ret;
  1796. }
  1797. ctx_obj->dirty = true;
  1798. /* The second page of the context object contains some fields which must
  1799. * be set up prior to the first execution. */
  1800. reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  1801. /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
  1802. * commands followed by (reg, value) pairs. The values we are setting here are
  1803. * only for the first context restore: on a subsequent save, the GPU will
  1804. * recreate this batchbuffer with new values (including all the missing
  1805. * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
  1806. reg_state[CTX_LRI_HEADER_0] =
  1807. MI_LOAD_REGISTER_IMM(engine->id == RCS ? 14 : 11) | MI_LRI_FORCE_POSTED;
  1808. ASSIGN_CTX_REG(reg_state, CTX_CONTEXT_CONTROL,
  1809. RING_CONTEXT_CONTROL(engine),
  1810. _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
  1811. CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
  1812. (HAS_RESOURCE_STREAMER(dev_priv) ?
  1813. CTX_CTRL_RS_CTX_ENABLE : 0)));
  1814. ASSIGN_CTX_REG(reg_state, CTX_RING_HEAD, RING_HEAD(engine->mmio_base),
  1815. 0);
  1816. ASSIGN_CTX_REG(reg_state, CTX_RING_TAIL, RING_TAIL(engine->mmio_base),
  1817. 0);
  1818. /* Ring buffer start address is not known until the buffer is pinned.
  1819. * It is written to the context image in execlists_update_context()
  1820. */
  1821. ASSIGN_CTX_REG(reg_state, CTX_RING_BUFFER_START,
  1822. RING_START(engine->mmio_base), 0);
  1823. ASSIGN_CTX_REG(reg_state, CTX_RING_BUFFER_CONTROL,
  1824. RING_CTL(engine->mmio_base),
  1825. ((ring->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID);
  1826. ASSIGN_CTX_REG(reg_state, CTX_BB_HEAD_U,
  1827. RING_BBADDR_UDW(engine->mmio_base), 0);
  1828. ASSIGN_CTX_REG(reg_state, CTX_BB_HEAD_L,
  1829. RING_BBADDR(engine->mmio_base), 0);
  1830. ASSIGN_CTX_REG(reg_state, CTX_BB_STATE,
  1831. RING_BBSTATE(engine->mmio_base),
  1832. RING_BB_PPGTT);
  1833. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_HEAD_U,
  1834. RING_SBBADDR_UDW(engine->mmio_base), 0);
  1835. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_HEAD_L,
  1836. RING_SBBADDR(engine->mmio_base), 0);
  1837. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_STATE,
  1838. RING_SBBSTATE(engine->mmio_base), 0);
  1839. if (engine->id == RCS) {
  1840. ASSIGN_CTX_REG(reg_state, CTX_BB_PER_CTX_PTR,
  1841. RING_BB_PER_CTX_PTR(engine->mmio_base), 0);
  1842. ASSIGN_CTX_REG(reg_state, CTX_RCS_INDIRECT_CTX,
  1843. RING_INDIRECT_CTX(engine->mmio_base), 0);
  1844. ASSIGN_CTX_REG(reg_state, CTX_RCS_INDIRECT_CTX_OFFSET,
  1845. RING_INDIRECT_CTX_OFFSET(engine->mmio_base), 0);
  1846. if (engine->wa_ctx.obj) {
  1847. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1848. uint32_t ggtt_offset = i915_gem_obj_ggtt_offset(wa_ctx->obj);
  1849. reg_state[CTX_RCS_INDIRECT_CTX+1] =
  1850. (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
  1851. (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
  1852. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
  1853. intel_lr_indirect_ctx_offset(engine) << 6;
  1854. reg_state[CTX_BB_PER_CTX_PTR+1] =
  1855. (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
  1856. 0x01;
  1857. }
  1858. }
  1859. reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
  1860. ASSIGN_CTX_REG(reg_state, CTX_CTX_TIMESTAMP,
  1861. RING_CTX_TIMESTAMP(engine->mmio_base), 0);
  1862. /* PDP values well be assigned later if needed */
  1863. ASSIGN_CTX_REG(reg_state, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3),
  1864. 0);
  1865. ASSIGN_CTX_REG(reg_state, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3),
  1866. 0);
  1867. ASSIGN_CTX_REG(reg_state, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2),
  1868. 0);
  1869. ASSIGN_CTX_REG(reg_state, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2),
  1870. 0);
  1871. ASSIGN_CTX_REG(reg_state, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1),
  1872. 0);
  1873. ASSIGN_CTX_REG(reg_state, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1),
  1874. 0);
  1875. ASSIGN_CTX_REG(reg_state, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0),
  1876. 0);
  1877. ASSIGN_CTX_REG(reg_state, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0),
  1878. 0);
  1879. if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
  1880. /* 64b PPGTT (48bit canonical)
  1881. * PDP0_DESCRIPTOR contains the base address to PML4 and
  1882. * other PDP Descriptors are ignored.
  1883. */
  1884. ASSIGN_CTX_PML4(ppgtt, reg_state);
  1885. } else {
  1886. /* 32b PPGTT
  1887. * PDP*_DESCRIPTOR contains the base address of space supported.
  1888. * With dynamic page allocation, PDPs may not be allocated at
  1889. * this point. Point the unallocated PDPs to the scratch page
  1890. */
  1891. execlists_update_context_pdps(ppgtt, reg_state);
  1892. }
  1893. if (engine->id == RCS) {
  1894. reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  1895. ASSIGN_CTX_REG(reg_state, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
  1896. make_rpcs(dev_priv));
  1897. }
  1898. i915_gem_object_unpin_map(ctx_obj);
  1899. return 0;
  1900. }
  1901. /**
  1902. * intel_lr_context_size() - return the size of the context for an engine
  1903. * @engine: which engine to find the context size for
  1904. *
  1905. * Each engine may require a different amount of space for a context image,
  1906. * so when allocating (or copying) an image, this function can be used to
  1907. * find the right size for the specific engine.
  1908. *
  1909. * Return: size (in bytes) of an engine-specific context image
  1910. *
  1911. * Note: this size includes the HWSP, which is part of the context image
  1912. * in LRC mode, but does not include the "shared data page" used with
  1913. * GuC submission. The caller should account for this if using the GuC.
  1914. */
  1915. uint32_t intel_lr_context_size(struct intel_engine_cs *engine)
  1916. {
  1917. int ret = 0;
  1918. WARN_ON(INTEL_GEN(engine->i915) < 8);
  1919. switch (engine->id) {
  1920. case RCS:
  1921. if (INTEL_GEN(engine->i915) >= 9)
  1922. ret = GEN9_LR_CONTEXT_RENDER_SIZE;
  1923. else
  1924. ret = GEN8_LR_CONTEXT_RENDER_SIZE;
  1925. break;
  1926. case VCS:
  1927. case BCS:
  1928. case VECS:
  1929. case VCS2:
  1930. ret = GEN8_LR_CONTEXT_OTHER_SIZE;
  1931. break;
  1932. }
  1933. return ret;
  1934. }
  1935. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  1936. struct intel_engine_cs *engine)
  1937. {
  1938. struct drm_i915_gem_object *ctx_obj;
  1939. struct intel_context *ce = &ctx->engine[engine->id];
  1940. uint32_t context_size;
  1941. struct intel_ring *ring;
  1942. int ret;
  1943. WARN_ON(ce->state);
  1944. context_size = round_up(intel_lr_context_size(engine), 4096);
  1945. /* One extra page as the sharing data between driver and GuC */
  1946. context_size += PAGE_SIZE * LRC_PPHWSP_PN;
  1947. ctx_obj = i915_gem_object_create(&ctx->i915->drm, context_size);
  1948. if (IS_ERR(ctx_obj)) {
  1949. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
  1950. return PTR_ERR(ctx_obj);
  1951. }
  1952. ring = intel_engine_create_ring(engine, ctx->ring_size);
  1953. if (IS_ERR(ring)) {
  1954. ret = PTR_ERR(ring);
  1955. goto error_deref_obj;
  1956. }
  1957. ret = populate_lr_context(ctx, ctx_obj, engine, ring);
  1958. if (ret) {
  1959. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  1960. goto error_ring_free;
  1961. }
  1962. ce->ring = ring;
  1963. ce->state = ctx_obj;
  1964. ce->initialised = engine->init_context == NULL;
  1965. return 0;
  1966. error_ring_free:
  1967. intel_ring_free(ring);
  1968. error_deref_obj:
  1969. i915_gem_object_put(ctx_obj);
  1970. ce->ring = NULL;
  1971. ce->state = NULL;
  1972. return ret;
  1973. }
  1974. void intel_lr_context_reset(struct drm_i915_private *dev_priv,
  1975. struct i915_gem_context *ctx)
  1976. {
  1977. struct intel_engine_cs *engine;
  1978. for_each_engine(engine, dev_priv) {
  1979. struct intel_context *ce = &ctx->engine[engine->id];
  1980. struct drm_i915_gem_object *ctx_obj = ce->state;
  1981. void *vaddr;
  1982. uint32_t *reg_state;
  1983. if (!ctx_obj)
  1984. continue;
  1985. vaddr = i915_gem_object_pin_map(ctx_obj);
  1986. if (WARN_ON(IS_ERR(vaddr)))
  1987. continue;
  1988. reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  1989. ctx_obj->dirty = true;
  1990. reg_state[CTX_RING_HEAD+1] = 0;
  1991. reg_state[CTX_RING_TAIL+1] = 0;
  1992. i915_gem_object_unpin_map(ctx_obj);
  1993. ce->ring->head = 0;
  1994. ce->ring->tail = 0;
  1995. }
  1996. }