intel_lrc.c 62 KB

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