intel_lrc.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853
  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 <linux/interrupt.h>
  134. #include <drm/drmP.h>
  135. #include <drm/i915_drm.h>
  136. #include "i915_drv.h"
  137. #include "i915_gem_render_state.h"
  138. #include "i915_vgpu.h"
  139. #include "intel_lrc_reg.h"
  140. #include "intel_mocs.h"
  141. #include "intel_workarounds.h"
  142. #define RING_EXECLIST_QFULL (1 << 0x2)
  143. #define RING_EXECLIST1_VALID (1 << 0x3)
  144. #define RING_EXECLIST0_VALID (1 << 0x4)
  145. #define RING_EXECLIST_ACTIVE_STATUS (3 << 0xE)
  146. #define RING_EXECLIST1_ACTIVE (1 << 0x11)
  147. #define RING_EXECLIST0_ACTIVE (1 << 0x12)
  148. #define GEN8_CTX_STATUS_IDLE_ACTIVE (1 << 0)
  149. #define GEN8_CTX_STATUS_PREEMPTED (1 << 1)
  150. #define GEN8_CTX_STATUS_ELEMENT_SWITCH (1 << 2)
  151. #define GEN8_CTX_STATUS_ACTIVE_IDLE (1 << 3)
  152. #define GEN8_CTX_STATUS_COMPLETE (1 << 4)
  153. #define GEN8_CTX_STATUS_LITE_RESTORE (1 << 15)
  154. #define GEN8_CTX_STATUS_COMPLETED_MASK \
  155. (GEN8_CTX_STATUS_COMPLETE | GEN8_CTX_STATUS_PREEMPTED)
  156. /* Typical size of the average request (2 pipecontrols and a MI_BB) */
  157. #define EXECLISTS_REQUEST_SIZE 64 /* bytes */
  158. #define WA_TAIL_DWORDS 2
  159. #define WA_TAIL_BYTES (sizeof(u32) * WA_TAIL_DWORDS)
  160. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  161. struct intel_engine_cs *engine,
  162. struct intel_context *ce);
  163. static void execlists_init_reg_state(u32 *reg_state,
  164. struct i915_gem_context *ctx,
  165. struct intel_engine_cs *engine,
  166. struct intel_ring *ring);
  167. static inline struct i915_priolist *to_priolist(struct rb_node *rb)
  168. {
  169. return rb_entry(rb, struct i915_priolist, node);
  170. }
  171. static inline int rq_prio(const struct i915_request *rq)
  172. {
  173. return rq->sched.attr.priority;
  174. }
  175. static inline bool need_preempt(const struct intel_engine_cs *engine,
  176. const struct i915_request *last,
  177. int prio)
  178. {
  179. return (intel_engine_has_preemption(engine) &&
  180. __execlists_need_preempt(prio, rq_prio(last)) &&
  181. !i915_request_completed(last));
  182. }
  183. /*
  184. * The context descriptor encodes various attributes of a context,
  185. * including its GTT address and some flags. Because it's fairly
  186. * expensive to calculate, we'll just do it once and cache the result,
  187. * which remains valid until the context is unpinned.
  188. *
  189. * This is what a descriptor looks like, from LSB to MSB::
  190. *
  191. * bits 0-11: flags, GEN8_CTX_* (cached in ctx->desc_template)
  192. * bits 12-31: LRCA, GTT address of (the HWSP of) this context
  193. * bits 32-52: ctx ID, a globally unique tag (highest bit used by GuC)
  194. * bits 53-54: mbz, reserved for use by hardware
  195. * bits 55-63: group ID, currently unused and set to 0
  196. *
  197. * Starting from Gen11, the upper dword of the descriptor has a new format:
  198. *
  199. * bits 32-36: reserved
  200. * bits 37-47: SW context ID
  201. * bits 48:53: engine instance
  202. * bit 54: mbz, reserved for use by hardware
  203. * bits 55-60: SW counter
  204. * bits 61-63: engine class
  205. *
  206. * engine info, SW context ID and SW counter need to form a unique number
  207. * (Context ID) per lrc.
  208. */
  209. static void
  210. intel_lr_context_descriptor_update(struct i915_gem_context *ctx,
  211. struct intel_engine_cs *engine,
  212. struct intel_context *ce)
  213. {
  214. u64 desc;
  215. BUILD_BUG_ON(MAX_CONTEXT_HW_ID > (BIT(GEN8_CTX_ID_WIDTH)));
  216. BUILD_BUG_ON(GEN11_MAX_CONTEXT_HW_ID > (BIT(GEN11_SW_CTX_ID_WIDTH)));
  217. desc = ctx->desc_template; /* bits 0-11 */
  218. GEM_BUG_ON(desc & GENMASK_ULL(63, 12));
  219. desc |= i915_ggtt_offset(ce->state) + LRC_HEADER_PAGES * PAGE_SIZE;
  220. /* bits 12-31 */
  221. GEM_BUG_ON(desc & GENMASK_ULL(63, 32));
  222. /*
  223. * The following 32bits are copied into the OA reports (dword 2).
  224. * Consider updating oa_get_render_ctx_id in i915_perf.c when changing
  225. * anything below.
  226. */
  227. if (INTEL_GEN(ctx->i915) >= 11) {
  228. GEM_BUG_ON(ctx->hw_id >= BIT(GEN11_SW_CTX_ID_WIDTH));
  229. desc |= (u64)ctx->hw_id << GEN11_SW_CTX_ID_SHIFT;
  230. /* bits 37-47 */
  231. desc |= (u64)engine->instance << GEN11_ENGINE_INSTANCE_SHIFT;
  232. /* bits 48-53 */
  233. /* TODO: decide what to do with SW counter (bits 55-60) */
  234. desc |= (u64)engine->class << GEN11_ENGINE_CLASS_SHIFT;
  235. /* bits 61-63 */
  236. } else {
  237. GEM_BUG_ON(ctx->hw_id >= BIT(GEN8_CTX_ID_WIDTH));
  238. desc |= (u64)ctx->hw_id << GEN8_CTX_ID_SHIFT; /* bits 32-52 */
  239. }
  240. ce->lrc_desc = desc;
  241. }
  242. static struct i915_priolist *
  243. lookup_priolist(struct intel_engine_cs *engine, int prio)
  244. {
  245. struct intel_engine_execlists * const execlists = &engine->execlists;
  246. struct i915_priolist *p;
  247. struct rb_node **parent, *rb;
  248. bool first = true;
  249. if (unlikely(execlists->no_priolist))
  250. prio = I915_PRIORITY_NORMAL;
  251. find_priolist:
  252. /* most positive priority is scheduled first, equal priorities fifo */
  253. rb = NULL;
  254. parent = &execlists->queue.rb_root.rb_node;
  255. while (*parent) {
  256. rb = *parent;
  257. p = to_priolist(rb);
  258. if (prio > p->priority) {
  259. parent = &rb->rb_left;
  260. } else if (prio < p->priority) {
  261. parent = &rb->rb_right;
  262. first = false;
  263. } else {
  264. return p;
  265. }
  266. }
  267. if (prio == I915_PRIORITY_NORMAL) {
  268. p = &execlists->default_priolist;
  269. } else {
  270. p = kmem_cache_alloc(engine->i915->priorities, GFP_ATOMIC);
  271. /* Convert an allocation failure to a priority bump */
  272. if (unlikely(!p)) {
  273. prio = I915_PRIORITY_NORMAL; /* recurses just once */
  274. /* To maintain ordering with all rendering, after an
  275. * allocation failure we have to disable all scheduling.
  276. * Requests will then be executed in fifo, and schedule
  277. * will ensure that dependencies are emitted in fifo.
  278. * There will be still some reordering with existing
  279. * requests, so if userspace lied about their
  280. * dependencies that reordering may be visible.
  281. */
  282. execlists->no_priolist = true;
  283. goto find_priolist;
  284. }
  285. }
  286. p->priority = prio;
  287. INIT_LIST_HEAD(&p->requests);
  288. rb_link_node(&p->node, rb, parent);
  289. rb_insert_color_cached(&p->node, &execlists->queue, first);
  290. return p;
  291. }
  292. static void unwind_wa_tail(struct i915_request *rq)
  293. {
  294. rq->tail = intel_ring_wrap(rq->ring, rq->wa_tail - WA_TAIL_BYTES);
  295. assert_ring_tail_valid(rq->ring, rq->tail);
  296. }
  297. static void __unwind_incomplete_requests(struct intel_engine_cs *engine)
  298. {
  299. struct i915_request *rq, *rn;
  300. struct i915_priolist *uninitialized_var(p);
  301. int last_prio = I915_PRIORITY_INVALID;
  302. lockdep_assert_held(&engine->timeline.lock);
  303. list_for_each_entry_safe_reverse(rq, rn,
  304. &engine->timeline.requests,
  305. link) {
  306. if (i915_request_completed(rq))
  307. return;
  308. __i915_request_unsubmit(rq);
  309. unwind_wa_tail(rq);
  310. GEM_BUG_ON(rq_prio(rq) == I915_PRIORITY_INVALID);
  311. if (rq_prio(rq) != last_prio) {
  312. last_prio = rq_prio(rq);
  313. p = lookup_priolist(engine, last_prio);
  314. }
  315. GEM_BUG_ON(p->priority != rq_prio(rq));
  316. list_add(&rq->sched.link, &p->requests);
  317. }
  318. }
  319. void
  320. execlists_unwind_incomplete_requests(struct intel_engine_execlists *execlists)
  321. {
  322. struct intel_engine_cs *engine =
  323. container_of(execlists, typeof(*engine), execlists);
  324. unsigned long flags;
  325. spin_lock_irqsave(&engine->timeline.lock, flags);
  326. __unwind_incomplete_requests(engine);
  327. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  328. }
  329. static inline void
  330. execlists_context_status_change(struct i915_request *rq, unsigned long status)
  331. {
  332. /*
  333. * Only used when GVT-g is enabled now. When GVT-g is disabled,
  334. * The compiler should eliminate this function as dead-code.
  335. */
  336. if (!IS_ENABLED(CONFIG_DRM_I915_GVT))
  337. return;
  338. atomic_notifier_call_chain(&rq->engine->context_status_notifier,
  339. status, rq);
  340. }
  341. inline void
  342. execlists_user_begin(struct intel_engine_execlists *execlists,
  343. const struct execlist_port *port)
  344. {
  345. execlists_set_active_once(execlists, EXECLISTS_ACTIVE_USER);
  346. }
  347. inline void
  348. execlists_user_end(struct intel_engine_execlists *execlists)
  349. {
  350. execlists_clear_active(execlists, EXECLISTS_ACTIVE_USER);
  351. }
  352. static inline void
  353. execlists_context_schedule_in(struct i915_request *rq)
  354. {
  355. execlists_context_status_change(rq, INTEL_CONTEXT_SCHEDULE_IN);
  356. intel_engine_context_in(rq->engine);
  357. }
  358. static inline void
  359. execlists_context_schedule_out(struct i915_request *rq, unsigned long status)
  360. {
  361. intel_engine_context_out(rq->engine);
  362. execlists_context_status_change(rq, status);
  363. trace_i915_request_out(rq);
  364. }
  365. static void
  366. execlists_update_context_pdps(struct i915_hw_ppgtt *ppgtt, u32 *reg_state)
  367. {
  368. ASSIGN_CTX_PDP(ppgtt, reg_state, 3);
  369. ASSIGN_CTX_PDP(ppgtt, reg_state, 2);
  370. ASSIGN_CTX_PDP(ppgtt, reg_state, 1);
  371. ASSIGN_CTX_PDP(ppgtt, reg_state, 0);
  372. }
  373. static u64 execlists_update_context(struct i915_request *rq)
  374. {
  375. struct intel_context *ce = rq->hw_context;
  376. struct i915_hw_ppgtt *ppgtt =
  377. rq->gem_context->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
  378. u32 *reg_state = ce->lrc_reg_state;
  379. reg_state[CTX_RING_TAIL+1] = intel_ring_set_tail(rq->ring, rq->tail);
  380. /*
  381. * True 32b PPGTT with dynamic page allocation: update PDP
  382. * registers and point the unallocated PDPs to scratch page.
  383. * PML4 is allocated during ppgtt init, so this is not needed
  384. * in 48-bit mode.
  385. */
  386. if (ppgtt && !i915_vm_is_48bit(&ppgtt->vm))
  387. execlists_update_context_pdps(ppgtt, reg_state);
  388. /*
  389. * Make sure the context image is complete before we submit it to HW.
  390. *
  391. * Ostensibly, writes (including the WCB) should be flushed prior to
  392. * an uncached write such as our mmio register access, the empirical
  393. * evidence (esp. on Braswell) suggests that the WC write into memory
  394. * may not be visible to the HW prior to the completion of the UC
  395. * register write and that we may begin execution from the context
  396. * before its image is complete leading to invalid PD chasing.
  397. *
  398. * Furthermore, Braswell, at least, wants a full mb to be sure that
  399. * the writes are coherent in memory (visible to the GPU) prior to
  400. * execution, and not just visible to other CPUs (as is the result of
  401. * wmb).
  402. */
  403. mb();
  404. return ce->lrc_desc;
  405. }
  406. static inline void write_desc(struct intel_engine_execlists *execlists, u64 desc, u32 port)
  407. {
  408. if (execlists->ctrl_reg) {
  409. writel(lower_32_bits(desc), execlists->submit_reg + port * 2);
  410. writel(upper_32_bits(desc), execlists->submit_reg + port * 2 + 1);
  411. } else {
  412. writel(upper_32_bits(desc), execlists->submit_reg);
  413. writel(lower_32_bits(desc), execlists->submit_reg);
  414. }
  415. }
  416. static void execlists_submit_ports(struct intel_engine_cs *engine)
  417. {
  418. struct intel_engine_execlists *execlists = &engine->execlists;
  419. struct execlist_port *port = execlists->port;
  420. unsigned int n;
  421. /*
  422. * We can skip acquiring intel_runtime_pm_get() here as it was taken
  423. * on our behalf by the request (see i915_gem_mark_busy()) and it will
  424. * not be relinquished until the device is idle (see
  425. * i915_gem_idle_work_handler()). As a precaution, we make sure
  426. * that all ELSP are drained i.e. we have processed the CSB,
  427. * before allowing ourselves to idle and calling intel_runtime_pm_put().
  428. */
  429. GEM_BUG_ON(!engine->i915->gt.awake);
  430. /*
  431. * ELSQ note: the submit queue is not cleared after being submitted
  432. * to the HW so we need to make sure we always clean it up. This is
  433. * currently ensured by the fact that we always write the same number
  434. * of elsq entries, keep this in mind before changing the loop below.
  435. */
  436. for (n = execlists_num_ports(execlists); n--; ) {
  437. struct i915_request *rq;
  438. unsigned int count;
  439. u64 desc;
  440. rq = port_unpack(&port[n], &count);
  441. if (rq) {
  442. GEM_BUG_ON(count > !n);
  443. if (!count++)
  444. execlists_context_schedule_in(rq);
  445. port_set(&port[n], port_pack(rq, count));
  446. desc = execlists_update_context(rq);
  447. GEM_DEBUG_EXEC(port[n].context_id = upper_32_bits(desc));
  448. GEM_TRACE("%s in[%d]: ctx=%d.%d, global=%d (fence %llx:%d) (current %d), prio=%d\n",
  449. engine->name, n,
  450. port[n].context_id, count,
  451. rq->global_seqno,
  452. rq->fence.context, rq->fence.seqno,
  453. intel_engine_get_seqno(engine),
  454. rq_prio(rq));
  455. } else {
  456. GEM_BUG_ON(!n);
  457. desc = 0;
  458. }
  459. write_desc(execlists, desc, n);
  460. }
  461. /* we need to manually load the submit queue */
  462. if (execlists->ctrl_reg)
  463. writel(EL_CTRL_LOAD, execlists->ctrl_reg);
  464. execlists_clear_active(execlists, EXECLISTS_ACTIVE_HWACK);
  465. }
  466. static bool ctx_single_port_submission(const struct intel_context *ce)
  467. {
  468. return (IS_ENABLED(CONFIG_DRM_I915_GVT) &&
  469. i915_gem_context_force_single_submission(ce->gem_context));
  470. }
  471. static bool can_merge_ctx(const struct intel_context *prev,
  472. const struct intel_context *next)
  473. {
  474. if (prev != next)
  475. return false;
  476. if (ctx_single_port_submission(prev))
  477. return false;
  478. return true;
  479. }
  480. static void port_assign(struct execlist_port *port, struct i915_request *rq)
  481. {
  482. GEM_BUG_ON(rq == port_request(port));
  483. if (port_isset(port))
  484. i915_request_put(port_request(port));
  485. port_set(port, port_pack(i915_request_get(rq), port_count(port)));
  486. }
  487. static void inject_preempt_context(struct intel_engine_cs *engine)
  488. {
  489. struct intel_engine_execlists *execlists = &engine->execlists;
  490. struct intel_context *ce =
  491. to_intel_context(engine->i915->preempt_context, engine);
  492. unsigned int n;
  493. GEM_BUG_ON(execlists->preempt_complete_status !=
  494. upper_32_bits(ce->lrc_desc));
  495. GEM_BUG_ON((ce->lrc_reg_state[CTX_CONTEXT_CONTROL + 1] &
  496. _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
  497. CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT)) !=
  498. _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
  499. CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT));
  500. /*
  501. * Switch to our empty preempt context so
  502. * the state of the GPU is known (idle).
  503. */
  504. GEM_TRACE("%s\n", engine->name);
  505. for (n = execlists_num_ports(execlists); --n; )
  506. write_desc(execlists, 0, n);
  507. write_desc(execlists, ce->lrc_desc, n);
  508. /* we need to manually load the submit queue */
  509. if (execlists->ctrl_reg)
  510. writel(EL_CTRL_LOAD, execlists->ctrl_reg);
  511. execlists_clear_active(execlists, EXECLISTS_ACTIVE_HWACK);
  512. execlists_set_active(execlists, EXECLISTS_ACTIVE_PREEMPT);
  513. }
  514. static void complete_preempt_context(struct intel_engine_execlists *execlists)
  515. {
  516. GEM_BUG_ON(!execlists_is_active(execlists, EXECLISTS_ACTIVE_PREEMPT));
  517. if (inject_preempt_hang(execlists))
  518. return;
  519. execlists_cancel_port_requests(execlists);
  520. __unwind_incomplete_requests(container_of(execlists,
  521. struct intel_engine_cs,
  522. execlists));
  523. }
  524. static void execlists_dequeue(struct intel_engine_cs *engine)
  525. {
  526. struct intel_engine_execlists * const execlists = &engine->execlists;
  527. struct execlist_port *port = execlists->port;
  528. const struct execlist_port * const last_port =
  529. &execlists->port[execlists->port_mask];
  530. struct i915_request *last = port_request(port);
  531. struct rb_node *rb;
  532. bool submit = false;
  533. /*
  534. * Hardware submission is through 2 ports. Conceptually each port
  535. * has a (RING_START, RING_HEAD, RING_TAIL) tuple. RING_START is
  536. * static for a context, and unique to each, so we only execute
  537. * requests belonging to a single context from each ring. RING_HEAD
  538. * is maintained by the CS in the context image, it marks the place
  539. * where it got up to last time, and through RING_TAIL we tell the CS
  540. * where we want to execute up to this time.
  541. *
  542. * In this list the requests are in order of execution. Consecutive
  543. * requests from the same context are adjacent in the ringbuffer. We
  544. * can combine these requests into a single RING_TAIL update:
  545. *
  546. * RING_HEAD...req1...req2
  547. * ^- RING_TAIL
  548. * since to execute req2 the CS must first execute req1.
  549. *
  550. * Our goal then is to point each port to the end of a consecutive
  551. * sequence of requests as being the most optimal (fewest wake ups
  552. * and context switches) submission.
  553. */
  554. if (last) {
  555. /*
  556. * Don't resubmit or switch until all outstanding
  557. * preemptions (lite-restore) are seen. Then we
  558. * know the next preemption status we see corresponds
  559. * to this ELSP update.
  560. */
  561. GEM_BUG_ON(!execlists_is_active(execlists,
  562. EXECLISTS_ACTIVE_USER));
  563. GEM_BUG_ON(!port_count(&port[0]));
  564. /*
  565. * If we write to ELSP a second time before the HW has had
  566. * a chance to respond to the previous write, we can confuse
  567. * the HW and hit "undefined behaviour". After writing to ELSP,
  568. * we must then wait until we see a context-switch event from
  569. * the HW to indicate that it has had a chance to respond.
  570. */
  571. if (!execlists_is_active(execlists, EXECLISTS_ACTIVE_HWACK))
  572. return;
  573. if (need_preempt(engine, last, execlists->queue_priority)) {
  574. inject_preempt_context(engine);
  575. return;
  576. }
  577. /*
  578. * In theory, we could coalesce more requests onto
  579. * the second port (the first port is active, with
  580. * no preemptions pending). However, that means we
  581. * then have to deal with the possible lite-restore
  582. * of the second port (as we submit the ELSP, there
  583. * may be a context-switch) but also we may complete
  584. * the resubmission before the context-switch. Ergo,
  585. * coalescing onto the second port will cause a
  586. * preemption event, but we cannot predict whether
  587. * that will affect port[0] or port[1].
  588. *
  589. * If the second port is already active, we can wait
  590. * until the next context-switch before contemplating
  591. * new requests. The GPU will be busy and we should be
  592. * able to resubmit the new ELSP before it idles,
  593. * avoiding pipeline bubbles (momentary pauses where
  594. * the driver is unable to keep up the supply of new
  595. * work). However, we have to double check that the
  596. * priorities of the ports haven't been switch.
  597. */
  598. if (port_count(&port[1]))
  599. return;
  600. /*
  601. * WaIdleLiteRestore:bdw,skl
  602. * Apply the wa NOOPs to prevent
  603. * ring:HEAD == rq:TAIL as we resubmit the
  604. * request. See gen8_emit_breadcrumb() for
  605. * where we prepare the padding after the
  606. * end of the request.
  607. */
  608. last->tail = last->wa_tail;
  609. }
  610. while ((rb = rb_first_cached(&execlists->queue))) {
  611. struct i915_priolist *p = to_priolist(rb);
  612. struct i915_request *rq, *rn;
  613. list_for_each_entry_safe(rq, rn, &p->requests, sched.link) {
  614. /*
  615. * Can we combine this request with the current port?
  616. * It has to be the same context/ringbuffer and not
  617. * have any exceptions (e.g. GVT saying never to
  618. * combine contexts).
  619. *
  620. * If we can combine the requests, we can execute both
  621. * by updating the RING_TAIL to point to the end of the
  622. * second request, and so we never need to tell the
  623. * hardware about the first.
  624. */
  625. if (last &&
  626. !can_merge_ctx(rq->hw_context, last->hw_context)) {
  627. /*
  628. * If we are on the second port and cannot
  629. * combine this request with the last, then we
  630. * are done.
  631. */
  632. if (port == last_port) {
  633. __list_del_many(&p->requests,
  634. &rq->sched.link);
  635. goto done;
  636. }
  637. /*
  638. * If GVT overrides us we only ever submit
  639. * port[0], leaving port[1] empty. Note that we
  640. * also have to be careful that we don't queue
  641. * the same context (even though a different
  642. * request) to the second port.
  643. */
  644. if (ctx_single_port_submission(last->hw_context) ||
  645. ctx_single_port_submission(rq->hw_context)) {
  646. __list_del_many(&p->requests,
  647. &rq->sched.link);
  648. goto done;
  649. }
  650. GEM_BUG_ON(last->hw_context == rq->hw_context);
  651. if (submit)
  652. port_assign(port, last);
  653. port++;
  654. GEM_BUG_ON(port_isset(port));
  655. }
  656. INIT_LIST_HEAD(&rq->sched.link);
  657. __i915_request_submit(rq);
  658. trace_i915_request_in(rq, port_index(port, execlists));
  659. last = rq;
  660. submit = true;
  661. }
  662. rb_erase_cached(&p->node, &execlists->queue);
  663. INIT_LIST_HEAD(&p->requests);
  664. if (p->priority != I915_PRIORITY_NORMAL)
  665. kmem_cache_free(engine->i915->priorities, p);
  666. }
  667. done:
  668. /*
  669. * Here be a bit of magic! Or sleight-of-hand, whichever you prefer.
  670. *
  671. * We choose queue_priority such that if we add a request of greater
  672. * priority than this, we kick the submission tasklet to decide on
  673. * the right order of submitting the requests to hardware. We must
  674. * also be prepared to reorder requests as they are in-flight on the
  675. * HW. We derive the queue_priority then as the first "hole" in
  676. * the HW submission ports and if there are no available slots,
  677. * the priority of the lowest executing request, i.e. last.
  678. *
  679. * When we do receive a higher priority request ready to run from the
  680. * user, see queue_request(), the queue_priority is bumped to that
  681. * request triggering preemption on the next dequeue (or subsequent
  682. * interrupt for secondary ports).
  683. */
  684. execlists->queue_priority =
  685. port != execlists->port ? rq_prio(last) : INT_MIN;
  686. if (submit) {
  687. port_assign(port, last);
  688. execlists_submit_ports(engine);
  689. }
  690. /* We must always keep the beast fed if we have work piled up */
  691. GEM_BUG_ON(rb_first_cached(&execlists->queue) &&
  692. !port_isset(execlists->port));
  693. /* Re-evaluate the executing context setup after each preemptive kick */
  694. if (last)
  695. execlists_user_begin(execlists, execlists->port);
  696. /* If the engine is now idle, so should be the flag; and vice versa. */
  697. GEM_BUG_ON(execlists_is_active(&engine->execlists,
  698. EXECLISTS_ACTIVE_USER) ==
  699. !port_isset(engine->execlists.port));
  700. }
  701. void
  702. execlists_cancel_port_requests(struct intel_engine_execlists * const execlists)
  703. {
  704. struct execlist_port *port = execlists->port;
  705. unsigned int num_ports = execlists_num_ports(execlists);
  706. while (num_ports-- && port_isset(port)) {
  707. struct i915_request *rq = port_request(port);
  708. GEM_TRACE("%s:port%u global=%d (fence %llx:%d), (current %d)\n",
  709. rq->engine->name,
  710. (unsigned int)(port - execlists->port),
  711. rq->global_seqno,
  712. rq->fence.context, rq->fence.seqno,
  713. intel_engine_get_seqno(rq->engine));
  714. GEM_BUG_ON(!execlists->active);
  715. execlists_context_schedule_out(rq,
  716. i915_request_completed(rq) ?
  717. INTEL_CONTEXT_SCHEDULE_OUT :
  718. INTEL_CONTEXT_SCHEDULE_PREEMPTED);
  719. i915_request_put(rq);
  720. memset(port, 0, sizeof(*port));
  721. port++;
  722. }
  723. execlists_clear_all_active(execlists);
  724. }
  725. static void reset_csb_pointers(struct intel_engine_execlists *execlists)
  726. {
  727. /*
  728. * After a reset, the HW starts writing into CSB entry [0]. We
  729. * therefore have to set our HEAD pointer back one entry so that
  730. * the *first* entry we check is entry 0. To complicate this further,
  731. * as we don't wait for the first interrupt after reset, we have to
  732. * fake the HW write to point back to the last entry so that our
  733. * inline comparison of our cached head position against the last HW
  734. * write works even before the first interrupt.
  735. */
  736. execlists->csb_head = execlists->csb_write_reset;
  737. WRITE_ONCE(*execlists->csb_write, execlists->csb_write_reset);
  738. }
  739. static void nop_submission_tasklet(unsigned long data)
  740. {
  741. /* The driver is wedged; don't process any more events. */
  742. }
  743. static void execlists_cancel_requests(struct intel_engine_cs *engine)
  744. {
  745. struct intel_engine_execlists * const execlists = &engine->execlists;
  746. struct i915_request *rq, *rn;
  747. struct rb_node *rb;
  748. unsigned long flags;
  749. GEM_TRACE("%s current %d\n",
  750. engine->name, intel_engine_get_seqno(engine));
  751. /*
  752. * Before we call engine->cancel_requests(), we should have exclusive
  753. * access to the submission state. This is arranged for us by the
  754. * caller disabling the interrupt generation, the tasklet and other
  755. * threads that may then access the same state, giving us a free hand
  756. * to reset state. However, we still need to let lockdep be aware that
  757. * we know this state may be accessed in hardirq context, so we
  758. * disable the irq around this manipulation and we want to keep
  759. * the spinlock focused on its duties and not accidentally conflate
  760. * coverage to the submission's irq state. (Similarly, although we
  761. * shouldn't need to disable irq around the manipulation of the
  762. * submission's irq state, we also wish to remind ourselves that
  763. * it is irq state.)
  764. */
  765. spin_lock_irqsave(&engine->timeline.lock, flags);
  766. /* Cancel the requests on the HW and clear the ELSP tracker. */
  767. execlists_cancel_port_requests(execlists);
  768. execlists_user_end(execlists);
  769. /* Mark all executing requests as skipped. */
  770. list_for_each_entry(rq, &engine->timeline.requests, link) {
  771. GEM_BUG_ON(!rq->global_seqno);
  772. if (!i915_request_completed(rq))
  773. dma_fence_set_error(&rq->fence, -EIO);
  774. }
  775. /* Flush the queued requests to the timeline list (for retiring). */
  776. while ((rb = rb_first_cached(&execlists->queue))) {
  777. struct i915_priolist *p = to_priolist(rb);
  778. list_for_each_entry_safe(rq, rn, &p->requests, sched.link) {
  779. INIT_LIST_HEAD(&rq->sched.link);
  780. dma_fence_set_error(&rq->fence, -EIO);
  781. __i915_request_submit(rq);
  782. }
  783. rb_erase_cached(&p->node, &execlists->queue);
  784. INIT_LIST_HEAD(&p->requests);
  785. if (p->priority != I915_PRIORITY_NORMAL)
  786. kmem_cache_free(engine->i915->priorities, p);
  787. }
  788. /* Remaining _unready_ requests will be nop'ed when submitted */
  789. execlists->queue_priority = INT_MIN;
  790. execlists->queue = RB_ROOT_CACHED;
  791. GEM_BUG_ON(port_isset(execlists->port));
  792. GEM_BUG_ON(__tasklet_is_enabled(&execlists->tasklet));
  793. execlists->tasklet.func = nop_submission_tasklet;
  794. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  795. }
  796. static inline bool
  797. reset_in_progress(const struct intel_engine_execlists *execlists)
  798. {
  799. return unlikely(!__tasklet_is_enabled(&execlists->tasklet));
  800. }
  801. static void process_csb(struct intel_engine_cs *engine)
  802. {
  803. struct intel_engine_execlists * const execlists = &engine->execlists;
  804. struct execlist_port *port = execlists->port;
  805. const u32 * const buf = execlists->csb_status;
  806. u8 head, tail;
  807. /*
  808. * Note that csb_write, csb_status may be either in HWSP or mmio.
  809. * When reading from the csb_write mmio register, we have to be
  810. * careful to only use the GEN8_CSB_WRITE_PTR portion, which is
  811. * the low 4bits. As it happens we know the next 4bits are always
  812. * zero and so we can simply masked off the low u8 of the register
  813. * and treat it identically to reading from the HWSP (without having
  814. * to use explicit shifting and masking, and probably bifurcating
  815. * the code to handle the legacy mmio read).
  816. */
  817. head = execlists->csb_head;
  818. tail = READ_ONCE(*execlists->csb_write);
  819. GEM_TRACE("%s cs-irq head=%d, tail=%d\n", engine->name, head, tail);
  820. if (unlikely(head == tail))
  821. return;
  822. /*
  823. * Hopefully paired with a wmb() in HW!
  824. *
  825. * We must complete the read of the write pointer before any reads
  826. * from the CSB, so that we do not see stale values. Without an rmb
  827. * (lfence) the HW may speculatively perform the CSB[] reads *before*
  828. * we perform the READ_ONCE(*csb_write).
  829. */
  830. rmb();
  831. do {
  832. struct i915_request *rq;
  833. unsigned int status;
  834. unsigned int count;
  835. if (++head == GEN8_CSB_ENTRIES)
  836. head = 0;
  837. /*
  838. * We are flying near dragons again.
  839. *
  840. * We hold a reference to the request in execlist_port[]
  841. * but no more than that. We are operating in softirq
  842. * context and so cannot hold any mutex or sleep. That
  843. * prevents us stopping the requests we are processing
  844. * in port[] from being retired simultaneously (the
  845. * breadcrumb will be complete before we see the
  846. * context-switch). As we only hold the reference to the
  847. * request, any pointer chasing underneath the request
  848. * is subject to a potential use-after-free. Thus we
  849. * store all of the bookkeeping within port[] as
  850. * required, and avoid using unguarded pointers beneath
  851. * request itself. The same applies to the atomic
  852. * status notifier.
  853. */
  854. GEM_TRACE("%s csb[%d]: status=0x%08x:0x%08x, active=0x%x\n",
  855. engine->name, head,
  856. buf[2 * head + 0], buf[2 * head + 1],
  857. execlists->active);
  858. status = buf[2 * head];
  859. if (status & (GEN8_CTX_STATUS_IDLE_ACTIVE |
  860. GEN8_CTX_STATUS_PREEMPTED))
  861. execlists_set_active(execlists,
  862. EXECLISTS_ACTIVE_HWACK);
  863. if (status & GEN8_CTX_STATUS_ACTIVE_IDLE)
  864. execlists_clear_active(execlists,
  865. EXECLISTS_ACTIVE_HWACK);
  866. if (!(status & GEN8_CTX_STATUS_COMPLETED_MASK))
  867. continue;
  868. /* We should never get a COMPLETED | IDLE_ACTIVE! */
  869. GEM_BUG_ON(status & GEN8_CTX_STATUS_IDLE_ACTIVE);
  870. if (status & GEN8_CTX_STATUS_COMPLETE &&
  871. buf[2*head + 1] == execlists->preempt_complete_status) {
  872. GEM_TRACE("%s preempt-idle\n", engine->name);
  873. complete_preempt_context(execlists);
  874. continue;
  875. }
  876. if (status & GEN8_CTX_STATUS_PREEMPTED &&
  877. execlists_is_active(execlists,
  878. EXECLISTS_ACTIVE_PREEMPT))
  879. continue;
  880. GEM_BUG_ON(!execlists_is_active(execlists,
  881. EXECLISTS_ACTIVE_USER));
  882. rq = port_unpack(port, &count);
  883. GEM_TRACE("%s out[0]: ctx=%d.%d, global=%d (fence %llx:%d) (current %d), prio=%d\n",
  884. engine->name,
  885. port->context_id, count,
  886. rq ? rq->global_seqno : 0,
  887. rq ? rq->fence.context : 0,
  888. rq ? rq->fence.seqno : 0,
  889. intel_engine_get_seqno(engine),
  890. rq ? rq_prio(rq) : 0);
  891. /* Check the context/desc id for this event matches */
  892. GEM_DEBUG_BUG_ON(buf[2 * head + 1] != port->context_id);
  893. GEM_BUG_ON(count == 0);
  894. if (--count == 0) {
  895. /*
  896. * On the final event corresponding to the
  897. * submission of this context, we expect either
  898. * an element-switch event or a completion
  899. * event (and on completion, the active-idle
  900. * marker). No more preemptions, lite-restore
  901. * or otherwise.
  902. */
  903. GEM_BUG_ON(status & GEN8_CTX_STATUS_PREEMPTED);
  904. GEM_BUG_ON(port_isset(&port[1]) &&
  905. !(status & GEN8_CTX_STATUS_ELEMENT_SWITCH));
  906. GEM_BUG_ON(!port_isset(&port[1]) &&
  907. !(status & GEN8_CTX_STATUS_ACTIVE_IDLE));
  908. /*
  909. * We rely on the hardware being strongly
  910. * ordered, that the breadcrumb write is
  911. * coherent (visible from the CPU) before the
  912. * user interrupt and CSB is processed.
  913. */
  914. GEM_BUG_ON(!i915_request_completed(rq));
  915. execlists_context_schedule_out(rq,
  916. INTEL_CONTEXT_SCHEDULE_OUT);
  917. i915_request_put(rq);
  918. GEM_TRACE("%s completed ctx=%d\n",
  919. engine->name, port->context_id);
  920. port = execlists_port_complete(execlists, port);
  921. if (port_isset(port))
  922. execlists_user_begin(execlists, port);
  923. else
  924. execlists_user_end(execlists);
  925. } else {
  926. port_set(port, port_pack(rq, count));
  927. }
  928. } while (head != tail);
  929. execlists->csb_head = head;
  930. }
  931. static void __execlists_submission_tasklet(struct intel_engine_cs *const engine)
  932. {
  933. lockdep_assert_held(&engine->timeline.lock);
  934. process_csb(engine);
  935. if (!execlists_is_active(&engine->execlists, EXECLISTS_ACTIVE_PREEMPT))
  936. execlists_dequeue(engine);
  937. }
  938. /*
  939. * Check the unread Context Status Buffers and manage the submission of new
  940. * contexts to the ELSP accordingly.
  941. */
  942. static void execlists_submission_tasklet(unsigned long data)
  943. {
  944. struct intel_engine_cs * const engine = (struct intel_engine_cs *)data;
  945. unsigned long flags;
  946. GEM_TRACE("%s awake?=%d, active=%x\n",
  947. engine->name,
  948. engine->i915->gt.awake,
  949. engine->execlists.active);
  950. spin_lock_irqsave(&engine->timeline.lock, flags);
  951. __execlists_submission_tasklet(engine);
  952. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  953. }
  954. static void queue_request(struct intel_engine_cs *engine,
  955. struct i915_sched_node *node,
  956. int prio)
  957. {
  958. list_add_tail(&node->link,
  959. &lookup_priolist(engine, prio)->requests);
  960. }
  961. static void __update_queue(struct intel_engine_cs *engine, int prio)
  962. {
  963. engine->execlists.queue_priority = prio;
  964. }
  965. static void __submit_queue_imm(struct intel_engine_cs *engine)
  966. {
  967. struct intel_engine_execlists * const execlists = &engine->execlists;
  968. if (reset_in_progress(execlists))
  969. return; /* defer until we restart the engine following reset */
  970. if (execlists->tasklet.func == execlists_submission_tasklet)
  971. __execlists_submission_tasklet(engine);
  972. else
  973. tasklet_hi_schedule(&execlists->tasklet);
  974. }
  975. static void submit_queue(struct intel_engine_cs *engine, int prio)
  976. {
  977. if (prio > engine->execlists.queue_priority) {
  978. __update_queue(engine, prio);
  979. __submit_queue_imm(engine);
  980. }
  981. }
  982. static void execlists_submit_request(struct i915_request *request)
  983. {
  984. struct intel_engine_cs *engine = request->engine;
  985. unsigned long flags;
  986. /* Will be called from irq-context when using foreign fences. */
  987. spin_lock_irqsave(&engine->timeline.lock, flags);
  988. queue_request(engine, &request->sched, rq_prio(request));
  989. GEM_BUG_ON(RB_EMPTY_ROOT(&engine->execlists.queue.rb_root));
  990. GEM_BUG_ON(list_empty(&request->sched.link));
  991. submit_queue(engine, rq_prio(request));
  992. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  993. }
  994. static struct i915_request *sched_to_request(struct i915_sched_node *node)
  995. {
  996. return container_of(node, struct i915_request, sched);
  997. }
  998. static struct intel_engine_cs *
  999. sched_lock_engine(struct i915_sched_node *node, struct intel_engine_cs *locked)
  1000. {
  1001. struct intel_engine_cs *engine = sched_to_request(node)->engine;
  1002. GEM_BUG_ON(!locked);
  1003. if (engine != locked) {
  1004. spin_unlock(&locked->timeline.lock);
  1005. spin_lock(&engine->timeline.lock);
  1006. }
  1007. return engine;
  1008. }
  1009. static void execlists_schedule(struct i915_request *request,
  1010. const struct i915_sched_attr *attr)
  1011. {
  1012. struct i915_priolist *uninitialized_var(pl);
  1013. struct intel_engine_cs *engine, *last;
  1014. struct i915_dependency *dep, *p;
  1015. struct i915_dependency stack;
  1016. const int prio = attr->priority;
  1017. LIST_HEAD(dfs);
  1018. GEM_BUG_ON(prio == I915_PRIORITY_INVALID);
  1019. if (i915_request_completed(request))
  1020. return;
  1021. if (prio <= READ_ONCE(request->sched.attr.priority))
  1022. return;
  1023. /* Need BKL in order to use the temporary link inside i915_dependency */
  1024. lockdep_assert_held(&request->i915->drm.struct_mutex);
  1025. stack.signaler = &request->sched;
  1026. list_add(&stack.dfs_link, &dfs);
  1027. /*
  1028. * Recursively bump all dependent priorities to match the new request.
  1029. *
  1030. * A naive approach would be to use recursion:
  1031. * static void update_priorities(struct i915_sched_node *node, prio) {
  1032. * list_for_each_entry(dep, &node->signalers_list, signal_link)
  1033. * update_priorities(dep->signal, prio)
  1034. * queue_request(node);
  1035. * }
  1036. * but that may have unlimited recursion depth and so runs a very
  1037. * real risk of overunning the kernel stack. Instead, we build
  1038. * a flat list of all dependencies starting with the current request.
  1039. * As we walk the list of dependencies, we add all of its dependencies
  1040. * to the end of the list (this may include an already visited
  1041. * request) and continue to walk onwards onto the new dependencies. The
  1042. * end result is a topological list of requests in reverse order, the
  1043. * last element in the list is the request we must execute first.
  1044. */
  1045. list_for_each_entry(dep, &dfs, dfs_link) {
  1046. struct i915_sched_node *node = dep->signaler;
  1047. /*
  1048. * Within an engine, there can be no cycle, but we may
  1049. * refer to the same dependency chain multiple times
  1050. * (redundant dependencies are not eliminated) and across
  1051. * engines.
  1052. */
  1053. list_for_each_entry(p, &node->signalers_list, signal_link) {
  1054. GEM_BUG_ON(p == dep); /* no cycles! */
  1055. if (i915_sched_node_signaled(p->signaler))
  1056. continue;
  1057. GEM_BUG_ON(p->signaler->attr.priority < node->attr.priority);
  1058. if (prio > READ_ONCE(p->signaler->attr.priority))
  1059. list_move_tail(&p->dfs_link, &dfs);
  1060. }
  1061. }
  1062. /*
  1063. * If we didn't need to bump any existing priorities, and we haven't
  1064. * yet submitted this request (i.e. there is no potential race with
  1065. * execlists_submit_request()), we can set our own priority and skip
  1066. * acquiring the engine locks.
  1067. */
  1068. if (request->sched.attr.priority == I915_PRIORITY_INVALID) {
  1069. GEM_BUG_ON(!list_empty(&request->sched.link));
  1070. request->sched.attr = *attr;
  1071. if (stack.dfs_link.next == stack.dfs_link.prev)
  1072. return;
  1073. __list_del_entry(&stack.dfs_link);
  1074. }
  1075. last = NULL;
  1076. engine = request->engine;
  1077. spin_lock_irq(&engine->timeline.lock);
  1078. /* Fifo and depth-first replacement ensure our deps execute before us */
  1079. list_for_each_entry_safe_reverse(dep, p, &dfs, dfs_link) {
  1080. struct i915_sched_node *node = dep->signaler;
  1081. INIT_LIST_HEAD(&dep->dfs_link);
  1082. engine = sched_lock_engine(node, engine);
  1083. if (prio <= node->attr.priority)
  1084. continue;
  1085. node->attr.priority = prio;
  1086. if (!list_empty(&node->link)) {
  1087. if (last != engine) {
  1088. pl = lookup_priolist(engine, prio);
  1089. last = engine;
  1090. }
  1091. GEM_BUG_ON(pl->priority != prio);
  1092. list_move_tail(&node->link, &pl->requests);
  1093. }
  1094. if (prio > engine->execlists.queue_priority &&
  1095. i915_sw_fence_done(&sched_to_request(node)->submit)) {
  1096. /* defer submission until after all of our updates */
  1097. __update_queue(engine, prio);
  1098. tasklet_hi_schedule(&engine->execlists.tasklet);
  1099. }
  1100. }
  1101. spin_unlock_irq(&engine->timeline.lock);
  1102. }
  1103. static void execlists_context_destroy(struct intel_context *ce)
  1104. {
  1105. GEM_BUG_ON(ce->pin_count);
  1106. if (!ce->state)
  1107. return;
  1108. intel_ring_free(ce->ring);
  1109. GEM_BUG_ON(i915_gem_object_is_active(ce->state->obj));
  1110. i915_gem_object_put(ce->state->obj);
  1111. }
  1112. static void execlists_context_unpin(struct intel_context *ce)
  1113. {
  1114. intel_ring_unpin(ce->ring);
  1115. ce->state->obj->pin_global--;
  1116. i915_gem_object_unpin_map(ce->state->obj);
  1117. i915_vma_unpin(ce->state);
  1118. i915_gem_context_put(ce->gem_context);
  1119. }
  1120. static int __context_pin(struct i915_gem_context *ctx, struct i915_vma *vma)
  1121. {
  1122. unsigned int flags;
  1123. int err;
  1124. /*
  1125. * Clear this page out of any CPU caches for coherent swap-in/out.
  1126. * We only want to do this on the first bind so that we do not stall
  1127. * on an active context (which by nature is already on the GPU).
  1128. */
  1129. if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
  1130. err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
  1131. if (err)
  1132. return err;
  1133. }
  1134. flags = PIN_GLOBAL | PIN_HIGH;
  1135. if (ctx->ggtt_offset_bias)
  1136. flags |= PIN_OFFSET_BIAS | ctx->ggtt_offset_bias;
  1137. return i915_vma_pin(vma, 0, GEN8_LR_CONTEXT_ALIGN, flags);
  1138. }
  1139. static struct intel_context *
  1140. __execlists_context_pin(struct intel_engine_cs *engine,
  1141. struct i915_gem_context *ctx,
  1142. struct intel_context *ce)
  1143. {
  1144. void *vaddr;
  1145. int ret;
  1146. ret = execlists_context_deferred_alloc(ctx, engine, ce);
  1147. if (ret)
  1148. goto err;
  1149. GEM_BUG_ON(!ce->state);
  1150. ret = __context_pin(ctx, ce->state);
  1151. if (ret)
  1152. goto err;
  1153. vaddr = i915_gem_object_pin_map(ce->state->obj, I915_MAP_WB);
  1154. if (IS_ERR(vaddr)) {
  1155. ret = PTR_ERR(vaddr);
  1156. goto unpin_vma;
  1157. }
  1158. ret = intel_ring_pin(ce->ring, ctx->i915, ctx->ggtt_offset_bias);
  1159. if (ret)
  1160. goto unpin_map;
  1161. intel_lr_context_descriptor_update(ctx, engine, ce);
  1162. ce->lrc_reg_state = vaddr + LRC_STATE_PN * PAGE_SIZE;
  1163. ce->lrc_reg_state[CTX_RING_BUFFER_START+1] =
  1164. i915_ggtt_offset(ce->ring->vma);
  1165. GEM_BUG_ON(!intel_ring_offset_valid(ce->ring, ce->ring->head));
  1166. ce->lrc_reg_state[CTX_RING_HEAD+1] = ce->ring->head;
  1167. ce->state->obj->pin_global++;
  1168. i915_gem_context_get(ctx);
  1169. return ce;
  1170. unpin_map:
  1171. i915_gem_object_unpin_map(ce->state->obj);
  1172. unpin_vma:
  1173. __i915_vma_unpin(ce->state);
  1174. err:
  1175. ce->pin_count = 0;
  1176. return ERR_PTR(ret);
  1177. }
  1178. static const struct intel_context_ops execlists_context_ops = {
  1179. .unpin = execlists_context_unpin,
  1180. .destroy = execlists_context_destroy,
  1181. };
  1182. static struct intel_context *
  1183. execlists_context_pin(struct intel_engine_cs *engine,
  1184. struct i915_gem_context *ctx)
  1185. {
  1186. struct intel_context *ce = to_intel_context(ctx, engine);
  1187. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  1188. if (likely(ce->pin_count++))
  1189. return ce;
  1190. GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
  1191. ce->ops = &execlists_context_ops;
  1192. return __execlists_context_pin(engine, ctx, ce);
  1193. }
  1194. static int execlists_request_alloc(struct i915_request *request)
  1195. {
  1196. int ret;
  1197. GEM_BUG_ON(!request->hw_context->pin_count);
  1198. /* Flush enough space to reduce the likelihood of waiting after
  1199. * we start building the request - in which case we will just
  1200. * have to repeat work.
  1201. */
  1202. request->reserved_space += EXECLISTS_REQUEST_SIZE;
  1203. ret = intel_ring_wait_for_space(request->ring, request->reserved_space);
  1204. if (ret)
  1205. return ret;
  1206. /* Note that after this point, we have committed to using
  1207. * this request as it is being used to both track the
  1208. * state of engine initialisation and liveness of the
  1209. * golden renderstate above. Think twice before you try
  1210. * to cancel/unwind this request now.
  1211. */
  1212. request->reserved_space -= EXECLISTS_REQUEST_SIZE;
  1213. return 0;
  1214. }
  1215. /*
  1216. * In this WA we need to set GEN8_L3SQCREG4[21:21] and reset it after
  1217. * PIPE_CONTROL instruction. This is required for the flush to happen correctly
  1218. * but there is a slight complication as this is applied in WA batch where the
  1219. * values are only initialized once so we cannot take register value at the
  1220. * beginning and reuse it further; hence we save its value to memory, upload a
  1221. * constant value with bit21 set and then we restore it back with the saved value.
  1222. * To simplify the WA, a constant value is formed by using the default value
  1223. * of this register. This shouldn't be a problem because we are only modifying
  1224. * it for a short period and this batch in non-premptible. We can ofcourse
  1225. * use additional instructions that read the actual value of the register
  1226. * at that time and set our bit of interest but it makes the WA complicated.
  1227. *
  1228. * This WA is also required for Gen9 so extracting as a function avoids
  1229. * code duplication.
  1230. */
  1231. static u32 *
  1232. gen8_emit_flush_coherentl3_wa(struct intel_engine_cs *engine, u32 *batch)
  1233. {
  1234. *batch++ = MI_STORE_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
  1235. *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
  1236. *batch++ = i915_ggtt_offset(engine->scratch) + 256;
  1237. *batch++ = 0;
  1238. *batch++ = MI_LOAD_REGISTER_IMM(1);
  1239. *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
  1240. *batch++ = 0x40400000 | GEN8_LQSC_FLUSH_COHERENT_LINES;
  1241. batch = gen8_emit_pipe_control(batch,
  1242. PIPE_CONTROL_CS_STALL |
  1243. PIPE_CONTROL_DC_FLUSH_ENABLE,
  1244. 0);
  1245. *batch++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_SRM_LRM_GLOBAL_GTT;
  1246. *batch++ = i915_mmio_reg_offset(GEN8_L3SQCREG4);
  1247. *batch++ = i915_ggtt_offset(engine->scratch) + 256;
  1248. *batch++ = 0;
  1249. return batch;
  1250. }
  1251. /*
  1252. * Typically we only have one indirect_ctx and per_ctx batch buffer which are
  1253. * initialized at the beginning and shared across all contexts but this field
  1254. * helps us to have multiple batches at different offsets and select them based
  1255. * on a criteria. At the moment this batch always start at the beginning of the page
  1256. * and at this point we don't have multiple wa_ctx batch buffers.
  1257. *
  1258. * The number of WA applied are not known at the beginning; we use this field
  1259. * to return the no of DWORDS written.
  1260. *
  1261. * It is to be noted that this batch does not contain MI_BATCH_BUFFER_END
  1262. * so it adds NOOPs as padding to make it cacheline aligned.
  1263. * MI_BATCH_BUFFER_END will be added to perctx batch and both of them together
  1264. * makes a complete batch buffer.
  1265. */
  1266. static u32 *gen8_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
  1267. {
  1268. /* WaDisableCtxRestoreArbitration:bdw,chv */
  1269. *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
  1270. /* WaFlushCoherentL3CacheLinesAtContextSwitch:bdw */
  1271. if (IS_BROADWELL(engine->i915))
  1272. batch = gen8_emit_flush_coherentl3_wa(engine, batch);
  1273. /* WaClearSlmSpaceAtContextSwitch:bdw,chv */
  1274. /* Actual scratch location is at 128 bytes offset */
  1275. batch = gen8_emit_pipe_control(batch,
  1276. PIPE_CONTROL_FLUSH_L3 |
  1277. PIPE_CONTROL_GLOBAL_GTT_IVB |
  1278. PIPE_CONTROL_CS_STALL |
  1279. PIPE_CONTROL_QW_WRITE,
  1280. i915_ggtt_offset(engine->scratch) +
  1281. 2 * CACHELINE_BYTES);
  1282. *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  1283. /* Pad to end of cacheline */
  1284. while ((unsigned long)batch % CACHELINE_BYTES)
  1285. *batch++ = MI_NOOP;
  1286. /*
  1287. * MI_BATCH_BUFFER_END is not required in Indirect ctx BB because
  1288. * execution depends on the length specified in terms of cache lines
  1289. * in the register CTX_RCS_INDIRECT_CTX
  1290. */
  1291. return batch;
  1292. }
  1293. struct lri {
  1294. i915_reg_t reg;
  1295. u32 value;
  1296. };
  1297. static u32 *emit_lri(u32 *batch, const struct lri *lri, unsigned int count)
  1298. {
  1299. GEM_BUG_ON(!count || count > 63);
  1300. *batch++ = MI_LOAD_REGISTER_IMM(count);
  1301. do {
  1302. *batch++ = i915_mmio_reg_offset(lri->reg);
  1303. *batch++ = lri->value;
  1304. } while (lri++, --count);
  1305. *batch++ = MI_NOOP;
  1306. return batch;
  1307. }
  1308. static u32 *gen9_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
  1309. {
  1310. static const struct lri lri[] = {
  1311. /* WaDisableGatherAtSetShaderCommonSlice:skl,bxt,kbl,glk */
  1312. {
  1313. COMMON_SLICE_CHICKEN2,
  1314. __MASKED_FIELD(GEN9_DISABLE_GATHER_AT_SET_SHADER_COMMON_SLICE,
  1315. 0),
  1316. },
  1317. /* BSpec: 11391 */
  1318. {
  1319. FF_SLICE_CHICKEN,
  1320. __MASKED_FIELD(FF_SLICE_CHICKEN_CL_PROVOKING_VERTEX_FIX,
  1321. FF_SLICE_CHICKEN_CL_PROVOKING_VERTEX_FIX),
  1322. },
  1323. /* BSpec: 11299 */
  1324. {
  1325. _3D_CHICKEN3,
  1326. __MASKED_FIELD(_3D_CHICKEN_SF_PROVOKING_VERTEX_FIX,
  1327. _3D_CHICKEN_SF_PROVOKING_VERTEX_FIX),
  1328. }
  1329. };
  1330. *batch++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
  1331. /* WaFlushCoherentL3CacheLinesAtContextSwitch:skl,bxt,glk */
  1332. batch = gen8_emit_flush_coherentl3_wa(engine, batch);
  1333. /* WaClearSlmSpaceAtContextSwitch:skl,bxt,kbl,glk,cfl */
  1334. batch = gen8_emit_pipe_control(batch,
  1335. PIPE_CONTROL_FLUSH_L3 |
  1336. PIPE_CONTROL_GLOBAL_GTT_IVB |
  1337. PIPE_CONTROL_CS_STALL |
  1338. PIPE_CONTROL_QW_WRITE,
  1339. i915_ggtt_offset(engine->scratch) +
  1340. 2 * CACHELINE_BYTES);
  1341. batch = emit_lri(batch, lri, ARRAY_SIZE(lri));
  1342. /* WaClearSlmSpaceAtContextSwitch:kbl */
  1343. /* Actual scratch location is at 128 bytes offset */
  1344. if (IS_KBL_REVID(engine->i915, 0, KBL_REVID_A0)) {
  1345. batch = gen8_emit_pipe_control(batch,
  1346. PIPE_CONTROL_FLUSH_L3 |
  1347. PIPE_CONTROL_GLOBAL_GTT_IVB |
  1348. PIPE_CONTROL_CS_STALL |
  1349. PIPE_CONTROL_QW_WRITE,
  1350. i915_ggtt_offset(engine->scratch)
  1351. + 2 * CACHELINE_BYTES);
  1352. }
  1353. /* WaMediaPoolStateCmdInWABB:bxt,glk */
  1354. if (HAS_POOLED_EU(engine->i915)) {
  1355. /*
  1356. * EU pool configuration is setup along with golden context
  1357. * during context initialization. This value depends on
  1358. * device type (2x6 or 3x6) and needs to be updated based
  1359. * on which subslice is disabled especially for 2x6
  1360. * devices, however it is safe to load default
  1361. * configuration of 3x6 device instead of masking off
  1362. * corresponding bits because HW ignores bits of a disabled
  1363. * subslice and drops down to appropriate config. Please
  1364. * see render_state_setup() in i915_gem_render_state.c for
  1365. * possible configurations, to avoid duplication they are
  1366. * not shown here again.
  1367. */
  1368. *batch++ = GEN9_MEDIA_POOL_STATE;
  1369. *batch++ = GEN9_MEDIA_POOL_ENABLE;
  1370. *batch++ = 0x00777000;
  1371. *batch++ = 0;
  1372. *batch++ = 0;
  1373. *batch++ = 0;
  1374. }
  1375. *batch++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  1376. /* Pad to end of cacheline */
  1377. while ((unsigned long)batch % CACHELINE_BYTES)
  1378. *batch++ = MI_NOOP;
  1379. return batch;
  1380. }
  1381. static u32 *
  1382. gen10_init_indirectctx_bb(struct intel_engine_cs *engine, u32 *batch)
  1383. {
  1384. int i;
  1385. /*
  1386. * WaPipeControlBefore3DStateSamplePattern: cnl
  1387. *
  1388. * Ensure the engine is idle prior to programming a
  1389. * 3DSTATE_SAMPLE_PATTERN during a context restore.
  1390. */
  1391. batch = gen8_emit_pipe_control(batch,
  1392. PIPE_CONTROL_CS_STALL,
  1393. 0);
  1394. /*
  1395. * WaPipeControlBefore3DStateSamplePattern says we need 4 dwords for
  1396. * the PIPE_CONTROL followed by 12 dwords of 0x0, so 16 dwords in
  1397. * total. However, a PIPE_CONTROL is 6 dwords long, not 4, which is
  1398. * confusing. Since gen8_emit_pipe_control() already advances the
  1399. * batch by 6 dwords, we advance the other 10 here, completing a
  1400. * cacheline. It's not clear if the workaround requires this padding
  1401. * before other commands, or if it's just the regular padding we would
  1402. * already have for the workaround bb, so leave it here for now.
  1403. */
  1404. for (i = 0; i < 10; i++)
  1405. *batch++ = MI_NOOP;
  1406. /* Pad to end of cacheline */
  1407. while ((unsigned long)batch % CACHELINE_BYTES)
  1408. *batch++ = MI_NOOP;
  1409. return batch;
  1410. }
  1411. #define CTX_WA_BB_OBJ_SIZE (PAGE_SIZE)
  1412. static int lrc_setup_wa_ctx(struct intel_engine_cs *engine)
  1413. {
  1414. struct drm_i915_gem_object *obj;
  1415. struct i915_vma *vma;
  1416. int err;
  1417. obj = i915_gem_object_create(engine->i915, CTX_WA_BB_OBJ_SIZE);
  1418. if (IS_ERR(obj))
  1419. return PTR_ERR(obj);
  1420. vma = i915_vma_instance(obj, &engine->i915->ggtt.vm, NULL);
  1421. if (IS_ERR(vma)) {
  1422. err = PTR_ERR(vma);
  1423. goto err;
  1424. }
  1425. err = i915_vma_pin(vma, 0, PAGE_SIZE, PIN_GLOBAL | PIN_HIGH);
  1426. if (err)
  1427. goto err;
  1428. engine->wa_ctx.vma = vma;
  1429. return 0;
  1430. err:
  1431. i915_gem_object_put(obj);
  1432. return err;
  1433. }
  1434. static void lrc_destroy_wa_ctx(struct intel_engine_cs *engine)
  1435. {
  1436. i915_vma_unpin_and_release(&engine->wa_ctx.vma);
  1437. }
  1438. typedef u32 *(*wa_bb_func_t)(struct intel_engine_cs *engine, u32 *batch);
  1439. static int intel_init_workaround_bb(struct intel_engine_cs *engine)
  1440. {
  1441. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  1442. struct i915_wa_ctx_bb *wa_bb[2] = { &wa_ctx->indirect_ctx,
  1443. &wa_ctx->per_ctx };
  1444. wa_bb_func_t wa_bb_fn[2];
  1445. struct page *page;
  1446. void *batch, *batch_ptr;
  1447. unsigned int i;
  1448. int ret;
  1449. if (GEM_WARN_ON(engine->id != RCS))
  1450. return -EINVAL;
  1451. switch (INTEL_GEN(engine->i915)) {
  1452. case 11:
  1453. return 0;
  1454. case 10:
  1455. wa_bb_fn[0] = gen10_init_indirectctx_bb;
  1456. wa_bb_fn[1] = NULL;
  1457. break;
  1458. case 9:
  1459. wa_bb_fn[0] = gen9_init_indirectctx_bb;
  1460. wa_bb_fn[1] = NULL;
  1461. break;
  1462. case 8:
  1463. wa_bb_fn[0] = gen8_init_indirectctx_bb;
  1464. wa_bb_fn[1] = NULL;
  1465. break;
  1466. default:
  1467. MISSING_CASE(INTEL_GEN(engine->i915));
  1468. return 0;
  1469. }
  1470. ret = lrc_setup_wa_ctx(engine);
  1471. if (ret) {
  1472. DRM_DEBUG_DRIVER("Failed to setup context WA page: %d\n", ret);
  1473. return ret;
  1474. }
  1475. page = i915_gem_object_get_dirty_page(wa_ctx->vma->obj, 0);
  1476. batch = batch_ptr = kmap_atomic(page);
  1477. /*
  1478. * Emit the two workaround batch buffers, recording the offset from the
  1479. * start of the workaround batch buffer object for each and their
  1480. * respective sizes.
  1481. */
  1482. for (i = 0; i < ARRAY_SIZE(wa_bb_fn); i++) {
  1483. wa_bb[i]->offset = batch_ptr - batch;
  1484. if (GEM_WARN_ON(!IS_ALIGNED(wa_bb[i]->offset,
  1485. CACHELINE_BYTES))) {
  1486. ret = -EINVAL;
  1487. break;
  1488. }
  1489. if (wa_bb_fn[i])
  1490. batch_ptr = wa_bb_fn[i](engine, batch_ptr);
  1491. wa_bb[i]->size = batch_ptr - (batch + wa_bb[i]->offset);
  1492. }
  1493. BUG_ON(batch_ptr - batch > CTX_WA_BB_OBJ_SIZE);
  1494. kunmap_atomic(batch);
  1495. if (ret)
  1496. lrc_destroy_wa_ctx(engine);
  1497. return ret;
  1498. }
  1499. static void enable_execlists(struct intel_engine_cs *engine)
  1500. {
  1501. struct drm_i915_private *dev_priv = engine->i915;
  1502. I915_WRITE(RING_HWSTAM(engine->mmio_base), 0xffffffff);
  1503. /*
  1504. * Make sure we're not enabling the new 12-deep CSB
  1505. * FIFO as that requires a slightly updated handling
  1506. * in the ctx switch irq. Since we're currently only
  1507. * using only 2 elements of the enhanced execlists the
  1508. * deeper FIFO it's not needed and it's not worth adding
  1509. * more statements to the irq handler to support it.
  1510. */
  1511. if (INTEL_GEN(dev_priv) >= 11)
  1512. I915_WRITE(RING_MODE_GEN7(engine),
  1513. _MASKED_BIT_DISABLE(GEN11_GFX_DISABLE_LEGACY_MODE));
  1514. else
  1515. I915_WRITE(RING_MODE_GEN7(engine),
  1516. _MASKED_BIT_ENABLE(GFX_RUN_LIST_ENABLE));
  1517. I915_WRITE(RING_MI_MODE(engine->mmio_base),
  1518. _MASKED_BIT_DISABLE(STOP_RING));
  1519. I915_WRITE(RING_HWS_PGA(engine->mmio_base),
  1520. engine->status_page.ggtt_offset);
  1521. POSTING_READ(RING_HWS_PGA(engine->mmio_base));
  1522. }
  1523. static bool unexpected_starting_state(struct intel_engine_cs *engine)
  1524. {
  1525. struct drm_i915_private *dev_priv = engine->i915;
  1526. bool unexpected = false;
  1527. if (I915_READ(RING_MI_MODE(engine->mmio_base)) & STOP_RING) {
  1528. DRM_DEBUG_DRIVER("STOP_RING still set in RING_MI_MODE\n");
  1529. unexpected = true;
  1530. }
  1531. return unexpected;
  1532. }
  1533. static int gen8_init_common_ring(struct intel_engine_cs *engine)
  1534. {
  1535. int ret;
  1536. ret = intel_mocs_init_engine(engine);
  1537. if (ret)
  1538. return ret;
  1539. intel_engine_reset_breadcrumbs(engine);
  1540. if (GEM_SHOW_DEBUG() && unexpected_starting_state(engine)) {
  1541. struct drm_printer p = drm_debug_printer(__func__);
  1542. intel_engine_dump(engine, &p, NULL);
  1543. }
  1544. enable_execlists(engine);
  1545. return 0;
  1546. }
  1547. static int gen8_init_render_ring(struct intel_engine_cs *engine)
  1548. {
  1549. struct drm_i915_private *dev_priv = engine->i915;
  1550. int ret;
  1551. ret = gen8_init_common_ring(engine);
  1552. if (ret)
  1553. return ret;
  1554. intel_whitelist_workarounds_apply(engine);
  1555. /* We need to disable the AsyncFlip performance optimisations in order
  1556. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  1557. * programmed to '1' on all products.
  1558. *
  1559. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw,chv
  1560. */
  1561. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  1562. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  1563. return 0;
  1564. }
  1565. static int gen9_init_render_ring(struct intel_engine_cs *engine)
  1566. {
  1567. int ret;
  1568. ret = gen8_init_common_ring(engine);
  1569. if (ret)
  1570. return ret;
  1571. intel_whitelist_workarounds_apply(engine);
  1572. return 0;
  1573. }
  1574. static struct i915_request *
  1575. execlists_reset_prepare(struct intel_engine_cs *engine)
  1576. {
  1577. struct intel_engine_execlists * const execlists = &engine->execlists;
  1578. struct i915_request *request, *active;
  1579. unsigned long flags;
  1580. GEM_TRACE("%s\n", engine->name);
  1581. /*
  1582. * Prevent request submission to the hardware until we have
  1583. * completed the reset in i915_gem_reset_finish(). If a request
  1584. * is completed by one engine, it may then queue a request
  1585. * to a second via its execlists->tasklet *just* as we are
  1586. * calling engine->init_hw() and also writing the ELSP.
  1587. * Turning off the execlists->tasklet until the reset is over
  1588. * prevents the race.
  1589. */
  1590. __tasklet_disable_sync_once(&execlists->tasklet);
  1591. spin_lock_irqsave(&engine->timeline.lock, flags);
  1592. /*
  1593. * We want to flush the pending context switches, having disabled
  1594. * the tasklet above, we can assume exclusive access to the execlists.
  1595. * For this allows us to catch up with an inflight preemption event,
  1596. * and avoid blaming an innocent request if the stall was due to the
  1597. * preemption itself.
  1598. */
  1599. process_csb(engine);
  1600. /*
  1601. * The last active request can then be no later than the last request
  1602. * now in ELSP[0]. So search backwards from there, so that if the GPU
  1603. * has advanced beyond the last CSB update, it will be pardoned.
  1604. */
  1605. active = NULL;
  1606. request = port_request(execlists->port);
  1607. if (request) {
  1608. /*
  1609. * Prevent the breadcrumb from advancing before we decide
  1610. * which request is currently active.
  1611. */
  1612. intel_engine_stop_cs(engine);
  1613. list_for_each_entry_from_reverse(request,
  1614. &engine->timeline.requests,
  1615. link) {
  1616. if (__i915_request_completed(request,
  1617. request->global_seqno))
  1618. break;
  1619. active = request;
  1620. }
  1621. }
  1622. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  1623. return active;
  1624. }
  1625. static void execlists_reset(struct intel_engine_cs *engine,
  1626. struct i915_request *request)
  1627. {
  1628. struct intel_engine_execlists * const execlists = &engine->execlists;
  1629. unsigned long flags;
  1630. u32 *regs;
  1631. GEM_TRACE("%s request global=%x, current=%d\n",
  1632. engine->name, request ? request->global_seqno : 0,
  1633. intel_engine_get_seqno(engine));
  1634. spin_lock_irqsave(&engine->timeline.lock, flags);
  1635. /*
  1636. * Catch up with any missed context-switch interrupts.
  1637. *
  1638. * Ideally we would just read the remaining CSB entries now that we
  1639. * know the gpu is idle. However, the CSB registers are sometimes^W
  1640. * often trashed across a GPU reset! Instead we have to rely on
  1641. * guessing the missed context-switch events by looking at what
  1642. * requests were completed.
  1643. */
  1644. execlists_cancel_port_requests(execlists);
  1645. /* Push back any incomplete requests for replay after the reset. */
  1646. __unwind_incomplete_requests(engine);
  1647. /* Following the reset, we need to reload the CSB read/write pointers */
  1648. reset_csb_pointers(&engine->execlists);
  1649. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  1650. /*
  1651. * If the request was innocent, we leave the request in the ELSP
  1652. * and will try to replay it on restarting. The context image may
  1653. * have been corrupted by the reset, in which case we may have
  1654. * to service a new GPU hang, but more likely we can continue on
  1655. * without impact.
  1656. *
  1657. * If the request was guilty, we presume the context is corrupt
  1658. * and have to at least restore the RING register in the context
  1659. * image back to the expected values to skip over the guilty request.
  1660. */
  1661. if (!request || request->fence.error != -EIO)
  1662. return;
  1663. /*
  1664. * We want a simple context + ring to execute the breadcrumb update.
  1665. * We cannot rely on the context being intact across the GPU hang,
  1666. * so clear it and rebuild just what we need for the breadcrumb.
  1667. * All pending requests for this context will be zapped, and any
  1668. * future request will be after userspace has had the opportunity
  1669. * to recreate its own state.
  1670. */
  1671. regs = request->hw_context->lrc_reg_state;
  1672. if (engine->pinned_default_state) {
  1673. memcpy(regs, /* skip restoring the vanilla PPHWSP */
  1674. engine->pinned_default_state + LRC_STATE_PN * PAGE_SIZE,
  1675. engine->context_size - PAGE_SIZE);
  1676. }
  1677. execlists_init_reg_state(regs,
  1678. request->gem_context, engine, request->ring);
  1679. /* Move the RING_HEAD onto the breadcrumb, past the hanging batch */
  1680. regs[CTX_RING_BUFFER_START + 1] = i915_ggtt_offset(request->ring->vma);
  1681. request->ring->head = intel_ring_wrap(request->ring, request->postfix);
  1682. regs[CTX_RING_HEAD + 1] = request->ring->head;
  1683. intel_ring_update_space(request->ring);
  1684. /* Reset WaIdleLiteRestore:bdw,skl as well */
  1685. unwind_wa_tail(request);
  1686. }
  1687. static void execlists_reset_finish(struct intel_engine_cs *engine)
  1688. {
  1689. struct intel_engine_execlists * const execlists = &engine->execlists;
  1690. /* After a GPU reset, we may have requests to replay */
  1691. if (!RB_EMPTY_ROOT(&execlists->queue.rb_root))
  1692. tasklet_schedule(&execlists->tasklet);
  1693. /*
  1694. * Flush the tasklet while we still have the forcewake to be sure
  1695. * that it is not allowed to sleep before we restart and reload a
  1696. * context.
  1697. *
  1698. * As before (with execlists_reset_prepare) we rely on the caller
  1699. * serialising multiple attempts to reset so that we know that we
  1700. * are the only one manipulating tasklet state.
  1701. */
  1702. __tasklet_enable_sync_once(&execlists->tasklet);
  1703. GEM_TRACE("%s\n", engine->name);
  1704. }
  1705. static int intel_logical_ring_emit_pdps(struct i915_request *rq)
  1706. {
  1707. struct i915_hw_ppgtt *ppgtt = rq->gem_context->ppgtt;
  1708. struct intel_engine_cs *engine = rq->engine;
  1709. const int num_lri_cmds = GEN8_3LVL_PDPES * 2;
  1710. u32 *cs;
  1711. int i;
  1712. cs = intel_ring_begin(rq, num_lri_cmds * 2 + 2);
  1713. if (IS_ERR(cs))
  1714. return PTR_ERR(cs);
  1715. *cs++ = MI_LOAD_REGISTER_IMM(num_lri_cmds);
  1716. for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
  1717. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  1718. *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, i));
  1719. *cs++ = upper_32_bits(pd_daddr);
  1720. *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, i));
  1721. *cs++ = lower_32_bits(pd_daddr);
  1722. }
  1723. *cs++ = MI_NOOP;
  1724. intel_ring_advance(rq, cs);
  1725. return 0;
  1726. }
  1727. static int gen8_emit_bb_start(struct i915_request *rq,
  1728. u64 offset, u32 len,
  1729. const unsigned int flags)
  1730. {
  1731. u32 *cs;
  1732. int ret;
  1733. /* Don't rely in hw updating PDPs, specially in lite-restore.
  1734. * Ideally, we should set Force PD Restore in ctx descriptor,
  1735. * but we can't. Force Restore would be a second option, but
  1736. * it is unsafe in case of lite-restore (because the ctx is
  1737. * not idle). PML4 is allocated during ppgtt init so this is
  1738. * not needed in 48-bit.*/
  1739. if (rq->gem_context->ppgtt &&
  1740. (intel_engine_flag(rq->engine) & rq->gem_context->ppgtt->pd_dirty_rings) &&
  1741. !i915_vm_is_48bit(&rq->gem_context->ppgtt->vm) &&
  1742. !intel_vgpu_active(rq->i915)) {
  1743. ret = intel_logical_ring_emit_pdps(rq);
  1744. if (ret)
  1745. return ret;
  1746. rq->gem_context->ppgtt->pd_dirty_rings &= ~intel_engine_flag(rq->engine);
  1747. }
  1748. cs = intel_ring_begin(rq, 6);
  1749. if (IS_ERR(cs))
  1750. return PTR_ERR(cs);
  1751. /*
  1752. * WaDisableCtxRestoreArbitration:bdw,chv
  1753. *
  1754. * We don't need to perform MI_ARB_ENABLE as often as we do (in
  1755. * particular all the gen that do not need the w/a at all!), if we
  1756. * took care to make sure that on every switch into this context
  1757. * (both ordinary and for preemption) that arbitrartion was enabled
  1758. * we would be fine. However, there doesn't seem to be a downside to
  1759. * being paranoid and making sure it is set before each batch and
  1760. * every context-switch.
  1761. *
  1762. * Note that if we fail to enable arbitration before the request
  1763. * is complete, then we do not see the context-switch interrupt and
  1764. * the engine hangs (with RING_HEAD == RING_TAIL).
  1765. *
  1766. * That satisfies both the GPGPU w/a and our heavy-handed paranoia.
  1767. */
  1768. *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  1769. /* FIXME(BDW): Address space and security selectors. */
  1770. *cs++ = MI_BATCH_BUFFER_START_GEN8 |
  1771. (flags & I915_DISPATCH_SECURE ? 0 : BIT(8)) |
  1772. (flags & I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
  1773. *cs++ = lower_32_bits(offset);
  1774. *cs++ = upper_32_bits(offset);
  1775. *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
  1776. *cs++ = MI_NOOP;
  1777. intel_ring_advance(rq, cs);
  1778. return 0;
  1779. }
  1780. static void gen8_logical_ring_enable_irq(struct intel_engine_cs *engine)
  1781. {
  1782. struct drm_i915_private *dev_priv = engine->i915;
  1783. I915_WRITE_IMR(engine,
  1784. ~(engine->irq_enable_mask | engine->irq_keep_mask));
  1785. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1786. }
  1787. static void gen8_logical_ring_disable_irq(struct intel_engine_cs *engine)
  1788. {
  1789. struct drm_i915_private *dev_priv = engine->i915;
  1790. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  1791. }
  1792. static int gen8_emit_flush(struct i915_request *request, u32 mode)
  1793. {
  1794. u32 cmd, *cs;
  1795. cs = intel_ring_begin(request, 4);
  1796. if (IS_ERR(cs))
  1797. return PTR_ERR(cs);
  1798. cmd = MI_FLUSH_DW + 1;
  1799. /* We always require a command barrier so that subsequent
  1800. * commands, such as breadcrumb interrupts, are strictly ordered
  1801. * wrt the contents of the write cache being flushed to memory
  1802. * (and thus being coherent from the CPU).
  1803. */
  1804. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1805. if (mode & EMIT_INVALIDATE) {
  1806. cmd |= MI_INVALIDATE_TLB;
  1807. if (request->engine->id == VCS)
  1808. cmd |= MI_INVALIDATE_BSD;
  1809. }
  1810. *cs++ = cmd;
  1811. *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
  1812. *cs++ = 0; /* upper addr */
  1813. *cs++ = 0; /* value */
  1814. intel_ring_advance(request, cs);
  1815. return 0;
  1816. }
  1817. static int gen8_emit_flush_render(struct i915_request *request,
  1818. u32 mode)
  1819. {
  1820. struct intel_engine_cs *engine = request->engine;
  1821. u32 scratch_addr =
  1822. i915_ggtt_offset(engine->scratch) + 2 * CACHELINE_BYTES;
  1823. bool vf_flush_wa = false, dc_flush_wa = false;
  1824. u32 *cs, flags = 0;
  1825. int len;
  1826. flags |= PIPE_CONTROL_CS_STALL;
  1827. if (mode & EMIT_FLUSH) {
  1828. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  1829. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  1830. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  1831. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  1832. }
  1833. if (mode & EMIT_INVALIDATE) {
  1834. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  1835. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  1836. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  1837. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  1838. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  1839. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  1840. flags |= PIPE_CONTROL_QW_WRITE;
  1841. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  1842. /*
  1843. * On GEN9: before VF_CACHE_INVALIDATE we need to emit a NULL
  1844. * pipe control.
  1845. */
  1846. if (IS_GEN9(request->i915))
  1847. vf_flush_wa = true;
  1848. /* WaForGAMHang:kbl */
  1849. if (IS_KBL_REVID(request->i915, 0, KBL_REVID_B0))
  1850. dc_flush_wa = true;
  1851. }
  1852. len = 6;
  1853. if (vf_flush_wa)
  1854. len += 6;
  1855. if (dc_flush_wa)
  1856. len += 12;
  1857. cs = intel_ring_begin(request, len);
  1858. if (IS_ERR(cs))
  1859. return PTR_ERR(cs);
  1860. if (vf_flush_wa)
  1861. cs = gen8_emit_pipe_control(cs, 0, 0);
  1862. if (dc_flush_wa)
  1863. cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_DC_FLUSH_ENABLE,
  1864. 0);
  1865. cs = gen8_emit_pipe_control(cs, flags, scratch_addr);
  1866. if (dc_flush_wa)
  1867. cs = gen8_emit_pipe_control(cs, PIPE_CONTROL_CS_STALL, 0);
  1868. intel_ring_advance(request, cs);
  1869. return 0;
  1870. }
  1871. /*
  1872. * Reserve space for 2 NOOPs at the end of each request to be
  1873. * used as a workaround for not being allowed to do lite
  1874. * restore with HEAD==TAIL (WaIdleLiteRestore).
  1875. */
  1876. static void gen8_emit_wa_tail(struct i915_request *request, u32 *cs)
  1877. {
  1878. /* Ensure there's always at least one preemption point per-request. */
  1879. *cs++ = MI_ARB_CHECK;
  1880. *cs++ = MI_NOOP;
  1881. request->wa_tail = intel_ring_offset(request, cs);
  1882. }
  1883. static void gen8_emit_breadcrumb(struct i915_request *request, u32 *cs)
  1884. {
  1885. /* w/a: bit 5 needs to be zero for MI_FLUSH_DW address. */
  1886. BUILD_BUG_ON(I915_GEM_HWS_INDEX_ADDR & (1 << 5));
  1887. cs = gen8_emit_ggtt_write(cs, request->global_seqno,
  1888. intel_hws_seqno_address(request->engine));
  1889. *cs++ = MI_USER_INTERRUPT;
  1890. *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  1891. request->tail = intel_ring_offset(request, cs);
  1892. assert_ring_tail_valid(request->ring, request->tail);
  1893. gen8_emit_wa_tail(request, cs);
  1894. }
  1895. static const int gen8_emit_breadcrumb_sz = 6 + WA_TAIL_DWORDS;
  1896. static void gen8_emit_breadcrumb_rcs(struct i915_request *request, u32 *cs)
  1897. {
  1898. /* We're using qword write, seqno should be aligned to 8 bytes. */
  1899. BUILD_BUG_ON(I915_GEM_HWS_INDEX & 1);
  1900. cs = gen8_emit_ggtt_write_rcs(cs, request->global_seqno,
  1901. intel_hws_seqno_address(request->engine));
  1902. *cs++ = MI_USER_INTERRUPT;
  1903. *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  1904. request->tail = intel_ring_offset(request, cs);
  1905. assert_ring_tail_valid(request->ring, request->tail);
  1906. gen8_emit_wa_tail(request, cs);
  1907. }
  1908. static const int gen8_emit_breadcrumb_rcs_sz = 8 + WA_TAIL_DWORDS;
  1909. static int gen8_init_rcs_context(struct i915_request *rq)
  1910. {
  1911. int ret;
  1912. ret = intel_ctx_workarounds_emit(rq);
  1913. if (ret)
  1914. return ret;
  1915. ret = intel_rcs_context_init_mocs(rq);
  1916. /*
  1917. * Failing to program the MOCS is non-fatal.The system will not
  1918. * run at peak performance. So generate an error and carry on.
  1919. */
  1920. if (ret)
  1921. DRM_ERROR("MOCS failed to program: expect performance issues.\n");
  1922. return i915_gem_render_state_emit(rq);
  1923. }
  1924. /**
  1925. * intel_logical_ring_cleanup() - deallocate the Engine Command Streamer
  1926. * @engine: Engine Command Streamer.
  1927. */
  1928. void intel_logical_ring_cleanup(struct intel_engine_cs *engine)
  1929. {
  1930. struct drm_i915_private *dev_priv;
  1931. /*
  1932. * Tasklet cannot be active at this point due intel_mark_active/idle
  1933. * so this is just for documentation.
  1934. */
  1935. if (WARN_ON(test_bit(TASKLET_STATE_SCHED,
  1936. &engine->execlists.tasklet.state)))
  1937. tasklet_kill(&engine->execlists.tasklet);
  1938. dev_priv = engine->i915;
  1939. if (engine->buffer) {
  1940. WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1941. }
  1942. if (engine->cleanup)
  1943. engine->cleanup(engine);
  1944. intel_engine_cleanup_common(engine);
  1945. lrc_destroy_wa_ctx(engine);
  1946. engine->i915 = NULL;
  1947. dev_priv->engine[engine->id] = NULL;
  1948. kfree(engine);
  1949. }
  1950. void intel_execlists_set_default_submission(struct intel_engine_cs *engine)
  1951. {
  1952. engine->submit_request = execlists_submit_request;
  1953. engine->cancel_requests = execlists_cancel_requests;
  1954. engine->schedule = execlists_schedule;
  1955. engine->execlists.tasklet.func = execlists_submission_tasklet;
  1956. engine->reset.prepare = execlists_reset_prepare;
  1957. engine->park = NULL;
  1958. engine->unpark = NULL;
  1959. engine->flags |= I915_ENGINE_SUPPORTS_STATS;
  1960. if (engine->i915->preempt_context)
  1961. engine->flags |= I915_ENGINE_HAS_PREEMPTION;
  1962. engine->i915->caps.scheduler =
  1963. I915_SCHEDULER_CAP_ENABLED |
  1964. I915_SCHEDULER_CAP_PRIORITY;
  1965. if (intel_engine_has_preemption(engine))
  1966. engine->i915->caps.scheduler |= I915_SCHEDULER_CAP_PREEMPTION;
  1967. }
  1968. static void
  1969. logical_ring_default_vfuncs(struct intel_engine_cs *engine)
  1970. {
  1971. /* Default vfuncs which can be overriden by each engine. */
  1972. engine->init_hw = gen8_init_common_ring;
  1973. engine->reset.prepare = execlists_reset_prepare;
  1974. engine->reset.reset = execlists_reset;
  1975. engine->reset.finish = execlists_reset_finish;
  1976. engine->context_pin = execlists_context_pin;
  1977. engine->request_alloc = execlists_request_alloc;
  1978. engine->emit_flush = gen8_emit_flush;
  1979. engine->emit_breadcrumb = gen8_emit_breadcrumb;
  1980. engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_sz;
  1981. engine->set_default_submission = intel_execlists_set_default_submission;
  1982. if (INTEL_GEN(engine->i915) < 11) {
  1983. engine->irq_enable = gen8_logical_ring_enable_irq;
  1984. engine->irq_disable = gen8_logical_ring_disable_irq;
  1985. } else {
  1986. /*
  1987. * TODO: On Gen11 interrupt masks need to be clear
  1988. * to allow C6 entry. Keep interrupts enabled at
  1989. * and take the hit of generating extra interrupts
  1990. * until a more refined solution exists.
  1991. */
  1992. }
  1993. engine->emit_bb_start = gen8_emit_bb_start;
  1994. }
  1995. static inline void
  1996. logical_ring_default_irqs(struct intel_engine_cs *engine)
  1997. {
  1998. unsigned int shift = 0;
  1999. if (INTEL_GEN(engine->i915) < 11) {
  2000. const u8 irq_shifts[] = {
  2001. [RCS] = GEN8_RCS_IRQ_SHIFT,
  2002. [BCS] = GEN8_BCS_IRQ_SHIFT,
  2003. [VCS] = GEN8_VCS1_IRQ_SHIFT,
  2004. [VCS2] = GEN8_VCS2_IRQ_SHIFT,
  2005. [VECS] = GEN8_VECS_IRQ_SHIFT,
  2006. };
  2007. shift = irq_shifts[engine->id];
  2008. }
  2009. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << shift;
  2010. engine->irq_keep_mask = GT_CONTEXT_SWITCH_INTERRUPT << shift;
  2011. }
  2012. static void
  2013. logical_ring_setup(struct intel_engine_cs *engine)
  2014. {
  2015. intel_engine_setup_common(engine);
  2016. /* Intentionally left blank. */
  2017. engine->buffer = NULL;
  2018. tasklet_init(&engine->execlists.tasklet,
  2019. execlists_submission_tasklet, (unsigned long)engine);
  2020. logical_ring_default_vfuncs(engine);
  2021. logical_ring_default_irqs(engine);
  2022. }
  2023. static bool csb_force_mmio(struct drm_i915_private *i915)
  2024. {
  2025. /* Older GVT emulation depends upon intercepting CSB mmio */
  2026. return intel_vgpu_active(i915) && !intel_vgpu_has_hwsp_emulation(i915);
  2027. }
  2028. static int logical_ring_init(struct intel_engine_cs *engine)
  2029. {
  2030. struct drm_i915_private *i915 = engine->i915;
  2031. struct intel_engine_execlists * const execlists = &engine->execlists;
  2032. int ret;
  2033. ret = intel_engine_init_common(engine);
  2034. if (ret)
  2035. goto error;
  2036. if (HAS_LOGICAL_RING_ELSQ(i915)) {
  2037. execlists->submit_reg = i915->regs +
  2038. i915_mmio_reg_offset(RING_EXECLIST_SQ_CONTENTS(engine));
  2039. execlists->ctrl_reg = i915->regs +
  2040. i915_mmio_reg_offset(RING_EXECLIST_CONTROL(engine));
  2041. } else {
  2042. execlists->submit_reg = i915->regs +
  2043. i915_mmio_reg_offset(RING_ELSP(engine));
  2044. }
  2045. execlists->preempt_complete_status = ~0u;
  2046. if (i915->preempt_context) {
  2047. struct intel_context *ce =
  2048. to_intel_context(i915->preempt_context, engine);
  2049. execlists->preempt_complete_status =
  2050. upper_32_bits(ce->lrc_desc);
  2051. }
  2052. execlists->csb_read =
  2053. i915->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_PTR(engine));
  2054. if (csb_force_mmio(i915)) {
  2055. execlists->csb_status = (u32 __force *)
  2056. (i915->regs + i915_mmio_reg_offset(RING_CONTEXT_STATUS_BUF_LO(engine, 0)));
  2057. execlists->csb_write = (u32 __force *)execlists->csb_read;
  2058. execlists->csb_write_reset =
  2059. _MASKED_FIELD(GEN8_CSB_WRITE_PTR_MASK,
  2060. GEN8_CSB_ENTRIES - 1);
  2061. } else {
  2062. execlists->csb_status =
  2063. &engine->status_page.page_addr[I915_HWS_CSB_BUF0_INDEX];
  2064. execlists->csb_write =
  2065. &engine->status_page.page_addr[intel_hws_csb_write_index(i915)];
  2066. execlists->csb_write_reset = GEN8_CSB_ENTRIES - 1;
  2067. }
  2068. reset_csb_pointers(execlists);
  2069. return 0;
  2070. error:
  2071. intel_logical_ring_cleanup(engine);
  2072. return ret;
  2073. }
  2074. int logical_render_ring_init(struct intel_engine_cs *engine)
  2075. {
  2076. struct drm_i915_private *dev_priv = engine->i915;
  2077. int ret;
  2078. logical_ring_setup(engine);
  2079. if (HAS_L3_DPF(dev_priv))
  2080. engine->irq_keep_mask |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  2081. /* Override some for render ring. */
  2082. if (INTEL_GEN(dev_priv) >= 9)
  2083. engine->init_hw = gen9_init_render_ring;
  2084. else
  2085. engine->init_hw = gen8_init_render_ring;
  2086. engine->init_context = gen8_init_rcs_context;
  2087. engine->emit_flush = gen8_emit_flush_render;
  2088. engine->emit_breadcrumb = gen8_emit_breadcrumb_rcs;
  2089. engine->emit_breadcrumb_sz = gen8_emit_breadcrumb_rcs_sz;
  2090. ret = intel_engine_create_scratch(engine, PAGE_SIZE);
  2091. if (ret)
  2092. return ret;
  2093. ret = intel_init_workaround_bb(engine);
  2094. if (ret) {
  2095. /*
  2096. * We continue even if we fail to initialize WA batch
  2097. * because we only expect rare glitches but nothing
  2098. * critical to prevent us from using GPU
  2099. */
  2100. DRM_ERROR("WA batch buffer initialization failed: %d\n",
  2101. ret);
  2102. }
  2103. return logical_ring_init(engine);
  2104. }
  2105. int logical_xcs_ring_init(struct intel_engine_cs *engine)
  2106. {
  2107. logical_ring_setup(engine);
  2108. return logical_ring_init(engine);
  2109. }
  2110. static u32
  2111. make_rpcs(struct drm_i915_private *dev_priv)
  2112. {
  2113. u32 rpcs = 0;
  2114. /*
  2115. * No explicit RPCS request is needed to ensure full
  2116. * slice/subslice/EU enablement prior to Gen9.
  2117. */
  2118. if (INTEL_GEN(dev_priv) < 9)
  2119. return 0;
  2120. /*
  2121. * Starting in Gen9, render power gating can leave
  2122. * slice/subslice/EU in a partially enabled state. We
  2123. * must make an explicit request through RPCS for full
  2124. * enablement.
  2125. */
  2126. if (INTEL_INFO(dev_priv)->sseu.has_slice_pg) {
  2127. rpcs |= GEN8_RPCS_S_CNT_ENABLE;
  2128. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.slice_mask) <<
  2129. GEN8_RPCS_S_CNT_SHIFT;
  2130. rpcs |= GEN8_RPCS_ENABLE;
  2131. }
  2132. if (INTEL_INFO(dev_priv)->sseu.has_subslice_pg) {
  2133. rpcs |= GEN8_RPCS_SS_CNT_ENABLE;
  2134. rpcs |= hweight8(INTEL_INFO(dev_priv)->sseu.subslice_mask[0]) <<
  2135. GEN8_RPCS_SS_CNT_SHIFT;
  2136. rpcs |= GEN8_RPCS_ENABLE;
  2137. }
  2138. if (INTEL_INFO(dev_priv)->sseu.has_eu_pg) {
  2139. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  2140. GEN8_RPCS_EU_MIN_SHIFT;
  2141. rpcs |= INTEL_INFO(dev_priv)->sseu.eu_per_subslice <<
  2142. GEN8_RPCS_EU_MAX_SHIFT;
  2143. rpcs |= GEN8_RPCS_ENABLE;
  2144. }
  2145. return rpcs;
  2146. }
  2147. static u32 intel_lr_indirect_ctx_offset(struct intel_engine_cs *engine)
  2148. {
  2149. u32 indirect_ctx_offset;
  2150. switch (INTEL_GEN(engine->i915)) {
  2151. default:
  2152. MISSING_CASE(INTEL_GEN(engine->i915));
  2153. /* fall through */
  2154. case 11:
  2155. indirect_ctx_offset =
  2156. GEN11_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  2157. break;
  2158. case 10:
  2159. indirect_ctx_offset =
  2160. GEN10_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  2161. break;
  2162. case 9:
  2163. indirect_ctx_offset =
  2164. GEN9_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  2165. break;
  2166. case 8:
  2167. indirect_ctx_offset =
  2168. GEN8_CTX_RCS_INDIRECT_CTX_OFFSET_DEFAULT;
  2169. break;
  2170. }
  2171. return indirect_ctx_offset;
  2172. }
  2173. static void execlists_init_reg_state(u32 *regs,
  2174. struct i915_gem_context *ctx,
  2175. struct intel_engine_cs *engine,
  2176. struct intel_ring *ring)
  2177. {
  2178. struct drm_i915_private *dev_priv = engine->i915;
  2179. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: dev_priv->mm.aliasing_ppgtt;
  2180. u32 base = engine->mmio_base;
  2181. bool rcs = engine->class == RENDER_CLASS;
  2182. /* A context is actually a big batch buffer with several
  2183. * MI_LOAD_REGISTER_IMM commands followed by (reg, value) pairs. The
  2184. * values we are setting here are only for the first context restore:
  2185. * on a subsequent save, the GPU will recreate this batchbuffer with new
  2186. * values (including all the missing MI_LOAD_REGISTER_IMM commands that
  2187. * we are not initializing here).
  2188. */
  2189. regs[CTX_LRI_HEADER_0] = MI_LOAD_REGISTER_IMM(rcs ? 14 : 11) |
  2190. MI_LRI_FORCE_POSTED;
  2191. CTX_REG(regs, CTX_CONTEXT_CONTROL, RING_CONTEXT_CONTROL(engine),
  2192. _MASKED_BIT_DISABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
  2193. CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT) |
  2194. _MASKED_BIT_ENABLE(CTX_CTRL_INHIBIT_SYN_CTX_SWITCH |
  2195. (HAS_RESOURCE_STREAMER(dev_priv) ?
  2196. CTX_CTRL_RS_CTX_ENABLE : 0)));
  2197. CTX_REG(regs, CTX_RING_HEAD, RING_HEAD(base), 0);
  2198. CTX_REG(regs, CTX_RING_TAIL, RING_TAIL(base), 0);
  2199. CTX_REG(regs, CTX_RING_BUFFER_START, RING_START(base), 0);
  2200. CTX_REG(regs, CTX_RING_BUFFER_CONTROL, RING_CTL(base),
  2201. RING_CTL_SIZE(ring->size) | RING_VALID);
  2202. CTX_REG(regs, CTX_BB_HEAD_U, RING_BBADDR_UDW(base), 0);
  2203. CTX_REG(regs, CTX_BB_HEAD_L, RING_BBADDR(base), 0);
  2204. CTX_REG(regs, CTX_BB_STATE, RING_BBSTATE(base), RING_BB_PPGTT);
  2205. CTX_REG(regs, CTX_SECOND_BB_HEAD_U, RING_SBBADDR_UDW(base), 0);
  2206. CTX_REG(regs, CTX_SECOND_BB_HEAD_L, RING_SBBADDR(base), 0);
  2207. CTX_REG(regs, CTX_SECOND_BB_STATE, RING_SBBSTATE(base), 0);
  2208. if (rcs) {
  2209. struct i915_ctx_workarounds *wa_ctx = &engine->wa_ctx;
  2210. CTX_REG(regs, CTX_RCS_INDIRECT_CTX, RING_INDIRECT_CTX(base), 0);
  2211. CTX_REG(regs, CTX_RCS_INDIRECT_CTX_OFFSET,
  2212. RING_INDIRECT_CTX_OFFSET(base), 0);
  2213. if (wa_ctx->indirect_ctx.size) {
  2214. u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
  2215. regs[CTX_RCS_INDIRECT_CTX + 1] =
  2216. (ggtt_offset + wa_ctx->indirect_ctx.offset) |
  2217. (wa_ctx->indirect_ctx.size / CACHELINE_BYTES);
  2218. regs[CTX_RCS_INDIRECT_CTX_OFFSET + 1] =
  2219. intel_lr_indirect_ctx_offset(engine) << 6;
  2220. }
  2221. CTX_REG(regs, CTX_BB_PER_CTX_PTR, RING_BB_PER_CTX_PTR(base), 0);
  2222. if (wa_ctx->per_ctx.size) {
  2223. u32 ggtt_offset = i915_ggtt_offset(wa_ctx->vma);
  2224. regs[CTX_BB_PER_CTX_PTR + 1] =
  2225. (ggtt_offset + wa_ctx->per_ctx.offset) | 0x01;
  2226. }
  2227. }
  2228. regs[CTX_LRI_HEADER_1] = MI_LOAD_REGISTER_IMM(9) | MI_LRI_FORCE_POSTED;
  2229. CTX_REG(regs, CTX_CTX_TIMESTAMP, RING_CTX_TIMESTAMP(base), 0);
  2230. /* PDP values well be assigned later if needed */
  2231. CTX_REG(regs, CTX_PDP3_UDW, GEN8_RING_PDP_UDW(engine, 3), 0);
  2232. CTX_REG(regs, CTX_PDP3_LDW, GEN8_RING_PDP_LDW(engine, 3), 0);
  2233. CTX_REG(regs, CTX_PDP2_UDW, GEN8_RING_PDP_UDW(engine, 2), 0);
  2234. CTX_REG(regs, CTX_PDP2_LDW, GEN8_RING_PDP_LDW(engine, 2), 0);
  2235. CTX_REG(regs, CTX_PDP1_UDW, GEN8_RING_PDP_UDW(engine, 1), 0);
  2236. CTX_REG(regs, CTX_PDP1_LDW, GEN8_RING_PDP_LDW(engine, 1), 0);
  2237. CTX_REG(regs, CTX_PDP0_UDW, GEN8_RING_PDP_UDW(engine, 0), 0);
  2238. CTX_REG(regs, CTX_PDP0_LDW, GEN8_RING_PDP_LDW(engine, 0), 0);
  2239. if (ppgtt && i915_vm_is_48bit(&ppgtt->vm)) {
  2240. /* 64b PPGTT (48bit canonical)
  2241. * PDP0_DESCRIPTOR contains the base address to PML4 and
  2242. * other PDP Descriptors are ignored.
  2243. */
  2244. ASSIGN_CTX_PML4(ppgtt, regs);
  2245. }
  2246. if (rcs) {
  2247. regs[CTX_LRI_HEADER_2] = MI_LOAD_REGISTER_IMM(1);
  2248. CTX_REG(regs, CTX_R_PWR_CLK_STATE, GEN8_R_PWR_CLK_STATE,
  2249. make_rpcs(dev_priv));
  2250. i915_oa_init_reg_state(engine, ctx, regs);
  2251. }
  2252. }
  2253. static int
  2254. populate_lr_context(struct i915_gem_context *ctx,
  2255. struct drm_i915_gem_object *ctx_obj,
  2256. struct intel_engine_cs *engine,
  2257. struct intel_ring *ring)
  2258. {
  2259. void *vaddr;
  2260. u32 *regs;
  2261. int ret;
  2262. ret = i915_gem_object_set_to_cpu_domain(ctx_obj, true);
  2263. if (ret) {
  2264. DRM_DEBUG_DRIVER("Could not set to CPU domain\n");
  2265. return ret;
  2266. }
  2267. vaddr = i915_gem_object_pin_map(ctx_obj, I915_MAP_WB);
  2268. if (IS_ERR(vaddr)) {
  2269. ret = PTR_ERR(vaddr);
  2270. DRM_DEBUG_DRIVER("Could not map object pages! (%d)\n", ret);
  2271. return ret;
  2272. }
  2273. ctx_obj->mm.dirty = true;
  2274. if (engine->default_state) {
  2275. /*
  2276. * We only want to copy over the template context state;
  2277. * skipping over the headers reserved for GuC communication,
  2278. * leaving those as zero.
  2279. */
  2280. const unsigned long start = LRC_HEADER_PAGES * PAGE_SIZE;
  2281. void *defaults;
  2282. defaults = i915_gem_object_pin_map(engine->default_state,
  2283. I915_MAP_WB);
  2284. if (IS_ERR(defaults)) {
  2285. ret = PTR_ERR(defaults);
  2286. goto err_unpin_ctx;
  2287. }
  2288. memcpy(vaddr + start, defaults + start, engine->context_size);
  2289. i915_gem_object_unpin_map(engine->default_state);
  2290. }
  2291. /* The second page of the context object contains some fields which must
  2292. * be set up prior to the first execution. */
  2293. regs = vaddr + LRC_STATE_PN * PAGE_SIZE;
  2294. execlists_init_reg_state(regs, ctx, engine, ring);
  2295. if (!engine->default_state)
  2296. regs[CTX_CONTEXT_CONTROL + 1] |=
  2297. _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT);
  2298. if (ctx == ctx->i915->preempt_context && INTEL_GEN(engine->i915) < 11)
  2299. regs[CTX_CONTEXT_CONTROL + 1] |=
  2300. _MASKED_BIT_ENABLE(CTX_CTRL_ENGINE_CTX_RESTORE_INHIBIT |
  2301. CTX_CTRL_ENGINE_CTX_SAVE_INHIBIT);
  2302. err_unpin_ctx:
  2303. i915_gem_object_unpin_map(ctx_obj);
  2304. return ret;
  2305. }
  2306. static int execlists_context_deferred_alloc(struct i915_gem_context *ctx,
  2307. struct intel_engine_cs *engine,
  2308. struct intel_context *ce)
  2309. {
  2310. struct drm_i915_gem_object *ctx_obj;
  2311. struct i915_vma *vma;
  2312. uint32_t context_size;
  2313. struct intel_ring *ring;
  2314. struct i915_timeline *timeline;
  2315. int ret;
  2316. if (ce->state)
  2317. return 0;
  2318. context_size = round_up(engine->context_size, I915_GTT_PAGE_SIZE);
  2319. /*
  2320. * Before the actual start of the context image, we insert a few pages
  2321. * for our own use and for sharing with the GuC.
  2322. */
  2323. context_size += LRC_HEADER_PAGES * PAGE_SIZE;
  2324. ctx_obj = i915_gem_object_create(ctx->i915, context_size);
  2325. if (IS_ERR(ctx_obj))
  2326. return PTR_ERR(ctx_obj);
  2327. vma = i915_vma_instance(ctx_obj, &ctx->i915->ggtt.vm, NULL);
  2328. if (IS_ERR(vma)) {
  2329. ret = PTR_ERR(vma);
  2330. goto error_deref_obj;
  2331. }
  2332. timeline = i915_timeline_create(ctx->i915, ctx->name);
  2333. if (IS_ERR(timeline)) {
  2334. ret = PTR_ERR(timeline);
  2335. goto error_deref_obj;
  2336. }
  2337. ring = intel_engine_create_ring(engine, timeline, ctx->ring_size);
  2338. i915_timeline_put(timeline);
  2339. if (IS_ERR(ring)) {
  2340. ret = PTR_ERR(ring);
  2341. goto error_deref_obj;
  2342. }
  2343. ret = populate_lr_context(ctx, ctx_obj, engine, ring);
  2344. if (ret) {
  2345. DRM_DEBUG_DRIVER("Failed to populate LRC: %d\n", ret);
  2346. goto error_ring_free;
  2347. }
  2348. ce->ring = ring;
  2349. ce->state = vma;
  2350. return 0;
  2351. error_ring_free:
  2352. intel_ring_free(ring);
  2353. error_deref_obj:
  2354. i915_gem_object_put(ctx_obj);
  2355. return ret;
  2356. }
  2357. void intel_lr_context_resume(struct drm_i915_private *dev_priv)
  2358. {
  2359. struct intel_engine_cs *engine;
  2360. struct i915_gem_context *ctx;
  2361. enum intel_engine_id id;
  2362. /* Because we emit WA_TAIL_DWORDS there may be a disparity
  2363. * between our bookkeeping in ce->ring->head and ce->ring->tail and
  2364. * that stored in context. As we only write new commands from
  2365. * ce->ring->tail onwards, everything before that is junk. If the GPU
  2366. * starts reading from its RING_HEAD from the context, it may try to
  2367. * execute that junk and die.
  2368. *
  2369. * So to avoid that we reset the context images upon resume. For
  2370. * simplicity, we just zero everything out.
  2371. */
  2372. list_for_each_entry(ctx, &dev_priv->contexts.list, link) {
  2373. for_each_engine(engine, dev_priv, id) {
  2374. struct intel_context *ce =
  2375. to_intel_context(ctx, engine);
  2376. u32 *reg;
  2377. if (!ce->state)
  2378. continue;
  2379. reg = i915_gem_object_pin_map(ce->state->obj,
  2380. I915_MAP_WB);
  2381. if (WARN_ON(IS_ERR(reg)))
  2382. continue;
  2383. reg += LRC_STATE_PN * PAGE_SIZE / sizeof(*reg);
  2384. reg[CTX_RING_HEAD+1] = 0;
  2385. reg[CTX_RING_TAIL+1] = 0;
  2386. ce->state->obj->mm.dirty = true;
  2387. i915_gem_object_unpin_map(ce->state->obj);
  2388. intel_ring_reset(ce->ring, 0);
  2389. }
  2390. }
  2391. }
  2392. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  2393. #include "selftests/intel_lrc.c"
  2394. #endif