Skip to contentSkip to conversation

drm/msm/a7xx: Call CP_RESET_CONTEXT_STATE

PATCH In review Connor Abbott 2 participants 6 messages May 20, 2025 – May 22, 2025

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

cover drm/msm/a7xx: Call CP_RESET_CONTEXT_STATE

This was missed during a7xx bringup and will become necessary for safety once Mesa starts using BV seriously. I'm hoping to backport this because it is a security fix and not new functionality even though we aren't yet using the userspace bits that will be insecure without it in Mesa.

The initial a7xx support landed before the register XML modified in patch 1 was introduced, but the XML seems to have landed in the same cycle so we shouldn't need a separate backport of the generated C headers.

---
Connor Abbott (2):
drm/msm: Fix CP_RESET_CONTEXT_STATE bitfield names drm/msm/a7xx: Call CP_RESET_CONTEXT_STATE

 drivers/gpu/drm/msm/adreno/a6xx_gpu.c               | 14 ++++++++++++++
 drivers/gpu/drm/msm/registers/adreno/adreno_pm4.xml |  3 ++-
 2 files changed, 16 insertions(+), 1 deletion(-)

---
base-commit: 8ffd015db85fea3e15a77027fda6c02ced4d2444
change-id: 20250520-msm-reset-context-state-100249ca5368

Best regards,

Signature · 1 line
Connor Abbott <[email protected]>
#1 lore raw

PATCH 1/2 drm/msm: Fix CP_RESET_CONTEXT_STATE bitfield names

Based on kgsl.

Fixes af66706accdf ("drm/msm/a6xx: Add skeleton A7xx support")
---
 drivers/gpu/drm/msm/registers/adreno/adreno_pm4.xml | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/msm/registers/adreno/adreno_pm4.xml +2 -1
index 55a35182858ccac3292849faaf12727257e053c7..2134731a86ba819215476d89c1e054328f901dd1 100644
@@ -2255,7 +2255,8 @@ opcode: CP_LOAD_STATE4 (30) (4 dwords)
 	<reg32 offset="0" name="0">
 		<bitfield name="CLEAR_ON_CHIP_TS" pos="0" type="boolean"/>
 		<bitfield name="CLEAR_RESOURCE_TABLE" pos="1" type="boolean"/>
-		<bitfield name="CLEAR_GLOBAL_LOCAL_TS" pos="2" type="boolean"/>
+		<bitfield name="CLEAR_BV_BR_COUNTER" pos="2" type="boolean"/>
+		<bitfield name="RESET_GLOBAL_LOCAL_TS" pos="3" type="boolean"/>
 	</reg32>
 </domain>
 
Signature · 1 line
2.47.1
#2 lore raw

PATCH 2/2 drm/msm/a7xx: Call CP_RESET_CONTEXT_STATE

In reply to Connor Abbott

Calling this packet is necessary when we switch contexts because there are various pieces of state used by userspace to synchronize between BR and BV that are persistent across submits and we need to make sure that they are in a "safe" state when switching contexts. Otherwise a userspace submission in one context could cause another context to function incorrectly and hang, effectively a denial of service (although without leaking data). This was missed during initial a7xx bringup.

Fixes af66706accdf ("drm/msm/a6xx: Add skeleton A7xx support")
---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +14 -0
index 06465bc2d0b4b128cddfcfcaf1fe4252632b6777..f776e9ce43a7cdbb4ef769606ec851909b0c4cdd 100644
@@ -130,6 +130,20 @@ static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
 		OUT_RING(ring, lower_32_bits(rbmemptr(ring, fence)));
 		OUT_RING(ring, upper_32_bits(rbmemptr(ring, fence)));
 		OUT_RING(ring, submit->seqno - 1);
