intel_lrc.c 80 KB

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