intel_ringbuffer.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  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 <drm/drmP.h>
  30. #include "i915_drv.h"
  31. #include <drm/i915_drm.h>
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. /* Early gen2 devices have a cacheline of just 32 bytes, using 64 is overkill,
  35. * but keeps the logic simple. Indeed, the whole purpose of this macro is just
  36. * to give some inclination as to some of the magic values used in the various
  37. * workarounds!
  38. */
  39. #define CACHELINE_BYTES 64
  40. static inline int __ring_space(int head, int tail, int size)
  41. {
  42. int space = head - (tail + I915_RING_FREE_SPACE);
  43. if (space < 0)
  44. space += size;
  45. return space;
  46. }
  47. static inline int ring_space(struct intel_ring_buffer *ring)
  48. {
  49. return __ring_space(ring->head & HEAD_ADDR, ring->tail, ring->size);
  50. }
  51. static bool intel_ring_stopped(struct intel_ring_buffer *ring)
  52. {
  53. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  54. return dev_priv->gpu_error.stop_rings & intel_ring_flag(ring);
  55. }
  56. void __intel_ring_advance(struct intel_ring_buffer *ring)
  57. {
  58. ring->tail &= ring->size - 1;
  59. if (intel_ring_stopped(ring))
  60. return;
  61. ring->write_tail(ring, ring->tail);
  62. }
  63. static int
  64. gen2_render_ring_flush(struct intel_ring_buffer *ring,
  65. u32 invalidate_domains,
  66. u32 flush_domains)
  67. {
  68. u32 cmd;
  69. int ret;
  70. cmd = MI_FLUSH;
  71. if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
  72. cmd |= MI_NO_WRITE_FLUSH;
  73. if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
  74. cmd |= MI_READ_FLUSH;
  75. ret = intel_ring_begin(ring, 2);
  76. if (ret)
  77. return ret;
  78. intel_ring_emit(ring, cmd);
  79. intel_ring_emit(ring, MI_NOOP);
  80. intel_ring_advance(ring);
  81. return 0;
  82. }
  83. static int
  84. gen4_render_ring_flush(struct intel_ring_buffer *ring,
  85. u32 invalidate_domains,
  86. u32 flush_domains)
  87. {
  88. struct drm_device *dev = ring->dev;
  89. u32 cmd;
  90. int ret;
  91. /*
  92. * read/write caches:
  93. *
  94. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  95. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  96. * also flushed at 2d versus 3d pipeline switches.
  97. *
  98. * read-only caches:
  99. *
  100. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  101. * MI_READ_FLUSH is set, and is always flushed on 965.
  102. *
  103. * I915_GEM_DOMAIN_COMMAND may not exist?
  104. *
  105. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  106. * invalidated when MI_EXE_FLUSH is set.
  107. *
  108. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  109. * invalidated with every MI_FLUSH.
  110. *
  111. * TLBs:
  112. *
  113. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  114. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  115. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  116. * are flushed at any MI_FLUSH.
  117. */
  118. cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  119. if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
  120. cmd &= ~MI_NO_WRITE_FLUSH;
  121. if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
  122. cmd |= MI_EXE_FLUSH;
  123. if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
  124. (IS_G4X(dev) || IS_GEN5(dev)))
  125. cmd |= MI_INVALIDATE_ISP;
  126. ret = intel_ring_begin(ring, 2);
  127. if (ret)
  128. return ret;
  129. intel_ring_emit(ring, cmd);
  130. intel_ring_emit(ring, MI_NOOP);
  131. intel_ring_advance(ring);
  132. return 0;
  133. }
  134. /**
  135. * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
  136. * implementing two workarounds on gen6. From section 1.4.7.1
  137. * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
  138. *
  139. * [DevSNB-C+{W/A}] Before any depth stall flush (including those
  140. * produced by non-pipelined state commands), software needs to first
  141. * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
  142. * 0.
  143. *
  144. * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
  145. * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
  146. *
  147. * And the workaround for these two requires this workaround first:
  148. *
  149. * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
  150. * BEFORE the pipe-control with a post-sync op and no write-cache
  151. * flushes.
  152. *
  153. * And this last workaround is tricky because of the requirements on
  154. * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
  155. * volume 2 part 1:
  156. *
  157. * "1 of the following must also be set:
  158. * - Render Target Cache Flush Enable ([12] of DW1)
  159. * - Depth Cache Flush Enable ([0] of DW1)
  160. * - Stall at Pixel Scoreboard ([1] of DW1)
  161. * - Depth Stall ([13] of DW1)
  162. * - Post-Sync Operation ([13] of DW1)
  163. * - Notify Enable ([8] of DW1)"
  164. *
  165. * The cache flushes require the workaround flush that triggered this
  166. * one, so we can't use it. Depth stall would trigger the same.
  167. * Post-sync nonzero is what triggered this second workaround, so we
  168. * can't use that one either. Notify enable is IRQs, which aren't
  169. * really our business. That leaves only stall at scoreboard.
  170. */
  171. static int
  172. intel_emit_post_sync_nonzero_flush(struct intel_ring_buffer *ring)
  173. {
  174. u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  175. int ret;
  176. ret = intel_ring_begin(ring, 6);
  177. if (ret)
  178. return ret;
  179. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
  180. intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
  181. PIPE_CONTROL_STALL_AT_SCOREBOARD);
  182. intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
  183. intel_ring_emit(ring, 0); /* low dword */
  184. intel_ring_emit(ring, 0); /* high dword */
  185. intel_ring_emit(ring, MI_NOOP);
  186. intel_ring_advance(ring);
  187. ret = intel_ring_begin(ring, 6);
  188. if (ret)
  189. return ret;
  190. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(5));
  191. intel_ring_emit(ring, PIPE_CONTROL_QW_WRITE);
  192. intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
  193. intel_ring_emit(ring, 0);
  194. intel_ring_emit(ring, 0);
  195. intel_ring_emit(ring, MI_NOOP);
  196. intel_ring_advance(ring);
  197. return 0;
  198. }
  199. static int
  200. gen6_render_ring_flush(struct intel_ring_buffer *ring,
  201. u32 invalidate_domains, u32 flush_domains)
  202. {
  203. u32 flags = 0;
  204. u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  205. int ret;
  206. /* Force SNB workarounds for PIPE_CONTROL flushes */
  207. ret = intel_emit_post_sync_nonzero_flush(ring);
  208. if (ret)
  209. return ret;
  210. /* Just flush everything. Experiments have shown that reducing the
  211. * number of bits based on the write domains has little performance
  212. * impact.
  213. */
  214. if (flush_domains) {
  215. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  216. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  217. /*
  218. * Ensure that any following seqno writes only happen
  219. * when the render cache is indeed flushed.
  220. */
  221. flags |= PIPE_CONTROL_CS_STALL;
  222. }
  223. if (invalidate_domains) {
  224. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  225. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  226. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  227. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  228. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  229. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  230. /*
  231. * TLB invalidate requires a post-sync write.
  232. */
  233. flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
  234. }
  235. ret = intel_ring_begin(ring, 4);
  236. if (ret)
  237. return ret;
  238. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
  239. intel_ring_emit(ring, flags);
  240. intel_ring_emit(ring, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
  241. intel_ring_emit(ring, 0);
  242. intel_ring_advance(ring);
  243. return 0;
  244. }
  245. static int
  246. gen7_render_ring_cs_stall_wa(struct intel_ring_buffer *ring)
  247. {
  248. int ret;
  249. ret = intel_ring_begin(ring, 4);
  250. if (ret)
  251. return ret;
  252. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
  253. intel_ring_emit(ring, PIPE_CONTROL_CS_STALL |
  254. PIPE_CONTROL_STALL_AT_SCOREBOARD);
  255. intel_ring_emit(ring, 0);
  256. intel_ring_emit(ring, 0);
  257. intel_ring_advance(ring);
  258. return 0;
  259. }
  260. static int gen7_ring_fbc_flush(struct intel_ring_buffer *ring, u32 value)
  261. {
  262. int ret;
  263. if (!ring->fbc_dirty)
  264. return 0;
  265. ret = intel_ring_begin(ring, 6);
  266. if (ret)
  267. return ret;
  268. /* WaFbcNukeOn3DBlt:ivb/hsw */
  269. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  270. intel_ring_emit(ring, MSG_FBC_REND_STATE);
  271. intel_ring_emit(ring, value);
  272. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) | MI_SRM_LRM_GLOBAL_GTT);
  273. intel_ring_emit(ring, MSG_FBC_REND_STATE);
  274. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  275. intel_ring_advance(ring);
  276. ring->fbc_dirty = false;
  277. return 0;
  278. }
  279. static int
  280. gen7_render_ring_flush(struct intel_ring_buffer *ring,
  281. u32 invalidate_domains, u32 flush_domains)
  282. {
  283. u32 flags = 0;
  284. u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  285. int ret;
  286. /*
  287. * Ensure that any following seqno writes only happen when the render
  288. * cache is indeed flushed.
  289. *
  290. * Workaround: 4th PIPE_CONTROL command (except the ones with only
  291. * read-cache invalidate bits set) must have the CS_STALL bit set. We
  292. * don't try to be clever and just set it unconditionally.
  293. */
  294. flags |= PIPE_CONTROL_CS_STALL;
  295. /* Just flush everything. Experiments have shown that reducing the
  296. * number of bits based on the write domains has little performance
  297. * impact.
  298. */
  299. if (flush_domains) {
  300. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  301. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  302. }
  303. if (invalidate_domains) {
  304. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  305. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  306. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  307. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  308. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  309. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  310. /*
  311. * TLB invalidate requires a post-sync write.
  312. */
  313. flags |= PIPE_CONTROL_QW_WRITE;
  314. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  315. /* Workaround: we must issue a pipe_control with CS-stall bit
  316. * set before a pipe_control command that has the state cache
  317. * invalidate bit set. */
  318. gen7_render_ring_cs_stall_wa(ring);
  319. }
  320. ret = intel_ring_begin(ring, 4);
  321. if (ret)
  322. return ret;
  323. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4));
  324. intel_ring_emit(ring, flags);
  325. intel_ring_emit(ring, scratch_addr);
  326. intel_ring_emit(ring, 0);
  327. intel_ring_advance(ring);
  328. if (!invalidate_domains && flush_domains)
  329. return gen7_ring_fbc_flush(ring, FBC_REND_NUKE);
  330. return 0;
  331. }
  332. static int
  333. gen8_render_ring_flush(struct intel_ring_buffer *ring,
  334. u32 invalidate_domains, u32 flush_domains)
  335. {
  336. u32 flags = 0;
  337. u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  338. int ret;
  339. flags |= PIPE_CONTROL_CS_STALL;
  340. if (flush_domains) {
  341. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  342. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  343. }
  344. if (invalidate_domains) {
  345. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  346. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  347. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  348. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  349. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  350. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  351. flags |= PIPE_CONTROL_QW_WRITE;
  352. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  353. }
  354. ret = intel_ring_begin(ring, 6);
  355. if (ret)
  356. return ret;
  357. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(6));
  358. intel_ring_emit(ring, flags);
  359. intel_ring_emit(ring, scratch_addr);
  360. intel_ring_emit(ring, 0);
  361. intel_ring_emit(ring, 0);
  362. intel_ring_emit(ring, 0);
  363. intel_ring_advance(ring);
  364. return 0;
  365. }
  366. static void ring_write_tail(struct intel_ring_buffer *ring,
  367. u32 value)
  368. {
  369. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  370. I915_WRITE_TAIL(ring, value);
  371. }
  372. u64 intel_ring_get_active_head(struct intel_ring_buffer *ring)
  373. {
  374. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  375. u64 acthd;
  376. if (INTEL_INFO(ring->dev)->gen >= 8)
  377. acthd = I915_READ64_2x32(RING_ACTHD(ring->mmio_base),
  378. RING_ACTHD_UDW(ring->mmio_base));
  379. else if (INTEL_INFO(ring->dev)->gen >= 4)
  380. acthd = I915_READ(RING_ACTHD(ring->mmio_base));
  381. else
  382. acthd = I915_READ(ACTHD);
  383. return acthd;
  384. }
  385. static void ring_setup_phys_status_page(struct intel_ring_buffer *ring)
  386. {
  387. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  388. u32 addr;
  389. addr = dev_priv->status_page_dmah->busaddr;
  390. if (INTEL_INFO(ring->dev)->gen >= 4)
  391. addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
  392. I915_WRITE(HWS_PGA, addr);
  393. }
  394. static bool stop_ring(struct intel_ring_buffer *ring)
  395. {
  396. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  397. if (!IS_GEN2(ring->dev)) {
  398. I915_WRITE_MODE(ring, _MASKED_BIT_ENABLE(STOP_RING));
  399. if (wait_for_atomic((I915_READ_MODE(ring) & MODE_IDLE) != 0, 1000)) {
  400. DRM_ERROR("%s :timed out trying to stop ring\n", ring->name);
  401. return false;
  402. }
  403. }
  404. I915_WRITE_CTL(ring, 0);
  405. I915_WRITE_HEAD(ring, 0);
  406. ring->write_tail(ring, 0);
  407. if (!IS_GEN2(ring->dev)) {
  408. (void)I915_READ_CTL(ring);
  409. I915_WRITE_MODE(ring, _MASKED_BIT_DISABLE(STOP_RING));
  410. }
  411. return (I915_READ_HEAD(ring) & HEAD_ADDR) == 0;
  412. }
  413. static int init_ring_common(struct intel_ring_buffer *ring)
  414. {
  415. struct drm_device *dev = ring->dev;
  416. struct drm_i915_private *dev_priv = dev->dev_private;
  417. struct drm_i915_gem_object *obj = ring->obj;
  418. int ret = 0;
  419. gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
  420. if (!stop_ring(ring)) {
  421. /* G45 ring initialization often fails to reset head to zero */
  422. DRM_DEBUG_KMS("%s head not reset to zero "
  423. "ctl %08x head %08x tail %08x start %08x\n",
  424. ring->name,
  425. I915_READ_CTL(ring),
  426. I915_READ_HEAD(ring),
  427. I915_READ_TAIL(ring),
  428. I915_READ_START(ring));
  429. if (!stop_ring(ring)) {
  430. DRM_ERROR("failed to set %s head to zero "
  431. "ctl %08x head %08x tail %08x start %08x\n",
  432. ring->name,
  433. I915_READ_CTL(ring),
  434. I915_READ_HEAD(ring),
  435. I915_READ_TAIL(ring),
  436. I915_READ_START(ring));
  437. ret = -EIO;
  438. goto out;
  439. }
  440. }
  441. if (I915_NEED_GFX_HWS(dev))
  442. intel_ring_setup_status_page(ring);
  443. else
  444. ring_setup_phys_status_page(ring);
  445. /* Initialize the ring. This must happen _after_ we've cleared the ring
  446. * registers with the above sequence (the readback of the HEAD registers
  447. * also enforces ordering), otherwise the hw might lose the new ring
  448. * register values. */
  449. I915_WRITE_START(ring, i915_gem_obj_ggtt_offset(obj));
  450. I915_WRITE_CTL(ring,
  451. ((ring->size - PAGE_SIZE) & RING_NR_PAGES)
  452. | RING_VALID);
  453. /* If the head is still not zero, the ring is dead */
  454. if (wait_for((I915_READ_CTL(ring) & RING_VALID) != 0 &&
  455. I915_READ_START(ring) == i915_gem_obj_ggtt_offset(obj) &&
  456. (I915_READ_HEAD(ring) & HEAD_ADDR) == 0, 50)) {
  457. DRM_ERROR("%s initialization failed "
  458. "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
  459. ring->name,
  460. I915_READ_CTL(ring), I915_READ_CTL(ring) & RING_VALID,
  461. I915_READ_HEAD(ring), I915_READ_TAIL(ring),
  462. I915_READ_START(ring), (unsigned long)i915_gem_obj_ggtt_offset(obj));
  463. ret = -EIO;
  464. goto out;
  465. }
  466. if (!drm_core_check_feature(ring->dev, DRIVER_MODESET))
  467. i915_kernel_lost_context(ring->dev);
  468. else {
  469. ring->head = I915_READ_HEAD(ring);
  470. ring->tail = I915_READ_TAIL(ring) & TAIL_ADDR;
  471. ring->space = ring_space(ring);
  472. ring->last_retired_head = -1;
  473. }
  474. memset(&ring->hangcheck, 0, sizeof(ring->hangcheck));
  475. out:
  476. gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
  477. return ret;
  478. }
  479. static int
  480. init_pipe_control(struct intel_ring_buffer *ring)
  481. {
  482. int ret;
  483. if (ring->scratch.obj)
  484. return 0;
  485. ring->scratch.obj = i915_gem_alloc_object(ring->dev, 4096);
  486. if (ring->scratch.obj == NULL) {
  487. DRM_ERROR("Failed to allocate seqno page\n");
  488. ret = -ENOMEM;
  489. goto err;
  490. }
  491. ret = i915_gem_object_set_cache_level(ring->scratch.obj, I915_CACHE_LLC);
  492. if (ret)
  493. goto err_unref;
  494. ret = i915_gem_obj_ggtt_pin(ring->scratch.obj, 4096, 0);
  495. if (ret)
  496. goto err_unref;
  497. ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(ring->scratch.obj);
  498. ring->scratch.cpu_page = kmap(sg_page(ring->scratch.obj->pages->sgl));
  499. if (ring->scratch.cpu_page == NULL) {
  500. ret = -ENOMEM;
  501. goto err_unpin;
  502. }
  503. DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
  504. ring->name, ring->scratch.gtt_offset);
  505. return 0;
  506. err_unpin:
  507. i915_gem_object_ggtt_unpin(ring->scratch.obj);
  508. err_unref:
  509. drm_gem_object_unreference(&ring->scratch.obj->base);
  510. err:
  511. return ret;
  512. }
  513. static int init_render_ring(struct intel_ring_buffer *ring)
  514. {
  515. struct drm_device *dev = ring->dev;
  516. struct drm_i915_private *dev_priv = dev->dev_private;
  517. int ret = init_ring_common(ring);
  518. /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
  519. if (INTEL_INFO(dev)->gen >= 4 && INTEL_INFO(dev)->gen < 7)
  520. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
  521. /* We need to disable the AsyncFlip performance optimisations in order
  522. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  523. * programmed to '1' on all products.
  524. *
  525. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv,bdw
  526. */
  527. if (INTEL_INFO(dev)->gen >= 6)
  528. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  529. /* Required for the hardware to program scanline values for waiting */
  530. /* WaEnableFlushTlbInvalidationMode:snb */
  531. if (INTEL_INFO(dev)->gen == 6)
  532. I915_WRITE(GFX_MODE,
  533. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
  534. /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
  535. if (IS_GEN7(dev))
  536. I915_WRITE(GFX_MODE_GEN7,
  537. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
  538. _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
  539. if (INTEL_INFO(dev)->gen >= 5) {
  540. ret = init_pipe_control(ring);
  541. if (ret)
  542. return ret;
  543. }
  544. if (IS_GEN6(dev)) {
  545. /* From the Sandybridge PRM, volume 1 part 3, page 24:
  546. * "If this bit is set, STCunit will have LRA as replacement
  547. * policy. [...] This bit must be reset. LRA replacement
  548. * policy is not supported."
  549. */
  550. I915_WRITE(CACHE_MODE_0,
  551. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  552. }
  553. if (INTEL_INFO(dev)->gen >= 6)
  554. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  555. if (HAS_L3_DPF(dev))
  556. I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
  557. return ret;
  558. }
  559. static void render_ring_cleanup(struct intel_ring_buffer *ring)
  560. {
  561. struct drm_device *dev = ring->dev;
  562. if (ring->scratch.obj == NULL)
  563. return;
  564. if (INTEL_INFO(dev)->gen >= 5) {
  565. kunmap(sg_page(ring->scratch.obj->pages->sgl));
  566. i915_gem_object_ggtt_unpin(ring->scratch.obj);
  567. }
  568. drm_gem_object_unreference(&ring->scratch.obj->base);
  569. ring->scratch.obj = NULL;
  570. }
  571. static int gen6_signal(struct intel_ring_buffer *signaller,
  572. unsigned int num_dwords)
  573. {
  574. struct drm_device *dev = signaller->dev;
  575. struct drm_i915_private *dev_priv = dev->dev_private;
  576. struct intel_ring_buffer *useless;
  577. int i, ret;
  578. /* NB: In order to be able to do semaphore MBOX updates for varying
  579. * number of rings, it's easiest if we round up each individual update
  580. * to a multiple of 2 (since ring updates must always be a multiple of
  581. * 2) even though the actual update only requires 3 dwords.
  582. */
  583. #define MBOX_UPDATE_DWORDS 4
  584. if (i915_semaphore_is_enabled(dev))
  585. num_dwords += ((I915_NUM_RINGS-1) * MBOX_UPDATE_DWORDS);
  586. ret = intel_ring_begin(signaller, num_dwords);
  587. if (ret)
  588. return ret;
  589. #undef MBOX_UPDATE_DWORDS
  590. for_each_ring(useless, dev_priv, i) {
  591. u32 mbox_reg = signaller->semaphore.mbox.signal[i];
  592. if (mbox_reg != GEN6_NOSYNC) {
  593. intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
  594. intel_ring_emit(signaller, mbox_reg);
  595. intel_ring_emit(signaller, signaller->outstanding_lazy_seqno);
  596. intel_ring_emit(signaller, MI_NOOP);
  597. } else {
  598. intel_ring_emit(signaller, MI_NOOP);
  599. intel_ring_emit(signaller, MI_NOOP);
  600. intel_ring_emit(signaller, MI_NOOP);
  601. intel_ring_emit(signaller, MI_NOOP);
  602. }
  603. }
  604. return 0;
  605. }
  606. /**
  607. * gen6_add_request - Update the semaphore mailbox registers
  608. *
  609. * @ring - ring that is adding a request
  610. * @seqno - return seqno stuck into the ring
  611. *
  612. * Update the mailbox registers in the *other* rings with the current seqno.
  613. * This acts like a signal in the canonical semaphore.
  614. */
  615. static int
  616. gen6_add_request(struct intel_ring_buffer *ring)
  617. {
  618. int ret;
  619. ret = ring->semaphore.signal(ring, 4);
  620. if (ret)
  621. return ret;
  622. intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
  623. intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  624. intel_ring_emit(ring, ring->outstanding_lazy_seqno);
  625. intel_ring_emit(ring, MI_USER_INTERRUPT);
  626. __intel_ring_advance(ring);
  627. return 0;
  628. }
  629. static inline bool i915_gem_has_seqno_wrapped(struct drm_device *dev,
  630. u32 seqno)
  631. {
  632. struct drm_i915_private *dev_priv = dev->dev_private;
  633. return dev_priv->last_seqno < seqno;
  634. }
  635. /**
  636. * intel_ring_sync - sync the waiter to the signaller on seqno
  637. *
  638. * @waiter - ring that is waiting
  639. * @signaller - ring which has, or will signal
  640. * @seqno - seqno which the waiter will block on
  641. */
  642. static int
  643. gen6_ring_sync(struct intel_ring_buffer *waiter,
  644. struct intel_ring_buffer *signaller,
  645. u32 seqno)
  646. {
  647. u32 dw1 = MI_SEMAPHORE_MBOX |
  648. MI_SEMAPHORE_COMPARE |
  649. MI_SEMAPHORE_REGISTER;
  650. u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
  651. int ret;
  652. /* Throughout all of the GEM code, seqno passed implies our current
  653. * seqno is >= the last seqno executed. However for hardware the
  654. * comparison is strictly greater than.
  655. */
  656. seqno -= 1;
  657. WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
  658. ret = intel_ring_begin(waiter, 4);
  659. if (ret)
  660. return ret;
  661. /* If seqno wrap happened, omit the wait with no-ops */
  662. if (likely(!i915_gem_has_seqno_wrapped(waiter->dev, seqno))) {
  663. intel_ring_emit(waiter, dw1 | wait_mbox);
  664. intel_ring_emit(waiter, seqno);
  665. intel_ring_emit(waiter, 0);
  666. intel_ring_emit(waiter, MI_NOOP);
  667. } else {
  668. intel_ring_emit(waiter, MI_NOOP);
  669. intel_ring_emit(waiter, MI_NOOP);
  670. intel_ring_emit(waiter, MI_NOOP);
  671. intel_ring_emit(waiter, MI_NOOP);
  672. }
  673. intel_ring_advance(waiter);
  674. return 0;
  675. }
  676. #define PIPE_CONTROL_FLUSH(ring__, addr__) \
  677. do { \
  678. intel_ring_emit(ring__, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE | \
  679. PIPE_CONTROL_DEPTH_STALL); \
  680. intel_ring_emit(ring__, (addr__) | PIPE_CONTROL_GLOBAL_GTT); \
  681. intel_ring_emit(ring__, 0); \
  682. intel_ring_emit(ring__, 0); \
  683. } while (0)
  684. static int
  685. pc_render_add_request(struct intel_ring_buffer *ring)
  686. {
  687. u32 scratch_addr = ring->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  688. int ret;
  689. /* For Ironlake, MI_USER_INTERRUPT was deprecated and apparently
  690. * incoherent with writes to memory, i.e. completely fubar,
  691. * so we need to use PIPE_NOTIFY instead.
  692. *
  693. * However, we also need to workaround the qword write
  694. * incoherence by flushing the 6 PIPE_NOTIFY buffers out to
  695. * memory before requesting an interrupt.
  696. */
  697. ret = intel_ring_begin(ring, 32);
  698. if (ret)
  699. return ret;
  700. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
  701. PIPE_CONTROL_WRITE_FLUSH |
  702. PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE);
  703. intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
  704. intel_ring_emit(ring, ring->outstanding_lazy_seqno);
  705. intel_ring_emit(ring, 0);
  706. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  707. scratch_addr += 2 * CACHELINE_BYTES; /* write to separate cachelines */
  708. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  709. scratch_addr += 2 * CACHELINE_BYTES;
  710. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  711. scratch_addr += 2 * CACHELINE_BYTES;
  712. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  713. scratch_addr += 2 * CACHELINE_BYTES;
  714. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  715. scratch_addr += 2 * CACHELINE_BYTES;
  716. PIPE_CONTROL_FLUSH(ring, scratch_addr);
  717. intel_ring_emit(ring, GFX_OP_PIPE_CONTROL(4) | PIPE_CONTROL_QW_WRITE |
  718. PIPE_CONTROL_WRITE_FLUSH |
  719. PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
  720. PIPE_CONTROL_NOTIFY);
  721. intel_ring_emit(ring, ring->scratch.gtt_offset | PIPE_CONTROL_GLOBAL_GTT);
  722. intel_ring_emit(ring, ring->outstanding_lazy_seqno);
  723. intel_ring_emit(ring, 0);
  724. __intel_ring_advance(ring);
  725. return 0;
  726. }
  727. static u32
  728. gen6_ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
  729. {
  730. /* Workaround to force correct ordering between irq and seqno writes on
  731. * ivb (and maybe also on snb) by reading from a CS register (like
  732. * ACTHD) before reading the status page. */
  733. if (!lazy_coherency) {
  734. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  735. POSTING_READ(RING_ACTHD(ring->mmio_base));
  736. }
  737. return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
  738. }
  739. static u32
  740. ring_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
  741. {
  742. return intel_read_status_page(ring, I915_GEM_HWS_INDEX);
  743. }
  744. static void
  745. ring_set_seqno(struct intel_ring_buffer *ring, u32 seqno)
  746. {
  747. intel_write_status_page(ring, I915_GEM_HWS_INDEX, seqno);
  748. }
  749. static u32
  750. pc_render_get_seqno(struct intel_ring_buffer *ring, bool lazy_coherency)
  751. {
  752. return ring->scratch.cpu_page[0];
  753. }
  754. static void
  755. pc_render_set_seqno(struct intel_ring_buffer *ring, u32 seqno)
  756. {
  757. ring->scratch.cpu_page[0] = seqno;
  758. }
  759. static bool
  760. gen5_ring_get_irq(struct intel_ring_buffer *ring)
  761. {
  762. struct drm_device *dev = ring->dev;
  763. struct drm_i915_private *dev_priv = dev->dev_private;
  764. unsigned long flags;
  765. if (!dev->irq_enabled)
  766. return false;
  767. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  768. if (ring->irq_refcount++ == 0)
  769. ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
  770. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  771. return true;
  772. }
  773. static void
  774. gen5_ring_put_irq(struct intel_ring_buffer *ring)
  775. {
  776. struct drm_device *dev = ring->dev;
  777. struct drm_i915_private *dev_priv = dev->dev_private;
  778. unsigned long flags;
  779. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  780. if (--ring->irq_refcount == 0)
  781. ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
  782. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  783. }
  784. static bool
  785. i9xx_ring_get_irq(struct intel_ring_buffer *ring)
  786. {
  787. struct drm_device *dev = ring->dev;
  788. struct drm_i915_private *dev_priv = dev->dev_private;
  789. unsigned long flags;
  790. if (!dev->irq_enabled)
  791. return false;
  792. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  793. if (ring->irq_refcount++ == 0) {
  794. dev_priv->irq_mask &= ~ring->irq_enable_mask;
  795. I915_WRITE(IMR, dev_priv->irq_mask);
  796. POSTING_READ(IMR);
  797. }
  798. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  799. return true;
  800. }
  801. static void
  802. i9xx_ring_put_irq(struct intel_ring_buffer *ring)
  803. {
  804. struct drm_device *dev = ring->dev;
  805. struct drm_i915_private *dev_priv = dev->dev_private;
  806. unsigned long flags;
  807. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  808. if (--ring->irq_refcount == 0) {
  809. dev_priv->irq_mask |= ring->irq_enable_mask;
  810. I915_WRITE(IMR, dev_priv->irq_mask);
  811. POSTING_READ(IMR);
  812. }
  813. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  814. }
  815. static bool
  816. i8xx_ring_get_irq(struct intel_ring_buffer *ring)
  817. {
  818. struct drm_device *dev = ring->dev;
  819. struct drm_i915_private *dev_priv = dev->dev_private;
  820. unsigned long flags;
  821. if (!dev->irq_enabled)
  822. return false;
  823. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  824. if (ring->irq_refcount++ == 0) {
  825. dev_priv->irq_mask &= ~ring->irq_enable_mask;
  826. I915_WRITE16(IMR, dev_priv->irq_mask);
  827. POSTING_READ16(IMR);
  828. }
  829. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  830. return true;
  831. }
  832. static void
  833. i8xx_ring_put_irq(struct intel_ring_buffer *ring)
  834. {
  835. struct drm_device *dev = ring->dev;
  836. struct drm_i915_private *dev_priv = dev->dev_private;
  837. unsigned long flags;
  838. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  839. if (--ring->irq_refcount == 0) {
  840. dev_priv->irq_mask |= ring->irq_enable_mask;
  841. I915_WRITE16(IMR, dev_priv->irq_mask);
  842. POSTING_READ16(IMR);
  843. }
  844. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  845. }
  846. void intel_ring_setup_status_page(struct intel_ring_buffer *ring)
  847. {
  848. struct drm_device *dev = ring->dev;
  849. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  850. u32 mmio = 0;
  851. /* The ring status page addresses are no longer next to the rest of
  852. * the ring registers as of gen7.
  853. */
  854. if (IS_GEN7(dev)) {
  855. switch (ring->id) {
  856. case RCS:
  857. mmio = RENDER_HWS_PGA_GEN7;
  858. break;
  859. case BCS:
  860. mmio = BLT_HWS_PGA_GEN7;
  861. break;
  862. /*
  863. * VCS2 actually doesn't exist on Gen7. Only shut up
  864. * gcc switch check warning
  865. */
  866. case VCS2:
  867. case VCS:
  868. mmio = BSD_HWS_PGA_GEN7;
  869. break;
  870. case VECS:
  871. mmio = VEBOX_HWS_PGA_GEN7;
  872. break;
  873. }
  874. } else if (IS_GEN6(ring->dev)) {
  875. mmio = RING_HWS_PGA_GEN6(ring->mmio_base);
  876. } else {
  877. /* XXX: gen8 returns to sanity */
  878. mmio = RING_HWS_PGA(ring->mmio_base);
  879. }
  880. I915_WRITE(mmio, (u32)ring->status_page.gfx_addr);
  881. POSTING_READ(mmio);
  882. /*
  883. * Flush the TLB for this page
  884. *
  885. * FIXME: These two bits have disappeared on gen8, so a question
  886. * arises: do we still need this and if so how should we go about
  887. * invalidating the TLB?
  888. */
  889. if (INTEL_INFO(dev)->gen >= 6 && INTEL_INFO(dev)->gen < 8) {
  890. u32 reg = RING_INSTPM(ring->mmio_base);
  891. /* ring should be idle before issuing a sync flush*/
  892. WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
  893. I915_WRITE(reg,
  894. _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
  895. INSTPM_SYNC_FLUSH));
  896. if (wait_for((I915_READ(reg) & INSTPM_SYNC_FLUSH) == 0,
  897. 1000))
  898. DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
  899. ring->name);
  900. }
  901. }
  902. static int
  903. bsd_ring_flush(struct intel_ring_buffer *ring,
  904. u32 invalidate_domains,
  905. u32 flush_domains)
  906. {
  907. int ret;
  908. ret = intel_ring_begin(ring, 2);
  909. if (ret)
  910. return ret;
  911. intel_ring_emit(ring, MI_FLUSH);
  912. intel_ring_emit(ring, MI_NOOP);
  913. intel_ring_advance(ring);
  914. return 0;
  915. }
  916. static int
  917. i9xx_add_request(struct intel_ring_buffer *ring)
  918. {
  919. int ret;
  920. ret = intel_ring_begin(ring, 4);
  921. if (ret)
  922. return ret;
  923. intel_ring_emit(ring, MI_STORE_DWORD_INDEX);
  924. intel_ring_emit(ring, I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  925. intel_ring_emit(ring, ring->outstanding_lazy_seqno);
  926. intel_ring_emit(ring, MI_USER_INTERRUPT);
  927. __intel_ring_advance(ring);
  928. return 0;
  929. }
  930. static bool
  931. gen6_ring_get_irq(struct intel_ring_buffer *ring)
  932. {
  933. struct drm_device *dev = ring->dev;
  934. struct drm_i915_private *dev_priv = dev->dev_private;
  935. unsigned long flags;
  936. if (!dev->irq_enabled)
  937. return false;
  938. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  939. if (ring->irq_refcount++ == 0) {
  940. if (HAS_L3_DPF(dev) && ring->id == RCS)
  941. I915_WRITE_IMR(ring,
  942. ~(ring->irq_enable_mask |
  943. GT_PARITY_ERROR(dev)));
  944. else
  945. I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
  946. ilk_enable_gt_irq(dev_priv, ring->irq_enable_mask);
  947. }
  948. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  949. return true;
  950. }
  951. static void
  952. gen6_ring_put_irq(struct intel_ring_buffer *ring)
  953. {
  954. struct drm_device *dev = ring->dev;
  955. struct drm_i915_private *dev_priv = dev->dev_private;
  956. unsigned long flags;
  957. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  958. if (--ring->irq_refcount == 0) {
  959. if (HAS_L3_DPF(dev) && ring->id == RCS)
  960. I915_WRITE_IMR(ring, ~GT_PARITY_ERROR(dev));
  961. else
  962. I915_WRITE_IMR(ring, ~0);
  963. ilk_disable_gt_irq(dev_priv, ring->irq_enable_mask);
  964. }
  965. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  966. }
  967. static bool
  968. hsw_vebox_get_irq(struct intel_ring_buffer *ring)
  969. {
  970. struct drm_device *dev = ring->dev;
  971. struct drm_i915_private *dev_priv = dev->dev_private;
  972. unsigned long flags;
  973. if (!dev->irq_enabled)
  974. return false;
  975. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  976. if (ring->irq_refcount++ == 0) {
  977. I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
  978. snb_enable_pm_irq(dev_priv, ring->irq_enable_mask);
  979. }
  980. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  981. return true;
  982. }
  983. static void
  984. hsw_vebox_put_irq(struct intel_ring_buffer *ring)
  985. {
  986. struct drm_device *dev = ring->dev;
  987. struct drm_i915_private *dev_priv = dev->dev_private;
  988. unsigned long flags;
  989. if (!dev->irq_enabled)
  990. return;
  991. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  992. if (--ring->irq_refcount == 0) {
  993. I915_WRITE_IMR(ring, ~0);
  994. snb_disable_pm_irq(dev_priv, ring->irq_enable_mask);
  995. }
  996. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  997. }
  998. static bool
  999. gen8_ring_get_irq(struct intel_ring_buffer *ring)
  1000. {
  1001. struct drm_device *dev = ring->dev;
  1002. struct drm_i915_private *dev_priv = dev->dev_private;
  1003. unsigned long flags;
  1004. if (!dev->irq_enabled)
  1005. return false;
  1006. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  1007. if (ring->irq_refcount++ == 0) {
  1008. if (HAS_L3_DPF(dev) && ring->id == RCS) {
  1009. I915_WRITE_IMR(ring,
  1010. ~(ring->irq_enable_mask |
  1011. GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
  1012. } else {
  1013. I915_WRITE_IMR(ring, ~ring->irq_enable_mask);
  1014. }
  1015. POSTING_READ(RING_IMR(ring->mmio_base));
  1016. }
  1017. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  1018. return true;
  1019. }
  1020. static void
  1021. gen8_ring_put_irq(struct intel_ring_buffer *ring)
  1022. {
  1023. struct drm_device *dev = ring->dev;
  1024. struct drm_i915_private *dev_priv = dev->dev_private;
  1025. unsigned long flags;
  1026. spin_lock_irqsave(&dev_priv->irq_lock, flags);
  1027. if (--ring->irq_refcount == 0) {
  1028. if (HAS_L3_DPF(dev) && ring->id == RCS) {
  1029. I915_WRITE_IMR(ring,
  1030. ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
  1031. } else {
  1032. I915_WRITE_IMR(ring, ~0);
  1033. }
  1034. POSTING_READ(RING_IMR(ring->mmio_base));
  1035. }
  1036. spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
  1037. }
  1038. static int
  1039. i965_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1040. u64 offset, u32 length,
  1041. unsigned flags)
  1042. {
  1043. int ret;
  1044. ret = intel_ring_begin(ring, 2);
  1045. if (ret)
  1046. return ret;
  1047. intel_ring_emit(ring,
  1048. MI_BATCH_BUFFER_START |
  1049. MI_BATCH_GTT |
  1050. (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
  1051. intel_ring_emit(ring, offset);
  1052. intel_ring_advance(ring);
  1053. return 0;
  1054. }
  1055. /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
  1056. #define I830_BATCH_LIMIT (256*1024)
  1057. static int
  1058. i830_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1059. u64 offset, u32 len,
  1060. unsigned flags)
  1061. {
  1062. int ret;
  1063. if (flags & I915_DISPATCH_PINNED) {
  1064. ret = intel_ring_begin(ring, 4);
  1065. if (ret)
  1066. return ret;
  1067. intel_ring_emit(ring, MI_BATCH_BUFFER);
  1068. intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
  1069. intel_ring_emit(ring, offset + len - 8);
  1070. intel_ring_emit(ring, MI_NOOP);
  1071. intel_ring_advance(ring);
  1072. } else {
  1073. u32 cs_offset = ring->scratch.gtt_offset;
  1074. if (len > I830_BATCH_LIMIT)
  1075. return -ENOSPC;
  1076. ret = intel_ring_begin(ring, 9+3);
  1077. if (ret)
  1078. return ret;
  1079. /* Blit the batch (which has now all relocs applied) to the stable batch
  1080. * scratch bo area (so that the CS never stumbles over its tlb
  1081. * invalidation bug) ... */
  1082. intel_ring_emit(ring, XY_SRC_COPY_BLT_CMD |
  1083. XY_SRC_COPY_BLT_WRITE_ALPHA |
  1084. XY_SRC_COPY_BLT_WRITE_RGB);
  1085. intel_ring_emit(ring, BLT_DEPTH_32 | BLT_ROP_GXCOPY | 4096);
  1086. intel_ring_emit(ring, 0);
  1087. intel_ring_emit(ring, (DIV_ROUND_UP(len, 4096) << 16) | 1024);
  1088. intel_ring_emit(ring, cs_offset);
  1089. intel_ring_emit(ring, 0);
  1090. intel_ring_emit(ring, 4096);
  1091. intel_ring_emit(ring, offset);
  1092. intel_ring_emit(ring, MI_FLUSH);
  1093. /* ... and execute it. */
  1094. intel_ring_emit(ring, MI_BATCH_BUFFER);
  1095. intel_ring_emit(ring, cs_offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
  1096. intel_ring_emit(ring, cs_offset + len - 8);
  1097. intel_ring_advance(ring);
  1098. }
  1099. return 0;
  1100. }
  1101. static int
  1102. i915_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1103. u64 offset, u32 len,
  1104. unsigned flags)
  1105. {
  1106. int ret;
  1107. ret = intel_ring_begin(ring, 2);
  1108. if (ret)
  1109. return ret;
  1110. intel_ring_emit(ring, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
  1111. intel_ring_emit(ring, offset | (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE));
  1112. intel_ring_advance(ring);
  1113. return 0;
  1114. }
  1115. static void cleanup_status_page(struct intel_ring_buffer *ring)
  1116. {
  1117. struct drm_i915_gem_object *obj;
  1118. obj = ring->status_page.obj;
  1119. if (obj == NULL)
  1120. return;
  1121. kunmap(sg_page(obj->pages->sgl));
  1122. i915_gem_object_ggtt_unpin(obj);
  1123. drm_gem_object_unreference(&obj->base);
  1124. ring->status_page.obj = NULL;
  1125. }
  1126. static int init_status_page(struct intel_ring_buffer *ring)
  1127. {
  1128. struct drm_i915_gem_object *obj;
  1129. if ((obj = ring->status_page.obj) == NULL) {
  1130. int ret;
  1131. obj = i915_gem_alloc_object(ring->dev, 4096);
  1132. if (obj == NULL) {
  1133. DRM_ERROR("Failed to allocate status page\n");
  1134. return -ENOMEM;
  1135. }
  1136. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  1137. if (ret)
  1138. goto err_unref;
  1139. ret = i915_gem_obj_ggtt_pin(obj, 4096, 0);
  1140. if (ret) {
  1141. err_unref:
  1142. drm_gem_object_unreference(&obj->base);
  1143. return ret;
  1144. }
  1145. ring->status_page.obj = obj;
  1146. }
  1147. ring->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
  1148. ring->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
  1149. memset(ring->status_page.page_addr, 0, PAGE_SIZE);
  1150. DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
  1151. ring->name, ring->status_page.gfx_addr);
  1152. return 0;
  1153. }
  1154. static int init_phys_status_page(struct intel_ring_buffer *ring)
  1155. {
  1156. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1157. if (!dev_priv->status_page_dmah) {
  1158. dev_priv->status_page_dmah =
  1159. drm_pci_alloc(ring->dev, PAGE_SIZE, PAGE_SIZE);
  1160. if (!dev_priv->status_page_dmah)
  1161. return -ENOMEM;
  1162. }
  1163. ring->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
  1164. memset(ring->status_page.page_addr, 0, PAGE_SIZE);
  1165. return 0;
  1166. }
  1167. static int allocate_ring_buffer(struct intel_ring_buffer *ring)
  1168. {
  1169. struct drm_device *dev = ring->dev;
  1170. struct drm_i915_private *dev_priv = to_i915(dev);
  1171. struct drm_i915_gem_object *obj;
  1172. int ret;
  1173. if (ring->obj)
  1174. return 0;
  1175. obj = NULL;
  1176. if (!HAS_LLC(dev))
  1177. obj = i915_gem_object_create_stolen(dev, ring->size);
  1178. if (obj == NULL)
  1179. obj = i915_gem_alloc_object(dev, ring->size);
  1180. if (obj == NULL)
  1181. return -ENOMEM;
  1182. ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, PIN_MAPPABLE);
  1183. if (ret)
  1184. goto err_unref;
  1185. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  1186. if (ret)
  1187. goto err_unpin;
  1188. ring->virtual_start =
  1189. ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
  1190. ring->size);
  1191. if (ring->virtual_start == NULL) {
  1192. ret = -EINVAL;
  1193. goto err_unpin;
  1194. }
  1195. ring->obj = obj;
  1196. return 0;
  1197. err_unpin:
  1198. i915_gem_object_ggtt_unpin(obj);
  1199. err_unref:
  1200. drm_gem_object_unreference(&obj->base);
  1201. return ret;
  1202. }
  1203. static int intel_init_ring_buffer(struct drm_device *dev,
  1204. struct intel_ring_buffer *ring)
  1205. {
  1206. int ret;
  1207. ring->dev = dev;
  1208. INIT_LIST_HEAD(&ring->active_list);
  1209. INIT_LIST_HEAD(&ring->request_list);
  1210. ring->size = 32 * PAGE_SIZE;
  1211. memset(ring->semaphore.sync_seqno, 0, sizeof(ring->semaphore.sync_seqno));
  1212. init_waitqueue_head(&ring->irq_queue);
  1213. if (I915_NEED_GFX_HWS(dev)) {
  1214. ret = init_status_page(ring);
  1215. if (ret)
  1216. return ret;
  1217. } else {
  1218. BUG_ON(ring->id != RCS);
  1219. ret = init_phys_status_page(ring);
  1220. if (ret)
  1221. return ret;
  1222. }
  1223. ret = allocate_ring_buffer(ring);
  1224. if (ret) {
  1225. DRM_ERROR("Failed to allocate ringbuffer %s: %d\n", ring->name, ret);
  1226. return ret;
  1227. }
  1228. /* Workaround an erratum on the i830 which causes a hang if
  1229. * the TAIL pointer points to within the last 2 cachelines
  1230. * of the buffer.
  1231. */
  1232. ring->effective_size = ring->size;
  1233. if (IS_I830(dev) || IS_845G(dev))
  1234. ring->effective_size -= 2 * CACHELINE_BYTES;
  1235. i915_cmd_parser_init_ring(ring);
  1236. return ring->init(ring);
  1237. }
  1238. void intel_cleanup_ring_buffer(struct intel_ring_buffer *ring)
  1239. {
  1240. struct drm_i915_private *dev_priv = to_i915(ring->dev);
  1241. if (ring->obj == NULL)
  1242. return;
  1243. intel_stop_ring_buffer(ring);
  1244. WARN_ON((I915_READ_MODE(ring) & MODE_IDLE) == 0);
  1245. iounmap(ring->virtual_start);
  1246. i915_gem_object_ggtt_unpin(ring->obj);
  1247. drm_gem_object_unreference(&ring->obj->base);
  1248. ring->obj = NULL;
  1249. ring->preallocated_lazy_request = NULL;
  1250. ring->outstanding_lazy_seqno = 0;
  1251. if (ring->cleanup)
  1252. ring->cleanup(ring);
  1253. cleanup_status_page(ring);
  1254. }
  1255. static int intel_ring_wait_request(struct intel_ring_buffer *ring, int n)
  1256. {
  1257. struct drm_i915_gem_request *request;
  1258. u32 seqno = 0;
  1259. int ret;
  1260. if (ring->last_retired_head != -1) {
  1261. ring->head = ring->last_retired_head;
  1262. ring->last_retired_head = -1;
  1263. ring->space = ring_space(ring);
  1264. if (ring->space >= n)
  1265. return 0;
  1266. }
  1267. list_for_each_entry(request, &ring->request_list, list) {
  1268. if (__ring_space(request->tail, ring->tail, ring->size) >= n) {
  1269. seqno = request->seqno;
  1270. break;
  1271. }
  1272. }
  1273. if (seqno == 0)
  1274. return -ENOSPC;
  1275. ret = i915_wait_seqno(ring, seqno);
  1276. if (ret)
  1277. return ret;
  1278. i915_gem_retire_requests_ring(ring);
  1279. ring->head = ring->last_retired_head;
  1280. ring->last_retired_head = -1;
  1281. ring->space = ring_space(ring);
  1282. return 0;
  1283. }
  1284. static int ring_wait_for_space(struct intel_ring_buffer *ring, int n)
  1285. {
  1286. struct drm_device *dev = ring->dev;
  1287. struct drm_i915_private *dev_priv = dev->dev_private;
  1288. unsigned long end;
  1289. int ret;
  1290. ret = intel_ring_wait_request(ring, n);
  1291. if (ret != -ENOSPC)
  1292. return ret;
  1293. /* force the tail write in case we have been skipping them */
  1294. __intel_ring_advance(ring);
  1295. /* With GEM the hangcheck timer should kick us out of the loop,
  1296. * leaving it early runs the risk of corrupting GEM state (due
  1297. * to running on almost untested codepaths). But on resume
  1298. * timers don't work yet, so prevent a complete hang in that
  1299. * case by choosing an insanely large timeout. */
  1300. end = jiffies + 60 * HZ;
  1301. trace_i915_ring_wait_begin(ring);
  1302. do {
  1303. ring->head = I915_READ_HEAD(ring);
  1304. ring->space = ring_space(ring);
  1305. if (ring->space >= n) {
  1306. ret = 0;
  1307. break;
  1308. }
  1309. if (!drm_core_check_feature(dev, DRIVER_MODESET) &&
  1310. dev->primary->master) {
  1311. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  1312. if (master_priv->sarea_priv)
  1313. master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
  1314. }
  1315. msleep(1);
  1316. if (dev_priv->mm.interruptible && signal_pending(current)) {
  1317. ret = -ERESTARTSYS;
  1318. break;
  1319. }
  1320. ret = i915_gem_check_wedge(&dev_priv->gpu_error,
  1321. dev_priv->mm.interruptible);
  1322. if (ret)
  1323. break;
  1324. if (time_after(jiffies, end)) {
  1325. ret = -EBUSY;
  1326. break;
  1327. }
  1328. } while (1);
  1329. trace_i915_ring_wait_end(ring);
  1330. return ret;
  1331. }
  1332. static int intel_wrap_ring_buffer(struct intel_ring_buffer *ring)
  1333. {
  1334. uint32_t __iomem *virt;
  1335. int rem = ring->size - ring->tail;
  1336. if (ring->space < rem) {
  1337. int ret = ring_wait_for_space(ring, rem);
  1338. if (ret)
  1339. return ret;
  1340. }
  1341. virt = ring->virtual_start + ring->tail;
  1342. rem /= 4;
  1343. while (rem--)
  1344. iowrite32(MI_NOOP, virt++);
  1345. ring->tail = 0;
  1346. ring->space = ring_space(ring);
  1347. return 0;
  1348. }
  1349. int intel_ring_idle(struct intel_ring_buffer *ring)
  1350. {
  1351. u32 seqno;
  1352. int ret;
  1353. /* We need to add any requests required to flush the objects and ring */
  1354. if (ring->outstanding_lazy_seqno) {
  1355. ret = i915_add_request(ring, NULL);
  1356. if (ret)
  1357. return ret;
  1358. }
  1359. /* Wait upon the last request to be completed */
  1360. if (list_empty(&ring->request_list))
  1361. return 0;
  1362. seqno = list_entry(ring->request_list.prev,
  1363. struct drm_i915_gem_request,
  1364. list)->seqno;
  1365. return i915_wait_seqno(ring, seqno);
  1366. }
  1367. static int
  1368. intel_ring_alloc_seqno(struct intel_ring_buffer *ring)
  1369. {
  1370. if (ring->outstanding_lazy_seqno)
  1371. return 0;
  1372. if (ring->preallocated_lazy_request == NULL) {
  1373. struct drm_i915_gem_request *request;
  1374. request = kmalloc(sizeof(*request), GFP_KERNEL);
  1375. if (request == NULL)
  1376. return -ENOMEM;
  1377. ring->preallocated_lazy_request = request;
  1378. }
  1379. return i915_gem_get_seqno(ring->dev, &ring->outstanding_lazy_seqno);
  1380. }
  1381. static int __intel_ring_prepare(struct intel_ring_buffer *ring,
  1382. int bytes)
  1383. {
  1384. int ret;
  1385. if (unlikely(ring->tail + bytes > ring->effective_size)) {
  1386. ret = intel_wrap_ring_buffer(ring);
  1387. if (unlikely(ret))
  1388. return ret;
  1389. }
  1390. if (unlikely(ring->space < bytes)) {
  1391. ret = ring_wait_for_space(ring, bytes);
  1392. if (unlikely(ret))
  1393. return ret;
  1394. }
  1395. return 0;
  1396. }
  1397. int intel_ring_begin(struct intel_ring_buffer *ring,
  1398. int num_dwords)
  1399. {
  1400. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1401. int ret;
  1402. ret = i915_gem_check_wedge(&dev_priv->gpu_error,
  1403. dev_priv->mm.interruptible);
  1404. if (ret)
  1405. return ret;
  1406. ret = __intel_ring_prepare(ring, num_dwords * sizeof(uint32_t));
  1407. if (ret)
  1408. return ret;
  1409. /* Preallocate the olr before touching the ring */
  1410. ret = intel_ring_alloc_seqno(ring);
  1411. if (ret)
  1412. return ret;
  1413. ring->space -= num_dwords * sizeof(uint32_t);
  1414. return 0;
  1415. }
  1416. /* Align the ring tail to a cacheline boundary */
  1417. int intel_ring_cacheline_align(struct intel_ring_buffer *ring)
  1418. {
  1419. int num_dwords = (ring->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
  1420. int ret;
  1421. if (num_dwords == 0)
  1422. return 0;
  1423. num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
  1424. ret = intel_ring_begin(ring, num_dwords);
  1425. if (ret)
  1426. return ret;
  1427. while (num_dwords--)
  1428. intel_ring_emit(ring, MI_NOOP);
  1429. intel_ring_advance(ring);
  1430. return 0;
  1431. }
  1432. void intel_ring_init_seqno(struct intel_ring_buffer *ring, u32 seqno)
  1433. {
  1434. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1435. BUG_ON(ring->outstanding_lazy_seqno);
  1436. if (INTEL_INFO(ring->dev)->gen >= 6) {
  1437. I915_WRITE(RING_SYNC_0(ring->mmio_base), 0);
  1438. I915_WRITE(RING_SYNC_1(ring->mmio_base), 0);
  1439. if (HAS_VEBOX(ring->dev))
  1440. I915_WRITE(RING_SYNC_2(ring->mmio_base), 0);
  1441. }
  1442. ring->set_seqno(ring, seqno);
  1443. ring->hangcheck.seqno = seqno;
  1444. }
  1445. static void gen6_bsd_ring_write_tail(struct intel_ring_buffer *ring,
  1446. u32 value)
  1447. {
  1448. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1449. /* Every tail move must follow the sequence below */
  1450. /* Disable notification that the ring is IDLE. The GT
  1451. * will then assume that it is busy and bring it out of rc6.
  1452. */
  1453. I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1454. _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  1455. /* Clear the context id. Here be magic! */
  1456. I915_WRITE64(GEN6_BSD_RNCID, 0x0);
  1457. /* Wait for the ring not to be idle, i.e. for it to wake up. */
  1458. if (wait_for((I915_READ(GEN6_BSD_SLEEP_PSMI_CONTROL) &
  1459. GEN6_BSD_SLEEP_INDICATOR) == 0,
  1460. 50))
  1461. DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
  1462. /* Now that the ring is fully powered up, update the tail */
  1463. I915_WRITE_TAIL(ring, value);
  1464. POSTING_READ(RING_TAIL(ring->mmio_base));
  1465. /* Let the ring send IDLE messages to the GT again,
  1466. * and so let it sleep to conserve power when idle.
  1467. */
  1468. I915_WRITE(GEN6_BSD_SLEEP_PSMI_CONTROL,
  1469. _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  1470. }
  1471. static int gen6_bsd_ring_flush(struct intel_ring_buffer *ring,
  1472. u32 invalidate, u32 flush)
  1473. {
  1474. uint32_t cmd;
  1475. int ret;
  1476. ret = intel_ring_begin(ring, 4);
  1477. if (ret)
  1478. return ret;
  1479. cmd = MI_FLUSH_DW;
  1480. if (INTEL_INFO(ring->dev)->gen >= 8)
  1481. cmd += 1;
  1482. /*
  1483. * Bspec vol 1c.5 - video engine command streamer:
  1484. * "If ENABLED, all TLBs will be invalidated once the flush
  1485. * operation is complete. This bit is only valid when the
  1486. * Post-Sync Operation field is a value of 1h or 3h."
  1487. */
  1488. if (invalidate & I915_GEM_GPU_DOMAINS)
  1489. cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD |
  1490. MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  1491. intel_ring_emit(ring, cmd);
  1492. intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
  1493. if (INTEL_INFO(ring->dev)->gen >= 8) {
  1494. intel_ring_emit(ring, 0); /* upper addr */
  1495. intel_ring_emit(ring, 0); /* value */
  1496. } else {
  1497. intel_ring_emit(ring, 0);
  1498. intel_ring_emit(ring, MI_NOOP);
  1499. }
  1500. intel_ring_advance(ring);
  1501. return 0;
  1502. }
  1503. static int
  1504. gen8_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1505. u64 offset, u32 len,
  1506. unsigned flags)
  1507. {
  1508. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1509. bool ppgtt = dev_priv->mm.aliasing_ppgtt != NULL &&
  1510. !(flags & I915_DISPATCH_SECURE);
  1511. int ret;
  1512. ret = intel_ring_begin(ring, 4);
  1513. if (ret)
  1514. return ret;
  1515. /* FIXME(BDW): Address space and security selectors. */
  1516. intel_ring_emit(ring, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8));
  1517. intel_ring_emit(ring, lower_32_bits(offset));
  1518. intel_ring_emit(ring, upper_32_bits(offset));
  1519. intel_ring_emit(ring, MI_NOOP);
  1520. intel_ring_advance(ring);
  1521. return 0;
  1522. }
  1523. static int
  1524. hsw_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1525. u64 offset, u32 len,
  1526. unsigned flags)
  1527. {
  1528. int ret;
  1529. ret = intel_ring_begin(ring, 2);
  1530. if (ret)
  1531. return ret;
  1532. intel_ring_emit(ring,
  1533. MI_BATCH_BUFFER_START | MI_BATCH_PPGTT_HSW |
  1534. (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_HSW));
  1535. /* bit0-7 is the length on GEN6+ */
  1536. intel_ring_emit(ring, offset);
  1537. intel_ring_advance(ring);
  1538. return 0;
  1539. }
  1540. static int
  1541. gen6_ring_dispatch_execbuffer(struct intel_ring_buffer *ring,
  1542. u64 offset, u32 len,
  1543. unsigned flags)
  1544. {
  1545. int ret;
  1546. ret = intel_ring_begin(ring, 2);
  1547. if (ret)
  1548. return ret;
  1549. intel_ring_emit(ring,
  1550. MI_BATCH_BUFFER_START |
  1551. (flags & I915_DISPATCH_SECURE ? 0 : MI_BATCH_NON_SECURE_I965));
  1552. /* bit0-7 is the length on GEN6+ */
  1553. intel_ring_emit(ring, offset);
  1554. intel_ring_advance(ring);
  1555. return 0;
  1556. }
  1557. /* Blitter support (SandyBridge+) */
  1558. static int gen6_ring_flush(struct intel_ring_buffer *ring,
  1559. u32 invalidate, u32 flush)
  1560. {
  1561. struct drm_device *dev = ring->dev;
  1562. uint32_t cmd;
  1563. int ret;
  1564. ret = intel_ring_begin(ring, 4);
  1565. if (ret)
  1566. return ret;
  1567. cmd = MI_FLUSH_DW;
  1568. if (INTEL_INFO(ring->dev)->gen >= 8)
  1569. cmd += 1;
  1570. /*
  1571. * Bspec vol 1c.3 - blitter engine command streamer:
  1572. * "If ENABLED, all TLBs will be invalidated once the flush
  1573. * operation is complete. This bit is only valid when the
  1574. * Post-Sync Operation field is a value of 1h or 3h."
  1575. */
  1576. if (invalidate & I915_GEM_DOMAIN_RENDER)
  1577. cmd |= MI_INVALIDATE_TLB | MI_FLUSH_DW_STORE_INDEX |
  1578. MI_FLUSH_DW_OP_STOREDW;
  1579. intel_ring_emit(ring, cmd);
  1580. intel_ring_emit(ring, I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
  1581. if (INTEL_INFO(ring->dev)->gen >= 8) {
  1582. intel_ring_emit(ring, 0); /* upper addr */
  1583. intel_ring_emit(ring, 0); /* value */
  1584. } else {
  1585. intel_ring_emit(ring, 0);
  1586. intel_ring_emit(ring, MI_NOOP);
  1587. }
  1588. intel_ring_advance(ring);
  1589. if (IS_GEN7(dev) && !invalidate && flush)
  1590. return gen7_ring_fbc_flush(ring, FBC_REND_CACHE_CLEAN);
  1591. return 0;
  1592. }
  1593. int intel_init_render_ring_buffer(struct drm_device *dev)
  1594. {
  1595. struct drm_i915_private *dev_priv = dev->dev_private;
  1596. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  1597. ring->name = "render ring";
  1598. ring->id = RCS;
  1599. ring->mmio_base = RENDER_RING_BASE;
  1600. if (INTEL_INFO(dev)->gen >= 6) {
  1601. ring->add_request = gen6_add_request;
  1602. ring->flush = gen7_render_ring_flush;
  1603. if (INTEL_INFO(dev)->gen == 6)
  1604. ring->flush = gen6_render_ring_flush;
  1605. if (INTEL_INFO(dev)->gen >= 8) {
  1606. ring->flush = gen8_render_ring_flush;
  1607. ring->irq_get = gen8_ring_get_irq;
  1608. ring->irq_put = gen8_ring_put_irq;
  1609. } else {
  1610. ring->irq_get = gen6_ring_get_irq;
  1611. ring->irq_put = gen6_ring_put_irq;
  1612. }
  1613. ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
  1614. ring->get_seqno = gen6_ring_get_seqno;
  1615. ring->set_seqno = ring_set_seqno;
  1616. ring->semaphore.sync_to = gen6_ring_sync;
  1617. ring->semaphore.signal = gen6_signal;
  1618. /*
  1619. * The current semaphore is only applied on pre-gen8 platform.
  1620. * And there is no VCS2 ring on the pre-gen8 platform. So the
  1621. * semaphore between RCS and VCS2 is initialized as INVALID.
  1622. * Gen8 will initialize the sema between VCS2 and RCS later.
  1623. */
  1624. ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
  1625. ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_RV;
  1626. ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_RB;
  1627. ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_RVE;
  1628. ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
  1629. ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
  1630. ring->semaphore.mbox.signal[VCS] = GEN6_VRSYNC;
  1631. ring->semaphore.mbox.signal[BCS] = GEN6_BRSYNC;
  1632. ring->semaphore.mbox.signal[VECS] = GEN6_VERSYNC;
  1633. ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
  1634. } else if (IS_GEN5(dev)) {
  1635. ring->add_request = pc_render_add_request;
  1636. ring->flush = gen4_render_ring_flush;
  1637. ring->get_seqno = pc_render_get_seqno;
  1638. ring->set_seqno = pc_render_set_seqno;
  1639. ring->irq_get = gen5_ring_get_irq;
  1640. ring->irq_put = gen5_ring_put_irq;
  1641. ring->irq_enable_mask = GT_RENDER_USER_INTERRUPT |
  1642. GT_RENDER_PIPECTL_NOTIFY_INTERRUPT;
  1643. } else {
  1644. ring->add_request = i9xx_add_request;
  1645. if (INTEL_INFO(dev)->gen < 4)
  1646. ring->flush = gen2_render_ring_flush;
  1647. else
  1648. ring->flush = gen4_render_ring_flush;
  1649. ring->get_seqno = ring_get_seqno;
  1650. ring->set_seqno = ring_set_seqno;
  1651. if (IS_GEN2(dev)) {
  1652. ring->irq_get = i8xx_ring_get_irq;
  1653. ring->irq_put = i8xx_ring_put_irq;
  1654. } else {
  1655. ring->irq_get = i9xx_ring_get_irq;
  1656. ring->irq_put = i9xx_ring_put_irq;
  1657. }
  1658. ring->irq_enable_mask = I915_USER_INTERRUPT;
  1659. }
  1660. ring->write_tail = ring_write_tail;
  1661. if (IS_HASWELL(dev))
  1662. ring->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
  1663. else if (IS_GEN8(dev))
  1664. ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
  1665. else if (INTEL_INFO(dev)->gen >= 6)
  1666. ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
  1667. else if (INTEL_INFO(dev)->gen >= 4)
  1668. ring->dispatch_execbuffer = i965_dispatch_execbuffer;
  1669. else if (IS_I830(dev) || IS_845G(dev))
  1670. ring->dispatch_execbuffer = i830_dispatch_execbuffer;
  1671. else
  1672. ring->dispatch_execbuffer = i915_dispatch_execbuffer;
  1673. ring->init = init_render_ring;
  1674. ring->cleanup = render_ring_cleanup;
  1675. /* Workaround batchbuffer to combat CS tlb bug. */
  1676. if (HAS_BROKEN_CS_TLB(dev)) {
  1677. struct drm_i915_gem_object *obj;
  1678. int ret;
  1679. obj = i915_gem_alloc_object(dev, I830_BATCH_LIMIT);
  1680. if (obj == NULL) {
  1681. DRM_ERROR("Failed to allocate batch bo\n");
  1682. return -ENOMEM;
  1683. }
  1684. ret = i915_gem_obj_ggtt_pin(obj, 0, 0);
  1685. if (ret != 0) {
  1686. drm_gem_object_unreference(&obj->base);
  1687. DRM_ERROR("Failed to ping batch bo\n");
  1688. return ret;
  1689. }
  1690. ring->scratch.obj = obj;
  1691. ring->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
  1692. }
  1693. return intel_init_ring_buffer(dev, ring);
  1694. }
  1695. int intel_render_ring_init_dri(struct drm_device *dev, u64 start, u32 size)
  1696. {
  1697. struct drm_i915_private *dev_priv = dev->dev_private;
  1698. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  1699. int ret;
  1700. ring->name = "render ring";
  1701. ring->id = RCS;
  1702. ring->mmio_base = RENDER_RING_BASE;
  1703. if (INTEL_INFO(dev)->gen >= 6) {
  1704. /* non-kms not supported on gen6+ */
  1705. return -ENODEV;
  1706. }
  1707. /* Note: gem is not supported on gen5/ilk without kms (the corresponding
  1708. * gem_init ioctl returns with -ENODEV). Hence we do not need to set up
  1709. * the special gen5 functions. */
  1710. ring->add_request = i9xx_add_request;
  1711. if (INTEL_INFO(dev)->gen < 4)
  1712. ring->flush = gen2_render_ring_flush;
  1713. else
  1714. ring->flush = gen4_render_ring_flush;
  1715. ring->get_seqno = ring_get_seqno;
  1716. ring->set_seqno = ring_set_seqno;
  1717. if (IS_GEN2(dev)) {
  1718. ring->irq_get = i8xx_ring_get_irq;
  1719. ring->irq_put = i8xx_ring_put_irq;
  1720. } else {
  1721. ring->irq_get = i9xx_ring_get_irq;
  1722. ring->irq_put = i9xx_ring_put_irq;
  1723. }
  1724. ring->irq_enable_mask = I915_USER_INTERRUPT;
  1725. ring->write_tail = ring_write_tail;
  1726. if (INTEL_INFO(dev)->gen >= 4)
  1727. ring->dispatch_execbuffer = i965_dispatch_execbuffer;
  1728. else if (IS_I830(dev) || IS_845G(dev))
  1729. ring->dispatch_execbuffer = i830_dispatch_execbuffer;
  1730. else
  1731. ring->dispatch_execbuffer = i915_dispatch_execbuffer;
  1732. ring->init = init_render_ring;
  1733. ring->cleanup = render_ring_cleanup;
  1734. ring->dev = dev;
  1735. INIT_LIST_HEAD(&ring->active_list);
  1736. INIT_LIST_HEAD(&ring->request_list);
  1737. ring->size = size;
  1738. ring->effective_size = ring->size;
  1739. if (IS_I830(ring->dev) || IS_845G(ring->dev))
  1740. ring->effective_size -= 2 * CACHELINE_BYTES;
  1741. ring->virtual_start = ioremap_wc(start, size);
  1742. if (ring->virtual_start == NULL) {
  1743. DRM_ERROR("can not ioremap virtual address for"
  1744. " ring buffer\n");
  1745. return -ENOMEM;
  1746. }
  1747. if (!I915_NEED_GFX_HWS(dev)) {
  1748. ret = init_phys_status_page(ring);
  1749. if (ret)
  1750. return ret;
  1751. }
  1752. return 0;
  1753. }
  1754. int intel_init_bsd_ring_buffer(struct drm_device *dev)
  1755. {
  1756. struct drm_i915_private *dev_priv = dev->dev_private;
  1757. struct intel_ring_buffer *ring = &dev_priv->ring[VCS];
  1758. ring->name = "bsd ring";
  1759. ring->id = VCS;
  1760. ring->write_tail = ring_write_tail;
  1761. if (INTEL_INFO(dev)->gen >= 6) {
  1762. ring->mmio_base = GEN6_BSD_RING_BASE;
  1763. /* gen6 bsd needs a special wa for tail updates */
  1764. if (IS_GEN6(dev))
  1765. ring->write_tail = gen6_bsd_ring_write_tail;
  1766. ring->flush = gen6_bsd_ring_flush;
  1767. ring->add_request = gen6_add_request;
  1768. ring->get_seqno = gen6_ring_get_seqno;
  1769. ring->set_seqno = ring_set_seqno;
  1770. if (INTEL_INFO(dev)->gen >= 8) {
  1771. ring->irq_enable_mask =
  1772. GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  1773. ring->irq_get = gen8_ring_get_irq;
  1774. ring->irq_put = gen8_ring_put_irq;
  1775. ring->dispatch_execbuffer =
  1776. gen8_ring_dispatch_execbuffer;
  1777. } else {
  1778. ring->irq_enable_mask = GT_BSD_USER_INTERRUPT;
  1779. ring->irq_get = gen6_ring_get_irq;
  1780. ring->irq_put = gen6_ring_put_irq;
  1781. ring->dispatch_execbuffer =
  1782. gen6_ring_dispatch_execbuffer;
  1783. }
  1784. ring->semaphore.sync_to = gen6_ring_sync;
  1785. ring->semaphore.signal = gen6_signal;
  1786. /*
  1787. * The current semaphore is only applied on pre-gen8 platform.
  1788. * And there is no VCS2 ring on the pre-gen8 platform. So the
  1789. * semaphore between VCS and VCS2 is initialized as INVALID.
  1790. * Gen8 will initialize the sema between VCS2 and VCS later.
  1791. */
  1792. ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VR;
  1793. ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
  1794. ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VB;
  1795. ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_VVE;
  1796. ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
  1797. ring->semaphore.mbox.signal[RCS] = GEN6_RVSYNC;
  1798. ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
  1799. ring->semaphore.mbox.signal[BCS] = GEN6_BVSYNC;
  1800. ring->semaphore.mbox.signal[VECS] = GEN6_VEVSYNC;
  1801. ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
  1802. } else {
  1803. ring->mmio_base = BSD_RING_BASE;
  1804. ring->flush = bsd_ring_flush;
  1805. ring->add_request = i9xx_add_request;
  1806. ring->get_seqno = ring_get_seqno;
  1807. ring->set_seqno = ring_set_seqno;
  1808. if (IS_GEN5(dev)) {
  1809. ring->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
  1810. ring->irq_get = gen5_ring_get_irq;
  1811. ring->irq_put = gen5_ring_put_irq;
  1812. } else {
  1813. ring->irq_enable_mask = I915_BSD_USER_INTERRUPT;
  1814. ring->irq_get = i9xx_ring_get_irq;
  1815. ring->irq_put = i9xx_ring_put_irq;
  1816. }
  1817. ring->dispatch_execbuffer = i965_dispatch_execbuffer;
  1818. }
  1819. ring->init = init_ring_common;
  1820. return intel_init_ring_buffer(dev, ring);
  1821. }
  1822. /**
  1823. * Initialize the second BSD ring for Broadwell GT3.
  1824. * It is noted that this only exists on Broadwell GT3.
  1825. */
  1826. int intel_init_bsd2_ring_buffer(struct drm_device *dev)
  1827. {
  1828. struct drm_i915_private *dev_priv = dev->dev_private;
  1829. struct intel_ring_buffer *ring = &dev_priv->ring[VCS2];
  1830. if ((INTEL_INFO(dev)->gen != 8)) {
  1831. DRM_ERROR("No dual-BSD ring on non-BDW machine\n");
  1832. return -EINVAL;
  1833. }
  1834. ring->name = "bds2_ring";
  1835. ring->id = VCS2;
  1836. ring->write_tail = ring_write_tail;
  1837. ring->mmio_base = GEN8_BSD2_RING_BASE;
  1838. ring->flush = gen6_bsd_ring_flush;
  1839. ring->add_request = gen6_add_request;
  1840. ring->get_seqno = gen6_ring_get_seqno;
  1841. ring->set_seqno = ring_set_seqno;
  1842. ring->irq_enable_mask =
  1843. GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  1844. ring->irq_get = gen8_ring_get_irq;
  1845. ring->irq_put = gen8_ring_put_irq;
  1846. ring->dispatch_execbuffer =
  1847. gen8_ring_dispatch_execbuffer;
  1848. ring->semaphore.sync_to = gen6_ring_sync;
  1849. /*
  1850. * The current semaphore is only applied on the pre-gen8. And there
  1851. * is no bsd2 ring on the pre-gen8. So now the semaphore_register
  1852. * between VCS2 and other ring is initialized as invalid.
  1853. * Gen8 will initialize the sema between VCS2 and other ring later.
  1854. */
  1855. ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_INVALID;
  1856. ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_INVALID;
  1857. ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
  1858. ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
  1859. ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
  1860. ring->semaphore.mbox.signal[RCS] = GEN6_NOSYNC;
  1861. ring->semaphore.mbox.signal[VCS] = GEN6_NOSYNC;
  1862. ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
  1863. ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
  1864. ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
  1865. ring->init = init_ring_common;
  1866. return intel_init_ring_buffer(dev, ring);
  1867. }
  1868. int intel_init_blt_ring_buffer(struct drm_device *dev)
  1869. {
  1870. struct drm_i915_private *dev_priv = dev->dev_private;
  1871. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  1872. ring->name = "blitter ring";
  1873. ring->id = BCS;
  1874. ring->mmio_base = BLT_RING_BASE;
  1875. ring->write_tail = ring_write_tail;
  1876. ring->flush = gen6_ring_flush;
  1877. ring->add_request = gen6_add_request;
  1878. ring->get_seqno = gen6_ring_get_seqno;
  1879. ring->set_seqno = ring_set_seqno;
  1880. if (INTEL_INFO(dev)->gen >= 8) {
  1881. ring->irq_enable_mask =
  1882. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  1883. ring->irq_get = gen8_ring_get_irq;
  1884. ring->irq_put = gen8_ring_put_irq;
  1885. ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
  1886. } else {
  1887. ring->irq_enable_mask = GT_BLT_USER_INTERRUPT;
  1888. ring->irq_get = gen6_ring_get_irq;
  1889. ring->irq_put = gen6_ring_put_irq;
  1890. ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
  1891. }
  1892. ring->semaphore.sync_to = gen6_ring_sync;
  1893. ring->semaphore.signal = gen6_signal;
  1894. /*
  1895. * The current semaphore is only applied on pre-gen8 platform. And
  1896. * there is no VCS2 ring on the pre-gen8 platform. So the semaphore
  1897. * between BCS and VCS2 is initialized as INVALID.
  1898. * Gen8 will initialize the sema between BCS and VCS2 later.
  1899. */
  1900. ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_BR;
  1901. ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_BV;
  1902. ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_INVALID;
  1903. ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_BVE;
  1904. ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
  1905. ring->semaphore.mbox.signal[RCS] = GEN6_RBSYNC;
  1906. ring->semaphore.mbox.signal[VCS] = GEN6_VBSYNC;
  1907. ring->semaphore.mbox.signal[BCS] = GEN6_NOSYNC;
  1908. ring->semaphore.mbox.signal[VECS] = GEN6_VEBSYNC;
  1909. ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
  1910. ring->init = init_ring_common;
  1911. return intel_init_ring_buffer(dev, ring);
  1912. }
  1913. int intel_init_vebox_ring_buffer(struct drm_device *dev)
  1914. {
  1915. struct drm_i915_private *dev_priv = dev->dev_private;
  1916. struct intel_ring_buffer *ring = &dev_priv->ring[VECS];
  1917. ring->name = "video enhancement ring";
  1918. ring->id = VECS;
  1919. ring->mmio_base = VEBOX_RING_BASE;
  1920. ring->write_tail = ring_write_tail;
  1921. ring->flush = gen6_ring_flush;
  1922. ring->add_request = gen6_add_request;
  1923. ring->get_seqno = gen6_ring_get_seqno;
  1924. ring->set_seqno = ring_set_seqno;
  1925. if (INTEL_INFO(dev)->gen >= 8) {
  1926. ring->irq_enable_mask =
  1927. GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  1928. ring->irq_get = gen8_ring_get_irq;
  1929. ring->irq_put = gen8_ring_put_irq;
  1930. ring->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
  1931. } else {
  1932. ring->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
  1933. ring->irq_get = hsw_vebox_get_irq;
  1934. ring->irq_put = hsw_vebox_put_irq;
  1935. ring->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
  1936. }
  1937. ring->semaphore.sync_to = gen6_ring_sync;
  1938. ring->semaphore.signal = gen6_signal;
  1939. ring->semaphore.mbox.wait[RCS] = MI_SEMAPHORE_SYNC_VER;
  1940. ring->semaphore.mbox.wait[VCS] = MI_SEMAPHORE_SYNC_VEV;
  1941. ring->semaphore.mbox.wait[BCS] = MI_SEMAPHORE_SYNC_VEB;
  1942. ring->semaphore.mbox.wait[VECS] = MI_SEMAPHORE_SYNC_INVALID;
  1943. ring->semaphore.mbox.wait[VCS2] = MI_SEMAPHORE_SYNC_INVALID;
  1944. ring->semaphore.mbox.signal[RCS] = GEN6_RVESYNC;
  1945. ring->semaphore.mbox.signal[VCS] = GEN6_VVESYNC;
  1946. ring->semaphore.mbox.signal[BCS] = GEN6_BVESYNC;
  1947. ring->semaphore.mbox.signal[VECS] = GEN6_NOSYNC;
  1948. ring->semaphore.mbox.signal[VCS2] = GEN6_NOSYNC;
  1949. ring->init = init_ring_common;
  1950. return intel_init_ring_buffer(dev, ring);
  1951. }
  1952. int
  1953. intel_ring_flush_all_caches(struct intel_ring_buffer *ring)
  1954. {
  1955. int ret;
  1956. if (!ring->gpu_caches_dirty)
  1957. return 0;
  1958. ret = ring->flush(ring, 0, I915_GEM_GPU_DOMAINS);
  1959. if (ret)
  1960. return ret;
  1961. trace_i915_gem_ring_flush(ring, 0, I915_GEM_GPU_DOMAINS);
  1962. ring->gpu_caches_dirty = false;
  1963. return 0;
  1964. }
  1965. int
  1966. intel_ring_invalidate_all_caches(struct intel_ring_buffer *ring)
  1967. {
  1968. uint32_t flush_domains;
  1969. int ret;
  1970. flush_domains = 0;
  1971. if (ring->gpu_caches_dirty)
  1972. flush_domains = I915_GEM_GPU_DOMAINS;
  1973. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
  1974. if (ret)
  1975. return ret;
  1976. trace_i915_gem_ring_flush(ring, I915_GEM_GPU_DOMAINS, flush_domains);
  1977. ring->gpu_caches_dirty = false;
  1978. return 0;
  1979. }
  1980. void
  1981. intel_stop_ring_buffer(struct intel_ring_buffer *ring)
  1982. {
  1983. int ret;
  1984. if (!intel_ring_initialized(ring))
  1985. return;
  1986. ret = intel_ring_idle(ring);
  1987. if (ret && !i915_reset_in_progress(&to_i915(ring->dev)->gpu_error))
  1988. DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
  1989. ring->name, ret);
  1990. stop_ring(ring);
  1991. }