Skip to contentSkip to conversation

drm/amdkfd: Move gfx9 MQD to HBM

PATCH Superseded Philip Yang 5 participants 18 messages Dec 15, 2025 – Dec 17, 2025

Subsystems (4)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 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.

v6:
- patch 5 remove color parameter from the helper
- patch 6 changes suggested by Christian
- add comment at the head of the functions

v5:
- patch 1 update commit message
- patch 5 separate GART alloc helpers
- patch 6 use resource cursor to handle GART entries

v4:
- patch 1 remove the executable bit change, cc stable
- patch 5 move GART helper functions to amdgpu_gtt_mgr.c
- add patch 6 to update MQD GART mapping using resource cursor

v3:
- add patch 1 to fix gfx9 mtype update bug
- patch 2 use ASIC specific mtype
- patch 5 use drm mm to alloc GART entries and store in mqd obj

v2:
- patch 4 GART mapping use MC address, vram_base_offset + physical address

Philip Yang (6):
drm/amdgpu: Fix gfx9 update PTE mtype flag drm/amdkfd: Bind MQD in GART with mtype RW drm/amdkfd: Add domain parameter to alloc kernel BO drm/amdkfd: Move gfx9 MQD to VRAM domain drm/amdgpu: Add helper to alloc GART entries drm/amdkfd: Map VRAM MQD on GART

 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c    |  13 +--
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h    |   6 +-
 .../gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c  |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c   |  48 +++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 101 +++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  10 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h        |   3 -
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c         |   8 +-
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c        |   3 +-
 drivers/gpu/drm/amd/amdkfd/kfd_device.c       |  13 +--
 .../drm/amd/amdkfd/kfd_device_queue_manager.c |   7 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c  |  16 +--
 .../drm/amd/amdkfd/kfd_mqd_manager_v12_1.c    |   4 +-
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  18 +++-
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |   3 +-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c      |   2 +-
 .../amd/amdkfd/kfd_process_queue_manager.c    |  12 ++-
 17 files changed, 220 insertions(+), 49 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v6 1/6 drm/amdgpu: Fix gfx9 update PTE mtype flag MIME-Version: 1.0

Fix copy&paste error, that should have been an assignment instead of an or, otherwise MTYPE_UC 0x3 can not be updated to MTYPE_RW 0x1.

CC stables.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +4 -4
index 97a04e3171f2..205c34eb8d11 100644
@@ -1204,16 +1204,16 @@ static void gmc_v9_0_get_vm_pte(struct amdgpu_device *adev,
 		*flags = AMDGPU_PTE_MTYPE_VG10(*flags, MTYPE_NC);
 		break;
 	case AMDGPU_VM_MTYPE_WC:
-		*flags |= AMDGPU_PTE_MTYPE_VG10(*flags, MTYPE_WC);
+		*flags = AMDGPU_PTE_MTYPE_VG10(*flags, MTYPE_WC);
 		break;
 	case AMDGPU_VM_MTYPE_RW:
-		*flags |= AMDGPU_PTE_MTYPE_VG10(*flags, MTYPE_RW);
+		*flags = AMDGPU_PTE_MTYPE_VG10(*flags, MTYPE_RW);
 		break;
 	case AMDGPU_VM_MTYPE_CC:
-		*flags |= AMDGPU_PTE_MTYPE_VG10(*flags, MTYPE_CC);
+		*flags = AMDGPU_PTE_MTYPE_VG10(*flags, MTYPE_CC);
 		break;
 	case AMDGPU_VM_MTYPE_UC:
-		*flags |= AMDGPU_PTE_MTYPE_VG10(*flags, MTYPE_UC);
+		*flags = AMDGPU_PTE_MTYPE_VG10(*flags, MTYPE_UC);
 		break;
 	}
 
Signature · 1 line
2.50.1
#2 lore raw

PATCH v6 2/6 drm/amdkfd: Bind MQD in GART with mtype RW

In reply to Philip Yang

For gfx version >= 9.4.3, bind MQD in GART with mtype RW to enable caching, to reduce queue switch latency.

Remove the redundant mtype definition in amdgpu_vm.h.

Call amdgpu_gmc_get_vm_pte with AMDGPU_VM_MTYPE_ to get ASIC specific mtype.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 7 ++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h  | 3 ---
 2 files changed, 6 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +6 -1
index 0f83d140b6ae..4f8bc7f35cdc 100644
@@ -853,7 +853,12 @@ static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,
 	int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp);
 	uint64_t page_idx, pages_per_xcc;
 	int i;
-	uint64_t ctrl_flags = AMDGPU_PTE_MTYPE_VG10(flags, AMDGPU_MTYPE_NC);
+	uint64_t ctrl_flags = flags;
+
+	amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_NC, &ctrl_flags);
+
+	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 3))
+		amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_RW, &flags);
 
 	pages_per_xcc = total_pages;
 	do_div(pages_per_xcc, num_xcc);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.h +0 -3
index 139642eacdd0..e34c8f854b2f 100644
@@ -100,9 +100,6 @@ struct amdgpu_bo_vm;
 	(((uint64_t)(flags) & (~AMDGPU_PTE_MTYPE_VG10_MASK)) |	\
 	  AMDGPU_PTE_MTYPE_VG10_SHIFT(mtype))
 
-#define AMDGPU_MTYPE_NC 0
-#define AMDGPU_MTYPE_CC 2
-
 #define AMDGPU_PTE_DEFAULT_ATC  (AMDGPU_PTE_SYSTEM      \
                                 | AMDGPU_PTE_SNOOPED    \
                                 | AMDGPU_PTE_EXECUTABLE \
Signature · 1 line
2.50.1
#3 lore raw

PATCH v6 3/6 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.

Rename amdgpu_amdkfd_alloc_gtt_mem to amdgpu_amdkfd_alloc_kernel_mem Rename amdgpu_amdkfd_free_gtt_mem to amdgpu_amdkfd_free_kernel_mem Rename mem_kfd_mem_obj gtt_mem to mem

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c         | 10 +++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h         |  6 +++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c   |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_debug.c             |  3 ++-
 drivers/gpu/drm/amd/amdkfd/kfd_device.c            | 13 +++++++------
 .../gpu/drm/amd/amdkfd/kfd_device_queue_manager.c  |  7 ++++---
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c       | 14 +++++++-------
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12_1.c |  4 ++--
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c    |  9 +++++----
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h              |  2 +-
 drivers/gpu/drm/amd/amdkfd/kfd_process.c           |  2 +-
 .../gpu/drm/amd/amdkfd/kfd_process_queue_manager.c | 12 +++++++-----
 12 files changed, 45 insertions(+), 39 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +5 -5
index a2879d2b7c8e..090d17911bc4 100644
@@ -316,8 +316,8 @@ void amdgpu_amdkfd_gpu_reset(struct amdgpu_device *adev)
 					     &adev->kfd.reset_work);
 }
 
-int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
-				void **mem_obj, uint64_t *gpu_addr,
+int amdgpu_amdkfd_alloc_kernel_mem(struct amdgpu_device *adev, size_t size,
+				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;
@@ -388,7 +388,7 @@ int amdgpu_amdkfd_alloc_gtt_mem(struct amdgpu_device *adev, size_t size,
 	return r;
 }
 
-void amdgpu_amdkfd_free_gtt_mem(struct amdgpu_device *adev, void **mem_obj)
+void amdgpu_amdkfd_free_kernel_mem(struct amdgpu_device *adev, void **mem_obj)
 {
 	struct amdgpu_bo **bo = (struct amdgpu_bo **) mem_obj;
 
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h +3 -3
index da4575676335..274a99eb6d44 100644
@@ -240,10 +240,10 @@ 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,
+int amdgpu_amdkfd_alloc_kernel_mem(struct amdgpu_device *adev, size_t size,
+				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);
+void amdgpu_amdkfd_free_kernel_mem(struct amdgpu_device *adev, void **mem_obj);
 int amdgpu_amdkfd_alloc_gws(struct amdgpu_device *adev, size_t size,
 				void **mem_obj);
 void amdgpu_amdkfd_free_gws(struct amdgpu_device *adev, void *mem_obj);
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c +1 -1
index 6585b8b9e709..b029a3e218b9 100644
@@ -2214,7 +2214,7 @@ int amdgpu_amdkfd_gpuvm_sync_memory(
  * @bo_gart: Return bo reference
  *
  * Before return, bo reference count is incremented. To release the reference and unpin/
- * unmap the BO, call amdgpu_amdkfd_free_gtt_mem.
+ * unmap the BO, call amdgpu_amdkfd_free_kernel_mem.
  */
 int amdgpu_amdkfd_map_gtt_bo_to_gart(struct amdgpu_bo *bo, struct amdgpu_bo **bo_gart)
 {
drivers/gpu/drm/amd/amdkfd/kfd_debug.c +2 -1
index f83e1238c1b3..903372ce29c9 100644
@@ -357,8 +357,9 @@ int kfd_dbg_set_mes_debug_mode(struct kfd_process_device *pdd, bool sq_trap_en)
 		return 0;
 
 	if (!pdd->proc_ctx_cpu_ptr) {
-		r = amdgpu_amdkfd_alloc_gtt_mem(adev,
+		r = amdgpu_amdkfd_alloc_kernel_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 +7 -6
index b91843b2af6a..23f4da4df3cd 100644
@@ -808,12 +808,13 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
 	/* add another 512KB for all other allocations on gart (HPD, fences) */
 	size += 512 * 1024;
 
-	if (amdgpu_amdkfd_alloc_gtt_mem(
-			kfd->adev, size, &kfd->gtt_mem,
+	if (amdgpu_amdkfd_alloc_kernel_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);
-		goto alloc_gtt_mem_failure;
+		goto alloc_kernel_mem_failure;
 	}
 
 	dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
@@ -937,8 +938,8 @@ bool kgd2kfd_device_init(struct kfd_dev *kfd,
 kfd_doorbell_error:
 	kfd_gtt_sa_fini(kfd);
 kfd_gtt_sa_init_error:
-	amdgpu_amdkfd_free_gtt_mem(kfd->adev, &kfd->gtt_mem);
-alloc_gtt_mem_failure:
+	amdgpu_amdkfd_free_kernel_mem(kfd->adev, &kfd->gtt_mem);
+alloc_kernel_mem_failure:
 	dev_err(kfd_device,
 		"device %x:%x NOT added due to errors\n",
 		kfd->adev->pdev->vendor, kfd->adev->pdev->device);
@@ -955,7 +956,7 @@ void kgd2kfd_device_exit(struct kfd_dev *kfd)
 		kfd_doorbell_fini(kfd);
 		ida_destroy(&kfd->doorbell_ida);
 		kfd_gtt_sa_fini(kfd);
-		amdgpu_amdkfd_free_gtt_mem(kfd->adev, &kfd->gtt_mem);
+		amdgpu_amdkfd_free_kernel_mem(kfd->adev, &kfd->gtt_mem);
 	}
 
 	kfree(kfd);
drivers/gpu/drm/amd/amdkfd/kfd_device_queue_manager.c +4 -3
index 1dbdf9465e59..3b846bc59e64 100644
@@ -2907,8 +2907,9 @@ static int allocate_hiq_sdma_mqd(struct device_queue_manager *dqm)
 		(dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]->mqd_size *
 		NUM_XCC(dqm->dev->xcc_mask));
 
-	retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev, size,
-		&(mem_obj->gtt_mem), &(mem_obj->gpu_addr),
+	retval = amdgpu_amdkfd_alloc_kernel_mem(dev->adev, size,
+		AMDGPU_GEM_DOMAIN_GTT,
+		&(mem_obj->mem), &(mem_obj->gpu_addr),
 		(void *)&(mem_obj->cpu_ptr), false);
 
 	return retval;
@@ -3049,7 +3050,7 @@ static void deallocate_hiq_sdma_mqd(struct kfd_node *dev,
 {
 	WARN(!mqd, "No hiq sdma mqd trunk to free");
 
-	amdgpu_amdkfd_free_gtt_mem(dev->adev, &mqd->gtt_mem);
+	amdgpu_amdkfd_free_kernel_mem(dev->adev, &mqd->mem);
 }
 
 void device_queue_manager_uninit(struct device_queue_manager *dqm)
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c +7 -7
index d9ae854b6908..f78b249e1a41 100644
@@ -54,7 +54,7 @@ struct kfd_mem_obj *allocate_hiq_mqd(struct kfd_node *dev, struct queue_properti
 	if (!mqd_mem_obj)
 		return NULL;
 
-	mqd_mem_obj->gtt_mem = dev->dqm->hiq_sdma_mqd.gtt_mem;
+	mqd_mem_obj->mem = dev->dqm->hiq_sdma_mqd.mem;
 	mqd_mem_obj->gpu_addr = dev->dqm->hiq_sdma_mqd.gpu_addr;
 	mqd_mem_obj->cpu_ptr = dev->dqm->hiq_sdma_mqd.cpu_ptr;
 
@@ -79,7 +79,7 @@ struct kfd_mem_obj *allocate_sdma_mqd(struct kfd_node *dev,
 	offset += dev->dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]->mqd_size *
 		  NUM_XCC(dev->xcc_mask);
 
-	mqd_mem_obj->gtt_mem = (void *)((uint64_t)dev->dqm->hiq_sdma_mqd.gtt_mem
+	mqd_mem_obj->mem = (void *)((uint64_t)dev->dqm->hiq_sdma_mqd.mem
 				+ offset);
 	mqd_mem_obj->gpu_addr = dev->dqm->hiq_sdma_mqd.gpu_addr + offset;
 	mqd_mem_obj->cpu_ptr = (uint32_t *)((uint64_t)
@@ -91,7 +91,7 @@ struct kfd_mem_obj *allocate_sdma_mqd(struct kfd_node *dev,
 void free_mqd_hiq_sdma(struct mqd_manager *mm, void *mqd,
 			struct kfd_mem_obj *mqd_mem_obj)
 {
-	WARN_ON(!mqd_mem_obj->gtt_mem);
+	WARN_ON(!mqd_mem_obj->mem);
 	kfree(mqd_mem_obj);
 }
 
@@ -224,8 +224,8 @@ int kfd_destroy_mqd_cp(struct mqd_manager *mm, void *mqd,
 void kfd_free_mqd_cp(struct mqd_manager *mm, void *mqd,
 	      struct kfd_mem_obj *mqd_mem_obj)
 {
-	if (mqd_mem_obj->gtt_mem) {
-		amdgpu_amdkfd_free_gtt_mem(mm->dev->adev, &mqd_mem_obj->gtt_mem);
+	if (mqd_mem_obj->mem) {
+		amdgpu_amdkfd_free_kernel_mem(mm->dev->adev, &mqd_mem_obj->mem);
 		kfree(mqd_mem_obj);
 	} else {
 		kfd_gtt_sa_free(mm->dev, mqd_mem_obj);
@@ -280,8 +280,8 @@ void kfd_get_hiq_xcc_mqd(struct kfd_node *dev, struct kfd_mem_obj *mqd_mem_obj,
 
 	offset = kfd_hiq_mqd_stride(dev) * virtual_xcc_id;
 
-	mqd_mem_obj->gtt_mem = (virtual_xcc_id == 0) ?
-			dev->dqm->hiq_sdma_mqd.gtt_mem : NULL;
+	mqd_mem_obj->mem = (virtual_xcc_id == 0) ?
+			dev->dqm->hiq_sdma_mqd.mem : NULL;
 	mqd_mem_obj->gpu_addr = dev->dqm->hiq_sdma_mqd.gpu_addr + offset;
 	mqd_mem_obj->cpu_ptr = (uint32_t *)((uintptr_t)
 				dev->dqm->hiq_sdma_mqd.cpu_ptr + offset);
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v12_1.c +2 -2
index 06ecc86fcb4c..22f51df6f174 100644
@@ -370,8 +370,8 @@ static void get_xcc_mqd(struct kfd_mem_obj *mqd_mem_obj,
 			       struct kfd_mem_obj *xcc_mqd_mem_obj,
 			       uint64_t offset)
 {
-	xcc_mqd_mem_obj->gtt_mem = (offset == 0) ?
-					mqd_mem_obj->gtt_mem : NULL;
+	xcc_mqd_mem_obj->mem = (offset == 0) ?
+					mqd_mem_obj->mem : NULL;
 	xcc_mqd_mem_obj->gpu_addr = mqd_mem_obj->gpu_addr + offset;
 	xcc_mqd_mem_obj->cpu_ptr = (uint32_t *)((uintptr_t)mqd_mem_obj->cpu_ptr
 						+ offset);
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +5 -4
index 2e9b6bcf2704..d234db138182 100644
@@ -135,11 +135,12 @@ static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node,
 		mqd_mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
 		if (!mqd_mem_obj)
 			return NULL;
-		retval = amdgpu_amdkfd_alloc_gtt_mem(node->adev,
+		retval = amdgpu_amdkfd_alloc_kernel_mem(node->adev,
 			(ALIGN(q->ctl_stack_size, PAGE_SIZE) +
 			ALIGN(sizeof(struct v9_mqd), PAGE_SIZE)) *
 			NUM_XCC(node->xcc_mask),
-			&(mqd_mem_obj->gtt_mem),
+			AMDGPU_GEM_DOMAIN_GTT,
+			&(mqd_mem_obj->mem),
 			&(mqd_mem_obj->gpu_addr),
 			(void *)&(mqd_mem_obj->cpu_ptr), true);
 
@@ -665,8 +666,8 @@ static void get_xcc_mqd(struct kfd_mem_obj *mqd_mem_obj,
 			       struct kfd_mem_obj *xcc_mqd_mem_obj,
 			       uint64_t offset)
 {
-	xcc_mqd_mem_obj->gtt_mem = (offset == 0) ?
-					mqd_mem_obj->gtt_mem : NULL;
+	xcc_mqd_mem_obj->mem = (offset == 0) ?
+					mqd_mem_obj->mem : NULL;
 	xcc_mqd_mem_obj->gpu_addr = mqd_mem_obj->gpu_addr + offset;
 	xcc_mqd_mem_obj->cpu_ptr = (uint32_t *)((uintptr_t)mqd_mem_obj->cpu_ptr
 						+ offset);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -1
index ebc637c38c04..06cd675c9e74 100644
@@ -252,7 +252,7 @@ struct kfd_mem_obj {
 	uint32_t range_end;
 	uint64_t gpu_addr;
 	uint32_t *cpu_ptr;
-	void *gtt_mem;
+	void *mem;
 };
 
 struct kfd_vmid_info {
drivers/gpu/drm/amd/amdkfd/kfd_process.c +1 -1
index 2a72dc95cc0f..79b72ee8c8a1 100644
@@ -1131,7 +1131,7 @@ static void kfd_process_destroy_pdds(struct kfd_process *p)
 
 		if (pdd->dev->kfd->shared_resources.enable_mes &&
 			pdd->proc_ctx_cpu_ptr)
-			amdgpu_amdkfd_free_gtt_mem(pdd->dev->adev,
+			amdgpu_amdkfd_free_kernel_mem(pdd->dev->adev,
 						   &pdd->proc_ctx_bo);
 		/*
 		 * before destroying pdd, make sure to report availability
drivers/gpu/drm/amd/amdkfd/kfd_process_queue_manager.c +7 -5
index 5f8cda4733f9..232103742712 100644
@@ -209,8 +209,8 @@ static void pqm_clean_queue_resource(struct process_queue_manager *pqm,
 	}
 
 	if (dev->kfd->shared_resources.enable_mes) {
-		amdgpu_amdkfd_free_gtt_mem(dev->adev, &pqn->q->gang_ctx_bo);
-		amdgpu_amdkfd_free_gtt_mem(dev->adev, (void **)&pqn->q->wptr_bo_gart);
+		amdgpu_amdkfd_free_kernel_mem(dev->adev, &pqn->q->gang_ctx_bo);
+		amdgpu_amdkfd_free_kernel_mem(dev->adev, (void **)&pqn->q->wptr_bo_gart);
 	}
 }
 
@@ -264,8 +264,9 @@ static int init_user_queue(struct process_queue_manager *pqm,
 	(*q)->process = pqm->process;
 
 	if (dev->kfd->shared_resources.enable_mes) {
-		retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev,
+		retval = amdgpu_amdkfd_alloc_kernel_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,
@@ -297,7 +298,7 @@ static int init_user_queue(struct process_queue_manager *pqm,
 	return 0;
 
 free_gang_ctx_bo:
-	amdgpu_amdkfd_free_gtt_mem(dev->adev, &(*q)->gang_ctx_bo);
+	amdgpu_amdkfd_free_kernel_mem(dev->adev, &(*q)->gang_ctx_bo);
 cleanup:
 	uninit_queue(*q);
 	*q = NULL;
@@ -367,8 +368,9 @@ int pqm_create_queue(struct process_queue_manager *pqm,
 
 	/* Allocate proc_ctx_bo only if MES is enabled and this is the first queue */
 	if (!pdd->proc_ctx_cpu_ptr && dev->kfd->shared_resources.enable_mes) {
-		retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev,
+		retval = amdgpu_amdkfd_alloc_kernel_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
#4 lore raw

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

In reply to Philip Yang

To reduce queue switch latency further, move MQD to VRAM domain, CP access MQD and control stack via FB aperture, this requires 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 090d17911bc4..113c058cf7b5 100644
@@ -329,7 +329,8 @@ int amdgpu_amdkfd_alloc_kernel_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 d234db138182..14123e1a9716 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->mem),
 			&(mqd_mem_obj->gpu_addr),
 			(void *)&(mqd_mem_obj->cpu_ptr), true);
Signature · 1 line
2.50.1
#5 lore raw

Re: [PATCH v6 4/6] drm/amdkfd: Move gfx9 MQD to VRAM domain

Can this patch be stand alone ? I think it needs be combined with patch 6.

Quoted 15 lines · Philip Yang

To reduce queue switch latency further, move MQD to VRAM domain, CP access MQD and control stack via FB aperture, this requires 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 +0 -0
index 090d17911bc4..113c058cf7b5 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c int amdgpu_amdkfd_alloc_kernel_mem(struct amdgpu_device *adev, size_t size, · line 333

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +2 -1
@@ -329,7 +329,8 @@ int amdgpu_amdkfd_alloc_kernel_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;

Should bp.flags setting depend on domain type: when domain is AMDGPU_GEM_DOMAIN_VRAM then bp.flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS?

Regards

Xiaogang

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +0 -0
  	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 d234db138182..14123e1a9716 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->mem),
  			&(mqd_mem_obj->gpu_addr),
  			(void *)&(mqd_mem_obj->cpu_ptr), true);
#6 lore raw

Re: [PATCH v6 4/6] drm/amdkfd: Move gfx9 MQD to VRAM domain

Quoted 4 lines · Xiaogang

Can this patch be stand alone ? I think it needs be combined with patch 6.

This patch alloc MQD on VRAM, without GART mapping, FW will access MQD via fb aperture address, with mtype UC. This works fine then patch 6 add GART mapping with mtype RW to improve performance.

Quoted 11 lines · Philip Yang
Quoted 10 lines

To reduce queue switch latency further, move MQD to VRAM domain, CP access MQD and control stack via FB aperture, this requires 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(-)
Quoted 14 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c int amdgpu_amdkfd_alloc_kernel_mem(struct amdgpu_device *adev, size_t size, · line 333

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +2 -1
index 090d17911bc4..113c058cf7b5 100644
@@ -329,7 +329,8 @@ int amdgpu_amdkfd_alloc_kernel_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;

Should bp.flags setting depend on domain type: when domain is AMDGPU_GEM_DOMAIN_VRAM then bp.flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS?

AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS only used by VRAM buddy allocator, we combine GTT and VRAM allocation flags in other places too, use domain to decide the mm. Regards, Philip

Quoted 21 lines

Regards

Xiaogang

Quoted 16 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.c +0 -0
  	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 d234db138182..14123e1a9716 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->mem),
  			&(mqd_mem_obj->gpu_addr),
  			(void *)&(mqd_mem_obj->cpu_ptr), true);
#7 lore raw

PATCH v6 5/6 drm/amdgpu: Add helper to alloc GART entries

In reply to Philip Yang

Add helper amdgpu_gtt_mgr_alloc/free_entries, define GART_ENTRY_WITOUT_BO_COLOR color for GART node not allocated with GTT bo, then amdgpu_gtt_mgr_recover skip those mm_node.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 48 +++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     |  6 +++
 2 files changed, 54 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +48 -0
index 895c1e4c6747..285177070895 100644
@@ -26,6 +26,8 @@
 
 #include "amdgpu.h"
 
+#define GART_ENTRY_WITOUT_BO_COLOR	1
+
 static inline struct amdgpu_gtt_mgr *
 to_gtt_mgr(struct ttm_resource_manager *man)
 {
@@ -180,6 +182,49 @@ static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
 	kfree(node);
 }
 
+/**
+ * amdgpu_gtt_mgr_alloc_entries - alloc GART entries without GTT bo
+ *
+ * @mgr: The GTT manager object
+ * @mm_node: The drm mm node to return the new allocation node information
+ * @num_pages: The number of pages for the new allocation
+ * @mode: The new allocation mode
+ *
+ * Helper to dynamic alloc GART entries to map memory not accociated with
+ * GTT BO, for example VRAM BO physical memory, remote physical memory.
+ */
+int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *mm_node,
+				 u64 num_pages,
+				 enum drm_mm_insert_mode mode)
+{
+	struct amdgpu_device *adev = container_of(mgr, typeof(*adev), mman.gtt_mgr);
+	int r;
+
+	spin_lock(&mgr->lock);
+	r = drm_mm_insert_node_in_range(&mgr->mm, mm_node, num_pages,
+					0, GART_ENTRY_WITOUT_BO_COLOR, 0,
+					adev->gmc.gart_size >> PAGE_SHIFT,
+					mode);
+	spin_unlock(&mgr->lock);
+	return r;
+}
+
+/**
+ * amdgpu_gtt_mgr_free_entries - free GART entries not accocaited with GTT bo
+ *
+ * @mgr: The GTT manager object
+ * @mm_node: The drm mm node to free
+ */
+void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *mm_node)
+{
+	spin_lock(&mgr->lock);
+	if (drm_mm_node_allocated(mm_node))
+		drm_mm_remove_node(mm_node);
+	spin_unlock(&mgr->lock);
+}
+
 /**
  * amdgpu_gtt_mgr_recover - re-init gart
  *
@@ -196,6 +241,9 @@ void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr)
 	adev = container_of(mgr, typeof(*adev), mman.gtt_mgr);
 	spin_lock(&mgr->lock);
 	drm_mm_for_each_node(mm_node, &mgr->mm) {
+		if (mm_node->color == GART_ENTRY_WITOUT_BO_COLOR)
+			continue;
+
 		node = container_of(mm_node, typeof(*node), mm_nodes[0]);
 		amdgpu_ttm_recover_gart(node->base.bo);
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +6 -0
index 72488124aa59..25640bed7dc9 100644
@@ -141,6 +141,12 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev);
 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
 void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
 
+int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *node,
+				 u64 num_pages,
+				 enum drm_mm_insert_mode mode);
+void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *mm_node);
 uint64_t amdgpu_preempt_mgr_usage(struct ttm_resource_manager *man);
 
 u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo);
Signature · 1 line
2.50.1
maintainer Reviewed-by on v6 5/6
#8 lore raw

Re: [PATCH v6 5/6] drm/amdgpu: Add helper to alloc GART entries

Quoted 5 lines · Philip Yang

Add helper amdgpu_gtt_mgr_alloc/free_entries, define GART_ENTRY_WITOUT_BO_COLOR color for GART node not allocated with GTT bo, then amdgpu_gtt_mgr_recover skip those mm_node.

Quoted 95 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 48 +++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     |  6 +++
 2 files changed, 54 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +48 -0
index 895c1e4c6747..285177070895 100644
@@ -26,6 +26,8 @@
 
 #include "amdgpu.h"
 
+#define GART_ENTRY_WITOUT_BO_COLOR	1
+
 static inline struct amdgpu_gtt_mgr *
 to_gtt_mgr(struct ttm_resource_manager *man)
 {
@@ -180,6 +182,49 @@ static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
 	kfree(node);
 }
 
+/**
+ * amdgpu_gtt_mgr_alloc_entries - alloc GART entries without GTT bo
+ *
+ * @mgr: The GTT manager object
+ * @mm_node: The drm mm node to return the new allocation node information
+ * @num_pages: The number of pages for the new allocation
+ * @mode: The new allocation mode
+ *
+ * Helper to dynamic alloc GART entries to map memory not accociated with
+ * GTT BO, for example VRAM BO physical memory, remote physical memory.
+ */
+int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *mm_node,
+				 u64 num_pages,
+				 enum drm_mm_insert_mode mode)
+{
+	struct amdgpu_device *adev = container_of(mgr, typeof(*adev), mman.gtt_mgr);
+	int r;
+
+	spin_lock(&mgr->lock);
+	r = drm_mm_insert_node_in_range(&mgr->mm, mm_node, num_pages,
+					0, GART_ENTRY_WITOUT_BO_COLOR, 0,
+					adev->gmc.gart_size >> PAGE_SHIFT,
+					mode);
+	spin_unlock(&mgr->lock);
+	return r;
+}
+
+/**
+ * amdgpu_gtt_mgr_free_entries - free GART entries not accocaited with GTT bo
+ *
+ * @mgr: The GTT manager object
+ * @mm_node: The drm mm node to free
+ */
+void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *mm_node)
+{
+	spin_lock(&mgr->lock);
+	if (drm_mm_node_allocated(mm_node))
+		drm_mm_remove_node(mm_node);
+	spin_unlock(&mgr->lock);
+}
+
 /**
  * amdgpu_gtt_mgr_recover - re-init gart
  *
@@ -196,6 +241,9 @@ void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr)
 	adev = container_of(mgr, typeof(*adev), mman.gtt_mgr);
 	spin_lock(&mgr->lock);
 	drm_mm_for_each_node(mm_node, &mgr->mm) {
+		if (mm_node->color == GART_ENTRY_WITOUT_BO_COLOR)
+			continue;
+
 		node = container_of(mm_node, typeof(*node), mm_nodes[0]);
 		amdgpu_ttm_recover_gart(node->base.bo);
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +6 -0
index 72488124aa59..25640bed7dc9 100644
@@ -141,6 +141,12 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev);
 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
 void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
 
+int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *node,
+				 u64 num_pages,
+				 enum drm_mm_insert_mode mode);
+void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *mm_node);
 uint64_t amdgpu_preempt_mgr_usage(struct ttm_resource_manager *man);
 
 u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo);
Reviewed-by on v6 5/6
#9 lore raw

Re: [PATCH v6 5/6] drm/amdgpu: Add helper to alloc GART entries

In reply to Philip Yang

Hi,

Le 15/12/2025 à 17:56, Philip Yang a écrit :

Quoted 90 lines

Add helper amdgpu_gtt_mgr_alloc/free_entries, define GART_ENTRY_WITOUT_BO_COLOR color for GART node not allocated with GTT bo, then amdgpu_gtt_mgr_recover skip those mm_node.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 48 +++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     |  6 +++
  2 files changed, 54 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +48 -0
index 895c1e4c6747..285177070895 100644
@@ -26,6 +26,8 @@
 
  #include "amdgpu.h"
 
+#define GART_ENTRY_WITOUT_BO_COLOR	1
+
  static inline struct amdgpu_gtt_mgr *
  to_gtt_mgr(struct ttm_resource_manager *man)
  {
@@ -180,6 +182,49 @@ static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
  	kfree(node);
  }
 
+/**
+ * amdgpu_gtt_mgr_alloc_entries - alloc GART entries without GTT bo
+ *
+ * @mgr: The GTT manager object
+ * @mm_node: The drm mm node to return the new allocation node information
+ * @num_pages: The number of pages for the new allocation
+ * @mode: The new allocation mode
+ *
+ * Helper to dynamic alloc GART entries to map memory not accociated with
+ * GTT BO, for example VRAM BO physical memory, remote physical memory.
+ */
+int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *mm_node,
+				 u64 num_pages,
+				 enum drm_mm_insert_mode mode)
+{
+	struct amdgpu_device *adev = container_of(mgr, typeof(*adev), mman.gtt_mgr);
+	int r;
+
+	spin_lock(&mgr->lock);
+	r = drm_mm_insert_node_in_range(&mgr->mm, mm_node, num_pages,
+					0, GART_ENTRY_WITOUT_BO_COLOR, 0,
+					adev->gmc.gart_size >> PAGE_SHIFT,
+					mode);
+	spin_unlock(&mgr->lock);
+	return r;
+}
+
+/**
+ * amdgpu_gtt_mgr_free_entries - free GART entries not accocaited with GTT bo
+ *
+ * @mgr: The GTT manager object
+ * @mm_node: The drm mm node to free
+ */
+void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *mm_node)
+{
+	spin_lock(&mgr->lock);
+	if (drm_mm_node_allocated(mm_node))
+		drm_mm_remove_node(mm_node);
+	spin_unlock(&mgr->lock);
+}
+
  /**
   * amdgpu_gtt_mgr_recover - re-init gart
   *
@@ -196,6 +241,9 @@ void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr)
  	adev = container_of(mgr, typeof(*adev), mman.gtt_mgr);
  	spin_lock(&mgr->lock);
  	drm_mm_for_each_node(mm_node, &mgr->mm) {
+		if (mm_node->color == GART_ENTRY_WITOUT_BO_COLOR)
+			continue;
+
  		node = container_of(mm_node, typeof(*node), mm_nodes[0]);
  		amdgpu_ttm_recover_gart(node->base.bo);
  	}
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +0 -0
index 72488124aa59..25640bed7dc9 100644
@@ -141,6 +141,12 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev);
  bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
  void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h void amdgpu_vram_mgr_fini(struct amdgpu_device *adev); · line 152

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +6 -0
 
+int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *node,
+				 u64 num_pages,
+				 enum drm_mm_insert_mode mode);
+void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr,
+				 struct drm_mm_node *mm_node);
  uint64_t amdgpu_preempt_mgr_usage(struct ttm_resource_manager *man);
 
  u64 amdgpu_vram_mgr_bo_visible_size(struct amdgpu_bo *bo);

Thanks!

#10 lore raw

PATCH v6 6/6 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

Add GART mm_node to kfd mem obj to free the GART entries after MQD mem obj is freed.

Use resource cursor to handle VRAM resource which maybe on multiple blocks and use cursor_gart to handle GART entries.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 94 +++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  4 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c  |  2 +
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  9 ++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  1 +
 5 files changed, 109 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +94 -0
index 4f8bc7f35cdc..d7bf96a7b6b2 100644
@@ -880,6 +880,67 @@ static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,
 	}
 }
 
+/*
+ * Same function and MQD description from amdgpu_ttm_gart_bind_gfx9_mqd,
+ * except this is for MQD on VRAM BO and use dynamic alloc GART entries.
+ */
+static void amdgpu_ttm_gart_bind_gfx9_mqd_vram(struct amdgpu_device *adev,
+				struct ttm_buffer_object *tbo,
+				struct drm_mm_node *mm_node,
+				uint64_t flags)
+{
+	uint64_t total_pages;
+	int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp);
+	uint64_t page_idx, pages_per_xcc;
+	struct amdgpu_res_cursor cursor_gart;
+	struct amdgpu_res_cursor cursor;
+	uint64_t ctrl_flags = flags;
+	int i;
+
+	total_pages = tbo->base.size >> PAGE_SHIFT;
+
+	amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_NC, &ctrl_flags);
+
+	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 3))
+		amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_RW, &flags);
+
+	pages_per_xcc = total_pages;
+	do_div(pages_per_xcc, num_xcc);
+
+	amdgpu_res_first(NULL, mm_node->start, total_pages, &cursor_gart);
+	amdgpu_res_first(tbo->resource, 0, tbo->resource->size, &cursor);
+
+	for (i = 0, page_idx = 0; i < num_xcc; i++, page_idx += pages_per_xcc) {
+		u64 start_page;
+		u64 npages, n;
+		u64 pa;
+
+		start_page = cursor_gart.start;
+		pa = cursor.start + adev->vm_manager.vram_base_offset;
+		n = 1;
+		amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+					   flags, NULL);
+
+		npages = pages_per_xcc - 1;
+		while (npages) {
+			amdgpu_res_next(&cursor_gart, n);
+			amdgpu_res_next(&cursor, n * PAGE_SIZE);
+
+			start_page = cursor_gart.start;
+			pa = cursor.start + adev->vm_manager.vram_base_offset;
+			n = min3(cursor.size / PAGE_SIZE, cursor_gart.size, npages);
+
+			amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+						   ctrl_flags, NULL);
+
+			npages -= n;
+		}
+
+		amdgpu_res_next(&cursor_gart, n);
+		amdgpu_res_next(&cursor, n * PAGE_SIZE);
+	}
+}
+
 static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
 				 struct ttm_buffer_object *tbo,
 				 uint64_t flags)
@@ -1017,6 +1078,39 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
 	return 0;
 }
 
+/*
+ * amdgpu_ttm_alloc_gart_vram_bo - Bind VRAM pages to GART mapping
+ *
+ * call amdgpu_ttm_alloc_gart_entries to alloc GART dynamically
+ */
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+				  struct drm_mm_node *mm_node,
+				  u64 *gpu_addr)
+{
+	struct ttm_buffer_object *bo = &abo->tbo;
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+	uint64_t flags;
+	int r;
+
+	/* Only for valid VRAM bo resource */
+	if (amdgpu_mem_type_to_domain(bo->resource->mem_type) !=
+	    AMDGPU_GEM_DOMAIN_VRAM)
+		return 0;
+
+	r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr, mm_node,
+					 amdgpu_bo_ngpu_pages(abo), 0);
+	if (r)
+		return r;
+
+	/* compute PTE flags for this buffer object */
+	flags = amdgpu_ttm_tt_pte_flags(adev, NULL, bo->resource);
+	amdgpu_ttm_gart_bind_gfx9_mqd_vram(adev, bo, mm_node, flags);
+	amdgpu_gart_invalidate_tlb(adev);
+
+	*gpu_addr = mm_node->start << PAGE_SHIFT;
+	return 0;
+}
+
 /*
  * amdgpu_ttm_recover_gart - Rebind GTT pages
  *
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -1
index 25640bed7dc9..9f07856433fd 100644
@@ -140,7 +140,6 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev);
 
 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
 void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
-
 int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
 				 struct drm_mm_node *node,
 				 u64 num_pages,
@@ -191,6 +190,9 @@ int amdgpu_fill_buffer(struct amdgpu_ttm_buffer_entity *entity,
 		       u64 k_job_id);
 
 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+				  struct drm_mm_node *mm_node,
+				  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.c +2 -0
index f78b249e1a41..edb72f4ef82d 100644
@@ -225,6 +225,8 @@ void kfd_free_mqd_cp(struct mqd_manager *mm, void *mqd,
 	      struct kfd_mem_obj *mqd_mem_obj)
 {
 	if (mqd_mem_obj->mem) {
+		amdgpu_gtt_mgr_free_entries(&mm->dev->adev->mman.gtt_mgr,
+					    &mqd_mem_obj->mm_node);
 		amdgpu_amdkfd_free_kernel_mem(mm->dev->adev, &mqd_mem_obj->mem);
 		kfree(mqd_mem_obj);
 	} else {
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +9 -0
index 14123e1a9716..5828220056bd 100644
@@ -148,6 +148,15 @@ 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->mem,
+						       &mqd_mem_obj->mm_node,
+						       &(mqd_mem_obj->gpu_addr));
+		if (retval) {
+			amdgpu_amdkfd_free_kernel_mem(node->adev, &(mqd_mem_obj->mem));
+			kfree(mqd_mem_obj);
+			return NULL;
+		}
 	} else {
 		retval = kfd_gtt_sa_allocate(node, sizeof(struct v9_mqd),
 				&mqd_mem_obj);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index 06cd675c9e74..55738b30c2ec 100644
@@ -253,6 +253,7 @@ struct kfd_mem_obj {
 	uint64_t gpu_addr;
 	uint32_t *cpu_ptr;
 	void *mem;
+	struct drm_mm_node mm_node;
 };
 
 struct kfd_vmid_info {
Signature · 1 line
2.50.1
maintainer
#11 lore raw

Re: [PATCH v6 6/6] drm/amdkfd: Map VRAM MQD on GART

Quoted 91 lines · 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

Add GART mm_node to kfd mem obj to free the GART entries after MQD mem obj is freed.

Use resource cursor to handle VRAM resource which maybe on multiple blocks and use cursor_gart to handle GART entries.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 94 +++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  4 +-
 drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c  |  2 +
 .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  9 ++
 drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  1 +
 5 files changed, 109 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +61 -0
index 4f8bc7f35cdc..d7bf96a7b6b2 100644
@@ -880,6 +880,67 @@ static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,
 	}
 }
 
+/*
+ * Same function and MQD description from amdgpu_ttm_gart_bind_gfx9_mqd,
+ * except this is for MQD on VRAM BO and use dynamic alloc GART entries.
+ */
+static void amdgpu_ttm_gart_bind_gfx9_mqd_vram(struct amdgpu_device *adev,
+				struct ttm_buffer_object *tbo,
+				struct drm_mm_node *mm_node,
+				uint64_t flags)
+{
+	uint64_t total_pages;
+	int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp);
+	uint64_t page_idx, pages_per_xcc;
+	struct amdgpu_res_cursor cursor_gart;
+	struct amdgpu_res_cursor cursor;
+	uint64_t ctrl_flags = flags;
+	int i;
+
+	total_pages = tbo->base.size >> PAGE_SHIFT;
+
+	amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_NC, &ctrl_flags);
+
+	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 3))
+		amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_RW, &flags);
+
+	pages_per_xcc = total_pages;
+	do_div(pages_per_xcc, num_xcc);
+
+	amdgpu_res_first(NULL, mm_node->start, total_pages, &cursor_gart);
+	amdgpu_res_first(tbo->resource, 0, tbo->resource->size, &cursor);
+
+	for (i = 0, page_idx = 0; i < num_xcc; i++, page_idx += pages_per_xcc) {
+		u64 start_page;
+		u64 npages, n;
+		u64 pa;
+
+		start_page = cursor_gart.start;
+		pa = cursor.start + adev->vm_manager.vram_base_offset;
+		n = 1;
+		amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+					   flags, NULL);
+
+		npages = pages_per_xcc - 1;
+		while (npages) {
+			amdgpu_res_next(&cursor_gart, n);
+			amdgpu_res_next(&cursor, n * PAGE_SIZE);
+
+			start_page = cursor_gart.start;
+			pa = cursor.start + adev->vm_manager.vram_base_offset;
+			n = min3(cursor.size / PAGE_SIZE, cursor_gart.size, npages);
+
+			amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+						   ctrl_flags, NULL);
+
+			npages -= n;
+		}
+
+		amdgpu_res_next(&cursor_gart, n);
+		amdgpu_res_next(&cursor, n * PAGE_SIZE);
+	}
+}
+
 static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
 				 struct ttm_buffer_object *tbo,
 				 uint64_t flags)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo) · line 1086

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +6 -0
@@ -1017,6 +1078,39 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
 	return 0;
 }
 
