intel_ringbuffer.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. /*
  2. * Copyright © 2008-2010 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Zou Nan hai <nanhai.zou@intel.com>
  26. * Xiang Hai hao<haihao.xiang@intel.com>
  27. *
  28. */
  29. #include <drm/drmP.h>
  30. #include "i915_drv.h"
  31. #include <drm/i915_drm.h>
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. static inline int ring_space(struct intel_ring_buffer *ring)
  35. {
  36. int space = (ring->head & HEAD_ADDR) - (ring->tail + I915_RING_FREE_SPACE);
  37. if (space < 0)
  38. space += ring->size;
  39. return space;
  40. }
  41. void __intel_ring_advance(struct intel_ring_buffer *ring)
  42. {
  43. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  44. ring->tail &= ring->size - 1;
  45. if (dev_priv->gpu_error.stop_rings & intel_ring_flag(ring))
  46. return;
  47. ring->write_tail(ring, ring->tail);
  48. }
  49. static int
  50. gen2_render_ring_flush(struct intel_ring_buffer *ring,
  51. u32 invalidate_domains,
  52. u32 flush_domains)
  53. {
  54. u32 cmd;
  55. int ret;
  56. cmd = MI_FLUSH;
  57. if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
  58. cmd |= MI_NO_WRITE_FLUSH;
  59. if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
  60. cmd |= MI_READ_FLUSH;
  61. ret = intel_ring_begin(ring, 2);
  62. if (ret)
  63. return ret;
  64. intel_ring_emit(ring, cmd);
  65. intel_ring_emit(ring, MI_NOOP);
  66. intel_ring_advance(ring);
  67. return 0;
  68. }
  69. static int
  70. gen4_render_ring_flush(struct intel_ring_buffer *ring,
  71. u32 invalidate_domains,
  72. u32 flush_domains)
  73. {
  74. struct drm_device *dev = ring->dev;
  75. u32 cmd;
  76. int ret;
  77. /*
  78. * read/write caches:
  79. *
  80. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  81. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  82. * also flushed at 2d versus 3d pipeline switches.
  83. *
  84. * read-only caches:
  85. *
  86. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  87. * MI_READ_FLUSH is set, and is always flushed on 965.
  88. *
  89. * I915_GEM_DOMAIN_COMMAND may not exist?
  90. *
  91. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  92. * invalidated when MI_EXE_FLUSH is set.
  93. *
  94. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  95. * invalidated with every MI_FLUSH.
  96. *
  97. * TLBs:
  98. *
  99. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  100. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  101. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  102. * are flushed at any MI_FLUSH.
  103. */
  104. cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  105. if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
  106. cmd &= ~MI_NO_WRITE_FLUSH;
  107. if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
  108. cmd |= MI_EXE_FLUSH;
  109. if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
  110. (IS_G4X(dev) || IS_GEN5(dev)))
  111. cmd |= MI_INVALIDATE_ISP;
  112. ret = intel_ring_begin(ring, 2);
  113. if (ret)
  114. return ret;
  115. intel_ring_emit(ring, cmd);
  116. intel_ring_emit(ring, MI_NOOP);
  117. intel_ring_advance(ring);
  118. return 0;
  119. }
  120. /**
  121. * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
  122. * implementing two workarounds on gen6. From section 1.4.7.1
  123. * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
  124. *
  125. * [DevSNB-C+{W/A}] Before any depth stall flush (including those
  126. * produced by non-pipelined state commands), software needs to first
  127. * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
  128. * 0.
  129. *
  130. * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
  131. * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
  132. *
  133. * And the workaround for these two requires this workaround first:
  134. *
  135. * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
  136. * BEFORE the pipe-control with a post-sync op and no write-cache
  137. * flushes.
  138. *
  139. * And this last workaround is tricky because of the requirements on
  140. * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
  141. * volume 2 part 1:
  142. *
  143. * "1 of the following must also be set:
  144. * - Render Target Cache Flush Enable ([12] of DW1)
  145. * - Depth Cache Flush Enable ([0] of DW1)
  146. * - Stall at Pixel Scoreboard ([1] of DW1)
  147. * - Depth Stall ([13] of DW1)
  148. * - Post-Sync Operation ([13] of DW1)
  149. * - Notify Enable ([8] of DW1)"
  150. *
  151. * The cache flushes require the workaround flush that triggered this
  152. * one, so we can't use it. Depth stall would trigger the same.
  153. * Post-sync nonzero is what triggered this second workaround, so we
  154. * can't use that one either. Notify enable is IRQs, which aren't
  155. * really our business. That leaves only stall at scoreboard.
  156. */
  157. static int
  158. intel_emit_post_sync_nonzero_flush(struct intel_ring_buffer *ring)
  159. {
  160. u32 scratch_addr = ring->scratch.gtt_offset + 128;
  161. int ret;
  162. ret = intel_ring_begin(ring, 6);
  163. if (ret)
  164. return ret;
  165. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
  166. intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
  167. PIPE_CONTROL_STALL_AT_SCOREBOARD);
  168. intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
  169. intel_ring_emit(ring, 0); /* low dword */
  170. intel_ring_emit(ring, 0); /* high dword */
  171. intel_ring_emit(ring, MI_NOOP);
  172. intel_ring_advance(ring);
  173. ret = intel_ring_begin(ring, 6);
  174. if (ret)
  175. return ret;
  176. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
  177. intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
  178. intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
  179. intel_ring_emit(ring, 0);
  180. intel_ring_emit(ring, 0);
  181. intel_ring_emit(ring, MI_NOOP);
  182. intel_ring_advance(ring);
  183. return 0;
  184. }
  185. static int
  186. gen6_render_ring_flush(struct intel_ring_buffer *ring,
  187. u32 invalidate_domains, u32 flush_domains)
  188. {
  189. u32 flags = 0;
  190. u32 scratch_addr = ring->scratch.gtt_offset + 128;
  191. int ret;
  192. /* Force SNB workarounds for PIPE_CONTROL flushes */
  193. ret = intel_emit_post_sync_nonzero_flush(ring);
  194. if (ret)
  195. return ret;
  196. /* Just flush everything. Experiments have shown that reducing the
  197. * number of bits based on the write domains has little performance
  198. * impact.
  199. */
  200. if (flush_domains) {
  201. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  202. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  203. /*
  204. * Ensure that any following seqno writes only happen
  205. * when the render cache is indeed flushed.
  206. */
  207. flags |= PIPE_CONTROL_CS_STALL;
  208. }
  209. if (invalidate_domains) {
  210. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  211. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  212. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  213. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  214. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  215. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  216. /*
  217. * TLB invalidate requires a post-sync write.
  218. */
  219. flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
  220. }
  221. ret = intel_ring_begin(ring, 4);
  222. if (ret)
  223. return ret;
  224. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
  225. intel_ring_emit(ring, flags);
  226. intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
  227. intel_ring_emit(ring, 0);
  228. intel_ring_advance(ring);
  229. return 0;
  230. }
  231. static int
  232. gen7_render_ring_cs_stall_wa(struct intel_ring_buffer *ring)
  233. {
  234. int ret;
  235. ret = intel_ring_begin(ring, 4);
  236. if (ret)
  237. return ret;
  238. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
  239. intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
  240. PIPE_CONTROL_STALL_AT_SCOREBOARD);
  241. intel_ring_emit(ring, 0);
  242. intel_ring_emit(ring, 0);
  243. intel_ring_advance(ring);
  244. return 0;
  245. }
  246. static int gen7_ring_fbc_flush(struct intel_ring_buffer *ring, u32 value)
  247. {
  248. int ret;
  249. if (!ring->fbc_dirty)
  250. return 0;
  251. ret = intel_ring_begin(ring, 6);
  252. if (ret)
  253. return ret;
  254. /* WaFbcNukeOn3DBlt:ivb/hsw */
  255. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  256. intel_ring_emit(ring, MSG_FBC_REND_STATE);
  257. intel_ring_emit(ring, value);
  258. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) | MI_SRM_LRM_GLOBAL_GTT);
  259. intel_ring_emit(ring, MSG_FBC_REND_STATE);
  260. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  261. intel_ring_advance(ring);
  262. ring->fbc_dirty = false;
  263. return 0;
  264. }
  265. static int
  266. gen7_render_ring_flush(struct intel_ring_buffer *ring,
  267. u32 invalidate_domains, u32 flush_domains)
  268. {
  269. u32 flags = 0;
  270. u32 scratch_addr = ring->scratch.gtt_offset + 128;
  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. }
  289. if (invalidate_domains) {
  290. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  291. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  292. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  293. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  294. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  295. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  296. /*
  297. * TLB invalidate requires a post-sync write.
  298. */
  299. flags |= PIPE_CONTROL_QW_WRITE;
  300. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  301. /* Workaround: we must issue a pipe_control with CS-stall bit
  302. * set before a pipe_control command that has the state cache
  303. * invalidate bit set. */
  304. gen7_render_ring_cs_stall_wa(ring);
  305. }
  306. ret = intel_ring_begin(ring, 4);
  307. if (ret)
  308. return ret;
  309. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
  310. intel_ring_emit(ring, flags);
  311. intel_ring_emit(ring, scratch_addr);
  312. intel_ring_emit(ring, 0);
  313. intel_ring_advance(ring);
  314. if (!invalidate_domains && flush_domains)
  315. return gen7_ring_fbc_flush(ring, FBC_REND_NUKE);
  316. return 0;
  317. }
  318. static int
  319. gen8_render_ring_flush(struct intel_ring_buffer *ring,
  320. u32 invalidate_domains, u32 flush_domains)
  321. {
  322. u32 flags = 0;
  323. u32 scratch_addr = ring->scratch.gtt_offset + 128;
  324. int ret;
  325. flags |= PIPE_CONTROL_CS_STALL;
  326. if (flush_domains) {
  327. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  328. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  329. }
  330. if (invalidate_domains) {
  331. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  332. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  333. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  334. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  335. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  336. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  337. flags |= PIPE_CONTROL_QW_WRITE;
  338. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  339. }
  340. ret = intel_ring_begin(ring, 6);
  341. if (ret)
  342. return ret;
  343. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  344. intel_ring_emit(ring, flags);
  345. intel_ring_emit(ring, scratch_addr);
  346. intel_ring_emit(ring, 0);
  347. intel_ring_emit(ring, 0);
  348. intel_ring_emit(ring, 0);
  349. intel_ring_advance(ring);
  350. return 0;
  351. }
  352. static void ring_write_tail(struct intel_ring_buffer *ring,
  353. u32 value)
  354. {
  355. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  356. I915_WRITE_TAIL(ring, value);
  357. }
  358. u32 intel_ring_get_active_head(struct intel_ring_buffer *ring)
  359. {
  360. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  361. u32 acthd_reg = INTEL_INFO(ring->dev)->gen >= 4 ?
  362. RING_ACTHD(ring->mmio_base) : ACTHD;
  363. return I915_READ(acthd_reg);
  364. }
  365. static void ring_setup_phys_status_page(struct intel_ring_buffer *ring)
  366. {
  367. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  368. u32 addr;
  369. addr = dev_priv->status_page_dmah->busaddr;
  370. if (INTEL_INFO(ring->dev)->gen >= 4)
  371. addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
  372. I915_WRITE(HWS_PGA, addr);
  373. }
  374. static int init_ring_common(struct intel_ring_buffer *ring)
  375. {
  376. struct drm_device *dev = ring->dev;
  377. drm_i915_private_t *dev_priv = dev->dev_private;
  378. struct drm_i915_gem_object *obj = ring->obj;
  379. int ret = 0;
  380. u32 head;
  381. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  382. if (I915_NEED_GFX_HWS(dev))
  383. intel_ring_setup_status_page(ring);
  384. else
  385. ring_setup_phys_status_page(ring);
  386. /* Stop the ring if it's running. */
  387. I915_WRITE_CTL(ring, 0);
  388. I915_WRITE_HEAD(ring, 0);
  389. ring->write_tail(ring, 0);
  390. head = I915_READ_HEAD(ring) & HEAD_ADDR;
  391. /* G45 ring initialization fails to reset head to zero */
  392. if (head != 0) {
  393. DRM_DEBUG_KMS("%s head not reset to zero "
  394. "ctl %08x head %08x tail %08x start %08x\n",
  395. ring->name,
  396. I915_READ_CTL(ring),
  397. I915_READ_HEAD(ring),
  398. I915_READ_TAIL(ring),
  399. I915_READ_START(ring));
  400. I915_WRITE_HEAD(ring, 0);
  401. if (I915_READ_HEAD(ring) & HEAD_ADDR) {
  402. DRM_ERROR("failed to set %s head to zero "
  403. "ctl %08x head %08x tail %08x start %08x\n",
  404. ring->name,
  405. I915_READ_CTL(ring),
  406. I915_READ_HEAD(ring),
  407. I915_READ_TAIL(ring),
  408. I915_READ_START(ring));
  409. }
  410. }
  411. /* Initialize the ring. This must happen _after_ we've cleared the ring
  412. * registers with the above sequence (the readback of the HEAD registers
  413. * also enforces ordering), otherwise the hw might lose the new ring
  414. * register values. */
  415. I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
  416. I915_WRITE_CTL(ring,
  417. ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
  418. | RING_VALID);
  419. /* If the head is still not zero, the ring is dead */
  420. if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
  421. I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
  422. (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
  423. DRM_ERROR("%s initialization failed "
  424. "ctl %08x head %08x tail %08x start %08x\n",
  425. ring->name,
  426. I915_READ_CTL(ring),
  427. I915_READ_HEAD(ring),
  428. I915_READ_TAIL(ring),
  429. I915_READ_START(ring));
  430. ret = -EIO;
  431. goto out;
  432. }
  433. if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
  434. i915_kernel_lost_context(ring->dev);
  435. else {
  436. ring->head = I915_READ_HEAD(ring);
  437. ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
  438. ring->space = ring_space(ring);
  439. ring->last_retired_head = -1;
  440. }
  441. memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
  442. out:
  443. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  444. return ret;
  445. }
  446. static int
  447. init_pipe_control(struct intel_ring_buffer *ring)
  448. {
  449. int ret;
  450. if (ring->scratch.obj)
  451. return 0;
  452. ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
  453. if (ring->scratch.obj == NULL) {
  454. DRM_ERROR("Failed to allocate seqno page\n");
  455. ret = -ENOMEM;
  456. goto err;
  457. }
  458. i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
  459. ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, true, false);
  460. if (ret)
  461. goto err_unref;
  462. ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
  463. ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
  464. if (ring->scratch.cpu_page == NULL) {
  465. ret = -ENOMEM;
  466. goto err_unpin;
  467. }
  468. DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
  469. ring->name, ring->scratch.gtt_offset);
  470. return 0;
  471. err_unpin:
  472. i915_gem_object_ggtt_unpin(ring->scratch.obj);
  473. err_unref:
  474. drm_gem_object_unreference(&ring->scratch.obj->base);
  475. err:
  476. return ret;
  477. }
  478. static int init_render_ring(struct intel_ring_buffer *ring)
  479. {
  480. struct drm_device *dev = ring->dev;
  481. struct drm_i915_private *dev_priv = dev->dev_private;
  482. int ret = init_ring_common(ring);
  483. if (INTEL_INFO(dev)->gen > 3)
  484. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
  485. /* We need to disable the AsyncFlip performance optimisations in order
  486. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  487. * programmed to '1' on all products.
  488. *
  489. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
  490. */
  491. if (INTEL_INFO(dev)->gen >= 6)
  492. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  493. /* Required for the hardware to program scanline values for waiting */
  494. if (INTEL_INFO(dev)->gen == 6)
  495. I915_WRITE(GFX_MODE,
  496. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_ALWAYS));
  497. if (IS_GEN7(dev))
  498. I915_WRITE(GFX_MODE_GEN7,
  499. _MASKED_BIT_DISABLE(GFX_TLB_INVALIDATE_ALWAYS) |
  500. _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
  501. if (INTEL_INFO(dev)->gen >= 5) {
  502. ret = init_pipe_control(ring);
  503. if (ret)
  504. return ret;
  505. }
  506. if (IS_GEN6(dev)) {
  507. /* From the Sandybridge PRM, volume 1 part 3, page 24:
  508. * "If this bit is set, STCunit will have LRA as replacement
  509. * policy. [...] This bit must be reset. LRA replacement
  510. * policy is not supported."
  511. */
  512. I915_WRITE(CACHE_MODE_0,
  513. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  514. /* This is not explicitly set for GEN6, so read the register.
  515. * see intel_ring_mi_set_context() for why we care.
  516. * TODO: consider explicitly setting the bit for GEN5
  517. */
  518. ring->itlb_before_ctx_switch =
  519. !!(I915_READ(GFX_MODE) & GFX_TLB_INVALIDATE_ALWAYS);
  520. }
  521. if (INTEL_INFO(dev)->gen >= 6)
  522. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  523. if (HAS_L3_DPF(dev))
  524. I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
  525. return ret;
  526. }
  527. static void render_ring_cleanup(struct intel_ring_buffer *ring)
  528. {
  529. struct drm_device *dev = ring->dev;
  530. if (ring->scratch.obj == NULL)
  531. return;
  532. if (INTEL_INFO(dev)->gen >= 5) {
  533. kunmap(sg_page(ring->scratch.obj->pages->sgl));
  534. i915_gem_object_ggtt_unpin(ring->scratch.obj);
  535. }
  536. drm_gem_object_unreference(&ring->scratch.obj->base);
  537. ring->scratch.obj = NULL;
  538. }
  539. static void
  540. update_mboxes(struct intel_ring_buffer *ring,
  541. u32 mmio_offset)
  542. {
  543. /* NB: In order to be able to do semaphore MBOX updates for varying number
  544. * of rings, it's easiest if we round up each individual update to a
  545. * multiple of 2 (since ring updates must always be a multiple of 2)
  546. * even though the actual update only requires 3 dwords.
  547. */
  548. #define MBOX_UPDATE_DWORDS 4
  549. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  550. intel_ring_emit(ring, mmio_offset);
  551. intel_ring_emit(ring, ring->outstanding_lazy_seqno);
  552. intel_ring_emit(ring, MI_NOOP);
  553. }
  554. /**
  555. * gen6_add_request - Update the semaphore mailbox registers
  556. *
  557. * @ring - ring that is adding a request
  558. * @seqno - return seqno stuck into the ring
  559. *
  560. * Update the mailbox registers in the *other* rings with the current seqno.
  561. * This acts like a signal in the canonical semaphore.
  562. */
  563. static int
  564. gen6_add_request(struct intel_ring_buffer *ring)
  565. {
  566. struct drm_device *dev = ring->dev;
  567. struct drm_i915_private *dev_priv = dev->dev_private;
  568. struct intel_ring_buffer *useless;
  569. int i, ret, num_dwords = 4;
  570. if (i915_semaphore_is_enabled(dev))
  571. num_dwords += ((I915_NUM_RINGS-1) * MBOX_UPDATE_DWORDS);
  572. #undef MBOX_UPDATE_DWORDS
  573. ret = intel_ring_begin(ring, num_dwords);
  574. if (ret)
  575. return ret;
  576. if (i915_semaphore_is_enabled(dev)) {
  577. for_each_ring(useless, dev_priv, i) {
  578. u32 mbox_reg = ring->signal_mbox[i];
  579. if (mbox_reg != GEN6_NOSYNC)
  580. update_mboxes(ring, mbox_reg);
  581. }
  582. }
  583. intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
  584. intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  585. intel_ring_emit(ring, ring->outstanding_lazy_seqno);
  586. intel_ring_emit(ring, MI_USER_INTERRUPT);
  587. __intel_ring_advance(ring);
  588. return 0;
  589. }
  590. static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
  591. u32 seqno)
  592. {
  593. struct drm_i915_private *dev_priv = dev->dev_private;
  594. return dev_priv->last_seqno < seqno;
  595. }
  596. /**
  597. * intel_ring_sync - sync the waiter to the signaller on seqno
  598. *
  599. * @waiter - ring that is waiting
  600. * @signaller - ring which has, or will signal
  601. * @seqno - seqno which the waiter will block on
  602. */
  603. static int
  604. gen6_ring_sync(struct intel_ring_buffer *waiter,
  605. struct intel_ring_buffer *signaller,
  606. u32 seqno)
  607. {
  608. int ret;
  609. u32 dw1 = MI_SEMAPHORE_MBOX |
  610. MI_SEMAPHORE_COMPARE |
  611. MI_SEMAPHORE_REGISTER;
  612. /* Throughout all of the GEM code, seqno passed implies our current
  613. * seqno is >= the last seqno executed. However for hardware the
  614. * comparison is strictly greater than.
  615. */
  616. seqno -= 1;
  617. WARN_ON(signaller->semaphore_register[waiter->id] ==
  618. MI_SEMAPHORE_SYNC_INVALID);
  619. ret = intel_ring_begin(waiter, 4);
  620. if (ret)
  621. return ret;
  622. /* If seqno wrap happened, omit the wait with no-ops */
  623. if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
  624. intel_ring_emit(waiter,
  625. dw1 |
  626. signaller->semaphore_register[waiter->id]);
  627. intel_ring_emit(waiter, seqno);
  628. intel_ring_emit(waiter, 0);
  629. intel_ring_emit(waiter, MI_NOOP);
  630. } else {
  631. intel_ring_emit(waiter, MI_NOOP);
  632. intel_ring_emit(waiter, MI_NOOP);
  633. intel_ring_emit(waiter, MI_NOOP);
  634. intel_ring_emit(waiter, MI_NOOP);
  635. }
  636. intel_ring_advance(waiter);
  637. return 0;
  638. }
  639. #define PIPE_CONTROL_FLUSH(ring__, addr__) \
  640. do { \
  641. intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
  642. PIPE_CONTROL_DEPTH_STALL); \
  643. intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
  644. intel_ring_emit(ring__, 0); \
  645. intel_ring_emit(ring__, 0); \
  646. } while (0)
  647. static int
  648. pc_render_add_request(struct intel_ring_buffer *ring)
  649. {
  650. u32 scratch_addr = ring->scratch.gtt_offset + 128;
  651. int ret;
  652. /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
  653. * incoherent with writes to memory, i.e. completely fubar,
  654. * so we need to use PIPE_NOTIFY instead.
  655. *
  656. * However, we also need to workaround the qword write
  657. * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
  658. * memory before requesting an interrupt.
  659. */
  660. ret = intel_ring_begin(ring, 32);
  661. if (ret)
  662. return ret;
  663. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
  664. PIPE_CONTROL_WRITE_FLUSH |
  665. PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
  666. intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
  667. intel_ring_emit(ring, ring->outstanding_lazy_seqno);
  668. intel_ring_emit(ring, 0);
  669. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  670. scratch_addr += 128; /* write to separate cachelines */
  671. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  672. scratch_addr += 128;
  673. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  674. scratch_addr += 128;
  675. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  676. scratch_addr += 128;
  677. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  678. scratch_addr += 128;
  679. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  680. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
  681. PIPE_CONTROL_WRITE_FLUSH |
  682. PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
  683. PIPE_CONTROL_NOTIFY);
  684. intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
  685. intel_ring_emit(ring, ring->outstanding_lazy_seqno);
  686. intel_ring_emit(ring, 0);
  687. __intel_ring_advance(ring);
  688. return 0;
  689. }
  690. static u32
  691. gen6_ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
  692. {
  693. /* Workaround to force correct ordering between irq and seqno writes on
  694. * ivb (and maybe also on snb) by reading from a CS register (like
  695. * ACTHD) before reading the status page. */
  696. if (!lazy_coherency)
  697. intel_ring_get_active_head(ring);
  698. return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
  699. }
  700. static u32
  701. ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
  702. {
  703. return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
  704. }
  705. static void
  706. ring_set_seqno(struct intel_ring_buffer *ring, u32 seqno)
  707. {
  708. intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
  709. }
  710. static u32
  711. pc_render_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
  712. {
  713. return ring->scratch.cpu_page[0];
  714. }
  715. static void
  716. pc_render_set_seqno(struct intel_ring_buffer *ring, u32 seqno)
  717. {
  718. ring->scratch.cpu_page[0] = seqno;
  719. }
  720. static bool
  721. gen5_ring_get_irq(struct intel_ring_buffer *ring)
  722. {
  723. struct drm_device *dev = ring->dev;
  724. drm_i915_private_t *dev_priv = dev->dev_private;
  725. unsigned long flags;
  726. if (!dev->irq_enabled)
  727. return false;
  728. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  729. if (ring->irq_refcount++ == 0)
  730. ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
  731. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  732. return true;
  733. }
  734. static void
  735. gen5_ring_put_irq(struct intel_ring_buffer *ring)
  736. {
  737. struct drm_device *dev = ring->dev;
  738. drm_i915_private_t *dev_priv = dev->dev_private;
  739. unsigned long flags;
  740. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  741. if (--ring->irq_refcount == 0)
  742. ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
  743. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  744. }
  745. static bool
  746. i9xx_ring_get_irq(struct intel_ring_buffer *ring)
  747. {
  748. struct drm_device *dev = ring->dev;
  749. drm_i915_private_t *dev_priv = dev->dev_private;
  750. unsigned long flags;
  751. if (!dev->irq_enabled)
  752. return false;
  753. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  754. if (ring->irq_refcount++ == 0) {
  755. dev_priv->irq_mask &= ~ring->irq_enable_mask;
  756. I915_WRITE(IMR, dev_priv->irq_mask);
  757. POSTING_READ(IMR);
  758. }
  759. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  760. return true;
  761. }
  762. static void
  763. i9xx_ring_put_irq(struct intel_ring_buffer *ring)
  764. {
  765. struct drm_device *dev = ring->dev;
  766. drm_i915_private_t *dev_priv = dev->dev_private;
  767. unsigned long flags;
  768. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  769. if (--ring->irq_refcount == 0) {
  770. dev_priv->irq_mask |= ring->irq_enable_mask;
  771. I915_WRITE(IMR, dev_priv->irq_mask);
  772. POSTING_READ(IMR);
  773. }
  774. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  775. }
  776. static bool
  777. i8xx_ring_get_irq(struct intel_ring_buffer *ring)
  778. {
  779. struct drm_device *dev = ring->dev;
  780. drm_i915_private_t *dev_priv = dev->dev_private;
  781. unsigned long flags;
  782. if (!dev->irq_enabled)
  783. return false;
  784. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  785. if (ring->irq_refcount++ == 0) {
  786. dev_priv->irq_mask &= ~ring->irq_enable_mask;
  787. I915_WRITE16(IMR, dev_priv->irq_mask);
  788. POSTING_READ16(IMR);
  789. }
  790. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  791. return true;
  792. }
  793. static void
  794. i8xx_ring_put_irq(struct intel_ring_buffer *ring)
  795. {
  796. struct drm_device *dev = ring->dev;
  797. drm_i915_private_t *dev_priv = dev->dev_private;
  798. unsigned long flags;
  799. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  800. if (--ring->irq_refcount == 0) {
  801. dev_priv->irq_mask |= ring->irq_enable_mask;
  802. I915_WRITE16(IMR, dev_priv->irq_mask);
  803. POSTING_READ16(IMR);
  804. }
  805. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  806. }
  807. void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
  808. {
  809. struct drm_device *dev = ring->dev;
  810. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  811. u32 mmio = 0;
  812. /* The ring status page addresses are no longer next to the rest of
  813. * the ring registers as of gen7.
  814. */
  815. if (IS_GEN7(dev)) {
  816. switch (ring->id) {
  817. case RCS:
  818. mmio = RENDER_HWS_PGA_GEN7;
  819. break;
  820. case BCS:
  821. mmio = BLT_HWS_PGA_GEN7;
  822. break;
  823. case VCS:
  824. mmio = BSD_HWS_PGA_GEN7;
  825. break;
  826. case VECS:
  827. mmio = VEBOX_HWS_PGA_GEN7;
  828. break;
  829. }
  830. } else if (IS_GEN6(ring->dev)) {
  831. mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
  832. } else {
  833. /* XXX: gen8 returns to sanity */
  834. mmio = RING_HWS_PGA(ring->mmio_base);
  835. }
  836. I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
  837. POSTING_READ(mmio);
  838. /* Flush the TLB for this page */
  839. if (INTEL_INFO(dev)->gen >= 6) {
  840. u32 reg = RING_INSTPM(ring->mmio_base);
  841. I915_WRITE(reg,
  842. _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
  843. INSTPM_SYNC_FLUSH));
  844. if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
  845. 1000))
  846. DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
  847. ring->name);
  848. }
  849. }
  850. static int
  851. bsd_ring_flush(struct intel_ring_buffer *ring,
  852. u32 invalidate_domains,
  853. u32 flush_domains)
  854. {
  855. int ret;
  856. ret = intel_ring_begin(ring, 2);
  857. if (ret)
  858. return ret;
  859. intel_ring_emit(ring, MI_FLUSH);
  860. intel_ring_emit(ring, MI_NOOP);
  861. intel_ring_advance(ring);
  862. return 0;
  863. }
  864. static int
  865. i9xx_add_request(struct intel_ring_buffer *ring)
  866. {
  867. int ret;
  868. ret = intel_ring_begin(ring, 4);
  869. if (ret)
  870. return ret;
  871. intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
  872. intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  873. intel_ring_emit(ring, ring->outstanding_lazy_seqno);
  874. intel_ring_emit(ring, MI_USER_INTERRUPT);
  875. __intel_ring_advance(ring);
  876. return 0;
  877. }
  878. static bool
  879. gen6_ring_get_irq(struct intel_ring_buffer *ring)
  880. {
  881. struct drm_device *dev = ring->dev;
  882. drm_i915_private_t *dev_priv = dev->dev_private;
  883. unsigned long flags;
  884. if (!dev->irq_enabled)
  885. return false;
  886. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  887. if (ring->irq_refcount++ == 0) {
  888. if (HAS_L3_DPF(dev) && ring->id == RCS)
  889. I915_WRITE_IMR(ring,
  890. ~(ring->irq_enable_mask |
  891. GT_PARITY_ERROR(dev)));
  892. else
  893. I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
  894. ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
  895. }
  896. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  897. return true;
  898. }
  899. static void
  900. gen6_ring_put_irq(struct intel_ring_buffer *ring)
  901. {
  902. struct drm_device *dev = ring->dev;
  903. drm_i915_private_t *dev_priv = dev->dev_private;
  904. unsigned long flags;
  905. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  906. if (--ring->irq_refcount == 0) {
  907. if (HAS_L3_DPF(dev) && ring->id == RCS)
  908. I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
  909. else
  910. I915_WRITE_IMR(ring, ~0);
  911. ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
  912. }
  913. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  914. }
  915. static bool
  916. hsw_vebox_get_irq(struct intel_ring_buffer *ring)
  917. {
  918. struct drm_device *dev = ring->dev;
  919. struct drm_i915_private *dev_priv = dev->dev_private;
  920. unsigned long flags;
  921. if (!dev->irq_enabled)
  922. return false;
  923. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  924. if (ring->irq_refcount++ == 0) {
  925. I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
  926. snb_enable_pm_irq(dev_priv, ring->irq_enable_mask);
  927. }
  928. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  929. return true;
  930. }
  931. static void
  932. hsw_vebox_put_irq(struct intel_ring_buffer *ring)
  933. {
  934. struct drm_device *dev = ring->dev;
  935. struct drm_i915_private *dev_priv = dev->dev_private;
  936. unsigned long flags;
  937. if (!dev->irq_enabled)
  938. return;
  939. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  940. if (--ring->irq_refcount == 0) {
  941. I915_WRITE_IMR(ring, ~0);
  942. snb_disable_pm_irq(dev_priv, ring->irq_enable_mask);
  943. }
  944. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  945. }
  946. static bool
  947. gen8_ring_get_irq(struct intel_ring_buffer *ring)
  948. {
  949. struct drm_device *dev = ring->dev;
  950. struct drm_i915_private *dev_priv = dev->dev_private;
  951. unsigned long flags;
  952. if (!dev->irq_enabled)
  953. return false;
  954. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  955. if (ring->irq_refcount++ == 0) {
  956. if (HAS_L3_DPF(dev) && ring->id == RCS) {
  957. I915_WRITE_IMR(ring,
  958. ~(ring->irq_enable_mask |
  959. GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
  960. } else {
  961. I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
  962. }
  963. POSTING_READ(RING_IMR(ring->mmio_base));
  964. }
  965. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  966. return true;
  967. }
  968. static void
  969. gen8_ring_put_irq(struct intel_ring_buffer *ring)
  970. {
  971. struct drm_device *dev = ring->dev;
  972. struct drm_i915_private *dev_priv = dev->dev_private;
  973. unsigned long flags;
  974. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  975. if (--ring->irq_refcount == 0) {
  976. if (HAS_L3_DPF(dev) && ring->id == RCS) {
  977. I915_WRITE_IMR(ring,
  978. ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
  979. } else {
  980. I915_WRITE_IMR(ring, ~0);
  981. }
  982. POSTING_READ(RING_IMR(ring->mmio_base));
  983. }
  984. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  985. }
  986. static int
  987. i965_dispatch_execbuffer(struct intel_ring_buffer *ring,
  988. u32 offset, u32 length,
  989. unsigned flags)
  990. {
  991. int ret;
  992. ret = intel_ring_begin(ring, 2);
  993. if (ret)
  994. return ret;
  995. intel_ring_emit(ring,
  996. MI_BATCH_BUFFER_START |
  997. MI_BATCH_GTT |
  998. (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
  999. intel_ring_emit(ring, offset);
  1000. intel_ring_advance(ring);
  1001. return 0;
  1002. }
  1003. /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
  1004. #define I830_BATCH_LIMIT (256*1024)
  1005. static int
  1006. i830_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1007. u32 offset, u32 len,
  1008. unsigned flags)
  1009. {
  1010. int ret;
  1011. if (flags & I915_DISPATCH_PINNED) {
  1012. ret = intel_ring_begin(ring, 4);
  1013. if (ret)
  1014. return ret;
  1015. intel_ring_emit(ring, MI_BATCH_BUFFER);
  1016. intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
  1017. intel_ring_emit(ring, offset + len - 8);
  1018. intel_ring_emit(ring, MI_NOOP);
  1019. intel_ring_advance(ring);
  1020. } else {
  1021. u32 cs_offset = ring->scratch.gtt_offset;
  1022. if (len > I830_BATCH_LIMIT)
  1023. return -ENOSPC;
  1024. ret = intel_ring_begin(ring, 9+3);
  1025. if (ret)
  1026. return ret;
  1027. /* Blit the batch (which has now all relocs applied) to the stable batch
  1028. * scratch bo area (so that the CS never stumbles over its tlb
  1029. * invalidation bug) ... */
  1030. intel_ring_emit(ring, XY_SRC_COPY_BLT_CMD |
  1031. XY_SRC_COPY_BLT_WRITE_ALPHA |
  1032. XY_SRC_COPY_BLT_WRITE_RGB);
  1033. intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_GXCOPY | 4096);
  1034. intel_ring_emit(ring, 0);
  1035. intel_ring_emit(ring, (DIV_ROUND_UP(len, 4096) << 16) | 1024);
  1036. intel_ring_emit(ring, cs_offset);
  1037. intel_ring_emit(ring, 0);
  1038. intel_ring_emit(ring, 4096);
  1039. intel_ring_emit(ring, offset);
  1040. intel_ring_emit(ring, MI_FLUSH);
  1041. /* ... and execute it. */
  1042. intel_ring_emit(ring, MI_BATCH_BUFFER);
  1043. intel_ring_emit(ring, cs_offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
  1044. intel_ring_emit(ring, cs_offset + len - 8);
  1045. intel_ring_advance(ring);
  1046. }
  1047. return 0;
  1048. }
  1049. static int
  1050. i915_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1051. u32 offset, u32 len,
  1052. unsigned flags)
  1053. {
  1054. int ret;
  1055. ret = intel_ring_begin(ring, 2);
  1056. if (ret)
  1057. return ret;
  1058. intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
  1059. intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
  1060. intel_ring_advance(ring);
  1061. return 0;
  1062. }
  1063. static void cleanup_status_page(struct intel_ring_buffer *ring)
  1064. {
  1065. struct drm_i915_gem_object *obj;
  1066. obj = ring->status_page.obj;
  1067. if (obj == NULL)
  1068. return;
  1069. kunmap(sg_page(obj->pages->sgl));
  1070. i915_gem_object_ggtt_unpin(obj);
  1071. drm_gem_object_unreference(&obj->base);
  1072. ring->status_page.obj = NULL;
  1073. }
  1074. static int init_status_page(struct intel_ring_buffer *ring)
  1075. {
  1076. struct drm_device *dev = ring->dev;
  1077. struct drm_i915_gem_object *obj;
  1078. int ret;
  1079. obj = i915_gem_alloc_object(dev, 4096);
  1080. if (obj == NULL) {
  1081. DRM_ERROR("Failed to allocate status page\n");
  1082. ret = -ENOMEM;
  1083. goto err;
  1084. }
  1085. i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  1086. ret = i915_gem_obj_ggtt_pin(obj, 4096, true, false);
  1087. if (ret != 0) {
  1088. goto err_unref;
  1089. }
  1090. ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
  1091. ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
  1092. if (ring->status_page.page_addr == NULL) {
  1093. ret = -ENOMEM;
  1094. goto err_unpin;
  1095. }
  1096. ring->status_page.obj = obj;
  1097. memset(ring->status_page.page_addr, 0, PAGE_SIZE);
  1098. DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
  1099. ring->name, ring->status_page.gfx_addr);
  1100. return 0;
  1101. err_unpin:
  1102. i915_gem_object_ggtt_unpin(obj);
  1103. err_unref:
  1104. drm_gem_object_unreference(&obj->base);
  1105. err:
  1106. return ret;
  1107. }
  1108. static int init_phys_status_page(struct intel_ring_buffer *ring)
  1109. {
  1110. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1111. if (!dev_priv->status_page_dmah) {
  1112. dev_priv->status_page_dmah =
  1113. drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
  1114. if (!dev_priv->status_page_dmah)
  1115. return -ENOMEM;
  1116. }
  1117. ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
  1118. memset(ring->status_page.page_addr, 0, PAGE_SIZE);
  1119. return 0;
  1120. }
  1121. static int intel_init_ring_buffer(struct drm_device *dev,
  1122. struct intel_ring_buffer *ring)
  1123. {
  1124. struct drm_i915_gem_object *obj;
  1125. struct drm_i915_private *dev_priv = dev->dev_private;
  1126. int ret;
  1127. ring->dev = dev;
  1128. INIT_LIST_HEAD(&ring->active_list);
  1129. INIT_LIST_HEAD(&ring->request_list);
  1130. ring->size = 32 * PAGE_SIZE;
  1131. memset(ring->sync_seqno, 0, sizeof(ring->sync_seqno));
  1132. init_waitqueue_head(&ring->irq_queue);
  1133. if (I915_NEED_GFX_HWS(dev)) {
  1134. ret = init_status_page(ring);
  1135. if (ret)
  1136. return ret;
  1137. } else {
  1138. BUG_ON(ring->id != RCS);
  1139. ret = init_phys_status_page(ring);
  1140. if (ret)
  1141. return ret;
  1142. }
  1143. obj = NULL;
  1144. if (!HAS_LLC(dev))
  1145. obj = i915_gem_object_create_stolen(dev, ring->size);
  1146. if (obj == NULL)
  1147. obj = i915_gem_alloc_object(dev, ring->size);
  1148. if (obj == NULL) {
  1149. DRM_ERROR("Failed to allocate ringbuffer\n");
  1150. ret = -ENOMEM;
  1151. goto err_hws;
  1152. }
  1153. ring->obj = obj;
  1154. ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, true, false);
  1155. if (ret)
  1156. goto err_unref;
  1157. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  1158. if (ret)
  1159. goto err_unpin;
  1160. ring->virtual_start =
  1161. ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
  1162. ring->size);
  1163. if (ring->virtual_start == NULL) {
  1164. DRM_ERROR("Failed to map ringbuffer.\n");
  1165. ret = -EINVAL;
  1166. goto err_unpin;
  1167. }
  1168. ret = ring->init(ring);
  1169. if (ret)
  1170. goto err_unmap;
  1171. /* Workaround an erratum on the i830 which causes a hang if
  1172. * the TAIL pointer points to within the last 2 cachelines
  1173. * of the buffer.
  1174. */
  1175. ring->effective_size = ring->size;
  1176. if (IS_I830(ring->dev) || IS_845G(ring->dev))
  1177. ring->effective_size -= 128;
  1178. return 0;
  1179. err_unmap:
  1180. iounmap(ring->virtual_start);
  1181. err_unpin:
  1182. i915_gem_object_ggtt_unpin(obj);
  1183. err_unref:
  1184. drm_gem_object_unreference(&obj->base);
  1185. ring->obj = NULL;
  1186. err_hws:
  1187. cleanup_status_page(ring);
  1188. return ret;
  1189. }
  1190. void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
  1191. {
  1192. struct drm_i915_private *dev_priv;
  1193. int ret;
  1194. if (ring->obj == NULL)
  1195. return;
  1196. /* Disable the ring buffer. The ring must be idle at this point */
  1197. dev_priv = ring->dev->dev_private;
  1198. ret = intel_ring_idle(ring);
  1199. if (ret && !i915_reset_in_progress(&dev_priv->gpu_error))
  1200. DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
  1201. ring->name, ret);
  1202. I915_WRITE_CTL(ring, 0);
  1203. iounmap(ring->virtual_start);
  1204. i915_gem_object_ggtt_unpin(ring->obj);
  1205. drm_gem_object_unreference(&ring->obj->base);
  1206. ring->obj = NULL;
  1207. ring->preallocated_lazy_request = NULL;
  1208. ring->outstanding_lazy_seqno = 0;
  1209. if (ring->cleanup)
  1210. ring->cleanup(ring);
  1211. cleanup_status_page(ring);
  1212. }
  1213. static int intel_ring_wait_request(struct intel_ring_buffer *ring, int n)
  1214. {
  1215. struct drm_i915_gem_request *request;
  1216. u32 seqno = 0, tail;
  1217. int ret;
  1218. if (ring->last_retired_head != -1) {
  1219. ring->head = ring->last_retired_head;
  1220. ring->last_retired_head = -1;
  1221. ring->space = ring_space(ring);
  1222. if (ring->space >= n)
  1223. return 0;
  1224. }
  1225. list_for_each_entry(request, &ring->request_list, list) {
  1226. int space;
  1227. if (request->tail == -1)
  1228. continue;
  1229. space = request->tail - (ring->tail + I915_RING_FREE_SPACE);
  1230. if (space < 0)
  1231. space += ring->size;
  1232. if (space >= n) {
  1233. seqno = request->seqno;
  1234. tail = request->tail;
  1235. break;
  1236. }
  1237. /* Consume this request in case we need more space than
  1238. * is available and so need to prevent a race between
  1239. * updating last_retired_head and direct reads of
  1240. * I915_RING_HEAD. It also provides a nice sanity check.
  1241. */
  1242. request->tail = -1;
  1243. }
  1244. if (seqno == 0)
  1245. return -ENOSPC;
  1246. ret = i915_wait_seqno(ring, seqno);
  1247. if (ret)
  1248. return ret;
  1249. ring->head = tail;
  1250. ring->space = ring_space(ring);
  1251. if (WARN_ON(ring->space < n))
  1252. return -ENOSPC;
  1253. return 0;
  1254. }
  1255. static int ring_wait_for_space(struct intel_ring_buffer *ring, int n)
  1256. {
  1257. struct drm_device *dev = ring->dev;
  1258. struct drm_i915_private *dev_priv = dev->dev_private;
  1259. unsigned long end;
  1260. int ret;
  1261. ret = intel_ring_wait_request(ring, n);
  1262. if (ret != -ENOSPC)
  1263. return ret;
  1264. /* force the tail write in case we have been skipping them */
  1265. __intel_ring_advance(ring);
  1266. trace_i915_ring_wait_begin(ring);
  1267. /* With GEM the hangcheck timer should kick us out of the loop,
  1268. * leaving it early runs the risk of corrupting GEM state (due
  1269. * to running on almost untested codepaths). But on resume
  1270. * timers don't work yet, so prevent a complete hang in that
  1271. * case by choosing an insanely large timeout. */
  1272. end = jiffies + 60 * HZ;
  1273. do {
  1274. ring->head = I915_READ_HEAD(ring);
  1275. ring->space = ring_space(ring);
  1276. if (ring->space >= n) {
  1277. trace_i915_ring_wait_end(ring);
  1278. return 0;
  1279. }
  1280. if (dev->primary->master) {
  1281. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  1282. if (master_priv->sarea_priv)
  1283. master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
  1284. }
  1285. msleep(1);
  1286. ret = i915_gem_check_wedge(&dev_priv->gpu_error,
  1287. dev_priv->mm.interruptible);
  1288. if (ret)
  1289. return ret;
  1290. } while (!time_after(jiffies, end));
  1291. trace_i915_ring_wait_end(ring);
  1292. return -EBUSY;
  1293. }
  1294. static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
  1295. {
  1296. uint32_t __iomem *virt;
  1297. int rem = ring->size - ring->tail;
  1298. if (ring->space < rem) {
  1299. int ret = ring_wait_for_space(ring, rem);
  1300. if (ret)
  1301. return ret;
  1302. }
  1303. virt = ring->virtual_start + ring->tail;
  1304. rem /= 4;
  1305. while (rem--)
  1306. iowrite32(MI_NOOP, virt++);
  1307. ring->tail = 0;
  1308. ring->space = ring_space(ring);
  1309. return 0;
  1310. }
  1311. int intel_ring_idle(struct intel_ring_buffer *ring)
  1312. {
  1313. u32 seqno;
  1314. int ret;
  1315. /* We need to add any requests required to flush the objects and ring */
  1316. if (ring->outstanding_lazy_seqno) {
  1317. ret = i915_add_request(ring, NULL);
  1318. if (ret)
  1319. return ret;
  1320. }
  1321. /* Wait upon the last request to be completed */
  1322. if (list_empty(&ring->request_list))
  1323. return 0;
  1324. seqno = list_entry(ring->request_list.prev,
  1325. struct drm_i915_gem_request,
  1326. list)->seqno;
  1327. return i915_wait_seqno(ring, seqno);
  1328. }
  1329. static int
  1330. intel_ring_alloc_seqno(struct intel_ring_buffer *ring)
  1331. {
  1332. if (ring->outstanding_lazy_seqno)
  1333. return 0;
  1334. if (ring->preallocated_lazy_request == NULL) {
  1335. struct drm_i915_gem_request *request;
  1336. request = kmalloc(sizeof(*request), GFP_KERNEL);
  1337. if (request == NULL)
  1338. return -ENOMEM;
  1339. ring->preallocated_lazy_request = request;
  1340. }
  1341. return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_seqno);
  1342. }
  1343. static int __intel_ring_prepare(struct intel_ring_buffer *ring,
  1344. int bytes)
  1345. {
  1346. int ret;
  1347. if (unlikely(ring->tail + bytes > ring->effective_size)) {
  1348. ret = intel_wrap_ring_buffer(ring);
  1349. if (unlikely(ret))
  1350. return ret;
  1351. }
  1352. if (unlikely(ring->space < bytes)) {
  1353. ret = ring_wait_for_space(ring, bytes);
  1354. if (unlikely(ret))
  1355. return ret;
  1356. }
  1357. return 0;
  1358. }
  1359. int intel_ring_begin(struct intel_ring_buffer *ring,
  1360. int num_dwords)
  1361. {
  1362. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1363. int ret;
  1364. ret = i915_gem_check_wedge(&dev_priv->gpu_error,
  1365. dev_priv->mm.interruptible);
  1366. if (ret)
  1367. return ret;
  1368. ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
  1369. if (ret)
  1370. return ret;
  1371. /* Preallocate the olr before touching the ring */
  1372. ret = intel_ring_alloc_seqno(ring);
  1373. if (ret)
  1374. return ret;
  1375. ring->space -= num_dwords * sizeof(uint32_t);
  1376. return 0;
  1377. }
  1378. /* Align the ring tail to a cacheline boundary */
  1379. int intel_ring_cacheline_align(struct intel_ring_buffer *ring)
  1380. {
  1381. int num_dwords = (64 - (ring->tail & 63)) / sizeof(uint32_t);
  1382. int ret;
  1383. if (num_dwords == 0)
  1384. return 0;
  1385. ret = intel_ring_begin(ring, num_dwords);
  1386. if (ret)
  1387. return ret;
  1388. while (num_dwords--)
  1389. intel_ring_emit(ring, MI_NOOP);
  1390. intel_ring_advance(ring);
  1391. return 0;
  1392. }
  1393. void intel_ring_init_seqno(struct intel_ring_buffer *ring, u32 seqno)
  1394. {
  1395. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1396. BUG_ON(ring->outstanding_lazy_seqno);
  1397. if (INTEL_INFO(ring->dev)->gen >= 6) {
  1398. I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
  1399. I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
  1400. if (HAS_VEBOX(ring->dev))
  1401. I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
  1402. }
  1403. ring->set_seqno(ring, seqno);
  1404. ring->hangcheck.seqno = seqno;
  1405. }
  1406. static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
  1407. u32 value)
  1408. {
  1409. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1410. /* Every tail move must follow the sequence below */
  1411. /* Disable notification that the ring is IDLE. The GT
  1412. * will then assume that it is busy and bring it out of rc6.
  1413. */
  1414. I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1415. _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  1416. /* Clear the context id. Here be magic! */
  1417. I915_WRITE64(GEN6_BSD_RNCID, 0x0);
  1418. /* Wait for the ring not to be idle, i.e. for it to wake up. */
  1419. if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
  1420. GEN6_BSD_SLEEP_INDICATOR) == 0,
  1421. 50))
  1422. DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
  1423. /* Now that the ring is fully powered up, update the tail */
  1424. I915_WRITE_TAIL(ring, value);
  1425. POSTING_READ(RING_TAIL(ring->mmio_base));
  1426. /* Let the ring send IDLE messages to the GT again,
  1427. * and so let it sleep to conserve power when idle.
  1428. */
  1429. I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1430. _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  1431. }
  1432. static int gen6_bsd_ring_flush(struct intel_ring_buffer *ring,
  1433. u32 invalidate, u32 flush)
  1434. {
  1435. uint32_t cmd;
  1436. int ret;
  1437. ret = intel_ring_begin(ring, 4);
  1438. if (ret)
  1439. return ret;
  1440. cmd = MI_FLUSH_DW;
  1441. if (INTEL_INFO(ring->dev)->gen >= 8)
  1442. cmd += 1;
  1443. /*
  1444. * Bspec vol 1c.5 - video engine command streamer:
  1445. * "If ENABLED, all TLBs will be invalidated once the flush
  1446. * operation is complete. This bit is only valid when the
  1447. * Post-Sync Operation field is a value of 1h or 3h."
  1448. */
  1449. if (invalidate & I915_GEM_GPU_DOMAINS)
  1450. cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
  1451. MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1452. intel_ring_emit(ring, cmd);
  1453. intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
  1454. if (INTEL_INFO(ring->dev)->gen >= 8) {
  1455. intel_ring_emit(ring, 0); /* upper addr */
  1456. intel_ring_emit(ring, 0); /* value */
  1457. } else {
  1458. intel_ring_emit(ring, 0);
  1459. intel_ring_emit(ring, MI_NOOP);
  1460. }
  1461. intel_ring_advance(ring);
  1462. return 0;
  1463. }
  1464. static int
  1465. gen8_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1466. u32 offset, u32 len,
  1467. unsigned flags)
  1468. {
  1469. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1470. bool ppgtt = dev_priv->mm.aliasing_ppgtt != NULL &&
  1471. !(flags & I915_DISPATCH_SECURE);
  1472. int ret;
  1473. ret = intel_ring_begin(ring, 4);
  1474. if (ret)
  1475. return ret;
  1476. /* FIXME(BDW): Address space and security selectors. */
  1477. intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
  1478. intel_ring_emit(ring, offset);
  1479. intel_ring_emit(ring, 0);
  1480. intel_ring_emit(ring, MI_NOOP);
  1481. intel_ring_advance(ring);
  1482. return 0;
  1483. }
  1484. static int
  1485. hsw_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1486. u32 offset, u32 len,
  1487. unsigned flags)
  1488. {
  1489. int ret;
  1490. ret = intel_ring_begin(ring, 2);
  1491. if (ret)
  1492. return ret;
  1493. intel_ring_emit(ring,
  1494. MI_BATCH_BUFFER_START | MI_BATCH_PPGTT_HSW |
  1495. (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_HSW));
  1496. /* bit0-7 is the length on GEN6+ */
  1497. intel_ring_emit(ring, offset);
  1498. intel_ring_advance(ring);
  1499. return 0;
  1500. }
  1501. static int
  1502. gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1503. u32 offset, u32 len,
  1504. unsigned flags)
  1505. {
  1506. int ret;
  1507. ret = intel_ring_begin(ring, 2);
  1508. if (ret)
  1509. return ret;
  1510. intel_ring_emit(ring,
  1511. MI_BATCH_BUFFER_START |
  1512. (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
  1513. /* bit0-7 is the length on GEN6+ */
  1514. intel_ring_emit(ring, offset);
  1515. intel_ring_advance(ring);
  1516. return 0;
  1517. }
  1518. /* Blitter support (SandyBridge+) */
  1519. static int gen6_ring_flush(struct intel_ring_buffer *ring,
  1520. u32 invalidate, u32 flush)
  1521. {
  1522. struct drm_device *dev = ring->dev;
  1523. uint32_t cmd;
  1524. int ret;
  1525. ret = intel_ring_begin(ring, 4);
  1526. if (ret)
  1527. return ret;
  1528. cmd = MI_FLUSH_DW;
  1529. if (INTEL_INFO(ring->dev)->gen >= 8)
  1530. cmd += 1;
  1531. /*
  1532. * Bspec vol 1c.3 - blitter engine command streamer:
  1533. * "If ENABLED, all TLBs will be invalidated once the flush
  1534. * operation is complete. This bit is only valid when the
  1535. * Post-Sync Operation field is a value of 1h or 3h."
  1536. */
  1537. if (invalidate & I915_GEM_DOMAIN_RENDER)
  1538. cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
  1539. MI_FLUSH_DW_OP_STOREDW;
  1540. intel_ring_emit(ring, cmd);
  1541. intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
  1542. if (INTEL_INFO(ring->dev)->gen >= 8) {
  1543. intel_ring_emit(ring, 0); /* upper addr */
  1544. intel_ring_emit(ring, 0); /* value */
  1545. } else {
  1546. intel_ring_emit(ring, 0);
  1547. intel_ring_emit(ring, MI_NOOP);
  1548. }
  1549. intel_ring_advance(ring);
  1550. if (IS_GEN7(dev) && !invalidate && flush)
  1551. return gen7_ring_fbc_flush(ring, FBC_REND_CACHE_CLEAN);
  1552. return 0;
  1553. }
  1554. int intel_init_render_ring_buffer(struct drm_device *dev)
  1555. {
  1556. drm_i915_private_t *dev_priv = dev->dev_private;
  1557. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  1558. ring->name = "render ring";
  1559. ring->id = RCS;
  1560. ring->mmio_base = RENDER_RING_BASE;
  1561. if (INTEL_INFO(dev)->gen >= 6) {
  1562. ring->add_request = gen6_add_request;
  1563. ring->flush = gen7_render_ring_flush;
  1564. if (INTEL_INFO(dev)->gen == 6)
  1565. ring->flush = gen6_render_ring_flush;
  1566. if (INTEL_INFO(dev)->gen >= 8) {
  1567. ring->flush = gen8_render_ring_flush;
  1568. ring->irq_get = gen8_ring_get_irq;
  1569. ring->irq_put = gen8_ring_put_irq;
  1570. } else {
  1571. ring->irq_get = gen6_ring_get_irq;
  1572. ring->irq_put = gen6_ring_put_irq;
  1573. }
  1574. ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
  1575. ring->get_seqno = gen6_ring_get_seqno;
  1576. ring->set_seqno = ring_set_seqno;
  1577. ring->sync_to = gen6_ring_sync;
  1578. ring->semaphore_register[RCS] = MI_SEMAPHORE_SYNC_INVALID;
  1579. ring->semaphore_register[VCS] = MI_SEMAPHORE_SYNC_RV;
  1580. ring->semaphore_register[BCS] = MI_SEMAPHORE_SYNC_RB;
  1581. ring->semaphore_register[VECS] = MI_SEMAPHORE_SYNC_RVE;
  1582. ring->signal_mbox[RCS] = GEN6_NOSYNC;
  1583. ring->signal_mbox[VCS] = GEN6_VRSYNC;
  1584. ring->signal_mbox[BCS] = GEN6_BRSYNC;
  1585. ring->signal_mbox[VECS] = GEN6_VERSYNC;
  1586. } else if (IS_GEN5(dev)) {
  1587. ring->add_request = pc_render_add_request;
  1588. ring->flush = gen4_render_ring_flush;
  1589. ring->get_seqno = pc_render_get_seqno;
  1590. ring->set_seqno = pc_render_set_seqno;
  1591. ring->irq_get = gen5_ring_get_irq;
  1592. ring->irq_put = gen5_ring_put_irq;
  1593. ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
  1594. GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
  1595. } else {
  1596. ring->add_request = i9xx_add_request;
  1597. if (INTEL_INFO(dev)->gen < 4)
  1598. ring->flush = gen2_render_ring_flush;
  1599. else
  1600. ring->flush = gen4_render_ring_flush;
  1601. ring->get_seqno = ring_get_seqno;
  1602. ring->set_seqno = ring_set_seqno;
  1603. if (IS_GEN2(dev)) {
  1604. ring->irq_get = i8xx_ring_get_irq;
  1605. ring->irq_put = i8xx_ring_put_irq;
  1606. } else {
  1607. ring->irq_get = i9xx_ring_get_irq;
  1608. ring->irq_put = i9xx_ring_put_irq;
  1609. }
  1610. ring->irq_enable_mask = I915_USER_INTERRUPT;
  1611. }
  1612. ring->write_tail = ring_write_tail;
  1613. if (IS_HASWELL(dev))
  1614. ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
  1615. else if (IS_GEN8(dev))
  1616. ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
  1617. else if (INTEL_INFO(dev)->gen >= 6)
  1618. ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
  1619. else if (INTEL_INFO(dev)->gen >= 4)
  1620. ring->dispatch_execbuffer = i965_dispatch_execbuffer;
  1621. else if (IS_I830(dev) || IS_845G(dev))
  1622. ring->dispatch_execbuffer = i830_dispatch_execbuffer;
  1623. else
  1624. ring->dispatch_execbuffer = i915_dispatch_execbuffer;
  1625. ring->init = init_render_ring;
  1626. ring->cleanup = render_ring_cleanup;
  1627. /* Workaround batchbuffer to combat CS tlb bug. */
  1628. if (HAS_BROKEN_CS_TLB(dev)) {
  1629. struct drm_i915_gem_object *obj;
  1630. int ret;
  1631. obj = i915_gem_alloc_object(dev, I830_BATCH_LIMIT);
  1632. if (obj == NULL) {
  1633. DRM_ERROR("Failed to allocate batch bo\n");
  1634. return -ENOMEM;
  1635. }
  1636. ret = i915_gem_obj_ggtt_pin(obj, 0, true, false);
  1637. if (ret != 0) {
  1638. drm_gem_object_unreference(&obj->base);
  1639. DRM_ERROR("Failed to ping batch bo\n");
  1640. return ret;
  1641. }
  1642. ring->scratch.obj = obj;
  1643. ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
  1644. }
  1645. return intel_init_ring_buffer(dev, ring);
  1646. }
  1647. int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
  1648. {
  1649. drm_i915_private_t *dev_priv = dev->dev_private;
  1650. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  1651. int ret;
  1652. ring->name = "render ring";
  1653. ring->id = RCS;
  1654. ring->mmio_base = RENDER_RING_BASE;
  1655. if (INTEL_INFO(dev)->gen >= 6) {
  1656. /* non-kms not supported on gen6+ */
  1657. return -ENODEV;
  1658. }
  1659. /* Note: gem is not supported on gen5/ilk without kms (the corresponding
  1660. * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
  1661. * the special gen5 functions. */
  1662. ring->add_request = i9xx_add_request;
  1663. if (INTEL_INFO(dev)->gen < 4)
  1664. ring->flush = gen2_render_ring_flush;
  1665. else
  1666. ring->flush = gen4_render_ring_flush;
  1667. ring->get_seqno = ring_get_seqno;
  1668. ring->set_seqno = ring_set_seqno;
  1669. if (IS_GEN2(dev)) {
  1670. ring->irq_get = i8xx_ring_get_irq;
  1671. ring->irq_put = i8xx_ring_put_irq;
  1672. } else {
  1673. ring->irq_get = i9xx_ring_get_irq;
  1674. ring->irq_put = i9xx_ring_put_irq;
  1675. }
  1676. ring->irq_enable_mask = I915_USER_INTERRUPT;
  1677. ring->write_tail = ring_write_tail;
  1678. if (INTEL_INFO(dev)->gen >= 4)
  1679. ring->dispatch_execbuffer = i965_dispatch_execbuffer;
  1680. else if (IS_I830(dev) || IS_845G(dev))
  1681. ring->dispatch_execbuffer = i830_dispatch_execbuffer;
  1682. else
  1683. ring->dispatch_execbuffer = i915_dispatch_execbuffer;
  1684. ring->init = init_render_ring;
  1685. ring->cleanup = render_ring_cleanup;
  1686. ring->dev = dev;
  1687. INIT_LIST_HEAD(&ring->active_list);
  1688. INIT_LIST_HEAD(&ring->request_list);
  1689. ring->size = size;
  1690. ring->effective_size = ring->size;
  1691. if (IS_I830(ring->dev) || IS_845G(ring->dev))
  1692. ring->effective_size -= 128;
  1693. ring->virtual_start = ioremap_wc(start, size);
  1694. if (ring->virtual_start == NULL) {
  1695. DRM_ERROR("can not ioremap virtual address for"
  1696. " ring buffer\n");
  1697. return -ENOMEM;
  1698. }
  1699. if (!I915_NEED_GFX_HWS(dev)) {
  1700. ret = init_phys_status_page(ring);
  1701. if (ret)
  1702. return ret;
  1703. }
  1704. return 0;
  1705. }
  1706. int intel_init_bsd_ring_buffer(struct drm_device *dev)
  1707. {
  1708. drm_i915_private_t *dev_priv = dev->dev_private;
  1709. struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
  1710. ring->name = "bsd ring";
  1711. ring->id = VCS;
  1712. ring->write_tail = ring_write_tail;
  1713. if (INTEL_INFO(dev)->gen >= 6) {
  1714. ring->mmio_base = GEN6_BSD_RING_BASE;
  1715. /* gen6 bsd needs a special wa for tail updates */
  1716. if (IS_GEN6(dev))
  1717. ring->write_tail = gen6_bsd_ring_write_tail;
  1718. ring->flush = gen6_bsd_ring_flush;
  1719. ring->add_request = gen6_add_request;
  1720. ring->get_seqno = gen6_ring_get_seqno;
  1721. ring->set_seqno = ring_set_seqno;
  1722. if (INTEL_INFO(dev)->gen >= 8) {
  1723. ring->irq_enable_mask =
  1724. GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1725. ring->irq_get = gen8_ring_get_irq;
  1726. ring->irq_put = gen8_ring_put_irq;
  1727. ring->dispatch_execbuffer =
  1728. gen8_ring_dispatch_execbuffer;
  1729. } else {
  1730. ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
  1731. ring->irq_get = gen6_ring_get_irq;
  1732. ring->irq_put = gen6_ring_put_irq;
  1733. ring->dispatch_execbuffer =
  1734. gen6_ring_dispatch_execbuffer;
  1735. }
  1736. ring->sync_to = gen6_ring_sync;
  1737. ring->semaphore_register[RCS] = MI_SEMAPHORE_SYNC_VR;
  1738. ring->semaphore_register[VCS] = MI_SEMAPHORE_SYNC_INVALID;
  1739. ring->semaphore_register[BCS] = MI_SEMAPHORE_SYNC_VB;
  1740. ring->semaphore_register[VECS] = MI_SEMAPHORE_SYNC_VVE;
  1741. ring->signal_mbox[RCS] = GEN6_RVSYNC;
  1742. ring->signal_mbox[VCS] = GEN6_NOSYNC;
  1743. ring->signal_mbox[BCS] = GEN6_BVSYNC;
  1744. ring->signal_mbox[VECS] = GEN6_VEVSYNC;
  1745. } else {
  1746. ring->mmio_base = BSD_RING_BASE;
  1747. ring->flush = bsd_ring_flush;
  1748. ring->add_request = i9xx_add_request;
  1749. ring->get_seqno = ring_get_seqno;
  1750. ring->set_seqno = ring_set_seqno;
  1751. if (IS_GEN5(dev)) {
  1752. ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
  1753. ring->irq_get = gen5_ring_get_irq;
  1754. ring->irq_put = gen5_ring_put_irq;
  1755. } else {
  1756. ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
  1757. ring->irq_get = i9xx_ring_get_irq;
  1758. ring->irq_put = i9xx_ring_put_irq;
  1759. }
  1760. ring->dispatch_execbuffer = i965_dispatch_execbuffer;
  1761. }
  1762. ring->init = init_ring_common;
  1763. return intel_init_ring_buffer(dev, ring);
  1764. }
  1765. int intel_init_blt_ring_buffer(struct drm_device *dev)
  1766. {
  1767. drm_i915_private_t *dev_priv = dev->dev_private;
  1768. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  1769. ring->name = "blitter ring";
  1770. ring->id = BCS;
  1771. ring->mmio_base = BLT_RING_BASE;
  1772. ring->write_tail = ring_write_tail;
  1773. ring->flush = gen6_ring_flush;
  1774. ring->add_request = gen6_add_request;
  1775. ring->get_seqno = gen6_ring_get_seqno;
  1776. ring->set_seqno = ring_set_seqno;
  1777. if (INTEL_INFO(dev)->gen >= 8) {
  1778. ring->irq_enable_mask =
  1779. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1780. ring->irq_get = gen8_ring_get_irq;
  1781. ring->irq_put = gen8_ring_put_irq;
  1782. ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
  1783. } else {
  1784. ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
  1785. ring->irq_get = gen6_ring_get_irq;
  1786. ring->irq_put = gen6_ring_put_irq;
  1787. ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
  1788. }
  1789. ring->sync_to = gen6_ring_sync;
  1790. ring->semaphore_register[RCS] = MI_SEMAPHORE_SYNC_BR;
  1791. ring->semaphore_register[VCS] = MI_SEMAPHORE_SYNC_BV;
  1792. ring->semaphore_register[BCS] = MI_SEMAPHORE_SYNC_INVALID;
  1793. ring->semaphore_register[VECS] = MI_SEMAPHORE_SYNC_BVE;
  1794. ring->signal_mbox[RCS] = GEN6_RBSYNC;
  1795. ring->signal_mbox[VCS] = GEN6_VBSYNC;
  1796. ring->signal_mbox[BCS] = GEN6_NOSYNC;
  1797. ring->signal_mbox[VECS] = GEN6_VEBSYNC;
  1798. ring->init = init_ring_common;
  1799. return intel_init_ring_buffer(dev, ring);
  1800. }
  1801. int intel_init_vebox_ring_buffer(struct drm_device *dev)
  1802. {
  1803. drm_i915_private_t *dev_priv = dev->dev_private;
  1804. struct intel_ring_buffer *ring = &dev_priv->ring[VECS];
  1805. ring->name = "video enhancement ring";
  1806. ring->id = VECS;
  1807. ring->mmio_base = VEBOX_RING_BASE;
  1808. ring->write_tail = ring_write_tail;
  1809. ring->flush = gen6_ring_flush;
  1810. ring->add_request = gen6_add_request;
  1811. ring->get_seqno = gen6_ring_get_seqno;
  1812. ring->set_seqno = ring_set_seqno;
  1813. if (INTEL_INFO(dev)->gen >= 8) {
  1814. ring->irq_enable_mask =
  1815. GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1816. ring->irq_get = gen8_ring_get_irq;
  1817. ring->irq_put = gen8_ring_put_irq;
  1818. ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
  1819. } else {
  1820. ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
  1821. ring->irq_get = hsw_vebox_get_irq;
  1822. ring->irq_put = hsw_vebox_put_irq;
  1823. ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
  1824. }
  1825. ring->sync_to = gen6_ring_sync;
  1826. ring->semaphore_register[RCS] = MI_SEMAPHORE_SYNC_VER;
  1827. ring->semaphore_register[VCS] = MI_SEMAPHORE_SYNC_VEV;
  1828. ring->semaphore_register[BCS] = MI_SEMAPHORE_SYNC_VEB;
  1829. ring->semaphore_register[VECS] = MI_SEMAPHORE_SYNC_INVALID;
  1830. ring->signal_mbox[RCS] = GEN6_RVESYNC;
  1831. ring->signal_mbox[VCS] = GEN6_VVESYNC;
  1832. ring->signal_mbox[BCS] = GEN6_BVESYNC;
  1833. ring->signal_mbox[VECS] = GEN6_NOSYNC;
  1834. ring->init = init_ring_common;
  1835. return intel_init_ring_buffer(dev, ring);
  1836. }
  1837. int
  1838. intel_ring_flush_all_caches(struct intel_ring_buffer *ring)
  1839. {
  1840. int ret;
  1841. if (!ring->gpu_caches_dirty)
  1842. return 0;
  1843. ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
  1844. if (ret)
  1845. return ret;
  1846. trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
  1847. ring->gpu_caches_dirty = false;
  1848. return 0;
  1849. }
  1850. int
  1851. intel_ring_invalidate_all_caches(struct intel_ring_buffer *ring)
  1852. {
  1853. uint32_t flush_domains;
  1854. int ret;
  1855. flush_domains = 0;
  1856. if (ring->gpu_caches_dirty)
  1857. flush_domains = I915_GEM_GPU_DOMAINS;
  1858. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
  1859. if (ret)
  1860. return ret;
  1861. trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
  1862. ring->gpu_caches_dirty = false;
  1863. return 0;
  1864. }