Skip to contentSkip to conversation

drm/amdgpu/gfx: extract compute wptr doorbell helpers to amdgpu_gfx.c

PATCH In review John B. Moore 3 participants 8 messages Apr 29 – May 1

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

PATCH 1/1 drm/amdgpu/gfx: extract compute wptr doorbell helpers to amdgpu_gfx.c

Move the duplicated doorbell-based get_wptr/set_wptr functions from gfx_v9_0.c, gfx_v10_0.c, gfx_v11_0.c, and gfx_v12_0.c into common helpers amdgpu_gfx_get_wptr_compute() and amdgpu_gfx_set_wptr_compute() in amdgpu_gfx.c.

These functions are not HW generation dependent -- the doorbell path is identical across all four GFX versions:

get: atomic64_read(ring->wptr_cpu_addr)
set: atomic64_set(ring->wptr_cpu_addr) + WDOORBELL64()

The non-doorbell fallback is replaced with WARN_ON_ONCE instead of BUG() since doorbell is the only supported method on gfx9+ compute rings.

Not touched: gfx_v7_0, gfx_v8_0, gfx_v9_4_3 -- these have different wptr access patterns (MMIO registers or wb.wb[] offsets).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 39 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  3 ++
 drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c  | 33 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  | 34 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c  | 34 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c   | 39 +++----------------------
 6 files changed, 58 insertions(+), 124 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +39 -0
index 77578ecc6..9e9c5cb81 100644
@@ -2596,3 +2596,42 @@ void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)
 #endif
 }
 
+/**
+ * amdgpu_gfx_get_wptr_compute - common get_wptr for compute rings using doorbells
+ * @ring: amdgpu_ring pointer
+ *
+ * Read the write pointer from the doorbell-mapped writeback address.
+ * This is HW-agnostic and shared across GFX generations that use
+ * doorbell-based compute queue management.
+ */
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring)
+{
+	/* XXX check if swapping is necessary on BE */
+	if (ring->use_doorbell)
+		return atomic64_read((atomic64_t *)ring->wptr_cpu_addr);
+
+	WARN_ON_ONCE(1);
+	return 0;
+}
+
+/**
+ * amdgpu_gfx_set_wptr_compute - common set_wptr for compute rings using doorbells
+ * @ring: amdgpu_ring pointer
+ *
+ * Write the write pointer to the doorbell-mapped writeback address and
+ * ring the doorbell.  This is HW-agnostic and shared across GFX
+ * generations that use doorbell-based compute queue management.
+ */
+void amdgpu_gfx_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 {
+		WARN_ON_ONCE(1);
+	}
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
index 585cc8e81..27f6beafb 100644
@@ -653,6 +653,9 @@ u32 amdgpu_gfx_csb_preamble_start(u32 *buffer);
 u32 amdgpu_gfx_csb_data_parser(struct amdgpu_device *adev, u32 *buffer, u32 count);
 void amdgpu_gfx_csb_preamble_end(u32 *buffer, u32 count);
 
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring);
+void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring);
+
 void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device *adev);
 void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev);
 
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +4 -29
index 1893ceeeb..4c0272cba 100644
@@ -8586,31 +8586,6 @@ static u64 gfx_v10_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v10_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;
-}
-
-static void gfx_v10_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
-{
-	struct amdgpu_device *adev = ring->adev;
-
-	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 gfx10 now */
-	}
-}
-
 static void gfx_v10_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -9881,8 +9856,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v10_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v10_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v10_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v10_0_ring_emit_gds_switch */
 		7 + /* gfx_v10_0_ring_emit_hdp_flush */
@@ -9921,8 +9896,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v10_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v10_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v10_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v10_0_ring_emit_gds_switch */
 		7 + /* gfx_v10_0_ring_emit_hdp_flush */
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +4 -30
index 427975b5a..404604f2d 100644
@@ -5818,32 +5818,6 @@ static u64 gfx_v11_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v11_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;
-}
-
-static void gfx_v11_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 gfx11 now */
-	}
-}
-
 static void gfx_v11_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -7266,8 +7240,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v11_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v11_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v11_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		5 + /* update_spm_vmid */
 		20 + /* gfx_v11_0_ring_emit_gds_switch */
@@ -7307,8 +7281,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v11_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v11_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v11_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v11_0_ring_emit_gds_switch */
 		7 + /* gfx_v11_0_ring_emit_hdp_flush */
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +4 -30
index 79ea1af36..7ba436444 100644
@@ -4363,32 +4363,6 @@ static u64 gfx_v12_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v12_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;
-}
-
-static void gfx_v12_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 gfx12 now */
-	}
-}
-
 static void gfx_v12_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -5523,8 +5497,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v12_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v12_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v12_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		7 + /* gfx_v12_0_ring_emit_hdp_flush */
 		5 + /* hdp invalidate */
@@ -5561,8 +5535,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v12_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v12_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v12_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		7 + /* gfx_v12_0_ring_emit_hdp_flush */
 		5 + /*hdp invalidate */
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +4 -35
index 8249135d7..798f94bca 100644
@@ -5640,37 +5640,6 @@ static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *ring->rptr_cpu_addr; /* gfx9 hardware is 32bit rptr */
 }
 
-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 {
-		WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr read on ring %s, "
-			  "only doorbell method supported on gfx9\n",
-			  ring->name);
-		wptr = 0;
-	}
-	return wptr;
-}
-
-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 {
-		WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr write on ring %s, "
-			  "only doorbell method supported on gfx9\n",
-			  ring->name);
-	}
-}
-
 static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
 					 u64 seq, unsigned int flags)
 {
@@ -7627,8 +7596,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v9_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v9_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v9_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v9_0_ring_emit_gds_switch */
 		7 + /* gfx_v9_0_ring_emit_hdp_flush */
@@ -7669,8 +7638,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v9_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v9_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v9_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v9_0_ring_emit_gds_switch */
 		7 + /* gfx_v9_0_ring_emit_hdp_flush */
Signature · 1 line
2.43.0
#1 lore raw
Quoted 36 lines · John B. Moore

Move the duplicated doorbell-based get_wptr/set_wptr functions from gfx_v9_0.c, gfx_v10_0.c, gfx_v11_0.c, and gfx_v12_0.c into common helpers amdgpu_gfx_get_wptr_compute() and amdgpu_gfx_set_wptr_compute() in amdgpu_gfx.c.

These functions are not HW generation dependent -- the doorbell path is identical across all four GFX versions:

get: atomic64_read(ring->wptr_cpu_addr)
set: atomic64_set(ring->wptr_cpu_addr) + WDOORBELL64()

The non-doorbell fallback is replaced with WARN_ON_ONCE instead of BUG() since doorbell is the only supported method on gfx9+ compute rings.

Not touched: gfx_v7_0, gfx_v8_0, gfx_v9_4_3 -- these have different wptr access patterns (MMIO registers or wb.wb[] offsets).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 39 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  3 ++
 drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c  | 33 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  | 34 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c  | 34 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c   | 39 +++----------------------
 6 files changed, 58 insertions(+), 124 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +0 -0
index 77578ecc6..9e9c5cb81 100644
@@ -2596,3 +2596,42 @@ void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)
 #endif
 }

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev) · line 2607

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +9 -0
 
+/**
+ * amdgpu_gfx_get_wptr_compute - common get_wptr for compute rings using doorbells
+ * @ring: amdgpu_ring pointer
+ *
+ * Read the write pointer from the doorbell-mapped writeback address.
+ * This is HW-agnostic and shared across GFX generations that use
+ * doorbell-based compute queue management.
+ */
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring)

I'd prefer to name this amdgpu_gfx_get_wptr_doorbell()

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +8 -0
+{
+       /* XXX check if swapping is necessary on BE */
+       if (ring->use_doorbell)
+               return atomic64_read((atomic64_t *)ring->wptr_cpu_addr);
+
+       WARN_ON_ONCE(1);
+       return 0;
+}

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev) · line 2625

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +10 -0
+
+/**
+ * amdgpu_gfx_set_wptr_compute - common set_wptr for compute rings using doorbells
+ * @ring: amdgpu_ring pointer
+ *
+ * Write the write pointer to the doorbell-mapped writeback address and
+ * ring the doorbell.  This is HW-agnostic and shared across GFX
+ * generations that use doorbell-based compute queue management.
+ */
+void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring)

And amdgpu_gfx_set_wptr_doorbell() here.

While they are used for compute, there is nothing compute specific to them.

Thanks,

Alex

Quoted 269 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +12 -0
+{
+       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 {
+               WARN_ON_ONCE(1);
+       }
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
index 585cc8e81..27f6beafb 100644
@@ -653,6 +653,9 @@ u32 amdgpu_gfx_csb_preamble_start(u32 *buffer);
 u32 amdgpu_gfx_csb_data_parser(struct amdgpu_device *adev, u32 *buffer, u32 count);
 void amdgpu_gfx_csb_preamble_end(u32 *buffer, u32 count);
 
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring);
+void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring);
+
 void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device *adev);
 void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev);
 
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +4 -29
index 1893ceeeb..4c0272cba 100644
@@ -8586,31 +8586,6 @@ static u64 gfx_v10_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
        return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v10_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;
-}
-
-static void gfx_v10_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
-{
-       struct amdgpu_device *adev = ring->adev;
-
-       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 gfx10 now */
-       }
-}
-
 static void gfx_v10_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
        struct amdgpu_device *adev = ring->adev;
@@ -9881,8 +9856,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_compute = {
        .nop = PACKET3(PACKET3_NOP, 0x3FFF),
        .support_64bit_ptrs = true,
        .get_rptr = gfx_v10_0_ring_get_rptr_compute,
-       .get_wptr = gfx_v10_0_ring_get_wptr_compute,
-       .set_wptr = gfx_v10_0_ring_set_wptr_compute,
+       .get_wptr = amdgpu_gfx_get_wptr_compute,
+       .set_wptr = amdgpu_gfx_set_wptr_compute,
        .emit_frame_size =
                20 + /* gfx_v10_0_ring_emit_gds_switch */
                7 + /* gfx_v10_0_ring_emit_hdp_flush */
@@ -9921,8 +9896,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_kiq = {
        .nop = PACKET3(PACKET3_NOP, 0x3FFF),
        .support_64bit_ptrs = true,
        .get_rptr = gfx_v10_0_ring_get_rptr_compute,
-       .get_wptr = gfx_v10_0_ring_get_wptr_compute,
-       .set_wptr = gfx_v10_0_ring_set_wptr_compute,
+       .get_wptr = amdgpu_gfx_get_wptr_compute,
+       .set_wptr = amdgpu_gfx_set_wptr_compute,
        .emit_frame_size =
                20 + /* gfx_v10_0_ring_emit_gds_switch */
                7 + /* gfx_v10_0_ring_emit_hdp_flush */
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +4 -30
index 427975b5a..404604f2d 100644
@@ -5818,32 +5818,6 @@ static u64 gfx_v11_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
        return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v11_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;
-}
-
-static void gfx_v11_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 gfx11 now */
-       }
-}
-
 static void gfx_v11_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
        struct amdgpu_device *adev = ring->adev;
@@ -7266,8 +7240,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_compute = {
        .nop = PACKET3(PACKET3_NOP, 0x3FFF),
        .support_64bit_ptrs = true,
        .get_rptr = gfx_v11_0_ring_get_rptr_compute,
-       .get_wptr = gfx_v11_0_ring_get_wptr_compute,
-       .set_wptr = gfx_v11_0_ring_set_wptr_compute,
+       .get_wptr = amdgpu_gfx_get_wptr_compute,
+       .set_wptr = amdgpu_gfx_set_wptr_compute,
        .emit_frame_size =
                5 + /* update_spm_vmid */
                20 + /* gfx_v11_0_ring_emit_gds_switch */
@@ -7307,8 +7281,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_kiq = {
        .nop = PACKET3(PACKET3_NOP, 0x3FFF),
        .support_64bit_ptrs = true,
        .get_rptr = gfx_v11_0_ring_get_rptr_compute,
-       .get_wptr = gfx_v11_0_ring_get_wptr_compute,
-       .set_wptr = gfx_v11_0_ring_set_wptr_compute,
+       .get_wptr = amdgpu_gfx_get_wptr_compute,
+       .set_wptr = amdgpu_gfx_set_wptr_compute,
        .emit_frame_size =
                20 + /* gfx_v11_0_ring_emit_gds_switch */
                7 + /* gfx_v11_0_ring_emit_hdp_flush */
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +4 -30
index 79ea1af36..7ba436444 100644
@@ -4363,32 +4363,6 @@ static u64 gfx_v12_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
        return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v12_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;
-}
-
-static void gfx_v12_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 gfx12 now */
-       }
-}
-
 static void gfx_v12_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
        struct amdgpu_device *adev = ring->adev;
