Skip to contentSkip to conversation

drm/amdgpu/gfx11: need acquire mutex before access CP_VMID_RESET

PATCH Superseded Jack Xiao 4 participants 6 messages Dec 19, 2023 – Dec 22, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jack Xiao

author
#0 lore raw

PATCH 1/1 drm/amdgpu/gfx11: need acquire mutex before access CP_VMID_RESET

It's required to take the gfx mutex before access to CP_VMID_RESET, for there is a race condition with CP firmware to write the register.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +20 -0
index bdcf96df69e6..ae3370d34d11 100644
@@ -4518,6 +4518,22 @@ static int gfx_v11_0_soft_reset(void *handle)
 		}
 	}
 
+	/* Try to require the gfx mutex before access to CP_VMID_RESET */
+	for (i = 0; i < adev->usec_timeout; i++) {
+		/* Request with MeId=2, PipeId=0 */
+		tmp = REG_SET_FIELD(0, CP_GFX_INDEX_MUTEX, REQUEST, 1);
+		tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, CLIENTID, 4);
+		WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+		if (RREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX) == tmp)
+			break;
+		udelay(1);
+	}
+
+	if (i >= adev->usec_timeout) {
+		printk("Failed to require the gfx mutex during soft reset\n");
+		return -EINVAL;
+	}
+
 	WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0xfffffffe);
 
 	// Read CP_VMID_RESET register three times.
@@ -4526,6 +4542,10 @@ static int gfx_v11_0_soft_reset(void *handle)
 	RREG32_SOC15(GC, 0, regCP_VMID_RESET);
 	RREG32_SOC15(GC, 0, regCP_VMID_RESET);
 
+	/* release the gfx mutex */
+	tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, REQUEST, 0);
+	WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+
 	for (i = 0; i < adev->usec_timeout; i++) {
 		if (!RREG32_SOC15(GC, 0, regCP_HQD_ACTIVE) &&
 		    !RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE))
Signature · 1 line
2.41.0
#1 lore raw

[AMD Official Use Only - General]

+       /* release the gfx mutex */
+       tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, REQUEST, 0);
+       WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);

Shall we add a check by reading back CP_GFX_INDEX_MUTEX to ensure the release is done correctly?

Regards,
Hawking

-----Original Message-----
From: Xiao, Jack <[email protected]>
Sent: Tuesday, December 19, 2023 17:24
To: [email protected]; Deucher, Alexander <[email protected]>; Zhang, Hawking <[email protected]>
Cc: Xiao, Jack <[email protected]>
Subject: [PATCH] drm/amdgpu/gfx11: need acquire mutex before access CP_VMID_RESET

It's required to take the gfx mutex before access to CP_VMID_RESET, for there is a race condition with CP firmware to write the register.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +20 -0
index bdcf96df69e6..ae3370d34d11 100644
@@ -4518,6 +4518,22 @@ static int gfx_v11_0_soft_reset(void *handle)
                }
        }
 
+       /* Try to require the gfx mutex before access to CP_VMID_RESET */
+       for (i = 0; i < adev->usec_timeout; i++) {
+               /* Request with MeId=2, PipeId=0 */
+               tmp = REG_SET_FIELD(0, CP_GFX_INDEX_MUTEX, REQUEST, 1);
+               tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, CLIENTID, 4);
+               WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+               if (RREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX) == tmp)
+                       break;
+               udelay(1);
+       }
+
+       if (i >= adev->usec_timeout) {
+               printk("Failed to require the gfx mutex during soft reset\n");
+               return -EINVAL;
+       }
+
        WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0xfffffffe);
 
        // Read CP_VMID_RESET register three times.
@@ -4526,6 +4542,10 @@ static int gfx_v11_0_soft_reset(void *handle)
        RREG32_SOC15(GC, 0, regCP_VMID_RESET);
        RREG32_SOC15(GC, 0, regCP_VMID_RESET);
 
+       /* release the gfx mutex */
+       tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, REQUEST, 0);
+       WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+
        for (i = 0; i < adev->usec_timeout; i++) {
                if (!RREG32_SOC15(GC, 0, regCP_HQD_ACTIVE) &&
                    !RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE))

--
2.41.0

#2 lore raw
In reply to Jack Xiao
Quoted 14 lines · Jack Xiao

It's required to take the gfx mutex before access to CP_VMID_RESET, for there is a race condition with CP firmware to write the register.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
index bdcf96df69e6..ae3370d34d11 100644

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static int gfx_v11_0_soft_reset(void *handle) · line 4520

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
@@ -4518,6 +4518,22 @@ static int gfx_v11_0_soft_reset(void *handle)
                }
        }
 

