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