intel_ringbuffer.c 82 KB

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