Specifications

This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
Direct Memory Access
|
449
Some important rules apply to streaming DMA mappings:
The buffer must be used only for a transfer that matches the direction value
given when it was mapped.
Once a buffer has been mapped, it belongs to the device, not the processor. Until
the buffer has been unmapped, the driver should not touch its contents in any
way. Only after dma_unmap_single has been called is it safe for the driver to
access the contents of the buffer (with one exception that we see shortly).
Among other things, this rule implies that a buffer being written to a device can-
not be mapped until it contains all the data to write.
The buffer must not be unmapped while DMA is still active, or serious system
instability is guaranteed.
You may be wondering why the driver can no longer work with a buffer once it has
been mapped. There are actually two reasons why this rule makes sense. First, when
a buffer is mapped for DMA, the kernel must ensure that all of the data in that buffer
has actually been written to memory. It is likely that some data is in the processor’s
cache when dma_unmap_single is issued, and must be explicitly flushed. Data writ-
ten to the buffer by the processor after the flush may not be visible to the device.
Second, consider what happens if the buffer to be mapped is in a region of memory
that is not accessible to the device. Some architectures simply fail in this case, but
others create a bounce buffer. The bounce buffer is just a separate region of memory
that is accessible to the device. If a buffer is mapped with a direction of
DMA_TO_
DEVICE
, and a bounce buffer is required, the contents of the original buffer are cop-
ied as part of the mapping operation. Clearly, changes to the original buffer after the
copy are not seen by the device. Similarly,
DMA_FROM_DEVICE bounce buffers are cop-
ied back to the original buffer by dma_unmap_single; the data from the device is not
present until that copy has been done.
Incidentally, bounce buffers are one reason why it is important to get the direction
right.
DMA_BIDIRECTIONAL bounce buffers are copied both before and after the opera-
tion, which is often an unnecessary waste of CPU cycles.
Occasionally a driver needs to access the contents of a streaming DMA buffer with-
out unmapping it. A call has been provided to make this possible:
void dma_sync_single_for_cpu(struct device *dev, dma_handle_t bus_addr,
size_t size, enum dma_data_direction direction);
This function should be called before the processor accesses a streaming DMA
buffer. Once the call has been made, the CPU “owns” the DMA buffer and can work
with it as needed. Before the device accesses the buffer, however, ownership should
be transferred back to it with:
void dma_sync_single_for_device(struct device *dev, dma_handle_t bus_addr,
size_t size, enum dma_data_direction direction);
,ch15.13676 Page 449 Friday, January 21, 2005 11:04 AM