intel_ringbuffer.c 56 KB

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