intel_lrc.c 71 KB

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