bus_dma.h revision 84593
120253Sjoerg/*	$NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $	*/
220302Sjoerg
320302Sjoerg/*-
420253Sjoerg * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
520253Sjoerg * All rights reserved.
620253Sjoerg *
720253Sjoerg * This code is derived from software contributed to The NetBSD Foundation
820253Sjoerg * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
920302Sjoerg * NASA Ames Research Center.
1020253Sjoerg *
1120253Sjoerg * Redistribution and use in source and binary forms, with or without
1220253Sjoerg * modification, are permitted provided that the following conditions
1320253Sjoerg * are met:
1420302Sjoerg * 1. Redistributions of source code must retain the above copyright
1520253Sjoerg *    notice, this list of conditions and the following disclaimer.
1620253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1720302Sjoerg *    notice, this list of conditions and the following disclaimer in the
1820253Sjoerg *    documentation and/or other materials provided with the distribution.
1920253Sjoerg * 3. All advertising materials mentioning features or use of this software
2020253Sjoerg *    must display the following acknowledgement:
2120253Sjoerg *	This product includes software developed by the NetBSD
2220253Sjoerg *	Foundation, Inc. and its contributors.
2320253Sjoerg * 4. Neither the name of The NetBSD Foundation nor the names of its
2420253Sjoerg *    contributors may be used to endorse or promote products derived
2544229Sdavidn *    from this software without specific prior written permission.
2620253Sjoerg *
2720253Sjoerg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2830259Scharnier * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2930259Scharnier * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3050479Speter * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
3130259Scharnier * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3230259Scharnier * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3330259Scharnier * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3430259Scharnier * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3520253Sjoerg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3620253Sjoerg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3720253Sjoerg * POSSIBILITY OF SUCH DAMAGE.
3830259Scharnier */
3920253Sjoerg
4020555Sdavidn/*
4120555Sdavidn * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
4220555Sdavidn * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
4364918Sgreen *
44242349Sbapt * Redistribution and use in source and binary forms, with or without
45242349Sbapt * modification, are permitted provided that the following conditions
46242349Sbapt * are met:
4720253Sjoerg * 1. Redistributions of source code must retain the above copyright
4820253Sjoerg *    notice, this list of conditions and the following disclaimer.
4920253Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
5023318Sache *    notice, this list of conditions and the following disclaimer in the
5122394Sdavidn *    documentation and/or other materials provided with the distribution.
5252512Sdavidn * 3. All advertising materials mentioning features or use of this software
5324214Sache *    must display the following acknowledgement:
54285401Sbapt *      This product includes software developed by Christopher G. Demetriou
55284124Sbapt *	for the NetBSD Project.
56284133Sbapt * 4. The name of the author may not be used to endorse or promote products
57284118Sbapt *    derived from this software without specific prior written permission
5820253Sjoerg *
5920253Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
6020253Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
6120253Sjoerg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62285409Sbapt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
6320253Sjoerg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
6420253Sjoerg * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
6585145Sache * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
6620253Sjoerg * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67283961Sbapt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68285403Sbapt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69283961Sbapt */
70284118Sbapt/* $FreeBSD: head/sys/sys/bus_dma.h 84593 2001-10-06 16:27:21Z nyan $ */
71285430Sbapt
72285430Sbapt#ifndef _I386_BUS_DMA_H_
73283961Sbapt#define _I386_BUS_DMA_H_
74285430Sbapt
75283961Sbapt/*
76285430Sbapt * Flags used in various bus DMA methods.
77285434Sbapt */
78285434Sbapt#define	BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
79285434Sbapt#define	BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
80283961Sbapt#define	BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
81283961Sbapt#define	BUS_DMAMEM_NOSYNC	0x04	/* map memory to not require sync */
82285430Sbapt#define	BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
83285430Sbapt#define	BUS_DMA_BUS2		0x20
84285403Sbapt#define	BUS_DMA_BUS3		0x40
85283961Sbapt#define	BUS_DMA_BUS4		0x80
86283961Sbapt
87283961Sbapt/* Forwards needed by prototypes below. */
88285133Sbaptstruct mbuf;
89285137Sbaptstruct uio;
90285133Sbapt
91285133Sbapt/*
92285133Sbapt *	bus_dmasync_op_t
93285133Sbapt *
94285133Sbapt *	Operations performed by bus_dmamap_sync().
95285133Sbapt */
96285133Sbapttypedef enum {
97285133Sbapt	BUS_DMASYNC_PREREAD,
98285133Sbapt	BUS_DMASYNC_POSTREAD,
99285133Sbapt	BUS_DMASYNC_PREWRITE,
100285133Sbapt	BUS_DMASYNC_POSTWRITE
101285133Sbapt} bus_dmasync_op_t;
102285133Sbapt
103285133Sbapt/*
104285133Sbapt *	bus_dma_tag_t
105285133Sbapt *
106285133Sbapt *	A machine-dependent opaque type describing the characteristics
107285133Sbapt *	of how to perform DMA mappings.  This structure encapsultes
108285133Sbapt *	information concerning address and alignment restrictions, number
109285137Sbapt *	of S/G	segments, amount of data per S/G segment, etc.
110285133Sbapt */
111285133Sbapttypedef struct bus_dma_tag	*bus_dma_tag_t;
112285133Sbapt
113285133Sbapt/*
114285133Sbapt *	bus_dmamap_t
115285133Sbapt *
116285133Sbapt *	DMA mapping instance information.
117285133Sbapt */
118285133Sbapttypedef struct bus_dmamap	*bus_dmamap_t;
119285133Sbapt
120285133Sbapt/*
121285133Sbapt *	bus_dma_segment_t
122285133Sbapt *
123285133Sbapt *	Describes a single contiguous DMA transaction.  Values
124285133Sbapt *	are suitable for programming into DMA registers.
125285133Sbapt */
126285133Sbapttypedef struct bus_dma_segment {
127285133Sbapt	bus_addr_t	ds_addr;	/* DMA address */
128285133Sbapt	bus_size_t	ds_len;		/* length of transfer */
129285133Sbapt} bus_dma_segment_t;
130285133Sbapt
131285133Sbapt/*
132285133Sbapt * A function that returns 1 if the address cannot be accessed by
133285133Sbapt * a device and 0 if it can be.
134285133Sbapt */
135285133Sbapttypedef int bus_dma_filter_t(void *, bus_addr_t);
136285133Sbapt
137285137Sbapt/*
138285133Sbapt * Allocate a device specific dma_tag encapsulating the constraints of
139285133Sbapt * the parent tag in addition to other restrictions specified:
140285133Sbapt *
141285133Sbapt *	alignment:	alignment for segments.
142285133Sbapt *	boundary:	Boundary that segments cannot cross.
143285133Sbapt *	lowaddr:	Low restricted address that cannot appear in a mapping.
144285133Sbapt *	highaddr:	High restricted address that cannot appear in a mapping.
145285133Sbapt *	filtfunc:	An optional function to further test if an address
146285133Sbapt *			within the range of lowaddr and highaddr cannot appear
147285133Sbapt *			in a mapping.
148285133Sbapt *	filtfuncarg:	An argument that will be passed to filtfunc in addition
149285133Sbapt *			to the address to test.
150285395Sbapt *	maxsize:	Maximum mapping size supported by this tag.
151285395Sbapt *	nsegments:	Number of discontinuities allowed in maps.
152285395Sbapt *	maxsegsz:	Maximum size of a segment in the map.
153285395Sbapt *	flags:		Bus DMA flags.
154285395Sbapt *	dmat:		A pointer to set to a valid dma tag should the return
155285395Sbapt *			value of this function indicate success.
156285395Sbapt */
157285395Sbapt/* XXX Should probably allow specification of alignment */
158285395Sbaptint bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
159285395Sbapt		       bus_size_t boundary, bus_addr_t lowaddr,
160285395Sbapt		       bus_addr_t highaddr, bus_dma_filter_t *filtfunc,
161285395Sbapt		       void *filtfuncarg, bus_size_t maxsize, int nsegments,
162285395Sbapt		       bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat);
163285395Sbapt
164285398Sbaptint bus_dma_tag_destroy(bus_dma_tag_t dmat);
165285398Sbapt
166285398Sbapt/*
167285398Sbapt * Allocate a handle for mapping from kva/uva/physical
168285398Sbapt * address space into bus device space.
169285398Sbapt */
170285398Sbaptint bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
171285398Sbapt
172285398Sbapt/*
173285398Sbapt * Destroy  a handle for mapping from kva/uva/physical
174285398Sbapt * address space into bus device space.
175285398Sbapt */
176285398Sbaptint bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
177285398Sbapt
178285398Sbapt/*
179285398Sbapt * Allocate a piece of memory that can be efficiently mapped into
180285398Sbapt * bus device space based on the constraints lited in the dma tag.
181285398Sbapt * A dmamap to for use with dmamap_load is also allocated.
182285398Sbapt */
183285398Sbaptint bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
184285398Sbapt		     bus_dmamap_t *mapp);
185285398Sbapt
186285398Sbapt/*
187285398Sbapt * Free a piece of memory and it's allociated dmamap, that was allocated
188285398Sbapt * via bus_dmamem_alloc.
189285398Sbapt */
190285398Sbaptvoid bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
191285398Sbapt
192285398Sbapt/*
193285398Sbapt * A function that processes a successfully loaded dma map or an error
194285405Sbapt * from a delayed load map.
195285405Sbapt */
196285405Sbapttypedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
197285405Sbapt
198285405Sbapt/*
199285405Sbapt * Map the buffer buf into bus space using the dmamap map.
200285405Sbapt */
201285405Sbaptint bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
202285405Sbapt		    bus_size_t buflen, bus_dmamap_callback_t *callback,
203285405Sbapt		    void *callback_arg, int flags);
204285405Sbapt
205285405Sbapt/*
206285405Sbapt * Perform a syncronization operation on the given map.
207285405Sbapt */
208285405Sbaptvoid _bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_dmasync_op_t);
209285405Sbapt#define bus_dmamap_sync(dmat, dmamap, op) 		\
210285405Sbapt	if ((dmamap) != NULL)				\
211285405Sbapt		_bus_dmamap_sync(dmat, dmamap, op)
212285405Sbapt
213285405Sbapt/*
214285405Sbapt * Release the mapping held by map.
215285405Sbapt */
216285405Sbaptvoid _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map);
217285405Sbapt#define bus_dmamap_unload(dmat, dmamap) 		\
218285405Sbapt	if ((dmamap) != NULL)				\
219285405Sbapt		_bus_dmamap_unload(dmat, dmamap)
220285405Sbapt
221285405Sbapt#endif /* _I386_BUS_DMA_H_ */
222285405Sbapt