@@ -5523,8 +5497,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_compute = {
        .nop = PACKET3(PACKET3_NOP, 0x3FFF),
        .support_64bit_ptrs = true,
        .get_rptr = gfx_v12_0_ring_get_rptr_compute,
-       .get_wptr = gfx_v12_0_ring_get_wptr_compute,
-       .set_wptr = gfx_v12_0_ring_set_wptr_compute,
+       .get_wptr = amdgpu_gfx_get_wptr_compute,
+       .set_wptr = amdgpu_gfx_set_wptr_compute,
        .emit_frame_size =
                7 + /* gfx_v12_0_ring_emit_hdp_flush */
                5 + /* hdp invalidate */
@@ -5561,8 +5535,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_kiq = {
        .nop = PACKET3(PACKET3_NOP, 0x3FFF),
        .support_64bit_ptrs = true,
        .get_rptr = gfx_v12_0_ring_get_rptr_compute,
-       .get_wptr = gfx_v12_0_ring_get_wptr_compute,
-       .set_wptr = gfx_v12_0_ring_set_wptr_compute,
+       .get_wptr = amdgpu_gfx_get_wptr_compute,
+       .set_wptr = amdgpu_gfx_set_wptr_compute,
        .emit_frame_size =
                7 + /* gfx_v12_0_ring_emit_hdp_flush */
                5 + /*hdp invalidate */
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +4 -36
index 8249135d7..798f94bca 100644
@@ -5640,37 +5640,6 @@ static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
        return *ring->rptr_cpu_addr; /* gfx9 hardware is 32bit rptr */
 }
 
-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 {
-               WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr read on ring %s, "
-                         "only doorbell method supported on gfx9\n",
-                         ring->name);
-               wptr = 0;
-       }
-       return wptr;
-}
-
-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 {
-               WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr write on ring %s, "
-                         "only doorbell method supported on gfx9\n",
-                         ring->name);
-       }
-}
-
 static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
                                         u64 seq, unsigned int flags)
 {
@@ -7627,8 +7596,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_compute = {
        .nop = PACKET3(PACKET3_NOP, 0x3FFF),
        .support_64bit_ptrs = true,
        .get_rptr = gfx_v9_0_ring_get_rptr_compute,
-       .get_wptr = gfx_v9_0_ring_get_wptr_compute,
-       .set_wptr = gfx_v9_0_ring_set_wptr_compute,
+       .get_wptr = amdgpu_gfx_get_wptr_compute,
+       .set_wptr = amdgpu_gfx_set_wptr_compute,
        .emit_frame_size =
                20 + /* gfx_v9_0_ring_emit_gds_switch */
                7 + /* gfx_v9_0_ring_emit_hdp_flush */
@@ -7669,8 +7638,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_kiq = {
        .nop = PACKET3(PACKET3_NOP, 0x3FFF),
        .support_64bit_ptrs = true,
        .get_rptr = gfx_v9_0_ring_get_rptr_compute,
-       .get_wptr = gfx_v9_0_ring_get_wptr_compute,
-       .set_wptr = gfx_v9_0_ring_set_wptr_compute,
+       .get_wptr = amdgpu_gfx_get_wptr_compute,
+       .set_wptr = amdgpu_gfx_set_wptr_compute,
        .emit_frame_size =
                20 + /* gfx_v9_0_ring_emit_gds_switch */
                7 + /* gfx_v9_0_ring_emit_hdp_flush */
--

2.43.0

maintainer
#2 lore raw
In reply to John B. Moore
Quoted 39 lines · John B. Moore

Move the duplicated doorbell-based get_wptr/set_wptr functions from gfx_v9_0.c, gfx_v10_0.c, gfx_v11_0.c, and gfx_v12_0.c into common helpers amdgpu_gfx_get_wptr_compute() and amdgpu_gfx_set_wptr_compute() in amdgpu_gfx.c.

These functions are not HW generation dependent -- the doorbell path is identical across all four GFX versions:

get: atomic64_read(ring->wptr_cpu_addr)
set: atomic64_set(ring->wptr_cpu_addr) + WDOORBELL64()

The non-doorbell fallback is replaced with WARN_ON_ONCE instead of BUG() since doorbell is the only supported method on gfx9+ compute rings.

Not touched: gfx_v7_0, gfx_v8_0, gfx_v9_4_3 -- these have different wptr access patterns (MMIO registers or wb.wb[] offsets).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 39 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  3 ++
 drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c  | 33 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  | 34 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c  | 34 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c   | 39 +++----------------------
 6 files changed, 58 insertions(+), 124 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -0
index 77578ecc6..9e9c5cb81 100644
@@ -2596,3 +2596,42 @@ void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)
 #endif
 }
 
+/**
+ * amdgpu_gfx_get_wptr_compute - common get_wptr for compute rings using doorbells
+ * @ring: amdgpu_ring pointer

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev) · line 2611

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +10 -0
+ *
+ * Read the write pointer from the doorbell-mapped writeback address.
+ * This is HW-agnostic and shared across GFX generations that use
+ * doorbell-based compute queue management.
+ */
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring)
+{
+	/* XXX check if swapping is necessary on BE */
+	if (ring->use_doorbell)
+		return atomic64_read((atomic64_t *)ring->wptr_cpu_addr);

That should probably be readq() instead of this horrible and not portable cast to atomic64_t.

Alternatively we could just normally read the pointer with a memory barrier since this is just system memory.

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev) · line 2613

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +2 -0
+
+	WARN_ON_ONCE(1);

Pre-requisite/error checking first please.

Make that a if (WARN_ON(!ring->use_doorbell)) return.

And please don't use WARN_ON_ONCE() that is just to reduce the amount of warnings printed into the logs on real HW errors.

On functional coding errors like this one here it doesn't make sense and is often overlooked.

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +8 -0
+	return 0;
+}
+
+/**
+ * amdgpu_gfx_set_wptr_compute - common set_wptr for compute rings using doorbells
+ * @ring: amdgpu_ring pointer
+ *
+ * Write the write pointer to the doorbell-mapped writeback address and

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev) · line 2631

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +10 -0
+ * ring the doorbell.  This is HW-agnostic and shared across GFX
+ * generations that use doorbell-based compute queue management.
+ */
+void amdgpu_gfx_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);

Same here the case to atomic64_t is extremely questionable.

Regards,
Christian.

Quoted 260 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +6 -0
+		WDOORBELL64(ring->doorbell_index, ring->wptr);
+	} else {
+		WARN_ON_ONCE(1);
+	}
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
index 585cc8e81..27f6beafb 100644
@@ -653,6 +653,9 @@ u32 amdgpu_gfx_csb_preamble_start(u32 *buffer);
 u32 amdgpu_gfx_csb_data_parser(struct amdgpu_device *adev, u32 *buffer, u32 count);
 void amdgpu_gfx_csb_preamble_end(u32 *buffer, u32 count);
 
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring);
+void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring);
+
 void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device *adev);
 void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev);
 
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +4 -29
index 1893ceeeb..4c0272cba 100644
@@ -8586,31 +8586,6 @@ static u64 gfx_v10_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v10_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;
-}
-
-static void gfx_v10_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
-{
-	struct amdgpu_device *adev = ring->adev;
-
-	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 gfx10 now */
-	}
-}
-
 static void gfx_v10_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -9881,8 +9856,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v10_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v10_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v10_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v10_0_ring_emit_gds_switch */
 		7 + /* gfx_v10_0_ring_emit_hdp_flush */
@@ -9921,8 +9896,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v10_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v10_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v10_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v10_0_ring_emit_gds_switch */
 		7 + /* gfx_v10_0_ring_emit_hdp_flush */
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +4 -30
index 427975b5a..404604f2d 100644
@@ -5818,32 +5818,6 @@ static u64 gfx_v11_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v11_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;
-}
-
-static void gfx_v11_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 gfx11 now */
-	}
-}
-
 static void gfx_v11_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -7266,8 +7240,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v11_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v11_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v11_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		5 + /* update_spm_vmid */
 		20 + /* gfx_v11_0_ring_emit_gds_switch */
@@ -7307,8 +7281,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v11_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v11_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v11_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v11_0_ring_emit_gds_switch */
 		7 + /* gfx_v11_0_ring_emit_hdp_flush */
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +4 -30
index 79ea1af36..7ba436444 100644
@@ -4363,32 +4363,6 @@ static u64 gfx_v12_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v12_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;
-}
-
-static void gfx_v12_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 gfx12 now */
-	}
-}
-
 static void gfx_v12_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -5523,8 +5497,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v12_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v12_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v12_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		7 + /* gfx_v12_0_ring_emit_hdp_flush */
 		5 + /* hdp invalidate */
@@ -5561,8 +5535,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v12_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v12_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v12_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		7 + /* gfx_v12_0_ring_emit_hdp_flush */
 		5 + /*hdp invalidate */
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +4 -35
index 8249135d7..798f94bca 100644
@@ -5640,37 +5640,6 @@ static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *ring->rptr_cpu_addr; /* gfx9 hardware is 32bit rptr */
 }
 
-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 {
-		WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr read on ring %s, "
-			  "only doorbell method supported on gfx9\n",
-			  ring->name);
-		wptr = 0;
-	}
-	return wptr;
-}
-
-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 {
-		WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr write on ring %s, "
-			  "only doorbell method supported on gfx9\n",
-			  ring->name);
-	}
-}
-
 static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
 					 u64 seq, unsigned int flags)
 {
@@ -7627,8 +7596,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v9_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v9_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v9_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v9_0_ring_emit_gds_switch */
 		7 + /* gfx_v9_0_ring_emit_hdp_flush */
@@ -7669,8 +7638,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v9_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v9_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v9_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v9_0_ring_emit_gds_switch */
 		7 + /* gfx_v9_0_ring_emit_hdp_flush */
author
#3 lore raw

Hi Christian,

Thanks for the review. All points addressed below.

Quoted 5 lines

That should probably be readq() instead of this horrible and not portable cast to atomic64_t.

Alternatively we could just normally read the pointer with a memory barrier since this is just system memory.

I went with the second option — this is system memory (writeback via GTT), not MMIO, so readq() felt semantically wrong. The v2 uses:

  get:  wptr = READ_ONCE(*(u64 *)ring->wptr_cpu_addr);
        smp_rmb();

  set:  WRITE_ONCE(*(u64 *)ring->wptr_cpu_addr, ring->wptr);
        smp_wmb();
        WDOORBELL64(ring->doorbell_index, ring->wptr);

The alignment is safe — amdgpu_device_wb_get() returns offsets in multiples of 8 dwords (32 bytes), so the u64* cast always lands on a naturally-aligned address.

One question: READ_ONCE on a u64 is not atomic on 32-bit architectures (unlike atomic64_read which uses cmpxchg8b). DRM_AMDGPU has no formal CONFIG_64BIT dependency in Kconfig, though in practice nobody runs it on 32-bit. Is READ_ONCE acceptable here, or would you prefer readq() to keep the atomicity guarantee?

Quoted 2 lines

Pre-requisite/error checking first please. Make that a if (WARN_ON(!ring->use_doorbell)) return.

Done. Both functions now have the guard at the top:

if (WARN_ON(!ring->use_doorbell))
return 0; /* or return; for set_wptr */

Quoted 5 lines

And please don't use WARN_ON_ONCE() that is just to reduce the amount of warnings printed into the logs on real HW errors.

On functional coding errors like this one here it doesn't make sense and is often overlooked.

Understood — changed to WARN_ON.

Quoted 1 line

Same here the case to atomic64_t is extremely questionable.

Fixed in set_wptr as well, same READ_ONCE/WRITE_ONCE approach.

v2 incoming once I hear back on the readq vs READ_ONCE question.

Thanks,
John

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

Quoted 383 lines · Christian König
Quoted 28 lines · John B. Moore

Move the duplicated doorbell-based get_wptr/set_wptr functions from gfx_v9_0.c, gfx_v10_0.c, gfx_v11_0.c, and gfx_v12_0.c into common helpers amdgpu_gfx_get_wptr_compute() and amdgpu_gfx_set_wptr_compute() in amdgpu_gfx.c.

These functions are not HW generation dependent -- the doorbell path is identical across all four GFX versions:

get: atomic64_read(ring->wptr_cpu_addr)
set: atomic64_set(ring->wptr_cpu_addr) + WDOORBELL64()

The non-doorbell fallback is replaced with WARN_ON_ONCE instead of BUG() since doorbell is the only supported method on gfx9+ compute rings.

Not touched: gfx_v7_0, gfx_v8_0, gfx_v9_4_3 -- these have different wptr access patterns (MMIO registers or wb.wb[] offsets).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 39 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  3 ++
 drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c  | 33 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  | 34 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c  | 34 +++------------------
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c   | 39 +++----------------------
 6 files changed, 58 insertions(+), 124 deletions(-)

Review

Quoted 1 line
(quoted diff) +0 -0

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

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void

Quoted 4 lines

index 77578ecc6..9e9c5cb81 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +0 -0
@@ -2596,3 +2596,42 @@ void

amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +2 -0
 #endif
 }
 
+/**
+ * amdgpu_gfx_get_wptr_compute - common get_wptr for compute rings

using doorbells

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev) · line 2611

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +11 -0
+ * @ring: amdgpu_ring pointer
+ *
+ * Read the write pointer from the doorbell-mapped writeback address.
+ * This is HW-agnostic and shared across GFX generations that use
+ * doorbell-based compute queue management.
+ */
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring)
+{
+     /* XXX check if swapping is necessary on BE */
+     if (ring->use_doorbell)
+             return atomic64_read((atomic64_t *)ring->wptr_cpu_addr);

That should probably be readq() instead of this horrible and not portable cast to atomic64_t.

Alternatively we could just normally read the pointer with a memory barrier since this is just system memory.

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void · line 2613

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +2 -0
+
+     WARN_ON_ONCE(1);

Pre-requisite/error checking first please.

Make that a if (WARN_ON(!ring->use_doorbell)) return.

And please don't use WARN_ON_ONCE() that is just to reduce the amount of warnings printed into the logs on real HW errors.

On functional coding errors like this one here it doesn't make sense and is often overlooked.

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +5 -0
+     return 0;
+}
+
+/**
+ * amdgpu_gfx_set_wptr_compute - common set_wptr for compute rings

using doorbells

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)

Quoted 13 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +13 -0
+ * @ring: amdgpu_ring pointer
+ *
+ * Write the write pointer to the doorbell-mapped writeback address and
+ * ring the doorbell.  This is HW-agnostic and shared across GFX
+ * generations that use doorbell-based compute queue management.
+ */
+void amdgpu_gfx_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);

Same here the case to atomic64_t is extremely questionable.

Regards,
Christian.

Review

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +6 -0
+             WDOORBELL64(ring->doorbell_index, ring->wptr);
+     } else {
+             WARN_ON_ONCE(1);
+     }
+}
+
(quoted diff) +0 -0

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

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h u32 amdgpu_gfx_csb_preamble_start(u32 *buffer); · line 653

Quoted 5 lines

index 585cc8e81..27f6beafb 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +0 -0
@@ -653,6 +653,9 @@ u32 amdgpu_gfx_csb_preamble_start(u32 *buffer);
 u32 amdgpu_gfx_csb_data_parser(struct amdgpu_device *adev, u32 *buffer,

u32 count);

Review

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
 void amdgpu_gfx_csb_preamble_end(u32 *buffer, u32 count);
 
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring);
+void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring);
+
 void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device *adev);
 void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev);
 
(quoted diff) +0 -0

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

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c static u64 gfx_v10_0_ring_get_rptr_compute(struct

Quoted 4 lines

index 1893ceeeb..4c0272cba 100644

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +0 -0
@@ -8586,31 +8586,6 @@ static u64 gfx_v10_0_ring_get_rptr_compute(struct

amdgpu_ring *ring)

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c static const struct amdgpu_ring_funcs

Quoted 32 lines
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +0 -25
      return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v10_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;
-}
-
-static void gfx_v10_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
-{
-     struct amdgpu_device *adev = ring->adev;
-
-     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 gfx10 now */
-     }
-}
-
 static void gfx_v10_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
      struct amdgpu_device *adev = ring->adev;
@@ -9881,8 +9856,8 @@ static const struct amdgpu_ring_funcs

gfx_v10_0_ring_funcs_compute = {

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c static const struct amdgpu_ring_funcs

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v10_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v10_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v10_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              20 + /* gfx_v10_0_ring_emit_gds_switch */
              7 + /* gfx_v10_0_ring_emit_hdp_flush */
@@ -9921,8 +9896,8 @@ static const struct amdgpu_ring_funcs

gfx_v10_0_ring_funcs_kiq = {

Review

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v10_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v10_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v10_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              20 + /* gfx_v10_0_ring_emit_gds_switch */
              7 + /* gfx_v10_0_ring_emit_hdp_flush */
(quoted diff) +0 -0

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

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static u64 gfx_v11_0_ring_get_rptr_compute(struct

Quoted 4 lines

index 427975b5a..404604f2d 100644

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
@@ -5818,32 +5818,6 @@ static u64 gfx_v11_0_ring_get_rptr_compute(struct

amdgpu_ring *ring)

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static const struct amdgpu_ring_funcs

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -26
      return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v11_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;
-}
-
-static void gfx_v11_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 gfx11 now */
-     }
-}
-
 static void gfx_v11_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
      struct amdgpu_device *adev = ring->adev;
@@ -7266,8 +7240,8 @@ static const struct amdgpu_ring_funcs

gfx_v11_0_ring_funcs_compute = {

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static const struct amdgpu_ring_funcs

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v11_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v11_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v11_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              5 + /* update_spm_vmid */
              20 + /* gfx_v11_0_ring_emit_gds_switch */
@@ -7307,8 +7281,8 @@ static const struct amdgpu_ring_funcs

gfx_v11_0_ring_funcs_kiq = {

Review

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v11_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v11_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v11_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              20 + /* gfx_v11_0_ring_emit_gds_switch */
              7 + /* gfx_v11_0_ring_emit_hdp_flush */
(quoted diff) +0 -0

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

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static u64 gfx_v12_0_ring_get_rptr_compute(struct

Quoted 4 lines

index 79ea1af36..7ba436444 100644

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +0 -0
@@ -4363,32 +4363,6 @@ static u64 gfx_v12_0_ring_get_rptr_compute(struct

amdgpu_ring *ring)

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static const struct amdgpu_ring_funcs

Quoted 33 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +0 -26
      return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v12_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;
-}
-
-static void gfx_v12_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 gfx12 now */
-     }
-}
-
 static void gfx_v12_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
      struct amdgpu_device *adev = ring->adev;
@@ -5523,8 +5497,8 @@ static const struct amdgpu_ring_funcs

gfx_v12_0_ring_funcs_compute = {

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static const struct amdgpu_ring_funcs

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v12_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v12_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v12_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              7 + /* gfx_v12_0_ring_emit_hdp_flush */
              5 + /* hdp invalidate */
@@ -5561,8 +5535,8 @@ static const struct amdgpu_ring_funcs

gfx_v12_0_ring_funcs_kiq = {

Review

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v12_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v12_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v12_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              7 + /* gfx_v12_0_ring_emit_hdp_flush */
              5 + /*hdp invalidate */
(quoted diff) +0 -0

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

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 gfx_v9_0_ring_get_rptr_compute(struct

Quoted 4 lines

index 8249135d7..798f94bca 100644

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -0
@@ -5640,37 +5640,6 @@ static u64 gfx_v9_0_ring_get_rptr_compute(struct

amdgpu_ring *ring)

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)

Quoted 12 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -9
      return *ring->rptr_cpu_addr; /* gfx9 hardware is 32bit rptr */
 }
 
-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 {
-             WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr read on ring %s,

"

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -4
-                       "only doorbell method supported on gfx9\n",
-                       ring->name);
-             wptr = 0;
-     }

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -10
-     return wptr;
-}
-
-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);

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 gfx_v9_0_ring_get_rptr_compute(struct · line 5668

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -3
-             WDOORBELL64(ring->doorbell_index, ring->wptr);
-     } else {
-             WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr write on ring

%s, "

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -5
-                       "only doorbell method supported on gfx9\n",
-                       ring->name);
-     }
-}
-
 static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64

