Skip to contentSkip to conversation

drm/amdgpu: Use feedback buffer for VCE destroy message

PATCH In review David Rosca 3 participants 6 messages Oct 9 – Oct 27

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

PATCH 1/4 drm/amdgpu: Use feedback buffer for VCE destroy message

VCE 1 and 2 always output feedback, same as for create message we must provide a valid buffer.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +20 -1
index 1f7a98cdce5a..39d87f3df9e0 100644
@@ -558,7 +558,9 @@ static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
 	const unsigned int ib_size_dw = 1024;
 	struct amdgpu_job *job;
 	struct amdgpu_ib *ib;
+	struct amdgpu_ib ib_msg;
 	struct dma_fence *f = NULL;
+	uint64_t addr;
 	int i, r;
 
 	r = amdgpu_job_alloc_with_ib(ring->adev, &ring->adev->vce.entity,
@@ -571,7 +573,17 @@ static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
 	if (r)
 		return r;
 
+	memset(&ib_msg, 0, sizeof(ib_msg));
+	/* only one gpu page is needed, alloc +1 page to make addr aligned. */
+	r = amdgpu_ib_get(ring->adev, NULL, AMDGPU_GPU_PAGE_SIZE * 2,
+			  direct ? AMDGPU_IB_POOL_DIRECT : AMDGPU_IB_POOL_DELAYED,
+			  &ib_msg);
+	if (r)
+		goto err;
+
 	ib = &job->ibs[0];
+	/* let addr point to page boundary */
+	addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg.gpu_addr);
 
 	/* stitch together an VCE destroy msg */
 	ib->length_dw = 0;
@@ -585,9 +597,15 @@ static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
 	ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */
 	ib->ptr[ib->length_dw++] = 0x00000000;
 	ib->ptr[ib->length_dw++] = 0x00000000;
-	ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */
+	ib->ptr[ib->length_dw++] = 0xffffffff;
 	ib->ptr[ib->length_dw++] = 0x00000000;
 
+	ib->ptr[ib->length_dw++] = 0x00000014; /* len */
+	ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
+	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
+	ib->ptr[ib->length_dw++] = addr;
+	ib->ptr[ib->length_dw++] = 0x00000001;
+
 	ib->ptr[ib->length_dw++] = 0x00000008; /* len */
 	ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
 
@@ -598,6 +616,7 @@ static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
 		r = amdgpu_job_submit_direct(job, ring, &f);
 	else
 		f = amdgpu_job_submit(job);
+	amdgpu_ib_free(&ib_msg, f);
 	if (r)
 		goto err;
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/4 drm/amdgpu: Fix VCE buffer size check for DPB and input surface

In create command we get pitch and height for DPB, input surface pitch and height is in encode command.
Fix the calculation to also consider different luma and chroma pitch.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 33 +++++++++++++++++--------
 1 file changed, 23 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +23 -10
index 07c4434c4948..2e1a48dafd21 100644
@@ -794,6 +794,7 @@ int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p,
 	uint32_t allocated = 0;
 	uint32_t tmp, handle = 0;
 	uint32_t dummy = 0xffffffff;
+	uint32_t luma_pitch, chroma_pitch, height;
 	uint32_t *size = &dummy;
 	unsigned int idx;
 	int i, r = 0;
@@ -902,19 +903,19 @@ int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p,
 				allocated |= 1 << session_idx;
 			}
 
-			uint32_t width, height;
-			width = amdgpu_ib_get_value(ib, idx + 8);
-			height = amdgpu_ib_get_value(ib, idx + 10);
+			luma_pitch = amdgpu_ib_get_value(ib, idx + 8);
+			chroma_pitch = amdgpu_ib_get_value(ib, idx + 9);
+			height = amdgpu_ib_get_value(ib, idx + 10); /* encRefYHeightInQw */
 
