dma: Introduce dma_max_mapping_size()
authorJoerg Roedel <jroedel@suse.de>
Thu, 7 Feb 2019 11:59:15 +0000 (12:59 +0100)
committerMichael S. Tsirkin <mst@redhat.com>
Wed, 6 Mar 2019 16:19:11 +0000 (11:19 -0500)
The function returns the maximum size that can be mapped
using DMA-API functions. The patch also adds the
implementation for direct DMA and a new dma_map_ops pointer
so that other implementations can expose their limit.

Cc: stable@vger.kernel.org
Reviewed-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Joerg Roedel <jroedel@suse.de>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Documentation/DMA-API.txt
include/linux/dma-mapping.h
kernel/dma/direct.c
kernel/dma/mapping.c

index e133ccd60228044ac1bce9026082bd5b3eb2411d..acfe3d0f78d1ff4a7f960ee4fa66ae7b0abe8881 100644 (file)
@@ -195,6 +195,14 @@ Requesting the required mask does not alter the current mask.  If you
 wish to take advantage of it, you should issue a dma_set_mask()
 call to set the mask to the value returned.
 
+::
+
+       size_t
+       dma_direct_max_mapping_size(struct device *dev);
+
+Returns the maximum size of a mapping for the device. The size parameter
+of the mapping functions like dma_map_single(), dma_map_page() and
+others should not be larger than the returned value.
 
 Part Id - Streaming DMA mappings
 --------------------------------
index f6ded992c1839765679549f828d2f67fae802633..5b21f14802e16b40535990d5245398a59b52391f 100644 (file)
@@ -130,6 +130,7 @@ struct dma_map_ops {
                        enum dma_data_direction direction);
        int (*dma_supported)(struct device *dev, u64 mask);
        u64 (*get_required_mask)(struct device *dev);
+       size_t (*max_mapping_size)(struct device *dev);
 };
 
 #define DMA_MAPPING_ERROR              (~(dma_addr_t)0)
@@ -257,6 +258,8 @@ static inline void dma_direct_sync_sg_for_cpu(struct device *dev,
 }
 #endif
 
+size_t dma_direct_max_mapping_size(struct device *dev);
+
 #ifdef CONFIG_HAS_DMA
 #include <asm/dma-mapping.h>
 
@@ -460,6 +463,7 @@ int dma_supported(struct device *dev, u64 mask);
 int dma_set_mask(struct device *dev, u64 mask);
 int dma_set_coherent_mask(struct device *dev, u64 mask);
 u64 dma_get_required_mask(struct device *dev);
+size_t dma_max_mapping_size(struct device *dev);
 #else /* CONFIG_HAS_DMA */
 static inline dma_addr_t dma_map_page_attrs(struct device *dev,
                struct page *page, size_t offset, size_t size,
@@ -561,6 +565,10 @@ static inline u64 dma_get_required_mask(struct device *dev)
 {
        return 0;
 }
+static inline size_t dma_max_mapping_size(struct device *dev)
+{
+       return 0;
+}
 #endif /* CONFIG_HAS_DMA */
 
 static inline dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
index 355d16acee6dd17aa7998d1ea55a6abcb3e01e92..6310ad01f915b1807bc87cbee1c44bae99eb5666 100644 (file)
@@ -380,3 +380,14 @@ int dma_direct_supported(struct device *dev, u64 mask)
         */
        return mask >= __phys_to_dma(dev, min_mask);
 }
+
+size_t dma_direct_max_mapping_size(struct device *dev)
+{
+       size_t size = SIZE_MAX;
+
+       /* If SWIOTLB is active, use its maximum mapping size */
+       if (is_swiotlb_active())
+               size = swiotlb_max_mapping_size(dev);
+
+       return size;
+}
index a11006b6d8e87d5f64db77e777da08355a32ad8d..5753008ab2860fba0c5b7dd911c047e1fc770e61 100644 (file)
@@ -357,3 +357,17 @@ void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
                ops->cache_sync(dev, vaddr, size, dir);
 }
 EXPORT_SYMBOL(dma_cache_sync);
+
+size_t dma_max_mapping_size(struct device *dev)
+{
+       const struct dma_map_ops *ops = get_dma_ops(dev);
+       size_t size = SIZE_MAX;
+
+       if (dma_is_direct(ops))
+               size = dma_direct_max_mapping_size(dev);
+       else if (ops && ops->max_mapping_size)
+               size = ops->max_mapping_size(dev);
+
+       return size;
+}
+EXPORT_SYMBOL_GPL(dma_max_mapping_size);