Skip to content

Commit 5ac3c3e

Browse files
fxkamdalexdeucher
authored andcommitted
drm/amdgpu: Add DMA mapping of GTT BOs
Use DMABufs with dynamic attachment to DMA-map GTT BOs on other GPUs. Signed-off-by: Felix Kuehling <[email protected]> Acked-by: Oak Zeng <[email protected]> Acked-by: Ramesh Errabolu <[email protected]> Signed-off-by: Alex Deucher <[email protected]>
1 parent 9e5d275 commit 5ac3c3e

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct amdgpu_device;
4747
enum kfd_mem_attachment_type {
4848
KFD_MEM_ATT_SHARED, /* Share kgd_mem->bo or another attachment's */
4949
KFD_MEM_ATT_USERPTR, /* SG bo to DMA map pages from a userptr bo */
50+
KFD_MEM_ATT_DMABUF, /* DMAbuf to DMA map TTM BOs */
5051
};
5152

5253
struct kfd_mem_attachment {
@@ -62,6 +63,7 @@ struct kfd_mem_attachment {
6263
struct kgd_mem {
6364
struct mutex lock;
6465
struct amdgpu_bo *bo;
66+
struct dma_buf *dmabuf;
6567
struct list_head attachments;
6668
/* protected by amdkfd_process_info.lock */
6769
struct ttm_validate_buffer validate_list;

drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,16 @@ kfd_mem_dmamap_userptr(struct kgd_mem *mem,
529529
return ret;
530530
}
531531

532+
static int
533+
kfd_mem_dmamap_dmabuf(struct kfd_mem_attachment *attachment)
534+
{
535+
struct ttm_operation_ctx ctx = {.interruptible = true};
536+
struct amdgpu_bo *bo = attachment->bo_va->base.bo;
537+
538+
amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
539+
return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
540+
}
541+
532542
static int
533543
kfd_mem_dmamap_attachment(struct kgd_mem *mem,
534544
struct kfd_mem_attachment *attachment)
@@ -538,6 +548,8 @@ kfd_mem_dmamap_attachment(struct kgd_mem *mem,
538548
return 0;
539549
case KFD_MEM_ATT_USERPTR:
540550
return kfd_mem_dmamap_userptr(mem, attachment);
551+
case KFD_MEM_ATT_DMABUF:
552+
return kfd_mem_dmamap_dmabuf(attachment);
541553
default:
542554
WARN_ON_ONCE(1);
543555
}
@@ -567,6 +579,19 @@ kfd_mem_dmaunmap_userptr(struct kgd_mem *mem,
567579
ttm->sg = NULL;
568580
}
569581

582+
static void
583+
kfd_mem_dmaunmap_dmabuf(struct kfd_mem_attachment *attachment)
584+
{
585+
struct ttm_operation_ctx ctx = {.interruptible = true};
586+
struct amdgpu_bo *bo = attachment->bo_va->base.bo;
587+
588+
amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
589+
ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
590+
/* FIXME: This does not guarantee that amdgpu_ttm_tt_unpopulate is
591+
* called
592+
*/
593+
}
594+
570595
static void
571596
kfd_mem_dmaunmap_attachment(struct kgd_mem *mem,
572597
struct kfd_mem_attachment *attachment)
@@ -577,6 +602,9 @@ kfd_mem_dmaunmap_attachment(struct kgd_mem *mem,
577602
case KFD_MEM_ATT_USERPTR:
578603
kfd_mem_dmaunmap_userptr(mem, attachment);
579604
break;
605+
case KFD_MEM_ATT_DMABUF:
606+
kfd_mem_dmaunmap_dmabuf(attachment);
607+
break;
580608
default:
581609
WARN_ON_ONCE(1);
582610
}
@@ -610,6 +638,38 @@ kfd_mem_attach_userptr(struct amdgpu_device *adev, struct kgd_mem *mem,
610638
return 0;
611639
}
612640

641+
static int
642+
kfd_mem_attach_dmabuf(struct amdgpu_device *adev, struct kgd_mem *mem,
643+
struct amdgpu_bo **bo)
644+
{
645+
struct drm_gem_object *gobj;
646+
647+
if (!mem->dmabuf) {
648+
mem->dmabuf = amdgpu_gem_prime_export(&mem->bo->tbo.base,
649+
mem->alloc_flags & KFD_IOC_ALLOC_MEM_FLAGS_WRITABLE ?
650+
DRM_RDWR : 0);
651+
if (IS_ERR(mem->dmabuf)) {
652+
mem->dmabuf = NULL;
653+
return PTR_ERR(mem->dmabuf);
654+
}
655+
}
656+
657+
gobj = amdgpu_gem_prime_import(&adev->ddev, mem->dmabuf);
658+
if (IS_ERR(gobj))
659+
return PTR_ERR(gobj);
660+
661+
/* Import takes an extra reference on the dmabuf. Drop it now to
662+
* avoid leaking it. We only need the one reference in
663+
* kgd_mem->dmabuf.
664+
*/
665+
dma_buf_put(mem->dmabuf);
666+
667+
*bo = gem_to_amdgpu_bo(gobj);
668+
(*bo)->parent = amdgpu_bo_ref(mem->bo);
669+
670+
return 0;
671+
}
672+
613673
/* kfd_mem_attach - Add a BO to a VM
614674
*
615675
* Everything that needs to bo done only once when a BO is first added
@@ -667,8 +727,20 @@ static int kfd_mem_attach(struct amdgpu_device *adev, struct kgd_mem *mem,
667727
ret = kfd_mem_attach_userptr(adev, mem, &bo[i]);
668728
if (ret)
669729
goto unwind;
730+
} else if (mem->domain == AMDGPU_GEM_DOMAIN_GTT &&
731+
mem->bo->tbo.type != ttm_bo_type_sg) {
732+
/* GTT BOs use DMA-mapping ability of dynamic-attach
733+
* DMA bufs. TODO: The same should work for VRAM on
734+
* large-BAR GPUs.
735+
*/
736+
attachment[i]->type = KFD_MEM_ATT_DMABUF;
737+
ret = kfd_mem_attach_dmabuf(adev, mem, &bo[i]);
738+
if (ret)
739+
goto unwind;
670740
} else {
671-
/* FIXME: Need to DMA-map other BO types */
741+
/* FIXME: Need to DMA-map other BO types:
742+
* large-BAR VRAM, doorbells, MMIO remap
743+
*/
672744
attachment[i]->type = KFD_MEM_ATT_SHARED;
673745
bo[i] = mem->bo;
674746
drm_gem_object_get(&bo[i]->tbo.base);
@@ -1527,6 +1599,8 @@ int amdgpu_amdkfd_gpuvm_free_memory_of_gpu(
15271599

15281600
/* Free the BO*/
15291601
drm_vma_node_revoke(&mem->bo->tbo.base.vma_node, drm_priv);
1602+
if (mem->dmabuf)
1603+
dma_buf_put(mem->dmabuf);
15301604
drm_gem_object_put(&mem->bo->tbo.base);
15311605
mutex_destroy(&mem->lock);
15321606
kfree(mem);

0 commit comments

Comments
 (0)