intel_lrc.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365
  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 "i915_gem_render_state.h"
  138. #include "intel_mocs.h"
  139. #define RING_EXECLIST_QFULL (1 << 0x2)
  140. #define RING_EXECLIST1_VALID (1 << 0x3)
  141. #define RING_EXECLIST0_VALID (1 << 0x4)
  142. #define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
  143. #define RING_EXECLIST1_ACTIVE (1 << 0x11)
  144. #define RING_EXECLIST0_ACTIVE (1 << 0x12)
  145. #define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
  146. #define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
  147. #define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
  148. #define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
  149. #define GEN8_CTX_STATUS_COMPLETE (1 << 4)
  150. #define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
  151. #define GEN8_CTX_STATUS_COMPLETED_MASK \
  152. (GEN8_CTX_STATUS_COMPLETE | GEN8_CTX_STATUS_PREEMPTED)
  153. #define CTX_LRI_HEADER_0 0x01
  154. #define CTX_CONTEXT_CONTROL 0x02
  155. #define CTX_RING_HEAD 0x04
  156. #define CTX_RING_TAIL 0x06
  157. #define CTX_RING_BUFFER_START 0x08
  158. #define CTX_RING_BUFFER_CONTROL 0x0a
  159. #define CTX_BB_HEAD_U 0x0c
  160. #define CTX_BB_HEAD_L 0x0e
  161. #define CTX_BB_STATE 0x10
  162. #define CTX_SECOND_BB_HEAD_U 0x12
  163. #define CTX_SECOND_BB_HEAD_L 0x14
  164. #define CTX_SECOND_BB_STATE 0x16
  165. #define CTX_BB_PER_CTX_PTR 0x18
  166. #define CTX_RCS_INDIRECT_CTX 0x1a
  167. #define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
  168. #define CTX_LRI_HEADER_1 0x21
  169. #define CTX_CTX_TIMESTAMP 0x22
  170. #define CTX_PDP3_UDW 0x24
  171. #define CTX_PDP3_LDW 0x26
  172. #define CTX_PDP2_UDW 0x28
  173. #define CTX_PDP2_LDW 0x2a
  174. #define CTX_PDP1_UDW 0x2c
  175. #define CTX_PDP1_LDW 0x2e
  176. #define CTX_PDP0_UDW 0x30
  177. #define CTX_PDP0_LDW 0x32
  178. #define CTX_LRI_HEADER_2 0x41
  179. #define CTX_R_PWR_CLK_STATE 0x42
  180. #define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
  181. #define CTX_REG(reg_state, pos, reg, val) do { \
  182. (reg_state)[(pos)+0] = i915_mmio_reg_offset(reg); \
  183. (reg_state)[(pos)+1] = (val); \
  184. } while (0)
  185. #define ASSIGN_CTX_PDP(ppgtt, reg_state, n) do { \
  186. const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
  187. reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
  188. reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
  189. } while (0)
  190. #define ASSIGN_CTX_PML4(ppgtt, reg_state) do { \
  191. reg_state[CTX_PDP0_UDW + 1] = upper_32_bits(px_dma(&ppgtt->pml4)); \
  192. reg_state[CTX_PDP0_LDW + 1] = lower_32_bits(px_dma(&ppgtt->pml4)); \
  193. } while (0)
  194. #define GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
  195. #define GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x26
  196. #define GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x19
  197. /* Typical size of the average request (2 pipecontrols and a MI_BB) */
  198. #define EXECLISTS_REQUEST_SIZE 64 /* bytes */
  199. #define WA_TAIL_DWORDS 2
  200. #define WA_TAIL_BYTES (sizeof(u32) * WA_TAIL_DWORDS)
  201. #define PREEMPT_ID 0x1
  202. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  203. struct intel_engine_cs *engine);
  204. static void execlists_init_reg_state(u32 *reg_state,
  205. struct i915_gem_context *ctx,
  206. struct intel_engine_cs *engine,
  207. struct intel_ring *ring);
  208. /**
  209. * intel_lr_context_descriptor_update() - calculate & cache the descriptor
  210. * descriptor for a pinned context
  211. * @ctx: Context to work on
  212. * @engine: Engine the descriptor will be used with
  213. *
  214. * The context descriptor encodes various attributes of a context,
  215. * including its GTT address and some flags. Because it's fairly
  216. * expensive to calculate, we'll just do it once and cache the result,
  217. * which remains valid until the context is unpinned.
  218. *
  219. * This is what a descriptor looks like, from LSB to MSB::
  220. *
  221. * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
  222. * bits 12-31: LRCA, GTT address of (the HWSP of) this context
  223. * bits 32-52: ctx ID, a globally unique tag
  224. * bits 53-54: mbz, reserved for use by hardware
  225. * bits 55-63: group ID, currently unused and set to 0
  226. */
  227. static void
  228. intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
  229. struct intel_engine_cs *engine)
  230. {
  231. struct intel_context *ce = &ctx->engine[engine->id];
  232. u64 desc;
  233. BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (1<<GEN8_CTX_ID_WIDTH));
  234. desc = ctx->desc_template; /* bits 0-11 */
  235. desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
  236. /* bits 12-31 */
  237. desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
  238. ce->lrc_desc = desc;
  239. }
  240. static struct i915_priolist *
  241. lookup_priolist(struct intel_engine_cs *engine,
  242. struct i915_priotree *pt,
  243. int prio)
  244. {
  245. struct intel_engine_execlists * const execlists = &engine->execlists;
  246. struct i915_priolist *p;
  247. struct rb_node **parent, *rb;
  248. bool first = true;
  249. if (unlikely(execlists->no_priolist))
  250. prio = I915_PRIORITY_NORMAL;
  251. find_priolist:
  252. /* most positive priority is scheduled first, equal priorities fifo */
  253. rb = NULL;
  254. parent = &execlists->queue.rb_node;
  255. while (*parent) {
  256. rb = *parent;
  257. p = rb_entry(rb, typeof(*p), node);
  258. if (prio > p->priority) {
  259. parent = &rb->rb_left;
  260. } else if (prio < p->priority) {
  261. parent = &rb->rb_right;
  262. first = false;
  263. } else {
  264. return p;
  265. }
  266. }
  267. if (prio == I915_PRIORITY_NORMAL) {
  268. p = &execlists->default_priolist;
  269. } else {
  270. p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
  271. /* Convert an allocation failure to a priority bump */
  272. if (unlikely(!p)) {
  273. prio = I915_PRIORITY_NORMAL; /* recurses just once */
  274. /* To maintain ordering with all rendering, after an
  275. * allocation failure we have to disable all scheduling.
  276. * Requests will then be executed in fifo, and schedule
  277. * will ensure that dependencies are emitted in fifo.
  278. * There will be still some reordering with existing
  279. * requests, so if userspace lied about their
  280. * dependencies that reordering may be visible.
  281. */
  282. execlists->no_priolist = true;
  283. goto find_priolist;
  284. }
  285. }
  286. p->priority = prio;
  287. INIT_LIST_HEAD(&p->requests);
  288. rb_link_node(&p->node, rb, parent);
  289. rb_insert_color(&p->node, &execlists->queue);
  290. if (first)
  291. execlists->first = &p->node;
  292. return ptr_pack_bits(p, first, 1);
  293. }
  294. static void unwind_wa_tail(struct drm_i915_gem_request *rq)
  295. {
  296. rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES);
  297. assert_ring_tail_valid(rq->ring, rq->tail);
  298. }
  299. static void __unwind_incomplete_requests(struct intel_engine_cs *engine)
  300. {
  301. struct drm_i915_gem_request *rq, *rn;
  302. struct i915_priolist *uninitialized_var(p);
  303. int last_prio = I915_PRIORITY_INVALID;
  304. lockdep_assert_held(&engine->timeline->lock);
  305. list_for_each_entry_safe_reverse(rq, rn,
  306. &engine->timeline->requests,
  307. link) {
  308. if (i915_gem_request_completed(rq))
  309. return;
  310. __i915_gem_request_unsubmit(rq);
  311. unwind_wa_tail(rq);
  312. GEM_BUG_ON(rq->priotree.priority == I915_PRIORITY_INVALID);
  313. if (rq->priotree.priority != last_prio) {
  314. p = lookup_priolist(engine,
  315. &rq->priotree,
  316. rq->priotree.priority);
  317. p = ptr_mask_bits(p, 1);
  318. last_prio = rq->priotree.priority;
  319. }
  320. list_add(&rq->priotree.link, &p->requests);
  321. }
  322. }
  323. void
  324. execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists)
  325. {
  326. struct intel_engine_cs *engine =
  327. container_of(execlists, typeof(*engine), execlists);
  328. spin_lock_irq(&engine->timeline->lock);
  329. __unwind_incomplete_requests(engine);
  330. spin_unlock_irq(&engine->timeline->lock);
  331. }
  332. static inline void
  333. execlists_context_status_change(struct drm_i915_gem_request *rq,
  334. unsigned long status)
  335. {
  336. /*
  337. * Only used when GVT-g is enabled now. When GVT-g is disabled,
  338. * The compiler should eliminate this function as dead-code.
  339. */
  340. if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
  341. return;
  342. atomic_notifier_call_chain(&rq->engine->context_status_notifier,
  343. status, rq);
  344. }
  345. static inline void
  346. execlists_context_schedule_in(struct drm_i915_gem_request *rq)
  347. {
  348. execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
  349. intel_engine_context_in(rq->engine);
  350. }
  351. static inline void
  352. execlists_context_schedule_out(struct drm_i915_gem_request *rq)
  353. {
  354. intel_engine_context_out(rq->engine);
  355. execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_OUT);
  356. }
  357. static void
  358. execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
  359. {
  360. ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
  361. ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
  362. ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
  363. ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
  364. }
  365. static u64 execlists_update_context(struct drm_i915_gem_request *rq)
  366. {
  367. struct intel_context *ce = &rq->ctx->engine[rq->engine->id];
  368. struct i915_hw_ppgtt *ppgtt =
  369. rq->ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
  370. u32 *reg_state = ce->lrc_reg_state;
  371. reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
  372. /* True 32b PPGTT with dynamic page allocation: update PDP
  373. * registers and point the unallocated PDPs to scratch page.
  374. * PML4 is allocated during ppgtt init, so this is not needed
  375. * in 48-bit mode.
  376. */
  377. if (ppgtt && !i915_vm_is_48bit(&ppgtt->base))
  378. execlists_update_context_pdps(ppgtt, reg_state);
  379. return ce->lrc_desc;
  380. }
  381. static inline void elsp_write(u64 desc, u32 __iomem *elsp)
  382. {
  383. writel(upper_32_bits(desc), elsp);
  384. writel(lower_32_bits(desc), elsp);
  385. }
  386. static void execlists_submit_ports(struct intel_engine_cs *engine)
  387. {
  388. struct execlist_port *port = engine->execlists.port;
  389. unsigned int n;
  390. for (n = execlists_num_ports(&engine->execlists); n--; ) {
  391. struct drm_i915_gem_request *rq;
  392. unsigned int count;
  393. u64 desc;
  394. rq = port_unpack(&port[n], &count);
  395. if (rq) {
  396. GEM_BUG_ON(count > !n);
  397. if (!count++)
  398. execlists_context_schedule_in(rq);
  399. port_set(&port[n], port_pack(rq, count));
  400. desc = execlists_update_context(rq);
  401. GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
  402. GEM_TRACE("%s in[%d]: ctx=%d.%d, seqno=%x\n",
  403. engine->name, n,
  404. port[n].context_id, count,
  405. rq->global_seqno);
  406. } else {
  407. GEM_BUG_ON(!n);
  408. desc = 0;
  409. }
  410. elsp_write(desc, engine->execlists.elsp);
  411. }
  412. execlists_clear_active(&engine->execlists, EXECLISTS_ACTIVE_HWACK);
  413. }
  414. static bool ctx_single_port_submission(const struct i915_gem_context *ctx)
  415. {
  416. return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
  417. i915_gem_context_force_single_submission(ctx));
  418. }
  419. static bool can_merge_ctx(const struct i915_gem_context *prev,
  420. const struct i915_gem_context *next)
  421. {
  422. if (prev != next)
  423. return false;
  424. if (ctx_single_port_submission(prev))
  425. return false;
  426. return true;
  427. }
  428. static void port_assign(struct execlist_port *port,
  429. struct drm_i915_gem_request *rq)
  430. {
  431. GEM_BUG_ON(rq == port_request(port));
  432. if (port_isset(port))
  433. i915_gem_request_put(port_request(port));
  434. port_set(port, port_pack(i915_gem_request_get(rq), port_count(port)));
  435. }
  436. static void inject_preempt_context(struct intel_engine_cs *engine)
  437. {
  438. struct intel_context *ce =
  439. &engine->i915->preempt_context->engine[engine->id];
  440. unsigned int n;
  441. GEM_BUG_ON(engine->i915->preempt_context->hw_id != PREEMPT_ID);
  442. GEM_BUG_ON(!IS_ALIGNED(ce->ring->size, WA_TAIL_BYTES));
  443. memset(ce->ring->vaddr + ce->ring->tail, 0, WA_TAIL_BYTES);
  444. ce->ring->tail += WA_TAIL_BYTES;
  445. ce->ring->tail &= (ce->ring->size - 1);
  446. ce->lrc_reg_state[CTX_RING_TAIL+1] = ce->ring->tail;
  447. GEM_TRACE("%s\n", engine->name);
  448. for (n = execlists_num_ports(&engine->execlists); --n; )
  449. elsp_write(0, engine->execlists.elsp);
  450. elsp_write(ce->lrc_desc, engine->execlists.elsp);
  451. execlists_clear_active(&engine->execlists, EXECLISTS_ACTIVE_HWACK);
  452. }
  453. static void execlists_dequeue(struct intel_engine_cs *engine)
  454. {
  455. struct intel_engine_execlists * const execlists = &engine->execlists;
  456. struct execlist_port *port = execlists->port;
  457. const struct execlist_port * const last_port =
  458. &execlists->port[execlists->port_mask];
  459. struct drm_i915_gem_request *last = port_request(port);
  460. struct rb_node *rb;
  461. bool submit = false;
  462. /* Hardware submission is through 2 ports. Conceptually each port
  463. * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
  464. * static for a context, and unique to each, so we only execute
  465. * requests belonging to a single context from each ring. RING_HEAD
  466. * is maintained by the CS in the context image, it marks the place
  467. * where it got up to last time, and through RING_TAIL we tell the CS
  468. * where we want to execute up to this time.
  469. *
  470. * In this list the requests are in order of execution. Consecutive
  471. * requests from the same context are adjacent in the ringbuffer. We
  472. * can combine these requests into a single RING_TAIL update:
  473. *
  474. * RING_HEAD...req1...req2
  475. * ^- RING_TAIL
  476. * since to execute req2 the CS must first execute req1.
  477. *
  478. * Our goal then is to point each port to the end of a consecutive
  479. * sequence of requests as being the most optimal (fewest wake ups
  480. * and context switches) submission.
  481. */
  482. spin_lock_irq(&engine->timeline->lock);
  483. rb = execlists->first;
  484. GEM_BUG_ON(rb_first(&execlists->queue) != rb);
  485. if (!rb)
  486. goto unlock;
  487. if (last) {
  488. /*
  489. * Don't resubmit or switch until all outstanding
  490. * preemptions (lite-restore) are seen. Then we
  491. * know the next preemption status we see corresponds
  492. * to this ELSP update.
  493. */
  494. GEM_BUG_ON(!port_count(&port[0]));
  495. if (port_count(&port[0]) > 1)
  496. goto unlock;
  497. /*
  498. * If we write to ELSP a second time before the HW has had
  499. * a chance to respond to the previous write, we can confuse
  500. * the HW and hit "undefined behaviour". After writing to ELSP,
  501. * we must then wait until we see a context-switch event from
  502. * the HW to indicate that it has had a chance to respond.
  503. */
  504. if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_HWACK))
  505. goto unlock;
  506. if (HAS_LOGICAL_RING_PREEMPTION(engine->i915) &&
  507. rb_entry(rb, struct i915_priolist, node)->priority >
  508. max(last->priotree.priority, 0)) {
  509. /*
  510. * Switch to our empty preempt context so
  511. * the state of the GPU is known (idle).
  512. */
  513. inject_preempt_context(engine);
  514. execlists_set_active(execlists,
  515. EXECLISTS_ACTIVE_PREEMPT);
  516. goto unlock;
  517. } else {
  518. /*
  519. * In theory, we could coalesce more requests onto
  520. * the second port (the first port is active, with
  521. * no preemptions pending). However, that means we
  522. * then have to deal with the possible lite-restore
  523. * of the second port (as we submit the ELSP, there
  524. * may be a context-switch) but also we may complete
  525. * the resubmission before the context-switch. Ergo,
  526. * coalescing onto the second port will cause a
  527. * preemption event, but we cannot predict whether
  528. * that will affect port[0] or port[1].
  529. *
  530. * If the second port is already active, we can wait
  531. * until the next context-switch before contemplating
  532. * new requests. The GPU will be busy and we should be
  533. * able to resubmit the new ELSP before it idles,
  534. * avoiding pipeline bubbles (momentary pauses where
  535. * the driver is unable to keep up the supply of new
  536. * work).
  537. */
  538. if (port_count(&port[1]))
  539. goto unlock;
  540. /* WaIdleLiteRestore:bdw,skl
  541. * Apply the wa NOOPs to prevent
  542. * ring:HEAD == req:TAIL as we resubmit the
  543. * request. See gen8_emit_breadcrumb() for
  544. * where we prepare the padding after the
  545. * end of the request.
  546. */
  547. last->tail = last->wa_tail;
  548. }
  549. }
  550. do {
  551. struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
  552. struct drm_i915_gem_request *rq, *rn;
  553. list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
  554. /*
  555. * Can we combine this request with the current port?
  556. * It has to be the same context/ringbuffer and not
  557. * have any exceptions (e.g. GVT saying never to
  558. * combine contexts).
  559. *
  560. * If we can combine the requests, we can execute both
  561. * by updating the RING_TAIL to point to the end of the
  562. * second request, and so we never need to tell the
  563. * hardware about the first.
  564. */
  565. if (last && !can_merge_ctx(rq->ctx, last->ctx)) {
  566. /*
  567. * If we are on the second port and cannot
  568. * combine this request with the last, then we
  569. * are done.
  570. */
  571. if (port == last_port) {
  572. __list_del_many(&p->requests,
  573. &rq->priotree.link);
  574. goto done;
  575. }
  576. /*
  577. * If GVT overrides us we only ever submit
  578. * port[0], leaving port[1] empty. Note that we
  579. * also have to be careful that we don't queue
  580. * the same context (even though a different
  581. * request) to the second port.
  582. */
  583. if (ctx_single_port_submission(last->ctx) ||
  584. ctx_single_port_submission(rq->ctx)) {
  585. __list_del_many(&p->requests,
  586. &rq->priotree.link);
  587. goto done;
  588. }
  589. GEM_BUG_ON(last->ctx == rq->ctx);
  590. if (submit)
  591. port_assign(port, last);
  592. port++;
  593. GEM_BUG_ON(port_isset(port));
  594. }
  595. INIT_LIST_HEAD(&rq->priotree.link);
  596. __i915_gem_request_submit(rq);
  597. trace_i915_gem_request_in(rq, port_index(port, execlists));
  598. last = rq;
  599. submit = true;
  600. }
  601. rb = rb_next(rb);
  602. rb_erase(&p->node, &execlists->queue);
  603. INIT_LIST_HEAD(&p->requests);
  604. if (p->priority != I915_PRIORITY_NORMAL)
  605. kmem_cache_free(engine->i915->priorities, p);
  606. } while (rb);
  607. done:
  608. execlists->first = rb;
  609. if (submit)
  610. port_assign(port, last);
  611. unlock:
  612. spin_unlock_irq(&engine->timeline->lock);
  613. if (submit) {
  614. execlists_set_active(execlists, EXECLISTS_ACTIVE_USER);
  615. execlists_submit_ports(engine);
  616. }
  617. }
  618. void
  619. execlists_cancel_port_requests(struct intel_engine_execlists * const execlists)
  620. {
  621. struct execlist_port *port = execlists->port;
  622. unsigned int num_ports = execlists_num_ports(execlists);
  623. while (num_ports-- && port_isset(port)) {
  624. struct drm_i915_gem_request *rq = port_request(port);
  625. GEM_BUG_ON(!execlists->active);
  626. intel_engine_context_out(rq->engine);
  627. execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_PREEMPTED);
  628. i915_gem_request_put(rq);
  629. memset(port, 0, sizeof(*port));
  630. port++;
  631. }
  632. }
  633. static void execlists_cancel_requests(struct intel_engine_cs *engine)
  634. {
  635. struct intel_engine_execlists * const execlists = &engine->execlists;
  636. struct drm_i915_gem_request *rq, *rn;
  637. struct rb_node *rb;
  638. unsigned long flags;
  639. spin_lock_irqsave(&engine->timeline->lock, flags);
  640. /* Cancel the requests on the HW and clear the ELSP tracker. */
  641. execlists_cancel_port_requests(execlists);
  642. /* Mark all executing requests as skipped. */
  643. list_for_each_entry(rq, &engine->timeline->requests, link) {
  644. GEM_BUG_ON(!rq->global_seqno);
  645. if (!i915_gem_request_completed(rq))
  646. dma_fence_set_error(&rq->fence, -EIO);
  647. }
  648. /* Flush the queued requests to the timeline list (for retiring). */
  649. rb = execlists->first;
  650. while (rb) {
  651. struct i915_priolist *p = rb_entry(rb, typeof(*p), node);
  652. list_for_each_entry_safe(rq, rn, &p->requests, priotree.link) {
  653. INIT_LIST_HEAD(&rq->priotree.link);
  654. dma_fence_set_error(&rq->fence, -EIO);
  655. __i915_gem_request_submit(rq);
  656. }
  657. rb = rb_next(rb);
  658. rb_erase(&p->node, &execlists->queue);
  659. INIT_LIST_HEAD(&p->requests);
  660. if (p->priority != I915_PRIORITY_NORMAL)
  661. kmem_cache_free(engine->i915->priorities, p);
  662. }
  663. /* Remaining _unready_ requests will be nop'ed when submitted */
  664. execlists->queue = RB_ROOT;
  665. execlists->first = NULL;
  666. GEM_BUG_ON(port_isset(execlists->port));
  667. /*
  668. * The port is checked prior to scheduling a tasklet, but
  669. * just in case we have suspended the tasklet to do the
  670. * wedging make sure that when it wakes, it decides there
  671. * is no work to do by clearing the irq_posted bit.
  672. */
  673. clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
  674. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  675. }
  676. /*
  677. * Check the unread Context Status Buffers and manage the submission of new
  678. * contexts to the ELSP accordingly.
  679. */
  680. static void execlists_submission_tasklet(unsigned long data)
  681. {
  682. struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
  683. struct intel_engine_execlists * const execlists = &engine->execlists;
  684. struct execlist_port * const port = execlists->port;
  685. struct drm_i915_private *dev_priv = engine->i915;
  686. /* We can skip acquiring intel_runtime_pm_get() here as it was taken
  687. * on our behalf by the request (see i915_gem_mark_busy()) and it will
  688. * not be relinquished until the device is idle (see
  689. * i915_gem_idle_work_handler()). As a precaution, we make sure
  690. * that all ELSP are drained i.e. we have processed the CSB,
  691. * before allowing ourselves to idle and calling intel_runtime_pm_put().
  692. */
  693. GEM_BUG_ON(!dev_priv->gt.awake);
  694. intel_uncore_forcewake_get(dev_priv, execlists->fw_domains);
  695. /* Prefer doing test_and_clear_bit() as a two stage operation to avoid
  696. * imposing the cost of a locked atomic transaction when submitting a
  697. * new request (outside of the context-switch interrupt).
  698. */
  699. while (test_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted)) {
  700. /* The HWSP contains a (cacheable) mirror of the CSB */
  701. const u32 *buf =
  702. &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
  703. unsigned int head, tail;
  704. if (unlikely(execlists->csb_use_mmio)) {
  705. buf = (u32 * __force)
  706. (dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
  707. execlists->csb_head = -1; /* force mmio read of CSB ptrs */
  708. }
  709. /* The write will be ordered by the uncached read (itself
  710. * a memory barrier), so we do not need another in the form
  711. * of a locked instruction. The race between the interrupt
  712. * handler and the split test/clear is harmless as we order
  713. * our clear before the CSB read. If the interrupt arrived
  714. * first between the test and the clear, we read the updated
  715. * CSB and clear the bit. If the interrupt arrives as we read
  716. * the CSB or later (i.e. after we had cleared the bit) the bit
  717. * is set and we do a new loop.
  718. */
  719. __clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
  720. if (unlikely(execlists->csb_head == -1)) { /* following a reset */
  721. head = readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
  722. tail = GEN8_CSB_WRITE_PTR(head);
  723. head = GEN8_CSB_READ_PTR(head);
  724. execlists->csb_head = head;
  725. } else {
  726. const int write_idx =
  727. intel_hws_csb_write_index(dev_priv) -
  728. I915_HWS_CSB_BUF0_INDEX;
  729. head = execlists->csb_head;
  730. tail = READ_ONCE(buf[write_idx]);
  731. }
  732. GEM_TRACE("%s cs-irq head=%d [%d], tail=%d [%d]\n",
  733. engine->name,
  734. head, GEN8_CSB_READ_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))),
  735. tail, GEN8_CSB_WRITE_PTR(readl(dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)))));
  736. while (head != tail) {
  737. struct drm_i915_gem_request *rq;
  738. unsigned int status;
  739. unsigned int count;
  740. if (++head == GEN8_CSB_ENTRIES)
  741. head = 0;
  742. /* We are flying near dragons again.
  743. *
  744. * We hold a reference to the request in execlist_port[]
  745. * but no more than that. We are operating in softirq
  746. * context and so cannot hold any mutex or sleep. That
  747. * prevents us stopping the requests we are processing
  748. * in port[] from being retired simultaneously (the
  749. * breadcrumb will be complete before we see the
  750. * context-switch). As we only hold the reference to the
  751. * request, any pointer chasing underneath the request
  752. * is subject to a potential use-after-free. Thus we
  753. * store all of the bookkeeping within port[] as
  754. * required, and avoid using unguarded pointers beneath
  755. * request itself. The same applies to the atomic
  756. * status notifier.
  757. */
  758. status = READ_ONCE(buf[2 * head]); /* maybe mmio! */
  759. GEM_TRACE("%s csb[%d]: status=0x%08x:0x%08x, active=0x%x\n",
  760. engine->name, head,
  761. status, buf[2*head + 1],
  762. execlists->active);
  763. if (status & (GEN8_CTX_STATUS_IDLE_ACTIVE |
  764. GEN8_CTX_STATUS_PREEMPTED))
  765. execlists_set_active(execlists,
  766. EXECLISTS_ACTIVE_HWACK);
  767. if (status & GEN8_CTX_STATUS_ACTIVE_IDLE)
  768. execlists_clear_active(execlists,
  769. EXECLISTS_ACTIVE_HWACK);
  770. if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
  771. continue;
  772. /* We should never get a COMPLETED | IDLE_ACTIVE! */
  773. GEM_BUG_ON(status & GEN8_CTX_STATUS_IDLE_ACTIVE);
  774. if (status & GEN8_CTX_STATUS_COMPLETE &&
  775. buf[2*head + 1] == PREEMPT_ID) {
  776. GEM_TRACE("%s preempt-idle\n", engine->name);
  777. execlists_cancel_port_requests(execlists);
  778. execlists_unwind_incomplete_requests(execlists);
  779. GEM_BUG_ON(!execlists_is_active(execlists,
  780. EXECLISTS_ACTIVE_PREEMPT));
  781. execlists_clear_active(execlists,
  782. EXECLISTS_ACTIVE_PREEMPT);
  783. continue;
  784. }
  785. if (status & GEN8_CTX_STATUS_PREEMPTED &&
  786. execlists_is_active(execlists,
  787. EXECLISTS_ACTIVE_PREEMPT))
  788. continue;
  789. GEM_BUG_ON(!execlists_is_active(execlists,
  790. EXECLISTS_ACTIVE_USER));
  791. /* Check the context/desc id for this event matches */
  792. GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
  793. rq = port_unpack(port, &count);
  794. GEM_TRACE("%s out[0]: ctx=%d.%d, seqno=%x\n",
  795. engine->name,
  796. port->context_id, count,
  797. rq ? rq->global_seqno : 0);
  798. GEM_BUG_ON(count == 0);
  799. if (--count == 0) {
  800. GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
  801. GEM_BUG_ON(port_isset(&port[1]) &&
  802. !(status & GEN8_CTX_STATUS_ELEMENT_SWITCH));
  803. GEM_BUG_ON(!i915_gem_request_completed(rq));
  804. execlists_context_schedule_out(rq);
  805. trace_i915_gem_request_out(rq);
  806. i915_gem_request_put(rq);
  807. execlists_port_complete(execlists, port);
  808. } else {
  809. port_set(port, port_pack(rq, count));
  810. }
  811. /* After the final element, the hw should be idle */
  812. GEM_BUG_ON(port_count(port) == 0 &&
  813. !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
  814. if (port_count(port) == 0)
  815. execlists_clear_active(execlists,
  816. EXECLISTS_ACTIVE_USER);
  817. }
  818. if (head != execlists->csb_head) {
  819. execlists->csb_head = head;
  820. writel(_MASKED_FIELD(GEN8_CSB_READ_PTR_MASK, head << 8),
  821. dev_priv->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine)));
  822. }
  823. }
  824. if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT))
  825. execlists_dequeue(engine);
  826. intel_uncore_forcewake_put(dev_priv, execlists->fw_domains);
  827. }
  828. static void insert_request(struct intel_engine_cs *engine,
  829. struct i915_priotree *pt,
  830. int prio)
  831. {
  832. struct i915_priolist *p = lookup_priolist(engine, pt, prio);
  833. list_add_tail(&pt->link, &ptr_mask_bits(p, 1)->requests);
  834. if (ptr_unmask_bits(p, 1))
  835. tasklet_hi_schedule(&engine->execlists.tasklet);
  836. }
  837. static void execlists_submit_request(struct drm_i915_gem_request *request)
  838. {
  839. struct intel_engine_cs *engine = request->engine;
  840. unsigned long flags;
  841. /* Will be called from irq-context when using foreign fences. */
  842. spin_lock_irqsave(&engine->timeline->lock, flags);
  843. insert_request(engine, &request->priotree, request->priotree.priority);
  844. GEM_BUG_ON(!engine->execlists.first);
  845. GEM_BUG_ON(list_empty(&request->priotree.link));
  846. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  847. }
  848. static struct drm_i915_gem_request *pt_to_request(struct i915_priotree *pt)
  849. {
  850. return container_of(pt, struct drm_i915_gem_request, priotree);
  851. }
  852. static struct intel_engine_cs *
  853. pt_lock_engine(struct i915_priotree *pt, struct intel_engine_cs *locked)
  854. {
  855. struct intel_engine_cs *engine = pt_to_request(pt)->engine;
  856. GEM_BUG_ON(!locked);
  857. if (engine != locked) {
  858. spin_unlock(&locked->timeline->lock);
  859. spin_lock(&engine->timeline->lock);
  860. }
  861. return engine;
  862. }
  863. static void execlists_schedule(struct drm_i915_gem_request *request, int prio)
  864. {
  865. struct intel_engine_cs *engine;
  866. struct i915_dependency *dep, *p;
  867. struct i915_dependency stack;
  868. LIST_HEAD(dfs);
  869. GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
  870. if (i915_gem_request_completed(request))
  871. return;
  872. if (prio <= READ_ONCE(request->priotree.priority))
  873. return;
  874. /* Need BKL in order to use the temporary link inside i915_dependency */
  875. lockdep_assert_held(&request->i915->drm.struct_mutex);
  876. stack.signaler = &request->priotree;
  877. list_add(&stack.dfs_link, &dfs);
  878. /* Recursively bump all dependent priorities to match the new request.
  879. *
  880. * A naive approach would be to use recursion:
  881. * static void update_priorities(struct i915_priotree *pt, prio) {
  882. * list_for_each_entry(dep, &pt->signalers_list, signal_link)
  883. * update_priorities(dep->signal, prio)
  884. * insert_request(pt);
  885. * }
  886. * but that may have unlimited recursion depth and so runs a very
  887. * real risk of overunning the kernel stack. Instead, we build
  888. * a flat list of all dependencies starting with the current request.
  889. * As we walk the list of dependencies, we add all of its dependencies
  890. * to the end of the list (this may include an already visited
  891. * request) and continue to walk onwards onto the new dependencies. The
  892. * end result is a topological list of requests in reverse order, the
  893. * last element in the list is the request we must execute first.
  894. */
  895. list_for_each_entry_safe(dep, p, &dfs, dfs_link) {
  896. struct i915_priotree *pt = dep->signaler;
  897. /* Within an engine, there can be no cycle, but we may
  898. * refer to the same dependency chain multiple times
  899. * (redundant dependencies are not eliminated) and across
  900. * engines.
  901. */
  902. list_for_each_entry(p, &pt->signalers_list, signal_link) {
  903. if (i915_gem_request_completed(pt_to_request(p->signaler)))
  904. continue;
  905. GEM_BUG_ON(p->signaler->priority < pt->priority);
  906. if (prio > READ_ONCE(p->signaler->priority))
  907. list_move_tail(&p->dfs_link, &dfs);
  908. }
  909. list_safe_reset_next(dep, p, dfs_link);
  910. }
  911. /* If we didn't need to bump any existing priorities, and we haven't
  912. * yet submitted this request (i.e. there is no potential race with
  913. * execlists_submit_request()), we can set our own priority and skip
  914. * acquiring the engine locks.
  915. */
  916. if (request->priotree.priority == I915_PRIORITY_INVALID) {
  917. GEM_BUG_ON(!list_empty(&request->priotree.link));
  918. request->priotree.priority = prio;
  919. if (stack.dfs_link.next == stack.dfs_link.prev)
  920. return;
  921. __list_del_entry(&stack.dfs_link);
  922. }
  923. engine = request->engine;
  924. spin_lock_irq(&engine->timeline->lock);
  925. /* Fifo and depth-first replacement ensure our deps execute before us */
  926. list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
  927. struct i915_priotree *pt = dep->signaler;
  928. INIT_LIST_HEAD(&dep->dfs_link);
  929. engine = pt_lock_engine(pt, engine);
  930. if (prio <= pt->priority)
  931. continue;
  932. pt->priority = prio;
  933. if (!list_empty(&pt->link)) {
  934. __list_del_entry(&pt->link);
  935. insert_request(engine, pt, prio);
  936. }
  937. }
  938. spin_unlock_irq(&engine->timeline->lock);
  939. }
  940. static int __context_pin(struct i915_gem_context *ctx, struct i915_vma *vma)
  941. {
  942. unsigned int flags;
  943. int err;
  944. /*
  945. * Clear this page out of any CPU caches for coherent swap-in/out.
  946. * We only want to do this on the first bind so that we do not stall
  947. * on an active context (which by nature is already on the GPU).
  948. */
  949. if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
  950. err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
  951. if (err)
  952. return err;
  953. }
  954. flags = PIN_GLOBAL | PIN_HIGH;
  955. if (ctx->ggtt_offset_bias)
  956. flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
  957. return i915_vma_pin(vma, 0, GEN8_LR_CONTEXT_ALIGN, flags);
  958. }
  959. static struct intel_ring *
  960. execlists_context_pin(struct intel_engine_cs *engine,
  961. struct i915_gem_context *ctx)
  962. {
  963. struct intel_context *ce = &ctx->engine[engine->id];
  964. void *vaddr;
  965. int ret;
  966. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  967. if (likely(ce->pin_count++))
  968. goto out;
  969. GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
  970. if (!ce->state) {
  971. ret = execlists_context_deferred_alloc(ctx, engine);
  972. if (ret)
  973. goto err;
  974. }
  975. GEM_BUG_ON(!ce->state);
  976. ret = __context_pin(ctx, ce->state);
  977. if (ret)
  978. goto err;
  979. vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
  980. if (IS_ERR(vaddr)) {
  981. ret = PTR_ERR(vaddr);
  982. goto unpin_vma;
  983. }
  984. ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
  985. if (ret)
  986. goto unpin_map;
  987. intel_lr_context_descriptor_update(ctx, engine);
  988. ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  989. ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
  990. i915_ggtt_offset(ce->ring->vma);
  991. ce->state->obj->pin_global++;
  992. i915_gem_context_get(ctx);
  993. out:
  994. return ce->ring;
  995. unpin_map:
  996. i915_gem_object_unpin_map(ce->state->obj);
  997. unpin_vma:
  998. __i915_vma_unpin(ce->state);
  999. err:
  1000. ce->pin_count = 0;
  1001. return ERR_PTR(ret);
  1002. }
  1003. static void execlists_context_unpin(struct intel_engine_cs *engine,
  1004. struct i915_gem_context *ctx)
  1005. {
  1006. struct intel_context *ce = &ctx->engine[engine->id];
  1007. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  1008. GEM_BUG_ON(ce->pin_count == 0);
  1009. if (--ce->pin_count)
  1010. return;
  1011. intel_ring_unpin(ce->ring);
  1012. ce->state->obj->pin_global--;
  1013. i915_gem_object_unpin_map(ce->state->obj);
  1014. i915_vma_unpin(ce->state);
  1015. i915_gem_context_put(ctx);
  1016. }
  1017. static int execlists_request_alloc(struct drm_i915_gem_request *request)
  1018. {
  1019. struct intel_engine_cs *engine = request->engine;
  1020. struct intel_context *ce = &request->ctx->engine[engine->id];
  1021. int ret;
  1022. GEM_BUG_ON(!ce->pin_count);
  1023. /* Flush enough space to reduce the likelihood of waiting after
  1024. * we start building the request - in which case we will just
  1025. * have to repeat work.
  1026. */
  1027. request->reserved_space += EXECLISTS_REQUEST_SIZE;
  1028. ret = intel_ring_wait_for_space(request->ring, request->reserved_space);
  1029. if (ret)
  1030. return ret;
  1031. /* Note that after this point, we have committed to using
  1032. * this request as it is being used to both track the
  1033. * state of engine initialisation and liveness of the
  1034. * golden renderstate above. Think twice before you try
  1035. * to cancel/unwind this request now.
  1036. */
  1037. request->reserved_space -= EXECLISTS_REQUEST_SIZE;
  1038. return 0;
  1039. }
  1040. /*
  1041. * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
  1042. * PIPE_CONTROL instruction. This is required for the flush to happen correctly
  1043. * but there is a slight complication as this is applied in WA batch where the
  1044. * values are only initialized once so we cannot take register value at the
  1045. * beginning and reuse it further; hence we save its value to memory, upload a
  1046. * constant value with bit21 set and then we restore it back with the saved value.
  1047. * To simplify the WA, a constant value is formed by using the default value
  1048. * of this register. This shouldn't be a problem because we are only modifying
  1049. * it for a short period and this batch in non-premptible. We can ofcourse
  1050. * use additional instructions that read the actual value of the register
  1051. * at that time and set our bit of interest but it makes the WA complicated.
  1052. *
  1053. * This WA is also required for Gen9 so extracting as a function avoids
  1054. * code duplication.
  1055. */
  1056. static u32 *
  1057. gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
  1058. {
  1059. *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
  1060. *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
  1061. *batch++ = i915_ggtt_offset(engine->scratch) + 256;
  1062. *batch++ = 0;
  1063. *batch++ = MI_LOAD_REGISTER_IMM(1);
  1064. *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
  1065. *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
  1066. batch = gen8_emit_pipe_control(batch,
  1067. PIPE_CONTROL_CS_STALL |
  1068. PIPE_CONTROL_DC_FLUSH_ENABLE,
  1069. 0);
  1070. *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
  1071. *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
  1072. *batch++ = i915_ggtt_offset(engine->scratch) + 256;
  1073. *batch++ = 0;
  1074. return batch;
  1075. }
  1076. /*
  1077. * Typically we only have one indirect_ctx and per_ctx batch buffer which are
  1078. * initialized at the beginning and shared across all contexts but this field
  1079. * helps us to have multiple batches at different offsets and select them based
  1080. * on a criteria. At the moment this batch always start at the beginning of the page
  1081. * and at this point we don't have multiple wa_ctx batch buffers.
  1082. *
  1083. * The number of WA applied are not known at the beginning; we use this field
  1084. * to return the no of DWORDS written.
  1085. *
  1086. * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
  1087. * so it adds NOOPs as padding to make it cacheline aligned.
  1088. * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
  1089. * makes a complete batch buffer.
  1090. */
  1091. static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
  1092. {
  1093. /* WaDisableCtxRestoreArbitration:bdw,chv */
  1094. *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
  1095. /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
  1096. if (IS_BROADWELL(engine->i915))
  1097. batch = gen8_emit_flush_coherentl3_wa(engine, batch);
  1098. /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
  1099. /* Actual scratch location is at 128 bytes offset */
  1100. batch = gen8_emit_pipe_control(batch,
  1101. PIPE_CONTROL_FLUSH_L3 |
  1102. PIPE_CONTROL_GLOBAL_GTT_IVB |
  1103. PIPE_CONTROL_CS_STALL |
  1104. PIPE_CONTROL_QW_WRITE,
  1105. i915_ggtt_offset(engine->scratch) +
  1106. 2 * CACHELINE_BYTES);
  1107. *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  1108. /* Pad to end of cacheline */
  1109. while ((unsigned long)batch % CACHELINE_BYTES)
  1110. *batch++ = MI_NOOP;
  1111. /*
  1112. * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
  1113. * execution depends on the length specified in terms of cache lines
  1114. * in the register CTX_RCS_INDIRECT_CTX
  1115. */
  1116. return batch;
  1117. }
  1118. static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
  1119. {
  1120. *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
  1121. /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
  1122. batch = gen8_emit_flush_coherentl3_wa(engine, batch);
  1123. /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
  1124. *batch++ = MI_LOAD_REGISTER_IMM(1);
  1125. *batch++ = i915_mmio_reg_offset(COMMON_SLICE_CHICKEN2);
  1126. *batch++ = _MASKED_BIT_DISABLE(
  1127. GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE);
  1128. *batch++ = MI_NOOP;
  1129. /* WaClearSlmSpaceAtContextSwitch:kbl */
  1130. /* Actual scratch location is at 128 bytes offset */
  1131. if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
  1132. batch = gen8_emit_pipe_control(batch,
  1133. PIPE_CONTROL_FLUSH_L3 |
  1134. PIPE_CONTROL_GLOBAL_GTT_IVB |
  1135. PIPE_CONTROL_CS_STALL |
  1136. PIPE_CONTROL_QW_WRITE,
  1137. i915_ggtt_offset(engine->scratch)
  1138. + 2 * CACHELINE_BYTES);
  1139. }
  1140. /* WaMediaPoolStateCmdInWABB:bxt,glk */
  1141. if (HAS_POOLED_EU(engine->i915)) {
  1142. /*
  1143. * EU pool configuration is setup along with golden context
  1144. * during context initialization. This value depends on
  1145. * device type (2x6 or 3x6) and needs to be updated based
  1146. * on which subslice is disabled especially for 2x6
  1147. * devices, however it is safe to load default
  1148. * configuration of 3x6 device instead of masking off
  1149. * corresponding bits because HW ignores bits of a disabled
  1150. * subslice and drops down to appropriate config. Please
  1151. * see render_state_setup() in i915_gem_render_state.c for
  1152. * possible configurations, to avoid duplication they are
  1153. * not shown here again.
  1154. */
  1155. *batch++ = GEN9_MEDIA_POOL_STATE;
  1156. *batch++ = GEN9_MEDIA_POOL_ENABLE;
  1157. *batch++ = 0x00777000;
  1158. *batch++ = 0;
  1159. *batch++ = 0;
  1160. *batch++ = 0;
  1161. }
  1162. *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  1163. /* Pad to end of cacheline */
  1164. while ((unsigned long)batch % CACHELINE_BYTES)
  1165. *batch++ = MI_NOOP;
  1166. return batch;
  1167. }
  1168. #define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
  1169. static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
  1170. {
  1171. struct drm_i915_gem_object *obj;
  1172. struct i915_vma *vma;
  1173. int err;
  1174. obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
  1175. if (IS_ERR(obj))
  1176. return PTR_ERR(obj);
  1177. vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
  1178. if (IS_ERR(vma)) {
  1179. err = PTR_ERR(vma);
  1180. goto err;
  1181. }
  1182. err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
  1183. if (err)
  1184. goto err;
  1185. engine->wa_ctx.vma = vma;
  1186. return 0;
  1187. err:
  1188. i915_gem_object_put(obj);
  1189. return err;
  1190. }
  1191. static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
  1192. {
  1193. i915_vma_unpin_and_release(&engine->wa_ctx.vma);
  1194. }
  1195. typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
  1196. static int intel_init_workaround_bb(struct intel_engine_cs *engine)
  1197. {
  1198. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1199. struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
  1200. &wa_ctx->per_ctx };
  1201. wa_bb_func_t wa_bb_fn[2];
  1202. struct page *page;
  1203. void *batch, *batch_ptr;
  1204. unsigned int i;
  1205. int ret;
  1206. if (WARN_ON(engine->id != RCS || !engine->scratch))
  1207. return -EINVAL;
  1208. switch (INTEL_GEN(engine->i915)) {
  1209. case 10:
  1210. return 0;
  1211. case 9:
  1212. wa_bb_fn[0] = gen9_init_indirectctx_bb;
  1213. wa_bb_fn[1] = NULL;
  1214. break;
  1215. case 8:
  1216. wa_bb_fn[0] = gen8_init_indirectctx_bb;
  1217. wa_bb_fn[1] = NULL;
  1218. break;
  1219. default:
  1220. MISSING_CASE(INTEL_GEN(engine->i915));
  1221. return 0;
  1222. }
  1223. ret = lrc_setup_wa_ctx(engine);
  1224. if (ret) {
  1225. DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
  1226. return ret;
  1227. }
  1228. page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
  1229. batch = batch_ptr = kmap_atomic(page);
  1230. /*
  1231. * Emit the two workaround batch buffers, recording the offset from the
  1232. * start of the workaround batch buffer object for each and their
  1233. * respective sizes.
  1234. */
  1235. for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
  1236. wa_bb[i]->offset = batch_ptr - batch;
  1237. if (WARN_ON(!IS_ALIGNED(wa_bb[i]->offset, CACHELINE_BYTES))) {
  1238. ret = -EINVAL;
  1239. break;
  1240. }
  1241. if (wa_bb_fn[i])
  1242. batch_ptr = wa_bb_fn[i](engine, batch_ptr);
  1243. wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
  1244. }
  1245. BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
  1246. kunmap_atomic(batch);
  1247. if (ret)
  1248. lrc_destroy_wa_ctx(engine);
  1249. return ret;
  1250. }
  1251. static u8 gtiir[] = {
  1252. [RCS] = 0,
  1253. [BCS] = 0,
  1254. [VCS] = 1,
  1255. [VCS2] = 1,
  1256. [VECS] = 3,
  1257. };
  1258. static int gen8_init_common_ring(struct intel_engine_cs *engine)
  1259. {
  1260. struct drm_i915_private *dev_priv = engine->i915;
  1261. struct intel_engine_execlists * const execlists = &engine->execlists;
  1262. int ret;
  1263. ret = intel_mocs_init_engine(engine);
  1264. if (ret)
  1265. return ret;
  1266. intel_engine_reset_breadcrumbs(engine);
  1267. intel_engine_init_hangcheck(engine);
  1268. I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
  1269. I915_WRITE(RING_MODE_GEN7(engine),
  1270. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  1271. I915_WRITE(RING_HWS_PGA(engine->mmio_base),
  1272. engine->status_page.ggtt_offset);
  1273. POSTING_READ(RING_HWS_PGA(engine->mmio_base));
  1274. DRM_DEBUG_DRIVER("Execlists enabled for %s\n", engine->name);
  1275. GEM_BUG_ON(engine->id >= ARRAY_SIZE(gtiir));
  1276. /*
  1277. * Clear any pending interrupt state.
  1278. *
  1279. * We do it twice out of paranoia that some of the IIR are double
  1280. * buffered, and if we only reset it once there may still be
  1281. * an interrupt pending.
  1282. */
  1283. I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
  1284. GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
  1285. I915_WRITE(GEN8_GT_IIR(gtiir[engine->id]),
  1286. GT_CONTEXT_SWITCH_INTERRUPT << engine->irq_shift);
  1287. clear_bit(ENGINE_IRQ_EXECLIST, &engine->irq_posted);
  1288. execlists->csb_head = -1;
  1289. execlists->active = 0;
  1290. execlists->elsp =
  1291. dev_priv->regs + i915_mmio_reg_offset(RING_ELSP(engine));
  1292. /* After a GPU reset, we may have requests to replay */
  1293. if (execlists->first)
  1294. tasklet_schedule(&execlists->tasklet);
  1295. return 0;
  1296. }
  1297. static int gen8_init_render_ring(struct intel_engine_cs *engine)
  1298. {
  1299. struct drm_i915_private *dev_priv = engine->i915;
  1300. int ret;
  1301. ret = gen8_init_common_ring(engine);
  1302. if (ret)
  1303. return ret;
  1304. /* We need to disable the AsyncFlip performance optimisations in order
  1305. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  1306. * programmed to '1' on all products.
  1307. *
  1308. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  1309. */
  1310. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  1311. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  1312. return init_workarounds_ring(engine);
  1313. }
  1314. static int gen9_init_render_ring(struct intel_engine_cs *engine)
  1315. {
  1316. int ret;
  1317. ret = gen8_init_common_ring(engine);
  1318. if (ret)
  1319. return ret;
  1320. return init_workarounds_ring(engine);
  1321. }
  1322. static void reset_common_ring(struct intel_engine_cs *engine,
  1323. struct drm_i915_gem_request *request)
  1324. {
  1325. struct intel_engine_execlists * const execlists = &engine->execlists;
  1326. struct intel_context *ce;
  1327. unsigned long flags;
  1328. GEM_TRACE("%s seqno=%x\n",
  1329. engine->name, request ? request->global_seqno : 0);
  1330. spin_lock_irqsave(&engine->timeline->lock, flags);
  1331. /*
  1332. * Catch up with any missed context-switch interrupts.
  1333. *
  1334. * Ideally we would just read the remaining CSB entries now that we
  1335. * know the gpu is idle. However, the CSB registers are sometimes^W
  1336. * often trashed across a GPU reset! Instead we have to rely on
  1337. * guessing the missed context-switch events by looking at what
  1338. * requests were completed.
  1339. */
  1340. execlists_cancel_port_requests(execlists);
  1341. /* Push back any incomplete requests for replay after the reset. */
  1342. __unwind_incomplete_requests(engine);
  1343. spin_unlock_irqrestore(&engine->timeline->lock, flags);
  1344. /* If the request was innocent, we leave the request in the ELSP
  1345. * and will try to replay it on restarting. The context image may
  1346. * have been corrupted by the reset, in which case we may have
  1347. * to service a new GPU hang, but more likely we can continue on
  1348. * without impact.
  1349. *
  1350. * If the request was guilty, we presume the context is corrupt
  1351. * and have to at least restore the RING register in the context
  1352. * image back to the expected values to skip over the guilty request.
  1353. */
  1354. if (!request || request->fence.error != -EIO)
  1355. return;
  1356. /* We want a simple context + ring to execute the breadcrumb update.
  1357. * We cannot rely on the context being intact across the GPU hang,
  1358. * so clear it and rebuild just what we need for the breadcrumb.
  1359. * All pending requests for this context will be zapped, and any
  1360. * future request will be after userspace has had the opportunity
  1361. * to recreate its own state.
  1362. */
  1363. ce = &request->ctx->engine[engine->id];
  1364. execlists_init_reg_state(ce->lrc_reg_state,
  1365. request->ctx, engine, ce->ring);
  1366. /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
  1367. ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
  1368. i915_ggtt_offset(ce->ring->vma);
  1369. ce->lrc_reg_state[CTX_RING_HEAD+1] = request->postfix;
  1370. request->ring->head = request->postfix;
  1371. intel_ring_update_space(request->ring);
  1372. /* Reset WaIdleLiteRestore:bdw,skl as well */
  1373. unwind_wa_tail(request);
  1374. }
  1375. static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
  1376. {
  1377. struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
  1378. struct intel_engine_cs *engine = req->engine;
  1379. const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
  1380. u32 *cs;
  1381. int i;
  1382. cs = intel_ring_begin(req, num_lri_cmds * 2 + 2);
  1383. if (IS_ERR(cs))
  1384. return PTR_ERR(cs);
  1385. *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
  1386. for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
  1387. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  1388. *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
  1389. *cs++ = upper_32_bits(pd_daddr);
  1390. *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
  1391. *cs++ = lower_32_bits(pd_daddr);
  1392. }
  1393. *cs++ = MI_NOOP;
  1394. intel_ring_advance(req, cs);
  1395. return 0;
  1396. }
  1397. static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
  1398. u64 offset, u32 len,
  1399. const unsigned int flags)
  1400. {
  1401. u32 *cs;
  1402. int ret;
  1403. /* Don't rely in hw updating PDPs, specially in lite-restore.
  1404. * Ideally, we should set Force PD Restore in ctx descriptor,
  1405. * but we can't. Force Restore would be a second option, but
  1406. * it is unsafe in case of lite-restore (because the ctx is
  1407. * not idle). PML4 is allocated during ppgtt init so this is
  1408. * not needed in 48-bit.*/
  1409. if (req->ctx->ppgtt &&
  1410. (intel_engine_flag(req->engine) & req->ctx->ppgtt->pd_dirty_rings) &&
  1411. !i915_vm_is_48bit(&req->ctx->ppgtt->base) &&
  1412. !intel_vgpu_active(req->i915)) {
  1413. ret = intel_logical_ring_emit_pdps(req);
  1414. if (ret)
  1415. return ret;
  1416. req->ctx->ppgtt->pd_dirty_rings &= ~intel_engine_flag(req->engine);
  1417. }
  1418. cs = intel_ring_begin(req, 4);
  1419. if (IS_ERR(cs))
  1420. return PTR_ERR(cs);
  1421. /*
  1422. * WaDisableCtxRestoreArbitration:bdw,chv
  1423. *
  1424. * We don't need to perform MI_ARB_ENABLE as often as we do (in
  1425. * particular all the gen that do not need the w/a at all!), if we
  1426. * took care to make sure that on every switch into this context
  1427. * (both ordinary and for preemption) that arbitrartion was enabled
  1428. * we would be fine. However, there doesn't seem to be a downside to
  1429. * being paranoid and making sure it is set before each batch and
  1430. * every context-switch.
  1431. *
  1432. * Note that if we fail to enable arbitration before the request
  1433. * is complete, then we do not see the context-switch interrupt and
  1434. * the engine hangs (with RING_HEAD == RING_TAIL).
  1435. *
  1436. * That satisfies both the GPGPU w/a and our heavy-handed paranoia.
  1437. */
  1438. *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  1439. /* FIXME(BDW): Address space and security selectors. */
  1440. *cs++ = MI_BATCH_BUFFER_START_GEN8 |
  1441. (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
  1442. (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
  1443. *cs++ = lower_32_bits(offset);
  1444. *cs++ = upper_32_bits(offset);
  1445. intel_ring_advance(req, cs);
  1446. return 0;
  1447. }
  1448. static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
  1449. {
  1450. struct drm_i915_private *dev_priv = engine->i915;
  1451. I915_WRITE_IMR(engine,
  1452. ~(engine->irq_enable_mask | engine->irq_keep_mask));
  1453. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1454. }
  1455. static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
  1456. {
  1457. struct drm_i915_private *dev_priv = engine->i915;
  1458. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  1459. }
  1460. static int gen8_emit_flush(struct drm_i915_gem_request *request, u32 mode)
  1461. {
  1462. u32 cmd, *cs;
  1463. cs = intel_ring_begin(request, 4);
  1464. if (IS_ERR(cs))
  1465. return PTR_ERR(cs);
  1466. cmd = MI_FLUSH_DW + 1;
  1467. /* We always require a command barrier so that subsequent
  1468. * commands, such as breadcrumb interrupts, are strictly ordered
  1469. * wrt the contents of the write cache being flushed to memory
  1470. * (and thus being coherent from the CPU).
  1471. */
  1472. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1473. if (mode & EMIT_INVALIDATE) {
  1474. cmd |= MI_INVALIDATE_TLB;
  1475. if (request->engine->id == VCS)
  1476. cmd |= MI_INVALIDATE_BSD;
  1477. }
  1478. *cs++ = cmd;
  1479. *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
  1480. *cs++ = 0; /* upper addr */
  1481. *cs++ = 0; /* value */
  1482. intel_ring_advance(request, cs);
  1483. return 0;
  1484. }
  1485. static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
  1486. u32 mode)
  1487. {
  1488. struct intel_engine_cs *engine = request->engine;
  1489. u32 scratch_addr =
  1490. i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  1491. bool vf_flush_wa = false, dc_flush_wa = false;
  1492. u32 *cs, flags = 0;
  1493. int len;
  1494. flags |= PIPE_CONTROL_CS_STALL;
  1495. if (mode & EMIT_FLUSH) {
  1496. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  1497. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  1498. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  1499. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  1500. }
  1501. if (mode & EMIT_INVALIDATE) {
  1502. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  1503. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  1504. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  1505. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1506. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  1507. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  1508. flags |= PIPE_CONTROL_QW_WRITE;
  1509. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1510. /*
  1511. * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
  1512. * pipe control.
  1513. */
  1514. if (IS_GEN9(request->i915))
  1515. vf_flush_wa = true;
  1516. /* WaForGAMHang:kbl */
  1517. if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
  1518. dc_flush_wa = true;
  1519. }
  1520. len = 6;
  1521. if (vf_flush_wa)
  1522. len += 6;
  1523. if (dc_flush_wa)
  1524. len += 12;
  1525. cs = intel_ring_begin(request, len);
  1526. if (IS_ERR(cs))
  1527. return PTR_ERR(cs);
  1528. if (vf_flush_wa)
  1529. cs = gen8_emit_pipe_control(cs, 0, 0);
  1530. if (dc_flush_wa)
  1531. cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
  1532. 0);
  1533. cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
  1534. if (dc_flush_wa)
  1535. cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
  1536. intel_ring_advance(request, cs);
  1537. return 0;
  1538. }
  1539. /*
  1540. * Reserve space for 2 NOOPs at the end of each request to be
  1541. * used as a workaround for not being allowed to do lite
  1542. * restore with HEAD==TAIL (WaIdleLiteRestore).
  1543. */
  1544. static void gen8_emit_wa_tail(struct drm_i915_gem_request *request, u32 *cs)
  1545. {
  1546. /* Ensure there's always at least one preemption point per-request. */
  1547. *cs++ = MI_ARB_CHECK;
  1548. *cs++ = MI_NOOP;
  1549. request->wa_tail = intel_ring_offset(request, cs);
  1550. }
  1551. static void gen8_emit_breadcrumb(struct drm_i915_gem_request *request, u32 *cs)
  1552. {
  1553. /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
  1554. BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
  1555. cs = gen8_emit_ggtt_write(cs, request->global_seqno,
  1556. intel_hws_seqno_address(request->engine));
  1557. *cs++ = MI_USER_INTERRUPT;
  1558. *cs++ = MI_NOOP;
  1559. request->tail = intel_ring_offset(request, cs);
  1560. assert_ring_tail_valid(request->ring, request->tail);
  1561. gen8_emit_wa_tail(request, cs);
  1562. }
  1563. static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
  1564. static void gen8_emit_breadcrumb_rcs(struct drm_i915_gem_request *request,
  1565. u32 *cs)
  1566. {
  1567. /* We're using qword write, seqno should be aligned to 8 bytes. */
  1568. BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
  1569. cs = gen8_emit_ggtt_write_rcs(cs, request->global_seqno,
  1570. intel_hws_seqno_address(request->engine));
  1571. *cs++ = MI_USER_INTERRUPT;
  1572. *cs++ = MI_NOOP;
  1573. request->tail = intel_ring_offset(request, cs);
  1574. assert_ring_tail_valid(request->ring, request->tail);
  1575. gen8_emit_wa_tail(request, cs);
  1576. }
  1577. static const int gen8_emit_breadcrumb_rcs_sz = 8 + WA_TAIL_DWORDS;
  1578. static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
  1579. {
  1580. int ret;
  1581. ret = intel_ring_workarounds_emit(req);
  1582. if (ret)
  1583. return ret;
  1584. ret = intel_rcs_context_init_mocs(req);
  1585. /*
  1586. * Failing to program the MOCS is non-fatal.The system will not
  1587. * run at peak performance. So generate an error and carry on.
  1588. */
  1589. if (ret)
  1590. DRM_ERROR("MOCS failed to program: expect performance issues.\n");
  1591. return i915_gem_render_state_emit(req);
  1592. }
  1593. /**
  1594. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1595. * @engine: Engine Command Streamer.
  1596. */
  1597. void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
  1598. {
  1599. struct drm_i915_private *dev_priv;
  1600. /*
  1601. * Tasklet cannot be active at this point due intel_mark_active/idle
  1602. * so this is just for documentation.
  1603. */
  1604. if (WARN_ON(test_bit(TASKLET_STATE_SCHED,
  1605. &engine->execlists.tasklet.state)))
  1606. tasklet_kill(&engine->execlists.tasklet);
  1607. dev_priv = engine->i915;
  1608. if (engine->buffer) {
  1609. WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1610. }
  1611. if (engine->cleanup)
  1612. engine->cleanup(engine);
  1613. intel_engine_cleanup_common(engine);
  1614. lrc_destroy_wa_ctx(engine);
  1615. engine->i915 = NULL;
  1616. dev_priv->engine[engine->id] = NULL;
  1617. kfree(engine);
  1618. }
  1619. static void execlists_set_default_submission(struct intel_engine_cs *engine)
  1620. {
  1621. engine->submit_request = execlists_submit_request;
  1622. engine->cancel_requests = execlists_cancel_requests;
  1623. engine->schedule = execlists_schedule;
  1624. engine->execlists.tasklet.func = execlists_submission_tasklet;
  1625. engine->park = NULL;
  1626. engine->unpark = NULL;
  1627. engine->flags |= I915_ENGINE_SUPPORTS_STATS;
  1628. }
  1629. static void
  1630. logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  1631. {
  1632. /* Default vfuncs which can be overriden by each engine. */
  1633. engine->init_hw = gen8_init_common_ring;
  1634. engine->reset_hw = reset_common_ring;
  1635. engine->context_pin = execlists_context_pin;
  1636. engine->context_unpin = execlists_context_unpin;
  1637. engine->request_alloc = execlists_request_alloc;
  1638. engine->emit_flush = gen8_emit_flush;
  1639. engine->emit_breadcrumb = gen8_emit_breadcrumb;
  1640. engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
  1641. engine->set_default_submission = execlists_set_default_submission;
  1642. engine->irq_enable = gen8_logical_ring_enable_irq;
  1643. engine->irq_disable = gen8_logical_ring_disable_irq;
  1644. engine->emit_bb_start = gen8_emit_bb_start;
  1645. }
  1646. static inline void
  1647. logical_ring_default_irqs(struct intel_engine_cs *engine)
  1648. {
  1649. unsigned shift = engine->irq_shift;
  1650. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
  1651. engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
  1652. }
  1653. static void
  1654. logical_ring_setup(struct intel_engine_cs *engine)
  1655. {
  1656. struct drm_i915_private *dev_priv = engine->i915;
  1657. enum forcewake_domains fw_domains;
  1658. intel_engine_setup_common(engine);
  1659. /* Intentionally left blank. */
  1660. engine->buffer = NULL;
  1661. fw_domains = intel_uncore_forcewake_for_reg(dev_priv,
  1662. RING_ELSP(engine),
  1663. FW_REG_WRITE);
  1664. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1665. RING_CONTEXT_STATUS_PTR(engine),
  1666. FW_REG_READ | FW_REG_WRITE);
  1667. fw_domains |= intel_uncore_forcewake_for_reg(dev_priv,
  1668. RING_CONTEXT_STATUS_BUF_BASE(engine),
  1669. FW_REG_READ);
  1670. engine->execlists.fw_domains = fw_domains;
  1671. tasklet_init(&engine->execlists.tasklet,
  1672. execlists_submission_tasklet, (unsigned long)engine);
  1673. logical_ring_default_vfuncs(engine);
  1674. logical_ring_default_irqs(engine);
  1675. }
  1676. static int logical_ring_init(struct intel_engine_cs *engine)
  1677. {
  1678. int ret;
  1679. ret = intel_engine_init_common(engine);
  1680. if (ret)
  1681. goto error;
  1682. return 0;
  1683. error:
  1684. intel_logical_ring_cleanup(engine);
  1685. return ret;
  1686. }
  1687. int logical_render_ring_init(struct intel_engine_cs *engine)
  1688. {
  1689. struct drm_i915_private *dev_priv = engine->i915;
  1690. int ret;
  1691. logical_ring_setup(engine);
  1692. if (HAS_L3_DPF(dev_priv))
  1693. engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1694. /* Override some for render ring. */
  1695. if (INTEL_GEN(dev_priv) >= 9)
  1696. engine->init_hw = gen9_init_render_ring;
  1697. else
  1698. engine->init_hw = gen8_init_render_ring;
  1699. engine->init_context = gen8_init_rcs_context;
  1700. engine->emit_flush = gen8_emit_flush_render;
  1701. engine->emit_breadcrumb = gen8_emit_breadcrumb_rcs;
  1702. engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_rcs_sz;
  1703. ret = intel_engine_create_scratch(engine, PAGE_SIZE);
  1704. if (ret)
  1705. return ret;
  1706. ret = intel_init_workaround_bb(engine);
  1707. if (ret) {
  1708. /*
  1709. * We continue even if we fail to initialize WA batch
  1710. * because we only expect rare glitches but nothing
  1711. * critical to prevent us from using GPU
  1712. */
  1713. DRM_ERROR("WA batch buffer initialization failed: %d\n",
  1714. ret);
  1715. }
  1716. return logical_ring_init(engine);
  1717. }
  1718. int logical_xcs_ring_init(struct intel_engine_cs *engine)
  1719. {
  1720. logical_ring_setup(engine);
  1721. return logical_ring_init(engine);
  1722. }
  1723. static u32
  1724. make_rpcs(struct drm_i915_private *dev_priv)
  1725. {
  1726. u32 rpcs = 0;
  1727. /*
  1728. * No explicit RPCS request is needed to ensure full
  1729. * slice/subslice/EU enablement prior to Gen9.
  1730. */
  1731. if (INTEL_GEN(dev_priv) < 9)
  1732. return 0;
  1733. /*
  1734. * Starting in Gen9, render power gating can leave
  1735. * slice/subslice/EU in a partially enabled state. We
  1736. * must make an explicit request through RPCS for full
  1737. * enablement.
  1738. */
  1739. if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
  1740. rpcs |= GEN8_RPCS_S_CNT_ENABLE;
  1741. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
  1742. GEN8_RPCS_S_CNT_SHIFT;
  1743. rpcs |= GEN8_RPCS_ENABLE;
  1744. }
  1745. if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
  1746. rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
  1747. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask) <<
  1748. GEN8_RPCS_SS_CNT_SHIFT;
  1749. rpcs |= GEN8_RPCS_ENABLE;
  1750. }
  1751. if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
  1752. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  1753. GEN8_RPCS_EU_MIN_SHIFT;
  1754. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  1755. GEN8_RPCS_EU_MAX_SHIFT;
  1756. rpcs |= GEN8_RPCS_ENABLE;
  1757. }
  1758. return rpcs;
  1759. }
  1760. static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
  1761. {
  1762. u32 indirect_ctx_offset;
  1763. switch (INTEL_GEN(engine->i915)) {
  1764. default:
  1765. MISSING_CASE(INTEL_GEN(engine->i915));
  1766. /* fall through */
  1767. case 10:
  1768. indirect_ctx_offset =
  1769. GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1770. break;
  1771. case 9:
  1772. indirect_ctx_offset =
  1773. GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1774. break;
  1775. case 8:
  1776. indirect_ctx_offset =
  1777. GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  1778. break;
  1779. }
  1780. return indirect_ctx_offset;
  1781. }
  1782. static void execlists_init_reg_state(u32 *regs,
  1783. struct i915_gem_context *ctx,
  1784. struct intel_engine_cs *engine,
  1785. struct intel_ring *ring)
  1786. {
  1787. struct drm_i915_private *dev_priv = engine->i915;
  1788. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
  1789. u32 base = engine->mmio_base;
  1790. bool rcs = engine->id == RCS;
  1791. /* A context is actually a big batch buffer with several
  1792. * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
  1793. * values we are setting here are only for the first context restore:
  1794. * on a subsequent save, the GPU will recreate this batchbuffer with new
  1795. * values (including all the missing MI_LOAD_REGISTER_IMM commands that
  1796. * we are not initializing here).
  1797. */
  1798. regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
  1799. MI_LRI_FORCE_POSTED;
  1800. CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
  1801. _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
  1802. (HAS_RESOURCE_STREAMER(dev_priv) ?
  1803. CTX_CTRL_RS_CTX_ENABLE : 0)));
  1804. CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
  1805. CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
  1806. CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
  1807. CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
  1808. RING_CTL_SIZE(ring->size) | RING_VALID);
  1809. CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
  1810. CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
  1811. CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
  1812. CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
  1813. CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
  1814. CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
  1815. if (rcs) {
  1816. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1817. CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
  1818. CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
  1819. RING_INDIRECT_CTX_OFFSET(base), 0);
  1820. if (wa_ctx->indirect_ctx.size) {
  1821. u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
  1822. regs[CTX_RCS_INDIRECT_CTX + 1] =
  1823. (ggtt_offset + wa_ctx->indirect_ctx.offset) |
  1824. (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
  1825. regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
  1826. intel_lr_indirect_ctx_offset(engine) << 6;
  1827. }
  1828. CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
  1829. if (wa_ctx->per_ctx.size) {
  1830. u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
  1831. regs[CTX_BB_PER_CTX_PTR + 1] =
  1832. (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
  1833. }
  1834. }
  1835. regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
  1836. CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
  1837. /* PDP values well be assigned later if needed */
  1838. CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
  1839. CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
  1840. CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
  1841. CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
  1842. CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
  1843. CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
  1844. CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
  1845. CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
  1846. if (ppgtt && i915_vm_is_48bit(&ppgtt->base)) {
  1847. /* 64b PPGTT (48bit canonical)
  1848. * PDP0_DESCRIPTOR contains the base address to PML4 and
  1849. * other PDP Descriptors are ignored.
  1850. */
  1851. ASSIGN_CTX_PML4(ppgtt, regs);
  1852. }
  1853. if (rcs) {
  1854. regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  1855. CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
  1856. make_rpcs(dev_priv));
  1857. i915_oa_init_reg_state(engine, ctx, regs);
  1858. }
  1859. }
  1860. static int
  1861. populate_lr_context(struct i915_gem_context *ctx,
  1862. struct drm_i915_gem_object *ctx_obj,
  1863. struct intel_engine_cs *engine,
  1864. struct intel_ring *ring)
  1865. {
  1866. void *vaddr;
  1867. u32 *regs;
  1868. int ret;
  1869. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1870. if (ret) {
  1871. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1872. return ret;
  1873. }
  1874. vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
  1875. if (IS_ERR(vaddr)) {
  1876. ret = PTR_ERR(vaddr);
  1877. DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
  1878. return ret;
  1879. }
  1880. ctx_obj->mm.dirty = true;
  1881. if (engine->default_state) {
  1882. /*
  1883. * We only want to copy over the template context state;
  1884. * skipping over the headers reserved for GuC communication,
  1885. * leaving those as zero.
  1886. */
  1887. const unsigned long start = LRC_HEADER_PAGES * PAGE_SIZE;
  1888. void *defaults;
  1889. defaults = i915_gem_object_pin_map(engine->default_state,
  1890. I915_MAP_WB);
  1891. if (IS_ERR(defaults))
  1892. return PTR_ERR(defaults);
  1893. memcpy(vaddr + start, defaults + start, engine->context_size);
  1894. i915_gem_object_unpin_map(engine->default_state);
  1895. }
  1896. /* The second page of the context object contains some fields which must
  1897. * be set up prior to the first execution. */
  1898. regs = vaddr + LRC_STATE_PN * PAGE_SIZE;
  1899. execlists_init_reg_state(regs, ctx, engine, ring);
  1900. if (!engine->default_state)
  1901. regs[CTX_CONTEXT_CONTROL + 1] |=
  1902. _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
  1903. i915_gem_object_unpin_map(ctx_obj);
  1904. return 0;
  1905. }
  1906. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  1907. struct intel_engine_cs *engine)
  1908. {
  1909. struct drm_i915_gem_object *ctx_obj;
  1910. struct intel_context *ce = &ctx->engine[engine->id];
  1911. struct i915_vma *vma;
  1912. uint32_t context_size;
  1913. struct intel_ring *ring;
  1914. int ret;
  1915. WARN_ON(ce->state);
  1916. context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
  1917. /*
  1918. * Before the actual start of the context image, we insert a few pages
  1919. * for our own use and for sharing with the GuC.
  1920. */
  1921. context_size += LRC_HEADER_PAGES * PAGE_SIZE;
  1922. ctx_obj = i915_gem_object_create(ctx->i915, context_size);
  1923. if (IS_ERR(ctx_obj)) {
  1924. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
  1925. return PTR_ERR(ctx_obj);
  1926. }
  1927. vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.base, NULL);
  1928. if (IS_ERR(vma)) {
  1929. ret = PTR_ERR(vma);
  1930. goto error_deref_obj;
  1931. }
  1932. ring = intel_engine_create_ring(engine, ctx->ring_size);
  1933. if (IS_ERR(ring)) {
  1934. ret = PTR_ERR(ring);
  1935. goto error_deref_obj;
  1936. }
  1937. ret = populate_lr_context(ctx, ctx_obj, engine, ring);
  1938. if (ret) {
  1939. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  1940. goto error_ring_free;
  1941. }
  1942. ce->ring = ring;
  1943. ce->state = vma;
  1944. return 0;
  1945. error_ring_free:
  1946. intel_ring_free(ring);
  1947. error_deref_obj:
  1948. i915_gem_object_put(ctx_obj);
  1949. return ret;
  1950. }
  1951. void intel_lr_context_resume(struct drm_i915_private *dev_priv)
  1952. {
  1953. struct intel_engine_cs *engine;
  1954. struct i915_gem_context *ctx;
  1955. enum intel_engine_id id;
  1956. /* Because we emit WA_TAIL_DWORDS there may be a disparity
  1957. * between our bookkeeping in ce->ring->head and ce->ring->tail and
  1958. * that stored in context. As we only write new commands from
  1959. * ce->ring->tail onwards, everything before that is junk. If the GPU
  1960. * starts reading from its RING_HEAD from the context, it may try to
  1961. * execute that junk and die.
  1962. *
  1963. * So to avoid that we reset the context images upon resume. For
  1964. * simplicity, we just zero everything out.
  1965. */
  1966. list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
  1967. for_each_engine(engine, dev_priv, id) {
  1968. struct intel_context *ce = &ctx->engine[engine->id];
  1969. u32 *reg;
  1970. if (!ce->state)
  1971. continue;
  1972. reg = i915_gem_object_pin_map(ce->state->obj,
  1973. I915_MAP_WB);
  1974. if (WARN_ON(IS_ERR(reg)))
  1975. continue;
  1976. reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
  1977. reg[CTX_RING_HEAD+1] = 0;
  1978. reg[CTX_RING_TAIL+1] = 0;
  1979. ce->state->obj->mm.dirty = true;
  1980. i915_gem_object_unpin_map(ce->state->obj);
  1981. intel_ring_reset(ce->ring, 0);
  1982. }
  1983. }
  1984. }