[PATCH v1 2/4] drm/xe/ttm: Add PF-mem VRAM placement types for TTM

Piórkowski, Piotr <[email protected]>
Newsgroups org.freedesktop.lists.intel-xe
Message-ID <[email protected]>
From: Piotr Piórkowski <[email protected]>

PF-mem regions are VRAM regions dedicated to kernel-only allocations,
where buffers are allocated exclusively by the kernel driver and are
never exposed to userspace workloads. Representing such regions
requires dedicated TTM placement types so the BO paths can route and
evict them correctly.

Add new PF-mem VRAM placement types and update BO paths to handle these
new VRAM placements.

Signed-off-by: Piotr Piórkowski <[email protected]>
---
 drivers/gpu/drm/xe/xe_bo.c           | 16 ++++++++++++----
 drivers/gpu/drm/xe/xe_bo.h           | 17 +++++++++++++++++
 drivers/gpu/drm/xe/xe_bo_evict.c     |  2 +-
 drivers/gpu/drm/xe/xe_pm.c           |  4 ++--
 drivers/gpu/drm/xe/xe_res_cursor.h   |  9 ++++++++-
 drivers/gpu/drm/xe/xe_ttm_vram_mgr.c |  6 ++----
 6 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
index dde309821237..7b1a82e18faa 100644
--- a/drivers/gpu/drm/xe/xe_bo.c
+++ b/drivers/gpu/drm/xe/xe_bo.c
@@ -46,6 +46,8 @@ const char *const xe_mem_type_to_name[TTM_NUM_MEM_TYPES]  = {
 	[XE_PL_TT] = "gtt",
 	[XE_PL_VRAM0] = "vram0",
 	[XE_PL_VRAM1] = "vram1",
+	[XE_PL_VRAM0_PFMEM] = "vram0_pfmem",
+	[XE_PL_VRAM1_PFMEM] = "vram1_pfmem",
 	[XE_PL_STOLEN] = "stolen"
 };
 