addr,

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static const struct amdgpu_ring_funcs

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -0
                                       u64 seq, unsigned int flags)
 {
@@ -7627,8 +7596,8 @@ static const struct amdgpu_ring_funcs

gfx_v9_0_ring_funcs_compute = {

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static const struct amdgpu_ring_funcs

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v9_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v9_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v9_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              20 + /* gfx_v9_0_ring_emit_gds_switch */
              7 + /* gfx_v9_0_ring_emit_hdp_flush */
@@ -7669,8 +7638,8 @@ static const struct amdgpu_ring_funcs

gfx_v9_0_ring_funcs_kiq = {

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v9_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v9_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v9_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              20 + /* gfx_v9_0_ring_emit_gds_switch */
              7 + /* gfx_v9_0_ring_emit_hdp_flush */
maintainer
#4 lore raw

Hi John,

Quoted 7 lines · John Moore

Hi Christian,

Thanks for the review. All points addressed below.

Quoted 3 lines

That should probably be readq() instead of this horrible and not portable cast to atomic64_t.

Quoted 8 lines
Quoted 2 lines

Alternatively we could just normally read the pointer with a memory barrier since this is just system memory.

I went with the second option — this is system memory (writeback via GTT), not MMIO, so readq() felt semantically wrong. The v2 uses:

  get:  wptr = READ_ONCE(*(u64 *)ring->wptr_cpu_addr); smp_rmb();

This needs to be rmb() and not smp_rmb(); smp_rmb() is only for CPU<->CPU synchronization but here we need CPU<->device synchronization.

And it needs to come *before* the read!

Quoted 3 lines

  set:  WRITE_ONCE(*(u64 *)ring->wptr_cpu_addr, ring->wptr); smp_wmb();

Same here, but this time least the barrier ordering is correct.

Quoted 10 lines

        WDOORBELL64(ring->doorbell_index, ring->wptr);

The alignment is safe — amdgpu_device_wb_get() returns offsets in multiples of 8 dwords (32 bytes), so the u64* cast always lands on a naturally-aligned address.

One question: READ_ONCE on a u64 is not atomic on 32-bit architectures (unlike atomic64_read which uses cmpxchg8b). DRM_AMDGPU has no formal CONFIG_64BIT dependency in Kconfig, though in practice nobody runs it on 32-bit.

We still have some people trying to use it on 32bit kernels. We should maybe consider to drop the 32bit support.

Quoted 2 lines

Is READ_ONCE acceptable here, or would you
prefer readq() to keep the atomicity guarantee?

Yeah good question I don't really know what to do here.

On the one hand you are right, writeq()/readq() are not correct because this isn't MMIO but system memory.

On the other hand I don't think Linux has an architecture independent way to guarantee that a write to system memory is done as an atomic 64bit write.

What we need to guarantee is that the device never sees an incomplete value because the write is done as two 32bit writes. That is probably the reason why we used the atomic64_t hack in the first place.

Regards,
Christian.

Quoted 385 lines
Quoted 3 lines

Pre-requisite/error checking first please. Make that a if (WARN_ON(!ring->use_doorbell)) return.

Done. Both functions now have the guard at the top:

  if (WARN_ON(!ring->use_doorbell))
      return 0;  /* or return; for set_wptr */

Quoted 5 lines

And please don't use WARN_ON_ONCE() that is just to reduce the amount of warnings printed into the logs on real HW errors.

On functional coding errors like this one here it doesn't make sense and is often overlooked.

Understood — changed to WARN_ON.

Quoted 1 line

Same here the case to atomic64_t is extremely questionable.

Fixed in set_wptr as well, same READ_ONCE/WRITE_ONCE approach.

v2 incoming once I hear back on the readq vs READ_ONCE question.

Thanks,
John

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

On Thu, Apr 30, 2026 at 2:19 AM Christian König <[email protected] <mailto:[email protected]>> wrote:

    On 4/29/26 22:20, John B. Moore wrote:
    > Move the duplicated doorbell-based get_wptr/set_wptr functions from
    > gfx_v9_0.c, gfx_v10_0.c, gfx_v11_0.c, and gfx_v12_0.c into common
    > helpers amdgpu_gfx_get_wptr_compute() and amdgpu_gfx_set_wptr_compute()
    > in amdgpu_gfx.c.
    >
    > These functions are not HW generation dependent -- the doorbell path is
    > identical across all four GFX versions:
    >
    >   get: atomic64_read(ring->wptr_cpu_addr)
    >   set: atomic64_set(ring->wptr_cpu_addr) + WDOORBELL64()
    >
    > The non-doorbell fallback is replaced with WARN_ON_ONCE instead of BUG()
    > since doorbell is the only supported method on gfx9+ compute rings.
    >
    > Not touched: gfx_v7_0, gfx_v8_0, gfx_v9_4_3 -- these have different
    > wptr access patterns (MMIO registers or wb.wb[] offsets).
    >
    > Suggested-by: Alex Deucher <[email protected] <mailto:[email protected]>>
    > Signed-off-by: John Moore <[email protected] <mailto:[email protected]>>
    > ---
    >  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 39 +++++++++++++++++++++++++
    >  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  3 ++
    >  drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c  | 33 +++------------------
    >  drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  | 34 +++------------------
    >  drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c  | 34 +++------------------
    >  drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c   | 39 +++----------------------
    >  6 files changed, 58 insertions(+), 124 deletions(-)
    >
    > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
    > index 77578ecc6..9e9c5cb81 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
    > @@ -2596,3 +2596,42 @@ void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)
    >  #endif
    >  }
    > 
    > +/**
    > + * amdgpu_gfx_get_wptr_compute - common get_wptr for compute rings using doorbells
    > + * @ring: amdgpu_ring pointer
    > + *
    > + * Read the write pointer from the doorbell-mapped writeback address.
    > + * This is HW-agnostic and shared across GFX generations that use
    > + * doorbell-based compute queue management.
    > + */
    > +u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring)
    > +{
    > +     /* XXX check if swapping is necessary on BE */
    > +     if (ring->use_doorbell)
    > +             return atomic64_read((atomic64_t *)ring->wptr_cpu_addr);

    That should probably be readq() instead of this horrible and not portable cast to atomic64_t.

    Alternatively we could just normally read the pointer with a memory barrier since this is just system memory.

    > +
    > +     WARN_ON_ONCE(1);

    Pre-requisite/error checking first please.

    Make that a if (WARN_ON(!ring->use_doorbell)) return.

    And please don't use WARN_ON_ONCE() that is just to reduce the amount of warnings printed into the logs on real HW errors.

    On functional coding errors like this one here it doesn't make sense and is often overlooked.

    > +     return 0;
    > +}
    > +
    > +/**
    > + * amdgpu_gfx_set_wptr_compute - common set_wptr for compute rings using doorbells
    > + * @ring: amdgpu_ring pointer
    > + *
    > + * Write the write pointer to the doorbell-mapped writeback address and
    > + * ring the doorbell.  This is HW-agnostic and shared across GFX
    > + * generations that use doorbell-based compute queue management.
    > + */
    > +void amdgpu_gfx_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);

    Same here the case to atomic64_t is extremely questionable.

    Regards,
    Christian.

    > +             WDOORBELL64(ring->doorbell_index, ring->wptr);
    > +     } else {
    > +             WARN_ON_ONCE(1);
    > +     }
    > +}
    > +
    > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
    > index 585cc8e81..27f6beafb 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
    > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
    > @@ -653,6 +653,9 @@ u32 amdgpu_gfx_csb_preamble_start(u32 *buffer);
    >  u32 amdgpu_gfx_csb_data_parser(struct amdgpu_device *adev, u32 *buffer, u32 count);
    >  void amdgpu_gfx_csb_preamble_end(u32 *buffer, u32 count);
    > 
    > +u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring);
    > +void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring);
    > +
    >  void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device *adev);
    >  void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev);
    > 
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
    > index 1893ceeeb..4c0272cba 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
    > @@ -8586,31 +8586,6 @@ static u64 gfx_v10_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *(uint32_t *)ring->rptr_cpu_addr;
    >  }
    > 
    > -static u64 gfx_v10_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;
    > -}
    > -
    > -static void gfx_v10_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
    > -{
    > -     struct amdgpu_device *adev = ring->adev;
    > -
    > -     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 gfx10 now */
    > -     }
    > -}
    > -
    >  static void gfx_v10_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
    >  {
    >       struct amdgpu_device *adev = ring->adev;
    > @@ -9881,8 +9856,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v10_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v10_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v10_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v10_0_ring_emit_gds_switch */
    >               7 + /* gfx_v10_0_ring_emit_hdp_flush */
    > @@ -9921,8 +9896,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v10_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v10_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v10_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v10_0_ring_emit_gds_switch */
    >               7 + /* gfx_v10_0_ring_emit_hdp_flush */
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
    > index 427975b5a..404604f2d 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
    > @@ -5818,32 +5818,6 @@ static u64 gfx_v11_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *(uint32_t *)ring->rptr_cpu_addr;
    >  }
    > 
    > -static u64 gfx_v11_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;
    > -}
    > -
    > -static void gfx_v11_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 gfx11 now */
    > -     }
    > -}
    > -
    >  static void gfx_v11_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
    >  {
    >       struct amdgpu_device *adev = ring->adev;
    > @@ -7266,8 +7240,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v11_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v11_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v11_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               5 + /* update_spm_vmid */
    >               20 + /* gfx_v11_0_ring_emit_gds_switch */
    > @@ -7307,8 +7281,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v11_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v11_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v11_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v11_0_ring_emit_gds_switch */
    >               7 + /* gfx_v11_0_ring_emit_hdp_flush */
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
    > index 79ea1af36..7ba436444 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
    > @@ -4363,32 +4363,6 @@ static u64 gfx_v12_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *(uint32_t *)ring->rptr_cpu_addr;
    >  }
    > 
    > -static u64 gfx_v12_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;
    > -}
    > -
    > -static void gfx_v12_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 gfx12 now */
    > -     }
    > -}
    > -
    >  static void gfx_v12_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
    >  {
    >       struct amdgpu_device *adev = ring->adev;
    > @@ -5523,8 +5497,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v12_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v12_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v12_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               7 + /* gfx_v12_0_ring_emit_hdp_flush */
    >               5 + /* hdp invalidate */
    > @@ -5561,8 +5535,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v12_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v12_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v12_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               7 + /* gfx_v12_0_ring_emit_hdp_flush */
    >               5 + /*hdp invalidate */
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
    > index 8249135d7..798f94bca 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
    > @@ -5640,37 +5640,6 @@ static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *ring->rptr_cpu_addr; /* gfx9 hardware is 32bit rptr */
    >  }
    > 
    > -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 {
    > -             WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr read on ring %s, "
    > -                       "only doorbell method supported on gfx9\n",
    > -                       ring->name);
    > -             wptr = 0;
    > -     }
    > -     return wptr;
    > -}
    > -
    > -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 {
    > -             WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr write on ring %s, "
    > -                       "only doorbell method supported on gfx9\n",
    > -                       ring->name);
    > -     }
    > -}
    > -
    >  static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
    >                                        u64 seq, unsigned int flags)
    >  {
    > @@ -7627,8 +7596,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v9_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v9_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v9_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v9_0_ring_emit_gds_switch */
    >               7 + /* gfx_v9_0_ring_emit_hdp_flush */
    > @@ -7669,8 +7638,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v9_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v9_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v9_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v9_0_ring_emit_gds_switch */
    >               7 + /* gfx_v9_0_ring_emit_hdp_flush */
#5 lore raw
Quoted 36 lines · Christian König

Hi John,

Quoted 7 lines · John Moore

Hi Christian,

Thanks for the review. All points addressed below.

Quoted 3 lines

That should probably be readq() instead of this horrible and not portable cast to atomic64_t.

Quoted 8 lines
Quoted 2 lines

Alternatively we could just normally read the pointer with a memory barrier since this is just system memory.

I went with the second option — this is system memory (writeback via GTT), not MMIO, so readq() felt semantically wrong. The v2 uses:

  get:  wptr = READ_ONCE(*(u64 *)ring->wptr_cpu_addr);
        smp_rmb();

This needs to be rmb() and not smp_rmb(); smp_rmb() is only for CPU<->CPU synchronization but here we need CPU<->device synchronization.

And it needs to come *before* the read!

Quoted 3 lines
  set:  WRITE_ONCE(*(u64 *)ring->wptr_cpu_addr, ring->wptr);
        smp_wmb();

Same here, but this time least the barrier ordering is correct.

Quoted 6 lines
        WDOORBELL64(ring->doorbell_index, ring->wptr);

The alignment is safe — amdgpu_device_wb_get() returns offsets in multiples of 8 dwords (32 bytes), so the u64* cast always lands on a naturally-aligned address.

Quoted 6 lines
Quoted 4 lines

One question: READ_ONCE on a u64 is not atomic on 32-bit architectures (unlike atomic64_read which uses cmpxchg8b). DRM_AMDGPU has no formal CONFIG_64BIT dependency in Kconfig, though in practice nobody runs it on 32-bit.

We still have some people trying to use it on 32bit kernels. We should maybe consider to drop the 32bit support.

32 bit support is the reason we used the atomic stuff in the first place.

Alex

Quoted 401 lines
Quoted 3 lines

Is READ_ONCE acceptable here, or would you
prefer readq() to keep the atomicity guarantee?

Yeah good question I don't really know what to do here.

On the one hand you are right, writeq()/readq() are not correct because this isn't MMIO but system memory.

On the other hand I don't think Linux has an architecture independent way to guarantee that a write to system memory is done as an atomic 64bit write.

What we need to guarantee is that the device never sees an incomplete value because the write is done as two 32bit writes. That is probably the reason why we used the atomic64_t hack in the first place.

Regards,
Christian.

Quoted 385 lines
Quoted 3 lines

Pre-requisite/error checking first please. Make that a if (WARN_ON(!ring->use_doorbell)) return.

Done. Both functions now have the guard at the top:

if (WARN_ON(!ring->use_doorbell))
return 0; /* or return; for set_wptr */

Quoted 5 lines

And please don't use WARN_ON_ONCE() that is just to reduce the amount of warnings printed into the logs on real HW errors.

On functional coding errors like this one here it doesn't make sense and is often overlooked.

Understood — changed to WARN_ON.

Quoted 1 line

Same here the case to atomic64_t is extremely questionable.

Fixed in set_wptr as well, same READ_ONCE/WRITE_ONCE approach.

v2 incoming once I hear back on the readq vs READ_ONCE question.

Thanks,
John

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

On Thu, Apr 30, 2026 at 2:19 AM Christian König <[email protected] <mailto:[email protected]>> wrote:

    On 4/29/26 22:20, John B. Moore wrote:
    > Move the duplicated doorbell-based get_wptr/set_wptr functions from
    > gfx_v9_0.c, gfx_v10_0.c, gfx_v11_0.c, and gfx_v12_0.c into common
    > helpers amdgpu_gfx_get_wptr_compute() and amdgpu_gfx_set_wptr_compute()
    > in amdgpu_gfx.c.
    >
    > These functions are not HW generation dependent -- the doorbell path is
    > identical across all four GFX versions:
    >
    >   get: atomic64_read(ring->wptr_cpu_addr)
    >   set: atomic64_set(ring->wptr_cpu_addr) + WDOORBELL64()
    >
    > The non-doorbell fallback is replaced with WARN_ON_ONCE instead of BUG()
    > since doorbell is the only supported method on gfx9+ compute rings.
    >
    > Not touched: gfx_v7_0, gfx_v8_0, gfx_v9_4_3 -- these have different
    > wptr access patterns (MMIO registers or wb.wb[] offsets).
    >
    > Suggested-by: Alex Deucher <[email protected] <mailto:[email protected]>>
    > Signed-off-by: John Moore <[email protected] <mailto:[email protected]>>
    > ---
    >  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 39 +++++++++++++++++++++++++
    >  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  3 ++
    >  drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c  | 33 +++------------------
    >  drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  | 34 +++------------------
    >  drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c  | 34 +++------------------
    >  drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c   | 39 +++----------------------
    >  6 files changed, 58 insertions(+), 124 deletions(-)
    >
    > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
    > index 77578ecc6..9e9c5cb81 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
    > @@ -2596,3 +2596,42 @@ void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)
    >  #endif
    >  }
    >
    > +/**
    > + * amdgpu_gfx_get_wptr_compute - common get_wptr for compute rings using doorbells
    > + * @ring: amdgpu_ring pointer
    > + *
    > + * Read the write pointer from the doorbell-mapped writeback address.
    > + * This is HW-agnostic and shared across GFX generations that use
    > + * doorbell-based compute queue management.
    > + */
    > +u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring)
    > +{
    > +     /* XXX check if swapping is necessary on BE */
    > +     if (ring->use_doorbell)
    > +             return atomic64_read((atomic64_t *)ring->wptr_cpu_addr);

    That should probably be readq() instead of this horrible and not portable cast to atomic64_t.

    Alternatively we could just normally read the pointer with a memory barrier since this is just system memory.

    > +
    > +     WARN_ON_ONCE(1);

    Pre-requisite/error checking first please.

    Make that a if (WARN_ON(!ring->use_doorbell)) return.

    And please don't use WARN_ON_ONCE() that is just to reduce the amount of warnings printed into the logs on real HW errors.

    On functional coding errors like this one here it doesn't make sense and is often overlooked.

    > +     return 0;
    > +}
    > +
    > +/**
    > + * amdgpu_gfx_set_wptr_compute - common set_wptr for compute rings using doorbells
    > + * @ring: amdgpu_ring pointer
    > + *
    > + * Write the write pointer to the doorbell-mapped writeback address and
    > + * ring the doorbell.  This is HW-agnostic and shared across GFX
    > + * generations that use doorbell-based compute queue management.
    > + */
    > +void amdgpu_gfx_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);

    Same here the case to atomic64_t is extremely questionable.

    Regards,
    Christian.

    > +             WDOORBELL64(ring->doorbell_index, ring->wptr);
    > +     } else {
    > +             WARN_ON_ONCE(1);
    > +     }
    > +}
    > +
    > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
    > index 585cc8e81..27f6beafb 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
    > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
    > @@ -653,6 +653,9 @@ u32 amdgpu_gfx_csb_preamble_start(u32 *buffer);
    >  u32 amdgpu_gfx_csb_data_parser(struct amdgpu_device *adev, u32 *buffer, u32 count);
    >  void amdgpu_gfx_csb_preamble_end(u32 *buffer, u32 count);
    >
    > +u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring);
    > +void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring);
    > +
    >  void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device *adev);
    >  void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev);
    >
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
    > index 1893ceeeb..4c0272cba 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
    > @@ -8586,31 +8586,6 @@ static u64 gfx_v10_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *(uint32_t *)ring->rptr_cpu_addr;
    >  }
    >
    > -static u64 gfx_v10_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;
    > -}
    > -
    > -static void gfx_v10_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
    > -{
    > -     struct amdgpu_device *adev = ring->adev;
    > -
    > -     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 gfx10 now */
    > -     }
    > -}
    > -
    >  static void gfx_v10_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
    >  {
    >       struct amdgpu_device *adev = ring->adev;
    > @@ -9881,8 +9856,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v10_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v10_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v10_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v10_0_ring_emit_gds_switch */
    >               7 + /* gfx_v10_0_ring_emit_hdp_flush */
    > @@ -9921,8 +9896,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v10_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v10_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v10_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v10_0_ring_emit_gds_switch */
    >               7 + /* gfx_v10_0_ring_emit_hdp_flush */
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
    > index 427975b5a..404604f2d 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
    > @@ -5818,32 +5818,6 @@ static u64 gfx_v11_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *(uint32_t *)ring->rptr_cpu_addr;
    >  }
    >
    > -static u64 gfx_v11_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;
    > -}
    > -
    > -static void gfx_v11_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 gfx11 now */
    > -     }
    > -}
    > -
    >  static void gfx_v11_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
    >  {
    >       struct amdgpu_device *adev = ring->adev;
    > @@ -7266,8 +7240,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v11_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v11_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v11_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               5 + /* update_spm_vmid */
    >               20 + /* gfx_v11_0_ring_emit_gds_switch */
    > @@ -7307,8 +7281,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v11_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v11_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v11_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v11_0_ring_emit_gds_switch */
    >               7 + /* gfx_v11_0_ring_emit_hdp_flush */
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
    > index 79ea1af36..7ba436444 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
    > @@ -4363,32 +4363,6 @@ static u64 gfx_v12_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *(uint32_t *)ring->rptr_cpu_addr;
    >  }
    >
    > -static u64 gfx_v12_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;
    > -}
    > -
    > -static void gfx_v12_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 gfx12 now */
    > -     }
    > -}
    > -
    >  static void gfx_v12_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
    >  {
    >       struct amdgpu_device *adev = ring->adev;
    > @@ -5523,8 +5497,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v12_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v12_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v12_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               7 + /* gfx_v12_0_ring_emit_hdp_flush */
    >               5 + /* hdp invalidate */
    > @@ -5561,8 +5535,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v12_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v12_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v12_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               7 + /* gfx_v12_0_ring_emit_hdp_flush */
    >               5 + /*hdp invalidate */
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
    > index 8249135d7..798f94bca 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
    > @@ -5640,37 +5640,6 @@ static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *ring->rptr_cpu_addr; /* gfx9 hardware is 32bit rptr */
    >  }
    >
    > -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 {
    > -             WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr read on ring %s, "
    > -                       "only doorbell method supported on gfx9\n",
    > -                       ring->name);
    > -             wptr = 0;
    > -     }
    > -     return wptr;
    > -}
    > -
    > -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 {
    > -             WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr write on ring %s, "
    > -                       "only doorbell method supported on gfx9\n",
    > -                       ring->name);
    > -     }
    > -}
    > -
    >  static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
    >                                        u64 seq, unsigned int flags)
    >  {
    > @@ -7627,8 +7596,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v9_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v9_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v9_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v9_0_ring_emit_gds_switch */
    >               7 + /* gfx_v9_0_ring_emit_hdp_flush */
    > @@ -7669,8 +7638,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v9_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v9_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v9_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v9_0_ring_emit_gds_switch */
    >               7 + /* gfx_v9_0_ring_emit_hdp_flush */
maintainer
#6 lore raw
Quoted 38 lines · Alex Deucher
Quoted 36 lines · Christian König

Hi John,

Quoted 7 lines · John Moore

Hi Christian,

Thanks for the review. All points addressed below.

Quoted 3 lines

That should probably be readq() instead of this horrible and not portable cast to atomic64_t.

Quoted 8 lines
Quoted 2 lines

Alternatively we could just normally read the pointer with a memory barrier since this is just system memory.

I went with the second option — this is system memory (writeback via GTT), not MMIO, so readq() felt semantically wrong. The v2 uses:

  get:  wptr = READ_ONCE(*(u64 *)ring->wptr_cpu_addr);
        smp_rmb();

This needs to be rmb() and not smp_rmb(); smp_rmb() is only for CPU<->CPU synchronization but here we need CPU<->device synchronization.

And it needs to come *before* the read!

Quoted 3 lines
  set:  WRITE_ONCE(*(u64 *)ring->wptr_cpu_addr, ring->wptr);
        smp_wmb();

Same here, but this time least the barrier ordering is correct.

Quoted 6 lines
        WDOORBELL64(ring->doorbell_index, ring->wptr);

The alignment is safe — amdgpu_device_wb_get() returns offsets in multiples of 8 dwords (32 bytes), so the u64* cast always lands on a naturally-aligned address.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

One question: READ_ONCE on a u64 is not atomic on 32-bit architectures (unlike atomic64_read which uses cmpxchg8b). DRM_AMDGPU has no formal CONFIG_64BIT dependency in Kconfig, though in practice nobody runs it on 32-bit.

We still have some people trying to use it on 32bit kernels. We should maybe consider to drop the 32bit support.

32 bit support is the reason we used the atomic stuff in the first place.

Yeah the problem is that is just and extremely ugly hack.

IIRC there are architecture who can't do 64bit writes at all, on those systems an atomic_64 is implemented by using a lock.

On the other hand I don't think anybody would be able to use HW AMDGPU supports on a SPARC, Alpha or +20 year old MIPS system because of the lack of PCIe bus.

My educated guess is that the correct answer is to have a config dependency to prevent AMDGPU from even compiling on such architectures and then using a WRITE_ONCE()/READ_ONCE() with appropriate memory barriers.

But of hand I don't know how that stuff is abstracted on the architecture side and which config option to depend on.

Regards,
Christian.

Quoted 404 lines

Alex

Quoted 401 lines
Quoted 3 lines

Is READ_ONCE acceptable here, or would you
prefer readq() to keep the atomicity guarantee?

Yeah good question I don't really know what to do here.

On the one hand you are right, writeq()/readq() are not correct because this isn't MMIO but system memory.

On the other hand I don't think Linux has an architecture independent way to guarantee that a write to system memory is done as an atomic 64bit write.

What we need to guarantee is that the device never sees an incomplete value because the write is done as two 32bit writes. That is probably the reason why we used the atomic64_t hack in the first place.

Regards,
Christian.

Quoted 385 lines
Quoted 3 lines

Pre-requisite/error checking first please. Make that a if (WARN_ON(!ring->use_doorbell)) return.

Done. Both functions now have the guard at the top:

if (WARN_ON(!ring->use_doorbell))
return 0; /* or return; for set_wptr */

Quoted 5 lines

And please don't use WARN_ON_ONCE() that is just to reduce the amount of warnings printed into the logs on real HW errors.

On functional coding errors like this one here it doesn't make sense and is often overlooked.

Understood — changed to WARN_ON.

Quoted 1 line

Same here the case to atomic64_t is extremely questionable.

Fixed in set_wptr as well, same READ_ONCE/WRITE_ONCE approach.

v2 incoming once I hear back on the readq vs READ_ONCE question.

Thanks,
John

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

On Thu, Apr 30, 2026 at 2:19 AM Christian König <[email protected] <mailto:[email protected]>> wrote:

    On 4/29/26 22:20, John B. Moore wrote:
    > Move the duplicated doorbell-based get_wptr/set_wptr functions from
    > gfx_v9_0.c, gfx_v10_0.c, gfx_v11_0.c, and gfx_v12_0.c into common
    > helpers amdgpu_gfx_get_wptr_compute() and amdgpu_gfx_set_wptr_compute()
    > in amdgpu_gfx.c.
    >
    > These functions are not HW generation dependent -- the doorbell path is
    > identical across all four GFX versions:
    >
    >   get: atomic64_read(ring->wptr_cpu_addr)
    >   set: atomic64_set(ring->wptr_cpu_addr) + WDOORBELL64()
    >
    > The non-doorbell fallback is replaced with WARN_ON_ONCE instead of BUG()
    > since doorbell is the only supported method on gfx9+ compute rings.
    >
    > Not touched: gfx_v7_0, gfx_v8_0, gfx_v9_4_3 -- these have different
    > wptr access patterns (MMIO registers or wb.wb[] offsets).
    >
    > Suggested-by: Alex Deucher <[email protected] <mailto:[email protected]>>
    > Signed-off-by: John Moore <[email protected] <mailto:[email protected]>>
    > ---
    >  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 39 +++++++++++++++++++++++++
    >  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  3 ++
    >  drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c  | 33 +++------------------
    >  drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  | 34 +++------------------
    >  drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c  | 34 +++------------------
    >  drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c   | 39 +++----------------------
    >  6 files changed, 58 insertions(+), 124 deletions(-)
    >
    > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
    > index 77578ecc6..9e9c5cb81 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c
    > @@ -2596,3 +2596,42 @@ void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)
    >  #endif
    >  }
    >
    > +/**
    > + * amdgpu_gfx_get_wptr_compute - common get_wptr for compute rings using doorbells
    > + * @ring: amdgpu_ring pointer
    > + *
    > + * Read the write pointer from the doorbell-mapped writeback address.
    > + * This is HW-agnostic and shared across GFX generations that use
    > + * doorbell-based compute queue management.
    > + */
    > +u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring)
    > +{
    > +     /* XXX check if swapping is necessary on BE */
    > +     if (ring->use_doorbell)
    > +             return atomic64_read((atomic64_t *)ring->wptr_cpu_addr);

    That should probably be readq() instead of this horrible and not portable cast to atomic64_t.

    Alternatively we could just normally read the pointer with a memory barrier since this is just system memory.

    > +
    > +     WARN_ON_ONCE(1);

    Pre-requisite/error checking first please.

    Make that a if (WARN_ON(!ring->use_doorbell)) return.

    And please don't use WARN_ON_ONCE() that is just to reduce the amount of warnings printed into the logs on real HW errors.

    On functional coding errors like this one here it doesn't make sense and is often overlooked.

    > +     return 0;
    > +}
    > +
    > +/**
    > + * amdgpu_gfx_set_wptr_compute - common set_wptr for compute rings using doorbells
    > + * @ring: amdgpu_ring pointer
    > + *
    > + * Write the write pointer to the doorbell-mapped writeback address and
    > + * ring the doorbell.  This is HW-agnostic and shared across GFX
    > + * generations that use doorbell-based compute queue management.
    > + */
    > +void amdgpu_gfx_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);

    Same here the case to atomic64_t is extremely questionable.

    Regards,
    Christian.

    > +             WDOORBELL64(ring->doorbell_index, ring->wptr);
    > +     } else {
    > +             WARN_ON_ONCE(1);
    > +     }
    > +}
    > +
    > diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
    > index 585cc8e81..27f6beafb 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
    > +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h
    > @@ -653,6 +653,9 @@ u32 amdgpu_gfx_csb_preamble_start(u32 *buffer);
    >  u32 amdgpu_gfx_csb_data_parser(struct amdgpu_device *adev, u32 *buffer, u32 count);
    >  void amdgpu_gfx_csb_preamble_end(u32 *buffer, u32 count);
    >
    > +u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring);
    > +void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring);
    > +
    >  void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device *adev);
    >  void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev);
    >
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
    > index 1893ceeeb..4c0272cba 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c
    > @@ -8586,31 +8586,6 @@ static u64 gfx_v10_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *(uint32_t *)ring->rptr_cpu_addr;
    >  }
    >
    > -static u64 gfx_v10_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;
    > -}
    > -
    > -static void gfx_v10_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
    > -{
    > -     struct amdgpu_device *adev = ring->adev;
    > -
    > -     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 gfx10 now */
    > -     }
    > -}
    > -
    >  static void gfx_v10_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
    >  {
    >       struct amdgpu_device *adev = ring->adev;
    > @@ -9881,8 +9856,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v10_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v10_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v10_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v10_0_ring_emit_gds_switch */
    >               7 + /* gfx_v10_0_ring_emit_hdp_flush */
    > @@ -9921,8 +9896,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v10_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v10_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v10_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v10_0_ring_emit_gds_switch */
    >               7 + /* gfx_v10_0_ring_emit_hdp_flush */
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
    > index 427975b5a..404604f2d 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c
    > @@ -5818,32 +5818,6 @@ static u64 gfx_v11_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *(uint32_t *)ring->rptr_cpu_addr;
    >  }
    >
    > -static u64 gfx_v11_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;
    > -}
    > -
    > -static void gfx_v11_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 gfx11 now */
    > -     }
    > -}
    > -
    >  static void gfx_v11_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
    >  {
    >       struct amdgpu_device *adev = ring->adev;
    > @@ -7266,8 +7240,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v11_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v11_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v11_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               5 + /* update_spm_vmid */
    >               20 + /* gfx_v11_0_ring_emit_gds_switch */
    > @@ -7307,8 +7281,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v11_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v11_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v11_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v11_0_ring_emit_gds_switch */
    >               7 + /* gfx_v11_0_ring_emit_hdp_flush */
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
    > index 79ea1af36..7ba436444 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c
    > @@ -4363,32 +4363,6 @@ static u64 gfx_v12_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *(uint32_t *)ring->rptr_cpu_addr;
    >  }
    >
    > -static u64 gfx_v12_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;
    > -}
    > -
    > -static void gfx_v12_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 gfx12 now */
    > -     }
    > -}
    > -
    >  static void gfx_v12_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
    >  {
    >       struct amdgpu_device *adev = ring->adev;
    > @@ -5523,8 +5497,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v12_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v12_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v12_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               7 + /* gfx_v12_0_ring_emit_hdp_flush */
    >               5 + /* hdp invalidate */
    > @@ -5561,8 +5535,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v12_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v12_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v12_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               7 + /* gfx_v12_0_ring_emit_hdp_flush */
    >               5 + /*hdp invalidate */
    > diff --git a/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c b/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
    > index 8249135d7..798f94bca 100644
    > --- a/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
    > +++ b/drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c
    > @@ -5640,37 +5640,6 @@ static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
    >       return *ring->rptr_cpu_addr; /* gfx9 hardware is 32bit rptr */
    >  }
    >
    > -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 {
    > -             WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr read on ring %s, "
    > -                       "only doorbell method supported on gfx9\n",
    > -                       ring->name);
    > -             wptr = 0;
    > -     }
    > -     return wptr;
    > -}
    > -
    > -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 {
    > -             WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr write on ring %s, "
    > -                       "only doorbell method supported on gfx9\n",
    > -                       ring->name);
    > -     }
    > -}
    > -
    >  static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
    >                                        u64 seq, unsigned int flags)
    >  {
    > @@ -7627,8 +7596,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_compute = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v9_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v9_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v9_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v9_0_ring_emit_gds_switch */
    >               7 + /* gfx_v9_0_ring_emit_hdp_flush */
    > @@ -7669,8 +7638,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_kiq = {
    >       .nop = PACKET3(PACKET3_NOP, 0x3FFF),
    >       .support_64bit_ptrs = true,
    >       .get_rptr = gfx_v9_0_ring_get_rptr_compute,
    > -     .get_wptr = gfx_v9_0_ring_get_wptr_compute,
    > -     .set_wptr = gfx_v9_0_ring_set_wptr_compute,
    > +     .get_wptr = amdgpu_gfx_get_wptr_compute,
    > +     .set_wptr = amdgpu_gfx_set_wptr_compute,
    >       .emit_frame_size =
    >               20 + /* gfx_v9_0_ring_emit_gds_switch */
    >               7 + /* gfx_v9_0_ring_emit_hdp_flush */
author
#7 lore raw

Hi Christian,

I completely understand the issue and that you are stuck due to the hardware architectures.
Thanks for the corrections on the barriers — both fixed:

  get:  rmb();
        wptr = <read>(ring->wptr_cpu_addr);

  set:  <write>(ring->wptr_cpu_addr, ring->wptr);
        wmb();
        WDOORBELL64(ring->doorbell_index, ring->wptr);

rmb()/wmb() for CPU<->device, and the read barrier now comes before the read.

On the 32-bit atomicity question — understood, that's exactly why the atomic64_t hack exists. Since there's no clean arch-independent way to do atomic 64-bit writes to system memory, I've prepared both options as complete patches against amd-staging-drm-next so you can pick whichever you prefer:

Option A (attached): Keep atomic64_set()/atomic64_read() with the cast hidden behind a static inline helper (wptr_as_atomic). Preserves 32-bit atomicity. Not pretty, but honest.

Option B (attached): Use writeq()/readq() instead. Atomic on all architectures, but semantically these are MMIO accessors being used on system memory. Documented in comments.

Both patches compile clean, address all your review feedback (WARN_ON at top, rmb/wmb, no WARN_ON_ONCE), and are identical except for the read/write mechanism.

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

Quoted 540 lines · Christian König
Quoted 16 lines · Alex Deucher
Quoted 14 lines · Christian König

Hi John,

Quoted 10 lines · John Moore

Hi Christian,

Thanks for the review. All points addressed below.

Quoted 5 lines

That should probably be readq() instead of this horrible and not portable cast to atomic64_t.

Alternatively we could just normally read the pointer with a memory barrier since this is just system memory.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

I went with the second option — this is system memory (writeback via GTT), not MMIO, so readq() felt semantically wrong. The v2 uses:

  get:  wptr = READ_ONCE(*(u64 *)ring->wptr_cpu_addr);
        smp_rmb();

This needs to be rmb() and not smp_rmb(); smp_rmb() is only for

CPU<->CPU synchronization but here we need CPU<->device synchronization.

Quoted 15 lines
Quoted 15 lines

And it needs to come *before* the read!

Quoted 3 lines
  set:  WRITE_ONCE(*(u64 *)ring->wptr_cpu_addr, ring->wptr);
        smp_wmb();

Same here, but this time least the barrier ordering is correct.

Quoted 6 lines
        WDOORBELL64(ring->doorbell_index, ring->wptr);

The alignment is safe — amdgpu_device_wb_get() returns offsets in multiples of 8 dwords (32 bytes), so the u64* cast always lands on a naturally-aligned address.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

One question: READ_ONCE on a u64 is not atomic on 32-bit architectures (unlike atomic64_read which uses cmpxchg8b). DRM_AMDGPU has no formal CONFIG_64BIT dependency in Kconfig, though in practice nobody runs it on 32-bit.

We still have some people trying to use it on 32bit kernels. We should

maybe consider to drop the 32bit support.

Quoted 2 lines

32 bit support is the reason we used the atomic stuff in the first place.

Yeah the problem is that is just and extremely ugly hack.

IIRC there are architecture who can't do 64bit writes at all, on those systems an atomic_64 is implemented by using a lock.

On the other hand I don't think anybody would be able to use HW AMDGPU supports on a SPARC, Alpha or +20 year old MIPS system because of the lack of PCIe bus.

My educated guess is that the correct answer is to have a config dependency to prevent AMDGPU from even compiling on such architectures and then using a WRITE_ONCE()/READ_ONCE() with appropriate memory barriers.

But of hand I don't know how that stuff is abstracted on the architecture side and which config option to depend on.

Regards,
Christian.

Quoted 10 lines

Alex

Quoted 7 lines
Quoted 3 lines

Is READ_ONCE acceptable here, or would you
prefer readq() to keep the atomicity guarantee?

Yeah good question I don't really know what to do here.

On the one hand you are right, writeq()/readq() are not correct because

this isn't MMIO but system memory.

Quoted 2 lines
Quoted 2 lines

On the other hand I don't think Linux has an architecture independent

way to guarantee that a write to system memory is done as an atomic 64bit write.

Quoted 2 lines
Quoted 2 lines

What we need to guarantee is that the device never sees an incomplete

value because the write is done as two 32bit writes. That is probably the reason why we used the atomic64_t hack in the first place.

Quoted 23 lines
Quoted 23 lines

Regards,
Christian.

Quoted 19 lines
Quoted 3 lines

Pre-requisite/error checking first please. Make that a if (WARN_ON(!ring->use_doorbell)) return.

Done. Both functions now have the guard at the top:

if (WARN_ON(!ring->use_doorbell))
return 0; /* or return; for set_wptr */

Quoted 5 lines

And please don't use WARN_ON_ONCE() that is just to reduce the amount of warnings printed into the logs on real HW errors.

On functional coding errors like this one here it doesn't make sense and is often overlooked.

Understood — changed to WARN_ON.

Quoted 1 line

Same here the case to atomic64_t is extremely questionable.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Fixed in set_wptr as well, same READ_ONCE/WRITE_ONCE approach.

v2 incoming once I hear back on the readq vs READ_ONCE question.

Thanks,
John

"I will not be pushed, filed, stamped, indexed, briefed, debriefed, or

numbered."

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

~ The Prisoner

On Thu, Apr 30, 2026 at 2:19 AM Christian König <

Quoted 3 lines · [email protected] <mailto:[email protected]>> wrote:
Quoted 3 lines
Quoted 3 lines · John B. Moore
Quoted 1 line

Move the duplicated doorbell-based get_wptr/set_wptr functions

from

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

gfx_v9_0.c, gfx_v10_0.c, gfx_v11_0.c, and gfx_v12_0.c into common helpers amdgpu_gfx_get_wptr_compute() and

amdgpu_gfx_set_wptr_compute()

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

in amdgpu_gfx.c.

These functions are not HW generation dependent -- the doorbell

path is

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

identical across all four GFX versions:

get: atomic64_read(ring->wptr_cpu_addr)
set: atomic64_set(ring->wptr_cpu_addr) + WDOORBELL64()

The non-doorbell fallback is replaced with WARN_ON_ONCE instead

of BUG()

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

since doorbell is the only supported method on gfx9+ compute

rings.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Not touched: gfx_v7_0, gfx_v8_0, gfx_v9_4_3 -- these have

different

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

wptr access patterns (MMIO registers or wb.wb[] offsets).

[email protected]>>

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

[email protected]>>

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 39

+++++++++++++++++++++++++

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  3 ++
 drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c  | 33

+++------------------

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  | 34

+++------------------

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c  | 34

+++------------------

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c   | 39

+++----------------------

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Review

Quoted 3 lines
 6 files changed, 58 insertions(+), 124 deletions(-)
(quoted diff) +0 -0

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

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void

Quoted 4 lines

index 77578ecc6..9e9c5cb81 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +0 -0
@@ -2596,3 +2596,42 @@ void

amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void · line 2600

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +2 -0
 #endif
 }
 
+/**
+ * amdgpu_gfx_get_wptr_compute - common get_wptr for compute

rings using doorbells

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void · line 2603

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -0
+ * @ring: amdgpu_ring pointer
+ *
+ * Read the write pointer from the doorbell-mapped writeback

address.

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void · line 2604

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +1 -0
+ * This is HW-agnostic and shared across GFX generations that

use

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void · line 2611

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +7 -0
+ * doorbell-based compute queue management.
+ */
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring)
+{
+     /* XXX check if swapping is necessary on BE */
+     if (ring->use_doorbell)
+             return atomic64_read((atomic64_t

*)ring->wptr_cpu_addr);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

That should probably be readq() instead of this horrible and not

portable cast to atomic64_t.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Alternatively we could just normally read the pointer with a

memory barrier since this is just system memory.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void · line 2613

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +2 -0
+
+     WARN_ON_ONCE(1);

Pre-requisite/error checking first please.

Make that a if (WARN_ON(!ring->use_doorbell)) return.

And please don't use WARN_ON_ONCE() that is just to reduce the

amount of warnings printed into the logs on real HW errors.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

On functional coding errors like this one here it doesn't make

sense and is often overlooked.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void · line 2618

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +5 -0
+     return 0;
+}
+
+/**
+ * amdgpu_gfx_set_wptr_compute - common set_wptr for compute

rings using doorbells

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void · line 2621

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -0
+ * @ring: amdgpu_ring pointer
+ *
+ * Write the write pointer to the doorbell-mapped writeback

address and

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void · line 2631

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +10 -0
+ * ring the doorbell.  This is HW-agnostic and shared across GFX
+ * generations that use doorbell-based compute queue management.
+ */
+void amdgpu_gfx_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);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Same here the case to atomic64_t is extremely questionable.

