i915_gem_gtt.c 105 KB

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