intel_lrc.c 77 KB

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