intel_ringbuffer.c 59 KB

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