]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - share/man/man9/bus_dma.9
MFC 337673: Add an overview section to bus_dma.9.
[FreeBSD/stable/10.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 August 11, 2018
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 OVERVIEW
139 A tag structure
140 .Vt ( bus_dma_tag_t )
141 is used to describe the properties of a group of related DMA
142 transactions.
143 One way to view this is that a tag describes the limitations of a DMA engine.
144 For example, if a DMA engine in a device is limited to 32-bit addresses,
145 that limitation is specified by a parameter when creating the tag
146 for that device.
147 Similarly, a tag can be marked as requiring buffers whose addresses are
148 aligned to a specific boundary.
149 .Pp
150 Some devices may require multiple tags to describe DMA
151 transactions with differing properties.
152 For example, a device might require 16-byte alignment of its descriptor ring
153 while permitting arbitrary alignment of I/O buffers.
154 In this case,
155 the driver must create one tag for the descriptor ring and a separate tag for
156 I/O buffers.
157 If a device has restrictions that are common to all DMA transactions
158 in addition to restrictions that differ between unrelated groups of
159 transactions,
160 the driver can first create a
161 .Dq parent
162 tag that decribes the common restrictions.
163 The per-group tags can then inherit these restrictions from this
164 .Dq parent
165 tag rather than having to list them explicitly when creating the per-group tags.
166 .Pp
167 A mapping structure
168 .Vt ( bus_dmamap_t )
169 represents a mapping of a memory region for DMA.
170 On systems with I/O MMUs,
171 the mapping structure tracks any I/O MMU entries used by a request.
172 For DMA requests that require bounce pages,
173 the mapping tracks the bounce pages used.
174 .Pp
175 To prepare for one or more DMA transactions,
176 a mapping must be bound to a memory region by calling one of the
177 .Fn bus_dmamap_load
178 functions.
179 These functions configure the mapping which can include programming entries
180 in an I/O MMU and/or allocating bounce pages.
181 An output of these functions
182 (either directly or indirectly by invoking a callback routine)
183 is the list of scatter/gather address ranges a consumer can pass to a DMA
184 engine to access the memory region.
185 When a mapping is no longer needed,
186 the mapping must be unloaded via
187 .Fn bus_dmamap_unload .
188 .Pp
189 Before and after each DMA transaction,
190 .Fn bus_dmamap_sync
191 must be used to ensure that the correct data is used by the DMA engine and
192 the CPU.
193 If a mapping uses bounce pages,
194 the sync operations copy data between the bounce pages and the memory region
195 bound to the mapping.
196 Sync operations also handle architecture-specific details such as CPU cache
197 flushing and CPU memory operation ordering.
198 .Sh STATIC VS DYNAMIC
199 .Nm
200 handles two types of DMA transactions: static and dynamic.
201 Static transactions are used with a long-lived memory region that is reused
202 for many transactions such as a descriptor ring.
203 Dynamic transactions are used for transfers to or from transient buffers
204 such as I/O buffers holding a network packet or disk block.
205 Each transaction type uses a different subset of the
206 .Nm
207 API.
208 .Ss Static Transactions
209 Static transactions use memory regions allocated by
210 .Nm .
211 Each static memory region is allocated by calling
212 .Fn bus_dmamem_alloc .
213 This function requires a valid tag describing the properties of the
214 DMA transactions to this region such as alignment or address restrictions.
215 Multiple regions can share a single tag if they share the same restrictions.
216 .Pp
217 .Fn bus_dmamem_alloc
218 allocates a memory region along with a mapping object.
219 The associated tag, memory region, and mapping object must then be passed to
220 .Fn bus_dmamap_load
221 to bind the mapping to the allocated region and obtain the
222 scatter/gather list.
223 .Pp
224 It is expected that
225 .Fn bus_dmamem_alloc
226 will attempt to allocate memory requiring less expensive sync operations
227 (for example, implementations should not allocate regions requiring bounce
228 pages),
229 but sync operations should still be used.
230 For example, a driver should use
231 .Fn bus_dmamap_sync
232 in an interrupt handler before reading descriptor ring entries written by the
233 device prior to the interrupt.
234 .Pp
235 When a consumer is finished with a memory region,
236 it should unload the mapping via
237 .Fn bus_dmamap_unload
238 and then release the memory region and mapping object via
239 .Fn bus_dmamem_free .
240 .Ss Dynamic Transactions
241 Dynamic transactions map memory regions provided by other parts of the system.
242 A tag must be created via
243 .Fn bus_dma_tag_create
244 to describe the DMA transactions to and from these memory regions,
245 and a pool of mapping objects must be allocated via
246 .Fn bus_dmamap_create
247 to track the mappings of any in-flight transactions.
248 .Pp
249 When a consumer wishes to schedule a transaction for a memory region,
250 the consumer must first obtain an unused mapping object from its pool
251 of mapping objects.
252 The memory region must be bound to the mapping object via one of the
253 .Fn bus_dmamap_load
254 functions.
255 Before scheduling the transaction,
256 the consumer should sync the memory region via
257 .Fn bus_dmamap_sync
258 with one or more of the
259 .Dq PRE
260 flags.
261 After the transaction has completed,
262 the consumer should sync the memory region via
263 .Fn bus_dmamap_sync
264 with one or more of the
265 .Dq POST
266 flags.
267 The mapping can then be unloaded via
268 .Fn bus_dmamap_unload ,
269 and the mapping object can be returned to the pool of unused mapping objects.
270 .Pp
271 When a consumer is no longer scheduling DMA transactions,
272 the mapping objects should be freed via
273 .Fn bus_dmamap_destroy ,
274 and the tag should be freed via
275 .Fn bus_dma_tag_destroy .
276 .Sh STRUCTURES AND TYPES
277 .Bl -tag -width indent
278 .It Vt bus_dma_tag_t
279 A machine-dependent (MD) opaque type that describes the
280 characteristics of a group of DMA transactions.
281 DMA tags are organized into a hierarchy, with each child
282 tag inheriting the restrictions of its parent.
283 This allows all devices along the path of DMA transactions
284 to contribute to the constraints of those transactions.
285 .It Vt bus_dma_filter_t
286 Client specified address filter having the format:
287 .Bl -tag -width indent
288 .It Ft int
289 .Fn "client_filter" "void *filtarg" "bus_addr_t testaddr"
290 .El
291 .Pp
292 Address filters can be specified during tag creation to allow
293 for devices whose DMA address restrictions cannot be specified
294 by a single window.
295 The
296 .Fa filtarg
297 argument is specified by the client during tag creation to be passed to all
298 invocations of the callback.
299 The
300 .Fa testaddr
301 argument contains a potential starting address of a DMA mapping.
302 The filter function operates on the set of addresses from
303 .Fa testaddr
304 to
305 .Ql trunc_page(testaddr) + PAGE_SIZE - 1 ,
306 inclusive.
307 The filter function should return zero if any mapping in this range
308 can be accommodated by the device and non-zero otherwise.
309 .It Vt bus_dma_segment_t
310 A machine-dependent type that describes individual
311 DMA segments.
312 It contains the following fields:
313 .Bd -literal
314         bus_addr_t      ds_addr;
315         bus_size_t      ds_len;
316 .Ed
317 .Pp
318 The
319 .Fa ds_addr
320 field contains the device visible address of the DMA segment, and
321 .Fa ds_len
322 contains the length of the DMA segment.
323 Although the DMA segments returned by a mapping call will adhere to
324 all restrictions necessary for a successful DMA operation, some conversion
325 (e.g.\& a conversion from host byte order to the device's byte order) is
326 almost always required when presenting segment information to the device.
327 .It Vt bus_dmamap_t
328 A machine-dependent opaque type describing an individual mapping.
329 One map is used for each memory allocation that will be loaded.
330 Maps can be reused once they have been unloaded.
331 Multiple maps can be associated with one DMA tag.
332 While the value of the map may evaluate to
333 .Dv NULL
334 on some platforms under certain conditions,
335 it should never be assumed that it will be
336 .Dv NULL
337 in all cases.
338 .It Vt bus_dmamap_callback_t
339 Client specified callback for receiving mapping information resulting from
340 the load of a
341 .Vt bus_dmamap_t
342 via
343 .Fn bus_dmamap_load ,
344 .Fn bus_dmamap_load_bio
345 or
346 .Fn bus_dmamap_load_ccb .
347 Callbacks are of the format:
348 .Bl -tag -width indent
349 .It Ft void
350 .Fn "client_callback" "void *callback_arg" "bus_dma_segment_t *segs" \
351 "int nseg" "int error"
352 .El
353 .Pp
354 The
355 .Fa callback_arg
356 is the callback argument passed to dmamap load functions.
357 The
358 .Fa segs
359 and
360 .Fa nseg
361 arguments describe an array of
362 .Vt bus_dma_segment_t
363 structures that represent the mapping.
364 This array is only valid within the scope of the callback function.
365 The success or failure of the mapping is indicated by the
366 .Fa error
367 argument.
368 More information on the use of callbacks can be found in the
369 description of the individual dmamap load functions.
370 .It Vt bus_dmamap_callback2_t
371 Client specified callback for receiving mapping information resulting from
372 the load of a
373 .Vt bus_dmamap_t
374 via
375 .Fn bus_dmamap_load_uio
376 or
377 .Fn bus_dmamap_load_mbuf .
378 .Pp
379 Callback2s are of the format:
380 .Bl -tag -width indent
381 .It Ft void
382 .Fn "client_callback2" "void *callback_arg" "bus_dma_segment_t *segs" \
383 "int nseg" "bus_size_t mapsize" "int error"
384 .El
385 .Pp
386 Callback2's behavior is the same as
387 .Vt bus_dmamap_callback_t
388 with the addition that the length of the data mapped is provided via
389 .Fa mapsize .
390 .It Vt bus_dmasync_op_t
391 Memory synchronization operation specifier.
392 Bus DMA requires explicit synchronization of memory with its device
393 visible mapping in order to guarantee memory coherency.
394 The
395 .Vt bus_dmasync_op_t
396 allows the type of DMA operation that will be or has been performed
397 to be communicated to the system so that the correct coherency measures
398 are taken.
399 The operations are represented as bitfield flags that can be combined together,
400 though it only makes sense to combine PRE flags or POST flags, not both.
401 See the
402 .Fn bus_dmamap_sync
403 description below for more details on how to use these operations.
404 .Pp
405 All operations specified below are performed from the host memory point of view,
406 where a read implies data coming from the device to the host memory, and a write
407 implies data going from the host memory to the device.
408 Alternatively, the operations can be thought of in terms of driver operations,
409 where reading a network packet or storage sector corresponds to a read operation
410 in
411 .Nm .
412 .Bl -tag -width ".Dv BUS_DMASYNC_POSTWRITE"
413 .It Dv BUS_DMASYNC_PREREAD
414 Perform any synchronization required prior to an update of host memory by the
415 device.
416 .It Dv BUS_DMASYNC_PREWRITE
417 Perform any synchronization required after an update of host memory by the CPU
418 and prior to device access to host memory.
419 .It Dv BUS_DMASYNC_POSTREAD
420 Perform any synchronization required after an update of host memory by the
421 device and prior to CPU access to host memory.
422 .It Dv BUS_DMASYNC_POSTWRITE
423 Perform any synchronization required after device access to host memory.
424 .El
425 .It Vt bus_dma_lock_t
426 Client specified lock/mutex manipulation method.
427 This will be called from
428 within busdma whenever a client lock needs to be manipulated.
429 In its current form, the function will be called immediately before
430 the callback for a DMA load operation that has been deferred with
431 .Dv BUS_DMA_LOCK
432 and immediately after with
433 .Dv BUS_DMA_UNLOCK .
434 If the load operation does not need to be deferred, then it
435 will not be called since the function loading the map should
436 be holding the appropriate locks.
437 This method is of the format:
438 .Bl -tag -width indent
439 .It Ft void
440 .Fn "lockfunc" "void *lockfunc_arg" "bus_dma_lock_op_t op"
441 .El
442 .Pp
443 The
444 .Fa lockfuncarg
445 argument is specified by the client during tag creation to be passed to all
446 invocations of the callback.
447 The
448 .Fa op
449 argument specifies the lock operation to perform.
450 .Pp
451 Two
452 .Vt lockfunc
453 implementations are provided for convenience.
454 .Fn busdma_lock_mutex
455 performs standard mutex operations on the sleep mutex provided via
456 .Fa lockfuncarg .
457 .Fn dflt_lock
458 will generate a system panic if it is called.
459 It is substituted into the tag when
460 .Fa lockfunc
461 is passed as
462 .Dv NULL
463 to
464 .Fn bus_dma_tag_create
465 and is useful for tags that should not be used with deferred load operations.
466 .It Vt bus_dma_lock_op_t
467 Operations to be performed by the client-specified
468 .Fn lockfunc .
469 .Bl -tag -width ".Dv BUS_DMA_UNLOCK"
470 .It Dv BUS_DMA_LOCK
471 Acquires and/or locks the client locking primitive.
472 .It Dv BUS_DMA_UNLOCK
473 Releases and/or unlocks the client locking primitive.
474 .El
475 .El
476 .Sh FUNCTIONS
477 .Bl -tag -width indent
478 .It Fn bus_dma_tag_create "parent" "alignment" "boundary" "lowaddr" \
479 "highaddr" "*filtfunc" "*filtfuncarg" "maxsize" "nsegments" "maxsegsz" \
480 "flags" "lockfunc" "lockfuncarg" "*dmat"
481 Allocates a DMA tag, and initializes it according to
482 the arguments provided:
483 .Bl -tag -width ".Fa filtfuncarg"
484 .It Fa parent
485 A parent tag from which to inherit restrictions.
486 The restrictions passed in other arguments can only further tighten the
487 restrictions inherited from the parent tag.
488 .Pp
489 All tags created by a device driver must inherit from the tag returned by
490 .Fn bus_get_dma_tag
491 to honor restrictions between the parent bridge, CPU memory, and the
492 device.
493 .It Fa alignment
494 Alignment constraint, in bytes, of any mappings created using this tag.
495 The alignment must be a power of 2.
496 Hardware that can DMA starting at any address would specify
497 .Em 1
498 for byte alignment.
499 Hardware requiring DMA transfers to start on a multiple of 4K
500 would specify
501 .Em 4096 .
502 .It Fa boundary
503 Boundary constraint, in bytes, of the target DMA memory region.
504 The boundary indicates the set of addresses, all multiples of the
505 boundary argument, that cannot be crossed by a single
506 .Vt bus_dma_segment_t .
507 The boundary must be a power of 2 and must be no smaller than the
508 maximum segment size.
509 .Ql 0
510 indicates that there are no boundary restrictions.
511 .It Fa lowaddr , highaddr
512 Bounds of the window of bus address space that
513 .Em cannot
514 be directly accessed by the device.
515 The window contains all addresses greater than
516 .Fa lowaddr
517 and less than or equal to
518 .Fa highaddr .
519 For example, a device incapable of DMA above 4GB, would specify a
520 .Fa highaddr
521 of
522 .Dv BUS_SPACE_MAXADDR
523 and a
524 .Fa lowaddr
525 of
526 .Dv BUS_SPACE_MAXADDR_32BIT .
527 Similarly a device that can only perform DMA to addresses below
528 16MB would specify a
529 .Fa highaddr
530 of
531 .Dv BUS_SPACE_MAXADDR
532 and a
533 .Fa lowaddr
534 of
535 .Dv BUS_SPACE_MAXADDR_24BIT .
536 Some implementations require that some region of device visible
537 address space, overlapping available host memory, be outside the
538 window.
539 This area of
540 .Ql safe memory
541 is used to bounce requests that would otherwise conflict with
542 the exclusion window.
543 .It Fa filtfunc
544 Optional filter function (may be
545 .Dv NULL )
546 to be called for any attempt to
547 map memory into the window described by
548 .Fa lowaddr
549 and
550 .Fa highaddr .
551 A filter function is only required when the single window described
552 by
553 .Fa lowaddr
554 and
555 .Fa highaddr
556 cannot adequately describe the constraints of the device.
557 The filter function will be called for every machine page
558 that overlaps the exclusion window.
559 .It Fa filtfuncarg
560 Argument passed to all calls to the filter function for this tag.
561 May be
562 .Dv NULL .
563 .It Fa maxsize
564 Maximum size, in bytes, of the sum of all segment lengths in a given
565 DMA mapping associated with this tag.
566 .It Fa nsegments
567 Number of discontinuities (scatter/gather segments) allowed
568 in a DMA mapped region.
569 If there is no restriction,
570 .Dv BUS_SPACE_UNRESTRICTED
571 may be specified.
572 .It Fa maxsegsz
573 Maximum size, in bytes, of a segment in any DMA mapped region associated
574 with
575 .Fa dmat .
576 .It Fa flags
577 Are as follows:
578 .Bl -tag -width ".Dv BUS_DMA_ALLOCNOW"
579 .It Dv BUS_DMA_ALLOCNOW
580 Pre-allocate enough resources to handle at least one map load operation on
581 this tag.
582 If sufficient resources are not available,
583 .Er ENOMEM
584 is returned.
585 This should not be used for tags that only describe buffers that will be
586 allocated with
587 .Fn bus_dmamem_alloc .
588 Also, due to resource sharing with other tags, this flag does not guarantee
589 that resources will be allocated or reserved exclusively for this tag.
590 It should be treated only as a minor optimization.
591 .El
592 .It Fa lockfunc
593 Optional lock manipulation function (may be
594 .Dv NULL )
595 to be called when busdma
596 needs to manipulate a lock on behalf of the client.
597 If
598 .Dv NULL
599 is specified,
600 .Fn dflt_lock
601 is used.
602 .It Fa lockfuncarg
603 Optional argument to be passed to the function specified by
604 .Fa lockfunc .
605 .It Fa dmat
606 Pointer to a bus_dma_tag_t where the resulting DMA tag will
607 be stored.
608 .El
609 .Pp
610 Returns
611 .Er ENOMEM
612 if sufficient memory is not available for tag creation
613 or allocating mapping resources.
614 .It Fn bus_dma_tag_destroy "dmat"
615 Deallocate the DMA tag
616 .Fa dmat
617 that was created by
618 .Fn bus_dma_tag_create .
619 .Pp
620 Returns
621 .Er EBUSY
622 if any DMA maps remain associated with
623 .Fa dmat
624 or
625 .Ql 0
626 on success.
627 .It Fn bus_dmamap_create "dmat" "flags" "*mapp"
628 Allocates and initializes a DMA map.
629 Arguments are as follows:
630 .Bl -tag -width ".Fa nsegments"
631 .It Fa dmat
632 DMA tag.
633 .It Fa flags
634 Are as follows:
635 .Bl -tag -width ".Dv BUS_DMA_COHERENT"
636 .It Dv BUS_DMA_COHERENT
637 Attempt to map the memory loaded with this map such that cache sync
638 operations are as cheap as possible.
639 This flag is typically set on maps when the memory loaded with these will
640 be accessed by both a CPU and a DMA engine, frequently such as control data
641 and as opposed to streamable data such as receive and transmit buffers.
642 Use of this flag does not remove the requirement of using
643 .Fn bus_dmamap_sync ,
644 but it may reduce the cost of performing these operations.
645 For
646 .Fn bus_dmamap_create ,
647 the
648 .Dv BUS_DMA_COHERENT
649 flag is currently implemented on sparc64.
650 .El
651 .It Fa mapp
652 Pointer to a
653 .Vt bus_dmamap_t
654 where the resulting DMA map will be stored.
655 .El
656 .Pp
657 Returns
658 .Er ENOMEM
659 if sufficient memory is not available for creating the
660 map or allocating mapping resources.
661 .It Fn bus_dmamap_destroy "dmat" "map"
662 Frees all resources associated with a given DMA map.
663 Arguments are as follows:
664 .Bl -tag -width ".Fa dmat"
665 .It Fa dmat
666 DMA tag used to allocate
667 .Fa map .
668 .It Fa map
669 The DMA map to destroy.
670 .El
671 .Pp
672 Returns
673 .Er EBUSY
674 if a mapping is still active for
675 .Fa map .
676 .It Fn bus_dmamap_load "dmat" "map" "buf" "buflen" "*callback" \
677 "callback_arg" "flags"
678 Creates a mapping in device visible address space of
679 .Fa buflen
680 bytes of
681 .Fa buf ,
682 associated with the DMA map
683 .Fa map .
684 This call will always return immediately and will not block for any reason.
685 Arguments are as follows:
686 .Bl -tag -width ".Fa buflen"
687 .It Fa dmat
688 DMA tag used to allocate
689 .Fa map .
690 .It Fa map
691 A DMA map without a currently active mapping.
692 .It Fa buf
693 A kernel virtual address pointer to a contiguous (in KVA) buffer, to be
694 mapped into device visible address space.
695 .It Fa buflen
696 The size of the buffer.
697 .It Fa callback Fa callback_arg
698 The callback function, and its argument.
699 This function is called once sufficient mapping resources are available for
700 the DMA operation.
701 If resources are temporarily unavailable, this function will be deferred until
702 later, but the load operation will still return immediately to the caller.
703 Thus, callers should not assume that the callback will be called before the
704 load returns, and code should be structured appropriately to handle this.
705 See below for specific flags and error codes that control this behavior.
706 .It Fa flags
707 Are as follows:
708 .Bl -tag -width ".Dv BUS_DMA_NOWAIT"
709 .It Dv BUS_DMA_NOWAIT
710 The load should not be deferred in case of insufficient mapping resources,
711 and instead should return immediately with an appropriate error.
712 .It Dv BUS_DMA_NOCACHE
713 The generated transactions to and from the virtual page are non-cacheable.
714 For
715 .Fn bus_dmamap_load ,
716 the
717 .Dv BUS_DMA_NOCACHE
718 flag is currently implemented on sparc64.
719 .El
720 .El
721 .Pp
722 Return values to the caller are as follows:
723 .Bl -tag -width ".Er EINPROGRESS"
724 .It 0
725 The callback has been called and completed.
726 The status of the mapping has been delivered to the callback.
727 .It Er EINPROGRESS
728 The mapping has been deferred for lack of resources.
729 The callback will be called as soon as resources are available.
730 Callbacks are serviced in FIFO order.
731 .Pp
732 Note that subsequent load operations for the same tag that do not require
733 extra resources will still succeed.
734 This may result in out-of-order processing of requests.
735 If the caller requires the order of requests to be preserved,
736 then the caller is required to stall subsequent requests until a pending
737 request's callback is invoked.
738 .It Er ENOMEM
739 The load request has failed due to insufficient resources, and the caller
740 specifically used the
741 .Dv BUS_DMA_NOWAIT
742 flag.
743 .It Er EINVAL
744 The load request was invalid.
745 The callback has been called and has been provided the same error.
746 This error value may indicate that
747 .Fa dmat ,
748 .Fa map ,
749 .Fa buf ,
750 or
751 .Fa callback
752 were invalid, or
753 .Fa buflen
754 was larger than the
755 .Fa maxsize
756 argument used to create the dma tag
757 .Fa dmat .
758 .El
759 .Pp
760 When the callback is called, it is presented with an error value
761 indicating the disposition of the mapping.
762 Error may be one of the following:
763 .Bl -tag -width ".Er EINPROGRESS"
764 .It 0
765 The mapping was successful and the
766 .Fa dm_segs
767 callback argument contains an array of
768 .Vt bus_dma_segment_t
769 elements describing the mapping.
770 This array is only valid during the scope of the callback function.
771 .It Er EFBIG
772 A mapping could not be achieved within the segment constraints provided
773 in the tag even though the requested allocation size was less than maxsize.
774 .El
775 .It Fn bus_dmamap_load_bio "dmat" "map" "bio" "callback" "callback_arg" "flags"
776 This is a variation of
777 .Fn bus_dmamap_load
778 which maps buffers pointed to by
779 .Fa bio
780 for DMA transfers.
781 .Fa bio
782 may point to either a mapped or unmapped buffer.
783 .It Fn bus_dmamap_load_ccb "dmat" "map" "ccb" "callback" "callback_arg" "flags"
784 This is a variation of
785 .Fn bus_dmamap_load
786 which maps data pointed to by
787 .Fa ccb
788 for DMA transfers.
789 The data for
790 .Fa ccb
791 may be any of the following types:
792 .Bl -tag -width ".Er CAM_DATA_SG_PADDR"
793 .It CAM_DATA_VADDR
794 The data is a single KVA buffer.
795 .It CAM_DATA_PADDR
796 The data is a single bus address range.
797 .It CAM_DATA_SG
798 The data is a scatter/gather list of KVA buffers.
799 .It CAM_DATA_SG_PADDR
800 The data is a scatter/gather list of bus address ranges.
801 .It CAM_DATA_BIO
802 The data is contained in a
803 .Vt struct bio
804 attached to the CCB.
805 .El
806 .Pp
807 .Fn bus_dmamap_load_ccb
808 supports the following CCB XPT function codes:
809 .Pp
810 .Bl -item -offset indent -compact
811 .It
812 XPT_ATA_IO
813 .It
814 XPT_CONT_TARGET_IO
815 .It
816 XPT_SCSI_IO
817 .El
818 .It Fn bus_dmamap_load_mbuf "dmat" "map" "mbuf" "callback2" "callback_arg" \
819 "flags"
820 This is a variation of
821 .Fn bus_dmamap_load
822 which maps mbuf chains
823 for DMA transfers.
824 A
825 .Vt bus_size_t
826 argument is also passed to the callback routine, which
827 contains the mbuf chain's packet header length.
828 The
829 .Dv BUS_DMA_NOWAIT
830 flag is implied, thus no callback deferral will happen.
831 .Pp
832 Mbuf chains are assumed to be in kernel virtual address space.
833 .Pp
834 Beside the error values listed for
835 .Fn bus_dmamap_load ,
836 .Er EINVAL
837 will be returned if the size of the mbuf chain exceeds the maximum limit of the
838 DMA tag.
839 .It Fn bus_dmamap_load_mbuf_sg "dmat" "map" "mbuf" "segs" "nsegs" "flags"
840 This is just like
841 .Fn bus_dmamap_load_mbuf
842 except that it returns immediately without calling a callback function.
843 It is provided for efficiency.
844 The scatter/gather segment array
845 .Va segs
846 is provided by the caller and filled in directly by the function.
847 The
848 .Va nsegs
849 argument is returned with the number of segments filled in.
850 Returns the same errors as
851 .Fn bus_dmamap_load_mbuf .
852 .It Fn bus_dmamap_load_uio "dmat" "map" "uio" "callback2" "callback_arg" "flags"
853 This is a variation of
854 .Fn bus_dmamap_load
855 which maps buffers pointed to by
856 .Fa uio
857 for DMA transfers.
858 A
859 .Vt bus_size_t
860 argument is also passed to the callback routine, which contains the size of
861 .Fa uio ,
862 i.e.
863 .Fa uio->uio_resid .
864 The
865 .Dv BUS_DMA_NOWAIT
866 flag is implied, thus no callback deferral will happen.
867 Returns the same errors as
868 .Fn bus_dmamap_load .
869 .Pp
870 If
871 .Fa uio->uio_segflg
872 is
873 .Dv UIO_USERSPACE ,
874 then it is assumed that the buffer,
875 .Fa uio
876 is in
877 .Fa "uio->uio_td->td_proc" Ns 's
878 address space.
879 User space memory must be in-core and wired prior to attempting a map
880 load operation.
881 Pages may be locked using
882 .Xr vslock 9 .
883 .It Fn bus_dmamap_unload "dmat" "map"
884 Unloads a DMA map.
885 Arguments are as follows:
886 .Bl -tag -width ".Fa dmam"
887 .It Fa dmat
888 DMA tag used to allocate
889 .Fa map .
890 .It Fa map
891 The DMA map that is to be unloaded.
892 .El
893 .Pp
894 .Fn bus_dmamap_unload
895 will not perform any implicit synchronization of DMA buffers.
896 This must be done explicitly by a call to
897 .Fn bus_dmamap_sync
898 prior to unloading the map.
899 .It Fn bus_dmamap_sync "dmat" "map" "op"
900 Performs synchronization of a device visible mapping with the CPU visible
901 memory referenced by that mapping.
902 Arguments are as follows:
903 .Bl -tag -width ".Fa dmat"
904 .It Fa dmat
905 DMA tag used to allocate
906 .Fa map .
907 .It Fa map
908 The DMA mapping to be synchronized.
909 .It Fa op
910 Type of synchronization operation to perform.
911 See the definition of
912 .Vt bus_dmasync_op_t
913 for a description of the acceptable values for
914 .Fa op .
915 .El
916 .Pp
917 The
918 .Fn bus_dmamap_sync
919 function
920 is the method used to ensure that CPU's and device's direct
921 memory access (DMA) to shared
922 memory is coherent.
923 For example, the CPU might be used to set up the contents of a buffer
924 that is to be made available to a device.
925 To ensure that the data are visible via the device's mapping of that
926 memory, the buffer must be loaded and a DMA sync operation of
927 .Dv BUS_DMASYNC_PREWRITE
928 must be performed after the CPU has updated the buffer and before the device
929 access is initiated.
930 If the CPU modifies this buffer again later, another
931 .Dv BUS_DMASYNC_PREWRITE
932 sync operation must be performed before an additional device
933 access.
934 Conversely, suppose a device updates memory that is to be read by a CPU.
935 In this case, the buffer must be loaded, and a DMA sync operation of
936 .Dv BUS_DMASYNC_PREREAD
937 must be performed before the device access is initiated.
938 The CPU will only be able to see the results of this memory update
939 once the DMA operation has completed and a
940 .Dv BUS_DMASYNC_POSTREAD
941 sync operation has been performed.
942 .Pp
943 If read and write operations are not preceded and followed by the
944 appropriate synchronization operations, behavior is undefined.
945 .It Fn bus_dmamem_alloc "dmat" "**vaddr" "flags" "*mapp"
946 Allocates memory that is mapped into KVA at the address returned
947 in
948 .Fa vaddr
949 and that is permanently loaded into the newly created
950 .Vt bus_dmamap_t
951 returned via
952 .Fa mapp .
953 Arguments are as follows:
954 .Bl -tag -width ".Fa alignment"
955 .It Fa dmat
956 DMA tag describing the constraints of the DMA mapping.
957 .It Fa vaddr
958 Pointer to a pointer that will hold the returned KVA mapping of
959 the allocated region.
960 .It Fa flags
961 Flags are defined as follows:
962 .Bl -tag -width ".Dv BUS_DMA_NOWAIT"
963 .It Dv BUS_DMA_WAITOK
964 The routine can safely wait (sleep) for resources.
965 .It Dv BUS_DMA_NOWAIT
966 The routine is not allowed to wait for resources.
967 If resources are not available,
968 .Dv ENOMEM
969 is returned.
970 .It Dv BUS_DMA_COHERENT
971 Attempt to map this memory in a coherent fashion.
972 See
973 .Fn bus_dmamap_create
974 above for a description of this flag.
975 For
976 .Fn bus_dmamem_alloc ,
977 the
978 .Dv BUS_DMA_COHERENT
979 flag is currently implemented on arm and sparc64.
980 .It Dv BUS_DMA_ZERO
981 Causes the allocated memory to be set to all zeros.
982 .It Dv BUS_DMA_NOCACHE
983 The allocated memory will not be cached in the processor caches.
984 All memory accesses appear on the bus and are executed
985 without reordering.
986 For
987 .Fn bus_dmamem_alloc ,
988 the
989 .Dv BUS_DMA_NOCACHE
990 flag is currently implemented on amd64 and i386 where it results in the
991 Strong Uncacheable PAT to be set for the allocated virtual address range.
992 .El
993 .It Fa mapp
994 Pointer to a
995 .Vt bus_dmamap_t
996 where the resulting DMA map will be stored.
997 .El
998 .Pp
999 The size of memory to be allocated is
1000 .Fa maxsize
1001 as specified in the call to
1002 .Fn bus_dma_tag_create
1003 for
1004 .Fa dmat .
1005 .Pp
1006 The current implementation of
1007 .Fn bus_dmamem_alloc
1008 will allocate all requests as a single segment.
1009 .Pp
1010 An initial load operation is required to obtain the bus address of the allocated
1011 memory, and an unload operation is required before freeing the memory, as
1012 described below in
1013 .Fn bus_dmamem_free .
1014 Maps are automatically handled by this function and should not be explicitly
1015 allocated or destroyed.
1016 .Pp
1017 Although an explicit load is not required for each access to the memory
1018 referenced by the returned map, the synchronization requirements
1019 as described in the
1020 .Fn bus_dmamap_sync
1021 section still apply and should be used to achieve portability on architectures
1022 without coherent buses.
1023 .Pp
1024 Returns
1025 .Er ENOMEM
1026 if sufficient memory is not available for completing
1027 the operation.
1028 .It Fn bus_dmamem_free "dmat" "*vaddr" "map"
1029 Frees memory previously allocated by
1030 .Fn bus_dmamem_alloc .
1031 Any mappings
1032 will be invalidated.
1033 Arguments are as follows:
1034 .Bl -tag -width ".Fa vaddr"
1035 .It Fa dmat
1036 DMA tag.
1037 .It Fa vaddr
1038 Kernel virtual address of the memory.
1039 .It Fa map
1040 DMA map to be invalidated.
1041 .El
1042 .El
1043 .Sh RETURN VALUES
1044 Behavior is undefined if invalid arguments are passed to
1045 any of the above functions.
1046 If sufficient resources cannot be allocated for a given
1047 transaction,
1048 .Er ENOMEM
1049 is returned.
1050 All
1051 routines that are not of type
1052 .Vt void
1053 will return 0 on success or an error
1054 code on failure as discussed above.
1055 .Pp
1056 All
1057 .Vt void
1058 routines will succeed if provided with valid arguments.
1059 .Sh LOCKING
1060 Two locking protocols are used by
1061 .Nm .
1062 The first is a private global lock that is used to synchronize access to the
1063 bounce buffer pool on the architectures that make use of them.
1064 This lock is strictly a leaf lock that is only used internally to
1065 .Nm
1066 and is not exposed to clients of the API.
1067 .Pp
1068 The second protocol involves protecting various resources stored in the tag.
1069 Since almost all
1070 .Nm
1071 operations are done through requests from the driver that created the tag,
1072 the most efficient way to protect the tag resources is through the lock that
1073 the driver uses.
1074 In cases where
1075 .Nm
1076 acts on its own without being called by the driver, the lock primitive
1077 specified in the tag is acquired and released automatically.
1078 An example of this is when the
1079 .Fn bus_dmamap_load
1080 callback function is called from a deferred context instead of the driver
1081 context.
1082 This means that certain
1083 .Nm
1084 functions must always be called with the same lock held that is specified in the
1085 tag.
1086 These functions include:
1087 .Pp
1088 .Bl -item -offset indent -compact
1089 .It
1090 .Fn bus_dmamap_load
1091 .It
1092 .Fn bus_dmamap_load_bio
1093 .It
1094 .Fn bus_dmamap_load_ccb
1095 .It
1096 .Fn bus_dmamap_load_mbuf
1097 .It
1098 .Fn bus_dmamap_load_mbuf_sg
1099 .It
1100 .Fn bus_dmamap_load_uio
1101 .It
1102 .Fn bus_dmamap_unload
1103 .It
1104 .Fn bus_dmamap_sync
1105 .El
1106 .Pp
1107 There is one exception to this rule.
1108 It is common practice to call some of these functions during driver start-up
1109 without any locks held.
1110 So long as there is a guarantee of no possible concurrent use of the tag by
1111 different threads during this operation, it is safe to not hold a lock for
1112 these functions.
1113 .Pp
1114 Certain
1115 .Nm
1116 operations should not be called with the driver lock held, either because
1117 they are already protected by an internal lock, or because they might sleep
1118 due to memory or resource allocation.
1119 The following functions must not be
1120 called with any non-sleepable locks held:
1121 .Pp
1122 .Bl -item -offset indent -compact
1123 .It
1124 .Fn bus_dma_tag_create
1125 .It
1126 .Fn bus_dmamap_create
1127 .It
1128 .Fn bus_dmamem_alloc
1129 .El
1130 .Pp
1131 All other functions do not have a locking protocol and can thus be
1132 called with or without any system or driver locks held.
1133 .Sh SEE ALSO
1134 .Xr devclass 9 ,
1135 .Xr device 9 ,
1136 .Xr driver 9 ,
1137 .Xr rman 9 ,
1138 .Xr vslock 9
1139 .Pp
1140 .Rs
1141 .%A "Jason R. Thorpe"
1142 .%T "A Machine-Independent DMA Framework for NetBSD"
1143 .%J "Proceedings of the Summer 1998 USENIX Technical Conference"
1144 .%Q "USENIX Association"
1145 .%D "June 1998"
1146 .Re
1147 .Sh HISTORY
1148 The
1149 .Nm
1150 interface first appeared in
1151 .Nx 1.3 .
1152 .Pp
1153 The
1154 .Nm
1155 API was adopted from
1156 .Nx
1157 for use in the CAM SCSI subsystem.
1158 The alterations to the original API were aimed to remove the need for
1159 a
1160 .Vt bus_dma_segment_t
1161 array stored in each
1162 .Vt bus_dmamap_t
1163 while allowing callers to queue up on scarce resources.
1164 .Sh AUTHORS
1165 The
1166 .Nm
1167 interface was designed and implemented by
1168 .An Jason R. Thorpe
1169 of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
1170 Additional input on the
1171 .Nm
1172 design was provided by
1173 .An -nosplit
1174 .An Chris Demetriou ,
1175 .An Charles Hannum ,
1176 .An Ross Harvey ,
1177 .An Matthew Jacob ,
1178 .An Jonathan Stone ,
1179 and
1180 .An Matt Thomas .
1181 .Pp
1182 The
1183 .Nm
1184 interface in
1185 .Fx
1186 benefits from the contributions of
1187 .An Justin T. Gibbs ,
1188 .An Peter Wemm ,
1189 .An Doug Rabson ,
1190 .An Matthew N. Dodd ,
1191 .An Sam Leffler ,
1192 .An Maxime Henrion ,
1193 .An Jake Burkholder ,
1194 .An Takahashi Yoshihiro ,
1195 .An Scott Long
1196 and many others.
1197 .Pp
1198 This manual page was written by
1199 .An Hiten M. Pandya
1200 and
1201 .An Justin T. Gibbs .