<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<div>
<div>
<div style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);" dir="ltr">
Ping…</div>
</div>
<div id="ms-outlook-mobile-signature">
<div></div>
</div>
</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> ricetons@gmail.com <ricetons@gmail.com><br>
<b>Sent:</b> Saturday, April 30, 2022 3:34:00 PM<br>
<b>To:</b> amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org><br>
<b>Cc:</b> lang.yu@amd.com <lang.yu@amd.com>; ckoenig.leichtzumerken@gmail.com <ckoenig.leichtzumerken@gmail.com>; Guchun.Chen@amd.com <Guchun.Chen@amd.com>; yifan1.zhang@amd.com <yifan1.zhang@amd.com>; Hawking.Zhang@amd.com <Hawking.Zhang@amd.com>; Haohui
 Mai <ricetons@gmail.com><br>
<b>Subject:</b> [PATCH v6] drm/amdgpu: Ensure the DMA engine is deactivated during set ups</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">From: Haohui Mai <ricetons@gmail.com><br>
<br>
The patch fully deactivates the DMA engine before setting up the ring<br>
buffer to avoid potential data races and crashes.<br>
<br>
Signed-off-by: Haohui Mai <ricetons@gmail.com><br>
---<br>
 drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c | 109 +++++++++++++++----------<br>
 1 file changed, 64 insertions(+), 45 deletions(-)<br>
<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c b/drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c<br>
index 013d2dec81d0..1fac9d8e99de 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c<br>
+++ b/drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c<br>
@@ -459,7 +459,6 @@ static void sdma_v5_2_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 se<br>
         }<br>
 }<br>
 <br>
-<br>
 /**<br>
  * sdma_v5_2_gfx_stop - stop the gfx async dma engines<br>
  *<br>
@@ -505,17 +504,21 @@ static void sdma_v5_2_rlc_stop(struct amdgpu_device *adev)<br>
 }<br>
 <br>
 /**<br>
- * sdma_v5_2_ctx_switch_enable - stop the async dma engines context switch<br>
+ * sdma_v5_2_ctx_switch_enable_for_instance - start the async dma engines<br>
+ * context switch for an instance<br>
  *<br>
  * @adev: amdgpu_device pointer<br>
- * @enable: enable/disable the DMA MEs context switch.<br>
+ * @instance_idx: the index of the SDMA instance<br>
  *<br>
- * Halt or unhalt the async dma engines context switch.<br>
+ * Unhalt the async dma engines context switch.<br>
  */<br>
-static void sdma_v5_2_ctx_switch_enable(struct amdgpu_device *adev, bool enable)<br>
+static void sdma_v5_2_ctx_switch_enable_for_instance(struct amdgpu_device *adev, int instance_idx)<br>
 {<br>
         u32 f32_cntl, phase_quantum = 0;<br>
-       int i;<br>
+<br>
+       if (WARN_ON(instance_idx >= adev->sdma.num_instances)) {<br>
+               return;<br>
+       }<br>
 <br>
         if (amdgpu_sdma_phase_quantum) {<br>
                 unsigned value = amdgpu_sdma_phase_quantum;<br>
@@ -539,50 +542,68 @@ static void sdma_v5_2_ctx_switch_enable(struct amdgpu_device *adev, bool enable)<br>
                 phase_quantum =<br>
                         value << SDMA0_PHASE0_QUANTUM__VALUE__SHIFT |<br>
                         unit  << SDMA0_PHASE0_QUANTUM__UNIT__SHIFT;<br>
-       }<br>
-<br>
-       for (i = 0; i < adev->sdma.num_instances; i++) {<br>
-               if (enable && amdgpu_sdma_phase_quantum) {<br>
-                       WREG32_SOC15_IP(GC, sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_PHASE0_QUANTUM),<br>
-                              phase_quantum);<br>
-                       WREG32_SOC15_IP(GC, sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_PHASE1_QUANTUM),<br>
-                              phase_quantum);<br>
-                       WREG32_SOC15_IP(GC, sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_PHASE2_QUANTUM),<br>
-                              phase_quantum);<br>
-               }<br>
 <br>
