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.
41 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
42 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
43 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
45 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51 .\" POSSIBILITY OF SUCH DAMAGE.
54 .\" $NetBSD: bus_dma.9,v 1.25 2002/10/14 13:43:16 wiz Exp $
61 .Nm bus_dma_tag_create ,
62 .Nm bus_dma_tag_destroy ,
63 .Nm bus_dma_template_init ,
64 .Nm bus_dma_template_tag ,
65 .Nm bus_dma_template_clone ,
66 .Nm bus_dma_template_fill ,
67 .Nm BUS_DMA_TEMPLATE_FILL ,
68 .Nm bus_dmamap_create ,
69 .Nm bus_dmamap_destroy ,
71 .Nm bus_dmamap_load_bio ,
72 .Nm bus_dmamap_load_ccb ,
73 .Nm bus_dmamap_load_crp ,
74 .Nm bus_dmamap_load_crp_buffer ,
75 .Nm bus_dmamap_load_mbuf ,
76 .Nm bus_dmamap_load_mbuf_sg ,
77 .Nm bus_dmamap_load_uio ,
78 .Nm bus_dmamap_unload ,
80 .Nm bus_dmamem_alloc ,
82 .Nd Bus and Machine Independent DMA Mapping Interface
86 .Fn bus_dma_tag_create "bus_dma_tag_t parent" "bus_size_t alignment" \
87 "bus_addr_t boundary" "bus_addr_t lowaddr" "bus_addr_t highaddr" \
88 "bus_dma_filter_t *filtfunc" "void *filtfuncarg" "bus_size_t maxsize" \
89 "int nsegments" "bus_size_t maxsegsz" "int flags" "bus_dma_lock_t *lockfunc" \
90 "void *lockfuncarg" "bus_dma_tag_t *dmat"
92 .Fn bus_dma_tag_destroy "bus_dma_tag_t dmat"
94 .Fo bus_dma_template_init
95 .Fa "bus_dma_template_t *template"
96 .Fa "bus_dma_tag_t parent"
99 .Fo bus_dma_template_tag
100 .Fa "bus_dma_template_t *template"
101 .Fa "bus_dma_tag_t *dmat"
104 .Fo bus_dma_template_clone
105 .Fa "bus_dma_template_t *template"
106 .Fa "bus_dma_tag_t dmat"
109 .Fo bus_dma_template_fill
110 .Fa "bus_dma_template_t *template"
111 .Fa "bus_dma_param_t params[]"
114 .Fo BUS_DMA_TEMPLATE_FILL
115 .Fa "bus_dma_template_t *template"
116 .Fa "bus_dma_param_t param ..."
119 .Fn bus_dmamap_create "bus_dma_tag_t dmat" "int flags" "bus_dmamap_t *mapp"
121 .Fn bus_dmamap_destroy "bus_dma_tag_t dmat" "bus_dmamap_t map"
123 .Fn bus_dmamap_load "bus_dma_tag_t dmat" "bus_dmamap_t map" "void *buf" \
124 "bus_size_t buflen" "bus_dmamap_callback_t *callback" "void *callback_arg" \
127 .Fn bus_dmamap_load_bio "bus_dma_tag_t dmat" "bus_dmamap_t map" \
128 "struct bio *bio" "bus_dmamap_callback_t *callback" "void *callback_arg" \
131 .Fn bus_dmamap_load_ccb "bus_dma_tag_t dmat" "bus_dmamap_t map" \
132 "union ccb *ccb" "bus_dmamap_callback_t *callback" "void *callback_arg" \
135 .Fn bus_dmamap_load_crp "bus_dma_tag_t dmat" "bus_dmamap_t map" \
136 "struct crypto *crp" "bus_dmamap_callback_t *callback" "void *callback_arg" \
139 .Fn bus_dmamap_load_crp_buffer "bus_dma_tag_t dmat" "bus_dmamap_t map" \
140 "struct crypto_buffer *cb" "bus_dmamap_callback_t *callback" \
141 "void *callback_arg" "int flags"
143 .Fn bus_dmamap_load_mbuf "bus_dma_tag_t dmat" "bus_dmamap_t map" \
144 "struct mbuf *mbuf" "bus_dmamap_callback2_t *callback" "void *callback_arg" \
147 .Fn bus_dmamap_load_mbuf_sg "bus_dma_tag_t dmat" "bus_dmamap_t map" \
148 "struct mbuf *mbuf" "bus_dma_segment_t *segs" "int *nsegs" "int flags"
150 .Fn bus_dmamap_load_uio "bus_dma_tag_t dmat" "bus_dmamap_t map" \
151 "struct uio *uio" "bus_dmamap_callback2_t *callback" "void *callback_arg" \
154 .Fn bus_dmamap_unload "bus_dma_tag_t dmat" "bus_dmamap_t map"
156 .Fn bus_dmamap_sync "bus_dma_tag_t dmat" "bus_dmamap_t map" \
159 .Fn bus_dmamem_alloc "bus_dma_tag_t dmat" "void **vaddr" \
160 "int flags" "bus_dmamap_t *mapp"
162 .Fn bus_dmamem_free "bus_dma_tag_t dmat" "void *vaddr" \
165 Direct Memory Access (DMA) is a method of transferring data
166 without involving the CPU, thus providing higher performance.
167 A DMA transaction can be achieved between device to memory,
168 device to device, or memory to memory.
172 API is a bus, device, and machine-independent (MI) interface to
174 It provides the client with flexibility and simplicity by
175 abstracting machine dependent issues like setting up
176 DMA mappings, handling cache issues, bus specific features
180 .Vt ( bus_dma_tag_t )
181 is used to describe the properties of a group of related DMA
183 One way to view this is that a tag describes the limitations of a DMA engine.
184 For example, if a DMA engine in a device is limited to 32-bit addresses,
185 that limitation is specified by a parameter when creating the tag
187 Similarly, a tag can be marked as requiring buffers whose addresses are
188 aligned to a specific boundary.
190 Some devices may require multiple tags to describe DMA
191 transactions with differing properties.
192 For example, a device might require 16-byte alignment of its descriptor ring
193 while permitting arbitrary alignment of I/O buffers.
195 the driver must create one tag for the descriptor ring and a separate tag for
197 If a device has restrictions that are common to all DMA transactions
198 in addition to restrictions that differ between unrelated groups of
200 the driver can first create a
202 tag that decribes the common restrictions.
203 The per-group tags can then inherit these restrictions from this
205 tag rather than having to list them explicitly when creating the per-group tags.
209 represents a mapping of a memory region for DMA.
210 On systems with I/O MMUs,
211 the mapping structure tracks any I/O MMU entries used by a request.
212 For DMA requests that require bounce pages,
213 the mapping tracks the bounce pages used.
215 To prepare for one or more DMA transactions,
216 a mapping must be bound to a memory region by calling one of the
219 These functions configure the mapping which can include programming entries
220 in an I/O MMU and/or allocating bounce pages.
221 An output of these functions
222 (either directly or indirectly by invoking a callback routine)
223 is the list of scatter/gather address ranges a consumer can pass to a DMA
224 engine to access the memory region.
225 When a mapping is no longer needed,
226 the mapping must be unloaded via
227 .Fn bus_dmamap_unload .
229 Before and after each DMA transaction,
231 must be used to ensure that the correct data is used by the DMA engine and
233 If a mapping uses bounce pages,
234 the sync operations copy data between the bounce pages and the memory region
235 bound to the mapping.
236 Sync operations also handle architecture-specific details such as CPU cache
237 flushing and CPU memory operation ordering.
238 .Sh STATIC VS DYNAMIC
240 handles two types of DMA transactions: static and dynamic.
241 Static transactions are used with a long-lived memory region that is reused
242 for many transactions such as a descriptor ring.
243 Dynamic transactions are used for transfers to or from transient buffers
244 such as I/O buffers holding a network packet or disk block.
245 Each transaction type uses a different subset of the
248 .Ss Static Transactions
249 Static transactions use memory regions allocated by
251 Each static memory region is allocated by calling
252 .Fn bus_dmamem_alloc .
253 This function requires a valid tag describing the properties of the
254 DMA transactions to this region such as alignment or address restrictions.
255 Multiple regions can share a single tag if they share the same restrictions.
258 allocates a memory region along with a mapping object.
259 The associated tag, memory region, and mapping object must then be passed to
261 to bind the mapping to the allocated region and obtain the
266 will attempt to allocate memory requiring less expensive sync operations
267 (for example, implementations should not allocate regions requiring bounce
269 but sync operations should still be used.
270 For example, a driver should use
272 in an interrupt handler before reading descriptor ring entries written by the
273 device prior to the interrupt.
275 When a consumer is finished with a memory region,
276 it should unload the mapping via
277 .Fn bus_dmamap_unload
278 and then release the memory region and mapping object via
279 .Fn bus_dmamem_free .
280 .Ss Dynamic Transactions
281 Dynamic transactions map memory regions provided by other parts of the system.
282 A tag must be created via
283 .Fn bus_dma_tag_create
284 to describe the DMA transactions to and from these memory regions,
285 and a pool of mapping objects must be allocated via
286 .Fn bus_dmamap_create
287 to track the mappings of any in-flight transactions.
289 When a consumer wishes to schedule a transaction for a memory region,
290 the consumer must first obtain an unused mapping object from its pool
292 The memory region must be bound to the mapping object via one of the
295 Before scheduling the transaction,
296 the consumer should sync the memory region via
298 with one or more of the
301 After the transaction has completed,
302 the consumer should sync the memory region via
304 with one or more of the
307 The mapping can then be unloaded via
308 .Fn bus_dmamap_unload ,
309 and the mapping object can be returned to the pool of unused mapping objects.
311 When a consumer is no longer scheduling DMA transactions,
312 the mapping objects should be freed via
313 .Fn bus_dmamap_destroy ,
314 and the tag should be freed via
315 .Fn bus_dma_tag_destroy .
316 .Sh STRUCTURES AND TYPES
317 .Bl -tag -width indent
319 A machine-dependent (MD) opaque type that describes the
320 characteristics of a group of DMA transactions.
321 DMA tags are organized into a hierarchy, with each child
322 tag inheriting the restrictions of its parent.
323 This allows all devices along the path of DMA transactions
324 to contribute to the constraints of those transactions.
325 .It Vt bus_dma_template_t
326 A template is a structure for creating a
328 from a set of defaults.
329 Once initialized with
330 .Fn bus_dma_template_init ,
331 a driver can over-ride individual fields to suit its needs.
332 The following fields start with the indicated default values:
336 lowaddr BUS_SPACE_MAXADDR
337 highaddr BUS_SPACE_MAXADDR
338 maxsize BUS_SPACE_MAXSIZE
339 nsegments BUS_SPACE_UNRESTRICTED
340 maxsegsize BUS_SPACE_MAXSIZE
346 Descriptions of each field are documented with
347 .Fn bus_dma_tag_create .
352 attributes of the DMA tag are not supported with templates.
353 .It Vt bus_dma_filter_t
354 Client specified address filter having the format:
355 .Bl -tag -width indent
357 .Fn "client_filter" "void *filtarg" "bus_addr_t testaddr"
360 Address filters can be specified during tag creation to allow
361 for devices whose DMA address restrictions cannot be specified
365 argument is specified by the client during tag creation to be passed to all
366 invocations of the callback.
369 argument contains a potential starting address of a DMA mapping.
370 The filter function operates on the set of addresses from
373 .Ql trunc_page(testaddr) + PAGE_SIZE - 1 ,
375 The filter function should return zero if any mapping in this range
376 can be accommodated by the device and non-zero otherwise.
378 .Em Note: The use of filters is deprecated. Proper operation is not guaranteed.
379 .It Vt bus_dma_segment_t
380 A machine-dependent type that describes individual
382 It contains the following fields:
390 field contains the device visible address of the DMA segment, and
392 contains the length of the DMA segment.
393 Although the DMA segments returned by a mapping call will adhere to
394 all restrictions necessary for a successful DMA operation, some conversion
395 (e.g.\& a conversion from host byte order to the device's byte order) is
396 almost always required when presenting segment information to the device.
398 A machine-dependent opaque type describing an individual mapping.
399 One map is used for each memory allocation that will be loaded.
400 Maps can be reused once they have been unloaded.
401 Multiple maps can be associated with one DMA tag.
402 While the value of the map may evaluate to
404 on some platforms under certain conditions,
405 it should never be assumed that it will be
408 .It Vt bus_dmamap_callback_t
409 Client specified callback for receiving mapping information resulting from
413 .Fn bus_dmamap_load ,
414 .Fn bus_dmamap_load_bio ,
415 .Fn bus_dmamap_load_ccb ,
416 .Fn bus_dmamap_load_crp ,
418 .Fn bus_dmamap_load_crp_buffer .
419 Callbacks are of the format:
420 .Bl -tag -width indent
422 .Fn "client_callback" "void *callback_arg" "bus_dma_segment_t *segs" \
423 "int nseg" "int error"
428 is the callback argument passed to dmamap load functions.
433 arguments describe an array of
434 .Vt bus_dma_segment_t
435 structures that represent the mapping.
436 This array is only valid within the scope of the callback function.
437 The success or failure of the mapping is indicated by the
440 More information on the use of callbacks can be found in the
441 description of the individual dmamap load functions.
442 .It Vt bus_dmamap_callback2_t
443 Client specified callback for receiving mapping information resulting from
447 .Fn bus_dmamap_load_uio
449 .Fn bus_dmamap_load_mbuf .
451 Callback2s are of the format:
452 .Bl -tag -width indent
454 .Fn "client_callback2" "void *callback_arg" "bus_dma_segment_t *segs" \
455 "int nseg" "bus_size_t mapsize" "int error"
458 Callback2's behavior is the same as
459 .Vt bus_dmamap_callback_t
460 with the addition that the length of the data mapped is provided via
462 .It Vt bus_dmasync_op_t
463 Memory synchronization operation specifier.
464 Bus DMA requires explicit synchronization of memory with its device
465 visible mapping in order to guarantee memory coherency.
468 allows the type of DMA operation that will be or has been performed
469 to be communicated to the system so that the correct coherency measures
471 The operations are represented as bitfield flags that can be combined together,
472 though it only makes sense to combine PRE flags or POST flags, not both.
475 description below for more details on how to use these operations.
477 All operations specified below are performed from the host memory point of view,
478 where a read implies data coming from the device to the host memory, and a write
479 implies data going from the host memory to the device.
480 Alternatively, the operations can be thought of in terms of driver operations,
481 where reading a network packet or storage sector corresponds to a read operation
484 .Bl -tag -width ".Dv BUS_DMASYNC_POSTWRITE"
485 .It Dv BUS_DMASYNC_PREREAD
486 Perform any synchronization required prior to an update of host memory by the
488 .It Dv BUS_DMASYNC_PREWRITE
489 Perform any synchronization required after an update of host memory by the CPU
490 and prior to device access to host memory.
491 .It Dv BUS_DMASYNC_POSTREAD
492 Perform any synchronization required after an update of host memory by the
493 device and prior to CPU access to host memory.
494 .It Dv BUS_DMASYNC_POSTWRITE
495 Perform any synchronization required after device access to host memory.
497 .It Vt bus_dma_lock_t
498 Client specified lock/mutex manipulation method.
499 This will be called from
500 within busdma whenever a client lock needs to be manipulated.
501 In its current form, the function will be called immediately before
502 the callback for a DMA load operation that has been deferred with
504 and immediately after with
506 If the load operation does not need to be deferred, then it
507 will not be called since the function loading the map should
508 be holding the appropriate locks.
509 This method is of the format:
510 .Bl -tag -width indent
512 .Fn "lockfunc" "void *lockfunc_arg" "bus_dma_lock_op_t op"
517 argument is specified by the client during tag creation to be passed to all
518 invocations of the callback.
521 argument specifies the lock operation to perform.
525 implementations are provided for convenience.
526 .Fn busdma_lock_mutex
527 performs standard mutex operations on the sleep mutex provided via
530 will generate a system panic if it is called.
531 It is substituted into the tag when
536 .Fn bus_dma_tag_create
537 and is useful for tags that should not be used with deferred load operations.
538 .It Vt bus_dma_lock_op_t
539 Operations to be performed by the client-specified
541 .Bl -tag -width ".Dv BUS_DMA_UNLOCK"
543 Acquires and/or locks the client locking primitive.
544 .It Dv BUS_DMA_UNLOCK
545 Releases and/or unlocks the client locking primitive.
549 .Bl -tag -width indent
550 .It Fn bus_dma_tag_create "parent" "alignment" "boundary" "lowaddr" \
551 "highaddr" "*filtfunc" "*filtfuncarg" "maxsize" "nsegments" "maxsegsz" \
552 "flags" "lockfunc" "lockfuncarg" "*dmat"
553 Allocates a DMA tag, and initializes it according to
554 the arguments provided:
555 .Bl -tag -width ".Fa filtfuncarg"
557 A parent tag from which to inherit restrictions.
558 The restrictions passed in other arguments can only further tighten the
559 restrictions inherited from the parent tag.
561 All tags created by a device driver must inherit from the tag returned by
563 to honor restrictions between the parent bridge, CPU memory, and the
566 Alignment constraint, in bytes, of any mappings created using this tag.
567 The alignment must be a power of 2.
568 Hardware that can DMA starting at any address would specify
571 Hardware requiring DMA transfers to start on a multiple of 4K
575 Boundary constraint, in bytes, of the target DMA memory region.
576 The boundary indicates the set of addresses, all multiples of the
577 boundary argument, that cannot be crossed by a single
578 .Vt bus_dma_segment_t .
579 The boundary must be a power of 2 and must be no smaller than the
580 maximum segment size.
582 indicates that there are no boundary restrictions.
583 .It Fa lowaddr , highaddr
584 Bounds of the window of bus address space that
586 be directly accessed by the device.
587 The window contains all addresses greater than
589 and less than or equal to
591 For example, a device incapable of DMA above 4GB, would specify a
594 .Dv BUS_SPACE_MAXADDR
598 .Dv BUS_SPACE_MAXADDR_32BIT .
599 Similarly a device that can only perform DMA to addresses below
603 .Dv BUS_SPACE_MAXADDR
607 .Dv BUS_SPACE_MAXADDR_24BIT .
608 Some implementations require that some region of device visible
609 address space, overlapping available host memory, be outside the
613 is used to bounce requests that would otherwise conflict with
614 the exclusion window.
616 Optional filter function (may be
618 to be called for any attempt to
619 map memory into the window described by
623 A filter function is only required when the single window described
628 cannot adequately describe the constraints of the device.
629 The filter function will be called for every machine page
630 that overlaps the exclusion window.
632 .Em Note: The use of filters is deprecated. Proper operation is not guaranteed.
634 Argument passed to all calls to the filter function for this tag.
638 Maximum size, in bytes, of the sum of all segment lengths in a given
639 DMA mapping associated with this tag.
641 Number of discontinuities (scatter/gather segments) allowed
642 in a DMA mapped region.
644 Maximum size, in bytes, of a segment in any DMA mapped region associated
649 .Bl -tag -width ".Dv BUS_DMA_ALLOCNOW"
650 .It Dv BUS_DMA_ALLOCNOW
651 Pre-allocate enough resources to handle at least one map load operation on
653 If sufficient resources are not available,
656 This should not be used for tags that only describe buffers that will be
658 .Fn bus_dmamem_alloc .
659 Also, due to resource sharing with other tags, this flag does not guarantee
660 that resources will be allocated or reserved exclusively for this tag.
661 It should be treated only as a minor optimization.
662 .It Dv BUS_DMA_COHERENT
663 Indicate that the DMA engine and CPU are cache-coherent.
664 Cached memory may be used to back allocations created by
665 .Fn bus_dmamem_alloc .
667 .Fn bus_dma_tag_create ,
670 flag is currently implemented on arm64.
673 Optional lock manipulation function (may be
675 to be called when busdma
676 needs to manipulate a lock on behalf of the client.
683 Optional argument to be passed to the function specified by
686 Pointer to a bus_dma_tag_t where the resulting DMA tag will
692 if sufficient memory is not available for tag creation
693 or allocating mapping resources.
694 .It Fn bus_dma_tag_destroy "dmat"
695 Deallocate the DMA tag
698 .Fn bus_dma_tag_create .
702 if any DMA maps remain associated with
707 .It Fn bus_dma_template_init "*template" "parent"
709 .Fa bus_dma_template_t
713 argument is non-NULL, this parent tag is associated with the template and
714 will be compiled into the dma tag that is later created.
715 The values of the parent are not copied into the template.
716 During tag creation in
717 .Fn bus_dma_tag_template ,
718 any parameters from the parent tag that are more restrictive than what is
719 in the provided template will overwrite what goes into the new tag.
720 .It Fn bus_dma_template_tag "*template" "*dmat"
721 Unpacks a template into a tag, and returns the tag via the
723 All return values are identical to
724 .Fn bus_dma_tag_create .
725 The template is not modified by this function, and can be reused and/or
727 .It Fn bus_dma_template_clone "*template" "dmat"
728 Copies the fields from an existing tag to a template.
729 The template does not need to be initialized first.
730 All of its fields will be overwritten by the values contained in the tag.
732 .Fn bus_dma_template_tag ,
733 this function is useful for creating copies of tags.
734 .It Fn bus_dma_template_fill "*template" "params[]" "count"
735 Fills in the selected fields of the template with the keyed values from the
738 This is not meant to be called directly, use
739 .Fn BUS_DMA_TEMPLATE_FILL
741 .It Fn BUS_DMA_TEMPLATE_FILL "*template" "param ..."
742 Fills in the selected fields of the template with a variable number of
743 key-value parameters.
744 The macros listed below take an argument of the specified type and encapsulate
745 it into a key-value structure that is directly usable as a parameter argument.
746 Muliple parameters may be provided at once.
749 BD_ALIGNMENT() uintmax_t
750 BD_BOUNDARY() uintmax_t
751 BD_LOWADDR() vm_paddr_t
752 BD_HIGHADDR() vm_paddr_t
753 BD_MAXSIZE() uintmax_t
754 BD_NSEGMENTS() uintmax_t
755 BD_MAXSEGSIZE() uintmax_t
758 BD_LOCKFUNCARG() void *
760 .It Fn bus_dmamap_create "dmat" "flags" "*mapp"
761 Allocates and initializes a DMA map.
762 Arguments are as follows:
763 .Bl -tag -width ".Fa nsegments"
768 .Bl -tag -width ".Dv BUS_DMA_COHERENT"
769 .It Dv BUS_DMA_COHERENT
770 Attempt to map the memory loaded with this map such that cache sync
771 operations are as cheap as possible.
772 This flag is typically set on maps when the memory loaded with these will
773 be accessed by both a CPU and a DMA engine, frequently such as control data
774 and as opposed to streamable data such as receive and transmit buffers.
775 Use of this flag does not remove the requirement of using
776 .Fn bus_dmamap_sync ,
777 but it may reduce the cost of performing these operations.
782 where the resulting DMA map will be stored.
787 if sufficient memory is not available for creating the
788 map or allocating mapping resources.
789 .It Fn bus_dmamap_destroy "dmat" "map"
790 Frees all resources associated with a given DMA map.
791 Arguments are as follows:
792 .Bl -tag -width ".Fa dmat"
794 DMA tag used to allocate
797 The DMA map to destroy.
802 if a mapping is still active for
804 .It Fn bus_dmamap_load "dmat" "map" "buf" "buflen" "*callback" \
805 "callback_arg" "flags"
806 Creates a mapping in device visible address space of
810 associated with the DMA map
812 This call will always return immediately and will not block for any reason.
813 Arguments are as follows:
814 .Bl -tag -width ".Fa buflen"
816 DMA tag used to allocate
819 A DMA map without a currently active mapping.
821 A kernel virtual address pointer to a contiguous (in KVA) buffer, to be
822 mapped into device visible address space.
824 The size of the buffer.
825 .It Fa callback Fa callback_arg
826 The callback function, and its argument.
827 This function is called once sufficient mapping resources are available for
829 If resources are temporarily unavailable, this function will be deferred until
830 later, but the load operation will still return immediately to the caller.
831 Thus, callers should not assume that the callback will be called before the
832 load returns, and code should be structured appropriately to handle this.
833 See below for specific flags and error codes that control this behavior.
836 .Bl -tag -width ".Dv BUS_DMA_NOWAIT"
837 .It Dv BUS_DMA_NOWAIT
838 The load should not be deferred in case of insufficient mapping resources,
839 and instead should return immediately with an appropriate error.
840 .It Dv BUS_DMA_NOCACHE
841 The generated transactions to and from the virtual page are non-cacheable.
845 Return values to the caller are as follows:
846 .Bl -tag -width ".Er EINPROGRESS"
848 The callback has been called and completed.
849 The status of the mapping has been delivered to the callback.
851 The mapping has been deferred for lack of resources.
852 The callback will be called as soon as resources are available.
853 Callbacks are serviced in FIFO order.
855 Note that subsequent load operations for the same tag that do not require
856 extra resources will still succeed.
857 This may result in out-of-order processing of requests.
858 If the caller requires the order of requests to be preserved,
859 then the caller is required to stall subsequent requests until a pending
860 request's callback is invoked.
862 The load request has failed due to insufficient resources, and the caller
863 specifically used the
867 The load request was invalid.
868 The callback has been called and has been provided the same error.
869 This error value may indicate that
879 argument used to create the dma tag
883 When the callback is called, it is presented with an error value
884 indicating the disposition of the mapping.
885 Error may be one of the following:
886 .Bl -tag -width ".Er EINPROGRESS"
888 The mapping was successful and the
890 callback argument contains an array of
891 .Vt bus_dma_segment_t
892 elements describing the mapping.
893 This array is only valid during the scope of the callback function.
895 A mapping could not be achieved within the segment constraints provided
896 in the tag even though the requested allocation size was less than maxsize.
898 .It Fn bus_dmamap_load_bio "dmat" "map" "bio" "callback" "callback_arg" "flags"
899 This is a variation of
901 which maps buffers pointed to by
905 may point to either a mapped or unmapped buffer.
906 .It Fn bus_dmamap_load_ccb "dmat" "map" "ccb" "callback" "callback_arg" "flags"
907 This is a variation of
909 which maps data pointed to by
914 may be any of the following types:
915 .Bl -tag -width ".Er CAM_DATA_SG_PADDR"
917 The data is a single KVA buffer.
919 The data is a single bus address range.
921 The data is a scatter/gather list of KVA buffers.
922 .It CAM_DATA_SG_PADDR
923 The data is a scatter/gather list of bus address ranges.
925 The data is contained in a
930 .Fn bus_dmamap_load_ccb
931 supports the following CCB XPT function codes:
933 .Bl -item -offset indent -compact
941 .It Fn bus_dmamap_load_crp "dmat" "map" "crp" "callback" "callback_arg" "flags"
942 This is a variation of
944 which maps the input buffer pointed to by
949 flag is implied, thus no callback deferral will happen.
950 .It Fn bus_dmamap_load_crp_buffer "dmat" "map" "cb" "callback" "callback_arg" \
952 This is a variation of
954 which maps the crypto data buffer pointed to by
959 flag is implied, thus no callback deferral will happen.
960 .It Fn bus_dmamap_load_mbuf "dmat" "map" "mbuf" "callback2" "callback_arg" \
962 This is a variation of
964 which maps mbuf chains
968 argument is also passed to the callback routine, which
969 contains the mbuf chain's packet header length.
972 flag is implied, thus no callback deferral will happen.
974 Mbuf chains are assumed to be in kernel virtual address space.
976 Beside the error values listed for
977 .Fn bus_dmamap_load ,
979 will be returned if the size of the mbuf chain exceeds the maximum limit of the
981 .It Fn bus_dmamap_load_mbuf_sg "dmat" "map" "mbuf" "segs" "nsegs" "flags"
983 .Fn bus_dmamap_load_mbuf
984 except that it returns immediately without calling a callback function.
985 It is provided for efficiency.
986 The scatter/gather segment array
988 is provided by the caller and filled in directly by the function.
991 argument is returned with the number of segments filled in.
992 Returns the same errors as
993 .Fn bus_dmamap_load_mbuf .
994 .It Fn bus_dmamap_load_uio "dmat" "map" "uio" "callback2" "callback_arg" "flags"
995 This is a variation of
997 which maps buffers pointed to by
1002 argument is also passed to the callback routine, which contains the size of
1005 .Fa uio->uio_resid .
1008 flag is implied, thus no callback deferral will happen.
1009 Returns the same errors as
1010 .Fn bus_dmamap_load .
1016 then it is assumed that the buffer,
1019 .Fa "uio->uio_td->td_proc" Ns 's
1021 User space memory must be in-core and wired prior to attempting a map
1023 Pages may be locked using
1025 .It Fn bus_dmamap_unload "dmat" "map"
1027 Arguments are as follows:
1028 .Bl -tag -width ".Fa dmam"
1030 DMA tag used to allocate
1033 The DMA map that is to be unloaded.
1036 .Fn bus_dmamap_unload
1037 will not perform any implicit synchronization of DMA buffers.
1038 This must be done explicitly by a call to
1040 prior to unloading the map.
1041 .It Fn bus_dmamap_sync "dmat" "map" "op"
1042 Performs synchronization of a device visible mapping with the CPU visible
1043 memory referenced by that mapping.
1044 Arguments are as follows:
1045 .Bl -tag -width ".Fa dmat"
1047 DMA tag used to allocate
1050 The DMA mapping to be synchronized.
1052 Type of synchronization operation to perform.
1053 See the definition of
1054 .Vt bus_dmasync_op_t
1055 for a description of the acceptable values for
1062 is the method used to ensure that CPU's and device's direct
1063 memory access (DMA) to shared
1065 For example, the CPU might be used to set up the contents of a buffer
1066 that is to be made available to a device.
1067 To ensure that the data are visible via the device's mapping of that
1068 memory, the buffer must be loaded and a DMA sync operation of
1069 .Dv BUS_DMASYNC_PREWRITE
1070 must be performed after the CPU has updated the buffer and before the device
1071 access is initiated.
1072 If the CPU modifies this buffer again later, another
1073 .Dv BUS_DMASYNC_PREWRITE
1074 sync operation must be performed before an additional device
1076 Conversely, suppose a device updates memory that is to be read by a CPU.
1077 In this case, the buffer must be loaded, and a DMA sync operation of
1078 .Dv BUS_DMASYNC_PREREAD
1079 must be performed before the device access is initiated.
1080 The CPU will only be able to see the results of this memory update
1081 once the DMA operation has completed and a
1082 .Dv BUS_DMASYNC_POSTREAD
1083 sync operation has been performed.
1085 If read and write operations are not preceded and followed by the
1086 appropriate synchronization operations, behavior is undefined.
1087 .It Fn bus_dmamem_alloc "dmat" "**vaddr" "flags" "*mapp"
1088 Allocates memory that is mapped into KVA at the address returned
1091 and that is permanently loaded into the newly created
1095 Arguments are as follows:
1096 .Bl -tag -width ".Fa alignment"
1098 DMA tag describing the constraints of the DMA mapping.
1100 Pointer to a pointer that will hold the returned KVA mapping of
1101 the allocated region.
1103 Flags are defined as follows:
1104 .Bl -tag -width ".Dv BUS_DMA_NOWAIT"
1105 .It Dv BUS_DMA_WAITOK
1106 The routine can safely wait (sleep) for resources.
1107 .It Dv BUS_DMA_NOWAIT
1108 The routine is not allowed to wait for resources.
1109 If resources are not available,
1112 .It Dv BUS_DMA_COHERENT
1113 Attempt to map this memory in a coherent fashion.
1115 .Fn bus_dmamap_create
1116 above for a description of this flag.
1118 .Fn bus_dmamem_alloc ,
1120 .Dv BUS_DMA_COHERENT
1121 flag is currently implemented on arm and arm64.
1123 Causes the allocated memory to be set to all zeros.
1124 .It Dv BUS_DMA_NOCACHE
1125 The allocated memory will not be cached in the processor caches.
1126 All memory accesses appear on the bus and are executed
1129 .Fn bus_dmamem_alloc ,
1132 flag is currently implemented on amd64 and i386 where it results in the
1133 Strong Uncacheable PAT to be set for the allocated virtual address range.
1138 where the resulting DMA map will be stored.
1141 The size of memory to be allocated is
1143 as specified in the call to
1144 .Fn bus_dma_tag_create
1148 The current implementation of
1149 .Fn bus_dmamem_alloc
1150 will allocate all requests as a single segment.
1152 An initial load operation is required to obtain the bus address of the allocated
1153 memory, and an unload operation is required before freeing the memory, as
1155 .Fn bus_dmamem_free .
1156 Maps are automatically handled by this function and should not be explicitly
1157 allocated or destroyed.
1159 Although an explicit load is not required for each access to the memory
1160 referenced by the returned map, the synchronization requirements
1163 section still apply and should be used to achieve portability on architectures
1164 without coherent buses.
1168 if sufficient memory is not available for completing
1170 .It Fn bus_dmamem_free "dmat" "*vaddr" "map"
1171 Frees memory previously allocated by
1172 .Fn bus_dmamem_alloc .
1174 will be invalidated.
1175 Arguments are as follows:
1176 .Bl -tag -width ".Fa vaddr"
1180 Kernel virtual address of the memory.
1182 DMA map to be invalidated.
1186 Behavior is undefined if invalid arguments are passed to
1187 any of the above functions.
1188 If sufficient resources cannot be allocated for a given
1193 routines that are not of type
1195 will return 0 on success or an error
1196 code on failure as discussed above.
1200 routines will succeed if provided with valid arguments.
1202 Two locking protocols are used by
1204 The first is a private global lock that is used to synchronize access to the
1205 bounce buffer pool on the architectures that make use of them.
1206 This lock is strictly a leaf lock that is only used internally to
1208 and is not exposed to clients of the API.
1210 The second protocol involves protecting various resources stored in the tag.
1213 operations are done through requests from the driver that created the tag,
1214 the most efficient way to protect the tag resources is through the lock that
1218 acts on its own without being called by the driver, the lock primitive
1219 specified in the tag is acquired and released automatically.
1220 An example of this is when the
1222 callback function is called from a deferred context instead of the driver
1224 This means that certain
1226 functions must always be called with the same lock held that is specified in the
1228 These functions include:
1230 .Bl -item -offset indent -compact
1234 .Fn bus_dmamap_load_bio
1236 .Fn bus_dmamap_load_ccb
1238 .Fn bus_dmamap_load_mbuf
1240 .Fn bus_dmamap_load_mbuf_sg
1242 .Fn bus_dmamap_load_uio
1244 .Fn bus_dmamap_unload
1249 There is one exception to this rule.
1250 It is common practice to call some of these functions during driver start-up
1251 without any locks held.
1252 So long as there is a guarantee of no possible concurrent use of the tag by
1253 different threads during this operation, it is safe to not hold a lock for
1258 operations should not be called with the driver lock held, either because
1259 they are already protected by an internal lock, or because they might sleep
1260 due to memory or resource allocation.
1261 The following functions must not be
1262 called with any non-sleepable locks held:
1264 .Bl -item -offset indent -compact
1266 .Fn bus_dma_tag_create
1268 .Fn bus_dmamap_create
1270 .Fn bus_dmamem_alloc
1273 All other functions do not have a locking protocol and can thus be
1274 called with or without any system or driver locks held.
1283 .%A "Jason R. Thorpe"
1284 .%T "A Machine-Independent DMA Framework for NetBSD"
1285 .%J "Proceedings of the Summer 1998 USENIX Technical Conference"
1286 .%Q "USENIX Association"
1292 interface first appeared in
1297 API was adopted from
1299 for use in the CAM SCSI subsystem.
1300 The alterations to the original API were aimed to remove the need for
1302 .Vt bus_dma_segment_t
1303 array stored in each
1305 while allowing callers to queue up on scarce resources.
1309 interface was designed and implemented by
1311 of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
1312 Additional input on the
1314 design was provided by
1316 .An Chris Demetriou ,
1317 .An Charles Hannum ,
1320 .An Jonathan Stone ,
1328 benefits from the contributions of
1329 .An Justin T. Gibbs ,
1332 .An Matthew N. Dodd ,
1334 .An Maxime Henrion ,
1335 .An Jake Burkholder ,
1336 .An Takahashi Yoshihiro ,
1340 This manual page was written by
1343 .An Justin T. Gibbs .