]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/bus_dma.9
bhnd_erom(9): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / share / man / man9 / bus_dma.9
1 .\" Copyright (c) 2002, 2003 Hiten M. Pandya.
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
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.
12 .\"
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.
24 .\"
25 .\" Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
26 .\" All rights reserved.
27 .\"
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.
31 .\"
32 .\" Redistribution and use in source and binary forms, with or without
33 .\" modification, are permitted provided that the following conditions
34 .\" are met:
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 .\"
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.
52 .\"
53 .\" $FreeBSD$
54 .\" $NetBSD: bus_dma.9,v 1.25 2002/10/14 13:43:16 wiz Exp $
55 .\"
56 .Dd May 25, 2020
57 .Dt BUS_DMA 9
58 .Os
59 .Sh NAME
60 .Nm bus_dma ,
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 ,
70 .Nm bus_dmamap_load ,
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 ,
79 .Nm bus_dmamap_sync ,
80 .Nm bus_dmamem_alloc ,
81 .Nm bus_dmamem_free
82 .Nd Bus and Machine Independent DMA Mapping Interface
83 .Sh SYNOPSIS
84 .In machine/bus.h
85 .Ft int
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"
91 .Ft int
92 .Fn bus_dma_tag_destroy "bus_dma_tag_t dmat"
93 .Ft void
94 .Fo bus_dma_template_init
95 .Fa "bus_dma_template_t *template"
96 .Fa "bus_dma_tag_t parent"
97 .Fc
98 .Ft int
99 .Fo bus_dma_template_tag
100 .Fa "bus_dma_template_t *template"
101 .Fa "bus_dma_tag_t *dmat"
102 .Fc
103 .Ft void
104 .Fo bus_dma_template_clone
105 .Fa "bus_dma_template_t *template"
106 .Fa "bus_dma_tag_t dmat"
107 .Fc
108 .Ft void
109 .Fo bus_dma_template_fill
110 .Fa "bus_dma_template_t *template"
111 .Fa "bus_dma_param_t params[]"
112 .Fa "u_int count"
113 .Fc
114 .Fo BUS_DMA_TEMPLATE_FILL
115 .Fa "bus_dma_template_t *template"
116 .Fa "bus_dma_param_t param ..."
117 .Fc
118 .Ft int
119 .Fn bus_dmamap_create "bus_dma_tag_t dmat" "int flags" "bus_dmamap_t *mapp"
120 .Ft int
121 .Fn bus_dmamap_destroy "bus_dma_tag_t dmat" "bus_dmamap_t map"
122 .Ft int
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" \
125 "int flags"
126 .Ft int
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" \
129 "int flags"
130 .Ft int
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" \
133 "int flags"
134 .Ft int
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" \
137 "int flags"
138 .Ft int
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"
142 .Ft int
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" \
145 "int flags"
146 .Ft int
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"
149 .Ft int
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" \
152 "int flags"
153 .Ft void
154 .Fn bus_dmamap_unload "bus_dma_tag_t dmat" "bus_dmamap_t map"
155 .Ft void
156 .Fn bus_dmamap_sync "bus_dma_tag_t dmat" "bus_dmamap_t map" \
157 "op"
158 .Ft int
159 .Fn bus_dmamem_alloc "bus_dma_tag_t dmat" "void **vaddr" \
160 "int flags" "bus_dmamap_t *mapp"
161 .Ft void
162 .Fn bus_dmamem_free "bus_dma_tag_t dmat" "void *vaddr" \
163 "bus_dmamap_t map"
164 .Sh DESCRIPTION
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.
169 .Pp
170 The
171 .Nm
172 API is a bus, device, and machine-independent (MI) interface to
173 DMA mechanisms.
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
177 and limitations.
178 .Sh OVERVIEW
179 A tag structure
180 .Vt ( bus_dma_tag_t )
181 is used to describe the properties of a group of related DMA
182 transactions.
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
186 for that device.
187 Similarly, a tag can be marked as requiring buffers whose addresses are
188 aligned to a specific boundary.
189 .Pp
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.
194 In this case,
195 the driver must create one tag for the descriptor ring and a separate tag for
196 I/O buffers.
197 If a device has restrictions that are common to all DMA transactions
198 in addition to restrictions that differ between unrelated groups of
199 transactions,
200 the driver can first create a
201 .Dq parent
202 tag that decribes the common restrictions.
203 The per-group tags can then inherit these restrictions from this
204 .Dq parent
205 tag rather than having to list them explicitly when creating the per-group tags.
206 .Pp
207 A mapping structure
208 .Vt ( bus_dmamap_t )
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.
214 .Pp
215 To prepare for one or more DMA transactions,
216 a mapping must be bound to a memory region by calling one of the
217 .Fn bus_dmamap_load
218 functions.
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 .
228 .Pp
229 Before and after each DMA transaction,
230 .Fn bus_dmamap_sync
231 must be used to ensure that the correct data is used by the DMA engine and
232 the CPU.
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
239 .Nm
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
246 .Nm
247 API.
248 .Ss Static Transactions
249 Static transactions use memory regions allocated by
250 .Nm .
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.
256 .Pp
257 .Fn bus_dmamem_alloc
258 allocates a memory region along with a mapping object.
259 The associated tag, memory region, and mapping object must then be passed to
260 .Fn bus_dmamap_load
261 to bind the mapping to the allocated region and obtain the
262 scatter/gather list.
263 .Pp
264 It is expected that
265 .Fn bus_dmamem_alloc
266 will attempt to allocate memory requiring less expensive sync operations
267 (for example, implementations should not allocate regions requiring bounce
268 pages),
269 but sync operations should still be used.
270 For example, a driver should use
271 .Fn bus_dmamap_sync
272 in an interrupt handler before reading descriptor ring entries written by the
273 device prior to the interrupt.
274 .Pp
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.
288 .Pp
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
291 of mapping objects.
292 The memory region must be bound to the mapping object via one of the
293 .Fn bus_dmamap_load
294 functions.
295 Before scheduling the transaction,
296 the consumer should sync the memory region via
297 .Fn bus_dmamap_sync
298 with one or more of the
299 .Dq PRE
300 flags.
301 After the transaction has completed,
302 the consumer should sync the memory region via
303 .Fn bus_dmamap_sync
304 with one or more of the
305 .Dq POST
306 flags.
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.
310 .Pp
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
318 .It Vt bus_dma_tag_t
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
327 .Fa bus_dma_tag_t
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:
333 .Bd -literal
334         alignment       1
335         boundary        0
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
341         flags           0
342         lockfunc        NULL
343         lockfuncarg     NULL
344 .Ed
345 .Pp
346 Descriptions of each field are documented with
347 .Fn bus_dma_tag_create .
348 Note that the
349 .Fa filtfunc
350 and
351 .Fa filtfuncarg
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
356 .It Ft int
357 .Fn "client_filter" "void *filtarg" "bus_addr_t testaddr"
358 .El
359 .Pp
360 Address filters can be specified during tag creation to allow
361 for devices whose DMA address restrictions cannot be specified
362 by a single window.
363 The
364 .Fa filtarg
365 argument is specified by the client during tag creation to be passed to all
366 invocations of the callback.
367 The
368 .Fa testaddr
369 argument contains a potential starting address of a DMA mapping.
370 The filter function operates on the set of addresses from
371 .Fa testaddr
372 to
373 .Ql trunc_page(testaddr) + PAGE_SIZE - 1 ,
374 inclusive.
375 The filter function should return zero if any mapping in this range
376 can be accommodated by the device and non-zero otherwise.
377 .Pp
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
381 DMA segments.
382 It contains the following fields:
383 .Bd -literal
384         bus_addr_t      ds_addr;
385         bus_size_t      ds_len;
386 .Ed
387 .Pp
388 The
389 .Fa ds_addr
390 field contains the device visible address of the DMA segment, and
391 .Fa ds_len
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.
397 .It Vt bus_dmamap_t
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
403 .Dv NULL
404 on some platforms under certain conditions,
405 it should never be assumed that it will be
406 .Dv NULL
407 in all cases.
408 .It Vt bus_dmamap_callback_t
409 Client specified callback for receiving mapping information resulting from
410 the load of a
411 .Vt bus_dmamap_t
412 via
413 .Fn bus_dmamap_load ,
414 .Fn bus_dmamap_load_bio ,
415 .Fn bus_dmamap_load_ccb ,
416 .Fn bus_dmamap_load_crp ,
417 or
418 .Fn bus_dmamap_load_crp_buffer .
419 Callbacks are of the format:
420 .Bl -tag -width indent
421 .It Ft void
422 .Fn "client_callback" "void *callback_arg" "bus_dma_segment_t *segs" \
423 "int nseg" "int error"
424 .El
425 .Pp
426 The
427 .Fa callback_arg
428 is the callback argument passed to dmamap load functions.
429 The
430 .Fa segs
431 and
432 .Fa nseg
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
438 .Fa error
439 argument.
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
444 the load of a
445 .Vt bus_dmamap_t
446 via
447 .Fn bus_dmamap_load_uio
448 or
449 .Fn bus_dmamap_load_mbuf .
450 .Pp
451 Callback2s are of the format:
452 .Bl -tag -width indent
453 .It Ft void
454 .Fn "client_callback2" "void *callback_arg" "bus_dma_segment_t *segs" \
455 "int nseg" "bus_size_t mapsize" "int error"
456 .El
457 .Pp
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
461 .Fa mapsize .
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.
466 The
467 .Vt bus_dmasync_op_t
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
470 are taken.
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.
473 See the
474 .Fn bus_dmamap_sync
475 description below for more details on how to use these operations.
476 .Pp
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
482 in
483 .Nm .
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
487 device.
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.
496 .El
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
503 .Dv BUS_DMA_LOCK
504 and immediately after with
505 .Dv BUS_DMA_UNLOCK .
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
511 .It Ft void
512 .Fn "lockfunc" "void *lockfunc_arg" "bus_dma_lock_op_t op"
513 .El
514 .Pp
515 The
516 .Fa lockfuncarg
517 argument is specified by the client during tag creation to be passed to all
518 invocations of the callback.
519 The
520 .Fa op
521 argument specifies the lock operation to perform.
522 .Pp
523 Two
524 .Vt lockfunc
525 implementations are provided for convenience.
526 .Fn busdma_lock_mutex
527 performs standard mutex operations on the sleep mutex provided via
528 .Fa lockfuncarg .
529 .Fn dflt_lock
530 will generate a system panic if it is called.
531 It is substituted into the tag when
532 .Fa lockfunc
533 is passed as
534 .Dv NULL
535 to
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
540 .Fn lockfunc .
541 .Bl -tag -width ".Dv BUS_DMA_UNLOCK"
542 .It Dv BUS_DMA_LOCK
543 Acquires and/or locks the client locking primitive.
544 .It Dv BUS_DMA_UNLOCK
545 Releases and/or unlocks the client locking primitive.
546 .El
547 .El
548 .Sh FUNCTIONS
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"
556 .It Fa parent
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.
560 .Pp
561 All tags created by a device driver must inherit from the tag returned by
562 .Fn bus_get_dma_tag
563 to honor restrictions between the parent bridge, CPU memory, and the
564 device.
565 .It Fa alignment
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
569 .Em 1
570 for byte alignment.
571 Hardware requiring DMA transfers to start on a multiple of 4K
572 would specify
573 .Em 4096 .
574 .It Fa boundary
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.
581 .Ql 0
582 indicates that there are no boundary restrictions.
583 .It Fa lowaddr , highaddr
584 Bounds of the window of bus address space that
585 .Em cannot
586 be directly accessed by the device.
587 The window contains all addresses greater than
588 .Fa lowaddr
589 and less than or equal to
590 .Fa highaddr .
591 For example, a device incapable of DMA above 4GB, would specify a
592 .Fa highaddr
593 of
594 .Dv BUS_SPACE_MAXADDR
595 and a
596 .Fa lowaddr
597 of
598 .Dv BUS_SPACE_MAXADDR_32BIT .
599 Similarly a device that can only perform DMA to addresses below
600 16MB would specify a
601 .Fa highaddr
602 of
603 .Dv BUS_SPACE_MAXADDR
604 and a
605 .Fa lowaddr
606 of
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
610 window.
611 This area of
612 .Ql safe memory
613 is used to bounce requests that would otherwise conflict with
614 the exclusion window.
615 .It Fa filtfunc
616 Optional filter function (may be
617 .Dv NULL )
618 to be called for any attempt to
619 map memory into the window described by
620 .Fa lowaddr
621 and
622 .Fa highaddr .
623 A filter function is only required when the single window described
624 by
625 .Fa lowaddr
626 and
627 .Fa highaddr
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.
631 .Pp
632 .Em Note: The use of filters is deprecated.  Proper operation is not guaranteed.
633 .It Fa filtfuncarg
634 Argument passed to all calls to the filter function for this tag.
635 May be
636 .Dv NULL .
637 .It Fa maxsize
638 Maximum size, in bytes, of the sum of all segment lengths in a given
639 DMA mapping associated with this tag.
640 .It Fa nsegments
641 Number of discontinuities (scatter/gather segments) allowed
642 in a DMA mapped region.
643 .It Fa maxsegsz
644 Maximum size, in bytes, of a segment in any DMA mapped region associated
645 with
646 .Fa dmat .
647 .It Fa flags
648 Are as follows:
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
652 this tag.
653 If sufficient resources are not available,
654 .Er ENOMEM
655 is returned.
656 This should not be used for tags that only describe buffers that will be
657 allocated with
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 .
666 For
667 .Fn bus_dma_tag_create ,
668 the
669 .Dv BUS_DMA_COHERENT
670 flag is currently implemented on arm64.
671 .El
672 .It Fa lockfunc
673 Optional lock manipulation function (may be
674 .Dv NULL )
675 to be called when busdma
676 needs to manipulate a lock on behalf of the client.
677 If
678 .Dv NULL
679 is specified,
680 .Fn dflt_lock
681 is used.
682 .It Fa lockfuncarg
683 Optional argument to be passed to the function specified by
684 .Fa lockfunc .
685 .It Fa dmat
686 Pointer to a bus_dma_tag_t where the resulting DMA tag will
687 be stored.
688 .El
689 .Pp
690 Returns
691 .Er ENOMEM
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
696 .Fa dmat
697 that was created by
698 .Fn bus_dma_tag_create .
699 .Pp
700 Returns
701 .Er EBUSY
702 if any DMA maps remain associated with
703 .Fa dmat
704 or
705 .Ql 0
706 on success.
707 .It Fn bus_dma_template_init "*template" "parent"
708 Initializes a
709 .Fa bus_dma_template_t
710 structure.
711 If the
712 .Fa parent
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
722 .Fa dmat .
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
726 freed upon return.
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.
731 When paired with
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
736 .Fa params
737 array.
738 This is not meant to be called directly, use
739 .Fn BUS_DMA_TEMPLATE_FILL
740 instead.
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.
747 .Bd -literal
748         BD_PARENT()     void *
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
756         BD_FLAGS()      uintmax_t
757         BD_LOCKFUNC()   void *
758         BD_LOCKFUNCARG() void *
759 .Ed
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"
764 .It Fa dmat
765 DMA tag.
766 .It Fa flags
767 Are as follows:
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.
778 .El
779 .It Fa mapp
780 Pointer to a
781 .Vt bus_dmamap_t
782 where the resulting DMA map will be stored.
783 .El
784 .Pp
785 Returns
786 .Er ENOMEM
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"
793 .It Fa dmat
794 DMA tag used to allocate
795 .Fa map .
796 .It Fa map
797 The DMA map to destroy.
798 .El
799 .Pp
800 Returns
801 .Er EBUSY
802 if a mapping is still active for
803 .Fa map .
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
807 .Fa buflen
808 bytes of
809 .Fa buf ,
810 associated with the DMA map
811 .Fa 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"
815 .It Fa dmat
816 DMA tag used to allocate
817 .Fa map .
818 .It Fa map
819 A DMA map without a currently active mapping.
820 .It Fa buf
821 A kernel virtual address pointer to a contiguous (in KVA) buffer, to be
822 mapped into device visible address space.
823 .It Fa buflen
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
828 the DMA operation.
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.
834 .It Fa flags
835 Are as follows:
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.
842 .El
843 .El
844 .Pp
845 Return values to the caller are as follows:
846 .Bl -tag -width ".Er EINPROGRESS"
847 .It 0
848 The callback has been called and completed.
849 The status of the mapping has been delivered to the callback.
850 .It Er EINPROGRESS
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.
854 .Pp
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.
861 .It Er ENOMEM
862 The load request has failed due to insufficient resources, and the caller
863 specifically used the
864 .Dv BUS_DMA_NOWAIT
865 flag.
866 .It Er EINVAL
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
870 .Fa dmat ,
871 .Fa map ,
872 .Fa buf ,
873 or
874 .Fa callback
875 were invalid, or
876 .Fa buflen
877 was larger than the
878 .Fa maxsize
879 argument used to create the dma tag
880 .Fa dmat .
881 .El
882 .Pp
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"
887 .It 0
888 The mapping was successful and the
889 .Fa dm_segs
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.
894 .It Er EFBIG
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.
897 .El
898 .It Fn bus_dmamap_load_bio "dmat" "map" "bio" "callback" "callback_arg" "flags"
899 This is a variation of
900 .Fn bus_dmamap_load
901 which maps buffers pointed to by
902 .Fa bio
903 for DMA transfers.
904 .Fa bio
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
908 .Fn bus_dmamap_load
909 which maps data pointed to by
910 .Fa ccb
911 for DMA transfers.
912 The data for
913 .Fa ccb
914 may be any of the following types:
915 .Bl -tag -width ".Er CAM_DATA_SG_PADDR"
916 .It CAM_DATA_VADDR
917 The data is a single KVA buffer.
918 .It CAM_DATA_PADDR
919 The data is a single bus address range.
920 .It CAM_DATA_SG
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.
924 .It CAM_DATA_BIO
925 The data is contained in a
926 .Vt struct bio
927 attached to the CCB.
928 .El
929 .Pp
930 .Fn bus_dmamap_load_ccb
931 supports the following CCB XPT function codes:
932 .Pp
933 .Bl -item -offset indent -compact
934 .It
935 XPT_ATA_IO
936 .It
937 XPT_CONT_TARGET_IO
938 .It
939 XPT_SCSI_IO
940 .El
941 .It Fn bus_dmamap_load_crp "dmat" "map" "crp" "callback" "callback_arg" "flags"
942 This is a variation of
943 .Fn bus_dmamap_load
944 which maps the input buffer pointed to by
945 .Fa crp
946 for DMA transfers.
947 The
948 .Dv BUS_DMA_NOWAIT
949 flag is implied, thus no callback deferral will happen.
950 .It Fn bus_dmamap_load_crp_buffer "dmat" "map" "cb" "callback" "callback_arg" \
951 "flags"
952 This is a variation of
953 .Fn bus_dmamap_load
954 which maps the crypto data buffer pointed to by
955 .Fa cb
956 for DMA transfers.
957 The
958 .Dv BUS_DMA_NOWAIT
959 flag is implied, thus no callback deferral will happen.
960 .It Fn bus_dmamap_load_mbuf "dmat" "map" "mbuf" "callback2" "callback_arg" \
961 "flags"
962 This is a variation of
963 .Fn bus_dmamap_load
964 which maps mbuf chains
965 for DMA transfers.
966 A
967 .Vt bus_size_t
968 argument is also passed to the callback routine, which
969 contains the mbuf chain's packet header length.
970 The
971 .Dv BUS_DMA_NOWAIT
972 flag is implied, thus no callback deferral will happen.
973 .Pp
974 Mbuf chains are assumed to be in kernel virtual address space.
975 .Pp
976 Beside the error values listed for
977 .Fn bus_dmamap_load ,
978 .Er EINVAL
979 will be returned if the size of the mbuf chain exceeds the maximum limit of the
980 DMA tag.
981 .It Fn bus_dmamap_load_mbuf_sg "dmat" "map" "mbuf" "segs" "nsegs" "flags"
982 This is just like
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
987 .Va segs
988 is provided by the caller and filled in directly by the function.
989 The
990 .Va nsegs
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
996 .Fn bus_dmamap_load
997 which maps buffers pointed to by
998 .Fa uio
999 for DMA transfers.
1000 A
1001 .Vt bus_size_t
1002 argument is also passed to the callback routine, which contains the size of
1003 .Fa uio ,
1004 i.e.
1005 .Fa uio->uio_resid .
1006 The
1007 .Dv BUS_DMA_NOWAIT
1008 flag is implied, thus no callback deferral will happen.
1009 Returns the same errors as
1010 .Fn bus_dmamap_load .
1011 .Pp
1012 If
1013 .Fa uio->uio_segflg
1014 is
1015 .Dv UIO_USERSPACE ,
1016 then it is assumed that the buffer,
1017 .Fa uio
1018 is in
1019 .Fa "uio->uio_td->td_proc" Ns 's
1020 address space.
1021 User space memory must be in-core and wired prior to attempting a map
1022 load operation.
1023 Pages may be locked using
1024 .Xr vslock 9 .
1025 .It Fn bus_dmamap_unload "dmat" "map"
1026 Unloads a DMA map.
1027 Arguments are as follows:
1028 .Bl -tag -width ".Fa dmam"
1029 .It Fa dmat
1030 DMA tag used to allocate
1031 .Fa map .
1032 .It Fa map
1033 The DMA map that is to be unloaded.
1034 .El
1035 .Pp
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
1039 .Fn bus_dmamap_sync
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"
1046 .It Fa dmat
1047 DMA tag used to allocate
1048 .Fa map .
1049 .It Fa map
1050 The DMA mapping to be synchronized.
1051 .It Fa op
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
1056 .Fa op .
1057 .El
1058 .Pp
1059 The
1060 .Fn bus_dmamap_sync
1061 function
1062 is the method used to ensure that CPU's and device's direct
1063 memory access (DMA) to shared
1064 memory is coherent.
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
1075 access.
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.
1084 .Pp
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
1089 in
1090 .Fa vaddr
1091 and that is permanently loaded into the newly created
1092 .Vt bus_dmamap_t
1093 returned via
1094 .Fa mapp .
1095 Arguments are as follows:
1096 .Bl -tag -width ".Fa alignment"
1097 .It Fa dmat
1098 DMA tag describing the constraints of the DMA mapping.
1099 .It Fa vaddr
1100 Pointer to a pointer that will hold the returned KVA mapping of
1101 the allocated region.
1102 .It Fa flags
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,
1110 .Dv ENOMEM
1111 is returned.
1112 .It Dv BUS_DMA_COHERENT
1113 Attempt to map this memory in a coherent fashion.
1114 See
1115 .Fn bus_dmamap_create
1116 above for a description of this flag.
1117 For
1118 .Fn bus_dmamem_alloc ,
1119 the
1120 .Dv BUS_DMA_COHERENT
1121 flag is currently implemented on arm and arm64.
1122 .It Dv BUS_DMA_ZERO
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
1127 without reordering.
1128 For
1129 .Fn bus_dmamem_alloc ,
1130 the
1131 .Dv BUS_DMA_NOCACHE
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.
1134 .El
1135 .It Fa mapp
1136 Pointer to a
1137 .Vt bus_dmamap_t
1138 where the resulting DMA map will be stored.
1139 .El
1140 .Pp
1141 The size of memory to be allocated is
1142 .Fa maxsize
1143 as specified in the call to
1144 .Fn bus_dma_tag_create
1145 for
1146 .Fa dmat .
1147 .Pp
1148 The current implementation of
1149 .Fn bus_dmamem_alloc
1150 will allocate all requests as a single segment.
1151 .Pp
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
1154 described below in
1155 .Fn bus_dmamem_free .
1156 Maps are automatically handled by this function and should not be explicitly
1157 allocated or destroyed.
1158 .Pp
1159 Although an explicit load is not required for each access to the memory
1160 referenced by the returned map, the synchronization requirements
1161 as described in the
1162 .Fn bus_dmamap_sync
1163 section still apply and should be used to achieve portability on architectures
1164 without coherent buses.
1165 .Pp
1166 Returns
1167 .Er ENOMEM
1168 if sufficient memory is not available for completing
1169 the operation.
1170 .It Fn bus_dmamem_free "dmat" "*vaddr" "map"
1171 Frees memory previously allocated by
1172 .Fn bus_dmamem_alloc .
1173 Any mappings
1174 will be invalidated.
1175 Arguments are as follows:
1176 .Bl -tag -width ".Fa vaddr"
1177 .It Fa dmat
1178 DMA tag.
1179 .It Fa vaddr
1180 Kernel virtual address of the memory.
1181 .It Fa map
1182 DMA map to be invalidated.
1183 .El
1184 .El
1185 .Sh RETURN VALUES
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
1189 transaction,
1190 .Er ENOMEM
1191 is returned.
1192 All
1193 routines that are not of type
1194 .Vt void
1195 will return 0 on success or an error
1196 code on failure as discussed above.
1197 .Pp
1198 All
1199 .Vt void
1200 routines will succeed if provided with valid arguments.
1201 .Sh LOCKING
1202 Two locking protocols are used by
1203 .Nm .
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
1207 .Nm
1208 and is not exposed to clients of the API.
1209 .Pp
1210 The second protocol involves protecting various resources stored in the tag.
1211 Since almost all
1212 .Nm
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
1215 the driver uses.
1216 In cases where
1217 .Nm
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
1221 .Fn bus_dmamap_load
1222 callback function is called from a deferred context instead of the driver
1223 context.
1224 This means that certain
1225 .Nm
1226 functions must always be called with the same lock held that is specified in the
1227 tag.
1228 These functions include:
1229 .Pp
1230 .Bl -item -offset indent -compact
1231 .It
1232 .Fn bus_dmamap_load
1233 .It
1234 .Fn bus_dmamap_load_bio
1235 .It
1236 .Fn bus_dmamap_load_ccb
1237 .It
1238 .Fn bus_dmamap_load_mbuf
1239 .It
1240 .Fn bus_dmamap_load_mbuf_sg
1241 .It
1242 .Fn bus_dmamap_load_uio
1243 .It
1244 .Fn bus_dmamap_unload
1245 .It
1246 .Fn bus_dmamap_sync
1247 .El
1248 .Pp
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
1254 these functions.
1255 .Pp
1256 Certain
1257 .Nm
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:
1263 .Pp
1264 .Bl -item -offset indent -compact
1265 .It
1266 .Fn bus_dma_tag_create
1267 .It
1268 .Fn bus_dmamap_create
1269 .It
1270 .Fn bus_dmamem_alloc
1271 .El
1272 .Pp
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.
1275 .Sh SEE ALSO
1276 .Xr devclass 9 ,
1277 .Xr device 9 ,
1278 .Xr driver 9 ,
1279 .Xr rman 9 ,
1280 .Xr vslock 9
1281 .Pp
1282 .Rs
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"
1287 .%D "June 1998"
1288 .Re
1289 .Sh HISTORY
1290 The
1291 .Nm
1292 interface first appeared in
1293 .Nx 1.3 .
1294 .Pp
1295 The
1296 .Nm
1297 API was adopted from
1298 .Nx
1299 for use in the CAM SCSI subsystem.
1300 The alterations to the original API were aimed to remove the need for
1301 a
1302 .Vt bus_dma_segment_t
1303 array stored in each
1304 .Vt bus_dmamap_t
1305 while allowing callers to queue up on scarce resources.
1306 .Sh AUTHORS
1307 The
1308 .Nm
1309 interface was designed and implemented by
1310 .An Jason R. Thorpe
1311 of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
1312 Additional input on the
1313 .Nm
1314 design was provided by
1315 .An -nosplit
1316 .An Chris Demetriou ,
1317 .An Charles Hannum ,
1318 .An Ross Harvey ,
1319 .An Matthew Jacob ,
1320 .An Jonathan Stone ,
1321 and
1322 .An Matt Thomas .
1323 .Pp
1324 The
1325 .Nm
1326 interface in
1327 .Fx
1328 benefits from the contributions of
1329 .An Justin T. Gibbs ,
1330 .An Peter Wemm ,
1331 .An Doug Rabson ,
1332 .An Matthew N. Dodd ,
1333 .An Sam Leffler ,
1334 .An Maxime Henrion ,
1335 .An Jake Burkholder ,
1336 .An Takahashi Yoshihiro ,
1337 .An Scott Long
1338 and many others.
1339 .Pp
1340 This manual page was written by
1341 .An Hiten M. Pandya
1342 and
1343 .An Justin T. Gibbs .