intel_ringbuffer.c 84 KB

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