device-io.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. .. Copyright 2001 Matthew Wilcox
  2. ..
  3. .. This documentation is free software; you can redistribute
  4. .. it and/or modify it under the terms of the GNU General Public
  5. .. License as published by the Free Software Foundation; either
  6. .. version 2 of the License, or (at your option) any later
  7. .. version.
  8. ===============================
  9. Bus-Independent Device Accesses
  10. ===============================
  11. :Author: Matthew Wilcox
  12. :Author: Alan Cox
  13. Introduction
  14. ============
  15. Linux provides an API which abstracts performing IO across all busses
  16. and devices, allowing device drivers to be written independently of bus
  17. type.
  18. Memory Mapped IO
  19. ================
  20. Getting Access to the Device
  21. ----------------------------
  22. The most widely supported form of IO is memory mapped IO. That is, a
  23. part of the CPU's address space is interpreted not as accesses to
  24. memory, but as accesses to a device. Some architectures define devices
  25. to be at a fixed address, but most have some method of discovering
  26. devices. The PCI bus walk is a good example of such a scheme. This
  27. document does not cover how to receive such an address, but assumes you
  28. are starting with one. Physical addresses are of type unsigned long.
  29. This address should not be used directly. Instead, to get an address
  30. suitable for passing to the accessor functions described below, you
  31. should call :c:func:`ioremap()`. An address suitable for accessing
  32. the device will be returned to you.
  33. After you've finished using the device (say, in your module's exit
  34. routine), call :c:func:`iounmap()` in order to return the address
  35. space to the kernel. Most architectures allocate new address space each
  36. time you call :c:func:`ioremap()`, and they can run out unless you
  37. call :c:func:`iounmap()`.
  38. Accessing the device
  39. --------------------
  40. The part of the interface most used by drivers is reading and writing
  41. memory-mapped registers on the device. Linux provides interfaces to read
  42. and write 8-bit, 16-bit, 32-bit and 64-bit quantities. Due to a
  43. historical accident, these are named byte, word, long and quad accesses.
  44. Both read and write accesses are supported; there is no prefetch support
  45. at this time.
  46. The functions are named readb(), readw(), readl(), readq(),
  47. readb_relaxed(), readw_relaxed(), readl_relaxed(), readq_relaxed(),
  48. writeb(), writew(), writel() and writeq().
  49. Some devices (such as framebuffers) would like to use larger transfers than
  50. 8 bytes at a time. For these devices, the :c:func:`memcpy_toio()`,
  51. :c:func:`memcpy_fromio()` and :c:func:`memset_io()` functions are
  52. provided. Do not use memset or memcpy on IO addresses; they are not
  53. guaranteed to copy data in order.
  54. The read and write functions are defined to be ordered. That is the
  55. compiler is not permitted to reorder the I/O sequence. When the ordering
  56. can be compiler optimised, you can use __readb() and friends to
  57. indicate the relaxed ordering. Use this with care.
  58. While the basic functions are defined to be synchronous with respect to
  59. each other and ordered with respect to each other the busses the devices
  60. sit on may themselves have asynchronicity. In particular many authors
  61. are burned by the fact that PCI bus writes are posted asynchronously. A
  62. driver author must issue a read from the same device to ensure that
  63. writes have occurred in the specific cases the author cares. This kind
  64. of property cannot be hidden from driver writers in the API. In some
  65. cases, the read used to flush the device may be expected to fail (if the
  66. card is resetting, for example). In that case, the read should be done
  67. from config space, which is guaranteed to soft-fail if the card doesn't
  68. respond.
  69. The following is an example of flushing a write to a device when the
  70. driver would like to ensure the write's effects are visible prior to
  71. continuing execution::
  72. static inline void
  73. qla1280_disable_intrs(struct scsi_qla_host *ha)
  74. {
  75. struct device_reg *reg;
  76. reg = ha->iobase;
  77. /* disable risc and host interrupts */
  78. WRT_REG_WORD(&reg->ictrl, 0);
  79. /*
  80. * The following read will ensure that the above write
  81. * has been received by the device before we return from this
  82. * function.
  83. */
  84. RD_REG_WORD(&reg->ictrl);
  85. ha->flags.ints_enabled = 0;
  86. }
  87. In addition to write posting, on some large multiprocessing systems
  88. (e.g. SGI Challenge, Origin and Altix machines) posted writes won't be
  89. strongly ordered coming from different CPUs. Thus it's important to
  90. properly protect parts of your driver that do memory-mapped writes with
  91. locks and use the :c:func:`mmiowb()` to make sure they arrive in the
  92. order intended. Issuing a regular readX() will also ensure write ordering,
  93. but should only be used when the
  94. driver has to be sure that the write has actually arrived at the device
  95. (not that it's simply ordered with respect to other writes), since a
  96. full readX() is a relatively expensive operation.
  97. Generally, one should use :c:func:`mmiowb()` prior to releasing a spinlock
  98. that protects regions using :c:func:`writeb()` or similar functions that
  99. aren't surrounded by readb() calls, which will ensure ordering
  100. and flushing. The following pseudocode illustrates what might occur if
  101. write ordering isn't guaranteed via :c:func:`mmiowb()` or one of the
  102. readX() functions::
  103. CPU A: spin_lock_irqsave(&dev_lock, flags)
  104. CPU A: ...
  105. CPU A: writel(newval, ring_ptr);
  106. CPU A: spin_unlock_irqrestore(&dev_lock, flags)
  107. ...
  108. CPU B: spin_lock_irqsave(&dev_lock, flags)
  109. CPU B: writel(newval2, ring_ptr);
  110. CPU B: ...
  111. CPU B: spin_unlock_irqrestore(&dev_lock, flags)
  112. In the case above, newval2 could be written to ring_ptr before newval.
  113. Fixing it is easy though::
  114. CPU A: spin_lock_irqsave(&dev_lock, flags)
  115. CPU A: ...
  116. CPU A: writel(newval, ring_ptr);
  117. CPU A: mmiowb(); /* ensure no other writes beat us to the device */
  118. CPU A: spin_unlock_irqrestore(&dev_lock, flags)
  119. ...
  120. CPU B: spin_lock_irqsave(&dev_lock, flags)
  121. CPU B: writel(newval2, ring_ptr);
  122. CPU B: ...
  123. CPU B: mmiowb();
  124. CPU B: spin_unlock_irqrestore(&dev_lock, flags)
  125. See tg3.c for a real world example of how to use :c:func:`mmiowb()`
  126. PCI ordering rules also guarantee that PIO read responses arrive after any
  127. outstanding DMA writes from that bus, since for some devices the result of
  128. a readb() call may signal to the driver that a DMA transaction is
  129. complete. In many cases, however, the driver may want to indicate that the
  130. next readb() call has no relation to any previous DMA writes
  131. performed by the device. The driver can use readb_relaxed() for
  132. these cases, although only some platforms will honor the relaxed
  133. semantics. Using the relaxed read functions will provide significant
  134. performance benefits on platforms that support it. The qla2xxx driver
  135. provides examples of how to use readX_relaxed(). In many cases, a majority
  136. of the driver's readX() calls can safely be converted to readX_relaxed()
  137. calls, since only a few will indicate or depend on DMA completion.
  138. Port Space Accesses
  139. ===================
  140. Port Space Explained
  141. --------------------
  142. Another form of IO commonly supported is Port Space. This is a range of
  143. addresses separate to the normal memory address space. Access to these
  144. addresses is generally not as fast as accesses to the memory mapped
  145. addresses, and it also has a potentially smaller address space.
  146. Unlike memory mapped IO, no preparation is required to access port
  147. space.
  148. Accessing Port Space
  149. --------------------
  150. Accesses to this space are provided through a set of functions which
  151. allow 8-bit, 16-bit and 32-bit accesses; also known as byte, word and
  152. long. These functions are :c:func:`inb()`, :c:func:`inw()`,
  153. :c:func:`inl()`, :c:func:`outb()`, :c:func:`outw()` and
  154. :c:func:`outl()`.
  155. Some variants are provided for these functions. Some devices require
  156. that accesses to their ports are slowed down. This functionality is
  157. provided by appending a ``_p`` to the end of the function.
  158. There are also equivalents to memcpy. The :c:func:`ins()` and
  159. :c:func:`outs()` functions copy bytes, words or longs to the given
  160. port.
  161. Public Functions Provided
  162. =========================
  163. .. kernel-doc:: arch/x86/include/asm/io.h
  164. :internal:
  165. .. kernel-doc:: lib/pci_iomap.c
  166. :export: