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