intel_lrc.c 57 KB

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