What is DMA mapping and DMA engine in context of linux kernel?
Asked Answered
A

2

10

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.

Almire answered 25/12, 2015 at 15:5 Comment(2)
DMA mapping is a conversion from virtual addressed memory to a memory which is DMA-able on physical addresses (actually bus addresses). You have to read a kernel documentation for that kernel.org/doc/Documentation/DMA-API-HOWTO.txtPaget
Sorry @AndyShevchenko I haven't had any idea that you have posted comment.I was writing the answer before your comment. I hope you don't mind I have answered the question.Gracye
G
8

What is DMA mapping and DMA engine in context of linux kernel?

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
            |       |             |      |
            |       |             |      |
            +-------+             +------+

If device supports DMA , the driver sets up buffer using kmalloc or similar interface which returns virtual address (X). The virtual memory system maps X to a physical address (Y) in system RAM. The driver can use virtual address X to access the buffer, but the device itself cannot 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.

When DMA mapping API can be used in Linux Device Driver?

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

Reference is taken from this link.

Any real Linux Device Driver example as a reference would be great.

A simple PCI DMA example

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

Gracye answered 25/12, 2015 at 17:20 Comment(3)
Could you please point out the driver in kernel source tree on lxr.free-electrons.com for real example of the concept?Almire
@JagsVG satisfied or Are you expecting something else ?Gracye
why is it required to memory map dma even in cases where dma engine is in device (like in pci)?Carapace
Z
0

dmaengine is a generic kernel framework for developing a DMA controller drivers.

You can read: dmaengine provider . You can find numerous examples of dmaengine drivers under drivers/dma.

Zoara answered 25/12, 2015 at 15:56 Comment(1)
It needs an answer to the first part of the question as well. See my comment above.Paget

© 2022 - 2024 — McMap. All rights reserved.