Regards,
Christian.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Review

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +6 -0
+             WDOORBELL64(ring->doorbell_index, ring->wptr);
+     } else {
+             WARN_ON_ONCE(1);
+     }
+}
+
(quoted diff) +0 -0

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

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h u32 amdgpu_gfx_csb_preamble_start(u32

Quoted 4 lines

index 585cc8e81..27f6beafb 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +0 -0
@@ -653,6 +653,9 @@ u32 amdgpu_gfx_csb_preamble_start(u32

*buffer);

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h u32 amdgpu_gfx_csb_preamble_start(u32 · line 653

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +0 -0
 u32 amdgpu_gfx_csb_data_parser(struct amdgpu_device *adev, u32

*buffer, u32 count);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h u32 amdgpu_gfx_csb_preamble_start(u32 · line 659

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
 void amdgpu_gfx_csb_preamble_end(u32 *buffer, u32 count);
 
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring);
+void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring);
+
 void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device

*adev);

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h u32 amdgpu_gfx_csb_preamble_start(u32 · line 660

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +0 -0
 void amdgpu_debugfs_compute_sched_mask_init(struct

amdgpu_device *adev);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +0 -0

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

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c static u64

Quoted 4 lines

index 1893ceeeb..4c0272cba 100644

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +0 -0
@@ -8586,31 +8586,6 @@ static u64

gfx_v10_0_ring_get_rptr_compute(struct amdgpu_ring *ring)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c static u64 · line 8589

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +0 -1
      return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v10_0_ring_get_wptr_compute(struct amdgpu_ring

*ring)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c static u64 · line 8595

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +0 -6
-{
-     u64 wptr;
-
-     /* XXX check if swapping is necessary on BE */
-     if (ring->use_doorbell)
-             wptr = atomic64_read((atomic64_t

*)ring->wptr_cpu_addr);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c static u64 · line 8601

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +0 -6
-     else
-             BUG();
-     return wptr;
-}
-
-static void gfx_v10_0_ring_set_wptr_compute(struct amdgpu_ring

*ring)

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c static u64 · line 8610

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +0 -9
-{
-     struct amdgpu_device *adev = ring->adev;
-
-     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 gfx10

now */

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c static u64 · line 8589

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +0 -3
-     }
-}
-
 static void gfx_v10_0_ring_emit_hdp_flush(struct amdgpu_ring

*ring)

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c static const struct amdgpu_ring_funcs

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +0 -0
 {
      struct amdgpu_device *adev = ring->adev;
@@ -9881,8 +9856,8 @@ static const struct amdgpu_ring_funcs

gfx_v10_0_ring_funcs_compute = {

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c static const struct amdgpu_ring_funcs

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v10_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v10_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v10_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              20 + /* gfx_v10_0_ring_emit_gds_switch */
              7 + /* gfx_v10_0_ring_emit_hdp_flush */
@@ -9921,8 +9896,8 @@ static const struct amdgpu_ring_funcs

gfx_v10_0_ring_funcs_kiq = {

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

Review

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v10_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v10_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v10_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              20 + /* gfx_v10_0_ring_emit_gds_switch */
              7 + /* gfx_v10_0_ring_emit_hdp_flush */
(quoted diff) +0 -0

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

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static u64

Quoted 4 lines

index 427975b5a..404604f2d 100644

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
@@ -5818,32 +5818,6 @@ static u64

gfx_v11_0_ring_get_rptr_compute(struct amdgpu_ring *ring)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static u64 · line 5821

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -1
      return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v11_0_ring_get_wptr_compute(struct amdgpu_ring

*ring)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static u64 · line 5827

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -6
-{
-     u64 wptr;
-
-     /* XXX check if swapping is necessary on BE */
-     if (ring->use_doorbell)
-             wptr = atomic64_read((atomic64_t

*)ring->wptr_cpu_addr);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static u64 · line 5833

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -6
-     else
-             BUG();
-     return wptr;
-}
-
-static void gfx_v11_0_ring_set_wptr_compute(struct amdgpu_ring

*ring)

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static u64 · line 5843

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -10
-{
-     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 gfx11

now */

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static u64 · line 5821

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -3
-     }
-}
-
 static void gfx_v11_0_ring_emit_hdp_flush(struct amdgpu_ring

*ring)

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static const struct amdgpu_ring_funcs

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +0 -0
 {
      struct amdgpu_device *adev = ring->adev;
@@ -7266,8 +7240,8 @@ static const struct amdgpu_ring_funcs

gfx_v11_0_ring_funcs_compute = {

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c static const struct amdgpu_ring_funcs

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v11_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v11_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v11_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              5 + /* update_spm_vmid */
              20 + /* gfx_v11_0_ring_emit_gds_switch */
@@ -7307,8 +7281,8 @@ static const struct amdgpu_ring_funcs

gfx_v11_0_ring_funcs_kiq = {

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

Review

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v11_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v11_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v11_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              20 + /* gfx_v11_0_ring_emit_gds_switch */
              7 + /* gfx_v11_0_ring_emit_hdp_flush */
(quoted diff) +0 -0

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

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static u64

Quoted 4 lines

index 79ea1af36..7ba436444 100644

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +0 -0
@@ -4363,32 +4363,6 @@ static u64

gfx_v12_0_ring_get_rptr_compute(struct amdgpu_ring *ring)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static u64 · line 4366

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +0 -1
      return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v12_0_ring_get_wptr_compute(struct amdgpu_ring

*ring)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static u64 · line 4372

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +0 -6
-{
-     u64 wptr;
-
-     /* XXX check if swapping is necessary on BE */
-     if (ring->use_doorbell)
-             wptr = atomic64_read((atomic64_t

*)ring->wptr_cpu_addr);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static u64 · line 4378

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +0 -6
-     else
-             BUG();
-     return wptr;
-}
-
-static void gfx_v12_0_ring_set_wptr_compute(struct amdgpu_ring

*ring)

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static u64 · line 4388

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +0 -10
-{
-     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 gfx12

now */

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static u64 · line 4366

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +0 -3
-     }
-}
-
 static void gfx_v12_0_ring_emit_hdp_flush(struct amdgpu_ring

*ring)

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static const struct amdgpu_ring_funcs

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +0 -0
 {
      struct amdgpu_device *adev = ring->adev;
@@ -5523,8 +5497,8 @@ static const struct amdgpu_ring_funcs

gfx_v12_0_ring_funcs_compute = {

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c static const struct amdgpu_ring_funcs

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v12_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v12_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v12_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              7 + /* gfx_v12_0_ring_emit_hdp_flush */
              5 + /* hdp invalidate */
@@ -5561,8 +5535,8 @@ static const struct amdgpu_ring_funcs

gfx_v12_0_ring_funcs_kiq = {

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

Review

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v12_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v12_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v12_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              7 + /* gfx_v12_0_ring_emit_hdp_flush */
              5 + /*hdp invalidate */
(quoted diff) +0 -0

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

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

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

Quoted 4 lines

index 8249135d7..798f94bca 100644

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -0
@@ -5640,37 +5640,6 @@ static u64

gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 · line 5640

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -0
      return *ring->rptr_cpu_addr; /* gfx9 hardware is 32bit

rptr */

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 · line 5643

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -1
 }
 
-static u64 gfx_v9_0_ring_get_wptr_compute(struct amdgpu_ring

*ring)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 · line 5649

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -6
-{
-     u64 wptr;
-
-     /* XXX check if swapping is necessary on BE */
-     if (ring->use_doorbell) {
-             wptr = atomic64_read((atomic64_t

*)ring->wptr_cpu_addr);

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 · line 5651

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -2
-     } else {
-             WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr read on

ring %s, "

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 · line 5652

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -1
-                       "only doorbell method supported on

gfx9\n",

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 · line 5659

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -7
-                       ring->name);
-             wptr = 0;
-     }
-     return wptr;
-}
-
-static void gfx_v9_0_ring_set_wptr_compute(struct amdgpu_ring

*ring)

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 · line 5665

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -6
-{
-     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);

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 · line 5668

Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -3
-             WDOORBELL64(ring->doorbell_index, ring->wptr);
-     } else {
-             WARN_ONCE(1, "gfx_v9_0: non-doorbell wptr write on

ring %s, "

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 · line 5669

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -1
-                       "only doorbell method supported on

gfx9\n",

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 · line 5643

Quoted 5 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -4
-                       ring->name);
-     }
-}
-
 static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring

*ring, u64 addr,

Quoted 1 line
Quoted 1 line
Quoted 1 line

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static u64 · line 5644

Quoted 1 line
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -0
                                       u64 seq, unsigned int

flags)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static const struct amdgpu_ring_funcs

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +0 -0
 {
@@ -7627,8 +7596,8 @@ static const struct amdgpu_ring_funcs

gfx_v9_0_ring_funcs_compute = {

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c static const struct amdgpu_ring_funcs

Quoted 11 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v9_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v9_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v9_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              20 + /* gfx_v9_0_ring_emit_gds_switch */
              7 + /* gfx_v9_0_ring_emit_hdp_flush */
@@ -7669,8 +7638,8 @@ static const struct amdgpu_ring_funcs
Quoted 15 lines

gfx_v9_0_ring_funcs_kiq = {

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +2 -2
      .nop = PACKET3(PACKET3_NOP, 0x3FFF),
      .support_64bit_ptrs = true,
      .get_rptr = gfx_v9_0_ring_get_rptr_compute,
-     .get_wptr = gfx_v9_0_ring_get_wptr_compute,
-     .set_wptr = gfx_v9_0_ring_set_wptr_compute,
+     .get_wptr = amdgpu_gfx_get_wptr_compute,
+     .set_wptr = amdgpu_gfx_set_wptr_compute,
      .emit_frame_size =
              20 + /* gfx_v9_0_ring_emit_gds_switch */
              7 + /* gfx_v9_0_ring_emit_hdp_flush */

From 87ee6f612896df7f8665abe7da1a68628f8ba379 Mon Sep 17 00:00:00 2001
From: "John B. Moore" <[email protected]>
Date: Thu, 30 Apr 2026 08:47:09 -0500
Subject: [PATCH] drm/amdgpu/gfx: extract compute wptr doorbell helpers to
amdgpu_gfx.c
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Move the duplicated doorbell-based get_wptr/set_wptr functions from gfx_v9_0.c, gfx_v10_0.c, gfx_v11_0.c, and gfx_v12_0.c into common helpers amdgpu_gfx_get_wptr_compute() and amdgpu_gfx_set_wptr_compute() in amdgpu_gfx.c.

These functions are not HW generation dependent -- the doorbell path is identical across all four GFX versions.

The atomic64_t cast is retained via a static inline helper (wptr_as_atomic) to preserve 32-bit atomicity guarantees, since DRM_AMDGPU still supports 32-bit kernels where a plain 64-bit write would tear into two 32-bit writes.

Changes from v1 (Christian König review):
- Move doorbell prerequisite check to the top of each function as

    if (WARN_ON(!ring->use_doorbell)) return
  - Use WARN_ON, not WARN_ON_ONCE -- this is a functional coding
    error, not a HW error that would spam logs
  - Use rmb()/wmb() for CPU<->device ordering, not smp_rmb()/smp_wmb()
    which are CPU<->CPU only

- Place rmb() before the read, not after
- Wrap the atomic64_t cast in a helper to contain the ugliness

Not touched: gfx_v7_0, gfx_v8_0, gfx_v9_4_3 -- these have different wptr access patterns (MMIO registers or wb.wb[] offsets).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 55 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  3 ++
 drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c  | 33 ++-------------
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  | 34 ++-------------
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c  | 34 ++-------------
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c   | 31 ++------------
 6 files changed, 74 insertions(+), 116 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +55 -0
index b8ca87669..eb03baf5b 100644
@@ -2686,3 +2686,58 @@ void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)
 #endif
 }
 
+/*
+ * Helper to cast the u32 *wptr_cpu_addr to atomic64_t * for 64-bit
+ * atomic access.  We need atomic 64-bit loads/stores here because the
+ * GPU reads/writes this value concurrently and on 32-bit kernels a
+ * plain 64-bit write would tear into two 32-bit writes.
+ */
+static inline atomic64_t *wptr_as_atomic(struct amdgpu_ring *ring)
+{
+	return (atomic64_t *)ring->wptr_cpu_addr;
+}
+
+/**
+ * amdgpu_gfx_get_wptr_compute - common get_wptr for compute rings using doorbells
+ * @ring: amdgpu_ring pointer
+ *
+ * Read the write pointer from the doorbell-mapped writeback address.
+ * This is HW-agnostic and shared across GFX generations that use
+ * doorbell-based compute queue management.
+ */
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring)
+{
+	if (WARN_ON(!ring->use_doorbell))
+		return 0;
+
+	/* Ensure we see the latest value written by the device */
+	rmb();
+
+	/* XXX check if swapping is necessary on BE */
+	return atomic64_read(wptr_as_atomic(ring));
+}
+
+/**
+ * amdgpu_gfx_set_wptr_compute - common set_wptr for compute rings using doorbells
+ * @ring: amdgpu_ring pointer
+ *
+ * Write the write pointer to the doorbell-mapped writeback address and
+ * ring the doorbell.  This is HW-agnostic and shared across GFX
+ * generations that use doorbell-based compute queue management.
+ */
+void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring)
+{
+	struct amdgpu_device *adev = ring->adev;
+
+	if (WARN_ON(!ring->use_doorbell))
+		return;
+
+	/* XXX check if swapping is necessary on BE */
+	atomic64_set(wptr_as_atomic(ring), ring->wptr);
+
+	/* Ensure the wptr write is visible to the device before ringing the doorbell */
+	wmb();
+
+	WDOORBELL64(ring->doorbell_index, ring->wptr);
+}
+
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
index a0cf0a3b4..7bf177d5a 100644
@@ -661,6 +661,9 @@ u32 amdgpu_gfx_csb_preamble_start(u32 *buffer);
 u32 amdgpu_gfx_csb_data_parser(struct amdgpu_device *adev, u32 *buffer, u32 count);
 void amdgpu_gfx_csb_preamble_end(u32 *buffer, u32 count);
 
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring);
+void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring);
+
 void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device *adev);
 void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev);
 
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +4 -29
index 58c69dcb5..4ee7b5a92 100644
@@ -8591,31 +8591,6 @@ static u64 gfx_v10_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v10_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;
-}
-
-static void gfx_v10_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
-{
-	struct amdgpu_device *adev = ring->adev;
-
-	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 gfx10 now */
-	}
-}
-
 static void gfx_v10_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -9886,8 +9861,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v10_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v10_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v10_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v10_0_ring_emit_gds_switch */
 		7 + /* gfx_v10_0_ring_emit_hdp_flush */
