Skip to contentSkip to conversation

drm/amdgpu: refine amdgpu_bo_create_kernel_at()

PATCH Superseded Lang Yu 3 participants 4 messages Jul 28, 2023

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Lang Yu

author
#0 lore raw

PATCH v2 1/1 drm/amdgpu: refine amdgpu_bo_create_kernel_at()

Use amdgpu_bo_create_reserved() to create a BO in VRAM domain would fail if requested VRAM size is large(>128MB) on APU which usually has a default 512MB VRAM.

That's because VRAM is framgented after several allocations.

The approach is using amdgpu_bo_create_reserved() to create a BO in CPU domain first, it will always succeed.

v2: Don't overwrite the contents at specific offset.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 24 ++++++++++++++++------
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  3 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c    | 15 +++++++-------
 drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c   |  2 +-
 4 files changed, 28 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +18 -6
index ff73cc11d47e..fa30e96f27d0 100644
@@ -358,6 +358,7 @@ int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  * @offset: offset of the BO
  * @size: size of the BO
  * @bo_ptr:  used to initialize BOs in structures
+ * @gpu_addr: GPU addr of the pinned BO
  * @cpu_addr: optional CPU address mapping
  *
  * Creates a kernel BO at a specific offset in VRAM.
@@ -367,7 +368,8 @@ int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  */
 int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
 			       uint64_t offset, uint64_t size,
-			       struct amdgpu_bo **bo_ptr, void **cpu_addr)
+			       struct amdgpu_bo **bo_ptr,
+			       u64 *gpu_addr, void **cpu_addr)
 {
 	struct ttm_operation_ctx ctx = { false, false };
 	unsigned int i;
@@ -377,32 +379,42 @@ int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
 	size = ALIGN(size, PAGE_SIZE);
 
 	r = amdgpu_bo_create_reserved(adev, size, PAGE_SIZE,
-				      AMDGPU_GEM_DOMAIN_VRAM, bo_ptr, NULL,
-				      cpu_addr);
+				      AMDGPU_GEM_DOMAIN_CPU,
+				      bo_ptr, NULL, NULL);
 	if (r)
 		return r;
 
 	if ((*bo_ptr) == NULL)
 		return 0;
 
+	(*bo_ptr)->flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
+	(*bo_ptr)->flags |= cpu_addr ? AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
+		: AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
+
 	/*
 	 * Remove the original mem node and create a new one at the request
 	 * position.
 	 */
-	if (cpu_addr)
-		amdgpu_bo_kunmap(*bo_ptr);
-
 	ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource);
 
 	for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {
 		(*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;
 		(*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
+		(*bo_ptr)->placements[i].mem_type = TTM_PL_VRAM;
+		(*bo_ptr)->placements[i].flags = TTM_PL_FLAG_CONTIGUOUS;
+
+		if (!((*bo_ptr)->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED))
+			(*bo_ptr)->placements[i].flags |= TTM_PL_FLAG_TOPDOWN;
 	}
+
 	r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
 			     &(*bo_ptr)->tbo.resource, &ctx);
 	if (r)
 		goto error;
 
+	if (gpu_addr)
+		*gpu_addr = amdgpu_bo_gpu_offset(*bo_ptr);
+
 	if (cpu_addr) {
 		r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
 		if (r)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -1
index 5d3440d719e4..8f5b5664a1b6 100644
@@ -315,7 +315,8 @@ int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
 			    u64 *gpu_addr, void **cpu_addr);
 int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
 			       uint64_t offset, uint64_t size,
-			       struct amdgpu_bo **bo_ptr, void **cpu_addr);
+			       struct amdgpu_bo **bo_ptr,
+			       u64 *gpu_addr, void **cpu_addr);
 int amdgpu_bo_create_user(struct amdgpu_device *adev,
 			  struct amdgpu_bo_param *bp,
 			  struct amdgpu_bo_user **ubo_ptr);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -8
index 7c6dd3de1867..a210c243dac0 100644
@@ -1619,7 +1619,7 @@ static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
 					  adev->mman.fw_vram_usage_start_offset,
 					  adev->mman.fw_vram_usage_size,
 					  &adev->mman.fw_vram_usage_reserved_bo,
