bus-virt-phys-mapping.txt 8.0 KB

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