i915_gem_gtt.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453
  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. /* emulation is too hard */
  133. has_full_ppgtt = false;
  134. has_full_48bit_ppgtt = false;
  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. #ifdef CONFIG_INTEL_IOMMU
  151. /* Disable ppgtt on SNB if VT-d is on. */
  152. if (IS_GEN6(dev_priv) && intel_iommu_gfx_mapped) {
  153. DRM_INFO("Disabling PPGTT because VT-d is on\n");
  154. return 0;
  155. }
  156. #endif
  157. /* Early VLV doesn't have this */
  158. if (IS_VALLEYVIEW(dev_priv) && dev_priv->drm.pdev->revision < 0xb) {
  159. DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
  160. return 0;
  161. }
  162. if (INTEL_GEN(dev_priv) >= 8 && i915.enable_execlists && has_full_ppgtt)
  163. return has_full_48bit_ppgtt ? 3 : 2;
  164. else
  165. return has_aliasing_ppgtt ? 1 : 0;
  166. }
  167. static int ppgtt_bind_vma(struct i915_vma *vma,
  168. enum i915_cache_level cache_level,
  169. u32 unused)
  170. {
  171. u32 pte_flags;
  172. int ret;
  173. if (!(vma->flags & I915_VMA_LOCAL_BIND)) {
  174. ret = vma->vm->allocate_va_range(vma->vm, vma->node.start,
  175. vma->size);
  176. if (ret)
  177. return ret;
  178. }
  179. vma->pages = vma->obj->mm.pages;
  180. /* Currently applicable only to VLV */
  181. pte_flags = 0;
  182. if (vma->obj->gt_ro)
  183. pte_flags |= PTE_READ_ONLY;
  184. vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
  185. 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 sg_table *pages,
  745. u64 start,
  746. enum i915_cache_level cache_level,
  747. u32 unused)
  748. {
  749. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  750. struct sgt_dma iter = {
  751. .sg = pages->sgl,
  752. .dma = sg_dma_address(iter.sg),
  753. .max = iter.dma + iter.sg->length,
  754. };
  755. struct gen8_insert_pte idx = gen8_insert_pte(start);
  756. gen8_ppgtt_insert_pte_entries(ppgtt, &ppgtt->pdp, &iter, &idx,
  757. cache_level);
  758. }
  759. static void gen8_ppgtt_insert_4lvl(struct i915_address_space *vm,
  760. struct sg_table *pages,
  761. u64 start,
  762. enum i915_cache_level cache_level,
  763. u32 unused)
  764. {
  765. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  766. struct sgt_dma iter = {
  767. .sg = pages->sgl,
  768. .dma = sg_dma_address(iter.sg),
  769. .max = iter.dma + iter.sg->length,
  770. };
  771. struct i915_page_directory_pointer **pdps = ppgtt->pml4.pdps;
  772. struct gen8_insert_pte idx = gen8_insert_pte(start);
  773. while (gen8_ppgtt_insert_pte_entries(ppgtt, pdps[idx.pml4e++], &iter,
  774. &idx, cache_level))
  775. GEM_BUG_ON(idx.pml4e >= GEN8_PML4ES_PER_PML4);
  776. }
  777. static void gen8_free_page_tables(struct i915_address_space *vm,
  778. struct i915_page_directory *pd)
  779. {
  780. int i;
  781. if (!px_page(pd))
  782. return;
  783. for (i = 0; i < I915_PDES; i++) {
  784. if (pd->page_table[i] != vm->scratch_pt)
  785. free_pt(vm, pd->page_table[i]);
  786. }
  787. }
  788. static int gen8_init_scratch(struct i915_address_space *vm)
  789. {
  790. int ret;
  791. ret = setup_scratch_page(vm, I915_GFP_DMA);
  792. if (ret)
  793. return ret;
  794. vm->scratch_pt = alloc_pt(vm);
  795. if (IS_ERR(vm->scratch_pt)) {
  796. ret = PTR_ERR(vm->scratch_pt);
  797. goto free_scratch_page;
  798. }
  799. vm->scratch_pd = alloc_pd(vm);
  800. if (IS_ERR(vm->scratch_pd)) {
  801. ret = PTR_ERR(vm->scratch_pd);
  802. goto free_pt;
  803. }
  804. if (use_4lvl(vm)) {
  805. vm->scratch_pdp = alloc_pdp(vm);
  806. if (IS_ERR(vm->scratch_pdp)) {
  807. ret = PTR_ERR(vm->scratch_pdp);
  808. goto free_pd;
  809. }
  810. }
  811. gen8_initialize_pt(vm, vm->scratch_pt);
  812. gen8_initialize_pd(vm, vm->scratch_pd);
  813. if (use_4lvl(vm))
  814. gen8_initialize_pdp(vm, vm->scratch_pdp);
  815. return 0;
  816. free_pd:
  817. free_pd(vm, vm->scratch_pd);
  818. free_pt:
  819. free_pt(vm, vm->scratch_pt);
  820. free_scratch_page:
  821. cleanup_scratch_page(vm);
  822. return ret;
  823. }
  824. static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
  825. {
  826. struct i915_address_space *vm = &ppgtt->base;
  827. struct drm_i915_private *dev_priv = vm->i915;
  828. enum vgt_g2v_type msg;
  829. int i;
  830. if (use_4lvl(vm)) {
  831. const u64 daddr = px_dma(&ppgtt->pml4);
  832. I915_WRITE(vgtif_reg(pdp[0].lo), lower_32_bits(daddr));
  833. I915_WRITE(vgtif_reg(pdp[0].hi), upper_32_bits(daddr));
  834. msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
  835. VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
  836. } else {
  837. for (i = 0; i < GEN8_3LVL_PDPES; i++) {
  838. const u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
  839. I915_WRITE(vgtif_reg(pdp[i].lo), lower_32_bits(daddr));
  840. I915_WRITE(vgtif_reg(pdp[i].hi), upper_32_bits(daddr));
  841. }
  842. msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
  843. VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
  844. }
  845. I915_WRITE(vgtif_reg(g2v_notify), msg);
  846. return 0;
  847. }
  848. static void gen8_free_scratch(struct i915_address_space *vm)
  849. {
  850. if (use_4lvl(vm))
  851. free_pdp(vm, vm->scratch_pdp);
  852. free_pd(vm, vm->scratch_pd);
  853. free_pt(vm, vm->scratch_pt);
  854. cleanup_scratch_page(vm);
  855. }
  856. static void gen8_ppgtt_cleanup_3lvl(struct i915_address_space *vm,
  857. struct i915_page_directory_pointer *pdp)
  858. {
  859. const unsigned int pdpes = i915_pdpes_per_pdp(vm);
  860. int i;
  861. for (i = 0; i < pdpes; i++) {
  862. if (pdp->page_directory[i] == vm->scratch_pd)
  863. continue;
  864. gen8_free_page_tables(vm, pdp->page_directory[i]);
  865. free_pd(vm, pdp->page_directory[i]);
  866. }
  867. free_pdp(vm, pdp);
  868. }
  869. static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
  870. {
  871. int i;
  872. for (i = 0; i < GEN8_PML4ES_PER_PML4; i++) {
  873. if (ppgtt->pml4.pdps[i] == ppgtt->base.scratch_pdp)
  874. continue;
  875. gen8_ppgtt_cleanup_3lvl(&ppgtt->base, ppgtt->pml4.pdps[i]);
  876. }
  877. cleanup_px(&ppgtt->base, &ppgtt->pml4);
  878. }
  879. static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
  880. {
  881. struct drm_i915_private *dev_priv = vm->i915;
  882. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  883. if (intel_vgpu_active(dev_priv))
  884. gen8_ppgtt_notify_vgt(ppgtt, false);
  885. if (use_4lvl(vm))
  886. gen8_ppgtt_cleanup_4lvl(ppgtt);
  887. else
  888. gen8_ppgtt_cleanup_3lvl(&ppgtt->base, &ppgtt->pdp);
  889. gen8_free_scratch(vm);
  890. }
  891. static int gen8_ppgtt_alloc_pd(struct i915_address_space *vm,
  892. struct i915_page_directory *pd,
  893. u64 start, u64 length)
  894. {
  895. struct i915_page_table *pt;
  896. u64 from = start;
  897. unsigned int pde;
  898. gen8_for_each_pde(pt, pd, start, length, pde) {
  899. if (pt == vm->scratch_pt) {
  900. pt = alloc_pt(vm);
  901. if (IS_ERR(pt))
  902. goto unwind;
  903. gen8_initialize_pt(vm, pt);
  904. gen8_ppgtt_set_pde(vm, pd, pt, pde);
  905. pd->used_pdes++;
  906. GEM_BUG_ON(pd->used_pdes > I915_PDES);
  907. }
  908. pt->used_ptes += gen8_pte_count(start, length);
  909. }
  910. return 0;
  911. unwind:
  912. gen8_ppgtt_clear_pd(vm, pd, from, start - from);
  913. return -ENOMEM;
  914. }
  915. static int gen8_ppgtt_alloc_pdp(struct i915_address_space *vm,
  916. struct i915_page_directory_pointer *pdp,
  917. u64 start, u64 length)
  918. {
  919. struct i915_page_directory *pd;
  920. u64 from = start;
  921. unsigned int pdpe;
  922. int ret;
  923. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  924. if (pd == vm->scratch_pd) {
  925. pd = alloc_pd(vm);
  926. if (IS_ERR(pd))
  927. goto unwind;
  928. gen8_initialize_pd(vm, pd);
  929. gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
  930. pdp->used_pdpes++;
  931. GEM_BUG_ON(pdp->used_pdpes > i915_pdpes_per_pdp(vm));
  932. mark_tlbs_dirty(i915_vm_to_ppgtt(vm));
  933. }
  934. ret = gen8_ppgtt_alloc_pd(vm, pd, start, length);
  935. if (unlikely(ret))
  936. goto unwind_pd;
  937. }
  938. return 0;
  939. unwind_pd:
  940. if (!pd->used_pdes) {
  941. gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
  942. GEM_BUG_ON(!pdp->used_pdpes);
  943. pdp->used_pdpes--;
  944. free_pd(vm, pd);
  945. }
  946. unwind:
  947. gen8_ppgtt_clear_pdp(vm, pdp, from, start - from);
  948. return -ENOMEM;
  949. }
  950. static int gen8_ppgtt_alloc_3lvl(struct i915_address_space *vm,
  951. u64 start, u64 length)
  952. {
  953. return gen8_ppgtt_alloc_pdp(vm,
  954. &i915_vm_to_ppgtt(vm)->pdp, start, length);
  955. }
  956. static int gen8_ppgtt_alloc_4lvl(struct i915_address_space *vm,
  957. u64 start, u64 length)
  958. {
  959. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  960. struct i915_pml4 *pml4 = &ppgtt->pml4;
  961. struct i915_page_directory_pointer *pdp;
  962. u64 from = start;
  963. u32 pml4e;
  964. int ret;
  965. gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
  966. if (pml4->pdps[pml4e] == vm->scratch_pdp) {
  967. pdp = alloc_pdp(vm);
  968. if (IS_ERR(pdp))
  969. goto unwind;
  970. gen8_initialize_pdp(vm, pdp);
  971. gen8_ppgtt_set_pml4e(pml4, pdp, pml4e);
  972. }
  973. ret = gen8_ppgtt_alloc_pdp(vm, pdp, start, length);
  974. if (unlikely(ret))
  975. goto unwind_pdp;
  976. }
  977. return 0;
  978. unwind_pdp:
  979. if (!pdp->used_pdpes) {
  980. gen8_ppgtt_set_pml4e(pml4, vm->scratch_pdp, pml4e);
  981. free_pdp(vm, pdp);
  982. }
  983. unwind:
  984. gen8_ppgtt_clear_4lvl(vm, from, start - from);
  985. return -ENOMEM;
  986. }
  987. static void gen8_dump_pdp(struct i915_hw_ppgtt *ppgtt,
  988. struct i915_page_directory_pointer *pdp,
  989. u64 start, u64 length,
  990. gen8_pte_t scratch_pte,
  991. struct seq_file *m)
  992. {
  993. struct i915_address_space *vm = &ppgtt->base;
  994. struct i915_page_directory *pd;
  995. u32 pdpe;
  996. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  997. struct i915_page_table *pt;
  998. u64 pd_len = length;
  999. u64 pd_start = start;
  1000. u32 pde;
  1001. if (pdp->page_directory[pdpe] == ppgtt->base.scratch_pd)
  1002. continue;
  1003. seq_printf(m, "\tPDPE #%d\n", pdpe);
  1004. gen8_for_each_pde(pt, pd, pd_start, pd_len, pde) {
  1005. u32 pte;
  1006. gen8_pte_t *pt_vaddr;
  1007. if (pd->page_table[pde] == ppgtt->base.scratch_pt)
  1008. continue;
  1009. pt_vaddr = kmap_atomic_px(pt);
  1010. for (pte = 0; pte < GEN8_PTES; pte += 4) {
  1011. u64 va = (pdpe << GEN8_PDPE_SHIFT |
  1012. pde << GEN8_PDE_SHIFT |
  1013. pte << GEN8_PTE_SHIFT);
  1014. int i;
  1015. bool found = false;
  1016. for (i = 0; i < 4; i++)
  1017. if (pt_vaddr[pte + i] != scratch_pte)
  1018. found = true;
  1019. if (!found)
  1020. continue;
  1021. seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
  1022. for (i = 0; i < 4; i++) {
  1023. if (pt_vaddr[pte + i] != scratch_pte)
  1024. seq_printf(m, " %llx", pt_vaddr[pte + i]);
  1025. else
  1026. seq_puts(m, " SCRATCH ");
  1027. }
  1028. seq_puts(m, "\n");
  1029. }
  1030. kunmap_atomic(pt_vaddr);
  1031. }
  1032. }
  1033. }
  1034. static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
  1035. {
  1036. struct i915_address_space *vm = &ppgtt->base;
  1037. const gen8_pte_t scratch_pte =
  1038. gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
  1039. u64 start = 0, length = ppgtt->base.total;
  1040. if (use_4lvl(vm)) {
  1041. u64 pml4e;
  1042. struct i915_pml4 *pml4 = &ppgtt->pml4;
  1043. struct i915_page_directory_pointer *pdp;
  1044. gen8_for_each_pml4e(pdp, pml4, start, length, pml4e) {
  1045. if (pml4->pdps[pml4e] == ppgtt->base.scratch_pdp)
  1046. continue;
  1047. seq_printf(m, " PML4E #%llu\n", pml4e);
  1048. gen8_dump_pdp(ppgtt, pdp, start, length, scratch_pte, m);
  1049. }
  1050. } else {
  1051. gen8_dump_pdp(ppgtt, &ppgtt->pdp, start, length, scratch_pte, m);
  1052. }
  1053. }
  1054. static int gen8_preallocate_top_level_pdp(struct i915_hw_ppgtt *ppgtt)
  1055. {
  1056. struct i915_address_space *vm = &ppgtt->base;
  1057. struct i915_page_directory_pointer *pdp = &ppgtt->pdp;
  1058. struct i915_page_directory *pd;
  1059. u64 start = 0, length = ppgtt->base.total;
  1060. u64 from = start;
  1061. unsigned int pdpe;
  1062. gen8_for_each_pdpe(pd, pdp, start, length, pdpe) {
  1063. pd = alloc_pd(vm);
  1064. if (IS_ERR(pd))
  1065. goto unwind;
  1066. gen8_initialize_pd(vm, pd);
  1067. gen8_ppgtt_set_pdpe(vm, pdp, pd, pdpe);
  1068. pdp->used_pdpes++;
  1069. }
  1070. pdp->used_pdpes++; /* never remove */
  1071. return 0;
  1072. unwind:
  1073. start -= from;
  1074. gen8_for_each_pdpe(pd, pdp, from, start, pdpe) {
  1075. gen8_ppgtt_set_pdpe(vm, pdp, vm->scratch_pd, pdpe);
  1076. free_pd(vm, pd);
  1077. }
  1078. pdp->used_pdpes = 0;
  1079. return -ENOMEM;
  1080. }
  1081. /*
  1082. * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
  1083. * with a net effect resembling a 2-level page table in normal x86 terms. Each
  1084. * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
  1085. * space.
  1086. *
  1087. */
  1088. static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
  1089. {
  1090. struct i915_address_space *vm = &ppgtt->base;
  1091. struct drm_i915_private *dev_priv = vm->i915;
  1092. int ret;
  1093. ppgtt->base.total = USES_FULL_48BIT_PPGTT(dev_priv) ?
  1094. 1ULL << 48 :
  1095. 1ULL << 32;
  1096. ret = gen8_init_scratch(&ppgtt->base);
  1097. if (ret) {
  1098. ppgtt->base.total = 0;
  1099. return ret;
  1100. }
  1101. /* There are only few exceptions for gen >=6. chv and bxt.
  1102. * And we are not sure about the latter so play safe for now.
  1103. */
  1104. if (IS_CHERRYVIEW(dev_priv) || IS_BROXTON(dev_priv))
  1105. ppgtt->base.pt_kmap_wc = true;
  1106. if (use_4lvl(vm)) {
  1107. ret = setup_px(&ppgtt->base, &ppgtt->pml4);
  1108. if (ret)
  1109. goto free_scratch;
  1110. gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
  1111. ppgtt->switch_mm = gen8_mm_switch_4lvl;
  1112. ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_4lvl;
  1113. ppgtt->base.insert_entries = gen8_ppgtt_insert_4lvl;
  1114. ppgtt->base.clear_range = gen8_ppgtt_clear_4lvl;
  1115. } else {
  1116. ret = __pdp_init(&ppgtt->base, &ppgtt->pdp);
  1117. if (ret)
  1118. goto free_scratch;
  1119. if (intel_vgpu_active(dev_priv)) {
  1120. ret = gen8_preallocate_top_level_pdp(ppgtt);
  1121. if (ret) {
  1122. __pdp_fini(&ppgtt->pdp);
  1123. goto free_scratch;
  1124. }
  1125. }
  1126. ppgtt->switch_mm = gen8_mm_switch_3lvl;
  1127. ppgtt->base.allocate_va_range = gen8_ppgtt_alloc_3lvl;
  1128. ppgtt->base.insert_entries = gen8_ppgtt_insert_3lvl;
  1129. ppgtt->base.clear_range = gen8_ppgtt_clear_3lvl;
  1130. }
  1131. if (intel_vgpu_active(dev_priv))
  1132. gen8_ppgtt_notify_vgt(ppgtt, true);
  1133. ppgtt->base.cleanup = gen8_ppgtt_cleanup;
  1134. ppgtt->base.unbind_vma = ppgtt_unbind_vma;
  1135. ppgtt->base.bind_vma = ppgtt_bind_vma;
  1136. ppgtt->debug_dump = gen8_dump_ppgtt;
  1137. return 0;
  1138. free_scratch:
  1139. gen8_free_scratch(&ppgtt->base);
  1140. return ret;
  1141. }
  1142. static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
  1143. {
  1144. struct i915_address_space *vm = &ppgtt->base;
  1145. struct i915_page_table *unused;
  1146. gen6_pte_t scratch_pte;
  1147. u32 pd_entry, pte, pde;
  1148. u32 start = 0, length = ppgtt->base.total;
  1149. scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
  1150. I915_CACHE_LLC, 0);
  1151. gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde) {
  1152. u32 expected;
  1153. gen6_pte_t *pt_vaddr;
  1154. const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
  1155. pd_entry = readl(ppgtt->pd_addr + pde);
  1156. expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
  1157. if (pd_entry != expected)
  1158. seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
  1159. pde,
  1160. pd_entry,
  1161. expected);
  1162. seq_printf(m, "\tPDE: %x\n", pd_entry);
  1163. pt_vaddr = kmap_atomic_px(ppgtt->pd.page_table[pde]);
  1164. for (pte = 0; pte < GEN6_PTES; pte+=4) {
  1165. unsigned long va =
  1166. (pde * PAGE_SIZE * GEN6_PTES) +
  1167. (pte * PAGE_SIZE);
  1168. int i;
  1169. bool found = false;
  1170. for (i = 0; i < 4; i++)
  1171. if (pt_vaddr[pte + i] != scratch_pte)
  1172. found = true;
  1173. if (!found)
  1174. continue;
  1175. seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
  1176. for (i = 0; i < 4; i++) {
  1177. if (pt_vaddr[pte + i] != scratch_pte)
  1178. seq_printf(m, " %08x", pt_vaddr[pte + i]);
  1179. else
  1180. seq_puts(m, " SCRATCH ");
  1181. }
  1182. seq_puts(m, "\n");
  1183. }
  1184. kunmap_atomic(pt_vaddr);
  1185. }
  1186. }
  1187. /* Write pde (index) from the page directory @pd to the page table @pt */
  1188. static inline void gen6_write_pde(const struct i915_hw_ppgtt *ppgtt,
  1189. const unsigned int pde,
  1190. const struct i915_page_table *pt)
  1191. {
  1192. /* Caller needs to make sure the write completes if necessary */
  1193. writel_relaxed(GEN6_PDE_ADDR_ENCODE(px_dma(pt)) | GEN6_PDE_VALID,
  1194. ppgtt->pd_addr + pde);
  1195. }
  1196. /* Write all the page tables found in the ppgtt structure to incrementing page
  1197. * directories. */
  1198. static void gen6_write_page_range(struct i915_hw_ppgtt *ppgtt,
  1199. u32 start, u32 length)
  1200. {
  1201. struct i915_page_table *pt;
  1202. unsigned int pde;
  1203. gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde)
  1204. gen6_write_pde(ppgtt, pde, pt);
  1205. mark_tlbs_dirty(ppgtt);
  1206. wmb();
  1207. }
  1208. static inline u32 get_pd_offset(struct i915_hw_ppgtt *ppgtt)
  1209. {
  1210. GEM_BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
  1211. return ppgtt->pd.base.ggtt_offset << 10;
  1212. }
  1213. static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
  1214. struct drm_i915_gem_request *req)
  1215. {
  1216. struct intel_engine_cs *engine = req->engine;
  1217. u32 *cs;
  1218. /* NB: TLBs must be flushed and invalidated before a switch */
  1219. cs = intel_ring_begin(req, 6);
  1220. if (IS_ERR(cs))
  1221. return PTR_ERR(cs);
  1222. *cs++ = MI_LOAD_REGISTER_IMM(2);
  1223. *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
  1224. *cs++ = PP_DIR_DCLV_2G;
  1225. *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
  1226. *cs++ = get_pd_offset(ppgtt);
  1227. *cs++ = MI_NOOP;
  1228. intel_ring_advance(req, cs);
  1229. return 0;
  1230. }
  1231. static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
  1232. struct drm_i915_gem_request *req)
  1233. {
  1234. struct intel_engine_cs *engine = req->engine;
  1235. u32 *cs;
  1236. /* NB: TLBs must be flushed and invalidated before a switch */
  1237. cs = intel_ring_begin(req, 6);
  1238. if (IS_ERR(cs))
  1239. return PTR_ERR(cs);
  1240. *cs++ = MI_LOAD_REGISTER_IMM(2);
  1241. *cs++ = i915_mmio_reg_offset(RING_PP_DIR_DCLV(engine));
  1242. *cs++ = PP_DIR_DCLV_2G;
  1243. *cs++ = i915_mmio_reg_offset(RING_PP_DIR_BASE(engine));
  1244. *cs++ = get_pd_offset(ppgtt);
  1245. *cs++ = MI_NOOP;
  1246. intel_ring_advance(req, cs);
  1247. return 0;
  1248. }
  1249. static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
  1250. struct drm_i915_gem_request *req)
  1251. {
  1252. struct intel_engine_cs *engine = req->engine;
  1253. struct drm_i915_private *dev_priv = req->i915;
  1254. I915_WRITE(RING_PP_DIR_DCLV(engine), PP_DIR_DCLV_2G);
  1255. I915_WRITE(RING_PP_DIR_BASE(engine), get_pd_offset(ppgtt));
  1256. return 0;
  1257. }
  1258. static void gen8_ppgtt_enable(struct drm_i915_private *dev_priv)
  1259. {
  1260. struct intel_engine_cs *engine;
  1261. enum intel_engine_id id;
  1262. for_each_engine(engine, dev_priv, id) {
  1263. u32 four_level = USES_FULL_48BIT_PPGTT(dev_priv) ?
  1264. GEN8_GFX_PPGTT_48B : 0;
  1265. I915_WRITE(RING_MODE_GEN7(engine),
  1266. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
  1267. }
  1268. }
  1269. static void gen7_ppgtt_enable(struct drm_i915_private *dev_priv)
  1270. {
  1271. struct intel_engine_cs *engine;
  1272. u32 ecochk, ecobits;
  1273. enum intel_engine_id id;
  1274. ecobits = I915_READ(GAC_ECO_BITS);
  1275. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
  1276. ecochk = I915_READ(GAM_ECOCHK);
  1277. if (IS_HASWELL(dev_priv)) {
  1278. ecochk |= ECOCHK_PPGTT_WB_HSW;
  1279. } else {
  1280. ecochk |= ECOCHK_PPGTT_LLC_IVB;
  1281. ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
  1282. }
  1283. I915_WRITE(GAM_ECOCHK, ecochk);
  1284. for_each_engine(engine, dev_priv, id) {
  1285. /* GFX_MODE is per-ring on gen7+ */
  1286. I915_WRITE(RING_MODE_GEN7(engine),
  1287. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  1288. }
  1289. }
  1290. static void gen6_ppgtt_enable(struct drm_i915_private *dev_priv)
  1291. {
  1292. u32 ecochk, gab_ctl, ecobits;
  1293. ecobits = I915_READ(GAC_ECO_BITS);
  1294. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
  1295. ECOBITS_PPGTT_CACHE64B);
  1296. gab_ctl = I915_READ(GAB_CTL);
  1297. I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
  1298. ecochk = I915_READ(GAM_ECOCHK);
  1299. I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
  1300. I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  1301. }
  1302. /* PPGTT support for Sandybdrige/Gen6 and later */
  1303. static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
  1304. u64 start, u64 length)
  1305. {
  1306. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1307. unsigned int first_entry = start >> PAGE_SHIFT;
  1308. unsigned int pde = first_entry / GEN6_PTES;
  1309. unsigned int pte = first_entry % GEN6_PTES;
  1310. unsigned int num_entries = length >> PAGE_SHIFT;
  1311. gen6_pte_t scratch_pte =
  1312. vm->pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC, 0);
  1313. while (num_entries) {
  1314. struct i915_page_table *pt = ppgtt->pd.page_table[pde++];
  1315. unsigned int end = min(pte + num_entries, GEN6_PTES);
  1316. gen6_pte_t *vaddr;
  1317. num_entries -= end - pte;
  1318. /* Note that the hw doesn't support removing PDE on the fly
  1319. * (they are cached inside the context with no means to
  1320. * invalidate the cache), so we can only reset the PTE
  1321. * entries back to scratch.
  1322. */
  1323. vaddr = kmap_atomic_px(pt);
  1324. do {
  1325. vaddr[pte++] = scratch_pte;
  1326. } while (pte < end);
  1327. kunmap_atomic(vaddr);
  1328. pte = 0;
  1329. }
  1330. }
  1331. static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
  1332. struct sg_table *pages,
  1333. u64 start,
  1334. enum i915_cache_level cache_level,
  1335. u32 flags)
  1336. {
  1337. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1338. unsigned first_entry = start >> PAGE_SHIFT;
  1339. unsigned act_pt = first_entry / GEN6_PTES;
  1340. unsigned act_pte = first_entry % GEN6_PTES;
  1341. const u32 pte_encode = vm->pte_encode(0, cache_level, flags);
  1342. struct sgt_dma iter;
  1343. gen6_pte_t *vaddr;
  1344. vaddr = kmap_atomic_px(ppgtt->pd.page_table[act_pt]);
  1345. iter.sg = pages->sgl;
  1346. iter.dma = sg_dma_address(iter.sg);
  1347. iter.max = iter.dma + iter.sg->length;
  1348. do {
  1349. vaddr[act_pte] = pte_encode | GEN6_PTE_ADDR_ENCODE(iter.dma);
  1350. iter.dma += PAGE_SIZE;
  1351. if (iter.dma == iter.max) {
  1352. iter.sg = __sg_next(iter.sg);
  1353. if (!iter.sg)
  1354. break;
  1355. iter.dma = sg_dma_address(iter.sg);
  1356. iter.max = iter.dma + iter.sg->length;
  1357. }
  1358. if (++act_pte == GEN6_PTES) {
  1359. kunmap_atomic(vaddr);
  1360. vaddr = kmap_atomic_px(ppgtt->pd.page_table[++act_pt]);
  1361. act_pte = 0;
  1362. }
  1363. } while (1);
  1364. kunmap_atomic(vaddr);
  1365. }
  1366. static int gen6_alloc_va_range(struct i915_address_space *vm,
  1367. u64 start, u64 length)
  1368. {
  1369. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1370. struct i915_page_table *pt;
  1371. u64 from = start;
  1372. unsigned int pde;
  1373. bool flush = false;
  1374. gen6_for_each_pde(pt, &ppgtt->pd, start, length, pde) {
  1375. if (pt == vm->scratch_pt) {
  1376. pt = alloc_pt(vm);
  1377. if (IS_ERR(pt))
  1378. goto unwind_out;
  1379. gen6_initialize_pt(vm, pt);
  1380. ppgtt->pd.page_table[pde] = pt;
  1381. gen6_write_pde(ppgtt, pde, pt);
  1382. flush = true;
  1383. }
  1384. }
  1385. if (flush) {
  1386. mark_tlbs_dirty(ppgtt);
  1387. wmb();
  1388. }
  1389. return 0;
  1390. unwind_out:
  1391. gen6_ppgtt_clear_range(vm, from, start);
  1392. return -ENOMEM;
  1393. }
  1394. static int gen6_init_scratch(struct i915_address_space *vm)
  1395. {
  1396. int ret;
  1397. ret = setup_scratch_page(vm, I915_GFP_DMA);
  1398. if (ret)
  1399. return ret;
  1400. vm->scratch_pt = alloc_pt(vm);
  1401. if (IS_ERR(vm->scratch_pt)) {
  1402. cleanup_scratch_page(vm);
  1403. return PTR_ERR(vm->scratch_pt);
  1404. }
  1405. gen6_initialize_pt(vm, vm->scratch_pt);
  1406. return 0;
  1407. }
  1408. static void gen6_free_scratch(struct i915_address_space *vm)
  1409. {
  1410. free_pt(vm, vm->scratch_pt);
  1411. cleanup_scratch_page(vm);
  1412. }
  1413. static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
  1414. {
  1415. struct i915_hw_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
  1416. struct i915_page_directory *pd = &ppgtt->pd;
  1417. struct i915_page_table *pt;
  1418. u32 pde;
  1419. drm_mm_remove_node(&ppgtt->node);
  1420. gen6_for_all_pdes(pt, pd, pde)
  1421. if (pt != vm->scratch_pt)
  1422. free_pt(vm, pt);
  1423. gen6_free_scratch(vm);
  1424. }
  1425. static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
  1426. {
  1427. struct i915_address_space *vm = &ppgtt->base;
  1428. struct drm_i915_private *dev_priv = ppgtt->base.i915;
  1429. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1430. int ret;
  1431. /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
  1432. * allocator works in address space sizes, so it's multiplied by page
  1433. * size. We allocate at the top of the GTT to avoid fragmentation.
  1434. */
  1435. BUG_ON(!drm_mm_initialized(&ggtt->base.mm));
  1436. ret = gen6_init_scratch(vm);
  1437. if (ret)
  1438. return ret;
  1439. ret = i915_gem_gtt_insert(&ggtt->base, &ppgtt->node,
  1440. GEN6_PD_SIZE, GEN6_PD_ALIGN,
  1441. I915_COLOR_UNEVICTABLE,
  1442. 0, ggtt->base.total,
  1443. PIN_HIGH);
  1444. if (ret)
  1445. goto err_out;
  1446. if (ppgtt->node.start < ggtt->mappable_end)
  1447. DRM_DEBUG("Forced to use aperture for PDEs\n");
  1448. ppgtt->pd.base.ggtt_offset =
  1449. ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
  1450. ppgtt->pd_addr = (gen6_pte_t __iomem *)ggtt->gsm +
  1451. ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
  1452. return 0;
  1453. err_out:
  1454. gen6_free_scratch(vm);
  1455. return ret;
  1456. }
  1457. static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
  1458. {
  1459. return gen6_ppgtt_allocate_page_directories(ppgtt);
  1460. }
  1461. static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
  1462. u64 start, u64 length)
  1463. {
  1464. struct i915_page_table *unused;
  1465. u32 pde;
  1466. gen6_for_each_pde(unused, &ppgtt->pd, start, length, pde)
  1467. ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
  1468. }
  1469. static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
  1470. {
  1471. struct drm_i915_private *dev_priv = ppgtt->base.i915;
  1472. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1473. int ret;
  1474. ppgtt->base.pte_encode = ggtt->base.pte_encode;
  1475. if (intel_vgpu_active(dev_priv) || IS_GEN6(dev_priv))
  1476. ppgtt->switch_mm = gen6_mm_switch;
  1477. else if (IS_HASWELL(dev_priv))
  1478. ppgtt->switch_mm = hsw_mm_switch;
  1479. else if (IS_GEN7(dev_priv))
  1480. ppgtt->switch_mm = gen7_mm_switch;
  1481. else
  1482. BUG();
  1483. ret = gen6_ppgtt_alloc(ppgtt);
  1484. if (ret)
  1485. return ret;
  1486. ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
  1487. gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
  1488. gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
  1489. ret = gen6_alloc_va_range(&ppgtt->base, 0, ppgtt->base.total);
  1490. if (ret) {
  1491. gen6_ppgtt_cleanup(&ppgtt->base);
  1492. return ret;
  1493. }
  1494. ppgtt->base.clear_range = gen6_ppgtt_clear_range;
  1495. ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
  1496. ppgtt->base.unbind_vma = ppgtt_unbind_vma;
  1497. ppgtt->base.bind_vma = ppgtt_bind_vma;
  1498. ppgtt->base.cleanup = gen6_ppgtt_cleanup;
  1499. ppgtt->debug_dump = gen6_dump_ppgtt;
  1500. DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
  1501. ppgtt->node.size >> 20,
  1502. ppgtt->node.start / PAGE_SIZE);
  1503. DRM_DEBUG_DRIVER("Adding PPGTT at offset %x\n",
  1504. ppgtt->pd.base.ggtt_offset << 10);
  1505. return 0;
  1506. }
  1507. static int __hw_ppgtt_init(struct i915_hw_ppgtt *ppgtt,
  1508. struct drm_i915_private *dev_priv)
  1509. {
  1510. ppgtt->base.i915 = dev_priv;
  1511. ppgtt->base.dma = &dev_priv->drm.pdev->dev;
  1512. if (INTEL_INFO(dev_priv)->gen < 8)
  1513. return gen6_ppgtt_init(ppgtt);
  1514. else
  1515. return gen8_ppgtt_init(ppgtt);
  1516. }
  1517. static void i915_address_space_init(struct i915_address_space *vm,
  1518. struct drm_i915_private *dev_priv,
  1519. const char *name)
  1520. {
  1521. i915_gem_timeline_init(dev_priv, &vm->timeline, name);
  1522. drm_mm_init(&vm->mm, 0, vm->total);
  1523. vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
  1524. INIT_LIST_HEAD(&vm->active_list);
  1525. INIT_LIST_HEAD(&vm->inactive_list);
  1526. INIT_LIST_HEAD(&vm->unbound_list);
  1527. list_add_tail(&vm->global_link, &dev_priv->vm_list);
  1528. pagevec_init(&vm->free_pages, false);
  1529. }
  1530. static void i915_address_space_fini(struct i915_address_space *vm)
  1531. {
  1532. if (pagevec_count(&vm->free_pages))
  1533. vm_free_pages_release(vm);
  1534. i915_gem_timeline_fini(&vm->timeline);
  1535. drm_mm_takedown(&vm->mm);
  1536. list_del(&vm->global_link);
  1537. }
  1538. static void gtt_write_workarounds(struct drm_i915_private *dev_priv)
  1539. {
  1540. /* This function is for gtt related workarounds. This function is
  1541. * called on driver load and after a GPU reset, so you can place
  1542. * workarounds here even if they get overwritten by GPU reset.
  1543. */
  1544. /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk */
  1545. if (IS_BROADWELL(dev_priv))
  1546. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
  1547. else if (IS_CHERRYVIEW(dev_priv))
  1548. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
  1549. else if (IS_GEN9_BC(dev_priv))
  1550. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
  1551. else if (IS_GEN9_LP(dev_priv))
  1552. I915_WRITE(GEN8_L3_LRA_1_GPGPU, GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
  1553. }
  1554. int i915_ppgtt_init_hw(struct drm_i915_private *dev_priv)
  1555. {
  1556. gtt_write_workarounds(dev_priv);
  1557. /* In the case of execlists, PPGTT is enabled by the context descriptor
  1558. * and the PDPs are contained within the context itself. We don't
  1559. * need to do anything here. */
  1560. if (i915.enable_execlists)
  1561. return 0;
  1562. if (!USES_PPGTT(dev_priv))
  1563. return 0;
  1564. if (IS_GEN6(dev_priv))
  1565. gen6_ppgtt_enable(dev_priv);
  1566. else if (IS_GEN7(dev_priv))
  1567. gen7_ppgtt_enable(dev_priv);
  1568. else if (INTEL_GEN(dev_priv) >= 8)
  1569. gen8_ppgtt_enable(dev_priv);
  1570. else
  1571. MISSING_CASE(INTEL_GEN(dev_priv));
  1572. return 0;
  1573. }
  1574. struct i915_hw_ppgtt *
  1575. i915_ppgtt_create(struct drm_i915_private *dev_priv,
  1576. struct drm_i915_file_private *fpriv,
  1577. const char *name)
  1578. {
  1579. struct i915_hw_ppgtt *ppgtt;
  1580. int ret;
  1581. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  1582. if (!ppgtt)
  1583. return ERR_PTR(-ENOMEM);
  1584. ret = __hw_ppgtt_init(ppgtt, dev_priv);
  1585. if (ret) {
  1586. kfree(ppgtt);
  1587. return ERR_PTR(ret);
  1588. }
  1589. kref_init(&ppgtt->ref);
  1590. i915_address_space_init(&ppgtt->base, dev_priv, name);
  1591. ppgtt->base.file = fpriv;
  1592. trace_i915_ppgtt_create(&ppgtt->base);
  1593. return ppgtt;
  1594. }
  1595. void i915_ppgtt_close(struct i915_address_space *vm)
  1596. {
  1597. struct list_head *phases[] = {
  1598. &vm->active_list,
  1599. &vm->inactive_list,
  1600. &vm->unbound_list,
  1601. NULL,
  1602. }, **phase;
  1603. GEM_BUG_ON(vm->closed);
  1604. vm->closed = true;
  1605. for (phase = phases; *phase; phase++) {
  1606. struct i915_vma *vma, *vn;
  1607. list_for_each_entry_safe(vma, vn, *phase, vm_link)
  1608. if (!i915_vma_is_closed(vma))
  1609. i915_vma_close(vma);
  1610. }
  1611. }
  1612. void i915_ppgtt_release(struct kref *kref)
  1613. {
  1614. struct i915_hw_ppgtt *ppgtt =
  1615. container_of(kref, struct i915_hw_ppgtt, ref);
  1616. trace_i915_ppgtt_release(&ppgtt->base);
  1617. /* vmas should already be unbound and destroyed */
  1618. WARN_ON(!list_empty(&ppgtt->base.active_list));
  1619. WARN_ON(!list_empty(&ppgtt->base.inactive_list));
  1620. WARN_ON(!list_empty(&ppgtt->base.unbound_list));
  1621. ppgtt->base.cleanup(&ppgtt->base);
  1622. i915_address_space_fini(&ppgtt->base);
  1623. kfree(ppgtt);
  1624. }
  1625. /* Certain Gen5 chipsets require require idling the GPU before
  1626. * unmapping anything from the GTT when VT-d is enabled.
  1627. */
  1628. static bool needs_idle_maps(struct drm_i915_private *dev_priv)
  1629. {
  1630. #ifdef CONFIG_INTEL_IOMMU
  1631. /* Query intel_iommu to see if we need the workaround. Presumably that
  1632. * was loaded first.
  1633. */
  1634. if (IS_GEN5(dev_priv) && IS_MOBILE(dev_priv) && intel_iommu_gfx_mapped)
  1635. return true;
  1636. #endif
  1637. return false;
  1638. }
  1639. void i915_check_and_clear_faults(struct drm_i915_private *dev_priv)
  1640. {
  1641. struct intel_engine_cs *engine;
  1642. enum intel_engine_id id;
  1643. if (INTEL_INFO(dev_priv)->gen < 6)
  1644. return;
  1645. for_each_engine(engine, dev_priv, id) {
  1646. u32 fault_reg;
  1647. fault_reg = I915_READ(RING_FAULT_REG(engine));
  1648. if (fault_reg & RING_FAULT_VALID) {
  1649. DRM_DEBUG_DRIVER("Unexpected fault\n"
  1650. "\tAddr: 0x%08lx\n"
  1651. "\tAddress space: %s\n"
  1652. "\tSource ID: %d\n"
  1653. "\tType: %d\n",
  1654. fault_reg & PAGE_MASK,
  1655. fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
  1656. RING_FAULT_SRCID(fault_reg),
  1657. RING_FAULT_FAULT_TYPE(fault_reg));
  1658. I915_WRITE(RING_FAULT_REG(engine),
  1659. fault_reg & ~RING_FAULT_VALID);
  1660. }
  1661. }
  1662. /* Engine specific init may not have been done till this point. */
  1663. if (dev_priv->engine[RCS])
  1664. POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
  1665. }
  1666. void i915_gem_suspend_gtt_mappings(struct drm_i915_private *dev_priv)
  1667. {
  1668. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1669. /* Don't bother messing with faults pre GEN6 as we have little
  1670. * documentation supporting that it's a good idea.
  1671. */
  1672. if (INTEL_GEN(dev_priv) < 6)
  1673. return;
  1674. i915_check_and_clear_faults(dev_priv);
  1675. ggtt->base.clear_range(&ggtt->base, 0, ggtt->base.total);
  1676. i915_ggtt_invalidate(dev_priv);
  1677. }
  1678. int i915_gem_gtt_prepare_pages(struct drm_i915_gem_object *obj,
  1679. struct sg_table *pages)
  1680. {
  1681. do {
  1682. if (dma_map_sg(&obj->base.dev->pdev->dev,
  1683. pages->sgl, pages->nents,
  1684. PCI_DMA_BIDIRECTIONAL))
  1685. return 0;
  1686. /* If the DMA remap fails, one cause can be that we have
  1687. * too many objects pinned in a small remapping table,
  1688. * such as swiotlb. Incrementally purge all other objects and
  1689. * try again - if there are no more pages to remove from
  1690. * the DMA remapper, i915_gem_shrink will return 0.
  1691. */
  1692. GEM_BUG_ON(obj->mm.pages == pages);
  1693. } while (i915_gem_shrink(to_i915(obj->base.dev),
  1694. obj->base.size >> PAGE_SHIFT,
  1695. I915_SHRINK_BOUND |
  1696. I915_SHRINK_UNBOUND |
  1697. I915_SHRINK_ACTIVE));
  1698. return -ENOSPC;
  1699. }
  1700. static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
  1701. {
  1702. writeq(pte, addr);
  1703. }
  1704. static void gen8_ggtt_insert_page(struct i915_address_space *vm,
  1705. dma_addr_t addr,
  1706. u64 offset,
  1707. enum i915_cache_level level,
  1708. u32 unused)
  1709. {
  1710. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1711. gen8_pte_t __iomem *pte =
  1712. (gen8_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
  1713. gen8_set_pte(pte, gen8_pte_encode(addr, level));
  1714. ggtt->invalidate(vm->i915);
  1715. }
  1716. static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
  1717. struct sg_table *st,
  1718. u64 start,
  1719. enum i915_cache_level level,
  1720. u32 unused)
  1721. {
  1722. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1723. struct sgt_iter sgt_iter;
  1724. gen8_pte_t __iomem *gtt_entries;
  1725. const gen8_pte_t pte_encode = gen8_pte_encode(0, level);
  1726. dma_addr_t addr;
  1727. gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
  1728. gtt_entries += start >> PAGE_SHIFT;
  1729. for_each_sgt_dma(addr, sgt_iter, st)
  1730. gen8_set_pte(gtt_entries++, pte_encode | addr);
  1731. wmb();
  1732. /* This next bit makes the above posting read even more important. We
  1733. * want to flush the TLBs only after we're certain all the PTE updates
  1734. * have finished.
  1735. */
  1736. ggtt->invalidate(vm->i915);
  1737. }
  1738. static void gen6_ggtt_insert_page(struct i915_address_space *vm,
  1739. dma_addr_t addr,
  1740. u64 offset,
  1741. enum i915_cache_level level,
  1742. u32 flags)
  1743. {
  1744. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1745. gen6_pte_t __iomem *pte =
  1746. (gen6_pte_t __iomem *)ggtt->gsm + (offset >> PAGE_SHIFT);
  1747. iowrite32(vm->pte_encode(addr, level, flags), pte);
  1748. ggtt->invalidate(vm->i915);
  1749. }
  1750. /*
  1751. * Binds an object into the global gtt with the specified cache level. The object
  1752. * will be accessible to the GPU via commands whose operands reference offsets
  1753. * within the global GTT as well as accessible by the GPU through the GMADR
  1754. * mapped BAR (dev_priv->mm.gtt->gtt).
  1755. */
  1756. static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
  1757. struct sg_table *st,
  1758. u64 start,
  1759. enum i915_cache_level level,
  1760. u32 flags)
  1761. {
  1762. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1763. gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
  1764. unsigned int i = start >> PAGE_SHIFT;
  1765. struct sgt_iter iter;
  1766. dma_addr_t addr;
  1767. for_each_sgt_dma(addr, iter, st)
  1768. iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
  1769. wmb();
  1770. /* This next bit makes the above posting read even more important. We
  1771. * want to flush the TLBs only after we're certain all the PTE updates
  1772. * have finished.
  1773. */
  1774. ggtt->invalidate(vm->i915);
  1775. }
  1776. static void nop_clear_range(struct i915_address_space *vm,
  1777. u64 start, u64 length)
  1778. {
  1779. }
  1780. static void gen8_ggtt_clear_range(struct i915_address_space *vm,
  1781. u64 start, u64 length)
  1782. {
  1783. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1784. unsigned first_entry = start >> PAGE_SHIFT;
  1785. unsigned num_entries = length >> PAGE_SHIFT;
  1786. const gen8_pte_t scratch_pte =
  1787. gen8_pte_encode(vm->scratch_page.daddr, I915_CACHE_LLC);
  1788. gen8_pte_t __iomem *gtt_base =
  1789. (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
  1790. const int max_entries = ggtt_total_entries(ggtt) - first_entry;
  1791. int i;
  1792. if (WARN(num_entries > max_entries,
  1793. "First entry = %d; Num entries = %d (max=%d)\n",
  1794. first_entry, num_entries, max_entries))
  1795. num_entries = max_entries;
  1796. for (i = 0; i < num_entries; i++)
  1797. gen8_set_pte(&gtt_base[i], scratch_pte);
  1798. }
  1799. static void gen6_ggtt_clear_range(struct i915_address_space *vm,
  1800. u64 start, u64 length)
  1801. {
  1802. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1803. unsigned first_entry = start >> PAGE_SHIFT;
  1804. unsigned num_entries = length >> PAGE_SHIFT;
  1805. gen6_pte_t scratch_pte, __iomem *gtt_base =
  1806. (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
  1807. const int max_entries = ggtt_total_entries(ggtt) - first_entry;
  1808. int i;
  1809. if (WARN(num_entries > max_entries,
  1810. "First entry = %d; Num entries = %d (max=%d)\n",
  1811. first_entry, num_entries, max_entries))
  1812. num_entries = max_entries;
  1813. scratch_pte = vm->pte_encode(vm->scratch_page.daddr,
  1814. I915_CACHE_LLC, 0);
  1815. for (i = 0; i < num_entries; i++)
  1816. iowrite32(scratch_pte, &gtt_base[i]);
  1817. }
  1818. static void i915_ggtt_insert_page(struct i915_address_space *vm,
  1819. dma_addr_t addr,
  1820. u64 offset,
  1821. enum i915_cache_level cache_level,
  1822. u32 unused)
  1823. {
  1824. unsigned int flags = (cache_level == I915_CACHE_NONE) ?
  1825. AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
  1826. intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
  1827. }
  1828. static void i915_ggtt_insert_entries(struct i915_address_space *vm,
  1829. struct sg_table *pages,
  1830. u64 start,
  1831. enum i915_cache_level cache_level,
  1832. u32 unused)
  1833. {
  1834. unsigned int flags = (cache_level == I915_CACHE_NONE) ?
  1835. AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
  1836. intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
  1837. }
  1838. static void i915_ggtt_clear_range(struct i915_address_space *vm,
  1839. u64 start, u64 length)
  1840. {
  1841. intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
  1842. }
  1843. static int ggtt_bind_vma(struct i915_vma *vma,
  1844. enum i915_cache_level cache_level,
  1845. u32 flags)
  1846. {
  1847. struct drm_i915_private *i915 = vma->vm->i915;
  1848. struct drm_i915_gem_object *obj = vma->obj;
  1849. u32 pte_flags;
  1850. if (unlikely(!vma->pages)) {
  1851. int ret = i915_get_ggtt_vma_pages(vma);
  1852. if (ret)
  1853. return ret;
  1854. }
  1855. /* Currently applicable only to VLV */
  1856. pte_flags = 0;
  1857. if (obj->gt_ro)
  1858. pte_flags |= PTE_READ_ONLY;
  1859. intel_runtime_pm_get(i915);
  1860. vma->vm->insert_entries(vma->vm, vma->pages, vma->node.start,
  1861. cache_level, pte_flags);
  1862. intel_runtime_pm_put(i915);
  1863. /*
  1864. * Without aliasing PPGTT there's no difference between
  1865. * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
  1866. * upgrade to both bound if we bind either to avoid double-binding.
  1867. */
  1868. vma->flags |= I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
  1869. return 0;
  1870. }
  1871. static void ggtt_unbind_vma(struct i915_vma *vma)
  1872. {
  1873. struct drm_i915_private *i915 = vma->vm->i915;
  1874. intel_runtime_pm_get(i915);
  1875. vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
  1876. intel_runtime_pm_put(i915);
  1877. }
  1878. static int aliasing_gtt_bind_vma(struct i915_vma *vma,
  1879. enum i915_cache_level cache_level,
  1880. u32 flags)
  1881. {
  1882. struct drm_i915_private *i915 = vma->vm->i915;
  1883. u32 pte_flags;
  1884. int ret;
  1885. if (unlikely(!vma->pages)) {
  1886. ret = i915_get_ggtt_vma_pages(vma);
  1887. if (ret)
  1888. return ret;
  1889. }
  1890. /* Currently applicable only to VLV */
  1891. pte_flags = 0;
  1892. if (vma->obj->gt_ro)
  1893. pte_flags |= PTE_READ_ONLY;
  1894. if (flags & I915_VMA_LOCAL_BIND) {
  1895. struct i915_hw_ppgtt *appgtt = i915->mm.aliasing_ppgtt;
  1896. if (!(vma->flags & I915_VMA_LOCAL_BIND) &&
  1897. appgtt->base.allocate_va_range) {
  1898. ret = appgtt->base.allocate_va_range(&appgtt->base,
  1899. vma->node.start,
  1900. vma->size);
  1901. if (ret)
  1902. goto err_pages;
  1903. }
  1904. appgtt->base.insert_entries(&appgtt->base,
  1905. vma->pages, vma->node.start,
  1906. cache_level, pte_flags);
  1907. }
  1908. if (flags & I915_VMA_GLOBAL_BIND) {
  1909. intel_runtime_pm_get(i915);
  1910. vma->vm->insert_entries(vma->vm,
  1911. vma->pages, vma->node.start,
  1912. cache_level, pte_flags);
  1913. intel_runtime_pm_put(i915);
  1914. }
  1915. return 0;
  1916. err_pages:
  1917. if (!(vma->flags & (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND))) {
  1918. if (vma->pages != vma->obj->mm.pages) {
  1919. GEM_BUG_ON(!vma->pages);
  1920. sg_free_table(vma->pages);
  1921. kfree(vma->pages);
  1922. }
  1923. vma->pages = NULL;
  1924. }
  1925. return ret;
  1926. }
  1927. static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
  1928. {
  1929. struct drm_i915_private *i915 = vma->vm->i915;
  1930. if (vma->flags & I915_VMA_GLOBAL_BIND) {
  1931. intel_runtime_pm_get(i915);
  1932. vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
  1933. intel_runtime_pm_put(i915);
  1934. }
  1935. if (vma->flags & I915_VMA_LOCAL_BIND) {
  1936. struct i915_address_space *vm = &i915->mm.aliasing_ppgtt->base;
  1937. vm->clear_range(vm, vma->node.start, vma->size);
  1938. }
  1939. }
  1940. void i915_gem_gtt_finish_pages(struct drm_i915_gem_object *obj,
  1941. struct sg_table *pages)
  1942. {
  1943. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  1944. struct device *kdev = &dev_priv->drm.pdev->dev;
  1945. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  1946. if (unlikely(ggtt->do_idle_maps)) {
  1947. if (i915_gem_wait_for_idle(dev_priv, 0)) {
  1948. DRM_ERROR("Failed to wait for idle; VT'd may hang.\n");
  1949. /* Wait a bit, in hopes it avoids the hang */
  1950. udelay(10);
  1951. }
  1952. }
  1953. dma_unmap_sg(kdev, pages->sgl, pages->nents, PCI_DMA_BIDIRECTIONAL);
  1954. }
  1955. static void i915_gtt_color_adjust(const struct drm_mm_node *node,
  1956. unsigned long color,
  1957. u64 *start,
  1958. u64 *end)
  1959. {
  1960. if (node->allocated && node->color != color)
  1961. *start += I915_GTT_PAGE_SIZE;
  1962. /* Also leave a space between the unallocated reserved node after the
  1963. * GTT and any objects within the GTT, i.e. we use the color adjustment
  1964. * to insert a guard page to prevent prefetches crossing over the
  1965. * GTT boundary.
  1966. */
  1967. node = list_next_entry(node, node_list);
  1968. if (node->color != color)
  1969. *end -= I915_GTT_PAGE_SIZE;
  1970. }
  1971. int i915_gem_init_aliasing_ppgtt(struct drm_i915_private *i915)
  1972. {
  1973. struct i915_ggtt *ggtt = &i915->ggtt;
  1974. struct i915_hw_ppgtt *ppgtt;
  1975. int err;
  1976. ppgtt = i915_ppgtt_create(i915, ERR_PTR(-EPERM), "[alias]");
  1977. if (IS_ERR(ppgtt))
  1978. return PTR_ERR(ppgtt);
  1979. if (WARN_ON(ppgtt->base.total < ggtt->base.total)) {
  1980. err = -ENODEV;
  1981. goto err_ppgtt;
  1982. }
  1983. if (ppgtt->base.allocate_va_range) {
  1984. /* Note we only pre-allocate as far as the end of the global
  1985. * GTT. On 48b / 4-level page-tables, the difference is very,
  1986. * very significant! We have to preallocate as GVT/vgpu does
  1987. * not like the page directory disappearing.
  1988. */
  1989. err = ppgtt->base.allocate_va_range(&ppgtt->base,
  1990. 0, ggtt->base.total);
  1991. if (err)
  1992. goto err_ppgtt;
  1993. }
  1994. i915->mm.aliasing_ppgtt = ppgtt;
  1995. WARN_ON(ggtt->base.bind_vma != ggtt_bind_vma);
  1996. ggtt->base.bind_vma = aliasing_gtt_bind_vma;
  1997. WARN_ON(ggtt->base.unbind_vma != ggtt_unbind_vma);
  1998. ggtt->base.unbind_vma = aliasing_gtt_unbind_vma;
  1999. return 0;
  2000. err_ppgtt:
  2001. i915_ppgtt_put(ppgtt);
  2002. return err;
  2003. }
  2004. void i915_gem_fini_aliasing_ppgtt(struct drm_i915_private *i915)
  2005. {
  2006. struct i915_ggtt *ggtt = &i915->ggtt;
  2007. struct i915_hw_ppgtt *ppgtt;
  2008. ppgtt = fetch_and_zero(&i915->mm.aliasing_ppgtt);
  2009. if (!ppgtt)
  2010. return;
  2011. i915_ppgtt_put(ppgtt);
  2012. ggtt->base.bind_vma = ggtt_bind_vma;
  2013. ggtt->base.unbind_vma = ggtt_unbind_vma;
  2014. }
  2015. int i915_gem_init_ggtt(struct drm_i915_private *dev_priv)
  2016. {
  2017. /* Let GEM Manage all of the aperture.
  2018. *
  2019. * However, leave one page at the end still bound to the scratch page.
  2020. * There are a number of places where the hardware apparently prefetches
  2021. * past the end of the object, and we've seen multiple hangs with the
  2022. * GPU head pointer stuck in a batchbuffer bound at the last page of the
  2023. * aperture. One page should be enough to keep any prefetching inside
  2024. * of the aperture.
  2025. */
  2026. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2027. unsigned long hole_start, hole_end;
  2028. struct drm_mm_node *entry;
  2029. int ret;
  2030. ret = intel_vgt_balloon(dev_priv);
  2031. if (ret)
  2032. return ret;
  2033. /* Reserve a mappable slot for our lockless error capture */
  2034. ret = drm_mm_insert_node_in_range(&ggtt->base.mm, &ggtt->error_capture,
  2035. PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE,
  2036. 0, ggtt->mappable_end,
  2037. DRM_MM_INSERT_LOW);
  2038. if (ret)
  2039. return ret;
  2040. /* Clear any non-preallocated blocks */
  2041. drm_mm_for_each_hole(entry, &ggtt->base.mm, hole_start, hole_end) {
  2042. DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
  2043. hole_start, hole_end);
  2044. ggtt->base.clear_range(&ggtt->base, hole_start,
  2045. hole_end - hole_start);
  2046. }
  2047. /* And finally clear the reserved guard page */
  2048. ggtt->base.clear_range(&ggtt->base,
  2049. ggtt->base.total - PAGE_SIZE, PAGE_SIZE);
  2050. if (USES_PPGTT(dev_priv) && !USES_FULL_PPGTT(dev_priv)) {
  2051. ret = i915_gem_init_aliasing_ppgtt(dev_priv);
  2052. if (ret)
  2053. goto err;
  2054. }
  2055. return 0;
  2056. err:
  2057. drm_mm_remove_node(&ggtt->error_capture);
  2058. return ret;
  2059. }
  2060. /**
  2061. * i915_ggtt_cleanup_hw - Clean up GGTT hardware initialization
  2062. * @dev_priv: i915 device
  2063. */
  2064. void i915_ggtt_cleanup_hw(struct drm_i915_private *dev_priv)
  2065. {
  2066. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2067. struct i915_vma *vma, *vn;
  2068. ggtt->base.closed = true;
  2069. mutex_lock(&dev_priv->drm.struct_mutex);
  2070. WARN_ON(!list_empty(&ggtt->base.active_list));
  2071. list_for_each_entry_safe(vma, vn, &ggtt->base.inactive_list, vm_link)
  2072. WARN_ON(i915_vma_unbind(vma));
  2073. mutex_unlock(&dev_priv->drm.struct_mutex);
  2074. i915_gem_cleanup_stolen(&dev_priv->drm);
  2075. mutex_lock(&dev_priv->drm.struct_mutex);
  2076. i915_gem_fini_aliasing_ppgtt(dev_priv);
  2077. if (drm_mm_node_allocated(&ggtt->error_capture))
  2078. drm_mm_remove_node(&ggtt->error_capture);
  2079. if (drm_mm_initialized(&ggtt->base.mm)) {
  2080. intel_vgt_deballoon(dev_priv);
  2081. i915_address_space_fini(&ggtt->base);
  2082. }
  2083. ggtt->base.cleanup(&ggtt->base);
  2084. mutex_unlock(&dev_priv->drm.struct_mutex);
  2085. arch_phys_wc_del(ggtt->mtrr);
  2086. io_mapping_fini(&ggtt->mappable);
  2087. }
  2088. static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
  2089. {
  2090. snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
  2091. snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
  2092. return snb_gmch_ctl << 20;
  2093. }
  2094. static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
  2095. {
  2096. bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
  2097. bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
  2098. if (bdw_gmch_ctl)
  2099. bdw_gmch_ctl = 1 << bdw_gmch_ctl;
  2100. #ifdef CONFIG_X86_32
  2101. /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
  2102. if (bdw_gmch_ctl > 4)
  2103. bdw_gmch_ctl = 4;
  2104. #endif
  2105. return bdw_gmch_ctl << 20;
  2106. }
  2107. static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
  2108. {
  2109. gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
  2110. gmch_ctrl &= SNB_GMCH_GGMS_MASK;
  2111. if (gmch_ctrl)
  2112. return 1 << (20 + gmch_ctrl);
  2113. return 0;
  2114. }
  2115. static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
  2116. {
  2117. snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
  2118. snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
  2119. return snb_gmch_ctl << 25; /* 32 MB units */
  2120. }
  2121. static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
  2122. {
  2123. bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  2124. bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
  2125. return bdw_gmch_ctl << 25; /* 32 MB units */
  2126. }
  2127. static size_t chv_get_stolen_size(u16 gmch_ctrl)
  2128. {
  2129. gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
  2130. gmch_ctrl &= SNB_GMCH_GMS_MASK;
  2131. /*
  2132. * 0x0 to 0x10: 32MB increments starting at 0MB
  2133. * 0x11 to 0x16: 4MB increments starting at 8MB
  2134. * 0x17 to 0x1d: 4MB increments start at 36MB
  2135. */
  2136. if (gmch_ctrl < 0x11)
  2137. return gmch_ctrl << 25;
  2138. else if (gmch_ctrl < 0x17)
  2139. return (gmch_ctrl - 0x11 + 2) << 22;
  2140. else
  2141. return (gmch_ctrl - 0x17 + 9) << 22;
  2142. }
  2143. static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
  2144. {
  2145. gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  2146. gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
  2147. if (gen9_gmch_ctl < 0xf0)
  2148. return gen9_gmch_ctl << 25; /* 32 MB units */
  2149. else
  2150. /* 4MB increments starting at 0xf0 for 4MB */
  2151. return (gen9_gmch_ctl - 0xf0 + 1) << 22;
  2152. }
  2153. static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
  2154. {
  2155. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2156. struct pci_dev *pdev = dev_priv->drm.pdev;
  2157. phys_addr_t phys_addr;
  2158. int ret;
  2159. /* For Modern GENs the PTEs and register space are split in the BAR */
  2160. phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
  2161. /*
  2162. * On BXT writes larger than 64 bit to the GTT pagetable range will be
  2163. * dropped. For WC mappings in general we have 64 byte burst writes
  2164. * when the WC buffer is flushed, so we can't use it, but have to
  2165. * resort to an uncached mapping. The WC issue is easily caught by the
  2166. * readback check when writing GTT PTE entries.
  2167. */
  2168. if (IS_GEN9_LP(dev_priv))
  2169. ggtt->gsm = ioremap_nocache(phys_addr, size);
  2170. else
  2171. ggtt->gsm = ioremap_wc(phys_addr, size);
  2172. if (!ggtt->gsm) {
  2173. DRM_ERROR("Failed to map the ggtt page table\n");
  2174. return -ENOMEM;
  2175. }
  2176. ret = setup_scratch_page(&ggtt->base, GFP_DMA32);
  2177. if (ret) {
  2178. DRM_ERROR("Scratch setup failed\n");
  2179. /* iounmap will also get called at remove, but meh */
  2180. iounmap(ggtt->gsm);
  2181. return ret;
  2182. }
  2183. return 0;
  2184. }
  2185. /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
  2186. * bits. When using advanced contexts each context stores its own PAT, but
  2187. * writing this data shouldn't be harmful even in those cases. */
  2188. static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
  2189. {
  2190. u64 pat;
  2191. pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
  2192. GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
  2193. GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
  2194. GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
  2195. GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
  2196. GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
  2197. GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
  2198. GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
  2199. if (!USES_PPGTT(dev_priv))
  2200. /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
  2201. * so RTL will always use the value corresponding to
  2202. * pat_sel = 000".
  2203. * So let's disable cache for GGTT to avoid screen corruptions.
  2204. * MOCS still can be used though.
  2205. * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
  2206. * before this patch, i.e. the same uncached + snooping access
  2207. * like on gen6/7 seems to be in effect.
  2208. * - So this just fixes blitter/render access. Again it looks
  2209. * like it's not just uncached access, but uncached + snooping.
  2210. * So we can still hold onto all our assumptions wrt cpu
  2211. * clflushing on LLC machines.
  2212. */
  2213. pat = GEN8_PPAT(0, GEN8_PPAT_UC);
  2214. /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
  2215. * write would work. */
  2216. I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
  2217. I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
  2218. }
  2219. static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
  2220. {
  2221. u64 pat;
  2222. /*
  2223. * Map WB on BDW to snooped on CHV.
  2224. *
  2225. * Only the snoop bit has meaning for CHV, the rest is
  2226. * ignored.
  2227. *
  2228. * The hardware will never snoop for certain types of accesses:
  2229. * - CPU GTT (GMADR->GGTT->no snoop->memory)
  2230. * - PPGTT page tables
  2231. * - some other special cycles
  2232. *
  2233. * As with BDW, we also need to consider the following for GT accesses:
  2234. * "For GGTT, there is NO pat_sel[2:0] from the entry,
  2235. * so RTL will always use the value corresponding to
  2236. * pat_sel = 000".
  2237. * Which means we must set the snoop bit in PAT entry 0
  2238. * in order to keep the global status page working.
  2239. */
  2240. pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
  2241. GEN8_PPAT(1, 0) |
  2242. GEN8_PPAT(2, 0) |
  2243. GEN8_PPAT(3, 0) |
  2244. GEN8_PPAT(4, CHV_PPAT_SNOOP) |
  2245. GEN8_PPAT(5, CHV_PPAT_SNOOP) |
  2246. GEN8_PPAT(6, CHV_PPAT_SNOOP) |
  2247. GEN8_PPAT(7, CHV_PPAT_SNOOP);
  2248. I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
  2249. I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
  2250. }
  2251. static void gen6_gmch_remove(struct i915_address_space *vm)
  2252. {
  2253. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  2254. iounmap(ggtt->gsm);
  2255. cleanup_scratch_page(vm);
  2256. }
  2257. static int gen8_gmch_probe(struct i915_ggtt *ggtt)
  2258. {
  2259. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2260. struct pci_dev *pdev = dev_priv->drm.pdev;
  2261. unsigned int size;
  2262. u16 snb_gmch_ctl;
  2263. /* TODO: We're not aware of mappable constraints on gen8 yet */
  2264. ggtt->mappable_base = pci_resource_start(pdev, 2);
  2265. ggtt->mappable_end = pci_resource_len(pdev, 2);
  2266. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(39)))
  2267. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
  2268. pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  2269. if (INTEL_GEN(dev_priv) >= 9) {
  2270. ggtt->stolen_size = gen9_get_stolen_size(snb_gmch_ctl);
  2271. size = gen8_get_total_gtt_size(snb_gmch_ctl);
  2272. } else if (IS_CHERRYVIEW(dev_priv)) {
  2273. ggtt->stolen_size = chv_get_stolen_size(snb_gmch_ctl);
  2274. size = chv_get_total_gtt_size(snb_gmch_ctl);
  2275. } else {
  2276. ggtt->stolen_size = gen8_get_stolen_size(snb_gmch_ctl);
  2277. size = gen8_get_total_gtt_size(snb_gmch_ctl);
  2278. }
  2279. ggtt->base.total = (size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
  2280. if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
  2281. chv_setup_private_ppat(dev_priv);
  2282. else
  2283. bdw_setup_private_ppat(dev_priv);
  2284. ggtt->base.cleanup = gen6_gmch_remove;
  2285. ggtt->base.bind_vma = ggtt_bind_vma;
  2286. ggtt->base.unbind_vma = ggtt_unbind_vma;
  2287. ggtt->base.insert_page = gen8_ggtt_insert_page;
  2288. ggtt->base.clear_range = nop_clear_range;
  2289. if (!USES_FULL_PPGTT(dev_priv) || intel_scanout_needs_vtd_wa(dev_priv))
  2290. ggtt->base.clear_range = gen8_ggtt_clear_range;
  2291. ggtt->base.insert_entries = gen8_ggtt_insert_entries;
  2292. ggtt->invalidate = gen6_ggtt_invalidate;
  2293. return ggtt_probe_common(ggtt, size);
  2294. }
  2295. static int gen6_gmch_probe(struct i915_ggtt *ggtt)
  2296. {
  2297. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2298. struct pci_dev *pdev = dev_priv->drm.pdev;
  2299. unsigned int size;
  2300. u16 snb_gmch_ctl;
  2301. ggtt->mappable_base = pci_resource_start(pdev, 2);
  2302. ggtt->mappable_end = pci_resource_len(pdev, 2);
  2303. /* 64/512MB is the current min/max we actually know of, but this is just
  2304. * a coarse sanity check.
  2305. */
  2306. if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
  2307. DRM_ERROR("Unknown GMADR size (%llx)\n", ggtt->mappable_end);
  2308. return -ENXIO;
  2309. }
  2310. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(40)))
  2311. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
  2312. pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  2313. ggtt->stolen_size = gen6_get_stolen_size(snb_gmch_ctl);
  2314. size = gen6_get_total_gtt_size(snb_gmch_ctl);
  2315. ggtt->base.total = (size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
  2316. ggtt->base.clear_range = gen6_ggtt_clear_range;
  2317. ggtt->base.insert_page = gen6_ggtt_insert_page;
  2318. ggtt->base.insert_entries = gen6_ggtt_insert_entries;
  2319. ggtt->base.bind_vma = ggtt_bind_vma;
  2320. ggtt->base.unbind_vma = ggtt_unbind_vma;
  2321. ggtt->base.cleanup = gen6_gmch_remove;
  2322. ggtt->invalidate = gen6_ggtt_invalidate;
  2323. if (HAS_EDRAM(dev_priv))
  2324. ggtt->base.pte_encode = iris_pte_encode;
  2325. else if (IS_HASWELL(dev_priv))
  2326. ggtt->base.pte_encode = hsw_pte_encode;
  2327. else if (IS_VALLEYVIEW(dev_priv))
  2328. ggtt->base.pte_encode = byt_pte_encode;
  2329. else if (INTEL_GEN(dev_priv) >= 7)
  2330. ggtt->base.pte_encode = ivb_pte_encode;
  2331. else
  2332. ggtt->base.pte_encode = snb_pte_encode;
  2333. return ggtt_probe_common(ggtt, size);
  2334. }
  2335. static void i915_gmch_remove(struct i915_address_space *vm)
  2336. {
  2337. intel_gmch_remove();
  2338. }
  2339. static int i915_gmch_probe(struct i915_ggtt *ggtt)
  2340. {
  2341. struct drm_i915_private *dev_priv = ggtt->base.i915;
  2342. int ret;
  2343. ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->drm.pdev, NULL);
  2344. if (!ret) {
  2345. DRM_ERROR("failed to set up gmch\n");
  2346. return -EIO;
  2347. }
  2348. intel_gtt_get(&ggtt->base.total,
  2349. &ggtt->stolen_size,
  2350. &ggtt->mappable_base,
  2351. &ggtt->mappable_end);
  2352. ggtt->do_idle_maps = needs_idle_maps(dev_priv);
  2353. ggtt->base.insert_page = i915_ggtt_insert_page;
  2354. ggtt->base.insert_entries = i915_ggtt_insert_entries;
  2355. ggtt->base.clear_range = i915_ggtt_clear_range;
  2356. ggtt->base.bind_vma = ggtt_bind_vma;
  2357. ggtt->base.unbind_vma = ggtt_unbind_vma;
  2358. ggtt->base.cleanup = i915_gmch_remove;
  2359. ggtt->invalidate = gmch_ggtt_invalidate;
  2360. if (unlikely(ggtt->do_idle_maps))
  2361. DRM_INFO("applying Ironlake quirks for intel_iommu\n");
  2362. return 0;
  2363. }
  2364. /**
  2365. * i915_ggtt_probe_hw - Probe GGTT hardware location
  2366. * @dev_priv: i915 device
  2367. */
  2368. int i915_ggtt_probe_hw(struct drm_i915_private *dev_priv)
  2369. {
  2370. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2371. int ret;
  2372. ggtt->base.i915 = dev_priv;
  2373. ggtt->base.dma = &dev_priv->drm.pdev->dev;
  2374. if (INTEL_GEN(dev_priv) <= 5)
  2375. ret = i915_gmch_probe(ggtt);
  2376. else if (INTEL_GEN(dev_priv) < 8)
  2377. ret = gen6_gmch_probe(ggtt);
  2378. else
  2379. ret = gen8_gmch_probe(ggtt);
  2380. if (ret)
  2381. return ret;
  2382. /* Trim the GGTT to fit the GuC mappable upper range (when enabled).
  2383. * This is easier than doing range restriction on the fly, as we
  2384. * currently don't have any bits spare to pass in this upper
  2385. * restriction!
  2386. */
  2387. if (HAS_GUC(dev_priv) && i915.enable_guc_loading) {
  2388. ggtt->base.total = min_t(u64, ggtt->base.total, GUC_GGTT_TOP);
  2389. ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
  2390. }
  2391. if ((ggtt->base.total - 1) >> 32) {
  2392. DRM_ERROR("We never expected a Global GTT with more than 32bits"
  2393. " of address space! Found %lldM!\n",
  2394. ggtt->base.total >> 20);
  2395. ggtt->base.total = 1ULL << 32;
  2396. ggtt->mappable_end = min(ggtt->mappable_end, ggtt->base.total);
  2397. }
  2398. if (ggtt->mappable_end > ggtt->base.total) {
  2399. DRM_ERROR("mappable aperture extends past end of GGTT,"
  2400. " aperture=%llx, total=%llx\n",
  2401. ggtt->mappable_end, ggtt->base.total);
  2402. ggtt->mappable_end = ggtt->base.total;
  2403. }
  2404. /* GMADR is the PCI mmio aperture into the global GTT. */
  2405. DRM_INFO("Memory usable by graphics device = %lluM\n",
  2406. ggtt->base.total >> 20);
  2407. DRM_DEBUG_DRIVER("GMADR size = %lldM\n", ggtt->mappable_end >> 20);
  2408. DRM_DEBUG_DRIVER("GTT stolen size = %uM\n", ggtt->stolen_size >> 20);
  2409. #ifdef CONFIG_INTEL_IOMMU
  2410. if (intel_iommu_gfx_mapped)
  2411. DRM_INFO("VT-d active for gfx access\n");
  2412. #endif
  2413. return 0;
  2414. }
  2415. /**
  2416. * i915_ggtt_init_hw - Initialize GGTT hardware
  2417. * @dev_priv: i915 device
  2418. */
  2419. int i915_ggtt_init_hw(struct drm_i915_private *dev_priv)
  2420. {
  2421. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2422. int ret;
  2423. INIT_LIST_HEAD(&dev_priv->vm_list);
  2424. /* Note that we use page colouring to enforce a guard page at the
  2425. * end of the address space. This is required as the CS may prefetch
  2426. * beyond the end of the batch buffer, across the page boundary,
  2427. * and beyond the end of the GTT if we do not provide a guard.
  2428. */
  2429. mutex_lock(&dev_priv->drm.struct_mutex);
  2430. i915_address_space_init(&ggtt->base, dev_priv, "[global]");
  2431. if (!HAS_LLC(dev_priv) && !USES_PPGTT(dev_priv))
  2432. ggtt->base.mm.color_adjust = i915_gtt_color_adjust;
  2433. mutex_unlock(&dev_priv->drm.struct_mutex);
  2434. if (!io_mapping_init_wc(&dev_priv->ggtt.mappable,
  2435. dev_priv->ggtt.mappable_base,
  2436. dev_priv->ggtt.mappable_end)) {
  2437. ret = -EIO;
  2438. goto out_gtt_cleanup;
  2439. }
  2440. ggtt->mtrr = arch_phys_wc_add(ggtt->mappable_base, ggtt->mappable_end);
  2441. /*
  2442. * Initialise stolen early so that we may reserve preallocated
  2443. * objects for the BIOS to KMS transition.
  2444. */
  2445. ret = i915_gem_init_stolen(dev_priv);
  2446. if (ret)
  2447. goto out_gtt_cleanup;
  2448. return 0;
  2449. out_gtt_cleanup:
  2450. ggtt->base.cleanup(&ggtt->base);
  2451. return ret;
  2452. }
  2453. int i915_ggtt_enable_hw(struct drm_i915_private *dev_priv)
  2454. {
  2455. if (INTEL_GEN(dev_priv) < 6 && !intel_enable_gtt())
  2456. return -EIO;
  2457. return 0;
  2458. }
  2459. void i915_ggtt_enable_guc(struct drm_i915_private *i915)
  2460. {
  2461. i915->ggtt.invalidate = guc_ggtt_invalidate;
  2462. }
  2463. void i915_ggtt_disable_guc(struct drm_i915_private *i915)
  2464. {
  2465. i915->ggtt.invalidate = gen6_ggtt_invalidate;
  2466. }
  2467. void i915_gem_restore_gtt_mappings(struct drm_i915_private *dev_priv)
  2468. {
  2469. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2470. struct drm_i915_gem_object *obj, *on;
  2471. i915_check_and_clear_faults(dev_priv);
  2472. /* First fill our portion of the GTT with scratch pages */
  2473. ggtt->base.clear_range(&ggtt->base, 0, ggtt->base.total);
  2474. ggtt->base.closed = true; /* skip rewriting PTE on VMA unbind */
  2475. /* clflush objects bound into the GGTT and rebind them. */
  2476. list_for_each_entry_safe(obj, on,
  2477. &dev_priv->mm.bound_list, global_link) {
  2478. bool ggtt_bound = false;
  2479. struct i915_vma *vma;
  2480. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  2481. if (vma->vm != &ggtt->base)
  2482. continue;
  2483. if (!i915_vma_unbind(vma))
  2484. continue;
  2485. WARN_ON(i915_vma_bind(vma, obj->cache_level,
  2486. PIN_UPDATE));
  2487. ggtt_bound = true;
  2488. }
  2489. if (ggtt_bound)
  2490. WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
  2491. }
  2492. ggtt->base.closed = false;
  2493. if (INTEL_GEN(dev_priv) >= 8) {
  2494. if (IS_CHERRYVIEW(dev_priv) || IS_GEN9_LP(dev_priv))
  2495. chv_setup_private_ppat(dev_priv);
  2496. else
  2497. bdw_setup_private_ppat(dev_priv);
  2498. return;
  2499. }
  2500. if (USES_PPGTT(dev_priv)) {
  2501. struct i915_address_space *vm;
  2502. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  2503. struct i915_hw_ppgtt *ppgtt;
  2504. if (i915_is_ggtt(vm))
  2505. ppgtt = dev_priv->mm.aliasing_ppgtt;
  2506. else
  2507. ppgtt = i915_vm_to_ppgtt(vm);
  2508. gen6_write_page_range(ppgtt, 0, ppgtt->base.total);
  2509. }
  2510. }
  2511. i915_ggtt_invalidate(dev_priv);
  2512. }
  2513. static struct scatterlist *
  2514. rotate_pages(const dma_addr_t *in, unsigned int offset,
  2515. unsigned int width, unsigned int height,
  2516. unsigned int stride,
  2517. struct sg_table *st, struct scatterlist *sg)
  2518. {
  2519. unsigned int column, row;
  2520. unsigned int src_idx;
  2521. for (column = 0; column < width; column++) {
  2522. src_idx = stride * (height - 1) + column;
  2523. for (row = 0; row < height; row++) {
  2524. st->nents++;
  2525. /* We don't need the pages, but need to initialize
  2526. * the entries so the sg list can be happily traversed.
  2527. * The only thing we need are DMA addresses.
  2528. */
  2529. sg_set_page(sg, NULL, PAGE_SIZE, 0);
  2530. sg_dma_address(sg) = in[offset + src_idx];
  2531. sg_dma_len(sg) = PAGE_SIZE;
  2532. sg = sg_next(sg);
  2533. src_idx -= stride;
  2534. }
  2535. }
  2536. return sg;
  2537. }
  2538. static noinline struct sg_table *
  2539. intel_rotate_pages(struct intel_rotation_info *rot_info,
  2540. struct drm_i915_gem_object *obj)
  2541. {
  2542. const unsigned long n_pages = obj->base.size / PAGE_SIZE;
  2543. unsigned int size = intel_rotation_info_size(rot_info);
  2544. struct sgt_iter sgt_iter;
  2545. dma_addr_t dma_addr;
  2546. unsigned long i;
  2547. dma_addr_t *page_addr_list;
  2548. struct sg_table *st;
  2549. struct scatterlist *sg;
  2550. int ret = -ENOMEM;
  2551. /* Allocate a temporary list of source pages for random access. */
  2552. page_addr_list = drm_malloc_gfp(n_pages,
  2553. sizeof(dma_addr_t),
  2554. GFP_TEMPORARY);
  2555. if (!page_addr_list)
  2556. return ERR_PTR(ret);
  2557. /* Allocate target SG list. */
  2558. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2559. if (!st)
  2560. goto err_st_alloc;
  2561. ret = sg_alloc_table(st, size, GFP_KERNEL);
  2562. if (ret)
  2563. goto err_sg_alloc;
  2564. /* Populate source page list from the object. */
  2565. i = 0;
  2566. for_each_sgt_dma(dma_addr, sgt_iter, obj->mm.pages)
  2567. page_addr_list[i++] = dma_addr;
  2568. GEM_BUG_ON(i != n_pages);
  2569. st->nents = 0;
  2570. sg = st->sgl;
  2571. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
  2572. sg = rotate_pages(page_addr_list, rot_info->plane[i].offset,
  2573. rot_info->plane[i].width, rot_info->plane[i].height,
  2574. rot_info->plane[i].stride, st, sg);
  2575. }
  2576. DRM_DEBUG_KMS("Created rotated page mapping for object size %zu (%ux%u tiles, %u pages)\n",
  2577. obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
  2578. drm_free_large(page_addr_list);
  2579. return st;
  2580. err_sg_alloc:
  2581. kfree(st);
  2582. err_st_alloc:
  2583. drm_free_large(page_addr_list);
  2584. DRM_DEBUG_KMS("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
  2585. obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
  2586. return ERR_PTR(ret);
  2587. }
  2588. static noinline struct sg_table *
  2589. intel_partial_pages(const struct i915_ggtt_view *view,
  2590. struct drm_i915_gem_object *obj)
  2591. {
  2592. struct sg_table *st;
  2593. struct scatterlist *sg, *iter;
  2594. unsigned int count = view->partial.size;
  2595. unsigned int offset;
  2596. int ret = -ENOMEM;
  2597. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2598. if (!st)
  2599. goto err_st_alloc;
  2600. ret = sg_alloc_table(st, count, GFP_KERNEL);
  2601. if (ret)
  2602. goto err_sg_alloc;
  2603. iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
  2604. GEM_BUG_ON(!iter);
  2605. sg = st->sgl;
  2606. st->nents = 0;
  2607. do {
  2608. unsigned int len;
  2609. len = min(iter->length - (offset << PAGE_SHIFT),
  2610. count << PAGE_SHIFT);
  2611. sg_set_page(sg, NULL, len, 0);
  2612. sg_dma_address(sg) =
  2613. sg_dma_address(iter) + (offset << PAGE_SHIFT);
  2614. sg_dma_len(sg) = len;
  2615. st->nents++;
  2616. count -= len >> PAGE_SHIFT;
  2617. if (count == 0) {
  2618. sg_mark_end(sg);
  2619. return st;
  2620. }
  2621. sg = __sg_next(sg);
  2622. iter = __sg_next(iter);
  2623. offset = 0;
  2624. } while (1);
  2625. err_sg_alloc:
  2626. kfree(st);
  2627. err_st_alloc:
  2628. return ERR_PTR(ret);
  2629. }
  2630. static int
  2631. i915_get_ggtt_vma_pages(struct i915_vma *vma)
  2632. {
  2633. int ret;
  2634. /* The vma->pages are only valid within the lifespan of the borrowed
  2635. * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
  2636. * must be the vma->pages. A simple rule is that vma->pages must only
  2637. * be accessed when the obj->mm.pages are pinned.
  2638. */
  2639. GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
  2640. switch (vma->ggtt_view.type) {
  2641. case I915_GGTT_VIEW_NORMAL:
  2642. vma->pages = vma->obj->mm.pages;
  2643. return 0;
  2644. case I915_GGTT_VIEW_ROTATED:
  2645. vma->pages =
  2646. intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
  2647. break;
  2648. case I915_GGTT_VIEW_PARTIAL:
  2649. vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
  2650. break;
  2651. default:
  2652. WARN_ONCE(1, "GGTT view %u not implemented!\n",
  2653. vma->ggtt_view.type);
  2654. return -EINVAL;
  2655. }
  2656. ret = 0;
  2657. if (unlikely(IS_ERR(vma->pages))) {
  2658. ret = PTR_ERR(vma->pages);
  2659. vma->pages = NULL;
  2660. DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
  2661. vma->ggtt_view.type, ret);
  2662. }
  2663. return ret;
  2664. }
  2665. /**
  2666. * i915_gem_gtt_reserve - reserve a node in an address_space (GTT)
  2667. * @vm: the &struct i915_address_space
  2668. * @node: the &struct drm_mm_node (typically i915_vma.mode)
  2669. * @size: how much space to allocate inside the GTT,
  2670. * must be #I915_GTT_PAGE_SIZE aligned
  2671. * @offset: where to insert inside the GTT,
  2672. * must be #I915_GTT_MIN_ALIGNMENT aligned, and the node
  2673. * (@offset + @size) must fit within the address space
  2674. * @color: color to apply to node, if this node is not from a VMA,
  2675. * color must be #I915_COLOR_UNEVICTABLE
  2676. * @flags: control search and eviction behaviour
  2677. *
  2678. * i915_gem_gtt_reserve() tries to insert the @node at the exact @offset inside
  2679. * the address space (using @size and @color). If the @node does not fit, it
  2680. * tries to evict any overlapping nodes from the GTT, including any
  2681. * neighbouring nodes if the colors do not match (to ensure guard pages between
  2682. * differing domains). See i915_gem_evict_for_node() for the gory details
  2683. * on the eviction algorithm. #PIN_NONBLOCK may used to prevent waiting on
  2684. * evicting active overlapping objects, and any overlapping node that is pinned
  2685. * or marked as unevictable will also result in failure.
  2686. *
  2687. * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
  2688. * asked to wait for eviction and interrupted.
  2689. */
  2690. int i915_gem_gtt_reserve(struct i915_address_space *vm,
  2691. struct drm_mm_node *node,
  2692. u64 size, u64 offset, unsigned long color,
  2693. unsigned int flags)
  2694. {
  2695. int err;
  2696. GEM_BUG_ON(!size);
  2697. GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
  2698. GEM_BUG_ON(!IS_ALIGNED(offset, I915_GTT_MIN_ALIGNMENT));
  2699. GEM_BUG_ON(range_overflows(offset, size, vm->total));
  2700. GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
  2701. GEM_BUG_ON(drm_mm_node_allocated(node));
  2702. node->size = size;
  2703. node->start = offset;
  2704. node->color = color;
  2705. err = drm_mm_reserve_node(&vm->mm, node);
  2706. if (err != -ENOSPC)
  2707. return err;
  2708. err = i915_gem_evict_for_node(vm, node, flags);
  2709. if (err == 0)
  2710. err = drm_mm_reserve_node(&vm->mm, node);
  2711. return err;
  2712. }
  2713. static u64 random_offset(u64 start, u64 end, u64 len, u64 align)
  2714. {
  2715. u64 range, addr;
  2716. GEM_BUG_ON(range_overflows(start, len, end));
  2717. GEM_BUG_ON(round_up(start, align) > round_down(end - len, align));
  2718. range = round_down(end - len, align) - round_up(start, align);
  2719. if (range) {
  2720. if (sizeof(unsigned long) == sizeof(u64)) {
  2721. addr = get_random_long();
  2722. } else {
  2723. addr = get_random_int();
  2724. if (range > U32_MAX) {
  2725. addr <<= 32;
  2726. addr |= get_random_int();
  2727. }
  2728. }
  2729. div64_u64_rem(addr, range, &addr);
  2730. start += addr;
  2731. }
  2732. return round_up(start, align);
  2733. }
  2734. /**
  2735. * i915_gem_gtt_insert - insert a node into an address_space (GTT)
  2736. * @vm: the &struct i915_address_space
  2737. * @node: the &struct drm_mm_node (typically i915_vma.node)
  2738. * @size: how much space to allocate inside the GTT,
  2739. * must be #I915_GTT_PAGE_SIZE aligned
  2740. * @alignment: required alignment of starting offset, may be 0 but
  2741. * if specified, this must be a power-of-two and at least
  2742. * #I915_GTT_MIN_ALIGNMENT
  2743. * @color: color to apply to node
  2744. * @start: start of any range restriction inside GTT (0 for all),
  2745. * must be #I915_GTT_PAGE_SIZE aligned
  2746. * @end: end of any range restriction inside GTT (U64_MAX for all),
  2747. * must be #I915_GTT_PAGE_SIZE aligned if not U64_MAX
  2748. * @flags: control search and eviction behaviour
  2749. *
  2750. * i915_gem_gtt_insert() first searches for an available hole into which
  2751. * is can insert the node. The hole address is aligned to @alignment and
  2752. * its @size must then fit entirely within the [@start, @end] bounds. The
  2753. * nodes on either side of the hole must match @color, or else a guard page
  2754. * will be inserted between the two nodes (or the node evicted). If no
  2755. * suitable hole is found, first a victim is randomly selected and tested
  2756. * for eviction, otherwise then the LRU list of objects within the GTT
  2757. * is scanned to find the first set of replacement nodes to create the hole.
  2758. * Those old overlapping nodes are evicted from the GTT (and so must be
  2759. * rebound before any future use). Any node that is currently pinned cannot
  2760. * be evicted (see i915_vma_pin()). Similar if the node's VMA is currently
  2761. * active and #PIN_NONBLOCK is specified, that node is also skipped when
  2762. * searching for an eviction candidate. See i915_gem_evict_something() for
  2763. * the gory details on the eviction algorithm.
  2764. *
  2765. * Returns: 0 on success, -ENOSPC if no suitable hole is found, -EINTR if
  2766. * asked to wait for eviction and interrupted.
  2767. */
  2768. int i915_gem_gtt_insert(struct i915_address_space *vm,
  2769. struct drm_mm_node *node,
  2770. u64 size, u64 alignment, unsigned long color,
  2771. u64 start, u64 end, unsigned int flags)
  2772. {
  2773. enum drm_mm_insert_mode mode;
  2774. u64 offset;
  2775. int err;
  2776. lockdep_assert_held(&vm->i915->drm.struct_mutex);
  2777. GEM_BUG_ON(!size);
  2778. GEM_BUG_ON(!IS_ALIGNED(size, I915_GTT_PAGE_SIZE));
  2779. GEM_BUG_ON(alignment && !is_power_of_2(alignment));
  2780. GEM_BUG_ON(alignment && !IS_ALIGNED(alignment, I915_GTT_MIN_ALIGNMENT));
  2781. GEM_BUG_ON(start >= end);
  2782. GEM_BUG_ON(start > 0 && !IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
  2783. GEM_BUG_ON(end < U64_MAX && !IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
  2784. GEM_BUG_ON(vm == &vm->i915->mm.aliasing_ppgtt->base);
  2785. GEM_BUG_ON(drm_mm_node_allocated(node));
  2786. if (unlikely(range_overflows(start, size, end)))
  2787. return -ENOSPC;
  2788. if (unlikely(round_up(start, alignment) > round_down(end - size, alignment)))
  2789. return -ENOSPC;
  2790. mode = DRM_MM_INSERT_BEST;
  2791. if (flags & PIN_HIGH)
  2792. mode = DRM_MM_INSERT_HIGH;
  2793. if (flags & PIN_MAPPABLE)
  2794. mode = DRM_MM_INSERT_LOW;
  2795. /* We only allocate in PAGE_SIZE/GTT_PAGE_SIZE (4096) chunks,
  2796. * so we know that we always have a minimum alignment of 4096.
  2797. * The drm_mm range manager is optimised to return results
  2798. * with zero alignment, so where possible use the optimal
  2799. * path.
  2800. */
  2801. BUILD_BUG_ON(I915_GTT_MIN_ALIGNMENT > I915_GTT_PAGE_SIZE);
  2802. if (alignment <= I915_GTT_MIN_ALIGNMENT)
  2803. alignment = 0;
  2804. err = drm_mm_insert_node_in_range(&vm->mm, node,
  2805. size, alignment, color,
  2806. start, end, mode);
  2807. if (err != -ENOSPC)
  2808. return err;
  2809. /* No free space, pick a slot at random.
  2810. *
  2811. * There is a pathological case here using a GTT shared between
  2812. * mmap and GPU (i.e. ggtt/aliasing_ppgtt but not full-ppgtt):
  2813. *
  2814. * |<-- 256 MiB aperture -->||<-- 1792 MiB unmappable -->|
  2815. * (64k objects) (448k objects)
  2816. *
  2817. * Now imagine that the eviction LRU is ordered top-down (just because
  2818. * pathology meets real life), and that we need to evict an object to
  2819. * make room inside the aperture. The eviction scan then has to walk
  2820. * the 448k list before it finds one within range. And now imagine that
  2821. * it has to search for a new hole between every byte inside the memcpy,
  2822. * for several simultaneous clients.
  2823. *
  2824. * On a full-ppgtt system, if we have run out of available space, there
  2825. * will be lots and lots of objects in the eviction list! Again,
  2826. * searching that LRU list may be slow if we are also applying any
  2827. * range restrictions (e.g. restriction to low 4GiB) and so, for
  2828. * simplicity and similarilty between different GTT, try the single
  2829. * random replacement first.
  2830. */
  2831. offset = random_offset(start, end,
  2832. size, alignment ?: I915_GTT_MIN_ALIGNMENT);
  2833. err = i915_gem_gtt_reserve(vm, node, size, offset, color, flags);
  2834. if (err != -ENOSPC)
  2835. return err;
  2836. /* Randomly selected placement is pinned, do a search */
  2837. err = i915_gem_evict_something(vm, size, alignment, color,
  2838. start, end, flags);
  2839. if (err)
  2840. return err;
  2841. return drm_mm_insert_node_in_range(&vm->mm, node,
  2842. size, alignment, color,
  2843. start, end, DRM_MM_INSERT_EVICT);
  2844. }
  2845. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  2846. #include "selftests/mock_gtt.c"
  2847. #include "selftests/i915_gem_gtt.c"
  2848. #endif