i915_gem_gtt.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582
  1. /*
  2. * Copyright © 2010 Daniel Vetter
  3. * Copyright © 2011-2014 Intel Corporation
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. *
  24. */
  25. #include <linux/slab.h> /* fault-inject.h is not standalone! */
  26. #include <linux/fault-inject.h>
  27. #include <linux/log2.h>
  28. #include <linux/random.h>
  29. #include <linux/seq_file.h>
  30. #include <linux/stop_machine.h>
  31. #include <asm/set_memory.h>
  32. #include <drm/drmP.h>
  33. #include <drm/i915_drm.h>
  34. #include "i915_drv.h"
  35. #include "i915_vgpu.h"
  36. #include "i915_trace.h"
  37. #include "intel_drv.h"
  38. #include "intel_frontbuffer.h"
  39. #define I915_GFP_DMA (GFP_KERNEL | __GFP_HIGHMEM)
  40. /**
  41. * DOC: Global GTT views
  42. *
  43. * Background and previous state
  44. *
  45. * Historically objects could exists (be bound) in global GTT space only as
  46. * singular instances with a view representing all of the object's backing pages
  47. * in a linear fashion. This view will be called a normal view.
  48. *
  49. * To support multiple views of the same object, where the number of mapped
  50. * pages is not equal to the backing store, or where the layout of the pages
  51. * is not linear, concept of a GGTT view was added.
  52. *
  53. * One example of an alternative view is a stereo display driven by a single
  54. * image. In this case we would have a framebuffer looking like this
  55. * (2x2 pages):
  56. *
  57. * 12
  58. * 34
  59. *
  60. * Above would represent a normal GGTT view as normally mapped for GPU or CPU
  61. * rendering. In contrast, fed to the display engine would be an alternative
  62. * view which could look something like this:
  63. *
  64. * 1212
  65. * 3434
  66. *
  67. * In this example both the size and layout of pages in the alternative view is
  68. * different from the normal view.
  69. *
  70. * Implementation and usage
  71. *
  72. * GGTT views are implemented using VMAs and are distinguished via enum
  73. * i915_ggtt_view_type and struct i915_ggtt_view.
  74. *
  75. * A new flavour of core GEM functions which work with GGTT bound objects were
  76. * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
  77. * renaming in large amounts of code. They take the struct i915_ggtt_view
  78. * parameter encapsulating all metadata required to implement a view.
  79. *
  80. * As a helper for callers which are only interested in the normal view,
  81. * globally const i915_ggtt_view_normal singleton instance exists. All old core
  82. * GEM API functions, the ones not taking the view parameter, are operating on,
  83. * or with the normal GGTT view.
  84. *
  85. * Code wanting to add or use a new GGTT view needs to:
  86. *
  87. * 1. Add a new enum with a suitable name.
  88. * 2. Extend the metadata in the i915_ggtt_view structure if required.
  89. * 3. Add support to i915_get_vma_pages().
  90. *
  91. * New views are required to build a scatter-gather table from within the
  92. * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
  93. * exists for the lifetime of an VMA.
  94. *
  95. * Core API is designed to have copy semantics which means that passed in
  96. * struct i915_ggtt_view does not need to be persistent (left around after
  97. * calling the core API functions).
  98. *
  99. */
  100. static int
  101. i915_get_ggtt_vma_pages(struct i915_vma *vma);
  102. static void gen6_ggtt_invalidate(struct drm_i915_private *dev_priv)
  103. {
  104. /* Note that as an uncached mmio write, this should flush the
  105. * WCB of the writes into the GGTT before it triggers the invalidate.
  106. */
  107. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  108. }
  109. static void guc_ggtt_invalidate(struct drm_i915_private *dev_priv)
  110. {
  111. gen6_ggtt_invalidate(dev_priv);
  112. I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
  113. }
  114. static void gmch_ggtt_invalidate(struct drm_i915_private *dev_priv)
  115. {
  116. intel_gtt_chipset_flush();
  117. }
  118. static inline void i915_ggtt_invalidate(struct drm_i915_private *i915)
  119. {
  120. i915->ggtt.invalidate(i915);
  121. }
  122. int intel_sanitize_enable_ppgtt(struct drm_i915_private *dev_priv,
  123. int enable_ppgtt)
  124. {
  125. bool has_aliasing_ppgtt;
  126. bool has_full_ppgtt;
  127. bool has_full_48bit_ppgtt;
  128. has_aliasing_ppgtt = dev_priv->info.has_aliasing_ppgtt;
  129. has_full_ppgtt = dev_priv->info.has_full_ppgtt;
  130. has_full_48bit_ppgtt = dev_priv->info.has_full_48bit_ppgtt;
  131. if (intel_vgpu_active(dev_priv)) {
  132. /* GVT-g has no support for 32bit ppgtt */
  133. has_full_ppgtt = false;
  134. has_full_48bit_ppgtt = intel_vgpu_has_full_48bit_ppgtt(dev_priv);
  135. }
  136. if (!has_aliasing_ppgtt)
  137. return 0;
  138. /*
  139. * We don't allow disabling PPGTT for gen9+ as it's a requirement for
  140. * execlists, the sole mechanism available to submit work.
  141. */
  142. if (enable_ppgtt == 0 && INTEL_GEN(dev_priv) < 9)
  143. return 0;
  144. if (enable_ppgtt == 1)
  145. return 1;
  146. if (enable_ppgtt == 2 && has_full_ppgtt)
  147. return 2;
  148. if (enable_ppgtt == 3 && has_full_48bit_ppgtt)
  149. return 3;
  150. /* Disable ppgtt on SNB if VT-d is on. */
  151. if (IS_GEN6(dev_priv) && intel_vtd_active()) {
  152. DRM_INFO("Disabling PPGTT because VT-d is on\n");
  153. return 0;
  154. }
  155. /* Early VLV doesn't have this */
  156. if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
  157. DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
  158. return 0;
  159. }
  160. if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists) {
  161. if (has_full_48bit_ppgtt)
  162. return 3;
  163. if (has_full_ppgtt)
  164. return 2;
  165. }
  166. return has_aliasing_ppgtt ? 1 : 0;
  167. }
  168. static int ppgtt_bind_vma(struct i915_vma *vma,
  169. enum i915_cache_level cache_level,
  170. u32 unused)
  171. {
  172. u32 pte_flags;
  173. int ret;
  174. if (!(vma->flags & I915_VMA_LOCAL_BIND)) {
  175. ret = vma->vm->allocate_va_range(vma->vm, vma->node.start,
  176. vma->size);
  177. if (ret)
  178. return ret;
  179. }
  180. vma->pages = vma->obj->mm.pages;
  181. /* Currently applicable only to VLV */
  182. pte_flags = 0;
  183. if (vma->obj->gt_ro)
  184. pte_flags |= PTE_READ_ONLY;
  185. vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
  186. return 0;
  187. }
  188. static void ppgtt_unbind_vma(struct i915_vma *vma)
  189. {
  190. vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
  191. }
  192. static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
  193. enum i915_cache_level level)
  194. {
  195. gen8_pte_t pte = _PAGE_PRESENT | _PAGE_RW;
  196. pte |= addr;
  197. switch (level) {
  198. case I915_CACHE_NONE:
  199. pte |= PPAT_UNCACHED_INDEX;
  200. break;
  201. case I915_CACHE_WT:
  202. pte |= PPAT_DISPLAY_ELLC_INDEX;
  203. break;
  204. default:
  205. pte |= PPAT_CACHED_INDEX;
  206. break;
  207. }
  208. return pte;
  209. }
  210. static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
  211. const enum i915_cache_level level)
  212. {
  213. gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
  214. pde |= addr;
  215. if (level != I915_CACHE_NONE)
  216. pde |= PPAT_CACHED_PDE_INDEX;
  217. else
  218. pde |= PPAT_UNCACHED_INDEX;
  219. return pde;
  220. }
  221. #define gen8_pdpe_encode gen8_pde_encode
  222. #define gen8_pml4e_encode gen8_pde_encode
  223. static gen6_pte_t snb_pte_encode(dma_addr_t addr,
  224. enum i915_cache_level level,
  225. u32 unused)
  226. {
  227. gen6_pte_t pte = GEN6_PTE_VALID;
  228. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  229. switch (level) {
  230. case I915_CACHE_L3_LLC:
  231. case I915_CACHE_LLC:
  232. pte |= GEN6_PTE_CACHE_LLC;
  233. break;
  234. case I915_CACHE_NONE:
  235. pte |= GEN6_PTE_UNCACHED;
  236. break;
  237. default:
  238. MISSING_CASE(level);
  239. }
  240. return pte;
  241. }
  242. static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
  243. enum i915_cache_level level,
  244. u32 unused)
  245. {
  246. gen6_pte_t pte = GEN6_PTE_VALID;
  247. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  248. switch (level) {
  249. case I915_CACHE_L3_LLC:
  250. pte |= GEN7_PTE_CACHE_L3_LLC;
  251. break;
  252. case I915_CACHE_LLC:
  253. pte |= GEN6_PTE_CACHE_LLC;
  254. break;
  255. case I915_CACHE_NONE:
  256. pte |= GEN6_PTE_UNCACHED;
  257. break;
  258. default:
  259. MISSING_CASE(level);
  260. }
  261. return pte;
  262. }
  263. static gen6_pte_t byt_pte_encode(dma_addr_t addr,
  264. enum i915_cache_level level,
  265. u32 flags)
  266. {
  267. gen6_pte_t pte = GEN6_PTE_VALID;
  268. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  269. if (!(flags & PTE_READ_ONLY))
  270. pte |= BYT_PTE_WRITEABLE;
  271. if (level != I915_CACHE_NONE)
  272. pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
  273. return pte;
  274. }
  275. static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
  276. enum i915_cache_level level,
  277. u32 unused)
  278. {
  279. gen6_pte_t pte = GEN6_PTE_VALID;
  280. pte |= HSW_PTE_ADDR_ENCODE(addr);
  281. if (level != I915_CACHE_NONE)
  282. pte |= HSW_WB_LLC_AGE3;
  283. return pte;
  284. }
  285. static gen6_pte_t iris_pte_encode(dma_addr_t addr,
  286. enum i915_cache_level level,
  287. u32 unused)
  288. {
  289. gen6_pte_t pte = GEN6_PTE_VALID;
  290. pte |= HSW_PTE_ADDR_ENCODE(addr);
  291. switch (level) {
  292. case I915_CACHE_NONE:
  293. break;
  294. case I915_CACHE_WT:
  295. pte |= HSW_WT_ELLC_LLC_AGE3;
  296. break;
  297. default:
  298. pte |= HSW_WB_ELLC_LLC_AGE3;
  299. break;
  300. }
  301. return pte;
  302. }
  303. static struct page *vm_alloc_page(struct i915_address_space *vm, gfp_t gfp)
  304. {
  305. struct page *page;
  306. if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
  307. i915_gem_shrink_all(vm->i915);
  308. if (vm->free_pages.nr)
  309. return vm->free_pages.pages[--vm->free_pages.nr];
  310. page = alloc_page(gfp);
  311. if (!page)
  312. return NULL;
  313. if (vm->pt_kmap_wc)
  314. set_pages_array_wc(&page, 1);
  315. return page;
  316. }
  317. static void vm_free_pages_release(struct i915_address_space *vm)
  318. {
  319. GEM_BUG_ON(!pagevec_count(&vm->free_pages));
  320. if (vm->pt_kmap_wc)
  321. set_pages_array_wb(vm->free_pages.pages,
  322. pagevec_count(&vm->free_pages));
  323. __pagevec_release(&vm->free_pages);
  324. }
  325. static void vm_free_page(struct i915_address_space *vm, struct page *page)
  326. {
  327. if (!pagevec_add(&vm->free_pages, page))
  328. vm_free_pages_release(vm);
  329. }
  330. static int __setup_page_dma(struct i915_address_space *vm,
  331. struct i915_page_dma *p,
  332. gfp_t gfp)
  333. {
  334. p->page = vm_alloc_page(vm, gfp | __GFP_NOWARN | __GFP_NORETRY);
  335. if (unlikely(!p->page))
  336. return -ENOMEM;
  337. p->daddr = dma_map_page(vm->dma, p->page, 0, PAGE_SIZE,
  338. PCI_DMA_BIDIRECTIONAL);
  339. if (unlikely(dma_mapping_error(vm->dma, p->daddr))) {
  340. vm_free_page(vm, p->page);
  341. return -ENOMEM;
  342. }
  343. return 0;
  344. }
  345. static int setup_page_dma(struct i915_address_space *vm,
  346. struct i915_page_dma *p)
  347. {
  348. return __setup_page_dma(vm, p, I915_GFP_DMA);
  349. }
  350. static void cleanup_page_dma(struct i915_address_space *vm,
  351. struct i915_page_dma *p)
  352. {
  353. dma_unmap_page(vm->dma, p->daddr, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  354. vm_free_page(vm, p->page);
  355. }
  356. #define kmap_atomic_px(px) kmap_atomic(px_base(px)->page)
  357. #define setup_px(vm, px) setup_page_dma((vm), px_base(px))
  358. #define cleanup_px(vm, px) cleanup_page_dma((vm), px_base(px))
  359. #define fill_px(ppgtt, px, v) fill_page_dma((vm), px_base(px), (v))
  360. #define fill32_px(ppgtt, px, v) fill_page_dma_32((vm), px_base(px), (v))
  361. static void fill_page_dma(struct i915_address_space *vm,
  362. struct i915_page_dma *p,
  363. const u64 val)
  364. {
  365. u64 * const vaddr = kmap_atomic(p->page);
  366. int i;
  367. for (i = 0; i < 512; i++)
  368. vaddr[i] = val;
  369. kunmap_atomic(vaddr);
  370. }
  371. static void fill_page_dma_32(struct i915_address_space *vm,
  372. struct i915_page_dma *p,
  373. const u32 v)
  374. {
  375. fill_page_dma(vm, p, (u64)v << 32 | v);
  376. }
  377. static int
  378. setup_scratch_page(struct i915_address_space *vm, gfp_t gfp)
  379. {
  380. return __setup_page_dma(vm, &vm->scratch_page, gfp | __GFP_ZERO);
  381. }
  382. static void cleanup_scratch_page(struct i915_address_space *vm)
  383. {
  384. cleanup_page_dma(vm, &vm->scratch_page);
  385. }
  386. static struct i915_page_table *alloc_pt(struct i915_address_space *vm)
  387. {
  388. struct i915_page_table *pt;
  389. pt = kmalloc(sizeof(*pt), GFP_KERNEL | __GFP_NOWARN);
  390. if (unlikely(!pt))
  391. return ERR_PTR(-ENOMEM);
  392. if (unlikely(setup_px(vm, pt))) {
  393. kfree(pt);
  394. return ERR_PTR(-ENOMEM);
  395. }
  396. pt->used_ptes = 0;
  397. return pt;
  398. }
  399. static void free_pt(struct i915_address_space *vm, struct i915_page_table *pt)
  400. {
  401. cleanup_px(vm, pt);
  402. kfree(pt);
  403. }
  404. static void gen8_initialize_pt(struct i915_address_space *vm,
  405. struct i915_page_table *pt)
  406. {
  407. fill_px(vm, pt,
  408. gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC));
  409. }
  410. static void gen6_initialize_pt(struct i915_address_space *vm,
  411. struct i915_page_table *pt)
  412. {
  413. fill32_px(vm, pt,
  414. vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0));
  415. }
  416. static struct i915_page_directory *alloc_pd(struct i915_address_space *vm)
  417. {
  418. struct i915_page_directory *pd;
  419. pd = kzalloc(sizeof(*pd), GFP_KERNEL | __GFP_NOWARN);
  420. if (unlikely(!pd))
  421. return ERR_PTR(-ENOMEM);
  422. if (unlikely(setup_px(vm, pd))) {
  423. kfree(pd);
  424. return ERR_PTR(-ENOMEM);
  425. }
  426. pd->used_pdes = 0;
  427. return pd;
  428. }
  429. static void free_pd(struct i915_address_space *vm,
  430. struct i915_page_directory *pd)
  431. {
  432. cleanup_px(vm, pd);
  433. kfree(pd);
  434. }
  435. static void gen8_initialize_pd(struct i915_address_space *vm,
  436. struct i915_page_directory *pd)
  437. {
  438. unsigned int i;
  439. fill_px(vm, pd,
  440. gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC));
  441. for (i = 0; i < I915_PDES; i++)
  442. pd->page_table[i] = vm->scratch_pt;
  443. }
  444. static int __pdp_init(struct i915_address_space *vm,
  445. struct i915_page_directory_pointer *pdp)
  446. {
  447. const unsigned int pdpes = i915_pdpes_per_pdp(vm);
  448. unsigned int i;
  449. pdp->page_directory = kmalloc_array(pdpes, sizeof(*pdp->page_directory),
  450. GFP_KERNEL | __GFP_NOWARN);
  451. if (unlikely(!pdp->page_directory))
  452. return -ENOMEM;
  453. for (i = 0; i < pdpes; i++)
  454. pdp->page_directory[i] = vm->scratch_pd;
  455. return 0;
  456. }
  457. static void __pdp_fini(struct i915_page_directory_pointer *pdp)
  458. {
  459. kfree(pdp->page_directory);
  460. pdp->page_directory = NULL;
  461. }
  462. static inline bool use_4lvl(const struct i915_address_space *vm)
  463. {
  464. return i915_vm_is_48bit(vm);
  465. }
  466. static struct i915_page_directory_pointer *
  467. alloc_pdp(struct i915_address_space *vm)
  468. {
  469. struct i915_page_directory_pointer *pdp;
  470. int ret = -ENOMEM;
  471. WARN_ON(!use_4lvl(vm));
  472. pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
  473. if (!pdp)
  474. return ERR_PTR(-ENOMEM);
  475. ret = __pdp_init(vm, pdp);
  476. if (ret)
  477. goto fail_bitmap;
  478. ret = setup_px(vm, pdp);
  479. if (ret)
  480. goto fail_page_m;
  481. return pdp;
  482. fail_page_m:
  483. __pdp_fini(pdp);
  484. fail_bitmap:
  485. kfree(pdp);
  486. return ERR_PTR(ret);
  487. }
  488. static void free_pdp(struct i915_address_space *vm,
  489. struct i915_page_directory_pointer *pdp)
  490. {
  491. __pdp_fini(pdp);
  492. if (!use_4lvl(vm))
  493. return;
  494. cleanup_px(vm, pdp);
  495. kfree(pdp);
  496. }
  497. static void gen8_initialize_pdp(struct i915_address_space *vm,
  498. struct i915_page_directory_pointer *pdp)
  499. {
  500. gen8_ppgtt_pdpe_t scratch_pdpe;
  501. scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
  502. fill_px(vm, pdp, scratch_pdpe);
  503. }
  504. static void gen8_initialize_pml4(struct i915_address_space *vm,
  505. struct i915_pml4 *pml4)
  506. {
  507. unsigned int i;
  508. fill_px(vm, pml4,
  509. gen8_pml4e_encode(px_dma(vm->scratch_pdp), I915_CACHE_LLC));
  510. for (i = 0; i < GEN8_PML4ES_PER_PML4; i++)
  511. pml4->pdps[i] = vm->scratch_pdp;
  512. }
  513. /* Broadwell Page Directory Pointer Descriptors */
  514. static int gen8_write_pdp(struct drm_i915_gem_request *req,
  515. unsigned entry,
  516. dma_addr_t addr)
  517. {
  518. struct intel_engine_cs *engine = req->engine;
  519. u32 *cs;
  520. BUG_ON(entry >= 4);
  521. cs = intel_ring_begin(req, 6);
  522. if (IS_ERR(cs))
  523. return PTR_ERR(cs);
  524. *cs++ = MI_LOAD_REGISTER_IMM(1);
  525. *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_UDW(engine, entry));
  526. *cs++ = upper_32_bits(addr);
  527. *cs++ = MI_LOAD_REGISTER_IMM(1);
  528. *cs++ = i915_mmio_reg_offset(GEN8_RING_PDP_LDW(engine, entry));
  529. *cs++ = lower_32_bits(addr);
  530. intel_ring_advance(req, cs);
  531. return 0;
  532. }
  533. static int gen8_mm_switch_3lvl(struct i915_hw_ppgtt *ppgtt,
  534. struct drm_i915_gem_request *req)
  535. {
  536. int i, ret;
  537. for (i = GEN8_3LVL_PDPES - 1; i >= 0; i--) {
  538. const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
  539. ret = gen8_write_pdp(req, i, pd_daddr);
  540. if (ret)
  541. return ret;
  542. }
  543. return 0;
  544. }
  545. static int gen8_mm_switch_4lvl(struct i915_hw_ppgtt *ppgtt,
  546. struct drm_i915_gem_request *req)
  547. {
  548. return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
  549. }
  550. /* PDE TLBs are a pain to invalidate on GEN8+. When we modify
  551. * the page table structures, we mark them dirty so that
  552. * context switching/execlist queuing code takes extra steps
  553. * to ensure that tlbs are flushed.
  554. */
  555. static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
  556. {
  557. ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.i915)->ring_mask;
  558. }
  559. /* Removes entries from a single page table, releasing it if it's empty.
  560. * Caller can use the return value to update higher-level entries.
  561. */
  562. static bool gen8_ppgtt_clear_pt(struct i915_address_space *vm,
  563. struct i915_page_table *pt,
  564. u64 start, u64 length)
  565. {
  566. unsigned int num_entries = gen8_pte_count(start, length);
  567. unsigned int pte = gen8_pte_index(start);
  568. unsigned int pte_end = pte + num_entries;
  569. const gen8_pte_t scratch_pte =
  570. gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
  571. gen8_pte_t *vaddr;
  572. GEM_BUG_ON(num_entries > pt->used_ptes);
  573. pt->used_ptes -= num_entries;
  574. if (!pt->used_ptes)
  575. return true;
  576. vaddr = kmap_atomic_px(pt);
  577. while (pte < pte_end)
  578. vaddr[pte++] = scratch_pte;
  579. kunmap_atomic(vaddr);
  580. return false;
  581. }
  582. static void gen8_ppgtt_set_pde(struct i915_address_space *vm,
  583. struct i915_page_directory *pd,
  584. struct i915_page_table *pt,
  585. unsigned int pde)
  586. {
  587. gen8_pde_t *vaddr;
  588. pd->page_table[pde] = pt;
  589. vaddr = kmap_atomic_px(pd);
  590. vaddr[pde] = gen8_pde_encode(px_dma(pt), I915_CACHE_LLC);
  591. kunmap_atomic(vaddr);
  592. }
  593. static bool gen8_ppgtt_clear_pd(struct i915_address_space *vm,
  594. struct i915_page_directory *pd,
  595. u64 start, u64 length)
  596. {
  597. struct i915_page_table *pt;
  598. u32 pde;
  599. gen8_for_each_pde(pt, pd, start, length, pde) {
  600. GEM_BUG_ON(pt == vm->scratch_pt);
  601. if (!gen8_ppgtt_clear_pt(vm, pt, start, length))
  602. continue;
  603. gen8_ppgtt_set_pde(vm, pd, vm->scratch_pt, pde);
  604. GEM_BUG_ON(!pd->used_pdes);
  605. pd->used_pdes--;
  606. free_pt(vm, pt);
  607. }
  608. return !pd->used_pdes;
  609. }
  610. static void gen8_ppgtt_set_pdpe(struct i915_address_space *vm,
  611. struct i915_page_directory_pointer *pdp,
  612. struct i915_page_directory *pd,
  613. unsigned int pdpe)
  614. {
  615. gen8_ppgtt_pdpe_t *vaddr;
  616. pdp->page_directory[pdpe] = pd;
  617. if (!use_4lvl(vm))
  618. return;
  619. vaddr = kmap_atomic_px(pdp);
  620. vaddr[pdpe] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
  621. kunmap_atomic(vaddr);
  622. }
  623. /* Removes entries from a single page dir pointer, releasing it if it's empty.
  624. * Caller can use the return value to update higher-level entries
  625. */
  626. static bool gen8_ppgtt_clear_pdp(struct i915_address_space *vm,
  627. struct i915_page_directory_pointer *pdp,
  628. u64 start, u64 length)
  629. {
  630. struct i915_page_directory *pd;
  631. unsigned int pdpe;
  632. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  633. GEM_BUG_ON(pd == vm->scratch_pd);
  634. if (!gen8_ppgtt_clear_pd(vm, pd, start, length))
  635. continue;
  636. gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
  637. GEM_BUG_ON(!pdp->used_pdpes);
  638. pdp->used_pdpes--;
  639. free_pd(vm, pd);
  640. }
  641. return !pdp->used_pdpes;
  642. }
  643. static void gen8_ppgtt_clear_3lvl(struct i915_address_space *vm,
  644. u64 start, u64 length)
  645. {
  646. gen8_ppgtt_clear_pdp(vm, &i915_vm_to_ppgtt(vm)->pdp, start, length);
  647. }
  648. static void gen8_ppgtt_set_pml4e(struct i915_pml4 *pml4,
  649. struct i915_page_directory_pointer *pdp,
  650. unsigned int pml4e)
  651. {
  652. gen8_ppgtt_pml4e_t *vaddr;
  653. pml4->pdps[pml4e] = pdp;
  654. vaddr = kmap_atomic_px(pml4);
  655. vaddr[pml4e] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
  656. kunmap_atomic(vaddr);
  657. }
  658. /* Removes entries from a single pml4.
  659. * This is the top-level structure in 4-level page tables used on gen8+.
  660. * Empty entries are always scratch pml4e.
  661. */
  662. static void gen8_ppgtt_clear_4lvl(struct i915_address_space *vm,
  663. u64 start, u64 length)
  664. {
  665. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  666. struct i915_pml4 *pml4 = &ppgtt->pml4;
  667. struct i915_page_directory_pointer *pdp;
  668. unsigned int pml4e;
  669. GEM_BUG_ON(!use_4lvl(vm));
  670. gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
  671. GEM_BUG_ON(pdp == vm->scratch_pdp);
  672. if (!gen8_ppgtt_clear_pdp(vm, pdp, start, length))
  673. continue;
  674. gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
  675. free_pdp(vm, pdp);
  676. }
  677. }
  678. struct sgt_dma {
  679. struct scatterlist *sg;
  680. dma_addr_t dma, max;
  681. };
  682. struct gen8_insert_pte {
  683. u16 pml4e;
  684. u16 pdpe;
  685. u16 pde;
  686. u16 pte;
  687. };
  688. static __always_inline struct gen8_insert_pte gen8_insert_pte(u64 start)
  689. {
  690. return (struct gen8_insert_pte) {
  691. gen8_pml4e_index(start),
  692. gen8_pdpe_index(start),
  693. gen8_pde_index(start),
  694. gen8_pte_index(start),
  695. };
  696. }
  697. static __always_inline bool
  698. gen8_ppgtt_insert_pte_entries(struct i915_hw_ppgtt *ppgtt,
  699. struct i915_page_directory_pointer *pdp,
  700. struct sgt_dma *iter,
  701. struct gen8_insert_pte *idx,
  702. enum i915_cache_level cache_level)
  703. {
  704. struct i915_page_directory *pd;
  705. const gen8_pte_t pte_encode = gen8_pte_encode(0, cache_level);
  706. gen8_pte_t *vaddr;
  707. bool ret;
  708. GEM_BUG_ON(idx->pdpe >= i915_pdpes_per_pdp(&ppgtt->base));
  709. pd = pdp->page_directory[idx->pdpe];
  710. vaddr = kmap_atomic_px(pd->page_table[idx->pde]);
  711. do {
  712. vaddr[idx->pte] = pte_encode | iter->dma;
  713. iter->dma += PAGE_SIZE;
  714. if (iter->dma >= iter->max) {
  715. iter->sg = __sg_next(iter->sg);
  716. if (!iter->sg) {
  717. ret = false;
  718. break;
  719. }
  720. iter->dma = sg_dma_address(iter->sg);
  721. iter->max = iter->dma + iter->sg->length;
  722. }
  723. if (++idx->pte == GEN8_PTES) {
  724. idx->pte = 0;
  725. if (++idx->pde == I915_PDES) {
  726. idx->pde = 0;
  727. /* Limited by sg length for 3lvl */
  728. if (++idx->pdpe == GEN8_PML4ES_PER_PML4) {
  729. idx->pdpe = 0;
  730. ret = true;
  731. break;
  732. }
  733. GEM_BUG_ON(idx->pdpe >= i915_pdpes_per_pdp(&ppgtt->base));
  734. pd = pdp->page_directory[idx->pdpe];
  735. }
  736. kunmap_atomic(vaddr);
  737. vaddr = kmap_atomic_px(pd->page_table[idx->pde]);
  738. }
  739. } while (1);
  740. kunmap_atomic(vaddr);
  741. return ret;
  742. }
  743. static void gen8_ppgtt_insert_3lvl(struct i915_address_space *vm,
  744. struct i915_vma *vma,
  745. enum i915_cache_level cache_level,
  746. u32 unused)
  747. {
  748. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  749. struct sgt_dma iter = {
  750. .sg = vma->pages->sgl,
  751. .dma = sg_dma_address(iter.sg),
  752. .max = iter.dma + iter.sg->length,
  753. };
  754. struct gen8_insert_pte idx = gen8_insert_pte(vma->node.start);
  755. gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter, &idx,
  756. cache_level);
  757. }
  758. static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
  759. struct i915_vma *vma,
  760. enum i915_cache_level cache_level,
  761. u32 unused)
  762. {
  763. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  764. struct sgt_dma iter = {
  765. .sg = vma->pages->sgl,
  766. .dma = sg_dma_address(iter.sg),
  767. .max = iter.dma + iter.sg->length,
  768. };
  769. struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
  770. struct gen8_insert_pte idx = gen8_insert_pte(vma->node.start);
  771. while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[idx.pml4e++], &iter,
  772. &idx, cache_level))
  773. GEM_BUG_ON(idx.pml4e >= GEN8_PML4ES_PER_PML4);
  774. }
  775. static void gen8_free_page_tables(struct i915_address_space *vm,
  776. struct i915_page_directory *pd)
  777. {
  778. int i;
  779. if (!px_page(pd))
  780. return;
  781. for (i = 0; i < I915_PDES; i++) {
  782. if (pd->page_table[i] != vm->scratch_pt)
  783. free_pt(vm, pd->page_table[i]);
  784. }
  785. }
  786. static int gen8_init_scratch(struct i915_address_space *vm)
  787. {
  788. int ret;
  789. ret = setup_scratch_page(vm, I915_GFP_DMA);
  790. if (ret)
  791. return ret;
  792. vm->scratch_pt = alloc_pt(vm);
  793. if (IS_ERR(vm->scratch_pt)) {
  794. ret = PTR_ERR(vm->scratch_pt);
  795. goto free_scratch_page;
  796. }
  797. vm->scratch_pd = alloc_pd(vm);
  798. if (IS_ERR(vm->scratch_pd)) {
  799. ret = PTR_ERR(vm->scratch_pd);
  800. goto free_pt;
  801. }
  802. if (use_4lvl(vm)) {
  803. vm->scratch_pdp = alloc_pdp(vm);
  804. if (IS_ERR(vm->scratch_pdp)) {
  805. ret = PTR_ERR(vm->scratch_pdp);
  806. goto free_pd;
  807. }
  808. }
  809. gen8_initialize_pt(vm, vm->scratch_pt);
  810. gen8_initialize_pd(vm, vm->scratch_pd);
  811. if (use_4lvl(vm))
  812. gen8_initialize_pdp(vm, vm->scratch_pdp);
  813. return 0;
  814. free_pd:
  815. free_pd(vm, vm->scratch_pd);
  816. free_pt:
  817. free_pt(vm, vm->scratch_pt);
  818. free_scratch_page:
  819. cleanup_scratch_page(vm);
  820. return ret;
  821. }
  822. static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
  823. {
  824. struct i915_address_space *vm = &ppgtt->base;
  825. struct drm_i915_private *dev_priv = vm->i915;
  826. enum vgt_g2v_type msg;
  827. int i;
  828. if (use_4lvl(vm)) {
  829. const u64 daddr = px_dma(&ppgtt->pml4);
  830. I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
  831. I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
  832. msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
  833. VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
  834. } else {
  835. for (i = 0; i < GEN8_3LVL_PDPES; i++) {
  836. const u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
  837. I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
  838. I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
  839. }
  840. msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
  841. VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
  842. }
  843. I915_WRITE(vgtif_reg(g2v_notify), msg);
  844. return 0;
  845. }
  846. static void gen8_free_scratch(struct i915_address_space *vm)
  847. {
  848. if (use_4lvl(vm))
  849. free_pdp(vm, vm->scratch_pdp);
  850. free_pd(vm, vm->scratch_pd);
  851. free_pt(vm, vm->scratch_pt);
  852. cleanup_scratch_page(vm);
  853. }
  854. static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
  855. struct i915_page_directory_pointer *pdp)
  856. {
  857. const unsigned int pdpes = i915_pdpes_per_pdp(vm);
  858. int i;
  859. for (i = 0; i < pdpes; i++) {
  860. if (pdp->page_directory[i] == vm->scratch_pd)
  861. continue;
  862. gen8_free_page_tables(vm, pdp->page_directory[i]);
  863. free_pd(vm, pdp->page_directory[i]);
  864. }
  865. free_pdp(vm, pdp);
  866. }
  867. static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
  868. {
  869. int i;
  870. for (i = 0; i < GEN8_PML4ES_PER_PML4; i++) {
  871. if (ppgtt->pml4.pdps[i] == ppgtt->base.scratch_pdp)
  872. continue;
  873. gen8_ppgtt_cleanup_3lvl(&ppgtt->base, ppgtt->pml4.pdps[i]);
  874. }
  875. cleanup_px(&ppgtt->base, &ppgtt->pml4);
  876. }
  877. static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
  878. {
  879. struct drm_i915_private *dev_priv = vm->i915;
  880. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  881. if (intel_vgpu_active(dev_priv))
  882. gen8_ppgtt_notify_vgt(ppgtt, false);
  883. if (use_4lvl(vm))
  884. gen8_ppgtt_cleanup_4lvl(ppgtt);
  885. else
  886. gen8_ppgtt_cleanup_3lvl(&ppgtt->base, &ppgtt->pdp);
  887. gen8_free_scratch(vm);
  888. }
  889. static int gen8_ppgtt_alloc_pd(struct i915_address_space *vm,
  890. struct i915_page_directory *pd,
  891. u64 start, u64 length)
  892. {
  893. struct i915_page_table *pt;
  894. u64 from = start;
  895. unsigned int pde;
  896. gen8_for_each_pde(pt, pd, start, length, pde) {
  897. if (pt == vm->scratch_pt) {
  898. pt = alloc_pt(vm);
  899. if (IS_ERR(pt))
  900. goto unwind;
  901. gen8_initialize_pt(vm, pt);
  902. gen8_ppgtt_set_pde(vm, pd, pt, pde);
  903. pd->used_pdes++;
  904. GEM_BUG_ON(pd->used_pdes > I915_PDES);
  905. }
  906. pt->used_ptes += gen8_pte_count(start, length);
  907. }
  908. return 0;
  909. unwind:
  910. gen8_ppgtt_clear_pd(vm, pd, from, start - from);
  911. return -ENOMEM;
  912. }
  913. static int gen8_ppgtt_alloc_pdp(struct i915_address_space *vm,
  914. struct i915_page_directory_pointer *pdp,
  915. u64 start, u64 length)
  916. {
  917. struct i915_page_directory *pd;
  918. u64 from = start;
  919. unsigned int pdpe;
  920. int ret;
  921. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  922. if (pd == vm->scratch_pd) {
  923. pd = alloc_pd(vm);
  924. if (IS_ERR(pd))
  925. goto unwind;
  926. gen8_initialize_pd(vm, pd);
  927. gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
  928. pdp->used_pdpes++;
  929. GEM_BUG_ON(pdp->used_pdpes > i915_pdpes_per_pdp(vm));
  930. mark_tlbs_dirty(i915_vm_to_ppgtt(vm));
  931. }
  932. ret = gen8_ppgtt_alloc_pd(vm, pd, start, length);
  933. if (unlikely(ret))
  934. goto unwind_pd;
  935. }
  936. return 0;
  937. unwind_pd:
  938. if (!pd->used_pdes) {
  939. gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
  940. GEM_BUG_ON(!pdp->used_pdpes);
  941. pdp->used_pdpes--;
  942. free_pd(vm, pd);
  943. }
  944. unwind:
  945. gen8_ppgtt_clear_pdp(vm, pdp, from, start - from);
  946. return -ENOMEM;
  947. }
  948. static int gen8_ppgtt_alloc_3lvl(struct i915_address_space *vm,
  949. u64 start, u64 length)
  950. {
  951. return gen8_ppgtt_alloc_pdp(vm,
  952. &i915_vm_to_ppgtt(vm)->pdp, start, length);
  953. }
  954. static int gen8_ppgtt_alloc_4lvl(struct i915_address_space *vm,
  955. u64 start, u64 length)
  956. {
  957. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  958. struct i915_pml4 *pml4 = &ppgtt->pml4;
  959. struct i915_page_directory_pointer *pdp;
  960. u64 from = start;
  961. u32 pml4e;
  962. int ret;
  963. gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
  964. if (pml4->pdps[pml4e] == vm->scratch_pdp) {
  965. pdp = alloc_pdp(vm);
  966. if (IS_ERR(pdp))
  967. goto unwind;
  968. gen8_initialize_pdp(vm, pdp);
  969. gen8_ppgtt_set_pml4e(pml4, pdp, pml4e);
  970. }
  971. ret = gen8_ppgtt_alloc_pdp(vm, pdp, start, length);
  972. if (unlikely(ret))
  973. goto unwind_pdp;
  974. }
  975. return 0;
  976. unwind_pdp:
  977. if (!pdp->used_pdpes) {
  978. gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
  979. free_pdp(vm, pdp);
  980. }
  981. unwind:
  982. gen8_ppgtt_clear_4lvl(vm, from, start - from);
  983. return -ENOMEM;
  984. }
  985. static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
  986. struct i915_page_directory_pointer *pdp,
  987. u64 start, u64 length,
  988. gen8_pte_t scratch_pte,
  989. struct seq_file *m)
  990. {
  991. struct i915_address_space *vm = &ppgtt->base;
  992. struct i915_page_directory *pd;
  993. u32 pdpe;
  994. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  995. struct i915_page_table *pt;
  996. u64 pd_len = length;
  997. u64 pd_start = start;
  998. u32 pde;
  999. if (pdp->page_directory[pdpe] == ppgtt->base.scratch_pd)
  1000. continue;
  1001. seq_printf(m, "\tPDPE #%d\n", pdpe);
  1002. gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
  1003. u32 pte;
  1004. gen8_pte_t *pt_vaddr;
  1005. if (pd->page_table[pde] == ppgtt->base.scratch_pt)
  1006. continue;
  1007. pt_vaddr = kmap_atomic_px(pt);
  1008. for (pte = 0; pte < GEN8_PTES; pte += 4) {
  1009. u64 va = (pdpe << GEN8_PDPE_SHIFT |
  1010. pde << GEN8_PDE_SHIFT |
  1011. pte << GEN8_PTE_SHIFT);
  1012. int i;
  1013. bool found = false;
  1014. for (i = 0; i < 4; i++)
  1015. if (pt_vaddr[pte + i] != scratch_pte)
  1016. found = true;
  1017. if (!found)
  1018. continue;
  1019. seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
  1020. for (i = 0; i < 4; i++) {
  1021. if (pt_vaddr[pte + i] != scratch_pte)
  1022. seq_printf(m, " %llx", pt_vaddr[pte + i]);
  1023. else
  1024. seq_puts(m, " SCRATCH ");
  1025. }
  1026. seq_puts(m, "\n");
  1027. }
  1028. kunmap_atomic(pt_vaddr);
  1029. }
  1030. }
  1031. }
  1032. static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
  1033. {
  1034. struct i915_address_space *vm = &ppgtt->base;
  1035. const gen8_pte_t scratch_pte =
  1036. gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
  1037. u64 start = 0, length = ppgtt->base.total;
  1038. if (use_4lvl(vm)) {
  1039. u64 pml4e;
  1040. struct i915_pml4 *pml4 = &ppgtt->pml4;
  1041. struct i915_page_directory_pointer *pdp;
  1042. gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
  1043. if (pml4->pdps[pml4e] == ppgtt->base.scratch_pdp)
  1044. continue;
  1045. seq_printf(m, " PML4E #%llu\n", pml4e);
  1046. gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
  1047. }
  1048. } else {
  1049. gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
  1050. }
  1051. }
  1052. static int gen8_preallocate_top_level_pdp(struct i915_hw_ppgtt *ppgtt)
  1053. {
  1054. struct i915_address_space *vm = &ppgtt->base;
  1055. struct i915_page_directory_pointer *pdp = &ppgtt->pdp;
  1056. struct i915_page_directory *pd;
  1057. u64 start = 0, length = ppgtt->base.total;
  1058. u64 from = start;
  1059. unsigned int pdpe;
  1060. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  1061. pd = alloc_pd(vm);
  1062. if (IS_ERR(pd))
  1063. goto unwind;
  1064. gen8_initialize_pd(vm, pd);
  1065. gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
  1066. pdp->used_pdpes++;
  1067. }
  1068. pdp->used_pdpes++; /* never remove */
  1069. return 0;
  1070. unwind:
  1071. start -= from;
  1072. gen8_for_each_pdpe(pd, pdp, from, start, pdpe) {
  1073. gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
  1074. free_pd(vm, pd);
  1075. }
  1076. pdp->used_pdpes = 0;
  1077. return -ENOMEM;
  1078. }
  1079. /*
  1080. * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
  1081. * with a net effect resembling a 2-level page table in normal x86 terms. Each
  1082. * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
  1083. * space.
  1084. *
  1085. */
  1086. static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
  1087. {
  1088. struct i915_address_space *vm = &ppgtt->base;
  1089. struct drm_i915_private *dev_priv = vm->i915;
  1090. int ret;
  1091. ppgtt->base.total = USES_FULL_48BIT_PPGTT(dev_priv) ?
  1092. 1ULL << 48 :
  1093. 1ULL << 32;
  1094. ret = gen8_init_scratch(&ppgtt->base);
  1095. if (ret) {
  1096. ppgtt->base.total = 0;
  1097. return ret;
  1098. }
  1099. /* There are only few exceptions for gen >=6. chv and bxt.
  1100. * And we are not sure about the latter so play safe for now.
  1101. */
  1102. if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
  1103. ppgtt->base.pt_kmap_wc = true;
  1104. if (use_4lvl(vm)) {
  1105. ret = setup_px(&ppgtt->base, &ppgtt->pml4);
  1106. if (ret)
  1107. goto free_scratch;
  1108. gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
  1109. ppgtt->switch_mm = gen8_mm_switch_4lvl;
  1110. ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_4lvl;
  1111. ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
  1112. ppgtt->base.clear_range = gen8_ppgtt_clear_4lvl;
  1113. } else {
  1114. ret = __pdp_init(&ppgtt->base, &ppgtt->pdp);
  1115. if (ret)
  1116. goto free_scratch;
  1117. if (intel_vgpu_active(dev_priv)) {
  1118. ret = gen8_preallocate_top_level_pdp(ppgtt);
  1119. if (ret) {
  1120. __pdp_fini(&ppgtt->pdp);
  1121. goto free_scratch;
  1122. }
  1123. }
  1124. ppgtt->switch_mm = gen8_mm_switch_3lvl;
  1125. ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_3lvl;
  1126. ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
  1127. ppgtt->base.clear_range = gen8_ppgtt_clear_3lvl;
  1128. }
  1129. if (intel_vgpu_active(dev_priv))
  1130. gen8_ppgtt_notify_vgt(ppgtt, true);
  1131. ppgtt->base.cleanup = gen8_ppgtt_cleanup;
  1132. ppgtt->base.unbind_vma = ppgtt_unbind_vma;
  1133. ppgtt->base.bind_vma = ppgtt_bind_vma;
  1134. ppgtt->debug_dump = gen8_dump_ppgtt;
  1135. return 0;
  1136. free_scratch:
  1137. gen8_free_scratch(&ppgtt->base);
  1138. return ret;
  1139. }
  1140. static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
  1141. {
  1142. struct i915_address_space *vm = &ppgtt->base;
  1143. struct i915_page_table *unused;
  1144. gen6_pte_t scratch_pte;
  1145. u32 pd_entry, pte, pde;
  1146. u32 start = 0, length = ppgtt->base.total;
  1147. scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
  1148. I915_CACHE_LLC, 0);
  1149. gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
  1150. u32 expected;
  1151. gen6_pte_t *pt_vaddr;
  1152. const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
  1153. pd_entry = readl(ppgtt->pd_addr + pde);
  1154. expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
  1155. if (pd_entry != expected)
  1156. seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
  1157. pde,
  1158. pd_entry,
  1159. expected);
  1160. seq_printf(m, "\tPDE: %x\n", pd_entry);
  1161. pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[pde]);
  1162. for (pte = 0; pte < GEN6_PTES; pte+=4) {
  1163. unsigned long va =
  1164. (pde * PAGE_SIZE * GEN6_PTES) +
  1165. (pte * PAGE_SIZE);
  1166. int i;
  1167. bool found = false;
  1168. for (i = 0; i < 4; i++)
  1169. if (pt_vaddr[pte + i] != scratch_pte)
  1170. found = true;
  1171. if (!found)
  1172. continue;
  1173. seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
  1174. for (i = 0; i < 4; i++) {
  1175. if (pt_vaddr[pte + i] != scratch_pte)
  1176. seq_printf(m, " %08x", pt_vaddr[pte + i]);
  1177. else
  1178. seq_puts(m, " SCRATCH ");
  1179. }
  1180. seq_puts(m, "\n");
  1181. }
  1182. kunmap_atomic(pt_vaddr);
  1183. }
  1184. }
  1185. /* Write pde (index) from the page directory @pd to the page table @pt */
  1186. static inline void gen6_write_pde(const struct i915_hw_ppgtt *ppgtt,
  1187. const unsigned int pde,
  1188. const struct i915_page_table *pt)
  1189. {
  1190. /* Caller needs to make sure the write completes if necessary */
  1191. writel_relaxed(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
  1192. ppgtt->pd_addr + pde);
  1193. }
  1194. /* Write all the page tables found in the ppgtt structure to incrementing page
  1195. * directories. */
  1196. static void gen6_write_page_range(struct i915_hw_ppgtt *ppgtt,
  1197. u32 start, u32 length)
  1198. {
  1199. struct i915_page_table *pt;
  1200. unsigned int pde;
  1201. gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde)
  1202. gen6_write_pde(ppgtt, pde, pt);
  1203. mark_tlbs_dirty(ppgtt);
  1204. wmb();
  1205. }
  1206. static inline u32 get_pd_offset(struct i915_hw_ppgtt *ppgtt)
  1207. {
  1208. GEM_BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
  1209. return ppgtt->pd.base.ggtt_offset << 10;
  1210. }
  1211. static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
  1212. struct drm_i915_gem_request *req)
  1213. {
  1214. struct intel_engine_cs *engine = req->engine;
  1215. u32 *cs;
  1216. /* NB: TLBs must be flushed and invalidated before a switch */
  1217. cs = intel_ring_begin(req, 6);
  1218. if (IS_ERR(cs))
  1219. return PTR_ERR(cs);
  1220. *cs++ = MI_LOAD_REGISTER_IMM(2);
  1221. *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
  1222. *cs++ = PP_DIR_DCLV_2G;
  1223. *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
  1224. *cs++ = get_pd_offset(ppgtt);
  1225. *cs++ = MI_NOOP;
  1226. intel_ring_advance(req, cs);
  1227. return 0;
  1228. }
  1229. static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
  1230. struct drm_i915_gem_request *req)
  1231. {
  1232. struct intel_engine_cs *engine = req->engine;
  1233. u32 *cs;
  1234. /* NB: TLBs must be flushed and invalidated before a switch */
  1235. cs = intel_ring_begin(req, 6);
  1236. if (IS_ERR(cs))
  1237. return PTR_ERR(cs);
  1238. *cs++ = MI_LOAD_REGISTER_IMM(2);
  1239. *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
  1240. *cs++ = PP_DIR_DCLV_2G;
  1241. *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
  1242. *cs++ = get_pd_offset(ppgtt);
  1243. *cs++ = MI_NOOP;
  1244. intel_ring_advance(req, cs);
  1245. return 0;
  1246. }
  1247. static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
  1248. struct drm_i915_gem_request *req)
  1249. {
  1250. struct intel_engine_cs *engine = req->engine;
  1251. struct drm_i915_private *dev_priv = req->i915;
  1252. I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
  1253. I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
  1254. return 0;
  1255. }
  1256. static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
  1257. {
  1258. struct intel_engine_cs *engine;
  1259. enum intel_engine_id id;
  1260. for_each_engine(engine, dev_priv, id) {
  1261. u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
  1262. GEN8_GFX_PPGTT_48B : 0;
  1263. I915_WRITE(RING_MODE_GEN7(engine),
  1264. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
  1265. }
  1266. }
  1267. static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
  1268. {
  1269. struct intel_engine_cs *engine;
  1270. u32 ecochk, ecobits;
  1271. enum intel_engine_id id;
  1272. ecobits = I915_READ(GAC_ECO_BITS);
  1273. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
  1274. ecochk = I915_READ(GAM_ECOCHK);
  1275. if (IS_HASWELL(dev_priv)) {
  1276. ecochk |= ECOCHK_PPGTT_WB_HSW;
  1277. } else {
  1278. ecochk |= ECOCHK_PPGTT_LLC_IVB;
  1279. ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
  1280. }
  1281. I915_WRITE(GAM_ECOCHK, ecochk);
  1282. for_each_engine(engine, dev_priv, id) {
  1283. /* GFX_MODE is per-ring on gen7+ */
  1284. I915_WRITE(RING_MODE_GEN7(engine),
  1285. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  1286. }
  1287. }
  1288. static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
  1289. {
  1290. u32 ecochk, gab_ctl, ecobits;
  1291. ecobits = I915_READ(GAC_ECO_BITS);
  1292. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
  1293. ECOBITS_PPGTT_CACHE64B);
  1294. gab_ctl = I915_READ(GAB_CTL);
  1295. I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
  1296. ecochk = I915_READ(GAM_ECOCHK);
  1297. I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
  1298. I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  1299. }
  1300. /* PPGTT support for Sandybdrige/Gen6 and later */
  1301. static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
  1302. u64 start, u64 length)
  1303. {
  1304. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1305. unsigned int first_entry = start >> PAGE_SHIFT;
  1306. unsigned int pde = first_entry / GEN6_PTES;
  1307. unsigned int pte = first_entry % GEN6_PTES;
  1308. unsigned int num_entries = length >> PAGE_SHIFT;
  1309. gen6_pte_t scratch_pte =
  1310. vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0);
  1311. while (num_entries) {
  1312. struct i915_page_table *pt = ppgtt->pd.page_table[pde++];
  1313. unsigned int end = min(pte + num_entries, GEN6_PTES);
  1314. gen6_pte_t *vaddr;
  1315. num_entries -= end - pte;
  1316. /* Note that the hw doesn't support removing PDE on the fly
  1317. * (they are cached inside the context with no means to
  1318. * invalidate the cache), so we can only reset the PTE
  1319. * entries back to scratch.
  1320. */
  1321. vaddr = kmap_atomic_px(pt);
  1322. do {
  1323. vaddr[pte++] = scratch_pte;
  1324. } while (pte < end);
  1325. kunmap_atomic(vaddr);
  1326. pte = 0;
  1327. }
  1328. }
  1329. static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
  1330. struct i915_vma *vma,
  1331. enum i915_cache_level cache_level,
  1332. u32 flags)
  1333. {
  1334. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1335. unsigned first_entry = vma->node.start >> PAGE_SHIFT;
  1336. unsigned act_pt = first_entry / GEN6_PTES;
  1337. unsigned act_pte = first_entry % GEN6_PTES;
  1338. const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
  1339. struct sgt_dma iter;
  1340. gen6_pte_t *vaddr;
  1341. vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
  1342. iter.sg = vma->pages->sgl;
  1343. iter.dma = sg_dma_address(iter.sg);
  1344. iter.max = iter.dma + iter.sg->length;
  1345. do {
  1346. vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
  1347. iter.dma += PAGE_SIZE;
  1348. if (iter.dma == iter.max) {
  1349. iter.sg = __sg_next(iter.sg);
  1350. if (!iter.sg)
  1351. break;
  1352. iter.dma = sg_dma_address(iter.sg);
  1353. iter.max = iter.dma + iter.sg->length;
  1354. }
  1355. if (++act_pte == GEN6_PTES) {
  1356. kunmap_atomic(vaddr);
  1357. vaddr = kmap_atomic_px(ppgtt->pd.page_table[++act_pt]);
  1358. act_pte = 0;
  1359. }
  1360. } while (1);
  1361. kunmap_atomic(vaddr);
  1362. }
  1363. static int gen6_alloc_va_range(struct i915_address_space *vm,
  1364. u64 start, u64 length)
  1365. {
  1366. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1367. struct i915_page_table *pt;
  1368. u64 from = start;
  1369. unsigned int pde;
  1370. bool flush = false;
  1371. gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
  1372. if (pt == vm->scratch_pt) {
  1373. pt = alloc_pt(vm);
  1374. if (IS_ERR(pt))
  1375. goto unwind_out;
  1376. gen6_initialize_pt(vm, pt);
  1377. ppgtt->pd.page_table[pde] = pt;
  1378. gen6_write_pde(ppgtt, pde, pt);
  1379. flush = true;
  1380. }
  1381. }
  1382. if (flush) {
  1383. mark_tlbs_dirty(ppgtt);
  1384. wmb();
  1385. }
  1386. return 0;
  1387. unwind_out:
  1388. gen6_ppgtt_clear_range(vm, from, start);
  1389. return -ENOMEM;
  1390. }
  1391. static int gen6_init_scratch(struct i915_address_space *vm)
  1392. {
  1393. int ret;
  1394. ret = setup_scratch_page(vm, I915_GFP_DMA);
  1395. if (ret)
  1396. return ret;
  1397. vm->scratch_pt = alloc_pt(vm);
  1398. if (IS_ERR(vm->scratch_pt)) {
  1399. cleanup_scratch_page(vm);
  1400. return PTR_ERR(vm->scratch_pt);
  1401. }
  1402. gen6_initialize_pt(vm, vm->scratch_pt);
  1403. return 0;
  1404. }
  1405. static void gen6_free_scratch(struct i915_address_space *vm)
  1406. {
  1407. free_pt(vm, vm->scratch_pt);
  1408. cleanup_scratch_page(vm);
  1409. }
  1410. static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
  1411. {
  1412. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1413. struct i915_page_directory *pd = &ppgtt->pd;
  1414. struct i915_page_table *pt;
  1415. u32 pde;
  1416. drm_mm_remove_node(&ppgtt->node);
  1417. gen6_for_all_pdes(pt, pd, pde)
  1418. if (pt != vm->scratch_pt)
  1419. free_pt(vm, pt);
  1420. gen6_free_scratch(vm);
  1421. }
  1422. static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
  1423. {
  1424. struct i915_address_space *vm = &ppgtt->base;
  1425. struct drm_i915_private *dev_priv = ppgtt->base.i915;
  1426. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1427. int ret;
  1428. /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
  1429. * allocator works in address space sizes, so it's multiplied by page
  1430. * size. We allocate at the top of the GTT to avoid fragmentation.
  1431. */
  1432. BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
  1433. ret = gen6_init_scratch(vm);
  1434. if (ret)
  1435. return ret;
  1436. ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
  1437. GEN6_PD_SIZE, GEN6_PD_ALIGN,
  1438. I915_COLOR_UNEVICTABLE,
  1439. 0, ggtt->base.total,
  1440. PIN_HIGH);
  1441. if (ret)
  1442. goto err_out;
  1443. if (ppgtt->node.start < ggtt->mappable_end)
  1444. DRM_DEBUG("Forced to use aperture for PDEs\n");
  1445. ppgtt->pd.base.ggtt_offset =
  1446. ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
  1447. ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
  1448. ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
  1449. return 0;
  1450. err_out:
  1451. gen6_free_scratch(vm);
  1452. return ret;
  1453. }
  1454. static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
  1455. {
  1456. return gen6_ppgtt_allocate_page_directories(ppgtt);
  1457. }
  1458. static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
  1459. u64 start, u64 length)
  1460. {
  1461. struct i915_page_table *unused;
  1462. u32 pde;
  1463. gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
  1464. ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
  1465. }
  1466. static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
  1467. {
  1468. struct drm_i915_private *dev_priv = ppgtt->base.i915;
  1469. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1470. int ret;
  1471. ppgtt->base.pte_encode = ggtt->base.pte_encode;
  1472. if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
  1473. ppgtt->switch_mm = gen6_mm_switch;
  1474. else if (IS_HASWELL(dev_priv))
  1475. ppgtt->switch_mm = hsw_mm_switch;
  1476. else if (IS_GEN7(dev_priv))
  1477. ppgtt->switch_mm = gen7_mm_switch;
  1478. else
  1479. BUG();
  1480. ret = gen6_ppgtt_alloc(ppgtt);
  1481. if (ret)
  1482. return ret;
  1483. ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
  1484. gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
  1485. gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
  1486. ret = gen6_alloc_va_range(&ppgtt->base, 0, ppgtt->base.total);
  1487. if (ret) {
  1488. gen6_ppgtt_cleanup(&ppgtt->base);
  1489. return ret;
  1490. }
  1491. ppgtt->base.clear_range = gen6_ppgtt_clear_range;
  1492. ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
  1493. ppgtt->base.unbind_vma = ppgtt_unbind_vma;
  1494. ppgtt->base.bind_vma = ppgtt_bind_vma;
  1495. ppgtt->base.cleanup = gen6_ppgtt_cleanup;
  1496. ppgtt->debug_dump = gen6_dump_ppgtt;
  1497. DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
  1498. ppgtt->node.size >> 20,
  1499. ppgtt->node.start / PAGE_SIZE);
  1500. DRM_DEBUG_DRIVER("Adding PPGTT at offset %x\n",
  1501. ppgtt->pd.base.ggtt_offset << 10);
  1502. return 0;
  1503. }
  1504. static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
  1505. struct drm_i915_private *dev_priv)
  1506. {
  1507. ppgtt->base.i915 = dev_priv;
  1508. ppgtt->base.dma = &dev_priv->drm.pdev->dev;
  1509. if (INTEL_INFO(dev_priv)->gen < 8)
  1510. return gen6_ppgtt_init(ppgtt);
  1511. else
  1512. return gen8_ppgtt_init(ppgtt);
  1513. }
  1514. static void i915_address_space_init(struct i915_address_space *vm,
  1515. struct drm_i915_private *dev_priv,
  1516. const char *name)
  1517. {
  1518. i915_gem_timeline_init(dev_priv, &vm->timeline, name);
  1519. drm_mm_init(&vm->mm, 0, vm->total);
  1520. vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
  1521. INIT_LIST_HEAD(&vm->active_list);
  1522. INIT_LIST_HEAD(&vm->inactive_list);
  1523. INIT_LIST_HEAD(&vm->unbound_list);
  1524. list_add_tail(&vm->global_link, &dev_priv->vm_list);
  1525. pagevec_init(&vm->free_pages, false);
  1526. }
  1527. static void i915_address_space_fini(struct i915_address_space *vm)
  1528. {
  1529. if (pagevec_count(&vm->free_pages))
  1530. vm_free_pages_release(vm);
  1531. i915_gem_timeline_fini(&vm->timeline);
  1532. drm_mm_takedown(&vm->mm);
  1533. list_del(&vm->global_link);
  1534. }
  1535. static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
  1536. {
  1537. /* This function is for gtt related workarounds. This function is
  1538. * called on driver load and after a GPU reset, so you can place
  1539. * workarounds here even if they get overwritten by GPU reset.
  1540. */
  1541. /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk,cfl */
  1542. if (IS_BROADWELL(dev_priv))
  1543. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
  1544. else if (IS_CHERRYVIEW(dev_priv))
  1545. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
  1546. else if (IS_GEN9_BC(dev_priv))
  1547. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
  1548. else if (IS_GEN9_LP(dev_priv))
  1549. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
  1550. }
  1551. int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
  1552. {
  1553. gtt_write_workarounds(dev_priv);
  1554. /* In the case of execlists, PPGTT is enabled by the context descriptor
  1555. * and the PDPs are contained within the context itself. We don't
  1556. * need to do anything here. */
  1557. if (i915.enable_execlists)
  1558. return 0;
  1559. if (!USES_PPGTT(dev_priv))
  1560. return 0;
  1561. if (IS_GEN6(dev_priv))
  1562. gen6_ppgtt_enable(dev_priv);
  1563. else if (IS_GEN7(dev_priv))
  1564. gen7_ppgtt_enable(dev_priv);
  1565. else if (INTEL_GEN(dev_priv) >= 8)
  1566. gen8_ppgtt_enable(dev_priv);
  1567. else
  1568. MISSING_CASE(INTEL_GEN(dev_priv));
  1569. return 0;
  1570. }
  1571. struct i915_hw_ppgtt *
  1572. i915_ppgtt_create(struct drm_i915_private *dev_priv,
  1573. struct drm_i915_file_private *fpriv,
  1574. const char *name)
  1575. {
  1576. struct i915_hw_ppgtt *ppgtt;
  1577. int ret;
  1578. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  1579. if (!ppgtt)
  1580. return ERR_PTR(-ENOMEM);
  1581. ret = __hw_ppgtt_init(ppgtt, dev_priv);
  1582. if (ret) {
  1583. kfree(ppgtt);
  1584. return ERR_PTR(ret);
  1585. }
  1586. kref_init(&ppgtt->ref);
  1587. i915_address_space_init(&ppgtt->base, dev_priv, name);
  1588. ppgtt->base.file = fpriv;
  1589. trace_i915_ppgtt_create(&ppgtt->base);
  1590. return ppgtt;
  1591. }
  1592. void i915_ppgtt_close(struct i915_address_space *vm)
  1593. {
  1594. struct list_head *phases[] = {
  1595. &vm->active_list,
  1596. &vm->inactive_list,
  1597. &vm->unbound_list,
  1598. NULL,
  1599. }, **phase;
  1600. GEM_BUG_ON(vm->closed);
  1601. vm->closed = true;
  1602. for (phase = phases; *phase; phase++) {
  1603. struct i915_vma *vma, *vn;
  1604. list_for_each_entry_safe(vma, vn, *phase, vm_link)
  1605. if (!i915_vma_is_closed(vma))
  1606. i915_vma_close(vma);
  1607. }
  1608. }
  1609. void i915_ppgtt_release(struct kref *kref)
  1610. {
  1611. struct i915_hw_ppgtt *ppgtt =
  1612. container_of(kref, struct i915_hw_ppgtt, ref);
  1613. trace_i915_ppgtt_release(&ppgtt->base);
  1614. /* vmas should already be unbound and destroyed */
  1615. WARN_ON(!list_empty(&ppgtt->base.active_list));
  1616. WARN_ON(!list_empty(&ppgtt->base.inactive_list));
  1617. WARN_ON(!list_empty(&ppgtt->base.unbound_list));
  1618. ppgtt->base.cleanup(&ppgtt->base);
  1619. i915_address_space_fini(&ppgtt->base);
  1620. kfree(ppgtt);
  1621. }
  1622. /* Certain Gen5 chipsets require require idling the GPU before
  1623. * unmapping anything from the GTT when VT-d is enabled.
  1624. */
  1625. static bool needs_idle_maps(struct drm_i915_private *dev_priv)
  1626. {
  1627. /* Query intel_iommu to see if we need the workaround. Presumably that
  1628. * was loaded first.
  1629. */
  1630. return IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_vtd_active();
  1631. }
  1632. void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
  1633. {
  1634. struct intel_engine_cs *engine;
  1635. enum intel_engine_id id;
  1636. if (INTEL_INFO(dev_priv)->gen < 6)
  1637. return;
  1638. for_each_engine(engine, dev_priv, id) {
  1639. u32 fault_reg;
  1640. fault_reg = I915_READ(RING_FAULT_REG(engine));
  1641. if (fault_reg & RING_FAULT_VALID) {
  1642. DRM_DEBUG_DRIVER("Unexpected fault\n"
  1643. "\tAddr: 0x%08lx\n"
  1644. "\tAddress space: %s\n"
  1645. "\tSource ID: %d\n"
  1646. "\tType: %d\n",
  1647. fault_reg & PAGE_MASK,
  1648. fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
  1649. RING_FAULT_SRCID(fault_reg),
  1650. RING_FAULT_FAULT_TYPE(fault_reg));
  1651. I915_WRITE(RING_FAULT_REG(engine),
  1652. fault_reg & ~RING_FAULT_VALID);
  1653. }
  1654. }
  1655. /* Engine specific init may not have been done till this point. */
  1656. if (dev_priv->engine[RCS])
  1657. POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
  1658. }
  1659. void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
  1660. {
  1661. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1662. /* Don't bother messing with faults pre GEN6 as we have little
  1663. * documentation supporting that it's a good idea.
  1664. */
  1665. if (INTEL_GEN(dev_priv) < 6)
  1666. return;
  1667. i915_check_and_clear_faults(dev_priv);
  1668. ggtt->base.clear_range(&ggtt->base, 0, ggtt->base.total);
  1669. i915_ggtt_invalidate(dev_priv);
  1670. }
  1671. int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
  1672. struct sg_table *pages)
  1673. {
  1674. do {
  1675. if (dma_map_sg(&obj->base.dev->pdev->dev,
  1676. pages->sgl, pages->nents,
  1677. PCI_DMA_BIDIRECTIONAL))
  1678. return 0;
  1679. /* If the DMA remap fails, one cause can be that we have
  1680. * too many objects pinned in a small remapping table,
  1681. * such as swiotlb. Incrementally purge all other objects and
  1682. * try again - if there are no more pages to remove from
  1683. * the DMA remapper, i915_gem_shrink will return 0.
  1684. */
  1685. GEM_BUG_ON(obj->mm.pages == pages);
  1686. } while (i915_gem_shrink(to_i915(obj->base.dev),
  1687. obj->base.size >> PAGE_SHIFT, NULL,
  1688. I915_SHRINK_BOUND |
  1689. I915_SHRINK_UNBOUND |
  1690. I915_SHRINK_ACTIVE));
  1691. return -ENOSPC;
  1692. }
  1693. static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
  1694. {
  1695. writeq(pte, addr);
  1696. }
  1697. static void gen8_ggtt_insert_page(struct i915_address_space *vm,
  1698. dma_addr_t addr,
  1699. u64 offset,
  1700. enum i915_cache_level level,
  1701. u32 unused)
  1702. {
  1703. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1704. gen8_pte_t __iomem *pte =
  1705. (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
  1706. gen8_set_pte(pte, gen8_pte_encode(addr, level));
  1707. ggtt->invalidate(vm->i915);
  1708. }
  1709. static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
  1710. struct i915_vma *vma,
  1711. enum i915_cache_level level,
  1712. u32 unused)
  1713. {
  1714. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1715. struct sgt_iter sgt_iter;
  1716. gen8_pte_t __iomem *gtt_entries;
  1717. const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
  1718. dma_addr_t addr;
  1719. gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
  1720. gtt_entries += vma->node.start >> PAGE_SHIFT;
  1721. for_each_sgt_dma(addr, sgt_iter, vma->pages)
  1722. gen8_set_pte(gtt_entries++, pte_encode | addr);
  1723. wmb();
  1724. /* This next bit makes the above posting read even more important. We
  1725. * want to flush the TLBs only after we're certain all the PTE updates
  1726. * have finished.
  1727. */
  1728. ggtt->invalidate(vm->i915);
  1729. }
  1730. static void gen6_ggtt_insert_page(struct i915_address_space *vm,
  1731. dma_addr_t addr,
  1732. u64 offset,
  1733. enum i915_cache_level level,
  1734. u32 flags)
  1735. {
  1736. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1737. gen6_pte_t __iomem *pte =
  1738. (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
  1739. iowrite32(vm->pte_encode(addr, level, flags), pte);
  1740. ggtt->invalidate(vm->i915);
  1741. }
  1742. /*
  1743. * Binds an object into the global gtt with the specified cache level. The object
  1744. * will be accessible to the GPU via commands whose operands reference offsets
  1745. * within the global GTT as well as accessible by the GPU through the GMADR
  1746. * mapped BAR (dev_priv->mm.gtt->gtt).
  1747. */
  1748. static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
  1749. struct i915_vma *vma,
  1750. enum i915_cache_level level,
  1751. u32 flags)
  1752. {
  1753. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1754. gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
  1755. unsigned int i = vma->node.start >> PAGE_SHIFT;
  1756. struct sgt_iter iter;
  1757. dma_addr_t addr;
  1758. for_each_sgt_dma(addr, iter, vma->pages)
  1759. iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
  1760. wmb();
  1761. /* This next bit makes the above posting read even more important. We
  1762. * want to flush the TLBs only after we're certain all the PTE updates
  1763. * have finished.
  1764. */
  1765. ggtt->invalidate(vm->i915);
  1766. }
  1767. static void nop_clear_range(struct i915_address_space *vm,
  1768. u64 start, u64 length)
  1769. {
  1770. }
  1771. static void gen8_ggtt_clear_range(struct i915_address_space *vm,
  1772. u64 start, u64 length)
  1773. {
  1774. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1775. unsigned first_entry = start >> PAGE_SHIFT;
  1776. unsigned num_entries = length >> PAGE_SHIFT;
  1777. const gen8_pte_t scratch_pte =
  1778. gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
  1779. gen8_pte_t __iomem *gtt_base =
  1780. (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
  1781. const int max_entries = ggtt_total_entries(ggtt) - first_entry;
  1782. int i;
  1783. if (WARN(num_entries > max_entries,
  1784. "First entry = %d; Num entries = %d (max=%d)\n",
  1785. first_entry, num_entries, max_entries))
  1786. num_entries = max_entries;
  1787. for (i = 0; i < num_entries; i++)
  1788. gen8_set_pte(&gtt_base[i], scratch_pte);
  1789. }
  1790. static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
  1791. {
  1792. struct drm_i915_private *dev_priv = vm->i915;
  1793. /*
  1794. * Make sure the internal GAM fifo has been cleared of all GTT
  1795. * writes before exiting stop_machine(). This guarantees that
  1796. * any aperture accesses waiting to start in another process
  1797. * cannot back up behind the GTT writes causing a hang.
  1798. * The register can be any arbitrary GAM register.
  1799. */
  1800. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1801. }
  1802. struct insert_page {
  1803. struct i915_address_space *vm;
  1804. dma_addr_t addr;
  1805. u64 offset;
  1806. enum i915_cache_level level;
  1807. };
  1808. static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
  1809. {
  1810. struct insert_page *arg = _arg;
  1811. gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset, arg->level, 0);
  1812. bxt_vtd_ggtt_wa(arg->vm);
  1813. return 0;
  1814. }
  1815. static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
  1816. dma_addr_t addr,
  1817. u64 offset,
  1818. enum i915_cache_level level,
  1819. u32 unused)
  1820. {
  1821. struct insert_page arg = { vm, addr, offset, level };
  1822. stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
  1823. }
  1824. struct insert_entries {
  1825. struct i915_address_space *vm;
  1826. struct i915_vma *vma;
  1827. enum i915_cache_level level;
  1828. };
  1829. static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
  1830. {
  1831. struct insert_entries *arg = _arg;
  1832. gen8_ggtt_insert_entries(arg->vm, arg->vma, arg->level, 0);
  1833. bxt_vtd_ggtt_wa(arg->vm);
  1834. return 0;
  1835. }
  1836. static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
  1837. struct i915_vma *vma,
  1838. enum i915_cache_level level,
  1839. u32 unused)
  1840. {
  1841. struct insert_entries arg = { vm, vma, level };
  1842. stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
  1843. }
  1844. struct clear_range {
  1845. struct i915_address_space *vm;
  1846. u64 start;
  1847. u64 length;
  1848. };
  1849. static int bxt_vtd_ggtt_clear_range__cb(void *_arg)
  1850. {
  1851. struct clear_range *arg = _arg;
  1852. gen8_ggtt_clear_range(arg->vm, arg->start, arg->length);
  1853. bxt_vtd_ggtt_wa(arg->vm);
  1854. return 0;
  1855. }
  1856. static void bxt_vtd_ggtt_clear_range__BKL(struct i915_address_space *vm,
  1857. u64 start,
  1858. u64 length)
  1859. {
  1860. struct clear_range arg = { vm, start, length };
  1861. stop_machine(bxt_vtd_ggtt_clear_range__cb, &arg, NULL);
  1862. }
  1863. static void gen6_ggtt_clear_range(struct i915_address_space *vm,
  1864. u64 start, u64 length)
  1865. {
  1866. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1867. unsigned first_entry = start >> PAGE_SHIFT;
  1868. unsigned num_entries = length >> PAGE_SHIFT;
  1869. gen6_pte_t scratch_pte, __iomem *gtt_base =
  1870. (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
  1871. const int max_entries = ggtt_total_entries(ggtt) - first_entry;
  1872. int i;
  1873. if (WARN(num_entries > max_entries,
  1874. "First entry = %d; Num entries = %d (max=%d)\n",
  1875. first_entry, num_entries, max_entries))
  1876. num_entries = max_entries;
  1877. scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
  1878. I915_CACHE_LLC, 0);
  1879. for (i = 0; i < num_entries; i++)
  1880. iowrite32(scratch_pte, &gtt_base[i]);
  1881. }
  1882. static void i915_ggtt_insert_page(struct i915_address_space *vm,
  1883. dma_addr_t addr,
  1884. u64 offset,
  1885. enum i915_cache_level cache_level,
  1886. u32 unused)
  1887. {
  1888. unsigned int flags = (cache_level == I915_CACHE_NONE) ?
  1889. AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
  1890. intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
  1891. }
  1892. static void i915_ggtt_insert_entries(struct i915_address_space *vm,
  1893. struct i915_vma *vma,
  1894. enum i915_cache_level cache_level,
  1895. u32 unused)
  1896. {
  1897. unsigned int flags = (cache_level == I915_CACHE_NONE) ?
  1898. AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
  1899. intel_gtt_insert_sg_entries(vma->pages, vma->node.start >> PAGE_SHIFT,
  1900. flags);
  1901. }
  1902. static void i915_ggtt_clear_range(struct i915_address_space *vm,
  1903. u64 start, u64 length)
  1904. {
  1905. intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
  1906. }
  1907. static int ggtt_bind_vma(struct i915_vma *vma,
  1908. enum i915_cache_level cache_level,
  1909. u32 flags)
  1910. {
  1911. struct drm_i915_private *i915 = vma->vm->i915;
  1912. struct drm_i915_gem_object *obj = vma->obj;
  1913. u32 pte_flags;
  1914. if (unlikely(!vma->pages)) {
  1915. int ret = i915_get_ggtt_vma_pages(vma);
  1916. if (ret)
  1917. return ret;
  1918. }
  1919. /* Currently applicable only to VLV */
  1920. pte_flags = 0;
  1921. if (obj->gt_ro)
  1922. pte_flags |= PTE_READ_ONLY;
  1923. intel_runtime_pm_get(i915);
  1924. vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
  1925. intel_runtime_pm_put(i915);
  1926. /*
  1927. * Without aliasing PPGTT there's no difference between
  1928. * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
  1929. * upgrade to both bound if we bind either to avoid double-binding.
  1930. */
  1931. vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
  1932. return 0;
  1933. }
  1934. static void ggtt_unbind_vma(struct i915_vma *vma)
  1935. {
  1936. struct drm_i915_private *i915 = vma->vm->i915;
  1937. intel_runtime_pm_get(i915);
  1938. vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
  1939. intel_runtime_pm_put(i915);
  1940. }
  1941. static int aliasing_gtt_bind_vma(struct i915_vma *vma,
  1942. enum i915_cache_level cache_level,
  1943. u32 flags)
  1944. {
  1945. struct drm_i915_private *i915 = vma->vm->i915;
  1946. u32 pte_flags;
  1947. int ret;
  1948. if (unlikely(!vma->pages)) {
  1949. ret = i915_get_ggtt_vma_pages(vma);
  1950. if (ret)
  1951. return ret;
  1952. }
  1953. /* Currently applicable only to VLV */
  1954. pte_flags = 0;
  1955. if (vma->obj->gt_ro)
  1956. pte_flags |= PTE_READ_ONLY;
  1957. if (flags & I915_VMA_LOCAL_BIND) {
  1958. struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
  1959. if (!(vma->flags & I915_VMA_LOCAL_BIND) &&
  1960. appgtt->base.allocate_va_range) {
  1961. ret = appgtt->base.allocate_va_range(&appgtt->base,
  1962. vma->node.start,
  1963. vma->size);
  1964. if (ret)
  1965. goto err_pages;
  1966. }
  1967. appgtt->base.insert_entries(&appgtt->base, vma, cache_level,
  1968. pte_flags);
  1969. }
  1970. if (flags & I915_VMA_GLOBAL_BIND) {
  1971. intel_runtime_pm_get(i915);
  1972. vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
  1973. intel_runtime_pm_put(i915);
  1974. }
  1975. return 0;
  1976. err_pages:
  1977. if (!(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND))) {
  1978. if (vma->pages != vma->obj->mm.pages) {
  1979. GEM_BUG_ON(!vma->pages);
  1980. sg_free_table(vma->pages);
  1981. kfree(vma->pages);
  1982. }
  1983. vma->pages = NULL;
  1984. }
  1985. return ret;
  1986. }
  1987. static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
  1988. {
  1989. struct drm_i915_private *i915 = vma->vm->i915;
  1990. if (vma->flags & I915_VMA_GLOBAL_BIND) {
  1991. intel_runtime_pm_get(i915);
  1992. vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
  1993. intel_runtime_pm_put(i915);
  1994. }
  1995. if (vma->flags & I915_VMA_LOCAL_BIND) {
  1996. struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
  1997. vm->clear_range(vm, vma->node.start, vma->size);
  1998. }
  1999. }
  2000. void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
  2001. struct sg_table *pages)
  2002. {
  2003. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2004. struct device *kdev = &dev_priv->drm.pdev->dev;
  2005. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2006. if (unlikely(ggtt->do_idle_maps)) {
  2007. if (i915_gem_wait_for_idle(dev_priv, 0)) {
  2008. DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
  2009. /* Wait a bit, in hopes it avoids the hang */
  2010. udelay(10);
  2011. }
  2012. }
  2013. dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
  2014. }
  2015. static void i915_gtt_color_adjust(const struct drm_mm_node *node,
  2016. unsigned long color,
  2017. u64 *start,
  2018. u64 *end)
  2019. {
  2020. if (node->allocated && node->color != color)
  2021. *start += I915_GTT_PAGE_SIZE;
  2022. /* Also leave a space between the unallocated reserved node after the
  2023. * GTT and any objects within the GTT, i.e. we use the color adjustment
  2024. * to insert a guard page to prevent prefetches crossing over the
  2025. * GTT boundary.
  2026. */
  2027. node = list_next_entry(node, node_list);
  2028. if (node->color != color)
  2029. *end -= I915_GTT_PAGE_SIZE;
  2030. }
  2031. int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
  2032. {
  2033. struct i915_ggtt *ggtt = &i915->ggtt;
  2034. struct i915_hw_ppgtt *ppgtt;
  2035. int err;
  2036. ppgtt = i915_ppgtt_create(i915, ERR_PTR(-EPERM), "[alias]");
  2037. if (IS_ERR(ppgtt))
  2038. return PTR_ERR(ppgtt);
  2039. if (WARN_ON(ppgtt->base.total < ggtt->base.total)) {
  2040. err = -ENODEV;
  2041. goto err_ppgtt;
  2042. }
  2043. if (ppgtt->base.allocate_va_range) {
  2044. /* Note we only pre-allocate as far as the end of the global
  2045. * GTT. On 48b / 4-level page-tables, the difference is very,
  2046. * very significant! We have to preallocate as GVT/vgpu does
  2047. * not like the page directory disappearing.
  2048. */
  2049. err = ppgtt->base.allocate_va_range(&ppgtt->base,
  2050. 0, ggtt->base.total);
  2051. if (err)
  2052. goto err_ppgtt;
  2053. }
  2054. i915->mm.aliasing_ppgtt = ppgtt;
  2055. WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
  2056. ggtt->base.bind_vma = aliasing_gtt_bind_vma;
  2057. WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
  2058. ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
  2059. return 0;
  2060. err_ppgtt:
  2061. i915_ppgtt_put(ppgtt);
  2062. return err;
  2063. }
  2064. void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
  2065. {
  2066. struct i915_ggtt *ggtt = &i915->ggtt;
  2067. struct i915_hw_ppgtt *ppgtt;
  2068. ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
  2069. if (!ppgtt)
  2070. return;
  2071. i915_ppgtt_put(ppgtt);
  2072. ggtt->base.bind_vma = ggtt_bind_vma;
  2073. ggtt->base.unbind_vma = ggtt_unbind_vma;
  2074. }
  2075. int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
  2076. {
  2077. /* Let GEM Manage all of the aperture.
  2078. *
  2079. * However, leave one page at the end still bound to the scratch page.
  2080. * There are a number of places where the hardware apparently prefetches
  2081. * past the end of the object, and we've seen multiple hangs with the
  2082. * GPU head pointer stuck in a batchbuffer bound at the last page of the
  2083. * aperture. One page should be enough to keep any prefetching inside
  2084. * of the aperture.
  2085. */
  2086. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2087. unsigned long hole_start, hole_end;
  2088. struct drm_mm_node *entry;
  2089. int ret;
  2090. ret = intel_vgt_balloon(dev_priv);
  2091. if (ret)
  2092. return ret;
  2093. /* Reserve a mappable slot for our lockless error capture */
  2094. ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
  2095. PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
  2096. 0, ggtt->mappable_end,
  2097. DRM_MM_INSERT_LOW);
  2098. if (ret)
  2099. return ret;
  2100. /* Clear any non-preallocated blocks */
  2101. drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
  2102. DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
  2103. hole_start, hole_end);
  2104. ggtt->base.clear_range(&ggtt->base, hole_start,
  2105. hole_end - hole_start);
  2106. }
  2107. /* And finally clear the reserved guard page */
  2108. ggtt->base.clear_range(&ggtt->base,
  2109. ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
  2110. if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
  2111. ret = i915_gem_init_aliasing_ppgtt(dev_priv);
  2112. if (ret)
  2113. goto err;
  2114. }
  2115. return 0;
  2116. err:
  2117. drm_mm_remove_node(&ggtt->error_capture);
  2118. return ret;
  2119. }
  2120. /**
  2121. * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
  2122. * @dev_priv: i915 device
  2123. */
  2124. void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
  2125. {
  2126. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2127. struct i915_vma *vma, *vn;
  2128. ggtt->base.closed = true;
  2129. mutex_lock(&dev_priv->drm.struct_mutex);
  2130. WARN_ON(!list_empty(&ggtt->base.active_list));
  2131. list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
  2132. WARN_ON(i915_vma_unbind(vma));
  2133. mutex_unlock(&dev_priv->drm.struct_mutex);
  2134. i915_gem_cleanup_stolen(&dev_priv->drm);
  2135. mutex_lock(&dev_priv->drm.struct_mutex);
  2136. i915_gem_fini_aliasing_ppgtt(dev_priv);
  2137. if (drm_mm_node_allocated(&ggtt->error_capture))
  2138. drm_mm_remove_node(&ggtt->error_capture);
  2139. if (drm_mm_initialized(&ggtt->base.mm)) {
  2140. intel_vgt_deballoon(dev_priv);
  2141. i915_address_space_fini(&ggtt->base);
  2142. }
  2143. ggtt->base.cleanup(&ggtt->base);
  2144. mutex_unlock(&dev_priv->drm.struct_mutex);
  2145. arch_phys_wc_del(ggtt->mtrr);
  2146. io_mapping_fini(&ggtt->mappable);
  2147. }
  2148. static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
  2149. {
  2150. snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
  2151. snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
  2152. return snb_gmch_ctl << 20;
  2153. }
  2154. static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
  2155. {
  2156. bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
  2157. bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
  2158. if (bdw_gmch_ctl)
  2159. bdw_gmch_ctl = 1 << bdw_gmch_ctl;
  2160. #ifdef CONFIG_X86_32
  2161. /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
  2162. if (bdw_gmch_ctl > 4)
  2163. bdw_gmch_ctl = 4;
  2164. #endif
  2165. return bdw_gmch_ctl << 20;
  2166. }
  2167. static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
  2168. {
  2169. gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
  2170. gmch_ctrl &= SNB_GMCH_GGMS_MASK;
  2171. if (gmch_ctrl)
  2172. return 1 << (20 + gmch_ctrl);
  2173. return 0;
  2174. }
  2175. static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
  2176. {
  2177. snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
  2178. snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
  2179. return (size_t)snb_gmch_ctl << 25; /* 32 MB units */
  2180. }
  2181. static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
  2182. {
  2183. bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  2184. bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
  2185. return (size_t)bdw_gmch_ctl << 25; /* 32 MB units */
  2186. }
  2187. static size_t chv_get_stolen_size(u16 gmch_ctrl)
  2188. {
  2189. gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
  2190. gmch_ctrl &= SNB_GMCH_GMS_MASK;
  2191. /*
  2192. * 0x0 to 0x10: 32MB increments starting at 0MB
  2193. * 0x11 to 0x16: 4MB increments starting at 8MB
  2194. * 0x17 to 0x1d: 4MB increments start at 36MB
  2195. */
  2196. if (gmch_ctrl < 0x11)
  2197. return (size_t)gmch_ctrl << 25;
  2198. else if (gmch_ctrl < 0x17)
  2199. return (size_t)(gmch_ctrl - 0x11 + 2) << 22;
  2200. else
  2201. return (size_t)(gmch_ctrl - 0x17 + 9) << 22;
  2202. }
  2203. static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
  2204. {
  2205. gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  2206. gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
  2207. if (gen9_gmch_ctl < 0xf0)
  2208. return (size_t)gen9_gmch_ctl << 25; /* 32 MB units */
  2209. else
  2210. /* 4MB increments starting at 0xf0 for 4MB */
  2211. return (size_t)(gen9_gmch_ctl - 0xf0 + 1) << 22;
  2212. }
  2213. static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
  2214. {
  2215. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2216. struct pci_dev *pdev = dev_priv->drm.pdev;
  2217. phys_addr_t phys_addr;
  2218. int ret;
  2219. /* For Modern GENs the PTEs and register space are split in the BAR */
  2220. phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
  2221. /*
  2222. * On BXT writes larger than 64 bit to the GTT pagetable range will be
  2223. * dropped. For WC mappings in general we have 64 byte burst writes
  2224. * when the WC buffer is flushed, so we can't use it, but have to
  2225. * resort to an uncached mapping. The WC issue is easily caught by the
  2226. * readback check when writing GTT PTE entries.
  2227. */
  2228. if (IS_GEN9_LP(dev_priv))
  2229. ggtt->gsm = ioremap_nocache(phys_addr, size);
  2230. else
  2231. ggtt->gsm = ioremap_wc(phys_addr, size);
  2232. if (!ggtt->gsm) {
  2233. DRM_ERROR("Failed to map the ggtt page table\n");
  2234. return -ENOMEM;
  2235. }
  2236. ret = setup_scratch_page(&ggtt->base, GFP_DMA32);
  2237. if (ret) {
  2238. DRM_ERROR("Scratch setup failed\n");
  2239. /* iounmap will also get called at remove, but meh */
  2240. iounmap(ggtt->gsm);
  2241. return ret;
  2242. }
  2243. return 0;
  2244. }
  2245. static void cnl_setup_private_ppat(struct drm_i915_private *dev_priv)
  2246. {
  2247. /* XXX: spec is unclear if this is still needed for CNL+ */
  2248. if (!USES_PPGTT(dev_priv)) {
  2249. I915_WRITE(GEN10_PAT_INDEX(0), GEN8_PPAT_UC);
  2250. return;
  2251. }
  2252. I915_WRITE(GEN10_PAT_INDEX(0), GEN8_PPAT_WB | GEN8_PPAT_LLC);
  2253. I915_WRITE(GEN10_PAT_INDEX(1), GEN8_PPAT_WC | GEN8_PPAT_LLCELLC);
  2254. I915_WRITE(GEN10_PAT_INDEX(2), GEN8_PPAT_WT | GEN8_PPAT_LLCELLC);
  2255. I915_WRITE(GEN10_PAT_INDEX(3), GEN8_PPAT_UC);
  2256. I915_WRITE(GEN10_PAT_INDEX(4), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0));
  2257. I915_WRITE(GEN10_PAT_INDEX(5), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1));
  2258. I915_WRITE(GEN10_PAT_INDEX(6), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2));
  2259. I915_WRITE(GEN10_PAT_INDEX(7), GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
  2260. }
  2261. /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
  2262. * bits. When using advanced contexts each context stores its own PAT, but
  2263. * writing this data shouldn't be harmful even in those cases. */
  2264. static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
  2265. {
  2266. u64 pat;
  2267. pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
  2268. GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
  2269. GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
  2270. GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
  2271. GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
  2272. GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
  2273. GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
  2274. GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
  2275. if (!USES_PPGTT(dev_priv))
  2276. /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
  2277. * so RTL will always use the value corresponding to
  2278. * pat_sel = 000".
  2279. * So let's disable cache for GGTT to avoid screen corruptions.
  2280. * MOCS still can be used though.
  2281. * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
  2282. * before this patch, i.e. the same uncached + snooping access
  2283. * like on gen6/7 seems to be in effect.
  2284. * - So this just fixes blitter/render access. Again it looks
  2285. * like it's not just uncached access, but uncached + snooping.
  2286. * So we can still hold onto all our assumptions wrt cpu
  2287. * clflushing on LLC machines.
  2288. */
  2289. pat = GEN8_PPAT(0, GEN8_PPAT_UC);
  2290. /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
  2291. * write would work. */
  2292. I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
  2293. I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
  2294. }
  2295. static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
  2296. {
  2297. u64 pat;
  2298. /*
  2299. * Map WB on BDW to snooped on CHV.
  2300. *
  2301. * Only the snoop bit has meaning for CHV, the rest is
  2302. * ignored.
  2303. *
  2304. * The hardware will never snoop for certain types of accesses:
  2305. * - CPU GTT (GMADR->GGTT->no snoop->memory)
  2306. * - PPGTT page tables
  2307. * - some other special cycles
  2308. *
  2309. * As with BDW, we also need to consider the following for GT accesses:
  2310. * "For GGTT, there is NO pat_sel[2:0] from the entry,
  2311. * so RTL will always use the value corresponding to
  2312. * pat_sel = 000".
  2313. * Which means we must set the snoop bit in PAT entry 0
  2314. * in order to keep the global status page working.
  2315. */
  2316. pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
  2317. GEN8_PPAT(1, 0) |
  2318. GEN8_PPAT(2, 0) |
  2319. GEN8_PPAT(3, 0) |
  2320. GEN8_PPAT(4, CHV_PPAT_SNOOP) |
  2321. GEN8_PPAT(5, CHV_PPAT_SNOOP) |
  2322. GEN8_PPAT(6, CHV_PPAT_SNOOP) |
  2323. GEN8_PPAT(7, CHV_PPAT_SNOOP);
  2324. I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
  2325. I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
  2326. }
  2327. static void gen6_gmch_remove(struct i915_address_space *vm)
  2328. {
  2329. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  2330. iounmap(ggtt->gsm);
  2331. cleanup_scratch_page(vm);
  2332. }
  2333. static int gen8_gmch_probe(struct i915_ggtt *ggtt)
  2334. {
  2335. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2336. struct pci_dev *pdev = dev_priv->drm.pdev;
  2337. unsigned int size;
  2338. u16 snb_gmch_ctl;
  2339. int err;
  2340. /* TODO: We're not aware of mappable constraints on gen8 yet */
  2341. ggtt->mappable_base = pci_resource_start(pdev, 2);
  2342. ggtt->mappable_end = pci_resource_len(pdev, 2);
  2343. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(39));
  2344. if (!err)
  2345. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
  2346. if (err)
  2347. DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
  2348. pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  2349. if (INTEL_GEN(dev_priv) >= 9) {
  2350. ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
  2351. size = gen8_get_total_gtt_size(snb_gmch_ctl);
  2352. } else if (IS_CHERRYVIEW(dev_priv)) {
  2353. ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
  2354. size = chv_get_total_gtt_size(snb_gmch_ctl);
  2355. } else {
  2356. ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
  2357. size = gen8_get_total_gtt_size(snb_gmch_ctl);
  2358. }
  2359. ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
  2360. if (INTEL_GEN(dev_priv) >= 10)
  2361. cnl_setup_private_ppat(dev_priv);
  2362. else if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
  2363. chv_setup_private_ppat(dev_priv);
  2364. else
  2365. bdw_setup_private_ppat(dev_priv);
  2366. ggtt->base.cleanup = gen6_gmch_remove;
  2367. ggtt->base.bind_vma = ggtt_bind_vma;
  2368. ggtt->base.unbind_vma = ggtt_unbind_vma;
  2369. ggtt->base.insert_page = gen8_ggtt_insert_page;
  2370. ggtt->base.clear_range = nop_clear_range;
  2371. if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
  2372. ggtt->base.clear_range = gen8_ggtt_clear_range;
  2373. ggtt->base.insert_entries = gen8_ggtt_insert_entries;
  2374. /* Serialize GTT updates with aperture access on BXT if VT-d is on. */
  2375. if (intel_ggtt_update_needs_vtd_wa(dev_priv)) {
  2376. ggtt->base.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
  2377. ggtt->base.insert_page = bxt_vtd_ggtt_insert_page__BKL;
  2378. if (ggtt->base.clear_range != nop_clear_range)
  2379. ggtt->base.clear_range = bxt_vtd_ggtt_clear_range__BKL;
  2380. }
  2381. ggtt->invalidate = gen6_ggtt_invalidate;
  2382. return ggtt_probe_common(ggtt, size);
  2383. }
  2384. static int gen6_gmch_probe(struct i915_ggtt *ggtt)
  2385. {
  2386. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2387. struct pci_dev *pdev = dev_priv->drm.pdev;
  2388. unsigned int size;
  2389. u16 snb_gmch_ctl;
  2390. int err;
  2391. ggtt->mappable_base = pci_resource_start(pdev, 2);
  2392. ggtt->mappable_end = pci_resource_len(pdev, 2);
  2393. /* 64/512MB is the current min/max we actually know of, but this is just
  2394. * a coarse sanity check.
  2395. */
  2396. if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
  2397. DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
  2398. return -ENXIO;
  2399. }
  2400. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
  2401. if (!err)
  2402. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
  2403. if (err)
  2404. DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
  2405. pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  2406. ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
  2407. size = gen6_get_total_gtt_size(snb_gmch_ctl);
  2408. ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
  2409. ggtt->base.clear_range = gen6_ggtt_clear_range;
  2410. ggtt->base.insert_page = gen6_ggtt_insert_page;
  2411. ggtt->base.insert_entries = gen6_ggtt_insert_entries;
  2412. ggtt->base.bind_vma = ggtt_bind_vma;
  2413. ggtt->base.unbind_vma = ggtt_unbind_vma;
  2414. ggtt->base.cleanup = gen6_gmch_remove;
  2415. ggtt->invalidate = gen6_ggtt_invalidate;
  2416. if (HAS_EDRAM(dev_priv))
  2417. ggtt->base.pte_encode = iris_pte_encode;
  2418. else if (IS_HASWELL(dev_priv))
  2419. ggtt->base.pte_encode = hsw_pte_encode;
  2420. else if (IS_VALLEYVIEW(dev_priv))
  2421. ggtt->base.pte_encode = byt_pte_encode;
  2422. else if (INTEL_GEN(dev_priv) >= 7)
  2423. ggtt->base.pte_encode = ivb_pte_encode;
  2424. else
  2425. ggtt->base.pte_encode = snb_pte_encode;
  2426. return ggtt_probe_common(ggtt, size);
  2427. }
  2428. static void i915_gmch_remove(struct i915_address_space *vm)
  2429. {
  2430. intel_gmch_remove();
  2431. }
  2432. static int i915_gmch_probe(struct i915_ggtt *ggtt)
  2433. {
  2434. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2435. int ret;
  2436. ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
  2437. if (!ret) {
  2438. DRM_ERROR("failed to set up gmch\n");
  2439. return -EIO;
  2440. }
  2441. intel_gtt_get(&ggtt->base.total,
  2442. &ggtt->stolen_size,
  2443. &ggtt->mappable_base,
  2444. &ggtt->mappable_end);
  2445. ggtt->do_idle_maps = needs_idle_maps(dev_priv);
  2446. ggtt->base.insert_page = i915_ggtt_insert_page;
  2447. ggtt->base.insert_entries = i915_ggtt_insert_entries;
  2448. ggtt->base.clear_range = i915_ggtt_clear_range;
  2449. ggtt->base.bind_vma = ggtt_bind_vma;
  2450. ggtt->base.unbind_vma = ggtt_unbind_vma;
  2451. ggtt->base.cleanup = i915_gmch_remove;
  2452. ggtt->invalidate = gmch_ggtt_invalidate;
  2453. if (unlikely(ggtt->do_idle_maps))
  2454. DRM_INFO("applying Ironlake quirks for intel_iommu\n");
  2455. return 0;
  2456. }
  2457. /**
  2458. * i915_ggtt_probe_hw - Probe GGTT hardware location
  2459. * @dev_priv: i915 device
  2460. */
  2461. int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
  2462. {
  2463. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2464. int ret;
  2465. ggtt->base.i915 = dev_priv;
  2466. ggtt->base.dma = &dev_priv->drm.pdev->dev;
  2467. if (INTEL_GEN(dev_priv) <= 5)
  2468. ret = i915_gmch_probe(ggtt);
  2469. else if (INTEL_GEN(dev_priv) < 8)
  2470. ret = gen6_gmch_probe(ggtt);
  2471. else
  2472. ret = gen8_gmch_probe(ggtt);
  2473. if (ret)
  2474. return ret;
  2475. /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
  2476. * This is easier than doing range restriction on the fly, as we
  2477. * currently don't have any bits spare to pass in this upper
  2478. * restriction!
  2479. */
  2480. if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
  2481. ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
  2482. ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
  2483. }
  2484. if ((ggtt->base.total - 1) >> 32) {
  2485. DRM_ERROR("We never expected a Global GTT with more than 32bits"
  2486. " of address space! Found %lldM!\n",
  2487. ggtt->base.total >> 20);
  2488. ggtt->base.total = 1ULL << 32;
  2489. ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
  2490. }
  2491. if (ggtt->mappable_end > ggtt->base.total) {
  2492. DRM_ERROR("mappable aperture extends past end of GGTT,"
  2493. " aperture=%llx, total=%llx\n",
  2494. ggtt->mappable_end, ggtt->base.total);
  2495. ggtt->mappable_end = ggtt->base.total;
  2496. }
  2497. /* GMADR is the PCI mmio aperture into the global GTT. */
  2498. DRM_INFO("Memory usable by graphics device = %lluM\n",
  2499. ggtt->base.total >> 20);
  2500. DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
  2501. DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
  2502. if (intel_vtd_active())
  2503. DRM_INFO("VT-d active for gfx access\n");
  2504. return 0;
  2505. }
  2506. /**
  2507. * i915_ggtt_init_hw - Initialize GGTT hardware
  2508. * @dev_priv: i915 device
  2509. */
  2510. int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
  2511. {
  2512. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2513. int ret;
  2514. INIT_LIST_HEAD(&dev_priv->vm_list);
  2515. /* Note that we use page colouring to enforce a guard page at the
  2516. * end of the address space. This is required as the CS may prefetch
  2517. * beyond the end of the batch buffer, across the page boundary,
  2518. * and beyond the end of the GTT if we do not provide a guard.
  2519. */
  2520. mutex_lock(&dev_priv->drm.struct_mutex);
  2521. i915_address_space_init(&ggtt->base, dev_priv, "[global]");
  2522. if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
  2523. ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
  2524. mutex_unlock(&dev_priv->drm.struct_mutex);
  2525. if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
  2526. dev_priv->ggtt.mappable_base,
  2527. dev_priv->ggtt.mappable_end)) {
  2528. ret = -EIO;
  2529. goto out_gtt_cleanup;
  2530. }
  2531. ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
  2532. /*
  2533. * Initialise stolen early so that we may reserve preallocated
  2534. * objects for the BIOS to KMS transition.
  2535. */
  2536. ret = i915_gem_init_stolen(dev_priv);
  2537. if (ret)
  2538. goto out_gtt_cleanup;
  2539. return 0;
  2540. out_gtt_cleanup:
  2541. ggtt->base.cleanup(&ggtt->base);
  2542. return ret;
  2543. }
  2544. int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
  2545. {
  2546. if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
  2547. return -EIO;
  2548. return 0;
  2549. }
  2550. void i915_ggtt_enable_guc(struct drm_i915_private *i915)
  2551. {
  2552. GEM_BUG_ON(i915->ggtt.invalidate != gen6_ggtt_invalidate);
  2553. i915->ggtt.invalidate = guc_ggtt_invalidate;
  2554. }
  2555. void i915_ggtt_disable_guc(struct drm_i915_private *i915)
  2556. {
  2557. /* We should only be called after i915_ggtt_enable_guc() */
  2558. GEM_BUG_ON(i915->ggtt.invalidate != guc_ggtt_invalidate);
  2559. i915->ggtt.invalidate = gen6_ggtt_invalidate;
  2560. }
  2561. void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
  2562. {
  2563. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2564. struct drm_i915_gem_object *obj, *on;
  2565. i915_check_and_clear_faults(dev_priv);
  2566. /* First fill our portion of the GTT with scratch pages */
  2567. ggtt->base.clear_range(&ggtt->base, 0, ggtt->base.total);
  2568. ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
  2569. /* clflush objects bound into the GGTT and rebind them. */
  2570. list_for_each_entry_safe(obj, on,
  2571. &dev_priv->mm.bound_list, global_link) {
  2572. bool ggtt_bound = false;
  2573. struct i915_vma *vma;
  2574. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2575. if (vma->vm != &ggtt->base)
  2576. continue;
  2577. if (!i915_vma_unbind(vma))
  2578. continue;
  2579. WARN_ON(i915_vma_bind(vma, obj->cache_level,
  2580. PIN_UPDATE));
  2581. ggtt_bound = true;
  2582. }
  2583. if (ggtt_bound)
  2584. WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
  2585. }
  2586. ggtt->base.closed = false;
  2587. if (INTEL_GEN(dev_priv) >= 8) {
  2588. if (INTEL_GEN(dev_priv) >= 10)
  2589. cnl_setup_private_ppat(dev_priv);
  2590. else if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
  2591. chv_setup_private_ppat(dev_priv);
  2592. else
  2593. bdw_setup_private_ppat(dev_priv);
  2594. return;
  2595. }
  2596. if (USES_PPGTT(dev_priv)) {
  2597. struct i915_address_space *vm;
  2598. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  2599. struct i915_hw_ppgtt *ppgtt;
  2600. if (i915_is_ggtt(vm))
  2601. ppgtt = dev_priv->mm.aliasing_ppgtt;
  2602. else
  2603. ppgtt = i915_vm_to_ppgtt(vm);
  2604. gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
  2605. }
  2606. }
  2607. i915_ggtt_invalidate(dev_priv);
  2608. }
  2609. static struct scatterlist *
  2610. rotate_pages(const dma_addr_t *in, unsigned int offset,
  2611. unsigned int width, unsigned int height,
  2612. unsigned int stride,
  2613. struct sg_table *st, struct scatterlist *sg)
  2614. {
  2615. unsigned int column, row;
  2616. unsigned int src_idx;
  2617. for (column = 0; column < width; column++) {
  2618. src_idx = stride * (height - 1) + column;
  2619. for (row = 0; row < height; row++) {
  2620. st->nents++;
  2621. /* We don't need the pages, but need to initialize
  2622. * the entries so the sg list can be happily traversed.
  2623. * The only thing we need are DMA addresses.
  2624. */
  2625. sg_set_page(sg, NULL, PAGE_SIZE, 0);
  2626. sg_dma_address(sg) = in[offset + src_idx];
  2627. sg_dma_len(sg) = PAGE_SIZE;
  2628. sg = sg_next(sg);
  2629. src_idx -= stride;
  2630. }
  2631. }
  2632. return sg;
  2633. }
  2634. static noinline struct sg_table *
  2635. intel_rotate_pages(struct intel_rotation_info *rot_info,
  2636. struct drm_i915_gem_object *obj)
  2637. {
  2638. const unsigned long n_pages = obj->base.size / PAGE_SIZE;
  2639. unsigned int size = intel_rotation_info_size(rot_info);
  2640. struct sgt_iter sgt_iter;
  2641. dma_addr_t dma_addr;
  2642. unsigned long i;
  2643. dma_addr_t *page_addr_list;
  2644. struct sg_table *st;
  2645. struct scatterlist *sg;
  2646. int ret = -ENOMEM;
  2647. /* Allocate a temporary list of source pages for random access. */
  2648. page_addr_list = kvmalloc_array(n_pages,
  2649. sizeof(dma_addr_t),
  2650. GFP_KERNEL);
  2651. if (!page_addr_list)
  2652. return ERR_PTR(ret);
  2653. /* Allocate target SG list. */
  2654. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2655. if (!st)
  2656. goto err_st_alloc;
  2657. ret = sg_alloc_table(st, size, GFP_KERNEL);
  2658. if (ret)
  2659. goto err_sg_alloc;
  2660. /* Populate source page list from the object. */
  2661. i = 0;
  2662. for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
  2663. page_addr_list[i++] = dma_addr;
  2664. GEM_BUG_ON(i != n_pages);
  2665. st->nents = 0;
  2666. sg = st->sgl;
  2667. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
  2668. sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
  2669. rot_info->plane[i].width, rot_info->plane[i].height,
  2670. rot_info->plane[i].stride, st, sg);
  2671. }
  2672. DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
  2673. obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
  2674. kvfree(page_addr_list);
  2675. return st;
  2676. err_sg_alloc:
  2677. kfree(st);
  2678. err_st_alloc:
  2679. kvfree(page_addr_list);
  2680. DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
  2681. obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
  2682. return ERR_PTR(ret);
  2683. }
  2684. static noinline struct sg_table *
  2685. intel_partial_pages(const struct i915_ggtt_view *view,
  2686. struct drm_i915_gem_object *obj)
  2687. {
  2688. struct sg_table *st;
  2689. struct scatterlist *sg, *iter;
  2690. unsigned int count = view->partial.size;
  2691. unsigned int offset;
  2692. int ret = -ENOMEM;
  2693. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2694. if (!st)
  2695. goto err_st_alloc;
  2696. ret = sg_alloc_table(st, count, GFP_KERNEL);
  2697. if (ret)
  2698. goto err_sg_alloc;
  2699. iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
  2700. GEM_BUG_ON(!iter);
  2701. sg = st->sgl;
  2702. st->nents = 0;
  2703. do {
  2704. unsigned int len;
  2705. len = min(iter->length - (offset << PAGE_SHIFT),
  2706. count << PAGE_SHIFT);
  2707. sg_set_page(sg, NULL, len, 0);
  2708. sg_dma_address(sg) =
  2709. sg_dma_address(iter) + (offset << PAGE_SHIFT);
  2710. sg_dma_len(sg) = len;
  2711. st->nents++;
  2712. count -= len >> PAGE_SHIFT;
  2713. if (count == 0) {
  2714. sg_mark_end(sg);
  2715. return st;
  2716. }
  2717. sg = __sg_next(sg);
  2718. iter = __sg_next(iter);
  2719. offset = 0;
  2720. } while (1);
  2721. err_sg_alloc:
  2722. kfree(st);
  2723. err_st_alloc:
  2724. return ERR_PTR(ret);
  2725. }
  2726. static int
  2727. i915_get_ggtt_vma_pages(struct i915_vma *vma)
  2728. {
  2729. int ret;
  2730. /* The vma->pages are only valid within the lifespan of the borrowed
  2731. * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
  2732. * must be the vma->pages. A simple rule is that vma->pages must only
  2733. * be accessed when the obj->mm.pages are pinned.
  2734. */
  2735. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
  2736. switch (vma->ggtt_view.type) {
  2737. case I915_GGTT_VIEW_NORMAL:
  2738. vma->pages = vma->obj->mm.pages;
  2739. return 0;
  2740. case I915_GGTT_VIEW_ROTATED:
  2741. vma->pages =
  2742. intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
  2743. break;
  2744. case I915_GGTT_VIEW_PARTIAL:
  2745. vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
  2746. break;
  2747. default:
  2748. WARN_ONCE(1, "GGTT view %u not implemented!\n",
  2749. vma->ggtt_view.type);
  2750. return -EINVAL;
  2751. }
  2752. ret = 0;
  2753. if (unlikely(IS_ERR(vma->pages))) {
  2754. ret = PTR_ERR(vma->pages);
  2755. vma->pages = NULL;
  2756. DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
  2757. vma->ggtt_view.type, ret);
  2758. }
  2759. return ret;
  2760. }
  2761. /**
  2762. * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
  2763. * @vm: the &struct i915_address_space
  2764. * @node: the &struct drm_mm_node (typically i915_vma.mode)
  2765. * @size: how much space to allocate inside the GTT,
  2766. * must be #I915_GTT_PAGE_SIZE aligned
  2767. * @offset: where to insert inside the GTT,
  2768. * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
  2769. * (@offset + @size) must fit within the address space
  2770. * @color: color to apply to node, if this node is not from a VMA,
  2771. * color must be #I915_COLOR_UNEVICTABLE
  2772. * @flags: control search and eviction behaviour
  2773. *
  2774. * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
  2775. * the address space (using @size and @color). If the @node does not fit, it
  2776. * tries to evict any overlapping nodes from the GTT, including any
  2777. * neighbouring nodes if the colors do not match (to ensure guard pages between
  2778. * differing domains). See i915_gem_evict_for_node() for the gory details
  2779. * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
  2780. * evicting active overlapping objects, and any overlapping node that is pinned
  2781. * or marked as unevictable will also result in failure.
  2782. *
  2783. * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
  2784. * asked to wait for eviction and interrupted.
  2785. */
  2786. int i915_gem_gtt_reserve(struct i915_address_space *vm,
  2787. struct drm_mm_node *node,
  2788. u64 size, u64 offset, unsigned long color,
  2789. unsigned int flags)
  2790. {
  2791. int err;
  2792. GEM_BUG_ON(!size);
  2793. GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
  2794. GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
  2795. GEM_BUG_ON(range_overflows(offset, size, vm->total));
  2796. GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
  2797. GEM_BUG_ON(drm_mm_node_allocated(node));
  2798. node->size = size;
  2799. node->start = offset;
  2800. node->color = color;
  2801. err = drm_mm_reserve_node(&vm->mm, node);
  2802. if (err != -ENOSPC)
  2803. return err;
  2804. if (flags & PIN_NOEVICT)
  2805. return -ENOSPC;
  2806. err = i915_gem_evict_for_node(vm, node, flags);
  2807. if (err == 0)
  2808. err = drm_mm_reserve_node(&vm->mm, node);
  2809. return err;
  2810. }
  2811. static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
  2812. {
  2813. u64 range, addr;
  2814. GEM_BUG_ON(range_overflows(start, len, end));
  2815. GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
  2816. range = round_down(end - len, align) - round_up(start, align);
  2817. if (range) {
  2818. if (sizeof(unsigned long) == sizeof(u64)) {
  2819. addr = get_random_long();
  2820. } else {
  2821. addr = get_random_int();
  2822. if (range > U32_MAX) {
  2823. addr <<= 32;
  2824. addr |= get_random_int();
  2825. }
  2826. }
  2827. div64_u64_rem(addr, range, &addr);
  2828. start += addr;
  2829. }
  2830. return round_up(start, align);
  2831. }
  2832. /**
  2833. * i915_gem_gtt_insert - insert a node into an address_space (GTT)
  2834. * @vm: the &struct i915_address_space
  2835. * @node: the &struct drm_mm_node (typically i915_vma.node)
  2836. * @size: how much space to allocate inside the GTT,
  2837. * must be #I915_GTT_PAGE_SIZE aligned
  2838. * @alignment: required alignment of starting offset, may be 0 but
  2839. * if specified, this must be a power-of-two and at least
  2840. * #I915_GTT_MIN_ALIGNMENT
  2841. * @color: color to apply to node
  2842. * @start: start of any range restriction inside GTT (0 for all),
  2843. * must be #I915_GTT_PAGE_SIZE aligned
  2844. * @end: end of any range restriction inside GTT (U64_MAX for all),
  2845. * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
  2846. * @flags: control search and eviction behaviour
  2847. *
  2848. * i915_gem_gtt_insert() first searches for an available hole into which
  2849. * is can insert the node. The hole address is aligned to @alignment and
  2850. * its @size must then fit entirely within the [@start, @end] bounds. The
  2851. * nodes on either side of the hole must match @color, or else a guard page
  2852. * will be inserted between the two nodes (or the node evicted). If no
  2853. * suitable hole is found, first a victim is randomly selected and tested
  2854. * for eviction, otherwise then the LRU list of objects within the GTT
  2855. * is scanned to find the first set of replacement nodes to create the hole.
  2856. * Those old overlapping nodes are evicted from the GTT (and so must be
  2857. * rebound before any future use). Any node that is currently pinned cannot
  2858. * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
  2859. * active and #PIN_NONBLOCK is specified, that node is also skipped when
  2860. * searching for an eviction candidate. See i915_gem_evict_something() for
  2861. * the gory details on the eviction algorithm.
  2862. *
  2863. * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
  2864. * asked to wait for eviction and interrupted.
  2865. */
  2866. int i915_gem_gtt_insert(struct i915_address_space *vm,
  2867. struct drm_mm_node *node,
  2868. u64 size, u64 alignment, unsigned long color,
  2869. u64 start, u64 end, unsigned int flags)
  2870. {
  2871. enum drm_mm_insert_mode mode;
  2872. u64 offset;
  2873. int err;
  2874. lockdep_assert_held(&vm->i915->drm.struct_mutex);
  2875. GEM_BUG_ON(!size);
  2876. GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
  2877. GEM_BUG_ON(alignment && !is_power_of_2(alignment));
  2878. GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
  2879. GEM_BUG_ON(start >= end);
  2880. GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
  2881. GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
  2882. GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
  2883. GEM_BUG_ON(drm_mm_node_allocated(node));
  2884. if (unlikely(range_overflows(start, size, end)))
  2885. return -ENOSPC;
  2886. if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
  2887. return -ENOSPC;
  2888. mode = DRM_MM_INSERT_BEST;
  2889. if (flags & PIN_HIGH)
  2890. mode = DRM_MM_INSERT_HIGH;
  2891. if (flags & PIN_MAPPABLE)
  2892. mode = DRM_MM_INSERT_LOW;
  2893. /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
  2894. * so we know that we always have a minimum alignment of 4096.
  2895. * The drm_mm range manager is optimised to return results
  2896. * with zero alignment, so where possible use the optimal
  2897. * path.
  2898. */
  2899. BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
  2900. if (alignment <= I915_GTT_MIN_ALIGNMENT)
  2901. alignment = 0;
  2902. err = drm_mm_insert_node_in_range(&vm->mm, node,
  2903. size, alignment, color,
  2904. start, end, mode);
  2905. if (err != -ENOSPC)
  2906. return err;
  2907. if (flags & PIN_NOEVICT)
  2908. return -ENOSPC;
  2909. /* No free space, pick a slot at random.
  2910. *
  2911. * There is a pathological case here using a GTT shared between
  2912. * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
  2913. *
  2914. * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
  2915. * (64k objects) (448k objects)
  2916. *
  2917. * Now imagine that the eviction LRU is ordered top-down (just because
  2918. * pathology meets real life), and that we need to evict an object to
  2919. * make room inside the aperture. The eviction scan then has to walk
  2920. * the 448k list before it finds one within range. And now imagine that
  2921. * it has to search for a new hole between every byte inside the memcpy,
  2922. * for several simultaneous clients.
  2923. *
  2924. * On a full-ppgtt system, if we have run out of available space, there
  2925. * will be lots and lots of objects in the eviction list! Again,
  2926. * searching that LRU list may be slow if we are also applying any
  2927. * range restrictions (e.g. restriction to low 4GiB) and so, for
  2928. * simplicity and similarilty between different GTT, try the single
  2929. * random replacement first.
  2930. */
  2931. offset = random_offset(start, end,
  2932. size, alignment ?: I915_GTT_MIN_ALIGNMENT);
  2933. err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
  2934. if (err != -ENOSPC)
  2935. return err;
  2936. /* Randomly selected placement is pinned, do a search */
  2937. err = i915_gem_evict_something(vm, size, alignment, color,
  2938. start, end, flags);
  2939. if (err)
  2940. return err;
  2941. return drm_mm_insert_node_in_range(&vm->mm, node,
  2942. size, alignment, color,
  2943. start, end, DRM_MM_INSERT_EVICT);
  2944. }
  2945. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  2946. #include "selftests/mock_gtt.c"
  2947. #include "selftests/i915_gem_gtt.c"
  2948. #endif