bus-virt-phys-mapping.txt

Chinese translated version of Documentation\filesystems\nfs


If you have any comment or update to the content, please contact the
original document maintainer directly.  However, if you have a problem
communicating in English you can also ask the Chinese maintainer for
help.  Contact the Chinese maintainer if this translation is outdated
or if there is a problem with the translation.


Chinese maintainer: yingli tao<tyl18768122426@163.com>
---------------------------------------------------------------------
DocumentationDocumentation\filesystems\nfs的中文翻译

 

如果想评论或更新本文的内容,请直接联系原文档的维护者。

如果你使用英文交流有困难的话,也可以向中文版维护者求助。

如果本翻译更新不及时或者翻译存在问题,请联系中文版维护者。

中文版维护者: 陶莹莉  tyl18768122426@163.com

中文版翻译者:  陶莹莉  tyl18768122426@163.com

中文版校译者:  陶莹莉  tyl18768122426@163.com

[ NOTE: The virt_to_bus() and bus_to_virt() functions have been
    superseded by the functionality provided by the PCI DMA interface
    (see Documentation/DMA-API-HOWTO.txt).  They continue
    to be documented below for historical purposes, but new code
    must not use them. --davidm 00/12/12 ]

[注:virt_to_bus和bus_to_virt()函数已被由PCI DMA接口提供的功能所取代
(请参阅Documentation/DMA-API-HOWTO.txt)。他们继续实现记录历史的目的,但新代码
不会使用它们。 - davidm 00/12/12 ]

[ This is a mail message in response to a query on IO mapping, thus the
  strange format for a "document" ]

  [这是一个响应一个查询IO映射的电子邮件消息,因此这个奇怪的格式为“文件”]
 
The AHA-1542 is a bus-master device, and your patch makes the driver give the
controller the physical address of the buffers, which is correct on x86
(because all bus master devices see the physical memory mappings directly).

AHA-1542是一个总线主设备,补丁驱使驱动器将缓冲区的物理地址给控制器,在x86上这是正确的
(因为所有的总线主设备都可以直接看到物理内存的映射)。 ]

However, on many setups, there are actually _three_ different ways of looking
at memory addresses, and in this case we actually want the third, the
so-called "bus address".

然而,在许多系统中,实际上有三种不同的方式来看待内存地址,在这种情况下,
我们实际上想要第三种,即所谓的“总线地址”。

Essentially, the three ways of addressing memory are (this is "real memory",
that is, normal RAM--see later about other details):

 - CPU untranslated.  This is the "physical" address.  Physical address
   0 is what the CPU sees when it drives zeroes on the memory bus.

 - CPU translated address. This is the "virtual" address, and is
   completely internal to the CPU itself with the CPU doing the appropriate
   translations into "CPU untranslated".

 - bus address. This is the address of memory as seen by OTHER devices,
   not the CPU. Now, in theory there could be many different bus
   addresses, with each device seeing memory in some device-specific way, but
   happily most hardware designers aren't actually actively trying to make
   things any more complex than necessary, so you can assume that all
   external hardware sees the memory the same way.

从本质上讲,内存寻址的三种方式(这是“真实的存储器”,也就是说,
正常的内存 -有关其他详细信息后面再看):

  -  未转换的CPU地址。这是“物理”地址。想知道物理地址0是什么样的CPU时,将它推动到零内存总线上。

  -  转换后的CPU地址。这是一个“虚拟”的地址,并且完全是由CPU内部执行适当的转换成“未转换的CPU地址”。

  - 总线地址。这是其他设备所看到的内存地址,不是CPU。现在,在理论上可以有许多不同的总线
   地址,每个设备通过某些特定的移动设备看到存储器,但大多数硬件设计师不会使事情变得更复杂的,
   当没必要时,所以你可以假设所有外部硬件通过相同的方式看到内存。
   
Now, on normal PCs the bus address is exactly the same as the physical
address, and things are very simple indeed. However, they are that simple
because the memory and the devices share the same address space, and that is
not generally necessarily true on other PCI/ISA setups.

