1 // SPDX-License-Identifier: MIT
3 * Copyright © 2020 Intel Corporation
6 #include <linux/slab.h> /* fault-inject.h is not standalone! */
8 #include <linux/fault-inject.h>
9 #include <linux/sched/mm.h>
11 #include <drm/drm_cache.h>
13 #include "gem/i915_gem_internal.h"
14 #include "gem/i915_gem_lmem.h"
16 #include "i915_trace.h"
17 #include "i915_utils.h"
19 #include "intel_gt_mcr.h"
20 #include "intel_gt_regs.h"
21 #include "intel_gtt.h"
24 static bool intel_ggtt_update_needs_vtd_wa(struct drm_i915_private *i915)
26 return IS_BROXTON(i915) && i915_vtd_active(i915);
29 bool intel_vm_no_concurrent_access_wa(struct drm_i915_private *i915)
31 return IS_CHERRYVIEW(i915) || intel_ggtt_update_needs_vtd_wa(i915);
34 struct drm_i915_gem_object *alloc_pt_lmem(struct i915_address_space *vm, int sz)
36 struct drm_i915_gem_object *obj;
39 * To avoid severe over-allocation when dealing with min_page_size
40 * restrictions, we override that behaviour here by allowing an object
41 * size and page layout which can be smaller. In practice this should be
42 * totally fine, since GTT paging structures are not typically inserted
45 * Note that we also hit this path for the scratch page, and for this
46 * case it might need to be 64K, but that should work fine here since we
47 * used the passed in size for the page size, which should ensure it
48 * also has the same alignment.
50 obj = __i915_gem_object_create_lmem_with_ps(vm->i915, sz, sz,
51 vm->lmem_pt_obj_flags);
53 * Ensure all paging structures for this vm share the same dma-resv
54 * object underneath, with the idea that one object_lock() will lock
58 obj->base.resv = i915_vm_resv_get(vm);
59 obj->shares_resv_from = vm;
65 struct drm_i915_gem_object *alloc_pt_dma(struct i915_address_space *vm, int sz)
67 struct drm_i915_gem_object *obj;
69 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
70 i915_gem_shrink_all(vm->i915);
72 obj = i915_gem_object_create_internal(vm->i915, sz);
74 * Ensure all paging structures for this vm share the same dma-resv
75 * object underneath, with the idea that one object_lock() will lock
79 obj->base.resv = i915_vm_resv_get(vm);
80 obj->shares_resv_from = vm;
86 int map_pt_dma(struct i915_address_space *vm, struct drm_i915_gem_object *obj)
88 enum i915_map_type type;
91 type = i915_coherent_map_type(vm->i915, obj, true);
92 vaddr = i915_gem_object_pin_map_unlocked(obj, type);
94 return PTR_ERR(vaddr);
96 i915_gem_object_make_unshrinkable(obj);
100 int map_pt_dma_locked(struct i915_address_space *vm, struct drm_i915_gem_object *obj)
102 enum i915_map_type type;
105 type = i915_coherent_map_type(vm->i915, obj, true);
106 vaddr = i915_gem_object_pin_map(obj, type);
108 return PTR_ERR(vaddr);
110 i915_gem_object_make_unshrinkable(obj);
114 static void clear_vm_list(struct list_head *list)
116 struct i915_vma *vma, *vn;
118 list_for_each_entry_safe(vma, vn, list, vm_link) {
119 struct drm_i915_gem_object *obj = vma->obj;
121 if (!i915_gem_object_get_rcu(obj)) {
123 * Object is dying, but has not yet cleared its
125 * Unbind the dying vma to ensure our list
126 * is completely drained. We leave the destruction to
127 * the object destructor to avoid the vma
128 * disappearing under it.
130 atomic_and(~I915_VMA_PIN_MASK, &vma->flags);
131 WARN_ON(__i915_vma_unbind(vma));
133 /* Remove from the unbound list */
134 list_del_init(&vma->vm_link);
137 * Delay the vm and vm mutex freeing until the
138 * object is done with destruction.
140 i915_vm_resv_get(vma->vm);
141 vma->vm_ddestroy = true;
143 i915_vma_destroy_locked(vma);
144 i915_gem_object_put(obj);
150 static void __i915_vm_close(struct i915_address_space *vm)
152 mutex_lock(&vm->mutex);
154 clear_vm_list(&vm->bound_list);
155 clear_vm_list(&vm->unbound_list);
157 /* Check for must-fix unanticipated side-effects */
158 GEM_BUG_ON(!list_empty(&vm->bound_list));
159 GEM_BUG_ON(!list_empty(&vm->unbound_list));
161 mutex_unlock(&vm->mutex);
164 /* lock the vm into the current ww, if we lock one, we lock all */
165 int i915_vm_lock_objects(struct i915_address_space *vm,
166 struct i915_gem_ww_ctx *ww)
168 if (vm->scratch[0]->base.resv == &vm->_resv) {
169 return i915_gem_object_lock(vm->scratch[0], ww);
171 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
173 /* We borrowed the scratch page from ggtt, take the top level object */
174 return i915_gem_object_lock(ppgtt->pd->pt.base, ww);
178 void i915_address_space_fini(struct i915_address_space *vm)
180 drm_mm_takedown(&vm->mm);
184 * i915_vm_resv_release - Final struct i915_address_space destructor
185 * @kref: Pointer to the &i915_address_space.resv_ref member.
187 * This function is called when the last lock sharer no longer shares the
188 * &i915_address_space._resv lock, and also if we raced when
189 * destroying a vma by the vma destruction
191 void i915_vm_resv_release(struct kref *kref)
193 struct i915_address_space *vm =
194 container_of(kref, typeof(*vm), resv_ref);
196 dma_resv_fini(&vm->_resv);
197 mutex_destroy(&vm->mutex);
202 static void __i915_vm_release(struct work_struct *work)
204 struct i915_address_space *vm =
205 container_of(work, struct i915_address_space, release_work);
209 /* Synchronize async unbinds. */
210 i915_vma_resource_bind_dep_sync_all(vm);
213 i915_address_space_fini(vm);
215 i915_vm_resv_put(vm);
218 void i915_vm_release(struct kref *kref)
220 struct i915_address_space *vm =
221 container_of(kref, struct i915_address_space, ref);
223 GEM_BUG_ON(i915_is_ggtt(vm));
224 trace_i915_ppgtt_release(vm);
226 queue_work(vm->i915->wq, &vm->release_work);
229 void i915_address_space_init(struct i915_address_space *vm, int subclass)
234 * Special case for GGTT that has already done an early
237 if (!kref_read(&vm->resv_ref))
238 kref_init(&vm->resv_ref);
240 vm->pending_unbind = RB_ROOT_CACHED;
241 INIT_WORK(&vm->release_work, __i915_vm_release);
244 * The vm->mutex must be reclaim safe (for use in the shrinker).
245 * Do a dummy acquire now under fs_reclaim so that any allocation
246 * attempt holding the lock is immediately reported by lockdep.
248 mutex_init(&vm->mutex);
249 lockdep_set_subclass(&vm->mutex, subclass);
251 if (!intel_vm_no_concurrent_access_wa(vm->i915)) {
252 i915_gem_shrinker_taints_mutex(vm->i915, &vm->mutex);
255 * CHV + BXT VTD workaround use stop_machine(),
256 * which is allowed to allocate memory. This means &vm->mutex
257 * is the outer lock, and in theory we can allocate memory inside
258 * it through stop_machine().
260 * Add the annotation for this, we use trylock in shrinker.
262 mutex_acquire(&vm->mutex.dep_map, 0, 0, _THIS_IP_);
263 might_alloc(GFP_KERNEL);
264 mutex_release(&vm->mutex.dep_map, _THIS_IP_);
266 dma_resv_init(&vm->_resv);
268 GEM_BUG_ON(!vm->total);
269 drm_mm_init(&vm->mm, 0, vm->total);
271 memset64(vm->min_alignment, I915_GTT_MIN_ALIGNMENT,
272 ARRAY_SIZE(vm->min_alignment));
274 if (HAS_64K_PAGES(vm->i915)) {
275 vm->min_alignment[INTEL_MEMORY_LOCAL] = I915_GTT_PAGE_SIZE_64K;
276 vm->min_alignment[INTEL_MEMORY_STOLEN_LOCAL] = I915_GTT_PAGE_SIZE_64K;
279 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
281 INIT_LIST_HEAD(&vm->bound_list);
282 INIT_LIST_HEAD(&vm->unbound_list);
285 void *__px_vaddr(struct drm_i915_gem_object *p)
287 enum i915_map_type type;
289 GEM_BUG_ON(!i915_gem_object_has_pages(p));
290 return page_unpack_bits(p->mm.mapping, &type);
293 dma_addr_t __px_dma(struct drm_i915_gem_object *p)
295 GEM_BUG_ON(!i915_gem_object_has_pages(p));
296 return sg_dma_address(p->mm.pages->sgl);
299 struct page *__px_page(struct drm_i915_gem_object *p)
301 GEM_BUG_ON(!i915_gem_object_has_pages(p));
302 return sg_page(p->mm.pages->sgl);
306 fill_page_dma(struct drm_i915_gem_object *p, const u64 val, unsigned int count)
308 void *vaddr = __px_vaddr(p);
310 memset64(vaddr, val, count);
311 drm_clflush_virt_range(vaddr, PAGE_SIZE);
314 static void poison_scratch_page(struct drm_i915_gem_object *scratch)
316 void *vaddr = __px_vaddr(scratch);
320 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
323 memset(vaddr, val, scratch->base.size);
324 drm_clflush_virt_range(vaddr, scratch->base.size);
327 int setup_scratch_page(struct i915_address_space *vm)
332 * In order to utilize 64K pages for an object with a size < 2M, we will
333 * need to support a 64K scratch page, given that every 16th entry for a
334 * page-table operating in 64K mode must point to a properly aligned 64K
335 * region, including any PTEs which happen to point to scratch.
337 * This is only relevant for the 48b PPGTT where we support
338 * huge-gtt-pages, see also i915_vma_insert(). However, as we share the
339 * scratch (read-only) between all vm, we create one 64k scratch page
342 size = I915_GTT_PAGE_SIZE_4K;
343 if (i915_vm_is_4lvl(vm) &&
344 HAS_PAGE_SIZES(vm->i915, I915_GTT_PAGE_SIZE_64K) &&
345 !HAS_64K_PAGES(vm->i915))
346 size = I915_GTT_PAGE_SIZE_64K;
349 struct drm_i915_gem_object *obj;
351 obj = vm->alloc_scratch_dma(vm, size);
355 if (map_pt_dma(vm, obj))
358 /* We need a single contiguous page for our scratch */
359 if (obj->mm.page_sizes.sg < size)
362 /* And it needs to be correspondingly aligned */
363 if (__px_dma(obj) & (size - 1))
367 * Use a non-zero scratch page for debugging.
369 * We want a value that should be reasonably obvious
370 * to spot in the error state, while also causing a GPU hang
371 * if executed. We prefer using a clear page in production, so
372 * should it ever be accidentally used, the effect should be
375 poison_scratch_page(obj);
377 vm->scratch[0] = obj;
378 vm->scratch_order = get_order(size);
382 i915_gem_object_put(obj);
384 if (size == I915_GTT_PAGE_SIZE_4K)
387 size = I915_GTT_PAGE_SIZE_4K;
391 void free_scratch(struct i915_address_space *vm)
398 for (i = 0; i <= vm->top; i++)
399 i915_gem_object_put(vm->scratch[i]);
402 void gtt_write_workarounds(struct intel_gt *gt)
404 struct drm_i915_private *i915 = gt->i915;
405 struct intel_uncore *uncore = gt->uncore;
408 * This function is for gtt related workarounds. This function is
409 * called on driver load and after a GPU reset, so you can place
410 * workarounds here even if they get overwritten by GPU reset.
412 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk,cfl,cnl,icl */
413 if (IS_BROADWELL(i915))
414 intel_uncore_write(uncore,
416 GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
417 else if (IS_CHERRYVIEW(i915))
418 intel_uncore_write(uncore,
420 GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
421 else if (IS_GEN9_LP(i915))
422 intel_uncore_write(uncore,
424 GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
425 else if (GRAPHICS_VER(i915) >= 9 && GRAPHICS_VER(i915) <= 11)
426 intel_uncore_write(uncore,
428 GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
431 * To support 64K PTEs we need to first enable the use of the
432 * Intermediate-Page-Size(IPS) bit of the PDE field via some magical
433 * mmio, otherwise the page-walker will simply ignore the IPS bit. This
434 * shouldn't be needed after GEN10.
436 * 64K pages were first introduced from BDW+, although technically they
437 * only *work* from gen9+. For pre-BDW we instead have the option for
438 * 32K pages, but we don't currently have any support for it in our
441 if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_64K) &&
442 GRAPHICS_VER(i915) <= 10)
443 intel_uncore_rmw(uncore,
444 GEN8_GAMW_ECO_DEV_RW_IA,
446 GAMW_ECO_ENABLE_64K_IPS_FIELD);
448 if (IS_GRAPHICS_VER(i915, 8, 11)) {
449 bool can_use_gtt_cache = true;
452 * According to the BSpec if we use 2M/1G pages then we also
453 * need to disable the GTT cache. At least on BDW we can see
454 * visual corruption when using 2M pages, and not disabling the
457 if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_2M))
458 can_use_gtt_cache = false;
460 /* WaGttCachingOffByDefault */
461 intel_uncore_write(uncore,
463 can_use_gtt_cache ? GTT_CACHE_EN_ALL : 0);
464 drm_WARN_ON_ONCE(&i915->drm, can_use_gtt_cache &&
465 intel_uncore_read(uncore,
466 HSW_GTT_CACHE_EN) == 0);
470 static void tgl_setup_private_ppat(struct intel_uncore *uncore)
472 /* TGL doesn't support LLC or AGE settings */
473 intel_uncore_write(uncore, GEN12_PAT_INDEX(0), GEN8_PPAT_WB);
474 intel_uncore_write(uncore, GEN12_PAT_INDEX(1), GEN8_PPAT_WC);
475 intel_uncore_write(uncore, GEN12_PAT_INDEX(2), GEN8_PPAT_WT);
476 intel_uncore_write(uncore, GEN12_PAT_INDEX(3), GEN8_PPAT_UC);
477 intel_uncore_write(uncore, GEN12_PAT_INDEX(4), GEN8_PPAT_WB);
478 intel_uncore_write(uncore, GEN12_PAT_INDEX(5), GEN8_PPAT_WB);
479 intel_uncore_write(uncore, GEN12_PAT_INDEX(6), GEN8_PPAT_WB);
480 intel_uncore_write(uncore, GEN12_PAT_INDEX(7), GEN8_PPAT_WB);
483 static void xehp_setup_private_ppat(struct intel_gt *gt)
485 enum forcewake_domains fw;
488 fw = intel_uncore_forcewake_for_reg(gt->uncore, _MMIO(XEHP_PAT_INDEX(0).reg),
490 intel_uncore_forcewake_get(gt->uncore, fw);
492 intel_gt_mcr_lock(gt, &flags);
493 intel_gt_mcr_multicast_write_fw(gt, XEHP_PAT_INDEX(0), GEN8_PPAT_WB);
494 intel_gt_mcr_multicast_write_fw(gt, XEHP_PAT_INDEX(1), GEN8_PPAT_WC);
495 intel_gt_mcr_multicast_write_fw(gt, XEHP_PAT_INDEX(2), GEN8_PPAT_WT);
496 intel_gt_mcr_multicast_write_fw(gt, XEHP_PAT_INDEX(3), GEN8_PPAT_UC);
497 intel_gt_mcr_multicast_write_fw(gt, XEHP_PAT_INDEX(4), GEN8_PPAT_WB);
498 intel_gt_mcr_multicast_write_fw(gt, XEHP_PAT_INDEX(5), GEN8_PPAT_WB);
499 intel_gt_mcr_multicast_write_fw(gt, XEHP_PAT_INDEX(6), GEN8_PPAT_WB);
500 intel_gt_mcr_multicast_write_fw(gt, XEHP_PAT_INDEX(7), GEN8_PPAT_WB);
501 intel_gt_mcr_unlock(gt, flags);
503 intel_uncore_forcewake_put(gt->uncore, fw);
506 static void icl_setup_private_ppat(struct intel_uncore *uncore)
508 intel_uncore_write(uncore,
510 GEN8_PPAT_WB | GEN8_PPAT_LLC);
511 intel_uncore_write(uncore,
513 GEN8_PPAT_WC | GEN8_PPAT_LLCELLC);
514 intel_uncore_write(uncore,
516 GEN8_PPAT_WB | GEN8_PPAT_ELLC_OVERRIDE);
517 intel_uncore_write(uncore,
520 intel_uncore_write(uncore,
522 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0));
523 intel_uncore_write(uncore,
525 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1));
526 intel_uncore_write(uncore,
528 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2));
529 intel_uncore_write(uncore,
531 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
535 * The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
536 * bits. When using advanced contexts each context stores its own PAT, but
537 * writing this data shouldn't be harmful even in those cases.
539 static void bdw_setup_private_ppat(struct intel_uncore *uncore)
541 struct drm_i915_private *i915 = uncore->i915;
544 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
545 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
546 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
547 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
548 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
549 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
550 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
552 /* for scanout with eLLC */
553 if (GRAPHICS_VER(i915) >= 9)
554 pat |= GEN8_PPAT(2, GEN8_PPAT_WB | GEN8_PPAT_ELLC_OVERRIDE);
556 pat |= GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC);
558 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
559 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
562 static void chv_setup_private_ppat(struct intel_uncore *uncore)
567 * Map WB on BDW to snooped on CHV.
569 * Only the snoop bit has meaning for CHV, the rest is
572 * The hardware will never snoop for certain types of accesses:
573 * - CPU GTT (GMADR->GGTT->no snoop->memory)
574 * - PPGTT page tables
575 * - some other special cycles
577 * As with BDW, we also need to consider the following for GT accesses:
578 * "For GGTT, there is NO pat_sel[2:0] from the entry,
579 * so RTL will always use the value corresponding to
581 * Which means we must set the snoop bit in PAT entry 0
582 * in order to keep the global status page working.
585 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
589 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
590 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
591 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
592 GEN8_PPAT(7, CHV_PPAT_SNOOP);
594 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
595 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
598 void setup_private_pat(struct intel_gt *gt)
600 struct intel_uncore *uncore = gt->uncore;
601 struct drm_i915_private *i915 = gt->i915;
603 GEM_BUG_ON(GRAPHICS_VER(i915) < 8);
605 if (GRAPHICS_VER_FULL(i915) >= IP_VER(12, 50))
606 xehp_setup_private_ppat(gt);
607 else if (GRAPHICS_VER(i915) >= 12)
608 tgl_setup_private_ppat(uncore);
609 else if (GRAPHICS_VER(i915) >= 11)
610 icl_setup_private_ppat(uncore);
611 else if (IS_CHERRYVIEW(i915) || IS_GEN9_LP(i915))
612 chv_setup_private_ppat(uncore);
614 bdw_setup_private_ppat(uncore);
618 __vm_create_scratch_for_read(struct i915_address_space *vm, unsigned long size)
620 struct drm_i915_gem_object *obj;
621 struct i915_vma *vma;
623 obj = i915_gem_object_create_internal(vm->i915, PAGE_ALIGN(size));
625 return ERR_CAST(obj);
627 i915_gem_object_set_cache_coherency(obj, I915_CACHING_CACHED);
629 vma = i915_vma_instance(obj, vm, NULL);
631 i915_gem_object_put(obj);
639 __vm_create_scratch_for_read_pinned(struct i915_address_space *vm, unsigned long size)
641 struct i915_vma *vma;
644 vma = __vm_create_scratch_for_read(vm, size);
648 err = i915_vma_pin(vma, 0, 0,
649 i915_vma_is_ggtt(vma) ? PIN_GLOBAL : PIN_USER);
658 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
659 #include "selftests/mock_gtt.c"