intel_lrc.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  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 CTX_LRI_HEADER_0 0x01
  154. #define CTX_CONTEXT_CONTROL 0x02
  155. #define CTX_RING_HEAD 0x04
  156. #define CTX_RING_TAIL 0x06
  157. #define CTX_RING_BUFFER_START 0x08
  158. #define CTX_RING_BUFFER_CONTROL 0x0a
  159. #define CTX_BB_HEAD_U 0x0c
  160. #define CTX_BB_HEAD_L 0x0e
  161. #define CTX_BB_STATE 0x10
  162. #define CTX_SECOND_BB_HEAD_U 0x12
  163. #define CTX_SECOND_BB_HEAD_L 0x14
  164. #define CTX_SECOND_BB_STATE 0x16
  165. #define CTX_BB_PER_CTX_PTR 0x18
  166. #define CTX_RCS_INDIRECT_CTX 0x1a
  167. #define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
  168. #define CTX_LRI_HEADER_1 0x21
  169. #define CTX_CTX_TIMESTAMP 0x22
  170. #define CTX_PDP3_UDW 0x24
  171. #define CTX_PDP3_LDW 0x26
  172. #define CTX_PDP2_UDW 0x28
  173. #define CTX_PDP2_LDW 0x2a
  174. #define CTX_PDP1_UDW 0x2c
  175. #define CTX_PDP1_LDW 0x2e
  176. #define CTX_PDP0_UDW 0x30
  177. #define CTX_PDP0_LDW 0x32
  178. #define CTX_LRI_HEADER_2 0x41
  179. #define CTX_R_PWR_CLK_STATE 0x42
  180. #define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
  181. #define GEN8_CTX_VALID (1<<0)
  182. #define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
  183. #define GEN8_CTX_FORCE_RESTORE (1<<2)
  184. #define GEN8_CTX_L3LLC_COHERENT (1<<5)
  185. #define GEN8_CTX_PRIVILEGE (1<<8)
  186. #define ASSIGN_CTX_REG(reg_state, pos, reg, val) do { \
  187. (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
  188. (reg_state)[(pos)+1] = (val); \
  189. } while (0)
  190. #define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \
  191. const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
  192. reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
  193. reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
  194. } while (0)
  195. #define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
  196. reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \
  197. reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \
  198. } while (0)
  199. enum {
  200. FAULT_AND_HANG = 0,
  201. FAULT_AND_HALT, /* Debug only */
  202. FAULT_AND_STREAM,
  203. FAULT_AND_CONTINUE /* Unsupported */
  204. };
  205. #define GEN8_CTX_ID_SHIFT 32
  206. #define GEN8_CTX_ID_WIDTH 21
  207. #define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
  208. #define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26
  209. /* Typical size of the average request (2 pipecontrols and a MI_BB) */
  210. #define EXECLISTS_REQUEST_SIZE 64 /* bytes */
  211. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  212. struct intel_engine_cs *engine);
  213. static int intel_lr_context_pin(struct i915_gem_context *ctx,
  214. struct intel_engine_cs *engine);
  215. /**
  216. * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
  217. * @dev_priv: i915 device private
  218. * @enable_execlists: value of i915.enable_execlists module parameter.
  219. *
  220. * Only certain platforms support Execlists (the prerequisites being
  221. * support for Logical Ring Contexts and Aliasing PPGTT or better).
  222. *
  223. * Return: 1 if Execlists is supported and has to be enabled.
  224. */
  225. int intel_sanitize_enable_execlists(struct drm_i915_private *dev_priv, int enable_execlists)
  226. {
  227. /* On platforms with execlist available, vGPU will only
  228. * support execlist mode, no ring buffer mode.
  229. */
  230. if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) && intel_vgpu_active(dev_priv))
  231. return 1;
  232. if (INTEL_GEN(dev_priv) >= 9)
  233. return 1;
  234. if (enable_execlists == 0)
  235. return 0;
  236. if (HAS_LOGICAL_RING_CONTEXTS(dev_priv) &&
  237. USES_PPGTT(dev_priv) &&
  238. i915.use_mmio_flip >= 0)
  239. return 1;
  240. return 0;
  241. }
  242. static void
  243. logical_ring_init_platform_invariants(struct intel_engine_cs *engine)
  244. {
  245. struct drm_i915_private *dev_priv = engine->i915;
  246. if (IS_GEN8(dev_priv) || IS_GEN9(dev_priv))
  247. engine->idle_lite_restore_wa = ~0;
  248. engine->disable_lite_restore_wa = (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
  249. IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) &&
  250. (engine->id == VCS || engine->id == VCS2);
  251. engine->ctx_desc_template = GEN8_CTX_VALID;
  252. if (IS_GEN8(dev_priv))
  253. engine->ctx_desc_template |= GEN8_CTX_L3LLC_COHERENT;
  254. engine->ctx_desc_template |= GEN8_CTX_PRIVILEGE;
  255. /* TODO: WaDisableLiteRestore when we start using semaphore
  256. * signalling between Command Streamers */
  257. /* ring->ctx_desc_template |= GEN8_CTX_FORCE_RESTORE; */
  258. /* WaEnableForceRestoreInCtxtDescForVCS:skl */
  259. /* WaEnableForceRestoreInCtxtDescForVCS:bxt */
  260. if (engine->disable_lite_restore_wa)
  261. engine->ctx_desc_template |= GEN8_CTX_FORCE_RESTORE;
  262. }
  263. /**
  264. * intel_lr_context_descriptor_update() - calculate & cache the descriptor
  265. * descriptor for a pinned context
  266. *
  267. * @ctx: Context to work on
  268. * @engine: Engine the descriptor will be used with
  269. *
  270. * The context descriptor encodes various attributes of a context,
  271. * including its GTT address and some flags. Because it's fairly
  272. * expensive to calculate, we'll just do it once and cache the result,
  273. * which remains valid until the context is unpinned.
  274. *
  275. * This is what a descriptor looks like, from LSB to MSB:
  276. * bits 0-11: flags, GEN8_CTX_* (cached in ctx_desc_template)
  277. * bits 12-31: LRCA, GTT address of (the HWSP of) this context
  278. * bits 32-52: ctx ID, a globally unique tag
  279. * bits 53-54: mbz, reserved for use by hardware
  280. * bits 55-63: group ID, currently unused and set to 0
  281. */
  282. static void
  283. intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
  284. struct intel_engine_cs *engine)
  285. {
  286. struct intel_context *ce = &ctx->engine[engine->id];
  287. u64 desc;
  288. BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
  289. desc = ctx->desc_template; /* bits 3-4 */
  290. desc |= engine->ctx_desc_template; /* bits 0-11 */
  291. desc |= ce->lrc_vma->node.start + LRC_PPHWSP_PN * PAGE_SIZE;
  292. /* bits 12-31 */
  293. desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
  294. ce->lrc_desc = desc;
  295. }
  296. uint64_t intel_lr_context_descriptor(struct i915_gem_context *ctx,
  297. struct intel_engine_cs *engine)
  298. {
  299. return ctx->engine[engine->id].lrc_desc;
  300. }
  301. static void execlists_elsp_write(struct drm_i915_gem_request *rq0,
  302. struct drm_i915_gem_request *rq1)
  303. {
  304. struct intel_engine_cs *engine = rq0->engine;
  305. struct drm_i915_private *dev_priv = rq0->i915;
  306. uint64_t desc[2];
  307. if (rq1) {
  308. desc[1] = intel_lr_context_descriptor(rq1->ctx, rq1->engine);
  309. rq1->elsp_submitted++;
  310. } else {
  311. desc[1] = 0;
  312. }
  313. desc[0] = intel_lr_context_descriptor(rq0->ctx, rq0->engine);
  314. rq0->elsp_submitted++;
  315. /* You must always write both descriptors in the order below. */
  316. I915_WRITE_FW(RING_ELSP(engine), upper_32_bits(desc[1]));
  317. I915_WRITE_FW(RING_ELSP(engine), lower_32_bits(desc[1]));
  318. I915_WRITE_FW(RING_ELSP(engine), upper_32_bits(desc[0]));
  319. /* The context is automatically loaded after the following */
  320. I915_WRITE_FW(RING_ELSP(engine), lower_32_bits(desc[0]));
  321. /* ELSP is a wo register, use another nearby reg for posting */
  322. POSTING_READ_FW(RING_EXECLIST_STATUS_LO(engine));
  323. }
  324. static void
  325. execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
  326. {
  327. ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
  328. ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
  329. ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
  330. ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
  331. }
  332. static void execlists_update_context(struct drm_i915_gem_request *rq)
  333. {
  334. struct intel_engine_cs *engine = rq->engine;
  335. struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
  336. uint32_t *reg_state = rq->ctx->engine[engine->id].lrc_reg_state;
  337. reg_state[CTX_RING_TAIL+1] = rq->tail;
  338. /* True 32b PPGTT with dynamic page allocation: update PDP
  339. * registers and point the unallocated PDPs to scratch page.
  340. * PML4 is allocated during ppgtt init, so this is not needed
  341. * in 48-bit mode.
  342. */
  343. if (ppgtt && !USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
  344. execlists_update_context_pdps(ppgtt, reg_state);
  345. }
  346. static void execlists_submit_requests(struct drm_i915_gem_request *rq0,
  347. struct drm_i915_gem_request *rq1)
  348. {
  349. struct drm_i915_private *dev_priv = rq0->i915;
  350. unsigned int fw_domains = rq0->engine->fw_domains;
  351. execlists_update_context(rq0);
  352. if (rq1)
  353. execlists_update_context(rq1);
  354. spin_lock_irq(&dev_priv->uncore.lock);
  355. intel_uncore_forcewake_get__locked(dev_priv, fw_domains);
  356. execlists_elsp_write(rq0, rq1);
  357. intel_uncore_forcewake_put__locked(dev_priv, fw_domains);
  358. spin_unlock_irq(&dev_priv->uncore.lock);
  359. }
  360. static inline void execlists_context_status_change(
  361. struct drm_i915_gem_request *rq,
  362. unsigned long status)
  363. {
  364. /*
  365. * Only used when GVT-g is enabled now. When GVT-g is disabled,
  366. * The compiler should eliminate this function as dead-code.
  367. */
  368. if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
  369. return;
  370. atomic_notifier_call_chain(&rq->ctx->status_notifier, status, rq);
  371. }
  372. static void execlists_context_unqueue(struct intel_engine_cs *engine)
  373. {
  374. struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
  375. struct drm_i915_gem_request *cursor, *tmp;
  376. assert_spin_locked(&engine->execlist_lock);
  377. /*
  378. * If irqs are not active generate a warning as batches that finish
  379. * without the irqs may get lost and a GPU Hang may occur.
  380. */
  381. WARN_ON(!intel_irqs_enabled(engine->i915));
  382. /* Try to read in pairs */
  383. list_for_each_entry_safe(cursor, tmp, &engine->execlist_queue,
  384. execlist_link) {
  385. if (!req0) {
  386. req0 = cursor;
  387. } else if (req0->ctx == cursor->ctx) {
  388. /* Same ctx: ignore first request, as second request
  389. * will update tail past first request's workload */
  390. cursor->elsp_submitted = req0->elsp_submitted;
  391. list_del(&req0->execlist_link);
  392. i915_gem_request_unreference(req0);
  393. req0 = cursor;
  394. } else {
  395. if (IS_ENABLED(CONFIG_DRM_I915_GVT)) {
  396. /*
  397. * req0 (after merged) ctx requires single
  398. * submission, stop picking
  399. */
  400. if (req0->ctx->execlists_force_single_submission)
  401. break;
  402. /*
  403. * req0 ctx doesn't require single submission,
  404. * but next req ctx requires, stop picking
  405. */
  406. if (cursor->ctx->execlists_force_single_submission)
  407. break;
  408. }
  409. req1 = cursor;
  410. WARN_ON(req1->elsp_submitted);
  411. break;
  412. }
  413. }
  414. if (unlikely(!req0))
  415. return;
  416. execlists_context_status_change(req0, INTEL_CONTEXT_SCHEDULE_IN);
  417. if (req1)
  418. execlists_context_status_change(req1,
  419. INTEL_CONTEXT_SCHEDULE_IN);
  420. if (req0->elsp_submitted & engine->idle_lite_restore_wa) {
  421. /*
  422. * WaIdleLiteRestore: make sure we never cause a lite restore
  423. * with HEAD==TAIL.
  424. *
  425. * Apply the wa NOOPS to prevent ring:HEAD == req:TAIL as we
  426. * resubmit the request. See gen8_emit_request() for where we
  427. * prepare the padding after the end of the request.
  428. */
  429. struct intel_ringbuffer *ringbuf;
  430. ringbuf = req0->ctx->engine[engine->id].ringbuf;
  431. req0->tail += 8;
  432. req0->tail &= ringbuf->size - 1;
  433. }
  434. execlists_submit_requests(req0, req1);
  435. }
  436. static unsigned int
  437. execlists_check_remove_request(struct intel_engine_cs *engine, u32 ctx_id)
  438. {
  439. struct drm_i915_gem_request *head_req;
  440. assert_spin_locked(&engine->execlist_lock);
  441. head_req = list_first_entry_or_null(&engine->execlist_queue,
  442. struct drm_i915_gem_request,
  443. execlist_link);
  444. if (WARN_ON(!head_req || (head_req->ctx_hw_id != ctx_id)))
  445. return 0;
  446. WARN(head_req->elsp_submitted == 0, "Never submitted head request\n");
  447. if (--head_req->elsp_submitted > 0)
  448. return 0;
  449. execlists_context_status_change(head_req, INTEL_CONTEXT_SCHEDULE_OUT);
  450. list_del(&head_req->execlist_link);
  451. i915_gem_request_unreference(head_req);
  452. return 1;
  453. }
  454. static u32
  455. get_context_status(struct intel_engine_cs *engine, unsigned int read_pointer,
  456. u32 *context_id)
  457. {
  458. struct drm_i915_private *dev_priv = engine->i915;
  459. u32 status;
  460. read_pointer %= GEN8_CSB_ENTRIES;
  461. status = I915_READ_FW(RING_CONTEXT_STATUS_BUF_LO(engine, read_pointer));
  462. if (status & GEN8_CTX_STATUS_IDLE_ACTIVE)
  463. return 0;
  464. *context_id = I915_READ_FW(RING_CONTEXT_STATUS_BUF_HI(engine,
  465. read_pointer));
  466. return status;
  467. }
  468. /**
  469. * intel_lrc_irq_handler() - handle Context Switch interrupts
  470. * @data: tasklet handler passed in unsigned long
  471. *
  472. * Check the unread Context Status Buffers and manage the submission of new
  473. * contexts to the ELSP accordingly.
  474. */
  475. static void intel_lrc_irq_handler(unsigned long data)
  476. {
  477. struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
  478. struct drm_i915_private *dev_priv = engine->i915;
  479. u32 status_pointer;
  480. unsigned int read_pointer, write_pointer;
  481. u32 csb[GEN8_CSB_ENTRIES][2];
  482. unsigned int csb_read = 0, i;
  483. unsigned int submit_contexts = 0;
  484. intel_uncore_forcewake_get(dev_priv, engine->fw_domains);
  485. status_pointer = I915_READ_FW(RING_CONTEXT_STATUS_PTR(engine));
  486. read_pointer = engine->next_context_status_buffer;
  487. write_pointer = GEN8_CSB_WRITE_PTR(status_pointer);
  488. if (read_pointer > write_pointer)
  489. write_pointer += GEN8_CSB_ENTRIES;
  490. while (read_pointer < write_pointer) {
  491. if (WARN_ON_ONCE(csb_read == GEN8_CSB_ENTRIES))
  492. break;
  493. csb[csb_read][0] = get_context_status(engine, ++read_pointer,
  494. &csb[csb_read][1]);
  495. csb_read++;
  496. }
  497. engine->next_context_status_buffer = write_pointer % GEN8_CSB_ENTRIES;
  498. /* Update the read pointer to the old write pointer. Manual ringbuffer
  499. * management ftw </sarcasm> */
  500. I915_WRITE_FW(RING_CONTEXT_STATUS_PTR(engine),
  501. _MASKED_FIELD(GEN8_CSB_READ_PTR_MASK,
  502. engine->next_context_status_buffer << 8));
  503. intel_uncore_forcewake_put(dev_priv, engine->fw_domains);
  504. spin_lock(&engine->execlist_lock);
  505. for (i = 0; i < csb_read; i++) {
  506. if (unlikely(csb[i][0] & GEN8_CTX_STATUS_PREEMPTED)) {
  507. if (csb[i][0] & GEN8_CTX_STATUS_LITE_RESTORE) {
  508. if (execlists_check_remove_request(engine, csb[i][1]))
  509. WARN(1, "Lite Restored request removed from queue\n");
  510. } else
  511. WARN(1, "Preemption without Lite Restore\n");
  512. }
  513. if (csb[i][0] & (GEN8_CTX_STATUS_ACTIVE_IDLE |
  514. GEN8_CTX_STATUS_ELEMENT_SWITCH))
  515. submit_contexts +=
  516. execlists_check_remove_request(engine, csb[i][1]);
  517. }
  518. if (submit_contexts) {
  519. if (!engine->disable_lite_restore_wa ||
  520. (csb[i][0] & GEN8_CTX_STATUS_ACTIVE_IDLE))
  521. execlists_context_unqueue(engine);
  522. }
  523. spin_unlock(&engine->execlist_lock);
  524. if (unlikely(submit_contexts > 2))
  525. DRM_ERROR("More than two context complete events?\n");
  526. }
  527. static void execlists_context_queue(struct drm_i915_gem_request *request)
  528. {
  529. struct intel_engine_cs *engine = request->engine;
  530. struct drm_i915_gem_request *cursor;
  531. int num_elements = 0;
  532. spin_lock_bh(&engine->execlist_lock);
  533. list_for_each_entry(cursor, &engine->execlist_queue, execlist_link)
  534. if (++num_elements > 2)
  535. break;
  536. if (num_elements > 2) {
  537. struct drm_i915_gem_request *tail_req;
  538. tail_req = list_last_entry(&engine->execlist_queue,
  539. struct drm_i915_gem_request,
  540. execlist_link);
  541. if (request->ctx == tail_req->ctx) {
  542. WARN(tail_req->elsp_submitted != 0,
  543. "More than 2 already-submitted reqs queued\n");
  544. list_del(&tail_req->execlist_link);
  545. i915_gem_request_unreference(tail_req);
  546. }
  547. }
  548. i915_gem_request_reference(request);
  549. list_add_tail(&request->execlist_link, &engine->execlist_queue);
  550. request->ctx_hw_id = request->ctx->hw_id;
  551. if (num_elements == 0)
  552. execlists_context_unqueue(engine);
  553. spin_unlock_bh(&engine->execlist_lock);
  554. }
  555. static int logical_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
  556. {
  557. struct intel_engine_cs *engine = req->engine;
  558. uint32_t flush_domains;
  559. int ret;
  560. flush_domains = 0;
  561. if (engine->gpu_caches_dirty)
  562. flush_domains = I915_GEM_GPU_DOMAINS;
  563. ret = engine->emit_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
  564. if (ret)
  565. return ret;
  566. engine->gpu_caches_dirty = false;
  567. return 0;
  568. }
  569. static int execlists_move_to_gpu(struct drm_i915_gem_request *req,
  570. struct list_head *vmas)
  571. {
  572. const unsigned other_rings = ~intel_engine_flag(req->engine);
  573. struct i915_vma *vma;
  574. uint32_t flush_domains = 0;
  575. bool flush_chipset = false;
  576. int ret;
  577. list_for_each_entry(vma, vmas, exec_list) {
  578. struct drm_i915_gem_object *obj = vma->obj;
  579. if (obj->active & other_rings) {
  580. ret = i915_gem_object_sync(obj, req->engine, &req);
  581. if (ret)
  582. return ret;
  583. }
  584. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  585. flush_chipset |= i915_gem_clflush_object(obj, false);
  586. flush_domains |= obj->base.write_domain;
  587. }
  588. if (flush_domains & I915_GEM_DOMAIN_GTT)
  589. wmb();
  590. /* Unconditionally invalidate gpu caches and ensure that we do flush
  591. * any residual writes from the previous batch.
  592. */
  593. return logical_ring_invalidate_all_caches(req);
  594. }
  595. int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request)
  596. {
  597. struct intel_engine_cs *engine = request->engine;
  598. struct intel_context *ce = &request->ctx->engine[engine->id];
  599. int ret;
  600. /* Flush enough space to reduce the likelihood of waiting after
  601. * we start building the request - in which case we will just
  602. * have to repeat work.
  603. */
  604. request->reserved_space += EXECLISTS_REQUEST_SIZE;
  605. if (!ce->state) {
  606. ret = execlists_context_deferred_alloc(request->ctx, engine);
  607. if (ret)
  608. return ret;
  609. }
  610. request->ringbuf = ce->ringbuf;
  611. if (i915.enable_guc_submission) {
  612. /*
  613. * Check that the GuC has space for the request before
  614. * going any further, as the i915_add_request() call
  615. * later on mustn't fail ...
  616. */
  617. ret = i915_guc_wq_check_space(request);
  618. if (ret)
  619. return ret;
  620. }
  621. ret = intel_lr_context_pin(request->ctx, engine);
  622. if (ret)
  623. return ret;
  624. ret = intel_ring_begin(request, 0);
  625. if (ret)
  626. goto err_unpin;
  627. if (!ce->initialised) {
  628. ret = engine->init_context(request);
  629. if (ret)
  630. goto err_unpin;
  631. ce->initialised = true;
  632. }
  633. /* Note that after this point, we have committed to using
  634. * this request as it is being used to both track the
  635. * state of engine initialisation and liveness of the
  636. * golden renderstate above. Think twice before you try
  637. * to cancel/unwind this request now.
  638. */
  639. request->reserved_space -= EXECLISTS_REQUEST_SIZE;
  640. return 0;
  641. err_unpin:
  642. intel_lr_context_unpin(request->ctx, engine);
  643. return ret;
  644. }
  645. /*
  646. * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
  647. * @request: Request to advance the logical ringbuffer of.
  648. *
  649. * The tail is updated in our logical ringbuffer struct, not in the actual context. What
  650. * really happens during submission is that the context and current tail will be placed
  651. * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
  652. * point, the tail *inside* the context is updated and the ELSP written to.
  653. */
  654. static int
  655. intel_logical_ring_advance_and_submit(struct drm_i915_gem_request *request)
  656. {
  657. struct intel_ringbuffer *ringbuf = request->ringbuf;
  658. struct intel_engine_cs *engine = request->engine;
  659. intel_logical_ring_advance(ringbuf);
  660. request->tail = ringbuf->tail;
  661. /*
  662. * Here we add two extra NOOPs as padding to avoid
  663. * lite restore of a context with HEAD==TAIL.
  664. *
  665. * Caller must reserve WA_TAIL_DWORDS for us!
  666. */
  667. intel_logical_ring_emit(ringbuf, MI_NOOP);
  668. intel_logical_ring_emit(ringbuf, MI_NOOP);
  669. intel_logical_ring_advance(ringbuf);
  670. /* We keep the previous context alive until we retire the following
  671. * request. This ensures that any the context object is still pinned
  672. * for any residual writes the HW makes into it on the context switch
  673. * into the next object following the breadcrumb. Otherwise, we may
  674. * retire the context too early.
  675. */
  676. request->previous_context = engine->last_context;
  677. engine->last_context = request->ctx;
  678. if (i915.enable_guc_submission)
  679. i915_guc_submit(request);
  680. else
  681. execlists_context_queue(request);
  682. return 0;
  683. }
  684. /**
  685. * execlists_submission() - submit a batchbuffer for execution, Execlists style
  686. * @params: execbuffer call parameters.
  687. * @args: execbuffer call arguments.
  688. * @vmas: list of vmas.
  689. *
  690. * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
  691. * away the submission details of the execbuffer ioctl call.
  692. *
  693. * Return: non-zero if the submission fails.
  694. */
  695. int intel_execlists_submission(struct i915_execbuffer_params *params,
  696. struct drm_i915_gem_execbuffer2 *args,
  697. struct list_head *vmas)
  698. {
  699. struct drm_device *dev = params->dev;
  700. struct intel_engine_cs *engine = params->engine;
  701. struct drm_i915_private *dev_priv = to_i915(dev);
  702. struct intel_ringbuffer *ringbuf = params->ctx->engine[engine->id].ringbuf;
  703. u64 exec_start;
  704. int instp_mode;
  705. u32 instp_mask;
  706. int ret;
  707. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  708. instp_mask = I915_EXEC_CONSTANTS_MASK;
  709. switch (instp_mode) {
  710. case I915_EXEC_CONSTANTS_REL_GENERAL:
  711. case I915_EXEC_CONSTANTS_ABSOLUTE:
  712. case I915_EXEC_CONSTANTS_REL_SURFACE:
  713. if (instp_mode != 0 && engine != &dev_priv->engine[RCS]) {
  714. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  715. return -EINVAL;
  716. }
  717. if (instp_mode != dev_priv->relative_constants_mode) {
  718. if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  719. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  720. return -EINVAL;
  721. }
  722. /* The HW changed the meaning on this bit on gen6 */
  723. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  724. }
  725. break;
  726. default:
  727. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  728. return -EINVAL;
  729. }
  730. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  731. DRM_DEBUG("sol reset is gen7 only\n");
  732. return -EINVAL;
  733. }
  734. ret = execlists_move_to_gpu(params->request, vmas);
  735. if (ret)
  736. return ret;
  737. if (engine == &dev_priv->engine[RCS] &&
  738. instp_mode != dev_priv->relative_constants_mode) {
  739. ret = intel_ring_begin(params->request, 4);
  740. if (ret)
  741. return ret;
  742. intel_logical_ring_emit(ringbuf, MI_NOOP);
  743. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
  744. intel_logical_ring_emit_reg(ringbuf, INSTPM);
  745. intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
  746. intel_logical_ring_advance(ringbuf);
  747. dev_priv->relative_constants_mode = instp_mode;
  748. }
  749. exec_start = params->batch_obj_vm_offset +
  750. args->batch_start_offset;
  751. ret = engine->emit_bb_start(params->request, exec_start, params->dispatch_flags);
  752. if (ret)
  753. return ret;
  754. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  755. i915_gem_execbuffer_move_to_active(vmas, params->request);
  756. return 0;
  757. }
  758. void intel_execlists_cancel_requests(struct intel_engine_cs *engine)
  759. {
  760. struct drm_i915_gem_request *req, *tmp;
  761. LIST_HEAD(cancel_list);
  762. WARN_ON(!mutex_is_locked(&engine->i915->drm.struct_mutex));
  763. spin_lock_bh(&engine->execlist_lock);
  764. list_replace_init(&engine->execlist_queue, &cancel_list);
  765. spin_unlock_bh(&engine->execlist_lock);
  766. list_for_each_entry_safe(req, tmp, &cancel_list, execlist_link) {
  767. list_del(&req->execlist_link);
  768. i915_gem_request_unreference(req);
  769. }
  770. }
  771. void intel_logical_ring_stop(struct intel_engine_cs *engine)
  772. {
  773. struct drm_i915_private *dev_priv = engine->i915;
  774. int ret;
  775. if (!intel_engine_initialized(engine))
  776. return;
  777. ret = intel_engine_idle(engine);
  778. if (ret)
  779. DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
  780. engine->name, ret);
  781. /* TODO: Is this correct with Execlists enabled? */
  782. I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
  783. if (intel_wait_for_register(dev_priv,
  784. RING_MI_MODE(engine->mmio_base),
  785. MODE_IDLE, MODE_IDLE,
  786. 1000)) {
  787. DRM_ERROR("%s :timed out trying to stop ring\n", engine->name);
  788. return;
  789. }
  790. I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
  791. }
  792. int logical_ring_flush_all_caches(struct drm_i915_gem_request *req)
  793. {
  794. struct intel_engine_cs *engine = req->engine;
  795. int ret;
  796. if (!engine->gpu_caches_dirty)
  797. return 0;
  798. ret = engine->emit_flush(req, 0, I915_GEM_GPU_DOMAINS);
  799. if (ret)
  800. return ret;
  801. engine->gpu_caches_dirty = false;
  802. return 0;
  803. }
  804. static int intel_lr_context_pin(struct i915_gem_context *ctx,
  805. struct intel_engine_cs *engine)
  806. {
  807. struct drm_i915_private *dev_priv = ctx->i915;
  808. struct intel_context *ce = &ctx->engine[engine->id];
  809. void *vaddr;
  810. u32 *lrc_reg_state;
  811. int ret;
  812. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  813. if (ce->pin_count++)
  814. return 0;
  815. ret = i915_gem_obj_ggtt_pin(ce->state, GEN8_LR_CONTEXT_ALIGN,
  816. PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
  817. if (ret)
  818. goto err;
  819. vaddr = i915_gem_object_pin_map(ce->state);
  820. if (IS_ERR(vaddr)) {
  821. ret = PTR_ERR(vaddr);
  822. goto unpin_ctx_obj;
  823. }
  824. lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  825. ret = intel_pin_and_map_ringbuffer_obj(dev_priv, ce->ringbuf);
  826. if (ret)
  827. goto unpin_map;
  828. i915_gem_context_reference(ctx);
  829. ce->lrc_vma = i915_gem_obj_to_ggtt(ce->state);
  830. intel_lr_context_descriptor_update(ctx, engine);
  831. lrc_reg_state[CTX_RING_BUFFER_START+1] = ce->ringbuf->vma->node.start;
  832. ce->lrc_reg_state = lrc_reg_state;
  833. ce->state->dirty = true;
  834. /* Invalidate GuC TLB. */
  835. if (i915.enable_guc_submission)
  836. I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
  837. return 0;
  838. unpin_map:
  839. i915_gem_object_unpin_map(ce->state);
  840. unpin_ctx_obj:
  841. i915_gem_object_ggtt_unpin(ce->state);
  842. err:
  843. ce->pin_count = 0;
  844. return ret;
  845. }
  846. void intel_lr_context_unpin(struct i915_gem_context *ctx,
  847. struct intel_engine_cs *engine)
  848. {
  849. struct intel_context *ce = &ctx->engine[engine->id];
  850. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  851. GEM_BUG_ON(ce->pin_count == 0);
  852. if (--ce->pin_count)
  853. return;
  854. intel_unpin_ringbuffer_obj(ce->ringbuf);
  855. i915_gem_object_unpin_map(ce->state);
  856. i915_gem_object_ggtt_unpin(ce->state);
  857. ce->lrc_vma = NULL;
  858. ce->lrc_desc = 0;
  859. ce->lrc_reg_state = NULL;
  860. i915_gem_context_unreference(ctx);
  861. }
  862. static int intel_logical_ring_workarounds_emit(struct drm_i915_gem_request *req)
  863. {
  864. int ret, i;
  865. struct intel_engine_cs *engine = req->engine;
  866. struct intel_ringbuffer *ringbuf = req->ringbuf;
  867. struct i915_workarounds *w = &req->i915->workarounds;
  868. if (w->count == 0)
  869. return 0;
  870. engine->gpu_caches_dirty = true;
  871. ret = logical_ring_flush_all_caches(req);
  872. if (ret)
  873. return ret;
  874. ret = intel_ring_begin(req, w->count * 2 + 2);
  875. if (ret)
  876. return ret;
  877. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
  878. for (i = 0; i < w->count; i++) {
  879. intel_logical_ring_emit_reg(ringbuf, w->reg[i].addr);
  880. intel_logical_ring_emit(ringbuf, w->reg[i].value);
  881. }
  882. intel_logical_ring_emit(ringbuf, MI_NOOP);
  883. intel_logical_ring_advance(ringbuf);
  884. engine->gpu_caches_dirty = true;
  885. ret = logical_ring_flush_all_caches(req);
  886. if (ret)
  887. return ret;
  888. return 0;
  889. }
  890. #define wa_ctx_emit(batch, index, cmd) \
  891. do { \
  892. int __index = (index)++; \
  893. if (WARN_ON(__index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
  894. return -ENOSPC; \
  895. } \
  896. batch[__index] = (cmd); \
  897. } while (0)
  898. #define wa_ctx_emit_reg(batch, index, reg) \
  899. wa_ctx_emit((batch), (index), i915_mmio_reg_offset(reg))
  900. /*
  901. * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
  902. * PIPE_CONTROL instruction. This is required for the flush to happen correctly
  903. * but there is a slight complication as this is applied in WA batch where the
  904. * values are only initialized once so we cannot take register value at the
  905. * beginning and reuse it further; hence we save its value to memory, upload a
  906. * constant value with bit21 set and then we restore it back with the saved value.
  907. * To simplify the WA, a constant value is formed by using the default value
  908. * of this register. This shouldn't be a problem because we are only modifying
  909. * it for a short period and this batch in non-premptible. We can ofcourse
  910. * use additional instructions that read the actual value of the register
  911. * at that time and set our bit of interest but it makes the WA complicated.
  912. *
  913. * This WA is also required for Gen9 so extracting as a function avoids
  914. * code duplication.
  915. */
  916. static inline int gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine,
  917. uint32_t *const batch,
  918. uint32_t index)
  919. {
  920. struct drm_i915_private *dev_priv = engine->i915;
  921. uint32_t l3sqc4_flush = (0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES);
  922. /*
  923. * WaDisableLSQCROPERFforOCL:skl,kbl
  924. * This WA is implemented in skl_init_clock_gating() but since
  925. * this batch updates GEN8_L3SQCREG4 with default value we need to
  926. * set this bit here to retain the WA during flush.
  927. */
  928. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0) ||
  929. IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
  930. l3sqc4_flush |= GEN8_LQSC_RO_PERF_DIS;
  931. wa_ctx_emit(batch, index, (MI_STORE_REGISTER_MEM_GEN8 |
  932. MI_SRM_LRM_GLOBAL_GTT));
  933. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  934. wa_ctx_emit(batch, index, engine->scratch.gtt_offset + 256);
  935. wa_ctx_emit(batch, index, 0);
  936. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  937. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  938. wa_ctx_emit(batch, index, l3sqc4_flush);
  939. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  940. wa_ctx_emit(batch, index, (PIPE_CONTROL_CS_STALL |
  941. PIPE_CONTROL_DC_FLUSH_ENABLE));
  942. wa_ctx_emit(batch, index, 0);
  943. wa_ctx_emit(batch, index, 0);
  944. wa_ctx_emit(batch, index, 0);
  945. wa_ctx_emit(batch, index, 0);
  946. wa_ctx_emit(batch, index, (MI_LOAD_REGISTER_MEM_GEN8 |
  947. MI_SRM_LRM_GLOBAL_GTT));
  948. wa_ctx_emit_reg(batch, index, GEN8_L3SQCREG4);
  949. wa_ctx_emit(batch, index, engine->scratch.gtt_offset + 256);
  950. wa_ctx_emit(batch, index, 0);
  951. return index;
  952. }
  953. static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
  954. uint32_t offset,
  955. uint32_t start_alignment)
  956. {
  957. return wa_ctx->offset = ALIGN(offset, start_alignment);
  958. }
  959. static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
  960. uint32_t offset,
  961. uint32_t size_alignment)
  962. {
  963. wa_ctx->size = offset - wa_ctx->offset;
  964. WARN(wa_ctx->size % size_alignment,
  965. "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
  966. wa_ctx->size, size_alignment);
  967. return 0;
  968. }
  969. /**
  970. * gen8_init_indirectctx_bb() - initialize indirect ctx batch with WA
  971. *
  972. * @engine: only applicable for RCS
  973. * @wa_ctx: structure representing wa_ctx
  974. * offset: specifies start of the batch, should be cache-aligned. This is updated
  975. * with the offset value received as input.
  976. * size: size of the batch in DWORDS but HW expects in terms of cachelines
  977. * @batch: page in which WA are loaded
  978. * @offset: This field specifies the start of the batch, it should be
  979. * cache-aligned otherwise it is adjusted accordingly.
  980. * Typically we only have one indirect_ctx and per_ctx batch buffer which are
  981. * initialized at the beginning and shared across all contexts but this field
  982. * helps us to have multiple batches at different offsets and select them based
  983. * on a criteria. At the moment this batch always start at the beginning of the page
  984. * and at this point we don't have multiple wa_ctx batch buffers.
  985. *
  986. * The number of WA applied are not known at the beginning; we use this field
  987. * to return the no of DWORDS written.
  988. *
  989. * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
  990. * so it adds NOOPs as padding to make it cacheline aligned.
  991. * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
  992. * makes a complete batch buffer.
  993. *
  994. * Return: non-zero if we exceed the PAGE_SIZE limit.
  995. */
  996. static int gen8_init_indirectctx_bb(struct intel_engine_cs *engine,
  997. struct i915_wa_ctx_bb *wa_ctx,
  998. uint32_t *const batch,
  999. uint32_t *offset)
  1000. {
  1001. uint32_t scratch_addr;
  1002. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1003. /* WaDisableCtxRestoreArbitration:bdw,chv */
  1004. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  1005. /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
  1006. if (IS_BROADWELL(engine->i915)) {
  1007. int rc = gen8_emit_flush_coherentl3_wa(engine, batch, index);
  1008. if (rc < 0)
  1009. return rc;
  1010. index = rc;
  1011. }
  1012. /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
  1013. /* Actual scratch location is at 128 bytes offset */
  1014. scratch_addr = engine->scratch.gtt_offset + 2*CACHELINE_BYTES;
  1015. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  1016. wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
  1017. PIPE_CONTROL_GLOBAL_GTT_IVB |
  1018. PIPE_CONTROL_CS_STALL |
  1019. PIPE_CONTROL_QW_WRITE));
  1020. wa_ctx_emit(batch, index, scratch_addr);
  1021. wa_ctx_emit(batch, index, 0);
  1022. wa_ctx_emit(batch, index, 0);
  1023. wa_ctx_emit(batch, index, 0);
  1024. /* Pad to end of cacheline */
  1025. while (index % CACHELINE_DWORDS)
  1026. wa_ctx_emit(batch, index, MI_NOOP);
  1027. /*
  1028. * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
  1029. * execution depends on the length specified in terms of cache lines
  1030. * in the register CTX_RCS_INDIRECT_CTX
  1031. */
  1032. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  1033. }
  1034. /**
  1035. * gen8_init_perctx_bb() - initialize per ctx batch with WA
  1036. *
  1037. * @engine: only applicable for RCS
  1038. * @wa_ctx: structure representing wa_ctx
  1039. * offset: specifies start of the batch, should be cache-aligned.
  1040. * size: size of the batch in DWORDS but HW expects in terms of cachelines
  1041. * @batch: page in which WA are loaded
  1042. * @offset: This field specifies the start of this batch.
  1043. * This batch is started immediately after indirect_ctx batch. Since we ensure
  1044. * that indirect_ctx ends on a cacheline this batch is aligned automatically.
  1045. *
  1046. * The number of DWORDS written are returned using this field.
  1047. *
  1048. * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
  1049. * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
  1050. */
  1051. static int gen8_init_perctx_bb(struct intel_engine_cs *engine,
  1052. struct i915_wa_ctx_bb *wa_ctx,
  1053. uint32_t *const batch,
  1054. uint32_t *offset)
  1055. {
  1056. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1057. /* WaDisableCtxRestoreArbitration:bdw,chv */
  1058. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  1059. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  1060. return wa_ctx_end(wa_ctx, *offset = index, 1);
  1061. }
  1062. static int gen9_init_indirectctx_bb(struct intel_engine_cs *engine,
  1063. struct i915_wa_ctx_bb *wa_ctx,
  1064. uint32_t *const batch,
  1065. uint32_t *offset)
  1066. {
  1067. int ret;
  1068. struct drm_i915_private *dev_priv = engine->i915;
  1069. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1070. /* WaDisableCtxRestoreArbitration:skl,bxt */
  1071. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_D0) ||
  1072. IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
  1073. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  1074. /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt */
  1075. ret = gen8_emit_flush_coherentl3_wa(engine, batch, index);
  1076. if (ret < 0)
  1077. return ret;
  1078. index = ret;
  1079. /* WaClearSlmSpaceAtContextSwitch:kbl */
  1080. /* Actual scratch location is at 128 bytes offset */
  1081. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_A0)) {
  1082. uint32_t scratch_addr
  1083. = engine->scratch.gtt_offset + 2*CACHELINE_BYTES;
  1084. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  1085. wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
  1086. PIPE_CONTROL_GLOBAL_GTT_IVB |
  1087. PIPE_CONTROL_CS_STALL |
  1088. PIPE_CONTROL_QW_WRITE));
  1089. wa_ctx_emit(batch, index, scratch_addr);
  1090. wa_ctx_emit(batch, index, 0);
  1091. wa_ctx_emit(batch, index, 0);
  1092. wa_ctx_emit(batch, index, 0);
  1093. }
  1094. /* WaMediaPoolStateCmdInWABB:bxt */
  1095. if (HAS_POOLED_EU(engine->i915)) {
  1096. /*
  1097. * EU pool configuration is setup along with golden context
  1098. * during context initialization. This value depends on
  1099. * device type (2x6 or 3x6) and needs to be updated based
  1100. * on which subslice is disabled especially for 2x6
  1101. * devices, however it is safe to load default
  1102. * configuration of 3x6 device instead of masking off
  1103. * corresponding bits because HW ignores bits of a disabled
  1104. * subslice and drops down to appropriate config. Please
  1105. * see render_state_setup() in i915_gem_render_state.c for
  1106. * possible configurations, to avoid duplication they are
  1107. * not shown here again.
  1108. */
  1109. u32 eu_pool_config = 0x00777000;
  1110. wa_ctx_emit(batch, index, GEN9_MEDIA_POOL_STATE);
  1111. wa_ctx_emit(batch, index, GEN9_MEDIA_POOL_ENABLE);
  1112. wa_ctx_emit(batch, index, eu_pool_config);
  1113. wa_ctx_emit(batch, index, 0);
  1114. wa_ctx_emit(batch, index, 0);
  1115. wa_ctx_emit(batch, index, 0);
  1116. }
  1117. /* Pad to end of cacheline */
  1118. while (index % CACHELINE_DWORDS)
  1119. wa_ctx_emit(batch, index, MI_NOOP);
  1120. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  1121. }
  1122. static int gen9_init_perctx_bb(struct intel_engine_cs *engine,
  1123. struct i915_wa_ctx_bb *wa_ctx,
  1124. uint32_t *const batch,
  1125. uint32_t *offset)
  1126. {
  1127. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1128. /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
  1129. if (IS_SKL_REVID(engine->i915, 0, SKL_REVID_B0) ||
  1130. IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1)) {
  1131. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  1132. wa_ctx_emit_reg(batch, index, GEN9_SLICE_COMMON_ECO_CHICKEN0);
  1133. wa_ctx_emit(batch, index,
  1134. _MASKED_BIT_ENABLE(DISABLE_PIXEL_MASK_CAMMING));
  1135. wa_ctx_emit(batch, index, MI_NOOP);
  1136. }
  1137. /* WaClearTdlStateAckDirtyBits:bxt */
  1138. if (IS_BXT_REVID(engine->i915, 0, BXT_REVID_B0)) {
  1139. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(4));
  1140. wa_ctx_emit_reg(batch, index, GEN8_STATE_ACK);
  1141. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  1142. wa_ctx_emit_reg(batch, index, GEN9_STATE_ACK_SLICE1);
  1143. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  1144. wa_ctx_emit_reg(batch, index, GEN9_STATE_ACK_SLICE2);
  1145. wa_ctx_emit(batch, index, _MASKED_BIT_DISABLE(GEN9_SUBSLICE_TDL_ACK_BITS));
  1146. wa_ctx_emit_reg(batch, index, GEN7_ROW_CHICKEN2);
  1147. /* dummy write to CS, mask bits are 0 to ensure the register is not modified */
  1148. wa_ctx_emit(batch, index, 0x0);
  1149. wa_ctx_emit(batch, index, MI_NOOP);
  1150. }
  1151. /* WaDisableCtxRestoreArbitration:skl,bxt */
  1152. if (IS_SKL_REVID(engine->i915, 0, SKL_REVID_D0) ||
  1153. IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1))
  1154. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  1155. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  1156. return wa_ctx_end(wa_ctx, *offset = index, 1);
  1157. }
  1158. static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *engine, u32 size)
  1159. {
  1160. int ret;
  1161. engine->wa_ctx.obj = i915_gem_object_create(&engine->i915->drm,
  1162. PAGE_ALIGN(size));
  1163. if (IS_ERR(engine->wa_ctx.obj)) {
  1164. DRM_DEBUG_DRIVER("alloc LRC WA ctx backing obj failed.\n");
  1165. ret = PTR_ERR(engine->wa_ctx.obj);
  1166. engine->wa_ctx.obj = NULL;
  1167. return ret;
  1168. }
  1169. ret = i915_gem_obj_ggtt_pin(engine->wa_ctx.obj, PAGE_SIZE, 0);
  1170. if (ret) {
  1171. DRM_DEBUG_DRIVER("pin LRC WA ctx backing obj failed: %d\n",
  1172. ret);
  1173. drm_gem_object_unreference(&engine->wa_ctx.obj->base);
  1174. return ret;
  1175. }
  1176. return 0;
  1177. }
  1178. static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *engine)
  1179. {
  1180. if (engine->wa_ctx.obj) {
  1181. i915_gem_object_ggtt_unpin(engine->wa_ctx.obj);
  1182. drm_gem_object_unreference(&engine->wa_ctx.obj->base);
  1183. engine->wa_ctx.obj = NULL;
  1184. }
  1185. }
  1186. static int intel_init_workaround_bb(struct intel_engine_cs *engine)
  1187. {
  1188. int ret;
  1189. uint32_t *batch;
  1190. uint32_t offset;
  1191. struct page *page;
  1192. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1193. WARN_ON(engine->id != RCS);
  1194. /* update this when WA for higher Gen are added */
  1195. if (INTEL_GEN(engine->i915) > 9) {
  1196. DRM_ERROR("WA batch buffer is not initialized for Gen%d\n",
  1197. INTEL_GEN(engine->i915));
  1198. return 0;
  1199. }
  1200. /* some WA perform writes to scratch page, ensure it is valid */
  1201. if (engine->scratch.obj == NULL) {
  1202. DRM_ERROR("scratch page not allocated for %s\n", engine->name);
  1203. return -EINVAL;
  1204. }
  1205. ret = lrc_setup_wa_ctx_obj(engine, PAGE_SIZE);
  1206. if (ret) {
  1207. DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
  1208. return ret;
  1209. }
  1210. page = i915_gem_object_get_dirty_page(wa_ctx->obj, 0);
  1211. batch = kmap_atomic(page);
  1212. offset = 0;
  1213. if (IS_GEN8(engine->i915)) {
  1214. ret = gen8_init_indirectctx_bb(engine,
  1215. &wa_ctx->indirect_ctx,
  1216. batch,
  1217. &offset);
  1218. if (ret)
  1219. goto out;
  1220. ret = gen8_init_perctx_bb(engine,
  1221. &wa_ctx->per_ctx,
  1222. batch,
  1223. &offset);
  1224. if (ret)
  1225. goto out;
  1226. } else if (IS_GEN9(engine->i915)) {
  1227. ret = gen9_init_indirectctx_bb(engine,
  1228. &wa_ctx->indirect_ctx,
  1229. batch,
  1230. &offset);
  1231. if (ret)
  1232. goto out;
  1233. ret = gen9_init_perctx_bb(engine,
  1234. &wa_ctx->per_ctx,
  1235. batch,
  1236. &offset);
  1237. if (ret)
  1238. goto out;
  1239. }
  1240. out:
  1241. kunmap_atomic(batch);
  1242. if (ret)
  1243. lrc_destroy_wa_ctx_obj(engine);
  1244. return ret;
  1245. }
  1246. static void lrc_init_hws(struct intel_engine_cs *engine)
  1247. {
  1248. struct drm_i915_private *dev_priv = engine->i915;
  1249. I915_WRITE(RING_HWS_PGA(engine->mmio_base),
  1250. (u32)engine->status_page.gfx_addr);
  1251. POSTING_READ(RING_HWS_PGA(engine->mmio_base));
  1252. }
  1253. static int gen8_init_common_ring(struct intel_engine_cs *engine)
  1254. {
  1255. struct drm_i915_private *dev_priv = engine->i915;
  1256. unsigned int next_context_status_buffer_hw;
  1257. lrc_init_hws(engine);
  1258. I915_WRITE_IMR(engine,
  1259. ~(engine->irq_enable_mask | engine->irq_keep_mask));
  1260. I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
  1261. I915_WRITE(RING_MODE_GEN7(engine),
  1262. _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
  1263. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  1264. POSTING_READ(RING_MODE_GEN7(engine));
  1265. /*
  1266. * Instead of resetting the Context Status Buffer (CSB) read pointer to
  1267. * zero, we need to read the write pointer from hardware and use its
  1268. * value because "this register is power context save restored".
  1269. * Effectively, these states have been observed:
  1270. *
  1271. * | Suspend-to-idle (freeze) | Suspend-to-RAM (mem) |
  1272. * BDW | CSB regs not reset | CSB regs reset |
  1273. * CHT | CSB regs not reset | CSB regs not reset |
  1274. * SKL | ? | ? |
  1275. * BXT | ? | ? |
  1276. */
  1277. next_context_status_buffer_hw =
  1278. GEN8_CSB_WRITE_PTR(I915_READ(RING_CONTEXT_STATUS_PTR(engine)));
  1279. /*
  1280. * When the CSB registers are reset (also after power-up / gpu reset),
  1281. * CSB write pointer is set to all 1's, which is not valid, use '5' in
  1282. * this special case, so the first element read is CSB[0].
  1283. */
  1284. if (next_context_status_buffer_hw == GEN8_CSB_PTR_MASK)
  1285. next_context_status_buffer_hw = (GEN8_CSB_ENTRIES - 1);
  1286. engine->next_context_status_buffer = next_context_status_buffer_hw;
  1287. DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
  1288. intel_engine_init_hangcheck(engine);
  1289. return intel_mocs_init_engine(engine);
  1290. }
  1291. static int gen8_init_render_ring(struct intel_engine_cs *engine)
  1292. {
  1293. struct drm_i915_private *dev_priv = engine->i915;
  1294. int ret;
  1295. ret = gen8_init_common_ring(engine);
  1296. if (ret)
  1297. return ret;
  1298. /* We need to disable the AsyncFlip performance optimisations in order
  1299. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  1300. * programmed to '1' on all products.
  1301. *
  1302. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  1303. */
  1304. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  1305. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  1306. return init_workarounds_ring(engine);
  1307. }
  1308. static int gen9_init_render_ring(struct intel_engine_cs *engine)
  1309. {
  1310. int ret;
  1311. ret = gen8_init_common_ring(engine);
  1312. if (ret)
  1313. return ret;
  1314. return init_workarounds_ring(engine);
  1315. }
  1316. static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
  1317. {
  1318. struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
  1319. struct intel_engine_cs *engine = req->engine;
  1320. struct intel_ringbuffer *ringbuf = req->ringbuf;
  1321. const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
  1322. int i, ret;
  1323. ret = intel_ring_begin(req, num_lri_cmds * 2 + 2);
  1324. if (ret)
  1325. return ret;
  1326. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(num_lri_cmds));
  1327. for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
  1328. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  1329. intel_logical_ring_emit_reg(ringbuf,
  1330. GEN8_RING_PDP_UDW(engine, i));
  1331. intel_logical_ring_emit(ringbuf, upper_32_bits(pd_daddr));
  1332. intel_logical_ring_emit_reg(ringbuf,
  1333. GEN8_RING_PDP_LDW(engine, i));
  1334. intel_logical_ring_emit(ringbuf, lower_32_bits(pd_daddr));
  1335. }
  1336. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1337. intel_logical_ring_advance(ringbuf);
  1338. return 0;
  1339. }
  1340. static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
  1341. u64 offset, unsigned dispatch_flags)
  1342. {
  1343. struct intel_ringbuffer *ringbuf = req->ringbuf;
  1344. bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
  1345. int ret;
  1346. /* Don't rely in hw updating PDPs, specially in lite-restore.
  1347. * Ideally, we should set Force PD Restore in ctx descriptor,
  1348. * but we can't. Force Restore would be a second option, but
  1349. * it is unsafe in case of lite-restore (because the ctx is
  1350. * not idle). PML4 is allocated during ppgtt init so this is
  1351. * not needed in 48-bit.*/
  1352. if (req->ctx->ppgtt &&
  1353. (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings)) {
  1354. if (!USES_FULL_48BIT_PPGTT(req->i915) &&
  1355. !intel_vgpu_active(req->i915)) {
  1356. ret = intel_logical_ring_emit_pdps(req);
  1357. if (ret)
  1358. return ret;
  1359. }
  1360. req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
  1361. }
  1362. ret = intel_ring_begin(req, 4);
  1363. if (ret)
  1364. return ret;
  1365. /* FIXME(BDW): Address space and security selectors. */
  1366. intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 |
  1367. (ppgtt<<8) |
  1368. (dispatch_flags & I915_DISPATCH_RS ?
  1369. MI_BATCH_RESOURCE_STREAMER : 0));
  1370. intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
  1371. intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
  1372. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1373. intel_logical_ring_advance(ringbuf);
  1374. return 0;
  1375. }
  1376. static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
  1377. {
  1378. struct drm_i915_private *dev_priv = engine->i915;
  1379. I915_WRITE_IMR(engine,
  1380. ~(engine->irq_enable_mask | engine->irq_keep_mask));
  1381. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1382. }
  1383. static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
  1384. {
  1385. struct drm_i915_private *dev_priv = engine->i915;
  1386. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  1387. }
  1388. static int gen8_emit_flush(struct drm_i915_gem_request *request,
  1389. u32 invalidate_domains,
  1390. u32 unused)
  1391. {
  1392. struct intel_ringbuffer *ringbuf = request->ringbuf;
  1393. struct intel_engine_cs *engine = ringbuf->engine;
  1394. struct drm_i915_private *dev_priv = request->i915;
  1395. uint32_t cmd;
  1396. int ret;
  1397. ret = intel_ring_begin(request, 4);
  1398. if (ret)
  1399. return ret;
  1400. cmd = MI_FLUSH_DW + 1;
  1401. /* We always require a command barrier so that subsequent
  1402. * commands, such as breadcrumb interrupts, are strictly ordered
  1403. * wrt the contents of the write cache being flushed to memory
  1404. * (and thus being coherent from the CPU).
  1405. */
  1406. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1407. if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
  1408. cmd |= MI_INVALIDATE_TLB;
  1409. if (engine == &dev_priv->engine[VCS])
  1410. cmd |= MI_INVALIDATE_BSD;
  1411. }
  1412. intel_logical_ring_emit(ringbuf, cmd);
  1413. intel_logical_ring_emit(ringbuf,
  1414. I915_GEM_HWS_SCRATCH_ADDR |
  1415. MI_FLUSH_DW_USE_GTT);
  1416. intel_logical_ring_emit(ringbuf, 0); /* upper addr */
  1417. intel_logical_ring_emit(ringbuf, 0); /* value */
  1418. intel_logical_ring_advance(ringbuf);
  1419. return 0;
  1420. }
  1421. static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
  1422. u32 invalidate_domains,
  1423. u32 flush_domains)
  1424. {
  1425. struct intel_ringbuffer *ringbuf = request->ringbuf;
  1426. struct intel_engine_cs *engine = ringbuf->engine;
  1427. u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  1428. bool vf_flush_wa = false, dc_flush_wa = false;
  1429. u32 flags = 0;
  1430. int ret;
  1431. int len;
  1432. flags |= PIPE_CONTROL_CS_STALL;
  1433. if (flush_domains) {
  1434. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  1435. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  1436. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  1437. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  1438. }
  1439. if (invalidate_domains) {
  1440. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  1441. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  1442. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  1443. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1444. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  1445. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  1446. flags |= PIPE_CONTROL_QW_WRITE;
  1447. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1448. /*
  1449. * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
  1450. * pipe control.
  1451. */
  1452. if (IS_GEN9(request->i915))
  1453. vf_flush_wa = true;
  1454. /* WaForGAMHang:kbl */
  1455. if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
  1456. dc_flush_wa = true;
  1457. }
  1458. len = 6;
  1459. if (vf_flush_wa)
  1460. len += 6;
  1461. if (dc_flush_wa)
  1462. len += 12;
  1463. ret = intel_ring_begin(request, len);
  1464. if (ret)
  1465. return ret;
  1466. if (vf_flush_wa) {
  1467. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  1468. intel_logical_ring_emit(ringbuf, 0);
  1469. intel_logical_ring_emit(ringbuf, 0);
  1470. intel_logical_ring_emit(ringbuf, 0);
  1471. intel_logical_ring_emit(ringbuf, 0);
  1472. intel_logical_ring_emit(ringbuf, 0);
  1473. }
  1474. if (dc_flush_wa) {
  1475. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  1476. intel_logical_ring_emit(ringbuf, PIPE_CONTROL_DC_FLUSH_ENABLE);
  1477. intel_logical_ring_emit(ringbuf, 0);
  1478. intel_logical_ring_emit(ringbuf, 0);
  1479. intel_logical_ring_emit(ringbuf, 0);
  1480. intel_logical_ring_emit(ringbuf, 0);
  1481. }
  1482. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  1483. intel_logical_ring_emit(ringbuf, flags);
  1484. intel_logical_ring_emit(ringbuf, scratch_addr);
  1485. intel_logical_ring_emit(ringbuf, 0);
  1486. intel_logical_ring_emit(ringbuf, 0);
  1487. intel_logical_ring_emit(ringbuf, 0);
  1488. if (dc_flush_wa) {
  1489. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  1490. intel_logical_ring_emit(ringbuf, PIPE_CONTROL_CS_STALL);
  1491. intel_logical_ring_emit(ringbuf, 0);
  1492. intel_logical_ring_emit(ringbuf, 0);
  1493. intel_logical_ring_emit(ringbuf, 0);
  1494. intel_logical_ring_emit(ringbuf, 0);
  1495. }
  1496. intel_logical_ring_advance(ringbuf);
  1497. return 0;
  1498. }
  1499. static void bxt_a_seqno_barrier(struct intel_engine_cs *engine)
  1500. {
  1501. /*
  1502. * On BXT A steppings there is a HW coherency issue whereby the
  1503. * MI_STORE_DATA_IMM storing the completed request's seqno
  1504. * occasionally doesn't invalidate the CPU cache. Work around this by
  1505. * clflushing the corresponding cacheline whenever the caller wants
  1506. * the coherency to be guaranteed. Note that this cacheline is known
  1507. * to be clean at this point, since we only write it in
  1508. * bxt_a_set_seqno(), where we also do a clflush after the write. So
  1509. * this clflush in practice becomes an invalidate operation.
  1510. */
  1511. intel_flush_status_page(engine, I915_GEM_HWS_INDEX);
  1512. }
  1513. /*
  1514. * Reserve space for 2 NOOPs at the end of each request to be
  1515. * used as a workaround for not being allowed to do lite
  1516. * restore with HEAD==TAIL (WaIdleLiteRestore).
  1517. */
  1518. #define WA_TAIL_DWORDS 2
  1519. static int gen8_emit_request(struct drm_i915_gem_request *request)
  1520. {
  1521. struct intel_ringbuffer *ringbuf = request->ringbuf;
  1522. int ret;
  1523. ret = intel_ring_begin(request, 6 + WA_TAIL_DWORDS);
  1524. if (ret)
  1525. return ret;
  1526. /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
  1527. BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
  1528. intel_logical_ring_emit(ringbuf,
  1529. (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW);
  1530. intel_logical_ring_emit(ringbuf,
  1531. intel_hws_seqno_address(request->engine) |
  1532. MI_FLUSH_DW_USE_GTT);
  1533. intel_logical_ring_emit(ringbuf, 0);
  1534. intel_logical_ring_emit(ringbuf, request->seqno);
  1535. intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
  1536. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1537. return intel_logical_ring_advance_and_submit(request);
  1538. }
  1539. static int gen8_emit_request_render(struct drm_i915_gem_request *request)
  1540. {
  1541. struct intel_ringbuffer *ringbuf = request->ringbuf;
  1542. int ret;
  1543. ret = intel_ring_begin(request, 8 + WA_TAIL_DWORDS);
  1544. if (ret)
  1545. return ret;
  1546. /* We're using qword write, seqno should be aligned to 8 bytes. */
  1547. BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
  1548. /* w/a for post sync ops following a GPGPU operation we
  1549. * need a prior CS_STALL, which is emitted by the flush
  1550. * following the batch.
  1551. */
  1552. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  1553. intel_logical_ring_emit(ringbuf,
  1554. (PIPE_CONTROL_GLOBAL_GTT_IVB |
  1555. PIPE_CONTROL_CS_STALL |
  1556. PIPE_CONTROL_QW_WRITE));
  1557. intel_logical_ring_emit(ringbuf,
  1558. intel_hws_seqno_address(request->engine));
  1559. intel_logical_ring_emit(ringbuf, 0);
  1560. intel_logical_ring_emit(ringbuf, i915_gem_request_get_seqno(request));
  1561. /* We're thrashing one dword of HWS. */
  1562. intel_logical_ring_emit(ringbuf, 0);
  1563. intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
  1564. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1565. return intel_logical_ring_advance_and_submit(request);
  1566. }
  1567. static int intel_lr_context_render_state_init(struct drm_i915_gem_request *req)
  1568. {
  1569. struct render_state so;
  1570. int ret;
  1571. ret = i915_gem_render_state_prepare(req->engine, &so);
  1572. if (ret)
  1573. return ret;
  1574. if (so.rodata == NULL)
  1575. return 0;
  1576. ret = req->engine->emit_bb_start(req, so.ggtt_offset,
  1577. I915_DISPATCH_SECURE);
  1578. if (ret)
  1579. goto out;
  1580. ret = req->engine->emit_bb_start(req,
  1581. (so.ggtt_offset + so.aux_batch_offset),
  1582. I915_DISPATCH_SECURE);
  1583. if (ret)
  1584. goto out;
  1585. i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), req);
  1586. out:
  1587. i915_gem_render_state_fini(&so);
  1588. return ret;
  1589. }
  1590. static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
  1591. {
  1592. int ret;
  1593. ret = intel_logical_ring_workarounds_emit(req);
  1594. if (ret)
  1595. return ret;
  1596. ret = intel_rcs_context_init_mocs(req);
  1597. /*
  1598. * Failing to program the MOCS is non-fatal.The system will not
  1599. * run at peak performance. So generate an error and carry on.
  1600. */
  1601. if (ret)
  1602. DRM_ERROR("MOCS failed to program: expect performance issues.\n");
  1603. return intel_lr_context_render_state_init(req);
  1604. }
  1605. /**
  1606. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1607. *
  1608. * @engine: Engine Command Streamer.
  1609. *
  1610. */
  1611. void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
  1612. {
  1613. struct drm_i915_private *dev_priv;
  1614. if (!intel_engine_initialized(engine))
  1615. return;
  1616. /*
  1617. * Tasklet cannot be active at this point due intel_mark_active/idle
  1618. * so this is just for documentation.
  1619. */
  1620. if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->irq_tasklet.state)))
  1621. tasklet_kill(&engine->irq_tasklet);
  1622. dev_priv = engine->i915;
  1623. if (engine->buffer) {
  1624. intel_logical_ring_stop(engine);
  1625. WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1626. }
  1627. if (engine->cleanup)
  1628. engine->cleanup(engine);
  1629. i915_cmd_parser_fini_ring(engine);
  1630. i915_gem_batch_pool_fini(&engine->batch_pool);
  1631. intel_engine_fini_breadcrumbs(engine);
  1632. if (engine->status_page.obj) {
  1633. i915_gem_object_unpin_map(engine->status_page.obj);
  1634. engine->status_page.obj = NULL;
  1635. }
  1636. intel_lr_context_unpin(dev_priv->kernel_context, engine);
  1637. engine->idle_lite_restore_wa = 0;
  1638. engine->disable_lite_restore_wa = false;
  1639. engine->ctx_desc_template = 0;
  1640. lrc_destroy_wa_ctx_obj(engine);
  1641. engine->i915 = NULL;
  1642. }
  1643. static void
  1644. logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  1645. {
  1646. /* Default vfuncs which can be overriden by each engine. */
  1647. engine->init_hw = gen8_init_common_ring;
  1648. engine->emit_request = gen8_emit_request;
  1649. engine->emit_flush = gen8_emit_flush;
  1650. engine->irq_enable = gen8_logical_ring_enable_irq;
  1651. engine->irq_disable = gen8_logical_ring_disable_irq;
  1652. engine->emit_bb_start = gen8_emit_bb_start;
  1653. if (IS_BXT_REVID(engine->i915, 0, BXT_REVID_A1))
  1654. engine->irq_seqno_barrier = bxt_a_seqno_barrier;
  1655. }
  1656. static inline void
  1657. logical_ring_default_irqs(struct intel_engine_cs *engine, unsigned shift)
  1658. {
  1659. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
  1660. engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
  1661. }
  1662. static int
  1663. lrc_setup_hws(struct intel_engine_cs *engine,
  1664. struct drm_i915_gem_object *dctx_obj)
  1665. {
  1666. void *hws;
  1667. /* The HWSP is part of the default context object in LRC mode. */
  1668. engine->status_page.gfx_addr = i915_gem_obj_ggtt_offset(dctx_obj) +
  1669. LRC_PPHWSP_PN * PAGE_SIZE;
  1670. hws = i915_gem_object_pin_map(dctx_obj);
  1671. if (IS_ERR(hws))
  1672. return PTR_ERR(hws);
  1673. engine->status_page.page_addr = hws + LRC_PPHWSP_PN * PAGE_SIZE;
  1674. engine->status_page.obj = dctx_obj;
  1675. return 0;
  1676. }
  1677. static int
  1678. logical_ring_init(struct intel_engine_cs *engine)
  1679. {
  1680. struct i915_gem_context *dctx = engine->i915->kernel_context;
  1681. int ret;
  1682. ret = intel_engine_init_breadcrumbs(engine);
  1683. if (ret)
  1684. goto error;
  1685. ret = i915_cmd_parser_init_ring(engine);
  1686. if (ret)
  1687. goto error;
  1688. ret = execlists_context_deferred_alloc(dctx, engine);
  1689. if (ret)
  1690. goto error;
  1691. /* As this is the default context, always pin it */
  1692. ret = intel_lr_context_pin(dctx, engine);
  1693. if (ret) {
  1694. DRM_ERROR("Failed to pin context for %s: %d\n",
  1695. engine->name, ret);
  1696. goto error;
  1697. }
  1698. /* And setup the hardware status page. */
  1699. ret = lrc_setup_hws(engine, dctx->engine[engine->id].state);
  1700. if (ret) {
  1701. DRM_ERROR("Failed to set up hws %s: %d\n", engine->name, ret);
  1702. goto error;
  1703. }
  1704. return 0;
  1705. error:
  1706. intel_logical_ring_cleanup(engine);
  1707. return ret;
  1708. }
  1709. static int logical_render_ring_init(struct intel_engine_cs *engine)
  1710. {
  1711. struct drm_i915_private *dev_priv = engine->i915;
  1712. int ret;
  1713. if (HAS_L3_DPF(dev_priv))
  1714. engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1715. /* Override some for render ring. */
  1716. if (INTEL_GEN(dev_priv) >= 9)
  1717. engine->init_hw = gen9_init_render_ring;
  1718. else
  1719. engine->init_hw = gen8_init_render_ring;
  1720. engine->init_context = gen8_init_rcs_context;
  1721. engine->cleanup = intel_fini_pipe_control;
  1722. engine->emit_flush = gen8_emit_flush_render;
  1723. engine->emit_request = gen8_emit_request_render;
  1724. ret = intel_init_pipe_control(engine, 4096);
  1725. if (ret)
  1726. return ret;
  1727. ret = intel_init_workaround_bb(engine);
  1728. if (ret) {
  1729. /*
  1730. * We continue even if we fail to initialize WA batch
  1731. * because we only expect rare glitches but nothing
  1732. * critical to prevent us from using GPU
  1733. */
  1734. DRM_ERROR("WA batch buffer initialization failed: %d\n",
  1735. ret);
  1736. }
  1737. ret = logical_ring_init(engine);
  1738. if (ret) {
  1739. lrc_destroy_wa_ctx_obj(engine);
  1740. }
  1741. return ret;
  1742. }
  1743. static const struct logical_ring_info {
  1744. const char *name;
  1745. unsigned exec_id;
  1746. unsigned guc_id;
  1747. u32 mmio_base;
  1748. unsigned irq_shift;
  1749. int (*init)(struct intel_engine_cs *engine);
  1750. } logical_rings[] = {
  1751. [RCS] = {
  1752. .name = "render ring",
  1753. .exec_id = I915_EXEC_RENDER,
  1754. .guc_id = GUC_RENDER_ENGINE,
  1755. .mmio_base = RENDER_RING_BASE,
  1756. .irq_shift = GEN8_RCS_IRQ_SHIFT,
  1757. .init = logical_render_ring_init,
  1758. },
  1759. [BCS] = {
  1760. .name = "blitter ring",
  1761. .exec_id = I915_EXEC_BLT,
  1762. .guc_id = GUC_BLITTER_ENGINE,
  1763. .mmio_base = BLT_RING_BASE,
  1764. .irq_shift = GEN8_BCS_IRQ_SHIFT,
  1765. .init = logical_ring_init,
  1766. },
  1767. [VCS] = {
  1768. .name = "bsd ring",
  1769. .exec_id = I915_EXEC_BSD,
  1770. .guc_id = GUC_VIDEO_ENGINE,
  1771. .mmio_base = GEN6_BSD_RING_BASE,
  1772. .irq_shift = GEN8_VCS1_IRQ_SHIFT,
  1773. .init = logical_ring_init,
  1774. },
  1775. [VCS2] = {
  1776. .name = "bsd2 ring",
  1777. .exec_id = I915_EXEC_BSD,
  1778. .guc_id = GUC_VIDEO_ENGINE2,
  1779. .mmio_base = GEN8_BSD2_RING_BASE,
  1780. .irq_shift = GEN8_VCS2_IRQ_SHIFT,
  1781. .init = logical_ring_init,
  1782. },
  1783. [VECS] = {
  1784. .name = "video enhancement ring",
  1785. .exec_id = I915_EXEC_VEBOX,
  1786. .guc_id = GUC_VIDEOENHANCE_ENGINE,
  1787. .mmio_base = VEBOX_RING_BASE,
  1788. .irq_shift = GEN8_VECS_IRQ_SHIFT,
  1789. .init = logical_ring_init,
  1790. },
  1791. };
  1792. static struct intel_engine_cs *
  1793. logical_ring_setup(struct drm_i915_private *dev_priv, enum intel_engine_id id)
  1794. {
  1795. const struct logical_ring_info *info = &logical_rings[id];
  1796. struct intel_engine_cs *engine = &dev_priv->engine[id];
  1797. enum forcewake_domains fw_domains;
  1798. engine->id = id;
  1799. engine->name = info->name;
  1800. engine->exec_id = info->exec_id;
  1801. engine->guc_id = info->guc_id;
  1802. engine->mmio_base = info->mmio_base;
  1803. engine->i915 = dev_priv;
  1804. /* Intentionally left blank. */
  1805. engine->buffer = NULL;
  1806. fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
  1807. RING_ELSP(engine),
  1808. FW_REG_WRITE);
  1809. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1810. RING_CONTEXT_STATUS_PTR(engine),
  1811. FW_REG_READ | FW_REG_WRITE);
  1812. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1813. RING_CONTEXT_STATUS_BUF_BASE(engine),
  1814. FW_REG_READ);
  1815. engine->fw_domains = fw_domains;
  1816. INIT_LIST_HEAD(&engine->active_list);
  1817. INIT_LIST_HEAD(&engine->request_list);
  1818. INIT_LIST_HEAD(&engine->buffers);
  1819. INIT_LIST_HEAD(&engine->execlist_queue);
  1820. spin_lock_init(&engine->execlist_lock);
  1821. tasklet_init(&engine->irq_tasklet,
  1822. intel_lrc_irq_handler, (unsigned long)engine);
  1823. logical_ring_init_platform_invariants(engine);
  1824. logical_ring_default_vfuncs(engine);
  1825. logical_ring_default_irqs(engine, info->irq_shift);
  1826. intel_engine_init_hangcheck(engine);
  1827. i915_gem_batch_pool_init(&dev_priv->drm, &engine->batch_pool);
  1828. return engine;
  1829. }
  1830. /**
  1831. * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
  1832. * @dev: DRM device.
  1833. *
  1834. * This function inits the engines for an Execlists submission style (the
  1835. * equivalent in the legacy ringbuffer submission world would be
  1836. * i915_gem_init_engines). It does it only for those engines that are present in
  1837. * the hardware.
  1838. *
  1839. * Return: non-zero if the initialization failed.
  1840. */
  1841. int intel_logical_rings_init(struct drm_device *dev)
  1842. {
  1843. struct drm_i915_private *dev_priv = to_i915(dev);
  1844. unsigned int mask = 0;
  1845. unsigned int i;
  1846. int ret;
  1847. WARN_ON(INTEL_INFO(dev_priv)->ring_mask &
  1848. GENMASK(sizeof(mask) * BITS_PER_BYTE - 1, I915_NUM_ENGINES));
  1849. for (i = 0; i < ARRAY_SIZE(logical_rings); i++) {
  1850. if (!HAS_ENGINE(dev_priv, i))
  1851. continue;
  1852. if (!logical_rings[i].init)
  1853. continue;
  1854. ret = logical_rings[i].init(logical_ring_setup(dev_priv, i));
  1855. if (ret)
  1856. goto cleanup;
  1857. mask |= ENGINE_MASK(i);
  1858. }
  1859. /*
  1860. * Catch failures to update logical_rings table when the new engines
  1861. * are added to the driver by a warning and disabling the forgotten
  1862. * engines.
  1863. */
  1864. if (WARN_ON(mask != INTEL_INFO(dev_priv)->ring_mask)) {
  1865. struct intel_device_info *info =
  1866. (struct intel_device_info *)&dev_priv->info;
  1867. info->ring_mask = mask;
  1868. }
  1869. return 0;
  1870. cleanup:
  1871. for (i = 0; i < I915_NUM_ENGINES; i++)
  1872. intel_logical_ring_cleanup(&dev_priv->engine[i]);
  1873. return ret;
  1874. }
  1875. static u32
  1876. make_rpcs(struct drm_i915_private *dev_priv)
  1877. {
  1878. u32 rpcs = 0;
  1879. /*
  1880. * No explicit RPCS request is needed to ensure full
  1881. * slice/subslice/EU enablement prior to Gen9.
  1882. */
  1883. if (INTEL_GEN(dev_priv) < 9)
  1884. return 0;
  1885. /*
  1886. * Starting in Gen9, render power gating can leave
  1887. * slice/subslice/EU in a partially enabled state. We
  1888. * must make an explicit request through RPCS for full
  1889. * enablement.
  1890. */
  1891. if (INTEL_INFO(dev_priv)->has_slice_pg) {
  1892. rpcs |= GEN8_RPCS_S_CNT_ENABLE;
  1893. rpcs |= INTEL_INFO(dev_priv)->slice_total <<
  1894. GEN8_RPCS_S_CNT_SHIFT;
  1895. rpcs |= GEN8_RPCS_ENABLE;
  1896. }
  1897. if (INTEL_INFO(dev_priv)->has_subslice_pg) {
  1898. rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
  1899. rpcs |= INTEL_INFO(dev_priv)->subslice_per_slice <<
  1900. GEN8_RPCS_SS_CNT_SHIFT;
  1901. rpcs |= GEN8_RPCS_ENABLE;
  1902. }
  1903. if (INTEL_INFO(dev_priv)->has_eu_pg) {
  1904. rpcs |= INTEL_INFO(dev_priv)->eu_per_subslice <<
  1905. GEN8_RPCS_EU_MIN_SHIFT;
  1906. rpcs |= INTEL_INFO(dev_priv)->eu_per_subslice <<
  1907. GEN8_RPCS_EU_MAX_SHIFT;
  1908. rpcs |= GEN8_RPCS_ENABLE;
  1909. }
  1910. return rpcs;
  1911. }
  1912. static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
  1913. {
  1914. u32 indirect_ctx_offset;
  1915. switch (INTEL_GEN(engine->i915)) {
  1916. default:
  1917. MISSING_CASE(INTEL_GEN(engine->i915));
  1918. /* fall through */
  1919. case 9:
  1920. indirect_ctx_offset =
  1921. GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1922. break;
  1923. case 8:
  1924. indirect_ctx_offset =
  1925. GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1926. break;
  1927. }
  1928. return indirect_ctx_offset;
  1929. }
  1930. static int
  1931. populate_lr_context(struct i915_gem_context *ctx,
  1932. struct drm_i915_gem_object *ctx_obj,
  1933. struct intel_engine_cs *engine,
  1934. struct intel_ringbuffer *ringbuf)
  1935. {
  1936. struct drm_i915_private *dev_priv = ctx->i915;
  1937. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
  1938. void *vaddr;
  1939. u32 *reg_state;
  1940. int ret;
  1941. if (!ppgtt)
  1942. ppgtt = dev_priv->mm.aliasing_ppgtt;
  1943. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1944. if (ret) {
  1945. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1946. return ret;
  1947. }
  1948. vaddr = i915_gem_object_pin_map(ctx_obj);
  1949. if (IS_ERR(vaddr)) {
  1950. ret = PTR_ERR(vaddr);
  1951. DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
  1952. return ret;
  1953. }
  1954. ctx_obj->dirty = true;
  1955. /* The second page of the context object contains some fields which must
  1956. * be set up prior to the first execution. */
  1957. reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  1958. /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
  1959. * commands followed by (reg, value) pairs. The values we are setting here are
  1960. * only for the first context restore: on a subsequent save, the GPU will
  1961. * recreate this batchbuffer with new values (including all the missing
  1962. * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
  1963. reg_state[CTX_LRI_HEADER_0] =
  1964. MI_LOAD_REGISTER_IMM(engine->id == RCS ? 14 : 11) | MI_LRI_FORCE_POSTED;
  1965. ASSIGN_CTX_REG(reg_state, CTX_CONTEXT_CONTROL,
  1966. RING_CONTEXT_CONTROL(engine),
  1967. _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
  1968. CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
  1969. (HAS_RESOURCE_STREAMER(dev_priv) ?
  1970. CTX_CTRL_RS_CTX_ENABLE : 0)));
  1971. ASSIGN_CTX_REG(reg_state, CTX_RING_HEAD, RING_HEAD(engine->mmio_base),
  1972. 0);
  1973. ASSIGN_CTX_REG(reg_state, CTX_RING_TAIL, RING_TAIL(engine->mmio_base),
  1974. 0);
  1975. /* Ring buffer start address is not known until the buffer is pinned.
  1976. * It is written to the context image in execlists_update_context()
  1977. */
  1978. ASSIGN_CTX_REG(reg_state, CTX_RING_BUFFER_START,
  1979. RING_START(engine->mmio_base), 0);
  1980. ASSIGN_CTX_REG(reg_state, CTX_RING_BUFFER_CONTROL,
  1981. RING_CTL(engine->mmio_base),
  1982. ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID);
  1983. ASSIGN_CTX_REG(reg_state, CTX_BB_HEAD_U,
  1984. RING_BBADDR_UDW(engine->mmio_base), 0);
  1985. ASSIGN_CTX_REG(reg_state, CTX_BB_HEAD_L,
  1986. RING_BBADDR(engine->mmio_base), 0);
  1987. ASSIGN_CTX_REG(reg_state, CTX_BB_STATE,
  1988. RING_BBSTATE(engine->mmio_base),
  1989. RING_BB_PPGTT);
  1990. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_HEAD_U,
  1991. RING_SBBADDR_UDW(engine->mmio_base), 0);
  1992. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_HEAD_L,
  1993. RING_SBBADDR(engine->mmio_base), 0);
  1994. ASSIGN_CTX_REG(reg_state, CTX_SECOND_BB_STATE,
  1995. RING_SBBSTATE(engine->mmio_base), 0);
  1996. if (engine->id == RCS) {
  1997. ASSIGN_CTX_REG(reg_state, CTX_BB_PER_CTX_PTR,
  1998. RING_BB_PER_CTX_PTR(engine->mmio_base), 0);
  1999. ASSIGN_CTX_REG(reg_state, CTX_RCS_INDIRECT_CTX,
  2000. RING_INDIRECT_CTX(engine->mmio_base), 0);
  2001. ASSIGN_CTX_REG(reg_state, CTX_RCS_INDIRECT_CTX_OFFSET,
  2002. RING_INDIRECT_CTX_OFFSET(engine->mmio_base), 0);
  2003. if (engine->wa_ctx.obj) {
  2004. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  2005. uint32_t ggtt_offset = i915_gem_obj_ggtt_offset(wa_ctx->obj);
  2006. reg_state[CTX_RCS_INDIRECT_CTX+1] =
  2007. (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
  2008. (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
  2009. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
  2010. intel_lr_indirect_ctx_offset(engine) << 6;
  2011. reg_state[CTX_BB_PER_CTX_PTR+1] =
  2012. (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
  2013. 0x01;
  2014. }
  2015. }
  2016. reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
  2017. ASSIGN_CTX_REG(reg_state, CTX_CTX_TIMESTAMP,
  2018. RING_CTX_TIMESTAMP(engine->mmio_base), 0);
  2019. /* PDP values well be assigned later if needed */
  2020. ASSIGN_CTX_REG(reg_state, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3),
  2021. 0);
  2022. ASSIGN_CTX_REG(reg_state, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3),
  2023. 0);
  2024. ASSIGN_CTX_REG(reg_state, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2),
  2025. 0);
  2026. ASSIGN_CTX_REG(reg_state, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2),
  2027. 0);
  2028. ASSIGN_CTX_REG(reg_state, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1),
  2029. 0);
  2030. ASSIGN_CTX_REG(reg_state, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1),
  2031. 0);
  2032. ASSIGN_CTX_REG(reg_state, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0),
  2033. 0);
  2034. ASSIGN_CTX_REG(reg_state, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0),
  2035. 0);
  2036. if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
  2037. /* 64b PPGTT (48bit canonical)
  2038. * PDP0_DESCRIPTOR contains the base address to PML4 and
  2039. * other PDP Descriptors are ignored.
  2040. */
  2041. ASSIGN_CTX_PML4(ppgtt, reg_state);
  2042. } else {
  2043. /* 32b PPGTT
  2044. * PDP*_DESCRIPTOR contains the base address of space supported.
  2045. * With dynamic page allocation, PDPs may not be allocated at
  2046. * this point. Point the unallocated PDPs to the scratch page
  2047. */
  2048. execlists_update_context_pdps(ppgtt, reg_state);
  2049. }
  2050. if (engine->id == RCS) {
  2051. reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  2052. ASSIGN_CTX_REG(reg_state, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
  2053. make_rpcs(dev_priv));
  2054. }
  2055. i915_gem_object_unpin_map(ctx_obj);
  2056. return 0;
  2057. }
  2058. /**
  2059. * intel_lr_context_size() - return the size of the context for an engine
  2060. * @engine: which engine to find the context size for
  2061. *
  2062. * Each engine may require a different amount of space for a context image,
  2063. * so when allocating (or copying) an image, this function can be used to
  2064. * find the right size for the specific engine.
  2065. *
  2066. * Return: size (in bytes) of an engine-specific context image
  2067. *
  2068. * Note: this size includes the HWSP, which is part of the context image
  2069. * in LRC mode, but does not include the "shared data page" used with
  2070. * GuC submission. The caller should account for this if using the GuC.
  2071. */
  2072. uint32_t intel_lr_context_size(struct intel_engine_cs *engine)
  2073. {
  2074. int ret = 0;
  2075. WARN_ON(INTEL_GEN(engine->i915) < 8);
  2076. switch (engine->id) {
  2077. case RCS:
  2078. if (INTEL_GEN(engine->i915) >= 9)
  2079. ret = GEN9_LR_CONTEXT_RENDER_SIZE;
  2080. else
  2081. ret = GEN8_LR_CONTEXT_RENDER_SIZE;
  2082. break;
  2083. case VCS:
  2084. case BCS:
  2085. case VECS:
  2086. case VCS2:
  2087. ret = GEN8_LR_CONTEXT_OTHER_SIZE;
  2088. break;
  2089. }
  2090. return ret;
  2091. }
  2092. /**
  2093. * execlists_context_deferred_alloc() - create the LRC specific bits of a context
  2094. * @ctx: LR context to create.
  2095. * @engine: engine to be used with the context.
  2096. *
  2097. * This function can be called more than once, with different engines, if we plan
  2098. * to use the context with them. The context backing objects and the ringbuffers
  2099. * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
  2100. * the creation is a deferred call: it's better to make sure first that we need to use
  2101. * a given ring with the context.
  2102. *
  2103. * Return: non-zero on error.
  2104. */
  2105. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  2106. struct intel_engine_cs *engine)
  2107. {
  2108. struct drm_i915_gem_object *ctx_obj;
  2109. struct intel_context *ce = &ctx->engine[engine->id];
  2110. uint32_t context_size;
  2111. struct intel_ringbuffer *ringbuf;
  2112. int ret;
  2113. WARN_ON(ce->state);
  2114. context_size = round_up(intel_lr_context_size(engine), 4096);
  2115. /* One extra page as the sharing data between driver and GuC */
  2116. context_size += PAGE_SIZE * LRC_PPHWSP_PN;
  2117. ctx_obj = i915_gem_object_create(&ctx->i915->drm, context_size);
  2118. if (IS_ERR(ctx_obj)) {
  2119. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
  2120. return PTR_ERR(ctx_obj);
  2121. }
  2122. ringbuf = intel_engine_create_ringbuffer(engine, ctx->ring_size);
  2123. if (IS_ERR(ringbuf)) {
  2124. ret = PTR_ERR(ringbuf);
  2125. goto error_deref_obj;
  2126. }
  2127. ret = populate_lr_context(ctx, ctx_obj, engine, ringbuf);
  2128. if (ret) {
  2129. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  2130. goto error_ringbuf;
  2131. }
  2132. ce->ringbuf = ringbuf;
  2133. ce->state = ctx_obj;
  2134. ce->initialised = engine->init_context == NULL;
  2135. return 0;
  2136. error_ringbuf:
  2137. intel_ringbuffer_free(ringbuf);
  2138. error_deref_obj:
  2139. drm_gem_object_unreference(&ctx_obj->base);
  2140. ce->ringbuf = NULL;
  2141. ce->state = NULL;
  2142. return ret;
  2143. }
  2144. void intel_lr_context_reset(struct drm_i915_private *dev_priv,
  2145. struct i915_gem_context *ctx)
  2146. {
  2147. struct intel_engine_cs *engine;
  2148. for_each_engine(engine, dev_priv) {
  2149. struct intel_context *ce = &ctx->engine[engine->id];
  2150. struct drm_i915_gem_object *ctx_obj = ce->state;
  2151. void *vaddr;
  2152. uint32_t *reg_state;
  2153. if (!ctx_obj)
  2154. continue;
  2155. vaddr = i915_gem_object_pin_map(ctx_obj);
  2156. if (WARN_ON(IS_ERR(vaddr)))
  2157. continue;
  2158. reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  2159. ctx_obj->dirty = true;
  2160. reg_state[CTX_RING_HEAD+1] = 0;
  2161. reg_state[CTX_RING_TAIL+1] = 0;
  2162. i915_gem_object_unpin_map(ctx_obj);
  2163. ce->ringbuf->head = 0;
  2164. ce->ringbuf->tail = 0;
  2165. }
  2166. }