i915_gem_gtt.c 106 KB

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