IO-mapping.txt
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:8k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

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