intel_lrc.c 73 KB

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