Implement soft_reset_engine() for the graphics engine of gfx11, as for
gfx12.

The CPF/CPG reset already puts PFP/ME back at their firmware start
address, so unhalting them restarts the RS64 and F32 firmware alike.

v4: Implement soft_reset_engine() directly instead of a reset_engine gfx
    callback, drop gfx_v11_0_restart_gfx_rs64(), and boot with
    CP_PFP_IB_CONTROL cleared for the F32 firmware (Timur).
v5: Don't save and restore CP_PFP_IB_CONTROL: the reset clears it, so
    the F32 PFP boots as it should, and RLC sets it again by itself
    (Timur).

Signed-off-by: Jesse Zhang <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 127 +++++++++++++++++++++++++
 1 file changed, 127 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c 
b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
index ecf8a156b7ec..a0ed3363f84d 100644
--- a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
+++ b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
@@ -6820,6 +6820,132 @@ static void gfx_v11_0_emit_mem_sync(struct amdgpu_ring 
*ring)
        amdgpu_ring_write(ring, gcr_cntl); /* GCR_CNTL */
 }
 
+/* The CP DMA and the interfaces its memory requests go through */
+#define GFX11_CP_STAT_MEM_BUSY (CP_STAT__DMA_BUSY_MASK | \
+                                CP_STAT__TCIU_BUSY_MASK | \
+                                CP_STAT__RCIU_BUSY_MASK | \
+                                CP_STAT__UTCL2IU_BUSY_MASK)
+
+/* Reset CPF/CPG, PFP/ME and the gfx HQDs, then boot PFP/ME again. */
+static int gfx_v11_0_soft_reset_gfx_cp(struct amdgpu_device *adev)
+{
+       u32 tmp, cntl, me_cntl;
+       int i;
+
+       amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
+       /* MES fetches its queues through CPF: keep it out of the reset. */
+       amdgpu_mes_lock(&adev->mes);
+
+       me_cntl = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+
+       tmp = REG_SET_FIELD(me_cntl, CP_ME_CNTL, ME_HALT, 1);
+       tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_HALT, 1);
+       WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
+       udelay(50);
+
+       /* A CP DMA in flight during the reset leaves the ME busy after it. */
+       for (i = 0; i < adev->usec_timeout; i++) {
+               if (!(RREG32_SOC15(GC, 0, regCP_STAT) & GFX11_CP_STAT_MEM_BUSY))
+                       break;
+               udelay(1);
+       }
+
+       /* GFX_ONLY keeps compute and MES out; CPF alone leaves the stream 
stuck. */
+       cntl = RREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL);
+       tmp = REG_SET_FIELD(cntl, CP_SOFT_RESET_CNTL, GFX_ONLY_SOFT_RESET, 1);
+       tmp = REG_SET_FIELD(tmp, CP_SOFT_RESET_CNTL, GFX_HQD_REG_RESET, 1);
+       WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, tmp);
+
+       tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+       tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 1);
+       tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 1);
+       WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+       tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+       udelay(50);
+       tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPF, 0);
+       tmp = REG_SET_FIELD(tmp, GRBM_SOFT_RESET, SOFT_RESET_CPG, 0);
+       WREG32_SOC15(GC, 0, regGRBM_SOFT_RESET, tmp);
+       tmp = RREG32_SOC15(GC, 0, regGRBM_SOFT_RESET);
+
+       WREG32_SOC15(GC, 0, regCP_SOFT_RESET_CNTL, cntl);
+
+       /* The halted PFP stays busy: only wait for the memory requests. */
+       for (i = 0; i < adev->usec_timeout; i++) {
+               if (!(RREG32_SOC15(GC, 0, regCP_STAT) & GFX11_CP_STAT_MEM_BUSY))
+                       break;
+               udelay(1);
+       }
+       /* A VMID reset the hung PFP didn't handle survives the reset. */
+       WREG32_SOC15(GC, 0, regCP_VMID_RESET, 0);
+       amdgpu_mes_unlock(&adev->mes);
+
+       tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+       tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, CE_HALT,
+                           REG_GET_FIELD(me_cntl, CP_ME_CNTL, CE_HALT));
+       WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
+       amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
+
+       tmp = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+       tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, ME_HALT, 0);
+       tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_HALT, 0);
+       WREG32_SOC15(GC, 0, regCP_ME_CNTL, tmp);
+       for (i = 0; i < adev->usec_timeout; i++) {
+               if (!RREG32_SOC15(GC, 0, regCP_STAT))
+                       return 0;
+               udelay(1);
+       }
+       return -ETIMEDOUT;
+}
+
+static int gfx_v11_0_soft_reset_engine(struct amdgpu_ip_block *ip_block,
+                                      enum amdgpu_ring_type ring_type, u32 
inst,
+                                      struct amdgpu_usermode_queue 
*guilty_queue)
+{
+       struct amdgpu_device *adev = ip_block->adev;
+       int i, r;
+
+       if (ring_type != AMDGPU_RING_TYPE_GFX || inst)
+               return -EOPNOTSUPP;
+
+       mutex_lock(&adev->gfx.mec.reset_mutex);
+       amdgpu_gfx_off_ctrl(adev, false);
+
+       /* A hung gfx stream can keep MES from suspending: reset anyway. */
+       amdgpu_mes_suspend(adev, 0);
+       amdgpu_userq_save_hqds(adev, AMDGPU_HW_IP_GFX);
+
+       r = gfx_v11_0_soft_reset_gfx_cp(adev);
+       if (r) {
+               dev_err(adev->dev, "gfx CP still busy after reset, CP_STAT 
0x%08x\n",
+                       RREG32_SOC15(GC, 0, regCP_STAT));
+               goto fail;
+       }
+
+       /* The reset cleared the HQD of the kernel gfx queue: map it again. */
+       r = amdgpu_gfx_disable_kgq(adev, 0);
+       if (r)
+               goto fail;
+       for (i = 0; i < adev->gfx.num_gfx_rings; i++)
+               amdgpu_gfx_mqd_reset_restore(&adev->gfx.gfx_ring[i]);
+       r = amdgpu_gfx_enable_kgq(adev, 0);
+       if (!r)
+               r = gfx_v11_0_cp_gfx_start(adev);
+       if (r)
+               goto fail;
+
+       amdgpu_userq_restore_hqds(adev, AMDGPU_HW_IP_GFX, guilty_queue);
+       r = amdgpu_mes_resume(adev, 0);
+       goto out;
+
+fail:
+       amdgpu_userq_clear_hqds(adev, AMDGPU_HW_IP_GFX);
+       amdgpu_mes_resume(adev, 0);
+out:
+       amdgpu_gfx_off_ctrl(adev, true);
+       mutex_unlock(&adev->gfx.mec.reset_mutex);
+       return r;
+}
+
 #define GFX11_HPD_QUEUE_STATE_CONNECTED_MIN    3
 #define GFX11_HPD_QUEUE_STATE_CONNECTED_MAX    15
 
@@ -7088,6 +7214,7 @@ static const struct amd_ip_funcs gfx_v11_0_ip_funcs = {
        .suspend = gfx_v11_0_suspend,
        .resume = gfx_v11_0_resume,
        .wait_for_idle = gfx_v11_0_wait_for_idle,
+       .soft_reset_engine = gfx_v11_0_soft_reset_engine,
        .soft_reset = gfx_v11_0_soft_reset,
        .set_clockgating_state = gfx_v11_0_set_clockgating_state,
        .set_powergating_state = gfx_v11_0_set_powergating_state,
-- 
2.50.1

Reply via email to