@@ -166,11 +168,12 @@ static bool xe_bo_is_user(struct xe_bo *bo)
 static struct xe_migrate *
 mem_type_to_migrate(struct xe_device *xe, u32 mem_type)
 {
-	struct xe_tile *tile;
+	u8 tile_id;
 
 	xe_assert(xe, mem_type == XE_PL_STOLEN || mem_type_is_vram(mem_type));
-	tile = &xe->tiles[mem_type == XE_PL_STOLEN ? 0 : (mem_type - XE_PL_VRAM0)];
-	return tile->migrate;
+	tile_id = mem_type == XE_PL_STOLEN ? 0 : xe_vram_pl_to_tile_id(xe, mem_type);
+
+	return xe->tiles[tile_id].migrate;
 }
 
 static void try_add_system(struct xe_device *xe, struct xe_bo *bo,
@@ -360,6 +363,8 @@ static void xe_evict_flags(struct ttm_buffer_object *tbo,
 	switch (tbo->resource->mem_type) {
 	case XE_PL_VRAM0:
 	case XE_PL_VRAM1:
+	case XE_PL_VRAM0_PFMEM:
+	case XE_PL_VRAM1_PFMEM:
 	case XE_PL_STOLEN:
 		*placement = tt_placement;
 		break;
@@ -639,7 +644,10 @@ static int xe_ttm_io_mem_reserve(struct ttm_device *bdev,
 	case XE_PL_TT:
 		return 0;
 	case XE_PL_VRAM0:
-	case XE_PL_VRAM1: {
+	case XE_PL_VRAM1:
+	case XE_PL_VRAM0_PFMEM:
+	case XE_PL_VRAM1_PFMEM:
+	{
 		struct xe_vram_region *vram = xe_map_resource_to_region(mem);
 
 		if (!xe_ttm_resource_visible(xe, mem))
diff --git a/drivers/gpu/drm/xe/xe_bo.h b/drivers/gpu/drm/xe/xe_bo.h
index e8081af5bfc1..c698f70a3b0c 100644
--- a/drivers/gpu/drm/xe/xe_bo.h
+++ b/drivers/gpu/drm/xe/xe_bo.h
@@ -83,8 +83,13 @@
 #define XE_PL_TT		TTM_PL_TT
 #define XE_PL_VRAM0		TTM_PL_VRAM
 #define XE_PL_VRAM1		(XE_PL_VRAM0 + 1)
+#define XE_PL_VRAM0_PFMEM	(XE_PL_VRAM1 + 1)
+#define XE_PL_VRAM1_PFMEM	(XE_PL_VRAM0_PFMEM + 1)
 #define XE_PL_STOLEN		(TTM_NUM_MEM_TYPES - 1)
 
+#define xe_for_each_vram_pl(i) \
+	for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1_PFMEM; i++)
+
 #define XE_BO_PROPS_INVALID	(-1)
 
 #define XE_PCI_BARRIER_MMAP_OFFSET	(0x50 << XE_PTE_SHIFT)
@@ -614,4 +619,16 @@ static inline bool xe_bo_is_mem_type(struct xe_bo *bo, u32 mem_type)
 	xe_bo_assert_held(bo);
 	return bo->ttm.resource->mem_type == mem_type;
 }
+
+static inline u8 xe_vram_pl_to_tile_id(struct xe_device *xe, u32 mem_type)
+{
+	xe_assert(xe, mem_type_is_vram(mem_type));
+
+	if (mem_type >= XE_PL_VRAM0 && mem_type <= XE_PL_VRAM1)
+		return mem_type - XE_PL_VRAM0;
+
+	xe_assert(xe, mem_type >= XE_PL_VRAM0_PFMEM && mem_type <= XE_PL_VRAM1_PFMEM);
+	return mem_type - XE_PL_VRAM0_PFMEM;
+}
+
 #endif
diff --git a/drivers/gpu/drm/xe/xe_bo_evict.c b/drivers/gpu/drm/xe/xe_bo_evict.c
index 7661fca7f278..e2ae97f5c684 100644
--- a/drivers/gpu/drm/xe/xe_bo_evict.c
+++ b/drivers/gpu/drm/xe/xe_bo_evict.c
@@ -303,7 +303,7 @@ void xe_bo_pci_dev_remove_all(struct xe_device *xe)
 	 * Move pagemap bos and exported dma-buf to system, and
 	 * purge everything else.
 	 */
-	for (mem_type = XE_PL_VRAM1; mem_type >= XE_PL_TT; --mem_type) {
+	for (mem_type = XE_PL_VRAM1_PFMEM; mem_type >= XE_PL_TT; --mem_type) {
 		struct ttm_resource_manager *man =
 			ttm_manager_type(&xe->ttm, mem_type);
 
diff --git a/drivers/gpu/drm/xe/xe_pm.c b/drivers/gpu/drm/xe/xe_pm.c
index a5289a9df8d2..4c1630bdcc0e 100644
--- a/drivers/gpu/drm/xe/xe_pm.c
+++ b/drivers/gpu/drm/xe/xe_pm.c
@@ -970,7 +970,7 @@ int xe_pm_set_vram_threshold(struct xe_device *xe, u32 threshold)
 	u32 vram_total_mb = 0;
 	int i;
 
-	for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
+	xe_for_each_vram_pl(i) {
 		man = ttm_manager_type(&xe->ttm, i);
 		if (man)
 			vram_total_mb += DIV_ROUND_UP_ULL(man->size, 1024 * 1024);
@@ -1007,7 +1007,7 @@ void xe_pm_d3cold_allowed_toggle(struct xe_device *xe)
 		return;
 	}
 
-	for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
+	xe_for_each_vram_pl(i) {
 		man = ttm_manager_type(&xe->ttm, i);
 		if (man) {
 			vram_used = ttm_resource_manager_usage(man);
diff --git a/drivers/gpu/drm/xe/xe_res_cursor.h b/drivers/gpu/drm/xe/xe_res_cursor.h
index 0522caafd89d..4603a3356e53 100644
--- a/drivers/gpu/drm/xe/xe_res_cursor.h
+++ b/drivers/gpu/drm/xe/xe_res_cursor.h
@@ -111,7 +111,9 @@ static inline void xe_res_first(struct ttm_resource *res,
 		break;
 	}
 	case XE_PL_VRAM0:
-	case XE_PL_VRAM1: {
+	case XE_PL_VRAM1:
+	case XE_PL_VRAM0_PFMEM:
+	case XE_PL_VRAM1_PFMEM: {
 		struct gpu_buddy_block *block;
 		struct list_head *head, *next;
 		struct gpu_buddy *mm = xe_res_get_buddy(res);
@@ -303,6 +305,8 @@ static inline void xe_res_next(struct xe_res_cursor *cur, u64 size)
 		break;
 	case XE_PL_VRAM0:
 	case XE_PL_VRAM1:
+	case XE_PL_VRAM0_PFMEM:
+	case XE_PL_VRAM1_PFMEM: {
 		start = size - cur->size;
 		block = cur->node;
 
@@ -322,6 +326,7 @@ static inline void xe_res_next(struct xe_res_cursor *cur, u64 size)
 				cur->remaining);
 		cur->node = block;
 		break;
+	}
 	default:
 		return;
 	}
@@ -358,6 +363,8 @@ static inline bool xe_res_is_vram(const struct xe_res_cursor *cur)
 	case XE_PL_STOLEN:
 	case XE_PL_VRAM0:
 	case XE_PL_VRAM1:
+	case XE_PL_VRAM0_PFMEM:
+	case XE_PL_VRAM1_PFMEM:
 		return true;
 	default:
 		break;
diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
index 05911904c1f9..4b4ab7db70f0 100644
--- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
+++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c
@@ -336,7 +336,6 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr,
 {
 	struct ttm_resource_manager *man = &mgr->manager;
 	struct dmem_cgroup_region *cg;
-	const char *name;
 	int err;
 
 	man->func = &xe_ttm_vram_mgr_func;
@@ -365,8 +364,7 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr,
 	if (err)
 		return err;
 
-	name = mem_type == XE_PL_VRAM0 ? "vram0" : "vram1";
-	cg = drmm_cgroup_register_region(&xe->drm, name,
+	cg = drmm_cgroup_register_region(&xe->drm, xe_mem_type_to_name[mem_type],
 					 &(struct dmem_cgroup_init){
 						.size = size,
 						.ops = &xe_ttm_vram_mgr_dmem_ops,
@@ -405,7 +403,7 @@ int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe,
 			      enum dma_data_direction dir,
 			      struct sg_table **sgt)
 {
-	struct xe_tile *tile = &xe->tiles[res->mem_type - XE_PL_VRAM0];
+	struct xe_tile *tile = &xe->tiles[xe_vram_pl_to_tile_id(xe, res->mem_type)];
 	struct xe_ttm_vram_mgr_resource *vres = to_xe_ttm_vram_mgr_resource(res);
 	struct xe_res_cursor cursor;
 	struct scatterlist *sg;
-- 
2.34.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.