i915_gem_gtt.c 94 KB

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