intel_ringbuffer.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  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. static 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 *ring)
  47. {
  48. if (ring->last_retired_head != -1) {
  49. ring->head = ring->last_retired_head;
  50. ring->last_retired_head = -1;
  51. }
  52. ring->space = __intel_ring_space(ring->head & HEAD_ADDR,
  53. ring->tail, ring->size);
  54. }
  55. static int
  56. gen2_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
  57. {
  58. u32 cmd, *cs;
  59. cmd = MI_FLUSH;
  60. if (mode & EMIT_INVALIDATE)
  61. cmd |= MI_READ_FLUSH;
  62. cs = intel_ring_begin(req, 2);
  63. if (IS_ERR(cs))
  64. return PTR_ERR(cs);
  65. *cs++ = cmd;
  66. *cs++ = MI_NOOP;
  67. intel_ring_advance(req, cs);
  68. return 0;
  69. }
  70. static int
  71. gen4_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
  72. {
  73. u32 cmd, *cs;
  74. /*
  75. * read/write caches:
  76. *
  77. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  78. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  79. * also flushed at 2d versus 3d pipeline switches.
  80. *
  81. * read-only caches:
  82. *
  83. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  84. * MI_READ_FLUSH is set, and is always flushed on 965.
  85. *
  86. * I915_GEM_DOMAIN_COMMAND may not exist?
  87. *
  88. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  89. * invalidated when MI_EXE_FLUSH is set.
  90. *
  91. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  92. * invalidated with every MI_FLUSH.
  93. *
  94. * TLBs:
  95. *
  96. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  97. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  98. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  99. * are flushed at any MI_FLUSH.
  100. */
  101. cmd = MI_FLUSH;
  102. if (mode & EMIT_INVALIDATE) {
  103. cmd |= MI_EXE_FLUSH;
  104. if (IS_G4X(req->i915) || IS_GEN5(req->i915))
  105. cmd |= MI_INVALIDATE_ISP;
  106. }
  107. cs = intel_ring_begin(req, 2);
  108. if (IS_ERR(cs))
  109. return PTR_ERR(cs);
  110. *cs++ = cmd;
  111. *cs++ = MI_NOOP;
  112. intel_ring_advance(req, cs);
  113. return 0;
  114. }
  115. /**
  116. * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
  117. * implementing two workarounds on gen6. From section 1.4.7.1
  118. * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
  119. *
  120. * [DevSNB-C+{W/A}] Before any depth stall flush (including those
  121. * produced by non-pipelined state commands), software needs to first
  122. * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
  123. * 0.
  124. *
  125. * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
  126. * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
  127. *
  128. * And the workaround for these two requires this workaround first:
  129. *
  130. * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
  131. * BEFORE the pipe-control with a post-sync op and no write-cache
  132. * flushes.
  133. *
  134. * And this last workaround is tricky because of the requirements on
  135. * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
  136. * volume 2 part 1:
  137. *
  138. * "1 of the following must also be set:
  139. * - Render Target Cache Flush Enable ([12] of DW1)
  140. * - Depth Cache Flush Enable ([0] of DW1)
  141. * - Stall at Pixel Scoreboard ([1] of DW1)
  142. * - Depth Stall ([13] of DW1)
  143. * - Post-Sync Operation ([13] of DW1)
  144. * - Notify Enable ([8] of DW1)"
  145. *
  146. * The cache flushes require the workaround flush that triggered this
  147. * one, so we can't use it. Depth stall would trigger the same.
  148. * Post-sync nonzero is what triggered this second workaround, so we
  149. * can't use that one either. Notify enable is IRQs, which aren't
  150. * really our business. That leaves only stall at scoreboard.
  151. */
  152. static int
  153. intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
  154. {
  155. u32 scratch_addr =
  156. i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
  157. u32 *cs;
  158. cs = intel_ring_begin(req, 6);
  159. if (IS_ERR(cs))
  160. return PTR_ERR(cs);
  161. *cs++ = GFX_OP_PIPE_CONTROL(5);
  162. *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
  163. *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
  164. *cs++ = 0; /* low dword */
  165. *cs++ = 0; /* high dword */
  166. *cs++ = MI_NOOP;
  167. intel_ring_advance(req, cs);
  168. cs = intel_ring_begin(req, 6);
  169. if (IS_ERR(cs))
  170. return PTR_ERR(cs);
  171. *cs++ = GFX_OP_PIPE_CONTROL(5);
  172. *cs++ = PIPE_CONTROL_QW_WRITE;
  173. *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
  174. *cs++ = 0;
  175. *cs++ = 0;
  176. *cs++ = MI_NOOP;
  177. intel_ring_advance(req, cs);
  178. return 0;
  179. }
  180. static int
  181. gen6_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
  182. {
  183. u32 scratch_addr =
  184. i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
  185. u32 *cs, flags = 0;
  186. int ret;
  187. /* Force SNB workarounds for PIPE_CONTROL flushes */
  188. ret = intel_emit_post_sync_nonzero_flush(req);
  189. if (ret)
  190. return ret;
  191. /* Just flush everything. Experiments have shown that reducing the
  192. * number of bits based on the write domains has little performance
  193. * impact.
  194. */
  195. if (mode & EMIT_FLUSH) {
  196. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  197. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  198. /*
  199. * Ensure that any following seqno writes only happen
  200. * when the render cache is indeed flushed.
  201. */
  202. flags |= PIPE_CONTROL_CS_STALL;
  203. }
  204. if (mode & EMIT_INVALIDATE) {
  205. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  206. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  207. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  208. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  209. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  210. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  211. /*
  212. * TLB invalidate requires a post-sync write.
  213. */
  214. flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
  215. }
  216. cs = intel_ring_begin(req, 4);
  217. if (IS_ERR(cs))
  218. return PTR_ERR(cs);
  219. *cs++ = GFX_OP_PIPE_CONTROL(4);
  220. *cs++ = flags;
  221. *cs++ = scratch_addr | PIPE_CONTROL_GLOBAL_GTT;
  222. *cs++ = 0;
  223. intel_ring_advance(req, cs);
  224. return 0;
  225. }
  226. static int
  227. gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
  228. {
  229. u32 *cs;
  230. cs = intel_ring_begin(req, 4);
  231. if (IS_ERR(cs))
  232. return PTR_ERR(cs);
  233. *cs++ = GFX_OP_PIPE_CONTROL(4);
  234. *cs++ = PIPE_CONTROL_CS_STALL | PIPE_CONTROL_STALL_AT_SCOREBOARD;
  235. *cs++ = 0;
  236. *cs++ = 0;
  237. intel_ring_advance(req, cs);
  238. return 0;
  239. }
  240. static int
  241. gen7_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
  242. {
  243. u32 scratch_addr =
  244. i915_ggtt_offset(req->engine->scratch) + 2 * CACHELINE_BYTES;
  245. u32 *cs, flags = 0;
  246. /*
  247. * Ensure that any following seqno writes only happen when the render
  248. * cache is indeed flushed.
  249. *
  250. * Workaround: 4th PIPE_CONTROL command (except the ones with only
  251. * read-cache invalidate bits set) must have the CS_STALL bit set. We
  252. * don't try to be clever and just set it unconditionally.
  253. */
  254. flags |= PIPE_CONTROL_CS_STALL;
  255. /* Just flush everything. Experiments have shown that reducing the
  256. * number of bits based on the write domains has little performance
  257. * impact.
  258. */
  259. if (mode & EMIT_FLUSH) {
  260. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  261. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  262. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  263. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  264. }
  265. if (mode & EMIT_INVALIDATE) {
  266. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  267. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  268. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  269. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  270. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  271. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  272. flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
  273. /*
  274. * TLB invalidate requires a post-sync write.
  275. */
  276. flags |= PIPE_CONTROL_QW_WRITE;
  277. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  278. flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
  279. /* Workaround: we must issue a pipe_control with CS-stall bit
  280. * set before a pipe_control command that has the state cache
  281. * invalidate bit set. */
  282. gen7_render_ring_cs_stall_wa(req);
  283. }
  284. cs = intel_ring_begin(req, 4);
  285. if (IS_ERR(cs))
  286. return PTR_ERR(cs);
  287. *cs++ = GFX_OP_PIPE_CONTROL(4);
  288. *cs++ = flags;
  289. *cs++ = scratch_addr;
  290. *cs++ = 0;
  291. intel_ring_advance(req, cs);
  292. return 0;
  293. }
  294. static int
  295. gen8_render_ring_flush(struct drm_i915_gem_request *req, u32 mode)
  296. {
  297. u32 flags;
  298. u32 *cs;
  299. cs = intel_ring_begin(req, mode & EMIT_INVALIDATE ? 12 : 6);
  300. if (IS_ERR(cs))
  301. return PTR_ERR(cs);
  302. flags = PIPE_CONTROL_CS_STALL;
  303. if (mode & EMIT_FLUSH) {
  304. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  305. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  306. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  307. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  308. }
  309. if (mode & EMIT_INVALIDATE) {
  310. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  311. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  312. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  313. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  314. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  315. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  316. flags |= PIPE_CONTROL_QW_WRITE;
  317. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  318. /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
  319. cs = gen8_emit_pipe_control(cs,
  320. PIPE_CONTROL_CS_STALL |
  321. PIPE_CONTROL_STALL_AT_SCOREBOARD,
  322. 0);
  323. }
  324. cs = gen8_emit_pipe_control(cs, flags,
  325. i915_ggtt_offset(req->engine->scratch) +
  326. 2 * CACHELINE_BYTES);
  327. intel_ring_advance(req, cs);
  328. return 0;
  329. }
  330. static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
  331. {
  332. struct drm_i915_private *dev_priv = engine->i915;
  333. u32 addr;
  334. addr = dev_priv->status_page_dmah->busaddr;
  335. if (INTEL_GEN(dev_priv) >= 4)
  336. addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
  337. I915_WRITE(HWS_PGA, addr);
  338. }
  339. static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
  340. {
  341. struct drm_i915_private *dev_priv = engine->i915;
  342. i915_reg_t mmio;
  343. /* The ring status page addresses are no longer next to the rest of
  344. * the ring registers as of gen7.
  345. */
  346. if (IS_GEN7(dev_priv)) {
  347. switch (engine->id) {
  348. case RCS:
  349. mmio = RENDER_HWS_PGA_GEN7;
  350. break;
  351. case BCS:
  352. mmio = BLT_HWS_PGA_GEN7;
  353. break;
  354. /*
  355. * VCS2 actually doesn't exist on Gen7. Only shut up
  356. * gcc switch check warning
  357. */
  358. case VCS2:
  359. case VCS:
  360. mmio = BSD_HWS_PGA_GEN7;
  361. break;
  362. case VECS:
  363. mmio = VEBOX_HWS_PGA_GEN7;
  364. break;
  365. }
  366. } else if (IS_GEN6(dev_priv)) {
  367. mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
  368. } else {
  369. /* XXX: gen8 returns to sanity */
  370. mmio = RING_HWS_PGA(engine->mmio_base);
  371. }
  372. I915_WRITE(mmio, engine->status_page.ggtt_offset);
  373. POSTING_READ(mmio);
  374. /*
  375. * Flush the TLB for this page
  376. *
  377. * FIXME: These two bits have disappeared on gen8, so a question
  378. * arises: do we still need this and if so how should we go about
  379. * invalidating the TLB?
  380. */
  381. if (IS_GEN(dev_priv, 6, 7)) {
  382. i915_reg_t reg = RING_INSTPM(engine->mmio_base);
  383. /* ring should be idle before issuing a sync flush*/
  384. WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
  385. I915_WRITE(reg,
  386. _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
  387. INSTPM_SYNC_FLUSH));
  388. if (intel_wait_for_register(dev_priv,
  389. reg, INSTPM_SYNC_FLUSH, 0,
  390. 1000))
  391. DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
  392. engine->name);
  393. }
  394. }
  395. static bool stop_ring(struct intel_engine_cs *engine)
  396. {
  397. struct drm_i915_private *dev_priv = engine->i915;
  398. if (INTEL_GEN(dev_priv) > 2) {
  399. I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
  400. if (intel_wait_for_register(dev_priv,
  401. RING_MI_MODE(engine->mmio_base),
  402. MODE_IDLE,
  403. MODE_IDLE,
  404. 1000)) {
  405. DRM_ERROR("%s : timed out trying to stop ring\n",
  406. engine->name);
  407. /* Sometimes we observe that the idle flag is not
  408. * set even though the ring is empty. So double
  409. * check before giving up.
  410. */
  411. if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
  412. return false;
  413. }
  414. }
  415. I915_WRITE_CTL(engine, 0);
  416. I915_WRITE_HEAD(engine, 0);
  417. I915_WRITE_TAIL(engine, 0);
  418. if (INTEL_GEN(dev_priv) > 2) {
  419. (void)I915_READ_CTL(engine);
  420. I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
  421. }
  422. return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
  423. }
  424. static int init_ring_common(struct intel_engine_cs *engine)
  425. {
  426. struct drm_i915_private *dev_priv = engine->i915;
  427. struct intel_ring *ring = engine->buffer;
  428. int ret = 0;
  429. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  430. if (!stop_ring(engine)) {
  431. /* G45 ring initialization often fails to reset head to zero */
  432. DRM_DEBUG_KMS("%s head not reset to zero "
  433. "ctl %08x head %08x tail %08x start %08x\n",
  434. engine->name,
  435. I915_READ_CTL(engine),
  436. I915_READ_HEAD(engine),
  437. I915_READ_TAIL(engine),
  438. I915_READ_START(engine));
  439. if (!stop_ring(engine)) {
  440. DRM_ERROR("failed to set %s head to zero "
  441. "ctl %08x head %08x tail %08x start %08x\n",
  442. engine->name,
  443. I915_READ_CTL(engine),
  444. I915_READ_HEAD(engine),
  445. I915_READ_TAIL(engine),
  446. I915_READ_START(engine));
  447. ret = -EIO;
  448. goto out;
  449. }
  450. }
  451. if (HWS_NEEDS_PHYSICAL(dev_priv))
  452. ring_setup_phys_status_page(engine);
  453. else
  454. intel_ring_setup_status_page(engine);
  455. intel_engine_reset_breadcrumbs(engine);
  456. /* Enforce ordering by reading HEAD register back */
  457. I915_READ_HEAD(engine);
  458. /* Initialize the ring. This must happen _after_ we've cleared the ring
  459. * registers with the above sequence (the readback of the HEAD registers
  460. * also enforces ordering), otherwise the hw might lose the new ring
  461. * register values. */
  462. I915_WRITE_START(engine, i915_ggtt_offset(ring->vma));
  463. /* WaClearRingBufHeadRegAtInit:ctg,elk */
  464. if (I915_READ_HEAD(engine))
  465. DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
  466. engine->name, I915_READ_HEAD(engine));
  467. intel_ring_update_space(ring);
  468. I915_WRITE_HEAD(engine, ring->head);
  469. I915_WRITE_TAIL(engine, ring->tail);
  470. (void)I915_READ_TAIL(engine);
  471. I915_WRITE_CTL(engine, RING_CTL_SIZE(ring->size) | RING_VALID);
  472. /* If the head is still not zero, the ring is dead */
  473. if (intel_wait_for_register_fw(dev_priv, RING_CTL(engine->mmio_base),
  474. RING_VALID, RING_VALID,
  475. 50)) {
  476. DRM_ERROR("%s initialization failed "
  477. "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
  478. engine->name,
  479. I915_READ_CTL(engine),
  480. I915_READ_CTL(engine) & RING_VALID,
  481. I915_READ_HEAD(engine), ring->head,
  482. I915_READ_TAIL(engine), ring->tail,
  483. I915_READ_START(engine),
  484. i915_ggtt_offset(ring->vma));
  485. ret = -EIO;
  486. goto out;
  487. }
  488. intel_engine_init_hangcheck(engine);
  489. out:
  490. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  491. return ret;
  492. }
  493. static void reset_ring_common(struct intel_engine_cs *engine,
  494. struct drm_i915_gem_request *request)
  495. {
  496. /* Try to restore the logical GPU state to match the continuation
  497. * of the request queue. If we skip the context/PD restore, then
  498. * the next request may try to execute assuming that its context
  499. * is valid and loaded on the GPU and so may try to access invalid
  500. * memory, prompting repeated GPU hangs.
  501. *
  502. * If the request was guilty, we still restore the logical state
  503. * in case the next request requires it (e.g. the aliasing ppgtt),
  504. * but skip over the hung batch.
  505. *
  506. * If the request was innocent, we try to replay the request with
  507. * the restored context.
  508. */
  509. if (request) {
  510. struct drm_i915_private *dev_priv = request->i915;
  511. struct intel_context *ce = &request->ctx->engine[engine->id];
  512. struct i915_hw_ppgtt *ppgtt;
  513. /* FIXME consider gen8 reset */
  514. if (ce->state) {
  515. I915_WRITE(CCID,
  516. i915_ggtt_offset(ce->state) |
  517. BIT(8) /* must be set! */ |
  518. CCID_EXTENDED_STATE_SAVE |
  519. CCID_EXTENDED_STATE_RESTORE |
  520. CCID_EN);
  521. }
  522. ppgtt = request->ctx->ppgtt ?: engine->i915->mm.aliasing_ppgtt;
  523. if (ppgtt) {
  524. u32 pd_offset = ppgtt->pd.base.ggtt_offset << 10;
  525. I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
  526. I915_WRITE(RING_PP_DIR_BASE(engine), pd_offset);
  527. /* Wait for the PD reload to complete */
  528. if (intel_wait_for_register(dev_priv,
  529. RING_PP_DIR_BASE(engine),
  530. BIT(0), 0,
  531. 10))
  532. DRM_ERROR("Wait for reload of ppgtt page-directory timed out\n");
  533. ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
  534. }
  535. /* If the rq hung, jump to its breadcrumb and skip the batch */
  536. if (request->fence.error == -EIO) {
  537. struct intel_ring *ring = request->ring;
  538. ring->head = request->postfix;
  539. ring->last_retired_head = -1;
  540. }
  541. } else {
  542. engine->legacy_active_context = NULL;
  543. }
  544. }
  545. static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
  546. {
  547. int ret;
  548. ret = intel_ring_workarounds_emit(req);
  549. if (ret != 0)
  550. return ret;
  551. ret = i915_gem_render_state_emit(req);
  552. if (ret)
  553. return ret;
  554. return 0;
  555. }
  556. static int init_render_ring(struct intel_engine_cs *engine)
  557. {
  558. struct drm_i915_private *dev_priv = engine->i915;
  559. int ret = init_ring_common(engine);
  560. if (ret)
  561. return ret;
  562. /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
  563. if (IS_GEN(dev_priv, 4, 6))
  564. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
  565. /* We need to disable the AsyncFlip performance optimisations in order
  566. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  567. * programmed to '1' on all products.
  568. *
  569. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
  570. */
  571. if (IS_GEN(dev_priv, 6, 7))
  572. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  573. /* Required for the hardware to program scanline values for waiting */
  574. /* WaEnableFlushTlbInvalidationMode:snb */
  575. if (IS_GEN6(dev_priv))
  576. I915_WRITE(GFX_MODE,
  577. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
  578. /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
  579. if (IS_GEN7(dev_priv))
  580. I915_WRITE(GFX_MODE_GEN7,
  581. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
  582. _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
  583. if (IS_GEN6(dev_priv)) {
  584. /* From the Sandybridge PRM, volume 1 part 3, page 24:
  585. * "If this bit is set, STCunit will have LRA as replacement
  586. * policy. [...] This bit must be reset. LRA replacement
  587. * policy is not supported."
  588. */
  589. I915_WRITE(CACHE_MODE_0,
  590. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  591. }
  592. if (IS_GEN(dev_priv, 6, 7))
  593. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  594. if (INTEL_INFO(dev_priv)->gen >= 6)
  595. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  596. return init_workarounds_ring(engine);
  597. }
  598. static void render_ring_cleanup(struct intel_engine_cs *engine)
  599. {
  600. struct drm_i915_private *dev_priv = engine->i915;
  601. i915_vma_unpin_and_release(&dev_priv->semaphore);
  602. }
  603. static u32 *gen8_rcs_signal(struct drm_i915_gem_request *req, u32 *cs)
  604. {
  605. struct drm_i915_private *dev_priv = req->i915;
  606. struct intel_engine_cs *waiter;
  607. enum intel_engine_id id;
  608. for_each_engine(waiter, dev_priv, id) {
  609. u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
  610. if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
  611. continue;
  612. *cs++ = GFX_OP_PIPE_CONTROL(6);
  613. *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_QW_WRITE |
  614. PIPE_CONTROL_CS_STALL;
  615. *cs++ = lower_32_bits(gtt_offset);
  616. *cs++ = upper_32_bits(gtt_offset);
  617. *cs++ = req->global_seqno;
  618. *cs++ = 0;
  619. *cs++ = MI_SEMAPHORE_SIGNAL |
  620. MI_SEMAPHORE_TARGET(waiter->hw_id);
  621. *cs++ = 0;
  622. }
  623. return cs;
  624. }
  625. static u32 *gen8_xcs_signal(struct drm_i915_gem_request *req, u32 *cs)
  626. {
  627. struct drm_i915_private *dev_priv = req->i915;
  628. struct intel_engine_cs *waiter;
  629. enum intel_engine_id id;
  630. for_each_engine(waiter, dev_priv, id) {
  631. u64 gtt_offset = req->engine->semaphore.signal_ggtt[id];
  632. if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
  633. continue;
  634. *cs++ = (MI_FLUSH_DW + 1) | MI_FLUSH_DW_OP_STOREDW;
  635. *cs++ = lower_32_bits(gtt_offset) | MI_FLUSH_DW_USE_GTT;
  636. *cs++ = upper_32_bits(gtt_offset);
  637. *cs++ = req->global_seqno;
  638. *cs++ = MI_SEMAPHORE_SIGNAL |
  639. MI_SEMAPHORE_TARGET(waiter->hw_id);
  640. *cs++ = 0;
  641. }
  642. return cs;
  643. }
  644. static u32 *gen6_signal(struct drm_i915_gem_request *req, u32 *cs)
  645. {
  646. struct drm_i915_private *dev_priv = req->i915;
  647. struct intel_engine_cs *engine;
  648. enum intel_engine_id id;
  649. int num_rings = 0;
  650. for_each_engine(engine, dev_priv, id) {
  651. i915_reg_t mbox_reg;
  652. if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
  653. continue;
  654. mbox_reg = req->engine->semaphore.mbox.signal[engine->hw_id];
  655. if (i915_mmio_reg_valid(mbox_reg)) {
  656. *cs++ = MI_LOAD_REGISTER_IMM(1);
  657. *cs++ = i915_mmio_reg_offset(mbox_reg);
  658. *cs++ = req->global_seqno;
  659. num_rings++;
  660. }
  661. }
  662. if (num_rings & 1)
  663. *cs++ = MI_NOOP;
  664. return cs;
  665. }
  666. static void i9xx_submit_request(struct drm_i915_gem_request *request)
  667. {
  668. struct drm_i915_private *dev_priv = request->i915;
  669. i915_gem_request_submit(request);
  670. GEM_BUG_ON(!IS_ALIGNED(request->tail, 8));
  671. I915_WRITE_TAIL(request->engine, request->tail);
  672. }
  673. static void i9xx_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
  674. {
  675. *cs++ = MI_STORE_DWORD_INDEX;
  676. *cs++ = I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT;
  677. *cs++ = req->global_seqno;
  678. *cs++ = MI_USER_INTERRUPT;
  679. req->tail = intel_ring_offset(req, cs);
  680. GEM_BUG_ON(!IS_ALIGNED(req->tail, 8));
  681. }
  682. static const int i9xx_emit_breadcrumb_sz = 4;
  683. /**
  684. * gen6_sema_emit_breadcrumb - Update the semaphore mailbox registers
  685. *
  686. * @request - request to write to the ring
  687. *
  688. * Update the mailbox registers in the *other* rings with the current seqno.
  689. * This acts like a signal in the canonical semaphore.
  690. */
  691. static void gen6_sema_emit_breadcrumb(struct drm_i915_gem_request *req, u32 *cs)
  692. {
  693. return i9xx_emit_breadcrumb(req,
  694. req->engine->semaphore.signal(req, cs));
  695. }
  696. static void gen8_render_emit_breadcrumb(struct drm_i915_gem_request *req,
  697. u32 *cs)
  698. {
  699. struct intel_engine_cs *engine = req->engine;
  700. if (engine->semaphore.signal)
  701. cs = engine->semaphore.signal(req, cs);
  702. *cs++ = GFX_OP_PIPE_CONTROL(6);
  703. *cs++ = PIPE_CONTROL_GLOBAL_GTT_IVB | PIPE_CONTROL_CS_STALL |
  704. PIPE_CONTROL_QW_WRITE;
  705. *cs++ = intel_hws_seqno_address(engine);
  706. *cs++ = 0;
  707. *cs++ = req->global_seqno;
  708. /* We're thrashing one dword of HWS. */
  709. *cs++ = 0;
  710. *cs++ = MI_USER_INTERRUPT;
  711. *cs++ = MI_NOOP;
  712. req->tail = intel_ring_offset(req, cs);
  713. GEM_BUG_ON(!IS_ALIGNED(req->tail, 8));
  714. }
  715. static const int gen8_render_emit_breadcrumb_sz = 8;
  716. /**
  717. * intel_ring_sync - sync the waiter to the signaller on seqno
  718. *
  719. * @waiter - ring that is waiting
  720. * @signaller - ring which has, or will signal
  721. * @seqno - seqno which the waiter will block on
  722. */
  723. static int
  724. gen8_ring_sync_to(struct drm_i915_gem_request *req,
  725. struct drm_i915_gem_request *signal)
  726. {
  727. struct drm_i915_private *dev_priv = req->i915;
  728. u64 offset = GEN8_WAIT_OFFSET(req->engine, signal->engine->id);
  729. struct i915_hw_ppgtt *ppgtt;
  730. u32 *cs;
  731. cs = intel_ring_begin(req, 4);
  732. if (IS_ERR(cs))
  733. return PTR_ERR(cs);
  734. *cs++ = MI_SEMAPHORE_WAIT | MI_SEMAPHORE_GLOBAL_GTT |
  735. MI_SEMAPHORE_SAD_GTE_SDD;
  736. *cs++ = signal->global_seqno;
  737. *cs++ = lower_32_bits(offset);
  738. *cs++ = upper_32_bits(offset);
  739. intel_ring_advance(req, cs);
  740. /* When the !RCS engines idle waiting upon a semaphore, they lose their
  741. * pagetables and we must reload them before executing the batch.
  742. * We do this on the i915_switch_context() following the wait and
  743. * before the dispatch.
  744. */
  745. ppgtt = req->ctx->ppgtt;
  746. if (ppgtt && req->engine->id != RCS)
  747. ppgtt->pd_dirty_rings |= intel_engine_flag(req->engine);
  748. return 0;
  749. }
  750. static int
  751. gen6_ring_sync_to(struct drm_i915_gem_request *req,
  752. struct drm_i915_gem_request *signal)
  753. {
  754. u32 dw1 = MI_SEMAPHORE_MBOX |
  755. MI_SEMAPHORE_COMPARE |
  756. MI_SEMAPHORE_REGISTER;
  757. u32 wait_mbox = signal->engine->semaphore.mbox.wait[req->engine->hw_id];
  758. u32 *cs;
  759. WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
  760. cs = intel_ring_begin(req, 4);
  761. if (IS_ERR(cs))
  762. return PTR_ERR(cs);
  763. *cs++ = dw1 | wait_mbox;
  764. /* Throughout all of the GEM code, seqno passed implies our current
  765. * seqno is >= the last seqno executed. However for hardware the
  766. * comparison is strictly greater than.
  767. */
  768. *cs++ = signal->global_seqno - 1;
  769. *cs++ = 0;
  770. *cs++ = MI_NOOP;
  771. intel_ring_advance(req, cs);
  772. return 0;
  773. }
  774. static void
  775. gen5_seqno_barrier(struct intel_engine_cs *engine)
  776. {
  777. /* MI_STORE are internally buffered by the GPU and not flushed
  778. * either by MI_FLUSH or SyncFlush or any other combination of
  779. * MI commands.
  780. *
  781. * "Only the submission of the store operation is guaranteed.
  782. * The write result will be complete (coherent) some time later
  783. * (this is practically a finite period but there is no guaranteed
  784. * latency)."
  785. *
  786. * Empirically, we observe that we need a delay of at least 75us to
  787. * be sure that the seqno write is visible by the CPU.
  788. */
  789. usleep_range(125, 250);
  790. }
  791. static void
  792. gen6_seqno_barrier(struct intel_engine_cs *engine)
  793. {
  794. struct drm_i915_private *dev_priv = engine->i915;
  795. /* Workaround to force correct ordering between irq and seqno writes on
  796. * ivb (and maybe also on snb) by reading from a CS register (like
  797. * ACTHD) before reading the status page.
  798. *
  799. * Note that this effectively stalls the read by the time it takes to
  800. * do a memory transaction, which more or less ensures that the write
  801. * from the GPU has sufficient time to invalidate the CPU cacheline.
  802. * Alternatively we could delay the interrupt from the CS ring to give
  803. * the write time to land, but that would incur a delay after every
  804. * batch i.e. much more frequent than a delay when waiting for the
  805. * interrupt (with the same net latency).
  806. *
  807. * Also note that to prevent whole machine hangs on gen7, we have to
  808. * take the spinlock to guard against concurrent cacheline access.
  809. */
  810. spin_lock_irq(&dev_priv->uncore.lock);
  811. POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
  812. spin_unlock_irq(&dev_priv->uncore.lock);
  813. }
  814. static void
  815. gen5_irq_enable(struct intel_engine_cs *engine)
  816. {
  817. gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
  818. }
  819. static void
  820. gen5_irq_disable(struct intel_engine_cs *engine)
  821. {
  822. gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
  823. }
  824. static void
  825. i9xx_irq_enable(struct intel_engine_cs *engine)
  826. {
  827. struct drm_i915_private *dev_priv = engine->i915;
  828. dev_priv->irq_mask &= ~engine->irq_enable_mask;
  829. I915_WRITE(IMR, dev_priv->irq_mask);
  830. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  831. }
  832. static void
  833. i9xx_irq_disable(struct intel_engine_cs *engine)
  834. {
  835. struct drm_i915_private *dev_priv = engine->i915;
  836. dev_priv->irq_mask |= engine->irq_enable_mask;
  837. I915_WRITE(IMR, dev_priv->irq_mask);
  838. }
  839. static void
  840. i8xx_irq_enable(struct intel_engine_cs *engine)
  841. {
  842. struct drm_i915_private *dev_priv = engine->i915;
  843. dev_priv->irq_mask &= ~engine->irq_enable_mask;
  844. I915_WRITE16(IMR, dev_priv->irq_mask);
  845. POSTING_READ16(RING_IMR(engine->mmio_base));
  846. }
  847. static void
  848. i8xx_irq_disable(struct intel_engine_cs *engine)
  849. {
  850. struct drm_i915_private *dev_priv = engine->i915;
  851. dev_priv->irq_mask |= engine->irq_enable_mask;
  852. I915_WRITE16(IMR, dev_priv->irq_mask);
  853. }
  854. static int
  855. bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
  856. {
  857. u32 *cs;
  858. cs = intel_ring_begin(req, 2);
  859. if (IS_ERR(cs))
  860. return PTR_ERR(cs);
  861. *cs++ = MI_FLUSH;
  862. *cs++ = MI_NOOP;
  863. intel_ring_advance(req, cs);
  864. return 0;
  865. }
  866. static void
  867. gen6_irq_enable(struct intel_engine_cs *engine)
  868. {
  869. struct drm_i915_private *dev_priv = engine->i915;
  870. I915_WRITE_IMR(engine,
  871. ~(engine->irq_enable_mask |
  872. engine->irq_keep_mask));
  873. gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
  874. }
  875. static void
  876. gen6_irq_disable(struct intel_engine_cs *engine)
  877. {
  878. struct drm_i915_private *dev_priv = engine->i915;
  879. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  880. gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
  881. }
  882. static void
  883. hsw_vebox_irq_enable(struct intel_engine_cs *engine)
  884. {
  885. struct drm_i915_private *dev_priv = engine->i915;
  886. I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
  887. gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
  888. }
  889. static void
  890. hsw_vebox_irq_disable(struct intel_engine_cs *engine)
  891. {
  892. struct drm_i915_private *dev_priv = engine->i915;
  893. I915_WRITE_IMR(engine, ~0);
  894. gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
  895. }
  896. static void
  897. gen8_irq_enable(struct intel_engine_cs *engine)
  898. {
  899. struct drm_i915_private *dev_priv = engine->i915;
  900. I915_WRITE_IMR(engine,
  901. ~(engine->irq_enable_mask |
  902. engine->irq_keep_mask));
  903. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  904. }
  905. static void
  906. gen8_irq_disable(struct intel_engine_cs *engine)
  907. {
  908. struct drm_i915_private *dev_priv = engine->i915;
  909. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  910. }
  911. static int
  912. i965_emit_bb_start(struct drm_i915_gem_request *req,
  913. u64 offset, u32 length,
  914. unsigned int dispatch_flags)
  915. {
  916. u32 *cs;
  917. cs = intel_ring_begin(req, 2);
  918. if (IS_ERR(cs))
  919. return PTR_ERR(cs);
  920. *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags &
  921. I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965);
  922. *cs++ = offset;
  923. intel_ring_advance(req, cs);
  924. return 0;
  925. }
  926. /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
  927. #define I830_BATCH_LIMIT (256*1024)
  928. #define I830_TLB_ENTRIES (2)
  929. #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
  930. static int
  931. i830_emit_bb_start(struct drm_i915_gem_request *req,
  932. u64 offset, u32 len,
  933. unsigned int dispatch_flags)
  934. {
  935. u32 *cs, cs_offset = i915_ggtt_offset(req->engine->scratch);
  936. cs = intel_ring_begin(req, 6);
  937. if (IS_ERR(cs))
  938. return PTR_ERR(cs);
  939. /* Evict the invalid PTE TLBs */
  940. *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA;
  941. *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096;
  942. *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */
  943. *cs++ = cs_offset;
  944. *cs++ = 0xdeadbeef;
  945. *cs++ = MI_NOOP;
  946. intel_ring_advance(req, cs);
  947. if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
  948. if (len > I830_BATCH_LIMIT)
  949. return -ENOSPC;
  950. cs = intel_ring_begin(req, 6 + 2);
  951. if (IS_ERR(cs))
  952. return PTR_ERR(cs);
  953. /* Blit the batch (which has now all relocs applied) to the
  954. * stable batch scratch bo area (so that the CS never
  955. * stumbles over its tlb invalidation bug) ...
  956. */
  957. *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA;
  958. *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096;
  959. *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096;
  960. *cs++ = cs_offset;
  961. *cs++ = 4096;
  962. *cs++ = offset;
  963. *cs++ = MI_FLUSH;
  964. *cs++ = MI_NOOP;
  965. intel_ring_advance(req, cs);
  966. /* ... and execute it. */
  967. offset = cs_offset;
  968. }
  969. cs = intel_ring_begin(req, 2);
  970. if (IS_ERR(cs))
  971. return PTR_ERR(cs);
  972. *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
  973. *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
  974. MI_BATCH_NON_SECURE);
  975. intel_ring_advance(req, cs);
  976. return 0;
  977. }
  978. static int
  979. i915_emit_bb_start(struct drm_i915_gem_request *req,
  980. u64 offset, u32 len,
  981. unsigned int dispatch_flags)
  982. {
  983. u32 *cs;
  984. cs = intel_ring_begin(req, 2);
  985. if (IS_ERR(cs))
  986. return PTR_ERR(cs);
  987. *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
  988. *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
  989. MI_BATCH_NON_SECURE);
  990. intel_ring_advance(req, cs);
  991. return 0;
  992. }
  993. static void cleanup_phys_status_page(struct intel_engine_cs *engine)
  994. {
  995. struct drm_i915_private *dev_priv = engine->i915;
  996. if (!dev_priv->status_page_dmah)
  997. return;
  998. drm_pci_free(&dev_priv->drm, dev_priv->status_page_dmah);
  999. engine->status_page.page_addr = NULL;
  1000. }
  1001. static void cleanup_status_page(struct intel_engine_cs *engine)
  1002. {
  1003. struct i915_vma *vma;
  1004. struct drm_i915_gem_object *obj;
  1005. vma = fetch_and_zero(&engine->status_page.vma);
  1006. if (!vma)
  1007. return;
  1008. obj = vma->obj;
  1009. i915_vma_unpin(vma);
  1010. i915_vma_close(vma);
  1011. i915_gem_object_unpin_map(obj);
  1012. __i915_gem_object_release_unless_active(obj);
  1013. }
  1014. static int init_status_page(struct intel_engine_cs *engine)
  1015. {
  1016. struct drm_i915_gem_object *obj;
  1017. struct i915_vma *vma;
  1018. unsigned int flags;
  1019. void *vaddr;
  1020. int ret;
  1021. obj = i915_gem_object_create_internal(engine->i915, PAGE_SIZE);
  1022. if (IS_ERR(obj)) {
  1023. DRM_ERROR("Failed to allocate status page\n");
  1024. return PTR_ERR(obj);
  1025. }
  1026. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  1027. if (ret)
  1028. goto err;
  1029. vma = i915_vma_instance(obj, &engine->i915->ggtt.base, NULL);
  1030. if (IS_ERR(vma)) {
  1031. ret = PTR_ERR(vma);
  1032. goto err;
  1033. }
  1034. flags = PIN_GLOBAL;
  1035. if (!HAS_LLC(engine->i915))
  1036. /* On g33, we cannot place HWS above 256MiB, so
  1037. * restrict its pinning to the low mappable arena.
  1038. * Though this restriction is not documented for
  1039. * gen4, gen5, or byt, they also behave similarly
  1040. * and hang if the HWS is placed at the top of the
  1041. * GTT. To generalise, it appears that all !llc
  1042. * platforms have issues with us placing the HWS
  1043. * above the mappable region (even though we never
  1044. * actualy map it).
  1045. */
  1046. flags |= PIN_MAPPABLE;
  1047. ret = i915_vma_pin(vma, 0, 4096, flags);
  1048. if (ret)
  1049. goto err;
  1050. vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
  1051. if (IS_ERR(vaddr)) {
  1052. ret = PTR_ERR(vaddr);
  1053. goto err_unpin;
  1054. }
  1055. engine->status_page.vma = vma;
  1056. engine->status_page.ggtt_offset = i915_ggtt_offset(vma);
  1057. engine->status_page.page_addr = memset(vaddr, 0, PAGE_SIZE);
  1058. DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
  1059. engine->name, i915_ggtt_offset(vma));
  1060. return 0;
  1061. err_unpin:
  1062. i915_vma_unpin(vma);
  1063. err:
  1064. i915_gem_object_put(obj);
  1065. return ret;
  1066. }
  1067. static int init_phys_status_page(struct intel_engine_cs *engine)
  1068. {
  1069. struct drm_i915_private *dev_priv = engine->i915;
  1070. dev_priv->status_page_dmah =
  1071. drm_pci_alloc(&dev_priv->drm, PAGE_SIZE, PAGE_SIZE);
  1072. if (!dev_priv->status_page_dmah)
  1073. return -ENOMEM;
  1074. engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
  1075. memset(engine->status_page.page_addr, 0, PAGE_SIZE);
  1076. return 0;
  1077. }
  1078. int intel_ring_pin(struct intel_ring *ring, unsigned int offset_bias)
  1079. {
  1080. unsigned int flags;
  1081. enum i915_map_type map;
  1082. struct i915_vma *vma = ring->vma;
  1083. void *addr;
  1084. int ret;
  1085. GEM_BUG_ON(ring->vaddr);
  1086. map = HAS_LLC(ring->engine->i915) ? I915_MAP_WB : I915_MAP_WC;
  1087. flags = PIN_GLOBAL;
  1088. if (offset_bias)
  1089. flags |= PIN_OFFSET_BIAS | offset_bias;
  1090. if (vma->obj->stolen)
  1091. flags |= PIN_MAPPABLE;
  1092. if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
  1093. if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
  1094. ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
  1095. else
  1096. ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
  1097. if (unlikely(ret))
  1098. return ret;
  1099. }
  1100. ret = i915_vma_pin(vma, 0, PAGE_SIZE, flags);
  1101. if (unlikely(ret))
  1102. return ret;
  1103. if (i915_vma_is_map_and_fenceable(vma))
  1104. addr = (void __force *)i915_vma_pin_iomap(vma);
  1105. else
  1106. addr = i915_gem_object_pin_map(vma->obj, map);
  1107. if (IS_ERR(addr))
  1108. goto err;
  1109. ring->vaddr = addr;
  1110. return 0;
  1111. err:
  1112. i915_vma_unpin(vma);
  1113. return PTR_ERR(addr);
  1114. }
  1115. void intel_ring_unpin(struct intel_ring *ring)
  1116. {
  1117. GEM_BUG_ON(!ring->vma);
  1118. GEM_BUG_ON(!ring->vaddr);
  1119. if (i915_vma_is_map_and_fenceable(ring->vma))
  1120. i915_vma_unpin_iomap(ring->vma);
  1121. else
  1122. i915_gem_object_unpin_map(ring->vma->obj);
  1123. ring->vaddr = NULL;
  1124. i915_vma_unpin(ring->vma);
  1125. }
  1126. static struct i915_vma *
  1127. intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
  1128. {
  1129. struct drm_i915_gem_object *obj;
  1130. struct i915_vma *vma;
  1131. obj = i915_gem_object_create_stolen(dev_priv, size);
  1132. if (!obj)
  1133. obj = i915_gem_object_create(dev_priv, size);
  1134. if (IS_ERR(obj))
  1135. return ERR_CAST(obj);
  1136. /* mark ring buffers as read-only from GPU side by default */
  1137. obj->gt_ro = 1;
  1138. vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
  1139. if (IS_ERR(vma))
  1140. goto err;
  1141. return vma;
  1142. err:
  1143. i915_gem_object_put(obj);
  1144. return vma;
  1145. }
  1146. struct intel_ring *
  1147. intel_engine_create_ring(struct intel_engine_cs *engine, int size)
  1148. {
  1149. struct intel_ring *ring;
  1150. struct i915_vma *vma;
  1151. GEM_BUG_ON(!is_power_of_2(size));
  1152. GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
  1153. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  1154. if (!ring)
  1155. return ERR_PTR(-ENOMEM);
  1156. ring->engine = engine;
  1157. INIT_LIST_HEAD(&ring->request_list);
  1158. ring->size = size;
  1159. /* Workaround an erratum on the i830 which causes a hang if
  1160. * the TAIL pointer points to within the last 2 cachelines
  1161. * of the buffer.
  1162. */
  1163. ring->effective_size = size;
  1164. if (IS_I830(engine->i915) || IS_I845G(engine->i915))
  1165. ring->effective_size -= 2 * CACHELINE_BYTES;
  1166. ring->last_retired_head = -1;
  1167. intel_ring_update_space(ring);
  1168. vma = intel_ring_create_vma(engine->i915, size);
  1169. if (IS_ERR(vma)) {
  1170. kfree(ring);
  1171. return ERR_CAST(vma);
  1172. }
  1173. ring->vma = vma;
  1174. return ring;
  1175. }
  1176. void
  1177. intel_ring_free(struct intel_ring *ring)
  1178. {
  1179. struct drm_i915_gem_object *obj = ring->vma->obj;
  1180. i915_vma_close(ring->vma);
  1181. __i915_gem_object_release_unless_active(obj);
  1182. kfree(ring);
  1183. }
  1184. static int context_pin(struct i915_gem_context *ctx)
  1185. {
  1186. struct i915_vma *vma = ctx->engine[RCS].state;
  1187. int ret;
  1188. /* Clear this page out of any CPU caches for coherent swap-in/out.
  1189. * We only want to do this on the first bind so that we do not stall
  1190. * on an active context (which by nature is already on the GPU).
  1191. */
  1192. if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
  1193. ret = i915_gem_object_set_to_gtt_domain(vma->obj, false);
  1194. if (ret)
  1195. return ret;
  1196. }
  1197. return i915_vma_pin(vma, 0, I915_GTT_MIN_ALIGNMENT,
  1198. PIN_GLOBAL | PIN_HIGH);
  1199. }
  1200. static int intel_ring_context_pin(struct intel_engine_cs *engine,
  1201. struct i915_gem_context *ctx)
  1202. {
  1203. struct intel_context *ce = &ctx->engine[engine->id];
  1204. int ret;
  1205. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  1206. if (ce->pin_count++)
  1207. return 0;
  1208. GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
  1209. if (ce->state) {
  1210. ret = context_pin(ctx);
  1211. if (ret)
  1212. goto error;
  1213. ce->state->obj->mm.dirty = true;
  1214. }
  1215. /* The kernel context is only used as a placeholder for flushing the
  1216. * active context. It is never used for submitting user rendering and
  1217. * as such never requires the golden render context, and so we can skip
  1218. * emitting it when we switch to the kernel context. This is required
  1219. * as during eviction we cannot allocate and pin the renderstate in
  1220. * order to initialise the context.
  1221. */
  1222. if (i915_gem_context_is_kernel(ctx))
  1223. ce->initialised = true;
  1224. i915_gem_context_get(ctx);
  1225. return 0;
  1226. error:
  1227. ce->pin_count = 0;
  1228. return ret;
  1229. }
  1230. static void intel_ring_context_unpin(struct intel_engine_cs *engine,
  1231. struct i915_gem_context *ctx)
  1232. {
  1233. struct intel_context *ce = &ctx->engine[engine->id];
  1234. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  1235. GEM_BUG_ON(ce->pin_count == 0);
  1236. if (--ce->pin_count)
  1237. return;
  1238. if (ce->state)
  1239. i915_vma_unpin(ce->state);
  1240. i915_gem_context_put(ctx);
  1241. }
  1242. static int intel_init_ring_buffer(struct intel_engine_cs *engine)
  1243. {
  1244. struct drm_i915_private *dev_priv = engine->i915;
  1245. struct intel_ring *ring;
  1246. int ret;
  1247. WARN_ON(engine->buffer);
  1248. intel_engine_setup_common(engine);
  1249. ret = intel_engine_init_common(engine);
  1250. if (ret)
  1251. goto error;
  1252. ring = intel_engine_create_ring(engine, 32 * PAGE_SIZE);
  1253. if (IS_ERR(ring)) {
  1254. ret = PTR_ERR(ring);
  1255. goto error;
  1256. }
  1257. if (HWS_NEEDS_PHYSICAL(dev_priv)) {
  1258. WARN_ON(engine->id != RCS);
  1259. ret = init_phys_status_page(engine);
  1260. if (ret)
  1261. goto error;
  1262. } else {
  1263. ret = init_status_page(engine);
  1264. if (ret)
  1265. goto error;
  1266. }
  1267. /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
  1268. ret = intel_ring_pin(ring, I915_GTT_PAGE_SIZE);
  1269. if (ret) {
  1270. intel_ring_free(ring);
  1271. goto error;
  1272. }
  1273. engine->buffer = ring;
  1274. return 0;
  1275. error:
  1276. intel_engine_cleanup(engine);
  1277. return ret;
  1278. }
  1279. void intel_engine_cleanup(struct intel_engine_cs *engine)
  1280. {
  1281. struct drm_i915_private *dev_priv;
  1282. dev_priv = engine->i915;
  1283. if (engine->buffer) {
  1284. WARN_ON(INTEL_GEN(dev_priv) > 2 &&
  1285. (I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1286. intel_ring_unpin(engine->buffer);
  1287. intel_ring_free(engine->buffer);
  1288. engine->buffer = NULL;
  1289. }
  1290. if (engine->cleanup)
  1291. engine->cleanup(engine);
  1292. if (HWS_NEEDS_PHYSICAL(dev_priv)) {
  1293. WARN_ON(engine->id != RCS);
  1294. cleanup_phys_status_page(engine);
  1295. } else {
  1296. cleanup_status_page(engine);
  1297. }
  1298. intel_engine_cleanup_common(engine);
  1299. engine->i915 = NULL;
  1300. dev_priv->engine[engine->id] = NULL;
  1301. kfree(engine);
  1302. }
  1303. void intel_legacy_submission_resume(struct drm_i915_private *dev_priv)
  1304. {
  1305. struct intel_engine_cs *engine;
  1306. enum intel_engine_id id;
  1307. for_each_engine(engine, dev_priv, id) {
  1308. engine->buffer->head = engine->buffer->tail;
  1309. engine->buffer->last_retired_head = -1;
  1310. }
  1311. }
  1312. static int ring_request_alloc(struct drm_i915_gem_request *request)
  1313. {
  1314. u32 *cs;
  1315. GEM_BUG_ON(!request->ctx->engine[request->engine->id].pin_count);
  1316. /* Flush enough space to reduce the likelihood of waiting after
  1317. * we start building the request - in which case we will just
  1318. * have to repeat work.
  1319. */
  1320. request->reserved_space += LEGACY_REQUEST_SIZE;
  1321. GEM_BUG_ON(!request->engine->buffer);
  1322. request->ring = request->engine->buffer;
  1323. cs = intel_ring_begin(request, 0);
  1324. if (IS_ERR(cs))
  1325. return PTR_ERR(cs);
  1326. request->reserved_space -= LEGACY_REQUEST_SIZE;
  1327. return 0;
  1328. }
  1329. static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
  1330. {
  1331. struct intel_ring *ring = req->ring;
  1332. struct drm_i915_gem_request *target;
  1333. long timeout;
  1334. lockdep_assert_held(&req->i915->drm.struct_mutex);
  1335. intel_ring_update_space(ring);
  1336. if (ring->space >= bytes)
  1337. return 0;
  1338. /*
  1339. * Space is reserved in the ringbuffer for finalising the request,
  1340. * as that cannot be allowed to fail. During request finalisation,
  1341. * reserved_space is set to 0 to stop the overallocation and the
  1342. * assumption is that then we never need to wait (which has the
  1343. * risk of failing with EINTR).
  1344. *
  1345. * See also i915_gem_request_alloc() and i915_add_request().
  1346. */
  1347. GEM_BUG_ON(!req->reserved_space);
  1348. list_for_each_entry(target, &ring->request_list, ring_link) {
  1349. unsigned space;
  1350. /* Would completion of this request free enough space? */
  1351. space = __intel_ring_space(target->postfix, ring->tail,
  1352. ring->size);
  1353. if (space >= bytes)
  1354. break;
  1355. }
  1356. if (WARN_ON(&target->ring_link == &ring->request_list))
  1357. return -ENOSPC;
  1358. timeout = i915_wait_request(target,
  1359. I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
  1360. MAX_SCHEDULE_TIMEOUT);
  1361. if (timeout < 0)
  1362. return timeout;
  1363. i915_gem_request_retire_upto(target);
  1364. intel_ring_update_space(ring);
  1365. GEM_BUG_ON(ring->space < bytes);
  1366. return 0;
  1367. }
  1368. u32 *intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
  1369. {
  1370. struct intel_ring *ring = req->ring;
  1371. int remain_actual = ring->size - ring->tail;
  1372. int remain_usable = ring->effective_size - ring->tail;
  1373. int bytes = num_dwords * sizeof(u32);
  1374. int total_bytes, wait_bytes;
  1375. bool need_wrap = false;
  1376. u32 *cs;
  1377. total_bytes = bytes + req->reserved_space;
  1378. if (unlikely(bytes > remain_usable)) {
  1379. /*
  1380. * Not enough space for the basic request. So need to flush
  1381. * out the remainder and then wait for base + reserved.
  1382. */
  1383. wait_bytes = remain_actual + total_bytes;
  1384. need_wrap = true;
  1385. } else if (unlikely(total_bytes > remain_usable)) {
  1386. /*
  1387. * The base request will fit but the reserved space
  1388. * falls off the end. So we don't need an immediate wrap
  1389. * and only need to effectively wait for the reserved
  1390. * size space from the start of ringbuffer.
  1391. */
  1392. wait_bytes = remain_actual + req->reserved_space;
  1393. } else {
  1394. /* No wrapping required, just waiting. */
  1395. wait_bytes = total_bytes;
  1396. }
  1397. if (wait_bytes > ring->space) {
  1398. int ret = wait_for_space(req, wait_bytes);
  1399. if (unlikely(ret))
  1400. return ERR_PTR(ret);
  1401. }
  1402. if (unlikely(need_wrap)) {
  1403. GEM_BUG_ON(remain_actual > ring->space);
  1404. GEM_BUG_ON(ring->tail + remain_actual > ring->size);
  1405. /* Fill the tail with MI_NOOP */
  1406. memset(ring->vaddr + ring->tail, 0, remain_actual);
  1407. ring->tail = 0;
  1408. ring->space -= remain_actual;
  1409. }
  1410. GEM_BUG_ON(ring->tail > ring->size - bytes);
  1411. cs = ring->vaddr + ring->tail;
  1412. ring->tail += bytes;
  1413. ring->space -= bytes;
  1414. GEM_BUG_ON(ring->space < 0);
  1415. return cs;
  1416. }
  1417. /* Align the ring tail to a cacheline boundary */
  1418. int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
  1419. {
  1420. int num_dwords =
  1421. (req->ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
  1422. u32 *cs;
  1423. if (num_dwords == 0)
  1424. return 0;
  1425. num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
  1426. cs = intel_ring_begin(req, num_dwords);
  1427. if (IS_ERR(cs))
  1428. return PTR_ERR(cs);
  1429. while (num_dwords--)
  1430. *cs++ = MI_NOOP;
  1431. intel_ring_advance(req, cs);
  1432. return 0;
  1433. }
  1434. static void gen6_bsd_submit_request(struct drm_i915_gem_request *request)
  1435. {
  1436. struct drm_i915_private *dev_priv = request->i915;
  1437. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  1438. /* Every tail move must follow the sequence below */
  1439. /* Disable notification that the ring is IDLE. The GT
  1440. * will then assume that it is busy and bring it out of rc6.
  1441. */
  1442. I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1443. _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  1444. /* Clear the context id. Here be magic! */
  1445. I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
  1446. /* Wait for the ring not to be idle, i.e. for it to wake up. */
  1447. if (intel_wait_for_register_fw(dev_priv,
  1448. GEN6_BSD_SLEEP_PSMI_CONTROL,
  1449. GEN6_BSD_SLEEP_INDICATOR,
  1450. 0,
  1451. 50))
  1452. DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
  1453. /* Now that the ring is fully powered up, update the tail */
  1454. i9xx_submit_request(request);
  1455. /* Let the ring send IDLE messages to the GT again,
  1456. * and so let it sleep to conserve power when idle.
  1457. */
  1458. I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1459. _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  1460. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  1461. }
  1462. static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req, u32 mode)
  1463. {
  1464. u32 cmd, *cs;
  1465. cs = intel_ring_begin(req, 4);
  1466. if (IS_ERR(cs))
  1467. return PTR_ERR(cs);
  1468. cmd = MI_FLUSH_DW;
  1469. if (INTEL_GEN(req->i915) >= 8)
  1470. cmd += 1;
  1471. /* We always require a command barrier so that subsequent
  1472. * commands, such as breadcrumb interrupts, are strictly ordered
  1473. * wrt the contents of the write cache being flushed to memory
  1474. * (and thus being coherent from the CPU).
  1475. */
  1476. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1477. /*
  1478. * Bspec vol 1c.5 - video engine command streamer:
  1479. * "If ENABLED, all TLBs will be invalidated once the flush
  1480. * operation is complete. This bit is only valid when the
  1481. * Post-Sync Operation field is a value of 1h or 3h."
  1482. */
  1483. if (mode & EMIT_INVALIDATE)
  1484. cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
  1485. *cs++ = cmd;
  1486. *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
  1487. if (INTEL_GEN(req->i915) >= 8) {
  1488. *cs++ = 0; /* upper addr */
  1489. *cs++ = 0; /* value */
  1490. } else {
  1491. *cs++ = 0;
  1492. *cs++ = MI_NOOP;
  1493. }
  1494. intel_ring_advance(req, cs);
  1495. return 0;
  1496. }
  1497. static int
  1498. gen8_emit_bb_start(struct drm_i915_gem_request *req,
  1499. u64 offset, u32 len,
  1500. unsigned int dispatch_flags)
  1501. {
  1502. bool ppgtt = USES_PPGTT(req->i915) &&
  1503. !(dispatch_flags & I915_DISPATCH_SECURE);
  1504. u32 *cs;
  1505. cs = intel_ring_begin(req, 4);
  1506. if (IS_ERR(cs))
  1507. return PTR_ERR(cs);
  1508. /* FIXME(BDW): Address space and security selectors. */
  1509. *cs++ = MI_BATCH_BUFFER_START_GEN8 | (ppgtt << 8) | (dispatch_flags &
  1510. I915_DISPATCH_RS ? MI_BATCH_RESOURCE_STREAMER : 0);
  1511. *cs++ = lower_32_bits(offset);
  1512. *cs++ = upper_32_bits(offset);
  1513. *cs++ = MI_NOOP;
  1514. intel_ring_advance(req, cs);
  1515. return 0;
  1516. }
  1517. static int
  1518. hsw_emit_bb_start(struct drm_i915_gem_request *req,
  1519. u64 offset, u32 len,
  1520. unsigned int dispatch_flags)
  1521. {
  1522. u32 *cs;
  1523. cs = intel_ring_begin(req, 2);
  1524. if (IS_ERR(cs))
  1525. return PTR_ERR(cs);
  1526. *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
  1527. 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
  1528. (dispatch_flags & I915_DISPATCH_RS ?
  1529. MI_BATCH_RESOURCE_STREAMER : 0);
  1530. /* bit0-7 is the length on GEN6+ */
  1531. *cs++ = offset;
  1532. intel_ring_advance(req, cs);
  1533. return 0;
  1534. }
  1535. static int
  1536. gen6_emit_bb_start(struct drm_i915_gem_request *req,
  1537. u64 offset, u32 len,
  1538. unsigned int dispatch_flags)
  1539. {
  1540. u32 *cs;
  1541. cs = intel_ring_begin(req, 2);
  1542. if (IS_ERR(cs))
  1543. return PTR_ERR(cs);
  1544. *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
  1545. 0 : MI_BATCH_NON_SECURE_I965);
  1546. /* bit0-7 is the length on GEN6+ */
  1547. *cs++ = offset;
  1548. intel_ring_advance(req, cs);
  1549. return 0;
  1550. }
  1551. /* Blitter support (SandyBridge+) */
  1552. static int gen6_ring_flush(struct drm_i915_gem_request *req, u32 mode)
  1553. {
  1554. u32 cmd, *cs;
  1555. cs = intel_ring_begin(req, 4);
  1556. if (IS_ERR(cs))
  1557. return PTR_ERR(cs);
  1558. cmd = MI_FLUSH_DW;
  1559. if (INTEL_GEN(req->i915) >= 8)
  1560. cmd += 1;
  1561. /* We always require a command barrier so that subsequent
  1562. * commands, such as breadcrumb interrupts, are strictly ordered
  1563. * wrt the contents of the write cache being flushed to memory
  1564. * (and thus being coherent from the CPU).
  1565. */
  1566. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1567. /*
  1568. * Bspec vol 1c.3 - blitter engine command streamer:
  1569. * "If ENABLED, all TLBs will be invalidated once the flush
  1570. * operation is complete. This bit is only valid when the
  1571. * Post-Sync Operation field is a value of 1h or 3h."
  1572. */
  1573. if (mode & EMIT_INVALIDATE)
  1574. cmd |= MI_INVALIDATE_TLB;
  1575. *cs++ = cmd;
  1576. *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
  1577. if (INTEL_GEN(req->i915) >= 8) {
  1578. *cs++ = 0; /* upper addr */
  1579. *cs++ = 0; /* value */
  1580. } else {
  1581. *cs++ = 0;
  1582. *cs++ = MI_NOOP;
  1583. }
  1584. intel_ring_advance(req, cs);
  1585. return 0;
  1586. }
  1587. static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
  1588. struct intel_engine_cs *engine)
  1589. {
  1590. struct drm_i915_gem_object *obj;
  1591. int ret, i;
  1592. if (!i915.semaphores)
  1593. return;
  1594. if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore) {
  1595. struct i915_vma *vma;
  1596. obj = i915_gem_object_create(dev_priv, PAGE_SIZE);
  1597. if (IS_ERR(obj))
  1598. goto err;
  1599. vma = i915_vma_instance(obj, &dev_priv->ggtt.base, NULL);
  1600. if (IS_ERR(vma))
  1601. goto err_obj;
  1602. ret = i915_gem_object_set_to_gtt_domain(obj, false);
  1603. if (ret)
  1604. goto err_obj;
  1605. ret = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
  1606. if (ret)
  1607. goto err_obj;
  1608. dev_priv->semaphore = vma;
  1609. }
  1610. if (INTEL_GEN(dev_priv) >= 8) {
  1611. u32 offset = i915_ggtt_offset(dev_priv->semaphore);
  1612. engine->semaphore.sync_to = gen8_ring_sync_to;
  1613. engine->semaphore.signal = gen8_xcs_signal;
  1614. for (i = 0; i < I915_NUM_ENGINES; i++) {
  1615. u32 ring_offset;
  1616. if (i != engine->id)
  1617. ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
  1618. else
  1619. ring_offset = MI_SEMAPHORE_SYNC_INVALID;
  1620. engine->semaphore.signal_ggtt[i] = ring_offset;
  1621. }
  1622. } else if (INTEL_GEN(dev_priv) >= 6) {
  1623. engine->semaphore.sync_to = gen6_ring_sync_to;
  1624. engine->semaphore.signal = gen6_signal;
  1625. /*
  1626. * The current semaphore is only applied on pre-gen8
  1627. * platform. And there is no VCS2 ring on the pre-gen8
  1628. * platform. So the semaphore between RCS and VCS2 is
  1629. * initialized as INVALID. Gen8 will initialize the
  1630. * sema between VCS2 and RCS later.
  1631. */
  1632. for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
  1633. static const struct {
  1634. u32 wait_mbox;
  1635. i915_reg_t mbox_reg;
  1636. } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
  1637. [RCS_HW] = {
  1638. [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
  1639. [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
  1640. [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
  1641. },
  1642. [VCS_HW] = {
  1643. [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
  1644. [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
  1645. [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
  1646. },
  1647. [BCS_HW] = {
  1648. [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
  1649. [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
  1650. [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
  1651. },
  1652. [VECS_HW] = {
  1653. [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
  1654. [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
  1655. [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
  1656. },
  1657. };
  1658. u32 wait_mbox;
  1659. i915_reg_t mbox_reg;
  1660. if (i == engine->hw_id) {
  1661. wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
  1662. mbox_reg = GEN6_NOSYNC;
  1663. } else {
  1664. wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
  1665. mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
  1666. }
  1667. engine->semaphore.mbox.wait[i] = wait_mbox;
  1668. engine->semaphore.mbox.signal[i] = mbox_reg;
  1669. }
  1670. }
  1671. return;
  1672. err_obj:
  1673. i915_gem_object_put(obj);
  1674. err:
  1675. DRM_DEBUG_DRIVER("Failed to allocate space for semaphores, disabling\n");
  1676. i915.semaphores = 0;
  1677. }
  1678. static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
  1679. struct intel_engine_cs *engine)
  1680. {
  1681. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT << engine->irq_shift;
  1682. if (INTEL_GEN(dev_priv) >= 8) {
  1683. engine->irq_enable = gen8_irq_enable;
  1684. engine->irq_disable = gen8_irq_disable;
  1685. engine->irq_seqno_barrier = gen6_seqno_barrier;
  1686. } else if (INTEL_GEN(dev_priv) >= 6) {
  1687. engine->irq_enable = gen6_irq_enable;
  1688. engine->irq_disable = gen6_irq_disable;
  1689. engine->irq_seqno_barrier = gen6_seqno_barrier;
  1690. } else if (INTEL_GEN(dev_priv) >= 5) {
  1691. engine->irq_enable = gen5_irq_enable;
  1692. engine->irq_disable = gen5_irq_disable;
  1693. engine->irq_seqno_barrier = gen5_seqno_barrier;
  1694. } else if (INTEL_GEN(dev_priv) >= 3) {
  1695. engine->irq_enable = i9xx_irq_enable;
  1696. engine->irq_disable = i9xx_irq_disable;
  1697. } else {
  1698. engine->irq_enable = i8xx_irq_enable;
  1699. engine->irq_disable = i8xx_irq_disable;
  1700. }
  1701. }
  1702. static void i9xx_set_default_submission(struct intel_engine_cs *engine)
  1703. {
  1704. engine->submit_request = i9xx_submit_request;
  1705. }
  1706. static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
  1707. {
  1708. engine->submit_request = gen6_bsd_submit_request;
  1709. }
  1710. static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
  1711. struct intel_engine_cs *engine)
  1712. {
  1713. intel_ring_init_irq(dev_priv, engine);
  1714. intel_ring_init_semaphores(dev_priv, engine);
  1715. engine->init_hw = init_ring_common;
  1716. engine->reset_hw = reset_ring_common;
  1717. engine->context_pin = intel_ring_context_pin;
  1718. engine->context_unpin = intel_ring_context_unpin;
  1719. engine->request_alloc = ring_request_alloc;
  1720. engine->emit_breadcrumb = i9xx_emit_breadcrumb;
  1721. engine->emit_breadcrumb_sz = i9xx_emit_breadcrumb_sz;
  1722. if (i915.semaphores) {
  1723. int num_rings;
  1724. engine->emit_breadcrumb = gen6_sema_emit_breadcrumb;
  1725. num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1;
  1726. if (INTEL_GEN(dev_priv) >= 8) {
  1727. engine->emit_breadcrumb_sz += num_rings * 6;
  1728. } else {
  1729. engine->emit_breadcrumb_sz += num_rings * 3;
  1730. if (num_rings & 1)
  1731. engine->emit_breadcrumb_sz++;
  1732. }
  1733. }
  1734. engine->set_default_submission = i9xx_set_default_submission;
  1735. if (INTEL_GEN(dev_priv) >= 8)
  1736. engine->emit_bb_start = gen8_emit_bb_start;
  1737. else if (INTEL_GEN(dev_priv) >= 6)
  1738. engine->emit_bb_start = gen6_emit_bb_start;
  1739. else if (INTEL_GEN(dev_priv) >= 4)
  1740. engine->emit_bb_start = i965_emit_bb_start;
  1741. else if (IS_I830(dev_priv) || IS_I845G(dev_priv))
  1742. engine->emit_bb_start = i830_emit_bb_start;
  1743. else
  1744. engine->emit_bb_start = i915_emit_bb_start;
  1745. }
  1746. int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
  1747. {
  1748. struct drm_i915_private *dev_priv = engine->i915;
  1749. int ret;
  1750. intel_ring_default_vfuncs(dev_priv, engine);
  1751. if (HAS_L3_DPF(dev_priv))
  1752. engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1753. if (INTEL_GEN(dev_priv) >= 8) {
  1754. engine->init_context = intel_rcs_ctx_init;
  1755. engine->emit_breadcrumb = gen8_render_emit_breadcrumb;
  1756. engine->emit_breadcrumb_sz = gen8_render_emit_breadcrumb_sz;
  1757. engine->emit_flush = gen8_render_ring_flush;
  1758. if (i915.semaphores) {
  1759. int num_rings;
  1760. engine->semaphore.signal = gen8_rcs_signal;
  1761. num_rings =
  1762. hweight32(INTEL_INFO(dev_priv)->ring_mask) - 1;
  1763. engine->emit_breadcrumb_sz += num_rings * 6;
  1764. }
  1765. } else if (INTEL_GEN(dev_priv) >= 6) {
  1766. engine->init_context = intel_rcs_ctx_init;
  1767. engine->emit_flush = gen7_render_ring_flush;
  1768. if (IS_GEN6(dev_priv))
  1769. engine->emit_flush = gen6_render_ring_flush;
  1770. } else if (IS_GEN5(dev_priv)) {
  1771. engine->emit_flush = gen4_render_ring_flush;
  1772. } else {
  1773. if (INTEL_GEN(dev_priv) < 4)
  1774. engine->emit_flush = gen2_render_ring_flush;
  1775. else
  1776. engine->emit_flush = gen4_render_ring_flush;
  1777. engine->irq_enable_mask = I915_USER_INTERRUPT;
  1778. }
  1779. if (IS_HASWELL(dev_priv))
  1780. engine->emit_bb_start = hsw_emit_bb_start;
  1781. engine->init_hw = init_render_ring;
  1782. engine->cleanup = render_ring_cleanup;
  1783. ret = intel_init_ring_buffer(engine);
  1784. if (ret)
  1785. return ret;
  1786. if (INTEL_GEN(dev_priv) >= 6) {
  1787. ret = intel_engine_create_scratch(engine, PAGE_SIZE);
  1788. if (ret)
  1789. return ret;
  1790. } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
  1791. ret = intel_engine_create_scratch(engine, I830_WA_SIZE);
  1792. if (ret)
  1793. return ret;
  1794. }
  1795. return 0;
  1796. }
  1797. int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
  1798. {
  1799. struct drm_i915_private *dev_priv = engine->i915;
  1800. intel_ring_default_vfuncs(dev_priv, engine);
  1801. if (INTEL_GEN(dev_priv) >= 6) {
  1802. /* gen6 bsd needs a special wa for tail updates */
  1803. if (IS_GEN6(dev_priv))
  1804. engine->set_default_submission = gen6_bsd_set_default_submission;
  1805. engine->emit_flush = gen6_bsd_ring_flush;
  1806. if (INTEL_GEN(dev_priv) < 8)
  1807. engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
  1808. } else {
  1809. engine->mmio_base = BSD_RING_BASE;
  1810. engine->emit_flush = bsd_ring_flush;
  1811. if (IS_GEN5(dev_priv))
  1812. engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
  1813. else
  1814. engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
  1815. }
  1816. return intel_init_ring_buffer(engine);
  1817. }
  1818. /**
  1819. * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
  1820. */
  1821. int intel_init_bsd2_ring_buffer(struct intel_engine_cs *engine)
  1822. {
  1823. struct drm_i915_private *dev_priv = engine->i915;
  1824. intel_ring_default_vfuncs(dev_priv, engine);
  1825. engine->emit_flush = gen6_bsd_ring_flush;
  1826. return intel_init_ring_buffer(engine);
  1827. }
  1828. int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
  1829. {
  1830. struct drm_i915_private *dev_priv = engine->i915;
  1831. intel_ring_default_vfuncs(dev_priv, engine);
  1832. engine->emit_flush = gen6_ring_flush;
  1833. if (INTEL_GEN(dev_priv) < 8)
  1834. engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
  1835. return intel_init_ring_buffer(engine);
  1836. }
  1837. int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
  1838. {
  1839. struct drm_i915_private *dev_priv = engine->i915;
  1840. intel_ring_default_vfuncs(dev_priv, engine);
  1841. engine->emit_flush = gen6_ring_flush;
  1842. if (INTEL_GEN(dev_priv) < 8) {
  1843. engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
  1844. engine->irq_enable = hsw_vebox_irq_enable;
  1845. engine->irq_disable = hsw_vebox_irq_disable;
  1846. }
  1847. return intel_init_ring_buffer(engine);
  1848. }