intel_lrc.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  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 GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
  137. #define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
  138. #define GEN8_LR_CONTEXT_ALIGN 4096
  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. /**
  199. * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
  200. * @dev: DRM device.
  201. * @enable_execlists: value of i915.enable_execlists module parameter.
  202. *
  203. * Only certain platforms support Execlists (the prerequisites being
  204. * support for Logical Ring Contexts and Aliasing PPGTT or better),
  205. * and only when enabled via module parameter.
  206. *
  207. * Return: 1 if Execlists is supported and has to be enabled.
  208. */
  209. int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists)
  210. {
  211. WARN_ON(i915.enable_ppgtt == -1);
  212. if (enable_execlists == 0)
  213. return 0;
  214. if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
  215. i915.use_mmio_flip >= 0)
  216. return 1;
  217. return 0;
  218. }
  219. /**
  220. * intel_execlists_ctx_id() - get the Execlists Context ID
  221. * @ctx_obj: Logical Ring Context backing object.
  222. *
  223. * Do not confuse with ctx->id! Unfortunately we have a name overload
  224. * here: the old context ID we pass to userspace as a handler so that
  225. * they can refer to a context, and the new context ID we pass to the
  226. * ELSP so that the GPU can inform us of the context status via
  227. * interrupts.
  228. *
  229. * Return: 20-bits globally unique context ID.
  230. */
  231. u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
  232. {
  233. u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
  234. /* LRCA is required to be 4K aligned so the more significant 20 bits
  235. * are globally unique */
  236. return lrca >> 12;
  237. }
  238. static uint64_t execlists_ctx_descriptor(struct drm_i915_gem_object *ctx_obj)
  239. {
  240. uint64_t desc;
  241. uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
  242. WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
  243. desc = GEN8_CTX_VALID;
  244. desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
  245. desc |= GEN8_CTX_L3LLC_COHERENT;
  246. desc |= GEN8_CTX_PRIVILEGE;
  247. desc |= lrca;
  248. desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
  249. /* TODO: WaDisableLiteRestore when we start using semaphore
  250. * signalling between Command Streamers */
  251. /* desc |= GEN8_CTX_FORCE_RESTORE; */
  252. return desc;
  253. }
  254. static void execlists_elsp_write(struct intel_engine_cs *ring,
  255. struct drm_i915_gem_object *ctx_obj0,
  256. struct drm_i915_gem_object *ctx_obj1)
  257. {
  258. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  259. uint64_t temp = 0;
  260. uint32_t desc[4];
  261. unsigned long flags;
  262. /* XXX: You must always write both descriptors in the order below. */
  263. if (ctx_obj1)
  264. temp = execlists_ctx_descriptor(ctx_obj1);
  265. else
  266. temp = 0;
  267. desc[1] = (u32)(temp >> 32);
  268. desc[0] = (u32)temp;
  269. temp = execlists_ctx_descriptor(ctx_obj0);
  270. desc[3] = (u32)(temp >> 32);
  271. desc[2] = (u32)temp;
  272. /* Set Force Wakeup bit to prevent GT from entering C6 while ELSP writes
  273. * are in progress.
  274. *
  275. * The other problem is that we can't just call gen6_gt_force_wake_get()
  276. * because that function calls intel_runtime_pm_get(), which might sleep.
  277. * Instead, we do the runtime_pm_get/put when creating/destroying requests.
  278. */
  279. spin_lock_irqsave(&dev_priv->uncore.lock, flags);
  280. if (IS_CHERRYVIEW(dev_priv->dev)) {
  281. if (dev_priv->uncore.fw_rendercount++ == 0)
  282. dev_priv->uncore.funcs.force_wake_get(dev_priv,
  283. FORCEWAKE_RENDER);
  284. if (dev_priv->uncore.fw_mediacount++ == 0)
  285. dev_priv->uncore.funcs.force_wake_get(dev_priv,
  286. FORCEWAKE_MEDIA);
  287. } else {
  288. if (dev_priv->uncore.forcewake_count++ == 0)
  289. dev_priv->uncore.funcs.force_wake_get(dev_priv,
  290. FORCEWAKE_ALL);
  291. }
  292. spin_unlock_irqrestore(&dev_priv->uncore.lock, flags);
  293. I915_WRITE(RING_ELSP(ring), desc[1]);
  294. I915_WRITE(RING_ELSP(ring), desc[0]);
  295. I915_WRITE(RING_ELSP(ring), desc[3]);
  296. /* The context is automatically loaded after the following */
  297. I915_WRITE(RING_ELSP(ring), desc[2]);
  298. /* ELSP is a wo register, so use another nearby reg for posting instead */
  299. POSTING_READ(RING_EXECLIST_STATUS(ring));
  300. /* Release Force Wakeup (see the big comment above). */
  301. spin_lock_irqsave(&dev_priv->uncore.lock, flags);
  302. if (IS_CHERRYVIEW(dev_priv->dev)) {
  303. if (--dev_priv->uncore.fw_rendercount == 0)
  304. dev_priv->uncore.funcs.force_wake_put(dev_priv,
  305. FORCEWAKE_RENDER);
  306. if (--dev_priv->uncore.fw_mediacount == 0)
  307. dev_priv->uncore.funcs.force_wake_put(dev_priv,
  308. FORCEWAKE_MEDIA);
  309. } else {
  310. if (--dev_priv->uncore.forcewake_count == 0)
  311. dev_priv->uncore.funcs.force_wake_put(dev_priv,
  312. FORCEWAKE_ALL);
  313. }
  314. spin_unlock_irqrestore(&dev_priv->uncore.lock, flags);
  315. }
  316. static int execlists_ctx_write_tail(struct drm_i915_gem_object *ctx_obj, u32 tail)
  317. {
  318. struct page *page;
  319. uint32_t *reg_state;
  320. page = i915_gem_object_get_page(ctx_obj, 1);
  321. reg_state = kmap_atomic(page);
  322. reg_state[CTX_RING_TAIL+1] = tail;
  323. kunmap_atomic(reg_state);
  324. return 0;
  325. }
  326. static void execlists_submit_contexts(struct intel_engine_cs *ring,
  327. struct intel_context *to0, u32 tail0,
  328. struct intel_context *to1, u32 tail1)
  329. {
  330. struct drm_i915_gem_object *ctx_obj0;
  331. struct drm_i915_gem_object *ctx_obj1 = NULL;
  332. ctx_obj0 = to0->engine[ring->id].state;
  333. BUG_ON(!ctx_obj0);
  334. WARN_ON(!i915_gem_obj_is_pinned(ctx_obj0));
  335. execlists_ctx_write_tail(ctx_obj0, tail0);
  336. if (to1) {
  337. ctx_obj1 = to1->engine[ring->id].state;
  338. BUG_ON(!ctx_obj1);
  339. WARN_ON(!i915_gem_obj_is_pinned(ctx_obj1));
  340. execlists_ctx_write_tail(ctx_obj1, tail1);
  341. }
  342. execlists_elsp_write(ring, ctx_obj0, ctx_obj1);
  343. }
  344. static void execlists_context_unqueue(struct intel_engine_cs *ring)
  345. {
  346. struct intel_ctx_submit_request *req0 = NULL, *req1 = NULL;
  347. struct intel_ctx_submit_request *cursor = NULL, *tmp = NULL;
  348. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  349. assert_spin_locked(&ring->execlist_lock);
  350. if (list_empty(&ring->execlist_queue))
  351. return;
  352. /* Try to read in pairs */
  353. list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
  354. execlist_link) {
  355. if (!req0) {
  356. req0 = cursor;
  357. } else if (req0->ctx == cursor->ctx) {
  358. /* Same ctx: ignore first request, as second request
  359. * will update tail past first request's workload */
  360. cursor->elsp_submitted = req0->elsp_submitted;
  361. list_del(&req0->execlist_link);
  362. queue_work(dev_priv->wq, &req0->work);
  363. req0 = cursor;
  364. } else {
  365. req1 = cursor;
  366. break;
  367. }
  368. }
  369. WARN_ON(req1 && req1->elsp_submitted);
  370. execlists_submit_contexts(ring, req0->ctx, req0->tail,
  371. req1 ? req1->ctx : NULL,
  372. req1 ? req1->tail : 0);
  373. req0->elsp_submitted++;
  374. if (req1)
  375. req1->elsp_submitted++;
  376. }
  377. static bool execlists_check_remove_request(struct intel_engine_cs *ring,
  378. u32 request_id)
  379. {
  380. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  381. struct intel_ctx_submit_request *head_req;
  382. assert_spin_locked(&ring->execlist_lock);
  383. head_req = list_first_entry_or_null(&ring->execlist_queue,
  384. struct intel_ctx_submit_request,
  385. execlist_link);
  386. if (head_req != NULL) {
  387. struct drm_i915_gem_object *ctx_obj =
  388. head_req->ctx->engine[ring->id].state;
  389. if (intel_execlists_ctx_id(ctx_obj) == request_id) {
  390. WARN(head_req->elsp_submitted == 0,
  391. "Never submitted head request\n");
  392. if (--head_req->elsp_submitted <= 0) {
  393. list_del(&head_req->execlist_link);
  394. queue_work(dev_priv->wq, &head_req->work);
  395. return true;
  396. }
  397. }
  398. }
  399. return false;
  400. }
  401. /**
  402. * intel_execlists_handle_ctx_events() - handle Context Switch interrupts
  403. * @ring: Engine Command Streamer to handle.
  404. *
  405. * Check the unread Context Status Buffers and manage the submission of new
  406. * contexts to the ELSP accordingly.
  407. */
  408. void intel_execlists_handle_ctx_events(struct intel_engine_cs *ring)
  409. {
  410. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  411. u32 status_pointer;
  412. u8 read_pointer;
  413. u8 write_pointer;
  414. u32 status;
  415. u32 status_id;
  416. u32 submit_contexts = 0;
  417. status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
  418. read_pointer = ring->next_context_status_buffer;
  419. write_pointer = status_pointer & 0x07;
  420. if (read_pointer > write_pointer)
  421. write_pointer += 6;
  422. spin_lock(&ring->execlist_lock);
  423. while (read_pointer < write_pointer) {
  424. read_pointer++;
  425. status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
  426. (read_pointer % 6) * 8);
  427. status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
  428. (read_pointer % 6) * 8 + 4);
  429. if (status & GEN8_CTX_STATUS_PREEMPTED) {
  430. if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
  431. if (execlists_check_remove_request(ring, status_id))
  432. WARN(1, "Lite Restored request removed from queue\n");
  433. } else
  434. WARN(1, "Preemption without Lite Restore\n");
  435. }
  436. if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
  437. (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
  438. if (execlists_check_remove_request(ring, status_id))
  439. submit_contexts++;
  440. }
  441. }
  442. if (submit_contexts != 0)
  443. execlists_context_unqueue(ring);
  444. spin_unlock(&ring->execlist_lock);
  445. WARN(submit_contexts > 2, "More than two context complete events?\n");
  446. ring->next_context_status_buffer = write_pointer % 6;
  447. I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
  448. ((u32)ring->next_context_status_buffer & 0x07) << 8);
  449. }
  450. static void execlists_free_request_task(struct work_struct *work)
  451. {
  452. struct intel_ctx_submit_request *req =
  453. container_of(work, struct intel_ctx_submit_request, work);
  454. struct drm_device *dev = req->ring->dev;
  455. struct drm_i915_private *dev_priv = dev->dev_private;
  456. intel_runtime_pm_put(dev_priv);
  457. mutex_lock(&dev->struct_mutex);
  458. i915_gem_context_unreference(req->ctx);
  459. mutex_unlock(&dev->struct_mutex);
  460. kfree(req);
  461. }
  462. static int execlists_context_queue(struct intel_engine_cs *ring,
  463. struct intel_context *to,
  464. u32 tail)
  465. {
  466. struct intel_ctx_submit_request *req = NULL, *cursor;
  467. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  468. unsigned long flags;
  469. int num_elements = 0;
  470. req = kzalloc(sizeof(*req), GFP_KERNEL);
  471. if (req == NULL)
  472. return -ENOMEM;
  473. req->ctx = to;
  474. i915_gem_context_reference(req->ctx);
  475. req->ring = ring;
  476. req->tail = tail;
  477. INIT_WORK(&req->work, execlists_free_request_task);
  478. intel_runtime_pm_get(dev_priv);
  479. spin_lock_irqsave(&ring->execlist_lock, flags);
  480. list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
  481. if (++num_elements > 2)
  482. break;
  483. if (num_elements > 2) {
  484. struct intel_ctx_submit_request *tail_req;
  485. tail_req = list_last_entry(&ring->execlist_queue,
  486. struct intel_ctx_submit_request,
  487. execlist_link);
  488. if (to == tail_req->ctx) {
  489. WARN(tail_req->elsp_submitted != 0,
  490. "More than 2 already-submitted reqs queued\n");
  491. list_del(&tail_req->execlist_link);
  492. queue_work(dev_priv->wq, &tail_req->work);
  493. }
  494. }
  495. list_add_tail(&req->execlist_link, &ring->execlist_queue);
  496. if (num_elements == 0)
  497. execlists_context_unqueue(ring);
  498. spin_unlock_irqrestore(&ring->execlist_lock, flags);
  499. return 0;
  500. }
  501. static int logical_ring_invalidate_all_caches(struct intel_ringbuffer *ringbuf)
  502. {
  503. struct intel_engine_cs *ring = ringbuf->ring;
  504. uint32_t flush_domains;
  505. int ret;
  506. flush_domains = 0;
  507. if (ring->gpu_caches_dirty)
  508. flush_domains = I915_GEM_GPU_DOMAINS;
  509. ret = ring->emit_flush(ringbuf, I915_GEM_GPU_DOMAINS, flush_domains);
  510. if (ret)
  511. return ret;
  512. ring->gpu_caches_dirty = false;
  513. return 0;
  514. }
  515. static int execlists_move_to_gpu(struct intel_ringbuffer *ringbuf,
  516. struct list_head *vmas)
  517. {
  518. struct intel_engine_cs *ring = ringbuf->ring;
  519. struct i915_vma *vma;
  520. uint32_t flush_domains = 0;
  521. bool flush_chipset = false;
  522. int ret;
  523. list_for_each_entry(vma, vmas, exec_list) {
  524. struct drm_i915_gem_object *obj = vma->obj;
  525. ret = i915_gem_object_sync(obj, ring);
  526. if (ret)
  527. return ret;
  528. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  529. flush_chipset |= i915_gem_clflush_object(obj, false);
  530. flush_domains |= obj->base.write_domain;
  531. }
  532. if (flush_domains & I915_GEM_DOMAIN_GTT)
  533. wmb();
  534. /* Unconditionally invalidate gpu caches and ensure that we do flush
  535. * any residual writes from the previous batch.
  536. */
  537. return logical_ring_invalidate_all_caches(ringbuf);
  538. }
  539. /**
  540. * execlists_submission() - submit a batchbuffer for execution, Execlists style
  541. * @dev: DRM device.
  542. * @file: DRM file.
  543. * @ring: Engine Command Streamer to submit to.
  544. * @ctx: Context to employ for this submission.
  545. * @args: execbuffer call arguments.
  546. * @vmas: list of vmas.
  547. * @batch_obj: the batchbuffer to submit.
  548. * @exec_start: batchbuffer start virtual address pointer.
  549. * @flags: translated execbuffer call flags.
  550. *
  551. * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
  552. * away the submission details of the execbuffer ioctl call.
  553. *
  554. * Return: non-zero if the submission fails.
  555. */
  556. int intel_execlists_submission(struct drm_device *dev, struct drm_file *file,
  557. struct intel_engine_cs *ring,
  558. struct intel_context *ctx,
  559. struct drm_i915_gem_execbuffer2 *args,
  560. struct list_head *vmas,
  561. struct drm_i915_gem_object *batch_obj,
  562. u64 exec_start, u32 flags)
  563. {
  564. struct drm_i915_private *dev_priv = dev->dev_private;
  565. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  566. int instp_mode;
  567. u32 instp_mask;
  568. int ret;
  569. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  570. instp_mask = I915_EXEC_CONSTANTS_MASK;
  571. switch (instp_mode) {
  572. case I915_EXEC_CONSTANTS_REL_GENERAL:
  573. case I915_EXEC_CONSTANTS_ABSOLUTE:
  574. case I915_EXEC_CONSTANTS_REL_SURFACE:
  575. if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
  576. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  577. return -EINVAL;
  578. }
  579. if (instp_mode != dev_priv->relative_constants_mode) {
  580. if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  581. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  582. return -EINVAL;
  583. }
  584. /* The HW changed the meaning on this bit on gen6 */
  585. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  586. }
  587. break;
  588. default:
  589. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  590. return -EINVAL;
  591. }
  592. if (args->num_cliprects != 0) {
  593. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  594. return -EINVAL;
  595. } else {
  596. if (args->DR4 == 0xffffffff) {
  597. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  598. args->DR4 = 0;
  599. }
  600. if (args->DR1 || args->DR4 || args->cliprects_ptr) {
  601. DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
  602. return -EINVAL;
  603. }
  604. }
  605. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  606. DRM_DEBUG("sol reset is gen7 only\n");
  607. return -EINVAL;
  608. }
  609. ret = execlists_move_to_gpu(ringbuf, vmas);
  610. if (ret)
  611. return ret;
  612. if (ring == &dev_priv->ring[RCS] &&
  613. instp_mode != dev_priv->relative_constants_mode) {
  614. ret = intel_logical_ring_begin(ringbuf, 4);
  615. if (ret)
  616. return ret;
  617. intel_logical_ring_emit(ringbuf, MI_NOOP);
  618. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
  619. intel_logical_ring_emit(ringbuf, INSTPM);
  620. intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
  621. intel_logical_ring_advance(ringbuf);
  622. dev_priv->relative_constants_mode = instp_mode;
  623. }
  624. ret = ring->emit_bb_start(ringbuf, exec_start, flags);
  625. if (ret)
  626. return ret;
  627. i915_gem_execbuffer_move_to_active(vmas, ring);
  628. i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  629. return 0;
  630. }
  631. void intel_logical_ring_stop(struct intel_engine_cs *ring)
  632. {
  633. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  634. int ret;
  635. if (!intel_ring_initialized(ring))
  636. return;
  637. ret = intel_ring_idle(ring);
  638. if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
  639. DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
  640. ring->name, ret);
  641. /* TODO: Is this correct with Execlists enabled? */
  642. I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
  643. if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
  644. DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
  645. return;
  646. }
  647. I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
  648. }
  649. int logical_ring_flush_all_caches(struct intel_ringbuffer *ringbuf)
  650. {
  651. struct intel_engine_cs *ring = ringbuf->ring;
  652. int ret;
  653. if (!ring->gpu_caches_dirty)
  654. return 0;
  655. ret = ring->emit_flush(ringbuf, 0, I915_GEM_GPU_DOMAINS);
  656. if (ret)
  657. return ret;
  658. ring->gpu_caches_dirty = false;
  659. return 0;
  660. }
  661. /**
  662. * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
  663. * @ringbuf: Logical Ringbuffer to advance.
  664. *
  665. * The tail is updated in our logical ringbuffer struct, not in the actual context. What
  666. * really happens during submission is that the context and current tail will be placed
  667. * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
  668. * point, the tail *inside* the context is updated and the ELSP written to.
  669. */
  670. void intel_logical_ring_advance_and_submit(struct intel_ringbuffer *ringbuf)
  671. {
  672. struct intel_engine_cs *ring = ringbuf->ring;
  673. struct intel_context *ctx = ringbuf->FIXME_lrc_ctx;
  674. intel_logical_ring_advance(ringbuf);
  675. if (intel_ring_stopped(ring))
  676. return;
  677. execlists_context_queue(ring, ctx, ringbuf->tail);
  678. }
  679. static int logical_ring_alloc_seqno(struct intel_engine_cs *ring,
  680. struct intel_context *ctx)
  681. {
  682. if (ring->outstanding_lazy_seqno)
  683. return 0;
  684. if (ring->preallocated_lazy_request == NULL) {
  685. struct drm_i915_gem_request *request;
  686. request = kmalloc(sizeof(*request), GFP_KERNEL);
  687. if (request == NULL)
  688. return -ENOMEM;
  689. /* Hold a reference to the context this request belongs to
  690. * (we will need it when the time comes to emit/retire the
  691. * request).
  692. */
  693. request->ctx = ctx;
  694. i915_gem_context_reference(request->ctx);
  695. ring->preallocated_lazy_request = request;
  696. }
  697. return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_seqno);
  698. }
  699. static int logical_ring_wait_request(struct intel_ringbuffer *ringbuf,
  700. int bytes)
  701. {
  702. struct intel_engine_cs *ring = ringbuf->ring;
  703. struct drm_i915_gem_request *request;
  704. u32 seqno = 0;
  705. int ret;
  706. if (ringbuf->last_retired_head != -1) {
  707. ringbuf->head = ringbuf->last_retired_head;
  708. ringbuf->last_retired_head = -1;
  709. ringbuf->space = intel_ring_space(ringbuf);
  710. if (ringbuf->space >= bytes)
  711. return 0;
  712. }
  713. list_for_each_entry(request, &ring->request_list, list) {
  714. if (__intel_ring_space(request->tail, ringbuf->tail,
  715. ringbuf->size) >= bytes) {
  716. seqno = request->seqno;
  717. break;
  718. }
  719. }
  720. if (seqno == 0)
  721. return -ENOSPC;
  722. ret = i915_wait_seqno(ring, seqno);
  723. if (ret)
  724. return ret;
  725. i915_gem_retire_requests_ring(ring);
  726. ringbuf->head = ringbuf->last_retired_head;
  727. ringbuf->last_retired_head = -1;
  728. ringbuf->space = intel_ring_space(ringbuf);
  729. return 0;
  730. }
  731. static int logical_ring_wait_for_space(struct intel_ringbuffer *ringbuf,
  732. int bytes)
  733. {
  734. struct intel_engine_cs *ring = ringbuf->ring;
  735. struct drm_device *dev = ring->dev;
  736. struct drm_i915_private *dev_priv = dev->dev_private;
  737. unsigned long end;
  738. int ret;
  739. ret = logical_ring_wait_request(ringbuf, bytes);
  740. if (ret != -ENOSPC)
  741. return ret;
  742. /* Force the context submission in case we have been skipping it */
  743. intel_logical_ring_advance_and_submit(ringbuf);
  744. /* With GEM the hangcheck timer should kick us out of the loop,
  745. * leaving it early runs the risk of corrupting GEM state (due
  746. * to running on almost untested codepaths). But on resume
  747. * timers don't work yet, so prevent a complete hang in that
  748. * case by choosing an insanely large timeout. */
  749. end = jiffies + 60 * HZ;
  750. do {
  751. ringbuf->head = I915_READ_HEAD(ring);
  752. ringbuf->space = intel_ring_space(ringbuf);
  753. if (ringbuf->space >= bytes) {
  754. ret = 0;
  755. break;
  756. }
  757. msleep(1);
  758. if (dev_priv->mm.interruptible && signal_pending(current)) {
  759. ret = -ERESTARTSYS;
  760. break;
  761. }
  762. ret = i915_gem_check_wedge(&dev_priv->gpu_error,
  763. dev_priv->mm.interruptible);
  764. if (ret)
  765. break;
  766. if (time_after(jiffies, end)) {
  767. ret = -EBUSY;
  768. break;
  769. }
  770. } while (1);
  771. return ret;
  772. }
  773. static int logical_ring_wrap_buffer(struct intel_ringbuffer *ringbuf)
  774. {
  775. uint32_t __iomem *virt;
  776. int rem = ringbuf->size - ringbuf->tail;
  777. if (ringbuf->space < rem) {
  778. int ret = logical_ring_wait_for_space(ringbuf, rem);
  779. if (ret)
  780. return ret;
  781. }
  782. virt = ringbuf->virtual_start + ringbuf->tail;
  783. rem /= 4;
  784. while (rem--)
  785. iowrite32(MI_NOOP, virt++);
  786. ringbuf->tail = 0;
  787. ringbuf->space = intel_ring_space(ringbuf);
  788. return 0;
  789. }
  790. static int logical_ring_prepare(struct intel_ringbuffer *ringbuf, int bytes)
  791. {
  792. int ret;
  793. if (unlikely(ringbuf->tail + bytes > ringbuf->effective_size)) {
  794. ret = logical_ring_wrap_buffer(ringbuf);
  795. if (unlikely(ret))
  796. return ret;
  797. }
  798. if (unlikely(ringbuf->space < bytes)) {
  799. ret = logical_ring_wait_for_space(ringbuf, bytes);
  800. if (unlikely(ret))
  801. return ret;
  802. }
  803. return 0;
  804. }
  805. /**
  806. * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
  807. *
  808. * @ringbuf: Logical ringbuffer.
  809. * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
  810. *
  811. * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
  812. * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
  813. * and also preallocates a request (every workload submission is still mediated through
  814. * requests, same as it did with legacy ringbuffer submission).
  815. *
  816. * Return: non-zero if the ringbuffer is not ready to be written to.
  817. */
  818. int intel_logical_ring_begin(struct intel_ringbuffer *ringbuf, int num_dwords)
  819. {
  820. struct intel_engine_cs *ring = ringbuf->ring;
  821. struct drm_device *dev = ring->dev;
  822. struct drm_i915_private *dev_priv = dev->dev_private;
  823. int ret;
  824. ret = i915_gem_check_wedge(&dev_priv->gpu_error,
  825. dev_priv->mm.interruptible);
  826. if (ret)
  827. return ret;
  828. ret = logical_ring_prepare(ringbuf, num_dwords * sizeof(uint32_t));
  829. if (ret)
  830. return ret;
  831. /* Preallocate the olr before touching the ring */
  832. ret = logical_ring_alloc_seqno(ring, ringbuf->FIXME_lrc_ctx);
  833. if (ret)
  834. return ret;
  835. ringbuf->space -= num_dwords * sizeof(uint32_t);
  836. return 0;
  837. }
  838. static int intel_logical_ring_workarounds_emit(struct intel_engine_cs *ring,
  839. struct intel_context *ctx)
  840. {
  841. int ret, i;
  842. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  843. struct drm_device *dev = ring->dev;
  844. struct drm_i915_private *dev_priv = dev->dev_private;
  845. struct i915_workarounds *w = &dev_priv->workarounds;
  846. if (WARN_ON(w->count == 0))
  847. return 0;
  848. ring->gpu_caches_dirty = true;
  849. ret = logical_ring_flush_all_caches(ringbuf);
  850. if (ret)
  851. return ret;
  852. ret = intel_logical_ring_begin(ringbuf, w->count * 2 + 2);
  853. if (ret)
  854. return ret;
  855. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
  856. for (i = 0; i < w->count; i++) {
  857. intel_logical_ring_emit(ringbuf, w->reg[i].addr);
  858. intel_logical_ring_emit(ringbuf, w->reg[i].value);
  859. }
  860. intel_logical_ring_emit(ringbuf, MI_NOOP);
  861. intel_logical_ring_advance(ringbuf);
  862. ring->gpu_caches_dirty = true;
  863. ret = logical_ring_flush_all_caches(ringbuf);
  864. if (ret)
  865. return ret;
  866. return 0;
  867. }
  868. static int gen8_init_common_ring(struct intel_engine_cs *ring)
  869. {
  870. struct drm_device *dev = ring->dev;
  871. struct drm_i915_private *dev_priv = dev->dev_private;
  872. I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
  873. I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
  874. I915_WRITE(RING_MODE_GEN7(ring),
  875. _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
  876. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  877. POSTING_READ(RING_MODE_GEN7(ring));
  878. DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
  879. memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
  880. return 0;
  881. }
  882. static int gen8_init_render_ring(struct intel_engine_cs *ring)
  883. {
  884. struct drm_device *dev = ring->dev;
  885. struct drm_i915_private *dev_priv = dev->dev_private;
  886. int ret;
  887. ret = gen8_init_common_ring(ring);
  888. if (ret)
  889. return ret;
  890. /* We need to disable the AsyncFlip performance optimisations in order
  891. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  892. * programmed to '1' on all products.
  893. *
  894. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  895. */
  896. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  897. ret = intel_init_pipe_control(ring);
  898. if (ret)
  899. return ret;
  900. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  901. return init_workarounds_ring(ring);
  902. }
  903. static int gen8_emit_bb_start(struct intel_ringbuffer *ringbuf,
  904. u64 offset, unsigned flags)
  905. {
  906. bool ppgtt = !(flags & I915_DISPATCH_SECURE);
  907. int ret;
  908. ret = intel_logical_ring_begin(ringbuf, 4);
  909. if (ret)
  910. return ret;
  911. /* FIXME(BDW): Address space and security selectors. */
  912. intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
  913. intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
  914. intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
  915. intel_logical_ring_emit(ringbuf, MI_NOOP);
  916. intel_logical_ring_advance(ringbuf);
  917. return 0;
  918. }
  919. static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
  920. {
  921. struct drm_device *dev = ring->dev;
  922. struct drm_i915_private *dev_priv = dev->dev_private;
  923. unsigned long flags;
  924. if (WARN_ON(!intel_irqs_enabled(dev_priv)))
  925. return false;
  926. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  927. if (ring->irq_refcount++ == 0) {
  928. I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
  929. POSTING_READ(RING_IMR(ring->mmio_base));
  930. }
  931. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  932. return true;
  933. }
  934. static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
  935. {
  936. struct drm_device *dev = ring->dev;
  937. struct drm_i915_private *dev_priv = dev->dev_private;
  938. unsigned long flags;
  939. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  940. if (--ring->irq_refcount == 0) {
  941. I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
  942. POSTING_READ(RING_IMR(ring->mmio_base));
  943. }
  944. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  945. }
  946. static int gen8_emit_flush(struct intel_ringbuffer *ringbuf,
  947. u32 invalidate_domains,
  948. u32 unused)
  949. {
  950. struct intel_engine_cs *ring = ringbuf->ring;
  951. struct drm_device *dev = ring->dev;
  952. struct drm_i915_private *dev_priv = dev->dev_private;
  953. uint32_t cmd;
  954. int ret;
  955. ret = intel_logical_ring_begin(ringbuf, 4);
  956. if (ret)
  957. return ret;
  958. cmd = MI_FLUSH_DW + 1;
  959. if (ring == &dev_priv->ring[VCS]) {
  960. if (invalidate_domains & I915_GEM_GPU_DOMAINS)
  961. cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
  962. MI_FLUSH_DW_STORE_INDEX |
  963. MI_FLUSH_DW_OP_STOREDW;
  964. } else {
  965. if (invalidate_domains & I915_GEM_DOMAIN_RENDER)
  966. cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
  967. MI_FLUSH_DW_OP_STOREDW;
  968. }
  969. intel_logical_ring_emit(ringbuf, cmd);
  970. intel_logical_ring_emit(ringbuf,
  971. I915_GEM_HWS_SCRATCH_ADDR |
  972. MI_FLUSH_DW_USE_GTT);
  973. intel_logical_ring_emit(ringbuf, 0); /* upper addr */
  974. intel_logical_ring_emit(ringbuf, 0); /* value */
  975. intel_logical_ring_advance(ringbuf);
  976. return 0;
  977. }
  978. static int gen8_emit_flush_render(struct intel_ringbuffer *ringbuf,
  979. u32 invalidate_domains,
  980. u32 flush_domains)
  981. {
  982. struct intel_engine_cs *ring = ringbuf->ring;
  983. u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  984. u32 flags = 0;
  985. int ret;
  986. flags |= PIPE_CONTROL_CS_STALL;
  987. if (flush_domains) {
  988. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  989. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  990. }
  991. if (invalidate_domains) {
  992. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  993. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  994. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  995. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  996. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  997. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  998. flags |= PIPE_CONTROL_QW_WRITE;
  999. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1000. }
  1001. ret = intel_logical_ring_begin(ringbuf, 6);
  1002. if (ret)
  1003. return ret;
  1004. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  1005. intel_logical_ring_emit(ringbuf, flags);
  1006. intel_logical_ring_emit(ringbuf, scratch_addr);
  1007. intel_logical_ring_emit(ringbuf, 0);
  1008. intel_logical_ring_emit(ringbuf, 0);
  1009. intel_logical_ring_emit(ringbuf, 0);
  1010. intel_logical_ring_advance(ringbuf);
  1011. return 0;
  1012. }
  1013. static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
  1014. {
  1015. return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
  1016. }
  1017. static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
  1018. {
  1019. intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
  1020. }
  1021. static int gen8_emit_request(struct intel_ringbuffer *ringbuf)
  1022. {
  1023. struct intel_engine_cs *ring = ringbuf->ring;
  1024. u32 cmd;
  1025. int ret;
  1026. ret = intel_logical_ring_begin(ringbuf, 6);
  1027. if (ret)
  1028. return ret;
  1029. cmd = MI_STORE_DWORD_IMM_GEN8;
  1030. cmd |= MI_GLOBAL_GTT;
  1031. intel_logical_ring_emit(ringbuf, cmd);
  1032. intel_logical_ring_emit(ringbuf,
  1033. (ring->status_page.gfx_addr +
  1034. (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
  1035. intel_logical_ring_emit(ringbuf, 0);
  1036. intel_logical_ring_emit(ringbuf, ring->outstanding_lazy_seqno);
  1037. intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
  1038. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1039. intel_logical_ring_advance_and_submit(ringbuf);
  1040. return 0;
  1041. }
  1042. /**
  1043. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1044. *
  1045. * @ring: Engine Command Streamer.
  1046. *
  1047. */
  1048. void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
  1049. {
  1050. struct drm_i915_private *dev_priv;
  1051. if (!intel_ring_initialized(ring))
  1052. return;
  1053. dev_priv = ring->dev->dev_private;
  1054. intel_logical_ring_stop(ring);
  1055. WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
  1056. ring->preallocated_lazy_request = NULL;
  1057. ring->outstanding_lazy_seqno = 0;
  1058. if (ring->cleanup)
  1059. ring->cleanup(ring);
  1060. i915_cmd_parser_fini_ring(ring);
  1061. if (ring->status_page.obj) {
  1062. kunmap(sg_page(ring->status_page.obj->pages->sgl));
  1063. ring->status_page.obj = NULL;
  1064. }
  1065. }
  1066. static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
  1067. {
  1068. int ret;
  1069. /* Intentionally left blank. */
  1070. ring->buffer = NULL;
  1071. ring->dev = dev;
  1072. INIT_LIST_HEAD(&ring->active_list);
  1073. INIT_LIST_HEAD(&ring->request_list);
  1074. init_waitqueue_head(&ring->irq_queue);
  1075. INIT_LIST_HEAD(&ring->execlist_queue);
  1076. spin_lock_init(&ring->execlist_lock);
  1077. ring->next_context_status_buffer = 0;
  1078. ret = i915_cmd_parser_init_ring(ring);
  1079. if (ret)
  1080. return ret;
  1081. if (ring->init) {
  1082. ret = ring->init(ring);
  1083. if (ret)
  1084. return ret;
  1085. }
  1086. ret = intel_lr_context_deferred_create(ring->default_context, ring);
  1087. return ret;
  1088. }
  1089. static int logical_render_ring_init(struct drm_device *dev)
  1090. {
  1091. struct drm_i915_private *dev_priv = dev->dev_private;
  1092. struct intel_engine_cs *ring = &dev_priv->ring[RCS];
  1093. ring->name = "render ring";
  1094. ring->id = RCS;
  1095. ring->mmio_base = RENDER_RING_BASE;
  1096. ring->irq_enable_mask =
  1097. GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
  1098. ring->irq_keep_mask =
  1099. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
  1100. if (HAS_L3_DPF(dev))
  1101. ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1102. ring->init = gen8_init_render_ring;
  1103. ring->init_context = intel_logical_ring_workarounds_emit;
  1104. ring->cleanup = intel_fini_pipe_control;
  1105. ring->get_seqno = gen8_get_seqno;
  1106. ring->set_seqno = gen8_set_seqno;
  1107. ring->emit_request = gen8_emit_request;
  1108. ring->emit_flush = gen8_emit_flush_render;
  1109. ring->irq_get = gen8_logical_ring_get_irq;
  1110. ring->irq_put = gen8_logical_ring_put_irq;
  1111. ring->emit_bb_start = gen8_emit_bb_start;
  1112. return logical_ring_init(dev, ring);
  1113. }
  1114. static int logical_bsd_ring_init(struct drm_device *dev)
  1115. {
  1116. struct drm_i915_private *dev_priv = dev->dev_private;
  1117. struct intel_engine_cs *ring = &dev_priv->ring[VCS];
  1118. ring->name = "bsd ring";
  1119. ring->id = VCS;
  1120. ring->mmio_base = GEN6_BSD_RING_BASE;
  1121. ring->irq_enable_mask =
  1122. GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1123. ring->irq_keep_mask =
  1124. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1125. ring->init = gen8_init_common_ring;
  1126. ring->get_seqno = gen8_get_seqno;
  1127. ring->set_seqno = gen8_set_seqno;
  1128. ring->emit_request = gen8_emit_request;
  1129. ring->emit_flush = gen8_emit_flush;
  1130. ring->irq_get = gen8_logical_ring_get_irq;
  1131. ring->irq_put = gen8_logical_ring_put_irq;
  1132. ring->emit_bb_start = gen8_emit_bb_start;
  1133. return logical_ring_init(dev, ring);
  1134. }
  1135. static int logical_bsd2_ring_init(struct drm_device *dev)
  1136. {
  1137. struct drm_i915_private *dev_priv = dev->dev_private;
  1138. struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
  1139. ring->name = "bds2 ring";
  1140. ring->id = VCS2;
  1141. ring->mmio_base = GEN8_BSD2_RING_BASE;
  1142. ring->irq_enable_mask =
  1143. GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  1144. ring->irq_keep_mask =
  1145. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  1146. ring->init = gen8_init_common_ring;
  1147. ring->get_seqno = gen8_get_seqno;
  1148. ring->set_seqno = gen8_set_seqno;
  1149. ring->emit_request = gen8_emit_request;
  1150. ring->emit_flush = gen8_emit_flush;
  1151. ring->irq_get = gen8_logical_ring_get_irq;
  1152. ring->irq_put = gen8_logical_ring_put_irq;
  1153. ring->emit_bb_start = gen8_emit_bb_start;
  1154. return logical_ring_init(dev, ring);
  1155. }
  1156. static int logical_blt_ring_init(struct drm_device *dev)
  1157. {
  1158. struct drm_i915_private *dev_priv = dev->dev_private;
  1159. struct intel_engine_cs *ring = &dev_priv->ring[BCS];
  1160. ring->name = "blitter ring";
  1161. ring->id = BCS;
  1162. ring->mmio_base = BLT_RING_BASE;
  1163. ring->irq_enable_mask =
  1164. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1165. ring->irq_keep_mask =
  1166. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1167. ring->init = gen8_init_common_ring;
  1168. ring->get_seqno = gen8_get_seqno;
  1169. ring->set_seqno = gen8_set_seqno;
  1170. ring->emit_request = gen8_emit_request;
  1171. ring->emit_flush = gen8_emit_flush;
  1172. ring->irq_get = gen8_logical_ring_get_irq;
  1173. ring->irq_put = gen8_logical_ring_put_irq;
  1174. ring->emit_bb_start = gen8_emit_bb_start;
  1175. return logical_ring_init(dev, ring);
  1176. }
  1177. static int logical_vebox_ring_init(struct drm_device *dev)
  1178. {
  1179. struct drm_i915_private *dev_priv = dev->dev_private;
  1180. struct intel_engine_cs *ring = &dev_priv->ring[VECS];
  1181. ring->name = "video enhancement ring";
  1182. ring->id = VECS;
  1183. ring->mmio_base = VEBOX_RING_BASE;
  1184. ring->irq_enable_mask =
  1185. GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1186. ring->irq_keep_mask =
  1187. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1188. ring->init = gen8_init_common_ring;
  1189. ring->get_seqno = gen8_get_seqno;
  1190. ring->set_seqno = gen8_set_seqno;
  1191. ring->emit_request = gen8_emit_request;
  1192. ring->emit_flush = gen8_emit_flush;
  1193. ring->irq_get = gen8_logical_ring_get_irq;
  1194. ring->irq_put = gen8_logical_ring_put_irq;
  1195. ring->emit_bb_start = gen8_emit_bb_start;
  1196. return logical_ring_init(dev, ring);
  1197. }
  1198. /**
  1199. * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
  1200. * @dev: DRM device.
  1201. *
  1202. * This function inits the engines for an Execlists submission style (the equivalent in the
  1203. * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
  1204. * those engines that are present in the hardware.
  1205. *
  1206. * Return: non-zero if the initialization failed.
  1207. */
  1208. int intel_logical_rings_init(struct drm_device *dev)
  1209. {
  1210. struct drm_i915_private *dev_priv = dev->dev_private;
  1211. int ret;
  1212. ret = logical_render_ring_init(dev);
  1213. if (ret)
  1214. return ret;
  1215. if (HAS_BSD(dev)) {
  1216. ret = logical_bsd_ring_init(dev);
  1217. if (ret)
  1218. goto cleanup_render_ring;
  1219. }
  1220. if (HAS_BLT(dev)) {
  1221. ret = logical_blt_ring_init(dev);
  1222. if (ret)
  1223. goto cleanup_bsd_ring;
  1224. }
  1225. if (HAS_VEBOX(dev)) {
  1226. ret = logical_vebox_ring_init(dev);
  1227. if (ret)
  1228. goto cleanup_blt_ring;
  1229. }
  1230. if (HAS_BSD2(dev)) {
  1231. ret = logical_bsd2_ring_init(dev);
  1232. if (ret)
  1233. goto cleanup_vebox_ring;
  1234. }
  1235. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  1236. if (ret)
  1237. goto cleanup_bsd2_ring;
  1238. return 0;
  1239. cleanup_bsd2_ring:
  1240. intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
  1241. cleanup_vebox_ring:
  1242. intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
  1243. cleanup_blt_ring:
  1244. intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
  1245. cleanup_bsd_ring:
  1246. intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
  1247. cleanup_render_ring:
  1248. intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
  1249. return ret;
  1250. }
  1251. int intel_lr_context_render_state_init(struct intel_engine_cs *ring,
  1252. struct intel_context *ctx)
  1253. {
  1254. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  1255. struct render_state so;
  1256. struct drm_i915_file_private *file_priv = ctx->file_priv;
  1257. struct drm_file *file = file_priv ? file_priv->file : NULL;
  1258. int ret;
  1259. ret = i915_gem_render_state_prepare(ring, &so);
  1260. if (ret)
  1261. return ret;
  1262. if (so.rodata == NULL)
  1263. return 0;
  1264. ret = ring->emit_bb_start(ringbuf,
  1265. so.ggtt_offset,
  1266. I915_DISPATCH_SECURE);
  1267. if (ret)
  1268. goto out;
  1269. i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), ring);
  1270. ret = __i915_add_request(ring, file, so.obj, NULL);
  1271. /* intel_logical_ring_add_request moves object to inactive if it
  1272. * fails */
  1273. out:
  1274. i915_gem_render_state_fini(&so);
  1275. return ret;
  1276. }
  1277. static int
  1278. populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
  1279. struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
  1280. {
  1281. struct drm_device *dev = ring->dev;
  1282. struct drm_i915_private *dev_priv = dev->dev_private;
  1283. struct drm_i915_gem_object *ring_obj = ringbuf->obj;
  1284. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
  1285. struct page *page;
  1286. uint32_t *reg_state;
  1287. int ret;
  1288. if (!ppgtt)
  1289. ppgtt = dev_priv->mm.aliasing_ppgtt;
  1290. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1291. if (ret) {
  1292. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1293. return ret;
  1294. }
  1295. ret = i915_gem_object_get_pages(ctx_obj);
  1296. if (ret) {
  1297. DRM_DEBUG_DRIVER("Could not get object pages\n");
  1298. return ret;
  1299. }
  1300. i915_gem_object_pin_pages(ctx_obj);
  1301. /* The second page of the context object contains some fields which must
  1302. * be set up prior to the first execution. */
  1303. page = i915_gem_object_get_page(ctx_obj, 1);
  1304. reg_state = kmap_atomic(page);
  1305. /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
  1306. * commands followed by (reg, value) pairs. The values we are setting here are
  1307. * only for the first context restore: on a subsequent save, the GPU will
  1308. * recreate this batchbuffer with new values (including all the missing
  1309. * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
  1310. if (ring->id == RCS)
  1311. reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
  1312. else
  1313. reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
  1314. reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
  1315. reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
  1316. reg_state[CTX_CONTEXT_CONTROL+1] =
  1317. _MASKED_BIT_ENABLE((1<<3) | MI_RESTORE_INHIBIT);
  1318. reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
  1319. reg_state[CTX_RING_HEAD+1] = 0;
  1320. reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
  1321. reg_state[CTX_RING_TAIL+1] = 0;
  1322. reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
  1323. reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(ring_obj);
  1324. reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
  1325. reg_state[CTX_RING_BUFFER_CONTROL+1] =
  1326. ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
  1327. reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
  1328. reg_state[CTX_BB_HEAD_U+1] = 0;
  1329. reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
  1330. reg_state[CTX_BB_HEAD_L+1] = 0;
  1331. reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
  1332. reg_state[CTX_BB_STATE+1] = (1<<5);
  1333. reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
  1334. reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
  1335. reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
  1336. reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
  1337. reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
  1338. reg_state[CTX_SECOND_BB_STATE+1] = 0;
  1339. if (ring->id == RCS) {
  1340. /* TODO: according to BSpec, the register state context
  1341. * for CHV does not have these. OTOH, these registers do
  1342. * exist in CHV. I'm waiting for a clarification */
  1343. reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
  1344. reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
  1345. reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
  1346. reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
  1347. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
  1348. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
  1349. }
  1350. reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
  1351. reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
  1352. reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
  1353. reg_state[CTX_CTX_TIMESTAMP+1] = 0;
  1354. reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
  1355. reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
  1356. reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
  1357. reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
  1358. reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
  1359. reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
  1360. reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
  1361. reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
  1362. reg_state[CTX_PDP3_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[3]);
  1363. reg_state[CTX_PDP3_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[3]);
  1364. reg_state[CTX_PDP2_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[2]);
  1365. reg_state[CTX_PDP2_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[2]);
  1366. reg_state[CTX_PDP1_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[1]);
  1367. reg_state[CTX_PDP1_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[1]);
  1368. reg_state[CTX_PDP0_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[0]);
  1369. reg_state[CTX_PDP0_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[0]);
  1370. if (ring->id == RCS) {
  1371. reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  1372. reg_state[CTX_R_PWR_CLK_STATE] = 0x20c8;
  1373. reg_state[CTX_R_PWR_CLK_STATE+1] = 0;
  1374. }
  1375. kunmap_atomic(reg_state);
  1376. ctx_obj->dirty = 1;
  1377. set_page_dirty(page);
  1378. i915_gem_object_unpin_pages(ctx_obj);
  1379. return 0;
  1380. }
  1381. /**
  1382. * intel_lr_context_free() - free the LRC specific bits of a context
  1383. * @ctx: the LR context to free.
  1384. *
  1385. * The real context freeing is done in i915_gem_context_free: this only
  1386. * takes care of the bits that are LRC related: the per-engine backing
  1387. * objects and the logical ringbuffer.
  1388. */
  1389. void intel_lr_context_free(struct intel_context *ctx)
  1390. {
  1391. int i;
  1392. for (i = 0; i < I915_NUM_RINGS; i++) {
  1393. struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
  1394. struct intel_ringbuffer *ringbuf = ctx->engine[i].ringbuf;
  1395. if (ctx_obj) {
  1396. intel_destroy_ringbuffer_obj(ringbuf);
  1397. kfree(ringbuf);
  1398. i915_gem_object_ggtt_unpin(ctx_obj);
  1399. drm_gem_object_unreference(&ctx_obj->base);
  1400. }
  1401. }
  1402. }
  1403. static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
  1404. {
  1405. int ret = 0;
  1406. WARN_ON(INTEL_INFO(ring->dev)->gen != 8);
  1407. switch (ring->id) {
  1408. case RCS:
  1409. ret = GEN8_LR_CONTEXT_RENDER_SIZE;
  1410. break;
  1411. case VCS:
  1412. case BCS:
  1413. case VECS:
  1414. case VCS2:
  1415. ret = GEN8_LR_CONTEXT_OTHER_SIZE;
  1416. break;
  1417. }
  1418. return ret;
  1419. }
  1420. static int lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
  1421. struct drm_i915_gem_object *default_ctx_obj)
  1422. {
  1423. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1424. /* The status page is offset 0 from the default context object
  1425. * in LRC mode. */
  1426. ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
  1427. ring->status_page.page_addr =
  1428. kmap(sg_page(default_ctx_obj->pages->sgl));
  1429. if (ring->status_page.page_addr == NULL)
  1430. return -ENOMEM;
  1431. ring->status_page.obj = default_ctx_obj;
  1432. I915_WRITE(RING_HWS_PGA(ring->mmio_base),
  1433. (u32)ring->status_page.gfx_addr);
  1434. POSTING_READ(RING_HWS_PGA(ring->mmio_base));
  1435. return 0;
  1436. }
  1437. /**
  1438. * intel_lr_context_deferred_create() - create the LRC specific bits of a context
  1439. * @ctx: LR context to create.
  1440. * @ring: engine to be used with the context.
  1441. *
  1442. * This function can be called more than once, with different engines, if we plan
  1443. * to use the context with them. The context backing objects and the ringbuffers
  1444. * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
  1445. * the creation is a deferred call: it's better to make sure first that we need to use
  1446. * a given ring with the context.
  1447. *
  1448. * Return: non-zero on error.
  1449. */
  1450. int intel_lr_context_deferred_create(struct intel_context *ctx,
  1451. struct intel_engine_cs *ring)
  1452. {
  1453. struct drm_device *dev = ring->dev;
  1454. struct drm_i915_gem_object *ctx_obj;
  1455. uint32_t context_size;
  1456. struct intel_ringbuffer *ringbuf;
  1457. int ret;
  1458. WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
  1459. if (ctx->engine[ring->id].state)
  1460. return 0;
  1461. context_size = round_up(get_lr_context_size(ring), 4096);
  1462. ctx_obj = i915_gem_alloc_context_obj(dev, context_size);
  1463. if (IS_ERR(ctx_obj)) {
  1464. ret = PTR_ERR(ctx_obj);
  1465. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed: %d\n", ret);
  1466. return ret;
  1467. }
  1468. ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
  1469. if (ret) {
  1470. DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n", ret);
  1471. drm_gem_object_unreference(&ctx_obj->base);
  1472. return ret;
  1473. }
  1474. ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
  1475. if (!ringbuf) {
  1476. DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
  1477. ring->name);
  1478. i915_gem_object_ggtt_unpin(ctx_obj);
  1479. drm_gem_object_unreference(&ctx_obj->base);
  1480. ret = -ENOMEM;
  1481. return ret;
  1482. }
  1483. ringbuf->ring = ring;
  1484. ringbuf->FIXME_lrc_ctx = ctx;
  1485. ringbuf->size = 32 * PAGE_SIZE;
  1486. ringbuf->effective_size = ringbuf->size;
  1487. ringbuf->head = 0;
  1488. ringbuf->tail = 0;
  1489. ringbuf->space = ringbuf->size;
  1490. ringbuf->last_retired_head = -1;
  1491. /* TODO: For now we put this in the mappable region so that we can reuse
  1492. * the existing ringbuffer code which ioremaps it. When we start
  1493. * creating many contexts, this will no longer work and we must switch
  1494. * to a kmapish interface.
  1495. */
  1496. ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
  1497. if (ret) {
  1498. DRM_DEBUG_DRIVER("Failed to allocate ringbuffer obj %s: %d\n",
  1499. ring->name, ret);
  1500. goto error;
  1501. }
  1502. ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
  1503. if (ret) {
  1504. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  1505. intel_destroy_ringbuffer_obj(ringbuf);
  1506. goto error;
  1507. }
  1508. ctx->engine[ring->id].ringbuf = ringbuf;
  1509. ctx->engine[ring->id].state = ctx_obj;
  1510. if (ctx == ring->default_context) {
  1511. ret = lrc_setup_hardware_status_page(ring, ctx_obj);
  1512. if (ret) {
  1513. DRM_ERROR("Failed to setup hardware status page\n");
  1514. goto error;
  1515. }
  1516. }
  1517. if (ring->id == RCS && !ctx->rcs_initialized) {
  1518. if (ring->init_context) {
  1519. ret = ring->init_context(ring, ctx);
  1520. if (ret)
  1521. DRM_ERROR("ring init context: %d\n", ret);
  1522. }
  1523. ret = intel_lr_context_render_state_init(ring, ctx);
  1524. if (ret) {
  1525. DRM_ERROR("Init render state failed: %d\n", ret);
  1526. ctx->engine[ring->id].ringbuf = NULL;
  1527. ctx->engine[ring->id].state = NULL;
  1528. intel_destroy_ringbuffer_obj(ringbuf);
  1529. goto error;
  1530. }
  1531. ctx->rcs_initialized = true;
  1532. }
  1533. return 0;
  1534. error:
  1535. kfree(ringbuf);
  1536. i915_gem_object_ggtt_unpin(ctx_obj);
  1537. drm_gem_object_unreference(&ctx_obj->base);
  1538. return ret;
  1539. }