intel_ringbuffer.c 56 KB

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