-					  &adev->mman.fw_vram_usage_va);
+					  NULL, &adev->mman.fw_vram_usage_va);
 }
 
 /**
@@ -1644,7 +1644,7 @@ static int amdgpu_ttm_drv_reserve_vram_init(struct amdgpu_device *adev)
 					  adev->mman.drv_vram_usage_start_offset,
 					  adev->mman.drv_vram_usage_size,
 					  &adev->mman.drv_vram_usage_reserved_bo,
-					  &adev->mman.drv_vram_usage_va);
+					  NULL, &adev->mman.drv_vram_usage_va);
 }
 
 /*
@@ -1729,8 +1729,7 @@ static int amdgpu_ttm_reserve_tmr(struct amdgpu_device *adev)
 		ret = amdgpu_bo_create_kernel_at(adev,
 						 ctx->c2p_train_data_offset,
 						 ctx->train_data_size,
-						 &ctx->c2p_bo,
-						 NULL);
+						 &ctx->c2p_bo, NULL, NULL);
 		if (ret) {
 			DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);
 			amdgpu_ttm_training_reserve_vram_fini(adev);
@@ -1742,7 +1741,7 @@ static int amdgpu_ttm_reserve_tmr(struct amdgpu_device *adev)
 	if (!adev->gmc.is_app_apu) {
 		ret = amdgpu_bo_create_kernel_at(
 			adev, adev->gmc.real_vram_size - reserve_size,
-			reserve_size, &adev->mman.fw_reserved_memory, NULL);
+			reserve_size, &adev->mman.fw_reserved_memory, NULL, NULL);
 		if (ret) {
 			DRM_ERROR("alloc tmr failed(%d)!\n", ret);
 			amdgpu_bo_free_kernel(&adev->mman.fw_reserved_memory,
@@ -1885,14 +1884,14 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
 		r = amdgpu_bo_create_kernel_at(adev, 0,
 					       adev->mman.stolen_vga_size,
 					       &adev->mman.stolen_vga_memory,
-					       NULL);
+					       NULL, NULL);
 		if (r)
 			return r;
 
 		r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_vga_size,
 					       adev->mman.stolen_extended_size,
 					       &adev->mman.stolen_extended_memory,
-					       NULL);
+					       NULL, NULL);
 
 		if (r)
 			return r;
@@ -1901,7 +1900,7 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
 					       adev->mman.stolen_reserved_offset,
 					       adev->mman.stolen_reserved_size,
 					       &adev->mman.stolen_reserved_memory,
-					       NULL);
+					       NULL, NULL);
 		if (r)
 			return r;
 	} else {
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +1 -1
index 41aa853a07d2..b93b42b916ce 100644
@@ -397,7 +397,7 @@ static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev)
 		 */
 		if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,
 					       AMDGPU_GPU_PAGE_SIZE,
-					       &bo, NULL))
+					       &bo, NULL, NULL))
 			DRM_DEBUG("RAS WARN: reserve vram for retired page %llx fail\n", bp);
 
 		data->bps_bo[i] = bo;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 32 lines · Lang Yu

Use amdgpu_bo_create_reserved() to create a BO in VRAM domain would fail if requested VRAM size is large(>128MB) on APU which usually has a default 512MB VRAM.

That's because VRAM is framgented after several allocations.

The approach is using amdgpu_bo_create_reserved() to create a BO in CPU domain first, it will always succeed.

v2: Don't overwrite the contents at specific offset.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 24 ++++++++++++++++------
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  3 ++-
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c    | 15 +++++++-------
  drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c   |  2 +-
  4 files changed, 28 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
index ff73cc11d47e..fa30e96f27d0 100644
@@ -358,6 +358,7 @@ int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
   * @offset: offset of the BO
   * @size: size of the BO
   * @bo_ptr:  used to initialize BOs in structures
+ * @gpu_addr: GPU addr of the pinned BO
   * @cpu_addr: optional CPU address mapping
   *
   * Creates a kernel BO at a specific offset in VRAM.

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c int amdgpu_bo_create_kernel(struct amdgpu_device *adev, · line 372

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -1
@@ -367,7 +368,8 @@ int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
   */
  int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
  			       uint64_t offset, uint64_t size,
-			       struct amdgpu_bo **bo_ptr, void **cpu_addr)
+			       struct amdgpu_bo **bo_ptr,
+			       u64 *gpu_addr, void **cpu_addr)

A generic question (not considering other details in this patch) - this API is literally asking to create BO at a particular GPU VRAM offset. The offset goes in as the input, so why does it need to return a GPU offset?