+/*
+ * amdgpu_ttm_alloc_gart_vram_bo - Bind VRAM pages to GART mapping
+ *
+ * call amdgpu_ttm_alloc_gart_entries to alloc GART dynamically
+ */
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,

That need a better name, something noting that it is mqd specific.

Potentially just merge the code into amdgpu_ttm_gart_bind_gfx9_mqd_vram() and keep that name.

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo) · line 1098

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +12 -0
+				  struct drm_mm_node *mm_node,
+				  u64 *gpu_addr)
+{
+	struct ttm_buffer_object *bo = &abo->tbo;
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+	uint64_t flags;
+	int r;
+
+	/* Only for valid VRAM bo resource */
+	if (amdgpu_mem_type_to_domain(bo->resource->mem_type) !=
+	    AMDGPU_GEM_DOMAIN_VRAM)
+		return 0;

Please drop that check.

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo) · line 1110

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +12 -0
+
+	r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr, mm_node,
+					 amdgpu_bo_ngpu_pages(abo), 0);
+	if (r)
+		return r;
+
+	/* compute PTE flags for this buffer object */
+	flags = amdgpu_ttm_tt_pte_flags(adev, NULL, bo->resource);
+	amdgpu_ttm_gart_bind_gfx9_mqd_vram(adev, bo, mm_node, flags);
+	amdgpu_gart_invalidate_tlb(adev);
+
+	*gpu_addr = mm_node->start << PAGE_SHIFT;

