From: Jason Gunthorpe <[email protected]> Move amdgpu's map and unmap callbacks to the SGT exporter ops through DMA_BUF_SIMPLE_SGT_EXP_MATCH() and take the importing device from the match entry with dma_buf_sgt_dma_device().
The SGT exporter ops return a struct dma_buf_sgt_mapping, so amdgpu_dma_buf_map() must allocate one and build the table in it. The GTT path takes over the entries of the table drm_prime_pages_to_sg() returns, and amdgpu_vram_mgr_alloc_sgt(), amdgpu_ttm_mmio_remap_alloc_sgt() and their free counterparts fill and tear down a caller-provided table instead of doing allocation and free themselves. Signed-off-by: Jason Gunthorpe <[email protected]> Signed-off-by: Alistair Popple <[email protected]> --- drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c | 85 +++++++++++++++++----------- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 25 +++----- drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 4 +- drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c | 30 ++++------ 4 files changed, 74 insertions(+), 70 deletions(-) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c index bbe518757907..7b110405b08d 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c @@ -41,6 +41,7 @@ #include <drm/amdgpu_drm.h> #include <drm/ttm/ttm_tt.h> #include <linux/dma-buf.h> +#include <linux/dma-buf-mapping.h> #include <linux/dma-fence-array.h> #include <linux/pci-p2pdma.h> #include <linux/pm_runtime.h> @@ -98,7 +99,8 @@ static int amdgpu_dma_buf_attach(struct dma_buf *dmabuf, attach->peer2peer = false; if (!amdgpu_dmabuf_is_xgmi_accessible(attach_adev, bo) && - pci_p2pdma_distance(adev->pdev, attach->dev, false) < 0) + pci_p2pdma_distance(adev->pdev, dma_buf_sgt_dma_device(attach), + false) < 0) attach->peer2peer = false; /* @@ -203,7 +205,7 @@ static void amdgpu_dma_buf_unpin(struct dma_buf_attachment *attach) } /** - * amdgpu_dma_buf_map - &dma_buf_ops.map_dma_buf implementation + * amdgpu_dma_buf_map - &dma_buf_mapping_sgt_exp_ops.map_dma_buf implementation * @attach: DMA-buf attachment * @dir: DMA direction * @@ -215,13 +217,16 @@ static void amdgpu_dma_buf_unpin(struct dma_buf_attachment *attach) * sg_table filled with the DMA addresses to use or ERR_PRT with negative error * code. */ -static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach, - enum dma_data_direction dir) +static struct dma_buf_sgt_mapping * +amdgpu_dma_buf_map(struct dma_buf_attachment *attach, + enum dma_data_direction dir) { + struct device *dma_dev = dma_buf_sgt_dma_device(attach); struct dma_buf *dma_buf = attach->dmabuf; struct drm_gem_object *obj = dma_buf->priv; struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); + struct dma_buf_sgt_mapping *mapping; struct sg_table *sgt; long r; @@ -241,80 +246,95 @@ static struct sg_table *amdgpu_dma_buf_map(struct dma_buf_attachment *attach, return ERR_PTR(r); } + mapping = kzalloc_obj(*mapping); + if (!mapping) + return ERR_PTR(-ENOMEM); + switch (bo->tbo.resource->mem_type) { case TTM_PL_TT: sgt = drm_prime_pages_to_sg(obj->dev, bo->tbo.ttm->pages, bo->tbo.ttm->num_pages); - if (IS_ERR(sgt)) - return sgt; + if (IS_ERR(sgt)) { + r = PTR_ERR(sgt); + goto error_free_mapping; + } + /* The mapping takes over the table's entries */ + mapping->sgt = *sgt; + kfree(sgt); - if (dma_map_sgtable(attach->dev, sgt, dir, - DMA_ATTR_SKIP_CPU_SYNC)) + if (dma_map_sgtable(dma_dev, &mapping->sgt, dir, + DMA_ATTR_SKIP_CPU_SYNC)) { + r = -EBUSY; goto error_free; + } break; case TTM_PL_VRAM: /* XGMI-accessible memory should never be DMA-mapped */ if (WARN_ON(amdgpu_dmabuf_is_xgmi_accessible( - dma_buf_attach_adev(attach), bo))) - return ERR_PTR(-EINVAL); + dma_buf_attach_adev(attach), bo))) { + r = -EINVAL; + goto error_free_mapping; + } r = amdgpu_vram_mgr_alloc_sgt(adev, bo->tbo.resource, 0, - bo->tbo.base.size, attach->dev, - dir, &sgt); + bo->tbo.base.size, dma_dev, + dir, &mapping->sgt); if (r) - return ERR_PTR(r); + goto error_free_mapping; break; case AMDGPU_PL_MMIO_REMAP: r = amdgpu_ttm_mmio_remap_alloc_sgt(adev, bo->tbo.resource, - attach->dev, dir, &sgt); + dma_dev, dir, + &mapping->sgt); if (r) - return ERR_PTR(r); + goto error_free_mapping; break; default: - return ERR_PTR(-EINVAL); + r = -EINVAL; + goto error_free_mapping; } - return sgt; + return mapping; error_free: - sg_free_table(sgt); - kfree(sgt); - return ERR_PTR(-EBUSY); + sg_free_table(&mapping->sgt); +error_free_mapping: + kfree(mapping); + return ERR_PTR(r); } /** - * amdgpu_dma_buf_unmap - &dma_buf_ops.unmap_dma_buf implementation + * amdgpu_dma_buf_unmap - &dma_buf_mapping_sgt_exp_ops.unmap_dma_buf implementation * @attach: DMA-buf attachment - * @sgt: sg_table to unmap + * @mapping: mapping to unmap * @dir: DMA direction * * This is called when a shared DMA buffer no longer needs to be accessible by * another device. For now, simply unpins the buffer from GTT. */ static void amdgpu_dma_buf_unmap(struct dma_buf_attachment *attach, - struct sg_table *sgt, + struct dma_buf_sgt_mapping *mapping, enum dma_data_direction dir) { + struct device *dma_dev = dma_buf_sgt_mapping_dma_device(mapping); struct drm_gem_object *obj = attach->dmabuf->priv; struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); + struct sg_table *sgt = &mapping->sgt; if (bo->tbo.resource && bo->tbo.resource->mem_type == AMDGPU_PL_MMIO_REMAP) { - amdgpu_ttm_mmio_remap_free_sgt(attach->dev, dir, sgt); - return; - } - - if (sg_page(sgt->sgl)) { - dma_unmap_sgtable(attach->dev, sgt, dir, 0); + amdgpu_ttm_mmio_remap_free_sgt(dma_dev, dir, sgt); + } else if (sg_page(sgt->sgl)) { + dma_unmap_sgtable(dma_dev, sgt, dir, 0); sg_free_table(sgt); - kfree(sgt); } else { - amdgpu_vram_mgr_free_sgt(attach->dev, dir, sgt); + amdgpu_vram_mgr_free_sgt(dma_dev, dir, sgt); } + kfree(mapping); } /** @@ -393,13 +413,12 @@ const struct dma_buf_ops amdgpu_dmabuf_ops = { .detach = amdgpu_dma_buf_detach, .pin = amdgpu_dma_buf_pin, .unpin = amdgpu_dma_buf_unpin, - .map_dma_buf = amdgpu_dma_buf_map, - .unmap_dma_buf = amdgpu_dma_buf_unmap, .release = drm_gem_dmabuf_release, .begin_cpu_access = amdgpu_dma_buf_begin_cpu_access, .mmap = drm_gem_dmabuf_mmap, .vmap = amdgpu_dma_buf_vmap, .vunmap = amdgpu_dma_buf_vunmap, + DMA_BUF_SIMPLE_SGT_EXP_MATCH(amdgpu_dma_buf_map, amdgpu_dma_buf_unmap), }; /** diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c index 30ec5cb9ddf5..9104b36f6184 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c @@ -1114,9 +1114,9 @@ static void amdgpu_ttm_backend_destroy(struct ttm_device *bdev, * amdgpu_ttm_mmio_remap_alloc_sgt - build an sg_table for MMIO_REMAP I/O aperture * @adev: amdgpu device providing the remap BAR base (adev->rmmio_remap.bus_addr) * @res: TTM resource of the BO to export; expected to live in AMDGPU_PL_MMIO_REMAP - * @dev: importing device to map for (typically @attach->dev in dma-buf paths) + * @dev: importing device to map for (dma_buf_sgt_dma_device() in dma-buf paths) * @dir: DMA data direction for the importer (passed to dma_map_resource()) - * @sgt: output; on success, set to a newly allocated sg_table describing the I/O span + * @sgt: sg_table to fill with the I/O span * * The HDP flush page (AMDGPU_PL_MMIO_REMAP) is a fixed hardware I/O window in a PCI * BAR—there are no struct pages to back it. Importers still need a DMA address list, @@ -1131,9 +1131,9 @@ static void amdgpu_ttm_backend_destroy(struct ttm_device *bdev, * - Peer reachability / p2pdma policy checks must be done by the caller. * * Return: - * * 0 on success, with *@sgt set to a valid table that must be freed via + * * 0 on success, with @sgt filled; tear it down via * amdgpu_ttm_mmio_remap_free_sgt(). - * * -ENOMEM if allocation of the sg_table fails. + * * -ENOMEM if allocation of the sg_table entries fails. * * -EIO if dma_map_resource() fails. * */ @@ -1141,7 +1141,7 @@ int amdgpu_ttm_mmio_remap_alloc_sgt(struct amdgpu_device *adev, struct ttm_resource *res, struct device *dev, enum dma_data_direction dir, - struct sg_table **sgt) + struct sg_table *sgt) { struct amdgpu_res_cursor cur; dma_addr_t dma; @@ -1156,21 +1156,15 @@ int amdgpu_ttm_mmio_remap_alloc_sgt(struct amdgpu_device *adev, phys = adev->rmmio_remap.bus_addr + cur.start; /* Build a single-entry sg_table mapped as I/O (no struct page backing). */ - *sgt = kzalloc_obj(**sgt); - if (!*sgt) - return -ENOMEM; - r = sg_alloc_table(*sgt, 1, GFP_KERNEL); - if (r) { - kfree(*sgt); + r = sg_alloc_table(sgt, 1, GFP_KERNEL); + if (r) return r; - } - sg = (*sgt)->sgl; + sg = sgt->sgl; sg_set_page(sg, NULL, cur.size, 0); /* WHY: I/O space → no pages */ dma = dma_map_resource(dev, phys, cur.size, dir, DMA_ATTR_SKIP_CPU_SYNC); if (dma_mapping_error(dev, dma)) { - sg_free_table(*sgt); - kfree(*sgt); + sg_free_table(sgt); return -EIO; } sg_dma_address(sg) = dma; @@ -1187,7 +1181,6 @@ void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev, dma_unmap_resource(dev, sg_dma_address(sg), sg_dma_len(sg), dir, DMA_ATTR_SKIP_CPU_SYNC); sg_free_table(sgt); - kfree(sgt); } /** diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h index adb6cfae5052..c20db985137a 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h @@ -164,7 +164,7 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev, u64 offset, u64 size, struct device *dev, enum dma_data_direction dir, - struct sg_table **sgt); + struct sg_table *sgt); void amdgpu_vram_mgr_free_sgt(struct device *dev, enum dma_data_direction dir, struct sg_table *sgt); @@ -266,7 +266,7 @@ int amdgpu_ttm_mmio_remap_alloc_sgt(struct amdgpu_device *adev, struct ttm_resource *res, struct device *dev, enum dma_data_direction dir, - struct sg_table **sgt); + struct sg_table *sgt); void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev, enum dma_data_direction dir, struct sg_table *sgt); diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c index 249f07f30951..3d245467b983 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vram_mgr.c @@ -671,26 +671,22 @@ static void amdgpu_vram_mgr_del(struct ttm_resource_manager *man, * @length: number of bytes to export in sg_table * @dev: the other device * @dir: dma direction - * @sgt: resulting sg table + * @sgt: sg table to fill * - * Allocate and fill a sg table from a VRAM allocation. + * Fill a sg table from a VRAM allocation. */ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev, struct ttm_resource *res, u64 offset, u64 length, struct device *dev, enum dma_data_direction dir, - struct sg_table **sgt) + struct sg_table *sgt) { struct amdgpu_res_cursor cursor; struct scatterlist *sg; int num_entries = 0; int i, r; - *sgt = kmalloc_obj(**sgt); - if (!*sgt) - return -ENOMEM; - /* Determine the number of GPU_BUDDY blocks to export */ amdgpu_res_first(res, offset, length, &cursor); while (cursor.remaining) { @@ -698,12 +694,12 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev, amdgpu_res_next(&cursor, min(cursor.size, AMDGPU_MAX_SG_SEGMENT_SIZE)); } - r = sg_alloc_table(*sgt, num_entries, GFP_KERNEL); + r = sg_alloc_table(sgt, num_entries, GFP_KERNEL); if (r) - goto error_free; + return r; /* Initialize scatterlist nodes of sg_table */ - for_each_sgtable_sg((*sgt), sg, i) + for_each_sgtable_sg(sgt, sg, i) sg->length = 0; /* @@ -713,7 +709,7 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev, * GPU_BUDDY block(s) if more buffer needs to exported */ amdgpu_res_first(res, offset, length, &cursor); - for_each_sgtable_sg((*sgt), sg, i) { + for_each_sgtable_sg(sgt, sg, i) { phys_addr_t phys = cursor.start + adev->gmc.aper_base; unsigned long size = min(cursor.size, AMDGPU_MAX_SG_SEGMENT_SIZE); dma_addr_t addr; @@ -734,7 +730,7 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev, return 0; error_unmap: - for_each_sgtable_sg((*sgt), sg, i) { + for_each_sgtable_sg(sgt, sg, i) { if (!sg->length) continue; @@ -742,10 +738,7 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev, sg->length, dir, DMA_ATTR_SKIP_CPU_SYNC); } - sg_free_table(*sgt); - -error_free: - kfree(*sgt); + sg_free_table(sgt); return r; } @@ -754,9 +747,9 @@ int amdgpu_vram_mgr_alloc_sgt(struct amdgpu_device *adev, * * @dev: device pointer * @dir: data direction of resource to unmap - * @sgt: sg table to free + * @sgt: sg table to tear down * - * Free a previously allocate sg table. + * Unmap and free the entries of a table filled by amdgpu_vram_mgr_alloc_sgt(). */ void amdgpu_vram_mgr_free_sgt(struct device *dev, enum dma_data_direction dir, @@ -770,7 +763,6 @@ void amdgpu_vram_mgr_free_sgt(struct device *dev, sg->length, dir, DMA_ATTR_SKIP_CPU_SYNC); sg_free_table(sgt); - kfree(sgt); } /** -- 2.54.0
