Skip to contentSkip to conversation

VCE1 fixes

PATCH In review Timur Kristóf 2 participants 16 messages Apr 20

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

cover VCE1 fixes

Fix various small issues regarding VCE1
and the workaround to ensure the VCPU BO
has a low 32-bit address.

Timur Kristóf (7):
drm/amdgpu: Add alignment to amdgpu_gtt_mgr_alloc_entries() drm/amdgpu/vce: Align VCPU BO to nearest power of two drm/amdgpu/vce1: Correct firmware offset mask drm/amdgpu/vce1: Remove superfluous address check drm/amdgpu/vce1: Check if VRAM address is lower than GART. drm/amdgpu/vce1: Don't repeat GTT MGR node allocation drm/amdgpu/vce1: Align VCPU BO GART address to nearest power of two

 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c |  5 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c     |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c     |  3 +-
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c       | 32 ++++++++++++++-------
 5 files changed, 28 insertions(+), 16 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH 1/7 drm/amdgpu: Add alignment to amdgpu_gtt_mgr_alloc_entries()

Add an argument to amdgpu_gtt_mgr_alloc_entries() so that the caller can specify an alignment.

This is a pre-requisite for fixing the workaround for ensuring that the VCE1 VCPU BO has a low 32-bit address.

Fixes 66a80158aa2a ("amdgpu/vce: use amdgpu_gtt_mgr_alloc_entries")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 5 +++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c     | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     | 2 +-
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c       | 2 +-
 4 files changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +3 -2
index 9b0bcf6aca445..4fea81479264f 100644
@@ -188,6 +188,7 @@ static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
  * @mgr: The GTT manager object
  * @mm_node: The drm mm node to return the new allocation node information
  * @num_pages: The number of pages for the new allocation
+ * @alignment: Alignment of the allocation (in pages)
  * @mode: The new allocation mode
  *
  * Helper to dynamic alloc GART entries to map memory not accociated with
@@ -195,7 +196,7 @@ static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
  */
 int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
 				 struct drm_mm_node *mm_node,
