intel_lrc.c 61 KB

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