xref: /freebsd/sys/sys/bus_dma.h (revision 4f52dfbb)
1 /*	$NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: (BSD-2-Clause-NetBSD AND BSD-4-Clause)
5  *
6  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
11  * NASA Ames Research Center.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*-
36  * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
37  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  *    must display the following acknowledgement:
49  *      This product includes software developed by Christopher G. Demetriou
50  *	for the NetBSD Project.
51  * 4. The name of the author may not be used to endorse or promote products
52  *    derived from this software without specific prior written permission
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 /* $FreeBSD$ */
66 
67 #ifndef _BUS_DMA_H_
68 #define _BUS_DMA_H_
69 
70 #include <sys/_bus_dma.h>
71 
72 /*
73  * Machine independent interface for mapping physical addresses to peripheral
74  * bus 'physical' addresses, and assisting with DMA operations.
75  *
76  * XXX This file is always included from <machine/bus_dma.h> and should not
77  *     (yet) be included directly.
78  */
79 
80 /*
81  * Flags used in various bus DMA methods.
82  */
83 #define	BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
84 #define	BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
85 #define	BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
86 #define	BUS_DMA_COHERENT	0x04	/* hint: map memory in a coherent way */
87 #define	BUS_DMA_ZERO		0x08	/* allocate zero'ed memory */
88 #define	BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
89 #define	BUS_DMA_BUS2		0x20
90 #define	BUS_DMA_BUS3		0x40
91 #define	BUS_DMA_BUS4		0x80
92 
93 /*
94  * The following two flags are non-standard or specific to only certain
95  * architectures
96  */
97 #define	BUS_DMA_NOWRITE		0x100
98 #define	BUS_DMA_NOCACHE		0x200
99 
100 /*
101  * The following flag is a DMA tag hint that the page offset of the
102  * loaded kernel virtual address must be preserved in the first
103  * physical segment address, when the KVA is loaded into DMA.
104  */
105 #define	BUS_DMA_KEEP_PG_OFFSET	0x400
106 
107 #define	BUS_DMA_LOAD_MBUF	0x800
108 
109 /* Forwards needed by prototypes below. */
110 union ccb;
111 struct bio;
112 struct mbuf;
113 struct memdesc;
114 struct pmap;
115 struct uio;
116 
117 /*
118  * Operations performed by bus_dmamap_sync().
119  */
120 #define	BUS_DMASYNC_PREREAD	1
121 #define	BUS_DMASYNC_POSTREAD	2
122 #define	BUS_DMASYNC_PREWRITE	4
123 #define	BUS_DMASYNC_POSTWRITE	8
124 
125 /*
126  *	bus_dma_segment_t
127  *
128  *	Describes a single contiguous DMA transaction.  Values
129  *	are suitable for programming into DMA registers.
130  */
131 typedef struct bus_dma_segment {
132 	bus_addr_t	ds_addr;	/* DMA address */
133 	bus_size_t	ds_len;		/* length of transfer */
134 } bus_dma_segment_t;
135 
136 /*
137  * A function that returns 1 if the address cannot be accessed by
138  * a device and 0 if it can be.
139  */
140 typedef int bus_dma_filter_t(void *, bus_addr_t);
141 
142 /*
143  * Generic helper function for manipulating mutexes.
144  */
145 void busdma_lock_mutex(void *arg, bus_dma_lock_op_t op);
146 
147 /*
148  * Allocate a device specific dma_tag encapsulating the constraints of
149  * the parent tag in addition to other restrictions specified:
150  *
151  *	alignment:	Alignment for segments.
152  *	boundary:	Boundary that segments cannot cross.
153  *	lowaddr:	Low restricted address that cannot appear in a mapping.
154  *	highaddr:	High restricted address that cannot appear in a mapping.
155  *	filtfunc:	An optional function to further test if an address
156  *			within the range of lowaddr and highaddr cannot appear
157  *			in a mapping.
158  *	filtfuncarg:	An argument that will be passed to filtfunc in addition
159  *			to the address to test.
160  *	maxsize:	Maximum mapping size supported by this tag.
161  *	nsegments:	Number of discontinuities allowed in maps.
162  *	maxsegsz:	Maximum size of a segment in the map.
163  *	flags:		Bus DMA flags.
164  *	lockfunc:	An optional function to handle driver-defined lock
165  *			operations.
166  *	lockfuncarg:	An argument that will be passed to lockfunc in addition
167  *			to the lock operation.
168  *	dmat:		A pointer to set to a valid dma tag should the return
169  *			value of this function indicate success.
170  */
171 /* XXX Should probably allow specification of alignment */
172 int bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
173 		       bus_addr_t boundary, bus_addr_t lowaddr,
174 		       bus_addr_t highaddr, bus_dma_filter_t *filtfunc,
175 		       void *filtfuncarg, bus_size_t maxsize, int nsegments,
176 		       bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
177 		       void *lockfuncarg, bus_dma_tag_t *dmat);
178 
179 /*
180  * Set the memory domain to be used for allocations.
181  *
182  * Automatic for PCI devices.  Must be set prior to creating maps or
183  * allocating memory.
184  */
185 int bus_dma_tag_set_domain(bus_dma_tag_t dmat, int domain);
186 
187 int bus_dma_tag_destroy(bus_dma_tag_t dmat);
188 
189 /*
190  * A function that processes a successfully loaded dma map or an error
191  * from a delayed load map.
192  */
193 typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
194 
195 /*
196  * Like bus_dmamap_callback but includes map size in bytes.  This is
197  * defined as a separate interface to maintain compatibility for users
198  * of bus_dmamap_callback_t--at some point these interfaces should be merged.
199  */
200 typedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
201 
202 /*
203  * Map the buffer buf into bus space using the dmamap map.
204  */
205 int bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
206 		    bus_size_t buflen, bus_dmamap_callback_t *callback,
207 		    void *callback_arg, int flags);
208 
209 /*
210  * Like bus_dmamap_load but for mbufs.  Note the use of the
211  * bus_dmamap_callback2_t interface.
212  */
213 int bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
214 			 struct mbuf *mbuf,
215 			 bus_dmamap_callback2_t *callback, void *callback_arg,
216 			 int flags);
217 
218 int bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
219 			    struct mbuf *mbuf, bus_dma_segment_t *segs,
220 			    int *nsegs, int flags);
221 
222 /*
223  * Like bus_dmamap_load but for uios.  Note the use of the
224  * bus_dmamap_callback2_t interface.
225  */
226 int bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
227 			struct uio *ui,
228 			bus_dmamap_callback2_t *callback, void *callback_arg,
229 			int flags);
230 
231 /*
232  * Like bus_dmamap_load but for cam control blocks.
233  */
234 int bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
235 			bus_dmamap_callback_t *callback, void *callback_arg,
236 			int flags);
237 
238 /*
239  * Like bus_dmamap_load but for bios.
240  */
241 int bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio,
242 			bus_dmamap_callback_t *callback, void *callback_arg,
243 			int flags);
244 
245 /*
246  * Loads any memory descriptor.
247  */
248 int bus_dmamap_load_mem(bus_dma_tag_t dmat, bus_dmamap_t map,
249 			struct memdesc *mem, bus_dmamap_callback_t *callback,
250 			void *callback_arg, int flags);
251 
252 /*
253  * Placeholder for use by busdma implementations which do not benefit
254  * from optimized procedure to load an array of vm_page_t.  Falls back
255  * to do _bus_dmamap_load_phys() in loop.
256  */
257 int bus_dmamap_load_ma_triv(bus_dma_tag_t dmat, bus_dmamap_t map,
258     struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
259     bus_dma_segment_t *segs, int *segp);
260 
261 #ifdef WANT_INLINE_DMAMAP
262 #define BUS_DMAMAP_OP static inline
263 #else
264 #define BUS_DMAMAP_OP
265 #endif
266 
267 /*
268  * Allocate a handle for mapping from kva/uva/physical
269  * address space into bus device space.
270  */
271 BUS_DMAMAP_OP int bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
272 
273 /*
274  * Destroy a handle for mapping from kva/uva/physical
275  * address space into bus device space.
276  */
277 BUS_DMAMAP_OP int bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
278 
279 /*
280  * Allocate a piece of memory that can be efficiently mapped into
281  * bus device space based on the constraints listed in the dma tag.
282  * A dmamap to for use with dmamap_load is also allocated.
283  */
284 BUS_DMAMAP_OP int bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
285 		     bus_dmamap_t *mapp);
286 
287 /*
288  * Free a piece of memory and its allocated dmamap, that was allocated
289  * via bus_dmamem_alloc.
290  */
291 BUS_DMAMAP_OP void bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
292 
293 /*
294  * Perform a synchronization operation on the given map. If the map
295  * is NULL we have a fully IO-coherent system.
296  */
297 BUS_DMAMAP_OP void bus_dmamap_sync(bus_dma_tag_t dmat, bus_dmamap_t dmamap, bus_dmasync_op_t op);
298 
299 /*
300  * Release the mapping held by map.
301  */
302 BUS_DMAMAP_OP void bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t dmamap);
303 
304 #undef BUS_DMAMAP_OP
305 
306 #endif /* _BUS_DMA_H_ */
307