[BACK]Return to 3.me CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / share / doc / papers / bus_dma

File: [cvs.NetBSD.org] / src / share / doc / papers / bus_dma / 3.me (download)

Revision 1.2, Wed Feb 5 00:02:26 2003 UTC (21 years, 2 months ago) by perry
Branch: MAIN
CVS Tags: yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, wrstuden-fixsa-newbase, wrstuden-fixsa-base-1, wrstuden-fixsa-base, wrstuden-fixsa, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-3-RELEASE, netbsd-9-2-RELEASE, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, netbsd-4-base, netbsd-4-0-RELEASE, netbsd-4-0-RC5, netbsd-4-0-RC4, netbsd-4-0-RC3, netbsd-4-0-RC2, netbsd-4-0-RC1, netbsd-4-0-1-RELEASE, netbsd-4-0, netbsd-4, netbsd-3-base, netbsd-3-1-RELEASE, netbsd-3-1-RC4, netbsd-3-1-RC3, netbsd-3-1-RC2, netbsd-3-1-RC1, netbsd-3-1-1-RELEASE, netbsd-3-1, netbsd-3-0-RELEASE, netbsd-3-0-RC6, netbsd-3-0-RC5, netbsd-3-0-RC4, netbsd-3-0-RC3, netbsd-3-0-RC2, netbsd-3-0-RC1, netbsd-3-0-3-RELEASE, netbsd-3-0-2-RELEASE, netbsd-3-0-1-RELEASE, netbsd-3-0, netbsd-3, netbsd-2-base, netbsd-2-1-RELEASE, netbsd-2-1-RC6, netbsd-2-1-RC5, netbsd-2-1-RC4, netbsd-2-1-RC3, netbsd-2-1-RC2, netbsd-2-1-RC1, netbsd-2-1, netbsd-2-0-base, netbsd-2-0-RELEASE, netbsd-2-0-RC5, netbsd-2-0-RC4, netbsd-2-0-RC3, netbsd-2-0-RC2, netbsd-2-0-RC1, netbsd-2-0-3-RELEASE, netbsd-2-0-2-RELEASE, netbsd-2-0-1-RELEASE, netbsd-2-0, netbsd-2, netbsd-10-base, netbsd-10-0-RELEASE, netbsd-10-0-RC6, netbsd-10-0-RC5, netbsd-10-0-RC4, netbsd-10-0-RC3, netbsd-10-0-RC2, netbsd-10-0-RC1, netbsd-10, mjf-devfs2-base, mjf-devfs2, matt-premerge-20091211, matt-nb8-mediatek-base, matt-nb8-mediatek, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, matt-mips64-base, matt-mips64, matt-armv6-prevmlocking, matt-armv6-nbase, matt-armv6-base, matt-armv6, localcount-20160914, keiichi-mipv6-nbase, keiichi-mipv6-base, keiichi-mipv6, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, is-mlppp-base, is-mlppp, hpcarm-cleanup-nbase, hpcarm-cleanup-base, hpcarm-cleanup, cube-autoconf-base, cube-autoconf, cjep_sun2x-base1, cjep_sun2x-base, cjep_sun2x, cjep_staticlib_x-base1, cjep_staticlib_x-base, cjep_staticlib_x, cherry-xenmp-base, cherry-xenmp, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, abandoned-netbsd-4-base, abandoned-netbsd-4, HEAD
Changes since 1.1: +2 -2 lines

"Utilize" has exactly the same meaning as "use," but it is more
difficult to read and understand. Most manuals of English style
therefore say that you should use "use".

.\"	$NetBSD: 3.me,v 1.2 2003/02/05 00:02:26 perry Exp $
.\"
.\" Copyright (c) 1998 Jason R. Thorpe.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgements:
.\"	This product includes software developed for the NetBSD Project
.\"	by Jason R. Thorpe.
.\" 4. The name of the author may not be used to endorse or promote products
.\"    derived from this software without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.sh 1 "The \fIbus_dma\fB interface"
.pp
What follows is a description of \fIbus_dma\fR, the DMA portion of the
machine-independent bus access interface in NetBSD, commonly referred to
as \fIbus.h\fR\**.  The DMA portion of the interface is comprised of
three DMA-specific data types and thirteen function calls.  The
\fIbus_dma\fR interface also shares two data types with the
\fIbus_space\fR interface.
.(f
\**The name is derived from the name of the include file that exports
the interface.
.)f
.pp
The \fIbus_dma\fR functional interface is split into two categories:
mapping calls and memory handling calls.  The function calls themselves
may be implemented as \fIcpp(1)\fR macros.
.sh 2 "Data types"
.pp
The first of the two data types shared with the \fIbus_space\fR
interface is the \fIbus_addr_t\fR type, which represents device bus
addresses to be used for CPU access or DMA, and must be large enough to
specify the largest possible bus address on the system.  The second is
the \fIbus_size_t\fR type, which represents sizes of bus address ranges.
.pp
The implementation of DMA on a given host/bus combination is described
by the \fIbus_dma_tag_t\fR.  This opaque type is passed to a bus's
autoconfiguration machinery by machine-dependent code.  The bus layer
in turn passes it down to the device drivers.  This tag is the first
argument to every function in the interface.
.pp
Individual DMA segments are described by the \fIbus_dma_segment_t\fR.
This type is a structure with two publicly accessible members.  The first
member, \fIds_addr\fR, is a \fIbus_addr_t\fR containing the address of a
DMA segment.  The second, \fIds_len\fR, is a
\fIbus_size_t\fR containing the length of the segment.
.pp
The third, and probably most important, data type is the \fIbus_dmamap_t\fR.
This type is a pointer to a structure which describes an individual DMA
mapping.  The structure has three public members.  The first member,
\fIdm_mapsize\fR is a \fIbus_size_t\fR describing the length of the
mapping, when valid.  A \fIdm_mapsize\fR of 0 indicates that the
mapping is invalid.  The second member, \fIdm_nsegs\fR, is an \fIint\fR
which contains the number of DMA segments that comprise the mapping.
The third public member, \fIdm_segs\fR, is an array or a pointer to
an array of \fIbus_dma_segment_t\fR structures.
.pp
In addition to data types, the \fIbus_dma\fR interface also defines
a set of flags which are passed to some of the interface's functions.
Two of these flags, \fBBUS_DMA_WAITOK\fR and \fBBUS_DMA_NOWAIT\fR,
indicate to the function that waiting for resources to become available
is or is not allowed\**.  There
are also four placeholder flags, \fBBUS_DMA_BUS1\fR through
\fBBUS_DMA_BUS4\fR.  These flags are reserved for the individual
bus layers, which may need to define special semantics specific to
that bus.  An example of this is the ability of VESA local bus
devices to use 32-bit DMA addresses; while the kernel considers
such devices to be logically connected to the ISA bus, they are not
limited to the addressing constraints of other ISA devices.
The placeholder flags allow such special cases to be handled on
a bus-by-bus basis.
.(f
\**Waiting (also called "blocking") is allowed only if the kernel is
running in a process context, as opposed to the interrupt context used
when handling device interrupts.
.)f
.sh 2 "Mapping functions"
.pp
There are eight functions in the \fIbus_dma\fR interface that
operate on DMA maps.  These can be sub-categorized into functions
that create and destroy maps, functions that load and unload mappings,
and functions that synchronize maps.
.pp
The first two functions fall into the create/destroy sub-category.
The \fIbus_dmamap_create()\fR function creates a DMA map and initializes
it according to the parameters provided.  The parameters include the
maximum DMA transfer size the DMA map will map, the maximum number of
DMA segments, the maximum size of any given segment, and any DMA
boundary limitations.  In addition to the standard flags,
\fIbus_dmamap_create()\fR also takes the flag \fBBUS_DMA_ALLOCNOW\fR.
This flag indicates that all resources necessary to map the maximum
size transfer should be allocated when the map is created, and is useful
in case the driver must load the DMA map at a time where blocking is not
allowed, such as in interrupt context.  The \fIbus_dmamap_destroy()\fR
function destroys a DMA map, and frees any resources that may be assigned
to it.
.pp
The next five functions fall into the load/unload sub-category.  The
two basic functions are \fIbus_dmamap_load()\fR and
\fIbus_dmamap_unload()\fR.  The former maps a DMA transfer to or from
a linear buffer.  This linear buffer may be mapped into either
kernel or a process's virtual address space.
The latter unloads the mappings previously
loaded into the DMA map.  If the \fBBUS_DMA_ALLOCNOW\fR flag was
specified when the map was created, \fIbus_dmamap_load()\fR will not
block or fail on resource allocation.  Similarly, when the map is
unloaded, the mapping resources will not be freed.
.pp
In addition to linear buffers handled by the basic \fIbus_dmamap_load()\fR,
there are three alternate data buffer structures handled by the interface.
The \fIbus_dmamap_load_mbuf()\fR function operates on mbuf chains.  The
individual data buffers are assumed to be in kernel virtual
address space.  The \fIbus_dmamap_load_uio()\fR function operates
on \fIuio\fR structures, from which it extracts information about the
address space in which the data resides.  Finally, the
\fIbus_dmamap_load_raw()\fR function operates on raw memory, which
is not mapped into any virtual address space.  All DMA maps loaded with
these functions are unloaded with the \fIbus_dmamap_unload()\fR function.
.pp
Finally, the map synchronization sub-category includes one function:
\fIbus_dmamap_sync()\fR.  This function performs the four DMA
synchronization operations necessary to handle caches and DMA bouncing.
The four operations are:
.(b
.b
BUS_DMASYNC_PREREAD
BUS_DMASYNC_POSTREAD
BUS_DMASYNC_PREWRITE
BUS_DMASYNC_POSTWRITE
.r
.)b
The direction is expressed from the perspective of the host's memory.
In other words, a device-to-memory transfer is a read, and a
memory-to-device transfer is
a write.  The synchronization operations are expressed as flags,
so it is possible to combine \fBREAD\fR and \fBWRITE\fR operations
in a single call.  This is especially useful for synchronizing
mappings of device control descriptors.  Mixing of \fBPRE\fR and
\fBPOST\fR operations is not allowed.
.pp
In addition to the map and operation arguments, \fIbus_dmamap_sync()\fR
also takes offset and length arguments.  This is done in order to
support partial syncs.  In the case where a control descriptor
is DMA'd to a device, it may be undesirable to synchronize the entire
mapping, as doing so may be inefficient or even destructive to other
control descriptors.  Synchronizing the entire mapping is supported
by passing an offset of 0 and the length specified by the map's
\fIdm_mapsize\fR.
.sh 2 "Memory handling functions"
.pp
There are two sub-categories of functions that handle DMA-safe
memory in the \fIbus_dma\fR interface: memory allocation and
memory mapping.
.pp
The first function in the memory allocation sub-category,
\fIbus_dmamem_alloc()\fR, allocates memory which has the specified
attributes.  The attributes that may be specified are: the size of the
memory region to allocate, the alignment of each segment in the
allocation, any boundary limitations, and the maximum number
of DMA segments that may make up the allocation.  The function
fills in a provided array of \fIbus_dma_segment_t\fRs and indicates
the number of valid segments in the array.  Memory allocated
by this interface is raw memory\**; it is not mapped into any virtual
address space.  Once it is no longer in use, it may be freed with
the \fIbus_dmamem_free()\fR function.
.(f
\**This implies that \fIbus_dmamap_load_raw()\fR is an appropriate
interface for mapping a DMA transfer to or from memory allocated by
this interface.
.)f
.pp
In order for the kernel or a user process to access the memory,
it must be mapped either into the kernel address space or the process's
address space.  These
operations are performed by the memory mapping sub-category of DMA-safe
memory handling functions.
The \fIbus_dmamem_map()\fR function maps the specified DMA-safe
raw memory into the kernel address space.  The address
of the mapping is returned by filling in a pointer passed
by reference.  Memory mapped in this manner may be unmapped by
calling \fIbus_dmamem_unmap()\fR.
.pp
DMA-safe raw memory may be mapped into a process's address space via
a device driver's \fImmap()\fR entry point.  In order to do this,
the VM system's device pager repeatedly calls the driver,
once for each page that is to be mapped.  The driver translates
the user-specified mmap offset into a DMA memory offset, and
calls the \fIbus_dmamem_mmap()\fR function to translate the
memory offset into an opaque value to be interpreted
by the \fIpmap module\**\fR.  The device pager invokes the
pmap module to translate the mmap cookie into a physical page
address which is then mapped into the process's address space.
.(f
\**The pmap module is the machine-dependent layer of the NetBSD
virtual memory system.
.)f
.pp
There are currently no methods for the virtual memory system to specify
that an mmap'd area is being unmapped, or for the device driver to specify
to the virtual memory system that a mmap'd region must be forcibly unmapped
(for example, if a hot-swapable device has been removed from the system).
This is widely regarded as a bug, and may be addressed in a future version
of the NetBSD virtual memory system.  If a change to this effect is made,
the \fIbus_dma\fR interface will have to be adjusted accordingly.