intel_lrc.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  1. /*
  2. * Copyright © 2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Ben Widawsky <ben@bwidawsk.net>
  25. * Michel Thierry <michel.thierry@intel.com>
  26. * Thomas Daniel <thomas.daniel@intel.com>
  27. * Oscar Mateo <oscar.mateo@intel.com>
  28. *
  29. */
  30. /**
  31. * DOC: Logical Rings, Logical Ring Contexts and Execlists
  32. *
  33. * Motivation:
  34. * GEN8 brings an expansion of the HW contexts: "Logical Ring Contexts".
  35. * These expanded contexts enable a number of new abilities, especially
  36. * "Execlists" (also implemented in this file).
  37. *
  38. * One of the main differences with the legacy HW contexts is that logical
  39. * ring contexts incorporate many more things to the context's state, like
  40. * PDPs or ringbuffer control registers:
  41. *
  42. * The reason why PDPs are included in the context is straightforward: as
  43. * PPGTTs (per-process GTTs) are actually per-context, having the PDPs
  44. * contained there mean you don't need to do a ppgtt->switch_mm yourself,
  45. * instead, the GPU will do it for you on the context switch.
  46. *
  47. * But, what about the ringbuffer control registers (head, tail, etc..)?
  48. * shouldn't we just need a set of those per engine command streamer? This is
  49. * where the name "Logical Rings" starts to make sense: by virtualizing the
  50. * rings, the engine cs shifts to a new "ring buffer" with every context
  51. * switch. When you want to submit a workload to the GPU you: A) choose your
  52. * context, B) find its appropriate virtualized ring, C) write commands to it
  53. * and then, finally, D) tell the GPU to switch to that context.
  54. *
  55. * Instead of the legacy MI_SET_CONTEXT, the way you tell the GPU to switch
  56. * to a contexts is via a context execution list, ergo "Execlists".
  57. *
  58. * LRC implementation:
  59. * Regarding the creation of contexts, we have:
  60. *
  61. * - One global default context.
  62. * - One local default context for each opened fd.
  63. * - One local extra context for each context create ioctl call.
  64. *
  65. * Now that ringbuffers belong per-context (and not per-engine, like before)
  66. * and that contexts are uniquely tied to a given engine (and not reusable,
  67. * like before) we need:
  68. *
  69. * - One ringbuffer per-engine inside each context.
  70. * - One backing object per-engine inside each context.
  71. *
  72. * The global default context starts its life with these new objects fully
  73. * allocated and populated. The local default context for each opened fd is
  74. * more complex, because we don't know at creation time which engine is going
  75. * to use them. To handle this, we have implemented a deferred creation of LR
  76. * contexts:
  77. *
  78. * The local context starts its life as a hollow or blank holder, that only
  79. * gets populated for a given engine once we receive an execbuffer. If later
  80. * on we receive another execbuffer ioctl for the same context but a different
  81. * engine, we allocate/populate a new ringbuffer and context backing object and
  82. * so on.
  83. *
  84. * Finally, regarding local contexts created using the ioctl call: as they are
  85. * only allowed with the render ring, we can allocate & populate them right
  86. * away (no need to defer anything, at least for now).
  87. *
  88. * Execlists implementation:
  89. * Execlists are the new method by which, on gen8+ hardware, workloads are
  90. * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
  91. * This method works as follows:
  92. *
  93. * When a request is committed, its commands (the BB start and any leading or
  94. * trailing commands, like the seqno breadcrumbs) are placed in the ringbuffer
  95. * for the appropriate context. The tail pointer in the hardware context is not
  96. * updated at this time, but instead, kept by the driver in the ringbuffer
  97. * structure. A structure representing this request is added to a request queue
  98. * for the appropriate engine: this structure contains a copy of the context's
  99. * tail after the request was written to the ring buffer and a pointer to the
  100. * context itself.
  101. *
  102. * If the engine's request queue was empty before the request was added, the
  103. * queue is processed immediately. Otherwise the queue will be processed during
  104. * a context switch interrupt. In any case, elements on the queue will get sent
  105. * (in pairs) to the GPU's ExecLists Submit Port (ELSP, for short) with a
  106. * globally unique 20-bits submission ID.
  107. *
  108. * When execution of a request completes, the GPU updates the context status
  109. * buffer with a context complete event and generates a context switch interrupt.
  110. * During the interrupt handling, the driver examines the events in the buffer:
  111. * for each context complete event, if the announced ID matches that on the head
  112. * of the request queue, then that request is retired and removed from the queue.
  113. *
  114. * After processing, if any requests were retired and the queue is not empty
  115. * then a new execution list can be submitted. The two requests at the front of
  116. * the queue are next to be submitted but since a context may not occur twice in
  117. * an execution list, if subsequent requests have the same ID as the first then
  118. * the two requests must be combined. This is done simply by discarding requests
  119. * at the head of the queue until either only one requests is left (in which case
  120. * we use a NULL second context) or the first two requests have unique IDs.
  121. *
  122. * By always executing the first two requests in the queue the driver ensures
  123. * that the GPU is kept as busy as possible. In the case where a single context
  124. * completes but a second context is still executing, the request for this second
  125. * context will be at the head of the queue when we remove the first one. This
  126. * request will then be resubmitted along with a new request for a different context,
  127. * which will cause the hardware to continue executing the second request and queue
  128. * the new request (the GPU detects the condition of a context getting preempted
  129. * with the same context and optimizes the context switch flow by not doing
  130. * preemption, but just sampling the new tail pointer).
  131. *
  132. */
  133. #include <linux/interrupt.h>
  134. #include <drm/drmP.h>
  135. #include <drm/i915_drm.h>
  136. #include "i915_drv.h"
  137. #include "intel_mocs.h"
  138. #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
  139. #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
  140. #define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
  141. #define RING_EXECLIST_QFULL (1 << 0x2)
  142. #define RING_EXECLIST1_VALID (1 << 0x3)
  143. #define RING_EXECLIST0_VALID (1 << 0x4)
  144. #define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
  145. #define RING_EXECLIST1_ACTIVE (1 << 0x11)
  146. #define RING_EXECLIST0_ACTIVE (1 << 0x12)
  147. #define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
  148. #define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
  149. #define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
  150. #define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
  151. #define GEN8_CTX_STATUS_COMPLETE (1 << 4)
  152. #define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
  153. #define GEN8_CTX_STATUS_COMPLETED_MASK \
  154. (GEN8_CTX_STATUS_ACTIVE_IDLE | \
  155. GEN8_CTX_STATUS_PREEMPTED | \
  156. GEN8_CTX_STATUS_ELEMENT_SWITCH)
  157. #define CTX_LRI_HEADER_0 0x01
  158. #define CTX_CONTEXT_CONTROL 0x02
  159. #define CTX_RING_HEAD 0x04
  160. #define CTX_RING_TAIL 0x06
  161. #define CTX_RING_BUFFER_START 0x08
  162. #define CTX_RING_BUFFER_CONTROL 0x0a
  163. #define CTX_BB_HEAD_U 0x0c
  164. #define CTX_BB_HEAD_L 0x0e
  165. #define CTX_BB_STATE 0x10
  166. #define CTX_SECOND_BB_HEAD_U 0x12
  167. #define CTX_SECOND_BB_HEAD_L 0x14
  168. #define CTX_SECOND_BB_STATE 0x16
  169. #define CTX_BB_PER_CTX_PTR 0x18
  170. #define CTX_RCS_INDIRECT_CTX 0x1a
  171. #define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
  172. #define CTX_LRI_HEADER_1 0x21
  173. #define CTX_CTX_TIMESTAMP 0x22
  174. #define CTX_PDP3_UDW 0x24
  175. #define CTX_PDP3_LDW 0x26
  176. #define CTX_PDP2_UDW 0x28
  177. #define CTX_PDP2_LDW 0x2a
  178. #define CTX_PDP1_UDW 0x2c
  179. #define CTX_PDP1_LDW 0x2e
  180. #define CTX_PDP0_UDW 0x30
  181. #define CTX_PDP0_LDW 0x32
  182. #define CTX_LRI_HEADER_2 0x41
  183. #define CTX_R_PWR_CLK_STATE 0x42
  184. #define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
  185. #define GEN8_CTX_VALID (1<<0)
  186. #define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
  187. #define GEN8_CTX_FORCE_RESTORE (1<<2)
  188. #define GEN8_CTX_L3LLC_COHERENT (1<<5)
  189. #define GEN8_CTX_PRIVILEGE (1<<8)
  190. #define ASSIGN_CTX_REG(reg_state, pos, reg, val) do { \
  191. (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
  192. (reg_state)[(pos)+1] = (val); \
  193. } while (0)
  194. #define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \
  195. const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
  196. reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
  197. reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
  198. } while (0)
  199. #define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
  200. reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \
  201. reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \
  202. } while (0)
  203. enum {
  204. FAULT_AND_HANG = 0,
  205. FAULT_AND_HALT, /* Debug only */
  206. FAULT_AND_STREAM,
  207. FAULT_AND_CONTINUE /* Unsupported */
  208. };
  209. #define GEN8_CTX_ID_SHIFT 32
  210. #define GEN8_CTX_ID_WIDTH 21
  211. #define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
  212. #define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26
  213. /* Typical size of the average request (2 pipecontrols and a MI_BB) */
  214. #define EXECLISTS_REQUEST_SIZE 64 /* bytes */
  215. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  216. struct intel_engine_cs *engine);
  217. static int intel_lr_context_pin(struct i915_gem_context *ctx,
  218. struct intel_engine_cs *engine);
  219. /**
  220. * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
  221. * @dev_priv: i915 device private
  222. * @enable_execlists: value of i915.enable_execlists module parameter.
  223. *
  224. * Only certain platforms support Execlists (the prerequisites being
  225. * support for Logical Ring Contexts and Aliasing PPGTT or better).
  226. *
  227. * Return: 1 if Execlists is supported and has to be enabled.
  228. */
  229. int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists)
  230. {
  231. /* On platforms with execlist available, vGPU will only
  232. * support execlist mode, no ring buffer mode.
  233. */
  234. if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv))
  235. return 1;
  236. if (INTEL_GEN(dev_priv) >= 9)
  237. return 1;
  238. if (enable_execlists == 0)
  239. return 0;
  240. if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) &&
  241. USES_PPGTT(dev_priv) &&
  242. i915.use_mmio_flip >= 0)
  243. return 1;
  244. return 0;
  245. }
  246. static void
  247. logical_ring_init_platform_invariants(struct intel_engine_cs *engine)
  248. {
  249. struct drm_i915_private *dev_priv = engine->i915;
  250. engine->disable_lite_restore_wa =
  251. (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
  252. IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) &&
  253. (engine->id == VCS || engine->id == VCS2);
  254. engine->ctx_desc_template = GEN8_CTX_VALID;
  255. if (IS_GEN8(dev_priv))
  256. engine->ctx_desc_template |= GEN8_CTX_L3LLC_COHERENT;
  257. engine->ctx_desc_template |= GEN8_CTX_PRIVILEGE;
  258. /* TODO: WaDisableLiteRestore when we start using semaphore
  259. * signalling between Command Streamers */
  260. /* ring->ctx_desc_template |= GEN8_CTX_FORCE_RESTORE; */
  261. /* WaEnableForceRestoreInCtxtDescForVCS:skl */
  262. /* WaEnableForceRestoreInCtxtDescForVCS:bxt */
  263. if (engine->disable_lite_restore_wa)
  264. engine->ctx_desc_template |= GEN8_CTX_FORCE_RESTORE;
  265. }
  266. /**
  267. * intel_lr_context_descriptor_update() - calculate & cache the descriptor
  268. * descriptor for a pinned context
  269. * @ctx: Context to work on
  270. * @engine: Engine the descriptor will be used with
  271. *
  272. * The context descriptor encodes various attributes of a context,
  273. * including its GTT address and some flags. Because it's fairly
  274. * expensive to calculate, we'll just do it once and cache the result,
  275. * which remains valid until the context is unpinned.
  276. *
  277. * This is what a descriptor looks like, from LSB to MSB::
  278. *
  279. * bits 0-11: flags, GEN8_CTX_* (cached in ctx_desc_template)
  280. * bits 12-31: LRCA, GTT address of (the HWSP of) this context
  281. * bits 32-52: ctx ID, a globally unique tag
  282. * bits 53-54: mbz, reserved for use by hardware
  283. * bits 55-63: group ID, currently unused and set to 0
  284. */
  285. static void
  286. intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
  287. struct intel_engine_cs *engine)
  288. {
  289. struct intel_context *ce = &ctx->engine[engine->id];
  290. u64 desc;
  291. BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
  292. desc = ctx->desc_template; /* bits 3-4 */
  293. desc |= engine->ctx_desc_template; /* bits 0-11 */
  294. desc |= i915_ggtt_offset(ce->state) + LRC_PPHWSP_PN * PAGE_SIZE;
  295. /* bits 12-31 */
  296. desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
  297. ce->lrc_desc = desc;
  298. }
  299. uint64_t intel_lr_context_descriptor(struct i915_gem_context *ctx,
  300. struct intel_engine_cs *engine)
  301. {
  302. return ctx->engine[engine->id].lrc_desc;
  303. }
  304. static inline void
  305. execlists_context_status_change(struct drm_i915_gem_request *rq,
  306. unsigned long status)
  307. {
  308. /*
  309. * Only used when GVT-g is enabled now. When GVT-g is disabled,
  310. * The compiler should eliminate this function as dead-code.
  311. */
  312. if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
  313. return;
  314. atomic_notifier_call_chain(&rq->ctx->status_notifier, status, rq);
  315. }
  316. static void
  317. execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
  318. {
  319. ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
  320. ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
  321. ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
  322. ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
  323. }
  324. static u64 execlists_update_context(struct drm_i915_gem_request *rq)
  325. {
  326. struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
  327. struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
  328. u32 *reg_state = ce->lrc_reg_state;
  329. reg_state[CTX_RING_TAIL+1] = intel_ring_offset(rq->ring, rq->tail);
  330. /* True 32b PPGTT with dynamic page allocation: update PDP
  331. * registers and point the unallocated PDPs to scratch page.
  332. * PML4 is allocated during ppgtt init, so this is not needed
  333. * in 48-bit mode.
  334. */
  335. if (ppgtt && !USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
  336. execlists_update_context_pdps(ppgtt, reg_state);
  337. return ce->lrc_desc;
  338. }
  339. static void execlists_submit_ports(struct intel_engine_cs *engine)
  340. {
  341. struct drm_i915_private *dev_priv = engine->i915;
  342. struct execlist_port *port = engine->execlist_port;
  343. u32 __iomem *elsp =
  344. dev_priv->regs + i915_mmio_reg_offset(RING_ELSP(engine));
  345. u64 desc[2];
  346. if (!port[0].count)
  347. execlists_context_status_change(port[0].request,
  348. INTEL_CONTEXT_SCHEDULE_IN);
  349. desc[0] = execlists_update_context(port[0].request);
  350. engine->preempt_wa = port[0].count++; /* bdw only? fixed on skl? */
  351. if (port[1].request) {
  352. GEM_BUG_ON(port[1].count);
  353. execlists_context_status_change(port[1].request,
  354. INTEL_CONTEXT_SCHEDULE_IN);
  355. desc[1] = execlists_update_context(port[1].request);
  356. port[1].count = 1;
  357. } else {
  358. desc[1] = 0;
  359. }
  360. GEM_BUG_ON(desc[0] == desc[1]);
  361. /* You must always write both descriptors in the order below. */
  362. writel(upper_32_bits(desc[1]), elsp);
  363. writel(lower_32_bits(desc[1]), elsp);
  364. writel(upper_32_bits(desc[0]), elsp);
  365. /* The context is automatically loaded after the following */
  366. writel(lower_32_bits(desc[0]), elsp);
  367. }
  368. static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
  369. {
  370. return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
  371. ctx->execlists_force_single_submission);
  372. }
  373. static bool can_merge_ctx(const struct i915_gem_context *prev,
  374. const struct i915_gem_context *next)
  375. {
  376. if (prev != next)
  377. return false;
  378. if (ctx_single_port_submission(prev))
  379. return false;
  380. return true;
  381. }
  382. static void execlists_dequeue(struct intel_engine_cs *engine)
  383. {
  384. struct drm_i915_gem_request *cursor, *last;
  385. struct execlist_port *port = engine->execlist_port;
  386. bool submit = false;
  387. last = port->request;
  388. if (last)
  389. /* WaIdleLiteRestore:bdw,skl
  390. * Apply the wa NOOPs to prevent ring:HEAD == req:TAIL
  391. * as we resubmit the request. See gen8_emit_request()
  392. * for where we prepare the padding after the end of the
  393. * request.
  394. */
  395. last->tail = last->wa_tail;
  396. GEM_BUG_ON(port[1].request);
  397. /* Hardware submission is through 2 ports. Conceptually each port
  398. * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
  399. * static for a context, and unique to each, so we only execute
  400. * requests belonging to a single context from each ring. RING_HEAD
  401. * is maintained by the CS in the context image, it marks the place
  402. * where it got up to last time, and through RING_TAIL we tell the CS
  403. * where we want to execute up to this time.
  404. *
  405. * In this list the requests are in order of execution. Consecutive
  406. * requests from the same context are adjacent in the ringbuffer. We
  407. * can combine these requests into a single RING_TAIL update:
  408. *
  409. * RING_HEAD...req1...req2
  410. * ^- RING_TAIL
  411. * since to execute req2 the CS must first execute req1.
  412. *
  413. * Our goal then is to point each port to the end of a consecutive
  414. * sequence of requests as being the most optimal (fewest wake ups
  415. * and context switches) submission.
  416. */
  417. spin_lock(&engine->execlist_lock);
  418. list_for_each_entry(cursor, &engine->execlist_queue, execlist_link) {
  419. /* Can we combine this request with the current port? It has to
  420. * be the same context/ringbuffer and not have any exceptions
  421. * (e.g. GVT saying never to combine contexts).
  422. *
  423. * If we can combine the requests, we can execute both by
  424. * updating the RING_TAIL to point to the end of the second
  425. * request, and so we never need to tell the hardware about
  426. * the first.
  427. */
  428. if (last && !can_merge_ctx(cursor->ctx, last->ctx)) {
  429. /* If we are on the second port and cannot combine
  430. * this request with the last, then we are done.
  431. */
  432. if (port != engine->execlist_port)
  433. break;
  434. /* If GVT overrides us we only ever submit port[0],
  435. * leaving port[1] empty. Note that we also have
  436. * to be careful that we don't queue the same
  437. * context (even though a different request) to
  438. * the second port.
  439. */
  440. if (ctx_single_port_submission(cursor->ctx))
  441. break;
  442. GEM_BUG_ON(last->ctx == cursor->ctx);
  443. i915_gem_request_assign(&port->request, last);
  444. port++;
  445. }
  446. last = cursor;
  447. submit = true;
  448. }
  449. if (submit) {
  450. /* Decouple all the requests submitted from the queue */
  451. engine->execlist_queue.next = &cursor->execlist_link;
  452. cursor->execlist_link.prev = &engine->execlist_queue;
  453. i915_gem_request_assign(&port->request, last);
  454. }
  455. spin_unlock(&engine->execlist_lock);
  456. if (submit)
  457. execlists_submit_ports(engine);
  458. }
  459. static bool execlists_elsp_idle(struct intel_engine_cs *engine)
  460. {
  461. return !engine->execlist_port[0].request;
  462. }
  463. static bool execlists_elsp_ready(struct intel_engine_cs *engine)
  464. {
  465. int port;
  466. port = 1; /* wait for a free slot */
  467. if (engine->disable_lite_restore_wa || engine->preempt_wa)
  468. port = 0; /* wait for GPU to be idle before continuing */
  469. return !engine->execlist_port[port].request;
  470. }
  471. /*
  472. * Check the unread Context Status Buffers and manage the submission of new
  473. * contexts to the ELSP accordingly.
  474. */
  475. static void intel_lrc_irq_handler(unsigned long data)
  476. {
  477. struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
  478. struct execlist_port *port = engine->execlist_port;
  479. struct drm_i915_private *dev_priv = engine->i915;
  480. intel_uncore_forcewake_get(dev_priv, engine->fw_domains);
  481. if (!execlists_elsp_idle(engine)) {
  482. u32 __iomem *csb_mmio =
  483. dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine));
  484. u32 __iomem *buf =
  485. dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0));
  486. unsigned int csb, head, tail;
  487. csb = readl(csb_mmio);
  488. head = GEN8_CSB_READ_PTR(csb);
  489. tail = GEN8_CSB_WRITE_PTR(csb);
  490. if (tail < head)
  491. tail += GEN8_CSB_ENTRIES;
  492. while (head < tail) {
  493. unsigned int idx = ++head % GEN8_CSB_ENTRIES;
  494. unsigned int status = readl(buf + 2 * idx);
  495. if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
  496. continue;
  497. GEM_BUG_ON(port[0].count == 0);
  498. if (--port[0].count == 0) {
  499. GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
  500. execlists_context_status_change(port[0].request,
  501. INTEL_CONTEXT_SCHEDULE_OUT);
  502. i915_gem_request_put(port[0].request);
  503. port[0] = port[1];
  504. memset(&port[1], 0, sizeof(port[1]));
  505. engine->preempt_wa = false;
  506. }
  507. GEM_BUG_ON(port[0].count == 0 &&
  508. !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
  509. }
  510. writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK,
  511. GEN8_CSB_WRITE_PTR(csb) << 8),
  512. csb_mmio);
  513. }
  514. if (execlists_elsp_ready(engine))
  515. execlists_dequeue(engine);
  516. intel_uncore_forcewake_put(dev_priv, engine->fw_domains);
  517. }
  518. static void execlists_submit_request(struct drm_i915_gem_request *request)
  519. {
  520. struct intel_engine_cs *engine = request->engine;
  521. spin_lock_bh(&engine->execlist_lock);
  522. list_add_tail(&request->execlist_link, &engine->execlist_queue);
  523. if (execlists_elsp_idle(engine))
  524. tasklet_hi_schedule(&engine->irq_tasklet);
  525. spin_unlock_bh(&engine->execlist_lock);
  526. }
  527. int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request)
  528. {
  529. struct intel_engine_cs *engine = request->engine;
  530. struct intel_context *ce = &request->ctx->engine[engine->id];
  531. int ret;
  532. /* Flush enough space to reduce the likelihood of waiting after
  533. * we start building the request - in which case we will just
  534. * have to repeat work.
  535. */
  536. request->reserved_space += EXECLISTS_REQUEST_SIZE;
  537. if (!ce->state) {
  538. ret = execlists_context_deferred_alloc(request->ctx, engine);
  539. if (ret)
  540. return ret;
  541. }
  542. request->ring = ce->ring;
  543. if (i915.enable_guc_submission) {
  544. /*
  545. * Check that the GuC has space for the request before
  546. * going any further, as the i915_add_request() call
  547. * later on mustn't fail ...
  548. */
  549. ret = i915_guc_wq_check_space(request);
  550. if (ret)
  551. return ret;
  552. }
  553. ret = intel_lr_context_pin(request->ctx, engine);
  554. if (ret)
  555. return ret;
  556. ret = intel_ring_begin(request, 0);
  557. if (ret)
  558. goto err_unpin;
  559. if (!ce->initialised) {
  560. ret = engine->init_context(request);
  561. if (ret)
  562. goto err_unpin;
  563. ce->initialised = true;
  564. }
  565. /* Note that after this point, we have committed to using
  566. * this request as it is being used to both track the
  567. * state of engine initialisation and liveness of the
  568. * golden renderstate above. Think twice before you try
  569. * to cancel/unwind this request now.
  570. */
  571. request->reserved_space -= EXECLISTS_REQUEST_SIZE;
  572. return 0;
  573. err_unpin:
  574. intel_lr_context_unpin(request->ctx, engine);
  575. return ret;
  576. }
  577. /*
  578. * intel_logical_ring_advance() - advance the tail and prepare for submission
  579. * @request: Request to advance the logical ringbuffer of.
  580. *
  581. * The tail is updated in our logical ringbuffer struct, not in the actual context. What
  582. * really happens during submission is that the context and current tail will be placed
  583. * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
  584. * point, the tail *inside* the context is updated and the ELSP written to.
  585. */
  586. static int
  587. intel_logical_ring_advance(struct drm_i915_gem_request *request)
  588. {
  589. struct intel_ring *ring = request->ring;
  590. struct intel_engine_cs *engine = request->engine;
  591. intel_ring_advance(ring);
  592. request->tail = ring->tail;
  593. /*
  594. * Here we add two extra NOOPs as padding to avoid
  595. * lite restore of a context with HEAD==TAIL.
  596. *
  597. * Caller must reserve WA_TAIL_DWORDS for us!
  598. */
  599. intel_ring_emit(ring, MI_NOOP);
  600. intel_ring_emit(ring, MI_NOOP);
  601. intel_ring_advance(ring);
  602. request->wa_tail = ring->tail;
  603. /* We keep the previous context alive until we retire the following
  604. * request. This ensures that any the context object is still pinned
  605. * for any residual writes the HW makes into it on the context switch
  606. * into the next object following the breadcrumb. Otherwise, we may
  607. * retire the context too early.
  608. */
  609. request->previous_context = engine->last_context;
  610. engine->last_context = request->ctx;
  611. return 0;
  612. }
  613. static int intel_lr_context_pin(struct i915_gem_context *ctx,
  614. struct intel_engine_cs *engine)
  615. {
  616. struct intel_context *ce = &ctx->engine[engine->id];
  617. void *vaddr;
  618. u32 *lrc_reg_state;
  619. int ret;
  620. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  621. if (ce->pin_count++)
  622. return 0;
  623. ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN,
  624. PIN_OFFSET_BIAS | GUC_WOPCM_TOP | PIN_GLOBAL);
  625. if (ret)
  626. goto err;
  627. vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
  628. if (IS_ERR(vaddr)) {
  629. ret = PTR_ERR(vaddr);
  630. goto unpin_vma;
  631. }
  632. lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  633. ret = intel_ring_pin(ce->ring);
  634. if (ret)
  635. goto unpin_map;
  636. intel_lr_context_descriptor_update(ctx, engine);
  637. lrc_reg_state[CTX_RING_BUFFER_START+1] =
  638. i915_ggtt_offset(ce->ring->vma);
  639. ce->lrc_reg_state = lrc_reg_state;
  640. ce->state->obj->dirty = true;
  641. /* Invalidate GuC TLB. */
  642. if (i915.enable_guc_submission) {
  643. struct drm_i915_private *dev_priv = ctx->i915;
  644. I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
  645. }
  646. i915_gem_context_get(ctx);
  647. return 0;
  648. unpin_map:
  649. i915_gem_object_unpin_map(ce->state->obj);
  650. unpin_vma:
  651. __i915_vma_unpin(ce->state);
  652. err:
  653. ce->pin_count = 0;
  654. return ret;
  655. }
  656. void intel_lr_context_unpin(struct i915_gem_context *ctx,
  657. struct intel_engine_cs *engine)
  658. {
  659. struct intel_context *ce = &ctx->engine[engine->id];
  660. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  661. GEM_BUG_ON(ce->pin_count == 0);
  662. if (--ce->pin_count)
  663. return;
  664. intel_ring_unpin(ce->ring);
  665. i915_gem_object_unpin_map(ce->state->obj);
  666. i915_vma_unpin(ce->state);
  667. i915_gem_context_put(ctx);
  668. }
  669. static int intel_logical_ring_workarounds_emit(struct drm_i915_gem_request *req)
  670. {
  671. int ret, i;
  672. struct intel_ring *ring = req->ring;
  673. struct i915_workarounds *w = &req->i915->workarounds;
  674. if (w->count == 0)
  675. return 0;
  676. ret = req->engine->emit_flush(req, EMIT_BARRIER);
  677. if (ret)
  678. return ret;
  679. ret = intel_ring_begin(req, w->count * 2 + 2);
  680. if (ret)
  681. return ret;
  682. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
  683. for (i = 0; i < w->count; i++) {
  684. intel_ring_emit_reg(ring, w->reg[i].addr);
  685. intel_ring_emit(ring, w->reg[i].value);
  686. }
  687. intel_ring_emit(ring, MI_NOOP);
  688. intel_ring_advance(ring);
  689. ret = req->engine->emit_flush(req, EMIT_BARRIER);
  690. if (ret)
  691. return ret;
  692. return 0;
  693. }
  694. #define wa_ctx_emit(batch, index, cmd) \
  695. do { \
  696. int __index = (index)++; \
  697. if (WARN_ON(__index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
  698. return -ENOSPC; \
  699. } \
  700. batch[__index] = (cmd); \
  701. } while (0)
  702. #define wa_ctx_emit_reg(batch, index, reg) \
  703. wa_ctx_emit((batch), (index), i915_mmio_reg_offset(reg))
  704. /*
  705. * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
  706. * PIPE_CONTROL instruction. This is required for the flush to happen correctly
  707. * but there is a slight complication as this is applied in WA batch where the
  708. * values are only initialized once so we cannot take register value at the
  709. * beginning and reuse it further; hence we save its value to memory, upload a
  710. * constant value with bit21 set and then we restore it back with the saved value.
  711. * To simplify the WA, a constant value is formed by using the default value
  712. * of this register. This shouldn't be a problem because we are only modifying
  713. * it for a short period and this batch in non-premptible. We can ofcourse
  714. * use additional instructions that read the actual value of the register
  715. * at that time and set our bit of interest but it makes the WA complicated.
  716. *
  717. * This WA is also required for Gen9 so extracting as a function avoids
  718. * code duplication.
  719. */
  720. static inline int gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine,
  721. uint32_t *batch,
  722. uint32_t index)
  723. {
  724. struct drm_i915_private *dev_priv = engine->i915;
  725. uint32_t l3sqc4_flush = (0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES);
  726. /*
  727. * WaDisableLSQCROPERFforOCL:skl,kbl
  728. * This WA is implemented in skl_init_clock_gating() but since
  729. * this batch updates GEN8_L3SQCREG4 with default value we need to
  730. * set this bit here to retain the WA during flush.
  731. */
  732. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0) ||
  733. IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
  734. l3sqc4_flush |= GEN8_LQSC_RO_PERF_DIS;
  735. wa_ctx_emit(batch, index, (MI_STORE_REGISTER_MEM_GEN8 |
  736. MI_SRM_LRM_GLOBAL_GTT));
  737. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  738. wa_ctx_emit(batch, index, i915_ggtt_offset(engine->scratch) + 256);
  739. wa_ctx_emit(batch, index, 0);
  740. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  741. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  742. wa_ctx_emit(batch, index, l3sqc4_flush);
  743. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  744. wa_ctx_emit(batch, index, (PIPE_CONTROL_CS_STALL |
  745. PIPE_CONTROL_DC_FLUSH_ENABLE));
  746. wa_ctx_emit(batch, index, 0);
  747. wa_ctx_emit(batch, index, 0);
  748. wa_ctx_emit(batch, index, 0);
  749. wa_ctx_emit(batch, index, 0);
  750. wa_ctx_emit(batch, index, (MI_LOAD_REGISTER_MEM_GEN8 |
  751. MI_SRM_LRM_GLOBAL_GTT));
  752. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  753. wa_ctx_emit(batch, index, i915_ggtt_offset(engine->scratch) + 256);
  754. wa_ctx_emit(batch, index, 0);
  755. return index;
  756. }
  757. static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
  758. uint32_t offset,
  759. uint32_t start_alignment)
  760. {
  761. return wa_ctx->offset = ALIGN(offset, start_alignment);
  762. }
  763. static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
  764. uint32_t offset,
  765. uint32_t size_alignment)
  766. {
  767. wa_ctx->size = offset - wa_ctx->offset;
  768. WARN(wa_ctx->size % size_alignment,
  769. "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
  770. wa_ctx->size, size_alignment);
  771. return 0;
  772. }
  773. /*
  774. * Typically we only have one indirect_ctx and per_ctx batch buffer which are
  775. * initialized at the beginning and shared across all contexts but this field
  776. * helps us to have multiple batches at different offsets and select them based
  777. * on a criteria. At the moment this batch always start at the beginning of the page
  778. * and at this point we don't have multiple wa_ctx batch buffers.
  779. *
  780. * The number of WA applied are not known at the beginning; we use this field
  781. * to return the no of DWORDS written.
  782. *
  783. * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
  784. * so it adds NOOPs as padding to make it cacheline aligned.
  785. * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
  786. * makes a complete batch buffer.
  787. */
  788. static int gen8_init_indirectctx_bb(struct intel_engine_cs *engine,
  789. struct i915_wa_ctx_bb *wa_ctx,
  790. uint32_t *batch,
  791. uint32_t *offset)
  792. {
  793. uint32_t scratch_addr;
  794. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  795. /* WaDisableCtxRestoreArbitration:bdw,chv */
  796. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  797. /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
  798. if (IS_BROADWELL(engine->i915)) {
  799. int rc = gen8_emit_flush_coherentl3_wa(engine, batch, index);
  800. if (rc < 0)
  801. return rc;
  802. index = rc;
  803. }
  804. /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
  805. /* Actual scratch location is at 128 bytes offset */
  806. scratch_addr = i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  807. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  808. wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
  809. PIPE_CONTROL_GLOBAL_GTT_IVB |
  810. PIPE_CONTROL_CS_STALL |
  811. PIPE_CONTROL_QW_WRITE));
  812. wa_ctx_emit(batch, index, scratch_addr);
  813. wa_ctx_emit(batch, index, 0);
  814. wa_ctx_emit(batch, index, 0);
  815. wa_ctx_emit(batch, index, 0);
  816. /* Pad to end of cacheline */
  817. while (index % CACHELINE_DWORDS)
  818. wa_ctx_emit(batch, index, MI_NOOP);
  819. /*
  820. * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
  821. * execution depends on the length specified in terms of cache lines
  822. * in the register CTX_RCS_INDIRECT_CTX
  823. */
  824. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  825. }
  826. /*
  827. * This batch is started immediately after indirect_ctx batch. Since we ensure
  828. * that indirect_ctx ends on a cacheline this batch is aligned automatically.
  829. *
  830. * The number of DWORDS written are returned using this field.
  831. *
  832. * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
  833. * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
  834. */
  835. static int gen8_init_perctx_bb(struct intel_engine_cs *engine,
  836. struct i915_wa_ctx_bb *wa_ctx,
  837. uint32_t *batch,
  838. uint32_t *offset)
  839. {
  840. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  841. /* WaDisableCtxRestoreArbitration:bdw,chv */
  842. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  843. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  844. return wa_ctx_end(wa_ctx, *offset = index, 1);
  845. }
  846. static int gen9_init_indirectctx_bb(struct intel_engine_cs *engine,
  847. struct i915_wa_ctx_bb *wa_ctx,
  848. uint32_t *batch,
  849. uint32_t *offset)
  850. {
  851. int ret;
  852. struct drm_i915_private *dev_priv = engine->i915;
  853. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  854. /* WaDisableCtxRestoreArbitration:skl,bxt */
  855. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_D0) ||
  856. IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
  857. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  858. /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt */
  859. ret = gen8_emit_flush_coherentl3_wa(engine, batch, index);
  860. if (ret < 0)
  861. return ret;
  862. index = ret;
  863. /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl */
  864. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  865. wa_ctx_emit_reg(batch, index, COMMON_SLICE_CHICKEN2);
  866. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(
  867. GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE));
  868. wa_ctx_emit(batch, index, MI_NOOP);
  869. /* WaClearSlmSpaceAtContextSwitch:kbl */
  870. /* Actual scratch location is at 128 bytes offset */
  871. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_A0)) {
  872. u32 scratch_addr =
  873. i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  874. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  875. wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
  876. PIPE_CONTROL_GLOBAL_GTT_IVB |
  877. PIPE_CONTROL_CS_STALL |
  878. PIPE_CONTROL_QW_WRITE));
  879. wa_ctx_emit(batch, index, scratch_addr);
  880. wa_ctx_emit(batch, index, 0);
  881. wa_ctx_emit(batch, index, 0);
  882. wa_ctx_emit(batch, index, 0);
  883. }
  884. /* WaMediaPoolStateCmdInWABB:bxt */
  885. if (HAS_POOLED_EU(engine->i915)) {
  886. /*
  887. * EU pool configuration is setup along with golden context
  888. * during context initialization. This value depends on
  889. * device type (2x6 or 3x6) and needs to be updated based
  890. * on which subslice is disabled especially for 2x6
  891. * devices, however it is safe to load default
  892. * configuration of 3x6 device instead of masking off
  893. * corresponding bits because HW ignores bits of a disabled
  894. * subslice and drops down to appropriate config. Please
  895. * see render_state_setup() in i915_gem_render_state.c for
  896. * possible configurations, to avoid duplication they are
  897. * not shown here again.
  898. */
  899. u32 eu_pool_config = 0x00777000;
  900. wa_ctx_emit(batch, index, GEN9_MEDIA_POOL_STATE);
  901. wa_ctx_emit(batch, index, GEN9_MEDIA_POOL_ENABLE);
  902. wa_ctx_emit(batch, index, eu_pool_config);
  903. wa_ctx_emit(batch, index, 0);
  904. wa_ctx_emit(batch, index, 0);
  905. wa_ctx_emit(batch, index, 0);
  906. }
  907. /* Pad to end of cacheline */
  908. while (index % CACHELINE_DWORDS)
  909. wa_ctx_emit(batch, index, MI_NOOP);
  910. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  911. }
  912. static int gen9_init_perctx_bb(struct intel_engine_cs *engine,
  913. struct i915_wa_ctx_bb *wa_ctx,
  914. uint32_t *batch,
  915. uint32_t *offset)
  916. {
  917. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  918. /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
  919. if (IS_SKL_REVID(engine->i915, 0, SKL_REVID_B0) ||
  920. IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1)) {
  921. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  922. wa_ctx_emit_reg(batch, index, GEN9_SLICE_COMMON_ECO_CHICKEN0);
  923. wa_ctx_emit(batch, index,
  924. _MASKED_BIT_ENABLE(DISABLE_PIXEL_MASK_CAMMING));
  925. wa_ctx_emit(batch, index, MI_NOOP);
  926. }
  927. /* WaClearTdlStateAckDirtyBits:bxt */
  928. if (IS_BXT_REVID(engine->i915, 0, BXT_REVID_B0)) {
  929. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(4));
  930. wa_ctx_emit_reg(batch, index, GEN8_STATE_ACK);
  931. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  932. wa_ctx_emit_reg(batch, index, GEN9_STATE_ACK_SLICE1);
  933. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  934. wa_ctx_emit_reg(batch, index, GEN9_STATE_ACK_SLICE2);
  935. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  936. wa_ctx_emit_reg(batch, index, GEN7_ROW_CHICKEN2);
  937. /* dummy write to CS, mask bits are 0 to ensure the register is not modified */
  938. wa_ctx_emit(batch, index, 0x0);
  939. wa_ctx_emit(batch, index, MI_NOOP);
  940. }
  941. /* WaDisableCtxRestoreArbitration:skl,bxt */
  942. if (IS_SKL_REVID(engine->i915, 0, SKL_REVID_D0) ||
  943. IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1))
  944. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  945. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  946. return wa_ctx_end(wa_ctx, *offset = index, 1);
  947. }
  948. static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *engine, u32 size)
  949. {
  950. struct drm_i915_gem_object *obj;
  951. struct i915_vma *vma;
  952. int err;
  953. obj = i915_gem_object_create(&engine->i915->drm, PAGE_ALIGN(size));
  954. if (IS_ERR(obj))
  955. return PTR_ERR(obj);
  956. vma = i915_vma_create(obj, &engine->i915->ggtt.base, NULL);
  957. if (IS_ERR(vma)) {
  958. err = PTR_ERR(vma);
  959. goto err;
  960. }
  961. err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
  962. if (err)
  963. goto err;
  964. engine->wa_ctx.vma = vma;
  965. return 0;
  966. err:
  967. i915_gem_object_put(obj);
  968. return err;
  969. }
  970. static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *engine)
  971. {
  972. i915_vma_unpin_and_release(&engine->wa_ctx.vma);
  973. }
  974. static int intel_init_workaround_bb(struct intel_engine_cs *engine)
  975. {
  976. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  977. uint32_t *batch;
  978. uint32_t offset;
  979. struct page *page;
  980. int ret;
  981. WARN_ON(engine->id != RCS);
  982. /* update this when WA for higher Gen are added */
  983. if (INTEL_GEN(engine->i915) > 9) {
  984. DRM_ERROR("WA batch buffer is not initialized for Gen%d\n",
  985. INTEL_GEN(engine->i915));
  986. return 0;
  987. }
  988. /* some WA perform writes to scratch page, ensure it is valid */
  989. if (!engine->scratch) {
  990. DRM_ERROR("scratch page not allocated for %s\n", engine->name);
  991. return -EINVAL;
  992. }
  993. ret = lrc_setup_wa_ctx_obj(engine, PAGE_SIZE);
  994. if (ret) {
  995. DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
  996. return ret;
  997. }
  998. page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
  999. batch = kmap_atomic(page);
  1000. offset = 0;
  1001. if (IS_GEN8(engine->i915)) {
  1002. ret = gen8_init_indirectctx_bb(engine,
  1003. &wa_ctx->indirect_ctx,
  1004. batch,
  1005. &offset);
  1006. if (ret)
  1007. goto out;
  1008. ret = gen8_init_perctx_bb(engine,
  1009. &wa_ctx->per_ctx,
  1010. batch,
  1011. &offset);
  1012. if (ret)
  1013. goto out;
  1014. } else if (IS_GEN9(engine->i915)) {
  1015. ret = gen9_init_indirectctx_bb(engine,
  1016. &wa_ctx->indirect_ctx,
  1017. batch,
  1018. &offset);
  1019. if (ret)
  1020. goto out;
  1021. ret = gen9_init_perctx_bb(engine,
  1022. &wa_ctx->per_ctx,
  1023. batch,
  1024. &offset);
  1025. if (ret)
  1026. goto out;
  1027. }
  1028. out:
  1029. kunmap_atomic(batch);
  1030. if (ret)
  1031. lrc_destroy_wa_ctx_obj(engine);
  1032. return ret;
  1033. }
  1034. static void lrc_init_hws(struct intel_engine_cs *engine)
  1035. {
  1036. struct drm_i915_private *dev_priv = engine->i915;
  1037. I915_WRITE(RING_HWS_PGA(engine->mmio_base),
  1038. engine->status_page.ggtt_offset);
  1039. POSTING_READ(RING_HWS_PGA(engine->mmio_base));
  1040. }
  1041. static int gen8_init_common_ring(struct intel_engine_cs *engine)
  1042. {
  1043. struct drm_i915_private *dev_priv = engine->i915;
  1044. int ret;
  1045. ret = intel_mocs_init_engine(engine);
  1046. if (ret)
  1047. return ret;
  1048. lrc_init_hws(engine);
  1049. intel_engine_reset_irq(engine);
  1050. I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
  1051. I915_WRITE(RING_MODE_GEN7(engine),
  1052. _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
  1053. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  1054. DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
  1055. intel_engine_init_hangcheck(engine);
  1056. if (!execlists_elsp_idle(engine))
  1057. execlists_submit_ports(engine);
  1058. return 0;
  1059. }
  1060. static int gen8_init_render_ring(struct intel_engine_cs *engine)
  1061. {
  1062. struct drm_i915_private *dev_priv = engine->i915;
  1063. int ret;
  1064. ret = gen8_init_common_ring(engine);
  1065. if (ret)
  1066. return ret;
  1067. /* We need to disable the AsyncFlip performance optimisations in order
  1068. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  1069. * programmed to '1' on all products.
  1070. *
  1071. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  1072. */
  1073. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  1074. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  1075. return init_workarounds_ring(engine);
  1076. }
  1077. static int gen9_init_render_ring(struct intel_engine_cs *engine)
  1078. {
  1079. int ret;
  1080. ret = gen8_init_common_ring(engine);
  1081. if (ret)
  1082. return ret;
  1083. return init_workarounds_ring(engine);
  1084. }
  1085. static void reset_common_ring(struct intel_engine_cs *engine,
  1086. struct drm_i915_gem_request *request)
  1087. {
  1088. struct drm_i915_private *dev_priv = engine->i915;
  1089. struct execlist_port *port = engine->execlist_port;
  1090. struct intel_context *ce = &request->ctx->engine[engine->id];
  1091. /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
  1092. ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
  1093. request->ring->head = request->postfix;
  1094. request->ring->last_retired_head = -1;
  1095. intel_ring_update_space(request->ring);
  1096. if (i915.enable_guc_submission)
  1097. return;
  1098. /* Catch up with any missed context-switch interrupts */
  1099. I915_WRITE(RING_CONTEXT_STATUS_PTR(engine), _MASKED_FIELD(0xffff, 0));
  1100. if (request->ctx != port[0].request->ctx) {
  1101. i915_gem_request_put(port[0].request);
  1102. port[0] = port[1];
  1103. memset(&port[1], 0, sizeof(port[1]));
  1104. }
  1105. /* CS is stopped, and we will resubmit both ports on resume */
  1106. GEM_BUG_ON(request->ctx != port[0].request->ctx);
  1107. port[0].count = 0;
  1108. port[1].count = 0;
  1109. }
  1110. static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
  1111. {
  1112. struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
  1113. struct intel_ring *ring = req->ring;
  1114. struct intel_engine_cs *engine = req->engine;
  1115. const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
  1116. int i, ret;
  1117. ret = intel_ring_begin(req, num_lri_cmds * 2 + 2);
  1118. if (ret)
  1119. return ret;
  1120. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(num_lri_cmds));
  1121. for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
  1122. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  1123. intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, i));
  1124. intel_ring_emit(ring, upper_32_bits(pd_daddr));
  1125. intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, i));
  1126. intel_ring_emit(ring, lower_32_bits(pd_daddr));
  1127. }
  1128. intel_ring_emit(ring, MI_NOOP);
  1129. intel_ring_advance(ring);
  1130. return 0;
  1131. }
  1132. static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
  1133. u64 offset, u32 len,
  1134. unsigned int dispatch_flags)
  1135. {
  1136. struct intel_ring *ring = req->ring;
  1137. bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
  1138. int ret;
  1139. /* Don't rely in hw updating PDPs, specially in lite-restore.
  1140. * Ideally, we should set Force PD Restore in ctx descriptor,
  1141. * but we can't. Force Restore would be a second option, but
  1142. * it is unsafe in case of lite-restore (because the ctx is
  1143. * not idle). PML4 is allocated during ppgtt init so this is
  1144. * not needed in 48-bit.*/
  1145. if (req->ctx->ppgtt &&
  1146. (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings)) {
  1147. if (!USES_FULL_48BIT_PPGTT(req->i915) &&
  1148. !intel_vgpu_active(req->i915)) {
  1149. ret = intel_logical_ring_emit_pdps(req);
  1150. if (ret)
  1151. return ret;
  1152. }
  1153. req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
  1154. }
  1155. ret = intel_ring_begin(req, 4);
  1156. if (ret)
  1157. return ret;
  1158. /* FIXME(BDW): Address space and security selectors. */
  1159. intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 |
  1160. (ppgtt<<8) |
  1161. (dispatch_flags & I915_DISPATCH_RS ?
  1162. MI_BATCH_RESOURCE_STREAMER : 0));
  1163. intel_ring_emit(ring, lower_32_bits(offset));
  1164. intel_ring_emit(ring, upper_32_bits(offset));
  1165. intel_ring_emit(ring, MI_NOOP);
  1166. intel_ring_advance(ring);
  1167. return 0;
  1168. }
  1169. static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
  1170. {
  1171. struct drm_i915_private *dev_priv = engine->i915;
  1172. I915_WRITE_IMR(engine,
  1173. ~(engine->irq_enable_mask | engine->irq_keep_mask));
  1174. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1175. }
  1176. static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
  1177. {
  1178. struct drm_i915_private *dev_priv = engine->i915;
  1179. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  1180. }
  1181. static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
  1182. {
  1183. struct intel_ring *ring = request->ring;
  1184. u32 cmd;
  1185. int ret;
  1186. ret = intel_ring_begin(request, 4);
  1187. if (ret)
  1188. return ret;
  1189. cmd = MI_FLUSH_DW + 1;
  1190. /* We always require a command barrier so that subsequent
  1191. * commands, such as breadcrumb interrupts, are strictly ordered
  1192. * wrt the contents of the write cache being flushed to memory
  1193. * (and thus being coherent from the CPU).
  1194. */
  1195. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1196. if (mode & EMIT_INVALIDATE) {
  1197. cmd |= MI_INVALIDATE_TLB;
  1198. if (request->engine->id == VCS)
  1199. cmd |= MI_INVALIDATE_BSD;
  1200. }
  1201. intel_ring_emit(ring, cmd);
  1202. intel_ring_emit(ring,
  1203. I915_GEM_HWS_SCRATCH_ADDR |
  1204. MI_FLUSH_DW_USE_GTT);
  1205. intel_ring_emit(ring, 0); /* upper addr */
  1206. intel_ring_emit(ring, 0); /* value */
  1207. intel_ring_advance(ring);
  1208. return 0;
  1209. }
  1210. static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
  1211. u32 mode)
  1212. {
  1213. struct intel_ring *ring = request->ring;
  1214. struct intel_engine_cs *engine = request->engine;
  1215. u32 scratch_addr =
  1216. i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  1217. bool vf_flush_wa = false, dc_flush_wa = false;
  1218. u32 flags = 0;
  1219. int ret;
  1220. int len;
  1221. flags |= PIPE_CONTROL_CS_STALL;
  1222. if (mode & EMIT_FLUSH) {
  1223. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  1224. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  1225. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  1226. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  1227. }
  1228. if (mode & EMIT_INVALIDATE) {
  1229. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  1230. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  1231. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  1232. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1233. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  1234. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  1235. flags |= PIPE_CONTROL_QW_WRITE;
  1236. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1237. /*
  1238. * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
  1239. * pipe control.
  1240. */
  1241. if (IS_GEN9(request->i915))
  1242. vf_flush_wa = true;
  1243. /* WaForGAMHang:kbl */
  1244. if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
  1245. dc_flush_wa = true;
  1246. }
  1247. len = 6;
  1248. if (vf_flush_wa)
  1249. len += 6;
  1250. if (dc_flush_wa)
  1251. len += 12;
  1252. ret = intel_ring_begin(request, len);
  1253. if (ret)
  1254. return ret;
  1255. if (vf_flush_wa) {
  1256. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1257. intel_ring_emit(ring, 0);
  1258. intel_ring_emit(ring, 0);
  1259. intel_ring_emit(ring, 0);
  1260. intel_ring_emit(ring, 0);
  1261. intel_ring_emit(ring, 0);
  1262. }
  1263. if (dc_flush_wa) {
  1264. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1265. intel_ring_emit(ring, PIPE_CONTROL_DC_FLUSH_ENABLE);
  1266. intel_ring_emit(ring, 0);
  1267. intel_ring_emit(ring, 0);
  1268. intel_ring_emit(ring, 0);
  1269. intel_ring_emit(ring, 0);
  1270. }
  1271. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1272. intel_ring_emit(ring, flags);
  1273. intel_ring_emit(ring, scratch_addr);
  1274. intel_ring_emit(ring, 0);
  1275. intel_ring_emit(ring, 0);
  1276. intel_ring_emit(ring, 0);
  1277. if (dc_flush_wa) {
  1278. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1279. intel_ring_emit(ring, PIPE_CONTROL_CS_STALL);
  1280. intel_ring_emit(ring, 0);
  1281. intel_ring_emit(ring, 0);
  1282. intel_ring_emit(ring, 0);
  1283. intel_ring_emit(ring, 0);
  1284. }
  1285. intel_ring_advance(ring);
  1286. return 0;
  1287. }
  1288. static void bxt_a_seqno_barrier(struct intel_engine_cs *engine)
  1289. {
  1290. /*
  1291. * On BXT A steppings there is a HW coherency issue whereby the
  1292. * MI_STORE_DATA_IMM storing the completed request's seqno
  1293. * occasionally doesn't invalidate the CPU cache. Work around this by
  1294. * clflushing the corresponding cacheline whenever the caller wants
  1295. * the coherency to be guaranteed. Note that this cacheline is known
  1296. * to be clean at this point, since we only write it in
  1297. * bxt_a_set_seqno(), where we also do a clflush after the write. So
  1298. * this clflush in practice becomes an invalidate operation.
  1299. */
  1300. intel_flush_status_page(engine, I915_GEM_HWS_INDEX);
  1301. }
  1302. /*
  1303. * Reserve space for 2 NOOPs at the end of each request to be
  1304. * used as a workaround for not being allowed to do lite
  1305. * restore with HEAD==TAIL (WaIdleLiteRestore).
  1306. */
  1307. #define WA_TAIL_DWORDS 2
  1308. static int gen8_emit_request(struct drm_i915_gem_request *request)
  1309. {
  1310. struct intel_ring *ring = request->ring;
  1311. int ret;
  1312. ret = intel_ring_begin(request, 6 + WA_TAIL_DWORDS);
  1313. if (ret)
  1314. return ret;
  1315. /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
  1316. BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
  1317. intel_ring_emit(ring, (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW);
  1318. intel_ring_emit(ring,
  1319. intel_hws_seqno_address(request->engine) |
  1320. MI_FLUSH_DW_USE_GTT);
  1321. intel_ring_emit(ring, 0);
  1322. intel_ring_emit(ring, request->fence.seqno);
  1323. intel_ring_emit(ring, MI_USER_INTERRUPT);
  1324. intel_ring_emit(ring, MI_NOOP);
  1325. return intel_logical_ring_advance(request);
  1326. }
  1327. static int gen8_emit_request_render(struct drm_i915_gem_request *request)
  1328. {
  1329. struct intel_ring *ring = request->ring;
  1330. int ret;
  1331. ret = intel_ring_begin(request, 8 + WA_TAIL_DWORDS);
  1332. if (ret)
  1333. return ret;
  1334. /* We're using qword write, seqno should be aligned to 8 bytes. */
  1335. BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
  1336. /* w/a for post sync ops following a GPGPU operation we
  1337. * need a prior CS_STALL, which is emitted by the flush
  1338. * following the batch.
  1339. */
  1340. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1341. intel_ring_emit(ring,
  1342. (PIPE_CONTROL_GLOBAL_GTT_IVB |
  1343. PIPE_CONTROL_CS_STALL |
  1344. PIPE_CONTROL_QW_WRITE));
  1345. intel_ring_emit(ring, intel_hws_seqno_address(request->engine));
  1346. intel_ring_emit(ring, 0);
  1347. intel_ring_emit(ring, i915_gem_request_get_seqno(request));
  1348. /* We're thrashing one dword of HWS. */
  1349. intel_ring_emit(ring, 0);
  1350. intel_ring_emit(ring, MI_USER_INTERRUPT);
  1351. intel_ring_emit(ring, MI_NOOP);
  1352. return intel_logical_ring_advance(request);
  1353. }
  1354. static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
  1355. {
  1356. int ret;
  1357. ret = intel_logical_ring_workarounds_emit(req);
  1358. if (ret)
  1359. return ret;
  1360. ret = intel_rcs_context_init_mocs(req);
  1361. /*
  1362. * Failing to program the MOCS is non-fatal.The system will not
  1363. * run at peak performance. So generate an error and carry on.
  1364. */
  1365. if (ret)
  1366. DRM_ERROR("MOCS failed to program: expect performance issues.\n");
  1367. return i915_gem_render_state_init(req);
  1368. }
  1369. /**
  1370. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1371. * @engine: Engine Command Streamer.
  1372. */
  1373. void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
  1374. {
  1375. struct drm_i915_private *dev_priv;
  1376. if (!intel_engine_initialized(engine))
  1377. return;
  1378. /*
  1379. * Tasklet cannot be active at this point due intel_mark_active/idle
  1380. * so this is just for documentation.
  1381. */
  1382. if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->irq_tasklet.state)))
  1383. tasklet_kill(&engine->irq_tasklet);
  1384. dev_priv = engine->i915;
  1385. if (engine->buffer) {
  1386. WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1387. }
  1388. if (engine->cleanup)
  1389. engine->cleanup(engine);
  1390. intel_engine_cleanup_common(engine);
  1391. if (engine->status_page.vma) {
  1392. i915_gem_object_unpin_map(engine->status_page.vma->obj);
  1393. engine->status_page.vma = NULL;
  1394. }
  1395. intel_lr_context_unpin(dev_priv->kernel_context, engine);
  1396. lrc_destroy_wa_ctx_obj(engine);
  1397. engine->i915 = NULL;
  1398. }
  1399. void intel_execlists_enable_submission(struct drm_i915_private *dev_priv)
  1400. {
  1401. struct intel_engine_cs *engine;
  1402. for_each_engine(engine, dev_priv)
  1403. engine->submit_request = execlists_submit_request;
  1404. }
  1405. static void
  1406. logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  1407. {
  1408. /* Default vfuncs which can be overriden by each engine. */
  1409. engine->init_hw = gen8_init_common_ring;
  1410. engine->reset_hw = reset_common_ring;
  1411. engine->emit_flush = gen8_emit_flush;
  1412. engine->emit_request = gen8_emit_request;
  1413. engine->submit_request = execlists_submit_request;
  1414. engine->irq_enable = gen8_logical_ring_enable_irq;
  1415. engine->irq_disable = gen8_logical_ring_disable_irq;
  1416. engine->emit_bb_start = gen8_emit_bb_start;
  1417. if (IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1))
  1418. engine->irq_seqno_barrier = bxt_a_seqno_barrier;
  1419. }
  1420. static inline void
  1421. logical_ring_default_irqs(struct intel_engine_cs *engine)
  1422. {
  1423. unsigned shift = engine->irq_shift;
  1424. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
  1425. engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
  1426. }
  1427. static int
  1428. lrc_setup_hws(struct intel_engine_cs *engine, struct i915_vma *vma)
  1429. {
  1430. const int hws_offset = LRC_PPHWSP_PN * PAGE_SIZE;
  1431. void *hws;
  1432. /* The HWSP is part of the default context object in LRC mode. */
  1433. hws = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  1434. if (IS_ERR(hws))
  1435. return PTR_ERR(hws);
  1436. engine->status_page.page_addr = hws + hws_offset;
  1437. engine->status_page.ggtt_offset = i915_ggtt_offset(vma) + hws_offset;
  1438. engine->status_page.vma = vma;
  1439. return 0;
  1440. }
  1441. static void
  1442. logical_ring_setup(struct intel_engine_cs *engine)
  1443. {
  1444. struct drm_i915_private *dev_priv = engine->i915;
  1445. enum forcewake_domains fw_domains;
  1446. intel_engine_setup_common(engine);
  1447. /* Intentionally left blank. */
  1448. engine->buffer = NULL;
  1449. fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
  1450. RING_ELSP(engine),
  1451. FW_REG_WRITE);
  1452. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1453. RING_CONTEXT_STATUS_PTR(engine),
  1454. FW_REG_READ | FW_REG_WRITE);
  1455. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1456. RING_CONTEXT_STATUS_BUF_BASE(engine),
  1457. FW_REG_READ);
  1458. engine->fw_domains = fw_domains;
  1459. tasklet_init(&engine->irq_tasklet,
  1460. intel_lrc_irq_handler, (unsigned long)engine);
  1461. logical_ring_init_platform_invariants(engine);
  1462. logical_ring_default_vfuncs(engine);
  1463. logical_ring_default_irqs(engine);
  1464. }
  1465. static int
  1466. logical_ring_init(struct intel_engine_cs *engine)
  1467. {
  1468. struct i915_gem_context *dctx = engine->i915->kernel_context;
  1469. int ret;
  1470. ret = intel_engine_init_common(engine);
  1471. if (ret)
  1472. goto error;
  1473. ret = execlists_context_deferred_alloc(dctx, engine);
  1474. if (ret)
  1475. goto error;
  1476. /* As this is the default context, always pin it */
  1477. ret = intel_lr_context_pin(dctx, engine);
  1478. if (ret) {
  1479. DRM_ERROR("Failed to pin context for %s: %d\n",
  1480. engine->name, ret);
  1481. goto error;
  1482. }
  1483. /* And setup the hardware status page. */
  1484. ret = lrc_setup_hws(engine, dctx->engine[engine->id].state);
  1485. if (ret) {
  1486. DRM_ERROR("Failed to set up hws %s: %d\n", engine->name, ret);
  1487. goto error;
  1488. }
  1489. return 0;
  1490. error:
  1491. intel_logical_ring_cleanup(engine);
  1492. return ret;
  1493. }
  1494. int logical_render_ring_init(struct intel_engine_cs *engine)
  1495. {
  1496. struct drm_i915_private *dev_priv = engine->i915;
  1497. int ret;
  1498. logical_ring_setup(engine);
  1499. if (HAS_L3_DPF(dev_priv))
  1500. engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1501. /* Override some for render ring. */
  1502. if (INTEL_GEN(dev_priv) >= 9)
  1503. engine->init_hw = gen9_init_render_ring;
  1504. else
  1505. engine->init_hw = gen8_init_render_ring;
  1506. engine->init_context = gen8_init_rcs_context;
  1507. engine->emit_flush = gen8_emit_flush_render;
  1508. engine->emit_request = gen8_emit_request_render;
  1509. ret = intel_engine_create_scratch(engine, 4096);
  1510. if (ret)
  1511. return ret;
  1512. ret = intel_init_workaround_bb(engine);
  1513. if (ret) {
  1514. /*
  1515. * We continue even if we fail to initialize WA batch
  1516. * because we only expect rare glitches but nothing
  1517. * critical to prevent us from using GPU
  1518. */
  1519. DRM_ERROR("WA batch buffer initialization failed: %d\n",
  1520. ret);
  1521. }
  1522. ret = logical_ring_init(engine);
  1523. if (ret) {
  1524. lrc_destroy_wa_ctx_obj(engine);
  1525. }
  1526. return ret;
  1527. }
  1528. int logical_xcs_ring_init(struct intel_engine_cs *engine)
  1529. {
  1530. logical_ring_setup(engine);
  1531. return logical_ring_init(engine);
  1532. }
  1533. static u32
  1534. make_rpcs(struct drm_i915_private *dev_priv)
  1535. {
  1536. u32 rpcs = 0;
  1537. /*
  1538. * No explicit RPCS request is needed to ensure full
  1539. * slice/subslice/EU enablement prior to Gen9.
  1540. */
  1541. if (INTEL_GEN(dev_priv) < 9)
  1542. return 0;
  1543. /*
  1544. * Starting in Gen9, render power gating can leave
  1545. * slice/subslice/EU in a partially enabled state. We
  1546. * must make an explicit request through RPCS for full
  1547. * enablement.
  1548. */
  1549. if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
  1550. rpcs |= GEN8_RPCS_S_CNT_ENABLE;
  1551. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
  1552. GEN8_RPCS_S_CNT_SHIFT;
  1553. rpcs |= GEN8_RPCS_ENABLE;
  1554. }
  1555. if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
  1556. rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
  1557. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
  1558. GEN8_RPCS_SS_CNT_SHIFT;
  1559. rpcs |= GEN8_RPCS_ENABLE;
  1560. }
  1561. if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
  1562. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  1563. GEN8_RPCS_EU_MIN_SHIFT;
  1564. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  1565. GEN8_RPCS_EU_MAX_SHIFT;
  1566. rpcs |= GEN8_RPCS_ENABLE;
  1567. }
  1568. return rpcs;
  1569. }
  1570. static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
  1571. {
  1572. u32 indirect_ctx_offset;
  1573. switch (INTEL_GEN(engine->i915)) {
  1574. default:
  1575. MISSING_CASE(INTEL_GEN(engine->i915));
  1576. /* fall through */
  1577. case 9:
  1578. indirect_ctx_offset =
  1579. GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1580. break;
  1581. case 8:
  1582. indirect_ctx_offset =
  1583. GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1584. break;
  1585. }
  1586. return indirect_ctx_offset;
  1587. }
  1588. static int
  1589. populate_lr_context(struct i915_gem_context *ctx,
  1590. struct drm_i915_gem_object *ctx_obj,
  1591. struct intel_engine_cs *engine,
  1592. struct intel_ring *ring)
  1593. {
  1594. struct drm_i915_private *dev_priv = ctx->i915;
  1595. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
  1596. void *vaddr;
  1597. u32 *reg_state;
  1598. int ret;
  1599. if (!ppgtt)
  1600. ppgtt = dev_priv->mm.aliasing_ppgtt;
  1601. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1602. if (ret) {
  1603. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1604. return ret;
  1605. }
  1606. vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
  1607. if (IS_ERR(vaddr)) {
  1608. ret = PTR_ERR(vaddr);
  1609. DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
  1610. return ret;
  1611. }
  1612. ctx_obj->dirty = true;
  1613. /* The second page of the context object contains some fields which must
  1614. * be set up prior to the first execution. */
  1615. reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  1616. /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
  1617. * commands followed by (reg, value) pairs. The values we are setting here are
  1618. * only for the first context restore: on a subsequent save, the GPU will
  1619. * recreate this batchbuffer with new values (including all the missing
  1620. * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
  1621. reg_state[CTX_LRI_HEADER_0] =
  1622. MI_LOAD_REGISTER_IMM(engine->id == RCS ? 14 : 11) | MI_LRI_FORCE_POSTED;
  1623. ASSIGN_CTX_REG(reg_state, CTX_CONTEXT_CONTROL,
  1624. RING_CONTEXT_CONTROL(engine),
  1625. _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
  1626. CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
  1627. (HAS_RESOURCE_STREAMER(dev_priv) ?
  1628. CTX_CTRL_RS_CTX_ENABLE : 0)));
  1629. ASSIGN_CTX_REG(reg_state, CTX_RING_HEAD, RING_HEAD(engine->mmio_base),
  1630. 0);
  1631. ASSIGN_CTX_REG(reg_state, CTX_RING_TAIL, RING_TAIL(engine->mmio_base),
  1632. 0);
  1633. /* Ring buffer start address is not known until the buffer is pinned.
  1634. * It is written to the context image in execlists_update_context()
  1635. */
  1636. ASSIGN_CTX_REG(reg_state, CTX_RING_BUFFER_START,
  1637. RING_START(engine->mmio_base), 0);
  1638. ASSIGN_CTX_REG(reg_state, CTX_RING_BUFFER_CONTROL,
  1639. RING_CTL(engine->mmio_base),
  1640. ((ring->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID);
  1641. ASSIGN_CTX_REG(reg_state, CTX_BB_HEAD_U,
  1642. RING_BBADDR_UDW(engine->mmio_base), 0);
  1643. ASSIGN_CTX_REG(reg_state, CTX_BB_HEAD_L,
  1644. RING_BBADDR(engine->mmio_base), 0);
  1645. ASSIGN_CTX_REG(reg_state, CTX_BB_STATE,
  1646. RING_BBSTATE(engine->mmio_base),
  1647. RING_BB_PPGTT);
  1648. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_HEAD_U,
  1649. RING_SBBADDR_UDW(engine->mmio_base), 0);
  1650. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_HEAD_L,
  1651. RING_SBBADDR(engine->mmio_base), 0);
  1652. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_STATE,
  1653. RING_SBBSTATE(engine->mmio_base), 0);
  1654. if (engine->id == RCS) {
  1655. ASSIGN_CTX_REG(reg_state, CTX_BB_PER_CTX_PTR,
  1656. RING_BB_PER_CTX_PTR(engine->mmio_base), 0);
  1657. ASSIGN_CTX_REG(reg_state, CTX_RCS_INDIRECT_CTX,
  1658. RING_INDIRECT_CTX(engine->mmio_base), 0);
  1659. ASSIGN_CTX_REG(reg_state, CTX_RCS_INDIRECT_CTX_OFFSET,
  1660. RING_INDIRECT_CTX_OFFSET(engine->mmio_base), 0);
  1661. if (engine->wa_ctx.vma) {
  1662. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1663. u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
  1664. reg_state[CTX_RCS_INDIRECT_CTX+1] =
  1665. (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
  1666. (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
  1667. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
  1668. intel_lr_indirect_ctx_offset(engine) << 6;
  1669. reg_state[CTX_BB_PER_CTX_PTR+1] =
  1670. (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
  1671. 0x01;
  1672. }
  1673. }
  1674. reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
  1675. ASSIGN_CTX_REG(reg_state, CTX_CTX_TIMESTAMP,
  1676. RING_CTX_TIMESTAMP(engine->mmio_base), 0);
  1677. /* PDP values well be assigned later if needed */
  1678. ASSIGN_CTX_REG(reg_state, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3),
  1679. 0);
  1680. ASSIGN_CTX_REG(reg_state, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3),
  1681. 0);
  1682. ASSIGN_CTX_REG(reg_state, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2),
  1683. 0);
  1684. ASSIGN_CTX_REG(reg_state, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2),
  1685. 0);
  1686. ASSIGN_CTX_REG(reg_state, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1),
  1687. 0);
  1688. ASSIGN_CTX_REG(reg_state, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1),
  1689. 0);
  1690. ASSIGN_CTX_REG(reg_state, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0),
  1691. 0);
  1692. ASSIGN_CTX_REG(reg_state, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0),
  1693. 0);
  1694. if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
  1695. /* 64b PPGTT (48bit canonical)
  1696. * PDP0_DESCRIPTOR contains the base address to PML4 and
  1697. * other PDP Descriptors are ignored.
  1698. */
  1699. ASSIGN_CTX_PML4(ppgtt, reg_state);
  1700. } else {
  1701. /* 32b PPGTT
  1702. * PDP*_DESCRIPTOR contains the base address of space supported.
  1703. * With dynamic page allocation, PDPs may not be allocated at
  1704. * this point. Point the unallocated PDPs to the scratch page
  1705. */
  1706. execlists_update_context_pdps(ppgtt, reg_state);
  1707. }
  1708. if (engine->id == RCS) {
  1709. reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  1710. ASSIGN_CTX_REG(reg_state, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
  1711. make_rpcs(dev_priv));
  1712. }
  1713. i915_gem_object_unpin_map(ctx_obj);
  1714. return 0;
  1715. }
  1716. /**
  1717. * intel_lr_context_size() - return the size of the context for an engine
  1718. * @engine: which engine to find the context size for
  1719. *
  1720. * Each engine may require a different amount of space for a context image,
  1721. * so when allocating (or copying) an image, this function can be used to
  1722. * find the right size for the specific engine.
  1723. *
  1724. * Return: size (in bytes) of an engine-specific context image
  1725. *
  1726. * Note: this size includes the HWSP, which is part of the context image
  1727. * in LRC mode, but does not include the "shared data page" used with
  1728. * GuC submission. The caller should account for this if using the GuC.
  1729. */
  1730. uint32_t intel_lr_context_size(struct intel_engine_cs *engine)
  1731. {
  1732. int ret = 0;
  1733. WARN_ON(INTEL_GEN(engine->i915) < 8);
  1734. switch (engine->id) {
  1735. case RCS:
  1736. if (INTEL_GEN(engine->i915) >= 9)
  1737. ret = GEN9_LR_CONTEXT_RENDER_SIZE;
  1738. else
  1739. ret = GEN8_LR_CONTEXT_RENDER_SIZE;
  1740. break;
  1741. case VCS:
  1742. case BCS:
  1743. case VECS:
  1744. case VCS2:
  1745. ret = GEN8_LR_CONTEXT_OTHER_SIZE;
  1746. break;
  1747. }
  1748. return ret;
  1749. }
  1750. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  1751. struct intel_engine_cs *engine)
  1752. {
  1753. struct drm_i915_gem_object *ctx_obj;
  1754. struct intel_context *ce = &ctx->engine[engine->id];
  1755. struct i915_vma *vma;
  1756. uint32_t context_size;
  1757. struct intel_ring *ring;
  1758. int ret;
  1759. WARN_ON(ce->state);
  1760. context_size = round_up(intel_lr_context_size(engine), 4096);
  1761. /* One extra page as the sharing data between driver and GuC */
  1762. context_size += PAGE_SIZE * LRC_PPHWSP_PN;
  1763. ctx_obj = i915_gem_object_create(&ctx->i915->drm, context_size);
  1764. if (IS_ERR(ctx_obj)) {
  1765. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
  1766. return PTR_ERR(ctx_obj);
  1767. }
  1768. vma = i915_vma_create(ctx_obj, &ctx->i915->ggtt.base, NULL);
  1769. if (IS_ERR(vma)) {
  1770. ret = PTR_ERR(vma);
  1771. goto error_deref_obj;
  1772. }
  1773. ring = intel_engine_create_ring(engine, ctx->ring_size);
  1774. if (IS_ERR(ring)) {
  1775. ret = PTR_ERR(ring);
  1776. goto error_deref_obj;
  1777. }
  1778. ret = populate_lr_context(ctx, ctx_obj, engine, ring);
  1779. if (ret) {
  1780. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  1781. goto error_ring_free;
  1782. }
  1783. ce->ring = ring;
  1784. ce->state = vma;
  1785. ce->initialised = engine->init_context == NULL;
  1786. return 0;
  1787. error_ring_free:
  1788. intel_ring_free(ring);
  1789. error_deref_obj:
  1790. i915_gem_object_put(ctx_obj);
  1791. return ret;
  1792. }
  1793. void intel_lr_context_resume(struct drm_i915_private *dev_priv)
  1794. {
  1795. struct i915_gem_context *ctx = dev_priv->kernel_context;
  1796. struct intel_engine_cs *engine;
  1797. for_each_engine(engine, dev_priv) {
  1798. struct intel_context *ce = &ctx->engine[engine->id];
  1799. void *vaddr;
  1800. uint32_t *reg_state;
  1801. if (!ce->state)
  1802. continue;
  1803. vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
  1804. if (WARN_ON(IS_ERR(vaddr)))
  1805. continue;
  1806. reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  1807. reg_state[CTX_RING_HEAD+1] = 0;
  1808. reg_state[CTX_RING_TAIL+1] = 0;
  1809. ce->state->obj->dirty = true;
  1810. i915_gem_object_unpin_map(ce->state->obj);
  1811. ce->ring->head = 0;
  1812. ce->ring->tail = 0;
  1813. }
  1814. }