Thanks,
Lijo

Quoted 145 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +15 -5
  {
  	struct ttm_operation_ctx ctx = { false, false };
  	unsigned int i;
@@ -377,32 +379,42 @@ int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
  	size = ALIGN(size, PAGE_SIZE);
 
  	r = amdgpu_bo_create_reserved(adev, size, PAGE_SIZE,
-				      AMDGPU_GEM_DOMAIN_VRAM, bo_ptr, NULL,
-				      cpu_addr);
+				      AMDGPU_GEM_DOMAIN_CPU,
+				      bo_ptr, NULL, NULL);
  	if (r)
  		return r;
 
  	if ((*bo_ptr) == NULL)
  		return 0;
 
+	(*bo_ptr)->flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
+	(*bo_ptr)->flags |= cpu_addr ? AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
+		: AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
+
  	/*
  	 * Remove the original mem node and create a new one at the request
  	 * position.
  	 */
-	if (cpu_addr)
-		amdgpu_bo_kunmap(*bo_ptr);
-
  	ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource);
 
  	for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {
  		(*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;
  		(*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
+		(*bo_ptr)->placements[i].mem_type = TTM_PL_VRAM;
+		(*bo_ptr)->placements[i].flags = TTM_PL_FLAG_CONTIGUOUS;
+
+		if (!((*bo_ptr)->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED))
+			(*bo_ptr)->placements[i].flags |= TTM_PL_FLAG_TOPDOWN;
  	}
+
  	r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
  			     &(*bo_ptr)->tbo.resource, &ctx);
  	if (r)
  		goto error;
 
+	if (gpu_addr)
+		*gpu_addr = amdgpu_bo_gpu_offset(*bo_ptr);
+
  	if (cpu_addr) {
  		r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
  		if (r)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -1
index 5d3440d719e4..8f5b5664a1b6 100644
@@ -315,7 +315,8 @@ int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
  			    u64 *gpu_addr, void **cpu_addr);
  int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
  			       uint64_t offset, uint64_t size,
-			       struct amdgpu_bo **bo_ptr, void **cpu_addr);
+			       struct amdgpu_bo **bo_ptr,
+			       u64 *gpu_addr, void **cpu_addr);
  int amdgpu_bo_create_user(struct amdgpu_device *adev,
  			  struct amdgpu_bo_param *bp,
  			  struct amdgpu_bo_user **ubo_ptr);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -8
index 7c6dd3de1867..a210c243dac0 100644
@@ -1619,7 +1619,7 @@ static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
  					  adev->mman.fw_vram_usage_start_offset,
  					  adev->mman.fw_vram_usage_size,
  					  &adev->mman.fw_vram_usage_reserved_bo,
-					  &adev->mman.fw_vram_usage_va);
+					  NULL, &adev->mman.fw_vram_usage_va);
  }
 
  /**
@@ -1644,7 +1644,7 @@ static int amdgpu_ttm_drv_reserve_vram_init(struct amdgpu_device *adev)
  					  adev->mman.drv_vram_usage_start_offset,
  					  adev->mman.drv_vram_usage_size,
  					  &adev->mman.drv_vram_usage_reserved_bo,
-					  &adev->mman.drv_vram_usage_va);
+					  NULL, &adev->mman.drv_vram_usage_va);
  }
 
  /*
@@ -1729,8 +1729,7 @@ static int amdgpu_ttm_reserve_tmr(struct amdgpu_device *adev)
  		ret = amdgpu_bo_create_kernel_at(adev,
  						 ctx->c2p_train_data_offset,
  						 ctx->train_data_size,
-						 &ctx->c2p_bo,
-						 NULL);
+						 &ctx->c2p_bo, NULL, NULL);
  		if (ret) {
  			DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);
  			amdgpu_ttm_training_reserve_vram_fini(adev);
@@ -1742,7 +1741,7 @@ static int amdgpu_ttm_reserve_tmr(struct amdgpu_device *adev)
  	if (!adev->gmc.is_app_apu) {
  		ret = amdgpu_bo_create_kernel_at(
  			adev, adev->gmc.real_vram_size - reserve_size,
-			reserve_size, &adev->mman.fw_reserved_memory, NULL);
+			reserve_size, &adev->mman.fw_reserved_memory, NULL, NULL);
  		if (ret) {
  			DRM_ERROR("alloc tmr failed(%d)!\n", ret);
  			amdgpu_bo_free_kernel(&adev->mman.fw_reserved_memory,
@@ -1885,14 +1884,14 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
  		r = amdgpu_bo_create_kernel_at(adev, 0,
  					       adev->mman.stolen_vga_size,
  					       &adev->mman.stolen_vga_memory,
-					       NULL);
+					       NULL, NULL);
  		if (r)
  			return r;
 
  		r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_vga_size,
  					       adev->mman.stolen_extended_size,
  					       &adev->mman.stolen_extended_memory,
-					       NULL);
+					       NULL, NULL);
 
  		if (r)
  			return r;
@@ -1901,7 +1900,7 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
  					       adev->mman.stolen_reserved_offset,
  					       adev->mman.stolen_reserved_size,
  					       &adev->mman.stolen_reserved_memory,
-					       NULL);
+					       NULL, NULL);
  		if (r)
  			return r;
  	} else {
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +1 -1
index 41aa853a07d2..b93b42b916ce 100644
@@ -397,7 +397,7 @@ static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev)
  		 */
  		if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,
  					       AMDGPU_GPU_PAGE_SIZE,