And a helper for this would be better. Maybe we should move all the gtt_mgr functions into a separate header like we did for the vram_mgr as well.

But that can come later on.

Regards,
Christian.

Quoted 73 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+	return 0;
+}
+
 /*
  * amdgpu_ttm_recover_gart - Rebind GTT pages
  *
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -1
index 25640bed7dc9..9f07856433fd 100644
@@ -140,7 +140,6 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev);
 
 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
 void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
-
 int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
 				 struct drm_mm_node *node,
 				 u64 num_pages,
@@ -191,6 +190,9 @@ int amdgpu_fill_buffer(struct amdgpu_ttm_buffer_entity *entity,
 		       u64 k_job_id);
 
 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+				  struct drm_mm_node *mm_node,
+				  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.c +2 -0
index f78b249e1a41..edb72f4ef82d 100644
@@ -225,6 +225,8 @@ void kfd_free_mqd_cp(struct mqd_manager *mm, void *mqd,
 	      struct kfd_mem_obj *mqd_mem_obj)
 {
 	if (mqd_mem_obj->mem) {
+		amdgpu_gtt_mgr_free_entries(&mm->dev->adev->mman.gtt_mgr,
+					    &mqd_mem_obj->mm_node);
 		amdgpu_amdkfd_free_kernel_mem(mm->dev->adev, &mqd_mem_obj->mem);
 		kfree(mqd_mem_obj);
 	} else {
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +9 -0
index 14123e1a9716..5828220056bd 100644
@@ -148,6 +148,15 @@ 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->mem,
+						       &mqd_mem_obj->mm_node,
+						       &(mqd_mem_obj->gpu_addr));
+		if (retval) {
+			amdgpu_amdkfd_free_kernel_mem(node->adev, &(mqd_mem_obj->mem));
+			kfree(mqd_mem_obj);
+			return NULL;
+		}
 	} else {
 		retval = kfd_gtt_sa_allocate(node, sizeof(struct v9_mqd),
 				&mqd_mem_obj);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index 06cd675c9e74..55738b30c2ec 100644
@@ -253,6 +253,7 @@ struct kfd_mem_obj {
 	uint64_t gpu_addr;
 	uint32_t *cpu_ptr;
 	void *mem;
+	struct drm_mm_node mm_node;
 };
 
 struct kfd_vmid_info {
#12 lore raw

Re: [PATCH v6 6/6] drm/amdkfd: Map VRAM MQD on GART

Quoted 103 lines · Christian König
Quoted 91 lines · 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

Add GART mm_node to kfd mem obj to free the GART entries after MQD mem obj is freed.

Use resource cursor to handle VRAM resource which maybe on multiple blocks and use cursor_gart to handle GART entries.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 94 +++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  4 +-
  drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c  |  2 +
  .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  9 ++
  drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  1 +
  5 files changed, 109 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +61 -0
index 4f8bc7f35cdc..d7bf96a7b6b2 100644
@@ -880,6 +880,67 @@ static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,
  	}
  }
 
+/*
+ * Same function and MQD description from amdgpu_ttm_gart_bind_gfx9_mqd,
+ * except this is for MQD on VRAM BO and use dynamic alloc GART entries.
+ */
+static void amdgpu_ttm_gart_bind_gfx9_mqd_vram(struct amdgpu_device *adev,
+				struct ttm_buffer_object *tbo,
+				struct drm_mm_node *mm_node,
+				uint64_t flags)
+{
+	uint64_t total_pages;
+	int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp);
+	uint64_t page_idx, pages_per_xcc;
+	struct amdgpu_res_cursor cursor_gart;
+	struct amdgpu_res_cursor cursor;
+	uint64_t ctrl_flags = flags;
+	int i;
+
+	total_pages = tbo->base.size >> PAGE_SHIFT;
+
+	amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_NC, &ctrl_flags);
+
+	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 3))
+		amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_RW, &flags);
+
+	pages_per_xcc = total_pages;
+	do_div(pages_per_xcc, num_xcc);
+
+	amdgpu_res_first(NULL, mm_node->start, total_pages, &cursor_gart);
+	amdgpu_res_first(tbo->resource, 0, tbo->resource->size, &cursor);
+
+	for (i = 0, page_idx = 0; i < num_xcc; i++, page_idx += pages_per_xcc) {
+		u64 start_page;
+		u64 npages, n;
+		u64 pa;
+
+		start_page = cursor_gart.start;
+		pa = cursor.start + adev->vm_manager.vram_base_offset;
+		n = 1;
+		amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+					   flags, NULL);
+
+		npages = pages_per_xcc - 1;
+		while (npages) {
+			amdgpu_res_next(&cursor_gart, n);
+			amdgpu_res_next(&cursor, n * PAGE_SIZE);
+
+			start_page = cursor_gart.start;
+			pa = cursor.start + adev->vm_manager.vram_base_offset;
+			n = min3(cursor.size / PAGE_SIZE, cursor_gart.size, npages);
+
+			amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+						   ctrl_flags, NULL);
+
+			npages -= n;
+		}
+
+		amdgpu_res_next(&cursor_gart, n);
+		amdgpu_res_next(&cursor, n * PAGE_SIZE);
+	}
+}
+
  static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
  				 struct ttm_buffer_object *tbo,
  				 uint64_t flags)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo) · line 1086

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +6 -0
@@ -1017,6 +1078,39 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
  	return 0;
  }
 
