Skip to contentSkip to conversation

drm/amdgpu: improve debug VRAM access performance using sdma

PATCH In review Jonathan Kim 5 participants 10 messages Jan 4, 2022 – Mar 21, 2023

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

PATCH 1/1 drm/amdgpu: improve debug VRAM access performance using sdma

For better performance during VRAM access for debugged processes, do read/write copies over SDMA.

In order to fulfill post mortem debugging on a broken device, fallback to stable MMIO access when gpu recovery is disabled or when job submission time outs are set to max. Failed SDMA access should automatically fall back to MMIO access.

Use a pre-allocated GTT bounce buffer pre-mapped into GART to avoid page-table updates and TLB flushes on access.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 78 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  5 +-
 2 files changed, 82 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +78 -0
index 367abed1d6e6..512df4c09772 100644
@@ -48,6 +48,7 @@
 #include <drm/ttm/ttm_range_manager.h>
 
 #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
 #include "amdgpu.h"
 #include "amdgpu_object.h"
@@ -1429,6 +1430,70 @@ static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,
 	}
 }
 
+static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
+					unsigned long offset, void *buf, int len, int write)
+{
+	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
+	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+	struct amdgpu_job *job;
+	struct dma_fence *fence;
+	uint64_t src_addr, dst_addr;
+	unsigned int num_dw;
+	int r, idx;
+
+	if (len != PAGE_SIZE)
+		return -EINVAL;
+
+	if (!adev->mman.sdma_access_ptr)
+		return -EACCES;
+
+	r = drm_dev_enter(adev_to_drm(adev), &idx);
+	if (r)
+		return r;
+
+	if (write)
+		memcpy(adev->mman.sdma_access_ptr, buf, len);
+
+	num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
+	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, AMDGPU_IB_POOL_DELAYED, &job);
+	if (r)
+		goto out;
+
+	src_addr = write ? amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo) :
+			amdgpu_bo_gpu_offset(abo);
+	dst_addr = write ? amdgpu_bo_gpu_offset(abo) :
+			amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo);
+	amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr, PAGE_SIZE, false);
+
+	amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
+	WARN_ON(job->ibs[0].length_dw > num_dw);
+
+	r = amdgpu_job_submit(job, &adev->mman.entity, AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
+	if (r) {
+		amdgpu_job_free(job);
+		goto out;
+	}
+
+	if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
+		r = -ETIMEDOUT;
+	dma_fence_put(fence);
+
+	if (!(r || write))
+		memcpy(buf, adev->mman.sdma_access_ptr, len);
+out:
+	drm_dev_exit(idx);
+	return r;
+}
+
+static inline bool amdgpu_ttm_allow_post_mortem_debug(struct amdgpu_device *adev)
+{
+	return amdgpu_gpu_recovery == 0 ||
+		adev->gfx_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->compute_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->sdma_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->video_timeout == MAX_SCHEDULE_TIMEOUT;
+}
+
 /**
  * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
  *
@@ -1453,6 +1518,10 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
 	if (bo->resource->mem_type != TTM_PL_VRAM)
 		return -EIO;
 
+	if (!amdgpu_ttm_allow_post_mortem_debug(adev) &&
+			!amdgpu_ttm_access_memory_sdma(bo, offset, buf, len, write))
+		return len;
+
 	amdgpu_res_first(bo->resource, offset, len, &cursor);
 	while (cursor.remaining) {
 		size_t count, size = cursor.size;
@@ -1793,6 +1862,12 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
 		return r;
 	}
 
+	if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
+				AMDGPU_GEM_DOMAIN_GTT,
+				&adev->mman.sdma_access_bo, NULL,
+				adev->mman.sdma_access_ptr))
+		DRM_WARN("Debug VRAM access will use slowpath MM access\n");
+
 	return 0;
 }
 
@@ -1823,6 +1898,9 @@ void amdgpu_ttm_fini(struct amdgpu_device *adev)
 	ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
 	ttm_device_fini(&adev->mman.bdev);
 	adev->mman.initialized = false;
+	if (adev->mman.sdma_access_ptr)
+		amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo, NULL,
+					&adev->mman.sdma_access_ptr);
 	DRM_INFO("amdgpu: ttm finalized\n");
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4 -1
index 91a087f9dc7c..b0116c4a768f 100644
@@ -98,6 +98,10 @@ struct amdgpu_mman {
 	u64		fw_vram_usage_size;
 	struct amdgpu_bo	*fw_vram_usage_reserved_bo;
 	void		*fw_vram_usage_va;
+
+	/* PAGE_SIZE'd BO for process memory r/w over SDMA. */
+	struct amdgpu_bo	*sdma_access_bo;
+	void			*sdma_access_ptr;
 };
 
 struct amdgpu_copy_mem {
@@ -193,5 +197,4 @@ uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
 int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int mem_type);
 
 void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
-
 #endif
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw

Am 2022-01-04 um 2:12 p.m. schrieb Jonathan Kim:

Quoted 8 lines

For better performance during VRAM access for debugged processes, do read/write copies over SDMA.

In order to fulfill post mortem debugging on a broken device, fallback to stable MMIO access when gpu recovery is disabled or when job submission time outs are set to max. Failed SDMA access should automatically fall back to MMIO access.

Quoted 4 lines

Use a pre-allocated GTT bounce buffer pre-mapped into GART to avoid page-table updates and TLB flushes on access.

Quoted 143 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 78 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  5 +-
 2 files changed, 82 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +78 -0
index 367abed1d6e6..512df4c09772 100644
@@ -48,6 +48,7 @@
 #include <drm/ttm/ttm_range_manager.h>
 
 #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
 #include "amdgpu.h"
 #include "amdgpu_object.h"
@@ -1429,6 +1430,70 @@ static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,
 	}
 }
 
+static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
+					unsigned long offset, void *buf, int len, int write)
+{
+	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
+	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+	struct amdgpu_job *job;
+	struct dma_fence *fence;
+	uint64_t src_addr, dst_addr;
+	unsigned int num_dw;
+	int r, idx;
+
+	if (len != PAGE_SIZE)
+		return -EINVAL;
+
+	if (!adev->mman.sdma_access_ptr)
+		return -EACCES;
+
+	r = drm_dev_enter(adev_to_drm(adev), &idx);
+	if (r)
+		return r;
+
+	if (write)
+		memcpy(adev->mman.sdma_access_ptr, buf, len);
+
+	num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
+	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, AMDGPU_IB_POOL_DELAYED, &job);
+	if (r)
+		goto out;
+
+	src_addr = write ? amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo) :
+			amdgpu_bo_gpu_offset(abo);
+	dst_addr = write ? amdgpu_bo_gpu_offset(abo) :
+			amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo);
+	amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr, PAGE_SIZE, false);
+
+	amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
+	WARN_ON(job->ibs[0].length_dw > num_dw);
+
+	r = amdgpu_job_submit(job, &adev->mman.entity, AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
+	if (r) {
+		amdgpu_job_free(job);
+		goto out;
+	}
+
+	if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
+		r = -ETIMEDOUT;
+	dma_fence_put(fence);
+
+	if (!(r || write))
+		memcpy(buf, adev->mman.sdma_access_ptr, len);
+out:
+	drm_dev_exit(idx);
+	return r;
+}
+
+static inline bool amdgpu_ttm_allow_post_mortem_debug(struct amdgpu_device *adev)
+{
+	return amdgpu_gpu_recovery == 0 ||
+		adev->gfx_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->compute_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->sdma_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->video_timeout == MAX_SCHEDULE_TIMEOUT;
+}
+
 /**
  * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
  *
@@ -1453,6 +1518,10 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
 	if (bo->resource->mem_type != TTM_PL_VRAM)
 		return -EIO;
 
+	if (!amdgpu_ttm_allow_post_mortem_debug(adev) &&
+			!amdgpu_ttm_access_memory_sdma(bo, offset, buf, len, write))
+		return len;
+
 	amdgpu_res_first(bo->resource, offset, len, &cursor);
 	while (cursor.remaining) {
 		size_t count, size = cursor.size;
@@ -1793,6 +1862,12 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
 		return r;
 	}
 
+	if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
+				AMDGPU_GEM_DOMAIN_GTT,
+				&adev->mman.sdma_access_bo, NULL,
+				adev->mman.sdma_access_ptr))
+		DRM_WARN("Debug VRAM access will use slowpath MM access\n");
+
 	return 0;
 }
 
@@ -1823,6 +1898,9 @@ void amdgpu_ttm_fini(struct amdgpu_device *adev)
 	ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
 	ttm_device_fini(&adev->mman.bdev);
 	adev->mman.initialized = false;
+	if (adev->mman.sdma_access_ptr)
+		amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo, NULL,
+					&adev->mman.sdma_access_ptr);
 	DRM_INFO("amdgpu: ttm finalized\n");
 }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4 -1
index 91a087f9dc7c..b0116c4a768f 100644
@@ -98,6 +98,10 @@ struct amdgpu_mman {
 	u64		fw_vram_usage_size;
 	struct amdgpu_bo	*fw_vram_usage_reserved_bo;
 	void		*fw_vram_usage_va;
+
+	/* PAGE_SIZE'd BO for process memory r/w over SDMA. */
+	struct amdgpu_bo	*sdma_access_bo;
+	void			*sdma_access_ptr;
 };
 
 struct amdgpu_copy_mem {
@@ -193,5 +197,4 @@ uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
 int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int mem_type);
 
 void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
