Skip to contentSkip to conversation

drm/amd/amdgpu: command submission parser for JPEG

PATCH In review David (Ming Qiang) Wu 4 participants 8 messages Aug 6, 2024 – Aug 19, 2024

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

PATCH 1/2 drm/amd/amdgpu: command submission parser for JPEG

Add JPEG IB command parser to ensure registers
in the command are within the JPEG IP block.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c   |  3 ++
 drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c | 58 +++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h |  7 ++-
 drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c |  1 +
 drivers/gpu/drm/amd/amdgpu/soc15d.h      |  6 +++
 5 files changed, 73 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -0
index 1e167d925b64..78b3c067fea7 100644
@@ -1061,6 +1061,9 @@ static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p,
 			r = amdgpu_ring_parse_cs(ring, p, job, ib);
 			if (r)
 				return r;
+
+			if (ib->sa_bo)
+				ib->gpu_addr =  amdgpu_sa_bo_gpu_addr(ib->sa_bo);
 		} else {
 			ib->ptr = (uint32_t *)kptr;
 			r = amdgpu_ring_patch_cs_in_place(ring, p, job, ib);
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +57 -1

73 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h +6 -1
index 747a3e5f6856..71c54b294e15 100644
@@ -46,6 +46,9 @@
 
 #define JRBC_DEC_EXTERNAL_REG_WRITE_ADDR				0x18000
 
+#define JPEG_REG_RANGE_START						0x4000
+#define JPEG_REG_RANGE_END						0x41c2
+
 extern const struct amdgpu_ip_block_version jpeg_v4_0_3_ip_block;
 
 void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring,
@@ -62,5 +65,7 @@ void jpeg_v4_0_3_dec_ring_insert_end(struct amdgpu_ring *ring);
 void jpeg_v4_0_3_dec_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
 void jpeg_v4_0_3_dec_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
 					uint32_t val, uint32_t mask);
-
+int jpeg_v4_0_3_dec_ring_parse_cs(struct amdgpu_cs_parser *parser,
+				  struct amdgpu_job *job,
+				  struct amdgpu_ib *ib);
 #endif /* __JPEG_V4_0_3_H__ */
drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c +1 -0
index d694a276498a..f4daff90c770 100644
@@ -646,6 +646,7 @@ static const struct amdgpu_ring_funcs jpeg_v5_0_0_dec_ring_vm_funcs = {
 	.get_rptr = jpeg_v5_0_0_dec_ring_get_rptr,
 	.get_wptr = jpeg_v5_0_0_dec_ring_get_wptr,
 	.set_wptr = jpeg_v5_0_0_dec_ring_set_wptr,
+	.parse_cs = jpeg_v4_0_3_dec_ring_parse_cs,
 	.emit_frame_size =
 		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
 		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
drivers/gpu/drm/amd/amdgpu/soc15d.h +6 -0
index 2357ff39323f..e74e1983da53 100644
@@ -76,6 +76,12 @@
 			 ((cond & 0xF) << 24) |				\
 			 ((type & 0xF) << 28))
 
+#define CP_PACKETJ_NOP		0x60000000
+#define CP_PACKETJ_GET_REG(x)  ((x) & 0x3FFFF)
+#define CP_PACKETJ_GET_RES(x)  (((x) >> 18) & 0x3F)
+#define CP_PACKETJ_GET_COND(x) (((x) >> 24) & 0xF)
+#define CP_PACKETJ_GET_TYPE(x) (((x) >> 28) & 0xF)
+
 /* Packet 3 types */
 #define	PACKET3_NOP					0x10
 #define	PACKET3_SET_BASE				0x11
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/2 drm/amd/amdgpu: cleanup parse_cs callbacks

Because gpu_addr is updated in the calling routine (amdgpu_cs_patch_ibs()),it is removed in the callback.

Use .patch_cs_in_place instead of .parse_cs for amdgpu_vce_ring_parse_cs_vm() as there is no need for keeping a temporary IB, therefore ib->sa_bo is NULL and amdgpu_ib_free() is removed.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 1 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 2 --
 drivers/gpu/drm/amd/amdgpu/vce_v3_0.c   | 2 +-
 drivers/gpu/drm/amd/amdgpu/vce_v4_0.c   | 2 +-
 4 files changed, 2 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +0 -1
