From c191515b8df0e47877a2976dac535fd4528bbb3a Mon Sep 17 00:00:00 2001
From: Ramesh Errabolu <Ramesh.Errabolu@amd.com>
Date: Fri, 14 Oct 2022 22:26:57 -0500
Subject: [PATCH] drm/amdgpu: Query P2PDMA distance only if CONFIG_HSA_AMD_P2P
 is defined

CONFIG_HSA_AMD_P2P indicates the requirements that are needed for P2P DMA
Mappings. It is important to note that enabling CONFIG_HSA_AMD_P2P is a
necessary but insufficient condition. It is possible to encounter runtime
errors - e.g. CPU's do not support Inter-CPU transport of PCIe transaction
packets i.e. P2PDMA distance API may return error

Signed-off-by: Ramesh Errabolu <Ramesh.Errabolu@amd.com>
Reviewed-by: Felix Kuehling <Felix.Kuehling@amd.com>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
index caba3e60dc135..828f6d6690068 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
@@ -5577,6 +5577,12 @@ static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
  * Return true if @peer_adev can access (DMA) @adev through the PCIe
  * BAR, i.e. @adev is "large BAR" and the BAR matches the DMA mask of
  * @peer_adev.
+ *
+ * @note: CONFIG_HSA_AMD_P2P indicates support for P2P DMA mappings. Query
+ * P2PDMA distance only if the kernel has all the prerequisites for P2P DMA
+ * support. Otherwise fall back to the less reliable legacy P2P support to
+ * avoid regressions.
+ *
  */
 bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev,
 				      struct amdgpu_device *peer_adev)
@@ -5587,7 +5593,7 @@ bool amdgpu_device_is_peer_accessible(struct amdgpu_device *adev,
 	resource_size_t aper_limit =
 		adev->gmc.aper_base + adev->gmc.aper_size - 1;
 
-#ifdef CONFIG_PCI_P2PDMA
+#ifdef CONFIG_HSA_AMD_P2P
 	p2p_access = !adev->gmc.xgmi.connected_to_cpu &&
 		     !(pci_p2pdma_distance_many(adev->pdev,
 				&peer_adev->dev, 1, true) < 0);