Skip to contentSkip to conversation

More compact IB emission

PATCH In review Tvrtko Ursulin 2 participants 30 messages Sep 18

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

cover More compact IB emission

Alex might remember this one. Good news is that I think I fixed VCN which was the reason we had to back it out last time round. Bad news is that I added GFX8 and GFX9 to the series so maybe I broke something new.

In short, the essence of the series is to replace a pattern of:

       ib->ptr[ib->length_dw++] = bytes - 1;
       ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
       ib->ptr[ib->length_dw++] = lower_32_bits(src);
       ib->ptr[ib->length_dw++] = upper_32_bits(src);
       ib->ptr[ib->length_dw++] = lower_32_bits(pe);
       ib->ptr[ib->length_dw++] = upper_32_bits(pe);

With:

       *ptr++ = bytes - 1;
       *ptr++ = 0; /* src/dst endian swap */
       *ptr++ = lower_32_bits(src);
       *ptr++ = upper_32_bits(src);
       *ptr++ = lower_32_bits(pe);
       *ptr++ = upper_32_bits(pe);
 
       ib->length_dw = ptr - ib->ptr;

As bloat-o-meter below shows, it is a nice compaction win for PTE updates and buffer migrations. Maybe it's not huge but then again a cacheline or two less code to execute, and cache to store it in here and there is not bad. Also important is that I find the source code pattern easier to read but for that I guess it could be a matter of taste.

add/remove: 0/1 grow/shrink: 3/74 up/down: 47/-9705 (-9658)

Function                                     old     new   delta
cik_sdma_ring_pad_ib                         120     151     +31
si_dma_ring_pad_ib                            36      44      +8
sdma_v7_1_ring_pad_ib                        118     126      +8
sdma_v7_0_ring_pad_ib                        118     117      -1
sdma_v6_0_ring_pad_ib                        118     117      -1
sdma_v5_2_ring_pad_ib                        118     117      -1
sdma_v5_0_ring_pad_ib                        118     117      -1
sdma_v4_4_2_ring_pad_ib                      118     117      -1
sdma_v4_0_ring_pad_ib                        118     117      -1
sdma_v2_4_ring_pad_ib                        118     117      -1
sdma_v3_0_ring_pad_ib                        118     115      -3
si_dma_emit_fill_buffer                      108      71     -37
amdgpu_vcn_dec_sw_send_msg                   816     777     -39
si_dma_vm_write_pte                          158     115     -43
si_dma_vm_copy_pte                           137      87     -50
si_dma_emit_copy_buffer                      134      84     -50
sgpr_init_compute_shader                     224     168     -56
sdma_v3_0_vm_write_pte                       163     102     -61
sdma_v2_4_vm_write_pte                       163     102     -61
cik_sdma_vm_write_pte                        163     102     -61
sdma_v7_1_vm_write_pte                       168     105     -63
sdma_v7_1_emit_fill_buffer                   119      56     -63
sdma_v7_0_vm_write_pte                       168     105     -63
sdma_v7_0_emit_fill_buffer                   119      56     -63
sdma_v6_0_vm_write_pte                       168     105     -63
sdma_v6_0_emit_fill_buffer                   119      56     -63
sdma_v5_2_vm_write_pte                       168     105     -63
sdma_v5_2_emit_fill_buffer                   119      56     -63
sdma_v5_0_vm_write_pte                       168     105     -63
sdma_v5_0_emit_fill_buffer                   119      56     -63
sdma_v4_4_2_vm_write_pte                     168     105     -63
sdma_v4_4_2_emit_fill_buffer                 119      56     -63
sdma_v4_0_vm_write_pte                       168     105     -63
sdma_v4_0_emit_fill_buffer                   119      56     -63
sdma_v3_0_emit_fill_buffer                   116      53     -63
sdma_v2_4_emit_fill_buffer                   116      53     -63
cik_sdma_emit_fill_buffer                    116      53     -63
amdgpu_vcn_dec_send_msg                      596     513     -83
sdma_v7_1_emit_copy_buffer                   169      76     -93
sdma_v6_0_emit_copy_buffer                   169      76     -93
sdma_v5_2_emit_copy_buffer                   169      76     -93
sdma_v5_0_emit_copy_buffer                   169      76     -93
sdma_v4_4_2_emit_copy_buffer                 169      76     -93
sdma_v4_0_emit_copy_buffer                   169      76     -93
sdma_v3_0_vm_copy_pte                        158      64     -94
sdma_v3_0_emit_copy_buffer                   155      61     -94
sdma_v2_4_vm_copy_pte                        158      64     -94
sdma_v2_4_emit_copy_buffer                   155      61     -94
cik_sdma_vm_copy_pte                         158      64     -94
cik_sdma_emit_copy_buffer                    155      61     -94
sdma_v7_1_vm_copy_pte                        163      68     -95
sdma_v6_0_vm_copy_pte                        163      68     -95
sdma_v5_2_vm_copy_pte                        163      68     -95
sdma_v5_0_vm_copy_pte                        163      68     -95
sdma_v4_4_2_vm_copy_pte                      163      68     -95
sdma_v4_0_vm_copy_pte                        163      68     -95
sdma_v7_0_vm_copy_pte                        183      75    -108
sdma_v7_0_emit_copy_buffer                   317     202    -115
si_dma_vm_set_pte_pde                        338     214    -124
sdma_v7_1_vm_set_pte_pde                     218      72    -146
sdma_v7_0_vm_set_pte_pde                     218      72    -146
sdma_v6_0_vm_set_pte_pde                     218      72    -146
sdma_v5_2_vm_set_pte_pde                     218      72    -146
sdma_v5_0_vm_set_pte_pde                     218      72    -146
sdma_v4_4_2_vm_set_pte_pde                   218      72    -146
sdma_v4_0_vm_set_pte_pde                     218      72    -146
sdma_v3_0_vm_set_pte_pde                     215      69    -146
sdma_v2_4_vm_set_pte_pde                     215      69    -146
cik_sdma_vm_set_pte_pde                      215      69    -146
amdgpu_vcn_unified_ring_ib_header            172       -    -172
amdgpu_vce_get_destroy_msg                   785     497    -288
gfx_v9_4_2_run_shader.constprop              885     519    -366
amdgpu_vcn_enc_ring_test_ib                 2044    1520    -524
uvd_v6_0_enc_ring_test_ib                   1466     928    -538
amdgpu_vce_ring_test_ib                     1363     822    -541
uvd_v7_0_enc_ring_test_ib                   1466     904    -562
gfx_v8_0_late_init                          2295    1533    -762
gfx_v9_0_late_init                          3443    2612    -831

Total: Before=10682414, After=10672756, chg -0.09%

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

Tvrtko Ursulin (18):
drm/amdgpu: More compact VCN IB emission
drm/amdgpu: More compact VCE IB emission
drm/amdgpu: More compact UVD 6 IB emission
drm/amdgpu: More compact UVD 7 IB emission
drm/amdgpu: More compact SI SDMA emission
drm/amdgpu: More compact CIK SDMA IB emission
drm/amdgpu: More compact GFX8 IB emission
drm/amdgpu: More compact GFX 9.4.2 IB emission
drm/amdgpu: More compact GFX9 IB emission
drm/amdgpu: More compact SDMA 2.4 IB emission
drm/amdgpu: More compact SDMA 3.0 IB emission
drm/amdgpu: More compact SDMA 4.0 IB emission
drm/amdgpu: More compact SDMA 4.4.2 IB emission
drm/amdgpu: More compact SDMA 5.0 IB emission
drm/amdgpu: More compact SDMA 5.2 IB emission
drm/amdgpu: More compact SDMA 6.0 IB emission
drm/amdgpu: More compact SDMA 7.0 IB emission
drm/amdgpu: More compact SDMA 7.1 IB emission

 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c  |  96 ++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c  | 134 +++++++++++++----------
 drivers/gpu/drm/amd/amdgpu/cik_sdma.c    | 105 +++++++++++-------
 drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c    | 106 +++++++++---------
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c    | 115 +++++++++----------
 drivers/gpu/drm/amd/amdgpu/gfx_v9_4_2.c  |  51 ++++-----
 drivers/gpu/drm/amd/amdgpu/sdma_v2_4.c   | 108 ++++++++++--------
 drivers/gpu/drm/amd/amdgpu/sdma_v3_0.c   | 108 ++++++++++--------
 drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c   | 109 ++++++++++--------
 drivers/gpu/drm/amd/amdgpu/sdma_v4_4_2.c | 108 ++++++++++--------
 drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c   | 106 +++++++++++-------
 drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c   | 110 +++++++++++--------
 drivers/gpu/drm/amd/amdgpu/sdma_v6_0.c   | 110 +++++++++++--------
 drivers/gpu/drm/amd/amdgpu/sdma_v7_0.c   | 119 +++++++++++---------
 drivers/gpu/drm/amd/amdgpu/sdma_v7_1.c   | 102 ++++++++++-------
 drivers/gpu/drm/amd/amdgpu/si_dma.c      |  84 ++++++++------
 drivers/gpu/drm/amd/amdgpu/uvd_v6_0.c    |  72 ++++++------
 drivers/gpu/drm/amd/amdgpu/uvd_v7_0.c    |  72 ++++++------
 18 files changed, 1038 insertions(+), 777 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/18 drm/amdgpu: More compact VCN IB emission

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
* Fix decode buffer overwrite.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 134 +++++++++++++-----------
 1 file changed, 75 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +75 -59
index 17db7264269e..2c367ee6e182 100644
@@ -622,9 +622,12 @@ static int amdgpu_vcn_dec_send_msg(struct amdgpu_ring *ring,
 {
 	u64 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
 	struct amdgpu_device *adev = ring->adev;
+	struct amdgpu_vcn_reg *internal = &adev->vcn.inst[ring->me].internal;
+	uint32_t nop = PACKET0(internal->nop, 0);
 	struct dma_fence *f = NULL;
 	struct amdgpu_job *job;
 	struct amdgpu_ib *ib;
+	uint32_t *ptr;
 	int i, r;
 
 	r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL,
@@ -635,15 +638,16 @@ static int amdgpu_vcn_dec_send_msg(struct amdgpu_ring *ring,
 		goto err;
 
 	ib = &job->ibs[0];
-	ib->ptr[0] = PACKET0(adev->vcn.inst[ring->me].internal.data0, 0);
-	ib->ptr[1] = addr;
-	ib->ptr[2] = PACKET0(adev->vcn.inst[ring->me].internal.data1, 0);
-	ib->ptr[3] = addr >> 32;
-	ib->ptr[4] = PACKET0(adev->vcn.inst[ring->me].internal.cmd, 0);
-	ib->ptr[5] = 0;
+	ptr = ib->ptr;
+	*ptr++ = PACKET0(internal->data0, 0);
+	*ptr++ = addr;
+	*ptr++ = PACKET0(internal->data1, 0);
+	*ptr++ = addr >> 32;
+	*ptr++ = PACKET0(internal->cmd, 0);
+	*ptr++ = 0;
 	for (i = 6; i < 16; i += 2) {
-		ib->ptr[i] = PACKET0(adev->vcn.inst[ring->me].internal.nop, 0);
-		ib->ptr[i+1] = 0;
+		*ptr++ = nop;
+		*ptr++ = 0;
 	}
 	ib->length_dw = 16;
 
@@ -763,17 +767,20 @@ int amdgpu_vcn_dec_ring_test_ib(struct amdgpu_ring *ring, long timeout)
 static uint32_t *amdgpu_vcn_unified_ring_ib_header(struct amdgpu_ib *ib,
 						uint32_t ib_pack_in_dw, bool enc)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	uint32_t *ib_checksum;
 
-	ib->ptr[ib->length_dw++] = 0x00000010; /* single queue checksum */
-	ib->ptr[ib->length_dw++] = 0x30000002;
-	ib_checksum = &ib->ptr[ib->length_dw++];
-	ib->ptr[ib->length_dw++] = ib_pack_in_dw;
+	*ptr++ = 0x00000010; /* single queue checksum */
+	*ptr++ = 0x30000002;
+	ib_checksum = ptr++;
+	*ptr++ = ib_pack_in_dw;
 
-	ib->ptr[ib->length_dw++] = 0x00000010; /* engine info */
-	ib->ptr[ib->length_dw++] = 0x30000001;
-	ib->ptr[ib->length_dw++] = enc ? 0x2 : 0x3;
-	ib->ptr[ib->length_dw++] = ib_pack_in_dw * sizeof(uint32_t);
+	*ptr++ = 0x00000010; /* engine info */
+	*ptr++ = 0x30000001;
+	*ptr++ = enc ? 0x2 : 0x3;
+	*ptr++ = ib_pack_in_dw * sizeof(uint32_t);
+
+	ib->length_dw = ptr - ib->ptr;
 
 	return ib_checksum;
 }
@@ -803,7 +810,8 @@ static int amdgpu_vcn_dec_sw_send_msg(struct amdgpu_ring *ring,
 	uint64_t addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
 	uint32_t *ib_checksum;
 	uint32_t ib_pack_in_dw;
-	int i, r;
+	u32 *ptr;
+	int r;
 
 	if (adev->vcn.inst[ring->me].using_unified_queue)
 		ib_size_dw += 8;
@@ -825,18 +833,20 @@ static int amdgpu_vcn_dec_sw_send_msg(struct amdgpu_ring *ring,
 		ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, ib_pack_in_dw, false);
 	}
 
-	ib->ptr[ib->length_dw++] = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
-	ib->ptr[ib->length_dw++] = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
-	decode_buffer = (struct amdgpu_vcn_decode_buffer *)&(ib->ptr[ib->length_dw]);
-	ib->length_dw += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+	ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
+	*ptr++ = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
+	decode_buffer = (struct amdgpu_vcn_decode_buffer *)ptr;
+	ptr += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+	ib->length_dw = ptr - ib->ptr;
 	memset(decode_buffer, 0, sizeof(struct amdgpu_vcn_decode_buffer));
 
 	decode_buffer->valid_buf_flag |= cpu_to_le32(AMDGPU_VCN_CMD_FLAG_MSG_BUFFER);
 	decode_buffer->msg_buffer_address_hi = cpu_to_le32(addr >> 32);
 	decode_buffer->msg_buffer_address_lo = cpu_to_le32(addr);
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
 	if (adev->vcn.inst[ring->me].using_unified_queue)
 		amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, ib_pack_in_dw);
@@ -934,7 +944,8 @@ static int amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand
 	struct dma_fence *f = NULL;
 	uint32_t *ib_checksum = NULL;
 	uint64_t addr;
-	int i, r;
+	u32 *ptr;
+	int r;
 
 	if (adev->vcn.inst[ring->me].using_unified_queue)
 		ib_size_dw += 8;
@@ -947,31 +958,33 @@ static int amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand
 		return r;
 
 	ib = &job->ibs[0];
-	addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
 	ib->length_dw = 0;
+	addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
 
 	if (adev->vcn.inst[ring->me].using_unified_queue)
 		ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true);
 
-	ib->ptr[ib->length_dw++] = 0x00000018;
-	ib->ptr[ib->length_dw++] = 0x00000001; /* session info */
-	ib->ptr[ib->length_dw++] = handle;
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = addr;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	ptr = &ib->ptr[ib->length_dw];
 
-	ib->ptr[ib->length_dw++] = 0x00000014;
-	ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-	ib->ptr[ib->length_dw++] = 0x0000001c;
-	ib->ptr[ib->length_dw++] = 0x00000000;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	*ptr++ = 0x00000018;
+	*ptr++ = 0x00000001; /* session info */
+	*ptr++ = handle;
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = addr;
+	*ptr++ = 0x00000000;
 
-	ib->ptr[ib->length_dw++] = 0x00000008;
-	ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */
+	*ptr++ = 0x00000014;
+	*ptr++ = 0x00000002; /* task info */
+	*ptr++ = 0x0000001c;
+	*ptr++ = 0x00000000;
+	*ptr++ = 0x00000000;
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	*ptr++ = 0x00000008;
+	*ptr++ = 0x08000001; /* op initialize */
+
+	ib->length_dw = ptr - ib->ptr;
+
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
 	if (adev->vcn.inst[ring->me].using_unified_queue)
 		amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);
@@ -1002,7 +1015,8 @@ static int amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han
 	struct dma_fence *f = NULL;
 	uint32_t *ib_checksum = NULL;
 	uint64_t addr;
-	int i, r;
+	u32 *ptr;
+	int r;
 
 	if (adev->vcn.inst[ring->me].using_unified_queue)
 		ib_size_dw += 8;
@@ -1015,31 +1029,33 @@ static int amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han
 		return r;
 
 	ib = &job->ibs[0];
-	addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
 	ib->length_dw = 0;
+	addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
 
 	if (adev->vcn.inst[ring->me].using_unified_queue)
 		ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true);
 
-	ib->ptr[ib->length_dw++] = 0x00000018;
-	ib->ptr[ib->length_dw++] = 0x00000001;
-	ib->ptr[ib->length_dw++] = handle;
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = addr;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	ptr = &ib->ptr[ib->length_dw];
 
-	ib->ptr[ib->length_dw++] = 0x00000014;
-	ib->ptr[ib->length_dw++] = 0x00000002;
-	ib->ptr[ib->length_dw++] = 0x0000001c;
-	ib->ptr[ib->length_dw++] = 0x00000000;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	*ptr++ = 0x00000018;
+	*ptr++ = 0x00000001;
+	*ptr++ = handle;
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = addr;
+	*ptr++ = 0x00000000;
 
-	ib->ptr[ib->length_dw++] = 0x00000008;
-	ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */
+	*ptr++ = 0x00000014;
+	*ptr++ = 0x00000002;
+	*ptr++ = 0x0000001c;
+	*ptr++ = 0x00000000;
+	*ptr++ = 0x00000000;
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	*ptr++ = 0x00000008;
+	*ptr++ = 0x08000002; /* op close session */
+
+	ib->length_dw = ptr - ib->ptr;
+
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
 	if (adev->vcn.inst[ring->me].using_unified_queue)
 		amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 01/18] drm/amdgpu: More compact VCN IB emission

Quoted 9 lines · Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
* Fix decode buffer overwrite

Overall for this series, below type of calculation -

	ib->length_dw = ptr - ib->ptr;

is not anticipating a case like below.

        r = amdgpu_vcn_enc_get_create_msg(ring, 1, &ib, NULL);
        if (r)
                goto error;

        r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &ib, &fence);
        if (r)
                goto error;

Thanks,
Lijo.

Quoted 234 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 134 +++++++++++++-----------
  1 file changed, 75 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +75 -59
index 17db7264269e..2c367ee6e182 100644
@@ -622,9 +622,12 @@ static int amdgpu_vcn_dec_send_msg(struct amdgpu_ring *ring,
  {
  	u64 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
  	struct amdgpu_device *adev = ring->adev;
+	struct amdgpu_vcn_reg *internal = &adev->vcn.inst[ring->me].internal;
+	uint32_t nop = PACKET0(internal->nop, 0);
  	struct dma_fence *f = NULL;
  	struct amdgpu_job *job;
  	struct amdgpu_ib *ib;
+	uint32_t *ptr;
  	int i, r;
 
  	r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL,
@@ -635,15 +638,16 @@ static int amdgpu_vcn_dec_send_msg(struct amdgpu_ring *ring,
  		goto err;
 
  	ib = &job->ibs[0];
-	ib->ptr[0] = PACKET0(adev->vcn.inst[ring->me].internal.data0, 0);
-	ib->ptr[1] = addr;
-	ib->ptr[2] = PACKET0(adev->vcn.inst[ring->me].internal.data1, 0);
-	ib->ptr[3] = addr >> 32;
-	ib->ptr[4] = PACKET0(adev->vcn.inst[ring->me].internal.cmd, 0);
-	ib->ptr[5] = 0;
+	ptr = ib->ptr;
+	*ptr++ = PACKET0(internal->data0, 0);
+	*ptr++ = addr;
+	*ptr++ = PACKET0(internal->data1, 0);
+	*ptr++ = addr >> 32;
+	*ptr++ = PACKET0(internal->cmd, 0);
+	*ptr++ = 0;
  	for (i = 6; i < 16; i += 2) {
-		ib->ptr[i] = PACKET0(adev->vcn.inst[ring->me].internal.nop, 0);
-		ib->ptr[i+1] = 0;
+		*ptr++ = nop;
+		*ptr++ = 0;
  	}
  	ib->length_dw = 16;
 
@@ -763,17 +767,20 @@ int amdgpu_vcn_dec_ring_test_ib(struct amdgpu_ring *ring, long timeout)
  static uint32_t *amdgpu_vcn_unified_ring_ib_header(struct amdgpu_ib *ib,
  						uint32_t ib_pack_in_dw, bool enc)
  {
+	u32 *ptr = &ib->ptr[ib->length_dw];
  	uint32_t *ib_checksum;
 
-	ib->ptr[ib->length_dw++] = 0x00000010; /* single queue checksum */
-	ib->ptr[ib->length_dw++] = 0x30000002;
-	ib_checksum = &ib->ptr[ib->length_dw++];
-	ib->ptr[ib->length_dw++] = ib_pack_in_dw;
+	*ptr++ = 0x00000010; /* single queue checksum */
+	*ptr++ = 0x30000002;
+	ib_checksum = ptr++;
+	*ptr++ = ib_pack_in_dw;
 
-	ib->ptr[ib->length_dw++] = 0x00000010; /* engine info */
-	ib->ptr[ib->length_dw++] = 0x30000001;
-	ib->ptr[ib->length_dw++] = enc ? 0x2 : 0x3;
-	ib->ptr[ib->length_dw++] = ib_pack_in_dw * sizeof(uint32_t);
+	*ptr++ = 0x00000010; /* engine info */
+	*ptr++ = 0x30000001;
+	*ptr++ = enc ? 0x2 : 0x3;
+	*ptr++ = ib_pack_in_dw * sizeof(uint32_t);
+
+	ib->length_dw = ptr - ib->ptr;
 
  	return ib_checksum;
  }
@@ -803,7 +810,8 @@ static int amdgpu_vcn_dec_sw_send_msg(struct amdgpu_ring *ring,
  	uint64_t addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
  	uint32_t *ib_checksum;
  	uint32_t ib_pack_in_dw;
-	int i, r;
+	u32 *ptr;
+	int r;
 
  	if (adev->vcn.inst[ring->me].using_unified_queue)
  		ib_size_dw += 8;
@@ -825,18 +833,20 @@ static int amdgpu_vcn_dec_sw_send_msg(struct amdgpu_ring *ring,
  		ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, ib_pack_in_dw, false);
  	}
 
-	ib->ptr[ib->length_dw++] = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
-	ib->ptr[ib->length_dw++] = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
-	decode_buffer = (struct amdgpu_vcn_decode_buffer *)&(ib->ptr[ib->length_dw]);
-	ib->length_dw += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+	ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
+	*ptr++ = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
+	decode_buffer = (struct amdgpu_vcn_decode_buffer *)ptr;
+	ptr += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+	ib->length_dw = ptr - ib->ptr;
  	memset(decode_buffer, 0, sizeof(struct amdgpu_vcn_decode_buffer));
 
  	decode_buffer->valid_buf_flag |= cpu_to_le32(AMDGPU_VCN_CMD_FLAG_MSG_BUFFER);
  	decode_buffer->msg_buffer_address_hi = cpu_to_le32(addr >> 32);
  	decode_buffer->msg_buffer_address_lo = cpu_to_le32(addr);
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
  	if (adev->vcn.inst[ring->me].using_unified_queue)
  		amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, ib_pack_in_dw);
@@ -934,7 +944,8 @@ static int amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand
  	struct dma_fence *f = NULL;
  	uint32_t *ib_checksum = NULL;
  	uint64_t addr;
-	int i, r;
+	u32 *ptr;
+	int r;
 
  	if (adev->vcn.inst[ring->me].using_unified_queue)
  		ib_size_dw += 8;
@@ -947,31 +958,33 @@ static int amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand
  		return r;
 
  	ib = &job->ibs[0];
-	addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
  	ib->length_dw = 0;
+	addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
 
  	if (adev->vcn.inst[ring->me].using_unified_queue)
  		ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true);
 
-	ib->ptr[ib->length_dw++] = 0x00000018;
-	ib->ptr[ib->length_dw++] = 0x00000001; /* session info */
-	ib->ptr[ib->length_dw++] = handle;
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = addr;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	ptr = &ib->ptr[ib->length_dw];
 
-	ib->ptr[ib->length_dw++] = 0x00000014;
-	ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-	ib->ptr[ib->length_dw++] = 0x0000001c;
-	ib->ptr[ib->length_dw++] = 0x00000000;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	*ptr++ = 0x00000018;
+	*ptr++ = 0x00000001; /* session info */
+	*ptr++ = handle;
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = addr;
+	*ptr++ = 0x00000000;
 
-	ib->ptr[ib->length_dw++] = 0x00000008;
-	ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */
+	*ptr++ = 0x00000014;
+	*ptr++ = 0x00000002; /* task info */
+	*ptr++ = 0x0000001c;
+	*ptr++ = 0x00000000;
+	*ptr++ = 0x00000000;
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	*ptr++ = 0x00000008;
+	*ptr++ = 0x08000001; /* op initialize */
+
+	ib->length_dw = ptr - ib->ptr;
+
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
  	if (adev->vcn.inst[ring->me].using_unified_queue)
  		amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);
@@ -1002,7 +1015,8 @@ static int amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han
  	struct dma_fence *f = NULL;
  	uint32_t *ib_checksum = NULL;
  	uint64_t addr;
-	int i, r;
+	u32 *ptr;
+	int r;
 
  	if (adev->vcn.inst[ring->me].using_unified_queue)
  		ib_size_dw += 8;
@@ -1015,31 +1029,33 @@ static int amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han
  		return r;
 
  	ib = &job->ibs[0];
-	addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
  	ib->length_dw = 0;
+	addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
 
  	if (adev->vcn.inst[ring->me].using_unified_queue)
  		ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true);
 
-	ib->ptr[ib->length_dw++] = 0x00000018;
-	ib->ptr[ib->length_dw++] = 0x00000001;
-	ib->ptr[ib->length_dw++] = handle;
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = addr;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	ptr = &ib->ptr[ib->length_dw];
 
-	ib->ptr[ib->length_dw++] = 0x00000014;
-	ib->ptr[ib->length_dw++] = 0x00000002;
-	ib->ptr[ib->length_dw++] = 0x0000001c;
-	ib->ptr[ib->length_dw++] = 0x00000000;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	*ptr++ = 0x00000018;
+	*ptr++ = 0x00000001;
+	*ptr++ = handle;
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = addr;
+	*ptr++ = 0x00000000;
 
-	ib->ptr[ib->length_dw++] = 0x00000008;
-	ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */
+	*ptr++ = 0x00000014;
+	*ptr++ = 0x00000002;
+	*ptr++ = 0x0000001c;
+	*ptr++ = 0x00000000;
+	*ptr++ = 0x00000000;
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	*ptr++ = 0x00000008;
+	*ptr++ = 0x08000002; /* op close session */
+
+	ib->length_dw = ptr - ib->ptr;
+
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
  	if (adev->vcn.inst[ring->me].using_unified_queue)
  		amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);
#3 lore raw

Re: [PATCH 01/18] drm/amdgpu: More compact VCN IB emission

Quoted 19 lines · Lijo
Quoted 9 lines · Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
  *  Fix decode buffer overwrite

Overall for this series, below type of calculation  -

    ib->length_dw = ptr - ib->ptr;

is not anticipating a case like below.

Quoted 7 lines

        r = amdgpu_vcn_enc_get_create_msg(ring, 1, &ib, NULL); if (r)
                goto error;

        r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &ib, &fence); if (r)
                goto error;

So amdgpu_vcn_enc_ring_test_ib() - are you saying there is a bug there? Help me see it please.

ib->length_dw update is enclosed in both of those helpers, same as before the patch.

Regards,

Tvrtko

Quoted 221 lines
Quoted 220 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 134 +++++++++++++-----------

  1 file changed, 75 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3 -0
index 17db7264269e..2c367ee6e182 100644
@@ -622,9 +622,12 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
   {
       u64 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       struct amdgpu_device *adev = ring->adev;
+    struct amdgpu_vcn_reg *internal = &adev->vcn.inst[ring- 
 >me].internal;
+    uint32_t nop = PACKET0(internal->nop, 0);
       struct dma_fence *f = NULL;
       struct amdgpu_job *job;
       struct amdgpu_ib *ib;
+    uint32_t *ptr;
       int i, r;

      r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL,

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +30 -23
@@ -635,15 +638,16 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
           goto err;
       ib = &job->ibs[0];
-    ib->ptr[0] = PACKET0(adev->vcn.inst[ring->me].internal.data0, 0);
-    ib->ptr[1] = addr;
-    ib->ptr[2] = PACKET0(adev->vcn.inst[ring->me].internal.data1, 0);
-    ib->ptr[3] = addr >> 32;
-    ib->ptr[4] = PACKET0(adev->vcn.inst[ring->me].internal.cmd, 0);
-    ib->ptr[5] = 0;
+    ptr = ib->ptr;
+    *ptr++ = PACKET0(internal->data0, 0);
+    *ptr++ = addr;
+    *ptr++ = PACKET0(internal->data1, 0);
+    *ptr++ = addr >> 32;
+    *ptr++ = PACKET0(internal->cmd, 0);
+    *ptr++ = 0;
       for (i = 6; i < 16; i += 2) {
-        ib->ptr[i] = PACKET0(adev->vcn.inst[ring->me].internal.nop, 0);
-        ib->ptr[i+1] = 0;
+        *ptr++ = nop;
+        *ptr++ = 0;
       }
       ib->length_dw = 16;
@@ -763,17 +767,20 @@ int amdgpu_vcn_dec_ring_test_ib(struct 
 amdgpu_ring *ring, long timeout)
   static uint32_t *amdgpu_vcn_unified_ring_ib_header(struct amdgpu_ib *ib,
                           uint32_t ib_pack_in_dw, bool enc)
   {
+    u32 *ptr = &ib->ptr[ib->length_dw];
       uint32_t *ib_checksum;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* single queue checksum */
-    ib->ptr[ib->length_dw++] = 0x30000002;
-    ib_checksum = &ib->ptr[ib->length_dw++];
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw;
+    *ptr++ = 0x00000010; /* single queue checksum */
+    *ptr++ = 0x30000002;
+    ib_checksum = ptr++;
+    *ptr++ = ib_pack_in_dw;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* engine info */
-    ib->ptr[ib->length_dw++] = 0x30000001;
-    ib->ptr[ib->length_dw++] = enc ? 0x2 : 0x3;
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw * sizeof(uint32_t);
+    *ptr++ = 0x00000010; /* engine info */
+    *ptr++ = 0x30000001;
+    *ptr++ = enc ? 0x2 : 0x3;
+    *ptr++ = ib_pack_in_dw * sizeof(uint32_t);
+
+    ib->length_dw = ptr - ib->ptr;
       return ib_checksum;
   }
@@ -803,7 +810,8 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
       uint64_t addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       uint32_t *ib_checksum;
       uint32_t ib_pack_in_dw;
-    int i, r;
+    u32 *ptr;
+    int r;
       if (adev->vcn.inst[ring->me].using_unified_queue)
           ib_size_dw += 8;
@@ -825,18 +833,20 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
           ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, ib_pack_in_dw, false);
       }
-    ib->ptr[ib->length_dw++] = sizeof(struct 
 amdgpu_vcn_decode_buffer) + 8;
-    ib->ptr[ib->length_dw++] = 
 cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
-    decode_buffer = (struct amdgpu_vcn_decode_buffer *)&(ib->ptr[ib- 
 >length_dw]);
-    ib->length_dw += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ptr = &ib->ptr[ib->length_dw];
+
+    *ptr++ = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
+    *ptr++ = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
+    decode_buffer = (struct amdgpu_vcn_decode_buffer *)ptr;
+    ptr += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ib->length_dw = ptr - ib->ptr;
       memset(decode_buffer, 0, sizeof(struct amdgpu_vcn_decode_buffer));
       decode_buffer->valid_buf_flag |= cpu_to_le32(AMDGPU_VCN_CMD_FLAG_MSG_BUFFER);
       decode_buffer->msg_buffer_address_hi = cpu_to_le32(addr >> 32);
       decode_buffer->msg_buffer_address_lo = cpu_to_le32(addr);
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);
       if (adev->vcn.inst[ring->me].using_unified_queue)
           amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, ib_pack_in_dw);
@@ -934,7 +944,8 @@ static int amdgpu_vcn_enc_get_create_msg(struct 

amdgpu_ring *ring, uint32_t hand
      struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -947,31 +958,33 @@ static int amdgpu_vcn_enc_get_create_msg(struct 

amdgpu_ring *ring, uint32_t hand
          return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001; /* session info */ -    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001; /* session info */
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002; /* task info */
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000001; /* op initialize */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1002,7 +1015,8 @@ static int amdgpu_vcn_enc_get_destroy_msg(struct 

amdgpu_ring *ring, uint32_t han
      struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1015,31 +1029,33 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001;
-    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002;
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001;
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002;
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000002; /* op close session */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

#4 lore raw

Re: [PATCH 01/18] drm/amdgpu: More compact VCN IB emission

Quoted 25 lines · Tvrtko Ursulin
Quoted 23 lines · Lijo
Quoted 9 lines · Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
  *  Fix decode buffer overwrite

Overall for this series, below type of calculation  -

     ib->length_dw = ptr - ib->ptr;

is not anticipating a case like below.

         r = amdgpu_vcn_enc_get_create_msg(ring, 1, &ib, NULL); if (r)
                 goto error;

Quoted 8 lines
Quoted 3 lines

         r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &ib, &fence); if (r)
                 goto error;

So amdgpu_vcn_enc_ring_test_ib() - are you saying there is a bug there? Help me see it please.

ib->length_dw update is enclosed in both of those helpers, same as before the patch.

What I'm saying is -

It's the same ib pointer that is passed to back to back calls.

Legacy implementation fills like -

ib->ptr[ib->length_dw++]

So no assumption that it is starting from offset 0 always. After the function is filled, length gets updated based on what is filled.

I don't see the same with an assignment like

	ib->length_dw = ptr - ib->ptr;

Thanks,
Lijo

Quoted 226 lines

Regards,

Tvrtko

Quoted 221 lines
Quoted 220 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 134 +++++++++++++-----------

  1 file changed, 75 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3 -0
index 17db7264269e..2c367ee6e182 100644
@@ -622,9 +622,12 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
   {
       u64 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       struct amdgpu_device *adev = ring->adev;
+    struct amdgpu_vcn_reg *internal = &adev->vcn.inst[ring- 
 >me].internal;
+    uint32_t nop = PACKET0(internal->nop, 0);
       struct dma_fence *f = NULL;
       struct amdgpu_job *job;
       struct amdgpu_ib *ib;
+    uint32_t *ptr;
       int i, r;

      r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL,

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +30 -23
@@ -635,15 +638,16 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
           goto err;
       ib = &job->ibs[0];
-    ib->ptr[0] = PACKET0(adev->vcn.inst[ring->me].internal.data0, 0);
-    ib->ptr[1] = addr;
-    ib->ptr[2] = PACKET0(adev->vcn.inst[ring->me].internal.data1, 0);
-    ib->ptr[3] = addr >> 32;
-    ib->ptr[4] = PACKET0(adev->vcn.inst[ring->me].internal.cmd, 0);
-    ib->ptr[5] = 0;
+    ptr = ib->ptr;
+    *ptr++ = PACKET0(internal->data0, 0);
+    *ptr++ = addr;
+    *ptr++ = PACKET0(internal->data1, 0);
+    *ptr++ = addr >> 32;
+    *ptr++ = PACKET0(internal->cmd, 0);
+    *ptr++ = 0;
       for (i = 6; i < 16; i += 2) {
-        ib->ptr[i] = PACKET0(adev->vcn.inst[ring->me].internal.nop, 0);
-        ib->ptr[i+1] = 0;
+        *ptr++ = nop;
+        *ptr++ = 0;
       }
       ib->length_dw = 16;
@@ -763,17 +767,20 @@ int amdgpu_vcn_dec_ring_test_ib(struct 
 amdgpu_ring *ring, long timeout)
   static uint32_t *amdgpu_vcn_unified_ring_ib_header(struct amdgpu_ib *ib,
                           uint32_t ib_pack_in_dw, bool enc)
   {
+    u32 *ptr = &ib->ptr[ib->length_dw];
       uint32_t *ib_checksum;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* single queue checksum */
-    ib->ptr[ib->length_dw++] = 0x30000002;
-    ib_checksum = &ib->ptr[ib->length_dw++];
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw;
+    *ptr++ = 0x00000010; /* single queue checksum */
+    *ptr++ = 0x30000002;
+    ib_checksum = ptr++;
+    *ptr++ = ib_pack_in_dw;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* engine info */
-    ib->ptr[ib->length_dw++] = 0x30000001;
-    ib->ptr[ib->length_dw++] = enc ? 0x2 : 0x3;
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw * sizeof(uint32_t);
+    *ptr++ = 0x00000010; /* engine info */
+    *ptr++ = 0x30000001;
+    *ptr++ = enc ? 0x2 : 0x3;
+    *ptr++ = ib_pack_in_dw * sizeof(uint32_t);
+
+    ib->length_dw = ptr - ib->ptr;
       return ib_checksum;
   }
@@ -803,7 +810,8 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
       uint64_t addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       uint32_t *ib_checksum;
       uint32_t ib_pack_in_dw;
-    int i, r;
+    u32 *ptr;
+    int r;
       if (adev->vcn.inst[ring->me].using_unified_queue)
           ib_size_dw += 8;
@@ -825,18 +833,20 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
           ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, ib_pack_in_dw, false);
       }
-    ib->ptr[ib->length_dw++] = sizeof(struct 
 amdgpu_vcn_decode_buffer) + 8;
-    ib->ptr[ib->length_dw++] = 
 cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
-    decode_buffer = (struct amdgpu_vcn_decode_buffer *)&(ib->ptr[ib- 
 >length_dw]);
-    ib->length_dw += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ptr = &ib->ptr[ib->length_dw];
+
+    *ptr++ = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
+    *ptr++ = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
+    decode_buffer = (struct amdgpu_vcn_decode_buffer *)ptr;
+    ptr += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ib->length_dw = ptr - ib->ptr;
       memset(decode_buffer, 0, sizeof(struct amdgpu_vcn_decode_buffer));
       decode_buffer->valid_buf_flag |= cpu_to_le32(AMDGPU_VCN_CMD_FLAG_MSG_BUFFER);
       decode_buffer->msg_buffer_address_hi = cpu_to_le32(addr >> 32);
       decode_buffer->msg_buffer_address_lo = cpu_to_le32(addr);
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);
       if (adev->vcn.inst[ring->me].using_unified_queue)
           amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, ib_pack_in_dw);
@@ -934,7 +944,8 @@ static int amdgpu_vcn_enc_get_create_msg(struct 

amdgpu_ring *ring, uint32_t hand
      struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -947,31 +958,33 @@ static int amdgpu_vcn_enc_get_create_msg(struct 

amdgpu_ring *ring, uint32_t hand
          return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001; /* session info */ -    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001; /* session info */
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002; /* task info */
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000001; /* op initialize */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1002,7 +1015,8 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1015,31 +1029,33 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001;
-    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002;
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001;
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002;
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000002; /* op close session */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

#5 lore raw

Re: [PATCH 01/18] drm/amdgpu: More compact VCN IB emission

Quoted 41 lines · Lijo
Quoted 25 lines · Tvrtko Ursulin
Quoted 23 lines · Lijo
Quoted 9 lines · Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
  *  Fix decode buffer overwrite

Overall for this series, below type of calculation  -

     ib->length_dw = ptr - ib->ptr;

is not anticipating a case like below.

         r = amdgpu_vcn_enc_get_create_msg(ring, 1, &ib, NULL); if (r)
                 goto error;

Quoted 8 lines
Quoted 3 lines

         r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &ib, &fence); if (r)
                 goto error;

So amdgpu_vcn_enc_ring_test_ib() - are you saying there is a bug there? Help me see it please.

ib->length_dw update is enclosed in both of those helpers, same as before the patch.

What I'm saying is -

It's the same ib pointer that is passed to back to back calls.

Legacy implementation fills like -

Quoted 7 lines

ib->ptr[ib->length_dw++]

So no assumption that it is starting from offset 0 always. After the function is filled, length gets updated based on what is filled.

I don't see the same with an assignment like

    ib->length_dw = ptr - ib->ptr;

Hm, but both of those helpers emit separate jobs with their own respective IB.

Regards,

Tvrtko

Quoted 225 lines
Quoted 224 lines
Quoted 223 lines
Quoted 222 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 134 ++++++++++++
+-----------

  1 file changed, 75 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3 -0
index 17db7264269e..2c367ee6e182 100644
@@ -622,9 +622,12 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
   {
       u64 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       struct amdgpu_device *adev = ring->adev;
+    struct amdgpu_vcn_reg *internal = &adev->vcn.inst[ring- 
 >me].internal;
+    uint32_t nop = PACKET0(internal->nop, 0);
       struct dma_fence *f = NULL;
       struct amdgpu_job *job;
       struct amdgpu_ib *ib;
+    uint32_t *ptr;
       int i, r;

      r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL,

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +30 -23
@@ -635,15 +638,16 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
           goto err;
       ib = &job->ibs[0];
-    ib->ptr[0] = PACKET0(adev->vcn.inst[ring->me].internal.data0, 0);
-    ib->ptr[1] = addr;
-    ib->ptr[2] = PACKET0(adev->vcn.inst[ring->me].internal.data1, 0);
-    ib->ptr[3] = addr >> 32;
-    ib->ptr[4] = PACKET0(adev->vcn.inst[ring->me].internal.cmd, 0);
-    ib->ptr[5] = 0;
+    ptr = ib->ptr;
+    *ptr++ = PACKET0(internal->data0, 0);
+    *ptr++ = addr;
+    *ptr++ = PACKET0(internal->data1, 0);
+    *ptr++ = addr >> 32;
+    *ptr++ = PACKET0(internal->cmd, 0);
+    *ptr++ = 0;
       for (i = 6; i < 16; i += 2) {
-        ib->ptr[i] = PACKET0(adev->vcn.inst[ring->me].internal.nop, 
 0);
-        ib->ptr[i+1] = 0;
+        *ptr++ = nop;
+        *ptr++ = 0;
       }
       ib->length_dw = 16;
@@ -763,17 +767,20 @@ int amdgpu_vcn_dec_ring_test_ib(struct 
 amdgpu_ring *ring, long timeout)
   static uint32_t *amdgpu_vcn_unified_ring_ib_header(struct amdgpu_ib *ib,
                           uint32_t ib_pack_in_dw, bool enc)
   {
+    u32 *ptr = &ib->ptr[ib->length_dw];
       uint32_t *ib_checksum;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* single queue checksum */
-    ib->ptr[ib->length_dw++] = 0x30000002;
-    ib_checksum = &ib->ptr[ib->length_dw++];
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw;
+    *ptr++ = 0x00000010; /* single queue checksum */
+    *ptr++ = 0x30000002;
+    ib_checksum = ptr++;
+    *ptr++ = ib_pack_in_dw;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* engine info */
-    ib->ptr[ib->length_dw++] = 0x30000001;
-    ib->ptr[ib->length_dw++] = enc ? 0x2 : 0x3;
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw * sizeof(uint32_t);
+    *ptr++ = 0x00000010; /* engine info */
+    *ptr++ = 0x30000001;
+    *ptr++ = enc ? 0x2 : 0x3;
+    *ptr++ = ib_pack_in_dw * sizeof(uint32_t);
+
+    ib->length_dw = ptr - ib->ptr;
       return ib_checksum;
   }
@@ -803,7 +810,8 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
       uint64_t addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       uint32_t *ib_checksum;
       uint32_t ib_pack_in_dw;
-    int i, r;
+    u32 *ptr;
+    int r;
       if (adev->vcn.inst[ring->me].using_unified_queue)
           ib_size_dw += 8;
@@ -825,18 +833,20 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
           ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, ib_pack_in_dw, false);
       }
-    ib->ptr[ib->length_dw++] = sizeof(struct 
 amdgpu_vcn_decode_buffer) + 8;
-    ib->ptr[ib->length_dw++] = 
 cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
-    decode_buffer = (struct amdgpu_vcn_decode_buffer *)&(ib- 
 >ptr[ib- >length_dw]);
