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