i915_gem_gtt.c 90 KB

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