Skip to contentSkip to conversation

PAT and cache coherency support

PATCH Superseded Matthew Auld 4 participants 11 messages Sep 27, 2023 – Sep 28, 2023

Subsystems (3)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v4 cover PAT and cache coherency support

Branch available here:
https://gitlab.freedesktop.org/mwa/kernel/-/tree/xe-pat-index?ref_type=heads

Series directly depends on the patches here:
https://patchwork.freedesktop.org/series/124225/

Goal here is to allow userspace to directly control the pat_index when mapping memory via the ppGTT, in addtion to the CPU caching mode. This is very much needed on newer igpu platforms which allow incoherent GT access, where the choice over the cache level and expected coherency is best left to userspace depending on their usecase. In the future there may also be other stuff encoded in the pat_index, so giving userspace direct control will also be needed there.

To support this we added new gem_create uAPI for selecting the CPU cache mode to use for system memory, including the expected GPU coherency mode. There are various restrictions here for the selected coherency mode and compatible CPU cache modes. With that in place the actual pat_index can now be provided as part of vm_bind. The only restriction is that the coherency mode of the pat_index must be at least as coherent as the gem_create coherency mode. There are also some special cases like with userptr and dma-buf.

v2:
- Loads of improvements/tweaks. Main changes are to now allow gem_create.coh_mode <= coh_mode(pat_index), rather than it needing to match exactly. This simplifies the dma-buf policy from userspace pov. Also we now only consider COH_NONE and COH_AT_LEAST_1WAY.
v3:
- Rebase. Split the pte_encode() refactoring, plus various smaller tweaks and fixes.
v4:
- Rebase on Lucas' new series.
- Drop UC cache mode.
- s/smem_cpu_caching/cpu_caching/. Idea is to make VRAM WC explicit in the uapi, plus make it more future proof.

Signature · 1 line
2.41.0
#1 lore raw

PATCH v4 1/5 drm/xe/pat: trim the xelp PAT table

We don't seem to use the 4-7 pat indexes, even though they are defined by the HW. In the next patch userspace will be able to directly set the pat_index as part of vm_bind and we don't want to allow setting 4-7. Simplest is to just ignore them here.

---
 drivers/gpu/drm/xe/xe_pat.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/gpu/drm/xe/xe_pat.c +0 -4
index 6efa44556689..2c759ffa35ef 100644
@@ -42,10 +42,6 @@ static const u32 xelp_pat_table[] = {
 	[1] = XELP_PAT_WC,
 	[2] = XELP_PAT_WT,
 	[3] = XELP_PAT_UC,
-	[4] = XELP_PAT_WB,
-	[5] = XELP_PAT_WB,
-	[6] = XELP_PAT_WB,
-	[7] = XELP_PAT_WB,
 };
 
 static const u32 xehpc_pat_table[] = {
Signature · 1 line
2.41.0
#2 lore raw

PATCH v4 2/5 drm/xe: directly use pat_index for pte_encode

In reply to Matthew Auld

In the next patch userspace will be able to directly set the pat_index as part of vm_bind. To support this we need to get away from using xe_cache_level in the low level routines and rather just use the pat_index directly.

v2: Rebase

Cc Pallavi Mishra <[email protected]>
Cc Lucas De Marchi <[email protected]>
Cc Matt Roper <[email protected]>
---
 drivers/gpu/drm/xe/xe_ggtt.c       |  7 +++----
 drivers/gpu/drm/xe/xe_ggtt_types.h |  3 +--
 drivers/gpu/drm/xe/xe_migrate.c    | 19 +++++++++++--------
 drivers/gpu/drm/xe/xe_pt.c         | 11 ++++++-----
 drivers/gpu/drm/xe/xe_pt_types.h   |  8 ++++----
 drivers/gpu/drm/xe/xe_vm.c         | 24 +++++++++++-------------
 6 files changed, 36 insertions(+), 36 deletions(-)
drivers/gpu/drm/xe/xe_ggtt.c +3 -4
index 99b54794917e..2334c47c19cc 100644
@@ -27,7 +27,7 @@
 #define GUC_GGTT_TOP	0xFEE00000
 
 static u64 xelp_ggtt_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
-				   enum xe_cache_level cache)
+				   u16 pat_index)
 {
 	u64 pte;
 
@@ -41,13 +41,12 @@ static u64 xelp_ggtt_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
 }
 
 static u64 xelpg_ggtt_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
-				    enum xe_cache_level cache)
+				    u16 pat_index)
 {
 	struct xe_device *xe = xe_bo_device(bo);
-	u32 pat_index = xe->pat.idx[cache];
 	u64 pte;
 
-	pte = xelp_ggtt_pte_encode_bo(bo, bo_offset, cache);
+	pte = xelp_ggtt_pte_encode_bo(bo, bo_offset, pat_index);
 
 	xe_assert(xe, pat_index <= 3);
 
drivers/gpu/drm/xe/xe_ggtt_types.h +1 -2
index 486016ea5b67..d8c584d9a8c3 100644
@@ -14,8 +14,7 @@ struct xe_bo;
 struct xe_gt;
 
 struct xe_ggtt_pt_ops {
-	u64 (*pte_encode_bo)(struct xe_bo *bo, u64 bo_offset,
-			     enum xe_cache_level cache);
+	u64 (*pte_encode_bo)(struct xe_bo *bo, u64 bo_offset, u16 pat_index);
 };
 
 struct xe_ggtt {
drivers/gpu/drm/xe/xe_migrate.c +11 -8
index 258c2269c916..90a1ff1aca9b 100644
@@ -158,6 +158,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 				 struct xe_vm *vm)
 {
 	struct xe_device *xe = tile_to_xe(tile);
+	u16 pat_index = xe->pat.idx[XE_CACHE_WB];
 	u8 id = tile->id;
 	u32 num_entries = NUM_PT_SLOTS, num_level = vm->pt_root[id]->level;
 	u32 map_ofs, level, i;
@@ -189,7 +190,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 		return ret;
 	}
 
-	entry = vm->pt_ops->pde_encode_bo(bo, bo->size - XE_PAGE_SIZE, XE_CACHE_WB);
+	entry = vm->pt_ops->pde_encode_bo(bo, bo->size - XE_PAGE_SIZE, pat_index);
 	xe_pt_write(xe, &vm->pt_root[id]->bo->vmap, 0, entry);
 
 	map_ofs = (num_entries - num_level) * XE_PAGE_SIZE;
@@ -197,7 +198,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 	/* Map the entire BO in our level 0 pt */
 	for (i = 0, level = 0; i < num_entries; level++) {
 		entry = vm->pt_ops->pte_encode_bo(bo, i * XE_PAGE_SIZE,
-						  XE_CACHE_WB, 0);
+						  pat_index, 0);
 
 		xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, entry);
 
@@ -216,7 +217,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 		     i += vm->flags & XE_VM_FLAG_64K ? XE_64K_PAGE_SIZE :
 		     XE_PAGE_SIZE) {
 			entry = vm->pt_ops->pte_encode_bo(batch, i,
-							  XE_CACHE_WB, 0);
+							  pat_index, 0);
 
 			xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64,
 				  entry);
@@ -241,7 +242,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 			flags = XE_PDE_64K;
 
 		entry = vm->pt_ops->pde_encode_bo(bo, map_ofs + (level - 1) *
-						  XE_PAGE_SIZE, XE_CACHE_WB);
+						  XE_PAGE_SIZE, pat_index);
 		xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE * level, u64,
 			  entry | flags);
 	}
@@ -249,7 +250,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 	/* Write PDE's that point to our BO. */
 	for (i = 0; i < num_entries - num_level; i++) {
 		entry = vm->pt_ops->pde_encode_bo(bo, i * XE_PAGE_SIZE,
-						  XE_CACHE_WB);
+						  pat_index);
 
 		xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE +
 			  (i + 1) * 8, u64, entry);
@@ -261,7 +262,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 
 		level = 2;
 		ofs = map_ofs + XE_PAGE_SIZE * level + 256 * 8;
-		flags = vm->pt_ops->pte_encode_addr(xe, 0, XE_CACHE_WB, level,
+		flags = vm->pt_ops->pte_encode_addr(xe, 0, pat_index, level,
 						    true, 0);
 
 		/*
@@ -457,6 +458,7 @@ static void emit_pte(struct xe_migrate *m,
 		     struct xe_res_cursor *cur,
 		     u32 size, struct xe_bo *bo)
 {
+	u16 pat_index = m->tile->xe->pat.idx[XE_CACHE_WB];
 	u32 ptes;
 	u64 ofs = at_pt * XE_PAGE_SIZE;
 	u64 cur_ofs;
@@ -500,7 +502,7 @@ static void emit_pte(struct xe_migrate *m,
 			}
 
 			addr = m->q->vm->pt_ops->pte_encode_addr(m->tile->xe,
-								 addr, XE_CACHE_WB,
+								 addr, pat_index,
 								 0, devmem, flags);
 			bb->cs[bb->len++] = lower_32_bits(addr);
 			bb->cs[bb->len++] = upper_32_bits(addr);
@@ -1190,6 +1192,7 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 	bool first_munmap_rebind = vma &&
 		vma->gpuva.flags & XE_VMA_FIRST_REBIND;
 	struct xe_exec_queue *q_override = !q ? m->q : q;
+	u16 pat_index = xe->pat.idx[XE_CACHE_WB];
 
 	/* Use the CPU if no in syncs and engine is idle */
 	if (no_in_syncs(syncs, num_syncs) && xe_exec_queue_is_idle(q_override)) {
@@ -1261,7 +1264,7 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 
 			xe_tile_assert(tile, pt_bo->size == SZ_4K);
 
-			addr = vm->pt_ops->pte_encode_bo(pt_bo, 0, XE_CACHE_WB, 0);
+			addr = vm->pt_ops->pte_encode_bo(pt_bo, 0, pat_index, 0);
 			bb->cs[bb->len++] = lower_32_bits(addr);
 			bb->cs[bb->len++] = upper_32_bits(addr);
 		}
drivers/gpu/drm/xe/xe_pt.c +6 -5
index 4d4c6a4c305e..92b512641b4a 100644
@@ -50,6 +50,7 @@ static struct xe_pt *xe_pt_entry(struct xe_pt_dir *pt_dir, unsigned int index)
 static u64 __xe_pt_empty_pte(struct xe_tile *tile, struct xe_vm *vm,
 			     unsigned int level)
 {
+	u16 pat_index = tile_to_xe(tile)->pat.idx[XE_CACHE_WB];
 	u8 id = tile->id;
 
 	if (!vm->scratch_bo[id])
@@ -57,9 +58,9 @@ static u64 __xe_pt_empty_pte(struct xe_tile *tile, struct xe_vm *vm,
 
 	if (level > 0)
 		return vm->pt_ops->pde_encode_bo(vm->scratch_pt[id][level - 1]->bo,
-						 0, XE_CACHE_WB);
+						 0, pat_index);
 
-	return vm->pt_ops->pte_encode_bo(vm->scratch_bo[id], 0, XE_CACHE_WB, 0);
+	return vm->pt_ops->pte_encode_bo(vm->scratch_bo[id], 0, pat_index, 0);
 }
 
 /**
@@ -510,6 +511,7 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset,
 {
 	struct xe_pt_stage_bind_walk *xe_walk =
 		container_of(walk, typeof(*xe_walk), base);
+	u16 pat_index = tile_to_xe(xe_walk->tile)->pat.idx[xe_walk->cache];
 	struct xe_pt *xe_parent = container_of(parent, typeof(*xe_parent), base);
 	struct xe_vm *vm = xe_walk->vm;
 	struct xe_pt *xe_child;
@@ -526,7 +528,7 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset,
 
 		pte = vm->pt_ops->pte_encode_vma(is_null ? 0 :
 						 xe_res_dma(curs) + xe_walk->dma_offset,
-						 xe_walk->vma, xe_walk->cache, level);
+						 xe_walk->vma, pat_index, level);
 		pte |= xe_walk->default_pte;
 
 		/*
@@ -591,8 +593,7 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset,
 			xe_child->is_compact = true;
 		}
 
-		pte = vm->pt_ops->pde_encode_bo(xe_child->bo, 0,
-						xe_walk->cache) | flags;
+		pte = vm->pt_ops->pde_encode_bo(xe_child->bo, 0, pat_index) | flags;
 		ret = xe_pt_insert_entry(xe_walk, xe_parent, offset, xe_child,
 					 pte);
 	}
drivers/gpu/drm/xe/xe_pt_types.h +4 -4
index bd6645295fe6..355fa8f014e9 100644
@@ -38,14 +38,14 @@ struct xe_pt {
 
 struct xe_pt_ops {
 	u64 (*pte_encode_bo)(struct xe_bo *bo, u64 bo_offset,
-			     enum xe_cache_level cache, u32 pt_level);
+			     u16 pat_index, u32 pt_level);
 	u64 (*pte_encode_vma)(u64 pte, struct xe_vma *vma,
-			      enum xe_cache_level cache, u32 pt_level);
+			      u16 pat_index, u32 pt_level);
 	u64 (*pte_encode_addr)(struct xe_device *xe, u64 addr,
-			       enum xe_cache_level cache,
+			       u16 pat_index,
 			       u32 pt_level, bool devmem, u64 flags);
 	u64 (*pde_encode_bo)(struct xe_bo *bo, u64 bo_offset,
-			     const enum xe_cache_level cache);
+			     const u16 pat_index);
 };
 
 struct xe_pt_entry {
drivers/gpu/drm/xe/xe_vm.c +11 -13
index beffbb1039d3..962bfd2b0179 100644
@@ -1191,9 +1191,8 @@ static struct drm_gpuva_fn_ops gpuva_ops = {
 	.op_alloc = xe_vm_op_alloc,
 };
 
-static u64 pde_encode_cache(struct xe_device *xe, enum xe_cache_level cache)
+static u64 pde_encode_pat_index(struct xe_device *xe, u16 pat_index)
 {
-	u32 pat_index = xe->pat.idx[cache];
 	u64 pte = 0;
 
 	if (pat_index & BIT(0))
@@ -1205,9 +1204,8 @@ static u64 pde_encode_cache(struct xe_device *xe, enum xe_cache_level cache)
 	return pte;
 }
 
-static u64 pte_encode_cache(struct xe_device *xe, enum xe_cache_level cache)
+static u64 pte_encode_pat_index(struct xe_device *xe, u16 pat_index)
 {
-	u32 pat_index = xe->pat.idx[cache];
 	u64 pte = 0;
 
 	if (pat_index & BIT(0))
@@ -1238,27 +1236,27 @@ static u64 pte_encode_ps(u32 pt_level)
 }
 
 static u64 xelp_pde_encode_bo(struct xe_bo *bo, u64 bo_offset,
-			      const enum xe_cache_level cache)
+			      const u16 pat_index)
 {
 	struct xe_device *xe = xe_bo_device(bo);
 	u64 pde;
 
 	pde = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
 	pde |= XE_PAGE_PRESENT | XE_PAGE_RW;
-	pde |= pde_encode_cache(xe, cache);
+	pde |= pde_encode_pat_index(xe, pat_index);
 
 	return pde;
 }
 
 static u64 xelp_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
-			      enum xe_cache_level cache, u32 pt_level)
+			      u16 pat_index, u32 pt_level)
 {
 	struct xe_device *xe = xe_bo_device(bo);
 	u64 pte;
 
 	pte = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
 	pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
-	pte |= pte_encode_cache(xe, cache);
+	pte |= pte_encode_pat_index(xe, pat_index);
 	pte |= pte_encode_ps(pt_level);
 
 	if (xe_bo_is_vram(bo) || xe_bo_is_stolen_devmem(bo))
@@ -1268,7 +1266,7 @@ static u64 xelp_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
 }
 
 static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
-			       enum xe_cache_level cache, u32 pt_level)
+			       u16 pat_index, u32 pt_level)
 {
 	struct xe_device *xe = xe_vma_vm(vma)->xe;
 
@@ -1277,7 +1275,7 @@ static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
 	if (likely(!xe_vma_read_only(vma)))
 		pte |= XE_PAGE_RW;
 
-	pte |= pte_encode_cache(xe, cache);
+	pte |= pte_encode_pat_index(xe, pat_index);
 	pte |= pte_encode_ps(pt_level);
 
 	if (unlikely(xe_vma_is_null(vma)))
@@ -1287,7 +1285,7 @@ static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
 }
 
 static u64 xelp_pte_encode_addr(struct xe_device *xe, u64 addr,
-				enum xe_cache_level cache,
+				u16 pat_index,
 				u32 pt_level, bool devmem, u64 flags)
 {
 	u64 pte;
@@ -1297,7 +1295,7 @@ static u64 xelp_pte_encode_addr(struct xe_device *xe, u64 addr,
 
 	pte = addr;
 	pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
-	pte |= pte_encode_cache(xe, cache);
+	pte |= pte_encode_pat_index(xe, pat_index);
 	pte |= pte_encode_ps(pt_level);
 
 	if (devmem)
@@ -1701,7 +1699,7 @@ struct xe_vm *xe_vm_lookup(struct xe_file *xef, u32 id)
 u64 xe_vm_pdp4_descriptor(struct xe_vm *vm, struct xe_tile *tile)
 {
 	return vm->pt_ops->pde_encode_bo(vm->pt_root[tile->id]->bo, 0,
-					 XE_CACHE_WB);
+					 tile->xe->pat.idx[XE_CACHE_WB]);
 }
 
 static struct dma_fence *
Signature · 1 line
2.41.0
#3 lore raw

Re: [Intel-xe] [PATCH v4 2/5] drm/xe: directly use pat_index for pte_encode

Quoted 39 lines · Matthew Auld

In the next patch userspace will be able to directly set the pat_index as part of vm_bind. To support this we need to get away from using xe_cache_level in the low level routines and rather just use the pat_index directly.

v2: Rebase

Cc Pallavi Mishra <[email protected]>
Cc Lucas De Marchi <[email protected]>
Cc Matt Roper <[email protected]>

---

drivers/gpu/drm/xe/xe_ggtt.c       |  7 +++----
drivers/gpu/drm/xe/xe_ggtt_types.h |  3 +--
drivers/gpu/drm/xe/xe_migrate.c    | 19 +++++++++++--------
drivers/gpu/drm/xe/xe_pt.c         | 11 ++++++-----
drivers/gpu/drm/xe/xe_pt_types.h   |  8 ++++----
drivers/gpu/drm/xe/xe_vm.c         | 24 +++++++++++-------------
6 files changed, 36 insertions(+), 36 deletions(-)
drivers/gpu/drm/xe/xe_ggtt.c +2 -2
index 99b54794917e..2334c47c19cc 100644
@@ -27,7 +27,7 @@
 #define GUC_GGTT_TOP	0xFEE00000
 
 static u64 xelp_ggtt_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
-				   enum xe_cache_level cache)
+				   u16 pat_index)
 {
 	u64 pte;
 
@@ -41,13 +41,12 @@ static u64 xelp_ggtt_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
 }
 
 static u64 xelpg_ggtt_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
-				    enum xe_cache_level cache)
+				    u16 pat_index)

drivers/gpu/drm/xe/xe_ggtt.c static u64 xelp_ggtt_pte_encode_bo(struct xe_bo *bo, u64 bo_offset, · line 52

Quoted 10 lines
drivers/gpu/drm/xe/xe_ggtt.c +1 -2
 {
 	struct xe_device *xe = xe_bo_device(bo);
-	u32 pat_index = xe->pat.idx[cache];
 	u64 pte;
 
-	pte = xelp_ggtt_pte_encode_bo(bo, bo_offset, cache);
+	pte = xelp_ggtt_pte_encode_bo(bo, bo_offset, pat_index);
 
 	xe_assert(xe, pat_index <= 3);
 

Looks like this file has couple instances of pte_encode_bo() calls which needs to be updated to use pat_index instead of cache level.

Quoted 81 lines
drivers/gpu/drm/xe/xe_ggtt_types.h +1 -2
index 486016ea5b67..d8c584d9a8c3 100644
@@ -14,8 +14,7 @@ struct xe_bo;
 struct xe_gt;
 
 struct xe_ggtt_pt_ops {
-	u64 (*pte_encode_bo)(struct xe_bo *bo, u64 bo_offset,
-			     enum xe_cache_level cache);
+	u64 (*pte_encode_bo)(struct xe_bo *bo, u64 bo_offset, u16 pat_index);
 };
 
 struct xe_ggtt {
drivers/gpu/drm/xe/xe_migrate.c +7 -6
index 258c2269c916..90a1ff1aca9b 100644
@@ -158,6 +158,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 				 struct xe_vm *vm)
 {
 	struct xe_device *xe = tile_to_xe(tile);
+	u16 pat_index = xe->pat.idx[XE_CACHE_WB];
 	u8 id = tile->id;
 	u32 num_entries = NUM_PT_SLOTS, num_level = vm->pt_root[id]->level;
 	u32 map_ofs, level, i;
@@ -189,7 +190,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 		return ret;
 	}
 
-	entry = vm->pt_ops->pde_encode_bo(bo, bo->size - XE_PAGE_SIZE, XE_CACHE_WB);
+	entry = vm->pt_ops->pde_encode_bo(bo, bo->size - XE_PAGE_SIZE, pat_index);
 	xe_pt_write(xe, &vm->pt_root[id]->bo->vmap, 0, entry);
 
 	map_ofs = (num_entries - num_level) * XE_PAGE_SIZE;
@@ -197,7 +198,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 	/* Map the entire BO in our level 0 pt */
 	for (i = 0, level = 0; i < num_entries; level++) {
 		entry = vm->pt_ops->pte_encode_bo(bo, i * XE_PAGE_SIZE,
-						  XE_CACHE_WB, 0);
+						  pat_index, 0);
 
 		xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, entry);
 
