deviceiobook.tmpl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
  4. <book id="DoingIO">
  5. <bookinfo>
  6. <title>Bus-Independent Device Accesses</title>
  7. <authorgroup>
  8. <author>
  9. <firstname>Matthew</firstname>
  10. <surname>Wilcox</surname>
  11. <affiliation>
  12. <address>
  13. <email>matthew@wil.cx</email>
  14. </address>
  15. </affiliation>
  16. </author>
  17. </authorgroup>
  18. <authorgroup>
  19. <author>
  20. <firstname>Alan</firstname>
  21. <surname>Cox</surname>
  22. <affiliation>
  23. <address>
  24. <email>alan@lxorguk.ukuu.org.uk</email>
  25. </address>
  26. </affiliation>
  27. </author>
  28. </authorgroup>
  29. <copyright>
  30. <year>2001</year>
  31. <holder>Matthew Wilcox</holder>
  32. </copyright>
  33. <legalnotice>
  34. <para>
  35. This documentation is free software; you can redistribute
  36. it and/or modify it under the terms of the GNU General Public
  37. License as published by the Free Software Foundation; either
  38. version 2 of the License, or (at your option) any later
  39. version.
  40. </para>
  41. <para>
  42. This program is distributed in the hope that it will be
  43. useful, but WITHOUT ANY WARRANTY; without even the implied
  44. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  45. See the GNU General Public License for more details.
  46. </para>
  47. <para>
  48. You should have received a copy of the GNU General Public
  49. License along with this program; if not, write to the Free
  50. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  51. MA 02111-1307 USA
  52. </para>
  53. <para>
  54. For more details see the file COPYING in the source
  55. distribution of Linux.
  56. </para>
  57. </legalnotice>
  58. </bookinfo>
  59. <toc></toc>
  60. <chapter id="intro">
  61. <title>Introduction</title>
  62. <para>
  63. Linux provides an API which abstracts performing IO across all busses
  64. and devices, allowing device drivers to be written independently of
  65. bus type.
  66. </para>
  67. </chapter>
  68. <chapter id="bugs">
  69. <title>Known Bugs And Assumptions</title>
  70. <para>
  71. None.
  72. </para>
  73. </chapter>
  74. <chapter id="mmio">
  75. <title>Memory Mapped IO</title>
  76. <sect1 id="getting_access_to_the_device">
  77. <title>Getting Access to the Device</title>
  78. <para>
  79. The most widely supported form of IO is memory mapped IO.
  80. That is, a part of the CPU's address space is interpreted
  81. not as accesses to memory, but as accesses to a device. Some
  82. architectures define devices to be at a fixed address, but most
  83. have some method of discovering devices. The PCI bus walk is a
  84. good example of such a scheme. This document does not cover how
  85. to receive such an address, but assumes you are starting with one.
  86. Physical addresses are of type unsigned long.
  87. </para>
  88. <para>
  89. This address should not be used directly. Instead, to get an
  90. address suitable for passing to the accessor functions described
  91. below, you should call <function>ioremap</function>.
  92. An address suitable for accessing the device will be returned to you.
  93. </para>
  94. <para>
  95. After you've finished using the device (say, in your module's
  96. exit routine), call <function>iounmap</function> in order to return
  97. the address space to the kernel. Most architectures allocate new
  98. address space each time you call <function>ioremap</function>, and
  99. they can run out unless you call <function>iounmap</function>.
  100. </para>
  101. </sect1>
  102. <sect1 id="accessing_the_device">
  103. <title>Accessing the device</title>
  104. <para>
  105. The part of the interface most used by drivers is reading and
  106. writing memory-mapped registers on the device. Linux provides
  107. interfaces to read and write 8-bit, 16-bit, 32-bit and 64-bit
  108. quantities. Due to a historical accident, these are named byte,
  109. word, long and quad accesses. Both read and write accesses are
  110. supported; there is no prefetch support at this time.
  111. </para>
  112. <para>
  113. The functions are named <function>readb</function>,
  114. <function>readw</function>, <function>readl</function>,
  115. <function>readq</function>, <function>readb_relaxed</function>,
  116. <function>readw_relaxed</function>, <function>readl_relaxed</function>,
  117. <function>readq_relaxed</function>, <function>writeb</function>,
  118. <function>writew</function>, <function>writel</function> and
  119. <function>writeq</function>.
  120. </para>
  121. <para>
  122. Some devices (such as framebuffers) would like to use larger
  123. transfers than 8 bytes at a time. For these devices, the
  124. <function>memcpy_toio</function>, <function>memcpy_fromio</function>
  125. and <function>memset_io</function> functions are provided.
  126. Do not use memset or memcpy on IO addresses; they
  127. are not guaranteed to copy data in order.
  128. </para>
  129. <para>
  130. The read and write functions are defined to be ordered. That is the
  131. compiler is not permitted to reorder the I/O sequence. When the
  132. ordering can be compiler optimised, you can use <function>
  133. __readb</function> and friends to indicate the relaxed ordering. Use
  134. this with care.
  135. </para>
  136. <para>
  137. While the basic functions are defined to be synchronous with respect
  138. to each other and ordered with respect to each other the busses the
  139. devices sit on may themselves have asynchronicity. In particular many
  140. authors are burned by the fact that PCI bus writes are posted
  141. asynchronously. A driver author must issue a read from the same
  142. device to ensure that writes have occurred in the specific cases the
  143. author cares. This kind of property cannot be hidden from driver
  144. writers in the API. In some cases, the read used to flush the device
  145. may be expected to fail (if the card is resetting, for example). In
  146. that case, the read should be done from config space, which is
  147. guaranteed to soft-fail if the card doesn't respond.
  148. </para>
  149. <para>
  150. The following is an example of flushing a write to a device when
  151. the driver would like to ensure the write's effects are visible prior
  152. to continuing execution.
  153. </para>
  154. <programlisting>
  155. static inline void
  156. qla1280_disable_intrs(struct scsi_qla_host *ha)
  157. {
  158. struct device_reg *reg;
  159. reg = ha->iobase;
  160. /* disable risc and host interrupts */
  161. WRT_REG_WORD(&amp;reg->ictrl, 0);
  162. /*
  163. * The following read will ensure that the above write
  164. * has been received by the device before we return from this
  165. * function.
  166. */
  167. RD_REG_WORD(&amp;reg->ictrl);
  168. ha->flags.ints_enabled = 0;
  169. }
  170. </programlisting>
  171. <para>
  172. In addition to write posting, on some large multiprocessing systems
  173. (e.g. SGI Challenge, Origin and Altix machines) posted writes won't
  174. be strongly ordered coming from different CPUs. Thus it's important
  175. to properly protect parts of your driver that do memory-mapped writes
  176. with locks and use the <function>mmiowb</function> to make sure they
  177. arrive in the order intended. Issuing a regular <function>readX
  178. </function> will also ensure write ordering, but should only be used
  179. when the driver has to be sure that the write has actually arrived
  180. at the device (not that it's simply ordered with respect to other
  181. writes), since a full <function>readX</function> is a relatively
  182. expensive operation.
  183. </para>
  184. <para>
  185. Generally, one should use <function>mmiowb</function> prior to
  186. releasing a spinlock that protects regions using <function>writeb
  187. </function> or similar functions that aren't surrounded by <function>
  188. readb</function> calls, which will ensure ordering and flushing. The
  189. following pseudocode illustrates what might occur if write ordering
  190. isn't guaranteed via <function>mmiowb</function> or one of the
  191. <function>readX</function> functions.
  192. </para>
  193. <programlisting>
  194. CPU A: spin_lock_irqsave(&amp;dev_lock, flags)
  195. CPU A: ...
  196. CPU A: writel(newval, ring_ptr);
  197. CPU A: spin_unlock_irqrestore(&amp;dev_lock, flags)
  198. ...
  199. CPU B: spin_lock_irqsave(&amp;dev_lock, flags)
  200. CPU B: writel(newval2, ring_ptr);
  201. CPU B: ...
  202. CPU B: spin_unlock_irqrestore(&amp;dev_lock, flags)
  203. </programlisting>
  204. <para>
  205. In the case above, newval2 could be written to ring_ptr before
  206. newval. Fixing it is easy though:
  207. </para>
  208. <programlisting>
  209. CPU A: spin_lock_irqsave(&amp;dev_lock, flags)
  210. CPU A: ...
  211. CPU A: writel(newval, ring_ptr);
  212. CPU A: mmiowb(); /* ensure no other writes beat us to the device */
  213. CPU A: spin_unlock_irqrestore(&amp;dev_lock, flags)
  214. ...
  215. CPU B: spin_lock_irqsave(&amp;dev_lock, flags)
  216. CPU B: writel(newval2, ring_ptr);
  217. CPU B: ...
  218. CPU B: mmiowb();
  219. CPU B: spin_unlock_irqrestore(&amp;dev_lock, flags)
  220. </programlisting>
  221. <para>
  222. See tg3.c for a real world example of how to use <function>mmiowb
  223. </function>
  224. </para>
  225. <para>
  226. PCI ordering rules also guarantee that PIO read responses arrive
  227. after any outstanding DMA writes from that bus, since for some devices
  228. the result of a <function>readb</function> call may signal to the
  229. driver that a DMA transaction is complete. In many cases, however,
  230. the driver may want to indicate that the next
  231. <function>readb</function> call has no relation to any previous DMA
  232. writes performed by the device. The driver can use
  233. <function>readb_relaxed</function> for these cases, although only
  234. some platforms will honor the relaxed semantics. Using the relaxed
  235. read functions will provide significant performance benefits on
  236. platforms that support it. The qla2xxx driver provides examples
  237. of how to use <function>readX_relaxed</function>. In many cases,
  238. a majority of the driver's <function>readX</function> calls can
  239. safely be converted to <function>readX_relaxed</function> calls, since
  240. only a few will indicate or depend on DMA completion.
  241. </para>
  242. </sect1>
  243. </chapter>
  244. <chapter id="port_space_accesses">
  245. <title>Port Space Accesses</title>
  246. <sect1 id="port_space_explained">
  247. <title>Port Space Explained</title>
  248. <para>
  249. Another form of IO commonly supported is Port Space. This is a
  250. range of addresses separate to the normal memory address space.
  251. Access to these addresses is generally not as fast as accesses
  252. to the memory mapped addresses, and it also has a potentially
  253. smaller address space.
  254. </para>
  255. <para>
  256. Unlike memory mapped IO, no preparation is required
  257. to access port space.
  258. </para>
  259. </sect1>
  260. <sect1 id="accessing_port_space">
  261. <title>Accessing Port Space</title>
  262. <para>
  263. Accesses to this space are provided through a set of functions
  264. which allow 8-bit, 16-bit and 32-bit accesses; also
  265. known as byte, word and long. These functions are
  266. <function>inb</function>, <function>inw</function>,
  267. <function>inl</function>, <function>outb</function>,
  268. <function>outw</function> and <function>outl</function>.
  269. </para>
  270. <para>
  271. Some variants are provided for these functions. Some devices
  272. require that accesses to their ports are slowed down. This
  273. functionality is provided by appending a <function>_p</function>
  274. to the end of the function. There are also equivalents to memcpy.
  275. The <function>ins</function> and <function>outs</function>
  276. functions copy bytes, words or longs to the given port.
  277. </para>
  278. </sect1>
  279. </chapter>
  280. <chapter id="pubfunctions">
  281. <title>Public Functions Provided</title>
  282. !Iarch/x86/include/asm/io.h
  283. !Elib/pci_iomap.c
  284. </chapter>
  285. </book>