]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/bus_dma.9
This commit was generated by cvs2svn to compensate for changes in r133543,
[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 .\" 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.
47 .\"
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.
59 .\"
60 .\" $FreeBSD$
61 .\" $NetBSD: bus_dma.9,v 1.25 2002/10/14 13:43:16 wiz Exp $
62 .\"
63 .Dd May 28, 2003
64 .Dt BUS_DMA 9
65 .Os
66 .Sh NAME
67 .Nm bus_dma ,
68 .Nm bus_dma_tag_create ,
69 .Nm bus_dma_tag_destroy ,
70 .Nm bus_dmamap_create ,
71 .Nm bus_dmamap_destroy ,
72 .Nm bus_dmamap_load ,
73 .Nm bus_dmamap_load_mbuf ,
74 .Nm bus_dmamap_load_uio ,
75 .Nm bus_dmamap_unload ,
76 .Nm bus_dmamap_sync ,
77 .Nm bus_dmamem_alloc ,
78 .Nm bus_dmamem_free
79 .Nd Bus and Machine Independent DMA Mapping Interface
80 .Sh SYNOPSIS
81 .In machine/bus.h
82 .Ft int
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"
88 .Ft int
89 .Fn bus_dma_tag_destroy "bus_dma_tag_t dmat"
90 .Ft int
91 .Fn bus_dmamap_create "bus_dma_tag_t dmat" "int flags" "bus_dmamap_t *mapp"
92 .Ft int
93 .Fn bus_dmamap_destroy "bus_dma_tag_t dmat" "bus_dmamap_t map"
94 .Ft int
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" \
97 "int flags"
98 .Ft int
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" \
101 "int flags"
102 .Ft int
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" \
105 "int flags"
106 .Ft int
107 .Fn bus_dmamem_alloc "bus_dma_tag_t dmat" "void **vaddr" \
108 "int flags" "bus_dmamap_t *mapp"
109 .Ft void
110 .Fn bus_dmamap_unload "bus_dma_tag_t dmat" "bus_dmamap_t map"
111 .Ft void
112 .Fn bus_dmamap_sync "bus_dma_tag_t dmat" "bus_dmamap_t map" \
113 "op"
114 .Ft void
115 .Fn bus_dmamem_free "bus_dma_tag_t dmat" "void *vaddr" \
116 "bus_dmamap_t map"
117 .Sh DESCRIPTION
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.
122 .Pp
123 The
124 .Nm
125 API is a bus, device, and machine-independent (MI) interface to
126 DMA mechanisms.
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
130 and limitations.
131 .Sh STRUCTURES AND TYPES
132 .Bl -tag -width compact
133 .It Vt bus_dma_tag_t
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
143 .It Ft int
144 .Fn "client_filter" "void *filtarg" "bus_addr_t testaddr"
145 .El
146 .sp
147 Address filters can be specified during tag creation to allow
148 for devices who's DMA address restrictions cannot be specified
149 by a single window.
150 The
151 .Fa filtarg
152 is client specified during tag creation to be passed to all
153 invocations of the callback.
154 The
155 .Fa testaddr
156 argument contains a potential starting address of a DMA mapping.
157 The filter function operates on the set of addresses from
158 .Fa testaddr
159 to
160 .Ql trunc_page(testaddr) + PAGE_SIZE - 1 ,
161 inclusive.
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
166 DMA segments.
167 .Bd -literal
168         bus_addr_t      ds_addr;
169         bus_size_t      ds_len;
170 .Ed
171 .sp
172 The
173 .Fa ds_addr
174 field contains the device visible address of the DMA segment, and
175 .Fa ds_len
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.
181 .It Vt bus_dmamap_t
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
186 the load of a
187 .Vt bus_dmamap_t
188 via
189 .Fn bus_dmamap_load .
190 Callbacks are of the format:
191 .Bl -tag -width compact
192 .It Ft void
193 .Fn "client_callback" "void *callback_arg" "bus_dma_segment_t *segs" \
194 "int nseg" "int error"
195 .El
196 .sp
197 The
198 .Fa callback_arg
199 is the callback argument passed to dmamap load functions.
200 The
201 .Fa segs
202 and
203 .Fa nseg
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
209 .Fa error
210 parameter.
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
215 the load of a
216 .Vt bus_dmamap_t
217 via
218 .Fn bus_dmamap_load_uio
219 or
220 .Fn bus_dmamap_load_mbuf .
221 .sp
222 Callback2s are of the format:
223 .Bl -tag -width compact
224 .It Ft void
225 .Fn "client_callback2" "void *callback_arg" "bus_dma_segment_t *segs" \
226 "int nseg" "bus_size_t mapsize" "int error"
227 .El
228 .sp
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
232 .Fa mapsize .
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.
237 The
238 .Vt bus_dmasync_op_t
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
241 are taken.
242 All operations specified below are performed from the CPU's
243 point of view (for a complete description, see the
244 .Fn bus_dmamap_sync
245 description below):
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.
265 .El
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 This method is of the format:
271 .Bl -tag -width compact
272 .It Ft void
273 .Fn "lockfunc" "void *lockfunc_arg" "bus_dma_lock_op_t op"
274 .El
275 .sp
276 Two
277 .Vt lockfunc
278 implementations are provided for convenience.
279 .Fn busdma_lock_mutex
280 performs standard mutex operations on the sleep mutex provided via the
281 .Fa lockfuncarg .
282 passed into
283 .Fn bus_dma_tag_create .
284 .Fn dflt_lock
285 will generate a system panic if it is called.
286 It is substituted into the tag when
287 .Fa lockfunc
288 is passed as NULL to
289 .Fn bus_dma_tag_create .
290 .It Vt bus_dma_lock_op_t
291 Operations to be performed by the client-specified
292 .Fn lockfunc .
293 .Bl -tag -width BUS_DMA_UNLOCK
294 .It Dv BUS_DMA_LOCK
295 Acquires and/or locks the client locking primitive.
296 .It Dv BUS_DMA_UNLOCK
297 Releases and/or unlocks the client locking primitive.
298 .El
299 .El
300 .sp
301 .Sh FUNCTIONS
302 .Bl -tag -width compact
303 .It Fn bus_dma_tag_create "parent" "alignment" "boundary" "lowaddr" \
304 "highaddr" "*filtfunc" "*filtfuncarg" "maxsize" "nsegments" "maxsegsz" \
305 "flags" "lockfunc" "lockfuncarg" "*dmat"
306 Allocates a device specific DMA tag, and initializes it according to
307 the arguments provided:
308 .Bl -tag -width *filtfuncarg -compact
309 .It Fa parent
310 Indicates restrictions between the parent bridge, CPU memory, and the
311 device.
312 May be NULL, if no DMA restrictions are to be inherited.
313 .It Fa alignment
314 Alignment constraint, in bytes, of any mappings created using this tag.
315 The alignment must be a power of 2.
316 Hardware that can DMA starting at any address would specify
317 .Em 1
318 for byte alignment.
319 Hardware requiring DMA transfers to start on a multiple of 4K
320 would specify
321 .Em 4096.
322 .It Fa boundary
323 Boundary constraint, in bytes, of the target DMA memory region.
324 The boundary indicates the set of addresses, all multiples of the
325 boundary argument, that cannot be crossed by a single
326 .Vt bus_dma_segment_t .
327 The boundary must be a power of 2 and must be no smaller that the
328 maximum segment size.
329 .Ql 0
330 indicates that there are no boundary restrictions.
331 .It Fa lowaddr
332 .It Fa highaddr
333 Bounds of the window of bus address space that
334 .Em cannot
335 be directly accessed by the device.
336 The window contains all address greater than lowaddr and
337 less than or equal to highaddr.
338 For example, a device incapable of DMA above 4GB, would specify
339 a highaddr of
340 .Dv BUS_SPACE_MAXADDR
341 and a lowaddr of
342 .Dv BUS_SPACE_MAXADDR_32BIT .
343 Similarly a device that can only dma to addresses bellow 16MB would
344 specify a highaddr of
345 .Dv BUS_SPACE_MAXADDR
346 and a lowaddr of
347 .Dv BUS_SPACE_MAXADDR_24BIT .
348 Some implementations requires that some region of device visible
349 address space, overlapping available host memory, be outside the
350 window.
351 This area of
352 .Ql safe memory
353 is used to bounce requests that would otherwise conflict with
354 the exclusion window.
355 .It Fa filtfunc
356 Optional filter function (may be NULL) to be called for any attempt to
357 map memory into the window described by
358 .Fa lowaddr
359 and
360 .Fa highaddr.
361 A filter function is only required when the single window described
362 by
363 .Fa lowaddr
364 and
365 .Fa highaddr
366 cannot adequately describe the constraints of the device.
367 The filter function will be called for every machine page
368 that overlaps the exclusion window.
369 .It Fa filtfuncarg
370 Argument passed to all calls to the filter function for this tag.
371 May be NULL.
372 .It Fa maxsize
373 Maximum size, in bytes, of the sum of all segment lengths in a given
374 DMA mapping associated with this tag.
375 .It Fa nsegments
376 Number of discontinuities (scatter/gather segments) allowed
377 in a DMA mapped region.
378 If there is no restriction,
379 .Dv BUS_SPACE_UNRESTRICTED
380 may be specified.
381 .It Fa maxsegsz
382 Maximum size, in bytes, of a segment in any DMA mapped region associated
383 with
384 .Fa dmat .
385 .It Fa flags
386 Are as follows:
387 .Bl -tag -width "BUS_DMA_ALLOCNOW" -compact
388 .It Dv BUS_DMA_ALLOCNOW
389 Allocate the resources necessary to guarantee that all map load
390 operations associated with this tag will not block.
391 If sufficient resources are not available,
392 .Er ENOMEM
393 is returned.
394 .El
395 .It Fa lockfunc
396 Optional lock manipulation function (may be NULL) to be called when busdma
397 needs to manipulate a lock on behalf of the client.
398 If NULL is specified,
399 .Fn dflt_lock
400 is used.
401 .It Fa lockfuncarg
402 Optional argument to be passed to the function specified by
403 .Fa lockfunc .
404 .It Fa dmat
405 Pointer to a bus_dma_tag_t where the resulting DMA tag will
406 be stored.
407 .El
408 .Pp
409 Returns
410 .Er ENOMEM
411 if sufficient memory is not available for tag creation
412 or allocating mapping resources.
413 .It Fn bus_dma_tag_destroy "dmat"
414 Deallocate the DMA tag
415 .Fa dmat
416 that was created by
417 .Fn bus_dma_tag_create .
418 .Pp
419 Returns
420 .Er EBUSY
421 if any DMA maps remain associated with
422 .Fa dmat
423 or
424 .Ql 0
425 on success.
426 .It Fn bus_dmamap_create "dmat" "flags" "*mapp"
427 Allocates and initializes a DMA map.
428 Arguments are as follows:
429 .Bl -tag -width nsegments -compact
430 .It Fa dmat
431 DMA tag.
432 .It Fa flags
433 The value of this argument is currently undefined and should be
434 specified as
435 .Ql 0 .
436 .It Fa mapp
437 Pointer to a
438 .Vt bus_dmamap_t
439 where the resulting DMA map will be stored.
440 .El
441 .Pp
442 Returns
443 .Er ENOMEM
444 if sufficient memory is not available for creating the
445 map or allocating mapping resources.
446 .It Fn bus_dmamap_destroy "dmat" "map"
447 Frees all resources associated with a given DMA map.
448 Arguments are as follows:
449 .Bl -tag -width dmat -compact
450 .It Fa dmat
451 DMA tag used to allocate
452 .Fa map .
453 .It Fa map
454 The DMA map to destroy.
455 .El
456 .Pp
457 Returns
458 .Er EBUSY
459 if a mapping is still active for
460 .Fa map .
461 .It Fn bus_dmamap_load "dmat" "map" "buf" "buflen" "*callback" "..."
462 Creates a mapping in device visible address space of
463 .Fa buflen
464 bytes of
465 .Fa buf ,
466 associated with the DMA map
467 .Fa map.
468 Arguments are as follows:
469 .Bl -tag -width buflen -compact
470 .It Fa dmat
471 DMA tag used to allocate
472 .Fa map.
473 .It Fa map
474 A DMA map without a currently active mapping.
475 .It Fa buf
476 A kernel virtual address pointer to a contiguous (in KVA) buffer, to be
477 mapped into device visible address space.
478 .It Fa buflen
479 The size of the buffer.
480 .It Fa callback Fa callback_arg
481 The callback function, and its argument.
482 .It Fa flags
483 The value of this argument is currently undefined, and should be
484 specified as
485 .Ql 0 .
486 .El
487 .Pp
488 Return values to the caller are as follows:
489 .Bl -tag -width EINPROGRESS -compact
490 .It 0
491 The callback has been called and completed.
492 The status of the mapping has been delivered to the callback.
493 .It Er EINPROGRESS
494 The mapping has been deferred for lack of resources.
495 The callback will be called as soon as resources are available.
496 Callbacks are serviced in FIFO order.
497 DMA maps created from DMA tags that are allocated with
498 the
499 .Dv BUS_DMA_ALLOCNOW
500 flag will never return this status for a load operation.
501 .It Er EINVAL
502 The load request was invalid.
503 The callback has not, and will not be called.
504 This error value may indicate that
505 .Fa dmat ,
506 .Fa map ,
507 .Fa buf ,
508 or
509 .Fa callback
510 were invalid, or
511 .Fa buslen
512 was larger than the
513 .Fa maxsize
514 argument used to create the dma tag
515 .Fa dmat .
516 .El
517 .Pp
518 When the callback is called, it is presented with an error value
519 indicating the disposition of the mapping.
520 Error may be one of the following:
521 .Bl -tag -width EINPROGRESS -compact
522 .It 0
523 The mapping was successful and the
524 .Fa dm_segs
525 callback argument contains an array of
526 .Vt bus_dma_segment_t
527 elements describing the mapping.
528 This array is only valid during the scope of the callback function.
529 .It Er EFBIG
530 A mapping could not be achieved within the segment constraints provided
531 in the tag even though the requested allocation size was less than maxsize.
532 .El
533 .It Fn bus_dmamap_load_mbuf "dmat" "map" "mbuf" "callback2" "callback_arg" \
534 "flags"
535 This is a variation of
536 .Fn bus_dmamap_load
537 which maps mbuf chains
538 for DMA transfers.
539 A
540 .Vt bus_size_t
541 argument is also passed to the callback routine, which
542 contains the mbuf chain's packet header length.
543 .Pp
544 Mbuf chains are assumed to be in kernel virtual address space.
545 .Pp
546 Returns
547 .Er EINVAL
548 if the size of the mbuf chain exceeds the maximum limit of the
549 DMA tag.
550 .It Fn bus_dmamap_load_uio "dmat" "map" "uio" "callback2" "callback_arg" "flags"
551 This is a variation of
552 .Fn bus_dmamap_load
553 which maps buffers pointed to by
554 .Fa uio
555 for DMA transfers.
556 A
557 .Vt bus_size_t
558 argument is also passed to the callback routine, which contains the size of
559 .Fa uio ,
560 i.e.
561 .Fa uio->uio_resid .
562 .Pp
563 If
564 .Fa uio->uio_segflg
565 is
566 .Dv UIO_USERSPACE ,
567 then it is assumed that the buffer,
568 .Fa uio
569 is in
570 .Fa "uio->uio_td->td_proc" Ns 's
571 address space.
572 User space memory must be in-core and wired prior to attempting a map
573 load operation.
574 .It Fn bus_dmamap_unload "dmat" "map"
575 Unloads a DMA map.
576 Arguments are as follows:
577 .Bl -tag -width dmam -compact
578 .It Fa dmat
579 DMA tag used to allocate
580 .Fa map .
581 .It Fa map
582 The DMA map that is to be unloaded.
583 .El
584 .Pp
585 .Fn bus_dmamap_unload
586 will not perform any implicit synchronization of DMA buffers.
587 This must be done explicitly by a call to
588 .Fn bus_dmamap_sync
589 prior to unloading the map.
590 .It Fn bus_dmamap_sync "dmat" "map" "op"
591 Performs synchronization of a device visible mapping with the CPU visible
592 memory referenced by that mapping.
593 Arguments are as follows:
594 .Bl -tag -width dmat -compact
595 .It Fa dmat
596 DMA tag used to allocate
597 .Fa map .
598 .It Fa map
599 The DMA mapping to be synchronized.
600 .It Fa op
601 Type of synchronization operation to perform.
602 See the definition of
603 .Vt bus_dmasync_op_t
604 for a description of the acceptable values for
605 .Fa op .
606 .El
607 .Pp
608 .Fn bus_dmamap_sync
609 is the method used to ensure that CPU and device DMA access to shared
610 memory is coherent.
611 For example, the CPU might be used to setup the contents of a buffer
612 that is to be DMA'ed into a device.
613 To ensure that the data are visible via the device's mapping of that
614 memory, the buffer must be loaded and a dma sync operation of
615 .Dv BUS_DMASYNC_PREREAD
616 must be performed.
617 Additional sync operations must be performed after every CPU write
618 to this memory if additional DMA reads are to be performed.
619 Conversely, for the DMA write case, the buffer must be loaded,
620 and a dma sync operation of
621 .Dv BUS_DMASYNC_PREWRITE
622 must be performed.
623 The CPU will only be able to see the results of this DMA write
624 once the DMA has completed and a
625 .Dv BUS_DMASYNC_POSTWRITE
626 operation has been performed.
627 .Pp
628 If DMA read and write operations are not preceded and followed by the
629 appropriate synchronization operations, behavior is undefined.
630 .It Fn bus_dmamem_alloc "dmat" "**vaddr" "flags" "mapp"
631 Allocates memory that is mapped into KVA at the address returned
632 in
633 .Fa vaddr
634 that is permanently loaded into the newly created
635 .Vt bus_dmamap_t
636 returned via
637 .Fa mapp .
638 Arguments are as follows:
639 .Bl -tag -width alignment -compact
640 .It Fa dmat
641 DMA tag describing the constraints of the DMA mapping.
642 .It Fa vaddr
643 Pointer to a pointer that will hold the returned KVA mapping of
644 the allocated region.
645 .It Fa flags
646 Flags are defined as follows:
647 .Bl -tag -width BUS_DMA_NOWAIT -compact
648 .It Dv BUS_DMA_WAITOK
649 The routine can safely wait (sleep) for resources.
650 .It Dv BUS_DMA_NOWAIT
651 The routine is not allowed to wait for resources.
652 If resources are not available,
653 .Dv ENOMEM
654 is returned.
655 .It Dv BUS_DMA_COHERENT
656 Attempt to map this memory such that cache sync operations are
657 as cheap as possible.
658 This flag is typically set on memory that will be accessed by both
659 a CPU and a DMA engine, frequently.
660 Use of this flag does not remove the requirement of using
661 bus_dmamap_sync, but it may reduce the cost of performing
662 these operations.
663 .It Dv BUS_DMA_ZERO
664 Causes the allocated memory to be set to all zeros.
665 .El
666 .It Fa mapp
667 Pointer to storage for the returned DMA map.
668 .El
669 .Pp
670 The size of memory to be allocated is
671 .Fa maxsize
672 as specified in
673 .Fa dmat .
674 .Pp
675 The current implementation of
676 .Fn bus_dmamem_alloc
677 will allocate all requests as a single segment.
678 .Pp
679 Although no explicit loading is required to access the memory
680 referenced by the returned map, the synchronization requirements
681 as described in the
682 .Fn bus_dmamap_sync
683 section still apply.
684 .Pp
685 Returns
686 .Er ENOMEM
687 if sufficient memory is not available for completing
688 the operation.
689 .It Fn bus_dmamem_free "dmat" "*vaddr" "map"
690 Frees memory previously allocated by
691 .Fn bus_dmamem_alloc .
692 Any mappings
693 will be invalidated.
694 Arguments are as follows:
695 .Bl -tag -width vaddr -compact
696 .It Fa dmat
697 DMA tag.
698 .It Fa vaddr
699 Kernel virtual address of the memory.
700 .It Fa map
701 DMA map to be invalidated.
702 .El
703 .El
704 .Sh RETURN VALUES
705 Behavior is undefined if invalid arguments are passed to
706 any of the above functions.
707 If sufficient resources cannot be allocated for a given
708 transaction,
709 .Er ENOMEM
710 is returned.
711 All
712 routines that are not of type,
713 .Vt void ,
714 will return 0 on success or an error
715 code, as discussed above.
716 .Pp
717 All
718 .Vt void
719 routines will succeed if provided with valid arguments.
720 .Sh SEE ALSO
721 .Xr devclass 9 ,
722 .Xr device 9 ,
723 .Xr driver 9 ,
724 .Xr rman 9
725 .Pp
726 .Rs
727 .%A "Jason R. Thorpe"
728 .%T "A Machine-Independent DMA Framework for NetBSD"
729 .%J "Proceedings of the Summer 1998 USENIX Technical Conference"
730 .%Q "USENIX Association"
731 .%D "June 1998"
732 .Re
733 .Sh HISTORY
734 The
735 .Nm
736 interface first appeared in
737 .Nx 1.3 .
738 .Pp
739 The
740 .Nm
741 API was adopted from
742 .Nx
743 for use in the CAM SCSI subsystem.
744 The alterations to the original API were aimed to remove the need for
745 a
746 .Vt bus_dma_segment_t
747 array stored in each
748 .Vt bus_dmamap_t
749 while allowing callers to queue up on scarce resources.
750 .Sh AUTHORS
751 The
752 .Nm
753 interface was designed and implemented by
754 .An Jason R. Thorpe
755 of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
756 Additional input on the
757 .Nm
758 design was provided by
759 .An -nosplit
760 .An Chris Demetriou ,
761 .An Charles Hannum ,
762 .An Ross Harvey ,
763 .An Matthew Jacob ,
764 .An Jonathan Stone ,
765 and
766 .An Matt Thomas .
767 .Pp
768 The
769 .Nm
770 interface in
771 .Fx
772 benefits from the contributions of
773 .An Justin T. Gibbs ,
774 .An Peter Wemm ,
775 .An Doug Rabson ,
776 .An Matthew N. Dodd ,
777 .An Sam Leffler ,
778 .An Maxime Henrion ,
779 .An Jake Burkholder ,
780 .An Takahashi Yoshihiro ,
781 .An Scott Long
782 and many others.
783 .Pp
784 This manual page was written by
785 .An Hiten M. Pandya
786 and
787 .An Justin T. Gibbs .