-    ib->length_dw += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ptr = &ib->ptr[ib->length_dw];
+
+    *ptr++ = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
+    *ptr++ = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
+    decode_buffer = (struct amdgpu_vcn_decode_buffer *)ptr;
+    ptr += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ib->length_dw = ptr - ib->ptr;
       memset(decode_buffer, 0, sizeof(struct amdgpu_vcn_decode_buffer));
       decode_buffer->valid_buf_flag |= cpu_to_le32(AMDGPU_VCN_CMD_FLAG_MSG_BUFFER);
       decode_buffer->msg_buffer_address_hi = cpu_to_le32(addr >> 32);
       decode_buffer->msg_buffer_address_lo = cpu_to_le32(addr);
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);
       if (adev->vcn.inst[ring->me].using_unified_queue)
           amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, ib_pack_in_dw);
@@ -934,7 +944,8 @@ static int amdgpu_vcn_enc_get_create_msg(struct 

amdgpu_ring *ring, uint32_t hand
      struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -947,31 +958,33 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001; /* session info */ -    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001; /* session info */
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002; /* task info */
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000001; /* op initialize */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1002,7 +1015,8 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1015,31 +1029,33 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001;
-    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002;
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001;
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002;
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000002; /* op close session */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

#6 lore raw

Re: [PATCH 01/18] drm/amdgpu: More compact VCN IB emission

Quoted 45 lines · Tvrtko Ursulin
Quoted 43 lines · Lijo
Quoted 25 lines · Tvrtko Ursulin
Quoted 23 lines · Lijo
Quoted 9 lines · Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
  *  Fix decode buffer overwrite

Overall for this series, below type of calculation  -

     ib->length_dw = ptr - ib->ptr;

is not anticipating a case like below.

         r = amdgpu_vcn_enc_get_create_msg(ring, 1, &ib, NULL); if (r)
                 goto error;

Quoted 8 lines
Quoted 3 lines

         r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &ib, &fence); if (r)
                 goto error;

So amdgpu_vcn_enc_ring_test_ib() - are you saying there is a bug there? Help me see it please.

ib->length_dw update is enclosed in both of those helpers, same as before the patch.

What I'm saying is -

It's the same ib pointer that is passed to back to back calls.

Legacy implementation fills like -

ib->ptr[ib->length_dw++]

Quoted 8 lines
Quoted 5 lines

So no assumption that it is starting from offset 0 always. After the function is filled, length gets updated based on what is filled.

I don't see the same with an assignment like

     ib->length_dw = ptr - ib->ptr;

Hm, but both of those helpers emit separate jobs with their own respective IB.

Wouldn't this kind of logic help to be on the safer side -

ib->length_dw += ptr - &ib->ptr[start_size];

Thanks,
Lijo

Quoted 231 lines

Regards,

Tvrtko

Quoted 226 lines
Quoted 225 lines
Quoted 224 lines
Quoted 223 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 134 ++++++++++++
+-----------

  1 file changed, 75 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3 -0
index 17db7264269e..2c367ee6e182 100644
@@ -622,9 +622,12 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
   {
       u64 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       struct amdgpu_device *adev = ring->adev;
+    struct amdgpu_vcn_reg *internal = &adev->vcn.inst[ring- 
 >me].internal;
+    uint32_t nop = PACKET0(internal->nop, 0);
       struct dma_fence *f = NULL;
       struct amdgpu_job *job;
       struct amdgpu_ib *ib;
+    uint32_t *ptr;
       int i, r;

      r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL,

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +10 -9
@@ -635,15 +638,16 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
           goto err;
       ib = &job->ibs[0];
-    ib->ptr[0] = PACKET0(adev->vcn.inst[ring->me].internal.data0, 0);
-    ib->ptr[1] = addr;
-    ib->ptr[2] = PACKET0(adev->vcn.inst[ring->me].internal.data1, 0);
-    ib->ptr[3] = addr >> 32;
-    ib->ptr[4] = PACKET0(adev->vcn.inst[ring->me].internal.cmd, 0);
-    ib->ptr[5] = 0;
+    ptr = ib->ptr;
+    *ptr++ = PACKET0(internal->data0, 0);
+    *ptr++ = addr;
+    *ptr++ = PACKET0(internal->data1, 0);
+    *ptr++ = addr >> 32;
+    *ptr++ = PACKET0(internal->cmd, 0);
+    *ptr++ = 0;
       for (i = 6; i < 16; i += 2) {
-        ib->ptr[i] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.nop, 0);
-        ib->ptr[i+1] = 0;
+        *ptr++ = nop;
+        *ptr++ = 0;
       }
       ib->length_dw = 16;
@@ -763,17 +767,20 @@ int amdgpu_vcn_dec_ring_test_ib(struct 
 amdgpu_ring *ring, long timeout)
   static uint32_t *amdgpu_vcn_unified_ring_ib_header(struct amdgpu_ib *ib,
                           uint32_t ib_pack_in_dw, bool enc)
   {
+    u32 *ptr = &ib->ptr[ib->length_dw];
       uint32_t *ib_checksum;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* single queue 

checksum */
-    ib->ptr[ib->length_dw++] = 0x30000002;
-    ib_checksum = &ib->ptr[ib->length_dw++];
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw;
+    *ptr++ = 0x00000010; /* single queue checksum */
+    *ptr++ = 0x30000002;
+    ib_checksum = ptr++;
+    *ptr++ = ib_pack_in_dw;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* engine info */ -    ib->ptr[ib->length_dw++] = 0x30000001;
-    ib->ptr[ib->length_dw++] = enc ? 0x2 : 0x3;
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw * sizeof(uint32_t); +    *ptr++ = 0x00000010; /* engine info */
+    *ptr++ = 0x30000001;
+    *ptr++ = enc ? 0x2 : 0x3;
+    *ptr++ = ib_pack_in_dw * sizeof(uint32_t);
+
+    ib->length_dw = ptr - ib->ptr;
      return ib_checksum;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +10 -7
@@ -803,7 +810,8 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
       uint64_t addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       uint32_t *ib_checksum;
       uint32_t ib_pack_in_dw;
-    int i, r;
+    u32 *ptr;
+    int r;
       if (adev->vcn.inst[ring->me].using_unified_queue)
           ib_size_dw += 8;
@@ -825,18 +833,20 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
           ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, ib_pack_in_dw, false);
       }
-    ib->ptr[ib->length_dw++] = sizeof(struct 
 amdgpu_vcn_decode_buffer) + 8;
-    ib->ptr[ib->length_dw++] = 
 cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
-    decode_buffer = (struct amdgpu_vcn_decode_buffer *)&(ib- 
 >ptr[ib- >length_dw]);
-    ib->length_dw += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ptr = &ib->ptr[ib->length_dw];
+
+    *ptr++ = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
+    *ptr++ = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
+    decode_buffer = (struct amdgpu_vcn_decode_buffer *)ptr;
+    ptr += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ib->length_dw = ptr - ib->ptr;
       memset(decode_buffer, 0, sizeof(struct amdgpu_vcn_decode_buffer));
       decode_buffer->valid_buf_flag |= cpu_to_le32(AMDGPU_VCN_CMD_FLAG_MSG_BUFFER);
       decode_buffer->msg_buffer_address_hi = cpu_to_le32(addr >> 32);
       decode_buffer->msg_buffer_address_lo = cpu_to_le32(addr);
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);
       if (adev->vcn.inst[ring->me].using_unified_queue)
           amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, ib_pack_in_dw);
@@ -934,7 +944,8 @@ static int amdgpu_vcn_enc_get_create_msg(struct 

amdgpu_ring *ring, uint32_t hand
      struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -947,31 +958,33 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001; /* session info */ -    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001; /* session info */
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002; /* task info */
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000001; /* op initialize */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1002,7 +1015,8 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1015,31 +1029,33 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001;
-    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002;
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001;
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002;
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000002; /* op close session */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

#7 lore raw

Re: [PATCH 01/18] drm/amdgpu: More compact VCN IB emission

Quoted 50 lines · Lijo
Quoted 49 lines · Tvrtko Ursulin
Quoted 47 lines · Lijo
Quoted 25 lines · Tvrtko Ursulin
Quoted 23 lines · Lijo
Quoted 9 lines · Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
  *  Fix decode buffer overwrite

Overall for this series, below type of calculation  -

     ib->length_dw = ptr - ib->ptr;

is not anticipating a case like below.

         r = amdgpu_vcn_enc_get_create_msg(ring, 1, &ib, NULL); if (r)
                 goto error;

Quoted 8 lines
Quoted 3 lines

         r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &ib, &fence); if (r)
                 goto error;

So amdgpu_vcn_enc_ring_test_ib() - are you saying there is a bug there? Help me see it please.

ib->length_dw update is enclosed in both of those helpers, same as before the patch.

What I'm saying is -

It's the same ib pointer that is passed to back to back calls.

Legacy implementation fills like -

ib->ptr[ib->length_dw++]

So no assumption that it is starting from offset 0 always. After the function is filled, length gets updated based on what is filled.

I don't see the same with an assignment like

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

     ib->length_dw = ptr - ib->ptr;

Hm, but both of those helpers emit separate jobs with their own respective IB.

Wouldn't this kind of logic help to be on the safer side -

ib->length_dw += ptr - &ib->ptr[start_size];

I am not sure at which call sites you would suggest that?

I used the pattern of whoever writes to the IB starts their block with:

	u32 *ptr = &ib->ptr[ib->length_dw];

Then they emit their stuff, and finally close with:

	ib->length_dw = ptr - ib->ptr;

By that design callers are safe since all are self-contained. I at the moment do not see how making that more complicated would make it safer.

Regards,

Tvrtko

Quoted 230 lines
Quoted 229 lines
Quoted 228 lines
Quoted 227 lines
Quoted 226 lines
Quoted 225 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 134 ++++++++++++
+-----------

  1 file changed, 75 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3 -0
index 17db7264269e..2c367ee6e182 100644
@@ -622,9 +622,12 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
   {
       u64 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       struct amdgpu_device *adev = ring->adev;
+    struct amdgpu_vcn_reg *internal = &adev->vcn.inst[ring- 
 >me].internal;
+    uint32_t nop = PACKET0(internal->nop, 0);
       struct dma_fence *f = NULL;
       struct amdgpu_job *job;
       struct amdgpu_ib *ib;
+    uint32_t *ptr;
       int i, r;

      r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL,

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +9 -8
@@ -635,15 +638,16 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
           goto err;
       ib = &job->ibs[0];
-    ib->ptr[0] = PACKET0(adev->vcn.inst[ring->me].internal.data0, 
 0);
-    ib->ptr[1] = addr;
-    ib->ptr[2] = PACKET0(adev->vcn.inst[ring->me].internal.data1, 
 0);
-    ib->ptr[3] = addr >> 32;
-    ib->ptr[4] = PACKET0(adev->vcn.inst[ring->me].internal.cmd, 0);
-    ib->ptr[5] = 0;
+    ptr = ib->ptr;
+    *ptr++ = PACKET0(internal->data0, 0);
+    *ptr++ = addr;
+    *ptr++ = PACKET0(internal->data1, 0);
+    *ptr++ = addr >> 32;
+    *ptr++ = PACKET0(internal->cmd, 0);
+    *ptr++ = 0;
       for (i = 6; i < 16; i += 2) {
-        ib->ptr[i] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.nop, 0);
-        ib->ptr[i+1] = 0;
+        *ptr++ = nop;
+        *ptr++ = 0;

      }
      ib->length_dw = 16;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1 -1
@@ -763,17 +767,20 @@ int amdgpu_vcn_dec_ring_test_ib(struct 
 amdgpu_ring *ring, long timeout)
   static uint32_t *amdgpu_vcn_unified_ring_ib_header(struct amdgpu_ib *ib,
                           uint32_t ib_pack_in_dw, bool enc)
   {
+    u32 *ptr = &ib->ptr[ib->length_dw];
       uint32_t *ib_checksum;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* single queue 

checksum */
-    ib->ptr[ib->length_dw++] = 0x30000002;
-    ib_checksum = &ib->ptr[ib->length_dw++];
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw;
+    *ptr++ = 0x00000010; /* single queue checksum */
+    *ptr++ = 0x30000002;
+    ib_checksum = ptr++;
+    *ptr++ = ib_pack_in_dw;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* engine info */ -    ib->ptr[ib->length_dw++] = 0x30000001;
-    ib->ptr[ib->length_dw++] = enc ? 0x2 : 0x3;
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw * sizeof(uint32_t); +    *ptr++ = 0x00000010; /* engine info */
+    *ptr++ = 0x30000001;
+    *ptr++ = enc ? 0x2 : 0x3;
+    *ptr++ = ib_pack_in_dw * sizeof(uint32_t);
+
+    ib->length_dw = ptr - ib->ptr;
      return ib_checksum;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +10 -7
@@ -803,7 +810,8 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
       uint64_t addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       uint32_t *ib_checksum;
       uint32_t ib_pack_in_dw;
-    int i, r;
+    u32 *ptr;
+    int r;
       if (adev->vcn.inst[ring->me].using_unified_queue)
           ib_size_dw += 8;
@@ -825,18 +833,20 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
           ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, ib_pack_in_dw, false);
       }
-    ib->ptr[ib->length_dw++] = sizeof(struct 
 amdgpu_vcn_decode_buffer) + 8;
-    ib->ptr[ib->length_dw++] = 
 cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
-    decode_buffer = (struct amdgpu_vcn_decode_buffer *)&(ib- 
 >ptr[ib- >length_dw]);
-    ib->length_dw += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ptr = &ib->ptr[ib->length_dw];
+
+    *ptr++ = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
+    *ptr++ = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
+    decode_buffer = (struct amdgpu_vcn_decode_buffer *)ptr;
+    ptr += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ib->length_dw = ptr - ib->ptr;
       memset(decode_buffer, 0, sizeof(struct amdgpu_vcn_decode_buffer));
       decode_buffer->valid_buf_flag |= cpu_to_le32(AMDGPU_VCN_CMD_FLAG_MSG_BUFFER);
       decode_buffer->msg_buffer_address_hi = cpu_to_le32(addr >> 32);
       decode_buffer->msg_buffer_address_lo = cpu_to_le32(addr);
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);
       if (adev->vcn.inst[ring->me].using_unified_queue)
           amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, ib_pack_in_dw);
@@ -934,7 +944,8 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -947,31 +958,33 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001; /* session info */ -    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001; /* session info */
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002; /* task info */
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000001; /* op initialize */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1002,7 +1015,8 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1015,31 +1029,33 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001;
-    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002;
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001;
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002;
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000002; /* op close session */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

#8 lore raw

Re: [PATCH 01/18] drm/amdgpu: More compact VCN IB emission

Quoted 65 lines · Tvrtko Ursulin
Quoted 50 lines · Lijo
Quoted 49 lines · Tvrtko Ursulin
Quoted 47 lines · Lijo
Quoted 25 lines · Tvrtko Ursulin
Quoted 23 lines · Lijo
Quoted 9 lines · Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
  *  Fix decode buffer overwrite

Overall for this series, below type of calculation  -

     ib->length_dw = ptr - ib->ptr;

is not anticipating a case like below.

         r = amdgpu_vcn_enc_get_create_msg(ring, 1, &ib, NULL); if (r)
                 goto error;

Quoted 8 lines
Quoted 3 lines

         r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &ib, &fence); if (r)
                 goto error;

So amdgpu_vcn_enc_ring_test_ib() - are you saying there is a bug there? Help me see it please.

ib->length_dw update is enclosed in both of those helpers, same as before the patch.

What I'm saying is -

It's the same ib pointer that is passed to back to back calls.

Legacy implementation fills like -

ib->ptr[ib->length_dw++]

So no assumption that it is starting from offset 0 always. After the function is filled, length gets updated based on what is filled.

I don't see the same with an assignment like

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

     ib->length_dw = ptr - ib->ptr;

Hm, but both of those helpers emit separate jobs with their own respective IB.

Wouldn't this kind of logic help to be on the safer side -

ib->length_dw += ptr - &ib->ptr[start_size];

I am not sure at which call sites you would suggest that?

I used the pattern of whoever writes to the IB starts their block with:

Quoted 8 lines

    u32 *ptr = &ib->ptr[ib->length_dw];

Then they emit their stuff, and finally close with:

    ib->length_dw = ptr - ib->ptr;

By that design callers are safe since all are self-contained. I at the moment do not see how making that more complicated would make it safer.

I would say all places where the function starts like below without keeping any assumptions.

	ib->ptr[ib->length_dw++] = <value>;

I don't see the complication with a code like this

	ib->length_dw += ptr - &ib->ptr[ib->length_dw];

It keeps the same behavior as before.

Thanks,
Lijo

Quoted 235 lines

Regards,

Tvrtko

Quoted 230 lines
Quoted 229 lines
Quoted 228 lines
Quoted 227 lines
Quoted 226 lines
Quoted 225 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 134 ++++++++++++
+-----------

  1 file changed, 75 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3 -0
index 17db7264269e..2c367ee6e182 100644
@@ -622,9 +622,12 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
   {
       u64 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       struct amdgpu_device *adev = ring->adev;
+    struct amdgpu_vcn_reg *internal = &adev->vcn.inst[ring- 
 >me].internal;
+    uint32_t nop = PACKET0(internal->nop, 0);
       struct dma_fence *f = NULL;
       struct amdgpu_job *job;
       struct amdgpu_ib *ib;
+    uint32_t *ptr;
       int i, r;

      r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL,

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +9 -8
@@ -635,15 +638,16 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
           goto err;
       ib = &job->ibs[0];
-    ib->ptr[0] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.data0, 0);
-    ib->ptr[1] = addr;
-    ib->ptr[2] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.data1, 0);
-    ib->ptr[3] = addr >> 32;
-    ib->ptr[4] = PACKET0(adev->vcn.inst[ring->me].internal.cmd, 0);
-    ib->ptr[5] = 0;
+    ptr = ib->ptr;
+    *ptr++ = PACKET0(internal->data0, 0);
+    *ptr++ = addr;
+    *ptr++ = PACKET0(internal->data1, 0);
+    *ptr++ = addr >> 32;
+    *ptr++ = PACKET0(internal->cmd, 0);
+    *ptr++ = 0;
       for (i = 6; i < 16; i += 2) {
-        ib->ptr[i] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.nop, 0);
-        ib->ptr[i+1] = 0;
+        *ptr++ = nop;
+        *ptr++ = 0;

      }
      ib->length_dw = 16;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1 -1
@@ -763,17 +767,20 @@ int amdgpu_vcn_dec_ring_test_ib(struct 
 amdgpu_ring *ring, long timeout)
   static uint32_t *amdgpu_vcn_unified_ring_ib_header(struct amdgpu_ib *ib,
                           uint32_t ib_pack_in_dw, bool enc)
   {
+    u32 *ptr = &ib->ptr[ib->length_dw];
       uint32_t *ib_checksum;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* single queue 

checksum */
-    ib->ptr[ib->length_dw++] = 0x30000002;
-    ib_checksum = &ib->ptr[ib->length_dw++];
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw;
+    *ptr++ = 0x00000010; /* single queue checksum */
+    *ptr++ = 0x30000002;
+    ib_checksum = ptr++;
+    *ptr++ = ib_pack_in_dw;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* engine info */ -    ib->ptr[ib->length_dw++] = 0x30000001;
-    ib->ptr[ib->length_dw++] = enc ? 0x2 : 0x3;
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw * sizeof(uint32_t); +    *ptr++ = 0x00000010; /* engine info */
+    *ptr++ = 0x30000001;
+    *ptr++ = enc ? 0x2 : 0x3;
+    *ptr++ = ib_pack_in_dw * sizeof(uint32_t);
+
+    ib->length_dw = ptr - ib->ptr;
      return ib_checksum;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +10 -7
@@ -803,7 +810,8 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
       uint64_t addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       uint32_t *ib_checksum;
       uint32_t ib_pack_in_dw;
-    int i, r;
+    u32 *ptr;
+    int r;
       if (adev->vcn.inst[ring->me].using_unified_queue)
           ib_size_dw += 8;
@@ -825,18 +833,20 @@ static int 
 amdgpu_vcn_dec_sw_send_msg(struct amdgpu_ring *ring,
           ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, ib_pack_in_dw, false);
       }
-    ib->ptr[ib->length_dw++] = sizeof(struct 
 amdgpu_vcn_decode_buffer) + 8;
-    ib->ptr[ib->length_dw++] = 
 cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
-    decode_buffer = (struct amdgpu_vcn_decode_buffer *)&(ib- 
 >ptr[ib- >length_dw]);
-    ib->length_dw += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ptr = &ib->ptr[ib->length_dw];
+
+    *ptr++ = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
+    *ptr++ = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
+    decode_buffer = (struct amdgpu_vcn_decode_buffer *)ptr;
+    ptr += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ib->length_dw = ptr - ib->ptr;
       memset(decode_buffer, 0, sizeof(struct amdgpu_vcn_decode_buffer));
       decode_buffer->valid_buf_flag |= cpu_to_le32(AMDGPU_VCN_CMD_FLAG_MSG_BUFFER);
       decode_buffer->msg_buffer_address_hi = cpu_to_le32(addr >> 32);
       decode_buffer->msg_buffer_address_lo = cpu_to_le32(addr);
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);
       if (adev->vcn.inst[ring->me].using_unified_queue)
           amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, ib_pack_in_dw);
@@ -934,7 +944,8 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -947,31 +958,33 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001; /* session info */ -    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001; /* session info */
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002; /* task info */
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000001; /* op initialize */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1002,7 +1015,8 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1015,31 +1029,33 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001;
-    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002;
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001;
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002;
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000002; /* op close session */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

#9 lore raw

Re: [PATCH 01/18] drm/amdgpu: More compact VCN IB emission

Quoted 79 lines · Lijo
Quoted 65 lines · Tvrtko Ursulin
Quoted 50 lines · Lijo
Quoted 49 lines · Tvrtko Ursulin
Quoted 47 lines · Lijo
Quoted 25 lines · Tvrtko Ursulin
Quoted 23 lines · Lijo
Quoted 9 lines · Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
  *  Fix decode buffer overwrite

Overall for this series, below type of calculation  -

     ib->length_dw = ptr - ib->ptr;

is not anticipating a case like below.

         r = amdgpu_vcn_enc_get_create_msg(ring, 1, &ib, NULL); if (r)
                 goto error;

Quoted 8 lines
Quoted 3 lines

         r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &ib, &fence); if (r)
                 goto error;

So amdgpu_vcn_enc_ring_test_ib() - are you saying there is a bug there? Help me see it please.

