intel_lrc.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  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. #include "intel_mocs.h"
  137. #define GEN9_LR_CONTEXT_RENDER_SIZE (22 * PAGE_SIZE)
  138. #define GEN8_LR_CONTEXT_RENDER_SIZE (20 * PAGE_SIZE)
  139. #define GEN8_LR_CONTEXT_OTHER_SIZE (2 * PAGE_SIZE)
  140. #define RING_EXECLIST_QFULL (1 << 0x2)
  141. #define RING_EXECLIST1_VALID (1 << 0x3)
  142. #define RING_EXECLIST0_VALID (1 << 0x4)
  143. #define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
  144. #define RING_EXECLIST1_ACTIVE (1 << 0x11)
  145. #define RING_EXECLIST0_ACTIVE (1 << 0x12)
  146. #define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
  147. #define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
  148. #define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
  149. #define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
  150. #define GEN8_CTX_STATUS_COMPLETE (1 << 4)
  151. #define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
  152. #define CTX_LRI_HEADER_0 0x01
  153. #define CTX_CONTEXT_CONTROL 0x02
  154. #define CTX_RING_HEAD 0x04
  155. #define CTX_RING_TAIL 0x06
  156. #define CTX_RING_BUFFER_START 0x08
  157. #define CTX_RING_BUFFER_CONTROL 0x0a
  158. #define CTX_BB_HEAD_U 0x0c
  159. #define CTX_BB_HEAD_L 0x0e
  160. #define CTX_BB_STATE 0x10
  161. #define CTX_SECOND_BB_HEAD_U 0x12
  162. #define CTX_SECOND_BB_HEAD_L 0x14
  163. #define CTX_SECOND_BB_STATE 0x16
  164. #define CTX_BB_PER_CTX_PTR 0x18
  165. #define CTX_RCS_INDIRECT_CTX 0x1a
  166. #define CTX_RCS_INDIRECT_CTX_OFFSET 0x1c
  167. #define CTX_LRI_HEADER_1 0x21
  168. #define CTX_CTX_TIMESTAMP 0x22
  169. #define CTX_PDP3_UDW 0x24
  170. #define CTX_PDP3_LDW 0x26
  171. #define CTX_PDP2_UDW 0x28
  172. #define CTX_PDP2_LDW 0x2a
  173. #define CTX_PDP1_UDW 0x2c
  174. #define CTX_PDP1_LDW 0x2e
  175. #define CTX_PDP0_UDW 0x30
  176. #define CTX_PDP0_LDW 0x32
  177. #define CTX_LRI_HEADER_2 0x41
  178. #define CTX_R_PWR_CLK_STATE 0x42
  179. #define CTX_GPGPU_CSR_BASE_ADDRESS 0x44
  180. #define GEN8_CTX_VALID (1<<0)
  181. #define GEN8_CTX_FORCE_PD_RESTORE (1<<1)
  182. #define GEN8_CTX_FORCE_RESTORE (1<<2)
  183. #define GEN8_CTX_L3LLC_COHERENT (1<<5)
  184. #define GEN8_CTX_PRIVILEGE (1<<8)
  185. #define ASSIGN_CTX_PDP(ppgtt, reg_state, n) { \
  186. const u64 _addr = i915_page_dir_dma_addr((ppgtt), (n)); \
  187. reg_state[CTX_PDP ## n ## _UDW+1] = upper_32_bits(_addr); \
  188. reg_state[CTX_PDP ## n ## _LDW+1] = lower_32_bits(_addr); \
  189. }
  190. enum {
  191. ADVANCED_CONTEXT = 0,
  192. LEGACY_CONTEXT,
  193. ADVANCED_AD_CONTEXT,
  194. LEGACY_64B_CONTEXT
  195. };
  196. #define GEN8_CTX_MODE_SHIFT 3
  197. enum {
  198. FAULT_AND_HANG = 0,
  199. FAULT_AND_HALT, /* Debug only */
  200. FAULT_AND_STREAM,
  201. FAULT_AND_CONTINUE /* Unsupported */
  202. };
  203. #define GEN8_CTX_ID_SHIFT 32
  204. #define CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT 0x17
  205. static int intel_lr_context_pin(struct drm_i915_gem_request *rq);
  206. /**
  207. * intel_sanitize_enable_execlists() - sanitize i915.enable_execlists
  208. * @dev: DRM device.
  209. * @enable_execlists: value of i915.enable_execlists module parameter.
  210. *
  211. * Only certain platforms support Execlists (the prerequisites being
  212. * support for Logical Ring Contexts and Aliasing PPGTT or better).
  213. *
  214. * Return: 1 if Execlists is supported and has to be enabled.
  215. */
  216. int intel_sanitize_enable_execlists(struct drm_device *dev, int enable_execlists)
  217. {
  218. WARN_ON(i915.enable_ppgtt == -1);
  219. if (INTEL_INFO(dev)->gen >= 9)
  220. return 1;
  221. if (enable_execlists == 0)
  222. return 0;
  223. if (HAS_LOGICAL_RING_CONTEXTS(dev) && USES_PPGTT(dev) &&
  224. i915.use_mmio_flip >= 0)
  225. return 1;
  226. return 0;
  227. }
  228. /**
  229. * intel_execlists_ctx_id() - get the Execlists Context ID
  230. * @ctx_obj: Logical Ring Context backing object.
  231. *
  232. * Do not confuse with ctx->id! Unfortunately we have a name overload
  233. * here: the old context ID we pass to userspace as a handler so that
  234. * they can refer to a context, and the new context ID we pass to the
  235. * ELSP so that the GPU can inform us of the context status via
  236. * interrupts.
  237. *
  238. * Return: 20-bits globally unique context ID.
  239. */
  240. u32 intel_execlists_ctx_id(struct drm_i915_gem_object *ctx_obj)
  241. {
  242. u32 lrca = i915_gem_obj_ggtt_offset(ctx_obj);
  243. /* LRCA is required to be 4K aligned so the more significant 20 bits
  244. * are globally unique */
  245. return lrca >> 12;
  246. }
  247. static uint64_t execlists_ctx_descriptor(struct drm_i915_gem_request *rq)
  248. {
  249. struct intel_engine_cs *ring = rq->ring;
  250. struct drm_device *dev = ring->dev;
  251. struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
  252. uint64_t desc;
  253. uint64_t lrca = i915_gem_obj_ggtt_offset(ctx_obj);
  254. WARN_ON(lrca & 0xFFFFFFFF00000FFFULL);
  255. desc = GEN8_CTX_VALID;
  256. desc |= LEGACY_CONTEXT << GEN8_CTX_MODE_SHIFT;
  257. if (IS_GEN8(ctx_obj->base.dev))
  258. desc |= GEN8_CTX_L3LLC_COHERENT;
  259. desc |= GEN8_CTX_PRIVILEGE;
  260. desc |= lrca;
  261. desc |= (u64)intel_execlists_ctx_id(ctx_obj) << GEN8_CTX_ID_SHIFT;
  262. /* TODO: WaDisableLiteRestore when we start using semaphore
  263. * signalling between Command Streamers */
  264. /* desc |= GEN8_CTX_FORCE_RESTORE; */
  265. /* WaEnableForceRestoreInCtxtDescForVCS:skl */
  266. if (IS_GEN9(dev) &&
  267. INTEL_REVID(dev) <= SKL_REVID_B0 &&
  268. (ring->id == BCS || ring->id == VCS ||
  269. ring->id == VECS || ring->id == VCS2))
  270. desc |= GEN8_CTX_FORCE_RESTORE;
  271. return desc;
  272. }
  273. static void execlists_elsp_write(struct drm_i915_gem_request *rq0,
  274. struct drm_i915_gem_request *rq1)
  275. {
  276. struct intel_engine_cs *ring = rq0->ring;
  277. struct drm_device *dev = ring->dev;
  278. struct drm_i915_private *dev_priv = dev->dev_private;
  279. uint64_t desc[2];
  280. if (rq1) {
  281. desc[1] = execlists_ctx_descriptor(rq1);
  282. rq1->elsp_submitted++;
  283. } else {
  284. desc[1] = 0;
  285. }
  286. desc[0] = execlists_ctx_descriptor(rq0);
  287. rq0->elsp_submitted++;
  288. /* You must always write both descriptors in the order below. */
  289. spin_lock(&dev_priv->uncore.lock);
  290. intel_uncore_forcewake_get__locked(dev_priv, FORCEWAKE_ALL);
  291. I915_WRITE_FW(RING_ELSP(ring), upper_32_bits(desc[1]));
  292. I915_WRITE_FW(RING_ELSP(ring), lower_32_bits(desc[1]));
  293. I915_WRITE_FW(RING_ELSP(ring), upper_32_bits(desc[0]));
  294. /* The context is automatically loaded after the following */
  295. I915_WRITE_FW(RING_ELSP(ring), lower_32_bits(desc[0]));
  296. /* ELSP is a wo register, use another nearby reg for posting */
  297. POSTING_READ_FW(RING_EXECLIST_STATUS(ring));
  298. intel_uncore_forcewake_put__locked(dev_priv, FORCEWAKE_ALL);
  299. spin_unlock(&dev_priv->uncore.lock);
  300. }
  301. static int execlists_update_context(struct drm_i915_gem_request *rq)
  302. {
  303. struct intel_engine_cs *ring = rq->ring;
  304. struct i915_hw_ppgtt *ppgtt = rq->ctx->ppgtt;
  305. struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
  306. struct drm_i915_gem_object *rb_obj = rq->ringbuf->obj;
  307. struct page *page;
  308. uint32_t *reg_state;
  309. BUG_ON(!ctx_obj);
  310. WARN_ON(!i915_gem_obj_is_pinned(ctx_obj));
  311. WARN_ON(!i915_gem_obj_is_pinned(rb_obj));
  312. page = i915_gem_object_get_page(ctx_obj, 1);
  313. reg_state = kmap_atomic(page);
  314. reg_state[CTX_RING_TAIL+1] = rq->tail;
  315. reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(rb_obj);
  316. /* True PPGTT with dynamic page allocation: update PDP registers and
  317. * point the unallocated PDPs to the scratch page
  318. */
  319. if (ppgtt) {
  320. ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
  321. ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
  322. ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
  323. ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
  324. }
  325. kunmap_atomic(reg_state);
  326. return 0;
  327. }
  328. static void execlists_submit_requests(struct drm_i915_gem_request *rq0,
  329. struct drm_i915_gem_request *rq1)
  330. {
  331. execlists_update_context(rq0);
  332. if (rq1)
  333. execlists_update_context(rq1);
  334. execlists_elsp_write(rq0, rq1);
  335. }
  336. static void execlists_context_unqueue(struct intel_engine_cs *ring)
  337. {
  338. struct drm_i915_gem_request *req0 = NULL, *req1 = NULL;
  339. struct drm_i915_gem_request *cursor = NULL, *tmp = NULL;
  340. assert_spin_locked(&ring->execlist_lock);
  341. /*
  342. * If irqs are not active generate a warning as batches that finish
  343. * without the irqs may get lost and a GPU Hang may occur.
  344. */
  345. WARN_ON(!intel_irqs_enabled(ring->dev->dev_private));
  346. if (list_empty(&ring->execlist_queue))
  347. return;
  348. /* Try to read in pairs */
  349. list_for_each_entry_safe(cursor, tmp, &ring->execlist_queue,
  350. execlist_link) {
  351. if (!req0) {
  352. req0 = cursor;
  353. } else if (req0->ctx == cursor->ctx) {
  354. /* Same ctx: ignore first request, as second request
  355. * will update tail past first request's workload */
  356. cursor->elsp_submitted = req0->elsp_submitted;
  357. list_del(&req0->execlist_link);
  358. list_add_tail(&req0->execlist_link,
  359. &ring->execlist_retired_req_list);
  360. req0 = cursor;
  361. } else {
  362. req1 = cursor;
  363. break;
  364. }
  365. }
  366. if (IS_GEN8(ring->dev) || IS_GEN9(ring->dev)) {
  367. /*
  368. * WaIdleLiteRestore: make sure we never cause a lite
  369. * restore with HEAD==TAIL
  370. */
  371. if (req0->elsp_submitted) {
  372. /*
  373. * Apply the wa NOOPS to prevent ring:HEAD == req:TAIL
  374. * as we resubmit the request. See gen8_emit_request()
  375. * for where we prepare the padding after the end of the
  376. * request.
  377. */
  378. struct intel_ringbuffer *ringbuf;
  379. ringbuf = req0->ctx->engine[ring->id].ringbuf;
  380. req0->tail += 8;
  381. req0->tail &= ringbuf->size - 1;
  382. }
  383. }
  384. WARN_ON(req1 && req1->elsp_submitted);
  385. execlists_submit_requests(req0, req1);
  386. }
  387. static bool execlists_check_remove_request(struct intel_engine_cs *ring,
  388. u32 request_id)
  389. {
  390. struct drm_i915_gem_request *head_req;
  391. assert_spin_locked(&ring->execlist_lock);
  392. head_req = list_first_entry_or_null(&ring->execlist_queue,
  393. struct drm_i915_gem_request,
  394. execlist_link);
  395. if (head_req != NULL) {
  396. struct drm_i915_gem_object *ctx_obj =
  397. head_req->ctx->engine[ring->id].state;
  398. if (intel_execlists_ctx_id(ctx_obj) == request_id) {
  399. WARN(head_req->elsp_submitted == 0,
  400. "Never submitted head request\n");
  401. if (--head_req->elsp_submitted <= 0) {
  402. list_del(&head_req->execlist_link);
  403. list_add_tail(&head_req->execlist_link,
  404. &ring->execlist_retired_req_list);
  405. return true;
  406. }
  407. }
  408. }
  409. return false;
  410. }
  411. /**
  412. * intel_lrc_irq_handler() - handle Context Switch interrupts
  413. * @ring: Engine Command Streamer to handle.
  414. *
  415. * Check the unread Context Status Buffers and manage the submission of new
  416. * contexts to the ELSP accordingly.
  417. */
  418. void intel_lrc_irq_handler(struct intel_engine_cs *ring)
  419. {
  420. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  421. u32 status_pointer;
  422. u8 read_pointer;
  423. u8 write_pointer;
  424. u32 status;
  425. u32 status_id;
  426. u32 submit_contexts = 0;
  427. status_pointer = I915_READ(RING_CONTEXT_STATUS_PTR(ring));
  428. read_pointer = ring->next_context_status_buffer;
  429. write_pointer = status_pointer & GEN8_CSB_PTR_MASK;
  430. if (read_pointer > write_pointer)
  431. write_pointer += GEN8_CSB_ENTRIES;
  432. spin_lock(&ring->execlist_lock);
  433. while (read_pointer < write_pointer) {
  434. read_pointer++;
  435. status = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
  436. (read_pointer % GEN8_CSB_ENTRIES) * 8);
  437. status_id = I915_READ(RING_CONTEXT_STATUS_BUF(ring) +
  438. (read_pointer % GEN8_CSB_ENTRIES) * 8 + 4);
  439. if (status & GEN8_CTX_STATUS_IDLE_ACTIVE)
  440. continue;
  441. if (status & GEN8_CTX_STATUS_PREEMPTED) {
  442. if (status & GEN8_CTX_STATUS_LITE_RESTORE) {
  443. if (execlists_check_remove_request(ring, status_id))
  444. WARN(1, "Lite Restored request removed from queue\n");
  445. } else
  446. WARN(1, "Preemption without Lite Restore\n");
  447. }
  448. if ((status & GEN8_CTX_STATUS_ACTIVE_IDLE) ||
  449. (status & GEN8_CTX_STATUS_ELEMENT_SWITCH)) {
  450. if (execlists_check_remove_request(ring, status_id))
  451. submit_contexts++;
  452. }
  453. }
  454. if (submit_contexts != 0)
  455. execlists_context_unqueue(ring);
  456. spin_unlock(&ring->execlist_lock);
  457. WARN(submit_contexts > 2, "More than two context complete events?\n");
  458. ring->next_context_status_buffer = write_pointer % GEN8_CSB_ENTRIES;
  459. I915_WRITE(RING_CONTEXT_STATUS_PTR(ring),
  460. _MASKED_FIELD(GEN8_CSB_PTR_MASK << 8,
  461. ((u32)ring->next_context_status_buffer &
  462. GEN8_CSB_PTR_MASK) << 8));
  463. }
  464. static int execlists_context_queue(struct drm_i915_gem_request *request)
  465. {
  466. struct intel_engine_cs *ring = request->ring;
  467. struct drm_i915_gem_request *cursor;
  468. int num_elements = 0;
  469. if (request->ctx != ring->default_context)
  470. intel_lr_context_pin(request);
  471. i915_gem_request_reference(request);
  472. request->tail = request->ringbuf->tail;
  473. spin_lock_irq(&ring->execlist_lock);
  474. list_for_each_entry(cursor, &ring->execlist_queue, execlist_link)
  475. if (++num_elements > 2)
  476. break;
  477. if (num_elements > 2) {
  478. struct drm_i915_gem_request *tail_req;
  479. tail_req = list_last_entry(&ring->execlist_queue,
  480. struct drm_i915_gem_request,
  481. execlist_link);
  482. if (request->ctx == tail_req->ctx) {
  483. WARN(tail_req->elsp_submitted != 0,
  484. "More than 2 already-submitted reqs queued\n");
  485. list_del(&tail_req->execlist_link);
  486. list_add_tail(&tail_req->execlist_link,
  487. &ring->execlist_retired_req_list);
  488. }
  489. }
  490. list_add_tail(&request->execlist_link, &ring->execlist_queue);
  491. if (num_elements == 0)
  492. execlists_context_unqueue(ring);
  493. spin_unlock_irq(&ring->execlist_lock);
  494. return 0;
  495. }
  496. static int logical_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
  497. {
  498. struct intel_engine_cs *ring = req->ring;
  499. uint32_t flush_domains;
  500. int ret;
  501. flush_domains = 0;
  502. if (ring->gpu_caches_dirty)
  503. flush_domains = I915_GEM_GPU_DOMAINS;
  504. ret = ring->emit_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
  505. if (ret)
  506. return ret;
  507. ring->gpu_caches_dirty = false;
  508. return 0;
  509. }
  510. static int execlists_move_to_gpu(struct drm_i915_gem_request *req,
  511. struct list_head *vmas)
  512. {
  513. const unsigned other_rings = ~intel_ring_flag(req->ring);
  514. struct i915_vma *vma;
  515. uint32_t flush_domains = 0;
  516. bool flush_chipset = false;
  517. int ret;
  518. list_for_each_entry(vma, vmas, exec_list) {
  519. struct drm_i915_gem_object *obj = vma->obj;
  520. if (obj->active & other_rings) {
  521. ret = i915_gem_object_sync(obj, req->ring, &req);
  522. if (ret)
  523. return ret;
  524. }
  525. if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  526. flush_chipset |= i915_gem_clflush_object(obj, false);
  527. flush_domains |= obj->base.write_domain;
  528. }
  529. if (flush_domains & I915_GEM_DOMAIN_GTT)
  530. wmb();
  531. /* Unconditionally invalidate gpu caches and ensure that we do flush
  532. * any residual writes from the previous batch.
  533. */
  534. return logical_ring_invalidate_all_caches(req);
  535. }
  536. int intel_logical_ring_alloc_request_extras(struct drm_i915_gem_request *request)
  537. {
  538. int ret;
  539. request->ringbuf = request->ctx->engine[request->ring->id].ringbuf;
  540. if (request->ctx != request->ring->default_context) {
  541. ret = intel_lr_context_pin(request);
  542. if (ret)
  543. return ret;
  544. }
  545. return 0;
  546. }
  547. static int logical_ring_wait_for_space(struct drm_i915_gem_request *req,
  548. int bytes)
  549. {
  550. struct intel_ringbuffer *ringbuf = req->ringbuf;
  551. struct intel_engine_cs *ring = req->ring;
  552. struct drm_i915_gem_request *target;
  553. unsigned space;
  554. int ret;
  555. if (intel_ring_space(ringbuf) >= bytes)
  556. return 0;
  557. /* The whole point of reserving space is to not wait! */
  558. WARN_ON(ringbuf->reserved_in_use);
  559. list_for_each_entry(target, &ring->request_list, list) {
  560. /*
  561. * The request queue is per-engine, so can contain requests
  562. * from multiple ringbuffers. Here, we must ignore any that
  563. * aren't from the ringbuffer we're considering.
  564. */
  565. if (target->ringbuf != ringbuf)
  566. continue;
  567. /* Would completion of this request free enough space? */
  568. space = __intel_ring_space(target->postfix, ringbuf->tail,
  569. ringbuf->size);
  570. if (space >= bytes)
  571. break;
  572. }
  573. if (WARN_ON(&target->list == &ring->request_list))
  574. return -ENOSPC;
  575. ret = i915_wait_request(target);
  576. if (ret)
  577. return ret;
  578. ringbuf->space = space;
  579. return 0;
  580. }
  581. /*
  582. * intel_logical_ring_advance_and_submit() - advance the tail and submit the workload
  583. * @request: Request to advance the logical ringbuffer of.
  584. *
  585. * The tail is updated in our logical ringbuffer struct, not in the actual context. What
  586. * really happens during submission is that the context and current tail will be placed
  587. * on a queue waiting for the ELSP to be ready to accept a new context submission. At that
  588. * point, the tail *inside* the context is updated and the ELSP written to.
  589. */
  590. static void
  591. intel_logical_ring_advance_and_submit(struct drm_i915_gem_request *request)
  592. {
  593. struct intel_engine_cs *ring = request->ring;
  594. intel_logical_ring_advance(request->ringbuf);
  595. if (intel_ring_stopped(ring))
  596. return;
  597. execlists_context_queue(request);
  598. }
  599. static void __wrap_ring_buffer(struct intel_ringbuffer *ringbuf)
  600. {
  601. uint32_t __iomem *virt;
  602. int rem = ringbuf->size - ringbuf->tail;
  603. virt = ringbuf->virtual_start + ringbuf->tail;
  604. rem /= 4;
  605. while (rem--)
  606. iowrite32(MI_NOOP, virt++);
  607. ringbuf->tail = 0;
  608. intel_ring_update_space(ringbuf);
  609. }
  610. static int logical_ring_prepare(struct drm_i915_gem_request *req, int bytes)
  611. {
  612. struct intel_ringbuffer *ringbuf = req->ringbuf;
  613. int remain_usable = ringbuf->effective_size - ringbuf->tail;
  614. int remain_actual = ringbuf->size - ringbuf->tail;
  615. int ret, total_bytes, wait_bytes = 0;
  616. bool need_wrap = false;
  617. if (ringbuf->reserved_in_use)
  618. total_bytes = bytes;
  619. else
  620. total_bytes = bytes + ringbuf->reserved_size;
  621. if (unlikely(bytes > remain_usable)) {
  622. /*
  623. * Not enough space for the basic request. So need to flush
  624. * out the remainder and then wait for base + reserved.
  625. */
  626. wait_bytes = remain_actual + total_bytes;
  627. need_wrap = true;
  628. } else {
  629. if (unlikely(total_bytes > remain_usable)) {
  630. /*
  631. * The base request will fit but the reserved space
  632. * falls off the end. So only need to to wait for the
  633. * reserved size after flushing out the remainder.
  634. */
  635. wait_bytes = remain_actual + ringbuf->reserved_size;
  636. need_wrap = true;
  637. } else if (total_bytes > ringbuf->space) {
  638. /* No wrapping required, just waiting. */
  639. wait_bytes = total_bytes;
  640. }
  641. }
  642. if (wait_bytes) {
  643. ret = logical_ring_wait_for_space(req, wait_bytes);
  644. if (unlikely(ret))
  645. return ret;
  646. if (need_wrap)
  647. __wrap_ring_buffer(ringbuf);
  648. }
  649. return 0;
  650. }
  651. /**
  652. * intel_logical_ring_begin() - prepare the logical ringbuffer to accept some commands
  653. *
  654. * @request: The request to start some new work for
  655. * @ctx: Logical ring context whose ringbuffer is being prepared.
  656. * @num_dwords: number of DWORDs that we plan to write to the ringbuffer.
  657. *
  658. * The ringbuffer might not be ready to accept the commands right away (maybe it needs to
  659. * be wrapped, or wait a bit for the tail to be updated). This function takes care of that
  660. * and also preallocates a request (every workload submission is still mediated through
  661. * requests, same as it did with legacy ringbuffer submission).
  662. *
  663. * Return: non-zero if the ringbuffer is not ready to be written to.
  664. */
  665. int intel_logical_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
  666. {
  667. struct drm_i915_private *dev_priv;
  668. int ret;
  669. WARN_ON(req == NULL);
  670. dev_priv = req->ring->dev->dev_private;
  671. ret = i915_gem_check_wedge(&dev_priv->gpu_error,
  672. dev_priv->mm.interruptible);
  673. if (ret)
  674. return ret;
  675. ret = logical_ring_prepare(req, num_dwords * sizeof(uint32_t));
  676. if (ret)
  677. return ret;
  678. req->ringbuf->space -= num_dwords * sizeof(uint32_t);
  679. return 0;
  680. }
  681. int intel_logical_ring_reserve_space(struct drm_i915_gem_request *request)
  682. {
  683. /*
  684. * The first call merely notes the reserve request and is common for
  685. * all back ends. The subsequent localised _begin() call actually
  686. * ensures that the reservation is available. Without the begin, if
  687. * the request creator immediately submitted the request without
  688. * adding any commands to it then there might not actually be
  689. * sufficient room for the submission commands.
  690. */
  691. intel_ring_reserved_space_reserve(request->ringbuf, MIN_SPACE_FOR_ADD_REQUEST);
  692. return intel_logical_ring_begin(request, 0);
  693. }
  694. /**
  695. * execlists_submission() - submit a batchbuffer for execution, Execlists style
  696. * @dev: DRM device.
  697. * @file: DRM file.
  698. * @ring: Engine Command Streamer to submit to.
  699. * @ctx: Context to employ for this submission.
  700. * @args: execbuffer call arguments.
  701. * @vmas: list of vmas.
  702. * @batch_obj: the batchbuffer to submit.
  703. * @exec_start: batchbuffer start virtual address pointer.
  704. * @dispatch_flags: translated execbuffer call flags.
  705. *
  706. * This is the evil twin version of i915_gem_ringbuffer_submission. It abstracts
  707. * away the submission details of the execbuffer ioctl call.
  708. *
  709. * Return: non-zero if the submission fails.
  710. */
  711. int intel_execlists_submission(struct i915_execbuffer_params *params,
  712. struct drm_i915_gem_execbuffer2 *args,
  713. struct list_head *vmas)
  714. {
  715. struct drm_device *dev = params->dev;
  716. struct intel_engine_cs *ring = params->ring;
  717. struct drm_i915_private *dev_priv = dev->dev_private;
  718. struct intel_ringbuffer *ringbuf = params->ctx->engine[ring->id].ringbuf;
  719. u64 exec_start;
  720. int instp_mode;
  721. u32 instp_mask;
  722. int ret;
  723. instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  724. instp_mask = I915_EXEC_CONSTANTS_MASK;
  725. switch (instp_mode) {
  726. case I915_EXEC_CONSTANTS_REL_GENERAL:
  727. case I915_EXEC_CONSTANTS_ABSOLUTE:
  728. case I915_EXEC_CONSTANTS_REL_SURFACE:
  729. if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
  730. DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  731. return -EINVAL;
  732. }
  733. if (instp_mode != dev_priv->relative_constants_mode) {
  734. if (instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  735. DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  736. return -EINVAL;
  737. }
  738. /* The HW changed the meaning on this bit on gen6 */
  739. instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  740. }
  741. break;
  742. default:
  743. DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  744. return -EINVAL;
  745. }
  746. if (args->num_cliprects != 0) {
  747. DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  748. return -EINVAL;
  749. } else {
  750. if (args->DR4 == 0xffffffff) {
  751. DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  752. args->DR4 = 0;
  753. }
  754. if (args->DR1 || args->DR4 || args->cliprects_ptr) {
  755. DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
  756. return -EINVAL;
  757. }
  758. }
  759. if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  760. DRM_DEBUG("sol reset is gen7 only\n");
  761. return -EINVAL;
  762. }
  763. ret = execlists_move_to_gpu(params->request, vmas);
  764. if (ret)
  765. return ret;
  766. if (ring == &dev_priv->ring[RCS] &&
  767. instp_mode != dev_priv->relative_constants_mode) {
  768. ret = intel_logical_ring_begin(params->request, 4);
  769. if (ret)
  770. return ret;
  771. intel_logical_ring_emit(ringbuf, MI_NOOP);
  772. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(1));
  773. intel_logical_ring_emit(ringbuf, INSTPM);
  774. intel_logical_ring_emit(ringbuf, instp_mask << 16 | instp_mode);
  775. intel_logical_ring_advance(ringbuf);
  776. dev_priv->relative_constants_mode = instp_mode;
  777. }
  778. exec_start = params->batch_obj_vm_offset +
  779. args->batch_start_offset;
  780. ret = ring->emit_bb_start(params->request, exec_start, params->dispatch_flags);
  781. if (ret)
  782. return ret;
  783. trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  784. i915_gem_execbuffer_move_to_active(vmas, params->request);
  785. i915_gem_execbuffer_retire_commands(params);
  786. return 0;
  787. }
  788. void intel_execlists_retire_requests(struct intel_engine_cs *ring)
  789. {
  790. struct drm_i915_gem_request *req, *tmp;
  791. struct list_head retired_list;
  792. WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  793. if (list_empty(&ring->execlist_retired_req_list))
  794. return;
  795. INIT_LIST_HEAD(&retired_list);
  796. spin_lock_irq(&ring->execlist_lock);
  797. list_replace_init(&ring->execlist_retired_req_list, &retired_list);
  798. spin_unlock_irq(&ring->execlist_lock);
  799. list_for_each_entry_safe(req, tmp, &retired_list, execlist_link) {
  800. struct intel_context *ctx = req->ctx;
  801. struct drm_i915_gem_object *ctx_obj =
  802. ctx->engine[ring->id].state;
  803. if (ctx_obj && (ctx != ring->default_context))
  804. intel_lr_context_unpin(req);
  805. list_del(&req->execlist_link);
  806. i915_gem_request_unreference(req);
  807. }
  808. }
  809. void intel_logical_ring_stop(struct intel_engine_cs *ring)
  810. {
  811. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  812. int ret;
  813. if (!intel_ring_initialized(ring))
  814. return;
  815. ret = intel_ring_idle(ring);
  816. if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
  817. DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
  818. ring->name, ret);
  819. /* TODO: Is this correct with Execlists enabled? */
  820. I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
  821. if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
  822. DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
  823. return;
  824. }
  825. I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
  826. }
  827. int logical_ring_flush_all_caches(struct drm_i915_gem_request *req)
  828. {
  829. struct intel_engine_cs *ring = req->ring;
  830. int ret;
  831. if (!ring->gpu_caches_dirty)
  832. return 0;
  833. ret = ring->emit_flush(req, 0, I915_GEM_GPU_DOMAINS);
  834. if (ret)
  835. return ret;
  836. ring->gpu_caches_dirty = false;
  837. return 0;
  838. }
  839. static int intel_lr_context_pin(struct drm_i915_gem_request *rq)
  840. {
  841. struct intel_engine_cs *ring = rq->ring;
  842. struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
  843. struct intel_ringbuffer *ringbuf = rq->ringbuf;
  844. int ret = 0;
  845. WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  846. if (rq->ctx->engine[ring->id].pin_count++ == 0) {
  847. ret = i915_gem_obj_ggtt_pin(ctx_obj,
  848. GEN8_LR_CONTEXT_ALIGN, 0);
  849. if (ret)
  850. goto reset_pin_count;
  851. ret = intel_pin_and_map_ringbuffer_obj(ring->dev, ringbuf);
  852. if (ret)
  853. goto unpin_ctx_obj;
  854. ctx_obj->dirty = true;
  855. }
  856. return ret;
  857. unpin_ctx_obj:
  858. i915_gem_object_ggtt_unpin(ctx_obj);
  859. reset_pin_count:
  860. rq->ctx->engine[ring->id].pin_count = 0;
  861. return ret;
  862. }
  863. void intel_lr_context_unpin(struct drm_i915_gem_request *rq)
  864. {
  865. struct intel_engine_cs *ring = rq->ring;
  866. struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring->id].state;
  867. struct intel_ringbuffer *ringbuf = rq->ringbuf;
  868. if (ctx_obj) {
  869. WARN_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  870. if (--rq->ctx->engine[ring->id].pin_count == 0) {
  871. intel_unpin_ringbuffer_obj(ringbuf);
  872. i915_gem_object_ggtt_unpin(ctx_obj);
  873. }
  874. }
  875. }
  876. static int intel_logical_ring_workarounds_emit(struct drm_i915_gem_request *req)
  877. {
  878. int ret, i;
  879. struct intel_engine_cs *ring = req->ring;
  880. struct intel_ringbuffer *ringbuf = req->ringbuf;
  881. struct drm_device *dev = ring->dev;
  882. struct drm_i915_private *dev_priv = dev->dev_private;
  883. struct i915_workarounds *w = &dev_priv->workarounds;
  884. if (WARN_ON_ONCE(w->count == 0))
  885. return 0;
  886. ring->gpu_caches_dirty = true;
  887. ret = logical_ring_flush_all_caches(req);
  888. if (ret)
  889. return ret;
  890. ret = intel_logical_ring_begin(req, w->count * 2 + 2);
  891. if (ret)
  892. return ret;
  893. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(w->count));
  894. for (i = 0; i < w->count; i++) {
  895. intel_logical_ring_emit(ringbuf, w->reg[i].addr);
  896. intel_logical_ring_emit(ringbuf, w->reg[i].value);
  897. }
  898. intel_logical_ring_emit(ringbuf, MI_NOOP);
  899. intel_logical_ring_advance(ringbuf);
  900. ring->gpu_caches_dirty = true;
  901. ret = logical_ring_flush_all_caches(req);
  902. if (ret)
  903. return ret;
  904. return 0;
  905. }
  906. #define wa_ctx_emit(batch, index, cmd) \
  907. do { \
  908. int __index = (index)++; \
  909. if (WARN_ON(__index >= (PAGE_SIZE / sizeof(uint32_t)))) { \
  910. return -ENOSPC; \
  911. } \
  912. batch[__index] = (cmd); \
  913. } while (0)
  914. /*
  915. * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
  916. * PIPE_CONTROL instruction. This is required for the flush to happen correctly
  917. * but there is a slight complication as this is applied in WA batch where the
  918. * values are only initialized once so we cannot take register value at the
  919. * beginning and reuse it further; hence we save its value to memory, upload a
  920. * constant value with bit21 set and then we restore it back with the saved value.
  921. * To simplify the WA, a constant value is formed by using the default value
  922. * of this register. This shouldn't be a problem because we are only modifying
  923. * it for a short period and this batch in non-premptible. We can ofcourse
  924. * use additional instructions that read the actual value of the register
  925. * at that time and set our bit of interest but it makes the WA complicated.
  926. *
  927. * This WA is also required for Gen9 so extracting as a function avoids
  928. * code duplication.
  929. */
  930. static inline int gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *ring,
  931. uint32_t *const batch,
  932. uint32_t index)
  933. {
  934. uint32_t l3sqc4_flush = (0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES);
  935. /*
  936. * WaDisableLSQCROPERFforOCL:skl
  937. * This WA is implemented in skl_init_clock_gating() but since
  938. * this batch updates GEN8_L3SQCREG4 with default value we need to
  939. * set this bit here to retain the WA during flush.
  940. */
  941. if (IS_SKYLAKE(ring->dev) && INTEL_REVID(ring->dev) <= SKL_REVID_E0)
  942. l3sqc4_flush |= GEN8_LQSC_RO_PERF_DIS;
  943. wa_ctx_emit(batch, index, (MI_STORE_REGISTER_MEM_GEN8(1) |
  944. MI_SRM_LRM_GLOBAL_GTT));
  945. wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
  946. wa_ctx_emit(batch, index, ring->scratch.gtt_offset + 256);
  947. wa_ctx_emit(batch, index, 0);
  948. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  949. wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
  950. wa_ctx_emit(batch, index, l3sqc4_flush);
  951. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  952. wa_ctx_emit(batch, index, (PIPE_CONTROL_CS_STALL |
  953. PIPE_CONTROL_DC_FLUSH_ENABLE));
  954. wa_ctx_emit(batch, index, 0);
  955. wa_ctx_emit(batch, index, 0);
  956. wa_ctx_emit(batch, index, 0);
  957. wa_ctx_emit(batch, index, 0);
  958. wa_ctx_emit(batch, index, (MI_LOAD_REGISTER_MEM_GEN8(1) |
  959. MI_SRM_LRM_GLOBAL_GTT));
  960. wa_ctx_emit(batch, index, GEN8_L3SQCREG4);
  961. wa_ctx_emit(batch, index, ring->scratch.gtt_offset + 256);
  962. wa_ctx_emit(batch, index, 0);
  963. return index;
  964. }
  965. static inline uint32_t wa_ctx_start(struct i915_wa_ctx_bb *wa_ctx,
  966. uint32_t offset,
  967. uint32_t start_alignment)
  968. {
  969. return wa_ctx->offset = ALIGN(offset, start_alignment);
  970. }
  971. static inline int wa_ctx_end(struct i915_wa_ctx_bb *wa_ctx,
  972. uint32_t offset,
  973. uint32_t size_alignment)
  974. {
  975. wa_ctx->size = offset - wa_ctx->offset;
  976. WARN(wa_ctx->size % size_alignment,
  977. "wa_ctx_bb failed sanity checks: size %d is not aligned to %d\n",
  978. wa_ctx->size, size_alignment);
  979. return 0;
  980. }
  981. /**
  982. * gen8_init_indirectctx_bb() - initialize indirect ctx batch with WA
  983. *
  984. * @ring: only applicable for RCS
  985. * @wa_ctx: structure representing wa_ctx
  986. * offset: specifies start of the batch, should be cache-aligned. This is updated
  987. * with the offset value received as input.
  988. * size: size of the batch in DWORDS but HW expects in terms of cachelines
  989. * @batch: page in which WA are loaded
  990. * @offset: This field specifies the start of the batch, it should be
  991. * cache-aligned otherwise it is adjusted accordingly.
  992. * Typically we only have one indirect_ctx and per_ctx batch buffer which are
  993. * initialized at the beginning and shared across all contexts but this field
  994. * helps us to have multiple batches at different offsets and select them based
  995. * on a criteria. At the moment this batch always start at the beginning of the page
  996. * and at this point we don't have multiple wa_ctx batch buffers.
  997. *
  998. * The number of WA applied are not known at the beginning; we use this field
  999. * to return the no of DWORDS written.
  1000. *
  1001. * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
  1002. * so it adds NOOPs as padding to make it cacheline aligned.
  1003. * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
  1004. * makes a complete batch buffer.
  1005. *
  1006. * Return: non-zero if we exceed the PAGE_SIZE limit.
  1007. */
  1008. static int gen8_init_indirectctx_bb(struct intel_engine_cs *ring,
  1009. struct i915_wa_ctx_bb *wa_ctx,
  1010. uint32_t *const batch,
  1011. uint32_t *offset)
  1012. {
  1013. uint32_t scratch_addr;
  1014. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1015. /* WaDisableCtxRestoreArbitration:bdw,chv */
  1016. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  1017. /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
  1018. if (IS_BROADWELL(ring->dev)) {
  1019. index = gen8_emit_flush_coherentl3_wa(ring, batch, index);
  1020. if (index < 0)
  1021. return index;
  1022. }
  1023. /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
  1024. /* Actual scratch location is at 128 bytes offset */
  1025. scratch_addr = ring->scratch.gtt_offset + 2*CACHELINE_BYTES;
  1026. wa_ctx_emit(batch, index, GFX_OP_PIPE_CONTROL(6));
  1027. wa_ctx_emit(batch, index, (PIPE_CONTROL_FLUSH_L3 |
  1028. PIPE_CONTROL_GLOBAL_GTT_IVB |
  1029. PIPE_CONTROL_CS_STALL |
  1030. PIPE_CONTROL_QW_WRITE));
  1031. wa_ctx_emit(batch, index, scratch_addr);
  1032. wa_ctx_emit(batch, index, 0);
  1033. wa_ctx_emit(batch, index, 0);
  1034. wa_ctx_emit(batch, index, 0);
  1035. /* Pad to end of cacheline */
  1036. while (index % CACHELINE_DWORDS)
  1037. wa_ctx_emit(batch, index, MI_NOOP);
  1038. /*
  1039. * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
  1040. * execution depends on the length specified in terms of cache lines
  1041. * in the register CTX_RCS_INDIRECT_CTX
  1042. */
  1043. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  1044. }
  1045. /**
  1046. * gen8_init_perctx_bb() - initialize per ctx batch with WA
  1047. *
  1048. * @ring: only applicable for RCS
  1049. * @wa_ctx: structure representing wa_ctx
  1050. * offset: specifies start of the batch, should be cache-aligned.
  1051. * size: size of the batch in DWORDS but HW expects in terms of cachelines
  1052. * @batch: page in which WA are loaded
  1053. * @offset: This field specifies the start of this batch.
  1054. * This batch is started immediately after indirect_ctx batch. Since we ensure
  1055. * that indirect_ctx ends on a cacheline this batch is aligned automatically.
  1056. *
  1057. * The number of DWORDS written are returned using this field.
  1058. *
  1059. * This batch is terminated with MI_BATCH_BUFFER_END and so we need not add padding
  1060. * to align it with cacheline as padding after MI_BATCH_BUFFER_END is redundant.
  1061. */
  1062. static int gen8_init_perctx_bb(struct intel_engine_cs *ring,
  1063. struct i915_wa_ctx_bb *wa_ctx,
  1064. uint32_t *const batch,
  1065. uint32_t *offset)
  1066. {
  1067. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1068. /* WaDisableCtxRestoreArbitration:bdw,chv */
  1069. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  1070. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  1071. return wa_ctx_end(wa_ctx, *offset = index, 1);
  1072. }
  1073. static int gen9_init_indirectctx_bb(struct intel_engine_cs *ring,
  1074. struct i915_wa_ctx_bb *wa_ctx,
  1075. uint32_t *const batch,
  1076. uint32_t *offset)
  1077. {
  1078. int ret;
  1079. struct drm_device *dev = ring->dev;
  1080. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1081. /* WaDisableCtxRestoreArbitration:skl,bxt */
  1082. if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) <= SKL_REVID_D0)) ||
  1083. (IS_BROXTON(dev) && (INTEL_REVID(dev) == BXT_REVID_A0)))
  1084. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_DISABLE);
  1085. /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt */
  1086. ret = gen8_emit_flush_coherentl3_wa(ring, batch, index);
  1087. if (ret < 0)
  1088. return ret;
  1089. index = ret;
  1090. /* Pad to end of cacheline */
  1091. while (index % CACHELINE_DWORDS)
  1092. wa_ctx_emit(batch, index, MI_NOOP);
  1093. return wa_ctx_end(wa_ctx, *offset = index, CACHELINE_DWORDS);
  1094. }
  1095. static int gen9_init_perctx_bb(struct intel_engine_cs *ring,
  1096. struct i915_wa_ctx_bb *wa_ctx,
  1097. uint32_t *const batch,
  1098. uint32_t *offset)
  1099. {
  1100. struct drm_device *dev = ring->dev;
  1101. uint32_t index = wa_ctx_start(wa_ctx, *offset, CACHELINE_DWORDS);
  1102. /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
  1103. if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) <= SKL_REVID_B0)) ||
  1104. (IS_BROXTON(dev) && (INTEL_REVID(dev) == BXT_REVID_A0))) {
  1105. wa_ctx_emit(batch, index, MI_LOAD_REGISTER_IMM(1));
  1106. wa_ctx_emit(batch, index, GEN9_SLICE_COMMON_ECO_CHICKEN0);
  1107. wa_ctx_emit(batch, index,
  1108. _MASKED_BIT_ENABLE(DISABLE_PIXEL_MASK_CAMMING));
  1109. wa_ctx_emit(batch, index, MI_NOOP);
  1110. }
  1111. /* WaDisableCtxRestoreArbitration:skl,bxt */
  1112. if ((IS_SKYLAKE(dev) && (INTEL_REVID(dev) <= SKL_REVID_D0)) ||
  1113. (IS_BROXTON(dev) && (INTEL_REVID(dev) == BXT_REVID_A0)))
  1114. wa_ctx_emit(batch, index, MI_ARB_ON_OFF | MI_ARB_ENABLE);
  1115. wa_ctx_emit(batch, index, MI_BATCH_BUFFER_END);
  1116. return wa_ctx_end(wa_ctx, *offset = index, 1);
  1117. }
  1118. static int lrc_setup_wa_ctx_obj(struct intel_engine_cs *ring, u32 size)
  1119. {
  1120. int ret;
  1121. ring->wa_ctx.obj = i915_gem_alloc_object(ring->dev, PAGE_ALIGN(size));
  1122. if (!ring->wa_ctx.obj) {
  1123. DRM_DEBUG_DRIVER("alloc LRC WA ctx backing obj failed.\n");
  1124. return -ENOMEM;
  1125. }
  1126. ret = i915_gem_obj_ggtt_pin(ring->wa_ctx.obj, PAGE_SIZE, 0);
  1127. if (ret) {
  1128. DRM_DEBUG_DRIVER("pin LRC WA ctx backing obj failed: %d\n",
  1129. ret);
  1130. drm_gem_object_unreference(&ring->wa_ctx.obj->base);
  1131. return ret;
  1132. }
  1133. return 0;
  1134. }
  1135. static void lrc_destroy_wa_ctx_obj(struct intel_engine_cs *ring)
  1136. {
  1137. if (ring->wa_ctx.obj) {
  1138. i915_gem_object_ggtt_unpin(ring->wa_ctx.obj);
  1139. drm_gem_object_unreference(&ring->wa_ctx.obj->base);
  1140. ring->wa_ctx.obj = NULL;
  1141. }
  1142. }
  1143. static int intel_init_workaround_bb(struct intel_engine_cs *ring)
  1144. {
  1145. int ret;
  1146. uint32_t *batch;
  1147. uint32_t offset;
  1148. struct page *page;
  1149. struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
  1150. WARN_ON(ring->id != RCS);
  1151. /* update this when WA for higher Gen are added */
  1152. if (INTEL_INFO(ring->dev)->gen > 9) {
  1153. DRM_ERROR("WA batch buffer is not initialized for Gen%d\n",
  1154. INTEL_INFO(ring->dev)->gen);
  1155. return 0;
  1156. }
  1157. /* some WA perform writes to scratch page, ensure it is valid */
  1158. if (ring->scratch.obj == NULL) {
  1159. DRM_ERROR("scratch page not allocated for %s\n", ring->name);
  1160. return -EINVAL;
  1161. }
  1162. ret = lrc_setup_wa_ctx_obj(ring, PAGE_SIZE);
  1163. if (ret) {
  1164. DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
  1165. return ret;
  1166. }
  1167. page = i915_gem_object_get_page(wa_ctx->obj, 0);
  1168. batch = kmap_atomic(page);
  1169. offset = 0;
  1170. if (INTEL_INFO(ring->dev)->gen == 8) {
  1171. ret = gen8_init_indirectctx_bb(ring,
  1172. &wa_ctx->indirect_ctx,
  1173. batch,
  1174. &offset);
  1175. if (ret)
  1176. goto out;
  1177. ret = gen8_init_perctx_bb(ring,
  1178. &wa_ctx->per_ctx,
  1179. batch,
  1180. &offset);
  1181. if (ret)
  1182. goto out;
  1183. } else if (INTEL_INFO(ring->dev)->gen == 9) {
  1184. ret = gen9_init_indirectctx_bb(ring,
  1185. &wa_ctx->indirect_ctx,
  1186. batch,
  1187. &offset);
  1188. if (ret)
  1189. goto out;
  1190. ret = gen9_init_perctx_bb(ring,
  1191. &wa_ctx->per_ctx,
  1192. batch,
  1193. &offset);
  1194. if (ret)
  1195. goto out;
  1196. }
  1197. out:
  1198. kunmap_atomic(batch);
  1199. if (ret)
  1200. lrc_destroy_wa_ctx_obj(ring);
  1201. return ret;
  1202. }
  1203. static int gen8_init_common_ring(struct intel_engine_cs *ring)
  1204. {
  1205. struct drm_device *dev = ring->dev;
  1206. struct drm_i915_private *dev_priv = dev->dev_private;
  1207. u8 next_context_status_buffer_hw;
  1208. I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
  1209. I915_WRITE(RING_HWSTAM(ring->mmio_base), 0xffffffff);
  1210. if (ring->status_page.obj) {
  1211. I915_WRITE(RING_HWS_PGA(ring->mmio_base),
  1212. (u32)ring->status_page.gfx_addr);
  1213. POSTING_READ(RING_HWS_PGA(ring->mmio_base));
  1214. }
  1215. I915_WRITE(RING_MODE_GEN7(ring),
  1216. _MASKED_BIT_DISABLE(GFX_REPLAY_MODE) |
  1217. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  1218. POSTING_READ(RING_MODE_GEN7(ring));
  1219. /*
  1220. * Instead of resetting the Context Status Buffer (CSB) read pointer to
  1221. * zero, we need to read the write pointer from hardware and use its
  1222. * value because "this register is power context save restored".
  1223. * Effectively, these states have been observed:
  1224. *
  1225. * | Suspend-to-idle (freeze) | Suspend-to-RAM (mem) |
  1226. * BDW | CSB regs not reset | CSB regs reset |
  1227. * CHT | CSB regs not reset | CSB regs not reset |
  1228. */
  1229. next_context_status_buffer_hw = (I915_READ(RING_CONTEXT_STATUS_PTR(ring))
  1230. & GEN8_CSB_PTR_MASK);
  1231. /*
  1232. * When the CSB registers are reset (also after power-up / gpu reset),
  1233. * CSB write pointer is set to all 1's, which is not valid, use '5' in
  1234. * this special case, so the first element read is CSB[0].
  1235. */
  1236. if (next_context_status_buffer_hw == GEN8_CSB_PTR_MASK)
  1237. next_context_status_buffer_hw = (GEN8_CSB_ENTRIES - 1);
  1238. ring->next_context_status_buffer = next_context_status_buffer_hw;
  1239. DRM_DEBUG_DRIVER("Execlists enabled for %s\n", ring->name);
  1240. memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
  1241. return 0;
  1242. }
  1243. static int gen8_init_render_ring(struct intel_engine_cs *ring)
  1244. {
  1245. struct drm_device *dev = ring->dev;
  1246. struct drm_i915_private *dev_priv = dev->dev_private;
  1247. int ret;
  1248. ret = gen8_init_common_ring(ring);
  1249. if (ret)
  1250. return ret;
  1251. /* We need to disable the AsyncFlip performance optimisations in order
  1252. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  1253. * programmed to '1' on all products.
  1254. *
  1255. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  1256. */
  1257. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  1258. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  1259. return init_workarounds_ring(ring);
  1260. }
  1261. static int gen9_init_render_ring(struct intel_engine_cs *ring)
  1262. {
  1263. int ret;
  1264. ret = gen8_init_common_ring(ring);
  1265. if (ret)
  1266. return ret;
  1267. return init_workarounds_ring(ring);
  1268. }
  1269. static int intel_logical_ring_emit_pdps(struct drm_i915_gem_request *req)
  1270. {
  1271. struct i915_hw_ppgtt *ppgtt = req->ctx->ppgtt;
  1272. struct intel_engine_cs *ring = req->ring;
  1273. struct intel_ringbuffer *ringbuf = req->ringbuf;
  1274. const int num_lri_cmds = GEN8_LEGACY_PDPES * 2;
  1275. int i, ret;
  1276. ret = intel_logical_ring_begin(req, num_lri_cmds * 2 + 2);
  1277. if (ret)
  1278. return ret;
  1279. intel_logical_ring_emit(ringbuf, MI_LOAD_REGISTER_IMM(num_lri_cmds));
  1280. for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
  1281. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  1282. intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_UDW(ring, i));
  1283. intel_logical_ring_emit(ringbuf, upper_32_bits(pd_daddr));
  1284. intel_logical_ring_emit(ringbuf, GEN8_RING_PDP_LDW(ring, i));
  1285. intel_logical_ring_emit(ringbuf, lower_32_bits(pd_daddr));
  1286. }
  1287. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1288. intel_logical_ring_advance(ringbuf);
  1289. return 0;
  1290. }
  1291. static int gen8_emit_bb_start(struct drm_i915_gem_request *req,
  1292. u64 offset, unsigned dispatch_flags)
  1293. {
  1294. struct intel_ringbuffer *ringbuf = req->ringbuf;
  1295. bool ppgtt = !(dispatch_flags & I915_DISPATCH_SECURE);
  1296. int ret;
  1297. /* Don't rely in hw updating PDPs, specially in lite-restore.
  1298. * Ideally, we should set Force PD Restore in ctx descriptor,
  1299. * but we can't. Force Restore would be a second option, but
  1300. * it is unsafe in case of lite-restore (because the ctx is
  1301. * not idle). */
  1302. if (req->ctx->ppgtt &&
  1303. (intel_ring_flag(req->ring) & req->ctx->ppgtt->pd_dirty_rings)) {
  1304. ret = intel_logical_ring_emit_pdps(req);
  1305. if (ret)
  1306. return ret;
  1307. req->ctx->ppgtt->pd_dirty_rings &= ~intel_ring_flag(req->ring);
  1308. }
  1309. ret = intel_logical_ring_begin(req, 4);
  1310. if (ret)
  1311. return ret;
  1312. /* FIXME(BDW): Address space and security selectors. */
  1313. intel_logical_ring_emit(ringbuf, MI_BATCH_BUFFER_START_GEN8 |
  1314. (ppgtt<<8) |
  1315. (dispatch_flags & I915_DISPATCH_RS ?
  1316. MI_BATCH_RESOURCE_STREAMER : 0));
  1317. intel_logical_ring_emit(ringbuf, lower_32_bits(offset));
  1318. intel_logical_ring_emit(ringbuf, upper_32_bits(offset));
  1319. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1320. intel_logical_ring_advance(ringbuf);
  1321. return 0;
  1322. }
  1323. static bool gen8_logical_ring_get_irq(struct intel_engine_cs *ring)
  1324. {
  1325. struct drm_device *dev = ring->dev;
  1326. struct drm_i915_private *dev_priv = dev->dev_private;
  1327. unsigned long flags;
  1328. if (WARN_ON(!intel_irqs_enabled(dev_priv)))
  1329. return false;
  1330. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  1331. if (ring->irq_refcount++ == 0) {
  1332. I915_WRITE_IMR(ring, ~(ring->irq_enable_mask | ring->irq_keep_mask));
  1333. POSTING_READ(RING_IMR(ring->mmio_base));
  1334. }
  1335. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  1336. return true;
  1337. }
  1338. static void gen8_logical_ring_put_irq(struct intel_engine_cs *ring)
  1339. {
  1340. struct drm_device *dev = ring->dev;
  1341. struct drm_i915_private *dev_priv = dev->dev_private;
  1342. unsigned long flags;
  1343. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  1344. if (--ring->irq_refcount == 0) {
  1345. I915_WRITE_IMR(ring, ~ring->irq_keep_mask);
  1346. POSTING_READ(RING_IMR(ring->mmio_base));
  1347. }
  1348. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  1349. }
  1350. static int gen8_emit_flush(struct drm_i915_gem_request *request,
  1351. u32 invalidate_domains,
  1352. u32 unused)
  1353. {
  1354. struct intel_ringbuffer *ringbuf = request->ringbuf;
  1355. struct intel_engine_cs *ring = ringbuf->ring;
  1356. struct drm_device *dev = ring->dev;
  1357. struct drm_i915_private *dev_priv = dev->dev_private;
  1358. uint32_t cmd;
  1359. int ret;
  1360. ret = intel_logical_ring_begin(request, 4);
  1361. if (ret)
  1362. return ret;
  1363. cmd = MI_FLUSH_DW + 1;
  1364. /* We always require a command barrier so that subsequent
  1365. * commands, such as breadcrumb interrupts, are strictly ordered
  1366. * wrt the contents of the write cache being flushed to memory
  1367. * (and thus being coherent from the CPU).
  1368. */
  1369. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1370. if (invalidate_domains & I915_GEM_GPU_DOMAINS) {
  1371. cmd |= MI_INVALIDATE_TLB;
  1372. if (ring == &dev_priv->ring[VCS])
  1373. cmd |= MI_INVALIDATE_BSD;
  1374. }
  1375. intel_logical_ring_emit(ringbuf, cmd);
  1376. intel_logical_ring_emit(ringbuf,
  1377. I915_GEM_HWS_SCRATCH_ADDR |
  1378. MI_FLUSH_DW_USE_GTT);
  1379. intel_logical_ring_emit(ringbuf, 0); /* upper addr */
  1380. intel_logical_ring_emit(ringbuf, 0); /* value */
  1381. intel_logical_ring_advance(ringbuf);
  1382. return 0;
  1383. }
  1384. static int gen8_emit_flush_render(struct drm_i915_gem_request *request,
  1385. u32 invalidate_domains,
  1386. u32 flush_domains)
  1387. {
  1388. struct intel_ringbuffer *ringbuf = request->ringbuf;
  1389. struct intel_engine_cs *ring = ringbuf->ring;
  1390. u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  1391. bool vf_flush_wa;
  1392. u32 flags = 0;
  1393. int ret;
  1394. flags |= PIPE_CONTROL_CS_STALL;
  1395. if (flush_domains) {
  1396. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  1397. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  1398. }
  1399. if (invalidate_domains) {
  1400. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  1401. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  1402. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  1403. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1404. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  1405. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  1406. flags |= PIPE_CONTROL_QW_WRITE;
  1407. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1408. }
  1409. /*
  1410. * On GEN9+ Before VF_CACHE_INVALIDATE we need to emit a NULL pipe
  1411. * control.
  1412. */
  1413. vf_flush_wa = INTEL_INFO(ring->dev)->gen >= 9 &&
  1414. flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1415. ret = intel_logical_ring_begin(request, vf_flush_wa ? 12 : 6);
  1416. if (ret)
  1417. return ret;
  1418. if (vf_flush_wa) {
  1419. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  1420. intel_logical_ring_emit(ringbuf, 0);
  1421. intel_logical_ring_emit(ringbuf, 0);
  1422. intel_logical_ring_emit(ringbuf, 0);
  1423. intel_logical_ring_emit(ringbuf, 0);
  1424. intel_logical_ring_emit(ringbuf, 0);
  1425. }
  1426. intel_logical_ring_emit(ringbuf, GFX_OP_PIPE_CONTROL(6));
  1427. intel_logical_ring_emit(ringbuf, flags);
  1428. intel_logical_ring_emit(ringbuf, scratch_addr);
  1429. intel_logical_ring_emit(ringbuf, 0);
  1430. intel_logical_ring_emit(ringbuf, 0);
  1431. intel_logical_ring_emit(ringbuf, 0);
  1432. intel_logical_ring_advance(ringbuf);
  1433. return 0;
  1434. }
  1435. static u32 gen8_get_seqno(struct intel_engine_cs *ring, bool lazy_coherency)
  1436. {
  1437. return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
  1438. }
  1439. static void gen8_set_seqno(struct intel_engine_cs *ring, u32 seqno)
  1440. {
  1441. intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
  1442. }
  1443. static int gen8_emit_request(struct drm_i915_gem_request *request)
  1444. {
  1445. struct intel_ringbuffer *ringbuf = request->ringbuf;
  1446. struct intel_engine_cs *ring = ringbuf->ring;
  1447. u32 cmd;
  1448. int ret;
  1449. /*
  1450. * Reserve space for 2 NOOPs at the end of each request to be
  1451. * used as a workaround for not being allowed to do lite
  1452. * restore with HEAD==TAIL (WaIdleLiteRestore).
  1453. */
  1454. ret = intel_logical_ring_begin(request, 8);
  1455. if (ret)
  1456. return ret;
  1457. cmd = MI_STORE_DWORD_IMM_GEN4;
  1458. cmd |= MI_GLOBAL_GTT;
  1459. intel_logical_ring_emit(ringbuf, cmd);
  1460. intel_logical_ring_emit(ringbuf,
  1461. (ring->status_page.gfx_addr +
  1462. (I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT)));
  1463. intel_logical_ring_emit(ringbuf, 0);
  1464. intel_logical_ring_emit(ringbuf, i915_gem_request_get_seqno(request));
  1465. intel_logical_ring_emit(ringbuf, MI_USER_INTERRUPT);
  1466. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1467. intel_logical_ring_advance_and_submit(request);
  1468. /*
  1469. * Here we add two extra NOOPs as padding to avoid
  1470. * lite restore of a context with HEAD==TAIL.
  1471. */
  1472. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1473. intel_logical_ring_emit(ringbuf, MI_NOOP);
  1474. intel_logical_ring_advance(ringbuf);
  1475. return 0;
  1476. }
  1477. static int intel_lr_context_render_state_init(struct drm_i915_gem_request *req)
  1478. {
  1479. struct render_state so;
  1480. int ret;
  1481. ret = i915_gem_render_state_prepare(req->ring, &so);
  1482. if (ret)
  1483. return ret;
  1484. if (so.rodata == NULL)
  1485. return 0;
  1486. ret = req->ring->emit_bb_start(req, so.ggtt_offset,
  1487. I915_DISPATCH_SECURE);
  1488. if (ret)
  1489. goto out;
  1490. ret = req->ring->emit_bb_start(req,
  1491. (so.ggtt_offset + so.aux_batch_offset),
  1492. I915_DISPATCH_SECURE);
  1493. if (ret)
  1494. goto out;
  1495. i915_vma_move_to_active(i915_gem_obj_to_ggtt(so.obj), req);
  1496. out:
  1497. i915_gem_render_state_fini(&so);
  1498. return ret;
  1499. }
  1500. static int gen8_init_rcs_context(struct drm_i915_gem_request *req)
  1501. {
  1502. int ret;
  1503. ret = intel_logical_ring_workarounds_emit(req);
  1504. if (ret)
  1505. return ret;
  1506. ret = intel_rcs_context_init_mocs(req);
  1507. /*
  1508. * Failing to program the MOCS is non-fatal.The system will not
  1509. * run at peak performance. So generate an error and carry on.
  1510. */
  1511. if (ret)
  1512. DRM_ERROR("MOCS failed to program: expect performance issues.\n");
  1513. return intel_lr_context_render_state_init(req);
  1514. }
  1515. /**
  1516. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1517. *
  1518. * @ring: Engine Command Streamer.
  1519. *
  1520. */
  1521. void intel_logical_ring_cleanup(struct intel_engine_cs *ring)
  1522. {
  1523. struct drm_i915_private *dev_priv;
  1524. if (!intel_ring_initialized(ring))
  1525. return;
  1526. dev_priv = ring->dev->dev_private;
  1527. intel_logical_ring_stop(ring);
  1528. WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
  1529. if (ring->cleanup)
  1530. ring->cleanup(ring);
  1531. i915_cmd_parser_fini_ring(ring);
  1532. i915_gem_batch_pool_fini(&ring->batch_pool);
  1533. if (ring->status_page.obj) {
  1534. kunmap(sg_page(ring->status_page.obj->pages->sgl));
  1535. ring->status_page.obj = NULL;
  1536. }
  1537. lrc_destroy_wa_ctx_obj(ring);
  1538. }
  1539. static int logical_ring_init(struct drm_device *dev, struct intel_engine_cs *ring)
  1540. {
  1541. int ret;
  1542. /* Intentionally left blank. */
  1543. ring->buffer = NULL;
  1544. ring->dev = dev;
  1545. INIT_LIST_HEAD(&ring->active_list);
  1546. INIT_LIST_HEAD(&ring->request_list);
  1547. i915_gem_batch_pool_init(dev, &ring->batch_pool);
  1548. init_waitqueue_head(&ring->irq_queue);
  1549. INIT_LIST_HEAD(&ring->execlist_queue);
  1550. INIT_LIST_HEAD(&ring->execlist_retired_req_list);
  1551. spin_lock_init(&ring->execlist_lock);
  1552. ret = i915_cmd_parser_init_ring(ring);
  1553. if (ret)
  1554. return ret;
  1555. ret = intel_lr_context_deferred_create(ring->default_context, ring);
  1556. return ret;
  1557. }
  1558. static int logical_render_ring_init(struct drm_device *dev)
  1559. {
  1560. struct drm_i915_private *dev_priv = dev->dev_private;
  1561. struct intel_engine_cs *ring = &dev_priv->ring[RCS];
  1562. int ret;
  1563. ring->name = "render ring";
  1564. ring->id = RCS;
  1565. ring->mmio_base = RENDER_RING_BASE;
  1566. ring->irq_enable_mask =
  1567. GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
  1568. ring->irq_keep_mask =
  1569. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT;
  1570. if (HAS_L3_DPF(dev))
  1571. ring->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1572. if (INTEL_INFO(dev)->gen >= 9)
  1573. ring->init_hw = gen9_init_render_ring;
  1574. else
  1575. ring->init_hw = gen8_init_render_ring;
  1576. ring->init_context = gen8_init_rcs_context;
  1577. ring->cleanup = intel_fini_pipe_control;
  1578. ring->get_seqno = gen8_get_seqno;
  1579. ring->set_seqno = gen8_set_seqno;
  1580. ring->emit_request = gen8_emit_request;
  1581. ring->emit_flush = gen8_emit_flush_render;
  1582. ring->irq_get = gen8_logical_ring_get_irq;
  1583. ring->irq_put = gen8_logical_ring_put_irq;
  1584. ring->emit_bb_start = gen8_emit_bb_start;
  1585. ring->dev = dev;
  1586. ret = intel_init_pipe_control(ring);
  1587. if (ret)
  1588. return ret;
  1589. ret = intel_init_workaround_bb(ring);
  1590. if (ret) {
  1591. /*
  1592. * We continue even if we fail to initialize WA batch
  1593. * because we only expect rare glitches but nothing
  1594. * critical to prevent us from using GPU
  1595. */
  1596. DRM_ERROR("WA batch buffer initialization failed: %d\n",
  1597. ret);
  1598. }
  1599. ret = logical_ring_init(dev, ring);
  1600. if (ret) {
  1601. lrc_destroy_wa_ctx_obj(ring);
  1602. }
  1603. return ret;
  1604. }
  1605. static int logical_bsd_ring_init(struct drm_device *dev)
  1606. {
  1607. struct drm_i915_private *dev_priv = dev->dev_private;
  1608. struct intel_engine_cs *ring = &dev_priv->ring[VCS];
  1609. ring->name = "bsd ring";
  1610. ring->id = VCS;
  1611. ring->mmio_base = GEN6_BSD_RING_BASE;
  1612. ring->irq_enable_mask =
  1613. GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1614. ring->irq_keep_mask =
  1615. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1616. ring->init_hw = gen8_init_common_ring;
  1617. ring->get_seqno = gen8_get_seqno;
  1618. ring->set_seqno = gen8_set_seqno;
  1619. ring->emit_request = gen8_emit_request;
  1620. ring->emit_flush = gen8_emit_flush;
  1621. ring->irq_get = gen8_logical_ring_get_irq;
  1622. ring->irq_put = gen8_logical_ring_put_irq;
  1623. ring->emit_bb_start = gen8_emit_bb_start;
  1624. return logical_ring_init(dev, ring);
  1625. }
  1626. static int logical_bsd2_ring_init(struct drm_device *dev)
  1627. {
  1628. struct drm_i915_private *dev_priv = dev->dev_private;
  1629. struct intel_engine_cs *ring = &dev_priv->ring[VCS2];
  1630. ring->name = "bds2 ring";
  1631. ring->id = VCS2;
  1632. ring->mmio_base = GEN8_BSD2_RING_BASE;
  1633. ring->irq_enable_mask =
  1634. GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  1635. ring->irq_keep_mask =
  1636. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  1637. ring->init_hw = gen8_init_common_ring;
  1638. ring->get_seqno = gen8_get_seqno;
  1639. ring->set_seqno = gen8_set_seqno;
  1640. ring->emit_request = gen8_emit_request;
  1641. ring->emit_flush = gen8_emit_flush;
  1642. ring->irq_get = gen8_logical_ring_get_irq;
  1643. ring->irq_put = gen8_logical_ring_put_irq;
  1644. ring->emit_bb_start = gen8_emit_bb_start;
  1645. return logical_ring_init(dev, ring);
  1646. }
  1647. static int logical_blt_ring_init(struct drm_device *dev)
  1648. {
  1649. struct drm_i915_private *dev_priv = dev->dev_private;
  1650. struct intel_engine_cs *ring = &dev_priv->ring[BCS];
  1651. ring->name = "blitter ring";
  1652. ring->id = BCS;
  1653. ring->mmio_base = BLT_RING_BASE;
  1654. ring->irq_enable_mask =
  1655. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1656. ring->irq_keep_mask =
  1657. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1658. ring->init_hw = gen8_init_common_ring;
  1659. ring->get_seqno = gen8_get_seqno;
  1660. ring->set_seqno = gen8_set_seqno;
  1661. ring->emit_request = gen8_emit_request;
  1662. ring->emit_flush = gen8_emit_flush;
  1663. ring->irq_get = gen8_logical_ring_get_irq;
  1664. ring->irq_put = gen8_logical_ring_put_irq;
  1665. ring->emit_bb_start = gen8_emit_bb_start;
  1666. return logical_ring_init(dev, ring);
  1667. }
  1668. static int logical_vebox_ring_init(struct drm_device *dev)
  1669. {
  1670. struct drm_i915_private *dev_priv = dev->dev_private;
  1671. struct intel_engine_cs *ring = &dev_priv->ring[VECS];
  1672. ring->name = "video enhancement ring";
  1673. ring->id = VECS;
  1674. ring->mmio_base = VEBOX_RING_BASE;
  1675. ring->irq_enable_mask =
  1676. GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1677. ring->irq_keep_mask =
  1678. GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1679. ring->init_hw = gen8_init_common_ring;
  1680. ring->get_seqno = gen8_get_seqno;
  1681. ring->set_seqno = gen8_set_seqno;
  1682. ring->emit_request = gen8_emit_request;
  1683. ring->emit_flush = gen8_emit_flush;
  1684. ring->irq_get = gen8_logical_ring_get_irq;
  1685. ring->irq_put = gen8_logical_ring_put_irq;
  1686. ring->emit_bb_start = gen8_emit_bb_start;
  1687. return logical_ring_init(dev, ring);
  1688. }
  1689. /**
  1690. * intel_logical_rings_init() - allocate, populate and init the Engine Command Streamers
  1691. * @dev: DRM device.
  1692. *
  1693. * This function inits the engines for an Execlists submission style (the equivalent in the
  1694. * legacy ringbuffer submission world would be i915_gem_init_rings). It does it only for
  1695. * those engines that are present in the hardware.
  1696. *
  1697. * Return: non-zero if the initialization failed.
  1698. */
  1699. int intel_logical_rings_init(struct drm_device *dev)
  1700. {
  1701. struct drm_i915_private *dev_priv = dev->dev_private;
  1702. int ret;
  1703. ret = logical_render_ring_init(dev);
  1704. if (ret)
  1705. return ret;
  1706. if (HAS_BSD(dev)) {
  1707. ret = logical_bsd_ring_init(dev);
  1708. if (ret)
  1709. goto cleanup_render_ring;
  1710. }
  1711. if (HAS_BLT(dev)) {
  1712. ret = logical_blt_ring_init(dev);
  1713. if (ret)
  1714. goto cleanup_bsd_ring;
  1715. }
  1716. if (HAS_VEBOX(dev)) {
  1717. ret = logical_vebox_ring_init(dev);
  1718. if (ret)
  1719. goto cleanup_blt_ring;
  1720. }
  1721. if (HAS_BSD2(dev)) {
  1722. ret = logical_bsd2_ring_init(dev);
  1723. if (ret)
  1724. goto cleanup_vebox_ring;
  1725. }
  1726. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  1727. if (ret)
  1728. goto cleanup_bsd2_ring;
  1729. return 0;
  1730. cleanup_bsd2_ring:
  1731. intel_logical_ring_cleanup(&dev_priv->ring[VCS2]);
  1732. cleanup_vebox_ring:
  1733. intel_logical_ring_cleanup(&dev_priv->ring[VECS]);
  1734. cleanup_blt_ring:
  1735. intel_logical_ring_cleanup(&dev_priv->ring[BCS]);
  1736. cleanup_bsd_ring:
  1737. intel_logical_ring_cleanup(&dev_priv->ring[VCS]);
  1738. cleanup_render_ring:
  1739. intel_logical_ring_cleanup(&dev_priv->ring[RCS]);
  1740. return ret;
  1741. }
  1742. static u32
  1743. make_rpcs(struct drm_device *dev)
  1744. {
  1745. u32 rpcs = 0;
  1746. /*
  1747. * No explicit RPCS request is needed to ensure full
  1748. * slice/subslice/EU enablement prior to Gen9.
  1749. */
  1750. if (INTEL_INFO(dev)->gen < 9)
  1751. return 0;
  1752. /*
  1753. * Starting in Gen9, render power gating can leave
  1754. * slice/subslice/EU in a partially enabled state. We
  1755. * must make an explicit request through RPCS for full
  1756. * enablement.
  1757. */
  1758. if (INTEL_INFO(dev)->has_slice_pg) {
  1759. rpcs |= GEN8_RPCS_S_CNT_ENABLE;
  1760. rpcs |= INTEL_INFO(dev)->slice_total <<
  1761. GEN8_RPCS_S_CNT_SHIFT;
  1762. rpcs |= GEN8_RPCS_ENABLE;
  1763. }
  1764. if (INTEL_INFO(dev)->has_subslice_pg) {
  1765. rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
  1766. rpcs |= INTEL_INFO(dev)->subslice_per_slice <<
  1767. GEN8_RPCS_SS_CNT_SHIFT;
  1768. rpcs |= GEN8_RPCS_ENABLE;
  1769. }
  1770. if (INTEL_INFO(dev)->has_eu_pg) {
  1771. rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
  1772. GEN8_RPCS_EU_MIN_SHIFT;
  1773. rpcs |= INTEL_INFO(dev)->eu_per_subslice <<
  1774. GEN8_RPCS_EU_MAX_SHIFT;
  1775. rpcs |= GEN8_RPCS_ENABLE;
  1776. }
  1777. return rpcs;
  1778. }
  1779. static int
  1780. populate_lr_context(struct intel_context *ctx, struct drm_i915_gem_object *ctx_obj,
  1781. struct intel_engine_cs *ring, struct intel_ringbuffer *ringbuf)
  1782. {
  1783. struct drm_device *dev = ring->dev;
  1784. struct drm_i915_private *dev_priv = dev->dev_private;
  1785. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt;
  1786. struct page *page;
  1787. uint32_t *reg_state;
  1788. int ret;
  1789. if (!ppgtt)
  1790. ppgtt = dev_priv->mm.aliasing_ppgtt;
  1791. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  1792. if (ret) {
  1793. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  1794. return ret;
  1795. }
  1796. ret = i915_gem_object_get_pages(ctx_obj);
  1797. if (ret) {
  1798. DRM_DEBUG_DRIVER("Could not get object pages\n");
  1799. return ret;
  1800. }
  1801. i915_gem_object_pin_pages(ctx_obj);
  1802. /* The second page of the context object contains some fields which must
  1803. * be set up prior to the first execution. */
  1804. page = i915_gem_object_get_page(ctx_obj, 1);
  1805. reg_state = kmap_atomic(page);
  1806. /* A context is actually a big batch buffer with several MI_LOAD_REGISTER_IMM
  1807. * commands followed by (reg, value) pairs. The values we are setting here are
  1808. * only for the first context restore: on a subsequent save, the GPU will
  1809. * recreate this batchbuffer with new values (including all the missing
  1810. * MI_LOAD_REGISTER_IMM commands that we are not initializing here). */
  1811. if (ring->id == RCS)
  1812. reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(14);
  1813. else
  1814. reg_state[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(11);
  1815. reg_state[CTX_LRI_HEADER_0] |= MI_LRI_FORCE_POSTED;
  1816. reg_state[CTX_CONTEXT_CONTROL] = RING_CONTEXT_CONTROL(ring);
  1817. reg_state[CTX_CONTEXT_CONTROL+1] =
  1818. _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
  1819. CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
  1820. CTX_CTRL_RS_CTX_ENABLE);
  1821. reg_state[CTX_RING_HEAD] = RING_HEAD(ring->mmio_base);
  1822. reg_state[CTX_RING_HEAD+1] = 0;
  1823. reg_state[CTX_RING_TAIL] = RING_TAIL(ring->mmio_base);
  1824. reg_state[CTX_RING_TAIL+1] = 0;
  1825. reg_state[CTX_RING_BUFFER_START] = RING_START(ring->mmio_base);
  1826. /* Ring buffer start address is not known until the buffer is pinned.
  1827. * It is written to the context image in execlists_update_context()
  1828. */
  1829. reg_state[CTX_RING_BUFFER_CONTROL] = RING_CTL(ring->mmio_base);
  1830. reg_state[CTX_RING_BUFFER_CONTROL+1] =
  1831. ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES) | RING_VALID;
  1832. reg_state[CTX_BB_HEAD_U] = ring->mmio_base + 0x168;
  1833. reg_state[CTX_BB_HEAD_U+1] = 0;
  1834. reg_state[CTX_BB_HEAD_L] = ring->mmio_base + 0x140;
  1835. reg_state[CTX_BB_HEAD_L+1] = 0;
  1836. reg_state[CTX_BB_STATE] = ring->mmio_base + 0x110;
  1837. reg_state[CTX_BB_STATE+1] = (1<<5);
  1838. reg_state[CTX_SECOND_BB_HEAD_U] = ring->mmio_base + 0x11c;
  1839. reg_state[CTX_SECOND_BB_HEAD_U+1] = 0;
  1840. reg_state[CTX_SECOND_BB_HEAD_L] = ring->mmio_base + 0x114;
  1841. reg_state[CTX_SECOND_BB_HEAD_L+1] = 0;
  1842. reg_state[CTX_SECOND_BB_STATE] = ring->mmio_base + 0x118;
  1843. reg_state[CTX_SECOND_BB_STATE+1] = 0;
  1844. if (ring->id == RCS) {
  1845. reg_state[CTX_BB_PER_CTX_PTR] = ring->mmio_base + 0x1c0;
  1846. reg_state[CTX_BB_PER_CTX_PTR+1] = 0;
  1847. reg_state[CTX_RCS_INDIRECT_CTX] = ring->mmio_base + 0x1c4;
  1848. reg_state[CTX_RCS_INDIRECT_CTX+1] = 0;
  1849. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET] = ring->mmio_base + 0x1c8;
  1850. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] = 0;
  1851. if (ring->wa_ctx.obj) {
  1852. struct i915_ctx_workarounds *wa_ctx = &ring->wa_ctx;
  1853. uint32_t ggtt_offset = i915_gem_obj_ggtt_offset(wa_ctx->obj);
  1854. reg_state[CTX_RCS_INDIRECT_CTX+1] =
  1855. (ggtt_offset + wa_ctx->indirect_ctx.offset * sizeof(uint32_t)) |
  1856. (wa_ctx->indirect_ctx.size / CACHELINE_DWORDS);
  1857. reg_state[CTX_RCS_INDIRECT_CTX_OFFSET+1] =
  1858. CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT << 6;
  1859. reg_state[CTX_BB_PER_CTX_PTR+1] =
  1860. (ggtt_offset + wa_ctx->per_ctx.offset * sizeof(uint32_t)) |
  1861. 0x01;
  1862. }
  1863. }
  1864. reg_state[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9);
  1865. reg_state[CTX_LRI_HEADER_1] |= MI_LRI_FORCE_POSTED;
  1866. reg_state[CTX_CTX_TIMESTAMP] = ring->mmio_base + 0x3a8;
  1867. reg_state[CTX_CTX_TIMESTAMP+1] = 0;
  1868. reg_state[CTX_PDP3_UDW] = GEN8_RING_PDP_UDW(ring, 3);
  1869. reg_state[CTX_PDP3_LDW] = GEN8_RING_PDP_LDW(ring, 3);
  1870. reg_state[CTX_PDP2_UDW] = GEN8_RING_PDP_UDW(ring, 2);
  1871. reg_state[CTX_PDP2_LDW] = GEN8_RING_PDP_LDW(ring, 2);
  1872. reg_state[CTX_PDP1_UDW] = GEN8_RING_PDP_UDW(ring, 1);
  1873. reg_state[CTX_PDP1_LDW] = GEN8_RING_PDP_LDW(ring, 1);
  1874. reg_state[CTX_PDP0_UDW] = GEN8_RING_PDP_UDW(ring, 0);
  1875. reg_state[CTX_PDP0_LDW] = GEN8_RING_PDP_LDW(ring, 0);
  1876. /* With dynamic page allocation, PDPs may not be allocated at this point,
  1877. * Point the unallocated PDPs to the scratch page
  1878. */
  1879. ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
  1880. ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
  1881. ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
  1882. ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
  1883. if (ring->id == RCS) {
  1884. reg_state[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  1885. reg_state[CTX_R_PWR_CLK_STATE] = GEN8_R_PWR_CLK_STATE;
  1886. reg_state[CTX_R_PWR_CLK_STATE+1] = make_rpcs(dev);
  1887. }
  1888. kunmap_atomic(reg_state);
  1889. ctx_obj->dirty = 1;
  1890. set_page_dirty(page);
  1891. i915_gem_object_unpin_pages(ctx_obj);
  1892. return 0;
  1893. }
  1894. /**
  1895. * intel_lr_context_free() - free the LRC specific bits of a context
  1896. * @ctx: the LR context to free.
  1897. *
  1898. * The real context freeing is done in i915_gem_context_free: this only
  1899. * takes care of the bits that are LRC related: the per-engine backing
  1900. * objects and the logical ringbuffer.
  1901. */
  1902. void intel_lr_context_free(struct intel_context *ctx)
  1903. {
  1904. int i;
  1905. for (i = 0; i < I915_NUM_RINGS; i++) {
  1906. struct drm_i915_gem_object *ctx_obj = ctx->engine[i].state;
  1907. if (ctx_obj) {
  1908. struct intel_ringbuffer *ringbuf =
  1909. ctx->engine[i].ringbuf;
  1910. struct intel_engine_cs *ring = ringbuf->ring;
  1911. if (ctx == ring->default_context) {
  1912. intel_unpin_ringbuffer_obj(ringbuf);
  1913. i915_gem_object_ggtt_unpin(ctx_obj);
  1914. }
  1915. WARN_ON(ctx->engine[ring->id].pin_count);
  1916. intel_destroy_ringbuffer_obj(ringbuf);
  1917. kfree(ringbuf);
  1918. drm_gem_object_unreference(&ctx_obj->base);
  1919. }
  1920. }
  1921. }
  1922. static uint32_t get_lr_context_size(struct intel_engine_cs *ring)
  1923. {
  1924. int ret = 0;
  1925. WARN_ON(INTEL_INFO(ring->dev)->gen < 8);
  1926. switch (ring->id) {
  1927. case RCS:
  1928. if (INTEL_INFO(ring->dev)->gen >= 9)
  1929. ret = GEN9_LR_CONTEXT_RENDER_SIZE;
  1930. else
  1931. ret = GEN8_LR_CONTEXT_RENDER_SIZE;
  1932. break;
  1933. case VCS:
  1934. case BCS:
  1935. case VECS:
  1936. case VCS2:
  1937. ret = GEN8_LR_CONTEXT_OTHER_SIZE;
  1938. break;
  1939. }
  1940. return ret;
  1941. }
  1942. static void lrc_setup_hardware_status_page(struct intel_engine_cs *ring,
  1943. struct drm_i915_gem_object *default_ctx_obj)
  1944. {
  1945. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1946. /* The status page is offset 0 from the default context object
  1947. * in LRC mode. */
  1948. ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(default_ctx_obj);
  1949. ring->status_page.page_addr =
  1950. kmap(sg_page(default_ctx_obj->pages->sgl));
  1951. ring->status_page.obj = default_ctx_obj;
  1952. I915_WRITE(RING_HWS_PGA(ring->mmio_base),
  1953. (u32)ring->status_page.gfx_addr);
  1954. POSTING_READ(RING_HWS_PGA(ring->mmio_base));
  1955. }
  1956. /**
  1957. * intel_lr_context_deferred_create() - create the LRC specific bits of a context
  1958. * @ctx: LR context to create.
  1959. * @ring: engine to be used with the context.
  1960. *
  1961. * This function can be called more than once, with different engines, if we plan
  1962. * to use the context with them. The context backing objects and the ringbuffers
  1963. * (specially the ringbuffer backing objects) suck a lot of memory up, and that's why
  1964. * the creation is a deferred call: it's better to make sure first that we need to use
  1965. * a given ring with the context.
  1966. *
  1967. * Return: non-zero on error.
  1968. */
  1969. int intel_lr_context_deferred_create(struct intel_context *ctx,
  1970. struct intel_engine_cs *ring)
  1971. {
  1972. const bool is_global_default_ctx = (ctx == ring->default_context);
  1973. struct drm_device *dev = ring->dev;
  1974. struct drm_i915_gem_object *ctx_obj;
  1975. uint32_t context_size;
  1976. struct intel_ringbuffer *ringbuf;
  1977. int ret;
  1978. WARN_ON(ctx->legacy_hw_ctx.rcs_state != NULL);
  1979. WARN_ON(ctx->engine[ring->id].state);
  1980. context_size = round_up(get_lr_context_size(ring), 4096);
  1981. ctx_obj = i915_gem_alloc_object(dev, context_size);
  1982. if (!ctx_obj) {
  1983. DRM_DEBUG_DRIVER("Alloc LRC backing obj failed.\n");
  1984. return -ENOMEM;
  1985. }
  1986. if (is_global_default_ctx) {
  1987. ret = i915_gem_obj_ggtt_pin(ctx_obj, GEN8_LR_CONTEXT_ALIGN, 0);
  1988. if (ret) {
  1989. DRM_DEBUG_DRIVER("Pin LRC backing obj failed: %d\n",
  1990. ret);
  1991. drm_gem_object_unreference(&ctx_obj->base);
  1992. return ret;
  1993. }
  1994. }
  1995. ringbuf = kzalloc(sizeof(*ringbuf), GFP_KERNEL);
  1996. if (!ringbuf) {
  1997. DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
  1998. ring->name);
  1999. ret = -ENOMEM;
  2000. goto error_unpin_ctx;
  2001. }
  2002. ringbuf->ring = ring;
  2003. ringbuf->size = 32 * PAGE_SIZE;
  2004. ringbuf->effective_size = ringbuf->size;
  2005. ringbuf->head = 0;
  2006. ringbuf->tail = 0;
  2007. ringbuf->last_retired_head = -1;
  2008. intel_ring_update_space(ringbuf);
  2009. if (ringbuf->obj == NULL) {
  2010. ret = intel_alloc_ringbuffer_obj(dev, ringbuf);
  2011. if (ret) {
  2012. DRM_DEBUG_DRIVER(
  2013. "Failed to allocate ringbuffer obj %s: %d\n",
  2014. ring->name, ret);
  2015. goto error_free_rbuf;
  2016. }
  2017. if (is_global_default_ctx) {
  2018. ret = intel_pin_and_map_ringbuffer_obj(dev, ringbuf);
  2019. if (ret) {
  2020. DRM_ERROR(
  2021. "Failed to pin and map ringbuffer %s: %d\n",
  2022. ring->name, ret);
  2023. goto error_destroy_rbuf;
  2024. }
  2025. }
  2026. }
  2027. ret = populate_lr_context(ctx, ctx_obj, ring, ringbuf);
  2028. if (ret) {
  2029. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  2030. goto error;
  2031. }
  2032. ctx->engine[ring->id].ringbuf = ringbuf;
  2033. ctx->engine[ring->id].state = ctx_obj;
  2034. if (ctx == ring->default_context)
  2035. lrc_setup_hardware_status_page(ring, ctx_obj);
  2036. else if (ring->id == RCS && !ctx->rcs_initialized) {
  2037. if (ring->init_context) {
  2038. struct drm_i915_gem_request *req;
  2039. ret = i915_gem_request_alloc(ring, ctx, &req);
  2040. if (ret)
  2041. return ret;
  2042. ret = ring->init_context(req);
  2043. if (ret) {
  2044. DRM_ERROR("ring init context: %d\n", ret);
  2045. i915_gem_request_cancel(req);
  2046. ctx->engine[ring->id].ringbuf = NULL;
  2047. ctx->engine[ring->id].state = NULL;
  2048. goto error;
  2049. }
  2050. i915_add_request_no_flush(req);
  2051. }
  2052. ctx->rcs_initialized = true;
  2053. }
  2054. return 0;
  2055. error:
  2056. if (is_global_default_ctx)
  2057. intel_unpin_ringbuffer_obj(ringbuf);
  2058. error_destroy_rbuf:
  2059. intel_destroy_ringbuffer_obj(ringbuf);
  2060. error_free_rbuf:
  2061. kfree(ringbuf);
  2062. error_unpin_ctx:
  2063. if (is_global_default_ctx)
  2064. i915_gem_object_ggtt_unpin(ctx_obj);
  2065. drm_gem_object_unreference(&ctx_obj->base);
  2066. return ret;
  2067. }
  2068. void intel_lr_context_reset(struct drm_device *dev,
  2069. struct intel_context *ctx)
  2070. {
  2071. struct drm_i915_private *dev_priv = dev->dev_private;
  2072. struct intel_engine_cs *ring;
  2073. int i;
  2074. for_each_ring(ring, dev_priv, i) {
  2075. struct drm_i915_gem_object *ctx_obj =
  2076. ctx->engine[ring->id].state;
  2077. struct intel_ringbuffer *ringbuf =
  2078. ctx->engine[ring->id].ringbuf;
  2079. uint32_t *reg_state;
  2080. struct page *page;
  2081. if (!ctx_obj)
  2082. continue;
  2083. if (i915_gem_object_get_pages(ctx_obj)) {
  2084. WARN(1, "Failed get_pages for context obj\n");
  2085. continue;
  2086. }
  2087. page = i915_gem_object_get_page(ctx_obj, 1);
  2088. reg_state = kmap_atomic(page);
  2089. reg_state[CTX_RING_HEAD+1] = 0;
  2090. reg_state[CTX_RING_TAIL+1] = 0;
  2091. kunmap_atomic(reg_state);
  2092. ringbuf->head = 0;
  2093. ringbuf->tail = 0;
  2094. }
  2095. }