1 // SPDX-License-Identifier: MIT
3 * Copyright © 2020 Intel Corporation
6 #include <asm/set_memory.h>
8 #include <linux/types.h>
9 #include <linux/stop_machine.h>
11 #include <drm/drm_managed.h>
12 #include <drm/i915_drm.h>
13 #include <drm/intel-gtt.h>
15 #include "display/intel_display.h"
16 #include "gem/i915_gem_lmem.h"
18 #include "intel_context.h"
19 #include "intel_ggtt_gmch.h"
20 #include "intel_gpu_commands.h"
22 #include "intel_gt_regs.h"
23 #include "intel_pci_config.h"
24 #include "intel_ring.h"
28 #include "i915_request.h"
29 #include "i915_scatterlist.h"
30 #include "i915_utils.h"
31 #include "i915_vgpu.h"
33 #include "intel_gtt.h"
34 #include "gen8_ppgtt.h"
35 #include "intel_engine_pm.h"
37 static void i915_ggtt_color_adjust(const struct drm_mm_node *node,
42 if (i915_node_color_differs(node, color))
43 *start += I915_GTT_PAGE_SIZE;
46 * Also leave a space between the unallocated reserved node after the
47 * GTT and any objects within the GTT, i.e. we use the color adjustment
48 * to insert a guard page to prevent prefetches crossing over the
51 node = list_next_entry(node, node_list);
52 if (node->color != color)
53 *end -= I915_GTT_PAGE_SIZE;
56 static int ggtt_init_hw(struct i915_ggtt *ggtt)
58 struct drm_i915_private *i915 = ggtt->vm.i915;
60 i915_address_space_init(&ggtt->vm, VM_CLASS_GGTT);
62 ggtt->vm.is_ggtt = true;
64 /* Only VLV supports read-only GGTT mappings */
65 ggtt->vm.has_read_only = IS_VALLEYVIEW(i915);
67 if (!HAS_LLC(i915) && !HAS_PPGTT(i915))
68 ggtt->vm.mm.color_adjust = i915_ggtt_color_adjust;
70 if (ggtt->mappable_end) {
71 if (!io_mapping_init_wc(&ggtt->iomap,
73 ggtt->mappable_end)) {
74 ggtt->vm.cleanup(&ggtt->vm);
78 ggtt->mtrr = arch_phys_wc_add(ggtt->gmadr.start,
82 intel_ggtt_init_fences(ggtt);
88 * i915_ggtt_init_hw - Initialize GGTT hardware
91 int i915_ggtt_init_hw(struct drm_i915_private *i915)
96 * Note that we use page colouring to enforce a guard page at the
97 * end of the address space. This is required as the CS may prefetch
98 * beyond the end of the batch buffer, across the page boundary,
99 * and beyond the end of the GTT if we do not provide a guard.
101 ret = ggtt_init_hw(to_gt(i915)->ggtt);
109 * i915_ggtt_suspend_vm - Suspend the memory mappings for a GGTT or DPT VM
110 * @vm: The VM to suspend the mappings for
112 * Suspend the memory mappings for all objects mapped to HW via the GGTT or a
115 void i915_ggtt_suspend_vm(struct i915_address_space *vm)
117 struct i915_vma *vma, *vn;
118 int save_skip_rewrite;
120 drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
123 i915_gem_drain_freed_objects(vm->i915);
125 mutex_lock(&vm->mutex);
128 * Skip rewriting PTE on VMA unbind.
129 * FIXME: Use an argument to i915_vma_unbind() instead?
131 save_skip_rewrite = vm->skip_pte_rewrite;
132 vm->skip_pte_rewrite = true;
134 list_for_each_entry_safe(vma, vn, &vm->bound_list, vm_link) {
135 struct drm_i915_gem_object *obj = vma->obj;
137 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
139 if (i915_vma_is_pinned(vma) || !i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
142 /* unlikely to race when GPU is idle, so no worry about slowpath.. */
143 if (WARN_ON(!i915_gem_object_trylock(obj, NULL))) {
145 * No dead objects should appear here, GPU should be
146 * completely idle, and userspace suspended
148 i915_gem_object_get(obj);
150 mutex_unlock(&vm->mutex);
152 i915_gem_object_lock(obj, NULL);
153 GEM_WARN_ON(i915_vma_unbind(vma));
154 i915_gem_object_unlock(obj);
155 i915_gem_object_put(obj);
157 vm->skip_pte_rewrite = save_skip_rewrite;
161 if (!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND)) {
162 i915_vma_wait_for_bind(vma);
164 __i915_vma_evict(vma, false);
165 drm_mm_remove_node(&vma->node);
168 i915_gem_object_unlock(obj);
171 vm->clear_range(vm, 0, vm->total);
173 vm->skip_pte_rewrite = save_skip_rewrite;
175 mutex_unlock(&vm->mutex);
178 void i915_ggtt_suspend(struct i915_ggtt *ggtt)
182 i915_ggtt_suspend_vm(&ggtt->vm);
183 ggtt->invalidate(ggtt);
185 list_for_each_entry(gt, &ggtt->gt_list, ggtt_link)
186 intel_gt_check_and_clear_faults(gt);
189 void gen6_ggtt_invalidate(struct i915_ggtt *ggtt)
191 struct intel_uncore *uncore = ggtt->vm.gt->uncore;
193 spin_lock_irq(&uncore->lock);
194 intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
195 intel_uncore_read_fw(uncore, GFX_FLSH_CNTL_GEN6);
196 spin_unlock_irq(&uncore->lock);
199 static bool needs_wc_ggtt_mapping(struct drm_i915_private *i915)
202 * On BXT+/ICL+ writes larger than 64 bit to the GTT pagetable range
203 * will be dropped. For WC mappings in general we have 64 byte burst
204 * writes when the WC buffer is flushed, so we can't use it, but have to
205 * resort to an uncached mapping. The WC issue is easily caught by the
206 * readback check when writing GTT PTE entries.
208 if (!IS_GEN9_LP(i915) && GRAPHICS_VER(i915) < 11)
214 static void gen8_ggtt_invalidate(struct i915_ggtt *ggtt)
216 struct intel_uncore *uncore = ggtt->vm.gt->uncore;
219 * Note that as an uncached mmio write, this will flush the
220 * WCB of the writes into the GGTT before it triggers the invalidate.
222 * Only perform this when GGTT is mapped as WC, see ggtt_probe_common().
224 if (needs_wc_ggtt_mapping(ggtt->vm.i915))
225 intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6,
229 static void guc_ggtt_ct_invalidate(struct intel_gt *gt)
231 struct intel_uncore *uncore = gt->uncore;
232 intel_wakeref_t wakeref;
234 with_intel_runtime_pm_if_active(uncore->rpm, wakeref)
235 intel_guc_invalidate_tlb_guc(gt_to_guc(gt));
238 static void guc_ggtt_invalidate(struct i915_ggtt *ggtt)
240 struct drm_i915_private *i915 = ggtt->vm.i915;
243 gen8_ggtt_invalidate(ggtt);
245 list_for_each_entry(gt, &ggtt->gt_list, ggtt_link) {
246 if (intel_guc_tlb_invalidation_is_available(gt_to_guc(gt)))
247 guc_ggtt_ct_invalidate(gt);
248 else if (GRAPHICS_VER(i915) >= 12)
249 intel_uncore_write_fw(gt->uncore,
250 GEN12_GUC_TLB_INV_CR,
251 GEN12_GUC_TLB_INV_CR_INVALIDATE);
253 intel_uncore_write_fw(gt->uncore,
254 GEN8_GTCR, GEN8_GTCR_INVALIDATE);
258 static u64 mtl_ggtt_pte_encode(dma_addr_t addr,
259 unsigned int pat_index,
262 gen8_pte_t pte = addr | GEN8_PAGE_PRESENT;
264 WARN_ON_ONCE(addr & ~GEN12_GGTT_PTE_ADDR_MASK);
267 pte |= GEN12_GGTT_PTE_LM;
269 if (pat_index & BIT(0))
270 pte |= MTL_GGTT_PTE_PAT0;
272 if (pat_index & BIT(1))
273 pte |= MTL_GGTT_PTE_PAT1;
278 u64 gen8_ggtt_pte_encode(dma_addr_t addr,
279 unsigned int pat_index,
282 gen8_pte_t pte = addr | GEN8_PAGE_PRESENT;
285 pte |= GEN12_GGTT_PTE_LM;
290 static bool should_update_ggtt_with_bind(struct i915_ggtt *ggtt)
292 struct intel_gt *gt = ggtt->vm.gt;
294 return intel_gt_is_bind_context_ready(gt);
297 static struct intel_context *gen8_ggtt_bind_get_ce(struct i915_ggtt *ggtt, intel_wakeref_t *wakeref)
299 struct intel_context *ce;
300 struct intel_gt *gt = ggtt->vm.gt;
302 if (intel_gt_is_wedged(gt))
305 ce = gt->engine[BCS0]->bind_context;
309 * If the GT is not awake already at this stage then fallback
310 * to pci based GGTT update otherwise __intel_wakeref_get_first()
311 * would conflict with fs_reclaim trying to allocate memory while
312 * doing rpm_resume().
314 *wakeref = intel_gt_pm_get_if_awake(gt);
318 intel_engine_pm_get(ce->engine);
323 static void gen8_ggtt_bind_put_ce(struct intel_context *ce, intel_wakeref_t wakeref)
325 intel_engine_pm_put(ce->engine);
326 intel_gt_pm_put(ce->engine->gt, wakeref);
329 static bool gen8_ggtt_bind_ptes(struct i915_ggtt *ggtt, u32 offset,
330 struct sg_table *pages, u32 num_entries,
331 const gen8_pte_t pte)
333 struct i915_sched_attr attr = {};
334 struct intel_gt *gt = ggtt->vm.gt;
335 const gen8_pte_t scratch_pte = ggtt->vm.scratch[0]->encode;
336 struct sgt_iter iter;
337 struct i915_request *rq;
338 struct intel_context *ce;
339 intel_wakeref_t wakeref;
345 ce = gen8_ggtt_bind_get_ce(ggtt, &wakeref);
350 iter = __sgt_iter(pages->sgl, true);
352 while (num_entries) {
356 * MI_UPDATE_GTT can update 512 entries in a single command but
357 * that end up with engine reset, 511 works.
359 u32 n_ptes = min_t(u32, 511, num_entries);
361 if (mutex_lock_interruptible(&ce->timeline->mutex))
364 intel_context_enter(ce);
365 rq = __i915_request_create(ce, GFP_NOWAIT | GFP_ATOMIC);
366 intel_context_exit(ce);
368 GT_TRACE(gt, "Failed to get bind request\n");
369 mutex_unlock(&ce->timeline->mutex);
373 cs = intel_ring_begin(rq, 2 * n_ptes + 2);
375 GT_TRACE(gt, "Failed to ring space for GGTT bind\n");
376 i915_request_set_error_once(rq, PTR_ERR(cs));
377 /* once a request is created, it must be queued */
381 *cs++ = MI_UPDATE_GTT | (2 * n_ptes);
382 *cs++ = offset << 12;
385 for_each_sgt_daddr_next(addr, iter) {
388 *cs++ = lower_32_bits(pte | addr);
389 *cs++ = upper_32_bits(pte | addr);
392 /* fill remaining with scratch pte, if any */
393 if (count < n_ptes) {
394 memset64((u64 *)cs, scratch_pte,
396 cs += (n_ptes - count) * 2;
399 memset64((u64 *)cs, pte, n_ptes);
403 intel_ring_advance(rq, cs);
405 i915_request_get(rq);
406 __i915_request_commit(rq);
407 __i915_request_queue(rq, &attr);
409 mutex_unlock(&ce->timeline->mutex);
410 /* This will break if the request is complete or after engine reset */
411 i915_request_wait(rq, 0, MAX_SCHEDULE_TIMEOUT);
415 i915_request_put(rq);
417 num_entries -= n_ptes;
421 gen8_ggtt_bind_put_ce(ce, wakeref);
425 i915_request_put(rq);
427 gen8_ggtt_bind_put_ce(ce, wakeref);
431 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
436 static void gen8_ggtt_insert_page(struct i915_address_space *vm,
439 unsigned int pat_index,
442 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
443 gen8_pte_t __iomem *pte =
444 (gen8_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
446 gen8_set_pte(pte, ggtt->vm.pte_encode(addr, pat_index, flags));
448 ggtt->invalidate(ggtt);
451 static void gen8_ggtt_insert_page_bind(struct i915_address_space *vm,
452 dma_addr_t addr, u64 offset,
453 unsigned int pat_index, u32 flags)
455 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
458 pte = ggtt->vm.pte_encode(addr, pat_index, flags);
459 if (should_update_ggtt_with_bind(i915_vm_to_ggtt(vm)) &&
460 gen8_ggtt_bind_ptes(ggtt, offset, NULL, 1, pte))
461 return ggtt->invalidate(ggtt);
463 gen8_ggtt_insert_page(vm, addr, offset, pat_index, flags);
466 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
467 struct i915_vma_resource *vma_res,
468 unsigned int pat_index,
471 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
472 const gen8_pte_t pte_encode = ggtt->vm.pte_encode(0, pat_index, flags);
473 gen8_pte_t __iomem *gte;
474 gen8_pte_t __iomem *end;
475 struct sgt_iter iter;
479 * Note that we ignore PTE_READ_ONLY here. The caller must be careful
480 * not to allow the user to override access to a read only page.
483 gte = (gen8_pte_t __iomem *)ggtt->gsm;
484 gte += (vma_res->start - vma_res->guard) / I915_GTT_PAGE_SIZE;
485 end = gte + vma_res->guard / I915_GTT_PAGE_SIZE;
487 gen8_set_pte(gte++, vm->scratch[0]->encode);
488 end += (vma_res->node_size + vma_res->guard) / I915_GTT_PAGE_SIZE;
490 for_each_sgt_daddr(addr, iter, vma_res->bi.pages)
491 gen8_set_pte(gte++, pte_encode | addr);
492 GEM_BUG_ON(gte > end);
494 /* Fill the allocated but "unused" space beyond the end of the buffer */
496 gen8_set_pte(gte++, vm->scratch[0]->encode);
499 * We want to flush the TLBs only after we're certain all the PTE
500 * updates have finished.
502 ggtt->invalidate(ggtt);
505 static bool __gen8_ggtt_insert_entries_bind(struct i915_address_space *vm,
506 struct i915_vma_resource *vma_res,
507 unsigned int pat_index, u32 flags)
509 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
510 gen8_pte_t scratch_pte = vm->scratch[0]->encode;
511 gen8_pte_t pte_encode;
514 pte_encode = ggtt->vm.pte_encode(0, pat_index, flags);
515 start = (vma_res->start - vma_res->guard) / I915_GTT_PAGE_SIZE;
516 end = start + vma_res->guard / I915_GTT_PAGE_SIZE;
517 if (!gen8_ggtt_bind_ptes(ggtt, start, NULL, end - start, scratch_pte))
521 end += (vma_res->node_size + vma_res->guard) / I915_GTT_PAGE_SIZE;
522 if (!gen8_ggtt_bind_ptes(ggtt, start, vma_res->bi.pages,
523 vma_res->node_size / I915_GTT_PAGE_SIZE, pte_encode))
526 start += vma_res->node_size / I915_GTT_PAGE_SIZE;
527 if (!gen8_ggtt_bind_ptes(ggtt, start, NULL, end - start, scratch_pte))
536 static void gen8_ggtt_insert_entries_bind(struct i915_address_space *vm,
537 struct i915_vma_resource *vma_res,
538 unsigned int pat_index, u32 flags)
540 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
542 if (should_update_ggtt_with_bind(i915_vm_to_ggtt(vm)) &&
543 __gen8_ggtt_insert_entries_bind(vm, vma_res, pat_index, flags))
544 return ggtt->invalidate(ggtt);
546 gen8_ggtt_insert_entries(vm, vma_res, pat_index, flags);
549 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
550 u64 start, u64 length)
552 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
553 unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
554 unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
555 const gen8_pte_t scratch_pte = vm->scratch[0]->encode;
556 gen8_pte_t __iomem *gtt_base =
557 (gen8_pte_t __iomem *)ggtt->gsm + first_entry;
558 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
561 if (WARN(num_entries > max_entries,
562 "First entry = %d; Num entries = %d (max=%d)\n",
563 first_entry, num_entries, max_entries))
564 num_entries = max_entries;
566 for (i = 0; i < num_entries; i++)
567 gen8_set_pte(>t_base[i], scratch_pte);
570 static void gen8_ggtt_scratch_range_bind(struct i915_address_space *vm,
571 u64 start, u64 length)
573 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
574 unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
575 unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
576 const gen8_pte_t scratch_pte = vm->scratch[0]->encode;
577 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
579 if (WARN(num_entries > max_entries,
580 "First entry = %d; Num entries = %d (max=%d)\n",
581 first_entry, num_entries, max_entries))
582 num_entries = max_entries;
584 if (should_update_ggtt_with_bind(ggtt) && gen8_ggtt_bind_ptes(ggtt, first_entry,
585 NULL, num_entries, scratch_pte))
586 return ggtt->invalidate(ggtt);
588 gen8_ggtt_clear_range(vm, start, length);
591 static void gen6_ggtt_insert_page(struct i915_address_space *vm,
594 unsigned int pat_index,
597 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
598 gen6_pte_t __iomem *pte =
599 (gen6_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
601 iowrite32(vm->pte_encode(addr, pat_index, flags), pte);
603 ggtt->invalidate(ggtt);
607 * Binds an object into the global gtt with the specified cache level.
608 * The object will be accessible to the GPU via commands whose operands
609 * reference offsets within the global GTT as well as accessible by the GPU
610 * through the GMADR mapped BAR (i915->mm.gtt->gtt).
612 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
613 struct i915_vma_resource *vma_res,
614 unsigned int pat_index,
617 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
618 gen6_pte_t __iomem *gte;
619 gen6_pte_t __iomem *end;
620 struct sgt_iter iter;
623 gte = (gen6_pte_t __iomem *)ggtt->gsm;
624 gte += (vma_res->start - vma_res->guard) / I915_GTT_PAGE_SIZE;
626 end = gte + vma_res->guard / I915_GTT_PAGE_SIZE;
628 iowrite32(vm->scratch[0]->encode, gte++);
629 end += (vma_res->node_size + vma_res->guard) / I915_GTT_PAGE_SIZE;
630 for_each_sgt_daddr(addr, iter, vma_res->bi.pages)
631 iowrite32(vm->pte_encode(addr, pat_index, flags), gte++);
632 GEM_BUG_ON(gte > end);
634 /* Fill the allocated but "unused" space beyond the end of the buffer */
636 iowrite32(vm->scratch[0]->encode, gte++);
639 * We want to flush the TLBs only after we're certain all the PTE
640 * updates have finished.
642 ggtt->invalidate(ggtt);
645 static void nop_clear_range(struct i915_address_space *vm,
646 u64 start, u64 length)
650 static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
653 * Make sure the internal GAM fifo has been cleared of all GTT
654 * writes before exiting stop_machine(). This guarantees that
655 * any aperture accesses waiting to start in another process
656 * cannot back up behind the GTT writes causing a hang.
657 * The register can be any arbitrary GAM register.
659 intel_uncore_posting_read_fw(vm->gt->uncore, GFX_FLSH_CNTL_GEN6);
663 struct i915_address_space *vm;
666 unsigned int pat_index;
669 static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
671 struct insert_page *arg = _arg;
673 gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset,
675 bxt_vtd_ggtt_wa(arg->vm);
680 static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
683 unsigned int pat_index,
686 struct insert_page arg = { vm, addr, offset, pat_index };
688 stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
691 struct insert_entries {
692 struct i915_address_space *vm;
693 struct i915_vma_resource *vma_res;
694 unsigned int pat_index;
698 static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
700 struct insert_entries *arg = _arg;
702 gen8_ggtt_insert_entries(arg->vm, arg->vma_res,
703 arg->pat_index, arg->flags);
704 bxt_vtd_ggtt_wa(arg->vm);
709 static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
710 struct i915_vma_resource *vma_res,
711 unsigned int pat_index,
714 struct insert_entries arg = { vm, vma_res, pat_index, flags };
716 stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
719 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
720 u64 start, u64 length)
722 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
723 unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
724 unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
725 gen6_pte_t scratch_pte, __iomem *gtt_base =
726 (gen6_pte_t __iomem *)ggtt->gsm + first_entry;
727 const int max_entries = ggtt_total_entries(ggtt) - first_entry;
730 if (WARN(num_entries > max_entries,
731 "First entry = %d; Num entries = %d (max=%d)\n",
732 first_entry, num_entries, max_entries))
733 num_entries = max_entries;
735 scratch_pte = vm->scratch[0]->encode;
736 for (i = 0; i < num_entries; i++)
737 iowrite32(scratch_pte, >t_base[i]);
740 void intel_ggtt_bind_vma(struct i915_address_space *vm,
741 struct i915_vm_pt_stash *stash,
742 struct i915_vma_resource *vma_res,
743 unsigned int pat_index,
748 if (vma_res->bound_flags & (~flags & I915_VMA_BIND_MASK))
751 vma_res->bound_flags |= flags;
753 /* Applicable to VLV (gen8+ do not support RO in the GGTT) */
755 if (vma_res->bi.readonly)
756 pte_flags |= PTE_READ_ONLY;
757 if (vma_res->bi.lmem)
760 vm->insert_entries(vm, vma_res, pat_index, pte_flags);
761 vma_res->page_sizes_gtt = I915_GTT_PAGE_SIZE;
764 void intel_ggtt_unbind_vma(struct i915_address_space *vm,
765 struct i915_vma_resource *vma_res)
767 vm->clear_range(vm, vma_res->start, vma_res->vma_size);
771 * Reserve the top of the GuC address space for firmware images. Addresses
772 * beyond GUC_GGTT_TOP in the GuC address space are inaccessible by GuC,
773 * which makes for a suitable range to hold GuC/HuC firmware images if the
774 * size of the GGTT is 4G. However, on a 32-bit platform the size of the GGTT
775 * is limited to 2G, which is less than GUC_GGTT_TOP, but we reserve a chunk
776 * of the same size anyway, which is far more than needed, to keep the logic
777 * in uc_fw_ggtt_offset() simple.
779 #define GUC_TOP_RESERVE_SIZE (SZ_4G - GUC_GGTT_TOP)
781 static int ggtt_reserve_guc_top(struct i915_ggtt *ggtt)
786 if (!intel_uc_uses_guc(&ggtt->vm.gt->uc))
789 GEM_BUG_ON(ggtt->vm.total <= GUC_TOP_RESERVE_SIZE);
790 offset = ggtt->vm.total - GUC_TOP_RESERVE_SIZE;
792 ret = i915_gem_gtt_reserve(&ggtt->vm, NULL, &ggtt->uc_fw,
793 GUC_TOP_RESERVE_SIZE, offset,
794 I915_COLOR_UNEVICTABLE, PIN_NOEVICT);
796 drm_dbg(&ggtt->vm.i915->drm,
797 "Failed to reserve top of GGTT for GuC\n");
802 static void ggtt_release_guc_top(struct i915_ggtt *ggtt)
804 if (drm_mm_node_allocated(&ggtt->uc_fw))
805 drm_mm_remove_node(&ggtt->uc_fw);
808 static void cleanup_init_ggtt(struct i915_ggtt *ggtt)
810 ggtt_release_guc_top(ggtt);
811 if (drm_mm_node_allocated(&ggtt->error_capture))
812 drm_mm_remove_node(&ggtt->error_capture);
813 mutex_destroy(&ggtt->error_mutex);
816 static int init_ggtt(struct i915_ggtt *ggtt)
819 * Let GEM Manage all of the aperture.
821 * However, leave one page at the end still bound to the scratch page.
822 * There are a number of places where the hardware apparently prefetches
823 * past the end of the object, and we've seen multiple hangs with the
824 * GPU head pointer stuck in a batchbuffer bound at the last page of the
825 * aperture. One page should be enough to keep any prefetching inside
828 unsigned long hole_start, hole_end;
829 struct drm_mm_node *entry;
833 * GuC requires all resources that we're sharing with it to be placed in
834 * non-WOPCM memory. If GuC is not present or not in use we still need a
835 * small bias as ring wraparound at offset 0 sometimes hangs. No idea
838 ggtt->pin_bias = max_t(u32, I915_GTT_PAGE_SIZE,
839 intel_wopcm_guc_size(&ggtt->vm.gt->wopcm));
841 ret = intel_vgt_balloon(ggtt);
845 mutex_init(&ggtt->error_mutex);
846 if (ggtt->mappable_end) {
848 * Reserve a mappable slot for our lockless error capture.
850 * We strongly prefer taking address 0x0 in order to protect
851 * other critical buffers against accidental overwrites,
852 * as writing to address 0 is a very common mistake.
854 * Since 0 may already be in use by the system (e.g. the BIOS
855 * framebuffer), we let the reservation fail quietly and hope
856 * 0 remains reserved always.
858 * If we fail to reserve 0, and then fail to find any space
859 * for an error-capture, remain silent. We can afford not
860 * to reserve an error_capture node as we have fallback
861 * paths, and we trust that 0 will remain reserved. However,
862 * the only likely reason for failure to insert is a driver
863 * bug, which we expect to cause other failures...
865 * Since CPU can perform speculative reads on error capture
866 * (write-combining allows it) add scratch page after error
867 * capture to avoid DMAR errors.
869 ggtt->error_capture.size = 2 * I915_GTT_PAGE_SIZE;
870 ggtt->error_capture.color = I915_COLOR_UNEVICTABLE;
871 if (drm_mm_reserve_node(&ggtt->vm.mm, &ggtt->error_capture))
872 drm_mm_insert_node_in_range(&ggtt->vm.mm,
873 &ggtt->error_capture,
874 ggtt->error_capture.size, 0,
875 ggtt->error_capture.color,
876 0, ggtt->mappable_end,
879 if (drm_mm_node_allocated(&ggtt->error_capture)) {
880 u64 start = ggtt->error_capture.start;
881 u64 size = ggtt->error_capture.size;
883 ggtt->vm.scratch_range(&ggtt->vm, start, size);
884 drm_dbg(&ggtt->vm.i915->drm,
885 "Reserved GGTT:[%llx, %llx] for use by error capture\n",
886 start, start + size);
890 * The upper portion of the GuC address space has a sizeable hole
891 * (several MB) that is inaccessible by GuC. Reserve this range within
892 * GGTT as it can comfortably hold GuC/HuC firmware images.
894 ret = ggtt_reserve_guc_top(ggtt);
898 /* Clear any non-preallocated blocks */
899 drm_mm_for_each_hole(entry, &ggtt->vm.mm, hole_start, hole_end) {
900 drm_dbg(&ggtt->vm.i915->drm,
901 "clearing unused GTT space: [%lx, %lx]\n",
902 hole_start, hole_end);
903 ggtt->vm.clear_range(&ggtt->vm, hole_start,
904 hole_end - hole_start);
907 /* And finally clear the reserved guard page */
908 ggtt->vm.clear_range(&ggtt->vm, ggtt->vm.total - PAGE_SIZE, PAGE_SIZE);
913 cleanup_init_ggtt(ggtt);
917 static void aliasing_gtt_bind_vma(struct i915_address_space *vm,
918 struct i915_vm_pt_stash *stash,
919 struct i915_vma_resource *vma_res,
920 unsigned int pat_index,
925 /* Currently applicable only to VLV */
927 if (vma_res->bi.readonly)
928 pte_flags |= PTE_READ_ONLY;
930 if (flags & I915_VMA_LOCAL_BIND)
931 ppgtt_bind_vma(&i915_vm_to_ggtt(vm)->alias->vm,
932 stash, vma_res, pat_index, flags);
934 if (flags & I915_VMA_GLOBAL_BIND)
935 vm->insert_entries(vm, vma_res, pat_index, pte_flags);
937 vma_res->bound_flags |= flags;
940 static void aliasing_gtt_unbind_vma(struct i915_address_space *vm,
941 struct i915_vma_resource *vma_res)
943 if (vma_res->bound_flags & I915_VMA_GLOBAL_BIND)
944 vm->clear_range(vm, vma_res->start, vma_res->vma_size);
946 if (vma_res->bound_flags & I915_VMA_LOCAL_BIND)
947 ppgtt_unbind_vma(&i915_vm_to_ggtt(vm)->alias->vm, vma_res);
950 static int init_aliasing_ppgtt(struct i915_ggtt *ggtt)
952 struct i915_vm_pt_stash stash = {};
953 struct i915_ppgtt *ppgtt;
956 ppgtt = i915_ppgtt_create(ggtt->vm.gt, 0);
958 return PTR_ERR(ppgtt);
960 if (GEM_WARN_ON(ppgtt->vm.total < ggtt->vm.total)) {
965 err = i915_vm_alloc_pt_stash(&ppgtt->vm, &stash, ggtt->vm.total);
969 i915_gem_object_lock(ppgtt->vm.scratch[0], NULL);
970 err = i915_vm_map_pt_stash(&ppgtt->vm, &stash);
971 i915_gem_object_unlock(ppgtt->vm.scratch[0]);
976 * Note we only pre-allocate as far as the end of the global
977 * GTT. On 48b / 4-level page-tables, the difference is very,
978 * very significant! We have to preallocate as GVT/vgpu does
979 * not like the page directory disappearing.
981 ppgtt->vm.allocate_va_range(&ppgtt->vm, &stash, 0, ggtt->vm.total);
984 ggtt->vm.bind_async_flags |= ppgtt->vm.bind_async_flags;
986 GEM_BUG_ON(ggtt->vm.vma_ops.bind_vma != intel_ggtt_bind_vma);
987 ggtt->vm.vma_ops.bind_vma = aliasing_gtt_bind_vma;
989 GEM_BUG_ON(ggtt->vm.vma_ops.unbind_vma != intel_ggtt_unbind_vma);
990 ggtt->vm.vma_ops.unbind_vma = aliasing_gtt_unbind_vma;
992 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
996 i915_vm_free_pt_stash(&ppgtt->vm, &stash);
998 i915_vm_put(&ppgtt->vm);
1002 static void fini_aliasing_ppgtt(struct i915_ggtt *ggtt)
1004 struct i915_ppgtt *ppgtt;
1006 ppgtt = fetch_and_zero(&ggtt->alias);
1010 i915_vm_put(&ppgtt->vm);
1012 ggtt->vm.vma_ops.bind_vma = intel_ggtt_bind_vma;
1013 ggtt->vm.vma_ops.unbind_vma = intel_ggtt_unbind_vma;
1016 int i915_init_ggtt(struct drm_i915_private *i915)
1020 ret = init_ggtt(to_gt(i915)->ggtt);
1024 if (INTEL_PPGTT(i915) == INTEL_PPGTT_ALIASING) {
1025 ret = init_aliasing_ppgtt(to_gt(i915)->ggtt);
1027 cleanup_init_ggtt(to_gt(i915)->ggtt);
1033 static void ggtt_cleanup_hw(struct i915_ggtt *ggtt)
1035 struct i915_vma *vma, *vn;
1037 flush_workqueue(ggtt->vm.i915->wq);
1038 i915_gem_drain_freed_objects(ggtt->vm.i915);
1040 mutex_lock(&ggtt->vm.mutex);
1042 ggtt->vm.skip_pte_rewrite = true;
1044 list_for_each_entry_safe(vma, vn, &ggtt->vm.bound_list, vm_link) {
1045 struct drm_i915_gem_object *obj = vma->obj;
1048 trylock = i915_gem_object_trylock(obj, NULL);
1051 WARN_ON(__i915_vma_unbind(vma));
1053 i915_gem_object_unlock(obj);
1056 if (drm_mm_node_allocated(&ggtt->error_capture))
1057 drm_mm_remove_node(&ggtt->error_capture);
1058 mutex_destroy(&ggtt->error_mutex);
1060 ggtt_release_guc_top(ggtt);
1061 intel_vgt_deballoon(ggtt);
1063 ggtt->vm.cleanup(&ggtt->vm);
1065 mutex_unlock(&ggtt->vm.mutex);
1066 i915_address_space_fini(&ggtt->vm);
1068 arch_phys_wc_del(ggtt->mtrr);
1070 if (ggtt->iomap.size)
1071 io_mapping_fini(&ggtt->iomap);
1075 * i915_ggtt_driver_release - Clean up GGTT hardware initialization
1076 * @i915: i915 device
1078 void i915_ggtt_driver_release(struct drm_i915_private *i915)
1080 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
1082 fini_aliasing_ppgtt(ggtt);
1084 intel_ggtt_fini_fences(ggtt);
1085 ggtt_cleanup_hw(ggtt);
1089 * i915_ggtt_driver_late_release - Cleanup of GGTT that needs to be done after
1090 * all free objects have been drained.
1091 * @i915: i915 device
1093 void i915_ggtt_driver_late_release(struct drm_i915_private *i915)
1095 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
1097 GEM_WARN_ON(kref_read(&ggtt->vm.resv_ref) != 1);
1098 dma_resv_fini(&ggtt->vm._resv);
1101 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
1103 snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
1104 snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
1105 return snb_gmch_ctl << 20;
1108 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
1110 bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
1111 bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
1113 bdw_gmch_ctl = 1 << bdw_gmch_ctl;
1115 #ifdef CONFIG_X86_32
1116 /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * I915_GTT_PAGE_SIZE */
1117 if (bdw_gmch_ctl > 4)
1121 return bdw_gmch_ctl << 20;
1124 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
1126 gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
1127 gmch_ctrl &= SNB_GMCH_GGMS_MASK;
1130 return 1 << (20 + gmch_ctrl);
1135 static unsigned int gen6_gttmmadr_size(struct drm_i915_private *i915)
1138 * GEN6: GTTMMADR size is 4MB and GTTADR starts at 2MB offset
1139 * GEN8: GTTMMADR size is 16MB and GTTADR starts at 8MB offset
1141 GEM_BUG_ON(GRAPHICS_VER(i915) < 6);
1142 return (GRAPHICS_VER(i915) < 8) ? SZ_4M : SZ_16M;
1145 static unsigned int gen6_gttadr_offset(struct drm_i915_private *i915)
1147 return gen6_gttmmadr_size(i915) / 2;
1150 static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
1152 struct drm_i915_private *i915 = ggtt->vm.i915;
1153 struct intel_uncore *uncore = ggtt->vm.gt->uncore;
1154 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
1155 phys_addr_t phys_addr;
1159 GEM_WARN_ON(pci_resource_len(pdev, GEN4_GTTMMADR_BAR) != gen6_gttmmadr_size(i915));
1161 if (i915_direct_stolen_access(i915)) {
1162 drm_dbg(&i915->drm, "Using direct GSM access\n");
1163 phys_addr = intel_uncore_read64(uncore, GEN6_GSMBASE) & GEN11_BDSM_MASK;
1165 phys_addr = pci_resource_start(pdev, GEN4_GTTMMADR_BAR) + gen6_gttadr_offset(i915);
1168 if (needs_wc_ggtt_mapping(i915))
1169 ggtt->gsm = ioremap_wc(phys_addr, size);
1171 ggtt->gsm = ioremap(phys_addr, size);
1174 drm_err(&i915->drm, "Failed to map the ggtt page table\n");
1178 kref_init(&ggtt->vm.resv_ref);
1179 ret = setup_scratch_page(&ggtt->vm);
1181 drm_err(&i915->drm, "Scratch setup failed\n");
1182 /* iounmap will also get called at remove, but meh */
1188 if (i915_gem_object_is_lmem(ggtt->vm.scratch[0]))
1189 pte_flags |= PTE_LM;
1191 ggtt->vm.scratch[0]->encode =
1192 ggtt->vm.pte_encode(px_dma(ggtt->vm.scratch[0]),
1193 i915_gem_get_pat_index(i915,
1200 static void gen6_gmch_remove(struct i915_address_space *vm)
1202 struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
1208 static struct resource pci_resource(struct pci_dev *pdev, int bar)
1210 return DEFINE_RES_MEM(pci_resource_start(pdev, bar),
1211 pci_resource_len(pdev, bar));
1214 static int gen8_gmch_probe(struct i915_ggtt *ggtt)
1216 struct drm_i915_private *i915 = ggtt->vm.i915;
1217 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
1221 if (!HAS_LMEM(i915) && !HAS_LMEMBAR_SMEM_STOLEN(i915)) {
1222 if (!i915_pci_resource_valid(pdev, GEN4_GMADR_BAR))
1225 ggtt->gmadr = pci_resource(pdev, GEN4_GMADR_BAR);
1226 ggtt->mappable_end = resource_size(&ggtt->gmadr);
1229 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1230 if (IS_CHERRYVIEW(i915))
1231 size = chv_get_total_gtt_size(snb_gmch_ctl);
1233 size = gen8_get_total_gtt_size(snb_gmch_ctl);
1235 ggtt->vm.alloc_pt_dma = alloc_pt_dma;
1236 ggtt->vm.alloc_scratch_dma = alloc_pt_dma;
1237 ggtt->vm.lmem_pt_obj_flags = I915_BO_ALLOC_PM_EARLY;
1239 ggtt->vm.total = (size / sizeof(gen8_pte_t)) * I915_GTT_PAGE_SIZE;
1240 ggtt->vm.cleanup = gen6_gmch_remove;
1241 ggtt->vm.insert_page = gen8_ggtt_insert_page;
1242 ggtt->vm.clear_range = nop_clear_range;
1243 ggtt->vm.scratch_range = gen8_ggtt_clear_range;
1245 ggtt->vm.insert_entries = gen8_ggtt_insert_entries;
1248 * Serialize GTT updates with aperture access on BXT if VT-d is on,
1249 * and always on CHV.
1251 if (intel_vm_no_concurrent_access_wa(i915)) {
1252 ggtt->vm.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
1253 ggtt->vm.insert_page = bxt_vtd_ggtt_insert_page__BKL;
1256 * Calling stop_machine() version of GGTT update function
1257 * at error capture/reset path will raise lockdep warning.
1258 * Allow calling gen8_ggtt_insert_* directly at reset path
1259 * which is safe from parallel GGTT updates.
1261 ggtt->vm.raw_insert_page = gen8_ggtt_insert_page;
1262 ggtt->vm.raw_insert_entries = gen8_ggtt_insert_entries;
1264 ggtt->vm.bind_async_flags =
1265 I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND;
1268 if (i915_ggtt_require_binder(i915)) {
1269 ggtt->vm.scratch_range = gen8_ggtt_scratch_range_bind;
1270 ggtt->vm.insert_page = gen8_ggtt_insert_page_bind;
1271 ggtt->vm.insert_entries = gen8_ggtt_insert_entries_bind;
1273 * On GPU is hung, we might bind VMAs for error capture.
1274 * Fallback to CPU GGTT updates in that case.
1276 ggtt->vm.raw_insert_page = gen8_ggtt_insert_page;
1279 if (intel_uc_wants_guc_submission(&ggtt->vm.gt->uc))
1280 ggtt->invalidate = guc_ggtt_invalidate;
1282 ggtt->invalidate = gen8_ggtt_invalidate;
1284 ggtt->vm.vma_ops.bind_vma = intel_ggtt_bind_vma;
1285 ggtt->vm.vma_ops.unbind_vma = intel_ggtt_unbind_vma;
1287 if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 70))
1288 ggtt->vm.pte_encode = mtl_ggtt_pte_encode;
1290 ggtt->vm.pte_encode = gen8_ggtt_pte_encode;
1292 return ggtt_probe_common(ggtt, size);
1296 * For pre-gen8 platforms pat_index is the same as enum i915_cache_level,
1297 * so the switch-case statements in these PTE encode functions are still valid.
1298 * See translation table LEGACY_CACHELEVEL.
1300 static u64 snb_pte_encode(dma_addr_t addr,
1301 unsigned int pat_index,
1304 gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1306 switch (pat_index) {
1307 case I915_CACHE_L3_LLC:
1308 case I915_CACHE_LLC:
1309 pte |= GEN6_PTE_CACHE_LLC;
1311 case I915_CACHE_NONE:
1312 pte |= GEN6_PTE_UNCACHED;
1315 MISSING_CASE(pat_index);
1321 static u64 ivb_pte_encode(dma_addr_t addr,
1322 unsigned int pat_index,
1325 gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1327 switch (pat_index) {
1328 case I915_CACHE_L3_LLC:
1329 pte |= GEN7_PTE_CACHE_L3_LLC;
1331 case I915_CACHE_LLC:
1332 pte |= GEN6_PTE_CACHE_LLC;
1334 case I915_CACHE_NONE:
1335 pte |= GEN6_PTE_UNCACHED;
1338 MISSING_CASE(pat_index);
1344 static u64 byt_pte_encode(dma_addr_t addr,
1345 unsigned int pat_index,
1348 gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1350 if (!(flags & PTE_READ_ONLY))
1351 pte |= BYT_PTE_WRITEABLE;
1353 if (pat_index != I915_CACHE_NONE)
1354 pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
1359 static u64 hsw_pte_encode(dma_addr_t addr,
1360 unsigned int pat_index,
1363 gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1365 if (pat_index != I915_CACHE_NONE)
1366 pte |= HSW_WB_LLC_AGE3;
1371 static u64 iris_pte_encode(dma_addr_t addr,
1372 unsigned int pat_index,
1375 gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
1377 switch (pat_index) {
1378 case I915_CACHE_NONE:
1381 pte |= HSW_WT_ELLC_LLC_AGE3;
1384 pte |= HSW_WB_ELLC_LLC_AGE3;
1391 static int gen6_gmch_probe(struct i915_ggtt *ggtt)
1393 struct drm_i915_private *i915 = ggtt->vm.i915;
1394 struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
1398 if (!i915_pci_resource_valid(pdev, GEN4_GMADR_BAR))
1401 ggtt->gmadr = pci_resource(pdev, GEN4_GMADR_BAR);
1402 ggtt->mappable_end = resource_size(&ggtt->gmadr);
1405 * 64/512MB is the current min/max we actually know of, but this is
1406 * just a coarse sanity check.
1408 if (ggtt->mappable_end < (64 << 20) ||
1409 ggtt->mappable_end > (512 << 20)) {
1410 drm_err(&i915->drm, "Unknown GMADR size (%pa)\n",
1411 &ggtt->mappable_end);
1415 pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
1417 size = gen6_get_total_gtt_size(snb_gmch_ctl);
1418 ggtt->vm.total = (size / sizeof(gen6_pte_t)) * I915_GTT_PAGE_SIZE;
1420 ggtt->vm.alloc_pt_dma = alloc_pt_dma;
1421 ggtt->vm.alloc_scratch_dma = alloc_pt_dma;
1423 ggtt->vm.clear_range = nop_clear_range;
1424 if (!HAS_FULL_PPGTT(i915))
1425 ggtt->vm.clear_range = gen6_ggtt_clear_range;
1426 ggtt->vm.scratch_range = gen6_ggtt_clear_range;
1427 ggtt->vm.insert_page = gen6_ggtt_insert_page;
1428 ggtt->vm.insert_entries = gen6_ggtt_insert_entries;
1429 ggtt->vm.cleanup = gen6_gmch_remove;
1431 ggtt->invalidate = gen6_ggtt_invalidate;
1433 if (HAS_EDRAM(i915))
1434 ggtt->vm.pte_encode = iris_pte_encode;
1435 else if (IS_HASWELL(i915))
1436 ggtt->vm.pte_encode = hsw_pte_encode;
1437 else if (IS_VALLEYVIEW(i915))
1438 ggtt->vm.pte_encode = byt_pte_encode;
1439 else if (GRAPHICS_VER(i915) >= 7)
1440 ggtt->vm.pte_encode = ivb_pte_encode;
1442 ggtt->vm.pte_encode = snb_pte_encode;
1444 ggtt->vm.vma_ops.bind_vma = intel_ggtt_bind_vma;
1445 ggtt->vm.vma_ops.unbind_vma = intel_ggtt_unbind_vma;
1447 return ggtt_probe_common(ggtt, size);
1450 static int ggtt_probe_hw(struct i915_ggtt *ggtt, struct intel_gt *gt)
1452 struct drm_i915_private *i915 = gt->i915;
1456 ggtt->vm.i915 = i915;
1457 ggtt->vm.dma = i915->drm.dev;
1458 dma_resv_init(&ggtt->vm._resv);
1460 if (GRAPHICS_VER(i915) >= 8)
1461 ret = gen8_gmch_probe(ggtt);
1462 else if (GRAPHICS_VER(i915) >= 6)
1463 ret = gen6_gmch_probe(ggtt);
1465 ret = intel_ggtt_gmch_probe(ggtt);
1468 dma_resv_fini(&ggtt->vm._resv);
1472 if ((ggtt->vm.total - 1) >> 32) {
1474 "We never expected a Global GTT with more than 32bits"
1475 " of address space! Found %lldM!\n",
1476 ggtt->vm.total >> 20);
1477 ggtt->vm.total = 1ULL << 32;
1478 ggtt->mappable_end =
1479 min_t(u64, ggtt->mappable_end, ggtt->vm.total);
1482 if (ggtt->mappable_end > ggtt->vm.total) {
1484 "mappable aperture extends past end of GGTT,"
1485 " aperture=%pa, total=%llx\n",
1486 &ggtt->mappable_end, ggtt->vm.total);
1487 ggtt->mappable_end = ggtt->vm.total;
1490 /* GMADR is the PCI mmio aperture into the global GTT. */
1491 drm_dbg(&i915->drm, "GGTT size = %lluM\n", ggtt->vm.total >> 20);
1492 drm_dbg(&i915->drm, "GMADR size = %lluM\n",
1493 (u64)ggtt->mappable_end >> 20);
1494 drm_dbg(&i915->drm, "DSM size = %lluM\n",
1495 (u64)resource_size(&intel_graphics_stolen_res) >> 20);
1501 * i915_ggtt_probe_hw - Probe GGTT hardware location
1502 * @i915: i915 device
1504 int i915_ggtt_probe_hw(struct drm_i915_private *i915)
1506 struct intel_gt *gt;
1509 for_each_gt(gt, i915, i) {
1510 ret = intel_gt_assign_ggtt(gt);
1515 ret = ggtt_probe_hw(to_gt(i915)->ggtt, to_gt(i915));
1519 if (i915_vtd_active(i915))
1520 drm_info(&i915->drm, "VT-d active for gfx access\n");
1525 struct i915_ggtt *i915_ggtt_create(struct drm_i915_private *i915)
1527 struct i915_ggtt *ggtt;
1529 ggtt = drmm_kzalloc(&i915->drm, sizeof(*ggtt), GFP_KERNEL);
1531 return ERR_PTR(-ENOMEM);
1533 INIT_LIST_HEAD(&ggtt->gt_list);
1538 int i915_ggtt_enable_hw(struct drm_i915_private *i915)
1540 if (GRAPHICS_VER(i915) < 6)
1541 return intel_ggtt_gmch_enable_hw(i915);
1547 * i915_ggtt_resume_vm - Restore the memory mappings for a GGTT or DPT VM
1548 * @vm: The VM to restore the mappings for
1550 * Restore the memory mappings for all objects mapped to HW via the GGTT or a
1553 * Returns %true if restoring the mapping for any object that was in a write
1554 * domain before suspend.
1556 bool i915_ggtt_resume_vm(struct i915_address_space *vm)
1558 struct i915_vma *vma;
1559 bool write_domain_objs = false;
1561 drm_WARN_ON(&vm->i915->drm, !vm->is_ggtt && !vm->is_dpt);
1563 /* First fill our portion of the GTT with scratch pages */
1564 vm->clear_range(vm, 0, vm->total);
1566 /* clflush objects bound into the GGTT and rebind them. */
1567 list_for_each_entry(vma, &vm->bound_list, vm_link) {
1568 struct drm_i915_gem_object *obj = vma->obj;
1569 unsigned int was_bound =
1570 atomic_read(&vma->flags) & I915_VMA_BIND_MASK;
1572 GEM_BUG_ON(!was_bound);
1575 * Clear the bound flags of the vma resource to allow
1576 * ptes to be repopulated.
1578 vma->resource->bound_flags = 0;
1579 vma->ops->bind_vma(vm, NULL, vma->resource,
1580 obj ? obj->pat_index :
1581 i915_gem_get_pat_index(vm->i915,
1585 if (obj) { /* only used during resume => exclusive access */
1586 write_domain_objs |= fetch_and_zero(&obj->write_domain);
1587 obj->read_domains |= I915_GEM_DOMAIN_GTT;
1591 return write_domain_objs;
1594 void i915_ggtt_resume(struct i915_ggtt *ggtt)
1596 struct intel_gt *gt;
1599 list_for_each_entry(gt, &ggtt->gt_list, ggtt_link)
1600 intel_gt_check_and_clear_faults(gt);
1602 flush = i915_ggtt_resume_vm(&ggtt->vm);
1604 if (drm_mm_node_allocated(&ggtt->error_capture))
1605 ggtt->vm.scratch_range(&ggtt->vm, ggtt->error_capture.start,
1606 ggtt->error_capture.size);
1608 list_for_each_entry(gt, &ggtt->gt_list, ggtt_link)
1609 intel_uc_resume_mappings(>->uc);
1611 ggtt->invalidate(ggtt);
1614 wbinvd_on_all_cpus();
1616 intel_ggtt_restore_fences(ggtt);