ib->length_dw update is enclosed in both of those helpers, same as before the patch.

What I'm saying is -

It's the same ib pointer that is passed to back to back calls.

Legacy implementation fills like -

ib->ptr[ib->length_dw++]

So no assumption that it is starting from offset 0 always. After the function is filled, length gets updated based on what is filled.

I don't see the same with an assignment like

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

     ib->length_dw = ptr - ib->ptr;

Hm, but both of those helpers emit separate jobs with their own respective IB.

Wouldn't this kind of logic help to be on the safer side -

ib->length_dw += ptr - &ib->ptr[start_size];

I am not sure at which call sites you would suggest that?

I used the pattern of whoever writes to the IB starts their block with:

Quoted 8 lines

     u32 *ptr = &ib->ptr[ib->length_dw];

Then they emit their stuff, and finally close with:

     ib->length_dw = ptr - ib->ptr;

By that design callers are safe since all are self-contained. I at the moment do not see how making that more complicated would make it safer.

I would say all places where the function starts like below without keeping any assumptions.

Quoted 8 lines

    ib->ptr[ib->length_dw++] = <value>;

I don't see the complication with a code like this

    ib->length_dw += ptr - &ib->ptr[ib->length_dw];

It keeps the same behavior as before.

As such the patch logic conflicts itself.

For this to be true -

	ib->length_dw = ptr - ib->ptr;

It should start like.

	u32 *ptr = ib->ptr;

Now it starts like

	u32 *ptr = &ib->ptr[ib->length_dw];

So if you feel the new way is self contained, I would expect a change here as well. Otherwise, this could be read as a bug.

Thanks,
Lijo

Quoted 240 lines

Thanks,
Lijo

Quoted 236 lines

Regards,

Tvrtko

Quoted 231 lines
Quoted 230 lines
Quoted 229 lines
Quoted 228 lines
Quoted 227 lines
Quoted 226 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 134 ++++++++++++
+-----------

  1 file changed, 75 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3 -0
index 17db7264269e..2c367ee6e182 100644
@@ -622,9 +622,12 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
   {
       u64 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       struct amdgpu_device *adev = ring->adev;
+    struct amdgpu_vcn_reg *internal = &adev->vcn.inst[ring- 
 >me].internal;
+    uint32_t nop = PACKET0(internal->nop, 0);
       struct dma_fence *f = NULL;
       struct amdgpu_job *job;
       struct amdgpu_ib *ib;
+    uint32_t *ptr;
       int i, r;

      r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL,

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +9 -8
@@ -635,15 +638,16 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
           goto err;
       ib = &job->ibs[0];
-    ib->ptr[0] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.data0, 0);
-    ib->ptr[1] = addr;
-    ib->ptr[2] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.data1, 0);
-    ib->ptr[3] = addr >> 32;
-    ib->ptr[4] = PACKET0(adev->vcn.inst[ring->me].internal.cmd, 
 0);
-    ib->ptr[5] = 0;
+    ptr = ib->ptr;
+    *ptr++ = PACKET0(internal->data0, 0);
+    *ptr++ = addr;
+    *ptr++ = PACKET0(internal->data1, 0);
+    *ptr++ = addr >> 32;
+    *ptr++ = PACKET0(internal->cmd, 0);
+    *ptr++ = 0;
       for (i = 6; i < 16; i += 2) {
-        ib->ptr[i] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.nop, 0);
-        ib->ptr[i+1] = 0;
+        *ptr++ = nop;
+        *ptr++ = 0;

      }
      ib->length_dw = 16;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1 -1
@@ -763,17 +767,20 @@ int amdgpu_vcn_dec_ring_test_ib(struct 
 amdgpu_ring *ring, long timeout)
   static uint32_t *amdgpu_vcn_unified_ring_ib_header(struct amdgpu_ib *ib,
                           uint32_t ib_pack_in_dw, bool enc)
   {
+    u32 *ptr = &ib->ptr[ib->length_dw];
       uint32_t *ib_checksum;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* single queue 

checksum */
-    ib->ptr[ib->length_dw++] = 0x30000002;
-    ib_checksum = &ib->ptr[ib->length_dw++];
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw;
+    *ptr++ = 0x00000010; /* single queue checksum */
+    *ptr++ = 0x30000002;
+    ib_checksum = ptr++;
+    *ptr++ = ib_pack_in_dw;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* engine info */ -    ib->ptr[ib->length_dw++] = 0x30000001;
-    ib->ptr[ib->length_dw++] = enc ? 0x2 : 0x3;
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw * sizeof(uint32_t); +    *ptr++ = 0x00000010; /* engine info */
+    *ptr++ = 0x30000001;
+    *ptr++ = enc ? 0x2 : 0x3;
+    *ptr++ = ib_pack_in_dw * sizeof(uint32_t);
+
+    ib->length_dw = ptr - ib->ptr;
      return ib_checksum;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +10 -7
@@ -803,7 +810,8 @@ static int amdgpu_vcn_dec_sw_send_msg(struct 
 amdgpu_ring *ring,
       uint64_t addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       uint32_t *ib_checksum;
       uint32_t ib_pack_in_dw;
-    int i, r;
+    u32 *ptr;
+    int r;
       if (adev->vcn.inst[ring->me].using_unified_queue)
           ib_size_dw += 8;
@@ -825,18 +833,20 @@ static int 
 amdgpu_vcn_dec_sw_send_msg(struct amdgpu_ring *ring,
           ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, ib_pack_in_dw, false);
       }
-    ib->ptr[ib->length_dw++] = sizeof(struct 
 amdgpu_vcn_decode_buffer) + 8;
-    ib->ptr[ib->length_dw++] = 
 cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
-    decode_buffer = (struct amdgpu_vcn_decode_buffer *)&(ib- 
 >ptr[ib- >length_dw]);
-    ib->length_dw += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ptr = &ib->ptr[ib->length_dw];
+
+    *ptr++ = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
+    *ptr++ = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
+    decode_buffer = (struct amdgpu_vcn_decode_buffer *)ptr;
+    ptr += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ib->length_dw = ptr - ib->ptr;
       memset(decode_buffer, 0, sizeof(struct amdgpu_vcn_decode_buffer));
       decode_buffer->valid_buf_flag |= cpu_to_le32(AMDGPU_VCN_CMD_FLAG_MSG_BUFFER);
       decode_buffer->msg_buffer_address_hi = cpu_to_le32(addr >> 32);
       decode_buffer->msg_buffer_address_lo = cpu_to_le32(addr);
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);
       if (adev->vcn.inst[ring->me].using_unified_queue)
           amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, ib_pack_in_dw);
@@ -934,7 +944,8 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -947,31 +958,33 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001; /* session info */ -    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001; /* session info */
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002; /* task info */
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000001; /* op initialize */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1002,7 +1015,8 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1015,31 +1029,33 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001;
-    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002;
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001;
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002;
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000002; /* op close session */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

#10 lore raw

Re: [PATCH 01/18] drm/amdgpu: More compact VCN IB emission

Quoted 96 lines · Lijo
Quoted 79 lines · Lijo
Quoted 65 lines · Tvrtko Ursulin
Quoted 50 lines · Lijo
Quoted 49 lines · Tvrtko Ursulin
Quoted 47 lines · Lijo
Quoted 25 lines · Tvrtko Ursulin
Quoted 23 lines · Lijo
Quoted 9 lines · Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
  *  Fix decode buffer overwrite

Overall for this series, below type of calculation  -

     ib->length_dw = ptr - ib->ptr;

is not anticipating a case like below.

         r = amdgpu_vcn_enc_get_create_msg(ring, 1, &ib, NULL); if (r)
                 goto error;

Quoted 8 lines
Quoted 3 lines

         r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &ib, &fence); if (r)
                 goto error;

So amdgpu_vcn_enc_ring_test_ib() - are you saying there is a bug there? Help me see it please.

ib->length_dw update is enclosed in both of those helpers, same as before the patch.

What I'm saying is -

It's the same ib pointer that is passed to back to back calls.

Legacy implementation fills like -

ib->ptr[ib->length_dw++]

So no assumption that it is starting from offset 0 always. After the function is filled, length gets updated based on what is filled.

I don't see the same with an assignment like

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

     ib->length_dw = ptr - ib->ptr;

Hm, but both of those helpers emit separate jobs with their own respective IB.

Wouldn't this kind of logic help to be on the safer side -

ib->length_dw += ptr - &ib->ptr[start_size];

I am not sure at which call sites you would suggest that?

I used the pattern of whoever writes to the IB starts their block with:

Quoted 8 lines

     u32 *ptr = &ib->ptr[ib->length_dw];

Then they emit their stuff, and finally close with:

     ib->length_dw = ptr - ib->ptr;

By that design callers are safe since all are self-contained. I at the moment do not see how making that more complicated would make it safer.

I would say all places where the function starts like below without keeping any assumptions.

Quoted 8 lines

     ib->ptr[ib->length_dw++] = <value>;

I don't see the complication with a code like this

     ib->length_dw += ptr - &ib->ptr[ib->length_dw];

It keeps the same behavior as before.

As such the patch logic conflicts itself.

For this to be true -
    ib->length_dw = ptr - ib->ptr;

Quoted 9 lines

It should start like.

    u32 *ptr = ib->ptr;

Now it starts like
    u32 *ptr = &ib->ptr[ib->length_dw];

So if you feel the new way is self contained, I would expect a change here as well. Otherwise, this could be read as a bug.

Sorry for the noise. Got confused for a while; now I see what you meant.

Thanks,
Lijo

Quoted 245 lines

Thanks,
Lijo

Quoted 241 lines

Thanks,
Lijo

Quoted 237 lines

Regards,

Tvrtko

Quoted 232 lines
Quoted 231 lines
Quoted 230 lines
Quoted 229 lines
Quoted 228 lines
Quoted 227 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 134 ++++++++++++
+-----------

  1 file changed, 75 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3 -0
index 17db7264269e..2c367ee6e182 100644
@@ -622,9 +622,12 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
   {
       u64 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       struct amdgpu_device *adev = ring->adev;
+    struct amdgpu_vcn_reg *internal = &adev->vcn.inst[ring- 
 >me].internal;
+    uint32_t nop = PACKET0(internal->nop, 0);
       struct dma_fence *f = NULL;
       struct amdgpu_job *job;
       struct amdgpu_ib *ib;
+    uint32_t *ptr;
       int i, r;

      r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL,

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +9 -8
@@ -635,15 +638,16 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
           goto err;
       ib = &job->ibs[0];
-    ib->ptr[0] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.data0, 0);
-    ib->ptr[1] = addr;
-    ib->ptr[2] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.data1, 0);
-    ib->ptr[3] = addr >> 32;
-    ib->ptr[4] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.cmd, 0);
-    ib->ptr[5] = 0;
+    ptr = ib->ptr;
+    *ptr++ = PACKET0(internal->data0, 0);
+    *ptr++ = addr;
+    *ptr++ = PACKET0(internal->data1, 0);
+    *ptr++ = addr >> 32;
+    *ptr++ = PACKET0(internal->cmd, 0);
+    *ptr++ = 0;
       for (i = 6; i < 16; i += 2) {
-        ib->ptr[i] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.nop, 0);
-        ib->ptr[i+1] = 0;
+        *ptr++ = nop;
+        *ptr++ = 0;

      }
      ib->length_dw = 16;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1 -1
@@ -763,17 +767,20 @@ int amdgpu_vcn_dec_ring_test_ib(struct 
 amdgpu_ring *ring, long timeout)
   static uint32_t *amdgpu_vcn_unified_ring_ib_header(struct amdgpu_ib *ib,
                           uint32_t ib_pack_in_dw, bool enc)
   {
+    u32 *ptr = &ib->ptr[ib->length_dw];
       uint32_t *ib_checksum;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* single queue 

checksum */
-    ib->ptr[ib->length_dw++] = 0x30000002;
-    ib_checksum = &ib->ptr[ib->length_dw++];
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw;
+    *ptr++ = 0x00000010; /* single queue checksum */
+    *ptr++ = 0x30000002;
+    ib_checksum = ptr++;
+    *ptr++ = ib_pack_in_dw;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* engine info */ -    ib->ptr[ib->length_dw++] = 0x30000001;
-    ib->ptr[ib->length_dw++] = enc ? 0x2 : 0x3;
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw * sizeof(uint32_t); +    *ptr++ = 0x00000010; /* engine info */
+    *ptr++ = 0x30000001;
+    *ptr++ = enc ? 0x2 : 0x3;
+    *ptr++ = ib_pack_in_dw * sizeof(uint32_t);
+
+    ib->length_dw = ptr - ib->ptr;
      return ib_checksum;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +9 -5
@@ -803,7 +810,8 @@ static int 
 amdgpu_vcn_dec_sw_send_msg(struct amdgpu_ring *ring,
       uint64_t addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       uint32_t *ib_checksum;
       uint32_t ib_pack_in_dw;
-    int i, r;
+    u32 *ptr;
+    int r;
       if (adev->vcn.inst[ring->me].using_unified_queue)
           ib_size_dw += 8;
@@ -825,18 +833,20 @@ static int 
 amdgpu_vcn_dec_sw_send_msg(struct amdgpu_ring *ring,
           ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, ib_pack_in_dw, false);
       }
-    ib->ptr[ib->length_dw++] = sizeof(struct 
 amdgpu_vcn_decode_buffer) + 8;
-    ib->ptr[ib->length_dw++] = 
 cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
-    decode_buffer = (struct amdgpu_vcn_decode_buffer *)&(ib- 
 >ptr[ib- >length_dw]);
-    ib->length_dw += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ptr = &ib->ptr[ib->length_dw];
+
+    *ptr++ = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
+    *ptr++ = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
+    decode_buffer = (struct amdgpu_vcn_decode_buffer *)ptr;
+    ptr += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ib->length_dw = ptr - ib->ptr;
       memset(decode_buffer, 0, sizeof(struct amdgpu_vcn_decode_buffer));
       decode_buffer->valid_buf_flag |= cpu_to_le32(AMDGPU_VCN_CMD_FLAG_MSG_BUFFER);

      decode_buffer->msg_buffer_address_hi = cpu_to_le32(addr

Quoted 1 line
Quoted 1 line

32);

      decode_buffer->msg_buffer_address_lo = cpu_to_le32(addr); -    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);
      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, ib_pack_in_dw);

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -934,7 +944,8 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -947,31 +958,33 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001; /* session info */ -    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001; /* session info */
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002; /* task info */
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000001; /* op initialize */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1002,7 +1015,8 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1015,31 +1029,33 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001;
-    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002;
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001;
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002;
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000002; /* op close session */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

#11 lore raw

Re: [PATCH 01/18] drm/amdgpu: More compact VCN IB emission

Quoted 103 lines · Lijo
Quoted 100 lines · Lijo
Quoted 79 lines · Lijo
Quoted 65 lines · Tvrtko Ursulin
Quoted 50 lines · Lijo
Quoted 49 lines · Tvrtko Ursulin
Quoted 47 lines · Lijo
Quoted 25 lines · Tvrtko Ursulin
Quoted 23 lines · Lijo
Quoted 9 lines · Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
  *  Fix decode buffer overwrite

Overall for this series, below type of calculation  -

     ib->length_dw = ptr - ib->ptr;

is not anticipating a case like below.

         r = amdgpu_vcn_enc_get_create_msg(ring, 1, &ib, NULL); if (r)
                 goto error;

Quoted 8 lines
Quoted 3 lines

         r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &ib, &fence); if (r)
                 goto error;

So amdgpu_vcn_enc_ring_test_ib() - are you saying there is a bug there? Help me see it please.

ib->length_dw update is enclosed in both of those helpers, same as before the patch.

What I'm saying is -

It's the same ib pointer that is passed to back to back calls.

Legacy implementation fills like -

ib->ptr[ib->length_dw++]

So no assumption that it is starting from offset 0 always. After the function is filled, length gets updated based on what is filled.

I don't see the same with an assignment like

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

     ib->length_dw = ptr - ib->ptr;

Hm, but both of those helpers emit separate jobs with their own respective IB.

Wouldn't this kind of logic help to be on the safer side -

ib->length_dw += ptr - &ib->ptr[start_size];

I am not sure at which call sites you would suggest that?

I used the pattern of whoever writes to the IB starts their block with:

Quoted 8 lines

     u32 *ptr = &ib->ptr[ib->length_dw];

Then they emit their stuff, and finally close with:

     ib->length_dw = ptr - ib->ptr;

By that design callers are safe since all are self-contained. I at the moment do not see how making that more complicated would make it safer.

I would say all places where the function starts like below without keeping any assumptions.

Quoted 8 lines

     ib->ptr[ib->length_dw++] = <value>;

I don't see the complication with a code like this

     ib->length_dw += ptr - &ib->ptr[ib->length_dw];

It keeps the same behavior as before.

As such the patch logic conflicts itself.

For this to be true -
     ib->length_dw = ptr - ib->ptr;

It should start like.

     u32 *ptr = ib->ptr;

Quoted 7 lines
Quoted 5 lines

Now it starts like
     u32 *ptr = &ib->ptr[ib->length_dw];

So if you feel the new way is self contained, I would expect a change here as well. Otherwise, this could be read as a bug.

Sorry for the noise. Got confused for a while; now I see what you meant.

No worries it happens to everyone! :)

Regards,

Tvrtko

#12 lore raw

Re: [PATCH 01/18] drm/amdgpu: More compact VCN IB emission

In reply to Lazar, Lijo
Quoted 96 lines · Lijo
Quoted 79 lines · Lijo
Quoted 65 lines · Tvrtko Ursulin
Quoted 50 lines · Lijo
Quoted 49 lines · Tvrtko Ursulin
Quoted 47 lines · Lijo
Quoted 25 lines · Tvrtko Ursulin
Quoted 23 lines · Lijo
Quoted 9 lines · Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>

---
v2:
  *  Fix decode buffer overwrite

Overall for this series, below type of calculation  -

     ib->length_dw = ptr - ib->ptr;

is not anticipating a case like below.

         r = amdgpu_vcn_enc_get_create_msg(ring, 1, &ib, NULL); if (r)
                 goto error;

Quoted 8 lines
Quoted 3 lines

         r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &ib, &fence); if (r)
                 goto error;

So amdgpu_vcn_enc_ring_test_ib() - are you saying there is a bug there? Help me see it please.

ib->length_dw update is enclosed in both of those helpers, same as before the patch.

What I'm saying is -

It's the same ib pointer that is passed to back to back calls.

Legacy implementation fills like -

ib->ptr[ib->length_dw++]

So no assumption that it is starting from offset 0 always. After the function is filled, length gets updated based on what is filled.

I don't see the same with an assignment like

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

     ib->length_dw = ptr - ib->ptr;

Hm, but both of those helpers emit separate jobs with their own respective IB.

Wouldn't this kind of logic help to be on the safer side -

ib->length_dw += ptr - &ib->ptr[start_size];

I am not sure at which call sites you would suggest that?

I used the pattern of whoever writes to the IB starts their block with:

Quoted 8 lines

     u32 *ptr = &ib->ptr[ib->length_dw];

Then they emit their stuff, and finally close with:

     ib->length_dw = ptr - ib->ptr;

By that design callers are safe since all are self-contained. I at the moment do not see how making that more complicated would make it safer.

I would say all places where the function starts like below without keeping any assumptions.

Quoted 8 lines

     ib->ptr[ib->length_dw++] = <value>;

I don't see the complication with a code like this

     ib->length_dw += ptr - &ib->ptr[ib->length_dw];

It keeps the same behavior as before.

As such the patch logic conflicts itself.

For this to be true -
    ib->length_dw = ptr - ib->ptr;

Quoted 8 lines

It should start like.

    u32 *ptr = ib->ptr;

Now it starts like
    u32 *ptr = &ib->ptr[ib->length_dw];

So if you feel the new way is self contained, I would expect a change here as well. Otherwise, this could be read as a bug.

Not sure if I follow. Which places would you like me to use "ptr = ib->ptr"?

amdgpu_vcn_enc_get_create_msg & amdgpu_vcn_enc_get_destroy_msg actually do "ptr = &ib->ptr[ib->length_dw];" to handle amdgpu_vcn_unified_ring_ib_header().

Other callers have the ib passed in so it makes sense they do "ptr = &ib->ptr[ib->length_dw];".

Maybe at this point, to avoid misunderstandings, you could write exactly which call sites you recommend to do what?

Regards,

Tvrtko

Quoted 235 lines
Quoted 234 lines
Quoted 233 lines
Quoted 232 lines
Quoted 231 lines
Quoted 230 lines
Quoted 229 lines
Quoted 228 lines
Quoted 227 lines

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 134 ++++++++++++
+-----------

  1 file changed, 75 insertions(+), 59 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +3 -0
index 17db7264269e..2c367ee6e182 100644
@@ -622,9 +622,12 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
   {
       u64 addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       struct amdgpu_device *adev = ring->adev;
+    struct amdgpu_vcn_reg *internal = &adev->vcn.inst[ring- 
 >me].internal;
+    uint32_t nop = PACKET0(internal->nop, 0);
       struct dma_fence *f = NULL;
       struct amdgpu_job *job;
       struct amdgpu_ib *ib;
+    uint32_t *ptr;
       int i, r;

      r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL,

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +9 -8
@@ -635,15 +638,16 @@ static int amdgpu_vcn_dec_send_msg(struct 
 amdgpu_ring *ring,
           goto err;
       ib = &job->ibs[0];
-    ib->ptr[0] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.data0, 0);
-    ib->ptr[1] = addr;
-    ib->ptr[2] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.data1, 0);
-    ib->ptr[3] = addr >> 32;
-    ib->ptr[4] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.cmd, 0);
-    ib->ptr[5] = 0;
+    ptr = ib->ptr;
+    *ptr++ = PACKET0(internal->data0, 0);
+    *ptr++ = addr;
+    *ptr++ = PACKET0(internal->data1, 0);
+    *ptr++ = addr >> 32;
+    *ptr++ = PACKET0(internal->cmd, 0);
+    *ptr++ = 0;
       for (i = 6; i < 16; i += 2) {
-        ib->ptr[i] = PACKET0(adev->vcn.inst[ring- 
 >me].internal.nop, 0);
-        ib->ptr[i+1] = 0;
+        *ptr++ = nop;
+        *ptr++ = 0;

      }
      ib->length_dw = 16;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +1 -1
