]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
dma-mapping: add a dma_can_mmap helper
authorChristoph Hellwig <hch@lst.de>
Sat, 3 Aug 2019 10:31:25 +0000 (13:31 +0300)
committerChristoph Hellwig <hch@lst.de>
Wed, 4 Sep 2019 09:13:18 +0000 (11:13 +0200)
Add a helper to check if DMA allocations for a specific device can be
mapped to userspace using dma_mmap_*.

Signed-off-by: Christoph Hellwig <hch@lst.de>
include/linux/dma-mapping.h
kernel/dma/mapping.c

index de41a4f0b9f6c57b2a250b2391ed68f54b94be9c..10dc9ac3cccb10c82e45842c00dfdcd83103e390 100644 (file)
@@ -463,6 +463,7 @@ int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
 int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
                void *cpu_addr, dma_addr_t dma_addr, size_t size,
                unsigned long attrs);
+bool dma_can_mmap(struct device *dev);
 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);
@@ -554,6 +555,10 @@ static inline int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
 {
        return -ENXIO;
 }
+static inline bool dma_can_mmap(struct device *dev)
+{
+       return false;
+}
 static inline int dma_supported(struct device *dev, u64 mask)
 {
        return 0;
index c8b4e46407bab55431cbe1b7ee5b168c0a75aa6c..18ba1ac93fc180713fd59ce8939a80ef574741e7 100644 (file)
@@ -222,6 +222,29 @@ int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
 #endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
 }
 
+/**
+ * dma_can_mmap - check if a given device supports dma_mmap_*
+ * @dev: device to check
+ *
+ * Returns %true if @dev supports dma_mmap_coherent() and dma_mmap_attrs() to
+ * map DMA allocations to userspace.
+ */
+bool dma_can_mmap(struct device *dev)
+{
+       const struct dma_map_ops *ops = get_dma_ops(dev);
+
+       if (IS_ENABLED(CONFIG_ARCH_NO_COHERENT_DMA_MMAP))
+               return false;
+
+       if (dma_is_direct(ops)) {
+               return dev_is_dma_coherent(dev) ||
+                       IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN);
+       }
+
+       return ops->mmap != NULL;
+}
+EXPORT_SYMBOL_GPL(dma_can_mmap);
+
 /**
  * dma_mmap_attrs - map a coherent DMA allocation into user space
  * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices