132517Sgibbs/*	$NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $	*/
232517Sgibbs
332517Sgibbs/*-
432517Sgibbs * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
532517Sgibbs * All rights reserved.
632517Sgibbs *
732517Sgibbs * This code is derived from software contributed to The NetBSD Foundation
832517Sgibbs * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
932517Sgibbs * NASA Ames Research Center.
1032517Sgibbs *
1132517Sgibbs * Redistribution and use in source and binary forms, with or without
1232517Sgibbs * modification, are permitted provided that the following conditions
1332517Sgibbs * are met:
1432517Sgibbs * 1. Redistributions of source code must retain the above copyright
1532517Sgibbs *    notice, this list of conditions and the following disclaimer.
1632517Sgibbs * 2. Redistributions in binary form must reproduce the above copyright
1732517Sgibbs *    notice, this list of conditions and the following disclaimer in the
1832517Sgibbs *    documentation and/or other materials provided with the distribution.
1932517Sgibbs *
2032517Sgibbs * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2132517Sgibbs * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2232517Sgibbs * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2332517Sgibbs * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2432517Sgibbs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2532517Sgibbs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2632517Sgibbs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2732517Sgibbs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2832517Sgibbs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2932517Sgibbs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3032517Sgibbs * POSSIBILITY OF SUCH DAMAGE.
3132517Sgibbs */
3232517Sgibbs
33139790Simp/*-
3432517Sgibbs * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
3532517Sgibbs * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
3632517Sgibbs *
3732517Sgibbs * Redistribution and use in source and binary forms, with or without
3832517Sgibbs * modification, are permitted provided that the following conditions
3932517Sgibbs * are met:
4032517Sgibbs * 1. Redistributions of source code must retain the above copyright
4132517Sgibbs *    notice, this list of conditions and the following disclaimer.
4232517Sgibbs * 2. Redistributions in binary form must reproduce the above copyright
4332517Sgibbs *    notice, this list of conditions and the following disclaimer in the
4432517Sgibbs *    documentation and/or other materials provided with the distribution.
4532517Sgibbs * 3. All advertising materials mentioning features or use of this software
4632517Sgibbs *    must display the following acknowledgement:
4732517Sgibbs *      This product includes software developed by Christopher G. Demetriou
4832517Sgibbs *	for the NetBSD Project.
4932517Sgibbs * 4. The name of the author may not be used to endorse or promote products
5032517Sgibbs *    derived from this software without specific prior written permission
5132517Sgibbs *
5232517Sgibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
5332517Sgibbs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
5432517Sgibbs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5532517Sgibbs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
5632517Sgibbs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5732517Sgibbs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5832517Sgibbs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5932517Sgibbs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
6032517Sgibbs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
6132517Sgibbs * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
6232517Sgibbs */
6350477Speter/* $FreeBSD$ */
6432517Sgibbs
65143598Sscottl#ifndef _BUS_DMA_H_
66143598Sscottl#define _BUS_DMA_H_
6732517Sgibbs
68161927Sjmg#include <sys/_bus_dma.h>
69161927Sjmg
7032517Sgibbs/*
71143598Sscottl * Machine independent interface for mapping physical addresses to peripheral
72143598Sscottl * bus 'physical' addresses, and assisting with DMA operations.
73143598Sscottl *
74143598Sscottl * XXX This file is always included from <machine/bus_dma.h> and should not
75143598Sscottl *     (yet) be included directly.
76143598Sscottl */
77143598Sscottl
78143598Sscottl/*
7932517Sgibbs * Flags used in various bus DMA methods.
8032517Sgibbs */
8132517Sgibbs#define	BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
8232517Sgibbs#define	BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
8332517Sgibbs#define	BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
84115416Shmp#define	BUS_DMA_COHERENT	0x04	/* hint: map memory in a coherent way */
85118081Smux#define	BUS_DMA_ZERO		0x08	/* allocate zero'ed memory */
8632517Sgibbs#define	BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
8732517Sgibbs#define	BUS_DMA_BUS2		0x20
8832517Sgibbs#define	BUS_DMA_BUS3		0x40
8932517Sgibbs#define	BUS_DMA_BUS4		0x80
9032517Sgibbs
91143598Sscottl/*
92143598Sscottl * The following two flags are non-standard or specific to only certain
93143598Sscottl * architectures
94143598Sscottl */
95143598Sscottl#define	BUS_DMA_NOWRITE		0x100
96143598Sscottl#define	BUS_DMA_NOCACHE		0x200
97143598Sscottl
98188350Simp/*
99188350Simp * The following flag is a DMA tag hint that the page offset of the
100188350Simp * loaded kernel virtual address must be preserved in the first
101188350Simp * physical segment address, when the KVA is loaded into DMA.
102188350Simp */
103188350Simp#define	BUS_DMA_KEEP_PG_OFFSET	0x400
104188350Simp
105246713Skib#define	BUS_DMA_LOAD_MBUF	0x800
106246713Skib
10732517Sgibbs/* Forwards needed by prototypes below. */
108246713Skibunion ccb;
109246713Skibstruct bio;
11032517Sgibbsstruct mbuf;
111246713Skibstruct memdesc;
112246713Skibstruct pmap;
11332517Sgibbsstruct uio;
11432517Sgibbs
11532517Sgibbs/*
116113347Smux * Operations performed by bus_dmamap_sync().
11732517Sgibbs */
118113347Smux#define	BUS_DMASYNC_PREREAD	1
119113347Smux#define	BUS_DMASYNC_POSTREAD	2
120113347Smux#define	BUS_DMASYNC_PREWRITE	4
121113347Smux#define	BUS_DMASYNC_POSTWRITE	8
12232517Sgibbs
12332517Sgibbs/*
12432517Sgibbs *	bus_dma_segment_t
12532517Sgibbs *
12632517Sgibbs *	Describes a single contiguous DMA transaction.  Values
12732517Sgibbs *	are suitable for programming into DMA registers.
12832517Sgibbs */
12932517Sgibbstypedef struct bus_dma_segment {
13032517Sgibbs	bus_addr_t	ds_addr;	/* DMA address */
13132517Sgibbs	bus_size_t	ds_len;		/* length of transfer */
13232517Sgibbs} bus_dma_segment_t;
13332517Sgibbs
13432517Sgibbs/*
13532517Sgibbs * A function that returns 1 if the address cannot be accessed by
13632517Sgibbs * a device and 0 if it can be.
13732517Sgibbs */
13832517Sgibbstypedef int bus_dma_filter_t(void *, bus_addr_t);
13932517Sgibbs
14032517Sgibbs/*
141143598Sscottl * Generic helper function for manipulating mutexes.
142143598Sscottl */
143143598Sscottlvoid busdma_lock_mutex(void *arg, bus_dma_lock_op_t op);
144143598Sscottl
145143598Sscottl/*
14632517Sgibbs * Allocate a device specific dma_tag encapsulating the constraints of
14732517Sgibbs * the parent tag in addition to other restrictions specified:
14832517Sgibbs *
149122154Sanholt *	alignment:	Alignment for segments.
15032517Sgibbs *	boundary:	Boundary that segments cannot cross.
15132517Sgibbs *	lowaddr:	Low restricted address that cannot appear in a mapping.
15232517Sgibbs *	highaddr:	High restricted address that cannot appear in a mapping.
15332517Sgibbs *	filtfunc:	An optional function to further test if an address
15432517Sgibbs *			within the range of lowaddr and highaddr cannot appear
15532517Sgibbs *			in a mapping.
15632517Sgibbs *	filtfuncarg:	An argument that will be passed to filtfunc in addition
15732517Sgibbs *			to the address to test.
15835767Sgibbs *	maxsize:	Maximum mapping size supported by this tag.
15935767Sgibbs *	nsegments:	Number of discontinuities allowed in maps.
16035767Sgibbs *	maxsegsz:	Maximum size of a segment in the map.
16132517Sgibbs *	flags:		Bus DMA flags.
162122266Sscottl *	lockfunc:	An optional function to handle driver-defined lock
163122266Sscottl *			operations.
164122266Sscottl *	lockfuncarg:	An argument that will be passed to lockfunc in addition
165122266Sscottl *			to the lock operation.
16632517Sgibbs *	dmat:		A pointer to set to a valid dma tag should the return
16732517Sgibbs *			value of this function indicate success.
16832517Sgibbs */
16935767Sgibbs/* XXX Should probably allow specification of alignment */
17078353Salexint bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
171232356Sjhb		       bus_addr_t boundary, bus_addr_t lowaddr,
17235767Sgibbs		       bus_addr_t highaddr, bus_dma_filter_t *filtfunc,
17335767Sgibbs		       void *filtfuncarg, bus_size_t maxsize, int nsegments,
174117126Sscottl		       bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
175117126Sscottl		       void *lockfuncarg, bus_dma_tag_t *dmat);
17632517Sgibbs
17732517Sgibbsint bus_dma_tag_destroy(bus_dma_tag_t dmat);
17832517Sgibbs
17932517Sgibbs/*
180143598Sscottl * A function that processes a successfully loaded dma map or an error
181143598Sscottl * from a delayed load map.
182143598Sscottl */
183143598Sscottltypedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
184143598Sscottl
185143598Sscottl/*
186143598Sscottl * Like bus_dmamap_callback but includes map size in bytes.  This is
187143598Sscottl * defined as a separate interface to maintain compatibility for users
188143598Sscottl * of bus_dmamap_callback_t--at some point these interfaces should be merged.
189143598Sscottl */
190143598Sscottltypedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
191143598Sscottl
192143598Sscottl/*
193246713Skib * Map the buffer buf into bus space using the dmamap map.
194246713Skib */
195246713Skibint bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
196246713Skib		    bus_size_t buflen, bus_dmamap_callback_t *callback,
197246713Skib		    void *callback_arg, int flags);
198246713Skib
199246713Skib/*
200246713Skib * Like bus_dmamap_load but for mbufs.  Note the use of the
201246713Skib * bus_dmamap_callback2_t interface.
202246713Skib */
203246713Skibint bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
204246713Skib			 struct mbuf *mbuf,
205246713Skib			 bus_dmamap_callback2_t *callback, void *callback_arg,
206246713Skib			 int flags);
207246713Skib
208246713Skibint bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
209246713Skib			    struct mbuf *mbuf, bus_dma_segment_t *segs,
210246713Skib			    int *nsegs, int flags);
211246713Skib
212246713Skib/*
213246713Skib * Like bus_dmamap_load but for uios.  Note the use of the
214246713Skib * bus_dmamap_callback2_t interface.
215246713Skib */
216246713Skibint bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
217246713Skib			struct uio *ui,
218246713Skib			bus_dmamap_callback2_t *callback, void *callback_arg,
219246713Skib			int flags);
220246713Skib
221246713Skib/*
222246713Skib * Like bus_dmamap_load but for cam control blocks.
223246713Skib */
224246713Skibint bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
225246713Skib			bus_dmamap_callback_t *callback, void *callback_arg,
226246713Skib			int flags);
227246713Skib
228246713Skib/*
229248896Sjimharris * Like bus_dmamap_load but for bios.
230248896Sjimharris */
231248896Sjimharrisint bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio,
232248896Sjimharris			bus_dmamap_callback_t *callback, void *callback_arg,
233248896Sjimharris			int flags);
234248896Sjimharris
235248896Sjimharris/*
236246713Skib * Loads any memory descriptor.
237246713Skib */
238246713Skibint bus_dmamap_load_mem(bus_dma_tag_t dmat, bus_dmamap_t map,
239246713Skib			struct memdesc *mem, bus_dmamap_callback_t *callback,
240246713Skib			void *callback_arg, int flags);
241246713Skib
242246713Skib/*
243257228Skib * Placeholder for use by busdma implementations which do not benefit
244257228Skib * from optimized procedure to load an array of vm_page_t.  Falls back
245257228Skib * to do _bus_dmamap_load_phys() in loop.
246257228Skib */
247257228Skibint bus_dmamap_load_ma_triv(bus_dma_tag_t dmat, bus_dmamap_t map,
248257228Skib    struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
249257228Skib    bus_dma_segment_t *segs, int *segp);
250257228Skib
251257228Skib/*
252143598Sscottl * XXX sparc64 uses the same interface, but a much different implementation.
253143598Sscottl *     <machine/bus_dma.h> for the sparc64 arch contains the equivalent
254143598Sscottl *     declarations.
255143598Sscottl */
256143598Sscottl#if !defined(__sparc64__)
257143598Sscottl
258143598Sscottl/*
25932517Sgibbs * Allocate a handle for mapping from kva/uva/physical
26032517Sgibbs * address space into bus device space.
26132517Sgibbs */
26232517Sgibbsint bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
26332517Sgibbs
26432517Sgibbs/*
265122154Sanholt * Destroy a handle for mapping from kva/uva/physical
26632517Sgibbs * address space into bus device space.
26732517Sgibbs */
26832517Sgibbsint bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
26932517Sgibbs
27032517Sgibbs/*
27135767Sgibbs * Allocate a piece of memory that can be efficiently mapped into
272122154Sanholt * bus device space based on the constraints listed in the dma tag.
27335767Sgibbs * A dmamap to for use with dmamap_load is also allocated.
27435767Sgibbs */
27535767Sgibbsint bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
27635767Sgibbs		     bus_dmamap_t *mapp);
27735767Sgibbs
27835767Sgibbs/*
279122154Sanholt * Free a piece of memory and its allocated dmamap, that was allocated
28035767Sgibbs * via bus_dmamem_alloc.
28135767Sgibbs */
28235767Sgibbsvoid bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
28335767Sgibbs
28435767Sgibbs/*
285285270Szbb * Perform a synchronization operation on the given map. If the map
286285270Szbb * is NULL we have a fully IO-coherent system. On every ARM architecture
287285270Szbb * there must be a memory barrier placed to ensure that all data
288285270Szbb * accesses are visible before going any further.
28932517Sgibbs */
290115343Sscottlvoid _bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_dmasync_op_t);
291285270Szbb#if defined(__arm__)
292285293Szbb	#define __BUS_DMAMAP_SYNC_DEFAULT		mb()
293285270Szbb#elif defined(__aarch64__)
294285293Szbb	#define	__BUS_DMAMAP_SYNC_DEFAULT		dmb(sy)
295285270Szbb#else
296285293Szbb	#define	__BUS_DMAMAP_SYNC_DEFAULT		do {} while (0)
297285270Szbb#endif
298143655Sjmg#define bus_dmamap_sync(dmat, dmamap, op) 			\
299143655Sjmg	do {							\
300143655Sjmg		if ((dmamap) != NULL)				\
301143655Sjmg			_bus_dmamap_sync(dmat, dmamap, op);	\
302285270Szbb		else						\
303285293Szbb			__BUS_DMAMAP_SYNC_DEFAULT;		\
304143655Sjmg	} while (0)
30532517Sgibbs
30632517Sgibbs/*
30732517Sgibbs * Release the mapping held by map.
30832517Sgibbs */
30932517Sgibbsvoid _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map);
310143655Sjmg#define bus_dmamap_unload(dmat, dmamap) 			\
311143655Sjmg	do {							\
312143655Sjmg		if ((dmamap) != NULL)				\
313143655Sjmg			_bus_dmamap_unload(dmat, dmamap);	\
314143655Sjmg	} while (0)
315143655Sjmg
316246713Skib/*
317246713Skib * The following functions define the interface between the MD and MI
318246713Skib * busdma layers.  These are not intended for consumption by driver
319246713Skib * software.
320246713Skib */
321246713Skibvoid __bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
322246713Skib			 struct memdesc *mem,
323246713Skib    			 bus_dmamap_callback_t *callback,
324246713Skib			 void *callback_arg);
325246713Skib
326246713Skib#define	_bus_dmamap_waitok(dmat, map, mem, callback, callback_arg)	\
327246713Skib	do {								\
328246713Skib		if ((map) != NULL)					\
329246713Skib			__bus_dmamap_waitok(dmat, map, mem, callback,	\
330246713Skib			    callback_arg);				\
331246713Skib	} while (0);
332246713Skib
333246713Skibint _bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map,
334246713Skib			    void *buf, bus_size_t buflen, struct pmap *pmap,
335246713Skib			    int flags, bus_dma_segment_t *segs, int *segp);
336246713Skib
337246713Skibint _bus_dmamap_load_phys(bus_dma_tag_t dmat, bus_dmamap_t map,
338246713Skib			  vm_paddr_t paddr, bus_size_t buflen,
339246713Skib			  int flags, bus_dma_segment_t *segs, int *segp);
340246713Skib
341257228Skibint _bus_dmamap_load_ma(bus_dma_tag_t dmat, bus_dmamap_t map,
342257228Skib    struct vm_page **ma, bus_size_t tlen, int ma_offs, int flags,
343257228Skib    bus_dma_segment_t *segs, int *segp);
344257228Skib
345246713Skibbus_dma_segment_t *_bus_dmamap_complete(bus_dma_tag_t dmat,
346246713Skib			   		bus_dmamap_t map,
347246713Skib					bus_dma_segment_t *segs,
348246713Skib					int nsegs, int error);
349246713Skib
350143598Sscottl#endif /* __sparc64__ */
35132517Sgibbs
352143598Sscottl#endif /* _BUS_DMA_H_ */
353