i915_gem_gtt.c 93 KB

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