Since its introduction, DMA-buf has only supported using scatterlist for
the exporter and importer to exchange address information.  This is not
sufficient for all use cases as dma_addr_t is a very specific and limited
type that should not be abused for things unrelated to the DMA API.

There are several motivations for addressing this now:
 1) VFIO to IOMMUFD and KVM requires a physical address, not a dma_addr_t
    scatterlist, it cannot be represented in the scatterlist structure
 2) xe vGPU requires the host driver to accept a DMABUF from VFIO of its
    own VF and convert it into an internal VRAM address on the PF
 3) We are starting to look at replacement datastructures for
    scatterlist
 4) Ideas around UALink/etc are suggesting not using the DMA API

None of these can sanely be achieved using scatterlist.

Introduce a new mechanism called "mapping types" which allows DMA-buf to
work with more map/unmap options than scatterlist. Each mapping type
encompasses a full set of functions and data unique to itself. The core
code provides a match-making system to select the best type offered by the
exporter and importer to be the active mapping type for the attachment.

Everything related to scatterlist is moved into a DMA-buf SGT mapping
type, and into the "dma_buf_sgt_*" namespace for clarity. Existing
exporters are moved over to explicitly declare SGT mapping types and
importers are adjusted to use the dma_buf_sgt_* named importer helpers.

Mapping types are designed to be extendable, a driver can declare its own
mapping type for its internal private interconnect and use that without
having to adjust the core code.

The new attachment sequence starts with the importing driver declaring
what mapping types it can accept:

        struct dma_buf_mapping_match imp_match[] = {
                DMA_BUF_IMAPPING_MY_DRIVER(dev, ...),
                DMA_BUF_IMAPPING_SGT(dev, false),
        };
        attach = dma_buf_mapping_attach(dmabuf, imp_match, ...)

Most drivers will do this via a dma_buf_sgt_*attach() helper.

The exporting driver can then declare what mapping types it can supply:

int exporter_match_mapping(struct dma_buf_match_args *args)
{
        struct dma_buf_mapping_match exp_match[] = {
                DMA_BUF_EMAPPING_MY_DRIVER(my_ops, dev, ...),
                DMA_BUF_EMAPPING_SGT(sgt_ops, dev, false),
                DMA_BUF_EMAPPING_PAL(PAL_ops),
        };
        return dma_buf_match_mapping(args, exp_match, ...);
}

Most drivers will do this via a helper:
 static const struct dma_buf_ops ops = {
     DMA_BUF_SIMPLE_SGT_EXP_MATCH(map_func, unmap_func)
  };

During dma_buf_mapping_attach() the core code will select a mutual match
between the importer and exporter and record it as the active match in
the attach->map_type.

Each mapping type has its own types/function calls for
mapping/unmapping, and storage in the attach->map_type for its
information. As such each mapping type can offer function signatures
and data that exactly matches its needs.

This series goes through a sequence of:
 1) Introduce the basic mapping type framework and the main components of
    the SGT mapping type
 2) Automatically make all existing exporters and importers use core
    generated SGT mapping types so every attachment has a SGT mapping type
 3) Convert all exporter drivers to natively create a SGT mapping type
 4) Move all dma_buf_* functions and types that are related to SGT into
    dma_buf_sgt_*
 5) Remove all the now-unused items that have been moved into SGT specific
    structures.
 6) Demonstrate adding a new Physical Address List alongside SGT.

Due to the high number of files touched I would expect this to be broken
into phases, but this shows the entire picture.

This is on github: https://github.com/jgunthorpe/linux/commits/dmabuf_map_type

It is a followup to the discussion here:

https://lore.kernel.org/dri-devel/[email protected]/

Jason Gunthorpe (26):
  dma-buf: Introduce DMA-buf mapping types
  dma-buf: Add the SGT DMA mapping type
  dma-buf: Add dma_buf_mapping_attach()
  dma-buf: Route SGT related actions through attach->map_type
  dma-buf: Allow single exporter drivers to avoid the match_mapping
    function
  drm: Check the SGT ops for drm_gem_map_dma_buf()
  dma-buf: Convert all the simple exporters to use SGT mapping type
  drm/vmwgfx: Use match_mapping instead of dummy calls
  accel/habanalabs: Use the SGT mapping type
  drm/xe/dma-buf: Use the SGT mapping type
  drm/amdgpu: Use the SGT mapping type
  vfio/pci: Change the DMA-buf exporter to use mapping_type
  dma-buf: Update dma_buf_phys_vec_to_sgt() to use the SGT mapping type
  iio: buffer: convert to use the SGT mapping type
  functionfs: convert to use the SGT mapping type
  dma-buf: Remove unused SGT stuff from the common structures
  treewide: Rename dma_buf_map_attachment(_unlocked) to dma_buf_sgt_
  treewide: Rename dma_buf_unmap_attachment(_unlocked) to dma_buf_sgt_*
  treewide: Rename dma_buf_attach() to dma_buf_sgt_attach()
  treewide: Rename dma_buf_dynamic_attach() to
    dma_buf_sgt_dynamic_attach()
  dma-buf: Add the Physical Address List DMA mapping type
  vfio/pci: Add physical address list support to DMABUF
  iommufd: Use the PAL mapping type instead of a vfio function
  iommufd: Support DMA-bufs with multiple physical ranges
  iommufd/selftest: Check multi-phys DMA-buf scenarios
  dma-buf: Add kunit tests for mapping type

 Documentation/gpu/todo.rst                    |   2 +-
 drivers/accel/amdxdna/amdxdna_gem.c           |  14 +-
 drivers/accel/amdxdna/amdxdna_ubuf.c          |  10 +-
 drivers/accel/habanalabs/common/memory.c      |  54 ++-
 drivers/accel/ivpu/ivpu_gem.c                 |  10 +-
 drivers/accel/ivpu/ivpu_gem_userptr.c         |  11 +-
 drivers/accel/qaic/qaic_data.c                |   8 +-
 drivers/dma-buf/Makefile                      |   1 +
 drivers/dma-buf/dma-buf-mapping.c             | 186 ++++++++-
 drivers/dma-buf/dma-buf.c                     | 180 ++++++---
 drivers/dma-buf/heaps/cma_heap.c              |  12 +-
 drivers/dma-buf/heaps/system_heap.c           |  13 +-
 drivers/dma-buf/st-dma-mapping.c              | 373 ++++++++++++++++++
 drivers/dma-buf/udmabuf.c                     |   8 +-
 drivers/gpu/drm/amd/amdgpu/amdgpu_dma_buf.c   |  98 +++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c       |   6 +-
 drivers/gpu/drm/armada/armada_gem.c           |  33 +-
 drivers/gpu/drm/drm_gem_shmem_helper.c        |   2 +-
 drivers/gpu/drm/drm_prime.c                   |  31 +-
 drivers/gpu/drm/i915/gem/i915_gem_dmabuf.c    |  18 +-
 drivers/gpu/drm/i915/gem/i915_gem_object.c    |   2 +-
 .../drm/i915/gem/selftests/i915_gem_dmabuf.c  |   8 +-
 .../gpu/drm/i915/gem/selftests/mock_dmabuf.c  |   8 +-
 drivers/gpu/drm/msm/msm_gem_prime.c           |   7 +-
 drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c     |  11 +-
 drivers/gpu/drm/tegra/gem.c                   |  33 +-
 drivers/gpu/drm/virtio/virtgpu_prime.c        |  23 +-
 drivers/gpu/drm/vmwgfx/vmwgfx_prime.c         |  32 +-
 drivers/gpu/drm/xe/xe_bo.c                    |  18 +-
 drivers/gpu/drm/xe/xe_dma_buf.c               |  61 +--
 drivers/iio/industrialio-buffer.c             |  15 +-
 drivers/infiniband/core/umem_dmabuf.c         |  15 +-
 drivers/iommu/iommufd/io_pagetable.h          |   4 +-
 drivers/iommu/iommufd/iommufd_private.h       |   8 -
 drivers/iommu/iommufd/iommufd_test.h          |   7 +
 drivers/iommu/iommufd/pages.c                 |  85 ++--
 drivers/iommu/iommufd/selftest.c              | 177 ++++++---
 .../media/common/videobuf2/videobuf2-core.c   |   2 +-
 .../common/videobuf2/videobuf2-dma-contig.c   |  26 +-
 .../media/common/videobuf2/videobuf2-dma-sg.c |  21 +-
 .../common/videobuf2/videobuf2-vmalloc.c      |  13 +-
 .../platform/nvidia/tegra-vde/dmabuf-cache.c  |   9 +-
 drivers/misc/fastrpc.c                        |  21 +-
 drivers/tee/tee_heap.c                        |  13 +-
 drivers/usb/gadget/function/f_fs.c            |  11 +-
 drivers/vfio/pci/vfio_pci_dmabuf.c            |  79 ++--
 drivers/xen/gntdev-dmabuf.c                   |  29 +-
 include/linux/dma-buf-mapping.h               | 297 ++++++++++++++
 include/linux/dma-buf.h                       | 168 ++++----
 io_uring/zcrx.c                               |   9 +-
 net/core/devmem.c                             |  14 +-
 samples/vfio-mdev/mbochs.c                    |  10 +-
 sound/soc/fsl/fsl_asrc_m2m.c                  |  12 +-
 tools/testing/selftests/iommu/iommufd.c       |  43 ++
 tools/testing/selftests/iommu/iommufd_utils.h |  17 +
 55 files changed, 1764 insertions(+), 614 deletions(-)
 create mode 100644 drivers/dma-buf/st-dma-mapping.c


base-commit: c63e5a50e1dd291cd95b04291b028fdcaba4c534
-- 
2.43.0

Reply via email to