Skip to contentSkip to conversation

Rework GPU TLB invalidation

PATCH Superseded Alex Deucher 2 participants 32 messages Aug 24 – Aug 25

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Alex Deucher

author maintainer
#0 lore raw

cover Rework GPU TLB invalidation

GMC 9-12 use KIQ or MES for TLB invalidations to avoid using MMIO which would require disallowing GFXOFF. KIQ and MES are management queues however and if they hang, they cannot be recovered by a queue reset since they are the mechanisms which handle queue resets. Since using KIQ or MES will exit GFXOFF anyway, explicitly disallow it on the MMIO path and use that. Next, switch to using SDMA for TLB invalidations. SDMA 4.4.x and newer have special packets specifically for this purpose. If SDMA hangs while doing the invalidation for some reason, it's easier to reset the SDMA queue than KIQ or MES. Finally, most of the TLB invalidation code between GMC 9 through 12 was identical, so move it to common GMC helpers and remove the IP specific code. If the SMDA and MMIO pathes prove to be stable, the KIQ pathes can be removed in the future to further simplify things. SDMA 4.x could also be updated to support PASID invalidation via SDMA using either the new packet (SDMA 4.4.x) or via REG_WRITE/REG_WAIT packets (SDMA 4.0.x).

Alex Deucher (30):
drm/amdgpu/gmc9: disallow gfxoff around TLB flushes drm/amdgpu/gmc10: disallow gfxoff around TLB flushes drm/amdgpu/gmc11: disallow gfxoff around TLB flushes drm/amdgpu/gmc12: disallow gfxoff around TLB flushes drm/amdgpu: add a gmc flag for using MMIO for TLB flush drm/amdgpu/gmc9: use MMIO for TLB flushes drm/amdgpu/gmc10: use MMIO for TLB flushes drm/amdgpu/gmc11: use MMIO for TLB flushes drm/amdgpu/gmc12: use MMIO for TLB flushes
drm/amdgpu: add a buffer funcs callback for TLB invalidation drm/amdgpu/sdma5.0: add tlb invalidation buffer func callback drm/amdgpu/sdma5.2: add tlb invalidation buffer func callback drm/amdgpu/sdma6: add tlb invalidation buffer func callback drm/amdgpu/sdma7: add tlb invalidation buffer func callback drm/amdgpu: simplify amdgpu_gmc_flush_gpu_tlb() drm/amdgpu: add tlb invalidation method enum
drm/amdgpu: plumb tlb inv method in amdgpu_gmc_flush_gpu_tlb_gart() drm/amdgpu: uplevel reset check in amdgpu_gmc_flush_gpu_tlb_gart() drm/amdgpu/gmc: add new callback to lookup vmid to pasid mapping drm/amdgpu: add a gmc callback for the inv semaphore drm/amdgpu/gmc: rework pasid flushing
drm/amdgpu/gmc9: use SDMA for gart TLB invalidation drm/amdgpu/gmc10: use SDMA for gart TLB invalidation drm/amdgpu/gmc11: use SDMA for gart TLB invalidation drm/amdgpu/gmc12: use SDMA for gart TLB invalidation drm/amdgpu/gmc10: use SDMA for pasid TLB invalidation drm/amdgpu/gmc11: use SDMA for pasid TLB invalidation drm/amdgpu/gmc12: use MES or SDMA for pasid TLB invalidation drm/amdgpu/gmc12: drop MES tlb inv in gmc callbacks drm/amdgpu/gmc: add helpers for various tlb inv functions

 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c  | 486 +++++++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h  |  33 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_sdma.h |  18 +
 drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c   |   2 +
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c   | 202 +---------
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c   | 207 +---------
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c   | 250 ++----------
 drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c   | 225 +----------
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c    | 114 +-----
 drivers/gpu/drm/amd/amdgpu/mes_v12_0.c   |   4 +
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c   |   4 +
 drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c   |  49 +++
 drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c   |  49 +++
 drivers/gpu/drm/amd/amdgpu/sdma_v6_0.c   |  49 +++
 drivers/gpu/drm/amd/amdgpu/sdma_v7_0.c   |  48 +++
 16 files changed, 747 insertions(+), 995 deletions(-)
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 1/30 drm/amdgpu/gmc9: disallow gfxoff around TLB flushes

We need to disallow gfxoff if we touch GC MMIO registers. At the moment we use KIQ or MES for TLB flushes so no intended functional change.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +7 -0
index b46b87291c512..80f1cf1f21736 100644
@@ -808,6 +808,10 @@ static void gmc_v9_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 		return;
 	}
 
+	/* disabllow gfxoff when we invalidate */
+	if (vmhub < AMDGPU_MMHUB0(0))
+		amdgpu_gfx_off_ctrl(adev, false);
+
 	/* This path is needed before KIQ/MES/GFXOFF are set up */
 	spin_lock(&adev->gmc.invalidate_lock);
 
@@ -873,6 +877,9 @@ static void gmc_v9_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 
 	spin_unlock(&adev->gmc.invalidate_lock);
 
+	if (vmhub < AMDGPU_MMHUB0(0))
+		amdgpu_gfx_off_ctrl(adev, true);
+
 	if (j < adev->usec_timeout)
 		return;
 
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

PATCH 2/30 drm/amdgpu/gmc10: disallow gfxoff around TLB flushes

In reply to Alex Deucher

We need to disallow gfxoff if we touch GC MMIO registers. At the moment we use KIQ or MES for TLB flushes so no intended functional change.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +7 -0
index 75a552782c979..8d88651b18ef9 100644
@@ -265,6 +265,10 @@ static void gmc_v10_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 	/* This path is needed before KIQ/MES/GFXOFF are set up */
 	hub_ip = (vmhub == AMDGPU_GFXHUB(0)) ? GC_HWIP : MMHUB_HWIP;
 
+	/* disabllow gfxoff when we invalidate */
+	if (hub_ip == GC_HWIP)
+		amdgpu_gfx_off_ctrl(adev, false);
+
 	spin_lock(&adev->gmc.invalidate_lock);
 	/*
 	 * It may lose gpuvm invalidate acknowldege state across power-gating
@@ -313,6 +317,9 @@ static void gmc_v10_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 
 	spin_unlock(&adev->gmc.invalidate_lock);
 
+	if (hub_ip == GC_HWIP)
+		amdgpu_gfx_off_ctrl(adev, true);
+
 	if (i >= adev->usec_timeout)
 		dev_err(adev->dev, "Timeout waiting for VM flush hub: %d!\n",
 			vmhub);
Signature · 1 line
2.55.0
author maintainer
#3 lore raw

PATCH 3/30 drm/amdgpu/gmc11: disallow gfxoff around TLB flushes

In reply to Alex Deucher

We need to disallow gfxoff if we touch GC MMIO registers. At the moment we use KIQ or MES for TLB flushes so no intended functional change.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c +7 -0
index f454aff831b03..41ebc6ae182cc 100644
@@ -253,6 +253,10 @@ static void gmc_v11_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 	/* This path is needed before KIQ/MES/GFXOFF are set up */
 	hub_ip = (vmhub == AMDGPU_GFXHUB(0)) ? GC_HWIP : MMHUB_HWIP;
 
+	/* disabllow gfxoff when we invalidate */
+	if (hub_ip == GC_HWIP)
+		amdgpu_gfx_off_ctrl(adev, false);
+
 	spin_lock(&adev->gmc.invalidate_lock);
 	/*
 	 * It may lose gpuvm invalidate acknowldege state across power-gating
@@ -306,6 +310,9 @@ static void gmc_v11_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 
 	spin_unlock(&adev->gmc.invalidate_lock);
 
+	if (hub_ip == GC_HWIP)
+		amdgpu_gfx_off_ctrl(adev, true);
+
 	if (i >= adev->usec_timeout)
 		dev_err(adev->dev, "Timeout waiting for VM flush ACK!\n");
 }
Signature · 1 line
2.55.0
author maintainer
#4 lore raw

PATCH 4/30 drm/amdgpu/gmc12: disallow gfxoff around TLB flushes

In reply to Alex Deucher

We need to disallow gfxoff if we touch GC MMIO registers. At the moment we use KIQ or MES for TLB flushes so no intended functional change.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c +7 -1
index 2c7e3eae7b237..3b7764b94de19 100644
@@ -327,8 +327,14 @@ static void gmc_v12_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 		return;
 	}
 
+	/* disabllow gfxoff when we invalidate */
+	if (vmhub == AMDGPU_GFXHUB(0))
+		amdgpu_gfx_off_ctrl(adev, false);
+
 	gmc_v12_0_flush_vm_hub(adev, vmid, vmhub, 0);
-	return;
+
+	if (vmhub == AMDGPU_GFXHUB(0))
+		amdgpu_gfx_off_ctrl(adev, true);
 }
 
 /**
Signature · 1 line
2.55.0
author maintainer
#5 lore raw

PATCH 5/30 drm/amdgpu: add a gmc flag for using MMIO for TLB flush

In reply to Alex Deucher

No intended functional change.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h | 2 ++
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c  | 5 ++++-
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c  | 5 ++++-
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c  | 5 ++++-
 drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c  | 6 +++---
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c   | 5 ++++-
 6 files changed, 21 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h +2 -0
index c0884797dd544..f8f4df37d5805 100644
@@ -367,6 +367,8 @@ struct amdgpu_gmc {
 	bool flush_pasid_uses_kiq;
 
 	bool override_pte;
+
+	bool use_mmio_for_tlb_flush;
 };
 
 #define amdgpu_gmc_emit_flush_gpu_tlb(r, vmid, addr) (r)->adev->gmc.gmc_funcs->emit_flush_gpu_tlb((r), (vmid), (addr))
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +4 -1
index 8d88651b18ef9..7ea1c4b878e97 100644
@@ -256,7 +256,7 @@ static void gmc_v10_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 	 * properly under bare metal
 	 */
 	if (adev->gfx.kiq[0].ring.sched.ready && !adev->enable_mes &&
-	    (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev))) {
+	    !adev->gmc.use_mmio_for_tlb_flush) {
 		amdgpu_gmc_fw_reg_write_reg_wait(adev, req, ack, inv_req,
 						 1 << vmid, GET_INST(GC, 0));
 		return;
@@ -883,6 +883,9 @@ static int gmc_v10_0_sw_init(struct amdgpu_ip_block *ip_block)
 	if (r)
 		return r;
 
+	adev->gmc.use_mmio_for_tlb_flush =
+		!(amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev));
+
 	return 0;
 }
 
drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c +4 -1
index 41ebc6ae182cc..fc730504be5f0 100644
@@ -244,7 +244,7 @@ static void gmc_v11_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 	 * properly under bare metal
 	 */
 	if ((adev->gfx.kiq[0].ring.sched.ready || adev->mes.ring[0].sched.ready) &&
-	    (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev))) {
+	    !adev->gmc.use_mmio_for_tlb_flush) {
 		amdgpu_gmc_fw_reg_write_reg_wait(adev, req, ack, inv_req,
 						 1 << vmid, GET_INST(GC, 0));
 		return;
@@ -865,6 +865,9 @@ static int gmc_v11_0_sw_init(struct amdgpu_ip_block *ip_block)
 	if (r)
 		return r;
 
+	adev->gmc.use_mmio_for_tlb_flush =
+		!(amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev));
+
 	return 0;
 }
 
drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c +4 -1
index 3b7764b94de19..3b1fe15fa1c01 100644
@@ -315,7 +315,7 @@ static void gmc_v12_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 	 * properly under bare metal
 	 */
 	if ((adev->gfx.kiq[0].ring.sched.ready || adev->mes.ring[0].sched.ready) &&
-	    (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev))) {
+	    !adev->gmc.use_mmio_for_tlb_flush) {
 		struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
 		const unsigned eng = 17;
 		u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
@@ -968,6 +968,9 @@ static int gmc_v12_0_sw_init(struct amdgpu_ip_block *ip_block)
 	if (r)
 		return r;
 
+	adev->gmc.use_mmio_for_tlb_flush =
+		!(amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev));
+
 	return 0;
 }
 
drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c +3 -3
index 5fe43f7eab29d..6c0d2689cc05d 100644
@@ -375,9 +375,9 @@ static void gmc_v12_1_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 	/* This is necessary for SRIOV as well as for GFXOFF to function
 	 * properly under bare metal
 	 */
-	if (((adev->gfx.kiq[inst].ring.sched.ready ||
-	      adev->mes.ring[MES_PIPE_INST(inst, 0)].sched.ready) &&
-	    (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev)))) {
+	if ((adev->gfx.kiq[inst].ring.sched.ready ||
+	     adev->mes.ring[MES_PIPE_INST(inst, 0)].sched.ready) &&
+	    !adev->gmc.use_mmio_for_tlb_flush) {
 		struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
 		const unsigned eng = 17;
 		u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +4 -1
index 80f1cf1f21736..1ff1d2ea02b3b 100644
@@ -799,7 +799,7 @@ static void gmc_v9_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 	 * properly under bare metal
 	 */
 	if (adev->gfx.kiq[inst].ring.sched.ready &&
-	    (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev))) {
+	    !adev->gmc.use_mmio_for_tlb_flush) {
 		uint32_t req = hub->vm_inv_eng0_req + hub->eng_distance * eng;
 		uint32_t ack = hub->vm_inv_eng0_ack + hub->eng_distance * eng;
 
@@ -2056,6 +2056,9 @@ static int gmc_v9_0_sw_init(struct amdgpu_ip_block *ip_block)
 	if (amdgpu_is_multi_aid(adev))
 		amdgpu_gmc_sysfs_init(adev);
 
+	adev->gmc.use_mmio_for_tlb_flush =
+		!(amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev));
+
 	return 0;
 }
 
Signature · 1 line
2.55.0
author maintainer
#6 lore raw

PATCH 6/30 drm/amdgpu/gmc9: use MMIO for TLB flushes

In reply to Alex Deucher

KIQ handles scheduling on GC 9 so it can get delayed if there are a lot of queues active.

KIQ was used to avoid disallowing GFXOFF, but since the KIQ will wake GFX anyway, just disallow it when we flush and use MMIO on vega20 and older.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +2 -1
index 1ff1d2ea02b3b..8edaa54fca82f 100644
@@ -2057,7 +2057,8 @@ static int gmc_v9_0_sw_init(struct amdgpu_ip_block *ip_block)
 		amdgpu_gmc_sysfs_init(adev);
 
 	adev->gmc.use_mmio_for_tlb_flush =
-		!(amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev));
+		!amdgpu_sriov_runtime(adev) &&
+		(amdgpu_ip_version(adev, GC_HWIP, 0) <= IP_VERSION(9, 4, 0));
 
 	return 0;
 }
Signature · 1 line
2.55.0
author maintainer
#7 lore raw

PATCH 7/30 drm/amdgpu/gmc10: use MMIO for TLB flushes

In reply to Alex Deucher

KIQ handles scheduling on GC 10 so it can get delayed if there are a lot of queues active.

KIQ was used to avoid disallowing GFXOFF, but since the KIQ will wake GFX anyway, just disallow it when we flush and use MMIO.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +1 -1
index 7ea1c4b878e97..23d9fe995a2b5 100644
@@ -884,7 +884,7 @@ static int gmc_v10_0_sw_init(struct amdgpu_ip_block *ip_block)
 		return r;
 
 	adev->gmc.use_mmio_for_tlb_flush =
-		!(amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev));
+		!amdgpu_sriov_runtime(adev);
 
 	return 0;
 }
Signature · 1 line
2.55.0
author maintainer
#8 lore raw

PATCH 8/30 drm/amdgpu/gmc11: use MMIO for TLB flushes

In reply to Alex Deucher

MES only has one pipe on GC 11 which handles scheduling so it can get delayed if there are a lot of queues active.

MES was used to avoid disallowing GFXOFF, but since the MES will wake GFX anyway, just disallow it when we flush and use MMIO.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c +1 -1
index fc730504be5f0..098e1340554c5 100644
@@ -866,7 +866,7 @@ static int gmc_v11_0_sw_init(struct amdgpu_ip_block *ip_block)
 		return r;
 
 	adev->gmc.use_mmio_for_tlb_flush =
-		!(amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev));
+		!amdgpu_sriov_runtime(adev);
 
 	return 0;
 }
Signature · 1 line
2.55.0
author maintainer
#9 lore raw

PATCH 9/30 drm/amdgpu/gmc12: use MMIO for TLB flushes

In reply to Alex Deucher

MES has a dedicated pipe for these sort of tasks on GC12 so it shouldn't be an issue.

MES is used to avoid disallowing GFXOFF, but since the MES will wake GFX anyway to execute the flush, just disallow it when we flush and use MMIO for consistency with other chips.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c +12 -2
index 3b1fe15fa1c01..cffc818880d12 100644
@@ -968,8 +968,18 @@ static int gmc_v12_0_sw_init(struct amdgpu_ip_block *ip_block)
 	if (r)
 		return r;
 
-	adev->gmc.use_mmio_for_tlb_flush =
-		!(amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev));
+	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
+	case IP_VERSION(12, 0, 0):
+	case IP_VERSION(12, 0, 1):
+	default:
+		adev->gmc.use_mmio_for_tlb_flush =
+			!amdgpu_sriov_runtime(adev);
+		break;
+	case IP_VERSION(12, 1, 0):
+		adev->gmc.use_mmio_for_tlb_flush =
+			!(amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev));
+		break;
+	}
 
 	return 0;
 }
Signature · 1 line
2.55.0
author maintainer
#10 lore raw

PATCH 10/30 drm/amdgpu: add a buffer funcs callback for TLB invalidation

In reply to Alex Deucher

Use this interface to issue TLB invalidations using SDMA.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_sdma.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_sdma.h +18 -0
index 4f4e56022c970..4ab92d287675a 100644
@@ -155,6 +155,23 @@ struct amdgpu_buffer_funcs {
 				 uint64_t dst_offset,
 				 /* number of byte to fill */
 				 uint32_t byte_count);