@@ -9926,8 +9901,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v10_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v10_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v10_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v10_0_ring_emit_gds_switch */
 		7 + /* gfx_v10_0_ring_emit_hdp_flush */
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +4 -30
index 2c6f1e25c..c069b7001 100644
@@ -5891,32 +5891,6 @@ static u64 gfx_v11_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v11_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;
-}
-
-static void gfx_v11_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 gfx11 now */
-	}
-}
-
 static void gfx_v11_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -7331,8 +7305,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v11_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v11_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v11_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		5 + /* update_spm_vmid */
 		20 + /* gfx_v11_0_ring_emit_gds_switch */
@@ -7372,8 +7346,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v11_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v11_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v11_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v11_0_ring_emit_gds_switch */
 		7 + /* gfx_v11_0_ring_emit_hdp_flush */
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +4 -30
index 6baac533a..1758a6051 100644
@@ -4401,32 +4401,6 @@ static u64 gfx_v12_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v12_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;
-}
-
-static void gfx_v12_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 gfx12 now */
-	}
-}
-
 static void gfx_v12_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -5553,8 +5527,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v12_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v12_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v12_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		7 + /* gfx_v12_0_ring_emit_hdp_flush */
 		5 + /* hdp invalidate */
@@ -5591,8 +5565,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v12_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v12_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v12_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		7 + /* gfx_v12_0_ring_emit_hdp_flush */
 		5 + /*hdp invalidate */
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +4 -27
index 2eb32f92a..d50016e9a 100644
@@ -5634,30 +5634,7 @@ static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *ring->rptr_cpu_addr; /* gfx9 hardware is 32bit rptr */
 }
 
