intel_lrc.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039
  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 <drm/drmP.h>
  134. #include <drm/i915_drm.h>
  135. #include "i915_drv.h"
  136. #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
  137. #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
  138. #define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
  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 CTX_LRI_HEADER_0 0x01
  152. #define CTX_CONTEXT_CONTROL 0x02
  153. #define CTX_RING_HEAD 0x04
  154. #define CTX_RING_TAIL 0x06
  155. #define CTX_RING_BUFFER_START 0x08
  156. #define CTX_RING_BUFFER_CONTROL 0x0a
  157. #define CTX_BB_HEAD_U 0x0c
  158. #define CTX_BB_HEAD_L 0x0e
  159. #define CTX_BB_STATE 0x10
  160. #define CTX_SECOND_BB_HEAD_U 0x12
  161. #define CTX_SECOND_BB_HEAD_L 0x14
  162. #define CTX_SECOND_BB_STATE 0x16
  163. #define CTX_BB_PER_CTX_PTR 0x18
  164. #define CTX_RCS_INDIRECT_CTX 0x1a
  165. #define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
  166. #define CTX_LRI_HEADER_1 0x21
  167. #define CTX_CTX_TIMESTAMP 0x22
  168. #define CTX_PDP3_UDW 0x24
  169. #define CTX_PDP3_LDW 0x26
  170. #define CTX_PDP2_UDW 0x28
  171. #define CTX_PDP2_LDW 0x2a
  172. #define CTX_PDP1_UDW 0x2c
  173. #define CTX_PDP1_LDW 0x2e
  174. #define CTX_PDP0_UDW 0x30
  175. #define CTX_PDP0_LDW 0x32
  176. #define CTX_LRI_HEADER_2 0x41
  177. #define CTX_R_PWR_CLK_STATE 0x42
  178. #define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
  179. #define GEN8_CTX_VALID (1<<0)
  180. #define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
  181. #define GEN8_CTX_FORCE_RESTORE (1<<2)
  182. #define GEN8_CTX_L3LLC_COHERENT (1<<5)
  183. #define GEN8_CTX_PRIVILEGE (1<<8)
  184. #define ASSIGN_CTX_PDP(ppgtt, reg_state, n) { \
  185. const u64 _addr = test_bit(n, ppgtt->pdp.used_pdpes) ? \
  186. ppgtt->pdp.page_directory[n]->daddr : \
  187. ppgtt->scratch_pd->daddr; \
  188. reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
  189. reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
  190. }
  191. enum {
  192. ADVANCED_CONTEXT = 0,
  193. LEGACY_CONTEXT,
  194. ADVANCED_AD_CONTEXT,
  195. LEGACY_64B_CONTEXT
  196. };
  197. #define GEN8_CTX_MODE_SHIFT 3
  198. enum {
  199. FAULT_AND_HANG = 0,
  200. FAULT_AND_HALT, /* Debug only */
  201. FAULT_AND_STREAM,
  202. FAULT_AND_CONTINUE /* Unsupported */
  203. };
  204. #define GEN8_CTX_ID_SHIFT 32
  205. static int intel_lr_context_pin(struct intel_engine_cs *ring,
  206. struct intel_context *ctx);
  207. /**
  208. * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
  209. * @dev: DRM device.
  210. * @enable_execlists: value of i915.enable_execlists module parameter.
  211. *
  212. * Only certain platforms support Execlists (the prerequisites being
  213. * support for Logical Ring Contexts and Aliasing PPGTT or better).
  214. *
  215. * Return: 1 if Execlists is supported and has to be enabled.
  216. */
  217. int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists)
  218. {
  219. WARN_ON(i915.enable_ppgtt == -1);
  220. if (INTEL_INFO(dev)->gen >= 9)
  221. return 1;
  222. if (enable_execlists == 0)
  223. return 0;
  224. if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
  225. i915.use_mmio_flip >= 0)
  226. return 1;
  227. return 0;
  228. }
  229. /**
  230. * intel_execlists_ctx_id() - get the Execlists Context ID
  231. * @ctx_obj: Logical Ring Context backing object.
  232. *
  233. * Do not confuse with ctx->id! Unfortunately we have a name overload
  234. * here: the old context ID we pass to userspace as a handler so that
  235. * they can refer to a context, and the new context ID we pass to the
  236. * ELSP so that the GPU can inform us of the context status via
  237. * interrupts.
  238. *
  239. * Return: 20-bits globally unique context ID.
  240. */
  241. u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
  242. {
  243. u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
  244. /* LRCA is required to be 4K aligned so the more significant 20 bits
  245. * are globally unique */
  246. return lrca >> 12;
  247. }
  248. static uint64_t execlists_ctx_descriptor(struct intel_engine_cs *ring,
  249. struct drm_i915_gem_object *ctx_obj)
  250. {
  251. struct drm_device *dev = ring->dev;
  252. uint64_t desc;
  253. uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
  254. WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
  255. desc = GEN8_CTX_VALID;
  256. desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
  257. if (IS_GEN8(ctx_obj->base.dev))
  258. desc |= GEN8_CTX_L3LLC_COHERENT;
  259. desc |= GEN8_CTX_PRIVILEGE;
  260. desc |= lrca;
  261. desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
  262. /* TODO: WaDisableLiteRestore when we start using semaphore
  263. * signalling between Command Streamers */
  264. /* desc |= GEN8_CTX_FORCE_RESTORE; */
  265. /* WaEnableForceRestoreInCtxtDescForVCS:skl */
  266. if (IS_GEN9(dev) &&
  267. INTEL_REVID(dev) <= SKL_REVID_B0 &&
  268. (ring->id == BCS || ring->id == VCS ||
  269. ring->id == VECS || ring->id == VCS2))
  270. desc |= GEN8_CTX_FORCE_RESTORE;
  271. return desc;
  272. }
  273. static void execlists_elsp_write(struct intel_engine_cs *ring,
  274. struct drm_i915_gem_object *ctx_obj0,
  275. struct drm_i915_gem_object *ctx_obj1)
  276. {
  277. struct drm_device *dev = ring->dev;
  278. struct drm_i915_private *dev_priv = dev->dev_private;
  279. uint64_t temp = 0;
  280. uint32_t desc[4];
  281. /* XXX: You must always write both descriptors in the order below. */
  282. if (ctx_obj1)
  283. temp = execlists_ctx_descriptor(ring, ctx_obj1);
  284. else
  285. temp = 0;
  286. desc[1] = (u32)(temp >> 32);
  287. desc[0] = (u32)temp;
  288. temp = execlists_ctx_descriptor(ring, ctx_obj0);
  289. desc[3] = (u32)(temp >> 32);
  290. desc[2] = (u32)temp;
  291. spin_lock(&dev_priv->uncore.lock);
  292. intel_uncore_forcewake_get__locked(dev_priv, FORCEWAKE_ALL);
  293. I915_WRITE_FW(RING_ELSP(ring), desc[1]);
  294. I915_WRITE_FW(RING_ELSP(ring), desc[0]);
  295. I915_WRITE_FW(RING_ELSP(ring), desc[3]);
  296. /* The context is automatically loaded after the following */
  297. I915_WRITE_FW(RING_ELSP(ring), desc[2]);
  298. /* ELSP is a wo register, so use another nearby reg for posting instead */
  299. POSTING_READ_FW(RING_EXECLIST_STATUS(ring));
  300. intel_uncore_forcewake_put__locked(dev_priv, FORCEWAKE_ALL);
  301. spin_unlock(&dev_priv->uncore.lock);
  302. }
  303. static int execlists_update_context(struct drm_i915_gem_object *ctx_obj,
  304. struct drm_i915_gem_object *ring_obj,
  305. struct i915_hw_ppgtt *ppgtt,
  306. u32 tail)
  307. {
  308. struct page *page;
  309. uint32_t *reg_state;
  310. page = i915_gem_object_get_page(ctx_obj, 1);
  311. reg_state = kmap_atomic(page);
  312. reg_state[CTX_RING_TAIL+1] = tail;
  313. reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(ring_obj);
  314. /* True PPGTT with dynamic page allocation: update PDP registers and
  315. * point the unallocated PDPs to the scratch page
  316. */
  317. if (ppgtt) {
  318. ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
  319. ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
  320. ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
  321. ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
  322. }
  323. kunmap_atomic(reg_state);
  324. return 0;
  325. }
  326. static void execlists_submit_contexts(struct intel_engine_cs *ring,
  327. struct intel_context *to0, u32 tail0,
  328. struct intel_context *to1, u32 tail1)
  329. {
  330. struct drm_i915_gem_object *ctx_obj0 = to0->engine[ring->id].state;
  331. struct intel_ringbuffer *ringbuf0 = to0->engine[ring->id].ringbuf;
  332. struct drm_i915_gem_object *ctx_obj1 = NULL;
  333. struct intel_ringbuffer *ringbuf1 = NULL;
  334. BUG_ON(!ctx_obj0);
  335. WARN_ON(!i915_gem_obj_is_pinned(ctx_obj0));
  336. WARN_ON(!i915_gem_obj_is_pinned(ringbuf0->obj));
  337. execlists_update_context(ctx_obj0, ringbuf0->obj, to0->ppgtt, tail0);
  338. if (to1) {
  339. ringbuf1 = to1->engine[ring->id].ringbuf;
  340. ctx_obj1 = to1->engine[ring->id].state;
  341. BUG_ON(!ctx_obj1);
  342. WARN_ON(!i915_gem_obj_is_pinned(ctx_obj1));
  343. WARN_ON(!i915_gem_obj_is_pinned(ringbuf1->obj));
  344. execlists_update_context(ctx_obj1, ringbuf1->obj, to1->ppgtt, tail1);
  345. }
  346. execlists_elsp_write(ring, ctx_obj0, ctx_obj1);
  347. }
  348. static void execlists_context_unqueue(struct intel_engine_cs *ring)
  349. {
  350. struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
  351. struct drm_i915_gem_request *cursor = NULL, *tmp = NULL;
  352. assert_spin_locked(&ring->execlist_lock);
  353. /*
  354. * If irqs are not active generate a warning as batches that finish
  355. * without the irqs may get lost and a GPU Hang may occur.
  356. */
  357. WARN_ON(!intel_irqs_enabled(ring->dev->dev_private));
  358. if (list_empty(&ring->execlist_queue))
  359. return;
  360. /* Try to read in pairs */
  361. list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
  362. execlist_link) {
  363. if (!req0) {
  364. req0 = cursor;
  365. } else if (req0->ctx == cursor->ctx) {
  366. /* Same ctx: ignore first request, as second request
  367. * will update tail past first request's workload */
  368. cursor->elsp_submitted = req0->elsp_submitted;
  369. list_del(&req0->execlist_link);
  370. list_add_tail(&req0->execlist_link,
  371. &ring->execlist_retired_req_list);
  372. req0 = cursor;
  373. } else {
  374. req1 = cursor;
  375. break;
  376. }
  377. }
  378. if (IS_GEN8(ring->dev) || IS_GEN9(ring->dev)) {
  379. /*
  380. * WaIdleLiteRestore: make sure we never cause a lite
  381. * restore with HEAD==TAIL
  382. */
  383. if (req0->elsp_submitted) {
  384. /*
  385. * Apply the wa NOOPS to prevent ring:HEAD == req:TAIL
  386. * as we resubmit the request. See gen8_emit_request()
  387. * for where we prepare the padding after the end of the
  388. * request.
  389. */
  390. struct intel_ringbuffer *ringbuf;
  391. ringbuf = req0->ctx->engine[ring->id].ringbuf;
  392. req0->tail += 8;
  393. req0->tail &= ringbuf->size - 1;
  394. }
  395. }
  396. WARN_ON(req1 && req1->elsp_submitted);
  397. execlists_submit_contexts(ring, req0->ctx, req0->tail,
  398. req1 ? req1->ctx : NULL,
  399. req1 ? req1->tail : 0);
  400. req0->elsp_submitted++;
  401. if (req1)
  402. req1->elsp_submitted++;
  403. }
  404. static bool execlists_check_remove_request(struct intel_engine_cs *ring,
  405. u32 request_id)
  406. {
  407. struct drm_i915_gem_request *head_req;
  408. assert_spin_locked(&ring->execlist_lock);
  409. head_req = list_first_entry_or_null(&ring->execlist_queue,
  410. struct drm_i915_gem_request,
  411. execlist_link);
  412. if (head_req != NULL) {
  413. struct drm_i915_gem_object *ctx_obj =
  414. head_req->ctx->engine[ring->id].state;
  415. if (intel_execlists_ctx_id(ctx_obj) == request_id) {
  416. WARN(head_req->elsp_submitted == 0,
  417. "Never submitted head request\n");
  418. if (--head_req->elsp_submitted <= 0) {
  419. list_del(&head_req->execlist_link);
  420. list_add_tail(&head_req->execlist_link,
  421. &ring->execlist_retired_req_list);
  422. return true;
  423. }
  424. }
  425. }
  426. return false;
  427. }
  428. /**
  429. * intel_lrc_irq_handler() - handle Context Switch interrupts
  430. * @ring: Engine Command Streamer to handle.
  431. *
  432. * Check the unread Context Status Buffers and manage the submission of new
  433. * contexts to the ELSP accordingly.
  434. */
  435. void intel_lrc_irq_handler(struct intel_engine_cs *ring)
  436. {
  437. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  438. u32 status_pointer;
  439. u8 read_pointer;
  440. u8 write_pointer;
  441. u32 status;
  442. u32 status_id;
  443. u32 submit_contexts = 0;
  444. status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
  445. read_pointer = ring->next_context_status_buffer;
  446. write_pointer = status_pointer & 0x07;
  447. if (read_pointer > write_pointer)
  448. write_pointer += 6;
  449. spin_lock(&ring->execlist_lock);
  450. while (read_pointer < write_pointer) {
  451. read_pointer++;
  452. status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
  453. (read_pointer % 6) * 8);
  454. status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
  455. (read_pointer % 6) * 8 + 4);
  456. if (status & GEN8_CTX_STATUS_PREEMPTED) {
  457. if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
  458. if (execlists_check_remove_request(ring, status_id))
  459. WARN(1, "Lite Restored request removed from queue\n");
  460. } else
  461. WARN(1, "Preemption without Lite Restore\n");
  462. }
  463. if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
  464. (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
  465. if (execlists_check_remove_request(ring, status_id))
  466. submit_contexts++;
  467. }
  468. }
  469. if (submit_contexts != 0)
  470. execlists_context_unqueue(ring);
  471. spin_unlock(&ring->execlist_lock);
  472. WARN(submit_contexts > 2, "More than two context complete events?\n");
  473. ring->next_context_status_buffer = write_pointer % 6;
  474. I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
  475. ((u32)ring->next_context_status_buffer & 0x07) << 8);
  476. }
  477. static int execlists_context_queue(struct intel_engine_cs *ring,
  478. struct intel_context *to,
  479. u32 tail,
  480. struct drm_i915_gem_request *request)
  481. {
  482. struct drm_i915_gem_request *cursor;
  483. int num_elements = 0;
  484. if (to != ring->default_context)
  485. intel_lr_context_pin(ring, to);
  486. if (!request) {
  487. /*
  488. * If there isn't a request associated with this submission,
  489. * create one as a temporary holder.
  490. */
  491. request = kzalloc(sizeof(*request), GFP_KERNEL);
  492. if (request == NULL)
  493. return -ENOMEM;
  494. request->ring = ring;
  495. request->ctx = to;
  496. kref_init(&request->ref);
  497. i915_gem_context_reference(request->ctx);
  498. } else {
  499. i915_gem_request_reference(request);
  500. WARN_ON(to != request->ctx);
  501. }
  502. request->tail = tail;
  503. spin_lock_irq(&ring->execlist_lock);
  504. list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
  505. if (++num_elements > 2)
  506. break;
  507. if (num_elements > 2) {
  508. struct drm_i915_gem_request *tail_req;
  509. tail_req = list_last_entry(&ring->execlist_queue,
  510. struct drm_i915_gem_request,
  511. execlist_link);
  512. if (to == tail_req->ctx) {
  513. WARN(tail_req->elsp_submitted != 0,
  514. "More than 2 already-submitted reqs queued\n");
  515. list_del(&tail_req->execlist_link);
  516. list_add_tail(&tail_req->execlist_link,
  517. &ring->execlist_retired_req_list);
  518. }
  519. }
  520. list_add_tail(&request->execlist_link, &ring->execlist_queue);
  521. if (num_elements == 0)
  522. execlists_context_unqueue(ring);
  523. spin_unlock_irq(&ring->execlist_lock);
  524. return 0;
  525. }
  526. static int logical_ring_invalidate_all_caches(struct intel_ringbuffer *ringbuf,
  527. struct intel_context *ctx)
  528. {
  529. struct intel_engine_cs *ring = ringbuf->ring;
  530. uint32_t flush_domains;
  531. int ret;
  532. flush_domains = 0;
  533. if (ring->gpu_caches_dirty)
  534. flush_domains = I915_GEM_GPU_DOMAINS;
  535. ret = ring->emit_flush(ringbuf, ctx,
  536. I915_GEM_GPU_DOMAINS, flush_domains);
  537. if (ret)
  538. return ret;
  539. ring->gpu_caches_dirty = false;
  540. return 0;
  541. }
  542. static int execlists_move_to_gpu(struct intel_ringbuffer *ringbuf,
  543. struct intel_context *ctx,
  544. struct list_head *vmas)
  545. {
  546. struct intel_engine_cs *ring = ringbuf->ring;
  547. const unsigned other_rings = ~intel_ring_flag(ring);
  548. struct i915_vma *vma;
  549. uint32_t flush_domains = 0;
  550. bool flush_chipset = false;
  551. int ret;
  552. list_for_each_entry(vma, vmas, exec_list) {
  553. struct drm_i915_gem_object *obj = vma->obj;
  554. if (obj->active & other_rings) {
  555. ret = i915_gem_object_sync(obj, ring);
  556. if (ret)
  557. return ret;
  558. }
  559. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  560. flush_chipset |= i915_gem_clflush_object(obj, false);
  561. flush_domains |= obj->base.write_domain;
  562. }
  563. if (flush_domains & I915_GEM_DOMAIN_GTT)
  564. wmb();
  565. /* Unconditionally invalidate gpu caches and ensure that we do flush
  566. * any residual writes from the previous batch.
  567. */
  568. return logical_ring_invalidate_all_caches(ringbuf, ctx);
  569. }
  570. int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request,
  571. struct intel_context *ctx)
  572. {
  573. int ret;
  574. if (ctx != request->ring->default_context) {
  575. ret = intel_lr_context_pin(request->ring, ctx);
  576. if (ret)
  577. return ret;
  578. }
  579. request->ringbuf = ctx->engine[request->ring->id].ringbuf;
  580. request->ctx = ctx;
  581. i915_gem_context_reference(request->ctx);
  582. return 0;
  583. }
  584. static int logical_ring_wait_for_space(struct intel_ringbuffer *ringbuf,
  585. struct intel_context *ctx,
  586. int bytes)
  587. {
  588. struct intel_engine_cs *ring = ringbuf->ring;
  589. struct drm_i915_gem_request *request;
  590. unsigned space;
  591. int ret;
  592. if (intel_ring_space(ringbuf) >= bytes)
  593. return 0;
  594. list_for_each_entry(request, &ring->request_list, list) {
  595. /*
  596. * The request queue is per-engine, so can contain requests
  597. * from multiple ringbuffers. Here, we must ignore any that
  598. * aren't from the ringbuffer we're considering.
  599. */
  600. if (request->ringbuf != ringbuf)
  601. continue;
  602. /* Would completion of this request free enough space? */
  603. space = __intel_ring_space(request->postfix, ringbuf->tail,
  604. ringbuf->size);
  605. if (space >= bytes)
  606. break;
  607. }
  608. if (WARN_ON(&request->list == &ring->request_list))
  609. return -ENOSPC;
  610. ret = i915_wait_request(request);
  611. if (ret)
  612. return ret;
  613. ringbuf->space = space;
  614. return 0;
  615. }
  616. /*
  617. * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
  618. * @ringbuf: Logical Ringbuffer to advance.
  619. *
  620. * The tail is updated in our logical ringbuffer struct, not in the actual context. What
  621. * really happens during submission is that the context and current tail will be placed
  622. * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
  623. * point, the tail *inside* the context is updated and the ELSP written to.
  624. */
  625. static void
  626. intel_logical_ring_advance_and_submit(struct intel_ringbuffer *ringbuf,
  627. struct intel_context *ctx,
  628. struct drm_i915_gem_request *request)
  629. {
  630. struct intel_engine_cs *ring = ringbuf->ring;
  631. intel_logical_ring_advance(ringbuf);
  632. if (intel_ring_stopped(ring))
  633. return;
  634. execlists_context_queue(ring, ctx, ringbuf->tail, request);
  635. }
  636. static int logical_ring_wrap_buffer(struct intel_ringbuffer *ringbuf,
  637. struct intel_context *ctx)
  638. {
  639. uint32_t __iomem *virt;
  640. int rem = ringbuf->size - ringbuf->tail;
  641. if (ringbuf->space < rem) {
  642. int ret = logical_ring_wait_for_space(ringbuf, ctx, rem);
  643. if (ret)
  644. return ret;
  645. }
  646. virt = ringbuf->virtual_start + ringbuf->tail;
  647. rem /= 4;
  648. while (rem--)
  649. iowrite32(MI_NOOP, virt++);
  650. ringbuf->tail = 0;
  651. intel_ring_update_space(ringbuf);
  652. return 0;
  653. }
  654. static int logical_ring_prepare(struct intel_ringbuffer *ringbuf,
  655. struct intel_context *ctx, int bytes)
  656. {
  657. int ret;
  658. if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
  659. ret = logical_ring_wrap_buffer(ringbuf, ctx);
  660. if (unlikely(ret))
  661. return ret;
  662. }
  663. if (unlikely(ringbuf->space < bytes)) {
  664. ret = logical_ring_wait_for_space(ringbuf, ctx, bytes);
  665. if (unlikely(ret))
  666. return ret;
  667. }
  668. return 0;
  669. }
  670. /**
  671. * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
  672. *
  673. * @ringbuf: Logical ringbuffer.
  674. * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
  675. *
  676. * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
  677. * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
  678. * and also preallocates a request (every workload submission is still mediated through
  679. * requests, same as it did with legacy ringbuffer submission).
  680. *
  681. * Return: non-zero if the ringbuffer is not ready to be written to.
  682. */
  683. static int intel_logical_ring_begin(struct intel_ringbuffer *ringbuf,
  684. struct intel_context *ctx, int num_dwords)
  685. {
  686. struct intel_engine_cs *ring = ringbuf->ring;
  687. struct drm_device *dev = ring->dev;
  688. struct drm_i915_private *dev_priv = dev->dev_private;
  689. int ret;
  690. ret = i915_gem_check_wedge(&dev_priv->gpu_error,
  691. dev_priv->mm.interruptible);
  692. if (ret)
  693. return ret;
  694. ret = logical_ring_prepare(ringbuf, ctx, num_dwords * sizeof(uint32_t));
  695. if (ret)
  696. return ret;
  697. /* Preallocate the olr before touching the ring */
  698. ret = i915_gem_request_alloc(ring, ctx);
  699. if (ret)
  700. return ret;
  701. ringbuf->space -= num_dwords * sizeof(uint32_t);
  702. return 0;
  703. }
  704. /**
  705. * execlists_submission() - submit a batchbuffer for execution, Execlists style
  706. * @dev: DRM device.
  707. * @file: DRM file.
  708. * @ring: Engine Command Streamer to submit to.
  709. * @ctx: Context to employ for this submission.
  710. * @args: execbuffer call arguments.
  711. * @vmas: list of vmas.
  712. * @batch_obj: the batchbuffer to submit.
  713. * @exec_start: batchbuffer start virtual address pointer.
  714. * @dispatch_flags: translated execbuffer call flags.
  715. *
  716. * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
  717. * away the submission details of the execbuffer ioctl call.
  718. *
  719. * Return: non-zero if the submission fails.
  720. */
  721. int intel_execlists_submission(struct drm_device *dev, struct drm_file *file,
  722. struct intel_engine_cs *ring,
  723. struct intel_context *ctx,
  724. struct drm_i915_gem_execbuffer2 *args,
  725. struct list_head *vmas,
  726. struct drm_i915_gem_object *batch_obj,
  727. u64 exec_start, u32 dispatch_flags)
  728. {
  729. struct drm_i915_private *dev_priv = dev->dev_private;
  730. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  731. int instp_mode;
  732. u32 instp_mask;
  733. int ret;
  734. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  735. instp_mask = I915_EXEC_CONSTANTS_MASK;
  736. switch (instp_mode) {
  737. case I915_EXEC_CONSTANTS_REL_GENERAL:
  738. case I915_EXEC_CONSTANTS_ABSOLUTE:
  739. case I915_EXEC_CONSTANTS_REL_SURFACE:
  740. if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
  741. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  742. return -EINVAL;
  743. }
  744. if (instp_mode != dev_priv->relative_constants_mode) {
  745. if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  746. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  747. return -EINVAL;
  748. }
  749. /* The HW changed the meaning on this bit on gen6 */
  750. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  751. }
  752. break;
  753. default:
  754. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  755. return -EINVAL;
  756. }
  757. if (args->num_cliprects != 0) {
  758. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  759. return -EINVAL;
  760. } else {
  761. if (args->DR4 == 0xffffffff) {
  762. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  763. args->DR4 = 0;
  764. }
  765. if (args->DR1 || args->DR4 || args->cliprects_ptr) {
  766. DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
  767. return -EINVAL;
  768. }
  769. }
  770. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  771. DRM_DEBUG("sol reset is gen7 only\n");
  772. return -EINVAL;
  773. }
  774. ret = execlists_move_to_gpu(ringbuf, ctx, vmas);
  775. if (ret)
  776. return ret;
  777. if (ring == &dev_priv->ring[RCS] &&
  778. instp_mode != dev_priv->relative_constants_mode) {
  779. ret = intel_logical_ring_begin(ringbuf, ctx, 4);
  780. if (ret)
  781. return ret;
  782. intel_logical_ring_emit(ringbuf, MI_NOOP);
  783. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
  784. intel_logical_ring_emit(ringbuf, INSTPM);
  785. intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
  786. intel_logical_ring_advance(ringbuf);
  787. dev_priv->relative_constants_mode = instp_mode;
  788. }
  789. ret = ring->emit_bb_start(ringbuf, ctx, exec_start, dispatch_flags);
  790. if (ret)
  791. return ret;
  792. trace_i915_gem_ring_dispatch(intel_ring_get_request(ring), dispatch_flags);
  793. i915_gem_execbuffer_move_to_active(vmas, ring);
  794. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  795. return 0;
  796. }
  797. void intel_execlists_retire_requests(struct intel_engine_cs *ring)
  798. {
  799. struct drm_i915_gem_request *req, *tmp;
  800. struct list_head retired_list;
  801. WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  802. if (list_empty(&ring->execlist_retired_req_list))
  803. return;
  804. INIT_LIST_HEAD(&retired_list);
  805. spin_lock_irq(&ring->execlist_lock);
  806. list_replace_init(&ring->execlist_retired_req_list, &retired_list);
  807. spin_unlock_irq(&ring->execlist_lock);
  808. list_for_each_entry_safe(req, tmp, &retired_list, execlist_link) {
  809. struct intel_context *ctx = req->ctx;
  810. struct drm_i915_gem_object *ctx_obj =
  811. ctx->engine[ring->id].state;
  812. if (ctx_obj && (ctx != ring->default_context))
  813. intel_lr_context_unpin(ring, ctx);
  814. list_del(&req->execlist_link);
  815. i915_gem_request_unreference(req);
  816. }
  817. }
  818. void intel_logical_ring_stop(struct intel_engine_cs *ring)
  819. {
  820. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  821. int ret;
  822. if (!intel_ring_initialized(ring))
  823. return;
  824. ret = intel_ring_idle(ring);
  825. if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
  826. DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
  827. ring->name, ret);
  828. /* TODO: Is this correct with Execlists enabled? */
  829. I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
  830. if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
  831. DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
  832. return;
  833. }
  834. I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
  835. }
  836. int logical_ring_flush_all_caches(struct intel_ringbuffer *ringbuf,
  837. struct intel_context *ctx)
  838. {
  839. struct intel_engine_cs *ring = ringbuf->ring;
  840. int ret;
  841. if (!ring->gpu_caches_dirty)
  842. return 0;
  843. ret = ring->emit_flush(ringbuf, ctx, 0, I915_GEM_GPU_DOMAINS);
  844. if (ret)
  845. return ret;
  846. ring->gpu_caches_dirty = false;
  847. return 0;
  848. }
  849. static int intel_lr_context_pin(struct intel_engine_cs *ring,
  850. struct intel_context *ctx)
  851. {
  852. struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
  853. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  854. int ret = 0;
  855. WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  856. if (ctx->engine[ring->id].pin_count++ == 0) {
  857. ret = i915_gem_obj_ggtt_pin(ctx_obj,
  858. GEN8_LR_CONTEXT_ALIGN, 0);
  859. if (ret)
  860. goto reset_pin_count;
  861. ret = intel_pin_and_map_ringbuffer_obj(ring->dev, ringbuf);
  862. if (ret)
  863. goto unpin_ctx_obj;
  864. ctx_obj->dirty = true;
  865. }
  866. return ret;
  867. unpin_ctx_obj:
  868. i915_gem_object_ggtt_unpin(ctx_obj);
  869. reset_pin_count:
  870. ctx->engine[ring->id].pin_count = 0;
  871. return ret;
  872. }
  873. void intel_lr_context_unpin(struct intel_engine_cs *ring,
  874. struct intel_context *ctx)
  875. {
  876. struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
  877. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  878. if (ctx_obj) {
  879. WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  880. if (--ctx->engine[ring->id].pin_count == 0) {
  881. intel_unpin_ringbuffer_obj(ringbuf);
  882. i915_gem_object_ggtt_unpin(ctx_obj);
  883. }
  884. }
  885. }
  886. static int intel_logical_ring_workarounds_emit(struct intel_engine_cs *ring,
  887. struct intel_context *ctx)
  888. {
  889. int ret, i;
  890. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  891. struct drm_device *dev = ring->dev;
  892. struct drm_i915_private *dev_priv = dev->dev_private;
  893. struct i915_workarounds *w = &dev_priv->workarounds;
  894. if (WARN_ON_ONCE(w->count == 0))
  895. return 0;
  896. ring->gpu_caches_dirty = true;
  897. ret = logical_ring_flush_all_caches(ringbuf, ctx);
  898. if (ret)
  899. return ret;
  900. ret = intel_logical_ring_begin(ringbuf, ctx, w->count * 2 + 2);
  901. if (ret)
  902. return ret;
  903. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
  904. for (i = 0; i < w->count; i++) {
  905. intel_logical_ring_emit(ringbuf, w->reg[i].addr);
  906. intel_logical_ring_emit(ringbuf, w->reg[i].value);
  907. }
  908. intel_logical_ring_emit(ringbuf, MI_NOOP);
  909. intel_logical_ring_advance(ringbuf);
  910. ring->gpu_caches_dirty = true;
  911. ret = logical_ring_flush_all_caches(ringbuf, ctx);
  912. if (ret)
  913. return ret;
  914. return 0;
  915. }
  916. static int gen8_init_common_ring(struct intel_engine_cs *ring)
  917. {
  918. struct drm_device *dev = ring->dev;
  919. struct drm_i915_private *dev_priv = dev->dev_private;
  920. I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
  921. I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
  922. if (ring->status_page.obj) {
  923. I915_WRITE(RING_HWS_PGA(ring->mmio_base),
  924. (u32)ring->status_page.gfx_addr);
  925. POSTING_READ(RING_HWS_PGA(ring->mmio_base));
  926. }
  927. I915_WRITE(RING_MODE_GEN7(ring),
  928. _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
  929. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  930. POSTING_READ(RING_MODE_GEN7(ring));
  931. ring->next_context_status_buffer = 0;
  932. DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
  933. memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
  934. return 0;
  935. }
  936. static int gen8_init_render_ring(struct intel_engine_cs *ring)
  937. {
  938. struct drm_device *dev = ring->dev;
  939. struct drm_i915_private *dev_priv = dev->dev_private;
  940. int ret;
  941. ret = gen8_init_common_ring(ring);
  942. if (ret)
  943. return ret;
  944. /* We need to disable the AsyncFlip performance optimisations in order
  945. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  946. * programmed to '1' on all products.
  947. *
  948. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  949. */
  950. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  951. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  952. return init_workarounds_ring(ring);
  953. }
  954. static int gen9_init_render_ring(struct intel_engine_cs *ring)
  955. {
  956. int ret;
  957. ret = gen8_init_common_ring(ring);
  958. if (ret)
  959. return ret;
  960. return init_workarounds_ring(ring);
  961. }
  962. static int gen8_emit_bb_start(struct intel_ringbuffer *ringbuf,
  963. struct intel_context *ctx,
  964. u64 offset, unsigned dispatch_flags)
  965. {
  966. bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
  967. int ret;
  968. ret = intel_logical_ring_begin(ringbuf, ctx, 4);
  969. if (ret)
  970. return ret;
  971. /* FIXME(BDW): Address space and security selectors. */
  972. intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
  973. intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
  974. intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
  975. intel_logical_ring_emit(ringbuf, MI_NOOP);
  976. intel_logical_ring_advance(ringbuf);
  977. return 0;
  978. }
  979. static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
  980. {
  981. struct drm_device *dev = ring->dev;
  982. struct drm_i915_private *dev_priv = dev->dev_private;
  983. unsigned long flags;
  984. if (WARN_ON(!intel_irqs_enabled(dev_priv)))
  985. return false;
  986. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  987. if (ring->irq_refcount++ == 0) {
  988. I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
  989. POSTING_READ(RING_IMR(ring->mmio_base));
  990. }
  991. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  992. return true;
  993. }
  994. static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
  995. {
  996. struct drm_device *dev = ring->dev;
  997. struct drm_i915_private *dev_priv = dev->dev_private;
  998. unsigned long flags;
  999. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  1000. if (--ring->irq_refcount == 0) {
  1001. I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
  1002. POSTING_READ(RING_IMR(ring->mmio_base));
  1003. }
  1004. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  1005. }
  1006. static int gen8_emit_flush(struct intel_ringbuffer *ringbuf,
  1007. struct intel_context *ctx,
  1008. u32 invalidate_domains,
  1009. u32 unused)
  1010. {
  1011. struct intel_engine_cs *ring = ringbuf->ring;
  1012. struct drm_device *dev = ring->dev;
  1013. struct drm_i915_private *dev_priv = dev->dev_private;
  1014. uint32_t cmd;
  1015. int ret;
  1016. ret = intel_logical_ring_begin(ringbuf, ctx, 4);
  1017. if (ret)
  1018. return ret;
  1019. cmd = MI_FLUSH_DW + 1;
  1020. /* We always require a command barrier so that subsequent
  1021. * commands, such as breadcrumb interrupts, are strictly ordered
  1022. * wrt the contents of the write cache being flushed to memory
  1023. * (and thus being coherent from the CPU).
  1024. */
  1025. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1026. if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
  1027. cmd |= MI_INVALIDATE_TLB;
  1028. if (ring == &dev_priv->ring[VCS])
  1029. cmd |= MI_INVALIDATE_BSD;
  1030. }
  1031. intel_logical_ring_emit(ringbuf, cmd);
  1032. intel_logical_ring_emit(ringbuf,
  1033. I915_GEM_HWS_SCRATCH_ADDR |
  1034. MI_FLUSH_DW_USE_GTT);
  1035. intel_logical_ring_emit(ringbuf, 0); /* upper addr */
  1036. intel_logical_ring_emit(ringbuf, 0); /* value */
  1037. intel_logical_ring_advance(ringbuf);
  1038. return 0;
  1039. }
  1040. static int gen8_emit_flush_render(struct intel_ringbuffer *ringbuf,
  1041. struct intel_context *ctx,
  1042. u32 invalidate_domains,
  1043. u32 flush_domains)
  1044. {
  1045. struct intel_engine_cs *ring = ringbuf->ring;
  1046. u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  1047. bool vf_flush_wa;
  1048. u32 flags = 0;
  1049. int ret;
  1050. flags |= PIPE_CONTROL_CS_STALL;
  1051. if (flush_domains) {
  1052. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  1053. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  1054. }
  1055. if (invalidate_domains) {
  1056. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  1057. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  1058. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  1059. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1060. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  1061. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  1062. flags |= PIPE_CONTROL_QW_WRITE;
  1063. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1064. }
  1065. /*
  1066. * On GEN9+ Before VF_CACHE_INVALIDATE we need to emit a NULL pipe
  1067. * control.
  1068. */
  1069. vf_flush_wa = INTEL_INFO(ring->dev)->gen >= 9 &&
  1070. flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1071. ret = intel_logical_ring_begin(ringbuf, ctx, vf_flush_wa ? 12 : 6);
  1072. if (ret)
  1073. return ret;
  1074. if (vf_flush_wa) {
  1075. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  1076. intel_logical_ring_emit(ringbuf, 0);
  1077. intel_logical_ring_emit(ringbuf, 0);
  1078. intel_logical_ring_emit(ringbuf, 0);
  1079. intel_logical_ring_emit(ringbuf, 0);
  1080. intel_logical_ring_emit(ringbuf, 0);
  1081. }
  1082. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  1083. intel_logical_ring_emit(ringbuf, flags);
  1084. intel_logical_ring_emit(ringbuf, scratch_addr);
  1085. intel_logical_ring_emit(ringbuf, 0);
  1086. intel_logical_ring_emit(ringbuf, 0);
  1087. intel_logical_ring_emit(ringbuf, 0);
  1088. intel_logical_ring_advance(ringbuf);
  1089. return 0;
  1090. }
  1091. static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
  1092. {
  1093. return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
  1094. }
  1095. static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
  1096. {
  1097. intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
  1098. }
  1099. static int gen8_emit_request(struct intel_ringbuffer *ringbuf,
  1100. struct drm_i915_gem_request *request)
  1101. {
  1102. struct intel_engine_cs *ring = ringbuf->ring;
  1103. u32 cmd;
  1104. int ret;
  1105. /*
  1106. * Reserve space for 2 NOOPs at the end of each request to be
  1107. * used as a workaround for not being allowed to do lite
  1108. * restore with HEAD==TAIL (WaIdleLiteRestore).
  1109. */
  1110. ret = intel_logical_ring_begin(ringbuf, request->ctx, 8);
  1111. if (ret)
  1112. return ret;
  1113. cmd = MI_STORE_DWORD_IMM_GEN4;
  1114. cmd |= MI_GLOBAL_GTT;
  1115. intel_logical_ring_emit(ringbuf, cmd);
  1116. intel_logical_ring_emit(ringbuf,
  1117. (ring->status_page.gfx_addr +
  1118. (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
  1119. intel_logical_ring_emit(ringbuf, 0);
  1120. intel_logical_ring_emit(ringbuf,
  1121. i915_gem_request_get_seqno(ring->outstanding_lazy_request));
  1122. intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
  1123. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1124. intel_logical_ring_advance_and_submit(ringbuf, request->ctx, request);
  1125. /*
  1126. * Here we add two extra NOOPs as padding to avoid
  1127. * lite restore of a context with HEAD==TAIL.
  1128. */
  1129. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1130. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1131. intel_logical_ring_advance(ringbuf);
  1132. return 0;
  1133. }
  1134. static int intel_lr_context_render_state_init(struct intel_engine_cs *ring,
  1135. struct intel_context *ctx)
  1136. {
  1137. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  1138. struct render_state so;
  1139. struct drm_i915_file_private *file_priv = ctx->file_priv;
  1140. struct drm_file *file = file_priv ? file_priv->file : NULL;
  1141. int ret;
  1142. ret = i915_gem_render_state_prepare(ring, &so);
  1143. if (ret)
  1144. return ret;
  1145. if (so.rodata == NULL)
  1146. return 0;
  1147. ret = ring->emit_bb_start(ringbuf,
  1148. ctx,
  1149. so.ggtt_offset,
  1150. I915_DISPATCH_SECURE);
  1151. if (ret)
  1152. goto out;
  1153. i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), ring);
  1154. ret = __i915_add_request(ring, file, so.obj);
  1155. /* intel_logical_ring_add_request moves object to inactive if it
  1156. * fails */
  1157. out:
  1158. i915_gem_render_state_fini(&so);
  1159. return ret;
  1160. }
  1161. static int gen8_init_rcs_context(struct intel_engine_cs *ring,
  1162. struct intel_context *ctx)
  1163. {
  1164. int ret;
  1165. ret = intel_logical_ring_workarounds_emit(ring, ctx);
  1166. if (ret)
  1167. return ret;
  1168. return intel_lr_context_render_state_init(ring, ctx);
  1169. }
  1170. /**
  1171. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1172. *
  1173. * @ring: Engine Command Streamer.
  1174. *
  1175. */
  1176. void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
  1177. {
  1178. struct drm_i915_private *dev_priv;
  1179. if (!intel_ring_initialized(ring))
  1180. return;
  1181. dev_priv = ring->dev->dev_private;
  1182. intel_logical_ring_stop(ring);
  1183. WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
  1184. i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
  1185. if (ring->cleanup)
  1186. ring->cleanup(ring);
  1187. i915_cmd_parser_fini_ring(ring);
  1188. i915_gem_batch_pool_fini(&ring->batch_pool);
  1189. if (ring->status_page.obj) {
  1190. kunmap(sg_page(ring->status_page.obj->pages->sgl));
  1191. ring->status_page.obj = NULL;
  1192. }
  1193. }
  1194. static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
  1195. {
  1196. int ret;
  1197. /* Intentionally left blank. */
  1198. ring->buffer = NULL;
  1199. ring->dev = dev;
  1200. INIT_LIST_HEAD(&ring->active_list);
  1201. INIT_LIST_HEAD(&ring->request_list);
  1202. i915_gem_batch_pool_init(dev, &ring->batch_pool);
  1203. init_waitqueue_head(&ring->irq_queue);
  1204. INIT_LIST_HEAD(&ring->execlist_queue);
  1205. INIT_LIST_HEAD(&ring->execlist_retired_req_list);
  1206. spin_lock_init(&ring->execlist_lock);
  1207. ret = i915_cmd_parser_init_ring(ring);
  1208. if (ret)
  1209. return ret;
  1210. ret = intel_lr_context_deferred_create(ring->default_context, ring);
  1211. return ret;
  1212. }
  1213. static int logical_render_ring_init(struct drm_device *dev)
  1214. {
  1215. struct drm_i915_private *dev_priv = dev->dev_private;
  1216. struct intel_engine_cs *ring = &dev_priv->ring[RCS];
  1217. int ret;
  1218. ring->name = "render ring";
  1219. ring->id = RCS;
  1220. ring->mmio_base = RENDER_RING_BASE;
  1221. ring->irq_enable_mask =
  1222. GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
  1223. ring->irq_keep_mask =
  1224. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
  1225. if (HAS_L3_DPF(dev))
  1226. ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1227. if (INTEL_INFO(dev)->gen >= 9)
  1228. ring->init_hw = gen9_init_render_ring;
  1229. else
  1230. ring->init_hw = gen8_init_render_ring;
  1231. ring->init_context = gen8_init_rcs_context;
  1232. ring->cleanup = intel_fini_pipe_control;
  1233. ring->get_seqno = gen8_get_seqno;
  1234. ring->set_seqno = gen8_set_seqno;
  1235. ring->emit_request = gen8_emit_request;
  1236. ring->emit_flush = gen8_emit_flush_render;
  1237. ring->irq_get = gen8_logical_ring_get_irq;
  1238. ring->irq_put = gen8_logical_ring_put_irq;
  1239. ring->emit_bb_start = gen8_emit_bb_start;
  1240. ring->dev = dev;
  1241. ret = logical_ring_init(dev, ring);
  1242. if (ret)
  1243. return ret;
  1244. return intel_init_pipe_control(ring);
  1245. }
  1246. static int logical_bsd_ring_init(struct drm_device *dev)
  1247. {
  1248. struct drm_i915_private *dev_priv = dev->dev_private;
  1249. struct intel_engine_cs *ring = &dev_priv->ring[VCS];
  1250. ring->name = "bsd ring";
  1251. ring->id = VCS;
  1252. ring->mmio_base = GEN6_BSD_RING_BASE;
  1253. ring->irq_enable_mask =
  1254. GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1255. ring->irq_keep_mask =
  1256. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1257. ring->init_hw = gen8_init_common_ring;
  1258. ring->get_seqno = gen8_get_seqno;
  1259. ring->set_seqno = gen8_set_seqno;
  1260. ring->emit_request = gen8_emit_request;
  1261. ring->emit_flush = gen8_emit_flush;
  1262. ring->irq_get = gen8_logical_ring_get_irq;
  1263. ring->irq_put = gen8_logical_ring_put_irq;
  1264. ring->emit_bb_start = gen8_emit_bb_start;
  1265. return logical_ring_init(dev, ring);
  1266. }
  1267. static int logical_bsd2_ring_init(struct drm_device *dev)
  1268. {
  1269. struct drm_i915_private *dev_priv = dev->dev_private;
  1270. struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
  1271. ring->name = "bds2 ring";
  1272. ring->id = VCS2;
  1273. ring->mmio_base = GEN8_BSD2_RING_BASE;
  1274. ring->irq_enable_mask =
  1275. GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  1276. ring->irq_keep_mask =
  1277. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  1278. ring->init_hw = gen8_init_common_ring;
  1279. ring->get_seqno = gen8_get_seqno;
  1280. ring->set_seqno = gen8_set_seqno;
  1281. ring->emit_request = gen8_emit_request;
  1282. ring->emit_flush = gen8_emit_flush;
  1283. ring->irq_get = gen8_logical_ring_get_irq;
  1284. ring->irq_put = gen8_logical_ring_put_irq;
  1285. ring->emit_bb_start = gen8_emit_bb_start;
  1286. return logical_ring_init(dev, ring);
  1287. }
  1288. static int logical_blt_ring_init(struct drm_device *dev)
  1289. {
  1290. struct drm_i915_private *dev_priv = dev->dev_private;
  1291. struct intel_engine_cs *ring = &dev_priv->ring[BCS];
  1292. ring->name = "blitter ring";
  1293. ring->id = BCS;
  1294. ring->mmio_base = BLT_RING_BASE;
  1295. ring->irq_enable_mask =
  1296. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1297. ring->irq_keep_mask =
  1298. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1299. ring->init_hw = gen8_init_common_ring;
  1300. ring->get_seqno = gen8_get_seqno;
  1301. ring->set_seqno = gen8_set_seqno;
  1302. ring->emit_request = gen8_emit_request;
  1303. ring->emit_flush = gen8_emit_flush;
  1304. ring->irq_get = gen8_logical_ring_get_irq;
  1305. ring->irq_put = gen8_logical_ring_put_irq;
  1306. ring->emit_bb_start = gen8_emit_bb_start;
  1307. return logical_ring_init(dev, ring);
  1308. }
  1309. static int logical_vebox_ring_init(struct drm_device *dev)
  1310. {
  1311. struct drm_i915_private *dev_priv = dev->dev_private;
  1312. struct intel_engine_cs *ring = &dev_priv->ring[VECS];
  1313. ring->name = "video enhancement ring";
  1314. ring->id = VECS;
  1315. ring->mmio_base = VEBOX_RING_BASE;
  1316. ring->irq_enable_mask =
  1317. GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1318. ring->irq_keep_mask =
  1319. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1320. ring->init_hw = gen8_init_common_ring;
  1321. ring->get_seqno = gen8_get_seqno;
  1322. ring->set_seqno = gen8_set_seqno;
  1323. ring->emit_request = gen8_emit_request;
  1324. ring->emit_flush = gen8_emit_flush;
  1325. ring->irq_get = gen8_logical_ring_get_irq;
  1326. ring->irq_put = gen8_logical_ring_put_irq;
  1327. ring->emit_bb_start = gen8_emit_bb_start;
  1328. return logical_ring_init(dev, ring);
  1329. }
  1330. /**
  1331. * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
  1332. * @dev: DRM device.
  1333. *
  1334. * This function inits the engines for an Execlists submission style (the equivalent in the
  1335. * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
  1336. * those engines that are present in the hardware.
  1337. *
  1338. * Return: non-zero if the initialization failed.
  1339. */
  1340. int intel_logical_rings_init(struct drm_device *dev)
  1341. {
  1342. struct drm_i915_private *dev_priv = dev->dev_private;
  1343. int ret;
  1344. ret = logical_render_ring_init(dev);
  1345. if (ret)
  1346. return ret;
  1347. if (HAS_BSD(dev)) {
  1348. ret = logical_bsd_ring_init(dev);
  1349. if (ret)
  1350. goto cleanup_render_ring;
  1351. }
  1352. if (HAS_BLT(dev)) {
  1353. ret = logical_blt_ring_init(dev);
  1354. if (ret)
  1355. goto cleanup_bsd_ring;
  1356. }
  1357. if (HAS_VEBOX(dev)) {
  1358. ret = logical_vebox_ring_init(dev);
  1359. if (ret)
  1360. goto cleanup_blt_ring;
  1361. }
  1362. if (HAS_BSD2(dev)) {
  1363. ret = logical_bsd2_ring_init(dev);
  1364. if (ret)
  1365. goto cleanup_vebox_ring;
  1366. }
  1367. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  1368. if (ret)
  1369. goto cleanup_bsd2_ring;
  1370. return 0;
  1371. cleanup_bsd2_ring:
  1372. intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
  1373. cleanup_vebox_ring:
  1374. intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
  1375. cleanup_blt_ring:
  1376. intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
  1377. cleanup_bsd_ring:
  1378. intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
  1379. cleanup_render_ring:
  1380. intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
  1381. return ret;
  1382. }
  1383. static u32
  1384. make_rpcs(struct drm_device *dev)
  1385. {
  1386. u32 rpcs = 0;
  1387. /*
  1388. * No explicit RPCS request is needed to ensure full
  1389. * slice/subslice/EU enablement prior to Gen9.
  1390. */
  1391. if (INTEL_INFO(dev)->gen < 9)
  1392. return 0;
  1393. /*
  1394. * Starting in Gen9, render power gating can leave
  1395. * slice/subslice/EU in a partially enabled state. We
  1396. * must make an explicit request through RPCS for full
  1397. * enablement.
  1398. */
  1399. if (INTEL_INFO(dev)->has_slice_pg) {
  1400. rpcs |= GEN8_RPCS_S_CNT_ENABLE;
  1401. rpcs |= INTEL_INFO(dev)->slice_total <<
  1402. GEN8_RPCS_S_CNT_SHIFT;
  1403. rpcs |= GEN8_RPCS_ENABLE;
  1404. }
  1405. if (INTEL_INFO(dev)->has_subslice_pg) {
  1406. rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
  1407. rpcs |= INTEL_INFO(dev)->subslice_per_slice <<
  1408. GEN8_RPCS_SS_CNT_SHIFT;
  1409. rpcs |= GEN8_RPCS_ENABLE;
  1410. }
  1411. if (INTEL_INFO(dev)->has_eu_pg) {
  1412. rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
  1413. GEN8_RPCS_EU_MIN_SHIFT;
  1414. rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
  1415. GEN8_RPCS_EU_MAX_SHIFT;
  1416. rpcs |= GEN8_RPCS_ENABLE;
  1417. }
  1418. return rpcs;
  1419. }
  1420. static int
  1421. populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
  1422. struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
  1423. {
  1424. struct drm_device *dev = ring->dev;
  1425. struct drm_i915_private *dev_priv = dev->dev_private;
  1426. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
  1427. struct page *page;
  1428. uint32_t *reg_state;
  1429. int ret;
  1430. if (!ppgtt)
  1431. ppgtt = dev_priv->mm.aliasing_ppgtt;
  1432. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1433. if (ret) {
  1434. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1435. return ret;
  1436. }
  1437. ret = i915_gem_object_get_pages(ctx_obj);
  1438. if (ret) {
  1439. DRM_DEBUG_DRIVER("Could not get object pages\n");
  1440. return ret;
  1441. }
  1442. i915_gem_object_pin_pages(ctx_obj);
  1443. /* The second page of the context object contains some fields which must
  1444. * be set up prior to the first execution. */
  1445. page = i915_gem_object_get_page(ctx_obj, 1);
  1446. reg_state = kmap_atomic(page);
  1447. /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
  1448. * commands followed by (reg, value) pairs. The values we are setting here are
  1449. * only for the first context restore: on a subsequent save, the GPU will
  1450. * recreate this batchbuffer with new values (including all the missing
  1451. * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
  1452. if (ring->id == RCS)
  1453. reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
  1454. else
  1455. reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
  1456. reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
  1457. reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
  1458. reg_state[CTX_CONTEXT_CONTROL+1] =
  1459. _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
  1460. CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
  1461. reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
  1462. reg_state[CTX_RING_HEAD+1] = 0;
  1463. reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
  1464. reg_state[CTX_RING_TAIL+1] = 0;
  1465. reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
  1466. /* Ring buffer start address is not known until the buffer is pinned.
  1467. * It is written to the context image in execlists_update_context()
  1468. */
  1469. reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
  1470. reg_state[CTX_RING_BUFFER_CONTROL+1] =
  1471. ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
  1472. reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
  1473. reg_state[CTX_BB_HEAD_U+1] = 0;
  1474. reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
  1475. reg_state[CTX_BB_HEAD_L+1] = 0;
  1476. reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
  1477. reg_state[CTX_BB_STATE+1] = (1<<5);
  1478. reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
  1479. reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
  1480. reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
  1481. reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
  1482. reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
  1483. reg_state[CTX_SECOND_BB_STATE+1] = 0;
  1484. if (ring->id == RCS) {
  1485. /* TODO: according to BSpec, the register state context
  1486. * for CHV does not have these. OTOH, these registers do
  1487. * exist in CHV. I'm waiting for a clarification */
  1488. reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
  1489. reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
  1490. reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
  1491. reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
  1492. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
  1493. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
  1494. }
  1495. reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
  1496. reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
  1497. reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
  1498. reg_state[CTX_CTX_TIMESTAMP+1] = 0;
  1499. reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
  1500. reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
  1501. reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
  1502. reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
  1503. reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
  1504. reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
  1505. reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
  1506. reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
  1507. /* With dynamic page allocation, PDPs may not be allocated at this point,
  1508. * Point the unallocated PDPs to the scratch page
  1509. */
  1510. ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
  1511. ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
  1512. ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
  1513. ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
  1514. if (ring->id == RCS) {
  1515. reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  1516. reg_state[CTX_R_PWR_CLK_STATE] = GEN8_R_PWR_CLK_STATE;
  1517. reg_state[CTX_R_PWR_CLK_STATE+1] = make_rpcs(dev);
  1518. }
  1519. kunmap_atomic(reg_state);
  1520. ctx_obj->dirty = 1;
  1521. set_page_dirty(page);
  1522. i915_gem_object_unpin_pages(ctx_obj);
  1523. return 0;
  1524. }
  1525. /**
  1526. * intel_lr_context_free() - free the LRC specific bits of a context
  1527. * @ctx: the LR context to free.
  1528. *
  1529. * The real context freeing is done in i915_gem_context_free: this only
  1530. * takes care of the bits that are LRC related: the per-engine backing
  1531. * objects and the logical ringbuffer.
  1532. */
  1533. void intel_lr_context_free(struct intel_context *ctx)
  1534. {
  1535. int i;
  1536. for (i = 0; i < I915_NUM_RINGS; i++) {
  1537. struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
  1538. if (ctx_obj) {
  1539. struct intel_ringbuffer *ringbuf =
  1540. ctx->engine[i].ringbuf;
  1541. struct intel_engine_cs *ring = ringbuf->ring;
  1542. if (ctx == ring->default_context) {
  1543. intel_unpin_ringbuffer_obj(ringbuf);
  1544. i915_gem_object_ggtt_unpin(ctx_obj);
  1545. }
  1546. WARN_ON(ctx->engine[ring->id].pin_count);
  1547. intel_destroy_ringbuffer_obj(ringbuf);
  1548. kfree(ringbuf);
  1549. drm_gem_object_unreference(&ctx_obj->base);
  1550. }
  1551. }
  1552. }
  1553. static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
  1554. {
  1555. int ret = 0;
  1556. WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
  1557. switch (ring->id) {
  1558. case RCS:
  1559. if (INTEL_INFO(ring->dev)->gen >= 9)
  1560. ret = GEN9_LR_CONTEXT_RENDER_SIZE;
  1561. else
  1562. ret = GEN8_LR_CONTEXT_RENDER_SIZE;
  1563. break;
  1564. case VCS:
  1565. case BCS:
  1566. case VECS:
  1567. case VCS2:
  1568. ret = GEN8_LR_CONTEXT_OTHER_SIZE;
  1569. break;
  1570. }
  1571. return ret;
  1572. }
  1573. static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
  1574. struct drm_i915_gem_object *default_ctx_obj)
  1575. {
  1576. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1577. /* The status page is offset 0 from the default context object
  1578. * in LRC mode. */
  1579. ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
  1580. ring->status_page.page_addr =
  1581. kmap(sg_page(default_ctx_obj->pages->sgl));
  1582. ring->status_page.obj = default_ctx_obj;
  1583. I915_WRITE(RING_HWS_PGA(ring->mmio_base),
  1584. (u32)ring->status_page.gfx_addr);
  1585. POSTING_READ(RING_HWS_PGA(ring->mmio_base));
  1586. }
  1587. /**
  1588. * intel_lr_context_deferred_create() - create the LRC specific bits of a context
  1589. * @ctx: LR context to create.
  1590. * @ring: engine to be used with the context.
  1591. *
  1592. * This function can be called more than once, with different engines, if we plan
  1593. * to use the context with them. The context backing objects and the ringbuffers
  1594. * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
  1595. * the creation is a deferred call: it's better to make sure first that we need to use
  1596. * a given ring with the context.
  1597. *
  1598. * Return: non-zero on error.
  1599. */
  1600. int intel_lr_context_deferred_create(struct intel_context *ctx,
  1601. struct intel_engine_cs *ring)
  1602. {
  1603. const bool is_global_default_ctx = (ctx == ring->default_context);
  1604. struct drm_device *dev = ring->dev;
  1605. struct drm_i915_gem_object *ctx_obj;
  1606. uint32_t context_size;
  1607. struct intel_ringbuffer *ringbuf;
  1608. int ret;
  1609. WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
  1610. WARN_ON(ctx->engine[ring->id].state);
  1611. context_size = round_up(get_lr_context_size(ring), 4096);
  1612. ctx_obj = i915_gem_alloc_object(dev, context_size);
  1613. if (!ctx_obj) {
  1614. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
  1615. return -ENOMEM;
  1616. }
  1617. if (is_global_default_ctx) {
  1618. ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
  1619. if (ret) {
  1620. DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
  1621. ret);
  1622. drm_gem_object_unreference(&ctx_obj->base);
  1623. return ret;
  1624. }
  1625. }
  1626. ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
  1627. if (!ringbuf) {
  1628. DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
  1629. ring->name);
  1630. ret = -ENOMEM;
  1631. goto error_unpin_ctx;
  1632. }
  1633. ringbuf->ring = ring;
  1634. ringbuf->size = 32 * PAGE_SIZE;
  1635. ringbuf->effective_size = ringbuf->size;
  1636. ringbuf->head = 0;
  1637. ringbuf->tail = 0;
  1638. ringbuf->last_retired_head = -1;
  1639. intel_ring_update_space(ringbuf);
  1640. if (ringbuf->obj == NULL) {
  1641. ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
  1642. if (ret) {
  1643. DRM_DEBUG_DRIVER(
  1644. "Failed to allocate ringbuffer obj %s: %d\n",
  1645. ring->name, ret);
  1646. goto error_free_rbuf;
  1647. }
  1648. if (is_global_default_ctx) {
  1649. ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
  1650. if (ret) {
  1651. DRM_ERROR(
  1652. "Failed to pin and map ringbuffer %s: %d\n",
  1653. ring->name, ret);
  1654. goto error_destroy_rbuf;
  1655. }
  1656. }
  1657. }
  1658. ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
  1659. if (ret) {
  1660. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  1661. goto error;
  1662. }
  1663. ctx->engine[ring->id].ringbuf = ringbuf;
  1664. ctx->engine[ring->id].state = ctx_obj;
  1665. if (ctx == ring->default_context)
  1666. lrc_setup_hardware_status_page(ring, ctx_obj);
  1667. else if (ring->id == RCS && !ctx->rcs_initialized) {
  1668. if (ring->init_context) {
  1669. ret = ring->init_context(ring, ctx);
  1670. if (ret) {
  1671. DRM_ERROR("ring init context: %d\n", ret);
  1672. ctx->engine[ring->id].ringbuf = NULL;
  1673. ctx->engine[ring->id].state = NULL;
  1674. goto error;
  1675. }
  1676. }
  1677. ctx->rcs_initialized = true;
  1678. }
  1679. return 0;
  1680. error:
  1681. if (is_global_default_ctx)
  1682. intel_unpin_ringbuffer_obj(ringbuf);
  1683. error_destroy_rbuf:
  1684. intel_destroy_ringbuffer_obj(ringbuf);
  1685. error_free_rbuf:
  1686. kfree(ringbuf);
  1687. error_unpin_ctx:
  1688. if (is_global_default_ctx)
  1689. i915_gem_object_ggtt_unpin(ctx_obj);
  1690. drm_gem_object_unreference(&ctx_obj->base);
  1691. return ret;
  1692. }
  1693. void intel_lr_context_reset(struct drm_device *dev,
  1694. struct intel_context *ctx)
  1695. {
  1696. struct drm_i915_private *dev_priv = dev->dev_private;
  1697. struct intel_engine_cs *ring;
  1698. int i;
  1699. for_each_ring(ring, dev_priv, i) {
  1700. struct drm_i915_gem_object *ctx_obj =
  1701. ctx->engine[ring->id].state;
  1702. struct intel_ringbuffer *ringbuf =
  1703. ctx->engine[ring->id].ringbuf;
  1704. uint32_t *reg_state;
  1705. struct page *page;
  1706. if (!ctx_obj)
  1707. continue;
  1708. if (i915_gem_object_get_pages(ctx_obj)) {
  1709. WARN(1, "Failed get_pages for context obj\n");
  1710. continue;
  1711. }
  1712. page = i915_gem_object_get_page(ctx_obj, 1);
  1713. reg_state = kmap_atomic(page);
  1714. reg_state[CTX_RING_HEAD+1] = 0;
  1715. reg_state[CTX_RING_TAIL+1] = 0;
  1716. kunmap_atomic(reg_state);
  1717. ringbuf->head = 0;
  1718. ringbuf->tail = 0;
  1719. }
  1720. }