intel_ringbuffer.c 58 KB

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