Skip to contentSkip to conversation

drm/amdgpu/gfx9: replace BUG_ON/BUG with WARN_ON_ONCE in ring emission

PATCH In review John B. Moore 2 participants 4 messages Apr 29 – Apr 30

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

PATCH v4 2/2 drm/amdgpu/ring: extract kiq_read_clock to common HW-agnostic code

In reply to John B. Moore

Move gfx_v9_0_kiq_read_clock() from gfx_v9_0.c to amdgpu_ring.c as amdgpu_kiq_read_clock(). The function uses PACKET3_COPY_DATA to read the GPU clock counter via the KIQ ring and is not in any way HW generation dependent -- it can be reused by any GFX IP version that needs KIQ-based clock reads in SRIOV environments.

Preserve the original block comment explaining the gpu_recover() deadlock avoidance logic for the reset-path bail-out.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h      |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 85 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c    | 74 +--------------------
 3 files changed, 88 insertions(+), 72 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +1 -0
index 447e734c3..75e200211 100644
@@ -524,6 +524,7 @@ struct amdgpu_wb {
 
 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb);
 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb);
+uint64_t amdgpu_kiq_read_clock(struct amdgpu_device *adev);
 
 /*
  * Benchmarking
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +85 -0

94 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +2 -72

86 lines not on this page · raw message

Signature · 1 line
2.43.0
#0 lore raw

PATCH v4 1/2 drm/amdgpu/gfx9: replace BUG_ON/BUG with WARN_ON_ONCE in ring emission

Replace all BUG_ON() and BUG() assertions in the gfx_v9_0 ring emission paths with WARN_ON_ONCE() and graceful recovery, or proper error returns.

Nine sites are converted across wait_reg_mem, gpu_early_init, parse_ind_reg_list, init_rlc_save_restore_list, kiq_read_clock, emit_ib_gfx, emit_ib_compute, emit_fence, and get/set_wptr_compute.

Changes since v3 (incorporating Christian König's review):
- gfx_v9_1_init_rlc_save_restore_list: return -EINVAL and kfree() instead of WARN_ON_ONCE + break, to properly abort driver loading. Caller gfx_v9_0_init_pg now checks the return value.
- gfx_v9_0_kiq_read_clock: drop the BUG_ON(!emit_rreg) check entirely rather than converting to WARN_ON. If KIQ cannot emit registers the driver would never have loaded in the first place.
- gfx_v9_0_ring_get/set_wptr_compute: drop the if (ring->use_doorbell) guard entirely since gfx9 compute always uses doorbell. Replace atomic64_read/set with READ_ONCE/WRITE_ONCE since wptr_cpu_addr points to system memory, not MMIO.

The remaining BUG_ON in emit_fence_kiq (64-bit flag) is addressed separately in the KIQ fence flag patch series.

Found by a custom amdgpu DRM ioctl fuzzer.

Fixes b1023571479020e9 ("drm/amdgpu: implement GFX 9.0 support (v2)")
---
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c | 60 ++++++++++++++-------------
 1 file changed, 31 insertions(+), 29 deletions(-)
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +31 -29
index 7e9d753f4..1153121e0 100644
@@ -1182,8 +1182,8 @@ static void gfx_v9_0_wait_reg_mem(struct amdgpu_ring *ring, int eng_sel,
 				 WAIT_REG_MEM_FUNCTION(3) |  /* equal */
 				 WAIT_REG_MEM_ENGINE(eng_sel)));
 
-	if (mem_space)
-		BUG_ON(addr0 & 0x3); /* Dword align */
+	if (mem_space && WARN_ON_ONCE(addr0 & 0x3))
+		addr0 &= ~0x3; /* Force dword align */
 	amdgpu_ring_write(ring, addr0);
 	amdgpu_ring_write(ring, addr1);
 	amdgpu_ring_write(ring, ref);
@@ -2107,8 +2107,10 @@ static int gfx_v9_0_gpu_early_init(struct amdgpu_device *adev)
 			return err;
 		break;
 	default:
-		BUG();
-		break;
+		dev_err(adev->dev,
+			"unsupported GFX IP version 0x%x for gfx_v9_0\n",
+			amdgpu_ip_version(adev, GC_HWIP, 0));
+		return -EINVAL;
 	}
 
 	adev->gfx.config.gb_addr_config = gb_addr_config;
@@ -2808,7 +2810,8 @@ static void gfx_v9_1_parse_ind_reg_list(int *register_list_format,
 					break;
 			}
 
-			BUG_ON(idx >= unique_indirect_reg_count);
+			if (WARN_ON_ONCE(idx >= unique_indirect_reg_count))
+				break;
 
 			if (!unique_indirect_regs[idx])
 				unique_indirect_regs[idx] = register_list_format[indirect_offset];
@@ -2885,7 +2888,11 @@ static int gfx_v9_1_init_rlc_save_restore_list(struct amdgpu_device *adev)
 			}
 		}
 
-		BUG_ON(j >= unique_indirect_reg_count);
+		if (j >= unique_indirect_reg_count) {
+			dev_err(adev->dev, "init_rlc_save_restore_list: indirect reg not found in unique list\n");
+			kfree(register_list_format);
+			return -EINVAL;
+		}
 
 		i++;
 	}
@@ -3103,8 +3110,10 @@ static void gfx_v9_0_init_pg(struct amdgpu_device *adev)
 	if (adev->gfx.rlc.is_rlc_v2_1) {
 		if (amdgpu_ip_version(adev, GC_HWIP, 0) ==
 			    IP_VERSION(9, 2, 1) ||
-		    (adev->apu_flags & AMD_APU_IS_RAVEN2))
-			gfx_v9_1_init_rlc_save_restore_list(adev);
+		    (adev->apu_flags & AMD_APU_IS_RAVEN2)) {
+			if (gfx_v9_1_init_rlc_save_restore_list(adev))
+				return;
+		}
 		gfx_v9_0_enable_save_restore_machine(adev);
 	}
 
@@ -4205,8 +4214,6 @@ static uint64_t gfx_v9_0_kiq_read_clock(struct amdgpu_device *adev)
 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[0];
 	struct amdgpu_ring *ring = &kiq->ring;
 
-	BUG_ON(!ring->funcs->emit_rreg);
-
 	spin_lock_irqsave(&kiq->ring_lock, flags);
 	if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
 		pr_err("critical bug! too many kiq readers\n");
@@ -5427,7 +5434,8 @@ static void gfx_v9_0_ring_emit_ib_gfx(struct amdgpu_ring *ring,
 	}
 
 	amdgpu_ring_write(ring, header);
