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>
10 #include "gem/i915_gem_lmem.h"
11 #include "i915_trace.h"
13 #include "intel_gtt.h"
15 struct drm_i915_gem_object *alloc_pt_lmem(struct i915_address_space *vm, int sz)
17 struct drm_i915_gem_object *obj;
19 obj = i915_gem_object_create_lmem(vm->i915, sz, 0);
21 * Ensure all paging structures for this vm share the same dma-resv
22 * object underneath, with the idea that one object_lock() will lock
26 obj->base.resv = i915_vm_resv_get(vm);
27 obj->shares_resv_from = vm;
33 struct drm_i915_gem_object *alloc_pt_dma(struct i915_address_space *vm, int sz)
35 struct drm_i915_gem_object *obj;
37 if (I915_SELFTEST_ONLY(should_fail(&vm->fault_attr, 1)))
38 i915_gem_shrink_all(vm->i915);
40 obj = i915_gem_object_create_internal(vm->i915, sz);
42 * Ensure all paging structures for this vm share the same dma-resv
43 * object underneath, with the idea that one object_lock() will lock
47 obj->base.resv = i915_vm_resv_get(vm);
48 obj->shares_resv_from = vm;
54 int map_pt_dma(struct i915_address_space *vm, struct drm_i915_gem_object *obj)
56 enum i915_map_type type;
59 type = i915_coherent_map_type(vm->i915, obj, true);
60 vaddr = i915_gem_object_pin_map_unlocked(obj, type);
62 return PTR_ERR(vaddr);
64 i915_gem_object_make_unshrinkable(obj);
68 int map_pt_dma_locked(struct i915_address_space *vm, struct drm_i915_gem_object *obj)
70 enum i915_map_type type;
73 type = i915_coherent_map_type(vm->i915, obj, true);
74 vaddr = i915_gem_object_pin_map(obj, type);
76 return PTR_ERR(vaddr);
78 i915_gem_object_make_unshrinkable(obj);
82 void __i915_vm_close(struct i915_address_space *vm)
84 struct i915_vma *vma, *vn;
86 if (!atomic_dec_and_mutex_lock(&vm->open, &vm->mutex))
89 list_for_each_entry_safe(vma, vn, &vm->bound_list, vm_link) {
90 struct drm_i915_gem_object *obj = vma->obj;
92 /* Keep the obj (and hence the vma) alive as _we_ destroy it */
93 if (!kref_get_unless_zero(&obj->base.refcount))
96 atomic_and(~I915_VMA_PIN_MASK, &vma->flags);
97 WARN_ON(__i915_vma_unbind(vma));
100 i915_gem_object_put(obj);
102 GEM_BUG_ON(!list_empty(&vm->bound_list));
104 mutex_unlock(&vm->mutex);
107 /* lock the vm into the current ww, if we lock one, we lock all */
108 int i915_vm_lock_objects(struct i915_address_space *vm,
109 struct i915_gem_ww_ctx *ww)
111 if (vm->scratch[0]->base.resv == &vm->_resv) {
112 return i915_gem_object_lock(vm->scratch[0], ww);
114 struct i915_ppgtt *ppgtt = i915_vm_to_ppgtt(vm);
116 /* We borrowed the scratch page from ggtt, take the top level object */
117 return i915_gem_object_lock(ppgtt->pd->pt.base, ww);
121 void i915_address_space_fini(struct i915_address_space *vm)
123 drm_mm_takedown(&vm->mm);
124 mutex_destroy(&vm->mutex);
128 * i915_vm_resv_release - Final struct i915_address_space destructor
129 * @kref: Pointer to the &i915_address_space.resv_ref member.
131 * This function is called when the last lock sharer no longer shares the
132 * &i915_address_space._resv lock.
134 void i915_vm_resv_release(struct kref *kref)
136 struct i915_address_space *vm =
137 container_of(kref, typeof(*vm), resv_ref);
139 dma_resv_fini(&vm->_resv);
143 static void __i915_vm_release(struct work_struct *work)
145 struct i915_address_space *vm =
146 container_of(work, struct i915_address_space, rcu.work);
149 i915_address_space_fini(vm);
151 i915_vm_resv_put(vm);
154 void i915_vm_release(struct kref *kref)
156 struct i915_address_space *vm =
157 container_of(kref, struct i915_address_space, ref);
159 GEM_BUG_ON(i915_is_ggtt(vm));
160 trace_i915_ppgtt_release(vm);
162 queue_rcu_work(vm->i915->wq, &vm->rcu);
165 void i915_address_space_init(struct i915_address_space *vm, int subclass)
170 * Special case for GGTT that has already done an early
173 if (!kref_read(&vm->resv_ref))
174 kref_init(&vm->resv_ref);
176 INIT_RCU_WORK(&vm->rcu, __i915_vm_release);
177 atomic_set(&vm->open, 1);
180 * The vm->mutex must be reclaim safe (for use in the shrinker).
181 * Do a dummy acquire now under fs_reclaim so that any allocation
182 * attempt holding the lock is immediately reported by lockdep.
184 mutex_init(&vm->mutex);
185 lockdep_set_subclass(&vm->mutex, subclass);
187 if (!intel_vm_no_concurrent_access_wa(vm->i915)) {
188 i915_gem_shrinker_taints_mutex(vm->i915, &vm->mutex);
191 * CHV + BXT VTD workaround use stop_machine(),
192 * which is allowed to allocate memory. This means &vm->mutex
193 * is the outer lock, and in theory we can allocate memory inside
194 * it through stop_machine().
196 * Add the annotation for this, we use trylock in shrinker.
198 mutex_acquire(&vm->mutex.dep_map, 0, 0, _THIS_IP_);
199 might_alloc(GFP_KERNEL);
200 mutex_release(&vm->mutex.dep_map, _THIS_IP_);
202 dma_resv_init(&vm->_resv);
204 GEM_BUG_ON(!vm->total);
205 drm_mm_init(&vm->mm, 0, vm->total);
206 vm->mm.head_node.color = I915_COLOR_UNEVICTABLE;
208 INIT_LIST_HEAD(&vm->bound_list);
211 void clear_pages(struct i915_vma *vma)
213 GEM_BUG_ON(!vma->pages);
215 if (vma->pages != vma->obj->mm.pages) {
216 sg_free_table(vma->pages);
221 memset(&vma->page_sizes, 0, sizeof(vma->page_sizes));
224 void *__px_vaddr(struct drm_i915_gem_object *p)
226 enum i915_map_type type;
228 GEM_BUG_ON(!i915_gem_object_has_pages(p));
229 return page_unpack_bits(p->mm.mapping, &type);
232 dma_addr_t __px_dma(struct drm_i915_gem_object *p)
234 GEM_BUG_ON(!i915_gem_object_has_pages(p));
235 return sg_dma_address(p->mm.pages->sgl);
238 struct page *__px_page(struct drm_i915_gem_object *p)
240 GEM_BUG_ON(!i915_gem_object_has_pages(p));
241 return sg_page(p->mm.pages->sgl);
245 fill_page_dma(struct drm_i915_gem_object *p, const u64 val, unsigned int count)
247 void *vaddr = __px_vaddr(p);
249 memset64(vaddr, val, count);
250 clflush_cache_range(vaddr, PAGE_SIZE);
253 static void poison_scratch_page(struct drm_i915_gem_object *scratch)
255 void *vaddr = __px_vaddr(scratch);
259 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
262 memset(vaddr, val, scratch->base.size);
265 int setup_scratch_page(struct i915_address_space *vm)
270 * In order to utilize 64K pages for an object with a size < 2M, we will
271 * need to support a 64K scratch page, given that every 16th entry for a
272 * page-table operating in 64K mode must point to a properly aligned 64K
273 * region, including any PTEs which happen to point to scratch.
275 * This is only relevant for the 48b PPGTT where we support
276 * huge-gtt-pages, see also i915_vma_insert(). However, as we share the
277 * scratch (read-only) between all vm, we create one 64k scratch page
280 size = I915_GTT_PAGE_SIZE_4K;
281 if (i915_vm_is_4lvl(vm) &&
282 HAS_PAGE_SIZES(vm->i915, I915_GTT_PAGE_SIZE_64K))
283 size = I915_GTT_PAGE_SIZE_64K;
286 struct drm_i915_gem_object *obj;
288 obj = vm->alloc_pt_dma(vm, size);
292 if (map_pt_dma(vm, obj))
295 /* We need a single contiguous page for our scratch */
296 if (obj->mm.page_sizes.sg < size)
299 /* And it needs to be correspondingly aligned */
300 if (__px_dma(obj) & (size - 1))
304 * Use a non-zero scratch page for debugging.
306 * We want a value that should be reasonably obvious
307 * to spot in the error state, while also causing a GPU hang
308 * if executed. We prefer using a clear page in production, so
309 * should it ever be accidentally used, the effect should be
312 poison_scratch_page(obj);
314 vm->scratch[0] = obj;
315 vm->scratch_order = get_order(size);
319 i915_gem_object_put(obj);
321 if (size == I915_GTT_PAGE_SIZE_4K)
324 size = I915_GTT_PAGE_SIZE_4K;
328 void free_scratch(struct i915_address_space *vm)
332 for (i = 0; i <= vm->top; i++)
333 i915_gem_object_put(vm->scratch[i]);
336 void gtt_write_workarounds(struct intel_gt *gt)
338 struct drm_i915_private *i915 = gt->i915;
339 struct intel_uncore *uncore = gt->uncore;
342 * This function is for gtt related workarounds. This function is
343 * called on driver load and after a GPU reset, so you can place
344 * workarounds here even if they get overwritten by GPU reset.
346 /* WaIncreaseDefaultTLBEntries:chv,bdw,skl,bxt,kbl,glk,cfl,cnl,icl */
347 if (IS_BROADWELL(i915))
348 intel_uncore_write(uncore,
350 GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_BDW);
351 else if (IS_CHERRYVIEW(i915))
352 intel_uncore_write(uncore,
354 GEN8_L3_LRA_1_GPGPU_DEFAULT_VALUE_CHV);
355 else if (IS_GEN9_LP(i915))
356 intel_uncore_write(uncore,
358 GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_BXT);
359 else if (GRAPHICS_VER(i915) >= 9 && GRAPHICS_VER(i915) <= 11)
360 intel_uncore_write(uncore,
362 GEN9_L3_LRA_1_GPGPU_DEFAULT_VALUE_SKL);
365 * To support 64K PTEs we need to first enable the use of the
366 * Intermediate-Page-Size(IPS) bit of the PDE field via some magical
367 * mmio, otherwise the page-walker will simply ignore the IPS bit. This
368 * shouldn't be needed after GEN10.
370 * 64K pages were first introduced from BDW+, although technically they
371 * only *work* from gen9+. For pre-BDW we instead have the option for
372 * 32K pages, but we don't currently have any support for it in our
375 if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_64K) &&
376 GRAPHICS_VER(i915) <= 10)
377 intel_uncore_rmw(uncore,
378 GEN8_GAMW_ECO_DEV_RW_IA,
380 GAMW_ECO_ENABLE_64K_IPS_FIELD);
382 if (IS_GRAPHICS_VER(i915, 8, 11)) {
383 bool can_use_gtt_cache = true;
386 * According to the BSpec if we use 2M/1G pages then we also
387 * need to disable the GTT cache. At least on BDW we can see
388 * visual corruption when using 2M pages, and not disabling the
391 if (HAS_PAGE_SIZES(i915, I915_GTT_PAGE_SIZE_2M))
392 can_use_gtt_cache = false;
394 /* WaGttCachingOffByDefault */
395 intel_uncore_write(uncore,
397 can_use_gtt_cache ? GTT_CACHE_EN_ALL : 0);
398 drm_WARN_ON_ONCE(&i915->drm, can_use_gtt_cache &&
399 intel_uncore_read(uncore,
400 HSW_GTT_CACHE_EN) == 0);
404 static void tgl_setup_private_ppat(struct intel_uncore *uncore)
406 /* TGL doesn't support LLC or AGE settings */
407 intel_uncore_write(uncore, GEN12_PAT_INDEX(0), GEN8_PPAT_WB);
408 intel_uncore_write(uncore, GEN12_PAT_INDEX(1), GEN8_PPAT_WC);
409 intel_uncore_write(uncore, GEN12_PAT_INDEX(2), GEN8_PPAT_WT);
410 intel_uncore_write(uncore, GEN12_PAT_INDEX(3), GEN8_PPAT_UC);
411 intel_uncore_write(uncore, GEN12_PAT_INDEX(4), GEN8_PPAT_WB);
412 intel_uncore_write(uncore, GEN12_PAT_INDEX(5), GEN8_PPAT_WB);
413 intel_uncore_write(uncore, GEN12_PAT_INDEX(6), GEN8_PPAT_WB);
414 intel_uncore_write(uncore, GEN12_PAT_INDEX(7), GEN8_PPAT_WB);
417 static void cnl_setup_private_ppat(struct intel_uncore *uncore)
419 intel_uncore_write(uncore,
421 GEN8_PPAT_WB | GEN8_PPAT_LLC);
422 intel_uncore_write(uncore,
424 GEN8_PPAT_WC | GEN8_PPAT_LLCELLC);
425 intel_uncore_write(uncore,
427 GEN8_PPAT_WB | GEN8_PPAT_ELLC_OVERRIDE);
428 intel_uncore_write(uncore,
431 intel_uncore_write(uncore,
433 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0));
434 intel_uncore_write(uncore,
436 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1));
437 intel_uncore_write(uncore,
439 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2));
440 intel_uncore_write(uncore,
442 GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
446 * The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
447 * bits. When using advanced contexts each context stores its own PAT, but
448 * writing this data shouldn't be harmful even in those cases.
450 static void bdw_setup_private_ppat(struct intel_uncore *uncore)
452 struct drm_i915_private *i915 = uncore->i915;
455 pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */
456 GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
457 GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */
458 GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
459 GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
460 GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
461 GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
463 /* for scanout with eLLC */
464 if (GRAPHICS_VER(i915) >= 9)
465 pat |= GEN8_PPAT(2, GEN8_PPAT_WB | GEN8_PPAT_ELLC_OVERRIDE);
467 pat |= GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC);
469 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
470 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
473 static void chv_setup_private_ppat(struct intel_uncore *uncore)
478 * Map WB on BDW to snooped on CHV.
480 * Only the snoop bit has meaning for CHV, the rest is
483 * The hardware will never snoop for certain types of accesses:
484 * - CPU GTT (GMADR->GGTT->no snoop->memory)
485 * - PPGTT page tables
486 * - some other special cycles
488 * As with BDW, we also need to consider the following for GT accesses:
489 * "For GGTT, there is NO pat_sel[2:0] from the entry,
490 * so RTL will always use the value corresponding to
492 * Which means we must set the snoop bit in PAT entry 0
493 * in order to keep the global status page working.
496 pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
500 GEN8_PPAT(4, CHV_PPAT_SNOOP) |
501 GEN8_PPAT(5, CHV_PPAT_SNOOP) |
502 GEN8_PPAT(6, CHV_PPAT_SNOOP) |
503 GEN8_PPAT(7, CHV_PPAT_SNOOP);
505 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_LO, lower_32_bits(pat));
506 intel_uncore_write(uncore, GEN8_PRIVATE_PAT_HI, upper_32_bits(pat));
509 void setup_private_pat(struct intel_uncore *uncore)
511 struct drm_i915_private *i915 = uncore->i915;
513 GEM_BUG_ON(GRAPHICS_VER(i915) < 8);
515 if (GRAPHICS_VER(i915) >= 12)
516 tgl_setup_private_ppat(uncore);
517 else if (GRAPHICS_VER(i915) >= 10)
518 cnl_setup_private_ppat(uncore);
519 else if (IS_CHERRYVIEW(i915) || IS_GEN9_LP(i915))
520 chv_setup_private_ppat(uncore);
522 bdw_setup_private_ppat(uncore);
526 __vm_create_scratch_for_read(struct i915_address_space *vm, unsigned long size)
528 struct drm_i915_gem_object *obj;
529 struct i915_vma *vma;
531 obj = i915_gem_object_create_internal(vm->i915, PAGE_ALIGN(size));
533 return ERR_CAST(obj);
535 i915_gem_object_set_cache_coherency(obj, I915_CACHING_CACHED);
537 vma = i915_vma_instance(obj, vm, NULL);
539 i915_gem_object_put(obj);
547 __vm_create_scratch_for_read_pinned(struct i915_address_space *vm, unsigned long size)
549 struct i915_vma *vma;
552 vma = __vm_create_scratch_for_read(vm, size);
556 err = i915_vma_pin(vma, 0, 0,
557 i915_vma_is_ggtt(vma) ? PIN_GLOBAL : PIN_USER);
566 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
567 #include "selftests/mock_gtt.c"