intel_lrc.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. /*
  2. * Copyright © 2014 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Ben Widawsky <ben@bwidawsk.net>
  25. * Michel Thierry <michel.thierry@intel.com>
  26. * Thomas Daniel <thomas.daniel@intel.com>
  27. * Oscar Mateo <oscar.mateo@intel.com>
  28. *
  29. */
  30. /**
  31. * DOC: Logical Rings, Logical Ring Contexts and Execlists
  32. *
  33. * Motivation:
  34. * GEN8 brings an expansion of the HW contexts: "Logical Ring Contexts".
  35. * These expanded contexts enable a number of new abilities, especially
  36. * "Execlists" (also implemented in this file).
  37. *
  38. * One of the main differences with the legacy HW contexts is that logical
  39. * ring contexts incorporate many more things to the context's state, like
  40. * PDPs or ringbuffer control registers:
  41. *
  42. * The reason why PDPs are included in the context is straightforward: as
  43. * PPGTTs (per-process GTTs) are actually per-context, having the PDPs
  44. * contained there mean you don't need to do a ppgtt->switch_mm yourself,
  45. * instead, the GPU will do it for you on the context switch.
  46. *
  47. * But, what about the ringbuffer control registers (head, tail, etc..)?
  48. * shouldn't we just need a set of those per engine command streamer? This is
  49. * where the name "Logical Rings" starts to make sense: by virtualizing the
  50. * rings, the engine cs shifts to a new "ring buffer" with every context
  51. * switch. When you want to submit a workload to the GPU you: A) choose your
  52. * context, B) find its appropriate virtualized ring, C) write commands to it
  53. * and then, finally, D) tell the GPU to switch to that context.
  54. *
  55. * Instead of the legacy MI_SET_CONTEXT, the way you tell the GPU to switch
  56. * to a contexts is via a context execution list, ergo "Execlists".
  57. *
  58. * LRC implementation:
  59. * Regarding the creation of contexts, we have:
  60. *
  61. * - One global default context.
  62. * - One local default context for each opened fd.
  63. * - One local extra context for each context create ioctl call.
  64. *
  65. * Now that ringbuffers belong per-context (and not per-engine, like before)
  66. * and that contexts are uniquely tied to a given engine (and not reusable,
  67. * like before) we need:
  68. *
  69. * - One ringbuffer per-engine inside each context.
  70. * - One backing object per-engine inside each context.
  71. *
  72. * The global default context starts its life with these new objects fully
  73. * allocated and populated. The local default context for each opened fd is
  74. * more complex, because we don't know at creation time which engine is going
  75. * to use them. To handle this, we have implemented a deferred creation of LR
  76. * contexts:
  77. *
  78. * The local context starts its life as a hollow or blank holder, that only
  79. * gets populated for a given engine once we receive an execbuffer. If later
  80. * on we receive another execbuffer ioctl for the same context but a different
  81. * engine, we allocate/populate a new ringbuffer and context backing object and
  82. * so on.
  83. *
  84. * Finally, regarding local contexts created using the ioctl call: as they are
  85. * only allowed with the render ring, we can allocate & populate them right
  86. * away (no need to defer anything, at least for now).
  87. *
  88. * Execlists implementation:
  89. * Execlists are the new method by which, on gen8+ hardware, workloads are
  90. * submitted for execution (as opposed to the legacy, ringbuffer-based, method).
  91. * This method works as follows:
  92. *
  93. * When a request is committed, its commands (the BB start and any leading or
  94. * trailing commands, like the seqno breadcrumbs) are placed in the ringbuffer
  95. * for the appropriate context. The tail pointer in the hardware context is not
  96. * updated at this time, but instead, kept by the driver in the ringbuffer
  97. * structure. A structure representing this request is added to a request queue
  98. * for the appropriate engine: this structure contains a copy of the context's
  99. * tail after the request was written to the ring buffer and a pointer to the
  100. * context itself.
  101. *
  102. * If the engine's request queue was empty before the request was added, the
  103. * queue is processed immediately. Otherwise the queue will be processed during
  104. * a context switch interrupt. In any case, elements on the queue will get sent
  105. * (in pairs) to the GPU's ExecLists Submit Port (ELSP, for short) with a
  106. * globally unique 20-bits submission ID.
  107. *
  108. * When execution of a request completes, the GPU updates the context status
  109. * buffer with a context complete event and generates a context switch interrupt.
  110. * During the interrupt handling, the driver examines the events in the buffer:
  111. * for each context complete event, if the announced ID matches that on the head
  112. * of the request queue, then that request is retired and removed from the queue.
  113. *
  114. * After processing, if any requests were retired and the queue is not empty
  115. * then a new execution list can be submitted. The two requests at the front of
  116. * the queue are next to be submitted but since a context may not occur twice in
  117. * an execution list, if subsequent requests have the same ID as the first then
  118. * the two requests must be combined. This is done simply by discarding requests
  119. * at the head of the queue until either only one requests is left (in which case
  120. * we use a NULL second context) or the first two requests have unique IDs.
  121. *
  122. * By always executing the first two requests in the queue the driver ensures
  123. * that the GPU is kept as busy as possible. In the case where a single context
  124. * completes but a second context is still executing, the request for this second
  125. * context will be at the head of the queue when we remove the first one. This
  126. * request will then be resubmitted along with a new request for a different context,
  127. * which will cause the hardware to continue executing the second request and queue
  128. * the new request (the GPU detects the condition of a context getting preempted
  129. * with the same context and optimizes the context switch flow by not doing
  130. * preemption, but just sampling the new tail pointer).
  131. *
  132. */
  133. #include <drm/drmP.h>
  134. #include <drm/i915_drm.h>
  135. #include "i915_drv.h"
  136. #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
  137. #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
  138. #define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
  139. #define RING_EXECLIST_QFULL (1 << 0x2)
  140. #define RING_EXECLIST1_VALID (1 << 0x3)
  141. #define RING_EXECLIST0_VALID (1 << 0x4)
  142. #define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
  143. #define RING_EXECLIST1_ACTIVE (1 << 0x11)
  144. #define RING_EXECLIST0_ACTIVE (1 << 0x12)
  145. #define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
  146. #define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
  147. #define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
  148. #define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
  149. #define GEN8_CTX_STATUS_COMPLETE (1 << 4)
  150. #define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
  151. #define CTX_LRI_HEADER_0 0x01
  152. #define CTX_CONTEXT_CONTROL 0x02
  153. #define CTX_RING_HEAD 0x04
  154. #define CTX_RING_TAIL 0x06
  155. #define CTX_RING_BUFFER_START 0x08
  156. #define CTX_RING_BUFFER_CONTROL 0x0a
  157. #define CTX_BB_HEAD_U 0x0c
  158. #define CTX_BB_HEAD_L 0x0e
  159. #define CTX_BB_STATE 0x10
  160. #define CTX_SECOND_BB_HEAD_U 0x12
  161. #define CTX_SECOND_BB_HEAD_L 0x14
  162. #define CTX_SECOND_BB_STATE 0x16
  163. #define CTX_BB_PER_CTX_PTR 0x18
  164. #define CTX_RCS_INDIRECT_CTX 0x1a
  165. #define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
  166. #define CTX_LRI_HEADER_1 0x21
  167. #define CTX_CTX_TIMESTAMP 0x22
  168. #define CTX_PDP3_UDW 0x24
  169. #define CTX_PDP3_LDW 0x26
  170. #define CTX_PDP2_UDW 0x28
  171. #define CTX_PDP2_LDW 0x2a
  172. #define CTX_PDP1_UDW 0x2c
  173. #define CTX_PDP1_LDW 0x2e
  174. #define CTX_PDP0_UDW 0x30
  175. #define CTX_PDP0_LDW 0x32
  176. #define CTX_LRI_HEADER_2 0x41
  177. #define CTX_R_PWR_CLK_STATE 0x42
  178. #define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
  179. #define GEN8_CTX_VALID (1<<0)
  180. #define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
  181. #define GEN8_CTX_FORCE_RESTORE (1<<2)
  182. #define GEN8_CTX_L3LLC_COHERENT (1<<5)
  183. #define GEN8_CTX_PRIVILEGE (1<<8)
  184. enum {
  185. ADVANCED_CONTEXT = 0,
  186. LEGACY_CONTEXT,
  187. ADVANCED_AD_CONTEXT,
  188. LEGACY_64B_CONTEXT
  189. };
  190. #define GEN8_CTX_MODE_SHIFT 3
  191. enum {
  192. FAULT_AND_HANG = 0,
  193. FAULT_AND_HALT, /* Debug only */
  194. FAULT_AND_STREAM,
  195. FAULT_AND_CONTINUE /* Unsupported */
  196. };
  197. #define GEN8_CTX_ID_SHIFT 32
  198. static int intel_lr_context_pin(struct intel_engine_cs *ring,
  199. struct intel_context *ctx);
  200. /**
  201. * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
  202. * @dev: DRM device.
  203. * @enable_execlists: value of i915.enable_execlists module parameter.
  204. *
  205. * Only certain platforms support Execlists (the prerequisites being
  206. * support for Logical Ring Contexts and Aliasing PPGTT or better).
  207. *
  208. * Return: 1 if Execlists is supported and has to be enabled.
  209. */
  210. int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists)
  211. {
  212. WARN_ON(i915.enable_ppgtt == -1);
  213. if (INTEL_INFO(dev)->gen >= 9)
  214. return 1;
  215. if (enable_execlists == 0)
  216. return 0;
  217. if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
  218. i915.use_mmio_flip >= 0)
  219. return 1;
  220. return 0;
  221. }
  222. /**
  223. * intel_execlists_ctx_id() - get the Execlists Context ID
  224. * @ctx_obj: Logical Ring Context backing object.
  225. *
  226. * Do not confuse with ctx->id! Unfortunately we have a name overload
  227. * here: the old context ID we pass to userspace as a handler so that
  228. * they can refer to a context, and the new context ID we pass to the
  229. * ELSP so that the GPU can inform us of the context status via
  230. * interrupts.
  231. *
  232. * Return: 20-bits globally unique context ID.
  233. */
  234. u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
  235. {
  236. u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
  237. /* LRCA is required to be 4K aligned so the more significant 20 bits
  238. * are globally unique */
  239. return lrca >> 12;
  240. }
  241. static uint64_t execlists_ctx_descriptor(struct drm_i915_gem_object *ctx_obj)
  242. {
  243. uint64_t desc;
  244. uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
  245. WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
  246. desc = GEN8_CTX_VALID;
  247. desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
  248. desc |= GEN8_CTX_L3LLC_COHERENT;
  249. desc |= GEN8_CTX_PRIVILEGE;
  250. desc |= lrca;
  251. desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
  252. /* TODO: WaDisableLiteRestore when we start using semaphore
  253. * signalling between Command Streamers */
  254. /* desc |= GEN8_CTX_FORCE_RESTORE; */
  255. return desc;
  256. }
  257. static void execlists_elsp_write(struct intel_engine_cs *ring,
  258. struct drm_i915_gem_object *ctx_obj0,
  259. struct drm_i915_gem_object *ctx_obj1)
  260. {
  261. struct drm_device *dev = ring->dev;
  262. struct drm_i915_private *dev_priv = dev->dev_private;
  263. uint64_t temp = 0;
  264. uint32_t desc[4];
  265. unsigned long flags;
  266. /* XXX: You must always write both descriptors in the order below. */
  267. if (ctx_obj1)
  268. temp = execlists_ctx_descriptor(ctx_obj1);
  269. else
  270. temp = 0;
  271. desc[1] = (u32)(temp >> 32);
  272. desc[0] = (u32)temp;
  273. temp = execlists_ctx_descriptor(ctx_obj0);
  274. desc[3] = (u32)(temp >> 32);
  275. desc[2] = (u32)temp;
  276. /* Set Force Wakeup bit to prevent GT from entering C6 while ELSP writes
  277. * are in progress.
  278. *
  279. * The other problem is that we can't just call gen6_gt_force_wake_get()
  280. * because that function calls intel_runtime_pm_get(), which might sleep.
  281. * Instead, we do the runtime_pm_get/put when creating/destroying requests.
  282. */
  283. spin_lock_irqsave(&dev_priv->uncore.lock, flags);
  284. if (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen >= 9) {
  285. if (dev_priv->uncore.fw_rendercount++ == 0)
  286. dev_priv->uncore.funcs.force_wake_get(dev_priv,
  287. FORCEWAKE_RENDER);
  288. if (dev_priv->uncore.fw_mediacount++ == 0)
  289. dev_priv->uncore.funcs.force_wake_get(dev_priv,
  290. FORCEWAKE_MEDIA);
  291. if (INTEL_INFO(dev)->gen >= 9) {
  292. if (dev_priv->uncore.fw_blittercount++ == 0)
  293. dev_priv->uncore.funcs.force_wake_get(dev_priv,
  294. FORCEWAKE_BLITTER);
  295. }
  296. } else {
  297. if (dev_priv->uncore.forcewake_count++ == 0)
  298. dev_priv->uncore.funcs.force_wake_get(dev_priv,
  299. FORCEWAKE_ALL);
  300. }
  301. spin_unlock_irqrestore(&dev_priv->uncore.lock, flags);
  302. I915_WRITE(RING_ELSP(ring), desc[1]);
  303. I915_WRITE(RING_ELSP(ring), desc[0]);
  304. I915_WRITE(RING_ELSP(ring), desc[3]);
  305. /* The context is automatically loaded after the following */
  306. I915_WRITE(RING_ELSP(ring), desc[2]);
  307. /* ELSP is a wo register, so use another nearby reg for posting instead */
  308. POSTING_READ(RING_EXECLIST_STATUS(ring));
  309. /* Release Force Wakeup (see the big comment above). */
  310. spin_lock_irqsave(&dev_priv->uncore.lock, flags);
  311. if (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen >= 9) {
  312. if (--dev_priv->uncore.fw_rendercount == 0)
  313. dev_priv->uncore.funcs.force_wake_put(dev_priv,
  314. FORCEWAKE_RENDER);
  315. if (--dev_priv->uncore.fw_mediacount == 0)
  316. dev_priv->uncore.funcs.force_wake_put(dev_priv,
  317. FORCEWAKE_MEDIA);
  318. if (INTEL_INFO(dev)->gen >= 9) {
  319. if (--dev_priv->uncore.fw_blittercount == 0)
  320. dev_priv->uncore.funcs.force_wake_put(dev_priv,
  321. FORCEWAKE_BLITTER);
  322. }
  323. } else {
  324. if (--dev_priv->uncore.forcewake_count == 0)
  325. dev_priv->uncore.funcs.force_wake_put(dev_priv,
  326. FORCEWAKE_ALL);
  327. }
  328. spin_unlock_irqrestore(&dev_priv->uncore.lock, flags);
  329. }
  330. static int execlists_update_context(struct drm_i915_gem_object *ctx_obj,
  331. struct drm_i915_gem_object *ring_obj,
  332. u32 tail)
  333. {
  334. struct page *page;
  335. uint32_t *reg_state;
  336. page = i915_gem_object_get_page(ctx_obj, 1);
  337. reg_state = kmap_atomic(page);
  338. reg_state[CTX_RING_TAIL+1] = tail;
  339. reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(ring_obj);
  340. kunmap_atomic(reg_state);
  341. return 0;
  342. }
  343. static void execlists_submit_contexts(struct intel_engine_cs *ring,
  344. struct intel_context *to0, u32 tail0,
  345. struct intel_context *to1, u32 tail1)
  346. {
  347. struct drm_i915_gem_object *ctx_obj0 = to0->engine[ring->id].state;
  348. struct intel_ringbuffer *ringbuf0 = to0->engine[ring->id].ringbuf;
  349. struct drm_i915_gem_object *ctx_obj1 = NULL;
  350. struct intel_ringbuffer *ringbuf1 = NULL;
  351. BUG_ON(!ctx_obj0);
  352. WARN_ON(!i915_gem_obj_is_pinned(ctx_obj0));
  353. WARN_ON(!i915_gem_obj_is_pinned(ringbuf0->obj));
  354. execlists_update_context(ctx_obj0, ringbuf0->obj, tail0);
  355. if (to1) {
  356. ringbuf1 = to1->engine[ring->id].ringbuf;
  357. ctx_obj1 = to1->engine[ring->id].state;
  358. BUG_ON(!ctx_obj1);
  359. WARN_ON(!i915_gem_obj_is_pinned(ctx_obj1));
  360. WARN_ON(!i915_gem_obj_is_pinned(ringbuf1->obj));
  361. execlists_update_context(ctx_obj1, ringbuf1->obj, tail1);
  362. }
  363. execlists_elsp_write(ring, ctx_obj0, ctx_obj1);
  364. }
  365. static void execlists_context_unqueue(struct intel_engine_cs *ring)
  366. {
  367. struct intel_ctx_submit_request *req0 = NULL, *req1 = NULL;
  368. struct intel_ctx_submit_request *cursor = NULL, *tmp = NULL;
  369. assert_spin_locked(&ring->execlist_lock);
  370. if (list_empty(&ring->execlist_queue))
  371. return;
  372. /* Try to read in pairs */
  373. list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
  374. execlist_link) {
  375. if (!req0) {
  376. req0 = cursor;
  377. } else if (req0->request->ctx == cursor->request->ctx) {
  378. /* Same ctx: ignore first request, as second request
  379. * will update tail past first request's workload */
  380. cursor->elsp_submitted = req0->elsp_submitted;
  381. list_del(&req0->execlist_link);
  382. list_add_tail(&req0->execlist_link,
  383. &ring->execlist_retired_req_list);
  384. req0 = cursor;
  385. } else {
  386. req1 = cursor;
  387. break;
  388. }
  389. }
  390. WARN_ON(req1 && req1->elsp_submitted);
  391. execlists_submit_contexts(ring, req0->request->ctx, req0->request->tail,
  392. req1 ? req1->request->ctx : NULL,
  393. req1 ? req1->request->tail : 0);
  394. req0->elsp_submitted++;
  395. if (req1)
  396. req1->elsp_submitted++;
  397. }
  398. static bool execlists_check_remove_request(struct intel_engine_cs *ring,
  399. u32 request_id)
  400. {
  401. struct intel_ctx_submit_request *head_req;
  402. assert_spin_locked(&ring->execlist_lock);
  403. head_req = list_first_entry_or_null(&ring->execlist_queue,
  404. struct intel_ctx_submit_request,
  405. execlist_link);
  406. if (head_req != NULL) {
  407. struct drm_i915_gem_object *ctx_obj =
  408. head_req->request->ctx->engine[ring->id].state;
  409. if (intel_execlists_ctx_id(ctx_obj) == request_id) {
  410. WARN(head_req->elsp_submitted == 0,
  411. "Never submitted head request\n");
  412. if (--head_req->elsp_submitted <= 0) {
  413. list_del(&head_req->execlist_link);
  414. list_add_tail(&head_req->execlist_link,
  415. &ring->execlist_retired_req_list);
  416. return true;
  417. }
  418. }
  419. }
  420. return false;
  421. }
  422. /**
  423. * intel_lrc_irq_handler() - handle Context Switch interrupts
  424. * @ring: Engine Command Streamer to handle.
  425. *
  426. * Check the unread Context Status Buffers and manage the submission of new
  427. * contexts to the ELSP accordingly.
  428. */
  429. void intel_lrc_irq_handler(struct intel_engine_cs *ring)
  430. {
  431. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  432. u32 status_pointer;
  433. u8 read_pointer;
  434. u8 write_pointer;
  435. u32 status;
  436. u32 status_id;
  437. u32 submit_contexts = 0;
  438. status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
  439. read_pointer = ring->next_context_status_buffer;
  440. write_pointer = status_pointer & 0x07;
  441. if (read_pointer > write_pointer)
  442. write_pointer += 6;
  443. spin_lock(&ring->execlist_lock);
  444. while (read_pointer < write_pointer) {
  445. read_pointer++;
  446. status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
  447. (read_pointer % 6) * 8);
  448. status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
  449. (read_pointer % 6) * 8 + 4);
  450. if (status & GEN8_CTX_STATUS_PREEMPTED) {
  451. if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
  452. if (execlists_check_remove_request(ring, status_id))
  453. WARN(1, "Lite Restored request removed from queue\n");
  454. } else
  455. WARN(1, "Preemption without Lite Restore\n");
  456. }
  457. if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
  458. (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
  459. if (execlists_check_remove_request(ring, status_id))
  460. submit_contexts++;
  461. }
  462. }
  463. if (submit_contexts != 0)
  464. execlists_context_unqueue(ring);
  465. spin_unlock(&ring->execlist_lock);
  466. WARN(submit_contexts > 2, "More than two context complete events?\n");
  467. ring->next_context_status_buffer = write_pointer % 6;
  468. I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
  469. ((u32)ring->next_context_status_buffer & 0x07) << 8);
  470. }
  471. static int execlists_context_queue(struct intel_engine_cs *ring,
  472. struct intel_context *to,
  473. u32 tail,
  474. struct drm_i915_gem_request *request)
  475. {
  476. struct intel_ctx_submit_request *req = NULL, *cursor;
  477. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  478. unsigned long flags;
  479. int num_elements = 0;
  480. req = kzalloc(sizeof(*req), GFP_KERNEL);
  481. if (req == NULL)
  482. return -ENOMEM;
  483. if (to != ring->default_context)
  484. intel_lr_context_pin(ring, to);
  485. if (!request) {
  486. /*
  487. * If there isn't a request associated with this submission,
  488. * create one as a temporary holder.
  489. */
  490. WARN(1, "execlist context submission without request");
  491. request = kzalloc(sizeof(*request), GFP_KERNEL);
  492. if (request == NULL)
  493. return -ENOMEM;
  494. request->ring = ring;
  495. }
  496. request->ctx = to;
  497. request->tail = tail;
  498. req->request = request;
  499. i915_gem_request_reference(request);
  500. i915_gem_context_reference(req->request->ctx);
  501. intel_runtime_pm_get(dev_priv);
  502. spin_lock_irqsave(&ring->execlist_lock, flags);
  503. list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
  504. if (++num_elements > 2)
  505. break;
  506. if (num_elements > 2) {
  507. struct intel_ctx_submit_request *tail_req;
  508. tail_req = list_last_entry(&ring->execlist_queue,
  509. struct intel_ctx_submit_request,
  510. execlist_link);
  511. if (to == tail_req->request->ctx) {
  512. WARN(tail_req->elsp_submitted != 0,
  513. "More than 2 already-submitted reqs queued\n");
  514. list_del(&tail_req->execlist_link);
  515. list_add_tail(&tail_req->execlist_link,
  516. &ring->execlist_retired_req_list);
  517. }
  518. }
  519. list_add_tail(&req->execlist_link, &ring->execlist_queue);
  520. if (num_elements == 0)
  521. execlists_context_unqueue(ring);
  522. spin_unlock_irqrestore(&ring->execlist_lock, flags);
  523. return 0;
  524. }
  525. static int logical_ring_invalidate_all_caches(struct intel_ringbuffer *ringbuf)
  526. {
  527. struct intel_engine_cs *ring = ringbuf->ring;
  528. uint32_t flush_domains;
  529. int ret;
  530. flush_domains = 0;
  531. if (ring->gpu_caches_dirty)
  532. flush_domains = I915_GEM_GPU_DOMAINS;
  533. ret = ring->emit_flush(ringbuf, I915_GEM_GPU_DOMAINS, flush_domains);
  534. if (ret)
  535. return ret;
  536. ring->gpu_caches_dirty = false;
  537. return 0;
  538. }
  539. static int execlists_move_to_gpu(struct intel_ringbuffer *ringbuf,
  540. struct list_head *vmas)
  541. {
  542. struct intel_engine_cs *ring = ringbuf->ring;
  543. struct i915_vma *vma;
  544. uint32_t flush_domains = 0;
  545. bool flush_chipset = false;
  546. int ret;
  547. list_for_each_entry(vma, vmas, exec_list) {
  548. struct drm_i915_gem_object *obj = vma->obj;
  549. ret = i915_gem_object_sync(obj, ring);
  550. if (ret)
  551. return ret;
  552. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  553. flush_chipset |= i915_gem_clflush_object(obj, false);
  554. flush_domains |= obj->base.write_domain;
  555. }
  556. if (flush_domains & I915_GEM_DOMAIN_GTT)
  557. wmb();
  558. /* Unconditionally invalidate gpu caches and ensure that we do flush
  559. * any residual writes from the previous batch.
  560. */
  561. return logical_ring_invalidate_all_caches(ringbuf);
  562. }
  563. /**
  564. * execlists_submission() - submit a batchbuffer for execution, Execlists style
  565. * @dev: DRM device.
  566. * @file: DRM file.
  567. * @ring: Engine Command Streamer to submit to.
  568. * @ctx: Context to employ for this submission.
  569. * @args: execbuffer call arguments.
  570. * @vmas: list of vmas.
  571. * @batch_obj: the batchbuffer to submit.
  572. * @exec_start: batchbuffer start virtual address pointer.
  573. * @flags: translated execbuffer call flags.
  574. *
  575. * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
  576. * away the submission details of the execbuffer ioctl call.
  577. *
  578. * Return: non-zero if the submission fails.
  579. */
  580. int intel_execlists_submission(struct drm_device *dev, struct drm_file *file,
  581. struct intel_engine_cs *ring,
  582. struct intel_context *ctx,
  583. struct drm_i915_gem_execbuffer2 *args,
  584. struct list_head *vmas,
  585. struct drm_i915_gem_object *batch_obj,
  586. u64 exec_start, u32 flags)
  587. {
  588. struct drm_i915_private *dev_priv = dev->dev_private;
  589. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  590. int instp_mode;
  591. u32 instp_mask;
  592. int ret;
  593. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  594. instp_mask = I915_EXEC_CONSTANTS_MASK;
  595. switch (instp_mode) {
  596. case I915_EXEC_CONSTANTS_REL_GENERAL:
  597. case I915_EXEC_CONSTANTS_ABSOLUTE:
  598. case I915_EXEC_CONSTANTS_REL_SURFACE:
  599. if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
  600. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  601. return -EINVAL;
  602. }
  603. if (instp_mode != dev_priv->relative_constants_mode) {
  604. if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  605. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  606. return -EINVAL;
  607. }
  608. /* The HW changed the meaning on this bit on gen6 */
  609. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  610. }
  611. break;
  612. default:
  613. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  614. return -EINVAL;
  615. }
  616. if (args->num_cliprects != 0) {
  617. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  618. return -EINVAL;
  619. } else {
  620. if (args->DR4 == 0xffffffff) {
  621. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  622. args->DR4 = 0;
  623. }
  624. if (args->DR1 || args->DR4 || args->cliprects_ptr) {
  625. DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
  626. return -EINVAL;
  627. }
  628. }
  629. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  630. DRM_DEBUG("sol reset is gen7 only\n");
  631. return -EINVAL;
  632. }
  633. ret = execlists_move_to_gpu(ringbuf, vmas);
  634. if (ret)
  635. return ret;
  636. if (ring == &dev_priv->ring[RCS] &&
  637. instp_mode != dev_priv->relative_constants_mode) {
  638. ret = intel_logical_ring_begin(ringbuf, 4);
  639. if (ret)
  640. return ret;
  641. intel_logical_ring_emit(ringbuf, MI_NOOP);
  642. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
  643. intel_logical_ring_emit(ringbuf, INSTPM);
  644. intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
  645. intel_logical_ring_advance(ringbuf);
  646. dev_priv->relative_constants_mode = instp_mode;
  647. }
  648. ret = ring->emit_bb_start(ringbuf, exec_start, flags);
  649. if (ret)
  650. return ret;
  651. i915_gem_execbuffer_move_to_active(vmas, ring);
  652. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  653. return 0;
  654. }
  655. void intel_execlists_retire_requests(struct intel_engine_cs *ring)
  656. {
  657. struct intel_ctx_submit_request *req, *tmp;
  658. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  659. unsigned long flags;
  660. struct list_head retired_list;
  661. WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  662. if (list_empty(&ring->execlist_retired_req_list))
  663. return;
  664. INIT_LIST_HEAD(&retired_list);
  665. spin_lock_irqsave(&ring->execlist_lock, flags);
  666. list_replace_init(&ring->execlist_retired_req_list, &retired_list);
  667. spin_unlock_irqrestore(&ring->execlist_lock, flags);
  668. list_for_each_entry_safe(req, tmp, &retired_list, execlist_link) {
  669. struct intel_context *ctx = req->request->ctx;
  670. struct drm_i915_gem_object *ctx_obj =
  671. ctx->engine[ring->id].state;
  672. if (ctx_obj && (ctx != ring->default_context))
  673. intel_lr_context_unpin(ring, ctx);
  674. intel_runtime_pm_put(dev_priv);
  675. i915_gem_context_unreference(ctx);
  676. i915_gem_request_unreference(req->request);
  677. list_del(&req->execlist_link);
  678. kfree(req);
  679. }
  680. }
  681. void intel_logical_ring_stop(struct intel_engine_cs *ring)
  682. {
  683. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  684. int ret;
  685. if (!intel_ring_initialized(ring))
  686. return;
  687. ret = intel_ring_idle(ring);
  688. if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
  689. DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
  690. ring->name, ret);
  691. /* TODO: Is this correct with Execlists enabled? */
  692. I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
  693. if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
  694. DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
  695. return;
  696. }
  697. I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
  698. }
  699. int logical_ring_flush_all_caches(struct intel_ringbuffer *ringbuf)
  700. {
  701. struct intel_engine_cs *ring = ringbuf->ring;
  702. int ret;
  703. if (!ring->gpu_caches_dirty)
  704. return 0;
  705. ret = ring->emit_flush(ringbuf, 0, I915_GEM_GPU_DOMAINS);
  706. if (ret)
  707. return ret;
  708. ring->gpu_caches_dirty = false;
  709. return 0;
  710. }
  711. /**
  712. * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
  713. * @ringbuf: Logical Ringbuffer to advance.
  714. *
  715. * The tail is updated in our logical ringbuffer struct, not in the actual context. What
  716. * really happens during submission is that the context and current tail will be placed
  717. * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
  718. * point, the tail *inside* the context is updated and the ELSP written to.
  719. */
  720. void intel_logical_ring_advance_and_submit(struct intel_ringbuffer *ringbuf,
  721. struct drm_i915_gem_request *request)
  722. {
  723. struct intel_engine_cs *ring = ringbuf->ring;
  724. struct intel_context *ctx = ringbuf->FIXME_lrc_ctx;
  725. intel_logical_ring_advance(ringbuf);
  726. if (intel_ring_stopped(ring))
  727. return;
  728. execlists_context_queue(ring, ctx, ringbuf->tail, request);
  729. }
  730. static int intel_lr_context_pin(struct intel_engine_cs *ring,
  731. struct intel_context *ctx)
  732. {
  733. struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
  734. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  735. int ret = 0;
  736. WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  737. if (ctx->engine[ring->id].unpin_count++ == 0) {
  738. ret = i915_gem_obj_ggtt_pin(ctx_obj,
  739. GEN8_LR_CONTEXT_ALIGN, 0);
  740. if (ret)
  741. goto reset_unpin_count;
  742. ret = intel_pin_and_map_ringbuffer_obj(ring->dev, ringbuf);
  743. if (ret)
  744. goto unpin_ctx_obj;
  745. }
  746. return ret;
  747. unpin_ctx_obj:
  748. i915_gem_object_ggtt_unpin(ctx_obj);
  749. reset_unpin_count:
  750. ctx->engine[ring->id].unpin_count = 0;
  751. return ret;
  752. }
  753. void intel_lr_context_unpin(struct intel_engine_cs *ring,
  754. struct intel_context *ctx)
  755. {
  756. struct drm_i915_gem_object *ctx_obj = ctx->engine[ring->id].state;
  757. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  758. if (ctx_obj) {
  759. WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  760. if (--ctx->engine[ring->id].unpin_count == 0) {
  761. intel_unpin_ringbuffer_obj(ringbuf);
  762. i915_gem_object_ggtt_unpin(ctx_obj);
  763. }
  764. }
  765. }
  766. static int logical_ring_alloc_request(struct intel_engine_cs *ring,
  767. struct intel_context *ctx)
  768. {
  769. struct drm_i915_gem_request *request;
  770. struct drm_i915_private *dev_private = ring->dev->dev_private;
  771. int ret;
  772. if (ring->outstanding_lazy_request)
  773. return 0;
  774. request = kzalloc(sizeof(*request), GFP_KERNEL);
  775. if (request == NULL)
  776. return -ENOMEM;
  777. if (ctx != ring->default_context) {
  778. ret = intel_lr_context_pin(ring, ctx);
  779. if (ret) {
  780. kfree(request);
  781. return ret;
  782. }
  783. }
  784. kref_init(&request->ref);
  785. request->ring = ring;
  786. request->uniq = dev_private->request_uniq++;
  787. ret = i915_gem_get_seqno(ring->dev, &request->seqno);
  788. if (ret) {
  789. intel_lr_context_unpin(ring, ctx);
  790. kfree(request);
  791. return ret;
  792. }
  793. /* Hold a reference to the context this request belongs to
  794. * (we will need it when the time comes to emit/retire the
  795. * request).
  796. */
  797. request->ctx = ctx;
  798. i915_gem_context_reference(request->ctx);
  799. ring->outstanding_lazy_request = request;
  800. return 0;
  801. }
  802. static int logical_ring_wait_request(struct intel_ringbuffer *ringbuf,
  803. int bytes)
  804. {
  805. struct intel_engine_cs *ring = ringbuf->ring;
  806. struct drm_i915_gem_request *request;
  807. int ret;
  808. if (intel_ring_space(ringbuf) >= bytes)
  809. return 0;
  810. list_for_each_entry(request, &ring->request_list, list) {
  811. /*
  812. * The request queue is per-engine, so can contain requests
  813. * from multiple ringbuffers. Here, we must ignore any that
  814. * aren't from the ringbuffer we're considering.
  815. */
  816. struct intel_context *ctx = request->ctx;
  817. if (ctx->engine[ring->id].ringbuf != ringbuf)
  818. continue;
  819. /* Would completion of this request free enough space? */
  820. if (__intel_ring_space(request->tail, ringbuf->tail,
  821. ringbuf->size) >= bytes) {
  822. break;
  823. }
  824. }
  825. if (&request->list == &ring->request_list)
  826. return -ENOSPC;
  827. ret = i915_wait_request(request);
  828. if (ret)
  829. return ret;
  830. i915_gem_retire_requests_ring(ring);
  831. return intel_ring_space(ringbuf) >= bytes ? 0 : -ENOSPC;
  832. }
  833. static int logical_ring_wait_for_space(struct intel_ringbuffer *ringbuf,
  834. int bytes)
  835. {
  836. struct intel_engine_cs *ring = ringbuf->ring;
  837. struct drm_device *dev = ring->dev;
  838. struct drm_i915_private *dev_priv = dev->dev_private;
  839. unsigned long end;
  840. int ret;
  841. ret = logical_ring_wait_request(ringbuf, bytes);
  842. if (ret != -ENOSPC)
  843. return ret;
  844. /* Force the context submission in case we have been skipping it */
  845. intel_logical_ring_advance_and_submit(ringbuf, NULL);
  846. /* With GEM the hangcheck timer should kick us out of the loop,
  847. * leaving it early runs the risk of corrupting GEM state (due
  848. * to running on almost untested codepaths). But on resume
  849. * timers don't work yet, so prevent a complete hang in that
  850. * case by choosing an insanely large timeout. */
  851. end = jiffies + 60 * HZ;
  852. ret = 0;
  853. do {
  854. if (intel_ring_space(ringbuf) >= bytes)
  855. break;
  856. msleep(1);
  857. if (dev_priv->mm.interruptible && signal_pending(current)) {
  858. ret = -ERESTARTSYS;
  859. break;
  860. }
  861. ret = i915_gem_check_wedge(&dev_priv->gpu_error,
  862. dev_priv->mm.interruptible);
  863. if (ret)
  864. break;
  865. if (time_after(jiffies, end)) {
  866. ret = -EBUSY;
  867. break;
  868. }
  869. } while (1);
  870. return ret;
  871. }
  872. static int logical_ring_wrap_buffer(struct intel_ringbuffer *ringbuf)
  873. {
  874. uint32_t __iomem *virt;
  875. int rem = ringbuf->size - ringbuf->tail;
  876. if (ringbuf->space < rem) {
  877. int ret = logical_ring_wait_for_space(ringbuf, rem);
  878. if (ret)
  879. return ret;
  880. }
  881. virt = ringbuf->virtual_start + ringbuf->tail;
  882. rem /= 4;
  883. while (rem--)
  884. iowrite32(MI_NOOP, virt++);
  885. ringbuf->tail = 0;
  886. intel_ring_update_space(ringbuf);
  887. return 0;
  888. }
  889. static int logical_ring_prepare(struct intel_ringbuffer *ringbuf, int bytes)
  890. {
  891. int ret;
  892. if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
  893. ret = logical_ring_wrap_buffer(ringbuf);
  894. if (unlikely(ret))
  895. return ret;
  896. }
  897. if (unlikely(ringbuf->space < bytes)) {
  898. ret = logical_ring_wait_for_space(ringbuf, bytes);
  899. if (unlikely(ret))
  900. return ret;
  901. }
  902. return 0;
  903. }
  904. /**
  905. * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
  906. *
  907. * @ringbuf: Logical ringbuffer.
  908. * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
  909. *
  910. * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
  911. * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
  912. * and also preallocates a request (every workload submission is still mediated through
  913. * requests, same as it did with legacy ringbuffer submission).
  914. *
  915. * Return: non-zero if the ringbuffer is not ready to be written to.
  916. */
  917. int intel_logical_ring_begin(struct intel_ringbuffer *ringbuf, int num_dwords)
  918. {
  919. struct intel_engine_cs *ring = ringbuf->ring;
  920. struct drm_device *dev = ring->dev;
  921. struct drm_i915_private *dev_priv = dev->dev_private;
  922. int ret;
  923. ret = i915_gem_check_wedge(&dev_priv->gpu_error,
  924. dev_priv->mm.interruptible);
  925. if (ret)
  926. return ret;
  927. ret = logical_ring_prepare(ringbuf, num_dwords * sizeof(uint32_t));
  928. if (ret)
  929. return ret;
  930. /* Preallocate the olr before touching the ring */
  931. ret = logical_ring_alloc_request(ring, ringbuf->FIXME_lrc_ctx);
  932. if (ret)
  933. return ret;
  934. ringbuf->space -= num_dwords * sizeof(uint32_t);
  935. return 0;
  936. }
  937. static int intel_logical_ring_workarounds_emit(struct intel_engine_cs *ring,
  938. struct intel_context *ctx)
  939. {
  940. int ret, i;
  941. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  942. struct drm_device *dev = ring->dev;
  943. struct drm_i915_private *dev_priv = dev->dev_private;
  944. struct i915_workarounds *w = &dev_priv->workarounds;
  945. if (WARN_ON_ONCE(w->count == 0))
  946. return 0;
  947. ring->gpu_caches_dirty = true;
  948. ret = logical_ring_flush_all_caches(ringbuf);
  949. if (ret)
  950. return ret;
  951. ret = intel_logical_ring_begin(ringbuf, w->count * 2 + 2);
  952. if (ret)
  953. return ret;
  954. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
  955. for (i = 0; i < w->count; i++) {
  956. intel_logical_ring_emit(ringbuf, w->reg[i].addr);
  957. intel_logical_ring_emit(ringbuf, w->reg[i].value);
  958. }
  959. intel_logical_ring_emit(ringbuf, MI_NOOP);
  960. intel_logical_ring_advance(ringbuf);
  961. ring->gpu_caches_dirty = true;
  962. ret = logical_ring_flush_all_caches(ringbuf);
  963. if (ret)
  964. return ret;
  965. return 0;
  966. }
  967. static int gen8_init_common_ring(struct intel_engine_cs *ring)
  968. {
  969. struct drm_device *dev = ring->dev;
  970. struct drm_i915_private *dev_priv = dev->dev_private;
  971. I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
  972. I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
  973. I915_WRITE(RING_MODE_GEN7(ring),
  974. _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
  975. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  976. POSTING_READ(RING_MODE_GEN7(ring));
  977. ring->next_context_status_buffer = 0;
  978. DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
  979. memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
  980. return 0;
  981. }
  982. static int gen8_init_render_ring(struct intel_engine_cs *ring)
  983. {
  984. struct drm_device *dev = ring->dev;
  985. struct drm_i915_private *dev_priv = dev->dev_private;
  986. int ret;
  987. ret = gen8_init_common_ring(ring);
  988. if (ret)
  989. return ret;
  990. /* We need to disable the AsyncFlip performance optimisations in order
  991. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  992. * programmed to '1' on all products.
  993. *
  994. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  995. */
  996. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  997. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  998. return init_workarounds_ring(ring);
  999. }
  1000. static int gen8_emit_bb_start(struct intel_ringbuffer *ringbuf,
  1001. u64 offset, unsigned flags)
  1002. {
  1003. bool ppgtt = !(flags & I915_DISPATCH_SECURE);
  1004. int ret;
  1005. ret = intel_logical_ring_begin(ringbuf, 4);
  1006. if (ret)
  1007. return ret;
  1008. /* FIXME(BDW): Address space and security selectors. */
  1009. intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
  1010. intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
  1011. intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
  1012. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1013. intel_logical_ring_advance(ringbuf);
  1014. return 0;
  1015. }
  1016. static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
  1017. {
  1018. struct drm_device *dev = ring->dev;
  1019. struct drm_i915_private *dev_priv = dev->dev_private;
  1020. unsigned long flags;
  1021. if (WARN_ON(!intel_irqs_enabled(dev_priv)))
  1022. return false;
  1023. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  1024. if (ring->irq_refcount++ == 0) {
  1025. I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
  1026. POSTING_READ(RING_IMR(ring->mmio_base));
  1027. }
  1028. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  1029. return true;
  1030. }
  1031. static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
  1032. {
  1033. struct drm_device *dev = ring->dev;
  1034. struct drm_i915_private *dev_priv = dev->dev_private;
  1035. unsigned long flags;
  1036. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  1037. if (--ring->irq_refcount == 0) {
  1038. I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
  1039. POSTING_READ(RING_IMR(ring->mmio_base));
  1040. }
  1041. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  1042. }
  1043. static int gen8_emit_flush(struct intel_ringbuffer *ringbuf,
  1044. u32 invalidate_domains,
  1045. u32 unused)
  1046. {
  1047. struct intel_engine_cs *ring = ringbuf->ring;
  1048. struct drm_device *dev = ring->dev;
  1049. struct drm_i915_private *dev_priv = dev->dev_private;
  1050. uint32_t cmd;
  1051. int ret;
  1052. ret = intel_logical_ring_begin(ringbuf, 4);
  1053. if (ret)
  1054. return ret;
  1055. cmd = MI_FLUSH_DW + 1;
  1056. if (ring == &dev_priv->ring[VCS]) {
  1057. if (invalidate_domains & I915_GEM_GPU_DOMAINS)
  1058. cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
  1059. MI_FLUSH_DW_STORE_INDEX |
  1060. MI_FLUSH_DW_OP_STOREDW;
  1061. } else {
  1062. if (invalidate_domains & I915_GEM_DOMAIN_RENDER)
  1063. cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
  1064. MI_FLUSH_DW_OP_STOREDW;
  1065. }
  1066. intel_logical_ring_emit(ringbuf, cmd);
  1067. intel_logical_ring_emit(ringbuf,
  1068. I915_GEM_HWS_SCRATCH_ADDR |
  1069. MI_FLUSH_DW_USE_GTT);
  1070. intel_logical_ring_emit(ringbuf, 0); /* upper addr */
  1071. intel_logical_ring_emit(ringbuf, 0); /* value */
  1072. intel_logical_ring_advance(ringbuf);
  1073. return 0;
  1074. }
  1075. static int gen8_emit_flush_render(struct intel_ringbuffer *ringbuf,
  1076. u32 invalidate_domains,
  1077. u32 flush_domains)
  1078. {
  1079. struct intel_engine_cs *ring = ringbuf->ring;
  1080. u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  1081. u32 flags = 0;
  1082. int ret;
  1083. flags |= PIPE_CONTROL_CS_STALL;
  1084. if (flush_domains) {
  1085. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  1086. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  1087. }
  1088. if (invalidate_domains) {
  1089. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  1090. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  1091. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  1092. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1093. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  1094. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  1095. flags |= PIPE_CONTROL_QW_WRITE;
  1096. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1097. }
  1098. ret = intel_logical_ring_begin(ringbuf, 6);
  1099. if (ret)
  1100. return ret;
  1101. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  1102. intel_logical_ring_emit(ringbuf, flags);
  1103. intel_logical_ring_emit(ringbuf, scratch_addr);
  1104. intel_logical_ring_emit(ringbuf, 0);
  1105. intel_logical_ring_emit(ringbuf, 0);
  1106. intel_logical_ring_emit(ringbuf, 0);
  1107. intel_logical_ring_advance(ringbuf);
  1108. return 0;
  1109. }
  1110. static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
  1111. {
  1112. return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
  1113. }
  1114. static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
  1115. {
  1116. intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
  1117. }
  1118. static int gen8_emit_request(struct intel_ringbuffer *ringbuf,
  1119. struct drm_i915_gem_request *request)
  1120. {
  1121. struct intel_engine_cs *ring = ringbuf->ring;
  1122. u32 cmd;
  1123. int ret;
  1124. ret = intel_logical_ring_begin(ringbuf, 6);
  1125. if (ret)
  1126. return ret;
  1127. cmd = MI_STORE_DWORD_IMM_GEN4;
  1128. cmd |= MI_GLOBAL_GTT;
  1129. intel_logical_ring_emit(ringbuf, cmd);
  1130. intel_logical_ring_emit(ringbuf,
  1131. (ring->status_page.gfx_addr +
  1132. (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
  1133. intel_logical_ring_emit(ringbuf, 0);
  1134. intel_logical_ring_emit(ringbuf,
  1135. i915_gem_request_get_seqno(ring->outstanding_lazy_request));
  1136. intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
  1137. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1138. intel_logical_ring_advance_and_submit(ringbuf, request);
  1139. return 0;
  1140. }
  1141. static int gen8_init_rcs_context(struct intel_engine_cs *ring,
  1142. struct intel_context *ctx)
  1143. {
  1144. int ret;
  1145. ret = intel_logical_ring_workarounds_emit(ring, ctx);
  1146. if (ret)
  1147. return ret;
  1148. return intel_lr_context_render_state_init(ring, ctx);
  1149. }
  1150. /**
  1151. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1152. *
  1153. * @ring: Engine Command Streamer.
  1154. *
  1155. */
  1156. void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
  1157. {
  1158. struct drm_i915_private *dev_priv;
  1159. if (!intel_ring_initialized(ring))
  1160. return;
  1161. dev_priv = ring->dev->dev_private;
  1162. intel_logical_ring_stop(ring);
  1163. WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
  1164. i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
  1165. if (ring->cleanup)
  1166. ring->cleanup(ring);
  1167. i915_cmd_parser_fini_ring(ring);
  1168. if (ring->status_page.obj) {
  1169. kunmap(sg_page(ring->status_page.obj->pages->sgl));
  1170. ring->status_page.obj = NULL;
  1171. }
  1172. }
  1173. static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
  1174. {
  1175. int ret;
  1176. /* Intentionally left blank. */
  1177. ring->buffer = NULL;
  1178. ring->dev = dev;
  1179. INIT_LIST_HEAD(&ring->active_list);
  1180. INIT_LIST_HEAD(&ring->request_list);
  1181. init_waitqueue_head(&ring->irq_queue);
  1182. INIT_LIST_HEAD(&ring->execlist_queue);
  1183. INIT_LIST_HEAD(&ring->execlist_retired_req_list);
  1184. spin_lock_init(&ring->execlist_lock);
  1185. ret = i915_cmd_parser_init_ring(ring);
  1186. if (ret)
  1187. return ret;
  1188. ret = intel_lr_context_deferred_create(ring->default_context, ring);
  1189. return ret;
  1190. }
  1191. static int logical_render_ring_init(struct drm_device *dev)
  1192. {
  1193. struct drm_i915_private *dev_priv = dev->dev_private;
  1194. struct intel_engine_cs *ring = &dev_priv->ring[RCS];
  1195. int ret;
  1196. ring->name = "render ring";
  1197. ring->id = RCS;
  1198. ring->mmio_base = RENDER_RING_BASE;
  1199. ring->irq_enable_mask =
  1200. GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
  1201. ring->irq_keep_mask =
  1202. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
  1203. if (HAS_L3_DPF(dev))
  1204. ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1205. ring->init_hw = gen8_init_render_ring;
  1206. ring->init_context = gen8_init_rcs_context;
  1207. ring->cleanup = intel_fini_pipe_control;
  1208. ring->get_seqno = gen8_get_seqno;
  1209. ring->set_seqno = gen8_set_seqno;
  1210. ring->emit_request = gen8_emit_request;
  1211. ring->emit_flush = gen8_emit_flush_render;
  1212. ring->irq_get = gen8_logical_ring_get_irq;
  1213. ring->irq_put = gen8_logical_ring_put_irq;
  1214. ring->emit_bb_start = gen8_emit_bb_start;
  1215. ring->dev = dev;
  1216. ret = logical_ring_init(dev, ring);
  1217. if (ret)
  1218. return ret;
  1219. return intel_init_pipe_control(ring);
  1220. }
  1221. static int logical_bsd_ring_init(struct drm_device *dev)
  1222. {
  1223. struct drm_i915_private *dev_priv = dev->dev_private;
  1224. struct intel_engine_cs *ring = &dev_priv->ring[VCS];
  1225. ring->name = "bsd ring";
  1226. ring->id = VCS;
  1227. ring->mmio_base = GEN6_BSD_RING_BASE;
  1228. ring->irq_enable_mask =
  1229. GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1230. ring->irq_keep_mask =
  1231. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1232. ring->init_hw = gen8_init_common_ring;
  1233. ring->get_seqno = gen8_get_seqno;
  1234. ring->set_seqno = gen8_set_seqno;
  1235. ring->emit_request = gen8_emit_request;
  1236. ring->emit_flush = gen8_emit_flush;
  1237. ring->irq_get = gen8_logical_ring_get_irq;
  1238. ring->irq_put = gen8_logical_ring_put_irq;
  1239. ring->emit_bb_start = gen8_emit_bb_start;
  1240. return logical_ring_init(dev, ring);
  1241. }
  1242. static int logical_bsd2_ring_init(struct drm_device *dev)
  1243. {
  1244. struct drm_i915_private *dev_priv = dev->dev_private;
  1245. struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
  1246. ring->name = "bds2 ring";
  1247. ring->id = VCS2;
  1248. ring->mmio_base = GEN8_BSD2_RING_BASE;
  1249. ring->irq_enable_mask =
  1250. GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  1251. ring->irq_keep_mask =
  1252. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  1253. ring->init_hw = gen8_init_common_ring;
  1254. ring->get_seqno = gen8_get_seqno;
  1255. ring->set_seqno = gen8_set_seqno;
  1256. ring->emit_request = gen8_emit_request;
  1257. ring->emit_flush = gen8_emit_flush;
  1258. ring->irq_get = gen8_logical_ring_get_irq;
  1259. ring->irq_put = gen8_logical_ring_put_irq;
  1260. ring->emit_bb_start = gen8_emit_bb_start;
  1261. return logical_ring_init(dev, ring);
  1262. }
  1263. static int logical_blt_ring_init(struct drm_device *dev)
  1264. {
  1265. struct drm_i915_private *dev_priv = dev->dev_private;
  1266. struct intel_engine_cs *ring = &dev_priv->ring[BCS];
  1267. ring->name = "blitter ring";
  1268. ring->id = BCS;
  1269. ring->mmio_base = BLT_RING_BASE;
  1270. ring->irq_enable_mask =
  1271. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1272. ring->irq_keep_mask =
  1273. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1274. ring->init_hw = gen8_init_common_ring;
  1275. ring->get_seqno = gen8_get_seqno;
  1276. ring->set_seqno = gen8_set_seqno;
  1277. ring->emit_request = gen8_emit_request;
  1278. ring->emit_flush = gen8_emit_flush;
  1279. ring->irq_get = gen8_logical_ring_get_irq;
  1280. ring->irq_put = gen8_logical_ring_put_irq;
  1281. ring->emit_bb_start = gen8_emit_bb_start;
  1282. return logical_ring_init(dev, ring);
  1283. }
  1284. static int logical_vebox_ring_init(struct drm_device *dev)
  1285. {
  1286. struct drm_i915_private *dev_priv = dev->dev_private;
  1287. struct intel_engine_cs *ring = &dev_priv->ring[VECS];
  1288. ring->name = "video enhancement ring";
  1289. ring->id = VECS;
  1290. ring->mmio_base = VEBOX_RING_BASE;
  1291. ring->irq_enable_mask =
  1292. GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1293. ring->irq_keep_mask =
  1294. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1295. ring->init_hw = gen8_init_common_ring;
  1296. ring->get_seqno = gen8_get_seqno;
  1297. ring->set_seqno = gen8_set_seqno;
  1298. ring->emit_request = gen8_emit_request;
  1299. ring->emit_flush = gen8_emit_flush;
  1300. ring->irq_get = gen8_logical_ring_get_irq;
  1301. ring->irq_put = gen8_logical_ring_put_irq;
  1302. ring->emit_bb_start = gen8_emit_bb_start;
  1303. return logical_ring_init(dev, ring);
  1304. }
  1305. /**
  1306. * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
  1307. * @dev: DRM device.
  1308. *
  1309. * This function inits the engines for an Execlists submission style (the equivalent in the
  1310. * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
  1311. * those engines that are present in the hardware.
  1312. *
  1313. * Return: non-zero if the initialization failed.
  1314. */
  1315. int intel_logical_rings_init(struct drm_device *dev)
  1316. {
  1317. struct drm_i915_private *dev_priv = dev->dev_private;
  1318. int ret;
  1319. ret = logical_render_ring_init(dev);
  1320. if (ret)
  1321. return ret;
  1322. if (HAS_BSD(dev)) {
  1323. ret = logical_bsd_ring_init(dev);
  1324. if (ret)
  1325. goto cleanup_render_ring;
  1326. }
  1327. if (HAS_BLT(dev)) {
  1328. ret = logical_blt_ring_init(dev);
  1329. if (ret)
  1330. goto cleanup_bsd_ring;
  1331. }
  1332. if (HAS_VEBOX(dev)) {
  1333. ret = logical_vebox_ring_init(dev);
  1334. if (ret)
  1335. goto cleanup_blt_ring;
  1336. }
  1337. if (HAS_BSD2(dev)) {
  1338. ret = logical_bsd2_ring_init(dev);
  1339. if (ret)
  1340. goto cleanup_vebox_ring;
  1341. }
  1342. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  1343. if (ret)
  1344. goto cleanup_bsd2_ring;
  1345. return 0;
  1346. cleanup_bsd2_ring:
  1347. intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
  1348. cleanup_vebox_ring:
  1349. intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
  1350. cleanup_blt_ring:
  1351. intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
  1352. cleanup_bsd_ring:
  1353. intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
  1354. cleanup_render_ring:
  1355. intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
  1356. return ret;
  1357. }
  1358. int intel_lr_context_render_state_init(struct intel_engine_cs *ring,
  1359. struct intel_context *ctx)
  1360. {
  1361. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  1362. struct render_state so;
  1363. struct drm_i915_file_private *file_priv = ctx->file_priv;
  1364. struct drm_file *file = file_priv ? file_priv->file : NULL;
  1365. int ret;
  1366. ret = i915_gem_render_state_prepare(ring, &so);
  1367. if (ret)
  1368. return ret;
  1369. if (so.rodata == NULL)
  1370. return 0;
  1371. ret = ring->emit_bb_start(ringbuf,
  1372. so.ggtt_offset,
  1373. I915_DISPATCH_SECURE);
  1374. if (ret)
  1375. goto out;
  1376. i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), ring);
  1377. ret = __i915_add_request(ring, file, so.obj);
  1378. /* intel_logical_ring_add_request moves object to inactive if it
  1379. * fails */
  1380. out:
  1381. i915_gem_render_state_fini(&so);
  1382. return ret;
  1383. }
  1384. static int
  1385. populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
  1386. struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
  1387. {
  1388. struct drm_device *dev = ring->dev;
  1389. struct drm_i915_private *dev_priv = dev->dev_private;
  1390. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
  1391. struct page *page;
  1392. uint32_t *reg_state;
  1393. int ret;
  1394. if (!ppgtt)
  1395. ppgtt = dev_priv->mm.aliasing_ppgtt;
  1396. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1397. if (ret) {
  1398. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1399. return ret;
  1400. }
  1401. ret = i915_gem_object_get_pages(ctx_obj);
  1402. if (ret) {
  1403. DRM_DEBUG_DRIVER("Could not get object pages\n");
  1404. return ret;
  1405. }
  1406. i915_gem_object_pin_pages(ctx_obj);
  1407. /* The second page of the context object contains some fields which must
  1408. * be set up prior to the first execution. */
  1409. page = i915_gem_object_get_page(ctx_obj, 1);
  1410. reg_state = kmap_atomic(page);
  1411. /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
  1412. * commands followed by (reg, value) pairs. The values we are setting here are
  1413. * only for the first context restore: on a subsequent save, the GPU will
  1414. * recreate this batchbuffer with new values (including all the missing
  1415. * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
  1416. if (ring->id == RCS)
  1417. reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
  1418. else
  1419. reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
  1420. reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
  1421. reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
  1422. reg_state[CTX_CONTEXT_CONTROL+1] =
  1423. _MASKED_BIT_ENABLE((1<<3) | MI_RESTORE_INHIBIT);
  1424. reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
  1425. reg_state[CTX_RING_HEAD+1] = 0;
  1426. reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
  1427. reg_state[CTX_RING_TAIL+1] = 0;
  1428. reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
  1429. /* Ring buffer start address is not known until the buffer is pinned.
  1430. * It is written to the context image in execlists_update_context()
  1431. */
  1432. reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
  1433. reg_state[CTX_RING_BUFFER_CONTROL+1] =
  1434. ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
  1435. reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
  1436. reg_state[CTX_BB_HEAD_U+1] = 0;
  1437. reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
  1438. reg_state[CTX_BB_HEAD_L+1] = 0;
  1439. reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
  1440. reg_state[CTX_BB_STATE+1] = (1<<5);
  1441. reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
  1442. reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
  1443. reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
  1444. reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
  1445. reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
  1446. reg_state[CTX_SECOND_BB_STATE+1] = 0;
  1447. if (ring->id == RCS) {
  1448. /* TODO: according to BSpec, the register state context
  1449. * for CHV does not have these. OTOH, these registers do
  1450. * exist in CHV. I'm waiting for a clarification */
  1451. reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
  1452. reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
  1453. reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
  1454. reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
  1455. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
  1456. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
  1457. }
  1458. reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
  1459. reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
  1460. reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
  1461. reg_state[CTX_CTX_TIMESTAMP+1] = 0;
  1462. reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
  1463. reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
  1464. reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
  1465. reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
  1466. reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
  1467. reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
  1468. reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
  1469. reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
  1470. reg_state[CTX_PDP3_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[3]);
  1471. reg_state[CTX_PDP3_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[3]);
  1472. reg_state[CTX_PDP2_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[2]);
  1473. reg_state[CTX_PDP2_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[2]);
  1474. reg_state[CTX_PDP1_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[1]);
  1475. reg_state[CTX_PDP1_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[1]);
  1476. reg_state[CTX_PDP0_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[0]);
  1477. reg_state[CTX_PDP0_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[0]);
  1478. if (ring->id == RCS) {
  1479. reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  1480. reg_state[CTX_R_PWR_CLK_STATE] = 0x20c8;
  1481. reg_state[CTX_R_PWR_CLK_STATE+1] = 0;
  1482. }
  1483. kunmap_atomic(reg_state);
  1484. ctx_obj->dirty = 1;
  1485. set_page_dirty(page);
  1486. i915_gem_object_unpin_pages(ctx_obj);
  1487. return 0;
  1488. }
  1489. /**
  1490. * intel_lr_context_free() - free the LRC specific bits of a context
  1491. * @ctx: the LR context to free.
  1492. *
  1493. * The real context freeing is done in i915_gem_context_free: this only
  1494. * takes care of the bits that are LRC related: the per-engine backing
  1495. * objects and the logical ringbuffer.
  1496. */
  1497. void intel_lr_context_free(struct intel_context *ctx)
  1498. {
  1499. int i;
  1500. for (i = 0; i < I915_NUM_RINGS; i++) {
  1501. struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
  1502. if (ctx_obj) {
  1503. struct intel_ringbuffer *ringbuf =
  1504. ctx->engine[i].ringbuf;
  1505. struct intel_engine_cs *ring = ringbuf->ring;
  1506. if (ctx == ring->default_context) {
  1507. intel_unpin_ringbuffer_obj(ringbuf);
  1508. i915_gem_object_ggtt_unpin(ctx_obj);
  1509. }
  1510. intel_destroy_ringbuffer_obj(ringbuf);
  1511. kfree(ringbuf);
  1512. drm_gem_object_unreference(&ctx_obj->base);
  1513. }
  1514. }
  1515. }
  1516. static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
  1517. {
  1518. int ret = 0;
  1519. WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
  1520. switch (ring->id) {
  1521. case RCS:
  1522. if (INTEL_INFO(ring->dev)->gen >= 9)
  1523. ret = GEN9_LR_CONTEXT_RENDER_SIZE;
  1524. else
  1525. ret = GEN8_LR_CONTEXT_RENDER_SIZE;
  1526. break;
  1527. case VCS:
  1528. case BCS:
  1529. case VECS:
  1530. case VCS2:
  1531. ret = GEN8_LR_CONTEXT_OTHER_SIZE;
  1532. break;
  1533. }
  1534. return ret;
  1535. }
  1536. static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
  1537. struct drm_i915_gem_object *default_ctx_obj)
  1538. {
  1539. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1540. /* The status page is offset 0 from the default context object
  1541. * in LRC mode. */
  1542. ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
  1543. ring->status_page.page_addr =
  1544. kmap(sg_page(default_ctx_obj->pages->sgl));
  1545. ring->status_page.obj = default_ctx_obj;
  1546. I915_WRITE(RING_HWS_PGA(ring->mmio_base),
  1547. (u32)ring->status_page.gfx_addr);
  1548. POSTING_READ(RING_HWS_PGA(ring->mmio_base));
  1549. }
  1550. /**
  1551. * intel_lr_context_deferred_create() - create the LRC specific bits of a context
  1552. * @ctx: LR context to create.
  1553. * @ring: engine to be used with the context.
  1554. *
  1555. * This function can be called more than once, with different engines, if we plan
  1556. * to use the context with them. The context backing objects and the ringbuffers
  1557. * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
  1558. * the creation is a deferred call: it's better to make sure first that we need to use
  1559. * a given ring with the context.
  1560. *
  1561. * Return: non-zero on error.
  1562. */
  1563. int intel_lr_context_deferred_create(struct intel_context *ctx,
  1564. struct intel_engine_cs *ring)
  1565. {
  1566. const bool is_global_default_ctx = (ctx == ring->default_context);
  1567. struct drm_device *dev = ring->dev;
  1568. struct drm_i915_gem_object *ctx_obj;
  1569. uint32_t context_size;
  1570. struct intel_ringbuffer *ringbuf;
  1571. int ret;
  1572. WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
  1573. WARN_ON(ctx->engine[ring->id].state);
  1574. context_size = round_up(get_lr_context_size(ring), 4096);
  1575. ctx_obj = i915_gem_alloc_context_obj(dev, context_size);
  1576. if (IS_ERR(ctx_obj)) {
  1577. ret = PTR_ERR(ctx_obj);
  1578. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed: %d\n", ret);
  1579. return ret;
  1580. }
  1581. if (is_global_default_ctx) {
  1582. ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
  1583. if (ret) {
  1584. DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
  1585. ret);
  1586. drm_gem_object_unreference(&ctx_obj->base);
  1587. return ret;
  1588. }
  1589. }
  1590. ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
  1591. if (!ringbuf) {
  1592. DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
  1593. ring->name);
  1594. ret = -ENOMEM;
  1595. goto error_unpin_ctx;
  1596. }
  1597. ringbuf->ring = ring;
  1598. ringbuf->FIXME_lrc_ctx = ctx;
  1599. ringbuf->size = 32 * PAGE_SIZE;
  1600. ringbuf->effective_size = ringbuf->size;
  1601. ringbuf->head = 0;
  1602. ringbuf->tail = 0;
  1603. ringbuf->last_retired_head = -1;
  1604. intel_ring_update_space(ringbuf);
  1605. if (ringbuf->obj == NULL) {
  1606. ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
  1607. if (ret) {
  1608. DRM_DEBUG_DRIVER(
  1609. "Failed to allocate ringbuffer obj %s: %d\n",
  1610. ring->name, ret);
  1611. goto error_free_rbuf;
  1612. }
  1613. if (is_global_default_ctx) {
  1614. ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
  1615. if (ret) {
  1616. DRM_ERROR(
  1617. "Failed to pin and map ringbuffer %s: %d\n",
  1618. ring->name, ret);
  1619. goto error_destroy_rbuf;
  1620. }
  1621. }
  1622. }
  1623. ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
  1624. if (ret) {
  1625. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  1626. goto error;
  1627. }
  1628. ctx->engine[ring->id].ringbuf = ringbuf;
  1629. ctx->engine[ring->id].state = ctx_obj;
  1630. if (ctx == ring->default_context)
  1631. lrc_setup_hardware_status_page(ring, ctx_obj);
  1632. else if (ring->id == RCS && !ctx->rcs_initialized) {
  1633. if (ring->init_context) {
  1634. ret = ring->init_context(ring, ctx);
  1635. if (ret) {
  1636. DRM_ERROR("ring init context: %d\n", ret);
  1637. ctx->engine[ring->id].ringbuf = NULL;
  1638. ctx->engine[ring->id].state = NULL;
  1639. goto error;
  1640. }
  1641. }
  1642. ctx->rcs_initialized = true;
  1643. }
  1644. return 0;
  1645. error:
  1646. if (is_global_default_ctx)
  1647. intel_unpin_ringbuffer_obj(ringbuf);
  1648. error_destroy_rbuf:
  1649. intel_destroy_ringbuffer_obj(ringbuf);
  1650. error_free_rbuf:
  1651. kfree(ringbuf);
  1652. error_unpin_ctx:
  1653. if (is_global_default_ctx)
  1654. i915_gem_object_ggtt_unpin(ctx_obj);
  1655. drm_gem_object_unreference(&ctx_obj->base);
  1656. return ret;
  1657. }