Specifications

This is the Title of the Book, eMatter Edition
Copyright © 2005 O’Reilly & Associates, Inc. All rights reserved.
The mmap Device Operation
|
423
The full list of the X server’s VMAs is lengthy, but most of the entries are not of inter-
est here. We do see, however, four separate mappings of /dev/mem, which give some
insight into how the X server works with the video card. The first mapping is at
a0000, which is the standard location for video RAM in the 640-KB ISA hole. Further
down, we see a large mapping at
e8000000, an address which is above the highest
RAM address on the system. This is a direct mapping of the video memory on the
adapter.
These regions can also be seen in /proc/iomem:
000a0000-000bffff : Video RAM area
000c0000-000ccfff : Video ROM
000d1000-000d1fff : Adapter ROM
000f0000-000fffff : System ROM
d7f00000-f7efffff : PCI Bus #01
e8000000-efffffff : 0000:01:00.0
fc700000-fccfffff : PCI Bus #01
fcc00000-fcc0ffff : 0000:01:00.0
Mapping a device means associating a range of user-space addresses to device mem-
ory. Whenever the program reads or writes in the assigned address range, it is actu-
ally accessing the device. In the X server example, using mmap allows quick and easy
access to the video card’s memory. For a performance-critical application like this,
direct access makes a large difference.
As you might suspect, not every device lends itself to the mmap abstraction; it makes
no sense, for instance, for serial ports and other stream-oriented devices. Another
limitation of mmap is that mapping is
PAGE_SIZE grained. The kernel can manage vir-
tual addresses only at the level of page tables; therefore, the mapped area must be a
multiple of
PAGE_SIZE and must live in physical memory starting at an address that is
a multiple of
PAGE_SIZE. The kernel forces size granularity by making a region slightly
bigger if its size isn’t a multiple of the page size.
These limits are not a big constraint for drivers, because the program accessing the
device is device dependent anyway. Since the program must know about how the
device works, the programmer is not unduly bothered by the need to see to details
like page alignment. A bigger constraint exists when ISA devices are used on some
non-x86 platforms, because their hardware view of ISA may not be contiguous. For
example, some Alpha computers see ISA memory as a scattered set of 8-bit, 16-bit,
or 32-bit items, with no direct mapping. In such cases, you can’t use mmap at all.
The inability to perform direct mapping of ISA addresses to Alpha addresses is due
to the incompatible data transfer specifications of the two systems. Whereas early
Alpha processors could issue only 32-bit and 64-bit memory accesses, ISA can do
only 8-bit and 16-bit transfers, and there’s no way to transparently map one proto-
col onto the other.
There are sound advantages to using mmap when it’s feasible to do so. For instance,
we have already looked at the X server, which transfers a lot of data to and from
,ch15.13676 Page 423 Friday, January 21, 2005 11:04 AM