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