Skip to contentSkip to conversation

drm/amdgpu/uvd: Fix UVD BO memory placement issues

PATCH In review Timur Kristóf 2 participants 14 messages May 19

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

cover drm/amdgpu/uvd: Fix UVD BO memory placement issues

UVD 4.x and older have two requirements for CS BOs:
1. All BOs must not cross 256M segments
2. MSG and FB BOs must be located in the same segment as the VCPU BO

The amdgpu_uvd code attempts to solve those requirements, but unfortunately it has hit various limitations:

* VCPU BO may be placed in a different segment
* VRAM allocations may cross 256M in low memory scenarios
* GTT manager doesn't respect placement requirements
* GTT allocations may cross 256M
* GTT->GTT moves are not implemented

Let's solve these issues by fixing the GTT manager, making sure that GTT allocations are placed in 256M segments and VRAM allocations are moved to GTT when they cross 256M. It also fixes forcing MSG and FB BOs to the UVD segment when the UVD segment isn't the first segment, which can be the case when resizable BAR is enabled.

This series should be backported to 7.0 and 7.1 because technically this may have been a regression for some users caused by switching to amdgpu by default.

Timur Kristóf (5):
drm/amdgpu: Respect placement requirements in amdgpu_gtt_mgr functions drm/amdgpu: Use placements of 256M GART segments for SI/CIK drm/amdgpu/uvd: Place VCPU BO only in VRAM for UVD 4.x and older drm/amdgpu/uvd: Fix forcing BOs into UVD segment when it isn't at 0 drm/amdgpu/uvd: Move BOs to GTT when we can't place them in VRAM
correctly

 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 30 ++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c     | 57 ++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     |  3 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c     | 74 +++++++++++++++------
 4 files changed, 136 insertions(+), 28 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/5 drm/amdgpu: Respect placement requirements in amdgpu_gtt_mgr functions

When testing intersection and compatibility, respect the actual placement requirements. This is a pre-requisite for ensuring that UVD CS BOs do not cross 256M segments.

Fixes ded910f368a5 ("drm/amdgpu: Implement intersect/compatible functions")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 30 +++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +28 -2
index 02f85802f579..19b6770a877d 100644
@@ -272,7 +272,20 @@ static bool amdgpu_gtt_mgr_intersects(struct ttm_resource_manager *man,
 				      const struct ttm_place *place,
 				      size_t size)
 {
-	return !place->lpfn || amdgpu_gtt_mgr_has_gart_addr(res);
+	const struct drm_mm_node *const node = &to_ttm_range_mgr_node(res)->mm_nodes[0];
+	const u32 num_pages = PFN_UP(size);
+
+	if (!place->lpfn)
+		return true;
+
+	if (!amdgpu_gtt_mgr_has_gart_addr(res))
+		return false;
+
+	if (place->fpfn >= (node->start + num_pages) ||
+	    (place->lpfn && place->lpfn <= node->start))
+		return false;
+
+	return true;
 }
 
 /**
@@ -290,7 +303,20 @@ static bool amdgpu_gtt_mgr_compatible(struct ttm_resource_manager *man,
 				      const struct ttm_place *place,
 				      size_t size)
 {
-	return !place->lpfn || amdgpu_gtt_mgr_has_gart_addr(res);
+	const struct drm_mm_node *const node = &to_ttm_range_mgr_node(res)->mm_nodes[0];
+	const u32 num_pages = PFN_UP(size);
+
+	if (!place->lpfn)
+		return true;
+
+	if (!amdgpu_gtt_mgr_has_gart_addr(res))
+		return false;
+
+	if (node->start < place->fpfn ||
+	    (place->lpfn && (node->start + num_pages) > place->lpfn))
+		return false;
+
+	return true;
 }
 
 /**
Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] drm/amdgpu: Respect placement requirements in amdgpu_gtt_mgr functions

Quoted 7 lines · Timur Kristóf

When testing intersection and compatibility, respect the actual placement requirements. This is a pre-requisite for ensuring that UVD CS BOs do not cross 256M segments.

Fixes ded910f368a5 ("drm/amdgpu: Implement intersect/compatible functions")
Quoted 52 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 30 +++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +28 -2
index 02f85802f579..19b6770a877d 100644
@@ -272,7 +272,20 @@ static bool amdgpu_gtt_mgr_intersects(struct ttm_resource_manager *man,
 				      const struct ttm_place *place,
 				      size_t size)
 {
-	return !place->lpfn || amdgpu_gtt_mgr_has_gart_addr(res);
+	const struct drm_mm_node *const node = &to_ttm_range_mgr_node(res)->mm_nodes[0];
+	const u32 num_pages = PFN_UP(size);
+
+	if (!place->lpfn)
+		return true;
+
+	if (!amdgpu_gtt_mgr_has_gart_addr(res))
+		return false;
+
+	if (place->fpfn >= (node->start + num_pages) ||
+	    (place->lpfn && place->lpfn <= node->start))
+		return false;
+
+	return true;
 }
 
 /**
@@ -290,7 +303,20 @@ static bool amdgpu_gtt_mgr_compatible(struct ttm_resource_manager *man,
 				      const struct ttm_place *place,
 				      size_t size)
 {
-	return !place->lpfn || amdgpu_gtt_mgr_has_gart_addr(res);
+	const struct drm_mm_node *const node = &to_ttm_range_mgr_node(res)->mm_nodes[0];
+	const u32 num_pages = PFN_UP(size);
+
+	if (!place->lpfn)
+		return true;
+
+	if (!amdgpu_gtt_mgr_has_gart_addr(res))
+		return false;
+
+	if (node->start < place->fpfn ||
+	    (place->lpfn && (node->start + num_pages) > place->lpfn))
+		return false;
+
+	return true;
 }
 
 /**
#3 lore raw

PATCH 2/5 drm/amdgpu: Use placements of 256M GART segments for SI/CIK

In reply to Timur Kristóf

UVD 4.x and older require that BOs don't cross 256M segments. We need to respect that in amdgpu_ttm_alloc_gart(). We can't move the BOs later because GTT->GTT moves are not implemented. We also can't force all BOs to VRAM because that becomes very problematic in low VRAM scenarios.

This fixes UVD CS BOs crossing 256M segments
when they are placed in the GART.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 56 ++++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  3 ++
 2 files changed, 53 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +50 -6
index 6c6ab4dd6ea9..a106c7e77e26 100644
@@ -959,6 +959,40 @@ static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
 	return 0;
 }
 
+/**
+ * amdgpu_ttm_fill_gart_256M_placements() - Fill placements array with 256M GART segments
+ *
+ * @bo: TTM buffer objects whose placements should be filled
+ * @placements: Pointer to an array of placements
+ * @max_placements: Size of the placements array
+ *
+ * Fill the specified placements array with 256M GART segments,
+ * starting from the highest address in order to reduce the
+ * contention of the lowest segment.
+ *
+ * Returns the number of placements filled.
+ */
+u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
+					 struct ttm_place *placements,
+					 u32 max_placements)
+{
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+	u32 i;
+
+	/* Fill the placements array with 256M segments, starting from highest. */
+	for (i = 0; i < max_placements; ++i) {
+		if (i * SZ_256M >= adev->gmc.gart_size)
+			break;
+
+		placements[i].lpfn = (adev->gmc.gart_size - i * SZ_256M) >> PAGE_SHIFT;
+		placements[i].fpfn = ALIGN_DOWN(placements[i].lpfn - 1, SZ_256M >> PAGE_SHIFT);
+		placements[i].mem_type = TTM_PL_TT;
+		placements[i].flags = bo->resource->placement;
+	}
+
+	return i;
+}
+
 /*
  * amdgpu_ttm_alloc_gart - Make sure buffer object is accessible either
  * through AGP or GART aperture.
@@ -973,7 +1007,7 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
 	struct ttm_operation_ctx ctx = { false, false };
 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
 	struct ttm_placement placement;
-	struct ttm_place placements;
+	struct ttm_place placements[AMDGPU_BO_MAX_PLACEMENTS];
 	struct ttm_resource *tmp;
 	uint64_t addr, flags;
 	int r;
@@ -987,11 +1021,21 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
 
 	/* allocate GART space */
 	placement.num_placement = 1;
