intel_ggtt.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2020 Intel Corporation
  4. */
  5. #include <asm/set_memory.h>
  6. #include <asm/smp.h>
  7. #include <linux/types.h>
  8. #include <linux/stop_machine.h>
  9. #include <drm/drm_managed.h>
  10. #include <drm/drm_print.h>
  11. #include <drm/intel/i915_drm.h>
  12. #include <drm/intel/intel-gtt.h>
  13. #include "gem/i915_gem_lmem.h"
  14. #include "intel_context.h"
  15. #include "intel_ggtt_gmch.h"
  16. #include "intel_gpu_commands.h"
  17. #include "intel_gt.h"
  18. #include "intel_gt_regs.h"
  19. #include "intel_pci_config.h"
  20. #include "intel_ring.h"
  21. #include "i915_drv.h"
  22. #include "i915_pci.h"
  23. #include "i915_reg.h"
  24. #include "i915_request.h"
  25. #include "i915_scatterlist.h"
  26. #include "i915_utils.h"
  27. #include "i915_vgpu.h"
  28. #include "intel_gtt.h"
  29. #include "gen8_ppgtt.h"
  30. #include "intel_engine_pm.h"
  31. static void i915_ggtt_color_adjust(const struct drm_mm_node *node,
  32. unsigned long color,
  33. u64 *start,
  34. u64 *end)
  35. {
  36. if (i915_node_color_differs(node, color))
  37. *start += I915_GTT_PAGE_SIZE;
  38. /*
  39. * Also leave a space between the unallocated reserved node after the
  40. * GTT and any objects within the GTT, i.e. we use the color adjustment
  41. * to insert a guard page to prevent prefetches crossing over the
  42. * GTT boundary.
  43. */
  44. node = list_next_entry(node, node_list);
  45. if (node->color != color)
  46. *end -= I915_GTT_PAGE_SIZE;
  47. }
  48. static int ggtt_init_hw(struct i915_ggtt *ggtt)
  49. {
  50. struct drm_i915_private *i915 = ggtt->vm.i915;
  51. i915_address_space_init(&ggtt->vm, VM_CLASS_GGTT);
  52. ggtt->vm.is_ggtt = true;
  53. /* Only VLV supports read-only GGTT mappings */
  54. ggtt->vm.has_read_only = IS_VALLEYVIEW(i915);
  55. if (!HAS_LLC(i915) && !HAS_PPGTT(i915))
  56. ggtt->vm.mm.color_adjust = i915_ggtt_color_adjust;
  57. if (ggtt->mappable_end) {
  58. if (!io_mapping_init_wc(&ggtt->iomap,
  59. ggtt->gmadr.start,
  60. ggtt->mappable_end)) {
  61. ggtt->vm.cleanup(&ggtt->vm);
  62. return -EIO;
  63. }
  64. ggtt->mtrr = arch_phys_wc_add(ggtt->gmadr.start,
  65. ggtt->mappable_end);
  66. }
  67. intel_ggtt_init_fences(ggtt);
  68. return 0;
  69. }
  70. /**
  71. * i915_ggtt_init_hw - Initialize GGTT hardware
  72. * @i915: i915 device
  73. */
  74. int i915_ggtt_init_hw(struct drm_i915_private *i915)
  75. {
  76. int ret;
  77. /*
  78. * Note that we use page colouring to enforce a guard page at the
  79. * end of the address space. This is required as the CS may prefetch
  80. * beyond the end of the batch buffer, across the page boundary,
  81. * and beyond the end of the GTT if we do not provide a guard.
  82. */
  83. ret = ggtt_init_hw(to_gt(i915)->ggtt);
  84. if (ret)
  85. return ret;
  86. return 0;
  87. }
  88. /**
  89. * i915_ggtt_suspend_vm - Suspend the memory mappings for a GGTT or DPT VM
  90. * @vm: The VM to suspend the mappings for
  91. * @evict_all: Evict all VMAs
  92. *
  93. * Suspend the memory mappings for all objects mapped to HW via the GGTT or a
  94. * DPT page table.
  95. */
  96. void i915_ggtt_suspend_vm(struct i915_address_space *vm, bool evict_all)
  97. {
  98. struct i915_vma *vma, *vn;
  99. int save_skip_rewrite;
  100. drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
  101. retry:
  102. i915_gem_drain_freed_objects(vm->i915);
  103. mutex_lock(&vm->mutex);
  104. /*
  105. * Skip rewriting PTE on VMA unbind.
  106. * FIXME: Use an argument to i915_vma_unbind() instead?
  107. */
  108. save_skip_rewrite = vm->skip_pte_rewrite;
  109. vm->skip_pte_rewrite = true;
  110. list_for_each_entry_safe(vma, vn, &vm->bound_list, vm_link) {
  111. struct drm_i915_gem_object *obj = vma->obj;
  112. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  113. if (i915_vma_is_pinned(vma) || !i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
  114. continue;
  115. /* unlikely to race when GPU is idle, so no worry about slowpath.. */
  116. if (WARN_ON(!i915_gem_object_trylock(obj, NULL))) {
  117. /*
  118. * No dead objects should appear here, GPU should be
  119. * completely idle, and userspace suspended
  120. */
  121. i915_gem_object_get(obj);
  122. mutex_unlock(&vm->mutex);
  123. i915_gem_object_lock(obj, NULL);
  124. GEM_WARN_ON(i915_vma_unbind(vma));
  125. i915_gem_object_unlock(obj);
  126. i915_gem_object_put(obj);
  127. vm->skip_pte_rewrite = save_skip_rewrite;
  128. goto retry;
  129. }
  130. if (evict_all || !i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND)) {
  131. i915_vma_wait_for_bind(vma);
  132. __i915_vma_evict(vma, false);
  133. drm_mm_remove_node(&vma->node);
  134. }
  135. i915_gem_object_unlock(obj);
  136. }
  137. vm->clear_range(vm, 0, vm->total);
  138. vm->skip_pte_rewrite = save_skip_rewrite;
  139. mutex_unlock(&vm->mutex);
  140. drm_WARN_ON(&vm->i915->drm, evict_all && !list_empty(&vm->bound_list));
  141. }
  142. void i915_ggtt_suspend(struct i915_ggtt *ggtt)
  143. {
  144. struct intel_gt *gt;
  145. i915_ggtt_suspend_vm(&ggtt->vm, false);
  146. ggtt->invalidate(ggtt);
  147. list_for_each_entry(gt, &ggtt->gt_list, ggtt_link)
  148. intel_gt_check_and_clear_faults(gt);
  149. }
  150. void gen6_ggtt_invalidate(struct i915_ggtt *ggtt)
  151. {
  152. struct intel_uncore *uncore = ggtt->vm.gt->uncore;
  153. spin_lock_irq(&uncore->lock);
  154. intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
  155. intel_uncore_read_fw(uncore, GFX_FLSH_CNTL_GEN6);
  156. spin_unlock_irq(&uncore->lock);
  157. }
  158. static bool needs_wc_ggtt_mapping(struct drm_i915_private *i915)
  159. {
  160. /*
  161. * On BXT+/ICL+ writes larger than 64 bit to the GTT pagetable range
  162. * will be dropped. For WC mappings in general we have 64 byte burst
  163. * writes when the WC buffer is flushed, so we can't use it, but have to
  164. * resort to an uncached mapping. The WC issue is easily caught by the
  165. * readback check when writing GTT PTE entries.
  166. */
  167. if (!IS_GEN9_LP(i915) && GRAPHICS_VER(i915) < 11)
  168. return true;
  169. return false;
  170. }
  171. static void gen8_ggtt_invalidate(struct i915_ggtt *ggtt)
  172. {
  173. struct intel_uncore *uncore = ggtt->vm.gt->uncore;
  174. /*
  175. * Note that as an uncached mmio write, this will flush the
  176. * WCB of the writes into the GGTT before it triggers the invalidate.
  177. *
  178. * Only perform this when GGTT is mapped as WC, see ggtt_probe_common().
  179. */
  180. if (needs_wc_ggtt_mapping(ggtt->vm.i915))
  181. intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6,
  182. GFX_FLSH_CNTL_EN);
  183. }
  184. static void guc_ggtt_ct_invalidate(struct intel_gt *gt)
  185. {
  186. struct intel_uncore *uncore = gt->uncore;
  187. intel_wakeref_t wakeref;
  188. with_intel_runtime_pm_if_active(uncore->rpm, wakeref)
  189. intel_guc_invalidate_tlb_guc(gt_to_guc(gt));
  190. }
  191. static void guc_ggtt_invalidate(struct i915_ggtt *ggtt)
  192. {
  193. struct drm_i915_private *i915 = ggtt->vm.i915;
  194. struct intel_gt *gt;
  195. gen8_ggtt_invalidate(ggtt);
  196. list_for_each_entry(gt, &ggtt->gt_list, ggtt_link) {
  197. if (intel_guc_tlb_invalidation_is_available(gt_to_guc(gt)))
  198. guc_ggtt_ct_invalidate(gt);
  199. else if (GRAPHICS_VER(i915) >= 12)
  200. intel_uncore_write_fw(gt->uncore,
  201. GEN12_GUC_TLB_INV_CR,
  202. GEN12_GUC_TLB_INV_CR_INVALIDATE);
  203. else
  204. intel_uncore_write_fw(gt->uncore,
  205. GEN8_GTCR, GEN8_GTCR_INVALIDATE);
  206. }
  207. }
  208. static u64 mtl_ggtt_pte_encode(dma_addr_t addr,
  209. unsigned int pat_index,
  210. u32 flags)
  211. {
  212. gen8_pte_t pte = addr | GEN8_PAGE_PRESENT;
  213. WARN_ON_ONCE(addr & ~GEN12_GGTT_PTE_ADDR_MASK);
  214. if (flags & PTE_LM)
  215. pte |= GEN12_GGTT_PTE_LM;
  216. if (pat_index & BIT(0))
  217. pte |= MTL_GGTT_PTE_PAT0;
  218. if (pat_index & BIT(1))
  219. pte |= MTL_GGTT_PTE_PAT1;
  220. return pte;
  221. }
  222. u64 gen8_ggtt_pte_encode(dma_addr_t addr,
  223. unsigned int pat_index,
  224. u32 flags)
  225. {
  226. gen8_pte_t pte = addr | GEN8_PAGE_PRESENT;
  227. if (flags & PTE_LM)
  228. pte |= GEN12_GGTT_PTE_LM;
  229. return pte;
  230. }
  231. static dma_addr_t gen8_ggtt_pte_decode(u64 pte, bool *is_present, bool *is_local)
  232. {
  233. *is_present = pte & GEN8_PAGE_PRESENT;
  234. *is_local = pte & GEN12_GGTT_PTE_LM;
  235. return pte & GEN12_GGTT_PTE_ADDR_MASK;
  236. }
  237. static bool should_update_ggtt_with_bind(struct i915_ggtt *ggtt)
  238. {
  239. struct intel_gt *gt = ggtt->vm.gt;
  240. return intel_gt_is_bind_context_ready(gt);
  241. }
  242. static struct intel_context *gen8_ggtt_bind_get_ce(struct i915_ggtt *ggtt, intel_wakeref_t *wakeref)
  243. {
  244. struct intel_context *ce;
  245. struct intel_gt *gt = ggtt->vm.gt;
  246. if (intel_gt_is_wedged(gt))
  247. return NULL;
  248. ce = gt->engine[BCS0]->bind_context;
  249. GEM_BUG_ON(!ce);
  250. /*
  251. * If the GT is not awake already at this stage then fallback
  252. * to pci based GGTT update otherwise __intel_wakeref_get_first()
  253. * would conflict with fs_reclaim trying to allocate memory while
  254. * doing rpm_resume().
  255. */
  256. *wakeref = intel_gt_pm_get_if_awake(gt);
  257. if (!*wakeref)
  258. return NULL;
  259. intel_engine_pm_get(ce->engine);
  260. return ce;
  261. }
  262. static void gen8_ggtt_bind_put_ce(struct intel_context *ce, intel_wakeref_t wakeref)
  263. {
  264. intel_engine_pm_put(ce->engine);
  265. intel_gt_pm_put(ce->engine->gt, wakeref);
  266. }
  267. static bool gen8_ggtt_bind_ptes(struct i915_ggtt *ggtt, u32 offset,
  268. struct sg_table *pages, u32 num_entries,
  269. const gen8_pte_t pte)
  270. {
  271. struct i915_sched_attr attr = {};
  272. struct intel_gt *gt = ggtt->vm.gt;
  273. const gen8_pte_t scratch_pte = ggtt->vm.scratch[0]->encode;
  274. struct sgt_iter iter;
  275. struct i915_request *rq;
  276. struct intel_context *ce;
  277. intel_wakeref_t wakeref;
  278. u32 *cs;
  279. if (!num_entries)
  280. return true;
  281. ce = gen8_ggtt_bind_get_ce(ggtt, &wakeref);
  282. if (!ce)
  283. return false;
  284. if (pages)
  285. iter = __sgt_iter(pages->sgl, true);
  286. while (num_entries) {
  287. int count = 0;
  288. dma_addr_t addr;
  289. /*
  290. * MI_UPDATE_GTT can update 512 entries in a single command but
  291. * that end up with engine reset, 511 works.
  292. */
  293. u32 n_ptes = min_t(u32, 511, num_entries);
  294. if (mutex_lock_interruptible(&ce->timeline->mutex))
  295. goto put_ce;
  296. intel_context_enter(ce);
  297. rq = __i915_request_create(ce, GFP_NOWAIT | GFP_ATOMIC);
  298. intel_context_exit(ce);
  299. if (IS_ERR(rq)) {
  300. GT_TRACE(gt, "Failed to get bind request\n");
  301. mutex_unlock(&ce->timeline->mutex);
  302. goto put_ce;
  303. }
  304. cs = intel_ring_begin(rq, 2 * n_ptes + 2);
  305. if (IS_ERR(cs)) {
  306. GT_TRACE(gt, "Failed to ring space for GGTT bind\n");
  307. i915_request_set_error_once(rq, PTR_ERR(cs));
  308. /* once a request is created, it must be queued */
  309. goto queue_err_rq;
  310. }
  311. *cs++ = MI_UPDATE_GTT | (2 * n_ptes);
  312. *cs++ = offset << 12;
  313. if (pages) {
  314. for_each_sgt_daddr_next(addr, iter) {
  315. if (count == n_ptes)
  316. break;
  317. *cs++ = lower_32_bits(pte | addr);
  318. *cs++ = upper_32_bits(pte | addr);
  319. count++;
  320. }
  321. /* fill remaining with scratch pte, if any */
  322. if (count < n_ptes) {
  323. memset64((u64 *)cs, scratch_pte,
  324. n_ptes - count);
  325. cs += (n_ptes - count) * 2;
  326. }
  327. } else {
  328. memset64((u64 *)cs, pte, n_ptes);
  329. cs += n_ptes * 2;
  330. }
  331. intel_ring_advance(rq, cs);
  332. queue_err_rq:
  333. i915_request_get(rq);
  334. __i915_request_commit(rq);
  335. __i915_request_queue(rq, &attr);
  336. mutex_unlock(&ce->timeline->mutex);
  337. /* This will break if the request is complete or after engine reset */
  338. i915_request_wait(rq, 0, MAX_SCHEDULE_TIMEOUT);
  339. if (rq->fence.error)
  340. goto err_rq;
  341. i915_request_put(rq);
  342. num_entries -= n_ptes;
  343. offset += n_ptes;
  344. }
  345. gen8_ggtt_bind_put_ce(ce, wakeref);
  346. return true;
  347. err_rq:
  348. i915_request_put(rq);
  349. put_ce:
  350. gen8_ggtt_bind_put_ce(ce, wakeref);
  351. return false;
  352. }
  353. static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
  354. {
  355. writeq(pte, addr);
  356. }
  357. static gen8_pte_t gen8_get_pte(void __iomem *addr)
  358. {
  359. return readq(addr);
  360. }
  361. static void gen8_ggtt_insert_page(struct i915_address_space *vm,
  362. dma_addr_t addr,
  363. u64 offset,
  364. unsigned int pat_index,
  365. u32 flags)
  366. {
  367. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  368. gen8_pte_t __iomem *pte =
  369. (gen8_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
  370. gen8_set_pte(pte, ggtt->vm.pte_encode(addr, pat_index, flags));
  371. ggtt->invalidate(ggtt);
  372. }
  373. static dma_addr_t gen8_ggtt_read_entry(struct i915_address_space *vm,
  374. u64 offset, bool *is_present, bool *is_local)
  375. {
  376. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  377. gen8_pte_t __iomem *pte =
  378. (gen8_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
  379. return ggtt->vm.pte_decode(gen8_get_pte(pte), is_present, is_local);
  380. }
  381. static void gen8_ggtt_insert_page_bind(struct i915_address_space *vm,
  382. dma_addr_t addr, u64 offset,
  383. unsigned int pat_index, u32 flags)
  384. {
  385. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  386. gen8_pte_t pte;
  387. pte = ggtt->vm.pte_encode(addr, pat_index, flags);
  388. if (should_update_ggtt_with_bind(i915_vm_to_ggtt(vm)) &&
  389. gen8_ggtt_bind_ptes(ggtt, offset, NULL, 1, pte))
  390. return ggtt->invalidate(ggtt);
  391. gen8_ggtt_insert_page(vm, addr, offset, pat_index, flags);
  392. }
  393. static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
  394. struct i915_vma_resource *vma_res,
  395. unsigned int pat_index,
  396. u32 flags)
  397. {
  398. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  399. const gen8_pte_t pte_encode = ggtt->vm.pte_encode(0, pat_index, flags);
  400. gen8_pte_t __iomem *gte;
  401. gen8_pte_t __iomem *end;
  402. struct sgt_iter iter;
  403. dma_addr_t addr;
  404. /*
  405. * Note that we ignore PTE_READ_ONLY here. The caller must be careful
  406. * not to allow the user to override access to a read only page.
  407. */
  408. gte = (gen8_pte_t __iomem *)ggtt->gsm;
  409. gte += (vma_res->start - vma_res->guard) / I915_GTT_PAGE_SIZE;
  410. end = gte + vma_res->guard / I915_GTT_PAGE_SIZE;
  411. while (gte < end)
  412. gen8_set_pte(gte++, vm->scratch[0]->encode);
  413. end += (vma_res->node_size + vma_res->guard) / I915_GTT_PAGE_SIZE;
  414. for_each_sgt_daddr(addr, iter, vma_res->bi.pages)
  415. gen8_set_pte(gte++, pte_encode | addr);
  416. GEM_BUG_ON(gte > end);
  417. /* Fill the allocated but "unused" space beyond the end of the buffer */
  418. while (gte < end)
  419. gen8_set_pte(gte++, vm->scratch[0]->encode);
  420. /*
  421. * We want to flush the TLBs only after we're certain all the PTE
  422. * updates have finished.
  423. */
  424. ggtt->invalidate(ggtt);
  425. }
  426. static bool __gen8_ggtt_insert_entries_bind(struct i915_address_space *vm,
  427. struct i915_vma_resource *vma_res,
  428. unsigned int pat_index, u32 flags)
  429. {
  430. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  431. gen8_pte_t scratch_pte = vm->scratch[0]->encode;
  432. gen8_pte_t pte_encode;
  433. u64 start, end;
  434. pte_encode = ggtt->vm.pte_encode(0, pat_index, flags);
  435. start = (vma_res->start - vma_res->guard) / I915_GTT_PAGE_SIZE;
  436. end = start + vma_res->guard / I915_GTT_PAGE_SIZE;
  437. if (!gen8_ggtt_bind_ptes(ggtt, start, NULL, end - start, scratch_pte))
  438. goto err;
  439. start = end;
  440. end += (vma_res->node_size + vma_res->guard) / I915_GTT_PAGE_SIZE;
  441. if (!gen8_ggtt_bind_ptes(ggtt, start, vma_res->bi.pages,
  442. vma_res->node_size / I915_GTT_PAGE_SIZE, pte_encode))
  443. goto err;
  444. start += vma_res->node_size / I915_GTT_PAGE_SIZE;
  445. if (!gen8_ggtt_bind_ptes(ggtt, start, NULL, end - start, scratch_pte))
  446. goto err;
  447. return true;
  448. err:
  449. return false;
  450. }
  451. static void gen8_ggtt_insert_entries_bind(struct i915_address_space *vm,
  452. struct i915_vma_resource *vma_res,
  453. unsigned int pat_index, u32 flags)
  454. {
  455. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  456. if (should_update_ggtt_with_bind(i915_vm_to_ggtt(vm)) &&
  457. __gen8_ggtt_insert_entries_bind(vm, vma_res, pat_index, flags))
  458. return ggtt->invalidate(ggtt);
  459. gen8_ggtt_insert_entries(vm, vma_res, pat_index, flags);
  460. }
  461. static void gen8_ggtt_clear_range(struct i915_address_space *vm,
  462. u64 start, u64 length)
  463. {
  464. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  465. unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
  466. unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
  467. const gen8_pte_t scratch_pte = vm->scratch[0]->encode;
  468. gen8_pte_t __iomem *gtt_base =
  469. (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
  470. const int max_entries = ggtt_total_entries(ggtt) - first_entry;
  471. int i;
  472. if (WARN(num_entries > max_entries,
  473. "First entry = %d; Num entries = %d (max=%d)\n",
  474. first_entry, num_entries, max_entries))
  475. num_entries = max_entries;
  476. for (i = 0; i < num_entries; i++)
  477. gen8_set_pte(&gtt_base[i], scratch_pte);
  478. }
  479. static void gen8_ggtt_scratch_range_bind(struct i915_address_space *vm,
  480. u64 start, u64 length)
  481. {
  482. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  483. unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
  484. unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
  485. const gen8_pte_t scratch_pte = vm->scratch[0]->encode;
  486. const int max_entries = ggtt_total_entries(ggtt) - first_entry;
  487. if (WARN(num_entries > max_entries,
  488. "First entry = %d; Num entries = %d (max=%d)\n",
  489. first_entry, num_entries, max_entries))
  490. num_entries = max_entries;
  491. if (should_update_ggtt_with_bind(ggtt) && gen8_ggtt_bind_ptes(ggtt, first_entry,
  492. NULL, num_entries, scratch_pte))
  493. return ggtt->invalidate(ggtt);
  494. gen8_ggtt_clear_range(vm, start, length);
  495. }
  496. static void gen6_ggtt_insert_page(struct i915_address_space *vm,
  497. dma_addr_t addr,
  498. u64 offset,
  499. unsigned int pat_index,
  500. u32 flags)
  501. {
  502. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  503. gen6_pte_t __iomem *pte =
  504. (gen6_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
  505. iowrite32(vm->pte_encode(addr, pat_index, flags), pte);
  506. ggtt->invalidate(ggtt);
  507. }
  508. static dma_addr_t gen6_ggtt_read_entry(struct i915_address_space *vm,
  509. u64 offset,
  510. bool *is_present, bool *is_local)
  511. {
  512. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  513. gen6_pte_t __iomem *pte =
  514. (gen6_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
  515. return vm->pte_decode(ioread32(pte), is_present, is_local);
  516. }
  517. /*
  518. * Binds an object into the global gtt with the specified cache level.
  519. * The object will be accessible to the GPU via commands whose operands
  520. * reference offsets within the global GTT as well as accessible by the GPU
  521. * through the GMADR mapped BAR (i915->mm.gtt->gtt).
  522. */
  523. static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
  524. struct i915_vma_resource *vma_res,
  525. unsigned int pat_index,
  526. u32 flags)
  527. {
  528. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  529. gen6_pte_t __iomem *gte;
  530. gen6_pte_t __iomem *end;
  531. struct sgt_iter iter;
  532. dma_addr_t addr;
  533. gte = (gen6_pte_t __iomem *)ggtt->gsm;
  534. gte += (vma_res->start - vma_res->guard) / I915_GTT_PAGE_SIZE;
  535. end = gte + vma_res->guard / I915_GTT_PAGE_SIZE;
  536. while (gte < end)
  537. iowrite32(vm->scratch[0]->encode, gte++);
  538. end += (vma_res->node_size + vma_res->guard) / I915_GTT_PAGE_SIZE;
  539. for_each_sgt_daddr(addr, iter, vma_res->bi.pages)
  540. iowrite32(vm->pte_encode(addr, pat_index, flags), gte++);
  541. GEM_BUG_ON(gte > end);
  542. /* Fill the allocated but "unused" space beyond the end of the buffer */
  543. while (gte < end)
  544. iowrite32(vm->scratch[0]->encode, gte++);
  545. /*
  546. * We want to flush the TLBs only after we're certain all the PTE
  547. * updates have finished.
  548. */
  549. ggtt->invalidate(ggtt);
  550. }
  551. static void nop_clear_range(struct i915_address_space *vm,
  552. u64 start, u64 length)
  553. {
  554. }
  555. static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
  556. {
  557. /*
  558. * Make sure the internal GAM fifo has been cleared of all GTT
  559. * writes before exiting stop_machine(). This guarantees that
  560. * any aperture accesses waiting to start in another process
  561. * cannot back up behind the GTT writes causing a hang.
  562. * The register can be any arbitrary GAM register.
  563. */
  564. intel_uncore_posting_read_fw(vm->gt->uncore, GFX_FLSH_CNTL_GEN6);
  565. }
  566. struct insert_page {
  567. struct i915_address_space *vm;
  568. dma_addr_t addr;
  569. u64 offset;
  570. unsigned int pat_index;
  571. };
  572. static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
  573. {
  574. struct insert_page *arg = _arg;
  575. gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset,
  576. arg->pat_index, 0);
  577. bxt_vtd_ggtt_wa(arg->vm);
  578. return 0;
  579. }
  580. static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
  581. dma_addr_t addr,
  582. u64 offset,
  583. unsigned int pat_index,
  584. u32 unused)
  585. {
  586. struct insert_page arg = { vm, addr, offset, pat_index };
  587. stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
  588. }
  589. struct insert_entries {
  590. struct i915_address_space *vm;
  591. struct i915_vma_resource *vma_res;
  592. unsigned int pat_index;
  593. u32 flags;
  594. };
  595. static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
  596. {
  597. struct insert_entries *arg = _arg;
  598. gen8_ggtt_insert_entries(arg->vm, arg->vma_res,
  599. arg->pat_index, arg->flags);
  600. bxt_vtd_ggtt_wa(arg->vm);
  601. return 0;
  602. }
  603. static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
  604. struct i915_vma_resource *vma_res,
  605. unsigned int pat_index,
  606. u32 flags)
  607. {
  608. struct insert_entries arg = { vm, vma_res, pat_index, flags };
  609. stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
  610. }
  611. static void gen6_ggtt_clear_range(struct i915_address_space *vm,
  612. u64 start, u64 length)
  613. {
  614. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  615. unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
  616. unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
  617. gen6_pte_t scratch_pte, __iomem *gtt_base =
  618. (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
  619. const int max_entries = ggtt_total_entries(ggtt) - first_entry;
  620. int i;
  621. if (WARN(num_entries > max_entries,
  622. "First entry = %d; Num entries = %d (max=%d)\n",
  623. first_entry, num_entries, max_entries))
  624. num_entries = max_entries;
  625. scratch_pte = vm->scratch[0]->encode;
  626. for (i = 0; i < num_entries; i++)
  627. iowrite32(scratch_pte, &gtt_base[i]);
  628. }
  629. void intel_ggtt_bind_vma(struct i915_address_space *vm,
  630. struct i915_vm_pt_stash *stash,
  631. struct i915_vma_resource *vma_res,
  632. unsigned int pat_index,
  633. u32 flags)
  634. {
  635. u32 pte_flags;
  636. if (vma_res->bound_flags & (~flags & I915_VMA_BIND_MASK))
  637. return;
  638. vma_res->bound_flags |= flags;
  639. /* Applicable to VLV (gen8+ do not support RO in the GGTT) */
  640. pte_flags = 0;
  641. if (vma_res->bi.readonly)
  642. pte_flags |= PTE_READ_ONLY;
  643. if (vma_res->bi.lmem)
  644. pte_flags |= PTE_LM;
  645. vm->insert_entries(vm, vma_res, pat_index, pte_flags);
  646. vma_res->page_sizes_gtt = I915_GTT_PAGE_SIZE;
  647. }
  648. void intel_ggtt_unbind_vma(struct i915_address_space *vm,
  649. struct i915_vma_resource *vma_res)
  650. {
  651. vm->clear_range(vm, vma_res->start, vma_res->vma_size);
  652. }
  653. dma_addr_t intel_ggtt_read_entry(struct i915_address_space *vm,
  654. u64 offset, bool *is_present, bool *is_local)
  655. {
  656. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  657. return ggtt->vm.read_entry(vm, offset, is_present, is_local);
  658. }
  659. /*
  660. * Reserve the top of the GuC address space for firmware images. Addresses
  661. * beyond GUC_GGTT_TOP in the GuC address space are inaccessible by GuC,
  662. * which makes for a suitable range to hold GuC/HuC firmware images if the
  663. * size of the GGTT is 4G. However, on a 32-bit platform the size of the GGTT
  664. * is limited to 2G, which is less than GUC_GGTT_TOP, but we reserve a chunk
  665. * of the same size anyway, which is far more than needed, to keep the logic
  666. * in uc_fw_ggtt_offset() simple.
  667. */
  668. #define GUC_TOP_RESERVE_SIZE (SZ_4G - GUC_GGTT_TOP)
  669. static int ggtt_reserve_guc_top(struct i915_ggtt *ggtt)
  670. {
  671. u64 offset;
  672. int ret;
  673. if (!intel_uc_uses_guc(&ggtt->vm.gt->uc))
  674. return 0;
  675. GEM_BUG_ON(ggtt->vm.total <= GUC_TOP_RESERVE_SIZE);
  676. offset = ggtt->vm.total - GUC_TOP_RESERVE_SIZE;
  677. ret = i915_gem_gtt_reserve(&ggtt->vm, NULL, &ggtt->uc_fw,
  678. GUC_TOP_RESERVE_SIZE, offset,
  679. I915_COLOR_UNEVICTABLE, PIN_NOEVICT);
  680. if (ret)
  681. drm_dbg(&ggtt->vm.i915->drm,
  682. "Failed to reserve top of GGTT for GuC\n");
  683. return ret;
  684. }
  685. static void ggtt_release_guc_top(struct i915_ggtt *ggtt)
  686. {
  687. if (drm_mm_node_allocated(&ggtt->uc_fw))
  688. drm_mm_remove_node(&ggtt->uc_fw);
  689. }
  690. static void cleanup_init_ggtt(struct i915_ggtt *ggtt)
  691. {
  692. ggtt_release_guc_top(ggtt);
  693. if (drm_mm_node_allocated(&ggtt->error_capture))
  694. drm_mm_remove_node(&ggtt->error_capture);
  695. mutex_destroy(&ggtt->error_mutex);
  696. }
  697. static int init_ggtt(struct i915_ggtt *ggtt)
  698. {
  699. /*
  700. * Let GEM Manage all of the aperture.
  701. *
  702. * However, leave one page at the end still bound to the scratch page.
  703. * There are a number of places where the hardware apparently prefetches
  704. * past the end of the object, and we've seen multiple hangs with the
  705. * GPU head pointer stuck in a batchbuffer bound at the last page of the
  706. * aperture. One page should be enough to keep any prefetching inside
  707. * of the aperture.
  708. */
  709. unsigned long hole_start, hole_end;
  710. struct drm_mm_node *entry;
  711. int ret;
  712. /*
  713. * GuC requires all resources that we're sharing with it to be placed in
  714. * non-WOPCM memory. If GuC is not present or not in use we still need a
  715. * small bias as ring wraparound at offset 0 sometimes hangs. No idea
  716. * why.
  717. */
  718. ggtt->pin_bias = max_t(u32, I915_GTT_PAGE_SIZE,
  719. intel_wopcm_guc_size(&ggtt->vm.gt->wopcm));
  720. ret = intel_vgt_balloon(ggtt);
  721. if (ret)
  722. return ret;
  723. mutex_init(&ggtt->error_mutex);
  724. if (ggtt->mappable_end) {
  725. /*
  726. * Reserve a mappable slot for our lockless error capture.
  727. *
  728. * We strongly prefer taking address 0x0 in order to protect
  729. * other critical buffers against accidental overwrites,
  730. * as writing to address 0 is a very common mistake.
  731. *
  732. * Since 0 may already be in use by the system (e.g. the BIOS
  733. * framebuffer), we let the reservation fail quietly and hope
  734. * 0 remains reserved always.
  735. *
  736. * If we fail to reserve 0, and then fail to find any space
  737. * for an error-capture, remain silent. We can afford not
  738. * to reserve an error_capture node as we have fallback
  739. * paths, and we trust that 0 will remain reserved. However,
  740. * the only likely reason for failure to insert is a driver
  741. * bug, which we expect to cause other failures...
  742. *
  743. * Since CPU can perform speculative reads on error capture
  744. * (write-combining allows it) add scratch page after error
  745. * capture to avoid DMAR errors.
  746. */
  747. ggtt->error_capture.size = 2 * I915_GTT_PAGE_SIZE;
  748. ggtt->error_capture.color = I915_COLOR_UNEVICTABLE;
  749. if (drm_mm_reserve_node(&ggtt->vm.mm, &ggtt->error_capture))
  750. drm_mm_insert_node_in_range(&ggtt->vm.mm,
  751. &ggtt->error_capture,
  752. ggtt->error_capture.size, 0,
  753. ggtt->error_capture.color,
  754. 0, ggtt->mappable_end,
  755. DRM_MM_INSERT_LOW);
  756. }
  757. if (drm_mm_node_allocated(&ggtt->error_capture)) {
  758. u64 start = ggtt->error_capture.start;
  759. u64 size = ggtt->error_capture.size;
  760. ggtt->vm.scratch_range(&ggtt->vm, start, size);
  761. drm_dbg(&ggtt->vm.i915->drm,
  762. "Reserved GGTT:[%llx, %llx] for use by error capture\n",
  763. start, start + size);
  764. }
  765. /*
  766. * The upper portion of the GuC address space has a sizeable hole
  767. * (several MB) that is inaccessible by GuC. Reserve this range within
  768. * GGTT as it can comfortably hold GuC/HuC firmware images.
  769. */
  770. ret = ggtt_reserve_guc_top(ggtt);
  771. if (ret)
  772. goto err;
  773. /* Clear any non-preallocated blocks */
  774. drm_mm_for_each_hole(entry, &ggtt->vm.mm, hole_start, hole_end) {
  775. drm_dbg(&ggtt->vm.i915->drm,
  776. "clearing unused GTT space: [%lx, %lx]\n",
  777. hole_start, hole_end);
  778. ggtt->vm.clear_range(&ggtt->vm, hole_start,
  779. hole_end - hole_start);
  780. }
  781. /* And finally clear the reserved guard page */
  782. ggtt->vm.clear_range(&ggtt->vm, ggtt->vm.total - PAGE_SIZE, PAGE_SIZE);
  783. return 0;
  784. err:
  785. cleanup_init_ggtt(ggtt);
  786. return ret;
  787. }
  788. static void aliasing_gtt_bind_vma(struct i915_address_space *vm,
  789. struct i915_vm_pt_stash *stash,
  790. struct i915_vma_resource *vma_res,
  791. unsigned int pat_index,
  792. u32 flags)
  793. {
  794. u32 pte_flags;
  795. /* Currently applicable only to VLV */
  796. pte_flags = 0;
  797. if (vma_res->bi.readonly)
  798. pte_flags |= PTE_READ_ONLY;
  799. if (flags & I915_VMA_LOCAL_BIND)
  800. ppgtt_bind_vma(&i915_vm_to_ggtt(vm)->alias->vm,
  801. stash, vma_res, pat_index, flags);
  802. if (flags & I915_VMA_GLOBAL_BIND)
  803. vm->insert_entries(vm, vma_res, pat_index, pte_flags);
  804. vma_res->bound_flags |= flags;
  805. }
  806. static void aliasing_gtt_unbind_vma(struct i915_address_space *vm,
  807. struct i915_vma_resource *vma_res)
  808. {
  809. if (vma_res->bound_flags & I915_VMA_GLOBAL_BIND)
  810. vm->clear_range(vm, vma_res->start, vma_res->vma_size);
  811. if (vma_res->bound_flags & I915_VMA_LOCAL_BIND)
  812. ppgtt_unbind_vma(&i915_vm_to_ggtt(vm)->alias->vm, vma_res);
  813. }
  814. static int init_aliasing_ppgtt(struct i915_ggtt *ggtt)
  815. {
  816. struct i915_vm_pt_stash stash = {};
  817. struct i915_ppgtt *ppgtt;
  818. int err;
  819. ppgtt = i915_ppgtt_create(ggtt->vm.gt, 0);
  820. if (IS_ERR(ppgtt))
  821. return PTR_ERR(ppgtt);
  822. if (GEM_WARN_ON(ppgtt->vm.total < ggtt->vm.total)) {
  823. err = -ENODEV;
  824. goto err_ppgtt;
  825. }
  826. err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, ggtt->vm.total);
  827. if (err)
  828. goto err_ppgtt;
  829. i915_gem_object_lock(ppgtt->vm.scratch[0], NULL);
  830. err = i915_vm_map_pt_stash(&ppgtt->vm, &stash);
  831. i915_gem_object_unlock(ppgtt->vm.scratch[0]);
  832. if (err)
  833. goto err_stash;
  834. /*
  835. * Note we only pre-allocate as far as the end of the global
  836. * GTT. On 48b / 4-level page-tables, the difference is very,
  837. * very significant! We have to preallocate as GVT/vgpu does
  838. * not like the page directory disappearing.
  839. */
  840. ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash, 0, ggtt->vm.total);
  841. ggtt->alias = ppgtt;
  842. ggtt->vm.bind_async_flags |= ppgtt->vm.bind_async_flags;
  843. GEM_BUG_ON(ggtt->vm.vma_ops.bind_vma != intel_ggtt_bind_vma);
  844. ggtt->vm.vma_ops.bind_vma = aliasing_gtt_bind_vma;
  845. GEM_BUG_ON(ggtt->vm.vma_ops.unbind_vma != intel_ggtt_unbind_vma);
  846. ggtt->vm.vma_ops.unbind_vma = aliasing_gtt_unbind_vma;
  847. i915_vm_free_pt_stash(&ppgtt->vm, &stash);
  848. return 0;
  849. err_stash:
  850. i915_vm_free_pt_stash(&ppgtt->vm, &stash);
  851. err_ppgtt:
  852. i915_vm_put(&ppgtt->vm);
  853. return err;
  854. }
  855. static void fini_aliasing_ppgtt(struct i915_ggtt *ggtt)
  856. {
  857. struct i915_ppgtt *ppgtt;
  858. ppgtt = fetch_and_zero(&ggtt->alias);
  859. if (!ppgtt)
  860. return;
  861. i915_vm_put(&ppgtt->vm);
  862. ggtt->vm.vma_ops.bind_vma = intel_ggtt_bind_vma;
  863. ggtt->vm.vma_ops.unbind_vma = intel_ggtt_unbind_vma;
  864. }
  865. int i915_init_ggtt(struct drm_i915_private *i915)
  866. {
  867. int ret;
  868. ret = init_ggtt(to_gt(i915)->ggtt);
  869. if (ret)
  870. return ret;
  871. if (INTEL_PPGTT(i915) == INTEL_PPGTT_ALIASING) {
  872. ret = init_aliasing_ppgtt(to_gt(i915)->ggtt);
  873. if (ret)
  874. cleanup_init_ggtt(to_gt(i915)->ggtt);
  875. }
  876. return 0;
  877. }
  878. static void ggtt_cleanup_hw(struct i915_ggtt *ggtt)
  879. {
  880. struct i915_vma *vma, *vn;
  881. flush_workqueue(ggtt->vm.i915->wq);
  882. i915_gem_drain_freed_objects(ggtt->vm.i915);
  883. mutex_lock(&ggtt->vm.mutex);
  884. ggtt->vm.skip_pte_rewrite = true;
  885. list_for_each_entry_safe(vma, vn, &ggtt->vm.bound_list, vm_link) {
  886. struct drm_i915_gem_object *obj = vma->obj;
  887. bool trylock;
  888. trylock = i915_gem_object_trylock(obj, NULL);
  889. WARN_ON(!trylock);
  890. WARN_ON(__i915_vma_unbind(vma));
  891. if (trylock)
  892. i915_gem_object_unlock(obj);
  893. }
  894. if (drm_mm_node_allocated(&ggtt->error_capture))
  895. drm_mm_remove_node(&ggtt->error_capture);
  896. mutex_destroy(&ggtt->error_mutex);
  897. ggtt_release_guc_top(ggtt);
  898. intel_vgt_deballoon(ggtt);
  899. ggtt->vm.cleanup(&ggtt->vm);
  900. mutex_unlock(&ggtt->vm.mutex);
  901. i915_address_space_fini(&ggtt->vm);
  902. arch_phys_wc_del(ggtt->mtrr);
  903. if (ggtt->iomap.size)
  904. io_mapping_fini(&ggtt->iomap);
  905. }
  906. /**
  907. * i915_ggtt_driver_release - Clean up GGTT hardware initialization
  908. * @i915: i915 device
  909. */
  910. void i915_ggtt_driver_release(struct drm_i915_private *i915)
  911. {
  912. struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
  913. fini_aliasing_ppgtt(ggtt);
  914. intel_ggtt_fini_fences(ggtt);
  915. ggtt_cleanup_hw(ggtt);
  916. }
  917. /**
  918. * i915_ggtt_driver_late_release - Cleanup of GGTT that needs to be done after
  919. * all free objects have been drained.
  920. * @i915: i915 device
  921. */
  922. void i915_ggtt_driver_late_release(struct drm_i915_private *i915)
  923. {
  924. struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
  925. GEM_WARN_ON(kref_read(&ggtt->vm.resv_ref) != 1);
  926. dma_resv_fini(&ggtt->vm._resv);
  927. }
  928. static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
  929. {
  930. snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
  931. snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
  932. return snb_gmch_ctl << 20;
  933. }
  934. static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
  935. {
  936. bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
  937. bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
  938. if (bdw_gmch_ctl)
  939. bdw_gmch_ctl = 1 << bdw_gmch_ctl;
  940. #ifdef CONFIG_X86_32
  941. /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * I915_GTT_PAGE_SIZE */
  942. if (bdw_gmch_ctl > 4)
  943. bdw_gmch_ctl = 4;
  944. #endif
  945. return bdw_gmch_ctl << 20;
  946. }
  947. static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
  948. {
  949. gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
  950. gmch_ctrl &= SNB_GMCH_GGMS_MASK;
  951. if (gmch_ctrl)
  952. return 1 << (20 + gmch_ctrl);
  953. return 0;
  954. }
  955. static unsigned int gen6_gttmmadr_size(struct drm_i915_private *i915)
  956. {
  957. /*
  958. * GEN6: GTTMMADR size is 4MB and GTTADR starts at 2MB offset
  959. * GEN8: GTTMMADR size is 16MB and GTTADR starts at 8MB offset
  960. */
  961. GEM_BUG_ON(GRAPHICS_VER(i915) < 6);
  962. return (GRAPHICS_VER(i915) < 8) ? SZ_4M : SZ_16M;
  963. }
  964. static unsigned int gen6_gttadr_offset(struct drm_i915_private *i915)
  965. {
  966. return gen6_gttmmadr_size(i915) / 2;
  967. }
  968. static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
  969. {
  970. struct drm_i915_private *i915 = ggtt->vm.i915;
  971. struct intel_uncore *uncore = ggtt->vm.gt->uncore;
  972. struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
  973. phys_addr_t phys_addr;
  974. u32 pte_flags;
  975. int ret;
  976. GEM_WARN_ON(pci_resource_len(pdev, GEN4_GTTMMADR_BAR) != gen6_gttmmadr_size(i915));
  977. if (i915_direct_stolen_access(i915)) {
  978. drm_dbg(&i915->drm, "Using direct GSM access\n");
  979. phys_addr = intel_uncore_read64(uncore, GEN6_GSMBASE) & GEN11_BDSM_MASK;
  980. } else {
  981. phys_addr = pci_resource_start(pdev, GEN4_GTTMMADR_BAR) + gen6_gttadr_offset(i915);
  982. }
  983. if (needs_wc_ggtt_mapping(i915))
  984. ggtt->gsm = ioremap_wc(phys_addr, size);
  985. else
  986. ggtt->gsm = ioremap(phys_addr, size);
  987. if (!ggtt->gsm) {
  988. drm_err(&i915->drm, "Failed to map the ggtt page table\n");
  989. return -ENOMEM;
  990. }
  991. kref_init(&ggtt->vm.resv_ref);
  992. ret = setup_scratch_page(&ggtt->vm);
  993. if (ret) {
  994. drm_err(&i915->drm, "Scratch setup failed\n");
  995. /* iounmap will also get called at remove, but meh */
  996. iounmap(ggtt->gsm);
  997. return ret;
  998. }
  999. pte_flags = 0;
  1000. if (i915_gem_object_is_lmem(ggtt->vm.scratch[0]))
  1001. pte_flags |= PTE_LM;
  1002. ggtt->vm.scratch[0]->encode =
  1003. ggtt->vm.pte_encode(px_dma(ggtt->vm.scratch[0]),
  1004. i915_gem_get_pat_index(i915,
  1005. I915_CACHE_NONE),
  1006. pte_flags);
  1007. return 0;
  1008. }
  1009. static void gen6_gmch_remove(struct i915_address_space *vm)
  1010. {
  1011. struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
  1012. iounmap(ggtt->gsm);
  1013. free_scratch(vm);
  1014. }
  1015. static struct resource pci_resource(struct pci_dev *pdev, int bar)
  1016. {
  1017. return DEFINE_RES_MEM(pci_resource_start(pdev, bar),
  1018. pci_resource_len(pdev, bar));
  1019. }
  1020. static int gen8_gmch_probe(struct i915_ggtt *ggtt)
  1021. {
  1022. struct drm_i915_private *i915 = ggtt->vm.i915;
  1023. struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
  1024. unsigned int size;
  1025. u16 snb_gmch_ctl;
  1026. if (!HAS_LMEM(i915) && !HAS_LMEMBAR_SMEM_STOLEN(i915)) {
  1027. if (!i915_pci_resource_valid(pdev, GEN4_GMADR_BAR))
  1028. return -ENXIO;
  1029. ggtt->gmadr = pci_resource(pdev, GEN4_GMADR_BAR);
  1030. ggtt->mappable_end = resource_size(&ggtt->gmadr);
  1031. }
  1032. pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  1033. if (IS_CHERRYVIEW(i915))
  1034. size = chv_get_total_gtt_size(snb_gmch_ctl);
  1035. else
  1036. size = gen8_get_total_gtt_size(snb_gmch_ctl);
  1037. ggtt->vm.alloc_pt_dma = alloc_pt_dma;
  1038. ggtt->vm.alloc_scratch_dma = alloc_pt_dma;
  1039. ggtt->vm.lmem_pt_obj_flags = I915_BO_ALLOC_PM_EARLY;
  1040. ggtt->vm.total = (size / sizeof(gen8_pte_t)) * I915_GTT_PAGE_SIZE;
  1041. ggtt->vm.cleanup = gen6_gmch_remove;
  1042. ggtt->vm.insert_page = gen8_ggtt_insert_page;
  1043. ggtt->vm.clear_range = nop_clear_range;
  1044. ggtt->vm.scratch_range = gen8_ggtt_clear_range;
  1045. ggtt->vm.insert_entries = gen8_ggtt_insert_entries;
  1046. ggtt->vm.read_entry = gen8_ggtt_read_entry;
  1047. /*
  1048. * Serialize GTT updates with aperture access on BXT if VT-d is on,
  1049. * and always on CHV.
  1050. */
  1051. if (intel_vm_no_concurrent_access_wa(i915)) {
  1052. ggtt->vm.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
  1053. ggtt->vm.insert_page = bxt_vtd_ggtt_insert_page__BKL;
  1054. /*
  1055. * Calling stop_machine() version of GGTT update function
  1056. * at error capture/reset path will raise lockdep warning.
  1057. * Allow calling gen8_ggtt_insert_* directly at reset path
  1058. * which is safe from parallel GGTT updates.
  1059. */
  1060. ggtt->vm.raw_insert_page = gen8_ggtt_insert_page;
  1061. ggtt->vm.raw_insert_entries = gen8_ggtt_insert_entries;
  1062. ggtt->vm.bind_async_flags =
  1063. I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
  1064. }
  1065. if (i915_ggtt_require_binder(i915)) {
  1066. ggtt->vm.scratch_range = gen8_ggtt_scratch_range_bind;
  1067. ggtt->vm.insert_page = gen8_ggtt_insert_page_bind;
  1068. ggtt->vm.insert_entries = gen8_ggtt_insert_entries_bind;
  1069. /*
  1070. * On GPU is hung, we might bind VMAs for error capture.
  1071. * Fallback to CPU GGTT updates in that case.
  1072. */
  1073. ggtt->vm.raw_insert_page = gen8_ggtt_insert_page;
  1074. }
  1075. if (intel_uc_wants_guc_submission(&ggtt->vm.gt->uc))
  1076. ggtt->invalidate = guc_ggtt_invalidate;
  1077. else
  1078. ggtt->invalidate = gen8_ggtt_invalidate;
  1079. ggtt->vm.vma_ops.bind_vma = intel_ggtt_bind_vma;
  1080. ggtt->vm.vma_ops.unbind_vma = intel_ggtt_unbind_vma;
  1081. if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70))
  1082. ggtt->vm.pte_encode = mtl_ggtt_pte_encode;
  1083. else
  1084. ggtt->vm.pte_encode = gen8_ggtt_pte_encode;
  1085. ggtt->vm.pte_decode = gen8_ggtt_pte_decode;
  1086. return ggtt_probe_common(ggtt, size);
  1087. }
  1088. /*
  1089. * For pre-gen8 platforms pat_index is the same as enum i915_cache_level,
  1090. * so the switch-case statements in these PTE encode functions are still valid.
  1091. * See translation table LEGACY_CACHELEVEL.
  1092. */
  1093. static u64 snb_pte_encode(dma_addr_t addr,
  1094. unsigned int pat_index,
  1095. u32 flags)
  1096. {
  1097. gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
  1098. switch (pat_index) {
  1099. case I915_CACHE_L3_LLC:
  1100. case I915_CACHE_LLC:
  1101. pte |= GEN6_PTE_CACHE_LLC;
  1102. break;
  1103. case I915_CACHE_NONE:
  1104. pte |= GEN6_PTE_UNCACHED;
  1105. break;
  1106. default:
  1107. MISSING_CASE(pat_index);
  1108. }
  1109. return pte;
  1110. }
  1111. static u64 ivb_pte_encode(dma_addr_t addr,
  1112. unsigned int pat_index,
  1113. u32 flags)
  1114. {
  1115. gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
  1116. switch (pat_index) {
  1117. case I915_CACHE_L3_LLC:
  1118. pte |= GEN7_PTE_CACHE_L3_LLC;
  1119. break;
  1120. case I915_CACHE_LLC:
  1121. pte |= GEN6_PTE_CACHE_LLC;
  1122. break;
  1123. case I915_CACHE_NONE:
  1124. pte |= GEN6_PTE_UNCACHED;
  1125. break;
  1126. default:
  1127. MISSING_CASE(pat_index);
  1128. }
  1129. return pte;
  1130. }
  1131. static u64 byt_pte_encode(dma_addr_t addr,
  1132. unsigned int pat_index,
  1133. u32 flags)
  1134. {
  1135. gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
  1136. if (!(flags & PTE_READ_ONLY))
  1137. pte |= BYT_PTE_WRITEABLE;
  1138. if (pat_index != I915_CACHE_NONE)
  1139. pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
  1140. return pte;
  1141. }
  1142. static u64 hsw_pte_encode(dma_addr_t addr,
  1143. unsigned int pat_index,
  1144. u32 flags)
  1145. {
  1146. gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
  1147. if (pat_index != I915_CACHE_NONE)
  1148. pte |= HSW_WB_LLC_AGE3;
  1149. return pte;
  1150. }
  1151. static u64 iris_pte_encode(dma_addr_t addr,
  1152. unsigned int pat_index,
  1153. u32 flags)
  1154. {
  1155. gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
  1156. switch (pat_index) {
  1157. case I915_CACHE_NONE:
  1158. break;
  1159. case I915_CACHE_WT:
  1160. pte |= HSW_WT_ELLC_LLC_AGE3;
  1161. break;
  1162. default:
  1163. pte |= HSW_WB_ELLC_LLC_AGE3;
  1164. break;
  1165. }
  1166. return pte;
  1167. }
  1168. static dma_addr_t gen6_pte_decode(u64 pte, bool *is_present, bool *is_local)
  1169. {
  1170. *is_present = pte & GEN6_PTE_VALID;
  1171. *is_local = false;
  1172. return ((pte & 0xff0) << 28) | (pte & ~0xfff);
  1173. }
  1174. static int gen6_gmch_probe(struct i915_ggtt *ggtt)
  1175. {
  1176. struct drm_i915_private *i915 = ggtt->vm.i915;
  1177. struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
  1178. unsigned int size;
  1179. u16 snb_gmch_ctl;
  1180. if (!i915_pci_resource_valid(pdev, GEN4_GMADR_BAR))
  1181. return -ENXIO;
  1182. ggtt->gmadr = pci_resource(pdev, GEN4_GMADR_BAR);
  1183. ggtt->mappable_end = resource_size(&ggtt->gmadr);
  1184. /*
  1185. * 64/512MB is the current min/max we actually know of, but this is
  1186. * just a coarse sanity check.
  1187. */
  1188. if (ggtt->mappable_end < (64 << 20) ||
  1189. ggtt->mappable_end > (512 << 20)) {
  1190. drm_err(&i915->drm, "Unknown GMADR size (%pa)\n",
  1191. &ggtt->mappable_end);
  1192. return -ENXIO;
  1193. }
  1194. pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
  1195. size = gen6_get_total_gtt_size(snb_gmch_ctl);
  1196. ggtt->vm.total = (size / sizeof(gen6_pte_t)) * I915_GTT_PAGE_SIZE;
  1197. ggtt->vm.alloc_pt_dma = alloc_pt_dma;
  1198. ggtt->vm.alloc_scratch_dma = alloc_pt_dma;
  1199. ggtt->vm.clear_range = nop_clear_range;
  1200. if (!HAS_FULL_PPGTT(i915))
  1201. ggtt->vm.clear_range = gen6_ggtt_clear_range;
  1202. ggtt->vm.scratch_range = gen6_ggtt_clear_range;
  1203. ggtt->vm.insert_page = gen6_ggtt_insert_page;
  1204. ggtt->vm.insert_entries = gen6_ggtt_insert_entries;
  1205. ggtt->vm.read_entry = gen6_ggtt_read_entry;
  1206. ggtt->vm.cleanup = gen6_gmch_remove;
  1207. ggtt->invalidate = gen6_ggtt_invalidate;
  1208. if (HAS_EDRAM(i915))
  1209. ggtt->vm.pte_encode = iris_pte_encode;
  1210. else if (IS_HASWELL(i915))
  1211. ggtt->vm.pte_encode = hsw_pte_encode;
  1212. else if (IS_VALLEYVIEW(i915))
  1213. ggtt->vm.pte_encode = byt_pte_encode;
  1214. else if (GRAPHICS_VER(i915) >= 7)
  1215. ggtt->vm.pte_encode = ivb_pte_encode;
  1216. else
  1217. ggtt->vm.pte_encode = snb_pte_encode;
  1218. ggtt->vm.pte_decode = gen6_pte_decode;
  1219. ggtt->vm.vma_ops.bind_vma = intel_ggtt_bind_vma;
  1220. ggtt->vm.vma_ops.unbind_vma = intel_ggtt_unbind_vma;
  1221. return ggtt_probe_common(ggtt, size);
  1222. }
  1223. static int ggtt_probe_hw(struct i915_ggtt *ggtt, struct intel_gt *gt)
  1224. {
  1225. struct drm_i915_private *i915 = gt->i915;
  1226. int ret;
  1227. ggtt->vm.gt = gt;
  1228. ggtt->vm.i915 = i915;
  1229. ggtt->vm.dma = i915->drm.dev;
  1230. dma_resv_init(&ggtt->vm._resv);
  1231. if (GRAPHICS_VER(i915) >= 8)
  1232. ret = gen8_gmch_probe(ggtt);
  1233. else if (GRAPHICS_VER(i915) >= 6)
  1234. ret = gen6_gmch_probe(ggtt);
  1235. else
  1236. ret = intel_ggtt_gmch_probe(ggtt);
  1237. if (ret) {
  1238. dma_resv_fini(&ggtt->vm._resv);
  1239. return ret;
  1240. }
  1241. if ((ggtt->vm.total - 1) >> 32) {
  1242. drm_err(&i915->drm,
  1243. "We never expected a Global GTT with more than 32bits"
  1244. " of address space! Found %lldM!\n",
  1245. ggtt->vm.total >> 20);
  1246. ggtt->vm.total = 1ULL << 32;
  1247. ggtt->mappable_end =
  1248. min_t(u64, ggtt->mappable_end, ggtt->vm.total);
  1249. }
  1250. if (ggtt->mappable_end > ggtt->vm.total) {
  1251. drm_err(&i915->drm,
  1252. "mappable aperture extends past end of GGTT,"
  1253. " aperture=%pa, total=%llx\n",
  1254. &ggtt->mappable_end, ggtt->vm.total);
  1255. ggtt->mappable_end = ggtt->vm.total;
  1256. }
  1257. /* GMADR is the PCI mmio aperture into the global GTT. */
  1258. drm_dbg(&i915->drm, "GGTT size = %lluM\n", ggtt->vm.total >> 20);
  1259. drm_dbg(&i915->drm, "GMADR size = %lluM\n",
  1260. (u64)ggtt->mappable_end >> 20);
  1261. drm_dbg(&i915->drm, "DSM size = %lluM\n",
  1262. (u64)resource_size(&intel_graphics_stolen_res) >> 20);
  1263. return 0;
  1264. }
  1265. /**
  1266. * i915_ggtt_probe_hw - Probe GGTT hardware location
  1267. * @i915: i915 device
  1268. */
  1269. int i915_ggtt_probe_hw(struct drm_i915_private *i915)
  1270. {
  1271. struct intel_gt *gt;
  1272. int ret, i;
  1273. for_each_gt(gt, i915, i) {
  1274. ret = intel_gt_assign_ggtt(gt);
  1275. if (ret)
  1276. return ret;
  1277. }
  1278. ret = ggtt_probe_hw(to_gt(i915)->ggtt, to_gt(i915));
  1279. if (ret)
  1280. return ret;
  1281. if (i915_vtd_active(i915))
  1282. drm_info(&i915->drm, "VT-d active for gfx access\n");
  1283. return 0;
  1284. }
  1285. struct i915_ggtt *i915_ggtt_create(struct drm_i915_private *i915)
  1286. {
  1287. struct i915_ggtt *ggtt;
  1288. ggtt = drmm_kzalloc(&i915->drm, sizeof(*ggtt), GFP_KERNEL);
  1289. if (!ggtt)
  1290. return ERR_PTR(-ENOMEM);
  1291. INIT_LIST_HEAD(&ggtt->gt_list);
  1292. return ggtt;
  1293. }
  1294. int i915_ggtt_enable_hw(struct drm_i915_private *i915)
  1295. {
  1296. if (GRAPHICS_VER(i915) < 6)
  1297. return intel_ggtt_gmch_enable_hw(i915);
  1298. return 0;
  1299. }
  1300. /**
  1301. * i915_ggtt_resume_vm - Restore the memory mappings for a GGTT or DPT VM
  1302. * @vm: The VM to restore the mappings for
  1303. * @all_evicted: Were all VMAs expected to be evicted on suspend?
  1304. *
  1305. * Restore the memory mappings for all objects mapped to HW via the GGTT or a
  1306. * DPT page table.
  1307. *
  1308. * Returns %true if restoring the mapping for any object that was in a write
  1309. * domain before suspend.
  1310. */
  1311. bool i915_ggtt_resume_vm(struct i915_address_space *vm, bool all_evicted)
  1312. {
  1313. struct i915_vma *vma;
  1314. bool write_domain_objs = false;
  1315. drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
  1316. if (all_evicted) {
  1317. drm_WARN_ON(&vm->i915->drm, !list_empty(&vm->bound_list));
  1318. return false;
  1319. }
  1320. /* First fill our portion of the GTT with scratch pages */
  1321. vm->clear_range(vm, 0, vm->total);
  1322. /* clflush objects bound into the GGTT and rebind them. */
  1323. list_for_each_entry(vma, &vm->bound_list, vm_link) {
  1324. struct drm_i915_gem_object *obj = vma->obj;
  1325. unsigned int was_bound =
  1326. atomic_read(&vma->flags) & I915_VMA_BIND_MASK;
  1327. GEM_BUG_ON(!was_bound);
  1328. /*
  1329. * Clear the bound flags of the vma resource to allow
  1330. * ptes to be repopulated.
  1331. */
  1332. vma->resource->bound_flags = 0;
  1333. vma->ops->bind_vma(vm, NULL, vma->resource,
  1334. obj ? obj->pat_index :
  1335. i915_gem_get_pat_index(vm->i915,
  1336. I915_CACHE_NONE),
  1337. was_bound);
  1338. if (obj) { /* only used during resume => exclusive access */
  1339. write_domain_objs |= fetch_and_zero(&obj->write_domain);
  1340. obj->read_domains |= I915_GEM_DOMAIN_GTT;
  1341. }
  1342. }
  1343. return write_domain_objs;
  1344. }
  1345. void i915_ggtt_resume(struct i915_ggtt *ggtt)
  1346. {
  1347. struct intel_gt *gt;
  1348. bool flush;
  1349. list_for_each_entry(gt, &ggtt->gt_list, ggtt_link)
  1350. intel_gt_check_and_clear_faults(gt);
  1351. flush = i915_ggtt_resume_vm(&ggtt->vm, false);
  1352. if (drm_mm_node_allocated(&ggtt->error_capture))
  1353. ggtt->vm.scratch_range(&ggtt->vm, ggtt->error_capture.start,
  1354. ggtt->error_capture.size);
  1355. list_for_each_entry(gt, &ggtt->gt_list, ggtt_link)
  1356. intel_uc_resume_mappings(&gt->uc);
  1357. ggtt->invalidate(ggtt);
  1358. if (flush)
  1359. wbinvd_on_all_cpus();
  1360. intel_ggtt_restore_fences(ggtt);
  1361. }