xref: /freebsd/sys/amd64/include/bus_dma.h (revision e9b193dc33f2e24df317c16c83c26865e774e11a)
1e1a1bba4SJustin T. Gibbs /*	$NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $	*/
2e1a1bba4SJustin T. Gibbs 
3e1a1bba4SJustin T. Gibbs /*-
4e1a1bba4SJustin T. Gibbs  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5e1a1bba4SJustin T. Gibbs  * All rights reserved.
6e1a1bba4SJustin T. Gibbs  *
7e1a1bba4SJustin T. Gibbs  * This code is derived from software contributed to The NetBSD Foundation
8e1a1bba4SJustin T. Gibbs  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9e1a1bba4SJustin T. Gibbs  * NASA Ames Research Center.
10e1a1bba4SJustin T. Gibbs  *
11e1a1bba4SJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
12e1a1bba4SJustin T. Gibbs  * modification, are permitted provided that the following conditions
13e1a1bba4SJustin T. Gibbs  * are met:
14e1a1bba4SJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
15e1a1bba4SJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer.
16e1a1bba4SJustin T. Gibbs  * 2. Redistributions in binary form must reproduce the above copyright
17e1a1bba4SJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer in the
18e1a1bba4SJustin T. Gibbs  *    documentation and/or other materials provided with the distribution.
19e1a1bba4SJustin T. Gibbs  * 3. All advertising materials mentioning features or use of this software
20e1a1bba4SJustin T. Gibbs  *    must display the following acknowledgement:
21e1a1bba4SJustin T. Gibbs  *	This product includes software developed by the NetBSD
22e1a1bba4SJustin T. Gibbs  *	Foundation, Inc. and its contributors.
23e1a1bba4SJustin T. Gibbs  * 4. Neither the name of The NetBSD Foundation nor the names of its
24e1a1bba4SJustin T. Gibbs  *    contributors may be used to endorse or promote products derived
25e1a1bba4SJustin T. Gibbs  *    from this software without specific prior written permission.
26e1a1bba4SJustin T. Gibbs  *
27e1a1bba4SJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28e1a1bba4SJustin T. Gibbs  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29e1a1bba4SJustin T. Gibbs  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30e1a1bba4SJustin T. Gibbs  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31e1a1bba4SJustin T. Gibbs  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32e1a1bba4SJustin T. Gibbs  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33e1a1bba4SJustin T. Gibbs  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34e1a1bba4SJustin T. Gibbs  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35e1a1bba4SJustin T. Gibbs  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36e1a1bba4SJustin T. Gibbs  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37e1a1bba4SJustin T. Gibbs  * POSSIBILITY OF SUCH DAMAGE.
38e1a1bba4SJustin T. Gibbs  */
39e1a1bba4SJustin T. Gibbs 
40e1a1bba4SJustin T. Gibbs /*
41e1a1bba4SJustin T. Gibbs  * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
42e1a1bba4SJustin T. Gibbs  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
43e1a1bba4SJustin T. Gibbs  *
44e1a1bba4SJustin T. Gibbs  * Redistribution and use in source and binary forms, with or without
45e1a1bba4SJustin T. Gibbs  * modification, are permitted provided that the following conditions
46e1a1bba4SJustin T. Gibbs  * are met:
47e1a1bba4SJustin T. Gibbs  * 1. Redistributions of source code must retain the above copyright
48e1a1bba4SJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer.
49e1a1bba4SJustin T. Gibbs  * 2. Redistributions in binary form must reproduce the above copyright
50e1a1bba4SJustin T. Gibbs  *    notice, this list of conditions and the following disclaimer in the
51e1a1bba4SJustin T. Gibbs  *    documentation and/or other materials provided with the distribution.
52e1a1bba4SJustin T. Gibbs  * 3. All advertising materials mentioning features or use of this software
53e1a1bba4SJustin T. Gibbs  *    must display the following acknowledgement:
54e1a1bba4SJustin T. Gibbs  *      This product includes software developed by Christopher G. Demetriou
55e1a1bba4SJustin T. Gibbs  *	for the NetBSD Project.
56e1a1bba4SJustin T. Gibbs  * 4. The name of the author may not be used to endorse or promote products
57e1a1bba4SJustin T. Gibbs  *    derived from this software without specific prior written permission
58e1a1bba4SJustin T. Gibbs  *
59e1a1bba4SJustin T. Gibbs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60e1a1bba4SJustin T. Gibbs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61e1a1bba4SJustin T. Gibbs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62e1a1bba4SJustin T. Gibbs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63e1a1bba4SJustin T. Gibbs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64e1a1bba4SJustin T. Gibbs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65e1a1bba4SJustin T. Gibbs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66e1a1bba4SJustin T. Gibbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67e1a1bba4SJustin T. Gibbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68e1a1bba4SJustin T. Gibbs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69e1a1bba4SJustin T. Gibbs  */
70c3aac50fSPeter Wemm /* $FreeBSD$ */
71e1a1bba4SJustin T. Gibbs 
72e9b193dcSPeter Wemm #ifndef _AMD64_BUS_DMA_H_
73e9b193dcSPeter Wemm #define _AMD64_BUS_DMA_H_
74f0ba8e21SMike Smith 
7525f3f7c5SJohn Baldwin /*
76e1a1bba4SJustin T. Gibbs  * Flags used in various bus DMA methods.
77e1a1bba4SJustin T. Gibbs  */
78e1a1bba4SJustin T. Gibbs #define	BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
79e1a1bba4SJustin T. Gibbs #define	BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
80e1a1bba4SJustin T. Gibbs #define	BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
81e1a1bba4SJustin T. Gibbs #define	BUS_DMAMEM_NOSYNC	0x04	/* map memory to not require sync */
82e1a1bba4SJustin T. Gibbs #define	BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
83e1a1bba4SJustin T. Gibbs #define	BUS_DMA_BUS2		0x20
84e1a1bba4SJustin T. Gibbs #define	BUS_DMA_BUS3		0x40
85e1a1bba4SJustin T. Gibbs #define	BUS_DMA_BUS4		0x80
86e1a1bba4SJustin T. Gibbs 
87e1a1bba4SJustin T. Gibbs /* Forwards needed by prototypes below. */
88e1a1bba4SJustin T. Gibbs struct mbuf;
89e1a1bba4SJustin T. Gibbs struct uio;
90e1a1bba4SJustin T. Gibbs 
91e1a1bba4SJustin T. Gibbs /*
92e1a1bba4SJustin T. Gibbs  * Operations performed by bus_dmamap_sync().
93e1a1bba4SJustin T. Gibbs  */
94141bacb0SMaxime Henrion #define	BUS_DMASYNC_PREREAD	1
95141bacb0SMaxime Henrion #define	BUS_DMASYNC_POSTREAD	2
96141bacb0SMaxime Henrion #define	BUS_DMASYNC_PREWRITE	4
97141bacb0SMaxime Henrion #define	BUS_DMASYNC_POSTWRITE	8
98e1a1bba4SJustin T. Gibbs 
99e1a1bba4SJustin T. Gibbs /*
100e1a1bba4SJustin T. Gibbs  *	bus_dma_tag_t
101e1a1bba4SJustin T. Gibbs  *
102e1a1bba4SJustin T. Gibbs  *	A machine-dependent opaque type describing the characteristics
103e1a1bba4SJustin T. Gibbs  *	of how to perform DMA mappings.  This structure encapsultes
104e1a1bba4SJustin T. Gibbs  *	information concerning address and alignment restrictions, number
105e1a1bba4SJustin T. Gibbs  *	of S/G	segments, amount of data per S/G segment, etc.
106e1a1bba4SJustin T. Gibbs  */
107e1a1bba4SJustin T. Gibbs typedef struct bus_dma_tag	*bus_dma_tag_t;
108e1a1bba4SJustin T. Gibbs 
109e1a1bba4SJustin T. Gibbs /*
110e1a1bba4SJustin T. Gibbs  *	bus_dmamap_t
111e1a1bba4SJustin T. Gibbs  *
112e1a1bba4SJustin T. Gibbs  *	DMA mapping instance information.
113e1a1bba4SJustin T. Gibbs  */
114e1a1bba4SJustin T. Gibbs typedef struct bus_dmamap	*bus_dmamap_t;
115e1a1bba4SJustin T. Gibbs 
116e1a1bba4SJustin T. Gibbs /*
117e1a1bba4SJustin T. Gibbs  *	bus_dma_segment_t
118e1a1bba4SJustin T. Gibbs  *
119e1a1bba4SJustin T. Gibbs  *	Describes a single contiguous DMA transaction.  Values
120e1a1bba4SJustin T. Gibbs  *	are suitable for programming into DMA registers.
121e1a1bba4SJustin T. Gibbs  */
122e1a1bba4SJustin T. Gibbs typedef struct bus_dma_segment {
123e1a1bba4SJustin T. Gibbs 	bus_addr_t	ds_addr;	/* DMA address */
124e1a1bba4SJustin T. Gibbs 	bus_size_t	ds_len;		/* length of transfer */
125e1a1bba4SJustin T. Gibbs } bus_dma_segment_t;
126e1a1bba4SJustin T. Gibbs 
127e1a1bba4SJustin T. Gibbs /*
128e1a1bba4SJustin T. Gibbs  * A function that returns 1 if the address cannot be accessed by
129e1a1bba4SJustin T. Gibbs  * a device and 0 if it can be.
130e1a1bba4SJustin T. Gibbs  */
131e1a1bba4SJustin T. Gibbs typedef int bus_dma_filter_t(void *, bus_addr_t);
132e1a1bba4SJustin T. Gibbs 
133e1a1bba4SJustin T. Gibbs /*
134e1a1bba4SJustin T. Gibbs  * Allocate a device specific dma_tag encapsulating the constraints of
135e1a1bba4SJustin T. Gibbs  * the parent tag in addition to other restrictions specified:
136e1a1bba4SJustin T. Gibbs  *
1378fddb21eSJustin T. Gibbs  *	alignment:	alignment for segments.
138e1a1bba4SJustin T. Gibbs  *	boundary:	Boundary that segments cannot cross.
139e1a1bba4SJustin T. Gibbs  *	lowaddr:	Low restricted address that cannot appear in a mapping.
140e1a1bba4SJustin T. Gibbs  *	highaddr:	High restricted address that cannot appear in a mapping.
141e1a1bba4SJustin T. Gibbs  *	filtfunc:	An optional function to further test if an address
142e1a1bba4SJustin T. Gibbs  *			within the range of lowaddr and highaddr cannot appear
143e1a1bba4SJustin T. Gibbs  *			in a mapping.
144e1a1bba4SJustin T. Gibbs  *	filtfuncarg:	An argument that will be passed to filtfunc in addition
145e1a1bba4SJustin T. Gibbs  *			to the address to test.
1468fddb21eSJustin T. Gibbs  *	maxsize:	Maximum mapping size supported by this tag.
1478fddb21eSJustin T. Gibbs  *	nsegments:	Number of discontinuities allowed in maps.
1488fddb21eSJustin T. Gibbs  *	maxsegsz:	Maximum size of a segment in the map.
149e1a1bba4SJustin T. Gibbs  *	flags:		Bus DMA flags.
150e1a1bba4SJustin T. Gibbs  *	dmat:		A pointer to set to a valid dma tag should the return
151e1a1bba4SJustin T. Gibbs  *			value of this function indicate success.
152e1a1bba4SJustin T. Gibbs  */
1538fddb21eSJustin T. Gibbs /* XXX Should probably allow specification of alignment */
15490f76f2dSAlexander Langer int bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
1558fddb21eSJustin T. Gibbs 		       bus_size_t boundary, bus_addr_t lowaddr,
1568fddb21eSJustin T. Gibbs 		       bus_addr_t highaddr, bus_dma_filter_t *filtfunc,
1578fddb21eSJustin T. Gibbs 		       void *filtfuncarg, bus_size_t maxsize, int nsegments,
1588fddb21eSJustin T. Gibbs 		       bus_size_t maxsegsz, int flags, bus_dma_tag_t *dmat);
159e1a1bba4SJustin T. Gibbs 
160e1a1bba4SJustin T. Gibbs int bus_dma_tag_destroy(bus_dma_tag_t dmat);
161e1a1bba4SJustin T. Gibbs 
162e1a1bba4SJustin T. Gibbs /*
163e1a1bba4SJustin T. Gibbs  * Allocate a handle for mapping from kva/uva/physical
164e1a1bba4SJustin T. Gibbs  * address space into bus device space.
165e1a1bba4SJustin T. Gibbs  */
166e1a1bba4SJustin T. Gibbs int bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
167e1a1bba4SJustin T. Gibbs 
168e1a1bba4SJustin T. Gibbs /*
169e1a1bba4SJustin T. Gibbs  * Destroy  a handle for mapping from kva/uva/physical
170e1a1bba4SJustin T. Gibbs  * address space into bus device space.
171e1a1bba4SJustin T. Gibbs  */
172e1a1bba4SJustin T. Gibbs int bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
173e1a1bba4SJustin T. Gibbs 
174e1a1bba4SJustin T. Gibbs /*
1758fddb21eSJustin T. Gibbs  * Allocate a piece of memory that can be efficiently mapped into
1768fddb21eSJustin T. Gibbs  * bus device space based on the constraints lited in the dma tag.
1778fddb21eSJustin T. Gibbs  * A dmamap to for use with dmamap_load is also allocated.
1788fddb21eSJustin T. Gibbs  */
1795193a346SScott Long int bus_dmamem_alloc_size(bus_dma_tag_t dmat, void** vaddr, int flags,
1805193a346SScott Long 			  bus_dmamap_t *mapp, bus_size_t size);
1815193a346SScott Long 
1828fddb21eSJustin T. Gibbs int bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
1838fddb21eSJustin T. Gibbs 		     bus_dmamap_t *mapp);
1848fddb21eSJustin T. Gibbs 
1858fddb21eSJustin T. Gibbs /*
1868fddb21eSJustin T. Gibbs  * Free a piece of memory and it's allociated dmamap, that was allocated
1878fddb21eSJustin T. Gibbs  * via bus_dmamem_alloc.
1888fddb21eSJustin T. Gibbs  */
1895193a346SScott Long void bus_dmamem_free_size(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map,
1905193a346SScott Long 			  bus_size_t size);
1915193a346SScott Long 
1928fddb21eSJustin T. Gibbs void bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
1938fddb21eSJustin T. Gibbs 
1948fddb21eSJustin T. Gibbs /*
195e1a1bba4SJustin T. Gibbs  * A function that processes a successfully loaded dma map or an error
196e1a1bba4SJustin T. Gibbs  * from a delayed load map.
197e1a1bba4SJustin T. Gibbs  */
198e1a1bba4SJustin T. Gibbs typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
199e1a1bba4SJustin T. Gibbs 
200e1a1bba4SJustin T. Gibbs /*
201e1a1bba4SJustin T. Gibbs  * Map the buffer buf into bus space using the dmamap map.
202e1a1bba4SJustin T. Gibbs  */
203e1a1bba4SJustin T. Gibbs int bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
204e1a1bba4SJustin T. Gibbs 		    bus_size_t buflen, bus_dmamap_callback_t *callback,
205e1a1bba4SJustin T. Gibbs 		    void *callback_arg, int flags);
206e1a1bba4SJustin T. Gibbs 
207e1a1bba4SJustin T. Gibbs /*
20814c17bd2SSam Leffler  * Like bus_dmamap_callback but includes map size in bytes.  This is
20914c17bd2SSam Leffler  * defined as a separate interface to maintain compatiiblity for users
21014c17bd2SSam Leffler  * of bus_dmamap_callback_t--at some point these interfaces should be merged.
21114c17bd2SSam Leffler  */
21214c17bd2SSam Leffler typedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
21314c17bd2SSam Leffler /*
21414c17bd2SSam Leffler  * Like bus_dmamap_load but for mbufs.  Note the use of the
21514c17bd2SSam Leffler  * bus_dmamap_callback2_t interface.
21614c17bd2SSam Leffler  */
21714c17bd2SSam Leffler int bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
21814c17bd2SSam Leffler 			 struct mbuf *mbuf,
21914c17bd2SSam Leffler 			 bus_dmamap_callback2_t *callback, void *callback_arg,
22014c17bd2SSam Leffler 			 int flags);
22114c17bd2SSam Leffler /*
22214c17bd2SSam Leffler  * Like bus_dmamap_load but for uios.  Note the use of the
22314c17bd2SSam Leffler  * bus_dmamap_callback2_t interface.
22414c17bd2SSam Leffler  */
22514c17bd2SSam Leffler int bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
22614c17bd2SSam Leffler 			struct uio *ui,
22714c17bd2SSam Leffler 			bus_dmamap_callback2_t *callback, void *callback_arg,
22814c17bd2SSam Leffler 			int flags);
22914c17bd2SSam Leffler 
23014c17bd2SSam Leffler /*
231e1a1bba4SJustin T. Gibbs  * Perform a syncronization operation on the given map.
232e1a1bba4SJustin T. Gibbs  */
233141bacb0SMaxime Henrion void _bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, int);
234e1a1bba4SJustin T. Gibbs #define bus_dmamap_sync(dmat, dmamap, op) 		\
235e1a1bba4SJustin T. Gibbs 	if ((dmamap) != NULL)				\
236e1a1bba4SJustin T. Gibbs 		_bus_dmamap_sync(dmat, dmamap, op)
237e1a1bba4SJustin T. Gibbs 
238e1a1bba4SJustin T. Gibbs /*
239e1a1bba4SJustin T. Gibbs  * Release the mapping held by map.
240e1a1bba4SJustin T. Gibbs  */
241e1a1bba4SJustin T. Gibbs void _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map);
242e1a1bba4SJustin T. Gibbs #define bus_dmamap_unload(dmat, dmamap) 		\
243e1a1bba4SJustin T. Gibbs 	if ((dmamap) != NULL)				\
244e1a1bba4SJustin T. Gibbs 		_bus_dmamap_unload(dmat, dmamap)
245e1a1bba4SJustin T. Gibbs 
246e9b193dcSPeter Wemm #endif /* _AMD64_BUS_DMA_H_ */
247