-	placement.placement = &placements;
-	placements.fpfn = 0;
-	placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
-	placements.mem_type = TTM_PL_TT;
-	placements.flags = bo->resource->placement;
+	placement.placement = &placements[0];
+	placements[0].fpfn = 0;
+	placements[0].lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
+	placements[0].mem_type = TTM_PL_TT;
+	placements[0].flags = bo->resource->placement;
+
+	/*
+	 * UVD 4.x and older require that BOs don't cross 256M segments.
+	 * We need to respect that here. We can't move the BO later
+	 * because GTT->GTT moves are not implemented.
+	 */
+	if (bo->base.size < SZ_256M && adev->family <= AMDGPU_FAMILY_KV)
+		placement.num_placement =
+			amdgpu_ttm_fill_gart_256M_placements(bo, placements,
+							     ARRAY_SIZE(placements));
 
 	r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
 	if (unlikely(r))
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -0
index 2d72fa217274..e9de628c8d2d 100644
@@ -202,6 +202,9 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_ttm_buffer_entity *entity,
 			    u64 k_job_id);
 struct amdgpu_ttm_buffer_entity *amdgpu_ttm_next_clear_entity(struct amdgpu_device *adev);
 
+u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
+					 struct ttm_place *placements,
+					 u32 max_placements);
 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
Signature · 1 line
2.54.0
maintainer
#4 lore raw

Re: [PATCH 2/5] drm/amdgpu: Use placements of 256M GART segments for SI/CIK

Quoted 8 lines · Timur Kristóf

UVD 4.x and older require that BOs don't cross 256M segments. We need to respect that in amdgpu_ttm_alloc_gart(). We can't move the BOs later because GTT->GTT moves are not implemented. We also can't force all BOs to VRAM because that becomes very problematic in low VRAM scenarios.

This fixes UVD CS BOs crossing 256M segments
when they are placed in the GART.

Clear NAK for that approach.

This is the general TTM interface function and shouldn't have any HW generation dependent code in it.

Regards,
Christian.

Quoted 103 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 56 ++++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  3 ++
 2 files changed, 53 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +50 -6
index 6c6ab4dd6ea9..a106c7e77e26 100644
@@ -959,6 +959,40 @@ static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
 	return 0;
 }
 
+/**
+ * amdgpu_ttm_fill_gart_256M_placements() - Fill placements array with 256M GART segments
+ *
+ * @bo: TTM buffer objects whose placements should be filled
+ * @placements: Pointer to an array of placements
+ * @max_placements: Size of the placements array
+ *
+ * Fill the specified placements array with 256M GART segments,
+ * starting from the highest address in order to reduce the
+ * contention of the lowest segment.
+ *
+ * Returns the number of placements filled.
+ */
+u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
+					 struct ttm_place *placements,
+					 u32 max_placements)
+{
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+	u32 i;
+
+	/* Fill the placements array with 256M segments, starting from highest. */
+	for (i = 0; i < max_placements; ++i) {
+		if (i * SZ_256M >= adev->gmc.gart_size)
+			break;
+
+		placements[i].lpfn = (adev->gmc.gart_size - i * SZ_256M) >> PAGE_SHIFT;
+		placements[i].fpfn = ALIGN_DOWN(placements[i].lpfn - 1, SZ_256M >> PAGE_SHIFT);
+		placements[i].mem_type = TTM_PL_TT;
+		placements[i].flags = bo->resource->placement;
+	}
+
+	return i;
+}
+
 /*
  * amdgpu_ttm_alloc_gart - Make sure buffer object is accessible either
  * through AGP or GART aperture.
@@ -973,7 +1007,7 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
 	struct ttm_operation_ctx ctx = { false, false };
 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
 	struct ttm_placement placement;
-	struct ttm_place placements;
+	struct ttm_place placements[AMDGPU_BO_MAX_PLACEMENTS];
 	struct ttm_resource *tmp;
 	uint64_t addr, flags;
 	int r;
@@ -987,11 +1021,21 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
 
 	/* allocate GART space */
 	placement.num_placement = 1;
-	placement.placement = &placements;
-	placements.fpfn = 0;
-	placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
-	placements.mem_type = TTM_PL_TT;
-	placements.flags = bo->resource->placement;
+	placement.placement = &placements[0];
+	placements[0].fpfn = 0;
+	placements[0].lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
+	placements[0].mem_type = TTM_PL_TT;
+	placements[0].flags = bo->resource->placement;
+
+	/*
+	 * UVD 4.x and older require that BOs don't cross 256M segments.
+	 * We need to respect that here. We can't move the BO later
+	 * because GTT->GTT moves are not implemented.
+	 */
+	if (bo->base.size < SZ_256M && adev->family <= AMDGPU_FAMILY_KV)
+		placement.num_placement =
+			amdgpu_ttm_fill_gart_256M_placements(bo, placements,
+							     ARRAY_SIZE(placements));
 
 	r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
 	if (unlikely(r))
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +3 -0
index 2d72fa217274..e9de628c8d2d 100644
@@ -202,6 +202,9 @@ int amdgpu_ttm_clear_buffer(struct amdgpu_ttm_buffer_entity *entity,
 			    u64 k_job_id);
 struct amdgpu_ttm_buffer_entity *amdgpu_ttm_next_clear_entity(struct amdgpu_device *adev);
 
+u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
+					 struct ttm_place *placements,
+					 u32 max_placements);
 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
#5 lore raw

Re: [PATCH 2/5] drm/amdgpu: Use placements of 256M GART segments for SI/CIK

Quoted 7 lines · Central European Summer Time Christian König
Quoted 6 lines · Timur Kristóf

UVD 4.x and older require that BOs don't cross 256M segments. We need to respect that in amdgpu_ttm_alloc_gart(). We can't move the BOs later because GTT->GTT moves are not implemented. We also can't force all BOs to VRAM because that becomes very problematic in low VRAM scenarios.

Quoted 7 lines
Quoted 2 lines

This fixes UVD CS BOs crossing 256M segments
when they are placed in the GART.

Clear NAK for that approach.

This is the general TTM interface function and shouldn't have any HW generation dependent code in it.

I don't see how else to solve this, since GTT->GTT moves are not implemented, so we can't move the BO to a suitable address later. We also can't move it to VRAM.

Please suggest a better approach if you don't like this one.

Quoted 23 lines

Regards,
Christian.

Quoted 19 lines

---

 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 56 ++++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  3 ++
 2 files changed, 53 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c index
6c6ab4dd6ea9..a106c7e77e26 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -959,6 +959,40 @@ static int amdgpu_ttm_backend_bind(struct ttm_device
 *bdev,> 
 	return 0;
 
 }
 
Quoted 15 lines
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
+/**
+ * amdgpu_ttm_fill_gart_256M_placements() - Fill placements array with

256M GART segments + *
+ * @bo: TTM buffer objects whose placements should be filled
+ * @placements: Pointer to an array of placements
+ * @max_placements: Size of the placements array
+ *
+ * Fill the specified placements array with 256M GART segments,
+ * starting from the highest address in order to reduce the
+ * contention of the lowest segment.
+ *
+ * Returns the number of placements filled.
+ */

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
+u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
+					 struct ttm_place 

*placements,

Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +6 -0
+					 u32 max_placements)
+{
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+	u32 i;
+
+	/* Fill the placements array with 256M segments, starting from 

highest.

Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,

Quoted 5 lines

*/ + for (i = 0; i < max_placements; ++i) {

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+		if (i * SZ_256M >= adev->gmc.gart_size)
+			break;
+
+		placements[i].lpfn = (adev->gmc.gart_size - i * 

SZ_256M) >> PAGE_SHIFT;

Quoted 1 line

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

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+		placements[i].fpfn = ALIGN_DOWN(placements[i].lpfn - 1, 

SZ_256M >>

Quoted 42 lines
Quoted 42 lines

PAGE_SHIFT); + placements[i].mem_type = TTM_PL_TT;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -1
+		placements[i].flags = bo->resource->placement;
+	}
+
+	return i;
+}
+
 
 /*
 
  * amdgpu_ttm_alloc_gart - Make sure buffer object is accessible either
  * through AGP or GART aperture.
@@ -973,7 +1007,7 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object
 *bo)> 
 	struct ttm_operation_ctx ctx = { false, false };
 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
 	struct ttm_placement placement;
 
-	struct ttm_place placements;
+	struct ttm_place placements[AMDGPU_BO_MAX_PLACEMENTS];
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +6 -5
 	struct ttm_resource *tmp;
 	uint64_t addr, flags;
 	int r;
@@ -987,11 +1021,21 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object
 *bo)> 
 	/* allocate GART space */
 	placement.num_placement = 1;
 
-	placement.placement = &placements;
-	placements.fpfn = 0;
-	placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
-	placements.mem_type = TTM_PL_TT;
-	placements.flags = bo->resource->placement;
+	placement.placement = &placements[0];
+	placements[0].fpfn = 0;
+	placements[0].lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
+	placements[0].mem_type = TTM_PL_TT;
+	placements[0].flags = bo->resource->placement;
+
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +8 -0
+	/*
+	 * UVD 4.x and older require that BOs don't cross 256M segments.
+	 * We need to respect that here. We can't move the BO later
+	 * because GTT->GTT moves are not implemented.
+	 */
+	if (bo->base.size < SZ_256M && adev->family <= AMDGPU_FAMILY_KV)
+		placement.num_placement =
+			amdgpu_ttm_fill_gart_256M_placements(bo, 

placements,

Quoted 1 line

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_alloc_gart(struct ttm_buffer_object · line 1039

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+							     

ARRAY_SIZE(placements));

Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
 	r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
 	if (unlikely(r))
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h index
2d72fa217274..e9de628c8d2d 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +0 -0
@@ -202,6 +202,9 @@ int amdgpu_ttm_clear_buffer(struct

amdgpu_ttm_buffer_entity *entity,>
u64 k_job_id);

Quoted 4 lines

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

Quoted 4 lines

struct amdgpu_ttm_buffer_entity *amdgpu_ttm_next_clear_entity(struct amdgpu_device *adev);>

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +2 -0
+u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
+					 struct ttm_place 

*placements,

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -0
+					 u32 max_placements);
 
 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t
 type);
maintainer
#6 lore raw

Re: [PATCH 2/5] drm/amdgpu: Use placements of 256M GART segments for SI/CIK

Quoted 12 lines · Timur Kristóf
Quoted 10 lines · Central European Summer Time Christian König
Quoted 8 lines · Timur Kristóf

UVD 4.x and older require that BOs don't cross 256M segments. We need to respect that in amdgpu_ttm_alloc_gart(). We can't move the BOs later because GTT->GTT moves are not implemented. We also can't force all BOs to VRAM because that becomes very problematic in low VRAM scenarios.

This fixes UVD CS BOs crossing 256M segments
when they are placed in the GART.

Quoted 8 lines
Quoted 4 lines

Clear NAK for that approach.

This is the general TTM interface function and shouldn't have any HW generation dependent code in it.