-               if (!amdgpu_sriov_vf(adev)) {<br>
-                       f32_cntl = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CNTL));<br>
-                       f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,<br>
-                                       AUTO_CTXSW_ENABLE, enable ? 1 : 0);<br>
-                       WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CNTL), f32_cntl);<br>
-               }<br>
+               WREG32_SOC15_IP(GC,<br>
+                       sdma_v5_2_get_reg_offset(adev, instance_idx, mmSDMA0_PHASE0_QUANTUM),<br>
+                       phase_quantum);<br>
+               WREG32_SOC15_IP(GC,<br>
+                       sdma_v5_2_get_reg_offset(adev, instance_idx, mmSDMA0_PHASE1_QUANTUM),<br>
+                   phase_quantum);<br>
+               WREG32_SOC15_IP(GC,<br>
+                       sdma_v5_2_get_reg_offset(adev, instance_idx, mmSDMA0_PHASE2_QUANTUM),<br>
+                   phase_quantum);<br>
         }<br>
 <br>
+       if (!amdgpu_sriov_vf(adev)) {<br>
+               f32_cntl = RREG32(sdma_v5_2_get_reg_offset(adev, instance_idx, mmSDMA0_CNTL));<br>
+               f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,<br>
+                               AUTO_CTXSW_ENABLE, 1);<br>
+               WREG32(sdma_v5_2_get_reg_offset(adev, instance_idx, mmSDMA0_CNTL), f32_cntl);<br>
+       }<br>
 }<br>
 <br>
 /**<br>
- * sdma_v5_2_enable - stop the async dma engines<br>
+ * sdma_v5_2_ctx_switch_disable_all - stop the async dma engines context switch<br>
  *<br>
  * @adev: amdgpu_device pointer<br>
- * @enable: enable/disable the DMA MEs.<br>
  *<br>
- * Halt or unhalt the async dma engines.<br>
+ * Halt the async dma engines context switch.<br>
  */<br>
-static void sdma_v5_2_enable(struct amdgpu_device *adev, bool enable)<br>
+static void sdma_v5_2_ctx_switch_disable_all(struct amdgpu_device *adev)<br>
 {<br>
         u32 f32_cntl;<br>
         int i;<br>
 <br>
-       if (!enable) {<br>
-               sdma_v5_2_gfx_stop(adev);<br>
-               sdma_v5_2_rlc_stop(adev);<br>
+       if (amdgpu_sriov_vf(adev))<br>
+               return;<br>
+<br>
+       for (i = 0; i < adev->sdma.num_instances; i++) {<br>
+               f32_cntl = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CNTL));<br>
+               f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,<br>
+                               AUTO_CTXSW_ENABLE, 0);<br>
+               WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_CNTL), f32_cntl);<br>
         }<br>
+}<br>
+<br>
+/**<br>
+ * sdma_v5_2_halt - stop the async dma engines<br>
+ *<br>
+ * @adev: amdgpu_device pointer<br>
+ *<br>
+ * Halt the async dma engines.<br>
+ */<br>
+static void sdma_v5_2_halt(struct amdgpu_device *adev)<br>
+{<br>
+       int i;<br>
+       u32 f32_cntl;<br>
+<br>
+       sdma_v5_2_gfx_stop(adev);<br>
+       sdma_v5_2_rlc_stop(adev);<br>
 <br>
         if (!amdgpu_sriov_vf(adev)) {<br>
                 for (i = 0; i < adev->sdma.num_instances; i++) {<br>
                         f32_cntl = RREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_F32_CNTL));<br>
-                       f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_F32_CNTL, HALT, enable ? 0 : 1);<br>
+                       f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_F32_CNTL, HALT, 1);<br>
                         WREG32(sdma_v5_2_get_reg_offset(adev, i, mmSDMA0_F32_CNTL), f32_cntl);<br>
                 }<br>
         }<br>
