i915_gem_gtt.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198
  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_trace.h"
  30. #include "intel_drv.h"
  31. static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv);
  32. static void chv_setup_private_ppat(struct drm_i915_private *dev_priv);
  33. static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
  34. {
  35. if (enable_ppgtt == 0 || !HAS_ALIASING_PPGTT(dev))
  36. return 0;
  37. if (enable_ppgtt == 1)
  38. return 1;
  39. if (enable_ppgtt == 2 && HAS_PPGTT(dev))
  40. return 2;
  41. #ifdef CONFIG_INTEL_IOMMU
  42. /* Disable ppgtt on SNB if VT-d is on. */
  43. if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
  44. DRM_INFO("Disabling PPGTT because VT-d is on\n");
  45. return 0;
  46. }
  47. #endif
  48. /* Early VLV doesn't have this */
  49. if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  50. dev->pdev->revision < 0xb) {
  51. DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
  52. return 0;
  53. }
  54. return HAS_ALIASING_PPGTT(dev) ? 1 : 0;
  55. }
  56. static void ppgtt_bind_vma(struct i915_vma *vma,
  57. enum i915_cache_level cache_level,
  58. u32 flags);
  59. static void ppgtt_unbind_vma(struct i915_vma *vma);
  60. static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt);
  61. static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr,
  62. enum i915_cache_level level,
  63. bool valid)
  64. {
  65. gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
  66. pte |= addr;
  67. switch (level) {
  68. case I915_CACHE_NONE:
  69. pte |= PPAT_UNCACHED_INDEX;
  70. break;
  71. case I915_CACHE_WT:
  72. pte |= PPAT_DISPLAY_ELLC_INDEX;
  73. break;
  74. default:
  75. pte |= PPAT_CACHED_INDEX;
  76. break;
  77. }
  78. return pte;
  79. }
  80. static inline gen8_ppgtt_pde_t gen8_pde_encode(struct drm_device *dev,
  81. dma_addr_t addr,
  82. enum i915_cache_level level)
  83. {
  84. gen8_ppgtt_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
  85. pde |= addr;
  86. if (level != I915_CACHE_NONE)
  87. pde |= PPAT_CACHED_PDE_INDEX;
  88. else
  89. pde |= PPAT_UNCACHED_INDEX;
  90. return pde;
  91. }
  92. static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr,
  93. enum i915_cache_level level,
  94. bool valid, u32 unused)
  95. {
  96. gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  97. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  98. switch (level) {
  99. case I915_CACHE_L3_LLC:
  100. case I915_CACHE_LLC:
  101. pte |= GEN6_PTE_CACHE_LLC;
  102. break;
  103. case I915_CACHE_NONE:
  104. pte |= GEN6_PTE_UNCACHED;
  105. break;
  106. default:
  107. WARN_ON(1);
  108. }
  109. return pte;
  110. }
  111. static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr,
  112. enum i915_cache_level level,
  113. bool valid, u32 unused)
  114. {
  115. gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  116. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  117. switch (level) {
  118. case I915_CACHE_L3_LLC:
  119. pte |= GEN7_PTE_CACHE_L3_LLC;
  120. break;
  121. case I915_CACHE_LLC:
  122. pte |= GEN6_PTE_CACHE_LLC;
  123. break;
  124. case I915_CACHE_NONE:
  125. pte |= GEN6_PTE_UNCACHED;
  126. break;
  127. default:
  128. WARN_ON(1);
  129. }
  130. return pte;
  131. }
  132. static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr,
  133. enum i915_cache_level level,
  134. bool valid, u32 flags)
  135. {
  136. gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  137. pte |= GEN6_PTE_ADDR_ENCODE(addr);
  138. /* Mark the page as writeable. Other platforms don't have a
  139. * setting for read-only/writable, so this matches that behavior.
  140. */
  141. if (!(flags & PTE_READ_ONLY))
  142. pte |= BYT_PTE_WRITEABLE;
  143. if (level != I915_CACHE_NONE)
  144. pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
  145. return pte;
  146. }
  147. static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr,
  148. enum i915_cache_level level,
  149. bool valid, u32 unused)
  150. {
  151. gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  152. pte |= HSW_PTE_ADDR_ENCODE(addr);
  153. if (level != I915_CACHE_NONE)
  154. pte |= HSW_WB_LLC_AGE3;
  155. return pte;
  156. }
  157. static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr,
  158. enum i915_cache_level level,
  159. bool valid, u32 unused)
  160. {
  161. gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0;
  162. pte |= HSW_PTE_ADDR_ENCODE(addr);
  163. switch (level) {
  164. case I915_CACHE_NONE:
  165. break;
  166. case I915_CACHE_WT:
  167. pte |= HSW_WT_ELLC_LLC_AGE3;
  168. break;
  169. default:
  170. pte |= HSW_WB_ELLC_LLC_AGE3;
  171. break;
  172. }
  173. return pte;
  174. }
  175. /* Broadwell Page Directory Pointer Descriptors */
  176. static int gen8_write_pdp(struct intel_engine_cs *ring, unsigned entry,
  177. uint64_t val, bool synchronous)
  178. {
  179. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  180. int ret;
  181. BUG_ON(entry >= 4);
  182. if (synchronous) {
  183. I915_WRITE(GEN8_RING_PDP_UDW(ring, entry), val >> 32);
  184. I915_WRITE(GEN8_RING_PDP_LDW(ring, entry), (u32)val);
  185. return 0;
  186. }
  187. ret = intel_ring_begin(ring, 6);
  188. if (ret)
  189. return ret;
  190. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  191. intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
  192. intel_ring_emit(ring, (u32)(val >> 32));
  193. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  194. intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
  195. intel_ring_emit(ring, (u32)(val));
  196. intel_ring_advance(ring);
  197. return 0;
  198. }
  199. static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt,
  200. struct intel_engine_cs *ring,
  201. bool synchronous)
  202. {
  203. int i, ret;
  204. /* bit of a hack to find the actual last used pd */
  205. int used_pd = ppgtt->num_pd_entries / GEN8_PDES_PER_PAGE;
  206. for (i = used_pd - 1; i >= 0; i--) {
  207. dma_addr_t addr = ppgtt->pd_dma_addr[i];
  208. ret = gen8_write_pdp(ring, i, addr, synchronous);
  209. if (ret)
  210. return ret;
  211. }
  212. return 0;
  213. }
  214. static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
  215. uint64_t start,
  216. uint64_t length,
  217. bool use_scratch)
  218. {
  219. struct i915_hw_ppgtt *ppgtt =
  220. container_of(vm, struct i915_hw_ppgtt, base);
  221. gen8_gtt_pte_t *pt_vaddr, scratch_pte;
  222. unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
  223. unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
  224. unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
  225. unsigned num_entries = length >> PAGE_SHIFT;
  226. unsigned last_pte, i;
  227. scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr,
  228. I915_CACHE_LLC, use_scratch);
  229. while (num_entries) {
  230. struct page *page_table = ppgtt->gen8_pt_pages[pdpe][pde];
  231. last_pte = pte + num_entries;
  232. if (last_pte > GEN8_PTES_PER_PAGE)
  233. last_pte = GEN8_PTES_PER_PAGE;
  234. pt_vaddr = kmap_atomic(page_table);
  235. for (i = pte; i < last_pte; i++) {
  236. pt_vaddr[i] = scratch_pte;
  237. num_entries--;
  238. }
  239. if (!HAS_LLC(ppgtt->base.dev))
  240. drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
  241. kunmap_atomic(pt_vaddr);
  242. pte = 0;
  243. if (++pde == GEN8_PDES_PER_PAGE) {
  244. pdpe++;
  245. pde = 0;
  246. }
  247. }
  248. }
  249. static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
  250. struct sg_table *pages,
  251. uint64_t start,
  252. enum i915_cache_level cache_level, u32 unused)
  253. {
  254. struct i915_hw_ppgtt *ppgtt =
  255. container_of(vm, struct i915_hw_ppgtt, base);
  256. gen8_gtt_pte_t *pt_vaddr;
  257. unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK;
  258. unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK;
  259. unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK;
  260. struct sg_page_iter sg_iter;
  261. pt_vaddr = NULL;
  262. for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
  263. if (WARN_ON(pdpe >= GEN8_LEGACY_PDPS))
  264. break;
  265. if (pt_vaddr == NULL)
  266. pt_vaddr = kmap_atomic(ppgtt->gen8_pt_pages[pdpe][pde]);
  267. pt_vaddr[pte] =
  268. gen8_pte_encode(sg_page_iter_dma_address(&sg_iter),
  269. cache_level, true);
  270. if (++pte == GEN8_PTES_PER_PAGE) {
  271. if (!HAS_LLC(ppgtt->base.dev))
  272. drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
  273. kunmap_atomic(pt_vaddr);
  274. pt_vaddr = NULL;
  275. if (++pde == GEN8_PDES_PER_PAGE) {
  276. pdpe++;
  277. pde = 0;
  278. }
  279. pte = 0;
  280. }
  281. }
  282. if (pt_vaddr) {
  283. if (!HAS_LLC(ppgtt->base.dev))
  284. drm_clflush_virt_range(pt_vaddr, PAGE_SIZE);
  285. kunmap_atomic(pt_vaddr);
  286. }
  287. }
  288. static void gen8_free_page_tables(struct page **pt_pages)
  289. {
  290. int i;
  291. if (pt_pages == NULL)
  292. return;
  293. for (i = 0; i < GEN8_PDES_PER_PAGE; i++)
  294. if (pt_pages[i])
  295. __free_pages(pt_pages[i], 0);
  296. }
  297. static void gen8_ppgtt_free(const struct i915_hw_ppgtt *ppgtt)
  298. {
  299. int i;
  300. for (i = 0; i < ppgtt->num_pd_pages; i++) {
  301. gen8_free_page_tables(ppgtt->gen8_pt_pages[i]);
  302. kfree(ppgtt->gen8_pt_pages[i]);
  303. kfree(ppgtt->gen8_pt_dma_addr[i]);
  304. }
  305. __free_pages(ppgtt->pd_pages, get_order(ppgtt->num_pd_pages << PAGE_SHIFT));
  306. }
  307. static void gen8_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
  308. {
  309. struct pci_dev *hwdev = ppgtt->base.dev->pdev;
  310. int i, j;
  311. for (i = 0; i < ppgtt->num_pd_pages; i++) {
  312. /* TODO: In the future we'll support sparse mappings, so this
  313. * will have to change. */
  314. if (!ppgtt->pd_dma_addr[i])
  315. continue;
  316. pci_unmap_page(hwdev, ppgtt->pd_dma_addr[i], PAGE_SIZE,
  317. PCI_DMA_BIDIRECTIONAL);
  318. for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
  319. dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
  320. if (addr)
  321. pci_unmap_page(hwdev, addr, PAGE_SIZE,
  322. PCI_DMA_BIDIRECTIONAL);
  323. }
  324. }
  325. }
  326. static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
  327. {
  328. struct i915_hw_ppgtt *ppgtt =
  329. container_of(vm, struct i915_hw_ppgtt, base);
  330. list_del(&vm->global_link);
  331. drm_mm_takedown(&vm->mm);
  332. gen8_ppgtt_unmap_pages(ppgtt);
  333. gen8_ppgtt_free(ppgtt);
  334. }
  335. static struct page **__gen8_alloc_page_tables(void)
  336. {
  337. struct page **pt_pages;
  338. int i;
  339. pt_pages = kcalloc(GEN8_PDES_PER_PAGE, sizeof(struct page *), GFP_KERNEL);
  340. if (!pt_pages)
  341. return ERR_PTR(-ENOMEM);
  342. for (i = 0; i < GEN8_PDES_PER_PAGE; i++) {
  343. pt_pages[i] = alloc_page(GFP_KERNEL);
  344. if (!pt_pages[i])
  345. goto bail;
  346. }
  347. return pt_pages;
  348. bail:
  349. gen8_free_page_tables(pt_pages);
  350. kfree(pt_pages);
  351. return ERR_PTR(-ENOMEM);
  352. }
  353. static int gen8_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt,
  354. const int max_pdp)
  355. {
  356. struct page **pt_pages[GEN8_LEGACY_PDPS];
  357. int i, ret;
  358. for (i = 0; i < max_pdp; i++) {
  359. pt_pages[i] = __gen8_alloc_page_tables();
  360. if (IS_ERR(pt_pages[i])) {
  361. ret = PTR_ERR(pt_pages[i]);
  362. goto unwind_out;
  363. }
  364. }
  365. /* NB: Avoid touching gen8_pt_pages until last to keep the allocation,
  366. * "atomic" - for cleanup purposes.
  367. */
  368. for (i = 0; i < max_pdp; i++)
  369. ppgtt->gen8_pt_pages[i] = pt_pages[i];
  370. return 0;
  371. unwind_out:
  372. while (i--) {
  373. gen8_free_page_tables(pt_pages[i]);
  374. kfree(pt_pages[i]);
  375. }
  376. return ret;
  377. }
  378. static int gen8_ppgtt_allocate_dma(struct i915_hw_ppgtt *ppgtt)
  379. {
  380. int i;
  381. for (i = 0; i < ppgtt->num_pd_pages; i++) {
  382. ppgtt->gen8_pt_dma_addr[i] = kcalloc(GEN8_PDES_PER_PAGE,
  383. sizeof(dma_addr_t),
  384. GFP_KERNEL);
  385. if (!ppgtt->gen8_pt_dma_addr[i])
  386. return -ENOMEM;
  387. }
  388. return 0;
  389. }
  390. static int gen8_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt,
  391. const int max_pdp)
  392. {
  393. ppgtt->pd_pages = alloc_pages(GFP_KERNEL, get_order(max_pdp << PAGE_SHIFT));
  394. if (!ppgtt->pd_pages)
  395. return -ENOMEM;
  396. ppgtt->num_pd_pages = 1 << get_order(max_pdp << PAGE_SHIFT);
  397. BUG_ON(ppgtt->num_pd_pages > GEN8_LEGACY_PDPS);
  398. return 0;
  399. }
  400. static int gen8_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt,
  401. const int max_pdp)
  402. {
  403. int ret;
  404. ret = gen8_ppgtt_allocate_page_directories(ppgtt, max_pdp);
  405. if (ret)
  406. return ret;
  407. ret = gen8_ppgtt_allocate_page_tables(ppgtt, max_pdp);
  408. if (ret) {
  409. __free_pages(ppgtt->pd_pages, get_order(max_pdp << PAGE_SHIFT));
  410. return ret;
  411. }
  412. ppgtt->num_pd_entries = max_pdp * GEN8_PDES_PER_PAGE;
  413. ret = gen8_ppgtt_allocate_dma(ppgtt);
  414. if (ret)
  415. gen8_ppgtt_free(ppgtt);
  416. return ret;
  417. }
  418. static int gen8_ppgtt_setup_page_directories(struct i915_hw_ppgtt *ppgtt,
  419. const int pd)
  420. {
  421. dma_addr_t pd_addr;
  422. int ret;
  423. pd_addr = pci_map_page(ppgtt->base.dev->pdev,
  424. &ppgtt->pd_pages[pd], 0,
  425. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  426. ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pd_addr);
  427. if (ret)
  428. return ret;
  429. ppgtt->pd_dma_addr[pd] = pd_addr;
  430. return 0;
  431. }
  432. static int gen8_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt,
  433. const int pd,
  434. const int pt)
  435. {
  436. dma_addr_t pt_addr;
  437. struct page *p;
  438. int ret;
  439. p = ppgtt->gen8_pt_pages[pd][pt];
  440. pt_addr = pci_map_page(ppgtt->base.dev->pdev,
  441. p, 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  442. ret = pci_dma_mapping_error(ppgtt->base.dev->pdev, pt_addr);
  443. if (ret)
  444. return ret;
  445. ppgtt->gen8_pt_dma_addr[pd][pt] = pt_addr;
  446. return 0;
  447. }
  448. /**
  449. * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
  450. * with a net effect resembling a 2-level page table in normal x86 terms. Each
  451. * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
  452. * space.
  453. *
  454. * FIXME: split allocation into smaller pieces. For now we only ever do this
  455. * once, but with full PPGTT, the multiple contiguous allocations will be bad.
  456. * TODO: Do something with the size parameter
  457. */
  458. static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt, uint64_t size)
  459. {
  460. const int max_pdp = DIV_ROUND_UP(size, 1 << 30);
  461. const int min_pt_pages = GEN8_PDES_PER_PAGE * max_pdp;
  462. int i, j, ret;
  463. if (size % (1<<30))
  464. DRM_INFO("Pages will be wasted unless GTT size (%llu) is divisible by 1GB\n", size);
  465. /* 1. Do all our allocations for page directories and page tables. */
  466. ret = gen8_ppgtt_alloc(ppgtt, max_pdp);
  467. if (ret)
  468. return ret;
  469. /*
  470. * 2. Create DMA mappings for the page directories and page tables.
  471. */
  472. for (i = 0; i < max_pdp; i++) {
  473. ret = gen8_ppgtt_setup_page_directories(ppgtt, i);
  474. if (ret)
  475. goto bail;
  476. for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
  477. ret = gen8_ppgtt_setup_page_tables(ppgtt, i, j);
  478. if (ret)
  479. goto bail;
  480. }
  481. }
  482. /*
  483. * 3. Map all the page directory entires to point to the page tables
  484. * we've allocated.
  485. *
  486. * For now, the PPGTT helper functions all require that the PDEs are
  487. * plugged in correctly. So we do that now/here. For aliasing PPGTT, we
  488. * will never need to touch the PDEs again.
  489. */
  490. for (i = 0; i < max_pdp; i++) {
  491. gen8_ppgtt_pde_t *pd_vaddr;
  492. pd_vaddr = kmap_atomic(&ppgtt->pd_pages[i]);
  493. for (j = 0; j < GEN8_PDES_PER_PAGE; j++) {
  494. dma_addr_t addr = ppgtt->gen8_pt_dma_addr[i][j];
  495. pd_vaddr[j] = gen8_pde_encode(ppgtt->base.dev, addr,
  496. I915_CACHE_LLC);
  497. }
  498. if (!HAS_LLC(ppgtt->base.dev))
  499. drm_clflush_virt_range(pd_vaddr, PAGE_SIZE);
  500. kunmap_atomic(pd_vaddr);
  501. }
  502. ppgtt->enable = gen8_ppgtt_enable;
  503. ppgtt->switch_mm = gen8_mm_switch;
  504. ppgtt->base.clear_range = gen8_ppgtt_clear_range;
  505. ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
  506. ppgtt->base.cleanup = gen8_ppgtt_cleanup;
  507. ppgtt->base.start = 0;
  508. ppgtt->base.total = ppgtt->num_pd_entries * GEN8_PTES_PER_PAGE * PAGE_SIZE;
  509. ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
  510. DRM_DEBUG_DRIVER("Allocated %d pages for page directories (%d wasted)\n",
  511. ppgtt->num_pd_pages, ppgtt->num_pd_pages - max_pdp);
  512. DRM_DEBUG_DRIVER("Allocated %d pages for page tables (%lld wasted)\n",
  513. ppgtt->num_pd_entries,
  514. (ppgtt->num_pd_entries - min_pt_pages) + size % (1<<30));
  515. return 0;
  516. bail:
  517. gen8_ppgtt_unmap_pages(ppgtt);
  518. gen8_ppgtt_free(ppgtt);
  519. return ret;
  520. }
  521. static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
  522. {
  523. struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
  524. struct i915_address_space *vm = &ppgtt->base;
  525. gen6_gtt_pte_t __iomem *pd_addr;
  526. gen6_gtt_pte_t scratch_pte;
  527. uint32_t pd_entry;
  528. int pte, pde;
  529. scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
  530. pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm +
  531. ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
  532. seq_printf(m, " VM %p (pd_offset %x-%x):\n", vm,
  533. ppgtt->pd_offset, ppgtt->pd_offset + ppgtt->num_pd_entries);
  534. for (pde = 0; pde < ppgtt->num_pd_entries; pde++) {
  535. u32 expected;
  536. gen6_gtt_pte_t *pt_vaddr;
  537. dma_addr_t pt_addr = ppgtt->pt_dma_addr[pde];
  538. pd_entry = readl(pd_addr + pde);
  539. expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
  540. if (pd_entry != expected)
  541. seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
  542. pde,
  543. pd_entry,
  544. expected);
  545. seq_printf(m, "\tPDE: %x\n", pd_entry);
  546. pt_vaddr = kmap_atomic(ppgtt->pt_pages[pde]);
  547. for (pte = 0; pte < I915_PPGTT_PT_ENTRIES; pte+=4) {
  548. unsigned long va =
  549. (pde * PAGE_SIZE * I915_PPGTT_PT_ENTRIES) +
  550. (pte * PAGE_SIZE);
  551. int i;
  552. bool found = false;
  553. for (i = 0; i < 4; i++)
  554. if (pt_vaddr[pte + i] != scratch_pte)
  555. found = true;
  556. if (!found)
  557. continue;
  558. seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
  559. for (i = 0; i < 4; i++) {
  560. if (pt_vaddr[pte + i] != scratch_pte)
  561. seq_printf(m, " %08x", pt_vaddr[pte + i]);
  562. else
  563. seq_puts(m, " SCRATCH ");
  564. }
  565. seq_puts(m, "\n");
  566. }
  567. kunmap_atomic(pt_vaddr);
  568. }
  569. }
  570. static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt)
  571. {
  572. struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
  573. gen6_gtt_pte_t __iomem *pd_addr;
  574. uint32_t pd_entry;
  575. int i;
  576. WARN_ON(ppgtt->pd_offset & 0x3f);
  577. pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm +
  578. ppgtt->pd_offset / sizeof(gen6_gtt_pte_t);
  579. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  580. dma_addr_t pt_addr;
  581. pt_addr = ppgtt->pt_dma_addr[i];
  582. pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
  583. pd_entry |= GEN6_PDE_VALID;
  584. writel(pd_entry, pd_addr + i);
  585. }
  586. readl(pd_addr);
  587. }
  588. static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
  589. {
  590. BUG_ON(ppgtt->pd_offset & 0x3f);
  591. return (ppgtt->pd_offset / 64) << 16;
  592. }
  593. static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
  594. struct intel_engine_cs *ring,
  595. bool synchronous)
  596. {
  597. struct drm_device *dev = ppgtt->base.dev;
  598. struct drm_i915_private *dev_priv = dev->dev_private;
  599. int ret;
  600. /* If we're in reset, we can assume the GPU is sufficiently idle to
  601. * manually frob these bits. Ideally we could use the ring functions,
  602. * except our error handling makes it quite difficult (can't use
  603. * intel_ring_begin, ring->flush, or intel_ring_advance)
  604. *
  605. * FIXME: We should try not to special case reset
  606. */
  607. if (synchronous ||
  608. i915_reset_in_progress(&dev_priv->gpu_error)) {
  609. WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
  610. I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
  611. I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
  612. POSTING_READ(RING_PP_DIR_BASE(ring));
  613. return 0;
  614. }
  615. /* NB: TLBs must be flushed and invalidated before a switch */
  616. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  617. if (ret)
  618. return ret;
  619. ret = intel_ring_begin(ring, 6);
  620. if (ret)
  621. return ret;
  622. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
  623. intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
  624. intel_ring_emit(ring, PP_DIR_DCLV_2G);
  625. intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
  626. intel_ring_emit(ring, get_pd_offset(ppgtt));
  627. intel_ring_emit(ring, MI_NOOP);
  628. intel_ring_advance(ring);
  629. return 0;
  630. }
  631. static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
  632. struct intel_engine_cs *ring,
  633. bool synchronous)
  634. {
  635. struct drm_device *dev = ppgtt->base.dev;
  636. struct drm_i915_private *dev_priv = dev->dev_private;
  637. int ret;
  638. /* If we're in reset, we can assume the GPU is sufficiently idle to
  639. * manually frob these bits. Ideally we could use the ring functions,
  640. * except our error handling makes it quite difficult (can't use
  641. * intel_ring_begin, ring->flush, or intel_ring_advance)
  642. *
  643. * FIXME: We should try not to special case reset
  644. */
  645. if (synchronous ||
  646. i915_reset_in_progress(&dev_priv->gpu_error)) {
  647. WARN_ON(ppgtt != dev_priv->mm.aliasing_ppgtt);
  648. I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
  649. I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
  650. POSTING_READ(RING_PP_DIR_BASE(ring));
  651. return 0;
  652. }
  653. /* NB: TLBs must be flushed and invalidated before a switch */
  654. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  655. if (ret)
  656. return ret;
  657. ret = intel_ring_begin(ring, 6);
  658. if (ret)
  659. return ret;
  660. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
  661. intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
  662. intel_ring_emit(ring, PP_DIR_DCLV_2G);
  663. intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
  664. intel_ring_emit(ring, get_pd_offset(ppgtt));
  665. intel_ring_emit(ring, MI_NOOP);
  666. intel_ring_advance(ring);
  667. /* XXX: RCS is the only one to auto invalidate the TLBs? */
  668. if (ring->id != RCS) {
  669. ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
  670. if (ret)
  671. return ret;
  672. }
  673. return 0;
  674. }
  675. static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
  676. struct intel_engine_cs *ring,
  677. bool synchronous)
  678. {
  679. struct drm_device *dev = ppgtt->base.dev;
  680. struct drm_i915_private *dev_priv = dev->dev_private;
  681. if (!synchronous)
  682. return 0;
  683. I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
  684. I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
  685. POSTING_READ(RING_PP_DIR_DCLV(ring));
  686. return 0;
  687. }
  688. static int gen8_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
  689. {
  690. struct drm_device *dev = ppgtt->base.dev;
  691. struct drm_i915_private *dev_priv = dev->dev_private;
  692. struct intel_engine_cs *ring;
  693. int j, ret;
  694. for_each_ring(ring, dev_priv, j) {
  695. I915_WRITE(RING_MODE_GEN7(ring),
  696. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  697. /* We promise to do a switch later with FULL PPGTT. If this is
  698. * aliasing, this is the one and only switch we'll do */
  699. if (USES_FULL_PPGTT(dev))
  700. continue;
  701. ret = ppgtt->switch_mm(ppgtt, ring, true);
  702. if (ret)
  703. goto err_out;
  704. }
  705. return 0;
  706. err_out:
  707. for_each_ring(ring, dev_priv, j)
  708. I915_WRITE(RING_MODE_GEN7(ring),
  709. _MASKED_BIT_DISABLE(GFX_PPGTT_ENABLE));
  710. return ret;
  711. }
  712. static int gen7_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
  713. {
  714. struct drm_device *dev = ppgtt->base.dev;
  715. struct drm_i915_private *dev_priv = dev->dev_private;
  716. struct intel_engine_cs *ring;
  717. uint32_t ecochk, ecobits;
  718. int i;
  719. ecobits = I915_READ(GAC_ECO_BITS);
  720. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
  721. ecochk = I915_READ(GAM_ECOCHK);
  722. if (IS_HASWELL(dev)) {
  723. ecochk |= ECOCHK_PPGTT_WB_HSW;
  724. } else {
  725. ecochk |= ECOCHK_PPGTT_LLC_IVB;
  726. ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
  727. }
  728. I915_WRITE(GAM_ECOCHK, ecochk);
  729. for_each_ring(ring, dev_priv, i) {
  730. int ret;
  731. /* GFX_MODE is per-ring on gen7+ */
  732. I915_WRITE(RING_MODE_GEN7(ring),
  733. _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  734. /* We promise to do a switch later with FULL PPGTT. If this is
  735. * aliasing, this is the one and only switch we'll do */
  736. if (USES_FULL_PPGTT(dev))
  737. continue;
  738. ret = ppgtt->switch_mm(ppgtt, ring, true);
  739. if (ret)
  740. return ret;
  741. }
  742. return 0;
  743. }
  744. static int gen6_ppgtt_enable(struct i915_hw_ppgtt *ppgtt)
  745. {
  746. struct drm_device *dev = ppgtt->base.dev;
  747. struct drm_i915_private *dev_priv = dev->dev_private;
  748. struct intel_engine_cs *ring;
  749. uint32_t ecochk, gab_ctl, ecobits;
  750. int i;
  751. ecobits = I915_READ(GAC_ECO_BITS);
  752. I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
  753. ECOBITS_PPGTT_CACHE64B);
  754. gab_ctl = I915_READ(GAB_CTL);
  755. I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
  756. ecochk = I915_READ(GAM_ECOCHK);
  757. I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
  758. I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
  759. for_each_ring(ring, dev_priv, i) {
  760. int ret = ppgtt->switch_mm(ppgtt, ring, true);
  761. if (ret)
  762. return ret;
  763. }
  764. return 0;
  765. }
  766. /* PPGTT support for Sandybdrige/Gen6 and later */
  767. static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
  768. uint64_t start,
  769. uint64_t length,
  770. bool use_scratch)
  771. {
  772. struct i915_hw_ppgtt *ppgtt =
  773. container_of(vm, struct i915_hw_ppgtt, base);
  774. gen6_gtt_pte_t *pt_vaddr, scratch_pte;
  775. unsigned first_entry = start >> PAGE_SHIFT;
  776. unsigned num_entries = length >> PAGE_SHIFT;
  777. unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
  778. unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
  779. unsigned last_pte, i;
  780. scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0);
  781. while (num_entries) {
  782. last_pte = first_pte + num_entries;
  783. if (last_pte > I915_PPGTT_PT_ENTRIES)
  784. last_pte = I915_PPGTT_PT_ENTRIES;
  785. pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
  786. for (i = first_pte; i < last_pte; i++)
  787. pt_vaddr[i] = scratch_pte;
  788. kunmap_atomic(pt_vaddr);
  789. num_entries -= last_pte - first_pte;
  790. first_pte = 0;
  791. act_pt++;
  792. }
  793. }
  794. static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
  795. struct sg_table *pages,
  796. uint64_t start,
  797. enum i915_cache_level cache_level, u32 flags)
  798. {
  799. struct i915_hw_ppgtt *ppgtt =
  800. container_of(vm, struct i915_hw_ppgtt, base);
  801. gen6_gtt_pte_t *pt_vaddr;
  802. unsigned first_entry = start >> PAGE_SHIFT;
  803. unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES;
  804. unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES;
  805. struct sg_page_iter sg_iter;
  806. pt_vaddr = NULL;
  807. for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
  808. if (pt_vaddr == NULL)
  809. pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]);
  810. pt_vaddr[act_pte] =
  811. vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
  812. cache_level, true, flags);
  813. if (++act_pte == I915_PPGTT_PT_ENTRIES) {
  814. kunmap_atomic(pt_vaddr);
  815. pt_vaddr = NULL;
  816. act_pt++;
  817. act_pte = 0;
  818. }
  819. }
  820. if (pt_vaddr)
  821. kunmap_atomic(pt_vaddr);
  822. }
  823. static void gen6_ppgtt_unmap_pages(struct i915_hw_ppgtt *ppgtt)
  824. {
  825. int i;
  826. if (ppgtt->pt_dma_addr) {
  827. for (i = 0; i < ppgtt->num_pd_entries; i++)
  828. pci_unmap_page(ppgtt->base.dev->pdev,
  829. ppgtt->pt_dma_addr[i],
  830. 4096, PCI_DMA_BIDIRECTIONAL);
  831. }
  832. }
  833. static void gen6_ppgtt_free(struct i915_hw_ppgtt *ppgtt)
  834. {
  835. int i;
  836. kfree(ppgtt->pt_dma_addr);
  837. for (i = 0; i < ppgtt->num_pd_entries; i++)
  838. __free_page(ppgtt->pt_pages[i]);
  839. kfree(ppgtt->pt_pages);
  840. }
  841. static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
  842. {
  843. struct i915_hw_ppgtt *ppgtt =
  844. container_of(vm, struct i915_hw_ppgtt, base);
  845. list_del(&vm->global_link);
  846. drm_mm_takedown(&ppgtt->base.mm);
  847. drm_mm_remove_node(&ppgtt->node);
  848. gen6_ppgtt_unmap_pages(ppgtt);
  849. gen6_ppgtt_free(ppgtt);
  850. }
  851. static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
  852. {
  853. struct drm_device *dev = ppgtt->base.dev;
  854. struct drm_i915_private *dev_priv = dev->dev_private;
  855. bool retried = false;
  856. int ret;
  857. /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
  858. * allocator works in address space sizes, so it's multiplied by page
  859. * size. We allocate at the top of the GTT to avoid fragmentation.
  860. */
  861. BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
  862. alloc:
  863. ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
  864. &ppgtt->node, GEN6_PD_SIZE,
  865. GEN6_PD_ALIGN, 0,
  866. 0, dev_priv->gtt.base.total,
  867. DRM_MM_TOPDOWN);
  868. if (ret == -ENOSPC && !retried) {
  869. ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
  870. GEN6_PD_SIZE, GEN6_PD_ALIGN,
  871. I915_CACHE_NONE,
  872. 0, dev_priv->gtt.base.total,
  873. 0);
  874. if (ret)
  875. return ret;
  876. retried = true;
  877. goto alloc;
  878. }
  879. if (ppgtt->node.start < dev_priv->gtt.mappable_end)
  880. DRM_DEBUG("Forced to use aperture for PDEs\n");
  881. ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES;
  882. return ret;
  883. }
  884. static int gen6_ppgtt_allocate_page_tables(struct i915_hw_ppgtt *ppgtt)
  885. {
  886. int i;
  887. ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *),
  888. GFP_KERNEL);
  889. if (!ppgtt->pt_pages)
  890. return -ENOMEM;
  891. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  892. ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
  893. if (!ppgtt->pt_pages[i]) {
  894. gen6_ppgtt_free(ppgtt);
  895. return -ENOMEM;
  896. }
  897. }
  898. return 0;
  899. }
  900. static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
  901. {
  902. int ret;
  903. ret = gen6_ppgtt_allocate_page_directories(ppgtt);
  904. if (ret)
  905. return ret;
  906. ret = gen6_ppgtt_allocate_page_tables(ppgtt);
  907. if (ret) {
  908. drm_mm_remove_node(&ppgtt->node);
  909. return ret;
  910. }
  911. ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t),
  912. GFP_KERNEL);
  913. if (!ppgtt->pt_dma_addr) {
  914. drm_mm_remove_node(&ppgtt->node);
  915. gen6_ppgtt_free(ppgtt);
  916. return -ENOMEM;
  917. }
  918. return 0;
  919. }
  920. static int gen6_ppgtt_setup_page_tables(struct i915_hw_ppgtt *ppgtt)
  921. {
  922. struct drm_device *dev = ppgtt->base.dev;
  923. int i;
  924. for (i = 0; i < ppgtt->num_pd_entries; i++) {
  925. dma_addr_t pt_addr;
  926. pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096,
  927. PCI_DMA_BIDIRECTIONAL);
  928. if (pci_dma_mapping_error(dev->pdev, pt_addr)) {
  929. gen6_ppgtt_unmap_pages(ppgtt);
  930. return -EIO;
  931. }
  932. ppgtt->pt_dma_addr[i] = pt_addr;
  933. }
  934. return 0;
  935. }
  936. static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
  937. {
  938. struct drm_device *dev = ppgtt->base.dev;
  939. struct drm_i915_private *dev_priv = dev->dev_private;
  940. int ret;
  941. ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
  942. if (IS_GEN6(dev)) {
  943. ppgtt->enable = gen6_ppgtt_enable;
  944. ppgtt->switch_mm = gen6_mm_switch;
  945. } else if (IS_HASWELL(dev)) {
  946. ppgtt->enable = gen7_ppgtt_enable;
  947. ppgtt->switch_mm = hsw_mm_switch;
  948. } else if (IS_GEN7(dev)) {
  949. ppgtt->enable = gen7_ppgtt_enable;
  950. ppgtt->switch_mm = gen7_mm_switch;
  951. } else
  952. BUG();
  953. ret = gen6_ppgtt_alloc(ppgtt);
  954. if (ret)
  955. return ret;
  956. ret = gen6_ppgtt_setup_page_tables(ppgtt);
  957. if (ret) {
  958. gen6_ppgtt_free(ppgtt);
  959. return ret;
  960. }
  961. ppgtt->base.clear_range = gen6_ppgtt_clear_range;
  962. ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
  963. ppgtt->base.cleanup = gen6_ppgtt_cleanup;
  964. ppgtt->base.start = 0;
  965. ppgtt->base.total = ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES * PAGE_SIZE;
  966. ppgtt->debug_dump = gen6_dump_ppgtt;
  967. ppgtt->pd_offset =
  968. ppgtt->node.start / PAGE_SIZE * sizeof(gen6_gtt_pte_t);
  969. ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true);
  970. DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n",
  971. ppgtt->node.size >> 20,
  972. ppgtt->node.start / PAGE_SIZE);
  973. return 0;
  974. }
  975. int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
  976. {
  977. struct drm_i915_private *dev_priv = dev->dev_private;
  978. int ret = 0;
  979. ppgtt->base.dev = dev;
  980. ppgtt->base.scratch = dev_priv->gtt.base.scratch;
  981. if (INTEL_INFO(dev)->gen < 8)
  982. ret = gen6_ppgtt_init(ppgtt);
  983. else if (IS_GEN8(dev))
  984. ret = gen8_ppgtt_init(ppgtt, dev_priv->gtt.base.total);
  985. else
  986. BUG();
  987. if (!ret) {
  988. struct drm_i915_private *dev_priv = dev->dev_private;
  989. kref_init(&ppgtt->ref);
  990. drm_mm_init(&ppgtt->base.mm, ppgtt->base.start,
  991. ppgtt->base.total);
  992. i915_init_vm(dev_priv, &ppgtt->base);
  993. if (INTEL_INFO(dev)->gen < 8) {
  994. gen6_write_pdes(ppgtt);
  995. DRM_DEBUG("Adding PPGTT at offset %x\n",
  996. ppgtt->pd_offset << 10);
  997. }
  998. }
  999. return ret;
  1000. }
  1001. struct i915_hw_ppgtt *
  1002. i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
  1003. {
  1004. struct i915_hw_ppgtt *ppgtt;
  1005. int ret;
  1006. ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
  1007. if (!ppgtt)
  1008. return ERR_PTR(-ENOMEM);
  1009. ret = i915_ppgtt_init(dev, ppgtt);
  1010. if (ret) {
  1011. kfree(ppgtt);
  1012. return ERR_PTR(ret);
  1013. }
  1014. ppgtt->file_priv = fpriv;
  1015. return ppgtt;
  1016. }
  1017. void i915_ppgtt_release(struct kref *kref)
  1018. {
  1019. struct i915_hw_ppgtt *ppgtt =
  1020. container_of(kref, struct i915_hw_ppgtt, ref);
  1021. /* vmas should already be unbound */
  1022. WARN_ON(!list_empty(&ppgtt->base.active_list));
  1023. WARN_ON(!list_empty(&ppgtt->base.inactive_list));
  1024. ppgtt->base.cleanup(&ppgtt->base);
  1025. kfree(ppgtt);
  1026. }
  1027. static void
  1028. ppgtt_bind_vma(struct i915_vma *vma,
  1029. enum i915_cache_level cache_level,
  1030. u32 flags)
  1031. {
  1032. /* Currently applicable only to VLV */
  1033. if (vma->obj->gt_ro)
  1034. flags |= PTE_READ_ONLY;
  1035. vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
  1036. cache_level, flags);
  1037. }
  1038. static void ppgtt_unbind_vma(struct i915_vma *vma)
  1039. {
  1040. vma->vm->clear_range(vma->vm,
  1041. vma->node.start,
  1042. vma->obj->base.size,
  1043. true);
  1044. }
  1045. extern int intel_iommu_gfx_mapped;
  1046. /* Certain Gen5 chipsets require require idling the GPU before
  1047. * unmapping anything from the GTT when VT-d is enabled.
  1048. */
  1049. static inline bool needs_idle_maps(struct drm_device *dev)
  1050. {
  1051. #ifdef CONFIG_INTEL_IOMMU
  1052. /* Query intel_iommu to see if we need the workaround. Presumably that
  1053. * was loaded first.
  1054. */
  1055. if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
  1056. return true;
  1057. #endif
  1058. return false;
  1059. }
  1060. static bool do_idling(struct drm_i915_private *dev_priv)
  1061. {
  1062. bool ret = dev_priv->mm.interruptible;
  1063. if (unlikely(dev_priv->gtt.do_idle_maps)) {
  1064. dev_priv->mm.interruptible = false;
  1065. if (i915_gpu_idle(dev_priv->dev)) {
  1066. DRM_ERROR("Couldn't idle GPU\n");
  1067. /* Wait a bit, in hopes it avoids the hang */
  1068. udelay(10);
  1069. }
  1070. }
  1071. return ret;
  1072. }
  1073. static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
  1074. {
  1075. if (unlikely(dev_priv->gtt.do_idle_maps))
  1076. dev_priv->mm.interruptible = interruptible;
  1077. }
  1078. void i915_check_and_clear_faults(struct drm_device *dev)
  1079. {
  1080. struct drm_i915_private *dev_priv = dev->dev_private;
  1081. struct intel_engine_cs *ring;
  1082. int i;
  1083. if (INTEL_INFO(dev)->gen < 6)
  1084. return;
  1085. for_each_ring(ring, dev_priv, i) {
  1086. u32 fault_reg;
  1087. fault_reg = I915_READ(RING_FAULT_REG(ring));
  1088. if (fault_reg & RING_FAULT_VALID) {
  1089. DRM_DEBUG_DRIVER("Unexpected fault\n"
  1090. "\tAddr: 0x%08lx\\n"
  1091. "\tAddress space: %s\n"
  1092. "\tSource ID: %d\n"
  1093. "\tType: %d\n",
  1094. fault_reg & PAGE_MASK,
  1095. fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
  1096. RING_FAULT_SRCID(fault_reg),
  1097. RING_FAULT_FAULT_TYPE(fault_reg));
  1098. I915_WRITE(RING_FAULT_REG(ring),
  1099. fault_reg & ~RING_FAULT_VALID);
  1100. }
  1101. }
  1102. POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
  1103. }
  1104. void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
  1105. {
  1106. struct drm_i915_private *dev_priv = dev->dev_private;
  1107. /* Don't bother messing with faults pre GEN6 as we have little
  1108. * documentation supporting that it's a good idea.
  1109. */
  1110. if (INTEL_INFO(dev)->gen < 6)
  1111. return;
  1112. i915_check_and_clear_faults(dev);
  1113. dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
  1114. dev_priv->gtt.base.start,
  1115. dev_priv->gtt.base.total,
  1116. true);
  1117. }
  1118. void i915_gem_restore_gtt_mappings(struct drm_device *dev)
  1119. {
  1120. struct drm_i915_private *dev_priv = dev->dev_private;
  1121. struct drm_i915_gem_object *obj;
  1122. struct i915_address_space *vm;
  1123. i915_check_and_clear_faults(dev);
  1124. /* First fill our portion of the GTT with scratch pages */
  1125. dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
  1126. dev_priv->gtt.base.start,
  1127. dev_priv->gtt.base.total,
  1128. true);
  1129. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  1130. struct i915_vma *vma = i915_gem_obj_to_vma(obj,
  1131. &dev_priv->gtt.base);
  1132. if (!vma)
  1133. continue;
  1134. i915_gem_clflush_object(obj, obj->pin_display);
  1135. /* The bind_vma code tries to be smart about tracking mappings.
  1136. * Unfortunately above, we've just wiped out the mappings
  1137. * without telling our object about it. So we need to fake it.
  1138. */
  1139. obj->has_global_gtt_mapping = 0;
  1140. vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
  1141. }
  1142. if (INTEL_INFO(dev)->gen >= 8) {
  1143. if (IS_CHERRYVIEW(dev))
  1144. chv_setup_private_ppat(dev_priv);
  1145. else
  1146. bdw_setup_private_ppat(dev_priv);
  1147. return;
  1148. }
  1149. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  1150. /* TODO: Perhaps it shouldn't be gen6 specific */
  1151. if (i915_is_ggtt(vm)) {
  1152. if (dev_priv->mm.aliasing_ppgtt)
  1153. gen6_write_pdes(dev_priv->mm.aliasing_ppgtt);
  1154. continue;
  1155. }
  1156. gen6_write_pdes(container_of(vm, struct i915_hw_ppgtt, base));
  1157. }
  1158. i915_gem_chipset_flush(dev);
  1159. }
  1160. int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
  1161. {
  1162. if (obj->has_dma_mapping)
  1163. return 0;
  1164. if (!dma_map_sg(&obj->base.dev->pdev->dev,
  1165. obj->pages->sgl, obj->pages->nents,
  1166. PCI_DMA_BIDIRECTIONAL))
  1167. return -ENOSPC;
  1168. return 0;
  1169. }
  1170. static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte)
  1171. {
  1172. #ifdef writeq
  1173. writeq(pte, addr);
  1174. #else
  1175. iowrite32((u32)pte, addr);
  1176. iowrite32(pte >> 32, addr + 4);
  1177. #endif
  1178. }
  1179. static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
  1180. struct sg_table *st,
  1181. uint64_t start,
  1182. enum i915_cache_level level, u32 unused)
  1183. {
  1184. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1185. unsigned first_entry = start >> PAGE_SHIFT;
  1186. gen8_gtt_pte_t __iomem *gtt_entries =
  1187. (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
  1188. int i = 0;
  1189. struct sg_page_iter sg_iter;
  1190. dma_addr_t addr = 0; /* shut up gcc */
  1191. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
  1192. addr = sg_dma_address(sg_iter.sg) +
  1193. (sg_iter.sg_pgoffset << PAGE_SHIFT);
  1194. gen8_set_pte(&gtt_entries[i],
  1195. gen8_pte_encode(addr, level, true));
  1196. i++;
  1197. }
  1198. /*
  1199. * XXX: This serves as a posting read to make sure that the PTE has
  1200. * actually been updated. There is some concern that even though
  1201. * registers and PTEs are within the same BAR that they are potentially
  1202. * of NUMA access patterns. Therefore, even with the way we assume
  1203. * hardware should work, we must keep this posting read for paranoia.
  1204. */
  1205. if (i != 0)
  1206. WARN_ON(readq(&gtt_entries[i-1])
  1207. != gen8_pte_encode(addr, level, true));
  1208. /* This next bit makes the above posting read even more important. We
  1209. * want to flush the TLBs only after we're certain all the PTE updates
  1210. * have finished.
  1211. */
  1212. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  1213. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1214. }
  1215. /*
  1216. * Binds an object into the global gtt with the specified cache level. The object
  1217. * will be accessible to the GPU via commands whose operands reference offsets
  1218. * within the global GTT as well as accessible by the GPU through the GMADR
  1219. * mapped BAR (dev_priv->mm.gtt->gtt).
  1220. */
  1221. static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
  1222. struct sg_table *st,
  1223. uint64_t start,
  1224. enum i915_cache_level level, u32 flags)
  1225. {
  1226. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1227. unsigned first_entry = start >> PAGE_SHIFT;
  1228. gen6_gtt_pte_t __iomem *gtt_entries =
  1229. (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
  1230. int i = 0;
  1231. struct sg_page_iter sg_iter;
  1232. dma_addr_t addr = 0;
  1233. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
  1234. addr = sg_page_iter_dma_address(&sg_iter);
  1235. iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
  1236. i++;
  1237. }
  1238. /* XXX: This serves as a posting read to make sure that the PTE has
  1239. * actually been updated. There is some concern that even though
  1240. * registers and PTEs are within the same BAR that they are potentially
  1241. * of NUMA access patterns. Therefore, even with the way we assume
  1242. * hardware should work, we must keep this posting read for paranoia.
  1243. */
  1244. if (i != 0) {
  1245. unsigned long gtt = readl(&gtt_entries[i-1]);
  1246. WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
  1247. }
  1248. /* This next bit makes the above posting read even more important. We
  1249. * want to flush the TLBs only after we're certain all the PTE updates
  1250. * have finished.
  1251. */
  1252. I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  1253. POSTING_READ(GFX_FLSH_CNTL_GEN6);
  1254. }
  1255. static void gen8_ggtt_clear_range(struct i915_address_space *vm,
  1256. uint64_t start,
  1257. uint64_t length,
  1258. bool use_scratch)
  1259. {
  1260. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1261. unsigned first_entry = start >> PAGE_SHIFT;
  1262. unsigned num_entries = length >> PAGE_SHIFT;
  1263. gen8_gtt_pte_t scratch_pte, __iomem *gtt_base =
  1264. (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
  1265. const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
  1266. int i;
  1267. if (WARN(num_entries > max_entries,
  1268. "First entry = %d; Num entries = %d (max=%d)\n",
  1269. first_entry, num_entries, max_entries))
  1270. num_entries = max_entries;
  1271. scratch_pte = gen8_pte_encode(vm->scratch.addr,
  1272. I915_CACHE_LLC,
  1273. use_scratch);
  1274. for (i = 0; i < num_entries; i++)
  1275. gen8_set_pte(&gtt_base[i], scratch_pte);
  1276. readl(gtt_base);
  1277. }
  1278. static void gen6_ggtt_clear_range(struct i915_address_space *vm,
  1279. uint64_t start,
  1280. uint64_t length,
  1281. bool use_scratch)
  1282. {
  1283. struct drm_i915_private *dev_priv = vm->dev->dev_private;
  1284. unsigned first_entry = start >> PAGE_SHIFT;
  1285. unsigned num_entries = length >> PAGE_SHIFT;
  1286. gen6_gtt_pte_t scratch_pte, __iomem *gtt_base =
  1287. (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
  1288. const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
  1289. int i;
  1290. if (WARN(num_entries > max_entries,
  1291. "First entry = %d; Num entries = %d (max=%d)\n",
  1292. first_entry, num_entries, max_entries))
  1293. num_entries = max_entries;
  1294. scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0);
  1295. for (i = 0; i < num_entries; i++)
  1296. iowrite32(scratch_pte, &gtt_base[i]);
  1297. readl(gtt_base);
  1298. }
  1299. static void i915_ggtt_bind_vma(struct i915_vma *vma,
  1300. enum i915_cache_level cache_level,
  1301. u32 unused)
  1302. {
  1303. const unsigned long entry = vma->node.start >> PAGE_SHIFT;
  1304. unsigned int flags = (cache_level == I915_CACHE_NONE) ?
  1305. AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
  1306. BUG_ON(!i915_is_ggtt(vma->vm));
  1307. intel_gtt_insert_sg_entries(vma->obj->pages, entry, flags);
  1308. vma->obj->has_global_gtt_mapping = 1;
  1309. }
  1310. static void i915_ggtt_clear_range(struct i915_address_space *vm,
  1311. uint64_t start,
  1312. uint64_t length,
  1313. bool unused)
  1314. {
  1315. unsigned first_entry = start >> PAGE_SHIFT;
  1316. unsigned num_entries = length >> PAGE_SHIFT;
  1317. intel_gtt_clear_range(first_entry, num_entries);
  1318. }
  1319. static void i915_ggtt_unbind_vma(struct i915_vma *vma)
  1320. {
  1321. const unsigned int first = vma->node.start >> PAGE_SHIFT;
  1322. const unsigned int size = vma->obj->base.size >> PAGE_SHIFT;
  1323. BUG_ON(!i915_is_ggtt(vma->vm));
  1324. vma->obj->has_global_gtt_mapping = 0;
  1325. intel_gtt_clear_range(first, size);
  1326. }
  1327. static void ggtt_bind_vma(struct i915_vma *vma,
  1328. enum i915_cache_level cache_level,
  1329. u32 flags)
  1330. {
  1331. struct drm_device *dev = vma->vm->dev;
  1332. struct drm_i915_private *dev_priv = dev->dev_private;
  1333. struct drm_i915_gem_object *obj = vma->obj;
  1334. /* Currently applicable only to VLV */
  1335. if (obj->gt_ro)
  1336. flags |= PTE_READ_ONLY;
  1337. /* If there is no aliasing PPGTT, or the caller needs a global mapping,
  1338. * or we have a global mapping already but the cacheability flags have
  1339. * changed, set the global PTEs.
  1340. *
  1341. * If there is an aliasing PPGTT it is anecdotally faster, so use that
  1342. * instead if none of the above hold true.
  1343. *
  1344. * NB: A global mapping should only be needed for special regions like
  1345. * "gtt mappable", SNB errata, or if specified via special execbuf
  1346. * flags. At all other times, the GPU will use the aliasing PPGTT.
  1347. */
  1348. if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) {
  1349. if (!obj->has_global_gtt_mapping ||
  1350. (cache_level != obj->cache_level)) {
  1351. vma->vm->insert_entries(vma->vm, obj->pages,
  1352. vma->node.start,
  1353. cache_level, flags);
  1354. obj->has_global_gtt_mapping = 1;
  1355. }
  1356. }
  1357. if (dev_priv->mm.aliasing_ppgtt &&
  1358. (!obj->has_aliasing_ppgtt_mapping ||
  1359. (cache_level != obj->cache_level))) {
  1360. struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
  1361. appgtt->base.insert_entries(&appgtt->base,
  1362. vma->obj->pages,
  1363. vma->node.start,
  1364. cache_level, flags);
  1365. vma->obj->has_aliasing_ppgtt_mapping = 1;
  1366. }
  1367. }
  1368. static void ggtt_unbind_vma(struct i915_vma *vma)
  1369. {
  1370. struct drm_device *dev = vma->vm->dev;
  1371. struct drm_i915_private *dev_priv = dev->dev_private;
  1372. struct drm_i915_gem_object *obj = vma->obj;
  1373. if (obj->has_global_gtt_mapping) {
  1374. vma->vm->clear_range(vma->vm,
  1375. vma->node.start,
  1376. obj->base.size,
  1377. true);
  1378. obj->has_global_gtt_mapping = 0;
  1379. }
  1380. if (obj->has_aliasing_ppgtt_mapping) {
  1381. struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
  1382. appgtt->base.clear_range(&appgtt->base,
  1383. vma->node.start,
  1384. obj->base.size,
  1385. true);
  1386. obj->has_aliasing_ppgtt_mapping = 0;
  1387. }
  1388. }
  1389. void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
  1390. {
  1391. struct drm_device *dev = obj->base.dev;
  1392. struct drm_i915_private *dev_priv = dev->dev_private;
  1393. bool interruptible;
  1394. interruptible = do_idling(dev_priv);
  1395. if (!obj->has_dma_mapping)
  1396. dma_unmap_sg(&dev->pdev->dev,
  1397. obj->pages->sgl, obj->pages->nents,
  1398. PCI_DMA_BIDIRECTIONAL);
  1399. undo_idling(dev_priv, interruptible);
  1400. }
  1401. static void i915_gtt_color_adjust(struct drm_mm_node *node,
  1402. unsigned long color,
  1403. unsigned long *start,
  1404. unsigned long *end)
  1405. {
  1406. if (node->color != color)
  1407. *start += 4096;
  1408. if (!list_empty(&node->node_list)) {
  1409. node = list_entry(node->node_list.next,
  1410. struct drm_mm_node,
  1411. node_list);
  1412. if (node->allocated && node->color != color)
  1413. *end -= 4096;
  1414. }
  1415. }
  1416. int i915_gem_setup_global_gtt(struct drm_device *dev,
  1417. unsigned long start,
  1418. unsigned long mappable_end,
  1419. unsigned long end)
  1420. {
  1421. /* Let GEM Manage all of the aperture.
  1422. *
  1423. * However, leave one page at the end still bound to the scratch page.
  1424. * There are a number of places where the hardware apparently prefetches
  1425. * past the end of the object, and we've seen multiple hangs with the
  1426. * GPU head pointer stuck in a batchbuffer bound at the last page of the
  1427. * aperture. One page should be enough to keep any prefetching inside
  1428. * of the aperture.
  1429. */
  1430. struct drm_i915_private *dev_priv = dev->dev_private;
  1431. struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
  1432. struct drm_mm_node *entry;
  1433. struct drm_i915_gem_object *obj;
  1434. unsigned long hole_start, hole_end;
  1435. BUG_ON(mappable_end > end);
  1436. /* Subtract the guard page ... */
  1437. drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE);
  1438. if (!HAS_LLC(dev))
  1439. dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust;
  1440. /* Mark any preallocated objects as occupied */
  1441. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  1442. struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
  1443. int ret;
  1444. DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n",
  1445. i915_gem_obj_ggtt_offset(obj), obj->base.size);
  1446. WARN_ON(i915_gem_obj_ggtt_bound(obj));
  1447. ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
  1448. if (ret) {
  1449. DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
  1450. return ret;
  1451. }
  1452. obj->has_global_gtt_mapping = 1;
  1453. }
  1454. dev_priv->gtt.base.start = start;
  1455. dev_priv->gtt.base.total = end - start;
  1456. /* Clear any non-preallocated blocks */
  1457. drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
  1458. DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
  1459. hole_start, hole_end);
  1460. ggtt_vm->clear_range(ggtt_vm, hole_start,
  1461. hole_end - hole_start, true);
  1462. }
  1463. /* And finally clear the reserved guard page */
  1464. ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
  1465. return 0;
  1466. }
  1467. void i915_gem_init_global_gtt(struct drm_device *dev)
  1468. {
  1469. struct drm_i915_private *dev_priv = dev->dev_private;
  1470. unsigned long gtt_size, mappable_size;
  1471. gtt_size = dev_priv->gtt.base.total;
  1472. mappable_size = dev_priv->gtt.mappable_end;
  1473. i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
  1474. }
  1475. static int setup_scratch_page(struct drm_device *dev)
  1476. {
  1477. struct drm_i915_private *dev_priv = dev->dev_private;
  1478. struct page *page;
  1479. dma_addr_t dma_addr;
  1480. page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
  1481. if (page == NULL)
  1482. return -ENOMEM;
  1483. get_page(page);
  1484. set_pages_uc(page, 1);
  1485. #ifdef CONFIG_INTEL_IOMMU
  1486. dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE,
  1487. PCI_DMA_BIDIRECTIONAL);
  1488. if (pci_dma_mapping_error(dev->pdev, dma_addr))
  1489. return -EINVAL;
  1490. #else
  1491. dma_addr = page_to_phys(page);
  1492. #endif
  1493. dev_priv->gtt.base.scratch.page = page;
  1494. dev_priv->gtt.base.scratch.addr = dma_addr;
  1495. return 0;
  1496. }
  1497. static void teardown_scratch_page(struct drm_device *dev)
  1498. {
  1499. struct drm_i915_private *dev_priv = dev->dev_private;
  1500. struct page *page = dev_priv->gtt.base.scratch.page;
  1501. set_pages_wb(page, 1);
  1502. pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr,
  1503. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  1504. put_page(page);
  1505. __free_page(page);
  1506. }
  1507. static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
  1508. {
  1509. snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
  1510. snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
  1511. return snb_gmch_ctl << 20;
  1512. }
  1513. static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
  1514. {
  1515. bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
  1516. bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
  1517. if (bdw_gmch_ctl)
  1518. bdw_gmch_ctl = 1 << bdw_gmch_ctl;
  1519. #ifdef CONFIG_X86_32
  1520. /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
  1521. if (bdw_gmch_ctl > 4)
  1522. bdw_gmch_ctl = 4;
  1523. #endif
  1524. return bdw_gmch_ctl << 20;
  1525. }
  1526. static inline unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
  1527. {
  1528. gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
  1529. gmch_ctrl &= SNB_GMCH_GGMS_MASK;
  1530. if (gmch_ctrl)
  1531. return 1 << (20 + gmch_ctrl);
  1532. return 0;
  1533. }
  1534. static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
  1535. {
  1536. snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
  1537. snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
  1538. return snb_gmch_ctl << 25; /* 32 MB units */
  1539. }
  1540. static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
  1541. {
  1542. bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
  1543. bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
  1544. return bdw_gmch_ctl << 25; /* 32 MB units */
  1545. }
  1546. static size_t chv_get_stolen_size(u16 gmch_ctrl)
  1547. {
  1548. gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
  1549. gmch_ctrl &= SNB_GMCH_GMS_MASK;
  1550. /*
  1551. * 0x0 to 0x10: 32MB increments starting at 0MB
  1552. * 0x11 to 0x16: 4MB increments starting at 8MB
  1553. * 0x17 to 0x1d: 4MB increments start at 36MB
  1554. */
  1555. if (gmch_ctrl < 0x11)
  1556. return gmch_ctrl << 25;
  1557. else if (gmch_ctrl < 0x17)
  1558. return (gmch_ctrl - 0x11 + 2) << 22;
  1559. else
  1560. return (gmch_ctrl - 0x17 + 9) << 22;
  1561. }
  1562. static int ggtt_probe_common(struct drm_device *dev,
  1563. size_t gtt_size)
  1564. {
  1565. struct drm_i915_private *dev_priv = dev->dev_private;
  1566. phys_addr_t gtt_phys_addr;
  1567. int ret;
  1568. /* For Modern GENs the PTEs and register space are split in the BAR */
  1569. gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
  1570. (pci_resource_len(dev->pdev, 0) / 2);
  1571. dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
  1572. if (!dev_priv->gtt.gsm) {
  1573. DRM_ERROR("Failed to map the gtt page table\n");
  1574. return -ENOMEM;
  1575. }
  1576. ret = setup_scratch_page(dev);
  1577. if (ret) {
  1578. DRM_ERROR("Scratch setup failed\n");
  1579. /* iounmap will also get called at remove, but meh */
  1580. iounmap(dev_priv->gtt.gsm);
  1581. }
  1582. return ret;
  1583. }
  1584. /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
  1585. * bits. When using advanced contexts each context stores its own PAT, but
  1586. * writing this data shouldn't be harmful even in those cases. */
  1587. static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
  1588. {
  1589. uint64_t pat;
  1590. pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
  1591. GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
  1592. GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
  1593. GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
  1594. GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
  1595. GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
  1596. GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
  1597. GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
  1598. /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
  1599. * write would work. */
  1600. I915_WRITE(GEN8_PRIVATE_PAT, pat);
  1601. I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
  1602. }
  1603. static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
  1604. {
  1605. uint64_t pat;
  1606. /*
  1607. * Map WB on BDW to snooped on CHV.
  1608. *
  1609. * Only the snoop bit has meaning for CHV, the rest is
  1610. * ignored.
  1611. *
  1612. * Note that the harware enforces snooping for all page
  1613. * table accesses. The snoop bit is actually ignored for
  1614. * PDEs.
  1615. */
  1616. pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
  1617. GEN8_PPAT(1, 0) |
  1618. GEN8_PPAT(2, 0) |
  1619. GEN8_PPAT(3, 0) |
  1620. GEN8_PPAT(4, CHV_PPAT_SNOOP) |
  1621. GEN8_PPAT(5, CHV_PPAT_SNOOP) |
  1622. GEN8_PPAT(6, CHV_PPAT_SNOOP) |
  1623. GEN8_PPAT(7, CHV_PPAT_SNOOP);
  1624. I915_WRITE(GEN8_PRIVATE_PAT, pat);
  1625. I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32);
  1626. }
  1627. static int gen8_gmch_probe(struct drm_device *dev,
  1628. size_t *gtt_total,
  1629. size_t *stolen,
  1630. phys_addr_t *mappable_base,
  1631. unsigned long *mappable_end)
  1632. {
  1633. struct drm_i915_private *dev_priv = dev->dev_private;
  1634. unsigned int gtt_size;
  1635. u16 snb_gmch_ctl;
  1636. int ret;
  1637. /* TODO: We're not aware of mappable constraints on gen8 yet */
  1638. *mappable_base = pci_resource_start(dev->pdev, 2);
  1639. *mappable_end = pci_resource_len(dev->pdev, 2);
  1640. if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
  1641. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
  1642. pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  1643. if (IS_CHERRYVIEW(dev)) {
  1644. *stolen = chv_get_stolen_size(snb_gmch_ctl);
  1645. gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
  1646. } else {
  1647. *stolen = gen8_get_stolen_size(snb_gmch_ctl);
  1648. gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
  1649. }
  1650. *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT;
  1651. if (IS_CHERRYVIEW(dev))
  1652. chv_setup_private_ppat(dev_priv);
  1653. else
  1654. bdw_setup_private_ppat(dev_priv);
  1655. ret = ggtt_probe_common(dev, gtt_size);
  1656. dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
  1657. dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
  1658. return ret;
  1659. }
  1660. static int gen6_gmch_probe(struct drm_device *dev,
  1661. size_t *gtt_total,
  1662. size_t *stolen,
  1663. phys_addr_t *mappable_base,
  1664. unsigned long *mappable_end)
  1665. {
  1666. struct drm_i915_private *dev_priv = dev->dev_private;
  1667. unsigned int gtt_size;
  1668. u16 snb_gmch_ctl;
  1669. int ret;
  1670. *mappable_base = pci_resource_start(dev->pdev, 2);
  1671. *mappable_end = pci_resource_len(dev->pdev, 2);
  1672. /* 64/512MB is the current min/max we actually know of, but this is just
  1673. * a coarse sanity check.
  1674. */
  1675. if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
  1676. DRM_ERROR("Unknown GMADR size (%lx)\n",
  1677. dev_priv->gtt.mappable_end);
  1678. return -ENXIO;
  1679. }
  1680. if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
  1681. pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
  1682. pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  1683. *stolen = gen6_get_stolen_size(snb_gmch_ctl);
  1684. gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
  1685. *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT;
  1686. ret = ggtt_probe_common(dev, gtt_size);
  1687. dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
  1688. dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
  1689. return ret;
  1690. }
  1691. static void gen6_gmch_remove(struct i915_address_space *vm)
  1692. {
  1693. struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
  1694. if (drm_mm_initialized(&vm->mm)) {
  1695. drm_mm_takedown(&vm->mm);
  1696. list_del(&vm->global_link);
  1697. }
  1698. iounmap(gtt->gsm);
  1699. teardown_scratch_page(vm->dev);
  1700. }
  1701. static int i915_gmch_probe(struct drm_device *dev,
  1702. size_t *gtt_total,
  1703. size_t *stolen,
  1704. phys_addr_t *mappable_base,
  1705. unsigned long *mappable_end)
  1706. {
  1707. struct drm_i915_private *dev_priv = dev->dev_private;
  1708. int ret;
  1709. ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
  1710. if (!ret) {
  1711. DRM_ERROR("failed to set up gmch\n");
  1712. return -EIO;
  1713. }
  1714. intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
  1715. dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
  1716. dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
  1717. if (unlikely(dev_priv->gtt.do_idle_maps))
  1718. DRM_INFO("applying Ironlake quirks for intel_iommu\n");
  1719. return 0;
  1720. }
  1721. static void i915_gmch_remove(struct i915_address_space *vm)
  1722. {
  1723. if (drm_mm_initialized(&vm->mm)) {
  1724. drm_mm_takedown(&vm->mm);
  1725. list_del(&vm->global_link);
  1726. }
  1727. intel_gmch_remove();
  1728. }
  1729. int i915_gem_gtt_init(struct drm_device *dev)
  1730. {
  1731. struct drm_i915_private *dev_priv = dev->dev_private;
  1732. struct i915_gtt *gtt = &dev_priv->gtt;
  1733. int ret;
  1734. if (INTEL_INFO(dev)->gen <= 5) {
  1735. gtt->gtt_probe = i915_gmch_probe;
  1736. gtt->base.cleanup = i915_gmch_remove;
  1737. } else if (INTEL_INFO(dev)->gen < 8) {
  1738. gtt->gtt_probe = gen6_gmch_probe;
  1739. gtt->base.cleanup = gen6_gmch_remove;
  1740. if (IS_HASWELL(dev) && dev_priv->ellc_size)
  1741. gtt->base.pte_encode = iris_pte_encode;
  1742. else if (IS_HASWELL(dev))
  1743. gtt->base.pte_encode = hsw_pte_encode;
  1744. else if (IS_VALLEYVIEW(dev))
  1745. gtt->base.pte_encode = byt_pte_encode;
  1746. else if (INTEL_INFO(dev)->gen >= 7)
  1747. gtt->base.pte_encode = ivb_pte_encode;
  1748. else
  1749. gtt->base.pte_encode = snb_pte_encode;
  1750. } else {
  1751. dev_priv->gtt.gtt_probe = gen8_gmch_probe;
  1752. dev_priv->gtt.base.cleanup = gen6_gmch_remove;
  1753. }
  1754. ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
  1755. &gtt->mappable_base, &gtt->mappable_end);
  1756. if (ret)
  1757. return ret;
  1758. gtt->base.dev = dev;
  1759. /* GMADR is the PCI mmio aperture into the global GTT. */
  1760. DRM_INFO("Memory usable by graphics device = %zdM\n",
  1761. gtt->base.total >> 20);
  1762. DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20);
  1763. DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
  1764. #ifdef CONFIG_INTEL_IOMMU
  1765. if (intel_iommu_gfx_mapped)
  1766. DRM_INFO("VT-d active for gfx access\n");
  1767. #endif
  1768. /*
  1769. * i915.enable_ppgtt is read-only, so do an early pass to validate the
  1770. * user's requested state against the hardware/driver capabilities. We
  1771. * do this now so that we can print out any log messages once rather
  1772. * than every time we check intel_enable_ppgtt().
  1773. */
  1774. i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
  1775. DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
  1776. return 0;
  1777. }
  1778. static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
  1779. struct i915_address_space *vm)
  1780. {
  1781. struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
  1782. if (vma == NULL)
  1783. return ERR_PTR(-ENOMEM);
  1784. INIT_LIST_HEAD(&vma->vma_link);
  1785. INIT_LIST_HEAD(&vma->mm_list);
  1786. INIT_LIST_HEAD(&vma->exec_list);
  1787. vma->vm = vm;
  1788. vma->obj = obj;
  1789. switch (INTEL_INFO(vm->dev)->gen) {
  1790. case 8:
  1791. case 7:
  1792. case 6:
  1793. if (i915_is_ggtt(vm)) {
  1794. vma->unbind_vma = ggtt_unbind_vma;
  1795. vma->bind_vma = ggtt_bind_vma;
  1796. } else {
  1797. vma->unbind_vma = ppgtt_unbind_vma;
  1798. vma->bind_vma = ppgtt_bind_vma;
  1799. }
  1800. break;
  1801. case 5:
  1802. case 4:
  1803. case 3:
  1804. case 2:
  1805. BUG_ON(!i915_is_ggtt(vm));
  1806. vma->unbind_vma = i915_ggtt_unbind_vma;
  1807. vma->bind_vma = i915_ggtt_bind_vma;
  1808. break;
  1809. default:
  1810. BUG();
  1811. }
  1812. /* Keep GGTT vmas first to make debug easier */
  1813. if (i915_is_ggtt(vm))
  1814. list_add(&vma->vma_link, &obj->vma_list);
  1815. else
  1816. list_add_tail(&vma->vma_link, &obj->vma_list);
  1817. return vma;
  1818. }
  1819. struct i915_vma *
  1820. i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
  1821. struct i915_address_space *vm)
  1822. {
  1823. struct i915_vma *vma;
  1824. vma = i915_gem_obj_to_vma(obj, vm);
  1825. if (!vma)
  1826. vma = __i915_gem_vma_create(obj, vm);
  1827. if (!i915_is_ggtt(vm))
  1828. i915_ppgtt_get(i915_vm_to_ppgtt(vm));
  1829. return vma;
  1830. }