i915_gem_gtt.c 105 KB

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