-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;
-}
 
-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 */
-	}
-}
 
 static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
 					 u64 seq, unsigned int flags)
@@ -7614,8 +7591,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v9_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v9_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v9_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v9_0_ring_emit_gds_switch */
 		7 + /* gfx_v9_0_ring_emit_hdp_flush */
@@ -7656,8 +7633,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v9_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v9_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v9_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v9_0_ring_emit_gds_switch */
 		7 + /* gfx_v9_0_ring_emit_hdp_flush */
Signature · 1 line
2.43.0

From b3bad3bc072041cd7fdebdad7831e5e70454ec68 Mon Sep 17 00:00:00 2001
From: "John B. Moore" <[email protected]>
Date: Thu, 30 Apr 2026 08:49:38 -0500
Subject: [PATCH] drm/amdgpu/gfx: extract compute wptr doorbell helpers to
amdgpu_gfx.c
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Move the duplicated doorbell-based get_wptr/set_wptr functions from gfx_v9_0.c, gfx_v10_0.c, gfx_v11_0.c, and gfx_v12_0.c into common helpers amdgpu_gfx_get_wptr_compute() and amdgpu_gfx_set_wptr_compute() in amdgpu_gfx.c.

These functions are not HW generation dependent -- the doorbell path is identical across all four GFX versions.

