intel_ringbuffer.c 58 KB

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