+/*
+ * amdgpu_ttm_alloc_gart_vram_bo - Bind VRAM pages to GART mapping
+ *
+ * call amdgpu_ttm_alloc_gart_entries to alloc GART dynamically
+ */
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,

That need a better name, something noting that it is mqd specific.

Quoted 1 line

Potentially just merge the code into amdgpu_ttm_gart_bind_gfx9_mqd_vram() and keep that name.

Looks better to merge this function into amdgpu_ttm_gart_bind_gfx9_mqd_vram, then don't need this function name.

Quoted 14 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo) · line 1098

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +12 -0
+				  struct drm_mm_node *mm_node,
+				  u64 *gpu_addr)
+{
+	struct ttm_buffer_object *bo = &abo->tbo;
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+	uint64_t flags;
+	int r;
+
+	/* Only for valid VRAM bo resource */
+	if (amdgpu_mem_type_to_domain(bo->resource->mem_type) !=
+	    AMDGPU_GEM_DOMAIN_VRAM)
+		return 0;

Please drop that check.

done.

Quoted 15 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo) · line 1110

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +12 -0
+
+	r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr, mm_node,
+					 amdgpu_bo_ngpu_pages(abo), 0);
+	if (r)
+		return r;
+
+	/* compute PTE flags for this buffer object */
+	flags = amdgpu_ttm_tt_pte_flags(adev, NULL, bo->resource);
+	amdgpu_ttm_gart_bind_gfx9_mqd_vram(adev, bo, mm_node, flags);
+	amdgpu_gart_invalidate_tlb(adev);
+
+	*gpu_addr = mm_node->start << PAGE_SHIFT;

And a helper for this would be better. Maybe we should move all the gtt_mgr functions into a separate header like we did for the vram_mgr as well.

Quoted 1 line

But that can come later on.

mm_node->start still use in many places, will add helper later.

Regards,
Philip

Quoted 78 lines

Regards,
Christian.

Quoted 73 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+	return 0;
+}
+
  /*
   * amdgpu_ttm_recover_gart - Rebind GTT pages
   *
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -1
index 25640bed7dc9..9f07856433fd 100644
@@ -140,7 +140,6 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev);
 
  bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
  void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
-
  int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
  				 struct drm_mm_node *node,
  				 u64 num_pages,
@@ -191,6 +190,9 @@ int amdgpu_fill_buffer(struct amdgpu_ttm_buffer_entity *entity,
  		       u64 k_job_id);
 
  int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+				  struct drm_mm_node *mm_node,
+				  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.c +2 -0
index f78b249e1a41..edb72f4ef82d 100644
@@ -225,6 +225,8 @@ void kfd_free_mqd_cp(struct mqd_manager *mm, void *mqd,
  	      struct kfd_mem_obj *mqd_mem_obj)
  {
  	if (mqd_mem_obj->mem) {
+		amdgpu_gtt_mgr_free_entries(&mm->dev->adev->mman.gtt_mgr,
+					    &mqd_mem_obj->mm_node);
  		amdgpu_amdkfd_free_kernel_mem(mm->dev->adev, &mqd_mem_obj->mem);
  		kfree(mqd_mem_obj);
  	} else {
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +9 -0
index 14123e1a9716..5828220056bd 100644
@@ -148,6 +148,15 @@ 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->mem,
+						       &mqd_mem_obj->mm_node,
+						       &(mqd_mem_obj->gpu_addr));
+		if (retval) {
+			amdgpu_amdkfd_free_kernel_mem(node->adev, &(mqd_mem_obj->mem));
+			kfree(mqd_mem_obj);
+			return NULL;
+		}
  	} else {
  		retval = kfd_gtt_sa_allocate(node, sizeof(struct v9_mqd),
  				&mqd_mem_obj);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index 06cd675c9e74..55738b30c2ec 100644
@@ -253,6 +253,7 @@ struct kfd_mem_obj {
  	uint64_t gpu_addr;
  	uint32_t *cpu_ptr;
  	void *mem;
+	struct drm_mm_node mm_node;
  };
 
  struct kfd_vmid_info {
#13 lore raw

Re: [PATCH v6 6/6] drm/amdkfd: Map VRAM MQD on GART

In reply to Philip Yang
Quoted 44 lines · 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

Add GART mm_node to kfd mem obj to free the GART entries after MQD mem obj is freed.

Use resource cursor to handle VRAM resource which maybe on multiple blocks and use cursor_gart to handle GART entries.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 94 +++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  4 +-
  drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c  |  2 +
  .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  9 ++
  drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  1 +
  5 files changed, 109 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +18 -0
index 4f8bc7f35cdc..d7bf96a7b6b2 100644
@@ -880,6 +880,67 @@ static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,
  	}
  }
 
+/*
+ * Same function and MQD description from amdgpu_ttm_gart_bind_gfx9_mqd,
+ * except this is for MQD on VRAM BO and use dynamic alloc GART entries.
+ */
+static void amdgpu_ttm_gart_bind_gfx9_mqd_vram(struct amdgpu_device *adev,
+				struct ttm_buffer_object *tbo,
+				struct drm_mm_node *mm_node,
+				uint64_t flags)
+{
+	uint64_t total_pages;
+	int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp);
+	uint64_t page_idx, pages_per_xcc;
+	struct amdgpu_res_cursor cursor_gart;
+	struct amdgpu_res_cursor cursor;
+	uint64_t ctrl_flags = flags;
+	int i;
+
+	total_pages = tbo->base.size >> PAGE_SHIFT;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev, · line 910

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+
+	amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_NC, &ctrl_flags);
+
+	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 3))
+		amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_RW, &flags);
+
+	pages_per_xcc = total_pages;
+	do_div(pages_per_xcc, num_xcc);
+
+	amdgpu_res_first(NULL, mm_node->start, total_pages, &cursor_gart);

no need use cursor_gar. mm_node->start + n indicates where to update gart page table.

Quoted 116 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +66 -0
+	amdgpu_res_first(tbo->resource, 0, tbo->resource->size, &cursor);
+
+	for (i = 0, page_idx = 0; i < num_xcc; i++, page_idx += pages_per_xcc) {
+		u64 start_page;
+		u64 npages, n;
+		u64 pa;
+
+		start_page = cursor_gart.start;
+		pa = cursor.start + adev->vm_manager.vram_base_offset;
+		n = 1;
+		amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+					   flags, NULL);
+
+		npages = pages_per_xcc - 1;
+		while (npages) {
+			amdgpu_res_next(&cursor_gart, n);
+			amdgpu_res_next(&cursor, n * PAGE_SIZE);
+
+			start_page = cursor_gart.start;
+			pa = cursor.start + adev->vm_manager.vram_base_offset;
+			n = min3(cursor.size / PAGE_SIZE, cursor_gart.size, npages);
+
+			amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+						   ctrl_flags, NULL);
+
+			npages -= n;
+		}
+
+		amdgpu_res_next(&cursor_gart, n);
+		amdgpu_res_next(&cursor, n * PAGE_SIZE);
+	}
+}
+
  static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
  				 struct ttm_buffer_object *tbo,
  				 uint64_t flags)
@@ -1017,6 +1078,39 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
  	return 0;
  }
 
