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