@@ -216,7 +217,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 		     i += vm->flags & XE_VM_FLAG_64K ? XE_64K_PAGE_SIZE :
 		     XE_PAGE_SIZE) {
 			entry = vm->pt_ops->pte_encode_bo(batch, i,
-							  XE_CACHE_WB, 0);
+							  pat_index, 0);
 
 			xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64,
 				  entry);
@@ -241,7 +242,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 			flags = XE_PDE_64K;
 
 		entry = vm->pt_ops->pde_encode_bo(bo, map_ofs + (level - 1) *
-						  XE_PAGE_SIZE, XE_CACHE_WB);
+						  XE_PAGE_SIZE, pat_index);
 		xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE * level, u64,
 			  entry | flags);
 	}
@@ -249,7 +250,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 	/* Write PDE's that point to our BO. */
 	for (i = 0; i < num_entries - num_level; i++) {
 		entry = vm->pt_ops->pde_encode_bo(bo, i * XE_PAGE_SIZE,
-						  XE_CACHE_WB);
+						  pat_index);
 
 		xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE +
 			  (i + 1) * 8, u64, entry);
@@ -261,7 +262,7 @@ static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m,
 
 		level = 2;
 		ofs = map_ofs + XE_PAGE_SIZE * level + 256 * 8;
-		flags = vm->pt_ops->pte_encode_addr(xe, 0, XE_CACHE_WB, level,
+		flags = vm->pt_ops->pte_encode_addr(xe, 0, pat_index, level,
 						    true, 0);
 
 		/*

drivers/gpu/drm/xe/xe_migrate.c static void emit_pte(struct xe_migrate *m, · line 461

Quoted 4 lines
drivers/gpu/drm/xe/xe_migrate.c +1 -0
@@ -457,6 +458,7 @@ static void emit_pte(struct xe_migrate *m,
 		     struct xe_res_cursor *cur,
 		     u32 size, struct xe_bo *bo)
 {
+	u16 pat_index = m->tile->xe->pat.idx[XE_CACHE_WB];

NIT...probably use tile_to_xe() instead of tile->xe here and elsewhere just to be consistent?

Quoted 54 lines
drivers/gpu/drm/xe/xe_migrate.c +3 -2
 	u32 ptes;
 	u64 ofs = at_pt * XE_PAGE_SIZE;
 	u64 cur_ofs;
@@ -500,7 +502,7 @@ static void emit_pte(struct xe_migrate *m,
 			}
 
 			addr = m->q->vm->pt_ops->pte_encode_addr(m->tile->xe,
-								 addr, XE_CACHE_WB,
+								 addr, pat_index,
 								 0, devmem, flags);
 			bb->cs[bb->len++] = lower_32_bits(addr);
 			bb->cs[bb->len++] = upper_32_bits(addr);
@@ -1190,6 +1192,7 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 	bool first_munmap_rebind = vma &&
 		vma->gpuva.flags & XE_VMA_FIRST_REBIND;
 	struct xe_exec_queue *q_override = !q ? m->q : q;
+	u16 pat_index = xe->pat.idx[XE_CACHE_WB];
 
 	/* Use the CPU if no in syncs and engine is idle */
 	if (no_in_syncs(syncs, num_syncs) && xe_exec_queue_is_idle(q_override)) {
@@ -1261,7 +1264,7 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 
 			xe_tile_assert(tile, pt_bo->size == SZ_4K);
 
-			addr = vm->pt_ops->pte_encode_bo(pt_bo, 0, XE_CACHE_WB, 0);
+			addr = vm->pt_ops->pte_encode_bo(pt_bo, 0, pat_index, 0);
 			bb->cs[bb->len++] = lower_32_bits(addr);
 			bb->cs[bb->len++] = upper_32_bits(addr);
 		}
drivers/gpu/drm/xe/xe_pt.c +3 -2
index 4d4c6a4c305e..92b512641b4a 100644
@@ -50,6 +50,7 @@ static struct xe_pt *xe_pt_entry(struct xe_pt_dir *pt_dir, unsigned int index)
 static u64 __xe_pt_empty_pte(struct xe_tile *tile, struct xe_vm *vm,
 			     unsigned int level)
 {
+	u16 pat_index = tile_to_xe(tile)->pat.idx[XE_CACHE_WB];
 	u8 id = tile->id;
 
 	if (!vm->scratch_bo[id])
@@ -57,9 +58,9 @@ static u64 __xe_pt_empty_pte(struct xe_tile *tile, struct xe_vm *vm,
 
 	if (level > 0)
 		return vm->pt_ops->pde_encode_bo(vm->scratch_pt[id][level - 1]->bo,
-						 0, XE_CACHE_WB);
+						 0, pat_index);
 
-	return vm->pt_ops->pte_encode_bo(vm->scratch_bo[id], 0, XE_CACHE_WB, 0);
+	return vm->pt_ops->pte_encode_bo(vm->scratch_bo[id], 0, pat_index, 0);
 }
 
 /**

drivers/gpu/drm/xe/xe_pt.c xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset, · line 514

Quoted 4 lines
drivers/gpu/drm/xe/xe_pt.c +1 -0
@@ -510,6 +511,7 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset,
 {
 	struct xe_pt_stage_bind_walk *xe_walk =
 		container_of(walk, typeof(*xe_walk), base);
+	u16 pat_index = tile_to_xe(xe_walk->tile)->pat.idx[xe_walk->cache];

why not change xe_walk->cache to a xe_walk->pat_index?

Niranjana

Quoted 151 lines
drivers/gpu/drm/xe/xe_pt.c +2 -3
 	struct xe_pt *xe_parent = container_of(parent, typeof(*xe_parent), base);
 	struct xe_vm *vm = xe_walk->vm;
 	struct xe_pt *xe_child;
@@ -526,7 +528,7 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset,
 
 		pte = vm->pt_ops->pte_encode_vma(is_null ? 0 :
 						 xe_res_dma(curs) + xe_walk->dma_offset,
-						 xe_walk->vma, xe_walk->cache, level);
+						 xe_walk->vma, pat_index, level);
 		pte |= xe_walk->default_pte;
 
 		/*
@@ -591,8 +593,7 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset,
 			xe_child->is_compact = true;
 		}
 
-		pte = vm->pt_ops->pde_encode_bo(xe_child->bo, 0,
-						xe_walk->cache) | flags;
+		pte = vm->pt_ops->pde_encode_bo(xe_child->bo, 0, pat_index) | flags;
 		ret = xe_pt_insert_entry(xe_walk, xe_parent, offset, xe_child,
 					 pte);
 	}
drivers/gpu/drm/xe/xe_pt_types.h +4 -4
index bd6645295fe6..355fa8f014e9 100644
@@ -38,14 +38,14 @@ struct xe_pt {
 
 struct xe_pt_ops {
 	u64 (*pte_encode_bo)(struct xe_bo *bo, u64 bo_offset,
-			     enum xe_cache_level cache, u32 pt_level);
+			     u16 pat_index, u32 pt_level);
 	u64 (*pte_encode_vma)(u64 pte, struct xe_vma *vma,
-			      enum xe_cache_level cache, u32 pt_level);
+			      u16 pat_index, u32 pt_level);
 	u64 (*pte_encode_addr)(struct xe_device *xe, u64 addr,
-			       enum xe_cache_level cache,
+			       u16 pat_index,
 			       u32 pt_level, bool devmem, u64 flags);
 	u64 (*pde_encode_bo)(struct xe_bo *bo, u64 bo_offset,
-			     const enum xe_cache_level cache);
+			     const u16 pat_index);
 };
 
 struct xe_pt_entry {
drivers/gpu/drm/xe/xe_vm.c +11 -13
index beffbb1039d3..962bfd2b0179 100644
@@ -1191,9 +1191,8 @@ static struct drm_gpuva_fn_ops gpuva_ops = {
 	.op_alloc = xe_vm_op_alloc,
 };
 
-static u64 pde_encode_cache(struct xe_device *xe, enum xe_cache_level cache)
+static u64 pde_encode_pat_index(struct xe_device *xe, u16 pat_index)
 {
-	u32 pat_index = xe->pat.idx[cache];
 	u64 pte = 0;
 
 	if (pat_index & BIT(0))
@@ -1205,9 +1204,8 @@ static u64 pde_encode_cache(struct xe_device *xe, enum xe_cache_level cache)
 	return pte;
 }
 
-static u64 pte_encode_cache(struct xe_device *xe, enum xe_cache_level cache)
+static u64 pte_encode_pat_index(struct xe_device *xe, u16 pat_index)
 {
-	u32 pat_index = xe->pat.idx[cache];
 	u64 pte = 0;
 
 	if (pat_index & BIT(0))
@@ -1238,27 +1236,27 @@ static u64 pte_encode_ps(u32 pt_level)
 }
 
 static u64 xelp_pde_encode_bo(struct xe_bo *bo, u64 bo_offset,
-			      const enum xe_cache_level cache)
+			      const u16 pat_index)
 {
 	struct xe_device *xe = xe_bo_device(bo);
 	u64 pde;
 
 	pde = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
 	pde |= XE_PAGE_PRESENT | XE_PAGE_RW;
-	pde |= pde_encode_cache(xe, cache);
+	pde |= pde_encode_pat_index(xe, pat_index);
 
 	return pde;
 }
 
 static u64 xelp_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
-			      enum xe_cache_level cache, u32 pt_level)
+			      u16 pat_index, u32 pt_level)
 {
 	struct xe_device *xe = xe_bo_device(bo);
 	u64 pte;
 
 	pte = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
 	pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
-	pte |= pte_encode_cache(xe, cache);
+	pte |= pte_encode_pat_index(xe, pat_index);
 	pte |= pte_encode_ps(pt_level);
 
 	if (xe_bo_is_vram(bo) || xe_bo_is_stolen_devmem(bo))
@@ -1268,7 +1266,7 @@ static u64 xelp_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
 }
 
 static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
-			       enum xe_cache_level cache, u32 pt_level)
+			       u16 pat_index, u32 pt_level)
 {
 	struct xe_device *xe = xe_vma_vm(vma)->xe;
 
@@ -1277,7 +1275,7 @@ static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
 	if (likely(!xe_vma_read_only(vma)))
 		pte |= XE_PAGE_RW;
 
-	pte |= pte_encode_cache(xe, cache);
+	pte |= pte_encode_pat_index(xe, pat_index);
 	pte |= pte_encode_ps(pt_level);
 
 	if (unlikely(xe_vma_is_null(vma)))
@@ -1287,7 +1285,7 @@ static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
 }
 
 static u64 xelp_pte_encode_addr(struct xe_device *xe, u64 addr,
-				enum xe_cache_level cache,
+				u16 pat_index,
 				u32 pt_level, bool devmem, u64 flags)
 {
 	u64 pte;
@@ -1297,7 +1295,7 @@ static u64 xelp_pte_encode_addr(struct xe_device *xe, u64 addr,
 
 	pte = addr;
 	pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
-	pte |= pte_encode_cache(xe, cache);
+	pte |= pte_encode_pat_index(xe, pat_index);
 	pte |= pte_encode_ps(pt_level);
 
 	if (devmem)
@@ -1701,7 +1699,7 @@ struct xe_vm *xe_vm_lookup(struct xe_file *xef, u32 id)
 u64 xe_vm_pdp4_descriptor(struct xe_vm *vm, struct xe_tile *tile)
 {
 	return vm->pt_ops->pde_encode_bo(vm->pt_root[tile->id]->bo, 0,
-					 XE_CACHE_WB);
+					 tile->xe->pat.idx[XE_CACHE_WB]);
 }
 

static struct dma_fence *

Signature · 1 line
2.41.0
#4 lore raw

Re: [Intel-xe] [PATCH v4 2/5] drm/xe: directly use pat_index for pte_encode

Quoted 46 lines · Niranjana Vishwanathapura
Quoted 45 lines · Matthew Auld

In the next patch userspace will be able to directly set the pat_index as part of vm_bind. To support this we need to get away from using xe_cache_level in the low level routines and rather just use the pat_index directly.

v2: Rebase

Cc Pallavi Mishra <[email protected]>
Cc Lucas De Marchi <[email protected]>
Cc Matt Roper <[email protected]>

---
drivers/gpu/drm/xe/xe_ggtt.c       |  7 +++---- drivers/gpu/drm/xe/xe_ggtt_types.h |  3 +-- drivers/gpu/drm/xe/xe_migrate.c    | 19 +++++++++++-------- drivers/gpu/drm/xe/xe_pt.c         | 11 ++++++----- drivers/gpu/drm/xe/xe_pt_types.h   |  8 ++++---- drivers/gpu/drm/xe/xe_vm.c         | 24 +++++++++++-------------

6 files changed, 36 insertions(+), 36 deletions(-)
drivers/gpu/drm/xe/xe_ggtt.c +2 -3
index 99b54794917e..2334c47c19cc 100644
@@ -27,7 +27,7 @@
 #define GUC_GGTT_TOP    0xFEE00000
 
 static u64 xelp_ggtt_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
-                   enum xe_cache_level cache)
+                   u16 pat_index)
 {
     u64 pte;
 
@@ -41,13 +41,12 @@ static u64 xelp_ggtt_pte_encode_bo(struct xe_bo 
 *bo, u64 bo_offset,
 }
 
 static u64 xelpg_ggtt_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
-                    enum xe_cache_level cache)
+                    u16 pat_index)
 {
     struct xe_device *xe = xe_bo_device(bo);
-    u32 pat_index = xe->pat.idx[cache];
     u64 pte;
 
Quoted 8 lines

drivers/gpu/drm/xe/xe_ggtt.c static u64 xelp_ggtt_pte_encode_bo(struct xe_bo *bo, u64 bo_offset, · line 51

Quoted 5 lines
drivers/gpu/drm/xe/xe_ggtt.c +1 -1
-    pte = xelp_ggtt_pte_encode_bo(bo, bo_offset, cache);
+    pte = xelp_ggtt_pte_encode_bo(bo, bo_offset, pat_index);
 
     xe_assert(xe, pat_index <= 3);

Looks like this file has couple instances of pte_encode_bo() calls which needs to be updated to use pat_index instead of cache level.

Indeed, I missed a few it seems. Thanks for catching that.

Quoted 90 lines
Quoted 90 lines
drivers/gpu/drm/xe/xe_ggtt_types.h +1 -2
index 486016ea5b67..d8c584d9a8c3 100644
@@ -14,8 +14,7 @@ struct xe_bo;
 struct xe_gt;
 
 struct xe_ggtt_pt_ops {
-    u64 (*pte_encode_bo)(struct xe_bo *bo, u64 bo_offset,
-                 enum xe_cache_level cache);
+    u64 (*pte_encode_bo)(struct xe_bo *bo, u64 bo_offset, u16 
 pat_index);
 };
 

struct xe_ggtt {

drivers/gpu/drm/xe/xe_migrate.c +1 -0
index 258c2269c916..90a1ff1aca9b 100644
@@ -158,6 +158,7 @@ static int xe_migrate_prepare_vm(struct xe_tile 
 *tile, struct xe_migrate *m,
                  struct xe_vm *vm)
 {
     struct xe_device *xe = tile_to_xe(tile);
+    u16 pat_index = xe->pat.idx[XE_CACHE_WB];
     u8 id = tile->id;
     u32 num_entries = NUM_PT_SLOTS, num_level = vm->pt_root[id]->level;

    u32 map_ofs, level, i;

drivers/gpu/drm/xe/xe_migrate.c +1 -1
@@ -189,7 +190,7 @@ static int xe_migrate_prepare_vm(struct xe_tile 
 *tile, struct xe_migrate *m,
         return ret;
     }
 
-    entry = vm->pt_ops->pde_encode_bo(bo, bo->size - XE_PAGE_SIZE, 
 XE_CACHE_WB);
+    entry = vm->pt_ops->pde_encode_bo(bo, bo->size - XE_PAGE_SIZE, 
 pat_index);

    xe_pt_write(xe, &vm->pt_root[id]->bo->vmap, 0, entry);

    map_ofs = (num_entries - num_level) * XE_PAGE_SIZE;

drivers/gpu/drm/xe/xe_migrate.c +1 -1
@@ -197,7 +198,7 @@ static int xe_migrate_prepare_vm(struct xe_tile 
 *tile, struct xe_migrate *m,
     /* Map the entire BO in our level 0 pt */
     for (i = 0, level = 0; i < num_entries; level++) {
         entry = vm->pt_ops->pte_encode_bo(bo, i * XE_PAGE_SIZE,
-                          XE_CACHE_WB, 0);
+                          pat_index, 0);
 
         xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, entry);
