i915_gem_gtt.c 93 KB

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