-				 u64 num_pages,
+				 u64 num_pages, u64 alignment,
 				 enum drm_mm_insert_mode mode)
 {
 	struct amdgpu_device *adev = container_of(mgr, typeof(*adev), mman.gtt_mgr);
@@ -203,7 +204,7 @@ int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
 
 	spin_lock(&mgr->lock);
 	r = drm_mm_insert_node_in_range(&mgr->mm, mm_node, num_pages,
-					0, GART_ENTRY_WITHOUT_BO_COLOR, 0,
+					alignment, GART_ENTRY_WITHOUT_BO_COLOR, 0,
 					adev->gmc.gart_size >> PAGE_SHIFT,
 					mode);
 	spin_unlock(&mgr->lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -1
index 4c7d1917d9bb1..b7b6c4469937b 100644
@@ -2026,7 +2026,7 @@ static int amdgpu_ttm_buffer_entity_init(struct amdgpu_gtt_mgr *mgr,
 		return 0;
 
 	num_pages = num_gart_windows * AMDGPU_GTT_MAX_TRANSFER_SIZE;
-	r = amdgpu_gtt_mgr_alloc_entries(mgr, &entity->gart_node, num_pages,
+	r = amdgpu_gtt_mgr_alloc_entries(mgr, &entity->gart_node, num_pages, 0,
 					 DRM_MM_INSERT_BEST);
 	if (r) {
 		drm_sched_entity_destroy(&entity->base);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -1
index 2d72fa2172745..09a524be64c3d 100644
@@ -148,7 +148,7 @@ void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
 
 int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
 				 struct drm_mm_node *mm_node,
-				 u64 num_pages,
+				 u64 num_pages, u64 alignment,
 				 enum drm_mm_insert_mode mode);
 void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr,
 				 struct drm_mm_node *mm_node);
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +1 -1
index 5b7b46d242c6d..2fe931366985a 100644
@@ -539,7 +539,7 @@ static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 	int r;
 
 	r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr,
-					 &adev->vce.gart_node, num_pages,
+					 &adev->vce.gart_node, num_pages, 0,
 					 DRM_MM_INSERT_LOW);
 	if (r)
 		return r;
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] drm/amdgpu: Add alignment to amdgpu_gtt_mgr_alloc_entries()

Quoted 8 lines · Timur Kristóf

Add an argument to amdgpu_gtt_mgr_alloc_entries() so that the caller can specify an alignment.

This is a pre-requisite for fixing the workaround for ensuring that the VCE1 VCPU BO has a low 32-bit address.

Fixes 66a80158aa2a ("amdgpu/vce: use amdgpu_gtt_mgr_alloc_entries")
Quoted 76 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c | 5 +++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c     | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h     | 2 +-
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c       | 2 +-
 4 files changed, 6 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gtt_mgr.c +3 -2
index 9b0bcf6aca445..4fea81479264f 100644
@@ -188,6 +188,7 @@ static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
  * @mgr: The GTT manager object
  * @mm_node: The drm mm node to return the new allocation node information
  * @num_pages: The number of pages for the new allocation
+ * @alignment: Alignment of the allocation (in pages)
  * @mode: The new allocation mode
  *
  * Helper to dynamic alloc GART entries to map memory not accociated with
@@ -195,7 +196,7 @@ static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man,
  */
 int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
 				 struct drm_mm_node *mm_node,
-				 u64 num_pages,
+				 u64 num_pages, u64 alignment,
 				 enum drm_mm_insert_mode mode)
 {
 	struct amdgpu_device *adev = container_of(mgr, typeof(*adev), mman.gtt_mgr);
@@ -203,7 +204,7 @@ int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
 
 	spin_lock(&mgr->lock);
 	r = drm_mm_insert_node_in_range(&mgr->mm, mm_node, num_pages,
-					0, GART_ENTRY_WITHOUT_BO_COLOR, 0,
+					alignment, GART_ENTRY_WITHOUT_BO_COLOR, 0,
 					adev->gmc.gart_size >> PAGE_SHIFT,
 					mode);
 	spin_unlock(&mgr->lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +1 -1
index 4c7d1917d9bb1..b7b6c4469937b 100644
@@ -2026,7 +2026,7 @@ static int amdgpu_ttm_buffer_entity_init(struct amdgpu_gtt_mgr *mgr,
 		return 0;
 
 	num_pages = num_gart_windows * AMDGPU_GTT_MAX_TRANSFER_SIZE;
-	r = amdgpu_gtt_mgr_alloc_entries(mgr, &entity->gart_node, num_pages,
+	r = amdgpu_gtt_mgr_alloc_entries(mgr, &entity->gart_node, num_pages, 0,
 					 DRM_MM_INSERT_BEST);
 	if (r) {
 		drm_sched_entity_destroy(&entity->base);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +1 -1
index 2d72fa2172745..09a524be64c3d 100644
@@ -148,7 +148,7 @@ void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr);
 
 int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr,
 				 struct drm_mm_node *mm_node,
-				 u64 num_pages,
+				 u64 num_pages, u64 alignment,
 				 enum drm_mm_insert_mode mode);
 void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr,
 				 struct drm_mm_node *mm_node);
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +1 -1
index 5b7b46d242c6d..2fe931366985a 100644
@@ -539,7 +539,7 @@ static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 	int r;
 
 	r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr,
-					 &adev->vce.gart_node, num_pages,
+					 &adev->vce.gart_node, num_pages, 0,
 					 DRM_MM_INSERT_LOW);
 	if (r)
 		return r;
#3 lore raw

PATCH 2/7 drm/amdgpu/vce: Align VCPU BO to nearest power of two

In reply to Timur Kristóf

VCE accesses memory, including its firmware, through a BAR. It works slightly differently on each generation.
In case of VCE1, the start address of this BAR is zero and we can't change it due to the firmware validation mechanism.

Align the VCE VCPU BO to the nearest power of two after the byte size of the BO in order to prevent it from crossing the boundaries of its BAR.

Fixes d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +2 -1
index efdebd9c0a1f3..0658f13f0bdf7 100644
@@ -218,7 +218,8 @@ int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size)
 	if (!adev->vce.fw)
 		return -ENOENT;
 
