bus-virt-phys-mapping.txt 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. [ NOTE: The virt_to_bus() and bus_to_virt() functions have been
  2. superseded by the functionality provided by the PCI DMA interface
  3. (see Documentation/DMA-API-HOWTO.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_pfn_range()" mm function wants the physical address of the memory to
  93. be remapped as measured in units of pages, a.k.a. the pfn (the memory
  94. management layer doesn't know about devices outside the CPU, so it
  95. shouldn't need to know about "bus addresses" etc).
  96. NOTE NOTE NOTE! The above is only one part of the whole equation. The above
  97. only talks about "real memory", that is, CPU memory (RAM).
  98. There is a completely different type of memory too, and that's the "shared
  99. memory" on the PCI or ISA bus. That's generally not RAM (although in the case
  100. of a video graphics card it can be normal DRAM that is just used for a frame
  101. buffer), but can be things like a packet buffer in a network card etc.
  102. This memory is called "PCI memory" or "shared memory" or "IO memory" or
  103. whatever, and there is only one way to access it: the readb/writeb and
  104. related functions. You should never take the address of such memory, because
  105. there is really nothing you can do with such an address: it's not
  106. conceptually in the same memory space as "real memory" at all, so you cannot
  107. just dereference a pointer. (Sadly, on x86 it _is_ in the same memory space,
  108. so on x86 it actually works to just deference a pointer, but it's not
  109. portable).
  110. For such memory, you can do things like
  111. - reading:
  112. /*
  113. * read first 32 bits from ISA memory at 0xC0000, aka
  114. * C000:0000 in DOS terms
  115. */
  116. unsigned int signature = isa_readl(0xC0000);
  117. - remapping and writing:
  118. /*
  119. * remap framebuffer PCI memory area at 0xFC000000,
  120. * size 1MB, so that we can access it: We can directly
  121. * access only the 640k-1MB area, so anything else
  122. * has to be remapped.
  123. */
  124. void __iomem *baseptr = ioremap(0xFC000000, 1024*1024);
  125. /* write a 'A' to the offset 10 of the area */
  126. writeb('A',baseptr+10);
  127. /* unmap when we unload the driver */
  128. iounmap(baseptr);
  129. - copying and clearing:
  130. /* get the 6-byte Ethernet address at ISA address E000:0040 */
  131. memcpy_fromio(kernel_buffer, 0xE0040, 6);
  132. /* write a packet to the driver */
  133. memcpy_toio(0xE1000, skb->data, skb->len);
  134. /* clear the frame buffer */
  135. memset_io(0xA0000, 0, 0x10000);
  136. OK, that just about covers the basics of accessing IO portably. Questions?
  137. Comments? You may think that all the above is overly complex, but one day you
  138. might find yourself with a 500 MHz Alpha in front of you, and then you'll be
  139. happy that your driver works ;)
  140. Note that kernel versions 2.0.x (and earlier) mistakenly called the
  141. ioremap() function "vremap()". ioremap() is the proper name, but I
  142. didn't think straight when I wrote it originally. People who have to
  143. support both can do something like:
  144. /* support old naming silliness */
  145. #if LINUX_VERSION_CODE < 0x020100
  146. #define ioremap vremap
  147. #define iounmap vfree
  148. #endif
  149. at the top of their source files, and then they can use the right names
  150. even on 2.0.x systems.
  151. And the above sounds worse than it really is. Most real drivers really
  152. don't do all that complex things (or rather: the complexity is not so
  153. much in the actual IO accesses as in error handling and timeouts etc).
  154. It's generally not hard to fix drivers, and in many cases the code
  155. actually looks better afterwards:
  156. unsigned long signature = *(unsigned int *) 0xC0000;
  157. vs
  158. unsigned long signature = readl(0xC0000);
  159. I think the second version actually is more readable, no?
  160. Linus