-
 #endif
maintainer
#2 lore raw
In reply to Jonathan Kim

Am 04.01.22 um 20:12 schrieb Jonathan Kim:

Quoted 56 lines

For better performance during VRAM access for debugged processes, do read/write copies over SDMA.

In order to fulfill post mortem debugging on a broken device, fallback to stable MMIO access when gpu recovery is disabled or when job submission time outs are set to max. Failed SDMA access should automatically fall back to MMIO access.

Use a pre-allocated GTT bounce buffer pre-mapped into GART to avoid page-table updates and TLB flushes on access.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 78 +++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  5 +-
  2 files changed, 82 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +24 -0
index 367abed1d6e6..512df4c09772 100644
@@ -48,6 +48,7 @@
  #include <drm/ttm/ttm_range_manager.h>
 
  #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
  #include "amdgpu.h"
  #include "amdgpu_object.h"
@@ -1429,6 +1430,70 @@ static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,
  	}
  }
 
+static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
+					unsigned long offset, void *buf, int len, int write)
+{
+	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
+	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+	struct amdgpu_job *job;
+	struct dma_fence *fence;
+	uint64_t src_addr, dst_addr;
+	unsigned int num_dw;
+	int r, idx;
+
+	if (len != PAGE_SIZE)
+		return -EINVAL;
+
+	if (!adev->mman.sdma_access_ptr)
+		return -EACCES;
+
+	r = drm_dev_enter(adev_to_drm(adev), &idx);
+	if (r)
+		return r;
+
+	if (write)
+		memcpy(adev->mman.sdma_access_ptr, buf, len);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos, · line 1465

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+
+	num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
+	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, AMDGPU_IB_POOL_DELAYED, &job);
+	if (r)
+		goto out;
+
+	src_addr = write ? amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo) :
+			amdgpu_bo_gpu_offset(abo);
+	dst_addr = write ? amdgpu_bo_gpu_offset(abo) :
+			amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo);

I suggest to write this as

src_addr = a;
dst_addr = b;
if (write)
    swap(src_addr, dst_addr);

This way we are not duplicating getting the different offsets.

Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +20 -0
+	amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr, PAGE_SIZE, false);
+
+	amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
+	WARN_ON(job->ibs[0].length_dw > num_dw);
+
+	r = amdgpu_job_submit(job, &adev->mman.entity, AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
+	if (r) {
+		amdgpu_job_free(job);
+		goto out;
+	}
+
+	if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
+		r = -ETIMEDOUT;
+	dma_fence_put(fence);
+
+	if (!(r || write))
+		memcpy(buf, adev->mman.sdma_access_ptr, len);
+out:
+	drm_dev_exit(idx);
+	return r;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos, · line 1495

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+}
+
+static inline bool amdgpu_ttm_allow_post_mortem_debug(struct amdgpu_device *adev)
+{
+	return amdgpu_gpu_recovery == 0 ||
+		adev->gfx_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->compute_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->sdma_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->video_timeout == MAX_SCHEDULE_TIMEOUT;
+}

This should probably be inside amdgpu_device.c

Quoted 29 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +11 -0
+
  /**
   * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
   *
@@ -1453,6 +1518,10 @@ static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
  	if (bo->resource->mem_type != TTM_PL_VRAM)
  		return -EIO;
 
+	if (!amdgpu_ttm_allow_post_mortem_debug(adev) &&
+			!amdgpu_ttm_access_memory_sdma(bo, offset, buf, len, write))
+		return len;
+
  	amdgpu_res_first(bo->resource, offset, len, &cursor);
  	while (cursor.remaining) {
  		size_t count, size = cursor.size;
@@ -1793,6 +1862,12 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
  		return r;
  	}
 
+	if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
+				AMDGPU_GEM_DOMAIN_GTT,
+				&adev->mman.sdma_access_bo, NULL,
+				adev->mman.sdma_access_ptr))
+		DRM_WARN("Debug VRAM access will use slowpath MM access\n");
+
  	return 0;
  }
 

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c void amdgpu_ttm_fini(struct amdgpu_device *adev) · line 1901

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
@@ -1823,6 +1898,9 @@ void amdgpu_ttm_fini(struct amdgpu_device *adev)
  	ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
  	ttm_device_fini(&adev->mman.bdev);
  	adev->mman.initialized = false;
+	if (adev->mman.sdma_access_ptr)

You can drop that if. Free functions can usually take a NULL pointer.

Apart from those nit picks looks good to me as well.

Regards,
Christian.

Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
+		amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo, NULL,
+					&adev->mman.sdma_access_ptr);
  	DRM_INFO("amdgpu: ttm finalized\n");
  }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4 -1
index 91a087f9dc7c..b0116c4a768f 100644
@@ -98,6 +98,10 @@ struct amdgpu_mman {
  	u64		fw_vram_usage_size;
  	struct amdgpu_bo	*fw_vram_usage_reserved_bo;
  	void		*fw_vram_usage_va;
+
+	/* PAGE_SIZE'd BO for process memory r/w over SDMA. */
+	struct amdgpu_bo	*sdma_access_bo;
+	void			*sdma_access_ptr;
  };
 
  struct amdgpu_copy_mem {
@@ -193,5 +197,4 @@ uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
  int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int mem_type);
 
  void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
-
  #endif
#3 lore raw

[Public]

Thanks Christian. I've already merged based on Felix's review. I'll send your suggested cleanup for review out soon.

Jon

Quoted 205 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: January 12, 2022 2:33 AM
To: Kim, Jonathan <[email protected]>; amd- [email protected]
Cc: Kuehling, Felix <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: improve debug VRAM access performance using sdma

Am 04.01.22 um 20:12 schrieb Jonathan Kim:

Quoted 8 lines

For better performance during VRAM access for debugged processes, do read/write copies over SDMA.

In order to fulfill post mortem debugging on a broken device, fallback to stable MMIO access when gpu recovery is disabled or when job submission time outs are set to max. Failed SDMA access should automatically fall back to MMIO access.

Quoted 6 lines

Use a pre-allocated GTT bounce buffer pre-mapped into GART to avoid page-table updates and TLB flushes on access.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 78

+++++++++++++++++++++++++

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void

Quoted 17 lines
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  5 +-
  2 files changed, 82 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 367abed1d6e6..512df4c09772 100644
@@ -48,6 +48,7 @@
  #include <drm/ttm/ttm_range_manager.h>
 
  #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
  #include "amdgpu.h"
  #include "amdgpu_object.h"
@@ -1429,6 +1430,70 @@ static void

amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void · line 1433

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
    }
  }
 
+static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object

*bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void · line 1434

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+                                   unsigned long offset, void *buf, int

len, int write) {

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +13 -0
+   struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
+   struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+   struct amdgpu_job *job;
+   struct dma_fence *fence;
+   uint64_t src_addr, dst_addr;
+   unsigned int num_dw;
+   int r, idx;
+
+   if (len != PAGE_SIZE)
+           return -EINVAL;
+
+   if (!adev->mman.sdma_access_ptr)
+           return -EACCES;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+
+   r = drm_dev_enter(adev_to_drm(adev), &idx);
+   if (r)
+           return r;
+
+   if (write)
+           memcpy(adev->mman.sdma_access_ptr, buf, len);
+
+   num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
+   r = amdgpu_job_alloc_with_ib(adev, num_dw * 4,

AMDGPU_IB_POOL_DELAYED, &job);

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+   if (r)
+           goto out;
+
+   src_addr = write ? amdgpu_bo_gpu_offset(adev-

mman.sdma_access_bo) :

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+                   amdgpu_bo_gpu_offset(abo);
+   dst_addr = write ? amdgpu_bo_gpu_offset(abo) :
+                   amdgpu_bo_gpu_offset(adev-

mman.sdma_access_bo);

I suggest to write this as

src_addr = a;
dst_addr = b;
if (write)

     swap(src_addr, dst_addr);

This way we are not duplicating getting the different offsets.

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+   amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
+PAGE_SIZE, false);
+
+   amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job-

ibs[0]);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+   WARN_ON(job->ibs[0].length_dw > num_dw);
+
+   r = amdgpu_job_submit(job, &adev->mman.entity,

AMDGPU_FENCE_OWNER_UNDEFINED, &fence);

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +13 -0
+   if (r) {
+           amdgpu_job_free(job);
+           goto out;
+   }
+
+   if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
+           r = -ETIMEDOUT;
+   dma_fence_put(fence);
+
+   if (!(r || write))
+           memcpy(buf, adev->mman.sdma_access_ptr, len);
+out:
+   drm_dev_exit(idx);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+   return r;
+}
+
+static inline bool amdgpu_ttm_allow_post_mortem_debug(struct
+amdgpu_device *adev) {
+   return amdgpu_gpu_recovery == 0 ||
+           adev->gfx_timeout == MAX_SCHEDULE_TIMEOUT ||
+           adev->compute_timeout == MAX_SCHEDULE_TIMEOUT ||
+           adev->sdma_timeout == MAX_SCHEDULE_TIMEOUT ||
+           adev->video_timeout == MAX_SCHEDULE_TIMEOUT; }

This should probably be inside amdgpu_device.c

Quoted 3 lines

+
/**
* amdgpu_ttm_access_memory - Read or Write memory that backs a

buffer object.

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_access_memory(struct

Quoted 2 lines
   *
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1453,6 +1518,10 @@ static int amdgpu_ttm_access_memory(struct

ttm_buffer_object *bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
    if (bo->resource->mem_type != TTM_PL_VRAM)
            return -EIO;
 
+   if (!amdgpu_ttm_allow_post_mortem_debug(adev) &&
+                   !amdgpu_ttm_access_memory_sdma(bo, offset,

buf, len, write))

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_init(struct amdgpu_device

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
+           return len;
+
    amdgpu_res_first(bo->resource, offset, len, &cursor);
    while (cursor.remaining) {
            size_t count, size = cursor.size;
@@ -1793,6 +1862,12 @@ int amdgpu_ttm_init(struct amdgpu_device

*adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_init(struct amdgpu_device *adev)

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -0
            return r;
    }
 
+   if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
+                           AMDGPU_GEM_DOMAIN_GTT,
+                           &adev->mman.sdma_access_bo, NULL,
+                           adev->mman.sdma_access_ptr))
+           DRM_WARN("Debug VRAM access will use slowpath MM

access\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c void amdgpu_ttm_fini(struct amdgpu_device

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+
    return 0;
  }
 
@@ -1823,6 +1898,9 @@ void amdgpu_ttm_fini(struct amdgpu_device

*adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c void amdgpu_ttm_fini(struct amdgpu_device *adev) · line 1901

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
    ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
    ttm_device_fini(&adev->mman.bdev);
    adev->mman.initialized = false;
+   if (adev->mman.sdma_access_ptr)

You can drop that if. Free functions can usually take a NULL pointer.

Apart from those nit picks looks good to me as well.

Regards,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c void amdgpu_ttm_fini(struct amdgpu_device · line 1902

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+           amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo,

NULL,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h uint64_t amdgpu_ttm_tt_pte_flags(struct

Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+                                   &adev->mman.sdma_access_ptr);
    DRM_INFO("amdgpu: ttm finalized\n");
  }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4 -0
index 91a087f9dc7c..b0116c4a768f 100644
@@ -98,6 +98,10 @@ struct amdgpu_mman {
    u64             fw_vram_usage_size;
    struct amdgpu_bo        *fw_vram_usage_reserved_bo;
    void            *fw_vram_usage_va;
+
+   /* PAGE_SIZE'd BO for process memory r/w over SDMA. */
+   struct amdgpu_bo        *sdma_access_bo;
+   void                    *sdma_access_ptr;
  };
 
  struct amdgpu_copy_mem {
@@ -193,5 +197,4 @@ uint64_t amdgpu_ttm_tt_pte_flags(struct

amdgpu_device *adev, struct ttm_tt *ttm,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +0 -1
  int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int
 mem_type);
 
  void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
-
  #endif
maintainer
#4 lore raw

Yeah, that's basically my fault.

I haven't even worked myself through all the mails which piled up during the xmas break :(

Christian.

Am 12.01.22 um 15:21 schrieb Kim, Jonathan:

Quoted 209 lines

[Public]

Thanks Christian. I've already merged based on Felix's review. I'll send your suggested cleanup for review out soon.

Jon

Quoted 202 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: January 12, 2022 2:33 AM
To: Kim, Jonathan <[email protected]>; amd- [email protected]
Cc: Kuehling, Felix <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: improve debug VRAM access performance using sdma

Am 04.01.22 um 20:12 schrieb Jonathan Kim:

Quoted 8 lines

For better performance during VRAM access for debugged processes, do read/write copies over SDMA.

In order to fulfill post mortem debugging on a broken device, fallback to stable MMIO access when gpu recovery is disabled or when job submission time outs are set to max. Failed SDMA access should automatically fall back to MMIO access.

Quoted 6 lines

Use a pre-allocated GTT bounce buffer pre-mapped into GART to avoid page-table updates and TLB flushes on access.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 78

+++++++++++++++++++++++++

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void

Quoted 17 lines
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  5 +-
   2 files changed, 82 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
index 367abed1d6e6..512df4c09772 100644
@@ -48,6 +48,7 @@
   #include <drm/ttm/ttm_range_manager.h>
 
   #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
   #include "amdgpu.h"
   #include "amdgpu_object.h"
@@ -1429,6 +1430,70 @@ static void

amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void · line 1433

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
     }
   }
 
+static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object

*bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void · line 1434

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+                                   unsigned long offset, void *buf, int

len, int write) {

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +13 -0
+   struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
+   struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+   struct amdgpu_job *job;
+   struct dma_fence *fence;
+   uint64_t src_addr, dst_addr;
+   unsigned int num_dw;
+   int r, idx;
+
+   if (len != PAGE_SIZE)
+           return -EINVAL;
+
+   if (!adev->mman.sdma_access_ptr)
+           return -EACCES;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+
+   r = drm_dev_enter(adev_to_drm(adev), &idx);
+   if (r)
+           return r;
+
+   if (write)
+           memcpy(adev->mman.sdma_access_ptr, buf, len);
+
+   num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
+   r = amdgpu_job_alloc_with_ib(adev, num_dw * 4,

AMDGPU_IB_POOL_DELAYED, &job);

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+   if (r)
+           goto out;
+
+   src_addr = write ? amdgpu_bo_gpu_offset(adev-

mman.sdma_access_bo) :

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+                   amdgpu_bo_gpu_offset(abo);
+   dst_addr = write ? amdgpu_bo_gpu_offset(abo) :
+                   amdgpu_bo_gpu_offset(adev-

mman.sdma_access_bo);

I suggest to write this as

src_addr = a;
dst_addr = b;
if (write)

      swap(src_addr, dst_addr);

This way we are not duplicating getting the different offsets.

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+   amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
+PAGE_SIZE, false);
+
+   amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job-

