intel_lrc.c 79 KB

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