intel_ringbuffer.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082
  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. 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_ringbuffer *ringbuf)
  47. {
  48. if (ringbuf->last_retired_head != -1) {
  49. ringbuf->head = ringbuf->last_retired_head;
  50. ringbuf->last_retired_head = -1;
  51. }
  52. ringbuf->space = __intel_ring_space(ringbuf->head & HEAD_ADDR,
  53. ringbuf->tail, ringbuf->size);
  54. }
  55. bool intel_engine_stopped(struct intel_engine_cs *engine)
  56. {
  57. struct drm_i915_private *dev_priv = engine->i915;
  58. return dev_priv->gpu_error.stop_rings & intel_engine_flag(engine);
  59. }
  60. static void __intel_ring_advance(struct intel_engine_cs *engine)
  61. {
  62. struct intel_ringbuffer *ringbuf = engine->buffer;
  63. ringbuf->tail &= ringbuf->size - 1;
  64. if (intel_engine_stopped(engine))
  65. return;
  66. engine->write_tail(engine, ringbuf->tail);
  67. }
  68. static int
  69. gen2_render_ring_flush(struct drm_i915_gem_request *req,
  70. u32 invalidate_domains,
  71. u32 flush_domains)
  72. {
  73. struct intel_engine_cs *engine = req->engine;
  74. u32 cmd;
  75. int ret;
  76. cmd = MI_FLUSH;
  77. if (((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER) == 0)
  78. cmd |= MI_NO_WRITE_FLUSH;
  79. if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER)
  80. cmd |= MI_READ_FLUSH;
  81. ret = intel_ring_begin(req, 2);
  82. if (ret)
  83. return ret;
  84. intel_ring_emit(engine, cmd);
  85. intel_ring_emit(engine, MI_NOOP);
  86. intel_ring_advance(engine);
  87. return 0;
  88. }
  89. static int
  90. gen4_render_ring_flush(struct drm_i915_gem_request *req,
  91. u32 invalidate_domains,
  92. u32 flush_domains)
  93. {
  94. struct intel_engine_cs *engine = req->engine;
  95. u32 cmd;
  96. int ret;
  97. /*
  98. * read/write caches:
  99. *
  100. * I915_GEM_DOMAIN_RENDER is always invalidated, but is
  101. * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is
  102. * also flushed at 2d versus 3d pipeline switches.
  103. *
  104. * read-only caches:
  105. *
  106. * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if
  107. * MI_READ_FLUSH is set, and is always flushed on 965.
  108. *
  109. * I915_GEM_DOMAIN_COMMAND may not exist?
  110. *
  111. * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is
  112. * invalidated when MI_EXE_FLUSH is set.
  113. *
  114. * I915_GEM_DOMAIN_VERTEX, which exists on 965, is
  115. * invalidated with every MI_FLUSH.
  116. *
  117. * TLBs:
  118. *
  119. * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND
  120. * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and
  121. * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER
  122. * are flushed at any MI_FLUSH.
  123. */
  124. cmd = MI_FLUSH | MI_NO_WRITE_FLUSH;
  125. if ((invalidate_domains|flush_domains) & I915_GEM_DOMAIN_RENDER)
  126. cmd &= ~MI_NO_WRITE_FLUSH;
  127. if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION)
  128. cmd |= MI_EXE_FLUSH;
  129. if (invalidate_domains & I915_GEM_DOMAIN_COMMAND &&
  130. (IS_G4X(req->i915) || IS_GEN5(req->i915)))
  131. cmd |= MI_INVALIDATE_ISP;
  132. ret = intel_ring_begin(req, 2);
  133. if (ret)
  134. return ret;
  135. intel_ring_emit(engine, cmd);
  136. intel_ring_emit(engine, MI_NOOP);
  137. intel_ring_advance(engine);
  138. return 0;
  139. }
  140. /**
  141. * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
  142. * implementing two workarounds on gen6. From section 1.4.7.1
  143. * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
  144. *
  145. * [DevSNB-C+{W/A}] Before any depth stall flush (including those
  146. * produced by non-pipelined state commands), software needs to first
  147. * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
  148. * 0.
  149. *
  150. * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
  151. * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
  152. *
  153. * And the workaround for these two requires this workaround first:
  154. *
  155. * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
  156. * BEFORE the pipe-control with a post-sync op and no write-cache
  157. * flushes.
  158. *
  159. * And this last workaround is tricky because of the requirements on
  160. * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
  161. * volume 2 part 1:
  162. *
  163. * "1 of the following must also be set:
  164. * - Render Target Cache Flush Enable ([12] of DW1)
  165. * - Depth Cache Flush Enable ([0] of DW1)
  166. * - Stall at Pixel Scoreboard ([1] of DW1)
  167. * - Depth Stall ([13] of DW1)
  168. * - Post-Sync Operation ([13] of DW1)
  169. * - Notify Enable ([8] of DW1)"
  170. *
  171. * The cache flushes require the workaround flush that triggered this
  172. * one, so we can't use it. Depth stall would trigger the same.
  173. * Post-sync nonzero is what triggered this second workaround, so we
  174. * can't use that one either. Notify enable is IRQs, which aren't
  175. * really our business. That leaves only stall at scoreboard.
  176. */
  177. static int
  178. intel_emit_post_sync_nonzero_flush(struct drm_i915_gem_request *req)
  179. {
  180. struct intel_engine_cs *engine = req->engine;
  181. u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  182. int ret;
  183. ret = intel_ring_begin(req, 6);
  184. if (ret)
  185. return ret;
  186. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(5));
  187. intel_ring_emit(engine, PIPE_CONTROL_CS_STALL |
  188. PIPE_CONTROL_STALL_AT_SCOREBOARD);
  189. intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
  190. intel_ring_emit(engine, 0); /* low dword */
  191. intel_ring_emit(engine, 0); /* high dword */
  192. intel_ring_emit(engine, MI_NOOP);
  193. intel_ring_advance(engine);
  194. ret = intel_ring_begin(req, 6);
  195. if (ret)
  196. return ret;
  197. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(5));
  198. intel_ring_emit(engine, PIPE_CONTROL_QW_WRITE);
  199. intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT); /* address */
  200. intel_ring_emit(engine, 0);
  201. intel_ring_emit(engine, 0);
  202. intel_ring_emit(engine, MI_NOOP);
  203. intel_ring_advance(engine);
  204. return 0;
  205. }
  206. static int
  207. gen6_render_ring_flush(struct drm_i915_gem_request *req,
  208. u32 invalidate_domains, u32 flush_domains)
  209. {
  210. struct intel_engine_cs *engine = req->engine;
  211. u32 flags = 0;
  212. u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  213. int ret;
  214. /* Force SNB workarounds for PIPE_CONTROL flushes */
  215. ret = intel_emit_post_sync_nonzero_flush(req);
  216. if (ret)
  217. return ret;
  218. /* Just flush everything. Experiments have shown that reducing the
  219. * number of bits based on the write domains has little performance
  220. * impact.
  221. */
  222. if (flush_domains) {
  223. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  224. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  225. /*
  226. * Ensure that any following seqno writes only happen
  227. * when the render cache is indeed flushed.
  228. */
  229. flags |= PIPE_CONTROL_CS_STALL;
  230. }
  231. if (invalidate_domains) {
  232. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  233. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  234. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  235. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  236. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  237. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  238. /*
  239. * TLB invalidate requires a post-sync write.
  240. */
  241. flags |= PIPE_CONTROL_QW_WRITE | PIPE_CONTROL_CS_STALL;
  242. }
  243. ret = intel_ring_begin(req, 4);
  244. if (ret)
  245. return ret;
  246. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
  247. intel_ring_emit(engine, flags);
  248. intel_ring_emit(engine, scratch_addr | PIPE_CONTROL_GLOBAL_GTT);
  249. intel_ring_emit(engine, 0);
  250. intel_ring_advance(engine);
  251. return 0;
  252. }
  253. static int
  254. gen7_render_ring_cs_stall_wa(struct drm_i915_gem_request *req)
  255. {
  256. struct intel_engine_cs *engine = req->engine;
  257. int ret;
  258. ret = intel_ring_begin(req, 4);
  259. if (ret)
  260. return ret;
  261. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
  262. intel_ring_emit(engine, PIPE_CONTROL_CS_STALL |
  263. PIPE_CONTROL_STALL_AT_SCOREBOARD);
  264. intel_ring_emit(engine, 0);
  265. intel_ring_emit(engine, 0);
  266. intel_ring_advance(engine);
  267. return 0;
  268. }
  269. static int
  270. gen7_render_ring_flush(struct drm_i915_gem_request *req,
  271. u32 invalidate_domains, u32 flush_domains)
  272. {
  273. struct intel_engine_cs *engine = req->engine;
  274. u32 flags = 0;
  275. u32 scratch_addr = engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  276. int ret;
  277. /*
  278. * Ensure that any following seqno writes only happen when the render
  279. * cache is indeed flushed.
  280. *
  281. * Workaround: 4th PIPE_CONTROL command (except the ones with only
  282. * read-cache invalidate bits set) must have the CS_STALL bit set. We
  283. * don't try to be clever and just set it unconditionally.
  284. */
  285. flags |= PIPE_CONTROL_CS_STALL;
  286. /* Just flush everything. Experiments have shown that reducing the
  287. * number of bits based on the write domains has little performance
  288. * impact.
  289. */
  290. if (flush_domains) {
  291. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  292. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  293. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  294. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  295. }
  296. if (invalidate_domains) {
  297. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  298. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  299. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  300. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  301. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  302. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  303. flags |= PIPE_CONTROL_MEDIA_STATE_CLEAR;
  304. /*
  305. * TLB invalidate requires a post-sync write.
  306. */
  307. flags |= PIPE_CONTROL_QW_WRITE;
  308. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  309. flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
  310. /* Workaround: we must issue a pipe_control with CS-stall bit
  311. * set before a pipe_control command that has the state cache
  312. * invalidate bit set. */
  313. gen7_render_ring_cs_stall_wa(req);
  314. }
  315. ret = intel_ring_begin(req, 4);
  316. if (ret)
  317. return ret;
  318. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(4));
  319. intel_ring_emit(engine, flags);
  320. intel_ring_emit(engine, scratch_addr);
  321. intel_ring_emit(engine, 0);
  322. intel_ring_advance(engine);
  323. return 0;
  324. }
  325. static int
  326. gen8_emit_pipe_control(struct drm_i915_gem_request *req,
  327. u32 flags, u32 scratch_addr)
  328. {
  329. struct intel_engine_cs *engine = req->engine;
  330. int ret;
  331. ret = intel_ring_begin(req, 6);
  332. if (ret)
  333. return ret;
  334. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(6));
  335. intel_ring_emit(engine, flags);
  336. intel_ring_emit(engine, scratch_addr);
  337. intel_ring_emit(engine, 0);
  338. intel_ring_emit(engine, 0);
  339. intel_ring_emit(engine, 0);
  340. intel_ring_advance(engine);
  341. return 0;
  342. }
  343. static int
  344. gen8_render_ring_flush(struct drm_i915_gem_request *req,
  345. u32 invalidate_domains, u32 flush_domains)
  346. {
  347. u32 flags = 0;
  348. u32 scratch_addr = req->engine->scratch.gtt_offset + 2 * CACHELINE_BYTES;
  349. int ret;
  350. flags |= PIPE_CONTROL_CS_STALL;
  351. if (flush_domains) {
  352. flags |= PIPE_CONTROL_RENDER_TARGET_CACHE_FLUSH;
  353. flags |= PIPE_CONTROL_DEPTH_CACHE_FLUSH;
  354. flags |= PIPE_CONTROL_DC_FLUSH_ENABLE;
  355. flags |= PIPE_CONTROL_FLUSH_ENABLE;
  356. }
  357. if (invalidate_domains) {
  358. flags |= PIPE_CONTROL_TLB_INVALIDATE;
  359. flags |= PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE;
  360. flags |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
  361. flags |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
  362. flags |= PIPE_CONTROL_CONST_CACHE_INVALIDATE;
  363. flags |= PIPE_CONTROL_STATE_CACHE_INVALIDATE;
  364. flags |= PIPE_CONTROL_QW_WRITE;
  365. flags |= PIPE_CONTROL_GLOBAL_GTT_IVB;
  366. /* WaCsStallBeforeStateCacheInvalidate:bdw,chv */
  367. ret = gen8_emit_pipe_control(req,
  368. PIPE_CONTROL_CS_STALL |
  369. PIPE_CONTROL_STALL_AT_SCOREBOARD,
  370. 0);
  371. if (ret)
  372. return ret;
  373. }
  374. return gen8_emit_pipe_control(req, flags, scratch_addr);
  375. }
  376. static void ring_write_tail(struct intel_engine_cs *engine,
  377. u32 value)
  378. {
  379. struct drm_i915_private *dev_priv = engine->i915;
  380. I915_WRITE_TAIL(engine, value);
  381. }
  382. u64 intel_ring_get_active_head(struct intel_engine_cs *engine)
  383. {
  384. struct drm_i915_private *dev_priv = engine->i915;
  385. u64 acthd;
  386. if (INTEL_GEN(dev_priv) >= 8)
  387. acthd = I915_READ64_2x32(RING_ACTHD(engine->mmio_base),
  388. RING_ACTHD_UDW(engine->mmio_base));
  389. else if (INTEL_GEN(dev_priv) >= 4)
  390. acthd = I915_READ(RING_ACTHD(engine->mmio_base));
  391. else
  392. acthd = I915_READ(ACTHD);
  393. return acthd;
  394. }
  395. static void ring_setup_phys_status_page(struct intel_engine_cs *engine)
  396. {
  397. struct drm_i915_private *dev_priv = engine->i915;
  398. u32 addr;
  399. addr = dev_priv->status_page_dmah->busaddr;
  400. if (INTEL_GEN(dev_priv) >= 4)
  401. addr |= (dev_priv->status_page_dmah->busaddr >> 28) & 0xf0;
  402. I915_WRITE(HWS_PGA, addr);
  403. }
  404. static void intel_ring_setup_status_page(struct intel_engine_cs *engine)
  405. {
  406. struct drm_i915_private *dev_priv = engine->i915;
  407. i915_reg_t mmio;
  408. /* The ring status page addresses are no longer next to the rest of
  409. * the ring registers as of gen7.
  410. */
  411. if (IS_GEN7(dev_priv)) {
  412. switch (engine->id) {
  413. case RCS:
  414. mmio = RENDER_HWS_PGA_GEN7;
  415. break;
  416. case BCS:
  417. mmio = BLT_HWS_PGA_GEN7;
  418. break;
  419. /*
  420. * VCS2 actually doesn't exist on Gen7. Only shut up
  421. * gcc switch check warning
  422. */
  423. case VCS2:
  424. case VCS:
  425. mmio = BSD_HWS_PGA_GEN7;
  426. break;
  427. case VECS:
  428. mmio = VEBOX_HWS_PGA_GEN7;
  429. break;
  430. }
  431. } else if (IS_GEN6(dev_priv)) {
  432. mmio = RING_HWS_PGA_GEN6(engine->mmio_base);
  433. } else {
  434. /* XXX: gen8 returns to sanity */
  435. mmio = RING_HWS_PGA(engine->mmio_base);
  436. }
  437. I915_WRITE(mmio, (u32)engine->status_page.gfx_addr);
  438. POSTING_READ(mmio);
  439. /*
  440. * Flush the TLB for this page
  441. *
  442. * FIXME: These two bits have disappeared on gen8, so a question
  443. * arises: do we still need this and if so how should we go about
  444. * invalidating the TLB?
  445. */
  446. if (IS_GEN(dev_priv, 6, 7)) {
  447. i915_reg_t reg = RING_INSTPM(engine->mmio_base);
  448. /* ring should be idle before issuing a sync flush*/
  449. WARN_ON((I915_READ_MODE(engine) & MODE_IDLE) == 0);
  450. I915_WRITE(reg,
  451. _MASKED_BIT_ENABLE(INSTPM_TLB_INVALIDATE |
  452. INSTPM_SYNC_FLUSH));
  453. if (intel_wait_for_register(dev_priv,
  454. reg, INSTPM_SYNC_FLUSH, 0,
  455. 1000))
  456. DRM_ERROR("%s: wait for SyncFlush to complete for TLB invalidation timed out\n",
  457. engine->name);
  458. }
  459. }
  460. static bool stop_ring(struct intel_engine_cs *engine)
  461. {
  462. struct drm_i915_private *dev_priv = engine->i915;
  463. if (!IS_GEN2(dev_priv)) {
  464. I915_WRITE_MODE(engine, _MASKED_BIT_ENABLE(STOP_RING));
  465. if (intel_wait_for_register(dev_priv,
  466. RING_MI_MODE(engine->mmio_base),
  467. MODE_IDLE,
  468. MODE_IDLE,
  469. 1000)) {
  470. DRM_ERROR("%s : timed out trying to stop ring\n",
  471. engine->name);
  472. /* Sometimes we observe that the idle flag is not
  473. * set even though the ring is empty. So double
  474. * check before giving up.
  475. */
  476. if (I915_READ_HEAD(engine) != I915_READ_TAIL(engine))
  477. return false;
  478. }
  479. }
  480. I915_WRITE_CTL(engine, 0);
  481. I915_WRITE_HEAD(engine, 0);
  482. engine->write_tail(engine, 0);
  483. if (!IS_GEN2(dev_priv)) {
  484. (void)I915_READ_CTL(engine);
  485. I915_WRITE_MODE(engine, _MASKED_BIT_DISABLE(STOP_RING));
  486. }
  487. return (I915_READ_HEAD(engine) & HEAD_ADDR) == 0;
  488. }
  489. void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
  490. {
  491. memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
  492. }
  493. static int init_ring_common(struct intel_engine_cs *engine)
  494. {
  495. struct drm_i915_private *dev_priv = engine->i915;
  496. struct intel_ringbuffer *ringbuf = engine->buffer;
  497. struct drm_i915_gem_object *obj = ringbuf->obj;
  498. int ret = 0;
  499. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  500. if (!stop_ring(engine)) {
  501. /* G45 ring initialization often fails to reset head to zero */
  502. DRM_DEBUG_KMS("%s head not reset to zero "
  503. "ctl %08x head %08x tail %08x start %08x\n",
  504. engine->name,
  505. I915_READ_CTL(engine),
  506. I915_READ_HEAD(engine),
  507. I915_READ_TAIL(engine),
  508. I915_READ_START(engine));
  509. if (!stop_ring(engine)) {
  510. DRM_ERROR("failed to set %s head to zero "
  511. "ctl %08x head %08x tail %08x start %08x\n",
  512. engine->name,
  513. I915_READ_CTL(engine),
  514. I915_READ_HEAD(engine),
  515. I915_READ_TAIL(engine),
  516. I915_READ_START(engine));
  517. ret = -EIO;
  518. goto out;
  519. }
  520. }
  521. if (I915_NEED_GFX_HWS(dev_priv))
  522. intel_ring_setup_status_page(engine);
  523. else
  524. ring_setup_phys_status_page(engine);
  525. /* Enforce ordering by reading HEAD register back */
  526. I915_READ_HEAD(engine);
  527. /* Initialize the ring. This must happen _after_ we've cleared the ring
  528. * registers with the above sequence (the readback of the HEAD registers
  529. * also enforces ordering), otherwise the hw might lose the new ring
  530. * register values. */
  531. I915_WRITE_START(engine, i915_gem_obj_ggtt_offset(obj));
  532. /* WaClearRingBufHeadRegAtInit:ctg,elk */
  533. if (I915_READ_HEAD(engine))
  534. DRM_DEBUG("%s initialization failed [head=%08x], fudging\n",
  535. engine->name, I915_READ_HEAD(engine));
  536. I915_WRITE_HEAD(engine, 0);
  537. (void)I915_READ_HEAD(engine);
  538. I915_WRITE_CTL(engine,
  539. ((ringbuf->size - PAGE_SIZE) & RING_NR_PAGES)
  540. | RING_VALID);
  541. /* If the head is still not zero, the ring is dead */
  542. if (wait_for((I915_READ_CTL(engine) & RING_VALID) != 0 &&
  543. I915_READ_START(engine) == i915_gem_obj_ggtt_offset(obj) &&
  544. (I915_READ_HEAD(engine) & HEAD_ADDR) == 0, 50)) {
  545. DRM_ERROR("%s initialization failed "
  546. "ctl %08x (valid? %d) head %08x tail %08x start %08x [expected %08lx]\n",
  547. engine->name,
  548. I915_READ_CTL(engine),
  549. I915_READ_CTL(engine) & RING_VALID,
  550. I915_READ_HEAD(engine), I915_READ_TAIL(engine),
  551. I915_READ_START(engine),
  552. (unsigned long)i915_gem_obj_ggtt_offset(obj));
  553. ret = -EIO;
  554. goto out;
  555. }
  556. ringbuf->last_retired_head = -1;
  557. ringbuf->head = I915_READ_HEAD(engine);
  558. ringbuf->tail = I915_READ_TAIL(engine) & TAIL_ADDR;
  559. intel_ring_update_space(ringbuf);
  560. intel_engine_init_hangcheck(engine);
  561. out:
  562. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  563. return ret;
  564. }
  565. void intel_fini_pipe_control(struct intel_engine_cs *engine)
  566. {
  567. if (engine->scratch.obj == NULL)
  568. return;
  569. i915_gem_object_ggtt_unpin(engine->scratch.obj);
  570. drm_gem_object_unreference(&engine->scratch.obj->base);
  571. engine->scratch.obj = NULL;
  572. }
  573. int intel_init_pipe_control(struct intel_engine_cs *engine, int size)
  574. {
  575. struct drm_i915_gem_object *obj;
  576. int ret;
  577. WARN_ON(engine->scratch.obj);
  578. obj = i915_gem_object_create_stolen(engine->i915->dev, size);
  579. if (!obj)
  580. obj = i915_gem_object_create(engine->i915->dev, size);
  581. if (IS_ERR(obj)) {
  582. DRM_ERROR("Failed to allocate scratch page\n");
  583. ret = PTR_ERR(obj);
  584. goto err;
  585. }
  586. ret = i915_gem_obj_ggtt_pin(obj, 4096, PIN_HIGH);
  587. if (ret)
  588. goto err_unref;
  589. engine->scratch.obj = obj;
  590. engine->scratch.gtt_offset = i915_gem_obj_ggtt_offset(obj);
  591. DRM_DEBUG_DRIVER("%s pipe control offset: 0x%08x\n",
  592. engine->name, engine->scratch.gtt_offset);
  593. return 0;
  594. err_unref:
  595. drm_gem_object_unreference(&engine->scratch.obj->base);
  596. err:
  597. return ret;
  598. }
  599. static int intel_ring_workarounds_emit(struct drm_i915_gem_request *req)
  600. {
  601. struct intel_engine_cs *engine = req->engine;
  602. struct i915_workarounds *w = &req->i915->workarounds;
  603. int ret, i;
  604. if (w->count == 0)
  605. return 0;
  606. engine->gpu_caches_dirty = true;
  607. ret = intel_ring_flush_all_caches(req);
  608. if (ret)
  609. return ret;
  610. ret = intel_ring_begin(req, (w->count * 2 + 2));
  611. if (ret)
  612. return ret;
  613. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(w->count));
  614. for (i = 0; i < w->count; i++) {
  615. intel_ring_emit_reg(engine, w->reg[i].addr);
  616. intel_ring_emit(engine, w->reg[i].value);
  617. }
  618. intel_ring_emit(engine, MI_NOOP);
  619. intel_ring_advance(engine);
  620. engine->gpu_caches_dirty = true;
  621. ret = intel_ring_flush_all_caches(req);
  622. if (ret)
  623. return ret;
  624. DRM_DEBUG_DRIVER("Number of Workarounds emitted: %d\n", w->count);
  625. return 0;
  626. }
  627. static int intel_rcs_ctx_init(struct drm_i915_gem_request *req)
  628. {
  629. int ret;
  630. ret = intel_ring_workarounds_emit(req);
  631. if (ret != 0)
  632. return ret;
  633. ret = i915_gem_render_state_init(req);
  634. if (ret)
  635. return ret;
  636. return 0;
  637. }
  638. static int wa_add(struct drm_i915_private *dev_priv,
  639. i915_reg_t addr,
  640. const u32 mask, const u32 val)
  641. {
  642. const u32 idx = dev_priv->workarounds.count;
  643. if (WARN_ON(idx >= I915_MAX_WA_REGS))
  644. return -ENOSPC;
  645. dev_priv->workarounds.reg[idx].addr = addr;
  646. dev_priv->workarounds.reg[idx].value = val;
  647. dev_priv->workarounds.reg[idx].mask = mask;
  648. dev_priv->workarounds.count++;
  649. return 0;
  650. }
  651. #define WA_REG(addr, mask, val) do { \
  652. const int r = wa_add(dev_priv, (addr), (mask), (val)); \
  653. if (r) \
  654. return r; \
  655. } while (0)
  656. #define WA_SET_BIT_MASKED(addr, mask) \
  657. WA_REG(addr, (mask), _MASKED_BIT_ENABLE(mask))
  658. #define WA_CLR_BIT_MASKED(addr, mask) \
  659. WA_REG(addr, (mask), _MASKED_BIT_DISABLE(mask))
  660. #define WA_SET_FIELD_MASKED(addr, mask, value) \
  661. WA_REG(addr, mask, _MASKED_FIELD(mask, value))
  662. #define WA_SET_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) | (mask))
  663. #define WA_CLR_BIT(addr, mask) WA_REG(addr, mask, I915_READ(addr) & ~(mask))
  664. #define WA_WRITE(addr, val) WA_REG(addr, 0xffffffff, val)
  665. static int wa_ring_whitelist_reg(struct intel_engine_cs *engine,
  666. i915_reg_t reg)
  667. {
  668. struct drm_i915_private *dev_priv = engine->i915;
  669. struct i915_workarounds *wa = &dev_priv->workarounds;
  670. const uint32_t index = wa->hw_whitelist_count[engine->id];
  671. if (WARN_ON(index >= RING_MAX_NONPRIV_SLOTS))
  672. return -EINVAL;
  673. WA_WRITE(RING_FORCE_TO_NONPRIV(engine->mmio_base, index),
  674. i915_mmio_reg_offset(reg));
  675. wa->hw_whitelist_count[engine->id]++;
  676. return 0;
  677. }
  678. static int gen8_init_workarounds(struct intel_engine_cs *engine)
  679. {
  680. struct drm_i915_private *dev_priv = engine->i915;
  681. WA_SET_BIT_MASKED(INSTPM, INSTPM_FORCE_ORDERING);
  682. /* WaDisableAsyncFlipPerfMode:bdw,chv */
  683. WA_SET_BIT_MASKED(MI_MODE, ASYNC_FLIP_PERF_DISABLE);
  684. /* WaDisablePartialInstShootdown:bdw,chv */
  685. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
  686. PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
  687. /* Use Force Non-Coherent whenever executing a 3D context. This is a
  688. * workaround for for a possible hang in the unlikely event a TLB
  689. * invalidation occurs during a PSD flush.
  690. */
  691. /* WaForceEnableNonCoherent:bdw,chv */
  692. /* WaHdcDisableFetchWhenMasked:bdw,chv */
  693. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  694. HDC_DONOT_FETCH_MEM_WHEN_MASKED |
  695. HDC_FORCE_NON_COHERENT);
  696. /* From the Haswell PRM, Command Reference: Registers, CACHE_MODE_0:
  697. * "The Hierarchical Z RAW Stall Optimization allows non-overlapping
  698. * polygons in the same 8x4 pixel/sample area to be processed without
  699. * stalling waiting for the earlier ones to write to Hierarchical Z
  700. * buffer."
  701. *
  702. * This optimization is off by default for BDW and CHV; turn it on.
  703. */
  704. WA_CLR_BIT_MASKED(CACHE_MODE_0_GEN7, HIZ_RAW_STALL_OPT_DISABLE);
  705. /* Wa4x4STCOptimizationDisable:bdw,chv */
  706. WA_SET_BIT_MASKED(CACHE_MODE_1, GEN8_4x4_STC_OPTIMIZATION_DISABLE);
  707. /*
  708. * BSpec recommends 8x4 when MSAA is used,
  709. * however in practice 16x4 seems fastest.
  710. *
  711. * Note that PS/WM thread counts depend on the WIZ hashing
  712. * disable bit, which we don't touch here, but it's good
  713. * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
  714. */
  715. WA_SET_FIELD_MASKED(GEN7_GT_MODE,
  716. GEN6_WIZ_HASHING_MASK,
  717. GEN6_WIZ_HASHING_16x4);
  718. return 0;
  719. }
  720. static int bdw_init_workarounds(struct intel_engine_cs *engine)
  721. {
  722. struct drm_i915_private *dev_priv = engine->i915;
  723. int ret;
  724. ret = gen8_init_workarounds(engine);
  725. if (ret)
  726. return ret;
  727. /* WaDisableThreadStallDopClockGating:bdw (pre-production) */
  728. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
  729. /* WaDisableDopClockGating:bdw */
  730. WA_SET_BIT_MASKED(GEN7_ROW_CHICKEN2,
  731. DOP_CLOCK_GATING_DISABLE);
  732. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
  733. GEN8_SAMPLER_POWER_BYPASS_DIS);
  734. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  735. /* WaForceContextSaveRestoreNonCoherent:bdw */
  736. HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
  737. /* WaDisableFenceDestinationToSLM:bdw (pre-prod) */
  738. (IS_BDW_GT3(dev_priv) ? HDC_FENCE_DEST_SLM_DISABLE : 0));
  739. return 0;
  740. }
  741. static int chv_init_workarounds(struct intel_engine_cs *engine)
  742. {
  743. struct drm_i915_private *dev_priv = engine->i915;
  744. int ret;
  745. ret = gen8_init_workarounds(engine);
  746. if (ret)
  747. return ret;
  748. /* WaDisableThreadStallDopClockGating:chv */
  749. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN, STALL_DOP_GATING_DISABLE);
  750. /* Improve HiZ throughput on CHV. */
  751. WA_SET_BIT_MASKED(HIZ_CHICKEN, CHV_HZ_8X8_MODE_IN_1X);
  752. return 0;
  753. }
  754. static int gen9_init_workarounds(struct intel_engine_cs *engine)
  755. {
  756. struct drm_i915_private *dev_priv = engine->i915;
  757. int ret;
  758. /* WaConextSwitchWithConcurrentTLBInvalidate:skl,bxt,kbl */
  759. I915_WRITE(GEN9_CSFE_CHICKEN1_RCS, _MASKED_BIT_ENABLE(GEN9_PREEMPT_GPGPU_SYNC_SWITCH_DISABLE));
  760. /* WaEnableLbsSlaRetryTimerDecrement:skl,bxt,kbl */
  761. I915_WRITE(BDW_SCRATCH1, I915_READ(BDW_SCRATCH1) |
  762. GEN9_LBS_SLA_RETRY_TIMER_DECREMENT_ENABLE);
  763. /* WaDisableKillLogic:bxt,skl,kbl */
  764. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
  765. ECOCHK_DIS_TLB);
  766. /* WaClearFlowControlGpgpuContextSave:skl,bxt,kbl */
  767. /* WaDisablePartialInstShootdown:skl,bxt,kbl */
  768. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
  769. FLOW_CONTROL_ENABLE |
  770. PARTIAL_INSTRUCTION_SHOOTDOWN_DISABLE);
  771. /* Syncing dependencies between camera and graphics:skl,bxt,kbl */
  772. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
  773. GEN9_DISABLE_OCL_OOB_SUPPRESS_LOGIC);
  774. /* WaDisableDgMirrorFixInHalfSliceChicken5:skl,bxt */
  775. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
  776. IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
  777. WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
  778. GEN9_DG_MIRROR_FIX_ENABLE);
  779. /* WaSetDisablePixMaskCammingAndRhwoInCommonSliceChicken:skl,bxt */
  780. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_B0) ||
  781. IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
  782. WA_SET_BIT_MASKED(GEN7_COMMON_SLICE_CHICKEN1,
  783. GEN9_RHWO_OPTIMIZATION_DISABLE);
  784. /*
  785. * WA also requires GEN9_SLICE_COMMON_ECO_CHICKEN0[14:14] to be set
  786. * but we do that in per ctx batchbuffer as there is an issue
  787. * with this register not getting restored on ctx restore
  788. */
  789. }
  790. /* WaEnableYV12BugFixInHalfSliceChicken7:skl,bxt,kbl */
  791. /* WaEnableSamplerGPGPUPreemptionSupport:skl,bxt,kbl */
  792. WA_SET_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN7,
  793. GEN9_ENABLE_YV12_BUGFIX |
  794. GEN9_ENABLE_GPGPU_PREEMPTION);
  795. /* Wa4x4STCOptimizationDisable:skl,bxt,kbl */
  796. /* WaDisablePartialResolveInVc:skl,bxt,kbl */
  797. WA_SET_BIT_MASKED(CACHE_MODE_1, (GEN8_4x4_STC_OPTIMIZATION_DISABLE |
  798. GEN9_PARTIAL_RESOLVE_IN_VC_DISABLE));
  799. /* WaCcsTlbPrefetchDisable:skl,bxt,kbl */
  800. WA_CLR_BIT_MASKED(GEN9_HALF_SLICE_CHICKEN5,
  801. GEN9_CCS_TLB_PREFETCH_ENABLE);
  802. /* WaDisableMaskBasedCammingInRCC:skl,bxt */
  803. if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_C0) ||
  804. IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
  805. WA_SET_BIT_MASKED(SLICE_ECO_CHICKEN0,
  806. PIXEL_MASK_CAMMING_DISABLE);
  807. /* WaForceContextSaveRestoreNonCoherent:skl,bxt,kbl */
  808. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  809. HDC_FORCE_CONTEXT_SAVE_RESTORE_NON_COHERENT |
  810. HDC_FORCE_CSR_NON_COHERENT_OVR_DISABLE);
  811. /* WaForceEnableNonCoherent and WaDisableHDCInvalidation are
  812. * both tied to WaForceContextSaveRestoreNonCoherent
  813. * in some hsds for skl. We keep the tie for all gen9. The
  814. * documentation is a bit hazy and so we want to get common behaviour,
  815. * even though there is no clear evidence we would need both on kbl/bxt.
  816. * This area has been source of system hangs so we play it safe
  817. * and mimic the skl regardless of what bspec says.
  818. *
  819. * Use Force Non-Coherent whenever executing a 3D context. This
  820. * is a workaround for a possible hang in the unlikely event
  821. * a TLB invalidation occurs during a PSD flush.
  822. */
  823. /* WaForceEnableNonCoherent:skl,bxt,kbl */
  824. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  825. HDC_FORCE_NON_COHERENT);
  826. /* WaDisableHDCInvalidation:skl,bxt,kbl */
  827. I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) |
  828. BDW_DISABLE_HDC_INVALIDATION);
  829. /* WaDisableSamplerPowerBypassForSOPingPong:skl,bxt,kbl */
  830. if (IS_SKYLAKE(dev_priv) ||
  831. IS_KABYLAKE(dev_priv) ||
  832. IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
  833. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN3,
  834. GEN8_SAMPLER_POWER_BYPASS_DIS);
  835. /* WaDisableSTUnitPowerOptimization:skl,bxt,kbl */
  836. WA_SET_BIT_MASKED(HALF_SLICE_CHICKEN2, GEN8_ST_PO_DISABLE);
  837. /* WaOCLCoherentLineFlush:skl,bxt,kbl */
  838. I915_WRITE(GEN8_L3SQCREG4, (I915_READ(GEN8_L3SQCREG4) |
  839. GEN8_LQSC_FLUSH_COHERENT_LINES));
  840. /* WaVFEStateAfterPipeControlwithMediaStateClear:skl,bxt */
  841. ret = wa_ring_whitelist_reg(engine, GEN9_CTX_PREEMPT_REG);
  842. if (ret)
  843. return ret;
  844. /* WaEnablePreemptionGranularityControlByUMD:skl,bxt,kbl */
  845. ret= wa_ring_whitelist_reg(engine, GEN8_CS_CHICKEN1);
  846. if (ret)
  847. return ret;
  848. /* WaAllowUMDToModifyHDCChicken1:skl,bxt,kbl */
  849. ret = wa_ring_whitelist_reg(engine, GEN8_HDC_CHICKEN1);
  850. if (ret)
  851. return ret;
  852. return 0;
  853. }
  854. static int skl_tune_iz_hashing(struct intel_engine_cs *engine)
  855. {
  856. struct drm_i915_private *dev_priv = engine->i915;
  857. u8 vals[3] = { 0, 0, 0 };
  858. unsigned int i;
  859. for (i = 0; i < 3; i++) {
  860. u8 ss;
  861. /*
  862. * Only consider slices where one, and only one, subslice has 7
  863. * EUs
  864. */
  865. if (!is_power_of_2(dev_priv->info.subslice_7eu[i]))
  866. continue;
  867. /*
  868. * subslice_7eu[i] != 0 (because of the check above) and
  869. * ss_max == 4 (maximum number of subslices possible per slice)
  870. *
  871. * -> 0 <= ss <= 3;
  872. */
  873. ss = ffs(dev_priv->info.subslice_7eu[i]) - 1;
  874. vals[i] = 3 - ss;
  875. }
  876. if (vals[0] == 0 && vals[1] == 0 && vals[2] == 0)
  877. return 0;
  878. /* Tune IZ hashing. See intel_device_info_runtime_init() */
  879. WA_SET_FIELD_MASKED(GEN7_GT_MODE,
  880. GEN9_IZ_HASHING_MASK(2) |
  881. GEN9_IZ_HASHING_MASK(1) |
  882. GEN9_IZ_HASHING_MASK(0),
  883. GEN9_IZ_HASHING(2, vals[2]) |
  884. GEN9_IZ_HASHING(1, vals[1]) |
  885. GEN9_IZ_HASHING(0, vals[0]));
  886. return 0;
  887. }
  888. static int skl_init_workarounds(struct intel_engine_cs *engine)
  889. {
  890. struct drm_i915_private *dev_priv = engine->i915;
  891. int ret;
  892. ret = gen9_init_workarounds(engine);
  893. if (ret)
  894. return ret;
  895. /*
  896. * Actual WA is to disable percontext preemption granularity control
  897. * until D0 which is the default case so this is equivalent to
  898. * !WaDisablePerCtxtPreemptionGranularityControl:skl
  899. */
  900. if (IS_SKL_REVID(dev_priv, SKL_REVID_E0, REVID_FOREVER)) {
  901. I915_WRITE(GEN7_FF_SLICE_CS_CHICKEN1,
  902. _MASKED_BIT_ENABLE(GEN9_FFSC_PERCTX_PREEMPT_CTRL));
  903. }
  904. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0)) {
  905. /* WaDisableChickenBitTSGBarrierAckForFFSliceCS:skl */
  906. I915_WRITE(FF_SLICE_CS_CHICKEN2,
  907. _MASKED_BIT_ENABLE(GEN9_TSG_BARRIER_ACK_DISABLE));
  908. }
  909. /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
  910. * involving this register should also be added to WA batch as required.
  911. */
  912. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_E0))
  913. /* WaDisableLSQCROPERFforOCL:skl */
  914. I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
  915. GEN8_LQSC_RO_PERF_DIS);
  916. /* WaEnableGapsTsvCreditFix:skl */
  917. if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, REVID_FOREVER)) {
  918. I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
  919. GEN9_GAPS_TSV_CREDIT_DISABLE));
  920. }
  921. /* WaDisablePowerCompilerClockGating:skl */
  922. if (IS_SKL_REVID(dev_priv, SKL_REVID_B0, SKL_REVID_B0))
  923. WA_SET_BIT_MASKED(HIZ_CHICKEN,
  924. BDW_HIZ_POWER_COMPILER_CLOCK_GATING_DISABLE);
  925. /* WaBarrierPerformanceFixDisable:skl */
  926. if (IS_SKL_REVID(dev_priv, SKL_REVID_C0, SKL_REVID_D0))
  927. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  928. HDC_FENCE_DEST_SLM_DISABLE |
  929. HDC_BARRIER_PERFORMANCE_DISABLE);
  930. /* WaDisableSbeCacheDispatchPortSharing:skl */
  931. if (IS_SKL_REVID(dev_priv, 0, SKL_REVID_F0))
  932. WA_SET_BIT_MASKED(
  933. GEN7_HALF_SLICE_CHICKEN1,
  934. GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
  935. /* WaDisableGafsUnitClkGating:skl */
  936. WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
  937. /* WaDisableLSQCROPERFforOCL:skl */
  938. ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
  939. if (ret)
  940. return ret;
  941. return skl_tune_iz_hashing(engine);
  942. }
  943. static int bxt_init_workarounds(struct intel_engine_cs *engine)
  944. {
  945. struct drm_i915_private *dev_priv = engine->i915;
  946. int ret;
  947. ret = gen9_init_workarounds(engine);
  948. if (ret)
  949. return ret;
  950. /* WaStoreMultiplePTEenable:bxt */
  951. /* This is a requirement according to Hardware specification */
  952. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1))
  953. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_TLBPF);
  954. /* WaSetClckGatingDisableMedia:bxt */
  955. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
  956. I915_WRITE(GEN7_MISCCPCTL, (I915_READ(GEN7_MISCCPCTL) &
  957. ~GEN8_DOP_CLOCK_GATE_MEDIA_ENABLE));
  958. }
  959. /* WaDisableThreadStallDopClockGating:bxt */
  960. WA_SET_BIT_MASKED(GEN8_ROW_CHICKEN,
  961. STALL_DOP_GATING_DISABLE);
  962. /* WaDisablePooledEuLoadBalancingFix:bxt */
  963. if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER)) {
  964. WA_SET_BIT_MASKED(FF_SLICE_CS_CHICKEN2,
  965. GEN9_POOLED_EU_LOAD_BALANCING_FIX_DISABLE);
  966. }
  967. /* WaDisableSbeCacheDispatchPortSharing:bxt */
  968. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0)) {
  969. WA_SET_BIT_MASKED(
  970. GEN7_HALF_SLICE_CHICKEN1,
  971. GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
  972. }
  973. /* WaDisableObjectLevelPreemptionForTrifanOrPolygon:bxt */
  974. /* WaDisableObjectLevelPreemptionForInstancedDraw:bxt */
  975. /* WaDisableObjectLevelPreemtionForInstanceId:bxt */
  976. /* WaDisableLSQCROPERFforOCL:bxt */
  977. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
  978. ret = wa_ring_whitelist_reg(engine, GEN9_CS_DEBUG_MODE1);
  979. if (ret)
  980. return ret;
  981. ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
  982. if (ret)
  983. return ret;
  984. }
  985. /* WaProgramL3SqcReg1DefaultForPerf:bxt */
  986. if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
  987. I915_WRITE(GEN8_L3SQCREG1, L3_GENERAL_PRIO_CREDITS(62) |
  988. L3_HIGH_PRIO_CREDITS(2));
  989. /* WaInsertDummyPushConstPs:bxt */
  990. if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
  991. WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
  992. GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
  993. return 0;
  994. }
  995. static int kbl_init_workarounds(struct intel_engine_cs *engine)
  996. {
  997. struct drm_i915_private *dev_priv = engine->i915;
  998. int ret;
  999. ret = gen9_init_workarounds(engine);
  1000. if (ret)
  1001. return ret;
  1002. /* WaEnableGapsTsvCreditFix:kbl */
  1003. I915_WRITE(GEN8_GARBCNTL, (I915_READ(GEN8_GARBCNTL) |
  1004. GEN9_GAPS_TSV_CREDIT_DISABLE));
  1005. /* WaDisableDynamicCreditSharing:kbl */
  1006. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
  1007. WA_SET_BIT(GAMT_CHKN_BIT_REG,
  1008. GAMT_CHKN_DISABLE_DYNAMIC_CREDIT_SHARING);
  1009. /* WaDisableFenceDestinationToSLM:kbl (pre-prod) */
  1010. if (IS_KBL_REVID(dev_priv, KBL_REVID_A0, KBL_REVID_A0))
  1011. WA_SET_BIT_MASKED(HDC_CHICKEN0,
  1012. HDC_FENCE_DEST_SLM_DISABLE);
  1013. /* GEN8_L3SQCREG4 has a dependency with WA batch so any new changes
  1014. * involving this register should also be added to WA batch as required.
  1015. */
  1016. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_E0))
  1017. /* WaDisableLSQCROPERFforOCL:kbl */
  1018. I915_WRITE(GEN8_L3SQCREG4, I915_READ(GEN8_L3SQCREG4) |
  1019. GEN8_LQSC_RO_PERF_DIS);
  1020. /* WaInsertDummyPushConstPs:kbl */
  1021. if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
  1022. WA_SET_BIT_MASKED(COMMON_SLICE_CHICKEN2,
  1023. GEN8_SBE_DISABLE_REPLAY_BUF_OPTIMIZATION);
  1024. /* WaDisableGafsUnitClkGating:kbl */
  1025. WA_SET_BIT(GEN7_UCGCTL4, GEN8_EU_GAUNIT_CLOCK_GATE_DISABLE);
  1026. /* WaDisableSbeCacheDispatchPortSharing:kbl */
  1027. WA_SET_BIT_MASKED(
  1028. GEN7_HALF_SLICE_CHICKEN1,
  1029. GEN7_SBE_SS_CACHE_DISPATCH_PORT_SHARING_DISABLE);
  1030. /* WaDisableLSQCROPERFforOCL:kbl */
  1031. ret = wa_ring_whitelist_reg(engine, GEN8_L3SQCREG4);
  1032. if (ret)
  1033. return ret;
  1034. return 0;
  1035. }
  1036. int init_workarounds_ring(struct intel_engine_cs *engine)
  1037. {
  1038. struct drm_i915_private *dev_priv = engine->i915;
  1039. WARN_ON(engine->id != RCS);
  1040. dev_priv->workarounds.count = 0;
  1041. dev_priv->workarounds.hw_whitelist_count[RCS] = 0;
  1042. if (IS_BROADWELL(dev_priv))
  1043. return bdw_init_workarounds(engine);
  1044. if (IS_CHERRYVIEW(dev_priv))
  1045. return chv_init_workarounds(engine);
  1046. if (IS_SKYLAKE(dev_priv))
  1047. return skl_init_workarounds(engine);
  1048. if (IS_BROXTON(dev_priv))
  1049. return bxt_init_workarounds(engine);
  1050. if (IS_KABYLAKE(dev_priv))
  1051. return kbl_init_workarounds(engine);
  1052. return 0;
  1053. }
  1054. static int init_render_ring(struct intel_engine_cs *engine)
  1055. {
  1056. struct drm_i915_private *dev_priv = engine->i915;
  1057. int ret = init_ring_common(engine);
  1058. if (ret)
  1059. return ret;
  1060. /* WaTimedSingleVertexDispatch:cl,bw,ctg,elk,ilk,snb */
  1061. if (IS_GEN(dev_priv, 4, 6))
  1062. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(VS_TIMER_DISPATCH));
  1063. /* We need to disable the AsyncFlip performance optimisations in order
  1064. * to use MI_WAIT_FOR_EVENT within the CS. It should already be
  1065. * programmed to '1' on all products.
  1066. *
  1067. * WaDisableAsyncFlipPerfMode:snb,ivb,hsw,vlv
  1068. */
  1069. if (IS_GEN(dev_priv, 6, 7))
  1070. I915_WRITE(MI_MODE, _MASKED_BIT_ENABLE(ASYNC_FLIP_PERF_DISABLE));
  1071. /* Required for the hardware to program scanline values for waiting */
  1072. /* WaEnableFlushTlbInvalidationMode:snb */
  1073. if (IS_GEN6(dev_priv))
  1074. I915_WRITE(GFX_MODE,
  1075. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT));
  1076. /* WaBCSVCSTlbInvalidationMode:ivb,vlv,hsw */
  1077. if (IS_GEN7(dev_priv))
  1078. I915_WRITE(GFX_MODE_GEN7,
  1079. _MASKED_BIT_ENABLE(GFX_TLB_INVALIDATE_EXPLICIT) |
  1080. _MASKED_BIT_ENABLE(GFX_REPLAY_MODE));
  1081. if (IS_GEN6(dev_priv)) {
  1082. /* From the Sandybridge PRM, volume 1 part 3, page 24:
  1083. * "If this bit is set, STCunit will have LRA as replacement
  1084. * policy. [...] This bit must be reset. LRA replacement
  1085. * policy is not supported."
  1086. */
  1087. I915_WRITE(CACHE_MODE_0,
  1088. _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
  1089. }
  1090. if (IS_GEN(dev_priv, 6, 7))
  1091. I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_FORCE_ORDERING));
  1092. if (HAS_L3_DPF(dev_priv))
  1093. I915_WRITE_IMR(engine, ~GT_PARITY_ERROR(dev_priv));
  1094. return init_workarounds_ring(engine);
  1095. }
  1096. static void render_ring_cleanup(struct intel_engine_cs *engine)
  1097. {
  1098. struct drm_i915_private *dev_priv = engine->i915;
  1099. if (dev_priv->semaphore_obj) {
  1100. i915_gem_object_ggtt_unpin(dev_priv->semaphore_obj);
  1101. drm_gem_object_unreference(&dev_priv->semaphore_obj->base);
  1102. dev_priv->semaphore_obj = NULL;
  1103. }
  1104. intel_fini_pipe_control(engine);
  1105. }
  1106. static int gen8_rcs_signal(struct drm_i915_gem_request *signaller_req,
  1107. unsigned int num_dwords)
  1108. {
  1109. #define MBOX_UPDATE_DWORDS 8
  1110. struct intel_engine_cs *signaller = signaller_req->engine;
  1111. struct drm_i915_private *dev_priv = signaller_req->i915;
  1112. struct intel_engine_cs *waiter;
  1113. enum intel_engine_id id;
  1114. int ret, num_rings;
  1115. num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
  1116. num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
  1117. #undef MBOX_UPDATE_DWORDS
  1118. ret = intel_ring_begin(signaller_req, num_dwords);
  1119. if (ret)
  1120. return ret;
  1121. for_each_engine_id(waiter, dev_priv, id) {
  1122. u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
  1123. if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
  1124. continue;
  1125. intel_ring_emit(signaller, GFX_OP_PIPE_CONTROL(6));
  1126. intel_ring_emit(signaller, PIPE_CONTROL_GLOBAL_GTT_IVB |
  1127. PIPE_CONTROL_QW_WRITE |
  1128. PIPE_CONTROL_CS_STALL);
  1129. intel_ring_emit(signaller, lower_32_bits(gtt_offset));
  1130. intel_ring_emit(signaller, upper_32_bits(gtt_offset));
  1131. intel_ring_emit(signaller, signaller_req->seqno);
  1132. intel_ring_emit(signaller, 0);
  1133. intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
  1134. MI_SEMAPHORE_TARGET(waiter->hw_id));
  1135. intel_ring_emit(signaller, 0);
  1136. }
  1137. return 0;
  1138. }
  1139. static int gen8_xcs_signal(struct drm_i915_gem_request *signaller_req,
  1140. unsigned int num_dwords)
  1141. {
  1142. #define MBOX_UPDATE_DWORDS 6
  1143. struct intel_engine_cs *signaller = signaller_req->engine;
  1144. struct drm_i915_private *dev_priv = signaller_req->i915;
  1145. struct intel_engine_cs *waiter;
  1146. enum intel_engine_id id;
  1147. int ret, num_rings;
  1148. num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
  1149. num_dwords += (num_rings-1) * MBOX_UPDATE_DWORDS;
  1150. #undef MBOX_UPDATE_DWORDS
  1151. ret = intel_ring_begin(signaller_req, num_dwords);
  1152. if (ret)
  1153. return ret;
  1154. for_each_engine_id(waiter, dev_priv, id) {
  1155. u64 gtt_offset = signaller->semaphore.signal_ggtt[id];
  1156. if (gtt_offset == MI_SEMAPHORE_SYNC_INVALID)
  1157. continue;
  1158. intel_ring_emit(signaller, (MI_FLUSH_DW + 1) |
  1159. MI_FLUSH_DW_OP_STOREDW);
  1160. intel_ring_emit(signaller, lower_32_bits(gtt_offset) |
  1161. MI_FLUSH_DW_USE_GTT);
  1162. intel_ring_emit(signaller, upper_32_bits(gtt_offset));
  1163. intel_ring_emit(signaller, signaller_req->seqno);
  1164. intel_ring_emit(signaller, MI_SEMAPHORE_SIGNAL |
  1165. MI_SEMAPHORE_TARGET(waiter->hw_id));
  1166. intel_ring_emit(signaller, 0);
  1167. }
  1168. return 0;
  1169. }
  1170. static int gen6_signal(struct drm_i915_gem_request *signaller_req,
  1171. unsigned int num_dwords)
  1172. {
  1173. struct intel_engine_cs *signaller = signaller_req->engine;
  1174. struct drm_i915_private *dev_priv = signaller_req->i915;
  1175. struct intel_engine_cs *useless;
  1176. enum intel_engine_id id;
  1177. int ret, num_rings;
  1178. #define MBOX_UPDATE_DWORDS 3
  1179. num_rings = hweight32(INTEL_INFO(dev_priv)->ring_mask);
  1180. num_dwords += round_up((num_rings-1) * MBOX_UPDATE_DWORDS, 2);
  1181. #undef MBOX_UPDATE_DWORDS
  1182. ret = intel_ring_begin(signaller_req, num_dwords);
  1183. if (ret)
  1184. return ret;
  1185. for_each_engine_id(useless, dev_priv, id) {
  1186. i915_reg_t mbox_reg = signaller->semaphore.mbox.signal[id];
  1187. if (i915_mmio_reg_valid(mbox_reg)) {
  1188. intel_ring_emit(signaller, MI_LOAD_REGISTER_IMM(1));
  1189. intel_ring_emit_reg(signaller, mbox_reg);
  1190. intel_ring_emit(signaller, signaller_req->seqno);
  1191. }
  1192. }
  1193. /* If num_dwords was rounded, make sure the tail pointer is correct */
  1194. if (num_rings % 2 == 0)
  1195. intel_ring_emit(signaller, MI_NOOP);
  1196. return 0;
  1197. }
  1198. /**
  1199. * gen6_add_request - Update the semaphore mailbox registers
  1200. *
  1201. * @request - request to write to the ring
  1202. *
  1203. * Update the mailbox registers in the *other* rings with the current seqno.
  1204. * This acts like a signal in the canonical semaphore.
  1205. */
  1206. static int
  1207. gen6_add_request(struct drm_i915_gem_request *req)
  1208. {
  1209. struct intel_engine_cs *engine = req->engine;
  1210. int ret;
  1211. if (engine->semaphore.signal)
  1212. ret = engine->semaphore.signal(req, 4);
  1213. else
  1214. ret = intel_ring_begin(req, 4);
  1215. if (ret)
  1216. return ret;
  1217. intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
  1218. intel_ring_emit(engine,
  1219. I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  1220. intel_ring_emit(engine, req->seqno);
  1221. intel_ring_emit(engine, MI_USER_INTERRUPT);
  1222. __intel_ring_advance(engine);
  1223. return 0;
  1224. }
  1225. static int
  1226. gen8_render_add_request(struct drm_i915_gem_request *req)
  1227. {
  1228. struct intel_engine_cs *engine = req->engine;
  1229. int ret;
  1230. if (engine->semaphore.signal)
  1231. ret = engine->semaphore.signal(req, 8);
  1232. else
  1233. ret = intel_ring_begin(req, 8);
  1234. if (ret)
  1235. return ret;
  1236. intel_ring_emit(engine, GFX_OP_PIPE_CONTROL(6));
  1237. intel_ring_emit(engine, (PIPE_CONTROL_GLOBAL_GTT_IVB |
  1238. PIPE_CONTROL_CS_STALL |
  1239. PIPE_CONTROL_QW_WRITE));
  1240. intel_ring_emit(engine, intel_hws_seqno_address(req->engine));
  1241. intel_ring_emit(engine, 0);
  1242. intel_ring_emit(engine, i915_gem_request_get_seqno(req));
  1243. /* We're thrashing one dword of HWS. */
  1244. intel_ring_emit(engine, 0);
  1245. intel_ring_emit(engine, MI_USER_INTERRUPT);
  1246. intel_ring_emit(engine, MI_NOOP);
  1247. __intel_ring_advance(engine);
  1248. return 0;
  1249. }
  1250. static inline bool i915_gem_has_seqno_wrapped(struct drm_i915_private *dev_priv,
  1251. u32 seqno)
  1252. {
  1253. return dev_priv->last_seqno < seqno;
  1254. }
  1255. /**
  1256. * intel_ring_sync - sync the waiter to the signaller on seqno
  1257. *
  1258. * @waiter - ring that is waiting
  1259. * @signaller - ring which has, or will signal
  1260. * @seqno - seqno which the waiter will block on
  1261. */
  1262. static int
  1263. gen8_ring_sync(struct drm_i915_gem_request *waiter_req,
  1264. struct intel_engine_cs *signaller,
  1265. u32 seqno)
  1266. {
  1267. struct intel_engine_cs *waiter = waiter_req->engine;
  1268. struct drm_i915_private *dev_priv = waiter_req->i915;
  1269. u64 offset = GEN8_WAIT_OFFSET(waiter, signaller->id);
  1270. struct i915_hw_ppgtt *ppgtt;
  1271. int ret;
  1272. ret = intel_ring_begin(waiter_req, 4);
  1273. if (ret)
  1274. return ret;
  1275. intel_ring_emit(waiter, MI_SEMAPHORE_WAIT |
  1276. MI_SEMAPHORE_GLOBAL_GTT |
  1277. MI_SEMAPHORE_SAD_GTE_SDD);
  1278. intel_ring_emit(waiter, seqno);
  1279. intel_ring_emit(waiter, lower_32_bits(offset));
  1280. intel_ring_emit(waiter, upper_32_bits(offset));
  1281. intel_ring_advance(waiter);
  1282. /* When the !RCS engines idle waiting upon a semaphore, they lose their
  1283. * pagetables and we must reload them before executing the batch.
  1284. * We do this on the i915_switch_context() following the wait and
  1285. * before the dispatch.
  1286. */
  1287. ppgtt = waiter_req->ctx->ppgtt;
  1288. if (ppgtt && waiter_req->engine->id != RCS)
  1289. ppgtt->pd_dirty_rings |= intel_engine_flag(waiter_req->engine);
  1290. return 0;
  1291. }
  1292. static int
  1293. gen6_ring_sync(struct drm_i915_gem_request *waiter_req,
  1294. struct intel_engine_cs *signaller,
  1295. u32 seqno)
  1296. {
  1297. struct intel_engine_cs *waiter = waiter_req->engine;
  1298. u32 dw1 = MI_SEMAPHORE_MBOX |
  1299. MI_SEMAPHORE_COMPARE |
  1300. MI_SEMAPHORE_REGISTER;
  1301. u32 wait_mbox = signaller->semaphore.mbox.wait[waiter->id];
  1302. int ret;
  1303. /* Throughout all of the GEM code, seqno passed implies our current
  1304. * seqno is >= the last seqno executed. However for hardware the
  1305. * comparison is strictly greater than.
  1306. */
  1307. seqno -= 1;
  1308. WARN_ON(wait_mbox == MI_SEMAPHORE_SYNC_INVALID);
  1309. ret = intel_ring_begin(waiter_req, 4);
  1310. if (ret)
  1311. return ret;
  1312. /* If seqno wrap happened, omit the wait with no-ops */
  1313. if (likely(!i915_gem_has_seqno_wrapped(waiter_req->i915, seqno))) {
  1314. intel_ring_emit(waiter, dw1 | wait_mbox);
  1315. intel_ring_emit(waiter, seqno);
  1316. intel_ring_emit(waiter, 0);
  1317. intel_ring_emit(waiter, MI_NOOP);
  1318. } else {
  1319. intel_ring_emit(waiter, MI_NOOP);
  1320. intel_ring_emit(waiter, MI_NOOP);
  1321. intel_ring_emit(waiter, MI_NOOP);
  1322. intel_ring_emit(waiter, MI_NOOP);
  1323. }
  1324. intel_ring_advance(waiter);
  1325. return 0;
  1326. }
  1327. static void
  1328. gen5_seqno_barrier(struct intel_engine_cs *ring)
  1329. {
  1330. /* MI_STORE are internally buffered by the GPU and not flushed
  1331. * either by MI_FLUSH or SyncFlush or any other combination of
  1332. * MI commands.
  1333. *
  1334. * "Only the submission of the store operation is guaranteed.
  1335. * The write result will be complete (coherent) some time later
  1336. * (this is practically a finite period but there is no guaranteed
  1337. * latency)."
  1338. *
  1339. * Empirically, we observe that we need a delay of at least 75us to
  1340. * be sure that the seqno write is visible by the CPU.
  1341. */
  1342. usleep_range(125, 250);
  1343. }
  1344. static void
  1345. gen6_seqno_barrier(struct intel_engine_cs *engine)
  1346. {
  1347. struct drm_i915_private *dev_priv = engine->i915;
  1348. /* Workaround to force correct ordering between irq and seqno writes on
  1349. * ivb (and maybe also on snb) by reading from a CS register (like
  1350. * ACTHD) before reading the status page.
  1351. *
  1352. * Note that this effectively stalls the read by the time it takes to
  1353. * do a memory transaction, which more or less ensures that the write
  1354. * from the GPU has sufficient time to invalidate the CPU cacheline.
  1355. * Alternatively we could delay the interrupt from the CS ring to give
  1356. * the write time to land, but that would incur a delay after every
  1357. * batch i.e. much more frequent than a delay when waiting for the
  1358. * interrupt (with the same net latency).
  1359. *
  1360. * Also note that to prevent whole machine hangs on gen7, we have to
  1361. * take the spinlock to guard against concurrent cacheline access.
  1362. */
  1363. spin_lock_irq(&dev_priv->uncore.lock);
  1364. POSTING_READ_FW(RING_ACTHD(engine->mmio_base));
  1365. spin_unlock_irq(&dev_priv->uncore.lock);
  1366. }
  1367. static void
  1368. gen5_irq_enable(struct intel_engine_cs *engine)
  1369. {
  1370. gen5_enable_gt_irq(engine->i915, engine->irq_enable_mask);
  1371. }
  1372. static void
  1373. gen5_irq_disable(struct intel_engine_cs *engine)
  1374. {
  1375. gen5_disable_gt_irq(engine->i915, engine->irq_enable_mask);
  1376. }
  1377. static void
  1378. i9xx_irq_enable(struct intel_engine_cs *engine)
  1379. {
  1380. struct drm_i915_private *dev_priv = engine->i915;
  1381. dev_priv->irq_mask &= ~engine->irq_enable_mask;
  1382. I915_WRITE(IMR, dev_priv->irq_mask);
  1383. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1384. }
  1385. static void
  1386. i9xx_irq_disable(struct intel_engine_cs *engine)
  1387. {
  1388. struct drm_i915_private *dev_priv = engine->i915;
  1389. dev_priv->irq_mask |= engine->irq_enable_mask;
  1390. I915_WRITE(IMR, dev_priv->irq_mask);
  1391. }
  1392. static void
  1393. i8xx_irq_enable(struct intel_engine_cs *engine)
  1394. {
  1395. struct drm_i915_private *dev_priv = engine->i915;
  1396. dev_priv->irq_mask &= ~engine->irq_enable_mask;
  1397. I915_WRITE16(IMR, dev_priv->irq_mask);
  1398. POSTING_READ16(RING_IMR(engine->mmio_base));
  1399. }
  1400. static void
  1401. i8xx_irq_disable(struct intel_engine_cs *engine)
  1402. {
  1403. struct drm_i915_private *dev_priv = engine->i915;
  1404. dev_priv->irq_mask |= engine->irq_enable_mask;
  1405. I915_WRITE16(IMR, dev_priv->irq_mask);
  1406. }
  1407. static int
  1408. bsd_ring_flush(struct drm_i915_gem_request *req,
  1409. u32 invalidate_domains,
  1410. u32 flush_domains)
  1411. {
  1412. struct intel_engine_cs *engine = req->engine;
  1413. int ret;
  1414. ret = intel_ring_begin(req, 2);
  1415. if (ret)
  1416. return ret;
  1417. intel_ring_emit(engine, MI_FLUSH);
  1418. intel_ring_emit(engine, MI_NOOP);
  1419. intel_ring_advance(engine);
  1420. return 0;
  1421. }
  1422. static int
  1423. i9xx_add_request(struct drm_i915_gem_request *req)
  1424. {
  1425. struct intel_engine_cs *engine = req->engine;
  1426. int ret;
  1427. ret = intel_ring_begin(req, 4);
  1428. if (ret)
  1429. return ret;
  1430. intel_ring_emit(engine, MI_STORE_DWORD_INDEX);
  1431. intel_ring_emit(engine,
  1432. I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
  1433. intel_ring_emit(engine, req->seqno);
  1434. intel_ring_emit(engine, MI_USER_INTERRUPT);
  1435. __intel_ring_advance(engine);
  1436. return 0;
  1437. }
  1438. static void
  1439. gen6_irq_enable(struct intel_engine_cs *engine)
  1440. {
  1441. struct drm_i915_private *dev_priv = engine->i915;
  1442. if (HAS_L3_DPF(dev_priv) && engine->id == RCS)
  1443. I915_WRITE_IMR(engine,
  1444. ~(engine->irq_enable_mask |
  1445. GT_PARITY_ERROR(dev_priv)));
  1446. else
  1447. I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
  1448. gen5_enable_gt_irq(dev_priv, engine->irq_enable_mask);
  1449. }
  1450. static void
  1451. gen6_irq_disable(struct intel_engine_cs *engine)
  1452. {
  1453. struct drm_i915_private *dev_priv = engine->i915;
  1454. if (HAS_L3_DPF(dev_priv) && engine->id == RCS)
  1455. I915_WRITE_IMR(engine, ~GT_PARITY_ERROR(dev_priv));
  1456. else
  1457. I915_WRITE_IMR(engine, ~0);
  1458. gen5_disable_gt_irq(dev_priv, engine->irq_enable_mask);
  1459. }
  1460. static void
  1461. hsw_vebox_irq_enable(struct intel_engine_cs *engine)
  1462. {
  1463. struct drm_i915_private *dev_priv = engine->i915;
  1464. I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
  1465. gen6_enable_pm_irq(dev_priv, engine->irq_enable_mask);
  1466. }
  1467. static void
  1468. hsw_vebox_irq_disable(struct intel_engine_cs *engine)
  1469. {
  1470. struct drm_i915_private *dev_priv = engine->i915;
  1471. I915_WRITE_IMR(engine, ~0);
  1472. gen6_disable_pm_irq(dev_priv, engine->irq_enable_mask);
  1473. }
  1474. static void
  1475. gen8_irq_enable(struct intel_engine_cs *engine)
  1476. {
  1477. struct drm_i915_private *dev_priv = engine->i915;
  1478. if (HAS_L3_DPF(dev_priv) && engine->id == RCS)
  1479. I915_WRITE_IMR(engine,
  1480. ~(engine->irq_enable_mask |
  1481. GT_RENDER_L3_PARITY_ERROR_INTERRUPT));
  1482. else
  1483. I915_WRITE_IMR(engine, ~engine->irq_enable_mask);
  1484. POSTING_READ_FW(RING_IMR(engine->mmio_base));
  1485. }
  1486. static void
  1487. gen8_irq_disable(struct intel_engine_cs *engine)
  1488. {
  1489. struct drm_i915_private *dev_priv = engine->i915;
  1490. if (HAS_L3_DPF(dev_priv) && engine->id == RCS)
  1491. I915_WRITE_IMR(engine,
  1492. ~GT_RENDER_L3_PARITY_ERROR_INTERRUPT);
  1493. else
  1494. I915_WRITE_IMR(engine, ~0);
  1495. }
  1496. static int
  1497. i965_dispatch_execbuffer(struct drm_i915_gem_request *req,
  1498. u64 offset, u32 length,
  1499. unsigned dispatch_flags)
  1500. {
  1501. struct intel_engine_cs *engine = req->engine;
  1502. int ret;
  1503. ret = intel_ring_begin(req, 2);
  1504. if (ret)
  1505. return ret;
  1506. intel_ring_emit(engine,
  1507. MI_BATCH_BUFFER_START |
  1508. MI_BATCH_GTT |
  1509. (dispatch_flags & I915_DISPATCH_SECURE ?
  1510. 0 : MI_BATCH_NON_SECURE_I965));
  1511. intel_ring_emit(engine, offset);
  1512. intel_ring_advance(engine);
  1513. return 0;
  1514. }
  1515. /* Just userspace ABI convention to limit the wa batch bo to a resonable size */
  1516. #define I830_BATCH_LIMIT (256*1024)
  1517. #define I830_TLB_ENTRIES (2)
  1518. #define I830_WA_SIZE max(I830_TLB_ENTRIES*4096, I830_BATCH_LIMIT)
  1519. static int
  1520. i830_dispatch_execbuffer(struct drm_i915_gem_request *req,
  1521. u64 offset, u32 len,
  1522. unsigned dispatch_flags)
  1523. {
  1524. struct intel_engine_cs *engine = req->engine;
  1525. u32 cs_offset = engine->scratch.gtt_offset;
  1526. int ret;
  1527. ret = intel_ring_begin(req, 6);
  1528. if (ret)
  1529. return ret;
  1530. /* Evict the invalid PTE TLBs */
  1531. intel_ring_emit(engine, COLOR_BLT_CMD | BLT_WRITE_RGBA);
  1532. intel_ring_emit(engine, BLT_DEPTH_32 | BLT_ROP_COLOR_COPY | 4096);
  1533. intel_ring_emit(engine, I830_TLB_ENTRIES << 16 | 4); /* load each page */
  1534. intel_ring_emit(engine, cs_offset);
  1535. intel_ring_emit(engine, 0xdeadbeef);
  1536. intel_ring_emit(engine, MI_NOOP);
  1537. intel_ring_advance(engine);
  1538. if ((dispatch_flags & I915_DISPATCH_PINNED) == 0) {
  1539. if (len > I830_BATCH_LIMIT)
  1540. return -ENOSPC;
  1541. ret = intel_ring_begin(req, 6 + 2);
  1542. if (ret)
  1543. return ret;
  1544. /* Blit the batch (which has now all relocs applied) to the
  1545. * stable batch scratch bo area (so that the CS never
  1546. * stumbles over its tlb invalidation bug) ...
  1547. */
  1548. intel_ring_emit(engine, SRC_COPY_BLT_CMD | BLT_WRITE_RGBA);
  1549. intel_ring_emit(engine,
  1550. BLT_DEPTH_32 | BLT_ROP_SRC_COPY | 4096);
  1551. intel_ring_emit(engine, DIV_ROUND_UP(len, 4096) << 16 | 4096);
  1552. intel_ring_emit(engine, cs_offset);
  1553. intel_ring_emit(engine, 4096);
  1554. intel_ring_emit(engine, offset);
  1555. intel_ring_emit(engine, MI_FLUSH);
  1556. intel_ring_emit(engine, MI_NOOP);
  1557. intel_ring_advance(engine);
  1558. /* ... and execute it. */
  1559. offset = cs_offset;
  1560. }
  1561. ret = intel_ring_begin(req, 2);
  1562. if (ret)
  1563. return ret;
  1564. intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
  1565. intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
  1566. 0 : MI_BATCH_NON_SECURE));
  1567. intel_ring_advance(engine);
  1568. return 0;
  1569. }
  1570. static int
  1571. i915_dispatch_execbuffer(struct drm_i915_gem_request *req,
  1572. u64 offset, u32 len,
  1573. unsigned dispatch_flags)
  1574. {
  1575. struct intel_engine_cs *engine = req->engine;
  1576. int ret;
  1577. ret = intel_ring_begin(req, 2);
  1578. if (ret)
  1579. return ret;
  1580. intel_ring_emit(engine, MI_BATCH_BUFFER_START | MI_BATCH_GTT);
  1581. intel_ring_emit(engine, offset | (dispatch_flags & I915_DISPATCH_SECURE ?
  1582. 0 : MI_BATCH_NON_SECURE));
  1583. intel_ring_advance(engine);
  1584. return 0;
  1585. }
  1586. static void cleanup_phys_status_page(struct intel_engine_cs *engine)
  1587. {
  1588. struct drm_i915_private *dev_priv = engine->i915;
  1589. if (!dev_priv->status_page_dmah)
  1590. return;
  1591. drm_pci_free(dev_priv->dev, dev_priv->status_page_dmah);
  1592. engine->status_page.page_addr = NULL;
  1593. }
  1594. static void cleanup_status_page(struct intel_engine_cs *engine)
  1595. {
  1596. struct drm_i915_gem_object *obj;
  1597. obj = engine->status_page.obj;
  1598. if (obj == NULL)
  1599. return;
  1600. kunmap(sg_page(obj->pages->sgl));
  1601. i915_gem_object_ggtt_unpin(obj);
  1602. drm_gem_object_unreference(&obj->base);
  1603. engine->status_page.obj = NULL;
  1604. }
  1605. static int init_status_page(struct intel_engine_cs *engine)
  1606. {
  1607. struct drm_i915_gem_object *obj = engine->status_page.obj;
  1608. if (obj == NULL) {
  1609. unsigned flags;
  1610. int ret;
  1611. obj = i915_gem_object_create(engine->i915->dev, 4096);
  1612. if (IS_ERR(obj)) {
  1613. DRM_ERROR("Failed to allocate status page\n");
  1614. return PTR_ERR(obj);
  1615. }
  1616. ret = i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  1617. if (ret)
  1618. goto err_unref;
  1619. flags = 0;
  1620. if (!HAS_LLC(engine->i915))
  1621. /* On g33, we cannot place HWS above 256MiB, so
  1622. * restrict its pinning to the low mappable arena.
  1623. * Though this restriction is not documented for
  1624. * gen4, gen5, or byt, they also behave similarly
  1625. * and hang if the HWS is placed at the top of the
  1626. * GTT. To generalise, it appears that all !llc
  1627. * platforms have issues with us placing the HWS
  1628. * above the mappable region (even though we never
  1629. * actualy map it).
  1630. */
  1631. flags |= PIN_MAPPABLE;
  1632. ret = i915_gem_obj_ggtt_pin(obj, 4096, flags);
  1633. if (ret) {
  1634. err_unref:
  1635. drm_gem_object_unreference(&obj->base);
  1636. return ret;
  1637. }
  1638. engine->status_page.obj = obj;
  1639. }
  1640. engine->status_page.gfx_addr = i915_gem_obj_ggtt_offset(obj);
  1641. engine->status_page.page_addr = kmap(sg_page(obj->pages->sgl));
  1642. memset(engine->status_page.page_addr, 0, PAGE_SIZE);
  1643. DRM_DEBUG_DRIVER("%s hws offset: 0x%08x\n",
  1644. engine->name, engine->status_page.gfx_addr);
  1645. return 0;
  1646. }
  1647. static int init_phys_status_page(struct intel_engine_cs *engine)
  1648. {
  1649. struct drm_i915_private *dev_priv = engine->i915;
  1650. if (!dev_priv->status_page_dmah) {
  1651. dev_priv->status_page_dmah =
  1652. drm_pci_alloc(dev_priv->dev, PAGE_SIZE, PAGE_SIZE);
  1653. if (!dev_priv->status_page_dmah)
  1654. return -ENOMEM;
  1655. }
  1656. engine->status_page.page_addr = dev_priv->status_page_dmah->vaddr;
  1657. memset(engine->status_page.page_addr, 0, PAGE_SIZE);
  1658. return 0;
  1659. }
  1660. void intel_unpin_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
  1661. {
  1662. GEM_BUG_ON(ringbuf->vma == NULL);
  1663. GEM_BUG_ON(ringbuf->virtual_start == NULL);
  1664. if (HAS_LLC(ringbuf->obj->base.dev) && !ringbuf->obj->stolen)
  1665. i915_gem_object_unpin_map(ringbuf->obj);
  1666. else
  1667. i915_vma_unpin_iomap(ringbuf->vma);
  1668. ringbuf->virtual_start = NULL;
  1669. i915_gem_object_ggtt_unpin(ringbuf->obj);
  1670. ringbuf->vma = NULL;
  1671. }
  1672. int intel_pin_and_map_ringbuffer_obj(struct drm_i915_private *dev_priv,
  1673. struct intel_ringbuffer *ringbuf)
  1674. {
  1675. struct drm_i915_gem_object *obj = ringbuf->obj;
  1676. /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
  1677. unsigned flags = PIN_OFFSET_BIAS | 4096;
  1678. void *addr;
  1679. int ret;
  1680. if (HAS_LLC(dev_priv) && !obj->stolen) {
  1681. ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, flags);
  1682. if (ret)
  1683. return ret;
  1684. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1685. if (ret)
  1686. goto err_unpin;
  1687. addr = i915_gem_object_pin_map(obj);
  1688. if (IS_ERR(addr)) {
  1689. ret = PTR_ERR(addr);
  1690. goto err_unpin;
  1691. }
  1692. } else {
  1693. ret = i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
  1694. flags | PIN_MAPPABLE);
  1695. if (ret)
  1696. return ret;
  1697. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  1698. if (ret)
  1699. goto err_unpin;
  1700. /* Access through the GTT requires the device to be awake. */
  1701. assert_rpm_wakelock_held(dev_priv);
  1702. addr = i915_vma_pin_iomap(i915_gem_obj_to_ggtt(obj));
  1703. if (IS_ERR(addr)) {
  1704. ret = PTR_ERR(addr);
  1705. goto err_unpin;
  1706. }
  1707. }
  1708. ringbuf->virtual_start = addr;
  1709. ringbuf->vma = i915_gem_obj_to_ggtt(obj);
  1710. return 0;
  1711. err_unpin:
  1712. i915_gem_object_ggtt_unpin(obj);
  1713. return ret;
  1714. }
  1715. static void intel_destroy_ringbuffer_obj(struct intel_ringbuffer *ringbuf)
  1716. {
  1717. drm_gem_object_unreference(&ringbuf->obj->base);
  1718. ringbuf->obj = NULL;
  1719. }
  1720. static int intel_alloc_ringbuffer_obj(struct drm_device *dev,
  1721. struct intel_ringbuffer *ringbuf)
  1722. {
  1723. struct drm_i915_gem_object *obj;
  1724. obj = NULL;
  1725. if (!HAS_LLC(dev))
  1726. obj = i915_gem_object_create_stolen(dev, ringbuf->size);
  1727. if (obj == NULL)
  1728. obj = i915_gem_object_create(dev, ringbuf->size);
  1729. if (IS_ERR(obj))
  1730. return PTR_ERR(obj);
  1731. /* mark ring buffers as read-only from GPU side by default */
  1732. obj->gt_ro = 1;
  1733. ringbuf->obj = obj;
  1734. return 0;
  1735. }
  1736. struct intel_ringbuffer *
  1737. intel_engine_create_ringbuffer(struct intel_engine_cs *engine, int size)
  1738. {
  1739. struct intel_ringbuffer *ring;
  1740. int ret;
  1741. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  1742. if (ring == NULL) {
  1743. DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s\n",
  1744. engine->name);
  1745. return ERR_PTR(-ENOMEM);
  1746. }
  1747. ring->engine = engine;
  1748. list_add(&ring->link, &engine->buffers);
  1749. ring->size = size;
  1750. /* Workaround an erratum on the i830 which causes a hang if
  1751. * the TAIL pointer points to within the last 2 cachelines
  1752. * of the buffer.
  1753. */
  1754. ring->effective_size = size;
  1755. if (IS_I830(engine->i915) || IS_845G(engine->i915))
  1756. ring->effective_size -= 2 * CACHELINE_BYTES;
  1757. ring->last_retired_head = -1;
  1758. intel_ring_update_space(ring);
  1759. ret = intel_alloc_ringbuffer_obj(engine->i915->dev, ring);
  1760. if (ret) {
  1761. DRM_DEBUG_DRIVER("Failed to allocate ringbuffer %s: %d\n",
  1762. engine->name, ret);
  1763. list_del(&ring->link);
  1764. kfree(ring);
  1765. return ERR_PTR(ret);
  1766. }
  1767. return ring;
  1768. }
  1769. void
  1770. intel_ringbuffer_free(struct intel_ringbuffer *ring)
  1771. {
  1772. intel_destroy_ringbuffer_obj(ring);
  1773. list_del(&ring->link);
  1774. kfree(ring);
  1775. }
  1776. static int intel_ring_context_pin(struct i915_gem_context *ctx,
  1777. struct intel_engine_cs *engine)
  1778. {
  1779. struct intel_context *ce = &ctx->engine[engine->id];
  1780. int ret;
  1781. lockdep_assert_held(&ctx->i915->dev->struct_mutex);
  1782. if (ce->pin_count++)
  1783. return 0;
  1784. if (ce->state) {
  1785. ret = i915_gem_obj_ggtt_pin(ce->state, ctx->ggtt_alignment, 0);
  1786. if (ret)
  1787. goto error;
  1788. }
  1789. /* The kernel context is only used as a placeholder for flushing the
  1790. * active context. It is never used for submitting user rendering and
  1791. * as such never requires the golden render context, and so we can skip
  1792. * emitting it when we switch to the kernel context. This is required
  1793. * as during eviction we cannot allocate and pin the renderstate in
  1794. * order to initialise the context.
  1795. */
  1796. if (ctx == ctx->i915->kernel_context)
  1797. ce->initialised = true;
  1798. i915_gem_context_reference(ctx);
  1799. return 0;
  1800. error:
  1801. ce->pin_count = 0;
  1802. return ret;
  1803. }
  1804. static void intel_ring_context_unpin(struct i915_gem_context *ctx,
  1805. struct intel_engine_cs *engine)
  1806. {
  1807. struct intel_context *ce = &ctx->engine[engine->id];
  1808. lockdep_assert_held(&ctx->i915->dev->struct_mutex);
  1809. if (--ce->pin_count)
  1810. return;
  1811. if (ce->state)
  1812. i915_gem_object_ggtt_unpin(ce->state);
  1813. i915_gem_context_unreference(ctx);
  1814. }
  1815. static int intel_init_ring_buffer(struct drm_device *dev,
  1816. struct intel_engine_cs *engine)
  1817. {
  1818. struct drm_i915_private *dev_priv = to_i915(dev);
  1819. struct intel_ringbuffer *ringbuf;
  1820. int ret;
  1821. WARN_ON(engine->buffer);
  1822. engine->i915 = dev_priv;
  1823. INIT_LIST_HEAD(&engine->active_list);
  1824. INIT_LIST_HEAD(&engine->request_list);
  1825. INIT_LIST_HEAD(&engine->execlist_queue);
  1826. INIT_LIST_HEAD(&engine->buffers);
  1827. i915_gem_batch_pool_init(dev, &engine->batch_pool);
  1828. memset(engine->semaphore.sync_seqno, 0,
  1829. sizeof(engine->semaphore.sync_seqno));
  1830. ret = intel_engine_init_breadcrumbs(engine);
  1831. if (ret)
  1832. goto error;
  1833. /* We may need to do things with the shrinker which
  1834. * require us to immediately switch back to the default
  1835. * context. This can cause a problem as pinning the
  1836. * default context also requires GTT space which may not
  1837. * be available. To avoid this we always pin the default
  1838. * context.
  1839. */
  1840. ret = intel_ring_context_pin(dev_priv->kernel_context, engine);
  1841. if (ret)
  1842. goto error;
  1843. ringbuf = intel_engine_create_ringbuffer(engine, 32 * PAGE_SIZE);
  1844. if (IS_ERR(ringbuf)) {
  1845. ret = PTR_ERR(ringbuf);
  1846. goto error;
  1847. }
  1848. engine->buffer = ringbuf;
  1849. if (I915_NEED_GFX_HWS(dev_priv)) {
  1850. ret = init_status_page(engine);
  1851. if (ret)
  1852. goto error;
  1853. } else {
  1854. WARN_ON(engine->id != RCS);
  1855. ret = init_phys_status_page(engine);
  1856. if (ret)
  1857. goto error;
  1858. }
  1859. ret = intel_pin_and_map_ringbuffer_obj(dev_priv, ringbuf);
  1860. if (ret) {
  1861. DRM_ERROR("Failed to pin and map ringbuffer %s: %d\n",
  1862. engine->name, ret);
  1863. intel_destroy_ringbuffer_obj(ringbuf);
  1864. goto error;
  1865. }
  1866. ret = i915_cmd_parser_init_ring(engine);
  1867. if (ret)
  1868. goto error;
  1869. return 0;
  1870. error:
  1871. intel_cleanup_engine(engine);
  1872. return ret;
  1873. }
  1874. void intel_cleanup_engine(struct intel_engine_cs *engine)
  1875. {
  1876. struct drm_i915_private *dev_priv;
  1877. if (!intel_engine_initialized(engine))
  1878. return;
  1879. dev_priv = engine->i915;
  1880. if (engine->buffer) {
  1881. intel_stop_engine(engine);
  1882. WARN_ON(!IS_GEN2(dev_priv) && (I915_READ_MODE(engine) & MODE_IDLE) == 0);
  1883. intel_unpin_ringbuffer_obj(engine->buffer);
  1884. intel_ringbuffer_free(engine->buffer);
  1885. engine->buffer = NULL;
  1886. }
  1887. if (engine->cleanup)
  1888. engine->cleanup(engine);
  1889. if (I915_NEED_GFX_HWS(dev_priv)) {
  1890. cleanup_status_page(engine);
  1891. } else {
  1892. WARN_ON(engine->id != RCS);
  1893. cleanup_phys_status_page(engine);
  1894. }
  1895. i915_cmd_parser_fini_ring(engine);
  1896. i915_gem_batch_pool_fini(&engine->batch_pool);
  1897. intel_engine_fini_breadcrumbs(engine);
  1898. intel_ring_context_unpin(dev_priv->kernel_context, engine);
  1899. engine->i915 = NULL;
  1900. }
  1901. int intel_engine_idle(struct intel_engine_cs *engine)
  1902. {
  1903. struct drm_i915_gem_request *req;
  1904. /* Wait upon the last request to be completed */
  1905. if (list_empty(&engine->request_list))
  1906. return 0;
  1907. req = list_entry(engine->request_list.prev,
  1908. struct drm_i915_gem_request,
  1909. list);
  1910. /* Make sure we do not trigger any retires */
  1911. return __i915_wait_request(req,
  1912. req->i915->mm.interruptible,
  1913. NULL, NULL);
  1914. }
  1915. int intel_ring_alloc_request_extras(struct drm_i915_gem_request *request)
  1916. {
  1917. int ret;
  1918. /* Flush enough space to reduce the likelihood of waiting after
  1919. * we start building the request - in which case we will just
  1920. * have to repeat work.
  1921. */
  1922. request->reserved_space += LEGACY_REQUEST_SIZE;
  1923. request->ringbuf = request->engine->buffer;
  1924. ret = intel_ring_begin(request, 0);
  1925. if (ret)
  1926. return ret;
  1927. request->reserved_space -= LEGACY_REQUEST_SIZE;
  1928. return 0;
  1929. }
  1930. static int wait_for_space(struct drm_i915_gem_request *req, int bytes)
  1931. {
  1932. struct intel_ringbuffer *ringbuf = req->ringbuf;
  1933. struct intel_engine_cs *engine = req->engine;
  1934. struct drm_i915_gem_request *target;
  1935. intel_ring_update_space(ringbuf);
  1936. if (ringbuf->space >= bytes)
  1937. return 0;
  1938. /*
  1939. * Space is reserved in the ringbuffer for finalising the request,
  1940. * as that cannot be allowed to fail. During request finalisation,
  1941. * reserved_space is set to 0 to stop the overallocation and the
  1942. * assumption is that then we never need to wait (which has the
  1943. * risk of failing with EINTR).
  1944. *
  1945. * See also i915_gem_request_alloc() and i915_add_request().
  1946. */
  1947. GEM_BUG_ON(!req->reserved_space);
  1948. list_for_each_entry(target, &engine->request_list, list) {
  1949. unsigned space;
  1950. /*
  1951. * The request queue is per-engine, so can contain requests
  1952. * from multiple ringbuffers. Here, we must ignore any that
  1953. * aren't from the ringbuffer we're considering.
  1954. */
  1955. if (target->ringbuf != ringbuf)
  1956. continue;
  1957. /* Would completion of this request free enough space? */
  1958. space = __intel_ring_space(target->postfix, ringbuf->tail,
  1959. ringbuf->size);
  1960. if (space >= bytes)
  1961. break;
  1962. }
  1963. if (WARN_ON(&target->list == &engine->request_list))
  1964. return -ENOSPC;
  1965. return i915_wait_request(target);
  1966. }
  1967. int intel_ring_begin(struct drm_i915_gem_request *req, int num_dwords)
  1968. {
  1969. struct intel_ringbuffer *ringbuf = req->ringbuf;
  1970. int remain_actual = ringbuf->size - ringbuf->tail;
  1971. int remain_usable = ringbuf->effective_size - ringbuf->tail;
  1972. int bytes = num_dwords * sizeof(u32);
  1973. int total_bytes, wait_bytes;
  1974. bool need_wrap = false;
  1975. total_bytes = bytes + req->reserved_space;
  1976. if (unlikely(bytes > remain_usable)) {
  1977. /*
  1978. * Not enough space for the basic request. So need to flush
  1979. * out the remainder and then wait for base + reserved.
  1980. */
  1981. wait_bytes = remain_actual + total_bytes;
  1982. need_wrap = true;
  1983. } else if (unlikely(total_bytes > remain_usable)) {
  1984. /*
  1985. * The base request will fit but the reserved space
  1986. * falls off the end. So we don't need an immediate wrap
  1987. * and only need to effectively wait for the reserved
  1988. * size space from the start of ringbuffer.
  1989. */
  1990. wait_bytes = remain_actual + req->reserved_space;
  1991. } else {
  1992. /* No wrapping required, just waiting. */
  1993. wait_bytes = total_bytes;
  1994. }
  1995. if (wait_bytes > ringbuf->space) {
  1996. int ret = wait_for_space(req, wait_bytes);
  1997. if (unlikely(ret))
  1998. return ret;
  1999. intel_ring_update_space(ringbuf);
  2000. if (unlikely(ringbuf->space < wait_bytes))
  2001. return -EAGAIN;
  2002. }
  2003. if (unlikely(need_wrap)) {
  2004. GEM_BUG_ON(remain_actual > ringbuf->space);
  2005. GEM_BUG_ON(ringbuf->tail + remain_actual > ringbuf->size);
  2006. /* Fill the tail with MI_NOOP */
  2007. memset(ringbuf->virtual_start + ringbuf->tail,
  2008. 0, remain_actual);
  2009. ringbuf->tail = 0;
  2010. ringbuf->space -= remain_actual;
  2011. }
  2012. ringbuf->space -= bytes;
  2013. GEM_BUG_ON(ringbuf->space < 0);
  2014. return 0;
  2015. }
  2016. /* Align the ring tail to a cacheline boundary */
  2017. int intel_ring_cacheline_align(struct drm_i915_gem_request *req)
  2018. {
  2019. struct intel_engine_cs *engine = req->engine;
  2020. int num_dwords = (engine->buffer->tail & (CACHELINE_BYTES - 1)) / sizeof(uint32_t);
  2021. int ret;
  2022. if (num_dwords == 0)
  2023. return 0;
  2024. num_dwords = CACHELINE_BYTES / sizeof(uint32_t) - num_dwords;
  2025. ret = intel_ring_begin(req, num_dwords);
  2026. if (ret)
  2027. return ret;
  2028. while (num_dwords--)
  2029. intel_ring_emit(engine, MI_NOOP);
  2030. intel_ring_advance(engine);
  2031. return 0;
  2032. }
  2033. void intel_ring_init_seqno(struct intel_engine_cs *engine, u32 seqno)
  2034. {
  2035. struct drm_i915_private *dev_priv = engine->i915;
  2036. /* Our semaphore implementation is strictly monotonic (i.e. we proceed
  2037. * so long as the semaphore value in the register/page is greater
  2038. * than the sync value), so whenever we reset the seqno,
  2039. * so long as we reset the tracking semaphore value to 0, it will
  2040. * always be before the next request's seqno. If we don't reset
  2041. * the semaphore value, then when the seqno moves backwards all
  2042. * future waits will complete instantly (causing rendering corruption).
  2043. */
  2044. if (IS_GEN6(dev_priv) || IS_GEN7(dev_priv)) {
  2045. I915_WRITE(RING_SYNC_0(engine->mmio_base), 0);
  2046. I915_WRITE(RING_SYNC_1(engine->mmio_base), 0);
  2047. if (HAS_VEBOX(dev_priv))
  2048. I915_WRITE(RING_SYNC_2(engine->mmio_base), 0);
  2049. }
  2050. if (dev_priv->semaphore_obj) {
  2051. struct drm_i915_gem_object *obj = dev_priv->semaphore_obj;
  2052. struct page *page = i915_gem_object_get_dirty_page(obj, 0);
  2053. void *semaphores = kmap(page);
  2054. memset(semaphores + GEN8_SEMAPHORE_OFFSET(engine->id, 0),
  2055. 0, I915_NUM_ENGINES * gen8_semaphore_seqno_size);
  2056. kunmap(page);
  2057. }
  2058. memset(engine->semaphore.sync_seqno, 0,
  2059. sizeof(engine->semaphore.sync_seqno));
  2060. intel_write_status_page(engine, I915_GEM_HWS_INDEX, seqno);
  2061. if (engine->irq_seqno_barrier)
  2062. engine->irq_seqno_barrier(engine);
  2063. engine->last_submitted_seqno = seqno;
  2064. engine->hangcheck.seqno = seqno;
  2065. /* After manually advancing the seqno, fake the interrupt in case
  2066. * there are any waiters for that seqno.
  2067. */
  2068. rcu_read_lock();
  2069. intel_engine_wakeup(engine);
  2070. rcu_read_unlock();
  2071. }
  2072. static void gen6_bsd_ring_write_tail(struct intel_engine_cs *engine,
  2073. u32 value)
  2074. {
  2075. struct drm_i915_private *dev_priv = engine->i915;
  2076. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  2077. /* Every tail move must follow the sequence below */
  2078. /* Disable notification that the ring is IDLE. The GT
  2079. * will then assume that it is busy and bring it out of rc6.
  2080. */
  2081. I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
  2082. _MASKED_BIT_ENABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  2083. /* Clear the context id. Here be magic! */
  2084. I915_WRITE64_FW(GEN6_BSD_RNCID, 0x0);
  2085. /* Wait for the ring not to be idle, i.e. for it to wake up. */
  2086. if (intel_wait_for_register_fw(dev_priv,
  2087. GEN6_BSD_SLEEP_PSMI_CONTROL,
  2088. GEN6_BSD_SLEEP_INDICATOR,
  2089. 0,
  2090. 50))
  2091. DRM_ERROR("timed out waiting for the BSD ring to wake up\n");
  2092. /* Now that the ring is fully powered up, update the tail */
  2093. I915_WRITE_FW(RING_TAIL(engine->mmio_base), value);
  2094. POSTING_READ_FW(RING_TAIL(engine->mmio_base));
  2095. /* Let the ring send IDLE messages to the GT again,
  2096. * and so let it sleep to conserve power when idle.
  2097. */
  2098. I915_WRITE_FW(GEN6_BSD_SLEEP_PSMI_CONTROL,
  2099. _MASKED_BIT_DISABLE(GEN6_BSD_SLEEP_MSG_DISABLE));
  2100. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  2101. }
  2102. static int gen6_bsd_ring_flush(struct drm_i915_gem_request *req,
  2103. u32 invalidate, u32 flush)
  2104. {
  2105. struct intel_engine_cs *engine = req->engine;
  2106. uint32_t cmd;
  2107. int ret;
  2108. ret = intel_ring_begin(req, 4);
  2109. if (ret)
  2110. return ret;
  2111. cmd = MI_FLUSH_DW;
  2112. if (INTEL_GEN(req->i915) >= 8)
  2113. cmd += 1;
  2114. /* We always require a command barrier so that subsequent
  2115. * commands, such as breadcrumb interrupts, are strictly ordered
  2116. * wrt the contents of the write cache being flushed to memory
  2117. * (and thus being coherent from the CPU).
  2118. */
  2119. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  2120. /*
  2121. * Bspec vol 1c.5 - video engine command streamer:
  2122. * "If ENABLED, all TLBs will be invalidated once the flush
  2123. * operation is complete. This bit is only valid when the
  2124. * Post-Sync Operation field is a value of 1h or 3h."
  2125. */
  2126. if (invalidate & I915_GEM_GPU_DOMAINS)
  2127. cmd |= MI_INVALIDATE_TLB | MI_INVALIDATE_BSD;
  2128. intel_ring_emit(engine, cmd);
  2129. intel_ring_emit(engine,
  2130. I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
  2131. if (INTEL_GEN(req->i915) >= 8) {
  2132. intel_ring_emit(engine, 0); /* upper addr */
  2133. intel_ring_emit(engine, 0); /* value */
  2134. } else {
  2135. intel_ring_emit(engine, 0);
  2136. intel_ring_emit(engine, MI_NOOP);
  2137. }
  2138. intel_ring_advance(engine);
  2139. return 0;
  2140. }
  2141. static int
  2142. gen8_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
  2143. u64 offset, u32 len,
  2144. unsigned dispatch_flags)
  2145. {
  2146. struct intel_engine_cs *engine = req->engine;
  2147. bool ppgtt = USES_PPGTT(engine->dev) &&
  2148. !(dispatch_flags & I915_DISPATCH_SECURE);
  2149. int ret;
  2150. ret = intel_ring_begin(req, 4);
  2151. if (ret)
  2152. return ret;
  2153. /* FIXME(BDW): Address space and security selectors. */
  2154. intel_ring_emit(engine, MI_BATCH_BUFFER_START_GEN8 | (ppgtt<<8) |
  2155. (dispatch_flags & I915_DISPATCH_RS ?
  2156. MI_BATCH_RESOURCE_STREAMER : 0));
  2157. intel_ring_emit(engine, lower_32_bits(offset));
  2158. intel_ring_emit(engine, upper_32_bits(offset));
  2159. intel_ring_emit(engine, MI_NOOP);
  2160. intel_ring_advance(engine);
  2161. return 0;
  2162. }
  2163. static int
  2164. hsw_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
  2165. u64 offset, u32 len,
  2166. unsigned dispatch_flags)
  2167. {
  2168. struct intel_engine_cs *engine = req->engine;
  2169. int ret;
  2170. ret = intel_ring_begin(req, 2);
  2171. if (ret)
  2172. return ret;
  2173. intel_ring_emit(engine,
  2174. MI_BATCH_BUFFER_START |
  2175. (dispatch_flags & I915_DISPATCH_SECURE ?
  2176. 0 : MI_BATCH_PPGTT_HSW | MI_BATCH_NON_SECURE_HSW) |
  2177. (dispatch_flags & I915_DISPATCH_RS ?
  2178. MI_BATCH_RESOURCE_STREAMER : 0));
  2179. /* bit0-7 is the length on GEN6+ */
  2180. intel_ring_emit(engine, offset);
  2181. intel_ring_advance(engine);
  2182. return 0;
  2183. }
  2184. static int
  2185. gen6_ring_dispatch_execbuffer(struct drm_i915_gem_request *req,
  2186. u64 offset, u32 len,
  2187. unsigned dispatch_flags)
  2188. {
  2189. struct intel_engine_cs *engine = req->engine;
  2190. int ret;
  2191. ret = intel_ring_begin(req, 2);
  2192. if (ret)
  2193. return ret;
  2194. intel_ring_emit(engine,
  2195. MI_BATCH_BUFFER_START |
  2196. (dispatch_flags & I915_DISPATCH_SECURE ?
  2197. 0 : MI_BATCH_NON_SECURE_I965));
  2198. /* bit0-7 is the length on GEN6+ */
  2199. intel_ring_emit(engine, offset);
  2200. intel_ring_advance(engine);
  2201. return 0;
  2202. }
  2203. /* Blitter support (SandyBridge+) */
  2204. static int gen6_ring_flush(struct drm_i915_gem_request *req,
  2205. u32 invalidate, u32 flush)
  2206. {
  2207. struct intel_engine_cs *engine = req->engine;
  2208. uint32_t cmd;
  2209. int ret;
  2210. ret = intel_ring_begin(req, 4);
  2211. if (ret)
  2212. return ret;
  2213. cmd = MI_FLUSH_DW;
  2214. if (INTEL_GEN(req->i915) >= 8)
  2215. cmd += 1;
  2216. /* We always require a command barrier so that subsequent
  2217. * commands, such as breadcrumb interrupts, are strictly ordered
  2218. * wrt the contents of the write cache being flushed to memory
  2219. * (and thus being coherent from the CPU).
  2220. */
  2221. cmd |= MI_FLUSH_DW_STORE_INDEX | MI_FLUSH_DW_OP_STOREDW;
  2222. /*
  2223. * Bspec vol 1c.3 - blitter engine command streamer:
  2224. * "If ENABLED, all TLBs will be invalidated once the flush
  2225. * operation is complete. This bit is only valid when the
  2226. * Post-Sync Operation field is a value of 1h or 3h."
  2227. */
  2228. if (invalidate & I915_GEM_DOMAIN_RENDER)
  2229. cmd |= MI_INVALIDATE_TLB;
  2230. intel_ring_emit(engine, cmd);
  2231. intel_ring_emit(engine,
  2232. I915_GEM_HWS_SCRATCH_ADDR | MI_FLUSH_DW_USE_GTT);
  2233. if (INTEL_GEN(req->i915) >= 8) {
  2234. intel_ring_emit(engine, 0); /* upper addr */
  2235. intel_ring_emit(engine, 0); /* value */
  2236. } else {
  2237. intel_ring_emit(engine, 0);
  2238. intel_ring_emit(engine, MI_NOOP);
  2239. }
  2240. intel_ring_advance(engine);
  2241. return 0;
  2242. }
  2243. static void intel_ring_init_semaphores(struct drm_i915_private *dev_priv,
  2244. struct intel_engine_cs *engine)
  2245. {
  2246. struct drm_i915_gem_object *obj;
  2247. int ret, i;
  2248. if (!i915_semaphore_is_enabled(dev_priv))
  2249. return;
  2250. if (INTEL_GEN(dev_priv) >= 8 && !dev_priv->semaphore_obj) {
  2251. obj = i915_gem_object_create(dev_priv->dev, 4096);
  2252. if (IS_ERR(obj)) {
  2253. DRM_ERROR("Failed to allocate semaphore bo. Disabling semaphores\n");
  2254. i915.semaphores = 0;
  2255. } else {
  2256. i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  2257. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_NONBLOCK);
  2258. if (ret != 0) {
  2259. drm_gem_object_unreference(&obj->base);
  2260. DRM_ERROR("Failed to pin semaphore bo. Disabling semaphores\n");
  2261. i915.semaphores = 0;
  2262. } else {
  2263. dev_priv->semaphore_obj = obj;
  2264. }
  2265. }
  2266. }
  2267. if (!i915_semaphore_is_enabled(dev_priv))
  2268. return;
  2269. if (INTEL_GEN(dev_priv) >= 8) {
  2270. u64 offset = i915_gem_obj_ggtt_offset(dev_priv->semaphore_obj);
  2271. engine->semaphore.sync_to = gen8_ring_sync;
  2272. engine->semaphore.signal = gen8_xcs_signal;
  2273. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2274. u64 ring_offset;
  2275. if (i != engine->id)
  2276. ring_offset = offset + GEN8_SEMAPHORE_OFFSET(engine->id, i);
  2277. else
  2278. ring_offset = MI_SEMAPHORE_SYNC_INVALID;
  2279. engine->semaphore.signal_ggtt[i] = ring_offset;
  2280. }
  2281. } else if (INTEL_GEN(dev_priv) >= 6) {
  2282. engine->semaphore.sync_to = gen6_ring_sync;
  2283. engine->semaphore.signal = gen6_signal;
  2284. /*
  2285. * The current semaphore is only applied on pre-gen8
  2286. * platform. And there is no VCS2 ring on the pre-gen8
  2287. * platform. So the semaphore between RCS and VCS2 is
  2288. * initialized as INVALID. Gen8 will initialize the
  2289. * sema between VCS2 and RCS later.
  2290. */
  2291. for (i = 0; i < I915_NUM_ENGINES; i++) {
  2292. static const struct {
  2293. u32 wait_mbox;
  2294. i915_reg_t mbox_reg;
  2295. } sem_data[I915_NUM_ENGINES][I915_NUM_ENGINES] = {
  2296. [RCS] = {
  2297. [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RV, .mbox_reg = GEN6_VRSYNC },
  2298. [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RB, .mbox_reg = GEN6_BRSYNC },
  2299. [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_RVE, .mbox_reg = GEN6_VERSYNC },
  2300. },
  2301. [VCS] = {
  2302. [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VR, .mbox_reg = GEN6_RVSYNC },
  2303. [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VB, .mbox_reg = GEN6_BVSYNC },
  2304. [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VVE, .mbox_reg = GEN6_VEVSYNC },
  2305. },
  2306. [BCS] = {
  2307. [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BR, .mbox_reg = GEN6_RBSYNC },
  2308. [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BV, .mbox_reg = GEN6_VBSYNC },
  2309. [VECS] = { .wait_mbox = MI_SEMAPHORE_SYNC_BVE, .mbox_reg = GEN6_VEBSYNC },
  2310. },
  2311. [VECS] = {
  2312. [RCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VER, .mbox_reg = GEN6_RVESYNC },
  2313. [VCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEV, .mbox_reg = GEN6_VVESYNC },
  2314. [BCS] = { .wait_mbox = MI_SEMAPHORE_SYNC_VEB, .mbox_reg = GEN6_BVESYNC },
  2315. },
  2316. };
  2317. u32 wait_mbox;
  2318. i915_reg_t mbox_reg;
  2319. if (i == engine->id || i == VCS2) {
  2320. wait_mbox = MI_SEMAPHORE_SYNC_INVALID;
  2321. mbox_reg = GEN6_NOSYNC;
  2322. } else {
  2323. wait_mbox = sem_data[engine->id][i].wait_mbox;
  2324. mbox_reg = sem_data[engine->id][i].mbox_reg;
  2325. }
  2326. engine->semaphore.mbox.wait[i] = wait_mbox;
  2327. engine->semaphore.mbox.signal[i] = mbox_reg;
  2328. }
  2329. }
  2330. }
  2331. static void intel_ring_init_irq(struct drm_i915_private *dev_priv,
  2332. struct intel_engine_cs *engine)
  2333. {
  2334. if (INTEL_GEN(dev_priv) >= 8) {
  2335. engine->irq_enable = gen8_irq_enable;
  2336. engine->irq_disable = gen8_irq_disable;
  2337. engine->irq_seqno_barrier = gen6_seqno_barrier;
  2338. } else if (INTEL_GEN(dev_priv) >= 6) {
  2339. engine->irq_enable = gen6_irq_enable;
  2340. engine->irq_disable = gen6_irq_disable;
  2341. engine->irq_seqno_barrier = gen6_seqno_barrier;
  2342. } else if (INTEL_GEN(dev_priv) >= 5) {
  2343. engine->irq_enable = gen5_irq_enable;
  2344. engine->irq_disable = gen5_irq_disable;
  2345. engine->irq_seqno_barrier = gen5_seqno_barrier;
  2346. } else if (INTEL_GEN(dev_priv) >= 3) {
  2347. engine->irq_enable = i9xx_irq_enable;
  2348. engine->irq_disable = i9xx_irq_disable;
  2349. } else {
  2350. engine->irq_enable = i8xx_irq_enable;
  2351. engine->irq_disable = i8xx_irq_disable;
  2352. }
  2353. }
  2354. static void intel_ring_default_vfuncs(struct drm_i915_private *dev_priv,
  2355. struct intel_engine_cs *engine)
  2356. {
  2357. engine->init_hw = init_ring_common;
  2358. engine->write_tail = ring_write_tail;
  2359. engine->add_request = i9xx_add_request;
  2360. if (INTEL_GEN(dev_priv) >= 6)
  2361. engine->add_request = gen6_add_request;
  2362. if (INTEL_GEN(dev_priv) >= 8)
  2363. engine->dispatch_execbuffer = gen8_ring_dispatch_execbuffer;
  2364. else if (INTEL_GEN(dev_priv) >= 6)
  2365. engine->dispatch_execbuffer = gen6_ring_dispatch_execbuffer;
  2366. else if (INTEL_GEN(dev_priv) >= 4)
  2367. engine->dispatch_execbuffer = i965_dispatch_execbuffer;
  2368. else if (IS_I830(dev_priv) || IS_845G(dev_priv))
  2369. engine->dispatch_execbuffer = i830_dispatch_execbuffer;
  2370. else
  2371. engine->dispatch_execbuffer = i915_dispatch_execbuffer;
  2372. intel_ring_init_irq(dev_priv, engine);
  2373. intel_ring_init_semaphores(dev_priv, engine);
  2374. }
  2375. int intel_init_render_ring_buffer(struct drm_device *dev)
  2376. {
  2377. struct drm_i915_private *dev_priv = dev->dev_private;
  2378. struct intel_engine_cs *engine = &dev_priv->engine[RCS];
  2379. int ret;
  2380. engine->name = "render ring";
  2381. engine->id = RCS;
  2382. engine->exec_id = I915_EXEC_RENDER;
  2383. engine->hw_id = 0;
  2384. engine->mmio_base = RENDER_RING_BASE;
  2385. intel_ring_default_vfuncs(dev_priv, engine);
  2386. engine->irq_enable_mask = GT_RENDER_USER_INTERRUPT;
  2387. if (INTEL_GEN(dev_priv) >= 8) {
  2388. engine->init_context = intel_rcs_ctx_init;
  2389. engine->add_request = gen8_render_add_request;
  2390. engine->flush = gen8_render_ring_flush;
  2391. if (i915_semaphore_is_enabled(dev_priv))
  2392. engine->semaphore.signal = gen8_rcs_signal;
  2393. } else if (INTEL_GEN(dev_priv) >= 6) {
  2394. engine->init_context = intel_rcs_ctx_init;
  2395. engine->flush = gen7_render_ring_flush;
  2396. if (IS_GEN6(dev_priv))
  2397. engine->flush = gen6_render_ring_flush;
  2398. } else if (IS_GEN5(dev_priv)) {
  2399. engine->flush = gen4_render_ring_flush;
  2400. } else {
  2401. if (INTEL_GEN(dev_priv) < 4)
  2402. engine->flush = gen2_render_ring_flush;
  2403. else
  2404. engine->flush = gen4_render_ring_flush;
  2405. engine->irq_enable_mask = I915_USER_INTERRUPT;
  2406. }
  2407. if (IS_HASWELL(dev_priv))
  2408. engine->dispatch_execbuffer = hsw_ring_dispatch_execbuffer;
  2409. engine->init_hw = init_render_ring;
  2410. engine->cleanup = render_ring_cleanup;
  2411. ret = intel_init_ring_buffer(dev, engine);
  2412. if (ret)
  2413. return ret;
  2414. if (INTEL_GEN(dev_priv) >= 6) {
  2415. ret = intel_init_pipe_control(engine, 4096);
  2416. if (ret)
  2417. return ret;
  2418. } else if (HAS_BROKEN_CS_TLB(dev_priv)) {
  2419. ret = intel_init_pipe_control(engine, I830_WA_SIZE);
  2420. if (ret)
  2421. return ret;
  2422. }
  2423. return 0;
  2424. }
  2425. int intel_init_bsd_ring_buffer(struct drm_device *dev)
  2426. {
  2427. struct drm_i915_private *dev_priv = dev->dev_private;
  2428. struct intel_engine_cs *engine = &dev_priv->engine[VCS];
  2429. engine->name = "bsd ring";
  2430. engine->id = VCS;
  2431. engine->exec_id = I915_EXEC_BSD;
  2432. engine->hw_id = 1;
  2433. intel_ring_default_vfuncs(dev_priv, engine);
  2434. if (INTEL_GEN(dev_priv) >= 6) {
  2435. engine->mmio_base = GEN6_BSD_RING_BASE;
  2436. /* gen6 bsd needs a special wa for tail updates */
  2437. if (IS_GEN6(dev_priv))
  2438. engine->write_tail = gen6_bsd_ring_write_tail;
  2439. engine->flush = gen6_bsd_ring_flush;
  2440. if (INTEL_GEN(dev_priv) >= 8)
  2441. engine->irq_enable_mask =
  2442. GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT;
  2443. else
  2444. engine->irq_enable_mask = GT_BSD_USER_INTERRUPT;
  2445. } else {
  2446. engine->mmio_base = BSD_RING_BASE;
  2447. engine->flush = bsd_ring_flush;
  2448. if (IS_GEN5(dev_priv))
  2449. engine->irq_enable_mask = ILK_BSD_USER_INTERRUPT;
  2450. else
  2451. engine->irq_enable_mask = I915_BSD_USER_INTERRUPT;
  2452. }
  2453. return intel_init_ring_buffer(dev, engine);
  2454. }
  2455. /**
  2456. * Initialize the second BSD ring (eg. Broadwell GT3, Skylake GT3)
  2457. */
  2458. int intel_init_bsd2_ring_buffer(struct drm_device *dev)
  2459. {
  2460. struct drm_i915_private *dev_priv = dev->dev_private;
  2461. struct intel_engine_cs *engine = &dev_priv->engine[VCS2];
  2462. engine->name = "bsd2 ring";
  2463. engine->id = VCS2;
  2464. engine->exec_id = I915_EXEC_BSD;
  2465. engine->hw_id = 4;
  2466. engine->mmio_base = GEN8_BSD2_RING_BASE;
  2467. intel_ring_default_vfuncs(dev_priv, engine);
  2468. engine->flush = gen6_bsd_ring_flush;
  2469. engine->irq_enable_mask =
  2470. GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT;
  2471. return intel_init_ring_buffer(dev, engine);
  2472. }
  2473. int intel_init_blt_ring_buffer(struct drm_device *dev)
  2474. {
  2475. struct drm_i915_private *dev_priv = dev->dev_private;
  2476. struct intel_engine_cs *engine = &dev_priv->engine[BCS];
  2477. engine->name = "blitter ring";
  2478. engine->id = BCS;
  2479. engine->exec_id = I915_EXEC_BLT;
  2480. engine->hw_id = 2;
  2481. engine->mmio_base = BLT_RING_BASE;
  2482. intel_ring_default_vfuncs(dev_priv, engine);
  2483. engine->flush = gen6_ring_flush;
  2484. if (INTEL_GEN(dev_priv) >= 8)
  2485. engine->irq_enable_mask =
  2486. GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT;
  2487. else
  2488. engine->irq_enable_mask = GT_BLT_USER_INTERRUPT;
  2489. return intel_init_ring_buffer(dev, engine);
  2490. }
  2491. int intel_init_vebox_ring_buffer(struct drm_device *dev)
  2492. {
  2493. struct drm_i915_private *dev_priv = dev->dev_private;
  2494. struct intel_engine_cs *engine = &dev_priv->engine[VECS];
  2495. engine->name = "video enhancement ring";
  2496. engine->id = VECS;
  2497. engine->exec_id = I915_EXEC_VEBOX;
  2498. engine->hw_id = 3;
  2499. engine->mmio_base = VEBOX_RING_BASE;
  2500. intel_ring_default_vfuncs(dev_priv, engine);
  2501. engine->flush = gen6_ring_flush;
  2502. if (INTEL_GEN(dev_priv) >= 8) {
  2503. engine->irq_enable_mask =
  2504. GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT;
  2505. } else {
  2506. engine->irq_enable_mask = PM_VEBOX_USER_INTERRUPT;
  2507. engine->irq_enable = hsw_vebox_irq_enable;
  2508. engine->irq_disable = hsw_vebox_irq_disable;
  2509. }
  2510. return intel_init_ring_buffer(dev, engine);
  2511. }
  2512. int
  2513. intel_ring_flush_all_caches(struct drm_i915_gem_request *req)
  2514. {
  2515. struct intel_engine_cs *engine = req->engine;
  2516. int ret;
  2517. if (!engine->gpu_caches_dirty)
  2518. return 0;
  2519. ret = engine->flush(req, 0, I915_GEM_GPU_DOMAINS);
  2520. if (ret)
  2521. return ret;
  2522. trace_i915_gem_ring_flush(req, 0, I915_GEM_GPU_DOMAINS);
  2523. engine->gpu_caches_dirty = false;
  2524. return 0;
  2525. }
  2526. int
  2527. intel_ring_invalidate_all_caches(struct drm_i915_gem_request *req)
  2528. {
  2529. struct intel_engine_cs *engine = req->engine;
  2530. uint32_t flush_domains;
  2531. int ret;
  2532. flush_domains = 0;
  2533. if (engine->gpu_caches_dirty)
  2534. flush_domains = I915_GEM_GPU_DOMAINS;
  2535. ret = engine->flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
  2536. if (ret)
  2537. return ret;
  2538. trace_i915_gem_ring_flush(req, I915_GEM_GPU_DOMAINS, flush_domains);
  2539. engine->gpu_caches_dirty = false;
  2540. return 0;
  2541. }
  2542. void
  2543. intel_stop_engine(struct intel_engine_cs *engine)
  2544. {
  2545. int ret;
  2546. if (!intel_engine_initialized(engine))
  2547. return;
  2548. ret = intel_engine_idle(engine);
  2549. if (ret)
  2550. DRM_ERROR("failed to quiesce %s whilst cleaning up: %d\n",
  2551. engine->name, ret);
  2552. stop_ring(engine);
  2553. }