Specifications
This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
Direct Memory Access
|
443
when the requested buffer is far less than 128 KB, because system memory becomes
fragmented over time.
*
When the kernel cannot return the requested amount of memory or when you need
more than 128 KB (a common requirement for PCI frame grabbers, for example), an
alternative to returning
-ENOMEM is to allocate memory at boot time or reserve the top
of physical RAM for your buffer. We described allocation at boot time in the section
“Obtaining Large Buffers” in Chapter 8, but it is not available to modules. Reserving
the top of RAM is accomplished by passing a
mem= argument to the kernel at boot
time. For example, if you have 256 MB, the argument
mem=255M keeps the kernel from
using the top megabyte. Your module could later use the following code to gain
access to such memory:
dmabuf = ioremap (0xFF00000 /* 255M */, 0x100000 /* 1M */);
The allocator, part of the sample code accompanying the book, offers a simple API to
probe and manage such reserved RAM and has been used successfully on several
architectures. However, this trick doesn’t work when you have an high-memory sys-
tem (i.e., one with more physical memory than could fit in the CPU address space).
Another option, of course, is to allocate your buffer with the
GFP_NOFAIL allocation
flag. This approach does, however, severely stress the memory management sub-
system, and it runs the risk of locking up the system altogether; it is best avoided
unless there is truly no other way.
If you are going to such lengths to allocate a large DMA buffer, however, it is worth
putting some thought into alternatives. If your device can do scatter/gather I/O, you
can allocate your buffer in smaller pieces and let the device do the rest. Scatter/gather
I/O can also be used when performing direct I/O into user space, which may well be
the best solution when a truly huge buffer is required.
Bus Addresses
A device driver using DMA has to talk to hardware connected to the interface bus,
which uses physical addresses, whereas program code uses virtual addresses.
As a matter of fact, the situation is slightly more complicated than that. DMA-based
hardware uses bus, rather than physical, addresses. Although ISA and PCI bus
addresses are simply physical addresses on the PC, this is not true for every plat-
form. Sometimes the interface bus is connected through bridge circuitry that maps I/O
addresses to different physical addresses. Some systems even have a page-mapping
scheme that can make arbitrary pages appear contiguous to the peripheral bus.
* The word fragmentation is usually applied to disks to express the idea that files are not stored consecutively
on the magnetic medium. The same concept applies to memory, where each virtual address space gets scat-
tered throughout physical RAM, and it becomes difficult to retrieve consecutive free pages when a DMA
buffer is requested.
,ch15.13676 Page 443 Friday, January 21, 2005 11:04 AM