intel_ringbuffer.c 59 KB

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