现在,在正常的PC上的总线地址是完全一样的物理地址,这的确是很简单的事情。
然而,他们那么简单是因为存储器和设备共享相同的地址空间,这就是其他PCI/ ISA设置并不一定是真实。

Now, just as an example, on the PReP (PowerPC Reference Platform), the
CPU sees a memory map something like this (this is from memory):

    0-2 GB        "real memory"
    2 GB-3 GB    "system IO" (inb/out and similar accesses on x86)
    3 GB-4 GB     "IO memory" (shared memory over the IO bus)

现在,只是作为一个例子,在PREP上(PowerPC的参考平台),
CPU看到这样的内存映射(来自内存):

    0-2 GB        “真实内存”
    2 GB-3 GB    “系统IO“(输入/输出和x86上的访问类似)
    3 GB-4 GB    “IO内存”(在IO总线上共享内存)

Now, that looks simple enough. However, when you look at the same thing from
the viewpoint of the devices, you have the reverse, and the physical memory
address 0 actually shows up as address 2 GB for any IO master.

现在,这看起来很简单。然而,当你从设备的观点出发看同样的事情,你会有反向的发现,
物理内存0在任何的IO地址上实际显示地址为2 GB。

So when the CPU wants any bus master to write to physical memory 0, it
has to give the master address 0x80000000 as the memory address.

所以当CPU希望任何总线主机写入到物理内存0,它必须给主机地址为0x80000000的内存地址。

So, for example, depending on how the kernel is actually mapped on the
PPC, you can end up with a setup like this:

 physical address:    0
 virtual address:    0xC0000000
 bus address:        0x80000000

因此,举例来说,取决于内核如何实际上映射到PPC,你可以这样的设置结束:

 物理地址:0
 虚拟地址:0xC0000000
 总线地址:0X80000000
 
where all the addresses actually point to the same thing.  It's just seen
through different translations..

所有地址指向同样的事件。只是通过不同的转换..

Similarly, on the Alpha, the normal translation is

 physical address:    0
 virtual address:    0xfffffc0000000000
 bus address:        0x40000000

(but there are also Alphas where the physical address and the bus address
are the same).

同样,在Alpha上,正常的转换是

 物理地址:0
 虚拟地址:0xfffffc0000000000
 总线地址:0x40000000

(但有的Alpha的物理地址和总线地址是相同的)。

Anyway, the way to look up all these translations, you do

    #include <asm/io.h>

    phys_addr = virt_to_phys(virt_addr);
    virt_addr = phys_to_virt(phys_addr);
     bus_addr = virt_to_bus(virt_addr);
    virt_addr = bus_to_virt(bus_addr);

总之,你可以这样做来实现所有的转换方式:

    #include<asm/io.h>“

    phys_addr= virt_to_phys(virt_addr);
    virt_addr= phys_to_virt(phys_addr);
    bus_addr= virt_to_bus的(virt_addr);
    virt_addr= bus_to_virt(bus_addr);

Now, when do you need these?

那么现在,你什么时候会需要这些?