@@ -763,17 +767,20 @@ int amdgpu_vcn_dec_ring_test_ib(struct 
 amdgpu_ring *ring, long timeout)
   static uint32_t *amdgpu_vcn_unified_ring_ib_header(struct amdgpu_ib *ib,
                           uint32_t ib_pack_in_dw, bool enc)
   {
+    u32 *ptr = &ib->ptr[ib->length_dw];
       uint32_t *ib_checksum;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* single queue 

checksum */
-    ib->ptr[ib->length_dw++] = 0x30000002;
-    ib_checksum = &ib->ptr[ib->length_dw++];
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw;
+    *ptr++ = 0x00000010; /* single queue checksum */
+    *ptr++ = 0x30000002;
+    ib_checksum = ptr++;
+    *ptr++ = ib_pack_in_dw;
-    ib->ptr[ib->length_dw++] = 0x00000010; /* engine info */ -    ib->ptr[ib->length_dw++] = 0x30000001;
-    ib->ptr[ib->length_dw++] = enc ? 0x2 : 0x3;
-    ib->ptr[ib->length_dw++] = ib_pack_in_dw * sizeof(uint32_t); +    *ptr++ = 0x00000010; /* engine info */
+    *ptr++ = 0x30000001;
+    *ptr++ = enc ? 0x2 : 0x3;
+    *ptr++ = ib_pack_in_dw * sizeof(uint32_t);
+
+    ib->length_dw = ptr - ib->ptr;
      return ib_checksum;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +9 -5
@@ -803,7 +810,8 @@ static int 
 amdgpu_vcn_dec_sw_send_msg(struct amdgpu_ring *ring,
       uint64_t addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
       uint32_t *ib_checksum;
       uint32_t ib_pack_in_dw;
-    int i, r;
+    u32 *ptr;
+    int r;
       if (adev->vcn.inst[ring->me].using_unified_queue)
           ib_size_dw += 8;
@@ -825,18 +833,20 @@ static int 
 amdgpu_vcn_dec_sw_send_msg(struct amdgpu_ring *ring,
           ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, ib_pack_in_dw, false);
       }
-    ib->ptr[ib->length_dw++] = sizeof(struct 
 amdgpu_vcn_decode_buffer) + 8;
-    ib->ptr[ib->length_dw++] = 
 cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
-    decode_buffer = (struct amdgpu_vcn_decode_buffer *)&(ib- 
 >ptr[ib- >length_dw]);
-    ib->length_dw += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ptr = &ib->ptr[ib->length_dw];
+
+    *ptr++ = sizeof(struct amdgpu_vcn_decode_buffer) + 8;
+    *ptr++ = cpu_to_le32(AMDGPU_VCN_IB_FLAG_DECODE_BUFFER);
+    decode_buffer = (struct amdgpu_vcn_decode_buffer *)ptr;
+    ptr += sizeof(struct amdgpu_vcn_decode_buffer) / 4;
+    ib->length_dw = ptr - ib->ptr;
       memset(decode_buffer, 0, sizeof(struct amdgpu_vcn_decode_buffer));
       decode_buffer->valid_buf_flag |= cpu_to_le32(AMDGPU_VCN_CMD_FLAG_MSG_BUFFER);

      decode_buffer->msg_buffer_address_hi = cpu_to_le32(addr

Quoted 1 line
Quoted 1 line

32);

      decode_buffer->msg_buffer_address_lo = cpu_to_le32(addr); -    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);
      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, ib_pack_in_dw);

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -934,7 +944,8 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -947,31 +958,33 @@ static int 

amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t hand return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001; /* session info */ -    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001; /* session info */
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002; /* task info */
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000001; /* op initialize */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1002,7 +1015,8 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han struct dma_fence *f = NULL;
      uint32_t *ib_checksum = NULL;
      uint64_t addr;
-    int i, r;
+    u32 *ptr;
+    int r;
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_size_dw += 8;

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +0 -0
@@ -1015,31 +1029,33 @@ static int 

amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t han return r;
      ib = &job->ibs[0];
-    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
-
      ib->length_dw = 0;
+    addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg->gpu_addr);
      if (adev->vcn.inst[ring->me].using_unified_queue) ib_checksum = amdgpu_vcn_unified_ring_ib_header(ib, 0x11, true); -    ib->ptr[ib->length_dw++] = 0x00000018;
-    ib->ptr[ib->length_dw++] = 0x00000001;
-    ib->ptr[ib->length_dw++] = handle;
-    ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-    ib->ptr[ib->length_dw++] = addr;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    ptr = &ib->ptr[ib->length_dw];
-    ib->ptr[ib->length_dw++] = 0x00000014;
-    ib->ptr[ib->length_dw++] = 0x00000002;
-    ib->ptr[ib->length_dw++] = 0x0000001c;
-    ib->ptr[ib->length_dw++] = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000000;
+    *ptr++ = 0x00000018;
+    *ptr++ = 0x00000001;
+    *ptr++ = handle;
+    *ptr++ = upper_32_bits(addr);
+    *ptr++ = addr;
+    *ptr++ = 0x00000000;
-    ib->ptr[ib->length_dw++] = 0x00000008;
-    ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */ +    *ptr++ = 0x00000014;
+    *ptr++ = 0x00000002;
+    *ptr++ = 0x0000001c;
+    *ptr++ = 0x00000000;
+    *ptr++ = 0x00000000;
-    for (i = ib->length_dw; i < ib_size_dw; ++i)
-        ib->ptr[i] = 0x0;
+    *ptr++ = 0x00000008;
+    *ptr++ = 0x08000002; /* op close session */

drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +4 -0
+
+    ib->length_dw = ptr - ib->ptr;
+
+    memset32(ptr, 0, ib_size_dw - ib->length_dw);

      if (adev->vcn.inst[ring->me].using_unified_queue) amdgpu_vcn_unified_ring_ib_checksum(&ib_checksum, 0x11);

#13 lore raw

PATCH 2/18 drm/amdgpu: More compact VCE IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c | 96 +++++++++++++------------
 1 file changed, 50 insertions(+), 46 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vce.c +50 -46
index 1f7a98cdce5a..bed02782b983 100644
@@ -468,7 +468,8 @@ static int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
 	struct amdgpu_ib ib_msg;
 	struct dma_fence *f = NULL;
 	uint64_t addr;
-	int i, r;
+	u32 *ptr;
+	int r;
 
 	r = amdgpu_job_alloc_with_ib(ring->adev, &ring->adev->vce.entity,
 				     AMDGPU_FENCE_OWNER_UNDEFINED,
@@ -487,45 +488,46 @@ static int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
 		goto err;
 
 	ib = &job->ibs[0];
+	ptr = ib->ptr;
 	/* let addr point to page boundary */
 	addr = AMDGPU_GPU_PAGE_ALIGN(ib_msg.gpu_addr);
 
 	/* stitch together an VCE create msg */
-	ib->length_dw = 0;
-	ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
-	ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
-	ib->ptr[ib->length_dw++] = handle;
+	*ptr++ = 0x0000000c; /* len */
+	*ptr++ = 0x00000001; /* session cmd */
+	*ptr++ = handle;
 
 	if ((ring->adev->vce.fw_version >> 24) >= 52)
-		ib->ptr[ib->length_dw++] = 0x00000040; /* len */
+		*ptr++ = 0x00000040; /* len */
 	else
-		ib->ptr[ib->length_dw++] = 0x00000030; /* len */
-	ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
-	ib->ptr[ib->length_dw++] = 0x00000000;
-	ib->ptr[ib->length_dw++] = 0x00000042;
-	ib->ptr[ib->length_dw++] = 0x0000000a;
-	ib->ptr[ib->length_dw++] = 0x00000001;
-	ib->ptr[ib->length_dw++] = 0x00000080;
-	ib->ptr[ib->length_dw++] = 0x00000060;
-	ib->ptr[ib->length_dw++] = 0x00000100;
-	ib->ptr[ib->length_dw++] = 0x00000100;
-	ib->ptr[ib->length_dw++] = 0x0000000c;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+		*ptr++ = 0x00000030; /* len */
+	*ptr++ = 0x01000001; /* create cmd */
+	*ptr++ = 0x00000000;
+	*ptr++ = 0x00000042;
+	*ptr++ = 0x0000000a;
+	*ptr++ = 0x00000001;
+	*ptr++ = 0x00000080;
+	*ptr++ = 0x00000060;
+	*ptr++ = 0x00000100;
+	*ptr++ = 0x00000100;
+	*ptr++ = 0x0000000c;
+	*ptr++ = 0x00000000;
 	if ((ring->adev->vce.fw_version >> 24) >= 52) {
-		ib->ptr[ib->length_dw++] = 0x00000000;
-		ib->ptr[ib->length_dw++] = 0x00000000;
-		ib->ptr[ib->length_dw++] = 0x00000000;
-		ib->ptr[ib->length_dw++] = 0x00000000;
+		*ptr++ = 0x00000000;
+		*ptr++ = 0x00000000;
+		*ptr++ = 0x00000000;
+		*ptr++ = 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;
+	*ptr++ = 0x00000014; /* len */
+	*ptr++ = 0x05000005; /* feedback buffer */
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = addr;
+	*ptr++ = 0x00000001;
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	ib->length_dw = ptr - ib->ptr;
+
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
 	r = amdgpu_job_submit_direct(job, ring, &f);
 	amdgpu_ib_free(&ib_msg, f);
@@ -559,7 +561,8 @@ static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
 	struct amdgpu_job *job;
 	struct amdgpu_ib *ib;
 	struct dma_fence *f = NULL;
-	int i, r;
+	u32 *ptr;
+	int r;
 
 	r = amdgpu_job_alloc_with_ib(ring->adev, &ring->adev->vce.entity,
 				     AMDGPU_FENCE_OWNER_UNDEFINED,
@@ -572,27 +575,28 @@ static int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
 		return r;
 
 	ib = &job->ibs[0];
+	ptr = ib->ptr;
 
 	/* stitch together an VCE destroy msg */
-	ib->length_dw = 0;
-	ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
-	ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
-	ib->ptr[ib->length_dw++] = handle;
+	*ptr++ = 0x0000000c; /* len */
+	*ptr++ = 0x00000001; /* session cmd */
+	*ptr++ = handle;
 
-	ib->ptr[ib->length_dw++] = 0x00000020; /* len */
-	ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-	ib->ptr[ib->length_dw++] = 0xffffffff; /* next task info, set to 0xffffffff if no */
-	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++] = 0x00000000;
+	*ptr++ = 0x00000020; /* len */
+	*ptr++ = 0x00000002; /* task info */
+	*ptr++ = 0xffffffff; /* next task info, set to 0xffffffff if no */
+	*ptr++ = 0x00000001; /* destroy session */
+	*ptr++ = 0x00000000;
+	*ptr++ = 0x00000000;
+	*ptr++ = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */
+	*ptr++ = 0x00000000;
 
-	ib->ptr[ib->length_dw++] = 0x00000008; /* len */
-	ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
+	*ptr++ = 0x00000008; /* len */
+	*ptr++ = 0x02000001; /* destroy cmd */
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	ib->length_dw = ptr - ib->ptr;
+
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
 	if (direct)
 		r = amdgpu_job_submit_direct(job, ring, &f);
Signature · 1 line
2.55.0
#14 lore raw

PATCH 3/18 drm/amdgpu: More compact UVD 6 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.uvd 6

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/uvd_v6_0.c | 72 ++++++++++++++-------------
 1 file changed, 38 insertions(+), 34 deletions(-)
drivers/gpu/drm/amd/amdgpu/uvd_v6_0.c +38 -34
index dcb8487e22b1..2267323333e8 100644
@@ -214,7 +214,8 @@ static int uvd_v6_0_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t handle
 	struct amdgpu_ib *ib;
 	struct dma_fence *f = NULL;
 	uint64_t addr;
-	int i, r;
+	u32 *ptr;
+	int r;
 
 	r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL, ib_size_dw * 4,
 				     AMDGPU_IB_POOL_DIRECT,
@@ -224,27 +225,28 @@ static int uvd_v6_0_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t handle
 		return r;
 
 	ib = &job->ibs[0];
+	ptr = ib->ptr;
 	addr = amdgpu_bo_gpu_offset(bo);
 
-	ib->length_dw = 0;
-	ib->ptr[ib->length_dw++] = 0x00000018;
-	ib->ptr[ib->length_dw++] = 0x00000001; /* session info */
-	ib->ptr[ib->length_dw++] = handle;
-	ib->ptr[ib->length_dw++] = 0x00010000;
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = addr;
+	*ptr++ = 0x00000018;
+	*ptr++ = 0x00000001; /* session info */
+	*ptr++ = handle;
+	*ptr++ = 0x00010000;
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = addr;
 
-	ib->ptr[ib->length_dw++] = 0x00000014;
-	ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-	ib->ptr[ib->length_dw++] = 0x0000001c;
-	ib->ptr[ib->length_dw++] = 0x00000001;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	*ptr++ = 0x00000014;
+	*ptr++ = 0x00000002; /* task info */
+	*ptr++ = 0x0000001c;
+	*ptr++ = 0x00000001;
+	*ptr++ = 0x00000000;
 
-	ib->ptr[ib->length_dw++] = 0x00000008;
-	ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */
+	*ptr++ = 0x00000008;
+	*ptr++ = 0x08000001; /* op initialize */
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	ib->length_dw = ptr - ib->ptr;
+
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
 	r = amdgpu_job_submit_direct(job, ring, &f);
 	if (r)
@@ -280,7 +282,8 @@ static int uvd_v6_0_enc_get_destroy_msg(struct amdgpu_ring *ring,
 	struct amdgpu_ib *ib;
 	struct dma_fence *f = NULL;
 	uint64_t addr;
-	int i, r;
+	u32 *ptr;
+	int r;
 
 	r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL, ib_size_dw * 4,
 				     AMDGPU_IB_POOL_DIRECT,
@@ -290,27 +293,28 @@ static int uvd_v6_0_enc_get_destroy_msg(struct amdgpu_ring *ring,
 		return r;
 
 	ib = &job->ibs[0];
+	ptr = ib->ptr;
 	addr = amdgpu_bo_gpu_offset(bo);
 
-	ib->length_dw = 0;
-	ib->ptr[ib->length_dw++] = 0x00000018;
-	ib->ptr[ib->length_dw++] = 0x00000001; /* session info */
-	ib->ptr[ib->length_dw++] = handle;
-	ib->ptr[ib->length_dw++] = 0x00010000;
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = addr;
+	*ptr++ = 0x00000018;
+	*ptr++ = 0x00000001; /* session info */
+	*ptr++ = handle;
+	*ptr++ = 0x00010000;
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = addr;
 
-	ib->ptr[ib->length_dw++] = 0x00000014;
-	ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-	ib->ptr[ib->length_dw++] = 0x0000001c;
-	ib->ptr[ib->length_dw++] = 0x00000001;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	*ptr++ = 0x00000014;
+	*ptr++ = 0x00000002; /* task info */
+	*ptr++ = 0x0000001c;
+	*ptr++ = 0x00000001;
+	*ptr++ = 0x00000000;
 
-	ib->ptr[ib->length_dw++] = 0x00000008;
-	ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */
+	*ptr++ = 0x00000008;
+	*ptr++ = 0x08000002; /* op close session */
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	ib->length_dw = ptr - ib->ptr;
+
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
 	r = amdgpu_job_submit_direct(job, ring, &f);
 	if (r)
Signature · 1 line
2.55.0
#15 lore raw

PATCH 4/18 drm/amdgpu: More compact UVD 7 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/uvd_v7_0.c | 72 ++++++++++++++-------------
 1 file changed, 38 insertions(+), 34 deletions(-)
drivers/gpu/drm/amd/amdgpu/uvd_v7_0.c +38 -34
index 46222fc30be6..54adea94b42e 100644
@@ -222,7 +222,8 @@ static int uvd_v7_0_enc_get_create_msg(struct amdgpu_ring *ring, u32 handle,
 	struct amdgpu_ib *ib;
 	struct dma_fence *f = NULL;
 	uint64_t addr;
-	int i, r;
+	u32 *ptr;
+	int r;
 
 	r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL, ib_size_dw * 4,
 				     AMDGPU_IB_POOL_DIRECT,
@@ -232,27 +233,28 @@ static int uvd_v7_0_enc_get_create_msg(struct amdgpu_ring *ring, u32 handle,
 		return r;
 
 	ib = &job->ibs[0];
+	ptr = ib->ptr;
 	addr = amdgpu_bo_gpu_offset(bo);
 
-	ib->length_dw = 0;
-	ib->ptr[ib->length_dw++] = 0x00000018;
-	ib->ptr[ib->length_dw++] = 0x00000001; /* session info */
-	ib->ptr[ib->length_dw++] = handle;
-	ib->ptr[ib->length_dw++] = 0x00000000;
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = addr;
+	*ptr++ = 0x00000018;
+	*ptr++ = 0x00000001; /* session info */
+	*ptr++ = handle;
+	*ptr++ = 0x00000000;
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = addr;
 
-	ib->ptr[ib->length_dw++] = 0x00000014;
-	ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
-	ib->ptr[ib->length_dw++] = 0x0000001c;
-	ib->ptr[ib->length_dw++] = 0x00000000;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	*ptr++ = 0x00000014;
+	*ptr++ = 0x00000002; /* task info */
+	*ptr++ = 0x0000001c;
+	*ptr++ = 0x00000000;
+	*ptr++ = 0x00000000;
 
-	ib->ptr[ib->length_dw++] = 0x00000008;
-	ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */
+	*ptr++ = 0x00000008;
+	*ptr++ = 0x08000001; /* op initialize */
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	ib->length_dw = ptr - ib->ptr;
+
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
 	r = amdgpu_job_submit_direct(job, ring, &f);
 	if (r)
@@ -287,7 +289,8 @@ static int uvd_v7_0_enc_get_destroy_msg(struct amdgpu_ring *ring, u32 handle,
 	struct amdgpu_ib *ib;
 	struct dma_fence *f = NULL;
 	uint64_t addr;
-	int i, r;
+	u32 *ptr;
+	int r;
 
 	r = amdgpu_job_alloc_with_ib(ring->adev, NULL, NULL, ib_size_dw * 4,
 				     AMDGPU_IB_POOL_DIRECT,
@@ -297,27 +300,28 @@ static int uvd_v7_0_enc_get_destroy_msg(struct amdgpu_ring *ring, u32 handle,
 		return r;
 
 	ib = &job->ibs[0];
+	ptr = ib->ptr;
 	addr = amdgpu_bo_gpu_offset(bo);
 
-	ib->length_dw = 0;
-	ib->ptr[ib->length_dw++] = 0x00000018;
-	ib->ptr[ib->length_dw++] = 0x00000001;
-	ib->ptr[ib->length_dw++] = handle;
-	ib->ptr[ib->length_dw++] = 0x00000000;
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = addr;
+	*ptr++ = 0x00000018;
+	*ptr++ = 0x00000001;
+	*ptr++ = handle;
+	*ptr++ = 0x00000000;
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = addr;
 
-	ib->ptr[ib->length_dw++] = 0x00000014;
-	ib->ptr[ib->length_dw++] = 0x00000002;
-	ib->ptr[ib->length_dw++] = 0x0000001c;
-	ib->ptr[ib->length_dw++] = 0x00000000;
-	ib->ptr[ib->length_dw++] = 0x00000000;
+	*ptr++ = 0x00000014;
+	*ptr++ = 0x00000002;
+	*ptr++ = 0x0000001c;
+	*ptr++ = 0x00000000;
+	*ptr++ = 0x00000000;
 
-	ib->ptr[ib->length_dw++] = 0x00000008;
-	ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */
+	*ptr++ = 0x00000008;
+	*ptr++ = 0x08000002; /* op close session */
 
-	for (i = ib->length_dw; i < ib_size_dw; ++i)
-		ib->ptr[i] = 0x0;
+	ib->length_dw = ptr - ib->ptr;
+
+	memset32(ptr, 0, ib_size_dw - ib->length_dw);
 
 	r = amdgpu_job_submit_direct(job, ring, &f);
 	if (r)
Signature · 1 line
2.55.0
#16 lore raw

PATCH 5/18 drm/amdgpu: More compact SI SDMA emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/si_dma.c | 84 +++++++++++++++++------------
 1 file changed, 51 insertions(+), 33 deletions(-)
drivers/gpu/drm/amd/amdgpu/si_dma.c +51 -33
index 31ab701d45d3..333861204546 100644
@@ -328,14 +328,16 @@ static void si_dma_vm_copy_pte(struct amdgpu_ib *ib,
 			       uint64_t pe, uint64_t src,
 			       unsigned count)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned bytes = count * 8;
 
-	ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_COPY,
-					      1, 0, 0, bytes);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = lower_32_bits(src);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
-	ib->ptr[ib->length_dw++] = upper_32_bits(src) & 0xff;
+	*ptr++ = DMA_PACKET(DMA_PACKET_COPY, 1, 0, 0, bytes);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = lower_32_bits(src);
+	*ptr++ = upper_32_bits(pe) & 0xff;
+	*ptr++ = upper_32_bits(src) & 0xff;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -353,16 +355,19 @@ static void si_dma_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
 				uint64_t value, unsigned count,
 				uint32_t incr)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned ndw = count * 2;
 
-	ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 0, ndw);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
+	*ptr++ = DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 0, ndw);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
 	for (; ndw > 0; ndw -= 2) {
-		ib->ptr[ib->length_dw++] = lower_32_bits(value);
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
+		*ptr++ = lower_32_bits(value);
+		*ptr++ = upper_32_bits(value);
 		value += incr;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -382,6 +387,7 @@ static void si_dma_vm_set_pte_pde(struct amdgpu_ib *ib,
 				     uint64_t addr, unsigned count,
 				     uint32_t incr, uint64_t flags)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	uint64_t value;
 	unsigned ndw;
 
@@ -396,19 +402,21 @@ static void si_dma_vm_set_pte_pde(struct amdgpu_ib *ib,
 			value = 0;
 
 		/* for physically contiguous pages (vram) */
-		ib->ptr[ib->length_dw++] = DMA_PTE_PDE_PACKET(ndw);
-		ib->ptr[ib->length_dw++] = pe; /* dst addr */
-		ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
-		ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
-		ib->ptr[ib->length_dw++] = upper_32_bits(flags);
-		ib->ptr[ib->length_dw++] = value; /* value */
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
-		ib->ptr[ib->length_dw++] = incr; /* increment size */
-		ib->ptr[ib->length_dw++] = 0;
+		*ptr++ = DMA_PTE_PDE_PACKET(ndw);
+		*ptr++ = pe; /* dst addr */
+		*ptr++ = upper_32_bits(pe) & 0xff;
+		*ptr++ = lower_32_bits(flags); /* mask */
+		*ptr++ = upper_32_bits(flags);
+		*ptr++ = value; /* value */
+		*ptr++ = upper_32_bits(value);
+		*ptr++ = incr; /* increment size */
+		*ptr++ = 0;
 		pe += ndw * 4;
 		addr += (ndw / 2) * incr;
 		count -= ndw / 2;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -420,8 +428,12 @@ static void si_dma_vm_set_pte_pde(struct amdgpu_ib *ib,
  */
 static void si_dma_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
 {
-	while (ib->length_dw & 0x7)
-		ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0, 0);
+	int pad = 8 - (ib->length_dw & 0x7);
+
+	if (pad && pad < 8) {
+		memset32(ib->ptr, DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0, 0), pad);
+		ib->length_dw += pad;
+	}
 }
 
 /**
@@ -793,12 +805,15 @@ static void si_dma_emit_copy_buffer(struct amdgpu_ib *ib,
 				       uint32_t byte_count,
 				       uint32_t copy_flags)
 {
-	ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_COPY,
-					      1, 0, 0, byte_count);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset) & 0xff;
-	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset) & 0xff;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = DMA_PACKET(DMA_PACKET_COPY, 1, 0, 0, byte_count);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = lower_32_bits(src_offset);
+	*ptr++ = upper_32_bits(dst_offset) & 0xff;
+	*ptr++ = upper_32_bits(src_offset) & 0xff;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -816,11 +831,14 @@ static void si_dma_emit_fill_buffer(struct amdgpu_ib *ib,
 				       uint64_t dst_offset,
 				       uint32_t byte_count)
 {
-	ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_CONSTANT_FILL,
-					      0, 0, 0, byte_count / 4);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = src_data;
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset) << 16;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = DMA_PACKET(DMA_PACKET_CONSTANT_FILL, 0, 0, 0, byte_count / 4);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = src_data;
+	*ptr++ = upper_32_bits(dst_offset) << 16;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 
Signature · 1 line
2.55.0
#17 lore raw

PATCH 6/18 drm/amdgpu: More compact CIK SDMA IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/cik_sdma.c | 105 +++++++++++++++-----------
 1 file changed, 63 insertions(+), 42 deletions(-)
drivers/gpu/drm/amd/amdgpu/cik_sdma.c +63 -42
index 8a09fa912931..4c4c8a8e3eb9 100644
@@ -693,16 +693,18 @@ static void cik_sdma_vm_copy_pte(struct amdgpu_ib *ib,
 				 uint64_t pe, uint64_t src,
 				 unsigned count)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned bytes = count * 8;
 
-	ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_COPY,
-		SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
-	ib->ptr[ib->length_dw++] = bytes;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
+	*ptr++ = SDMA_PACKET(SDMA_OPCODE_COPY, SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
+	*ptr++ = bytes;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src);
+	*ptr++ = upper_32_bits(src);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -720,18 +722,21 @@ static void cik_sdma_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
 				  uint64_t value, unsigned count,
 				  uint32_t incr)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned ndw = count * 2;
 
-	ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_WRITE,
-		SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = ndw;
+	*ptr++ = SDMA_PACKET(SDMA_OPCODE_WRITE,
+			     SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = ndw;
 	for (; ndw > 0; ndw -= 2) {
-		ib->ptr[ib->length_dw++] = lower_32_bits(value);
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
+		*ptr++ = lower_32_bits(value);
+		*ptr++ = upper_32_bits(value);
 		value += incr;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -750,17 +755,21 @@ static void cik_sdma_vm_set_pte_pde(struct amdgpu_ib *ib, uint64_t pe,
 				    uint64_t addr, unsigned count,
 				    uint32_t incr, uint64_t flags)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
 	/* for physically contiguous pages (vram) */
-	ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_GENERATE_PTE_PDE, 0, 0);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
-	ib->ptr[ib->length_dw++] = upper_32_bits(flags);
-	ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = incr; /* increment size */
-	ib->ptr[ib->length_dw++] = 0;
-	ib->ptr[ib->length_dw++] = count; /* number of entries */
+	*ptr++ = SDMA_PACKET(SDMA_OPCODE_GENERATE_PTE_PDE, 0, 0);
+	*ptr++ = lower_32_bits(pe); /* dst addr */
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = lower_32_bits(flags); /* mask */
+	*ptr++ = upper_32_bits(flags);
+	*ptr++ = lower_32_bits(addr); /* value */
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = incr; /* increment size */
+	*ptr++ = 0;
+	*ptr++ = count; /* number of entries */
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -773,18 +782,22 @@ static void cik_sdma_vm_set_pte_pde(struct amdgpu_ib *ib, uint64_t pe,
 static void cik_sdma_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
 {
 	struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	u32 pad_count;
 	int i;
 
 	pad_count = (-ib->length_dw) & 7;
+	if (!pad_count)
+		return;
+
 	for (i = 0; i < pad_count; i++)
 		if (sdma && sdma->burst_nop && (i == 0))
-			ib->ptr[ib->length_dw++] =
-					SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0) |
-					SDMA_NOP_COUNT(pad_count - 1);
+			*ptr++ = SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0) |
+				 SDMA_NOP_COUNT(pad_count - 1);
 		else
-			ib->ptr[ib->length_dw++] =
-					SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0);
+			*ptr++ = SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0);
+
+	ib->length_dw += pad_count;
 }
 
 /**
@@ -1278,13 +1291,17 @@ static void cik_sdma_emit_copy_buffer(struct amdgpu_ib *ib,
 				      uint32_t byte_count,
 				      uint32_t copy_flags)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_COPY, SDMA_COPY_SUB_OPCODE_LINEAR, 0);
-	ib->ptr[ib->length_dw++] = byte_count;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PACKET(SDMA_OPCODE_COPY, SDMA_COPY_SUB_OPCODE_LINEAR, 0);
+	*ptr++ = byte_count;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src_offset);
+	*ptr++ = upper_32_bits(src_offset);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1302,11 +1319,15 @@ static void cik_sdma_emit_fill_buffer(struct amdgpu_ib *ib,
 				      uint64_t dst_offset,
 				      uint32_t byte_count)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_CONSTANT_FILL, 0, 0);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = src_data;
-	ib->ptr[ib->length_dw++] = byte_count;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PACKET(SDMA_OPCODE_CONSTANT_FILL, 0, 0);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+	*ptr++ = src_data;
+	*ptr++ = byte_count;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 static const struct amdgpu_buffer_funcs cik_sdma_buffer_funcs = {
Signature · 1 line
2.55.0
#18 lore raw

PATCH 7/18 drm/amdgpu: More compact GFX8 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c | 106 +++++++++++++-------------
 1 file changed, 53 insertions(+), 53 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v8_0.c +53 -53
index ab4123ddbb94..86319810e16e 100644
@@ -1477,8 +1477,8 @@ static int gfx_v8_0_do_edc_gpr_workarounds(struct amdgpu_device *adev)
 	struct amdgpu_job *job;
 	struct amdgpu_ib *ib;
 	struct dma_fence *f = NULL;
+	u32 tmp, *ptr;
 	int r, i;
-	u32 tmp;
 	unsigned total_size, vgpr_offset, sgpr_offset;
 	u64 gpu_addr;
 
@@ -1524,92 +1524,92 @@ static int gfx_v8_0_do_edc_gpr_workarounds(struct amdgpu_device *adev)
 	ib = &job->ibs[0];
 
 	/* load the compute shaders */
-	for (i = 0; i < ARRAY_SIZE(vgpr_init_compute_shader); i++)
-		ib->ptr[i + (vgpr_offset / 4)] = vgpr_init_compute_shader[i];
+	memcpy(&ib->ptr[vgpr_offset / sizeof(u32)], vgpr_init_compute_shader,
+	       sizeof(vgpr_init_compute_shader));
+	memcpy(&ib->ptr[sgpr_offset / sizeof(u32)], sgpr_init_compute_shader,
+	       sizeof(sgpr_init_compute_shader));
 
-	for (i = 0; i < ARRAY_SIZE(sgpr_init_compute_shader); i++)
-		ib->ptr[i + (sgpr_offset / 4)] = sgpr_init_compute_shader[i];
-
-	/* init the ib length to 0 */
-	ib->length_dw = 0;
+	ptr = ib->ptr;
 
 	/* VGPR */
 	/* write the register state for the compute dispatch */
 	for (i = 0; i < ARRAY_SIZE(vgpr_init_regs); i += 2) {
-		ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 1);
-		ib->ptr[ib->length_dw++] = vgpr_init_regs[i] - PACKET3_SET_SH_REG_START;
-		ib->ptr[ib->length_dw++] = vgpr_init_regs[i + 1];
+		*ptr++ = PACKET3(PACKET3_SET_SH_REG, 1);
+		*ptr++ = vgpr_init_regs[i] - PACKET3_SET_SH_REG_START;
+		*ptr++ = vgpr_init_regs[i + 1];
 	}
 	/* write the shader start address: mmCOMPUTE_PGM_LO, mmCOMPUTE_PGM_HI */
 	gpu_addr = (ib->gpu_addr + (u64)vgpr_offset) >> 8;
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 2);
-	ib->ptr[ib->length_dw++] = mmCOMPUTE_PGM_LO - PACKET3_SET_SH_REG_START;
-	ib->ptr[ib->length_dw++] = lower_32_bits(gpu_addr);
-	ib->ptr[ib->length_dw++] = upper_32_bits(gpu_addr);
+	*ptr++ = PACKET3(PACKET3_SET_SH_REG, 2);
+	*ptr++ = mmCOMPUTE_PGM_LO - PACKET3_SET_SH_REG_START;
+	*ptr++ = lower_32_bits(gpu_addr);
+	*ptr++ = upper_32_bits(gpu_addr);
 
 	/* write dispatch packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
-	ib->ptr[ib->length_dw++] = 8; /* x */
-	ib->ptr[ib->length_dw++] = 1; /* y */
-	ib->ptr[ib->length_dw++] = 1; /* z */
-	ib->ptr[ib->length_dw++] =
-		REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN, 1);
+	*ptr++ = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
+	*ptr++ = 8; /* x */
+	*ptr++ = 1; /* y */
+	*ptr++ = 1; /* z */
+	*ptr++ = REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN,
+			       1);
 
 	/* write CS partial flush packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_EVENT_WRITE, 0);
-	ib->ptr[ib->length_dw++] = EVENT_TYPE(7) | EVENT_INDEX(4);
+	*ptr++ = PACKET3(PACKET3_EVENT_WRITE, 0);
+	*ptr++ = EVENT_TYPE(7) | EVENT_INDEX(4);
 
 	/* SGPR1 */
 	/* write the register state for the compute dispatch */
 	for (i = 0; i < ARRAY_SIZE(sgpr1_init_regs); i += 2) {
-		ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 1);
-		ib->ptr[ib->length_dw++] = sgpr1_init_regs[i] - PACKET3_SET_SH_REG_START;
-		ib->ptr[ib->length_dw++] = sgpr1_init_regs[i + 1];
+		*ptr++ = PACKET3(PACKET3_SET_SH_REG, 1);
+		*ptr++ = sgpr1_init_regs[i] - PACKET3_SET_SH_REG_START;
+		*ptr++ = sgpr1_init_regs[i + 1];
 	}
 	/* write the shader start address: mmCOMPUTE_PGM_LO, mmCOMPUTE_PGM_HI */
 	gpu_addr = (ib->gpu_addr + (u64)sgpr_offset) >> 8;
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 2);
-	ib->ptr[ib->length_dw++] = mmCOMPUTE_PGM_LO - PACKET3_SET_SH_REG_START;
-	ib->ptr[ib->length_dw++] = lower_32_bits(gpu_addr);
-	ib->ptr[ib->length_dw++] = upper_32_bits(gpu_addr);
+	*ptr++ = PACKET3(PACKET3_SET_SH_REG, 2);
+	*ptr++ = mmCOMPUTE_PGM_LO - PACKET3_SET_SH_REG_START;
+	*ptr++ = lower_32_bits(gpu_addr);
+	*ptr++ = upper_32_bits(gpu_addr);
 
 	/* write dispatch packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
-	ib->ptr[ib->length_dw++] = 8; /* x */
-	ib->ptr[ib->length_dw++] = 1; /* y */
-	ib->ptr[ib->length_dw++] = 1; /* z */
-	ib->ptr[ib->length_dw++] =
-		REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN, 1);
+	*ptr++ = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
+	*ptr++ = 8; /* x */
+	*ptr++ = 1; /* y */
+	*ptr++ = 1; /* z */
+	*ptr++ = REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN,
+			       1);
 
 	/* write CS partial flush packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_EVENT_WRITE, 0);
-	ib->ptr[ib->length_dw++] = EVENT_TYPE(7) | EVENT_INDEX(4);
+	*ptr++ = PACKET3(PACKET3_EVENT_WRITE, 0);
+	*ptr++ = EVENT_TYPE(7) | EVENT_INDEX(4);
 
 	/* SGPR2 */
 	/* write the register state for the compute dispatch */
 	for (i = 0; i < ARRAY_SIZE(sgpr2_init_regs); i += 2) {
-		ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 1);
-		ib->ptr[ib->length_dw++] = sgpr2_init_regs[i] - PACKET3_SET_SH_REG_START;
-		ib->ptr[ib->length_dw++] = sgpr2_init_regs[i + 1];
+		*ptr++ = PACKET3(PACKET3_SET_SH_REG, 1);
+		*ptr++ = sgpr2_init_regs[i] - PACKET3_SET_SH_REG_START;
+		*ptr++ = sgpr2_init_regs[i + 1];
 	}
 	/* write the shader start address: mmCOMPUTE_PGM_LO, mmCOMPUTE_PGM_HI */
 	gpu_addr = (ib->gpu_addr + (u64)sgpr_offset) >> 8;
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 2);
-	ib->ptr[ib->length_dw++] = mmCOMPUTE_PGM_LO - PACKET3_SET_SH_REG_START;
-	ib->ptr[ib->length_dw++] = lower_32_bits(gpu_addr);
-	ib->ptr[ib->length_dw++] = upper_32_bits(gpu_addr);
+	*ptr++ = PACKET3(PACKET3_SET_SH_REG, 2);
+	*ptr++ = mmCOMPUTE_PGM_LO - PACKET3_SET_SH_REG_START;
+	*ptr++ = lower_32_bits(gpu_addr);
+	*ptr++ = upper_32_bits(gpu_addr);
 
 	/* write dispatch packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
-	ib->ptr[ib->length_dw++] = 8; /* x */
-	ib->ptr[ib->length_dw++] = 1; /* y */
-	ib->ptr[ib->length_dw++] = 1; /* z */
-	ib->ptr[ib->length_dw++] =
-		REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN, 1);
+	*ptr++ = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
+	*ptr++ = 8; /* x */
+	*ptr++ = 1; /* y */
+	*ptr++ = 1; /* z */
+	*ptr++ = REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN,
+			       1);
 
 	/* write CS partial flush packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_EVENT_WRITE, 0);
-	ib->ptr[ib->length_dw++] = EVENT_TYPE(7) | EVENT_INDEX(4);
+	*ptr++ = PACKET3(PACKET3_EVENT_WRITE, 0);
+	*ptr++ = EVENT_TYPE(7) | EVENT_INDEX(4);
+
+	ib->length_dw = ptr - ib->ptr;
 
 	/* schedule the ib on the ring */
 	r = amdgpu_job_submit_direct(job, ring, &f);
