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 , 6624503465SHiten Pandya.Nm bus_dmamap_create , 6724503465SHiten Pandya.Nm bus_dmamap_destroy , 6824503465SHiten Pandya.Nm bus_dmamap_load , 69a073133cSJim Harris.Nm bus_dmamap_load_bio , 70a073133cSJim Harris.Nm bus_dmamap_load_ccb , 71c0341432SJohn Baldwin.Nm bus_dmamap_load_crp , 729c0e3d3aSJohn Baldwin.Nm bus_dmamap_load_crp_buffer , 7324503465SHiten Pandya.Nm bus_dmamap_load_mbuf , 74824f4edfSScott Long.Nm bus_dmamap_load_mbuf_sg , 7524503465SHiten Pandya.Nm bus_dmamap_load_uio , 7624503465SHiten Pandya.Nm bus_dmamap_unload , 7724503465SHiten Pandya.Nm bus_dmamap_sync , 7824503465SHiten Pandya.Nm bus_dmamem_alloc , 79a1bd700aSHiten Pandya.Nm bus_dmamem_free 8024503465SHiten Pandya.Nd Bus and Machine Independent DMA Mapping Interface 8124503465SHiten Pandya.Sh SYNOPSIS 8224503465SHiten Pandya.In machine/bus.h 8324503465SHiten Pandya.Ft int 8424503465SHiten Pandya.Fn bus_dma_tag_create "bus_dma_tag_t parent" "bus_size_t alignment" \ 855c8d3de0SJohn Baldwin"bus_addr_t boundary" "bus_addr_t lowaddr" "bus_addr_t highaddr" \ 8624503465SHiten Pandya"bus_dma_filter_t *filtfunc" "void *filtfuncarg" "bus_size_t maxsize" \ 870e5f9b7dSScott Long"int nsegments" "bus_size_t maxsegsz" "int flags" "bus_dma_lock_t *lockfunc" \ 880e5f9b7dSScott Long"void *lockfuncarg" "bus_dma_tag_t *dmat" 8924503465SHiten Pandya.Ft int 9024503465SHiten Pandya.Fn bus_dma_tag_destroy "bus_dma_tag_t dmat" 91757d4fbaSScott Long.Ft void 92757d4fbaSScott Long.Fo bus_dma_template_init 93757d4fbaSScott Long.Fa "bus_dma_template_t template" 94757d4fbaSScott Long.Fa "bus_dma_tag_t parent" 95757d4fbaSScott Long.Fc 96757d4fbaSScott Long.Ft int 97757d4fbaSScott Long.Fo bus_dma_template_tag 98757d4fbaSScott Long.Fa "bus_dma_template_t template" 99757d4fbaSScott Long.Fa "bus_dma_tag_t *dmat" 100757d4fbaSScott Long.Fc 101757d4fbaSScott Long.Ft void 102757d4fbaSScott Long.Fo bus_dma_template_clone 103757d4fbaSScott Long.Fa "bus_dma_template_t template" 104757d4fbaSScott Long.Fa "bus_dma_tag_t dmat" 105757d4fbaSScott Long.Fc 10624503465SHiten Pandya.Ft int 10724503465SHiten Pandya.Fn bus_dmamap_create "bus_dma_tag_t dmat" "int flags" "bus_dmamap_t *mapp" 10824503465SHiten Pandya.Ft int 10924503465SHiten Pandya.Fn bus_dmamap_destroy "bus_dma_tag_t dmat" "bus_dmamap_t map" 11024503465SHiten Pandya.Ft int 11124503465SHiten Pandya.Fn bus_dmamap_load "bus_dma_tag_t dmat" "bus_dmamap_t map" "void *buf" \ 11224503465SHiten Pandya"bus_size_t buflen" "bus_dmamap_callback_t *callback" "void *callback_arg" \ 11324503465SHiten Pandya"int flags" 11424503465SHiten Pandya.Ft int 115a073133cSJim Harris.Fn bus_dmamap_load_bio "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 116a073133cSJim Harris"struct bio *bio" "bus_dmamap_callback_t *callback" "void *callback_arg" \ 117a073133cSJim Harris"int flags" 118a073133cSJim Harris.Ft int 119a073133cSJim Harris.Fn bus_dmamap_load_ccb "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 120a073133cSJim Harris"union ccb *ccb" "bus_dmamap_callback_t *callback" "void *callback_arg" \ 121a073133cSJim Harris"int flags" 122a073133cSJim Harris.Ft int 123c0341432SJohn Baldwin.Fn bus_dmamap_load_crp "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 124c0341432SJohn Baldwin"struct crypto *crp" "bus_dmamap_callback_t *callback" "void *callback_arg" \ 125c0341432SJohn Baldwin"int flags" 126c0341432SJohn Baldwin.Ft int 1279c0e3d3aSJohn Baldwin.Fn bus_dmamap_load_crp_buffer "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 1289c0e3d3aSJohn Baldwin"struct crypto_buffer *cb" "bus_dmamap_callback_t *callback" \ 1299c0e3d3aSJohn Baldwin"void *callback_arg" "int flags" 1309c0e3d3aSJohn Baldwin.Ft int 13124503465SHiten Pandya.Fn bus_dmamap_load_mbuf "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 13224503465SHiten Pandya"struct mbuf *mbuf" "bus_dmamap_callback2_t *callback" "void *callback_arg" \ 13324503465SHiten Pandya"int flags" 13424503465SHiten Pandya.Ft int 135824f4edfSScott Long.Fn bus_dmamap_load_mbuf_sg "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 136824f4edfSScott Long"struct mbuf *mbuf" "bus_dma_segment_t *segs" "int *nsegs" "int flags" 137824f4edfSScott Long.Ft int 13824503465SHiten Pandya.Fn bus_dmamap_load_uio "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 13924503465SHiten Pandya"struct uio *uio" "bus_dmamap_callback2_t *callback" "void *callback_arg" \ 14024503465SHiten Pandya"int flags" 14124503465SHiten Pandya.Ft void 14224503465SHiten Pandya.Fn bus_dmamap_unload "bus_dma_tag_t dmat" "bus_dmamap_t map" 14324503465SHiten Pandya.Ft void 14424503465SHiten Pandya.Fn bus_dmamap_sync "bus_dma_tag_t dmat" "bus_dmamap_t map" \ 14524503465SHiten Pandya"op" 146cbfef69cSWarner Losh.Ft int 147cbfef69cSWarner Losh.Fn bus_dmamem_alloc "bus_dma_tag_t dmat" "void **vaddr" \ 148cbfef69cSWarner Losh"int flags" "bus_dmamap_t *mapp" 14924503465SHiten Pandya.Ft void 15024503465SHiten Pandya.Fn bus_dmamem_free "bus_dma_tag_t dmat" "void *vaddr" \ 15124503465SHiten Pandya"bus_dmamap_t map" 15224503465SHiten Pandya.Sh DESCRIPTION 15324503465SHiten PandyaDirect Memory Access (DMA) is a method of transferring data 15424503465SHiten Pandyawithout involving the CPU, thus providing higher performance. 15524503465SHiten PandyaA DMA transaction can be achieved between device to memory, 15624503465SHiten Pandyadevice to device, or memory to memory. 15724503465SHiten Pandya.Pp 15824503465SHiten PandyaThe 15924503465SHiten Pandya.Nm 16024503465SHiten PandyaAPI is a bus, device, and machine-independent (MI) interface to 16124503465SHiten PandyaDMA mechanisms. 16224503465SHiten PandyaIt provides the client with flexibility and simplicity by 16324503465SHiten Pandyaabstracting machine dependent issues like setting up 16424503465SHiten PandyaDMA mappings, handling cache issues, bus specific features 16524503465SHiten Pandyaand limitations. 166306095b8SJohn Baldwin.Sh OVERVIEW 167306095b8SJohn BaldwinA tag structure 168306095b8SJohn Baldwin.Vt ( bus_dma_tag_t ) 169306095b8SJohn Baldwinis used to describe the properties of a group of related DMA 170306095b8SJohn Baldwintransactions. 171306095b8SJohn BaldwinOne way to view this is that a tag describes the limitations of a DMA engine. 172306095b8SJohn BaldwinFor example, if a DMA engine in a device is limited to 32-bit addresses, 173306095b8SJohn Baldwinthat limitation is specified by a parameter when creating the tag 174306095b8SJohn Baldwinfor that device. 175306095b8SJohn BaldwinSimilarly, a tag can be marked as requiring buffers whose addresses are 176306095b8SJohn Baldwinaligned to a specific boundary. 177306095b8SJohn Baldwin.Pp 178306095b8SJohn BaldwinSome devices may require multiple tags to describe DMA 179306095b8SJohn Baldwintransactions with differing properties. 180306095b8SJohn BaldwinFor example, a device might require 16-byte alignment of its descriptor ring 181306095b8SJohn Baldwinwhile permitting arbitrary alignment of I/O buffers. 182306095b8SJohn BaldwinIn this case, 183306095b8SJohn Baldwinthe driver must create one tag for the descriptor ring and a separate tag for 184306095b8SJohn BaldwinI/O buffers. 185306095b8SJohn BaldwinIf a device has restrictions that are common to all DMA transactions 186306095b8SJohn Baldwinin addition to restrictions that differ between unrelated groups of 187306095b8SJohn Baldwintransactions, 188306095b8SJohn Baldwinthe driver can first create a 189306095b8SJohn Baldwin.Dq parent 190306095b8SJohn Baldwintag that decribes the common restrictions. 191306095b8SJohn BaldwinThe per-group tags can then inherit these restrictions from this 192306095b8SJohn Baldwin.Dq parent 193306095b8SJohn Baldwintag rather than having to list them explicitly when creating the per-group tags. 194306095b8SJohn Baldwin.Pp 195306095b8SJohn BaldwinA mapping structure 196306095b8SJohn Baldwin.Vt ( bus_dmamap_t ) 197306095b8SJohn Baldwinrepresents a mapping of a memory region for DMA. 198306095b8SJohn BaldwinOn systems with I/O MMUs, 199306095b8SJohn Baldwinthe mapping structure tracks any I/O MMU entries used by a request. 200306095b8SJohn BaldwinFor DMA requests that require bounce pages, 201306095b8SJohn Baldwinthe mapping tracks the bounce pages used. 202306095b8SJohn Baldwin.Pp 203306095b8SJohn BaldwinTo prepare for one or more DMA transactions, 204306095b8SJohn Baldwina mapping must be bound to a memory region by calling one of the 205306095b8SJohn Baldwin.Fn bus_dmamap_load 206306095b8SJohn Baldwinfunctions. 207306095b8SJohn BaldwinThese functions configure the mapping which can include programming entries 208306095b8SJohn Baldwinin an I/O MMU and/or allocating bounce pages. 209306095b8SJohn BaldwinAn output of these functions 210306095b8SJohn Baldwin(either directly or indirectly by invoking a callback routine) 211306095b8SJohn Baldwinis the list of scatter/gather address ranges a consumer can pass to a DMA 212306095b8SJohn Baldwinengine to access the memory region. 213306095b8SJohn BaldwinWhen a mapping is no longer needed, 214306095b8SJohn Baldwinthe mapping must be unloaded via 215306095b8SJohn Baldwin.Fn bus_dmamap_unload . 216306095b8SJohn Baldwin.Pp 217306095b8SJohn BaldwinBefore and after each DMA transaction, 218306095b8SJohn Baldwin.Fn bus_dmamap_sync 219306095b8SJohn Baldwinmust be used to ensure that the correct data is used by the DMA engine and 220306095b8SJohn Baldwinthe CPU. 221306095b8SJohn BaldwinIf a mapping uses bounce pages, 222306095b8SJohn Baldwinthe sync operations copy data between the bounce pages and the memory region 223306095b8SJohn Baldwinbound to the mapping. 224306095b8SJohn BaldwinSync operations also handle architecture-specific details such as CPU cache 225306095b8SJohn Baldwinflushing and CPU memory operation ordering. 226306095b8SJohn Baldwin.Sh STATIC VS DYNAMIC 227306095b8SJohn Baldwin.Nm 228306095b8SJohn Baldwinhandles two types of DMA transactions: static and dynamic. 229306095b8SJohn BaldwinStatic transactions are used with a long-lived memory region that is reused 230306095b8SJohn Baldwinfor many transactions such as a descriptor ring. 231306095b8SJohn BaldwinDynamic transactions are used for transfers to or from transient buffers 232306095b8SJohn Baldwinsuch as I/O buffers holding a network packet or disk block. 233306095b8SJohn BaldwinEach transaction type uses a different subset of the 234306095b8SJohn Baldwin.Nm 235306095b8SJohn BaldwinAPI. 236306095b8SJohn Baldwin.Ss Static Transactions 237306095b8SJohn BaldwinStatic transactions use memory regions allocated by 238306095b8SJohn Baldwin.Nm . 239306095b8SJohn BaldwinEach static memory region is allocated by calling 240306095b8SJohn Baldwin.Fn bus_dmamem_alloc . 241306095b8SJohn BaldwinThis function requires a valid tag describing the properties of the 242306095b8SJohn BaldwinDMA transactions to this region such as alignment or address restrictions. 243306095b8SJohn BaldwinMultiple regions can share a single tag if they share the same restrictions. 244306095b8SJohn Baldwin.Pp 245306095b8SJohn Baldwin.Fn bus_dmamem_alloc 246306095b8SJohn Baldwinallocates a memory region along with a mapping object. 247306095b8SJohn BaldwinThe associated tag, memory region, and mapping object must then be passed to 248306095b8SJohn Baldwin.Fn bus_dmamap_load 249306095b8SJohn Baldwinto bind the mapping to the allocated region and obtain the 250306095b8SJohn Baldwinscatter/gather list. 251306095b8SJohn Baldwin.Pp 252306095b8SJohn BaldwinIt is expected that 253306095b8SJohn Baldwin.Fn bus_dmamem_alloc 254306095b8SJohn Baldwinwill attempt to allocate memory requiring less expensive sync operations 255306095b8SJohn Baldwin(for example, implementations should not allocate regions requiring bounce 256306095b8SJohn Baldwinpages), 257306095b8SJohn Baldwinbut sync operations should still be used. 258306095b8SJohn BaldwinFor example, a driver should use 259306095b8SJohn Baldwin.Fn bus_dmamap_sync 260306095b8SJohn Baldwinin an interrupt handler before reading descriptor ring entries written by the 261306095b8SJohn Baldwindevice prior to the interrupt. 262306095b8SJohn Baldwin.Pp 263306095b8SJohn BaldwinWhen a consumer is finished with a memory region, 264306095b8SJohn Baldwinit should unload the mapping via 265306095b8SJohn Baldwin.Fn bus_dmamap_unload 266306095b8SJohn Baldwinand then release the memory region and mapping object via 267306095b8SJohn Baldwin.Fn bus_dmamem_free . 268306095b8SJohn Baldwin.Ss Dynamic Transactions 269306095b8SJohn BaldwinDynamic transactions map memory regions provided by other parts of the system. 270306095b8SJohn BaldwinA tag must be created via 271306095b8SJohn Baldwin.Fn bus_dma_tag_create 272306095b8SJohn Baldwinto describe the DMA transactions to and from these memory regions, 273306095b8SJohn Baldwinand a pool of mapping objects must be allocated via 274306095b8SJohn Baldwin.Fn bus_dmamap_create 275306095b8SJohn Baldwinto track the mappings of any in-flight transactions. 276306095b8SJohn Baldwin.Pp 277306095b8SJohn BaldwinWhen a consumer wishes to schedule a transaction for a memory region, 278306095b8SJohn Baldwinthe consumer must first obtain an unused mapping object from its pool 279306095b8SJohn Baldwinof mapping objects. 280306095b8SJohn BaldwinThe memory region must be bound to the mapping object via one of the 281306095b8SJohn Baldwin.Fn bus_dmamap_load 282306095b8SJohn Baldwinfunctions. 283306095b8SJohn BaldwinBefore scheduling the transaction, 284306095b8SJohn Baldwinthe consumer should sync the memory region via 285306095b8SJohn Baldwin.Fn bus_dmamap_sync 286306095b8SJohn Baldwinwith one or more of the 287306095b8SJohn Baldwin.Dq PRE 288306095b8SJohn Baldwinflags. 289306095b8SJohn BaldwinAfter the transaction has completed, 290306095b8SJohn Baldwinthe consumer should sync the memory region via 291306095b8SJohn Baldwin.Fn bus_dmamap_sync 292306095b8SJohn Baldwinwith one or more of the 293306095b8SJohn Baldwin.Dq POST 294306095b8SJohn Baldwinflags. 295306095b8SJohn BaldwinThe mapping can then be unloaded via 296306095b8SJohn Baldwin.Fn bus_dmamap_unload , 297306095b8SJohn Baldwinand the mapping object can be returned to the pool of unused mapping objects. 298306095b8SJohn Baldwin.Pp 299306095b8SJohn BaldwinWhen a consumer is no longer scheduling DMA transactions, 300306095b8SJohn Baldwinthe mapping objects should be freed via 301306095b8SJohn Baldwin.Fn bus_dmamap_destroy , 302306095b8SJohn Baldwinand the tag should be freed via 303306095b8SJohn Baldwin.Fn bus_dma_tag_destroy . 30424503465SHiten Pandya.Sh STRUCTURES AND TYPES 30581ae4b8dSRuslan Ermilov.Bl -tag -width indent 30624503465SHiten Pandya.It Vt bus_dma_tag_t 30724503465SHiten PandyaA machine-dependent (MD) opaque type that describes the 308306095b8SJohn Baldwincharacteristics of a group of DMA transactions. 30924503465SHiten PandyaDMA tags are organized into a hierarchy, with each child 31024503465SHiten Pandyatag inheriting the restrictions of its parent. 31124503465SHiten PandyaThis allows all devices along the path of DMA transactions 31224503465SHiten Pandyato contribute to the constraints of those transactions. 313757d4fbaSScott Long.It Vt bus_dma_template_t 314*d58ff30aSScott LongA template is a structure for creating a 315757d4fbaSScott Long.Fa bus_dma_tag_t 316757d4fbaSScott Longfrom a set of defaults. 317757d4fbaSScott LongOnce initialized with 318757d4fbaSScott Long.Fn bus_dma_template_init , 319757d4fbaSScott Longa driver can over-ride individual fields to suit its needs. 320*d58ff30aSScott LongThe following fields start with the indicated default values: 321757d4fbaSScott Long.Bd -literal 322757d4fbaSScott Long alignment 1 323757d4fbaSScott Long boundary 0 324757d4fbaSScott Long lowaddr BUS_SPACE_MAXADDR 325757d4fbaSScott Long highaddr BUS_SPACE_MAXADDR 326757d4fbaSScott Long maxsize BUS_SPACE_MAXSIZE 327757d4fbaSScott Long nsegments BUS_SPACE_UNRESTRICTED 328757d4fbaSScott Long maxsegsize BUS_SPACE_MAXSIZE 329757d4fbaSScott Long flags 0 330757d4fbaSScott Long lockfunc NULL 331757d4fbaSScott Long lockfuncarg NULL 332757d4fbaSScott Long.Ed 333757d4fbaSScott Long.Pp 334757d4fbaSScott LongDescriptions of each field are documented with 335757d4fbaSScott Long.Fn bus_dma_tag_create . 336757d4fbaSScott LongNote that the 337757d4fbaSScott Long.Fa filtfunc 338757d4fbaSScott Longand 339757d4fbaSScott Long.Fa filtfuncarg 340757d4fbaSScott Longattributes of the DMA tag are not supported with templates. 34124503465SHiten Pandya.It Vt bus_dma_filter_t 34224503465SHiten PandyaClient specified address filter having the format: 34381ae4b8dSRuslan Ermilov.Bl -tag -width indent 34424503465SHiten Pandya.It Ft int 34524503465SHiten Pandya.Fn "client_filter" "void *filtarg" "bus_addr_t testaddr" 34624503465SHiten Pandya.El 34781ae4b8dSRuslan Ermilov.Pp 34824503465SHiten PandyaAddress filters can be specified during tag creation to allow 34959892d33SRuslan Ermilovfor devices whose DMA address restrictions cannot be specified 35024503465SHiten Pandyaby a single window. 35124503465SHiten PandyaThe 35224503465SHiten Pandya.Fa filtarg 3532f46d621SJohn Baldwinargument is specified by the client during tag creation to be passed to all 35424503465SHiten Pandyainvocations of the callback. 35524503465SHiten PandyaThe 35624503465SHiten Pandya.Fa testaddr 35724503465SHiten Pandyaargument contains a potential starting address of a DMA mapping. 35824503465SHiten PandyaThe filter function operates on the set of addresses from 35924503465SHiten Pandya.Fa testaddr 36024503465SHiten Pandyato 36124503465SHiten Pandya.Ql trunc_page(testaddr) + PAGE_SIZE - 1 , 36224503465SHiten Pandyainclusive. 3632f46d621SJohn BaldwinThe filter function should return zero if any mapping in this range 3642f46d621SJohn Baldwincan be accommodated by the device and non-zero otherwise. 365*d58ff30aSScott Long.Pp 366*d58ff30aSScott Long.Em Note: The use of filters is deprecated. Proper operation is not guaranteed. 36724503465SHiten Pandya.It Vt bus_dma_segment_t 36824503465SHiten PandyaA machine-dependent type that describes individual 36924503465SHiten PandyaDMA segments. 3702f46d621SJohn BaldwinIt contains the following fields: 37124503465SHiten Pandya.Bd -literal 37224503465SHiten Pandya bus_addr_t ds_addr; 37324503465SHiten Pandya bus_size_t ds_len; 37424503465SHiten Pandya.Ed 37581ae4b8dSRuslan Ermilov.Pp 37624503465SHiten PandyaThe 37724503465SHiten Pandya.Fa ds_addr 37824503465SHiten Pandyafield contains the device visible address of the DMA segment, and 37924503465SHiten Pandya.Fa ds_len 38024503465SHiten Pandyacontains the length of the DMA segment. 38124503465SHiten PandyaAlthough the DMA segments returned by a mapping call will adhere to 38224503465SHiten Pandyaall restrictions necessary for a successful DMA operation, some conversion 3835203edcdSRuslan Ermilov(e.g.\& a conversion from host byte order to the device's byte order) is 38424503465SHiten Pandyaalmost always required when presenting segment information to the device. 38524503465SHiten Pandya.It Vt bus_dmamap_t 38624503465SHiten PandyaA machine-dependent opaque type describing an individual mapping. 38771bb1f9bSScott LongOne map is used for each memory allocation that will be loaded. 38871bb1f9bSScott LongMaps can be reused once they have been unloaded. 38971bb1f9bSScott LongMultiple maps can be associated with one DMA tag. 39081ae4b8dSRuslan ErmilovWhile the value of the map may evaluate to 39181ae4b8dSRuslan Ermilov.Dv NULL 39281ae4b8dSRuslan Ermilovon some platforms under certain conditions, 39381ae4b8dSRuslan Ermilovit should never be assumed that it will be 39481ae4b8dSRuslan Ermilov.Dv NULL 39581ae4b8dSRuslan Ermilovin all cases. 39624503465SHiten Pandya.It Vt bus_dmamap_callback_t 39724503465SHiten PandyaClient specified callback for receiving mapping information resulting from 39824503465SHiten Pandyathe load of a 39924503465SHiten Pandya.Vt bus_dmamap_t 40024503465SHiten Pandyavia 401a073133cSJim Harris.Fn bus_dmamap_load , 402c0341432SJohn Baldwin.Fn bus_dmamap_load_bio , 403c0341432SJohn Baldwin.Fn bus_dmamap_load_ccb , 4049c0e3d3aSJohn Baldwin.Fn bus_dmamap_load_crp , 405a073133cSJim Harrisor 4069c0e3d3aSJohn Baldwin.Fn bus_dmamap_load_crp_buffer . 40724503465SHiten PandyaCallbacks are of the format: 40881ae4b8dSRuslan Ermilov.Bl -tag -width indent 40924503465SHiten Pandya.It Ft void 41024503465SHiten Pandya.Fn "client_callback" "void *callback_arg" "bus_dma_segment_t *segs" \ 41124503465SHiten Pandya"int nseg" "int error" 41224503465SHiten Pandya.El 41381ae4b8dSRuslan Ermilov.Pp 41424503465SHiten PandyaThe 41524503465SHiten Pandya.Fa callback_arg 41624503465SHiten Pandyais the callback argument passed to dmamap load functions. 41724503465SHiten PandyaThe 41824503465SHiten Pandya.Fa segs 41924503465SHiten Pandyaand 42024503465SHiten Pandya.Fa nseg 4212f46d621SJohn Baldwinarguments describe an array of 42224503465SHiten Pandya.Vt bus_dma_segment_t 42324503465SHiten Pandyastructures that represent the mapping. 42424503465SHiten PandyaThis array is only valid within the scope of the callback function. 42524503465SHiten PandyaThe success or failure of the mapping is indicated by the 42624503465SHiten Pandya.Fa error 4272f46d621SJohn Baldwinargument. 42824503465SHiten PandyaMore information on the use of callbacks can be found in the 42924503465SHiten Pandyadescription of the individual dmamap load functions. 43024503465SHiten Pandya.It Vt bus_dmamap_callback2_t 43124503465SHiten PandyaClient specified callback for receiving mapping information resulting from 43224503465SHiten Pandyathe load of a 43324503465SHiten Pandya.Vt bus_dmamap_t 43424503465SHiten Pandyavia 43524503465SHiten Pandya.Fn bus_dmamap_load_uio 43624503465SHiten Pandyaor 43724503465SHiten Pandya.Fn bus_dmamap_load_mbuf . 43881ae4b8dSRuslan Ermilov.Pp 43924503465SHiten PandyaCallback2s are of the format: 44081ae4b8dSRuslan Ermilov.Bl -tag -width indent 44124503465SHiten Pandya.It Ft void 44224503465SHiten Pandya.Fn "client_callback2" "void *callback_arg" "bus_dma_segment_t *segs" \ 44324503465SHiten Pandya"int nseg" "bus_size_t mapsize" "int error" 44424503465SHiten Pandya.El 44581ae4b8dSRuslan Ermilov.Pp 44624503465SHiten PandyaCallback2's behavior is the same as 44724503465SHiten Pandya.Vt bus_dmamap_callback_t 44824503465SHiten Pandyawith the addition that the length of the data mapped is provided via 44924503465SHiten Pandya.Fa mapsize . 45024503465SHiten Pandya.It Vt bus_dmasync_op_t 45124503465SHiten PandyaMemory synchronization operation specifier. 45236a142c4SRuslan ErmilovBus DMA requires explicit synchronization of memory with its device 45324503465SHiten Pandyavisible mapping in order to guarantee memory coherency. 45424503465SHiten PandyaThe 45524503465SHiten Pandya.Vt bus_dmasync_op_t 45624503465SHiten Pandyaallows the type of DMA operation that will be or has been performed 45724503465SHiten Pandyato be communicated to the system so that the correct coherency measures 45824503465SHiten Pandyaare taken. 4591cb112abSScott LongThe operations are represented as bitfield flags that can be combined together, 4601cb112abSScott Longthough it only makes sense to combine PRE flags or POST flags, not both. 4611cb112abSScott LongSee the 462e8c9966dSJohn-Mark Gurney.Fn bus_dmamap_sync 4631cb112abSScott Longdescription below for more details on how to use these operations. 4646f3d2701SScott Long.Pp 4656f3d2701SScott LongAll operations specified below are performed from the host memory point of view, 4666f3d2701SScott Longwhere a read implies data coming from the device to the host memory, and a write 4676f3d2701SScott Longimplies data going from the host memory to the device. 46807bf564cSRuslan ErmilovAlternatively, the operations can be thought of in terms of driver operations, 469a79d1e83SScott Longwhere reading a network packet or storage sector corresponds to a read operation 470a79d1e83SScott Longin 471a79d1e83SScott Long.Nm . 47207bf564cSRuslan Ermilov.Bl -tag -width ".Dv BUS_DMASYNC_POSTWRITE" 47324503465SHiten Pandya.It Dv BUS_DMASYNC_PREREAD 4746f3d2701SScott LongPerform any synchronization required prior to an update of host memory by the 47507bf564cSRuslan Ermilovdevice. 47624503465SHiten Pandya.It Dv BUS_DMASYNC_PREWRITE 4776f3d2701SScott LongPerform any synchronization required after an update of host memory by the CPU 47807bf564cSRuslan Ermilovand prior to device access to host memory. 47924503465SHiten Pandya.It Dv BUS_DMASYNC_POSTREAD 48007bf564cSRuslan ErmilovPerform any synchronization required after an update of host memory by the 48107bf564cSRuslan Ermilovdevice and prior to CPU access to host memory. 48224503465SHiten Pandya.It Dv BUS_DMASYNC_POSTWRITE 4832f46d621SJohn BaldwinPerform any synchronization required after device access to host memory. 48424503465SHiten Pandya.El 4850e5f9b7dSScott Long.It Vt bus_dma_lock_t 486c7d3a65dSHiten PandyaClient specified lock/mutex manipulation method. 487c7d3a65dSHiten PandyaThis will be called from 4880e5f9b7dSScott Longwithin busdma whenever a client lock needs to be manipulated. 4894d855643SWarner LoshIn its current form, the function will be called immediately before 4905bbaa5cfSWojciech A. Koszekthe callback for a DMA load operation that has been deferred with 4914d855643SWarner Losh.Dv BUS_DMA_LOCK 4924d855643SWarner Loshand immediately after with 4934d855643SWarner Losh.Dv BUS_DMA_UNLOCK . 4944d855643SWarner LoshIf the load operation does not need to be deferred, then it 4954d855643SWarner Loshwill not be called since the function loading the map should 4964d855643SWarner Loshbe holding the appropriate locks. 4970e5f9b7dSScott LongThis method is of the format: 49881ae4b8dSRuslan Ermilov.Bl -tag -width indent 4990e5f9b7dSScott Long.It Ft void 5000e5f9b7dSScott Long.Fn "lockfunc" "void *lockfunc_arg" "bus_dma_lock_op_t op" 50124503465SHiten Pandya.El 50281ae4b8dSRuslan Ermilov.Pp 5032f46d621SJohn BaldwinThe 5042f46d621SJohn Baldwin.Fa lockfuncarg 5052f46d621SJohn Baldwinargument is specified by the client during tag creation to be passed to all 5062f46d621SJohn Baldwininvocations of the callback. 5072f46d621SJohn BaldwinThe 5082f46d621SJohn Baldwin.Fa op 5092f46d621SJohn Baldwinargument specifies the lock operation to perform. 5102f46d621SJohn Baldwin.Pp 5110e5f9b7dSScott LongTwo 5120e5f9b7dSScott Long.Vt lockfunc 5130e5f9b7dSScott Longimplementations are provided for convenience. 5140e5f9b7dSScott Long.Fn busdma_lock_mutex 5152f46d621SJohn Baldwinperforms standard mutex operations on the sleep mutex provided via 5160e5f9b7dSScott Long.Fa lockfuncarg . 5170e5f9b7dSScott Long.Fn dflt_lock 518c7d3a65dSHiten Pandyawill generate a system panic if it is called. 519c7d3a65dSHiten PandyaIt is substituted into the tag when 5200e5f9b7dSScott Long.Fa lockfunc 52181ae4b8dSRuslan Ermilovis passed as 52281ae4b8dSRuslan Ermilov.Dv NULL 52381ae4b8dSRuslan Ermilovto 5242f46d621SJohn Baldwin.Fn bus_dma_tag_create 5252f46d621SJohn Baldwinand is useful for tags that should not be used with deferred load operations. 5260e5f9b7dSScott Long.It Vt bus_dma_lock_op_t 5270e5f9b7dSScott LongOperations to be performed by the client-specified 5280e5f9b7dSScott Long.Fn lockfunc . 52981ae4b8dSRuslan Ermilov.Bl -tag -width ".Dv BUS_DMA_UNLOCK" 5300e5f9b7dSScott Long.It Dv BUS_DMA_LOCK 5312988974bSMike PritchardAcquires and/or locks the client locking primitive. 5320e5f9b7dSScott Long.It Dv BUS_DMA_UNLOCK 5330e5f9b7dSScott LongReleases and/or unlocks the client locking primitive. 5340e5f9b7dSScott Long.El 5350e5f9b7dSScott Long.El 53624503465SHiten Pandya.Sh FUNCTIONS 53781ae4b8dSRuslan Ermilov.Bl -tag -width indent 53824503465SHiten Pandya.It Fn bus_dma_tag_create "parent" "alignment" "boundary" "lowaddr" \ 53924503465SHiten Pandya"highaddr" "*filtfunc" "*filtfuncarg" "maxsize" "nsegments" "maxsegsz" \ 5409b5c4b66SScott Long"flags" "lockfunc" "lockfuncarg" "*dmat" 541306095b8SJohn BaldwinAllocates a DMA tag, and initializes it according to 54224503465SHiten Pandyathe arguments provided: 54381ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa filtfuncarg" 54424503465SHiten Pandya.It Fa parent 545306095b8SJohn BaldwinA parent tag from which to inherit restrictions. 546306095b8SJohn BaldwinThe restrictions passed in other arguments can only further tighten the 547306095b8SJohn Baldwinrestrictions inherited from the parent tag. 548306095b8SJohn Baldwin.Pp 549306095b8SJohn BaldwinAll tags created by a device driver must inherit from the tag returned by 550306095b8SJohn Baldwin.Fn bus_get_dma_tag 551306095b8SJohn Baldwinto honor restrictions between the parent bridge, CPU memory, and the 55224503465SHiten Pandyadevice. 55324503465SHiten Pandya.It Fa alignment 55424503465SHiten PandyaAlignment constraint, in bytes, of any mappings created using this tag. 55524503465SHiten PandyaThe alignment must be a power of 2. 55624503465SHiten PandyaHardware that can DMA starting at any address would specify 55724503465SHiten Pandya.Em 1 55824503465SHiten Pandyafor byte alignment. 55924503465SHiten PandyaHardware requiring DMA transfers to start on a multiple of 4K 56024503465SHiten Pandyawould specify 56124503465SHiten Pandya.Em 4096 . 56224503465SHiten Pandya.It Fa boundary 56324503465SHiten PandyaBoundary constraint, in bytes, of the target DMA memory region. 56424503465SHiten PandyaThe boundary indicates the set of addresses, all multiples of the 56524503465SHiten Pandyaboundary argument, that cannot be crossed by a single 56624503465SHiten Pandya.Vt bus_dma_segment_t . 5672d4071acSChristian BruefferThe boundary must be a power of 2 and must be no smaller than the 568ab0b83b5SScott Longmaximum segment size. 56924503465SHiten Pandya.Ql 0 57024503465SHiten Pandyaindicates that there are no boundary restrictions. 57181ae4b8dSRuslan Ermilov.It Fa lowaddr , highaddr 57224503465SHiten PandyaBounds of the window of bus address space that 57324503465SHiten Pandya.Em cannot 57424503465SHiten Pandyabe directly accessed by the device. 5755bbaa5cfSWojciech A. KoszekThe window contains all addresses greater than 5765bbaa5cfSWojciech A. Koszek.Fa lowaddr 5775bbaa5cfSWojciech A. Koszekand less than or equal to 5785bbaa5cfSWojciech A. Koszek.Fa highaddr . 5795bbaa5cfSWojciech A. KoszekFor example, a device incapable of DMA above 4GB, would specify a 5805bbaa5cfSWojciech A. Koszek.Fa highaddr 5815bbaa5cfSWojciech A. Koszekof 58224503465SHiten Pandya.Dv BUS_SPACE_MAXADDR 5835bbaa5cfSWojciech A. Koszekand a 5845bbaa5cfSWojciech A. Koszek.Fa lowaddr 5855bbaa5cfSWojciech A. Koszekof 58624503465SHiten Pandya.Dv BUS_SPACE_MAXADDR_32BIT . 5875bbaa5cfSWojciech A. KoszekSimilarly a device that can only perform DMA to addresses below 5885bbaa5cfSWojciech A. Koszek16MB would specify a 5895bbaa5cfSWojciech A. Koszek.Fa highaddr 5905bbaa5cfSWojciech A. Koszekof 59124503465SHiten Pandya.Dv BUS_SPACE_MAXADDR 5925bbaa5cfSWojciech A. Koszekand a 5935bbaa5cfSWojciech A. Koszek.Fa lowaddr 5945bbaa5cfSWojciech A. Koszekof 59524503465SHiten Pandya.Dv BUS_SPACE_MAXADDR_24BIT . 596306095b8SJohn BaldwinSome implementations require that some region of device visible 59724503465SHiten Pandyaaddress space, overlapping available host memory, be outside the 59824503465SHiten Pandyawindow. 59924503465SHiten PandyaThis area of 60024503465SHiten Pandya.Ql safe memory 60124503465SHiten Pandyais used to bounce requests that would otherwise conflict with 60224503465SHiten Pandyathe exclusion window. 60324503465SHiten Pandya.It Fa filtfunc 60481ae4b8dSRuslan ErmilovOptional filter function (may be 60581ae4b8dSRuslan Ermilov.Dv NULL ) 60681ae4b8dSRuslan Ermilovto be called for any attempt to 60724503465SHiten Pandyamap memory into the window described by 60824503465SHiten Pandya.Fa lowaddr 60924503465SHiten Pandyaand 61024503465SHiten Pandya.Fa highaddr . 61124503465SHiten PandyaA filter function is only required when the single window described 61224503465SHiten Pandyaby 61324503465SHiten Pandya.Fa lowaddr 61424503465SHiten Pandyaand 61524503465SHiten Pandya.Fa highaddr 61624503465SHiten Pandyacannot adequately describe the constraints of the device. 61724503465SHiten PandyaThe filter function will be called for every machine page 61824503465SHiten Pandyathat overlaps the exclusion window. 619*d58ff30aSScott Long.Pp 620*d58ff30aSScott Long.Em Note: The use of filters is deprecated. Proper operation is not guaranteed. 62124503465SHiten Pandya.It Fa filtfuncarg 62224503465SHiten PandyaArgument passed to all calls to the filter function for this tag. 62381ae4b8dSRuslan ErmilovMay be 62481ae4b8dSRuslan Ermilov.Dv NULL . 62524503465SHiten Pandya.It Fa maxsize 62624503465SHiten PandyaMaximum size, in bytes, of the sum of all segment lengths in a given 62724503465SHiten PandyaDMA mapping associated with this tag. 62824503465SHiten Pandya.It Fa nsegments 62924503465SHiten PandyaNumber of discontinuities (scatter/gather segments) allowed 63024503465SHiten Pandyain a DMA mapped region. 631094dc7e3SJohn-Mark Gurney.It Fa maxsegsz 632094dc7e3SJohn-Mark GurneyMaximum size, in bytes, of a segment in any DMA mapped region associated 633094dc7e3SJohn-Mark Gurneywith 634094dc7e3SJohn-Mark Gurney.Fa dmat . 63524503465SHiten Pandya.It Fa flags 63624503465SHiten PandyaAre as follows: 63781ae4b8dSRuslan Ermilov.Bl -tag -width ".Dv BUS_DMA_ALLOCNOW" 63824503465SHiten Pandya.It Dv BUS_DMA_ALLOCNOW 6390f3a0078SScott LongPre-allocate enough resources to handle at least one map load operation on 64071bb1f9bSScott Longthis tag. 64124503465SHiten PandyaIf sufficient resources are not available, 64224503465SHiten Pandya.Er ENOMEM 64324503465SHiten Pandyais returned. 64471bb1f9bSScott LongThis should not be used for tags that only describe buffers that will be 64571bb1f9bSScott Longallocated with 64671bb1f9bSScott Long.Fn bus_dmamem_alloc . 64771bb1f9bSScott LongAlso, due to resource sharing with other tags, this flag does not guarantee 64871bb1f9bSScott Longthat resources will be allocated or reserved exclusively for this tag. 64971bb1f9bSScott LongIt should be treated only as a minor optimization. 650435b87a9SEmmanuel Vadot.It Dv BUS_DMA_COHERENT 651435b87a9SEmmanuel VadotIndicate that the DMA engine and CPU are cache-coherent. 652435b87a9SEmmanuel VadotCached memory may be used to back allocations created by 653435b87a9SEmmanuel Vadot.Fn bus_dmamem_alloc . 654435b87a9SEmmanuel VadotFor 655435b87a9SEmmanuel Vadot.Fn bus_dma_tag_create , 656435b87a9SEmmanuel Vadotthe 657435b87a9SEmmanuel Vadot.Dv BUS_DMA_COHERENT 658435b87a9SEmmanuel Vadotflag is currently implemented on arm64. 65924503465SHiten Pandya.El 6609b5c4b66SScott Long.It Fa lockfunc 66181ae4b8dSRuslan ErmilovOptional lock manipulation function (may be 66281ae4b8dSRuslan Ermilov.Dv NULL ) 66381ae4b8dSRuslan Ermilovto be called when busdma 664c7d3a65dSHiten Pandyaneeds to manipulate a lock on behalf of the client. 66581ae4b8dSRuslan ErmilovIf 66681ae4b8dSRuslan Ermilov.Dv NULL 66781ae4b8dSRuslan Ermilovis specified, 6689b5c4b66SScott Long.Fn dflt_lock 6699b5c4b66SScott Longis used. 6709b5c4b66SScott Long.It Fa lockfuncarg 6719b5c4b66SScott LongOptional argument to be passed to the function specified by 6729b5c4b66SScott Long.Fa lockfunc . 67324503465SHiten Pandya.It Fa dmat 67424503465SHiten PandyaPointer to a bus_dma_tag_t where the resulting DMA tag will 67524503465SHiten Pandyabe stored. 67624503465SHiten Pandya.El 67724503465SHiten Pandya.Pp 67824503465SHiten PandyaReturns 67924503465SHiten Pandya.Er ENOMEM 68024503465SHiten Pandyaif sufficient memory is not available for tag creation 68124503465SHiten Pandyaor allocating mapping resources. 68224503465SHiten Pandya.It Fn bus_dma_tag_destroy "dmat" 68324503465SHiten PandyaDeallocate the DMA tag 68424503465SHiten Pandya.Fa dmat 68524503465SHiten Pandyathat was created by 68624503465SHiten Pandya.Fn bus_dma_tag_create . 68724503465SHiten Pandya.Pp 68824503465SHiten PandyaReturns 68924503465SHiten Pandya.Er EBUSY 69024503465SHiten Pandyaif any DMA maps remain associated with 69124503465SHiten Pandya.Fa dmat 69224503465SHiten Pandyaor 69324503465SHiten Pandya.Ql 0 69424503465SHiten Pandyaon success. 695757d4fbaSScott Long.It Fn bus_dma_template_init "*template" "parent" 696757d4fbaSScott LongInitializes a 697757d4fbaSScott Long.Fa bus_dma_template_t 698*d58ff30aSScott Longstructure. If the 699757d4fbaSScott Long.Fa parent 700*d58ff30aSScott Longargument is non-NULL, values from this tag will be copied into the template, 701*d58ff30aSScott Longreplacing any defaults. 702757d4fbaSScott Long.It Fn bus_dma_template_tag "*template" "*dmat" 703757d4fbaSScott LongUnpacks a template into a tag, and returns the tag via the 704757d4fbaSScott Long.Fa dmat . 705757d4fbaSScott LongAll return values are identical to 706757d4fbaSScott Long.Fn bus_dma_tag_create . 707*d58ff30aSScott LongThe template is not modified by this function, and can be reused and/or 708*d58ff30aSScott Longfreed upon return. 709757d4fbaSScott Long.It Fn bus_dma_template_clone "*template" "dmat" 710*d58ff30aSScott LongCopies the fields from an existing tag to a template. 711*d58ff30aSScott LongThe template does not need to be initialized first. All of its fields will 712*d58ff30aSScott Longbe overwritten by the values contained in the tag. When paired with 713*d58ff30aSScott Long.Fn bus_dma_template_tag , 714*d58ff30aSScott Longthis function is useful for creating copies of tags. 71524503465SHiten Pandya.It Fn bus_dmamap_create "dmat" "flags" "*mapp" 71624503465SHiten PandyaAllocates and initializes a DMA map. 71724503465SHiten PandyaArguments are as follows: 71881ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa nsegments" 71924503465SHiten Pandya.It Fa dmat 72024503465SHiten PandyaDMA tag. 72124503465SHiten Pandya.It Fa flags 722afbeac3eSMarius StroblAre as follows: 723afbeac3eSMarius Strobl.Bl -tag -width ".Dv BUS_DMA_COHERENT" 724afbeac3eSMarius Strobl.It Dv BUS_DMA_COHERENT 725afbeac3eSMarius StroblAttempt to map the memory loaded with this map such that cache sync 726afbeac3eSMarius Strobloperations are as cheap as possible. 727afbeac3eSMarius StroblThis flag is typically set on maps when the memory loaded with these will 728afbeac3eSMarius Stroblbe accessed by both a CPU and a DMA engine, frequently such as control data 729afbeac3eSMarius Strobland as opposed to streamable data such as receive and transmit buffers. 730afbeac3eSMarius StroblUse of this flag does not remove the requirement of using 731afbeac3eSMarius Strobl.Fn bus_dmamap_sync , 732afbeac3eSMarius Stroblbut it may reduce the cost of performing these operations. 733afbeac3eSMarius Strobl.El 73424503465SHiten Pandya.It Fa mapp 73524503465SHiten PandyaPointer to a 73624503465SHiten Pandya.Vt bus_dmamap_t 73724503465SHiten Pandyawhere the resulting DMA map will be stored. 73824503465SHiten Pandya.El 73924503465SHiten Pandya.Pp 74024503465SHiten PandyaReturns 74124503465SHiten Pandya.Er ENOMEM 74224503465SHiten Pandyaif sufficient memory is not available for creating the 74324503465SHiten Pandyamap or allocating mapping resources. 74424503465SHiten Pandya.It Fn bus_dmamap_destroy "dmat" "map" 74524503465SHiten PandyaFrees all resources associated with a given DMA map. 74624503465SHiten PandyaArguments are as follows: 74781ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa dmat" 74824503465SHiten Pandya.It Fa dmat 74924503465SHiten PandyaDMA tag used to allocate 75024503465SHiten Pandya.Fa map . 75124503465SHiten Pandya.It Fa map 75224503465SHiten PandyaThe DMA map to destroy. 75324503465SHiten Pandya.El 75424503465SHiten Pandya.Pp 75524503465SHiten PandyaReturns 75624503465SHiten Pandya.Er EBUSY 75724503465SHiten Pandyaif a mapping is still active for 75824503465SHiten Pandya.Fa map . 75995929b66SWarner Losh.It Fn bus_dmamap_load "dmat" "map" "buf" "buflen" "*callback" \ 76095929b66SWarner Losh"callback_arg" "flags" 76124503465SHiten PandyaCreates a mapping in device visible address space of 76224503465SHiten Pandya.Fa buflen 76324503465SHiten Pandyabytes of 76424503465SHiten Pandya.Fa buf , 76524503465SHiten Pandyaassociated with the DMA map 76624503465SHiten Pandya.Fa map . 76771bb1f9bSScott LongThis call will always return immediately and will not block for any reason. 76824503465SHiten PandyaArguments are as follows: 76981ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa buflen" 77024503465SHiten Pandya.It Fa dmat 77124503465SHiten PandyaDMA tag used to allocate 77224503465SHiten Pandya.Fa map . 77324503465SHiten Pandya.It Fa map 77424503465SHiten PandyaA DMA map without a currently active mapping. 77524503465SHiten Pandya.It Fa buf 77624503465SHiten PandyaA kernel virtual address pointer to a contiguous (in KVA) buffer, to be 77724503465SHiten Pandyamapped into device visible address space. 77824503465SHiten Pandya.It Fa buflen 77924503465SHiten PandyaThe size of the buffer. 78024503465SHiten Pandya.It Fa callback Fa callback_arg 78124503465SHiten PandyaThe callback function, and its argument. 78271bb1f9bSScott LongThis function is called once sufficient mapping resources are available for 78371bb1f9bSScott Longthe DMA operation. 78471bb1f9bSScott LongIf resources are temporarily unavailable, this function will be deferred until 78571bb1f9bSScott Longlater, but the load operation will still return immediately to the caller. 78671bb1f9bSScott LongThus, callers should not assume that the callback will be called before the 78771bb1f9bSScott Longload returns, and code should be structured appropriately to handle this. 78871bb1f9bSScott LongSee below for specific flags and error codes that control this behavior. 78924503465SHiten Pandya.It Fa flags 79071bb1f9bSScott LongAre as follows: 79181ae4b8dSRuslan Ermilov.Bl -tag -width ".Dv BUS_DMA_NOWAIT" 79281ae4b8dSRuslan Ermilov.It Dv BUS_DMA_NOWAIT 79371bb1f9bSScott LongThe load should not be deferred in case of insufficient mapping resources, 79471bb1f9bSScott Longand instead should return immediately with an appropriate error. 79508390d3bSMarius Strobl.It Dv BUS_DMA_NOCACHE 79608390d3bSMarius StroblThe generated transactions to and from the virtual page are non-cacheable. 79771bb1f9bSScott Long.El 79824503465SHiten Pandya.El 79924503465SHiten Pandya.Pp 80024503465SHiten PandyaReturn values to the caller are as follows: 80181ae4b8dSRuslan Ermilov.Bl -tag -width ".Er EINPROGRESS" 80224503465SHiten Pandya.It 0 80324503465SHiten PandyaThe callback has been called and completed. 80424503465SHiten PandyaThe status of the mapping has been delivered to the callback. 80524503465SHiten Pandya.It Er EINPROGRESS 80624503465SHiten PandyaThe mapping has been deferred for lack of resources. 80724503465SHiten PandyaThe callback will be called as soon as resources are available. 80824503465SHiten PandyaCallbacks are serviced in FIFO order. 80928b5187fSJohn Baldwin.Pp 81028b5187fSJohn BaldwinNote that subsequent load operations for the same tag that do not require 81128b5187fSJohn Baldwinextra resources will still succeed. 81228b5187fSJohn BaldwinThis may result in out-of-order processing of requests. 81328b5187fSJohn BaldwinIf the caller requires the order of requests to be preserved, 81428b5187fSJohn Baldwinthen the caller is required to stall subsequent requests until a pending 81528b5187fSJohn Baldwinrequest's callback is invoked. 81671bb1f9bSScott Long.It Er ENOMEM 81771bb1f9bSScott LongThe load request has failed due to insufficient resources, and the caller 81871bb1f9bSScott Longspecifically used the 81981ae4b8dSRuslan Ermilov.Dv BUS_DMA_NOWAIT 82071bb1f9bSScott Longflag. 82124503465SHiten Pandya.It Er EINVAL 82224503465SHiten PandyaThe load request was invalid. 82371bb1f9bSScott LongThe callback has been called and has been provided the same error. 82424503465SHiten PandyaThis error value may indicate that 82524503465SHiten Pandya.Fa dmat , 82624503465SHiten Pandya.Fa map , 82724503465SHiten Pandya.Fa buf , 82824503465SHiten Pandyaor 82924503465SHiten Pandya.Fa callback 83024503465SHiten Pandyawere invalid, or 83171bb1f9bSScott Long.Fa buflen 83224503465SHiten Pandyawas larger than the 83324503465SHiten Pandya.Fa maxsize 83424503465SHiten Pandyaargument used to create the dma tag 83524503465SHiten Pandya.Fa dmat . 83624503465SHiten Pandya.El 83724503465SHiten Pandya.Pp 83824503465SHiten PandyaWhen the callback is called, it is presented with an error value 83924503465SHiten Pandyaindicating the disposition of the mapping. 84024503465SHiten PandyaError may be one of the following: 84181ae4b8dSRuslan Ermilov.Bl -tag -width ".Er EINPROGRESS" 84224503465SHiten Pandya.It 0 84324503465SHiten PandyaThe mapping was successful and the 84424503465SHiten Pandya.Fa dm_segs 84524503465SHiten Pandyacallback argument contains an array of 84624503465SHiten Pandya.Vt bus_dma_segment_t 84724503465SHiten Pandyaelements describing the mapping. 84824503465SHiten PandyaThis array is only valid during the scope of the callback function. 84924503465SHiten Pandya.It Er EFBIG 85024503465SHiten PandyaA mapping could not be achieved within the segment constraints provided 85124503465SHiten Pandyain the tag even though the requested allocation size was less than maxsize. 85224503465SHiten Pandya.El 853a073133cSJim Harris.It Fn bus_dmamap_load_bio "dmat" "map" "bio" "callback" "callback_arg" "flags" 854a073133cSJim HarrisThis is a variation of 855a073133cSJim Harris.Fn bus_dmamap_load 856a073133cSJim Harriswhich maps buffers pointed to by 857a073133cSJim Harris.Fa bio 858a073133cSJim Harrisfor DMA transfers. 859a073133cSJim Harris.Fa bio 860a073133cSJim Harrismay point to either a mapped or unmapped buffer. 861a073133cSJim Harris.It Fn bus_dmamap_load_ccb "dmat" "map" "ccb" "callback" "callback_arg" "flags" 862a073133cSJim HarrisThis is a variation of 863a073133cSJim Harris.Fn bus_dmamap_load 864a073133cSJim Harriswhich maps data pointed to by 865a073133cSJim Harris.Fa ccb 866a073133cSJim Harrisfor DMA transfers. 867a073133cSJim HarrisThe data for 868a073133cSJim Harris.Fa ccb 869a073133cSJim Harrismay be any of the following types: 870a073133cSJim Harris.Bl -tag -width ".Er CAM_DATA_SG_PADDR" 871a073133cSJim Harris.It CAM_DATA_VADDR 872a073133cSJim HarrisThe data is a single KVA buffer. 873a073133cSJim Harris.It CAM_DATA_PADDR 874a073133cSJim HarrisThe data is a single bus address range. 875a073133cSJim Harris.It CAM_DATA_SG 876a073133cSJim HarrisThe data is a scatter/gather list of KVA buffers. 877a073133cSJim Harris.It CAM_DATA_SG_PADDR 878a073133cSJim HarrisThe data is a scatter/gather list of bus address ranges. 879a073133cSJim Harris.It CAM_DATA_BIO 880a073133cSJim HarrisThe data is contained in a 881a073133cSJim Harris.Vt struct bio 882a073133cSJim Harrisattached to the CCB. 883a073133cSJim Harris.El 884a073133cSJim Harris.Pp 885a073133cSJim Harris.Fn bus_dmamap_load_ccb 886a073133cSJim Harrissupports the following CCB XPT function codes: 887a073133cSJim Harris.Pp 888a073133cSJim Harris.Bl -item -offset indent -compact 889a073133cSJim Harris.It 890a073133cSJim HarrisXPT_ATA_IO 891a073133cSJim Harris.It 892a073133cSJim HarrisXPT_CONT_TARGET_IO 893a073133cSJim Harris.It 894a073133cSJim HarrisXPT_SCSI_IO 895a073133cSJim Harris.El 896c0341432SJohn Baldwin.It Fn bus_dmamap_load_crp "dmat" "map" "crp" "callback" "callback_arg" "flags" 897c0341432SJohn BaldwinThis is a variation of 898c0341432SJohn Baldwin.Fn bus_dmamap_load 8999c0e3d3aSJohn Baldwinwhich maps the input buffer pointed to by 900c0341432SJohn Baldwin.Fa crp 901c0341432SJohn Baldwinfor DMA transfers. 902c0341432SJohn BaldwinThe 903c0341432SJohn Baldwin.Dv BUS_DMA_NOWAIT 904c0341432SJohn Baldwinflag is implied, thus no callback deferral will happen. 9059c0e3d3aSJohn Baldwin.It Fn bus_dmamap_load_crp_buffer "dmat" "map" "cb" "callback" "callback_arg" \ 9069c0e3d3aSJohn Baldwin"flags" 9079c0e3d3aSJohn BaldwinThis is a variation of 9089c0e3d3aSJohn Baldwin.Fn bus_dmamap_load 9099c0e3d3aSJohn Baldwinwhich maps the crypto data buffer pointed to by 9109c0e3d3aSJohn Baldwin.Fa cb 9119c0e3d3aSJohn Baldwinfor DMA transfers. 9129c0e3d3aSJohn BaldwinThe 9139c0e3d3aSJohn Baldwin.Dv BUS_DMA_NOWAIT 9149c0e3d3aSJohn Baldwinflag is implied, thus no callback deferral will happen. 91524503465SHiten Pandya.It Fn bus_dmamap_load_mbuf "dmat" "map" "mbuf" "callback2" "callback_arg" \ 91624503465SHiten Pandya"flags" 91724503465SHiten PandyaThis is a variation of 91824503465SHiten Pandya.Fn bus_dmamap_load 91924503465SHiten Pandyawhich maps mbuf chains 92024503465SHiten Pandyafor DMA transfers. 92124503465SHiten PandyaA 92224503465SHiten Pandya.Vt bus_size_t 92324503465SHiten Pandyaargument is also passed to the callback routine, which 92424503465SHiten Pandyacontains the mbuf chain's packet header length. 92571bb1f9bSScott LongThe 92681ae4b8dSRuslan Ermilov.Dv BUS_DMA_NOWAIT 92771bb1f9bSScott Longflag is implied, thus no callback deferral will happen. 92824503465SHiten Pandya.Pp 92924503465SHiten PandyaMbuf chains are assumed to be in kernel virtual address space. 93024503465SHiten Pandya.Pp 93171bb1f9bSScott LongBeside the error values listed for 93271bb1f9bSScott Long.Fn bus_dmamap_load , 93324503465SHiten Pandya.Er EINVAL 93471bb1f9bSScott Longwill be returned if the size of the mbuf chain exceeds the maximum limit of the 93524503465SHiten PandyaDMA tag. 936824f4edfSScott Long.It Fn bus_dmamap_load_mbuf_sg "dmat" "map" "mbuf" "segs" "nsegs" "flags" 937824f4edfSScott LongThis is just like 938824f4edfSScott Long.Fn bus_dmamap_load_mbuf 9394f068961SRuslan Ermilovexcept that it returns immediately without calling a callback function. 9404f068961SRuslan ErmilovIt is provided for efficiency. 941824f4edfSScott LongThe scatter/gather segment array 942824f4edfSScott Long.Va segs 943824f4edfSScott Longis provided by the caller and filled in directly by the function. 944824f4edfSScott LongThe 945824f4edfSScott Long.Va nsegs 946824f4edfSScott Longargument is returned with the number of segments filled in. 947824f4edfSScott LongReturns the same errors as 948824f4edfSScott Long.Fn bus_dmamap_load_mbuf . 94924503465SHiten Pandya.It Fn bus_dmamap_load_uio "dmat" "map" "uio" "callback2" "callback_arg" "flags" 95024503465SHiten PandyaThis is a variation of 95124503465SHiten Pandya.Fn bus_dmamap_load 95224503465SHiten Pandyawhich maps buffers pointed to by 95324503465SHiten Pandya.Fa uio 95424503465SHiten Pandyafor DMA transfers. 95524503465SHiten PandyaA 95624503465SHiten Pandya.Vt bus_size_t 95724503465SHiten Pandyaargument is also passed to the callback routine, which contains the size of 95824503465SHiten Pandya.Fa uio , 95924503465SHiten Pandyai.e. 96024503465SHiten Pandya.Fa uio->uio_resid . 96171bb1f9bSScott LongThe 96281ae4b8dSRuslan Ermilov.Dv BUS_DMA_NOWAIT 96371bb1f9bSScott Longflag is implied, thus no callback deferral will happen. 96471bb1f9bSScott LongReturns the same errors as 96571bb1f9bSScott Long.Fn bus_dmamap_load . 96624503465SHiten Pandya.Pp 96724503465SHiten PandyaIf 96824503465SHiten Pandya.Fa uio->uio_segflg 96924503465SHiten Pandyais 97024503465SHiten Pandya.Dv UIO_USERSPACE , 97124503465SHiten Pandyathen it is assumed that the buffer, 97224503465SHiten Pandya.Fa uio 97324503465SHiten Pandyais in 97424503465SHiten Pandya.Fa "uio->uio_td->td_proc" Ns 's 97524503465SHiten Pandyaaddress space. 97624503465SHiten PandyaUser space memory must be in-core and wired prior to attempting a map 97724503465SHiten Pandyaload operation. 978490ec740SWarner LoshPages may be locked using 979490ec740SWarner Losh.Xr vslock 9 . 98024503465SHiten Pandya.It Fn bus_dmamap_unload "dmat" "map" 98124503465SHiten PandyaUnloads a DMA map. 98224503465SHiten PandyaArguments are as follows: 98381ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa dmam" 98424503465SHiten Pandya.It Fa dmat 98524503465SHiten PandyaDMA tag used to allocate 98624503465SHiten Pandya.Fa map . 98724503465SHiten Pandya.It Fa map 98824503465SHiten PandyaThe DMA map that is to be unloaded. 98924503465SHiten Pandya.El 99024503465SHiten Pandya.Pp 99124503465SHiten Pandya.Fn bus_dmamap_unload 99224503465SHiten Pandyawill not perform any implicit synchronization of DMA buffers. 99324503465SHiten PandyaThis must be done explicitly by a call to 99424503465SHiten Pandya.Fn bus_dmamap_sync 99524503465SHiten Pandyaprior to unloading the map. 99624503465SHiten Pandya.It Fn bus_dmamap_sync "dmat" "map" "op" 99724503465SHiten PandyaPerforms synchronization of a device visible mapping with the CPU visible 99824503465SHiten Pandyamemory referenced by that mapping. 99924503465SHiten PandyaArguments are as follows: 100081ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa dmat" 100124503465SHiten Pandya.It Fa dmat 100224503465SHiten PandyaDMA tag used to allocate 100324503465SHiten Pandya.Fa map . 100424503465SHiten Pandya.It Fa map 100524503465SHiten PandyaThe DMA mapping to be synchronized. 100624503465SHiten Pandya.It Fa op 100724503465SHiten PandyaType of synchronization operation to perform. 100824503465SHiten PandyaSee the definition of 100924503465SHiten Pandya.Vt bus_dmasync_op_t 101024503465SHiten Pandyafor a description of the acceptable values for 101124503465SHiten Pandya.Fa op . 101224503465SHiten Pandya.El 101324503465SHiten Pandya.Pp 101407bf564cSRuslan ErmilovThe 101524503465SHiten Pandya.Fn bus_dmamap_sync 101607bf564cSRuslan Ermilovfunction 101707bf564cSRuslan Ermilovis the method used to ensure that CPU's and device's direct 101807bf564cSRuslan Ermilovmemory access (DMA) to shared 101924503465SHiten Pandyamemory is coherent. 102024503465SHiten PandyaFor example, the CPU might be used to set up the contents of a buffer 102107bf564cSRuslan Ermilovthat is to be made available to a device. 102224503465SHiten PandyaTo ensure that the data are visible via the device's mapping of that 102307bf564cSRuslan Ermilovmemory, the buffer must be loaded and a DMA sync operation of 10246f56dcd4SJohn-Mark Gurney.Dv BUS_DMASYNC_PREWRITE 10252f46d621SJohn Baldwinmust be performed after the CPU has updated the buffer and before the device 10262f46d621SJohn Baldwinaccess is initiated. 10272f46d621SJohn BaldwinIf the CPU modifies this buffer again later, another 102807bf564cSRuslan Ermilov.Dv BUS_DMASYNC_PREWRITE 10292f46d621SJohn Baldwinsync operation must be performed before an additional device 10302f46d621SJohn Baldwinaccess. 10312f46d621SJohn BaldwinConversely, suppose a device updates memory that is to be read by a CPU. 103207bf564cSRuslan ErmilovIn this case, the buffer must be loaded, and a DMA sync operation of 10336f56dcd4SJohn-Mark Gurney.Dv BUS_DMASYNC_PREREAD 10342f46d621SJohn Baldwinmust be performed before the device access is initiated. 103507bf564cSRuslan ErmilovThe CPU will only be able to see the results of this memory update 103607bf564cSRuslan Ermilovonce the DMA operation has completed and a 10376f56dcd4SJohn-Mark Gurney.Dv BUS_DMASYNC_POSTREAD 103807bf564cSRuslan Ermilovsync operation has been performed. 103924503465SHiten Pandya.Pp 104007bf564cSRuslan ErmilovIf read and write operations are not preceded and followed by the 104124503465SHiten Pandyaappropriate synchronization operations, behavior is undefined. 10426f3d2701SScott Long.It Fn bus_dmamem_alloc "dmat" "**vaddr" "flags" "*mapp" 104324503465SHiten PandyaAllocates memory that is mapped into KVA at the address returned 104424503465SHiten Pandyain 104524503465SHiten Pandya.Fa vaddr 10462f46d621SJohn Baldwinand that is permanently loaded into the newly created 104724503465SHiten Pandya.Vt bus_dmamap_t 104824503465SHiten Pandyareturned via 104924503465SHiten Pandya.Fa mapp . 105024503465SHiten PandyaArguments are as follows: 105181ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa alignment" 105224503465SHiten Pandya.It Fa dmat 105324503465SHiten PandyaDMA tag describing the constraints of the DMA mapping. 105424503465SHiten Pandya.It Fa vaddr 105524503465SHiten PandyaPointer to a pointer that will hold the returned KVA mapping of 105624503465SHiten Pandyathe allocated region. 105724503465SHiten Pandya.It Fa flags 105824503465SHiten PandyaFlags are defined as follows: 105981ae4b8dSRuslan Ermilov.Bl -tag -width ".Dv BUS_DMA_NOWAIT" 106024503465SHiten Pandya.It Dv BUS_DMA_WAITOK 106124503465SHiten PandyaThe routine can safely wait (sleep) for resources. 106224503465SHiten Pandya.It Dv BUS_DMA_NOWAIT 106324503465SHiten PandyaThe routine is not allowed to wait for resources. 106424503465SHiten PandyaIf resources are not available, 106524503465SHiten Pandya.Dv ENOMEM 106624503465SHiten Pandyais returned. 106724503465SHiten Pandya.It Dv BUS_DMA_COHERENT 1068afbeac3eSMarius StroblAttempt to map this memory in a coherent fashion. 1069afbeac3eSMarius StroblSee 1070afbeac3eSMarius Strobl.Fn bus_dmamap_create 1071afbeac3eSMarius Stroblabove for a description of this flag. 1072afbeac3eSMarius StroblFor 1073afbeac3eSMarius Strobl.Fn bus_dmamem_alloc , 1074afbeac3eSMarius Stroblthe 107581ae4b8dSRuslan Ermilov.Dv BUS_DMA_COHERENT 107670254772SWarner Loshflag is currently implemented on arm and arm64. 107782d227dcSMaxime Henrion.It Dv BUS_DMA_ZERO 107882d227dcSMaxime HenrionCauses the allocated memory to be set to all zeros. 1079ba0b1618SRobert Noland.It Dv BUS_DMA_NOCACHE 1080ba0b1618SRobert NolandThe allocated memory will not be cached in the processor caches. 1081ba0b1618SRobert NolandAll memory accesses appear on the bus and are executed 1082ba0b1618SRobert Nolandwithout reordering. 108308390d3bSMarius StroblFor 108408390d3bSMarius Strobl.Fn bus_dmamem_alloc , 108508390d3bSMarius Stroblthe 1086ba0b1618SRobert Noland.Dv BUS_DMA_NOCACHE 108708390d3bSMarius Stroblflag is currently implemented on amd64 and i386 where it results in the 108808390d3bSMarius StroblStrong Uncacheable PAT to be set for the allocated virtual address range. 108924503465SHiten Pandya.El 109024503465SHiten Pandya.It Fa mapp 10916f3d2701SScott LongPointer to a 10926f3d2701SScott Long.Vt bus_dmamap_t 10936f3d2701SScott Longwhere the resulting DMA map will be stored. 109424503465SHiten Pandya.El 109524503465SHiten Pandya.Pp 109624503465SHiten PandyaThe size of memory to be allocated is 109724503465SHiten Pandya.Fa maxsize 10982f46d621SJohn Baldwinas specified in the call to 10992f46d621SJohn Baldwin.Fn bus_dma_tag_create 11002f46d621SJohn Baldwinfor 110124503465SHiten Pandya.Fa dmat . 110224503465SHiten Pandya.Pp 110324503465SHiten PandyaThe current implementation of 110424503465SHiten Pandya.Fn bus_dmamem_alloc 110524503465SHiten Pandyawill allocate all requests as a single segment. 110624503465SHiten Pandya.Pp 11076f3d2701SScott LongAn initial load operation is required to obtain the bus address of the allocated 11086f3d2701SScott Longmemory, and an unload operation is required before freeing the memory, as 11096f3d2701SScott Longdescribed below in 11106f3d2701SScott Long.Fn bus_dmamem_free . 11116f3d2701SScott LongMaps are automatically handled by this function and should not be explicitly 11126f3d2701SScott Longallocated or destroyed. 11136f3d2701SScott Long.Pp 11146f3d2701SScott LongAlthough an explicit load is not required for each access to the memory 111524503465SHiten Pandyareferenced by the returned map, the synchronization requirements 111624503465SHiten Pandyaas described in the 111724503465SHiten Pandya.Fn bus_dmamap_sync 11189280e5faSMike Pritchardsection still apply and should be used to achieve portability on architectures 11196f3d2701SScott Longwithout coherent buses. 112024503465SHiten Pandya.Pp 112124503465SHiten PandyaReturns 112224503465SHiten Pandya.Er ENOMEM 112324503465SHiten Pandyaif sufficient memory is not available for completing 112424503465SHiten Pandyathe operation. 112524503465SHiten Pandya.It Fn bus_dmamem_free "dmat" "*vaddr" "map" 112624503465SHiten PandyaFrees memory previously allocated by 112724503465SHiten Pandya.Fn bus_dmamem_alloc . 112824503465SHiten PandyaAny mappings 112924503465SHiten Pandyawill be invalidated. 113024503465SHiten PandyaArguments are as follows: 113181ae4b8dSRuslan Ermilov.Bl -tag -width ".Fa vaddr" 113224503465SHiten Pandya.It Fa dmat 113324503465SHiten PandyaDMA tag. 113424503465SHiten Pandya.It Fa vaddr 113524503465SHiten PandyaKernel virtual address of the memory. 113624503465SHiten Pandya.It Fa map 113724503465SHiten PandyaDMA map to be invalidated. 113824503465SHiten Pandya.El 113924503465SHiten Pandya.El 114024503465SHiten Pandya.Sh RETURN VALUES 114124503465SHiten PandyaBehavior is undefined if invalid arguments are passed to 114224503465SHiten Pandyaany of the above functions. 114324503465SHiten PandyaIf sufficient resources cannot be allocated for a given 114424503465SHiten Pandyatransaction, 114524503465SHiten Pandya.Er ENOMEM 114624503465SHiten Pandyais returned. 114724503465SHiten PandyaAll 11482f46d621SJohn Baldwinroutines that are not of type 11492f46d621SJohn Baldwin.Vt void 115024503465SHiten Pandyawill return 0 on success or an error 11512f46d621SJohn Baldwincode on failure as discussed above. 115224503465SHiten Pandya.Pp 115324503465SHiten PandyaAll 115424503465SHiten Pandya.Vt void 115524503465SHiten Pandyaroutines will succeed if provided with valid arguments. 11564905491fSScott Long.Sh LOCKING 11574905491fSScott LongTwo locking protocols are used by 11584905491fSScott Long.Nm . 11594905491fSScott LongThe first is a private global lock that is used to synchronize access to the 11604905491fSScott Longbounce buffer pool on the architectures that make use of them. 11614905491fSScott LongThis lock is strictly a leaf lock that is only used internally to 11624905491fSScott Long.Nm 11634905491fSScott Longand is not exposed to clients of the API. 11644905491fSScott Long.Pp 11654905491fSScott LongThe second protocol involves protecting various resources stored in the tag. 11664905491fSScott LongSince almost all 11674905491fSScott Long.Nm 11684905491fSScott Longoperations are done through requests from the driver that created the tag, 11694905491fSScott Longthe most efficient way to protect the tag resources is through the lock that 11704905491fSScott Longthe driver uses. 11714905491fSScott LongIn cases where 11724905491fSScott Long.Nm 11734905491fSScott Longacts on its own without being called by the driver, the lock primitive 11744905491fSScott Longspecified in the tag is acquired and released automatically. 11754905491fSScott LongAn example of this is when the 11764905491fSScott Long.Fn bus_dmamap_load 1177b1cc1020SGiorgos Keramidascallback function is called from a deferred context instead of the driver 11784905491fSScott Longcontext. 11794905491fSScott LongThis means that certain 11804905491fSScott Long.Nm 118181ae4b8dSRuslan Ermilovfunctions must always be called with the same lock held that is specified in the 118281ae4b8dSRuslan Ermilovtag. 118381ae4b8dSRuslan ErmilovThese functions include: 11844905491fSScott Long.Pp 118581ae4b8dSRuslan Ermilov.Bl -item -offset indent -compact 118681ae4b8dSRuslan Ermilov.It 118781ae4b8dSRuslan Ermilov.Fn bus_dmamap_load 118881ae4b8dSRuslan Ermilov.It 1189a073133cSJim Harris.Fn bus_dmamap_load_bio 1190a073133cSJim Harris.It 1191a073133cSJim Harris.Fn bus_dmamap_load_ccb 119281ae4b8dSRuslan Ermilov.It 119381ae4b8dSRuslan Ermilov.Fn bus_dmamap_load_mbuf 119481ae4b8dSRuslan Ermilov.It 119581ae4b8dSRuslan Ermilov.Fn bus_dmamap_load_mbuf_sg 119681ae4b8dSRuslan Ermilov.It 1197a073133cSJim Harris.Fn bus_dmamap_load_uio 1198a073133cSJim Harris.It 119981ae4b8dSRuslan Ermilov.Fn bus_dmamap_unload 120081ae4b8dSRuslan Ermilov.It 120181ae4b8dSRuslan Ermilov.Fn bus_dmamap_sync 12024905491fSScott Long.El 12034905491fSScott Long.Pp 12044905491fSScott LongThere is one exception to this rule. 12054905491fSScott LongIt is common practice to call some of these functions during driver start-up 12064905491fSScott Longwithout any locks held. 12074905491fSScott LongSo long as there is a guarantee of no possible concurrent use of the tag by 12084905491fSScott Longdifferent threads during this operation, it is safe to not hold a lock for 12094905491fSScott Longthese functions. 12104905491fSScott Long.Pp 12114905491fSScott LongCertain 12124905491fSScott Long.Nm 12134905491fSScott Longoperations should not be called with the driver lock held, either because 12144905491fSScott Longthey are already protected by an internal lock, or because they might sleep 121581ae4b8dSRuslan Ermilovdue to memory or resource allocation. 121681ae4b8dSRuslan ErmilovThe following functions must not be 12174905491fSScott Longcalled with any non-sleepable locks held: 12184905491fSScott Long.Pp 121981ae4b8dSRuslan Ermilov.Bl -item -offset indent -compact 122081ae4b8dSRuslan Ermilov.It 122181ae4b8dSRuslan Ermilov.Fn bus_dma_tag_create 122281ae4b8dSRuslan Ermilov.It 122381ae4b8dSRuslan Ermilov.Fn bus_dmamap_create 122481ae4b8dSRuslan Ermilov.It 122581ae4b8dSRuslan Ermilov.Fn bus_dmamem_alloc 12264905491fSScott Long.El 12274905491fSScott Long.Pp 12284905491fSScott LongAll other functions do not have a locking protocol and can thus be 12292f46d621SJohn Baldwincalled with or without any system or driver locks held. 123024503465SHiten Pandya.Sh SEE ALSO 123124503465SHiten Pandya.Xr devclass 9 , 123224503465SHiten Pandya.Xr device 9 , 123324503465SHiten Pandya.Xr driver 9 , 1234490ec740SWarner Losh.Xr rman 9 , 1235490ec740SWarner Losh.Xr vslock 9 123624503465SHiten Pandya.Pp 123724503465SHiten Pandya.Rs 123824503465SHiten Pandya.%A "Jason R. Thorpe" 123924503465SHiten Pandya.%T "A Machine-Independent DMA Framework for NetBSD" 124024503465SHiten Pandya.%J "Proceedings of the Summer 1998 USENIX Technical Conference" 124124503465SHiten Pandya.%Q "USENIX Association" 124224503465SHiten Pandya.%D "June 1998" 124324503465SHiten Pandya.Re 124424503465SHiten Pandya.Sh HISTORY 124524503465SHiten PandyaThe 124624503465SHiten Pandya.Nm 124724503465SHiten Pandyainterface first appeared in 124824503465SHiten Pandya.Nx 1.3 . 124924503465SHiten Pandya.Pp 125024503465SHiten PandyaThe 125124503465SHiten Pandya.Nm 125224503465SHiten PandyaAPI was adopted from 125324503465SHiten Pandya.Nx 125424503465SHiten Pandyafor use in the CAM SCSI subsystem. 125524503465SHiten PandyaThe alterations to the original API were aimed to remove the need for 125624503465SHiten Pandyaa 125724503465SHiten Pandya.Vt bus_dma_segment_t 125824503465SHiten Pandyaarray stored in each 125924503465SHiten Pandya.Vt bus_dmamap_t 126024503465SHiten Pandyawhile allowing callers to queue up on scarce resources. 126124503465SHiten Pandya.Sh AUTHORS 126224503465SHiten PandyaThe 126324503465SHiten Pandya.Nm 1264b9b855ddSMaxime Henrioninterface was designed and implemented by 1265b9b855ddSMaxime Henrion.An Jason R. Thorpe 1266b9b855ddSMaxime Henrionof the Numerical Aerospace Simulation Facility, NASA Ames Research Center. 126724503465SHiten PandyaAdditional input on the 126824503465SHiten Pandya.Nm 1269b9b855ddSMaxime Henriondesign was provided by 1270b9b855ddSMaxime Henrion.An -nosplit 1271b9b855ddSMaxime Henrion.An Chris Demetriou , 1272b9b855ddSMaxime Henrion.An Charles Hannum , 1273b9b855ddSMaxime Henrion.An Ross Harvey , 1274b9b855ddSMaxime Henrion.An Matthew Jacob , 1275b9b855ddSMaxime Henrion.An Jonathan Stone , 1276b9b855ddSMaxime Henrionand 1277b9b855ddSMaxime Henrion.An Matt Thomas . 127824503465SHiten Pandya.Pp 127924503465SHiten PandyaThe 128024503465SHiten Pandya.Nm 128124503465SHiten Pandyainterface in 128224503465SHiten Pandya.Fx 1283b9b855ddSMaxime Henrionbenefits from the contributions of 1284b9b855ddSMaxime Henrion.An Justin T. Gibbs , 1285b9b855ddSMaxime Henrion.An Peter Wemm , 1286b9b855ddSMaxime Henrion.An Doug Rabson , 1287b9b855ddSMaxime Henrion.An Matthew N. Dodd , 1288b9b855ddSMaxime Henrion.An Sam Leffler , 1289b9b855ddSMaxime Henrion.An Maxime Henrion , 1290b9b855ddSMaxime Henrion.An Jake Burkholder , 1291b9b855ddSMaxime Henrion.An Takahashi Yoshihiro , 1292b9b855ddSMaxime Henrion.An Scott Long 1293b9b855ddSMaxime Henrionand many others. 129424503465SHiten Pandya.Pp 1295b9b855ddSMaxime HenrionThis manual page was written by 1296b9b855ddSMaxime Henrion.An Hiten M. Pandya 1297b9b855ddSMaxime Henrionand 1298b9b855ddSMaxime Henrion.An Justin T. Gibbs . 1299