intel_ringbuffer.c 58 KB

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