Skip to contentSkip to conversation

drm/amdkfd: Move gfx9 MQD to HBM

PATCH Superseded Philip Yang 1 participant 5 messages Nov 26, 2025

Subsystems (3)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover drm/amdkfd: Move gfx9 MQD to HBM

To reduce multiple queues switch latency, move user queues MQD to HBM, and map on GART with mtype RW.

Philip Yang (4):
drm/amdkfd: Bind gfx9 MQD in GART with mtype RW drm/amdkfd: Add domain parameter to alloc kernel BO drm/amdkfd: Move gfx9 MQD to VRAM domain drm/amdkfd: Map VRAM MQD on GART

 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c    |   9 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |   3 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 112 +++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |   1 +
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c        |   1 +
 drivers/gpu/drm/amd/amdkfd/kfd_device.c       |   3 +-
 .../drm/amd/amdkfd/kfd_device_queue_manager.c |   1 +
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |   9 ++
 .../amd/amdkfd/kfd_process_queue_manager.c    |   2 +
 12 files changed, 121 insertions(+), 24 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH 1/4 drm/amdkfd: Bind gfx9 MQD in GART with mtype RW

For gfx9, bind MQD in GART with mtype RW to enable caching, to reduce queue switch latency.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  | 1 +
 2 files changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
index b589fa9370d2..b1810d0893e8 100644
@@ -840,6 +840,8 @@ static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,
 	int i;
 	uint64_t ctrl_flags = AMDGPU_PTE_MTYPE_VG10(flags, AMDGPU_MTYPE_NC);
 
+	flags = AMDGPU_PTE_MTYPE_VG10(flags, AMDGPU_MTYPE_RW);
+
 	pages_per_xcc = total_pages;
 	do_div(pages_per_xcc, num_xcc);
 
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +1 -0
index 330e4bdea387..cc546c9ef34a 100644
@@ -101,6 +101,7 @@ struct amdgpu_bo_vm;
 	  AMDGPU_PTE_MTYPE_VG10_SHIFT(mtype))
 
 #define AMDGPU_MTYPE_NC 0
+#define AMDGPU_MTYPE_RW 1
 #define AMDGPU_MTYPE_CC 2
 
 #define AMDGPU_PTE_DEFAULT_ATC  (AMDGPU_PTE_SYSTEM      \
Signature · 1 line
2.50.1
#2 lore raw

PATCH 2/4 drm/amdkfd: Add domain parameter to alloc kernel BO

In reply to Philip Yang

To allocate kernel BO from VRAM domain for MQD in the following patch. No functional change because kernel BO allocate all from GTT domain.

amdgpu_amdkfd_alloc_gtt_mem has many duplicate code as function amdgpu_bo_create_kernel, with one extra flag MQD_GFX9 to bind MQD and control stack in GART with different mtype, the duplicate code could be removed in future.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c             | 6 +++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h             | 2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c                 | 1 +
 drivers/gpu/drm/amd/amdkfd/kfd_device.c                | 3 ++-
 drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c  | 1 +
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c        | 1 +
 drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 2 ++
 7 files changed, 11 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +3 -3
index a2879d2b7c8e..9cd1660b8f60 100644
@@ -317,7 +317,7 @@ void amdgpu_amdkfd_gpu_reset(struct amdgpu_device *adev)
 }
 
 int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
-				void **mem_obj, uint64_t *gpu_addr,
+				u32 domain, void **mem_obj, uint64_t *gpu_addr,
 				void **cpu_ptr, bool cp_mqd_gfx9)
 {
 	struct amdgpu_bo *bo = NULL;
@@ -328,7 +328,7 @@ int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
 	memset(&bp, 0, sizeof(bp));
 	bp.size = size;
 	bp.byte_align = PAGE_SIZE;
-	bp.domain = AMDGPU_GEM_DOMAIN_GTT;
+	bp.domain = domain;
 	bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
 	bp.type = ttm_bo_type_kernel;
 	bp.resv = NULL;
@@ -351,7 +351,7 @@ int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
 		goto allocate_mem_reserve_bo_failed;
 	}
 
