intel_ringbuffer.c 59 KB

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