Skip to contentSkip to conversation

drm/amdgpu/vcn4: Fix IB parsing with multiple engine info packages

PATCH In review David Rosca 2 participants 3 messages Aug 18, 2025 – Aug 19, 2025

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

PATCH 1/2 drm/amdgpu/vcn4: Fix IB parsing with multiple engine info packages

There can be multiple engine info packages in one IB and the first one may be common engine, not decode/encode.
We need to parse the entire IB instead of stopping after finding first engine info.

---
 drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c | 52 +++++++++++----------------
 1 file changed, 21 insertions(+), 31 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c +21 -31
index 1785786a72f8..23b98f011879 100644
@@ -1904,22 +1904,16 @@ static int vcn_v4_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job,
 
 #define RADEON_VCN_ENGINE_TYPE_ENCODE			(0x00000002)
 #define RADEON_VCN_ENGINE_TYPE_DECODE			(0x00000003)
-
 #define RADEON_VCN_ENGINE_INFO				(0x30000001)
-#define RADEON_VCN_ENGINE_INFO_MAX_OFFSET		16
-
 #define RENCODE_ENCODE_STANDARD_AV1			2
 #define RENCODE_IB_PARAM_SESSION_INIT			0x00000003
-#define RENCODE_IB_PARAM_SESSION_INIT_MAX_OFFSET	64
 