-	r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
+	r = amdgpu_bo_pin(bo, domain);
 	if (r) {
 		dev_err(adev->dev, "(%d) failed to pin bo for amdkfd\n", r);
 		goto allocate_mem_pin_bo_failed;
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +1 -1
index 335d733751cb..14b5ab6fa051 100644
@@ -237,7 +237,7 @@ int amdgpu_amdkfd_bo_validate_and_fence(struct amdgpu_bo *bo,
 #endif
 /* Shared API */
 int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
-				void **mem_obj, uint64_t *gpu_addr,
+				u32 domain, void **mem_obj, uint64_t *gpu_addr,
 				void **cpu_ptr, bool mqd_gfx9);
 void amdgpu_amdkfd_free_gtt_mem(struct amdgpu_device *adev, void **mem_obj);
 int amdgpu_amdkfd_alloc_gws(struct amdgpu_device *adev, size_t size,
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +1 -0
index ba9a09b6589a..494a563e072e 100644
@@ -359,6 +359,7 @@ int kfd_dbg_set_mes_debug_mode(struct kfd_process_device *pdd, bool sq_trap_en)
 	if (!pdd->proc_ctx_cpu_ptr) {
 		r = amdgpu_amdkfd_alloc_gtt_mem(adev,
 			AMDGPU_MES_PROC_CTX_SIZE,
+			AMDGPU_GEM_DOMAIN_GTT,
 			&pdd->proc_ctx_bo,
 			&pdd->proc_ctx_gpu_addr,
 			&pdd->proc_ctx_cpu_ptr,
drivers/gpu/drm/amd/amdkfd/kfd_device.c +2 -1
index 9f183d27a0ef..b0cfad750919 100644
@@ -787,7 +787,8 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
 	size += 512 * 1024;
 
 	if (amdgpu_amdkfd_alloc_gtt_mem(
-			kfd->adev, size, &kfd->gtt_mem,
+			kfd->adev, size, AMDGPU_GEM_DOMAIN_GTT,
+			&kfd->gtt_mem,
 			&kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
 			false)) {
 		dev_err(kfd_device, "Could not allocate %d bytes\n", size);
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +1 -0
index d701c66179cf..6de341dfdf48 100644
@@ -2908,6 +2908,7 @@ static int allocate_hiq_sdma_mqd(struct device_queue_manager *dqm)
 		NUM_XCC(dqm->dev->xcc_mask));
 
 	retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev, size,
+		AMDGPU_GEM_DOMAIN_GTT,
 		&(mem_obj->gtt_mem), &(mem_obj->gpu_addr),
 		(void *)&(mem_obj->cpu_ptr), false);
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +1 -0
index 2e9b6bcf2704..a489d43d5f64 100644
@@ -139,6 +139,7 @@ static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node,
 			(ALIGN(q->ctl_stack_size, PAGE_SIZE) +
 			ALIGN(sizeof(struct v9_mqd), PAGE_SIZE)) *
 			NUM_XCC(node->xcc_mask),
+			AMDGPU_GEM_DOMAIN_GTT,
 			&(mqd_mem_obj->gtt_mem),
 			&(mqd_mem_obj->gpu_addr),
 			(void *)&(mqd_mem_obj->cpu_ptr), true);
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +2 -0
index fb1aeeacf02d..eca68d2953ac 100644
@@ -266,6 +266,7 @@ static int init_user_queue(struct process_queue_manager *pqm,
 	if (dev->kfd->shared_resources.enable_mes) {
 		retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev,
 						AMDGPU_MES_GANG_CTX_SIZE,
+						AMDGPU_GEM_DOMAIN_GTT,
 						&(*q)->gang_ctx_bo,
 						&(*q)->gang_ctx_gpu_addr,
 						&(*q)->gang_ctx_cpu_ptr,
@@ -369,6 +370,7 @@ int pqm_create_queue(struct process_queue_manager *pqm,
 	if (!pdd->proc_ctx_cpu_ptr && dev->kfd->shared_resources.enable_mes) {
 		retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev,
 						     AMDGPU_MES_PROC_CTX_SIZE,
+						     AMDGPU_GEM_DOMAIN_GTT,
 						     &pdd->proc_ctx_bo,
 						     &pdd->proc_ctx_gpu_addr,
 						     &pdd->proc_ctx_cpu_ptr,
Signature · 1 line
2.50.1
#3 lore raw

PATCH 3/4 drm/amdkfd: Move gfx9 MQD to VRAM domain

In reply to Philip Yang

To reduce queue switch latency further, move MQD to VRAM domain, add AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS flag to allocate contiguous pages.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c      | 3 ++-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +2 -1
index 9cd1660b8f60..c11e37915365 100644
@@ -329,7 +329,8 @@ int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
 	bp.size = size;
 	bp.byte_align = PAGE_SIZE;
 	bp.domain = domain;
-	bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
+	bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
+		   AMDGPU_GEM_CREATE_CPU_GTT_USWC;
 	bp.type = ttm_bo_type_kernel;
 	bp.resv = NULL;
 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +1 -1
index a489d43d5f64..c6945c842267 100644
@@ -139,7 +139,7 @@ static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node,
 			(ALIGN(q->ctl_stack_size, PAGE_SIZE) +
 			ALIGN(sizeof(struct v9_mqd), PAGE_SIZE)) *
 			NUM_XCC(node->xcc_mask),
-			AMDGPU_GEM_DOMAIN_GTT,
+			AMDGPU_GEM_DOMAIN_VRAM,
 			&(mqd_mem_obj->gtt_mem),
 			&(mqd_mem_obj->gpu_addr),
 			(void *)&(mqd_mem_obj->cpu_ptr), true);
Signature · 1 line
2.50.1
#4 lore raw

PATCH 4/4 drm/amdkfd: Map VRAM MQD on GART

In reply to Philip Yang

MQD BO on VRAM access via FB aperture is mtype UC uncaching, map to GART as mtype RW caching, to reduce queue switch latency.

TTM bo only has one resource, add GART resource to amdgpu_bo in order to support dynamic GART allocation to map VRAM MQD BO.

Change amdgpu_ttm_gart_bind_gfx9_mqd to map MQD in GART with gtt offset for system pages or with gart_res for VRAM BO resource.

Add helper amdgpu_ttm_alloc_gart_for_vram_bo to alloc GART entries resource for MQD bo->gart_res and bind to GART mapping.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c    |   3 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h    |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 110 +++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |   1 +
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |   8 ++
 5 files changed, 105 insertions(+), 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +3 -0
index 24ebba43a469..676b039ef70a 100644
@@ -1297,6 +1297,9 @@ void amdgpu_bo_release_notify(struct ttm_buffer_object *bo)
 
 	abo = ttm_to_amdgpu_bo(bo);
 
+	if (abo->gart_res)
+		ttm_resource_free(bo, &abo->gart_res);
+
 	WARN_ON(abo->vm_bo);
 
 	if (abo->kfd_bo)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +1 -0
index 52c2d1731aab..a412f5ec2a09 100644
@@ -106,6 +106,7 @@ struct amdgpu_bo {
 	struct ttm_place		placements[AMDGPU_BO_MAX_PLACEMENTS];
 	struct ttm_placement		placement;
 	struct ttm_buffer_object	tbo;
+	struct ttm_resource		*gart_res;
 	struct ttm_bo_kmap_obj		kmap;
 	u64				flags;
 	/* per VM structure for page tables and with virtual addresses */
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +92 -18

143 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -0
index 74940e59ed6e..0a9d2fc72c56 100644
@@ -178,6 +178,7 @@ int amdgpu_fill_buffer(struct amdgpu_bo *bo,
 			bool delayed);
 
 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo, u64 *gpu_addr);
 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
 
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +8 -0
index c6945c842267..d96de02c6bb9 100644
@@ -148,6 +148,14 @@ static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node,
 			kfree(mqd_mem_obj);
 			return NULL;
 		}
+
+		retval = amdgpu_ttm_alloc_gart_vram_bo(mqd_mem_obj->gtt_mem,
+						       &(mqd_mem_obj->gpu_addr));
+		if (retval) {
+			amdgpu_amdkfd_free_gtt_mem(node->adev, &(mqd_mem_obj->gtt_mem));
+			kfree(mqd_mem_obj);
+			return NULL;
+		}
 	} else {
 		retval = kfd_gtt_sa_allocate(node, sizeof(struct v9_mqd),
 				&mqd_mem_obj);
Signature · 1 line
2.50.1