intel_lrc.c 69 KB

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