-	BUG_ON(ib->gpu_addr & 0x3); /* Dword align */
+	if (WARN_ON_ONCE(ib->gpu_addr & 0x3)) /* Dword align */
+		ib->gpu_addr &= ~0x3ULL;
 	amdgpu_ring_write(ring,
 #ifdef __BIG_ENDIAN
 		(2 << 0) |
@@ -5523,7 +5531,8 @@ static void gfx_v9_0_ring_emit_ib_compute(struct amdgpu_ring *ring,
 	}
 
 	amdgpu_ring_write(ring, PACKET3(PACKET3_INDIRECT_BUFFER, 2));
-	BUG_ON(ib->gpu_addr & 0x3); /* Dword align */
+	if (WARN_ON_ONCE(ib->gpu_addr & 0x3)) /* Dword align */
+		ib->gpu_addr &= ~0x3ULL;
 	amdgpu_ring_write(ring,
 #ifdef __BIG_ENDIAN
 				(2 << 0) |
@@ -5563,10 +5572,13 @@ static void gfx_v9_0_ring_emit_fence(struct amdgpu_ring *ring, u64 addr,
 	 * the address should be Qword aligned if 64bit write, Dword
 	 * aligned if only send 32bit data low (discard data high)
 	 */
-	if (write64bit)
-		BUG_ON(addr & 0x7);
-	else
-		BUG_ON(addr & 0x3);
+	if (write64bit) {
+		if (WARN_ON_ONCE(addr & 0x7))
+			addr &= ~0x7ULL;
+	} else {
+		if (WARN_ON_ONCE(addr & 0x3))
+			addr &= ~0x3ULL;
+	}
 	amdgpu_ring_write(ring, lower_32_bits(addr));
 	amdgpu_ring_write(ring, upper_32_bits(addr));
 	amdgpu_ring_write(ring, lower_32_bits(seq));
@@ -5632,14 +5644,8 @@ static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 
 static u64 gfx_v9_0_ring_get_wptr_compute(struct amdgpu_ring *ring)
 {
-	u64 wptr;
-
 	/* XXX check if swapping is necessary on BE */
-	if (ring->use_doorbell)
-		wptr = atomic64_read((atomic64_t *)ring->wptr_cpu_addr);
-	else
-		BUG();
-	return wptr;
+	return READ_ONCE(*(u64 *)ring->wptr_cpu_addr);
 }
 
 static void gfx_v9_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
@@ -5647,12 +5653,8 @@ static void gfx_v9_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
 	struct amdgpu_device *adev = ring->adev;
 
 	/* XXX check if swapping is necessary on BE */
-	if (ring->use_doorbell) {
-		atomic64_set((atomic64_t *)ring->wptr_cpu_addr, ring->wptr);
-		WDOORBELL64(ring->doorbell_index, ring->wptr);
-	} else{
-		BUG(); /* only DOORBELL method supported on gfx9 now */
-	}
+	WRITE_ONCE(*(u64 *)ring->wptr_cpu_addr, ring->wptr);
+	WDOORBELL64(ring->doorbell_index, ring->wptr);
 }
 
 static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v4 2/2] drm/amdgpu/ring: extract kiq_read_clock to common HW-agnostic code

In reply to John B. Moore
Quoted 6 lines · John B. Moore

Move gfx_v9_0_kiq_read_clock() from gfx_v9_0.c to amdgpu_ring.c as amdgpu_kiq_read_clock(). The function uses PACKET3_COPY_DATA to read the GPU clock counter via the KIQ ring and is not in any way HW generation dependent -- it can be reused by any GFX IP version that needs KIQ-based clock reads in SRIOV environments.

This function is dependent. The packet format format is specific to an IP type and version. I think the idea was to move gfx_v*_ring_get_wptr_compute() to a general helper in amdgpu_gfx.c. E.g., amdgpu_gfx_get_wptr_doorbell() That said, the BUG() (i.e., the non-doorbell path could be implemented, it's just more costly because you have to use the GRBM_INDEX register to access the registers indirectly.

Alex

Quoted 219 lines

Preserve the original block comment explaining the gpu_recover() deadlock avoidance logic for the reset-path bail-out.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h      |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 85 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c    | 74 +--------------------
 3 files changed, 88 insertions(+), 72 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +1 -0
index 447e734c3..75e200211 100644
@@ -524,6 +524,7 @@ struct amdgpu_wb {
 
 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb);
 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb);
+uint64_t amdgpu_kiq_read_clock(struct amdgpu_device *adev);
 
 /*
  * Benchmarking
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +85 -0
index 4638a686a..6049215ce 100644
@@ -35,6 +35,7 @@
 #include "amdgpu.h"
 #include "amdgpu_ras_mgr.h"
 #include "atom.h"
+#include "sid.h"
 
 /*
  * Rings
@@ -926,3 +927,87 @@ bool amdgpu_ring_is_reset_type_supported(struct amdgpu_ring *ring,
        }
        return false;
 }
+
+/**
+ * amdgpu_kiq_read_clock - read GPU clock via KIQ ring
+ *
+ * @adev: amdgpu_device pointer
+ *
+ * Use the KIQ (Kernel Interface Queue) to issue a COPY_DATA packet
+ * that reads the GPU clock counter into a writeback buffer.
+ * This is HW-generation agnostic and can be used by any IP that
+ * needs to read the GPU clock via KIQ in SRIOV environments.
+ *
+ * Returns the 64-bit GPU clock value, or ~0 on failure.
+ */
+uint64_t amdgpu_kiq_read_clock(struct amdgpu_device *adev)
+{
+       signed long r, cnt = 0;
+       unsigned long flags;
+       uint32_t seq, reg_val_offs = 0;
+       uint64_t value = 0;
+       struct amdgpu_kiq *kiq = &adev->gfx.kiq[0];
+       struct amdgpu_ring *ring = &kiq->ring;
+
+       spin_lock_irqsave(&kiq->ring_lock, flags);
+       if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
+               pr_err("critical bug! too many kiq readers\n");
+               goto failed_unlock;
+       }
+       amdgpu_ring_alloc(ring, 32);
+       amdgpu_ring_write(ring, PACKET3(PACKET3_COPY_DATA, 4));
+       amdgpu_ring_write(ring, 9 |             /* src: GPU clock */
+                               (5 << 8) |      /* dst: memory */
+                               (1 << 16) |     /* count sel */
+                               (1 << 20));     /* write confirm */
+       amdgpu_ring_write(ring, 0);
+       amdgpu_ring_write(ring, 0);
+       amdgpu_ring_write(ring, lower_32_bits(adev->wb.gpu_addr +
+                               reg_val_offs * 4));
+       amdgpu_ring_write(ring, upper_32_bits(adev->wb.gpu_addr +
+                               reg_val_offs * 4));
+       r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
+       if (r)
+               goto failed_undo;
+
+       amdgpu_ring_commit(ring);
+       spin_unlock_irqrestore(&kiq->ring_lock, flags);
+
+       r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
+
+       /* don't wait anymore for gpu reset case because this way may
+        * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
+        * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
+        * never return if we keep waiting in virt_kiq_rreg, which cause
+        * gpu_recover() hang there.
+        *
+        * also don't wait anymore for IRQ context
+        * */
+       if (r < 1 && (amdgpu_in_reset(adev)))
+               goto failed_kiq_read;
+
+       might_sleep();
+       while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
+               msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
+               r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
+       }
+
+       if (cnt > MAX_KIQ_REG_TRY)
+               goto failed_kiq_read;
+
+       mb();
+       value = (uint64_t)adev->wb.wb[reg_val_offs] |
+               (uint64_t)adev->wb.wb[reg_val_offs + 1] << 32ULL;
+       amdgpu_device_wb_free(adev, reg_val_offs);
+       return value;
+
+failed_undo:
+       amdgpu_ring_undo(ring);
+failed_unlock:
+       spin_unlock_irqrestore(&kiq->ring_lock, flags);
+failed_kiq_read:
+       if (reg_val_offs)
+               amdgpu_device_wb_free(adev, reg_val_offs);
+       pr_err("failed to read gpu clock\n");
+       return ~0;
+}
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +2 -73
index 1153121e0..9ae55b060 100644
@@ -4205,77 +4205,7 @@ static int gfx_v9_0_soft_reset(struct amdgpu_ip_block *ip_block)
        return 0;
 }
 
