~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/Documentation/DMA-mapping.txt

Version: ~ [ 2.2.5 ] ~ [ 2.4.1 ] ~ [ 2.4.9 ] ~ [ 2.6.17.10 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1                         Dynamic DMA mapping
  2                         ===================
  3 
  4                  David S. Miller <davem@redhat.com>
  5                  Richard Henderson <rth@cygnus.com>
  6                   Jakub Jelinek <jakub@redhat.com>
  7 
  8 Most of the 64bit platforms have special hardware that translates bus
  9 addresses (DMA addresses) to physical addresses similarly to how page
 10 tables and/or TLB translate virtual addresses to physical addresses.
 11 This is needed so that e.g. PCI devices can access with a Single Address
 12 Cycle (32bit DMA address) any page in the 64bit physical address space.
 13 Previously in Linux those 64bit platforms had to set artificial limits on
 14 the maximum RAM size in the system, so that the virt_to_bus() static scheme
 15 works (the DMA address translation tables were simply filled on bootup
 16 to map each bus address to the physical page __pa(bus_to_virt())).
 17 
 18 So that Linux can use the dynamic DMA mapping, it needs some help from the
 19 drivers, namely it has to take into account that DMA addresses should be
 20 mapped only for the time they are actually used and unmapped after the DMA
 21 transfer.
 22 
 23 The following API will work of course even on platforms where no such
 24 hardware exists, see e.g. include/asm-i386/pci.h for how it is implemented on
 25 top of the virt_to_bus interface.
 26 
 27 First of all, you should make sure
 28 
 29 #include <linux/pci.h>
 30 
 31 is in your driver. This file will obtain for you the definition of
 32 the dma_addr_t type which should be used everywhere you hold a DMA
 33 (bus) address returned from the DMA mapping functions.
 34 
 35                         DMA addressing limitations
 36 
 37 Does your device have any DMA addressing limitations?  For example, is
 38 your device only capable of driving the low order 24-bits of address
 39 on the PCI bus for DMA transfers?  If your device can handle any PCI
 40 dma address fully, then please skip to the next section, the rest of
 41 this section does not concern your device.
 42 
 43 For correct operation, you must interrogate the PCI layer in your
 44 device probe routine to see if the PCI controller on the machine can
 45 properly support the DMA addressing limitation your device has.  This
 46 query is performed via a call to pci_dma_supported():
 47 
 48         int pci_dma_supported(struct pci_dev *pdev, dma_addr_t device_mask)
 49 
 50 Here, pdev is a pointer to the PCI device struct of your device, and
 51 device_mask is a bit mask describing which bits of a PCI address your
 52 device supports.  It returns non-zero if your card can perform DMA
 53 properly on the machine.  If it returns zero, your device can not
 54 perform DMA properly on this platform, and attempting to do so will
 55 result in undefined behavior.
 56 
 57 In the failure case, you have two options:
 58 
 59 1) Use some non-DMA mode for data transfer, if possible.
 60 2) Ignore this device and do not initialize it.
 61 
 62 It is recommended that your driver print a kernel KERN_WARN message
 63 when you do one of these two things.  In this manner, if a user of
 64 your driver reports that performance is bad or that the device is not
 65 even detected, you can ask him for the kernel messages to find out
 66 exactly why.
 67 
 68 So if, for example, you device can only drive the low 24-bits of
 69 address during PCI bus mastering you might do something like:
 70 
 71         if (! pci_dma_supported(pdev, 0x00ffffff))
 72                 goto ignore_this_device;
 73 
 74 There is a case which we are aware of at this time, which is worth
 75 mentioning in this documentation.  If your device supports multiple
 76 functions (for example a sound card provides playback and record
 77 functions) and the various different functions have _different_
 78 DMA addressing limitations, you may wish to probe each mask and
 79 only provide the functionality which the machine can handle.
 80 Here is pseudo-code showing how this might be done:
 81 
 82         #define PLAYBACK_ADDRESS_BITS   0xffffffff
 83         #define RECORD_ADDRESS_BITS     0x00ffffff
 84 
 85         struct my_sound_card *card;
 86         struct pci_dev *pdev;
 87 
 88         ...
 89         if (pci_dma_supported(pdev, PLAYBACK_ADDRESS_BITS)) {
 90                 card->playback_enabled = 1;
 91         } else {
 92                 card->playback_enabled = 0;
 93                 printk(KERN_WARN "%s: Playback disabled due to DMA limitations.\n",
 94                        card->name);
 95         }
 96         if (pci_dma_supported(pdev, RECORD_ADDRESS_BITS)) {
 97                 card->record_enabled = 1;
 98         } else {
 99                 card->record_enabled = 0;
100                 printk(KERN_WARN "%s: Record disabled due to DMA limitations.\n",
101                        card->name);
102         }
103 
104 A sound card was used as an example here because this genre of PCI
105 devices seems to be littered with ISA chips given a PCI front end,
106 and thus retaining the 16MB DMA addressing limitations of ISA.
107 
108                         Types of DMA mappings
109 
110 There are two types of DMA mappings:
111 
112 - Consistent DMA mappings which are usually mapped at driver
113   initialization, unmapped at the end and for which the hardware should
114   guarantee that the device and the cpu can access the data
115   in parallel and will see updates made by each other without any
116   explicit software flushing.
117 
118   Think of "consistent" as "synchronous" or "coherent".
119 
120   Good examples of what to use consistent mappings for are:
121 
122         - Network card DMA ring descriptors.
123         - SCSI adapter mailbox command data structures.
124         - Device firmware microcode executed out of
125           main memory.
126 
127   The invariant these examples all require is that any cpu store
128   to memory is immediately visible to the device, and vice
129   versa.  Consistent mappings guarantee this.
130 
131 - Streaming DMA mappings which are usually mapped for one DMA transfer,
132   unmapped right after it (unless you use pci_dma_sync below) and for which
133   hardware can optimize for sequential accesses.
134 
135   This of "streaming" as "asynchronous" or "outside the coherency
136   domain".
137 
138   Good examples of what to use streaming mappings for are:
139 
140         - Networking buffers transmitted/received by a device.
141         - Filesystem buffers written/read by a SCSI device.
142 
143   The interfaces for using this type of mapping were designed in
144   such a way that an implementation can make whatever performance
145   optimizations the hardware allows.  To this end, when using
146   such mappings you must be explicit about what you want to happen.
147 
148                  Using Consistent DMA mappings.
149 
150 To allocate and map a consistent DMA region, you should do:
151 
152         dma_addr_t dma_handle;
153 
154         cpu_addr = pci_alloc_consistent(dev, size, &dma_handle);
155 
156 where dev is a struct pci_dev *. You should pass NULL for PCI like buses
157 where devices don't have struct pci_dev (like ISA, EISA).
158 
159 This argument is needed because the DMA translations may be bus
160 specific (and often is private to the bus which the device is attached
161 to).
162 
163 Size is the length of the region you want to allocate.
164 
165 This routine will allocate RAM for that region, so it acts similarly to
166 __get_free_pages (but takes size instead of a page order).
167 
168 It returns two values: the virtual address which you can use to access
169 it from the CPU and dma_handle which you pass to the card.
170 
171 The cpu return address and the DMA bus master address are both
172 guaranteed to be aligned to the smallest PAGE_SIZE order which
173 is greater than or equal to the requested size.  This invariant
174 exists (for example) to guarantee that if you allocate a chunk
175 which is smaller than or equal to 64 kilobytes, the extent of the
176 buffer you receive will not cross a 64K boundary.
177 
178 To unmap and free such a DMA region, you call:
179 
180         pci_free_consistent(dev, size, cpu_addr, dma_handle);
181 
182 where dev, size are the same as in the above call and cpu_addr and
183 dma_handle are the values pci_alloc_consistent returned to you.
184 
185                         DMA Direction
186 
187 The interfaces described in subsequent portions of this document
188 take a DMA direction argument, which is an integer and takes on
189 one of the following values:
190 
191  PCI_DMA_BIDIRECTIONAL
192  PCI_DMA_TODEVICE
193  PCI_DMA_FROMDEVICE
194  PCI_DMA_NONE
195 
196 One should provide the exact DMA direction if you know it.
197 
198 PCI_DMA_TODEVICE means "from main memory to the PCI device"
199 PCI_DMA_FROMDEVICE means "from the PCI device to main memory"
200 
201 You are _strongly_ encouraged to specify this as precisely
202 as you possibly can.
203 
204 If you absolutely cannot know the direction of the DMA transfer,
205 specify PCI_DMA_BIDIRECTIONAL.  It means that the DMA can go in
206 either direction.  The platform guarantees that you may legally
207 specify this, and that it will work, but this may be at the
208 cost of performance for example.
209 
210 The value PCI_DMA_NONE is to be used for debugging.  One can
211 hold this in a data structure before you come to know the
212 precise direction, and this will help catch cases where your
213 direction tracking logic has failed to set things up properly.
214 
215 Another advantage of specifying this value precisely (outside
216 of potential platform-specific optimizations of such) is for
217 debugging.  Some platforms actually have a write permission
218 boolean which DMA mappings can be marked with, much like page
219 protections in a user program can have.  Such platforms can
220 and do report errors in the kernel logs when the PCI controller
221 hardware detects violation of the permission setting.
222 
223 Only streaming mappings specify a direction, consistent mappings
224 implicitly have a direction attribute setting of
225 PCI_DMA_BIDIRECTIONAL.
226 
227 The SCSI subsystem provides mechanisms for you to easily obtain
228 the direction to use, in the SCSI command:
229 
230         scsi_to_pci_dma_dir(SCSI_DIRECTION)
231 
232 Where SCSI_DIRECTION is obtained from the 'sc_data_direction'
233 member of the SCSI command your driver is working on.  The
234 mentioned interface above returns a value suitable for passing
235 into the streaming DMA mapping interfaces below.
236 
237 For Networking drivers, it's a rather simple affair.  For transmit
238 packets, map/unmap them with the PCI_DMA_TODEVICE direction
239 specifier.  For receive packets, just the opposite, map/unmap them
240 with the PCI_DMA_FROMDEVICE direction specifier.
241 
242                   Using Streaming DMA mappings
243 
244 The streaming DMA mapping routines can be called from interrupt context.
245 There are two versions of each map/unmap, one which map/unmap a single
246 memory region, one which map/unmap a scatterlist.
247 
248 To map a single region, you do:
249 
250         dma_addr_t dma_handle;
251 
252         dma_handle = pci_map_single(dev, addr, size, direction);
253 
254 and to unmap it:
255 
256         pci_unmap_single(dev, dma_handle, size, direction);
257 
258 You should call pci_unmap_single when the DMA activity is finished, e.g.
259 from interrupt which told you the DMA transfer is done.
260 
261 Similarly with scatterlists, you map a region gathered from several regions by:
262 
263         int i, count = pci_map_sg(dev, sglist, nents, direction);
264         struct scatterlist *sg;
265 
266         for (i = 0, sg = sglist; i < count; i++, sg++) {
267                 hw_address[i] = sg_dma_address(sg);
268                 hw_len[i] = sg_dma_len(sg);
269         }
270 
271 where nents is the number of entries in the sglist.
272 
273 The implementation is free to merge several consecutive sglist entries
274 into one (e.g. if DMA mapping is done with PAGE_SIZE granularity, any
275 consecutive sglist entries can be merged into one provided the first one
276 ends and the second one starts on a page boundary - in fact this is a huge
277 advantage for cards which either cannot do scatter-gather or have very
278 limited number of scatter-gather entries) and returns the actual number
279 of sg entries it mapped them to.
280 
281 Then you should loop count times (note: this can be less than nents times)
282 and use sg_dma_address() and sg_dma_length() macros where you previously
283 accessed sg->address and sg->length as shown above.
284 
285 To unmap a scatterlist, just call:
286 
287         pci_unmap_sg(dev, sglist, nents, direction);
288 
289 Again, make sure DMA activity finished.
290 
291 PLEASE NOTE:  The 'nents' argument to the pci_unmap_sg call must be
292               the _same_ one you passed into the pci_map_sg call,
293               it should _NOT_ be the 'count' value _returned_ from the
294               pci_map_sg call.
295 
296 Every pci_map_{single,sg} call should have its pci_unmap_{single,sg}
297 counterpart, because the bus address space is a shared resource (although
298 in some ports the mapping is per each BUS so less devices contend for the
299 same bus address space) and you could render the machine unusable by eating
300 all bus addresses.
301 
302 If you need to use the same streaming DMA region multiple times and touch
303 the data in between the DMA transfers, just map it
304 with pci_map_{single,sg}, after each DMA transfer call either:
305 
306         pci_dma_sync_single(dev, dma_handle, size, direction);
307 
308 or:
309 
310         pci_dma_sync_sg(dev, sglist, nents, direction);
311 
312 and after the last DMA transfer call one of the DMA unmap routines
313 pci_unmap_{single,sg}. If you don't touch the data from the first pci_map_*
314 call till pci_unmap_*, then you don't have to call the pci_sync_*
315 routines at all.
316 
317 Here is pseudo code which shows a situation in which you would need
318 to use the pci_dma_sync_*() interfaces.
319 
320         my_card_setup_receive_buffer(struct my_card *cp, char *buffer, int len)
321         {
322                 dma_addr_t mapping;
323 
324                 mapping = pci_map_single(cp->pdev, buffer, len, PCI_DMA_FROMDEVICE);
325 
326                 cp->rx_buf = buffer;
327                 cp->rx_len = len;
328                 cp->rx_dma = mapping;
329 
330                 give_rx_buf_to_card(cp);
331         }
332 
333         ...
334 
335         my_card_interrupt_handler(int irq, void *devid, struct pt_regs *regs)
336         {
337                 struct my_card *cp = devid;
338 
339                 ...
340                 if (read_card_status(cp) == RX_BUF_TRANSFERRED) {
341                         struct my_card_header *hp;
342 
343                         /* Examine the header to see if we wish
344                          * to accept the data.  But synchronize
345                          * the DMA transfer with the CPU first
346                          * so that we see updated contents.
347                          */
348                         pci_dma_sync_single(cp->pdev, cp->rx_dma, cp->rx_len,
349                                             PCI_DMA_FROMDEVICE);
350 
351                         /* Now it is safe to examine the buffer. */
352                         hp = (struct my_card_header *) cp->rx_buf;
353                         if (header_is_ok(hp)) {
354                                 pci_unmap_single(cp->pdev, cp->rx_dma, cp->rx_len,
355                                                  PCI_DMA_FROMDEVICE);
356                                 pass_to_upper_layers(cp->rx_buf);
357                                 make_and_setup_new_rx_buf(cp);
358                         } else {
359                                 /* Just give the buffer back to the card. */
360                                 give_rx_buf_to_card(cp);
361                         }
362                 }
363         }
364 
365 Drivers converted fully to this interface should not use virt_to_bus any
366 longer, nor should they use bus_to_virt. Some drivers have to be changed a
367 little bit, because there is no longer an equivalent to bus_to_virt in the
368 dynamic DMA mapping scheme - you have to always store the DMA addresses
369 returned by the pci_alloc_consistent and pci_map_single calls (pci_map_sg
370 stores them in the scatterlist itself if the platform supports dynamic DMA
371 mapping in hardware) in your driver structures and/or in the card registers.
372 
373 This document, and the API itself, would not be in it's current
374 form without the feedback and suggestions from numerous individuals.
375 We would like to specifically mention, in no particular order, the
376 following people:
377 
378         Russell King <rmk@arm.linux.org.uk>
379         Leo Dagum <dagum@barrel.engr.sgi.com>
380         Ralf Baechle <ralf@oss.sgi.com>
381         Grant Grundler <grundler@cup.hp.com>
382         Jay Estabrook <Jay.Estabrook@compaq.com>
383         Thomas Sailer <sailer@ife.ee.ethz.ch>

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.