intel_lrc.c 76 KB

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