Skip to contentSkip to conversation

drm/amdgpu/gfx: add a detect_hung_queue callback

PATCH In review Jesse Zhang 2 participants 23 messages Sep 21

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

PATCH 1/21 drm/amdgpu/gfx: add a detect_hung_queue callback

Allow GFX recovery to resolve a doorbell to an HQD (ME, pipe, queue) without querying a stalled MES scheduler.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -0
index f5fb2e585e8a..a158b7f71b2f 100644
@@ -373,6 +373,9 @@ struct amdgpu_gfx_funcs {
 	int (*get_xccs_per_xcp)(struct amdgpu_device *adev);
 	void (*get_hdp_flush_mask)(struct amdgpu_ring *ring,
 				uint32_t *ref_and_mask, uint32_t *reg_mem_engine);
+	/* find the gfx HQD slot (me/pipe/queue) for a doorbell */
+	bool (*detect_hung_queue)(struct amdgpu_device *adev, u32 doorbell_index,
+				  u32 *me, u32 *pipe, u32 *queue);
 };
 
 struct sq_work {
Signature · 1 line
2.49.0
#1 lore raw

PATCH 2/21 drm/amdgpu/gfx12: implement detect_hung_queue

Implement detect_hung_queue() by matching enabled HQD doorbells over MMIO, allowing GFX reset to locate the queue without a MES request.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 42 ++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +42 -0
index ec1e142549c5..96aacaf2fc27 100644
@@ -912,6 +912,10 @@ static void gfx_v12_0_select_me_pipe_q(struct amdgpu_device *adev,
 	soc24_grbm_select(adev, me, pipe, q, vm);
 }
 
+static bool gfx_v12_0_detect_hung_queue(struct amdgpu_device *adev,
+					u32 doorbell_index,
+					u32 *me, u32 *pipe, u32 *queue);
+
 /* all sizes are in bytes */
 #define MQD_SHADOW_BASE_SIZE      73728
 #define MQD_SHADOW_BASE_ALIGNMENT 256
@@ -951,6 +955,7 @@ static const struct amdgpu_gfx_funcs gfx_v12_0_gfx_funcs = {
 	.read_wave_sgprs = &gfx_v12_0_read_wave_sgprs,
 	.read_wave_vgprs = &gfx_v12_0_read_wave_vgprs,
 	.select_me_pipe_q = &gfx_v12_0_select_me_pipe_q,
+	.detect_hung_queue = &gfx_v12_0_detect_hung_queue,
 	.update_perfmon_mgcg = &gfx_v12_0_update_perf_clk,
 	.get_gfx_shadow_info = &gfx_v12_0_get_gfx_shadow_info,
 	.get_hdp_flush_mask = &amdgpu_gfx_get_hdp_flush_mask,
@@ -5046,6 +5051,43 @@ static int gfx_v12_0_set_priv_inst_fault_state(struct amdgpu_device *adev,
 	return 0;
 }
 
+static bool gfx_v12_0_detect_hung_queue(struct amdgpu_device *adev,
+					u32 doorbell_index,
+					u32 *me, u32 *pipe, u32 *queue)
+{
+	u32 i, k, p, db_ctrl, dboff;
+	bool found = false;
+
+	amdgpu_gfx_off_ctrl(adev, false);
+	mutex_lock(&adev->srbm_mutex);
+	for (i = 0; i < adev->gfx.me.num_me && !found; i++) {
+		for (p = 0; p < adev->gfx.me.num_pipe_per_me && !found; p++) {
+			for (k = 0; k < adev->gfx.me.num_queue_per_pipe; k++) {
+				soc24_grbm_select(adev, i, p, k, 0);
+				db_ctrl = RREG32_SOC15(GC, 0,
+						       regCP_RB_DOORBELL_CONTROL);
+				if (!(db_ctrl & CP_RB_DOORBELL_CONTROL__DOORBELL_EN_MASK))
+					continue;
+				dboff = (db_ctrl &
+					 CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET_MASK) >>
+					CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT;
+				if (dboff == doorbell_index) {
+					*me = i;
+					*pipe = p;
+					*queue = k;
+					found = true;
+					break;
+				}
+			}
+		}
+	}
+	soc24_grbm_select(adev, 0, 0, 0, 0);
+	mutex_unlock(&adev->srbm_mutex);
+	amdgpu_gfx_off_ctrl(adev, true);
+
+	return found;
+}
+
 /*
  * A gfx exception IV carries no doorbell. For each faulted slot, read the
  * doorbell back from the HQD (left in place by the fatal fault), look up the
Signature · 1 line
2.49.0
#2 lore raw

PATCH 3/21 drm/amdgpu/gfx11: implement detect_hung_queue

In reply to Jesse Zhang

Implement detect_hung_queue() by matching enabled HQD doorbells over MMIO, allowing GFX reset to locate the queue without a MES request.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 38 ++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +38 -0
index a7ed4923857b..76e133d283a9 100644
@@ -1102,6 +1102,43 @@ static int gfx_v11_0_get_gfx_shadow_info(struct amdgpu_device *adev,
 	}
 }
 
+static bool gfx_v11_0_detect_hung_queue(struct amdgpu_device *adev,
+					u32 doorbell_index,
+					u32 *me, u32 *pipe, u32 *queue)
+{
+	u32 i, k, p, db_ctrl, dboff;
+	bool found = false;
+
+	amdgpu_gfx_off_ctrl(adev, false);
+	mutex_lock(&adev->srbm_mutex);
+	for (i = 0; i < adev->gfx.me.num_me && !found; i++) {
+		for (p = 0; p < adev->gfx.me.num_pipe_per_me && !found; p++) {
+			for (k = 0; k < adev->gfx.me.num_queue_per_pipe; k++) {
+				soc21_grbm_select(adev, i, p, k, 0);
+				db_ctrl = RREG32_SOC15(GC, 0,
+						       regCP_RB_DOORBELL_CONTROL);
+				if (!(db_ctrl & CP_RB_DOORBELL_CONTROL__DOORBELL_EN_MASK))
+					continue;
+				dboff = (db_ctrl &
+					 CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET_MASK) >>
+					CP_RB_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT;
+				if (dboff == doorbell_index) {
+					*me = i;
+					*pipe = p;
+					*queue = k;
+					found = true;
+					break;
+				}
+			}
+		}
+	}
+	soc21_grbm_select(adev, 0, 0, 0, 0);
+	mutex_unlock(&adev->srbm_mutex);
+	amdgpu_gfx_off_ctrl(adev, true);
+
+	return found;
+}
+
 static const struct amdgpu_gfx_funcs gfx_v11_0_gfx_funcs = {
 	.get_gpu_clock_counter = &gfx_v11_0_get_gpu_clock_counter,
 	.select_se_sh = &gfx_v11_0_select_se_sh,
@@ -1112,6 +1149,7 @@ static const struct amdgpu_gfx_funcs gfx_v11_0_gfx_funcs = {
 	.update_perfmon_mgcg = &gfx_v11_0_update_perf_clk,
 	.get_gfx_shadow_info = &gfx_v11_0_get_gfx_shadow_info,
 	.get_hdp_flush_mask = &amdgpu_gfx_get_hdp_flush_mask,
+	.detect_hung_queue = &gfx_v11_0_detect_hung_queue,
 };
 
 static int gfx_v11_0_gpu_early_init(struct amdgpu_device *adev)
Signature · 1 line
2.49.0
#3 lore raw

PATCH 4/21 drm/amdgpu/gfx: export amdgpu_gfx_mqd_reset_restore

In reply to Jesse Zhang

Expose amdgpu_gfx_mqd_reset_restore() so MES11/12 pipe reset can rebuild collateral kernel rings.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +1 -1
index 7a132e6d7e42..b91bb5166ea8 100644
@@ -379,7 +379,7 @@ int amdgpu_gfx_kiq_init(struct amdgpu_device *adev,
 	return 0;
 }
 
-static void amdgpu_gfx_mqd_reset_restore(struct amdgpu_ring *ring)
+void amdgpu_gfx_mqd_reset_restore(struct amdgpu_ring *ring)
 {
 	struct amdgpu_device *adev = ring->adev;
 	int mqd_idx, mqd_size;
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +1 -0
index a158b7f71b2f..c948fd2130a0 100644
@@ -699,6 +699,7 @@ int amdgpu_gfx_mes_reset_queue(struct amdgpu_ring *ring,
 			       unsigned int vmid,
 			       struct amdgpu_fence *timedout_fence,
 			       bool use_mmio);
+void amdgpu_gfx_mqd_reset_restore(struct amdgpu_ring *ring);
 
 static inline const char *amdgpu_gfx_compute_mode_desc(int mode)
 {
Signature · 1 line
2.49.0
#4 lore raw

PATCH 5/21 drm/amdgpu/mes12: restore collateral gfx queues after pipe reset

In reply to Jesse Zhang

A pipe reset restarts the shared PFP/ME, but CP firmware dequeues only the requested queue. Wait for its CP_VMID_RESET bit and HQD_ACTIVE to clear, and return -ETIMEDOUT if recovery does not complete.

Preempt each collateral kernel queue and wait for its HQD to deactivate before rebuilding its MQD/ring and remapping it. Leave the guilty kernel queue to the outer reset helper.

---
 drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 111 +++++++++++++++++++++----
 1 file changed, 96 insertions(+), 15 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +96 -15
index ee72afdb0a1d..41d6799177d2 100644
@@ -425,11 +425,73 @@ static bool mes_v12_0_pipe_reset_support(struct amdgpu_device *adev)
 	return false;
 }
 
+/* Use MES callbacks directly while holding the MES lock. */
+static int mes_v12_0_restore_gfx_kernel_queue(struct amdgpu_device *adev,
+					      u32 me, u32 pipe, u32 guilty_queue,
+					      bool is_kq)
+{
+	struct amdgpu_ring *ring;
+	int i, j, r;
+
+	/*
+	 * Only the guilty HQD was dequeued; stop collateral HQDs before rebuild.
+	 * The outer reset helper restores the guilty kernel queue.
+	 */
+	for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
+		struct mes_unmap_legacy_queue_input unmap = { 0 };
+		struct mes_map_legacy_queue_input map = { 0 };
+
+		ring = &adev->gfx.gfx_ring[i];
+		if (ring->me != me || ring->pipe != pipe ||
+		    (is_kq && ring->queue == guilty_queue))
+			continue;
+
+		unmap.action = PREEMPT_QUEUES;
+		unmap.queue_type = ring->funcs->type;
+		unmap.doorbell_offset = ring->doorbell_index;
+		unmap.pipe_id = ring->pipe;
+		unmap.queue_id = ring->queue;
+		r = adev->mes.funcs->unmap_legacy_queue(&adev->mes, &unmap);
+		if (r)
+			return r;
+
+		amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
+		mutex_lock(&adev->srbm_mutex);
+		soc24_grbm_select(adev, me, pipe, ring->queue, 0);
+		for (j = 0; j < adev->usec_timeout; j++) {
+			if (!(RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE) & 1))
+				break;
+			udelay(1);
+		}
+		soc24_grbm_select(adev, 0, 0, 0, 0);
+		mutex_unlock(&adev->srbm_mutex);
+		amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
+		if (j == adev->usec_timeout)
+			return -ETIMEDOUT;
+
+		amdgpu_gfx_mqd_reset_restore(ring);
+
+		map.queue_type = ring->funcs->type;
+		map.doorbell_offset = ring->doorbell_index;
+		map.pipe_id = ring->pipe;
+		map.queue_id = ring->queue;
+		map.mqd_addr = amdgpu_bo_gpu_offset(ring->mqd_obj);
+		map.wptr_addr = ring->wptr_gpu_addr;
+		r = adev->mes.funcs->map_legacy_queue(&adev->mes, &map);
+		if (r)
+			return r;
+	}
+
+	return 0;
+}
+
 static int mes_v12_0_reset_gfx_pipe_mmio(struct amdgpu_device *adev,
-					 u32 me, u32 pipe, u32 queue)
+					 u32 me, u32 pipe, u32 queue,
+					 bool is_kq)
 {
-	uint32_t reset_pipe = 0, clean_pipe = 0;
-	int r;
+	bool has_kq = adev->gfx.num_gfx_rings >= 1;
+	uint32_t reset_pipe, clean_pipe;
+	int i;
 
 	if (!mes_v12_0_pipe_reset_support(adev))
 		return -EOPNOTSUPP;
@@ -438,6 +500,9 @@ static int mes_v12_0_reset_gfx_pipe_mmio(struct amdgpu_device *adev,
 	mutex_lock(&adev->srbm_mutex);
 	soc24_grbm_select(adev, me, pipe, queue, 0);
 
+	reset_pipe = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+	clean_pipe = reset_pipe;
+
 	switch (pipe) {
 	case 0:
 		reset_pipe = REG_SET_FIELD(reset_pipe, CP_ME_CNTL,
@@ -463,22 +528,35 @@ static int mes_v12_0_reset_gfx_pipe_mmio(struct amdgpu_device *adev,
 		break;
 	}
 
+	/* Preserve ACTIVE/DEQUEUE so warm firmware can complete queue release. */
 	WREG32_SOC15(GC, 0, regCP_ME_CNTL, reset_pipe);
+	soc24_grbm_select(adev, me, pipe, queue, 0);
 	WREG32_SOC15(GC, 0, regCP_ME_CNTL, clean_pipe);
 
-	r = (RREG32(SOC15_REG_OFFSET(GC, 0, regCP_GFX_RS64_INSTR_PNTR1)) << 2) -
-					RS64_FW_UC_START_ADDR_LO;
+	/* CP firmware releases only the queue named in CP_VMID_RESET. */
+	soc24_grbm_select(adev, me, pipe, queue, 0);
+	for (i = 0; i < adev->usec_timeout; i++) {
+		if (!(RREG32_SOC15(GC, 0, regCP_VMID_RESET) &
+		      BIT(16 + pipe * 8 + queue)) &&
+		    !(RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE) & 1))
+			break;
+		udelay(1);
+	}
+
 	soc24_grbm_select(adev, 0, 0, 0, 0);
 	mutex_unlock(&adev->srbm_mutex);
 	amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
 
-	dev_info(adev->dev, "The gfx pipe reset: %s\n",
-			r == 0 ? "successfully" : "failed");
-	/* Sometimes the ME start pc counter can't cache correctly, so the
-	 * PC check only as a reference and pipe reset result rely on the
-	 * later ring test.
-	 */
-	return 0;
+	if (i >= adev->usec_timeout) {
+		dev_err(adev->dev,
+			"gfx pipe reset: CP front-end still busy, CP still wedged\n");
+		return -ETIMEDOUT;
+	}
+
+	if (!has_kq)
+		return 0;
+
+	return mes_v12_0_restore_gfx_kernel_queue(adev, me, pipe, queue, is_kq);
 }
 
 /*
@@ -679,12 +757,14 @@ static int mes_v12_0_reset_queue_mmio(struct amdgpu_mes *mes, uint32_t queue_typ
 
 static int mes_v12_0_reset_pipe_mmio(struct amdgpu_mes *mes, uint32_t queue_type,
 				     uint32_t me_id, uint32_t pipe_id,
-				     uint32_t queue_id, uint32_t vmid)
+				     uint32_t queue_id, uint32_t vmid,
+				     bool is_kq)
 {
 	struct amdgpu_device *adev = mes->adev;
 
 	if (queue_type == AMDGPU_RING_TYPE_GFX)
-		return mes_v12_0_reset_gfx_pipe_mmio(adev, me_id, pipe_id, queue_id);
+		return mes_v12_0_reset_gfx_pipe_mmio(adev, me_id, pipe_id, queue_id,
+						     is_kq);
 	else if (queue_type == AMDGPU_RING_TYPE_COMPUTE)
 		return mes_v12_0_reset_compute_pipe_mmio(adev, me_id, pipe_id, queue_id);
 	else
@@ -1106,7 +1186,8 @@ static int mes_v12_0_reset_hw_queue(struct amdgpu_mes *mes,
 		if (r)
 			return mes_v12_0_reset_pipe_mmio(mes, input->queue_type,
 							 input->me_id, input->pipe_id,
-							 input->queue_id, input->vmid);
+							 input->queue_id, input->vmid,
+							 input->is_kq);
 		return 0;
 	}
 
Signature · 1 line
2.49.0
#5 lore raw

PATCH 6/21 drm/amdgpu/mes11: restore collateral gfx queues after pipe reset

In reply to Jesse Zhang

A pipe reset restarts the shared PFP/ME, but CP firmware dequeues only the requested queue. Wait for its CP_VMID_RESET bit and HQD_ACTIVE to clear, and return -ETIMEDOUT if recovery does not complete.

Preempt each collateral kernel queue and wait for its HQD to deactivate before rebuilding its MQD/ring and remapping it. Leave the guilty kernel queue to the outer reset helper.

---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 137 ++++++++++++++++++++-----
 1 file changed, 110 insertions(+), 27 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +110 -27
index c81e4b8616d0..be3941ae337a 100644
@@ -410,19 +410,85 @@ static bool mes_v11_0_pipe_reset_support(struct amdgpu_device *adev)
 	dev_warn_once(adev->dev, "The CPFW hasn't support pipe reset yet.\n");
 	return false;
 }
+
+/* Use MES callbacks directly while holding the MES lock. */
+static int mes_v11_0_restore_gfx_kernel_queue(struct amdgpu_device *adev,
+					      u32 me, u32 pipe, u32 guilty_queue,
+					      bool is_kq)
+{
+	struct amdgpu_ring *ring;
+	int i, j, r;
+
+	/*
+	 * Only the guilty HQD was dequeued; stop collateral HQDs before rebuild.
+	 * The outer reset helper restores the guilty kernel queue.
+	 */
+	for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
+		struct mes_unmap_legacy_queue_input unmap = { 0 };
+		struct mes_map_legacy_queue_input map = { 0 };
+
+		ring = &adev->gfx.gfx_ring[i];
+		if (ring->me != me || ring->pipe != pipe ||
+		    (is_kq && ring->queue == guilty_queue))
+			continue;
+
+		unmap.action = PREEMPT_QUEUES;
+		unmap.queue_type = ring->funcs->type;
+		unmap.doorbell_offset = ring->doorbell_index;
+		unmap.pipe_id = ring->pipe;
+		unmap.queue_id = ring->queue;
+		r = adev->mes.funcs->unmap_legacy_queue(&adev->mes, &unmap);
+		if (r)
+			return r;
+
+		amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
+		mutex_lock(&adev->srbm_mutex);
+		soc21_grbm_select(adev, me, pipe, ring->queue, 0);
+		for (j = 0; j < adev->usec_timeout; j++) {
+			if (!(RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE) & 1))
+				break;
+			udelay(1);
+		}
+		soc21_grbm_select(adev, 0, 0, 0, 0);
+		mutex_unlock(&adev->srbm_mutex);
+		amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
+		if (j == adev->usec_timeout)
+			return -ETIMEDOUT;
+
+		amdgpu_gfx_mqd_reset_restore(ring);
+
+		map.queue_type = ring->funcs->type;
+		map.doorbell_offset = ring->doorbell_index;
+		map.pipe_id = ring->pipe;
+		map.queue_id = ring->queue;
+		map.mqd_addr = amdgpu_bo_gpu_offset(ring->mqd_obj);
+		map.wptr_addr = ring->wptr_gpu_addr;
+		r = adev->mes.funcs->map_legacy_queue(&adev->mes, &map);
+		if (r)
+			return r;
+	}
+
+	return 0;
+}
+
 static int mes_v11_0_reset_gfx_pipe_mmio(struct amdgpu_device *adev,
-					 u32 me, u32 pipe, u32 queue)
+					 u32 me, u32 pipe, u32 queue,
+					 bool is_kq)
 {
-	uint32_t reset_pipe = 0, clean_pipe = 0;
-	int r;
+	/* gfx11 keeps a setup ring object even when kernel queues are disabled. */
+	bool has_kq = !adev->gfx.disable_kq && adev->gfx.num_gfx_rings >= 1;
+	uint32_t reset_pipe, clean_pipe;
+	int i;
 
 	if (!mes_v11_0_pipe_reset_support(adev))
 		return -EOPNOTSUPP;
-
 	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
 	mutex_lock(&adev->srbm_mutex);
 	soc21_grbm_select(adev, me, pipe, queue, 0);
 
+	reset_pipe = RREG32_SOC15(GC, 0, regCP_ME_CNTL);
+	clean_pipe = reset_pipe;
+
 	switch (pipe) {
 	case 0:
 		reset_pipe = REG_SET_FIELD(reset_pipe, CP_ME_CNTL,
@@ -448,21 +514,35 @@ static int mes_v11_0_reset_gfx_pipe_mmio(struct amdgpu_device *adev,
 		break;
 	}
 
+	/* Preserve ACTIVE/DEQUEUE so warm firmware can complete queue release. */
 	WREG32_SOC15(GC, 0, regCP_ME_CNTL, reset_pipe);
+	soc21_grbm_select(adev, me, pipe, queue, 0);
 	WREG32_SOC15(GC, 0, regCP_ME_CNTL, clean_pipe);
 
-	r = (RREG32(SOC15_REG_OFFSET(GC, 0, regCP_GFX_RS64_INSTR_PNTR1)) << 2) -
-						RS64_FW_UC_START_ADDR_LO;
+	/* CP firmware releases only the queue named in CP_VMID_RESET. */
+	soc21_grbm_select(adev, me, pipe, queue, 0);
+	for (i = 0; i < adev->usec_timeout; i++) {
+		if (!(RREG32_SOC15(GC, 0, regCP_VMID_RESET) &
+		      BIT(16 + pipe * 8 + queue)) &&
+		    !(RREG32_SOC15(GC, 0, regCP_GFX_HQD_ACTIVE) & 1))
+			break;
+		udelay(1);
+	}
+
 	soc21_grbm_select(adev, 0, 0, 0, 0);
 	mutex_unlock(&adev->srbm_mutex);
 	amdgpu_gfx_rlc_exit_safe_mode(adev, 0);
 
-	dev_info(adev->dev, "The gfx pipe reset to the ME firmware start PC: %s\n",
-			r == 0 ? "successfully" : "failed");
-	/* FIXME: Sometimes driver can't cache the ME firmware start PC correctly,
-	 * so the pipe reset status relies on the later gfx ring test result.
-	 */
-	return 0;
+	if (i >= adev->usec_timeout) {
+		dev_err(adev->dev,
+			"gfx pipe reset: CP front-end still busy, CP still wedged\n");
+		return -ETIMEDOUT;
+	}
+
+	if (!has_kq)
+		return 0;
+
+	return mes_v11_0_restore_gfx_kernel_queue(adev, me, pipe, queue, is_kq);
 }
 
 /*
@@ -614,20 +694,6 @@ static int mes_v11_0_reset_compute_pipe_mmio(struct amdgpu_device *adev,
 	return 0;
 }
 
-static int mes_v11_0_reset_pipe_mmio(struct amdgpu_mes *mes, uint32_t queue_type,
-				     uint32_t me_id, uint32_t pipe_id,
-				     uint32_t queue_id, uint32_t vmid)
-{
-	struct amdgpu_device *adev = mes->adev;
-
-	if (queue_type == AMDGPU_RING_TYPE_GFX)
-		return mes_v11_0_reset_gfx_pipe_mmio(adev, me_id, pipe_id, queue_id);
-	else if (queue_type == AMDGPU_RING_TYPE_COMPUTE)
-		return mes_v11_0_reset_compute_pipe_mmio(adev, me_id, pipe_id, queue_id);
-	else
-		return -EOPNOTSUPP;
-}
-
 static int mes_v11_0_reset_queue_mmio(struct amdgpu_mes *mes, uint32_t queue_type,
 				      uint32_t me_id, uint32_t pipe_id,
 				      uint32_t queue_id, uint32_t vmid)
@@ -722,6 +788,22 @@ static int mes_v11_0_reset_queue_mmio(struct amdgpu_mes *mes, uint32_t queue_typ
 	return r;
 }
 
+static int mes_v11_0_reset_pipe_mmio(struct amdgpu_mes *mes, uint32_t queue_type,
+				     uint32_t me_id, uint32_t pipe_id,
+				     uint32_t queue_id, uint32_t vmid,
+				     bool is_kq)
+{
+	struct amdgpu_device *adev = mes->adev;
+
+	if (queue_type == AMDGPU_RING_TYPE_GFX)
+		return mes_v11_0_reset_gfx_pipe_mmio(adev, me_id, pipe_id,
+						  queue_id, is_kq);
+	else if (queue_type == AMDGPU_RING_TYPE_COMPUTE)
+		return mes_v11_0_reset_compute_pipe_mmio(adev, me_id, pipe_id, queue_id);
+	else
+		return -EOPNOTSUPP;
+}
+
 static int mes_v11_0_map_legacy_queue(struct amdgpu_mes *mes,
 				      struct mes_map_legacy_queue_input *input)
 {
@@ -1084,7 +1166,8 @@ static int mes_v11_0_reset_hw_queue(struct amdgpu_mes *mes,
 		if (r)
 			return mes_v11_0_reset_pipe_mmio(mes, input->queue_type,
 							 input->me_id, input->pipe_id,
-							 input->queue_id, input->vmid);
+							 input->queue_id, input->vmid,
+							 input->is_kq);
 		return 0;
 	}
 
Signature · 1 line
2.49.0
#6 lore raw

PATCH 7/21 drm/amdgpu/mes11: read the gfx user queue VMID for MMIO reset

In reply to Jesse Zhang

Read the MES-assigned VMID from CP_GFX_HQD_VMID under srbm_mutex when resetting a GFX user queue over MMIO. Kernel queues keep the IB VMID supplied by the caller.

---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +13 -5
index be3941ae337a..a019b1926305 100644
@@ -696,7 +696,8 @@ static int mes_v11_0_reset_compute_pipe_mmio(struct amdgpu_device *adev,
 
 static int mes_v11_0_reset_queue_mmio(struct amdgpu_mes *mes, uint32_t queue_type,
 				      uint32_t me_id, uint32_t pipe_id,
-				      uint32_t queue_id, uint32_t vmid)
+				      uint32_t queue_id, uint32_t vmid,
+				      bool is_kq)
 {
 	struct amdgpu_device *adev = mes->adev;
 	uint32_t value, reg;
@@ -705,11 +706,15 @@ static int mes_v11_0_reset_queue_mmio(struct amdgpu_mes *mes, uint32_t queue_typ
 	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
 
 	if (queue_type == AMDGPU_RING_TYPE_GFX) {
-		dev_info(adev->dev, "reset gfx queue (%d:%d:%d: vmid:%d)\n",
-			 me_id, pipe_id, queue_id, vmid);
-
 		mutex_lock(&adev->gfx.reset_sem_mutex);
 		gfx_v11_0_request_gfx_index_mutex(adev, true);
+		mutex_lock(&adev->srbm_mutex);
+		soc21_grbm_select(adev, me_id, pipe_id, queue_id, 0);
+		/* Userq VMIDs are assigned by MES; KGQ uses the supplied IB VMID. */
+		if (!is_kq) {
+			value = RREG32_SOC15(GC, 0, regCP_GFX_HQD_VMID);
+			vmid = REG_GET_FIELD(value, CP_GFX_HQD_VMID, VMID);
+		}
 		/* all se allow writes */
 		WREG32_SOC15(GC, 0, regGRBM_GFX_INDEX,
 			     (uint32_t)(0x1 << GRBM_GFX_INDEX__SE_BROADCAST_WRITES__SHIFT));
@@ -719,6 +724,8 @@ static int mes_v11_0_reset_queue_mmio(struct amdgpu_mes *mes, uint32_t queue_typ
 		else
 			value = REG_SET_FIELD(value, CP_VMID_RESET, PIPE1_QUEUES, 1 << queue_id);
 		WREG32_SOC15(GC, 0, regCP_VMID_RESET, value);
+		soc21_grbm_select(adev, 0, 0, 0, 0);
+		mutex_unlock(&adev->srbm_mutex);
 		gfx_v11_0_request_gfx_index_mutex(adev, false);
 		mutex_unlock(&adev->gfx.reset_sem_mutex);
 
@@ -1162,7 +1169,8 @@ static int mes_v11_0_reset_hw_queue(struct amdgpu_mes *mes,
 	if (input->use_mmio) {
 		int r = mes_v11_0_reset_queue_mmio(mes, input->queue_type,
 						   input->me_id, input->pipe_id,
-						   input->queue_id, input->vmid);
+						   input->queue_id, input->vmid,
+						   input->is_kq);
 		if (r)
 			return mes_v11_0_reset_pipe_mmio(mes, input->queue_type,
 							 input->me_id, input->pipe_id,
Signature · 1 line
2.49.0
#7 lore raw

PATCH 8/21 drm/amdgpu/mes12: read the gfx user queue VMID for MMIO reset

In reply to Jesse Zhang

Read the MES-assigned VMID from CP_GFX_HQD_VMID under srbm_mutex when resetting a GFX user queue over MMIO. Kernel queues keep the IB VMID supplied by the caller.

---
 drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +13 -5
index 41d6799177d2..ead276503522 100644
@@ -663,7 +663,8 @@ static int mes_v12_0_reset_compute_pipe_mmio(struct amdgpu_device *adev,
 
 static int mes_v12_0_reset_queue_mmio(struct amdgpu_mes *mes, uint32_t queue_type,
 				      uint32_t me_id, uint32_t pipe_id,
-				      uint32_t queue_id, uint32_t vmid)
+				      uint32_t queue_id, uint32_t vmid,
+				      bool is_kq)
 {
 	struct amdgpu_device *adev = mes->adev;
 	uint32_t value, reg;
@@ -672,11 +673,15 @@ static int mes_v12_0_reset_queue_mmio(struct amdgpu_mes *mes, uint32_t queue_typ
 	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
 
 	if (queue_type == AMDGPU_RING_TYPE_GFX) {
-		dev_info(adev->dev, "reset gfx queue (%d:%d:%d: vmid:%d)\n",
-			 me_id, pipe_id, queue_id, vmid);
-
 		mutex_lock(&adev->gfx.reset_sem_mutex);
 		gfx_v12_0_request_gfx_index_mutex(adev, true);
+		mutex_lock(&adev->srbm_mutex);
+		soc24_grbm_select(adev, me_id, pipe_id, queue_id, 0);
+		/* Userq VMIDs are assigned by MES; KGQ uses the supplied IB VMID. */
+		if (!is_kq) {
+			value = RREG32_SOC15(GC, 0, regCP_GFX_HQD_VMID);
+			vmid = REG_GET_FIELD(value, CP_GFX_HQD_VMID, VMID);
+		}
 		/* all se allow writes */
 		WREG32_SOC15(GC, 0, regGRBM_GFX_INDEX,
 			     (uint32_t)(0x1 << GRBM_GFX_INDEX__SE_BROADCAST_WRITES__SHIFT));
@@ -686,6 +691,8 @@ static int mes_v12_0_reset_queue_mmio(struct amdgpu_mes *mes, uint32_t queue_typ
 		else
 			value = REG_SET_FIELD(value, CP_VMID_RESET, PIPE1_QUEUES, 1 << queue_id);
 		WREG32_SOC15(GC, 0, regCP_VMID_RESET, value);
+		soc24_grbm_select(adev, 0, 0, 0, 0);
+		mutex_unlock(&adev->srbm_mutex);
 		gfx_v12_0_request_gfx_index_mutex(adev, false);
 		mutex_unlock(&adev->gfx.reset_sem_mutex);
 
@@ -1182,7 +1189,8 @@ static int mes_v12_0_reset_hw_queue(struct amdgpu_mes *mes,
 	if (input->use_mmio) {
 		int r = mes_v12_0_reset_queue_mmio(mes, input->queue_type,
 						   input->me_id, input->pipe_id,
-						   input->queue_id, input->vmid);
+						   input->queue_id, input->vmid,
+						   input->is_kq);
 		if (r)
 			return mes_v12_0_reset_pipe_mmio(mes, input->queue_type,
 							 input->me_id, input->pipe_id,
Signature · 1 line
2.49.0
#8 lore raw

PATCH 9/21 drm/amdgpu/gfx12: set KMD_QUEUE for kernel gfx queues

In reply to Jesse Zhang

Set KMD_QUEUE in the GFX MQD and kernel ring control registers so CP preserves kernel IB VMIDs instead of replacing them with the queue VMID. Use prop->kernel_queue to distinguish kernel and user MQDs.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +2 -0
index 96aacaf2fc27..c559b355eb49 100644
@@ -2776,6 +2776,7 @@ static int gfx_v12_0_cp_gfx_resume(struct amdgpu_device *adev)
 	rb_bufsz = order_base_2(ring->ring_size / 8);
 	tmp = REG_SET_FIELD(0, CP_RB0_CNTL, RB_BUFSZ, rb_bufsz);
 	tmp = REG_SET_FIELD(tmp, CP_RB0_CNTL, RB_BLKSZ, rb_bufsz - 2);
+	tmp = REG_SET_FIELD(tmp, CP_RB0_CNTL, KMD_QUEUE, 1);
 	WREG32_SOC15(GC, 0, regCP_RB0_CNTL, tmp);
 
 	/* Initialize the ring buffer's write pointers */
@@ -3068,6 +3069,7 @@ static int gfx_v12_0_gfx_mqd_init(struct amdgpu_device *adev, void *m,
 	tmp = regCP_GFX_HQD_CNTL_DEFAULT;
 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, RB_BUFSZ, rb_bufsz);
 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, RB_BLKSZ, rb_bufsz - 2);
+	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, KMD_QUEUE, prop->kernel_queue);
 #ifdef __BIG_ENDIAN
 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, BUF_SWAP, 1);
 #endif
Signature · 1 line
2.49.0
#9 lore raw

PATCH 10/21 drm/amdgpu/gfx11: set KMD_QUEUE for kernel gfx queues

In reply to Jesse Zhang

Set KMD_QUEUE in the GFX MQD and kernel ring control registers so CP preserves kernel IB VMIDs instead of replacing them with the queue VMID. Use prop->kernel_queue to distinguish kernel and user MQDs.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +2 -0
index 76e133d283a9..5ed70bbf5313 100644
@@ -3850,6 +3850,7 @@ static int gfx_v11_0_cp_gfx_resume(struct amdgpu_device *adev)
 	rb_bufsz = order_base_2(ring->ring_size / 8);
 	tmp = REG_SET_FIELD(0, CP_RB0_CNTL, RB_BUFSZ, rb_bufsz);
 	tmp = REG_SET_FIELD(tmp, CP_RB0_CNTL, RB_BLKSZ, rb_bufsz - 2);
+	tmp = REG_SET_FIELD(tmp, CP_RB0_CNTL, KMD_QUEUE, 1);
 	WREG32_SOC15(GC, 0, regCP_RB0_CNTL, tmp);
 
 	/* Initialize the ring buffer's write pointers */
@@ -4258,6 +4259,7 @@ static int gfx_v11_0_gfx_mqd_init(struct amdgpu_device *adev, void *m,
 	tmp = regCP_GFX_HQD_CNTL_DEFAULT;
 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, RB_BUFSZ, rb_bufsz);
 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, RB_BLKSZ, rb_bufsz - 2);
+	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, KMD_QUEUE, prop->kernel_queue);
 #ifdef __BIG_ENDIAN
 	tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, BUF_SWAP, 1);
 #endif
Signature · 1 line
2.49.0
#10 lore raw

PATCH 11/21 drm/amdgpu: support non-guilty job save on ring backup

In reply to Jesse Zhang

amdgpu_ring_backup_unprocessed_commands() is also used to preserve a ring that carries no guilty command - for example a collateral kernel gfx ring that shares the faulting pipe during a gfx user-queue pipe reset. Such a ring is backed up with a NULL guilty_fence and all of its unprocessed commands must be saved so they can be replayed after the reset rebuilds the ring's MQD.

The early-return that skips an already-seen guilty fence compared "ring->guilty_fence == guilty_fence", which is also true when both are NULL. A non-guilty save therefore hit the early return and left ring_backup_entries_to_copy at 0, so the pending commands were dropped when the ring was rebuilt.

Guard the early return with a non-NULL guilty_fence so a NULL fence never takes it and every unprocessed command is backed up.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +7 -6
index 3043ad041bb4..6537e8845fb6 100644
@@ -733,7 +733,9 @@ void amdgpu_ring_set_fence_errors_and_reemit(struct amdgpu_ring *ring,
 	 * we are skipping it on purpose.
 	 */
 	if (!ring->ring_backup_entries_to_copy) {
-		amdgpu_fence_driver_force_completion(ring, &guilty_fence->base);
+		if (guilty_fence)
+			amdgpu_fence_driver_force_completion(ring,
+						     &guilty_fence->base);
 		return;
 	}
 	ring->reemit = true;
@@ -804,12 +806,11 @@ void amdgpu_ring_backup_unprocessed_commands(struct amdgpu_ring *ring,
 	seq = ring->fence_drv.sync_seq & ring->fence_drv.num_fences_mask;
 	ring->ring_backup_entries_to_copy = 0;
 
-	/* if we've already seen this fence, return early.
-	 * ring->ring_backup_entries_to_copy is set to 0 so
-	 * the reemit helper will return early as well to
-	 * avoid getting stuck in a reemit loop.
+	/*
+	 * Avoid replay loops for a repeated guilty fence. NULL identifies a
+	 * collateral ring, whose pending commands still need backup.
 	 */
-	if (ring->guilty_fence == guilty_fence) {
+	if (guilty_fence && ring->guilty_fence == guilty_fence) {
 		ring->guilty_fence = NULL;
 		return;
 	}
Signature · 1 line
2.49.0
#11 lore raw

PATCH 12/21 drm/amdgpu: fix empty fence backup and replay intervals

In reply to Jesse Zhang

amdgpu_ring_set_fence_errors_and_reemit() and amdgpu_ring_backup_unprocessed_commands() masked last_seq and sync_seq to fence-array indices before the loop, then walked a do/while until the two indices met. When hardware completed every command before the driver signalled the fences, last_seq == sync_seq, but the masked-index do/while could not tell that empty interval from a full table and walked the whole fence array, backing up and replaying already-completed commands.

Read the raw sequence numbers, return early through amdgpu_fence_process() when they are equal, and mask only at the array index. Guard the force-completion against a NULL guilty_fence so a non-guilty backup does not dereference it.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c | 24 +++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_fence.c +14 -10
index 6537e8845fb6..3017ddcefbe2 100644
@@ -724,8 +724,14 @@ void amdgpu_ring_set_fence_errors_and_reemit(struct amdgpu_ring *ring,
 	bool is_guilty_fence;
 	bool is_guilty_context;
 
-	last_seq = amdgpu_fence_read(ring) & ring->fence_drv.num_fences_mask;
-	seq = ring->fence_drv.sync_seq & ring->fence_drv.num_fences_mask;
+	last_seq = amdgpu_fence_read(ring);
+	seq = ring->fence_drv.sync_seq;
+
+	/* Hardware completion can precede the interrupt which signals fences. */
+	if (last_seq == seq) {
+		amdgpu_fence_process(ring);
+		return;
+	}
 
 	/* If there is nothing to reemit, return early and set an error on the fence
 	 * if applicable. If all of the fences are siganlled, this will be a nop.
@@ -743,9 +749,8 @@ void amdgpu_ring_set_fence_errors_and_reemit(struct amdgpu_ring *ring,
 	spin_lock_irqsave(&ring->fence_drv.lock, flags);
 	do {
 		last_seq++;
-		last_seq &= ring->fence_drv.num_fences_mask;
 
-		ptr = &ring->fence_drv.fences[last_seq];
+		ptr = &ring->fence_drv.fences[last_seq & ring->fence_drv.num_fences_mask];
 		rcu_read_lock();
 		unprocessed = rcu_dereference(*ptr);
 
@@ -802,8 +807,8 @@ void amdgpu_ring_backup_unprocessed_commands(struct amdgpu_ring *ring,
 	struct amdgpu_fence *fence;
 	u32 seq, last_seq;
 
-	last_seq = amdgpu_fence_read(ring) & ring->fence_drv.num_fences_mask;
-	seq = ring->fence_drv.sync_seq & ring->fence_drv.num_fences_mask;
+	last_seq = amdgpu_fence_read(ring);
+	seq = ring->fence_drv.sync_seq;
 	ring->ring_backup_entries_to_copy = 0;
 
 	/*
@@ -816,11 +821,10 @@ void amdgpu_ring_backup_unprocessed_commands(struct amdgpu_ring *ring,
 	}
 	ring->guilty_fence = guilty_fence;
 
-	do {
+	while (last_seq != seq) {
 		last_seq++;
-		last_seq &= ring->fence_drv.num_fences_mask;
 
-		ptr = &ring->fence_drv.fences[last_seq];
+		ptr = &ring->fence_drv.fences[last_seq & ring->fence_drv.num_fences_mask];
 		rcu_read_lock();
 		unprocessed = rcu_dereference(*ptr);
 
@@ -830,7 +834,7 @@ void amdgpu_ring_backup_unprocessed_commands(struct amdgpu_ring *ring,
 			amdgpu_ring_backup_unprocessed_command(ring, fence);
 		}
 		rcu_read_unlock();
-	} while (last_seq != seq);
+	}
 }
 
 struct amdgpu_fence *
Signature · 1 line
2.49.0
#12 lore raw

PATCH 13/21 drm/amdgpu: factor out the MES user queue reset body

In reply to Jesse Zhang

Extract mes_userq_reset_hw() for reuse by GFX recovery. Keep reset and MES queue removal together, marking the queue HUNG only after both operations succeed. The existing reset entry point uses the same helper.

---
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 44 ++++++++++++----------
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.h |  4 ++
 2 files changed, 28 insertions(+), 20 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +24 -20
index fe49aa4dc5ff..948813042e98 100644
@@ -207,12 +207,34 @@ static int mes_userq_unmap(struct amdgpu_usermode_queue *queue)
 	return 0;
 }
 
+int mes_userq_reset_hw(struct amdgpu_usermode_queue *queue,
+		       struct mes_reset_queue_input *input)
+{
+	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
+	struct amdgpu_device *adev = uq_mgr->adev;
+	int r;
+
+	amdgpu_mes_lock(&adev->mes);
+	r = adev->mes.funcs->reset_hw_queue(&adev->mes, input);
+	amdgpu_mes_unlock(&adev->mes);
+	if (r)
+		return r;
+
+	r = mes_userq_unmap(queue);
+	if (r)
+		return r;
+
+	/* Prevent the restore worker from resubmitting the guilty job. */
+	trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_HUNG);
+	queue->state = AMDGPU_USERQ_STATE_HUNG;
+	return 0;
+}
+
 int mes_userq_reset(struct amdgpu_usermode_queue *queue)
 {
 	struct amdgpu_userq_mgr *uq_mgr = queue->userq_mgr;
 	struct amdgpu_device *adev = uq_mgr->adev;
 	struct mes_reset_queue_input queue_input;
-	int r;
 
 	/* already reset by an earlier job's hang-detect; just signal and bail */
 	if (queue->state == AMDGPU_USERQ_STATE_HUNG)
@@ -244,25 +266,7 @@ int mes_userq_reset(struct amdgpu_usermode_queue *queue)
 		}
 	}
 
-	amdgpu_mes_lock(&adev->mes);
-	r = adev->mes.funcs->reset_hw_queue(&adev->mes, &queue_input);
-	amdgpu_mes_unlock(&adev->mes);
-	if (r)
-		return r;
-
-	/* drop the queue from MES */
-	r = mes_userq_unmap(queue);
-	if (r)
-		return r;
-
-	/*
-	 * HUNG, not UNMAPPED: the guilty job is still in the ring, so the
-	 * restore worker must not re-map and re-run it.
-	 */
-	trace_amdgpu_userq_state_changed(queue, AMDGPU_USERQ_STATE_HUNG);
-	queue->state = AMDGPU_USERQ_STATE_HUNG;
-
-	return 0;
+	return mes_userq_reset_hw(queue, &queue_input);
 }
 
 int mes_userq_reset_queue(struct amdgpu_device *adev,
drivers/gpu/drm/amd/amdgpu/mes_userqueue.h +4 -0
index a473360d6a8b..5e6d444447c9 100644
@@ -26,8 +26,12 @@
 #define MES_USERQ_H
 #include "amdgpu_userq.h"
 
+struct mes_reset_queue_input;
+
 extern const struct amdgpu_userq_funcs userq_mes_funcs;
 
+int mes_userq_reset_hw(struct amdgpu_usermode_queue *queue,
+		       struct mes_reset_queue_input *input);
 int mes_userq_reset(struct amdgpu_usermode_queue *queue);
 int mes_userq_reset_queue(struct amdgpu_device *adev,
 			  struct amdgpu_usermode_queue *guilty_uq,
Signature · 1 line
2.49.0
#13 lore raw

PATCH 14/21 drm/amdgpu/gfx: add a shared gfx pipe reset helper

In reply to Jesse Zhang

Serialize GFX recovery with mec.reset_mutex and attempt MES suspend before locating a user HQD. Back up collateral kernel rings before reset, then resume MES before testing and replaying rebuilt rings.

Allow reset to proceed if suspend fails. Always attempt resume, report reset or resume errors, and leave schedulers stopped on failure.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 115 +++++++++++++++++++++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  16 ++++
 2 files changed, 129 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +0 -0
index b91bb5166ea8..2da839228911 100644
@@ -1704,7 +1704,6 @@ static ssize_t compute_partition_mem_alloc_mode_show(struct device *dev,
 			  amdgpu_gfx_compute_mem_alloc_mode_desc(mode));
 }
 
 
 static ssize_t compute_partition_mem_alloc_mode_store(struct device *dev,
 						struct device_attribute *addr,

const char *buf, size_t count)
@@ -2099,6 +2098,84 @@ static int amdgpu_gfx_mes_reset_queue_start(struct amdgpu_ring *ring,
return 0;
}

+/*
+ * Reset one GFX queue and preserve collateral kernel rings on its pipe.
+ * Exactly one of @ring / @queue is non-NULL.
+ * This helper takes the reset mutex; callers must not already hold it.
+ */
+int amdgpu_gfx_reset_mes_gfx(struct amdgpu_device *adev,

+			     struct amdgpu_ring *ring,
+			     struct amdgpu_fence *guilty_fence,
+			     struct amdgpu_usermode_queue *queue,
+			     unsigned int vmid, bool use_mmio)

+{
+ struct mes_reset_queue_input queue_input = { 0 };
+ struct amdgpu_gfx_pipe_reset_ctx ctx;
+ u32 me = U32_MAX, pipe = U32_MAX;
+ int i, r, resume_r;
+
+ mutex_lock(&adev->gfx.mec.reset_mutex);
+
+ /*
+ * Suspend before detecting the HQD to avoid resolving a slot MES remaps.
+ * A hung queue can prevent suspend; still try reset and always resume.
+ */
+ amdgpu_mes_suspend(adev, 0);
+
+ if (queue) {
+ u32 hw_queue_id;
+
+ queue_input.queue_type = queue->queue_type;
+ queue_input.doorbell_offset = queue->doorbell_index;
+ if (adev->gfx.funcs->detect_hung_queue(adev,
+ queue->doorbell_index, &me, &pipe,
+ &hw_queue_id)) {
+ queue_input.use_mmio = true;
+ queue_input.me_id = me;
+ queue_input.pipe_id = pipe;
+ queue_input.queue_id = hw_queue_id;
+ }
+ } else {
+ me = ring->me;
+ pipe = ring->pipe;
+ }
+
+ amdgpu_gfx_pipe_reset_prepare(adev, ring, me, pipe, &ctx);
+
+ if (ring)
+ r = amdgpu_gfx_mes_reset_queue_start(ring, vmid, guilty_fence,
+ use_mmio);
+ else
+ r = mes_userq_reset_hw(queue, &queue_input);
+
+ resume_r = amdgpu_mes_resume(adev, 0);
+ if (!r)
+ r = resume_r;
+
+ /* MES must run before ring tests or replay. */
+ if (!r && ring)
+ r = amdgpu_ring_reset_helper_end(ring, guilty_fence);
+
+ for (i = 0; !r && i < adev->gfx.num_gfx_rings; i++) {
+ struct amdgpu_ring *cring = &adev->gfx.gfx_ring[i];
+
+ /* MQD rebuild clears wptr; replay only rings rebuilt by pipe reset. */
+ if ((ctx.replay_mask & BIT(i)) && !cring->wptr)
+ r = amdgpu_ring_reset_helper_end(cring, NULL);
+ }
+
+ /* Leave stopped schedulers to GPU recovery if any part failed. */
+ if (!r) {
+ for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
+ if (ctx.sched_mask & BIT(i))
+ drm_sched_wqueue_start(&adev->gfx.gfx_ring[i].sched);
+ }
+ }
+
+ mutex_unlock(&adev->gfx.mec.reset_mutex);
+ return r;
+}
+
int amdgpu_gfx_mes_reset_queue(struct amdgpu_ring *ring,
unsigned int vmid,
struct amdgpu_fence *timedout_fence,
@@ -2299,6 +2376,41 @@ static void amdgpu_gfx_reset_stop_compute_scheds(struct amdgpu_device *adev,
}
}

+/* Caller holds reset_mutex and attempted MES suspend before HQD lookup. */ +void amdgpu_gfx_pipe_reset_prepare(struct amdgpu_device *adev,

+				   struct amdgpu_ring *guilty_ring,
+				   u32 me, u32 pipe,
+				   struct amdgpu_gfx_pipe_reset_ctx *ctx)

+{
+ struct amdgpu_ring *ring;
+ int i;
+
+ lockdep_assert_held(&adev->gfx.mec.reset_mutex);
+ ctx->sched_mask = 0;
+ ctx->replay_mask = 0;
+
+ for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
+ ring = &adev->gfx.gfx_ring[i];
+ /* An unresolved userq slot requires protecting every GFX pipe. */
+ if (ring == guilty_ring ||

+		    (me != U32_MAX && ring->me != me) ||
+		    (pipe != U32_MAX && ring->pipe != pipe) ||
+		    !amdgpu_ring_sched_ready(ring))

+ continue;
+
+ if (!drm_sched_is_stopped(&ring->sched)) {
+ drm_sched_wqueue_stop(&ring->sched);
+ ctx->sched_mask |= BIT(i);
+ }
+
+ if (amdgpu_fence_count_emitted(ring)) {
+ amdgpu_ring_reset_helper_begin(ring, NULL);
+ if (ring->ring_backup_entries_to_copy)
+ ctx->replay_mask |= BIT(i);
+ }
+ }
+}
+
/*
* Match the MES-reported hung doorbell against a compute ring and run
* the reset. On hit, the matched ring and its guilty fence are returned
@@ -3096,4 +3208,3 @@ int amdgpu_gfx_ring_preempt_ib(struct amdgpu_ring *ring)

return 0;
}
-

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +16 -0
index c948fd2130a0..855651f0c930 100644
@@ -700,6 +700,22 @@ int amdgpu_gfx_mes_reset_queue(struct amdgpu_ring *ring,
 			       struct amdgpu_fence *timedout_fence,
 			       bool use_mmio);
 void amdgpu_gfx_mqd_reset_restore(struct amdgpu_ring *ring);
+struct amdgpu_gfx_pipe_reset_ctx {
+	/* Schedulers stopped by this reset. */
+	unsigned long sched_mask;
+	/* Rings with backed-up commands. */
+	unsigned long replay_mask;
+};
+
+void amdgpu_gfx_pipe_reset_prepare(struct amdgpu_device *adev,
+				   struct amdgpu_ring *guilty_ring,
+				   u32 me, u32 pipe,
+				   struct amdgpu_gfx_pipe_reset_ctx *ctx);
+int amdgpu_gfx_reset_mes_gfx(struct amdgpu_device *adev,
+			     struct amdgpu_ring *ring,
+			     struct amdgpu_fence *guilty_fence,
+			     struct amdgpu_usermode_queue *queue,
+			     unsigned int vmid, bool use_mmio);
 
 static inline const char *amdgpu_gfx_compute_mode_desc(int mode)
 {
Signature · 1 line
2.49.0
#14 lore raw

PATCH 15/21 drm/amdgpu/gfx: add a gfx ME pipe reset capability helper

In reply to Jesse Zhang

Use gfx_supported_reset's PER_PIPE bit to report GFX pipe reset support through a shared helper.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 10 ++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +10 -0
index 2da839228911..7c597dab5b2a 100644
@@ -2376,6 +2376,16 @@ static void amdgpu_gfx_reset_stop_compute_scheds(struct amdgpu_device *adev,
 	}
 }
 
+bool amdgpu_gfx_me_pipe_reset_supported(struct amdgpu_device *adev)
+{
+	if (!!(adev->gfx.gfx_supported_reset & AMDGPU_RESET_TYPE_PER_PIPE))
+		return true;
+	else
+		dev_warn_once(adev->dev, "Please use the latest MEC version to see whether support pipe reset\n");
+
+	return false;
+}
+
 /* Caller holds reset_mutex and attempted MES suspend before HQD lookup. */
 void amdgpu_gfx_pipe_reset_prepare(struct amdgpu_device *adev,
 				   struct amdgpu_ring *guilty_ring,
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +1 -0
index 855651f0c930..5939e1fb3b24 100644
@@ -707,6 +707,7 @@ struct amdgpu_gfx_pipe_reset_ctx {
 	unsigned long replay_mask;
 };
 
+bool amdgpu_gfx_me_pipe_reset_supported(struct amdgpu_device *adev);
 void amdgpu_gfx_pipe_reset_prepare(struct amdgpu_device *adev,
 				   struct amdgpu_ring *guilty_ring,
 				   u32 me, u32 pipe,
Signature · 1 line
2.49.0
#15 lore raw

Re: [PATCH 15/21] drm/amdgpu/gfx: add a gfx ME pipe reset capability helper

Quoted 15 lines · Jesse Zhang

Use gfx_supported_reset's PER_PIPE bit to report GFX pipe reset support through a shared helper.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 10 ++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +0 -0
index 2da839228911..7c597dab5b2a 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c static void amdgpu_gfx_reset_stop_compute_scheds(struct amdgpu_device *adev, · line 2384

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +6 -0
@@ -2376,6 +2376,16 @@ static void amdgpu_gfx_reset_stop_compute_scheds(struct amdgpu_device *adev,
        }
 }
 
+bool amdgpu_gfx_me_pipe_reset_supported(struct amdgpu_device *adev)
+{
+       if (!!(adev->gfx.gfx_supported_reset & AMDGPU_RESET_TYPE_PER_PIPE))
+               return true;
+       else
+               dev_warn_once(adev->dev, "Please use the latest MEC version to see whether support pipe reset\n");

Should be ME rather than MEC?

Alex

Quoted 22 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +4 -0
+
+       return false;
+}
+
 /* Caller holds reset_mutex and attempted MES suspend before HQD lookup. */
 void amdgpu_gfx_pipe_reset_prepare(struct amdgpu_device *adev,
                                   struct amdgpu_ring *guilty_ring,
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +1 -1
index 855651f0c930..5939e1fb3b24 100644
@@ -707,6 +707,7 @@ struct amdgpu_gfx_pipe_reset_ctx {
        unsigned long replay_mask;
 };
 
+bool amdgpu_gfx_me_pipe_reset_supported(struct amdgpu_device *adev);
 void amdgpu_gfx_pipe_reset_prepare(struct amdgpu_device *adev,
                                   struct amdgpu_ring *guilty_ring,
                                   u32 me, u32 pipe,
--

2.49.0

#16 lore raw

PATCH 16/21 drm/amdgpu/gfx: preserve collateral rings during gfx pipe reset

In reply to Jesse Zhang

Route kernel GFX resets through amdgpu_gfx_reset_mes_gfx() when PER_PIPE is supported, so collateral kernel rings are preserved across a pipe reset. Keep the existing queue reset path otherwise.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +11 -5
index 7c597dab5b2a..94d45d7960e2 100644
@@ -2181,13 +2181,19 @@ int amdgpu_gfx_mes_reset_queue(struct amdgpu_ring *ring,
 			       struct amdgpu_fence *timedout_fence,
 			       bool use_mmio)
 {
+	struct amdgpu_device *adev = ring->adev;
 	int r;
 
-	r = amdgpu_gfx_mes_reset_queue_start(ring, vmid, timedout_fence,
-					      use_mmio);
-	if (r)
-		return r;
-	return amdgpu_ring_reset_helper_end(ring, timedout_fence);
+	if (!amdgpu_gfx_me_pipe_reset_supported(adev)) {
+		r = amdgpu_gfx_mes_reset_queue_start(ring, vmid, timedout_fence,
+						     use_mmio);
+		if (r)
+			return r;
+		return amdgpu_ring_reset_helper_end(ring, timedout_fence);
+	}
+
+	return amdgpu_gfx_reset_mes_gfx(adev, ring, timedout_fence, NULL, vmid,
+				      use_mmio);
 }
 
 static DEVICE_ATTR(run_cleaner_shader, 0200,
Signature · 1 line
2.49.0
#17 lore raw

PATCH 17/21 drm/amdgpu/userq: preserve kernel rings during gfx pipe reset

In reply to Jesse Zhang

Route GFX user queue resets through amdgpu_gfx_reset_mes_gfx() when HQD detection and PER_PIPE are available, preserving collateral kernel rings across a pipe reset.

If the HQD cannot be located, use MES packet reset and protect kernel rings on all GFX pipes.

---
 drivers/gpu/drm/amd/amdgpu/mes_userqueue.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/amd/amdgpu/mes_userqueue.c +6 -0
index 948813042e98..1b06a4618125 100644
@@ -240,6 +240,12 @@ int mes_userq_reset(struct amdgpu_usermode_queue *queue)
 	if (queue->state == AMDGPU_USERQ_STATE_HUNG)
 		return 0;
 
+	/* A GFX pipe reset also affects the kernel rings on that pipe. */
+	if (queue->queue_type == AMDGPU_HW_IP_GFX &&
+	    adev->gfx.funcs->detect_hung_queue &&
+	    amdgpu_gfx_me_pipe_reset_supported(adev))
+		return amdgpu_gfx_reset_mes_gfx(adev, NULL, NULL, queue, 0, true);
+
 	memset(&queue_input, 0x0, sizeof(struct mes_reset_queue_input));
 	queue_input.doorbell_offset = queue->doorbell_index;
 	queue_input.queue_type = queue->queue_type;
Signature · 1 line
2.49.0
#18 lore raw

PATCH 18/21 drm/amdgpu/mes11: gate gfx pipe reset on PER_PIPE

In reply to Jesse Zhang

Replace the disabled pipe-reset stub with the shared PER_PIPE capability check, so GFX MMIO pipe reset follows gfx_supported_reset.

---
 drivers/gpu/drm/amd/amdgpu/mes_v11_0.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v11_0.c +1 -8
index a019b1926305..c98e2fcf8b70 100644
@@ -404,13 +404,6 @@ static int mes_v11_0_remove_hw_queue(struct amdgpu_mes *mes,
 			offsetof(union MESAPI__REMOVE_QUEUE, api_status));
 }
 
-static bool mes_v11_0_pipe_reset_support(struct amdgpu_device *adev)
-{
-	/* Disable the pipe reset until the CPFW fully support it.*/
-	dev_warn_once(adev->dev, "The CPFW hasn't support pipe reset yet.\n");
-	return false;
-}
-
 /* Use MES callbacks directly while holding the MES lock. */
 static int mes_v11_0_restore_gfx_kernel_queue(struct amdgpu_device *adev,
 					      u32 me, u32 pipe, u32 guilty_queue,
@@ -480,7 +473,7 @@ static int mes_v11_0_reset_gfx_pipe_mmio(struct amdgpu_device *adev,
 	uint32_t reset_pipe, clean_pipe;
 	int i;
 
-	if (!mes_v11_0_pipe_reset_support(adev))
+	if (!amdgpu_gfx_me_pipe_reset_supported(adev))
 		return -EOPNOTSUPP;
 	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
 	mutex_lock(&adev->srbm_mutex);
Signature · 1 line
2.49.0
#19 lore raw

PATCH 19/21 drm/amdgpu/mes12: gate gfx pipe reset on PER_PIPE

In reply to Jesse Zhang

Replace the disabled pipe-reset stub with the shared PER_PIPE capability check, so GFX MMIO pipe reset follows gfx_supported_reset.

---
 drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +1 -8
index ead276503522..cc2e343903e9 100644
@@ -418,13 +418,6 @@ int gfx_v12_0_request_gfx_index_mutex(struct amdgpu_device *adev,
 	return 0;
 }
 
-static bool mes_v12_0_pipe_reset_support(struct amdgpu_device *adev)
-{
-	/* Disable the pipe reset until the CPFW fully support it.*/
-	dev_warn_once(adev->dev, "The CPFW hasn't support pipe reset yet.\n");
-	return false;
-}
-
 /* Use MES callbacks directly while holding the MES lock. */
 static int mes_v12_0_restore_gfx_kernel_queue(struct amdgpu_device *adev,
 					      u32 me, u32 pipe, u32 guilty_queue,
@@ -493,7 +486,7 @@ static int mes_v12_0_reset_gfx_pipe_mmio(struct amdgpu_device *adev,
 	uint32_t reset_pipe, clean_pipe;
 	int i;
 
-	if (!mes_v12_0_pipe_reset_support(adev))
+	if (!amdgpu_gfx_me_pipe_reset_supported(adev))
 		return -EOPNOTSUPP;
 
 	amdgpu_gfx_rlc_enter_safe_mode(adev, 0);
Signature · 1 line
2.49.0
#20 lore raw

PATCH 20/21 drm/amdgpu/gfx11: defer gfx pipe reset enablement

In reply to Jesse Zhang

Keep PER_PIPE disabled until the minimum CP firmware version for warm reset is confirmed.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v11_0.c +2 -0
index 5ed70bbf5313..70a7b890e70d 100644
@@ -1921,6 +1921,8 @@ static int gfx_v11_0_sw_init(struct amdgpu_ip_block *ip_block)
 		    !adev->debug_disable_gpu_ring_reset) {
 			adev->gfx.compute_supported_reset |= AMDGPU_RESET_TYPE_PER_QUEUE;
 			adev->gfx.gfx_supported_reset |= AMDGPU_RESET_TYPE_PER_QUEUE;
+			/* TODO: verify CP warm-reset support before enabling PER_PIPE. */
+			/* adev->gfx.gfx_supported_reset |= AMDGPU_RESET_TYPE_PER_PIPE; */
 		}
 		break;
 	default:
Signature · 1 line
2.49.0
#21 lore raw

PATCH 21/21 drm/amdgpu/gfx12: defer gfx pipe reset enablement

In reply to Jesse Zhang

Keep PER_PIPE disabled until the minimum CP firmware version for warm reset is confirmed.

---
 drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/gfx_v12_0.c +2 -0
index c559b355eb49..1b9a4e7ea833 100644
@@ -1575,6 +1575,8 @@ static int gfx_v12_0_sw_init(struct amdgpu_ip_block *ip_block)
 		    !adev->debug_disable_gpu_ring_reset) {
 			adev->gfx.compute_supported_reset |= AMDGPU_RESET_TYPE_PER_QUEUE;
 			adev->gfx.gfx_supported_reset |= AMDGPU_RESET_TYPE_PER_QUEUE;
+			/* TODO: verify CP warm-reset support before enabling PER_PIPE. */
+			/* adev->gfx.gfx_supported_reset |= AMDGPU_RESET_TYPE_PER_PIPE; */
 		}
 		break;
 	default:
Signature · 1 line
2.49.0
Reviewed-by on v1 1/21
#22 lore raw
In reply to Jesse Zhang

Patches 11 and 12 look like bug fixes. Maybe move them to the start of the series? One small comment on patch 15. Other than that, the series is:

Quoted 26 lines · Jesse Zhang

Allow GFX recovery to resolve a doorbell to an HQD (ME, pipe, queue) without querying a stalled MES scheduler.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h | 3 +++
 1 file changed, 3 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +3 -1
index f5fb2e585e8a..a158b7f71b2f 100644
@@ -373,6 +373,9 @@ struct amdgpu_gfx_funcs {
        int (*get_xccs_per_xcp)(struct amdgpu_device *adev);
        void (*get_hdp_flush_mask)(struct amdgpu_ring *ring,
                                uint32_t *ref_and_mask, uint32_t *reg_mem_engine);
+       /* find the gfx HQD slot (me/pipe/queue) for a doorbell */
+       bool (*detect_hung_queue)(struct amdgpu_device *adev, u32 doorbell_index,
+                                 u32 *me, u32 *pipe, u32 *queue);
 };
 
 struct sq_work {
--

2.49.0