Skip to content

Commit e29ccc1

Browse files
author
Christoph Hellwig
committed
dma-mapping: add a dma_can_mmap helper
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>
1 parent f9f3232 commit e29ccc1

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

‎include/linux/dma-mapping.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt,
463463
int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma,
464464
void *cpu_addr, dma_addr_t dma_addr, size_t size,
465465
unsigned long attrs);
466+
bool dma_can_mmap(struct device *dev);
466467
int dma_supported(struct device *dev, u64 mask);
467468
int dma_set_mask(struct device *dev, u64 mask);
468469
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,
554555
{
555556
return -ENXIO;
556557
}
558+
static inline bool dma_can_mmap(struct device *dev)
559+
{
560+
return false;
561+
}
557562
static inline int dma_supported(struct device *dev, u64 mask)
558563
{
559564
return 0;

‎kernel/dma/mapping.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,29 @@ int dma_common_mmap(struct device *dev, struct vm_area_struct *vma,
222222
#endif /* !CONFIG_ARCH_NO_COHERENT_DMA_MMAP */
223223
}
224224

225+
/**
226+
* dma_can_mmap - check if a given device supports dma_mmap_*
227+
* @dev: device to check
228+
*
229+
* Returns %true if @dev supports dma_mmap_coherent() and dma_mmap_attrs() to
230+
* map DMA allocations to userspace.
231+
*/
232+
bool dma_can_mmap(struct device *dev)
233+
{
234+
const struct dma_map_ops *ops = get_dma_ops(dev);
235+
236+
if (IS_ENABLED(CONFIG_ARCH_NO_COHERENT_DMA_MMAP))
237+
return false;
238+
239+
if (dma_is_direct(ops)) {
240+
return dev_is_dma_coherent(dev) ||
241+
IS_ENABLED(CONFIG_ARCH_HAS_DMA_COHERENT_TO_PFN);
242+
}
243+
244+
return ops->mmap != NULL;
245+
}
246+
EXPORT_SYMBOL_GPL(dma_can_mmap);
247+
225248
/**
226249
* dma_mmap_attrs - map a coherent DMA allocation into user space
227250
* @dev: valid struct device pointer, or NULL for ISA and EISA-like devices

0 commit comments

Comments
 (0)