Back to home page

LXR

 
 

    


0001 [ NOTE: The virt_to_bus() and bus_to_virt() functions have been
0002         superseded by the functionality provided by the PCI DMA interface
0003         (see Documentation/DMA-API-HOWTO.txt).  They continue
0004         to be documented below for historical purposes, but new code
0005         must not use them. --davidm 00/12/12 ]
0006 
0007 [ This is a mail message in response to a query on IO mapping, thus the
0008   strange format for a "document" ]
0009 
0010 The AHA-1542 is a bus-master device, and your patch makes the driver give the
0011 controller the physical address of the buffers, which is correct on x86
0012 (because all bus master devices see the physical memory mappings directly). 
0013 
0014 However, on many setups, there are actually _three_ different ways of looking
0015 at memory addresses, and in this case we actually want the third, the
0016 so-called "bus address". 
0017 
0018 Essentially, the three ways of addressing memory are (this is "real memory",
0019 that is, normal RAM--see later about other details): 
0020 
0021  - CPU untranslated.  This is the "physical" address.  Physical address 
0022    0 is what the CPU sees when it drives zeroes on the memory bus.
0023 
0024  - CPU translated address. This is the "virtual" address, and is 
0025    completely internal to the CPU itself with the CPU doing the appropriate
0026    translations into "CPU untranslated". 
0027 
0028  - bus address. This is the address of memory as seen by OTHER devices, 
0029    not the CPU. Now, in theory there could be many different bus 
0030    addresses, with each device seeing memory in some device-specific way, but
0031    happily most hardware designers aren't actually actively trying to make
0032    things any more complex than necessary, so you can assume that all 
0033    external hardware sees the memory the same way. 
0034 
0035 Now, on normal PCs the bus address is exactly the same as the physical
0036 address, and things are very simple indeed. However, they are that simple
0037 because the memory and the devices share the same address space, and that is
0038 not generally necessarily true on other PCI/ISA setups. 
0039 
0040 Now, just as an example, on the PReP (PowerPC Reference Platform), the 
0041 CPU sees a memory map something like this (this is from memory):
0042 
0043         0-2 GB          "real memory"
0044         2 GB-3 GB       "system IO" (inb/out and similar accesses on x86)
0045         3 GB-4 GB       "IO memory" (shared memory over the IO bus)
0046 
0047 Now, that looks simple enough. However, when you look at the same thing from
0048 the viewpoint of the devices, you have the reverse, and the physical memory
0049 address 0 actually shows up as address 2 GB for any IO master.
0050 
0051 So when the CPU wants any bus master to write to physical memory 0, it 
0052 has to give the master address 0x80000000 as the memory address.
0053 
0054 So, for example, depending on how the kernel is actually mapped on the 
0055 PPC, you can end up with a setup like this:
0056 
0057  physical address:      0
0058  virtual address:       0xC0000000
0059  bus address:           0x80000000
0060 
0061 where all the addresses actually point to the same thing.  It's just seen 
0062 through different translations..
0063 
0064 Similarly, on the Alpha, the normal translation is
0065 
0066  physical address:      0
0067  virtual address:       0xfffffc0000000000
0068  bus address:           0x40000000
0069 
0070 (but there are also Alphas where the physical address and the bus address
0071 are the same). 
0072 
0073 Anyway, the way to look up all these translations, you do
0074 
0075         #include <asm/io.h>
0076 
0077         phys_addr = virt_to_phys(virt_addr);
0078         virt_addr = phys_to_virt(phys_addr);
0079          bus_addr = virt_to_bus(virt_addr);
0080         virt_addr = bus_to_virt(bus_addr);
0081 
0082 Now, when do you need these?
0083 
0084 You want the _virtual_ address when you are actually going to access that 
0085 pointer from the kernel. So you can have something like this:
0086 
0087         /*
0088          * this is the hardware "mailbox" we use to communicate with
0089          * the controller. The controller sees this directly.
0090          */
0091         struct mailbox {
0092                 __u32 status;
0093                 __u32 bufstart;
0094                 __u32 buflen;
0095                 ..
0096         } mbox;
0097 
0098                 unsigned char * retbuffer;
0099 
0100                 /* get the address from the controller */
0101                 retbuffer = bus_to_virt(mbox.bufstart);
0102                 switch (retbuffer[0]) {
0103                         case STATUS_OK:
0104                                 ...
0105 
0106 on the other hand, you want the bus address when you have a buffer that 
0107 you want to give to the controller:
0108 
0109         /* ask the controller to read the sense status into "sense_buffer" */
0110         mbox.bufstart = virt_to_bus(&sense_buffer);
0111         mbox.buflen = sizeof(sense_buffer);
0112         mbox.status = 0;
0113         notify_controller(&mbox);
0114 
0115 And you generally _never_ want to use the physical address, because you can't
0116 use that from the CPU (the CPU only uses translated virtual addresses), and
0117 you can't use it from the bus master. 
0118 
0119 So why do we care about the physical address at all? We do need the physical
0120 address in some cases, it's just not very often in normal code.  The physical
0121 address is needed if you use memory mappings, for example, because the
0122 "remap_pfn_range()" mm function wants the physical address of the memory to
0123 be remapped as measured in units of pages, a.k.a. the pfn (the memory
0124 management layer doesn't know about devices outside the CPU, so it
0125 shouldn't need to know about "bus addresses" etc).
0126 
0127 NOTE NOTE NOTE! The above is only one part of the whole equation. The above
0128 only talks about "real memory", that is, CPU memory (RAM). 
0129 
0130 There is a completely different type of memory too, and that's the "shared
0131 memory" on the PCI or ISA bus. That's generally not RAM (although in the case
0132 of a video graphics card it can be normal DRAM that is just used for a frame
0133 buffer), but can be things like a packet buffer in a network card etc. 
0134 
0135 This memory is called "PCI memory" or "shared memory" or "IO memory" or
0136 whatever, and there is only one way to access it: the readb/writeb and
0137 related functions. You should never take the address of such memory, because
0138 there is really nothing you can do with such an address: it's not
0139 conceptually in the same memory space as "real memory" at all, so you cannot
0140 just dereference a pointer. (Sadly, on x86 it _is_ in the same memory space,
0141 so on x86 it actually works to just deference a pointer, but it's not
0142 portable). 
0143 
0144 For such memory, you can do things like
0145 
0146  - reading:
0147         /*
0148          * read first 32 bits from ISA memory at 0xC0000, aka
0149          * C000:0000 in DOS terms
0150          */
0151         unsigned int signature = isa_readl(0xC0000);
0152 
0153  - remapping and writing:
0154         /*
0155          * remap framebuffer PCI memory area at 0xFC000000,
0156          * size 1MB, so that we can access it: We can directly
0157          * access only the 640k-1MB area, so anything else
0158          * has to be remapped.
0159          */
0160         void __iomem *baseptr = ioremap(0xFC000000, 1024*1024);
0161 
0162         /* write a 'A' to the offset 10 of the area */
0163         writeb('A',baseptr+10);
0164 
0165         /* unmap when we unload the driver */
0166         iounmap(baseptr);
0167 
0168  - copying and clearing:
0169         /* get the 6-byte Ethernet address at ISA address E000:0040 */
0170         memcpy_fromio(kernel_buffer, 0xE0040, 6);
0171         /* write a packet to the driver */
0172         memcpy_toio(0xE1000, skb->data, skb->len);
0173         /* clear the frame buffer */
0174         memset_io(0xA0000, 0, 0x10000);
0175 
0176 OK, that just about covers the basics of accessing IO portably.  Questions?
0177 Comments? You may think that all the above is overly complex, but one day you
0178 might find yourself with a 500 MHz Alpha in front of you, and then you'll be
0179 happy that your driver works ;)
0180 
0181 Note that kernel versions 2.0.x (and earlier) mistakenly called the
0182 ioremap() function "vremap()".  ioremap() is the proper name, but I
0183 didn't think straight when I wrote it originally.  People who have to
0184 support both can do something like:
0185  
0186         /* support old naming silliness */
0187         #if LINUX_VERSION_CODE < 0x020100                                     
0188         #define ioremap vremap
0189         #define iounmap vfree                                                     
0190         #endif
0191  
0192 at the top of their source files, and then they can use the right names
0193 even on 2.0.x systems. 
0194 
0195 And the above sounds worse than it really is.  Most real drivers really
0196 don't do all that complex things (or rather: the complexity is not so
0197 much in the actual IO accesses as in error handling and timeouts etc). 
0198 It's generally not hard to fix drivers, and in many cases the code
0199 actually looks better afterwards:
0200 
0201         unsigned long signature = *(unsigned int *) 0xC0000;
0202                 vs
0203         unsigned long signature = readl(0xC0000);
0204 
0205 I think the second version actually is more readable, no?
0206 
0207                 Linus
0208