intel_lrc.c 64 KB

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