intel_lrc.c 69 KB

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