@@ -594,6 +615,9 @@ static void sdma_v5_2_enable(struct amdgpu_device *adev, bool enable)<br>
  * @adev: amdgpu_device pointer<br>
  *<br>
  * Set up the gfx DMA ring buffers and enable them.<br>
+ * It assumes that the dma engine is stopped for each instance.<br>
+ * The function enables the engine and preemptions sequentially for each instance.<br>
+ *<br>
  * Returns 0 for success, error for failure.<br>
  */<br>
 static int sdma_v5_2_gfx_resume(struct amdgpu_device *adev)<br>
@@ -737,10 +761,7 @@ static int sdma_v5_2_gfx_resume(struct amdgpu_device *adev)<br>
 <br>
                 ring->sched.ready = true;<br>
 <br>
-               if (amdgpu_sriov_vf(adev)) { /* bare-metal sequence doesn't need below to lines */<br>
-                       sdma_v5_2_ctx_switch_enable(adev, true);<br>
-                       sdma_v5_2_enable(adev, true);<br>
-               }<br>
+               sdma_v5_2_ctx_switch_enable_for_instance(adev, i);<br>
 <br>
                 r = amdgpu_ring_test_ring(ring);<br>
                 if (r) {<br>
@@ -784,7 +805,7 @@ static int sdma_v5_2_load_microcode(struct amdgpu_device *adev)<br>
         int i, j;<br>
 <br>
         /* halt the MEs */<br>
-       sdma_v5_2_enable(adev, false);<br>
+       sdma_v5_2_halt(adev);<br>
 <br>
         for (i = 0; i < adev->sdma.num_instances; i++) {<br>
                 if (!adev->sdma.instance[i].fw)<br>
@@ -856,8 +877,8 @@ static int sdma_v5_2_start(struct amdgpu_device *adev)<br>
         int r = 0;<br>
 <br>
         if (amdgpu_sriov_vf(adev)) {<br>
-               sdma_v5_2_ctx_switch_enable(adev, false);<br>
-               sdma_v5_2_enable(adev, false);<br>
+               sdma_v5_2_ctx_switch_disable_all(adev);<br>
+               sdma_v5_2_halt(adev);<br>
 <br>
                 /* set RB registers */<br>
                 r = sdma_v5_2_gfx_resume(adev);<br>
@@ -881,12 +902,10 @@ static int sdma_v5_2_start(struct amdgpu_device *adev)<br>
                 amdgpu_gfx_off_ctrl(adev, false);<br>
 <br>
         sdma_v5_2_soft_reset(adev);<br>
-       /* unhalt the MEs */<br>
-       sdma_v5_2_enable(adev, true);<br>
-       /* enable sdma ring preemption */<br>
-       sdma_v5_2_ctx_switch_enable(adev, true);<br>
 <br>
-       /* start the gfx rings and rlc compute queues */<br>
+       /* Soft reset supposes to disable the dma engine and preemption.<br>
+        * Now start the gfx rings and rlc compute queues.<br>
+        */<br>
         r = sdma_v5_2_gfx_resume(adev);<br>
         if (adev->in_s0ix)<br>
                 amdgpu_gfx_off_ctrl(adev, true);<br>
@@ -1340,8 +1359,8 @@ static int sdma_v5_2_hw_fini(void *handle)<br>
         if (amdgpu_sriov_vf(adev))<br>
                 return 0;<br>
 <br>
-       sdma_v5_2_ctx_switch_enable(adev, false);<br>
-       sdma_v5_2_enable(adev, false);<br>
+       sdma_v5_2_ctx_switch_disable_all(adev);<br>
+       sdma_v5_2_halt(adev);<br>
 <br>
         return 0;<br>
 }<br>
-- <br>
2.25.1<br>
<br>
</div>
</span></font></div>
</body>
</html>