We probably want a CPU mutex or spinlock to protect this as well unless this is already protected by the reset lock.

Alex

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +20 -1
+       /* Try to require the gfx mutex before access to CP_VMID_RESET */
+       for (i = 0; i < adev->usec_timeout; i++) {
+               /* Request with MeId=2, PipeId=0 */
+               tmp = REG_SET_FIELD(0, CP_GFX_INDEX_MUTEX, REQUEST, 1);
+               tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, CLIENTID, 4);
+               WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+               if (RREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX) == tmp)
+                       break;
+               udelay(1);
+       }
+
+       if (i >= adev->usec_timeout) {
+               printk("Failed to require the gfx mutex during soft reset\n");
+               return -EINVAL;
+       }
+
        WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0xfffffffe);
 
        // Read CP_VMID_RESET register three times.
@@ -4526,6 +4542,10 @@ static int gfx_v11_0_soft_reset(void *handle)
        RREG32_SOC15(GC, 0, regCP_VMID_RESET);
        RREG32_SOC15(GC, 0, regCP_VMID_RESET);
 
+       /* release the gfx mutex */
+       tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, REQUEST, 0);
+       WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+
        for (i = 0; i < adev->usec_timeout; i++) {
                if (!RREG32_SOC15(GC, 0, regCP_HQD_ACTIVE) &&
                    !RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE))
--

2.41.0

author
#3 lore raw

[AMD Official Use Only - General]

It's already protected by the reset lock. In my understanding, soft reset should not run in parallel.

Regards,
Jack

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Wednesday, December 20, 2023 1:04 AM
To: Xiao, Jack <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Zhang, Hawking <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/gfx11: need acquire mutex before access CP_VMID_RESET

Quoted 15 lines · Jack Xiao

It's required to take the gfx mutex before access to CP_VMID_RESET, for there is a race condition with CP firmware to write the register.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
index bdcf96df69e6..ae3370d34d11 100644

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static int gfx_v11_0_soft_reset(void *handle) · line 4520

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
@@ -4518,6 +4518,22 @@ static int gfx_v11_0_soft_reset(void *handle)
                }
        }
 

We probably want a CPU mutex or spinlock to protect this as well unless this is already protected by the reset lock.

Alex

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +20 -1
+       /* Try to require the gfx mutex before access to CP_VMID_RESET */
+       for (i = 0; i < adev->usec_timeout; i++) {
+               /* Request with MeId=2, PipeId=0 */
+               tmp = REG_SET_FIELD(0, CP_GFX_INDEX_MUTEX, REQUEST, 1);
+               tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, CLIENTID, 4);
+               WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+               if (RREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX) == tmp)
+                       break;
+               udelay(1);
+       }
+
+       if (i >= adev->usec_timeout) {
+               printk("Failed to require the gfx mutex during soft reset\n");
+               return -EINVAL;
+       }
+
        WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0xfffffffe);
 
        // Read CP_VMID_RESET register three times.
@@ -4526,6 +4542,10 @@ static int gfx_v11_0_soft_reset(void *handle)
        RREG32_SOC15(GC, 0, regCP_VMID_RESET);
        RREG32_SOC15(GC, 0, regCP_VMID_RESET);
 
+       /* release the gfx mutex */
+       tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, REQUEST, 0);
+       WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+
        for (i = 0; i < adev->usec_timeout; i++) {
                if (!RREG32_SOC15(GC, 0, regCP_HQD_ACTIVE) &&
                    !RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE))
--

2.41.0

#4 lore raw

Well not the reset lock, but there should only be a single reset queue which this runs on.

Regards,
Christian.

Am 20.12.23 um 10:49 schrieb Xiao, Jack:

Quoted 69 lines

[AMD Official Use Only - General]

It's already protected by the reset lock. In my understanding, soft reset should not run in parallel.

Regards,
Jack

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Wednesday, December 20, 2023 1:04 AM
To: Xiao, Jack <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Zhang, Hawking <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/gfx11: need acquire mutex before access CP_VMID_RESET

Quoted 14 lines · Jack Xiao

It's required to take the gfx mutex before access to CP_VMID_RESET, for there is a race condition with CP firmware to write the register.

---
  drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 20 ++++++++++++++++++++
  1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
index bdcf96df69e6..ae3370d34d11 100644

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static int gfx_v11_0_soft_reset(void *handle) · line 4520

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
@@ -4518,6 +4518,22 @@ static int gfx_v11_0_soft_reset(void *handle)
                 }
         }
 

We probably want a CPU mutex or spinlock to protect this as well unless this is already protected by the reset lock.

