intel_lrc.c 76 KB

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