-			if (width == 0 || height == 0 ||
-			    width > 4096 || height > 2304) {
-				DRM_ERROR("invalid VCE image size: %ux%u\n",
-					  width, height);
+			if (luma_pitch == 0 || chroma_pitch == 0 || height == 0 ||
+			    luma_pitch > 4096 || chroma_pitch > 4096 || height > 2304 / 8) {
+				DRM_ERROR("invalid VCE ref image size: (%u %u)x%u\n",
+					  luma_pitch, chroma_pitch, height * 8);
 				r = -EINVAL;
 				goto out;
 			}
 
-			*size = width * height * 8 * 3 / 2;
+			*size = luma_pitch * height * 8 + chroma_pitch * height * 8 / 2;
 
 			break;
 
@@ -943,13 +944,25 @@ int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p,
 			break;
 
 		case 0x03000001: /* encode */
+			height = amdgpu_ib_get_value(ib, idx + 13);
+			luma_pitch = amdgpu_ib_get_value(ib, idx + 14);
+			chroma_pitch = amdgpu_ib_get_value(ib, idx + 15);
+
+			if (luma_pitch == 0 || chroma_pitch == 0 || height == 0 ||
+			    luma_pitch > 4096 || chroma_pitch > 4096 || height > 2304) {
+				DRM_ERROR("invalid VCE image size: (%u %u)x%u\n",
+					  luma_pitch, chroma_pitch, height);
+				r = -EINVAL;
+				goto out;
+			}
+
 			r = amdgpu_vce_cs_reloc(p, ib, idx + 10, idx + 9,
-						*size, 0);
+						luma_pitch * height, 0);
 			if (r)
 				goto out;
 
 			r = amdgpu_vce_cs_reloc(p, ib, idx + 12, idx + 11,
-						*size / 3, 0);
+						chroma_pitch * height / 2, 0);
 			if (r)
 				goto out;
 			break;
Signature · 1 line
2.43.0
#3 lore raw

PATCH 4/4 drm/amdgpu: Allow VCE SEI command in IB

In reply to David Rosca

Mesa will use it to get the slice offset in bitstream from feedback, which makes it possible to filter out SPS/PPS headers that VCE 1 and 2 FW always forces on IDR frames.

Bump driver version so that Mesa can use it.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 3 ++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 1 +
 2 files changed, 3 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -1
index 9c5e93cd3ee6..bb8e3e7b953c 100644
@@ -128,9 +128,10 @@
  * - 3.63.0 - GFX12 display DCC supports 256B max compressed block size
  * - 3.64.0 - Userq IP support query
  * - 3.65.0 - Add userq syncobj timeline signaling support
+ * - 3.66.0 - VCE SEI command and fixes
  */
 #define KMS_DRIVER_MAJOR	3
-#define KMS_DRIVER_MINOR	65
+#define KMS_DRIVER_MINOR	66
 #define KMS_DRIVER_PATCHLEVEL	0
 
 /*
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +1 -0
index 2e1a48dafd21..c18741d16ad8 100644
@@ -925,6 +925,7 @@ int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p,
 		case 0x04000007: /* motion estimation */
 		case 0x04000008: /* rdo */
 		case 0x04000009: /* vui */
+		case 0x0400000d: /* sei */
 		case 0x05000002: /* auxiliary buffer */
 		case 0x05000009: /* clock table */
 			break;
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/4 drm/amdgpu: Allow multiple VCE create commands with same handle

In reply to David Rosca

As long as it is done by the process that owns the session, additional create commands should be allowed.
This is needed for Vulkan to reset existing session.

The image size is now staged locally and only committed when the IB parses successfully, so a failed create can't change the size of a live session.

Assisted-by Claude:claude-opus-5-5
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 28 ++++++++++++++-----------
 1 file changed, 16 insertions(+), 12 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +16 -12
index 39d87f3df9e0..07c4434c4948 100644
@@ -788,6 +788,7 @@ int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p,
 {
 	unsigned int fb_idx = 0, bs_idx = 0;
 	int session_idx = -1;
+	uint32_t img_size[AMDGPU_MAX_VCE_HANDLES];
 	uint32_t destroyed = 0;
 	uint32_t created = 0;
 	uint32_t allocated = 0;
@@ -878,7 +879,15 @@ int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p,
 				r = session_idx;
 				goto out;
 			}
