intel_ringbuffer.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018
  1. /*
  2. * Copyright © 2008-2010 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. * Eric Anholt <eric@anholt.net>
  25. * Zou Nan hai <nanhai.zou@intel.com>
  26. * Xiang Hai hao<haihao.xiang@intel.com>
  27. *
  28. */
  29. #include <linux/log2.h>
  30. #include <drm/drmP.h>
  31. #include "i915_drv.h"
  32. #include <drm/i915_drm.h>
  33. #include "i915_trace.h"
  34. #include "intel_drv.h"
  35. /* Rough estimate of the typical request size, performing a flush,
  36. * set-context and then emitting the batch.
  37. */
  38. #define LEGACY_REQUEST_SIZE 200
  39. int __intel_ring_space(int head, int tail, int size)
  40. {
  41. int space = head - tail;
  42. if (space <= 0)
  43. space += size;
  44. return space - I915_RING_FREE_SPACE;
  45. }
  46. void intel_ring_update_space(struct intel_ringbuffer *ringbuf)
  47. {
  48. if (ringbuf->last_retired_head != -1) {
  49. ringbuf->head = ringbuf->last_retired_head;
  50. ringbuf->last_retired_head = -1;
  51. }
  52. ringbuf->space = __intel_ring_space(ringbuf->head & HEAD_ADDR,
  53. ringbuf->tail, ringbuf->size);
  54. }
  55. static void __intel_ring_advance(struct intel_engine_cs *engine)
  56. {
  57. struct intel_ringbuffer *ringbuf = engine->buffer;
  58. ringbuf->tail &= ringbuf->size - 1;
  59. engine->write_tail(engine, ringbuf->tail);
  60. }
  61. static int
  62. gen2_render_ring_flush(struct drm_i915_gem_request *req,
  63. u32 invalidate_domains,
  64. u32 flush_domains)
  65. {
  66. struct intel_engine_cs *engine = req->engine;
  67. u32 cmd;
  68. int ret;
  69. cmd = MI_FLUSH;
  70. if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
  71. cmd |= MI_NO_WRITE_FLUSH;
  72. if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
  73. cmd |= MI_READ_FLUSH;
  74. ret = intel_ring_begin(req, 2);
  75. if (ret)
  76. return ret;
  77. intel_ring_emit(engine, cmd);
  78. intel_ring_emit(engine, MI_NOOP);
  79. intel_ring_advance(engine);
  80. return 0;
  81. }
  82. static int
  83. gen4_render_ring_flush(struct drm_i915_gem_request *req,
  84. u32 invalidate_domains,
  85. u32 flush_domains)
  86. {
  87. struct intel_engine_cs *engine = req->engine;
  88. u32 cmd;
  89. int ret;
  90. /*
  91. * read/write caches:
  92. *
  93. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  94. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  95. * also flushed at 2d versus 3d pipeline switches.
  96. *
  97. * read-only caches:
  98. *
  99. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  100. * MI_READ_FLUSH is set, and is always flushed on 965.
  101. *
  102. * I915_GEM_DOMAIN_COMMAND may not exist?
  103. *
  104. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  105. * invalidated when MI_EXE_FLUSH is set.
  106. *
  107. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  108. * invalidated with every MI_FLUSH.
  109. *
  110. * TLBs:
  111. *
  112. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  113. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  114. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  115. * are flushed at any MI_FLUSH.
  116. */
  117. cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  118. if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
  119. cmd &= ~MI_NO_WRITE_FLUSH;
  120. if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
  121. cmd |= MI_EXE_FLUSH;
  122. if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
  123. (IS_G4X(req->i915) || IS_GEN5(req->i915)))
  124. cmd |= MI_INVALIDATE_ISP;
  125. ret = intel_ring_begin(req, 2);
  126. if (ret)
  127. return ret;
  128. intel_ring_emit(engine, cmd);
  129. intel_ring_emit(engine, MI_NOOP);
  130. intel_ring_advance(engine);
  131. return 0;
  132. }
  133. /**
  134. * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
  135. * implementing two workarounds on gen6. From section 1.4.7.1
  136. * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
  137. *
  138. * [DevSNB-C+{W/A}] Before any depth stall flush (including those
  139. * produced by non-pipelined state commands), software needs to first
  140. * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
  141. * 0.
  142. *
  143. * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
  144. * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
  145. *
  146. * And the workaround for these two requires this workaround first:
  147. *
  148. * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
  149. * BEFORE the pipe-control with a post-sync op and no write-cache
  150. * flushes.
  151. *
  152. * And this last workaround is tricky because of the requirements on
  153. * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
  154. * volume 2 part 1:
  155. *
  156. * "1 of the following must also be set:
  157. * - Render Target Cache Flush Enable ([12] of DW1)
  158. * - Depth Cache Flush Enable ([0] of DW1)
  159. * - Stall at Pixel Scoreboard ([1] of DW1)
  160. * - Depth Stall ([13] of DW1)
  161. * - Post-Sync Operation ([13] of DW1)
  162. * - Notify Enable ([8] of DW1)"
  163. *
  164. * The cache flushes require the workaround flush that triggered this
  165. * one, so we can't use it. Depth stall would trigger the same.
  166. * Post-sync nonzero is what triggered this second workaround, so we
  167. * can't use that one either. Notify enable is IRQs, which aren't
  168. * really our business. That leaves only stall at scoreboard.
  169. */
  170. static int
  171. intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
  172. {
  173. struct intel_engine_cs *engine = req->engine;
  174. u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  175. int ret;
  176. ret = intel_ring_begin(req, 6);
  177. if (ret)
  178. return ret;
  179. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(5));
  180. intel_ring_emit(engine, PIPE_CONTROL_CS_STALL |
  181. PIPE_CONTROL_STALL_AT_SCOREBOARD);
  182. intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
  183. intel_ring_emit(engine, 0); /* low dword */
  184. intel_ring_emit(engine, 0); /* high dword */
  185. intel_ring_emit(engine, MI_NOOP);
  186. intel_ring_advance(engine);
  187. ret = intel_ring_begin(req, 6);
  188. if (ret)
  189. return ret;
  190. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(5));
  191. intel_ring_emit(engine, PIPE_CONTROL_QW_WRITE);
  192. intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
  193. intel_ring_emit(engine, 0);
  194. intel_ring_emit(engine, 0);
  195. intel_ring_emit(engine, MI_NOOP);
  196. intel_ring_advance(engine);
  197. return 0;
  198. }
  199. static int
  200. gen6_render_ring_flush(struct drm_i915_gem_request *req,
  201. u32 invalidate_domains, u32 flush_domains)
  202. {
  203. struct intel_engine_cs *engine = req->engine;
  204. u32 flags = 0;
  205. u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  206. int ret;
  207. /* Force SNB workarounds for PIPE_CONTROL flushes */
  208. ret = intel_emit_post_sync_nonzero_flush(req);
  209. if (ret)
  210. return ret;
  211. /* Just flush everything. Experiments have shown that reducing the
  212. * number of bits based on the write domains has little performance
  213. * impact.
  214. */
  215. if (flush_domains) {
  216. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  217. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  218. /*
  219. * Ensure that any following seqno writes only happen
  220. * when the render cache is indeed flushed.
  221. */
  222. flags |= PIPE_CONTROL_CS_STALL;
  223. }
  224. if (invalidate_domains) {
  225. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  226. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  227. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  228. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  229. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  230. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  231. /*
  232. * TLB invalidate requires a post-sync write.
  233. */
  234. flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
  235. }
  236. ret = intel_ring_begin(req, 4);
  237. if (ret)
  238. return ret;
  239. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
  240. intel_ring_emit(engine, flags);
  241. intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
  242. intel_ring_emit(engine, 0);
  243. intel_ring_advance(engine);
  244. return 0;
  245. }
  246. static int
  247. gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
  248. {
  249. struct intel_engine_cs *engine = req->engine;
  250. int ret;
  251. ret = intel_ring_begin(req, 4);
  252. if (ret)
  253. return ret;
  254. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
  255. intel_ring_emit(engine, PIPE_CONTROL_CS_STALL |
  256. PIPE_CONTROL_STALL_AT_SCOREBOARD);
  257. intel_ring_emit(engine, 0);
  258. intel_ring_emit(engine, 0);
  259. intel_ring_advance(engine);
  260. return 0;
  261. }
  262. static int
  263. gen7_render_ring_flush(struct drm_i915_gem_request *req,
  264. u32 invalidate_domains, u32 flush_domains)
  265. {
  266. struct intel_engine_cs *engine = req->engine;
  267. u32 flags = 0;
  268. u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  269. int ret;
  270. /*
  271. * Ensure that any following seqno writes only happen when the render
  272. * cache is indeed flushed.
  273. *
  274. * Workaround: 4th PIPE_CONTROL command (except the ones with only
  275. * read-cache invalidate bits set) must have the CS_STALL bit set. We
  276. * don't try to be clever and just set it unconditionally.
  277. */
  278. flags |= PIPE_CONTROL_CS_STALL;
  279. /* Just flush everything. Experiments have shown that reducing the
  280. * number of bits based on the write domains has little performance
  281. * impact.
  282. */
  283. if (flush_domains) {
  284. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  285. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  286. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  287. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  288. }
  289. if (invalidate_domains) {
  290. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  291. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  292. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  293. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  294. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  295. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  296. flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
  297. /*
  298. * TLB invalidate requires a post-sync write.
  299. */
  300. flags |= PIPE_CONTROL_QW_WRITE;
  301. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  302. flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
  303. /* Workaround: we must issue a pipe_control with CS-stall bit
  304. * set before a pipe_control command that has the state cache
  305. * invalidate bit set. */
  306. gen7_render_ring_cs_stall_wa(req);
  307. }
  308. ret = intel_ring_begin(req, 4);
  309. if (ret)
  310. return ret;
  311. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
  312. intel_ring_emit(engine, flags);
  313. intel_ring_emit(engine, scratch_addr);
  314. intel_ring_emit(engine, 0);
  315. intel_ring_advance(engine);
  316. return 0;
  317. }
  318. static int
  319. gen8_emit_pipe_control(struct drm_i915_gem_request *req,
  320. u32 flags, u32 scratch_addr)
  321. {
  322. struct intel_engine_cs *engine = req->engine;
  323. int ret;
  324. ret = intel_ring_begin(req, 6);
  325. if (ret)
  326. return ret;
  327. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(6));
  328. intel_ring_emit(engine, flags);
  329. intel_ring_emit(engine, scratch_addr);
  330. intel_ring_emit(engine, 0);
  331. intel_ring_emit(engine, 0);
  332. intel_ring_emit(engine, 0);
  333. intel_ring_advance(engine);
  334. return 0;
  335. }
  336. static int
  337. gen8_render_ring_flush(struct drm_i915_gem_request *req,
  338. u32 invalidate_domains, u32 flush_domains)
  339. {
  340. u32 flags = 0;
  341. u32 scratch_addr = req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  342. int ret;
  343. flags |= PIPE_CONTROL_CS_STALL;
  344. if (flush_domains) {
  345. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  346. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  347. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  348. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  349. }
  350. if (invalidate_domains) {
  351. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  352. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  353. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  354. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  355. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  356. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  357. flags |= PIPE_CONTROL_QW_WRITE;
  358. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  359. /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
  360. ret = gen8_emit_pipe_control(req,
  361. PIPE_CONTROL_CS_STALL |
  362. PIPE_CONTROL_STALL_AT_SCOREBOARD,
  363. 0);
  364. if (ret)
  365. return ret;
  366. }
  367. return gen8_emit_pipe_control(req, flags, scratch_addr);
  368. }
  369. static void ring_write_tail(struct intel_engine_cs *engine,
  370. u32 value)
  371. {
  372. struct drm_i915_private *dev_priv = engine->i915;
  373. I915_WRITE_TAIL(engine, value);
  374. }
  375. u64 intel_ring_get_active_head(struct intel_engine_cs *engine)
  376. {
  377. struct drm_i915_private *dev_priv = engine->i915;
  378. u64 acthd;
  379. if (INTEL_GEN(dev_priv) >= 8)
  380. acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
  381. RING_ACTHD_UDW(engine->mmio_base));
  382. else if (INTEL_GEN(dev_priv) >= 4)
  383. acthd = I915_READ(RING_ACTHD(engine->mmio_base));
  384. else
  385. acthd = I915_READ(ACTHD);
  386. return acthd;
  387. }
  388. static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
  389. {
  390. struct drm_i915_private *dev_priv = engine->i915;
  391. u32 addr;
  392. addr = dev_priv->status_page_dmah->busaddr;
  393. if (INTEL_GEN(dev_priv) >= 4)
  394. addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
  395. I915_WRITE(HWS_PGA, addr);
  396. }
  397. static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
  398. {
  399. struct drm_i915_private *dev_priv = engine->i915;
  400. i915_reg_t mmio;
  401. /* The ring status page addresses are no longer next to the rest of
  402. * the ring registers as of gen7.
  403. */
  404. if (IS_GEN7(dev_priv)) {
  405. switch (engine->id) {
  406. case RCS:
  407. mmio = RENDER_HWS_PGA_GEN7;
  408. break;
  409. case BCS:
  410. mmio = BLT_HWS_PGA_GEN7;
  411. break;
  412. /*
  413. * VCS2 actually doesn't exist on Gen7. Only shut up
  414. * gcc switch check warning
  415. */
  416. case VCS2:
  417. case VCS:
  418. mmio = BSD_HWS_PGA_GEN7;
  419. break;
  420. case VECS:
  421. mmio = VEBOX_HWS_PGA_GEN7;
  422. break;
  423. }
  424. } else if (IS_GEN6(dev_priv)) {
  425. mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
  426. } else {
  427. /* XXX: gen8 returns to sanity */
  428. mmio = RING_HWS_PGA(engine->mmio_base);
  429. }
  430. I915_WRITE(mmio, (u32)engine->status_page.gfx_addr);
  431. POSTING_READ(mmio);
  432. /*
  433. * Flush the TLB for this page
  434. *
  435. * FIXME: These two bits have disappeared on gen8, so a question
  436. * arises: do we still need this and if so how should we go about
  437. * invalidating the TLB?
  438. */
  439. if (IS_GEN(dev_priv, 6, 7)) {
  440. i915_reg_t reg = RING_INSTPM(engine->mmio_base);
  441. /* ring should be idle before issuing a sync flush*/
  442. WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
  443. I915_WRITE(reg,
  444. _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
  445. INSTPM_SYNC_FLUSH));
  446. if (intel_wait_for_register(dev_priv,
  447. reg, INSTPM_SYNC_FLUSH, 0,
  448. 1000))
  449. DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
  450. engine->name);
  451. }
  452. }
  453. static bool stop_ring(struct intel_engine_cs *engine)
  454. {
  455. struct drm_i915_private *dev_priv = engine->i915;
  456. if (!IS_GEN2(dev_priv)) {
  457. I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
  458. if (intel_wait_for_register(dev_priv,
  459. RING_MI_MODE(engine->mmio_base),
  460. MODE_IDLE,
  461. MODE_IDLE,
  462. 1000)) {
  463. DRM_ERROR("%s : timed out trying to stop ring\n",
  464. engine->name);
  465. /* Sometimes we observe that the idle flag is not
  466. * set even though the ring is empty. So double
  467. * check before giving up.
  468. */
  469. if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
  470. return false;
  471. }
  472. }
  473. I915_WRITE_CTL(engine, 0);
  474. I915_WRITE_HEAD(engine, 0);
  475. engine->write_tail(engine, 0);
  476. if (!IS_GEN2(dev_priv)) {
  477. (void)I915_READ_CTL(engine);
  478. I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
  479. }
  480. return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
  481. }
  482. static int init_ring_common(struct intel_engine_cs *engine)
  483. {
  484. struct drm_i915_private *dev_priv = engine->i915;
  485. struct intel_ringbuffer *ringbuf = engine->buffer;
  486. struct drm_i915_gem_object *obj = ringbuf->obj;
  487. int ret = 0;
  488. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  489. if (!stop_ring(engine)) {
  490. /* G45 ring initialization often fails to reset head to zero */
  491. DRM_DEBUG_KMS("%s head not reset to zero "
  492. "ctl %08x head %08x tail %08x start %08x\n",
  493. engine->name,
  494. I915_READ_CTL(engine),
  495. I915_READ_HEAD(engine),
  496. I915_READ_TAIL(engine),
  497. I915_READ_START(engine));
  498. if (!stop_ring(engine)) {
  499. DRM_ERROR("failed to set %s head to zero "
  500. "ctl %08x head %08x tail %08x start %08x\n",
  501. engine->name,
  502. I915_READ_CTL(engine),
  503. I915_READ_HEAD(engine),
  504. I915_READ_TAIL(engine),
  505. I915_READ_START(engine));
  506. ret = -EIO;
  507. goto out;
  508. }
  509. }
  510. if (I915_NEED_GFX_HWS(dev_priv))
  511. intel_ring_setup_status_page(engine);
  512. else
  513. ring_setup_phys_status_page(engine);
  514. /* Enforce ordering by reading HEAD register back */
  515. I915_READ_HEAD(engine);
  516. /* Initialize the ring. This must happen _after_ we've cleared the ring
  517. * registers with the above sequence (the readback of the HEAD registers
  518. * also enforces ordering), otherwise the hw might lose the new ring
  519. * register values. */
  520. I915_WRITE_START(engine, i915_gem_obj_ggtt_offset(obj));
  521. /* WaClearRingBufHeadRegAtInit:ctg,elk */
  522. if (I915_READ_HEAD(engine))
  523. DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
  524. engine->name, I915_READ_HEAD(engine));
  525. I915_WRITE_HEAD(engine, 0);
  526. (void)I915_READ_HEAD(engine);
  527. I915_WRITE_CTL(engine,
  528. ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
  529. | RING_VALID);
  530. /* If the head is still not zero, the ring is dead */
  531. if (wait_for((I915_READ_CTL(engine) & RING_VALID) != 0 &&
  532. I915_READ_START(engine) == i915_gem_obj_ggtt_offset(obj) &&
  533. (I915_READ_HEAD(engine) & HEAD_ADDR) == 0, 50)) {
  534. DRM_ERROR("%s initialization failed "
  535. "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
  536. engine->name,
  537. I915_READ_CTL(engine),
  538. I915_READ_CTL(engine) & RING_VALID,
  539. I915_READ_HEAD(engine), I915_READ_TAIL(engine),
  540. I915_READ_START(engine),
  541. (unsigned long)i915_gem_obj_ggtt_offset(obj));
  542. ret = -EIO;
  543. goto out;
  544. }
  545. ringbuf->last_retired_head = -1;
  546. ringbuf->head = I915_READ_HEAD(engine);
  547. ringbuf->tail = I915_READ_TAIL(engine) & TAIL_ADDR;
  548. intel_ring_update_space(ringbuf);
  549. intel_engine_init_hangcheck(engine);
  550. out:
  551. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  552. return ret;
  553. }
  554. void intel_fini_pipe_control(struct intel_engine_cs *engine)
  555. {
  556. if (engine->scratch.obj == NULL)
  557. return;
  558. i915_gem_object_ggtt_unpin(engine->scratch.obj);
  559. i915_gem_object_put(engine->scratch.obj);
  560. engine->scratch.obj = NULL;
  561. }
  562. int intel_init_pipe_control(struct intel_engine_cs *engine, int size)
  563. {
  564. struct drm_i915_gem_object *obj;
  565. int ret;
  566. WARN_ON(engine->scratch.obj);
  567. obj = i915_gem_object_create_stolen(&engine->i915->drm, size);
  568. if (!obj)
  569. obj = i915_gem_object_create(&engine->i915->drm, size);
  570. if (IS_ERR(obj)) {
  571. DRM_ERROR("Failed to allocate scratch page\n");
  572. ret = PTR_ERR(obj);
  573. goto err;
  574. }
  575. ret = i915_gem_obj_ggtt_pin(obj, 4096, PIN_HIGH);
  576. if (ret)
  577. goto err_unref;
  578. engine->scratch.obj = obj;
  579. engine->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
  580. DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
  581. engine->name, engine->scratch.gtt_offset);
  582. return 0;
  583. err_unref:
  584. i915_gem_object_put(engine->scratch.obj);
  585. err:
  586. return ret;
  587. }
  588. static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
  589. {
  590. struct intel_engine_cs *engine = req->engine;
  591. struct i915_workarounds *w = &req->i915->workarounds;
  592. int ret, i;
  593. if (w->count == 0)
  594. return 0;
  595. engine->gpu_caches_dirty = true;
  596. ret = intel_ring_flush_all_caches(req);
  597. if (ret)
  598. return ret;
  599. ret = intel_ring_begin(req, (w->count * 2 + 2));
  600. if (ret)
  601. return ret;
  602. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(w->count));
  603. for (i = 0; i < w->count; i++) {
  604. intel_ring_emit_reg(engine, w->reg[i].addr);
  605. intel_ring_emit(engine, w->reg[i].value);
  606. }
  607. intel_ring_emit(engine, MI_NOOP);
  608. intel_ring_advance(engine);
  609. engine->gpu_caches_dirty = true;
  610. ret = intel_ring_flush_all_caches(req);
  611. if (ret)
  612. return ret;
  613. DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
  614. return 0;
  615. }
  616. static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
  617. {
  618. int ret;
  619. ret = intel_ring_workarounds_emit(req);
  620. if (ret != 0)
  621. return ret;
  622. ret = i915_gem_render_state_init(req);
  623. if (ret)
  624. return ret;
  625. return 0;
  626. }
  627. static int wa_add(struct drm_i915_private *dev_priv,
  628. i915_reg_t addr,
  629. const u32 mask, const u32 val)
  630. {
  631. const u32 idx = dev_priv->workarounds.count;
  632. if (WARN_ON(idx >= I915_MAX_WA_REGS))
  633. return -ENOSPC;
  634. dev_priv->workarounds.reg[idx].addr = addr;
  635. dev_priv->workarounds.reg[idx].value = val;
  636. dev_priv->workarounds.reg[idx].mask = mask;
  637. dev_priv->workarounds.count++;
  638. return 0;
  639. }
  640. #define WA_REG(addr, mask, val) do { \
  641. const int r = wa_add(dev_priv, (addr), (mask), (val)); \
  642. if (r) \
  643. return r; \
  644. } while (0)
  645. #define WA_SET_BIT_MASKED(addr, mask) \
  646. WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
  647. #define WA_CLR_BIT_MASKED(addr, mask) \
  648. WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
  649. #define WA_SET_FIELD_MASKED(addr, mask, value) \
  650. WA_REG(addr, mask, _MASKED_FIELD(mask, value))
  651. #define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
  652. #define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
  653. #define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
  654. static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
  655. i915_reg_t reg)
  656. {
  657. struct drm_i915_private *dev_priv = engine->i915;
  658. struct i915_workarounds *wa = &dev_priv->workarounds;
  659. const uint32_t index = wa->hw_whitelist_count[engine->id];
  660. if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
  661. return -EINVAL;
  662. WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
  663. i915_mmio_reg_offset(reg));
  664. wa->hw_whitelist_count[engine->id]++;
  665. return 0;
  666. }
  667. static int gen8_init_workarounds(struct intel_engine_cs *engine)
  668. {
  669. struct drm_i915_private *dev_priv = engine->i915;
  670. WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
  671. /* WaDisableAsyncFlipPerfMode:bdw,chv */
  672. WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
  673. /* WaDisablePartialInstShootdown:bdw,chv */
  674. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
  675. PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
  676. /* Use Force Non-Coherent whenever executing a 3D context. This is a
  677. * workaround for for a possible hang in the unlikely event a TLB
  678. * invalidation occurs during a PSD flush.
  679. */
  680. /* WaForceEnableNonCoherent:bdw,chv */
  681. /* WaHdcDisableFetchWhenMasked:bdw,chv */
  682. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  683. HDC_DONOT_FETCH_MEM_WHEN_MASKED |
  684. HDC_FORCE_NON_COHERENT);
  685. /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
  686. * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
  687. * polygons in the same 8x4 pixel/sample area to be processed without
  688. * stalling waiting for the earlier ones to write to Hierarchical Z
  689. * buffer."
  690. *
  691. * This optimization is off by default for BDW and CHV; turn it on.
  692. */
  693. WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
  694. /* Wa4x4STCOptimizationDisable:bdw,chv */
  695. WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
  696. /*
  697. * BSpec recommends 8x4 when MSAA is used,
  698. * however in practice 16x4 seems fastest.
  699. *
  700. * Note that PS/WM thread counts depend on the WIZ hashing
  701. * disable bit, which we don't touch here, but it's good
  702. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  703. */
  704. WA_SET_FIELD_MASKED(GEN7_GT_MODE,
  705. GEN6_WIZ_HASHING_MASK,
  706. GEN6_WIZ_HASHING_16x4);
  707. return 0;
  708. }
  709. static int bdw_init_workarounds(struct intel_engine_cs *engine)
  710. {
  711. struct drm_i915_private *dev_priv = engine->i915;
  712. int ret;
  713. ret = gen8_init_workarounds(engine);
  714. if (ret)
  715. return ret;
  716. /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
  717. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
  718. /* WaDisableDopClockGating:bdw */
  719. WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
  720. DOP_CLOCK_GATING_DISABLE);
  721. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
  722. GEN8_SAMPLER_POWER_BYPASS_DIS);
  723. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  724. /* WaForceContextSaveRestoreNonCoherent:bdw */
  725. HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
  726. /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
  727. (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
  728. return 0;
  729. }
  730. static int chv_init_workarounds(struct intel_engine_cs *engine)
  731. {
  732. struct drm_i915_private *dev_priv = engine->i915;
  733. int ret;
  734. ret = gen8_init_workarounds(engine);
  735. if (ret)
  736. return ret;
  737. /* WaDisableThreadStallDopClockGating:chv */
  738. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
  739. /* Improve HiZ throughput on CHV. */
  740. WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
  741. return 0;
  742. }
  743. static int gen9_init_workarounds(struct intel_engine_cs *engine)
  744. {
  745. struct drm_i915_private *dev_priv = engine->i915;
  746. int ret;
  747. /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl */
  748. I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
  749. /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl */
  750. I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
  751. GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
  752. /* WaDisableKillLogic:bxt,skl,kbl */
  753. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
  754. ECOCHK_DIS_TLB);
  755. /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl */
  756. /* WaDisablePartialInstShootdown:skl,bxt,kbl */
  757. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
  758. FLOW_CONTROL_ENABLE |
  759. PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
  760. /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
  761. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
  762. GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
  763. /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
  764. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
  765. IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
  766. WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
  767. GEN9_DG_MIRROR_FIX_ENABLE);
  768. /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
  769. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
  770. IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
  771. WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
  772. GEN9_RHWO_OPTIMIZATION_DISABLE);
  773. /*
  774. * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
  775. * but we do that in per ctx batchbuffer as there is an issue
  776. * with this register not getting restored on ctx restore
  777. */
  778. }
  779. /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl */
  780. /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
  781. WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
  782. GEN9_ENABLE_YV12_BUGFIX |
  783. GEN9_ENABLE_GPGPU_PREEMPTION);
  784. /* Wa4x4STCOptimizationDisable:skl,bxt,kbl */
  785. /* WaDisablePartialResolveInVc:skl,bxt,kbl */
  786. WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
  787. GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
  788. /* WaCcsTlbPrefetchDisable:skl,bxt,kbl */
  789. WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
  790. GEN9_CCS_TLB_PREFETCH_ENABLE);
  791. /* WaDisableMaskBasedCammingInRCC:skl,bxt */
  792. if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_C0) ||
  793. IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
  794. WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
  795. PIXEL_MASK_CAMMING_DISABLE);
  796. /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
  797. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  798. HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
  799. HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
  800. /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
  801. * both tied to WaForceContextSaveRestoreNonCoherent
  802. * in some hsds for skl. We keep the tie for all gen9. The
  803. * documentation is a bit hazy and so we want to get common behaviour,
  804. * even though there is no clear evidence we would need both on kbl/bxt.
  805. * This area has been source of system hangs so we play it safe
  806. * and mimic the skl regardless of what bspec says.
  807. *
  808. * Use Force Non-Coherent whenever executing a 3D context. This
  809. * is a workaround for a possible hang in the unlikely event
  810. * a TLB invalidation occurs during a PSD flush.
  811. */
  812. /* WaForceEnableNonCoherent:skl,bxt,kbl */
  813. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  814. HDC_FORCE_NON_COHERENT);
  815. /* WaDisableHDCInvalidation:skl,bxt,kbl */
  816. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
  817. BDW_DISABLE_HDC_INVALIDATION);
  818. /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
  819. if (IS_SKYLAKE(dev_priv) ||
  820. IS_KABYLAKE(dev_priv) ||
  821. IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
  822. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
  823. GEN8_SAMPLER_POWER_BYPASS_DIS);
  824. /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl */
  825. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
  826. /* WaOCLCoherentLineFlush:skl,bxt,kbl */
  827. I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
  828. GEN8_LQSC_FLUSH_COHERENT_LINES));
  829. /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt */
  830. ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
  831. if (ret)
  832. return ret;
  833. /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
  834. ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
  835. if (ret)
  836. return ret;
  837. /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl */
  838. ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
  839. if (ret)
  840. return ret;
  841. return 0;
  842. }
  843. static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
  844. {
  845. struct drm_i915_private *dev_priv = engine->i915;
  846. u8 vals[3] = { 0, 0, 0 };
  847. unsigned int i;
  848. for (i = 0; i < 3; i++) {
  849. u8 ss;
  850. /*
  851. * Only consider slices where one, and only one, subslice has 7
  852. * EUs
  853. */
  854. if (!is_power_of_2(dev_priv->info.subslice_7eu[i]))
  855. continue;
  856. /*
  857. * subslice_7eu[i] != 0 (because of the check above) and
  858. * ss_max == 4 (maximum number of subslices possible per slice)
  859. *
  860. * -> 0 <= ss <= 3;
  861. */
  862. ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
  863. vals[i] = 3 - ss;
  864. }
  865. if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
  866. return 0;
  867. /* Tune IZ hashing. See intel_device_info_runtime_init() */
  868. WA_SET_FIELD_MASKED(GEN7_GT_MODE,
  869. GEN9_IZ_HASHING_MASK(2) |
  870. GEN9_IZ_HASHING_MASK(1) |
  871. GEN9_IZ_HASHING_MASK(0),
  872. GEN9_IZ_HASHING(2, vals[2]) |
  873. GEN9_IZ_HASHING(1, vals[1]) |
  874. GEN9_IZ_HASHING(0, vals[0]));
  875. return 0;
  876. }
  877. static int skl_init_workarounds(struct intel_engine_cs *engine)
  878. {
  879. struct drm_i915_private *dev_priv = engine->i915;
  880. int ret;
  881. ret = gen9_init_workarounds(engine);
  882. if (ret)
  883. return ret;
  884. /*
  885. * Actual WA is to disable percontext preemption granularity control
  886. * until D0 which is the default case so this is equivalent to
  887. * !WaDisablePerCtxtPreemptionGranularityControl:skl
  888. */
  889. if (IS_SKL_REVID(dev_priv, SKL_REVID_E0, REVID_FOREVER)) {
  890. I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
  891. _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
  892. }
  893. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0)) {
  894. /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
  895. I915_WRITE(FF_SLICE_CS_CHICKEN2,
  896. _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
  897. }
  898. /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
  899. * involving this register should also be added to WA batch as required.
  900. */
  901. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0))
  902. /* WaDisableLSQCROPERFforOCL:skl */
  903. I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
  904. GEN8_LQSC_RO_PERF_DIS);
  905. /* WaEnableGapsTsvCreditFix:skl */
  906. if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, REVID_FOREVER)) {
  907. I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
  908. GEN9_GAPS_TSV_CREDIT_DISABLE));
  909. }
  910. /* WaDisablePowerCompilerClockGating:skl */
  911. if (IS_SKL_REVID(dev_priv, SKL_REVID_B0, SKL_REVID_B0))
  912. WA_SET_BIT_MASKED(HIZ_CHICKEN,
  913. BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
  914. /* WaBarrierPerformanceFixDisable:skl */
  915. if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_D0))
  916. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  917. HDC_FENCE_DEST_SLM_DISABLE |
  918. HDC_BARRIER_PERFORMANCE_DISABLE);
  919. /* WaDisableSbeCacheDispatchPortSharing:skl */
  920. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0))
  921. WA_SET_BIT_MASKED(
  922. GEN7_HALF_SLICE_CHICKEN1,
  923. GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
  924. /* WaDisableGafsUnitClkGating:skl */
  925. WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
  926. /* WaInPlaceDecompressionHang:skl */
  927. if (IS_SKL_REVID(dev_priv, SKL_REVID_H0, REVID_FOREVER))
  928. WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
  929. GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
  930. /* WaDisableLSQCROPERFforOCL:skl */
  931. ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
  932. if (ret)
  933. return ret;
  934. return skl_tune_iz_hashing(engine);
  935. }
  936. static int bxt_init_workarounds(struct intel_engine_cs *engine)
  937. {
  938. struct drm_i915_private *dev_priv = engine->i915;
  939. int ret;
  940. ret = gen9_init_workarounds(engine);
  941. if (ret)
  942. return ret;
  943. /* WaStoreMultiplePTEenable:bxt */
  944. /* This is a requirement according to Hardware specification */
  945. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
  946. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
  947. /* WaSetClckGatingDisableMedia:bxt */
  948. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
  949. I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
  950. ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
  951. }
  952. /* WaDisableThreadStallDopClockGating:bxt */
  953. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
  954. STALL_DOP_GATING_DISABLE);
  955. /* WaDisablePooledEuLoadBalancingFix:bxt */
  956. if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
  957. WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
  958. GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
  959. }
  960. /* WaDisableSbeCacheDispatchPortSharing:bxt */
  961. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
  962. WA_SET_BIT_MASKED(
  963. GEN7_HALF_SLICE_CHICKEN1,
  964. GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
  965. }
  966. /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
  967. /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
  968. /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
  969. /* WaDisableLSQCROPERFforOCL:bxt */
  970. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
  971. ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
  972. if (ret)
  973. return ret;
  974. ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
  975. if (ret)
  976. return ret;
  977. }
  978. /* WaProgramL3SqcReg1DefaultForPerf:bxt */
  979. if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
  980. I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
  981. L3_HIGH_PRIO_CREDITS(2));
  982. /* WaInsertDummyPushConstPs:bxt */
  983. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
  984. WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
  985. GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
  986. /* WaInPlaceDecompressionHang:bxt */
  987. if (IS_BXT_REVID(dev_priv, BXT_REVID_C0, REVID_FOREVER))
  988. WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
  989. GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
  990. return 0;
  991. }
  992. static int kbl_init_workarounds(struct intel_engine_cs *engine)
  993. {
  994. struct drm_i915_private *dev_priv = engine->i915;
  995. int ret;
  996. ret = gen9_init_workarounds(engine);
  997. if (ret)
  998. return ret;
  999. /* WaEnableGapsTsvCreditFix:kbl */
  1000. I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
  1001. GEN9_GAPS_TSV_CREDIT_DISABLE));
  1002. /* WaDisableDynamicCreditSharing:kbl */
  1003. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
  1004. WA_SET_BIT(GAMT_CHKN_BIT_REG,
  1005. GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
  1006. /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
  1007. if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
  1008. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  1009. HDC_FENCE_DEST_SLM_DISABLE);
  1010. /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
  1011. * involving this register should also be added to WA batch as required.
  1012. */
  1013. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
  1014. /* WaDisableLSQCROPERFforOCL:kbl */
  1015. I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
  1016. GEN8_LQSC_RO_PERF_DIS);
  1017. /* WaInsertDummyPushConstPs:kbl */
  1018. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
  1019. WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
  1020. GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
  1021. /* WaDisableGafsUnitClkGating:kbl */
  1022. WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
  1023. /* WaDisableSbeCacheDispatchPortSharing:kbl */
  1024. WA_SET_BIT_MASKED(
  1025. GEN7_HALF_SLICE_CHICKEN1,
  1026. GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
  1027. /* WaInPlaceDecompressionHang:kbl */
  1028. WA_SET_BIT(GEN9_GAMT_ECO_REG_RW_IA,
  1029. GAMT_ECO_ENABLE_IN_PLACE_DECOMPRESS);
  1030. /* WaDisableLSQCROPERFforOCL:kbl */
  1031. ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
  1032. if (ret)
  1033. return ret;
  1034. return 0;
  1035. }
  1036. int init_workarounds_ring(struct intel_engine_cs *engine)
  1037. {
  1038. struct drm_i915_private *dev_priv = engine->i915;
  1039. WARN_ON(engine->id != RCS);
  1040. dev_priv->workarounds.count = 0;
  1041. dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
  1042. if (IS_BROADWELL(dev_priv))
  1043. return bdw_init_workarounds(engine);
  1044. if (IS_CHERRYVIEW(dev_priv))
  1045. return chv_init_workarounds(engine);
  1046. if (IS_SKYLAKE(dev_priv))
  1047. return skl_init_workarounds(engine);
  1048. if (IS_BROXTON(dev_priv))
  1049. return bxt_init_workarounds(engine);
  1050. if (IS_KABYLAKE(dev_priv))
  1051. return kbl_init_workarounds(engine);
  1052. return 0;
  1053. }
  1054. static int init_render_ring(struct intel_engine_cs *engine)
  1055. {
  1056. struct drm_i915_private *dev_priv = engine->i915;
  1057. int ret = init_ring_common(engine);
  1058. if (ret)
  1059. return ret;
  1060. /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
  1061. if (IS_GEN(dev_priv, 4, 6))
  1062. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
  1063. /* We need to disable the AsyncFlip performance optimisations in order
  1064. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  1065. * programmed to '1' on all products.
  1066. *
  1067. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
  1068. */
  1069. if (IS_GEN(dev_priv, 6, 7))
  1070. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  1071. /* Required for the hardware to program scanline values for waiting */
  1072. /* WaEnableFlushTlbInvalidationMode:snb */
  1073. if (IS_GEN6(dev_priv))
  1074. I915_WRITE(GFX_MODE,
  1075. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
  1076. /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
  1077. if (IS_GEN7(dev_priv))
  1078. I915_WRITE(GFX_MODE_GEN7,
  1079. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
  1080. _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
  1081. if (IS_GEN6(dev_priv)) {
  1082. /* From the Sandybridge PRM, volume 1 part 3, page 24:
  1083. * "If this bit is set, STCunit will have LRA as replacement
  1084. * policy. [...] This bit must be reset. LRA replacement
  1085. * policy is not supported."
  1086. */
  1087. I915_WRITE(CACHE_MODE_0,
  1088. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  1089. }
  1090. if (IS_GEN(dev_priv, 6, 7))
  1091. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  1092. if (INTEL_INFO(dev_priv)->gen >= 6)
  1093. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  1094. return init_workarounds_ring(engine);
  1095. }
  1096. static void render_ring_cleanup(struct intel_engine_cs *engine)
  1097. {
  1098. struct drm_i915_private *dev_priv = engine->i915;
  1099. if (dev_priv->semaphore_obj) {
  1100. i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
  1101. i915_gem_object_put(dev_priv->semaphore_obj);
  1102. dev_priv->semaphore_obj = NULL;
  1103. }
  1104. intel_fini_pipe_control(engine);
  1105. }
  1106. static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
  1107. unsigned int num_dwords)
  1108. {
  1109. #define MBOX_UPDATE_DWORDS 8
  1110. struct intel_engine_cs *signaller = signaller_req->engine;
  1111. struct drm_i915_private *dev_priv = signaller_req->i915;
  1112. struct intel_engine_cs *waiter;
  1113. enum intel_engine_id id;
  1114. int ret, num_rings;
  1115. num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
  1116. num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
  1117. #undef MBOX_UPDATE_DWORDS
  1118. ret = intel_ring_begin(signaller_req, num_dwords);
  1119. if (ret)
  1120. return ret;
  1121. for_each_engine_id(waiter, dev_priv, id) {
  1122. u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
  1123. if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
  1124. continue;
  1125. intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
  1126. intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
  1127. PIPE_CONTROL_QW_WRITE |
  1128. PIPE_CONTROL_CS_STALL);
  1129. intel_ring_emit(signaller, lower_32_bits(gtt_offset));
  1130. intel_ring_emit(signaller, upper_32_bits(gtt_offset));
  1131. intel_ring_emit(signaller, signaller_req->fence.seqno);
  1132. intel_ring_emit(signaller, 0);
  1133. intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
  1134. MI_SEMAPHORE_TARGET(waiter->hw_id));
  1135. intel_ring_emit(signaller, 0);
  1136. }
  1137. return 0;
  1138. }
  1139. static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
  1140. unsigned int num_dwords)
  1141. {
  1142. #define MBOX_UPDATE_DWORDS 6
  1143. struct intel_engine_cs *signaller = signaller_req->engine;
  1144. struct drm_i915_private *dev_priv = signaller_req->i915;
  1145. struct intel_engine_cs *waiter;
  1146. enum intel_engine_id id;
  1147. int ret, num_rings;
  1148. num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
  1149. num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
  1150. #undef MBOX_UPDATE_DWORDS
  1151. ret = intel_ring_begin(signaller_req, num_dwords);
  1152. if (ret)
  1153. return ret;
  1154. for_each_engine_id(waiter, dev_priv, id) {
  1155. u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
  1156. if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
  1157. continue;
  1158. intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
  1159. MI_FLUSH_DW_OP_STOREDW);
  1160. intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
  1161. MI_FLUSH_DW_USE_GTT);
  1162. intel_ring_emit(signaller, upper_32_bits(gtt_offset));
  1163. intel_ring_emit(signaller, signaller_req->fence.seqno);
  1164. intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
  1165. MI_SEMAPHORE_TARGET(waiter->hw_id));
  1166. intel_ring_emit(signaller, 0);
  1167. }
  1168. return 0;
  1169. }
  1170. static int gen6_signal(struct drm_i915_gem_request *signaller_req,
  1171. unsigned int num_dwords)
  1172. {
  1173. struct intel_engine_cs *signaller = signaller_req->engine;
  1174. struct drm_i915_private *dev_priv = signaller_req->i915;
  1175. struct intel_engine_cs *useless;
  1176. enum intel_engine_id id;
  1177. int ret, num_rings;
  1178. #define MBOX_UPDATE_DWORDS 3
  1179. num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
  1180. num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
  1181. #undef MBOX_UPDATE_DWORDS
  1182. ret = intel_ring_begin(signaller_req, num_dwords);
  1183. if (ret)
  1184. return ret;
  1185. for_each_engine_id(useless, dev_priv, id) {
  1186. i915_reg_t mbox_reg = signaller->semaphore.mbox.signal[id];
  1187. if (i915_mmio_reg_valid(mbox_reg)) {
  1188. intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
  1189. intel_ring_emit_reg(signaller, mbox_reg);
  1190. intel_ring_emit(signaller, signaller_req->fence.seqno);
  1191. }
  1192. }
  1193. /* If num_dwords was rounded, make sure the tail pointer is correct */
  1194. if (num_rings % 2 == 0)
  1195. intel_ring_emit(signaller, MI_NOOP);
  1196. return 0;
  1197. }
  1198. /**
  1199. * gen6_add_request - Update the semaphore mailbox registers
  1200. *
  1201. * @request - request to write to the ring
  1202. *
  1203. * Update the mailbox registers in the *other* rings with the current seqno.
  1204. * This acts like a signal in the canonical semaphore.
  1205. */
  1206. static int
  1207. gen6_add_request(struct drm_i915_gem_request *req)
  1208. {
  1209. struct intel_engine_cs *engine = req->engine;
  1210. int ret;
  1211. if (engine->semaphore.signal)
  1212. ret = engine->semaphore.signal(req, 4);
  1213. else
  1214. ret = intel_ring_begin(req, 4);
  1215. if (ret)
  1216. return ret;
  1217. intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
  1218. intel_ring_emit(engine,
  1219. I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  1220. intel_ring_emit(engine, req->fence.seqno);
  1221. intel_ring_emit(engine, MI_USER_INTERRUPT);
  1222. __intel_ring_advance(engine);
  1223. return 0;
  1224. }
  1225. static int
  1226. gen8_render_add_request(struct drm_i915_gem_request *req)
  1227. {
  1228. struct intel_engine_cs *engine = req->engine;
  1229. int ret;
  1230. if (engine->semaphore.signal)
  1231. ret = engine->semaphore.signal(req, 8);
  1232. else
  1233. ret = intel_ring_begin(req, 8);
  1234. if (ret)
  1235. return ret;
  1236. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(6));
  1237. intel_ring_emit(engine, (PIPE_CONTROL_GLOBAL_GTT_IVB |
  1238. PIPE_CONTROL_CS_STALL |
  1239. PIPE_CONTROL_QW_WRITE));
  1240. intel_ring_emit(engine, intel_hws_seqno_address(req->engine));
  1241. intel_ring_emit(engine, 0);
  1242. intel_ring_emit(engine, i915_gem_request_get_seqno(req));
  1243. /* We're thrashing one dword of HWS. */
  1244. intel_ring_emit(engine, 0);
  1245. intel_ring_emit(engine, MI_USER_INTERRUPT);
  1246. intel_ring_emit(engine, MI_NOOP);
  1247. __intel_ring_advance(engine);
  1248. return 0;
  1249. }
  1250. static inline bool i915_gem_has_seqno_wrapped(struct drm_i915_private *dev_priv,
  1251. u32 seqno)
  1252. {
  1253. return dev_priv->last_seqno < seqno;
  1254. }
  1255. /**
  1256. * intel_ring_sync - sync the waiter to the signaller on seqno
  1257. *
  1258. * @waiter - ring that is waiting
  1259. * @signaller - ring which has, or will signal
  1260. * @seqno - seqno which the waiter will block on
  1261. */
  1262. static int
  1263. gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
  1264. struct intel_engine_cs *signaller,
  1265. u32 seqno)
  1266. {
  1267. struct intel_engine_cs *waiter = waiter_req->engine;
  1268. struct drm_i915_private *dev_priv = waiter_req->i915;
  1269. u64 offset = GEN8_WAIT_OFFSET(waiter, signaller->id);
  1270. struct i915_hw_ppgtt *ppgtt;
  1271. int ret;
  1272. ret = intel_ring_begin(waiter_req, 4);
  1273. if (ret)
  1274. return ret;
  1275. intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
  1276. MI_SEMAPHORE_GLOBAL_GTT |
  1277. MI_SEMAPHORE_SAD_GTE_SDD);
  1278. intel_ring_emit(waiter, seqno);
  1279. intel_ring_emit(waiter, lower_32_bits(offset));
  1280. intel_ring_emit(waiter, upper_32_bits(offset));
  1281. intel_ring_advance(waiter);
  1282. /* When the !RCS engines idle waiting upon a semaphore, they lose their
  1283. * pagetables and we must reload them before executing the batch.
  1284. * We do this on the i915_switch_context() following the wait and
  1285. * before the dispatch.
  1286. */
  1287. ppgtt = waiter_req->ctx->ppgtt;
  1288. if (ppgtt && waiter_req->engine->id != RCS)
  1289. ppgtt->pd_dirty_rings |= intel_engine_flag(waiter_req->engine);
  1290. return 0;
  1291. }
  1292. static int
  1293. gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
  1294. struct intel_engine_cs *signaller,
  1295. u32 seqno)
  1296. {
  1297. struct intel_engine_cs *waiter = waiter_req->engine;
  1298. u32 dw1 = MI_SEMAPHORE_MBOX |
  1299. MI_SEMAPHORE_COMPARE |
  1300. MI_SEMAPHORE_REGISTER;
  1301. u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
  1302. int ret;
  1303. /* Throughout all of the GEM code, seqno passed implies our current
  1304. * seqno is >= the last seqno executed. However for hardware the
  1305. * comparison is strictly greater than.
  1306. */
  1307. seqno -= 1;
  1308. WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
  1309. ret = intel_ring_begin(waiter_req, 4);
  1310. if (ret)
  1311. return ret;
  1312. /* If seqno wrap happened, omit the wait with no-ops */
  1313. if (likely(!i915_gem_has_seqno_wrapped(waiter_req->i915, seqno))) {
  1314. intel_ring_emit(waiter, dw1 | wait_mbox);
  1315. intel_ring_emit(waiter, seqno);
  1316. intel_ring_emit(waiter, 0);
  1317. intel_ring_emit(waiter, MI_NOOP);
  1318. } else {
  1319. intel_ring_emit(waiter, MI_NOOP);
  1320. intel_ring_emit(waiter, MI_NOOP);
  1321. intel_ring_emit(waiter, MI_NOOP);
  1322. intel_ring_emit(waiter, MI_NOOP);
  1323. }
  1324. intel_ring_advance(waiter);
  1325. return 0;
  1326. }
  1327. static void
  1328. gen5_seqno_barrier(struct intel_engine_cs *ring)
  1329. {
  1330. /* MI_STORE are internally buffered by the GPU and not flushed
  1331. * either by MI_FLUSH or SyncFlush or any other combination of
  1332. * MI commands.
  1333. *
  1334. * "Only the submission of the store operation is guaranteed.
  1335. * The write result will be complete (coherent) some time later
  1336. * (this is practically a finite period but there is no guaranteed
  1337. * latency)."
  1338. *
  1339. * Empirically, we observe that we need a delay of at least 75us to
  1340. * be sure that the seqno write is visible by the CPU.
  1341. */
  1342. usleep_range(125, 250);
  1343. }
  1344. static void
  1345. gen6_seqno_barrier(struct intel_engine_cs *engine)
  1346. {
  1347. struct drm_i915_private *dev_priv = engine->i915;
  1348. /* Workaround to force correct ordering between irq and seqno writes on
  1349. * ivb (and maybe also on snb) by reading from a CS register (like
  1350. * ACTHD) before reading the status page.
  1351. *
  1352. * Note that this effectively stalls the read by the time it takes to
  1353. * do a memory transaction, which more or less ensures that the write
  1354. * from the GPU has sufficient time to invalidate the CPU cacheline.
  1355. * Alternatively we could delay the interrupt from the CS ring to give
  1356. * the write time to land, but that would incur a delay after every
  1357. * batch i.e. much more frequent than a delay when waiting for the
  1358. * interrupt (with the same net latency).
  1359. *
  1360. * Also note that to prevent whole machine hangs on gen7, we have to
  1361. * take the spinlock to guard against concurrent cacheline access.
  1362. */
  1363. spin_lock_irq(&dev_priv->uncore.lock);
  1364. POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
  1365. spin_unlock_irq(&dev_priv->uncore.lock);
  1366. }
  1367. static void
  1368. gen5_irq_enable(struct intel_engine_cs *engine)
  1369. {
  1370. gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
  1371. }
  1372. static void
  1373. gen5_irq_disable(struct intel_engine_cs *engine)
  1374. {
  1375. gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
  1376. }
  1377. static void
  1378. i9xx_irq_enable(struct intel_engine_cs *engine)
  1379. {
  1380. struct drm_i915_private *dev_priv = engine->i915;
  1381. dev_priv->irq_mask &= ~engine->irq_enable_mask;
  1382. I915_WRITE(IMR, dev_priv->irq_mask);
  1383. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1384. }
  1385. static void
  1386. i9xx_irq_disable(struct intel_engine_cs *engine)
  1387. {
  1388. struct drm_i915_private *dev_priv = engine->i915;
  1389. dev_priv->irq_mask |= engine->irq_enable_mask;
  1390. I915_WRITE(IMR, dev_priv->irq_mask);
  1391. }
  1392. static void
  1393. i8xx_irq_enable(struct intel_engine_cs *engine)
  1394. {
  1395. struct drm_i915_private *dev_priv = engine->i915;
  1396. dev_priv->irq_mask &= ~engine->irq_enable_mask;
  1397. I915_WRITE16(IMR, dev_priv->irq_mask);
  1398. POSTING_READ16(RING_IMR(engine->mmio_base));
  1399. }
  1400. static void
  1401. i8xx_irq_disable(struct intel_engine_cs *engine)
  1402. {
  1403. struct drm_i915_private *dev_priv = engine->i915;
  1404. dev_priv->irq_mask |= engine->irq_enable_mask;
  1405. I915_WRITE16(IMR, dev_priv->irq_mask);
  1406. }
  1407. static int
  1408. bsd_ring_flush(struct drm_i915_gem_request *req,
  1409. u32 invalidate_domains,
  1410. u32 flush_domains)
  1411. {
  1412. struct intel_engine_cs *engine = req->engine;
  1413. int ret;
  1414. ret = intel_ring_begin(req, 2);
  1415. if (ret)
  1416. return ret;
  1417. intel_ring_emit(engine, MI_FLUSH);
  1418. intel_ring_emit(engine, MI_NOOP);
  1419. intel_ring_advance(engine);
  1420. return 0;
  1421. }
  1422. static int
  1423. i9xx_add_request(struct drm_i915_gem_request *req)
  1424. {
  1425. struct intel_engine_cs *engine = req->engine;
  1426. int ret;
  1427. ret = intel_ring_begin(req, 4);
  1428. if (ret)
  1429. return ret;
  1430. intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
  1431. intel_ring_emit(engine,
  1432. I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  1433. intel_ring_emit(engine, req->fence.seqno);
  1434. intel_ring_emit(engine, MI_USER_INTERRUPT);
  1435. __intel_ring_advance(engine);
  1436. return 0;
  1437. }
  1438. static void
  1439. gen6_irq_enable(struct intel_engine_cs *engine)
  1440. {
  1441. struct drm_i915_private *dev_priv = engine->i915;
  1442. I915_WRITE_IMR(engine,
  1443. ~(engine->irq_enable_mask |
  1444. engine->irq_keep_mask));
  1445. gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
  1446. }
  1447. static void
  1448. gen6_irq_disable(struct intel_engine_cs *engine)
  1449. {
  1450. struct drm_i915_private *dev_priv = engine->i915;
  1451. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  1452. gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
  1453. }
  1454. static void
  1455. hsw_vebox_irq_enable(struct intel_engine_cs *engine)
  1456. {
  1457. struct drm_i915_private *dev_priv = engine->i915;
  1458. I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
  1459. gen6_enable_pm_irq(dev_priv, engine->irq_enable_mask);
  1460. }
  1461. static void
  1462. hsw_vebox_irq_disable(struct intel_engine_cs *engine)
  1463. {
  1464. struct drm_i915_private *dev_priv = engine->i915;
  1465. I915_WRITE_IMR(engine, ~0);
  1466. gen6_disable_pm_irq(dev_priv, engine->irq_enable_mask);
  1467. }
  1468. static void
  1469. gen8_irq_enable(struct intel_engine_cs *engine)
  1470. {
  1471. struct drm_i915_private *dev_priv = engine->i915;
  1472. I915_WRITE_IMR(engine,
  1473. ~(engine->irq_enable_mask |
  1474. engine->irq_keep_mask));
  1475. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1476. }
  1477. static void
  1478. gen8_irq_disable(struct intel_engine_cs *engine)
  1479. {
  1480. struct drm_i915_private *dev_priv = engine->i915;
  1481. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  1482. }
  1483. static int
  1484. i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
  1485. u64 offset, u32 length,
  1486. unsigned dispatch_flags)
  1487. {
  1488. struct intel_engine_cs *engine = req->engine;
  1489. int ret;
  1490. ret = intel_ring_begin(req, 2);
  1491. if (ret)
  1492. return ret;
  1493. intel_ring_emit(engine,
  1494. MI_BATCH_BUFFER_START |
  1495. MI_BATCH_GTT |
  1496. (dispatch_flags & I915_DISPATCH_SECURE ?
  1497. 0 : MI_BATCH_NON_SECURE_I965));
  1498. intel_ring_emit(engine, offset);
  1499. intel_ring_advance(engine);
  1500. return 0;
  1501. }
  1502. /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
  1503. #define I830_BATCH_LIMIT (256*1024)
  1504. #define I830_TLB_ENTRIES (2)
  1505. #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
  1506. static int
  1507. i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
  1508. u64 offset, u32 len,
  1509. unsigned dispatch_flags)
  1510. {
  1511. struct intel_engine_cs *engine = req->engine;
  1512. u32 cs_offset = engine->scratch.gtt_offset;
  1513. int ret;
  1514. ret = intel_ring_begin(req, 6);
  1515. if (ret)
  1516. return ret;
  1517. /* Evict the invalid PTE TLBs */
  1518. intel_ring_emit(engine, COLOR_BLT_CMD | BLT_WRITE_RGBA);
  1519. intel_ring_emit(engine, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
  1520. intel_ring_emit(engine, I830_TLB_ENTRIES << 16 | 4); /* load each page */
  1521. intel_ring_emit(engine, cs_offset);
  1522. intel_ring_emit(engine, 0xdeadbeef);
  1523. intel_ring_emit(engine, MI_NOOP);
  1524. intel_ring_advance(engine);
  1525. if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
  1526. if (len > I830_BATCH_LIMIT)
  1527. return -ENOSPC;
  1528. ret = intel_ring_begin(req, 6 + 2);
  1529. if (ret)
  1530. return ret;
  1531. /* Blit the batch (which has now all relocs applied) to the
  1532. * stable batch scratch bo area (so that the CS never
  1533. * stumbles over its tlb invalidation bug) ...
  1534. */
  1535. intel_ring_emit(engine, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
  1536. intel_ring_emit(engine,
  1537. BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
  1538. intel_ring_emit(engine, DIV_ROUND_UP(len, 4096) << 16 | 4096);
  1539. intel_ring_emit(engine, cs_offset);
  1540. intel_ring_emit(engine, 4096);
  1541. intel_ring_emit(engine, offset);
  1542. intel_ring_emit(engine, MI_FLUSH);
  1543. intel_ring_emit(engine, MI_NOOP);
  1544. intel_ring_advance(engine);
  1545. /* ... and execute it. */
  1546. offset = cs_offset;
  1547. }
  1548. ret = intel_ring_begin(req, 2);
  1549. if (ret)
  1550. return ret;
  1551. intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
  1552. intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
  1553. 0 : MI_BATCH_NON_SECURE));
  1554. intel_ring_advance(engine);
  1555. return 0;
  1556. }
  1557. static int
  1558. i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
  1559. u64 offset, u32 len,
  1560. unsigned dispatch_flags)
  1561. {
  1562. struct intel_engine_cs *engine = req->engine;
  1563. int ret;
  1564. ret = intel_ring_begin(req, 2);
  1565. if (ret)
  1566. return ret;
  1567. intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
  1568. intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
  1569. 0 : MI_BATCH_NON_SECURE));
  1570. intel_ring_advance(engine);
  1571. return 0;
  1572. }
  1573. static void cleanup_phys_status_page(struct intel_engine_cs *engine)
  1574. {
  1575. struct drm_i915_private *dev_priv = engine->i915;
  1576. if (!dev_priv->status_page_dmah)
  1577. return;
  1578. drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
  1579. engine->status_page.page_addr = NULL;
  1580. }
  1581. static void cleanup_status_page(struct intel_engine_cs *engine)
  1582. {
  1583. struct drm_i915_gem_object *obj;
  1584. obj = engine->status_page.obj;
  1585. if (obj == NULL)
  1586. return;
  1587. kunmap(sg_page(obj->pages->sgl));
  1588. i915_gem_object_ggtt_unpin(obj);
  1589. i915_gem_object_put(obj);
  1590. engine->status_page.obj = NULL;
  1591. }
  1592. static int init_status_page(struct intel_engine_cs *engine)
  1593. {
  1594. struct drm_i915_gem_object *obj = engine->status_page.obj;
  1595. if (obj == NULL) {
  1596. unsigned flags;
  1597. int ret;
  1598. obj = i915_gem_object_create(&engine->i915->drm, 4096);
  1599. if (IS_ERR(obj)) {
  1600. DRM_ERROR("Failed to allocate status page\n");
  1601. return PTR_ERR(obj);
  1602. }
  1603. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  1604. if (ret)
  1605. goto err_unref;
  1606. flags = 0;
  1607. if (!HAS_LLC(engine->i915))
  1608. /* On g33, we cannot place HWS above 256MiB, so
  1609. * restrict its pinning to the low mappable arena.
  1610. * Though this restriction is not documented for
  1611. * gen4, gen5, or byt, they also behave similarly
  1612. * and hang if the HWS is placed at the top of the
  1613. * GTT. To generalise, it appears that all !llc
  1614. * platforms have issues with us placing the HWS
  1615. * above the mappable region (even though we never
  1616. * actualy map it).
  1617. */
  1618. flags |= PIN_MAPPABLE;
  1619. ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
  1620. if (ret) {
  1621. err_unref:
  1622. i915_gem_object_put(obj);
  1623. return ret;
  1624. }
  1625. engine->status_page.obj = obj;
  1626. }
  1627. engine->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
  1628. engine->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
  1629. memset(engine->status_page.page_addr, 0, PAGE_SIZE);
  1630. DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
  1631. engine->name, engine->status_page.gfx_addr);
  1632. return 0;
  1633. }
  1634. static int init_phys_status_page(struct intel_engine_cs *engine)
  1635. {
  1636. struct drm_i915_private *dev_priv = engine->i915;
  1637. if (!dev_priv->status_page_dmah) {
  1638. dev_priv->status_page_dmah =
  1639. drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
  1640. if (!dev_priv->status_page_dmah)
  1641. return -ENOMEM;
  1642. }
  1643. engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
  1644. memset(engine->status_page.page_addr, 0, PAGE_SIZE);
  1645. return 0;
  1646. }
  1647. void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
  1648. {
  1649. GEM_BUG_ON(!ringbuf->vma);
  1650. GEM_BUG_ON(!ringbuf->vaddr);
  1651. if (HAS_LLC(ringbuf->obj->base.dev) && !ringbuf->obj->stolen)
  1652. i915_gem_object_unpin_map(ringbuf->obj);
  1653. else
  1654. i915_vma_unpin_iomap(ringbuf->vma);
  1655. ringbuf->vaddr = NULL;
  1656. i915_gem_object_ggtt_unpin(ringbuf->obj);
  1657. ringbuf->vma = NULL;
  1658. }
  1659. int intel_pin_and_map_ringbuffer_obj(struct drm_i915_private *dev_priv,
  1660. struct intel_ringbuffer *ringbuf)
  1661. {
  1662. struct drm_i915_gem_object *obj = ringbuf->obj;
  1663. /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
  1664. unsigned flags = PIN_OFFSET_BIAS | 4096;
  1665. void *addr;
  1666. int ret;
  1667. if (HAS_LLC(dev_priv) && !obj->stolen) {
  1668. ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, flags);
  1669. if (ret)
  1670. return ret;
  1671. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1672. if (ret)
  1673. goto err_unpin;
  1674. addr = i915_gem_object_pin_map(obj);
  1675. if (IS_ERR(addr)) {
  1676. ret = PTR_ERR(addr);
  1677. goto err_unpin;
  1678. }
  1679. } else {
  1680. ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
  1681. flags | PIN_MAPPABLE);
  1682. if (ret)
  1683. return ret;
  1684. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  1685. if (ret)
  1686. goto err_unpin;
  1687. /* Access through the GTT requires the device to be awake. */
  1688. assert_rpm_wakelock_held(dev_priv);
  1689. addr = (void __force *)
  1690. i915_vma_pin_iomap(i915_gem_obj_to_ggtt(obj));
  1691. if (IS_ERR(addr)) {
  1692. ret = PTR_ERR(addr);
  1693. goto err_unpin;
  1694. }
  1695. }
  1696. ringbuf->vaddr = addr;
  1697. ringbuf->vma = i915_gem_obj_to_ggtt(obj);
  1698. return 0;
  1699. err_unpin:
  1700. i915_gem_object_ggtt_unpin(obj);
  1701. return ret;
  1702. }
  1703. static void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
  1704. {
  1705. i915_gem_object_put(ringbuf->obj);
  1706. ringbuf->obj = NULL;
  1707. }
  1708. static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
  1709. struct intel_ringbuffer *ringbuf)
  1710. {
  1711. struct drm_i915_gem_object *obj;
  1712. obj = NULL;
  1713. if (!HAS_LLC(dev))
  1714. obj = i915_gem_object_create_stolen(dev, ringbuf->size);
  1715. if (obj == NULL)
  1716. obj = i915_gem_object_create(dev, ringbuf->size);
  1717. if (IS_ERR(obj))
  1718. return PTR_ERR(obj);
  1719. /* mark ring buffers as read-only from GPU side by default */
  1720. obj->gt_ro = 1;
  1721. ringbuf->obj = obj;
  1722. return 0;
  1723. }
  1724. struct intel_ringbuffer *
  1725. intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size)
  1726. {
  1727. struct intel_ringbuffer *ring;
  1728. int ret;
  1729. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  1730. if (ring == NULL) {
  1731. DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
  1732. engine->name);
  1733. return ERR_PTR(-ENOMEM);
  1734. }
  1735. ring->engine = engine;
  1736. list_add(&ring->link, &engine->buffers);
  1737. ring->size = size;
  1738. /* Workaround an erratum on the i830 which causes a hang if
  1739. * the TAIL pointer points to within the last 2 cachelines
  1740. * of the buffer.
  1741. */
  1742. ring->effective_size = size;
  1743. if (IS_I830(engine->i915) || IS_845G(engine->i915))
  1744. ring->effective_size -= 2 * CACHELINE_BYTES;
  1745. ring->last_retired_head = -1;
  1746. intel_ring_update_space(ring);
  1747. ret = intel_alloc_ringbuffer_obj(&engine->i915->drm, ring);
  1748. if (ret) {
  1749. DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s: %d\n",
  1750. engine->name, ret);
  1751. list_del(&ring->link);
  1752. kfree(ring);
  1753. return ERR_PTR(ret);
  1754. }
  1755. return ring;
  1756. }
  1757. void
  1758. intel_ringbuffer_free(struct intel_ringbuffer *ring)
  1759. {
  1760. intel_destroy_ringbuffer_obj(ring);
  1761. list_del(&ring->link);
  1762. kfree(ring);
  1763. }
  1764. static int intel_ring_context_pin(struct i915_gem_context *ctx,
  1765. struct intel_engine_cs *engine)
  1766. {
  1767. struct intel_context *ce = &ctx->engine[engine->id];
  1768. int ret;
  1769. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  1770. if (ce->pin_count++)
  1771. return 0;
  1772. if (ce->state) {
  1773. ret = i915_gem_obj_ggtt_pin(ce->state, ctx->ggtt_alignment, 0);
  1774. if (ret)
  1775. goto error;
  1776. }
  1777. /* The kernel context is only used as a placeholder for flushing the
  1778. * active context. It is never used for submitting user rendering and
  1779. * as such never requires the golden render context, and so we can skip
  1780. * emitting it when we switch to the kernel context. This is required
  1781. * as during eviction we cannot allocate and pin the renderstate in
  1782. * order to initialise the context.
  1783. */
  1784. if (ctx == ctx->i915->kernel_context)
  1785. ce->initialised = true;
  1786. i915_gem_context_get(ctx);
  1787. return 0;
  1788. error:
  1789. ce->pin_count = 0;
  1790. return ret;
  1791. }
  1792. static void intel_ring_context_unpin(struct i915_gem_context *ctx,
  1793. struct intel_engine_cs *engine)
  1794. {
  1795. struct intel_context *ce = &ctx->engine[engine->id];
  1796. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  1797. if (--ce->pin_count)
  1798. return;
  1799. if (ce->state)
  1800. i915_gem_object_ggtt_unpin(ce->state);
  1801. i915_gem_context_put(ctx);
  1802. }
  1803. static int intel_init_ring_buffer(struct intel_engine_cs *engine)
  1804. {
  1805. struct drm_i915_private *dev_priv = engine->i915;
  1806. struct intel_ringbuffer *ringbuf;
  1807. int ret;
  1808. WARN_ON(engine->buffer);
  1809. intel_engine_setup_common(engine);
  1810. memset(engine->semaphore.sync_seqno, 0,
  1811. sizeof(engine->semaphore.sync_seqno));
  1812. ret = intel_engine_init_common(engine);
  1813. if (ret)
  1814. goto error;
  1815. /* We may need to do things with the shrinker which
  1816. * require us to immediately switch back to the default
  1817. * context. This can cause a problem as pinning the
  1818. * default context also requires GTT space which may not
  1819. * be available. To avoid this we always pin the default
  1820. * context.
  1821. */
  1822. ret = intel_ring_context_pin(dev_priv->kernel_context, engine);
  1823. if (ret)
  1824. goto error;
  1825. ringbuf = intel_engine_create_ringbuffer(engine, 32 * PAGE_SIZE);
  1826. if (IS_ERR(ringbuf)) {
  1827. ret = PTR_ERR(ringbuf);
  1828. goto error;
  1829. }
  1830. engine->buffer = ringbuf;
  1831. if (I915_NEED_GFX_HWS(dev_priv)) {
  1832. ret = init_status_page(engine);
  1833. if (ret)
  1834. goto error;
  1835. } else {
  1836. WARN_ON(engine->id != RCS);
  1837. ret = init_phys_status_page(engine);
  1838. if (ret)
  1839. goto error;
  1840. }
  1841. ret = intel_pin_and_map_ringbuffer_obj(dev_priv, ringbuf);
  1842. if (ret) {
  1843. DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
  1844. engine->name, ret);
  1845. intel_destroy_ringbuffer_obj(ringbuf);
  1846. goto error;
  1847. }
  1848. return 0;
  1849. error:
  1850. intel_cleanup_engine(engine);
  1851. return ret;
  1852. }
  1853. void intel_cleanup_engine(struct intel_engine_cs *engine)
  1854. {
  1855. struct drm_i915_private *dev_priv;
  1856. if (!intel_engine_initialized(engine))
  1857. return;
  1858. dev_priv = engine->i915;
  1859. if (engine->buffer) {
  1860. intel_stop_engine(engine);
  1861. WARN_ON(!IS_GEN2(dev_priv) && (I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1862. intel_unpin_ringbuffer_obj(engine->buffer);
  1863. intel_ringbuffer_free(engine->buffer);
  1864. engine->buffer = NULL;
  1865. }
  1866. if (engine->cleanup)
  1867. engine->cleanup(engine);
  1868. if (I915_NEED_GFX_HWS(dev_priv)) {
  1869. cleanup_status_page(engine);
  1870. } else {
  1871. WARN_ON(engine->id != RCS);
  1872. cleanup_phys_status_page(engine);
  1873. }
  1874. i915_cmd_parser_fini_ring(engine);
  1875. i915_gem_batch_pool_fini(&engine->batch_pool);
  1876. intel_engine_fini_breadcrumbs(engine);
  1877. intel_ring_context_unpin(dev_priv->kernel_context, engine);
  1878. engine->i915 = NULL;
  1879. }
  1880. int intel_engine_idle(struct intel_engine_cs *engine)
  1881. {
  1882. struct drm_i915_gem_request *req;
  1883. /* Wait upon the last request to be completed */
  1884. if (list_empty(&engine->request_list))
  1885. return 0;
  1886. req = list_entry(engine->request_list.prev,
  1887. struct drm_i915_gem_request,
  1888. list);
  1889. /* Make sure we do not trigger any retires */
  1890. return __i915_wait_request(req,
  1891. req->i915->mm.interruptible,
  1892. NULL, NULL);
  1893. }
  1894. int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
  1895. {
  1896. int ret;
  1897. /* Flush enough space to reduce the likelihood of waiting after
  1898. * we start building the request - in which case we will just
  1899. * have to repeat work.
  1900. */
  1901. request->reserved_space += LEGACY_REQUEST_SIZE;
  1902. request->ringbuf = request->engine->buffer;
  1903. ret = intel_ring_begin(request, 0);
  1904. if (ret)
  1905. return ret;
  1906. request->reserved_space -= LEGACY_REQUEST_SIZE;
  1907. return 0;
  1908. }
  1909. static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
  1910. {
  1911. struct intel_ringbuffer *ringbuf = req->ringbuf;
  1912. struct intel_engine_cs *engine = req->engine;
  1913. struct drm_i915_gem_request *target;
  1914. intel_ring_update_space(ringbuf);
  1915. if (ringbuf->space >= bytes)
  1916. return 0;
  1917. /*
  1918. * Space is reserved in the ringbuffer for finalising the request,
  1919. * as that cannot be allowed to fail. During request finalisation,
  1920. * reserved_space is set to 0 to stop the overallocation and the
  1921. * assumption is that then we never need to wait (which has the
  1922. * risk of failing with EINTR).
  1923. *
  1924. * See also i915_gem_request_alloc() and i915_add_request().
  1925. */
  1926. GEM_BUG_ON(!req->reserved_space);
  1927. list_for_each_entry(target, &engine->request_list, list) {
  1928. unsigned space;
  1929. /*
  1930. * The request queue is per-engine, so can contain requests
  1931. * from multiple ringbuffers. Here, we must ignore any that
  1932. * aren't from the ringbuffer we're considering.
  1933. */
  1934. if (target->ringbuf != ringbuf)
  1935. continue;
  1936. /* Would completion of this request free enough space? */
  1937. space = __intel_ring_space(target->postfix, ringbuf->tail,
  1938. ringbuf->size);
  1939. if (space >= bytes)
  1940. break;
  1941. }
  1942. if (WARN_ON(&target->list == &engine->request_list))
  1943. return -ENOSPC;
  1944. return i915_wait_request(target);
  1945. }
  1946. int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
  1947. {
  1948. struct intel_ringbuffer *ringbuf = req->ringbuf;
  1949. int remain_actual = ringbuf->size - ringbuf->tail;
  1950. int remain_usable = ringbuf->effective_size - ringbuf->tail;
  1951. int bytes = num_dwords * sizeof(u32);
  1952. int total_bytes, wait_bytes;
  1953. bool need_wrap = false;
  1954. total_bytes = bytes + req->reserved_space;
  1955. if (unlikely(bytes > remain_usable)) {
  1956. /*
  1957. * Not enough space for the basic request. So need to flush
  1958. * out the remainder and then wait for base + reserved.
  1959. */
  1960. wait_bytes = remain_actual + total_bytes;
  1961. need_wrap = true;
  1962. } else if (unlikely(total_bytes > remain_usable)) {
  1963. /*
  1964. * The base request will fit but the reserved space
  1965. * falls off the end. So we don't need an immediate wrap
  1966. * and only need to effectively wait for the reserved
  1967. * size space from the start of ringbuffer.
  1968. */
  1969. wait_bytes = remain_actual + req->reserved_space;
  1970. } else {
  1971. /* No wrapping required, just waiting. */
  1972. wait_bytes = total_bytes;
  1973. }
  1974. if (wait_bytes > ringbuf->space) {
  1975. int ret = wait_for_space(req, wait_bytes);
  1976. if (unlikely(ret))
  1977. return ret;
  1978. intel_ring_update_space(ringbuf);
  1979. if (unlikely(ringbuf->space < wait_bytes))
  1980. return -EAGAIN;
  1981. }
  1982. if (unlikely(need_wrap)) {
  1983. GEM_BUG_ON(remain_actual > ringbuf->space);
  1984. GEM_BUG_ON(ringbuf->tail + remain_actual > ringbuf->size);
  1985. /* Fill the tail with MI_NOOP */
  1986. memset(ringbuf->vaddr + ringbuf->tail, 0, remain_actual);
  1987. ringbuf->tail = 0;
  1988. ringbuf->space -= remain_actual;
  1989. }
  1990. ringbuf->space -= bytes;
  1991. GEM_BUG_ON(ringbuf->space < 0);
  1992. return 0;
  1993. }
  1994. /* Align the ring tail to a cacheline boundary */
  1995. int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
  1996. {
  1997. struct intel_engine_cs *engine = req->engine;
  1998. int num_dwords = (engine->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
  1999. int ret;
  2000. if (num_dwords == 0)
  2001. return 0;
  2002. num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
  2003. ret = intel_ring_begin(req, num_dwords);
  2004. if (ret)
  2005. return ret;
  2006. while (num_dwords--)
  2007. intel_ring_emit(engine, MI_NOOP);
  2008. intel_ring_advance(engine);
  2009. return 0;
  2010. }
  2011. void intel_ring_init_seqno(struct intel_engine_cs *engine, u32 seqno)
  2012. {
  2013. struct drm_i915_private *dev_priv = engine->i915;
  2014. /* Our semaphore implementation is strictly monotonic (i.e. we proceed
  2015. * so long as the semaphore value in the register/page is greater
  2016. * than the sync value), so whenever we reset the seqno,
  2017. * so long as we reset the tracking semaphore value to 0, it will
  2018. * always be before the next request's seqno. If we don't reset
  2019. * the semaphore value, then when the seqno moves backwards all
  2020. * future waits will complete instantly (causing rendering corruption).
  2021. */
  2022. if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
  2023. I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
  2024. I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
  2025. if (HAS_VEBOX(dev_priv))
  2026. I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
  2027. }
  2028. if (dev_priv->semaphore_obj) {
  2029. struct drm_i915_gem_object *obj = dev_priv->semaphore_obj;
  2030. struct page *page = i915_gem_object_get_dirty_page(obj, 0);
  2031. void *semaphores = kmap(page);
  2032. memset(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
  2033. 0, I915_NUM_ENGINES * gen8_semaphore_seqno_size);
  2034. kunmap(page);
  2035. }
  2036. memset(engine->semaphore.sync_seqno, 0,
  2037. sizeof(engine->semaphore.sync_seqno));
  2038. intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
  2039. if (engine->irq_seqno_barrier)
  2040. engine->irq_seqno_barrier(engine);
  2041. engine->last_submitted_seqno = seqno;
  2042. engine->hangcheck.seqno = seqno;
  2043. /* After manually advancing the seqno, fake the interrupt in case
  2044. * there are any waiters for that seqno.
  2045. */
  2046. rcu_read_lock();
  2047. intel_engine_wakeup(engine);
  2048. rcu_read_unlock();
  2049. }
  2050. static void gen6_bsd_ring_write_tail(struct intel_engine_cs *engine,
  2051. u32 value)
  2052. {
  2053. struct drm_i915_private *dev_priv = engine->i915;
  2054. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  2055. /* Every tail move must follow the sequence below */
  2056. /* Disable notification that the ring is IDLE. The GT
  2057. * will then assume that it is busy and bring it out of rc6.
  2058. */
  2059. I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
  2060. _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  2061. /* Clear the context id. Here be magic! */
  2062. I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
  2063. /* Wait for the ring not to be idle, i.e. for it to wake up. */
  2064. if (intel_wait_for_register_fw(dev_priv,
  2065. GEN6_BSD_SLEEP_PSMI_CONTROL,
  2066. GEN6_BSD_SLEEP_INDICATOR,
  2067. 0,
  2068. 50))
  2069. DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
  2070. /* Now that the ring is fully powered up, update the tail */
  2071. I915_WRITE_FW(RING_TAIL(engine->mmio_base), value);
  2072. POSTING_READ_FW(RING_TAIL(engine->mmio_base));
  2073. /* Let the ring send IDLE messages to the GT again,
  2074. * and so let it sleep to conserve power when idle.
  2075. */
  2076. I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
  2077. _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  2078. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  2079. }
  2080. static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
  2081. u32 invalidate, u32 flush)
  2082. {
  2083. struct intel_engine_cs *engine = req->engine;
  2084. uint32_t cmd;
  2085. int ret;
  2086. ret = intel_ring_begin(req, 4);
  2087. if (ret)
  2088. return ret;
  2089. cmd = MI_FLUSH_DW;
  2090. if (INTEL_GEN(req->i915) >= 8)
  2091. cmd += 1;
  2092. /* We always require a command barrier so that subsequent
  2093. * commands, such as breadcrumb interrupts, are strictly ordered
  2094. * wrt the contents of the write cache being flushed to memory
  2095. * (and thus being coherent from the CPU).
  2096. */
  2097. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  2098. /*
  2099. * Bspec vol 1c.5 - video engine command streamer:
  2100. * "If ENABLED, all TLBs will be invalidated once the flush
  2101. * operation is complete. This bit is only valid when the
  2102. * Post-Sync Operation field is a value of 1h or 3h."
  2103. */
  2104. if (invalidate & I915_GEM_GPU_DOMAINS)
  2105. cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
  2106. intel_ring_emit(engine, cmd);
  2107. intel_ring_emit(engine,
  2108. I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
  2109. if (INTEL_GEN(req->i915) >= 8) {
  2110. intel_ring_emit(engine, 0); /* upper addr */
  2111. intel_ring_emit(engine, 0); /* value */
  2112. } else {
  2113. intel_ring_emit(engine, 0);
  2114. intel_ring_emit(engine, MI_NOOP);
  2115. }
  2116. intel_ring_advance(engine);
  2117. return 0;
  2118. }
  2119. static int
  2120. gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
  2121. u64 offset, u32 len,
  2122. unsigned dispatch_flags)
  2123. {
  2124. struct intel_engine_cs *engine = req->engine;
  2125. bool ppgtt = USES_PPGTT(engine->dev) &&
  2126. !(dispatch_flags & I915_DISPATCH_SECURE);
  2127. int ret;
  2128. ret = intel_ring_begin(req, 4);
  2129. if (ret)
  2130. return ret;
  2131. /* FIXME(BDW): Address space and security selectors. */
  2132. intel_ring_emit(engine, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
  2133. (dispatch_flags & I915_DISPATCH_RS ?
  2134. MI_BATCH_RESOURCE_STREAMER : 0));
  2135. intel_ring_emit(engine, lower_32_bits(offset));
  2136. intel_ring_emit(engine, upper_32_bits(offset));
  2137. intel_ring_emit(engine, MI_NOOP);
  2138. intel_ring_advance(engine);
  2139. return 0;
  2140. }
  2141. static int
  2142. hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
  2143. u64 offset, u32 len,
  2144. unsigned dispatch_flags)
  2145. {
  2146. struct intel_engine_cs *engine = req->engine;
  2147. int ret;
  2148. ret = intel_ring_begin(req, 2);
  2149. if (ret)
  2150. return ret;
  2151. intel_ring_emit(engine,
  2152. MI_BATCH_BUFFER_START |
  2153. (dispatch_flags & I915_DISPATCH_SECURE ?
  2154. 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
  2155. (dispatch_flags & I915_DISPATCH_RS ?
  2156. MI_BATCH_RESOURCE_STREAMER : 0));
  2157. /* bit0-7 is the length on GEN6+ */
  2158. intel_ring_emit(engine, offset);
  2159. intel_ring_advance(engine);
  2160. return 0;
  2161. }
  2162. static int
  2163. gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
  2164. u64 offset, u32 len,
  2165. unsigned dispatch_flags)
  2166. {
  2167. struct intel_engine_cs *engine = req->engine;
  2168. int ret;
  2169. ret = intel_ring_begin(req, 2);
  2170. if (ret)
  2171. return ret;
  2172. intel_ring_emit(engine,
  2173. MI_BATCH_BUFFER_START |
  2174. (dispatch_flags & I915_DISPATCH_SECURE ?
  2175. 0 : MI_BATCH_NON_SECURE_I965));
  2176. /* bit0-7 is the length on GEN6+ */
  2177. intel_ring_emit(engine, offset);
  2178. intel_ring_advance(engine);
  2179. return 0;
  2180. }
  2181. /* Blitter support (SandyBridge+) */
  2182. static int gen6_ring_flush(struct drm_i915_gem_request *req,
  2183. u32 invalidate, u32 flush)
  2184. {
  2185. struct intel_engine_cs *engine = req->engine;
  2186. uint32_t cmd;
  2187. int ret;
  2188. ret = intel_ring_begin(req, 4);
  2189. if (ret)
  2190. return ret;
  2191. cmd = MI_FLUSH_DW;
  2192. if (INTEL_GEN(req->i915) >= 8)
  2193. cmd += 1;
  2194. /* We always require a command barrier so that subsequent
  2195. * commands, such as breadcrumb interrupts, are strictly ordered
  2196. * wrt the contents of the write cache being flushed to memory
  2197. * (and thus being coherent from the CPU).
  2198. */
  2199. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  2200. /*
  2201. * Bspec vol 1c.3 - blitter engine command streamer:
  2202. * "If ENABLED, all TLBs will be invalidated once the flush
  2203. * operation is complete. This bit is only valid when the
  2204. * Post-Sync Operation field is a value of 1h or 3h."
  2205. */
  2206. if (invalidate & I915_GEM_DOMAIN_RENDER)
  2207. cmd |= MI_INVALIDATE_TLB;
  2208. intel_ring_emit(engine, cmd);
  2209. intel_ring_emit(engine,
  2210. I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
  2211. if (INTEL_GEN(req->i915) >= 8) {
  2212. intel_ring_emit(engine, 0); /* upper addr */
  2213. intel_ring_emit(engine, 0); /* value */
  2214. } else {
  2215. intel_ring_emit(engine, 0);
  2216. intel_ring_emit(engine, MI_NOOP);
  2217. }
  2218. intel_ring_advance(engine);
  2219. return 0;
  2220. }
  2221. static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
  2222. struct intel_engine_cs *engine)
  2223. {
  2224. struct drm_i915_gem_object *obj;
  2225. int ret, i;
  2226. if (!i915.semaphores)
  2227. return;
  2228. if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore_obj) {
  2229. obj = i915_gem_object_create(&dev_priv->drm, 4096);
  2230. if (IS_ERR(obj)) {
  2231. DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
  2232. i915.semaphores = 0;
  2233. } else {
  2234. i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  2235. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
  2236. if (ret != 0) {
  2237. i915_gem_object_put(obj);
  2238. DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
  2239. i915.semaphores = 0;
  2240. } else {
  2241. dev_priv->semaphore_obj = obj;
  2242. }
  2243. }
  2244. }
  2245. if (!i915.semaphores)
  2246. return;
  2247. if (INTEL_GEN(dev_priv) >= 8) {
  2248. u64 offset = i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj);
  2249. engine->semaphore.sync_to = gen8_ring_sync;
  2250. engine->semaphore.signal = gen8_xcs_signal;
  2251. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2252. u64 ring_offset;
  2253. if (i != engine->id)
  2254. ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
  2255. else
  2256. ring_offset = MI_SEMAPHORE_SYNC_INVALID;
  2257. engine->semaphore.signal_ggtt[i] = ring_offset;
  2258. }
  2259. } else if (INTEL_GEN(dev_priv) >= 6) {
  2260. engine->semaphore.sync_to = gen6_ring_sync;
  2261. engine->semaphore.signal = gen6_signal;
  2262. /*
  2263. * The current semaphore is only applied on pre-gen8
  2264. * platform. And there is no VCS2 ring on the pre-gen8
  2265. * platform. So the semaphore between RCS and VCS2 is
  2266. * initialized as INVALID. Gen8 will initialize the
  2267. * sema between VCS2 and RCS later.
  2268. */
  2269. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2270. static const struct {
  2271. u32 wait_mbox;
  2272. i915_reg_t mbox_reg;
  2273. } sem_data[I915_NUM_ENGINES][I915_NUM_ENGINES] = {
  2274. [RCS] = {
  2275. [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
  2276. [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
  2277. [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
  2278. },
  2279. [VCS] = {
  2280. [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
  2281. [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
  2282. [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
  2283. },
  2284. [BCS] = {
  2285. [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
  2286. [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
  2287. [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
  2288. },
  2289. [VECS] = {
  2290. [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
  2291. [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
  2292. [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
  2293. },
  2294. };
  2295. u32 wait_mbox;
  2296. i915_reg_t mbox_reg;
  2297. if (i == engine->id || i == VCS2) {
  2298. wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
  2299. mbox_reg = GEN6_NOSYNC;
  2300. } else {
  2301. wait_mbox = sem_data[engine->id][i].wait_mbox;
  2302. mbox_reg = sem_data[engine->id][i].mbox_reg;
  2303. }
  2304. engine->semaphore.mbox.wait[i] = wait_mbox;
  2305. engine->semaphore.mbox.signal[i] = mbox_reg;
  2306. }
  2307. }
  2308. }
  2309. static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
  2310. struct intel_engine_cs *engine)
  2311. {
  2312. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
  2313. if (INTEL_GEN(dev_priv) >= 8) {
  2314. engine->irq_enable = gen8_irq_enable;
  2315. engine->irq_disable = gen8_irq_disable;
  2316. engine->irq_seqno_barrier = gen6_seqno_barrier;
  2317. } else if (INTEL_GEN(dev_priv) >= 6) {
  2318. engine->irq_enable = gen6_irq_enable;
  2319. engine->irq_disable = gen6_irq_disable;
  2320. engine->irq_seqno_barrier = gen6_seqno_barrier;
  2321. } else if (INTEL_GEN(dev_priv) >= 5) {
  2322. engine->irq_enable = gen5_irq_enable;
  2323. engine->irq_disable = gen5_irq_disable;
  2324. engine->irq_seqno_barrier = gen5_seqno_barrier;
  2325. } else if (INTEL_GEN(dev_priv) >= 3) {
  2326. engine->irq_enable = i9xx_irq_enable;
  2327. engine->irq_disable = i9xx_irq_disable;
  2328. } else {
  2329. engine->irq_enable = i8xx_irq_enable;
  2330. engine->irq_disable = i8xx_irq_disable;
  2331. }
  2332. }
  2333. static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
  2334. struct intel_engine_cs *engine)
  2335. {
  2336. engine->init_hw = init_ring_common;
  2337. engine->write_tail = ring_write_tail;
  2338. engine->add_request = i9xx_add_request;
  2339. if (INTEL_GEN(dev_priv) >= 6)
  2340. engine->add_request = gen6_add_request;
  2341. if (INTEL_GEN(dev_priv) >= 8)
  2342. engine->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
  2343. else if (INTEL_GEN(dev_priv) >= 6)
  2344. engine->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
  2345. else if (INTEL_GEN(dev_priv) >= 4)
  2346. engine->dispatch_execbuffer = i965_dispatch_execbuffer;
  2347. else if (IS_I830(dev_priv) || IS_845G(dev_priv))
  2348. engine->dispatch_execbuffer = i830_dispatch_execbuffer;
  2349. else
  2350. engine->dispatch_execbuffer = i915_dispatch_execbuffer;
  2351. intel_ring_init_irq(dev_priv, engine);
  2352. intel_ring_init_semaphores(dev_priv, engine);
  2353. }
  2354. int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
  2355. {
  2356. struct drm_i915_private *dev_priv = engine->i915;
  2357. int ret;
  2358. intel_ring_default_vfuncs(dev_priv, engine);
  2359. if (HAS_L3_DPF(dev_priv))
  2360. engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  2361. if (INTEL_GEN(dev_priv) >= 8) {
  2362. engine->init_context = intel_rcs_ctx_init;
  2363. engine->add_request = gen8_render_add_request;
  2364. engine->flush = gen8_render_ring_flush;
  2365. if (i915.semaphores)
  2366. engine->semaphore.signal = gen8_rcs_signal;
  2367. } else if (INTEL_GEN(dev_priv) >= 6) {
  2368. engine->init_context = intel_rcs_ctx_init;
  2369. engine->flush = gen7_render_ring_flush;
  2370. if (IS_GEN6(dev_priv))
  2371. engine->flush = gen6_render_ring_flush;
  2372. } else if (IS_GEN5(dev_priv)) {
  2373. engine->flush = gen4_render_ring_flush;
  2374. } else {
  2375. if (INTEL_GEN(dev_priv) < 4)
  2376. engine->flush = gen2_render_ring_flush;
  2377. else
  2378. engine->flush = gen4_render_ring_flush;
  2379. engine->irq_enable_mask = I915_USER_INTERRUPT;
  2380. }
  2381. if (IS_HASWELL(dev_priv))
  2382. engine->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
  2383. engine->init_hw = init_render_ring;
  2384. engine->cleanup = render_ring_cleanup;
  2385. ret = intel_init_ring_buffer(engine);
  2386. if (ret)
  2387. return ret;
  2388. if (INTEL_GEN(dev_priv) >= 6) {
  2389. ret = intel_init_pipe_control(engine, 4096);
  2390. if (ret)
  2391. return ret;
  2392. } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
  2393. ret = intel_init_pipe_control(engine, I830_WA_SIZE);
  2394. if (ret)
  2395. return ret;
  2396. }
  2397. return 0;
  2398. }
  2399. int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
  2400. {
  2401. struct drm_i915_private *dev_priv = engine->i915;
  2402. intel_ring_default_vfuncs(dev_priv, engine);
  2403. if (INTEL_GEN(dev_priv) >= 6) {
  2404. /* gen6 bsd needs a special wa for tail updates */
  2405. if (IS_GEN6(dev_priv))
  2406. engine->write_tail = gen6_bsd_ring_write_tail;
  2407. engine->flush = gen6_bsd_ring_flush;
  2408. if (INTEL_GEN(dev_priv) < 8)
  2409. engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
  2410. } else {
  2411. engine->mmio_base = BSD_RING_BASE;
  2412. engine->flush = bsd_ring_flush;
  2413. if (IS_GEN5(dev_priv))
  2414. engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
  2415. else
  2416. engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
  2417. }
  2418. return intel_init_ring_buffer(engine);
  2419. }
  2420. /**
  2421. * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
  2422. */
  2423. int intel_init_bsd2_ring_buffer(struct intel_engine_cs *engine)
  2424. {
  2425. struct drm_i915_private *dev_priv = engine->i915;
  2426. intel_ring_default_vfuncs(dev_priv, engine);
  2427. engine->flush = gen6_bsd_ring_flush;
  2428. return intel_init_ring_buffer(engine);
  2429. }
  2430. int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
  2431. {
  2432. struct drm_i915_private *dev_priv = engine->i915;
  2433. intel_ring_default_vfuncs(dev_priv, engine);
  2434. engine->flush = gen6_ring_flush;
  2435. if (INTEL_GEN(dev_priv) < 8)
  2436. engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
  2437. return intel_init_ring_buffer(engine);
  2438. }
  2439. int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
  2440. {
  2441. struct drm_i915_private *dev_priv = engine->i915;
  2442. intel_ring_default_vfuncs(dev_priv, engine);
  2443. engine->flush = gen6_ring_flush;
  2444. if (INTEL_GEN(dev_priv) < 8) {
  2445. engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
  2446. engine->irq_enable = hsw_vebox_irq_enable;
  2447. engine->irq_disable = hsw_vebox_irq_disable;
  2448. }
  2449. return intel_init_ring_buffer(engine);
  2450. }
  2451. int
  2452. intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
  2453. {
  2454. struct intel_engine_cs *engine = req->engine;
  2455. int ret;
  2456. if (!engine->gpu_caches_dirty)
  2457. return 0;
  2458. ret = engine->flush(req, 0, I915_GEM_GPU_DOMAINS);
  2459. if (ret)
  2460. return ret;
  2461. trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
  2462. engine->gpu_caches_dirty = false;
  2463. return 0;
  2464. }
  2465. int
  2466. intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
  2467. {
  2468. struct intel_engine_cs *engine = req->engine;
  2469. uint32_t flush_domains;
  2470. int ret;
  2471. flush_domains = 0;
  2472. if (engine->gpu_caches_dirty)
  2473. flush_domains = I915_GEM_GPU_DOMAINS;
  2474. ret = engine->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
  2475. if (ret)
  2476. return ret;
  2477. trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
  2478. engine->gpu_caches_dirty = false;
  2479. return 0;
  2480. }
  2481. void
  2482. intel_stop_engine(struct intel_engine_cs *engine)
  2483. {
  2484. int ret;
  2485. if (!intel_engine_initialized(engine))
  2486. return;
  2487. ret = intel_engine_idle(engine);
  2488. if (ret)
  2489. DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
  2490. engine->name, ret);
  2491. stop_ring(engine);
  2492. }