Xe DMA maps its DMA-bufs for the importing device itself. A buffer in system memory has its pages put in an sg_table and mapped with dma_map_sgtable(). A buffer in VRAM has the address of each of its blocks in the VRAM BAR mapped with dma_map_resource() and put in an sg_table without pages. However nothing in either path depends on the driver other than where the buffer currently is, and the VRAM path duplicates what the dma-shared-buf code now does for exporters of MMIO. Instead hand out a dma_shared_buf through map_dma_shared_buf() and unmap_dma_shared_buf(). The dma_shared_buf for a buffer in system memory is built from its pages by dma_shared_buf_from_pages(). A buffer in VRAM is a list of its blocks with the p2pdma provider of the VRAM BAR, so call pcim_p2pdma_init() at probe to have one. The placement is still decided when the importer maps, and importers without allow_peer2peer still have the buffer migrated to system memory first, so the dma_shared_buf always matches what the core will hand that importer. This removes open-coding of VRAM mapping, instead using the PCI peer-to-peer core to correctly map it, and allows iommufd to create mappings for GPU buffers. Signed-off-by: Alistair Popple --- drivers/gpu/drm/xe/xe_dma_buf.c | 55 +++++----------- drivers/gpu/drm/xe/xe_ttm_vram_mgr.c | 117 +++++++++++++---------------------- drivers/gpu/drm/xe/xe_ttm_vram_mgr.h | 13 ++-- drivers/gpu/drm/xe/xe_vram.c | 21 +++++++ drivers/gpu/drm/xe/xe_vram.h | 2 + 5 files changed, 84 insertions(+), 124 deletions(-) diff --git a/drivers/gpu/drm/xe/xe_dma_buf.c b/drivers/gpu/drm/xe/xe_dma_buf.c index 97299ca8f831..8727ca456f2f 100644 --- a/drivers/gpu/drm/xe/xe_dma_buf.c +++ b/drivers/gpu/drm/xe/xe_dma_buf.c @@ -111,14 +111,14 @@ static void xe_dma_buf_unpin(struct dma_buf_attachment *attach) xe_bo_unpin_external(bo); } -static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach, - enum dma_data_direction dir) +static struct dma_shared_buf * +xe_dma_buf_map_dma_shared_buf(struct dma_buf_attachment *attach) { struct dma_buf *dma_buf = attach->dmabuf; struct drm_gem_object *obj = dma_buf->priv; struct xe_bo *bo = gem_to_xe_bo(obj); struct drm_exec *exec = XE_VALIDATION_UNSUPPORTED; - struct sg_table *sgt; + struct dma_shared_buf *shared_buf; int r = 0; if (xe_bo_is_purged(bo)) @@ -138,49 +138,24 @@ static struct sg_table *xe_dma_buf_map(struct dma_buf_attachment *attach, switch (bo->ttm.resource->mem_type) { case XE_PL_TT: - sgt = drm_prime_pages_to_sg(obj->dev, - bo->ttm.ttm->pages, - obj->size >> PAGE_SHIFT); - if (IS_ERR(sgt)) - return sgt; - - if (dma_map_sgtable(attach->dev, sgt, dir, - DMA_ATTR_SKIP_CPU_SYNC)) - goto error_free; - break; - + shared_buf = dma_shared_buf_from_pages(bo->ttm.ttm->pages, + obj->size >> PAGE_SHIFT); + if (!IS_ERR(shared_buf)) + shared_buf->attrs = DMA_ATTR_SKIP_CPU_SYNC; + return shared_buf; case XE_PL_VRAM0: case XE_PL_VRAM1: - r = xe_ttm_vram_mgr_alloc_sgt(xe_bo_device(bo), - bo->ttm.resource, 0, - bo->ttm.base.size, attach->dev, - dir, &sgt); - if (r) - return ERR_PTR(r); - break; + return xe_ttm_vram_mgr_alloc_shared_buf(xe_bo_device(bo), + bo->ttm.resource); default: return ERR_PTR(-EINVAL); } - - return sgt; - -error_free: - sg_free_table(sgt); - kfree(sgt); - return ERR_PTR(-EBUSY); } -static void xe_dma_buf_unmap(struct dma_buf_attachment *attach, - struct sg_table *sgt, - enum dma_data_direction dir) +static void xe_dma_buf_unmap_dma_shared_buf(struct dma_buf_attachment *attach, + struct dma_shared_buf *shared_buf) { - if (sg_page(sgt->sgl)) { - dma_unmap_sgtable(attach->dev, sgt, dir, 0); - sg_free_table(sgt); - kfree(sgt); - } else { - xe_ttm_vram_mgr_free_sgt(attach->dev, dir, sgt); - } + kvfree(shared_buf); } static int xe_dma_buf_begin_cpu_access(struct dma_buf *dma_buf, @@ -230,8 +205,8 @@ static const struct dma_buf_ops xe_dmabuf_ops = { .detach = xe_dma_buf_detach, .pin = xe_dma_buf_pin, .unpin = xe_dma_buf_unpin, - .map_dma_buf = xe_dma_buf_map, - .unmap_dma_buf = xe_dma_buf_unmap, + .map_dma_shared_buf = xe_dma_buf_map_dma_shared_buf, + .unmap_dma_shared_buf = xe_dma_buf_unmap_dma_shared_buf, .release = xe_dma_buf_release, .begin_cpu_access = xe_dma_buf_begin_cpu_access, .mmap = drm_gem_dmabuf_mmap, diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c index f28550b29f6b..6e41af57ef78 100644 --- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c +++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.c @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -27,6 +28,7 @@ #include "xe_res_cursor.h" #include "xe_ttm_stolen_mgr.h" #include "xe_ttm_vram_mgr.h" +#include "xe_vram.h" #include "xe_vram_types.h" static inline struct gpu_buddy_block * @@ -500,98 +502,63 @@ int xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_vram_region *vram) PAGE_SIZE); } -int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe, - struct ttm_resource *res, - u64 offset, u64 length, - struct device *dev, - enum dma_data_direction dir, - struct sg_table **sgt) +/** + * xe_ttm_vram_mgr_alloc_shared_buf() - dma_shared_buf of a VRAM resource + * @xe: xe device + * @res: The VRAM resource, all of which must be CPU visible + * + * The ranges are the addresses of the resource's blocks in the VRAM BAR, for + * a dma-buf exporter to hand out through &dma_buf_ops.map_dma_shared_buf. + * + * Return: The dma_shared_buf, to be freed with kvfree(), or an ERR_PTR() on + * failure. + */ +struct dma_shared_buf * +xe_ttm_vram_mgr_alloc_shared_buf(struct xe_device *xe, + struct ttm_resource *res) { struct xe_tile *tile = &xe->tiles[res->mem_type - XE_PL_VRAM0]; struct xe_ttm_vram_mgr_resource *vres = to_xe_ttm_vram_mgr_resource(res); + struct p2pdma_provider *provider = xe_vram_p2pdma_provider(xe); + struct dma_shared_buf *shared_buf; struct xe_res_cursor cursor; - struct scatterlist *sg; - int num_entries = 0; - int i, r; + size_t nr_ranges = 0; + size_t i; if (vres->used_visible_size < res->size) - return -EOPNOTSUPP; + return ERR_PTR(-EOPNOTSUPP); - *sgt = kmalloc_obj(**sgt); - if (!*sgt) - return -ENOMEM; + /* Without a provider the core would map the BAR as system memory */ + if (!provider) + return ERR_PTR(-EOPNOTSUPP); /* Determine the number of GPU_BUDDY blocks to export */ - xe_res_first(res, offset, length, &cursor); + xe_res_first(res, 0, res->size, &cursor); while (cursor.remaining) { - num_entries++; - /* Limit maximum size to 2GiB due to SG table limitations. */ - xe_res_next(&cursor, min_t(u64, cursor.size, SZ_2G)); + nr_ranges++; + xe_res_next(&cursor, cursor.size); } - r = sg_alloc_table(*sgt, num_entries, GFP_KERNEL); - if (r) - goto error_free; - - /* Initialize scatterlist nodes of sg_table */ - for_each_sgtable_sg((*sgt), sg, i) - sg->length = 0; - - /* - * Walk down GPU_BUDDY blocks to populate scatterlist nodes - * @note: Use iterator api to get first the GPU_BUDDY block - * and the number of bytes from it. Access the following - * GPU_BUDDY block(s) if more buffer needs to exported - */ - xe_res_first(res, offset, length, &cursor); - for_each_sgtable_sg((*sgt), sg, i) { - phys_addr_t phys = cursor.start + xe_vram_region_io_start(tile->mem.vram); - size_t size = min_t(u64, cursor.size, SZ_2G); - dma_addr_t addr; - - addr = dma_map_resource(dev, phys, size, dir, - DMA_ATTR_SKIP_CPU_SYNC); - r = dma_mapping_error(dev, addr); - if (r) - goto error_unmap; - - sg_set_page(sg, NULL, size, 0); - sg_dma_address(sg) = addr; - sg_dma_len(sg) = size; - - xe_res_next(&cursor, size); - } + shared_buf = kvmalloc(struct_size(shared_buf, ranges, nr_ranges), + GFP_KERNEL); + if (!shared_buf) + return ERR_PTR(-ENOMEM); - return 0; + shared_buf->provider = provider; + shared_buf->attrs = 0; + shared_buf->size = res->size; + shared_buf->nr_ranges = nr_ranges; -error_unmap: - for_each_sgtable_sg((*sgt), sg, i) { - if (!sg->length) - continue; + xe_res_first(res, 0, res->size, &cursor); + for (i = 0; i < nr_ranges; i++) { + shared_buf->ranges[i].paddr = cursor.start + + xe_vram_region_io_start(tile->mem.vram); + shared_buf->ranges[i].len = cursor.size; - dma_unmap_resource(dev, sg->dma_address, - sg->length, dir, - DMA_ATTR_SKIP_CPU_SYNC); + xe_res_next(&cursor, cursor.size); } - sg_free_table(*sgt); - -error_free: - kfree(*sgt); - return r; -} -void xe_ttm_vram_mgr_free_sgt(struct device *dev, enum dma_data_direction dir, - struct sg_table *sgt) -{ - struct scatterlist *sg; - int i; - - for_each_sgtable_sg(sgt, sg, i) - dma_unmap_resource(dev, sg->dma_address, - sg->length, dir, - DMA_ATTR_SKIP_CPU_SYNC); - sg_free_table(sgt); - kfree(sgt); + return shared_buf; } u64 xe_ttm_vram_get_cpu_visible_size(struct ttm_resource_manager *man) diff --git a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h index d77f067d197b..c1613efde23a 100644 --- a/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h +++ b/drivers/gpu/drm/xe/xe_ttm_vram_mgr.h @@ -8,7 +8,7 @@ #include "xe_ttm_vram_mgr_types.h" -enum dma_data_direction; +struct dma_shared_buf; struct xe_device; struct xe_tile; struct xe_vram_region; @@ -18,14 +18,9 @@ int __xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_ttm_vram_mgr *mgr, u64 default_page_size); int xe_ttm_vram_mgr_init(struct xe_device *xe, struct xe_vram_region *vram); void xe_ttm_vram_debugfs_init(struct xe_device *xe, struct dentry *root); -int xe_ttm_vram_mgr_alloc_sgt(struct xe_device *xe, - struct ttm_resource *res, - u64 offset, u64 length, - struct device *dev, - enum dma_data_direction dir, - struct sg_table **sgt); -void xe_ttm_vram_mgr_free_sgt(struct device *dev, enum dma_data_direction dir, - struct sg_table *sgt); +struct dma_shared_buf * +xe_ttm_vram_mgr_alloc_shared_buf(struct xe_device *xe, + struct ttm_resource *res); u64 xe_ttm_vram_get_avail(struct ttm_resource_manager *man); u64 xe_ttm_vram_get_cpu_visible_size(struct ttm_resource_manager *man); diff --git a/drivers/gpu/drm/xe/xe_vram.c b/drivers/gpu/drm/xe/xe_vram.c index dcedd8cfd731..c0b040582b81 100644 --- a/drivers/gpu/drm/xe/xe_vram.c +++ b/drivers/gpu/drm/xe/xe_vram.c @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -320,6 +321,11 @@ int xe_vram_probe(struct xe_device *xe) return err; drm_info(&xe->drm, "VISIBLE VRAM: %pa, %pa\n", &lmem_bar.io_start, &lmem_bar.io_size); + /* VRAM is exported to other devices as ranges of the BAR */ + err = pcim_p2pdma_init(to_pci_dev(xe->drm.dev)); + if (err && err != -EOPNOTSUPP) + return err; + remain_io_size = lmem_bar.io_size; for_each_tile(tile, xe, id) { @@ -355,6 +361,21 @@ int xe_vram_probe(struct xe_device *xe) return devm_add_action_or_reset(xe->drm.dev, vram_fini, xe); } +/** + * xe_vram_p2pdma_provider - Get the p2pdma provider of the VRAM BAR + * @xe: xe device + * + * Return: the provider, or NULL if the device has no VRAM BAR or the BAR + * cannot be used for PCI peer-to-peer DMA + */ +struct p2pdma_provider *xe_vram_p2pdma_provider(struct xe_device *xe) +{ + if (!IS_DGFX(xe)) + return NULL; + + return pcim_p2pdma_provider(to_pci_dev(xe->drm.dev), LMEM_BAR); +} + /** * xe_vram_region_io_start - Get the IO start of a VRAM region * @vram: the VRAM region diff --git a/drivers/gpu/drm/xe/xe_vram.h b/drivers/gpu/drm/xe/xe_vram.h index 38425d81f777..3abe6c6e50f9 100644 --- a/drivers/gpu/drm/xe/xe_vram.h +++ b/drivers/gpu/drm/xe/xe_vram.h @@ -8,12 +8,14 @@ #include +struct p2pdma_provider; struct xe_device; struct xe_vram_region; struct ttm_resource; struct xe_vram_region *xe_map_resource_to_region(struct ttm_resource *res); int xe_vram_probe(struct xe_device *xe); +struct p2pdma_provider *xe_vram_p2pdma_provider(struct xe_device *xe); struct xe_vram_region *xe_vram_region_alloc(struct xe_device *xe, u8 id, u32 placement); -- 2.54.0