1*4882a593Smuzhiyun========================================================== 2*4882a593SmuzhiyunHow to access I/O mapped memory from within device drivers 3*4882a593Smuzhiyun========================================================== 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun:Author: Linus 6*4882a593Smuzhiyun 7*4882a593Smuzhiyun.. warning:: 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun The virt_to_bus() and bus_to_virt() functions have been 10*4882a593Smuzhiyun superseded by the functionality provided by the PCI DMA interface 11*4882a593Smuzhiyun (see :doc:`/core-api/dma-api-howto`). They continue 12*4882a593Smuzhiyun to be documented below for historical purposes, but new code 13*4882a593Smuzhiyun must not use them. --davidm 00/12/12 14*4882a593Smuzhiyun 15*4882a593Smuzhiyun:: 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun [ This is a mail message in response to a query on IO mapping, thus the 18*4882a593Smuzhiyun strange format for a "document" ] 19*4882a593Smuzhiyun 20*4882a593SmuzhiyunThe AHA-1542 is a bus-master device, and your patch makes the driver give the 21*4882a593Smuzhiyuncontroller the physical address of the buffers, which is correct on x86 22*4882a593Smuzhiyun(because all bus master devices see the physical memory mappings directly). 23*4882a593Smuzhiyun 24*4882a593SmuzhiyunHowever, on many setups, there are actually **three** different ways of looking 25*4882a593Smuzhiyunat memory addresses, and in this case we actually want the third, the 26*4882a593Smuzhiyunso-called "bus address". 27*4882a593Smuzhiyun 28*4882a593SmuzhiyunEssentially, the three ways of addressing memory are (this is "real memory", 29*4882a593Smuzhiyunthat is, normal RAM--see later about other details): 30*4882a593Smuzhiyun 31*4882a593Smuzhiyun - CPU untranslated. This is the "physical" address. Physical address 32*4882a593Smuzhiyun 0 is what the CPU sees when it drives zeroes on the memory bus. 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun - CPU translated address. This is the "virtual" address, and is 35*4882a593Smuzhiyun completely internal to the CPU itself with the CPU doing the appropriate 36*4882a593Smuzhiyun translations into "CPU untranslated". 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun - bus address. This is the address of memory as seen by OTHER devices, 39*4882a593Smuzhiyun not the CPU. Now, in theory there could be many different bus 40*4882a593Smuzhiyun addresses, with each device seeing memory in some device-specific way, but 41*4882a593Smuzhiyun happily most hardware designers aren't actually actively trying to make 42*4882a593Smuzhiyun things any more complex than necessary, so you can assume that all 43*4882a593Smuzhiyun external hardware sees the memory the same way. 44*4882a593Smuzhiyun 45*4882a593SmuzhiyunNow, on normal PCs the bus address is exactly the same as the physical 46*4882a593Smuzhiyunaddress, and things are very simple indeed. However, they are that simple 47*4882a593Smuzhiyunbecause the memory and the devices share the same address space, and that is 48*4882a593Smuzhiyunnot generally necessarily true on other PCI/ISA setups. 49*4882a593Smuzhiyun 50*4882a593SmuzhiyunNow, just as an example, on the PReP (PowerPC Reference Platform), the 51*4882a593SmuzhiyunCPU sees a memory map something like this (this is from memory):: 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun 0-2 GB "real memory" 54*4882a593Smuzhiyun 2 GB-3 GB "system IO" (inb/out and similar accesses on x86) 55*4882a593Smuzhiyun 3 GB-4 GB "IO memory" (shared memory over the IO bus) 56*4882a593Smuzhiyun 57*4882a593SmuzhiyunNow, that looks simple enough. However, when you look at the same thing from 58*4882a593Smuzhiyunthe viewpoint of the devices, you have the reverse, and the physical memory 59*4882a593Smuzhiyunaddress 0 actually shows up as address 2 GB for any IO master. 60*4882a593Smuzhiyun 61*4882a593SmuzhiyunSo when the CPU wants any bus master to write to physical memory 0, it 62*4882a593Smuzhiyunhas to give the master address 0x80000000 as the memory address. 63*4882a593Smuzhiyun 64*4882a593SmuzhiyunSo, for example, depending on how the kernel is actually mapped on the 65*4882a593SmuzhiyunPPC, you can end up with a setup like this:: 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun physical address: 0 68*4882a593Smuzhiyun virtual address: 0xC0000000 69*4882a593Smuzhiyun bus address: 0x80000000 70*4882a593Smuzhiyun 71*4882a593Smuzhiyunwhere all the addresses actually point to the same thing. It's just seen 72*4882a593Smuzhiyunthrough different translations.. 73*4882a593Smuzhiyun 74*4882a593SmuzhiyunSimilarly, on the Alpha, the normal translation is:: 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun physical address: 0 77*4882a593Smuzhiyun virtual address: 0xfffffc0000000000 78*4882a593Smuzhiyun bus address: 0x40000000 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun(but there are also Alphas where the physical address and the bus address 81*4882a593Smuzhiyunare the same). 82*4882a593Smuzhiyun 83*4882a593SmuzhiyunAnyway, the way to look up all these translations, you do:: 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun #include <asm/io.h> 86*4882a593Smuzhiyun 87*4882a593Smuzhiyun phys_addr = virt_to_phys(virt_addr); 88*4882a593Smuzhiyun virt_addr = phys_to_virt(phys_addr); 89*4882a593Smuzhiyun bus_addr = virt_to_bus(virt_addr); 90*4882a593Smuzhiyun virt_addr = bus_to_virt(bus_addr); 91*4882a593Smuzhiyun 92*4882a593SmuzhiyunNow, when do you need these? 93*4882a593Smuzhiyun 94*4882a593SmuzhiyunYou want the **virtual** address when you are actually going to access that 95*4882a593Smuzhiyunpointer from the kernel. So you can have something like this:: 96*4882a593Smuzhiyun 97*4882a593Smuzhiyun /* 98*4882a593Smuzhiyun * this is the hardware "mailbox" we use to communicate with 99*4882a593Smuzhiyun * the controller. The controller sees this directly. 100*4882a593Smuzhiyun */ 101*4882a593Smuzhiyun struct mailbox { 102*4882a593Smuzhiyun __u32 status; 103*4882a593Smuzhiyun __u32 bufstart; 104*4882a593Smuzhiyun __u32 buflen; 105*4882a593Smuzhiyun .. 106*4882a593Smuzhiyun } mbox; 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun unsigned char * retbuffer; 109*4882a593Smuzhiyun 110*4882a593Smuzhiyun /* get the address from the controller */ 111*4882a593Smuzhiyun retbuffer = bus_to_virt(mbox.bufstart); 112*4882a593Smuzhiyun switch (retbuffer[0]) { 113*4882a593Smuzhiyun case STATUS_OK: 114*4882a593Smuzhiyun ... 115*4882a593Smuzhiyun 116*4882a593Smuzhiyunon the other hand, you want the bus address when you have a buffer that 117*4882a593Smuzhiyunyou want to give to the controller:: 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun /* ask the controller to read the sense status into "sense_buffer" */ 120*4882a593Smuzhiyun mbox.bufstart = virt_to_bus(&sense_buffer); 121*4882a593Smuzhiyun mbox.buflen = sizeof(sense_buffer); 122*4882a593Smuzhiyun mbox.status = 0; 123*4882a593Smuzhiyun notify_controller(&mbox); 124*4882a593Smuzhiyun 125*4882a593SmuzhiyunAnd you generally **never** want to use the physical address, because you can't 126*4882a593Smuzhiyunuse that from the CPU (the CPU only uses translated virtual addresses), and 127*4882a593Smuzhiyunyou can't use it from the bus master. 128*4882a593Smuzhiyun 129*4882a593SmuzhiyunSo why do we care about the physical address at all? We do need the physical 130*4882a593Smuzhiyunaddress in some cases, it's just not very often in normal code. The physical 131*4882a593Smuzhiyunaddress is needed if you use memory mappings, for example, because the 132*4882a593Smuzhiyun"remap_pfn_range()" mm function wants the physical address of the memory to 133*4882a593Smuzhiyunbe remapped as measured in units of pages, a.k.a. the pfn (the memory 134*4882a593Smuzhiyunmanagement layer doesn't know about devices outside the CPU, so it 135*4882a593Smuzhiyunshouldn't need to know about "bus addresses" etc). 136*4882a593Smuzhiyun 137*4882a593Smuzhiyun.. note:: 138*4882a593Smuzhiyun 139*4882a593Smuzhiyun The above is only one part of the whole equation. The above 140*4882a593Smuzhiyun only talks about "real memory", that is, CPU memory (RAM). 141*4882a593Smuzhiyun 142*4882a593SmuzhiyunThere is a completely different type of memory too, and that's the "shared 143*4882a593Smuzhiyunmemory" on the PCI or ISA bus. That's generally not RAM (although in the case 144*4882a593Smuzhiyunof a video graphics card it can be normal DRAM that is just used for a frame 145*4882a593Smuzhiyunbuffer), but can be things like a packet buffer in a network card etc. 146*4882a593Smuzhiyun 147*4882a593SmuzhiyunThis memory is called "PCI memory" or "shared memory" or "IO memory" or 148*4882a593Smuzhiyunwhatever, and there is only one way to access it: the readb/writeb and 149*4882a593Smuzhiyunrelated functions. You should never take the address of such memory, because 150*4882a593Smuzhiyunthere is really nothing you can do with such an address: it's not 151*4882a593Smuzhiyunconceptually in the same memory space as "real memory" at all, so you cannot 152*4882a593Smuzhiyunjust dereference a pointer. (Sadly, on x86 it **is** in the same memory space, 153*4882a593Smuzhiyunso on x86 it actually works to just deference a pointer, but it's not 154*4882a593Smuzhiyunportable). 155*4882a593Smuzhiyun 156*4882a593SmuzhiyunFor such memory, you can do things like: 157*4882a593Smuzhiyun 158*4882a593Smuzhiyun - reading:: 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun /* 161*4882a593Smuzhiyun * read first 32 bits from ISA memory at 0xC0000, aka 162*4882a593Smuzhiyun * C000:0000 in DOS terms 163*4882a593Smuzhiyun */ 164*4882a593Smuzhiyun unsigned int signature = isa_readl(0xC0000); 165*4882a593Smuzhiyun 166*4882a593Smuzhiyun - remapping and writing:: 167*4882a593Smuzhiyun 168*4882a593Smuzhiyun /* 169*4882a593Smuzhiyun * remap framebuffer PCI memory area at 0xFC000000, 170*4882a593Smuzhiyun * size 1MB, so that we can access it: We can directly 171*4882a593Smuzhiyun * access only the 640k-1MB area, so anything else 172*4882a593Smuzhiyun * has to be remapped. 173*4882a593Smuzhiyun */ 174*4882a593Smuzhiyun void __iomem *baseptr = ioremap(0xFC000000, 1024*1024); 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun /* write a 'A' to the offset 10 of the area */ 177*4882a593Smuzhiyun writeb('A',baseptr+10); 178*4882a593Smuzhiyun 179*4882a593Smuzhiyun /* unmap when we unload the driver */ 180*4882a593Smuzhiyun iounmap(baseptr); 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun - copying and clearing:: 183*4882a593Smuzhiyun 184*4882a593Smuzhiyun /* get the 6-byte Ethernet address at ISA address E000:0040 */ 185*4882a593Smuzhiyun memcpy_fromio(kernel_buffer, 0xE0040, 6); 186*4882a593Smuzhiyun /* write a packet to the driver */ 187*4882a593Smuzhiyun memcpy_toio(0xE1000, skb->data, skb->len); 188*4882a593Smuzhiyun /* clear the frame buffer */ 189*4882a593Smuzhiyun memset_io(0xA0000, 0, 0x10000); 190*4882a593Smuzhiyun 191*4882a593SmuzhiyunOK, that just about covers the basics of accessing IO portably. Questions? 192*4882a593SmuzhiyunComments? You may think that all the above is overly complex, but one day you 193*4882a593Smuzhiyunmight find yourself with a 500 MHz Alpha in front of you, and then you'll be 194*4882a593Smuzhiyunhappy that your driver works ;) 195*4882a593Smuzhiyun 196*4882a593SmuzhiyunNote that kernel versions 2.0.x (and earlier) mistakenly called the 197*4882a593Smuzhiyunioremap() function "vremap()". ioremap() is the proper name, but I 198*4882a593Smuzhiyundidn't think straight when I wrote it originally. People who have to 199*4882a593Smuzhiyunsupport both can do something like:: 200*4882a593Smuzhiyun 201*4882a593Smuzhiyun /* support old naming silliness */ 202*4882a593Smuzhiyun #if LINUX_VERSION_CODE < 0x020100 203*4882a593Smuzhiyun #define ioremap vremap 204*4882a593Smuzhiyun #define iounmap vfree 205*4882a593Smuzhiyun #endif 206*4882a593Smuzhiyun 207*4882a593Smuzhiyunat the top of their source files, and then they can use the right names 208*4882a593Smuzhiyuneven on 2.0.x systems. 209*4882a593Smuzhiyun 210*4882a593SmuzhiyunAnd the above sounds worse than it really is. Most real drivers really 211*4882a593Smuzhiyundon't do all that complex things (or rather: the complexity is not so 212*4882a593Smuzhiyunmuch in the actual IO accesses as in error handling and timeouts etc). 213*4882a593SmuzhiyunIt's generally not hard to fix drivers, and in many cases the code 214*4882a593Smuzhiyunactually looks better afterwards:: 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun unsigned long signature = *(unsigned int *) 0xC0000; 217*4882a593Smuzhiyun vs 218*4882a593Smuzhiyun unsigned long signature = readl(0xC0000); 219*4882a593Smuzhiyun 220*4882a593SmuzhiyunI think the second version actually is more readable, no? 221