ibs[0]);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+   WARN_ON(job->ibs[0].length_dw > num_dw);
+
+   r = amdgpu_job_submit(job, &adev->mman.entity,

AMDGPU_FENCE_OWNER_UNDEFINED, &fence);

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +13 -0
+   if (r) {
+           amdgpu_job_free(job);
+           goto out;
+   }
+
+   if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
+           r = -ETIMEDOUT;
+   dma_fence_put(fence);
+
+   if (!(r || write))
+           memcpy(buf, adev->mman.sdma_access_ptr, len);
+out:
+   drm_dev_exit(idx);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+   return r;
+}
+
+static inline bool amdgpu_ttm_allow_post_mortem_debug(struct
+amdgpu_device *adev) {
+   return amdgpu_gpu_recovery == 0 ||
+           adev->gfx_timeout == MAX_SCHEDULE_TIMEOUT ||
+           adev->compute_timeout == MAX_SCHEDULE_TIMEOUT ||
+           adev->sdma_timeout == MAX_SCHEDULE_TIMEOUT ||
+           adev->video_timeout == MAX_SCHEDULE_TIMEOUT; }

This should probably be inside amdgpu_device.c

Quoted 3 lines

+
/**
* amdgpu_ttm_access_memory - Read or Write memory that backs a

buffer object.

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_access_memory(struct

Quoted 2 lines
    *
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1453,6 +1518,10 @@ static int amdgpu_ttm_access_memory(struct

ttm_buffer_object *bo,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
     if (bo->resource->mem_type != TTM_PL_VRAM)
             return -EIO;
 
+   if (!amdgpu_ttm_allow_post_mortem_debug(adev) &&
+                   !amdgpu_ttm_access_memory_sdma(bo, offset,

buf, len, write))

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_init(struct amdgpu_device

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
+           return len;
+
     amdgpu_res_first(bo->resource, offset, len, &cursor);
     while (cursor.remaining) {
             size_t count, size = cursor.size;
@@ -1793,6 +1862,12 @@ int amdgpu_ttm_init(struct amdgpu_device

*adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_init(struct amdgpu_device *adev)

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -0
             return r;
     }
 
+   if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
+                           AMDGPU_GEM_DOMAIN_GTT,
+                           &adev->mman.sdma_access_bo, NULL,
+                           adev->mman.sdma_access_ptr))
+           DRM_WARN("Debug VRAM access will use slowpath MM

access\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c void amdgpu_ttm_fini(struct amdgpu_device

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+
     return 0;
   }
 
@@ -1823,6 +1898,9 @@ void amdgpu_ttm_fini(struct amdgpu_device

*adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c void amdgpu_ttm_fini(struct amdgpu_device *adev) · line 1901

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
     ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
     ttm_device_fini(&adev->mman.bdev);
     adev->mman.initialized = false;
+   if (adev->mman.sdma_access_ptr)

You can drop that if. Free functions can usually take a NULL pointer.

Apart from those nit picks looks good to me as well.

Regards,
Christian.

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c void amdgpu_ttm_fini(struct amdgpu_device · line 1902

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+           amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo,

NULL,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h uint64_t amdgpu_ttm_tt_pte_flags(struct

Quoted 21 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+                                   &adev->mman.sdma_access_ptr);
     DRM_INFO("amdgpu: ttm finalized\n");
   }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4 -0
index 91a087f9dc7c..b0116c4a768f 100644
@@ -98,6 +98,10 @@ struct amdgpu_mman {
     u64             fw_vram_usage_size;
     struct amdgpu_bo        *fw_vram_usage_reserved_bo;
     void            *fw_vram_usage_va;
+
+   /* PAGE_SIZE'd BO for process memory r/w over SDMA. */
+   struct amdgpu_bo        *sdma_access_bo;
+   void                    *sdma_access_ptr;
   };
 
   struct amdgpu_copy_mem {
@@ -193,5 +197,4 @@ uint64_t amdgpu_ttm_tt_pte_flags(struct

amdgpu_device *adev, struct ttm_tt *ttm,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +0 -1
   int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int
 mem_type);
 
   void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
-
   #endif
#5 lore raw
In reply to Jonathan Kim

[AMD Official Use Only - General]

I happened to find the sdma_access_bo allocation from GTT seems performing before gart is ready. That makes the "amdgpu_gart_map" is skipped since adev->gart.ptr is still NULL. Is that done intentionally ?

Evan

Quoted 187 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Jonathan Kim
Sent: Wednesday, January 5, 2022 3:12 AM
To: [email protected]
Cc: Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>; Koenig, Christian <[email protected]>
Subject: [PATCH] drm/amdgpu: improve debug VRAM access performance using sdma

For better performance during VRAM access for debugged processes, do read/write copies over SDMA.

In order to fulfill post mortem debugging on a broken device, fallback to stable MMIO access when gpu recovery is disabled or when job submission time outs are set to max. Failed SDMA access should automatically fall back to MMIO access.

Use a pre-allocated GTT bounce buffer pre-mapped into GART to avoid page-table updates and TLB flushes on access.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 78
+++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  5 +-
 2 files changed, 82 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -0
index 367abed1d6e6..512df4c09772 100644
@@ -48,6 +48,7 @@
 #include <drm/ttm/ttm_range_manager.h>
 
 #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
 #include "amdgpu.h"
 #include "amdgpu_object.h"
@@ -1429,6 +1430,70 @@ static void amdgpu_ttm_vram_mm_access(struct
 amdgpu_device *adev, loff_t pos,
 	}
 }
 
+static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object
 *bo,
+					unsigned long offset, void *buf, int
 len, int write)
+{
+	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
+	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+	struct amdgpu_job *job;
+	struct dma_fence *fence;
+	uint64_t src_addr, dst_addr;
+	unsigned int num_dw;
+	int r, idx;
+
+	if (len != PAGE_SIZE)
+		return -EINVAL;
+
+	if (!adev->mman.sdma_access_ptr)
+		return -EACCES;
+
+	r = drm_dev_enter(adev_to_drm(adev), &idx);
+	if (r)
+		return r;
+
+	if (write)
+		memcpy(adev->mman.sdma_access_ptr, buf, len);
+
+	num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
+	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4,

AMDGPU_IB_POOL_DELAYED, &job);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+	if (r)
+		goto out;
+
+	src_addr = write ? amdgpu_bo_gpu_offset(adev-
Quoted 1 line

mman.sdma_access_bo) :

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+			amdgpu_bo_gpu_offset(abo);
+	dst_addr = write ? amdgpu_bo_gpu_offset(abo) :
+			amdgpu_bo_gpu_offset(adev-
Quoted 1 line

mman.sdma_access_bo);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+	amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,