-/* return the offset in ib if id is found, -1 otherwise
- * to speed up the searching we only search upto max_offset
- */
-static int vcn_v4_0_enc_find_ib_param(struct amdgpu_ib *ib, uint32_t id, int max_offset)
+/* return the offset in ib if id is found, -1 otherwise */
+static int vcn_v4_0_enc_find_ib_param(struct amdgpu_ib *ib, uint32_t id, int start)
 {
 	int i;
 
-	for (i = 0; i < ib->length_dw && i < max_offset && ib->ptr[i] >= 8; i += ib->ptr[i]/4) {
+	for (i = start; i < ib->length_dw && ib->ptr[i] >= 8; i += ib->ptr[i]/4) {
 		if (ib->ptr[i + 1] == id)
 			return i;
 	}
@@ -1934,33 +1928,29 @@ static int vcn_v4_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p,
 	struct amdgpu_vcn_decode_buffer *decode_buffer;
 	uint64_t addr;
 	uint32_t val;
-	int idx;
+	int idx = 0;
 
 	/* The first instance can decode anything */
 	if (!ring->me)
 		return 0;
 
-	/* RADEON_VCN_ENGINE_INFO is at the top of ib block */
-	idx = vcn_v4_0_enc_find_ib_param(ib, RADEON_VCN_ENGINE_INFO,
-			RADEON_VCN_ENGINE_INFO_MAX_OFFSET);
-	if (idx < 0) /* engine info is missing */
-		return 0;
-
-	val = amdgpu_ib_get_value(ib, idx + 2); /* RADEON_VCN_ENGINE_TYPE */
-	if (val == RADEON_VCN_ENGINE_TYPE_DECODE) {
-		decode_buffer = (struct amdgpu_vcn_decode_buffer *)&ib->ptr[idx + 6];
-
-		if (!(decode_buffer->valid_buf_flag  & 0x1))
-			return 0;
-
-		addr = ((u64)decode_buffer->msg_buffer_address_hi) << 32 |
-			decode_buffer->msg_buffer_address_lo;
-		return vcn_v4_0_dec_msg(p, job, addr);
-	} else if (val == RADEON_VCN_ENGINE_TYPE_ENCODE) {
-		idx = vcn_v4_0_enc_find_ib_param(ib, RENCODE_IB_PARAM_SESSION_INIT,
-			RENCODE_IB_PARAM_SESSION_INIT_MAX_OFFSET);
-		if (idx >= 0 && ib->ptr[idx + 2] == RENCODE_ENCODE_STANDARD_AV1)
-			return vcn_v4_0_limit_sched(p, job);
+	while ((idx = vcn_v4_0_enc_find_ib_param(ib, RADEON_VCN_ENGINE_INFO, idx)) >= 0) {
+		val = amdgpu_ib_get_value(ib, idx + 2); /* RADEON_VCN_ENGINE_TYPE */
+		if (val == RADEON_VCN_ENGINE_TYPE_DECODE) {
+			decode_buffer = (struct amdgpu_vcn_decode_buffer *)&ib->ptr[idx + 6];
+
+			if (!(decode_buffer->valid_buf_flag & 0x1))
+				return 0;
+
+			addr = ((u64)decode_buffer->msg_buffer_address_hi) << 32 |
+				decode_buffer->msg_buffer_address_lo;
+			return vcn_v4_0_dec_msg(p, job, addr);
+		} else if (val == RADEON_VCN_ENGINE_TYPE_ENCODE) {
+			idx = vcn_v4_0_enc_find_ib_param(ib, RENCODE_IB_PARAM_SESSION_INIT, idx);
+			if (idx >= 0 && ib->ptr[idx + 2] == RENCODE_ENCODE_STANDARD_AV1)
+				return vcn_v4_0_limit_sched(p, job);
+		}
+		idx += ib->ptr[idx] / 4;
 	}
 	return 0;
 }
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 drm/amdgpu/vcn: Allow limiting ctx to instance 0 for AV1 at any time

There is no reason to require this to happen on first submitted IB only. We need to wait for the queue to be idle, but it can be done at any time (including when there are multiple video sessions active).

---
 drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c | 12 ++++++++----
 drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c | 12 ++++++++----
 2 files changed, 16 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c +8 -4
index 95173156f956..f3085137ba08 100644
@@ -1886,15 +1886,19 @@ static int vcn_v3_0_limit_sched(struct amdgpu_cs_parser *p,
 				struct amdgpu_job *job)
 {
 	struct drm_gpu_scheduler **scheds;
-
-	/* The create msg must be in the first IB submitted */
-	if (atomic_read(&job->base.entity->fence_seq))
-		return -EINVAL;
+	struct dma_fence *fence;
 
 	/* if VCN0 is harvested, we can't support AV1 */
 	if (p->adev->vcn.harvest_config & AMDGPU_VCN_HARVEST_VCN0)
 		return -EINVAL;
 
+	/* wait for all jobs to finish before switching to instance 0 */
+	fence = amdgpu_ctx_get_fence(p->ctx, job->base.entity, ~0ull);
+	if (fence) {
+		dma_fence_wait(fence, false);
+		dma_fence_put(fence);
+	}
+
 	scheds = p->adev->gpu_sched[AMDGPU_HW_IP_VCN_DEC]
 		[AMDGPU_RING_PRIO_DEFAULT].sched;
 	drm_sched_entity_modify_sched(job->base.entity, scheds, 1);
drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c +8 -4
index 23b98f011879..235fc9e01dd7 100644
@@ -1805,15 +1805,19 @@ static int vcn_v4_0_limit_sched(struct amdgpu_cs_parser *p,
 				struct amdgpu_job *job)
 {
 	struct drm_gpu_scheduler **scheds;
-
-	/* The create msg must be in the first IB submitted */
-	if (atomic_read(&job->base.entity->fence_seq))
-		return -EINVAL;
+	struct dma_fence *fence;
 
 	/* if VCN0 is harvested, we can't support AV1 */
 	if (p->adev->vcn.harvest_config & AMDGPU_VCN_HARVEST_VCN0)
 		return -EINVAL;
 
+	/* wait for all jobs to finish before switching to instance 0 */
+	fence = amdgpu_ctx_get_fence(p->ctx, job->base.entity, ~0ull);
+	if (fence) {
+		dma_fence_wait(fence, false);
+		dma_fence_put(fence);
+	}
+
 	scheds = p->adev->gpu_sched[AMDGPU_HW_IP_VCN_ENC]
 		[AMDGPU_RING_PRIO_0].sched;
 	drm_sched_entity_modify_sched(job->base.entity, scheds, 1);
Signature · 1 line
2.43.0
Reviewed-by on v1 2/2
#2 lore raw

RE: [PATCH 2/2] drm/amdgpu/vcn: Allow limiting ctx to instance 0 for AV1 at any time

[AMD Official Use Only - AMD Internal Distribution Only]

The series is:

Quoted 81 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of David Rosca
Sent: August 18, 2025 5:43 AM
To: [email protected]
Cc: Rosca, David <[email protected]>
Subject: [PATCH 2/2] drm/amdgpu/vcn: Allow limiting ctx to instance 0 for AV1 at any time

There is no reason to require this to happen on first submitted IB only. We need to wait for the queue to be idle, but it can be done at any time (including when there are multiple video sessions active).

---
 drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c | 12 ++++++++----
drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c | 12 ++++++++----
 2 files changed, 16 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v3_0.c +8 -4
index 95173156f956..f3085137ba08 100644
@@ -1886,15 +1886,19 @@ static int vcn_v3_0_limit_sched(struct
 amdgpu_cs_parser *p,
                              struct amdgpu_job *job)
 {
      struct drm_gpu_scheduler **scheds;
-
-     /* The create msg must be in the first IB submitted */
-     if (atomic_read(&job->base.entity->fence_seq))
-             return -EINVAL;
+     struct dma_fence *fence;
 
      /* if VCN0 is harvested, we can't support AV1 */
      if (p->adev->vcn.harvest_config & AMDGPU_VCN_HARVEST_VCN0)
              return -EINVAL;
 
+     /* wait for all jobs to finish before switching to instance 0 */
+     fence = amdgpu_ctx_get_fence(p->ctx, job->base.entity, ~0ull);
+     if (fence) {
+             dma_fence_wait(fence, false);
+             dma_fence_put(fence);
+     }
+
      scheds = p->adev->gpu_sched[AMDGPU_HW_IP_VCN_DEC]
              [AMDGPU_RING_PRIO_DEFAULT].sched;
      drm_sched_entity_modify_sched(job->base.entity, scheds, 1); diff --

git a/drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c
b/drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c
index 23b98f011879..235fc9e01dd7 100644

drivers/gpu/drm/amd/amdgpu/vcn_v4_0.c +8 -4
@@ -1805,15 +1805,19 @@ static int vcn_v4_0_limit_sched(struct
 amdgpu_cs_parser *p,
                              struct amdgpu_job *job)
 {
      struct drm_gpu_scheduler **scheds;
-
-     /* The create msg must be in the first IB submitted */
-     if (atomic_read(&job->base.entity->fence_seq))
-             return -EINVAL;
+     struct dma_fence *fence;
 
      /* if VCN0 is harvested, we can't support AV1 */
      if (p->adev->vcn.harvest_config & AMDGPU_VCN_HARVEST_VCN0)
              return -EINVAL;
 
+     /* wait for all jobs to finish before switching to instance 0 */
+     fence = amdgpu_ctx_get_fence(p->ctx, job->base.entity, ~0ull);
+     if (fence) {
+             dma_fence_wait(fence, false);
+             dma_fence_put(fence);
+     }
+
      scheds = p->adev->gpu_sched[AMDGPU_HW_IP_VCN_ENC]
              [AMDGPU_RING_PRIO_0].sched;
      drm_sched_entity_modify_sched(job->base.entity, scheds, 1);

--
2.43.0