drm/amdgpu: Allow P2P access through XGMI
authorFelix Kuehling <felix.kuehling@amd.com>
Wed, 16 Apr 2025 04:19:13 +0000 (00:19 -0400)
committerAlex Deucher <alexander.deucher@amd.com>
Mon, 21 Apr 2025 15:28:36 +0000 (11:28 -0400)
If peer memory is accessible through XGMI, allow leaving it in VRAM
rather than forcing its migration to GTT on DMABuf attachment.

Signed-off-by: Felix Kuehling <felix.kuehling@amd.com>
Tested-by: Hao (Claire) Zhou <hao.zhou@amd.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c

index 5740e8d1a5226e54267a247e84369713a359afd0..e6913fcf2c7be2a7a15708c0540cb8b1863d9b4b 100644 (file)
 #include <linux/dma-fence-array.h>
 #include <linux/pci-p2pdma.h>
 
+static const struct dma_buf_attach_ops amdgpu_dma_buf_attach_ops;
+
+/**
+ * dma_buf_attach_adev - Helper to get adev of an attachment
+ *
+ * @attach: attachment
+ *
+ * Returns:
+ * A struct amdgpu_device * if the attaching device is an amdgpu device or
+ * partition, NULL otherwise.
+ */
+static struct amdgpu_device *dma_buf_attach_adev(struct dma_buf_attachment *attach)
+{
+       if (attach->importer_ops == &amdgpu_dma_buf_attach_ops) {
+               struct drm_gem_object *obj = attach->importer_priv;
+               struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
+
+               return amdgpu_ttm_adev(bo->tbo.bdev);
+       }
+
+       return NULL;
+}
+
 /**
  * amdgpu_dma_buf_attach - &dma_buf_ops.attach implementation
  *
 static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf,
                                 struct dma_buf_attachment *attach)
 {
+       struct amdgpu_device *attach_adev = dma_buf_attach_adev(attach);
        struct drm_gem_object *obj = dmabuf->priv;
        struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
        struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
 
-       if (pci_p2pdma_distance(adev->pdev, attach->dev, false) < 0)
+       if (!amdgpu_dmabuf_is_xgmi_accessible(attach_adev, bo) &&
+           pci_p2pdma_distance(adev->pdev, attach->dev, false) < 0)
                attach->peer2peer = false;
 
        amdgpu_vm_bo_update_shared(bo);
@@ -480,6 +505,9 @@ bool amdgpu_dmabuf_is_xgmi_accessible(struct amdgpu_device *adev,
        struct drm_gem_object *obj = &bo->tbo.base;
        struct drm_gem_object *gobj;
 
+       if (!adev)
+               return false;
+
        if (obj->import_attach) {
                struct dma_buf *dma_buf = obj->import_attach->dmabuf;