+
+	/* number of dw to reserve per operation */
+	unsigned	tlb_inv_num_dw;
+
+	/* used for buffer clearing */
+	void (*emit_tlb_inv)(struct amdgpu_device *adev,
+			     struct amdgpu_ib *ib,
+			     /* vmid to target */
+			     unsigned int vmid,
+			     /* vmhub to target */
+			     u32 vmhub,
+			     /* inv eng to target */
+			     u32 eng,
+			     /* flush type */
+			     u32 flush_type,
+			     /* XCC to target */
+			     u32 xcc_inst);
 };
 
 int amdgpu_sdma_reset_engine(struct amdgpu_device *adev, uint32_t instance_id,
@@ -162,6 +179,7 @@ int amdgpu_sdma_reset_engine(struct amdgpu_device *adev, uint32_t instance_id,
 
 #define amdgpu_emit_copy_buffer(adev, ib, s, d, b, t) (adev)->mman.buffer_funcs->emit_copy_buffer((ib),  (s), (d), (b), (t))
 #define amdgpu_emit_fill_buffer(adev, ib, s, d, b) (adev)->mman.buffer_funcs->emit_fill_buffer((ib), (s), (d), (b))
+#define amdgpu_emit_tlb_inv(adev, ib, v, h, e, f, x) (adev)->mman.buffer_funcs->emit_tlb_inv((adev), (ib), (v), (h), (e), (f), (x))
 
 struct amdgpu_sdma_instance *
 amdgpu_sdma_get_instance_from_ring(struct amdgpu_ring *ring);
Signature · 1 line
2.55.0
author maintainer
#11 lore raw

PATCH 11/30 drm/amdgpu/sdma5.0: add tlb invalidation buffer func callback

In reply to Alex Deucher

Will be used for TLB invalidation.

---
 drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c | 49 ++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c +49 -0
index 97fee70dc2f64..8cb579ecf522e 100644
@@ -2030,6 +2030,52 @@ static void sdma_v5_0_emit_fill_buffer(struct amdgpu_ib *ib,
 	ib->ptr[ib->length_dw++] = byte_count - 1;
 }
 
+/**
+ * sdma_v5_0_emit_tlb_inv - Invalidate TLB using the sDMA engine
+ *
+ * @adev: amdgpu device structure
+ * @ib: indirect buffer to fill
+ * @vmid: vmid to target
+ * @vmhub: vmhub to target
+ * @eng: invalidation engine to use
+ * @flush_type: type of flush (lightweight, heavyweight)
+ * @xcc_inst: XCC to target
+ *
+ * Invalidate TLB using the DMA engine.
+ */
+static void sdma_v5_0_emit_tlb_inv(struct amdgpu_device *adev,
+				   struct amdgpu_ib *ib,
+				   unsigned int vmid,
+				   u32 vmhub,
+				   u32 eng,
+				   u32 flush_type,
+				   u32 xcc_inst)
+{
+	struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
+	u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
+	u32 mmhub_eng, gfxhub_eng;
+
+	if (AMDGPU_IS_GFXHUB(vmhub)) {
+		mmhub_eng = 0x1f;
+		gfxhub_eng = eng;
+	} else {
+		mmhub_eng = eng;
+		gfxhub_eng = 0x1f;
+	}
+
+	/* Trigger invalidation. */
+	ib->ptr[ib->length_dw++] =
+		(SDMA_PKT_VM_INVALIDATION_HEADER_OP(SDMA_OP_POLL_REGMEM) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_SUB_OP(SDMA_SUBOP_VM_INVALIDATION) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_GFX_ENG_ID(gfxhub_eng) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_MM_ENG_ID(mmhub_eng));
+	ib->ptr[ib->length_dw++] = inv_req;
+	ib->ptr[ib->length_dw++] = 0xFFFFFFFF;
+	ib->ptr[ib->length_dw++] =
+		(SDMA_PKT_VM_INVALIDATION_ADDRESSRANGEHI_INVALIDATEACK(1 << vmid) |
+		 SDMA_PKT_VM_INVALIDATION_ADDRESSRANGEHI_ADDRESSRANGEHI(0x1F));
+}
+
 static const struct amdgpu_buffer_funcs sdma_v5_0_buffer_funcs = {
 	.copy_max_bytes = 0x400000,
 	.copy_num_dw = 7,
@@ -2038,6 +2084,9 @@ static const struct amdgpu_buffer_funcs sdma_v5_0_buffer_funcs = {
 	.fill_max_bytes = 0x400000,
 	.fill_num_dw = 5,
 	.emit_fill_buffer = sdma_v5_0_emit_fill_buffer,
+
+	.tlb_inv_num_dw = 4,
+	.emit_tlb_inv = sdma_v5_0_emit_tlb_inv,
 };
 
 static void sdma_v5_0_set_buffer_funcs(struct amdgpu_device *adev)
Signature · 1 line
2.55.0
author maintainer
#12 lore raw

PATCH 12/30 drm/amdgpu/sdma5.2: add tlb invalidation buffer func callback

In reply to Alex Deucher

Will be used for TLB invalidation.

---
 drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c | 49 ++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c +49 -0
index 35cdf6c149f86..a10086de35957 100644
@@ -2038,6 +2038,52 @@ static void sdma_v5_2_emit_fill_buffer(struct amdgpu_ib *ib,
 	ib->ptr[ib->length_dw++] = byte_count - 1;
 }
 
+/**
+ * sdma_v5_2_emit_tlb_inv - Invalidate TLB using the sDMA engine
+ *
+ * @adev: amdgpu device structure
+ * @ib: indirect buffer to fill
+ * @vmid: vmid to target
+ * @vmhub: vmhub to target
+ * @eng: invalidation engine to use
+ * @flush_type: type of flush (lightweight, heavyweight)
+ * @xcc_inst: XCC to target
+ *
+ * Invalidate TLB using the DMA engine.
+ */
+static void sdma_v5_2_emit_tlb_inv(struct amdgpu_device *adev,
+				   struct amdgpu_ib *ib,
+				   unsigned int vmid,
+				   u32 vmhub,
+				   u32 eng,
+				   u32 flush_type,
+				   u32 xcc_inst)
+{
+	struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
+	u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
+	u32 mmhub_eng, gfxhub_eng;
+
+	if (AMDGPU_IS_GFXHUB(vmhub)) {
+		mmhub_eng = 0x1f;
+		gfxhub_eng = eng;
+	} else {
+		mmhub_eng = eng;
+		gfxhub_eng = 0x1f;
+	}
+
+	/* Trigger invalidation. */
+	ib->ptr[ib->length_dw++] =
+		(SDMA_PKT_VM_INVALIDATION_HEADER_OP(SDMA_OP_POLL_REGMEM) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_SUB_OP(SDMA_SUBOP_VM_INVALIDATION) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_GFX_ENG_ID(gfxhub_eng) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_MM_ENG_ID(mmhub_eng));
+	ib->ptr[ib->length_dw++] = inv_req;
+	ib->ptr[ib->length_dw++] = 0xFFFFFFFF;
+	ib->ptr[ib->length_dw++] =
+		(SDMA_PKT_VM_INVALIDATION_ADDRESSRANGEHI_INVALIDATEACK(1 << vmid) |
+		 SDMA_PKT_VM_INVALIDATION_ADDRESSRANGEHI_ADDRESSRANGEHI(0x1F));
+}
+
 static const struct amdgpu_buffer_funcs sdma_v5_2_buffer_funcs = {
 	.copy_max_bytes = 1 << 30,
 	.copy_num_dw = 7,
@@ -2046,6 +2092,9 @@ static const struct amdgpu_buffer_funcs sdma_v5_2_buffer_funcs = {
 	.fill_max_bytes = 1 << 30, /* HW supports 1 << 30, but PAL uses 1 << 22 */
 	.fill_num_dw = 5,
 	.emit_fill_buffer = sdma_v5_2_emit_fill_buffer,
+
+	.tlb_inv_num_dw = 4,
+	.emit_tlb_inv = sdma_v5_2_emit_tlb_inv,
 };
 
 static void sdma_v5_2_set_buffer_funcs(struct amdgpu_device *adev)
Signature · 1 line
2.55.0
author maintainer
#13 lore raw

PATCH 13/30 drm/amdgpu/sdma6: add tlb invalidation buffer func callback

In reply to Alex Deucher

Will be used for TLB invalidation.

---
 drivers/gpu/drm/amd/amdgpu/sdma_v6_0.c | 49 ++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
drivers/gpu/drm/amd/amdgpu/sdma_v6_0.c +49 -0
index 303fd7d1b7c88..7b84c996e2359 100644
@@ -1846,6 +1846,52 @@ static void sdma_v6_0_emit_fill_buffer(struct amdgpu_ib *ib,
 	ib->ptr[ib->length_dw++] = byte_count - 1;
 }
 
+/**
+ * sdma_v6_0_emit_tlb_inv - Invalidate TLB using the sDMA engine
+ *
+ * @adev: amdgpu device structure
+ * @ib: indirect buffer to fill
+ * @vmid: vmid to target
+ * @vmhub: vmhub to target
+ * @eng: invalidation engine to use
+ * @flush_type: type of flush (lightweight, heavyweight)
+ * @xcc_inst: XCC to target
+ *
+ * Invalidate TLB using the DMA engine.
+ */
+static void sdma_v6_0_emit_tlb_inv(struct amdgpu_device *adev,
+				   struct amdgpu_ib *ib,
+				   unsigned int vmid,
+				   u32 vmhub,
+				   u32 eng,
+				   u32 flush_type,
+				   u32 xcc_inst)
+{
+	struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
+	u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
+	u32 mmhub_eng, gfxhub_eng;
+
+	if (AMDGPU_IS_GFXHUB(vmhub)) {
+		mmhub_eng = 0x1f;
+		gfxhub_eng = eng;
+	} else {
+		mmhub_eng = eng;
+		gfxhub_eng = 0x1f;
+	}
+
+	/* Trigger invalidation. */
+	ib->ptr[ib->length_dw++] =
+		(SDMA_PKT_VM_INVALIDATION_HEADER_OP(SDMA_OP_POLL_REGMEM) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_SUB_OP(SDMA_SUBOP_VM_INVALIDATION) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_GFX_ENG_ID(gfxhub_eng) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_MM_ENG_ID(mmhub_eng));
+	ib->ptr[ib->length_dw++] = inv_req;
+	ib->ptr[ib->length_dw++] = 0xFFFFFFFF;
+	ib->ptr[ib->length_dw++] =
+		(SDMA_PKT_VM_INVALIDATION_ADDRESSRANGEHI_INVALIDATEACK(1 << vmid) |
+		 SDMA_PKT_VM_INVALIDATION_ADDRESSRANGEHI_ADDRESSRANGEHI(0x1F));
+}
+
 static const struct amdgpu_buffer_funcs sdma_v6_0_buffer_funcs = {
 	.copy_max_bytes = 1 << 30,
 	.copy_num_dw = 7,
@@ -1854,6 +1900,9 @@ static const struct amdgpu_buffer_funcs sdma_v6_0_buffer_funcs = {
 	.fill_max_bytes = 1 << 30,
 	.fill_num_dw = 5,
 	.emit_fill_buffer = sdma_v6_0_emit_fill_buffer,
+
+	.tlb_inv_num_dw = 4,
+	.emit_tlb_inv = sdma_v6_0_emit_tlb_inv,
 };
 
 static void sdma_v6_0_set_buffer_funcs(struct amdgpu_device *adev)
Signature · 1 line
2.55.0
author maintainer
#14 lore raw

PATCH 14/30 drm/amdgpu/sdma7: add tlb invalidation buffer func callback

In reply to Alex Deucher

Will be used for TLB invalidation.

---
 drivers/gpu/drm/amd/amdgpu/sdma_v7_0.c | 48 ++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
drivers/gpu/drm/amd/amdgpu/sdma_v7_0.c +48 -0
index d5552f206e4dc..dab2d08d8766b 100644
@@ -1796,6 +1796,52 @@ static void sdma_v7_0_emit_fill_buffer(struct amdgpu_ib *ib,
 	ib->ptr[ib->length_dw++] = byte_count - 1;
 }
 
+/**
+ * sdma_v7_0_emit_tlb_inv - Invalidate TLB using the sDMA engine
+ *
+ * @adev: amdgpu device structure
+ * @ib: indirect buffer to fill
+ * @vmid: vmid to target
+ * @vmhub: vmhub to target
+ * @eng: invalidation engine to use
+ * @flush_type: type of flush (lightweight, heavyweight)
+ * @xcc_inst: XCC to target
+ *
+ * Invalidate TLB using the DMA engine.
+ */
+static void sdma_v7_0_emit_tlb_inv(struct amdgpu_device *adev,
+				   struct amdgpu_ib *ib,
+				   unsigned int vmid,
+				   u32 vmhub,
+				   u32 eng,
+				   u32 flush_type,
+				   u32 xcc_inst)
+{
+	struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
+	u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
+	u32 mmhub_eng, gfxhub_eng;
+
+	if (AMDGPU_IS_GFXHUB(vmhub)) {
+		mmhub_eng = 0x1f;
+		gfxhub_eng = eng;
+	} else {
+		mmhub_eng = eng;
+		gfxhub_eng = 0x1f;
+	}
+
+	/* Trigger invalidation. */
+	ib->ptr[ib->length_dw++] =
+		(SDMA_PKT_VM_INVALIDATION_HEADER_OP(SDMA_OP_POLL_REGMEM) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_SUB_OP(SDMA_SUBOP_VM_INVALIDATION) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_GFX_ENG_ID(gfxhub_eng) |
+		 SDMA_PKT_VM_INVALIDATION_HEADER_MM_ENG_ID(mmhub_eng));
+	ib->ptr[ib->length_dw++] = inv_req;
+	ib->ptr[ib->length_dw++] = 0xFFFFFFFF;
+	ib->ptr[ib->length_dw++] =
+		(SDMA_PKT_VM_INVALIDATION_ADDRESSRANGEHI_INVALIDATEACK(1 << vmid) |
+		 SDMA_PKT_VM_INVALIDATION_ADDRESSRANGEHI_ADDRESSRANGEHI(0x1F));
+}
+
 static const struct amdgpu_buffer_funcs sdma_v7_0_buffer_funcs = {
 	.copy_max_bytes = 1 << 30,
 	.copy_num_dw = 8,
@@ -1803,6 +1849,8 @@ static const struct amdgpu_buffer_funcs sdma_v7_0_buffer_funcs = {
 	.fill_max_bytes = 1 << 30,
 	.fill_num_dw = 5,
 	.emit_fill_buffer = sdma_v7_0_emit_fill_buffer,
+	.tlb_inv_num_dw = 4,
+	.emit_tlb_inv = sdma_v7_0_emit_tlb_inv,
 };
 
 static void sdma_v7_0_set_buffer_funcs(struct amdgpu_device *adev)
Signature · 1 line
2.55.0
author maintainer
#15 lore raw

PATCH 15/30 drm/amdgpu: simplify amdgpu_gmc_flush_gpu_tlb()

In reply to Alex Deucher

It's only used for GART TLB flushes so rename it and remove additional parameters that are never used.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c |  2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c  | 16 ++++++----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h  |  4 ++--
 3 files changed, 9 insertions(+), 13 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -1
index c4c21dbbbdbf8..0db997c8fa03a 100644
@@ -502,7 +502,7 @@ void amdgpu_gart_invalidate_tlb(struct amdgpu_device *adev)
 		up_read(&adev->reset_domain->sem);
 	}
 	for_each_set_bit(i, adev->vmhubs_mask, AMDGPU_MAX_VMHUBS)
-		amdgpu_gmc_flush_gpu_tlb(adev, 0, i, 0);
+		amdgpu_gmc_flush_gpu_tlb_gart(adev, i);
 }
 
 /**
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +6 -10
index 2f6d20c00ce29..6af4ed2170b06 100644
@@ -714,8 +714,8 @@ int amdgpu_gmc_allocate_vm_inv_eng(struct amdgpu_device *adev)
 	return 0;
 }
 
-void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
-			      uint32_t vmhub, uint32_t flush_type)
+void amdgpu_gmc_flush_gpu_tlb_gart(struct amdgpu_device *adev,
+				   uint32_t vmhub)
 {
 	struct amdgpu_ring *ring;
 	struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
@@ -725,7 +725,7 @@ void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 
 	ring = to_amdgpu_ring(adev->mman.buffer_funcs_scheds[0]);
 
-	if (!hub->sdma_invalidation_workaround || vmid ||
+	if (!hub->sdma_invalidation_workaround ||
 	    !adev->mman.buffer_funcs_enabled || !adev->ib_pool_ready ||
 	    !ring->sched.ready) {
 		/*
@@ -736,15 +736,11 @@ void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
 			return;
 
 		if (adev->gmc.flush_tlb_needs_extra_type_2)
-			adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid,
+			adev->gmc.gmc_funcs->flush_gpu_tlb(adev, 0,
 							   vmhub, 2);
 
-		if (adev->gmc.flush_tlb_needs_extra_type_0 && flush_type == 2)
-			adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid,
-							   vmhub, 0);
-
-		adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid, vmhub,
-						   flush_type);
+		adev->gmc.gmc_funcs->flush_gpu_tlb(adev, 0, vmhub,
+						   0);
 		up_read(&adev->reset_domain->sem);
 		return;
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h +2 -2
index f8f4df37d5805..23954e3180a8c 100644
@@ -441,8 +441,8 @@ int amdgpu_gmc_handle_retry_fault(struct amdgpu_device *adev,
 				  bool write_fault);
 int amdgpu_gmc_ras_sw_init(struct amdgpu_device *adev);
 int amdgpu_gmc_allocate_vm_inv_eng(struct amdgpu_device *adev);
-void amdgpu_gmc_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
-			      uint32_t vmhub, uint32_t flush_type);
+void amdgpu_gmc_flush_gpu_tlb_gart(struct amdgpu_device *adev,
+				   uint32_t vmhub);
 int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
 				   uint32_t flush_type, bool all_hub,
 				   uint32_t inst);
Signature · 1 line
2.55.0
author maintainer
#16 lore raw

PATCH 16/30 drm/amdgpu: add tlb invalidation method enum

In reply to Alex Deucher

Use it to track invalidation method.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h +10 -0
index 23954e3180a8c..204cf1c360896 100644
@@ -98,6 +98,13 @@ enum amdgpu_memory_partition {
 #define AMDGPU_GMC121_FAULT_SOURCE_DATA_WRITE 0x200000
 #define AMDGPU_GMC121_FAULT_SOURCE_DATA_EXE   0x100000
 
+enum amdgpu_tlb_inv_method {
+	AMDGPU_TLB_INV_METHOD_MMIO = 0,
+	AMDGPU_TLB_INV_METHOD_SDMA,
+	AMDGPU_TLB_INV_METHOD_KIQ,
+	AMDGPU_TLB_INV_METHOD_MES
+};
+
 /*
  * GMC page fault information
  */
@@ -369,6 +376,9 @@ struct amdgpu_gmc {
 	bool override_pte;
 
 	bool use_mmio_for_tlb_flush;
+
+	enum amdgpu_tlb_inv_method gart_inv_method;
+	enum amdgpu_tlb_inv_method pasid_inv_method;
 };
 
 #define amdgpu_gmc_emit_flush_gpu_tlb(r, vmid, addr) (r)->adev->gmc.gmc_funcs->emit_flush_gpu_tlb((r), (vmid), (addr))
Signature · 1 line
2.55.0
author maintainer
#17 lore raw

PATCH 17/30 drm/amdgpu: plumb tlb inv method in amdgpu_gmc_flush_gpu_tlb_gart()

In reply to Alex Deucher

Plumb the invalidation method into amdgpu_gmc_flush_gpu_tlb_gart(). Keep the current behavior.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +13 -3
index 6af4ed2170b06..779f9b0974d2e 100644
@@ -719,15 +719,25 @@ void amdgpu_gmc_flush_gpu_tlb_gart(struct amdgpu_device *adev,
 {
 	struct amdgpu_ring *ring;
 	struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
+	bool use_mmio;
 	struct dma_fence *fence;
 	struct amdgpu_job *job;
 	int r;
 
 	ring = to_amdgpu_ring(adev->mman.buffer_funcs_scheds[0]);
 
-	if (!hub->sdma_invalidation_workaround ||
-	    !adev->mman.buffer_funcs_enabled || !adev->ib_pool_ready ||
-	    !ring->sched.ready) {
+	switch (adev->gmc.gart_inv_method) {
+	case AMDGPU_TLB_INV_METHOD_MMIO:
+	default:
+		use_mmio = !hub->sdma_invalidation_workaround;
+		break;
+	case AMDGPU_TLB_INV_METHOD_SDMA:
+		use_mmio = false;
+		break;
+	}
+
+	if (!adev->mman.buffer_funcs_enabled ||
+	    !adev->ib_pool_ready || !ring->sched.ready || use_mmio) {
 		/*
 		 * A GPU reset should flush all TLBs anyway, so no need to do
 		 * this while one is ongoing.
Signature · 1 line
2.55.0
author maintainer
#18 lore raw

PATCH 18/30 drm/amdgpu: uplevel reset check in amdgpu_gmc_flush_gpu_tlb_gart()

In reply to Alex Deucher

Check for both the MMIO and SDMA pathes.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +9 -7
index 779f9b0974d2e..8a975eddd75c7 100644
@@ -724,6 +724,13 @@ void amdgpu_gmc_flush_gpu_tlb_gart(struct amdgpu_device *adev,
 	struct amdgpu_job *job;
 	int r;
 
+	/*
+	 * A GPU reset should flush all TLBs anyway, so no need to do
+	 * this while one is ongoing.
+	 */
+	if (!down_read_trylock(&adev->reset_domain->sem))
+		return;
+
 	ring = to_amdgpu_ring(adev->mman.buffer_funcs_scheds[0]);
 
 	switch (adev->gmc.gart_inv_method) {
@@ -738,13 +745,6 @@ void amdgpu_gmc_flush_gpu_tlb_gart(struct amdgpu_device *adev,
 
 	if (!adev->mman.buffer_funcs_enabled ||
 	    !adev->ib_pool_ready || !ring->sched.ready || use_mmio) {
-		/*
-		 * A GPU reset should flush all TLBs anyway, so no need to do
-		 * this while one is ongoing.
-		 */
-		if (!down_read_trylock(&adev->reset_domain->sem))
-			return;
-
 		if (adev->gmc.flush_tlb_needs_extra_type_2)
 			adev->gmc.gmc_funcs->flush_gpu_tlb(adev, 0,
 							   vmhub, 2);
@@ -778,11 +778,13 @@ void amdgpu_gmc_flush_gpu_tlb_gart(struct amdgpu_device *adev,
 
 	dma_fence_wait(fence, false);
 	dma_fence_put(fence);
+	up_read(&adev->reset_domain->sem);
 
 	return;
 
 error_alloc:
 	mutex_unlock(&adev->mman.default_entity.lock);
+	up_read(&adev->reset_domain->sem);
 	dev_err(adev->dev, "Error flushing GPU TLB using the SDMA (%d)!\n", r);
 }
 
Signature · 1 line
2.55.0
author maintainer
#19 lore raw

PATCH 19/30 drm/amdgpu/gmc: add new callback to lookup vmid to pasid mapping

In reply to Alex Deucher

Look up the mapping so we know which vmid to flush.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h | 7 ++++++-
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c  | 6 ++++--
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c  | 6 ++++--
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c  | 6 ++++--
 drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c  | 1 +
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c   | 6 ++++--
 6 files changed, 23 insertions(+), 9 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h +6 -1
index 204cf1c360896..27785b1b38ccd 100644
@@ -171,6 +171,10 @@ struct amdgpu_gmc_funcs {
 	/* Change the VMID -> PASID mapping */
 	void (*emit_pasid_mapping)(struct amdgpu_ring *ring, unsigned vmid,
 				   unsigned pasid);
+	/* look up the vmids for the pasid */
+	bool (*get_vmid_pasid_mapping_info)(struct amdgpu_device *adev,
+					    uint8_t vmid, uint8_t inst,
+					    uint16_t *p_pasid);
 	/* enable/disable PRT support */
 	void (*set_prt)(struct amdgpu_device *adev, bool enable);
 	/* get the pde for a given mc addr */
@@ -384,7 +388,8 @@ struct amdgpu_gmc {
 #define amdgpu_gmc_emit_flush_gpu_tlb(r, vmid, addr) (r)->adev->gmc.gmc_funcs->emit_flush_gpu_tlb((r), (vmid), (addr))
 #define amdgpu_gmc_emit_pasid_mapping(r, vmid, pasid) (r)->adev->gmc.gmc_funcs->emit_pasid_mapping((r), (vmid), (pasid))
 #define amdgpu_gmc_get_vm_pde(adev, level, dst, flags) (adev)->gmc.gmc_funcs->get_vm_pde((adev), (level), (dst), (flags))
-#define amdgpu_gmc_get_vm_pte(adev, vm, bo, vm_flags, pte_flags) \
+#define amdgpu_gmc_get_vmid_pasid_mapping_info(adev, v, i, p) (adev)->gmc.gmc_funcs->get_vmid_pasid_mapping_info((a), (v), (i), (p))
+#define amdgpu_gmc_get_vm_pte(adev, vm, bo, vm_flags, pte_flags)	\
 	((adev)->gmc.gmc_funcs->get_vm_pte((adev), (vm), (bo), (vm_flags), \
 					   (pte_flags)))
 #define amdgpu_gmc_override_vm_pte_flags(adev, vm, addr, pte_flags)	\
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +4 -2
index 23d9fe995a2b5..0028639448956 100644
@@ -204,7 +204,8 @@ static bool gmc_v10_0_use_invalidate_semaphore(struct amdgpu_device *adev,
 
 static bool gmc_v10_0_get_atc_vmid_pasid_mapping_info(
 					struct amdgpu_device *adev,
-					uint8_t vmid, uint16_t *p_pasid)
+					uint8_t vmid, uint8_t inst,
+					uint16_t *p_pasid)
 {
 	uint32_t value;
 
@@ -346,7 +347,7 @@ static void gmc_v10_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 	for (vmid = 1; vmid < AMDGPU_NUM_VMID; vmid++) {
 		bool valid;
 
-		valid = gmc_v10_0_get_atc_vmid_pasid_mapping_info(adev, vmid,
+		valid = gmc_v10_0_get_atc_vmid_pasid_mapping_info(adev, vmid, 0,
 								  &queried);
 		if (!valid || queried != pasid)
 			continue;
@@ -555,6 +556,7 @@ static const struct amdgpu_gmc_funcs gmc_v10_0_gmc_funcs = {
 	.flush_gpu_tlb_pasid = gmc_v10_0_flush_gpu_tlb_pasid,
 	.emit_flush_gpu_tlb = gmc_v10_0_emit_flush_gpu_tlb,
 	.emit_pasid_mapping = gmc_v10_0_emit_pasid_mapping,
+	.get_vmid_pasid_mapping_info = gmc_v10_0_get_atc_vmid_pasid_mapping_info,
 	.get_vm_pde = gmc_v10_0_get_vm_pde,
 	.get_vm_pte = gmc_v10_0_get_vm_pte,
 	.get_vbios_fb_size = gmc_v10_0_get_vbios_fb_size,
drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c +4 -2
index 098e1340554c5..b34bd7881726d 100644
@@ -200,7 +200,8 @@ static bool gmc_v11_0_use_invalidate_semaphore(struct amdgpu_device *adev,
 
 static bool gmc_v11_0_get_vmid_pasid_mapping_info(
 					struct amdgpu_device *adev,
-					uint8_t vmid, uint16_t *p_pasid)
+					uint8_t vmid, uint8_t inst,
+					uint16_t *p_pasid)
 {
 	*p_pasid = RREG32(SOC15_REG_OFFSET(OSSSYS, 0, regIH_VMID_0_LUT) + vmid) & 0xffff;
 
@@ -338,7 +339,7 @@ static void gmc_v11_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 	for (vmid = 1; vmid < 16; vmid++) {
 		bool valid;
 
-		valid = gmc_v11_0_get_vmid_pasid_mapping_info(adev, vmid,
+		valid = gmc_v11_0_get_vmid_pasid_mapping_info(adev, vmid, 0,
 							      &queried);
 		if (!valid || queried != pasid)
 			continue;
@@ -546,6 +547,7 @@ static const struct amdgpu_gmc_funcs gmc_v11_0_gmc_funcs = {
 	.flush_gpu_tlb_pasid = gmc_v11_0_flush_gpu_tlb_pasid,
 	.emit_flush_gpu_tlb = gmc_v11_0_emit_flush_gpu_tlb,
 	.emit_pasid_mapping = gmc_v11_0_emit_pasid_mapping,
+	.get_vmid_pasid_mapping_info = gmc_v11_0_get_vmid_pasid_mapping_info,
 	.get_vm_pde = gmc_v11_0_get_vm_pde,
 	.get_vm_pte = gmc_v11_0_get_vm_pte,
 	.get_vbios_fb_size = gmc_v11_0_get_vbios_fb_size,
drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c +4 -2
index cffc818880d12..9179dc0787f13 100644
@@ -196,7 +196,8 @@ static bool gmc_v12_0_use_invalidate_semaphore(struct amdgpu_device *adev,
 
 static bool gmc_v12_0_get_vmid_pasid_mapping_info(
 					struct amdgpu_device *adev,
-					uint8_t vmid, uint16_t *p_pasid)
+					uint8_t vmid, uint8_t inst,
+					uint16_t *p_pasid)
 {
 	*p_pasid = RREG32(SOC15_REG_OFFSET(OSSSYS, 0, regIH_VMID_0_LUT) + vmid) & 0xffff;
 
@@ -374,7 +375,7 @@ static void gmc_v12_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 	for (vmid = 1; vmid < 16; vmid++) {
 		bool valid;
 
-		valid = gmc_v12_0_get_vmid_pasid_mapping_info(adev, vmid,
+		valid = gmc_v12_0_get_vmid_pasid_mapping_info(adev, vmid, 0,
 							      &queried);
 		if (!valid || queried != pasid)
 			continue;
@@ -581,6 +582,7 @@ static const struct amdgpu_gmc_funcs gmc_v12_0_gmc_funcs = {
 	.flush_gpu_tlb_pasid = gmc_v12_0_flush_gpu_tlb_pasid,
 	.emit_flush_gpu_tlb = gmc_v12_0_emit_flush_gpu_tlb,
 	.emit_pasid_mapping = gmc_v12_0_emit_pasid_mapping,
+	.get_vmid_pasid_mapping_info = gmc_v12_0_get_vmid_pasid_mapping_info,
 	.get_vm_pde = gmc_v12_0_get_vm_pde,
 	.get_vm_pte = gmc_v12_0_get_vm_pte,
 	.get_vbios_fb_size = gmc_v12_0_get_vbios_fb_size,
drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c +1 -0
index 6c0d2689cc05d..3fa1ec3dca273 100644
@@ -668,6 +668,7 @@ static const struct amdgpu_gmc_funcs gmc_v12_1_gmc_funcs = {
 	.flush_gpu_tlb_pasid = gmc_v12_1_flush_gpu_tlb_pasid,
 	.emit_flush_gpu_tlb = gmc_v12_1_emit_flush_gpu_tlb,
 	.emit_pasid_mapping = gmc_v12_1_emit_pasid_mapping,
+	.get_vmid_pasid_mapping_info = gmc_v12_1_get_vmid_pasid_mapping_info,
 	.get_vm_pde = gmc_v12_1_get_vm_pde,
 	.get_vm_pte = gmc_v12_1_get_vm_pte,
 	.query_mem_partition_mode = &amdgpu_gmc_query_memory_partition,
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +4 -2
index 8edaa54fca82f..19a10acedc6ce 100644
@@ -746,7 +746,8 @@ static bool gmc_v9_0_use_invalidate_semaphore(struct amdgpu_device *adev,
 }
 
 static bool gmc_v9_0_get_atc_vmid_pasid_mapping_info(struct amdgpu_device *adev,
-					uint8_t vmid, uint16_t *p_pasid)
+						     uint8_t vmid, uint8_t inst,
+						     uint16_t *p_pasid)
 {
 	uint32_t value;
 
@@ -908,7 +909,7 @@ static void gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 		bool valid;
 
 		valid = gmc_v9_0_get_atc_vmid_pasid_mapping_info(adev, vmid,
-								 &queried);
+								 inst, &queried);
 		if (!valid || queried != pasid)
 			continue;
 
@@ -1321,6 +1322,7 @@ static const struct amdgpu_gmc_funcs gmc_v9_0_gmc_funcs = {
 	.flush_gpu_tlb_pasid = gmc_v9_0_flush_gpu_tlb_pasid,
 	.emit_flush_gpu_tlb = gmc_v9_0_emit_flush_gpu_tlb,
 	.emit_pasid_mapping = gmc_v9_0_emit_pasid_mapping,
+	.get_vmid_pasid_mapping_info = gmc_v9_0_get_atc_vmid_pasid_mapping_info,
 	.get_vm_pde = gmc_v9_0_get_vm_pde,
 	.get_vm_pte = gmc_v9_0_get_vm_pte,
 	.override_vm_pte_flags = gmc_v9_0_override_vm_pte_flags,
Signature · 1 line
2.55.0
author maintainer
#20 lore raw

PATCH 20/30 drm/amdgpu: add a gmc callback for the inv semaphore

In reply to Alex Deucher

Used to determine whether or not to use the invalidation semaphore for a particular hub.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h | 3 +++
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c  | 1 +
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c  | 1 +
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c  | 1 +
 drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c  | 1 +
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c   | 1 +
 6 files changed, 8 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h +3 -0
index 27785b1b38ccd..f20f08630408e 100644
@@ -175,6 +175,9 @@ struct amdgpu_gmc_funcs {
 	bool (*get_vmid_pasid_mapping_info)(struct amdgpu_device *adev,
 					    uint8_t vmid, uint8_t inst,
 					    uint16_t *p_pasid);
+	/* determine whether or not to use the inv semaphore */
+	bool (*use_invalidate_semaphore)(struct amdgpu_device *adev,
+					 uint32_t vmhub);
 	/* enable/disable PRT support */
 	void (*set_prt)(struct amdgpu_device *adev, bool enable);
 	/* get the pde for a given mc addr */
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +1 -0
index 0028639448956..4bb10a89ce582 100644
@@ -557,6 +557,7 @@ static const struct amdgpu_gmc_funcs gmc_v10_0_gmc_funcs = {
 	.emit_flush_gpu_tlb = gmc_v10_0_emit_flush_gpu_tlb,
 	.emit_pasid_mapping = gmc_v10_0_emit_pasid_mapping,
 	.get_vmid_pasid_mapping_info = gmc_v10_0_get_atc_vmid_pasid_mapping_info,
+	.use_invalidate_semaphore = gmc_v10_0_use_invalidate_semaphore,
 	.get_vm_pde = gmc_v10_0_get_vm_pde,
 	.get_vm_pte = gmc_v10_0_get_vm_pte,
 	.get_vbios_fb_size = gmc_v10_0_get_vbios_fb_size,
drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c +1 -0
index b34bd7881726d..acab9f4180da6 100644
@@ -548,6 +548,7 @@ static const struct amdgpu_gmc_funcs gmc_v11_0_gmc_funcs = {
 	.emit_flush_gpu_tlb = gmc_v11_0_emit_flush_gpu_tlb,
 	.emit_pasid_mapping = gmc_v11_0_emit_pasid_mapping,
 	.get_vmid_pasid_mapping_info = gmc_v11_0_get_vmid_pasid_mapping_info,
+	.use_invalidate_semaphore = gmc_v11_0_use_invalidate_semaphore,
 	.get_vm_pde = gmc_v11_0_get_vm_pde,
 	.get_vm_pte = gmc_v11_0_get_vm_pte,
 	.get_vbios_fb_size = gmc_v11_0_get_vbios_fb_size,
drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c +1 -0
index 9179dc0787f13..889c0005fec3d 100644
@@ -583,6 +583,7 @@ static const struct amdgpu_gmc_funcs gmc_v12_0_gmc_funcs = {
 	.emit_flush_gpu_tlb = gmc_v12_0_emit_flush_gpu_tlb,
 	.emit_pasid_mapping = gmc_v12_0_emit_pasid_mapping,
 	.get_vmid_pasid_mapping_info = gmc_v12_0_get_vmid_pasid_mapping_info,
+	.use_invalidate_semaphore = gmc_v12_0_use_invalidate_semaphore,
 	.get_vm_pde = gmc_v12_0_get_vm_pde,
 	.get_vm_pte = gmc_v12_0_get_vm_pte,
 	.get_vbios_fb_size = gmc_v12_0_get_vbios_fb_size,
drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c +1 -0
index 3fa1ec3dca273..d6be7c092a22d 100644
@@ -669,6 +669,7 @@ static const struct amdgpu_gmc_funcs gmc_v12_1_gmc_funcs = {
 	.emit_flush_gpu_tlb = gmc_v12_1_emit_flush_gpu_tlb,
 	.emit_pasid_mapping = gmc_v12_1_emit_pasid_mapping,
 	.get_vmid_pasid_mapping_info = gmc_v12_1_get_vmid_pasid_mapping_info,
+	.use_invalidate_semaphore = gmc_v12_1_use_invalidate_semaphore,
 	.get_vm_pde = gmc_v12_1_get_vm_pde,
 	.get_vm_pte = gmc_v12_1_get_vm_pte,
 	.query_mem_partition_mode = &amdgpu_gmc_query_memory_partition,
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +1 -0
index 19a10acedc6ce..13423258d33ed 100644
@@ -1323,6 +1323,7 @@ static const struct amdgpu_gmc_funcs gmc_v9_0_gmc_funcs = {
 	.emit_flush_gpu_tlb = gmc_v9_0_emit_flush_gpu_tlb,
 	.emit_pasid_mapping = gmc_v9_0_emit_pasid_mapping,
 	.get_vmid_pasid_mapping_info = gmc_v9_0_get_atc_vmid_pasid_mapping_info,
+	.use_invalidate_semaphore = gmc_v9_0_use_invalidate_semaphore,
 	.get_vm_pde = gmc_v9_0_get_vm_pde,
 	.get_vm_pte = gmc_v9_0_get_vm_pte,
 	.override_vm_pte_flags = gmc_v9_0_override_vm_pte_flags,
Signature · 1 line
2.55.0
author maintainer
#21 lore raw

PATCH 21/30 drm/amdgpu/gmc: rework pasid flushing

In reply to Alex Deucher

Split out all of the various flush methods and use the new pasid flush method enum to determine which one to use.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 247 +++++++++++++++++++-----
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c  |   2 +
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c  |   2 +
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c   |   2 +
 4 files changed, 201 insertions(+), 52 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +195 -52
index 8a975eddd75c7..2800eebe50649 100644
@@ -788,9 +788,66 @@ void amdgpu_gmc_flush_gpu_tlb_gart(struct amdgpu_device *adev,
 	dev_err(adev->dev, "Error flushing GPU TLB using the SDMA (%d)!\n", r);
 }
 
-int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
-				   uint32_t flush_type, bool all_hub,
-				   uint32_t inst)
+static int amdgpu_gmc_flush_gpu_tlb_sdma_helper(struct amdgpu_device *adev,
+						uint16_t pasid, uint32_t flush_type,
+						bool all_hub, uint32_t inst)
+{
+	struct amdgpu_ring *ring;
+	struct dma_fence *fence;
+	struct amdgpu_job *job;
+	uint16_t queried;
+	/* Use register 17 for GART */
+	u32 eng = 17;
+	int vmid, r, i, ndw;
+
+	/* flush hdp cache */
+	amdgpu_device_flush_hdp(adev, NULL);
+
+	ndw = ALIGN(adev->mman.buffer_funcs->tlb_inv_num_dw * 16 * AMDGPU_MAX_VMHUBS, 8);
+	ring = to_amdgpu_ring(adev->mman.buffer_funcs_scheds[0]);
+
+	mutex_lock(&adev->mman.default_entity.lock);
+	r = amdgpu_job_alloc_with_ib(ring->adev, &adev->mman.default_entity.base,
+				     AMDGPU_FENCE_OWNER_UNDEFINED,
+				     ndw * 4, AMDGPU_IB_POOL_IMMEDIATE,
+				     AMDGPU_KERNEL_JOB_ID_VM_UPDATE,
+				     &job);
+	if (r)
+		goto exit;
+
+	for (vmid = 1; vmid < 16; vmid++) {
+		bool valid;
+
+		valid = adev->gmc.gmc_funcs->get_vmid_pasid_mapping_info(adev, vmid, inst,
+									 &queried);
+		if (!valid || queried != pasid)
+			continue;
+
+		if (all_hub) {
+			for_each_set_bit(i, adev->vmhubs_mask, AMDGPU_MAX_VMHUBS)
+				amdgpu_emit_tlb_inv(adev, &job->ibs[0], vmid, i, eng,
+						    flush_type, inst);
+		} else {
+			amdgpu_emit_tlb_inv(adev, &job->ibs[0], vmid, AMDGPU_GFXHUB(inst), eng,
+					    flush_type, inst);
+		}
+	}
+	amdgpu_ring_pad_ib(ring, &job->ibs[0]);
+	fence = amdgpu_job_submit(job);
+	mutex_unlock(&adev->mman.default_entity.lock);
+
+	dma_fence_wait(fence, false);
+	dma_fence_put(fence);
+
+exit:
+	mutex_unlock(&adev->mman.default_entity.lock);
+
+	return r;
+}
+
+static int amdgpu_gmc_flush_gpu_tlb_kiq_helper(struct amdgpu_device *adev,
+					       uint16_t pasid, uint32_t flush_type,
+					       bool all_hub, uint32_t inst)
 {
 	struct amdgpu_ring *ring = &adev->gfx.kiq[inst].ring;
 	struct amdgpu_kiq *kiq = &adev->gfx.kiq[inst];
@@ -798,6 +855,110 @@ int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
 	int r, cnt = 0;
 	uint32_t seq;
 
+	/* 2 dwords flush + 8 dwords fence */
+	ndw = kiq->pmf->invalidate_tlbs_size + 8;
+
+	if (adev->gmc.flush_tlb_needs_extra_type_2)
+		ndw += kiq->pmf->invalidate_tlbs_size;
+
+	if (adev->gmc.flush_tlb_needs_extra_type_0)
+		ndw += kiq->pmf->invalidate_tlbs_size;
+
+	spin_lock(&adev->gfx.kiq[inst].ring_lock);
+	r = amdgpu_ring_alloc(ring, ndw);
+	if (r) {
+		spin_unlock(&adev->gfx.kiq[inst].ring_lock);
+		return r;
+	}
+	if (adev->gmc.flush_tlb_needs_extra_type_2)
+		kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 2, all_hub);
+
+	if (flush_type == 2 && adev->gmc.flush_tlb_needs_extra_type_0)
+		kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 0, all_hub);
+
+	kiq->pmf->kiq_invalidate_tlbs(ring, pasid, flush_type, all_hub);
+	r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
+	if (r) {
+		amdgpu_ring_undo(ring);
+		spin_unlock(&adev->gfx.kiq[inst].ring_lock);
+		return r;
+	}
+
+	amdgpu_ring_commit(ring);
+	spin_unlock(&adev->gfx.kiq[inst].ring_lock);
+
+	r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
+
+	might_sleep();
+	while (r < 1 && cnt++ < MAX_KIQ_REG_TRY &&
+	       !amdgpu_reset_pending(adev->reset_domain)) {
+		msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
+		r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
+	}
+
+	if (cnt > MAX_KIQ_REG_TRY) {
+		dev_err(adev->dev, "timeout waiting for kiq fence\n");
+		r = -ETIME;
+	} else
+		r = 0;
+
+	return r;
+}
+
+static int amdgpu_gmc_flush_gpu_tlb_mes_helper(struct amdgpu_device *adev,
+					       uint16_t pasid, uint32_t flush_type,
+					       bool all_hub, uint32_t inst)
+{
+	struct mes_inv_tlbs_pasid_input input = {0};
+	int r;
+
+	input.xcc_id = inst;
+	input.pasid = pasid;
+	input.flush_type = flush_type;
+
+	/* MES will invalidate hubs for the device(including slave xcc)
+	 * from master, ignore request from slave
+	 */
+	if (!amdgpu_gfx_is_master_xcc(adev, inst))
+		return -EINVAL;
+
+	input.hub_id = AMDGPU_GFXHUB(0);
+	amdgpu_mes_lock(&adev->mes);
+	r = adev->mes.funcs->invalidate_tlbs_pasid(&adev->mes, &input);
+	amdgpu_mes_unlock(&adev->mes);
+	if (r)
+		return r;
+
+	if (all_hub) {
+		/* invalidate mm_hub */
+		if (test_bit(AMDGPU_MMHUB0(0), adev->vmhubs_mask)) {
+			input.hub_id = AMDGPU_MMHUB0(0);
+			amdgpu_mes_lock(&adev->mes);
+			r = adev->mes.funcs->invalidate_tlbs_pasid(&adev->mes, &input);
+			amdgpu_mes_unlock(&adev->mes);
+			if (r)
+				return r;
+		}
+		if (test_bit(AMDGPU_MMHUB1(0), adev->vmhubs_mask)) {
+			input.hub_id = AMDGPU_MMHUB1(0);
+			amdgpu_mes_lock(&adev->mes);
+			r = adev->mes.funcs->invalidate_tlbs_pasid(&adev->mes, &input);
+			amdgpu_mes_unlock(&adev->mes);
+			if (r)
+				return r;
+		}
+	}
+	return 0;
+}
+
+int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
+				   uint32_t flush_type, bool all_hub,
+				   uint32_t inst)
+{
+	struct amdgpu_ring *ring;
+	bool use_mmio = false;
+	int r;
+
 	/*
 	 * A GPU reset should flush all TLBs anyway, so no need to do
 	 * this while one is ongoing.
@@ -805,8 +966,38 @@ int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
 	if (!down_read_trylock(&adev->reset_domain->sem))
 		return 0;
 
-	if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready) {
+	switch (adev->gmc.pasid_inv_method) {
+	case AMDGPU_TLB_INV_METHOD_MMIO:
+	default:
+		use_mmio = true;
+		break;
+	case AMDGPU_TLB_INV_METHOD_SDMA:
+		ring = to_amdgpu_ring(adev->mman.buffer_funcs_scheds[0]);
+		if (!ring->sched.ready)
+			use_mmio = true;
+		else
+			r = amdgpu_gmc_flush_gpu_tlb_sdma_helper(adev, pasid, flush_type,
+								 all_hub, inst);
+		break;
+	case AMDGPU_TLB_INV_METHOD_KIQ:
+		ring = &adev->gfx.kiq[inst].ring;
+		if (!adev->gmc.flush_pasid_uses_kiq || !ring->sched.ready)
+			use_mmio = true;
+		else
+			r = amdgpu_gmc_flush_gpu_tlb_kiq_helper(adev, pasid, flush_type,
+								all_hub, inst);
+		break;
+	case AMDGPU_TLB_INV_METHOD_MES:
+		ring = &adev->mes.ring[MES_PIPE_INST(inst, 0)];
+		if (!ring->sched.ready)
+			use_mmio = true;
+		else
+			r = amdgpu_gmc_flush_gpu_tlb_mes_helper(adev, pasid, flush_type,
+								all_hub, inst);
+		break;
+	}
 
+	if (use_mmio) {
 		if (!adev->gmc.gmc_funcs->flush_gpu_tlb_pasid) {
 			r = 0;
 			goto error_unlock_reset;
@@ -825,54 +1016,6 @@ int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
 		adev->gmc.gmc_funcs->flush_gpu_tlb_pasid(adev, pasid,
 							 flush_type, all_hub,
 							 inst);
-		r = 0;
-	} else {
-		/* 2 dwords flush + 8 dwords fence */
-		ndw = kiq->pmf->invalidate_tlbs_size + 8;
-
-		if (adev->gmc.flush_tlb_needs_extra_type_2)
-			ndw += kiq->pmf->invalidate_tlbs_size;
-
-		if (adev->gmc.flush_tlb_needs_extra_type_0)
-			ndw += kiq->pmf->invalidate_tlbs_size;
-
-		spin_lock(&adev->gfx.kiq[inst].ring_lock);
-		r = amdgpu_ring_alloc(ring, ndw);
-		if (r) {
-			spin_unlock(&adev->gfx.kiq[inst].ring_lock);
-			goto error_unlock_reset;
-		}
-		if (adev->gmc.flush_tlb_needs_extra_type_2)
-			kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 2, all_hub);
-
-		if (flush_type == 2 && adev->gmc.flush_tlb_needs_extra_type_0)
-			kiq->pmf->kiq_invalidate_tlbs(ring, pasid, 0, all_hub);
-
-		kiq->pmf->kiq_invalidate_tlbs(ring, pasid, flush_type, all_hub);
-		r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
-		if (r) {
-			amdgpu_ring_undo(ring);
-			spin_unlock(&adev->gfx.kiq[inst].ring_lock);
-			goto error_unlock_reset;
-		}
-
-		amdgpu_ring_commit(ring);
-		spin_unlock(&adev->gfx.kiq[inst].ring_lock);
-
-		r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
-
-		might_sleep();
-		while (r < 1 && cnt++ < MAX_KIQ_REG_TRY &&
-		       !amdgpu_reset_pending(adev->reset_domain)) {
-			msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
-			r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
-		}
-
-		if (cnt > MAX_KIQ_REG_TRY) {
-			dev_err(adev->dev, "timeout waiting for kiq fence\n");
-			r = -ETIME;
-		} else
-			r = 0;
 	}
 
 error_unlock_reset:
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +2 -0
index 4bb10a89ce582..e7c529620199b 100644
@@ -974,6 +974,8 @@ static int gmc_v10_0_hw_init(struct amdgpu_ip_block *ip_block)
 	int r;
 
 	adev->gmc.flush_pasid_uses_kiq = !amdgpu_emu_mode;
+	if (adev->gmc.flush_pasid_uses_kiq)
+		adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_KIQ;
 
 	/* The sequence of these two function calls matters.*/
 	gmc_v10_0_init_golden_registers(adev);
drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c +2 -0
index acab9f4180da6..a35c84cc7385f 100644
@@ -950,6 +950,8 @@ static int gmc_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 	int r;
 
 	adev->gmc.flush_pasid_uses_kiq = !amdgpu_emu_mode;
+	if (adev->gmc.flush_pasid_uses_kiq)
+		adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_KIQ;
 
 	/* The sequence of these two function calls matters.*/
 	gmc_v11_0_init_golden_registers(adev);
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +2 -0
index 13423258d33ed..e9e5a1e183231 100644
@@ -2181,6 +2181,8 @@ static int gmc_v9_0_hw_init(struct amdgpu_ip_block *ip_block)
 	int i, r;
 
 	adev->gmc.flush_pasid_uses_kiq = true;
+	if (adev->gmc.flush_pasid_uses_kiq)
+		adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_KIQ;
 
 	/* Vega20+XGMI caches PTEs in TC and TLB. Add a heavy-weight TLB flush
 	 * (type 2), which flushes both. Due to a race condition with
Signature · 1 line
2.55.0
author maintainer
#22 lore raw

PATCH 22/30 drm/amdgpu/gmc9: use SDMA for gart TLB invalidation

In reply to Alex Deucher

Use SDMA rather than MMIO on vega20 and older. The avoids the need to disallow gfxoff when invalidating.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +2 -0
index e9e5a1e183231..2b359c56f8d44 100644
@@ -2183,6 +2183,8 @@ static int gmc_v9_0_hw_init(struct amdgpu_ip_block *ip_block)
 	adev->gmc.flush_pasid_uses_kiq = true;
 	if (adev->gmc.flush_pasid_uses_kiq)
 		adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_KIQ;
+	if (amdgpu_ip_version(adev, GC_HWIP, 0) <= IP_VERSION(9, 4, 0))
+		adev->gmc.gart_inv_method = AMDGPU_TLB_INV_METHOD_SDMA;
 
 	/* Vega20+XGMI caches PTEs in TC and TLB. Add a heavy-weight TLB flush
 	 * (type 2), which flushes both. Due to a race condition with
Signature · 1 line
2.55.0
author maintainer
#23 lore raw

PATCH 23/30 drm/amdgpu/gmc10: use SDMA for gart TLB invalidation

In reply to Alex Deucher

Use SDMA rather than MMIO. The avoids the need to disallow gfxoff when invalidating.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +1 -0
index e7c529620199b..7eb34d55565cd 100644
@@ -976,6 +976,7 @@ static int gmc_v10_0_hw_init(struct amdgpu_ip_block *ip_block)
 	adev->gmc.flush_pasid_uses_kiq = !amdgpu_emu_mode;
 	if (adev->gmc.flush_pasid_uses_kiq)
 		adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_KIQ;
+	adev->gmc.gart_inv_method = AMDGPU_TLB_INV_METHOD_SDMA;
 
 	/* The sequence of these two function calls matters.*/
 	gmc_v10_0_init_golden_registers(adev);
Signature · 1 line
2.55.0
author maintainer
#24 lore raw

PATCH 24/30 drm/amdgpu/gmc11: use SDMA for gart TLB invalidation

In reply to Alex Deucher

Use SDMA rather than MMIO. The avoids the need to disallow gfxoff when invalidating.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c +1 -0
index a35c84cc7385f..46e98fe66dd3e 100644
@@ -952,6 +952,7 @@ static int gmc_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 	adev->gmc.flush_pasid_uses_kiq = !amdgpu_emu_mode;
 	if (adev->gmc.flush_pasid_uses_kiq)
 		adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_KIQ;
+	adev->gmc.gart_inv_method = AMDGPU_TLB_INV_METHOD_SDMA;
 
 	/* The sequence of these two function calls matters.*/
 	gmc_v11_0_init_golden_registers(adev);
Signature · 1 line
2.55.0
author maintainer
#25 lore raw

PATCH 25/30 drm/amdgpu/gmc12: use SDMA for gart TLB invalidation

In reply to Alex Deucher

Use SDMA rather than MMIO. The avoids the need to disallow gfxoff when invalidating.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c +2 -0
index 889c0005fec3d..6ec5798494be1 100644
@@ -1061,6 +1061,8 @@ static int gmc_v12_0_hw_init(struct amdgpu_ip_block *ip_block)
 	int r;
 	struct amdgpu_device *adev = ip_block->adev;
 
+	adev->gmc.gart_inv_method = AMDGPU_TLB_INV_METHOD_SDMA;
+
 	/* The sequence of these two function calls matters.*/
 	gmc_v12_0_init_golden_registers(adev);
 
Signature · 1 line
2.55.0
author maintainer
#26 lore raw

PATCH 26/30 drm/amdgpu/gmc10: use SDMA for pasid TLB invalidation

In reply to Alex Deucher

Use SDMA rather than MMIO. The avoids the need to disallow gfxoff when invalidating.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +1 -2
index 7eb34d55565cd..e716a86ba2913 100644
@@ -974,8 +974,7 @@ static int gmc_v10_0_hw_init(struct amdgpu_ip_block *ip_block)
 	int r;
 
 	adev->gmc.flush_pasid_uses_kiq = !amdgpu_emu_mode;
-	if (adev->gmc.flush_pasid_uses_kiq)
-		adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_KIQ;
+	adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_SDMA;
 	adev->gmc.gart_inv_method = AMDGPU_TLB_INV_METHOD_SDMA;
 
 	/* The sequence of these two function calls matters.*/
Signature · 1 line
2.55.0
author maintainer
#27 lore raw

PATCH 27/30 drm/amdgpu/gmc11: use SDMA for pasid TLB invalidation

In reply to Alex Deucher

Use SDMA rather than MMIO. The avoids the need to disallow gfxoff when invalidating.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c +1 -2
index 46e98fe66dd3e..8b95a1281886b 100644
@@ -950,8 +950,7 @@ static int gmc_v11_0_hw_init(struct amdgpu_ip_block *ip_block)
 	int r;
 
 	adev->gmc.flush_pasid_uses_kiq = !amdgpu_emu_mode;
-	if (adev->gmc.flush_pasid_uses_kiq)
-		adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_KIQ;
+	adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_SDMA;
 	adev->gmc.gart_inv_method = AMDGPU_TLB_INV_METHOD_SDMA;
 
 	/* The sequence of these two function calls matters.*/
Signature · 1 line
2.55.0
author maintainer
#28 lore raw

PATCH 28/30 drm/amdgpu/gmc12: use MES or SDMA for pasid TLB invalidation

In reply to Alex Deucher

Use SDMA or MES rather than MMIO. The avoids the need to disallow gfxoff when invalidating. Default to SDMA and then use MES if the MES firmware supports it.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c | 1 +
 drivers/gpu/drm/amd/amdgpu/mes_v12_0.c | 4 ++++
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c | 4 ++++
 3 files changed, 9 insertions(+)
drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c +1 -0
index 6ec5798494be1..67eb1b595abe0 100644
@@ -1061,6 +1061,7 @@ static int gmc_v12_0_hw_init(struct amdgpu_ip_block *ip_block)
 	int r;
 	struct amdgpu_device *adev = ip_block->adev;
 
+	adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_SDMA;
 	adev->gmc.gart_inv_method = AMDGPU_TLB_INV_METHOD_SDMA;
 
 	/* The sequence of these two function calls matters.*/
drivers/gpu/drm/amd/amdgpu/mes_v12_0.c +4 -0
index ee72afdb0a1d3..dd68db14c43f6 100644
@@ -2097,6 +2097,10 @@ static int mes_v12_0_hw_init(struct amdgpu_ip_block *ip_block)
 	adev->gfx.kiq[0].ring.sched.ready = false;
 	adev->mes.ring[0].sched.ready = true;
 
+	if (adev->enable_uni_mes &&
+	    (adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) >= 0x84)
+		adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_MES;
+
 	return 0;
 
 failure:
drivers/gpu/drm/amd/amdgpu/mes_v12_1.c +4 -0
index f1098118d5c05..5eac7615efff5 100644
@@ -1962,6 +1962,10 @@ static int mes_v12_1_hw_init(struct amdgpu_ip_block *ip_block)
 			return r;
 	}
 
+	if (adev->enable_uni_mes &&
+	    (adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) >= 0x6f)
+		adev->gmc.pasid_inv_method = AMDGPU_TLB_INV_METHOD_MES;
+
 	return 0;
 }
 
Signature · 1 line
2.55.0
author maintainer
#29 lore raw

PATCH 29/30 drm/amdgpu/gmc12: drop MES tlb inv in gmc callbacks

In reply to Alex Deucher

It's now handled directly in the front end functions.

---
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c | 16 ---------------
 drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c | 28 --------------------------
 2 files changed, 44 deletions(-)
drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c +0 -16
index 67eb1b595abe0..4bb8c1e4f335b 100644
@@ -356,22 +356,6 @@ static void gmc_v12_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 	uint16_t queried;
 	int vmid, i;
 
-	if (adev->enable_uni_mes && adev->mes.ring[AMDGPU_MES_SCHED_PIPE].sched.ready &&
-	    (adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) >= 0x84) {
-		struct mes_inv_tlbs_pasid_input input = {0};
-		input.pasid = pasid;
-		input.flush_type = flush_type;
-		input.hub_id = AMDGPU_GFXHUB(0);
-		/* MES will invalidate all gc_hub for the device from master */
-		adev->mes.funcs->invalidate_tlbs_pasid(&adev->mes, &input);
-		if (all_hub) {
-			/* Only need to invalidate mm_hub now, gfx12 only support one mmhub */
-			input.hub_id = AMDGPU_MMHUB0(0);
-			adev->mes.funcs->invalidate_tlbs_pasid(&adev->mes, &input);
-		}
-		return;
-	}
-
 	for (vmid = 1; vmid < 16; vmid++) {
 		bool valid;
 
drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c +0 -28
index d6be7c092a22d..2c980dc02c262 100644
@@ -411,34 +411,6 @@ static void gmc_v12_1_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
 	uint16_t queried;
 	int vmid, i;
 
-	if (adev->enable_uni_mes && adev->mes.ring[0].sched.ready &&
-	    (adev->mes.sched_version & AMDGPU_MES_VERSION_MASK) >= 0x6f) {
-		struct mes_inv_tlbs_pasid_input input = {0};
-		input.xcc_id = inst;
-		input.pasid = pasid;
-		input.flush_type = flush_type;
-
-		/* MES will invalidate hubs for the device(including slave xcc) from master, ignore request from slave */
-		if (!amdgpu_gfx_is_master_xcc(adev, inst))
-			return;
-
-		input.hub_id = AMDGPU_GFXHUB(0);
-		adev->mes.funcs->invalidate_tlbs_pasid(&adev->mes, &input);
-
-		if (all_hub) {
-			/* invalidate mm_hub */
-			if (test_bit(AMDGPU_MMHUB0(0), adev->vmhubs_mask)) {
-				input.hub_id = AMDGPU_MMHUB0(0);
-				adev->mes.funcs->invalidate_tlbs_pasid(&adev->mes, &input);
-			}
-			if (test_bit(AMDGPU_MMHUB1(0), adev->vmhubs_mask)) {
-				input.hub_id = AMDGPU_MMHUB1(0);
-				adev->mes.funcs->invalidate_tlbs_pasid(&adev->mes, &input);
-			}
-		}
-		return;
-	}
-
 	for (vmid = 1; vmid < 16; vmid++) {
 		bool valid;
 
Signature · 1 line
2.55.0
author maintainer
#30 lore raw

PATCH 30/30 drm/amdgpu/gmc: add helpers for various tlb inv functions

In reply to Alex Deucher

gmc9-12 use the same logic for almost all of these, so move it to helpers and remove the gmc specific functions.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 195 +++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h |   7 +
 drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c  |   2 +
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c  | 199 +--------------------
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c  | 204 +---------------------
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c  | 219 +-----------------------
 drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c  | 195 +--------------------
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c   |  92 +---------
 8 files changed, 221 insertions(+), 892 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +195 -0
index 2800eebe50649..185290a392bec 100644
@@ -951,6 +951,201 @@ static int amdgpu_gmc_flush_gpu_tlb_mes_helper(struct amdgpu_device *adev,
 	return 0;
 }
 
+/**
+ * amdgpu_gmc_flush_gpu_tlb_pasid_helper - tlb flush via pasid
+ *
+ * @adev: amdgpu_device pointer
+ * @pasid: pasid to be flush
+ * @flush_type: the flush type
+ * @all_hub: flush all hubs
+ * @inst: is used to select which instance of KIQ to use for the invalidation
+ *
+ * A helper to flush the TLB for the requested pasid using other callbacks.
+ */
+void amdgpu_gmc_flush_gpu_tlb_pasid_helper(struct amdgpu_device *adev,
+					   uint16_t pasid, uint32_t flush_type,
+					   bool all_hub, uint32_t inst)
+{
+	uint16_t queried;
+	int vmid, i;
+
+	for (vmid = 1; vmid < 16; vmid++) {
+		bool valid;
+
+		valid = adev->gmc.gmc_funcs->get_vmid_pasid_mapping_info(adev, vmid, inst,
+									 &queried);
+		if (!valid || queried != pasid)
+			continue;
+
+		if (all_hub) {
+			for_each_set_bit(i, adev->vmhubs_mask, AMDGPU_MAX_VMHUBS)
+				adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid, i,
+								   flush_type);
+		} else {
+			adev->gmc.gmc_funcs->flush_gpu_tlb(adev, vmid, AMDGPU_GFXHUB(inst),
+							   flush_type);
+		}
+	}
+}
+
+/**
+ * amdgpu_gmc_flush_gpu_tlb_helper - gart tlb flush callback
+ *
+ * @adev: amdgpu_device pointer
+ * @vmid: vm instance to flush
+ * @vmhub: which hub to flush
+ * @flush_type: the flush type
+ *
+ * Flush the TLB for the requested page table.
+ */
+void amdgpu_gmc_flush_gpu_tlb_helper(struct amdgpu_device *adev, uint32_t vmid,
+				     uint32_t vmhub, uint32_t flush_type)
+{
+	bool use_semaphore = adev->gmc.gmc_funcs->use_invalidate_semaphore(adev, vmhub);
+	struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
+	u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
+	/* Use register 17 for GART */
+	const unsigned int eng = 17;
+	unsigned char hub_ip;
+	u32 sem, req, ack;
+	unsigned int i;
+	u32 tmp, inst;
+
+	if (AMDGPU_IS_GFXHUB(vmhub) && !adev->gfx.is_poweron)
+		return;
+
+	sem = hub->vm_inv_eng0_sem + hub->eng_distance * eng;
+	req = hub->vm_inv_eng0_req + hub->eng_distance * eng;
+	ack = hub->vm_inv_eng0_ack + hub->eng_distance * eng;
+
+
+	if (vmhub >= AMDGPU_MMHUB0(0))
+		inst = 0;
+	else
+		inst = vmhub;
+
+	/* flush hdp cache */
+	amdgpu_device_flush_hdp(adev, NULL);
+
+	/* This is necessary for SRIOV as well as for GFXOFF to function
+	 * properly under bare metal
+	 */
+	if ((adev->gfx.kiq[inst].ring.sched.ready ||
+	     adev->mes.ring[MES_PIPE_INST(inst, 0)].sched.ready) &&
+	    !adev->gmc.use_mmio_for_tlb_flush) {
+		amdgpu_gmc_fw_reg_write_reg_wait(adev, req, ack, inv_req,
+						 1 << vmid, inst);
+		return;
+	}
+
+	/* This path is needed before KIQ/MES/GFXOFF are set up */
+	hub_ip = AMDGPU_IS_GFXHUB(vmhub) ? GC_HWIP : MMHUB_HWIP;
+
+	/* disabllow gfxoff when we invalidate */
+	if (hub_ip == GC_HWIP)
+		amdgpu_gfx_off_ctrl(adev, false);
+
+	spin_lock(&adev->gmc.invalidate_lock);
+	/*
+	 * It may lose gpuvm invalidate acknowldege state across power-gating
+	 * off cycle, add semaphore acquire before invalidation and semaphore
+	 * release after invalidation to avoid entering power gated state
+	 * to WA the Issue
+	 */
+
+	/* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
+	if (use_semaphore) {
+		for (i = 0; i < adev->usec_timeout; i++) {
+			/* a read return value of 1 means semaphore acuqire */
+			tmp = RREG32_RLC_NO_KIQ(sem, hub_ip);
+			if (tmp & 0x1)
+				break;
+			udelay(1);
+		}
+
+		if (i >= adev->usec_timeout)
+			DRM_ERROR("Timeout waiting for sem acquire in VM flush!\n");
+	}
+
+	WREG32_RLC_NO_KIQ(req, inv_req, hub_ip);
+
+	/* Wait for ACK with a delay.*/
+	for (i = 0; i < adev->usec_timeout; i++) {
+		tmp = RREG32_RLC_NO_KIQ(ack, hub_ip);
+		tmp &= 1 << vmid;
+		if (tmp)
+			break;
+
+		udelay(1);
+	}
+
+	/* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
+	if (use_semaphore)
+		WREG32_RLC_NO_KIQ(sem, 0, hub_ip);
+
+	/* Issue additional private vm invalidation to MMHUB */
+	if ((vmhub != AMDGPU_GFXHUB(0)) &&
+	    (hub->vm_l2_bank_select_reserved_cid2) &&
+		!amdgpu_sriov_vf(adev)) {
+		inv_req = RREG32_NO_KIQ(hub->vm_l2_bank_select_reserved_cid2);
+		/* bit 25: RSERVED_CACHE_PRIVATE_INVALIDATION */
+		inv_req |= (1 << 25);
+		/* Issue private invalidation */
+		WREG32_NO_KIQ(hub->vm_l2_bank_select_reserved_cid2, inv_req);
+		/* Read back to ensure invalidation is done*/
+		RREG32_NO_KIQ(hub->vm_l2_bank_select_reserved_cid2);
+	}
+
+	spin_unlock(&adev->gmc.invalidate_lock);
+
+	if (hub_ip == GC_HWIP)
+		amdgpu_gfx_off_ctrl(adev, true);
+
+	if (i >= adev->usec_timeout)
+		dev_err(adev->dev, "Timeout waiting for VM flush ACK!\n");
+}
+
+uint64_t amdgpu_gmc_emit_flush_gpu_tlb_helper(struct amdgpu_ring *ring,
+					      unsigned vmid, uint64_t pd_addr)
+{
+	bool use_semaphore =
+		ring->adev->gmc.gmc_funcs->use_invalidate_semaphore(ring->adev,
+								    ring->vm_hub);
+	struct amdgpu_vmhub *hub = &ring->adev->vmhub[ring->vm_hub];
+	uint32_t req = hub->vmhub_funcs->get_invalidate_req(vmid, 0);
+	unsigned eng = ring->vm_inv_eng;
+
+	if (use_semaphore)
+		/* a read return value of 1 means semaphore acuqire */
+		amdgpu_ring_emit_reg_wait(ring,
+					  hub->vm_inv_eng0_sem +
+					  hub->eng_distance * eng, 0x1, 0x1);
+
+	amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_lo32 +
+			      (hub->ctx_addr_distance * vmid),
+			      lower_32_bits(pd_addr));
+
+	amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_hi32 +
+			      (hub->ctx_addr_distance * vmid),
+			      upper_32_bits(pd_addr));
+
+	amdgpu_ring_emit_reg_write_reg_wait(ring, hub->vm_inv_eng0_req +
+					    hub->eng_distance * eng,
+					    hub->vm_inv_eng0_ack +
+					    hub->eng_distance * eng,
+					    req, 1 << vmid);
+
+	if (use_semaphore)
+		/*
+		 * add semaphore release after invalidation,
+		 * write with 0 means semaphore release
+		 */
+		amdgpu_ring_emit_wreg(ring, hub->vm_inv_eng0_sem +
+				      hub->eng_distance * eng, 0);
+
+	return pd_addr;
+}
+
 int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
 				   uint32_t flush_type, bool all_hub,
 				   uint32_t inst)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h +7 -0
index f20f08630408e..22814dd451430 100644
@@ -459,6 +459,13 @@ int amdgpu_gmc_handle_retry_fault(struct amdgpu_device *adev,
 				  bool write_fault);
 int amdgpu_gmc_ras_sw_init(struct amdgpu_device *adev);
 int amdgpu_gmc_allocate_vm_inv_eng(struct amdgpu_device *adev);
+void amdgpu_gmc_flush_gpu_tlb_pasid_helper(struct amdgpu_device *adev,
+					   uint16_t pasid, uint32_t flush_type,
+					   bool all_hub, uint32_t inst);
+void amdgpu_gmc_flush_gpu_tlb_helper(struct amdgpu_device *adev, uint32_t vmid,
+				     uint32_t vmhub, uint32_t flush_type);
+uint64_t amdgpu_gmc_emit_flush_gpu_tlb_helper(struct amdgpu_ring *ring,
+					      unsigned vmid, uint64_t pd_addr);
 void amdgpu_gmc_flush_gpu_tlb_gart(struct amdgpu_device *adev,
 				   uint32_t vmhub);
 int amdgpu_gmc_flush_gpu_tlb_pasid(struct amdgpu_device *adev, uint16_t pasid,
drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c +2 -0
index 5033f85d31022..654bf7f780d59 100644
@@ -7497,6 +7497,8 @@ static int gfx_v10_0_hw_init(struct amdgpu_ip_block *ip_block)
 	int r;
 	struct amdgpu_device *adev = ip_block->adev;
 
+	adev->gfx.is_poweron = true;
+
 	if (!amdgpu_emu_mode)
 		gfx_v10_0_init_golden_registers(adev);
 
drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c +5 -194

218 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c +4 -200

222 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c +3 -216

231 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c +3 -192

207 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +2 -90

104 lines not on this page · raw message

Signature · 1 line
2.55.0
#31 lore raw
In reply to Alex Deucher
Quoted 15 lines · Alex Deucher

GMC 9-12 use KIQ or MES for TLB invalidations to avoid using MMIO which would require disallowing GFXOFF. KIQ and MES are management queues however and if they hang, they cannot be recovered by a queue reset since they are the mechanisms which handle queue resets. Since using KIQ or MES will exit GFXOFF anyway, explicitly disallow it on the MMIO path and use that. Next, switch to using SDMA for TLB invalidations. SDMA 4.4.x and newer have special packets specifically for this purpose. If SDMA hangs while doing the invalidation for some reason, it's easier to reset the SDMA queue than KIQ or MES. Finally, most of the TLB invalidation code between GMC 9 through 12 was identical, so move it to common GMC helpers and remove the IP specific code. If the SMDA and MMIO pathes prove to be stable, the KIQ pathes can be removed in the future to further simplify things. SDMA 4.x could also be updated to support PASID invalidation via SDMA using either the new packet (SDMA 4.4.x) or via REG_WRITE/REG_WAIT packets (SDMA 4.0.x).

Some older gfxhubs and mmhubs will also need this patch:
https://gitlab.freedesktop.org/agd5f/linux/-/commit/a1f1823a8ecd80bee660244def5f20179926fe8c

Updated patches with that included here:
https://gitlab.freedesktop.org/agd5f/linux/-/commits/tlb_inv_rework?ref_type=heads

Alex

Quoted 55 lines

Alex Deucher (30):
drm/amdgpu/gmc9: disallow gfxoff around TLB flushes drm/amdgpu/gmc10: disallow gfxoff around TLB flushes drm/amdgpu/gmc11: disallow gfxoff around TLB flushes drm/amdgpu/gmc12: disallow gfxoff around TLB flushes drm/amdgpu: add a gmc flag for using MMIO for TLB flush drm/amdgpu/gmc9: use MMIO for TLB flushes drm/amdgpu/gmc10: use MMIO for TLB flushes drm/amdgpu/gmc11: use MMIO for TLB flushes drm/amdgpu/gmc12: use MMIO for TLB flushes
drm/amdgpu: add a buffer funcs callback for TLB invalidation drm/amdgpu/sdma5.0: add tlb invalidation buffer func callback drm/amdgpu/sdma5.2: add tlb invalidation buffer func callback drm/amdgpu/sdma6: add tlb invalidation buffer func callback drm/amdgpu/sdma7: add tlb invalidation buffer func callback drm/amdgpu: simplify amdgpu_gmc_flush_gpu_tlb() drm/amdgpu: add tlb invalidation method enum
drm/amdgpu: plumb tlb inv method in amdgpu_gmc_flush_gpu_tlb_gart() drm/amdgpu: uplevel reset check in amdgpu_gmc_flush_gpu_tlb_gart() drm/amdgpu/gmc: add new callback to lookup vmid to pasid mapping drm/amdgpu: add a gmc callback for the inv semaphore drm/amdgpu/gmc: rework pasid flushing
drm/amdgpu/gmc9: use SDMA for gart TLB invalidation drm/amdgpu/gmc10: use SDMA for gart TLB invalidation drm/amdgpu/gmc11: use SDMA for gart TLB invalidation drm/amdgpu/gmc12: use SDMA for gart TLB invalidation drm/amdgpu/gmc10: use SDMA for pasid TLB invalidation drm/amdgpu/gmc11: use SDMA for pasid TLB invalidation drm/amdgpu/gmc12: use MES or SDMA for pasid TLB invalidation drm/amdgpu/gmc12: drop MES tlb inv in gmc callbacks drm/amdgpu/gmc: add helpers for various tlb inv functions

 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c |   2 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c  | 486 +++++++++++++++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.h  |  33 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_sdma.h |  18 +
 drivers/gpu/drm/amd/amdgpu/gfx_v10_0.c   |   2 +
 drivers/gpu/drm/amd/amdgpu/gmc_v10_0.c   | 202 +---------
 drivers/gpu/drm/amd/amdgpu/gmc_v11_0.c   | 207 +---------
 drivers/gpu/drm/amd/amdgpu/gmc_v12_0.c   | 250 ++----------
 drivers/gpu/drm/amd/amdgpu/gmc_v12_1.c   | 225 +----------
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c    | 114 +-----
 drivers/gpu/drm/amd/amdgpu/mes_v12_0.c   |   4 +
 drivers/gpu/drm/amd/amdgpu/mes_v12_1.c   |   4 +
 drivers/gpu/drm/amd/amdgpu/sdma_v5_0.c   |  49 +++
 drivers/gpu/drm/amd/amdgpu/sdma_v5_2.c   |  49 +++
 drivers/gpu/drm/amd/amdgpu/sdma_v6_0.c   |  49 +++
 drivers/gpu/drm/amd/amdgpu/sdma_v7_0.c   |  48 +++
 16 files changed, 747 insertions(+), 995 deletions(-)

--
2.55.0