PAGE_SIZE, false);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -0
+
+	amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
+	WARN_ON(job->ibs[0].length_dw > num_dw);
+
+	r = amdgpu_job_submit(job, &adev->mman.entity,

AMDGPU_FENCE_OWNER_UNDEFINED, &fence);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +17 -0
+	if (r) {
+		amdgpu_job_free(job);
+		goto out;
+	}
+
+	if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
+		r = -ETIMEDOUT;
+	dma_fence_put(fence);
+
+	if (!(r || write))
+		memcpy(buf, adev->mman.sdma_access_ptr, len);
+out:
+	drm_dev_exit(idx);
+	return r;
+}
+
+static inline bool amdgpu_ttm_allow_post_mortem_debug(struct

amdgpu_device *adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +8 -0
+{
+	return amdgpu_gpu_recovery == 0 ||
+		adev->gfx_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->compute_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->sdma_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->video_timeout == MAX_SCHEDULE_TIMEOUT;
+}
+
 /**
  * amdgpu_ttm_access_memory - Read or Write memory that backs a

buffer object.
*

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
@@ -1453,6 +1518,10 @@ static int amdgpu_ttm_access_memory(struct
 ttm_buffer_object *bo,
 	if (bo->resource->mem_type != TTM_PL_VRAM)
 		return -EIO;
 
+	if (!amdgpu_ttm_allow_post_mortem_debug(adev) &&
+			!amdgpu_ttm_access_memory_sdma(bo, offset, buf,
 len, write))
+		return len;
+
 	amdgpu_res_first(bo->resource, offset, len, &cursor);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
 	while (cursor.remaining) {
 		size_t count, size = cursor.size;
@@ -1793,6 +1862,12 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
 		return r;
 	}
 
+	if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
+				AMDGPU_GEM_DOMAIN_GTT,
+				&adev->mman.sdma_access_bo, NULL,
+				adev->mman.sdma_access_ptr))
+		DRM_WARN("Debug VRAM access will use slowpath MM
 access\n");
+
 	return 0;
 }
@@ -1823,6 +1898,9 @@ void amdgpu_ttm_fini(struct amdgpu_device
 *adev)
 	ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
 	ttm_device_fini(&adev->mman.bdev);
 	adev->mman.initialized = false;
+	if (adev->mman.sdma_access_ptr)
+		amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo,
 NULL,
+					&adev->mman.sdma_access_ptr);
 	DRM_INFO("amdgpu: ttm finalized\n");

}

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4 -2
index 91a087f9dc7c..b0116c4a768f 100644
@@ -98,6 +98,10 @@ struct amdgpu_mman {
 	u64		fw_vram_usage_size;
 	struct amdgpu_bo	*fw_vram_usage_reserved_bo;
 	void		*fw_vram_usage_va;
+
+	/* PAGE_SIZE'd BO for process memory r/w over SDMA. */
+	struct amdgpu_bo	*sdma_access_bo;
+	void			*sdma_access_ptr;
 };
 
 struct amdgpu_copy_mem {
@@ -193,5 +197,4 @@ uint64_t amdgpu_ttm_tt_pte_flags(struct
 amdgpu_device *adev, struct ttm_tt *ttm,
 int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int
 mem_type);
 
 void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
-
 #endif
--

2.25.1

#6 lore raw

I don't think so. Have we recently re-ordered something here?

Christian.

Am 20.03.23 um 08:05 schrieb Quan, Evan:

Quoted 194 lines

[AMD Official Use Only - General]

I happened to find the sdma_access_bo allocation from GTT seems performing before gart is ready. That makes the "amdgpu_gart_map" is skipped since adev->gart.ptr is still NULL. Is that done intentionally ?

Evan

Quoted 187 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Jonathan Kim
Sent: Wednesday, January 5, 2022 3:12 AM
To: [email protected]
Cc: Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>; Koenig, Christian <[email protected]>
Subject: [PATCH] drm/amdgpu: improve debug VRAM access performance using sdma

For better performance during VRAM access for debugged processes, do read/write copies over SDMA.

In order to fulfill post mortem debugging on a broken device, fallback to stable MMIO access when gpu recovery is disabled or when job submission time outs are set to max. Failed SDMA access should automatically fall back to MMIO access.

Use a pre-allocated GTT bounce buffer pre-mapped into GART to avoid page-table updates and TLB flushes on access.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 78
+++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  5 +-
  2 files changed, 82 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -0
index 367abed1d6e6..512df4c09772 100644
@@ -48,6 +48,7 @@
  #include <drm/ttm/ttm_range_manager.h>
 
  #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
  #include "amdgpu.h"
  #include "amdgpu_object.h"
@@ -1429,6 +1430,70 @@ static void amdgpu_ttm_vram_mm_access(struct
 amdgpu_device *adev, loff_t pos,
  	}
  }
 
+static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object
 *bo,
+					unsigned long offset, void *buf, int
 len, int write)
+{
+	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
+	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+	struct amdgpu_job *job;
+	struct dma_fence *fence;
+	uint64_t src_addr, dst_addr;
+	unsigned int num_dw;
+	int r, idx;
+
+	if (len != PAGE_SIZE)
+		return -EINVAL;
+
+	if (!adev->mman.sdma_access_ptr)
+		return -EACCES;
+
+	r = drm_dev_enter(adev_to_drm(adev), &idx);
+	if (r)
+		return r;
+
+	if (write)
+		memcpy(adev->mman.sdma_access_ptr, buf, len);
+
+	num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
+	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4,

AMDGPU_IB_POOL_DELAYED, &job);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+	if (r)
+		goto out;
+
+	src_addr = write ? amdgpu_bo_gpu_offset(adev-
Quoted 1 line

mman.sdma_access_bo) :

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+			amdgpu_bo_gpu_offset(abo);
+	dst_addr = write ? amdgpu_bo_gpu_offset(abo) :
+			amdgpu_bo_gpu_offset(adev-
Quoted 1 line

mman.sdma_access_bo);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+	amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,

