intel_lrc.c 68 KB

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