1 .\" Copyright (c) 2002, 2003 Hiten M. Pandya.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions, and the following disclaimer,
9 .\" without modification, immediately at the beginning of the file.
10 .\" 2. The name of the author may not be used to endorse or promote products
11 .\" derived from this software without specific prior written permission.
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, CONTRIBUTORS OR THE
17 .\" VOICES IN HITEN PANDYA'S HEAD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19 .\" TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 .\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 .\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22 .\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 .\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 .\" Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
26 .\" All rights reserved.
28 .\" This code is derived from software contributed to The NetBSD Foundation
29 .\" by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
30 .\" NASA Ames Research Center.
32 .\" Redistribution and use in source and binary forms, with or without
33 .\" modification, are permitted provided that the following conditions
35 .\" 1. Redistributions of source code must retain the above copyright
36 .\" notice, this list of conditions and the following disclaimer.
37 .\" 2. Redistributions in binary form must reproduce the above copyright
38 .\" notice, this list of conditions and the following disclaimer in the
39 .\" documentation and/or other materials provided with the distribution.
40 .\" 3. All advertising materials mentioning features or use of this software
41 .\" must display the following acknowledgment:
42 .\" This product includes software developed by the NetBSD
43 .\" Foundation, Inc. and its contributors.
44 .\" 4. Neither the name of The NetBSD Foundation nor the names of its
45 .\" contributors may be used to endorse or promote products derived
46 .\" from this software without specific prior written permission.
48 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
49 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 .\" POSSIBILITY OF SUCH DAMAGE.
61 .\" $NetBSD: bus_dma.9,v 1.25 2002/10/14 13:43:16 wiz Exp $
68 .Nm bus_dma_tag_create ,
69 .Nm bus_dma_tag_destroy ,
70 .Nm bus_dmamap_create ,
71 .Nm bus_dmamap_destroy ,
73 .Nm bus_dmamap_load_mbuf ,
74 .Nm bus_dmamap_load_uio ,
75 .Nm bus_dmamap_unload ,
77 .Nm bus_dmamem_alloc ,
79 .Nd Bus and Machine Independent DMA Mapping Interface
83 .Fn bus_dma_tag_create "bus_dma_tag_t parent" "bus_size_t alignment" \
84 "bus_size_t boundary" "bus_addr_t lowaddr" "bus_addr_t highaddr" \
85 "bus_dma_filter_t *filtfunc" "void *filtfuncarg" "bus_size_t maxsize" \
86 "int nsegments" "bus_size_t maxsegsz" "int flags" "bus_dma_lock_t *lockfunc" \
87 "void *lockfuncarg" "bus_dma_tag_t *dmat"
89 .Fn bus_dma_tag_destroy "bus_dma_tag_t dmat"
91 .Fn bus_dmamap_create "bus_dma_tag_t dmat" "int flags" "bus_dmamap_t *mapp"
93 .Fn bus_dmamap_destroy "bus_dma_tag_t dmat" "bus_dmamap_t map"
95 .Fn bus_dmamap_load "bus_dma_tag_t dmat" "bus_dmamap_t map" "void *buf" \
96 "bus_size_t buflen" "bus_dmamap_callback_t *callback" "void *callback_arg" \
99 .Fn bus_dmamap_load_mbuf "bus_dma_tag_t dmat" "bus_dmamap_t map" \
100 "struct mbuf *mbuf" "bus_dmamap_callback2_t *callback" "void *callback_arg" \
103 .Fn bus_dmamap_load_uio "bus_dma_tag_t dmat" "bus_dmamap_t map" \
104 "struct uio *uio" "bus_dmamap_callback2_t *callback" "void *callback_arg" \
107 .Fn bus_dmamap_unload "bus_dma_tag_t dmat" "bus_dmamap_t map"
109 .Fn bus_dmamap_sync "bus_dma_tag_t dmat" "bus_dmamap_t map" \
112 .Fn bus_dmamem_alloc "bus_dma_tag_t dmat" "void **vaddr" \
113 "int flags" "bus_dmamap_t *mapp"
115 .Fn bus_dmamem_free "bus_dma_tag_t dmat" "void *vaddr" \
118 Direct Memory Access (DMA) is a method of transferring data
119 without involving the CPU, thus providing higher performance.
120 A DMA transaction can be achieved between device to memory,
121 device to device, or memory to memory.
125 API is a bus, device, and machine-independent (MI) interface to
127 It provides the client with flexibility and simplicity by
128 abstracting machine dependent issues like setting up
129 DMA mappings, handling cache issues, bus specific features
131 .Sh STRUCTURES AND TYPES
132 .Bl -tag -width compact
134 A machine-dependent (MD) opaque type that describes the
135 characteristics of DMA transactions.
136 DMA tags are organized into a hierarchy, with each child
137 tag inheriting the restrictions of its parent.
138 This allows all devices along the path of DMA transactions
139 to contribute to the constraints of those transactions.
140 .It Vt bus_dma_filter_t
141 Client specified address filter having the format:
142 .Bl -tag -width compact
144 .Fn "client_filter" "void *filtarg" "bus_addr_t testaddr"
147 Address filters can be specified during tag creation to allow
148 for devices who's DMA address restrictions cannot be specified
152 is client specified during tag creation to be passed to all
153 invocations of the callback.
156 argument contains a potential starting address of a DMA mapping.
157 The filter function operates on the set of addresses from
160 .Ql trunc_page(testaddr) + PAGE_SIZE - 1 ,
162 The filter function should return zero for any mapping in this range
163 that can be accommodated by the device and non-zero otherwise.
164 .It Vt bus_dma_segment_t
165 A machine-dependent type that describes individual
174 field contains the device visible address of the DMA segment, and
176 contains the length of the DMA segment.
177 Although the DMA segments returned by a mapping call will adhere to
178 all restrictions necessary for a successful DMA operation, some conversion
179 (e.g.\& a conversion from host byte order to the device's byte order) is
180 almost always required when presenting segment information to the device.
182 A machine-dependent opaque type describing an individual mapping.
183 Multiple DMA maps can be associated with one DMA tag.
184 .It Vt bus_dmamap_callback_t
185 Client specified callback for receiving mapping information resulting from
189 .Fn bus_dmamap_load .
190 Callbacks are of the format:
191 .Bl -tag -width compact
193 .Fn "client_callback" "void *callback_arg" "bus_dma_segment_t *segs" \
194 "int nseg" "int error"
199 is the callback argument passed to dmamap load functions.
204 parameters describe an array of
205 .Vt bus_dma_segment_t
206 structures that represent the mapping.
207 This array is only valid within the scope of the callback function.
208 The success or failure of the mapping is indicated by the
211 More information on the use of callbacks can be found in the
212 description of the individual dmamap load functions.
213 .It Vt bus_dmamap_callback2_t
214 Client specified callback for receiving mapping information resulting from
218 .Fn bus_dmamap_load_uio
220 .Fn bus_dmamap_load_mbuf .
222 Callback2s are of the format:
223 .Bl -tag -width compact
225 .Fn "client_callback2" "void *callback_arg" "bus_dma_segment_t *segs" \
226 "int nseg" "bus_size_t mapsize" "int error"
229 Callback2's behavior is the same as
230 .Vt bus_dmamap_callback_t
231 with the addition that the length of the data mapped is provided via
233 .It Vt bus_dmasync_op_t
234 Memory synchronization operation specifier.
235 Bus DMA requires explicit synchronization of memory with it's device
236 visible mapping in order to guarantee memory coherency.
239 allows the type of DMA operation that will be or has been performed
240 to be communicated to the system so that the correct coherency measures
242 All operations specified below are performed from the CPU's
243 point of view (for a complete description, see the
246 .Bl -tag -width BUS_DMASYNC_POSTWRITE
247 .It Dv BUS_DMASYNC_PREREAD
248 Perform any synchronization required after an update of memory by the CPU
249 but prior to DMA read operations.
250 .It Dv BUS_DMASYNC_PREWRITE
251 Perform any synchronization required after an update of memory by the CPU
252 but prior to DMA write operations.
253 .It Dv BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE
254 Perform any synchronization required prior to a combination of DMA read
255 and write operations.
256 .It Dv BUS_DMASYNC_POSTREAD
257 Perform any synchronization required after DMA read operations, but prior
258 to CPU access of the memory.
259 .It Dv BUS_DMASYNC_POSTWRITE
260 Perform any synchronization required after DMA write operations, but prior
261 to CPU access of the memory.
262 .It Dv BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE
263 Perform any synchronization required after a combination of DMA read
264 and write operations.
266 .It Vt bus_dma_lock_t
267 Client specified lock/mutex manipulation method.
268 This will be called from
269 within busdma whenever a client lock needs to be manipulated.
270 In its current form, the function will be called immediately before
271 the callback for a dma load operation that has been deferred with
273 and immediately after with
275 If the load operation does not need to be deferred, then it
276 will not be called since the function loading the map should
277 be holding the appropriate locks.
278 This method is of the format:
279 .Bl -tag -width compact
281 .Fn "lockfunc" "void *lockfunc_arg" "bus_dma_lock_op_t op"
286 implementations are provided for convenience.
287 .Fn busdma_lock_mutex
288 performs standard mutex operations on the sleep mutex provided via the
291 .Fn bus_dma_tag_create .
293 will generate a system panic if it is called.
294 It is substituted into the tag when
297 .Fn bus_dma_tag_create .
298 .It Vt bus_dma_lock_op_t
299 Operations to be performed by the client-specified
301 .Bl -tag -width BUS_DMA_UNLOCK
303 Acquires and/or locks the client locking primitive.
304 .It Dv BUS_DMA_UNLOCK
305 Releases and/or unlocks the client locking primitive.
310 .Bl -tag -width compact
311 .It Fn bus_dma_tag_create "parent" "alignment" "boundary" "lowaddr" \
312 "highaddr" "*filtfunc" "*filtfuncarg" "maxsize" "nsegments" "maxsegsz" \
313 "flags" "lockfunc" "lockfuncarg" "*dmat"
314 Allocates a device specific DMA tag, and initializes it according to
315 the arguments provided:
316 .Bl -tag -width *filtfuncarg -compact
318 Indicates restrictions between the parent bridge, CPU memory, and the
320 May be NULL, if no DMA restrictions are to be inherited.
322 Alignment constraint, in bytes, of any mappings created using this tag.
323 The alignment must be a power of 2.
324 Hardware that can DMA starting at any address would specify
327 Hardware requiring DMA transfers to start on a multiple of 4K
331 Boundary constraint, in bytes, of the target DMA memory region.
332 The boundary indicates the set of addresses, all multiples of the
333 boundary argument, that cannot be crossed by a single
334 .Vt bus_dma_segment_t .
335 The boundary must be a power of 2 and must be no smaller that the
336 maximum segment size.
338 indicates that there are no boundary restrictions.
341 Bounds of the window of bus address space that
343 be directly accessed by the device.
344 The window contains all address greater than lowaddr and
345 less than or equal to highaddr.
346 For example, a device incapable of DMA above 4GB, would specify
348 .Dv BUS_SPACE_MAXADDR
350 .Dv BUS_SPACE_MAXADDR_32BIT .
351 Similarly a device that can only dma to addresses bellow 16MB would
352 specify a highaddr of
353 .Dv BUS_SPACE_MAXADDR
355 .Dv BUS_SPACE_MAXADDR_24BIT .
356 Some implementations requires that some region of device visible
357 address space, overlapping available host memory, be outside the
361 is used to bounce requests that would otherwise conflict with
362 the exclusion window.
364 Optional filter function (may be NULL) to be called for any attempt to
365 map memory into the window described by
369 A filter function is only required when the single window described
374 cannot adequately describe the constraints of the device.
375 The filter function will be called for every machine page
376 that overlaps the exclusion window.
378 Argument passed to all calls to the filter function for this tag.
381 Maximum size, in bytes, of the sum of all segment lengths in a given
382 DMA mapping associated with this tag.
384 Number of discontinuities (scatter/gather segments) allowed
385 in a DMA mapped region.
386 If there is no restriction,
387 .Dv BUS_SPACE_UNRESTRICTED
390 Maximum size, in bytes, of a segment in any DMA mapped region associated
395 .Bl -tag -width "BUS_DMA_ALLOCNOW" -compact
396 .It Dv BUS_DMA_ALLOCNOW
397 Pre-allocate enough resources to handle at least one map load operation on
398 this tag without blocking.
399 If sufficient resources are not available,
402 This should not be used for tags that will not be directly associated with
406 Optional lock manipulation function (may be NULL) to be called when busdma
407 needs to manipulate a lock on behalf of the client.
408 If NULL is specified,
412 Optional argument to be passed to the function specified by
415 Pointer to a bus_dma_tag_t where the resulting DMA tag will
421 if sufficient memory is not available for tag creation
422 or allocating mapping resources.
423 .It Fn bus_dma_tag_destroy "dmat"
424 Deallocate the DMA tag
427 .Fn bus_dma_tag_create .
431 if any DMA maps remain associated with
436 .It Fn bus_dmamap_create "dmat" "flags" "*mapp"
437 Allocates and initializes a DMA map.
438 Arguments are as follows:
439 .Bl -tag -width nsegments -compact
443 The value of this argument is currently undefined and should be
449 where the resulting DMA map will be stored.
454 if sufficient memory is not available for creating the
455 map or allocating mapping resources.
456 .It Fn bus_dmamap_destroy "dmat" "map"
457 Frees all resources associated with a given DMA map.
458 Arguments are as follows:
459 .Bl -tag -width dmat -compact
461 DMA tag used to allocate
464 The DMA map to destroy.
469 if a mapping is still active for
471 .It Fn bus_dmamap_load "dmat" "map" "buf" "buflen" "*callback" \
472 "callback_arg" "flags"
473 Creates a mapping in device visible address space of
477 associated with the DMA map
479 Arguments are as follows:
480 .Bl -tag -width buflen -compact
482 DMA tag used to allocate
485 A DMA map without a currently active mapping.
487 A kernel virtual address pointer to a contiguous (in KVA) buffer, to be
488 mapped into device visible address space.
490 The size of the buffer.
491 .It Fa callback Fa callback_arg
492 The callback function, and its argument.
494 The value of this argument is currently undefined, and should be
499 Return values to the caller are as follows:
500 .Bl -tag -width EINPROGRESS -compact
502 The callback has been called and completed.
503 The status of the mapping has been delivered to the callback.
505 The mapping has been deferred for lack of resources.
506 The callback will be called as soon as resources are available.
507 Callbacks are serviced in FIFO order.
508 To ensure that ordering is guaranteed, all subsequent load requests will also
509 be deferred until all callbacks have been processed.
511 The load request was invalid.
512 The callback has not, and will not be called.
513 This error value may indicate that
523 argument used to create the dma tag
527 When the callback is called, it is presented with an error value
528 indicating the disposition of the mapping.
529 Error may be one of the following:
530 .Bl -tag -width EINPROGRESS -compact
532 The mapping was successful and the
534 callback argument contains an array of
535 .Vt bus_dma_segment_t
536 elements describing the mapping.
537 This array is only valid during the scope of the callback function.
539 A mapping could not be achieved within the segment constraints provided
540 in the tag even though the requested allocation size was less than maxsize.
542 .It Fn bus_dmamap_load_mbuf "dmat" "map" "mbuf" "callback2" "callback_arg" \
544 This is a variation of
546 which maps mbuf chains
550 argument is also passed to the callback routine, which
551 contains the mbuf chain's packet header length.
553 Mbuf chains are assumed to be in kernel virtual address space.
557 if the size of the mbuf chain exceeds the maximum limit of the
559 .It Fn bus_dmamap_load_uio "dmat" "map" "uio" "callback2" "callback_arg" "flags"
560 This is a variation of
562 which maps buffers pointed to by
567 argument is also passed to the callback routine, which contains the size of
576 then it is assumed that the buffer,
579 .Fa "uio->uio_td->td_proc" Ns 's
581 User space memory must be in-core and wired prior to attempting a map
583 Pages may be locked using
585 .It Fn bus_dmamap_unload "dmat" "map"
587 Arguments are as follows:
588 .Bl -tag -width dmam -compact
590 DMA tag used to allocate
593 The DMA map that is to be unloaded.
596 .Fn bus_dmamap_unload
597 will not perform any implicit synchronization of DMA buffers.
598 This must be done explicitly by a call to
600 prior to unloading the map.
601 .It Fn bus_dmamap_sync "dmat" "map" "op"
602 Performs synchronization of a device visible mapping with the CPU visible
603 memory referenced by that mapping.
604 Arguments are as follows:
605 .Bl -tag -width dmat -compact
607 DMA tag used to allocate
610 The DMA mapping to be synchronized.
612 Type of synchronization operation to perform.
613 See the definition of
615 for a description of the acceptable values for
620 is the method used to ensure that CPU and device DMA access to shared
622 For example, the CPU might be used to setup the contents of a buffer
623 that is to be DMA'ed into a device.
624 To ensure that the data are visible via the device's mapping of that
625 memory, the buffer must be loaded and a dma sync operation of
626 .Dv BUS_DMASYNC_PREREAD
628 Additional sync operations must be performed after every CPU write
629 to this memory if additional DMA reads are to be performed.
630 Conversely, for the DMA write case, the buffer must be loaded,
631 and a dma sync operation of
632 .Dv BUS_DMASYNC_PREWRITE
634 The CPU will only be able to see the results of this DMA write
635 once the DMA has completed and a
636 .Dv BUS_DMASYNC_POSTWRITE
637 operation has been performed.
639 If DMA read and write operations are not preceded and followed by the
640 appropriate synchronization operations, behavior is undefined.
641 .It Fn bus_dmamem_alloc "dmat" "**vaddr" "flags" "mapp"
642 Allocates memory that is mapped into KVA at the address returned
645 that is permanently loaded into the newly created
649 Arguments are as follows:
650 .Bl -tag -width alignment -compact
652 DMA tag describing the constraints of the DMA mapping.
654 Pointer to a pointer that will hold the returned KVA mapping of
655 the allocated region.
657 Flags are defined as follows:
658 .Bl -tag -width BUS_DMA_NOWAIT -compact
659 .It Dv BUS_DMA_WAITOK
660 The routine can safely wait (sleep) for resources.
661 .It Dv BUS_DMA_NOWAIT
662 The routine is not allowed to wait for resources.
663 If resources are not available,
666 .It Dv BUS_DMA_COHERENT
667 Attempt to map this memory such that cache sync operations are
668 as cheap as possible.
669 This flag is typically set on memory that will be accessed by both
670 a CPU and a DMA engine, frequently.
671 Use of this flag does not remove the requirement of using
672 bus_dmamap_sync, but it may reduce the cost of performing
675 Causes the allocated memory to be set to all zeros.
678 Pointer to storage for the returned DMA map.
681 The size of memory to be allocated is
686 The current implementation of
688 will allocate all requests as a single segment.
690 Although no explicit loading is required to access the memory
691 referenced by the returned map, the synchronization requirements
698 if sufficient memory is not available for completing
700 .It Fn bus_dmamem_free "dmat" "*vaddr" "map"
701 Frees memory previously allocated by
702 .Fn bus_dmamem_alloc .
705 Arguments are as follows:
706 .Bl -tag -width vaddr -compact
710 Kernel virtual address of the memory.
712 DMA map to be invalidated.
716 Behavior is undefined if invalid arguments are passed to
717 any of the above functions.
718 If sufficient resources cannot be allocated for a given
723 routines that are not of type,
725 will return 0 on success or an error
726 code, as discussed above.
730 routines will succeed if provided with valid arguments.
739 .%A "Jason R. Thorpe"
740 .%T "A Machine-Independent DMA Framework for NetBSD"
741 .%J "Proceedings of the Summer 1998 USENIX Technical Conference"
742 .%Q "USENIX Association"
748 interface first appeared in
755 for use in the CAM SCSI subsystem.
756 The alterations to the original API were aimed to remove the need for
758 .Vt bus_dma_segment_t
761 while allowing callers to queue up on scarce resources.
765 interface was designed and implemented by
767 of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
768 Additional input on the
770 design was provided by
772 .An Chris Demetriou ,
784 benefits from the contributions of
785 .An Justin T. Gibbs ,
788 .An Matthew N. Dodd ,
791 .An Jake Burkholder ,
792 .An Takahashi Yoshihiro ,
796 This manual page was written by
799 .An Justin T. Gibbs .