intel_lrc.c 52 KB

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