Specifications
This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
Direct Memory Access
|
441
Overview of a DMA Data Transfer
Before introducing the programming details, let’s review how a DMA transfer takes
place, considering only input transfers to simplify the discussion.
Data transfer can be triggered in two ways: either the software asks for data (via a
function such as read) or the hardware asynchronously pushes data to the system.
In the first case, the steps involved can be summarized as follows:
1. When a process calls read, the driver method allocates a DMA buffer and
instructs the hardware to transfer its data into that buffer. The process is put to
sleep.
2. The hardware writes data to the DMA buffer and raises an interrupt when it’s
done.
3. The interrupt handler gets the input data, acknowledges the interrupt, and
awakens the process, which is now able to read data.
The second case comes about when DMA is used asynchronously. This happens, for
example, with data acquisition devices that go on pushing data even if nobody is
reading them. In this case, the driver should maintain a buffer so that a subsequent
read call will return all the accumulated data to user space. The steps involved in this
kind of transfer are slightly different:
1. The hardware raises an interrupt to announce that new data has arrived.
2. The interrupt handler allocates a buffer and tells the hardware where to transfer
its data.
3. The peripheral device writes the data to the buffer and raises another interrupt
when it’s done.
4. The handler dispatches the new data, wakes any relevant process, and takes care
of housekeeping.
A variant of the asynchronous approach is often seen with network cards. These
cards often expect to see a circular buffer (often called a DMA ring buffer) estab-
lished in memory shared with the processor; each incoming packet is placed in the
next available buffer in the ring, and an interrupt is signaled. The driver then passes
the network packets to the rest of the kernel and places a new DMA buffer in the
ring.
The processing steps in all of these cases emphasize that efficient DMA handling
relies on interrupt reporting. While it is possible to implement DMA with a polling
driver, it wouldn’t make sense, because a polling driver would waste the perfor-
mance benefits that DMA offers over the easier processor-driven I/O.
*
* There are, of course, exceptions to everything; see the section “Receive Interrupt Mitigation” in Chapter 17
for a demonstration of how high-performance network drivers are best implemented using polling.
,ch15.13676 Page 441 Friday, January 21, 2005 11:04 AM