dma_common_mmap 文档让用户 read/write 物理地址

dma_common_mmap documentation to let user read/write physical address

我正在尝试编写一个 Linux 内核模块以使用 dma_common_mmap() 将一些地址映射回用户。然后我希望用户 mmap 和 write/read 地址 space.

我现在的主要问题是找不到 dma_common_mmap() 的文档,有吗?我用谷歌搜索但没有找到如何使用它并让用户 read/write 地址。

dma_common_mmap() 的文档不存在。但是您可以查看 dma_mmap_attrs() 函数的 Doxygen 注释:

/**
 * 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
 * @handle: 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.
 */
static inline int
dma_mmap_attrs(struct device *dev, struct vm_area_struct *vma, void *cpu_addr,
           dma_addr_t dma_addr, size_t size, struct dma_attrs *attrs)
{
    struct dma_map_ops *ops = get_dma_ops(dev);
    BUG_ON(!ops);
    if (ops->mmap)
        return ops->mmap(dev, vma, cpu_addr, dma_addr, size, attrs);
    return dma_common_mmap(dev, vma, cpu_addr, dma_addr, size);
}

#define dma_mmap_coherent(d, v, c, h, s) dma_mmap_attrs(d, v, c, h, s, NULL)

dma_mmap_attrs() 依次调用 dma_common_mmap(),因此所有文档(attrs 参数除外)按原样适用于 dma_common_mmap()

编辑

我认为你应该使用 dma_mmap_coherent()(连同 dma_alloc_coherent()),它的作用与 dma_common_mmap() 几乎相同(参见上面的代码)。请参见 this example to get some clue on how to use it both in kernel side and in user-space. See also how dma_mmap_coherent() is used in ALSA kernel code, in snd_pcm_lib_default_mmap() 函数。