OpenCores
URL https://opencores.org/ocsvn/test_project/test_project/trunk

Subversion Repositories test_project

[/] [test_project/] [trunk/] [linux_sd_driver/] [Documentation/] [DocBook/] [deviceiobook.tmpl] - Blame information for rev 62

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.