-			size = &p->adev->vce.img_size[session_idx];
+			/*
+			 * Stage the image size locally and only commit it once
+			 * the whole IB is valid, otherwise a failed create
+			 * could change the size of a live session.
+			 */
+			if (!(created & (1 << session_idx)))
+				img_size[session_idx] =
+					p->adev->vce.img_size[session_idx];
+			size = &img_size[session_idx];
 			break;
 
 		case 0x00000002: /* task info */
@@ -891,11 +900,6 @@ int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p,
 			if (destroyed & (1 << session_idx)) {
 				destroyed &= ~(1 << session_idx);
 				allocated |= 1 << session_idx;
-
-			} else if (!(allocated & (1 << session_idx))) {
-				DRM_ERROR("Handle already in use!\n");
-				r = -EINVAL;
-				goto out;
 			}
 
 			uint32_t width, height;
@@ -1010,7 +1014,12 @@ int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p,
 
 out:
 	if (!r) {
-		/* No error, free all destroyed handle slots */
+		/* No error, commit the image size of created sessions */
+		for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
+			if (created & (1 << i))
+				p->adev->vce.img_size[i] = img_size[i];
+
+		/* and free all destroyed handle slots */
 		tmp = destroyed;
 	} else {
 		/* Error during parsing, free all allocated handle slots */
@@ -1068,11 +1077,6 @@ int amdgpu_vce_ring_parse_cs_vm(struct amdgpu_cs_parser *p,
 			if (destroyed & (1 << session_idx)) {
 				destroyed &= ~(1 << session_idx);
 				allocated |= 1 << session_idx;
-
-			} else if (!(allocated & (1 << session_idx))) {
-				DRM_ERROR("Handle already in use!\n");
-				r = -EINVAL;
-				goto out;
 			}
 
 			break;
Signature · 1 line
2.43.0
Acked-by on v1 1/4
#4 lore raw
In reply to David Rosca

AMD General

The series is:

Quoted 82 lines

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of David Rosca
Sent: Friday, October 9, 2026 11:03 AM
To: [email protected]
Cc: Rosca, David <[email protected]>
Subject: [PATCH 1/4] drm/amdgpu: Use feedback buffer for VCE destroy message

VCE 1 and 2 always output feedback, same as for create message we must provide a valid buffer.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +2 -0
index 1f7a98cdce5a..39d87f3df9e0 100644
@@ -558,7 +558,9 @@ static int amdgpu_vce_get_destroy_msg(struct
 amdgpu_ring *ring, uint32_t handle,
      const unsigned int ib_size_dw = 1024;
      struct amdgpu_job *job;
      struct amdgpu_ib *ib;
+     struct amdgpu_ib ib_msg;
      struct dma_fence *f = NULL;
+     uint64_t addr;
      int i, r;
 
      r = amdgpu_job_alloc_with_ib(ring->adev, &ring->adev->vce.entity, @@
-571,7 +573,17 @@ static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring
*ring, uint32_t handle,
      if (r)
              return r;
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +4 -0
+     memset(&ib_msg, 0, sizeof(ib_msg));
+     /* only one gpu page is needed, alloc +1 page to make addr aligned. */
+     r = amdgpu_ib_get(ring->adev, NULL, AMDGPU_GPU_PAGE_SIZE * 2,
+                       direct ? AMDGPU_IB_POOL_DIRECT :

AMDGPU_IB_POOL_DELAYED,

drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +6 -1
+                       &ib_msg);
+     if (r)
+             goto err;
+
      ib = &job->ibs[0];
+     /* let addr point to page boundary */
+     addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg.gpu_addr);
 
      /* stitch together an VCE destroy msg */
      ib->length_dw = 0;
@@ -585,9 +597,15 @@ static int amdgpu_vce_get_destroy_msg(struct
 amdgpu_ring *ring, uint32_t handle,
      ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */
      ib->ptr[ib->length_dw++] = 0x00000000;
      ib->ptr[ib->length_dw++] = 0x00000000;
-     ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to

0xffffffff and firmware will not output feedback */

drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +8 -1
+     ib->ptr[ib->length_dw++] = 0xffffffff;
      ib->ptr[ib->length_dw++] = 0x00000000;
 
+     ib->ptr[ib->length_dw++] = 0x00000014; /* len */
+     ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
+     ib->ptr[ib->length_dw++] = upper_32_bits(addr);
+     ib->ptr[ib->length_dw++] = addr;
+     ib->ptr[ib->length_dw++] = 0x00000001;
+
      ib->ptr[ib->length_dw++] = 0x00000008; /* len */
      ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
@@ -598,6 +616,7 @@ static int amdgpu_vce_get_destroy_msg(struct
 amdgpu_ring *ring, uint32_t handle,
              r = amdgpu_job_submit_direct(job, ring, &f);
      else
              f = amdgpu_job_submit(job);
+     amdgpu_ib_free(&ib_msg, f);
      if (r)
              goto err;
 
--

2.43.0

Reviewed-by on v1 1/4
#5 lore raw
In reply to David Rosca

AMD General

This series is

-----Original Message-----
From: amd-gfx <[email protected]> On Behalf Of David Rosca
Sent: Friday, October 9, 2026 11:03 AM
To: [email protected]
Cc: Rosca, David <[email protected]>
Subject: [PATCH 1/4] drm/amdgpu: Use feedback buffer for VCE destroy message

VCE 1 and 2 always output feedback, same as for create message we must provide a valid buffer.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +2 -0
index 1f7a98cdce5a..39d87f3df9e0 100644
@@ -558,7 +558,9 @@ static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
        const unsigned int ib_size_dw = 1024;
        struct amdgpu_job *job;
        struct amdgpu_ib *ib;
+       struct amdgpu_ib ib_msg;
        struct dma_fence *f = NULL;
+       uint64_t addr;
        int i, r;
 
        r = amdgpu_job_alloc_with_ib(ring->adev, &ring->adev->vce.entity, @@ -571,7 +573,17 @@ static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
        if (r)
                return r;
+       memset(&ib_msg, 0, sizeof(ib_msg));
+       /* only one gpu page is needed, alloc +1 page to make addr aligned. */
+       r = amdgpu_ib_get(ring->adev, NULL, AMDGPU_GPU_PAGE_SIZE * 2,

+ direct ? AMDGPU_IB_POOL_DIRECT : AMDGPU_IB_POOL_DELAYED,
+ &ib_msg);
+ if (r)
+ goto err;
+

        ib = &job->ibs[0];

+ /* let addr point to page boundary */
+ addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg.gpu_addr);

        /* stitch together an VCE destroy msg */
        ib->length_dw = 0;
@@ -585,9 +597,15 @@ static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
        ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */
        ib->ptr[ib->length_dw++] = 0x00000000;
        ib->ptr[ib->length_dw++] = 0x00000000;
-       ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */
+       ib->ptr[ib->length_dw++] = 0xffffffff;
        ib->ptr[ib->length_dw++] = 0x00000000;
+       ib->ptr[ib->length_dw++] = 0x00000014; /* len */
+       ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
+       ib->ptr[ib->length_dw++] = upper_32_bits(addr);
+       ib->ptr[ib->length_dw++] = addr;
+       ib->ptr[ib->length_dw++] = 0x00000001;
+
        ib->ptr[ib->length_dw++] = 0x00000008; /* len */
        ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */

@@ -598,6 +616,7 @@ static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,

                r = amdgpu_job_submit_direct(job, ring, &f);
        else
                f = amdgpu_job_submit(job);
+       amdgpu_ib_free(&ib_msg, f);
        if (r)
                goto err;

--
2.43.0