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