i915_gem_gtt.c 94 KB

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