Skip to contentSkip to conversation

drm/msm/a6xx+: Insert a fence wait before SMMU table update

PATCH Superseded Rob Clark 4 participants 9 messages Sep 13, 2024 – Sep 26, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Rob Clark

author
#0 lore raw

PATCH 1/1 drm/msm/a6xx+: Insert a fence wait before SMMU table update

From: Rob Clark <[email protected]>

The CP_SMMU_TABLE_UPDATE _should_ be waiting for idle, but on some devices (x1-85, possibly others), it seems to pass that barrier while there are still things in the event completion FIFO waiting to be written back to memory.

Work around that by adding a fence wait before context switch. The CP_EVENT_WRITE that writes the fence is the last write from a submit, so seeing this value hit memory is a reliable indication that it is safe to proceed with the context switch.

---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +11 -3
index bcaec86ac67a..ba5b35502e6d 100644
@@ -101,9 +101,10 @@ static void get_stats_counter(struct msm_ringbuffer *ring, u32 counter,
 }
 
 static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
-		struct msm_ringbuffer *ring, struct msm_file_private *ctx)
+		struct msm_ringbuffer *ring, struct msm_gem_submit *submit)
 {
 	bool sysprof = refcount_read(&a6xx_gpu->base.base.sysprof_active) > 1;
+	struct msm_file_private *ctx = submit->queue->ctx;
 	struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
 	phys_addr_t ttbr;
 	u32 asid;
@@ -115,6 +116,13 @@ static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
 	if (msm_iommu_pagetable_params(ctx->aspace->mmu, &ttbr, &asid))
 		return;
 
+	/* Wait for previous submit to complete before continuing: */
+	OUT_PKT7(ring, CP_WAIT_TIMESTAMP, 4);
+	OUT_RING(ring, 0);
+	OUT_RING(ring, lower_32_bits(rbmemptr(ring, fence)));
+	OUT_RING(ring, upper_32_bits(rbmemptr(ring, fence)));
+	OUT_RING(ring, submit->seqno - 1);
+
 	if (!sysprof) {
 		if (!adreno_is_a7xx(adreno_gpu)) {
 			/* Turn off protected mode to write to special registers */
@@ -193,7 +201,7 @@ static void a6xx_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
 	struct msm_ringbuffer *ring = submit->ring;
 	unsigned int i, ibs = 0;
 
-	a6xx_set_pagetable(a6xx_gpu, ring, submit->queue->ctx);
+	a6xx_set_pagetable(a6xx_gpu, ring, submit);
 
 	get_stats_counter(ring, REG_A6XX_RBBM_PERFCTR_CP(0),
 		rbmemptr_stats(ring, index, cpcycles_start));
@@ -283,7 +291,7 @@ static void a7xx_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
 	OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
 	OUT_RING(ring, CP_THREAD_CONTROL_0_SYNC_THREADS | CP_SET_THREAD_BR);
 
-	a6xx_set_pagetable(a6xx_gpu, ring, submit->queue->ctx);
+	a6xx_set_pagetable(a6xx_gpu, ring, submit);
 
 	get_stats_counter(ring, REG_A7XX_RBBM_PERFCTR_CP(0),
 		rbmemptr_stats(ring, index, cpcycles_start));
Signature · 1 line
2.46.0
reviewer
#1 lore raw
Quoted 6 lines · Rob Clark

From: Rob Clark <[email protected]>

The CP_SMMU_TABLE_UPDATE _should_ be waiting for idle, but on some devices (x1-85, possibly others), it seems to pass that barrier while there are still things in the event completion FIFO waiting to be written back to memory.

Can we try to force-fault around here on other GPUs and perhaps limit this workaround?

Akhil, do we have any insight on this?

Konrad

#2 lore raw
Quoted 8 lines · Konrad Dybcio
Quoted 6 lines · Rob Clark

From: Rob Clark <[email protected]>

The CP_SMMU_TABLE_UPDATE _should_ be waiting for idle, but on some devices (x1-85, possibly others), it seems to pass that barrier while there are still things in the event completion FIFO waiting to be written back to memory.

Quoted 4 lines

Can we try to force-fault around here on other GPUs and perhaps limit this workaround?

Akhil, do we have any insight on this?

Nothing at the moment. I will check this further.

-Akhil.

Quoted 2 lines

Konrad

author
#3 lore raw
In reply to Konrad Dybcio
Quoted 11 lines · Konrad Dybcio
Quoted 6 lines · Rob Clark

From: Rob Clark <[email protected]>

The CP_SMMU_TABLE_UPDATE _should_ be waiting for idle, but on some devices (x1-85, possibly others), it seems to pass that barrier while there are still things in the event completion FIFO waiting to be written back to memory.

Can we try to force-fault around here on other GPUs and perhaps limit this workaround?

not sure what you mean by "force-fault"... we could probably limit this to certain GPUs, the only reason I didn't is (a) it should be harmless when it is not needed, and (b) I have no real good way to get an exhaustive list of where it is needed. Maybe/hopefully it is only x1-85, but idk.

It does bring up an interesting question about preemption, though

BR,
-R

Quoted 3 lines

Akhil, do we have any insight on this?

Konrad

reviewer
#4 lore raw
Quoted 10 lines · Rob Clark
Quoted 9 lines · Konrad Dybcio
Quoted 6 lines · Rob Clark

From: Rob Clark <[email protected]>

The CP_SMMU_TABLE_UPDATE _should_ be waiting for idle, but on some devices (x1-85, possibly others), it seems to pass that barrier while there are still things in the event completion FIFO waiting to be written back to memory.

Quoted 4 lines
Quoted 2 lines

Can we try to force-fault around here on other GPUs and perhaps limit this workaround?

not sure what you mean by "force-fault"...

I suppose 'reproduce' is what I meant

Quoted 3 lines

we could probably limit
this to certain GPUs, the only reason I didn't is (a) it should be harmless when it is not needed,

Do we have any realistic perf hits here?

Quoted 5 lines

and (b) I have no real good way to get
an exhaustive list of where it is needed. Maybe/hopefully it is only x1-85, but idk.

It does bring up an interesting question about preemption, though

Yeah..

Do we know what windows does here?

Konrad

author
#5 lore raw
Quoted 12 lines · Konrad Dybcio
Quoted 10 lines · Rob Clark
Quoted 9 lines · Konrad Dybcio
Quoted 6 lines · Rob Clark

From: Rob Clark <[email protected]>

The CP_SMMU_TABLE_UPDATE _should_ be waiting for idle, but on some devices (x1-85, possibly others), it seems to pass that barrier while there are still things in the event completion FIFO waiting to be written back to memory.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Can we try to force-fault around here on other GPUs and perhaps limit this workaround?

not sure what you mean by "force-fault"...

I suppose 'reproduce' is what I meant

I haven't _noticed_ it yet.. if you want to try on devices you have, glmark2 seems to be good at reproducing..

I think the reason is combo of high fps (on x1-85 most scenes are north of 8k fps) so you get a lot of context switches btwn compositor and glmark2. Most scenes are just a clear plus single draw, and I guess the compositor is just doing a single draw/blit. A6xx can be two draws/blits deep in it's pipeline, a7xx can be four, which maybe exacerbates this.

Quoted 5 lines
Quoted 3 lines

we could probably limit
this to certain GPUs, the only reason I didn't is (a) it should be harmless when it is not needed,

Do we have any realistic perf hits here?

I don't think so, we can't switch ttbr0 while the gpu is still busy so what the sqe does for CP_SMMU_TABLE_UPDATE _should_ be equivalent. Maybe it amounts to some extra CP cycles and memory read, but I think that should be negligible given that the expensive thing is that we are stalling the gpu until it is idle.

Quoted 7 lines
Quoted 5 lines

and (b) I have no real good way to get
an exhaustive list of where it is needed. Maybe/hopefully it is only x1-85, but idk.

It does bring up an interesting question about preemption, though

Yeah..

The KMD does setup an xAMBLE to clear the perfcntrs on context switch. We could maybe piggy back on that, but I guess we'd have to patch in the fence value to wait for?

Quoted 1 line

Do we know what windows does here?

not sure, maybe akhil has some way to check. Whether a similar scenario comes up with windows probably depends on how the winsys works. If it dropped frames when rendering >vblank rate, you'd get fewer context switches.

BR,
-R

Quoted 1 line

Konrad

#6 lore raw
In reply to Rob Clark
Quoted 36 lines · Rob Clark

From: Rob Clark <[email protected]>

The CP_SMMU_TABLE_UPDATE _should_ be waiting for idle, but on some devices (x1-85, possibly others), it seems to pass that barrier while there are still things in the event completion FIFO waiting to be written back to memory.

Work around that by adding a fence wait before context switch. The CP_EVENT_WRITE that writes the fence is the last write from a submit, so seeing this value hit memory is a reliable indication that it is safe to proceed with the context switch.

---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +2 -1
index bcaec86ac67a..ba5b35502e6d 100644
@@ -101,9 +101,10 @@ static void get_stats_counter(struct msm_ringbuffer *ring, u32 counter,
 }
 
 static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
-               struct msm_ringbuffer *ring, struct msm_file_private *ctx)
+               struct msm_ringbuffer *ring, struct msm_gem_submit *submit)
 {
        bool sysprof = refcount_read(&a6xx_gpu->base.base.sysprof_active) > 1;
+       struct msm_file_private *ctx = submit->queue->ctx;
        struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
        phys_addr_t ttbr;
        u32 asid;

drivers/gpu/drm/msm/adreno/a6xx_gpu.c static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu, · line 120

Quoted 5 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +2 -0
@@ -115,6 +116,13 @@ static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
        if (msm_iommu_pagetable_params(ctx->aspace->mmu, &ttbr, &asid))
                return;
 
+       /* Wait for previous submit to complete before continuing: */
+       OUT_PKT7(ring, CP_WAIT_TIMESTAMP, 4);

CP_WAIT_TIMESTAMP doesn't exist on a6xx, so this won't work there. I don't know if the bug exists on a6xx, but I'd be inclined to say it has always existed and we just never hit it because it requires some very specific timing conditions. We can make it work on a6xx by using CP_WAIT_REG_MEM and waiting for it to equal the exact value.

Connor

Quoted 29 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +7 -3
+       OUT_RING(ring, 0);
+       OUT_RING(ring, lower_32_bits(rbmemptr(ring, fence)));
+       OUT_RING(ring, upper_32_bits(rbmemptr(ring, fence)));
+       OUT_RING(ring, submit->seqno - 1);
+
        if (!sysprof) {
                if (!adreno_is_a7xx(adreno_gpu)) {
                        /* Turn off protected mode to write to special registers */
@@ -193,7 +201,7 @@ static void a6xx_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
        struct msm_ringbuffer *ring = submit->ring;
        unsigned int i, ibs = 0;
 
-       a6xx_set_pagetable(a6xx_gpu, ring, submit->queue->ctx);
+       a6xx_set_pagetable(a6xx_gpu, ring, submit);
 
        get_stats_counter(ring, REG_A6XX_RBBM_PERFCTR_CP(0),
                rbmemptr_stats(ring, index, cpcycles_start));
@@ -283,7 +291,7 @@ static void a7xx_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
        OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
        OUT_RING(ring, CP_THREAD_CONTROL_0_SYNC_THREADS | CP_SET_THREAD_BR);
 
-       a6xx_set_pagetable(a6xx_gpu, ring, submit->queue->ctx);
+       a6xx_set_pagetable(a6xx_gpu, ring, submit);
 
        get_stats_counter(ring, REG_A7XX_RBBM_PERFCTR_CP(0),
                rbmemptr_stats(ring, index, cpcycles_start));
--

2.46.0

author
#7 lore raw
Quoted 41 lines · Connor Abbott
Quoted 39 lines · Rob Clark

From: Rob Clark <[email protected]>

The CP_SMMU_TABLE_UPDATE _should_ be waiting for idle, but on some devices (x1-85, possibly others), it seems to pass that barrier while there are still things in the event completion FIFO waiting to be written back to memory.

Work around that by adding a fence wait before context switch. The CP_EVENT_WRITE that writes the fence is the last write from a submit, so seeing this value hit memory is a reliable indication that it is safe to proceed with the context switch.

---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +2 -1
index bcaec86ac67a..ba5b35502e6d 100644
@@ -101,9 +101,10 @@ static void get_stats_counter(struct msm_ringbuffer *ring, u32 counter,
 }
 
 static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
-               struct msm_ringbuffer *ring, struct msm_file_private *ctx)
+               struct msm_ringbuffer *ring, struct msm_gem_submit *submit)
 {
        bool sysprof = refcount_read(&a6xx_gpu->base.base.sysprof_active) > 1;
+       struct msm_file_private *ctx = submit->queue->ctx;
        struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
        phys_addr_t ttbr;
        u32 asid;
@@ -115,6 +116,13 @@ static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
        if (msm_iommu_pagetable_params(ctx->aspace->mmu, &ttbr, &asid))
                return;
 
Quoted 8 lines

drivers/gpu/drm/msm/adreno/a6xx_gpu.c static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu, · line 120

Quoted 2 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +2 -0
+       /* Wait for previous submit to complete before continuing: */
+       OUT_PKT7(ring, CP_WAIT_TIMESTAMP, 4);

CP_WAIT_TIMESTAMP doesn't exist on a6xx, so this won't work there. I don't know if the bug exists on a6xx, but I'd be inclined to say it has always existed and we just never hit it because it requires some very specific timing conditions. We can make it work on a6xx by using CP_WAIT_REG_MEM and waiting for it to equal the exact value.

I've been unable to reproduce this on a690, despite running at a similar fps (so similar rate of CP_SMMU_TABLE_UPDATEs). I guess I can't rule out that this is just _harder_ to hit on a6xx due to the shallower pipeline. It would be nice to get some data points on other a7xx, but I only have the one.

I did attempt to come up with an igt stand-alone reproducer by just ping-ponging between contexts (with fences to force context switches) with 1000's of CP_EVENT_WRITE's, to no avail. I guess I'd need to actually setup a blit or draw to make the event-write asynchronous, but that would be a lot harder to do in igt.

I guess for now I'll re-work this patch to only do the workaround on a7xx. And wire up the gallium preemption support so we can confirm whether this is also an issue for preemption.

BR,
-R

Quoted 31 lines

Connor

Quoted 29 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +7 -3
+       OUT_RING(ring, 0);
+       OUT_RING(ring, lower_32_bits(rbmemptr(ring, fence)));
+       OUT_RING(ring, upper_32_bits(rbmemptr(ring, fence)));
+       OUT_RING(ring, submit->seqno - 1);
+
        if (!sysprof) {
                if (!adreno_is_a7xx(adreno_gpu)) {
                        /* Turn off protected mode to write to special registers */
@@ -193,7 +201,7 @@ static void a6xx_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
        struct msm_ringbuffer *ring = submit->ring;
        unsigned int i, ibs = 0;
 
-       a6xx_set_pagetable(a6xx_gpu, ring, submit->queue->ctx);
+       a6xx_set_pagetable(a6xx_gpu, ring, submit);
 
        get_stats_counter(ring, REG_A6XX_RBBM_PERFCTR_CP(0),
                rbmemptr_stats(ring, index, cpcycles_start));
@@ -283,7 +291,7 @@ static void a7xx_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
        OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
        OUT_RING(ring, CP_THREAD_CONTROL_0_SYNC_THREADS | CP_SET_THREAD_BR);
 
-       a6xx_set_pagetable(a6xx_gpu, ring, submit->queue->ctx);
+       a6xx_set_pagetable(a6xx_gpu, ring, submit);
 
        get_stats_counter(ring, REG_A7XX_RBBM_PERFCTR_CP(0),
                rbmemptr_stats(ring, index, cpcycles_start));
--

2.46.0

Reviewed-by on v1 1/1
#8 lore raw
In reply to Rob Clark
Quoted 7 lines · Rob Clark

From: Rob Clark <[email protected]>

The CP_SMMU_TABLE_UPDATE _should_ be waiting for idle, but on some devices (x1-85, possibly others), it seems to pass that barrier while there are still things in the event completion FIFO waiting to be written back to memory.

Quoted 7 lines

Work around that by adding a fence wait before context switch. The CP_EVENT_WRITE that writes the fence is the last write from a submit, so seeing this value hit memory is a reliable indication that it is safe to proceed with the context switch.

-Akhil

Quoted 55 lines
---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +11 -3
index bcaec86ac67a..ba5b35502e6d 100644
@@ -101,9 +101,10 @@ static void get_stats_counter(struct msm_ringbuffer *ring, u32 counter,
 }
 
 static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
-		struct msm_ringbuffer *ring, struct msm_file_private *ctx)
+		struct msm_ringbuffer *ring, struct msm_gem_submit *submit)
 {
 	bool sysprof = refcount_read(&a6xx_gpu->base.base.sysprof_active) > 1;
+	struct msm_file_private *ctx = submit->queue->ctx;
 	struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
 	phys_addr_t ttbr;
 	u32 asid;
@@ -115,6 +116,13 @@ static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
 	if (msm_iommu_pagetable_params(ctx->aspace->mmu, &ttbr, &asid))
 		return;
 
+	/* Wait for previous submit to complete before continuing: */
+	OUT_PKT7(ring, CP_WAIT_TIMESTAMP, 4);
+	OUT_RING(ring, 0);
+	OUT_RING(ring, lower_32_bits(rbmemptr(ring, fence)));
+	OUT_RING(ring, upper_32_bits(rbmemptr(ring, fence)));
+	OUT_RING(ring, submit->seqno - 1);
+
 	if (!sysprof) {
 		if (!adreno_is_a7xx(adreno_gpu)) {
 			/* Turn off protected mode to write to special registers */
@@ -193,7 +201,7 @@ static void a6xx_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
 	struct msm_ringbuffer *ring = submit->ring;
 	unsigned int i, ibs = 0;
 
-	a6xx_set_pagetable(a6xx_gpu, ring, submit->queue->ctx);
+	a6xx_set_pagetable(a6xx_gpu, ring, submit);
 
 	get_stats_counter(ring, REG_A6XX_RBBM_PERFCTR_CP(0),
 		rbmemptr_stats(ring, index, cpcycles_start));
@@ -283,7 +291,7 @@ static void a7xx_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
 	OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
 	OUT_RING(ring, CP_THREAD_CONTROL_0_SYNC_THREADS | CP_SET_THREAD_BR);
 
-	a6xx_set_pagetable(a6xx_gpu, ring, submit->queue->ctx);
+	a6xx_set_pagetable(a6xx_gpu, ring, submit);
 
 	get_stats_counter(ring, REG_A7XX_RBBM_PERFCTR_CP(0),
 		rbmemptr_stats(ring, index, cpcycles_start));
Signature · 1 line
2.46.0