intel_ringbuffer.c 89 KB

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