+/*
+ * amdgpu_ttm_alloc_gart_vram_bo - Bind VRAM pages to GART mapping
+ *
+ * call amdgpu_ttm_alloc_gart_entries to alloc GART dynamically
+ */
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+				  struct drm_mm_node *mm_node,
+				  u64 *gpu_addr)
+{
+	struct ttm_buffer_object *bo = &abo->tbo;
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+	uint64_t flags;
+	int r;
+
+	/* Only for valid VRAM bo resource */
+	if (amdgpu_mem_type_to_domain(bo->resource->mem_type) !=
+	    AMDGPU_GEM_DOMAIN_VRAM)
+		return 0;
+
+	r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr, mm_node,
+					 amdgpu_bo_ngpu_pages(abo), 0);
+	if (r)
+		return r;
+
+	/* compute PTE flags for this buffer object */
+	flags = amdgpu_ttm_tt_pte_flags(adev, NULL, bo->resource);
+	amdgpu_ttm_gart_bind_gfx9_mqd_vram(adev, bo, mm_node, flags);
+	amdgpu_gart_invalidate_tlb(adev);
+
+	*gpu_addr = mm_node->start << PAGE_SHIFT;
+	return 0;
+}
+
  /*
   * amdgpu_ttm_recover_gart - Rebind GTT pages
   *
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -1
index 25640bed7dc9..9f07856433fd 100644
@@ -140,7 +140,6 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev);
 
  bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
  void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
-
  int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
  				 struct drm_mm_node *node,
  				 u64 num_pages,
@@ -191,6 +190,9 @@ int amdgpu_fill_buffer(struct amdgpu_ttm_buffer_entity *entity,
  		       u64 k_job_id);
 
  int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+				  struct drm_mm_node *mm_node,
+				  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.c +2 -0
index f78b249e1a41..edb72f4ef82d 100644
@@ -225,6 +225,8 @@ void kfd_free_mqd_cp(struct mqd_manager *mm, void *mqd,
  	      struct kfd_mem_obj *mqd_mem_obj)
  {
  	if (mqd_mem_obj->mem) {
+		amdgpu_gtt_mgr_free_entries(&mm->dev->adev->mman.gtt_mgr,
+					    &mqd_mem_obj->mm_node);
  		amdgpu_amdkfd_free_kernel_mem(mm->dev->adev, &mqd_mem_obj->mem);
  		kfree(mqd_mem_obj);
  	} else {
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +0 -0
index 14123e1a9716..5828220056bd 100644

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node, · line 154

Quoted 7 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +4 -0
@@ -148,6 +148,15 @@ 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->mem,
+						       &mqd_mem_obj->mm_node,
+						       &(mqd_mem_obj->gpu_addr));

Here you create new drm_mm_node for gart page table entries. Before that amdgpu_amdkfd_alloc_kernel_mem also creates gart page table entries and drm_mm_node. Is there duplication or how do you handle the entries/drm_mm_node from amdgpu_amdkfd_alloc_kernel_mem?

Regards

Xiaogang

Quoted 20 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +5 -0
+		if (retval) {
+			amdgpu_amdkfd_free_kernel_mem(node->adev, &(mqd_mem_obj->mem));
+			kfree(mqd_mem_obj);
+			return NULL;
+		}
  	} else {
  		retval = kfd_gtt_sa_allocate(node, sizeof(struct v9_mqd),
  				&mqd_mem_obj);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index 06cd675c9e74..55738b30c2ec 100644
@@ -253,6 +253,7 @@ struct kfd_mem_obj {
  	uint64_t gpu_addr;
  	uint32_t *cpu_ptr;
  	void *mem;
+	struct drm_mm_node mm_node;
  };
 
  struct kfd_vmid_info {
#14 lore raw

Re: [PATCH v6 6/6] drm/amdkfd: Map VRAM MQD on GART

Quoted 50 lines · Xiaogang
Quoted 47 lines · 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

Add GART mm_node to kfd mem obj to free the GART entries after MQD mem obj is freed.

Use resource cursor to handle VRAM resource which maybe on multiple blocks and use cursor_gart to handle GART entries.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 94 +++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  4 +-
  drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c  |  2 +
  .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  9 ++
  drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  1 +
  5 files changed, 109 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +21 -0
index 4f8bc7f35cdc..d7bf96a7b6b2 100644
@@ -880,6 +880,67 @@ static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,
  	}
  }
 
+/*
+ * Same function and MQD description from amdgpu_ttm_gart_bind_gfx9_mqd,
+ * except this is for MQD on VRAM BO and use dynamic alloc GART entries.
+ */
+static void amdgpu_ttm_gart_bind_gfx9_mqd_vram(struct amdgpu_device *adev,
+				struct ttm_buffer_object *tbo,
+				struct drm_mm_node *mm_node,
+				uint64_t flags)
+{
+	uint64_t total_pages;
+	int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp);
+	uint64_t page_idx, pages_per_xcc;
+	struct amdgpu_res_cursor cursor_gart;
+	struct amdgpu_res_cursor cursor;
+	uint64_t ctrl_flags = flags;
+	int i;
+
+	total_pages = tbo->base.size >> PAGE_SHIFT;
+
+	amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_NC, &ctrl_flags);
+
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev, · line 910

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -0
+	if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 3))
+		amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_RW, &flags);
+
+	pages_per_xcc = total_pages;
+	do_div(pages_per_xcc, num_xcc);
+
+	amdgpu_res_first(NULL, mm_node->start, total_pages, &cursor_gart);

no need use cursor_gar. mm_node->start + n indicates where to update gart page table.

yes, that is the last version, change to use resource cursor for both gart entries and VRAM pages

Quoted 117 lines
Quoted 117 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +66 -0
+	amdgpu_res_first(tbo->resource, 0, tbo->resource->size, &cursor);
+
+	for (i = 0, page_idx = 0; i < num_xcc; i++, page_idx += pages_per_xcc) {
+		u64 start_page;
+		u64 npages, n;
+		u64 pa;
+
+		start_page = cursor_gart.start;
+		pa = cursor.start + adev->vm_manager.vram_base_offset;
+		n = 1;
+		amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+					   flags, NULL);
+
+		npages = pages_per_xcc - 1;
+		while (npages) {
+			amdgpu_res_next(&cursor_gart, n);
+			amdgpu_res_next(&cursor, n * PAGE_SIZE);
+
+			start_page = cursor_gart.start;
+			pa = cursor.start + adev->vm_manager.vram_base_offset;
+			n = min3(cursor.size / PAGE_SIZE, cursor_gart.size, npages);
+
+			amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+						   ctrl_flags, NULL);
+
+			npages -= n;
+		}
+
+		amdgpu_res_next(&cursor_gart, n);
+		amdgpu_res_next(&cursor, n * PAGE_SIZE);
+	}
+}
+
  static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
  				 struct ttm_buffer_object *tbo,
  				 uint64_t flags)
@@ -1017,6 +1078,39 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
  	return 0;
  }
 
+/*
+ * amdgpu_ttm_alloc_gart_vram_bo - Bind VRAM pages to GART mapping
+ *
+ * call amdgpu_ttm_alloc_gart_entries to alloc GART dynamically
+ */
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+				  struct drm_mm_node *mm_node,
+				  u64 *gpu_addr)
+{
+	struct ttm_buffer_object *bo = &abo->tbo;
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+	uint64_t flags;
+	int r;
+
+	/* Only for valid VRAM bo resource */
+	if (amdgpu_mem_type_to_domain(bo->resource->mem_type) !=
+	    AMDGPU_GEM_DOMAIN_VRAM)
+		return 0;
+
+	r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr, mm_node,
+					 amdgpu_bo_ngpu_pages(abo), 0);
+	if (r)
+		return r;
+
+	/* compute PTE flags for this buffer object */
+	flags = amdgpu_ttm_tt_pte_flags(adev, NULL, bo->resource);
+	amdgpu_ttm_gart_bind_gfx9_mqd_vram(adev, bo, mm_node, flags);
+	amdgpu_gart_invalidate_tlb(adev);
+
+	*gpu_addr = mm_node->start << PAGE_SHIFT;
+	return 0;
+}
+
  /*
   * amdgpu_ttm_recover_gart - Rebind GTT pages
   *
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -1
index 25640bed7dc9..9f07856433fd 100644
@@ -140,7 +140,6 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device *adev);
 
  bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
  void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
-
  int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
  				 struct drm_mm_node *node,
  				 u64 num_pages,
@@ -191,6 +190,9 @@ int amdgpu_fill_buffer(struct amdgpu_ttm_buffer_entity *entity,
  		       u64 k_job_id);
 
  int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+				  struct drm_mm_node *mm_node,
+				  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.c +2 -0
index f78b249e1a41..edb72f4ef82d 100644
@@ -225,6 +225,8 @@ void kfd_free_mqd_cp(struct mqd_manager *mm, void *mqd,
  	      struct kfd_mem_obj *mqd_mem_obj)
  {
  	if (mqd_mem_obj->mem) {
+		amdgpu_gtt_mgr_free_entries(&mm->dev->adev->mman.gtt_mgr,
+					    &mqd_mem_obj->mm_node);
  		amdgpu_amdkfd_free_kernel_mem(mm->dev->adev, &mqd_mem_obj->mem);
  		kfree(mqd_mem_obj);
  	} else {
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +0 -0
index 14123e1a9716..5828220056bd 100644
@@ -148,6 +148,15 @@ static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node,
  			kfree(mqd_mem_obj);
Quoted 9 lines

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node, · line 154

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +4 -0
  			return NULL;
  		}
+
+		retval = amdgpu_ttm_alloc_gart_vram_bo(mqd_mem_obj->mem,
+						       &mqd_mem_obj->mm_node,
+						       &(mqd_mem_obj->gpu_addr));

Here you create new drm_mm_node for gart page table entries. Before that amdgpu_amdkfd_alloc_kernel_mem also creates gart page table entries and drm_mm_node. Is there duplication or how do you handle the entries/drm_mm_node from amdgpu_amdkfd_alloc_kernel_mem?

amdgpu_ttm_alloc_gart allocate GART entries for GTT domain only.

Regards,
Philip

Quoted 25 lines

Regards

Xiaogang

Quoted 20 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +5 -0
+		if (retval) {
+			amdgpu_amdkfd_free_kernel_mem(node->adev, &(mqd_mem_obj->mem));
+			kfree(mqd_mem_obj);
+			return NULL;
+		}
  	} else {
  		retval = kfd_gtt_sa_allocate(node, sizeof(struct v9_mqd),
  				&mqd_mem_obj);
drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index 06cd675c9e74..55738b30c2ec 100644
@@ -253,6 +253,7 @@ struct kfd_mem_obj {
  	uint64_t gpu_addr;
  	uint32_t *cpu_ptr;
  	void *mem;
+	struct drm_mm_node mm_node;
  };
 
  struct kfd_vmid_info {
#15 lore raw

Re: [PATCH v6 6/6] drm/amdkfd: Map VRAM MQD on GART

Quoted 197 lines · Philip Yang
Quoted 56 lines · Xiaogang
Quoted 53 lines · 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

Add GART mm_node to kfd mem obj to free the GART entries after MQD mem obj is freed.

Use resource cursor to handle VRAM resource which maybe on multiple blocks and use cursor_gart to handle GART entries.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 94
+++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  4 +- drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c  |  2 + .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  9 ++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  1 +

  5 files changed, 109 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
index 4f8bc7f35cdc..d7bf96a7b6b2 100644
@@ -880,6 +880,67 @@ static void 
 amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,
       }
   }

  +/*
+ * Same function and MQD description from amdgpu_ttm_gart_bind_gfx9_mqd,
+ * except this is for MQD on VRAM BO and use dynamic alloc GART entries.
+ */
+static void amdgpu_ttm_gart_bind_gfx9_mqd_vram(struct amdgpu_device *adev,
+                struct ttm_buffer_object *tbo,
+                struct drm_mm_node *mm_node,
+                uint64_t flags)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +12 -0
+{
+    uint64_t total_pages;
+    int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp);
+    uint64_t page_idx, pages_per_xcc;
+    struct amdgpu_res_cursor cursor_gart;
+    struct amdgpu_res_cursor cursor;
+    uint64_t ctrl_flags = flags;
+    int i;
+
+    total_pages = tbo->base.size >> PAGE_SHIFT;
+
+    amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_NC, 

