rman.9: Comment out documentation for rman_await_resource().
[dragonfly.git] / share / man / man9 / bus_dma.9
1 .\"
2 .\" Copyright (c) 2002, 2003, 2004 The DragonFly Project.  All rights reserved.
3 .\"
4 .\" This code is derived from software contributed to The DragonFly Project
5 .\" by Hiten Pandya <hmp@backplane.com>.
6 .\"
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
9 .\" are met:
10 .\"
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\"    notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\"    notice, this list of conditions and the following disclaimer in
15 .\"    the documentation and/or other materials provided with the
16 .\"    distribution.
17 .\" 3. Neither the name of The DragonFly Project nor the names of its
18 .\"    contributors may be used to endorse or promote products derived
19 .\"    from this software without specific, prior written permission.
20 .\"
21 .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 .\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 .\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25 .\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 .\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 .\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 .\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 .\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 .\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" SUCH DAMAGE.
33 .\"
34 .\" Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
35 .\" All rights reserved.
36 .\"
37 .\" This code is derived from software contributed to The NetBSD Foundation
38 .\" by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
39 .\" NASA Ames Research Center.
40 .\"
41 .\" Redistribution and use in source and binary forms, with or without
42 .\" modification, are permitted provided that the following conditions
43 .\" are met:
44 .\" 1. Redistributions of source code must retain the above copyright
45 .\"    notice, this list of conditions and the following disclaimer.
46 .\" 2. Redistributions in binary form must reproduce the above copyright
47 .\"    notice, this list of conditions and the following disclaimer in the
48 .\"    documentation and/or other materials provided with the distribution.
49 .\" 3. All advertising materials mentioning features or use of this software
50 .\"    must display the following acknowledgment:
51 .\"     This product includes software developed by the NetBSD
52 .\"     Foundation, Inc. and its contributors.
53 .\" 4. Neither the name of The NetBSD Foundation nor the names of its
54 .\"    contributors may be used to endorse or promote products derived
55 .\"    from this software without specific prior written permission.
56 .\"
57 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
58 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
59 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
60 .\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
61 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
64 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
65 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
66 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
67 .\" POSSIBILITY OF SUCH DAMAGE.
68 .\"
69 .\" $FreeBSD: /repoman/r/ncvs/src/share/man/man9/bus_dma.9,v 1.7 2003/07/27 14:05:29 mux Exp $
70 .\" $NetBSD: bus_dma.9,v 1.25 2002/10/14 13:43:16 wiz Exp $
71 .\" $DragonFly: src/share/man/man9/bus_dma.9,v 1.8 2007/04/09 21:20:37 swildner Exp $
72 .\"
73 .Dd March 17, 2004
74 .Dt BUS_DMA 9
75 .Os
76 .Sh NAME
77 .Nm bus_dma ,
78 .Nm bus_dma_tag_create ,
79 .Nm bus_dma_tag_destroy ,
80 .Nm bus_dmamap_create ,
81 .Nm bus_dmamap_destroy ,
82 .Nm bus_dmamap_load ,
83 .Nm bus_dmamap_load_mbuf ,
84 .Nm bus_dmamap_load_mbuf_segment ,
85 .Nm bus_dmamap_load_mbuf_defrag ,
86 .Nm bus_dmamap_load_uio ,
87 .Nm bus_dmamap_unload ,
88 .Nm bus_dmamap_sync ,
89 .Nm bus_dmamem_alloc ,
90 .Nm bus_dmamem_coherent ,
91 .Nm bus_dmamem_coherent_any ,
92 .Nm bus_dmamem_free
93 .Nd Bus and Machine Independent DMA Mapping Interface
94 .Sh SYNOPSIS
95 .In machine/bus.h
96 .Ft int
97 .Fn bus_dma_tag_create "bus_dma_tag_t parent" "bus_size_t alignment" \
98 "bus_size_t boundary" "bus_addr_t lowaddr" "bus_addr_t highaddr" \
99 "bus_dma_filter_t *filtfunc" "void *filtfuncarg" "bus_size_t maxsize" \
100 "int nsegments" "bus_size_t maxsegsz" "int flags" "bus_dma_tag_t *dmat"
101 .Ft int
102 .Fn bus_dma_tag_destroy "bus_dma_tag_t dmat"
103 .Ft int
104 .Fn bus_dmamap_create "bus_dma_tag_t dmat" "int flags" "bus_dmamap_t *mapp"
105 .Ft int
106 .Fn bus_dmamap_destroy "bus_dma_tag_t dmat" "bus_dmamap_t map"
107 .Ft int
108 .Fn bus_dmamap_load "bus_dma_tag_t dmat" "bus_dmamap_t map" "void *buf" \
109 "bus_size_t buflen" "bus_dmamap_callback_t *callback" "void *callback_arg" \
110 "int flags"
111 .Ft int
112 .Fn bus_dmamap_load_mbuf "bus_dma_tag_t dmat" "bus_dmamap_t map" \
113 "struct mbuf *mbuf" "bus_dmamap_callback2_t *callback" "void *callback_arg" \
114 "int flags"
115 .Ft int
116 .Fn bus_dmamap_load_mbuf_segment "bus_dma_tag_t dmat" "bus_dmamap_t map" \
117 "struct mbuf *mbuf" "bus_dma_segment_t *segs" "int maxsegs" "int *nsegs" \
118 "int flags"
119 .Ft int
120 .Fn bus_dmamap_load_mbuf_defrag "bus_dma_tag_t dmat" "bus_dmamap_t map" \
121 "struct mbuf **mbuf" "bus_dma_segment_t *segs" "int maxsegs" "int *nsegs" \
122 "int flags"
123 .Ft int
124 .Fn bus_dmamap_load_uio "bus_dma_tag_t dmat" "bus_dmamap_t map" \
125 "struct uio *uio" "bus_dmamap_callback2_t *callback" "void *callback_arg" \
126 "int flags"
127 .Ft int
128 .Fn bus_dmamem_alloc "bus_dma_tag_t dmat" "void **vaddr" \
129 "int flags" "bus_dmamap_t *mapp"
130 .Ft int
131 .Fn bus_dmamem_coherent "bus_dma_tag_t parent" "bus_size_t alignment" \
132 "bus_size_t boundary" "bus_addr_t lowaddr" "bus_addr_t highaddr" \
133 "bus_size_t maxsize" "int flags" "bus_dmamem_t *dmem"
134 .Ft void *
135 .Fn bus_dmamem_coherent_any "bus_dma_tag_t parent" "bus_size_t alignment" \
136 "bus_size_t maxsize" "int flags" "bus_dma_tag_t *dtag" "bus_dmamap_t *dmap" \
137 "bus_addr_t *busaddr"
138 .Ft void
139 .Fn bus_dmamap_unload "bus_dma_tag_t dmat" "bus_dmamap_t map"
140 .Ft void
141 .Fn bus_dmamap_sync "bus_dma_tag_t dmat" "bus_dmamap_t map" \
142 "bus_dmasync_op_t op"
143 .Ft void
144 .Fn bus_dmamem_free "bus_dma_tag_t dmat" "void *vaddr" \
145 "bus_dmamap_t map"
146 .Sh DESCRIPTION
147 Direct Memory Access (DMA) is a method of transferring data
148 without involving the CPU, thus providing higher performance.
149 A DMA transaction can be achieved between device to memory,
150 device to device, or memory to memory.
151 .Pp
152 The
153 .Nm
154 API is a bus, device, and machine-independent (MI) interface to
155 DMA mechanisms.
156 It provides the client with flexibility and simplicity by
157 abstracting machine dependent issues like setting up
158 DMA mappings, handling cache issues, bus specific features
159 and limitations.
160 .Sh STRUCTURES AND TYPES
161 .Bl -tag -width compact
162 .It Vt bus_dma_tag_t
163 A machine-dependent (MD) opaque type that describes the
164 characteristics of DMA transactions.
165 DMA tags are organized into a hierarchy, with each child
166 tag inheriting the restrictions of its parent.
167 This allows all devices along the path of DMA transactions
168 to contribute to the constraints of those transactions.
169 .It Vt bus_dma_filter_t
170 Client specified address filter having the format:
171 .Bl -tag -width compact
172 .It Ft int
173 .Fn "client_filter" "void *filtarg" "bus_addr_t testaddr"
174 .El
175 .sp
176 Address filters can be specified during tag creation to allow
177 for devices who's DMA address restrictions cannot be specified
178 by a single window.
179 The
180 .Fa filtarg
181 is client specified during tag creation to be passed to all
182 invocations of the callback.
183 The
184 .Fa testaddr
185 argument contains a potential starting address of a DMA mapping.
186 The filter function operates on the set of addresses from
187 .Fa testaddr
188 to
189 .Ql trunc_page(testaddr) + PAGE_SIZE - 1 ,
190 inclusive.
191 The filter function should return zero for any mapping in this range
192 that can be accommodated by the device and non-zero otherwise.
193 .It Vt bus_dma_segment_t
194 A machine-dependent type that describes individual
195 DMA segments.
196 .Bd -literal
197         bus_addr_t      ds_addr;
198         bus_size_t      ds_len;
199 .Ed
200 .sp
201 The
202 .Fa ds_addr
203 field contains the device visible address of the DMA segment, and
204 .Fa ds_len
205 contains the length of the DMA segment.
206 Although the DMA segments returned by a mapping call will adhere to
207 all restrictions necessary for a successful DMA operation, some conversion
208 (e.g. a conversion from host byte order to the device's byte order) is
209 almost always required when presenting segment information to the device.
210 .It Vt bus_dmamap_t
211 A machine-dependent opaque type describing an individual mapping.
212 Multiple DMA maps can be associated with one DMA tag.
213 .It Vt bus_dmamem_t
214 A machine-dependent type that describes DMA memory created by
215 .Fn bus_dmamem_coherent .
216 .Bd -literal
217         bus_dma_tag_t   dmem_tag;
218         bus_dmamap_t    dmem_map;
219         void            *dmem_addr;
220         bus_addr_t      dmem_busaddr;
221 .Ed
222 .sp
223 The
224 .Fa dmem_tag
225 field contains the DMA tag of the DMA memory and
226 .Fa dmem_map
227 field contains the DMA map of the DMA memory.
228 The
229 .Fa dmem_addr
230 field points to the allocated DMA memory in kernel virtual address space.
231 The
232 .Fa dmem_busaddr
233 field contains the device visible address of the DMA memory.
234 .It Vt bus_dmamap_callback_t
235 Client specified callback for receiving mapping information resulting from
236 the load of a
237 .Vt bus_dmamap_t
238 via
239 .Fn bus_dmamap_load .
240 Callbacks are of the format:
241 .Bl -tag -width compact
242 .It Ft void
243 .Fn "client_callback" "void *callback_arg" "bus_dma_segment_t *segs" \
244 "int nseg" "int error"
245 .El
246 .sp
247 The
248 .Fa callback_arg
249 is the callback argument passed to dmamap load functions.
250 The
251 .Fa segs
252 and
253 .Fa nseg
254 parameters describe an array of
255 .Vt bus_dma_segment_t
256 structures that represent the mapping.
257 This array is only valid within the scope of the callback function.
258 The success or failure of the mapping is indicated by the
259 .Fa error
260 parameter.
261 More information on the use of callbacks can be found in the
262 description of the individual dmamap load functions.
263 .It Vt bus_dmamap_callback2_t
264 Client specified callback for receiving mapping information resulting from
265 the load of a
266 .Vt bus_dmamap_t
267 via
268 .Fn bus_dmamap_load_uio
269 or
270 .Fn bus_dmamap_load_mbuf .
271 .sp
272 Callback2s are of the format:
273 .Bl -tag -width compact
274 .It Ft void
275 .Fn "client_callback2" "void *callback_arg" "bus_dma_segment_t *segs" \
276 "int nseg" "bus_size_t mapsize" "int error"
277 .El
278 .sp
279 Callback2's behavior is the same as
280 .Vt bus_dmamap_callback_t
281 with the addition that the length of the data mapped is provided via
282 .Fa mapsize .
283 .It Vt bus_dmasync_op_t
284 Memory synchronization operation specifier.
285 Bus DMA requires explicit synchronization of memory with it's device
286 visible mapping in order to guarantee memory coherency.
287 The
288 .Vt bus_dmasync_op_t
289 allows the type of DMA operation that will be or has been performed
290 to be communicated to the system so that the correct coherency measures
291 are taken.
292 All operations specified below are performed from the DMA engine's
293 point of view:
294 .Bl -tag -width BUS_DMASYNC_POSTWRITE
295 .It Dv BUS_DMASYNC_PREREAD
296 Perform any synchronization required after an update of memory by the CPU
297 but prior to DMA read operations.
298 .It Dv BUS_DMASYNC_PREWRITE
299 Perform any synchronization required after an update of memory by the CPU
300 but prior to DMA write operations.
301 .It Dv BUS_DMASYNC_POSTREAD
302 Perform any synchronization required after DMA read operations, but prior
303 to CPU access of the memory.
304 .It Dv BUS_DMASYNC_POSTWRITE
305 Perform any synchronization required after DMA write operations, but prior
306 to CPU access of the memory.
307 .El
308 .El
309 .sp
310 .Sh FUNCTIONS
311 .Bl -tag -width compact
312 .It Fn bus_dma_tag_create "parent" "alignment" "boundary" "lowaddr" \
313 "highaddr" "*filtfunc" "*filtfuncarg" "maxsize" "nsegments" "maxsegsz" \
314 "flags" "*dmat"
315 Allocates a device specific DMA tag, and initializes it according to
316 the arguments provided:
317 .Bl -tag -width *filtfuncarg -compact
318 .It Fa parent
319 Indicates restrictions between the parent bridge, CPU memory, and the
320 device.
321 May be NULL, if no DMA restrictions are to be inherited.
322 .It Fa alignment
323 Alignment constraint, in bytes, of any mappings created using this tag.
324 The alignment must be a power of 2.
325 Hardware that can DMA starting at any address would specify
326 .Em 1
327 for byte alignment.
328 Hardware requiring DMA transfers to start on a multiple of 4K
329 would specify
330 .Em 4096 .
331 .It Fa boundary
332 Boundary constraint, in bytes, of the target DMA memory region.
333 The boundary indicates the set of addresses, all multiples of the
334 boundary argument, that cannot be crossed by a single
335 .Vt bus_dma_segment_t .
336 The boundary must be either a power of 2 or 0.
337 .Ql 0
338 indicates that there are no boundary restrictions.
339 .It Fa lowaddr
340 .It Fa highaddr
341 Bounds of the window of bus address space that
342 .Em cannot
343 be directly accessed by the device.
344 The window contains all address greater than lowaddr and
345 less than or equal to highaddr.
346 For example, a device incapable of DMA above 4GB, would specify
347 a highaddr of
348 .Dv BUS_SPACE_MAXADDR
349 and a lowaddr of
350 .Dv BUS_SPACE_MAXADDR_32BIT .
351 Similarly a device that can only dma to addresses bellow 16MB would
352 specify a highaddr of
353 .Dv BUS_SPACE_MAXADDR
354 and a lowaddr of
355 .Dv BUS_SPACE_MAXADDR_24BIT .
356 Some implementations requires that some region of device visible
357 address space, overlapping available host memory, be outside the
358 window.
359 This area of
360 .Ql safe memory
361 is used to bounce requests that would otherwise conflict with
362 the exclusion window.
363 .It Fa filtfunc
364 Optional filter function (may be NULL) to be called for any attempt to
365 map memory into the window described by
366 .Fa lowaddr
367 and
368 .Fa highaddr .
369 A filter function is only required when the single window described
370 by
371 .Fa lowaddr
372 and
373 .Fa highaddr
374 cannot adequately describe the constraints of the device.
375 The filter function will be called for every machine page
376 that overlaps the exclusion window.
377 .It Fa filtfuncarg
378 Argument passed to all calls to the filter function for this tag.
379 May be NULL.
380 .It Fa maxsize
381 Maximum size, in bytes, of the sum of all segment lengths in a given
382 DMA mapping associated with this tag.
383 .It Fa nsegments
384 Number of discontinuities (scatter/gather segments) allowed
385 in a DMA mapped region.
386 If there is no restriction,
387 .Dv BUS_SPACE_UNRESTRICTED
388 may be specified.
389 .It Fa maxsegsz
390 Maximum size, in bytes, of a segment in any DMA mapped region associated
391 with
392 .Fa dmat .
393 .It Fa flags
394 Are as follows:
395 .Bl -tag -width ".Dv BUS_DMA_ALLOCNOW" -compact
396 .It Dv BUS_DMA_ALLOCNOW
397 Allocate the minimum resources necessary to guarantee that all map load
398 operations associated with this tag may not block.
399 If sufficient resources are not available,
400 .Er ENOMEM
401 is returned.
402 .It Dv BUS_DMA_WAITOK
403 Indicates that it is OK to wait for resources.
404 However,
405 unlike
406 .Xr kmalloc 9 ,
407 it is not guaranteed that the resource allocation will succeed.
408 This flag is the default one,
409 if
410 .Dv BUS_DMA_NOWAIT
411 is not supplied.
412 .It Dv BUS_DMA_NOWAIT
413 If the resource allocation request cannot be immediately fulfilled,
414 .Er ENOMEM
415 is returned.
416 .It Dv BUS_DMA_ONEBPAGE
417 Allocte one bounce page at most,
418 even if the
419 .Fa maxsize
420 indicates that multiple bounce pages are needed.
421 .It Dv BUS_DMA_ALIGNED
422 Indicates that all memory to be loaded into the DMA maps associated
423 with this DMA tag is properly aligned according to
424 .Fa alignment
425 constraint.
426 No resources,
427 e.g. bounce pages,
428 will be allocated due to the
429 .Fa alignment
430 constraint.
431 If unaligned memory was loaded into the DMA maps associated with this DMA tag,
432 system will panic.
433 .El
434 .It Fa dmat
435 Pointer to a bus_dma_tag_t where the resulting DMA tag will
436 be stored.
437 .El
438 .Pp
439 Returns
440 .Er ENOMEM
441 if sufficient memory is not available for tag creation
442 or allocating mapping resources.
443 .It Fn bus_dma_tag_destroy "dmat"
444 Deallocate the DMA tag
445 .Fa dmat
446 that was created by
447 .Fn bus_dma_tag_create .
448 .Pp
449 Returns
450 .Er EBUSY
451 if any DMA maps remain associated with
452 .Fa dmat
453 or
454 .Ql 0
455 on success.
456 .It Fn bus_dmamap_create "dmat" "flags" "*mapp"
457 Allocates and initializes a DMA map.
458 Arguments are as follows:
459 .Bl -tag -width nsegments -compact
460 .It Fa dmat
461 DMA tag.
462 .It Fa flags
463 Are as follows:
464 .Bl -tag -width ".Dv BUS_DMA_ONEBPAGE" -compact
465 .It Dv BUS_DMA_WAITOK
466 Indicates that it is OK to wait for resources.
467 However,
468 unlike
469 .Xr kmalloc 9 ,
470 it is not guaranteed that the resource allocation will succeed.
471 This flag is the default one,
472 if
473 .Dv BUS_DMA_NOWAIT
474 is not supplied.
475 .It Dv BUS_DMA_NOWAIT
476 If the resource allocation request cannot be immediately fulfilled,
477 .Er ENOMEM
478 is returned.
479 .It Dv BUS_DMA_ONEBPAGE
480 Allocte one bounce page at most,
481 even if the
482 .Fa maxsize
483 used to create the
484 .Fa dmat
485 indicates that multiple bounce pages are needed.
486 .El
487 .It Fa mapp
488 Pointer to a
489 .Vt bus_dmamap_t
490 where the resulting DMA map will be stored.
491 .El
492 .Pp
493 Returns
494 .Er ENOMEM
495 if sufficient memory is not available for creating the
496 map or allocating mapping resources.
497 .It Fn bus_dmamap_destroy "dmat" "map"
498 Frees all resources associated with a given DMA map.
499 Arguments are as follows:
500 .Bl -tag -width dmat -compact
501 .It Fa dmat
502 DMA tag used to allocate
503 .Fa map .
504 .It Fa map
505 The DMA map to destroy.
506 .El
507 .Pp
508 Returns
509 .Er EBUSY
510 if a mapping is still active for
511 .Fa map .
512 .It Fn bus_dmamap_load "dmat" "map" "buf" "buflen" "*callback" "..."
513 Creates a mapping in device visible address space of
514 .Fa buflen
515 bytes of
516 .Fa buf ,
517 associated with the DMA map
518 .Fa map .
519 Arguments are as follows:
520 .Bl -tag -width buflen -compact
521 .It Fa dmat
522 DMA tag used to allocate
523 .Fa map .
524 .It Fa map
525 A DMA map without a currently active mapping.
526 .It Fa buf
527 A kernel virtual address pointer to a contiguous (in KVA) buffer, to be
528 mapped into device visible address space.
529 .It Fa buflen
530 The size of the buffer.
531 .It Fa callback Fa callback_arg
532 The callback function, and its argument.
533 .It Fa flags
534 The value of this argument is currently undefined, and should be
535 specified as
536 .Ql 0 .
537 .El
538 .Pp
539 Return values to the caller are as follows:
540 .Bl -tag -width ".Er EINPROGRESS" -compact
541 .It 0
542 The callback has been called and completed.
543 The status of the mapping has been delivered to the callback.
544 .It Er EINPROGRESS
545 The mapping has been deferred for lack of resources.
546 The callback will be called as soon as resources are available.
547 Callbacks are serviced in FIFO order.
548 DMA maps created from DMA tags that are allocated with
549 the
550 .Dv BUS_DMA_ALLOCNOW
551 flag will never return this status for a load operation.
552 .It Er EINVAL
553 The load request was invalid.
554 The callback has not, and will not be called.
555 This error value may indicate that
556 .Fa dmat ,
557 .Fa map ,
558 .Fa buf ,
559 or
560 .Fa callback
561 were invalid, or
562 .Fa buslen
563 was larger than the
564 .Fa maxsize
565 argument used to create the dma tag
566 .Fa dmat .
567 .El
568 .Pp
569 When the callback is called, it is presented with an error value
570 indicating the disposition of the mapping.
571 Error may be one of the following:
572 .Bl -tag -width ".Er EINPROGRESS" -compact
573 .It 0
574 The mapping was successful and the
575 .Fa dm_segs
576 callback argument contains an array of
577 .Vt bus_dma_segment_t
578 elements describing the mapping.
579 This array is only valid during the scope of the callback function.
580 .It Er EFBIG
581 A mapping could not be achieved within the segment constraints provided
582 in the tag even though the requested allocation size was less than maxsize.
583 .El
584 .It Fn bus_dmamap_load_mbuf "dmat" "map" "mbuf" "callback2" "callback_arg" \
585 "flags"
586 This is a variation of
587 .Fn bus_dmamap_load
588 which maps mbuf chains
589 for DMA transfers.
590 A
591 .Vt bus_size_t
592 argument is also passed to the callback routine, which
593 contains the mbuf chain's packet header length.
594 .Pp
595 Mbuf chains are assumed to be in kernel virtual address space.
596 .Pp
597 Returns
598 .Er EINVAL
599 if the size of the mbuf chain exceeds the maximum limit of the
600 DMA tag.
601 .It Fn bus_dmamap_load_mbuf_segment "dmat" "map" "mbuf" "*segs" "maxsegs" \
602 "*nsegs" "flags"
603 It is like
604 .Fn bus_dmamap_load_mbuf
605 without callback.
606 Segmentation information are saved in the
607 .Fa segs
608 and
609 .Fa nsegs
610 if the loading is successful.
611 The
612 .Fa maxsegs ,
613 which indicates the number of elements in the
614 .Fa segs ,
615 must be set by the caller and must be at least 1 but less than the
616 .Fa nsegments
617 used to create the
618 .Fa dmat .
619 The
620 .Fa flags
621 must have
622 .Dv BUS_DMA_NOWAIT
623 turned on.
624 .Pp
625 This function will not block.
626 When system is short of DMA resources,
627 this function will return
628 .Er ENOMEM ,
629 instead of
630 .Er EINPROGRESS .
631 .It Fn bus_dmamap_load_mbuf_defrag "dmat" "map" "*mbuf" "*segs" "maxsegs" \
632 "*nsegs" "flags"
633 This function is like
634 .Fn bus_dmamap_load_mbuf_segment ,
635 but it will call
636 .Fn m_defrag
637 on the
638 .Fa *mbuf
639 and try reloading,
640 if low level code indicates too many fragments in the
641 .Fa *mbuf ;
642 the
643 .Fa mbuf
644 will be updated under this situation.
645 However,
646 .Fa *mbuf
647 would not be freed by this function,
648 even if
649 .Fn m_defrag
650 failed.
651 .Pp
652 Return
653 .Er ENOBUFS ,
654 if the calling of
655 .Fn m_defrag
656 failed.
657 .It Fn bus_dmamap_load_uio "dmat" "map" "uio" "callback2" "callback_arg" "flags"
658 This is a variation of
659 .Fn bus_dmamap_load
660 which maps buffers pointed to by
661 .Fa uio
662 for DMA transfers.
663 A
664 .Vt bus_size_t
665 argument is also passed to the callback routine, which contains the size of
666 .Fa uio ,
667 i.e.
668 .Fa uio->uio_resid .
669 .Pp
670 If
671 .Fa uio->uio_segflg
672 is
673 .Dv UIO_USERSPACE ,
674 then it is assumed that the buffer,
675 .Fa uio
676 is in
677 .Fa "uio->uio_td->td_proc" Ns 's
678 address space.
679 User space memory must be in-core and wired prior to attempting a map
680 load operation.
681 .It Fn bus_dmamap_unload "dmat" "map"
682 Unloads a DMA map.
683 Arguments are as follows:
684 .Bl -tag -width dmam -compact
685 .It Fa dmat
686 DMA tag used to allocate
687 .Fa map .
688 .It Fa map
689 The DMA map that is to be unloaded.
690 .El
691 .Pp
692 .Fn bus_dmamap_unload
693 will not perform any implicit synchronization of DMA buffers.
694 This must be done explicitly by a call to
695 .Fn bus_dmamap_sync
696 prior to unloading the map.
697 .It Fn bus_dmamap_sync "dmat" "map" "op"
698 Performs synchronization of a device visible mapping with the CPU visible
699 memory referenced by that mapping.
700 Arguments are as follows:
701 .Bl -tag -width dmat -compact
702 .It Fa dmat
703 DMA tag used to allocate
704 .Fa map .
705 .It Fa map
706 The DMA mapping to be synchronized.
707 .It Fa op
708 Type of synchronization operation to perform.
709 See the definition of
710 .Vt bus_dmasync_op_t
711 for a description of the acceptable values for
712 .Fa op .
713 .El
714 .Pp
715 .Fn bus_dmamap_sync
716 is the method used to ensure that CPU and device DMA access to shared
717 memory is coherent.
718 For example, the CPU might be used to setup the contents of a buffer
719 that is to be DMA'ed into a device.
720 To ensure that the data are visible via the device's mapping of that
721 memory, the buffer must be loaded and a dma sync operation of
722 .Dv BUS_DMASYNC_PREREAD
723 must be performed.
724 Additional sync operations must be performed after every CPU write
725 to this memory if additional DMA reads are to be performed.
726 Conversely, for the DMA write case, the buffer must be loaded,
727 and a dma sync operation of
728 .Dv BUS_DMASYNC_PREWRITE
729 must be performed.
730 The CPU will only be able to see the results of this DMA write
731 once the DMA has completed and a
732 .Dv BUS_DMASYNC_POSTWRITE
733 operation has been performed.
734 .Pp
735 If DMA read and write operations are not preceded and followed by the
736 appropriate synchronization operations, behavior is undefined.
737 .It Fn bus_dmamem_alloc "dmat" "**vaddr" "flags" "mapp"
738 Allocates memory that is mapped into KVA at the address returned
739 in
740 .Fa vaddr
741 that is permanently loaded into the newly created
742 .Vt bus_dmamap_t
743 returned via
744 .Fa mapp .
745 Arguments are as follows:
746 .Bl -tag -width alignment -compact
747 .It Fa dmat
748 DMA tag describing the constraints of the DMA mapping.
749 .It Fa vaddr
750 Pointer to a pointer that will hold the returned KVA mapping of
751 the allocated region.
752 .It Fa flags
753 Flags are defined as follows:
754 .Bl -tag -width ".Dv BUS_DMA_NOWAIT" -compact
755 .It Dv BUS_DMA_WAITOK
756 The routine can safely wait (sleep) for resources.
757 .It Dv BUS_DMA_NOWAIT
758 The routine is not allowed to wait for resources.
759 If resources are not available,
760 .Er ENOMEM
761 is returned.
762 .It Dv BUS_DMA_COHERENT
763 Attempt to map this memory such that cache sync operations are
764 as cheap as possible.
765 This flag is typically set on memory that will be accessed by both
766 a CPU and a DMA engine, frequently.
767 Use of this flag does not remove the requirement of using
768 bus_dmamap_sync, but it may reduce the cost of performing
769 these operations.
770 .It Dv BUS_DMA_ZERO
771 Causes the allocated memory to be set to all zeros.
772 .El
773 .It Fa mapp
774 Pointer to storage for the returned DMA map.
775 .El
776 .Pp
777 The size of memory to be allocated is
778 .Fa maxsize
779 as specified in
780 .Fa dmat .
781 .Pp
782 The current implementation of
783 .Fn bus_dmamem_alloc
784 will allocate all requests as a single segment.
785 .Pp
786 Although no explicit loading is required to access the memory
787 referenced by the returned map, the synchronization requirements
788 as described in the
789 .Fn bus_dmamap_sync
790 section still apply.
791 .Pp
792 Returns
793 .Er ENOMEM
794 if sufficient memory is not available for completing
795 the operation.
796 .It Fn bus_dmamem_coherent "parent" "alignment" "boundary" "lowaddr" \
797 "highaddr" "maxsize" "flags" "*dmem"
798 This is a convenient function to create one segment of DMA memory.
799 It combines following
800 .Xr bus_dma 9
801 function calls:
802 .Bd -literal
803         bus_dma_tag_create(..., dtag);
804         bus_dmamem_alloc(*dtag, vaddr, ..., dmap);
805         bus_dmamap_load(*dtag, *dmap, *vaddr, ..., \\
806                         callback, busaddr, ...);
807 .Ed
808 .sp
809 The final results of the above function calls are:
810 DMA tag,
811 DMA map,
812 DMA memory's kernel virtual address and
813 its device visible address.
814 .Fn bus_dmamem_coherent
815 saves the results in
816 .Fa *dmem .
817 .Pp
818 The
819 .Fa parent ,
820 .Fa alignment ,
821 .Fa boundary ,
822 .Fa lowaddr
823 and
824 .Fa highaddr
825 will be passed to
826 .Fn bus_dma_tag_create
827 as they are.
828 The
829 .Fa maxsize
830 will be passed to
831 .Fn bus_dma_tag_create
832 as its
833 .Fa maxsize
834 and
835 .Fa maxsegsz
836 and
837 .Ql 1
838 will be passed to
839 .Fn bus_dma_tag_create
840 as its
841 .Fa nsegments .
842 When
843 .Fn bus_dmamem_alloc
844 is called,
845 .Fa flags
846 will be first or'ed with
847 .Dv BUS_DMA_COHERENT
848 then passed to it.
849 The final results of the above three functions,
850 i.e. DMA tag,
851 DMA map,
852 DMA memory's kernel virtual address and
853 its device visible address,
854 are saved in
855 .Fa *dmem .
856 If any of the three functions failed,
857 this function will return the error code and the
858 .Fa *dmem
859 should not be used.
860 .It Fn bus_dmamem_coherent_any "parent" "alignment" "maxsize" "flags" \
861 "*dtag" "*dmap" "*busaddr"
862 This function is a simplified version of
863 .Fn bus_dmamem_coherent
864 with
865 its
866 .Fa boundary
867 set to
868 .Ql 0 ,
869 .Fa lowaddr
870 set to
871 .Dv BUS_SPACE_MAXADDR
872 and
873 .Fa highaddr
874 set to
875 .Dv BUS_SPACE_MAXADDR .
876 The
877 .Fa parent
878 usually should not be NULL.
879 .Pp
880 Return the DMA memory's kernel virtual address.
881 The DMA tag, DMA map and device visible address are returned in
882 .Fa *dtag ,
883 .Fa *dmap ,
884 and
885 .Fa *busaddr .
886 If this function failed,
887 NULL will be returned;
888 .Fa *dtag ,
889 .Fa *dmap ,
890 and
891 .Fa *busaddr
892 are left unchanged.
893 .It Fn bus_dmamem_free "dmat" "*vaddr" "map"
894 Frees memory previously allocated by
895 .Fn bus_dmamem_alloc .
896 Any mappings
897 will be invalidated.
898 Arguments are as follows:
899 .Bl -tag -width vaddr -compact
900 .It Fa dmat
901 DMA tag.
902 .It Fa vaddr
903 Kernel virtual address of the memory.
904 .It Fa map
905 DMA map to be invalidated.
906 .El
907 .El
908 .Sh RETURN VALUES
909 Behavior is undefined if invalid arguments are passed to
910 any of the above functions.
911 If sufficient resources cannot be allocated for a given
912 transaction,
913 .Er ENOMEM
914 is returned.
915 All
916 routines that are not of type,
917 .Vt void ,
918 will return 0 on success or an error
919 code, as discussed above.
920 .Pp
921 All
922 .Vt void
923 routines will succeed if provided with valid arguments.
924 .Sh SEE ALSO
925 .Xr devclass 9 ,
926 .Xr device 9 ,
927 .Xr driver 9 ,
928 .Xr rman 9
929 .Rs
930 .%A "Jason R. Thorpe"
931 .%T "A Machine-Independent DMA Framework for NetBSD"
932 .%J "Proceedings of the Summer 1998 USENIX Technical Conference"
933 .%Q "USENIX Association"
934 .%D "June 1998"
935 .Re
936 .Sh HISTORY
937 The
938 .Nm
939 interface first appeared in
940 .Nx 1.3 .
941 .Pp
942 The
943 .Nm
944 API was adopted from
945 .Nx
946 for use in the CAM SCSI subsystem.
947 The alterations to the original API were aimed to remove the need for
948 a
949 .Vt bus_dma_segment_t
950 array stored in each
951 .Vt bus_dmamap_t
952 while allowing callers to queue up on scarce resources.
953 .Sh AUTHORS
954 The
955 .Nm
956 interface was designed and implemented by
957 .An Jason R. Thorpe
958 of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
959 Additional input on the
960 .Nm
961 design was provided by
962 .An -nosplit
963 .An Chris Demetriou ,
964 .An Charles Hannum ,
965 .An Ross Harvey ,
966 .An Matthew Jacob ,
967 .An Jonathan Stone ,
968 and
969 .An Matt Thomas .
970 .Pp
971 This manual page was written by
972 .An Hiten Pandya
973 and
974 .An Justin T. Gibbs .