intel_ringbuffer.c 55 KB

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