intel_ringbuffer.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  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. /* Check that the ring offsets point within the ring! */
  431. GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->head));
  432. GEM_BUG_ON(!intel_ring_offset_valid(ring, ring->tail));
  433. intel_ring_update_space(ring);
  434. I915_WRITE_HEAD(engine, ring->head);
  435. I915_WRITE_TAIL(engine, ring->tail);
  436. (void)I915_READ_TAIL(engine);
  437. I915_WRITE_CTL(engine, RING_CTL_SIZE(ring->size) | RING_VALID);
  438. /* If the head is still not zero, the ring is dead */
  439. if (intel_wait_for_register(dev_priv, RING_CTL(engine->mmio_base),
  440. RING_VALID, RING_VALID,
  441. 50)) {
  442. DRM_ERROR("%s initialization failed "
  443. "ctl %08x (valid? %d) head %08x [%08x] tail %08x [%08x] start %08x [expected %08x]\n",
  444. engine->name,
  445. I915_READ_CTL(engine),
  446. I915_READ_CTL(engine) & RING_VALID,
  447. I915_READ_HEAD(engine), ring->head,
  448. I915_READ_TAIL(engine), ring->tail,
  449. I915_READ_START(engine),
  450. i915_ggtt_offset(ring->vma));
  451. ret = -EIO;
  452. goto out;
  453. }
  454. if (INTEL_GEN(dev_priv) > 2)
  455. I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
  456. /* Papering over lost _interrupts_ immediately following the restart */
  457. intel_engine_wakeup(engine);
  458. out:
  459. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  460. return ret;
  461. }
  462. static struct i915_request *reset_prepare(struct intel_engine_cs *engine)
  463. {
  464. intel_engine_stop_cs(engine);
  465. if (engine->irq_seqno_barrier)
  466. engine->irq_seqno_barrier(engine);
  467. return i915_gem_find_active_request(engine);
  468. }
  469. static void skip_request(struct i915_request *rq)
  470. {
  471. void *vaddr = rq->ring->vaddr;
  472. u32 head;
  473. head = rq->infix;
  474. if (rq->postfix < head) {
  475. memset32(vaddr + head, MI_NOOP,
  476. (rq->ring->size - head) / sizeof(u32));
  477. head = 0;
  478. }
  479. memset32(vaddr + head, MI_NOOP, (rq->postfix - head) / sizeof(u32));
  480. }
  481. static void reset_ring(struct intel_engine_cs *engine, struct i915_request *rq)
  482. {
  483. GEM_TRACE("%s seqno=%x\n", engine->name, rq ? rq->global_seqno : 0);
  484. /*
  485. * Try to restore the logical GPU state to match the continuation
  486. * of the request queue. If we skip the context/PD restore, then
  487. * the next request may try to execute assuming that its context
  488. * is valid and loaded on the GPU and so may try to access invalid
  489. * memory, prompting repeated GPU hangs.
  490. *
  491. * If the request was guilty, we still restore the logical state
  492. * in case the next request requires it (e.g. the aliasing ppgtt),
  493. * but skip over the hung batch.
  494. *
  495. * If the request was innocent, we try to replay the request with
  496. * the restored context.
  497. */
  498. if (rq) {
  499. /* If the rq hung, jump to its breadcrumb and skip the batch */
  500. rq->ring->head = intel_ring_wrap(rq->ring, rq->head);
  501. if (rq->fence.error == -EIO)
  502. skip_request(rq);
  503. }
  504. }
  505. static void reset_finish(struct intel_engine_cs *engine)
  506. {
  507. }
  508. static int intel_rcs_ctx_init(struct i915_request *rq)
  509. {
  510. int ret;
  511. ret = intel_ctx_workarounds_emit(rq);
  512. if (ret != 0)
  513. return ret;
  514. ret = i915_gem_render_state_emit(rq);
  515. if (ret)
  516. return ret;
  517. return 0;
  518. }
  519. static int init_render_ring(struct intel_engine_cs *engine)
  520. {
  521. struct drm_i915_private *dev_priv = engine->i915;
  522. int ret = init_ring_common(engine);
  523. if (ret)
  524. return ret;
  525. intel_whitelist_workarounds_apply(engine);
  526. /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
  527. if (IS_GEN(dev_priv, 4, 6))
  528. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
  529. /* We need to disable the AsyncFlip performance optimisations in order
  530. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  531. * programmed to '1' on all products.
  532. *
  533. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
  534. */
  535. if (IS_GEN(dev_priv, 6, 7))
  536. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  537. /* Required for the hardware to program scanline values for waiting */
  538. /* WaEnableFlushTlbInvalidationMode:snb */
  539. if (IS_GEN6(dev_priv))
  540. I915_WRITE(GFX_MODE,
  541. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
  542. /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
  543. if (IS_GEN7(dev_priv))
  544. I915_WRITE(GFX_MODE_GEN7,
  545. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
  546. _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
  547. if (IS_GEN6(dev_priv)) {
  548. /* From the Sandybridge PRM, volume 1 part 3, page 24:
  549. * "If this bit is set, STCunit will have LRA as replacement
  550. * policy. [...] This bit must be reset. LRA replacement
  551. * policy is not supported."
  552. */
  553. I915_WRITE(CACHE_MODE_0,
  554. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  555. }
  556. if (IS_GEN(dev_priv, 6, 7))
  557. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  558. if (INTEL_GEN(dev_priv) >= 6)
  559. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  560. return 0;
  561. }
  562. static u32 *gen6_signal(struct i915_request *rq, u32 *cs)
  563. {
  564. struct drm_i915_private *dev_priv = rq->i915;
  565. struct intel_engine_cs *engine;
  566. enum intel_engine_id id;
  567. int num_rings = 0;
  568. for_each_engine(engine, dev_priv, id) {
  569. i915_reg_t mbox_reg;
  570. if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
  571. continue;
  572. mbox_reg = rq->engine->semaphore.mbox.signal[engine->hw_id];
  573. if (i915_mmio_reg_valid(mbox_reg)) {
  574. *cs++ = MI_LOAD_REGISTER_IMM(1);
  575. *cs++ = i915_mmio_reg_offset(mbox_reg);
  576. *cs++ = rq->global_seqno;
  577. num_rings++;
  578. }
  579. }
  580. if (num_rings & 1)
  581. *cs++ = MI_NOOP;
  582. return cs;
  583. }
  584. static void cancel_requests(struct intel_engine_cs *engine)
  585. {
  586. struct i915_request *request;
  587. unsigned long flags;
  588. spin_lock_irqsave(&engine->timeline.lock, flags);
  589. /* Mark all submitted requests as skipped. */
  590. list_for_each_entry(request, &engine->timeline.requests, link) {
  591. GEM_BUG_ON(!request->global_seqno);
  592. if (!i915_request_completed(request))
  593. dma_fence_set_error(&request->fence, -EIO);
  594. }
  595. /* Remaining _unready_ requests will be nop'ed when submitted */
  596. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  597. }
  598. static void i9xx_submit_request(struct i915_request *request)
  599. {
  600. struct drm_i915_private *dev_priv = request->i915;
  601. i915_request_submit(request);
  602. I915_WRITE_TAIL(request->engine,
  603. intel_ring_set_tail(request->ring, request->tail));
  604. }
  605. static void i9xx_emit_breadcrumb(struct i915_request *rq, u32 *cs)
  606. {
  607. *cs++ = MI_STORE_DWORD_INDEX;
  608. *cs++ = I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT;
  609. *cs++ = rq->global_seqno;
  610. *cs++ = MI_USER_INTERRUPT;
  611. rq->tail = intel_ring_offset(rq, cs);
  612. assert_ring_tail_valid(rq->ring, rq->tail);
  613. }
  614. static const int i9xx_emit_breadcrumb_sz = 4;
  615. static void gen6_sema_emit_breadcrumb(struct i915_request *rq, u32 *cs)
  616. {
  617. return i9xx_emit_breadcrumb(rq, rq->engine->semaphore.signal(rq, cs));
  618. }
  619. static int
  620. gen6_ring_sync_to(struct i915_request *rq, struct i915_request *signal)
  621. {
  622. u32 dw1 = MI_SEMAPHORE_MBOX |
  623. MI_SEMAPHORE_COMPARE |
  624. MI_SEMAPHORE_REGISTER;
  625. u32 wait_mbox = signal->engine->semaphore.mbox.wait[rq->engine->hw_id];
  626. u32 *cs;
  627. WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
  628. cs = intel_ring_begin(rq, 4);
  629. if (IS_ERR(cs))
  630. return PTR_ERR(cs);
  631. *cs++ = dw1 | wait_mbox;
  632. /* Throughout all of the GEM code, seqno passed implies our current
  633. * seqno is >= the last seqno executed. However for hardware the
  634. * comparison is strictly greater than.
  635. */
  636. *cs++ = signal->global_seqno - 1;
  637. *cs++ = 0;
  638. *cs++ = MI_NOOP;
  639. intel_ring_advance(rq, cs);
  640. return 0;
  641. }
  642. static void
  643. gen5_seqno_barrier(struct intel_engine_cs *engine)
  644. {
  645. /* MI_STORE are internally buffered by the GPU and not flushed
  646. * either by MI_FLUSH or SyncFlush or any other combination of
  647. * MI commands.
  648. *
  649. * "Only the submission of the store operation is guaranteed.
  650. * The write result will be complete (coherent) some time later
  651. * (this is practically a finite period but there is no guaranteed
  652. * latency)."
  653. *
  654. * Empirically, we observe that we need a delay of at least 75us to
  655. * be sure that the seqno write is visible by the CPU.
  656. */
  657. usleep_range(125, 250);
  658. }
  659. static void
  660. gen6_seqno_barrier(struct intel_engine_cs *engine)
  661. {
  662. struct drm_i915_private *dev_priv = engine->i915;
  663. /* Workaround to force correct ordering between irq and seqno writes on
  664. * ivb (and maybe also on snb) by reading from a CS register (like
  665. * ACTHD) before reading the status page.
  666. *
  667. * Note that this effectively stalls the read by the time it takes to
  668. * do a memory transaction, which more or less ensures that the write
  669. * from the GPU has sufficient time to invalidate the CPU cacheline.
  670. * Alternatively we could delay the interrupt from the CS ring to give
  671. * the write time to land, but that would incur a delay after every
  672. * batch i.e. much more frequent than a delay when waiting for the
  673. * interrupt (with the same net latency).
  674. *
  675. * Also note that to prevent whole machine hangs on gen7, we have to
  676. * take the spinlock to guard against concurrent cacheline access.
  677. */
  678. spin_lock_irq(&dev_priv->uncore.lock);
  679. POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
  680. spin_unlock_irq(&dev_priv->uncore.lock);
  681. }
  682. static void
  683. gen5_irq_enable(struct intel_engine_cs *engine)
  684. {
  685. gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
  686. }
  687. static void
  688. gen5_irq_disable(struct intel_engine_cs *engine)
  689. {
  690. gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
  691. }
  692. static void
  693. i9xx_irq_enable(struct intel_engine_cs *engine)
  694. {
  695. struct drm_i915_private *dev_priv = engine->i915;
  696. dev_priv->irq_mask &= ~engine->irq_enable_mask;
  697. I915_WRITE(IMR, dev_priv->irq_mask);
  698. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  699. }
  700. static void
  701. i9xx_irq_disable(struct intel_engine_cs *engine)
  702. {
  703. struct drm_i915_private *dev_priv = engine->i915;
  704. dev_priv->irq_mask |= engine->irq_enable_mask;
  705. I915_WRITE(IMR, dev_priv->irq_mask);
  706. }
  707. static void
  708. i8xx_irq_enable(struct intel_engine_cs *engine)
  709. {
  710. struct drm_i915_private *dev_priv = engine->i915;
  711. dev_priv->irq_mask &= ~engine->irq_enable_mask;
  712. I915_WRITE16(IMR, dev_priv->irq_mask);
  713. POSTING_READ16(RING_IMR(engine->mmio_base));
  714. }
  715. static void
  716. i8xx_irq_disable(struct intel_engine_cs *engine)
  717. {
  718. struct drm_i915_private *dev_priv = engine->i915;
  719. dev_priv->irq_mask |= engine->irq_enable_mask;
  720. I915_WRITE16(IMR, dev_priv->irq_mask);
  721. }
  722. static int
  723. bsd_ring_flush(struct i915_request *rq, u32 mode)
  724. {
  725. u32 *cs;
  726. cs = intel_ring_begin(rq, 2);
  727. if (IS_ERR(cs))
  728. return PTR_ERR(cs);
  729. *cs++ = MI_FLUSH;
  730. *cs++ = MI_NOOP;
  731. intel_ring_advance(rq, cs);
  732. return 0;
  733. }
  734. static void
  735. gen6_irq_enable(struct intel_engine_cs *engine)
  736. {
  737. struct drm_i915_private *dev_priv = engine->i915;
  738. I915_WRITE_IMR(engine,
  739. ~(engine->irq_enable_mask |
  740. engine->irq_keep_mask));
  741. gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
  742. }
  743. static void
  744. gen6_irq_disable(struct intel_engine_cs *engine)
  745. {
  746. struct drm_i915_private *dev_priv = engine->i915;
  747. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  748. gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
  749. }
  750. static void
  751. hsw_vebox_irq_enable(struct intel_engine_cs *engine)
  752. {
  753. struct drm_i915_private *dev_priv = engine->i915;
  754. I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
  755. gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
  756. }
  757. static void
  758. hsw_vebox_irq_disable(struct intel_engine_cs *engine)
  759. {
  760. struct drm_i915_private *dev_priv = engine->i915;
  761. I915_WRITE_IMR(engine, ~0);
  762. gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
  763. }
  764. static int
  765. i965_emit_bb_start(struct i915_request *rq,
  766. u64 offset, u32 length,
  767. unsigned int dispatch_flags)
  768. {
  769. u32 *cs;
  770. cs = intel_ring_begin(rq, 2);
  771. if (IS_ERR(cs))
  772. return PTR_ERR(cs);
  773. *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags &
  774. I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965);
  775. *cs++ = offset;
  776. intel_ring_advance(rq, cs);
  777. return 0;
  778. }
  779. /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
  780. #define I830_BATCH_LIMIT (256*1024)
  781. #define I830_TLB_ENTRIES (2)
  782. #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
  783. static int
  784. i830_emit_bb_start(struct i915_request *rq,
  785. u64 offset, u32 len,
  786. unsigned int dispatch_flags)
  787. {
  788. u32 *cs, cs_offset = i915_ggtt_offset(rq->engine->scratch);
  789. cs = intel_ring_begin(rq, 6);
  790. if (IS_ERR(cs))
  791. return PTR_ERR(cs);
  792. /* Evict the invalid PTE TLBs */
  793. *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA;
  794. *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096;
  795. *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */
  796. *cs++ = cs_offset;
  797. *cs++ = 0xdeadbeef;
  798. *cs++ = MI_NOOP;
  799. intel_ring_advance(rq, cs);
  800. if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
  801. if (len > I830_BATCH_LIMIT)
  802. return -ENOSPC;
  803. cs = intel_ring_begin(rq, 6 + 2);
  804. if (IS_ERR(cs))
  805. return PTR_ERR(cs);
  806. /* Blit the batch (which has now all relocs applied) to the
  807. * stable batch scratch bo area (so that the CS never
  808. * stumbles over its tlb invalidation bug) ...
  809. */
  810. *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA;
  811. *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096;
  812. *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096;
  813. *cs++ = cs_offset;
  814. *cs++ = 4096;
  815. *cs++ = offset;
  816. *cs++ = MI_FLUSH;
  817. *cs++ = MI_NOOP;
  818. intel_ring_advance(rq, cs);
  819. /* ... and execute it. */
  820. offset = cs_offset;
  821. }
  822. cs = intel_ring_begin(rq, 2);
  823. if (IS_ERR(cs))
  824. return PTR_ERR(cs);
  825. *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
  826. *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
  827. MI_BATCH_NON_SECURE);
  828. intel_ring_advance(rq, cs);
  829. return 0;
  830. }
  831. static int
  832. i915_emit_bb_start(struct i915_request *rq,
  833. u64 offset, u32 len,
  834. unsigned int dispatch_flags)
  835. {
  836. u32 *cs;
  837. cs = intel_ring_begin(rq, 2);
  838. if (IS_ERR(cs))
  839. return PTR_ERR(cs);
  840. *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
  841. *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
  842. MI_BATCH_NON_SECURE);
  843. intel_ring_advance(rq, cs);
  844. return 0;
  845. }
  846. int intel_ring_pin(struct intel_ring *ring)
  847. {
  848. struct i915_vma *vma = ring->vma;
  849. enum i915_map_type map =
  850. HAS_LLC(vma->vm->i915) ? I915_MAP_WB : I915_MAP_WC;
  851. unsigned int flags;
  852. void *addr;
  853. int ret;
  854. GEM_BUG_ON(ring->vaddr);
  855. flags = PIN_GLOBAL;
  856. /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
  857. flags |= PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
  858. if (vma->obj->stolen)
  859. flags |= PIN_MAPPABLE;
  860. else
  861. flags |= PIN_HIGH;
  862. if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
  863. if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
  864. ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
  865. else
  866. ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
  867. if (unlikely(ret))
  868. return ret;
  869. }
  870. ret = i915_vma_pin(vma, 0, 0, flags);
  871. if (unlikely(ret))
  872. return ret;
  873. if (i915_vma_is_map_and_fenceable(vma))
  874. addr = (void __force *)i915_vma_pin_iomap(vma);
  875. else
  876. addr = i915_gem_object_pin_map(vma->obj, map);
  877. if (IS_ERR(addr))
  878. goto err;
  879. vma->obj->pin_global++;
  880. ring->vaddr = addr;
  881. return 0;
  882. err:
  883. i915_vma_unpin(vma);
  884. return PTR_ERR(addr);
  885. }
  886. void intel_ring_reset(struct intel_ring *ring, u32 tail)
  887. {
  888. GEM_BUG_ON(!intel_ring_offset_valid(ring, tail));
  889. ring->tail = tail;
  890. ring->head = tail;
  891. ring->emit = tail;
  892. intel_ring_update_space(ring);
  893. }
  894. void intel_ring_unpin(struct intel_ring *ring)
  895. {
  896. GEM_BUG_ON(!ring->vma);
  897. GEM_BUG_ON(!ring->vaddr);
  898. /* Discard any unused bytes beyond that submitted to hw. */
  899. intel_ring_reset(ring, ring->tail);
  900. if (i915_vma_is_map_and_fenceable(ring->vma))
  901. i915_vma_unpin_iomap(ring->vma);
  902. else
  903. i915_gem_object_unpin_map(ring->vma->obj);
  904. ring->vaddr = NULL;
  905. ring->vma->obj->pin_global--;
  906. i915_vma_unpin(ring->vma);
  907. }
  908. static struct i915_vma *
  909. intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
  910. {
  911. struct i915_address_space *vm = &dev_priv->ggtt.vm;
  912. struct drm_i915_gem_object *obj;
  913. struct i915_vma *vma;
  914. obj = i915_gem_object_create_stolen(dev_priv, size);
  915. if (!obj)
  916. obj = i915_gem_object_create_internal(dev_priv, size);
  917. if (IS_ERR(obj))
  918. return ERR_CAST(obj);
  919. /*
  920. * Mark ring buffers as read-only from GPU side (so no stray overwrites)
  921. * if supported by the platform's GGTT.
  922. */
  923. if (vm->has_read_only)
  924. i915_gem_object_set_readonly(obj);
  925. vma = i915_vma_instance(obj, vm, NULL);
  926. if (IS_ERR(vma))
  927. goto err;
  928. return vma;
  929. err:
  930. i915_gem_object_put(obj);
  931. return vma;
  932. }
  933. struct intel_ring *
  934. intel_engine_create_ring(struct intel_engine_cs *engine,
  935. struct i915_timeline *timeline,
  936. int size)
  937. {
  938. struct intel_ring *ring;
  939. struct i915_vma *vma;
  940. GEM_BUG_ON(!is_power_of_2(size));
  941. GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
  942. GEM_BUG_ON(timeline == &engine->timeline);
  943. lockdep_assert_held(&engine->i915->drm.struct_mutex);
  944. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  945. if (!ring)
  946. return ERR_PTR(-ENOMEM);
  947. INIT_LIST_HEAD(&ring->request_list);
  948. ring->timeline = i915_timeline_get(timeline);
  949. ring->size = size;
  950. /* Workaround an erratum on the i830 which causes a hang if
  951. * the TAIL pointer points to within the last 2 cachelines
  952. * of the buffer.
  953. */
  954. ring->effective_size = size;
  955. if (IS_I830(engine->i915) || IS_I845G(engine->i915))
  956. ring->effective_size -= 2 * CACHELINE_BYTES;
  957. intel_ring_update_space(ring);
  958. vma = intel_ring_create_vma(engine->i915, size);
  959. if (IS_ERR(vma)) {
  960. kfree(ring);
  961. return ERR_CAST(vma);
  962. }
  963. ring->vma = vma;
  964. return ring;
  965. }
  966. void
  967. intel_ring_free(struct intel_ring *ring)
  968. {
  969. struct drm_i915_gem_object *obj = ring->vma->obj;
  970. i915_vma_close(ring->vma);
  971. __i915_gem_object_release_unless_active(obj);
  972. i915_timeline_put(ring->timeline);
  973. kfree(ring);
  974. }
  975. static void intel_ring_context_destroy(struct intel_context *ce)
  976. {
  977. GEM_BUG_ON(ce->pin_count);
  978. if (!ce->state)
  979. return;
  980. GEM_BUG_ON(i915_gem_object_is_active(ce->state->obj));
  981. i915_gem_object_put(ce->state->obj);
  982. }
  983. static int __context_pin_ppgtt(struct i915_gem_context *ctx)
  984. {
  985. struct i915_hw_ppgtt *ppgtt;
  986. int err = 0;
  987. ppgtt = ctx->ppgtt ?: ctx->i915->mm.aliasing_ppgtt;
  988. if (ppgtt)
  989. err = gen6_ppgtt_pin(ppgtt);
  990. return err;
  991. }
  992. static void __context_unpin_ppgtt(struct i915_gem_context *ctx)
  993. {
  994. struct i915_hw_ppgtt *ppgtt;
  995. ppgtt = ctx->ppgtt ?: ctx->i915->mm.aliasing_ppgtt;
  996. if (ppgtt)
  997. gen6_ppgtt_unpin(ppgtt);
  998. }
  999. static int __context_pin(struct intel_context *ce)
  1000. {
  1001. struct i915_vma *vma;
  1002. int err;
  1003. vma = ce->state;
  1004. if (!vma)
  1005. return 0;
  1006. /*
  1007. * Clear this page out of any CPU caches for coherent swap-in/out.
  1008. * We only want to do this on the first bind so that we do not stall
  1009. * on an active context (which by nature is already on the GPU).
  1010. */
  1011. if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
  1012. err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
  1013. if (err)
  1014. return err;
  1015. }
  1016. err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL | PIN_HIGH);
  1017. if (err)
  1018. return err;
  1019. /*
  1020. * And mark is as a globally pinned object to let the shrinker know
  1021. * it cannot reclaim the object until we release it.
  1022. */
  1023. vma->obj->pin_global++;
  1024. return 0;
  1025. }
  1026. static void __context_unpin(struct intel_context *ce)
  1027. {
  1028. struct i915_vma *vma;
  1029. vma = ce->state;
  1030. if (!vma)
  1031. return;
  1032. vma->obj->pin_global--;
  1033. i915_vma_unpin(vma);
  1034. }
  1035. static void intel_ring_context_unpin(struct intel_context *ce)
  1036. {
  1037. __context_unpin_ppgtt(ce->gem_context);
  1038. __context_unpin(ce);
  1039. i915_gem_context_put(ce->gem_context);
  1040. }
  1041. static struct i915_vma *
  1042. alloc_context_vma(struct intel_engine_cs *engine)
  1043. {
  1044. struct drm_i915_private *i915 = engine->i915;
  1045. struct drm_i915_gem_object *obj;
  1046. struct i915_vma *vma;
  1047. int err;
  1048. obj = i915_gem_object_create(i915, engine->context_size);
  1049. if (IS_ERR(obj))
  1050. return ERR_CAST(obj);
  1051. if (engine->default_state) {
  1052. void *defaults, *vaddr;
  1053. vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
  1054. if (IS_ERR(vaddr)) {
  1055. err = PTR_ERR(vaddr);
  1056. goto err_obj;
  1057. }
  1058. defaults = i915_gem_object_pin_map(engine->default_state,
  1059. I915_MAP_WB);
  1060. if (IS_ERR(defaults)) {
  1061. err = PTR_ERR(defaults);
  1062. goto err_map;
  1063. }
  1064. memcpy(vaddr, defaults, engine->context_size);
  1065. i915_gem_object_unpin_map(engine->default_state);
  1066. i915_gem_object_unpin_map(obj);
  1067. }
  1068. /*
  1069. * Try to make the context utilize L3 as well as LLC.
  1070. *
  1071. * On VLV we don't have L3 controls in the PTEs so we
  1072. * shouldn't touch the cache level, especially as that
  1073. * would make the object snooped which might have a
  1074. * negative performance impact.
  1075. *
  1076. * Snooping is required on non-llc platforms in execlist
  1077. * mode, but since all GGTT accesses use PAT entry 0 we
  1078. * get snooping anyway regardless of cache_level.
  1079. *
  1080. * This is only applicable for Ivy Bridge devices since
  1081. * later platforms don't have L3 control bits in the PTE.
  1082. */
  1083. if (IS_IVYBRIDGE(i915)) {
  1084. /* Ignore any error, regard it as a simple optimisation */
  1085. i915_gem_object_set_cache_level(obj, I915_CACHE_L3_LLC);
  1086. }
  1087. vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
  1088. if (IS_ERR(vma)) {
  1089. err = PTR_ERR(vma);
  1090. goto err_obj;
  1091. }
  1092. return vma;
  1093. err_map:
  1094. i915_gem_object_unpin_map(obj);
  1095. err_obj:
  1096. i915_gem_object_put(obj);
  1097. return ERR_PTR(err);
  1098. }
  1099. static struct intel_context *
  1100. __ring_context_pin(struct intel_engine_cs *engine,
  1101. struct i915_gem_context *ctx,
  1102. struct intel_context *ce)
  1103. {
  1104. int err;
  1105. if (!ce->state && engine->context_size) {
  1106. struct i915_vma *vma;
  1107. vma = alloc_context_vma(engine);
  1108. if (IS_ERR(vma)) {
  1109. err = PTR_ERR(vma);
  1110. goto err;
  1111. }
  1112. ce->state = vma;
  1113. }
  1114. err = __context_pin(ce);
  1115. if (err)
  1116. goto err;
  1117. err = __context_pin_ppgtt(ce->gem_context);
  1118. if (err)
  1119. goto err_unpin;
  1120. i915_gem_context_get(ctx);
  1121. /* One ringbuffer to rule them all */
  1122. GEM_BUG_ON(!engine->buffer);
  1123. ce->ring = engine->buffer;
  1124. return ce;
  1125. err_unpin:
  1126. __context_unpin(ce);
  1127. err:
  1128. ce->pin_count = 0;
  1129. return ERR_PTR(err);
  1130. }
  1131. static const struct intel_context_ops ring_context_ops = {
  1132. .unpin = intel_ring_context_unpin,
  1133. .destroy = intel_ring_context_destroy,
  1134. };
  1135. static struct intel_context *
  1136. intel_ring_context_pin(struct intel_engine_cs *engine,
  1137. struct i915_gem_context *ctx)
  1138. {
  1139. struct intel_context *ce = to_intel_context(ctx, engine);
  1140. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  1141. if (likely(ce->pin_count++))
  1142. return ce;
  1143. GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
  1144. ce->ops = &ring_context_ops;
  1145. return __ring_context_pin(engine, ctx, ce);
  1146. }
  1147. static int intel_init_ring_buffer(struct intel_engine_cs *engine)
  1148. {
  1149. struct i915_timeline *timeline;
  1150. struct intel_ring *ring;
  1151. unsigned int size;
  1152. int err;
  1153. intel_engine_setup_common(engine);
  1154. timeline = i915_timeline_create(engine->i915, engine->name);
  1155. if (IS_ERR(timeline)) {
  1156. err = PTR_ERR(timeline);
  1157. goto err;
  1158. }
  1159. ring = intel_engine_create_ring(engine, timeline, 32 * PAGE_SIZE);
  1160. i915_timeline_put(timeline);
  1161. if (IS_ERR(ring)) {
  1162. err = PTR_ERR(ring);
  1163. goto err;
  1164. }
  1165. err = intel_ring_pin(ring);
  1166. if (err)
  1167. goto err_ring;
  1168. GEM_BUG_ON(engine->buffer);
  1169. engine->buffer = ring;
  1170. size = PAGE_SIZE;
  1171. if (HAS_BROKEN_CS_TLB(engine->i915))
  1172. size = I830_WA_SIZE;
  1173. err = intel_engine_create_scratch(engine, size);
  1174. if (err)
  1175. goto err_unpin;
  1176. err = intel_engine_init_common(engine);
  1177. if (err)
  1178. goto err_scratch;
  1179. return 0;
  1180. err_scratch:
  1181. intel_engine_cleanup_scratch(engine);
  1182. err_unpin:
  1183. intel_ring_unpin(ring);
  1184. err_ring:
  1185. intel_ring_free(ring);
  1186. err:
  1187. intel_engine_cleanup_common(engine);
  1188. return err;
  1189. }
  1190. void intel_engine_cleanup(struct intel_engine_cs *engine)
  1191. {
  1192. struct drm_i915_private *dev_priv = engine->i915;
  1193. WARN_ON(INTEL_GEN(dev_priv) > 2 &&
  1194. (I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1195. intel_ring_unpin(engine->buffer);
  1196. intel_ring_free(engine->buffer);
  1197. if (engine->cleanup)
  1198. engine->cleanup(engine);
  1199. intel_engine_cleanup_common(engine);
  1200. dev_priv->engine[engine->id] = NULL;
  1201. kfree(engine);
  1202. }
  1203. void intel_legacy_submission_resume(struct drm_i915_private *dev_priv)
  1204. {
  1205. struct intel_engine_cs *engine;
  1206. enum intel_engine_id id;
  1207. /* Restart from the beginning of the rings for convenience */
  1208. for_each_engine(engine, dev_priv, id)
  1209. intel_ring_reset(engine->buffer, 0);
  1210. }
  1211. static int load_pd_dir(struct i915_request *rq,
  1212. const struct i915_hw_ppgtt *ppgtt)
  1213. {
  1214. const struct intel_engine_cs * const engine = rq->engine;
  1215. u32 *cs;
  1216. cs = intel_ring_begin(rq, 6);
  1217. if (IS_ERR(cs))
  1218. return PTR_ERR(cs);
  1219. *cs++ = MI_LOAD_REGISTER_IMM(1);
  1220. *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
  1221. *cs++ = PP_DIR_DCLV_2G;
  1222. *cs++ = MI_LOAD_REGISTER_IMM(1);
  1223. *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
  1224. *cs++ = ppgtt->pd.base.ggtt_offset << 10;
  1225. intel_ring_advance(rq, cs);
  1226. return 0;
  1227. }
  1228. static int flush_pd_dir(struct i915_request *rq)
  1229. {
  1230. const struct intel_engine_cs * const engine = rq->engine;
  1231. u32 *cs;
  1232. cs = intel_ring_begin(rq, 4);
  1233. if (IS_ERR(cs))
  1234. return PTR_ERR(cs);
  1235. /* Stall until the page table load is complete */
  1236. *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  1237. *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
  1238. *cs++ = i915_ggtt_offset(engine->scratch);
  1239. *cs++ = MI_NOOP;
  1240. intel_ring_advance(rq, cs);
  1241. return 0;
  1242. }
  1243. static inline int mi_set_context(struct i915_request *rq, u32 flags)
  1244. {
  1245. struct drm_i915_private *i915 = rq->i915;
  1246. struct intel_engine_cs *engine = rq->engine;
  1247. enum intel_engine_id id;
  1248. const int num_rings =
  1249. /* Use an extended w/a on gen7 if signalling from other rings */
  1250. (HAS_LEGACY_SEMAPHORES(i915) && IS_GEN7(i915)) ?
  1251. INTEL_INFO(i915)->num_rings - 1 :
  1252. 0;
  1253. bool force_restore = false;
  1254. int len;
  1255. u32 *cs;
  1256. flags |= MI_MM_SPACE_GTT;
  1257. if (IS_HASWELL(i915))
  1258. /* These flags are for resource streamer on HSW+ */
  1259. flags |= HSW_MI_RS_SAVE_STATE_EN | HSW_MI_RS_RESTORE_STATE_EN;
  1260. else
  1261. flags |= MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN;
  1262. len = 4;
  1263. if (IS_GEN7(i915))
  1264. len += 2 + (num_rings ? 4*num_rings + 6 : 0);
  1265. if (flags & MI_FORCE_RESTORE) {
  1266. GEM_BUG_ON(flags & MI_RESTORE_INHIBIT);
  1267. flags &= ~MI_FORCE_RESTORE;
  1268. force_restore = true;
  1269. len += 2;
  1270. }
  1271. cs = intel_ring_begin(rq, len);
  1272. if (IS_ERR(cs))
  1273. return PTR_ERR(cs);
  1274. /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
  1275. if (IS_GEN7(i915)) {
  1276. *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
  1277. if (num_rings) {
  1278. struct intel_engine_cs *signaller;
  1279. *cs++ = MI_LOAD_REGISTER_IMM(num_rings);
  1280. for_each_engine(signaller, i915, id) {
  1281. if (signaller == engine)
  1282. continue;
  1283. *cs++ = i915_mmio_reg_offset(
  1284. RING_PSMI_CTL(signaller->mmio_base));
  1285. *cs++ = _MASKED_BIT_ENABLE(
  1286. GEN6_PSMI_SLEEP_MSG_DISABLE);
  1287. }
  1288. }
  1289. }
  1290. if (force_restore) {
  1291. /*
  1292. * The HW doesn't handle being told to restore the current
  1293. * context very well. Quite often it likes goes to go off and
  1294. * sulk, especially when it is meant to be reloading PP_DIR.
  1295. * A very simple fix to force the reload is to simply switch
  1296. * away from the current context and back again.
  1297. *
  1298. * Note that the kernel_context will contain random state
  1299. * following the INHIBIT_RESTORE. We accept this since we
  1300. * never use the kernel_context state; it is merely a
  1301. * placeholder we use to flush other contexts.
  1302. */
  1303. *cs++ = MI_SET_CONTEXT;
  1304. *cs++ = i915_ggtt_offset(to_intel_context(i915->kernel_context,
  1305. engine)->state) |
  1306. MI_MM_SPACE_GTT |
  1307. MI_RESTORE_INHIBIT;
  1308. }
  1309. *cs++ = MI_NOOP;
  1310. *cs++ = MI_SET_CONTEXT;
  1311. *cs++ = i915_ggtt_offset(rq->hw_context->state) | flags;
  1312. /*
  1313. * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
  1314. * WaMiSetContext_Hang:snb,ivb,vlv
  1315. */
  1316. *cs++ = MI_NOOP;
  1317. if (IS_GEN7(i915)) {
  1318. if (num_rings) {
  1319. struct intel_engine_cs *signaller;
  1320. i915_reg_t last_reg = {}; /* keep gcc quiet */
  1321. *cs++ = MI_LOAD_REGISTER_IMM(num_rings);
  1322. for_each_engine(signaller, i915, id) {
  1323. if (signaller == engine)
  1324. continue;
  1325. last_reg = RING_PSMI_CTL(signaller->mmio_base);
  1326. *cs++ = i915_mmio_reg_offset(last_reg);
  1327. *cs++ = _MASKED_BIT_DISABLE(
  1328. GEN6_PSMI_SLEEP_MSG_DISABLE);
  1329. }
  1330. /* Insert a delay before the next switch! */
  1331. *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  1332. *cs++ = i915_mmio_reg_offset(last_reg);
  1333. *cs++ = i915_ggtt_offset(engine->scratch);
  1334. *cs++ = MI_NOOP;
  1335. }
  1336. *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  1337. }
  1338. intel_ring_advance(rq, cs);
  1339. return 0;
  1340. }
  1341. static int remap_l3(struct i915_request *rq, int slice)
  1342. {
  1343. u32 *cs, *remap_info = rq->i915->l3_parity.remap_info[slice];
  1344. int i;
  1345. if (!remap_info)
  1346. return 0;
  1347. cs = intel_ring_begin(rq, GEN7_L3LOG_SIZE/4 * 2 + 2);
  1348. if (IS_ERR(cs))
  1349. return PTR_ERR(cs);
  1350. /*
  1351. * Note: We do not worry about the concurrent register cacheline hang
  1352. * here because no other code should access these registers other than
  1353. * at initialization time.
  1354. */
  1355. *cs++ = MI_LOAD_REGISTER_IMM(GEN7_L3LOG_SIZE/4);
  1356. for (i = 0; i < GEN7_L3LOG_SIZE/4; i++) {
  1357. *cs++ = i915_mmio_reg_offset(GEN7_L3LOG(slice, i));
  1358. *cs++ = remap_info[i];
  1359. }
  1360. *cs++ = MI_NOOP;
  1361. intel_ring_advance(rq, cs);
  1362. return 0;
  1363. }
  1364. static int switch_context(struct i915_request *rq)
  1365. {
  1366. struct intel_engine_cs *engine = rq->engine;
  1367. struct i915_gem_context *ctx = rq->gem_context;
  1368. struct i915_hw_ppgtt *ppgtt = ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
  1369. unsigned int unwind_mm = 0;
  1370. u32 hw_flags = 0;
  1371. int ret, i;
  1372. lockdep_assert_held(&rq->i915->drm.struct_mutex);
  1373. GEM_BUG_ON(HAS_EXECLISTS(rq->i915));
  1374. if (ppgtt) {
  1375. ret = load_pd_dir(rq, ppgtt);
  1376. if (ret)
  1377. goto err;
  1378. if (intel_engine_flag(engine) & ppgtt->pd_dirty_rings) {
  1379. unwind_mm = intel_engine_flag(engine);
  1380. ppgtt->pd_dirty_rings &= ~unwind_mm;
  1381. hw_flags = MI_FORCE_RESTORE;
  1382. }
  1383. }
  1384. if (rq->hw_context->state) {
  1385. GEM_BUG_ON(engine->id != RCS);
  1386. /*
  1387. * The kernel context(s) is treated as pure scratch and is not
  1388. * expected to retain any state (as we sacrifice it during
  1389. * suspend and on resume it may be corrupted). This is ok,
  1390. * as nothing actually executes using the kernel context; it
  1391. * is purely used for flushing user contexts.
  1392. */
  1393. if (i915_gem_context_is_kernel(ctx))
  1394. hw_flags = MI_RESTORE_INHIBIT;
  1395. ret = mi_set_context(rq, hw_flags);
  1396. if (ret)
  1397. goto err_mm;
  1398. }
  1399. if (ppgtt) {
  1400. ret = flush_pd_dir(rq);
  1401. if (ret)
  1402. goto err_mm;
  1403. }
  1404. if (ctx->remap_slice) {
  1405. for (i = 0; i < MAX_L3_SLICES; i++) {
  1406. if (!(ctx->remap_slice & BIT(i)))
  1407. continue;
  1408. ret = remap_l3(rq, i);
  1409. if (ret)
  1410. goto err_mm;
  1411. }
  1412. ctx->remap_slice = 0;
  1413. }
  1414. return 0;
  1415. err_mm:
  1416. if (unwind_mm)
  1417. ppgtt->pd_dirty_rings |= unwind_mm;
  1418. err:
  1419. return ret;
  1420. }
  1421. static int ring_request_alloc(struct i915_request *request)
  1422. {
  1423. int ret;
  1424. GEM_BUG_ON(!request->hw_context->pin_count);
  1425. /* Flush enough space to reduce the likelihood of waiting after
  1426. * we start building the request - in which case we will just
  1427. * have to repeat work.
  1428. */
  1429. request->reserved_space += LEGACY_REQUEST_SIZE;
  1430. ret = intel_ring_wait_for_space(request->ring, request->reserved_space);
  1431. if (ret)
  1432. return ret;
  1433. ret = switch_context(request);
  1434. if (ret)
  1435. return ret;
  1436. request->reserved_space -= LEGACY_REQUEST_SIZE;
  1437. return 0;
  1438. }
  1439. static noinline int wait_for_space(struct intel_ring *ring, unsigned int bytes)
  1440. {
  1441. struct i915_request *target;
  1442. long timeout;
  1443. lockdep_assert_held(&ring->vma->vm->i915->drm.struct_mutex);
  1444. if (intel_ring_update_space(ring) >= bytes)
  1445. return 0;
  1446. GEM_BUG_ON(list_empty(&ring->request_list));
  1447. list_for_each_entry(target, &ring->request_list, ring_link) {
  1448. /* Would completion of this request free enough space? */
  1449. if (bytes <= __intel_ring_space(target->postfix,
  1450. ring->emit, ring->size))
  1451. break;
  1452. }
  1453. if (WARN_ON(&target->ring_link == &ring->request_list))
  1454. return -ENOSPC;
  1455. timeout = i915_request_wait(target,
  1456. I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
  1457. MAX_SCHEDULE_TIMEOUT);
  1458. if (timeout < 0)
  1459. return timeout;
  1460. i915_request_retire_upto(target);
  1461. intel_ring_update_space(ring);
  1462. GEM_BUG_ON(ring->space < bytes);
  1463. return 0;
  1464. }
  1465. int intel_ring_wait_for_space(struct intel_ring *ring, unsigned int bytes)
  1466. {
  1467. GEM_BUG_ON(bytes > ring->effective_size);
  1468. if (unlikely(bytes > ring->effective_size - ring->emit))
  1469. bytes += ring->size - ring->emit;
  1470. if (unlikely(bytes > ring->space)) {
  1471. int ret = wait_for_space(ring, bytes);
  1472. if (unlikely(ret))
  1473. return ret;
  1474. }
  1475. GEM_BUG_ON(ring->space < bytes);
  1476. return 0;
  1477. }
  1478. u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords)
  1479. {
  1480. struct intel_ring *ring = rq->ring;
  1481. const unsigned int remain_usable = ring->effective_size - ring->emit;
  1482. const unsigned int bytes = num_dwords * sizeof(u32);
  1483. unsigned int need_wrap = 0;
  1484. unsigned int total_bytes;
  1485. u32 *cs;
  1486. /* Packets must be qword aligned. */
  1487. GEM_BUG_ON(num_dwords & 1);
  1488. total_bytes = bytes + rq->reserved_space;
  1489. GEM_BUG_ON(total_bytes > ring->effective_size);
  1490. if (unlikely(total_bytes > remain_usable)) {
  1491. const int remain_actual = ring->size - ring->emit;
  1492. if (bytes > remain_usable) {
  1493. /*
  1494. * Not enough space for the basic request. So need to
  1495. * flush out the remainder and then wait for
  1496. * base + reserved.
  1497. */
  1498. total_bytes += remain_actual;
  1499. need_wrap = remain_actual | 1;
  1500. } else {
  1501. /*
  1502. * The base request will fit but the reserved space
  1503. * falls off the end. So we don't need an immediate
  1504. * wrap and only need to effectively wait for the
  1505. * reserved size from the start of ringbuffer.
  1506. */
  1507. total_bytes = rq->reserved_space + remain_actual;
  1508. }
  1509. }
  1510. if (unlikely(total_bytes > ring->space)) {
  1511. int ret;
  1512. /*
  1513. * Space is reserved in the ringbuffer for finalising the
  1514. * request, as that cannot be allowed to fail. During request
  1515. * finalisation, reserved_space is set to 0 to stop the
  1516. * overallocation and the assumption is that then we never need
  1517. * to wait (which has the risk of failing with EINTR).
  1518. *
  1519. * See also i915_request_alloc() and i915_request_add().
  1520. */
  1521. GEM_BUG_ON(!rq->reserved_space);
  1522. ret = wait_for_space(ring, total_bytes);
  1523. if (unlikely(ret))
  1524. return ERR_PTR(ret);
  1525. }
  1526. if (unlikely(need_wrap)) {
  1527. need_wrap &= ~1;
  1528. GEM_BUG_ON(need_wrap > ring->space);
  1529. GEM_BUG_ON(ring->emit + need_wrap > ring->size);
  1530. GEM_BUG_ON(!IS_ALIGNED(need_wrap, sizeof(u64)));
  1531. /* Fill the tail with MI_NOOP */
  1532. memset64(ring->vaddr + ring->emit, 0, need_wrap / sizeof(u64));
  1533. ring->space -= need_wrap;
  1534. ring->emit = 0;
  1535. }
  1536. GEM_BUG_ON(ring->emit > ring->size - bytes);
  1537. GEM_BUG_ON(ring->space < bytes);
  1538. cs = ring->vaddr + ring->emit;
  1539. GEM_DEBUG_EXEC(memset32(cs, POISON_INUSE, bytes / sizeof(*cs)));
  1540. ring->emit += bytes;
  1541. ring->space -= bytes;
  1542. return cs;
  1543. }
  1544. /* Align the ring tail to a cacheline boundary */
  1545. int intel_ring_cacheline_align(struct i915_request *rq)
  1546. {
  1547. int num_dwords;
  1548. void *cs;
  1549. num_dwords = (rq->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(u32);
  1550. if (num_dwords == 0)
  1551. return 0;
  1552. num_dwords = CACHELINE_DWORDS - num_dwords;
  1553. GEM_BUG_ON(num_dwords & 1);
  1554. cs = intel_ring_begin(rq, num_dwords);
  1555. if (IS_ERR(cs))
  1556. return PTR_ERR(cs);
  1557. memset64(cs, (u64)MI_NOOP << 32 | MI_NOOP, num_dwords / 2);
  1558. intel_ring_advance(rq, cs);
  1559. GEM_BUG_ON(rq->ring->emit & (CACHELINE_BYTES - 1));
  1560. return 0;
  1561. }
  1562. static void gen6_bsd_submit_request(struct i915_request *request)
  1563. {
  1564. struct drm_i915_private *dev_priv = request->i915;
  1565. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  1566. /* Every tail move must follow the sequence below */
  1567. /* Disable notification that the ring is IDLE. The GT
  1568. * will then assume that it is busy and bring it out of rc6.
  1569. */
  1570. I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1571. _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  1572. /* Clear the context id. Here be magic! */
  1573. I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
  1574. /* Wait for the ring not to be idle, i.e. for it to wake up. */
  1575. if (__intel_wait_for_register_fw(dev_priv,
  1576. GEN6_BSD_SLEEP_PSMI_CONTROL,
  1577. GEN6_BSD_SLEEP_INDICATOR,
  1578. 0,
  1579. 1000, 0, NULL))
  1580. DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
  1581. /* Now that the ring is fully powered up, update the tail */
  1582. i9xx_submit_request(request);
  1583. /* Let the ring send IDLE messages to the GT again,
  1584. * and so let it sleep to conserve power when idle.
  1585. */
  1586. I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1587. _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  1588. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  1589. }
  1590. static int gen6_bsd_ring_flush(struct i915_request *rq, u32 mode)
  1591. {
  1592. u32 cmd, *cs;
  1593. cs = intel_ring_begin(rq, 4);
  1594. if (IS_ERR(cs))
  1595. return PTR_ERR(cs);
  1596. cmd = MI_FLUSH_DW;
  1597. /* We always require a command barrier so that subsequent
  1598. * commands, such as breadcrumb interrupts, are strictly ordered
  1599. * wrt the contents of the write cache being flushed to memory
  1600. * (and thus being coherent from the CPU).
  1601. */
  1602. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1603. /*
  1604. * Bspec vol 1c.5 - video engine command streamer:
  1605. * "If ENABLED, all TLBs will be invalidated once the flush
  1606. * operation is complete. This bit is only valid when the
  1607. * Post-Sync Operation field is a value of 1h or 3h."
  1608. */
  1609. if (mode & EMIT_INVALIDATE)
  1610. cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
  1611. *cs++ = cmd;
  1612. *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
  1613. *cs++ = 0;
  1614. *cs++ = MI_NOOP;
  1615. intel_ring_advance(rq, cs);
  1616. return 0;
  1617. }
  1618. static int
  1619. hsw_emit_bb_start(struct i915_request *rq,
  1620. u64 offset, u32 len,
  1621. unsigned int dispatch_flags)
  1622. {
  1623. u32 *cs;
  1624. cs = intel_ring_begin(rq, 2);
  1625. if (IS_ERR(cs))
  1626. return PTR_ERR(cs);
  1627. *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
  1628. 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW);
  1629. /* bit0-7 is the length on GEN6+ */
  1630. *cs++ = offset;
  1631. intel_ring_advance(rq, cs);
  1632. return 0;
  1633. }
  1634. static int
  1635. gen6_emit_bb_start(struct i915_request *rq,
  1636. u64 offset, u32 len,
  1637. unsigned int dispatch_flags)
  1638. {
  1639. u32 *cs;
  1640. cs = intel_ring_begin(rq, 2);
  1641. if (IS_ERR(cs))
  1642. return PTR_ERR(cs);
  1643. *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
  1644. 0 : MI_BATCH_NON_SECURE_I965);
  1645. /* bit0-7 is the length on GEN6+ */
  1646. *cs++ = offset;
  1647. intel_ring_advance(rq, cs);
  1648. return 0;
  1649. }
  1650. /* Blitter support (SandyBridge+) */
  1651. static int gen6_ring_flush(struct i915_request *rq, u32 mode)
  1652. {
  1653. u32 cmd, *cs;
  1654. cs = intel_ring_begin(rq, 4);
  1655. if (IS_ERR(cs))
  1656. return PTR_ERR(cs);
  1657. cmd = MI_FLUSH_DW;
  1658. /* We always require a command barrier so that subsequent
  1659. * commands, such as breadcrumb interrupts, are strictly ordered
  1660. * wrt the contents of the write cache being flushed to memory
  1661. * (and thus being coherent from the CPU).
  1662. */
  1663. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1664. /*
  1665. * Bspec vol 1c.3 - blitter engine command streamer:
  1666. * "If ENABLED, all TLBs will be invalidated once the flush
  1667. * operation is complete. This bit is only valid when the
  1668. * Post-Sync Operation field is a value of 1h or 3h."
  1669. */
  1670. if (mode & EMIT_INVALIDATE)
  1671. cmd |= MI_INVALIDATE_TLB;
  1672. *cs++ = cmd;
  1673. *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
  1674. *cs++ = 0;
  1675. *cs++ = MI_NOOP;
  1676. intel_ring_advance(rq, cs);
  1677. return 0;
  1678. }
  1679. static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
  1680. struct intel_engine_cs *engine)
  1681. {
  1682. int i;
  1683. if (!HAS_LEGACY_SEMAPHORES(dev_priv))
  1684. return;
  1685. GEM_BUG_ON(INTEL_GEN(dev_priv) < 6);
  1686. engine->semaphore.sync_to = gen6_ring_sync_to;
  1687. engine->semaphore.signal = gen6_signal;
  1688. /*
  1689. * The current semaphore is only applied on pre-gen8
  1690. * platform. And there is no VCS2 ring on the pre-gen8
  1691. * platform. So the semaphore between RCS and VCS2 is
  1692. * initialized as INVALID.
  1693. */
  1694. for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
  1695. static const struct {
  1696. u32 wait_mbox;
  1697. i915_reg_t mbox_reg;
  1698. } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
  1699. [RCS_HW] = {
  1700. [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
  1701. [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
  1702. [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
  1703. },
  1704. [VCS_HW] = {
  1705. [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
  1706. [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
  1707. [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
  1708. },
  1709. [BCS_HW] = {
  1710. [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
  1711. [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
  1712. [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
  1713. },
  1714. [VECS_HW] = {
  1715. [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
  1716. [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
  1717. [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
  1718. },
  1719. };
  1720. u32 wait_mbox;
  1721. i915_reg_t mbox_reg;
  1722. if (i == engine->hw_id) {
  1723. wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
  1724. mbox_reg = GEN6_NOSYNC;
  1725. } else {
  1726. wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
  1727. mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
  1728. }
  1729. engine->semaphore.mbox.wait[i] = wait_mbox;
  1730. engine->semaphore.mbox.signal[i] = mbox_reg;
  1731. }
  1732. }
  1733. static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
  1734. struct intel_engine_cs *engine)
  1735. {
  1736. if (INTEL_GEN(dev_priv) >= 6) {
  1737. engine->irq_enable = gen6_irq_enable;
  1738. engine->irq_disable = gen6_irq_disable;
  1739. engine->irq_seqno_barrier = gen6_seqno_barrier;
  1740. } else if (INTEL_GEN(dev_priv) >= 5) {
  1741. engine->irq_enable = gen5_irq_enable;
  1742. engine->irq_disable = gen5_irq_disable;
  1743. engine->irq_seqno_barrier = gen5_seqno_barrier;
  1744. } else if (INTEL_GEN(dev_priv) >= 3) {
  1745. engine->irq_enable = i9xx_irq_enable;
  1746. engine->irq_disable = i9xx_irq_disable;
  1747. } else {
  1748. engine->irq_enable = i8xx_irq_enable;
  1749. engine->irq_disable = i8xx_irq_disable;
  1750. }
  1751. }
  1752. static void i9xx_set_default_submission(struct intel_engine_cs *engine)
  1753. {
  1754. engine->submit_request = i9xx_submit_request;
  1755. engine->cancel_requests = cancel_requests;
  1756. engine->park = NULL;
  1757. engine->unpark = NULL;
  1758. }
  1759. static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
  1760. {
  1761. i9xx_set_default_submission(engine);
  1762. engine->submit_request = gen6_bsd_submit_request;
  1763. }
  1764. static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
  1765. struct intel_engine_cs *engine)
  1766. {
  1767. /* gen8+ are only supported with execlists */
  1768. GEM_BUG_ON(INTEL_GEN(dev_priv) >= 8);
  1769. intel_ring_init_irq(dev_priv, engine);
  1770. intel_ring_init_semaphores(dev_priv, engine);
  1771. engine->init_hw = init_ring_common;
  1772. engine->reset.prepare = reset_prepare;
  1773. engine->reset.reset = reset_ring;
  1774. engine->reset.finish = reset_finish;
  1775. engine->context_pin = intel_ring_context_pin;
  1776. engine->request_alloc = ring_request_alloc;
  1777. engine->emit_breadcrumb = i9xx_emit_breadcrumb;
  1778. engine->emit_breadcrumb_sz = i9xx_emit_breadcrumb_sz;
  1779. if (HAS_LEGACY_SEMAPHORES(dev_priv)) {
  1780. int num_rings;
  1781. engine->emit_breadcrumb = gen6_sema_emit_breadcrumb;
  1782. num_rings = INTEL_INFO(dev_priv)->num_rings - 1;
  1783. engine->emit_breadcrumb_sz += num_rings * 3;
  1784. if (num_rings & 1)
  1785. engine->emit_breadcrumb_sz++;
  1786. }
  1787. engine->set_default_submission = i9xx_set_default_submission;
  1788. if (INTEL_GEN(dev_priv) >= 6)
  1789. engine->emit_bb_start = gen6_emit_bb_start;
  1790. else if (INTEL_GEN(dev_priv) >= 4)
  1791. engine->emit_bb_start = i965_emit_bb_start;
  1792. else if (IS_I830(dev_priv) || IS_I845G(dev_priv))
  1793. engine->emit_bb_start = i830_emit_bb_start;
  1794. else
  1795. engine->emit_bb_start = i915_emit_bb_start;
  1796. }
  1797. int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
  1798. {
  1799. struct drm_i915_private *dev_priv = engine->i915;
  1800. int ret;
  1801. intel_ring_default_vfuncs(dev_priv, engine);
  1802. if (HAS_L3_DPF(dev_priv))
  1803. engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1804. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
  1805. if (INTEL_GEN(dev_priv) >= 6) {
  1806. engine->init_context = intel_rcs_ctx_init;
  1807. engine->emit_flush = gen7_render_ring_flush;
  1808. if (IS_GEN6(dev_priv))
  1809. engine->emit_flush = gen6_render_ring_flush;
  1810. } else if (IS_GEN5(dev_priv)) {
  1811. engine->emit_flush = gen4_render_ring_flush;
  1812. } else {
  1813. if (INTEL_GEN(dev_priv) < 4)
  1814. engine->emit_flush = gen2_render_ring_flush;
  1815. else
  1816. engine->emit_flush = gen4_render_ring_flush;
  1817. engine->irq_enable_mask = I915_USER_INTERRUPT;
  1818. }
  1819. if (IS_HASWELL(dev_priv))
  1820. engine->emit_bb_start = hsw_emit_bb_start;
  1821. engine->init_hw = init_render_ring;
  1822. ret = intel_init_ring_buffer(engine);
  1823. if (ret)
  1824. return ret;
  1825. return 0;
  1826. }
  1827. int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
  1828. {
  1829. struct drm_i915_private *dev_priv = engine->i915;
  1830. intel_ring_default_vfuncs(dev_priv, engine);
  1831. if (INTEL_GEN(dev_priv) >= 6) {
  1832. /* gen6 bsd needs a special wa for tail updates */
  1833. if (IS_GEN6(dev_priv))
  1834. engine->set_default_submission = gen6_bsd_set_default_submission;
  1835. engine->emit_flush = gen6_bsd_ring_flush;
  1836. engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
  1837. } else {
  1838. engine->emit_flush = bsd_ring_flush;
  1839. if (IS_GEN5(dev_priv))
  1840. engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
  1841. else
  1842. engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
  1843. }
  1844. return intel_init_ring_buffer(engine);
  1845. }
  1846. int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
  1847. {
  1848. struct drm_i915_private *dev_priv = engine->i915;
  1849. intel_ring_default_vfuncs(dev_priv, engine);
  1850. engine->emit_flush = gen6_ring_flush;
  1851. engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
  1852. return intel_init_ring_buffer(engine);
  1853. }
  1854. int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
  1855. {
  1856. struct drm_i915_private *dev_priv = engine->i915;
  1857. intel_ring_default_vfuncs(dev_priv, engine);
  1858. engine->emit_flush = gen6_ring_flush;
  1859. engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
  1860. engine->irq_enable = hsw_vebox_irq_enable;
  1861. engine->irq_disable = hsw_vebox_irq_disable;
  1862. return intel_init_ring_buffer(engine);
  1863. }