-	r = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
+	r = amdgpu_bo_create_kernel(adev, size,
+				    roundup_pow_of_two(size),
 				    AMDGPU_GEM_DOMAIN_VRAM |
 				    AMDGPU_GEM_DOMAIN_GTT,
 				    &adev->vce.vcpu_bo,
Signature · 1 line
2.53.0
maintainer
#4 lore raw

Re: [PATCH 2/7] drm/amdgpu/vce: Align VCPU BO to nearest power of two

Quoted 20 lines · Timur Kristóf

VCE accesses memory, including its firmware, through a BAR. It works slightly differently on each generation.
In case of VCE1, the start address of this BAR is zero and we can't change it due to the firmware validation mechanism.

Align the VCE VCPU BO to the nearest power of two after the byte size of the BO in order to prevent it from crossing the boundaries of its BAR.

Fixes d38ceaf99ed0 ("drm/amdgpu: add core driver (v4)")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +0 -0
index efdebd9c0a1f3..0658f13f0bdf7 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size) · line 224

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +2 -1
@@ -218,7 +218,8 @@ int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size)
 	if (!adev->vce.fw)
 		return -ENOENT;
 
-	r = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
+	r = amdgpu_bo_create_kernel(adev, size,
+				    roundup_pow_of_two(size),
 				    AMDGPU_GEM_DOMAIN_VRAM |
 				    AMDGPU_GEM_DOMAIN_GTT,

I don't think that this is a good idea. The VRAM BO should never cross a BAR boundary.

What could be is that the GTT BO does that, but approach that is only available on VCE4 anyway.

I think we should rather restrict falling back to GTT to VCE4 only.

Regards,
Christian.

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +0 -0
 				    &adev->vce.vcpu_bo,
#5 lore raw

PATCH 3/7 drm/amdgpu/vce1: Correct firmware offset mask

In reply to Timur Kristóf

It's 0x0fffffff and not 0x7fffffff.

Fixes d4a640d4b9f3 ("drm/amdgpu/vce1: Implement VCE1 IP block (v2)")
---
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +4 -4
index 2fe931366985a..ce993b57b0e9f 100644
@@ -313,17 +313,17 @@ static int vce_v1_0_mc_resume(struct amdgpu_device *adev)
 
 	offset =  adev->vce.gpu_addr + AMDGPU_VCE_FIRMWARE_OFFSET;
 	size = VCE_V1_0_FW_SIZE;
-	WREG32(mmVCE_VCPU_CACHE_OFFSET0, offset & 0x7fffffff);
+	WREG32(mmVCE_VCPU_CACHE_OFFSET0, offset & 0x0fffffff);
 	WREG32(mmVCE_VCPU_CACHE_SIZE0, size);
 
 	offset += size;
 	size = VCE_V1_0_STACK_SIZE;
-	WREG32(mmVCE_VCPU_CACHE_OFFSET1, offset & 0x7fffffff);
+	WREG32(mmVCE_VCPU_CACHE_OFFSET1, offset & 0x0fffffff);
 	WREG32(mmVCE_VCPU_CACHE_SIZE1, size);
 
 	offset += size;
 	size = VCE_V1_0_DATA_SIZE;
-	WREG32(mmVCE_VCPU_CACHE_OFFSET2, offset & 0x7fffffff);
+	WREG32(mmVCE_VCPU_CACHE_OFFSET2, offset & 0x0fffffff);
 	WREG32(mmVCE_VCPU_CACHE_SIZE2, size);
 
 	WREG32_P(mmVCE_LMI_CTRL2, 0x0, ~0x100);
@@ -531,7 +531,7 @@ static int vce_v1_0_early_init(struct amdgpu_ip_block *ip_block)
 static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 {
 	u64 bo_size = amdgpu_bo_size(adev->vce.vcpu_bo);
-	u64 max_vcpu_bo_addr = 0xffffffff - bo_size;
+	u64 max_vcpu_bo_addr = 0x0fffffff - bo_size;
 	u64 num_pages = ALIGN(bo_size, AMDGPU_GPU_PAGE_SIZE) / AMDGPU_GPU_PAGE_SIZE;
 	u64 pa = amdgpu_gmc_vram_pa(adev, adev->vce.vcpu_bo);
 	u64 flags = AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE | AMDGPU_PTE_VALID;
Signature · 1 line
2.53.0
maintainer
#6 lore raw

Re: [PATCH 3/7] drm/amdgpu/vce1: Correct firmware offset mask

Quoted 20 lines · Timur Kristóf

It's 0x0fffffff and not 0x7fffffff.

Fixes d4a640d4b9f3 ("drm/amdgpu/vce1: Implement VCE1 IP block (v2)")
---
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +1 -1
index 2fe931366985a..ce993b57b0e9f 100644
@@ -313,17 +313,17 @@ static int vce_v1_0_mc_resume(struct amdgpu_device *adev)
 
 	offset =  adev->vce.gpu_addr + AMDGPU_VCE_FIRMWARE_OFFSET;
 	size = VCE_V1_0_FW_SIZE;
-	WREG32(mmVCE_VCPU_CACHE_OFFSET0, offset & 0x7fffffff);
+	WREG32(mmVCE_VCPU_CACHE_OFFSET0, offset & 0x0fffffff);
 	WREG32(mmVCE_VCPU_CACHE_SIZE0, size);
 

drivers/gpu/drm/amd/amdgpu/vce_v1_0.c static int vce_v1_0_mc_resume(struct amdgpu_device *adev) · line 326

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +2 -2
 	offset += size;
 	size = VCE_V1_0_STACK_SIZE;
-	WREG32(mmVCE_VCPU_CACHE_OFFSET1, offset & 0x7fffffff);
+	WREG32(mmVCE_VCPU_CACHE_OFFSET1, offset & 0x0fffffff);
 	WREG32(mmVCE_VCPU_CACHE_SIZE1, size);
 
 	offset += size;
 	size = VCE_V1_0_DATA_SIZE;
-	WREG32(mmVCE_VCPU_CACHE_OFFSET2, offset & 0x7fffffff);
+	WREG32(mmVCE_VCPU_CACHE_OFFSET2, offset & 0x0fffffff);

We actually have that as VCE_VCPU_CACHE_OFFSET*__OFFSET_MASK in the headers, would probably be a good idea to use that one instead.

Additional to that limiting the value actually doesn't make much sense, that just hides the problem when we really get an offset which is to large.

We should probably rather have a WARN_ON(offset & ~VCE_VCPU_CACHE_OFFSET2__OFFSET_MASK) directly above the register write.

Apart from that the patch looks good to me.

Regards,
Christian.

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +1 -1
 	WREG32(mmVCE_VCPU_CACHE_SIZE2, size);
 
 	WREG32_P(mmVCE_LMI_CTRL2, 0x0, ~0x100);
@@ -531,7 +531,7 @@ static int vce_v1_0_early_init(struct amdgpu_ip_block *ip_block)
 static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 {
 	u64 bo_size = amdgpu_bo_size(adev->vce.vcpu_bo);
-	u64 max_vcpu_bo_addr = 0xffffffff - bo_size;
+	u64 max_vcpu_bo_addr = 0x0fffffff - bo_size;
 	u64 num_pages = ALIGN(bo_size, AMDGPU_GPU_PAGE_SIZE) / AMDGPU_GPU_PAGE_SIZE;
 	u64 pa = amdgpu_gmc_vram_pa(adev, adev->vce.vcpu_bo);
 	u64 flags = AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE | AMDGPU_PTE_VALID;
#7 lore raw

Re: [PATCH 3/7] drm/amdgpu/vce1: Correct firmware offset mask

Quoted 45 lines · Central European Summer Time Christian König
Quoted 27 lines · Timur Kristóf

It's 0x0fffffff and not 0x7fffffff.

Fixes d4a640d4b9f3 ("drm/amdgpu/vce1: Implement VCE1 IP block (v2)")

---

 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/vce_v1_0.c index
2fe931366985a..ce993b57b0e9f 100644

drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +1 -1
@@ -313,17 +313,17 @@ static int vce_v1_0_mc_resume(struct amdgpu_device
 *adev)> 
 	offset =  adev->vce.gpu_addr + AMDGPU_VCE_FIRMWARE_OFFSET;
 	size = VCE_V1_0_FW_SIZE;
 
-	WREG32(mmVCE_VCPU_CACHE_OFFSET0, offset & 0x7fffffff);
+	WREG32(mmVCE_VCPU_CACHE_OFFSET0, offset & 0x0fffffff);
 
 	WREG32(mmVCE_VCPU_CACHE_SIZE0, size);
 
 	offset += size;
 	size = VCE_V1_0_STACK_SIZE;
 

drivers/gpu/drm/amd/amdgpu/vce_v1_0.c static int vce_v1_0_mc_resume(struct amdgpu_device

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +2 -2
-	WREG32(mmVCE_VCPU_CACHE_OFFSET1, offset & 0x7fffffff);
+	WREG32(mmVCE_VCPU_CACHE_OFFSET1, offset & 0x0fffffff);
 
 	WREG32(mmVCE_VCPU_CACHE_SIZE1, size);
 
 	offset += size;
 	size = VCE_V1_0_DATA_SIZE;
 
-	WREG32(mmVCE_VCPU_CACHE_OFFSET2, offset & 0x7fffffff);
+	WREG32(mmVCE_VCPU_CACHE_OFFSET2, offset & 0x0fffffff);

We actually have that as VCE_VCPU_CACHE_OFFSET*__OFFSET_MASK in the headers, would probably be a good idea to use that one instead.

Additional to that limiting the value actually doesn't make much sense, that just hides the problem when we really get an offset which is to large.

Quoted 7 lines

We should probably rather have a WARN_ON(offset & ~VCE_VCPU_CACHE_OFFSET2__OFFSET_MASK) directly above the register write.

Apart from that the patch looks good to me.

Regards,
Christian.

Hi Christian,

Thanks, I agree and I'll add the necessary changes.
That being said, with some further testing it seems that VCE fails to initialize when the gpu_addr is 128 MiB or more, which makes me think that we should use 0x07ffffff instead of 0x0fffffff. Does that sound reasonable to you?

Thanks,
Timur

Quoted 20 lines
Quoted 20 lines
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +1 -1
 	WREG32(mmVCE_VCPU_CACHE_SIZE2, size);
 
 	WREG32_P(mmVCE_LMI_CTRL2, 0x0, ~0x100);
@@ -531,7 +531,7 @@ static int vce_v1_0_early_init(struct amdgpu_ip_block
 *ip_block)> 
 static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 {
 
 	u64 bo_size = amdgpu_bo_size(adev->vce.vcpu_bo);
 
-	u64 max_vcpu_bo_addr = 0xffffffff - bo_size;
+	u64 max_vcpu_bo_addr = 0x0fffffff - bo_size;
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +0 -0
 	u64 num_pages = ALIGN(bo_size, AMDGPU_GPU_PAGE_SIZE) /
 	AMDGPU_GPU_PAGE_SIZE; u64 pa = amdgpu_gmc_vram_pa(adev,
 	adev->vce.vcpu_bo);
 	u64 flags = AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE |
 	AMDGPU_PTE_VALID;
#8 lore raw

PATCH 4/7 drm/amdgpu/vce1: Remove superfluous address check

In reply to Timur Kristóf

The same thing is already checked a few lines above.

---
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c | 2 --
 1 file changed, 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +0 -2
index ce993b57b0e9f..35caef5a8102e 100644
@@ -553,8 +553,6 @@ static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 	amdgpu_gart_map_vram_range(adev, pa, adev->vce.gart_node.start,
 				   num_pages, flags, adev->gart.ptr);
 	adev->vce.gpu_addr = adev->gmc.gart_start + vce_gart_start_offs;
-	if (adev->vce.gpu_addr > max_vcpu_bo_addr)
-		return -EINVAL;
 
 	return 0;
 }
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 4/7
#9 lore raw

Re: [PATCH 4/7] drm/amdgpu/vce1: Remove superfluous address check

Quoted 3 lines · Timur Kristóf

The same thing is already checked a few lines above.

Quoted 17 lines
---
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c | 2 --
 1 file changed, 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +0 -2
index ce993b57b0e9f..35caef5a8102e 100644
@@ -553,8 +553,6 @@ static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 	amdgpu_gart_map_vram_range(adev, pa, adev->vce.gart_node.start,
 				   num_pages, flags, adev->gart.ptr);
 	adev->vce.gpu_addr = adev->gmc.gart_start + vce_gart_start_offs;
-	if (adev->vce.gpu_addr > max_vcpu_bo_addr)
-		return -EINVAL;
 
 	return 0;
 }
#10 lore raw

PATCH 5/7 drm/amdgpu/vce1: Check if VRAM address is lower than GART.

In reply to Timur Kristóf

Previously, I had assumed this was not possible so it was OK to not handle it, but now we got a report from a user who has a board that is configured this way.

When the VCPU BO is already located in a low 32-bit address in VRAM (eg. when VRAM is mapped to the low address space), don't do the workaround.

Fixes 66a80158aa2a ("amdgpu/vce: use amdgpu_gtt_mgr_alloc_entries")
---
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +3 -0
index 35caef5a8102e..b7b6096c1a1fd 100644
@@ -538,6 +538,9 @@ static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 	u64 vce_gart_start_offs;
 	int r;
 
+	if (adev->gmc.vram_start < adev->gmc.gart_start)
+		return amdgpu_bo_gpu_offset(adev->vce.vcpu_bo) <= max_vcpu_bo_addr ? 0 : -EINVAL;
+
 	r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr,
 					 &adev->vce.gart_node, num_pages, 0,
 					 DRM_MM_INSERT_LOW);
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 5/7
#11 lore raw

Re: [PATCH 5/7] drm/amdgpu/vce1: Check if VRAM address is lower than GART.

Quoted 10 lines · Timur Kristóf

Previously, I had assumed this was not possible so it was OK to not handle it, but now we got a report from a user who has a board that is configured this way.

When the VCPU BO is already located in a low 32-bit address in VRAM (eg. when VRAM is mapped to the low address space), don't do the workaround.

Fixes 66a80158aa2a ("amdgpu/vce: use amdgpu_gtt_mgr_alloc_entries")
Quoted 18 lines
---
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +3 -0
index 35caef5a8102e..b7b6096c1a1fd 100644
@@ -538,6 +538,9 @@ static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 	u64 vce_gart_start_offs;
 	int r;
 
+	if (adev->gmc.vram_start < adev->gmc.gart_start)
+		return amdgpu_bo_gpu_offset(adev->vce.vcpu_bo) <= max_vcpu_bo_addr ? 0 : -EINVAL;
+
 	r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr,
 					 &adev->vce.gart_node, num_pages, 0,
 					 DRM_MM_INSERT_LOW);
#12 lore raw

PATCH 6/7 drm/amdgpu/vce1: Don't repeat GTT MGR node allocation

In reply to Timur Kristóf

Only allocate entries from the GTT manager when the VCE GTT node is not allocated yet. This prevents the possibility of allocating them multiple times, which causes issues during GPU reset and suspend/resume.

Fixes 66a80158aa2a ("amdgpu/vce: use amdgpu_gtt_mgr_alloc_entries")
---
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +7 -5
index b7b6096c1a1fd..100aa48204c77 100644
@@ -541,11 +541,13 @@ static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 	if (adev->gmc.vram_start < adev->gmc.gart_start)
 		return amdgpu_bo_gpu_offset(adev->vce.vcpu_bo) <= max_vcpu_bo_addr ? 0 : -EINVAL;
 
-	r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr,
-					 &adev->vce.gart_node, num_pages, 0,
-					 DRM_MM_INSERT_LOW);
-	if (r)
-		return r;
+	if (!drm_mm_node_allocated(&adev->vce.gart_node)) {
+		r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr,
+						 &adev->vce.gart_node, num_pages, 0,
+						 DRM_MM_INSERT_LOW);
+		if (r)
+			return r;
+	}
 
 	vce_gart_start_offs = amdgpu_gtt_node_to_byte_offset(&adev->vce.gart_node);
 
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 6/7
#13 lore raw

