]> www.infradead.org Git - users/hch/misc.git/commitdiff
dma-mapping: convert dma_direct_*map_page to be phys_addr_t based
authorLeon Romanovsky <leonro@nvidia.com>
Tue, 9 Sep 2025 13:27:35 +0000 (16:27 +0300)
committerMarek Szyprowski <m.szyprowski@samsung.com>
Thu, 11 Sep 2025 22:18:20 +0000 (00:18 +0200)
Convert the DMA direct mapping functions to accept physical addresses
directly instead of page+offset parameters. The functions were already
operating on physical addresses internally, so this change eliminates
the redundant page-to-physical conversion at the API boundary.

The functions dma_direct_map_page() and dma_direct_unmap_page() are
renamed to dma_direct_map_phys() and dma_direct_unmap_phys() respectively,
with their calling convention changed from (struct page *page,
unsigned long offset) to (phys_addr_t phys).

Architecture-specific functions arch_dma_map_page_direct() and
arch_dma_unmap_page_direct() are similarly renamed to
arch_dma_map_phys_direct() and arch_dma_unmap_phys_direct().

The is_pci_p2pdma_page() checks are replaced with DMA_ATTR_MMIO checks
to allow integration with dma_direct_map_resource and dma_direct_map_phys()
is extended to support MMIO path either.

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Link: https://lore.kernel.org/r/bb15a22f76dc2e26683333ff54e789606cfbfcf0.1757423202.git.leonro@nvidia.com
arch/powerpc/kernel/dma-iommu.c
include/linux/dma-map-ops.h
kernel/dma/direct.c
kernel/dma/direct.h
kernel/dma/mapping.c

index 4d64a5db50f389470dadbfd2417778f3180c5210..0359ab72cd3ba5b64757d1effadabe5adbd4d0c7 100644 (file)
@@ -14,7 +14,7 @@
 #define can_map_direct(dev, addr) \
        ((dev)->bus_dma_limit >= phys_to_dma((dev), (addr)))
 