I don't see how else to solve this, since GTT->GTT moves are not implemented, so we can't move the BO to a suitable address later. We also can't move it to VRAM.

GTT to GTT moves should be relatively easy to implement.

We just need to wait for the BO to be idle, unbind, move and bind again.

Regards,
Christian.

Quoted 139 lines

Please suggest a better approach if you don't like this one.

Quoted 23 lines

Regards,
Christian.

Quoted 19 lines

---

 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 56 ++++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  3 ++
 2 files changed, 53 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c index
6c6ab4dd6ea9..a106c7e77e26 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -959,6 +959,40 @@ static int amdgpu_ttm_backend_bind(struct ttm_device
 *bdev,> 
 	return 0;
 
 }
 
Quoted 15 lines
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
+/**
+ * amdgpu_ttm_fill_gart_256M_placements() - Fill placements array with

256M GART segments + *
+ * @bo: TTM buffer objects whose placements should be filled
+ * @placements: Pointer to an array of placements
+ * @max_placements: Size of the placements array
+ *
+ * Fill the specified placements array with 256M GART segments,
+ * starting from the highest address in order to reduce the
+ * contention of the lowest segment.
+ *
+ * Returns the number of placements filled.
+ */

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
+u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
+					 struct ttm_place 

*placements,

Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +6 -0
+					 u32 max_placements)
+{
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+	u32 i;
+
+	/* Fill the placements array with 256M segments, starting from 

highest.

Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,

Quoted 5 lines

*/ + for (i = 0; i < max_placements; ++i) {

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+		if (i * SZ_256M >= adev->gmc.gart_size)
+			break;
+
+		placements[i].lpfn = (adev->gmc.gart_size - i * 

SZ_256M) >> PAGE_SHIFT;

Quoted 1 line

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

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+		placements[i].fpfn = ALIGN_DOWN(placements[i].lpfn - 1, 

SZ_256M >>

Quoted 42 lines
Quoted 42 lines

PAGE_SHIFT); + placements[i].mem_type = TTM_PL_TT;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -1
+		placements[i].flags = bo->resource->placement;
+	}
+
+	return i;
+}
+
 
 /*
 
  * amdgpu_ttm_alloc_gart - Make sure buffer object is accessible either
  * through AGP or GART aperture.
@@ -973,7 +1007,7 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object
 *bo)> 
 	struct ttm_operation_ctx ctx = { false, false };
 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
 	struct ttm_placement placement;
 
-	struct ttm_place placements;
+	struct ttm_place placements[AMDGPU_BO_MAX_PLACEMENTS];
 
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +6 -5
 	struct ttm_resource *tmp;
 	uint64_t addr, flags;
 	int r;
@@ -987,11 +1021,21 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object
 *bo)> 
 	/* allocate GART space */
 	placement.num_placement = 1;
 
-	placement.placement = &placements;
-	placements.fpfn = 0;
-	placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
-	placements.mem_type = TTM_PL_TT;
-	placements.flags = bo->resource->placement;
+	placement.placement = &placements[0];
+	placements[0].fpfn = 0;
+	placements[0].lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
+	placements[0].mem_type = TTM_PL_TT;
+	placements[0].flags = bo->resource->placement;
+
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +8 -0
+	/*
+	 * UVD 4.x and older require that BOs don't cross 256M segments.
+	 * We need to respect that here. We can't move the BO later
+	 * because GTT->GTT moves are not implemented.
+	 */
+	if (bo->base.size < SZ_256M && adev->family <= AMDGPU_FAMILY_KV)
+		placement.num_placement =
+			amdgpu_ttm_fill_gart_256M_placements(bo, 

placements,

Quoted 1 line

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_alloc_gart(struct ttm_buffer_object · line 1039

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+							     

ARRAY_SIZE(placements));

Quoted 13 lines
Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
 	r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
 	if (unlikely(r))
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h index
2d72fa217274..e9de628c8d2d 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +0 -0
@@ -202,6 +202,9 @@ int amdgpu_ttm_clear_buffer(struct

amdgpu_ttm_buffer_entity *entity,>
u64 k_job_id);

Quoted 4 lines

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

Quoted 4 lines

struct amdgpu_ttm_buffer_entity *amdgpu_ttm_next_clear_entity(struct amdgpu_device *adev);>

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +2 -0
+u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
+					 struct ttm_place 

*placements,

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -0
+					 u32 max_placements);
 
 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t
 type);
#7 lore raw

Re: [PATCH 2/5] drm/amdgpu: Use placements of 256M GART segments for SI/CIK

Quoted 20 lines · Central European Summer Time Christian König
Quoted 19 lines · Timur Kristóf

On Tuesday, May 19, 2026 10:54:10 AM Central European Summer Time Christian

Quoted 14 lines · König
Quoted 8 lines · Timur Kristóf

UVD 4.x and older require that BOs don't cross 256M segments. We need to respect that in amdgpu_ttm_alloc_gart(). We can't move the BOs later because GTT->GTT moves are not implemented. We also can't force all BOs to VRAM because that becomes very problematic in low VRAM scenarios.

This fixes UVD CS BOs crossing 256M segments
when they are placed in the GART.

Clear NAK for that approach.

This is the general TTM interface function and shouldn't have any HW generation dependent code in it.

Quoted 7 lines
Quoted 3 lines

I don't see how else to solve this, since GTT->GTT moves are not implemented, so we can't move the BO to a suitable address later. We also can't move it to VRAM.

GTT to GTT moves should be relatively easy to implement.

We just need to wait for the BO to be idle, unbind, move and bind again.

Implementing GTT->GTT moves sounds like a bigger task and cannot be backported as that would be a new feature not a bug fix. So I strongly prefer to solve this problem with the tools we already have available.

Also, I would prefer to not have to move the BO at all and give it a suitable address from the beginning, to avoid the overhead of the move.

As far as I understand you take issue with checking adev->family in amdgpu_ttm_alloc_gart(), right? So, how about one of these alternatives:

- add a bool argument so the caller can request 256M segments, then the caller can check the GPU generation
- add an optional argument so the caller can just pass in a placements array

Or, if you have a different suggestion, let me know.

Thanks,
Timur

Quoted 148 lines
Quoted 148 lines
Quoted 22 lines
Quoted 22 lines

---

 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 56 ++++++++++++++++++++++---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  3 ++
 2 files changed, 53 insertions(+), 6 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c index
