intel_ringbuffer.c 58 KB

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