Skip to contentSkip to conversation

drm/amdgpu: Use iosys_map for trap TMA BO mappings

PATCH In review Srinivasan Shanmugam 2 participants 4 messages Sep 17

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: Use iosys_map for trap TMA BO mappings

Replace amdgpu_bo_kmap()/amdgpu_bo_kunmap() in the trap handler code with ttm_bo_vmap()/ttm_bo_vunmap().

Store the mapping as struct iosys_map and use iosys_map helpers for memory access. This aligns the trap code with the modern DRM/TTM mapping interfaces.

Using iosys_map allows the code to use common DRM mapping helpers instead of directly accessing memory through raw CPU pointers.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_trap.c | 31 +++++++++++-------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_trap.h |  2 +-
 2 files changed, 15 insertions(+), 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_trap.c +14 -17
index 29eba2968350..eb842fe8aed1 100644
@@ -427,29 +427,29 @@ int amdgpu_trap_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r)
 		goto err_exec;
 
-	/* VM resv already held — kmap directly, no separate reserve needed */
-	r = amdgpu_bo_kmap(cwsr->tma_bo, &cwsr->tma_cpu_addr);
+	/* VM resv already held, vmap directly without a separate reserve */
+	r = ttm_bo_vmap(&cwsr->tma_bo->tbo, &cwsr->tma_map);
 	if (r)
 		goto err_tma;
 
-	memset(cwsr->tma_cpu_addr, 0, AMDGPU_TRAP_TMA_MAX_SIZE);
+	iosys_map_memset(&cwsr->tma_map, 0, 0, AMDGPU_TRAP_TMA_MAX_SIZE);
 
 	r = amdgpu_trap_map_region(adev, vm, cwsr, AMDGPU_TRAP_TMA);
 	if (r)
-		goto err_kmap;
+		goto err_vmap;
 
 	r = amdgpu_trap_map_region(adev, vm, cwsr, AMDGPU_TRAP_TBA);
 	if (r) {
 		amdgpu_trap_unmap_region(adev, cwsr, AMDGPU_TRAP_TMA);
-		goto err_kmap;
+		goto err_vmap;
 	}
 
 	drm_exec_fini(&exec);
 	*trap_obj = cwsr;
 	return 0;
 
-err_kmap:
-	amdgpu_bo_kunmap(cwsr->tma_bo);
+err_vmap:
+	ttm_bo_vunmap(&cwsr->tma_bo->tbo, &cwsr->tma_map);
 err_tma:
 	amdgpu_bo_unref(&cwsr->tma_bo);
 err_exec:
@@ -524,7 +524,7 @@ void amdgpu_trap_free(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 err:
 	drm_exec_fini(&exec);
 	/* TMA BO not pinned — release CPU mapping and free */
-	amdgpu_bo_kunmap((*trap_obj)->tma_bo);
+	ttm_bo_vunmap(&(*trap_obj)->tma_bo->tbo, &(*trap_obj)->tma_map);
 	amdgpu_bo_unref(&(*trap_obj)->tma_bo);
 	kfree(*trap_obj);
 	*trap_obj = NULL;
@@ -569,24 +569,24 @@ static int amdgpu_trap_set_l2_trap_handler(struct amdgpu_device *adev, struct am
 					   struct amdgpu_trap_usr_addr *tma,
 					   struct amdgpu_trap_usr_addr *tba)
 {
-	uint64_t *l1tma;
 	int r;
 
 	if (!amdgpu_trap_is_enabled(adev))
 		return -EOPNOTSUPP;
 
-	if (!cwsr_obj || !cwsr_obj->tma_cpu_addr || !tma || !tba)
+	if (!cwsr_obj || iosys_map_is_null(&cwsr_obj->tma_map) || !tma || !tba)
 		return -EINVAL;
+
 	r = amdgpu_trap_validate_user_addr(adev, vm, tma);
 	if (r)
 		return r;
+
 	r = amdgpu_trap_validate_user_addr(adev, vm, tba);
 	if (r)
 		return r;
 
-	l1tma = (uint64_t *)(cwsr_obj->tma_cpu_addr);
-	l1tma[0] = tma->addr;
-	l1tma[1] = tba->addr;
+	iosys_map_wr(&cwsr_obj->tma_map, 0, u64, tma->addr);
+	iosys_map_wr(&cwsr_obj->tma_map, sizeof(u64), u64, tba->addr);
 
 	return 0;
 }
@@ -638,16 +638,13 @@ int amdgpu_trap_set_trap_debug_flag(struct amdgpu_device *adev,
 				    struct amdgpu_trap_obj *cwsr_obj,
 				    bool enabled)
 {
-	uint64_t *l1tma;
-
 	if (!amdgpu_trap_is_enabled(adev))
 		return -EOPNOTSUPP;
 
 	if (!cwsr_obj)
 		return -EINVAL;
 
-	l1tma = (uint64_t *)(cwsr_obj->tma_cpu_addr);
-	l1tma[2] = enabled;
+	iosys_map_wr(&cwsr_obj->tma_map, 2 * sizeof(u64), u64, enabled);
 
 	return 0;
 }
\ No newline at end of file
drivers/gpu/drm/amd/amdgpu/amdgpu_trap.h +1 -1
index 6d4664469bad..57e338c91f0a 100644
@@ -42,7 +42,7 @@ struct amdgpu_trap_obj {
 	struct amdgpu_bo *tma_bo;
 	struct amdgpu_bo_va *tba_va;
 	struct amdgpu_bo_va *tma_va;
-	void *tma_cpu_addr;
+	struct iosys_map tma_map;
 };
 
 struct amdgpu_trap_info {
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 9 lines · Srinivasan Shanmugam

Replace amdgpu_bo_kmap()/amdgpu_bo_kunmap() in the trap handler code with ttm_bo_vmap()/ttm_bo_vunmap().

Store the mapping as struct iosys_map and use iosys_map helpers for memory access. This aligns the trap code with the modern DRM/TTM mapping interfaces.

Using iosys_map allows the code to use common DRM mapping helpers instead of directly accessing memory through raw CPU pointers.

I'm really wondering if we shouldn't rework amdgpu_bo_kmap() into amdgpu_bo_vmap() and use that everywhere?

We can of course start with backward compatible return the kernel poiunter and then switch other users of amdgpu_bo_kmap() over to iosys_map one by one.

Regards,
Christian.

Quoted 120 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_trap.c | 31 +++++++++++-------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_trap.h |  2 +-
 2 files changed, 15 insertions(+), 18 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_trap.c +14 -17
index 29eba2968350..eb842fe8aed1 100644
@@ -427,29 +427,29 @@ int amdgpu_trap_alloc(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 	if (r)
 		goto err_exec;
 
-	/* VM resv already held — kmap directly, no separate reserve needed */
-	r = amdgpu_bo_kmap(cwsr->tma_bo, &cwsr->tma_cpu_addr);
+	/* VM resv already held, vmap directly without a separate reserve */
+	r = ttm_bo_vmap(&cwsr->tma_bo->tbo, &cwsr->tma_map);
 	if (r)
 		goto err_tma;
 
-	memset(cwsr->tma_cpu_addr, 0, AMDGPU_TRAP_TMA_MAX_SIZE);
+	iosys_map_memset(&cwsr->tma_map, 0, 0, AMDGPU_TRAP_TMA_MAX_SIZE);
 
 	r = amdgpu_trap_map_region(adev, vm, cwsr, AMDGPU_TRAP_TMA);
 	if (r)
-		goto err_kmap;
+		goto err_vmap;
 
 	r = amdgpu_trap_map_region(adev, vm, cwsr, AMDGPU_TRAP_TBA);
 	if (r) {
 		amdgpu_trap_unmap_region(adev, cwsr, AMDGPU_TRAP_TMA);
-		goto err_kmap;
+		goto err_vmap;
 	}
 
 	drm_exec_fini(&exec);
 	*trap_obj = cwsr;
 	return 0;
 
-err_kmap:
-	amdgpu_bo_kunmap(cwsr->tma_bo);
+err_vmap:
+	ttm_bo_vunmap(&cwsr->tma_bo->tbo, &cwsr->tma_map);
 err_tma:
 	amdgpu_bo_unref(&cwsr->tma_bo);
 err_exec:
@@ -524,7 +524,7 @@ void amdgpu_trap_free(struct amdgpu_device *adev, struct amdgpu_vm *vm,
 err:
 	drm_exec_fini(&exec);
 	/* TMA BO not pinned — release CPU mapping and free */
-	amdgpu_bo_kunmap((*trap_obj)->tma_bo);
+	ttm_bo_vunmap(&(*trap_obj)->tma_bo->tbo, &(*trap_obj)->tma_map);
 	amdgpu_bo_unref(&(*trap_obj)->tma_bo);
 	kfree(*trap_obj);
 	*trap_obj = NULL;
@@ -569,24 +569,24 @@ static int amdgpu_trap_set_l2_trap_handler(struct amdgpu_device *adev, struct am
 					   struct amdgpu_trap_usr_addr *tma,
 					   struct amdgpu_trap_usr_addr *tba)
 {
-	uint64_t *l1tma;
 	int r;
 
 	if (!amdgpu_trap_is_enabled(adev))
 		return -EOPNOTSUPP;
 
-	if (!cwsr_obj || !cwsr_obj->tma_cpu_addr || !tma || !tba)
+	if (!cwsr_obj || iosys_map_is_null(&cwsr_obj->tma_map) || !tma || !tba)
 		return -EINVAL;
+
 	r = amdgpu_trap_validate_user_addr(adev, vm, tma);
 	if (r)
 		return r;
+
 	r = amdgpu_trap_validate_user_addr(adev, vm, tba);
 	if (r)
 		return r;
 
-	l1tma = (uint64_t *)(cwsr_obj->tma_cpu_addr);
-	l1tma[0] = tma->addr;
-	l1tma[1] = tba->addr;
+	iosys_map_wr(&cwsr_obj->tma_map, 0, u64, tma->addr);
+	iosys_map_wr(&cwsr_obj->tma_map, sizeof(u64), u64, tba->addr);
 
 	return 0;
 }
@@ -638,16 +638,13 @@ int amdgpu_trap_set_trap_debug_flag(struct amdgpu_device *adev,
 				    struct amdgpu_trap_obj *cwsr_obj,
 				    bool enabled)
 {
-	uint64_t *l1tma;
-
 	if (!amdgpu_trap_is_enabled(adev))
 		return -EOPNOTSUPP;
 
 	if (!cwsr_obj)
 		return -EINVAL;
 
-	l1tma = (uint64_t *)(cwsr_obj->tma_cpu_addr);
-	l1tma[2] = enabled;
+	iosys_map_wr(&cwsr_obj->tma_map, 2 * sizeof(u64), u64, enabled);
 
 	return 0;
 }
\ No newline at end of file
drivers/gpu/drm/amd/amdgpu/amdgpu_trap.h +1 -1
index 6d4664469bad..57e338c91f0a 100644
@@ -42,7 +42,7 @@ struct amdgpu_trap_obj {
 	struct amdgpu_bo *tma_bo;
 	struct amdgpu_bo_va *tba_va;
 	struct amdgpu_bo_va *tma_va;
-	void *tma_cpu_addr;
+	struct iosys_map tma_map;
 };
 
 struct amdgpu_trap_info {
#2 lore raw
Quoted 11 lines · Christian König
Quoted 9 lines · Srinivasan Shanmugam

Replace amdgpu_bo_kmap()/amdgpu_bo_kunmap() in the trap handler code with ttm_bo_vmap()/ttm_bo_vunmap().

Store the mapping as struct iosys_map and use iosys_map helpers for memory access. This aligns the trap code with the modern DRM/TTM mapping interfaces.

Using iosys_map allows the code to use common DRM mapping helpers instead of directly accessing memory through raw CPU pointers.

I'm really wondering if we shouldn't rework amdgpu_bo_kmap() into amdgpu_bo_vmap() and use that everywhere?

Hi Christian,

Thanks, that's a very good point.

Introducing amdgpu_bo_vmap()/amdgpu_bo_vunmap() and gradually converting existing amdgpu_bo_kmap() users sounds like a useful cleanup for AMDGPU in general.

I did a quick grep and found amdgpu_bo_kmap() users in a number of AMDGPU/KFD files, so this seems broader than the trap-specific conversion.

Would it make sense to keep this trap change focused on the original conversion and handle the wider AMDGPU migration as a separate follow-up cleanup activity?

I'd be happy to look into that separately as well.

Regards,
Srini

Quoted 5 lines

We can of course start with backward compatible return the kernel poiunter and then switch other users of amdgpu_bo_kmap() over to iosys_map one by one.

Regards,
Christian.

maintainer
#3 lore raw
Quoted 18 lines · SRINIVASAN SHANMUGAM
Quoted 11 lines · Christian König
Quoted 9 lines · Srinivasan Shanmugam

Replace amdgpu_bo_kmap()/amdgpu_bo_kunmap() in the trap handler code with ttm_bo_vmap()/ttm_bo_vunmap().

Store the mapping as struct iosys_map and use iosys_map helpers for memory access. This aligns the trap code with the modern DRM/TTM mapping interfaces.

Using iosys_map allows the code to use common DRM mapping helpers instead of directly accessing memory through raw CPU pointers.

I'm really wondering if we shouldn't rework amdgpu_bo_kmap() into amdgpu_bo_vmap() and use that everywhere?

Hi Christian,

Thanks, that's a very good point.

Quoted 7 lines

Introducing amdgpu_bo_vmap()/amdgpu_bo_vunmap() and gradually converting existing amdgpu_bo_kmap() users sounds like a useful cleanup for AMDGPU in general.

I did a quick grep and found amdgpu_bo_kmap() users in a number of AMDGPU/KFD files, so this seems broader than the trap-specific conversion.

That's what I meant with a backward compatible interface. E.g. just return the kernel pointer the same way kmap does, it isn't 100% clean but still better than what we do today.

Quoted 4 lines

Would it make sense to keep this trap change focused on the original conversion and handle the wider AMDGPU migration as a separate follow-up cleanup activity?

I leave that up to you, if you feel that this is to much work feel free to add my rb to the existing patch. That one looks good enough that we can land it.

Regards,
Christian.

Quoted 11 lines

I'd be happy to look into that separately as well.

Regards,
Srini

Quoted 5 lines

We can of course start with backward compatible return the kernel poiunter and then switch other users of amdgpu_bo_kmap() over to iosys_map one by one.

Regards,
Christian.