6c6ab4dd6ea9..a106c7e77e26 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
@@ -959,6 +959,40 @@ static int amdgpu_ttm_backend_bind(struct

ttm_device
*bdev,>

return 0;

}

Quoted 15 lines
Quoted 13 lines

+/**
+ * amdgpu_ttm_fill_gart_256M_placements() - Fill placements array with 256M GART segments + *
+ * @bo: TTM buffer objects whose placements should be filled
+ * @placements: Pointer to an array of placements
+ * @max_placements: Size of the placements array
+ *
+ * Fill the specified placements array with 256M GART segments,
+ * starting from the highest address in order to reduce the
+ * contention of the lowest segment.
+ *
+ * Returns the number of placements filled.
+ */

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -0
+u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
+					 struct ttm_place

*placements,

Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +6 -0
+					 u32 max_placements)
+{
+	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+	u32 i;
+
+	/* Fill the placements array with 256M segments, starting from

highest.

Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,

Quoted 5 lines

*/ + for (i = 0; i < max_placements; ++i) {

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +4 -0
+		if (i * SZ_256M >= adev->gmc.gart_size)
+			break;
+
+		placements[i].lpfn = (adev->gmc.gart_size - i *

SZ_256M) >> PAGE_SHIFT;

Quoted 1 line

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

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -0
+		placements[i].fpfn = ALIGN_DOWN(placements[i].lpfn - 1,

SZ_256M >>

Quoted 45 lines
Quoted 45 lines

PAGE_SHIFT); + placements[i].mem_type = TTM_PL_TT;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -1
+		placements[i].flags = bo->resource->placement;
+	}
+
+	return i;
+}
+
 
 /*
 
  * amdgpu_ttm_alloc_gart - Make sure buffer object is accessible either
  * through AGP or GART aperture.
@@ -973,7 +1007,7 @@ int amdgpu_ttm_alloc_gart(struct ttm_buffer_object
 *bo)>
 
 	struct ttm_operation_ctx ctx = { false, false };
 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
 	struct ttm_placement placement;
 
-	struct ttm_place placements;
+	struct ttm_place placements[AMDGPU_BO_MAX_PLACEMENTS];
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
 	struct ttm_resource *tmp;
 	uint64_t addr, flags;
 	int r;
@@ -987,11 +1021,21 @@ int amdgpu_ttm_alloc_gart(struct

ttm_buffer_object
*bo)>

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +6 -5
 	/* allocate GART space */
 	placement.num_placement = 1;
 
-	placement.placement = &placements;
-	placements.fpfn = 0;
-	placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
-	placements.mem_type = TTM_PL_TT;
-	placements.flags = bo->resource->placement;
+	placement.placement = &placements[0];
+	placements[0].fpfn = 0;
+	placements[0].lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
+	placements[0].mem_type = TTM_PL_TT;
+	placements[0].flags = bo->resource->placement;
+
Quoted 8 lines

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

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +8 -0
+	/*
+	 * UVD 4.x and older require that BOs don't cross 256M segments.
+	 * We need to respect that here. We can't move the BO later
+	 * because GTT->GTT moves are not implemented.
+	 */
+	if (bo->base.size < SZ_256M && adev->family <= AMDGPU_FAMILY_KV)
+		placement.num_placement =
+			amdgpu_ttm_fill_gart_256M_placements(bo,

placements,

Quoted 1 line
Quoted 1 line

+

ARRAY_SIZE(placements));

Quoted 11 lines
Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -0
 	r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
 	if (unlikely(r))
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h index
2d72fa217274..e9de628c8d2d 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +0 -0
@@ -202,6 +202,9 @@ int amdgpu_ttm_clear_buffer(struct

amdgpu_ttm_buffer_entity *entity,>

Quoted 7 lines

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

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +2 -0
 			    u64 k_job_id);
 
 struct amdgpu_ttm_buffer_entity *amdgpu_ttm_next_clear_entity(struct
 amdgpu_device *adev);>
 
+u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
+					 struct ttm_place

*placements,

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -0
+					 u32 max_placements);
 
 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo);
 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo);
 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t
 type);
#8 lore raw

PATCH 3/5 drm/amdgpu/uvd: Place VCPU BO only in VRAM for UVD 4.x and older

In reply to Timur Kristóf

These UVD versions don't fully support GPUVM and are only validated to work when their VCPU BO is placed in VRAM.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +11 -6
index 3a3bc0d370fa..1e59ca924abe 100644
@@ -188,6 +188,7 @@ int amdgpu_uvd_sw_init(struct amdgpu_device *adev)
 	const struct common_firmware_header *hdr;
 	unsigned int family_id;
 	int i, j, r;
+	u32 vcpu_bo_domain;
 
 	INIT_DELAYED_WORK(&adev->uvd.idle_work, amdgpu_uvd_idle_work_handler);
 
@@ -319,12 +320,20 @@ int amdgpu_uvd_sw_init(struct amdgpu_device *adev)
 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
 		bo_size += AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8);
 
+	/* UVD 5.0 and newer HW can use 64 bit addressing. */
+	adev->uvd.address_64_bit =
+		!amdgpu_device_ip_block_version_cmp(adev, AMD_IP_BLOCK_TYPE_UVD, 5, 0);
+
+	vcpu_bo_domain = AMDGPU_GEM_DOMAIN_VRAM;
+	if (adev->uvd.address_64_bit)
+		vcpu_bo_domain |= AMDGPU_GEM_DOMAIN_GTT;
+
 	for (j = 0; j < adev->uvd.num_uvd_inst; j++) {
 		if (adev->uvd.harvest_config & (1 << j))
 			continue;
+
 		r = amdgpu_bo_create_kernel(adev, bo_size, PAGE_SIZE,
-					    AMDGPU_GEM_DOMAIN_VRAM |
-					    AMDGPU_GEM_DOMAIN_GTT,
+					    vcpu_bo_domain,
 					    &adev->uvd.inst[j].vcpu_bo,
 					    &adev->uvd.inst[j].gpu_addr,
 					    &adev->uvd.inst[j].cpu_addr);
@@ -339,10 +348,6 @@ int amdgpu_uvd_sw_init(struct amdgpu_device *adev)
 		adev->uvd.filp[i] = NULL;
 	}
 
-	/* from uvd v5.0 HW addressing capacity increased to 64 bits */
-	if (!amdgpu_device_ip_block_version_cmp(adev, AMD_IP_BLOCK_TYPE_UVD, 5, 0))
-		adev->uvd.address_64_bit = true;
-
 	r = amdgpu_uvd_create_msg_bo_helper(adev, 128 << 10, &adev->uvd.ib_bo);
 	if (r)
 		return r;