@@ -216,7 +217,7 @@ static int xe_migrate_prepare_vm(struct xe_tile 
 *tile, struct xe_migrate *m,

             i += vm->flags & XE_VM_FLAG_64K ? XE_64K_PAGE_SIZE :
             XE_PAGE_SIZE) {
            entry = vm->pt_ops->pte_encode_bo(batch, i, -                              XE_CACHE_WB, 0);
+                              pat_index, 0);

            xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, entry);

drivers/gpu/drm/xe/xe_migrate.c +0 -0
@@ -241,7 +242,7 @@ static int xe_migrate_prepare_vm(struct xe_tile 
 *tile, struct xe_migrate *m,
             flags = XE_PDE_64K;
 

        entry = vm->pt_ops->pde_encode_bo(bo, map_ofs + (level - 1) * -                          XE_PAGE_SIZE, XE_CACHE_WB);
+                          XE_PAGE_SIZE, pat_index); xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE * level, u64, entry | flags);
    }

drivers/gpu/drm/xe/xe_migrate.c +1 -1
@@ -249,7 +250,7 @@ static int xe_migrate_prepare_vm(struct xe_tile 
 *tile, struct xe_migrate *m,
     /* Write PDE's that point to our BO. */
     for (i = 0; i < num_entries - num_level; i++) {
         entry = vm->pt_ops->pde_encode_bo(bo, i * XE_PAGE_SIZE,
-                          XE_CACHE_WB);
+                          pat_index);
 

        xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE + (i + 1) * 8, u64, entry);

