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