<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<p style="font-family:Arial;font-size:10pt;color:#0000FF;margin:5pt;" align="Left">
[AMD Official Use Only - General]<br>
</p>
<br>
<div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
>> <span style="color:rgb(36, 36, 36);font-family:"Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif;font-size:14.6667px;background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted0">+      
 /* dequeue sched pipe via kiq */</span><br style="color:rgb(36, 36, 36);font-family:"Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif;font-size:14.6667px;background-color:rgb(255, 255, 255)" class="ContentPasted0">
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span style="color:rgb(36, 36, 36);font-family:"Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif;font-size:14.6667px;background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted0">>>
 +       void                            (*kiq_dequeue_sched)(struct amdgpu_device *adev);</span><br style="color:rgb(36, 36, 36);font-family:"Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif;font-size:14.6667px;background-color:rgb(255, 255, 255)" class="ContentPasted0">
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span style="color:rgb(36, 36, 36);font-family:"Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif;font-size:14.6667px;background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted0">>>
 +</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span style="color:rgb(36, 36, 36);font-family:"Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif;font-size:14.6667px;background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted0"><br>
</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span style="color:rgb(36, 36, 36);font-family:"Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif;font-size:14.6667px;background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted0">It's
 unnecessary to expose a new callback function proto to perform dequeuing scheduler queue,</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span style="color:rgb(36, 36, 36);font-family:"Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif;font-size:14.6667px;background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted0">for
 kiq_fini or mes_hw_fini callback had got the propriate sequence to do this </span><span style="color: rgb(36, 36, 36); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px;">during
 fini procedure.</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span style="color: rgb(36, 36, 36); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px;">In addition, it seems that the issue is caused by that
 unclear <span style="background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted1">CP_HQD_PQ_WPTR/CONTROL registers would </span></span>trigger scheduler reading from unexpected address once CP_MES_CNTL is enabled:</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span style="color: rgb(36, 36, 36); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px;"><span style="background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted1">a.
 for dequeue operation uses MMIO rather than kiq, can the dequeue operation move to mes_v11_hw_fini?</span></span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span style="color: rgb(36, 36, 36); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px;"><span style="background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted1 ContentPasted2">b.
 if something went wrong, it's better move the dequeue operation inside mes_v11_0_kiq_hw_fini before disabling mes controller.</span></span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span style="color: rgb(36, 36, 36); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px;"><span style="background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted1 ContentPasted2"><br>
</span></span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span style="color: rgb(36, 36, 36); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px;"><span style="background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted1 ContentPasted2">Regards,</span></span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span style="color: rgb(36, 36, 36); font-family: "Segoe UI", "Segoe UI Web (West European)", "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue", sans-serif; font-size: 14.6667px;"><span style="background-color:rgb(255, 255, 255);display:inline !important" class="ContentPasted1 ContentPasted2">Jack</span></span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div id="appendonsend"></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> YuBiao Wang <YuBiao.Wang@amd.com><br>
<b>Sent:</b> Friday, 14 October 2022 11:34<br>
<b>To:</b> amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org><br>
<b>Cc:</b> Grodzovsky, Andrey <Andrey.Grodzovsky@amd.com>; Quan, Evan <Evan.Quan@amd.com>; Chen, Horace <Horace.Chen@amd.com>; Tuikov, Luben <Luben.Tuikov@amd.com>; Koenig, Christian <Christian.Koenig@amd.com>; Deucher, Alexander <Alexander.Deucher@amd.com>;
 Xiao, Jack <Jack.Xiao@amd.com>; Zhang, Hawking <Hawking.Zhang@amd.com>; Liu, Monk <Monk.Liu@amd.com>; Xu, Feifei <Feifei.Xu@amd.com>; Wang, Yang(Kevin) <KevinYang.Wang@amd.com>; Wang, YuBiao <YuBiao.Wang@amd.com><br>
<b>Subject:</b> [PATCH v3] drm/amdgpu: dequeue mes scheduler during fini</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Update: Remove redundant comments as Christian suggests.<br>
<br>
[Why]<br>
If mes is not dequeued during fini, mes will be in an uncleaned state<br>
during reload, then mes couldn't receive some commands which leads to<br>
reload failure.<br>
<br>
[How]<br>
Perform MES dequeue via MMIO after all the unmap jobs are done by mes<br>
and before kiq fini.<br>
<br>
Signed-off-by: YuBiao Wang <YuBiao.Wang@amd.com><br>
---<br>
 drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h |  3 ++<br>
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  |  3 ++<br>
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c  | 41 +++++++++++++++++++++++--<br>
 3 files changed, 44 insertions(+), 3 deletions(-)<br>
<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h<br>
index ad980f4b66e1..ea8efa52503b 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h<br>
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_mes.h<br>
@@ -130,6 +130,9 @@ struct amdgpu_mes {<br>
         int                             (*kiq_hw_init)(struct amdgpu_device *adev);<br>
         int                             (*kiq_hw_fini)(struct amdgpu_device *adev);<br>
 <br>
+       /* dequeue sched pipe via kiq */<br>
+       void                            (*kiq_dequeue_sched)(struct amdgpu_device *adev);<br>
+<br>
         /* ip specific functions */<br>
         const struct amdgpu_mes_funcs   *funcs;<br>
 };<br>
diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c<br>
index 257b2e4de600..7c75758f58e1 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c<br>
+++ b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c<br>
@@ -4406,6 +4406,9 @@ static int gfx_v11_0_hw_fini(void *handle)<br>
                 if (amdgpu_gfx_disable_kcq(adev))<br>
                         DRM_ERROR("KCQ disable failed\n");<br>
 <br>
+               if (adev->mes.ring.sched.ready && adev->mes.kiq_dequeue_sched)<br>
+                       adev->mes.kiq_dequeue_sched(adev);<br>
+<br>
                 amdgpu_mes_kiq_hw_fini(adev);<br>
         }<br>
 <br>
diff --git a/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c b/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c<br>
index b48684db2832..eef29646b074 100644<br>
--- a/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c<br>
+++ b/drivers/gpu/drm/amd/amdgpu/mes_v11_0.c<br>
@@ -44,6 +44,7 @@ MODULE_FIRMWARE("amdgpu/gc_11_0_3_mes1.bin");<br>
 static int mes_v11_0_hw_fini(void *handle);<br>
 static int mes_v11_0_kiq_hw_init(struct amdgpu_device *adev);<br>
 static int mes_v11_0_kiq_hw_fini(struct amdgpu_device *adev);<br>
+static void mes_v11_0_kiq_dequeue_sched(struct amdgpu_device *adev);<br>
 <br>
 #define MES_EOP_SIZE   2048<br>
 <br>
@@ -1078,6 +1079,7 @@ static int mes_v11_0_sw_init(void *handle)<br>
         adev->mes.funcs = &mes_v11_0_funcs;<br>
         adev->mes.kiq_hw_init = &mes_v11_0_kiq_hw_init;<br>
         adev->mes.kiq_hw_fini = &mes_v11_0_kiq_hw_fini;<br>
+       adev->mes.kiq_dequeue_sched = &mes_v11_0_kiq_dequeue_sched;<br>
 <br>
         r = amdgpu_mes_init(adev);<br>
         if (r)<br>
@@ -1151,6 +1153,42 @@ static int mes_v11_0_sw_fini(void *handle)<br>
         return 0;<br>
 }<br>
 <br>
+static void mes_v11_0_kiq_dequeue_sched(struct amdgpu_device *adev)<br>
+{<br>
+       uint32_t data;<br>
+       int i;<br>
+<br>
+       mutex_lock(&adev->srbm_mutex);<br>
+       soc21_grbm_select(adev, 3, AMDGPU_MES_SCHED_PIPE, 0, 0);<br>
+<br>
+       /* disable the queue if it's active */<br>
+       if (RREG32_SOC15(GC, 0, regCP_HQD_ACTIVE) & 1) {<br>
+               WREG32_SOC15(GC, 0, regCP_HQD_DEQUEUE_REQUEST, 1);<br>
+               for (i = 0; i < adev->usec_timeout; i++) {<br>
+                       if (!(RREG32_SOC15(GC, 0, regCP_HQD_ACTIVE) & 1))<br>
+                               break;<br>
+                       udelay(1);<br>
+               }<br>
+       }<br>
+       data = RREG32_SOC15(GC, 0, regCP_HQD_PQ_DOORBELL_CONTROL);<br>
+       data = REG_SET_FIELD(data, CP_HQD_PQ_DOORBELL_CONTROL,<br>
+                               DOORBELL_EN, 0);<br>
+       data = REG_SET_FIELD(data, CP_HQD_PQ_DOORBELL_CONTROL,<br>
+                               DOORBELL_HIT, 1);<br>
+       WREG32_SOC15(GC, 0, regCP_HQD_PQ_DOORBELL_CONTROL, data);<br>
+<br>
+       WREG32_SOC15(GC, 0, regCP_HQD_PQ_DOORBELL_CONTROL, 0);<br>
+<br>
+       WREG32_SOC15(GC, 0, regCP_HQD_PQ_WPTR_LO, 0);<br>
+       WREG32_SOC15(GC, 0, regCP_HQD_PQ_WPTR_HI, 0);<br>
+       WREG32_SOC15(GC, 0, regCP_HQD_PQ_RPTR, 0);<br>
+<br>
+       soc21_grbm_select(adev, 0, 0, 0, 0);<br>
+       mutex_unlock(&adev->srbm_mutex);<br>
+<br>
+       adev->mes.ring.sched.ready = false;<br>
+}<br>
+<br>
 static void mes_v11_0_kiq_setting(struct amdgpu_ring *ring)<br>
 {<br>
         uint32_t tmp;<br>
@@ -1257,9 +1295,6 @@ static int mes_v11_0_hw_init(void *handle)<br>
 <br>
 static int mes_v11_0_hw_fini(void *handle)<br>
 {<br>
-       struct amdgpu_device *adev = (struct amdgpu_device *)handle;<br>
-<br>
-       adev->mes.ring.sched.ready = false;<br>
         return 0;<br>
 }<br>
 <br>
-- <br>
2.25.1<br>
<br>
</div>
</span></font></div>
</div>
</body>
</html>