1 [ This is a mail message in response to a query on IO mapping, thus the
2 strange format for a "document" ]
4 The AHA-1542 is a bus-master device, and your patch makes the driver give the
5 controller the physical address of the buffers, which is correct on x86
6 (because all bus master devices see the physical memory mappings directly).
8 However, on many setups, there are actually _three_ different ways of looking
9 at memory addresses, and in this case we actually want the third, the
10 so-called "bus address".
12 Essentially, the three ways of addressing memory are (this is "real memory",
13 that is, normal RAM--see later about other details):
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.
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".
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.
29 Now, on normal PCs the bus address is exactly the same as the physical
30 address, and things are very simple indeed. However, they are that simple
31 because the memory and the devices share the same address space, and that is
32 not generally necessarily true on other PCI/ISA setups.
34 Now, just as an example, on the PReP (PowerPC Reference Platform), the
35 CPU sees a memory map something like this (this is from 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)
41 Now, that looks simple enough. However, when you look at the same thing from
42 the viewpoint of the devices, you have the reverse, and the physical memory
43 address 0 actually shows up as address 2 GB for any IO master.
45 So when the CPU wants any bus master to write to physical memory 0, it
46 has to give the master address 0x80000000 as the memory address.
48 So, for example, depending on how the kernel is actually mapped on the
49 PPC, you can end up with a setup like this:
52 virtual address: 0xC0000000
53 bus address: 0x80000000
55 where all the addresses actually point to the same thing. It's just seen
56 through different translations..
58 Similarly, on the Alpha, the normal translation is
61 virtual address: 0xfffffc0000000000
62 bus address: 0x40000000
64 (but there are also Alphas where the physical address and the bus address
67 Anyway, the way to look up all these translations, you do
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);
76 Now, when do you need these?
78 You want the _virtual_ address when you are actually going to access that
79 pointer from the kernel. So you can have something like this:
82 * this is the hardware "mailbox" we use to communicate with
83 * the controller. The controller sees this directly.
92 unsigned char * retbuffer;
94 /* get the address from the controller */
95 retbuffer = bus_to_virt(mbox.bufstart);
96 switch (retbuffer[0]) {
100 on the other hand, you want the bus address when you have a buffer that
101 you want to give to the controller:
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);
107 notify_controller(&mbox);
109 And you generally _never_ want to use the physical address, because you can't
110 use that from the CPU (the CPU only uses translated virtual addresses), and
111 you can't use it from the bus master.
113 So why do we care about the physical address at all? We do need the physical
114 address in some cases, it's just not very often in normal code. The physical
115 address 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
117 be remapped (the memory management layer doesn't know about devices outside
118 the CPU, so it shouldn't need to know about "bus addresses" etc).
120 NOTE NOTE NOTE! The above is only one part of the whole equation. The above
121 only talks about "real memory", that is, CPU memory (RAM).
123 There is a completely different type of memory too, and that's the "shared
124 memory" on the PCI or ISA bus. That's generally not RAM (although in the case
125 of a video graphics card it can be normal DRAM that is just used for a frame
126 buffer), but can be things like a packet buffer in a network card etc.
128 This memory is called "PCI memory" or "shared memory" or "IO memory" or
129 whatever, and there is only one way to access it: the readb/writeb and
130 related functions. You should never take the address of such memory, because
131 there is really nothing you can do with such an address: it's not
132 conceptually in the same memory space as "real memory" at all, so you cannot
133 just dereference a pointer. (Sadly, on x86 it _is_ in the same memory space,
134 so on x86 it actually works to just deference a pointer, but it's not
137 For such memory, you can do things like
141 * read first 32 bits from ISA memory at 0xC0000, aka
142 * C000:0000 in DOS terms
144 unsigned int signature = readl(0xC0000);
146 - remapping and writing:
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.
153 char * baseptr = ioremap(0xFC000000, 1024*1024);
155 /* write a 'A' to the offset 10 of the area */
156 writeb('A',baseptr+10);
158 /* unmap when we unload the driver */
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);
169 OK, that just about covers the basics of accessing IO portably. Questions?
170 Comments? You may think that all the above is overly complex, but one day you
171 might find yourself with a 500 MHz Alpha in front of you, and then you'll be
172 happy that your driver works ;)
174 Note that kernel versions 2.0.x (and earlier) mistakenly called the
175 ioremap() function "vremap()". ioremap() is the proper name, but I
176 didn't think straight when I wrote it originally. People who have to
177 support both can do something like:
179 /* support old naming sillyness */
180 #if LINUX_VERSION_CODE < 0x020100
181 #define ioremap vremap
182 #define iounmap vfree
185 at the top of their source files, and then they can use the right names
186 even on 2.0.x systems.
188 And the above sounds worse than it really is. Most real drivers really
189 don't do all that complex things (or rather: the complexity is not so
190 much in the actual IO accesses as in error handling and timeouts etc).
191 It's generally not hard to fix drivers, and in many cases the code
192 actually looks better afterwards:
194 unsigned long signature = *(unsigned int *) 0xC0000;
196 unsigned long signature = readl(0xC0000);
198 I think the second version actually is more readable, no?