xref: /dragonfly/sys/sys/bus_dma.h (revision 03517d4e)
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  */
71 
72 #ifndef _SYS_BUS_DMA_H_
73 #define _SYS_BUS_DMA_H_
74 
75 /*
76  * Include machine-specific bus stuff
77  */
78 #include <machine/bus_dma.h>	/* bus_addr_t */
79 
80 /*
81  * Flags used in various bus DMA methods.
82  */
83 #define	BUS_DMA_WAITOK		0x0000	/* safe to sleep (pseudo-flag) */
84 #define	BUS_DMA_NOWAIT		0x0001	/* not safe to sleep */
85 #define	BUS_DMA_ALLOCNOW	0x0002	/* perform resource allocation now */
86 #define	BUS_DMA_COHERENT	0x0004	/* map memory to not require sync */
87 #define	BUS_DMA_ZERO		0x0008	/* allocate zero'ed memory */
88 #define	BUS_DMA_BUS1		0x0010	/* placeholders for bus functions... */
89 #define	BUS_DMA_BUS2		0x0020
90 #define	BUS_DMA_BUS3		0x0040
91 #define	BUS_DMA_BUS4		0x0080
92 #define	BUS_DMA_ONEBPAGE	0x0100	/* allocate one bpage per map at most */
93 #define	BUS_DMA_ALIGNED		0x0200	/* no bpage should be allocated due to
94 					 * alignment requirement; all to-be-
95 					 * loaded memory is properly aligned */
96 #define BUS_DMA_PRIVBZONE	0x0400	/* need private bounce zone */
97 #define BUS_DMA_ALLOCALL	0x0800	/* allocate all needed resources */
98 #define BUS_DMA_PROTECTED	0x1000	/* all busdma functions are already
99 					 * protected */
100 #define BUS_DMA_KEEP_PG_OFFSET	0x2000	/* DMA tag hint that the page offset of the
101 					 * loaded kernel virtual address must be
102 					 * preserved in the first physical segment
103 					 * address, when the KVA is loaded into DMA. */
104 #define BUS_DMA_NOCACHE		0x4000	/* map memory uncached */
105 
106 /* Forwards needed by prototypes below. */
107 struct mbuf;
108 struct uio;
109 union ccb;
110 
111 /*
112  *	bus_dmasync_op_t
113  *
114  *	Operations performed by bus_dmamap_sync().
115  */
116 typedef int bus_dmasync_op_t;
117 
118 #define	BUS_DMASYNC_PREREAD	0x01
119 #define	BUS_DMASYNC_POSTREAD	0x02
120 #define	BUS_DMASYNC_PREWRITE	0x04
121 #define	BUS_DMASYNC_POSTWRITE	0x08
122 
123 /*
124  *	bus_dma_tag_t
125  *
126  *	A machine-dependent opaque type describing the characteristics
127  *	of how to perform DMA mappings.  This structure encapsultes
128  *	information concerning address and alignment restrictions, number
129  *	of S/G	segments, amount of data per S/G segment, etc.
130  */
131 typedef struct bus_dma_tag	*bus_dma_tag_t;
132 
133 /*
134  *	bus_dmamap_t
135  *
136  *	DMA mapping instance information.
137  */
138 typedef struct bus_dmamap	*bus_dmamap_t;
139 
140 /*
141  *	bus_dma_segment_t
142  *
143  *	Describes a single contiguous DMA transaction.  Values
144  *	are suitable for programming into DMA registers.
145  */
146 typedef struct bus_dma_segment {
147 	bus_addr_t	ds_addr;	/* DMA address */
148 	bus_size_t	ds_len;		/* length of transfer */
149 } bus_dma_segment_t;
150 
151 typedef struct bus_dmamem {
152 	bus_dma_tag_t	dmem_tag;
153 	bus_dmamap_t	dmem_map;
154 	void		*dmem_addr;
155 	bus_addr_t	dmem_busaddr;
156 } bus_dmamem_t;
157 
158 /*
159  * Allocate a device specific dma_tag encapsulating the constraints of
160  * the parent tag in addition to other restrictions specified:
161  *
162  *	alignment:	alignment for segments.
163  *	boundary:	Boundary that segments cannot cross.
164  *	lowaddr:	Low restricted address that cannot appear in a mapping.
165  *	highaddr:	High restricted address that cannot appear in a mapping.
166  *	maxsize:	Maximum mapping size supported by this tag.
167  *	nsegments:	Number of discontinuities allowed in maps.
168  *	maxsegsz:	Maximum size of a segment in the map.
169  *	flags:		Bus DMA flags.
170  *	dmat:		A pointer to set to a valid dma tag should the return
171  *			value of this function indicate success.
172  */
173 /* XXX Should probably allow specification of alignment */
174 int bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
175 		       bus_size_t boundary, bus_addr_t lowaddr,
176 		       bus_addr_t highaddr, bus_size_t maxsize, int nsegments,
177 		       bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat);
178 
179 int bus_dma_tag_destroy(bus_dma_tag_t dmat);
180 bus_size_t bus_dma_tag_getmaxsize(bus_dma_tag_t tag);
181 
182 /*
183  * Allocate a handle for mapping from kva/uva/physical
184  * address space into bus device space.
185  */
186 int bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
187 
188 /*
189  * Destroy  a handle for mapping from kva/uva/physical
190  * address space into bus device space.
191  */
192 int bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
193 
194 /*
195  * Allocate a piece of memory that can be efficiently mapped into
196  * bus device space based on the constraints lited in the dma tag.
197  * A dmamap to for use with dmamap_load is also allocated.
198  */
199 int bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
200 		     bus_dmamap_t *mapp);
201 
202 /*
203  * Free a piece of memory and it's allociated dmamap, that was allocated
204  * via bus_dmamem_alloc.
205  */
206 void bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
207 
208 /*
209  * A function that processes a successfully loaded dma map or an error
210  * from a delayed load map.
211  */
212 typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
213 
214 /*
215  * Map the buffer buf into bus space using the dmamap map.
216  */
217 int bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
218 		    bus_size_t buflen, bus_dmamap_callback_t *callback,
219 		    void *callback_arg, int flags);
220 
221 /*
222  * Like bus_dmamap_callback but includes map size in bytes.  This is
223  * defined as a separate interface to maintain compatiiblity for users
224  * of bus_dmamap_callback_t--at some point these interfaces should be merged.
225  */
226 typedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
227 /*
228  * Like bus_dmamap_load but for mbufs.  Note the use of the
229  * bus_dmamap_callback2_t interface.
230  */
231 int bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
232 			 struct mbuf *mbuf,
233 			 bus_dmamap_callback2_t *callback, void *callback_arg,
234 			 int flags);
235 /*
236  * Like bus_dmamap_load but for uios.  Note the use of the
237  * bus_dmamap_callback2_t interface.
238  */
239 int bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
240 			struct uio *ui,
241 			bus_dmamap_callback2_t *callback, void *callback_arg,
242 			int flags);
243 /*
244  * Like bus_dmamap_load but for ccb.
245  */
246 int bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map,
247 			union ccb *ccb,
248 			bus_dmamap_callback_t *callback, void *callback_arg,
249 			int flags);
250 
251 /*
252  * Like bus_dmamap_load_mbuf without callback.
253  * Segmentation information are saved in 'segs' and 'nsegs' if
254  * the loading is successful.  'maxsegs' must be set by caller
255  * and must be at least 1 but less than 'dmat' nsegment.  It
256  * indicates the number of elements in 'segs'.  'flags' must
257  * have BUS_DMA_NOWAIT turned on.
258  */
259 int
260 bus_dmamap_load_mbuf_segment(bus_dma_tag_t dmat, bus_dmamap_t map,
261 			     struct mbuf *mbuf,
262 			     bus_dma_segment_t *segs, int maxsegs,
263 			     int *nsegs, int flags);
264 
265 /*
266  * Like bus_dmamap_load_mbuf_segment, but it will call m_defrag()
267  * and try reloading if low level code indicates too many fragments
268  * in the '*mbuf'; 'mbuf' will be updated under this situation.
269  */
270 int
271 bus_dmamap_load_mbuf_defrag(bus_dma_tag_t dmat, bus_dmamap_t map,
272 			    struct mbuf **mbuf,
273 			    bus_dma_segment_t *segs, int maxsegs,
274 			    int *nsegs, int flags);
275 
276 /*
277  * Convenient function to create coherent busdma memory
278  */
279 int
280 bus_dmamem_coherent(bus_dma_tag_t parent,
281 		    bus_size_t alignment, bus_size_t boundary,
282 		    bus_addr_t lowaddr, bus_addr_t highaddr,
283 		    bus_size_t maxsize, int flags,
284 		    bus_dmamem_t *dmem);
285 
286 /*
287  * Simplified version of bus_dmamem_coherent() with:
288  * boundary == 0
289  * lowaddr  == BUS_SPACE_MAXADDR
290  * highaddr == BUS_SPACE_MAXADDR
291  *
292  * 'parent' usually should not be NULL, so we could inherit
293  * boundary, lowaddr and highaddr from it.
294  */
295 void *
296 bus_dmamem_coherent_any(bus_dma_tag_t parent, bus_size_t alignment,
297 			bus_size_t maxsize, int flags,
298 			bus_dma_tag_t *dtag, bus_dmamap_t *dmap,
299 			bus_addr_t *busaddr);
300 
301 /*
302  * Perform a syncronization operation on the given map.
303  */
304 void _bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_dmasync_op_t);
305 #define bus_dmamap_sync(dmat, dmamap, op) 		\
306 	if ((dmamap) != NULL && (dmamap) != (void *)-1)	\
307 		_bus_dmamap_sync(dmat, dmamap, op)
308 
309 /*
310  * Release the mapping held by map.
311  */
312 void _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map);
313 #define bus_dmamap_unload(dmat, dmamap) 		\
314 	if ((dmamap) != NULL && (dmamap) != (void *)-1)	\
315 		_bus_dmamap_unload(dmat, dmamap)
316 
317 #endif /* _SYS_BUS_DMA_H_ */
318