&ctrl_flags);
+
+    if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 3))

Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev, · line 910

Quoted 7 lines

+        amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_RW, &flags);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -0
+
+    pages_per_xcc = total_pages;
+    do_div(pages_per_xcc, num_xcc);
+
+    amdgpu_res_first(NULL, mm_node->start, total_pages, &cursor_gart);

no need use cursor_gar. mm_node->start + n indicates where to update gart page table.

yes, that is the last version, change to use resource cursor for both gart entries and VRAM pages

Quoted 120 lines
Quoted 120 lines

+    amdgpu_res_first(tbo->resource, 0, tbo->resource->size, &cursor); +
+    for (i = 0, page_idx = 0; i < num_xcc; i++, page_idx += pages_per_xcc) {
+        u64 start_page;
+        u64 npages, n;
+        u64 pa;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +15 -0
+
+        start_page = cursor_gart.start;
+        pa = cursor.start + adev->vm_manager.vram_base_offset;
+        n = 1;
+        amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+                       flags, NULL);
+
+        npages = pages_per_xcc - 1;
+        while (npages) {
+            amdgpu_res_next(&cursor_gart, n);
+            amdgpu_res_next(&cursor, n * PAGE_SIZE);
+
+            start_page = cursor_gart.start;
+            pa = cursor.start + adev->vm_manager.vram_base_offset;
+            n = min3(cursor.size / PAGE_SIZE, cursor_gart.size, 

npages);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +12 -0
+
+            amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+                           ctrl_flags, NULL);
+
+            npages -= n;
+        }
+
+        amdgpu_res_next(&cursor_gart, n);
+        amdgpu_res_next(&cursor, n * PAGE_SIZE);
+    }
+}
+

  static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev, struct ttm_buffer_object *tbo, uint64_t flags)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1017,6 +1078,39 @@ int amdgpu_ttm_alloc_gart(struct 
 ttm_buffer_object *bo)
       return 0;
   }

  +/*
+ * amdgpu_ttm_alloc_gart_vram_bo - Bind VRAM pages to GART mapping
+ *
+ * call amdgpu_ttm_alloc_gart_entries to alloc GART dynamically
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +28 -0
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+                  struct drm_mm_node *mm_node,
+                  u64 *gpu_addr)
+{
+    struct ttm_buffer_object *bo = &abo->tbo;
+    struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+    uint64_t flags;
+    int r;
+
+    /* Only for valid VRAM bo resource */
+    if (amdgpu_mem_type_to_domain(bo->resource->mem_type) !=
+        AMDGPU_GEM_DOMAIN_VRAM)
+        return 0;
+
+    r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr, mm_node,
+                     amdgpu_bo_ngpu_pages(abo), 0);
+    if (r)
+        return r;
+
+    /* compute PTE flags for this buffer object */
+    flags = amdgpu_ttm_tt_pte_flags(adev, NULL, bo->resource);
+    amdgpu_ttm_gart_bind_gfx9_mqd_vram(adev, bo, mm_node, flags);
+    amdgpu_gart_invalidate_tlb(adev);
+
+    *gpu_addr = mm_node->start << PAGE_SHIFT;
+    return 0;
+}
+

  /*
   * amdgpu_ttm_recover_gart - Rebind GTT pages
   *

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -1
index 25640bed7dc9..9f07856433fd 100644
@@ -140,7 +140,6 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device 
 *adev);
     bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
   void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
-
   int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
                    struct drm_mm_node *node,
                    u64 num_pages,
@@ -191,6 +190,9 @@ int amdgpu_fill_buffer(struct 
 amdgpu_ttm_buffer_entity *entity,
                  u64 k_job_id);
     int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+                  struct drm_mm_node *mm_node,
+                  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);

  diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c b/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c index f78b249e1a41..edb72f4ef82d 100644

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c +2 -0
@@ -225,6 +225,8 @@ void kfd_free_mqd_cp(struct mqd_manager *mm, 
 void *mqd,
             struct kfd_mem_obj *mqd_mem_obj)
   {
       if (mqd_mem_obj->mem) {
+ amdgpu_gtt_mgr_free_entries(&mm->dev->adev->mman.gtt_mgr,
+                        &mqd_mem_obj->mm_node);
           amdgpu_amdkfd_free_kernel_mem(mm->dev->adev, &mqd_mem_obj->mem);
           kfree(mqd_mem_obj);

      } else {

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +0 -0
index 14123e1a9716..5828220056bd 100644
@@ -148,6 +148,15 @@ static struct kfd_mem_obj *allocate_mqd(struct 
 kfd_node *node,
               kfree(mqd_mem_obj);
Quoted 9 lines

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node, · line 154

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +4 -0
               return NULL;
           }
+
+        retval = amdgpu_ttm_alloc_gart_vram_bo(mqd_mem_obj->mem,
+                               &mqd_mem_obj->mm_node,
+ &(mqd_mem_obj->gpu_addr));

Here you create new drm_mm_node for gart page table entries. Before that amdgpu_amdkfd_alloc_kernel_mem also creates gart page table entries and drm_mm_node. Is there duplication or how do you handle the entries/drm_mm_node from amdgpu_amdkfd_alloc_kernel_mem?

Quoted 1 line

amdgpu_ttm_alloc_gart allocate GART entries for GTT domain only.

Why still need keep GART entries for GTT domain if use vram bo through GART here?

Regards

Xiaogang

Quoted 29 lines

Regards,
Philip

Quoted 25 lines

Regards

Xiaogang

Quoted 20 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +5 -0
+        if (retval) {
+            amdgpu_amdkfd_free_kernel_mem(node->adev, 
 &(mqd_mem_obj->mem));
+            kfree(mqd_mem_obj);
+            return NULL;
+        }
       } else {

          retval = kfd_gtt_sa_allocate(node, sizeof(struct v9_mqd), &mqd_mem_obj);

drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index 06cd675c9e74..55738b30c2ec 100644
@@ -253,6 +253,7 @@ struct kfd_mem_obj {
       uint64_t gpu_addr;
       uint32_t *cpu_ptr;
       void *mem;
+    struct drm_mm_node mm_node;
   };
     struct kfd_vmid_info {
#16 lore raw

Re: [PATCH v6 6/6] drm/amdkfd: Map VRAM MQD on GART

Quoted 200 lines · Xiaogang
Quoted 198 lines · Philip Yang
Quoted 57 lines · Xiaogang
Quoted 54 lines · 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

Add GART mm_node to kfd mem obj to free the GART entries after MQD mem obj is freed.

Use resource cursor to handle VRAM resource which maybe on multiple blocks and use cursor_gart to handle GART entries.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 94
+++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  4 +- drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c  |  2 + .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  9 ++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  1 +

  5 files changed, 109 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
index 4f8bc7f35cdc..d7bf96a7b6b2 100644
@@ -880,6 +880,67 @@ static void 
 amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,
       }
   }

  +/*
+ * Same function and MQD description from amdgpu_ttm_gart_bind_gfx9_mqd,
+ * except this is for MQD on VRAM BO and use dynamic alloc GART entries.
+ */
+static void amdgpu_ttm_gart_bind_gfx9_mqd_vram(struct amdgpu_device *adev,
+                struct ttm_buffer_object *tbo,
+                struct drm_mm_node *mm_node,
+                uint64_t flags)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +12 -0
+{
+    uint64_t total_pages;
+    int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp);
+    uint64_t page_idx, pages_per_xcc;
+    struct amdgpu_res_cursor cursor_gart;
+    struct amdgpu_res_cursor cursor;
+    uint64_t ctrl_flags = flags;
+    int i;
+
+    total_pages = tbo->base.size >> PAGE_SHIFT;
+
+    amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_NC, 

&ctrl_flags);
+
+    if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 3)) +        amdgpu_gmc_get_vm_pte(adev, NULL, NULL,

Quoted 8 lines
Quoted 7 lines

AMDGPU_VM_MTYPE_RW, &flags);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -0
+
+    pages_per_xcc = total_pages;
+    do_div(pages_per_xcc, num_xcc);
+
+    amdgpu_res_first(NULL, mm_node->start, total_pages, 

&cursor_gart);

no need use cursor_gar. mm_node->start + n indicates where to update gart page table.

yes, that is the last version, change to use resource cursor for both gart entries and VRAM pages

Quoted 129 lines
Quoted 118 lines

