i915_gem_gtt.c 94 KB

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