]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - share/man/man9/bus_dma.9
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / share / man / man9 / bus_dma.9
1 .\" Copyright (c) 2002, 2003 Hiten M. Pandya.
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions, and the following disclaimer,
9 .\"    without modification, immediately at the beginning of the file.
10 .\" 2. The name of the author may not be used to endorse or promote products
11 .\"    derived from this software without specific prior written permission.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, CONTRIBUTORS OR THE
17 .\" VOICES IN HITEN PANDYA'S HEAD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19 .\" TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 .\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 .\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22 .\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 .\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 .\"
25 .\" Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc.
26 .\" All rights reserved.
27 .\"
28 .\" This code is derived from software contributed to The NetBSD Foundation
29 .\" by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
30 .\" NASA Ames Research Center.
31 .\"
32 .\" Redistribution and use in source and binary forms, with or without
33 .\" modification, are permitted provided that the following conditions
34 .\" are met:
35 .\" 1. Redistributions of source code must retain the above copyright
36 .\"    notice, this list of conditions and the following disclaimer.
37 .\" 2. Redistributions in binary form must reproduce the above copyright
38 .\"    notice, this list of conditions and the following disclaimer in the
39 .\"    documentation and/or other materials provided with the distribution.
40 .\"
41 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
42 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
43 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 .\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
45 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
46 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
47 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
48 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51 .\" POSSIBILITY OF SUCH DAMAGE.
52 .\"
53 .\" $FreeBSD$
54 .\" $NetBSD: bus_dma.9,v 1.25 2002/10/14 13:43:16 wiz Exp $
55 .\"
56 .Dd July 17, 2013
57 .Dt BUS_DMA 9
58 .Os
59 .Sh NAME
60 .Nm bus_dma ,
61 .Nm bus_dma_tag_create ,
62 .Nm bus_dma_tag_destroy ,
63 .Nm bus_dmamap_create ,
64 .Nm bus_dmamap_destroy ,
65 .Nm bus_dmamap_load ,
66 .Nm bus_dmamap_load_bio ,
67 .Nm bus_dmamap_load_ccb ,
68 .Nm bus_dmamap_load_mbuf ,
69 .Nm bus_dmamap_load_mbuf_sg ,
70 .Nm bus_dmamap_load_uio ,
71 .Nm bus_dmamap_unload ,
72 .Nm bus_dmamap_sync ,
73 .Nm bus_dmamem_alloc ,
74 .Nm bus_dmamem_free
75 .Nd Bus and Machine Independent DMA Mapping Interface
76 .Sh SYNOPSIS
77 .In machine/bus.h
78 .Ft int
79 .Fn bus_dma_tag_create "bus_dma_tag_t parent" "bus_size_t alignment" \
80 "bus_addr_t boundary" "bus_addr_t lowaddr" "bus_addr_t highaddr" \
81 "bus_dma_filter_t *filtfunc" "void *filtfuncarg" "bus_size_t maxsize" \
82 "int nsegments" "bus_size_t maxsegsz" "int flags" "bus_dma_lock_t *lockfunc" \
83 "void *lockfuncarg" "bus_dma_tag_t *dmat"
84 .Ft int
85 .Fn bus_dma_tag_destroy "bus_dma_tag_t dmat"
86 .Ft int
87 .Fn bus_dmamap_create "bus_dma_tag_t dmat" "int flags" "bus_dmamap_t *mapp"
88 .Ft int
89 .Fn bus_dmamap_destroy "bus_dma_tag_t dmat" "bus_dmamap_t map"
90 .Ft int
91 .Fn bus_dmamap_load "bus_dma_tag_t dmat" "bus_dmamap_t map" "void *buf" \
92 "bus_size_t buflen" "bus_dmamap_callback_t *callback" "void *callback_arg" \
93 "int flags"
94 .Ft int
95 .Fn bus_dmamap_load_bio "bus_dma_tag_t dmat" "bus_dmamap_t map" \
96 "struct bio *bio" "bus_dmamap_callback_t *callback" "void *callback_arg" \
97 "int flags"
98 .Ft int
99 .Fn bus_dmamap_load_ccb "bus_dma_tag_t dmat" "bus_dmamap_t map" \
100 "union ccb *ccb" "bus_dmamap_callback_t *callback" "void *callback_arg" \
101 "int flags"
102 .Ft int
103 .Fn bus_dmamap_load_mbuf "bus_dma_tag_t dmat" "bus_dmamap_t map" \
104 "struct mbuf *mbuf" "bus_dmamap_callback2_t *callback" "void *callback_arg" \
105 "int flags"
106 .Ft int
107 .Fn bus_dmamap_load_mbuf_sg "bus_dma_tag_t dmat" "bus_dmamap_t map" \
108 "struct mbuf *mbuf" "bus_dma_segment_t *segs" "int *nsegs" "int flags"
109 .Ft int
110 .Fn bus_dmamap_load_uio "bus_dma_tag_t dmat" "bus_dmamap_t map" \
111 "struct uio *uio" "bus_dmamap_callback2_t *callback" "void *callback_arg" \
112 "int flags"
113 .Ft void
114 .Fn bus_dmamap_unload "bus_dma_tag_t dmat" "bus_dmamap_t map"
115 .Ft void
116 .Fn bus_dmamap_sync "bus_dma_tag_t dmat" "bus_dmamap_t map" \
117 "op"
118 .Ft int
119 .Fn bus_dmamem_alloc "bus_dma_tag_t dmat" "void **vaddr" \
120 "int flags" "bus_dmamap_t *mapp"
121 .Ft void
122 .Fn bus_dmamem_free "bus_dma_tag_t dmat" "void *vaddr" \
123 "bus_dmamap_t map"
124 .Sh DESCRIPTION
125 Direct Memory Access (DMA) is a method of transferring data
126 without involving the CPU, thus providing higher performance.
127 A DMA transaction can be achieved between device to memory,
128 device to device, or memory to memory.
129 .Pp
130 The
131 .Nm
132 API is a bus, device, and machine-independent (MI) interface to
133 DMA mechanisms.
134 It provides the client with flexibility and simplicity by
135 abstracting machine dependent issues like setting up
136 DMA mappings, handling cache issues, bus specific features
137 and limitations.
138 .Sh STRUCTURES AND TYPES
139 .Bl -tag -width indent
140 .It Vt bus_dma_tag_t
141 A machine-dependent (MD) opaque type that describes the
142 characteristics of DMA transactions.
143 DMA tags are organized into a hierarchy, with each child
144 tag inheriting the restrictions of its parent.
145 This allows all devices along the path of DMA transactions
146 to contribute to the constraints of those transactions.
147 .It Vt bus_dma_filter_t
148 Client specified address filter having the format:
149 .Bl -tag -width indent
150 .It Ft int
151 .Fn "client_filter" "void *filtarg" "bus_addr_t testaddr"
152 .El
153 .Pp
154 Address filters can be specified during tag creation to allow
155 for devices whose DMA address restrictions cannot be specified
156 by a single window.
157 The
158 .Fa filtarg
159 argument is specified by the client during tag creation to be passed to all
160 invocations of the callback.
161 The
162 .Fa testaddr
163 argument contains a potential starting address of a DMA mapping.
164 The filter function operates on the set of addresses from
165 .Fa testaddr
166 to
167 .Ql trunc_page(testaddr) + PAGE_SIZE - 1 ,
168 inclusive.
169 The filter function should return zero if any mapping in this range
170 can be accommodated by the device and non-zero otherwise.
171 .It Vt bus_dma_segment_t
172 A machine-dependent type that describes individual
173 DMA segments.
174 It contains the following fields:
175 .Bd -literal
176         bus_addr_t      ds_addr;
177         bus_size_t      ds_len;
178 .Ed
179 .Pp
180 The
181 .Fa ds_addr
182 field contains the device visible address of the DMA segment, and
183 .Fa ds_len
184 contains the length of the DMA segment.
185 Although the DMA segments returned by a mapping call will adhere to
186 all restrictions necessary for a successful DMA operation, some conversion
187 (e.g.\& a conversion from host byte order to the device's byte order) is
188 almost always required when presenting segment information to the device.
189 .It Vt bus_dmamap_t
190 A machine-dependent opaque type describing an individual mapping.
191 One map is used for each memory allocation that will be loaded.
192 Maps can be reused once they have been unloaded.
193 Multiple maps can be associated with one DMA tag.
194 While the value of the map may evaluate to
195 .Dv NULL
196 on some platforms under certain conditions,
197 it should never be assumed that it will be
198 .Dv NULL
199 in all cases.
200 .It Vt bus_dmamap_callback_t
201 Client specified callback for receiving mapping information resulting from
202 the load of a
203 .Vt bus_dmamap_t
204 via
205 .Fn bus_dmamap_load ,
206 .Fn bus_dmamap_load_bio
207 or
208 .Fn bus_dmamap_load_ccb .
209 Callbacks are of the format:
210 .Bl -tag -width indent
211 .It Ft void
212 .Fn "client_callback" "void *callback_arg" "bus_dma_segment_t *segs" \
213 "int nseg" "int error"
214 .El
215 .Pp
216 The
217 .Fa callback_arg
218 is the callback argument passed to dmamap load functions.
219 The
220 .Fa segs
221 and
222 .Fa nseg
223 arguments describe an array of
224 .Vt bus_dma_segment_t
225 structures that represent the mapping.
226 This array is only valid within the scope of the callback function.
227 The success or failure of the mapping is indicated by the
228 .Fa error
229 argument.
230 More information on the use of callbacks can be found in the
231 description of the individual dmamap load functions.
232 .It Vt bus_dmamap_callback2_t
233 Client specified callback for receiving mapping information resulting from
234 the load of a
235 .Vt bus_dmamap_t
236 via
237 .Fn bus_dmamap_load_uio
238 or
239 .Fn bus_dmamap_load_mbuf .
240 .Pp
241 Callback2s are of the format:
242 .Bl -tag -width indent
243 .It Ft void
244 .Fn "client_callback2" "void *callback_arg" "bus_dma_segment_t *segs" \
245 "int nseg" "bus_size_t mapsize" "int error"
246 .El
247 .Pp
248 Callback2's behavior is the same as
249 .Vt bus_dmamap_callback_t
250 with the addition that the length of the data mapped is provided via
251 .Fa mapsize .
252 .It Vt bus_dmasync_op_t
253 Memory synchronization operation specifier.
254 Bus DMA requires explicit synchronization of memory with its device
255 visible mapping in order to guarantee memory coherency.
256 The
257 .Vt bus_dmasync_op_t
258 allows the type of DMA operation that will be or has been performed
259 to be communicated to the system so that the correct coherency measures
260 are taken.
261 The operations are represented as bitfield flags that can be combined together,
262 though it only makes sense to combine PRE flags or POST flags, not both.
263 See the
264 .Fn bus_dmamap_sync
265 description below for more details on how to use these operations.
266 .Pp
267 All operations specified below are performed from the host memory point of view,
268 where a read implies data coming from the device to the host memory, and a write
269 implies data going from the host memory to the device.
270 Alternatively, the operations can be thought of in terms of driver operations,
271 where reading a network packet or storage sector corresponds to a read operation
272 in
273 .Nm .
274 .Bl -tag -width ".Dv BUS_DMASYNC_POSTWRITE"
275 .It Dv BUS_DMASYNC_PREREAD
276 Perform any synchronization required prior to an update of host memory by the
277 device.
278 .It Dv BUS_DMASYNC_PREWRITE
279 Perform any synchronization required after an update of host memory by the CPU
280 and prior to device access to host memory.
281 .It Dv BUS_DMASYNC_POSTREAD
282 Perform any synchronization required after an update of host memory by the
283 device and prior to CPU access to host memory.
284 .It Dv BUS_DMASYNC_POSTWRITE
285 Perform any synchronization required after device access to host memory.
286 .El
287 .It Vt bus_dma_lock_t
288 Client specified lock/mutex manipulation method.
289 This will be called from
290 within busdma whenever a client lock needs to be manipulated.
291 In its current form, the function will be called immediately before
292 the callback for a DMA load operation that has been deferred with
293 .Dv BUS_DMA_LOCK
294 and immediately after with
295 .Dv BUS_DMA_UNLOCK .
296 If the load operation does not need to be deferred, then it
297 will not be called since the function loading the map should
298 be holding the appropriate locks.
299 This method is of the format:
300 .Bl -tag -width indent
301 .It Ft void
302 .Fn "lockfunc" "void *lockfunc_arg" "bus_dma_lock_op_t op"
303 .El
304 .Pp
305 The
306 .Fa lockfuncarg
307 argument is specified by the client during tag creation to be passed to all
308 invocations of the callback.
309 The
310 .Fa op
311 argument specifies the lock operation to perform.
312 .Pp
313 Two
314 .Vt lockfunc
315 implementations are provided for convenience.
316 .Fn busdma_lock_mutex
317 performs standard mutex operations on the sleep mutex provided via
318 .Fa lockfuncarg .
319 .Fn dflt_lock
320 will generate a system panic if it is called.
321 It is substituted into the tag when
322 .Fa lockfunc
323 is passed as
324 .Dv NULL
325 to
326 .Fn bus_dma_tag_create
327 and is useful for tags that should not be used with deferred load operations.
328 .It Vt bus_dma_lock_op_t
329 Operations to be performed by the client-specified
330 .Fn lockfunc .
331 .Bl -tag -width ".Dv BUS_DMA_UNLOCK"
332 .It Dv BUS_DMA_LOCK
333 Acquires and/or locks the client locking primitive.
334 .It Dv BUS_DMA_UNLOCK
335 Releases and/or unlocks the client locking primitive.
336 .El
337 .El
338 .Sh FUNCTIONS
339 .Bl -tag -width indent
340 .It Fn bus_dma_tag_create "parent" "alignment" "boundary" "lowaddr" \
341 "highaddr" "*filtfunc" "*filtfuncarg" "maxsize" "nsegments" "maxsegsz" \
342 "flags" "lockfunc" "lockfuncarg" "*dmat"
343 Allocates a device specific DMA tag, and initializes it according to
344 the arguments provided:
345 .Bl -tag -width ".Fa filtfuncarg"
346 .It Fa parent
347 Indicates restrictions between the parent bridge, CPU memory, and the
348 device.
349 Each device must use a master parent tag by calling
350 .Fn bus_get_dma_tag .
351 .It Fa alignment
352 Alignment constraint, in bytes, of any mappings created using this tag.
353 The alignment must be a power of 2.
354 Hardware that can DMA starting at any address would specify
355 .Em 1
356 for byte alignment.
357 Hardware requiring DMA transfers to start on a multiple of 4K
358 would specify
359 .Em 4096 .
360 .It Fa boundary
361 Boundary constraint, in bytes, of the target DMA memory region.
362 The boundary indicates the set of addresses, all multiples of the
363 boundary argument, that cannot be crossed by a single
364 .Vt bus_dma_segment_t .
365 The boundary must be a power of 2 and must be no smaller than the
366 maximum segment size.
367 .Ql 0
368 indicates that there are no boundary restrictions.
369 .It Fa lowaddr , highaddr
370 Bounds of the window of bus address space that
371 .Em cannot
372 be directly accessed by the device.
373 The window contains all addresses greater than
374 .Fa lowaddr
375 and less than or equal to
376 .Fa highaddr .
377 For example, a device incapable of DMA above 4GB, would specify a
378 .Fa highaddr
379 of
380 .Dv BUS_SPACE_MAXADDR
381 and a
382 .Fa lowaddr
383 of
384 .Dv BUS_SPACE_MAXADDR_32BIT .
385 Similarly a device that can only perform DMA to addresses below
386 16MB would specify a
387 .Fa highaddr
388 of
389 .Dv BUS_SPACE_MAXADDR
390 and a
391 .Fa lowaddr
392 of
393 .Dv BUS_SPACE_MAXADDR_24BIT .
394 Some implementations requires that some region of device visible
395 address space, overlapping available host memory, be outside the
396 window.
397 This area of
398 .Ql safe memory
399 is used to bounce requests that would otherwise conflict with
400 the exclusion window.
401 .It Fa filtfunc
402 Optional filter function (may be
403 .Dv NULL )
404 to be called for any attempt to
405 map memory into the window described by
406 .Fa lowaddr
407 and
408 .Fa highaddr .
409 A filter function is only required when the single window described
410 by
411 .Fa lowaddr
412 and
413 .Fa highaddr
414 cannot adequately describe the constraints of the device.
415 The filter function will be called for every machine page
416 that overlaps the exclusion window.
417 .It Fa filtfuncarg
418 Argument passed to all calls to the filter function for this tag.
419 May be
420 .Dv NULL .
421 .It Fa maxsize
422 Maximum size, in bytes, of the sum of all segment lengths in a given
423 DMA mapping associated with this tag.
424 .It Fa nsegments
425 Number of discontinuities (scatter/gather segments) allowed
426 in a DMA mapped region.
427 If there is no restriction,
428 .Dv BUS_SPACE_UNRESTRICTED
429 may be specified.
430 .It Fa maxsegsz
431 Maximum size, in bytes, of a segment in any DMA mapped region associated
432 with
433 .Fa dmat .
434 .It Fa flags
435 Are as follows:
436 .Bl -tag -width ".Dv BUS_DMA_ALLOCNOW"
437 .It Dv BUS_DMA_ALLOCNOW
438 Pre-allocate enough resources to handle at least one map load operation on
439 this tag.
440 If sufficient resources are not available,
441 .Er ENOMEM
442 is returned.
443 This should not be used for tags that only describe buffers that will be
444 allocated with
445 .Fn bus_dmamem_alloc .
446 Also, due to resource sharing with other tags, this flag does not guarantee
447 that resources will be allocated or reserved exclusively for this tag.
448 It should be treated only as a minor optimization.
449 .El
450 .It Fa lockfunc
451 Optional lock manipulation function (may be
452 .Dv NULL )
453 to be called when busdma
454 needs to manipulate a lock on behalf of the client.
455 If
456 .Dv NULL
457 is specified,
458 .Fn dflt_lock
459 is used.
460 .It Fa lockfuncarg
461 Optional argument to be passed to the function specified by
462 .Fa lockfunc .
463 .It Fa dmat
464 Pointer to a bus_dma_tag_t where the resulting DMA tag will
465 be stored.
466 .El
467 .Pp
468 Returns
469 .Er ENOMEM
470 if sufficient memory is not available for tag creation
471 or allocating mapping resources.
472 .It Fn bus_dma_tag_destroy "dmat"
473 Deallocate the DMA tag
474 .Fa dmat
475 that was created by
476 .Fn bus_dma_tag_create .
477 .Pp
478 Returns
479 .Er EBUSY
480 if any DMA maps remain associated with
481 .Fa dmat
482 or
483 .Ql 0
484 on success.
485 .It Fn bus_dmamap_create "dmat" "flags" "*mapp"
486 Allocates and initializes a DMA map.
487 Arguments are as follows:
488 .Bl -tag -width ".Fa nsegments"
489 .It Fa dmat
490 DMA tag.
491 .It Fa flags
492 Are as follows:
493 .Bl -tag -width ".Dv BUS_DMA_COHERENT"
494 .It Dv BUS_DMA_COHERENT
495 Attempt to map the memory loaded with this map such that cache sync
496 operations are as cheap as possible.
497 This flag is typically set on maps when the memory loaded with these will
498 be accessed by both a CPU and a DMA engine, frequently such as control data
499 and as opposed to streamable data such as receive and transmit buffers.
500 Use of this flag does not remove the requirement of using
501 .Fn bus_dmamap_sync ,
502 but it may reduce the cost of performing these operations.
503 For
504 .Fn bus_dmamap_create ,
505 the
506 .Dv BUS_DMA_COHERENT
507 flag is currently implemented on sparc64.
508 .El
509 .It Fa mapp
510 Pointer to a
511 .Vt bus_dmamap_t
512 where the resulting DMA map will be stored.
513 .El
514 .Pp
515 Returns
516 .Er ENOMEM
517 if sufficient memory is not available for creating the
518 map or allocating mapping resources.
519 .It Fn bus_dmamap_destroy "dmat" "map"
520 Frees all resources associated with a given DMA map.
521 Arguments are as follows:
522 .Bl -tag -width ".Fa dmat"
523 .It Fa dmat
524 DMA tag used to allocate
525 .Fa map .
526 .It Fa map
527 The DMA map to destroy.
528 .El
529 .Pp
530 Returns
531 .Er EBUSY
532 if a mapping is still active for
533 .Fa map .
534 .It Fn bus_dmamap_load "dmat" "map" "buf" "buflen" "*callback" \
535 "callback_arg" "flags"
536 Creates a mapping in device visible address space of
537 .Fa buflen
538 bytes of
539 .Fa buf ,
540 associated with the DMA map
541 .Fa map .
542 This call will always return immediately and will not block for any reason.
543 Arguments are as follows:
544 .Bl -tag -width ".Fa buflen"
545 .It Fa dmat
546 DMA tag used to allocate
547 .Fa map .
548 .It Fa map
549 A DMA map without a currently active mapping.
550 .It Fa buf
551 A kernel virtual address pointer to a contiguous (in KVA) buffer, to be
552 mapped into device visible address space.
553 .It Fa buflen
554 The size of the buffer.
555 .It Fa callback Fa callback_arg
556 The callback function, and its argument.
557 This function is called once sufficient mapping resources are available for
558 the DMA operation.
559 If resources are temporarily unavailable, this function will be deferred until
560 later, but the load operation will still return immediately to the caller.
561 Thus, callers should not assume that the callback will be called before the
562 load returns, and code should be structured appropriately to handle this.
563 See below for specific flags and error codes that control this behavior.
564 .It Fa flags
565 Are as follows:
566 .Bl -tag -width ".Dv BUS_DMA_NOWAIT"
567 .It Dv BUS_DMA_NOWAIT
568 The load should not be deferred in case of insufficient mapping resources,
569 and instead should return immediately with an appropriate error.
570 .It Dv BUS_DMA_NOCACHE
571 The generated transactions to and from the virtual page are non-cacheable.
572 For
573 .Fn bus_dmamap_load ,
574 the
575 .Dv BUS_DMA_NOCACHE
576 flag is currently implemented on sparc64.
577 .El
578 .El
579 .Pp
580 Return values to the caller are as follows:
581 .Bl -tag -width ".Er EINPROGRESS"
582 .It 0
583 The callback has been called and completed.
584 The status of the mapping has been delivered to the callback.
585 .It Er EINPROGRESS
586 The mapping has been deferred for lack of resources.
587 The callback will be called as soon as resources are available.
588 Callbacks are serviced in FIFO order.
589 .Pp
590 Note that subsequent load operations for the same tag that do not require
591 extra resources will still succeed.
592 This may result in out-of-order processing of requests.
593 If the caller requires the order of requests to be preserved,
594 then the caller is required to stall subsequent requests until a pending
595 request's callback is invoked.
596 .It Er ENOMEM
597 The load request has failed due to insufficient resources, and the caller
598 specifically used the
599 .Dv BUS_DMA_NOWAIT
600 flag.
601 .It Er EINVAL
602 The load request was invalid.
603 The callback has been called and has been provided the same error.
604 This error value may indicate that
605 .Fa dmat ,
606 .Fa map ,
607 .Fa buf ,
608 or
609 .Fa callback
610 were invalid, or
611 .Fa buflen
612 was larger than the
613 .Fa maxsize
614 argument used to create the dma tag
615 .Fa dmat .
616 .El
617 .Pp
618 When the callback is called, it is presented with an error value
619 indicating the disposition of the mapping.
620 Error may be one of the following:
621 .Bl -tag -width ".Er EINPROGRESS"
622 .It 0
623 The mapping was successful and the
624 .Fa dm_segs
625 callback argument contains an array of
626 .Vt bus_dma_segment_t
627 elements describing the mapping.
628 This array is only valid during the scope of the callback function.
629 .It Er EFBIG
630 A mapping could not be achieved within the segment constraints provided
631 in the tag even though the requested allocation size was less than maxsize.
632 .El
633 .It Fn bus_dmamap_load_bio "dmat" "map" "bio" "callback" "callback_arg" "flags"
634 This is a variation of
635 .Fn bus_dmamap_load
636 which maps buffers pointed to by
637 .Fa bio
638 for DMA transfers.
639 .Fa bio
640 may point to either a mapped or unmapped buffer.
641 .It Fn bus_dmamap_load_ccb "dmat" "map" "ccb" "callback" "callback_arg" "flags"
642 This is a variation of
643 .Fn bus_dmamap_load
644 which maps data pointed to by
645 .Fa ccb
646 for DMA transfers.
647 The data for
648 .Fa ccb
649 may be any of the following types:
650 .Bl -tag -width ".Er CAM_DATA_SG_PADDR"
651 .It CAM_DATA_VADDR
652 The data is a single KVA buffer.
653 .It CAM_DATA_PADDR
654 The data is a single bus address range.
655 .It CAM_DATA_SG
656 The data is a scatter/gather list of KVA buffers.
657 .It CAM_DATA_SG_PADDR
658 The data is a scatter/gather list of bus address ranges.
659 .It CAM_DATA_BIO
660 The data is contained in a
661 .Vt struct bio
662 attached to the CCB.
663 .El
664 .Pp
665 .Fn bus_dmamap_load_ccb
666 supports the following CCB XPT function codes:
667 .Pp
668 .Bl -item -offset indent -compact
669 .It
670 XPT_ATA_IO
671 .It
672 XPT_CONT_TARGET_IO
673 .It
674 XPT_SCSI_IO
675 .El
676 .It Fn bus_dmamap_load_mbuf "dmat" "map" "mbuf" "callback2" "callback_arg" \
677 "flags"
678 This is a variation of
679 .Fn bus_dmamap_load
680 which maps mbuf chains
681 for DMA transfers.
682 A
683 .Vt bus_size_t
684 argument is also passed to the callback routine, which
685 contains the mbuf chain's packet header length.
686 The
687 .Dv BUS_DMA_NOWAIT
688 flag is implied, thus no callback deferral will happen.
689 .Pp
690 Mbuf chains are assumed to be in kernel virtual address space.
691 .Pp
692 Beside the error values listed for
693 .Fn bus_dmamap_load ,
694 .Er EINVAL
695 will be returned if the size of the mbuf chain exceeds the maximum limit of the
696 DMA tag.
697 .It Fn bus_dmamap_load_mbuf_sg "dmat" "map" "mbuf" "segs" "nsegs" "flags"
698 This is just like
699 .Fn bus_dmamap_load_mbuf
700 except that it returns immediately without calling a callback function.
701 It is provided for efficiency.
702 The scatter/gather segment array
703 .Va segs
704 is provided by the caller and filled in directly by the function.
705 The
706 .Va nsegs
707 argument is returned with the number of segments filled in.
708 Returns the same errors as
709 .Fn bus_dmamap_load_mbuf .
710 .It Fn bus_dmamap_load_uio "dmat" "map" "uio" "callback2" "callback_arg" "flags"
711 This is a variation of
712 .Fn bus_dmamap_load
713 which maps buffers pointed to by
714 .Fa uio
715 for DMA transfers.
716 A
717 .Vt bus_size_t
718 argument is also passed to the callback routine, which contains the size of
719 .Fa uio ,
720 i.e.
721 .Fa uio->uio_resid .
722 The
723 .Dv BUS_DMA_NOWAIT
724 flag is implied, thus no callback deferral will happen.
725 Returns the same errors as
726 .Fn bus_dmamap_load .
727 .Pp
728 If
729 .Fa uio->uio_segflg
730 is
731 .Dv UIO_USERSPACE ,
732 then it is assumed that the buffer,
733 .Fa uio
734 is in
735 .Fa "uio->uio_td->td_proc" Ns 's
736 address space.
737 User space memory must be in-core and wired prior to attempting a map
738 load operation.
739 Pages may be locked using
740 .Xr vslock 9 .
741 .It Fn bus_dmamap_unload "dmat" "map"
742 Unloads a DMA map.
743 Arguments are as follows:
744 .Bl -tag -width ".Fa dmam"
745 .It Fa dmat
746 DMA tag used to allocate
747 .Fa map .
748 .It Fa map
749 The DMA map that is to be unloaded.
750 .El
751 .Pp
752 .Fn bus_dmamap_unload
753 will not perform any implicit synchronization of DMA buffers.
754 This must be done explicitly by a call to
755 .Fn bus_dmamap_sync
756 prior to unloading the map.
757 .It Fn bus_dmamap_sync "dmat" "map" "op"
758 Performs synchronization of a device visible mapping with the CPU visible
759 memory referenced by that mapping.
760 Arguments are as follows:
761 .Bl -tag -width ".Fa dmat"
762 .It Fa dmat
763 DMA tag used to allocate
764 .Fa map .
765 .It Fa map
766 The DMA mapping to be synchronized.
767 .It Fa op
768 Type of synchronization operation to perform.
769 See the definition of
770 .Vt bus_dmasync_op_t
771 for a description of the acceptable values for
772 .Fa op .
773 .El
774 .Pp
775 The
776 .Fn bus_dmamap_sync
777 function
778 is the method used to ensure that CPU's and device's direct
779 memory access (DMA) to shared
780 memory is coherent.
781 For example, the CPU might be used to set up the contents of a buffer
782 that is to be made available to a device.
783 To ensure that the data are visible via the device's mapping of that
784 memory, the buffer must be loaded and a DMA sync operation of
785 .Dv BUS_DMASYNC_PREWRITE
786 must be performed after the CPU has updated the buffer and before the device
787 access is initiated.
788 If the CPU modifies this buffer again later, another
789 .Dv BUS_DMASYNC_PREWRITE
790 sync operation must be performed before an additional device
791 access.
792 Conversely, suppose a device updates memory that is to be read by a CPU.
793 In this case, the buffer must be loaded, and a DMA sync operation of
794 .Dv BUS_DMASYNC_PREREAD
795 must be performed before the device access is initiated.
796 The CPU will only be able to see the results of this memory update
797 once the DMA operation has completed and a
798 .Dv BUS_DMASYNC_POSTREAD
799 sync operation has been performed.
800 .Pp
801 If read and write operations are not preceded and followed by the
802 appropriate synchronization operations, behavior is undefined.
803 .It Fn bus_dmamem_alloc "dmat" "**vaddr" "flags" "*mapp"
804 Allocates memory that is mapped into KVA at the address returned
805 in
806 .Fa vaddr
807 and that is permanently loaded into the newly created
808 .Vt bus_dmamap_t
809 returned via
810 .Fa mapp .
811 Arguments are as follows:
812 .Bl -tag -width ".Fa alignment"
813 .It Fa dmat
814 DMA tag describing the constraints of the DMA mapping.
815 .It Fa vaddr
816 Pointer to a pointer that will hold the returned KVA mapping of
817 the allocated region.
818 .It Fa flags
819 Flags are defined as follows:
820 .Bl -tag -width ".Dv BUS_DMA_NOWAIT"
821 .It Dv BUS_DMA_WAITOK
822 The routine can safely wait (sleep) for resources.
823 .It Dv BUS_DMA_NOWAIT
824 The routine is not allowed to wait for resources.
825 If resources are not available,
826 .Dv ENOMEM
827 is returned.
828 .It Dv BUS_DMA_COHERENT
829 Attempt to map this memory in a coherent fashion.
830 See
831 .Fn bus_dmamap_create
832 above for a description of this flag.
833 For
834 .Fn bus_dmamem_alloc ,
835 the
836 .Dv BUS_DMA_COHERENT
837 flag is currently implemented on arm and sparc64.
838 .It Dv BUS_DMA_ZERO
839 Causes the allocated memory to be set to all zeros.
840 .It Dv BUS_DMA_NOCACHE
841 The allocated memory will not be cached in the processor caches.
842 All memory accesses appear on the bus and are executed
843 without reordering.
844 For
845 .Fn bus_dmamem_alloc ,
846 the
847 .Dv BUS_DMA_NOCACHE
848 flag is currently implemented on amd64 and i386 where it results in the
849 Strong Uncacheable PAT to be set for the allocated virtual address range.
850 .El
851 .It Fa mapp
852 Pointer to a
853 .Vt bus_dmamap_t
854 where the resulting DMA map will be stored.
855 .El
856 .Pp
857 The size of memory to be allocated is
858 .Fa maxsize
859 as specified in the call to
860 .Fn bus_dma_tag_create
861 for
862 .Fa dmat .
863 .Pp
864 The current implementation of
865 .Fn bus_dmamem_alloc
866 will allocate all requests as a single segment.
867 .Pp
868 An initial load operation is required to obtain the bus address of the allocated
869 memory, and an unload operation is required before freeing the memory, as
870 described below in
871 .Fn bus_dmamem_free .
872 Maps are automatically handled by this function and should not be explicitly
873 allocated or destroyed.
874 .Pp
875 Although an explicit load is not required for each access to the memory
876 referenced by the returned map, the synchronization requirements
877 as described in the
878 .Fn bus_dmamap_sync
879 section still apply and should be used to achieve portability on architectures
880 without coherent buses.
881 .Pp
882 Returns
883 .Er ENOMEM
884 if sufficient memory is not available for completing
885 the operation.
886 .It Fn bus_dmamem_free "dmat" "*vaddr" "map"
887 Frees memory previously allocated by
888 .Fn bus_dmamem_alloc .
889 Any mappings
890 will be invalidated.
891 Arguments are as follows:
892 .Bl -tag -width ".Fa vaddr"
893 .It Fa dmat
894 DMA tag.
895 .It Fa vaddr
896 Kernel virtual address of the memory.
897 .It Fa map
898 DMA map to be invalidated.
899 .El
900 .El
901 .Sh RETURN VALUES
902 Behavior is undefined if invalid arguments are passed to
903 any of the above functions.
904 If sufficient resources cannot be allocated for a given
905 transaction,
906 .Er ENOMEM
907 is returned.
908 All
909 routines that are not of type
910 .Vt void
911 will return 0 on success or an error
912 code on failure as discussed above.
913 .Pp
914 All
915 .Vt void
916 routines will succeed if provided with valid arguments.
917 .Sh LOCKING
918 Two locking protocols are used by
919 .Nm .
920 The first is a private global lock that is used to synchronize access to the
921 bounce buffer pool on the architectures that make use of them.
922 This lock is strictly a leaf lock that is only used internally to
923 .Nm
924 and is not exposed to clients of the API.
925 .Pp
926 The second protocol involves protecting various resources stored in the tag.
927 Since almost all
928 .Nm
929 operations are done through requests from the driver that created the tag,
930 the most efficient way to protect the tag resources is through the lock that
931 the driver uses.
932 In cases where
933 .Nm
934 acts on its own without being called by the driver, the lock primitive
935 specified in the tag is acquired and released automatically.
936 An example of this is when the
937 .Fn bus_dmamap_load
938 callback function is called from a deferred context instead of the driver
939 context.
940 This means that certain
941 .Nm
942 functions must always be called with the same lock held that is specified in the
943 tag.
944 These functions include:
945 .Pp
946 .Bl -item -offset indent -compact
947 .It
948 .Fn bus_dmamap_load
949 .It
950 .Fn bus_dmamap_load_bio
951 .It
952 .Fn bus_dmamap_load_ccb
953 .It
954 .Fn bus_dmamap_load_mbuf
955 .It
956 .Fn bus_dmamap_load_mbuf_sg
957 .It
958 .Fn bus_dmamap_load_uio
959 .It
960 .Fn bus_dmamap_unload
961 .It
962 .Fn bus_dmamap_sync
963 .El
964 .Pp
965 There is one exception to this rule.
966 It is common practice to call some of these functions during driver start-up
967 without any locks held.
968 So long as there is a guarantee of no possible concurrent use of the tag by
969 different threads during this operation, it is safe to not hold a lock for
970 these functions.
971 .Pp
972 Certain
973 .Nm
974 operations should not be called with the driver lock held, either because
975 they are already protected by an internal lock, or because they might sleep
976 due to memory or resource allocation.
977 The following functions must not be
978 called with any non-sleepable locks held:
979 .Pp
980 .Bl -item -offset indent -compact
981 .It
982 .Fn bus_dma_tag_create
983 .It
984 .Fn bus_dmamap_create
985 .It
986 .Fn bus_dmamem_alloc
987 .El
988 .Pp
989 All other functions do not have a locking protocol and can thus be
990 called with or without any system or driver locks held.
991 .Sh SEE ALSO
992 .Xr devclass 9 ,
993 .Xr device 9 ,
994 .Xr driver 9 ,
995 .Xr rman 9 ,
996 .Xr vslock 9
997 .Pp
998 .Rs
999 .%A "Jason R. Thorpe"
1000 .%T "A Machine-Independent DMA Framework for NetBSD"
1001 .%J "Proceedings of the Summer 1998 USENIX Technical Conference"
1002 .%Q "USENIX Association"
1003 .%D "June 1998"
1004 .Re
1005 .Sh HISTORY
1006 The
1007 .Nm
1008 interface first appeared in
1009 .Nx 1.3 .
1010 .Pp
1011 The
1012 .Nm
1013 API was adopted from
1014 .Nx
1015 for use in the CAM SCSI subsystem.
1016 The alterations to the original API were aimed to remove the need for
1017 a
1018 .Vt bus_dma_segment_t
1019 array stored in each
1020 .Vt bus_dmamap_t
1021 while allowing callers to queue up on scarce resources.
1022 .Sh AUTHORS
1023 The
1024 .Nm
1025 interface was designed and implemented by
1026 .An Jason R. Thorpe
1027 of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
1028 Additional input on the
1029 .Nm
1030 design was provided by
1031 .An -nosplit
1032 .An Chris Demetriou ,
1033 .An Charles Hannum ,
1034 .An Ross Harvey ,
1035 .An Matthew Jacob ,
1036 .An Jonathan Stone ,
1037 and
1038 .An Matt Thomas .
1039 .Pp
1040 The
1041 .Nm
1042 interface in
1043 .Fx
1044 benefits from the contributions of
1045 .An Justin T. Gibbs ,
1046 .An Peter Wemm ,
1047 .An Doug Rabson ,
1048 .An Matthew N. Dodd ,
1049 .An Sam Leffler ,
1050 .An Maxime Henrion ,
1051 .An Jake Burkholder ,
1052 .An Takahashi Yoshihiro ,
1053 .An Scott Long
1054 and many others.
1055 .Pp
1056 This manual page was written by
1057 .An Hiten M. Pandya
1058 and
1059 .An Justin T. Gibbs .