-					       &bo, NULL))
+					       &bo, NULL, NULL))
  			DRM_DEBUG("RAS WARN: reserve vram for retired page %llx fail\n", bp);
 
  		data->bps_bo[i] = bo;
maintainer
#2 lore raw

Am 28.07.23 um 09:00 schrieb Lazar, Lijo:

Quoted 37 lines · Lang Yu
Quoted 34 lines

Use amdgpu_bo_create_reserved() to create a BO in VRAM domain would fail if requested VRAM size is large(>128MB) on APU which usually has a default 512MB VRAM.

That's because VRAM is framgented after several allocations.

The approach is using amdgpu_bo_create_reserved() to create a BO in CPU domain first, it will always succeed.

v2: Don't overwrite the contents at specific offset.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 24 ++++++++++++++++------ drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  3 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c    | 15 +++++++------- drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c   |  2 +-

  4 files changed, 28 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
index ff73cc11d47e..fa30e96f27d0 100644
@@ -358,6 +358,7 @@ int amdgpu_bo_create_kernel(struct amdgpu_device 
 *adev,
    * @offset: offset of the BO
    * @size: size of the BO
    * @bo_ptr:  used to initialize BOs in structures
+ * @gpu_addr: GPU addr of the pinned BO
    * @cpu_addr: optional CPU address mapping
    *

   * Creates a kernel BO at a specific offset in VRAM.

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
@@ -367,7 +368,8 @@ int amdgpu_bo_create_kernel(struct amdgpu_device 
 *adev,
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c int amdgpu_bo_create_kernel(struct amdgpu_device *adev, · line 372

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -1
    */
   int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
                      uint64_t offset, uint64_t size,
-                   struct amdgpu_bo **bo_ptr, void **cpu_addr)
+                   struct amdgpu_bo **bo_ptr,
+                   u64 *gpu_addr, void **cpu_addr)

A generic question (not considering other details in this patch) - this API is literally asking to create BO at a particular GPU VRAM offset. The offset goes in as the input, so why does it need to return a GPU offset?

Yeah, that's exactly why I said that the intention of the function was misunderstood.

This change here doesn't seem to make much sense.

Regards,
Christian.

Quoted 148 lines

Thanks,
Lijo

Quoted 144 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
   {
       struct ttm_operation_ctx ctx = { false, false };

      unsigned int i;

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +3 -2
@@ -377,32 +379,42 @@ int amdgpu_bo_create_kernel_at(struct 
 amdgpu_device *adev,
       size = ALIGN(size, PAGE_SIZE);
         r = amdgpu_bo_create_reserved(adev, size, PAGE_SIZE,
-                      AMDGPU_GEM_DOMAIN_VRAM, bo_ptr, NULL,
-                      cpu_addr);
+                      AMDGPU_GEM_DOMAIN_CPU,
+                      bo_ptr, NULL, NULL);
       if (r)
           return r;
         if ((*bo_ptr) == NULL)
           return 0;
   +    (*bo_ptr)->flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
+    (*bo_ptr)->flags |= cpu_addr ? 

AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
+        : AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
+
      /*
       * Remove the original mem node and create a new one at the request
       * position.
       */
-    if (cpu_addr)
-        amdgpu_bo_kunmap(*bo_ptr);
-
      ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource); for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) { (*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT; (*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT; +        (*bo_ptr)->placements[i].mem_type = TTM_PL_VRAM;
+        (*bo_ptr)->placements[i].flags = TTM_PL_FLAG_CONTIGUOUS; +
+        if (!((*bo_ptr)->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED))
+            (*bo_ptr)->placements[i].flags |= TTM_PL_FLAG_TOPDOWN; }
+
      r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement, &(*bo_ptr)->tbo.resource, &ctx);
      if (r)
          goto error;
  +    if (gpu_addr)
+        *gpu_addr = amdgpu_bo_gpu_offset(*bo_ptr);
+
      if (cpu_addr) {
          r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
          if (r)

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -1
index 5d3440d719e4..8f5b5664a1b6 100644
@@ -315,7 +315,8 @@ int amdgpu_bo_create_kernel(struct amdgpu_device 
 *adev,
                   u64 *gpu_addr, void **cpu_addr);
   int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
                      uint64_t offset, uint64_t size,
-                   struct amdgpu_bo **bo_ptr, void **cpu_addr);
+                   struct amdgpu_bo **bo_ptr,
+                   u64 *gpu_addr, void **cpu_addr);
   int amdgpu_bo_create_user(struct amdgpu_device *adev,
                 struct amdgpu_bo_param *bp,

                struct amdgpu_bo_user **ubo_ptr);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -4
