Skip to contentSkip to conversation

drm/amdgpu/gfx9: deactivate the KIQ before MODE2 on APUs

PATCH In review Christoph Berliner 2 participants 2 messages Nov 30 – Dec 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu/gfx9: deactivate the KIQ before MODE2 on APUs

MODE2 does not reset the CP HQD registers on gfx9 APUs. "drm/amdgpu/gfx9:
deactivate all compute HQDs before MODE2 on APUs" deactivates all compute HQDs before MODE2 except the KIQ, on the assumption that gfx_v9_0_kiq_init_register() reprograms it on resume. It does, but the KIQ HQD is then still active, so kiq_init_register() takes its "disable the queue if it's active" branch and issues CP_HQD_DEQUEUE_REQUEST right after gfx_v9_0_cp_resume() has halted the MEC and put its pipes into reset. Recovery itself succeeds, but the CP fetcher never becomes idle again. Right after the KIQ setup CP_CPF_BUSY_STAT shows a pending HQD message (HQD_MESSAGE_BUSY, HQD_ROQ_ALIGN_BUSY, HQD_ROQ_PQ_BUSY, HQD_FETCHER_ARB_BUSY). From then on CP_CPF_STATUS keeps HQD_BUSY, CP_CPF_BUSY_STAT keeps HQD_ROQ_PQ_BUSY/HQD_FETCHER_ARB_BUSY and GRBM reports CP_BUSY, while CP_STAT and CP_CPC_BUSY_STAT are 0 and no HQD has pending work. The RLC therefore never sees the GFX block idle: clock gating and light sleep stay off (RLC_GPM_STAT), gpu_busy_percent reads 99 and the sclk stays at the top DPM level until the next reboot.

On a fresh boot the KIQ HQD starts inactive and the CPF stays idle after the KIQ setup. Run the CAIL deinitialize sequence that is already used when unbinding the driver (gfx_v9_0_kiq_fini_register()) before MODE2 on APUs as well. The dequeue is then handled while the MEC is still running, exactly like on unbind, and the KIQ is set up after the reset the same way as on boot. Nothing uses the KIQ after this point of gfx hw_fini during a reset: KCQ unmapping and the GMC TLB flushes are skipped while the reset domain is locked, and KFD only uses the KIQ again in post-reset, after it has been set up again.

Tested on Ryzen 5 5600G (Cezanne, gfx90c, GC 9.3.0), MEC firmware 0x1e3, HWS enabled, amdgpu.noretry=1, with forced MODE2 resets on an idle GPU via amdgpu_gpu_recover. Without this change the GPU stayed at 99 % / max sclk after every reset (5 forced resets, each from a fresh boot; one run
instrumented: CPF idle at the start of gfx_v9_0_hw_init() and busy right after gfx_v9_0_kiq_resume()). With it (3 forced resets, two of them in the same boot without a reboot in between): the KIQ HQD is inactive at hw_init, the CPF is idle after the KIQ setup, and after the resets the GPU idles at 0 % / 400 MHz as after boot, with RLC_GPM_STAT back to its pre-reset value. HIP/rocBLAS and Vulkan work afterwards. Not tested on other gfx9 APUs, and not tested with a reset after a real hang.

