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

