intel_ringbuffer.c 57 KB

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