124503465SHiten Pandya.\" Copyright (c) 2002, 2003 Hiten M. Pandya. 224503465SHiten Pandya.\" All rights reserved. 324503465SHiten Pandya.\" 424503465SHiten Pandya.\" Redistribution and use in source and binary forms, with or without 524503465SHiten Pandya.\" modification, are permitted provided that the following conditions 624503465SHiten Pandya.\" are met: 724503465SHiten Pandya.\" 1. Redistributions of source code must retain the above copyright 824503465SHiten Pandya.\" notice, this list of conditions, and the following disclaimer, 924503465SHiten Pandya.\" without modification, immediately at the beginning of the file. 1024503465SHiten Pandya.\" 2. The name of the author may not be used to endorse or promote products 1124503465SHiten Pandya.\" derived from this software without specific prior written permission. 1224503465SHiten Pandya.\" 1324503465SHiten Pandya.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1424503465SHiten Pandya.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1524503465SHiten Pandya.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1624503465SHiten Pandya.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, CONTRIBUTORS OR THE 1724503465SHiten Pandya.\" VOICES IN HITEN PANDYA'S HEAD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1824503465SHiten Pandya.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 1924503465SHiten Pandya.\" TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 2024503465SHiten Pandya.\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 2124503465SHiten Pandya.\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2224503465SHiten Pandya.\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 2324503465SHiten Pandya.\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2424503465SHiten Pandya.\" 2524503465SHiten Pandya.\" Copyright (c) 1996, 1997, 1998, 2001 The NetBSD Foundation, Inc. 2624503465SHiten Pandya.\" All rights reserved. 2724503465SHiten Pandya.\" 2824503465SHiten Pandya.\" This code is derived from software contributed to The NetBSD Foundation 2924503465SHiten Pandya.\" by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 3024503465SHiten Pandya.\" NASA Ames Research Center. 3124503465SHiten Pandya.\" 3224503465SHiten Pandya.\" Redistribution and use in source and binary forms, with or without 3324503465SHiten Pandya.\" modification, are permitted provided that the following conditions 3424503465SHiten Pandya.\" are met: 3524503465SHiten Pandya.\" 1. Redistributions of source code must retain the above copyright 3624503465SHiten Pandya.\" notice, this list of conditions and the following disclaimer. 3724503465SHiten Pandya.\" 2. Redistributions in binary form must reproduce the above copyright 3824503465SHiten Pandya.\" notice, this list of conditions and the following disclaimer in the 3924503465SHiten Pandya.\" documentation and/or other materials provided with the distribution. 4024503465SHiten Pandya.\" 4124503465SHiten Pandya.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 4224503465SHiten Pandya.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 4324503465SHiten Pandya.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4424503465SHiten Pandya.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 4524503465SHiten Pandya.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 4624503465SHiten Pandya.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 4724503465SHiten Pandya.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 4824503465SHiten Pandya.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 4924503465SHiten Pandya.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5024503465SHiten Pandya.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 5124503465SHiten Pandya.\" POSSIBILITY OF SUCH DAMAGE. 5224503465SHiten Pandya.\" 5324503465SHiten Pandya.\" $FreeBSD$ 5424503465SHiten Pandya.\" $NetBSD: bus_dma.9,v 1.25 2002/10/14 13:43:16 wiz Exp $ 5524503465SHiten Pandya.\" 569c0e3d3aSJohn Baldwin.Dd May 25, 2020 5724503465SHiten Pandya.Dt BUS_DMA 9 5824503465SHiten Pandya.Os 5924503465SHiten Pandya.Sh NAME 6024503465SHiten Pandya.Nm bus_dma , 6124503465SHiten Pandya.Nm bus_dma_tag_create , 6224503465SHiten Pandya.Nm bus_dma_tag_destroy , 63757d4fbaSScott Long.Nm bus_dma_template_init , 64757d4fbaSScott Long.Nm bus_dma_template_tag , 65757d4fbaSScott Long.Nm bus_dma_template_clone , 66e18eb7b1SScott Long.Nm bus_dma_template_fill , 67e18eb7b1SScott Long.Nm BUS_DMA_TEMPLATE_FILL , 6824503465SHiten Pandya.Nm bus_dmamap_create , 6924503465SHiten Pandya.Nm bus_dmamap_destroy , 7024503465SHiten Pandya.Nm bus_dmamap_load , 71a073133cSJim Harris.Nm bus_dmamap_load_bio , 72a073133cSJim Harris.Nm bus_dmamap_load_ccb , 73c0341432SJohn Baldwin.Nm bus_dmamap_load_crp , 749c0e3d3aSJohn Baldwin.Nm bus_dmamap_load_crp_buffer , 7524503465SHiten Pandya.Nm bus_dmamap_load_mbuf , 76824f4edfSScott Long.Nm bus_dmamap_load_mbuf_sg , 7724503465SHiten Pandya.Nm bus_dmamap_load_uio , 7824503465SHiten Pandya.Nm bus_dmamap_unload , 7924503465SHiten Pandya.Nm bus_dmamap_sync , 8024503465SHiten Pandya.Nm bus_dmamem_alloc , 81a1bd700aSHiten Pandya.Nm bus_dmamem_free 8224503465SHiten Pandya.Nd Bus and Machine Independent DMA Mapping Interface 8324503465SHiten Pandya.Sh SYNOPSIS 8424503465SHiten Pandya.In machine/bus.h 8524503465SHiten Pandya.Ft int 8624503465SHiten Pandya.Fn bus_dma_tag_create "bus_dma_tag_t parent" "bus_size_t alignment" \ 875c8d3de0SJohn Baldwin"bus_addr_t boundary" "bus_addr_t lowaddr" "bus_addr_t highaddr" \ 8824503465SHiten Pandya"bus_dma_filter_t *filtfunc" "void *filtfuncarg" "bus_size_t maxsize" \ 890e5f9b7dSScott Long"int nsegments" "bus_size_t maxsegsz" "int flags" "bus_dma_lock_t *lockfunc" \ 900e5f9b7dSScott Long"void *lockfuncarg" "bus_dma_tag_t *dmat" 9124503465SHiten Pandya.Ft int 9224503465SHiten Pandya.Fn bus_dma_tag_destroy "bus_dma_tag_t dmat" 93757d4fbaSScott Long.Ft void 94757d4fbaSScott Long.Fo bus_dma_template_init 95e18eb7b1SScott Long.Fa "bus_dma_template_t *template" 96757d4fbaSScott Long.Fa "bus_dma_tag_t parent" 97757d4fbaSScott Long.Fc 98757d4fbaSScott Long.Ft int 99757d4fbaSScott Long.Fo bus_dma_template_tag 100e18eb7b1SScott Long.Fa "bus_dma_template_t *template" 101757d4fbaSScott Long.Fa "bus_dma_tag_t *dmat" 102757d4fbaSScott Long.Fc 103757d4fbaSScott Long.Ft void 104757d4fbaSScott Long.Fo bus_dma_template_clone 105e18eb7b1SScott Long.Fa "bus_dma_template_t *template" 106757d4fbaSScott Long.Fa "bus_dma_tag_t dmat" 107757d4fbaSScott Long.Fc 108e18eb7b1SScott Long.Ft void 109e18eb7b1SScott Long.Fo bus_dma_template_fill 110e18eb7b1SScott Long.Fa "bus_dma_template_t *template" 111e18eb7b1SScott Long.Fa "bus_dma_param_t params[]" 112e18eb7b1SScott Long.Fa "u_int count" 113e18eb7b1SScott Long.Fc 114e18eb7b1SScott Long.Fo BUS_DMA_TEMPLATE_FILL 115e18eb7b1SScott Long.Fa "bus_dma_template_t *template" 116e18eb7b1SScott Long.Fa "bus_dma_param_t param ..." 117e18eb7b1SScott Long.Fc 11824503465SHiten Pandya.Ft int 11924503465SHiten Pandya.Fn bus_dmamap_create "bus_dma_tag_t dmat" "int flags" "bus_dmamap_t *mapp" 12024503465SHiten Pandya.Ft int 12124503465SHiten Pandya.Fn bus_dmamap_destroy "bus_dma_tag_t dmat" "bus_dmamap_t map" 12224503465SHiten Pandya.Ft int 12324503465SHiten Pandya.Fn bus_dmamap_load "bus_dma_tag_t dmat" "bus_dmamap_t map" "void *buf" \ 12424503465SHiten Pandya"bus_size_t buflen" "bus_dmamap_callback_t *callback" "void *callback_arg" \ 12524503465SHiten Pandya"int flags" 12624503465SHiten Pandya.Ft int 127a073133cSJim Harris.Fn bus_dmamap_load_bio "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 128a073133cSJim Harris"struct bio *bio" "bus_dmamap_callback_t *callback" "void *callback_arg" \ 129a073133cSJim Harris"int flags" 130a073133cSJim Harris.Ft int 131a073133cSJim Harris.Fn bus_dmamap_load_ccb "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 132a073133cSJim Harris"union ccb *ccb" "bus_dmamap_callback_t *callback" "void *callback_arg" \ 133a073133cSJim Harris"int flags" 134a073133cSJim Harris.Ft int 135c0341432SJohn Baldwin.Fn bus_dmamap_load_crp "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 136c0341432SJohn Baldwin"struct crypto *crp" "bus_dmamap_callback_t *callback" "void *callback_arg" \ 137c0341432SJohn Baldwin"int flags" 138c0341432SJohn Baldwin.Ft int 1399c0e3d3aSJohn Baldwin.Fn bus_dmamap_load_crp_buffer "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 1409c0e3d3aSJohn Baldwin"struct crypto_buffer *cb" "bus_dmamap_callback_t *callback" \ 1419c0e3d3aSJohn Baldwin"void *callback_arg" "int flags" 1429c0e3d3aSJohn Baldwin.Ft int 14324503465SHiten Pandya.Fn bus_dmamap_load_mbuf "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 14424503465SHiten Pandya"struct mbuf *mbuf" "bus_dmamap_callback2_t *callback" "void *callback_arg" \ 14524503465SHiten Pandya"int flags" 14624503465SHiten Pandya.Ft int 147824f4edfSScott Long.Fn bus_dmamap_load_mbuf_sg "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 148824f4edfSScott Long"struct mbuf *mbuf" "bus_dma_segment_t *segs" "int *nsegs" "int flags" 149824f4edfSScott Long.Ft int 15024503465SHiten Pandya.Fn bus_dmamap_load_uio "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 15124503465SHiten Pandya"struct uio *uio" "bus_dmamap_callback2_t *callback" "void *callback_arg" \ 15224503465SHiten Pandya"int flags" 15324503465SHiten Pandya.Ft void 15424503465SHiten Pandya.Fn bus_dmamap_unload "bus_dma_tag_t dmat" "bus_dmamap_t map" 15524503465SHiten Pandya.Ft void 15624503465SHiten Pandya.Fn bus_dmamap_sync "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 15724503465SHiten Pandya"op" 158cbfef69cSWarner Losh.Ft int 159cbfef69cSWarner Losh.Fn bus_dmamem_alloc "bus_dma_tag_t dmat" "void **vaddr" \ 160cbfef69cSWarner Losh"int flags" "bus_dmamap_t *mapp" 16124503465SHiten Pandya.Ft void 16224503465SHiten Pandya.Fn bus_dmamem_free "bus_dma_tag_t dmat" "void *vaddr" \ 16324503465SHiten Pandya"bus_dmamap_t map" 16424503465SHiten Pandya.Sh DESCRIPTION 16524503465SHiten PandyaDirect Memory Access (DMA) is a method of transferring data 16624503465SHiten Pandyawithout involving the CPU, thus providing higher performance. 16724503465SHiten PandyaA DMA transaction can be achieved between device to memory, 16824503465SHiten Pandyadevice to device, or memory to memory. 16924503465SHiten Pandya.Pp 17024503465SHiten PandyaThe 17124503465SHiten Pandya.Nm 17224503465SHiten PandyaAPI is a bus, device, and machine-independent (MI) interface to 17324503465SHiten PandyaDMA mechanisms. 17424503465SHiten PandyaIt provides the client with flexibility and simplicity by 17524503465SHiten Pandyaabstracting machine dependent issues like setting up 17624503465SHiten PandyaDMA mappings, handling cache issues, bus specific features 17724503465SHiten Pandyaand limitations. 178306095b8SJohn Baldwin.Sh OVERVIEW 179306095b8SJohn BaldwinA tag structure 180306095b8SJohn Baldwin.Vt ( bus_dma_tag_t ) 181306095b8SJohn Baldwinis used to describe the properties of a group of related DMA 182306095b8SJohn Baldwintransactions. 183306095b8SJohn BaldwinOne way to view this is that a tag describes the limitations of a DMA engine. 184306095b8SJohn BaldwinFor example, if a DMA engine in a device is limited to 32-bit addresses, 185306095b8SJohn Baldwinthat limitation is specified by a parameter when creating the tag 186306095b8SJohn Baldwinfor that device. 187306095b8SJohn BaldwinSimilarly, a tag can be marked as requiring buffers whose addresses are 188306095b8SJohn Baldwinaligned to a specific boundary. 189306095b8SJohn Baldwin.Pp 190306095b8SJohn BaldwinSome devices may require multiple tags to describe DMA 191306095b8SJohn Baldwintransactions with differing properties. 192306095b8SJohn BaldwinFor example, a device might require 16-byte alignment of its descriptor ring 193306095b8SJohn Baldwinwhile permitting arbitrary alignment of I/O buffers. 194306095b8SJohn BaldwinIn this case, 195306095b8SJohn Baldwinthe driver must create one tag for the descriptor ring and a separate tag for 196306095b8SJohn BaldwinI/O buffers. 197306095b8SJohn BaldwinIf a device has restrictions that are common to all DMA transactions 198306095b8SJohn Baldwinin addition to restrictions that differ between unrelated groups of 199306095b8SJohn Baldwintransactions, 200306095b8SJohn Baldwinthe driver can first create a 201306095b8SJohn Baldwin.Dq parent 202306095b8SJohn Baldwintag that decribes the common restrictions. 203306095b8SJohn BaldwinThe per-group tags can then inherit these restrictions from this 204306095b8SJohn Baldwin.Dq parent 205306095b8SJohn Baldwintag rather than having to list them explicitly when creating the per-group tags. 206306095b8SJohn Baldwin.Pp 207306095b8SJohn BaldwinA mapping structure 208306095b8SJohn Baldwin.Vt ( bus_dmamap_t ) 209306095b8SJohn Baldwinrepresents a mapping of a memory region for DMA. 210306095b8SJohn BaldwinOn systems with I/O MMUs, 211306095b8SJohn Baldwinthe mapping structure tracks any I/O MMU entries used by a request. 212306095b8SJohn BaldwinFor DMA requests that require bounce pages, 213306095b8SJohn Baldwinthe mapping tracks the bounce pages used. 214306095b8SJohn Baldwin.Pp 215306095b8SJohn BaldwinTo prepare for one or more DMA transactions, 216306095b8SJohn Baldwina mapping must be bound to a memory region by calling one of the 217306095b8SJohn Baldwin.Fn bus_dmamap_load 218306095b8SJohn Baldwinfunctions. 219306095b8SJohn BaldwinThese functions configure the mapping which can include programming entries 220306095b8SJohn Baldwinin an I/O MMU and/or allocating bounce pages. 221306095b8SJohn BaldwinAn output of these functions 222306095b8SJohn Baldwin(either directly or indirectly by invoking a callback routine) 223306095b8SJohn Baldwinis the list of scatter/gather address ranges a consumer can pass to a DMA 224306095b8SJohn Baldwinengine to access the memory region. 225306095b8SJohn BaldwinWhen a mapping is no longer needed, 226306095b8SJohn Baldwinthe mapping must be unloaded via 227306095b8SJohn Baldwin.Fn bus_dmamap_unload . 228306095b8SJohn Baldwin.Pp 229306095b8SJohn BaldwinBefore and after each DMA transaction, 230306095b8SJohn Baldwin.Fn bus_dmamap_sync 231306095b8SJohn Baldwinmust be used to ensure that the correct data is used by the DMA engine and 232306095b8SJohn Baldwinthe CPU. 233306095b8SJohn BaldwinIf a mapping uses bounce pages, 234306095b8SJohn Baldwinthe sync operations copy data between the bounce pages and the memory region 235306095b8SJohn Baldwinbound to the mapping. 236306095b8SJohn BaldwinSync operations also handle architecture-specific details such as CPU cache 237306095b8SJohn Baldwinflushing and CPU memory operation ordering. 238306095b8SJohn Baldwin.Sh STATIC VS DYNAMIC 239306095b8SJohn Baldwin.Nm 240306095b8SJohn Baldwinhandles two types of DMA transactions: static and dynamic. 241306095b8SJohn BaldwinStatic transactions are used with a long-lived memory region that is reused 242306095b8SJohn Baldwinfor many transactions such as a descriptor ring. 243306095b8SJohn BaldwinDynamic transactions are used for transfers to or from transient buffers 244306095b8SJohn Baldwinsuch as I/O buffers holding a network packet or disk block. 245306095b8SJohn BaldwinEach transaction type uses a different subset of the 246306095b8SJohn Baldwin.Nm 247306095b8SJohn BaldwinAPI. 248306095b8SJohn Baldwin.Ss Static Transactions 249306095b8SJohn BaldwinStatic transactions use memory regions allocated by 250306095b8SJohn Baldwin.Nm . 251306095b8SJohn BaldwinEach static memory region is allocated by calling 252306095b8SJohn Baldwin.Fn bus_dmamem_alloc . 253306095b8SJohn BaldwinThis function requires a valid tag describing the properties of the 254306095b8SJohn BaldwinDMA transactions to this region such as alignment or address restrictions. 255306095b8SJohn BaldwinMultiple regions can share a single tag if they share the same restrictions. 256306095b8SJohn Baldwin.Pp 257306095b8SJohn Baldwin.Fn bus_dmamem_alloc 258306095b8SJohn Baldwinallocates a memory region along with a mapping object. 259306095b8SJohn BaldwinThe associated tag, memory region, and mapping object must then be passed to 260306095b8SJohn Baldwin.Fn bus_dmamap_load 261306095b8SJohn Baldwinto bind the mapping to the allocated region and obtain the 262306095b8SJohn Baldwinscatter/gather list. 263306095b8SJohn Baldwin.Pp 264306095b8SJohn BaldwinIt is expected that 265306095b8SJohn Baldwin.Fn bus_dmamem_alloc 266306095b8SJohn Baldwinwill attempt to allocate memory requiring less expensive sync operations 267306095b8SJohn Baldwin(for example, implementations should not allocate regions requiring bounce 268306095b8SJohn Baldwinpages), 269306095b8SJohn Baldwinbut sync operations should still be used. 270306095b8SJohn BaldwinFor example, a driver should use 271306095b8SJohn Baldwin.Fn bus_dmamap_sync 272306095b8SJohn Baldwinin an interrupt handler before reading descriptor ring entries written by the 273306095b8SJohn Baldwindevice prior to the interrupt. 274306095b8SJohn Baldwin.Pp 275306095b8SJohn BaldwinWhen a consumer is finished with a memory region, 276306095b8SJohn Baldwinit should unload the mapping via 277306095b8SJohn Baldwin.Fn bus_dmamap_unload 278306095b8SJohn Baldwinand then release the memory region and mapping object via 279306095b8SJohn Baldwin.Fn bus_dmamem_free . 280306095b8SJohn Baldwin.Ss Dynamic Transactions 281306095b8SJohn BaldwinDynamic transactions map memory regions provided by other parts of the system. 282306095b8SJohn BaldwinA tag must be created via 283306095b8SJohn Baldwin.Fn bus_dma_tag_create 284306095b8SJohn Baldwinto describe the DMA transactions to and from these memory regions, 285306095b8SJohn Baldwinand a pool of mapping objects must be allocated via 286306095b8SJohn Baldwin.Fn bus_dmamap_create 287306095b8SJohn Baldwinto track the mappings of any in-flight transactions. 288306095b8SJohn Baldwin.Pp 289306095b8SJohn BaldwinWhen a consumer wishes to schedule a transaction for a memory region, 290306095b8SJohn Baldwinthe consumer must first obtain an unused mapping object from its pool 291306095b8SJohn Baldwinof mapping objects. 292306095b8SJohn BaldwinThe memory region must be bound to the mapping object via one of the 293306095b8SJohn Baldwin.Fn bus_dmamap_load 294306095b8SJohn Baldwinfunctions. 295306095b8SJohn BaldwinBefore scheduling the transaction, 296306095b8SJohn Baldwinthe consumer should sync the memory region via 297306095b8SJohn Baldwin.Fn bus_dmamap_sync 298306095b8SJohn Baldwinwith one or more of the 299306095b8SJohn Baldwin.Dq PRE 300306095b8SJohn Baldwinflags. 301306095b8SJohn BaldwinAfter the transaction has completed, 302306095b8SJohn Baldwinthe consumer should sync the memory region via 303306095b8SJohn Baldwin.Fn bus_dmamap_sync 304306095b8SJohn Baldwinwith one or more of the 305306095b8SJohn Baldwin.Dq POST 306306095b8SJohn Baldwinflags. 307306095b8SJohn BaldwinThe mapping can then be unloaded via 308306095b8SJohn Baldwin.Fn bus_dmamap_unload , 309306095b8SJohn Baldwinand the mapping object can be returned to the pool of unused mapping objects. 310306095b8SJohn Baldwin.Pp 311306095b8SJohn BaldwinWhen a consumer is no longer scheduling DMA transactions, 312306095b8SJohn Baldwinthe mapping objects should be freed via 313306095b8SJohn Baldwin.Fn bus_dmamap_destroy , 314306095b8SJohn Baldwinand the tag should be freed via 315306095b8SJohn Baldwin.Fn bus_dma_tag_destroy . 31624503465SHiten Pandya.Sh STRUCTURES AND TYPES 31781ae4b8dSRuslan Ermilov.Bl -tag -width indent 31824503465SHiten Pandya.It Vt bus_dma_tag_t 31924503465SHiten PandyaA machine-dependent (MD) opaque type that describes the 320306095b8SJohn Baldwincharacteristics of a group of DMA transactions. 32124503465SHiten PandyaDMA tags are organized into a hierarchy, with each child 32224503465SHiten Pandyatag inheriting the restrictions of its parent. 32324503465SHiten PandyaThis allows all devices along the path of DMA transactions 32424503465SHiten Pandyato contribute to the constraints of those transactions. 325757d4fbaSScott Long.It Vt bus_dma_template_t 326d58ff30aSScott LongA template is a structure for creating a 327757d4fbaSScott Long.Fa bus_dma_tag_t 328757d4fbaSScott Longfrom a set of defaults. 329757d4fbaSScott LongOnce initialized with 330757d4fbaSScott Long.Fn bus_dma_template_init , 331757d4fbaSScott Longa driver can over-ride individual fields to suit its needs. 332d58ff30aSScott LongThe following fields start with the indicated default values: 333757d4fbaSScott Long.Bd -literal 334757d4fbaSScott Long alignment 1 335757d4fbaSScott Long boundary 0 336757d4fbaSScott Long lowaddr BUS_SPACE_MAXADDR 337757d4fbaSScott Long highaddr BUS_SPACE_MAXADDR 338757d4fbaSScott Long maxsize BUS_SPACE_MAXSIZE 339757d4fbaSScott Long nsegments BUS_SPACE_UNRESTRICTED 340757d4fbaSScott Long maxsegsize BUS_SPACE_MAXSIZE 341757d4fbaSScott Long flags 0 342757d4fbaSScott Long lockfunc NULL 343757d4fbaSScott Long lockfuncarg NULL 344757d4fbaSScott Long.Ed 345757d4fbaSScott Long.Pp 346757d4fbaSScott LongDescriptions of each field are documented with 347757d4fbaSScott Long.Fn bus_dma_tag_create . 348757d4fbaSScott LongNote that the 349757d4fbaSScott Long.Fa filtfunc 350757d4fbaSScott Longand 351757d4fbaSScott Long.Fa filtfuncarg 352757d4fbaSScott Longattributes of the DMA tag are not supported with templates. 35324503465SHiten Pandya.It Vt bus_dma_filter_t 35424503465SHiten PandyaClient specified address filter having the format: 35581ae4b8dSRuslan Ermilov.Bl -tag -width indent 35624503465SHiten Pandya.It Ft int 35724503465SHiten Pandya.Fn "client_filter" "void *filtarg" "bus_addr_t testaddr" 35824503465SHiten Pandya.El 35981ae4b8dSRuslan Ermilov.Pp 36024503465SHiten PandyaAddress filters can be specified during tag creation to allow 36159892d33SRuslan Ermilovfor devices whose DMA address restrictions cannot be specified 36224503465SHiten Pandyaby a single window. 36324503465SHiten PandyaThe 36424503465SHiten Pandya.Fa filtarg 3652f46d621SJohn Baldwinargument is specified by the client during tag creation to be passed to all 36624503465SHiten Pandyainvocations of the callback. 36724503465SHiten PandyaThe 36824503465SHiten Pandya.Fa testaddr 36924503465SHiten Pandyaargument contains a potential starting address of a DMA mapping. 37024503465SHiten PandyaThe filter function operates on the set of addresses from 37124503465SHiten Pandya.Fa testaddr 37224503465SHiten Pandyato 37324503465SHiten Pandya.Ql trunc_page(testaddr) + PAGE_SIZE - 1 , 37424503465SHiten Pandyainclusive. 3752f46d621SJohn BaldwinThe filter function should return zero if any mapping in this range 3762f46d621SJohn Baldwincan be accommodated by the device and non-zero otherwise. 377d58ff30aSScott Long.Pp 378d58ff30aSScott Long.Em Note: The use of filters is deprecated. Proper operation is not guaranteed. 37924503465SHiten Pandya.It Vt bus_dma_segment_t 38024503465SHiten PandyaA machine-dependent type that describes individual 38124503465SHiten PandyaDMA segments. 3822f46d621SJohn BaldwinIt contains the following fields: 38324503465SHiten Pandya.Bd -literal 38424503465SHiten Pandya bus_addr_t ds_addr; 38524503465SHiten Pandya bus_size_t ds_len; 38624503465SHiten Pandya.Ed 38781ae4b8dSRuslan Ermilov.Pp 38824503465SHiten PandyaThe 38924503465SHiten Pandya.Fa ds_addr 39024503465SHiten Pandyafield contains the device visible address of the DMA segment, and 39124503465SHiten Pandya.Fa ds_len 39224503465SHiten Pandyacontains the length of the DMA segment. 39324503465SHiten PandyaAlthough the DMA segments returned by a mapping call will adhere to 39424503465SHiten Pandyaall restrictions necessary for a successful DMA operation, some conversion 3955203edcdSRuslan Ermilov(e.g.\& a conversion from host byte order to the device's byte order) is 39624503465SHiten Pandyaalmost always required when presenting segment information to the device. 39724503465SHiten Pandya.It Vt bus_dmamap_t 39824503465SHiten PandyaA machine-dependent opaque type describing an individual mapping. 39971bb1f9bSScott LongOne map is used for each memory allocation that will be loaded. 40071bb1f9bSScott LongMaps can be reused once they have been unloaded. 40171bb1f9bSScott LongMultiple maps can be associated with one DMA tag. 40281ae4b8dSRuslan ErmilovWhile the value of the map may evaluate to 40381ae4b8dSRuslan Ermilov.Dv NULL 40481ae4b8dSRuslan Ermilovon some platforms under certain conditions, 40581ae4b8dSRuslan Ermilovit should never be assumed that it will be 40681ae4b8dSRuslan Ermilov.Dv NULL 40781ae4b8dSRuslan Ermilovin all cases. 40824503465SHiten Pandya.It Vt bus_dmamap_callback_t 40924503465SHiten PandyaClient specified callback for receiving mapping information resulting from 41024503465SHiten Pandyathe load of a 41124503465SHiten Pandya.Vt bus_dmamap_t 41224503465SHiten Pandyavia 413a073133cSJim Harris.Fn bus_dmamap_load , 414c0341432SJohn Baldwin.Fn bus_dmamap_load_bio , 415c0341432SJohn Baldwin.Fn bus_dmamap_load_ccb , 4169c0e3d3aSJohn Baldwin.Fn bus_dmamap_load_crp , 417a073133cSJim Harrisor 4189c0e3d3aSJohn Baldwin.Fn bus_dmamap_load_crp_buffer . 41924503465SHiten PandyaCallbacks are of the format: 42081ae4b8dSRuslan Ermilov.Bl -tag -width indent 42124503465SHiten Pandya.It Ft void 42224503465SHiten Pandya.Fn "client_callback" "void *callback_arg" "bus_dma_segment_t *segs" \ 42324503465SHiten Pandya"int nseg" "int error" 42424503465SHiten Pandya.El 42581ae4b8dSRuslan Ermilov.Pp 42624503465SHiten PandyaThe 42724503465SHiten Pandya.Fa callback_arg 42824503465SHiten Pandyais the callback argument passed to dmamap load functions. 42924503465SHiten PandyaThe 43024503465SHiten Pandya.Fa segs 43124503465SHiten Pandyaand 43224503465SHiten Pandya.Fa nseg 4332f46d621SJohn Baldwinarguments describe an array of 43424503465SHiten Pandya.Vt bus_dma_segment_t 43524503465SHiten Pandyastructures that represent the mapping. 43624503465SHiten PandyaThis array is only valid within the scope of the callback function. 43724503465SHiten PandyaThe success or failure of the mapping is indicated by the 43824503465SHiten Pandya.Fa error 4392f46d621SJohn Baldwinargument. 44024503465SHiten PandyaMore information on the use of callbacks can be found in the 44124503465SHiten Pandyadescription of the individual dmamap load functions. 44224503465SHiten Pandya.It Vt bus_dmamap_callback2_t 44324503465SHiten PandyaClient specified callback for receiving mapping information resulting from 44424503465SHiten Pandyathe load of a 44524503465SHiten Pandya.Vt bus_dmamap_t 44624503465SHiten Pandyavia 44724503465SHiten Pandya.Fn bus_dmamap_load_uio 44824503465SHiten Pandyaor 44924503465SHiten Pandya.Fn bus_dmamap_load_mbuf . 45081ae4b8dSRuslan Ermilov.Pp 45124503465SHiten PandyaCallback2s are of the format: 45281ae4b8dSRuslan Ermilov.Bl -tag -width indent 45324503465SHiten Pandya.It Ft void 45424503465SHiten Pandya.Fn "client_callback2" "void *callback_arg" "bus_dma_segment_t *segs" \ 45524503465SHiten Pandya"int nseg" "bus_size_t mapsize" "int error" 45624503465SHiten Pandya.El 45781ae4b8dSRuslan Ermilov.Pp 45824503465SHiten PandyaCallback2's behavior is the same as 45924503465SHiten Pandya.Vt bus_dmamap_callback_t 46024503465SHiten Pandyawith the addition that the length of the data mapped is provided via 46124503465SHiten Pandya.Fa mapsize . 46224503465SHiten Pandya.It Vt bus_dmasync_op_t 46324503465SHiten PandyaMemory synchronization operation specifier. 46436a142c4SRuslan ErmilovBus DMA requires explicit synchronization of memory with its device 46524503465SHiten Pandyavisible mapping in order to guarantee memory coherency. 46624503465SHiten PandyaThe 46724503465SHiten Pandya.Vt bus_dmasync_op_t 46824503465SHiten Pandyaallows the type of DMA operation that will be or has been performed 46924503465SHiten Pandyato be communicated to the system so that the correct coherency measures 47024503465SHiten Pandyaare taken. 4711cb112abSScott LongThe operations are represented as bitfield flags that can be combined together, 4721cb112abSScott Longthough it only makes sense to combine PRE flags or POST flags, not both. 4731cb112abSScott LongSee the 474e8c9966dSJohn-Mark Gurney.Fn bus_dmamap_sync 4751cb112abSScott Longdescription below for more details on how to use these operations. 4766f3d2701SScott Long.Pp 4776f3d2701SScott LongAll operations specified below are performed from the host memory point of view, 4786f3d2701SScott Longwhere a read implies data coming from the device to the host memory, and a write 4796f3d2701SScott Longimplies data going from the host memory to the device. 48007bf564cSRuslan ErmilovAlternatively, the operations can be thought of in terms of driver operations, 481a79d1e83SScott Longwhere reading a network packet or storage sector corresponds to a read operation 482a79d1e83SScott Longin 483a79d1e83SScott Long.Nm . 48407bf564cSRuslan Ermilov.Bl -tag -width ".Dv BUS_DMASYNC_POSTWRITE" 48524503465SHiten Pandya.It Dv BUS_DMASYNC_PREREAD 4866f3d2701SScott LongPerform any synchronization required prior to an update of host memory by the 48707bf564cSRuslan Ermilovdevice. 48824503465SHiten Pandya.It Dv BUS_DMASYNC_PREWRITE 4896f3d2701SScott LongPerform any synchronization required after an update of host memory by the CPU 49007bf564cSRuslan Ermilovand prior to device access to host memory. 49124503465SHiten Pandya.It Dv BUS_DMASYNC_POSTREAD 49207bf564cSRuslan ErmilovPerform any synchronization required after an update of host memory by the 49307bf564cSRuslan Ermilovdevice and prior to CPU access to host memory. 49424503465SHiten Pandya.It Dv BUS_DMASYNC_POSTWRITE 4952f46d621SJohn BaldwinPerform any synchronization required after device access to host memory. 49624503465SHiten Pandya.El 4970e5f9b7dSScott Long.It Vt bus_dma_lock_t 498c7d3a65dSHiten PandyaClient specified lock/mutex manipulation method. 499c7d3a65dSHiten PandyaThis will be called from 5000e5f9b7dSScott Longwithin busdma whenever a client lock needs to be manipulated. 5014d855643SWarner LoshIn its current form, the function will be called immediately before 5025bbaa5cfSWojciech A. Koszekthe callback for a DMA load operation that has been deferred with 5034d855643SWarner Losh.Dv BUS_DMA_LOCK 5044d855643SWarner Loshand immediately after with 5054d855643SWarner Losh.Dv BUS_DMA_UNLOCK . 5064d855643SWarner LoshIf the load operation does not need to be deferred, then it 5074d855643SWarner Loshwill not be called since the function loading the map should 5084d855643SWarner Loshbe holding the appropriate locks. 5090e5f9b7dSScott LongThis method is of the format: 51081ae4b8dSRuslan Ermilov.Bl -tag -width indent 5110e5f9b7dSScott Long.It Ft void 5120e5f9b7dSScott Long.Fn "lockfunc" "void *lockfunc_arg" "bus_dma_lock_op_t op" 51324503465SHiten Pandya.El 51481ae4b8dSRuslan Ermilov.Pp 5152f46d621SJohn BaldwinThe 5162f46d621SJohn Baldwin.Fa lockfuncarg 5172f46d621SJohn Baldwinargument is specified by the client during tag creation to be passed to all 5182f46d621SJohn Baldwininvocations of the callback. 5192f46d621SJohn BaldwinThe 5202f46d621SJohn Baldwin.Fa op 5212f46d621SJohn Baldwinargument specifies the lock operation to perform. 5222f46d621SJohn Baldwin.Pp 5230e5f9b7dSScott LongTwo 5240e5f9b7dSScott Long.Vt lockfunc 5250e5f9b7dSScott Longimplementations are provided for convenience. 5260e5f9b7dSScott Long.Fn busdma_lock_mutex 5272f46d621SJohn Baldwinperforms standard mutex operations on the sleep mutex provided via 5280e5f9b7dSScott Long.Fa lockfuncarg . 5290e5f9b7dSScott Long.Fn dflt_lock 530c7d3a65dSHiten Pandyawill generate a system panic if it is called. 531c7d3a65dSHiten PandyaIt is substituted into the tag when 5320e5f9b7dSScott Long.Fa lockfunc 53381ae4b8dSRuslan Ermilovis passed as 53481ae4b8dSRuslan Ermilov.Dv NULL 53581ae4b8dSRuslan Ermilovto 5362f46d621SJohn Baldwin.Fn bus_dma_tag_create 5372f46d621SJohn Baldwinand is useful for tags that should not be used with deferred load operations. 5380e5f9b7dSScott Long.It Vt bus_dma_lock_op_t 5390e5f9b7dSScott LongOperations to be performed by the client-specified 5400e5f9b7dSScott Long.Fn lockfunc . 54181ae4b8dSRuslan Ermilov.Bl -tag -width ".Dv BUS_DMA_UNLOCK" 5420e5f9b7dSScott Long.It Dv BUS_DMA_LOCK 5432988974bSMike PritchardAcquires and/or locks the client locking primitive. 5440e5f9b7dSScott Long.It Dv BUS_DMA_UNLOCK 5450e5f9b7dSScott LongReleases and/or unlocks the client locking primitive. 5460e5f9b7dSScott Long.El 5470e5f9b7dSScott Long.El 54824503465SHiten Pandya.Sh FUNCTIONS 54981ae4b8dSRuslan Ermilov.Bl -tag -width indent 55024503465SHiten Pandya.It Fn bus_dma_tag_create "parent" "alignment" "boundary" "lowaddr" \ 55124503465SHiten Pandya"highaddr" "*filtfunc" "*filtfuncarg" "maxsize" "nsegments" "maxsegsz" \ 5529b5c4b66SScott Long"flags" "lockfunc" "lockfuncarg" "*dmat" 553306095b8SJohn BaldwinAllocates a DMA tag, and initializes it according to 55424503465SHiten Pandyathe arguments provided: 55581ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa filtfuncarg" 55624503465SHiten Pandya.It Fa parent 557306095b8SJohn BaldwinA parent tag from which to inherit restrictions. 558306095b8SJohn BaldwinThe restrictions passed in other arguments can only further tighten the 559306095b8SJohn Baldwinrestrictions inherited from the parent tag. 560306095b8SJohn Baldwin.Pp 561306095b8SJohn BaldwinAll tags created by a device driver must inherit from the tag returned by 562306095b8SJohn Baldwin.Fn bus_get_dma_tag 563306095b8SJohn Baldwinto honor restrictions between the parent bridge, CPU memory, and the 56424503465SHiten Pandyadevice. 56524503465SHiten Pandya.It Fa alignment 56624503465SHiten PandyaAlignment constraint, in bytes, of any mappings created using this tag. 56724503465SHiten PandyaThe alignment must be a power of 2. 56824503465SHiten PandyaHardware that can DMA starting at any address would specify 56924503465SHiten Pandya.Em 1 57024503465SHiten Pandyafor byte alignment. 57124503465SHiten PandyaHardware requiring DMA transfers to start on a multiple of 4K 57224503465SHiten Pandyawould specify 57324503465SHiten Pandya.Em 4096 . 57424503465SHiten Pandya.It Fa boundary 57524503465SHiten PandyaBoundary constraint, in bytes, of the target DMA memory region. 57624503465SHiten PandyaThe boundary indicates the set of addresses, all multiples of the 57724503465SHiten Pandyaboundary argument, that cannot be crossed by a single 57824503465SHiten Pandya.Vt bus_dma_segment_t . 5792d4071acSChristian BruefferThe boundary must be a power of 2 and must be no smaller than the 580ab0b83b5SScott Longmaximum segment size. 58124503465SHiten Pandya.Ql 0 58224503465SHiten Pandyaindicates that there are no boundary restrictions. 58381ae4b8dSRuslan Ermilov.It Fa lowaddr , highaddr 58424503465SHiten PandyaBounds of the window of bus address space that 58524503465SHiten Pandya.Em cannot 58624503465SHiten Pandyabe directly accessed by the device. 5875bbaa5cfSWojciech A. KoszekThe window contains all addresses greater than 5885bbaa5cfSWojciech A. Koszek.Fa lowaddr 5895bbaa5cfSWojciech A. Koszekand less than or equal to 5905bbaa5cfSWojciech A. Koszek.Fa highaddr . 5915bbaa5cfSWojciech A. KoszekFor example, a device incapable of DMA above 4GB, would specify a 5925bbaa5cfSWojciech A. Koszek.Fa highaddr 5935bbaa5cfSWojciech A. Koszekof 59424503465SHiten Pandya.Dv BUS_SPACE_MAXADDR 5955bbaa5cfSWojciech A. Koszekand a 5965bbaa5cfSWojciech A. Koszek.Fa lowaddr 5975bbaa5cfSWojciech A. Koszekof 59824503465SHiten Pandya.Dv BUS_SPACE_MAXADDR_32BIT . 5995bbaa5cfSWojciech A. KoszekSimilarly a device that can only perform DMA to addresses below 6005bbaa5cfSWojciech A. Koszek16MB would specify a 6015bbaa5cfSWojciech A. Koszek.Fa highaddr 6025bbaa5cfSWojciech A. Koszekof 60324503465SHiten Pandya.Dv BUS_SPACE_MAXADDR 6045bbaa5cfSWojciech A. Koszekand a 6055bbaa5cfSWojciech A. Koszek.Fa lowaddr 6065bbaa5cfSWojciech A. Koszekof 60724503465SHiten Pandya.Dv BUS_SPACE_MAXADDR_24BIT . 608306095b8SJohn BaldwinSome implementations require that some region of device visible 60924503465SHiten Pandyaaddress space, overlapping available host memory, be outside the 61024503465SHiten Pandyawindow. 61124503465SHiten PandyaThis area of 61224503465SHiten Pandya.Ql safe memory 61324503465SHiten Pandyais used to bounce requests that would otherwise conflict with 61424503465SHiten Pandyathe exclusion window. 61524503465SHiten Pandya.It Fa filtfunc 61681ae4b8dSRuslan ErmilovOptional filter function (may be 61781ae4b8dSRuslan Ermilov.Dv NULL ) 61881ae4b8dSRuslan Ermilovto be called for any attempt to 61924503465SHiten Pandyamap memory into the window described by 62024503465SHiten Pandya.Fa lowaddr 62124503465SHiten Pandyaand 62224503465SHiten Pandya.Fa highaddr . 62324503465SHiten PandyaA filter function is only required when the single window described 62424503465SHiten Pandyaby 62524503465SHiten Pandya.Fa lowaddr 62624503465SHiten Pandyaand 62724503465SHiten Pandya.Fa highaddr 62824503465SHiten Pandyacannot adequately describe the constraints of the device. 62924503465SHiten PandyaThe filter function will be called for every machine page 63024503465SHiten Pandyathat overlaps the exclusion window. 631d58ff30aSScott Long.Pp 632d58ff30aSScott Long.Em Note: The use of filters is deprecated. Proper operation is not guaranteed. 63324503465SHiten Pandya.It Fa filtfuncarg 63424503465SHiten PandyaArgument passed to all calls to the filter function for this tag. 63581ae4b8dSRuslan ErmilovMay be 63681ae4b8dSRuslan Ermilov.Dv NULL . 63724503465SHiten Pandya.It Fa maxsize 63824503465SHiten PandyaMaximum size, in bytes, of the sum of all segment lengths in a given 63924503465SHiten PandyaDMA mapping associated with this tag. 64024503465SHiten Pandya.It Fa nsegments 64124503465SHiten PandyaNumber of discontinuities (scatter/gather segments) allowed 64224503465SHiten Pandyain a DMA mapped region. 643094dc7e3SJohn-Mark Gurney.It Fa maxsegsz 644094dc7e3SJohn-Mark GurneyMaximum size, in bytes, of a segment in any DMA mapped region associated 645094dc7e3SJohn-Mark Gurneywith 646094dc7e3SJohn-Mark Gurney.Fa dmat . 64724503465SHiten Pandya.It Fa flags 64824503465SHiten PandyaAre as follows: 64981ae4b8dSRuslan Ermilov.Bl -tag -width ".Dv BUS_DMA_ALLOCNOW" 65024503465SHiten Pandya.It Dv BUS_DMA_ALLOCNOW 6510f3a0078SScott LongPre-allocate enough resources to handle at least one map load operation on 65271bb1f9bSScott Longthis tag. 65324503465SHiten PandyaIf sufficient resources are not available, 65424503465SHiten Pandya.Er ENOMEM 65524503465SHiten Pandyais returned. 65671bb1f9bSScott LongThis should not be used for tags that only describe buffers that will be 65771bb1f9bSScott Longallocated with 65871bb1f9bSScott Long.Fn bus_dmamem_alloc . 65971bb1f9bSScott LongAlso, due to resource sharing with other tags, this flag does not guarantee 66071bb1f9bSScott Longthat resources will be allocated or reserved exclusively for this tag. 66171bb1f9bSScott LongIt should be treated only as a minor optimization. 662435b87a9SEmmanuel Vadot.It Dv BUS_DMA_COHERENT 663435b87a9SEmmanuel VadotIndicate that the DMA engine and CPU are cache-coherent. 664435b87a9SEmmanuel VadotCached memory may be used to back allocations created by 665435b87a9SEmmanuel Vadot.Fn bus_dmamem_alloc . 666435b87a9SEmmanuel VadotFor 667435b87a9SEmmanuel Vadot.Fn bus_dma_tag_create , 668435b87a9SEmmanuel Vadotthe 669435b87a9SEmmanuel Vadot.Dv BUS_DMA_COHERENT 670435b87a9SEmmanuel Vadotflag is currently implemented on arm64. 67124503465SHiten Pandya.El 6729b5c4b66SScott Long.It Fa lockfunc 67381ae4b8dSRuslan ErmilovOptional lock manipulation function (may be 67481ae4b8dSRuslan Ermilov.Dv NULL ) 67581ae4b8dSRuslan Ermilovto be called when busdma 676c7d3a65dSHiten Pandyaneeds to manipulate a lock on behalf of the client. 67781ae4b8dSRuslan ErmilovIf 67881ae4b8dSRuslan Ermilov.Dv NULL 67981ae4b8dSRuslan Ermilovis specified, 6809b5c4b66SScott Long.Fn dflt_lock 6819b5c4b66SScott Longis used. 6829b5c4b66SScott Long.It Fa lockfuncarg 6839b5c4b66SScott LongOptional argument to be passed to the function specified by 6849b5c4b66SScott Long.Fa lockfunc . 68524503465SHiten Pandya.It Fa dmat 68624503465SHiten PandyaPointer to a bus_dma_tag_t where the resulting DMA tag will 68724503465SHiten Pandyabe stored. 68824503465SHiten Pandya.El 68924503465SHiten Pandya.Pp 69024503465SHiten PandyaReturns 69124503465SHiten Pandya.Er ENOMEM 69224503465SHiten Pandyaif sufficient memory is not available for tag creation 69324503465SHiten Pandyaor allocating mapping resources. 69424503465SHiten Pandya.It Fn bus_dma_tag_destroy "dmat" 69524503465SHiten PandyaDeallocate the DMA tag 69624503465SHiten Pandya.Fa dmat 69724503465SHiten Pandyathat was created by 69824503465SHiten Pandya.Fn bus_dma_tag_create . 69924503465SHiten Pandya.Pp 70024503465SHiten PandyaReturns 70124503465SHiten Pandya.Er EBUSY 70224503465SHiten Pandyaif any DMA maps remain associated with 70324503465SHiten Pandya.Fa dmat 70424503465SHiten Pandyaor 70524503465SHiten Pandya.Ql 0 70624503465SHiten Pandyaon success. 707757d4fbaSScott Long.It Fn bus_dma_template_init "*template" "parent" 708757d4fbaSScott LongInitializes a 709757d4fbaSScott Long.Fa bus_dma_template_t 710*f2f60544SGordon Berglingstructure. 711*f2f60544SGordon BerglingIf the 712757d4fbaSScott Long.Fa parent 713e18eb7b1SScott Longargument is non-NULL, this parent tag is associated with the template and 714*f2f60544SGordon Berglingwill be compiled into the dma tag that is later created. 715*f2f60544SGordon BerglingThe values of the parent are not copied into the template. 716*f2f60544SGordon BerglingDuring tag creation in 717e18eb7b1SScott Long.Fn bus_dma_tag_template , 718e18eb7b1SScott Longany parameters from the parent tag that are more restrictive than what is 719e18eb7b1SScott Longin the provided template will overwrite what goes into the new tag. 720757d4fbaSScott Long.It Fn bus_dma_template_tag "*template" "*dmat" 721757d4fbaSScott LongUnpacks a template into a tag, and returns the tag via the 722757d4fbaSScott Long.Fa dmat . 723757d4fbaSScott LongAll return values are identical to 724757d4fbaSScott Long.Fn bus_dma_tag_create . 725d58ff30aSScott LongThe template is not modified by this function, and can be reused and/or 726d58ff30aSScott Longfreed upon return. 727757d4fbaSScott Long.It Fn bus_dma_template_clone "*template" "dmat" 728d58ff30aSScott LongCopies the fields from an existing tag to a template. 729*f2f60544SGordon BerglingThe template does not need to be initialized first. 730*f2f60544SGordon BerglingAll of its fields will be overwritten by the values contained in the tag. 731*f2f60544SGordon BerglingWhen paired with 732d58ff30aSScott Long.Fn bus_dma_template_tag , 733d58ff30aSScott Longthis function is useful for creating copies of tags. 734e18eb7b1SScott Long.It Fn bus_dma_template_fill "*template" "params[]" "count" 735e18eb7b1SScott LongFills in the selected fields of the template with the keyed values from the 736e18eb7b1SScott Long.Fa params 737*f2f60544SGordon Berglingarray. 738*f2f60544SGordon BerglingThis is not meant to be called directly, use 739e18eb7b1SScott Long.Fn BUS_DMA_TEMPLATE_FILL 740e18eb7b1SScott Longinstead. 741e18eb7b1SScott Long.It Fn BUS_DMA_TEMPLATE_FILL "*template" "param ..." 742e18eb7b1SScott LongFills in the selected fields of the template with a variable number of 743*f2f60544SGordon Berglingkey-value parameters. 744*f2f60544SGordon BerglingThe macros listed below take an argument of the specified type and encapsulate 745*f2f60544SGordon Berglingit into a key-value structure that is directly usable as a parameter argument. 746*f2f60544SGordon BerglingMuliple parameters may be provided at once. 747e18eb7b1SScott Long.Bd -literal 748e18eb7b1SScott Long BD_PARENT() void * 749e18eb7b1SScott Long BD_ALIGNMENT() uintmax_t 750e18eb7b1SScott Long BD_BOUNDARY() uintmax_t 751e18eb7b1SScott Long BD_LOWADDR() vm_paddr_t 752e18eb7b1SScott Long BD_HIGHADDR() vm_paddr_t 753e18eb7b1SScott Long BD_MAXSIZE() uintmax_t 754e18eb7b1SScott Long BD_NSEGMENTS() uintmax_t 755e18eb7b1SScott Long BD_MAXSEGSIZE() uintmax_t 756e18eb7b1SScott Long BD_FLAGS() uintmax_t 757e18eb7b1SScott Long BD_LOCKFUNC() void * 758e18eb7b1SScott Long BD_LOCKFUNCARG() void * 759e18eb7b1SScott Long.Ed 76024503465SHiten Pandya.It Fn bus_dmamap_create "dmat" "flags" "*mapp" 76124503465SHiten PandyaAllocates and initializes a DMA map. 76224503465SHiten PandyaArguments are as follows: 76381ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa nsegments" 76424503465SHiten Pandya.It Fa dmat 76524503465SHiten PandyaDMA tag. 76624503465SHiten Pandya.It Fa flags 767afbeac3eSMarius StroblAre as follows: 768afbeac3eSMarius Strobl.Bl -tag -width ".Dv BUS_DMA_COHERENT" 769afbeac3eSMarius Strobl.It Dv BUS_DMA_COHERENT 770afbeac3eSMarius StroblAttempt to map the memory loaded with this map such that cache sync 771afbeac3eSMarius Strobloperations are as cheap as possible. 772afbeac3eSMarius StroblThis flag is typically set on maps when the memory loaded with these will 773afbeac3eSMarius Stroblbe accessed by both a CPU and a DMA engine, frequently such as control data 774afbeac3eSMarius Strobland as opposed to streamable data such as receive and transmit buffers. 775afbeac3eSMarius StroblUse of this flag does not remove the requirement of using 776afbeac3eSMarius Strobl.Fn bus_dmamap_sync , 777afbeac3eSMarius Stroblbut it may reduce the cost of performing these operations. 778afbeac3eSMarius Strobl.El 77924503465SHiten Pandya.It Fa mapp 78024503465SHiten PandyaPointer to a 78124503465SHiten Pandya.Vt bus_dmamap_t 78224503465SHiten Pandyawhere the resulting DMA map will be stored. 78324503465SHiten Pandya.El 78424503465SHiten Pandya.Pp 78524503465SHiten PandyaReturns 78624503465SHiten Pandya.Er ENOMEM 78724503465SHiten Pandyaif sufficient memory is not available for creating the 78824503465SHiten Pandyamap or allocating mapping resources. 78924503465SHiten Pandya.It Fn bus_dmamap_destroy "dmat" "map" 79024503465SHiten PandyaFrees all resources associated with a given DMA map. 79124503465SHiten PandyaArguments are as follows: 79281ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa dmat" 79324503465SHiten Pandya.It Fa dmat 79424503465SHiten PandyaDMA tag used to allocate 79524503465SHiten Pandya.Fa map . 79624503465SHiten Pandya.It Fa map 79724503465SHiten PandyaThe DMA map to destroy. 79824503465SHiten Pandya.El 79924503465SHiten Pandya.Pp 80024503465SHiten PandyaReturns 80124503465SHiten Pandya.Er EBUSY 80224503465SHiten Pandyaif a mapping is still active for 80324503465SHiten Pandya.Fa map . 80495929b66SWarner Losh.It Fn bus_dmamap_load "dmat" "map" "buf" "buflen" "*callback" \ 80595929b66SWarner Losh"callback_arg" "flags" 80624503465SHiten PandyaCreates a mapping in device visible address space of 80724503465SHiten Pandya.Fa buflen 80824503465SHiten Pandyabytes of 80924503465SHiten Pandya.Fa buf , 81024503465SHiten Pandyaassociated with the DMA map 81124503465SHiten Pandya.Fa map . 81271bb1f9bSScott LongThis call will always return immediately and will not block for any reason. 81324503465SHiten PandyaArguments are as follows: 81481ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa buflen" 81524503465SHiten Pandya.It Fa dmat 81624503465SHiten PandyaDMA tag used to allocate 81724503465SHiten Pandya.Fa map . 81824503465SHiten Pandya.It Fa map 81924503465SHiten PandyaA DMA map without a currently active mapping. 82024503465SHiten Pandya.It Fa buf 82124503465SHiten PandyaA kernel virtual address pointer to a contiguous (in KVA) buffer, to be 82224503465SHiten Pandyamapped into device visible address space. 82324503465SHiten Pandya.It Fa buflen 82424503465SHiten PandyaThe size of the buffer. 82524503465SHiten Pandya.It Fa callback Fa callback_arg 82624503465SHiten PandyaThe callback function, and its argument. 82771bb1f9bSScott LongThis function is called once sufficient mapping resources are available for 82871bb1f9bSScott Longthe DMA operation. 82971bb1f9bSScott LongIf resources are temporarily unavailable, this function will be deferred until 83071bb1f9bSScott Longlater, but the load operation will still return immediately to the caller. 83171bb1f9bSScott LongThus, callers should not assume that the callback will be called before the 83271bb1f9bSScott Longload returns, and code should be structured appropriately to handle this. 83371bb1f9bSScott LongSee below for specific flags and error codes that control this behavior. 83424503465SHiten Pandya.It Fa flags 83571bb1f9bSScott LongAre as follows: 83681ae4b8dSRuslan Ermilov.Bl -tag -width ".Dv BUS_DMA_NOWAIT" 83781ae4b8dSRuslan Ermilov.It Dv BUS_DMA_NOWAIT 83871bb1f9bSScott LongThe load should not be deferred in case of insufficient mapping resources, 83971bb1f9bSScott Longand instead should return immediately with an appropriate error. 84008390d3bSMarius Strobl.It Dv BUS_DMA_NOCACHE 84108390d3bSMarius StroblThe generated transactions to and from the virtual page are non-cacheable. 84271bb1f9bSScott Long.El 84324503465SHiten Pandya.El 84424503465SHiten Pandya.Pp 84524503465SHiten PandyaReturn values to the caller are as follows: 84681ae4b8dSRuslan Ermilov.Bl -tag -width ".Er EINPROGRESS" 84724503465SHiten Pandya.It 0 84824503465SHiten PandyaThe callback has been called and completed. 84924503465SHiten PandyaThe status of the mapping has been delivered to the callback. 85024503465SHiten Pandya.It Er EINPROGRESS 85124503465SHiten PandyaThe mapping has been deferred for lack of resources. 85224503465SHiten PandyaThe callback will be called as soon as resources are available. 85324503465SHiten PandyaCallbacks are serviced in FIFO order. 85428b5187fSJohn Baldwin.Pp 85528b5187fSJohn BaldwinNote that subsequent load operations for the same tag that do not require 85628b5187fSJohn Baldwinextra resources will still succeed. 85728b5187fSJohn BaldwinThis may result in out-of-order processing of requests. 85828b5187fSJohn BaldwinIf the caller requires the order of requests to be preserved, 85928b5187fSJohn Baldwinthen the caller is required to stall subsequent requests until a pending 86028b5187fSJohn Baldwinrequest's callback is invoked. 86171bb1f9bSScott Long.It Er ENOMEM 86271bb1f9bSScott LongThe load request has failed due to insufficient resources, and the caller 86371bb1f9bSScott Longspecifically used the 86481ae4b8dSRuslan Ermilov.Dv BUS_DMA_NOWAIT 86571bb1f9bSScott Longflag. 86624503465SHiten Pandya.It Er EINVAL 86724503465SHiten PandyaThe load request was invalid. 86871bb1f9bSScott LongThe callback has been called and has been provided the same error. 86924503465SHiten PandyaThis error value may indicate that 87024503465SHiten Pandya.Fa dmat , 87124503465SHiten Pandya.Fa map , 87224503465SHiten Pandya.Fa buf , 87324503465SHiten Pandyaor 87424503465SHiten Pandya.Fa callback 87524503465SHiten Pandyawere invalid, or 87671bb1f9bSScott Long.Fa buflen 87724503465SHiten Pandyawas larger than the 87824503465SHiten Pandya.Fa maxsize 87924503465SHiten Pandyaargument used to create the dma tag 88024503465SHiten Pandya.Fa dmat . 88124503465SHiten Pandya.El 88224503465SHiten Pandya.Pp 88324503465SHiten PandyaWhen the callback is called, it is presented with an error value 88424503465SHiten Pandyaindicating the disposition of the mapping. 88524503465SHiten PandyaError may be one of the following: 88681ae4b8dSRuslan Ermilov.Bl -tag -width ".Er EINPROGRESS" 88724503465SHiten Pandya.It 0 88824503465SHiten PandyaThe mapping was successful and the 88924503465SHiten Pandya.Fa dm_segs 89024503465SHiten Pandyacallback argument contains an array of 89124503465SHiten Pandya.Vt bus_dma_segment_t 89224503465SHiten Pandyaelements describing the mapping. 89324503465SHiten PandyaThis array is only valid during the scope of the callback function. 89424503465SHiten Pandya.It Er EFBIG 89524503465SHiten PandyaA mapping could not be achieved within the segment constraints provided 89624503465SHiten Pandyain the tag even though the requested allocation size was less than maxsize. 89724503465SHiten Pandya.El 898a073133cSJim Harris.It Fn bus_dmamap_load_bio "dmat" "map" "bio" "callback" "callback_arg" "flags" 899a073133cSJim HarrisThis is a variation of 900a073133cSJim Harris.Fn bus_dmamap_load 901a073133cSJim Harriswhich maps buffers pointed to by 902a073133cSJim Harris.Fa bio 903a073133cSJim Harrisfor DMA transfers. 904a073133cSJim Harris.Fa bio 905a073133cSJim Harrismay point to either a mapped or unmapped buffer. 906a073133cSJim Harris.It Fn bus_dmamap_load_ccb "dmat" "map" "ccb" "callback" "callback_arg" "flags" 907a073133cSJim HarrisThis is a variation of 908a073133cSJim Harris.Fn bus_dmamap_load 909a073133cSJim Harriswhich maps data pointed to by 910a073133cSJim Harris.Fa ccb 911a073133cSJim Harrisfor DMA transfers. 912a073133cSJim HarrisThe data for 913a073133cSJim Harris.Fa ccb 914a073133cSJim Harrismay be any of the following types: 915a073133cSJim Harris.Bl -tag -width ".Er CAM_DATA_SG_PADDR" 916a073133cSJim Harris.It CAM_DATA_VADDR 917a073133cSJim HarrisThe data is a single KVA buffer. 918a073133cSJim Harris.It CAM_DATA_PADDR 919a073133cSJim HarrisThe data is a single bus address range. 920a073133cSJim Harris.It CAM_DATA_SG 921a073133cSJim HarrisThe data is a scatter/gather list of KVA buffers. 922a073133cSJim Harris.It CAM_DATA_SG_PADDR 923a073133cSJim HarrisThe data is a scatter/gather list of bus address ranges. 924a073133cSJim Harris.It CAM_DATA_BIO 925a073133cSJim HarrisThe data is contained in a 926a073133cSJim Harris.Vt struct bio 927a073133cSJim Harrisattached to the CCB. 928a073133cSJim Harris.El 929a073133cSJim Harris.Pp 930a073133cSJim Harris.Fn bus_dmamap_load_ccb 931a073133cSJim Harrissupports the following CCB XPT function codes: 932a073133cSJim Harris.Pp 933a073133cSJim Harris.Bl -item -offset indent -compact 934a073133cSJim Harris.It 935a073133cSJim HarrisXPT_ATA_IO 936a073133cSJim Harris.It 937a073133cSJim HarrisXPT_CONT_TARGET_IO 938a073133cSJim Harris.It 939a073133cSJim HarrisXPT_SCSI_IO 940a073133cSJim Harris.El 941c0341432SJohn Baldwin.It Fn bus_dmamap_load_crp "dmat" "map" "crp" "callback" "callback_arg" "flags" 942c0341432SJohn BaldwinThis is a variation of 943c0341432SJohn Baldwin.Fn bus_dmamap_load 9449c0e3d3aSJohn Baldwinwhich maps the input buffer pointed to by 945c0341432SJohn Baldwin.Fa crp 946c0341432SJohn Baldwinfor DMA transfers. 947c0341432SJohn BaldwinThe 948c0341432SJohn Baldwin.Dv BUS_DMA_NOWAIT 949c0341432SJohn Baldwinflag is implied, thus no callback deferral will happen. 9509c0e3d3aSJohn Baldwin.It Fn bus_dmamap_load_crp_buffer "dmat" "map" "cb" "callback" "callback_arg" \ 9519c0e3d3aSJohn Baldwin"flags" 9529c0e3d3aSJohn BaldwinThis is a variation of 9539c0e3d3aSJohn Baldwin.Fn bus_dmamap_load 9549c0e3d3aSJohn Baldwinwhich maps the crypto data buffer pointed to by 9559c0e3d3aSJohn Baldwin.Fa cb 9569c0e3d3aSJohn Baldwinfor DMA transfers. 9579c0e3d3aSJohn BaldwinThe 9589c0e3d3aSJohn Baldwin.Dv BUS_DMA_NOWAIT 9599c0e3d3aSJohn Baldwinflag is implied, thus no callback deferral will happen. 96024503465SHiten Pandya.It Fn bus_dmamap_load_mbuf "dmat" "map" "mbuf" "callback2" "callback_arg" \ 96124503465SHiten Pandya"flags" 96224503465SHiten PandyaThis is a variation of 96324503465SHiten Pandya.Fn bus_dmamap_load 96424503465SHiten Pandyawhich maps mbuf chains 96524503465SHiten Pandyafor DMA transfers. 96624503465SHiten PandyaA 96724503465SHiten Pandya.Vt bus_size_t 96824503465SHiten Pandyaargument is also passed to the callback routine, which 96924503465SHiten Pandyacontains the mbuf chain's packet header length. 97071bb1f9bSScott LongThe 97181ae4b8dSRuslan Ermilov.Dv BUS_DMA_NOWAIT 97271bb1f9bSScott Longflag is implied, thus no callback deferral will happen. 97324503465SHiten Pandya.Pp 97424503465SHiten PandyaMbuf chains are assumed to be in kernel virtual address space. 97524503465SHiten Pandya.Pp 97671bb1f9bSScott LongBeside the error values listed for 97771bb1f9bSScott Long.Fn bus_dmamap_load , 97824503465SHiten Pandya.Er EINVAL 97971bb1f9bSScott Longwill be returned if the size of the mbuf chain exceeds the maximum limit of the 98024503465SHiten PandyaDMA tag. 981824f4edfSScott Long.It Fn bus_dmamap_load_mbuf_sg "dmat" "map" "mbuf" "segs" "nsegs" "flags" 982824f4edfSScott LongThis is just like 983824f4edfSScott Long.Fn bus_dmamap_load_mbuf 9844f068961SRuslan Ermilovexcept that it returns immediately without calling a callback function. 9854f068961SRuslan ErmilovIt is provided for efficiency. 986824f4edfSScott LongThe scatter/gather segment array 987824f4edfSScott Long.Va segs 988824f4edfSScott Longis provided by the caller and filled in directly by the function. 989824f4edfSScott LongThe 990824f4edfSScott Long.Va nsegs 991824f4edfSScott Longargument is returned with the number of segments filled in. 992824f4edfSScott LongReturns the same errors as 993824f4edfSScott Long.Fn bus_dmamap_load_mbuf . 99424503465SHiten Pandya.It Fn bus_dmamap_load_uio "dmat" "map" "uio" "callback2" "callback_arg" "flags" 99524503465SHiten PandyaThis is a variation of 99624503465SHiten Pandya.Fn bus_dmamap_load 99724503465SHiten Pandyawhich maps buffers pointed to by 99824503465SHiten Pandya.Fa uio 99924503465SHiten Pandyafor DMA transfers. 100024503465SHiten PandyaA 100124503465SHiten Pandya.Vt bus_size_t 100224503465SHiten Pandyaargument is also passed to the callback routine, which contains the size of 100324503465SHiten Pandya.Fa uio , 100424503465SHiten Pandyai.e. 100524503465SHiten Pandya.Fa uio->uio_resid . 100671bb1f9bSScott LongThe 100781ae4b8dSRuslan Ermilov.Dv BUS_DMA_NOWAIT 100871bb1f9bSScott Longflag is implied, thus no callback deferral will happen. 100971bb1f9bSScott LongReturns the same errors as 101071bb1f9bSScott Long.Fn bus_dmamap_load . 101124503465SHiten Pandya.Pp 101224503465SHiten PandyaIf 101324503465SHiten Pandya.Fa uio->uio_segflg 101424503465SHiten Pandyais 101524503465SHiten Pandya.Dv UIO_USERSPACE , 101624503465SHiten Pandyathen it is assumed that the buffer, 101724503465SHiten Pandya.Fa uio 101824503465SHiten Pandyais in 101924503465SHiten Pandya.Fa "uio->uio_td->td_proc" Ns 's 102024503465SHiten Pandyaaddress space. 102124503465SHiten PandyaUser space memory must be in-core and wired prior to attempting a map 102224503465SHiten Pandyaload operation. 1023490ec740SWarner LoshPages may be locked using 1024490ec740SWarner Losh.Xr vslock 9 . 102524503465SHiten Pandya.It Fn bus_dmamap_unload "dmat" "map" 102624503465SHiten PandyaUnloads a DMA map. 102724503465SHiten PandyaArguments are as follows: 102881ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa dmam" 102924503465SHiten Pandya.It Fa dmat 103024503465SHiten PandyaDMA tag used to allocate 103124503465SHiten Pandya.Fa map . 103224503465SHiten Pandya.It Fa map 103324503465SHiten PandyaThe DMA map that is to be unloaded. 103424503465SHiten Pandya.El 103524503465SHiten Pandya.Pp 103624503465SHiten Pandya.Fn bus_dmamap_unload 103724503465SHiten Pandyawill not perform any implicit synchronization of DMA buffers. 103824503465SHiten PandyaThis must be done explicitly by a call to 103924503465SHiten Pandya.Fn bus_dmamap_sync 104024503465SHiten Pandyaprior to unloading the map. 104124503465SHiten Pandya.It Fn bus_dmamap_sync "dmat" "map" "op" 104224503465SHiten PandyaPerforms synchronization of a device visible mapping with the CPU visible 104324503465SHiten Pandyamemory referenced by that mapping. 104424503465SHiten PandyaArguments are as follows: 104581ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa dmat" 104624503465SHiten Pandya.It Fa dmat 104724503465SHiten PandyaDMA tag used to allocate 104824503465SHiten Pandya.Fa map . 104924503465SHiten Pandya.It Fa map 105024503465SHiten PandyaThe DMA mapping to be synchronized. 105124503465SHiten Pandya.It Fa op 105224503465SHiten PandyaType of synchronization operation to perform. 105324503465SHiten PandyaSee the definition of 105424503465SHiten Pandya.Vt bus_dmasync_op_t 105524503465SHiten Pandyafor a description of the acceptable values for 105624503465SHiten Pandya.Fa op . 105724503465SHiten Pandya.El 105824503465SHiten Pandya.Pp 105907bf564cSRuslan ErmilovThe 106024503465SHiten Pandya.Fn bus_dmamap_sync 106107bf564cSRuslan Ermilovfunction 106207bf564cSRuslan Ermilovis the method used to ensure that CPU's and device's direct 106307bf564cSRuslan Ermilovmemory access (DMA) to shared 106424503465SHiten Pandyamemory is coherent. 106524503465SHiten PandyaFor example, the CPU might be used to set up the contents of a buffer 106607bf564cSRuslan Ermilovthat is to be made available to a device. 106724503465SHiten PandyaTo ensure that the data are visible via the device's mapping of that 106807bf564cSRuslan Ermilovmemory, the buffer must be loaded and a DMA sync operation of 10696f56dcd4SJohn-Mark Gurney.Dv BUS_DMASYNC_PREWRITE 10702f46d621SJohn Baldwinmust be performed after the CPU has updated the buffer and before the device 10712f46d621SJohn Baldwinaccess is initiated. 10722f46d621SJohn BaldwinIf the CPU modifies this buffer again later, another 107307bf564cSRuslan Ermilov.Dv BUS_DMASYNC_PREWRITE 10742f46d621SJohn Baldwinsync operation must be performed before an additional device 10752f46d621SJohn Baldwinaccess. 10762f46d621SJohn BaldwinConversely, suppose a device updates memory that is to be read by a CPU. 107707bf564cSRuslan ErmilovIn this case, the buffer must be loaded, and a DMA sync operation of 10786f56dcd4SJohn-Mark Gurney.Dv BUS_DMASYNC_PREREAD 10792f46d621SJohn Baldwinmust be performed before the device access is initiated. 108007bf564cSRuslan ErmilovThe CPU will only be able to see the results of this memory update 108107bf564cSRuslan Ermilovonce the DMA operation has completed and a 10826f56dcd4SJohn-Mark Gurney.Dv BUS_DMASYNC_POSTREAD 108307bf564cSRuslan Ermilovsync operation has been performed. 108424503465SHiten Pandya.Pp 108507bf564cSRuslan ErmilovIf read and write operations are not preceded and followed by the 108624503465SHiten Pandyaappropriate synchronization operations, behavior is undefined. 10876f3d2701SScott Long.It Fn bus_dmamem_alloc "dmat" "**vaddr" "flags" "*mapp" 108824503465SHiten PandyaAllocates memory that is mapped into KVA at the address returned 108924503465SHiten Pandyain 109024503465SHiten Pandya.Fa vaddr 10912f46d621SJohn Baldwinand that is permanently loaded into the newly created 109224503465SHiten Pandya.Vt bus_dmamap_t 109324503465SHiten Pandyareturned via 109424503465SHiten Pandya.Fa mapp . 109524503465SHiten PandyaArguments are as follows: 109681ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa alignment" 109724503465SHiten Pandya.It Fa dmat 109824503465SHiten PandyaDMA tag describing the constraints of the DMA mapping. 109924503465SHiten Pandya.It Fa vaddr 110024503465SHiten PandyaPointer to a pointer that will hold the returned KVA mapping of 110124503465SHiten Pandyathe allocated region. 110224503465SHiten Pandya.It Fa flags 110324503465SHiten PandyaFlags are defined as follows: 110481ae4b8dSRuslan Ermilov.Bl -tag -width ".Dv BUS_DMA_NOWAIT" 110524503465SHiten Pandya.It Dv BUS_DMA_WAITOK 110624503465SHiten PandyaThe routine can safely wait (sleep) for resources. 110724503465SHiten Pandya.It Dv BUS_DMA_NOWAIT 110824503465SHiten PandyaThe routine is not allowed to wait for resources. 110924503465SHiten PandyaIf resources are not available, 111024503465SHiten Pandya.Dv ENOMEM 111124503465SHiten Pandyais returned. 111224503465SHiten Pandya.It Dv BUS_DMA_COHERENT 1113afbeac3eSMarius StroblAttempt to map this memory in a coherent fashion. 1114afbeac3eSMarius StroblSee 1115afbeac3eSMarius Strobl.Fn bus_dmamap_create 1116afbeac3eSMarius Stroblabove for a description of this flag. 1117afbeac3eSMarius StroblFor 1118afbeac3eSMarius Strobl.Fn bus_dmamem_alloc , 1119afbeac3eSMarius Stroblthe 112081ae4b8dSRuslan Ermilov.Dv BUS_DMA_COHERENT 112170254772SWarner Loshflag is currently implemented on arm and arm64. 112282d227dcSMaxime Henrion.It Dv BUS_DMA_ZERO 112382d227dcSMaxime HenrionCauses the allocated memory to be set to all zeros. 1124ba0b1618SRobert Noland.It Dv BUS_DMA_NOCACHE 1125ba0b1618SRobert NolandThe allocated memory will not be cached in the processor caches. 1126ba0b1618SRobert NolandAll memory accesses appear on the bus and are executed 1127ba0b1618SRobert Nolandwithout reordering. 112808390d3bSMarius StroblFor 112908390d3bSMarius Strobl.Fn bus_dmamem_alloc , 113008390d3bSMarius Stroblthe 1131ba0b1618SRobert Noland.Dv BUS_DMA_NOCACHE 113208390d3bSMarius Stroblflag is currently implemented on amd64 and i386 where it results in the 113308390d3bSMarius StroblStrong Uncacheable PAT to be set for the allocated virtual address range. 113424503465SHiten Pandya.El 113524503465SHiten Pandya.It Fa mapp 11366f3d2701SScott LongPointer to a 11376f3d2701SScott Long.Vt bus_dmamap_t 11386f3d2701SScott Longwhere the resulting DMA map will be stored. 113924503465SHiten Pandya.El 114024503465SHiten Pandya.Pp 114124503465SHiten PandyaThe size of memory to be allocated is 114224503465SHiten Pandya.Fa maxsize 11432f46d621SJohn Baldwinas specified in the call to 11442f46d621SJohn Baldwin.Fn bus_dma_tag_create 11452f46d621SJohn Baldwinfor 114624503465SHiten Pandya.Fa dmat . 114724503465SHiten Pandya.Pp 114824503465SHiten PandyaThe current implementation of 114924503465SHiten Pandya.Fn bus_dmamem_alloc 115024503465SHiten Pandyawill allocate all requests as a single segment. 115124503465SHiten Pandya.Pp 11526f3d2701SScott LongAn initial load operation is required to obtain the bus address of the allocated 11536f3d2701SScott Longmemory, and an unload operation is required before freeing the memory, as 11546f3d2701SScott Longdescribed below in 11556f3d2701SScott Long.Fn bus_dmamem_free . 11566f3d2701SScott LongMaps are automatically handled by this function and should not be explicitly 11576f3d2701SScott Longallocated or destroyed. 11586f3d2701SScott Long.Pp 11596f3d2701SScott LongAlthough an explicit load is not required for each access to the memory 116024503465SHiten Pandyareferenced by the returned map, the synchronization requirements 116124503465SHiten Pandyaas described in the 116224503465SHiten Pandya.Fn bus_dmamap_sync 11639280e5faSMike Pritchardsection still apply and should be used to achieve portability on architectures 11646f3d2701SScott Longwithout coherent buses. 116524503465SHiten Pandya.Pp 116624503465SHiten PandyaReturns 116724503465SHiten Pandya.Er ENOMEM 116824503465SHiten Pandyaif sufficient memory is not available for completing 116924503465SHiten Pandyathe operation. 117024503465SHiten Pandya.It Fn bus_dmamem_free "dmat" "*vaddr" "map" 117124503465SHiten PandyaFrees memory previously allocated by 117224503465SHiten Pandya.Fn bus_dmamem_alloc . 117324503465SHiten PandyaAny mappings 117424503465SHiten Pandyawill be invalidated. 117524503465SHiten PandyaArguments are as follows: 117681ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa vaddr" 117724503465SHiten Pandya.It Fa dmat 117824503465SHiten PandyaDMA tag. 117924503465SHiten Pandya.It Fa vaddr 118024503465SHiten PandyaKernel virtual address of the memory. 118124503465SHiten Pandya.It Fa map 118224503465SHiten PandyaDMA map to be invalidated. 118324503465SHiten Pandya.El 118424503465SHiten Pandya.El 118524503465SHiten Pandya.Sh RETURN VALUES 118624503465SHiten PandyaBehavior is undefined if invalid arguments are passed to 118724503465SHiten Pandyaany of the above functions. 118824503465SHiten PandyaIf sufficient resources cannot be allocated for a given 118924503465SHiten Pandyatransaction, 119024503465SHiten Pandya.Er ENOMEM 119124503465SHiten Pandyais returned. 119224503465SHiten PandyaAll 11932f46d621SJohn Baldwinroutines that are not of type 11942f46d621SJohn Baldwin.Vt void 119524503465SHiten Pandyawill return 0 on success or an error 11962f46d621SJohn Baldwincode on failure as discussed above. 119724503465SHiten Pandya.Pp 119824503465SHiten PandyaAll 119924503465SHiten Pandya.Vt void 120024503465SHiten Pandyaroutines will succeed if provided with valid arguments. 12014905491fSScott Long.Sh LOCKING 12024905491fSScott LongTwo locking protocols are used by 12034905491fSScott Long.Nm . 12044905491fSScott LongThe first is a private global lock that is used to synchronize access to the 12054905491fSScott Longbounce buffer pool on the architectures that make use of them. 12064905491fSScott LongThis lock is strictly a leaf lock that is only used internally to 12074905491fSScott Long.Nm 12084905491fSScott Longand is not exposed to clients of the API. 12094905491fSScott Long.Pp 12104905491fSScott LongThe second protocol involves protecting various resources stored in the tag. 12114905491fSScott LongSince almost all 12124905491fSScott Long.Nm 12134905491fSScott Longoperations are done through requests from the driver that created the tag, 12144905491fSScott Longthe most efficient way to protect the tag resources is through the lock that 12154905491fSScott Longthe driver uses. 12164905491fSScott LongIn cases where 12174905491fSScott Long.Nm 12184905491fSScott Longacts on its own without being called by the driver, the lock primitive 12194905491fSScott Longspecified in the tag is acquired and released automatically. 12204905491fSScott LongAn example of this is when the 12214905491fSScott Long.Fn bus_dmamap_load 1222b1cc1020SGiorgos Keramidascallback function is called from a deferred context instead of the driver 12234905491fSScott Longcontext. 12244905491fSScott LongThis means that certain 12254905491fSScott Long.Nm 122681ae4b8dSRuslan Ermilovfunctions must always be called with the same lock held that is specified in the 122781ae4b8dSRuslan Ermilovtag. 122881ae4b8dSRuslan ErmilovThese functions include: 12294905491fSScott Long.Pp 123081ae4b8dSRuslan Ermilov.Bl -item -offset indent -compact 123181ae4b8dSRuslan Ermilov.It 123281ae4b8dSRuslan Ermilov.Fn bus_dmamap_load 123381ae4b8dSRuslan Ermilov.It 1234a073133cSJim Harris.Fn bus_dmamap_load_bio 1235a073133cSJim Harris.It 1236a073133cSJim Harris.Fn bus_dmamap_load_ccb 123781ae4b8dSRuslan Ermilov.It 123881ae4b8dSRuslan Ermilov.Fn bus_dmamap_load_mbuf 123981ae4b8dSRuslan Ermilov.It 124081ae4b8dSRuslan Ermilov.Fn bus_dmamap_load_mbuf_sg 124181ae4b8dSRuslan Ermilov.It 1242a073133cSJim Harris.Fn bus_dmamap_load_uio 1243a073133cSJim Harris.It 124481ae4b8dSRuslan Ermilov.Fn bus_dmamap_unload 124581ae4b8dSRuslan Ermilov.It 124681ae4b8dSRuslan Ermilov.Fn bus_dmamap_sync 12474905491fSScott Long.El 12484905491fSScott Long.Pp 12494905491fSScott LongThere is one exception to this rule. 12504905491fSScott LongIt is common practice to call some of these functions during driver start-up 12514905491fSScott Longwithout any locks held. 12524905491fSScott LongSo long as there is a guarantee of no possible concurrent use of the tag by 12534905491fSScott Longdifferent threads during this operation, it is safe to not hold a lock for 12544905491fSScott Longthese functions. 12554905491fSScott Long.Pp 12564905491fSScott LongCertain 12574905491fSScott Long.Nm 12584905491fSScott Longoperations should not be called with the driver lock held, either because 12594905491fSScott Longthey are already protected by an internal lock, or because they might sleep 126081ae4b8dSRuslan Ermilovdue to memory or resource allocation. 126181ae4b8dSRuslan ErmilovThe following functions must not be 12624905491fSScott Longcalled with any non-sleepable locks held: 12634905491fSScott Long.Pp 126481ae4b8dSRuslan Ermilov.Bl -item -offset indent -compact 126581ae4b8dSRuslan Ermilov.It 126681ae4b8dSRuslan Ermilov.Fn bus_dma_tag_create 126781ae4b8dSRuslan Ermilov.It 126881ae4b8dSRuslan Ermilov.Fn bus_dmamap_create 126981ae4b8dSRuslan Ermilov.It 127081ae4b8dSRuslan Ermilov.Fn bus_dmamem_alloc 12714905491fSScott Long.El 12724905491fSScott Long.Pp 12734905491fSScott LongAll other functions do not have a locking protocol and can thus be 12742f46d621SJohn Baldwincalled with or without any system or driver locks held. 127524503465SHiten Pandya.Sh SEE ALSO 127624503465SHiten Pandya.Xr devclass 9 , 127724503465SHiten Pandya.Xr device 9 , 127824503465SHiten Pandya.Xr driver 9 , 1279490ec740SWarner Losh.Xr rman 9 , 1280490ec740SWarner Losh.Xr vslock 9 128124503465SHiten Pandya.Pp 128224503465SHiten Pandya.Rs 128324503465SHiten Pandya.%A "Jason R. Thorpe" 128424503465SHiten Pandya.%T "A Machine-Independent DMA Framework for NetBSD" 128524503465SHiten Pandya.%J "Proceedings of the Summer 1998 USENIX Technical Conference" 128624503465SHiten Pandya.%Q "USENIX Association" 128724503465SHiten Pandya.%D "June 1998" 128824503465SHiten Pandya.Re 128924503465SHiten Pandya.Sh HISTORY 129024503465SHiten PandyaThe 129124503465SHiten Pandya.Nm 129224503465SHiten Pandyainterface first appeared in 129324503465SHiten Pandya.Nx 1.3 . 129424503465SHiten Pandya.Pp 129524503465SHiten PandyaThe 129624503465SHiten Pandya.Nm 129724503465SHiten PandyaAPI was adopted from 129824503465SHiten Pandya.Nx 129924503465SHiten Pandyafor use in the CAM SCSI subsystem. 130024503465SHiten PandyaThe alterations to the original API were aimed to remove the need for 130124503465SHiten Pandyaa 130224503465SHiten Pandya.Vt bus_dma_segment_t 130324503465SHiten Pandyaarray stored in each 130424503465SHiten Pandya.Vt bus_dmamap_t 130524503465SHiten Pandyawhile allowing callers to queue up on scarce resources. 130624503465SHiten Pandya.Sh AUTHORS 130724503465SHiten PandyaThe 130824503465SHiten Pandya.Nm 1309b9b855ddSMaxime Henrioninterface was designed and implemented by 1310b9b855ddSMaxime Henrion.An Jason R. Thorpe 1311b9b855ddSMaxime Henrionof the Numerical Aerospace Simulation Facility, NASA Ames Research Center. 131224503465SHiten PandyaAdditional input on the 131324503465SHiten Pandya.Nm 1314b9b855ddSMaxime Henriondesign was provided by 1315b9b855ddSMaxime Henrion.An -nosplit 1316b9b855ddSMaxime Henrion.An Chris Demetriou , 1317b9b855ddSMaxime Henrion.An Charles Hannum , 1318b9b855ddSMaxime Henrion.An Ross Harvey , 1319b9b855ddSMaxime Henrion.An Matthew Jacob , 1320b9b855ddSMaxime Henrion.An Jonathan Stone , 1321b9b855ddSMaxime Henrionand 1322b9b855ddSMaxime Henrion.An Matt Thomas . 132324503465SHiten Pandya.Pp 132424503465SHiten PandyaThe 132524503465SHiten Pandya.Nm 132624503465SHiten Pandyainterface in 132724503465SHiten Pandya.Fx 1328b9b855ddSMaxime Henrionbenefits from the contributions of 1329b9b855ddSMaxime Henrion.An Justin T. Gibbs , 1330b9b855ddSMaxime Henrion.An Peter Wemm , 1331b9b855ddSMaxime Henrion.An Doug Rabson , 1332b9b855ddSMaxime Henrion.An Matthew N. Dodd , 1333b9b855ddSMaxime Henrion.An Sam Leffler , 1334b9b855ddSMaxime Henrion.An Maxime Henrion , 1335b9b855ddSMaxime Henrion.An Jake Burkholder , 1336b9b855ddSMaxime Henrion.An Takahashi Yoshihiro , 1337b9b855ddSMaxime Henrion.An Scott Long 1338b9b855ddSMaxime Henrionand many others. 133924503465SHiten Pandya.Pp 1340b9b855ddSMaxime HenrionThis manual page was written by 1341b9b855ddSMaxime Henrion.An Hiten M. Pandya 1342b9b855ddSMaxime Henrionand 1343b9b855ddSMaxime Henrion.An Justin T. Gibbs . 1344