bus_dma.h revision 248896
1148330Snetchild/*	$NetBSD: bus.h,v 1.12 1997/10/01 08:25:15 fvdl Exp $	*/
2148330Snetchild
3148330Snetchild/*-
4148330Snetchild * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5148330Snetchild * All rights reserved.
6148330Snetchild *
7148330Snetchild * This code is derived from software contributed to The NetBSD Foundation
8148330Snetchild * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9148330Snetchild * NASA Ames Research Center.
10148330Snetchild *
11148330Snetchild * Redistribution and use in source and binary forms, with or without
12148330Snetchild * modification, are permitted provided that the following conditions
13148330Snetchild * are met:
14148543Snetchild * 1. Redistributions of source code must retain the above copyright
15148543Snetchild *    notice, this list of conditions and the following disclaimer.
16148330Snetchild * 2. Redistributions in binary form must reproduce the above copyright
17194860Sthompsa *    notice, this list of conditions and the following disclaimer in the
18195096Santoine *    documentation and/or other materials provided with the distribution.
19195096Santoine * 3. All advertising materials mentioning features or use of this software
20195096Santoine *    must display the following acknowledgement:
21195096Santoine *	This product includes software developed by the NetBSD
22195096Santoine *	Foundation, Inc. and its contributors.
23195096Santoine * 4. Neither the name of The NetBSD Foundation nor the names of its
24195096Santoine *    contributors may be used to endorse or promote products derived
25195096Santoine *    from this software without specific prior written permission.
26195096Santoine *
27195096Santoine * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28195096Santoine * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29195096Santoine * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30195096Santoine * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31195096Santoine * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32195096Santoine * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33195096Santoine * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34195096Santoine * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35195096Santoine * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36195096Santoine * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37195096Santoine * POSSIBILITY OF SUCH DAMAGE.
38195096Santoine */
39195096Santoine
40195096Santoine/*-
41195096Santoine * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
42195096Santoine * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
43195096Santoine *
44195096Santoine * Redistribution and use in source and binary forms, with or without
45195096Santoine * modification, are permitted provided that the following conditions
46195096Santoine * are met:
47195096Santoine * 1. Redistributions of source code must retain the above copyright
48195096Santoine *    notice, this list of conditions and the following disclaimer.
49195096Santoine * 2. Redistributions in binary form must reproduce the above copyright
50195096Santoine *    notice, this list of conditions and the following disclaimer in the
51195096Santoine *    documentation and/or other materials provided with the distribution.
52195096Santoine * 3. All advertising materials mentioning features or use of this software
53195096Santoine *    must display the following acknowledgement:
54195096Santoine *      This product includes software developed by Christopher G. Demetriou
55195096Santoine *	for the NetBSD Project.
56195096Santoine * 4. The name of the author may not be used to endorse or promote products
57195096Santoine *    derived from this software without specific prior written permission
58195096Santoine *
59195096Santoine * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
60195096Santoine * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
61195096Santoine * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
62195096Santoine * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
63195096Santoine * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
64195096Santoine * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
65195096Santoine * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
66195096Santoine * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
67195096Santoine * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
68195096Santoine * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69195096Santoine */
70195096Santoine/* $FreeBSD: head/sys/sys/bus_dma.h 248896 2013-03-29 16:26:25Z jimharris $ */
71195096Santoine
72195096Santoine#ifndef _BUS_DMA_H_
73195096Santoine#define _BUS_DMA_H_
74195096Santoine
75195096Santoine#include <sys/_bus_dma.h>
76195096Santoine
77193513Sed/*
78193513Sed * Machine independent interface for mapping physical addresses to peripheral
79193308Sed * bus 'physical' addresses, and assisting with DMA operations.
80193308Sed *
81193308Sed * XXX This file is always included from <machine/bus_dma.h> and should not
82195096Santoine *     (yet) be included directly.
83195096Santoine */
84193113Sdougb
85193113Sdougb/*
86192926Sed * Flags used in various bus DMA methods.
87192926Sed */
88192926Sed#define	BUS_DMA_WAITOK		0x00	/* safe to sleep (pseudo-flag) */
89192901Sthompsa#define	BUS_DMA_NOWAIT		0x01	/* not safe to sleep */
90192901Sthompsa#define	BUS_DMA_ALLOCNOW	0x02	/* perform resource allocation now */
91192901Sthompsa#define	BUS_DMA_COHERENT	0x04	/* hint: map memory in a coherent way */
92192901Sthompsa#define	BUS_DMA_ZERO		0x08	/* allocate zero'ed memory */
93192901Sthompsa#define	BUS_DMA_BUS1		0x10	/* placeholders for bus functions... */
94192901Sthompsa#define	BUS_DMA_BUS2		0x20
95192901Sthompsa#define	BUS_DMA_BUS3		0x40
96192901Sthompsa#define	BUS_DMA_BUS4		0x80
97192901Sthompsa
98192901Sthompsa/*
99192901Sthompsa * The following two flags are non-standard or specific to only certain
100192901Sthompsa * architectures
101192901Sthompsa */
102192901Sthompsa#define	BUS_DMA_NOWRITE		0x100
103192901Sthompsa#define	BUS_DMA_NOCACHE		0x200
104192901Sthompsa
105192901Sthompsa/*
106192901Sthompsa * The following flag is a DMA tag hint that the page offset of the
107192901Sthompsa * loaded kernel virtual address must be preserved in the first
108192901Sthompsa * physical segment address, when the KVA is loaded into DMA.
109192901Sthompsa */
110192901Sthompsa#define	BUS_DMA_KEEP_PG_OFFSET	0x400
111192901Sthompsa
112192901Sthompsa#define	BUS_DMA_LOAD_MBUF	0x800
113192901Sthompsa
114192901Sthompsa/* Forwards needed by prototypes below. */
115192901Sthompsaunion ccb;
116192901Sthompsastruct bio;
117192901Sthompsastruct mbuf;
118192901Sthompsastruct memdesc;
119192901Sthompsastruct pmap;
120192901Sthompsastruct uio;
121192901Sthompsa
122192916Sdougb/*
123195096Santoine * Operations performed by bus_dmamap_sync().
124192916Sdougb */
125192580Srwatson#define	BUS_DMASYNC_PREREAD	1
126192580Srwatson#define	BUS_DMASYNC_POSTREAD	2
127192580Srwatson#define	BUS_DMASYNC_PREWRITE	4
128192580Srwatson#define	BUS_DMASYNC_POSTWRITE	8
129192650Santoine
130192580Srwatson/*
131192650Santoine *	bus_dma_segment_t
132192650Santoine *
133192650Santoine *	Describes a single contiguous DMA transaction.  Values
134191250Santoine *	are suitable for programming into DMA registers.
135191250Santoine */
136191250Santoinetypedef struct bus_dma_segment {
137191250Santoine	bus_addr_t	ds_addr;	/* DMA address */
138191250Santoine	bus_size_t	ds_len;		/* length of transfer */
139191250Santoine} bus_dma_segment_t;
140191250Santoine
141191250Santoine/*
142191250Santoine * A function that returns 1 if the address cannot be accessed by
143191250Santoine * a device and 0 if it can be.
144191250Santoine */
145191250Santoinetypedef int bus_dma_filter_t(void *, bus_addr_t);
146191250Santoine
147191250Santoine/*
148191250Santoine * Generic helper function for manipulating mutexes.
149191146Smaxim */
150191146Smaximvoid busdma_lock_mutex(void *arg, bus_dma_lock_op_t op);
151191146Smaxim
152191146Smaxim/*
153191146Smaxim * Allocate a device specific dma_tag encapsulating the constraints of
154191146Smaxim * the parent tag in addition to other restrictions specified:
155190894Sdanger *
156190905Sdanger *	alignment:	Alignment for segments.
157191250Santoine *	boundary:	Boundary that segments cannot cross.
158191250Santoine *	lowaddr:	Low restricted address that cannot appear in a mapping.
159190751Sed *	highaddr:	High restricted address that cannot appear in a mapping.
160190864Sru *	filtfunc:	An optional function to further test if an address
161190864Sru *			within the range of lowaddr and highaddr cannot appear
162190751Sed *			in a mapping.
163190751Sed *	filtfuncarg:	An argument that will be passed to filtfunc in addition
164190751Sed *			to the address to test.
165190751Sed *	maxsize:	Maximum mapping size supported by this tag.
166190751Sed *	nsegments:	Number of discontinuities allowed in maps.
167190751Sed *	maxsegsz:	Maximum size of a segment in the map.
168190864Sru *	flags:		Bus DMA flags.
169190751Sed *	lockfunc:	An optional function to handle driver-defined lock
170190751Sed *			operations.
171190751Sed *	lockfuncarg:	An argument that will be passed to lockfunc in addition
172190751Sed *			to the lock operation.
173190751Sed *	dmat:		A pointer to set to a valid dma tag should the return
174190751Sed *			value of this function indicate success.
175190751Sed */
176190751Sed/* XXX Should probably allow specification of alignment */
177190751Sedint bus_dma_tag_create(bus_dma_tag_t parent, bus_size_t alignment,
178190751Sed		       bus_addr_t boundary, bus_addr_t lowaddr,
179190864Sru		       bus_addr_t highaddr, bus_dma_filter_t *filtfunc,
180190751Sed		       void *filtfuncarg, bus_size_t maxsize, int nsegments,
181190864Sru		       bus_size_t maxsegsz, int flags, bus_dma_lock_t *lockfunc,
182190772Sru		       void *lockfuncarg, bus_dma_tag_t *dmat);
183190772Sru
184190772Sruint bus_dma_tag_destroy(bus_dma_tag_t dmat);
185190772Sru
186190772Sru/*
187190100Sthompsa * A function that processes a successfully loaded dma map or an error
188190100Sthompsa * from a delayed load map.
189189977Sbrueffer */
190189977Sbrueffertypedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
191189585Sthompsa
192189585Sthompsa/*
193189607Sthompsa * Like bus_dmamap_callback but includes map size in bytes.  This is
194189607Sthompsa * defined as a separate interface to maintain compatibility for users
195189607Sthompsa * of bus_dmamap_callback_t--at some point these interfaces should be merged.
196189585Sthompsa */
197191250Santoinetypedef void bus_dmamap_callback2_t(void *, bus_dma_segment_t *, int, bus_size_t, int);
198190772Sru
199190772Sru/*
200190772Sru * Map the buffer buf into bus space using the dmamap map.
201190772Sru */
202190772Sruint bus_dmamap_load(bus_dma_tag_t dmat, bus_dmamap_t map, void *buf,
203190772Sru		    bus_size_t buflen, bus_dmamap_callback_t *callback,
204189092Sed		    void *callback_arg, int flags);
205189092Sed
206190772Sru/*
207190772Sru * Like bus_dmamap_load but for mbufs.  Note the use of the
208190772Sru * bus_dmamap_callback2_t interface.
209188948Sthompsa */
210189000Sthompsaint bus_dmamap_load_mbuf(bus_dma_tag_t dmat, bus_dmamap_t map,
211189000Sthompsa			 struct mbuf *mbuf,
212189000Sthompsa			 bus_dmamap_callback2_t *callback, void *callback_arg,
213189000Sthompsa			 int flags);
214189000Sthompsa
215189000Sthompsaint bus_dmamap_load_mbuf_sg(bus_dma_tag_t dmat, bus_dmamap_t map,
216189000Sthompsa			    struct mbuf *mbuf, bus_dma_segment_t *segs,
217189000Sthompsa			    int *nsegs, int flags);
218189000Sthompsa
219189000Sthompsa/*
220189000Sthompsa * Like bus_dmamap_load but for uios.  Note the use of the
221189000Sthompsa * bus_dmamap_callback2_t interface.
222189000Sthompsa */
223189000Sthompsaint bus_dmamap_load_uio(bus_dma_tag_t dmat, bus_dmamap_t map,
224188948Sthompsa			struct uio *ui,
225188948Sthompsa			bus_dmamap_callback2_t *callback, void *callback_arg,
226188948Sthompsa			int flags);
227188948Sthompsa
228188948Sthompsa/*
229188948Sthompsa * Like bus_dmamap_load but for cam control blocks.
230188948Sthompsa */
231188948Sthompsaint bus_dmamap_load_ccb(bus_dma_tag_t dmat, bus_dmamap_t map, union ccb *ccb,
232188948Sthompsa			bus_dmamap_callback_t *callback, void *callback_arg,
233188948Sthompsa			int flags);
234188948Sthompsa
235188948Sthompsa/*
236188948Sthompsa * Like bus_dmamap_load but for bios.
237188948Sthompsa */
238188948Sthompsaint bus_dmamap_load_bio(bus_dma_tag_t dmat, bus_dmamap_t map, struct bio *bio,
239188948Sthompsa			bus_dmamap_callback_t *callback, void *callback_arg,
240188948Sthompsa			int flags);
241191250Santoine
242191250Santoine/*
243188948Sthompsa * Loads any memory descriptor.
244188948Sthompsa */
245188948Sthompsaint bus_dmamap_load_mem(bus_dma_tag_t dmat, bus_dmamap_t map,
246188948Sthompsa			struct memdesc *mem, bus_dmamap_callback_t *callback,
247188948Sthompsa			void *callback_arg, int flags);
248188948Sthompsa
249188948Sthompsa/*
250188948Sthompsa * XXX sparc64 uses the same interface, but a much different implementation.
251188948Sthompsa *     <machine/bus_dma.h> for the sparc64 arch contains the equivalent
252188948Sthompsa *     declarations.
253188948Sthompsa */
254188948Sthompsa#if !defined(__sparc64__)
255188948Sthompsa
256188948Sthompsa/*
257188948Sthompsa * Allocate a handle for mapping from kva/uva/physical
258188948Sthompsa * address space into bus device space.
259188948Sthompsa */
260188948Sthompsaint bus_dmamap_create(bus_dma_tag_t dmat, int flags, bus_dmamap_t *mapp);
261188948Sthompsa
262188948Sthompsa/*
263188948Sthompsa * Destroy a handle for mapping from kva/uva/physical
264188948Sthompsa * address space into bus device space.
265188948Sthompsa */
266188948Sthompsaint bus_dmamap_destroy(bus_dma_tag_t dmat, bus_dmamap_t map);
267188948Sthompsa
268188652Sed/*
269188652Sed * Allocate a piece of memory that can be efficiently mapped into
270188652Sed * bus device space based on the constraints listed in the dma tag.
271188652Sed * A dmamap to for use with dmamap_load is also allocated.
272188102Sgabor */
273188102Sgaborint bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags,
274187694Santoine		     bus_dmamap_t *mapp);
275187694Santoine
276187694Santoine/*
277187694Santoine * Free a piece of memory and its allocated dmamap, that was allocated
278187694Santoine * via bus_dmamem_alloc.
279186716Santoine */
280186716Santoinevoid bus_dmamem_free(bus_dma_tag_t dmat, void *vaddr, bus_dmamap_t map);
281186437Sbz
282186437Sbz/*
283185472Santoine * Perform a synchronization operation on the given map.
284185472Santoine */
285185472Santoinevoid _bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_dmasync_op_t);
286185472Santoine#define bus_dmamap_sync(dmat, dmamap, op) 			\
287185472Santoine	do {							\
288183442Sed		if ((dmamap) != NULL)				\
289183442Sed			_bus_dmamap_sync(dmat, dmamap, op);	\
290183442Sed	} while (0)
291183442Sed
292183442Sed/*
293183442Sed * Release the mapping held by map.
294183113Sattilio */
295183235Santoinevoid _bus_dmamap_unload(bus_dma_tag_t dmat, bus_dmamap_t map);
296183235Santoine#define bus_dmamap_unload(dmat, dmamap) 			\
297183026Santoine	do {							\
298183026Santoine		if ((dmamap) != NULL)				\
299182105Sed			_bus_dmamap_unload(dmat, dmamap);	\
300182105Sed	} while (0)
301182518Santoine
302182518Santoine/*
303182518Santoine * The following functions define the interface between the MD and MI
304182518Santoine * busdma layers.  These are not intended for consumption by driver
305182518Santoine * software.
306182518Santoine */
307182518Santoinevoid __bus_dmamap_waitok(bus_dma_tag_t dmat, bus_dmamap_t map,
308182518Santoine			 struct memdesc *mem,
309182518Santoine    			 bus_dmamap_callback_t *callback,
310182518Santoine			 void *callback_arg);
311182518Santoine
312182518Santoine#define	_bus_dmamap_waitok(dmat, map, mem, callback, callback_arg)	\
313182518Santoine	do {								\
314182518Santoine		if ((map) != NULL)					\
315182518Santoine			__bus_dmamap_waitok(dmat, map, mem, callback,	\
316182518Santoine			    callback_arg);				\
317182518Santoine	} while (0);
318182518Santoine
319181905Sedint _bus_dmamap_load_buffer(bus_dma_tag_t dmat, bus_dmamap_t map,
320181905Sed			    void *buf, bus_size_t buflen, struct pmap *pmap,
321181905Sed			    int flags, bus_dma_segment_t *segs, int *segp);
322180800Sed
323180800Sedint _bus_dmamap_load_phys(bus_dma_tag_t dmat, bus_dmamap_t map,
324180614Smarcel			  vm_paddr_t paddr, bus_size_t buflen,
325180614Smarcel			  int flags, bus_dma_segment_t *segs, int *segp);
326180614Smarcel
327180614Smarcelbus_dma_segment_t *_bus_dmamap_complete(bus_dma_tag_t dmat,
328180614Smarcel			   		bus_dmamap_t map,
329180614Smarcel					bus_dma_segment_t *segs,
330180331Smarcel					int nsegs, int error);
331180331Smarcel
332180331Smarcel#endif /* __sparc64__ */
333180331Smarcel
334180331Smarcel#endif /* _BUS_DMA_H_ */
335180267Sjhb