Signature · 1 line
2.54.0
maintainer Reviewed-by on v1 3/5
#9 lore raw

Re: [PATCH 3/5] drm/amdgpu/uvd: Place VCPU BO only in VRAM for UVD 4.x and older

Quoted 4 lines · Timur Kristóf

These UVD versions don't fully support GPUVM and are only validated to work when their VCPU BO is placed in VRAM.

Quoted 50 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +11 -6
index 3a3bc0d370fa..1e59ca924abe 100644
@@ -188,6 +188,7 @@ int amdgpu_uvd_sw_init(struct amdgpu_device *adev)
 	const struct common_firmware_header *hdr;
 	unsigned int family_id;
 	int i, j, r;
+	u32 vcpu_bo_domain;
 
 	INIT_DELAYED_WORK(&adev->uvd.idle_work, amdgpu_uvd_idle_work_handler);
 
@@ -319,12 +320,20 @@ int amdgpu_uvd_sw_init(struct amdgpu_device *adev)
 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
 		bo_size += AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8);
 
+	/* UVD 5.0 and newer HW can use 64 bit addressing. */
+	adev->uvd.address_64_bit =
+		!amdgpu_device_ip_block_version_cmp(adev, AMD_IP_BLOCK_TYPE_UVD, 5, 0);
+
+	vcpu_bo_domain = AMDGPU_GEM_DOMAIN_VRAM;
+	if (adev->uvd.address_64_bit)
+		vcpu_bo_domain |= AMDGPU_GEM_DOMAIN_GTT;
+
 	for (j = 0; j < adev->uvd.num_uvd_inst; j++) {
 		if (adev->uvd.harvest_config & (1 << j))
 			continue;
+
 		r = amdgpu_bo_create_kernel(adev, bo_size, PAGE_SIZE,
-					    AMDGPU_GEM_DOMAIN_VRAM |
-					    AMDGPU_GEM_DOMAIN_GTT,
+					    vcpu_bo_domain,
 					    &adev->uvd.inst[j].vcpu_bo,
 					    &adev->uvd.inst[j].gpu_addr,
 					    &adev->uvd.inst[j].cpu_addr);
@@ -339,10 +348,6 @@ int amdgpu_uvd_sw_init(struct amdgpu_device *adev)
 		adev->uvd.filp[i] = NULL;
 	}
 
-	/* from uvd v5.0 HW addressing capacity increased to 64 bits */
-	if (!amdgpu_device_ip_block_version_cmp(adev, AMD_IP_BLOCK_TYPE_UVD, 5, 0))
-		adev->uvd.address_64_bit = true;
-
 	r = amdgpu_uvd_create_msg_bo_helper(adev, 128 << 10, &adev->uvd.ib_bo);
 	if (r)
 		return r;
#10 lore raw

PATCH 4/5 drm/amdgpu/uvd: Fix forcing BOs into UVD segment when it isn't at 0

In reply to Timur Kristóf

UVD 4.x and older can only access FB and MSG buffers from a specific 256M VRAM segment that the VCPU BO is also located in. We already modify all placements of the given BO to ensure the BO is placed within this segment.

Previously, amdgpu_uvd_force_into_uvd_segment() always assumed that the UVD segment is the first 256M of VRAM, even though under some conditions the VCPU BO could be allocated outside this segment, which made UVD non-functional as the BOs were not inside the same segment as the UVD VCPU BO.

Solve that by using the segment where the VCPU BO actually is.

This fixes an issue with UVD failing to initialize on SI/CIK when resizable BAR is enabled and the VCPU BO is allocated in a different segment.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 36 +++++++++++++++----------
 1 file changed, 22 insertions(+), 14 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +22 -14
index 1e59ca924abe..993957927782 100644
@@ -550,16 +550,29 @@ void amdgpu_uvd_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
 	}
 }
 
+/**
+ * amdgpu_uvd_force_into_uvd_segment() - Forces placement of a BO into the UVD segment
+ *
+ * @abo: buffer object whose placement is forced
+ *
+ * UVD 4.x and older can only access FB and MSG buffers from a specific 256M VRAM segment
+ * that the VCPU BO is also located in. Force the BO into that segment.
+ */
 static void amdgpu_uvd_force_into_uvd_segment(struct amdgpu_bo *abo)
 {
-	int i;
+	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+	struct amdgpu_bo *vcpu_bo = adev->uvd.inst[0].vcpu_bo;
+	struct amdgpu_res_cursor vcpu_cur;
 
-	for (i = 0; i < abo->placement.num_placement; ++i) {
-		abo->placements[i].fpfn = 0 >> PAGE_SHIFT;
-		abo->placements[i].lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
-		if (abo->placements[i].mem_type == TTM_PL_VRAM)
-			abo->placements[i].flags |= TTM_PL_FLAG_CONTIGUOUS;
-	}
+	amdgpu_res_first(vcpu_bo->tbo.resource, 0, amdgpu_bo_size(vcpu_bo), &vcpu_cur);
+
+	abo->placement.num_placement = 1;
+	abo->placements[0].fpfn = ALIGN_DOWN(vcpu_cur.start, SZ_256M) >> PAGE_SHIFT;
+	abo->placements[0].lpfn = abo->placements[0].fpfn + (SZ_256M >> PAGE_SHIFT);
+	abo->placements[0].mem_type = adev->uvd.inst[0].vcpu_bo->tbo.resource->mem_type;
+
+	if (abo->placements[0].mem_type == TTM_PL_VRAM)
+		abo->placements[0].flags |= TTM_PL_FLAG_CONTIGUOUS;
 }
 
 static u64 amdgpu_uvd_get_addr_from_ctx(struct amdgpu_uvd_cs_ctx *ctx)
@@ -600,13 +613,8 @@ static int amdgpu_uvd_cs_pass1(struct amdgpu_uvd_cs_ctx *ctx)
 	if (!ctx->parser->adev->uvd.address_64_bit) {
 		/* check if it's a message or feedback command */
 		cmd = amdgpu_ib_get_value(ctx->ib, ctx->idx) >> 1;
-		if (cmd == 0x0 || cmd == 0x3) {
-			/* yes, force it into VRAM */
-			uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
-
-			amdgpu_bo_placement_from_domain(bo, domain);
-		}
-		amdgpu_uvd_force_into_uvd_segment(bo);
+		if (cmd == 0x0 || cmd == 0x3)
+			amdgpu_uvd_force_into_uvd_segment(bo);
 
 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &tctx);
 	}
