intel_ringbuffer.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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 struct i915_request *reset_prepare(struct intel_engine_cs *engine)
  459. {
  460. intel_engine_stop_cs(engine);
  461. if (engine->irq_seqno_barrier)
  462. engine->irq_seqno_barrier(engine);
  463. return i915_gem_find_active_request(engine);
  464. }
  465. static void reset_ring(struct intel_engine_cs *engine,
  466. struct i915_request *request)
  467. {
  468. GEM_TRACE("%s seqno=%x\n",
  469. engine->name, request ? request->global_seqno : 0);
  470. /*
  471. * RC6 must be prevented until the reset is complete and the engine
  472. * reinitialised. If it occurs in the middle of this sequence, the
  473. * state written to/loaded from the power context is ill-defined (e.g.
  474. * the PP_BASE_DIR may be lost).
  475. */
  476. assert_forcewakes_active(engine->i915, FORCEWAKE_ALL);
  477. /*
  478. * Try to restore the logical GPU state to match the continuation
  479. * of the request queue. If we skip the context/PD restore, then
  480. * the next request may try to execute assuming that its context
  481. * is valid and loaded on the GPU and so may try to access invalid
  482. * memory, prompting repeated GPU hangs.
  483. *
  484. * If the request was guilty, we still restore the logical state
  485. * in case the next request requires it (e.g. the aliasing ppgtt),
  486. * but skip over the hung batch.
  487. *
  488. * If the request was innocent, we try to replay the request with
  489. * the restored context.
  490. */
  491. if (request) {
  492. struct drm_i915_private *dev_priv = request->i915;
  493. struct intel_context *ce = request->hw_context;
  494. struct i915_hw_ppgtt *ppgtt;
  495. if (ce->state) {
  496. I915_WRITE(CCID,
  497. i915_ggtt_offset(ce->state) |
  498. BIT(8) /* must be set! */ |
  499. CCID_EXTENDED_STATE_SAVE |
  500. CCID_EXTENDED_STATE_RESTORE |
  501. CCID_EN);
  502. }
  503. ppgtt = request->gem_context->ppgtt ?: engine->i915->mm.aliasing_ppgtt;
  504. if (ppgtt) {
  505. u32 pd_offset = ppgtt->pd.base.ggtt_offset << 10;
  506. I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
  507. I915_WRITE(RING_PP_DIR_BASE(engine), pd_offset);
  508. /* Wait for the PD reload to complete */
  509. if (intel_wait_for_register(dev_priv,
  510. RING_PP_DIR_BASE(engine),
  511. BIT(0), 0,
  512. 10))
  513. DRM_ERROR("Wait for reload of ppgtt page-directory timed out\n");
  514. ppgtt->pd_dirty_rings &= ~intel_engine_flag(engine);
  515. }
  516. /* If the rq hung, jump to its breadcrumb and skip the batch */
  517. if (request->fence.error == -EIO)
  518. request->ring->head = request->postfix;
  519. } else {
  520. engine->legacy_active_context = NULL;
  521. engine->legacy_active_ppgtt = NULL;
  522. }
  523. }
  524. static void reset_finish(struct intel_engine_cs *engine)
  525. {
  526. }
  527. static int intel_rcs_ctx_init(struct i915_request *rq)
  528. {
  529. int ret;
  530. ret = intel_ctx_workarounds_emit(rq);
  531. if (ret != 0)
  532. return ret;
  533. ret = i915_gem_render_state_emit(rq);
  534. if (ret)
  535. return ret;
  536. return 0;
  537. }
  538. static int init_render_ring(struct intel_engine_cs *engine)
  539. {
  540. struct drm_i915_private *dev_priv = engine->i915;
  541. int ret = init_ring_common(engine);
  542. if (ret)
  543. return ret;
  544. intel_whitelist_workarounds_apply(engine);
  545. /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
  546. if (IS_GEN(dev_priv, 4, 6))
  547. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
  548. /* We need to disable the AsyncFlip performance optimisations in order
  549. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  550. * programmed to '1' on all products.
  551. *
  552. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
  553. */
  554. if (IS_GEN(dev_priv, 6, 7))
  555. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  556. /* Required for the hardware to program scanline values for waiting */
  557. /* WaEnableFlushTlbInvalidationMode:snb */
  558. if (IS_GEN6(dev_priv))
  559. I915_WRITE(GFX_MODE,
  560. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
  561. /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
  562. if (IS_GEN7(dev_priv))
  563. I915_WRITE(GFX_MODE_GEN7,
  564. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
  565. _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
  566. if (IS_GEN6(dev_priv)) {
  567. /* From the Sandybridge PRM, volume 1 part 3, page 24:
  568. * "If this bit is set, STCunit will have LRA as replacement
  569. * policy. [...] This bit must be reset. LRA replacement
  570. * policy is not supported."
  571. */
  572. I915_WRITE(CACHE_MODE_0,
  573. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  574. }
  575. if (IS_GEN(dev_priv, 6, 7))
  576. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  577. if (INTEL_GEN(dev_priv) >= 6)
  578. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  579. return 0;
  580. }
  581. static u32 *gen6_signal(struct i915_request *rq, u32 *cs)
  582. {
  583. struct drm_i915_private *dev_priv = rq->i915;
  584. struct intel_engine_cs *engine;
  585. enum intel_engine_id id;
  586. int num_rings = 0;
  587. for_each_engine(engine, dev_priv, id) {
  588. i915_reg_t mbox_reg;
  589. if (!(BIT(engine->hw_id) & GEN6_SEMAPHORES_MASK))
  590. continue;
  591. mbox_reg = rq->engine->semaphore.mbox.signal[engine->hw_id];
  592. if (i915_mmio_reg_valid(mbox_reg)) {
  593. *cs++ = MI_LOAD_REGISTER_IMM(1);
  594. *cs++ = i915_mmio_reg_offset(mbox_reg);
  595. *cs++ = rq->global_seqno;
  596. num_rings++;
  597. }
  598. }
  599. if (num_rings & 1)
  600. *cs++ = MI_NOOP;
  601. return cs;
  602. }
  603. static void cancel_requests(struct intel_engine_cs *engine)
  604. {
  605. struct i915_request *request;
  606. unsigned long flags;
  607. spin_lock_irqsave(&engine->timeline.lock, flags);
  608. /* Mark all submitted requests as skipped. */
  609. list_for_each_entry(request, &engine->timeline.requests, link) {
  610. GEM_BUG_ON(!request->global_seqno);
  611. if (!i915_request_completed(request))
  612. dma_fence_set_error(&request->fence, -EIO);
  613. }
  614. /* Remaining _unready_ requests will be nop'ed when submitted */
  615. spin_unlock_irqrestore(&engine->timeline.lock, flags);
  616. }
  617. static void i9xx_submit_request(struct i915_request *request)
  618. {
  619. struct drm_i915_private *dev_priv = request->i915;
  620. i915_request_submit(request);
  621. I915_WRITE_TAIL(request->engine,
  622. intel_ring_set_tail(request->ring, request->tail));
  623. }
  624. static void i9xx_emit_breadcrumb(struct i915_request *rq, u32 *cs)
  625. {
  626. *cs++ = MI_STORE_DWORD_INDEX;
  627. *cs++ = I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT;
  628. *cs++ = rq->global_seqno;
  629. *cs++ = MI_USER_INTERRUPT;
  630. rq->tail = intel_ring_offset(rq, cs);
  631. assert_ring_tail_valid(rq->ring, rq->tail);
  632. }
  633. static const int i9xx_emit_breadcrumb_sz = 4;
  634. static void gen6_sema_emit_breadcrumb(struct i915_request *rq, u32 *cs)
  635. {
  636. return i9xx_emit_breadcrumb(rq, rq->engine->semaphore.signal(rq, cs));
  637. }
  638. static int
  639. gen6_ring_sync_to(struct i915_request *rq, struct i915_request *signal)
  640. {
  641. u32 dw1 = MI_SEMAPHORE_MBOX |
  642. MI_SEMAPHORE_COMPARE |
  643. MI_SEMAPHORE_REGISTER;
  644. u32 wait_mbox = signal->engine->semaphore.mbox.wait[rq->engine->hw_id];
  645. u32 *cs;
  646. WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
  647. cs = intel_ring_begin(rq, 4);
  648. if (IS_ERR(cs))
  649. return PTR_ERR(cs);
  650. *cs++ = dw1 | wait_mbox;
  651. /* Throughout all of the GEM code, seqno passed implies our current
  652. * seqno is >= the last seqno executed. However for hardware the
  653. * comparison is strictly greater than.
  654. */
  655. *cs++ = signal->global_seqno - 1;
  656. *cs++ = 0;
  657. *cs++ = MI_NOOP;
  658. intel_ring_advance(rq, cs);
  659. return 0;
  660. }
  661. static void
  662. gen5_seqno_barrier(struct intel_engine_cs *engine)
  663. {
  664. /* MI_STORE are internally buffered by the GPU and not flushed
  665. * either by MI_FLUSH or SyncFlush or any other combination of
  666. * MI commands.
  667. *
  668. * "Only the submission of the store operation is guaranteed.
  669. * The write result will be complete (coherent) some time later
  670. * (this is practically a finite period but there is no guaranteed
  671. * latency)."
  672. *
  673. * Empirically, we observe that we need a delay of at least 75us to
  674. * be sure that the seqno write is visible by the CPU.
  675. */
  676. usleep_range(125, 250);
  677. }
  678. static void
  679. gen6_seqno_barrier(struct intel_engine_cs *engine)
  680. {
  681. struct drm_i915_private *dev_priv = engine->i915;
  682. /* Workaround to force correct ordering between irq and seqno writes on
  683. * ivb (and maybe also on snb) by reading from a CS register (like
  684. * ACTHD) before reading the status page.
  685. *
  686. * Note that this effectively stalls the read by the time it takes to
  687. * do a memory transaction, which more or less ensures that the write
  688. * from the GPU has sufficient time to invalidate the CPU cacheline.
  689. * Alternatively we could delay the interrupt from the CS ring to give
  690. * the write time to land, but that would incur a delay after every
  691. * batch i.e. much more frequent than a delay when waiting for the
  692. * interrupt (with the same net latency).
  693. *
  694. * Also note that to prevent whole machine hangs on gen7, we have to
  695. * take the spinlock to guard against concurrent cacheline access.
  696. */
  697. spin_lock_irq(&dev_priv->uncore.lock);
  698. POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
  699. spin_unlock_irq(&dev_priv->uncore.lock);
  700. }
  701. static void
  702. gen5_irq_enable(struct intel_engine_cs *engine)
  703. {
  704. gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
  705. }
  706. static void
  707. gen5_irq_disable(struct intel_engine_cs *engine)
  708. {
  709. gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
  710. }
  711. static void
  712. i9xx_irq_enable(struct intel_engine_cs *engine)
  713. {
  714. struct drm_i915_private *dev_priv = engine->i915;
  715. dev_priv->irq_mask &= ~engine->irq_enable_mask;
  716. I915_WRITE(IMR, dev_priv->irq_mask);
  717. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  718. }
  719. static void
  720. i9xx_irq_disable(struct intel_engine_cs *engine)
  721. {
  722. struct drm_i915_private *dev_priv = engine->i915;
  723. dev_priv->irq_mask |= engine->irq_enable_mask;
  724. I915_WRITE(IMR, dev_priv->irq_mask);
  725. }
  726. static void
  727. i8xx_irq_enable(struct intel_engine_cs *engine)
  728. {
  729. struct drm_i915_private *dev_priv = engine->i915;
  730. dev_priv->irq_mask &= ~engine->irq_enable_mask;
  731. I915_WRITE16(IMR, dev_priv->irq_mask);
  732. POSTING_READ16(RING_IMR(engine->mmio_base));
  733. }
  734. static void
  735. i8xx_irq_disable(struct intel_engine_cs *engine)
  736. {
  737. struct drm_i915_private *dev_priv = engine->i915;
  738. dev_priv->irq_mask |= engine->irq_enable_mask;
  739. I915_WRITE16(IMR, dev_priv->irq_mask);
  740. }
  741. static int
  742. bsd_ring_flush(struct i915_request *rq, u32 mode)
  743. {
  744. u32 *cs;
  745. cs = intel_ring_begin(rq, 2);
  746. if (IS_ERR(cs))
  747. return PTR_ERR(cs);
  748. *cs++ = MI_FLUSH;
  749. *cs++ = MI_NOOP;
  750. intel_ring_advance(rq, cs);
  751. return 0;
  752. }
  753. static void
  754. gen6_irq_enable(struct intel_engine_cs *engine)
  755. {
  756. struct drm_i915_private *dev_priv = engine->i915;
  757. I915_WRITE_IMR(engine,
  758. ~(engine->irq_enable_mask |
  759. engine->irq_keep_mask));
  760. gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
  761. }
  762. static void
  763. gen6_irq_disable(struct intel_engine_cs *engine)
  764. {
  765. struct drm_i915_private *dev_priv = engine->i915;
  766. I915_WRITE_IMR(engine, ~engine->irq_keep_mask);
  767. gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
  768. }
  769. static void
  770. hsw_vebox_irq_enable(struct intel_engine_cs *engine)
  771. {
  772. struct drm_i915_private *dev_priv = engine->i915;
  773. I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
  774. gen6_unmask_pm_irq(dev_priv, engine->irq_enable_mask);
  775. }
  776. static void
  777. hsw_vebox_irq_disable(struct intel_engine_cs *engine)
  778. {
  779. struct drm_i915_private *dev_priv = engine->i915;
  780. I915_WRITE_IMR(engine, ~0);
  781. gen6_mask_pm_irq(dev_priv, engine->irq_enable_mask);
  782. }
  783. static int
  784. i965_emit_bb_start(struct i915_request *rq,
  785. u64 offset, u32 length,
  786. unsigned int dispatch_flags)
  787. {
  788. u32 *cs;
  789. cs = intel_ring_begin(rq, 2);
  790. if (IS_ERR(cs))
  791. return PTR_ERR(cs);
  792. *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | (dispatch_flags &
  793. I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965);
  794. *cs++ = offset;
  795. intel_ring_advance(rq, cs);
  796. return 0;
  797. }
  798. /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
  799. #define I830_BATCH_LIMIT (256*1024)
  800. #define I830_TLB_ENTRIES (2)
  801. #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
  802. static int
  803. i830_emit_bb_start(struct i915_request *rq,
  804. u64 offset, u32 len,
  805. unsigned int dispatch_flags)
  806. {
  807. u32 *cs, cs_offset = i915_ggtt_offset(rq->engine->scratch);
  808. cs = intel_ring_begin(rq, 6);
  809. if (IS_ERR(cs))
  810. return PTR_ERR(cs);
  811. /* Evict the invalid PTE TLBs */
  812. *cs++ = COLOR_BLT_CMD | BLT_WRITE_RGBA;
  813. *cs++ = BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096;
  814. *cs++ = I830_TLB_ENTRIES << 16 | 4; /* load each page */
  815. *cs++ = cs_offset;
  816. *cs++ = 0xdeadbeef;
  817. *cs++ = MI_NOOP;
  818. intel_ring_advance(rq, cs);
  819. if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
  820. if (len > I830_BATCH_LIMIT)
  821. return -ENOSPC;
  822. cs = intel_ring_begin(rq, 6 + 2);
  823. if (IS_ERR(cs))
  824. return PTR_ERR(cs);
  825. /* Blit the batch (which has now all relocs applied) to the
  826. * stable batch scratch bo area (so that the CS never
  827. * stumbles over its tlb invalidation bug) ...
  828. */
  829. *cs++ = SRC_COPY_BLT_CMD | BLT_WRITE_RGBA;
  830. *cs++ = BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096;
  831. *cs++ = DIV_ROUND_UP(len, 4096) << 16 | 4096;
  832. *cs++ = cs_offset;
  833. *cs++ = 4096;
  834. *cs++ = offset;
  835. *cs++ = MI_FLUSH;
  836. *cs++ = MI_NOOP;
  837. intel_ring_advance(rq, cs);
  838. /* ... and execute it. */
  839. offset = cs_offset;
  840. }
  841. cs = intel_ring_begin(rq, 2);
  842. if (IS_ERR(cs))
  843. return PTR_ERR(cs);
  844. *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
  845. *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
  846. MI_BATCH_NON_SECURE);
  847. intel_ring_advance(rq, cs);
  848. return 0;
  849. }
  850. static int
  851. i915_emit_bb_start(struct i915_request *rq,
  852. u64 offset, u32 len,
  853. unsigned int dispatch_flags)
  854. {
  855. u32 *cs;
  856. cs = intel_ring_begin(rq, 2);
  857. if (IS_ERR(cs))
  858. return PTR_ERR(cs);
  859. *cs++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
  860. *cs++ = offset | (dispatch_flags & I915_DISPATCH_SECURE ? 0 :
  861. MI_BATCH_NON_SECURE);
  862. intel_ring_advance(rq, cs);
  863. return 0;
  864. }
  865. int intel_ring_pin(struct intel_ring *ring,
  866. struct drm_i915_private *i915,
  867. unsigned int offset_bias)
  868. {
  869. enum i915_map_type map = HAS_LLC(i915) ? I915_MAP_WB : I915_MAP_WC;
  870. struct i915_vma *vma = ring->vma;
  871. unsigned int flags;
  872. void *addr;
  873. int ret;
  874. GEM_BUG_ON(ring->vaddr);
  875. flags = PIN_GLOBAL;
  876. if (offset_bias)
  877. flags |= PIN_OFFSET_BIAS | offset_bias;
  878. if (vma->obj->stolen)
  879. flags |= PIN_MAPPABLE;
  880. if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
  881. if (flags & PIN_MAPPABLE || map == I915_MAP_WC)
  882. ret = i915_gem_object_set_to_gtt_domain(vma->obj, true);
  883. else
  884. ret = i915_gem_object_set_to_cpu_domain(vma->obj, true);
  885. if (unlikely(ret))
  886. return ret;
  887. }
  888. ret = i915_vma_pin(vma, 0, PAGE_SIZE, flags);
  889. if (unlikely(ret))
  890. return ret;
  891. if (i915_vma_is_map_and_fenceable(vma))
  892. addr = (void __force *)i915_vma_pin_iomap(vma);
  893. else
  894. addr = i915_gem_object_pin_map(vma->obj, map);
  895. if (IS_ERR(addr))
  896. goto err;
  897. vma->obj->pin_global++;
  898. ring->vaddr = addr;
  899. return 0;
  900. err:
  901. i915_vma_unpin(vma);
  902. return PTR_ERR(addr);
  903. }
  904. void intel_ring_reset(struct intel_ring *ring, u32 tail)
  905. {
  906. ring->tail = tail;
  907. ring->head = tail;
  908. ring->emit = tail;
  909. intel_ring_update_space(ring);
  910. }
  911. void intel_ring_unpin(struct intel_ring *ring)
  912. {
  913. GEM_BUG_ON(!ring->vma);
  914. GEM_BUG_ON(!ring->vaddr);
  915. /* Discard any unused bytes beyond that submitted to hw. */
  916. intel_ring_reset(ring, ring->tail);
  917. if (i915_vma_is_map_and_fenceable(ring->vma))
  918. i915_vma_unpin_iomap(ring->vma);
  919. else
  920. i915_gem_object_unpin_map(ring->vma->obj);
  921. ring->vaddr = NULL;
  922. ring->vma->obj->pin_global--;
  923. i915_vma_unpin(ring->vma);
  924. }
  925. static struct i915_vma *
  926. intel_ring_create_vma(struct drm_i915_private *dev_priv, int size)
  927. {
  928. struct drm_i915_gem_object *obj;
  929. struct i915_vma *vma;
  930. obj = i915_gem_object_create_stolen(dev_priv, size);
  931. if (!obj)
  932. obj = i915_gem_object_create_internal(dev_priv, size);
  933. if (IS_ERR(obj))
  934. return ERR_CAST(obj);
  935. /* mark ring buffers as read-only from GPU side by default */
  936. obj->gt_ro = 1;
  937. vma = i915_vma_instance(obj, &dev_priv->ggtt.vm, NULL);
  938. if (IS_ERR(vma))
  939. goto err;
  940. return vma;
  941. err:
  942. i915_gem_object_put(obj);
  943. return vma;
  944. }
  945. struct intel_ring *
  946. intel_engine_create_ring(struct intel_engine_cs *engine,
  947. struct i915_timeline *timeline,
  948. int size)
  949. {
  950. struct intel_ring *ring;
  951. struct i915_vma *vma;
  952. GEM_BUG_ON(!is_power_of_2(size));
  953. GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
  954. GEM_BUG_ON(timeline == &engine->timeline);
  955. lockdep_assert_held(&engine->i915->drm.struct_mutex);
  956. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  957. if (!ring)
  958. return ERR_PTR(-ENOMEM);
  959. INIT_LIST_HEAD(&ring->request_list);
  960. ring->timeline = i915_timeline_get(timeline);
  961. ring->size = size;
  962. /* Workaround an erratum on the i830 which causes a hang if
  963. * the TAIL pointer points to within the last 2 cachelines
  964. * of the buffer.
  965. */
  966. ring->effective_size = size;
  967. if (IS_I830(engine->i915) || IS_I845G(engine->i915))
  968. ring->effective_size -= 2 * CACHELINE_BYTES;
  969. intel_ring_update_space(ring);
  970. vma = intel_ring_create_vma(engine->i915, size);
  971. if (IS_ERR(vma)) {
  972. kfree(ring);
  973. return ERR_CAST(vma);
  974. }
  975. ring->vma = vma;
  976. return ring;
  977. }
  978. void
  979. intel_ring_free(struct intel_ring *ring)
  980. {
  981. struct drm_i915_gem_object *obj = ring->vma->obj;
  982. i915_vma_close(ring->vma);
  983. __i915_gem_object_release_unless_active(obj);
  984. i915_timeline_put(ring->timeline);
  985. kfree(ring);
  986. }
  987. static void intel_ring_context_destroy(struct intel_context *ce)
  988. {
  989. GEM_BUG_ON(ce->pin_count);
  990. if (ce->state)
  991. __i915_gem_object_release_unless_active(ce->state->obj);
  992. }
  993. static int __context_pin(struct intel_context *ce)
  994. {
  995. struct i915_vma *vma;
  996. int err;
  997. vma = ce->state;
  998. if (!vma)
  999. return 0;
  1000. /*
  1001. * Clear this page out of any CPU caches for coherent swap-in/out.
  1002. * We only want to do this on the first bind so that we do not stall
  1003. * on an active context (which by nature is already on the GPU).
  1004. */
  1005. if (!(vma->flags & I915_VMA_GLOBAL_BIND)) {
  1006. err = i915_gem_object_set_to_gtt_domain(vma->obj, true);
  1007. if (err)
  1008. return err;
  1009. }
  1010. err = i915_vma_pin(vma, 0, I915_GTT_MIN_ALIGNMENT,
  1011. PIN_GLOBAL | PIN_HIGH);
  1012. if (err)
  1013. return err;
  1014. /*
  1015. * And mark is as a globally pinned object to let the shrinker know
  1016. * it cannot reclaim the object until we release it.
  1017. */
  1018. vma->obj->pin_global++;
  1019. return 0;
  1020. }
  1021. static void __context_unpin(struct intel_context *ce)
  1022. {
  1023. struct i915_vma *vma;
  1024. vma = ce->state;
  1025. if (!vma)
  1026. return;
  1027. vma->obj->pin_global--;
  1028. i915_vma_unpin(vma);
  1029. }
  1030. static void intel_ring_context_unpin(struct intel_context *ce)
  1031. {
  1032. __context_unpin(ce);
  1033. i915_gem_context_put(ce->gem_context);
  1034. }
  1035. static struct i915_vma *
  1036. alloc_context_vma(struct intel_engine_cs *engine)
  1037. {
  1038. struct drm_i915_private *i915 = engine->i915;
  1039. struct drm_i915_gem_object *obj;
  1040. struct i915_vma *vma;
  1041. int err;
  1042. obj = i915_gem_object_create(i915, engine->context_size);
  1043. if (IS_ERR(obj))
  1044. return ERR_CAST(obj);
  1045. if (engine->default_state) {
  1046. void *defaults, *vaddr;
  1047. vaddr = i915_gem_object_pin_map(obj, I915_MAP_WB);
  1048. if (IS_ERR(vaddr)) {
  1049. err = PTR_ERR(vaddr);
  1050. goto err_obj;
  1051. }
  1052. defaults = i915_gem_object_pin_map(engine->default_state,
  1053. I915_MAP_WB);
  1054. if (IS_ERR(defaults)) {
  1055. err = PTR_ERR(defaults);
  1056. goto err_map;
  1057. }
  1058. memcpy(vaddr, defaults, engine->context_size);
  1059. i915_gem_object_unpin_map(engine->default_state);
  1060. i915_gem_object_unpin_map(obj);
  1061. }
  1062. /*
  1063. * Try to make the context utilize L3 as well as LLC.
  1064. *
  1065. * On VLV we don't have L3 controls in the PTEs so we
  1066. * shouldn't touch the cache level, especially as that
  1067. * would make the object snooped which might have a
  1068. * negative performance impact.
  1069. *
  1070. * Snooping is required on non-llc platforms in execlist
  1071. * mode, but since all GGTT accesses use PAT entry 0 we
  1072. * get snooping anyway regardless of cache_level.
  1073. *
  1074. * This is only applicable for Ivy Bridge devices since
  1075. * later platforms don't have L3 control bits in the PTE.
  1076. */
  1077. if (IS_IVYBRIDGE(i915)) {
  1078. /* Ignore any error, regard it as a simple optimisation */
  1079. i915_gem_object_set_cache_level(obj, I915_CACHE_L3_LLC);
  1080. }
  1081. vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
  1082. if (IS_ERR(vma)) {
  1083. err = PTR_ERR(vma);
  1084. goto err_obj;
  1085. }
  1086. return vma;
  1087. err_map:
  1088. i915_gem_object_unpin_map(obj);
  1089. err_obj:
  1090. i915_gem_object_put(obj);
  1091. return ERR_PTR(err);
  1092. }
  1093. static struct intel_context *
  1094. __ring_context_pin(struct intel_engine_cs *engine,
  1095. struct i915_gem_context *ctx,
  1096. struct intel_context *ce)
  1097. {
  1098. int err;
  1099. if (!ce->state && engine->context_size) {
  1100. struct i915_vma *vma;
  1101. vma = alloc_context_vma(engine);
  1102. if (IS_ERR(vma)) {
  1103. err = PTR_ERR(vma);
  1104. goto err;
  1105. }
  1106. ce->state = vma;
  1107. }
  1108. err = __context_pin(ce);
  1109. if (err)
  1110. goto err;
  1111. i915_gem_context_get(ctx);
  1112. /* One ringbuffer to rule them all */
  1113. GEM_BUG_ON(!engine->buffer);
  1114. ce->ring = engine->buffer;
  1115. return ce;
  1116. err:
  1117. ce->pin_count = 0;
  1118. return ERR_PTR(err);
  1119. }
  1120. static const struct intel_context_ops ring_context_ops = {
  1121. .unpin = intel_ring_context_unpin,
  1122. .destroy = intel_ring_context_destroy,
  1123. };
  1124. static struct intel_context *
  1125. intel_ring_context_pin(struct intel_engine_cs *engine,
  1126. struct i915_gem_context *ctx)
  1127. {
  1128. struct intel_context *ce = to_intel_context(ctx, engine);
  1129. lockdep_assert_held(&ctx->i915->drm.struct_mutex);
  1130. if (likely(ce->pin_count++))
  1131. return ce;
  1132. GEM_BUG_ON(!ce->pin_count); /* no overflow please! */
  1133. ce->ops = &ring_context_ops;
  1134. return __ring_context_pin(engine, ctx, ce);
  1135. }
  1136. static int intel_init_ring_buffer(struct intel_engine_cs *engine)
  1137. {
  1138. struct intel_ring *ring;
  1139. struct i915_timeline *timeline;
  1140. int err;
  1141. intel_engine_setup_common(engine);
  1142. timeline = i915_timeline_create(engine->i915, engine->name);
  1143. if (IS_ERR(timeline)) {
  1144. err = PTR_ERR(timeline);
  1145. goto err;
  1146. }
  1147. ring = intel_engine_create_ring(engine, timeline, 32 * PAGE_SIZE);
  1148. i915_timeline_put(timeline);
  1149. if (IS_ERR(ring)) {
  1150. err = PTR_ERR(ring);
  1151. goto err;
  1152. }
  1153. /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
  1154. err = intel_ring_pin(ring, engine->i915, I915_GTT_PAGE_SIZE);
  1155. if (err)
  1156. goto err_ring;
  1157. GEM_BUG_ON(engine->buffer);
  1158. engine->buffer = ring;
  1159. err = intel_engine_init_common(engine);
  1160. if (err)
  1161. goto err_unpin;
  1162. return 0;
  1163. err_unpin:
  1164. intel_ring_unpin(ring);
  1165. err_ring:
  1166. intel_ring_free(ring);
  1167. err:
  1168. intel_engine_cleanup_common(engine);
  1169. return err;
  1170. }
  1171. void intel_engine_cleanup(struct intel_engine_cs *engine)
  1172. {
  1173. struct drm_i915_private *dev_priv = engine->i915;
  1174. WARN_ON(INTEL_GEN(dev_priv) > 2 &&
  1175. (I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1176. intel_ring_unpin(engine->buffer);
  1177. intel_ring_free(engine->buffer);
  1178. if (engine->cleanup)
  1179. engine->cleanup(engine);
  1180. intel_engine_cleanup_common(engine);
  1181. dev_priv->engine[engine->id] = NULL;
  1182. kfree(engine);
  1183. }
  1184. void intel_legacy_submission_resume(struct drm_i915_private *dev_priv)
  1185. {
  1186. struct intel_engine_cs *engine;
  1187. enum intel_engine_id id;
  1188. /* Restart from the beginning of the rings for convenience */
  1189. for_each_engine(engine, dev_priv, id)
  1190. intel_ring_reset(engine->buffer, 0);
  1191. }
  1192. static inline int mi_set_context(struct i915_request *rq, u32 flags)
  1193. {
  1194. struct drm_i915_private *i915 = rq->i915;
  1195. struct intel_engine_cs *engine = rq->engine;
  1196. enum intel_engine_id id;
  1197. const int num_rings =
  1198. /* Use an extended w/a on gen7 if signalling from other rings */
  1199. (HAS_LEGACY_SEMAPHORES(i915) && IS_GEN7(i915)) ?
  1200. INTEL_INFO(i915)->num_rings - 1 :
  1201. 0;
  1202. bool force_restore = false;
  1203. int len;
  1204. u32 *cs;
  1205. flags |= MI_MM_SPACE_GTT;
  1206. if (IS_HASWELL(i915))
  1207. /* These flags are for resource streamer on HSW+ */
  1208. flags |= HSW_MI_RS_SAVE_STATE_EN | HSW_MI_RS_RESTORE_STATE_EN;
  1209. else
  1210. flags |= MI_SAVE_EXT_STATE_EN | MI_RESTORE_EXT_STATE_EN;
  1211. len = 4;
  1212. if (IS_GEN7(i915))
  1213. len += 2 + (num_rings ? 4*num_rings + 6 : 0);
  1214. if (flags & MI_FORCE_RESTORE) {
  1215. GEM_BUG_ON(flags & MI_RESTORE_INHIBIT);
  1216. flags &= ~MI_FORCE_RESTORE;
  1217. force_restore = true;
  1218. len += 2;
  1219. }
  1220. cs = intel_ring_begin(rq, len);
  1221. if (IS_ERR(cs))
  1222. return PTR_ERR(cs);
  1223. /* WaProgramMiArbOnOffAroundMiSetContext:ivb,vlv,hsw,bdw,chv */
  1224. if (IS_GEN7(i915)) {
  1225. *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
  1226. if (num_rings) {
  1227. struct intel_engine_cs *signaller;
  1228. *cs++ = MI_LOAD_REGISTER_IMM(num_rings);
  1229. for_each_engine(signaller, i915, id) {
  1230. if (signaller == engine)
  1231. continue;
  1232. *cs++ = i915_mmio_reg_offset(
  1233. RING_PSMI_CTL(signaller->mmio_base));
  1234. *cs++ = _MASKED_BIT_ENABLE(
  1235. GEN6_PSMI_SLEEP_MSG_DISABLE);
  1236. }
  1237. }
  1238. }
  1239. if (force_restore) {
  1240. /*
  1241. * The HW doesn't handle being told to restore the current
  1242. * context very well. Quite often it likes goes to go off and
  1243. * sulk, especially when it is meant to be reloading PP_DIR.
  1244. * A very simple fix to force the reload is to simply switch
  1245. * away from the current context and back again.
  1246. *
  1247. * Note that the kernel_context will contain random state
  1248. * following the INHIBIT_RESTORE. We accept this since we
  1249. * never use the kernel_context state; it is merely a
  1250. * placeholder we use to flush other contexts.
  1251. */
  1252. *cs++ = MI_SET_CONTEXT;
  1253. *cs++ = i915_ggtt_offset(to_intel_context(i915->kernel_context,
  1254. engine)->state) |
  1255. MI_MM_SPACE_GTT |
  1256. MI_RESTORE_INHIBIT;
  1257. }
  1258. *cs++ = MI_NOOP;
  1259. *cs++ = MI_SET_CONTEXT;
  1260. *cs++ = i915_ggtt_offset(rq->hw_context->state) | flags;
  1261. /*
  1262. * w/a: MI_SET_CONTEXT must always be followed by MI_NOOP
  1263. * WaMiSetContext_Hang:snb,ivb,vlv
  1264. */
  1265. *cs++ = MI_NOOP;
  1266. if (IS_GEN7(i915)) {
  1267. if (num_rings) {
  1268. struct intel_engine_cs *signaller;
  1269. i915_reg_t last_reg = {}; /* keep gcc quiet */
  1270. *cs++ = MI_LOAD_REGISTER_IMM(num_rings);
  1271. for_each_engine(signaller, i915, id) {
  1272. if (signaller == engine)
  1273. continue;
  1274. last_reg = RING_PSMI_CTL(signaller->mmio_base);
  1275. *cs++ = i915_mmio_reg_offset(last_reg);
  1276. *cs++ = _MASKED_BIT_DISABLE(
  1277. GEN6_PSMI_SLEEP_MSG_DISABLE);
  1278. }
  1279. /* Insert a delay before the next switch! */
  1280. *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  1281. *cs++ = i915_mmio_reg_offset(last_reg);
  1282. *cs++ = i915_ggtt_offset(engine->scratch);
  1283. *cs++ = MI_NOOP;
  1284. }
  1285. *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
  1286. }
  1287. intel_ring_advance(rq, cs);
  1288. return 0;
  1289. }
  1290. static int remap_l3(struct i915_request *rq, int slice)
  1291. {
  1292. u32 *cs, *remap_info = rq->i915->l3_parity.remap_info[slice];
  1293. int i;
  1294. if (!remap_info)
  1295. return 0;
  1296. cs = intel_ring_begin(rq, GEN7_L3LOG_SIZE/4 * 2 + 2);
  1297. if (IS_ERR(cs))
  1298. return PTR_ERR(cs);
  1299. /*
  1300. * Note: We do not worry about the concurrent register cacheline hang
  1301. * here because no other code should access these registers other than
  1302. * at initialization time.
  1303. */
  1304. *cs++ = MI_LOAD_REGISTER_IMM(GEN7_L3LOG_SIZE/4);
  1305. for (i = 0; i < GEN7_L3LOG_SIZE/4; i++) {
  1306. *cs++ = i915_mmio_reg_offset(GEN7_L3LOG(slice, i));
  1307. *cs++ = remap_info[i];
  1308. }
  1309. *cs++ = MI_NOOP;
  1310. intel_ring_advance(rq, cs);
  1311. return 0;
  1312. }
  1313. static int switch_context(struct i915_request *rq)
  1314. {
  1315. struct intel_engine_cs *engine = rq->engine;
  1316. struct i915_gem_context *to_ctx = rq->gem_context;
  1317. struct i915_hw_ppgtt *to_mm =
  1318. to_ctx->ppgtt ?: rq->i915->mm.aliasing_ppgtt;
  1319. struct i915_gem_context *from_ctx = engine->legacy_active_context;
  1320. struct i915_hw_ppgtt *from_mm = engine->legacy_active_ppgtt;
  1321. u32 hw_flags = 0;
  1322. int ret, i;
  1323. lockdep_assert_held(&rq->i915->drm.struct_mutex);
  1324. GEM_BUG_ON(HAS_EXECLISTS(rq->i915));
  1325. if (to_mm != from_mm ||
  1326. (to_mm && intel_engine_flag(engine) & to_mm->pd_dirty_rings)) {
  1327. trace_switch_mm(engine, to_ctx);
  1328. ret = to_mm->switch_mm(to_mm, rq);
  1329. if (ret)
  1330. goto err;
  1331. to_mm->pd_dirty_rings &= ~intel_engine_flag(engine);
  1332. engine->legacy_active_ppgtt = to_mm;
  1333. hw_flags = MI_FORCE_RESTORE;
  1334. }
  1335. if (rq->hw_context->state &&
  1336. (to_ctx != from_ctx || hw_flags & MI_FORCE_RESTORE)) {
  1337. GEM_BUG_ON(engine->id != RCS);
  1338. /*
  1339. * The kernel context(s) is treated as pure scratch and is not
  1340. * expected to retain any state (as we sacrifice it during
  1341. * suspend and on resume it may be corrupted). This is ok,
  1342. * as nothing actually executes using the kernel context; it
  1343. * is purely used for flushing user contexts.
  1344. */
  1345. if (i915_gem_context_is_kernel(to_ctx))
  1346. hw_flags = MI_RESTORE_INHIBIT;
  1347. ret = mi_set_context(rq, hw_flags);
  1348. if (ret)
  1349. goto err_mm;
  1350. engine->legacy_active_context = to_ctx;
  1351. }
  1352. if (to_ctx->remap_slice) {
  1353. for (i = 0; i < MAX_L3_SLICES; i++) {
  1354. if (!(to_ctx->remap_slice & BIT(i)))
  1355. continue;
  1356. ret = remap_l3(rq, i);
  1357. if (ret)
  1358. goto err_ctx;
  1359. }
  1360. to_ctx->remap_slice = 0;
  1361. }
  1362. return 0;
  1363. err_ctx:
  1364. engine->legacy_active_context = from_ctx;
  1365. err_mm:
  1366. engine->legacy_active_ppgtt = from_mm;
  1367. err:
  1368. return ret;
  1369. }
  1370. static int ring_request_alloc(struct i915_request *request)
  1371. {
  1372. int ret;
  1373. GEM_BUG_ON(!request->hw_context->pin_count);
  1374. /* Flush enough space to reduce the likelihood of waiting after
  1375. * we start building the request - in which case we will just
  1376. * have to repeat work.
  1377. */
  1378. request->reserved_space += LEGACY_REQUEST_SIZE;
  1379. ret = intel_ring_wait_for_space(request->ring, request->reserved_space);
  1380. if (ret)
  1381. return ret;
  1382. ret = switch_context(request);
  1383. if (ret)
  1384. return ret;
  1385. request->reserved_space -= LEGACY_REQUEST_SIZE;
  1386. return 0;
  1387. }
  1388. static noinline int wait_for_space(struct intel_ring *ring, unsigned int bytes)
  1389. {
  1390. struct i915_request *target;
  1391. long timeout;
  1392. lockdep_assert_held(&ring->vma->vm->i915->drm.struct_mutex);
  1393. if (intel_ring_update_space(ring) >= bytes)
  1394. return 0;
  1395. GEM_BUG_ON(list_empty(&ring->request_list));
  1396. list_for_each_entry(target, &ring->request_list, ring_link) {
  1397. /* Would completion of this request free enough space? */
  1398. if (bytes <= __intel_ring_space(target->postfix,
  1399. ring->emit, ring->size))
  1400. break;
  1401. }
  1402. if (WARN_ON(&target->ring_link == &ring->request_list))
  1403. return -ENOSPC;
  1404. timeout = i915_request_wait(target,
  1405. I915_WAIT_INTERRUPTIBLE | I915_WAIT_LOCKED,
  1406. MAX_SCHEDULE_TIMEOUT);
  1407. if (timeout < 0)
  1408. return timeout;
  1409. i915_request_retire_upto(target);
  1410. intel_ring_update_space(ring);
  1411. GEM_BUG_ON(ring->space < bytes);
  1412. return 0;
  1413. }
  1414. int intel_ring_wait_for_space(struct intel_ring *ring, unsigned int bytes)
  1415. {
  1416. GEM_BUG_ON(bytes > ring->effective_size);
  1417. if (unlikely(bytes > ring->effective_size - ring->emit))
  1418. bytes += ring->size - ring->emit;
  1419. if (unlikely(bytes > ring->space)) {
  1420. int ret = wait_for_space(ring, bytes);
  1421. if (unlikely(ret))
  1422. return ret;
  1423. }
  1424. GEM_BUG_ON(ring->space < bytes);
  1425. return 0;
  1426. }
  1427. u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords)
  1428. {
  1429. struct intel_ring *ring = rq->ring;
  1430. const unsigned int remain_usable = ring->effective_size - ring->emit;
  1431. const unsigned int bytes = num_dwords * sizeof(u32);
  1432. unsigned int need_wrap = 0;
  1433. unsigned int total_bytes;
  1434. u32 *cs;
  1435. /* Packets must be qword aligned. */
  1436. GEM_BUG_ON(num_dwords & 1);
  1437. total_bytes = bytes + rq->reserved_space;
  1438. GEM_BUG_ON(total_bytes > ring->effective_size);
  1439. if (unlikely(total_bytes > remain_usable)) {
  1440. const int remain_actual = ring->size - ring->emit;
  1441. if (bytes > remain_usable) {
  1442. /*
  1443. * Not enough space for the basic request. So need to
  1444. * flush out the remainder and then wait for
  1445. * base + reserved.
  1446. */
  1447. total_bytes += remain_actual;
  1448. need_wrap = remain_actual | 1;
  1449. } else {
  1450. /*
  1451. * The base request will fit but the reserved space
  1452. * falls off the end. So we don't need an immediate
  1453. * wrap and only need to effectively wait for the
  1454. * reserved size from the start of ringbuffer.
  1455. */
  1456. total_bytes = rq->reserved_space + remain_actual;
  1457. }
  1458. }
  1459. if (unlikely(total_bytes > ring->space)) {
  1460. int ret;
  1461. /*
  1462. * Space is reserved in the ringbuffer for finalising the
  1463. * request, as that cannot be allowed to fail. During request
  1464. * finalisation, reserved_space is set to 0 to stop the
  1465. * overallocation and the assumption is that then we never need
  1466. * to wait (which has the risk of failing with EINTR).
  1467. *
  1468. * See also i915_request_alloc() and i915_request_add().
  1469. */
  1470. GEM_BUG_ON(!rq->reserved_space);
  1471. ret = wait_for_space(ring, total_bytes);
  1472. if (unlikely(ret))
  1473. return ERR_PTR(ret);
  1474. }
  1475. if (unlikely(need_wrap)) {
  1476. need_wrap &= ~1;
  1477. GEM_BUG_ON(need_wrap > ring->space);
  1478. GEM_BUG_ON(ring->emit + need_wrap > ring->size);
  1479. GEM_BUG_ON(!IS_ALIGNED(need_wrap, sizeof(u64)));
  1480. /* Fill the tail with MI_NOOP */
  1481. memset64(ring->vaddr + ring->emit, 0, need_wrap / sizeof(u64));
  1482. ring->space -= need_wrap;
  1483. ring->emit = 0;
  1484. }
  1485. GEM_BUG_ON(ring->emit > ring->size - bytes);
  1486. GEM_BUG_ON(ring->space < bytes);
  1487. cs = ring->vaddr + ring->emit;
  1488. GEM_DEBUG_EXEC(memset32(cs, POISON_INUSE, bytes / sizeof(*cs)));
  1489. ring->emit += bytes;
  1490. ring->space -= bytes;
  1491. return cs;
  1492. }
  1493. /* Align the ring tail to a cacheline boundary */
  1494. int intel_ring_cacheline_align(struct i915_request *rq)
  1495. {
  1496. int num_dwords;
  1497. void *cs;
  1498. num_dwords = (rq->ring->emit & (CACHELINE_BYTES - 1)) / sizeof(u32);
  1499. if (num_dwords == 0)
  1500. return 0;
  1501. num_dwords = CACHELINE_DWORDS - num_dwords;
  1502. GEM_BUG_ON(num_dwords & 1);
  1503. cs = intel_ring_begin(rq, num_dwords);
  1504. if (IS_ERR(cs))
  1505. return PTR_ERR(cs);
  1506. memset64(cs, (u64)MI_NOOP << 32 | MI_NOOP, num_dwords / 2);
  1507. intel_ring_advance(rq, cs);
  1508. GEM_BUG_ON(rq->ring->emit & (CACHELINE_BYTES - 1));
  1509. return 0;
  1510. }
  1511. static void gen6_bsd_submit_request(struct i915_request *request)
  1512. {
  1513. struct drm_i915_private *dev_priv = request->i915;
  1514. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  1515. /* Every tail move must follow the sequence below */
  1516. /* Disable notification that the ring is IDLE. The GT
  1517. * will then assume that it is busy and bring it out of rc6.
  1518. */
  1519. I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1520. _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  1521. /* Clear the context id. Here be magic! */
  1522. I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
  1523. /* Wait for the ring not to be idle, i.e. for it to wake up. */
  1524. if (__intel_wait_for_register_fw(dev_priv,
  1525. GEN6_BSD_SLEEP_PSMI_CONTROL,
  1526. GEN6_BSD_SLEEP_INDICATOR,
  1527. 0,
  1528. 1000, 0, NULL))
  1529. DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
  1530. /* Now that the ring is fully powered up, update the tail */
  1531. i9xx_submit_request(request);
  1532. /* Let the ring send IDLE messages to the GT again,
  1533. * and so let it sleep to conserve power when idle.
  1534. */
  1535. I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1536. _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  1537. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  1538. }
  1539. static int gen6_bsd_ring_flush(struct i915_request *rq, u32 mode)
  1540. {
  1541. u32 cmd, *cs;
  1542. cs = intel_ring_begin(rq, 4);
  1543. if (IS_ERR(cs))
  1544. return PTR_ERR(cs);
  1545. cmd = MI_FLUSH_DW;
  1546. /* We always require a command barrier so that subsequent
  1547. * commands, such as breadcrumb interrupts, are strictly ordered
  1548. * wrt the contents of the write cache being flushed to memory
  1549. * (and thus being coherent from the CPU).
  1550. */
  1551. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1552. /*
  1553. * Bspec vol 1c.5 - video engine command streamer:
  1554. * "If ENABLED, all TLBs will be invalidated once the flush
  1555. * operation is complete. This bit is only valid when the
  1556. * Post-Sync Operation field is a value of 1h or 3h."
  1557. */
  1558. if (mode & EMIT_INVALIDATE)
  1559. cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
  1560. *cs++ = cmd;
  1561. *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
  1562. *cs++ = 0;
  1563. *cs++ = MI_NOOP;
  1564. intel_ring_advance(rq, cs);
  1565. return 0;
  1566. }
  1567. static int
  1568. hsw_emit_bb_start(struct i915_request *rq,
  1569. u64 offset, u32 len,
  1570. unsigned int dispatch_flags)
  1571. {
  1572. u32 *cs;
  1573. cs = intel_ring_begin(rq, 2);
  1574. if (IS_ERR(cs))
  1575. return PTR_ERR(cs);
  1576. *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
  1577. 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
  1578. (dispatch_flags & I915_DISPATCH_RS ?
  1579. MI_BATCH_RESOURCE_STREAMER : 0);
  1580. /* bit0-7 is the length on GEN6+ */
  1581. *cs++ = offset;
  1582. intel_ring_advance(rq, cs);
  1583. return 0;
  1584. }
  1585. static int
  1586. gen6_emit_bb_start(struct i915_request *rq,
  1587. u64 offset, u32 len,
  1588. unsigned int dispatch_flags)
  1589. {
  1590. u32 *cs;
  1591. cs = intel_ring_begin(rq, 2);
  1592. if (IS_ERR(cs))
  1593. return PTR_ERR(cs);
  1594. *cs++ = MI_BATCH_BUFFER_START | (dispatch_flags & I915_DISPATCH_SECURE ?
  1595. 0 : MI_BATCH_NON_SECURE_I965);
  1596. /* bit0-7 is the length on GEN6+ */
  1597. *cs++ = offset;
  1598. intel_ring_advance(rq, cs);
  1599. return 0;
  1600. }
  1601. /* Blitter support (SandyBridge+) */
  1602. static int gen6_ring_flush(struct i915_request *rq, u32 mode)
  1603. {
  1604. u32 cmd, *cs;
  1605. cs = intel_ring_begin(rq, 4);
  1606. if (IS_ERR(cs))
  1607. return PTR_ERR(cs);
  1608. cmd = MI_FLUSH_DW;
  1609. /* We always require a command barrier so that subsequent
  1610. * commands, such as breadcrumb interrupts, are strictly ordered
  1611. * wrt the contents of the write cache being flushed to memory
  1612. * (and thus being coherent from the CPU).
  1613. */
  1614. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1615. /*
  1616. * Bspec vol 1c.3 - blitter engine command streamer:
  1617. * "If ENABLED, all TLBs will be invalidated once the flush
  1618. * operation is complete. This bit is only valid when the
  1619. * Post-Sync Operation field is a value of 1h or 3h."
  1620. */
  1621. if (mode & EMIT_INVALIDATE)
  1622. cmd |= MI_INVALIDATE_TLB;
  1623. *cs++ = cmd;
  1624. *cs++ = I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT;
  1625. *cs++ = 0;
  1626. *cs++ = MI_NOOP;
  1627. intel_ring_advance(rq, cs);
  1628. return 0;
  1629. }
  1630. static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
  1631. struct intel_engine_cs *engine)
  1632. {
  1633. int i;
  1634. if (!HAS_LEGACY_SEMAPHORES(dev_priv))
  1635. return;
  1636. GEM_BUG_ON(INTEL_GEN(dev_priv) < 6);
  1637. engine->semaphore.sync_to = gen6_ring_sync_to;
  1638. engine->semaphore.signal = gen6_signal;
  1639. /*
  1640. * The current semaphore is only applied on pre-gen8
  1641. * platform. And there is no VCS2 ring on the pre-gen8
  1642. * platform. So the semaphore between RCS and VCS2 is
  1643. * initialized as INVALID.
  1644. */
  1645. for (i = 0; i < GEN6_NUM_SEMAPHORES; i++) {
  1646. static const struct {
  1647. u32 wait_mbox;
  1648. i915_reg_t mbox_reg;
  1649. } sem_data[GEN6_NUM_SEMAPHORES][GEN6_NUM_SEMAPHORES] = {
  1650. [RCS_HW] = {
  1651. [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
  1652. [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
  1653. [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
  1654. },
  1655. [VCS_HW] = {
  1656. [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
  1657. [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
  1658. [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
  1659. },
  1660. [BCS_HW] = {
  1661. [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
  1662. [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
  1663. [VECS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
  1664. },
  1665. [VECS_HW] = {
  1666. [RCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
  1667. [VCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
  1668. [BCS_HW] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
  1669. },
  1670. };
  1671. u32 wait_mbox;
  1672. i915_reg_t mbox_reg;
  1673. if (i == engine->hw_id) {
  1674. wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
  1675. mbox_reg = GEN6_NOSYNC;
  1676. } else {
  1677. wait_mbox = sem_data[engine->hw_id][i].wait_mbox;
  1678. mbox_reg = sem_data[engine->hw_id][i].mbox_reg;
  1679. }
  1680. engine->semaphore.mbox.wait[i] = wait_mbox;
  1681. engine->semaphore.mbox.signal[i] = mbox_reg;
  1682. }
  1683. }
  1684. static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
  1685. struct intel_engine_cs *engine)
  1686. {
  1687. if (INTEL_GEN(dev_priv) >= 6) {
  1688. engine->irq_enable = gen6_irq_enable;
  1689. engine->irq_disable = gen6_irq_disable;
  1690. engine->irq_seqno_barrier = gen6_seqno_barrier;
  1691. } else if (INTEL_GEN(dev_priv) >= 5) {
  1692. engine->irq_enable = gen5_irq_enable;
  1693. engine->irq_disable = gen5_irq_disable;
  1694. engine->irq_seqno_barrier = gen5_seqno_barrier;
  1695. } else if (INTEL_GEN(dev_priv) >= 3) {
  1696. engine->irq_enable = i9xx_irq_enable;
  1697. engine->irq_disable = i9xx_irq_disable;
  1698. } else {
  1699. engine->irq_enable = i8xx_irq_enable;
  1700. engine->irq_disable = i8xx_irq_disable;
  1701. }
  1702. }
  1703. static void i9xx_set_default_submission(struct intel_engine_cs *engine)
  1704. {
  1705. engine->submit_request = i9xx_submit_request;
  1706. engine->cancel_requests = cancel_requests;
  1707. engine->park = NULL;
  1708. engine->unpark = NULL;
  1709. }
  1710. static void gen6_bsd_set_default_submission(struct intel_engine_cs *engine)
  1711. {
  1712. i9xx_set_default_submission(engine);
  1713. engine->submit_request = gen6_bsd_submit_request;
  1714. }
  1715. static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
  1716. struct intel_engine_cs *engine)
  1717. {
  1718. /* gen8+ are only supported with execlists */
  1719. GEM_BUG_ON(INTEL_GEN(dev_priv) >= 8);
  1720. intel_ring_init_irq(dev_priv, engine);
  1721. intel_ring_init_semaphores(dev_priv, engine);
  1722. engine->init_hw = init_ring_common;
  1723. engine->reset.prepare = reset_prepare;
  1724. engine->reset.reset = reset_ring;
  1725. engine->reset.finish = reset_finish;
  1726. engine->context_pin = intel_ring_context_pin;
  1727. engine->request_alloc = ring_request_alloc;
  1728. engine->emit_breadcrumb = i9xx_emit_breadcrumb;
  1729. engine->emit_breadcrumb_sz = i9xx_emit_breadcrumb_sz;
  1730. if (HAS_LEGACY_SEMAPHORES(dev_priv)) {
  1731. int num_rings;
  1732. engine->emit_breadcrumb = gen6_sema_emit_breadcrumb;
  1733. num_rings = INTEL_INFO(dev_priv)->num_rings - 1;
  1734. engine->emit_breadcrumb_sz += num_rings * 3;
  1735. if (num_rings & 1)
  1736. engine->emit_breadcrumb_sz++;
  1737. }
  1738. engine->set_default_submission = i9xx_set_default_submission;
  1739. if (INTEL_GEN(dev_priv) >= 6)
  1740. engine->emit_bb_start = gen6_emit_bb_start;
  1741. else if (INTEL_GEN(dev_priv) >= 4)
  1742. engine->emit_bb_start = i965_emit_bb_start;
  1743. else if (IS_I830(dev_priv) || IS_I845G(dev_priv))
  1744. engine->emit_bb_start = i830_emit_bb_start;
  1745. else
  1746. engine->emit_bb_start = i915_emit_bb_start;
  1747. }
  1748. int intel_init_render_ring_buffer(struct intel_engine_cs *engine)
  1749. {
  1750. struct drm_i915_private *dev_priv = engine->i915;
  1751. int ret;
  1752. intel_ring_default_vfuncs(dev_priv, engine);
  1753. if (HAS_L3_DPF(dev_priv))
  1754. engine->irq_keep_mask = GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
  1755. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
  1756. if (INTEL_GEN(dev_priv) >= 6) {
  1757. engine->init_context = intel_rcs_ctx_init;
  1758. engine->emit_flush = gen7_render_ring_flush;
  1759. if (IS_GEN6(dev_priv))
  1760. engine->emit_flush = gen6_render_ring_flush;
  1761. } else if (IS_GEN5(dev_priv)) {
  1762. engine->emit_flush = gen4_render_ring_flush;
  1763. } else {
  1764. if (INTEL_GEN(dev_priv) < 4)
  1765. engine->emit_flush = gen2_render_ring_flush;
  1766. else
  1767. engine->emit_flush = gen4_render_ring_flush;
  1768. engine->irq_enable_mask = I915_USER_INTERRUPT;
  1769. }
  1770. if (IS_HASWELL(dev_priv))
  1771. engine->emit_bb_start = hsw_emit_bb_start;
  1772. engine->init_hw = init_render_ring;
  1773. ret = intel_init_ring_buffer(engine);
  1774. if (ret)
  1775. return ret;
  1776. if (INTEL_GEN(dev_priv) >= 6) {
  1777. ret = intel_engine_create_scratch(engine, PAGE_SIZE);
  1778. if (ret)
  1779. return ret;
  1780. } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
  1781. ret = intel_engine_create_scratch(engine, I830_WA_SIZE);
  1782. if (ret)
  1783. return ret;
  1784. }
  1785. return 0;
  1786. }
  1787. int intel_init_bsd_ring_buffer(struct intel_engine_cs *engine)
  1788. {
  1789. struct drm_i915_private *dev_priv = engine->i915;
  1790. intel_ring_default_vfuncs(dev_priv, engine);
  1791. if (INTEL_GEN(dev_priv) >= 6) {
  1792. /* gen6 bsd needs a special wa for tail updates */
  1793. if (IS_GEN6(dev_priv))
  1794. engine->set_default_submission = gen6_bsd_set_default_submission;
  1795. engine->emit_flush = gen6_bsd_ring_flush;
  1796. engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
  1797. } else {
  1798. engine->emit_flush = bsd_ring_flush;
  1799. if (IS_GEN5(dev_priv))
  1800. engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
  1801. else
  1802. engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
  1803. }
  1804. return intel_init_ring_buffer(engine);
  1805. }
  1806. int intel_init_blt_ring_buffer(struct intel_engine_cs *engine)
  1807. {
  1808. struct drm_i915_private *dev_priv = engine->i915;
  1809. intel_ring_default_vfuncs(dev_priv, engine);
  1810. engine->emit_flush = gen6_ring_flush;
  1811. engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
  1812. return intel_init_ring_buffer(engine);
  1813. }
  1814. int intel_init_vebox_ring_buffer(struct intel_engine_cs *engine)
  1815. {
  1816. struct drm_i915_private *dev_priv = engine->i915;
  1817. intel_ring_default_vfuncs(dev_priv, engine);
  1818. engine->emit_flush = gen6_ring_flush;
  1819. engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
  1820. engine->irq_enable = hsw_vebox_irq_enable;
  1821. engine->irq_disable = hsw_vebox_irq_disable;
  1822. return intel_init_ring_buffer(engine);
  1823. }