/* * Check if the devices uses a direct mapping for streaming DMA operations. * This allows IOMMU drivers to set a bypass mode if the DMA mask is large * enough.
*/ staticinlinebool dma_alloc_direct(struct device *dev, conststruct dma_map_ops *ops)
{ return dma_go_direct(dev, dev->coherent_dma_mask, ops);
}
/** * dma_map_sg_attrs - Map the given buffer for DMA * @dev: The device for which to perform the DMA operation * @sg: The sg_table object describing the buffer * @nents: Number of entries to map * @dir: DMA direction * @attrs: Optional DMA attributes for the map operation * * Maps a buffer described by a scatterlist passed in the sg argument with * nents segments for the @dir DMA operation by the @dev device. * * Returns the number of mapped entries (which can be less than nents) * on success. Zero is returned for any error. * * dma_unmap_sg_attrs() should be used to unmap the buffer with the * original sg and original nents (not the value returned by this funciton).
*/ unsignedint dma_map_sg_attrs(struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir, unsignedlong attrs)
{ int ret;
ret = __dma_map_sg_attrs(dev, sg, nents, dir, attrs); if (ret < 0) return 0; return ret;
}
EXPORT_SYMBOL(dma_map_sg_attrs);
/** * dma_map_sgtable - Map the given buffer for DMA * @dev: The device for which to perform the DMA operation * @sgt: The sg_table object describing the buffer * @dir: DMA direction * @attrs: Optional DMA attributes for the map operation * * Maps a buffer described by a scatterlist stored in the given sg_table * object for the @dir DMA operation by the @dev device. After success, the * ownership for the buffer is transferred to the DMA domain. One has to * call dma_sync_sgtable_for_cpu() or dma_unmap_sgtable() to move the * ownership of the buffer back to the CPU domain before touching the * buffer by the CPU. * * Returns 0 on success or a negative error code on error. The following * error codes are supported with the given meaning: * * -EINVAL An invalid argument, unaligned access or other error * in usage. Will not succeed if retried. * -ENOMEM Insufficient resources (like memory or IOVA space) to * complete the mapping. Should succeed if retried later. * -EIO Legacy error code with an unknown meaning. eg. this is * returned if a lower level call returned * DMA_MAPPING_ERROR. * -EREMOTEIO The DMA device cannot access P2PDMA memory specified * in the sg_table. This will not succeed if retried.
*/ int dma_map_sgtable(struct device *dev, struct sg_table *sgt, enum dma_data_direction dir, unsignedlong attrs)
{ int nents;
if (dma_map_direct(dev, ops)) /* * dma_skip_sync could've been reset on first SWIOTLB buffer * mapping, but @dma_addr is not necessary an SWIOTLB buffer. * In this case, fall back to more granular check.
*/ return dma_direct_need_sync(dev, dma_addr); returntrue;
}
EXPORT_SYMBOL_GPL(__dma_need_sync);
/** * dma_need_unmap - does this device need dma_unmap_* operations * @dev: device to check * * If this function returns %false, drivers can skip calling dma_unmap_* after * finishing an I/O. This function must be called after all mappings that might * need to be unmapped have been performed.
*/ bool dma_need_unmap(struct device *dev)
{ if (!dma_map_direct(dev, get_dma_ops(dev))) returntrue; if (!dev->dma_skip_sync) returntrue; return IS_ENABLED(CONFIG_DMA_API_DEBUG);
}
EXPORT_SYMBOL_GPL(dma_need_unmap);
if (dma_map_direct(dev, ops) || use_dma_iommu(dev)) /* * dma_skip_sync will be reset to %false on first SWIOTLB buffer * mapping, if any. During the device initialization, it's * enough to check only for the DMA coherence.
*/
dev->dma_skip_sync = dev_is_dma_coherent(dev); elseif (!ops->sync_single_for_device && !ops->sync_single_for_cpu &&
!ops->sync_sg_for_device && !ops->sync_sg_for_cpu) /* * Synchronization is not possible when none of DMA sync ops * is set.
*/
dev->dma_skip_sync = true; else
dev->dma_skip_sync = false;
} #else/* !CONFIG_DMA_NEED_SYNC */ staticinlinevoid dma_setup_need_sync(struct device *dev) { } #endif/* !CONFIG_DMA_NEED_SYNC */
/* * The whole dma_get_sgtable() idea is fundamentally unsafe - it seems * that the intention is to allow exporting memory allocated via the * coherent DMA APIs through the dma_buf API, which only accepts a * scattertable. This presents a couple of problems: * 1. Not all memory allocated via the coherent DMA APIs is backed by * a struct page * 2. Passing coherent DMA memory into the streaming APIs is not allowed * as we will try to flush the memory through a different alias to that * actually being used (and the flushes are redundant.)
*/ int dma_get_sgtable_attrs(struct device *dev, struct sg_table *sgt, void *cpu_addr, dma_addr_t dma_addr, size_t size, unsignedlong attrs)
{ conststruct dma_map_ops *ops = get_dma_ops(dev);
#ifdef CONFIG_MMU /* * Return the page attributes used for mapping dma_alloc_* memory, either in * kernel space if remapping is needed, or to userspace through dma_mmap_*.
*/
pgprot_t dma_pgprot(struct device *dev, pgprot_t prot, unsignedlong attrs)
{ if (dev_is_dma_coherent(dev)) return prot; #ifdef CONFIG_ARCH_HAS_DMA_WRITE_COMBINE if (attrs & DMA_ATTR_WRITE_COMBINE) return pgprot_writecombine(prot); #endif return pgprot_dmacoherent(prot);
} #endif/* CONFIG_MMU */
/** * 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)
{ conststruct dma_map_ops *ops = get_dma_ops(dev);
if (dma_alloc_direct(dev, ops)) return dma_direct_can_mmap(dev); if (use_dma_iommu(dev)) returntrue; 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 * @vma: vm_area_struct describing requested user mapping * @cpu_addr: kernel CPU-view address returned from dma_alloc_attrs * @dma_addr: device-view address returned from dma_alloc_attrs * @size: size of memory originally requested in dma_alloc_attrs * @attrs: attributes of mapping properties requested in dma_alloc_attrs * * Map a coherent DMA buffer previously allocated by dma_alloc_attrs into user * space. The coherent DMA buffer must not be freed by the driver until the * user space mapping has been released.
*/ int dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr, dma_addr_t dma_addr, size_t size, unsignedlong attrs)
{ conststruct dma_map_ops *ops = get_dma_ops(dev);
if (dma_alloc_direct(dev, ops)) return dma_direct_get_required_mask(dev);
if (use_dma_iommu(dev)) return DMA_BIT_MASK(32);
if (ops->get_required_mask) return ops->get_required_mask(dev);
/* * We require every DMA ops implementation to at least support a 32-bit * DMA mask (and use bounce buffering if that isn't supported in * hardware). As the direct mapping code has its own routine to * actually report an optimal mask we default to 32-bit here as that * is the right thing for most IOMMUs, and at least not actively * harmful in general.
*/ return DMA_BIT_MASK(32);
}
EXPORT_SYMBOL_GPL(dma_get_required_mask);
/* * DMA allocations can never be turned back into a page pointer, so * requesting compound pages doesn't make sense (and can't even be * supported at all by various backends).
*/ if (WARN_ON_ONCE(flag & __GFP_COMP)) return NULL;
if (dma_release_from_dev_coherent(dev, get_order(size), cpu_addr)) return; /* * On non-coherent platforms which implement DMA-coherent buffers via * non-cacheable remaps, ops->free() may call vunmap(). Thus getting * this far in IRQ context is a) at risk of a BUG_ON() or trying to * sleep on some machines, and b) an indication that the driver is * probably misusing the coherent API anyway.
*/
WARN_ON(irqs_disabled());
trace_dma_free(dev, cpu_addr, dma_handle, size, DMA_BIDIRECTIONAL,
attrs); if (!cpu_addr) return;
if (use_dma_iommu(dev)) { if (WARN_ON(ops)) returnfalse; returntrue;
}
/* * ->dma_supported sets and clears the bypass flag, so ignore it here * and always call into the method if there is one.
*/ if (ops) { if (!ops->dma_supported) returntrue; return ops->dma_supported(dev, mask);
}
/* * Note: dma_ops_bypass is not checked here because P2PDMA should * not be used with dma mapping ops that do not have support even * if the specific device is bypassing them.
*/
/* if ops is not set, dma direct and default IOMMU support P2PDMA */ return !ops;
}
EXPORT_SYMBOL_GPL(dma_pci_p2pdma_supported);
int dma_set_mask(struct device *dev, u64 mask)
{ /* * Truncate the mask to the actually supported dma_addr_t width to * avoid generating unsupportable addresses.
*/
mask = (dma_addr_t)mask;
if (!dev->dma_mask || !dma_supported(dev, mask)) return -EIO;
if (min_not_zero(dma_get_mask(dev), dev->bus_dma_limit) <
dma_get_required_mask(dev)) returntrue;
if (unlikely(ops) || use_dma_iommu(dev)) returnfalse; return !dma_direct_all_ram_mapped(dev);
}
/** * dma_addressing_limited - return if the device is addressing limited * @dev: device to check * * Return %true if the devices DMA mask is too small to address all memory in * the system, else %false. Lack of addressing bits is the prime reason for * bounce buffering, but might not be the only one.
*/ bool dma_addressing_limited(struct device *dev)
{ if (!__dma_addressing_limited(dev)) returnfalse;
dev_dbg(dev, "device is DMA addressing limited\n"); returntrue;
}
EXPORT_SYMBOL_GPL(dma_addressing_limited);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.