drivers/gpu/drm/xe/xe_migrate.c +1 -1
@@ -261,7 +262,7 @@ static int xe_migrate_prepare_vm(struct xe_tile 
 *tile, struct xe_migrate *m,
 
         level = 2;
         ofs = map_ofs + XE_PAGE_SIZE * level + 256 * 8;
-        flags = vm->pt_ops->pte_encode_addr(xe, 0, XE_CACHE_WB, level,
+        flags = vm->pt_ops->pte_encode_addr(xe, 0, pat_index, level,
                             true, 0);
 
Quoted 9 lines

drivers/gpu/drm/xe/xe_migrate.c static void emit_pte(struct xe_migrate *m, · line 461

Quoted 6 lines

        /*

drivers/gpu/drm/xe/xe_migrate.c +1 -0
@@ -457,6 +458,7 @@ static void emit_pte(struct xe_migrate *m,
              struct xe_res_cursor *cur,
              u32 size, struct xe_bo *bo)
 {
+    u16 pat_index = m->tile->xe->pat.idx[XE_CACHE_WB];

NIT...probably use tile_to_xe() instead of tile->xe here and elsewhere just to be consistent?

Ok, will fix.

Quoted 57 lines
Quoted 57 lines
drivers/gpu/drm/xe/xe_migrate.c +1 -1
     u32 ptes;
     u64 ofs = at_pt * XE_PAGE_SIZE;
     u64 cur_ofs;
@@ -500,7 +502,7 @@ static void emit_pte(struct xe_migrate *m,
             }
 
             addr = m->q->vm->pt_ops->pte_encode_addr(m->tile->xe,
-                                 addr, XE_CACHE_WB,
+                                 addr, pat_index,
                                  0, devmem, flags);
             bb->cs[bb->len++] = lower_32_bits(addr);
             bb->cs[bb->len++] = upper_32_bits(addr);
@@ -1190,6 +1192,7 @@ xe_migrate_update_pgtables(struct xe_migrate *m,

    bool first_munmap_rebind = vma && vma->gpuva.flags & XE_VMA_FIRST_REBIND; struct xe_exec_queue *q_override = !q ? m->q : q; +    u16 pat_index = xe->pat.idx[XE_CACHE_WB];

    /* Use the CPU if no in syncs and engine is idle */
    if (no_in_syncs(syncs, num_syncs) && xe_exec_queue_is_idle(q_override)) {

drivers/gpu/drm/xe/xe_migrate.c +1 -1
@@ -1261,7 +1264,7 @@ xe_migrate_update_pgtables(struct xe_migrate *m,
 
             xe_tile_assert(tile, pt_bo->size == SZ_4K);
 
-            addr = vm->pt_ops->pte_encode_bo(pt_bo, 0, XE_CACHE_WB, 0);
+            addr = vm->pt_ops->pte_encode_bo(pt_bo, 0, pat_index, 0);
             bb->cs[bb->len++] = lower_32_bits(addr);
             bb->cs[bb->len++] = upper_32_bits(addr);
         }
drivers/gpu/drm/xe/xe_pt.c +1 -0
index 4d4c6a4c305e..92b512641b4a 100644
@@ -50,6 +50,7 @@ static struct xe_pt *xe_pt_entry(struct xe_pt_dir 
 *pt_dir, unsigned int index)
 static u64 __xe_pt_empty_pte(struct xe_tile *tile, struct xe_vm *vm,
                  unsigned int level)
 {
+    u16 pat_index = tile_to_xe(tile)->pat.idx[XE_CACHE_WB];
     u8 id = tile->id;
 

    if (!vm->scratch_bo[id])

drivers/gpu/drm/xe/xe_pt.c +2 -2
@@ -57,9 +58,9 @@ static u64 __xe_pt_empty_pte(struct xe_tile *tile, 
 struct xe_vm *vm,
 
     if (level > 0)
         return vm->pt_ops->pde_encode_bo(vm->scratch_pt[id][level - 1]->bo,
-                         0, XE_CACHE_WB);
+                         0, pat_index);
 
-    return vm->pt_ops->pte_encode_bo(vm->scratch_bo[id], 0, 
 XE_CACHE_WB, 0);
+    return vm->pt_ops->pte_encode_bo(vm->scratch_bo[id], 0, 
 pat_index, 0);

}

Quoted 9 lines
Quoted 7 lines

/**

drivers/gpu/drm/xe/xe_pt.c +0 -0
@@ -510,6 +511,7 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, 
 pgoff_t offset,
 {

    struct xe_pt_stage_bind_walk *xe_walk = container_of(walk, typeof(*xe_walk), base);
+    u16 pat_index = tile_to_xe(xe_walk->tile)->pat.idx[xe_walk->cache];

why not change xe_walk->cache to a xe_walk->pat_index?

In the later vm_bind patch the xe_walk->[cache, pat_index] is anyway removed and instead extracted directly from the vma, so figured there was not much point. The above line just becomes xe_walk->vma->pat_index.

Quoted 165 lines

Niranjana

Quoted 162 lines

    struct xe_pt *xe_parent = container_of(parent, typeof(*xe_parent), base); struct xe_vm *vm = xe_walk->vm;
    struct xe_pt *xe_child;

drivers/gpu/drm/xe/xe_pt.c +0 -0
@@ -526,7 +528,7 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, 
 pgoff_t offset,
 

        pte = vm->pt_ops->pte_encode_vma(is_null ? 0 :
                         xe_res_dma(curs) + xe_walk->dma_offset, -                         xe_walk->vma, xe_walk->cache, level); +                         xe_walk->vma, pat_index, level); pte |= xe_walk->default_pte;

        /*

drivers/gpu/drm/xe/xe_pt.c +1 -2
@@ -591,8 +593,7 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, 
 pgoff_t offset,
             xe_child->is_compact = true;
         }
 
-        pte = vm->pt_ops->pde_encode_bo(xe_child->bo, 0,
-                        xe_walk->cache) | flags;
+        pte = vm->pt_ops->pde_encode_bo(xe_child->bo, 0, pat_index) | 
 flags;
         ret = xe_pt_insert_entry(xe_walk, xe_parent, offset, xe_child,

                     pte);
    }

drivers/gpu/drm/xe/xe_pt_types.h +4 -4
index bd6645295fe6..355fa8f014e9 100644
@@ -38,14 +38,14 @@ struct xe_pt {
 
 struct xe_pt_ops {
     u64 (*pte_encode_bo)(struct xe_bo *bo, u64 bo_offset,
-                 enum xe_cache_level cache, u32 pt_level);
+                 u16 pat_index, u32 pt_level);
     u64 (*pte_encode_vma)(u64 pte, struct xe_vma *vma,
-                  enum xe_cache_level cache, u32 pt_level);
+                  u16 pat_index, u32 pt_level);
     u64 (*pte_encode_addr)(struct xe_device *xe, u64 addr,
-                   enum xe_cache_level cache,
+                   u16 pat_index,
                    u32 pt_level, bool devmem, u64 flags);
     u64 (*pde_encode_bo)(struct xe_bo *bo, u64 bo_offset,
-                 const enum xe_cache_level cache);
+                 const u16 pat_index);
 };
 
 struct xe_pt_entry {
drivers/gpu/drm/xe/xe_vm.c +1 -2
index beffbb1039d3..962bfd2b0179 100644
@@ -1191,9 +1191,8 @@ static struct drm_gpuva_fn_ops gpuva_ops = {
     .op_alloc = xe_vm_op_alloc,
 };
 
-static u64 pde_encode_cache(struct xe_device *xe, enum xe_cache_level 
 cache)
+static u64 pde_encode_pat_index(struct xe_device *xe, u16 pat_index)
 {
-    u32 pat_index = xe->pat.idx[cache];
     u64 pte = 0;
 

    if (pat_index & BIT(0))

drivers/gpu/drm/xe/xe_vm.c +1 -2
@@ -1205,9 +1204,8 @@ static u64 pde_encode_cache(struct xe_device 
 *xe, enum xe_cache_level cache)
     return pte;
 }
 
-static u64 pte_encode_cache(struct xe_device *xe, enum xe_cache_level 
 cache)
+static u64 pte_encode_pat_index(struct xe_device *xe, u16 pat_index)
 {
-    u32 pat_index = xe->pat.idx[cache];
     u64 pte = 0;

    if (pat_index & BIT(0))

drivers/gpu/drm/xe/xe_vm.c +6 -6
@@ -1238,27 +1236,27 @@ static u64 pte_encode_ps(u32 pt_level)
 }
 
 static u64 xelp_pde_encode_bo(struct xe_bo *bo, u64 bo_offset,
-                  const enum xe_cache_level cache)
+                  const u16 pat_index)
 {
     struct xe_device *xe = xe_bo_device(bo);
     u64 pde;
 
     pde = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
     pde |= XE_PAGE_PRESENT | XE_PAGE_RW;
-    pde |= pde_encode_cache(xe, cache);
+    pde |= pde_encode_pat_index(xe, pat_index);
 
     return pde;
 }
 
 static u64 xelp_pte_encode_bo(struct xe_bo *bo, u64 bo_offset,
-                  enum xe_cache_level cache, u32 pt_level)
+                  u16 pat_index, u32 pt_level)
 {
     struct xe_device *xe = xe_bo_device(bo);
     u64 pte;
 
     pte = xe_bo_addr(bo, bo_offset, XE_PAGE_SIZE);
     pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
-    pte |= pte_encode_cache(xe, cache);
+    pte |= pte_encode_pat_index(xe, pat_index);
     pte |= pte_encode_ps(pt_level);
 
     if (xe_bo_is_vram(bo) || xe_bo_is_stolen_devmem(bo))
@@ -1268,7 +1266,7 @@ static u64 xelp_pte_encode_bo(struct xe_bo *bo, 
 u64 bo_offset,
 }
 
 static u64 xelp_pte_encode_vma(u64 pte, struct xe_vma *vma,
-                   enum xe_cache_level cache, u32 pt_level)
+                   u16 pat_index, u32 pt_level)
 {
     struct xe_device *xe = xe_vma_vm(vma)->xe;
@@ -1277,7 +1275,7 @@ static u64 xelp_pte_encode_vma(u64 pte, struct 
 xe_vma *vma,
     if (likely(!xe_vma_read_only(vma)))
         pte |= XE_PAGE_RW;
 
-    pte |= pte_encode_cache(xe, cache);
+    pte |= pte_encode_pat_index(xe, pat_index);
     pte |= pte_encode_ps(pt_level);
 

    if (unlikely(xe_vma_is_null(vma)))

drivers/gpu/drm/xe/xe_vm.c +1 -1
@@ -1287,7 +1285,7 @@ static u64 xelp_pte_encode_vma(u64 pte, struct 
 xe_vma *vma,
 }
 
 static u64 xelp_pte_encode_addr(struct xe_device *xe, u64 addr,
-                enum xe_cache_level cache,
+                u16 pat_index,
                 u32 pt_level, bool devmem, u64 flags)
 {

    u64 pte;

drivers/gpu/drm/xe/xe_vm.c +1 -1
@@ -1297,7 +1295,7 @@ static u64 xelp_pte_encode_addr(struct xe_device 
 *xe, u64 addr,
 
     pte = addr;
     pte |= XE_PAGE_PRESENT | XE_PAGE_RW;
-    pte |= pte_encode_cache(xe, cache);
+    pte |= pte_encode_pat_index(xe, pat_index);
     pte |= pte_encode_ps(pt_level);
 

    if (devmem)

drivers/gpu/drm/xe/xe_vm.c +1 -1
@@ -1701,7 +1699,7 @@ struct xe_vm *xe_vm_lookup(struct xe_file *xef, 
 u32 id)
 u64 xe_vm_pdp4_descriptor(struct xe_vm *vm, struct xe_tile *tile)
 {
     return vm->pt_ops->pde_encode_bo(vm->pt_root[tile->id]->bo, 0,
-                     XE_CACHE_WB);
+                     tile->xe->pat.idx[XE_CACHE_WB]);
 }
 

static struct dma_fence *

Signature · 1 line
2.41.0
#5 lore raw

PATCH v4 3/5 drm/xe/uapi: Add support for cache and coherency mode

In reply to Matthew Auld

From: Pallavi Mishra <[email protected]>

Allow userspace to specify the CPU caching mode to use in addition to coherency modes during object creation. Modify gem create handler and introduce xe_bo_create_user to replace xe_bo_create. In a later patch we will support setting the pat_index as part of vm_bind, where expectation is that the coherency mode extracted from the pat_index must match the one set at object creation.

v2
- s/smem_caching/smem_cpu_caching/ and s/XE_GEM_CACHING/XE_GEM_CPU_CACHING/. (Matt Roper)
- Drop COH_2WAY and just use COH_NONE + COH_AT_LEAST_1WAY; KMD mostly just cares that zeroing/swap-in can't be bypassed with the given smem_caching mode. (Matt Roper)
- Fix broken range check for coh_mode and smem_cpu_caching and also don't use constant value, but the already defined macros. (José)
- Prefer switch statement for smem_cpu_caching -> ttm_caching. (José)
- Add note in kernel-doc for dgpu and coherency modes for system memory. (José)
v3 (José):
- Make sure to reject coh_mode == 0 for VRAM-only.
- Also make sure to actually pass along the (start, end) for

    __xe_bo_create_locked.

v4
- Drop UC caching mode. Can be added back if we need it. (Matt Roper)
- s/smem_cpu_caching/cpu_caching. Idea is that VRAM is always WC, but that is currently implicit and KMD controlled. Make it explicit in the uapi with the limitation that it currently must be WC. For VRAM
+ SYS objects userspace must now select WC. (José)
- Make sure to initialize bo_flags. (José)

Cc Thomas Hellström <[email protected]>
Cc Joonas Lahtinen <[email protected]>
Cc Lucas De Marchi <[email protected]>
Cc Matt Roper <[email protected]>
Cc José Roberto de Souza <[email protected]>
Cc Filip Hazubski <[email protected]>
Cc Carl Zhang <[email protected]>
Cc Effie Yu <[email protected]>
---
 drivers/gpu/drm/xe/xe_bo.c       | 97 ++++++++++++++++++++++++++------
 drivers/gpu/drm/xe/xe_bo.h       |  3 +-
 drivers/gpu/drm/xe/xe_bo_types.h | 10 ++++
 drivers/gpu/drm/xe/xe_dma_buf.c  |  5 +-
 include/uapi/drm/xe_drm.h        | 50 +++++++++++++++-
 5 files changed, 143 insertions(+), 22 deletions(-)
drivers/gpu/drm/xe/xe_bo.c +80 -17
index 61789c0e88fb..0de463907428 100644
@@ -326,7 +326,7 @@ static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
 	struct xe_device *xe = xe_bo_device(bo);
 	struct xe_ttm_tt *tt;
 	unsigned long extra_pages;
-	enum ttm_caching caching = ttm_cached;
+	enum ttm_caching caching;
 	int err;
 
 	tt = kzalloc(sizeof(*tt), GFP_KERNEL);
@@ -340,13 +340,22 @@ static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
 		extra_pages = DIV_ROUND_UP(xe_device_ccs_bytes(xe, bo->size),
 					   PAGE_SIZE);
 
+	switch (bo->cpu_caching) {
+	case XE_GEM_CPU_CACHING_WC:
+		caching = ttm_write_combined;
+		break;
+	default:
+		caching = ttm_cached;
+		break;
+	}
+
 	/*
 	 * Display scanout is always non-coherent with the CPU cache.
 	 *
 	 * For Xe_LPG and beyond, PPGTT PTE lookups are also non-coherent and
 	 * require a CPU:WC mapping.
 	 */
-	if (bo->flags & XE_BO_SCANOUT_BIT ||
+	if ((!bo->cpu_caching && bo->flags & XE_BO_SCANOUT_BIT) ||
 	    (xe->info.graphics_verx100 >= 1270 && bo->flags & XE_BO_PAGETABLE))
 		caching = ttm_write_combined;
 
@@ -1190,9 +1199,10 @@ void xe_bo_free(struct xe_bo *bo)
 	kfree(bo);
 }
 
-struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
+struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 				    struct xe_tile *tile, struct dma_resv *resv,
 				    struct ttm_lru_bulk_move *bulk, size_t size,
+				    u16 cpu_caching, u16 coh_mode,
 				    enum ttm_bo_type type, u32 flags)
 {
 	struct ttm_operation_ctx ctx = {
@@ -1231,6 +1241,8 @@ struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 	bo->tile = tile;
 	bo->size = size;
 	bo->flags = flags;
+	bo->cpu_caching = cpu_caching;
+	bo->coh_mode = coh_mode;
 	bo->ttm.base.funcs = &xe_gem_object_funcs;
 	bo->props.preferred_mem_class = XE_BO_PROPS_INVALID;
 	bo->props.preferred_gt = XE_BO_PROPS_INVALID;
@@ -1316,10 +1328,11 @@ static int __xe_bo_fixed_placement(struct xe_device *xe,
 }
 
 struct xe_bo *
-xe_bo_create_locked_range(struct xe_device *xe,
-			  struct xe_tile *tile, struct xe_vm *vm,
-			  size_t size, u64 start, u64 end,
-			  enum ttm_bo_type type, u32 flags)
+__xe_bo_create_locked(struct xe_device *xe,
+		      struct xe_tile *tile, struct xe_vm *vm,
+		      size_t size, u64 start, u64 end,
+		      u16 cpu_caching, u16 coh_mode,
+		      enum ttm_bo_type type, u32 flags)
 {
 	struct xe_bo *bo = NULL;
 	int err;
@@ -1340,10 +1353,11 @@ xe_bo_create_locked_range(struct xe_device *xe,
 		}
 	}
 
-	bo = __xe_bo_create_locked(xe, bo, tile, vm ? &vm->resv : NULL,
+	bo = ___xe_bo_create_locked(xe, bo, tile, vm ? &vm->resv : NULL,
 				   vm && !xe_vm_in_fault_mode(vm) &&
 				   flags & XE_BO_CREATE_USER_BIT ?
 				   &vm->lru_bulk_move : NULL, size,
+				   cpu_caching, coh_mode,
 				   type, flags);
 	if (IS_ERR(bo))
 		return bo;
@@ -1377,11 +1391,35 @@ xe_bo_create_locked_range(struct xe_device *xe,
 	return ERR_PTR(err);
 }
 
+struct xe_bo *
+xe_bo_create_locked_range(struct xe_device *xe,
+			  struct xe_tile *tile, struct xe_vm *vm,
+			  size_t size, u64 start, u64 end,
+			  enum ttm_bo_type type, u32 flags)
+{
+	return __xe_bo_create_locked(xe, tile, vm, size, start, end, 0, 0, type, flags);
+}
+
 struct xe_bo *xe_bo_create_locked(struct xe_device *xe, struct xe_tile *tile,
 				  struct xe_vm *vm, size_t size,
 				  enum ttm_bo_type type, u32 flags)
 {
-	return xe_bo_create_locked_range(xe, tile, vm, size, 0, ~0ULL, type, flags);
+	return __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL, 0, 0, type, flags);
+}
+
+static struct xe_bo *xe_bo_create_user(struct xe_device *xe, struct xe_tile *tile,
+				       struct xe_vm *vm, size_t size,
+				       u16 cpu_caching, u16 coh_mode,
+				       enum ttm_bo_type type,
+				       u32 flags)
+{
+	struct xe_bo *bo = __xe_bo_create_locked(xe, tile, vm, size, 0, ~0ULL,
+						 cpu_caching, coh_mode, type,
+						 flags | XE_BO_CREATE_USER_BIT);
+	if (!IS_ERR(bo))
+		xe_bo_unlock_vm_held(bo);
+
+	return bo;
 }
 
 struct xe_bo *xe_bo_create(struct xe_device *xe, struct xe_tile *tile,
@@ -1764,11 +1802,11 @@ int xe_gem_create_ioctl(struct drm_device *dev, void *data,
 	struct drm_xe_gem_create *args = data;
 	struct xe_vm *vm = NULL;
 	struct xe_bo *bo;
-	unsigned int bo_flags = XE_BO_CREATE_USER_BIT;
+	unsigned int bo_flags;
 	u32 handle;
 	int err;
 
-	if (XE_IOCTL_DBG(xe, args->extensions) || XE_IOCTL_DBG(xe, args->pad) ||
+	if (XE_IOCTL_DBG(xe, args->extensions) ||
 	    XE_IOCTL_DBG(xe, args->reserved[0] || args->reserved[1]))
 		return -EINVAL;
 
@@ -1795,6 +1833,7 @@ int xe_gem_create_ioctl(struct drm_device *dev, void *data,
 	if (XE_IOCTL_DBG(xe, args->size & ~PAGE_MASK))
 		return -EINVAL;
 
+	bo_flags = 0;
 	if (args->flags & XE_GEM_CREATE_FLAG_DEFER_BACKING)
 		bo_flags |= XE_BO_DEFER_BACKING;
 
@@ -1810,6 +1849,26 @@ int xe_gem_create_ioctl(struct drm_device *dev, void *data,
 		bo_flags |= XE_BO_NEEDS_CPU_ACCESS;
 	}
 
+	if (XE_IOCTL_DBG(xe, !args->coh_mode ||
+			 args->coh_mode > XE_GEM_COH_AT_LEAST_1WAY))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, !args->cpu_caching ||
+			 args->cpu_caching > XE_GEM_CPU_CACHING_WC))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_CREATE_VRAM_MASK &&
+			 args->cpu_caching != XE_GEM_CPU_CACHING_WC))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, bo_flags & XE_BO_SCANOUT_BIT &&
+			 args->cpu_caching == XE_GEM_CPU_CACHING_WB))
+		return -EINVAL;
+
+	if (XE_IOCTL_DBG(xe, args->coh_mode == XE_GEM_COH_NONE &&
+			 args->cpu_caching == XE_GEM_CPU_CACHING_WB))
+		return -EINVAL;
+
 	if (args->vm_id) {
 		vm = xe_vm_lookup(xef, args->vm_id);
 		if (XE_IOCTL_DBG(xe, !vm))
@@ -1821,8 +1880,10 @@ int xe_gem_create_ioctl(struct drm_device *dev, void *data,
 		}
 	}
 
-	bo = xe_bo_create(xe, NULL, vm, args->size, ttm_bo_type_device,
-			  bo_flags);
+	bo = xe_bo_create_user(xe, NULL, vm, args->size,
+			       args->cpu_caching, args->coh_mode,
+			       ttm_bo_type_device,
+			       bo_flags);
 	if (IS_ERR(bo)) {
 		err = PTR_ERR(bo);
 		goto out_vm;
@@ -2114,10 +2175,12 @@ int xe_bo_dumb_create(struct drm_file *file_priv,
 	args->size = ALIGN(mul_u32_u32(args->pitch, args->height),
 			   page_size);
 
-	bo = xe_bo_create(xe, NULL, NULL, args->size, ttm_bo_type_device,
-			  XE_BO_CREATE_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |
-			  XE_BO_CREATE_USER_BIT | XE_BO_SCANOUT_BIT |
-			  XE_BO_NEEDS_CPU_ACCESS);
+	bo = xe_bo_create_user(xe, NULL, NULL, args->size,
+			       XE_GEM_CPU_CACHING_WC, XE_GEM_COH_NONE,
+			       ttm_bo_type_device,
+			       XE_BO_CREATE_VRAM_IF_DGFX(xe_device_get_root_tile(xe)) |
+			       XE_BO_CREATE_USER_BIT | XE_BO_SCANOUT_BIT |
+			       XE_BO_NEEDS_CPU_ACCESS);
 	if (IS_ERR(bo))
 		return PTR_ERR(bo);
 
drivers/gpu/drm/xe/xe_bo.h +2 -1
index 5090bdd1e462..eccfa7d187f8 100644
@@ -83,9 +83,10 @@ struct sg_table;
 struct xe_bo *xe_bo_alloc(void);
 void xe_bo_free(struct xe_bo *bo);
 
-struct xe_bo *__xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
+struct xe_bo *___xe_bo_create_locked(struct xe_device *xe, struct xe_bo *bo,
 				    struct xe_tile *tile, struct dma_resv *resv,
 				    struct ttm_lru_bulk_move *bulk, size_t size,
+				    u16 cpu_caching, u16 coh_mode,
 				    enum ttm_bo_type type, u32 flags);
 struct xe_bo *
 xe_bo_create_locked_range(struct xe_device *xe,
drivers/gpu/drm/xe/xe_bo_types.h +10 -0
index 051fe990c133..56f7f9a4975f 100644
@@ -76,6 +76,16 @@ struct xe_bo {
 	struct llist_node freed;
 	/** @created: Whether the bo has passed initial creation */
 	bool created;
+	/**
+	 * @coh_mode: Coherency setting. Currently only used for userspace
+	 * objects.
+	 */
+	u16 coh_mode;
+	/**
+	 * @cpu_caching: CPU caching mode. Currently only used for userspace
+	 * objects.
+	 */
+	u16 cpu_caching;
 };
 
 #define intel_bo_to_drm_bo(bo) (&(bo)->ttm.base)
drivers/gpu/drm/xe/xe_dma_buf.c +3 -2
index cfde3be3b0dc..9da5cffeef13 100644
@@ -214,8 +214,9 @@ xe_dma_buf_init_obj(struct drm_device *dev, struct xe_bo *storage,
 	int ret;
 
 	dma_resv_lock(resv, NULL);
-	bo = __xe_bo_create_locked(xe, storage, NULL, resv, NULL, dma_buf->size,
-				   ttm_bo_type_sg, XE_BO_CREATE_SYSTEM_BIT);
+	bo = ___xe_bo_create_locked(xe, storage, NULL, resv, NULL, dma_buf->size,
+				    0, 0, /* Will require 1way or 2way for vm_bind */
+				    ttm_bo_type_sg, XE_BO_CREATE_SYSTEM_BIT);
 	if (IS_ERR(bo)) {
 		ret = PTR_ERR(bo);
 		goto error;
include/uapi/drm/xe_drm.h +48 -2

56 lines not on this page · raw message

Signature · 1 line
2.41.0
#6 lore raw

PATCH v4 4/5 drm/xe/pat: annotate pat_index with coherency mode

In reply to Matthew Auld

Future uapi needs to give userspace the ability to select the pat_index for a given vm_bind. However we need to be able to extract the coherency mode from the provided pat_index to ensure it matches the coherency mode set at object creation. There are various security reasons for why this matters. However the pat_index itself is very platform specific, so seems reasonable to annotate each platform definition of the pat table. On some older platforms there is no explicit coherency mode, so we just pick whatever makes sense.

v2:
- Simplify with COH_AT_LEAST_1_WAY
- Add some kernel-doc
v3 (Matt Roper):
- Some small tweaks
v4:
- Rebase

Bspec: 45101, 44235 #xe
Bspec: 70552, 71582, 59400 #xe2

Cc Pallavi Mishra <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Joonas Lahtinen <[email protected]>
Cc Lucas De Marchi <[email protected]>
Cc Matt Roper <[email protected]>
Cc José Roberto de Souza <[email protected]>
Cc Filip Hazubski <[email protected]>
Cc Carl Zhang <[email protected]>
Cc Effie Yu <[email protected]>
---
 drivers/gpu/drm/xe/xe_device_types.h |  2 +-
 drivers/gpu/drm/xe/xe_pat.c          | 62 ++++++++++++++++------------
 drivers/gpu/drm/xe/xe_pat.h          | 28 +++++++++++++
 3 files changed, 65 insertions(+), 27 deletions(-)
drivers/gpu/drm/xe/xe_device_types.h +1 -1
index 7d0f2109c23a..18af9e29f42f 100644
@@ -340,7 +340,7 @@ struct xe_device {
 		/** Internal operations to abstract platforms */
 		const struct xe_pat_ops *ops;
 		/** PAT table to program in the HW */
-		const u32 *table;
+		const struct xe_pat_table_entry *table;
 		/** Number of PAT entries */
 		int n_entries;
 		u32 idx[__XE_CACHE_LEVEL_COUNT];
drivers/gpu/drm/xe/xe_pat.c +36 -26

99 lines not on this page · raw message

drivers/gpu/drm/xe/xe_pat.h +28 -0
index 744318cab69b..4032c0ef975c 100644
@@ -6,9 +6,29 @@
 #ifndef _XE_PAT_H_
 #define _XE_PAT_H_
 
+#include <linux/types.h>
+
 struct xe_gt;
 struct xe_device;
 
+/**
+ * struct xe_pat_table_entry - The pat_index encoding and other meta information.
+ */
+struct xe_pat_table_entry {
+	/**
+	 * @value: The platform specific value encoding the various memory
+	 * attributes (this maps to some fixed pat_index). So things like
+	 * caching, coherency, compression etc can be encoded here.
+	 */
+	u32 value;
+
+	/**
+	 * @coh_mode: The GPU coherency mode that @value maps to. Either
+	 * XE_GEM_COH_NONE or XE_GEM_COH_AT_LEAST_1WAY.
+	 */
+	u16 coh_mode;
+};
+
 /**
  * xe_pat_init_early - SW initialization, setting up data based on device
  * @xe: xe device
@@ -21,4 +41,12 @@ void xe_pat_init_early(struct xe_device *xe);
  */
 void xe_pat_init(struct xe_gt *gt);
 
+/**
+ * xe_pat_index_get_coh_mode - Extract the coherency mode for the given
+ * pat_index.
+ * @xe: xe device
+ * @pat_index: The pat_index to query
+ */
+u16 xe_pat_index_get_coh_mode(struct xe_device *xe, u16 pat_index);
+
 #endif
Signature · 1 line
2.41.0
#7 lore raw

PATCH v4 5/5 drm/xe/uapi: support pat_index selection with vm_bind

In reply to Matthew Auld

Allow userspace to directly control the pat_index for a given vm binding. This should allow directly controlling the coherency, caching and potentially other stuff in the future for the ppGTT binding.

The exact meaning behind the pat_index is very platform specific (see BSpec or PRMs) but effectively maps to some predefined memory attributes. From the KMD pov we only care about the coherency that is provided by the pat_index, which falls into either NONE, 1WAY or 2WAY. The vm_bind coherency mode for the given pat_index needs to be at least as coherent as the coh_mode that was set at object creation. For platforms that lack the explicit coherency mode, we treat UC/WT/WC as NONE and WB as AT_LEAST_1WAY.

For userptr mappings we lack a corresponding gem object, so the expected coherency mode is instead implicit and must fall into either 1WAY or 2WAY. Trying to use NONE will be rejected by the kernel. For imported dma-buf (from a different device) the coherency mode is also implicit and must also be either 1WAY or 2WAY i.e AT_LEAST_1WAY.

v2:
- Undefined coh_mode(pat_index) can now be treated as programmer error. (Matt Roper)
- We now allow gem_create.coh_mode <= coh_mode(pat_index), rather than having to match exactly. This ensures imported dma-buf can always just use 1way (or even 2way), now that we also bundle 1way/2way into at_least_1way. We still require 1way/2way for external dma-buf, but the policy can now be the same for self-import, if desired.
- Use u16 for pat_index in uapi. u32 is massive overkill. (José)
- Move as much of the pat_index validation as we can into vm_bind_ioctl_check_args. (José)
v3 (Matt Roper):
- Split the pte_encode() refactoring into separate patch.
v4:
- Rebase

Bspec: 45101, 44235 #xe
Bspec: 70552, 71582, 59400 #xe2

Cc Pallavi Mishra <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Joonas Lahtinen <[email protected]>
Cc Lucas De Marchi <[email protected]>
Cc Matt Roper <[email protected]>
Cc José Roberto de Souza <[email protected]>
Cc Filip Hazubski <[email protected]>
Cc Carl Zhang <[email protected]>
Cc Effie Yu <[email protected]>
---
 drivers/gpu/drm/xe/xe_pt.c       | 11 ++----
 drivers/gpu/drm/xe/xe_vm.c       | 61 +++++++++++++++++++++++++++-----
 drivers/gpu/drm/xe/xe_vm_types.h |  7 ++++
 include/uapi/drm/xe_drm.h        | 43 +++++++++++++++++++++-
 4 files changed, 104 insertions(+), 18 deletions(-)
drivers/gpu/drm/xe/xe_pt.c +2 -9
index 92b512641b4a..f9f9010dca10 100644
@@ -290,8 +290,6 @@ struct xe_pt_stage_bind_walk {
 	struct xe_vm *vm;
 	/** @tile: The tile we're building for. */
 	struct xe_tile *tile;
-	/** @cache: Desired cache level for the ptes */
-	enum xe_cache_level cache;
 	/** @default_pte: PTE flag only template. No address is associated */
 	u64 default_pte;
 	/** @dma_offset: DMA offset to add to the PTE. */
@@ -511,7 +509,7 @@ xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset,
 {
 	struct xe_pt_stage_bind_walk *xe_walk =
 		container_of(walk, typeof(*xe_walk), base);
-	u16 pat_index = tile_to_xe(xe_walk->tile)->pat.idx[xe_walk->cache];
+	u16 pat_index = xe_walk->vma->pat_index;
 	struct xe_pt *xe_parent = container_of(parent, typeof(*xe_parent), base);
 	struct xe_vm *vm = xe_walk->vm;
 	struct xe_pt *xe_child;
@@ -654,13 +652,8 @@ xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma,
 		if (vma && vma->gpuva.flags & XE_VMA_ATOMIC_PTE_BIT)
 			xe_walk.default_pte |= XE_USM_PPGTT_PTE_AE;
 		xe_walk.dma_offset = vram_region_gpu_offset(bo->ttm.resource);
-		xe_walk.cache = XE_CACHE_WB;
-	} else {
-		if (!xe_vma_has_no_bo(vma) && bo->flags & XE_BO_SCANOUT_BIT)
-			xe_walk.cache = XE_CACHE_WT;
-		else
-			xe_walk.cache = XE_CACHE_WB;
 	}
+
 	if (!xe_vma_has_no_bo(vma) && xe_bo_is_stolen(bo))
 		xe_walk.dma_offset = xe_ttm_stolen_gpu_offset(xe_bo_device(bo));
 
drivers/gpu/drm/xe/xe_vm.c +53 -8

167 lines not on this page · raw message

drivers/gpu/drm/xe/xe_vm_types.h +7 -0
index 1c5553b842d7..692e1cecb64f 100644
@@ -111,6 +111,11 @@ struct xe_vma {
 	 */
 	u8 tile_present;
 
+	/**
+	 * @pat_index: The pat index to use when encoding the PTEs for this vma.
+	 */
+	u16 pat_index;
+
 	struct {
 		struct list_head rebind_link;
 	} notifier;
@@ -418,6 +423,8 @@ struct xe_vma_op {
 	struct async_op_fence *fence;
 	/** @tile_mask: gt mask for this operation */
 	u8 tile_mask;
+	/** @pat_index: The pat index to use for this operation. */
+	u16 pat_index;
 	/** @flags: operation flags */
 	enum xe_vma_op_flags flags;
 
include/uapi/drm/xe_drm.h +42 -1

50 lines not on this page · raw message

Signature · 1 line
2.41.0
#8 lore raw

[Intel-xe] ✗ CI.Patch_applied: failure for PAT and cache coherency support (rev5)

In reply to Matthew Auld
== Series Details ==

== Series Details ==

Series: PAT and cache coherency support (rev5)
URL : https://patchwork.freedesktop.org/series/123027/
State : failure

== Summary ==

=== Applying kernel patches on branch 'drm-xe-next' with base: === Base commit: fc8ec3c56 drm/xe: Add Wa_18028616096
=== git am output follows ===
error: patch failed: drivers/gpu/drm/xe/xe_pat.c:42
error: drivers/gpu/drm/xe/xe_pat.c: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch
Applying: drm/xe/pat: trim the xelp PAT table
Patch failed at 0001 drm/xe/pat: trim the xelp PAT table When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".

#9 lore raw
In reply to Matthew Auld
Quoted 21 lines · Matthew Auld

Branch available here:
https://gitlab.freedesktop.org/mwa/kernel/-/tree/xe-pat-index?ref_type=heads

Series directly depends on the patches here:
https://patchwork.freedesktop.org/series/124225/

Goal here is to allow userspace to directly control the pat_index when mapping memory via the ppGTT, in addtion to the CPU caching mode. This is very much needed on newer igpu platforms which allow incoherent GT access, where the choice over the cache level and expected coherency is best left to userspace depending on their usecase. In the future there may also be other stuff encoded in the pat_index, so giving userspace direct control will also be needed there.

To support this we added new gem_create uAPI for selecting the CPU cache mode to use for system memory, including the expected GPU coherency mode. There are various restrictions here for the selected coherency mode and compatible CPU cache modes. With that in place the actual pat_index can now be provided as part of vm_bind. The only restriction is that the coherency mode of the pat_index must be at least as coherent as the gem_create coherency mode. There are also some special cases like with userptr and dma-buf.

Quoted 14 lines

v2:
- Loads of improvements/tweaks. Main changes are to now allow gem_create.coh_mode <= coh_mode(pat_index), rather than it needing to match exactly. This simplifies the dma-buf policy from userspace pov. Also we now only consider COH_NONE and COH_AT_LEAST_1WAY.
v3:
- Rebase. Split the pte_encode() refactoring, plus various smaller tweaks and fixes.
v4:
- Rebase on Lucas' new series.
- Drop UC cache mode.
- s/smem_cpu_caching/cpu_caching/. Idea is to make VRAM WC explicit in the uapi, plus make it more future proof.

Thanks for the smem_cpu_caching to cpu_caching change.

This latest version is causing a GuC fw load failure in MTL, I have bisected and it is caused by "drm/xe: directly use pat_index for pte_encode".

[  173.995308] xe 0000:00:02.0: [drm:xe_guc_init [xe]] GuC param[12] = 0x00000000
[  173.995388] xe 0000:00:02.0: [drm:xe_guc_init [xe]] GuC param[13] = 0x00000000
[  173.995467] xe 0000:00:02.0: [drm:xe_wopcm_init [xe]] WOPCM: 4096K
[  173.995609] xe 0000:00:02.0: [drm:xe_wopcm_init [xe]] GuC WOPCM is already locked [2048K, 832K)
[  174.234667] xe 0000:00:02.0: [drm] GuC load failed: status = 0x80007134
[  174.234681] xe 0000:00:02.0: [drm] GuC load failed: status: Reset = 0, BootROM = 0x1A, UKernel = 0x71, MIA = 0x00, Auth = 0x02
[  174.234690] xe 0000:00:02.0: [drm] 0xcabba9e6 0xdeadfeed 0x00000000 0x00000078
[  174.234697] xe 0000:00:02.0: [drm] 0x00010000 0x00000000 0x0000fff0 0x00000000
[  174.234703] xe 0000:00:02.0: [drm] 0x00000002 0xcabba9e6 0x8086dead 0x00000000
[  174.234709] xe 0000:00:02.0: [drm] 0x00000000 0x00002000 0x00000000 0x00002000
[  174.234714] xe 0000:00:02.0: [drm] 0x00000000 0x00000002 0xcabba9f6 0xbeeffeed
[  174.234719] xe 0000:00:02.0: [drm] 0x00000000 0x00000000 0x00004000 0x00000000
[  174.234724] xe 0000:00:02.0: [drm] 0x00004000 0x00000000 0x00000002 0x8086900d
[  174.234730] xe 0000:00:02.0: [drm] 0x00010000 0x00000006 0x00010001 0x00460606
[  174.234735] xe 0000:00:02.0: [drm] 0x00020001 0x00004050 0x00030001 0x00004b00
[  174.234741] xe 0000:00:02.0: [drm] 0x00000000 0x00000000 0x00000000 0x00000000

uAPI wise it need some renames to follow with the uAPI alignment series(https://patchwork.freedesktop.org/series/124271/) take a look at
https://patchwork.freedesktop.org/patch/559576/?series=124271&rev=1
https://patchwork.freedesktop.org/patch/559577/?series=124271&rev=1

#10 lore raw
Quoted 37 lines · Jose
Quoted 21 lines · Matthew Auld

Branch available here:
https://gitlab.freedesktop.org/mwa/kernel/-/tree/xe-pat-index?ref_type=heads

Series directly depends on the patches here:
https://patchwork.freedesktop.org/series/124225/

Goal here is to allow userspace to directly control the pat_index when mapping memory via the ppGTT, in addtion to the CPU caching mode. This is very much needed on newer igpu platforms which allow incoherent GT access, where the choice over the cache level and expected coherency is best left to userspace depending on their usecase. In the future there may also be other stuff encoded in the pat_index, so giving userspace direct control will also be needed there.

To support this we added new gem_create uAPI for selecting the CPU cache mode to use for system memory, including the expected GPU coherency mode. There are various restrictions here for the selected coherency mode and compatible CPU cache modes. With that in place the actual pat_index can now be provided as part of vm_bind. The only restriction is that the coherency mode of the pat_index must be at least as coherent as the gem_create coherency mode. There are also some special cases like with userptr and dma-buf.

Quoted 14 lines

v2:
- Loads of improvements/tweaks. Main changes are to now allow gem_create.coh_mode <= coh_mode(pat_index), rather than it needing to match exactly. This simplifies the dma-buf policy from userspace pov. Also we now only consider COH_NONE and COH_AT_LEAST_1WAY.
v3:
- Rebase. Split the pte_encode() refactoring, plus various smaller tweaks and fixes.
v4:
- Rebase on Lucas' new series.
- Drop UC cache mode.
- s/smem_cpu_caching/cpu_caching/. Idea is to make VRAM WC explicit in the uapi, plus make it more future proof.

Quoted 3 lines

Thanks for the smem_cpu_caching to cpu_caching change.

This latest version is causing a GuC fw load failure in MTL, I have bisected and it is caused by "drm/xe: directly use pat_index for pte_encode".

Ok, there are some fixes in the latest version around that patch, perhaps that resolves it.

Quoted 19 lines
[  173.995308] xe 0000:00:02.0: [drm:xe_guc_init [xe]] GuC param[12] = 0x00000000
[  173.995388] xe 0000:00:02.0: [drm:xe_guc_init [xe]] GuC param[13] = 0x00000000
[  173.995467] xe 0000:00:02.0: [drm:xe_wopcm_init [xe]] WOPCM: 4096K
[  173.995609] xe 0000:00:02.0: [drm:xe_wopcm_init [xe]] GuC WOPCM is already locked [2048K, 832K)
[  174.234667] xe 0000:00:02.0: [drm] GuC load failed: status = 0x80007134
[  174.234681] xe 0000:00:02.0: [drm] GuC load failed: status: Reset = 0, BootROM = 0x1A, UKernel = 0x71, MIA = 0x00, Auth = 0x02
[  174.234690] xe 0000:00:02.0: [drm] 0xcabba9e6 0xdeadfeed 0x00000000 0x00000078
[  174.234697] xe 0000:00:02.0: [drm] 0x00010000 0x00000000 0x0000fff0 0x00000000
[  174.234703] xe 0000:00:02.0: [drm] 0x00000002 0xcabba9e6 0x8086dead 0x00000000
[  174.234709] xe 0000:00:02.0: [drm] 0x00000000 0x00002000 0x00000000 0x00002000
[  174.234714] xe 0000:00:02.0: [drm] 0x00000000 0x00000002 0xcabba9f6 0xbeeffeed
[  174.234719] xe 0000:00:02.0: [drm] 0x00000000 0x00000000 0x00004000 0x00000000
[  174.234724] xe 0000:00:02.0: [drm] 0x00004000 0x00000000 0x00000002 0x8086900d
[  174.234730] xe 0000:00:02.0: [drm] 0x00010000 0x00000006 0x00010001 0x00460606
[  174.234735] xe 0000:00:02.0: [drm] 0x00020001 0x00004050 0x00030001 0x00004b00
[  174.234741] xe 0000:00:02.0: [drm] 0x00000000 0x00000000 0x00000000 0x00000000
Quoted 2 lines

uAPI wise it need some renames to follow with the uAPI alignment series(https://patchwork.freedesktop.org/series/124271/) take a look at
https://patchwork.freedesktop.org/patch/559576/?series=124271&rev=1

Ok, will align with that.

Quoted 1 line

https://patchwork.freedesktop.org/patch/559577/?series=124271&rev=1

I think we are only dealing with adding properties and not so much flags. i.e you can't apply them like flags: cpu_mapping = WC | WB.

Quoted 2 lines