intel_lrc.c 71 KB

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