Path: blob/master/Documentation/bus-virt-phys-mapping.txt
10820 views
[ NOTE: The virt_to_bus() and bus_to_virt() functions have been1superseded by the functionality provided by the PCI DMA interface2(see Documentation/PCI/PCI-DMA-mapping.txt). They continue3to be documented below for historical purposes, but new code4must not use them. --davidm 00/12/12 ]56[ This is a mail message in response to a query on IO mapping, thus the7strange format for a "document" ]89The AHA-1542 is a bus-master device, and your patch makes the driver give the10controller the physical address of the buffers, which is correct on x8611(because all bus master devices see the physical memory mappings directly).1213However, on many setups, there are actually _three_ different ways of looking14at memory addresses, and in this case we actually want the third, the15so-called "bus address".1617Essentially, the three ways of addressing memory are (this is "real memory",18that is, normal RAM--see later about other details):1920- CPU untranslated. This is the "physical" address. Physical address210 is what the CPU sees when it drives zeroes on the memory bus.2223- CPU translated address. This is the "virtual" address, and is24completely internal to the CPU itself with the CPU doing the appropriate25translations into "CPU untranslated".2627- bus address. This is the address of memory as seen by OTHER devices,28not the CPU. Now, in theory there could be many different bus29addresses, with each device seeing memory in some device-specific way, but30happily most hardware designers aren't actually actively trying to make31things any more complex than necessary, so you can assume that all32external hardware sees the memory the same way.3334Now, on normal PCs the bus address is exactly the same as the physical35address, and things are very simple indeed. However, they are that simple36because the memory and the devices share the same address space, and that is37not generally necessarily true on other PCI/ISA setups.3839Now, just as an example, on the PReP (PowerPC Reference Platform), the40CPU sees a memory map something like this (this is from memory):41420-2 GB "real memory"432 GB-3 GB "system IO" (inb/out and similar accesses on x86)443 GB-4 GB "IO memory" (shared memory over the IO bus)4546Now, that looks simple enough. However, when you look at the same thing from47the viewpoint of the devices, you have the reverse, and the physical memory48address 0 actually shows up as address 2 GB for any IO master.4950So when the CPU wants any bus master to write to physical memory 0, it51has to give the master address 0x80000000 as the memory address.5253So, for example, depending on how the kernel is actually mapped on the54PPC, you can end up with a setup like this:5556physical address: 057virtual address: 0xC000000058bus address: 0x800000005960where all the addresses actually point to the same thing. It's just seen61through different translations..6263Similarly, on the Alpha, the normal translation is6465physical address: 066virtual address: 0xfffffc000000000067bus address: 0x400000006869(but there are also Alphas where the physical address and the bus address70are the same).7172Anyway, the way to look up all these translations, you do7374#include <asm/io.h>7576phys_addr = virt_to_phys(virt_addr);77virt_addr = phys_to_virt(phys_addr);78bus_addr = virt_to_bus(virt_addr);79virt_addr = bus_to_virt(bus_addr);8081Now, when do you need these?8283You want the _virtual_ address when you are actually going to access that84pointer from the kernel. So you can have something like this:8586/*87* this is the hardware "mailbox" we use to communicate with88* the controller. The controller sees this directly.89*/90struct mailbox {91__u32 status;92__u32 bufstart;93__u32 buflen;94..95} mbox;9697unsigned char * retbuffer;9899/* get the address from the controller */100retbuffer = bus_to_virt(mbox.bufstart);101switch (retbuffer[0]) {102case STATUS_OK:103...104105on the other hand, you want the bus address when you have a buffer that106you want to give to the controller:107108/* ask the controller to read the sense status into "sense_buffer" */109mbox.bufstart = virt_to_bus(&sense_buffer);110mbox.buflen = sizeof(sense_buffer);111mbox.status = 0;112notify_controller(&mbox);113114And you generally _never_ want to use the physical address, because you can't115use that from the CPU (the CPU only uses translated virtual addresses), and116you can't use it from the bus master.117118So why do we care about the physical address at all? We do need the physical119address in some cases, it's just not very often in normal code. The physical120address is needed if you use memory mappings, for example, because the121"remap_pfn_range()" mm function wants the physical address of the memory to122be remapped as measured in units of pages, a.k.a. the pfn (the memory123management layer doesn't know about devices outside the CPU, so it124shouldn't need to know about "bus addresses" etc).125126NOTE NOTE NOTE! The above is only one part of the whole equation. The above127only talks about "real memory", that is, CPU memory (RAM).128129There is a completely different type of memory too, and that's the "shared130memory" on the PCI or ISA bus. That's generally not RAM (although in the case131of a video graphics card it can be normal DRAM that is just used for a frame132buffer), but can be things like a packet buffer in a network card etc.133134This memory is called "PCI memory" or "shared memory" or "IO memory" or135whatever, and there is only one way to access it: the readb/writeb and136related functions. You should never take the address of such memory, because137there is really nothing you can do with such an address: it's not138conceptually in the same memory space as "real memory" at all, so you cannot139just dereference a pointer. (Sadly, on x86 it _is_ in the same memory space,140so on x86 it actually works to just deference a pointer, but it's not141portable).142143For such memory, you can do things like144145- reading:146/*147* read first 32 bits from ISA memory at 0xC0000, aka148* C000:0000 in DOS terms149*/150unsigned int signature = isa_readl(0xC0000);151152- remapping and writing:153/*154* remap framebuffer PCI memory area at 0xFC000000,155* size 1MB, so that we can access it: We can directly156* access only the 640k-1MB area, so anything else157* has to be remapped.158*/159void __iomem *baseptr = ioremap(0xFC000000, 1024*1024);160161/* write a 'A' to the offset 10 of the area */162writeb('A',baseptr+10);163164/* unmap when we unload the driver */165iounmap(baseptr);166167- copying and clearing:168/* get the 6-byte Ethernet address at ISA address E000:0040 */169memcpy_fromio(kernel_buffer, 0xE0040, 6);170/* write a packet to the driver */171memcpy_toio(0xE1000, skb->data, skb->len);172/* clear the frame buffer */173memset_io(0xA0000, 0, 0x10000);174175OK, that just about covers the basics of accessing IO portably. Questions?176Comments? You may think that all the above is overly complex, but one day you177might find yourself with a 500 MHz Alpha in front of you, and then you'll be178happy that your driver works ;)179180Note that kernel versions 2.0.x (and earlier) mistakenly called the181ioremap() function "vremap()". ioremap() is the proper name, but I182didn't think straight when I wrote it originally. People who have to183support both can do something like:184185/* support old naming silliness */186#if LINUX_VERSION_CODE < 0x020100187#define ioremap vremap188#define iounmap vfree189#endif190191at the top of their source files, and then they can use the right names192even on 2.0.x systems.193194And the above sounds worse than it really is. Most real drivers really195don't do all that complex things (or rather: the complexity is not so196much in the actual IO accesses as in error handling and timeouts etc).197It's generally not hard to fix drivers, and in many cases the code198actually looks better afterwards:199200unsigned long signature = *(unsigned int *) 0xC0000;201vs202unsigned long signature = readl(0xC0000);203204I think the second version actually is more readable, no?205206Linus207208209210