-static uint64_t gfx_v9_0_kiq_read_clock(struct amdgpu_device *adev)
-{
-       signed long r, cnt = 0;
-       unsigned long flags;
-       uint32_t seq, reg_val_offs = 0;
-       uint64_t value = 0;
-       struct amdgpu_kiq *kiq = &adev->gfx.kiq[0];
-       struct amdgpu_ring *ring = &kiq->ring;
-
-       spin_lock_irqsave(&kiq->ring_lock, flags);
-       if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
-               pr_err("critical bug! too many kiq readers\n");
-               goto failed_unlock;
-       }
-       amdgpu_ring_alloc(ring, 32);
-       amdgpu_ring_write(ring, PACKET3(PACKET3_COPY_DATA, 4));
-       amdgpu_ring_write(ring, 9 |     /* src: register*/
-                               (5 << 8) |      /* dst: memory */
-                               (1 << 16) |     /* count sel */
-                               (1 << 20));     /* write confirm */
-       amdgpu_ring_write(ring, 0);
-       amdgpu_ring_write(ring, 0);
-       amdgpu_ring_write(ring, lower_32_bits(adev->wb.gpu_addr +
-                               reg_val_offs * 4));
-       amdgpu_ring_write(ring, upper_32_bits(adev->wb.gpu_addr +
-                               reg_val_offs * 4));
-       r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
-       if (r)
-               goto failed_undo;
-
-       amdgpu_ring_commit(ring);
-       spin_unlock_irqrestore(&kiq->ring_lock, flags);
-
-       r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
-
-       /* don't wait anymore for gpu reset case because this way may
-        * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
-        * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
-        * never return if we keep waiting in virt_kiq_rreg, which cause
-        * gpu_recover() hang there.
-        *
-        * also don't wait anymore for IRQ context
-        * */
-       if (r < 1 && (amdgpu_in_reset(adev)))
-               goto failed_kiq_read;
-
-       might_sleep();
-       while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
-               msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
-               r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
-       }
-
-       if (cnt > MAX_KIQ_REG_TRY)
-               goto failed_kiq_read;
-
-       mb();
-       value = (uint64_t)adev->wb.wb[reg_val_offs] |
-               (uint64_t)adev->wb.wb[reg_val_offs + 1 ] << 32ULL;
-       amdgpu_device_wb_free(adev, reg_val_offs);
-       return value;
-
-failed_undo:
-       amdgpu_ring_undo(ring);
-failed_unlock:
-       spin_unlock_irqrestore(&kiq->ring_lock, flags);
-failed_kiq_read:
-       if (reg_val_offs)
-               amdgpu_device_wb_free(adev, reg_val_offs);
-       pr_err("failed to read gpu clock\n");
-       return ~0;
-}
+/* kiq_read_clock moved to amdgpu_ring.c as amdgpu_kiq_read_clock() */
 
 static uint64_t gfx_v9_0_get_gpu_clock_counter(struct amdgpu_device *adev)
 {
@@ -4303,7 +4233,7 @@ static uint64_t gfx_v9_0_get_gpu_clock_counter(struct amdgpu_device *adev)
                if (amdgpu_ip_version(adev, GC_HWIP, 0) ==
                            IP_VERSION(9, 0, 1) &&
                    amdgpu_sriov_runtime(adev)) {
-                       clock = gfx_v9_0_kiq_read_clock(adev);
+                       clock = amdgpu_kiq_read_clock(adev);
                } else {
                        WREG32_SOC15(GC, 0, mmRLC_CAPTURE_GPU_CLOCK_COUNT, 1);
                        clock = (uint64_t)RREG32_SOC15(GC, 0, mmRLC_GPU_CLOCK_COUNT_LSB) |
--

2.43.0

author
#3 lore raw

Re: [PATCH v4 2/2] drm/amdgpu/ring: extract kiq_read_clock to common HW-agnostic code

Alex,

Thanks for the correction -- you're right, the PACKET3_COPY_DATA encoding is IP-version-specific. I've dropped that patch entirely.

I have a revised patch that does what you suggested: extracts the doorbell-based get_wptr/set_wptr for compute rings into common helpers in amdgpu_gfx.c (amdgpu_gfx_get_wptr_compute / amdgpu_gfx_set_wptr_compute).

The doorbell path is identical across gfx9, gfx10, gfx11, and gfx12 -- this deduplicates ~120 lines and replaces the BUG() in the non-doorbell fallback with WARN_ON_ONCE.

Not touched: gfx_v7_0, gfx_v8_0, gfx_v9_4_3 (different wptr access patterns).
I sent the revised patch via git-sendmail.

John
"I will not be pushed, filed, stamped, indexed, briefed, debriefed, or numbered."
~ The Prisoner

Quoted 249 lines · Alex Deucher
Quoted 6 lines · John B. Moore

Move gfx_v9_0_kiq_read_clock() from gfx_v9_0.c to amdgpu_ring.c as amdgpu_kiq_read_clock(). The function uses PACKET3_COPY_DATA to read the GPU clock counter via the KIQ ring and is not in any way HW generation dependent -- it can be reused by any GFX IP version that needs KIQ-based clock reads in SRIOV environments.

This function is dependent. The packet format format is specific to an IP type and version. I think the idea was to move gfx_v*_ring_get_wptr_compute() to a general helper in amdgpu_gfx.c. E.g., amdgpu_gfx_get_wptr_doorbell() That said, the BUG() (i.e., the non-doorbell path could be implemented, it's just more costly because you have to use the GRBM_INDEX register to access the registers indirectly.

Alex

Quoted 6 lines

Preserve the original block comment explaining the gpu_recover() deadlock avoidance logic for the reset-path bail-out.

Review

Quoted 7 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h      |  1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c | 85 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c    | 74 +--------------------
 3 files changed, 88 insertions(+), 72 deletions(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu.h

Review

Quoted 12 lines

index 447e734c3..75e200211 100644

drivers/gpu/drm/amd/amdgpu/amdgpu.h +1 -0
@@ -524,6 +524,7 @@ struct amdgpu_wb {
 
 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb);
 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb);
+uint64_t amdgpu_kiq_read_clock(struct amdgpu_device *adev);
 
 /*
  * Benchmarking
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c bool amdgpu_ring_is_reset_type_supported(struct

Quoted 12 lines

index 4638a686a..6049215ce 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +1 -0
@@ -35,6 +35,7 @@
 #include "amdgpu.h"
 #include "amdgpu_ras_mgr.h"
 #include "atom.h"
+#include "sid.h"
 
 /*
  * Rings
@@ -926,3 +927,87 @@ bool amdgpu_ring_is_reset_type_supported(struct

amdgpu_ring *ring,

Quoted 56 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +53 -0
        }
        return false;
 }
+
+/**
+ * amdgpu_kiq_read_clock - read GPU clock via KIQ ring
+ *
+ * @adev: amdgpu_device pointer
+ *
+ * Use the KIQ (Kernel Interface Queue) to issue a COPY_DATA packet
+ * that reads the GPU clock counter into a writeback buffer.
+ * This is HW-generation agnostic and can be used by any IP that
+ * needs to read the GPU clock via KIQ in SRIOV environments.
+ *
+ * Returns the 64-bit GPU clock value, or ~0 on failure.
+ */
+uint64_t amdgpu_kiq_read_clock(struct amdgpu_device *adev)
+{
+       signed long r, cnt = 0;
+       unsigned long flags;
+       uint32_t seq, reg_val_offs = 0;
+       uint64_t value = 0;
+       struct amdgpu_kiq *kiq = &adev->gfx.kiq[0];
+       struct amdgpu_ring *ring = &kiq->ring;
+
+       spin_lock_irqsave(&kiq->ring_lock, flags);
+       if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
+               pr_err("critical bug! too many kiq readers\n");
+               goto failed_unlock;
+       }
+       amdgpu_ring_alloc(ring, 32);
+       amdgpu_ring_write(ring, PACKET3(PACKET3_COPY_DATA, 4));
+       amdgpu_ring_write(ring, 9 |             /* src: GPU clock */
+                               (5 << 8) |      /* dst: memory */
+                               (1 << 16) |     /* count sel */
+                               (1 << 20));     /* write confirm */
+       amdgpu_ring_write(ring, 0);
+       amdgpu_ring_write(ring, 0);
+       amdgpu_ring_write(ring, lower_32_bits(adev->wb.gpu_addr +
+                               reg_val_offs * 4));
+       amdgpu_ring_write(ring, upper_32_bits(adev->wb.gpu_addr +
+                               reg_val_offs * 4));
+       r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
+       if (r)
+               goto failed_undo;
+
+       amdgpu_ring_commit(ring);
+       spin_unlock_irqrestore(&kiq->ring_lock, flags);
+
+       r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
+
+       /* don't wait anymore for gpu reset case because this way may
+        * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
+        * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
+        * never return if we keep waiting in virt_kiq_rreg, which cause
+        * gpu_recover() hang there.

drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c bool amdgpu_ring_is_reset_type_supported(struct amdgpu_ring *ring,

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +10 -0
+        *
+        * also don't wait anymore for IRQ context
+        * */
+       if (r < 1 && (amdgpu_in_reset(adev)))
+               goto failed_kiq_read;
+
+       might_sleep();
+       while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
+               msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
+               r = amdgpu_fence_wait_polling(ring, seq,

MAX_KIQ_REG_WAIT);

Review

Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c +21 -0
+       }
+
+       if (cnt > MAX_KIQ_REG_TRY)
+               goto failed_kiq_read;
+
+       mb();
+       value = (uint64_t)adev->wb.wb[reg_val_offs] |
+               (uint64_t)adev->wb.wb[reg_val_offs + 1] << 32ULL;
+       amdgpu_device_wb_free(adev, reg_val_offs);
+       return value;
+
+failed_undo:
+       amdgpu_ring_undo(ring);
+failed_unlock:
+       spin_unlock_irqrestore(&kiq->ring_lock, flags);
+failed_kiq_read:
+       if (reg_val_offs)
+               amdgpu_device_wb_free(adev, reg_val_offs);
+       pr_err("failed to read gpu clock\n");
+       return ~0;
+}
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static int gfx_v9_0_soft_reset(struct

Quoted 4 lines

index 1153121e0..9ae55b060 100644

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -0
@@ -4205,77 +4205,7 @@ static int gfx_v9_0_soft_reset(struct

amdgpu_ip_block *ip_block)

Quoted 43 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -40
        return 0;
 }
 
-static uint64_t gfx_v9_0_kiq_read_clock(struct amdgpu_device *adev)
-{
-       signed long r, cnt = 0;
-       unsigned long flags;
-       uint32_t seq, reg_val_offs = 0;
-       uint64_t value = 0;
-       struct amdgpu_kiq *kiq = &adev->gfx.kiq[0];
-       struct amdgpu_ring *ring = &kiq->ring;
-
-       spin_lock_irqsave(&kiq->ring_lock, flags);
-       if (amdgpu_device_wb_get(adev, &reg_val_offs)) {
-               pr_err("critical bug! too many kiq readers\n");
-               goto failed_unlock;
-       }
-       amdgpu_ring_alloc(ring, 32);
-       amdgpu_ring_write(ring, PACKET3(PACKET3_COPY_DATA, 4));
-       amdgpu_ring_write(ring, 9 |     /* src: register*/
-                               (5 << 8) |      /* dst: memory */
-                               (1 << 16) |     /* count sel */
-                               (1 << 20));     /* write confirm */
-       amdgpu_ring_write(ring, 0);
-       amdgpu_ring_write(ring, 0);
-       amdgpu_ring_write(ring, lower_32_bits(adev->wb.gpu_addr +
-                               reg_val_offs * 4));
-       amdgpu_ring_write(ring, upper_32_bits(adev->wb.gpu_addr +
-                               reg_val_offs * 4));
-       r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
-       if (r)
-               goto failed_undo;
-
-       amdgpu_ring_commit(ring);
-       spin_unlock_irqrestore(&kiq->ring_lock, flags);
-
-       r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
-
-       /* don't wait anymore for gpu reset case because this way may
-        * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
-        * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
-        * never return if we keep waiting in virt_kiq_rreg, which cause
-        * gpu_recover() hang there.

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static int gfx_v9_0_soft_reset(struct amdgpu_ip_block *ip_block)

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -10
-        *
-        * also don't wait anymore for IRQ context
-        * */
-       if (r < 1 && (amdgpu_in_reset(adev)))
-               goto failed_kiq_read;
-
-       might_sleep();
-       while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
-               msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
-               r = amdgpu_fence_wait_polling(ring, seq,

MAX_KIQ_REG_WAIT);

Quoted 14 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -14
-       }
-
-       if (cnt > MAX_KIQ_REG_TRY)
-               goto failed_kiq_read;
-
-       mb();
-       value = (uint64_t)adev->wb.wb[reg_val_offs] |
-               (uint64_t)adev->wb.wb[reg_val_offs + 1 ] << 32ULL;
-       amdgpu_device_wb_free(adev, reg_val_offs);
-       return value;
-
-failed_undo:
-       amdgpu_ring_undo(ring);
-failed_unlock:

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static int gfx_v9_0_soft_reset(struct amdgpu_ip_block *ip_block)

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +1 -7
-       spin_unlock_irqrestore(&kiq->ring_lock, flags);
-failed_kiq_read:
-       if (reg_val_offs)
-               amdgpu_device_wb_free(adev, reg_val_offs);
-       pr_err("failed to read gpu clock\n");
-       return ~0;
-}
+/* kiq_read_clock moved to amdgpu_ring.c as amdgpu_kiq_read_clock() */
 
 static uint64_t gfx_v9_0_get_gpu_clock_counter(struct amdgpu_device

*adev)

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static uint64_t

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -0
 {
@@ -4303,7 +4233,7 @@ static uint64_t

gfx_v9_0_get_gpu_clock_counter(struct amdgpu_device *adev)

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static uint64_t gfx_v9_0_get_gpu_clock_counter(struct amdgpu_device *adev)

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +1 -1
                if (amdgpu_ip_version(adev, GC_HWIP, 0) ==
                            IP_VERSION(9, 0, 1) &&
                    amdgpu_sriov_runtime(adev)) {
-                       clock = gfx_v9_0_kiq_read_clock(adev);
+                       clock = amdgpu_kiq_read_clock(adev);
                } else {
                        WREG32_SOC15(GC, 0,

mmRLC_CAPTURE_GPU_CLOCK_COUNT, 1);

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static uint64_t · line 4239

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -0
                        clock = (uint64_t)RREG32_SOC15(GC, 0,

mmRLC_GPU_CLOCK_COUNT_LSB) |

Quoted 3 lines

--
2.43.0