PAGE_SIZE, false);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -0
+
+	amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
+	WARN_ON(job->ibs[0].length_dw > num_dw);
+
+	r = amdgpu_job_submit(job, &adev->mman.entity,

AMDGPU_FENCE_OWNER_UNDEFINED, &fence);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +17 -0
+	if (r) {
+		amdgpu_job_free(job);
+		goto out;
+	}
+
+	if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
+		r = -ETIMEDOUT;
+	dma_fence_put(fence);
+
+	if (!(r || write))
+		memcpy(buf, adev->mman.sdma_access_ptr, len);
+out:
+	drm_dev_exit(idx);
+	return r;
+}
+
+static inline bool amdgpu_ttm_allow_post_mortem_debug(struct

amdgpu_device *adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +8 -0
+{
+	return amdgpu_gpu_recovery == 0 ||
+		adev->gfx_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->compute_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->sdma_timeout == MAX_SCHEDULE_TIMEOUT ||
+		adev->video_timeout == MAX_SCHEDULE_TIMEOUT;
+}
+
  /**
   * amdgpu_ttm_access_memory - Read or Write memory that backs a

buffer object.
*

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
@@ -1453,6 +1518,10 @@ static int amdgpu_ttm_access_memory(struct
 ttm_buffer_object *bo,
  	if (bo->resource->mem_type != TTM_PL_VRAM)
  		return -EIO;
 
+	if (!amdgpu_ttm_allow_post_mortem_debug(adev) &&
+			!amdgpu_ttm_access_memory_sdma(bo, offset, buf,
 len, write))
+		return len;
+
  	amdgpu_res_first(bo->resource, offset, len, &cursor);
  	while (cursor.remaining) {
  		size_t count, size = cursor.size;
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
@@ -1793,6 +1862,12 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
  		return r;
  	}
 
+	if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
+				AMDGPU_GEM_DOMAIN_GTT,
+				&adev->mman.sdma_access_bo, NULL,
+				adev->mman.sdma_access_ptr))
+		DRM_WARN("Debug VRAM access will use slowpath MM
 access\n");
+
  	return 0;
  }
@@ -1823,6 +1898,9 @@ void amdgpu_ttm_fini(struct amdgpu_device
 *adev)
  	ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
  	ttm_device_fini(&adev->mman.bdev);
  	adev->mman.initialized = false;
+	if (adev->mman.sdma_access_ptr)
+		amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo,
 NULL,
+					&adev->mman.sdma_access_ptr);
  	DRM_INFO("amdgpu: ttm finalized\n");
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4 -2
index 91a087f9dc7c..b0116c4a768f 100644
@@ -98,6 +98,10 @@ struct amdgpu_mman {
  	u64		fw_vram_usage_size;
  	struct amdgpu_bo	*fw_vram_usage_reserved_bo;
  	void		*fw_vram_usage_va;
+
+	/* PAGE_SIZE'd BO for process memory r/w over SDMA. */
+	struct amdgpu_bo	*sdma_access_bo;
+	void			*sdma_access_ptr;
  };
 
  struct amdgpu_copy_mem {
@@ -193,5 +197,4 @@ uint64_t amdgpu_ttm_tt_pte_flags(struct
 amdgpu_device *adev, struct ttm_tt *ttm,
  int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int
 mem_type);
 
  void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
-
  #endif
--

2.25.1

#7 lore raw

[Public]

This was a long time ago but I think we agreed allocation was ok before GART was ready.
IIRC, there was also some mentioned related scenario where APUs needed to work without VRAM but allocations were required (but I don't know the details regarding that). I vaguely remember the requirement for GART readiness for the bounce buffer allocation caused some problems elsewhere.
Are there problems observed with the bounce buffer being allocated without GART readiness?

Thanks,

Jon

Quoted 219 lines

-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, March 20, 2023 1:02 PM
To: Quan, Evan <[email protected]>; Kim, Jonathan <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: improve debug VRAM access performance using sdma

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

I don't think so. Have we recently re-ordered something here?

Christian.

Am 20.03.23 um 08:05 schrieb Quan, Evan:

Quoted 3 lines

[AMD Official Use Only - General]

I happened to find the sdma_access_bo allocation from GTT seems

performing before gart is ready.

Quoted 1 line

That makes the "amdgpu_gart_map" is skipped since adev->gart.ptr is still

NULL.

Quoted 10 lines

Is that done intentionally ?

Evan

Quoted 7 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Jonathan Kim
Sent: Wednesday, January 5, 2022 3:12 AM
To: [email protected]
Cc: Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>; Koenig, Christian

<[email protected]>

Quoted 8 lines
Quoted 8 lines

Subject: [PATCH] drm/amdgpu: improve debug VRAM access performance using sdma

For better performance during VRAM access for debugged processes, do read/write copies over SDMA.

In order to fulfill post mortem debugging on a broken device, fallback to stable MMIO access when gpu recovery is disabled or when job

submission

Quoted 21 lines
Quoted 21 lines

time outs are set to max. Failed SDMA access should automatically fall back to MMIO access.

Use a pre-allocated GTT bounce buffer pre-mapped into GART to avoid page-table updates and TLB flushes on access.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 78
+++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  5 +-
  2 files changed, 82 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
index 367abed1d6e6..512df4c09772 100644
@@ -48,6 +48,7 @@
  #include <drm/ttm/ttm_range_manager.h>
 
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
  #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
  #include "amdgpu.h"
  #include "amdgpu_object.h"
@@ -1429,6 +1430,70 @@ static void

amdgpu_ttm_vram_mm_access(struct

Quoted 85 lines
Quoted 85 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +26 -0
 amdgpu_device *adev, loff_t pos,
     }
  }
 
+static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object
 *bo,
+                                    unsigned long offset, void *buf, int
 len, int write)
+{
+    struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
+    struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+    struct amdgpu_job *job;
+    struct dma_fence *fence;
+    uint64_t src_addr, dst_addr;
+    unsigned int num_dw;
+    int r, idx;
+
+    if (len != PAGE_SIZE)
+            return -EINVAL;
+
+    if (!adev->mman.sdma_access_ptr)
+            return -EACCES;
+
+    r = drm_dev_enter(adev_to_drm(adev), &idx);
+    if (r)
+            return r;
+
+    if (write)
+            memcpy(adev->mman.sdma_access_ptr, buf, len);
+
+    num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
+    r = amdgpu_job_alloc_with_ib(adev, num_dw * 4,

AMDGPU_IB_POOL_DELAYED, &job);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+    if (r)
+            goto out;
+
+    src_addr = write ? amdgpu_bo_gpu_offset(adev-
Quoted 1 line

mman.sdma_access_bo) :

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+                    amdgpu_bo_gpu_offset(abo);
+    dst_addr = write ? amdgpu_bo_gpu_offset(abo) :
+                    amdgpu_bo_gpu_offset(adev-
Quoted 1 line

mman.sdma_access_bo);

+ amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
PAGE_SIZE, false);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -0
+
+    amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
+    WARN_ON(job->ibs[0].length_dw > num_dw);
+
+    r = amdgpu_job_submit(job, &adev->mman.entity,

AMDGPU_FENCE_OWNER_UNDEFINED, &fence);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +17 -0
+    if (r) {
+            amdgpu_job_free(job);
+            goto out;
+    }
+
+    if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
+            r = -ETIMEDOUT;
+    dma_fence_put(fence);
+
+    if (!(r || write))
+            memcpy(buf, adev->mman.sdma_access_ptr, len);
+out:
+    drm_dev_exit(idx);
+    return r;
+}
+
+static inline bool amdgpu_ttm_allow_post_mortem_debug(struct

amdgpu_device *adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +8 -0
+{
+    return amdgpu_gpu_recovery == 0 ||
+            adev->gfx_timeout == MAX_SCHEDULE_TIMEOUT ||
+            adev->compute_timeout == MAX_SCHEDULE_TIMEOUT ||
+            adev->sdma_timeout == MAX_SCHEDULE_TIMEOUT ||
+            adev->video_timeout == MAX_SCHEDULE_TIMEOUT;
+}
+
  /**
   * amdgpu_ttm_access_memory - Read or Write memory that backs a

buffer object.
*

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1453,6 +1518,10 @@ static int amdgpu_ttm_access_memory(struct
 ttm_buffer_object *bo,
     if (bo->resource->mem_type != TTM_PL_VRAM)
             return -EIO;
 
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_init(struct amdgpu_device

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+    if (!amdgpu_ttm_allow_post_mortem_debug(adev) &&
+                    !amdgpu_ttm_access_memory_sdma(bo, offset, buf,
 len, write))
+            return len;
+
     amdgpu_res_first(bo->resource, offset, len, &cursor);
     while (cursor.remaining) {
             size_t count, size = cursor.size;
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1793,6 +1862,12 @@ int amdgpu_ttm_init(struct amdgpu_device

*adev)

Quoted 51 lines
Quoted 51 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
             return r;
     }
 
+    if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
+                            AMDGPU_GEM_DOMAIN_GTT,
+                            &adev->mman.sdma_access_bo, NULL,
+                            adev->mman.sdma_access_ptr))
+            DRM_WARN("Debug VRAM access will use slowpath MM
 access\n");
+
     return 0;
  }
@@ -1823,6 +1898,9 @@ void amdgpu_ttm_fini(struct amdgpu_device
 *adev)
     ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
     ttm_device_fini(&adev->mman.bdev);
     adev->mman.initialized = false;
+    if (adev->mman.sdma_access_ptr)
+            amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo,
 NULL,
+                                    &adev->mman.sdma_access_ptr);
     DRM_INFO("amdgpu: ttm finalized\n");
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4 -2
index 91a087f9dc7c..b0116c4a768f 100644
@@ -98,6 +98,10 @@ struct amdgpu_mman {
     u64             fw_vram_usage_size;
     struct amdgpu_bo        *fw_vram_usage_reserved_bo;
     void            *fw_vram_usage_va;
+
+    /* PAGE_SIZE'd BO for process memory r/w over SDMA. */
+    struct amdgpu_bo        *sdma_access_bo;
+    void                    *sdma_access_ptr;
  };
 
  struct amdgpu_copy_mem {
@@ -193,5 +197,4 @@ uint64_t amdgpu_ttm_tt_pte_flags(struct
 amdgpu_device *adev, struct ttm_tt *ttm,
  int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int
 mem_type);
 
  void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
-
  #endif
--

2.25.1

maintainer
#8 lore raw

Ah, yes! GART doesn't need to be read to make a GTT allocation.

When GART becomes ready it will be filled with all the buffers which were allocated before it was ready.

So this is perfectly fine.

Thanks,
Christian.

Am 20.03.23 um 18:24 schrieb Kim, Jonathan:

Quoted 229 lines

[Public]

This was a long time ago but I think we agreed allocation was ok before GART was ready.
IIRC, there was also some mentioned related scenario where APUs needed to work without VRAM but allocations were required (but I don't know the details regarding that). I vaguely remember the requirement for GART readiness for the bounce buffer allocation caused some problems elsewhere.
Are there problems observed with the bounce buffer being allocated without GART readiness?

Thanks,

Jon

Quoted 219 lines

-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, March 20, 2023 1:02 PM
To: Quan, Evan <[email protected]>; Kim, Jonathan <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: improve debug VRAM access performance using sdma

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

I don't think so. Have we recently re-ordered something here?

Christian.

Am 20.03.23 um 08:05 schrieb Quan, Evan:

Quoted 3 lines

[AMD Official Use Only - General]

I happened to find the sdma_access_bo allocation from GTT seems

performing before gart is ready.

Quoted 1 line

That makes the "amdgpu_gart_map" is skipped since adev->gart.ptr is still

NULL.

Quoted 10 lines

Is that done intentionally ?

Evan

Quoted 7 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of Jonathan Kim
Sent: Wednesday, January 5, 2022 3:12 AM
To: [email protected]
Cc: Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>; Koenig, Christian

<[email protected]>

Quoted 8 lines
Quoted 8 lines

Subject: [PATCH] drm/amdgpu: improve debug VRAM access performance using sdma

For better performance during VRAM access for debugged processes, do read/write copies over SDMA.

In order to fulfill post mortem debugging on a broken device, fallback to stable MMIO access when gpu recovery is disabled or when job

submission

Quoted 21 lines
Quoted 21 lines

time outs are set to max. Failed SDMA access should automatically fall back to MMIO access.

Use a pre-allocated GTT bounce buffer pre-mapped into GART to avoid page-table updates and TLB flushes on access.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 78
+++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  5 +-
   2 files changed, 82 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
index 367abed1d6e6..512df4c09772 100644
@@ -48,6 +48,7 @@
   #include <drm/ttm/ttm_range_manager.h>
 
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
   #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
   #include "amdgpu.h"
   #include "amdgpu_object.h"
@@ -1429,6 +1430,70 @@ static void

amdgpu_ttm_vram_mm_access(struct

Quoted 85 lines
Quoted 85 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +26 -0
 amdgpu_device *adev, loff_t pos,
      }
   }
 
+static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object
 *bo,
+                                    unsigned long offset, void *buf, int
 len, int write)