You want the _virtual_ address when you are actually going to access that
pointer from the kernel. So you can have something like this:

    /*
     * this is the hardware "mailbox" we use to communicate with
     * the controller. The controller sees this directly.
     */
    struct mailbox {
        __u32 status;
        __u32 bufstart;
        __u32 buflen;
        ..
    } mbox;

        unsigned char * retbuffer;

        /* get the address from the controller */
        retbuffer = bus_to_virt(mbox.bufstart);
        switch (retbuffer[0]) {
            case STATUS_OK:
                ...

当你实际上是在访问内核的指针时,你想要虚拟地址。你可以这样做:

/ *
*这是硬件的“信箱”,我们用与控制器来沟通。是控制器能直接看到的
*/
struct mailbox {
        __u32 status;
        __u32 bufstart;
        __u32 buflen;
        ..
    } mbox;

        unsigned char * retbuffer;

        /* get the address from the controller */
        retbuffer = bus_to_virt(mbox.bufstart);
        switch (retbuffer[0]) {
            case STATUS_OK:
                ...

on the other hand, you want the bus address when you have a buffer that
you want to give to the controller:

    /* ask the controller to read the sense status into "sense_buffer" */
    mbox.bufstart = virt_to_bus(&sense_buffer);
    mbox.buflen = sizeof(sense_buffer);
    mbox.status = 0;
    notify_controller(&mbox);
    
另一方面,当你有一个缓冲区想给控制器时,你要总线地址:

    / *要求控制器将读入的状态改成“sense_buffer”*/
    mbox.bufstart= virt_to_bus的(sense_buffer);
    mbox.buflen= sizeof(sense_buffer)的;
    mbox.status= 0;
    notify_controller(MBOX);

And you generally _never_ want to use the physical address, because you can't
use that from the CPU (the CPU only uses translated virtual addresses), and
you can't use it from the bus master.

一般而言你不会想要使用物理地址,因为你不能从CPU使用这个(CPU只使用转换后的虚拟地址),
并且你也不能从总线使用它。

So why do we care about the physical address at all? We do need the physical
address in some cases, it's just not very often in normal code.  The physical
address is needed if you use memory mappings, for example, because the
"remap_pfn_range()" mm function wants the physical address of the memory to
be remapped as measured in units of pages, a.k.a. the pfn (the memory
management layer doesn't know about devices outside the CPU, so it
shouldn't need to know about "bus addresses" etc).

那么,为什么我们要关心物理地址?我们确实需要物理地址在某些情况下,只是在正常的代码它不是经常被使用。
如果你使用内存映射物理地址是必要的,例如:因为“remap_pfn_range()”的功能希望存储器的物理地址
被重映射为测量单位的页面,又名PFN(内存管理者不知道在CPU之外的设备,所以它
不应该需要了解“总线地址”等)。

NOTE NOTE NOTE! The above is only one part of the whole equation. The above
only talks about "real memory", that is, CPU memory (RAM).

注意 注意 注意!以上只是整个公式其中的一部分。以上只谈“真实记忆”,就是CPU内存(RAM)。

There is a completely different type of memory too, and that's the "shared
memory" on the PCI or ISA bus. That's generally not RAM (although in the case
of a video graphics card it can be normal DRAM that is just used for a frame
buffer), but can be things like a packet buffer in a network card etc.

有一个完全不同类型的内存,就是在PCI或ISA总线的“共享内存“。一般情况下这不是内存
(虽然在视频图形卡的情况下,它可以是正常的只是用于一帧缓冲的动态内存,)
但可以是如在一块网卡的一个分组缓冲器

This memory is called "PCI memory" or "shared memory" or "IO memory" or
whatever, and there is only one way to access it: the readb/writeb and
related functions. You should never take the address of such memory, because
there is really nothing you can do with such an address: it's not
conceptually in the same memory space as "real memory" at all, so you cannot
just dereference a pointer. (Sadly, on x86 it _is_ in the same memory space,
so on x86 it actually works to just deference a pointer, but it's not
portable).

这种内存被称为“PCI内存”或“共享内存”或“IO内存”或其他什么,只有一个方法来访问它:
只读 或只写和相关的功能。你永远不可能取得这样的内存地址,因为真的是没有什么可以做为这样一个地址:不是
在概念上的在相同的内存空间的“真实内存”,所以你不能只是反引用指针。 (可悲的是,在x86上,它就是“在相同的内存空间,
所以在x86实际工作只是反引用一个指针,但它不是可移植的)。

For such memory, you can do things like

 - reading:
    /*
     * read first 32 bits from ISA memory at 0xC0000, aka
     * C000:0000 in DOS terms
     */
    unsigned int signature = isa_readl(0xC0000);

 - remapping and writing:
    /*
     * remap framebuffer PCI memory area at 0xFC000000,
     * size 1MB, so that we can access it: We can directly
     * access only the 640k-1MB area, so anything else
     * has to be remapped.
     */
    void __iomem *baseptr = ioremap(0xFC000000, 1024*1024);

    /* write a 'A' to the offset 10 of the area */
    writeb('A',baseptr+10);

    /* unmap when we unload the driver */
    iounmap(baseptr);

 - copying and clearing:
    /* get the 6-byte Ethernet address at ISA address E000:0040 */
    memcpy_fromio(kernel_buffer, 0xE0040, 6);
    /* write a packet to the driver */
    memcpy_toio(0xE1000, skb->data, skb->len);
    /* clear the frame buffer */
    memset_io(0xA0000, 0, 0x10000);

    对于这样的内存,你可以做这样的事情

  - 阅读:
    / *
    *在0xC0000的位置从ISA内存先读32位
    * 即C000:0000的位置在DOS中
    */
    unsigned int signature = isa_readl(0xC0000);

  - 重映射和写入:
    / *
    *在0xFC000000重映射帧缓冲区PCI内存
    *大小为1MB,这样我们就可以访问它,我们可以直接
    *访问只有640K-1MB的区域,所以其他的都要被重映射。
    */
    void __iomem *baseptr = ioremap(0xFC000000, 1024*1024);

    / *写一个'A'到偏移为10的区域* /
    WRITEB('A',+10 baseptr);

    / *取消映射,当我们卸载驱动程序* /
    iounmap(baseptr);

    - 复制和清除:
    / *得到6字节的以太网地址在ISA地址为E000:0040的位置*/
    memcpy_fromio(kernel_buffer0xE0040,6);
    / *写一个数据包的驱动程序* /
    memcpy_toio(0xE1000,SKB->数据,SKB-> len个);
    / *清除帧缓冲区*/
    memset_io(0xa0000为0,为0x10000);

OK, that just about covers the basics of accessing IO portably.  Questions?
Comments? You may think that all the above is overly complex, but one day you
might find yourself with a 500 MHz Alpha in front of you, and then you'll be
happy that your driver works ;)

好吧,那只是关于访问IO可移植的基本内容。有问题吗?
要发表评论吗?你可能会认为,所有上述过于复杂,但有一天你
可能会发现有个500 MHz的阿尔法在你面前,然后你就会
高兴你的驱动器的工作了;)

