TEXT   61

IO-mapping

Guest on 22nd July 2022 02:08:40 AM

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

Raw Paste


Login or Register to edit or fork this paste. It's free.