Implement the gfx callbacks to save and restore a gfx HQD for gfx12:
- save_hqd() saves the HQD offset, CSMD read pointer and write pointer
  of the gfx HQD of a doorbell, and whether the queue is the one
  connected to its pipe;
- restore_hqd() writes them into the MQD after a reset, for MES to map
  the queue again where it was. A queue switched out of the pipe resumes
  from the preemption status the PFP saved in its MQD, also in the
  middle of an IB. The queue the pipe was executing has its state in the
  PFP, which the reset cleared: it restarts after the work submitted so
  far instead, and that work is dropped.

v4: Rename to save_hqd()/restore_hqd() (Timur).

Reviewed-by: Timur Kristóf <[email protected]>
Signed-off-by: Jesse Zhang <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 83 ++++++++++++++++++++++++++
 1 file changed, 83 insertions(+)

diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c 
b/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
index 09a27c3f6921..8ca8e3f8392d 100644
--- a/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
+++ b/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
@@ -274,6 +274,11 @@ static void gfx_v12_0_set_imu_funcs(struct amdgpu_device 
*adev);
 static int gfx_v12_0_get_cu_info(struct amdgpu_device *adev,
                                 struct amdgpu_cu_info *cu_info);
 static uint64_t gfx_v12_0_get_gpu_clock_counter(struct amdgpu_device *adev);
+static bool gfx_v12_0_save_hqd(struct amdgpu_device *adev, u32 doorbell_index,
+                              struct amdgpu_gfx_saved_hqd *hqd);
+static bool gfx_v12_0_restore_hqd(struct amdgpu_device *adev, void *m,
+                                 u32 ring_size,
+                                 const struct amdgpu_gfx_saved_hqd *hqd);
 static void gfx_v12_0_select_se_sh(struct amdgpu_device *adev, u32 se_num,
                                   u32 sh_num, u32 instance, int xcc_id);
 static u32 gfx_v12_0_get_wgp_active_bitmap_per_sh(struct amdgpu_device *adev);
@@ -967,6 +972,8 @@ static const struct amdgpu_gfx_funcs gfx_v12_0_gfx_funcs = {
        .set_perfmon_clk_override = &gfx_v12_0_set_perfmon_clk_override,
        .get_gfx_shadow_info = &gfx_v12_0_get_gfx_shadow_info,
        .get_hdp_flush_mask = &amdgpu_gfx_get_hdp_flush_mask,
+       .save_hqd = &gfx_v12_0_save_hqd,
+       .restore_hqd = &gfx_v12_0_restore_hqd,
 };
 
 static int gfx_v12_0_gpu_early_init(struct amdgpu_device *adev)
@@ -3861,6 +3868,82 @@ static int gfx_v12_0_wait_for_idle(struct 
amdgpu_ip_block *ip_block)
        return -ETIMEDOUT;
 }
 
+#define GFX12_HPD_QUEUE_STATE_CONNECTED_MIN    3
+#define GFX12_HPD_QUEUE_STATE_CONNECTED_MAX    15
+
+static bool gfx_v12_0_save_hqd(struct amdgpu_device *adev, u32 doorbell_index,
+                              struct amdgpu_gfx_saved_hqd *hqd)
+{
+       u32 me, pipe, queue, status, state;
+
+       memset(hqd, 0, sizeof(*hqd));
+       hqd->doorbell_index = doorbell_index;
+       if (!gfx_v12_0_detect_hung_queue(adev, doorbell_index, &me, &pipe, 
&queue))
+               return true;
+
+       amdgpu_gfx_off_ctrl(adev, false);
+       mutex_lock(&adev->srbm_mutex);
+       soc24_grbm_select(adev, me, pipe, queue, 0);
+       hqd->on_hqd = RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE) &
+                     CP_GFX_HQD_ACTIVE__ACTIVE_MASK;
+       if (!hqd->on_hqd)
+               goto out;
+       hqd->mqd_addr = RREG32_SOC15(GC, 0, regCP_GFX_MQD_BASE_ADDR) |
+                       (u64)RREG32_SOC15(GC, 0, regCP_GFX_MQD_BASE_ADDR_HI) << 
32;
+       hqd->offset = RREG32_SOC15(GC, 0, regCP_GFX_HQD_OFFSET);
+       hqd->csmd_rptr = RREG32_SOC15(GC, 0, regCP_GFX_HQD_CSMD_RPTR);
+       hqd->wptr = RREG32_SOC15(GC, 0, regCP_GFX_HQD_WPTR) |
+                   (u64)RREG32_SOC15(GC, 0, regCP_GFX_HQD_WPTR_HI) << 32;
+       status = RREG32_SOC15(GC, 0, regCP_GFX_HPD_STATUS0);
+       state = REG_GET_FIELD(status, CP_GFX_HPD_STATUS0, QUEUE_STATE);
+       hqd->connected =
+               state >= GFX12_HPD_QUEUE_STATE_CONNECTED_MIN &&
+               state <= GFX12_HPD_QUEUE_STATE_CONNECTED_MAX &&
+               REG_GET_FIELD(status, CP_GFX_HPD_STATUS0, MAPPED_QUEUE) == 
queue;
+out:
+       soc24_grbm_select(adev, 0, 0, 0, 0);
+       mutex_unlock(&adev->srbm_mutex);
+       amdgpu_gfx_off_ctrl(adev, true);
+
+       return true;
+}
+
+static bool gfx_v12_0_restore_hqd(struct amdgpu_device *adev, void *m,
+                                 u32 ring_size,
+                                 const struct amdgpu_gfx_saved_hqd *hqd)
+{
+       struct v12_gfx_mqd *mqd = m;
+       u32 wptr = lower_32_bits(hqd->wptr) & ((ring_size >> 2) - 1);
+       u32 *status;
+
+       /*
+        * MQD dwords 32-41 (reserved_32..41) are the PFP preemption status,
+        * mcbp_status_pfp: the offset and chained IB size and address of IB1
+        * (32-35) and IB2 (36-39), rb_preempt (40: preempted in the ring) and
+        * completion_status (41: 0 IB not started, 1 preempted inside an IB,
+        * 2 IB completed, 3 IB resumed). The PFP saves it when the queue is
+        * switched out of the pipe and resumes from it when the queue runs
+        * again, at the HQD offset. That survives the reset for a queue that
+        * was switched out. The queue the pipe executed has its state in the
+        * PFP, which the reset cleared: restart it at the write pointer with
+        * no IB to resume, and drop its work in flight.
+        */
+       if (hqd->connected) {
+               mqd->cp_gfx_hqd_offset = wptr;
+               mqd->cp_gfx_hqd_csmd_rptr = wptr;
+               for (status = &mqd->reserved_32; status <= &mqd->reserved_41; 
status++)
+                       *status = 0;
+       } else {
+               mqd->cp_gfx_hqd_offset = hqd->offset;
+               mqd->cp_gfx_hqd_csmd_rptr = hqd->csmd_rptr;
+       }
+       mqd->cp_gfx_hqd_wptr = lower_32_bits(hqd->wptr);
+       mqd->cp_gfx_hqd_wptr_hi = upper_32_bits(hqd->wptr);
+       mqd->cp_gfx_hqd_active = 0;
+
+       return hqd->connected;
+}
+
 static uint64_t gfx_v12_0_get_gpu_clock_counter(struct amdgpu_device *adev)
 {
        uint64_t clock = 0;
-- 
2.50.1

Reply via email to