index 07d930339b07..98863e920e00 100644
@@ -1088,7 +1088,6 @@ int amdgpu_uvd_ring_parse_cs(struct amdgpu_cs_parser *parser,
 	int r;
 
 	job->vm = NULL;
-	ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
 
 	if (ib->length_dw % 16) {
 		DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +0 -2
index 968ca2c84ef7..6efb15c5fa1a 100644
@@ -749,7 +749,6 @@ int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p,
 	int i, r = 0;
 
 	job->vm = NULL;
-	ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
 
 	for (idx = 0; idx < ib->length_dw;) {
 		uint32_t len = amdgpu_ib_get_value(ib, idx);
@@ -1044,7 +1043,6 @@ int amdgpu_vce_ring_parse_cs_vm(struct amdgpu_cs_parser *p,
 	if (!r) {
 		/* No error, free all destroyed handle slots */
 		tmp = destroyed;
-		amdgpu_ib_free(p->adev, ib, NULL);
 	} else {
 		/* Error during parsing, free all allocated handle slots */
 		tmp = allocated;
drivers/gpu/drm/amd/amdgpu/vce_v3_0.c +1 -1
index 32517c364cf7..4bfba2931b08 100644
@@ -950,7 +950,7 @@ static const struct amdgpu_ring_funcs vce_v3_0_ring_vm_funcs = {
 	.get_rptr = vce_v3_0_ring_get_rptr,
 	.get_wptr = vce_v3_0_ring_get_wptr,
 	.set_wptr = vce_v3_0_ring_set_wptr,
-	.parse_cs = amdgpu_vce_ring_parse_cs_vm,
+	.patch_cs_in_place = amdgpu_vce_ring_parse_cs_vm,
 	.emit_frame_size =
 		6 + /* vce_v3_0_emit_vm_flush */
 		4 + /* vce_v3_0_emit_pipeline_sync */
drivers/gpu/drm/amd/amdgpu/vce_v4_0.c +1 -1
index 06d787385ad4..0748bf44c880 100644
@@ -1102,7 +1102,7 @@ static const struct amdgpu_ring_funcs vce_v4_0_ring_vm_funcs = {
 	.get_rptr = vce_v4_0_ring_get_rptr,
 	.get_wptr = vce_v4_0_ring_get_wptr,
 	.set_wptr = vce_v4_0_ring_set_wptr,
-	.parse_cs = amdgpu_vce_ring_parse_cs_vm,
+	.patch_cs_in_place = amdgpu_vce_ring_parse_cs_vm,
 	.emit_frame_size =
 		SOC15_FLUSH_GPU_TLB_NUM_WREG * 3 +
 		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 4 +
Signature · 1 line
2.34.1
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] drm/amd/amdgpu: cleanup parse_cs callbacks

Quoted 8 lines · David (Ming Qiang) Wu

Because gpu_addr is updated in the calling routine (amdgpu_cs_patch_ibs()),it is removed in the callback.

Use .patch_cs_in_place instead of .parse_cs for amdgpu_vce_ring_parse_cs_vm() as there is no need for keeping a temporary IB, therefore ib->sa_bo is NULL and amdgpu_ib_free() is removed.

Took me a while to page this all back into my head, but this works because in this case, we still use the user's IB, we just use the parsing to deal with the session handles. This code really should have used patch_cs_in_place from the beginning, but it didn't exist at that point.

Quoted 70 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c | 1 -
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 2 --
 drivers/gpu/drm/amd/amdgpu/vce_v3_0.c   | 2 +-
 drivers/gpu/drm/amd/amdgpu/vce_v4_0.c   | 2 +-
 4 files changed, 2 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_uvd.c +0 -1
index 07d930339b07..98863e920e00 100644
@@ -1088,7 +1088,6 @@ int amdgpu_uvd_ring_parse_cs(struct amdgpu_cs_parser *parser,
        int r;
 
        job->vm = NULL;
-       ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
 
        if (ib->length_dw % 16) {
                DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +0 -2
index 968ca2c84ef7..6efb15c5fa1a 100644
@@ -749,7 +749,6 @@ int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p,
        int i, r = 0;
 
        job->vm = NULL;
-       ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
 
        for (idx = 0; idx < ib->length_dw;) {
                uint32_t len = amdgpu_ib_get_value(ib, idx);
@@ -1044,7 +1043,6 @@ int amdgpu_vce_ring_parse_cs_vm(struct amdgpu_cs_parser *p,
        if (!r) {
                /* No error, free all destroyed handle slots */
                tmp = destroyed;
-               amdgpu_ib_free(p->adev, ib, NULL);
        } else {
                /* Error during parsing, free all allocated handle slots */
                tmp = allocated;
drivers/gpu/drm/amd/amdgpu/vce_v3_0.c +1 -1
index 32517c364cf7..4bfba2931b08 100644
@@ -950,7 +950,7 @@ static const struct amdgpu_ring_funcs vce_v3_0_ring_vm_funcs = {
        .get_rptr = vce_v3_0_ring_get_rptr,
        .get_wptr = vce_v3_0_ring_get_wptr,
        .set_wptr = vce_v3_0_ring_set_wptr,
-       .parse_cs = amdgpu_vce_ring_parse_cs_vm,
+       .patch_cs_in_place = amdgpu_vce_ring_parse_cs_vm,
        .emit_frame_size =
                6 + /* vce_v3_0_emit_vm_flush */
                4 + /* vce_v3_0_emit_pipeline_sync */
drivers/gpu/drm/amd/amdgpu/vce_v4_0.c +1 -2
index 06d787385ad4..0748bf44c880 100644
@@ -1102,7 +1102,7 @@ static const struct amdgpu_ring_funcs vce_v4_0_ring_vm_funcs = {
        .get_rptr = vce_v4_0_ring_get_rptr,
        .get_wptr = vce_v4_0_ring_get_wptr,
        .set_wptr = vce_v4_0_ring_set_wptr,
-       .parse_cs = amdgpu_vce_ring_parse_cs_vm,
+       .patch_cs_in_place = amdgpu_vce_ring_parse_cs_vm,
        .emit_frame_size =
                SOC15_FLUSH_GPU_TLB_NUM_WREG * 3 +
                SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 4 +
--

2.34.1

#3 lore raw
In reply to David (Ming Qiang) Wu
Quoted 32 lines · David (Ming Qiang) Wu

Add JPEG IB command parser to ensure registers
in the command are within the JPEG IP block.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c   |  3 ++
 drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c | 58 +++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h |  7 ++-
 drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c |  1 +
 drivers/gpu/drm/amd/amdgpu/soc15d.h      |  6 +++
 5 files changed, 73 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -0
index 1e167d925b64..78b3c067fea7 100644
@@ -1061,6 +1061,9 @@ static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p,
                        r = amdgpu_ring_parse_cs(ring, p, job, ib);
                        if (r)
                                return r;
+
+                       if (ib->sa_bo)
+                               ib->gpu_addr =  amdgpu_sa_bo_gpu_addr(ib->sa_bo);
                } else {
                        ib->ptr = (uint32_t *)kptr;
                        r = amdgpu_ring_patch_cs_in_place(ring, p, job, ib);
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +0 -0
index ad524ddc9760..6ccf61d7c13d 100644

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring, · line 789

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +5 -1
@@ -782,7 +782,11 @@ void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring,
 
        amdgpu_ring_write(ring, PACKETJ(regUVD_LMI_JRBC_IB_VMID_INTERNAL_OFFSET,
                0, 0, PACKETJ_TYPE0));
-       amdgpu_ring_write(ring, (vmid | (vmid << 4)));
+
+       if (ring->funcs->parse_cs)
+               amdgpu_ring_write(ring, 0);
+       else
+               amdgpu_ring_write(ring, (vmid | (vmid << 4)));

This part doesn't make sense to me. If we use vmid 0, we'll need to also patch any addresses in the IB itself to use vmid 0 as well unless there is a way to use a vmid 0 address for the IB base, but a different vmid for the addresses in the IB. If it's the latter, then we need to make sure that is set up and specify the vmid to use.

Quoted 29 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +15 -0
        amdgpu_ring_write(ring, PACKETJ(regUVD_LMI_JPEG_VMID_INTERNAL_OFFSET,
                0, 0, PACKETJ_TYPE0));
@@ -1084,6 +1088,7 @@ static const struct amdgpu_ring_funcs jpeg_v4_0_3_dec_ring_vm_funcs = {
        .get_rptr = jpeg_v4_0_3_dec_ring_get_rptr,
        .get_wptr = jpeg_v4_0_3_dec_ring_get_wptr,
        .set_wptr = jpeg_v4_0_3_dec_ring_set_wptr,
+       .parse_cs = jpeg_v4_0_3_dec_ring_parse_cs,
        .emit_frame_size =
                SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
                SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
@@ -1248,3 +1253,54 @@ static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev)
 {
        adev->jpeg.ras = &jpeg_v4_0_3_ras;
 }
+
+/**
+ * jpeg_v4_0_3_dec_ring_parse_cs - command submission parser
+ *
+ * @parser: Command submission parser context
+ * @job: the job to parse
+ * @ib: the IB to parse
+ *
+ * Parse the command stream, return -EINVAL for invalid packet
+ * 0 otherwise
+ */
+int jpeg_v4_0_3_dec_ring_parse_cs(struct amdgpu_cs_parser *parser,
+                            struct amdgpu_job *job,
+                            struct amdgpu_ib *ib)

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev) · line 1279

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +10 -0
+{
+       uint32_t i, reg, res, cond, type;
+
+       for (i = 0; i < ib->length_dw ; i += 2) {
+               reg  = CP_PACKETJ_GET_REG(ib->ptr[i]);
+               res  = CP_PACKETJ_GET_RES(ib->ptr[i]);
+               cond = CP_PACKETJ_GET_COND(ib->ptr[i]);
+               type = CP_PACKETJ_GET_TYPE(ib->ptr[i]);
+
+               if(res) /* only support 0 at the moment */

space between if and (.

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev) · line 1285

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +6 -0
+                       return -EINVAL;
+
+               switch (type) {
+               case PACKETJ_TYPE0:
+                       if (cond != PACKETJ_CONDITION_CHECK0 || reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END) {
+                               DRM_ERROR("Invalid packet [0x%08x]!\n", ib->ptr[i]);

use dev_err() so we can differentiate between multiple GPUs.

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev) · line 1291

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +6 -0
+                               return -EINVAL;
+                       }
+                       break;
+               case PACKETJ_TYPE3:
+                       if (cond != PACKETJ_CONDITION_CHECK3 || reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END) {
+                               DRM_ERROR("Invalid packet [0x%08x]!\n", ib->ptr[i]);

Same here.

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev) · line 1300

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +9 -0
+                               return -EINVAL;
+                       }
+                       break;
+               case PACKETJ_TYPE6:
+                       if (ib->ptr[i] == CP_PACKETJ_NOP)
+                               continue;
+                       return -EINVAL;
+               default:
+                       DRM_ERROR("Unknown packet type %d !\n", type);

And here.

Quoted 61 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +6 -0
+                       return -EINVAL;
+               }
+       }
+
+       return 0;
+}
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h +6 -1
index 747a3e5f6856..71c54b294e15 100644
@@ -46,6 +46,9 @@
 
 #define JRBC_DEC_EXTERNAL_REG_WRITE_ADDR                               0x18000
 
+#define JPEG_REG_RANGE_START                                           0x4000
+#define JPEG_REG_RANGE_END                                             0x41c2
+
 extern const struct amdgpu_ip_block_version jpeg_v4_0_3_ip_block;
 
 void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring,
@@ -62,5 +65,7 @@ void jpeg_v4_0_3_dec_ring_insert_end(struct amdgpu_ring *ring);
 void jpeg_v4_0_3_dec_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
 void jpeg_v4_0_3_dec_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
                                        uint32_t val, uint32_t mask);
-
+int jpeg_v4_0_3_dec_ring_parse_cs(struct amdgpu_cs_parser *parser,
+                                 struct amdgpu_job *job,
+                                 struct amdgpu_ib *ib);
 #endif /* __JPEG_V4_0_3_H__ */
drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c +1 -0
index d694a276498a..f4daff90c770 100644
@@ -646,6 +646,7 @@ static const struct amdgpu_ring_funcs jpeg_v5_0_0_dec_ring_vm_funcs = {
        .get_rptr = jpeg_v5_0_0_dec_ring_get_rptr,
        .get_wptr = jpeg_v5_0_0_dec_ring_get_wptr,
        .set_wptr = jpeg_v5_0_0_dec_ring_set_wptr,
+       .parse_cs = jpeg_v4_0_3_dec_ring_parse_cs,
        .emit_frame_size =
                SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
                SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
drivers/gpu/drm/amd/amdgpu/soc15d.h +6 -1
index 2357ff39323f..e74e1983da53 100644
@@ -76,6 +76,12 @@
                         ((cond & 0xF) << 24) |                         \
                         ((type & 0xF) << 28))
 
+#define CP_PACKETJ_NOP         0x60000000
+#define CP_PACKETJ_GET_REG(x)  ((x) & 0x3FFFF)
+#define CP_PACKETJ_GET_RES(x)  (((x) >> 18) & 0x3F)
+#define CP_PACKETJ_GET_COND(x) (((x) >> 24) & 0xF)
+#define CP_PACKETJ_GET_TYPE(x) (((x) >> 28) & 0xF)
+
 /* Packet 3 types */
 #define        PACKET3_NOP                                     0x10
 #define        PACKET3_SET_BASE                                0x11
--

2.34.1

Reviewed-by
#4 lore raw
Quoted 35 lines · Alex Deucher
Quoted 33 lines · David (Ming Qiang) Wu

Add JPEG IB command parser to ensure registers
in the command are within the JPEG IP block.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c   |  3 ++
 drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c | 58 +++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h |  7 ++-
 drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c |  1 +
 drivers/gpu/drm/amd/amdgpu/soc15d.h      |  6 +++
 5 files changed, 73 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -0
index 1e167d925b64..78b3c067fea7 100644
@@ -1061,6 +1061,9 @@ static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p,
                        r = amdgpu_ring_parse_cs(ring, p, job, ib);
                        if (r)
                                return r;
+
+                       if (ib->sa_bo)
+                               ib->gpu_addr =  amdgpu_sa_bo_gpu_addr(ib->sa_bo);
                } else {
                        ib->ptr = (uint32_t *)kptr;
                        r = amdgpu_ring_patch_cs_in_place(ring, p, job, ib);
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +0 -0
index ad524ddc9760..6ccf61d7c13d 100644
@@ -782,7 +782,11 @@ void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring,
 
Quoted 14 lines

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring, · line 789

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +5 -1
        amdgpu_ring_write(ring, PACKETJ(regUVD_LMI_JRBC_IB_VMID_INTERNAL_OFFSET,
                0, 0, PACKETJ_TYPE0));
-       amdgpu_ring_write(ring, (vmid | (vmid << 4)));
+
+       if (ring->funcs->parse_cs)
+               amdgpu_ring_write(ring, 0);
+       else
+               amdgpu_ring_write(ring, (vmid | (vmid << 4)));

This part doesn't make sense to me. If we use vmid 0, we'll need to also patch any addresses in the IB itself to use vmid 0 as well unless there is a way to use a vmid 0 address for the IB base, but a different vmid for the addresses in the IB. If it's the latter, then we need to make sure that is set up and specify the vmid to use.

David and I discussed this and this is handled in UVD_LMI_JPEG_VMID.

With the below nits fixed, this patch is:

Quoted 134 lines
Quoted 30 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +15 -0
        amdgpu_ring_write(ring, PACKETJ(regUVD_LMI_JPEG_VMID_INTERNAL_OFFSET,
                0, 0, PACKETJ_TYPE0));
@@ -1084,6 +1088,7 @@ static const struct amdgpu_ring_funcs jpeg_v4_0_3_dec_ring_vm_funcs = {
        .get_rptr = jpeg_v4_0_3_dec_ring_get_rptr,
        .get_wptr = jpeg_v4_0_3_dec_ring_get_wptr,
        .set_wptr = jpeg_v4_0_3_dec_ring_set_wptr,
+       .parse_cs = jpeg_v4_0_3_dec_ring_parse_cs,
        .emit_frame_size =
                SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
                SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
@@ -1248,3 +1253,54 @@ static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev)
 {
        adev->jpeg.ras = &jpeg_v4_0_3_ras;
 }
+
+/**
+ * jpeg_v4_0_3_dec_ring_parse_cs - command submission parser
+ *
+ * @parser: Command submission parser context
+ * @job: the job to parse
+ * @ib: the IB to parse
+ *
+ * Parse the command stream, return -EINVAL for invalid packet
+ * 0 otherwise
+ */
+int jpeg_v4_0_3_dec_ring_parse_cs(struct amdgpu_cs_parser *parser,
+                            struct amdgpu_job *job,
+                            struct amdgpu_ib *ib)

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev) · line 1279

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +10 -0
+{
+       uint32_t i, reg, res, cond, type;
+
+       for (i = 0; i < ib->length_dw ; i += 2) {
+               reg  = CP_PACKETJ_GET_REG(ib->ptr[i]);
+               res  = CP_PACKETJ_GET_RES(ib->ptr[i]);
+               cond = CP_PACKETJ_GET_COND(ib->ptr[i]);
+               type = CP_PACKETJ_GET_TYPE(ib->ptr[i]);
+
+               if(res) /* only support 0 at the moment */

space between if and (.

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev) · line 1285

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +6 -0
+                       return -EINVAL;
+
+               switch (type) {
+               case PACKETJ_TYPE0:
+                       if (cond != PACKETJ_CONDITION_CHECK0 || reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END) {
+                               DRM_ERROR("Invalid packet [0x%08x]!\n", ib->ptr[i]);

use dev_err() so we can differentiate between multiple GPUs.

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev) · line 1291

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +6 -0
+                               return -EINVAL;
+                       }
+                       break;
+               case PACKETJ_TYPE3:
+                       if (cond != PACKETJ_CONDITION_CHECK3 || reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END) {
+                               DRM_ERROR("Invalid packet [0x%08x]!\n", ib->ptr[i]);

Same here.

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev) · line 1300

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +9 -0
+                               return -EINVAL;
+                       }
+                       break;
+               case PACKETJ_TYPE6:
+                       if (ib->ptr[i] == CP_PACKETJ_NOP)
+                               continue;
+                       return -EINVAL;
+               default:
+                       DRM_ERROR("Unknown packet type %d !\n", type);

And here.

Quoted 61 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +6 -0
+                       return -EINVAL;
+               }
+       }
+
+       return 0;
+}
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h +6 -1
index 747a3e5f6856..71c54b294e15 100644
@@ -46,6 +46,9 @@
 
 #define JRBC_DEC_EXTERNAL_REG_WRITE_ADDR                               0x18000
 
+#define JPEG_REG_RANGE_START                                           0x4000
+#define JPEG_REG_RANGE_END                                             0x41c2
+
 extern const struct amdgpu_ip_block_version jpeg_v4_0_3_ip_block;
 
 void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring,
@@ -62,5 +65,7 @@ void jpeg_v4_0_3_dec_ring_insert_end(struct amdgpu_ring *ring);
 void jpeg_v4_0_3_dec_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
 void jpeg_v4_0_3_dec_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
                                        uint32_t val, uint32_t mask);
-
+int jpeg_v4_0_3_dec_ring_parse_cs(struct amdgpu_cs_parser *parser,
+                                 struct amdgpu_job *job,
+                                 struct amdgpu_ib *ib);
 #endif /* __JPEG_V4_0_3_H__ */
drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c +1 -0
index d694a276498a..f4daff90c770 100644
@@ -646,6 +646,7 @@ static const struct amdgpu_ring_funcs jpeg_v5_0_0_dec_ring_vm_funcs = {
        .get_rptr = jpeg_v5_0_0_dec_ring_get_rptr,
        .get_wptr = jpeg_v5_0_0_dec_ring_get_wptr,
        .set_wptr = jpeg_v5_0_0_dec_ring_set_wptr,
+       .parse_cs = jpeg_v4_0_3_dec_ring_parse_cs,
        .emit_frame_size =
                SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
                SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
drivers/gpu/drm/amd/amdgpu/soc15d.h +6 -1
index 2357ff39323f..e74e1983da53 100644
@@ -76,6 +76,12 @@
                         ((cond & 0xF) << 24) |                         \
                         ((type & 0xF) << 28))
 
+#define CP_PACKETJ_NOP         0x60000000
+#define CP_PACKETJ_GET_REG(x)  ((x) & 0x3FFFF)
+#define CP_PACKETJ_GET_RES(x)  (((x) >> 18) & 0x3F)
+#define CP_PACKETJ_GET_COND(x) (((x) >> 24) & 0xF)
+#define CP_PACKETJ_GET_TYPE(x) (((x) >> 28) & 0xF)
+
 /* Packet 3 types */
 #define        PACKET3_NOP                                     0x10
 #define        PACKET3_SET_BASE                                0x11
--

2.34.1

#5 lore raw
In reply to David (Ming Qiang) Wu
Quoted 55 lines · David (Ming Qiang) Wu

Add JPEG IB command parser to ensure registers
in the command are within the JPEG IP block.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c   |  3 ++
 drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c | 58 +++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h |  7 ++-
 drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c |  1 +
 drivers/gpu/drm/amd/amdgpu/soc15d.h      |  6 +++
 5 files changed, 73 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -0
index 1e167d925b64..78b3c067fea7 100644
@@ -1061,6 +1061,9 @@ static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p,
                        r = amdgpu_ring_parse_cs(ring, p, job, ib);
                        if (r)
                                return r;
+
+                       if (ib->sa_bo)
+                               ib->gpu_addr =  amdgpu_sa_bo_gpu_addr(ib->sa_bo);
                } else {
                        ib->ptr = (uint32_t *)kptr;
                        r = amdgpu_ring_patch_cs_in_place(ring, p, job, ib);
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +6 -1
index ad524ddc9760..6ccf61d7c13d 100644
@@ -782,7 +782,11 @@ void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring,
 
        amdgpu_ring_write(ring, PACKETJ(regUVD_LMI_JRBC_IB_VMID_INTERNAL_OFFSET,
                0, 0, PACKETJ_TYPE0));
-       amdgpu_ring_write(ring, (vmid | (vmid << 4)));
+
+       if (ring->funcs->parse_cs)
+               amdgpu_ring_write(ring, 0);
+       else
+               amdgpu_ring_write(ring, (vmid | (vmid << 4)));
 
        amdgpu_ring_write(ring, PACKETJ(regUVD_LMI_JPEG_VMID_INTERNAL_OFFSET,
                0, 0, PACKETJ_TYPE0));
@@ -1084,6 +1088,7 @@ static const struct amdgpu_ring_funcs jpeg_v4_0_3_dec_ring_vm_funcs = {
        .get_rptr = jpeg_v4_0_3_dec_ring_get_rptr,
        .get_wptr = jpeg_v4_0_3_dec_ring_get_wptr,
        .set_wptr = jpeg_v4_0_3_dec_ring_set_wptr,
+       .parse_cs = jpeg_v4_0_3_dec_ring_parse_cs,
        .emit_frame_size =
                SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
                SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
@@ -1248,3 +1253,54 @@ static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev)
 {
        adev->jpeg.ras = &jpeg_v4_0_3_ras;

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev) · line 1264

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +9 -0
 }
+
+/**
+ * jpeg_v4_0_3_dec_ring_parse_cs - command submission parser
+ *
+ * @parser: Command submission parser context
+ * @job: the job to parse
+ * @ib: the IB to parse
+ *
+ * Parse the command stream, return -EINVAL for invalid packet

Needs a comma at the end of this line.

Alex

Quoted 97 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +42 -0
+ * 0 otherwise
+ */
+int jpeg_v4_0_3_dec_ring_parse_cs(struct amdgpu_cs_parser *parser,
+                            struct amdgpu_job *job,
+                            struct amdgpu_ib *ib)
+{
+       uint32_t i, reg, res, cond, type;
+
+       for (i = 0; i < ib->length_dw ; i += 2) {
+               reg  = CP_PACKETJ_GET_REG(ib->ptr[i]);
+               res  = CP_PACKETJ_GET_RES(ib->ptr[i]);
+               cond = CP_PACKETJ_GET_COND(ib->ptr[i]);
+               type = CP_PACKETJ_GET_TYPE(ib->ptr[i]);
+
+               if(res) /* only support 0 at the moment */
+                       return -EINVAL;
+
+               switch (type) {
+               case PACKETJ_TYPE0:
+                       if (cond != PACKETJ_CONDITION_CHECK0 || reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END) {
+                               DRM_ERROR("Invalid packet [0x%08x]!\n", ib->ptr[i]);
+                               return -EINVAL;
+                       }
+                       break;
+               case PACKETJ_TYPE3:
+                       if (cond != PACKETJ_CONDITION_CHECK3 || reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END) {
+                               DRM_ERROR("Invalid packet [0x%08x]!\n", ib->ptr[i]);
+                               return -EINVAL;
+                       }
+                       break;
+               case PACKETJ_TYPE6:
+                       if (ib->ptr[i] == CP_PACKETJ_NOP)
+                               continue;
+                       return -EINVAL;
+               default:
+                       DRM_ERROR("Unknown packet type %d !\n", type);
+                       return -EINVAL;
+               }
+       }
+
+       return 0;
+}
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h +6 -1
index 747a3e5f6856..71c54b294e15 100644
@@ -46,6 +46,9 @@
 
 #define JRBC_DEC_EXTERNAL_REG_WRITE_ADDR                               0x18000
 
+#define JPEG_REG_RANGE_START                                           0x4000
+#define JPEG_REG_RANGE_END                                             0x41c2
+
 extern const struct amdgpu_ip_block_version jpeg_v4_0_3_ip_block;
 
 void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring,
@@ -62,5 +65,7 @@ void jpeg_v4_0_3_dec_ring_insert_end(struct amdgpu_ring *ring);
 void jpeg_v4_0_3_dec_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
 void jpeg_v4_0_3_dec_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
                                        uint32_t val, uint32_t mask);
-
+int jpeg_v4_0_3_dec_ring_parse_cs(struct amdgpu_cs_parser *parser,
+                                 struct amdgpu_job *job,
+                                 struct amdgpu_ib *ib);
 #endif /* __JPEG_V4_0_3_H__ */
drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c +1 -0
index d694a276498a..f4daff90c770 100644
@@ -646,6 +646,7 @@ static const struct amdgpu_ring_funcs jpeg_v5_0_0_dec_ring_vm_funcs = {
        .get_rptr = jpeg_v5_0_0_dec_ring_get_rptr,
        .get_wptr = jpeg_v5_0_0_dec_ring_get_wptr,
        .set_wptr = jpeg_v5_0_0_dec_ring_set_wptr,
+       .parse_cs = jpeg_v4_0_3_dec_ring_parse_cs,
        .emit_frame_size =
                SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
                SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
drivers/gpu/drm/amd/amdgpu/soc15d.h +6 -1
index 2357ff39323f..e74e1983da53 100644
@@ -76,6 +76,12 @@
                         ((cond & 0xF) << 24) |                         \
                         ((type & 0xF) << 28))
 
+#define CP_PACKETJ_NOP         0x60000000
+#define CP_PACKETJ_GET_REG(x)  ((x) & 0x3FFFF)
+#define CP_PACKETJ_GET_RES(x)  (((x) >> 18) & 0x3F)
+#define CP_PACKETJ_GET_COND(x) (((x) >> 24) & 0xF)
+#define CP_PACKETJ_GET_TYPE(x) (((x) >> 28) & 0xF)
+
 /* Packet 3 types */
 #define        PACKET3_NOP                                     0x10
 #define        PACKET3_SET_BASE                                0x11
--

2.34.1

#6 lore raw

sounds good - will fix them - thanks!

David

Quoted 166 lines · Alex Deucher
Quoted 54 lines · David (Ming Qiang) Wu

Add JPEG IB command parser to ensure registers
in the command are within the JPEG IP block.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c   |  3 ++
  drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c | 58 +++++++++++++++++++++++-
  drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h |  7 ++-
  drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c |  1 +
  drivers/gpu/drm/amd/amdgpu/soc15d.h      |  6 +++
  5 files changed, 73 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -0
index 1e167d925b64..78b3c067fea7 100644
@@ -1061,6 +1061,9 @@ static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p,
                         r = amdgpu_ring_parse_cs(ring, p, job, ib);
                         if (r)
                                 return r;
+
+                       if (ib->sa_bo)
+                               ib->gpu_addr =  amdgpu_sa_bo_gpu_addr(ib->sa_bo);
                 } else {
                         ib->ptr = (uint32_t *)kptr;
                         r = amdgpu_ring_patch_cs_in_place(ring, p, job, ib);
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +6 -1
index ad524ddc9760..6ccf61d7c13d 100644
@@ -782,7 +782,11 @@ void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring,
 
         amdgpu_ring_write(ring, PACKETJ(regUVD_LMI_JRBC_IB_VMID_INTERNAL_OFFSET,
                 0, 0, PACKETJ_TYPE0));
-       amdgpu_ring_write(ring, (vmid | (vmid << 4)));
+
+       if (ring->funcs->parse_cs)
+               amdgpu_ring_write(ring, 0);
+       else
+               amdgpu_ring_write(ring, (vmid | (vmid << 4)));
 
         amdgpu_ring_write(ring, PACKETJ(regUVD_LMI_JPEG_VMID_INTERNAL_OFFSET,
                 0, 0, PACKETJ_TYPE0));
@@ -1084,6 +1088,7 @@ static const struct amdgpu_ring_funcs jpeg_v4_0_3_dec_ring_vm_funcs = {
         .get_rptr = jpeg_v4_0_3_dec_ring_get_rptr,
         .get_wptr = jpeg_v4_0_3_dec_ring_get_wptr,
         .set_wptr = jpeg_v4_0_3_dec_ring_set_wptr,
+       .parse_cs = jpeg_v4_0_3_dec_ring_parse_cs,
         .emit_frame_size =
                 SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
                 SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
@@ -1248,3 +1253,54 @@ static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev)
  {
         adev->jpeg.ras = &jpeg_v4_0_3_ras;

drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev) · line 1264

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +9 -0
  }
+
+/**
+ * jpeg_v4_0_3_dec_ring_parse_cs - command submission parser
+ *
+ * @parser: Command submission parser context
+ * @job: the job to parse
+ * @ib: the IB to parse
+ *
+ * Parse the command stream, return -EINVAL for invalid packet

Needs a comma at the end of this line.

Alex

Quoted 97 lines
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +42 -0
+ * 0 otherwise
+ */
+int jpeg_v4_0_3_dec_ring_parse_cs(struct amdgpu_cs_parser *parser,
+                            struct amdgpu_job *job,
+                            struct amdgpu_ib *ib)
+{
+       uint32_t i, reg, res, cond, type;
+
+       for (i = 0; i < ib->length_dw ; i += 2) {
+               reg  = CP_PACKETJ_GET_REG(ib->ptr[i]);
+               res  = CP_PACKETJ_GET_RES(ib->ptr[i]);
+               cond = CP_PACKETJ_GET_COND(ib->ptr[i]);
+               type = CP_PACKETJ_GET_TYPE(ib->ptr[i]);
+
+               if(res) /* only support 0 at the moment */
+                       return -EINVAL;
+
+               switch (type) {
+               case PACKETJ_TYPE0:
+                       if (cond != PACKETJ_CONDITION_CHECK0 || reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END) {
+                               DRM_ERROR("Invalid packet [0x%08x]!\n", ib->ptr[i]);
+                               return -EINVAL;
+                       }
+                       break;
+               case PACKETJ_TYPE3:
+                       if (cond != PACKETJ_CONDITION_CHECK3 || reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END) {
+                               DRM_ERROR("Invalid packet [0x%08x]!\n", ib->ptr[i]);
+                               return -EINVAL;
+                       }
+                       break;
+               case PACKETJ_TYPE6:
+                       if (ib->ptr[i] == CP_PACKETJ_NOP)
+                               continue;
+                       return -EINVAL;
+               default:
+                       DRM_ERROR("Unknown packet type %d !\n", type);
+                       return -EINVAL;
+               }
+       }
+
+       return 0;
+}
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h +6 -1
index 747a3e5f6856..71c54b294e15 100644
@@ -46,6 +46,9 @@
 
  #define JRBC_DEC_EXTERNAL_REG_WRITE_ADDR                               0x18000
 
+#define JPEG_REG_RANGE_START                                           0x4000
+#define JPEG_REG_RANGE_END                                             0x41c2
+
  extern const struct amdgpu_ip_block_version jpeg_v4_0_3_ip_block;
 
  void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring,
@@ -62,5 +65,7 @@ void jpeg_v4_0_3_dec_ring_insert_end(struct amdgpu_ring *ring);
  void jpeg_v4_0_3_dec_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
  void jpeg_v4_0_3_dec_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
                                         uint32_t val, uint32_t mask);
-
+int jpeg_v4_0_3_dec_ring_parse_cs(struct amdgpu_cs_parser *parser,
+                                 struct amdgpu_job *job,
+                                 struct amdgpu_ib *ib);
  #endif /* __JPEG_V4_0_3_H__ */
drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c +1 -0
index d694a276498a..f4daff90c770 100644
@@ -646,6 +646,7 @@ static const struct amdgpu_ring_funcs jpeg_v5_0_0_dec_ring_vm_funcs = {
         .get_rptr = jpeg_v5_0_0_dec_ring_get_rptr,
         .get_wptr = jpeg_v5_0_0_dec_ring_get_wptr,
         .set_wptr = jpeg_v5_0_0_dec_ring_set_wptr,
+       .parse_cs = jpeg_v4_0_3_dec_ring_parse_cs,
         .emit_frame_size =
                 SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
                 SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
drivers/gpu/drm/amd/amdgpu/soc15d.h +6 -1
index 2357ff39323f..e74e1983da53 100644
@@ -76,6 +76,12 @@
                          ((cond & 0xF) << 24) |                         \
                          ((type & 0xF) << 28))
 
+#define CP_PACKETJ_NOP         0x60000000
+#define CP_PACKETJ_GET_REG(x)  ((x) & 0x3FFFF)
+#define CP_PACKETJ_GET_RES(x)  (((x) >> 18) & 0x3F)
+#define CP_PACKETJ_GET_COND(x) (((x) >> 24) & 0xF)
+#define CP_PACKETJ_GET_TYPE(x) (((x) >> 28) & 0xF)
+
  /* Packet 3 types */
  #define        PACKET3_NOP                                     0x10
  #define        PACKET3_SET_BASE                                0x11
--

2.34.1

maintainer
#7 lore raw
In reply to David (Ming Qiang) Wu

Am 06.08.24 um 18:00 schrieb David (Ming Qiang) Wu:

Quoted 2 lines

Add JPEG IB command parser to ensure registers
in the command are within the JPEG IP block.

You should probably re-order the patches so that the cleanup comes first and then the new functionality.

Apart from that looks good to me.

Regards,
Christian.

Quoted 156 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c   |  3 ++
  drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c | 58 +++++++++++++++++++++++-
  drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h |  7 ++-
  drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c |  1 +
  drivers/gpu/drm/amd/amdgpu/soc15d.h      |  6 +++
  5 files changed, 73 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c +3 -0
index 1e167d925b64..78b3c067fea7 100644
@@ -1061,6 +1061,9 @@ static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p,
  			r = amdgpu_ring_parse_cs(ring, p, job, ib);
  			if (r)
  				return r;
+
+			if (ib->sa_bo)
+				ib->gpu_addr =  amdgpu_sa_bo_gpu_addr(ib->sa_bo);
  		} else {
  			ib->ptr = (uint32_t *)kptr;
  			r = amdgpu_ring_patch_cs_in_place(ring, p, job, ib);
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.c +57 -1
index ad524ddc9760..6ccf61d7c13d 100644
@@ -782,7 +782,11 @@ void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring,
 
  	amdgpu_ring_write(ring, PACKETJ(regUVD_LMI_JRBC_IB_VMID_INTERNAL_OFFSET,
  		0, 0, PACKETJ_TYPE0));
-	amdgpu_ring_write(ring, (vmid | (vmid << 4)));
+
+	if (ring->funcs->parse_cs)
+		amdgpu_ring_write(ring, 0);
+	else
+		amdgpu_ring_write(ring, (vmid | (vmid << 4)));
 
  	amdgpu_ring_write(ring, PACKETJ(regUVD_LMI_JPEG_VMID_INTERNAL_OFFSET,
  		0, 0, PACKETJ_TYPE0));
@@ -1084,6 +1088,7 @@ static const struct amdgpu_ring_funcs jpeg_v4_0_3_dec_ring_vm_funcs = {
  	.get_rptr = jpeg_v4_0_3_dec_ring_get_rptr,
  	.get_wptr = jpeg_v4_0_3_dec_ring_get_wptr,
  	.set_wptr = jpeg_v4_0_3_dec_ring_set_wptr,
+	.parse_cs = jpeg_v4_0_3_dec_ring_parse_cs,
  	.emit_frame_size =
  		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
  		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
@@ -1248,3 +1253,54 @@ static void jpeg_v4_0_3_set_ras_funcs(struct amdgpu_device *adev)
  {
  	adev->jpeg.ras = &jpeg_v4_0_3_ras;
  }
+
+/**
+ * jpeg_v4_0_3_dec_ring_parse_cs - command submission parser
+ *
+ * @parser: Command submission parser context
+ * @job: the job to parse
+ * @ib: the IB to parse
+ *
+ * Parse the command stream, return -EINVAL for invalid packet
+ * 0 otherwise
+ */
+int jpeg_v4_0_3_dec_ring_parse_cs(struct amdgpu_cs_parser *parser,
+			     struct amdgpu_job *job,
+			     struct amdgpu_ib *ib)
+{
+	uint32_t i, reg, res, cond, type;
+
+	for (i = 0; i < ib->length_dw ; i += 2) {
+		reg  = CP_PACKETJ_GET_REG(ib->ptr[i]);
+		res  = CP_PACKETJ_GET_RES(ib->ptr[i]);
+		cond = CP_PACKETJ_GET_COND(ib->ptr[i]);
+		type = CP_PACKETJ_GET_TYPE(ib->ptr[i]);
+
+		if(res) /* only support 0 at the moment */
+			return -EINVAL;
+
+		switch (type) {
+		case PACKETJ_TYPE0:
+			if (cond != PACKETJ_CONDITION_CHECK0 || reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END) {
+				DRM_ERROR("Invalid packet [0x%08x]!\n", ib->ptr[i]);
+				return -EINVAL;
+			}
+			break;
+		case PACKETJ_TYPE3:
+			if (cond != PACKETJ_CONDITION_CHECK3 || reg < JPEG_REG_RANGE_START || reg > JPEG_REG_RANGE_END) {
+				DRM_ERROR("Invalid packet [0x%08x]!\n", ib->ptr[i]);
+				return -EINVAL;
+			}
+			break;
+		case PACKETJ_TYPE6:
+			if (ib->ptr[i] == CP_PACKETJ_NOP)
+				continue;
+			return -EINVAL;
+		default:
+			DRM_ERROR("Unknown packet type %d !\n", type);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}
drivers/gpu/drm/amd/amdgpu/jpeg_v4_0_3.h +6 -1
index 747a3e5f6856..71c54b294e15 100644
@@ -46,6 +46,9 @@
 
  #define JRBC_DEC_EXTERNAL_REG_WRITE_ADDR				0x18000
 
+#define JPEG_REG_RANGE_START						0x4000
+#define JPEG_REG_RANGE_END						0x41c2
+
  extern const struct amdgpu_ip_block_version jpeg_v4_0_3_ip_block;
 
  void jpeg_v4_0_3_dec_ring_emit_ib(struct amdgpu_ring *ring,
@@ -62,5 +65,7 @@ void jpeg_v4_0_3_dec_ring_insert_end(struct amdgpu_ring *ring);
  void jpeg_v4_0_3_dec_ring_emit_wreg(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
  void jpeg_v4_0_3_dec_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
  					uint32_t val, uint32_t mask);
-
+int jpeg_v4_0_3_dec_ring_parse_cs(struct amdgpu_cs_parser *parser,
+				  struct amdgpu_job *job,
+				  struct amdgpu_ib *ib);
  #endif /* __JPEG_V4_0_3_H__ */
drivers/gpu/drm/amd/amdgpu/jpeg_v5_0_0.c +1 -0
index d694a276498a..f4daff90c770 100644
@@ -646,6 +646,7 @@ static const struct amdgpu_ring_funcs jpeg_v5_0_0_dec_ring_vm_funcs = {
  	.get_rptr = jpeg_v5_0_0_dec_ring_get_rptr,
  	.get_wptr = jpeg_v5_0_0_dec_ring_get_wptr,
  	.set_wptr = jpeg_v5_0_0_dec_ring_set_wptr,
+	.parse_cs = jpeg_v4_0_3_dec_ring_parse_cs,
  	.emit_frame_size =
  		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
  		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
drivers/gpu/drm/amd/amdgpu/soc15d.h +6 -0
index 2357ff39323f..e74e1983da53 100644
@@ -76,6 +76,12 @@
  			 ((cond & 0xF) << 24) |				\
  			 ((type & 0xF) << 28))
 
+#define CP_PACKETJ_NOP		0x60000000
+#define CP_PACKETJ_GET_REG(x)  ((x) & 0x3FFFF)
+#define CP_PACKETJ_GET_RES(x)  (((x) >> 18) & 0x3F)
+#define CP_PACKETJ_GET_COND(x) (((x) >> 24) & 0xF)
+#define CP_PACKETJ_GET_TYPE(x) (((x) >> 28) & 0xF)
+
  /* Packet 3 types */
  #define	PACKET3_NOP					0x10
  #define	PACKET3_SET_BASE				0x11