+
+		OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
+		OUT_RING(ring, CP_SET_THREAD_BOTH);
+
+		/* Reset state used to synchronize BR and BV */
+		OUT_PKT7(ring, CP_RESET_CONTEXT_STATE, 1);
+		OUT_RING(ring,
+			 CP_RESET_CONTEXT_STATE_0_CLEAR_ON_CHIP_TS |
+			 CP_RESET_CONTEXT_STATE_0_CLEAR_RESOURCE_TABLE |
+			 CP_RESET_CONTEXT_STATE_0_CLEAR_BV_BR_COUNTER |
+			 CP_RESET_CONTEXT_STATE_0_RESET_GLOBAL_LOCAL_TS);
+
+		OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
+		OUT_RING(ring, CP_SET_THREAD_BR);
 	}
 
 	if (!sysprof) {
Signature · 1 line
2.47.1
#3 lore raw
Quoted 19 lines · Connor Abbott

Calling this packet is necessary when we switch contexts because there are various pieces of state used by userspace to synchronize between BR and BV that are persistent across submits and we need to make sure that they are in a "safe" state when switching contexts. Otherwise a userspace submission in one context could cause another context to function incorrectly and hang, effectively a denial of service (although without leaking data). This was missed during initial a7xx bringup.

Fixes af66706accdf ("drm/msm/a6xx: Add skeleton A7xx support")
---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +0 -0
index 06465bc2d0b4b128cddfcfcaf1fe4252632b6777..f776e9ce43a7cdbb4ef769606ec851909b0c4cdd 100644

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

Quoted 6 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +3 -0
@@ -130,6 +130,20 @@ static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
 		OUT_RING(ring, lower_32_bits(rbmemptr(ring, fence)));
 		OUT_RING(ring, upper_32_bits(rbmemptr(ring, fence)));
 		OUT_RING(ring, submit->seqno - 1);
+
+		OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
+		OUT_RING(ring, CP_SET_THREAD_BOTH);

Downstream does CP_SYNC_THREADS | CP_SET_THREAD_BOTH, fwiw

Konrad

Quoted 15 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +11 -0
+
+		/* Reset state used to synchronize BR and BV */
+		OUT_PKT7(ring, CP_RESET_CONTEXT_STATE, 1);
+		OUT_RING(ring,
+			 CP_RESET_CONTEXT_STATE_0_CLEAR_ON_CHIP_TS |
+			 CP_RESET_CONTEXT_STATE_0_CLEAR_RESOURCE_TABLE |
+			 CP_RESET_CONTEXT_STATE_0_CLEAR_BV_BR_COUNTER |
+			 CP_RESET_CONTEXT_STATE_0_RESET_GLOBAL_LOCAL_TS);
+
+		OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
+		OUT_RING(ring, CP_SET_THREAD_BR);
 	}
 
 	if (!sysprof) {
#4 lore raw
Quoted 30 lines · Konrad Dybcio
Quoted 19 lines · Connor Abbott

Calling this packet is necessary when we switch contexts because there are various pieces of state used by userspace to synchronize between BR and BV that are persistent across submits and we need to make sure that they are in a "safe" state when switching contexts. Otherwise a userspace submission in one context could cause another context to function incorrectly and hang, effectively a denial of service (although without leaking data). This was missed during initial a7xx bringup.

Fixes af66706accdf ("drm/msm/a6xx: Add skeleton A7xx support")
---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +0 -0
index 06465bc2d0b4b128cddfcfcaf1fe4252632b6777..f776e9ce43a7cdbb4ef769606ec851909b0c4cdd 100644

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

Quoted 6 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +3 -0
@@ -130,6 +130,20 @@ static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
              OUT_RING(ring, lower_32_bits(rbmemptr(ring, fence)));
              OUT_RING(ring, upper_32_bits(rbmemptr(ring, fence)));
              OUT_RING(ring, submit->seqno - 1);
+
+             OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
+             OUT_RING(ring, CP_SET_THREAD_BOTH);

Downstream does CP_SYNC_THREADS | CP_SET_THREAD_BOTH, fwiw

Quoted 1 line

Konrad

Unfortunately the rest of the sequence downstream is slightly different so I had to adapt this somewhat. Downstream does this as the very first thing whereas we have a CP_SYNC_THREADS | CP_SET_THREAD_BR beforehand so the threads are already synced, and there were a few other minor differences iirc.

Connor

Quoted 16 lines
Quoted 16 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +11 -0
+
+             /* Reset state used to synchronize BR and BV */
+             OUT_PKT7(ring, CP_RESET_CONTEXT_STATE, 1);
+             OUT_RING(ring,
+                      CP_RESET_CONTEXT_STATE_0_CLEAR_ON_CHIP_TS |
+                      CP_RESET_CONTEXT_STATE_0_CLEAR_RESOURCE_TABLE |
+                      CP_RESET_CONTEXT_STATE_0_CLEAR_BV_BR_COUNTER |
+                      CP_RESET_CONTEXT_STATE_0_RESET_GLOBAL_LOCAL_TS);
+
+             OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
+             OUT_RING(ring, CP_SET_THREAD_BR);
      }
 
      if (!sysprof) {
#5 lore raw
Quoted 32 lines · Connor Abbott
Quoted 30 lines · Konrad Dybcio
Quoted 19 lines · Connor Abbott

Calling this packet is necessary when we switch contexts because there are various pieces of state used by userspace to synchronize between BR and BV that are persistent across submits and we need to make sure that they are in a "safe" state when switching contexts. Otherwise a userspace submission in one context could cause another context to function incorrectly and hang, effectively a denial of service (although without leaking data). This was missed during initial a7xx bringup.

Fixes af66706accdf ("drm/msm/a6xx: Add skeleton A7xx support")
---
 drivers/gpu/drm/msm/adreno/a6xx_gpu.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +0 -0
index 06465bc2d0b4b128cddfcfcaf1fe4252632b6777..f776e9ce43a7cdbb4ef769606ec851909b0c4cdd 100644

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

Quoted 6 lines
drivers/gpu/drm/msm/adreno/a6xx_gpu.c +3 -0
@@ -130,6 +130,20 @@ static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
              OUT_RING(ring, lower_32_bits(rbmemptr(ring, fence)));
              OUT_RING(ring, upper_32_bits(rbmemptr(ring, fence)));
              OUT_RING(ring, submit->seqno - 1);
+
+             OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
+             OUT_RING(ring, CP_SET_THREAD_BOTH);

Downstream does CP_SYNC_THREADS | CP_SET_THREAD_BOTH, fwiw

Quoted 7 lines
Quoted 1 line

Konrad

Unfortunately the rest of the sequence downstream is slightly different so I had to adapt this somewhat. Downstream does this as the very first thing whereas we have a CP_SYNC_THREADS | CP_SET_THREAD_BR beforehand so the threads are already synced, and there were a few other minor differences iirc.

Seems you're right, thanks for confirming!

Konrad