-bool arch_dma_map_page_direct(struct device *dev, phys_addr_t addr)
+bool arch_dma_map_phys_direct(struct device *dev, phys_addr_t addr)
 {
        if (likely(!dev->bus_dma_limit))
                return false;
@@ -24,7 +24,7 @@ bool arch_dma_map_page_direct(struct device *dev, phys_addr_t addr)
 
 #define is_direct_handle(dev, h) ((h) >= (dev)->archdata.dma_offset)
 
-bool arch_dma_unmap_page_direct(struct device *dev, dma_addr_t dma_handle)
+bool arch_dma_unmap_phys_direct(struct device *dev, dma_addr_t dma_handle)
 {
        if (likely(!dev->bus_dma_limit))
                return false;
index 332b80c42b6f3271ad91d1c0d0d0481a7ab232c8..10882d00cb170413fbaf1cf1ddb2b841a97fb023 100644 (file)
@@ -395,15 +395,15 @@ void *arch_dma_set_uncached(void *addr, size_t size);
 void arch_dma_clear_uncached(void *addr, size_t size);
 
 #ifdef CONFIG_ARCH_HAS_DMA_MAP_DIRECT
-bool arch_dma_map_page_direct(struct device *dev, phys_addr_t addr);
-bool arch_dma_unmap_page_direct(struct device *dev, dma_addr_t dma_handle);
+bool arch_dma_map_phys_direct(struct device *dev, phys_addr_t addr);
+bool arch_dma_unmap_phys_direct(struct device *dev, dma_addr_t dma_handle);
 bool arch_dma_map_sg_direct(struct device *dev, struct scatterlist *sg,
                int nents);
 bool arch_dma_unmap_sg_direct(struct device *dev, struct scatterlist *sg,
                int nents);
 #else
-#define arch_dma_map_page_direct(d, a)         (false)
-#define arch_dma_unmap_page_direct(d, a)       (false)
+#define arch_dma_map_phys_direct(d, a)         (false)
+#define arch_dma_unmap_phys_direct(d, a)       (false)
 #define arch_dma_map_sg_direct(d, s, n)                (false)
 #define arch_dma_unmap_sg_direct(d, s, n)      (false)
 #endif
index 302e89580972af82b762fea101325a265b17121d..ba7524f169bc15f3ef9208fd4bc04c2858b7a0a2 100644 (file)
@@ -448,7 +448,7 @@ void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl,
                if (sg_dma_is_bus_address(sg))
                        sg_dma_unmark_bus_address(sg);
                else
-                       dma_direct_unmap_page(dev, sg->dma_address,
+                       dma_direct_unmap_phys(dev, sg->dma_address,
                                              sg_dma_len(sg), dir, attrs);
        }
 }
@@ -471,8 +471,8 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
                         */
                        break;
                case PCI_P2PDMA_MAP_NONE:
-                       sg->dma_address = dma_direct_map_page(dev, sg_page(sg),
-                                       sg->offset, sg->length, dir, attrs);
+                       sg->dma_address = dma_direct_map_phys(dev, sg_phys(sg),
+                                       sg->length, dir, attrs);
                        if (sg->dma_address == DMA_MAPPING_ERROR) {
                                ret = -EIO;
                                goto out_unmap;
index d2c0b7e632fc0088a3ad7774239642af0bda233d..da2fadf45bcd6c6034f636ef952a4436fcc30364 100644 (file)
@@ -80,42 +80,57 @@ static inline void dma_direct_sync_single_for_cpu(struct device *dev,
                arch_dma_mark_clean(paddr, size);
 }
 
-static inline dma_addr_t dma_direct_map_page(struct device *dev,
-               struct page *page, unsigned long offset, size_t size,
-               enum dma_data_direction dir, unsigned long attrs)
+static inline dma_addr_t dma_direct_map_phys(struct device *dev,
+               phys_addr_t phys, size_t size, enum dma_data_direction dir,
+               unsigned long attrs)
 {
-       phys_addr_t phys = page_to_phys(page) + offset;
-       dma_addr_t dma_addr = phys_to_dma(dev, phys);
+       dma_addr_t dma_addr;
 
        if (is_swiotlb_force_bounce(dev)) {
-               if (is_pci_p2pdma_page(page))
-                       return DMA_MAPPING_ERROR;
+               if (attrs & DMA_ATTR_MMIO)
+                       goto err_overflow;
+
                return swiotlb_map(dev, phys, size, dir, attrs);
        }
 
-       if (unlikely(!dma_capable(dev, dma_addr, size, true)) ||
-           dma_kmalloc_needs_bounce(dev, size, dir)) {
-               if (is_pci_p2pdma_page(page))
-                       return DMA_MAPPING_ERROR;
-               if (is_swiotlb_active(dev))
-                       return swiotlb_map(dev, phys, size, dir, attrs);
-
-               dev_WARN_ONCE(dev, 1,
-                            "DMA addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
-                            &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
-               return DMA_MAPPING_ERROR;
+       if (attrs & DMA_ATTR_MMIO) {
+               dma_addr = phys;
+               if (unlikely(!dma_capable(dev, dma_addr, size, false)))
+                       goto err_overflow;
+       } else {
+               dma_addr = phys_to_dma(dev, phys);
+               if (unlikely(!dma_capable(dev, dma_addr, size, true)) ||
+                   dma_kmalloc_needs_bounce(dev, size, dir)) {
+                       if (is_swiotlb_active(dev))
+                               return swiotlb_map(dev, phys, size, dir, attrs);
+
+                       goto err_overflow;
+               }
        }
 
-       if (!dev_is_dma_coherent(dev) && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
+       if (!dev_is_dma_coherent(dev) &&
+           !(attrs & (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_MMIO)))
                arch_sync_dma_for_device(phys, size, dir);
        return dma_addr;
+
+err_overflow:
+       dev_WARN_ONCE(
+               dev, 1,
+               "DMA addr %pad+%zu overflow (mask %llx, bus limit %llx).\n",
+               &dma_addr, size, *dev->dma_mask, dev->bus_dma_limit);
+       return DMA_MAPPING_ERROR;
 }
 
-static inline void dma_direct_unmap_page(struct device *dev, dma_addr_t addr,
+static inline void dma_direct_unmap_phys(struct device *dev, dma_addr_t addr,
                size_t size, enum dma_data_direction dir, unsigned long attrs)
 {
-       phys_addr_t phys = dma_to_phys(dev, addr);
+       phys_addr_t phys;
+
+       if (attrs & DMA_ATTR_MMIO)
+               /* nothing to do: uncached and no swiotlb */
+               return;
 
+       phys = dma_to_phys(dev, addr);
        if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
                dma_direct_sync_single_for_cpu(dev, addr, size, dir);
 
index 90ad728205b934c6302f1a2dbeb8b5ec45786eff..3ac7d15e095f9355577c7ba9a6084149251a16b9 100644 (file)
@@ -166,8 +166,8 @@ dma_addr_t dma_map_page_attrs(struct device *dev, struct page *page,
                return DMA_MAPPING_ERROR;
 
        if (dma_map_direct(dev, ops) ||
-           arch_dma_map_page_direct(dev, phys + size))
-               addr = dma_direct_map_page(dev, page, offset, size, dir, attrs);
+           arch_dma_map_phys_direct(dev, phys + size))
+               addr = dma_direct_map_phys(dev, phys, size, dir, attrs);
        else if (use_dma_iommu(dev))
                addr = iommu_dma_map_phys(dev, phys, size, dir, attrs);
        else
@@ -187,8 +187,8 @@ void dma_unmap_page_attrs(struct device *dev, dma_addr_t addr, size_t size,
 
        BUG_ON(!valid_dma_direction(dir));
        if (dma_map_direct(dev, ops) ||
-           arch_dma_unmap_page_direct(dev, addr + size))
-               dma_direct_unmap_page(dev, addr, size, dir, attrs);
+           arch_dma_unmap_phys_direct(dev, addr + size))
+               dma_direct_unmap_phys(dev, addr, size, dir, attrs);
        else if (use_dma_iommu(dev))
                iommu_dma_unmap_phys(dev, addr, size, dir, attrs);
        else