Re: [PATCH 6/7] drm/amdgpu/vce1: Don't repeat GTT MGR node allocation

Quoted 7 lines · Timur Kristóf

Only allocate entries from the GTT manager when the VCE GTT node is not allocated yet. This prevents the possibility of allocating them multiple times, which causes issues during GPU reset and suspend/resume.

Fixes 66a80158aa2a ("amdgpu/vce: use amdgpu_gtt_mgr_alloc_entries")
Quoted 27 lines
---
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +7 -5
index b7b6096c1a1fd..100aa48204c77 100644
@@ -541,11 +541,13 @@ static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 	if (adev->gmc.vram_start < adev->gmc.gart_start)
 		return amdgpu_bo_gpu_offset(adev->vce.vcpu_bo) <= max_vcpu_bo_addr ? 0 : -EINVAL;
 
-	r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr,
-					 &adev->vce.gart_node, num_pages, 0,
-					 DRM_MM_INSERT_LOW);
-	if (r)
-		return r;
+	if (!drm_mm_node_allocated(&adev->vce.gart_node)) {
+		r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr,
+						 &adev->vce.gart_node, num_pages, 0,
+						 DRM_MM_INSERT_LOW);
+		if (r)
+			return r;
+	}
 
 	vce_gart_start_offs = amdgpu_gtt_node_to_byte_offset(&adev->vce.gart_node);
 