Alex

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +20 -1
+       /* Try to require the gfx mutex before access to CP_VMID_RESET */
+       for (i = 0; i < adev->usec_timeout; i++) {
+               /* Request with MeId=2, PipeId=0 */
+               tmp = REG_SET_FIELD(0, CP_GFX_INDEX_MUTEX, REQUEST, 1);
+               tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, CLIENTID, 4);
+               WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+               if (RREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX) == tmp)
+                       break;
+               udelay(1);
+       }
+
+       if (i >= adev->usec_timeout) {
+               printk("Failed to require the gfx mutex during soft reset\n");
+               return -EINVAL;
+       }
+
         WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0xfffffffe);
 
         // Read CP_VMID_RESET register three times.
@@ -4526,6 +4542,10 @@ static int gfx_v11_0_soft_reset(void *handle)
         RREG32_SOC15(GC, 0, regCP_VMID_RESET);
         RREG32_SOC15(GC, 0, regCP_VMID_RESET);
 
+       /* release the gfx mutex */
+       tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, REQUEST, 0);
+       WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+
         for (i = 0; i < adev->usec_timeout; i++) {
                 if (!RREG32_SOC15(GC, 0, regCP_HQD_ACTIVE) &&
                     !RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE))
--

2.41.0

author
#5 lore raw

[AMD Official Use Only - General]

I can see the soft reset can be called from amdgpu_device_gpu_recover and pci error handler, they have called amdgpu_device_lock_reset_domain to acquire a reset lock before soft reset.

Regards,
Jack

-----Original Message-----
From: Christian König <[email protected]>
Sent: Wednesday, December 20, 2023 10:06 PM
To: Xiao, Jack <[email protected]>; Alex Deucher <[email protected]>
Cc: Deucher, Alexander <[email protected]>; [email protected]; Zhang, Hawking <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/gfx11: need acquire mutex before access CP_VMID_RESET

Well not the reset lock, but there should only be a single reset queue which this runs on.

Regards,
Christian.

Am 20.12.23 um 10:49 schrieb Xiao, Jack:

Quoted 71 lines

[AMD Official Use Only - General]

It's already protected by the reset lock. In my understanding, soft reset should not run in parallel.

Regards,
Jack

-----Original Message-----
From: Alex Deucher <[email protected]>
Sent: Wednesday, December 20, 2023 1:04 AM
To: Xiao, Jack <[email protected]>
Cc: [email protected]; Deucher, Alexander <[email protected]>; Zhang, Hawking <[email protected]>
Subject: Re: [PATCH] drm/amdgpu/gfx11: need acquire mutex before access CP_VMID_RESET

Quoted 14 lines · Jack Xiao

It's required to take the gfx mutex before access to CP_VMID_RESET, for there is a race condition with CP firmware to write the register.

---
  drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 20 ++++++++++++++++++++
  1 file changed, 20 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
index bdcf96df69e6..ae3370d34d11 100644

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static int gfx_v11_0_soft_reset(void *handle) · line 4520

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
@@ -4518,6 +4518,22 @@ static int gfx_v11_0_soft_reset(void *handle)
                 }
         }
 

We probably want a CPU mutex or spinlock to protect this as well unless this is already protected by the reset lock.

Alex

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +20 -1
+       /* Try to require the gfx mutex before access to CP_VMID_RESET */
+       for (i = 0; i < adev->usec_timeout; i++) {
+               /* Request with MeId=2, PipeId=0 */
+               tmp = REG_SET_FIELD(0, CP_GFX_INDEX_MUTEX, REQUEST, 1);
+               tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, CLIENTID, 4);
+               WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+               if (RREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX) == tmp)
+                       break;
+               udelay(1);
+       }
+
+       if (i >= adev->usec_timeout) {
+               printk("Failed to require the gfx mutex during soft reset\n");
+               return -EINVAL;
+       }
+
         WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0xfffffffe);
 
         // Read CP_VMID_RESET register three times.
@@ -4526,6 +4542,10 @@ static int gfx_v11_0_soft_reset(void *handle)
         RREG32_SOC15(GC, 0, regCP_VMID_RESET);
         RREG32_SOC15(GC, 0, regCP_VMID_RESET);
 
+       /* release the gfx mutex */
+       tmp = REG_SET_FIELD(tmp, CP_GFX_INDEX_MUTEX, REQUEST, 0);
+       WREG32_SOC15(GC, 0, regCP_GFX_INDEX_MUTEX, tmp);
+
         for (i = 0; i < adev->usec_timeout; i++) {
                 if (!RREG32_SOC15(GC, 0, regCP_HQD_ACTIVE) &&
                     !RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE))
--

2.41.0