intel_ringbuffer.c 55 KB

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