intel_lrc.c 66 KB

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