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