difference between the physical address,device address and virtiual address

前端 未结 1 1473
醉梦人生
醉梦人生 2021-02-10 19:43

What is the difference between device address, physical address and virtual address?

Actually I am trying for mmap in drivers, I

1条回答
  •  忘了有多久
    2021-02-10 20:28

    The documentation says:

    The kernel normally uses virtual addresses. Any address returned by kmalloc(), vmalloc(), and similar interfaces is a virtual address and can be stored in a "void *".

    The virtual memory system (TLB, page tables, etc.) translates virtual addresses to CPU physical addresses, which are stored as "phys_addr_t" or "resource_size_t". The kernel manages device resources like registers as physical addresses. These are the addresses in /proc/iomem. The physical address is not directly useful to a driver; it must use ioremap() to map the space and produce a virtual address.

    I/O devices use a third kind of address: a "bus address" or "DMA address". If a device has registers at an MMIO address, or if it performs DMA to read or write system memory, the addresses used by the device are bus addresses. In some systems, bus addresses are identical to CPU physical addresses, but in general they are not. IOMMUs and host bridges can produce arbitrary mappings between physical and bus addresses.

    Here's a picture and some examples:

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

    During the enumeration process, the kernel learns about I/O devices and their MMIO space and the host bridges that connect them to the system. For example, if a PCI device has a BAR, the kernel reads the bus address (A) from the BAR and converts it to a CPU physical address (B). The address B is stored in a struct resource and usually exposed via /proc/iomem. When a driver claims a device, it typically uses ioremap() to map physical address B at a virtual address (C). It can then use, e.g., ioread32(C), to access the device registers at bus address A.

    If the device supports DMA, the driver sets up a buffer using kmalloc() or a similar interface, which returns a 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 simple systems, the device can do DMA directly to physical address Y. But in many others, there is IOMMU hardware that translates bus addresses to physical addresses, e.g., it translates Z to Y. This is part of the reason for the DMA API: the driver can give a virtual address X to an interface like dma_map_single(), which sets up any required IOMMU mapping and returns the bus 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.

    0 讨论(0)
提交回复
热议问题