index 7c6dd3de1867..a210c243dac0 100644
@@ -1619,7 +1619,7 @@ static int 
 amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
 adev->mman.fw_vram_usage_start_offset,
                         adev->mman.fw_vram_usage_size,
 &adev->mman.fw_vram_usage_reserved_bo,
-                      &adev->mman.fw_vram_usage_va);
+                      NULL, &adev->mman.fw_vram_usage_va);
   }
     /**
@@ -1644,7 +1644,7 @@ static int 
 amdgpu_ttm_drv_reserve_vram_init(struct amdgpu_device *adev)
 adev->mman.drv_vram_usage_start_offset,
                         adev->mman.drv_vram_usage_size,
 &adev->mman.drv_vram_usage_reserved_bo,
-                      &adev->mman.drv_vram_usage_va);
+                      NULL, &adev->mman.drv_vram_usage_va);
   }
     /*
@@ -1729,8 +1729,7 @@ static int amdgpu_ttm_reserve_tmr(struct 
 amdgpu_device *adev)
           ret = amdgpu_bo_create_kernel_at(adev,
                            ctx->c2p_train_data_offset,
                            ctx->train_data_size,
-                         &ctx->c2p_bo,
-                         NULL);
+                         &ctx->c2p_bo, NULL, NULL);
           if (ret) {
               DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);

              amdgpu_ttm_training_reserve_vram_fini(adev);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1742,7 +1741,7 @@ static int amdgpu_ttm_reserve_tmr(struct 
 amdgpu_device *adev)
       if (!adev->gmc.is_app_apu) {

          ret = amdgpu_bo_create_kernel_at(
              adev, adev->gmc.real_vram_size - reserve_size, -            reserve_size, &adev->mman.fw_reserved_memory, NULL); +            reserve_size, &adev->mman.fw_reserved_memory, NULL, NULL); if (ret) {
              DRM_ERROR("alloc tmr failed(%d)!\n", ret); amdgpu_bo_free_kernel(&adev->mman.fw_reserved_memory,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -3
@@ -1885,14 +1884,14 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
           r = amdgpu_bo_create_kernel_at(adev, 0,
                              adev->mman.stolen_vga_size,
 &adev->mman.stolen_vga_memory,
-                           NULL);
+                           NULL, NULL);
           if (r)
               return r;
             r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_vga_size,
                              adev->mman.stolen_extended_size,
 &adev->mman.stolen_extended_memory,
-                           NULL);
+                           NULL, NULL);
             if (r)
               return r;
@@ -1901,7 +1900,7 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
 adev->mman.stolen_reserved_offset,
                              adev->mman.stolen_reserved_size,
 &adev->mman.stolen_reserved_memory,
-                           NULL);
+                           NULL, NULL);
           if (r)
               return r;
       } else {
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +1 -1
index 41aa853a07d2..b93b42b916ce 100644
@@ -397,7 +397,7 @@ static void amdgpu_virt_ras_reserve_bps(struct 
 amdgpu_device *adev)
            */
           if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,
                              AMDGPU_GPU_PAGE_SIZE,
-                           &bo, NULL))
+                           &bo, NULL, NULL))
               DRM_DEBUG("RAS WARN: reserve vram for retired page %llx fail\n", bp);
             data->bps_bo[i] = bo;
author
#3 lore raw
Quoted 51 lines · Christian König

Am 28.07.23 um 09:00 schrieb Lazar, Lijo:

Quoted 41 lines · Lang Yu
Quoted 38 lines

Use amdgpu_bo_create_reserved() to create a BO in VRAM domain would fail if requested VRAM size is large(>128MB) on APU which usually has a default 512MB VRAM.

That's because VRAM is framgented after several allocations.

The approach is using amdgpu_bo_create_reserved() to create a BO in CPU domain first, it will always succeed.

v2: Don't overwrite the contents at specific offset.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 24 ++++++++++++++++------ drivers/gpu/drm/amd/amdgpu/amdgpu_object.h |  3 ++- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c    | 15 +++++++------- drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c   |  2 +-

  4 files changed, 28 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +1 -0
index ff73cc11d47e..fa30e96f27d0 100644
@@ -358,6 +358,7 @@ int amdgpu_bo_create_kernel(struct amdgpu_device
 *adev,
    * @offset: offset of the BO
    * @size: size of the BO
    * @bo_ptr:  used to initialize BOs in structures
+ * @gpu_addr: GPU addr of the pinned BO
    * @cpu_addr: optional CPU address mapping
    *

   * Creates a kernel BO at a specific offset in VRAM.

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
@@ -367,7 +368,8 @@ int amdgpu_bo_create_kernel(struct amdgpu_device
 *adev,
    */
   int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
                      uint64_t offset, uint64_t size,
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c int amdgpu_bo_create_kernel(struct amdgpu_device *adev, · line 372

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +2 -1
-                   struct amdgpu_bo **bo_ptr, void **cpu_addr)
+                   struct amdgpu_bo **bo_ptr,
+                   u64 *gpu_addr, void **cpu_addr)

A generic question (not considering other details in this patch) - this API is literally asking to create BO at a particular GPU VRAM offset. The offset goes in as the input, so why does it need to return a GPU offset?

Quoted 4 lines

Yeah, that's exactly why I said that the intention of the function was misunderstood.

This change here doesn't seem to make much sense.

Because we have such use case acutally. When creating a TMR BO, we also acquire it's GPU address. Of course, we can call amdgpu_bo_gpu_offset(tmr_bo) to acquire it.

Regards,
Lang

Quoted 159 lines

Regards,
Christian.

Quoted 155 lines

Thanks,
Lijo

Quoted 151 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +0 -0
   {
       struct ttm_operation_ctx ctx = { false, false };

      unsigned int i;

drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +3 -2
@@ -377,32 +379,42 @@ int amdgpu_bo_create_kernel_at(struct
 amdgpu_device *adev,
       size = ALIGN(size, PAGE_SIZE);
         r = amdgpu_bo_create_reserved(adev, size, PAGE_SIZE,
-                      AMDGPU_GEM_DOMAIN_VRAM, bo_ptr, NULL,
-                      cpu_addr);
+                      AMDGPU_GEM_DOMAIN_CPU,
+                      bo_ptr, NULL, NULL);
       if (r)
           return r;
         if ((*bo_ptr) == NULL)
           return 0;
   +    (*bo_ptr)->flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
+    (*bo_ptr)->flags |= cpu_addr ?

AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
+        : AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
+
      /*
       * Remove the original mem node and create a new one at the request
       * position.
       */
-    if (cpu_addr)
-        amdgpu_bo_kunmap(*bo_ptr);
-
      ttm_resource_free(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.resource); for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) { (*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT; (*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT; +        (*bo_ptr)->placements[i].mem_type = TTM_PL_VRAM;
+        (*bo_ptr)->placements[i].flags = TTM_PL_FLAG_CONTIGUOUS; +
+        if (!((*bo_ptr)->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED))
+            (*bo_ptr)->placements[i].flags |= TTM_PL_FLAG_TOPDOWN; }
+
      r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement, &(*bo_ptr)->tbo.resource, &ctx);
      if (r)
          goto error;
  +    if (gpu_addr)
+        *gpu_addr = amdgpu_bo_gpu_offset(*bo_ptr);
+
      if (cpu_addr) {
          r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
          if (r)

drivers/gpu/drm/amd/amdgpu/amdgpu_object.h +2 -1
index 5d3440d719e4..8f5b5664a1b6 100644
@@ -315,7 +315,8 @@ int amdgpu_bo_create_kernel(struct amdgpu_device
 *adev,
                   u64 *gpu_addr, void **cpu_addr);
   int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
                      uint64_t offset, uint64_t size,
-                   struct amdgpu_bo **bo_ptr, void **cpu_addr);
+                   struct amdgpu_bo **bo_ptr,
+                   u64 *gpu_addr, void **cpu_addr);
   int amdgpu_bo_create_user(struct amdgpu_device *adev,
                 struct amdgpu_bo_param *bp,

                struct amdgpu_bo_user **ubo_ptr);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -4
index 7c6dd3de1867..a210c243dac0 100644
@@ -1619,7 +1619,7 @@ static int
 amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
 adev->mman.fw_vram_usage_start_offset,
                         adev->mman.fw_vram_usage_size,
 &adev->mman.fw_vram_usage_reserved_bo,
-                      &adev->mman.fw_vram_usage_va);
+                      NULL, &adev->mman.fw_vram_usage_va);
   }
     /**
@@ -1644,7 +1644,7 @@ static int
 amdgpu_ttm_drv_reserve_vram_init(struct amdgpu_device *adev)
 adev->mman.drv_vram_usage_start_offset,
                         adev->mman.drv_vram_usage_size,
 &adev->mman.drv_vram_usage_reserved_bo,
-                      &adev->mman.drv_vram_usage_va);
+                      NULL, &adev->mman.drv_vram_usage_va);
   }
     /*
@@ -1729,8 +1729,7 @@ static int amdgpu_ttm_reserve_tmr(struct
 amdgpu_device *adev)
           ret = amdgpu_bo_create_kernel_at(adev,
                            ctx->c2p_train_data_offset,
                            ctx->train_data_size,
-                         &ctx->c2p_bo,
-                         NULL);
+                         &ctx->c2p_bo, NULL, NULL);
           if (ret) {
               DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);

              amdgpu_ttm_training_reserve_vram_fini(adev);

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -1742,7 +1741,7 @@ static int amdgpu_ttm_reserve_tmr(struct
 amdgpu_device *adev)
       if (!adev->gmc.is_app_apu) {

          ret = amdgpu_bo_create_kernel_at(
              adev, adev->gmc.real_vram_size - reserve_size, -            reserve_size, &adev->mman.fw_reserved_memory, NULL); +            reserve_size, &adev->mman.fw_reserved_memory, NULL, NULL); if (ret) {
              DRM_ERROR("alloc tmr failed(%d)!\n", ret); amdgpu_bo_free_kernel(&adev->mman.fw_reserved_memory,

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +3 -3
@@ -1885,14 +1884,14 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
           r = amdgpu_bo_create_kernel_at(adev, 0,
                              adev->mman.stolen_vga_size,
 &adev->mman.stolen_vga_memory,
-                           NULL);
+                           NULL, NULL);
           if (r)
               return r;
             r = amdgpu_bo_create_kernel_at(adev,
 adev->mman.stolen_vga_size,
                              adev->mman.stolen_extended_size,
 &adev->mman.stolen_extended_memory,
-                           NULL);
+                           NULL, NULL);
             if (r)
               return r;
@@ -1901,7 +1900,7 @@ int amdgpu_ttm_init(struct amdgpu_device *adev)
 adev->mman.stolen_reserved_offset,
                              adev->mman.stolen_reserved_size,
 &adev->mman.stolen_reserved_memory,
-                           NULL);
+                           NULL, NULL);
           if (r)
               return r;
       } else {
drivers/gpu/drm/amd/amdgpu/amdgpu_virt.c +0 -0
index 41aa853a07d2..b93b42b916ce 100644
@@ -397,7 +397,7 @@ static void amdgpu_virt_ras_reserve_bps(struct
 amdgpu_device *adev)
            */

          if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,
                             AMDGPU_GPU_PAGE_SIZE,
-                           &bo, NULL))
+                           &bo, NULL, NULL)) DRM_DEBUG("RAS WARN: reserve vram for retired page %llx fail\n", bp);
            data->bps_bo[i] = bo;