i915_gem_gtt.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759
  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 <drm/drmP.h>
  27. #include <drm/i915_drm.h>
  28. #include "i915_drv.h"
  29. #include "i915_vgpu.h"
  30. #include "i915_trace.h"
  31. #include "intel_drv.h"
  32. /**
  33. * DOC: Global GTT views
  34. *
  35. * Background and previous state
  36. *
  37. * Historically objects could exists (be bound) in global GTT space only as
  38. * singular instances with a view representing all of the object's backing pages
  39. * in a linear fashion. This view will be called a normal view.
  40. *
  41. * To support multiple views of the same object, where the number of mapped
  42. * pages is not equal to the backing store, or where the layout of the pages
  43. * is not linear, concept of a GGTT view was added.
  44. *
  45. * One example of an alternative view is a stereo display driven by a single
  46. * image. In this case we would have a framebuffer looking like this
  47. * (2x2 pages):
  48. *
  49. * 12
  50. * 34
  51. *
  52. * Above would represent a normal GGTT view as normally mapped for GPU or CPU
  53. * rendering. In contrast, fed to the display engine would be an alternative
  54. * view which could look something like this:
  55. *
  56. * 1212
  57. * 3434
  58. *
  59. * In this example both the size and layout of pages in the alternative view is
  60. * different from the normal view.
  61. *
  62. * Implementation and usage
  63. *
  64. * GGTT views are implemented using VMAs and are distinguished via enum
  65. * i915_ggtt_view_type and struct i915_ggtt_view.
  66. *
  67. * A new flavour of core GEM functions which work with GGTT bound objects were
  68. * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
  69. * renaming in large amounts of code. They take the struct i915_ggtt_view
  70. * parameter encapsulating all metadata required to implement a view.
  71. *
  72. * As a helper for callers which are only interested in the normal view,
  73. * globally const i915_ggtt_view_normal singleton instance exists. All old core
  74. * GEM API functions, the ones not taking the view parameter, are operating on,
  75. * or with the normal GGTT view.
  76. *
  77. * Code wanting to add or use a new GGTT view needs to:
  78. *
  79. * 1. Add a new enum with a suitable name.
  80. * 2. Extend the metadata in the i915_ggtt_view structure if required.
  81. * 3. Add support to i915_get_vma_pages().
  82. *
  83. * New views are required to build a scatter-gather table from within the
  84. * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
  85. * exists for the lifetime of an VMA.
  86. *
  87. * Core API is designed to have copy semantics which means that passed in
  88. * struct i915_ggtt_view does not need to be persistent (left around after
  89. * calling the core API functions).
  90. *
  91. */
  92. const struct i915_ggtt_view i915_ggtt_view_normal;
  93. const struct i915_ggtt_view i915_ggtt_view_rotated = {
  94. .type = I915_GGTT_VIEW_ROTATED
  95. };
  96. static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv);
  97. static void chv_setup_private_ppat(struct drm_i915_private *dev_priv);
  98. static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
  99. {
  100. bool has_aliasing_ppgtt;
  101. bool has_full_ppgtt;
  102. has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
  103. has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
  104. if (intel_vgpu_active(dev))
  105. has_full_ppgtt = false; /* emulation is too hard */
  106. /*
  107. * We don't allow disabling PPGTT for gen9+ as it's a requirement for
  108. * execlists, the sole mechanism available to submit work.
  109. */
  110. if (INTEL_INFO(dev)->gen < 9 &&
  111. (enable_ppgtt == 0 || !has_aliasing_ppgtt))
  112. return 0;
  113. if (enable_ppgtt == 1)
  114. return 1;
  115. if (enable_ppgtt == 2 && has_full_ppgtt)
  116. return 2;
  117. #ifdef CONFIG_INTEL_IOMMU
  118. /* Disable ppgtt on SNB if VT-d is on. */
  119. if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
  120. DRM_INFO("Disabling PPGTT because VT-d is on\n");
  121. return 0;
  122. }
  123. #endif
  124. /* Early VLV doesn't have this */
  125. if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  126. dev->pdev->revision < 0xb) {
  127. DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
  128. return 0;
  129. }
  130. if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
  131. return 2;
  132. else
  133. return has_aliasing_ppgtt ? 1 : 0;
  134. }
  135. static void ppgtt_bind_vma(struct i915_vma *vma,
  136. enum i915_cache_level cache_level,
  137. u32 flags);
  138. static void ppgtt_unbind_vma(struct i915_vma *vma);
  139. static inline gen8_pte_t gen8_pte_encode(dma_addr_t addr,
  140. enum i915_cache_level level,
  141. bool valid)
  142. {
  143. gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
  144. pte |= addr;
  145. switch (level) {
  146. case I915_CACHE_NONE:
  147. pte |= PPAT_UNCACHED_INDEX;
  148. break;
  149. case I915_CACHE_WT:
  150. pte |= PPAT_DISPLAY_ELLC_INDEX;
  151. break;
  152. default:
  153. pte |= PPAT_CACHED_INDEX;
  154. break;
  155. }
  156. return pte;
  157. }
  158. static inline gen8_pde_t gen8_pde_encode(struct drm_device *dev,
  159. dma_addr_t addr,
  160. enum i915_cache_level level)
  161. {
  162. gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
  163. pde |= addr;
  164. if (level != I915_CACHE_NONE)
  165. pde |= PPAT_CACHED_PDE_INDEX;
  166. else
  167. pde |= PPAT_UNCACHED_INDEX;
  168. return pde;
  169. }
  170. static gen6_pte_t snb_pte_encode(dma_addr_t addr,
  171. enum i915_cache_level level,
  172. bool valid, u32 unused)
  173. {
  174. gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  175. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  176. switch (level) {
  177. case I915_CACHE_L3_LLC:
  178. case I915_CACHE_LLC:
  179. pte |= GEN6_PTE_CACHE_LLC;
  180. break;
  181. case I915_CACHE_NONE:
  182. pte |= GEN6_PTE_UNCACHED;
  183. break;
  184. default:
  185. MISSING_CASE(level);
  186. }
  187. return pte;
  188. }
  189. static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
  190. enum i915_cache_level level,
  191. bool valid, u32 unused)
  192. {
  193. gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  194. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  195. switch (level) {
  196. case I915_CACHE_L3_LLC:
  197. pte |= GEN7_PTE_CACHE_L3_LLC;
  198. break;
  199. case I915_CACHE_LLC:
  200. pte |= GEN6_PTE_CACHE_LLC;
  201. break;
  202. case I915_CACHE_NONE:
  203. pte |= GEN6_PTE_UNCACHED;
  204. break;
  205. default:
  206. MISSING_CASE(level);
  207. }
  208. return pte;
  209. }
  210. static gen6_pte_t byt_pte_encode(dma_addr_t addr,
  211. enum i915_cache_level level,
  212. bool valid, u32 flags)
  213. {
  214. gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  215. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  216. if (!(flags & PTE_READ_ONLY))
  217. pte |= BYT_PTE_WRITEABLE;
  218. if (level != I915_CACHE_NONE)
  219. pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
  220. return pte;
  221. }
  222. static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
  223. enum i915_cache_level level,
  224. bool valid, u32 unused)
  225. {
  226. gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  227. pte |= HSW_PTE_ADDR_ENCODE(addr);
  228. if (level != I915_CACHE_NONE)
  229. pte |= HSW_WB_LLC_AGE3;
  230. return pte;
  231. }
  232. static gen6_pte_t iris_pte_encode(dma_addr_t addr,
  233. enum i915_cache_level level,
  234. bool valid, u32 unused)
  235. {
  236. gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  237. pte |= HSW_PTE_ADDR_ENCODE(addr);
  238. switch (level) {
  239. case I915_CACHE_NONE:
  240. break;
  241. case I915_CACHE_WT:
  242. pte |= HSW_WT_ELLC_LLC_AGE3;
  243. break;
  244. default:
  245. pte |= HSW_WB_ELLC_LLC_AGE3;
  246. break;
  247. }
  248. return pte;
  249. }
  250. #define i915_dma_unmap_single(px, dev) \
  251. __i915_dma_unmap_single((px)->daddr, dev)
  252. static inline void __i915_dma_unmap_single(dma_addr_t daddr,
  253. struct drm_device *dev)
  254. {
  255. struct device *device = &dev->pdev->dev;
  256. dma_unmap_page(device, daddr, 4096, PCI_DMA_BIDIRECTIONAL);
  257. }
  258. /**
  259. * i915_dma_map_single() - Create a dma mapping for a page table/dir/etc.
  260. * @px: Page table/dir/etc to get a DMA map for
  261. * @dev: drm device
  262. *
  263. * Page table allocations are unified across all gens. They always require a
  264. * single 4k allocation, as well as a DMA mapping. If we keep the structs
  265. * symmetric here, the simple macro covers us for every page table type.
  266. *
  267. * Return: 0 if success.
  268. */
  269. #define i915_dma_map_single(px, dev) \
  270. i915_dma_map_page_single((px)->page, (dev), &(px)->daddr)
  271. static inline int i915_dma_map_page_single(struct page *page,
  272. struct drm_device *dev,
  273. dma_addr_t *daddr)
  274. {
  275. struct device *device = &dev->pdev->dev;
  276. *daddr = dma_map_page(device, page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
  277. if (dma_mapping_error(device, *daddr))
  278. return -ENOMEM;
  279. return 0;
  280. }
  281. static void unmap_and_free_pt(struct i915_page_table_entry *pt,
  282. struct drm_device *dev)
  283. {
  284. if (WARN_ON(!pt->page))
  285. return;
  286. i915_dma_unmap_single(pt, dev);
  287. __free_page(pt->page);
  288. kfree(pt->used_ptes);
  289. kfree(pt);
  290. }
  291. static struct i915_page_table_entry *alloc_pt_single(struct drm_device *dev)
  292. {
  293. struct i915_page_table_entry *pt;
  294. const size_t count = INTEL_INFO(dev)->gen >= 8 ?
  295. GEN8_PTES : GEN6_PTES;
  296. int ret = -ENOMEM;
  297. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  298. if (!pt)
  299. return ERR_PTR(-ENOMEM);
  300. pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
  301. GFP_KERNEL);
  302. if (!pt->used_ptes)
  303. goto fail_bitmap;
  304. pt->page = alloc_page(GFP_KERNEL);
  305. if (!pt->page)
  306. goto fail_page;
  307. ret = i915_dma_map_single(pt, dev);
  308. if (ret)
  309. goto fail_dma;
  310. return pt;
  311. fail_dma:
  312. __free_page(pt->page);
  313. fail_page:
  314. kfree(pt->used_ptes);
  315. fail_bitmap:
  316. kfree(pt);
  317. return ERR_PTR(ret);
  318. }
  319. /**
  320. * alloc_pt_range() - Allocate a multiple page tables
  321. * @pd: The page directory which will have at least @count entries
  322. * available to point to the allocated page tables.
  323. * @pde: First page directory entry for which we are allocating.
  324. * @count: Number of pages to allocate.
  325. * @dev: DRM device.
  326. *
  327. * Allocates multiple page table pages and sets the appropriate entries in the
  328. * page table structure within the page directory. Function cleans up after
  329. * itself on any failures.
  330. *
  331. * Return: 0 if allocation succeeded.
  332. */
  333. static int alloc_pt_range(struct i915_page_directory_entry *pd, uint16_t pde, size_t count,
  334. struct drm_device *dev)
  335. {
  336. int i, ret;
  337. /* 512 is the max page tables per page_directory on any platform. */
  338. if (WARN_ON(pde + count > I915_PDES))
  339. return -EINVAL;
  340. for (i = pde; i < pde + count; i++) {
  341. struct i915_page_table_entry *pt = alloc_pt_single(dev);
  342. if (IS_ERR(pt)) {
  343. ret = PTR_ERR(pt);
  344. goto err_out;
  345. }
  346. WARN(pd->page_table[i],
  347. "Leaking page directory entry %d (%p)\n",
  348. i, pd->page_table[i]);
  349. pd->page_table[i] = pt;
  350. }
  351. return 0;
  352. err_out:
  353. while (i-- > pde)
  354. unmap_and_free_pt(pd->page_table[i], dev);
  355. return ret;
  356. }
  357. static void unmap_and_free_pd(struct i915_page_directory_entry *pd)
  358. {
  359. if (pd->page) {
  360. __free_page(pd->page);
  361. kfree(pd);
  362. }
  363. }
  364. static struct i915_page_directory_entry *alloc_pd_single(void)
  365. {
  366. struct i915_page_directory_entry *pd;
  367. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  368. if (!pd)
  369. return ERR_PTR(-ENOMEM);
  370. pd->page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  371. if (!pd->page) {
  372. kfree(pd);
  373. return ERR_PTR(-ENOMEM);
  374. }
  375. return pd;
  376. }
  377. /* Broadwell Page Directory Pointer Descriptors */
  378. static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry,
  379. uint64_t val)
  380. {
  381. int ret;
  382. BUG_ON(entry >= 4);
  383. ret = intel_ring_begin(ring, 6);
  384. if (ret)
  385. return ret;
  386. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  387. intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
  388. intel_ring_emit(ring, (u32)(val >> 32));
  389. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  390. intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
  391. intel_ring_emit(ring, (u32)(val));
  392. intel_ring_advance(ring);
  393. return 0;
  394. }
  395. static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
  396. struct intel_engine_cs *ring)
  397. {
  398. int i, ret;
  399. /* bit of a hack to find the actual last used pd */
  400. int used_pd = ppgtt->num_pd_entries / I915_PDES;
  401. for (i = used_pd - 1; i >= 0; i--) {
  402. dma_addr_t addr = ppgtt->pdp.page_directory[i]->daddr;
  403. ret = gen8_write_pdp(ring, i, addr);
  404. if (ret)
  405. return ret;
  406. }
  407. return 0;
  408. }
  409. static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
  410. uint64_t start,
  411. uint64_t length,
  412. bool use_scratch)
  413. {
  414. struct i915_hw_ppgtt *ppgtt =
  415. container_of(vm, struct i915_hw_ppgtt, base);
  416. gen8_pte_t *pt_vaddr, scratch_pte;
  417. unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
  418. unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
  419. unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
  420. unsigned num_entries = length >> PAGE_SHIFT;
  421. unsigned last_pte, i;
  422. scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
  423. I915_CACHE_LLC, use_scratch);
  424. while (num_entries) {
  425. struct i915_page_directory_entry *pd;
  426. struct i915_page_table_entry *pt;
  427. struct page *page_table;
  428. if (WARN_ON(!ppgtt->pdp.page_directory[pdpe]))
  429. continue;
  430. pd = ppgtt->pdp.page_directory[pdpe];
  431. if (WARN_ON(!pd->page_table[pde]))
  432. continue;
  433. pt = pd->page_table[pde];
  434. if (WARN_ON(!pt->page))
  435. continue;
  436. page_table = pt->page;
  437. last_pte = pte + num_entries;
  438. if (last_pte > GEN8_PTES)
  439. last_pte = GEN8_PTES;
  440. pt_vaddr = kmap_atomic(page_table);
  441. for (i = pte; i < last_pte; i++) {
  442. pt_vaddr[i] = scratch_pte;
  443. num_entries--;
  444. }
  445. if (!HAS_LLC(ppgtt->base.dev))
  446. drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
  447. kunmap_atomic(pt_vaddr);
  448. pte = 0;
  449. if (++pde == I915_PDES) {
  450. pdpe++;
  451. pde = 0;
  452. }
  453. }
  454. }
  455. static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
  456. struct sg_table *pages,
  457. uint64_t start,
  458. enum i915_cache_level cache_level, u32 unused)
  459. {
  460. struct i915_hw_ppgtt *ppgtt =
  461. container_of(vm, struct i915_hw_ppgtt, base);
  462. gen8_pte_t *pt_vaddr;
  463. unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
  464. unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
  465. unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
  466. struct sg_page_iter sg_iter;
  467. pt_vaddr = NULL;
  468. for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
  469. if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES))
  470. break;
  471. if (pt_vaddr == NULL) {
  472. struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[pdpe];
  473. struct i915_page_table_entry *pt = pd->page_table[pde];
  474. struct page *page_table = pt->page;
  475. pt_vaddr = kmap_atomic(page_table);
  476. }
  477. pt_vaddr[pte] =
  478. gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
  479. cache_level, true);
  480. if (++pte == GEN8_PTES) {
  481. if (!HAS_LLC(ppgtt->base.dev))
  482. drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
  483. kunmap_atomic(pt_vaddr);
  484. pt_vaddr = NULL;
  485. if (++pde == I915_PDES) {
  486. pdpe++;
  487. pde = 0;
  488. }
  489. pte = 0;
  490. }
  491. }
  492. if (pt_vaddr) {
  493. if (!HAS_LLC(ppgtt->base.dev))
  494. drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
  495. kunmap_atomic(pt_vaddr);
  496. }
  497. }
  498. static void gen8_free_page_tables(struct i915_page_directory_entry *pd, struct drm_device *dev)
  499. {
  500. int i;
  501. if (!pd->page)
  502. return;
  503. for (i = 0; i < I915_PDES; i++) {
  504. if (WARN_ON(!pd->page_table[i]))
  505. continue;
  506. unmap_and_free_pt(pd->page_table[i], dev);
  507. pd->page_table[i] = NULL;
  508. }
  509. }
  510. static void gen8_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
  511. {
  512. int i;
  513. for (i = 0; i < ppgtt->num_pd_pages; i++) {
  514. if (WARN_ON(!ppgtt->pdp.page_directory[i]))
  515. continue;
  516. gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
  517. unmap_and_free_pd(ppgtt->pdp.page_directory[i]);
  518. }
  519. }
  520. static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
  521. {
  522. struct pci_dev *hwdev = ppgtt->base.dev->pdev;
  523. int i, j;
  524. for (i = 0; i < ppgtt->num_pd_pages; i++) {
  525. /* TODO: In the future we'll support sparse mappings, so this
  526. * will have to change. */
  527. if (!ppgtt->pdp.page_directory[i]->daddr)
  528. continue;
  529. pci_unmap_page(hwdev, ppgtt->pdp.page_directory[i]->daddr, PAGE_SIZE,
  530. PCI_DMA_BIDIRECTIONAL);
  531. for (j = 0; j < I915_PDES; j++) {
  532. struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[i];
  533. struct i915_page_table_entry *pt;
  534. dma_addr_t addr;
  535. if (WARN_ON(!pd->page_table[j]))
  536. continue;
  537. pt = pd->page_table[j];
  538. addr = pt->daddr;
  539. if (addr)
  540. pci_unmap_page(hwdev, addr, PAGE_SIZE,
  541. PCI_DMA_BIDIRECTIONAL);
  542. }
  543. }
  544. }
  545. static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
  546. {
  547. struct i915_hw_ppgtt *ppgtt =
  548. container_of(vm, struct i915_hw_ppgtt, base);
  549. gen8_ppgtt_unmap_pages(ppgtt);
  550. gen8_ppgtt_free(ppgtt);
  551. }
  552. static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
  553. {
  554. int i, ret;
  555. for (i = 0; i < ppgtt->num_pd_pages; i++) {
  556. ret = alloc_pt_range(ppgtt->pdp.page_directory[i],
  557. 0, I915_PDES, ppgtt->base.dev);
  558. if (ret)
  559. goto unwind_out;
  560. }
  561. return 0;
  562. unwind_out:
  563. while (i--)
  564. gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev);
  565. return -ENOMEM;
  566. }
  567. static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
  568. const int max_pdp)
  569. {
  570. int i;
  571. for (i = 0; i < max_pdp; i++) {
  572. ppgtt->pdp.page_directory[i] = alloc_pd_single();
  573. if (IS_ERR(ppgtt->pdp.page_directory[i]))
  574. goto unwind_out;
  575. }
  576. ppgtt->num_pd_pages = max_pdp;
  577. BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPES);
  578. return 0;
  579. unwind_out:
  580. while (i--)
  581. unmap_and_free_pd(ppgtt->pdp.page_directory[i]);
  582. return -ENOMEM;
  583. }
  584. static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
  585. const int max_pdp)
  586. {
  587. int ret;
  588. ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
  589. if (ret)
  590. return ret;
  591. ret = gen8_ppgtt_allocate_page_tables(ppgtt);
  592. if (ret)
  593. goto err_out;
  594. ppgtt->num_pd_entries = max_pdp * I915_PDES;
  595. return 0;
  596. err_out:
  597. gen8_ppgtt_free(ppgtt);
  598. return ret;
  599. }
  600. static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
  601. const int pd)
  602. {
  603. dma_addr_t pd_addr;
  604. int ret;
  605. pd_addr = pci_map_page(ppgtt->base.dev->pdev,
  606. ppgtt->pdp.page_directory[pd]->page, 0,
  607. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  608. ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
  609. if (ret)
  610. return ret;
  611. ppgtt->pdp.page_directory[pd]->daddr = pd_addr;
  612. return 0;
  613. }
  614. static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
  615. const int pd,
  616. const int pt)
  617. {
  618. dma_addr_t pt_addr;
  619. struct i915_page_directory_entry *pdir = ppgtt->pdp.page_directory[pd];
  620. struct i915_page_table_entry *ptab = pdir->page_table[pt];
  621. struct page *p = ptab->page;
  622. int ret;
  623. pt_addr = pci_map_page(ppgtt->base.dev->pdev,
  624. p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  625. ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
  626. if (ret)
  627. return ret;
  628. ptab->daddr = pt_addr;
  629. return 0;
  630. }
  631. /*
  632. * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
  633. * with a net effect resembling a 2-level page table in normal x86 terms. Each
  634. * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
  635. * space.
  636. *
  637. * FIXME: split allocation into smaller pieces. For now we only ever do this
  638. * once, but with full PPGTT, the multiple contiguous allocations will be bad.
  639. * TODO: Do something with the size parameter
  640. */
  641. static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
  642. {
  643. const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
  644. const int min_pt_pages = I915_PDES * max_pdp;
  645. int i, j, ret;
  646. if (size % (1<<30))
  647. DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
  648. /* 1. Do all our allocations for page directories and page tables.
  649. * We allocate more than was asked so that we can point the unused parts
  650. * to valid entries that point to scratch page. Dynamic page tables
  651. * will fix this eventually.
  652. */
  653. ret = gen8_ppgtt_alloc(ppgtt, GEN8_LEGACY_PDPES);
  654. if (ret)
  655. return ret;
  656. /*
  657. * 2. Create DMA mappings for the page directories and page tables.
  658. */
  659. for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
  660. ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
  661. if (ret)
  662. goto bail;
  663. for (j = 0; j < I915_PDES; j++) {
  664. ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
  665. if (ret)
  666. goto bail;
  667. }
  668. }
  669. /*
  670. * 3. Map all the page directory entires to point to the page tables
  671. * we've allocated.
  672. *
  673. * For now, the PPGTT helper functions all require that the PDEs are
  674. * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
  675. * will never need to touch the PDEs again.
  676. */
  677. for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
  678. struct i915_page_directory_entry *pd = ppgtt->pdp.page_directory[i];
  679. gen8_pde_t *pd_vaddr;
  680. pd_vaddr = kmap_atomic(ppgtt->pdp.page_directory[i]->page);
  681. for (j = 0; j < I915_PDES; j++) {
  682. struct i915_page_table_entry *pt = pd->page_table[j];
  683. dma_addr_t addr = pt->daddr;
  684. pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
  685. I915_CACHE_LLC);
  686. }
  687. if (!HAS_LLC(ppgtt->base.dev))
  688. drm_clflush_virt_range(pd_vaddr, PAGE_SIZE);
  689. kunmap_atomic(pd_vaddr);
  690. }
  691. ppgtt->switch_mm = gen8_mm_switch;
  692. ppgtt->base.clear_range = gen8_ppgtt_clear_range;
  693. ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
  694. ppgtt->base.cleanup = gen8_ppgtt_cleanup;
  695. ppgtt->base.start = 0;
  696. /* This is the area that we advertise as usable for the caller */
  697. ppgtt->base.total = max_pdp * I915_PDES * GEN8_PTES * PAGE_SIZE;
  698. /* Set all ptes to a valid scratch page. Also above requested space */
  699. ppgtt->base.clear_range(&ppgtt->base, 0,
  700. ppgtt->num_pd_pages * GEN8_PTES * PAGE_SIZE,
  701. true);
  702. DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
  703. ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
  704. DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
  705. ppgtt->num_pd_entries,
  706. (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30));
  707. return 0;
  708. bail:
  709. gen8_ppgtt_unmap_pages(ppgtt);
  710. gen8_ppgtt_free(ppgtt);
  711. return ret;
  712. }
  713. static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
  714. {
  715. struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
  716. struct i915_address_space *vm = &ppgtt->base;
  717. gen6_pte_t __iomem *pd_addr;
  718. gen6_pte_t scratch_pte;
  719. uint32_t pd_entry;
  720. int pte, pde;
  721. scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
  722. pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
  723. ppgtt->pd.pd_offset / sizeof(gen6_pte_t);
  724. seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
  725. ppgtt->pd.pd_offset,
  726. ppgtt->pd.pd_offset + ppgtt->num_pd_entries);
  727. for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
  728. u32 expected;
  729. gen6_pte_t *pt_vaddr;
  730. dma_addr_t pt_addr = ppgtt->pd.page_table[pde]->daddr;
  731. pd_entry = readl(pd_addr + pde);
  732. expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
  733. if (pd_entry != expected)
  734. seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
  735. pde,
  736. pd_entry,
  737. expected);
  738. seq_printf(m, "\tPDE: %x\n", pd_entry);
  739. pt_vaddr = kmap_atomic(ppgtt->pd.page_table[pde]->page);
  740. for (pte = 0; pte < GEN6_PTES; pte+=4) {
  741. unsigned long va =
  742. (pde * PAGE_SIZE * GEN6_PTES) +
  743. (pte * PAGE_SIZE);
  744. int i;
  745. bool found = false;
  746. for (i = 0; i < 4; i++)
  747. if (pt_vaddr[pte + i] != scratch_pte)
  748. found = true;
  749. if (!found)
  750. continue;
  751. seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
  752. for (i = 0; i < 4; i++) {
  753. if (pt_vaddr[pte + i] != scratch_pte)
  754. seq_printf(m, " %08x", pt_vaddr[pte + i]);
  755. else
  756. seq_puts(m, " SCRATCH ");
  757. }
  758. seq_puts(m, "\n");
  759. }
  760. kunmap_atomic(pt_vaddr);
  761. }
  762. }
  763. /* Write pde (index) from the page directory @pd to the page table @pt */
  764. static void gen6_write_pde(struct i915_page_directory_entry *pd,
  765. const int pde, struct i915_page_table_entry *pt)
  766. {
  767. /* Caller needs to make sure the write completes if necessary */
  768. struct i915_hw_ppgtt *ppgtt =
  769. container_of(pd, struct i915_hw_ppgtt, pd);
  770. u32 pd_entry;
  771. pd_entry = GEN6_PDE_ADDR_ENCODE(pt->daddr);
  772. pd_entry |= GEN6_PDE_VALID;
  773. writel(pd_entry, ppgtt->pd_addr + pde);
  774. }
  775. /* Write all the page tables found in the ppgtt structure to incrementing page
  776. * directories. */
  777. static void gen6_write_page_range(struct drm_i915_private *dev_priv,
  778. struct i915_page_directory_entry *pd,
  779. uint32_t start, uint32_t length)
  780. {
  781. struct i915_page_table_entry *pt;
  782. uint32_t pde, temp;
  783. gen6_for_each_pde(pt, pd, start, length, temp, pde)
  784. gen6_write_pde(pd, pde, pt);
  785. /* Make sure write is complete before other code can use this page
  786. * table. Also require for WC mapped PTEs */
  787. readl(dev_priv->gtt.gsm);
  788. }
  789. static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
  790. {
  791. BUG_ON(ppgtt->pd.pd_offset & 0x3f);
  792. return (ppgtt->pd.pd_offset / 64) << 16;
  793. }
  794. static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
  795. struct intel_engine_cs *ring)
  796. {
  797. int ret;
  798. /* NB: TLBs must be flushed and invalidated before a switch */
  799. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  800. if (ret)
  801. return ret;
  802. ret = intel_ring_begin(ring, 6);
  803. if (ret)
  804. return ret;
  805. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
  806. intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
  807. intel_ring_emit(ring, PP_DIR_DCLV_2G);
  808. intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
  809. intel_ring_emit(ring, get_pd_offset(ppgtt));
  810. intel_ring_emit(ring, MI_NOOP);
  811. intel_ring_advance(ring);
  812. return 0;
  813. }
  814. static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
  815. struct intel_engine_cs *ring)
  816. {
  817. struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
  818. I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
  819. I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
  820. return 0;
  821. }
  822. static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
  823. struct intel_engine_cs *ring)
  824. {
  825. int ret;
  826. /* NB: TLBs must be flushed and invalidated before a switch */
  827. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  828. if (ret)
  829. return ret;
  830. ret = intel_ring_begin(ring, 6);
  831. if (ret)
  832. return ret;
  833. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
  834. intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
  835. intel_ring_emit(ring, PP_DIR_DCLV_2G);
  836. intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
  837. intel_ring_emit(ring, get_pd_offset(ppgtt));
  838. intel_ring_emit(ring, MI_NOOP);
  839. intel_ring_advance(ring);
  840. /* XXX: RCS is the only one to auto invalidate the TLBs? */
  841. if (ring->id != RCS) {
  842. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  843. if (ret)
  844. return ret;
  845. }
  846. return 0;
  847. }
  848. static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
  849. struct intel_engine_cs *ring)
  850. {
  851. struct drm_device *dev = ppgtt->base.dev;
  852. struct drm_i915_private *dev_priv = dev->dev_private;
  853. I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
  854. I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
  855. POSTING_READ(RING_PP_DIR_DCLV(ring));
  856. return 0;
  857. }
  858. static void gen8_ppgtt_enable(struct drm_device *dev)
  859. {
  860. struct drm_i915_private *dev_priv = dev->dev_private;
  861. struct intel_engine_cs *ring;
  862. int j;
  863. for_each_ring(ring, dev_priv, j) {
  864. I915_WRITE(RING_MODE_GEN7(ring),
  865. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  866. }
  867. }
  868. static void gen7_ppgtt_enable(struct drm_device *dev)
  869. {
  870. struct drm_i915_private *dev_priv = dev->dev_private;
  871. struct intel_engine_cs *ring;
  872. uint32_t ecochk, ecobits;
  873. int i;
  874. ecobits = I915_READ(GAC_ECO_BITS);
  875. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
  876. ecochk = I915_READ(GAM_ECOCHK);
  877. if (IS_HASWELL(dev)) {
  878. ecochk |= ECOCHK_PPGTT_WB_HSW;
  879. } else {
  880. ecochk |= ECOCHK_PPGTT_LLC_IVB;
  881. ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
  882. }
  883. I915_WRITE(GAM_ECOCHK, ecochk);
  884. for_each_ring(ring, dev_priv, i) {
  885. /* GFX_MODE is per-ring on gen7+ */
  886. I915_WRITE(RING_MODE_GEN7(ring),
  887. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  888. }
  889. }
  890. static void gen6_ppgtt_enable(struct drm_device *dev)
  891. {
  892. struct drm_i915_private *dev_priv = dev->dev_private;
  893. uint32_t ecochk, gab_ctl, ecobits;
  894. ecobits = I915_READ(GAC_ECO_BITS);
  895. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
  896. ECOBITS_PPGTT_CACHE64B);
  897. gab_ctl = I915_READ(GAB_CTL);
  898. I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
  899. ecochk = I915_READ(GAM_ECOCHK);
  900. I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
  901. I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  902. }
  903. /* PPGTT support for Sandybdrige/Gen6 and later */
  904. static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
  905. uint64_t start,
  906. uint64_t length,
  907. bool use_scratch)
  908. {
  909. struct i915_hw_ppgtt *ppgtt =
  910. container_of(vm, struct i915_hw_ppgtt, base);
  911. gen6_pte_t *pt_vaddr, scratch_pte;
  912. unsigned first_entry = start >> PAGE_SHIFT;
  913. unsigned num_entries = length >> PAGE_SHIFT;
  914. unsigned act_pt = first_entry / GEN6_PTES;
  915. unsigned first_pte = first_entry % GEN6_PTES;
  916. unsigned last_pte, i;
  917. scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
  918. while (num_entries) {
  919. last_pte = first_pte + num_entries;
  920. if (last_pte > GEN6_PTES)
  921. last_pte = GEN6_PTES;
  922. pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
  923. for (i = first_pte; i < last_pte; i++)
  924. pt_vaddr[i] = scratch_pte;
  925. kunmap_atomic(pt_vaddr);
  926. num_entries -= last_pte - first_pte;
  927. first_pte = 0;
  928. act_pt++;
  929. }
  930. }
  931. static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
  932. struct sg_table *pages,
  933. uint64_t start,
  934. enum i915_cache_level cache_level, u32 flags)
  935. {
  936. struct i915_hw_ppgtt *ppgtt =
  937. container_of(vm, struct i915_hw_ppgtt, base);
  938. gen6_pte_t *pt_vaddr;
  939. unsigned first_entry = start >> PAGE_SHIFT;
  940. unsigned act_pt = first_entry / GEN6_PTES;
  941. unsigned act_pte = first_entry % GEN6_PTES;
  942. struct sg_page_iter sg_iter;
  943. pt_vaddr = NULL;
  944. for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
  945. if (pt_vaddr == NULL)
  946. pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page);
  947. pt_vaddr[act_pte] =
  948. vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
  949. cache_level, true, flags);
  950. if (++act_pte == GEN6_PTES) {
  951. kunmap_atomic(pt_vaddr);
  952. pt_vaddr = NULL;
  953. act_pt++;
  954. act_pte = 0;
  955. }
  956. }
  957. if (pt_vaddr)
  958. kunmap_atomic(pt_vaddr);
  959. }
  960. /* PDE TLBs are a pain invalidate pre GEN8. It requires a context reload. If we
  961. * are switching between contexts with the same LRCA, we also must do a force
  962. * restore.
  963. */
  964. static inline void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
  965. {
  966. /* If current vm != vm, */
  967. ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
  968. }
  969. static void gen6_initialize_pt(struct i915_address_space *vm,
  970. struct i915_page_table_entry *pt)
  971. {
  972. gen6_pte_t *pt_vaddr, scratch_pte;
  973. int i;
  974. WARN_ON(vm->scratch.addr == 0);
  975. scratch_pte = vm->pte_encode(vm->scratch.addr,
  976. I915_CACHE_LLC, true, 0);
  977. pt_vaddr = kmap_atomic(pt->page);
  978. for (i = 0; i < GEN6_PTES; i++)
  979. pt_vaddr[i] = scratch_pte;
  980. kunmap_atomic(pt_vaddr);
  981. }
  982. static int gen6_alloc_va_range(struct i915_address_space *vm,
  983. uint64_t start, uint64_t length)
  984. {
  985. DECLARE_BITMAP(new_page_tables, I915_PDES);
  986. struct drm_device *dev = vm->dev;
  987. struct drm_i915_private *dev_priv = dev->dev_private;
  988. struct i915_hw_ppgtt *ppgtt =
  989. container_of(vm, struct i915_hw_ppgtt, base);
  990. struct i915_page_table_entry *pt;
  991. const uint32_t start_save = start, length_save = length;
  992. uint32_t pde, temp;
  993. int ret;
  994. WARN_ON(upper_32_bits(start));
  995. bitmap_zero(new_page_tables, I915_PDES);
  996. /* The allocation is done in two stages so that we can bail out with
  997. * minimal amount of pain. The first stage finds new page tables that
  998. * need allocation. The second stage marks use ptes within the page
  999. * tables.
  1000. */
  1001. gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
  1002. if (pt != ppgtt->scratch_pt) {
  1003. WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
  1004. continue;
  1005. }
  1006. /* We've already allocated a page table */
  1007. WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
  1008. pt = alloc_pt_single(dev);
  1009. if (IS_ERR(pt)) {
  1010. ret = PTR_ERR(pt);
  1011. goto unwind_out;
  1012. }
  1013. gen6_initialize_pt(vm, pt);
  1014. ppgtt->pd.page_table[pde] = pt;
  1015. set_bit(pde, new_page_tables);
  1016. trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
  1017. }
  1018. start = start_save;
  1019. length = length_save;
  1020. gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
  1021. DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
  1022. bitmap_zero(tmp_bitmap, GEN6_PTES);
  1023. bitmap_set(tmp_bitmap, gen6_pte_index(start),
  1024. gen6_pte_count(start, length));
  1025. if (test_and_clear_bit(pde, new_page_tables))
  1026. gen6_write_pde(&ppgtt->pd, pde, pt);
  1027. trace_i915_page_table_entry_map(vm, pde, pt,
  1028. gen6_pte_index(start),
  1029. gen6_pte_count(start, length),
  1030. GEN6_PTES);
  1031. bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
  1032. GEN6_PTES);
  1033. }
  1034. WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
  1035. /* Make sure write is complete before other code can use this page
  1036. * table. Also require for WC mapped PTEs */
  1037. readl(dev_priv->gtt.gsm);
  1038. mark_tlbs_dirty(ppgtt);
  1039. return 0;
  1040. unwind_out:
  1041. for_each_set_bit(pde, new_page_tables, I915_PDES) {
  1042. struct i915_page_table_entry *pt = ppgtt->pd.page_table[pde];
  1043. ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
  1044. unmap_and_free_pt(pt, vm->dev);
  1045. }
  1046. mark_tlbs_dirty(ppgtt);
  1047. return ret;
  1048. }
  1049. static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
  1050. {
  1051. int i;
  1052. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  1053. struct i915_page_table_entry *pt = ppgtt->pd.page_table[i];
  1054. if (pt != ppgtt->scratch_pt)
  1055. unmap_and_free_pt(ppgtt->pd.page_table[i], ppgtt->base.dev);
  1056. }
  1057. unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
  1058. unmap_and_free_pd(&ppgtt->pd);
  1059. }
  1060. static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
  1061. {
  1062. struct i915_hw_ppgtt *ppgtt =
  1063. container_of(vm, struct i915_hw_ppgtt, base);
  1064. drm_mm_remove_node(&ppgtt->node);
  1065. gen6_ppgtt_free(ppgtt);
  1066. }
  1067. static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
  1068. {
  1069. struct drm_device *dev = ppgtt->base.dev;
  1070. struct drm_i915_private *dev_priv = dev->dev_private;
  1071. bool retried = false;
  1072. int ret;
  1073. /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
  1074. * allocator works in address space sizes, so it's multiplied by page
  1075. * size. We allocate at the top of the GTT to avoid fragmentation.
  1076. */
  1077. BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
  1078. ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev);
  1079. if (IS_ERR(ppgtt->scratch_pt))
  1080. return PTR_ERR(ppgtt->scratch_pt);
  1081. gen6_initialize_pt(&ppgtt->base, ppgtt->scratch_pt);
  1082. alloc:
  1083. ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
  1084. &ppgtt->node, GEN6_PD_SIZE,
  1085. GEN6_PD_ALIGN, 0,
  1086. 0, dev_priv->gtt.base.total,
  1087. DRM_MM_TOPDOWN);
  1088. if (ret == -ENOSPC && !retried) {
  1089. ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
  1090. GEN6_PD_SIZE, GEN6_PD_ALIGN,
  1091. I915_CACHE_NONE,
  1092. 0, dev_priv->gtt.base.total,
  1093. 0);
  1094. if (ret)
  1095. goto err_out;
  1096. retried = true;
  1097. goto alloc;
  1098. }
  1099. if (ret)
  1100. goto err_out;
  1101. if (ppgtt->node.start < dev_priv->gtt.mappable_end)
  1102. DRM_DEBUG("Forced to use aperture for PDEs\n");
  1103. ppgtt->num_pd_entries = I915_PDES;
  1104. return 0;
  1105. err_out:
  1106. unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev);
  1107. return ret;
  1108. }
  1109. static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
  1110. {
  1111. return gen6_ppgtt_allocate_page_directories(ppgtt);
  1112. }
  1113. static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
  1114. uint64_t start, uint64_t length)
  1115. {
  1116. struct i915_page_table_entry *unused;
  1117. uint32_t pde, temp;
  1118. gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
  1119. ppgtt->pd.page_table[pde] = ppgtt->scratch_pt;
  1120. }
  1121. static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt, bool aliasing)
  1122. {
  1123. struct drm_device *dev = ppgtt->base.dev;
  1124. struct drm_i915_private *dev_priv = dev->dev_private;
  1125. int ret;
  1126. ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
  1127. if (IS_GEN6(dev)) {
  1128. ppgtt->switch_mm = gen6_mm_switch;
  1129. } else if (IS_HASWELL(dev)) {
  1130. ppgtt->switch_mm = hsw_mm_switch;
  1131. } else if (IS_GEN7(dev)) {
  1132. ppgtt->switch_mm = gen7_mm_switch;
  1133. } else
  1134. BUG();
  1135. if (intel_vgpu_active(dev))
  1136. ppgtt->switch_mm = vgpu_mm_switch;
  1137. ret = gen6_ppgtt_alloc(ppgtt);
  1138. if (ret)
  1139. return ret;
  1140. if (aliasing) {
  1141. /* preallocate all pts */
  1142. ret = alloc_pt_range(&ppgtt->pd, 0, ppgtt->num_pd_entries,
  1143. ppgtt->base.dev);
  1144. if (ret) {
  1145. gen6_ppgtt_cleanup(&ppgtt->base);
  1146. return ret;
  1147. }
  1148. }
  1149. ppgtt->base.allocate_va_range = gen6_alloc_va_range;
  1150. ppgtt->base.clear_range = gen6_ppgtt_clear_range;
  1151. ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
  1152. ppgtt->base.cleanup = gen6_ppgtt_cleanup;
  1153. ppgtt->base.start = 0;
  1154. ppgtt->base.total = ppgtt->num_pd_entries * GEN6_PTES * PAGE_SIZE;
  1155. ppgtt->debug_dump = gen6_dump_ppgtt;
  1156. ppgtt->pd.pd_offset =
  1157. ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
  1158. ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
  1159. ppgtt->pd.pd_offset / sizeof(gen6_pte_t);
  1160. if (aliasing)
  1161. ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
  1162. else
  1163. gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
  1164. gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
  1165. DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
  1166. ppgtt->node.size >> 20,
  1167. ppgtt->node.start / PAGE_SIZE);
  1168. DRM_DEBUG("Adding PPGTT at offset %x\n",
  1169. ppgtt->pd.pd_offset << 10);
  1170. return 0;
  1171. }
  1172. static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt,
  1173. bool aliasing)
  1174. {
  1175. struct drm_i915_private *dev_priv = dev->dev_private;
  1176. ppgtt->base.dev = dev;
  1177. ppgtt->base.scratch = dev_priv->gtt.base.scratch;
  1178. if (INTEL_INFO(dev)->gen < 8)
  1179. return gen6_ppgtt_init(ppgtt, aliasing);
  1180. else
  1181. return gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
  1182. }
  1183. int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
  1184. {
  1185. struct drm_i915_private *dev_priv = dev->dev_private;
  1186. int ret = 0;
  1187. ret = __hw_ppgtt_init(dev, ppgtt, false);
  1188. if (ret == 0) {
  1189. kref_init(&ppgtt->ref);
  1190. drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
  1191. ppgtt->base.total);
  1192. i915_init_vm(dev_priv, &ppgtt->base);
  1193. }
  1194. return ret;
  1195. }
  1196. int i915_ppgtt_init_hw(struct drm_device *dev)
  1197. {
  1198. struct drm_i915_private *dev_priv = dev->dev_private;
  1199. struct intel_engine_cs *ring;
  1200. struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
  1201. int i, ret = 0;
  1202. /* In the case of execlists, PPGTT is enabled by the context descriptor
  1203. * and the PDPs are contained within the context itself. We don't
  1204. * need to do anything here. */
  1205. if (i915.enable_execlists)
  1206. return 0;
  1207. if (!USES_PPGTT(dev))
  1208. return 0;
  1209. if (IS_GEN6(dev))
  1210. gen6_ppgtt_enable(dev);
  1211. else if (IS_GEN7(dev))
  1212. gen7_ppgtt_enable(dev);
  1213. else if (INTEL_INFO(dev)->gen >= 8)
  1214. gen8_ppgtt_enable(dev);
  1215. else
  1216. MISSING_CASE(INTEL_INFO(dev)->gen);
  1217. if (ppgtt) {
  1218. for_each_ring(ring, dev_priv, i) {
  1219. ret = ppgtt->switch_mm(ppgtt, ring);
  1220. if (ret != 0)
  1221. return ret;
  1222. }
  1223. }
  1224. return ret;
  1225. }
  1226. struct i915_hw_ppgtt *
  1227. i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
  1228. {
  1229. struct i915_hw_ppgtt *ppgtt;
  1230. int ret;
  1231. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  1232. if (!ppgtt)
  1233. return ERR_PTR(-ENOMEM);
  1234. ret = i915_ppgtt_init(dev, ppgtt);
  1235. if (ret) {
  1236. kfree(ppgtt);
  1237. return ERR_PTR(ret);
  1238. }
  1239. ppgtt->file_priv = fpriv;
  1240. trace_i915_ppgtt_create(&ppgtt->base);
  1241. return ppgtt;
  1242. }
  1243. void i915_ppgtt_release(struct kref *kref)
  1244. {
  1245. struct i915_hw_ppgtt *ppgtt =
  1246. container_of(kref, struct i915_hw_ppgtt, ref);
  1247. trace_i915_ppgtt_release(&ppgtt->base);
  1248. /* vmas should already be unbound */
  1249. WARN_ON(!list_empty(&ppgtt->base.active_list));
  1250. WARN_ON(!list_empty(&ppgtt->base.inactive_list));
  1251. list_del(&ppgtt->base.global_link);
  1252. drm_mm_takedown(&ppgtt->base.mm);
  1253. ppgtt->base.cleanup(&ppgtt->base);
  1254. kfree(ppgtt);
  1255. }
  1256. static void
  1257. ppgtt_bind_vma(struct i915_vma *vma,
  1258. enum i915_cache_level cache_level,
  1259. u32 flags)
  1260. {
  1261. /* Currently applicable only to VLV */
  1262. if (vma->obj->gt_ro)
  1263. flags |= PTE_READ_ONLY;
  1264. vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
  1265. cache_level, flags);
  1266. }
  1267. static void ppgtt_unbind_vma(struct i915_vma *vma)
  1268. {
  1269. vma->vm->clear_range(vma->vm,
  1270. vma->node.start,
  1271. vma->obj->base.size,
  1272. true);
  1273. }
  1274. extern int intel_iommu_gfx_mapped;
  1275. /* Certain Gen5 chipsets require require idling the GPU before
  1276. * unmapping anything from the GTT when VT-d is enabled.
  1277. */
  1278. static inline bool needs_idle_maps(struct drm_device *dev)
  1279. {
  1280. #ifdef CONFIG_INTEL_IOMMU
  1281. /* Query intel_iommu to see if we need the workaround. Presumably that
  1282. * was loaded first.
  1283. */
  1284. if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
  1285. return true;
  1286. #endif
  1287. return false;
  1288. }
  1289. static bool do_idling(struct drm_i915_private *dev_priv)
  1290. {
  1291. bool ret = dev_priv->mm.interruptible;
  1292. if (unlikely(dev_priv->gtt.do_idle_maps)) {
  1293. dev_priv->mm.interruptible = false;
  1294. if (i915_gpu_idle(dev_priv->dev)) {
  1295. DRM_ERROR("Couldn't idle GPU\n");
  1296. /* Wait a bit, in hopes it avoids the hang */
  1297. udelay(10);
  1298. }
  1299. }
  1300. return ret;
  1301. }
  1302. static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
  1303. {
  1304. if (unlikely(dev_priv->gtt.do_idle_maps))
  1305. dev_priv->mm.interruptible = interruptible;
  1306. }
  1307. void i915_check_and_clear_faults(struct drm_device *dev)
  1308. {
  1309. struct drm_i915_private *dev_priv = dev->dev_private;
  1310. struct intel_engine_cs *ring;
  1311. int i;
  1312. if (INTEL_INFO(dev)->gen < 6)
  1313. return;
  1314. for_each_ring(ring, dev_priv, i) {
  1315. u32 fault_reg;
  1316. fault_reg = I915_READ(RING_FAULT_REG(ring));
  1317. if (fault_reg & RING_FAULT_VALID) {
  1318. DRM_DEBUG_DRIVER("Unexpected fault\n"
  1319. "\tAddr: 0x%08lx\n"
  1320. "\tAddress space: %s\n"
  1321. "\tSource ID: %d\n"
  1322. "\tType: %d\n",
  1323. fault_reg & PAGE_MASK,
  1324. fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
  1325. RING_FAULT_SRCID(fault_reg),
  1326. RING_FAULT_FAULT_TYPE(fault_reg));
  1327. I915_WRITE(RING_FAULT_REG(ring),
  1328. fault_reg & ~RING_FAULT_VALID);
  1329. }
  1330. }
  1331. POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
  1332. }
  1333. static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
  1334. {
  1335. if (INTEL_INFO(dev_priv->dev)->gen < 6) {
  1336. intel_gtt_chipset_flush();
  1337. } else {
  1338. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  1339. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1340. }
  1341. }
  1342. void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
  1343. {
  1344. struct drm_i915_private *dev_priv = dev->dev_private;
  1345. /* Don't bother messing with faults pre GEN6 as we have little
  1346. * documentation supporting that it's a good idea.
  1347. */
  1348. if (INTEL_INFO(dev)->gen < 6)
  1349. return;
  1350. i915_check_and_clear_faults(dev);
  1351. dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
  1352. dev_priv->gtt.base.start,
  1353. dev_priv->gtt.base.total,
  1354. true);
  1355. i915_ggtt_flush(dev_priv);
  1356. }
  1357. void i915_gem_restore_gtt_mappings(struct drm_device *dev)
  1358. {
  1359. struct drm_i915_private *dev_priv = dev->dev_private;
  1360. struct drm_i915_gem_object *obj;
  1361. struct i915_address_space *vm;
  1362. i915_check_and_clear_faults(dev);
  1363. /* First fill our portion of the GTT with scratch pages */
  1364. dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
  1365. dev_priv->gtt.base.start,
  1366. dev_priv->gtt.base.total,
  1367. true);
  1368. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  1369. struct i915_vma *vma = i915_gem_obj_to_vma(obj,
  1370. &dev_priv->gtt.base);
  1371. if (!vma)
  1372. continue;
  1373. i915_gem_clflush_object(obj, obj->pin_display);
  1374. /* The bind_vma code tries to be smart about tracking mappings.
  1375. * Unfortunately above, we've just wiped out the mappings
  1376. * without telling our object about it. So we need to fake it.
  1377. *
  1378. * Bind is not expected to fail since this is only called on
  1379. * resume and assumption is all requirements exist already.
  1380. */
  1381. vma->bound &= ~GLOBAL_BIND;
  1382. WARN_ON(i915_vma_bind(vma, obj->cache_level, GLOBAL_BIND));
  1383. }
  1384. if (INTEL_INFO(dev)->gen >= 8) {
  1385. if (IS_CHERRYVIEW(dev))
  1386. chv_setup_private_ppat(dev_priv);
  1387. else
  1388. bdw_setup_private_ppat(dev_priv);
  1389. return;
  1390. }
  1391. if (USES_PPGTT(dev)) {
  1392. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  1393. /* TODO: Perhaps it shouldn't be gen6 specific */
  1394. struct i915_hw_ppgtt *ppgtt =
  1395. container_of(vm, struct i915_hw_ppgtt,
  1396. base);
  1397. if (i915_is_ggtt(vm))
  1398. ppgtt = dev_priv->mm.aliasing_ppgtt;
  1399. gen6_write_page_range(dev_priv, &ppgtt->pd,
  1400. 0, ppgtt->base.total);
  1401. }
  1402. }
  1403. i915_ggtt_flush(dev_priv);
  1404. }
  1405. int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
  1406. {
  1407. if (obj->has_dma_mapping)
  1408. return 0;
  1409. if (!dma_map_sg(&obj->base.dev->pdev->dev,
  1410. obj->pages->sgl, obj->pages->nents,
  1411. PCI_DMA_BIDIRECTIONAL))
  1412. return -ENOSPC;
  1413. return 0;
  1414. }
  1415. static inline void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
  1416. {
  1417. #ifdef writeq
  1418. writeq(pte, addr);
  1419. #else
  1420. iowrite32((u32)pte, addr);
  1421. iowrite32(pte >> 32, addr + 4);
  1422. #endif
  1423. }
  1424. static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
  1425. struct sg_table *st,
  1426. uint64_t start,
  1427. enum i915_cache_level level, u32 unused)
  1428. {
  1429. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1430. unsigned first_entry = start >> PAGE_SHIFT;
  1431. gen8_pte_t __iomem *gtt_entries =
  1432. (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
  1433. int i = 0;
  1434. struct sg_page_iter sg_iter;
  1435. dma_addr_t addr = 0; /* shut up gcc */
  1436. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
  1437. addr = sg_dma_address(sg_iter.sg) +
  1438. (sg_iter.sg_pgoffset << PAGE_SHIFT);
  1439. gen8_set_pte(&gtt_entries[i],
  1440. gen8_pte_encode(addr, level, true));
  1441. i++;
  1442. }
  1443. /*
  1444. * XXX: This serves as a posting read to make sure that the PTE has
  1445. * actually been updated. There is some concern that even though
  1446. * registers and PTEs are within the same BAR that they are potentially
  1447. * of NUMA access patterns. Therefore, even with the way we assume
  1448. * hardware should work, we must keep this posting read for paranoia.
  1449. */
  1450. if (i != 0)
  1451. WARN_ON(readq(&gtt_entries[i-1])
  1452. != gen8_pte_encode(addr, level, true));
  1453. /* This next bit makes the above posting read even more important. We
  1454. * want to flush the TLBs only after we're certain all the PTE updates
  1455. * have finished.
  1456. */
  1457. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  1458. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1459. }
  1460. /*
  1461. * Binds an object into the global gtt with the specified cache level. The object
  1462. * will be accessible to the GPU via commands whose operands reference offsets
  1463. * within the global GTT as well as accessible by the GPU through the GMADR
  1464. * mapped BAR (dev_priv->mm.gtt->gtt).
  1465. */
  1466. static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
  1467. struct sg_table *st,
  1468. uint64_t start,
  1469. enum i915_cache_level level, u32 flags)
  1470. {
  1471. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1472. unsigned first_entry = start >> PAGE_SHIFT;
  1473. gen6_pte_t __iomem *gtt_entries =
  1474. (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
  1475. int i = 0;
  1476. struct sg_page_iter sg_iter;
  1477. dma_addr_t addr = 0;
  1478. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
  1479. addr = sg_page_iter_dma_address(&sg_iter);
  1480. iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
  1481. i++;
  1482. }
  1483. /* XXX: This serves as a posting read to make sure that the PTE has
  1484. * actually been updated. There is some concern that even though
  1485. * registers and PTEs are within the same BAR that they are potentially
  1486. * of NUMA access patterns. Therefore, even with the way we assume
  1487. * hardware should work, we must keep this posting read for paranoia.
  1488. */
  1489. if (i != 0) {
  1490. unsigned long gtt = readl(&gtt_entries[i-1]);
  1491. WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
  1492. }
  1493. /* This next bit makes the above posting read even more important. We
  1494. * want to flush the TLBs only after we're certain all the PTE updates
  1495. * have finished.
  1496. */
  1497. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  1498. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1499. }
  1500. static void gen8_ggtt_clear_range(struct i915_address_space *vm,
  1501. uint64_t start,
  1502. uint64_t length,
  1503. bool use_scratch)
  1504. {
  1505. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1506. unsigned first_entry = start >> PAGE_SHIFT;
  1507. unsigned num_entries = length >> PAGE_SHIFT;
  1508. gen8_pte_t scratch_pte, __iomem *gtt_base =
  1509. (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
  1510. const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
  1511. int i;
  1512. if (WARN(num_entries > max_entries,
  1513. "First entry = %d; Num entries = %d (max=%d)\n",
  1514. first_entry, num_entries, max_entries))
  1515. num_entries = max_entries;
  1516. scratch_pte = gen8_pte_encode(vm->scratch.addr,
  1517. I915_CACHE_LLC,
  1518. use_scratch);
  1519. for (i = 0; i < num_entries; i++)
  1520. gen8_set_pte(&gtt_base[i], scratch_pte);
  1521. readl(gtt_base);
  1522. }
  1523. static void gen6_ggtt_clear_range(struct i915_address_space *vm,
  1524. uint64_t start,
  1525. uint64_t length,
  1526. bool use_scratch)
  1527. {
  1528. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1529. unsigned first_entry = start >> PAGE_SHIFT;
  1530. unsigned num_entries = length >> PAGE_SHIFT;
  1531. gen6_pte_t scratch_pte, __iomem *gtt_base =
  1532. (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
  1533. const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
  1534. int i;
  1535. if (WARN(num_entries > max_entries,
  1536. "First entry = %d; Num entries = %d (max=%d)\n",
  1537. first_entry, num_entries, max_entries))
  1538. num_entries = max_entries;
  1539. scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
  1540. for (i = 0; i < num_entries; i++)
  1541. iowrite32(scratch_pte, &gtt_base[i]);
  1542. readl(gtt_base);
  1543. }
  1544. static void i915_ggtt_bind_vma(struct i915_vma *vma,
  1545. enum i915_cache_level cache_level,
  1546. u32 unused)
  1547. {
  1548. const unsigned long entry = vma->node.start >> PAGE_SHIFT;
  1549. unsigned int flags = (cache_level == I915_CACHE_NONE) ?
  1550. AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
  1551. BUG_ON(!i915_is_ggtt(vma->vm));
  1552. intel_gtt_insert_sg_entries(vma->ggtt_view.pages, entry, flags);
  1553. vma->bound = GLOBAL_BIND;
  1554. }
  1555. static void i915_ggtt_clear_range(struct i915_address_space *vm,
  1556. uint64_t start,
  1557. uint64_t length,
  1558. bool unused)
  1559. {
  1560. unsigned first_entry = start >> PAGE_SHIFT;
  1561. unsigned num_entries = length >> PAGE_SHIFT;
  1562. intel_gtt_clear_range(first_entry, num_entries);
  1563. }
  1564. static void i915_ggtt_unbind_vma(struct i915_vma *vma)
  1565. {
  1566. const unsigned int first = vma->node.start >> PAGE_SHIFT;
  1567. const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
  1568. BUG_ON(!i915_is_ggtt(vma->vm));
  1569. vma->bound = 0;
  1570. intel_gtt_clear_range(first, size);
  1571. }
  1572. static void ggtt_bind_vma(struct i915_vma *vma,
  1573. enum i915_cache_level cache_level,
  1574. u32 flags)
  1575. {
  1576. struct drm_device *dev = vma->vm->dev;
  1577. struct drm_i915_private *dev_priv = dev->dev_private;
  1578. struct drm_i915_gem_object *obj = vma->obj;
  1579. struct sg_table *pages = obj->pages;
  1580. /* Currently applicable only to VLV */
  1581. if (obj->gt_ro)
  1582. flags |= PTE_READ_ONLY;
  1583. if (i915_is_ggtt(vma->vm))
  1584. pages = vma->ggtt_view.pages;
  1585. /* If there is no aliasing PPGTT, or the caller needs a global mapping,
  1586. * or we have a global mapping already but the cacheability flags have
  1587. * changed, set the global PTEs.
  1588. *
  1589. * If there is an aliasing PPGTT it is anecdotally faster, so use that
  1590. * instead if none of the above hold true.
  1591. *
  1592. * NB: A global mapping should only be needed for special regions like
  1593. * "gtt mappable", SNB errata, or if specified via special execbuf
  1594. * flags. At all other times, the GPU will use the aliasing PPGTT.
  1595. */
  1596. if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
  1597. if (!(vma->bound & GLOBAL_BIND) ||
  1598. (cache_level != obj->cache_level)) {
  1599. vma->vm->insert_entries(vma->vm, pages,
  1600. vma->node.start,
  1601. cache_level, flags);
  1602. vma->bound |= GLOBAL_BIND;
  1603. }
  1604. }
  1605. if (dev_priv->mm.aliasing_ppgtt &&
  1606. (!(vma->bound & LOCAL_BIND) ||
  1607. (cache_level != obj->cache_level))) {
  1608. struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
  1609. appgtt->base.insert_entries(&appgtt->base, pages,
  1610. vma->node.start,
  1611. cache_level, flags);
  1612. vma->bound |= LOCAL_BIND;
  1613. }
  1614. }
  1615. static void ggtt_unbind_vma(struct i915_vma *vma)
  1616. {
  1617. struct drm_device *dev = vma->vm->dev;
  1618. struct drm_i915_private *dev_priv = dev->dev_private;
  1619. struct drm_i915_gem_object *obj = vma->obj;
  1620. if (vma->bound & GLOBAL_BIND) {
  1621. vma->vm->clear_range(vma->vm,
  1622. vma->node.start,
  1623. obj->base.size,
  1624. true);
  1625. vma->bound &= ~GLOBAL_BIND;
  1626. }
  1627. if (vma->bound & LOCAL_BIND) {
  1628. struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
  1629. appgtt->base.clear_range(&appgtt->base,
  1630. vma->node.start,
  1631. obj->base.size,
  1632. true);
  1633. vma->bound &= ~LOCAL_BIND;
  1634. }
  1635. }
  1636. void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
  1637. {
  1638. struct drm_device *dev = obj->base.dev;
  1639. struct drm_i915_private *dev_priv = dev->dev_private;
  1640. bool interruptible;
  1641. interruptible = do_idling(dev_priv);
  1642. if (!obj->has_dma_mapping)
  1643. dma_unmap_sg(&dev->pdev->dev,
  1644. obj->pages->sgl, obj->pages->nents,
  1645. PCI_DMA_BIDIRECTIONAL);
  1646. undo_idling(dev_priv, interruptible);
  1647. }
  1648. static void i915_gtt_color_adjust(struct drm_mm_node *node,
  1649. unsigned long color,
  1650. u64 *start,
  1651. u64 *end)
  1652. {
  1653. if (node->color != color)
  1654. *start += 4096;
  1655. if (!list_empty(&node->node_list)) {
  1656. node = list_entry(node->node_list.next,
  1657. struct drm_mm_node,
  1658. node_list);
  1659. if (node->allocated && node->color != color)
  1660. *end -= 4096;
  1661. }
  1662. }
  1663. static int i915_gem_setup_global_gtt(struct drm_device *dev,
  1664. unsigned long start,
  1665. unsigned long mappable_end,
  1666. unsigned long end)
  1667. {
  1668. /* Let GEM Manage all of the aperture.
  1669. *
  1670. * However, leave one page at the end still bound to the scratch page.
  1671. * There are a number of places where the hardware apparently prefetches
  1672. * past the end of the object, and we've seen multiple hangs with the
  1673. * GPU head pointer stuck in a batchbuffer bound at the last page of the
  1674. * aperture. One page should be enough to keep any prefetching inside
  1675. * of the aperture.
  1676. */
  1677. struct drm_i915_private *dev_priv = dev->dev_private;
  1678. struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
  1679. struct drm_mm_node *entry;
  1680. struct drm_i915_gem_object *obj;
  1681. unsigned long hole_start, hole_end;
  1682. int ret;
  1683. BUG_ON(mappable_end > end);
  1684. /* Subtract the guard page ... */
  1685. drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
  1686. dev_priv->gtt.base.start = start;
  1687. dev_priv->gtt.base.total = end - start;
  1688. if (intel_vgpu_active(dev)) {
  1689. ret = intel_vgt_balloon(dev);
  1690. if (ret)
  1691. return ret;
  1692. }
  1693. if (!HAS_LLC(dev))
  1694. dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
  1695. /* Mark any preallocated objects as occupied */
  1696. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  1697. struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
  1698. DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
  1699. i915_gem_obj_ggtt_offset(obj), obj->base.size);
  1700. WARN_ON(i915_gem_obj_ggtt_bound(obj));
  1701. ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
  1702. if (ret) {
  1703. DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
  1704. return ret;
  1705. }
  1706. vma->bound |= GLOBAL_BIND;
  1707. }
  1708. /* Clear any non-preallocated blocks */
  1709. drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
  1710. DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
  1711. hole_start, hole_end);
  1712. ggtt_vm->clear_range(ggtt_vm, hole_start,
  1713. hole_end - hole_start, true);
  1714. }
  1715. /* And finally clear the reserved guard page */
  1716. ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
  1717. if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
  1718. struct i915_hw_ppgtt *ppgtt;
  1719. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  1720. if (!ppgtt)
  1721. return -ENOMEM;
  1722. ret = __hw_ppgtt_init(dev, ppgtt, true);
  1723. if (ret) {
  1724. kfree(ppgtt);
  1725. return ret;
  1726. }
  1727. dev_priv->mm.aliasing_ppgtt = ppgtt;
  1728. }
  1729. return 0;
  1730. }
  1731. void i915_gem_init_global_gtt(struct drm_device *dev)
  1732. {
  1733. struct drm_i915_private *dev_priv = dev->dev_private;
  1734. unsigned long gtt_size, mappable_size;
  1735. gtt_size = dev_priv->gtt.base.total;
  1736. mappable_size = dev_priv->gtt.mappable_end;
  1737. i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
  1738. }
  1739. void i915_global_gtt_cleanup(struct drm_device *dev)
  1740. {
  1741. struct drm_i915_private *dev_priv = dev->dev_private;
  1742. struct i915_address_space *vm = &dev_priv->gtt.base;
  1743. if (dev_priv->mm.aliasing_ppgtt) {
  1744. struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
  1745. ppgtt->base.cleanup(&ppgtt->base);
  1746. }
  1747. if (drm_mm_initialized(&vm->mm)) {
  1748. if (intel_vgpu_active(dev))
  1749. intel_vgt_deballoon();
  1750. drm_mm_takedown(&vm->mm);
  1751. list_del(&vm->global_link);
  1752. }
  1753. vm->cleanup(vm);
  1754. }
  1755. static int setup_scratch_page(struct drm_device *dev)
  1756. {
  1757. struct drm_i915_private *dev_priv = dev->dev_private;
  1758. struct page *page;
  1759. dma_addr_t dma_addr;
  1760. page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
  1761. if (page == NULL)
  1762. return -ENOMEM;
  1763. set_pages_uc(page, 1);
  1764. #ifdef CONFIG_INTEL_IOMMU
  1765. dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
  1766. PCI_DMA_BIDIRECTIONAL);
  1767. if (pci_dma_mapping_error(dev->pdev, dma_addr))
  1768. return -EINVAL;
  1769. #else
  1770. dma_addr = page_to_phys(page);
  1771. #endif
  1772. dev_priv->gtt.base.scratch.page = page;
  1773. dev_priv->gtt.base.scratch.addr = dma_addr;
  1774. return 0;
  1775. }
  1776. static void teardown_scratch_page(struct drm_device *dev)
  1777. {
  1778. struct drm_i915_private *dev_priv = dev->dev_private;
  1779. struct page *page = dev_priv->gtt.base.scratch.page;
  1780. set_pages_wb(page, 1);
  1781. pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
  1782. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  1783. __free_page(page);
  1784. }
  1785. static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
  1786. {
  1787. snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
  1788. snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
  1789. return snb_gmch_ctl << 20;
  1790. }
  1791. static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
  1792. {
  1793. bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
  1794. bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
  1795. if (bdw_gmch_ctl)
  1796. bdw_gmch_ctl = 1 << bdw_gmch_ctl;
  1797. #ifdef CONFIG_X86_32
  1798. /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
  1799. if (bdw_gmch_ctl > 4)
  1800. bdw_gmch_ctl = 4;
  1801. #endif
  1802. return bdw_gmch_ctl << 20;
  1803. }
  1804. static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
  1805. {
  1806. gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
  1807. gmch_ctrl &= SNB_GMCH_GGMS_MASK;
  1808. if (gmch_ctrl)
  1809. return 1 << (20 + gmch_ctrl);
  1810. return 0;
  1811. }
  1812. static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
  1813. {
  1814. snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
  1815. snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
  1816. return snb_gmch_ctl << 25; /* 32 MB units */
  1817. }
  1818. static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
  1819. {
  1820. bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  1821. bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
  1822. return bdw_gmch_ctl << 25; /* 32 MB units */
  1823. }
  1824. static size_t chv_get_stolen_size(u16 gmch_ctrl)
  1825. {
  1826. gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
  1827. gmch_ctrl &= SNB_GMCH_GMS_MASK;
  1828. /*
  1829. * 0x0 to 0x10: 32MB increments starting at 0MB
  1830. * 0x11 to 0x16: 4MB increments starting at 8MB
  1831. * 0x17 to 0x1d: 4MB increments start at 36MB
  1832. */
  1833. if (gmch_ctrl < 0x11)
  1834. return gmch_ctrl << 25;
  1835. else if (gmch_ctrl < 0x17)
  1836. return (gmch_ctrl - 0x11 + 2) << 22;
  1837. else
  1838. return (gmch_ctrl - 0x17 + 9) << 22;
  1839. }
  1840. static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
  1841. {
  1842. gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  1843. gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
  1844. if (gen9_gmch_ctl < 0xf0)
  1845. return gen9_gmch_ctl << 25; /* 32 MB units */
  1846. else
  1847. /* 4MB increments starting at 0xf0 for 4MB */
  1848. return (gen9_gmch_ctl - 0xf0 + 1) << 22;
  1849. }
  1850. static int ggtt_probe_common(struct drm_device *dev,
  1851. size_t gtt_size)
  1852. {
  1853. struct drm_i915_private *dev_priv = dev->dev_private;
  1854. phys_addr_t gtt_phys_addr;
  1855. int ret;
  1856. /* For Modern GENs the PTEs and register space are split in the BAR */
  1857. gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
  1858. (pci_resource_len(dev->pdev, 0) / 2);
  1859. dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
  1860. if (!dev_priv->gtt.gsm) {
  1861. DRM_ERROR("Failed to map the gtt page table\n");
  1862. return -ENOMEM;
  1863. }
  1864. ret = setup_scratch_page(dev);
  1865. if (ret) {
  1866. DRM_ERROR("Scratch setup failed\n");
  1867. /* iounmap will also get called at remove, but meh */
  1868. iounmap(dev_priv->gtt.gsm);
  1869. }
  1870. return ret;
  1871. }
  1872. /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
  1873. * bits. When using advanced contexts each context stores its own PAT, but
  1874. * writing this data shouldn't be harmful even in those cases. */
  1875. static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
  1876. {
  1877. uint64_t pat;
  1878. pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
  1879. GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
  1880. GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
  1881. GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
  1882. GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
  1883. GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
  1884. GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
  1885. GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
  1886. if (!USES_PPGTT(dev_priv->dev))
  1887. /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
  1888. * so RTL will always use the value corresponding to
  1889. * pat_sel = 000".
  1890. * So let's disable cache for GGTT to avoid screen corruptions.
  1891. * MOCS still can be used though.
  1892. * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
  1893. * before this patch, i.e. the same uncached + snooping access
  1894. * like on gen6/7 seems to be in effect.
  1895. * - So this just fixes blitter/render access. Again it looks
  1896. * like it's not just uncached access, but uncached + snooping.
  1897. * So we can still hold onto all our assumptions wrt cpu
  1898. * clflushing on LLC machines.
  1899. */
  1900. pat = GEN8_PPAT(0, GEN8_PPAT_UC);
  1901. /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
  1902. * write would work. */
  1903. I915_WRITE(GEN8_PRIVATE_PAT, pat);
  1904. I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
  1905. }
  1906. static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
  1907. {
  1908. uint64_t pat;
  1909. /*
  1910. * Map WB on BDW to snooped on CHV.
  1911. *
  1912. * Only the snoop bit has meaning for CHV, the rest is
  1913. * ignored.
  1914. *
  1915. * The hardware will never snoop for certain types of accesses:
  1916. * - CPU GTT (GMADR->GGTT->no snoop->memory)
  1917. * - PPGTT page tables
  1918. * - some other special cycles
  1919. *
  1920. * As with BDW, we also need to consider the following for GT accesses:
  1921. * "For GGTT, there is NO pat_sel[2:0] from the entry,
  1922. * so RTL will always use the value corresponding to
  1923. * pat_sel = 000".
  1924. * Which means we must set the snoop bit in PAT entry 0
  1925. * in order to keep the global status page working.
  1926. */
  1927. pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
  1928. GEN8_PPAT(1, 0) |
  1929. GEN8_PPAT(2, 0) |
  1930. GEN8_PPAT(3, 0) |
  1931. GEN8_PPAT(4, CHV_PPAT_SNOOP) |
  1932. GEN8_PPAT(5, CHV_PPAT_SNOOP) |
  1933. GEN8_PPAT(6, CHV_PPAT_SNOOP) |
  1934. GEN8_PPAT(7, CHV_PPAT_SNOOP);
  1935. I915_WRITE(GEN8_PRIVATE_PAT, pat);
  1936. I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
  1937. }
  1938. static int gen8_gmch_probe(struct drm_device *dev,
  1939. size_t *gtt_total,
  1940. size_t *stolen,
  1941. phys_addr_t *mappable_base,
  1942. unsigned long *mappable_end)
  1943. {
  1944. struct drm_i915_private *dev_priv = dev->dev_private;
  1945. unsigned int gtt_size;
  1946. u16 snb_gmch_ctl;
  1947. int ret;
  1948. /* TODO: We're not aware of mappable constraints on gen8 yet */
  1949. *mappable_base = pci_resource_start(dev->pdev, 2);
  1950. *mappable_end = pci_resource_len(dev->pdev, 2);
  1951. if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
  1952. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
  1953. pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  1954. if (INTEL_INFO(dev)->gen >= 9) {
  1955. *stolen = gen9_get_stolen_size(snb_gmch_ctl);
  1956. gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
  1957. } else if (IS_CHERRYVIEW(dev)) {
  1958. *stolen = chv_get_stolen_size(snb_gmch_ctl);
  1959. gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
  1960. } else {
  1961. *stolen = gen8_get_stolen_size(snb_gmch_ctl);
  1962. gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
  1963. }
  1964. *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
  1965. if (IS_CHERRYVIEW(dev))
  1966. chv_setup_private_ppat(dev_priv);
  1967. else
  1968. bdw_setup_private_ppat(dev_priv);
  1969. ret = ggtt_probe_common(dev, gtt_size);
  1970. dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
  1971. dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
  1972. return ret;
  1973. }
  1974. static int gen6_gmch_probe(struct drm_device *dev,
  1975. size_t *gtt_total,
  1976. size_t *stolen,
  1977. phys_addr_t *mappable_base,
  1978. unsigned long *mappable_end)
  1979. {
  1980. struct drm_i915_private *dev_priv = dev->dev_private;
  1981. unsigned int gtt_size;
  1982. u16 snb_gmch_ctl;
  1983. int ret;
  1984. *mappable_base = pci_resource_start(dev->pdev, 2);
  1985. *mappable_end = pci_resource_len(dev->pdev, 2);
  1986. /* 64/512MB is the current min/max we actually know of, but this is just
  1987. * a coarse sanity check.
  1988. */
  1989. if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
  1990. DRM_ERROR("Unknown GMADR size (%lx)\n",
  1991. dev_priv->gtt.mappable_end);
  1992. return -ENXIO;
  1993. }
  1994. if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
  1995. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
  1996. pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  1997. *stolen = gen6_get_stolen_size(snb_gmch_ctl);
  1998. gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
  1999. *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
  2000. ret = ggtt_probe_common(dev, gtt_size);
  2001. dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
  2002. dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
  2003. return ret;
  2004. }
  2005. static void gen6_gmch_remove(struct i915_address_space *vm)
  2006. {
  2007. struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
  2008. iounmap(gtt->gsm);
  2009. teardown_scratch_page(vm->dev);
  2010. }
  2011. static int i915_gmch_probe(struct drm_device *dev,
  2012. size_t *gtt_total,
  2013. size_t *stolen,
  2014. phys_addr_t *mappable_base,
  2015. unsigned long *mappable_end)
  2016. {
  2017. struct drm_i915_private *dev_priv = dev->dev_private;
  2018. int ret;
  2019. ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
  2020. if (!ret) {
  2021. DRM_ERROR("failed to set up gmch\n");
  2022. return -EIO;
  2023. }
  2024. intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
  2025. dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
  2026. dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
  2027. if (unlikely(dev_priv->gtt.do_idle_maps))
  2028. DRM_INFO("applying Ironlake quirks for intel_iommu\n");
  2029. return 0;
  2030. }
  2031. static void i915_gmch_remove(struct i915_address_space *vm)
  2032. {
  2033. intel_gmch_remove();
  2034. }
  2035. int i915_gem_gtt_init(struct drm_device *dev)
  2036. {
  2037. struct drm_i915_private *dev_priv = dev->dev_private;
  2038. struct i915_gtt *gtt = &dev_priv->gtt;
  2039. int ret;
  2040. if (INTEL_INFO(dev)->gen <= 5) {
  2041. gtt->gtt_probe = i915_gmch_probe;
  2042. gtt->base.cleanup = i915_gmch_remove;
  2043. } else if (INTEL_INFO(dev)->gen < 8) {
  2044. gtt->gtt_probe = gen6_gmch_probe;
  2045. gtt->base.cleanup = gen6_gmch_remove;
  2046. if (IS_HASWELL(dev) && dev_priv->ellc_size)
  2047. gtt->base.pte_encode = iris_pte_encode;
  2048. else if (IS_HASWELL(dev))
  2049. gtt->base.pte_encode = hsw_pte_encode;
  2050. else if (IS_VALLEYVIEW(dev))
  2051. gtt->base.pte_encode = byt_pte_encode;
  2052. else if (INTEL_INFO(dev)->gen >= 7)
  2053. gtt->base.pte_encode = ivb_pte_encode;
  2054. else
  2055. gtt->base.pte_encode = snb_pte_encode;
  2056. } else {
  2057. dev_priv->gtt.gtt_probe = gen8_gmch_probe;
  2058. dev_priv->gtt.base.cleanup = gen6_gmch_remove;
  2059. }
  2060. ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
  2061. &gtt->mappable_base, &gtt->mappable_end);
  2062. if (ret)
  2063. return ret;
  2064. gtt->base.dev = dev;
  2065. /* GMADR is the PCI mmio aperture into the global GTT. */
  2066. DRM_INFO("Memory usable by graphics device = %zdM\n",
  2067. gtt->base.total >> 20);
  2068. DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
  2069. DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
  2070. #ifdef CONFIG_INTEL_IOMMU
  2071. if (intel_iommu_gfx_mapped)
  2072. DRM_INFO("VT-d active for gfx access\n");
  2073. #endif
  2074. /*
  2075. * i915.enable_ppgtt is read-only, so do an early pass to validate the
  2076. * user's requested state against the hardware/driver capabilities. We
  2077. * do this now so that we can print out any log messages once rather
  2078. * than every time we check intel_enable_ppgtt().
  2079. */
  2080. i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
  2081. DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
  2082. return 0;
  2083. }
  2084. static struct i915_vma *
  2085. __i915_gem_vma_create(struct drm_i915_gem_object *obj,
  2086. struct i915_address_space *vm,
  2087. const struct i915_ggtt_view *ggtt_view)
  2088. {
  2089. struct i915_vma *vma;
  2090. if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
  2091. return ERR_PTR(-EINVAL);
  2092. vma = kzalloc(sizeof(*vma), GFP_KERNEL);
  2093. if (vma == NULL)
  2094. return ERR_PTR(-ENOMEM);
  2095. INIT_LIST_HEAD(&vma->vma_link);
  2096. INIT_LIST_HEAD(&vma->mm_list);
  2097. INIT_LIST_HEAD(&vma->exec_list);
  2098. vma->vm = vm;
  2099. vma->obj = obj;
  2100. if (INTEL_INFO(vm->dev)->gen >= 6) {
  2101. if (i915_is_ggtt(vm)) {
  2102. vma->ggtt_view = *ggtt_view;
  2103. vma->unbind_vma = ggtt_unbind_vma;
  2104. vma->bind_vma = ggtt_bind_vma;
  2105. } else {
  2106. vma->unbind_vma = ppgtt_unbind_vma;
  2107. vma->bind_vma = ppgtt_bind_vma;
  2108. }
  2109. } else {
  2110. BUG_ON(!i915_is_ggtt(vm));
  2111. vma->ggtt_view = *ggtt_view;
  2112. vma->unbind_vma = i915_ggtt_unbind_vma;
  2113. vma->bind_vma = i915_ggtt_bind_vma;
  2114. }
  2115. list_add_tail(&vma->vma_link, &obj->vma_list);
  2116. if (!i915_is_ggtt(vm))
  2117. i915_ppgtt_get(i915_vm_to_ppgtt(vm));
  2118. return vma;
  2119. }
  2120. struct i915_vma *
  2121. i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
  2122. struct i915_address_space *vm)
  2123. {
  2124. struct i915_vma *vma;
  2125. vma = i915_gem_obj_to_vma(obj, vm);
  2126. if (!vma)
  2127. vma = __i915_gem_vma_create(obj, vm,
  2128. i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
  2129. return vma;
  2130. }
  2131. struct i915_vma *
  2132. i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
  2133. const struct i915_ggtt_view *view)
  2134. {
  2135. struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
  2136. struct i915_vma *vma;
  2137. if (WARN_ON(!view))
  2138. return ERR_PTR(-EINVAL);
  2139. vma = i915_gem_obj_to_ggtt_view(obj, view);
  2140. if (IS_ERR(vma))
  2141. return vma;
  2142. if (!vma)
  2143. vma = __i915_gem_vma_create(obj, ggtt, view);
  2144. return vma;
  2145. }
  2146. static void
  2147. rotate_pages(dma_addr_t *in, unsigned int width, unsigned int height,
  2148. struct sg_table *st)
  2149. {
  2150. unsigned int column, row;
  2151. unsigned int src_idx;
  2152. struct scatterlist *sg = st->sgl;
  2153. st->nents = 0;
  2154. for (column = 0; column < width; column++) {
  2155. src_idx = width * (height - 1) + column;
  2156. for (row = 0; row < height; row++) {
  2157. st->nents++;
  2158. /* We don't need the pages, but need to initialize
  2159. * the entries so the sg list can be happily traversed.
  2160. * The only thing we need are DMA addresses.
  2161. */
  2162. sg_set_page(sg, NULL, PAGE_SIZE, 0);
  2163. sg_dma_address(sg) = in[src_idx];
  2164. sg_dma_len(sg) = PAGE_SIZE;
  2165. sg = sg_next(sg);
  2166. src_idx -= width;
  2167. }
  2168. }
  2169. }
  2170. static struct sg_table *
  2171. intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
  2172. struct drm_i915_gem_object *obj)
  2173. {
  2174. struct drm_device *dev = obj->base.dev;
  2175. struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
  2176. unsigned long size, pages, rot_pages;
  2177. struct sg_page_iter sg_iter;
  2178. unsigned long i;
  2179. dma_addr_t *page_addr_list;
  2180. struct sg_table *st;
  2181. unsigned int tile_pitch, tile_height;
  2182. unsigned int width_pages, height_pages;
  2183. int ret = -ENOMEM;
  2184. pages = obj->base.size / PAGE_SIZE;
  2185. /* Calculate tiling geometry. */
  2186. tile_height = intel_tile_height(dev, rot_info->pixel_format,
  2187. rot_info->fb_modifier);
  2188. tile_pitch = PAGE_SIZE / tile_height;
  2189. width_pages = DIV_ROUND_UP(rot_info->pitch, tile_pitch);
  2190. height_pages = DIV_ROUND_UP(rot_info->height, tile_height);
  2191. rot_pages = width_pages * height_pages;
  2192. size = rot_pages * PAGE_SIZE;
  2193. /* Allocate a temporary list of source pages for random access. */
  2194. page_addr_list = drm_malloc_ab(pages, sizeof(dma_addr_t));
  2195. if (!page_addr_list)
  2196. return ERR_PTR(ret);
  2197. /* Allocate target SG list. */
  2198. st = kmalloc(sizeof(*st), GFP_KERNEL);
  2199. if (!st)
  2200. goto err_st_alloc;
  2201. ret = sg_alloc_table(st, rot_pages, GFP_KERNEL);
  2202. if (ret)
  2203. goto err_sg_alloc;
  2204. /* Populate source page list from the object. */
  2205. i = 0;
  2206. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  2207. page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
  2208. i++;
  2209. }
  2210. /* Rotate the pages. */
  2211. rotate_pages(page_addr_list, width_pages, height_pages, st);
  2212. DRM_DEBUG_KMS(
  2213. "Created rotated page mapping for object size %lu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages).\n",
  2214. size, rot_info->pitch, rot_info->height,
  2215. rot_info->pixel_format, width_pages, height_pages,
  2216. rot_pages);
  2217. drm_free_large(page_addr_list);
  2218. return st;
  2219. err_sg_alloc:
  2220. kfree(st);
  2221. err_st_alloc:
  2222. drm_free_large(page_addr_list);
  2223. DRM_DEBUG_KMS(
  2224. "Failed to create rotated mapping for object size %lu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages)\n",
  2225. size, ret, rot_info->pitch, rot_info->height,
  2226. rot_info->pixel_format, width_pages, height_pages,
  2227. rot_pages);
  2228. return ERR_PTR(ret);
  2229. }
  2230. static inline int
  2231. i915_get_ggtt_vma_pages(struct i915_vma *vma)
  2232. {
  2233. int ret = 0;
  2234. if (vma->ggtt_view.pages)
  2235. return 0;
  2236. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
  2237. vma->ggtt_view.pages = vma->obj->pages;
  2238. else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
  2239. vma->ggtt_view.pages =
  2240. intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
  2241. else
  2242. WARN_ONCE(1, "GGTT view %u not implemented!\n",
  2243. vma->ggtt_view.type);
  2244. if (!vma->ggtt_view.pages) {
  2245. DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
  2246. vma->ggtt_view.type);
  2247. ret = -EINVAL;
  2248. } else if (IS_ERR(vma->ggtt_view.pages)) {
  2249. ret = PTR_ERR(vma->ggtt_view.pages);
  2250. vma->ggtt_view.pages = NULL;
  2251. DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
  2252. vma->ggtt_view.type, ret);
  2253. }
  2254. return ret;
  2255. }
  2256. /**
  2257. * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
  2258. * @vma: VMA to map
  2259. * @cache_level: mapping cache level
  2260. * @flags: flags like global or local mapping
  2261. *
  2262. * DMA addresses are taken from the scatter-gather table of this object (or of
  2263. * this VMA in case of non-default GGTT views) and PTE entries set up.
  2264. * Note that DMA addresses are also the only part of the SG table we care about.
  2265. */
  2266. int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
  2267. u32 flags)
  2268. {
  2269. if (i915_is_ggtt(vma->vm)) {
  2270. int ret = i915_get_ggtt_vma_pages(vma);
  2271. if (ret)
  2272. return ret;
  2273. }
  2274. vma->bind_vma(vma, cache_level, flags);
  2275. return 0;
  2276. }