intel_ringbuffer.c 57 KB

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