bus_dma.h revision 139790
132517Sgibbs/*	$NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $	*/
232517Sgibbs
332517Sgibbs/*-
432517Sgibbs * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
532517Sgibbs * All rights reserved.
632517Sgibbs *
732517Sgibbs * This code is derived from software contributed to The NetBSD Foundation
832517Sgibbs * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
932517Sgibbs * NASA Ames Research Center.
1032517Sgibbs *
1132517Sgibbs * Redistribution and use in source and binary forms, with or without
1232517Sgibbs * modification, are permitted provided that the following conditions
1332517Sgibbs * are met:
1432517Sgibbs * 1. Redistributions of source code must retain the above copyright
1532517Sgibbs *    notice, this list of conditions and the following disclaimer.
1632517Sgibbs * 2. Redistributions in binary form must reproduce the above copyright
1732517Sgibbs *    notice, this list of conditions and the following disclaimer in the
1832517Sgibbs *    documentation and/or other materials provided with the distribution.
1932517Sgibbs * 3. All advertising materials mentioning features or use of this software
2032517Sgibbs *    must display the following acknowledgement:
2132517Sgibbs *	This product includes software developed by the NetBSD
2232517Sgibbs *	Foundation, Inc. and its contributors.
2332517Sgibbs * 4. Neither the name of The NetBSD Foundation nor the names of its
2432517Sgibbs *    contributors may be used to endorse or promote products derived
2532517Sgibbs *    from this software without specific prior written permission.
2632517Sgibbs *
2732517Sgibbs * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2832517Sgibbs * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2932517Sgibbs * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3032517Sgibbs * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3132517Sgibbs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3232517Sgibbs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3332517Sgibbs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3432517Sgibbs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3532517Sgibbs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3632517Sgibbs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3732517Sgibbs * POSSIBILITY OF SUCH DAMAGE.
3832517Sgibbs */
3932517Sgibbs
40139790Simp/*-
4132517Sgibbs * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
4232517Sgibbs * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
4332517Sgibbs *
4432517Sgibbs * Redistribution and use in source and binary forms, with or without
4532517Sgibbs * modification, are permitted provided that the following conditions
4632517Sgibbs * are met:
4732517Sgibbs * 1. Redistributions of source code must retain the above copyright
4832517Sgibbs *    notice, this list of conditions and the following disclaimer.
4932517Sgibbs * 2. Redistributions in binary form must reproduce the above copyright
5032517Sgibbs *    notice, this list of conditions and the following disclaimer in the
5132517Sgibbs *    documentation and/or other materials provided with the distribution.
5232517Sgibbs * 3. All advertising materials mentioning features or use of this software
5332517Sgibbs *    must display the following acknowledgement:
5432517Sgibbs *      This product includes software developed by Christopher G. Demetriou
5532517Sgibbs *	for the NetBSD Project.
5632517Sgibbs * 4. The name of the author may not be used to endorse or promote products
5732517Sgibbs *    derived from this software without specific prior written permission
5832517Sgibbs *
5932517Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
6032517Sgibbs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6132517Sgibbs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
6232517Sgibbs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
6332517Sgibbs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
6432517Sgibbs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
6532517Sgibbs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
6632517Sgibbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6732517Sgibbs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
6832517Sgibbs * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6932517Sgibbs */
7050477Speter/* $FreeBSD: head/sys/sys/bus_dma.h 139790 2005-01-06 22:18:23Z imp $ */
7132517Sgibbs
7284593Snyan#ifndef _I386_BUS_DMA_H_
7384593Snyan#define _I386_BUS_DMA_H_
7432517Sgibbs
7532517Sgibbs/*
7632517Sgibbs * Flags used in various bus DMA methods.
7732517Sgibbs */
7832517Sgibbs#define	BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
7932517Sgibbs#define	BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
8032517Sgibbs#define	BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
81115416Shmp#define	BUS_DMA_COHERENT	0x04	/* hint: map memory in a coherent way */
82118081Smux#define	BUS_DMA_ZERO		0x08	/* allocate zero'ed memory */
8332517Sgibbs#define	BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
8432517Sgibbs#define	BUS_DMA_BUS2		0x20
8532517Sgibbs#define	BUS_DMA_BUS3		0x40
8632517Sgibbs#define	BUS_DMA_BUS4		0x80
8732517Sgibbs
8832517Sgibbs/* Forwards needed by prototypes below. */
8932517Sgibbsstruct mbuf;
9032517Sgibbsstruct uio;
9132517Sgibbs
9232517Sgibbs/*
93113347Smux * Operations performed by bus_dmamap_sync().
9432517Sgibbs */
95115343Sscottltypedef int bus_dmasync_op_t;
96113347Smux#define	BUS_DMASYNC_PREREAD	1
97113347Smux#define	BUS_DMASYNC_POSTREAD	2
98113347Smux#define	BUS_DMASYNC_PREWRITE	4
99113347Smux#define	BUS_DMASYNC_POSTWRITE	8
10032517Sgibbs
10132517Sgibbs/*
10232517Sgibbs *	bus_dma_tag_t
10332517Sgibbs *
10432517Sgibbs *	A machine-dependent opaque type describing the characteristics
10532517Sgibbs *	of how to perform DMA mappings.  This structure encapsultes
10632517Sgibbs *	information concerning address and alignment restrictions, number
107122154Sanholt *	of S/G segments, amount of data per S/G segment, etc.
10832517Sgibbs */
10932517Sgibbstypedef struct bus_dma_tag	*bus_dma_tag_t;
11032517Sgibbs
11132517Sgibbs/*
11232517Sgibbs *	bus_dmamap_t
11332517Sgibbs *
11432517Sgibbs *	DMA mapping instance information.
11532517Sgibbs */
11632517Sgibbstypedef struct bus_dmamap	*bus_dmamap_t;
11732517Sgibbs
11832517Sgibbs/*
11932517Sgibbs *	bus_dma_segment_t
12032517Sgibbs *
12132517Sgibbs *	Describes a single contiguous DMA transaction.  Values
12232517Sgibbs *	are suitable for programming into DMA registers.
12332517Sgibbs */
12432517Sgibbstypedef struct bus_dma_segment {
12532517Sgibbs	bus_addr_t	ds_addr;	/* DMA address */
12632517Sgibbs	bus_size_t	ds_len;		/* length of transfer */
12732517Sgibbs} bus_dma_segment_t;
12832517Sgibbs
12932517Sgibbs/*
13032517Sgibbs * A function that returns 1 if the address cannot be accessed by
13132517Sgibbs * a device and 0 if it can be.
13232517Sgibbs */
13332517Sgibbstypedef int bus_dma_filter_t(void *, bus_addr_t);
13432517Sgibbs
13532517Sgibbs/*
136122154Sanholt * A function that performs driver-specific synchronization on behalf of
137117126Sscottl * busdma.
138117126Sscottl */
139117126Sscottltypedef enum {
140117126Sscottl	BUS_DMA_LOCK	= 0x01,
141117126Sscottl	BUS_DMA_UNLOCK	= 0x02,
142117126Sscottl} bus_dma_lock_op_t;
143117126Sscottl
144117126Sscottltypedef void bus_dma_lock_t(void *, bus_dma_lock_op_t);
145117126Sscottl
146117126Sscottl/*
14732517Sgibbs * Allocate a device specific dma_tag encapsulating the constraints of
14832517Sgibbs * the parent tag in addition to other restrictions specified:
14932517Sgibbs *
150122154Sanholt *	alignment:	Alignment for segments.
15132517Sgibbs *	boundary:	Boundary that segments cannot cross.
15232517Sgibbs *	lowaddr:	Low restricted address that cannot appear in a mapping.
15332517Sgibbs *	highaddr:	High restricted address that cannot appear in a mapping.
15432517Sgibbs *	filtfunc:	An optional function to further test if an address
15532517Sgibbs *			within the range of lowaddr and highaddr cannot appear
15632517Sgibbs *			in a mapping.
15732517Sgibbs *	filtfuncarg:	An argument that will be passed to filtfunc in addition
15832517Sgibbs *			to the address to test.
15935767Sgibbs *	maxsize:	Maximum mapping size supported by this tag.
16035767Sgibbs *	nsegments:	Number of discontinuities allowed in maps.
16135767Sgibbs *	maxsegsz:	Maximum size of a segment in the map.
16232517Sgibbs *	flags:		Bus DMA flags.
163122266Sscottl *	lockfunc:	An optional function to handle driver-defined lock
164122266Sscottl *			operations.
165122266Sscottl *	lockfuncarg:	An argument that will be passed to lockfunc in addition
166122266Sscottl *			to the lock operation.
16732517Sgibbs *	dmat:		A pointer to set to a valid dma tag should the return
16832517Sgibbs *			value of this function indicate success.
16932517Sgibbs */
17035767Sgibbs/* XXX Should probably allow specification of alignment */
17178353Salexint bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
17235767Sgibbs		       bus_size_t boundary, bus_addr_t lowaddr,
17335767Sgibbs		       bus_addr_t highaddr, bus_dma_filter_t *filtfunc,
17435767Sgibbs		       void *filtfuncarg, bus_size_t maxsize, int nsegments,
175117126Sscottl		       bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
176117126Sscottl		       void *lockfuncarg, bus_dma_tag_t *dmat);
17732517Sgibbs
17832517Sgibbsint bus_dma_tag_destroy(bus_dma_tag_t dmat);
17932517Sgibbs
18032517Sgibbs/*
18132517Sgibbs * Allocate a handle for mapping from kva/uva/physical
18232517Sgibbs * address space into bus device space.
18332517Sgibbs */
18432517Sgibbsint bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
18532517Sgibbs
18632517Sgibbs/*
187122154Sanholt * Destroy a handle for mapping from kva/uva/physical
18832517Sgibbs * address space into bus device space.
18932517Sgibbs */
19032517Sgibbsint bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
19132517Sgibbs
19232517Sgibbs/*
19335767Sgibbs * Allocate a piece of memory that can be efficiently mapped into
194122154Sanholt * bus device space based on the constraints listed in the dma tag.
19535767Sgibbs * A dmamap to for use with dmamap_load is also allocated.
19635767Sgibbs */
19735767Sgibbsint bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
19835767Sgibbs		     bus_dmamap_t *mapp);
19935767Sgibbs
20035767Sgibbs/*
201122154Sanholt * Free a piece of memory and its allocated dmamap, that was allocated
20235767Sgibbs * via bus_dmamem_alloc.
20335767Sgibbs */
20435767Sgibbsvoid bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
20535767Sgibbs
20635767Sgibbs/*
20732517Sgibbs * A function that processes a successfully loaded dma map or an error
20832517Sgibbs * from a delayed load map.
20932517Sgibbs */
21032517Sgibbstypedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
21132517Sgibbs
21232517Sgibbs/*
21332517Sgibbs * Map the buffer buf into bus space using the dmamap map.
21432517Sgibbs */
21532517Sgibbsint bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
21632517Sgibbs		    bus_size_t buflen, bus_dmamap_callback_t *callback,
21732517Sgibbs		    void *callback_arg, int flags);
21832517Sgibbs
21932517Sgibbs/*
220104486Ssam * Like bus_dmamap_callback but includes map size in bytes.  This is
221122154Sanholt * defined as a separate interface to maintain compatibility for users
222104486Ssam * of bus_dmamap_callback_t--at some point these interfaces should be merged.
223104486Ssam */
224104486Ssamtypedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
225104486Ssam/*
226104486Ssam * Like bus_dmamap_load but for mbufs.  Note the use of the
227104486Ssam * bus_dmamap_callback2_t interface.
228104486Ssam */
229104486Ssamint bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
230104486Ssam			 struct mbuf *mbuf,
231104486Ssam			 bus_dmamap_callback2_t *callback, void *callback_arg,
232104486Ssam			 int flags);
233104486Ssam/*
234104486Ssam * Like bus_dmamap_load but for uios.  Note the use of the
235104486Ssam * bus_dmamap_callback2_t interface.
236104486Ssam */
237104486Ssamint bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
238104486Ssam			struct uio *ui,
239104486Ssam			bus_dmamap_callback2_t *callback, void *callback_arg,
240104486Ssam			int flags);
241104486Ssam
242104486Ssam/*
243122154Sanholt * Perform a synchronization operation on the given map.
24432517Sgibbs */
245115343Sscottlvoid _bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_dmasync_op_t);
24632517Sgibbs#define bus_dmamap_sync(dmat, dmamap, op) 		\
24732517Sgibbs	if ((dmamap) != NULL)				\
24832517Sgibbs		_bus_dmamap_sync(dmat, dmamap, op)
24932517Sgibbs
25032517Sgibbs/*
25132517Sgibbs * Release the mapping held by map.
25232517Sgibbs */
25332517Sgibbsvoid _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map);
25432517Sgibbs#define bus_dmamap_unload(dmat, dmamap) 		\
25532517Sgibbs	if ((dmamap) != NULL)				\
25632517Sgibbs		_bus_dmamap_unload(dmat, dmamap)
25732517Sgibbs
258117126Sscottl/*
259117126Sscottl * Generic helper function for manipulating mutexes.
260117126Sscottl */
261117126Sscottlvoid busdma_lock_mutex(void *arg, bus_dma_lock_op_t op);
26284593Snyan#endif /* _I386_BUS_DMA_H_ */
263