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"
15 #include "i915_trace.h"
16 #include "i915_utils.h"
18 #include "intel_gt_regs.h"
19 #include "intel_gtt.h"
22 static bool intel_ggtt_update_needs_vtd_wa(struct drm_i915_private *i915)
24 return IS_BROXTON(i915) && i915_vtd_active(i915);
27 bool intel_vm_no_concurrent_access_wa(struct drm_i915_private *i915)
29 return IS_CHERRYVIEW(i915) || intel_ggtt_update_needs_vtd_wa(i915);
32 struct drm_i915_gem_object *alloc_pt_lmem(struct i915_address_space *vm, int sz)
34 struct drm_i915_gem_object *obj;
37 * To avoid severe over-allocation when dealing with min_page_size
38 * restrictions, we override that behaviour here by allowing an object
39 * size and page layout which can be smaller. In practice this should be
40 * totally fine, since GTT paging structures are not typically inserted
43 * Note that we also hit this path for the scratch page, and for this
44 * case it might need to be 64K, but that should work fine here since we
45 * used the passed in size for the page size, which should ensure it
46 * also has the same alignment.
48 obj = __i915_gem_object_create_lmem_with_ps(vm->i915, sz, sz,
49 vm->lmem_pt_obj_flags);
51 * Ensure all paging structures for this vm share the same dma-resv
52 * object underneath, with the idea that one object_lock() will lock
56 obj->base.resv = i915_vm_resv_get(vm);
57 obj->shares_resv_from = vm;
63 struct drm_i915_gem_object *alloc_pt_dma(struct i915_address_space *vm, int sz)
65 struct drm_i915_gem_object *obj;
67 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
68 i915_gem_shrink_all(vm->i915);
70 obj = i915_gem_object_create_internal(vm->i915, sz);
72 * Ensure all paging structures for this vm share the same dma-resv
73 * object underneath, with the idea that one object_lock() will lock
77 obj->base.resv = i915_vm_resv_get(vm);
78 obj->shares_resv_from = vm;
84 int map_pt_dma(struct i915_address_space *vm, struct drm_i915_gem_object *obj)
86 enum i915_map_type type;
89 type = i915_coherent_map_type(vm->i915, obj, true);
90 vaddr = i915_gem_object_pin_map_unlocked(obj, type);
92 return PTR_ERR(vaddr);
94 i915_gem_object_make_unshrinkable(obj);
98 int map_pt_dma_locked(struct i915_address_space *vm, struct drm_i915_gem_object *obj)
100 enum i915_map_type type;
103 type = i915_coherent_map_type(vm->i915, obj, true);
104 vaddr = i915_gem_object_pin_map(obj, type);
106 return PTR_ERR(vaddr);
108 i915_gem_object_make_unshrinkable(obj);
112 static void clear_vm_list(struct list_head *list)
114 struct i915_vma *vma, *vn;
116 list_for_each_entry_safe(vma, vn, list, vm_link) {
117 struct drm_i915_gem_object *obj = vma->obj;
119 if (!i915_gem_object_get_rcu(obj)) {
121 * Object is dying, but has not yet cleared its
123 * Unbind the dying vma to ensure our list
124 * is completely drained. We leave the destruction to
125 * the object destructor to avoid the vma
126 * disappearing under it.
128 atomic_and(~I915_VMA_PIN_MASK, &vma->flags);
129 WARN_ON(__i915_vma_unbind(vma));
131 /* Remove from the unbound list */
132 list_del_init(&vma->vm_link);
135 * Delay the vm and vm mutex freeing until the
136 * object is done with destruction.
138 i915_vm_resv_get(vma->vm);
139 vma->vm_ddestroy = true;
141 i915_vma_destroy_locked(vma);
142 i915_gem_object_put(obj);
148 static void __i915_vm_close(struct i915_address_space *vm)
150 mutex_lock(&vm->mutex);
152 clear_vm_list(&vm->bound_list);
153 clear_vm_list(&vm->unbound_list);
155 /* Check for must-fix unanticipated side-effects */
156 GEM_BUG_ON(!list_empty(&vm->bound_list));
157 GEM_BUG_ON(!list_empty(&vm->unbound_list));
159 mutex_unlock(&vm->mutex);
162 /* lock the vm into the current ww, if we lock one, we lock all */
163 int i915_vm_lock_objects(struct i915_address_space *vm,
164 struct i915_gem_ww_ctx *ww)
166 if (vm->scratch[0]->base.resv == &vm->_resv) {
167 return i915_gem_object_lock(vm->scratch[0], ww);
169 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
171 /* We borrowed the scratch page from ggtt, take the top level object */
172 return i915_gem_object_lock(ppgtt->pd->pt.base, ww);
176 void i915_address_space_fini(struct i915_address_space *vm)
178 drm_mm_takedown(&vm->mm);
182 * i915_vm_resv_release - Final struct i915_address_space destructor
183 * @kref: Pointer to the &i915_address_space.resv_ref member.
185 * This function is called when the last lock sharer no longer shares the
186 * &i915_address_space._resv lock, and also if we raced when
187 * destroying a vma by the vma destruction
189 void i915_vm_resv_release(struct kref *kref)
191 struct i915_address_space *vm =
192 container_of(kref, typeof(*vm), resv_ref);
194 dma_resv_fini(&vm->_resv);
195 mutex_destroy(&vm->mutex);
200 static void __i915_vm_release(struct work_struct *work)
202 struct i915_address_space *vm =
203 container_of(work, struct i915_address_space, release_work);
207 /* Synchronize async unbinds. */
208 i915_vma_resource_bind_dep_sync_all(vm);
211 i915_address_space_fini(vm);
213 i915_vm_resv_put(vm);
216 void i915_vm_release(struct kref *kref)
218 struct i915_address_space *vm =
219 container_of(kref, struct i915_address_space, ref);
221 GEM_BUG_ON(i915_is_ggtt(vm));
222 trace_i915_ppgtt_release(vm);
224 queue_work(vm->i915->wq, &vm->release_work);
227 void i915_address_space_init(struct i915_address_space *vm, int subclass)
232 * Special case for GGTT that has already done an early
235 if (!kref_read(&vm->resv_ref))
236 kref_init(&vm->resv_ref);
238 vm->pending_unbind = RB_ROOT_CACHED;
239 INIT_WORK(&vm->release_work, __i915_vm_release);
242 * The vm->mutex must be reclaim safe (for use in the shrinker).
243 * Do a dummy acquire now under fs_reclaim so that any allocation
244 * attempt holding the lock is immediately reported by lockdep.
246 mutex_init(&vm->mutex);
247 lockdep_set_subclass(&vm->mutex, subclass);
249 if (!intel_vm_no_concurrent_access_wa(vm->i915)) {
250 i915_gem_shrinker_taints_mutex(vm->i915, &vm->mutex);
253 * CHV + BXT VTD workaround use stop_machine(),
254 * which is allowed to allocate memory. This means &vm->mutex
255 * is the outer lock, and in theory we can allocate memory inside
256 * it through stop_machine().
258 * Add the annotation for this, we use trylock in shrinker.
260 mutex_acquire(&vm->mutex.dep_map, 0, 0, _THIS_IP_);
261 might_alloc(GFP_KERNEL);
262 mutex_release(&vm->mutex.dep_map, _THIS_IP_);
264 dma_resv_init(&vm->_resv);
266 GEM_BUG_ON(!vm->total);
267 drm_mm_init(&vm->mm, 0, vm->total);
269 memset64(vm->min_alignment, I915_GTT_MIN_ALIGNMENT,
270 ARRAY_SIZE(vm->min_alignment));
272 if (HAS_64K_PAGES(vm->i915) && NEEDS_COMPACT_PT(vm->i915) &&
273 subclass == VM_CLASS_PPGTT) {
274 vm->min_alignment[INTEL_MEMORY_LOCAL] = I915_GTT_PAGE_SIZE_2M;
275 vm->min_alignment[INTEL_MEMORY_STOLEN_LOCAL] = I915_GTT_PAGE_SIZE_2M;
276 } else if (HAS_64K_PAGES(vm->i915)) {
277 vm->min_alignment[INTEL_MEMORY_LOCAL] = I915_GTT_PAGE_SIZE_64K;
278 vm->min_alignment[INTEL_MEMORY_STOLEN_LOCAL] = I915_GTT_PAGE_SIZE_64K;
281 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
283 INIT_LIST_HEAD(&vm->bound_list);
284 INIT_LIST_HEAD(&vm->unbound_list);
287 void *__px_vaddr(struct drm_i915_gem_object *p)
289 enum i915_map_type type;
291 GEM_BUG_ON(!i915_gem_object_has_pages(p));
292 return page_unpack_bits(p->mm.mapping, &type);
295 dma_addr_t __px_dma(struct drm_i915_gem_object *p)
297 GEM_BUG_ON(!i915_gem_object_has_pages(p));
298 return sg_dma_address(p->mm.pages->sgl);
301 struct page *__px_page(struct drm_i915_gem_object *p)
303 GEM_BUG_ON(!i915_gem_object_has_pages(p));
304 return sg_page(p->mm.pages->sgl);
308 fill_page_dma(struct drm_i915_gem_object *p, const u64 val, unsigned int count)
310 void *vaddr = __px_vaddr(p);
312 memset64(vaddr, val, count);
313 drm_clflush_virt_range(vaddr, PAGE_SIZE);
316 static void poison_scratch_page(struct drm_i915_gem_object *scratch)
318 void *vaddr = __px_vaddr(scratch);
322 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
325 memset(vaddr, val, scratch->base.size);
326 drm_clflush_virt_range(vaddr, scratch->base.size);
329 int setup_scratch_page(struct i915_address_space *vm)
334 * In order to utilize 64K pages for an object with a size < 2M, we will
335 * need to support a 64K scratch page, given that every 16th entry for a
336 * page-table operating in 64K mode must point to a properly aligned 64K
337 * region, including any PTEs which happen to point to scratch.
339 * This is only relevant for the 48b PPGTT where we support
340 * huge-gtt-pages, see also i915_vma_insert(). However, as we share the
341 * scratch (read-only) between all vm, we create one 64k scratch page
344 size = I915_GTT_PAGE_SIZE_4K;
345 if (i915_vm_is_4lvl(vm) &&
346 HAS_PAGE_SIZES(vm->i915, I915_GTT_PAGE_SIZE_64K))
347 size = I915_GTT_PAGE_SIZE_64K;
350 struct drm_i915_gem_object *obj;
352 obj = vm->alloc_scratch_dma(vm, size);
356 if (map_pt_dma(vm, obj))
359 /* We need a single contiguous page for our scratch */
360 if (obj->mm.page_sizes.sg < size)
363 /* And it needs to be correspondingly aligned */
364 if (__px_dma(obj) & (size - 1))
368 * Use a non-zero scratch page for debugging.
370 * We want a value that should be reasonably obvious
371 * to spot in the error state, while also causing a GPU hang
372 * if executed. We prefer using a clear page in production, so
373 * should it ever be accidentally used, the effect should be
376 poison_scratch_page(obj);
378 vm->scratch[0] = obj;
379 vm->scratch_order = get_order(size);
383 i915_gem_object_put(obj);
385 if (size == I915_GTT_PAGE_SIZE_4K)
389 * If we need 64K minimum GTT pages for device local-memory,
390 * like on XEHPSDV, then we need to fail the allocation here,
391 * otherwise we can't safely support the insertion of
392 * local-memory pages for this vm, since the HW expects the
393 * correct physical alignment and size when the page-table is
394 * operating in 64K GTT mode, which includes any scratch PTEs,
395 * since userspace can still touch them.
397 if (HAS_64K_PAGES(vm->i915))
400 size = I915_GTT_PAGE_SIZE_4K;
404 void free_scratch(struct i915_address_space *vm)
408 for (i = 0; i <= vm->top; i++)
409 i915_gem_object_put(vm->scratch[i]);
412 void gtt_write_workarounds(struct intel_gt *gt)
414 struct drm_i915_private *i915 = gt->i915;
415 struct intel_uncore *uncore = gt->uncore;
418 * This function is for gtt related workarounds. This function is
419 * called on driver load and after a GPU reset, so you can place
420 * workarounds here even if they get overwritten by GPU reset.
422 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk,cfl,cnl,icl */
423 if (IS_BROADWELL(i915))
424 intel_uncore_write(uncore,
426 GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
427 else if (IS_CHERRYVIEW(i915))
428 intel_uncore_write(uncore,
430 GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
431 else if (IS_GEN9_LP(i915))
432 intel_uncore_write(uncore,
434 GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
435 else if (GRAPHICS_VER(i915) >= 9 && GRAPHICS_VER(i915) <= 11)
436 intel_uncore_write(uncore,
438 GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
441 * To support 64K PTEs we need to first enable the use of the
442 * Intermediate-Page-Size(IPS) bit of the PDE field via some magical
443 * mmio, otherwise the page-walker will simply ignore the IPS bit. This
444 * shouldn't be needed after GEN10.
446 * 64K pages were first introduced from BDW+, although technically they
447 * only *work* from gen9+. For pre-BDW we instead have the option for
448 * 32K pages, but we don't currently have any support for it in our
451 if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_64K) &&
452 GRAPHICS_VER(i915) <= 10)
453 intel_uncore_rmw(uncore,
454 GEN8_GAMW_ECO_DEV_RW_IA,
456 GAMW_ECO_ENABLE_64K_IPS_FIELD);
458 if (IS_GRAPHICS_VER(i915, 8, 11)) {
459 bool can_use_gtt_cache = true;
462 * According to the BSpec if we use 2M/1G pages then we also
463 * need to disable the GTT cache. At least on BDW we can see
464 * visual corruption when using 2M pages, and not disabling the
467 if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_2M))
468 can_use_gtt_cache = false;
470 /* WaGttCachingOffByDefault */
471 intel_uncore_write(uncore,
473 can_use_gtt_cache ? GTT_CACHE_EN_ALL : 0);
474 drm_WARN_ON_ONCE(&i915->drm, can_use_gtt_cache &&
475 intel_uncore_read(uncore,
476 HSW_GTT_CACHE_EN) == 0);
480 static void tgl_setup_private_ppat(struct intel_uncore *uncore)
482 /* TGL doesn't support LLC or AGE settings */
483 intel_uncore_write(uncore, GEN12_PAT_INDEX(0), GEN8_PPAT_WB);
484 intel_uncore_write(uncore, GEN12_PAT_INDEX(1), GEN8_PPAT_WC);
485 intel_uncore_write(uncore, GEN12_PAT_INDEX(2), GEN8_PPAT_WT);
486 intel_uncore_write(uncore, GEN12_PAT_INDEX(3), GEN8_PPAT_UC);
487 intel_uncore_write(uncore, GEN12_PAT_INDEX(4), GEN8_PPAT_WB);
488 intel_uncore_write(uncore, GEN12_PAT_INDEX(5), GEN8_PPAT_WB);
489 intel_uncore_write(uncore, GEN12_PAT_INDEX(6), GEN8_PPAT_WB);
490 intel_uncore_write(uncore, GEN12_PAT_INDEX(7), GEN8_PPAT_WB);
493 static void icl_setup_private_ppat(struct intel_uncore *uncore)
495 intel_uncore_write(uncore,
497 GEN8_PPAT_WB | GEN8_PPAT_LLC);
498 intel_uncore_write(uncore,
500 GEN8_PPAT_WC | GEN8_PPAT_LLCELLC);
501 intel_uncore_write(uncore,
503 GEN8_PPAT_WB | GEN8_PPAT_ELLC_OVERRIDE);
504 intel_uncore_write(uncore,
507 intel_uncore_write(uncore,
509 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0));
510 intel_uncore_write(uncore,
512 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1));
513 intel_uncore_write(uncore,
515 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2));
516 intel_uncore_write(uncore,
518 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
522 * The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
523 * bits. When using advanced contexts each context stores its own PAT, but
524 * writing this data shouldn't be harmful even in those cases.
526 static void bdw_setup_private_ppat(struct intel_uncore *uncore)
528 struct drm_i915_private *i915 = uncore->i915;
531 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
532 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
533 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
534 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
535 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
536 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
537 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
539 /* for scanout with eLLC */
540 if (GRAPHICS_VER(i915) >= 9)
541 pat |= GEN8_PPAT(2, GEN8_PPAT_WB | GEN8_PPAT_ELLC_OVERRIDE);
543 pat |= GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC);
545 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
546 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
549 static void chv_setup_private_ppat(struct intel_uncore *uncore)
554 * Map WB on BDW to snooped on CHV.
556 * Only the snoop bit has meaning for CHV, the rest is
559 * The hardware will never snoop for certain types of accesses:
560 * - CPU GTT (GMADR->GGTT->no snoop->memory)
561 * - PPGTT page tables
562 * - some other special cycles
564 * As with BDW, we also need to consider the following for GT accesses:
565 * "For GGTT, there is NO pat_sel[2:0] from the entry,
566 * so RTL will always use the value corresponding to
568 * Which means we must set the snoop bit in PAT entry 0
569 * in order to keep the global status page working.
572 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
576 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
577 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
578 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
579 GEN8_PPAT(7, CHV_PPAT_SNOOP);
581 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
582 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
585 void setup_private_pat(struct intel_uncore *uncore)
587 struct drm_i915_private *i915 = uncore->i915;
589 GEM_BUG_ON(GRAPHICS_VER(i915) < 8);
591 if (GRAPHICS_VER(i915) >= 12)
592 tgl_setup_private_ppat(uncore);
593 else if (GRAPHICS_VER(i915) >= 11)
594 icl_setup_private_ppat(uncore);
595 else if (IS_CHERRYVIEW(i915) || IS_GEN9_LP(i915))
596 chv_setup_private_ppat(uncore);
598 bdw_setup_private_ppat(uncore);
602 __vm_create_scratch_for_read(struct i915_address_space *vm, unsigned long size)
604 struct drm_i915_gem_object *obj;
605 struct i915_vma *vma;
607 obj = i915_gem_object_create_internal(vm->i915, PAGE_ALIGN(size));
609 return ERR_CAST(obj);
611 i915_gem_object_set_cache_coherency(obj, I915_CACHING_CACHED);
613 vma = i915_vma_instance(obj, vm, NULL);
615 i915_gem_object_put(obj);
623 __vm_create_scratch_for_read_pinned(struct i915_address_space *vm, unsigned long size)
625 struct i915_vma *vma;
628 vma = __vm_create_scratch_for_read(vm, size);
632 err = i915_vma_pin(vma, 0, 0,
633 i915_vma_is_ggtt(vma) ? PIN_GLOBAL : PIN_USER);
642 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
643 #include "selftests/mock_gtt.c"