intel_lrc.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323
  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_BXT_REVID(dev_priv, 0, BXT_REVID_A1) &&
  257. (engine->id == VCS || engine->id == VCS2);
  258. engine->ctx_desc_template = GEN8_CTX_VALID;
  259. if (IS_GEN8(dev_priv))
  260. engine->ctx_desc_template |= GEN8_CTX_L3LLC_COHERENT;
  261. engine->ctx_desc_template |= GEN8_CTX_PRIVILEGE;
  262. /* TODO: WaDisableLiteRestore when we start using semaphore
  263. * signalling between Command Streamers */
  264. /* ring->ctx_desc_template |= GEN8_CTX_FORCE_RESTORE; */
  265. /* WaEnableForceRestoreInCtxtDescForVCS:skl */
  266. /* WaEnableForceRestoreInCtxtDescForVCS:bxt */
  267. if (engine->disable_lite_restore_wa)
  268. engine->ctx_desc_template |= GEN8_CTX_FORCE_RESTORE;
  269. }
  270. /**
  271. * intel_lr_context_descriptor_update() - calculate & cache the descriptor
  272. * descriptor for a pinned context
  273. * @ctx: Context to work on
  274. * @engine: Engine the descriptor will be used with
  275. *
  276. * The context descriptor encodes various attributes of a context,
  277. * including its GTT address and some flags. Because it's fairly
  278. * expensive to calculate, we'll just do it once and cache the result,
  279. * which remains valid until the context is unpinned.
  280. *
  281. * This is what a descriptor looks like, from LSB to MSB::
  282. *
  283. * bits 0-11: flags, GEN8_CTX_* (cached in ctx_desc_template)
  284. * bits 12-31: LRCA, GTT address of (the HWSP of) this context
  285. * bits 32-52: ctx ID, a globally unique tag
  286. * bits 53-54: mbz, reserved for use by hardware
  287. * bits 55-63: group ID, currently unused and set to 0
  288. */
  289. static void
  290. intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
  291. struct intel_engine_cs *engine)
  292. {
  293. struct intel_context *ce = &ctx->engine[engine->id];
  294. u64 desc;
  295. BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
  296. desc = ctx->desc_template; /* bits 3-4 */
  297. desc |= engine->ctx_desc_template; /* bits 0-11 */
  298. desc |= i915_ggtt_offset(ce->state) + LRC_PPHWSP_PN * PAGE_SIZE;
  299. /* bits 12-31 */
  300. desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
  301. ce->lrc_desc = desc;
  302. }
  303. uint64_t intel_lr_context_descriptor(struct i915_gem_context *ctx,
  304. struct intel_engine_cs *engine)
  305. {
  306. return ctx->engine[engine->id].lrc_desc;
  307. }
  308. static inline void
  309. execlists_context_status_change(struct drm_i915_gem_request *rq,
  310. unsigned long status)
  311. {
  312. /*
  313. * Only used when GVT-g is enabled now. When GVT-g is disabled,
  314. * The compiler should eliminate this function as dead-code.
  315. */
  316. if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
  317. return;
  318. atomic_notifier_call_chain(&rq->ctx->status_notifier, status, rq);
  319. }
  320. static void
  321. execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
  322. {
  323. ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
  324. ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
  325. ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
  326. ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
  327. }
  328. static u64 execlists_update_context(struct drm_i915_gem_request *rq)
  329. {
  330. struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
  331. struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
  332. u32 *reg_state = ce->lrc_reg_state;
  333. reg_state[CTX_RING_TAIL+1] = rq->tail;
  334. /* True 32b PPGTT with dynamic page allocation: update PDP
  335. * registers and point the unallocated PDPs to scratch page.
  336. * PML4 is allocated during ppgtt init, so this is not needed
  337. * in 48-bit mode.
  338. */
  339. if (ppgtt && !USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
  340. execlists_update_context_pdps(ppgtt, reg_state);
  341. return ce->lrc_desc;
  342. }
  343. static void execlists_submit_ports(struct intel_engine_cs *engine)
  344. {
  345. struct drm_i915_private *dev_priv = engine->i915;
  346. struct execlist_port *port = engine->execlist_port;
  347. u32 __iomem *elsp =
  348. dev_priv->regs + i915_mmio_reg_offset(RING_ELSP(engine));
  349. u64 desc[2];
  350. if (!port[0].count)
  351. execlists_context_status_change(port[0].request,
  352. INTEL_CONTEXT_SCHEDULE_IN);
  353. desc[0] = execlists_update_context(port[0].request);
  354. engine->preempt_wa = port[0].count++; /* bdw only? fixed on skl? */
  355. if (port[1].request) {
  356. GEM_BUG_ON(port[1].count);
  357. execlists_context_status_change(port[1].request,
  358. INTEL_CONTEXT_SCHEDULE_IN);
  359. desc[1] = execlists_update_context(port[1].request);
  360. port[1].count = 1;
  361. } else {
  362. desc[1] = 0;
  363. }
  364. GEM_BUG_ON(desc[0] == desc[1]);
  365. /* You must always write both descriptors in the order below. */
  366. writel(upper_32_bits(desc[1]), elsp);
  367. writel(lower_32_bits(desc[1]), elsp);
  368. writel(upper_32_bits(desc[0]), elsp);
  369. /* The context is automatically loaded after the following */
  370. writel(lower_32_bits(desc[0]), elsp);
  371. }
  372. static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
  373. {
  374. return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
  375. ctx->execlists_force_single_submission);
  376. }
  377. static bool can_merge_ctx(const struct i915_gem_context *prev,
  378. const struct i915_gem_context *next)
  379. {
  380. if (prev != next)
  381. return false;
  382. if (ctx_single_port_submission(prev))
  383. return false;
  384. return true;
  385. }
  386. static void execlists_dequeue(struct intel_engine_cs *engine)
  387. {
  388. struct drm_i915_gem_request *last;
  389. struct execlist_port *port = engine->execlist_port;
  390. unsigned long flags;
  391. struct rb_node *rb;
  392. bool submit = false;
  393. last = port->request;
  394. if (last)
  395. /* WaIdleLiteRestore:bdw,skl
  396. * Apply the wa NOOPs to prevent ring:HEAD == req:TAIL
  397. * as we resubmit the request. See gen8_emit_breadcrumb()
  398. * for where we prepare the padding after the end of the
  399. * request.
  400. */
  401. last->tail = last->wa_tail;
  402. GEM_BUG_ON(port[1].request);
  403. /* Hardware submission is through 2 ports. Conceptually each port
  404. * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
  405. * static for a context, and unique to each, so we only execute
  406. * requests belonging to a single context from each ring. RING_HEAD
  407. * is maintained by the CS in the context image, it marks the place
  408. * where it got up to last time, and through RING_TAIL we tell the CS
  409. * where we want to execute up to this time.
  410. *
  411. * In this list the requests are in order of execution. Consecutive
  412. * requests from the same context are adjacent in the ringbuffer. We
  413. * can combine these requests into a single RING_TAIL update:
  414. *
  415. * RING_HEAD...req1...req2
  416. * ^- RING_TAIL
  417. * since to execute req2 the CS must first execute req1.
  418. *
  419. * Our goal then is to point each port to the end of a consecutive
  420. * sequence of requests as being the most optimal (fewest wake ups
  421. * and context switches) submission.
  422. */
  423. spin_lock_irqsave(&engine->timeline->lock, flags);
  424. rb = engine->execlist_first;
  425. while (rb) {
  426. struct drm_i915_gem_request *cursor =
  427. rb_entry(rb, typeof(*cursor), priotree.node);
  428. /* Can we combine this request with the current port? It has to
  429. * be the same context/ringbuffer and not have any exceptions
  430. * (e.g. GVT saying never to combine contexts).
  431. *
  432. * If we can combine the requests, we can execute both by
  433. * updating the RING_TAIL to point to the end of the second
  434. * request, and so we never need to tell the hardware about
  435. * the first.
  436. */
  437. if (last && !can_merge_ctx(cursor->ctx, last->ctx)) {
  438. /* If we are on the second port and cannot combine
  439. * this request with the last, then we are done.
  440. */
  441. if (port != engine->execlist_port)
  442. break;
  443. /* If GVT overrides us we only ever submit port[0],
  444. * leaving port[1] empty. Note that we also have
  445. * to be careful that we don't queue the same
  446. * context (even though a different request) to
  447. * the second port.
  448. */
  449. if (ctx_single_port_submission(last->ctx) ||
  450. ctx_single_port_submission(cursor->ctx))
  451. break;
  452. GEM_BUG_ON(last->ctx == cursor->ctx);
  453. i915_gem_request_assign(&port->request, last);
  454. port++;
  455. }
  456. rb = rb_next(rb);
  457. rb_erase(&cursor->priotree.node, &engine->execlist_queue);
  458. RB_CLEAR_NODE(&cursor->priotree.node);
  459. cursor->priotree.priority = INT_MAX;
  460. /* We keep the previous context alive until we retire the
  461. * following request. This ensures that any the context object
  462. * is still pinned for any residual writes the HW makes into it
  463. * on the context switch into the next object following the
  464. * breadcrumb. Otherwise, we may retire the context too early.
  465. */
  466. cursor->previous_context = engine->last_context;
  467. engine->last_context = cursor->ctx;
  468. __i915_gem_request_submit(cursor);
  469. last = cursor;
  470. submit = true;
  471. }
  472. if (submit) {
  473. i915_gem_request_assign(&port->request, last);
  474. engine->execlist_first = rb;
  475. }
  476. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  477. if (submit)
  478. execlists_submit_ports(engine);
  479. }
  480. static bool execlists_elsp_idle(struct intel_engine_cs *engine)
  481. {
  482. return !engine->execlist_port[0].request;
  483. }
  484. /**
  485. * intel_execlists_idle() - Determine if all engine submission ports are idle
  486. * @dev_priv: i915 device private
  487. *
  488. * Return true if there are no requests pending on any of the submission ports
  489. * of any engines.
  490. */
  491. bool intel_execlists_idle(struct drm_i915_private *dev_priv)
  492. {
  493. struct intel_engine_cs *engine;
  494. enum intel_engine_id id;
  495. if (!i915.enable_execlists)
  496. return true;
  497. for_each_engine(engine, dev_priv, id)
  498. if (!execlists_elsp_idle(engine))
  499. return false;
  500. return true;
  501. }
  502. static bool execlists_elsp_ready(struct intel_engine_cs *engine)
  503. {
  504. int port;
  505. port = 1; /* wait for a free slot */
  506. if (engine->disable_lite_restore_wa || engine->preempt_wa)
  507. port = 0; /* wait for GPU to be idle before continuing */
  508. return !engine->execlist_port[port].request;
  509. }
  510. /*
  511. * Check the unread Context Status Buffers and manage the submission of new
  512. * contexts to the ELSP accordingly.
  513. */
  514. static void intel_lrc_irq_handler(unsigned long data)
  515. {
  516. struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
  517. struct execlist_port *port = engine->execlist_port;
  518. struct drm_i915_private *dev_priv = engine->i915;
  519. intel_uncore_forcewake_get(dev_priv, engine->fw_domains);
  520. if (!execlists_elsp_idle(engine)) {
  521. u32 __iomem *csb_mmio =
  522. dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine));
  523. u32 __iomem *buf =
  524. dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0));
  525. unsigned int csb, head, tail;
  526. csb = readl(csb_mmio);
  527. head = GEN8_CSB_READ_PTR(csb);
  528. tail = GEN8_CSB_WRITE_PTR(csb);
  529. if (tail < head)
  530. tail += GEN8_CSB_ENTRIES;
  531. while (head < tail) {
  532. unsigned int idx = ++head % GEN8_CSB_ENTRIES;
  533. unsigned int status = readl(buf + 2 * idx);
  534. if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
  535. continue;
  536. GEM_BUG_ON(port[0].count == 0);
  537. if (--port[0].count == 0) {
  538. GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
  539. execlists_context_status_change(port[0].request,
  540. INTEL_CONTEXT_SCHEDULE_OUT);
  541. i915_gem_request_put(port[0].request);
  542. port[0] = port[1];
  543. memset(&port[1], 0, sizeof(port[1]));
  544. engine->preempt_wa = false;
  545. }
  546. GEM_BUG_ON(port[0].count == 0 &&
  547. !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
  548. }
  549. writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK,
  550. GEN8_CSB_WRITE_PTR(csb) << 8),
  551. csb_mmio);
  552. }
  553. if (execlists_elsp_ready(engine))
  554. execlists_dequeue(engine);
  555. intel_uncore_forcewake_put(dev_priv, engine->fw_domains);
  556. }
  557. static bool insert_request(struct i915_priotree *pt, struct rb_root *root)
  558. {
  559. struct rb_node **p, *rb;
  560. bool first = true;
  561. /* most positive priority is scheduled first, equal priorities fifo */
  562. rb = NULL;
  563. p = &root->rb_node;
  564. while (*p) {
  565. struct i915_priotree *pos;
  566. rb = *p;
  567. pos = rb_entry(rb, typeof(*pos), node);
  568. if (pt->priority > pos->priority) {
  569. p = &rb->rb_left;
  570. } else {
  571. p = &rb->rb_right;
  572. first = false;
  573. }
  574. }
  575. rb_link_node(&pt->node, rb, p);
  576. rb_insert_color(&pt->node, root);
  577. return first;
  578. }
  579. static void execlists_submit_request(struct drm_i915_gem_request *request)
  580. {
  581. struct intel_engine_cs *engine = request->engine;
  582. unsigned long flags;
  583. /* Will be called from irq-context when using foreign fences. */
  584. spin_lock_irqsave(&engine->timeline->lock, flags);
  585. if (insert_request(&request->priotree, &engine->execlist_queue))
  586. engine->execlist_first = &request->priotree.node;
  587. if (execlists_elsp_idle(engine))
  588. tasklet_hi_schedule(&engine->irq_tasklet);
  589. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  590. }
  591. static struct intel_engine_cs *
  592. pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
  593. {
  594. struct intel_engine_cs *engine;
  595. engine = container_of(pt,
  596. struct drm_i915_gem_request,
  597. priotree)->engine;
  598. if (engine != locked) {
  599. if (locked)
  600. spin_unlock_irq(&locked->timeline->lock);
  601. spin_lock_irq(&engine->timeline->lock);
  602. }
  603. return engine;
  604. }
  605. static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
  606. {
  607. struct intel_engine_cs *engine = NULL;
  608. struct i915_dependency *dep, *p;
  609. struct i915_dependency stack;
  610. LIST_HEAD(dfs);
  611. if (prio <= READ_ONCE(request->priotree.priority))
  612. return;
  613. /* Need BKL in order to use the temporary link inside i915_dependency */
  614. lockdep_assert_held(&request->i915->drm.struct_mutex);
  615. stack.signaler = &request->priotree;
  616. list_add(&stack.dfs_link, &dfs);
  617. /* Recursively bump all dependent priorities to match the new request.
  618. *
  619. * A naive approach would be to use recursion:
  620. * static void update_priorities(struct i915_priotree *pt, prio) {
  621. * list_for_each_entry(dep, &pt->signalers_list, signal_link)
  622. * update_priorities(dep->signal, prio)
  623. * insert_request(pt);
  624. * }
  625. * but that may have unlimited recursion depth and so runs a very
  626. * real risk of overunning the kernel stack. Instead, we build
  627. * a flat list of all dependencies starting with the current request.
  628. * As we walk the list of dependencies, we add all of its dependencies
  629. * to the end of the list (this may include an already visited
  630. * request) and continue to walk onwards onto the new dependencies. The
  631. * end result is a topological list of requests in reverse order, the
  632. * last element in the list is the request we must execute first.
  633. */
  634. list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
  635. struct i915_priotree *pt = dep->signaler;
  636. list_for_each_entry(p, &pt->signalers_list, signal_link)
  637. if (prio > READ_ONCE(p->signaler->priority))
  638. list_move_tail(&p->dfs_link, &dfs);
  639. p = list_next_entry(dep, dfs_link);
  640. if (!RB_EMPTY_NODE(&pt->node))
  641. continue;
  642. engine = pt_lock_engine(pt, engine);
  643. /* If it is not already in the rbtree, we can update the
  644. * priority inplace and skip over it (and its dependencies)
  645. * if it is referenced *again* as we descend the dfs.
  646. */
  647. if (prio > pt->priority && RB_EMPTY_NODE(&pt->node)) {
  648. pt->priority = prio;
  649. list_del_init(&dep->dfs_link);
  650. }
  651. }
  652. /* Fifo and depth-first replacement ensure our deps execute before us */
  653. list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
  654. struct i915_priotree *pt = dep->signaler;
  655. INIT_LIST_HEAD(&dep->dfs_link);
  656. engine = pt_lock_engine(pt, engine);
  657. if (prio <= pt->priority)
  658. continue;
  659. GEM_BUG_ON(RB_EMPTY_NODE(&pt->node));
  660. pt->priority = prio;
  661. rb_erase(&pt->node, &engine->execlist_queue);
  662. if (insert_request(pt, &engine->execlist_queue))
  663. engine->execlist_first = &pt->node;
  664. }
  665. if (engine)
  666. spin_unlock_irq(&engine->timeline->lock);
  667. /* XXX Do we need to preempt to make room for us and our deps? */
  668. }
  669. int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request)
  670. {
  671. struct intel_engine_cs *engine = request->engine;
  672. struct intel_context *ce = &request->ctx->engine[engine->id];
  673. int ret;
  674. /* Flush enough space to reduce the likelihood of waiting after
  675. * we start building the request - in which case we will just
  676. * have to repeat work.
  677. */
  678. request->reserved_space += EXECLISTS_REQUEST_SIZE;
  679. if (!ce->state) {
  680. ret = execlists_context_deferred_alloc(request->ctx, engine);
  681. if (ret)
  682. return ret;
  683. }
  684. request->ring = ce->ring;
  685. ret = intel_lr_context_pin(request->ctx, engine);
  686. if (ret)
  687. return ret;
  688. if (i915.enable_guc_submission) {
  689. /*
  690. * Check that the GuC has space for the request before
  691. * going any further, as the i915_add_request() call
  692. * later on mustn't fail ...
  693. */
  694. ret = i915_guc_wq_reserve(request);
  695. if (ret)
  696. goto err_unpin;
  697. }
  698. ret = intel_ring_begin(request, 0);
  699. if (ret)
  700. goto err_unreserve;
  701. if (!ce->initialised) {
  702. ret = engine->init_context(request);
  703. if (ret)
  704. goto err_unreserve;
  705. ce->initialised = true;
  706. }
  707. /* Note that after this point, we have committed to using
  708. * this request as it is being used to both track the
  709. * state of engine initialisation and liveness of the
  710. * golden renderstate above. Think twice before you try
  711. * to cancel/unwind this request now.
  712. */
  713. request->reserved_space -= EXECLISTS_REQUEST_SIZE;
  714. return 0;
  715. err_unreserve:
  716. if (i915.enable_guc_submission)
  717. i915_guc_wq_unreserve(request);
  718. err_unpin:
  719. intel_lr_context_unpin(request->ctx, engine);
  720. return ret;
  721. }
  722. static int intel_lr_context_pin(struct i915_gem_context *ctx,
  723. struct intel_engine_cs *engine)
  724. {
  725. struct intel_context *ce = &ctx->engine[engine->id];
  726. void *vaddr;
  727. int ret;
  728. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  729. if (ce->pin_count++)
  730. return 0;
  731. ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN,
  732. PIN_OFFSET_BIAS | GUC_WOPCM_TOP | PIN_GLOBAL);
  733. if (ret)
  734. goto err;
  735. vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
  736. if (IS_ERR(vaddr)) {
  737. ret = PTR_ERR(vaddr);
  738. goto unpin_vma;
  739. }
  740. ret = intel_ring_pin(ce->ring);
  741. if (ret)
  742. goto unpin_map;
  743. intel_lr_context_descriptor_update(ctx, engine);
  744. ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  745. ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
  746. i915_ggtt_offset(ce->ring->vma);
  747. ce->state->obj->mm.dirty = true;
  748. /* Invalidate GuC TLB. */
  749. if (i915.enable_guc_submission) {
  750. struct drm_i915_private *dev_priv = ctx->i915;
  751. I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
  752. }
  753. i915_gem_context_get(ctx);
  754. return 0;
  755. unpin_map:
  756. i915_gem_object_unpin_map(ce->state->obj);
  757. unpin_vma:
  758. __i915_vma_unpin(ce->state);
  759. err:
  760. ce->pin_count = 0;
  761. return ret;
  762. }
  763. void intel_lr_context_unpin(struct i915_gem_context *ctx,
  764. struct intel_engine_cs *engine)
  765. {
  766. struct intel_context *ce = &ctx->engine[engine->id];
  767. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  768. GEM_BUG_ON(ce->pin_count == 0);
  769. if (--ce->pin_count)
  770. return;
  771. intel_ring_unpin(ce->ring);
  772. i915_gem_object_unpin_map(ce->state->obj);
  773. i915_vma_unpin(ce->state);
  774. i915_gem_context_put(ctx);
  775. }
  776. static int intel_logical_ring_workarounds_emit(struct drm_i915_gem_request *req)
  777. {
  778. int ret, i;
  779. struct intel_ring *ring = req->ring;
  780. struct i915_workarounds *w = &req->i915->workarounds;
  781. if (w->count == 0)
  782. return 0;
  783. ret = req->engine->emit_flush(req, EMIT_BARRIER);
  784. if (ret)
  785. return ret;
  786. ret = intel_ring_begin(req, w->count * 2 + 2);
  787. if (ret)
  788. return ret;
  789. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(w->count));
  790. for (i = 0; i < w->count; i++) {
  791. intel_ring_emit_reg(ring, w->reg[i].addr);
  792. intel_ring_emit(ring, w->reg[i].value);
  793. }
  794. intel_ring_emit(ring, MI_NOOP);
  795. intel_ring_advance(ring);
  796. ret = req->engine->emit_flush(req, EMIT_BARRIER);
  797. if (ret)
  798. return ret;
  799. return 0;
  800. }
  801. #define wa_ctx_emit(batch, index, cmd) \
  802. do { \
  803. int __index = (index)++; \
  804. if (WARN_ON(__index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
  805. return -ENOSPC; \
  806. } \
  807. batch[__index] = (cmd); \
  808. } while (0)
  809. #define wa_ctx_emit_reg(batch, index, reg) \
  810. wa_ctx_emit((batch), (index), i915_mmio_reg_offset(reg))
  811. /*
  812. * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
  813. * PIPE_CONTROL instruction. This is required for the flush to happen correctly
  814. * but there is a slight complication as this is applied in WA batch where the
  815. * values are only initialized once so we cannot take register value at the
  816. * beginning and reuse it further; hence we save its value to memory, upload a
  817. * constant value with bit21 set and then we restore it back with the saved value.
  818. * To simplify the WA, a constant value is formed by using the default value
  819. * of this register. This shouldn't be a problem because we are only modifying
  820. * it for a short period and this batch in non-premptible. We can ofcourse
  821. * use additional instructions that read the actual value of the register
  822. * at that time and set our bit of interest but it makes the WA complicated.
  823. *
  824. * This WA is also required for Gen9 so extracting as a function avoids
  825. * code duplication.
  826. */
  827. static inline int gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine,
  828. uint32_t *batch,
  829. uint32_t index)
  830. {
  831. struct drm_i915_private *dev_priv = engine->i915;
  832. uint32_t l3sqc4_flush = (0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES);
  833. /*
  834. * WaDisableLSQCROPERFforOCL:kbl
  835. * This WA is implemented in skl_init_clock_gating() but since
  836. * this batch updates GEN8_L3SQCREG4 with default value we need to
  837. * set this bit here to retain the WA during flush.
  838. */
  839. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
  840. l3sqc4_flush |= GEN8_LQSC_RO_PERF_DIS;
  841. wa_ctx_emit(batch, index, (MI_STORE_REGISTER_MEM_GEN8 |
  842. MI_SRM_LRM_GLOBAL_GTT));
  843. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  844. wa_ctx_emit(batch, index, i915_ggtt_offset(engine->scratch) + 256);
  845. wa_ctx_emit(batch, index, 0);
  846. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  847. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  848. wa_ctx_emit(batch, index, l3sqc4_flush);
  849. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  850. wa_ctx_emit(batch, index, (PIPE_CONTROL_CS_STALL |
  851. PIPE_CONTROL_DC_FLUSH_ENABLE));
  852. wa_ctx_emit(batch, index, 0);
  853. wa_ctx_emit(batch, index, 0);
  854. wa_ctx_emit(batch, index, 0);
  855. wa_ctx_emit(batch, index, 0);
  856. wa_ctx_emit(batch, index, (MI_LOAD_REGISTER_MEM_GEN8 |
  857. MI_SRM_LRM_GLOBAL_GTT));
  858. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  859. wa_ctx_emit(batch, index, i915_ggtt_offset(engine->scratch) + 256);
  860. wa_ctx_emit(batch, index, 0);
  861. return index;
  862. }
  863. static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
  864. uint32_t offset,
  865. uint32_t start_alignment)
  866. {
  867. return wa_ctx->offset = ALIGN(offset, start_alignment);
  868. }
  869. static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
  870. uint32_t offset,
  871. uint32_t size_alignment)
  872. {
  873. wa_ctx->size = offset - wa_ctx->offset;
  874. WARN(wa_ctx->size % size_alignment,
  875. "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
  876. wa_ctx->size, size_alignment);
  877. return 0;
  878. }
  879. /*
  880. * Typically we only have one indirect_ctx and per_ctx batch buffer which are
  881. * initialized at the beginning and shared across all contexts but this field
  882. * helps us to have multiple batches at different offsets and select them based
  883. * on a criteria. At the moment this batch always start at the beginning of the page
  884. * and at this point we don't have multiple wa_ctx batch buffers.
  885. *
  886. * The number of WA applied are not known at the beginning; we use this field
  887. * to return the no of DWORDS written.
  888. *
  889. * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
  890. * so it adds NOOPs as padding to make it cacheline aligned.
  891. * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
  892. * makes a complete batch buffer.
  893. */
  894. static int gen8_init_indirectctx_bb(struct intel_engine_cs *engine,
  895. struct i915_wa_ctx_bb *wa_ctx,
  896. uint32_t *batch,
  897. uint32_t *offset)
  898. {
  899. uint32_t scratch_addr;
  900. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  901. /* WaDisableCtxRestoreArbitration:bdw,chv */
  902. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  903. /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
  904. if (IS_BROADWELL(engine->i915)) {
  905. int rc = gen8_emit_flush_coherentl3_wa(engine, batch, index);
  906. if (rc < 0)
  907. return rc;
  908. index = rc;
  909. }
  910. /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
  911. /* Actual scratch location is at 128 bytes offset */
  912. scratch_addr = i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  913. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  914. wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
  915. PIPE_CONTROL_GLOBAL_GTT_IVB |
  916. PIPE_CONTROL_CS_STALL |
  917. PIPE_CONTROL_QW_WRITE));
  918. wa_ctx_emit(batch, index, scratch_addr);
  919. wa_ctx_emit(batch, index, 0);
  920. wa_ctx_emit(batch, index, 0);
  921. wa_ctx_emit(batch, index, 0);
  922. /* Pad to end of cacheline */
  923. while (index % CACHELINE_DWORDS)
  924. wa_ctx_emit(batch, index, MI_NOOP);
  925. /*
  926. * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
  927. * execution depends on the length specified in terms of cache lines
  928. * in the register CTX_RCS_INDIRECT_CTX
  929. */
  930. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  931. }
  932. /*
  933. * This batch is started immediately after indirect_ctx batch. Since we ensure
  934. * that indirect_ctx ends on a cacheline this batch is aligned automatically.
  935. *
  936. * The number of DWORDS written are returned using this field.
  937. *
  938. * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
  939. * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
  940. */
  941. static int gen8_init_perctx_bb(struct intel_engine_cs *engine,
  942. struct i915_wa_ctx_bb *wa_ctx,
  943. uint32_t *batch,
  944. uint32_t *offset)
  945. {
  946. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  947. /* WaDisableCtxRestoreArbitration:bdw,chv */
  948. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  949. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  950. return wa_ctx_end(wa_ctx, *offset = index, 1);
  951. }
  952. static int gen9_init_indirectctx_bb(struct intel_engine_cs *engine,
  953. struct i915_wa_ctx_bb *wa_ctx,
  954. uint32_t *batch,
  955. uint32_t *offset)
  956. {
  957. int ret;
  958. struct drm_i915_private *dev_priv = engine->i915;
  959. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  960. /* WaDisableCtxRestoreArbitration:bxt */
  961. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
  962. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  963. /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt */
  964. ret = gen8_emit_flush_coherentl3_wa(engine, batch, index);
  965. if (ret < 0)
  966. return ret;
  967. index = ret;
  968. /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl */
  969. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  970. wa_ctx_emit_reg(batch, index, COMMON_SLICE_CHICKEN2);
  971. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(
  972. GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE));
  973. wa_ctx_emit(batch, index, MI_NOOP);
  974. /* WaClearSlmSpaceAtContextSwitch:kbl */
  975. /* Actual scratch location is at 128 bytes offset */
  976. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_A0)) {
  977. u32 scratch_addr =
  978. i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  979. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  980. wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
  981. PIPE_CONTROL_GLOBAL_GTT_IVB |
  982. PIPE_CONTROL_CS_STALL |
  983. PIPE_CONTROL_QW_WRITE));
  984. wa_ctx_emit(batch, index, scratch_addr);
  985. wa_ctx_emit(batch, index, 0);
  986. wa_ctx_emit(batch, index, 0);
  987. wa_ctx_emit(batch, index, 0);
  988. }
  989. /* WaMediaPoolStateCmdInWABB:bxt */
  990. if (HAS_POOLED_EU(engine->i915)) {
  991. /*
  992. * EU pool configuration is setup along with golden context
  993. * during context initialization. This value depends on
  994. * device type (2x6 or 3x6) and needs to be updated based
  995. * on which subslice is disabled especially for 2x6
  996. * devices, however it is safe to load default
  997. * configuration of 3x6 device instead of masking off
  998. * corresponding bits because HW ignores bits of a disabled
  999. * subslice and drops down to appropriate config. Please
  1000. * see render_state_setup() in i915_gem_render_state.c for
  1001. * possible configurations, to avoid duplication they are
  1002. * not shown here again.
  1003. */
  1004. u32 eu_pool_config = 0x00777000;
  1005. wa_ctx_emit(batch, index, GEN9_MEDIA_POOL_STATE);
  1006. wa_ctx_emit(batch, index, GEN9_MEDIA_POOL_ENABLE);
  1007. wa_ctx_emit(batch, index, eu_pool_config);
  1008. wa_ctx_emit(batch, index, 0);
  1009. wa_ctx_emit(batch, index, 0);
  1010. wa_ctx_emit(batch, index, 0);
  1011. }
  1012. /* Pad to end of cacheline */
  1013. while (index % CACHELINE_DWORDS)
  1014. wa_ctx_emit(batch, index, MI_NOOP);
  1015. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  1016. }
  1017. static int gen9_init_perctx_bb(struct intel_engine_cs *engine,
  1018. struct i915_wa_ctx_bb *wa_ctx,
  1019. uint32_t *batch,
  1020. uint32_t *offset)
  1021. {
  1022. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1023. /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:bxt */
  1024. if (IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1)) {
  1025. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  1026. wa_ctx_emit_reg(batch, index, GEN9_SLICE_COMMON_ECO_CHICKEN0);
  1027. wa_ctx_emit(batch, index,
  1028. _MASKED_BIT_ENABLE(DISABLE_PIXEL_MASK_CAMMING));
  1029. wa_ctx_emit(batch, index, MI_NOOP);
  1030. }
  1031. /* WaClearTdlStateAckDirtyBits:bxt */
  1032. if (IS_BXT_REVID(engine->i915, 0, BXT_REVID_B0)) {
  1033. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(4));
  1034. wa_ctx_emit_reg(batch, index, GEN8_STATE_ACK);
  1035. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  1036. wa_ctx_emit_reg(batch, index, GEN9_STATE_ACK_SLICE1);
  1037. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  1038. wa_ctx_emit_reg(batch, index, GEN9_STATE_ACK_SLICE2);
  1039. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  1040. wa_ctx_emit_reg(batch, index, GEN7_ROW_CHICKEN2);
  1041. /* dummy write to CS, mask bits are 0 to ensure the register is not modified */
  1042. wa_ctx_emit(batch, index, 0x0);
  1043. wa_ctx_emit(batch, index, MI_NOOP);
  1044. }
  1045. /* WaDisableCtxRestoreArbitration:bxt */
  1046. if (IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1))
  1047. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  1048. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  1049. return wa_ctx_end(wa_ctx, *offset = index, 1);
  1050. }
  1051. static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *engine, u32 size)
  1052. {
  1053. struct drm_i915_gem_object *obj;
  1054. struct i915_vma *vma;
  1055. int err;
  1056. obj = i915_gem_object_create(engine->i915, PAGE_ALIGN(size));
  1057. if (IS_ERR(obj))
  1058. return PTR_ERR(obj);
  1059. vma = i915_vma_create(obj, &engine->i915->ggtt.base, NULL);
  1060. if (IS_ERR(vma)) {
  1061. err = PTR_ERR(vma);
  1062. goto err;
  1063. }
  1064. err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
  1065. if (err)
  1066. goto err;
  1067. engine->wa_ctx.vma = vma;
  1068. return 0;
  1069. err:
  1070. i915_gem_object_put(obj);
  1071. return err;
  1072. }
  1073. static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *engine)
  1074. {
  1075. i915_vma_unpin_and_release(&engine->wa_ctx.vma);
  1076. }
  1077. static int intel_init_workaround_bb(struct intel_engine_cs *engine)
  1078. {
  1079. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1080. uint32_t *batch;
  1081. uint32_t offset;
  1082. struct page *page;
  1083. int ret;
  1084. WARN_ON(engine->id != RCS);
  1085. /* update this when WA for higher Gen are added */
  1086. if (INTEL_GEN(engine->i915) > 9) {
  1087. DRM_ERROR("WA batch buffer is not initialized for Gen%d\n",
  1088. INTEL_GEN(engine->i915));
  1089. return 0;
  1090. }
  1091. /* some WA perform writes to scratch page, ensure it is valid */
  1092. if (!engine->scratch) {
  1093. DRM_ERROR("scratch page not allocated for %s\n", engine->name);
  1094. return -EINVAL;
  1095. }
  1096. ret = lrc_setup_wa_ctx_obj(engine, PAGE_SIZE);
  1097. if (ret) {
  1098. DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
  1099. return ret;
  1100. }
  1101. page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
  1102. batch = kmap_atomic(page);
  1103. offset = 0;
  1104. if (IS_GEN8(engine->i915)) {
  1105. ret = gen8_init_indirectctx_bb(engine,
  1106. &wa_ctx->indirect_ctx,
  1107. batch,
  1108. &offset);
  1109. if (ret)
  1110. goto out;
  1111. ret = gen8_init_perctx_bb(engine,
  1112. &wa_ctx->per_ctx,
  1113. batch,
  1114. &offset);
  1115. if (ret)
  1116. goto out;
  1117. } else if (IS_GEN9(engine->i915)) {
  1118. ret = gen9_init_indirectctx_bb(engine,
  1119. &wa_ctx->indirect_ctx,
  1120. batch,
  1121. &offset);
  1122. if (ret)
  1123. goto out;
  1124. ret = gen9_init_perctx_bb(engine,
  1125. &wa_ctx->per_ctx,
  1126. batch,
  1127. &offset);
  1128. if (ret)
  1129. goto out;
  1130. }
  1131. out:
  1132. kunmap_atomic(batch);
  1133. if (ret)
  1134. lrc_destroy_wa_ctx_obj(engine);
  1135. return ret;
  1136. }
  1137. static void lrc_init_hws(struct intel_engine_cs *engine)
  1138. {
  1139. struct drm_i915_private *dev_priv = engine->i915;
  1140. I915_WRITE(RING_HWS_PGA(engine->mmio_base),
  1141. engine->status_page.ggtt_offset);
  1142. POSTING_READ(RING_HWS_PGA(engine->mmio_base));
  1143. }
  1144. static int gen8_init_common_ring(struct intel_engine_cs *engine)
  1145. {
  1146. struct drm_i915_private *dev_priv = engine->i915;
  1147. int ret;
  1148. ret = intel_mocs_init_engine(engine);
  1149. if (ret)
  1150. return ret;
  1151. lrc_init_hws(engine);
  1152. intel_engine_reset_breadcrumbs(engine);
  1153. I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
  1154. I915_WRITE(RING_MODE_GEN7(engine),
  1155. _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
  1156. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  1157. DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
  1158. intel_engine_init_hangcheck(engine);
  1159. /* After a GPU reset, we may have requests to replay */
  1160. if (!execlists_elsp_idle(engine)) {
  1161. engine->execlist_port[0].count = 0;
  1162. engine->execlist_port[1].count = 0;
  1163. execlists_submit_ports(engine);
  1164. }
  1165. return 0;
  1166. }
  1167. static int gen8_init_render_ring(struct intel_engine_cs *engine)
  1168. {
  1169. struct drm_i915_private *dev_priv = engine->i915;
  1170. int ret;
  1171. ret = gen8_init_common_ring(engine);
  1172. if (ret)
  1173. return ret;
  1174. /* We need to disable the AsyncFlip performance optimisations in order
  1175. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  1176. * programmed to '1' on all products.
  1177. *
  1178. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  1179. */
  1180. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  1181. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  1182. return init_workarounds_ring(engine);
  1183. }
  1184. static int gen9_init_render_ring(struct intel_engine_cs *engine)
  1185. {
  1186. int ret;
  1187. ret = gen8_init_common_ring(engine);
  1188. if (ret)
  1189. return ret;
  1190. return init_workarounds_ring(engine);
  1191. }
  1192. static void reset_common_ring(struct intel_engine_cs *engine,
  1193. struct drm_i915_gem_request *request)
  1194. {
  1195. struct drm_i915_private *dev_priv = engine->i915;
  1196. struct execlist_port *port = engine->execlist_port;
  1197. struct intel_context *ce = &request->ctx->engine[engine->id];
  1198. /* We want a simple context + ring to execute the breadcrumb update.
  1199. * We cannot rely on the context being intact across the GPU hang,
  1200. * so clear it and rebuild just what we need for the breadcrumb.
  1201. * All pending requests for this context will be zapped, and any
  1202. * future request will be after userspace has had the opportunity
  1203. * to recreate its own state.
  1204. */
  1205. execlists_init_reg_state(ce->lrc_reg_state,
  1206. request->ctx, engine, ce->ring);
  1207. /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
  1208. ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
  1209. i915_ggtt_offset(ce->ring->vma);
  1210. ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
  1211. request->ring->head = request->postfix;
  1212. request->ring->last_retired_head = -1;
  1213. intel_ring_update_space(request->ring);
  1214. if (i915.enable_guc_submission)
  1215. return;
  1216. /* Catch up with any missed context-switch interrupts */
  1217. I915_WRITE(RING_CONTEXT_STATUS_PTR(engine), _MASKED_FIELD(0xffff, 0));
  1218. if (request->ctx != port[0].request->ctx) {
  1219. i915_gem_request_put(port[0].request);
  1220. port[0] = port[1];
  1221. memset(&port[1], 0, sizeof(port[1]));
  1222. }
  1223. GEM_BUG_ON(request->ctx != port[0].request->ctx);
  1224. /* Reset WaIdleLiteRestore:bdw,skl as well */
  1225. request->tail = request->wa_tail - WA_TAIL_DWORDS * sizeof(u32);
  1226. }
  1227. static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
  1228. {
  1229. struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
  1230. struct intel_ring *ring = req->ring;
  1231. struct intel_engine_cs *engine = req->engine;
  1232. const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
  1233. int i, ret;
  1234. ret = intel_ring_begin(req, num_lri_cmds * 2 + 2);
  1235. if (ret)
  1236. return ret;
  1237. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(num_lri_cmds));
  1238. for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
  1239. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  1240. intel_ring_emit_reg(ring, GEN8_RING_PDP_UDW(engine, i));
  1241. intel_ring_emit(ring, upper_32_bits(pd_daddr));
  1242. intel_ring_emit_reg(ring, GEN8_RING_PDP_LDW(engine, i));
  1243. intel_ring_emit(ring, lower_32_bits(pd_daddr));
  1244. }
  1245. intel_ring_emit(ring, MI_NOOP);
  1246. intel_ring_advance(ring);
  1247. return 0;
  1248. }
  1249. static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
  1250. u64 offset, u32 len,
  1251. unsigned int dispatch_flags)
  1252. {
  1253. struct intel_ring *ring = req->ring;
  1254. bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
  1255. int ret;
  1256. /* Don't rely in hw updating PDPs, specially in lite-restore.
  1257. * Ideally, we should set Force PD Restore in ctx descriptor,
  1258. * but we can't. Force Restore would be a second option, but
  1259. * it is unsafe in case of lite-restore (because the ctx is
  1260. * not idle). PML4 is allocated during ppgtt init so this is
  1261. * not needed in 48-bit.*/
  1262. if (req->ctx->ppgtt &&
  1263. (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings)) {
  1264. if (!USES_FULL_48BIT_PPGTT(req->i915) &&
  1265. !intel_vgpu_active(req->i915)) {
  1266. ret = intel_logical_ring_emit_pdps(req);
  1267. if (ret)
  1268. return ret;
  1269. }
  1270. req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
  1271. }
  1272. ret = intel_ring_begin(req, 4);
  1273. if (ret)
  1274. return ret;
  1275. /* FIXME(BDW): Address space and security selectors. */
  1276. intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 |
  1277. (ppgtt<<8) |
  1278. (dispatch_flags & I915_DISPATCH_RS ?
  1279. MI_BATCH_RESOURCE_STREAMER : 0));
  1280. intel_ring_emit(ring, lower_32_bits(offset));
  1281. intel_ring_emit(ring, upper_32_bits(offset));
  1282. intel_ring_emit(ring, MI_NOOP);
  1283. intel_ring_advance(ring);
  1284. return 0;
  1285. }
  1286. static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
  1287. {
  1288. struct drm_i915_private *dev_priv = engine->i915;
  1289. I915_WRITE_IMR(engine,
  1290. ~(engine->irq_enable_mask | engine->irq_keep_mask));
  1291. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1292. }
  1293. static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
  1294. {
  1295. struct drm_i915_private *dev_priv = engine->i915;
  1296. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  1297. }
  1298. static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
  1299. {
  1300. struct intel_ring *ring = request->ring;
  1301. u32 cmd;
  1302. int ret;
  1303. ret = intel_ring_begin(request, 4);
  1304. if (ret)
  1305. return ret;
  1306. cmd = MI_FLUSH_DW + 1;
  1307. /* We always require a command barrier so that subsequent
  1308. * commands, such as breadcrumb interrupts, are strictly ordered
  1309. * wrt the contents of the write cache being flushed to memory
  1310. * (and thus being coherent from the CPU).
  1311. */
  1312. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1313. if (mode & EMIT_INVALIDATE) {
  1314. cmd |= MI_INVALIDATE_TLB;
  1315. if (request->engine->id == VCS)
  1316. cmd |= MI_INVALIDATE_BSD;
  1317. }
  1318. intel_ring_emit(ring, cmd);
  1319. intel_ring_emit(ring,
  1320. I915_GEM_HWS_SCRATCH_ADDR |
  1321. MI_FLUSH_DW_USE_GTT);
  1322. intel_ring_emit(ring, 0); /* upper addr */
  1323. intel_ring_emit(ring, 0); /* value */
  1324. intel_ring_advance(ring);
  1325. return 0;
  1326. }
  1327. static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
  1328. u32 mode)
  1329. {
  1330. struct intel_ring *ring = request->ring;
  1331. struct intel_engine_cs *engine = request->engine;
  1332. u32 scratch_addr =
  1333. i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  1334. bool vf_flush_wa = false, dc_flush_wa = false;
  1335. u32 flags = 0;
  1336. int ret;
  1337. int len;
  1338. flags |= PIPE_CONTROL_CS_STALL;
  1339. if (mode & EMIT_FLUSH) {
  1340. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  1341. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  1342. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  1343. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  1344. }
  1345. if (mode & EMIT_INVALIDATE) {
  1346. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  1347. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  1348. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  1349. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1350. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  1351. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  1352. flags |= PIPE_CONTROL_QW_WRITE;
  1353. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1354. /*
  1355. * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
  1356. * pipe control.
  1357. */
  1358. if (IS_GEN9(request->i915))
  1359. vf_flush_wa = true;
  1360. /* WaForGAMHang:kbl */
  1361. if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
  1362. dc_flush_wa = true;
  1363. }
  1364. len = 6;
  1365. if (vf_flush_wa)
  1366. len += 6;
  1367. if (dc_flush_wa)
  1368. len += 12;
  1369. ret = intel_ring_begin(request, len);
  1370. if (ret)
  1371. return ret;
  1372. if (vf_flush_wa) {
  1373. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1374. intel_ring_emit(ring, 0);
  1375. intel_ring_emit(ring, 0);
  1376. intel_ring_emit(ring, 0);
  1377. intel_ring_emit(ring, 0);
  1378. intel_ring_emit(ring, 0);
  1379. }
  1380. if (dc_flush_wa) {
  1381. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1382. intel_ring_emit(ring, PIPE_CONTROL_DC_FLUSH_ENABLE);
  1383. intel_ring_emit(ring, 0);
  1384. intel_ring_emit(ring, 0);
  1385. intel_ring_emit(ring, 0);
  1386. intel_ring_emit(ring, 0);
  1387. }
  1388. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1389. intel_ring_emit(ring, flags);
  1390. intel_ring_emit(ring, scratch_addr);
  1391. intel_ring_emit(ring, 0);
  1392. intel_ring_emit(ring, 0);
  1393. intel_ring_emit(ring, 0);
  1394. if (dc_flush_wa) {
  1395. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  1396. intel_ring_emit(ring, PIPE_CONTROL_CS_STALL);
  1397. intel_ring_emit(ring, 0);
  1398. intel_ring_emit(ring, 0);
  1399. intel_ring_emit(ring, 0);
  1400. intel_ring_emit(ring, 0);
  1401. }
  1402. intel_ring_advance(ring);
  1403. return 0;
  1404. }
  1405. static void bxt_a_seqno_barrier(struct intel_engine_cs *engine)
  1406. {
  1407. /*
  1408. * On BXT A steppings there is a HW coherency issue whereby the
  1409. * MI_STORE_DATA_IMM storing the completed request's seqno
  1410. * occasionally doesn't invalidate the CPU cache. Work around this by
  1411. * clflushing the corresponding cacheline whenever the caller wants
  1412. * the coherency to be guaranteed. Note that this cacheline is known
  1413. * to be clean at this point, since we only write it in
  1414. * bxt_a_set_seqno(), where we also do a clflush after the write. So
  1415. * this clflush in practice becomes an invalidate operation.
  1416. */
  1417. intel_flush_status_page(engine, I915_GEM_HWS_INDEX);
  1418. }
  1419. /*
  1420. * Reserve space for 2 NOOPs at the end of each request to be
  1421. * used as a workaround for not being allowed to do lite
  1422. * restore with HEAD==TAIL (WaIdleLiteRestore).
  1423. */
  1424. static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *out)
  1425. {
  1426. *out++ = MI_NOOP;
  1427. *out++ = MI_NOOP;
  1428. request->wa_tail = intel_ring_offset(request->ring, out);
  1429. }
  1430. static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request,
  1431. u32 *out)
  1432. {
  1433. /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
  1434. BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
  1435. *out++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
  1436. *out++ = intel_hws_seqno_address(request->engine) | MI_FLUSH_DW_USE_GTT;
  1437. *out++ = 0;
  1438. *out++ = request->global_seqno;
  1439. *out++ = MI_USER_INTERRUPT;
  1440. *out++ = MI_NOOP;
  1441. request->tail = intel_ring_offset(request->ring, out);
  1442. gen8_emit_wa_tail(request, out);
  1443. }
  1444. static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
  1445. static void gen8_emit_breadcrumb_render(struct drm_i915_gem_request *request,
  1446. u32 *out)
  1447. {
  1448. /* We're using qword write, seqno should be aligned to 8 bytes. */
  1449. BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
  1450. /* w/a for post sync ops following a GPGPU operation we
  1451. * need a prior CS_STALL, which is emitted by the flush
  1452. * following the batch.
  1453. */
  1454. *out++ = GFX_OP_PIPE_CONTROL(6);
  1455. *out++ = (PIPE_CONTROL_GLOBAL_GTT_IVB |
  1456. PIPE_CONTROL_CS_STALL |
  1457. PIPE_CONTROL_QW_WRITE);
  1458. *out++ = intel_hws_seqno_address(request->engine);
  1459. *out++ = 0;
  1460. *out++ = request->global_seqno;
  1461. /* We're thrashing one dword of HWS. */
  1462. *out++ = 0;
  1463. *out++ = MI_USER_INTERRUPT;
  1464. *out++ = MI_NOOP;
  1465. request->tail = intel_ring_offset(request->ring, out);
  1466. gen8_emit_wa_tail(request, out);
  1467. }
  1468. static const int gen8_emit_breadcrumb_render_sz = 8 + WA_TAIL_DWORDS;
  1469. static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
  1470. {
  1471. int ret;
  1472. ret = intel_logical_ring_workarounds_emit(req);
  1473. if (ret)
  1474. return ret;
  1475. ret = intel_rcs_context_init_mocs(req);
  1476. /*
  1477. * Failing to program the MOCS is non-fatal.The system will not
  1478. * run at peak performance. So generate an error and carry on.
  1479. */
  1480. if (ret)
  1481. DRM_ERROR("MOCS failed to program: expect performance issues.\n");
  1482. return i915_gem_render_state_emit(req);
  1483. }
  1484. /**
  1485. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1486. * @engine: Engine Command Streamer.
  1487. */
  1488. void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
  1489. {
  1490. struct drm_i915_private *dev_priv;
  1491. /*
  1492. * Tasklet cannot be active at this point due intel_mark_active/idle
  1493. * so this is just for documentation.
  1494. */
  1495. if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->irq_tasklet.state)))
  1496. tasklet_kill(&engine->irq_tasklet);
  1497. dev_priv = engine->i915;
  1498. if (engine->buffer) {
  1499. WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1500. }
  1501. if (engine->cleanup)
  1502. engine->cleanup(engine);
  1503. intel_engine_cleanup_common(engine);
  1504. if (engine->status_page.vma) {
  1505. i915_gem_object_unpin_map(engine->status_page.vma->obj);
  1506. engine->status_page.vma = NULL;
  1507. }
  1508. intel_lr_context_unpin(dev_priv->kernel_context, engine);
  1509. lrc_destroy_wa_ctx_obj(engine);
  1510. engine->i915 = NULL;
  1511. dev_priv->engine[engine->id] = NULL;
  1512. kfree(engine);
  1513. }
  1514. void intel_execlists_enable_submission(struct drm_i915_private *dev_priv)
  1515. {
  1516. struct intel_engine_cs *engine;
  1517. enum intel_engine_id id;
  1518. for_each_engine(engine, dev_priv, id) {
  1519. engine->submit_request = execlists_submit_request;
  1520. engine->schedule = execlists_schedule;
  1521. }
  1522. }
  1523. static void
  1524. logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  1525. {
  1526. /* Default vfuncs which can be overriden by each engine. */
  1527. engine->init_hw = gen8_init_common_ring;
  1528. engine->reset_hw = reset_common_ring;
  1529. engine->emit_flush = gen8_emit_flush;
  1530. engine->emit_breadcrumb = gen8_emit_breadcrumb;
  1531. engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
  1532. engine->submit_request = execlists_submit_request;
  1533. engine->schedule = execlists_schedule;
  1534. engine->irq_enable = gen8_logical_ring_enable_irq;
  1535. engine->irq_disable = gen8_logical_ring_disable_irq;
  1536. engine->emit_bb_start = gen8_emit_bb_start;
  1537. if (IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1))
  1538. engine->irq_seqno_barrier = bxt_a_seqno_barrier;
  1539. }
  1540. static inline void
  1541. logical_ring_default_irqs(struct intel_engine_cs *engine)
  1542. {
  1543. unsigned shift = engine->irq_shift;
  1544. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
  1545. engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
  1546. }
  1547. static int
  1548. lrc_setup_hws(struct intel_engine_cs *engine, struct i915_vma *vma)
  1549. {
  1550. const int hws_offset = LRC_PPHWSP_PN * PAGE_SIZE;
  1551. void *hws;
  1552. /* The HWSP is part of the default context object in LRC mode. */
  1553. hws = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  1554. if (IS_ERR(hws))
  1555. return PTR_ERR(hws);
  1556. engine->status_page.page_addr = hws + hws_offset;
  1557. engine->status_page.ggtt_offset = i915_ggtt_offset(vma) + hws_offset;
  1558. engine->status_page.vma = vma;
  1559. return 0;
  1560. }
  1561. static void
  1562. logical_ring_setup(struct intel_engine_cs *engine)
  1563. {
  1564. struct drm_i915_private *dev_priv = engine->i915;
  1565. enum forcewake_domains fw_domains;
  1566. intel_engine_setup_common(engine);
  1567. /* Intentionally left blank. */
  1568. engine->buffer = NULL;
  1569. fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
  1570. RING_ELSP(engine),
  1571. FW_REG_WRITE);
  1572. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1573. RING_CONTEXT_STATUS_PTR(engine),
  1574. FW_REG_READ | FW_REG_WRITE);
  1575. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1576. RING_CONTEXT_STATUS_BUF_BASE(engine),
  1577. FW_REG_READ);
  1578. engine->fw_domains = fw_domains;
  1579. tasklet_init(&engine->irq_tasklet,
  1580. intel_lrc_irq_handler, (unsigned long)engine);
  1581. logical_ring_init_platform_invariants(engine);
  1582. logical_ring_default_vfuncs(engine);
  1583. logical_ring_default_irqs(engine);
  1584. }
  1585. static int
  1586. logical_ring_init(struct intel_engine_cs *engine)
  1587. {
  1588. struct i915_gem_context *dctx = engine->i915->kernel_context;
  1589. int ret;
  1590. ret = intel_engine_init_common(engine);
  1591. if (ret)
  1592. goto error;
  1593. ret = execlists_context_deferred_alloc(dctx, engine);
  1594. if (ret)
  1595. goto error;
  1596. /* As this is the default context, always pin it */
  1597. ret = intel_lr_context_pin(dctx, engine);
  1598. if (ret) {
  1599. DRM_ERROR("Failed to pin context for %s: %d\n",
  1600. engine->name, ret);
  1601. goto error;
  1602. }
  1603. /* And setup the hardware status page. */
  1604. ret = lrc_setup_hws(engine, dctx->engine[engine->id].state);
  1605. if (ret) {
  1606. DRM_ERROR("Failed to set up hws %s: %d\n", engine->name, ret);
  1607. goto error;
  1608. }
  1609. return 0;
  1610. error:
  1611. intel_logical_ring_cleanup(engine);
  1612. return ret;
  1613. }
  1614. int logical_render_ring_init(struct intel_engine_cs *engine)
  1615. {
  1616. struct drm_i915_private *dev_priv = engine->i915;
  1617. int ret;
  1618. logical_ring_setup(engine);
  1619. if (HAS_L3_DPF(dev_priv))
  1620. engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1621. /* Override some for render ring. */
  1622. if (INTEL_GEN(dev_priv) >= 9)
  1623. engine->init_hw = gen9_init_render_ring;
  1624. else
  1625. engine->init_hw = gen8_init_render_ring;
  1626. engine->init_context = gen8_init_rcs_context;
  1627. engine->emit_flush = gen8_emit_flush_render;
  1628. engine->emit_breadcrumb = gen8_emit_breadcrumb_render;
  1629. engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_render_sz;
  1630. ret = intel_engine_create_scratch(engine, 4096);
  1631. if (ret)
  1632. return ret;
  1633. ret = intel_init_workaround_bb(engine);
  1634. if (ret) {
  1635. /*
  1636. * We continue even if we fail to initialize WA batch
  1637. * because we only expect rare glitches but nothing
  1638. * critical to prevent us from using GPU
  1639. */
  1640. DRM_ERROR("WA batch buffer initialization failed: %d\n",
  1641. ret);
  1642. }
  1643. ret = logical_ring_init(engine);
  1644. if (ret) {
  1645. lrc_destroy_wa_ctx_obj(engine);
  1646. }
  1647. return ret;
  1648. }
  1649. int logical_xcs_ring_init(struct intel_engine_cs *engine)
  1650. {
  1651. logical_ring_setup(engine);
  1652. return logical_ring_init(engine);
  1653. }
  1654. static u32
  1655. make_rpcs(struct drm_i915_private *dev_priv)
  1656. {
  1657. u32 rpcs = 0;
  1658. /*
  1659. * No explicit RPCS request is needed to ensure full
  1660. * slice/subslice/EU enablement prior to Gen9.
  1661. */
  1662. if (INTEL_GEN(dev_priv) < 9)
  1663. return 0;
  1664. /*
  1665. * Starting in Gen9, render power gating can leave
  1666. * slice/subslice/EU in a partially enabled state. We
  1667. * must make an explicit request through RPCS for full
  1668. * enablement.
  1669. */
  1670. if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
  1671. rpcs |= GEN8_RPCS_S_CNT_ENABLE;
  1672. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
  1673. GEN8_RPCS_S_CNT_SHIFT;
  1674. rpcs |= GEN8_RPCS_ENABLE;
  1675. }
  1676. if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
  1677. rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
  1678. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
  1679. GEN8_RPCS_SS_CNT_SHIFT;
  1680. rpcs |= GEN8_RPCS_ENABLE;
  1681. }
  1682. if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
  1683. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  1684. GEN8_RPCS_EU_MIN_SHIFT;
  1685. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  1686. GEN8_RPCS_EU_MAX_SHIFT;
  1687. rpcs |= GEN8_RPCS_ENABLE;
  1688. }
  1689. return rpcs;
  1690. }
  1691. static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
  1692. {
  1693. u32 indirect_ctx_offset;
  1694. switch (INTEL_GEN(engine->i915)) {
  1695. default:
  1696. MISSING_CASE(INTEL_GEN(engine->i915));
  1697. /* fall through */
  1698. case 9:
  1699. indirect_ctx_offset =
  1700. GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1701. break;
  1702. case 8:
  1703. indirect_ctx_offset =
  1704. GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1705. break;
  1706. }
  1707. return indirect_ctx_offset;
  1708. }
  1709. static void execlists_init_reg_state(u32 *reg_state,
  1710. struct i915_gem_context *ctx,
  1711. struct intel_engine_cs *engine,
  1712. struct intel_ring *ring)
  1713. {
  1714. struct drm_i915_private *dev_priv = engine->i915;
  1715. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
  1716. /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
  1717. * commands followed by (reg, value) pairs. The values we are setting here are
  1718. * only for the first context restore: on a subsequent save, the GPU will
  1719. * recreate this batchbuffer with new values (including all the missing
  1720. * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
  1721. reg_state[CTX_LRI_HEADER_0] =
  1722. MI_LOAD_REGISTER_IMM(engine->id == RCS ? 14 : 11) | MI_LRI_FORCE_POSTED;
  1723. ASSIGN_CTX_REG(reg_state, CTX_CONTEXT_CONTROL,
  1724. RING_CONTEXT_CONTROL(engine),
  1725. _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
  1726. CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
  1727. (HAS_RESOURCE_STREAMER(dev_priv) ?
  1728. CTX_CTRL_RS_CTX_ENABLE : 0)));
  1729. ASSIGN_CTX_REG(reg_state, CTX_RING_HEAD, RING_HEAD(engine->mmio_base),
  1730. 0);
  1731. ASSIGN_CTX_REG(reg_state, CTX_RING_TAIL, RING_TAIL(engine->mmio_base),
  1732. 0);
  1733. ASSIGN_CTX_REG(reg_state, CTX_RING_BUFFER_START,
  1734. RING_START(engine->mmio_base), 0);
  1735. ASSIGN_CTX_REG(reg_state, CTX_RING_BUFFER_CONTROL,
  1736. RING_CTL(engine->mmio_base),
  1737. RING_CTL_SIZE(ring->size) | RING_VALID);
  1738. ASSIGN_CTX_REG(reg_state, CTX_BB_HEAD_U,
  1739. RING_BBADDR_UDW(engine->mmio_base), 0);
  1740. ASSIGN_CTX_REG(reg_state, CTX_BB_HEAD_L,
  1741. RING_BBADDR(engine->mmio_base), 0);
  1742. ASSIGN_CTX_REG(reg_state, CTX_BB_STATE,
  1743. RING_BBSTATE(engine->mmio_base),
  1744. RING_BB_PPGTT);
  1745. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_HEAD_U,
  1746. RING_SBBADDR_UDW(engine->mmio_base), 0);
  1747. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_HEAD_L,
  1748. RING_SBBADDR(engine->mmio_base), 0);
  1749. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_STATE,
  1750. RING_SBBSTATE(engine->mmio_base), 0);
  1751. if (engine->id == RCS) {
  1752. ASSIGN_CTX_REG(reg_state, CTX_BB_PER_CTX_PTR,
  1753. RING_BB_PER_CTX_PTR(engine->mmio_base), 0);
  1754. ASSIGN_CTX_REG(reg_state, CTX_RCS_INDIRECT_CTX,
  1755. RING_INDIRECT_CTX(engine->mmio_base), 0);
  1756. ASSIGN_CTX_REG(reg_state, CTX_RCS_INDIRECT_CTX_OFFSET,
  1757. RING_INDIRECT_CTX_OFFSET(engine->mmio_base), 0);
  1758. if (engine->wa_ctx.vma) {
  1759. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1760. u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
  1761. reg_state[CTX_RCS_INDIRECT_CTX+1] =
  1762. (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
  1763. (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
  1764. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
  1765. intel_lr_indirect_ctx_offset(engine) << 6;
  1766. reg_state[CTX_BB_PER_CTX_PTR+1] =
  1767. (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
  1768. 0x01;
  1769. }
  1770. }
  1771. reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
  1772. ASSIGN_CTX_REG(reg_state, CTX_CTX_TIMESTAMP,
  1773. RING_CTX_TIMESTAMP(engine->mmio_base), 0);
  1774. /* PDP values well be assigned later if needed */
  1775. ASSIGN_CTX_REG(reg_state, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3),
  1776. 0);
  1777. ASSIGN_CTX_REG(reg_state, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3),
  1778. 0);
  1779. ASSIGN_CTX_REG(reg_state, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2),
  1780. 0);
  1781. ASSIGN_CTX_REG(reg_state, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2),
  1782. 0);
  1783. ASSIGN_CTX_REG(reg_state, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1),
  1784. 0);
  1785. ASSIGN_CTX_REG(reg_state, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1),
  1786. 0);
  1787. ASSIGN_CTX_REG(reg_state, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0),
  1788. 0);
  1789. ASSIGN_CTX_REG(reg_state, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0),
  1790. 0);
  1791. if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
  1792. /* 64b PPGTT (48bit canonical)
  1793. * PDP0_DESCRIPTOR contains the base address to PML4 and
  1794. * other PDP Descriptors are ignored.
  1795. */
  1796. ASSIGN_CTX_PML4(ppgtt, reg_state);
  1797. } else {
  1798. /* 32b PPGTT
  1799. * PDP*_DESCRIPTOR contains the base address of space supported.
  1800. * With dynamic page allocation, PDPs may not be allocated at
  1801. * this point. Point the unallocated PDPs to the scratch page
  1802. */
  1803. execlists_update_context_pdps(ppgtt, reg_state);
  1804. }
  1805. if (engine->id == RCS) {
  1806. reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  1807. ASSIGN_CTX_REG(reg_state, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
  1808. make_rpcs(dev_priv));
  1809. }
  1810. }
  1811. static int
  1812. populate_lr_context(struct i915_gem_context *ctx,
  1813. struct drm_i915_gem_object *ctx_obj,
  1814. struct intel_engine_cs *engine,
  1815. struct intel_ring *ring)
  1816. {
  1817. void *vaddr;
  1818. int ret;
  1819. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1820. if (ret) {
  1821. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1822. return ret;
  1823. }
  1824. vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
  1825. if (IS_ERR(vaddr)) {
  1826. ret = PTR_ERR(vaddr);
  1827. DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
  1828. return ret;
  1829. }
  1830. ctx_obj->mm.dirty = true;
  1831. /* The second page of the context object contains some fields which must
  1832. * be set up prior to the first execution. */
  1833. execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
  1834. ctx, engine, ring);
  1835. i915_gem_object_unpin_map(ctx_obj);
  1836. return 0;
  1837. }
  1838. /**
  1839. * intel_lr_context_size() - return the size of the context for an engine
  1840. * @engine: which engine to find the context size for
  1841. *
  1842. * Each engine may require a different amount of space for a context image,
  1843. * so when allocating (or copying) an image, this function can be used to
  1844. * find the right size for the specific engine.
  1845. *
  1846. * Return: size (in bytes) of an engine-specific context image
  1847. *
  1848. * Note: this size includes the HWSP, which is part of the context image
  1849. * in LRC mode, but does not include the "shared data page" used with
  1850. * GuC submission. The caller should account for this if using the GuC.
  1851. */
  1852. uint32_t intel_lr_context_size(struct intel_engine_cs *engine)
  1853. {
  1854. int ret = 0;
  1855. WARN_ON(INTEL_GEN(engine->i915) < 8);
  1856. switch (engine->id) {
  1857. case RCS:
  1858. if (INTEL_GEN(engine->i915) >= 9)
  1859. ret = GEN9_LR_CONTEXT_RENDER_SIZE;
  1860. else
  1861. ret = GEN8_LR_CONTEXT_RENDER_SIZE;
  1862. break;
  1863. case VCS:
  1864. case BCS:
  1865. case VECS:
  1866. case VCS2:
  1867. ret = GEN8_LR_CONTEXT_OTHER_SIZE;
  1868. break;
  1869. }
  1870. return ret;
  1871. }
  1872. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  1873. struct intel_engine_cs *engine)
  1874. {
  1875. struct drm_i915_gem_object *ctx_obj;
  1876. struct intel_context *ce = &ctx->engine[engine->id];
  1877. struct i915_vma *vma;
  1878. uint32_t context_size;
  1879. struct intel_ring *ring;
  1880. int ret;
  1881. WARN_ON(ce->state);
  1882. context_size = round_up(intel_lr_context_size(engine), 4096);
  1883. /* One extra page as the sharing data between driver and GuC */
  1884. context_size += PAGE_SIZE * LRC_PPHWSP_PN;
  1885. ctx_obj = i915_gem_object_create(ctx->i915, context_size);
  1886. if (IS_ERR(ctx_obj)) {
  1887. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
  1888. return PTR_ERR(ctx_obj);
  1889. }
  1890. vma = i915_vma_create(ctx_obj, &ctx->i915->ggtt.base, NULL);
  1891. if (IS_ERR(vma)) {
  1892. ret = PTR_ERR(vma);
  1893. goto error_deref_obj;
  1894. }
  1895. ring = intel_engine_create_ring(engine, ctx->ring_size);
  1896. if (IS_ERR(ring)) {
  1897. ret = PTR_ERR(ring);
  1898. goto error_deref_obj;
  1899. }
  1900. ret = populate_lr_context(ctx, ctx_obj, engine, ring);
  1901. if (ret) {
  1902. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  1903. goto error_ring_free;
  1904. }
  1905. ce->ring = ring;
  1906. ce->state = vma;
  1907. ce->initialised = engine->init_context == NULL;
  1908. return 0;
  1909. error_ring_free:
  1910. intel_ring_free(ring);
  1911. error_deref_obj:
  1912. i915_gem_object_put(ctx_obj);
  1913. return ret;
  1914. }
  1915. void intel_lr_context_resume(struct drm_i915_private *dev_priv)
  1916. {
  1917. struct intel_engine_cs *engine;
  1918. struct i915_gem_context *ctx;
  1919. enum intel_engine_id id;
  1920. /* Because we emit WA_TAIL_DWORDS there may be a disparity
  1921. * between our bookkeeping in ce->ring->head and ce->ring->tail and
  1922. * that stored in context. As we only write new commands from
  1923. * ce->ring->tail onwards, everything before that is junk. If the GPU
  1924. * starts reading from its RING_HEAD from the context, it may try to
  1925. * execute that junk and die.
  1926. *
  1927. * So to avoid that we reset the context images upon resume. For
  1928. * simplicity, we just zero everything out.
  1929. */
  1930. list_for_each_entry(ctx, &dev_priv->context_list, link) {
  1931. for_each_engine(engine, dev_priv, id) {
  1932. struct intel_context *ce = &ctx->engine[engine->id];
  1933. u32 *reg;
  1934. if (!ce->state)
  1935. continue;
  1936. reg = i915_gem_object_pin_map(ce->state->obj,
  1937. I915_MAP_WB);
  1938. if (WARN_ON(IS_ERR(reg)))
  1939. continue;
  1940. reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
  1941. reg[CTX_RING_HEAD+1] = 0;
  1942. reg[CTX_RING_TAIL+1] = 0;
  1943. ce->state->obj->mm.dirty = true;
  1944. i915_gem_object_unpin_map(ce->state->obj);
  1945. ce->ring->head = ce->ring->tail = 0;
  1946. ce->ring->last_retired_head = -1;
  1947. intel_ring_update_space(ce->ring);
  1948. }
  1949. }
  1950. }