intel_lrc.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  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. assert_ring_tail_valid(rq->ring, rq->tail);
  301. reg_state[CTX_RING_TAIL+1] = rq->tail;
  302. /* True 32b PPGTT with dynamic page allocation: update PDP
  303. * registers and point the unallocated PDPs to scratch page.
  304. * PML4 is allocated during ppgtt init, so this is not needed
  305. * in 48-bit mode.
  306. */
  307. if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
  308. execlists_update_context_pdps(ppgtt, reg_state);
  309. return ce->lrc_desc;
  310. }
  311. static void execlists_submit_ports(struct intel_engine_cs *engine)
  312. {
  313. struct drm_i915_private *dev_priv = engine->i915;
  314. struct execlist_port *port = engine->execlist_port;
  315. u32 __iomem *elsp =
  316. dev_priv->regs + i915_mmio_reg_offset(RING_ELSP(engine));
  317. u64 desc[2];
  318. GEM_BUG_ON(port[0].count > 1);
  319. if (!port[0].count)
  320. execlists_context_status_change(port[0].request,
  321. INTEL_CONTEXT_SCHEDULE_IN);
  322. desc[0] = execlists_update_context(port[0].request);
  323. GEM_DEBUG_EXEC(port[0].context_id = upper_32_bits(desc[0]));
  324. port[0].count++;
  325. if (port[1].request) {
  326. GEM_BUG_ON(port[1].count);
  327. execlists_context_status_change(port[1].request,
  328. INTEL_CONTEXT_SCHEDULE_IN);
  329. desc[1] = execlists_update_context(port[1].request);
  330. GEM_DEBUG_EXEC(port[1].context_id = upper_32_bits(desc[1]));
  331. port[1].count = 1;
  332. } else {
  333. desc[1] = 0;
  334. }
  335. GEM_BUG_ON(desc[0] == desc[1]);
  336. /* You must always write both descriptors in the order below. */
  337. writel(upper_32_bits(desc[1]), elsp);
  338. writel(lower_32_bits(desc[1]), elsp);
  339. writel(upper_32_bits(desc[0]), elsp);
  340. /* The context is automatically loaded after the following */
  341. writel(lower_32_bits(desc[0]), elsp);
  342. }
  343. static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
  344. {
  345. return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
  346. i915_gem_context_force_single_submission(ctx));
  347. }
  348. static bool can_merge_ctx(const struct i915_gem_context *prev,
  349. const struct i915_gem_context *next)
  350. {
  351. if (prev != next)
  352. return false;
  353. if (ctx_single_port_submission(prev))
  354. return false;
  355. return true;
  356. }
  357. static void execlists_dequeue(struct intel_engine_cs *engine)
  358. {
  359. struct drm_i915_gem_request *last;
  360. struct execlist_port *port = engine->execlist_port;
  361. struct rb_node *rb;
  362. bool submit = false;
  363. last = port->request;
  364. if (last)
  365. /* WaIdleLiteRestore:bdw,skl
  366. * Apply the wa NOOPs to prevent ring:HEAD == req:TAIL
  367. * as we resubmit the request. See gen8_emit_breadcrumb()
  368. * for where we prepare the padding after the end of the
  369. * request.
  370. */
  371. last->tail = last->wa_tail;
  372. GEM_BUG_ON(port[1].request);
  373. /* Hardware submission is through 2 ports. Conceptually each port
  374. * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
  375. * static for a context, and unique to each, so we only execute
  376. * requests belonging to a single context from each ring. RING_HEAD
  377. * is maintained by the CS in the context image, it marks the place
  378. * where it got up to last time, and through RING_TAIL we tell the CS
  379. * where we want to execute up to this time.
  380. *
  381. * In this list the requests are in order of execution. Consecutive
  382. * requests from the same context are adjacent in the ringbuffer. We
  383. * can combine these requests into a single RING_TAIL update:
  384. *
  385. * RING_HEAD...req1...req2
  386. * ^- RING_TAIL
  387. * since to execute req2 the CS must first execute req1.
  388. *
  389. * Our goal then is to point each port to the end of a consecutive
  390. * sequence of requests as being the most optimal (fewest wake ups
  391. * and context switches) submission.
  392. */
  393. spin_lock_irq(&engine->timeline->lock);
  394. rb = engine->execlist_first;
  395. while (rb) {
  396. struct drm_i915_gem_request *cursor =
  397. rb_entry(rb, typeof(*cursor), priotree.node);
  398. /* Can we combine this request with the current port? It has to
  399. * be the same context/ringbuffer and not have any exceptions
  400. * (e.g. GVT saying never to combine contexts).
  401. *
  402. * If we can combine the requests, we can execute both by
  403. * updating the RING_TAIL to point to the end of the second
  404. * request, and so we never need to tell the hardware about
  405. * the first.
  406. */
  407. if (last && !can_merge_ctx(cursor->ctx, last->ctx)) {
  408. /* If we are on the second port and cannot combine
  409. * this request with the last, then we are done.
  410. */
  411. if (port != engine->execlist_port)
  412. break;
  413. /* If GVT overrides us we only ever submit port[0],
  414. * leaving port[1] empty. Note that we also have
  415. * to be careful that we don't queue the same
  416. * context (even though a different request) to
  417. * the second port.
  418. */
  419. if (ctx_single_port_submission(last->ctx) ||
  420. ctx_single_port_submission(cursor->ctx))
  421. break;
  422. GEM_BUG_ON(last->ctx == cursor->ctx);
  423. i915_gem_request_assign(&port->request, last);
  424. port++;
  425. }
  426. rb = rb_next(rb);
  427. rb_erase(&cursor->priotree.node, &engine->execlist_queue);
  428. RB_CLEAR_NODE(&cursor->priotree.node);
  429. cursor->priotree.priority = INT_MAX;
  430. __i915_gem_request_submit(cursor);
  431. trace_i915_gem_request_in(cursor, port - engine->execlist_port);
  432. last = cursor;
  433. submit = true;
  434. }
  435. if (submit) {
  436. i915_gem_request_assign(&port->request, last);
  437. engine->execlist_first = rb;
  438. }
  439. spin_unlock_irq(&engine->timeline->lock);
  440. if (submit)
  441. execlists_submit_ports(engine);
  442. }
  443. static bool execlists_elsp_idle(struct intel_engine_cs *engine)
  444. {
  445. return !engine->execlist_port[0].request;
  446. }
  447. static bool execlists_elsp_ready(const struct intel_engine_cs *engine)
  448. {
  449. const struct execlist_port *port = engine->execlist_port;
  450. return port[0].count + port[1].count < 2;
  451. }
  452. /*
  453. * Check the unread Context Status Buffers and manage the submission of new
  454. * contexts to the ELSP accordingly.
  455. */
  456. static void intel_lrc_irq_handler(unsigned long data)
  457. {
  458. struct intel_engine_cs *engine = (struct intel_engine_cs *)data;
  459. struct execlist_port *port = engine->execlist_port;
  460. struct drm_i915_private *dev_priv = engine->i915;
  461. intel_uncore_forcewake_get(dev_priv, engine->fw_domains);
  462. /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
  463. * imposing the cost of a locked atomic transaction when submitting a
  464. * new request (outside of the context-switch interrupt).
  465. */
  466. while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
  467. u32 __iomem *csb_mmio =
  468. dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine));
  469. u32 __iomem *buf =
  470. dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0));
  471. unsigned int head, tail;
  472. /* The write will be ordered by the uncached read (itself
  473. * a memory barrier), so we do not need another in the form
  474. * of a locked instruction. The race between the interrupt
  475. * handler and the split test/clear is harmless as we order
  476. * our clear before the CSB read. If the interrupt arrived
  477. * first between the test and the clear, we read the updated
  478. * CSB and clear the bit. If the interrupt arrives as we read
  479. * the CSB or later (i.e. after we had cleared the bit) the bit
  480. * is set and we do a new loop.
  481. */
  482. __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
  483. head = readl(csb_mmio);
  484. tail = GEN8_CSB_WRITE_PTR(head);
  485. head = GEN8_CSB_READ_PTR(head);
  486. while (head != tail) {
  487. unsigned int status;
  488. if (++head == GEN8_CSB_ENTRIES)
  489. head = 0;
  490. /* We are flying near dragons again.
  491. *
  492. * We hold a reference to the request in execlist_port[]
  493. * but no more than that. We are operating in softirq
  494. * context and so cannot hold any mutex or sleep. That
  495. * prevents us stopping the requests we are processing
  496. * in port[] from being retired simultaneously (the
  497. * breadcrumb will be complete before we see the
  498. * context-switch). As we only hold the reference to the
  499. * request, any pointer chasing underneath the request
  500. * is subject to a potential use-after-free. Thus we
  501. * store all of the bookkeeping within port[] as
  502. * required, and avoid using unguarded pointers beneath
  503. * request itself. The same applies to the atomic
  504. * status notifier.
  505. */
  506. status = readl(buf + 2 * head);
  507. if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
  508. continue;
  509. /* Check the context/desc id for this event matches */
  510. GEM_DEBUG_BUG_ON(readl(buf + 2 * head + 1) !=
  511. port[0].context_id);
  512. GEM_BUG_ON(port[0].count == 0);
  513. if (--port[0].count == 0) {
  514. GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
  515. GEM_BUG_ON(!i915_gem_request_completed(port[0].request));
  516. execlists_context_status_change(port[0].request,
  517. INTEL_CONTEXT_SCHEDULE_OUT);
  518. trace_i915_gem_request_out(port[0].request);
  519. i915_gem_request_put(port[0].request);
  520. port[0] = port[1];
  521. memset(&port[1], 0, sizeof(port[1]));
  522. }
  523. GEM_BUG_ON(port[0].count == 0 &&
  524. !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
  525. }
  526. writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
  527. csb_mmio);
  528. }
  529. if (execlists_elsp_ready(engine))
  530. execlists_dequeue(engine);
  531. intel_uncore_forcewake_put(dev_priv, engine->fw_domains);
  532. }
  533. static bool insert_request(struct i915_priotree *pt, struct rb_root *root)
  534. {
  535. struct rb_node **p, *rb;
  536. bool first = true;
  537. /* most positive priority is scheduled first, equal priorities fifo */
  538. rb = NULL;
  539. p = &root->rb_node;
  540. while (*p) {
  541. struct i915_priotree *pos;
  542. rb = *p;
  543. pos = rb_entry(rb, typeof(*pos), node);
  544. if (pt->priority > pos->priority) {
  545. p = &rb->rb_left;
  546. } else {
  547. p = &rb->rb_right;
  548. first = false;
  549. }
  550. }
  551. rb_link_node(&pt->node, rb, p);
  552. rb_insert_color(&pt->node, root);
  553. return first;
  554. }
  555. static void execlists_submit_request(struct drm_i915_gem_request *request)
  556. {
  557. struct intel_engine_cs *engine = request->engine;
  558. unsigned long flags;
  559. /* Will be called from irq-context when using foreign fences. */
  560. spin_lock_irqsave(&engine->timeline->lock, flags);
  561. if (insert_request(&request->priotree, &engine->execlist_queue)) {
  562. engine->execlist_first = &request->priotree.node;
  563. if (execlists_elsp_ready(engine))
  564. tasklet_hi_schedule(&engine->irq_tasklet);
  565. }
  566. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  567. }
  568. static struct intel_engine_cs *
  569. pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
  570. {
  571. struct intel_engine_cs *engine =
  572. container_of(pt, struct drm_i915_gem_request, priotree)->engine;
  573. GEM_BUG_ON(!locked);
  574. if (engine != locked) {
  575. spin_unlock(&locked->timeline->lock);
  576. spin_lock(&engine->timeline->lock);
  577. }
  578. return engine;
  579. }
  580. static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
  581. {
  582. struct intel_engine_cs *engine;
  583. struct i915_dependency *dep, *p;
  584. struct i915_dependency stack;
  585. LIST_HEAD(dfs);
  586. if (prio <= READ_ONCE(request->priotree.priority))
  587. return;
  588. /* Need BKL in order to use the temporary link inside i915_dependency */
  589. lockdep_assert_held(&request->i915->drm.struct_mutex);
  590. stack.signaler = &request->priotree;
  591. list_add(&stack.dfs_link, &dfs);
  592. /* Recursively bump all dependent priorities to match the new request.
  593. *
  594. * A naive approach would be to use recursion:
  595. * static void update_priorities(struct i915_priotree *pt, prio) {
  596. * list_for_each_entry(dep, &pt->signalers_list, signal_link)
  597. * update_priorities(dep->signal, prio)
  598. * insert_request(pt);
  599. * }
  600. * but that may have unlimited recursion depth and so runs a very
  601. * real risk of overunning the kernel stack. Instead, we build
  602. * a flat list of all dependencies starting with the current request.
  603. * As we walk the list of dependencies, we add all of its dependencies
  604. * to the end of the list (this may include an already visited
  605. * request) and continue to walk onwards onto the new dependencies. The
  606. * end result is a topological list of requests in reverse order, the
  607. * last element in the list is the request we must execute first.
  608. */
  609. list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
  610. struct i915_priotree *pt = dep->signaler;
  611. /* Within an engine, there can be no cycle, but we may
  612. * refer to the same dependency chain multiple times
  613. * (redundant dependencies are not eliminated) and across
  614. * engines.
  615. */
  616. list_for_each_entry(p, &pt->signalers_list, signal_link) {
  617. GEM_BUG_ON(p->signaler->priority < pt->priority);
  618. if (prio > READ_ONCE(p->signaler->priority))
  619. list_move_tail(&p->dfs_link, &dfs);
  620. }
  621. list_safe_reset_next(dep, p, dfs_link);
  622. }
  623. engine = request->engine;
  624. spin_lock_irq(&engine->timeline->lock);
  625. /* Fifo and depth-first replacement ensure our deps execute before us */
  626. list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
  627. struct i915_priotree *pt = dep->signaler;
  628. INIT_LIST_HEAD(&dep->dfs_link);
  629. engine = pt_lock_engine(pt, engine);
  630. if (prio <= pt->priority)
  631. continue;
  632. pt->priority = prio;
  633. if (!RB_EMPTY_NODE(&pt->node)) {
  634. rb_erase(&pt->node, &engine->execlist_queue);
  635. if (insert_request(pt, &engine->execlist_queue))
  636. engine->execlist_first = &pt->node;
  637. }
  638. }
  639. spin_unlock_irq(&engine->timeline->lock);
  640. /* XXX Do we need to preempt to make room for us and our deps? */
  641. }
  642. static int execlists_context_pin(struct intel_engine_cs *engine,
  643. struct i915_gem_context *ctx)
  644. {
  645. struct intel_context *ce = &ctx->engine[engine->id];
  646. unsigned int flags;
  647. void *vaddr;
  648. int ret;
  649. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  650. if (ce->pin_count++)
  651. return 0;
  652. GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
  653. if (!ce->state) {
  654. ret = execlists_context_deferred_alloc(ctx, engine);
  655. if (ret)
  656. goto err;
  657. }
  658. GEM_BUG_ON(!ce->state);
  659. flags = PIN_GLOBAL | PIN_HIGH;
  660. if (ctx->ggtt_offset_bias)
  661. flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
  662. ret = i915_vma_pin(ce->state, 0, GEN8_LR_CONTEXT_ALIGN, flags);
  663. if (ret)
  664. goto err;
  665. vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
  666. if (IS_ERR(vaddr)) {
  667. ret = PTR_ERR(vaddr);
  668. goto unpin_vma;
  669. }
  670. ret = intel_ring_pin(ce->ring, ctx->ggtt_offset_bias);
  671. if (ret)
  672. goto unpin_map;
  673. intel_lr_context_descriptor_update(ctx, engine);
  674. ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  675. ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
  676. i915_ggtt_offset(ce->ring->vma);
  677. ce->state->obj->mm.dirty = true;
  678. i915_gem_context_get(ctx);
  679. return 0;
  680. unpin_map:
  681. i915_gem_object_unpin_map(ce->state->obj);
  682. unpin_vma:
  683. __i915_vma_unpin(ce->state);
  684. err:
  685. ce->pin_count = 0;
  686. return ret;
  687. }
  688. static void execlists_context_unpin(struct intel_engine_cs *engine,
  689. struct i915_gem_context *ctx)
  690. {
  691. struct intel_context *ce = &ctx->engine[engine->id];
  692. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  693. GEM_BUG_ON(ce->pin_count == 0);
  694. if (--ce->pin_count)
  695. return;
  696. intel_ring_unpin(ce->ring);
  697. i915_gem_object_unpin_map(ce->state->obj);
  698. i915_vma_unpin(ce->state);
  699. i915_gem_context_put(ctx);
  700. }
  701. static int execlists_request_alloc(struct drm_i915_gem_request *request)
  702. {
  703. struct intel_engine_cs *engine = request->engine;
  704. struct intel_context *ce = &request->ctx->engine[engine->id];
  705. u32 *cs;
  706. int ret;
  707. GEM_BUG_ON(!ce->pin_count);
  708. /* Flush enough space to reduce the likelihood of waiting after
  709. * we start building the request - in which case we will just
  710. * have to repeat work.
  711. */
  712. request->reserved_space += EXECLISTS_REQUEST_SIZE;
  713. GEM_BUG_ON(!ce->ring);
  714. request->ring = ce->ring;
  715. if (i915.enable_guc_submission) {
  716. /*
  717. * Check that the GuC has space for the request before
  718. * going any further, as the i915_add_request() call
  719. * later on mustn't fail ...
  720. */
  721. ret = i915_guc_wq_reserve(request);
  722. if (ret)
  723. goto err;
  724. }
  725. cs = intel_ring_begin(request, 0);
  726. if (IS_ERR(cs)) {
  727. ret = PTR_ERR(cs);
  728. goto err_unreserve;
  729. }
  730. if (!ce->initialised) {
  731. ret = engine->init_context(request);
  732. if (ret)
  733. goto err_unreserve;
  734. ce->initialised = true;
  735. }
  736. /* Note that after this point, we have committed to using
  737. * this request as it is being used to both track the
  738. * state of engine initialisation and liveness of the
  739. * golden renderstate above. Think twice before you try
  740. * to cancel/unwind this request now.
  741. */
  742. request->reserved_space -= EXECLISTS_REQUEST_SIZE;
  743. return 0;
  744. err_unreserve:
  745. if (i915.enable_guc_submission)
  746. i915_guc_wq_unreserve(request);
  747. err:
  748. return ret;
  749. }
  750. /*
  751. * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
  752. * PIPE_CONTROL instruction. This is required for the flush to happen correctly
  753. * but there is a slight complication as this is applied in WA batch where the
  754. * values are only initialized once so we cannot take register value at the
  755. * beginning and reuse it further; hence we save its value to memory, upload a
  756. * constant value with bit21 set and then we restore it back with the saved value.
  757. * To simplify the WA, a constant value is formed by using the default value
  758. * of this register. This shouldn't be a problem because we are only modifying
  759. * it for a short period and this batch in non-premptible. We can ofcourse
  760. * use additional instructions that read the actual value of the register
  761. * at that time and set our bit of interest but it makes the WA complicated.
  762. *
  763. * This WA is also required for Gen9 so extracting as a function avoids
  764. * code duplication.
  765. */
  766. static u32 *
  767. gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
  768. {
  769. *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
  770. *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
  771. *batch++ = i915_ggtt_offset(engine->scratch) + 256;
  772. *batch++ = 0;
  773. *batch++ = MI_LOAD_REGISTER_IMM(1);
  774. *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
  775. *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
  776. batch = gen8_emit_pipe_control(batch,
  777. PIPE_CONTROL_CS_STALL |
  778. PIPE_CONTROL_DC_FLUSH_ENABLE,
  779. 0);
  780. *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
  781. *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
  782. *batch++ = i915_ggtt_offset(engine->scratch) + 256;
  783. *batch++ = 0;
  784. return batch;
  785. }
  786. /*
  787. * Typically we only have one indirect_ctx and per_ctx batch buffer which are
  788. * initialized at the beginning and shared across all contexts but this field
  789. * helps us to have multiple batches at different offsets and select them based
  790. * on a criteria. At the moment this batch always start at the beginning of the page
  791. * and at this point we don't have multiple wa_ctx batch buffers.
  792. *
  793. * The number of WA applied are not known at the beginning; we use this field
  794. * to return the no of DWORDS written.
  795. *
  796. * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
  797. * so it adds NOOPs as padding to make it cacheline aligned.
  798. * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
  799. * makes a complete batch buffer.
  800. */
  801. static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
  802. {
  803. /* WaDisableCtxRestoreArbitration:bdw,chv */
  804. *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
  805. /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
  806. if (IS_BROADWELL(engine->i915))
  807. batch = gen8_emit_flush_coherentl3_wa(engine, batch);
  808. /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
  809. /* Actual scratch location is at 128 bytes offset */
  810. batch = gen8_emit_pipe_control(batch,
  811. PIPE_CONTROL_FLUSH_L3 |
  812. PIPE_CONTROL_GLOBAL_GTT_IVB |
  813. PIPE_CONTROL_CS_STALL |
  814. PIPE_CONTROL_QW_WRITE,
  815. i915_ggtt_offset(engine->scratch) +
  816. 2 * CACHELINE_BYTES);
  817. /* Pad to end of cacheline */
  818. while ((unsigned long)batch % CACHELINE_BYTES)
  819. *batch++ = MI_NOOP;
  820. /*
  821. * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
  822. * execution depends on the length specified in terms of cache lines
  823. * in the register CTX_RCS_INDIRECT_CTX
  824. */
  825. return batch;
  826. }
  827. /*
  828. * This batch is started immediately after indirect_ctx batch. Since we ensure
  829. * that indirect_ctx ends on a cacheline this batch is aligned automatically.
  830. *
  831. * The number of DWORDS written are returned using this field.
  832. *
  833. * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
  834. * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
  835. */
  836. static u32 *gen8_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
  837. {
  838. /* WaDisableCtxRestoreArbitration:bdw,chv */
  839. *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  840. *batch++ = MI_BATCH_BUFFER_END;
  841. return batch;
  842. }
  843. static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
  844. {
  845. /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
  846. batch = gen8_emit_flush_coherentl3_wa(engine, batch);
  847. /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
  848. *batch++ = MI_LOAD_REGISTER_IMM(1);
  849. *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
  850. *batch++ = _MASKED_BIT_DISABLE(
  851. GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
  852. *batch++ = MI_NOOP;
  853. /* WaClearSlmSpaceAtContextSwitch:kbl */
  854. /* Actual scratch location is at 128 bytes offset */
  855. if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
  856. batch = gen8_emit_pipe_control(batch,
  857. PIPE_CONTROL_FLUSH_L3 |
  858. PIPE_CONTROL_GLOBAL_GTT_IVB |
  859. PIPE_CONTROL_CS_STALL |
  860. PIPE_CONTROL_QW_WRITE,
  861. i915_ggtt_offset(engine->scratch)
  862. + 2 * CACHELINE_BYTES);
  863. }
  864. /* WaMediaPoolStateCmdInWABB:bxt,glk */
  865. if (HAS_POOLED_EU(engine->i915)) {
  866. /*
  867. * EU pool configuration is setup along with golden context
  868. * during context initialization. This value depends on
  869. * device type (2x6 or 3x6) and needs to be updated based
  870. * on which subslice is disabled especially for 2x6
  871. * devices, however it is safe to load default
  872. * configuration of 3x6 device instead of masking off
  873. * corresponding bits because HW ignores bits of a disabled
  874. * subslice and drops down to appropriate config. Please
  875. * see render_state_setup() in i915_gem_render_state.c for
  876. * possible configurations, to avoid duplication they are
  877. * not shown here again.
  878. */
  879. *batch++ = GEN9_MEDIA_POOL_STATE;
  880. *batch++ = GEN9_MEDIA_POOL_ENABLE;
  881. *batch++ = 0x00777000;
  882. *batch++ = 0;
  883. *batch++ = 0;
  884. *batch++ = 0;
  885. }
  886. /* Pad to end of cacheline */
  887. while ((unsigned long)batch % CACHELINE_BYTES)
  888. *batch++ = MI_NOOP;
  889. return batch;
  890. }
  891. static u32 *gen9_init_perctx_bb(struct intel_engine_cs *engine, u32 *batch)
  892. {
  893. *batch++ = MI_BATCH_BUFFER_END;
  894. return batch;
  895. }
  896. #define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
  897. static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
  898. {
  899. struct drm_i915_gem_object *obj;
  900. struct i915_vma *vma;
  901. int err;
  902. obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
  903. if (IS_ERR(obj))
  904. return PTR_ERR(obj);
  905. vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
  906. if (IS_ERR(vma)) {
  907. err = PTR_ERR(vma);
  908. goto err;
  909. }
  910. err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
  911. if (err)
  912. goto err;
  913. engine->wa_ctx.vma = vma;
  914. return 0;
  915. err:
  916. i915_gem_object_put(obj);
  917. return err;
  918. }
  919. static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
  920. {
  921. i915_vma_unpin_and_release(&engine->wa_ctx.vma);
  922. }
  923. typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
  924. static int intel_init_workaround_bb(struct intel_engine_cs *engine)
  925. {
  926. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  927. struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
  928. &wa_ctx->per_ctx };
  929. wa_bb_func_t wa_bb_fn[2];
  930. struct page *page;
  931. void *batch, *batch_ptr;
  932. unsigned int i;
  933. int ret;
  934. if (WARN_ON(engine->id != RCS || !engine->scratch))
  935. return -EINVAL;
  936. switch (INTEL_GEN(engine->i915)) {
  937. case 9:
  938. wa_bb_fn[0] = gen9_init_indirectctx_bb;
  939. wa_bb_fn[1] = gen9_init_perctx_bb;
  940. break;
  941. case 8:
  942. wa_bb_fn[0] = gen8_init_indirectctx_bb;
  943. wa_bb_fn[1] = gen8_init_perctx_bb;
  944. break;
  945. default:
  946. MISSING_CASE(INTEL_GEN(engine->i915));
  947. return 0;
  948. }
  949. ret = lrc_setup_wa_ctx(engine);
  950. if (ret) {
  951. DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
  952. return ret;
  953. }
  954. page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
  955. batch = batch_ptr = kmap_atomic(page);
  956. /*
  957. * Emit the two workaround batch buffers, recording the offset from the
  958. * start of the workaround batch buffer object for each and their
  959. * respective sizes.
  960. */
  961. for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
  962. wa_bb[i]->offset = batch_ptr - batch;
  963. if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
  964. ret = -EINVAL;
  965. break;
  966. }
  967. batch_ptr = wa_bb_fn[i](engine, batch_ptr);
  968. wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
  969. }
  970. BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
  971. kunmap_atomic(batch);
  972. if (ret)
  973. lrc_destroy_wa_ctx(engine);
  974. return ret;
  975. }
  976. static u32 port_seqno(struct execlist_port *port)
  977. {
  978. return port->request ? port->request->global_seqno : 0;
  979. }
  980. static int gen8_init_common_ring(struct intel_engine_cs *engine)
  981. {
  982. struct drm_i915_private *dev_priv = engine->i915;
  983. int ret;
  984. ret = intel_mocs_init_engine(engine);
  985. if (ret)
  986. return ret;
  987. intel_engine_reset_breadcrumbs(engine);
  988. intel_engine_init_hangcheck(engine);
  989. I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
  990. I915_WRITE(RING_MODE_GEN7(engine),
  991. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  992. I915_WRITE(RING_HWS_PGA(engine->mmio_base),
  993. engine->status_page.ggtt_offset);
  994. POSTING_READ(RING_HWS_PGA(engine->mmio_base));
  995. DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
  996. /* After a GPU reset, we may have requests to replay */
  997. clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
  998. if (!i915.enable_guc_submission && !execlists_elsp_idle(engine)) {
  999. DRM_DEBUG_DRIVER("Restarting %s from requests [0x%x, 0x%x]\n",
  1000. engine->name,
  1001. port_seqno(&engine->execlist_port[0]),
  1002. port_seqno(&engine->execlist_port[1]));
  1003. engine->execlist_port[0].count = 0;
  1004. engine->execlist_port[1].count = 0;
  1005. execlists_submit_ports(engine);
  1006. }
  1007. return 0;
  1008. }
  1009. static int gen8_init_render_ring(struct intel_engine_cs *engine)
  1010. {
  1011. struct drm_i915_private *dev_priv = engine->i915;
  1012. int ret;
  1013. ret = gen8_init_common_ring(engine);
  1014. if (ret)
  1015. return ret;
  1016. /* We need to disable the AsyncFlip performance optimisations in order
  1017. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  1018. * programmed to '1' on all products.
  1019. *
  1020. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  1021. */
  1022. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  1023. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  1024. return init_workarounds_ring(engine);
  1025. }
  1026. static int gen9_init_render_ring(struct intel_engine_cs *engine)
  1027. {
  1028. int ret;
  1029. ret = gen8_init_common_ring(engine);
  1030. if (ret)
  1031. return ret;
  1032. return init_workarounds_ring(engine);
  1033. }
  1034. static void reset_common_ring(struct intel_engine_cs *engine,
  1035. struct drm_i915_gem_request *request)
  1036. {
  1037. struct execlist_port *port = engine->execlist_port;
  1038. struct intel_context *ce;
  1039. /* If the request was innocent, we leave the request in the ELSP
  1040. * and will try to replay it on restarting. The context image may
  1041. * have been corrupted by the reset, in which case we may have
  1042. * to service a new GPU hang, but more likely we can continue on
  1043. * without impact.
  1044. *
  1045. * If the request was guilty, we presume the context is corrupt
  1046. * and have to at least restore the RING register in the context
  1047. * image back to the expected values to skip over the guilty request.
  1048. */
  1049. if (!request || request->fence.error != -EIO)
  1050. return;
  1051. /* We want a simple context + ring to execute the breadcrumb update.
  1052. * We cannot rely on the context being intact across the GPU hang,
  1053. * so clear it and rebuild just what we need for the breadcrumb.
  1054. * All pending requests for this context will be zapped, and any
  1055. * future request will be after userspace has had the opportunity
  1056. * to recreate its own state.
  1057. */
  1058. ce = &request->ctx->engine[engine->id];
  1059. execlists_init_reg_state(ce->lrc_reg_state,
  1060. request->ctx, engine, ce->ring);
  1061. /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
  1062. ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
  1063. i915_ggtt_offset(ce->ring->vma);
  1064. ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
  1065. request->ring->head = request->postfix;
  1066. intel_ring_update_space(request->ring);
  1067. /* Catch up with any missed context-switch interrupts */
  1068. if (request->ctx != port[0].request->ctx) {
  1069. i915_gem_request_put(port[0].request);
  1070. port[0] = port[1];
  1071. memset(&port[1], 0, sizeof(port[1]));
  1072. }
  1073. GEM_BUG_ON(request->ctx != port[0].request->ctx);
  1074. /* Reset WaIdleLiteRestore:bdw,skl as well */
  1075. request->tail =
  1076. intel_ring_wrap(request->ring,
  1077. request->wa_tail - WA_TAIL_DWORDS*sizeof(u32));
  1078. assert_ring_tail_valid(request->ring, request->tail);
  1079. }
  1080. static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
  1081. {
  1082. struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
  1083. struct intel_engine_cs *engine = req->engine;
  1084. const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
  1085. u32 *cs;
  1086. int i;
  1087. cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
  1088. if (IS_ERR(cs))
  1089. return PTR_ERR(cs);
  1090. *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
  1091. for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
  1092. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  1093. *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
  1094. *cs++ = upper_32_bits(pd_daddr);
  1095. *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
  1096. *cs++ = lower_32_bits(pd_daddr);
  1097. }
  1098. *cs++ = MI_NOOP;
  1099. intel_ring_advance(req, cs);
  1100. return 0;
  1101. }
  1102. static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
  1103. u64 offset, u32 len,
  1104. const unsigned int flags)
  1105. {
  1106. u32 *cs;
  1107. int ret;
  1108. /* Don't rely in hw updating PDPs, specially in lite-restore.
  1109. * Ideally, we should set Force PD Restore in ctx descriptor,
  1110. * but we can't. Force Restore would be a second option, but
  1111. * it is unsafe in case of lite-restore (because the ctx is
  1112. * not idle). PML4 is allocated during ppgtt init so this is
  1113. * not needed in 48-bit.*/
  1114. if (req->ctx->ppgtt &&
  1115. (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
  1116. !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
  1117. !intel_vgpu_active(req->i915)) {
  1118. ret = intel_logical_ring_emit_pdps(req);
  1119. if (ret)
  1120. return ret;
  1121. req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
  1122. }
  1123. cs = intel_ring_begin(req, 4);
  1124. if (IS_ERR(cs))
  1125. return PTR_ERR(cs);
  1126. /* FIXME(BDW): Address space and security selectors. */
  1127. *cs++ = MI_BATCH_BUFFER_START_GEN8 |
  1128. (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
  1129. (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
  1130. *cs++ = lower_32_bits(offset);
  1131. *cs++ = upper_32_bits(offset);
  1132. *cs++ = MI_NOOP;
  1133. intel_ring_advance(req, cs);
  1134. return 0;
  1135. }
  1136. static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
  1137. {
  1138. struct drm_i915_private *dev_priv = engine->i915;
  1139. I915_WRITE_IMR(engine,
  1140. ~(engine->irq_enable_mask | engine->irq_keep_mask));
  1141. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1142. }
  1143. static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
  1144. {
  1145. struct drm_i915_private *dev_priv = engine->i915;
  1146. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  1147. }
  1148. static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
  1149. {
  1150. u32 cmd, *cs;
  1151. cs = intel_ring_begin(request, 4);
  1152. if (IS_ERR(cs))
  1153. return PTR_ERR(cs);
  1154. cmd = MI_FLUSH_DW + 1;
  1155. /* We always require a command barrier so that subsequent
  1156. * commands, such as breadcrumb interrupts, are strictly ordered
  1157. * wrt the contents of the write cache being flushed to memory
  1158. * (and thus being coherent from the CPU).
  1159. */
  1160. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1161. if (mode & EMIT_INVALIDATE) {
  1162. cmd |= MI_INVALIDATE_TLB;
  1163. if (request->engine->id == VCS)
  1164. cmd |= MI_INVALIDATE_BSD;
  1165. }
  1166. *cs++ = cmd;
  1167. *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
  1168. *cs++ = 0; /* upper addr */
  1169. *cs++ = 0; /* value */
  1170. intel_ring_advance(request, cs);
  1171. return 0;
  1172. }
  1173. static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
  1174. u32 mode)
  1175. {
  1176. struct intel_engine_cs *engine = request->engine;
  1177. u32 scratch_addr =
  1178. i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  1179. bool vf_flush_wa = false, dc_flush_wa = false;
  1180. u32 *cs, flags = 0;
  1181. int len;
  1182. flags |= PIPE_CONTROL_CS_STALL;
  1183. if (mode & EMIT_FLUSH) {
  1184. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  1185. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  1186. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  1187. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  1188. }
  1189. if (mode & EMIT_INVALIDATE) {
  1190. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  1191. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  1192. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  1193. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1194. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  1195. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  1196. flags |= PIPE_CONTROL_QW_WRITE;
  1197. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1198. /*
  1199. * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
  1200. * pipe control.
  1201. */
  1202. if (IS_GEN9(request->i915))
  1203. vf_flush_wa = true;
  1204. /* WaForGAMHang:kbl */
  1205. if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
  1206. dc_flush_wa = true;
  1207. }
  1208. len = 6;
  1209. if (vf_flush_wa)
  1210. len += 6;
  1211. if (dc_flush_wa)
  1212. len += 12;
  1213. cs = intel_ring_begin(request, len);
  1214. if (IS_ERR(cs))
  1215. return PTR_ERR(cs);
  1216. if (vf_flush_wa)
  1217. cs = gen8_emit_pipe_control(cs, 0, 0);
  1218. if (dc_flush_wa)
  1219. cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
  1220. 0);
  1221. cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
  1222. if (dc_flush_wa)
  1223. cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
  1224. intel_ring_advance(request, cs);
  1225. return 0;
  1226. }
  1227. /*
  1228. * Reserve space for 2 NOOPs at the end of each request to be
  1229. * used as a workaround for not being allowed to do lite
  1230. * restore with HEAD==TAIL (WaIdleLiteRestore).
  1231. */
  1232. static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
  1233. {
  1234. *cs++ = MI_NOOP;
  1235. *cs++ = MI_NOOP;
  1236. request->wa_tail = intel_ring_offset(request, cs);
  1237. }
  1238. static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
  1239. {
  1240. /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
  1241. BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
  1242. *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
  1243. *cs++ = intel_hws_seqno_address(request->engine) | MI_FLUSH_DW_USE_GTT;
  1244. *cs++ = 0;
  1245. *cs++ = request->global_seqno;
  1246. *cs++ = MI_USER_INTERRUPT;
  1247. *cs++ = MI_NOOP;
  1248. request->tail = intel_ring_offset(request, cs);
  1249. assert_ring_tail_valid(request->ring, request->tail);
  1250. gen8_emit_wa_tail(request, cs);
  1251. }
  1252. static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
  1253. static void gen8_emit_breadcrumb_render(struct drm_i915_gem_request *request,
  1254. u32 *cs)
  1255. {
  1256. /* We're using qword write, seqno should be aligned to 8 bytes. */
  1257. BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
  1258. /* w/a for post sync ops following a GPGPU operation we
  1259. * need a prior CS_STALL, which is emitted by the flush
  1260. * following the batch.
  1261. */
  1262. *cs++ = GFX_OP_PIPE_CONTROL(6);
  1263. *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
  1264. PIPE_CONTROL_QW_WRITE;
  1265. *cs++ = intel_hws_seqno_address(request->engine);
  1266. *cs++ = 0;
  1267. *cs++ = request->global_seqno;
  1268. /* We're thrashing one dword of HWS. */
  1269. *cs++ = 0;
  1270. *cs++ = MI_USER_INTERRUPT;
  1271. *cs++ = MI_NOOP;
  1272. request->tail = intel_ring_offset(request, cs);
  1273. assert_ring_tail_valid(request->ring, request->tail);
  1274. gen8_emit_wa_tail(request, cs);
  1275. }
  1276. static const int gen8_emit_breadcrumb_render_sz = 8 + WA_TAIL_DWORDS;
  1277. static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
  1278. {
  1279. int ret;
  1280. ret = intel_ring_workarounds_emit(req);
  1281. if (ret)
  1282. return ret;
  1283. ret = intel_rcs_context_init_mocs(req);
  1284. /*
  1285. * Failing to program the MOCS is non-fatal.The system will not
  1286. * run at peak performance. So generate an error and carry on.
  1287. */
  1288. if (ret)
  1289. DRM_ERROR("MOCS failed to program: expect performance issues.\n");
  1290. return i915_gem_render_state_emit(req);
  1291. }
  1292. /**
  1293. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1294. * @engine: Engine Command Streamer.
  1295. */
  1296. void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
  1297. {
  1298. struct drm_i915_private *dev_priv;
  1299. /*
  1300. * Tasklet cannot be active at this point due intel_mark_active/idle
  1301. * so this is just for documentation.
  1302. */
  1303. if (WARN_ON(test_bit(TASKLET_STATE_SCHED, &engine->irq_tasklet.state)))
  1304. tasklet_kill(&engine->irq_tasklet);
  1305. dev_priv = engine->i915;
  1306. if (engine->buffer) {
  1307. WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1308. }
  1309. if (engine->cleanup)
  1310. engine->cleanup(engine);
  1311. if (engine->status_page.vma) {
  1312. i915_gem_object_unpin_map(engine->status_page.vma->obj);
  1313. engine->status_page.vma = NULL;
  1314. }
  1315. intel_engine_cleanup_common(engine);
  1316. lrc_destroy_wa_ctx(engine);
  1317. engine->i915 = NULL;
  1318. dev_priv->engine[engine->id] = NULL;
  1319. kfree(engine);
  1320. }
  1321. static void execlists_set_default_submission(struct intel_engine_cs *engine)
  1322. {
  1323. engine->submit_request = execlists_submit_request;
  1324. engine->schedule = execlists_schedule;
  1325. engine->irq_tasklet.func = intel_lrc_irq_handler;
  1326. }
  1327. static void
  1328. logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  1329. {
  1330. /* Default vfuncs which can be overriden by each engine. */
  1331. engine->init_hw = gen8_init_common_ring;
  1332. engine->reset_hw = reset_common_ring;
  1333. engine->context_pin = execlists_context_pin;
  1334. engine->context_unpin = execlists_context_unpin;
  1335. engine->request_alloc = execlists_request_alloc;
  1336. engine->emit_flush = gen8_emit_flush;
  1337. engine->emit_breadcrumb = gen8_emit_breadcrumb;
  1338. engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
  1339. engine->set_default_submission = execlists_set_default_submission;
  1340. engine->irq_enable = gen8_logical_ring_enable_irq;
  1341. engine->irq_disable = gen8_logical_ring_disable_irq;
  1342. engine->emit_bb_start = gen8_emit_bb_start;
  1343. }
  1344. static inline void
  1345. logical_ring_default_irqs(struct intel_engine_cs *engine)
  1346. {
  1347. unsigned shift = engine->irq_shift;
  1348. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
  1349. engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
  1350. }
  1351. static int
  1352. lrc_setup_hws(struct intel_engine_cs *engine, struct i915_vma *vma)
  1353. {
  1354. const int hws_offset = LRC_PPHWSP_PN * PAGE_SIZE;
  1355. void *hws;
  1356. /* The HWSP is part of the default context object in LRC mode. */
  1357. hws = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
  1358. if (IS_ERR(hws))
  1359. return PTR_ERR(hws);
  1360. engine->status_page.page_addr = hws + hws_offset;
  1361. engine->status_page.ggtt_offset = i915_ggtt_offset(vma) + hws_offset;
  1362. engine->status_page.vma = vma;
  1363. return 0;
  1364. }
  1365. static void
  1366. logical_ring_setup(struct intel_engine_cs *engine)
  1367. {
  1368. struct drm_i915_private *dev_priv = engine->i915;
  1369. enum forcewake_domains fw_domains;
  1370. intel_engine_setup_common(engine);
  1371. /* Intentionally left blank. */
  1372. engine->buffer = NULL;
  1373. fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
  1374. RING_ELSP(engine),
  1375. FW_REG_WRITE);
  1376. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1377. RING_CONTEXT_STATUS_PTR(engine),
  1378. FW_REG_READ | FW_REG_WRITE);
  1379. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1380. RING_CONTEXT_STATUS_BUF_BASE(engine),
  1381. FW_REG_READ);
  1382. engine->fw_domains = fw_domains;
  1383. tasklet_init(&engine->irq_tasklet,
  1384. intel_lrc_irq_handler, (unsigned long)engine);
  1385. logical_ring_default_vfuncs(engine);
  1386. logical_ring_default_irqs(engine);
  1387. }
  1388. static int
  1389. logical_ring_init(struct intel_engine_cs *engine)
  1390. {
  1391. struct i915_gem_context *dctx = engine->i915->kernel_context;
  1392. int ret;
  1393. ret = intel_engine_init_common(engine);
  1394. if (ret)
  1395. goto error;
  1396. /* And setup the hardware status page. */
  1397. ret = lrc_setup_hws(engine, dctx->engine[engine->id].state);
  1398. if (ret) {
  1399. DRM_ERROR("Failed to set up hws %s: %d\n", engine->name, ret);
  1400. goto error;
  1401. }
  1402. return 0;
  1403. error:
  1404. intel_logical_ring_cleanup(engine);
  1405. return ret;
  1406. }
  1407. int logical_render_ring_init(struct intel_engine_cs *engine)
  1408. {
  1409. struct drm_i915_private *dev_priv = engine->i915;
  1410. int ret;
  1411. logical_ring_setup(engine);
  1412. if (HAS_L3_DPF(dev_priv))
  1413. engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1414. /* Override some for render ring. */
  1415. if (INTEL_GEN(dev_priv) >= 9)
  1416. engine->init_hw = gen9_init_render_ring;
  1417. else
  1418. engine->init_hw = gen8_init_render_ring;
  1419. engine->init_context = gen8_init_rcs_context;
  1420. engine->emit_flush = gen8_emit_flush_render;
  1421. engine->emit_breadcrumb = gen8_emit_breadcrumb_render;
  1422. engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_render_sz;
  1423. ret = intel_engine_create_scratch(engine, PAGE_SIZE);
  1424. if (ret)
  1425. return ret;
  1426. ret = intel_init_workaround_bb(engine);
  1427. if (ret) {
  1428. /*
  1429. * We continue even if we fail to initialize WA batch
  1430. * because we only expect rare glitches but nothing
  1431. * critical to prevent us from using GPU
  1432. */
  1433. DRM_ERROR("WA batch buffer initialization failed: %d\n",
  1434. ret);
  1435. }
  1436. return logical_ring_init(engine);
  1437. }
  1438. int logical_xcs_ring_init(struct intel_engine_cs *engine)
  1439. {
  1440. logical_ring_setup(engine);
  1441. return logical_ring_init(engine);
  1442. }
  1443. static u32
  1444. make_rpcs(struct drm_i915_private *dev_priv)
  1445. {
  1446. u32 rpcs = 0;
  1447. /*
  1448. * No explicit RPCS request is needed to ensure full
  1449. * slice/subslice/EU enablement prior to Gen9.
  1450. */
  1451. if (INTEL_GEN(dev_priv) < 9)
  1452. return 0;
  1453. /*
  1454. * Starting in Gen9, render power gating can leave
  1455. * slice/subslice/EU in a partially enabled state. We
  1456. * must make an explicit request through RPCS for full
  1457. * enablement.
  1458. */
  1459. if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
  1460. rpcs |= GEN8_RPCS_S_CNT_ENABLE;
  1461. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
  1462. GEN8_RPCS_S_CNT_SHIFT;
  1463. rpcs |= GEN8_RPCS_ENABLE;
  1464. }
  1465. if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
  1466. rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
  1467. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
  1468. GEN8_RPCS_SS_CNT_SHIFT;
  1469. rpcs |= GEN8_RPCS_ENABLE;
  1470. }
  1471. if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
  1472. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  1473. GEN8_RPCS_EU_MIN_SHIFT;
  1474. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  1475. GEN8_RPCS_EU_MAX_SHIFT;
  1476. rpcs |= GEN8_RPCS_ENABLE;
  1477. }
  1478. return rpcs;
  1479. }
  1480. static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
  1481. {
  1482. u32 indirect_ctx_offset;
  1483. switch (INTEL_GEN(engine->i915)) {
  1484. default:
  1485. MISSING_CASE(INTEL_GEN(engine->i915));
  1486. /* fall through */
  1487. case 9:
  1488. indirect_ctx_offset =
  1489. GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1490. break;
  1491. case 8:
  1492. indirect_ctx_offset =
  1493. GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1494. break;
  1495. }
  1496. return indirect_ctx_offset;
  1497. }
  1498. static void execlists_init_reg_state(u32 *regs,
  1499. struct i915_gem_context *ctx,
  1500. struct intel_engine_cs *engine,
  1501. struct intel_ring *ring)
  1502. {
  1503. struct drm_i915_private *dev_priv = engine->i915;
  1504. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
  1505. u32 base = engine->mmio_base;
  1506. bool rcs = engine->id == RCS;
  1507. /* A context is actually a big batch buffer with several
  1508. * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
  1509. * values we are setting here are only for the first context restore:
  1510. * on a subsequent save, the GPU will recreate this batchbuffer with new
  1511. * values (including all the missing MI_LOAD_REGISTER_IMM commands that
  1512. * we are not initializing here).
  1513. */
  1514. regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
  1515. MI_LRI_FORCE_POSTED;
  1516. CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
  1517. _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
  1518. CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
  1519. (HAS_RESOURCE_STREAMER(dev_priv) ?
  1520. CTX_CTRL_RS_CTX_ENABLE : 0)));
  1521. CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
  1522. CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
  1523. CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
  1524. CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
  1525. RING_CTL_SIZE(ring->size) | RING_VALID);
  1526. CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
  1527. CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
  1528. CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
  1529. CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
  1530. CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
  1531. CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
  1532. if (rcs) {
  1533. CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
  1534. CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
  1535. CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
  1536. RING_INDIRECT_CTX_OFFSET(base), 0);
  1537. if (engine->wa_ctx.vma) {
  1538. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1539. u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
  1540. regs[CTX_RCS_INDIRECT_CTX + 1] =
  1541. (ggtt_offset + wa_ctx->indirect_ctx.offset) |
  1542. (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
  1543. regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
  1544. intel_lr_indirect_ctx_offset(engine) << 6;
  1545. regs[CTX_BB_PER_CTX_PTR + 1] =
  1546. (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
  1547. }
  1548. }
  1549. regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
  1550. CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
  1551. /* PDP values well be assigned later if needed */
  1552. CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
  1553. CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
  1554. CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
  1555. CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
  1556. CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
  1557. CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
  1558. CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
  1559. CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
  1560. if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
  1561. /* 64b PPGTT (48bit canonical)
  1562. * PDP0_DESCRIPTOR contains the base address to PML4 and
  1563. * other PDP Descriptors are ignored.
  1564. */
  1565. ASSIGN_CTX_PML4(ppgtt, regs);
  1566. }
  1567. if (rcs) {
  1568. regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  1569. CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
  1570. make_rpcs(dev_priv));
  1571. }
  1572. }
  1573. static int
  1574. populate_lr_context(struct i915_gem_context *ctx,
  1575. struct drm_i915_gem_object *ctx_obj,
  1576. struct intel_engine_cs *engine,
  1577. struct intel_ring *ring)
  1578. {
  1579. void *vaddr;
  1580. int ret;
  1581. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1582. if (ret) {
  1583. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1584. return ret;
  1585. }
  1586. vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
  1587. if (IS_ERR(vaddr)) {
  1588. ret = PTR_ERR(vaddr);
  1589. DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
  1590. return ret;
  1591. }
  1592. ctx_obj->mm.dirty = true;
  1593. /* The second page of the context object contains some fields which must
  1594. * be set up prior to the first execution. */
  1595. execlists_init_reg_state(vaddr + LRC_STATE_PN * PAGE_SIZE,
  1596. ctx, engine, ring);
  1597. i915_gem_object_unpin_map(ctx_obj);
  1598. return 0;
  1599. }
  1600. /**
  1601. * intel_lr_context_size() - return the size of the context for an engine
  1602. * @engine: which engine to find the context size for
  1603. *
  1604. * Each engine may require a different amount of space for a context image,
  1605. * so when allocating (or copying) an image, this function can be used to
  1606. * find the right size for the specific engine.
  1607. *
  1608. * Return: size (in bytes) of an engine-specific context image
  1609. *
  1610. * Note: this size includes the HWSP, which is part of the context image
  1611. * in LRC mode, but does not include the "shared data page" used with
  1612. * GuC submission. The caller should account for this if using the GuC.
  1613. */
  1614. uint32_t intel_lr_context_size(struct intel_engine_cs *engine)
  1615. {
  1616. int ret = 0;
  1617. WARN_ON(INTEL_GEN(engine->i915) < 8);
  1618. switch (engine->id) {
  1619. case RCS:
  1620. if (INTEL_GEN(engine->i915) >= 9)
  1621. ret = GEN9_LR_CONTEXT_RENDER_SIZE;
  1622. else
  1623. ret = GEN8_LR_CONTEXT_RENDER_SIZE;
  1624. break;
  1625. case VCS:
  1626. case BCS:
  1627. case VECS:
  1628. case VCS2:
  1629. ret = GEN8_LR_CONTEXT_OTHER_SIZE;
  1630. break;
  1631. }
  1632. return ret;
  1633. }
  1634. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  1635. struct intel_engine_cs *engine)
  1636. {
  1637. struct drm_i915_gem_object *ctx_obj;
  1638. struct intel_context *ce = &ctx->engine[engine->id];
  1639. struct i915_vma *vma;
  1640. uint32_t context_size;
  1641. struct intel_ring *ring;
  1642. int ret;
  1643. WARN_ON(ce->state);
  1644. context_size = round_up(intel_lr_context_size(engine),
  1645. I915_GTT_PAGE_SIZE);
  1646. /* One extra page as the sharing data between driver and GuC */
  1647. context_size += PAGE_SIZE * LRC_PPHWSP_PN;
  1648. ctx_obj = i915_gem_object_create(ctx->i915, context_size);
  1649. if (IS_ERR(ctx_obj)) {
  1650. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
  1651. return PTR_ERR(ctx_obj);
  1652. }
  1653. vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
  1654. if (IS_ERR(vma)) {
  1655. ret = PTR_ERR(vma);
  1656. goto error_deref_obj;
  1657. }
  1658. ring = intel_engine_create_ring(engine, ctx->ring_size);
  1659. if (IS_ERR(ring)) {
  1660. ret = PTR_ERR(ring);
  1661. goto error_deref_obj;
  1662. }
  1663. ret = populate_lr_context(ctx, ctx_obj, engine, ring);
  1664. if (ret) {
  1665. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  1666. goto error_ring_free;
  1667. }
  1668. ce->ring = ring;
  1669. ce->state = vma;
  1670. ce->initialised |= engine->init_context == NULL;
  1671. return 0;
  1672. error_ring_free:
  1673. intel_ring_free(ring);
  1674. error_deref_obj:
  1675. i915_gem_object_put(ctx_obj);
  1676. return ret;
  1677. }
  1678. void intel_lr_context_resume(struct drm_i915_private *dev_priv)
  1679. {
  1680. struct intel_engine_cs *engine;
  1681. struct i915_gem_context *ctx;
  1682. enum intel_engine_id id;
  1683. /* Because we emit WA_TAIL_DWORDS there may be a disparity
  1684. * between our bookkeeping in ce->ring->head and ce->ring->tail and
  1685. * that stored in context. As we only write new commands from
  1686. * ce->ring->tail onwards, everything before that is junk. If the GPU
  1687. * starts reading from its RING_HEAD from the context, it may try to
  1688. * execute that junk and die.
  1689. *
  1690. * So to avoid that we reset the context images upon resume. For
  1691. * simplicity, we just zero everything out.
  1692. */
  1693. list_for_each_entry(ctx, &dev_priv->context_list, link) {
  1694. for_each_engine(engine, dev_priv, id) {
  1695. struct intel_context *ce = &ctx->engine[engine->id];
  1696. u32 *reg;
  1697. if (!ce->state)
  1698. continue;
  1699. reg = i915_gem_object_pin_map(ce->state->obj,
  1700. I915_MAP_WB);
  1701. if (WARN_ON(IS_ERR(reg)))
  1702. continue;
  1703. reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
  1704. reg[CTX_RING_HEAD+1] = 0;
  1705. reg[CTX_RING_TAIL+1] = 0;
  1706. ce->state->obj->mm.dirty = true;
  1707. i915_gem_object_unpin_map(ce->state->obj);
  1708. ce->ring->head = ce->ring->tail = 0;
  1709. intel_ring_update_space(ce->ring);
  1710. }
  1711. }
  1712. }