+ amdgpu_res_first(tbo->resource, 0, tbo->resource->size, &cursor); +
+    for (i = 0, page_idx = 0; i < num_xcc; i++, page_idx += pages_per_xcc) {
+        u64 start_page;
+        u64 npages, n;
+        u64 pa;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +15 -0
+
+        start_page = cursor_gart.start;
+        pa = cursor.start + adev->vm_manager.vram_base_offset;
+        n = 1;
+        amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+                       flags, NULL);
+
+        npages = pages_per_xcc - 1;
+        while (npages) {
+            amdgpu_res_next(&cursor_gart, n);
+            amdgpu_res_next(&cursor, n * PAGE_SIZE);
+
+            start_page = cursor_gart.start;
+            pa = cursor.start + adev->vm_manager.vram_base_offset;
+            n = min3(cursor.size / PAGE_SIZE, cursor_gart.size, 

npages);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +12 -0
+
+            amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+                           ctrl_flags, NULL);
+
+            npages -= n;
+        }
+
+        amdgpu_res_next(&cursor_gart, n);
+        amdgpu_res_next(&cursor, n * PAGE_SIZE);
+    }
+}
+

  static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev, struct ttm_buffer_object *tbo, uint64_t flags)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1017,6 +1078,39 @@ int amdgpu_ttm_alloc_gart(struct 
 ttm_buffer_object *bo)
       return 0;
   }

  +/*
+ * amdgpu_ttm_alloc_gart_vram_bo - Bind VRAM pages to GART mapping
+ *
+ * call amdgpu_ttm_alloc_gart_entries to alloc GART dynamically
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +28 -0
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+                  struct drm_mm_node *mm_node,
+                  u64 *gpu_addr)
+{
+    struct ttm_buffer_object *bo = &abo->tbo;
+    struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+    uint64_t flags;
+    int r;
+
+    /* Only for valid VRAM bo resource */
+    if (amdgpu_mem_type_to_domain(bo->resource->mem_type) !=
+        AMDGPU_GEM_DOMAIN_VRAM)
+        return 0;
+
+    r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr, mm_node,
+                     amdgpu_bo_ngpu_pages(abo), 0);
+    if (r)
+        return r;
+
+    /* compute PTE flags for this buffer object */
+    flags = amdgpu_ttm_tt_pte_flags(adev, NULL, bo->resource);
+    amdgpu_ttm_gart_bind_gfx9_mqd_vram(adev, bo, mm_node, flags);
+    amdgpu_gart_invalidate_tlb(adev);
+
+    *gpu_addr = mm_node->start << PAGE_SHIFT;
+    return 0;
+}
+

  /*
   * amdgpu_ttm_recover_gart - Rebind GTT pages
   *

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -1
index 25640bed7dc9..9f07856433fd 100644
@@ -140,7 +140,6 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device 
 *adev);
     bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
   void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
-
   int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
                    struct drm_mm_node *node,
                    u64 num_pages,
@@ -191,6 +190,9 @@ int amdgpu_fill_buffer(struct 
 amdgpu_ttm_buffer_entity *entity,
                  u64 k_job_id);
     int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+                  struct drm_mm_node *mm_node,
+                  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);

  diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c b/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c index f78b249e1a41..edb72f4ef82d 100644

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c +2 -0
@@ -225,6 +225,8 @@ void kfd_free_mqd_cp(struct mqd_manager *mm, 
 void *mqd,
             struct kfd_mem_obj *mqd_mem_obj)
   {
       if (mqd_mem_obj->mem) {
+ amdgpu_gtt_mgr_free_entries(&mm->dev->adev->mman.gtt_mgr,
+                        &mqd_mem_obj->mm_node);
           amdgpu_amdkfd_free_kernel_mem(mm->dev->adev, &mqd_mem_obj->mem);
           kfree(mqd_mem_obj);

      } else {

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +0 -0
index 14123e1a9716..5828220056bd 100644

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c static struct kfd_mem_obj *allocate_mqd(struct · line 155

Quoted 8 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +4 -0
@@ -148,6 +148,15 @@ 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->mem,
+ &mqd_mem_obj->mm_node,
+ &(mqd_mem_obj->gpu_addr));

Here you create new drm_mm_node for gart page table entries. Before that amdgpu_amdkfd_alloc_kernel_mem also creates gart page table entries and drm_mm_node. Is there duplication or how do you handle the entries/drm_mm_node from amdgpu_amdkfd_alloc_kernel_mem?

Quoted 3 lines
Quoted 1 line

amdgpu_ttm_alloc_gart allocate GART entries for GTT domain only.

Why still need keep GART entries for GTT domain if use vram bo through GART here?

If MQD allocated on VRAM, amdgpu_ttm_alloc_gart will not alloc GART entries because this check

    if (bo->resource->start != AMDGPU_BO_INVALID_OFFSET) return 0;

bo->resource->start is AMDGPU_BO_INVALID_OFFSET only for GTT bo resource, VRAM bo->resource->start has valid value.

Regards,
Philip

Quoted 34 lines

Regards

Xiaogang

Quoted 29 lines

Regards,
Philip

Quoted 25 lines

Regards

Xiaogang

Quoted 20 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +5 -0
+        if (retval) {
+            amdgpu_amdkfd_free_kernel_mem(node->adev, 
 &(mqd_mem_obj->mem));
+            kfree(mqd_mem_obj);
+            return NULL;
+        }
       } else {

          retval = kfd_gtt_sa_allocate(node, sizeof(struct v9_mqd), &mqd_mem_obj);

drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index 06cd675c9e74..55738b30c2ec 100644
@@ -253,6 +253,7 @@ struct kfd_mem_obj {
       uint64_t gpu_addr;
       uint32_t *cpu_ptr;
       void *mem;
+    struct drm_mm_node mm_node;
   };
     struct kfd_vmid_info {
#17 lore raw

Re: [PATCH v6 6/6] drm/amdkfd: Map VRAM MQD on GART

Quoted 205 lines · Philip Yang
Quoted 202 lines · Xiaogang
Quoted 199 lines · Philip Yang
Quoted 57 lines · Xiaogang
Quoted 54 lines · 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

Add GART mm_node to kfd mem obj to free the GART entries after MQD mem obj is freed.

Use resource cursor to handle VRAM resource which maybe on multiple blocks and use cursor_gart to handle GART entries.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       | 94
+++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h       |  4 +- drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c  |  2 + .../gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c   |  9 ++ drivers/gpu/drm/amd/amdkfd/kfd_priv.h         |  1 +

  5 files changed, 109 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
index 4f8bc7f35cdc..d7bf96a7b6b2 100644
@@ -880,6 +880,67 @@ static void 
 amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev,
       }
   }

  +/*
+ * Same function and MQD description from amdgpu_ttm_gart_bind_gfx9_mqd,
+ * except this is for MQD on VRAM BO and use dynamic alloc GART entries.
+ */
+static void amdgpu_ttm_gart_bind_gfx9_mqd_vram(struct amdgpu_device *adev,
+                struct ttm_buffer_object *tbo,
+                struct drm_mm_node *mm_node,
+                uint64_t flags)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +12 -0
+{
+    uint64_t total_pages;
+    int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp);
+    uint64_t page_idx, pages_per_xcc;
+    struct amdgpu_res_cursor cursor_gart;
+    struct amdgpu_res_cursor cursor;
+    uint64_t ctrl_flags = flags;
+    int i;
+
+    total_pages = tbo->base.size >> PAGE_SHIFT;
+
+    amdgpu_gmc_get_vm_pte(adev, NULL, NULL, AMDGPU_VM_MTYPE_NC, 

&ctrl_flags);
+
+    if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(9, 4, 3)) +        amdgpu_gmc_get_vm_pte(adev, NULL, NULL,

Quoted 8 lines
Quoted 7 lines

AMDGPU_VM_MTYPE_RW, &flags);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -0
+
+    pages_per_xcc = total_pages;
+    do_div(pages_per_xcc, num_xcc);
+
+    amdgpu_res_first(NULL, mm_node->start, total_pages, 

&cursor_gart);

no need use cursor_gar. mm_node->start + n indicates where to update gart page table.

yes, that is the last version, change to use resource cursor for both gart entries and VRAM pages

Quoted 120 lines
Quoted 120 lines

+ amdgpu_res_first(tbo->resource, 0, tbo->resource->size, &cursor); +
+    for (i = 0, page_idx = 0; i < num_xcc; i++, page_idx += pages_per_xcc) {
+        u64 start_page;
+        u64 npages, n;
+        u64 pa;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +15 -0
+
+        start_page = cursor_gart.start;
+        pa = cursor.start + adev->vm_manager.vram_base_offset;
+        n = 1;
+        amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+                       flags, NULL);
+
+        npages = pages_per_xcc - 1;
+        while (npages) {
+            amdgpu_res_next(&cursor_gart, n);
+            amdgpu_res_next(&cursor, n * PAGE_SIZE);
+
+            start_page = cursor_gart.start;
+            pa = cursor.start + adev->vm_manager.vram_base_offset;
+            n = min3(cursor.size / PAGE_SIZE, cursor_gart.size, 

npages);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +12 -0
+
+            amdgpu_gart_map_vram_range(adev, pa, start_page, n,
+                           ctrl_flags, NULL);
+
+            npages -= n;
+        }
+
+        amdgpu_res_next(&cursor_gart, n);
+        amdgpu_res_next(&cursor, n * PAGE_SIZE);
+    }
+}
+

  static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev, struct ttm_buffer_object *tbo, uint64_t flags)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1017,6 +1078,39 @@ int amdgpu_ttm_alloc_gart(struct 
 ttm_buffer_object *bo)
       return 0;
   }

  +/*
+ * amdgpu_ttm_alloc_gart_vram_bo - Bind VRAM pages to GART mapping
+ *
+ * call amdgpu_ttm_alloc_gart_entries to alloc GART dynamically
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +28 -0
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+                  struct drm_mm_node *mm_node,
+                  u64 *gpu_addr)
+{
+    struct ttm_buffer_object *bo = &abo->tbo;
+    struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+    uint64_t flags;
+    int r;
+
+    /* Only for valid VRAM bo resource */
+    if (amdgpu_mem_type_to_domain(bo->resource->mem_type) !=
+        AMDGPU_GEM_DOMAIN_VRAM)
+        return 0;
+
+    r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr, mm_node,
+                     amdgpu_bo_ngpu_pages(abo), 0);
+    if (r)
+        return r;
+
+    /* compute PTE flags for this buffer object */
+    flags = amdgpu_ttm_tt_pte_flags(adev, NULL, bo->resource);
+    amdgpu_ttm_gart_bind_gfx9_mqd_vram(adev, bo, mm_node, flags);
+    amdgpu_gart_invalidate_tlb(adev);
+
+    *gpu_addr = mm_node->start << PAGE_SHIFT;
+    return 0;
+}
+

  /*
   * amdgpu_ttm_recover_gart - Rebind GTT pages
   *

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -1
index 25640bed7dc9..9f07856433fd 100644
@@ -140,7 +140,6 @@ void amdgpu_vram_mgr_fini(struct amdgpu_device 
 *adev);
     bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem);
   void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
-
   int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
                    struct drm_mm_node *node,
                    u64 num_pages,
@@ -191,6 +190,9 @@ int amdgpu_fill_buffer(struct 
 amdgpu_ttm_buffer_entity *entity,
                  u64 k_job_id);
     int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
+int amdgpu_ttm_alloc_gart_vram_bo(struct amdgpu_bo *abo,
+                  struct drm_mm_node *mm_node,
+                  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);

  diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c b/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c index f78b249e1a41..edb72f4ef82d 100644

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager.c +2 -0
@@ -225,6 +225,8 @@ void kfd_free_mqd_cp(struct mqd_manager *mm, 
 void *mqd,
             struct kfd_mem_obj *mqd_mem_obj)
   {
       if (mqd_mem_obj->mem) {
+ amdgpu_gtt_mgr_free_entries(&mm->dev->adev->mman.gtt_mgr,
+                        &mqd_mem_obj->mm_node);
 amdgpu_amdkfd_free_kernel_mem(mm->dev->adev, &mqd_mem_obj->mem);
           kfree(mqd_mem_obj);

      } else {

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +0 -0
index 14123e1a9716..5828220056bd 100644
@@ -148,6 +148,15 @@ static struct kfd_mem_obj 
 *allocate_mqd(struct kfd_node *node,
               kfree(mqd_mem_obj);
Quoted 9 lines

drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node, · line 154

Quoted 6 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +4 -0
               return NULL;
           }
+
+        retval = amdgpu_ttm_alloc_gart_vram_bo(mqd_mem_obj->mem,
+ &mqd_mem_obj->mm_node,
+ &(mqd_mem_obj->gpu_addr));

Here you create new drm_mm_node for gart page table entries. Before that amdgpu_amdkfd_alloc_kernel_mem also creates gart page table entries and drm_mm_node. Is there duplication or how do you handle the entries/drm_mm_node from amdgpu_amdkfd_alloc_kernel_mem?

amdgpu_ttm_alloc_gart allocate GART entries for GTT domain only.

Quoted 7 lines
Quoted 1 line

Why still need keep GART entries for GTT domain if use vram bo through GART here?

If MQD allocated on VRAM, amdgpu_ttm_alloc_gart will not alloc GART entries because this check

    if (bo->resource->start != AMDGPU_BO_INVALID_OFFSET) return 0;

bo->resource->start is AMDGPU_BO_INVALID_OFFSET only for GTT bo resource, VRAM bo->resource->start has valid value.

Ok, thanks for pointing it. Then at amdgpu_amdkfd_alloc_kernel_mem we do not need call amdgpu_ttm_alloc_gart for AMDGPU_GEM_DOMAIN_VRAM domain. That made me think a GART range/drm_mm_node is created.

This using vram for MQD is not asic specific and use same logic. Can we make it a general method for all asic?

Regards

Xiaogang

Quoted 39 lines

Regards,
Philip

Quoted 34 lines

Regards

Xiaogang

Quoted 29 lines

Regards,
Philip

Quoted 25 lines

Regards

Xiaogang

Quoted 20 lines
drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c +5 -0
+        if (retval) {
+            amdgpu_amdkfd_free_kernel_mem(node->adev, 
 &(mqd_mem_obj->mem));
+            kfree(mqd_mem_obj);
+            return NULL;
+        }
       } else {

          retval = kfd_gtt_sa_allocate(node, sizeof(struct v9_mqd), &mqd_mem_obj);

drivers/gpu/drm/amd/amdkfd/kfd_priv.h +1 -0
index 06cd675c9e74..55738b30c2ec 100644
@@ -253,6 +253,7 @@ struct kfd_mem_obj {
       uint64_t gpu_addr;
       uint32_t *cpu_ptr;
       void *mem;
+    struct drm_mm_node mm_node;
   };
     struct kfd_vmid_info {