readq()/writeq() are used despite this being system memory (not MMIO) because we need atomic 64-bit reads/writes that will not tear on 32-bit kernels. The semantic mismatch with MMIO accessors is documented in the function comments.

Changes from v1 (Christian König review):
- Move doorbell prerequisite check to the top of each function as

    if (WARN_ON(!ring->use_doorbell)) return
  - Use WARN_ON, not WARN_ON_ONCE -- this is a functional coding
    error, not a HW error that would spam logs
  - Use rmb()/wmb() for CPU<->device ordering, not smp_rmb()/smp_wmb()
    which are CPU<->CPU only

- Place rmb() before the read, not after
- Replace atomic64_t cast with readq()/writeq()

Not touched: gfx_v7_0, gfx_v8_0, gfx_v9_4_3 -- these have different wptr access patterns (MMIO registers or wb.wb[] offsets).

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 52 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  3 ++
 drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c  | 33 ++--------------
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c  | 34 ++--------------
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c  | 34 ++--------------
 drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c   | 33 ++--------------
 6 files changed, 71 insertions(+), 118 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +52 -0

55 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
index a0cf0a3b4..7bf177d5a 100644
@@ -661,6 +661,9 @@ u32 amdgpu_gfx_csb_preamble_start(u32 *buffer);
 u32 amdgpu_gfx_csb_data_parser(struct amdgpu_device *adev, u32 *buffer, u32 count);
 void amdgpu_gfx_csb_preamble_end(u32 *buffer, u32 count);
 
+u64 amdgpu_gfx_get_wptr_compute(struct amdgpu_ring *ring);
+void amdgpu_gfx_set_wptr_compute(struct amdgpu_ring *ring);
+
 void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device *adev);
 void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev);
 
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +4 -29
index 58c69dcb5..4ee7b5a92 100644
@@ -8591,31 +8591,6 @@ static u64 gfx_v10_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v10_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;
-}
-
-static void gfx_v10_0_ring_set_wptr_compute(struct amdgpu_ring *ring)
-{
-	struct amdgpu_device *adev = ring->adev;
-
-	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 gfx10 now */
-	}
-}
-
 static void gfx_v10_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -9886,8 +9861,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v10_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v10_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v10_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v10_0_ring_emit_gds_switch */
 		7 + /* gfx_v10_0_ring_emit_hdp_flush */
@@ -9926,8 +9901,8 @@ static const struct amdgpu_ring_funcs gfx_v10_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v10_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v10_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v10_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v10_0_ring_emit_gds_switch */
 		7 + /* gfx_v10_0_ring_emit_hdp_flush */
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +4 -30
index 2c6f1e25c..c069b7001 100644
@@ -5891,32 +5891,6 @@ static u64 gfx_v11_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v11_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;
-}
-
-static void gfx_v11_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 gfx11 now */
-	}
-}
-
 static void gfx_v11_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -7331,8 +7305,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v11_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v11_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v11_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		5 + /* update_spm_vmid */
 		20 + /* gfx_v11_0_ring_emit_gds_switch */
@@ -7372,8 +7346,8 @@ static const struct amdgpu_ring_funcs gfx_v11_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v11_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v11_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v11_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v11_0_ring_emit_gds_switch */
 		7 + /* gfx_v11_0_ring_emit_hdp_flush */
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +4 -30
index 6baac533a..1758a6051 100644
@@ -4401,32 +4401,6 @@ static u64 gfx_v12_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *(uint32_t *)ring->rptr_cpu_addr;
 }
 
-static u64 gfx_v12_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;
-}
-
-static void gfx_v12_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 gfx12 now */
-	}
-}
-
 static void gfx_v12_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
@@ -5553,8 +5527,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v12_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v12_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v12_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		7 + /* gfx_v12_0_ring_emit_hdp_flush */
 		5 + /* hdp invalidate */
@@ -5591,8 +5565,8 @@ static const struct amdgpu_ring_funcs gfx_v12_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v12_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v12_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v12_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		7 + /* gfx_v12_0_ring_emit_hdp_flush */
 		5 + /*hdp invalidate */
drivers/gpu/drm/amd/amdgpu/gfx_v9_0.c +4 -29
index 2eb32f92a..cd8c62b6d 100644
@@ -5634,31 +5634,6 @@ static u64 gfx_v9_0_ring_get_rptr_compute(struct amdgpu_ring *ring)
 	return *ring->rptr_cpu_addr; /* gfx9 hardware is 32bit rptr */
 }
 
-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;
-}
-
-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 */
-	}
-}
-
 static void gfx_v9_0_ring_emit_fence_kiq(struct amdgpu_ring *ring, u64 addr,
 					 u64 seq, unsigned int flags)
 {
@@ -7614,8 +7589,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_compute = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v9_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v9_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v9_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v9_0_ring_emit_gds_switch */
 		7 + /* gfx_v9_0_ring_emit_hdp_flush */
@@ -7656,8 +7631,8 @@ static const struct amdgpu_ring_funcs gfx_v9_0_ring_funcs_kiq = {
 	.nop = PACKET3(PACKET3_NOP, 0x3FFF),
 	.support_64bit_ptrs = true,
 	.get_rptr = gfx_v9_0_ring_get_rptr_compute,
-	.get_wptr = gfx_v9_0_ring_get_wptr_compute,
-	.set_wptr = gfx_v9_0_ring_set_wptr_compute,
+	.get_wptr = amdgpu_gfx_get_wptr_compute,
+	.set_wptr = amdgpu_gfx_set_wptr_compute,
 	.emit_frame_size =
 		20 + /* gfx_v9_0_ring_emit_gds_switch */
 		7 + /* gfx_v9_0_ring_emit_hdp_flush */
Signature · 1 line
2.43.0