Fixes c3988a7ad479 ("drm/amdgpu/gfx9: Fix Ring and IB test fail after mode2")
Assisted-by Claude:Opus 5.5
---
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +10 -3
index 2d8ab438c..c34322442 100644
@@ -4110,7 +4110,8 @@ static bool gfx_v9_0_deactivate_hqd(struct amdgpu_device *adev,
  * active, because KFD cannot unmap/destroy them while the reset domain is
  * locked. After MODE2 the MEC then resumes with stale HQDs and the HWS no
  * longer answers (qcm fence timeout on the next KFD queue). Deactivate every
- * active HQD of all MECs except the KIQ, which is reprogrammed on resume.
+ * active HQD of all MECs except the KIQ, which gfx_v9_0_hw_fini() deactivates
+ * separately with the CAIL deinitialize sequence.
  */
 static void gfx_v9_0_deactivate_all_hqd(struct amdgpu_device *adev)
 {
@@ -4180,9 +4181,15 @@ static int gfx_v9_0_hw_fini(struct amdgpu_ip_block *ip_block)
 		gfx_v9_0_deactivate_all_hqd(adev);
 
 	/* Use deinitialize sequence from CAIL when unbinding device from driver,
-	 * otherwise KIQ is hanging when binding back
+	 * otherwise KIQ is hanging when binding back. MODE2 does not reset the
+	 * HQD registers on APUs either, so do the same before MODE2: otherwise
+	 * the KIQ HQD is still active after the reset, kiq_init_register()
+	 * dequeues it while the MEC is halted, and the CPF stays busy, so GFX
+	 * never goes idle again until reboot.
 	 */
-	if (!amdgpu_in_reset(adev) && !adev->in_suspend) {
+	if ((!amdgpu_in_reset(adev) && !adev->in_suspend) ||
+	    ((adev->flags & AMD_IS_APU) && amdgpu_in_reset(adev) &&
+	     amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_MODE2)) {
 		mutex_lock(&adev->srbm_mutex);
 		soc15_grbm_select(adev, adev->gfx.kiq[0].ring.me,
 				adev->gfx.kiq[0].ring.pipe,
Signature · 1 line
2.56.1
#1 lore raw

Reviewed and applied. Thanks!

Alex

Quoted 83 lines · Christoph Berliner

MODE2 does not reset the CP HQD registers on gfx9 APUs. "drm/amdgpu/gfx9:
deactivate all compute HQDs before MODE2 on APUs" deactivates all compute HQDs before MODE2 except the KIQ, on the assumption that gfx_v9_0_kiq_init_register() reprograms it on resume. It does, but the KIQ HQD is then still active, so kiq_init_register() takes its "disable the queue if it's active" branch and issues CP_HQD_DEQUEUE_REQUEST right after gfx_v9_0_cp_resume() has halted the MEC and put its pipes into reset. Recovery itself succeeds, but the CP fetcher never becomes idle again. Right after the KIQ setup CP_CPF_BUSY_STAT shows a pending HQD message (HQD_MESSAGE_BUSY, HQD_ROQ_ALIGN_BUSY, HQD_ROQ_PQ_BUSY, HQD_FETCHER_ARB_BUSY). From then on CP_CPF_STATUS keeps HQD_BUSY, CP_CPF_BUSY_STAT keeps HQD_ROQ_PQ_BUSY/HQD_FETCHER_ARB_BUSY and GRBM reports CP_BUSY, while CP_STAT and CP_CPC_BUSY_STAT are 0 and no HQD has pending work. The RLC therefore never sees the GFX block idle: clock gating and light sleep stay off (RLC_GPM_STAT), gpu_busy_percent reads 99 and the sclk stays at the top DPM level until the next reboot.

On a fresh boot the KIQ HQD starts inactive and the CPF stays idle after the KIQ setup. Run the CAIL deinitialize sequence that is already used when unbinding the driver (gfx_v9_0_kiq_fini_register()) before MODE2 on APUs as well. The dequeue is then handled while the MEC is still running, exactly like on unbind, and the KIQ is set up after the reset the same way as on boot. Nothing uses the KIQ after this point of gfx hw_fini during a reset: KCQ unmapping and the GMC TLB flushes are skipped while the reset domain is locked, and KFD only uses the KIQ again in post-reset, after it has been set up again.

Tested on Ryzen 5 5600G (Cezanne, gfx90c, GC 9.3.0), MEC firmware 0x1e3, HWS enabled, amdgpu.noretry=1, with forced MODE2 resets on an idle GPU via amdgpu_gpu_recover. Without this change the GPU stayed at 99 % / max sclk after every reset (5 forced resets, each from a fresh boot; one run
instrumented: CPF idle at the start of gfx_v9_0_hw_init() and busy right after gfx_v9_0_kiq_resume()). With it (3 forced resets, two of them in the same boot without a reboot in between): the KIQ HQD is inactive at hw_init, the CPF is idle after the KIQ setup, and after the resets the GPU idles at 0 % / 400 MHz as after boot, with RLC_GPM_STAT back to its pre-reset value. HIP/rocBLAS and Vulkan work afterwards. Not tested on other gfx9 APUs, and not tested with a reset after a real hang.

Fixes c3988a7ad479 ("drm/amdgpu/gfx9: Fix Ring and IB test fail after mode2")
Assisted-by Claude:Opus 5.5
---
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +10 -4
index 2d8ab438c..c34322442 100644
@@ -4110,7 +4110,8 @@ static bool gfx_v9_0_deactivate_hqd(struct amdgpu_device *adev,
  * active, because KFD cannot unmap/destroy them while the reset domain is
  * locked. After MODE2 the MEC then resumes with stale HQDs and the HWS no
  * longer answers (qcm fence timeout on the next KFD queue). Deactivate every
- * active HQD of all MECs except the KIQ, which is reprogrammed on resume.
+ * active HQD of all MECs except the KIQ, which gfx_v9_0_hw_fini() deactivates
+ * separately with the CAIL deinitialize sequence.
  */
 static void gfx_v9_0_deactivate_all_hqd(struct amdgpu_device *adev)
 {
@@ -4180,9 +4181,15 @@ static int gfx_v9_0_hw_fini(struct amdgpu_ip_block *ip_block)
                gfx_v9_0_deactivate_all_hqd(adev);
 
        /* Use deinitialize sequence from CAIL when unbinding device from driver,
-        * otherwise KIQ is hanging when binding back
+        * otherwise KIQ is hanging when binding back. MODE2 does not reset the
+        * HQD registers on APUs either, so do the same before MODE2: otherwise
+        * the KIQ HQD is still active after the reset, kiq_init_register()
+        * dequeues it while the MEC is halted, and the CPF stays busy, so GFX
+        * never goes idle again until reboot.
         */
-       if (!amdgpu_in_reset(adev) && !adev->in_suspend) {
+       if ((!amdgpu_in_reset(adev) && !adev->in_suspend) ||
+           ((adev->flags & AMD_IS_APU) && amdgpu_in_reset(adev) &&
+            amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_MODE2)) {
                mutex_lock(&adev->srbm_mutex);
                soc15_grbm_select(adev, adev->gfx.kiq[0].ring.me,
                                adev->gfx.kiq[0].ring.pipe,
--

2.56.1