Signature · 1 line
2.55.0
#19 lore raw

PATCH 8/18 drm/amdgpu: More compact GFX 9.4.2 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/gfx_v9_4_2.c | 51 +++++++++++++------------
 1 file changed, 26 insertions(+), 25 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v9_4_2.c +26 -25
index 344156bf20ca..3c33772591d0 100644
@@ -377,6 +377,7 @@ static int gfx_v9_4_2_run_shader(struct amdgpu_device *adev,
 	int r, i;
 	uint32_t total_size, shader_offset;
 	u64 gpu_addr;
+	u32 *ptr;
 
 	total_size = (regs_size * 3 + 4 + 5 + 5) * 4;
 	total_size = ALIGN(total_size, 256);
@@ -394,43 +395,43 @@ static int gfx_v9_4_2_run_shader(struct amdgpu_device *adev,
 	ib = &job->ibs[0];
 
 	/* load the compute shaders */
-	for (i = 0; i < shader_size/sizeof(u32); i++)
-		ib->ptr[i + (shader_offset / 4)] = shader_ptr[i];
+	memcpy(&ib->ptr[shader_offset / sizeof(u32)], shader_ptr, shader_size);
 
-	/* init the ib length to 0 */
-	ib->length_dw = 0;
+	ptr = ib->ptr;
 
 	/* write the register state for the compute dispatch */
 	for (i = 0; i < regs_size; i++) {
-		ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 1);
-		ib->ptr[ib->length_dw++] = SOC15_REG_ENTRY_OFFSET(init_regs[i])
-								- PACKET3_SET_SH_REG_START;
-		ib->ptr[ib->length_dw++] = init_regs[i].reg_value;
+		*ptr++ = PACKET3(PACKET3_SET_SH_REG, 1);
+		*ptr++ = SOC15_REG_ENTRY_OFFSET(init_regs[i]) -
+			 PACKET3_SET_SH_REG_START;
+		*ptr++ = init_regs[i].reg_value;
 	}
 
 	/* write the shader start address: mmCOMPUTE_PGM_LO, mmCOMPUTE_PGM_HI */
 	gpu_addr = (ib->gpu_addr + (u64)shader_offset) >> 8;
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 2);
-	ib->ptr[ib->length_dw++] = SOC15_REG_OFFSET(GC, 0, regCOMPUTE_PGM_LO)
-							- PACKET3_SET_SH_REG_START;
-	ib->ptr[ib->length_dw++] = lower_32_bits(gpu_addr);
-	ib->ptr[ib->length_dw++] = upper_32_bits(gpu_addr);
+	*ptr++ = PACKET3(PACKET3_SET_SH_REG, 2);
+	*ptr++ = SOC15_REG_OFFSET(GC, 0, regCOMPUTE_PGM_LO)
+		 - PACKET3_SET_SH_REG_START;
+	*ptr++ = lower_32_bits(gpu_addr);
+	*ptr++ = upper_32_bits(gpu_addr);
 
 	/* write the wb buffer address */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 3);
-	ib->ptr[ib->length_dw++] = SOC15_REG_OFFSET(GC, 0, regCOMPUTE_USER_DATA_0)
-							- PACKET3_SET_SH_REG_START;
-	ib->ptr[ib->length_dw++] = lower_32_bits(wb_gpu_addr);
-	ib->ptr[ib->length_dw++] = upper_32_bits(wb_gpu_addr);
-	ib->ptr[ib->length_dw++] = pattern;
+	*ptr++ = PACKET3(PACKET3_SET_SH_REG, 3);
+	*ptr++ = SOC15_REG_OFFSET(GC, 0, regCOMPUTE_USER_DATA_0) -
+		 PACKET3_SET_SH_REG_START;
+	*ptr++ = lower_32_bits(wb_gpu_addr);
+	*ptr++ = upper_32_bits(wb_gpu_addr);
+	*ptr++ = pattern;
 
 	/* write dispatch packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
-	ib->ptr[ib->length_dw++] = compute_dim_x; /* x */
-	ib->ptr[ib->length_dw++] = 1; /* y */
-	ib->ptr[ib->length_dw++] = 1; /* z */
-	ib->ptr[ib->length_dw++] =
-		REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN, 1);
+	*ptr++ = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
+	*ptr++ = compute_dim_x; /* x */
+	*ptr++ = 1; /* y */
+	*ptr++ = 1; /* z */
+	*ptr++ = REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN,
+			       1);
+
+	ib->length_dw = ptr - ib->ptr;
 
 	/* schedule the ib on the ring */
 	r = amdgpu_job_submit_direct(job, ring, fence_ptr);
Signature · 1 line
2.55.0
#20 lore raw

PATCH 9/18 drm/amdgpu: More compact GFX9 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c | 115 +++++++++++++-------------
 1 file changed, 58 insertions(+), 57 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +58 -57
index 3ba2584e48c6..60f410ab1afa 100644
@@ -4703,6 +4703,7 @@ static int gfx_v9_0_do_edc_gpr_workarounds(struct amdgpu_device *adev)
 	int vgpr_init_shader_size;
 	const u32 *vgpr_init_shader_ptr;
 	const struct soc15_reg_entry *vgpr_init_regs_ptr;
+	u32 *ptr;
 
 	/* only support when RAS is enabled */
 	if (!amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__GFX))
@@ -4746,98 +4747,98 @@ static int gfx_v9_0_do_edc_gpr_workarounds(struct amdgpu_device *adev)
 	ib = &job->ibs[0];
 
 	/* load the compute shaders */
-	for (i = 0; i < vgpr_init_shader_size/sizeof(u32); i++)
-		ib->ptr[i + (vgpr_offset / 4)] = vgpr_init_shader_ptr[i];
+	memcpy(&ib->ptr[vgpr_offset / sizeof(u32)], vgpr_init_shader_ptr,
+	       vgpr_init_shader_size);
+	memcpy(&ib->ptr[sgpr_offset / sizeof(u32)], sgpr_init_compute_shader,
+	       sizeof(sgpr_init_compute_shader));
 
