intel_lrc.c 62 KB

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