Signature · 1 line
2.54.0
maintainer
#11 lore raw

Re: [PATCH 4/5] drm/amdgpu/uvd: Fix forcing BOs into UVD segment when it isn't at 0

Quoted 48 lines · Timur Kristóf

UVD 4.x and older can only access FB and MSG buffers from a specific 256M VRAM segment that the VCPU BO is also located in. We already modify all placements of the given BO to ensure the BO is placed within this segment.

Previously, amdgpu_uvd_force_into_uvd_segment() always assumed that the UVD segment is the first 256M of VRAM, even though under some conditions the VCPU BO could be allocated outside this segment, which made UVD non-functional as the BOs were not inside the same segment as the UVD VCPU BO.

Solve that by using the segment where the VCPU BO actually is.

This fixes an issue with UVD failing to initialize on SI/CIK when resizable BAR is enabled and the VCPU BO is allocated in a different segment.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 36 +++++++++++++++----------
 1 file changed, 22 insertions(+), 14 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +11 -3
index 1e59ca924abe..993957927782 100644
@@ -550,16 +550,29 @@ void amdgpu_uvd_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
 	}
 }
 
+/**
+ * amdgpu_uvd_force_into_uvd_segment() - Forces placement of a BO into the UVD segment
+ *
+ * @abo: buffer object whose placement is forced
+ *
+ * UVD 4.x and older can only access FB and MSG buffers from a specific 256M VRAM segment
+ * that the VCPU BO is also located in. Force the BO into that segment.
+ */
 static void amdgpu_uvd_force_into_uvd_segment(struct amdgpu_bo *abo)
 {
-	int i;
+	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+	struct amdgpu_bo *vcpu_bo = adev->uvd.inst[0].vcpu_bo;
+	struct amdgpu_res_cursor vcpu_cur;
 
-	for (i = 0; i < abo->placement.num_placement; ++i) {
-		abo->placements[i].fpfn = 0 >> PAGE_SHIFT;

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c void amdgpu_uvd_free_handles(struct amdgpu_device *adev, struct drm_file *filp) · line 572

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +6 -4
-		abo->placements[i].lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
-		if (abo->placements[i].mem_type == TTM_PL_VRAM)
-			abo->placements[i].flags |= TTM_PL_FLAG_CONTIGUOUS;
-	}
+	amdgpu_res_first(vcpu_bo->tbo.resource, 0, amdgpu_bo_size(vcpu_bo), &vcpu_cur);
+
+	abo->placement.num_placement = 1;
+	abo->placements[0].fpfn = ALIGN_DOWN(vcpu_cur.start, SZ_256M) >> PAGE_SHIFT;
+	abo->placements[0].lpfn = abo->placements[0].fpfn + (SZ_256M >> PAGE_SHIFT);
+	abo->placements[0].mem_type = adev->uvd.inst[0].vcpu_bo->tbo.resource->mem_type;

This should clearly be applied to all placements, it's just that VRAM should use the vcpu segment and GTT the first one.

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +3 -0
+
+	if (abo->placements[0].mem_type == TTM_PL_VRAM)
+		abo->placements[0].flags |= TTM_PL_FLAG_CONTIGUOUS;
 }
 
 static u64 amdgpu_uvd_get_addr_from_ctx(struct amdgpu_uvd_cs_ctx *ctx)
@@ -600,13 +613,8 @@ static int amdgpu_uvd_cs_pass1(struct amdgpu_uvd_cs_ctx *ctx)
 	if (!ctx->parser->adev->uvd.address_64_bit) {
 		/* check if it's a message or feedback command */

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c static int amdgpu_uvd_cs_pass1(struct amdgpu_uvd_cs_ctx *ctx) · line 617

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +2 -7
 		cmd = amdgpu_ib_get_value(ctx->ib, ctx->idx) >> 1;
-		if (cmd == 0x0 || cmd == 0x3) {
-			/* yes, force it into VRAM */
-			uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
-
-			amdgpu_bo_placement_from_domain(bo, domain);
-		}
-		amdgpu_uvd_force_into_uvd_segment(bo);
+		if (cmd == 0x0 || cmd == 0x3)
+			amdgpu_uvd_force_into_uvd_segment(bo);

The existing code was already correct. We just messed up the GTT handling by not having the correct check in the manager and not supporting GTT->GTT moves.

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +0 -0
 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &tctx);
 	}
#12 lore raw

Re: [PATCH 4/5] drm/amdgpu/uvd: Fix forcing BOs into UVD segment when it isn't at 0

Quoted 48 lines · Central European Summer Time Christian König
Quoted 47 lines · Timur Kristóf

UVD 4.x and older can only access FB and MSG buffers from a specific 256M VRAM segment that the VCPU BO is also located in. We already modify all placements of the given BO to ensure the BO is placed within this segment.

Previously, amdgpu_uvd_force_into_uvd_segment() always assumed that the UVD segment is the first 256M of VRAM, even though under some conditions the VCPU BO could be allocated outside this segment, which made UVD non-functional as the BOs were not inside the same segment as the UVD VCPU BO.

Solve that by using the segment where the VCPU BO actually is.

This fixes an issue with UVD failing to initialize on SI/CIK when resizable BAR is enabled and the VCPU BO is allocated in a different segment.

---

 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 36 +++++++++++++++----------
 1 file changed, 22 insertions(+), 14 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c index
1e59ca924abe..993957927782 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +2 -0
@@ -550,16 +550,29 @@ void amdgpu_uvd_free_handles(struct amdgpu_device
 *adev, struct drm_file *filp)> 
 	}
 
 }
 
+/**
+ * amdgpu_uvd_force_into_uvd_segment() - Forces placement of a BO into

the UVD segment + *
+ * @abo: buffer object whose placement is forced
+ *
+ * UVD 4.x and older can only access FB and MSG buffers from a specific 256M VRAM segment + * that the VCPU BO is also located in. Force the BO into that segment. + */

