的物理地址,设备地址和virtiual地址之间的差值 [英] difference between the physical address,device address and virtiual address

查看:203
本文介绍了的物理地址,设备地址和virtiual地址之间的差值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么的设备地址物理地址的和的虚拟地址的?

其实我想为 MMAP 的司机,我坚持这样的理念。

Actually I am trying for mmap in drivers, I am stuck on this concept.

推荐答案

的<一个href=\"http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/DMA-API-HOWTO.txt\"相对=nofollow>文档说:

内核通常使用虚拟地址。任何地址由返回
  的kmalloc(),vmalloc的(),以及类似的接口是一个虚拟地址和可以
  被存储在一个无效*

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 *".

的虚拟存储器系统(TLB页表等)转换的虚拟
  地址给CPU的物理地址,其被存储为phys_addr_t或
  resource_size_t。内核管理设备资源,如寄存器,
  物理地址。这些都是在/ proc / iomem中的地址。物理
  地址不给驾驶员直接有用它必须使用的ioremap()映射
  的空间,并产生一个虚拟地址。

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设备使用第三种地址:一个总线地址或DMA地址。
  如果一个设备在一个MMIO地址具有寄存器,或者如果它执行的DMA读
  或写入系统存储器,由该装置所用的地址是总线地址。
  在一些系统中,总线地址是相同的CPU的物理地址,但
  一般他们都没有。 IOMMUs和主桥可以产生任意
  物理和总线地址之间的映射。

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


  
  

在枚举过程中,内核得知I / O设备和
  其MMIO空间和它们连接到系统的主桥。对于
  例如,如果PCI设备具有一个BAR,内核读出总线地址(A)
  从BAR,并将其转换到CPU的物理地址(B)。地址b
  存储在资源结构,通常通过/ proc / iomem中暴露出来。当一个
  司机声称装置,它通常使用的ioremap()映射到物理地址
  b。在一个虚拟地址(C)。然后,它可以用,例如,ioread32(C)中,以访问
  该设备在总线地址的注册。

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.

如果设备支持DMA时,司机通过kmalloc进行设置一个缓冲器()或
  类似的界面,它返回一个虚拟地址(X)。虚拟
  内存映射系统X要在系统RAM中的物理地址(Y)。司机
  可以使用虚拟地址X来访问缓冲器,但设备本身
  不能因为DMA不经过CPU的虚拟内存系统。

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.

在一些简单的系统中,设备可以直接做DMA将物理地址
  Y.但在许多其他国家,有IOMMU硬件总线转换
  到物理地址的地址,例如,将其转换Z到Y.这是一部分
  为DMA API的原因:司机可以给一个虚拟地址X来
  像dma_map_single接口(),其中规定了所有必需的IOMMU
  映射和返回总线地址Z.然后,驱动程序通知设备
  做DMA到Z,和IOMMU它的寻址Y映射到缓冲系统
  RAM。

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.

这篇关于的物理地址,设备地址和virtiual地址之间的差值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