-	for (i = 0; i < ARRAY_SIZE(sgpr_init_compute_shader); i++)
-		ib->ptr[i + (sgpr_offset / 4)] = sgpr_init_compute_shader[i];
-
-	/* init the ib length to 0 */
-	ib->length_dw = 0;
+	ptr = ib->ptr;
 
 	/* VGPR */
 	/* write the register state for the compute dispatch */
 	for (i = 0; i < gpr_reg_size; i++) {
-		ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 1);
-		ib->ptr[ib->length_dw++] = SOC15_REG_ENTRY_OFFSET(vgpr_init_regs_ptr[i])
-								- PACKET3_SET_SH_REG_START;
-		ib->ptr[ib->length_dw++] = vgpr_init_regs_ptr[i].reg_value;
+		*ptr++ = PACKET3(PACKET3_SET_SH_REG, 1);
+		*ptr++ = SOC15_REG_ENTRY_OFFSET(vgpr_init_regs_ptr[i]) -
+			 PACKET3_SET_SH_REG_START;
+		*ptr++ = vgpr_init_regs_ptr[i].reg_value;
 	}
 	/* write the shader start address: mmCOMPUTE_PGM_LO, mmCOMPUTE_PGM_HI */
 	gpu_addr = (ib->gpu_addr + (u64)vgpr_offset) >> 8;
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 2);
-	ib->ptr[ib->length_dw++] = SOC15_REG_OFFSET(GC, 0, mmCOMPUTE_PGM_LO)
-							- PACKET3_SET_SH_REG_START;
-	ib->ptr[ib->length_dw++] = lower_32_bits(gpu_addr);
-	ib->ptr[ib->length_dw++] = upper_32_bits(gpu_addr);
+	*ptr++ = PACKET3(PACKET3_SET_SH_REG, 2);
+	*ptr++ = SOC15_REG_OFFSET(GC, 0, mmCOMPUTE_PGM_LO) -
+		 PACKET3_SET_SH_REG_START;
+	*ptr++ = lower_32_bits(gpu_addr);
+	*ptr++ = upper_32_bits(gpu_addr);
 
 	/* write dispatch packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
-	ib->ptr[ib->length_dw++] = compute_dim_x * 2; /* x */
-	ib->ptr[ib->length_dw++] = 1; /* y */
-	ib->ptr[ib->length_dw++] = 1; /* z */
-	ib->ptr[ib->length_dw++] =
-		REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN, 1);
+	*ptr++ = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
+	*ptr++ = compute_dim_x * 2; /* x */
+	*ptr++ = 1; /* y */
+	*ptr++ = 1; /* z */
+	*ptr++ = REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN,
+			       1);
 
 	/* write CS partial flush packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_EVENT_WRITE, 0);
-	ib->ptr[ib->length_dw++] = EVENT_TYPE(7) | EVENT_INDEX(4);
+	*ptr++ = PACKET3(PACKET3_EVENT_WRITE, 0);
+	*ptr++ = EVENT_TYPE(7) | EVENT_INDEX(4);
 
 	/* SGPR1 */
 	/* write the register state for the compute dispatch */
 	for (i = 0; i < gpr_reg_size; i++) {
-		ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 1);
-		ib->ptr[ib->length_dw++] = SOC15_REG_ENTRY_OFFSET(sgpr1_init_regs[i])
-								- PACKET3_SET_SH_REG_START;
-		ib->ptr[ib->length_dw++] = sgpr1_init_regs[i].reg_value;
+		*ptr++ = PACKET3(PACKET3_SET_SH_REG, 1);
+		*ptr++ = SOC15_REG_ENTRY_OFFSET(sgpr1_init_regs[i]) -
+			 PACKET3_SET_SH_REG_START;
+		*ptr++ = sgpr1_init_regs[i].reg_value;
 	}
 	/* write the shader start address: mmCOMPUTE_PGM_LO, mmCOMPUTE_PGM_HI */
 	gpu_addr = (ib->gpu_addr + (u64)sgpr_offset) >> 8;
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 2);
-	ib->ptr[ib->length_dw++] = SOC15_REG_OFFSET(GC, 0, mmCOMPUTE_PGM_LO)
-							- PACKET3_SET_SH_REG_START;
-	ib->ptr[ib->length_dw++] = lower_32_bits(gpu_addr);
-	ib->ptr[ib->length_dw++] = upper_32_bits(gpu_addr);
+	*ptr++ = PACKET3(PACKET3_SET_SH_REG, 2);
+	*ptr++ = SOC15_REG_OFFSET(GC, 0, mmCOMPUTE_PGM_LO) -
+		 PACKET3_SET_SH_REG_START;
+	*ptr++ = lower_32_bits(gpu_addr);
+	*ptr++ = upper_32_bits(gpu_addr);
 
 	/* write dispatch packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
-	ib->ptr[ib->length_dw++] = compute_dim_x / 2 * sgpr_work_group_size; /* x */
-	ib->ptr[ib->length_dw++] = 1; /* y */
-	ib->ptr[ib->length_dw++] = 1; /* z */
-	ib->ptr[ib->length_dw++] =
-		REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN, 1);
+	*ptr++ = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
+	*ptr++ = compute_dim_x / 2 * sgpr_work_group_size; /* x */
+	*ptr++ = 1; /* y */
+	*ptr++ = 1; /* z */
+	*ptr++ = REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN,
+			       1);
 
 	/* write CS partial flush packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_EVENT_WRITE, 0);
-	ib->ptr[ib->length_dw++] = EVENT_TYPE(7) | EVENT_INDEX(4);
+	*ptr++ = PACKET3(PACKET3_EVENT_WRITE, 0);
+	*ptr++ = EVENT_TYPE(7) | EVENT_INDEX(4);
 
 	/* SGPR2 */
 	/* write the register state for the compute dispatch */
 	for (i = 0; i < gpr_reg_size; i++) {
-		ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 1);
-		ib->ptr[ib->length_dw++] = SOC15_REG_ENTRY_OFFSET(sgpr2_init_regs[i])
-								- PACKET3_SET_SH_REG_START;
-		ib->ptr[ib->length_dw++] = sgpr2_init_regs[i].reg_value;
+		*ptr++ = PACKET3(PACKET3_SET_SH_REG, 1);
+		*ptr++ = SOC15_REG_ENTRY_OFFSET(sgpr2_init_regs[i]) -
+			 PACKET3_SET_SH_REG_START;
+		*ptr++ = sgpr2_init_regs[i].reg_value;
 	}
 	/* write the shader start address: mmCOMPUTE_PGM_LO, mmCOMPUTE_PGM_HI */
 	gpu_addr = (ib->gpu_addr + (u64)sgpr_offset) >> 8;
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_SET_SH_REG, 2);
-	ib->ptr[ib->length_dw++] = SOC15_REG_OFFSET(GC, 0, mmCOMPUTE_PGM_LO)
+	*ptr++ = PACKET3(PACKET3_SET_SH_REG, 2);
+	*ptr++ = SOC15_REG_OFFSET(GC, 0, mmCOMPUTE_PGM_LO)
 							- PACKET3_SET_SH_REG_START;
-	ib->ptr[ib->length_dw++] = lower_32_bits(gpu_addr);
-	ib->ptr[ib->length_dw++] = upper_32_bits(gpu_addr);
+	*ptr++ = lower_32_bits(gpu_addr);
+	*ptr++ = upper_32_bits(gpu_addr);
 
 	/* write dispatch packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
-	ib->ptr[ib->length_dw++] = compute_dim_x / 2 * sgpr_work_group_size; /* x */
-	ib->ptr[ib->length_dw++] = 1; /* y */
-	ib->ptr[ib->length_dw++] = 1; /* z */
-	ib->ptr[ib->length_dw++] =
-		REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN, 1);
+	*ptr++ = PACKET3(PACKET3_DISPATCH_DIRECT, 3);
+	*ptr++ = compute_dim_x / 2 * sgpr_work_group_size; /* x */
+	*ptr++ = 1; /* y */
+	*ptr++ = 1; /* z */
+	*ptr++ = REG_SET_FIELD(0, COMPUTE_DISPATCH_INITIATOR, COMPUTE_SHADER_EN,
+			       1);
 
 	/* write CS partial flush packet */
-	ib->ptr[ib->length_dw++] = PACKET3(PACKET3_EVENT_WRITE, 0);
-	ib->ptr[ib->length_dw++] = EVENT_TYPE(7) | EVENT_INDEX(4);
+	*ptr++ = PACKET3(PACKET3_EVENT_WRITE, 0);
+	*ptr++ = EVENT_TYPE(7) | EVENT_INDEX(4);
+
+	ib->length_dw = ptr - ib->ptr;
 
 	/* schedule the ib on the ring */
 	r = amdgpu_job_submit_direct(job, ring, &f);
Signature · 1 line
2.55.0
#21 lore raw

PATCH 10/18 drm/amdgpu: More compact SDMA 2.4 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/sdma_v2_4.c | 108 +++++++++++++++----------
 1 file changed, 65 insertions(+), 43 deletions(-)
drivers/gpu/drm/amd/amdgpu/sdma_v2_4.c +65 -43
index 6b64cf83ee5c..fc5de1cbf2f1 100644
@@ -629,16 +629,19 @@ static void sdma_v2_4_vm_copy_pte(struct amdgpu_ib *ib,
 				  uint64_t pe, uint64_t src,
 				  unsigned count)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned bytes = count * 8;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
-	ib->ptr[ib->length_dw++] = bytes;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
+	*ptr++ = bytes;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src);
+	*ptr++ = upper_32_bits(src);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -656,18 +659,21 @@ static void sdma_v2_4_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
 				   uint64_t value, unsigned count,
 				   uint32_t incr)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned ndw = count * 2;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
-	ib->ptr[ib->length_dw++] = pe;
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = ndw;
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
+	*ptr++ = pe;
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = ndw;
 	for (; ndw > 0; ndw -= 2) {
-		ib->ptr[ib->length_dw++] = lower_32_bits(value);
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
+		*ptr++ = lower_32_bits(value);
+		*ptr++ = upper_32_bits(value);
 		value += incr;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -686,17 +692,21 @@ static void sdma_v2_4_vm_set_pte_pde(struct amdgpu_ib *ib, uint64_t pe,
 				     uint64_t addr, unsigned count,
 				     uint32_t incr, uint64_t flags)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
 	/* for physically contiguous pages (vram) */
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_GEN_PTEPDE);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
-	ib->ptr[ib->length_dw++] = upper_32_bits(flags);
-	ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = incr; /* increment size */
-	ib->ptr[ib->length_dw++] = 0;
-	ib->ptr[ib->length_dw++] = count; /* number of entries */
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_GEN_PTEPDE);
+	*ptr++ = lower_32_bits(pe); /* dst addr */
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = lower_32_bits(flags); /* mask */
+	*ptr++ = upper_32_bits(flags);
+	*ptr++ = lower_32_bits(addr); /* value */
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = incr; /* increment size */
+	*ptr++ = 0;
+	*ptr++ = count; /* number of entries */
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -710,18 +720,22 @@ static void sdma_v2_4_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib
 {
 	struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
 	const bool burst_nop = sdma->burst_nop;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	u32 pad_count;
 	int i;
 
 	pad_count = (-ib->length_dw) & 7;
+	if (!pad_count)
+		return;
+
 	for (i = 0; i < pad_count; i++)
 		if (i == 0 && burst_nop)
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
-				SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
+				 SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
 		else
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+
+	ib->length_dw += pad_count;
 }
 
 /**
@@ -1151,14 +1165,18 @@ static void sdma_v2_4_emit_copy_buffer(struct amdgpu_ib *ib,
 				       uint32_t byte_count,
 				       uint32_t copy_flags)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
-	ib->ptr[ib->length_dw++] = byte_count;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
+	*ptr++ = byte_count;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src_offset);
+	*ptr++ = upper_32_bits(src_offset);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1176,11 +1194,15 @@ static void sdma_v2_4_emit_fill_buffer(struct amdgpu_ib *ib,
 				       uint64_t dst_offset,
 				       uint32_t byte_count)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = src_data;
-	ib->ptr[ib->length_dw++] = byte_count;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+	*ptr++ = src_data;
+	*ptr++ = byte_count;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 static const struct amdgpu_buffer_funcs sdma_v2_4_buffer_funcs = {
Signature · 1 line
2.55.0
#22 lore raw

PATCH 11/18 drm/amdgpu: More compact SDMA 3.0 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/sdma_v3_0.c | 108 +++++++++++++++----------
 1 file changed, 65 insertions(+), 43 deletions(-)
drivers/gpu/drm/amd/amdgpu/sdma_v3_0.c +65 -43
index 6f7243370acb..0f12d0b0f631 100644
@@ -903,16 +903,19 @@ static void sdma_v3_0_vm_copy_pte(struct amdgpu_ib *ib,
 				  uint64_t pe, uint64_t src,
 				  unsigned count)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned bytes = count * 8;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
-	ib->ptr[ib->length_dw++] = bytes;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
+	*ptr++ = bytes;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src);
+	*ptr++ = upper_32_bits(src);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -930,18 +933,21 @@ static void sdma_v3_0_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
 				   uint64_t value, unsigned count,
 				   uint32_t incr)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned ndw = count * 2;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = ndw;
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = ndw;
 	for (; ndw > 0; ndw -= 2) {
-		ib->ptr[ib->length_dw++] = lower_32_bits(value);
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
+		*ptr++ = lower_32_bits(value);
+		*ptr++ = upper_32_bits(value);
 		value += incr;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -960,17 +966,21 @@ static void sdma_v3_0_vm_set_pte_pde(struct amdgpu_ib *ib, uint64_t pe,
 				     uint64_t addr, unsigned count,
 				     uint32_t incr, uint64_t flags)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
 	/* for physically contiguous pages (vram) */
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_GEN_PTEPDE);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
-	ib->ptr[ib->length_dw++] = upper_32_bits(flags);
-	ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = incr; /* increment size */
-	ib->ptr[ib->length_dw++] = 0;
-	ib->ptr[ib->length_dw++] = count; /* number of entries */
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_GEN_PTEPDE);
+	*ptr++ = lower_32_bits(pe); /* dst addr */
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = lower_32_bits(flags); /* mask */
+	*ptr++ = upper_32_bits(flags);
+	*ptr++ = lower_32_bits(addr); /* value */
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = incr; /* increment size */
+	*ptr++ = 0;
+	*ptr++ = count; /* number of entries */
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -984,18 +994,22 @@ static void sdma_v3_0_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib
 {
 	struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
 	const bool burst_nop = sdma->burst_nop;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	u32 pad_count;
 	int i;
 
 	pad_count = (-ib->length_dw) & 7;
+	if (!pad_count)
+		return;
+
 	for (i = 0; i < pad_count; i++)
 		if (i == 0 && burst_nop)
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
-				SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
+				 SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
 		else
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+
+	ib->length_dw = pad_count;
 }
 
 /**
@@ -1530,14 +1544,18 @@ static void sdma_v3_0_emit_copy_buffer(struct amdgpu_ib *ib,
 				       uint32_t byte_count,
 				       uint32_t copy_flags)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
-	ib->ptr[ib->length_dw++] = byte_count;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
+	*ptr++ = byte_count;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src_offset);
+	*ptr++ = upper_32_bits(src_offset);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1555,11 +1573,15 @@ static void sdma_v3_0_emit_fill_buffer(struct amdgpu_ib *ib,
 				       uint64_t dst_offset,
 				       uint32_t byte_count)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = src_data;
-	ib->ptr[ib->length_dw++] = byte_count;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+	*ptr++ = src_data;
+	*ptr++ = byte_count;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 static const struct amdgpu_buffer_funcs sdma_v3_0_buffer_funcs = {
Signature · 1 line
2.55.0
#23 lore raw

PATCH 12/18 drm/amdgpu: More compact SDMA 4.0 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c | 109 +++++++++++++++----------
 1 file changed, 65 insertions(+), 44 deletions(-)
drivers/gpu/drm/amd/amdgpu/sdma_v4_0.c +65 -44
index 902d31300326..2dee17cc8673 100644
@@ -1562,17 +1562,19 @@ static void sdma_v4_0_vm_copy_pte(struct amdgpu_ib *ib,
 				  uint64_t pe, uint64_t src,
 				  unsigned count)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned bytes = count * 8;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
-	ib->ptr[ib->length_dw++] = bytes - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
+	*ptr++ = bytes - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src);
+	*ptr++ = upper_32_bits(src);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
 
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1590,18 +1592,21 @@ static void sdma_v4_0_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
 				   uint64_t value, unsigned count,
 				   uint32_t incr)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned ndw = count * 2;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = ndw - 1;
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = ndw - 1;
 	for (; ndw > 0; ndw -= 2) {
-		ib->ptr[ib->length_dw++] = lower_32_bits(value);
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
+		*ptr++ = lower_32_bits(value);
+		*ptr++ = upper_32_bits(value);
 		value += incr;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1621,17 +1626,21 @@ static void sdma_v4_0_vm_set_pte_pde(struct amdgpu_ib *ib,
 				     uint64_t addr, unsigned count,
 				     uint32_t incr, uint64_t flags)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
 	/* for physically contiguous pages (vram) */
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_PTEPDE);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
-	ib->ptr[ib->length_dw++] = upper_32_bits(flags);
-	ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = incr; /* increment size */
-	ib->ptr[ib->length_dw++] = 0;
-	ib->ptr[ib->length_dw++] = count - 1; /* number of entries */
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_PTEPDE);
+	*ptr++ = lower_32_bits(pe); /* dst addr */
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = lower_32_bits(flags); /* mask */
+	*ptr++ = upper_32_bits(flags);
+	*ptr++ = lower_32_bits(addr); /* value */
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = incr; /* increment size */
+	*ptr++ = 0;
+	*ptr++ = count - 1; /* number of entries */
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1644,18 +1653,22 @@ static void sdma_v4_0_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib
 {
 	struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
 	const bool burst_nop = sdma->burst_nop;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	u32 pad_count;
 	int i;
 
 	pad_count = (-ib->length_dw) & 7;
+	if (!pad_count)
+		return;
+
 	for (i = 0; i < pad_count; i++)
 		if (i == 0 && burst_nop)
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
-				SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
+				 SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
 		else
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+
+	ib->length_dw += pad_count;
 }
 
 
@@ -2519,15 +2532,19 @@ static void sdma_v4_0_emit_copy_buffer(struct amdgpu_ib *ib,
 				       uint32_t byte_count,
 				       uint32_t copy_flags)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
-		SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0);
-	ib->ptr[ib->length_dw++] = byte_count - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0);
+	*ptr++ = byte_count - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src_offset);
+	*ptr++ = upper_32_bits(src_offset);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -2545,11 +2562,15 @@ static void sdma_v4_0_emit_fill_buffer(struct amdgpu_ib *ib,
 				       uint64_t dst_offset,
 				       uint32_t byte_count)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = src_data;
-	ib->ptr[ib->length_dw++] = byte_count - 1;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+	*ptr++ = src_data;
+	*ptr++ = byte_count - 1;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 static const struct amdgpu_buffer_funcs sdma_v4_0_buffer_funcs = {
Signature · 1 line
2.55.0
#24 lore raw

PATCH 13/18 drm/amdgpu: More compact SDMA 4.4.2 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/sdma_v4_4_2.c | 108 ++++++++++++++---------
 1 file changed, 65 insertions(+), 43 deletions(-)
drivers/gpu/drm/amd/amdgpu/sdma_v4_4_2.c +65 -43
index df788361e865..fa62f813d513 100644
@@ -1118,16 +1118,19 @@ static void sdma_v4_4_2_vm_copy_pte(struct amdgpu_ib *ib,
 				  uint64_t pe, uint64_t src,
 				  unsigned count)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned bytes = count * 8;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
-	ib->ptr[ib->length_dw++] = bytes - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
+	*ptr++ = bytes - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src);
+	*ptr++ = upper_32_bits(src);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+
+	ib->length_dw = ptr - ib->ptr;
 
 }
 
@@ -1146,18 +1149,21 @@ static void sdma_v4_4_2_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
 				   uint64_t value, unsigned count,
 				   uint32_t incr)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned ndw = count * 2;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
 		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = ndw - 1;
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = ndw - 1;
 	for (; ndw > 0; ndw -= 2) {
-		ib->ptr[ib->length_dw++] = lower_32_bits(value);
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
+		*ptr++ = lower_32_bits(value);
+		*ptr++ = upper_32_bits(value);
 		value += incr;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1177,17 +1183,21 @@ static void sdma_v4_4_2_vm_set_pte_pde(struct amdgpu_ib *ib,
 				     uint64_t addr, unsigned count,
 				     uint32_t incr, uint64_t flags)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
 	/* for physically contiguous pages (vram) */
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_PTEPDE);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
-	ib->ptr[ib->length_dw++] = upper_32_bits(flags);
-	ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = incr; /* increment size */
-	ib->ptr[ib->length_dw++] = 0;
-	ib->ptr[ib->length_dw++] = count - 1; /* number of entries */
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_PTEPDE);
+	*ptr++ = lower_32_bits(pe); /* dst addr */
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = lower_32_bits(flags); /* mask */
+	*ptr++ = upper_32_bits(flags);
+	*ptr++ = lower_32_bits(addr); /* value */
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = incr; /* increment size */
+	*ptr++ = 0;
+	*ptr++ = count - 1; /* number of entries */
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1200,18 +1210,22 @@ static void sdma_v4_4_2_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *
 {
 	struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
 	const bool burst_nop = sdma->burst_nop;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	u32 pad_count;
 	int i;
 
 	pad_count = (-ib->length_dw) & 7;
+	if (!pad_count)
+		return;
+
 	for (i = 0; i < pad_count; i++)
 		if (i == 0 && burst_nop)
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
-				SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
+				 SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
 		else
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+
+	ib->length_dw += pad_count;
 }
 
 
@@ -2143,15 +2157,19 @@ static void sdma_v4_4_2_emit_copy_buffer(struct amdgpu_ib *ib,
 				       uint32_t byte_count,
 				       uint32_t copy_flags)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
-		SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0);
-	ib->ptr[ib->length_dw++] = byte_count - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0);
+	*ptr++ = byte_count - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src_offset);
+	*ptr++ = upper_32_bits(src_offset);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -2169,11 +2187,15 @@ static void sdma_v4_4_2_emit_fill_buffer(struct amdgpu_ib *ib,
 				       uint64_t dst_offset,
 				       uint32_t byte_count)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = src_data;
-	ib->ptr[ib->length_dw++] = byte_count - 1;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+	*ptr++ = src_data;
+	*ptr++ = byte_count - 1;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 static const struct amdgpu_buffer_funcs sdma_v4_4_2_buffer_funcs = {
Signature · 1 line
2.55.0
#25 lore raw

PATCH 14/18 drm/amdgpu: More compact SDMA 5.0 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c | 106 +++++++++++++++----------
 1 file changed, 64 insertions(+), 42 deletions(-)
drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c +64 -42
index c6349f0e632f..76899b65fc28 100644
@@ -1132,16 +1132,19 @@ static void sdma_v5_0_vm_copy_pte(struct amdgpu_ib *ib,
 				  uint64_t pe, uint64_t src,
 				  unsigned count)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned bytes = count * 8;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
-	ib->ptr[ib->length_dw++] = bytes - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
+	*ptr++ = bytes - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src);
+	*ptr++ = upper_32_bits(src);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+
+	ib->length_dw = ptr - ib->ptr;
 
 }
 
@@ -1160,18 +1163,21 @@ static void sdma_v5_0_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
 				   uint64_t value, unsigned count,
 				   uint32_t incr)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned ndw = count * 2;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = ndw - 1;
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = ndw - 1;
 	for (; ndw > 0; ndw -= 2) {
-		ib->ptr[ib->length_dw++] = lower_32_bits(value);
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
+		*ptr++ = lower_32_bits(value);
+		*ptr++ = upper_32_bits(value);
 		value += incr;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1191,17 +1197,21 @@ static void sdma_v5_0_vm_set_pte_pde(struct amdgpu_ib *ib,
 				     uint64_t addr, unsigned count,
 				     uint32_t incr, uint64_t flags)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
 	/* for physically contiguous pages (vram) */
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_PTEPDE);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
-	ib->ptr[ib->length_dw++] = upper_32_bits(flags);
-	ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = incr; /* increment size */
-	ib->ptr[ib->length_dw++] = 0;
-	ib->ptr[ib->length_dw++] = count - 1; /* number of entries */
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_PTEPDE);
+	*ptr++ = lower_32_bits(pe); /* dst addr */
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = lower_32_bits(flags); /* mask */
+	*ptr++ = upper_32_bits(flags);
+	*ptr++ = lower_32_bits(addr); /* value */
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = incr; /* increment size */
+	*ptr++ = 0;
+	*ptr++ = count - 1; /* number of entries */
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1215,18 +1225,22 @@ static void sdma_v5_0_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib
 {
 	struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
 	const bool burst_nop = sdma->burst_nop;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	u32 pad_count;
 	int i;
 
 	pad_count = (-ib->length_dw) & 0x7;
+	if (!pad_count)
+		return;
+
 	for (i = 0; i < pad_count; i++)
 		if (i == 0 && burst_nop)
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
-				SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
+				 SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
 		else
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+
+	ib->length_dw += pad_count;
 }
 
 
@@ -1934,15 +1948,19 @@ static void sdma_v5_0_emit_copy_buffer(struct amdgpu_ib *ib,
 				       uint32_t byte_count,
 				       uint32_t copy_flags)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
 		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
 		SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0);
-	ib->ptr[ib->length_dw++] = byte_count - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
+	*ptr++ = byte_count - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src_offset);
+	*ptr++ = upper_32_bits(src_offset);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1960,11 +1978,15 @@ static void sdma_v5_0_emit_fill_buffer(struct amdgpu_ib *ib,
 				       uint64_t dst_offset,
 				       uint32_t byte_count)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = src_data;
-	ib->ptr[ib->length_dw++] = byte_count - 1;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+	*ptr++ = src_data;
+	*ptr++ = byte_count - 1;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 static const struct amdgpu_buffer_funcs sdma_v5_0_buffer_funcs = {
Signature · 1 line
2.55.0
#26 lore raw

PATCH 15/18 drm/amdgpu: More compact SDMA 5.2 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c | 110 +++++++++++++++----------
 1 file changed, 66 insertions(+), 44 deletions(-)
drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c +66 -44
index 683f27c27665..a867d5de154e 100644
@@ -1033,15 +1033,18 @@ static void sdma_v5_2_vm_copy_pte(struct amdgpu_ib *ib,
 				  unsigned count)
 {
 	unsigned bytes = count * 8;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
-	ib->ptr[ib->length_dw++] = bytes - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
+	*ptr++ = bytes - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src);
+	*ptr++ = upper_32_bits(src);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+
+	ib->length_dw = ptr - ib->ptr;
 
 }
 
@@ -1061,17 +1064,20 @@ static void sdma_v5_2_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
 				   uint32_t incr)
 {
 	unsigned ndw = count * 2;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = ndw - 1;
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = ndw - 1;
 	for (; ndw > 0; ndw -= 2) {
-		ib->ptr[ib->length_dw++] = lower_32_bits(value);
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
+		*ptr++ = lower_32_bits(value);
+		*ptr++ = upper_32_bits(value);
 		value += incr;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1091,17 +1097,21 @@ static void sdma_v5_2_vm_set_pte_pde(struct amdgpu_ib *ib,
 				     uint64_t addr, unsigned count,
 				     uint32_t incr, uint64_t flags)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
 	/* for physically contiguous pages (vram) */
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_PTEPDE);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
-	ib->ptr[ib->length_dw++] = upper_32_bits(flags);
-	ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = incr; /* increment size */
-	ib->ptr[ib->length_dw++] = 0;
-	ib->ptr[ib->length_dw++] = count - 1; /* number of entries */
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_PTEPDE);
+	*ptr++ = lower_32_bits(pe); /* dst addr */
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = lower_32_bits(flags); /* mask */
+	*ptr++ = upper_32_bits(flags);
+	*ptr++ = lower_32_bits(addr); /* value */
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = incr; /* increment size */
+	*ptr++ = 0;
+	*ptr++ = count - 1; /* number of entries */
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1116,18 +1126,22 @@ static void sdma_v5_2_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib
 {
 	struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
 	const bool burst_nop = sdma->burst_nop;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	u32 pad_count;
 	int i;
 
 	pad_count = (-ib->length_dw) & 0x7;
+	if (!pad_count)
+		return;
+
 	for (i = 0; i < pad_count; i++)
 		if (i == 0 && burst_nop)
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
-				SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
+				 SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
 		else
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+			*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
+
+	ib->length_dw += pad_count;
 }
 
 
@@ -1951,15 +1965,19 @@ static void sdma_v5_2_emit_copy_buffer(struct amdgpu_ib *ib,
 				       uint32_t byte_count,
 				       uint32_t copy_flags)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
-		SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0);
-	ib->ptr[ib->length_dw++] = byte_count - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0);
+	*ptr++ = byte_count - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src_offset);
+	*ptr++ = upper_32_bits(src_offset);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1977,11 +1995,15 @@ static void sdma_v5_2_emit_fill_buffer(struct amdgpu_ib *ib,
 				       uint64_t dst_offset,
 				       uint32_t byte_count)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = src_data;
-	ib->ptr[ib->length_dw++] = byte_count - 1;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+	*ptr++ = src_data;
+	*ptr++ = byte_count - 1;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 static const struct amdgpu_buffer_funcs sdma_v5_2_buffer_funcs = {
Signature · 1 line
2.55.0
#27 lore raw

PATCH 16/18 drm/amdgpu: More compact SDMA 6.0 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/sdma_v6_0.c | 110 +++++++++++++++----------
 1 file changed, 66 insertions(+), 44 deletions(-)
drivers/gpu/drm/amd/amdgpu/sdma_v6_0.c +66 -44
index 16649dba6329..78c511d23727 100644
@@ -1023,16 +1023,19 @@ static void sdma_v6_0_vm_copy_pte(struct amdgpu_ib *ib,
 				  uint64_t pe, uint64_t src,
 				  unsigned count)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned bytes = count * 8;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
-	ib->ptr[ib->length_dw++] = bytes - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
+	*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
+	*ptr++ = bytes - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src);
+	*ptr++ = upper_32_bits(src);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+
+	ib->length_dw = ptr - ib->ptr;
 
 }
 
@@ -1051,18 +1054,21 @@ static void sdma_v6_0_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
 				   uint64_t value, unsigned count,
 				   uint32_t incr)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned ndw = count * 2;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_WRITE) |
-		SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = ndw - 1;
+	*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_WRITE) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = ndw - 1;
 	for (; ndw > 0; ndw -= 2) {
-		ib->ptr[ib->length_dw++] = lower_32_bits(value);
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
+		*ptr++ = lower_32_bits(value);
+		*ptr++ = upper_32_bits(value);
 		value += incr;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1082,17 +1088,21 @@ static void sdma_v6_0_vm_set_pte_pde(struct amdgpu_ib *ib,
 				     uint64_t addr, unsigned count,
 				     uint32_t incr, uint64_t flags)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
 	/* for physically contiguous pages (vram) */
-	ib->ptr[ib->length_dw++] = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_PTEPDE);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
-	ib->ptr[ib->length_dw++] = upper_32_bits(flags);
-	ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = incr; /* increment size */
-	ib->ptr[ib->length_dw++] = 0;
-	ib->ptr[ib->length_dw++] = count - 1; /* number of entries */
+	*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_PTEPDE);
+	*ptr++ = lower_32_bits(pe); /* dst addr */
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = lower_32_bits(flags); /* mask */
+	*ptr++ = upper_32_bits(flags);
+	*ptr++ = lower_32_bits(addr); /* value */
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = incr; /* increment size */
+	*ptr++ = 0;
+	*ptr++ = count - 1; /* number of entries */
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /*
@@ -1106,18 +1116,22 @@ static void sdma_v6_0_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib
 {
 	struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
 	const bool burst_nop = sdma->burst_nop;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	u32 pad_count;
 	int i;
 
 	pad_count = (-ib->length_dw) & 0x7;
+	if (!pad_count)
+		return;
+
 	for (i = 0; i < pad_count; i++)
 		if (i == 0 && burst_nop)
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP) |
-				SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
+			*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP) |
+				 SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
 		else
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP);
+			*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP);
+
+	ib->length_dw += pad_count;
 }
 
 /**
@@ -1815,15 +1829,19 @@ static void sdma_v6_0_emit_copy_buffer(struct amdgpu_ib *ib,
 				       uint32_t byte_count,
 				       uint32_t copy_flags)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
-		SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0);
-	ib->ptr[ib->length_dw++] = byte_count - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0);
+	*ptr++ = byte_count - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src_offset);
+	*ptr++ = upper_32_bits(src_offset);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1841,11 +1859,15 @@ static void sdma_v6_0_emit_fill_buffer(struct amdgpu_ib *ib,
 				       uint64_t dst_offset,
 				       uint32_t byte_count)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_CONSTANT_FILL_HEADER_OP(SDMA_OP_CONST_FILL);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = src_data;
-	ib->ptr[ib->length_dw++] = byte_count - 1;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_CONSTANT_FILL_HEADER_OP(SDMA_OP_CONST_FILL);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+	*ptr++ = src_data;
+	*ptr++ = byte_count - 1;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 static const struct amdgpu_buffer_funcs sdma_v6_0_buffer_funcs = {
Signature · 1 line
2.55.0
#28 lore raw

PATCH 17/18 drm/amdgpu: More compact SDMA 7.0 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/sdma_v7_0.c | 119 +++++++++++++++----------
 1 file changed, 70 insertions(+), 49 deletions(-)
drivers/gpu/drm/amd/amdgpu/sdma_v7_0.c +70 -49
index edd9c437f7cb..fbfe1bd76894 100644
@@ -1066,19 +1066,22 @@ static void sdma_v7_0_vm_copy_pte(struct amdgpu_ib *ib,
 				  uint64_t pe, uint64_t src,
 				  unsigned count)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned bytes = count * 8;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
-		SDMA_PKT_COPY_LINEAR_HEADER_CPV(1);
+	*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_CPV(1);
 
-	ib->ptr[ib->length_dw++] = bytes - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = 0;
+	*ptr++ = bytes - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src);
+	*ptr++ = upper_32_bits(src);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = 0;
+
+	ib->length_dw = ptr - ib->ptr;
 
 }
 
@@ -1097,18 +1100,21 @@ static void sdma_v7_0_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
 				   uint64_t value, unsigned count,
 				   uint32_t incr)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned ndw = count * 2;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_WRITE) |
-		SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = ndw - 1;
+	*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_WRITE) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = ndw - 1;
 	for (; ndw > 0; ndw -= 2) {
-		ib->ptr[ib->length_dw++] = lower_32_bits(value);
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
+		*ptr++ = lower_32_bits(value);
+		*ptr++ = upper_32_bits(value);
 		value += incr;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1128,17 +1134,21 @@ static void sdma_v7_0_vm_set_pte_pde(struct amdgpu_ib *ib,
 				     uint64_t addr, unsigned count,
 				     uint32_t incr, uint64_t flags)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
 	/* for physically contiguous pages (vram) */
-	ib->ptr[ib->length_dw++] = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_PTEPDE);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
-	ib->ptr[ib->length_dw++] = upper_32_bits(flags);
-	ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = incr; /* increment size */
-	ib->ptr[ib->length_dw++] = 0;
-	ib->ptr[ib->length_dw++] = count - 1; /* number of entries */
+	*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_PTEPDE);
+	*ptr++ = lower_32_bits(pe); /* dst addr */
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = lower_32_bits(flags); /* mask */
+	*ptr++ = upper_32_bits(flags);
+	*ptr++ = lower_32_bits(addr); /* value */
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = incr; /* increment size */
+	*ptr++ = 0;
+	*ptr++ = count - 1; /* number of entries */
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1153,18 +1163,22 @@ static void sdma_v7_0_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib
 {
 	struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
 	const bool burst_nop = sdma->burst_nop;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	u32 pad_count;
 	int i;
 
 	pad_count = (-ib->length_dw) & 0x7;
+	if (!pad_count)
+		return;
+
 	for (i = 0; i < pad_count; i++)
 		if (i == 0 && burst_nop)
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP) |
+			*ptr++= SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP) |
 				SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
 		else
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP);
+			*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP);
+
+	ib->length_dw += pad_count;
 }
 
 /**
@@ -1747,31 +1761,34 @@ static void sdma_v7_0_emit_copy_buffer(struct amdgpu_ib *ib,
 				       uint32_t copy_flags)
 {
 	uint32_t num_type, data_format, max_com, write_cm;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 
 	max_com = AMDGPU_COPY_FLAGS_GET(copy_flags, MAX_COMPRESSED);
 	data_format = AMDGPU_COPY_FLAGS_GET(copy_flags, DATA_FORMAT);
 	num_type = AMDGPU_COPY_FLAGS_GET(copy_flags, NUMBER_TYPE);
 	write_cm = AMDGPU_COPY_FLAGS_GET(copy_flags, WRITE_COMPRESS_DISABLE) ? 2 : 1;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
-		SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0) |
-		SDMA_PKT_COPY_LINEAR_HEADER_CPV(1);
+	*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_CPV(1);
 
-	ib->ptr[ib->length_dw++] = byte_count - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
+	*ptr++ = byte_count - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src_offset);
+	*ptr++ = upper_32_bits(src_offset);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
 
 	if ((copy_flags & (AMDGPU_COPY_FLAGS_READ_DECOMPRESSED | AMDGPU_COPY_FLAGS_WRITE_COMPRESSED)))
-		ib->ptr[ib->length_dw++] = SDMA_DCC_DATA_FORMAT(data_format) | SDMA_DCC_NUM_TYPE(num_type) |
+		*ptr++ = SDMA_DCC_DATA_FORMAT(data_format) | SDMA_DCC_NUM_TYPE(num_type) |
 			((copy_flags & AMDGPU_COPY_FLAGS_READ_DECOMPRESSED) ? SDMA_DCC_READ_CM(2) : 0) |
 			((copy_flags & AMDGPU_COPY_FLAGS_WRITE_COMPRESSED) ? SDMA_DCC_WRITE_CM(write_cm) : 0) |
 			SDMA_DCC_MAX_COM(max_com) | SDMA_DCC_MAX_UCOM(1);
 	else
-		ib->ptr[ib->length_dw++] = 0;
+		*ptr++ = 0;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1789,12 +1806,16 @@ static void sdma_v7_0_emit_fill_buffer(struct amdgpu_ib *ib,
 				       uint64_t dst_offset,
 				       uint32_t byte_count)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_CONSTANT_FILL_HEADER_OP(SDMA_OP_CONST_FILL) |
-		SDMA_PKT_CONSTANT_FILL_HEADER_COMPRESS(1);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = src_data;
-	ib->ptr[ib->length_dw++] = byte_count - 1;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_CONSTANT_FILL_HEADER_OP(SDMA_OP_CONST_FILL) |
+		 SDMA_PKT_CONSTANT_FILL_HEADER_COMPRESS(1);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+	*ptr++ = src_data;
+	*ptr++ = byte_count - 1;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 static const struct amdgpu_buffer_funcs sdma_v7_0_buffer_funcs = {
Signature · 1 line
2.55.0
#29 lore raw

PATCH 18/18 drm/amdgpu: More compact SDMA 7.1 IB emission

In reply to Tvrtko Ursulin

Avoid constant register reloads while emitting IBs by using a local write pointer and only updating the size at the end of each helper.

Cc Timur Kristóf <[email protected]>
Cc Alex Deucher <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/sdma_v7_1.c | 102 +++++++++++++++----------
 1 file changed, 60 insertions(+), 42 deletions(-)
drivers/gpu/drm/amd/amdgpu/sdma_v7_1.c +60 -42
index 321c67e01443..ff9ae57066c3 100644
@@ -1060,18 +1060,21 @@ static void sdma_v7_1_vm_copy_pte(struct amdgpu_ib *ib,
 				  uint64_t pe, uint64_t src,
 				  unsigned count)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
 	unsigned bytes = count * 8;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
+	*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
 		SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
 
-	ib->ptr[ib->length_dw++] = bytes - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
+	*ptr++ = bytes - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src);
+	*ptr++ = upper_32_bits(src);
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
 
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1089,18 +1092,21 @@ static void sdma_v7_1_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
 				   uint64_t value, unsigned count,
 				   uint32_t incr)
 {
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	unsigned ndw = count * 2;
 
-	ib->ptr[ib->length_dw++] = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_WRITE) |
+	*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_WRITE) |
 		SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe);
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = ndw - 1;
+	*ptr++ = lower_32_bits(pe);
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = ndw - 1;
 	for (; ndw > 0; ndw -= 2) {
-		ib->ptr[ib->length_dw++] = lower_32_bits(value);
-		ib->ptr[ib->length_dw++] = upper_32_bits(value);
+		*ptr++ = lower_32_bits(value);
+		*ptr++ = upper_32_bits(value);
 		value += incr;
 	}
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1122,6 +1128,7 @@ static void sdma_v7_1_vm_set_pte_pde(struct amdgpu_ib *ib,
 {
 	/* for physically contiguous pages (vram) */
 	u32 header = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_PTEPDE);
+	u32 *ptr = &ib->ptr[ib->length_dw];
 
 	/* TODO:
 	 * When VM_L2_CNTL5.WALKER_FETCH_PDE_MTYPE_ENABLE is enabled, change below MTYPE
@@ -1139,16 +1146,18 @@ static void sdma_v7_1_vm_set_pte_pde(struct amdgpu_ib *ib,
 	 */
 	header |= SDMA_PKT_PTEPDE_COPY_HEADER_MTYPE(0x3) | SDMA_PKT_PTEPDE_COPY_HEADER_SNOOP(0x1);
 
-	ib->ptr[ib->length_dw++] = header;
-	ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
-	ib->ptr[ib->length_dw++] = upper_32_bits(pe);
-	ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
-	ib->ptr[ib->length_dw++] = upper_32_bits(flags);
-	ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
-	ib->ptr[ib->length_dw++] = upper_32_bits(addr);
-	ib->ptr[ib->length_dw++] = incr; /* increment size */
-	ib->ptr[ib->length_dw++] = 0;
-	ib->ptr[ib->length_dw++] = count - 1; /* number of entries */
+	*ptr++ = header;
+	*ptr++ = lower_32_bits(pe); /* dst addr */
+	*ptr++ = upper_32_bits(pe);
+	*ptr++ = lower_32_bits(flags); /* mask */
+	*ptr++ = upper_32_bits(flags);
+	*ptr++ = lower_32_bits(addr); /* value */
+	*ptr++ = upper_32_bits(addr);
+	*ptr++ = incr; /* increment size */
+	*ptr++ = 0;
+	*ptr++ = count - 1; /* number of entries */
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1163,18 +1172,19 @@ static void sdma_v7_1_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib
 {
 	struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
 	const bool burst_nop = sdma->burst_nop;
+	u32 *ptr = &ib->ptr[ib->length_dw];
 	u32 pad_count;
 	int i;
 
 	pad_count = (-ib->length_dw) & 0x7;
 	for (i = 0; i < pad_count; i++)
 		if (i == 0 && burst_nop)
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP) |
-				SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
+			*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP) |
+				 SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
 		else
-			ib->ptr[ib->length_dw++] =
-				SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP);
+			*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_NOP);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1727,16 +1737,20 @@ static void sdma_v7_1_emit_copy_buffer(struct amdgpu_ib *ib,
 				       uint32_t byte_count,
 				       uint32_t copy_flags)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
-		SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
-		SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0);
+	u32 *ptr = &ib->ptr[ib->length_dw];
 
-	ib->ptr[ib->length_dw++] = byte_count - 1;
-	ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
-	ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
+	*ptr++ = SDMA_PKT_COPY_LINEAR_HEADER_OP(SDMA_OP_COPY) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
+		 SDMA_PKT_COPY_LINEAR_HEADER_TMZ((copy_flags & AMDGPU_COPY_FLAGS_TMZ) ? 1 : 0);
+
+	*ptr++ = byte_count - 1;
+	*ptr++ = 0; /* src/dst endian swap */
+	*ptr++ = lower_32_bits(src_offset);
+	*ptr++ = upper_32_bits(src_offset);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 /**
@@ -1754,11 +1768,15 @@ static void sdma_v7_1_emit_fill_buffer(struct amdgpu_ib *ib,
 				       uint64_t dst_offset,
 				       uint32_t byte_count)
 {
-	ib->ptr[ib->length_dw++] = SDMA_PKT_CONSTANT_FILL_HEADER_OP(SDMA_OP_CONST_FILL);
-	ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
-	ib->ptr[ib->length_dw++] = src_data;
-	ib->ptr[ib->length_dw++] = byte_count - 1;
+	u32 *ptr = &ib->ptr[ib->length_dw];
+
+	*ptr++ = SDMA_PKT_CONSTANT_FILL_HEADER_OP(SDMA_OP_CONST_FILL);
+	*ptr++ = lower_32_bits(dst_offset);
+	*ptr++ = upper_32_bits(dst_offset);
+	*ptr++ = src_data;
+	*ptr++ = byte_count - 1;
+
+	ib->length_dw = ptr - ib->ptr;
 }
 
 static const struct amdgpu_buffer_funcs sdma_v7_1_buffer_funcs = {
Signature · 1 line
2.55.0