+{
+    struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
+    struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+    struct amdgpu_job *job;
+    struct dma_fence *fence;
+    uint64_t src_addr, dst_addr;
+    unsigned int num_dw;
+    int r, idx;
+
+    if (len != PAGE_SIZE)
+            return -EINVAL;
+
+    if (!adev->mman.sdma_access_ptr)
+            return -EACCES;
+
+    r = drm_dev_enter(adev_to_drm(adev), &idx);
+    if (r)
+            return r;
+
+    if (write)
+            memcpy(adev->mman.sdma_access_ptr, buf, len);
+
+    num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
+    r = amdgpu_job_alloc_with_ib(adev, num_dw * 4,

AMDGPU_IB_POOL_DELAYED, &job);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+    if (r)
+            goto out;
+
+    src_addr = write ? amdgpu_bo_gpu_offset(adev-
Quoted 1 line

mman.sdma_access_bo) :

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+                    amdgpu_bo_gpu_offset(abo);
+    dst_addr = write ? amdgpu_bo_gpu_offset(abo) :
+                    amdgpu_bo_gpu_offset(adev-
Quoted 1 line

mman.sdma_access_bo);

+ amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr,
PAGE_SIZE, false);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -0
+
+    amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
+    WARN_ON(job->ibs[0].length_dw > num_dw);
+
+    r = amdgpu_job_submit(job, &adev->mman.entity,

AMDGPU_FENCE_OWNER_UNDEFINED, &fence);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +17 -0
+    if (r) {
+            amdgpu_job_free(job);
+            goto out;
+    }
+
+    if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
+            r = -ETIMEDOUT;
+    dma_fence_put(fence);
+
+    if (!(r || write))
+            memcpy(buf, adev->mman.sdma_access_ptr, len);
+out:
+    drm_dev_exit(idx);
+    return r;
+}
+
+static inline bool amdgpu_ttm_allow_post_mortem_debug(struct

amdgpu_device *adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +8 -0
+{
+    return amdgpu_gpu_recovery == 0 ||
+            adev->gfx_timeout == MAX_SCHEDULE_TIMEOUT ||
+            adev->compute_timeout == MAX_SCHEDULE_TIMEOUT ||
+            adev->sdma_timeout == MAX_SCHEDULE_TIMEOUT ||
+            adev->video_timeout == MAX_SCHEDULE_TIMEOUT;
+}
+
   /**
    * amdgpu_ttm_access_memory - Read or Write memory that backs a

buffer object.

    *
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1453,6 +1518,10 @@ static int amdgpu_ttm_access_memory(struct
 ttm_buffer_object *bo,
      if (bo->resource->mem_type != TTM_PL_VRAM)
              return -EIO;
 
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_init(struct amdgpu_device

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+    if (!amdgpu_ttm_allow_post_mortem_debug(adev) &&
+                    !amdgpu_ttm_access_memory_sdma(bo, offset, buf,
 len, write))
+            return len;
+
      amdgpu_res_first(bo->resource, offset, len, &cursor);
      while (cursor.remaining) {
              size_t count, size = cursor.size;
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1793,6 +1862,12 @@ int amdgpu_ttm_init(struct amdgpu_device

*adev)

Quoted 51 lines
Quoted 51 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
              return r;
      }
 
+    if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
+                            AMDGPU_GEM_DOMAIN_GTT,
+                            &adev->mman.sdma_access_bo, NULL,
+                            adev->mman.sdma_access_ptr))
+            DRM_WARN("Debug VRAM access will use slowpath MM
 access\n");
+
      return 0;
   }
@@ -1823,6 +1898,9 @@ void amdgpu_ttm_fini(struct amdgpu_device
 *adev)
      ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
      ttm_device_fini(&adev->mman.bdev);
      adev->mman.initialized = false;
+    if (adev->mman.sdma_access_ptr)
+            amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo,
 NULL,
+                                    &adev->mman.sdma_access_ptr);
      DRM_INFO("amdgpu: ttm finalized\n");
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4 -2
index 91a087f9dc7c..b0116c4a768f 100644
@@ -98,6 +98,10 @@ struct amdgpu_mman {
      u64             fw_vram_usage_size;
      struct amdgpu_bo        *fw_vram_usage_reserved_bo;
      void            *fw_vram_usage_va;
+
+    /* PAGE_SIZE'd BO for process memory r/w over SDMA. */
+    struct amdgpu_bo        *sdma_access_bo;
+    void                    *sdma_access_ptr;
   };
 
   struct amdgpu_copy_mem {
@@ -193,5 +197,4 @@ uint64_t amdgpu_ttm_tt_pte_flags(struct
 amdgpu_device *adev, struct ttm_tt *ttm,
   int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int
 mem_type);
 
   void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
-
   #endif
--

2.25.1

#9 lore raw

[AMD Official Use Only - General]

OK, I see. Thanks for the explanations.

BR
Evan

Quoted 264 lines

-----Original Message-----
From: Koenig, Christian <[email protected]>
Sent: Tuesday, March 21, 2023 1:32 AM
To: Kim, Jonathan <[email protected]>; Christian König <[email protected]>; Quan, Evan <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: improve debug VRAM access performance using sdma

Ah, yes! GART doesn't need to be read to make a GTT allocation.

When GART becomes ready it will be filled with all the buffers which were allocated before it was ready.

So this is perfectly fine.

Thanks,
Christian.

Am 20.03.23 um 18:24 schrieb Kim, Jonathan:

Quoted 3 lines

[Public]

This was a long time ago but I think we agreed allocation was ok before

GART was ready.

Quoted 1 line

IIRC, there was also some mentioned related scenario where APUs needed

to work without VRAM but allocations were required (but I don't know the details regarding that).

Quoted 1 line

I vaguely remember the requirement for GART readiness for the bounce

buffer allocation caused some problems elsewhere.

Quoted 1 line

Are there problems observed with the bounce buffer being allocated

without GART readiness?

Quoted 35 lines

Thanks,

Jon

Quoted 31 lines

-----Original Message-----
From: Christian König <[email protected]>
Sent: Monday, March 20, 2023 1:02 PM
To: Quan, Evan <[email protected]>; Kim, Jonathan <[email protected]>; [email protected]
Cc: Kuehling, Felix <[email protected]>; Koenig, Christian <[email protected]>
Subject: Re: [PATCH] drm/amdgpu: improve debug VRAM access performance using sdma

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

I don't think so. Have we recently re-ordered something here?

Christian.

Am 20.03.23 um 08:05 schrieb Quan, Evan:

Quoted 3 lines

[AMD Official Use Only - General]

I happened to find the sdma_access_bo allocation from GTT seems

performing before gart is ready.

Quoted 2 lines

That makes the "amdgpu_gart_map" is skipped since adev->gart.ptr is still

NULL.

Quoted 5 lines

Is that done intentionally ?

Evan

Quoted 2 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf

Of

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines

Jonathan Kim
Sent: Wednesday, January 5, 2022 3:12 AM
To: [email protected]
Cc: Kuehling, Felix <[email protected]>; Kim, Jonathan <[email protected]>; Koenig, Christian

<[email protected]>

Quoted 1 line
Quoted 1 line

Subject: [PATCH] drm/amdgpu: improve debug VRAM access

performance

Quoted 37 lines
Quoted 37 lines
Quoted 8 lines
Quoted 8 lines

using sdma

For better performance during VRAM access for debugged processes, do read/write copies over SDMA.

In order to fulfill post mortem debugging on a broken device, fallback to stable MMIO access when gpu recovery is disabled or when job

submission

Quoted 21 lines
Quoted 21 lines

time outs are set to max. Failed SDMA access should automatically fall back to MMIO access.

Use a pre-allocated GTT bounce buffer pre-mapped into GART to avoid page-table updates and TLB flushes on access.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 78
+++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  5 +-
   2 files changed, 82 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
index 367abed1d6e6..512df4c09772 100644
@@ -48,6 +48,7 @@
   #include <drm/ttm/ttm_range_manager.h>
 
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
   #include <drm/amdgpu_drm.h>
+#include <drm/drm_drv.h>
 
   #include "amdgpu.h"
   #include "amdgpu_object.h"
@@ -1429,6 +1430,70 @@ static void

amdgpu_ttm_vram_mm_access(struct

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static void · line 1434

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
 amdgpu_device *adev, loff_t pos,
      }
   }
 
+static int amdgpu_ttm_access_memory_sdma(struct

ttm_buffer_object

Quoted 70 lines
Quoted 37 lines
Quoted 37 lines
Quoted 37 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +26 -0
 *bo,
+                                    unsigned long offset, void
+ *buf, int
 len, int write)
+{
+    struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
+    struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+    struct amdgpu_job *job;
+    struct dma_fence *fence;
+    uint64_t src_addr, dst_addr;
+    unsigned int num_dw;
+    int r, idx;
+
+    if (len != PAGE_SIZE)
+            return -EINVAL;
+
+    if (!adev->mman.sdma_access_ptr)
+            return -EACCES;
+
+    r = drm_dev_enter(adev_to_drm(adev), &idx);
+    if (r)
+            return r;
+
+    if (write)
+            memcpy(adev->mman.sdma_access_ptr, buf, len);
+
+    num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
+    r = amdgpu_job_alloc_with_ib(adev, num_dw * 4,

AMDGPU_IB_POOL_DELAYED, &job);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+    if (r)
+            goto out;
+
+    src_addr = write ? amdgpu_bo_gpu_offset(adev-
Quoted 1 line

mman.sdma_access_bo) :

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+                    amdgpu_bo_gpu_offset(abo);
+    dst_addr = write ? amdgpu_bo_gpu_offset(abo) :
+                    amdgpu_bo_gpu_offset(adev-
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c

Quoted 6 lines
Quoted 1 line

mman.sdma_access_bo);

+ amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
+ dst_addr,
PAGE_SIZE, false);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
+
+    amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job-

ibs[0]);

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+    WARN_ON(job->ibs[0].length_dw > num_dw);
+
+    r = amdgpu_job_submit(job, &adev->mman.entity,

AMDGPU_FENCE_OWNER_UNDEFINED, &fence);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +17 -0
+    if (r) {
+            amdgpu_job_free(job);
+            goto out;
+    }
+
+    if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
+            r = -ETIMEDOUT;
+    dma_fence_put(fence);
+
+    if (!(r || write))
+            memcpy(buf, adev->mman.sdma_access_ptr, len);
+out:
+    drm_dev_exit(idx);
+    return r;
+}
+
+static inline bool amdgpu_ttm_allow_post_mortem_debug(struct

amdgpu_device *adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+{
+    return amdgpu_gpu_recovery == 0 ||
+            adev->gfx_timeout == MAX_SCHEDULE_TIMEOUT ||
+            adev->compute_timeout == MAX_SCHEDULE_TIMEOUT ||
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -0
+            adev->sdma_timeout == MAX_SCHEDULE_TIMEOUT ||
+            adev->video_timeout == MAX_SCHEDULE_TIMEOUT; }
+
   /**
    * amdgpu_ttm_access_memory - Read or Write memory that backs a

buffer object.

    *
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1453,6 +1518,10 @@ static int

amdgpu_ttm_access_memory(struct

Quoted 66 lines
Quoted 66 lines
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
 ttm_buffer_object *bo,
      if (bo->resource->mem_type != TTM_PL_VRAM)
              return -EIO;
 
Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -0
+    if (!amdgpu_ttm_allow_post_mortem_debug(adev) &&
+                    !amdgpu_ttm_access_memory_sdma(bo, offset,
+ buf,
 len, write))
+            return len;
+
      amdgpu_res_first(bo->resource, offset, len, &cursor);
      while (cursor.remaining) {
              size_t count, size = cursor.size; @@ -1793,6 +1862,12

@@ int amdgpu_ttm_init(struct amdgpu_device

*adev)

Quoted 51 lines
Quoted 51 lines
              return r;
      }
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +5 -0
+    if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
+                            AMDGPU_GEM_DOMAIN_GTT,
+                            &adev->mman.sdma_access_bo, NULL,
+                            adev->mman.sdma_access_ptr))
+            DRM_WARN("Debug VRAM access will use slowpath MM

access\n");

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+
      return 0;
   }
@@ -1823,6 +1898,9 @@ void amdgpu_ttm_fini(struct amdgpu_device
 *adev)
      ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
      ttm_device_fini(&adev->mman.bdev);
      adev->mman.initialized = false;
+    if (adev->mman.sdma_access_ptr)
+            amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo,
 NULL,
+                                    &adev->mman.sdma_access_ptr);
      DRM_INFO("amdgpu: ttm finalized\n");
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +4 -2
index 91a087f9dc7c..b0116c4a768f 100644
@@ -98,6 +98,10 @@ struct amdgpu_mman {
      u64             fw_vram_usage_size;
      struct amdgpu_bo        *fw_vram_usage_reserved_bo;
      void            *fw_vram_usage_va;
+
+    /* PAGE_SIZE'd BO for process memory r/w over SDMA. */
+    struct amdgpu_bo        *sdma_access_bo;
+    void                    *sdma_access_ptr;
   };
 
   struct amdgpu_copy_mem {
@@ -193,5 +197,4 @@ uint64_t amdgpu_ttm_tt_pte_flags(struct
 amdgpu_device *adev, struct ttm_tt *ttm,
   int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int
 mem_type);
 
   void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
-
   #endif
--

2.25.1