#14 lore raw

PATCH 7/7 drm/amdgpu/vce1: Align VCPU BO GART address to nearest power of two

In reply to Timur Kristóf

VCE accesses memory, including its firmware, through a BAR. It works slightly differently on each generation.
In case of VCE1, the start address of this BAR is zero and we can't change it due to the firmware validation mechanism.

Align the GART address of the VCPU BO like the VRAM address, in order to prevent it from crossing the boundaries of its BAR.

This fixes VCE1 initialization failure after suspend/resume.

Fixes 66a80158aa2a ("amdgpu/vce: use amdgpu_gtt_mgr_alloc_entries")
---
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +9 -2
index 100aa48204c77..9ddd635449873 100644
@@ -527,11 +527,17 @@ static int vce_v1_0_early_init(struct amdgpu_ip_block *ip_block)
  * To accomodate that, we put GART to the LOW address range
  * and reserve some GART pages where we map the VCPU BO,
  * so that it gets a 32-bit address.
+ *
+ * VCE accesses memory, including its firmware, through a BAR.
+ * It works slightly differently on each generation.
+ * In case of VCE1, the start address of this BAR is zero
+ * and we can't change it due to the firmware validation mechanism.
  */
 static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 {
 	u64 bo_size = amdgpu_bo_size(adev->vce.vcpu_bo);
-	u64 max_vcpu_bo_addr = 0x0fffffff - bo_size;
+	u64 aligned_size = roundup_pow_of_two(bo_size);
+	u64 max_vcpu_bo_addr = 0x0fffffff - aligned_size;
 	u64 num_pages = ALIGN(bo_size, AMDGPU_GPU_PAGE_SIZE) / AMDGPU_GPU_PAGE_SIZE;
 	u64 pa = amdgpu_gmc_vram_pa(adev, adev->vce.vcpu_bo);
 	u64 flags = AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE | AMDGPU_PTE_VALID;
@@ -543,7 +549,8 @@ static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 
 	if (!drm_mm_node_allocated(&adev->vce.gart_node)) {
 		r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr,
-						 &adev->vce.gart_node, num_pages, 0,
+						 &adev->vce.gart_node, num_pages,
+						 aligned_size >> AMDGPU_GPU_PAGE_SHIFT,
 						 DRM_MM_INSERT_LOW);
 		if (r)
 			return r;
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 7/7
#15 lore raw

Re: [PATCH 7/7] drm/amdgpu/vce1: Align VCPU BO GART address to nearest power of two

Quoted 5 lines · Timur Kristóf

VCE accesses memory, including its firmware, through a BAR. It works slightly differently on each generation.
In case of VCE1, the start address of this BAR is zero and we can't change it due to the firmware validation mechanism.

Quoted 7 lines

Align the GART address of the VCPU BO like the VRAM address, in order to prevent it from crossing the boundaries of its BAR.

This fixes VCE1 initialization failure after suspend/resume.

Fixes 66a80158aa2a ("amdgpu/vce: use amdgpu_gtt_mgr_alloc_entries")
Quoted 37 lines
---
 drivers/gpu/drm/amd/amdgpu/vce_v1_0.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/vce_v1_0.c +9 -2
index 100aa48204c77..9ddd635449873 100644
@@ -527,11 +527,17 @@ static int vce_v1_0_early_init(struct amdgpu_ip_block *ip_block)
  * To accomodate that, we put GART to the LOW address range
  * and reserve some GART pages where we map the VCPU BO,
  * so that it gets a 32-bit address.
+ *
+ * VCE accesses memory, including its firmware, through a BAR.
+ * It works slightly differently on each generation.
+ * In case of VCE1, the start address of this BAR is zero
+ * and we can't change it due to the firmware validation mechanism.
  */
 static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 {
 	u64 bo_size = amdgpu_bo_size(adev->vce.vcpu_bo);
-	u64 max_vcpu_bo_addr = 0x0fffffff - bo_size;
+	u64 aligned_size = roundup_pow_of_two(bo_size);
+	u64 max_vcpu_bo_addr = 0x0fffffff - aligned_size;
 	u64 num_pages = ALIGN(bo_size, AMDGPU_GPU_PAGE_SIZE) / AMDGPU_GPU_PAGE_SIZE;
 	u64 pa = amdgpu_gmc_vram_pa(adev, adev->vce.vcpu_bo);
 	u64 flags = AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE | AMDGPU_PTE_VALID;
@@ -543,7 +549,8 @@ static int vce_v1_0_ensure_vcpu_bo_32bit_addr(struct amdgpu_device *adev)
 
 	if (!drm_mm_node_allocated(&adev->vce.gart_node)) {
 		r = amdgpu_gtt_mgr_alloc_entries(&adev->mman.gtt_mgr,
-						 &adev->vce.gart_node, num_pages, 0,
+						 &adev->vce.gart_node, num_pages,
+						 aligned_size >> AMDGPU_GPU_PAGE_SHIFT,
 						 DRM_MM_INSERT_LOW);
 		if (r)
 			return r;