intel_lrc.c 82 KB

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