在Linux内核的上下文中

在Linux内核的上下文中

本文介绍了在Linux内核的上下文中,什么是DMA映射和DMA引擎?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux内核的上下文中,什么是DMA映射和DMA引擎?何时可以在Linux设备驱动程序中使用DMA映射API和DMA引擎API?任何真正的Linux设备驱动程序示例都可以作为参考.

What is DMA mapping and DMA engine in context of linux kernel?When DMA mapping API and DMA engine API can be used in Linux Device Driver?Any real Linux Device Driver example as a reference would be great.

推荐答案

内核通常使用虚拟地址. kmalloc()vmalloc()之类的函数通常返回虚拟地址.可以将其存储在void*中.虚拟内存系统将这些地址转换为物理地址.这些物理地址实际上对驱动程序没有用.驾驶员必须使用ioremap()来映射空间并产生一个虚拟地址.

The kernel normally uses virtual address. Functions like kmalloc(), vmalloc() normally return virtual address. It can be stored in void*. Virtual memory system converts these addresses to physical addresses. These physical addresses are not actually useful to drivers. Drivers must use ioremap() to map the space and produce a virtual address.

               CPU                  CPU                  Bus
             Virtual              Physical             Address
             Address              Address               Space
              Space                Space

            +-------+             +------+             +------+
            |       |             |MMIO  |   Offset    |      |
            |       |  Virtual    |Space |   applied   |      |
          C +-------+ --------> B +------+ ----------> +------+ A
            |       |  mapping    |      |   by host   |      |
  +-----+   |       |             |      |   bridge    |      |   +--------+
  |     |   |       |             +------+             |      |   |        |
  | CPU |   |       |             | RAM  |             |      |   | Device |
  |     |   |       |             |      |             |      |   |        |
  +-----+   +-------+             +------+             +------+   +--------+
            |       |  Virtual    |Buffer|   Mapping   |      |
          X +-------+ --------> Y +------+ <---------- +------+ Z
            |       |  mapping    | RAM  |   by IOMMU
            |       |             |      |
            |       |             |      |
            +-------+             +------+

如果设备支持DMA,则驱动程序将使用kmalloc或类似的接口设置缓冲区,以返回虚拟地址(X).虚拟的内存系统将X映射到系统RAM中的物理地址(Y).司机可以使用虚拟地址X来访问缓冲区,但是设备本身不能,因为DMA不会通过CPU虚拟内存系统.在某些系统中,只有设备可以直接对物理地址进行DMA.在某些系统中,IOMMU硬件用于将DMA地址转换为物理地址.请参见上图,将Z转换为Y.

If device supports DMA , the driver sets up buffer using kmalloc or similar interface which returns virtual address (X). The virtualmemory system maps X to a physical address (Y) in system RAM. The drivercan use virtual address X to access the buffer, but the device itselfcannot because DMA doesn't go through the CPU virtual memory system. In some system only Device can directly do DMA to physical address.In some system IOMMU hardware is used to translate DMA address to physical address.Look at the figure above It translate Z to Y.

使用DMA映射API的原因是驱动程序可以将虚拟地址X返回到dma_map_single()之类的接口,该接口设置了任何必需的IOMMU映射并返回DMA地址Z,然后驱动程序告知设备将DMA执行到Z,然后IOMMU将其映射到系统中地址Y的缓冲区RAM.

Reason to use DMA mapping API is driver can return virtual address X to interface like dma_map_single(), which sets up any required IOMMUmapping and returns the DMA address Z.The driver then tells the device todo DMA to Z, and the IOMMU maps it to the buffer at address Y in systemRAM.

引用来自此链接.

一个简单的PCI DMA示例

在Linux内核内部,您可以查看 drivers/dma 驱动程序.

Inside linux kernel you can look to drivers/dma for various real drivers.

这篇关于在Linux内核的上下文中,什么是DMA映射和DMA引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:57