xref: /dragonfly/sys/sys/bus_dma.h (revision 8a7bdfea)
1 /*-
2  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
7  * NASA Ames Research Center.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the NetBSD
20  *	Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*
38  * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
39  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. All advertising materials mentioning features or use of this software
50  *    must display the following acknowledgement:
51  *      This product includes software developed by Christopher G. Demetriou
52  *	for the NetBSD Project.
53  * 4. The name of the author may not be used to endorse or promote products
54  *    derived from this software without specific prior written permission
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
57  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
58  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
59  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
60  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
61  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
62  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
63  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
64  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
65  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66  */
67 /*
68  * $NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $
69  * $FreeBSD: src/sys/i386/include/bus_dma.h,v 1.15.2.2 2002/11/21 23:36:01 sam Exp $
70  * $DragonFly: src/sys/sys/bus_dma.h,v 1.1 2006/10/25 20:56:03 dillon Exp $
71  */
72 
73 #ifndef _SYS_BUS_DMA_H_
74 #define _SYS_BUS_DMA_H_
75 
76 /*
77  * Include machine-specific bus stuff
78  */
79 #ifndef _MACHINE_BUS_DMA_H_
80 #include <machine/bus_dma.h>	/* bus_addr_t */
81 #endif
82 
83 /*
84  * Flags used in various bus DMA methods.
85  */
86 #define	BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
87 #define	BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
88 #define	BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
89 #define	BUS_DMA_COHERENT	0x04	/* map memory to not require sync */
90 #define	BUS_DMA_ZERO		0x08	/* allocate zero'ed memory */
91 #define	BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
92 #define	BUS_DMA_BUS2		0x20
93 #define	BUS_DMA_BUS3		0x40
94 #define	BUS_DMA_BUS4		0x80
95 
96 /* Forwards needed by prototypes below. */
97 struct mbuf;
98 struct uio;
99 
100 /*
101  *	bus_dmasync_op_t
102  *
103  *	Operations performed by bus_dmamap_sync().
104  */
105 typedef enum {
106 	BUS_DMASYNC_PREREAD,
107 	BUS_DMASYNC_POSTREAD,
108 	BUS_DMASYNC_PREWRITE,
109 	BUS_DMASYNC_POSTWRITE
110 } bus_dmasync_op_t;
111 
112 /*
113  *	bus_dma_tag_t
114  *
115  *	A machine-dependent opaque type describing the characteristics
116  *	of how to perform DMA mappings.  This structure encapsultes
117  *	information concerning address and alignment restrictions, number
118  *	of S/G	segments, amount of data per S/G segment, etc.
119  */
120 typedef struct bus_dma_tag	*bus_dma_tag_t;
121 
122 /*
123  *	bus_dmamap_t
124  *
125  *	DMA mapping instance information.
126  */
127 typedef struct bus_dmamap	*bus_dmamap_t;
128 
129 /*
130  *	bus_dma_segment_t
131  *
132  *	Describes a single contiguous DMA transaction.  Values
133  *	are suitable for programming into DMA registers.
134  */
135 typedef struct bus_dma_segment {
136 	bus_addr_t	ds_addr;	/* DMA address */
137 	bus_size_t	ds_len;		/* length of transfer */
138 } bus_dma_segment_t;
139 
140 /*
141  * A function that returns 1 if the address cannot be accessed by
142  * a device and 0 if it can be.
143  */
144 typedef int bus_dma_filter_t(void *, bus_addr_t);
145 
146 /*
147  * Allocate a device specific dma_tag encapsulating the constraints of
148  * the parent tag in addition to other restrictions specified:
149  *
150  *	alignment:	alignment for segments.
151  *	boundary:	Boundary that segments cannot cross.
152  *	lowaddr:	Low restricted address that cannot appear in a mapping.
153  *	highaddr:	High restricted address that cannot appear in a mapping.
154  *	filtfunc:	An optional function to further test if an address
155  *			within the range of lowaddr and highaddr cannot appear
156  *			in a mapping.
157  *	filtfuncarg:	An argument that will be passed to filtfunc in addition
158  *			to the address to test.
159  *	maxsize:	Maximum mapping size supported by this tag.
160  *	nsegments:	Number of discontinuities allowed in maps.
161  *	maxsegsz:	Maximum size of a segment in the map.
162  *	flags:		Bus DMA flags.
163  *	dmat:		A pointer to set to a valid dma tag should the return
164  *			value of this function indicate success.
165  */
166 /* XXX Should probably allow specification of alignment */
167 int bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
168 		       bus_size_t boundary, bus_addr_t lowaddr,
169 		       bus_addr_t highaddr, bus_dma_filter_t *filtfunc,
170 		       void *filtfuncarg, bus_size_t maxsize, int nsegments,
171 		       bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat);
172 
173 int bus_dma_tag_destroy(bus_dma_tag_t dmat);
174 
175 /*
176  * Allocate a handle for mapping from kva/uva/physical
177  * address space into bus device space.
178  */
179 int bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
180 
181 /*
182  * Destroy  a handle for mapping from kva/uva/physical
183  * address space into bus device space.
184  */
185 int bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
186 
187 /*
188  * Allocate a piece of memory that can be efficiently mapped into
189  * bus device space based on the constraints lited in the dma tag.
190  * A dmamap to for use with dmamap_load is also allocated.
191  */
192 int bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
193 		     bus_dmamap_t *mapp);
194 
195 /*
196  * Free a piece of memory and it's allociated dmamap, that was allocated
197  * via bus_dmamem_alloc.
198  */
199 void bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
200 
201 /*
202  * A function that processes a successfully loaded dma map or an error
203  * from a delayed load map.
204  */
205 typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
206 
207 /*
208  * Map the buffer buf into bus space using the dmamap map.
209  */
210 int bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
211 		    bus_size_t buflen, bus_dmamap_callback_t *callback,
212 		    void *callback_arg, int flags);
213 
214 /*
215  * Like bus_dmamap_callback but includes map size in bytes.  This is
216  * defined as a separate interface to maintain compatiiblity for users
217  * of bus_dmamap_callback_t--at some point these interfaces should be merged.
218  */
219 typedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
220 /*
221  * Like bus_dmamap_load but for mbufs.  Note the use of the
222  * bus_dmamap_callback2_t interface.
223  */
224 int bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
225 			 struct mbuf *mbuf,
226 			 bus_dmamap_callback2_t *callback, void *callback_arg,
227 			 int flags);
228 /*
229  * Like bus_dmamap_load but for uios.  Note the use of the
230  * bus_dmamap_callback2_t interface.
231  */
232 int bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
233 			struct uio *ui,
234 			bus_dmamap_callback2_t *callback, void *callback_arg,
235 			int flags);
236 
237 /*
238  * Perform a syncronization operation on the given map.
239  */
240 void _bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_dmasync_op_t);
241 #define bus_dmamap_sync(dmat, dmamap, op) 		\
242 	if ((dmamap) != NULL)				\
243 		_bus_dmamap_sync(dmat, dmamap, op)
244 
245 /*
246  * Release the mapping held by map.
247  */
248 void _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map);
249 #define bus_dmamap_unload(dmat, dmamap) 		\
250 	if ((dmamap) != NULL)				\
251 		_bus_dmamap_unload(dmat, dmamap)
252 
253 #endif /* _SYS_BUS_DMA_H_ */
254 
255