static void amdgpu_uvd_force_into_uvd_segment(struct amdgpu_bo *abo) {

Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c void amdgpu_uvd_free_handles(struct amdgpu_device *adev, struct drm_file *filp)

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +3 -4
-	int i;
+	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
+	struct amdgpu_bo *vcpu_bo = adev->uvd.inst[0].vcpu_bo;
+	struct amdgpu_res_cursor vcpu_cur;
 
-	for (i = 0; i < abo->placement.num_placement; ++i) {
-		abo->placements[i].fpfn = 0 >> PAGE_SHIFT;
-		abo->placements[i].lpfn = (256 * 1024 * 1024) >> 

PAGE_SHIFT;

Quoted 2 lines

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

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +0 -2
-		if (abo->placements[i].mem_type == TTM_PL_VRAM)
-			abo->placements[i].flags |= 

TTM_PL_FLAG_CONTIGUOUS;

Quoted 10 lines
Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +1 -1
-	}
+	amdgpu_res_first(vcpu_bo->tbo.resource, 0, amdgpu_bo_size(vcpu_bo),

&vcpu_cur); +

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +2 -0
+	abo->placement.num_placement = 1;
+	abo->placements[0].fpfn = ALIGN_DOWN(vcpu_cur.start, SZ_256M) >>

PAGE_SHIFT; + abo->placements[0].lpfn = abo->placements[0].fpfn +
(SZ_256M >> PAGE_SHIFT); + abo->placements[0].mem_type =
adev->uvd.inst[0].vcpu_bo->tbo.resource->mem_type;

This should clearly be applied to all placements, it's just that VRAM should use the vcpu segment and GTT the first one.

Previously, the function was trying to do two things at once:

- Forcing some BOs into the UVD segment, as the name of the function suggests, but it was buggy because it assumed the UVD segment is always segment 0
- Forcing GTT placements for other BOs, which is not what its name suggests, and was actually wrong and not working.

With this patch, the function is now only responsible for forcing MSG/FB BOs into the UVD segment, and is no longer responsible for doing anything with the other BOs, which was incorrect anyway.

This is also beneficial because it reduces contention for the UVD segment for the BOs that don't actually need to be located in that segment.

For non-MSG/FB BOs, this function should no longer be called. When those BOs are located in GTT, patch 2 should already make sure they are placed correctly. When they are in VRAM, patch 5 will make sure to correct their placement if it was wrong.

Quoted 15 lines
Quoted 15 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +3 -0
+
+	if (abo->placements[0].mem_type == TTM_PL_VRAM)
+		abo->placements[0].flags |= TTM_PL_FLAG_CONTIGUOUS;
 
 }
 
 static u64 amdgpu_uvd_get_addr_from_ctx(struct amdgpu_uvd_cs_ctx *ctx)
@@ -600,13 +613,8 @@ static int amdgpu_uvd_cs_pass1(struct

amdgpu_uvd_cs_ctx *ctx)>

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +0 -0
 	if (!ctx->parser->adev->uvd.address_64_bit) {
 
 		/* check if it's a message or feedback command */
 		cmd = amdgpu_ib_get_value(ctx->ib, ctx->idx) >> 1;
Quoted 13 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c static int amdgpu_uvd_cs_pass1(struct amdgpu_uvd_cs_ctx *ctx) · line 617

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +2 -7
-		if (cmd == 0x0 || cmd == 0x3) {
-			/* yes, force it into VRAM */
-			uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
-
-			amdgpu_bo_placement_from_domain(bo, domain);
-		}
-		amdgpu_uvd_force_into_uvd_segment(bo);
+		if (cmd == 0x0 || cmd == 0x3)
+			amdgpu_uvd_force_into_uvd_segment(bo);

The existing code was already correct. We just messed up the GTT handling by not having the correct check in the manager and not supporting GTT->GTT moves.

See above for an explanation of why I changed this part.

Quoted 7 lines

Regards,
Christian.

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +0 -0
 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &tctx);
 
 	}
#13 lore raw

PATCH 5/5 drm/amdgpu/uvd: Move BOs to GTT when we can't place them in VRAM correctly

In reply to Timur Kristóf

When VRAM is nearly full, the Buddy allocator makes tradeoffs and it may place BOs in a way that they cross 256M segments.

Move the BO to GTT when this eventuality is detected.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  3 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 21 +++++++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +2 -1
index a106c7e77e26..fb49bd53bd00 100644
@@ -977,6 +977,7 @@ u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
 					 u32 max_placements)
 {
 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
+	const u64 sz = adev->gmc.gart_size;
 	u32 i;
 
 	/* Fill the placements array with 256M segments, starting from highest. */
@@ -984,7 +985,7 @@ u32 amdgpu_ttm_fill_gart_256M_placements(struct ttm_buffer_object *bo,
 		if (i * SZ_256M >= adev->gmc.gart_size)
 			break;
 
-		placements[i].lpfn = (adev->gmc.gart_size - i * SZ_256M) >> PAGE_SHIFT;
+		placements[i].lpfn = MIN(ALIGN(sz, SZ_256M) - i * SZ_256M, sz) >> PAGE_SHIFT;
 		placements[i].fpfn = ALIGN_DOWN(placements[i].lpfn - 1, SZ_256M >> PAGE_SHIFT);
 		placements[i].mem_type = TTM_PL_TT;
 		placements[i].flags = bo->resource->placement;
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +21 -0
index 993957927782..53f810c2a5fb 100644
@@ -617,6 +617,27 @@ static int amdgpu_uvd_cs_pass1(struct amdgpu_uvd_cs_ctx *ctx)
 			amdgpu_uvd_force_into_uvd_segment(bo);
 
 		r = ttm_bo_validate(&bo->tbo, &bo->placement, &tctx);
+		if (r)
+			return r;
+
+		/* Check if the BO placement crosses a 256M segment. */
+		if ((amdgpu_bo_gpu_offset(bo) >> 28) !=
+		    ((amdgpu_bo_gpu_offset(bo) + amdgpu_bo_size(bo)) >> 28)) {
+			/* There is not enough memory for correct placement of FB/MSG BOs. */
+			if (cmd == 0x0 || cmd == 0x3)
+				return -ENOMEM;
+
+			/* GTT->GTT moves are not implemented yet. */
+			if (bo->tbo.resource->mem_type != TTM_PL_VRAM)
+				return -ENOMEM;
+
+			/* Try to move the BO from VRAM to GART into a 256M segment. */
+			amdgpu_ttm_fill_gart_256M_placements(&bo->tbo,
+							     bo->placements,
+							     ARRAY_SIZE(bo->placements));
+
+			r = ttm_bo_validate(&bo->tbo, &bo->placement, &tctx);
+		}
 	}
 
 	return r;
Signature · 1 line
2.54.0