intel_lrc.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  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. /* We always require a command barrier so that subsequent
  931. * commands, such as breadcrumb interrupts, are strictly ordered
  932. * wrt the contents of the write cache being flushed to memory
  933. * (and thus being coherent from the CPU).
  934. */
  935. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  936. if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
  937. cmd |= MI_INVALIDATE_TLB;
  938. if (ring == &dev_priv->ring[VCS])
  939. cmd |= MI_INVALIDATE_BSD;
  940. }
  941. intel_logical_ring_emit(ringbuf, cmd);
  942. intel_logical_ring_emit(ringbuf,
  943. I915_GEM_HWS_SCRATCH_ADDR |
  944. MI_FLUSH_DW_USE_GTT);
  945. intel_logical_ring_emit(ringbuf, 0); /* upper addr */
  946. intel_logical_ring_emit(ringbuf, 0); /* value */
  947. intel_logical_ring_advance(ringbuf);
  948. return 0;
  949. }
  950. static int gen8_emit_flush_render(struct intel_ringbuffer *ringbuf,
  951. u32 invalidate_domains,
  952. u32 flush_domains)
  953. {
  954. struct intel_engine_cs *ring = ringbuf->ring;
  955. u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  956. u32 flags = 0;
  957. int ret;
  958. flags |= PIPE_CONTROL_CS_STALL;
  959. if (flush_domains) {
  960. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  961. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  962. }
  963. if (invalidate_domains) {
  964. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  965. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  966. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  967. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  968. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  969. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  970. flags |= PIPE_CONTROL_QW_WRITE;
  971. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  972. }
  973. ret = intel_logical_ring_begin(ringbuf, 6);
  974. if (ret)
  975. return ret;
  976. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  977. intel_logical_ring_emit(ringbuf, flags);
  978. intel_logical_ring_emit(ringbuf, scratch_addr);
  979. intel_logical_ring_emit(ringbuf, 0);
  980. intel_logical_ring_emit(ringbuf, 0);
  981. intel_logical_ring_emit(ringbuf, 0);
  982. intel_logical_ring_advance(ringbuf);
  983. return 0;
  984. }
  985. static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
  986. {
  987. return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
  988. }
  989. static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
  990. {
  991. intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
  992. }
  993. static int gen8_emit_request(struct intel_ringbuffer *ringbuf)
  994. {
  995. struct intel_engine_cs *ring = ringbuf->ring;
  996. u32 cmd;
  997. int ret;
  998. ret = intel_logical_ring_begin(ringbuf, 6);
  999. if (ret)
  1000. return ret;
  1001. cmd = MI_STORE_DWORD_IMM_GEN8;
  1002. cmd |= MI_GLOBAL_GTT;
  1003. intel_logical_ring_emit(ringbuf, cmd);
  1004. intel_logical_ring_emit(ringbuf,
  1005. (ring->status_page.gfx_addr +
  1006. (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
  1007. intel_logical_ring_emit(ringbuf, 0);
  1008. intel_logical_ring_emit(ringbuf, ring->outstanding_lazy_seqno);
  1009. intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
  1010. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1011. intel_logical_ring_advance_and_submit(ringbuf);
  1012. return 0;
  1013. }
  1014. /**
  1015. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1016. *
  1017. * @ring: Engine Command Streamer.
  1018. *
  1019. */
  1020. void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
  1021. {
  1022. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1023. if (!intel_ring_initialized(ring))
  1024. return;
  1025. intel_logical_ring_stop(ring);
  1026. WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
  1027. ring->preallocated_lazy_request = NULL;
  1028. ring->outstanding_lazy_seqno = 0;
  1029. if (ring->cleanup)
  1030. ring->cleanup(ring);
  1031. i915_cmd_parser_fini_ring(ring);
  1032. if (ring->status_page.obj) {
  1033. kunmap(sg_page(ring->status_page.obj->pages->sgl));
  1034. ring->status_page.obj = NULL;
  1035. }
  1036. }
  1037. static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
  1038. {
  1039. int ret;
  1040. /* Intentionally left blank. */
  1041. ring->buffer = NULL;
  1042. ring->dev = dev;
  1043. INIT_LIST_HEAD(&ring->active_list);
  1044. INIT_LIST_HEAD(&ring->request_list);
  1045. init_waitqueue_head(&ring->irq_queue);
  1046. INIT_LIST_HEAD(&ring->execlist_queue);
  1047. spin_lock_init(&ring->execlist_lock);
  1048. ring->next_context_status_buffer = 0;
  1049. ret = i915_cmd_parser_init_ring(ring);
  1050. if (ret)
  1051. return ret;
  1052. if (ring->init) {
  1053. ret = ring->init(ring);
  1054. if (ret)
  1055. return ret;
  1056. }
  1057. ret = intel_lr_context_deferred_create(ring->default_context, ring);
  1058. return ret;
  1059. }
  1060. static int logical_render_ring_init(struct drm_device *dev)
  1061. {
  1062. struct drm_i915_private *dev_priv = dev->dev_private;
  1063. struct intel_engine_cs *ring = &dev_priv->ring[RCS];
  1064. ring->name = "render ring";
  1065. ring->id = RCS;
  1066. ring->mmio_base = RENDER_RING_BASE;
  1067. ring->irq_enable_mask =
  1068. GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
  1069. ring->irq_keep_mask =
  1070. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
  1071. if (HAS_L3_DPF(dev))
  1072. ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1073. ring->init = gen8_init_render_ring;
  1074. ring->cleanup = intel_fini_pipe_control;
  1075. ring->get_seqno = gen8_get_seqno;
  1076. ring->set_seqno = gen8_set_seqno;
  1077. ring->emit_request = gen8_emit_request;
  1078. ring->emit_flush = gen8_emit_flush_render;
  1079. ring->irq_get = gen8_logical_ring_get_irq;
  1080. ring->irq_put = gen8_logical_ring_put_irq;
  1081. ring->emit_bb_start = gen8_emit_bb_start;
  1082. return logical_ring_init(dev, ring);
  1083. }
  1084. static int logical_bsd_ring_init(struct drm_device *dev)
  1085. {
  1086. struct drm_i915_private *dev_priv = dev->dev_private;
  1087. struct intel_engine_cs *ring = &dev_priv->ring[VCS];
  1088. ring->name = "bsd ring";
  1089. ring->id = VCS;
  1090. ring->mmio_base = GEN6_BSD_RING_BASE;
  1091. ring->irq_enable_mask =
  1092. GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1093. ring->irq_keep_mask =
  1094. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1095. ring->init = gen8_init_common_ring;
  1096. ring->get_seqno = gen8_get_seqno;
  1097. ring->set_seqno = gen8_set_seqno;
  1098. ring->emit_request = gen8_emit_request;
  1099. ring->emit_flush = gen8_emit_flush;
  1100. ring->irq_get = gen8_logical_ring_get_irq;
  1101. ring->irq_put = gen8_logical_ring_put_irq;
  1102. ring->emit_bb_start = gen8_emit_bb_start;
  1103. return logical_ring_init(dev, ring);
  1104. }
  1105. static int logical_bsd2_ring_init(struct drm_device *dev)
  1106. {
  1107. struct drm_i915_private *dev_priv = dev->dev_private;
  1108. struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
  1109. ring->name = "bds2 ring";
  1110. ring->id = VCS2;
  1111. ring->mmio_base = GEN8_BSD2_RING_BASE;
  1112. ring->irq_enable_mask =
  1113. GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  1114. ring->irq_keep_mask =
  1115. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  1116. ring->init = gen8_init_common_ring;
  1117. ring->get_seqno = gen8_get_seqno;
  1118. ring->set_seqno = gen8_set_seqno;
  1119. ring->emit_request = gen8_emit_request;
  1120. ring->emit_flush = gen8_emit_flush;
  1121. ring->irq_get = gen8_logical_ring_get_irq;
  1122. ring->irq_put = gen8_logical_ring_put_irq;
  1123. ring->emit_bb_start = gen8_emit_bb_start;
  1124. return logical_ring_init(dev, ring);
  1125. }
  1126. static int logical_blt_ring_init(struct drm_device *dev)
  1127. {
  1128. struct drm_i915_private *dev_priv = dev->dev_private;
  1129. struct intel_engine_cs *ring = &dev_priv->ring[BCS];
  1130. ring->name = "blitter ring";
  1131. ring->id = BCS;
  1132. ring->mmio_base = BLT_RING_BASE;
  1133. ring->irq_enable_mask =
  1134. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1135. ring->irq_keep_mask =
  1136. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1137. ring->init = gen8_init_common_ring;
  1138. ring->get_seqno = gen8_get_seqno;
  1139. ring->set_seqno = gen8_set_seqno;
  1140. ring->emit_request = gen8_emit_request;
  1141. ring->emit_flush = gen8_emit_flush;
  1142. ring->irq_get = gen8_logical_ring_get_irq;
  1143. ring->irq_put = gen8_logical_ring_put_irq;
  1144. ring->emit_bb_start = gen8_emit_bb_start;
  1145. return logical_ring_init(dev, ring);
  1146. }
  1147. static int logical_vebox_ring_init(struct drm_device *dev)
  1148. {
  1149. struct drm_i915_private *dev_priv = dev->dev_private;
  1150. struct intel_engine_cs *ring = &dev_priv->ring[VECS];
  1151. ring->name = "video enhancement ring";
  1152. ring->id = VECS;
  1153. ring->mmio_base = VEBOX_RING_BASE;
  1154. ring->irq_enable_mask =
  1155. GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1156. ring->irq_keep_mask =
  1157. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1158. ring->init = gen8_init_common_ring;
  1159. ring->get_seqno = gen8_get_seqno;
  1160. ring->set_seqno = gen8_set_seqno;
  1161. ring->emit_request = gen8_emit_request;
  1162. ring->emit_flush = gen8_emit_flush;
  1163. ring->irq_get = gen8_logical_ring_get_irq;
  1164. ring->irq_put = gen8_logical_ring_put_irq;
  1165. ring->emit_bb_start = gen8_emit_bb_start;
  1166. return logical_ring_init(dev, ring);
  1167. }
  1168. /**
  1169. * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
  1170. * @dev: DRM device.
  1171. *
  1172. * This function inits the engines for an Execlists submission style (the equivalent in the
  1173. * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
  1174. * those engines that are present in the hardware.
  1175. *
  1176. * Return: non-zero if the initialization failed.
  1177. */
  1178. int intel_logical_rings_init(struct drm_device *dev)
  1179. {
  1180. struct drm_i915_private *dev_priv = dev->dev_private;
  1181. int ret;
  1182. ret = logical_render_ring_init(dev);
  1183. if (ret)
  1184. return ret;
  1185. if (HAS_BSD(dev)) {
  1186. ret = logical_bsd_ring_init(dev);
  1187. if (ret)
  1188. goto cleanup_render_ring;
  1189. }
  1190. if (HAS_BLT(dev)) {
  1191. ret = logical_blt_ring_init(dev);
  1192. if (ret)
  1193. goto cleanup_bsd_ring;
  1194. }
  1195. if (HAS_VEBOX(dev)) {
  1196. ret = logical_vebox_ring_init(dev);
  1197. if (ret)
  1198. goto cleanup_blt_ring;
  1199. }
  1200. if (HAS_BSD2(dev)) {
  1201. ret = logical_bsd2_ring_init(dev);
  1202. if (ret)
  1203. goto cleanup_vebox_ring;
  1204. }
  1205. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  1206. if (ret)
  1207. goto cleanup_bsd2_ring;
  1208. return 0;
  1209. cleanup_bsd2_ring:
  1210. intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
  1211. cleanup_vebox_ring:
  1212. intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
  1213. cleanup_blt_ring:
  1214. intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
  1215. cleanup_bsd_ring:
  1216. intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
  1217. cleanup_render_ring:
  1218. intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
  1219. return ret;
  1220. }
  1221. int intel_lr_context_render_state_init(struct intel_engine_cs *ring,
  1222. struct intel_context *ctx)
  1223. {
  1224. struct intel_ringbuffer *ringbuf = ctx->engine[ring->id].ringbuf;
  1225. struct render_state so;
  1226. struct drm_i915_file_private *file_priv = ctx->file_priv;
  1227. struct drm_file *file = file_priv ? file_priv->file : NULL;
  1228. int ret;
  1229. ret = i915_gem_render_state_prepare(ring, &so);
  1230. if (ret)
  1231. return ret;
  1232. if (so.rodata == NULL)
  1233. return 0;
  1234. ret = ring->emit_bb_start(ringbuf,
  1235. so.ggtt_offset,
  1236. I915_DISPATCH_SECURE);
  1237. if (ret)
  1238. goto out;
  1239. i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), ring);
  1240. ret = __i915_add_request(ring, file, so.obj, NULL);
  1241. /* intel_logical_ring_add_request moves object to inactive if it
  1242. * fails */
  1243. out:
  1244. i915_gem_render_state_fini(&so);
  1245. return ret;
  1246. }
  1247. static int
  1248. populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
  1249. struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
  1250. {
  1251. struct drm_device *dev = ring->dev;
  1252. struct drm_i915_private *dev_priv = dev->dev_private;
  1253. struct drm_i915_gem_object *ring_obj = ringbuf->obj;
  1254. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
  1255. struct page *page;
  1256. uint32_t *reg_state;
  1257. int ret;
  1258. if (!ppgtt)
  1259. ppgtt = dev_priv->mm.aliasing_ppgtt;
  1260. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1261. if (ret) {
  1262. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1263. return ret;
  1264. }
  1265. ret = i915_gem_object_get_pages(ctx_obj);
  1266. if (ret) {
  1267. DRM_DEBUG_DRIVER("Could not get object pages\n");
  1268. return ret;
  1269. }
  1270. i915_gem_object_pin_pages(ctx_obj);
  1271. /* The second page of the context object contains some fields which must
  1272. * be set up prior to the first execution. */
  1273. page = i915_gem_object_get_page(ctx_obj, 1);
  1274. reg_state = kmap_atomic(page);
  1275. /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
  1276. * commands followed by (reg, value) pairs. The values we are setting here are
  1277. * only for the first context restore: on a subsequent save, the GPU will
  1278. * recreate this batchbuffer with new values (including all the missing
  1279. * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
  1280. if (ring->id == RCS)
  1281. reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
  1282. else
  1283. reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
  1284. reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
  1285. reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
  1286. reg_state[CTX_CONTEXT_CONTROL+1] =
  1287. _MASKED_BIT_ENABLE((1<<3) | MI_RESTORE_INHIBIT);
  1288. reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
  1289. reg_state[CTX_RING_HEAD+1] = 0;
  1290. reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
  1291. reg_state[CTX_RING_TAIL+1] = 0;
  1292. reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
  1293. reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(ring_obj);
  1294. reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
  1295. reg_state[CTX_RING_BUFFER_CONTROL+1] =
  1296. ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
  1297. reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
  1298. reg_state[CTX_BB_HEAD_U+1] = 0;
  1299. reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
  1300. reg_state[CTX_BB_HEAD_L+1] = 0;
  1301. reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
  1302. reg_state[CTX_BB_STATE+1] = (1<<5);
  1303. reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
  1304. reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
  1305. reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
  1306. reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
  1307. reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
  1308. reg_state[CTX_SECOND_BB_STATE+1] = 0;
  1309. if (ring->id == RCS) {
  1310. /* TODO: according to BSpec, the register state context
  1311. * for CHV does not have these. OTOH, these registers do
  1312. * exist in CHV. I'm waiting for a clarification */
  1313. reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
  1314. reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
  1315. reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
  1316. reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
  1317. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
  1318. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
  1319. }
  1320. reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
  1321. reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
  1322. reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
  1323. reg_state[CTX_CTX_TIMESTAMP+1] = 0;
  1324. reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
  1325. reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
  1326. reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
  1327. reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
  1328. reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
  1329. reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
  1330. reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
  1331. reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
  1332. reg_state[CTX_PDP3_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[3]);
  1333. reg_state[CTX_PDP3_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[3]);
  1334. reg_state[CTX_PDP2_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[2]);
  1335. reg_state[CTX_PDP2_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[2]);
  1336. reg_state[CTX_PDP1_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[1]);
  1337. reg_state[CTX_PDP1_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[1]);
  1338. reg_state[CTX_PDP0_UDW+1] = upper_32_bits(ppgtt->pd_dma_addr[0]);
  1339. reg_state[CTX_PDP0_LDW+1] = lower_32_bits(ppgtt->pd_dma_addr[0]);
  1340. if (ring->id == RCS) {
  1341. reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  1342. reg_state[CTX_R_PWR_CLK_STATE] = 0x20c8;
  1343. reg_state[CTX_R_PWR_CLK_STATE+1] = 0;
  1344. }
  1345. kunmap_atomic(reg_state);
  1346. ctx_obj->dirty = 1;
  1347. set_page_dirty(page);
  1348. i915_gem_object_unpin_pages(ctx_obj);
  1349. return 0;
  1350. }
  1351. /**
  1352. * intel_lr_context_free() - free the LRC specific bits of a context
  1353. * @ctx: the LR context to free.
  1354. *
  1355. * The real context freeing is done in i915_gem_context_free: this only
  1356. * takes care of the bits that are LRC related: the per-engine backing
  1357. * objects and the logical ringbuffer.
  1358. */
  1359. void intel_lr_context_free(struct intel_context *ctx)
  1360. {
  1361. int i;
  1362. for (i = 0; i < I915_NUM_RINGS; i++) {
  1363. struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
  1364. struct intel_ringbuffer *ringbuf = ctx->engine[i].ringbuf;
  1365. if (ctx_obj) {
  1366. intel_destroy_ringbuffer_obj(ringbuf);
  1367. kfree(ringbuf);
  1368. i915_gem_object_ggtt_unpin(ctx_obj);
  1369. drm_gem_object_unreference(&ctx_obj->base);
  1370. }
  1371. }
  1372. }
  1373. static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
  1374. {
  1375. int ret = 0;
  1376. WARN_ON(INTEL_INFO(ring->dev)->gen != 8);
  1377. switch (ring->id) {
  1378. case RCS:
  1379. ret = GEN8_LR_CONTEXT_RENDER_SIZE;
  1380. break;
  1381. case VCS:
  1382. case BCS:
  1383. case VECS:
  1384. case VCS2:
  1385. ret = GEN8_LR_CONTEXT_OTHER_SIZE;
  1386. break;
  1387. }
  1388. return ret;
  1389. }
  1390. /**
  1391. * intel_lr_context_deferred_create() - create the LRC specific bits of a context
  1392. * @ctx: LR context to create.
  1393. * @ring: engine to be used with the context.
  1394. *
  1395. * This function can be called more than once, with different engines, if we plan
  1396. * to use the context with them. The context backing objects and the ringbuffers
  1397. * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
  1398. * the creation is a deferred call: it's better to make sure first that we need to use
  1399. * a given ring with the context.
  1400. *
  1401. * Return: non-zero on eror.
  1402. */
  1403. int intel_lr_context_deferred_create(struct intel_context *ctx,
  1404. struct intel_engine_cs *ring)
  1405. {
  1406. struct drm_device *dev = ring->dev;
  1407. struct drm_i915_gem_object *ctx_obj;
  1408. uint32_t context_size;
  1409. struct intel_ringbuffer *ringbuf;
  1410. int ret;
  1411. WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
  1412. if (ctx->engine[ring->id].state)
  1413. return 0;
  1414. context_size = round_up(get_lr_context_size(ring), 4096);
  1415. ctx_obj = i915_gem_alloc_context_obj(dev, context_size);
  1416. if (IS_ERR(ctx_obj)) {
  1417. ret = PTR_ERR(ctx_obj);
  1418. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed: %d\n", ret);
  1419. return ret;
  1420. }
  1421. ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
  1422. if (ret) {
  1423. DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n", ret);
  1424. drm_gem_object_unreference(&ctx_obj->base);
  1425. return ret;
  1426. }
  1427. ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
  1428. if (!ringbuf) {
  1429. DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
  1430. ring->name);
  1431. i915_gem_object_ggtt_unpin(ctx_obj);
  1432. drm_gem_object_unreference(&ctx_obj->base);
  1433. ret = -ENOMEM;
  1434. return ret;
  1435. }
  1436. ringbuf->ring = ring;
  1437. ringbuf->FIXME_lrc_ctx = ctx;
  1438. ringbuf->size = 32 * PAGE_SIZE;
  1439. ringbuf->effective_size = ringbuf->size;
  1440. ringbuf->head = 0;
  1441. ringbuf->tail = 0;
  1442. ringbuf->space = ringbuf->size;
  1443. ringbuf->last_retired_head = -1;
  1444. /* TODO: For now we put this in the mappable region so that we can reuse
  1445. * the existing ringbuffer code which ioremaps it. When we start
  1446. * creating many contexts, this will no longer work and we must switch
  1447. * to a kmapish interface.
  1448. */
  1449. ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
  1450. if (ret) {
  1451. DRM_DEBUG_DRIVER("Failed to allocate ringbuffer obj %s: %d\n",
  1452. ring->name, ret);
  1453. goto error;
  1454. }
  1455. ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
  1456. if (ret) {
  1457. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  1458. intel_destroy_ringbuffer_obj(ringbuf);
  1459. goto error;
  1460. }
  1461. ctx->engine[ring->id].ringbuf = ringbuf;
  1462. ctx->engine[ring->id].state = ctx_obj;
  1463. if (ctx == ring->default_context) {
  1464. /* The status page is offset 0 from the default context object
  1465. * in LRC mode. */
  1466. ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(ctx_obj);
  1467. ring->status_page.page_addr =
  1468. kmap(sg_page(ctx_obj->pages->sgl));
  1469. if (ring->status_page.page_addr == NULL)
  1470. return -ENOMEM;
  1471. ring->status_page.obj = ctx_obj;
  1472. }
  1473. if (ring->id == RCS && !ctx->rcs_initialized) {
  1474. ret = intel_lr_context_render_state_init(ring, ctx);
  1475. if (ret) {
  1476. DRM_ERROR("Init render state failed: %d\n", ret);
  1477. ctx->engine[ring->id].ringbuf = NULL;
  1478. ctx->engine[ring->id].state = NULL;
  1479. intel_destroy_ringbuffer_obj(ringbuf);
  1480. goto error;
  1481. }
  1482. ctx->rcs_initialized = true;
  1483. }
  1484. return 0;
  1485. error:
  1486. kfree(ringbuf);
  1487. i915_gem_object_ggtt_unpin(ctx_obj);
  1488. drm_gem_object_unreference(&ctx_obj->base);
  1489. return ret;
  1490. }