intel_ringbuffer.c 58 KB

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