Note that kernel versions 2.0.x (and earlier) mistakenly called the
ioremap() function "vremap()".  ioremap() is the proper name, but I
didn't think straight when I wrote it originally.  People who have to
support both can do something like:
 
    /* support old naming silliness */
    #if LINUX_VERSION_CODE < 0x020100                                     
    #define ioremap vremap
    #define iounmap vfree                                                     
    #endif
 
at the top of their source files, and then they can use the right names
even on 2.0.x systems.

需要注意的是内核2.0.x的版本(或更早)将ioremap()函数误称为
“vremap()”。 ioremap的()是正确的名字,但我
没想到当我开始写它。该支持的人都做这样的事情:
 
    /*支持旧的命名愚蠢* /
    #if LINUX_VERSION_CODE < 0x020100                                     
    #define ioremap vremap
    #define iounmap vfree                                                     
    #endif
    
位于其源文件的顶部,即使在2.0.x的系统。他们可以使用正确的名称


And the above sounds worse than it really is.  Most real drivers really
don't do all that complex things (or rather: the complexity is not so
much in the actual IO accesses as in error handling and timeouts etc).
It's generally not hard to fix drivers, and in many cases the code
actually looks better afterwards:

    unsigned long signature = *(unsigned int *) 0xC0000;
        vs
    unsigned long signature = readl(0xC0000);

I think the second version actually is more readable, no?

        Linus
上述听起来它确实是差。很多真正的驱动其实不做这样复杂的事情(或者说:
在实际的IO访问处理错误和超时也不是那么复杂等)。
这通常不是很难解决的驱动程序,在许多情况下,以下代码实际上看起来更好:


    unsigned long signature = *(unsigned int *) 0xC0000;
        vs
    unsigned long signature = readl(0xC0000);


我认为第二个版本,实际上更具可读性,不是吗?

    莱纳斯


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值