Specifications

This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
442
|
Chapter 15: Memory Mapping and DMA
Another relevant item introduced here is the DMA buffer. DMA requires device driv-
ers to allocate one or more special buffers suited to DMA. Note that many drivers
allocate their buffers at initialization time and use them until shutdown—the word
allocate in the previous lists, therefore, means “get hold of a previously allocated
buffer.”
Allocating the DMA Buffer
This section covers the allocation of DMA buffers at a low level; we introduce a
higher-level interface shortly, but it is still a good idea to understand the material
presented here.
The main issue that arrises with DMA buffers is that, when they are bigger than one
page, they must occupy contiguous pages in physical memory because the device
transfers data using the ISA or PCI system bus, both of which carry physical
addresses. It’s interesting to note that this constraint doesn’t apply to the SBus (see
the section “SBus” in Chapter 12), which uses virtual addresses on the peripheral
bus. Some architectures can also use virtual addresses on the PCI bus, but a portable
driver cannot count on that capability.
Although DMA buffers can be allocated either at system boot or at runtime, mod-
ules can allocate their buffers only at runtime. (Chapter 8 introduced these tech-
niques; the section “Obtaining Large Buffers” covered allocation at system boot,
while “The Real Story of kmalloc” and “get_free_page and Friends” described alloca-
tion at runtime.) Driver writers must take care to allocate the right kind of memory
when it is used for DMA operations; not all memory zones are suitable. In particu-
lar, high memory may not work for DMA on some systems and with some devices—
the peripherals simply cannot work with addresses that high.
Most devices on modern buses can handle 32-bit addresses, meaning that normal
memory allocations work just fine for them. Some PCI devices, however, fail to
implement the full PCI standard and cannot work with 32-bit addresses. And ISA
devices, of course, are limited to 24-bit addresses only.
For devices with this kind of limitation, memory should be allocated from the DMA
zone by adding the
GFP_DMA flag to the kmalloc or get_free_pages call. When this flag
is present, only memory that can be addressed with 24 bits is allocated. Alterna-
tively, you can use the generic DMA layer (which we discuss shortly) to allocate buff-
ers that work around your device’s limitations.
Do-it-yourself allocation
We have seen how get_free_pages can allocate up to a few megabytes (as order can
range up to
MAX_ORDER, currently 11), but high-order requests are prone to fail even
,ch15.13676 Page 442 Friday, January 21, 2005 11:04 AM