]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/share/man/man9/bus_dma.9
Clone Kip's Xen on stable/6 tree so that I can work on improving FreeBSD/amd64
[FreeBSD/FreeBSD.git] / 6 / 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, 2006
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 indent
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 indent
147 .It Ft int
148 .Fn "client_filter" "void *filtarg" "bus_addr_t testaddr"
149 .El
150 .Pp
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 .Pp
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 One map is used for each memory allocation that will be loaded.
188 Maps can be reused once they have been unloaded.
189 Multiple maps can be associated with one DMA tag.
190 While the value of the map may evaluate to
191 .Dv NULL
192 on some platforms under certain conditions,
193 it should never be assumed that it will be
194 .Dv NULL
195 in all cases.
196 .It Vt bus_dmamap_callback_t
197 Client specified callback for receiving mapping information resulting from
198 the load of a
199 .Vt bus_dmamap_t
200 via
201 .Fn bus_dmamap_load .
202 Callbacks are of the format:
203 .Bl -tag -width indent
204 .It Ft void
205 .Fn "client_callback" "void *callback_arg" "bus_dma_segment_t *segs" \
206 "int nseg" "int error"
207 .El
208 .Pp
209 The
210 .Fa callback_arg
211 is the callback argument passed to dmamap load functions.
212 The
213 .Fa segs
214 and
215 .Fa nseg
216 parameters describe an array of
217 .Vt bus_dma_segment_t
218 structures that represent the mapping.
219 This array is only valid within the scope of the callback function.
220 The success or failure of the mapping is indicated by the
221 .Fa error
222 parameter.
223 More information on the use of callbacks can be found in the
224 description of the individual dmamap load functions.
225 .It Vt bus_dmamap_callback2_t
226 Client specified callback for receiving mapping information resulting from
227 the load of a
228 .Vt bus_dmamap_t
229 via
230 .Fn bus_dmamap_load_uio
231 or
232 .Fn bus_dmamap_load_mbuf .
233 .Pp
234 Callback2s are of the format:
235 .Bl -tag -width indent
236 .It Ft void
237 .Fn "client_callback2" "void *callback_arg" "bus_dma_segment_t *segs" \
238 "int nseg" "bus_size_t mapsize" "int error"
239 .El
240 .Pp
241 Callback2's behavior is the same as
242 .Vt bus_dmamap_callback_t
243 with the addition that the length of the data mapped is provided via
244 .Fa mapsize .
245 .It Vt bus_dmasync_op_t
246 Memory synchronization operation specifier.
247 Bus DMA requires explicit synchronization of memory with its device
248 visible mapping in order to guarantee memory coherency.
249 The
250 .Vt bus_dmasync_op_t
251 allows the type of DMA operation that will be or has been performed
252 to be communicated to the system so that the correct coherency measures
253 are taken.
254 The operations are represented as bitfield flags that can be combined together,
255 though it only makes sense to combine PRE flags or POST flags, not both.
256 See the
257 .Fn bus_dmamap_sync
258 description below for more details on how to use these operations.
259 .Pp
260 All operations specified below are performed from the host memory point of view,
261 where a read implies data coming from the device to the host memory, and a write
262 implies data going from the host memory to the device.
263 Alternatively, the operations can be thought of in terms of driver operations,
264 where reading a network packet or storage sector corresponds to a read operation
265 in
266 .Nm .
267 .Bl -tag -width ".Dv BUS_DMASYNC_POSTWRITE"
268 .It Dv BUS_DMASYNC_PREREAD
269 Perform any synchronization required prior to an update of host memory by the
270 device.
271 .It Dv BUS_DMASYNC_PREWRITE
272 Perform any synchronization required after an update of host memory by the CPU
273 and prior to device access to host memory.
274 .It Dv BUS_DMASYNC_POSTREAD
275 Perform any synchronization required after an update of host memory by the
276 device and prior to CPU access to host memory.
277 .It Dv BUS_DMASYNC_POSTWRITE
278 Perform any synchronization required after the device access to host memory.
279 .El
280 .It Vt bus_dma_lock_t
281 Client specified lock/mutex manipulation method.
282 This will be called from
283 within busdma whenever a client lock needs to be manipulated.
284 In its current form, the function will be called immediately before
285 the callback for a dma load operation that has been deferred with
286 .Dv BUS_DMA_LOCK
287 and immediately after with
288 .Dv BUS_DMA_UNLOCK .
289 If the load operation does not need to be deferred, then it
290 will not be called since the function loading the map should
291 be holding the appropriate locks.
292 This method is of the format:
293 .Bl -tag -width indent
294 .It Ft void
295 .Fn "lockfunc" "void *lockfunc_arg" "bus_dma_lock_op_t op"
296 .El
297 .Pp
298 Two
299 .Vt lockfunc
300 implementations are provided for convenience.
301 .Fn busdma_lock_mutex
302 performs standard mutex operations on the sleep mutex provided via the
303 .Fa lockfuncarg .
304 passed into
305 .Fn bus_dma_tag_create .
306 .Fn dflt_lock
307 will generate a system panic if it is called.
308 It is substituted into the tag when
309 .Fa lockfunc
310 is passed as
311 .Dv NULL
312 to
313 .Fn bus_dma_tag_create .
314 .It Vt bus_dma_lock_op_t
315 Operations to be performed by the client-specified
316 .Fn lockfunc .
317 .Bl -tag -width ".Dv BUS_DMA_UNLOCK"
318 .It Dv BUS_DMA_LOCK
319 Acquires and/or locks the client locking primitive.
320 .It Dv BUS_DMA_UNLOCK
321 Releases and/or unlocks the client locking primitive.
322 .El
323 .El
324 .Sh FUNCTIONS
325 .Bl -tag -width indent
326 .It Fn bus_dma_tag_create "parent" "alignment" "boundary" "lowaddr" \
327 "highaddr" "*filtfunc" "*filtfuncarg" "maxsize" "nsegments" "maxsegsz" \
328 "flags" "lockfunc" "lockfuncarg" "*dmat"
329 Allocates a device specific DMA tag, and initializes it according to
330 the arguments provided:
331 .Bl -tag -width ".Fa filtfuncarg"
332 .It Fa parent
333 Indicates restrictions between the parent bridge, CPU memory, and the
334 device.
335 May be NULL, if no DMA restrictions are to be inherited.
336 .It Fa alignment
337 Alignment constraint, in bytes, of any mappings created using this tag.
338 The alignment must be a power of 2.
339 Hardware that can DMA starting at any address would specify
340 .Em 1
341 for byte alignment.
342 Hardware requiring DMA transfers to start on a multiple of 4K
343 would specify
344 .Em 4096 .
345 .It Fa boundary
346 Boundary constraint, in bytes, of the target DMA memory region.
347 The boundary indicates the set of addresses, all multiples of the
348 boundary argument, that cannot be crossed by a single
349 .Vt bus_dma_segment_t .
350 The boundary must be a power of 2 and must be no smaller than the
351 maximum segment size.
352 .Ql 0
353 indicates that there are no boundary restrictions.
354 .It Fa lowaddr , highaddr
355 Bounds of the window of bus address space that
356 .Em cannot
357 be directly accessed by the device.
358 The window contains all addresses greater than lowaddr and
359 less than or equal to highaddr.
360 For example, a device incapable of DMA above 4GB, would specify
361 a highaddr of
362 .Dv BUS_SPACE_MAXADDR
363 and a lowaddr of
364 .Dv BUS_SPACE_MAXADDR_32BIT .
365 Similarly a device that can only dma to addresses bellow 16MB would
366 specify a highaddr of
367 .Dv BUS_SPACE_MAXADDR
368 and a lowaddr of
369 .Dv BUS_SPACE_MAXADDR_24BIT .
370 Some implementations requires that some region of device visible
371 address space, overlapping available host memory, be outside the
372 window.
373 This area of
374 .Ql safe memory
375 is used to bounce requests that would otherwise conflict with
376 the exclusion window.
377 .It Fa filtfunc
378 Optional filter function (may be
379 .Dv NULL )
380 to be called for any attempt to
381 map memory into the window described by
382 .Fa lowaddr
383 and
384 .Fa highaddr .
385 A filter function is only required when the single window described
386 by
387 .Fa lowaddr
388 and
389 .Fa highaddr
390 cannot adequately describe the constraints of the device.
391 The filter function will be called for every machine page
392 that overlaps the exclusion window.
393 .It Fa filtfuncarg
394 Argument passed to all calls to the filter function for this tag.
395 May be
396 .Dv NULL .
397 .It Fa maxsize
398 Maximum size, in bytes, of the sum of all segment lengths in a given
399 DMA mapping associated with this tag.
400 .It Fa nsegments
401 Number of discontinuities (scatter/gather segments) allowed
402 in a DMA mapped region.
403 If there is no restriction,
404 .Dv BUS_SPACE_UNRESTRICTED
405 may be specified.
406 .It Fa maxsegsz
407 Maximum size, in bytes, of a segment in any DMA mapped region associated
408 with
409 .Fa dmat .
410 .It Fa flags
411 Are as follows:
412 .Bl -tag -width ".Dv BUS_DMA_ALLOCNOW"
413 .It Dv BUS_DMA_ALLOCNOW
414 Pre-allocate enough resources to handle at least one map load operation on
415 this tag.
416 If sufficient resources are not available,
417 .Er ENOMEM
418 is returned.
419 This should not be used for tags that only describe buffers that will be
420 allocated with
421 .Fn bus_dmamem_alloc .
422 Also, due to resource sharing with other tags, this flag does not guarantee
423 that resources will be allocated or reserved exclusively for this tag.
424 It should be treated only as a minor optimization.
425 .El
426 .It Fa lockfunc
427 Optional lock manipulation function (may be
428 .Dv NULL )
429 to be called when busdma
430 needs to manipulate a lock on behalf of the client.
431 If
432 .Dv NULL
433 is specified,
434 .Fn dflt_lock
435 is used.
436 .It Fa lockfuncarg
437 Optional argument to be passed to the function specified by
438 .Fa lockfunc .
439 .It Fa dmat
440 Pointer to a bus_dma_tag_t where the resulting DMA tag will
441 be stored.
442 .El
443 .Pp
444 Returns
445 .Er ENOMEM
446 if sufficient memory is not available for tag creation
447 or allocating mapping resources.
448 .It Fn bus_dma_tag_destroy "dmat"
449 Deallocate the DMA tag
450 .Fa dmat
451 that was created by
452 .Fn bus_dma_tag_create .
453 .Pp
454 Returns
455 .Er EBUSY
456 if any DMA maps remain associated with
457 .Fa dmat
458 or
459 .Ql 0
460 on success.
461 .It Fn bus_dmamap_create "dmat" "flags" "*mapp"
462 Allocates and initializes a DMA map.
463 Arguments are as follows:
464 .Bl -tag -width ".Fa nsegments"
465 .It Fa dmat
466 DMA tag.
467 .It Fa flags
468 The value of this argument is currently undefined and should be
469 specified as
470 .Ql 0 .
471 .It Fa mapp
472 Pointer to a
473 .Vt bus_dmamap_t
474 where the resulting DMA map will be stored.
475 .El
476 .Pp
477 Returns
478 .Er ENOMEM
479 if sufficient memory is not available for creating the
480 map or allocating mapping resources.
481 .It Fn bus_dmamap_destroy "dmat" "map"
482 Frees all resources associated with a given DMA map.
483 Arguments are as follows:
484 .Bl -tag -width ".Fa dmat"
485 .It Fa dmat
486 DMA tag used to allocate
487 .Fa map .
488 .It Fa map
489 The DMA map to destroy.
490 .El
491 .Pp
492 Returns
493 .Er EBUSY
494 if a mapping is still active for
495 .Fa map .
496 .It Fn bus_dmamap_load "dmat" "map" "buf" "buflen" "*callback" \
497 "callback_arg" "flags"
498 Creates a mapping in device visible address space of
499 .Fa buflen
500 bytes of
501 .Fa buf ,
502 associated with the DMA map
503 .Fa map .
504 This call will always return immediately and will not block for any reason.
505 Arguments are as follows:
506 .Bl -tag -width ".Fa buflen"
507 .It Fa dmat
508 DMA tag used to allocate
509 .Fa map .
510 .It Fa map
511 A DMA map without a currently active mapping.
512 .It Fa buf
513 A kernel virtual address pointer to a contiguous (in KVA) buffer, to be
514 mapped into device visible address space.
515 .It Fa buflen
516 The size of the buffer.
517 .It Fa callback Fa callback_arg
518 The callback function, and its argument.
519 This function is called once sufficient mapping resources are available for
520 the DMA operation.
521 If resources are temporarily unavailable, this function will be deferred until
522 later, but the load operation will still return immediately to the caller.
523 Thus, callers should not assume that the callback will be called before the
524 load returns, and code should be structured appropriately to handle this.
525 See below for specific flags and error codes that control this behavior.
526 .It Fa flags
527 Are as follows:
528 .Bl -tag -width ".Dv BUS_DMA_NOWAIT"
529 .It Dv BUS_DMA_NOWAIT
530 The load should not be deferred in case of insufficient mapping resources,
531 and instead should return immediately with an appropriate error.
532 .El
533 .El
534 .Pp
535 Return values to the caller are as follows:
536 .Bl -tag -width ".Er EINPROGRESS"
537 .It 0
538 The callback has been called and completed.
539 The status of the mapping has been delivered to the callback.
540 .It Er EINPROGRESS
541 The mapping has been deferred for lack of resources.
542 The callback will be called as soon as resources are available.
543 Callbacks are serviced in FIFO order.
544 To ensure that ordering is guaranteed, all subsequent load requests will also
545 be deferred until all callbacks have been processed.
546 .It Er ENOMEM
547 The load request has failed due to insufficient resources, and the caller
548 specifically used the
549 .Dv BUS_DMA_NOWAIT
550 flag.
551 .It Er EINVAL
552 The load request was invalid.
553 The callback has been called and has been provided the same error.
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 buflen
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"
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 The
594 .Dv BUS_DMA_NOWAIT
595 flag is implied, thus no callback deferral will happen.
596 .Pp
597 Mbuf chains are assumed to be in kernel virtual address space.
598 .Pp
599 Beside the error values listed for
600 .Fn bus_dmamap_load ,
601 .Er EINVAL
602 will be returned if the size of the mbuf chain exceeds the maximum limit of the
603 DMA tag.
604 .It Fn bus_dmamap_load_mbuf_sg "dmat" "map" "mbuf" "segs" "nsegs" "flags"
605 This is just like
606 .Fn bus_dmamap_load_mbuf
607 except that it returns immediately without calling a callback function.
608 It is provided for efficiency.
609 The scatter/gather segment array
610 .Va segs
611 is provided by the caller and filled in directly by the function.
612 The
613 .Va nsegs
614 argument is returned with the number of segments filled in.
615 Returns the same errors as
616 .Fn bus_dmamap_load_mbuf .
617 .It Fn bus_dmamap_load_uio "dmat" "map" "uio" "callback2" "callback_arg" "flags"
618 This is a variation of
619 .Fn bus_dmamap_load
620 which maps buffers pointed to by
621 .Fa uio
622 for DMA transfers.
623 A
624 .Vt bus_size_t
625 argument is also passed to the callback routine, which contains the size of
626 .Fa uio ,
627 i.e.
628 .Fa uio->uio_resid .
629 The
630 .Dv BUS_DMA_NOWAIT
631 flag is implied, thus no callback deferral will happen.
632 Returns the same errors as
633 .Fn bus_dmamap_load .
634 .Pp
635 If
636 .Fa uio->uio_segflg
637 is
638 .Dv UIO_USERSPACE ,
639 then it is assumed that the buffer,
640 .Fa uio
641 is in
642 .Fa "uio->uio_td->td_proc" Ns 's
643 address space.
644 User space memory must be in-core and wired prior to attempting a map
645 load operation.
646 Pages may be locked using
647 .Xr vslock 9 .
648 .It Fn bus_dmamap_unload "dmat" "map"
649 Unloads a DMA map.
650 Arguments are as follows:
651 .Bl -tag -width ".Fa dmam"
652 .It Fa dmat
653 DMA tag used to allocate
654 .Fa map .
655 .It Fa map
656 The DMA map that is to be unloaded.
657 .El
658 .Pp
659 .Fn bus_dmamap_unload
660 will not perform any implicit synchronization of DMA buffers.
661 This must be done explicitly by a call to
662 .Fn bus_dmamap_sync
663 prior to unloading the map.
664 .It Fn bus_dmamap_sync "dmat" "map" "op"
665 Performs synchronization of a device visible mapping with the CPU visible
666 memory referenced by that mapping.
667 Arguments are as follows:
668 .Bl -tag -width ".Fa dmat"
669 .It Fa dmat
670 DMA tag used to allocate
671 .Fa map .
672 .It Fa map
673 The DMA mapping to be synchronized.
674 .It Fa op
675 Type of synchronization operation to perform.
676 See the definition of
677 .Vt bus_dmasync_op_t
678 for a description of the acceptable values for
679 .Fa op .
680 .El
681 .Pp
682 The
683 .Fn bus_dmamap_sync
684 function
685 is the method used to ensure that CPU's and device's direct
686 memory access (DMA) to shared
687 memory is coherent.
688 For example, the CPU might be used to set up the contents of a buffer
689 that is to be made available to a device.
690 To ensure that the data are visible via the device's mapping of that
691 memory, the buffer must be loaded and a DMA sync operation of
692 .Dv BUS_DMASYNC_PREWRITE
693 must be performed.
694 If later CPU modifies this buffer again, another
695 .Dv BUS_DMASYNC_PREWRITE
696 sync operation must be performed before an additional
697 access to this memory by a device.
698 Conversely, a device updates the memory that is to be read by a CPU.
699 In this case, the buffer must be loaded, and a DMA sync operation of
700 .Dv BUS_DMASYNC_PREREAD
701 must be performed.
702 The CPU will only be able to see the results of this memory update
703 once the DMA operation has completed and a
704 .Dv BUS_DMASYNC_POSTREAD
705 sync operation has been performed.
706 .Pp
707 If read and write operations are not preceded and followed by the
708 appropriate synchronization operations, behavior is undefined.
709 .It Fn bus_dmamem_alloc "dmat" "**vaddr" "flags" "*mapp"
710 Allocates memory that is mapped into KVA at the address returned
711 in
712 .Fa vaddr
713 that is permanently loaded into the newly created
714 .Vt bus_dmamap_t
715 returned via
716 .Fa mapp .
717 Arguments are as follows:
718 .Bl -tag -width ".Fa alignment"
719 .It Fa dmat
720 DMA tag describing the constraints of the DMA mapping.
721 .It Fa vaddr
722 Pointer to a pointer that will hold the returned KVA mapping of
723 the allocated region.
724 .It Fa flags
725 Flags are defined as follows:
726 .Bl -tag -width ".Dv BUS_DMA_NOWAIT"
727 .It Dv BUS_DMA_WAITOK
728 The routine can safely wait (sleep) for resources.
729 .It Dv BUS_DMA_NOWAIT
730 The routine is not allowed to wait for resources.
731 If resources are not available,
732 .Dv ENOMEM
733 is returned.
734 .It Dv BUS_DMA_COHERENT
735 Attempt to map this memory such that cache sync operations are
736 as cheap as possible.
737 This flag is typically set on memory that will be accessed by both
738 a CPU and a DMA engine, frequently.
739 Use of this flag does not remove the requirement of using
740 bus_dmamap_sync, but it may reduce the cost of performing
741 these operations.
742 The
743 .Dv BUS_DMA_COHERENT
744 flag is currently implemented on sparc64 and arm.
745 .It Dv BUS_DMA_ZERO
746 Causes the allocated memory to be set to all zeros.
747 .El
748 .It Fa mapp
749 Pointer to a
750 .Vt bus_dmamap_t
751 where the resulting DMA map will be stored.
752 .El
753 .Pp
754 The size of memory to be allocated is
755 .Fa maxsize
756 as specified in
757 .Fa dmat .
758 .Pp
759 The current implementation of
760 .Fn bus_dmamem_alloc
761 will allocate all requests as a single segment.
762 .Pp
763 An initial load operation is required to obtain the bus address of the allocated
764 memory, and an unload operation is required before freeing the memory, as
765 described below in
766 .Fn bus_dmamem_free .
767 Maps are automatically handled by this function and should not be explicitly
768 allocated or destroyed.
769 .Pp
770 Although an explicit load is not required for each access to the memory
771 referenced by the returned map, the synchronization requirements
772 as described in the
773 .Fn bus_dmamap_sync
774 section still apply and should be used to achieve portability on architectures
775 without coherent buses.
776 .Pp
777 Returns
778 .Er ENOMEM
779 if sufficient memory is not available for completing
780 the operation.
781 .It Fn bus_dmamem_free "dmat" "*vaddr" "map"
782 Frees memory previously allocated by
783 .Fn bus_dmamem_alloc .
784 Any mappings
785 will be invalidated.
786 Arguments are as follows:
787 .Bl -tag -width ".Fa vaddr"
788 .It Fa dmat
789 DMA tag.
790 .It Fa vaddr
791 Kernel virtual address of the memory.
792 .It Fa map
793 DMA map to be invalidated.
794 .El
795 .El
796 .Sh RETURN VALUES
797 Behavior is undefined if invalid arguments are passed to
798 any of the above functions.
799 If sufficient resources cannot be allocated for a given
800 transaction,
801 .Er ENOMEM
802 is returned.
803 All
804 routines that are not of type,
805 .Vt void ,
806 will return 0 on success or an error
807 code, as discussed above.
808 .Pp
809 All
810 .Vt void
811 routines will succeed if provided with valid arguments.
812 .Sh LOCKING
813 Two locking protocols are used by
814 .Nm .
815 The first is a private global lock that is used to synchronize access to the
816 bounce buffer pool on the architectures that make use of them.
817 This lock is strictly a leaf lock that is only used internally to
818 .Nm
819 and is not exposed to clients of the API.
820 .Pp
821 The second protocol involves protecting various resources stored in the tag.
822 Since almost all
823 .Nm
824 operations are done through requests from the driver that created the tag,
825 the most efficient way to protect the tag resources is through the lock that
826 the driver uses.
827 In cases where
828 .Nm
829 acts on its own without being called by the driver, the lock primitive
830 specified in the tag is acquired and released automatically.
831 An example of this is when the
832 .Fn bus_dmamap_load
833 callback function is called from a deferred context instead of the driver
834 context.
835 This means that certain
836 .Nm
837 functions must always be called with the same lock held that is specified in the
838 tag.
839 These functions include:
840 .Pp
841 .Bl -item -offset indent -compact
842 .It
843 .Fn bus_dmamap_load
844 .It
845 .Fn bus_dmamap_load_uio
846 .It
847 .Fn bus_dmamap_load_mbuf
848 .It
849 .Fn bus_dmamap_load_mbuf_sg
850 .It
851 .Fn bus_dmamap_unload
852 .It
853 .Fn bus_dmamap_sync
854 .El
855 .Pp
856 There is one exception to this rule.
857 It is common practice to call some of these functions during driver start-up
858 without any locks held.
859 So long as there is a guarantee of no possible concurrent use of the tag by
860 different threads during this operation, it is safe to not hold a lock for
861 these functions.
862 .Pp
863 Certain
864 .Nm
865 operations should not be called with the driver lock held, either because
866 they are already protected by an internal lock, or because they might sleep
867 due to memory or resource allocation.
868 The following functions must not be
869 called with any non-sleepable locks held:
870 .Pp
871 .Bl -item -offset indent -compact
872 .It
873 .Fn bus_dma_tag_create
874 .It
875 .Fn bus_dmamap_create
876 .It
877 .Fn bus_dmamem_alloc
878 .El
879 .Pp
880 All other functions do not have a locking protocol and can thus be
881 called with or without and system or driver locks held.
882 .Sh SEE ALSO
883 .Xr devclass 9 ,
884 .Xr device 9 ,
885 .Xr driver 9 ,
886 .Xr rman 9 ,
887 .Xr vslock 9
888 .Pp
889 .Rs
890 .%A "Jason R. Thorpe"
891 .%T "A Machine-Independent DMA Framework for NetBSD"
892 .%J "Proceedings of the Summer 1998 USENIX Technical Conference"
893 .%Q "USENIX Association"
894 .%D "June 1998"
895 .Re
896 .Sh HISTORY
897 The
898 .Nm
899 interface first appeared in
900 .Nx 1.3 .
901 .Pp
902 The
903 .Nm
904 API was adopted from
905 .Nx
906 for use in the CAM SCSI subsystem.
907 The alterations to the original API were aimed to remove the need for
908 a
909 .Vt bus_dma_segment_t
910 array stored in each
911 .Vt bus_dmamap_t
912 while allowing callers to queue up on scarce resources.
913 .Sh AUTHORS
914 The
915 .Nm
916 interface was designed and implemented by
917 .An Jason R. Thorpe
918 of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
919 Additional input on the
920 .Nm
921 design was provided by
922 .An -nosplit
923 .An Chris Demetriou ,
924 .An Charles Hannum ,
925 .An Ross Harvey ,
926 .An Matthew Jacob ,
927 .An Jonathan Stone ,
928 and
929 .An Matt Thomas .
930 .Pp
931 The
932 .Nm
933 interface in
934 .Fx
935 benefits from the contributions of
936 .An Justin T. Gibbs ,
937 .An Peter Wemm ,
938 .An Doug Rabson ,
939 .An Matthew N. Dodd ,
940 .An Sam Leffler ,
941 .An Maxime Henrion ,
942 .An Jake Burkholder ,
943 .An Takahashi Yoshihiro ,
944 .An Scott Long
945 and many others.
946 .Pp
947 This manual page was written by
948 .An Hiten M. Pandya
949 and
950 .An Justin T. Gibbs .