1/*	$NetBSD: bus_funcs.h,v 1.3 2023/12/07 03:46:10 thorpej Exp $	*/
2/*	NetBSD: bus.h,v 1.27 2000/03/15 16:44:50 drochner Exp 	*/
3/*	$OpenBSD: bus.h,v 1.15 1999/08/11 23:15:21 niklas Exp $	*/
4
5/*-
6 * Copyright (c) 1996, 1997, 1998, 2001 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) 1997 Per Fogelstrom.  All rights reserved.
37 * Copyright (c) 1996 Niklas Hallqvist.  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
66#ifndef _ARC_BUS_FUNCS_H_
67#define _ARC_BUS_FUNCS_H_
68#ifdef _KERNEL
69
70struct vmem;
71struct vmem_btag;
72
73/* machine dependent utility function for bus_space users */
74void	arc_bus_space_init(bus_space_tag_t, const char *,
75	    paddr_t, vaddr_t, bus_addr_t, bus_size_t);
76void	arc_bus_space_init_arena(bus_space_tag_t, struct vmem *,
77	    struct vmem_btag *, unsigned int);
78void	arc_bus_space_set_aligned_stride(bus_space_tag_t, unsigned int);
79void	arc_sparse_bus_space_init(bus_space_tag_t, const char *,
80	    paddr_t, bus_addr_t, bus_size_t);
81void	arc_large_bus_space_init(bus_space_tag_t, const char *,
82	    paddr_t, bus_addr_t, bus_size_t);
83
84/* these are provided for subclasses which override base bus_space. */
85
86int	arc_bus_space_compose_handle(bus_space_tag_t,
87	    bus_addr_t, bus_size_t, int, bus_space_handle_t *);
88int	arc_bus_space_dispose_handle(bus_space_tag_t,
89	    bus_space_handle_t, bus_size_t);
90int	arc_bus_space_paddr(bus_space_tag_t,
91	    bus_space_handle_t, paddr_t *);
92
93int	arc_sparse_bus_space_compose_handle(bus_space_tag_t,
94	    bus_addr_t, bus_size_t, int, bus_space_handle_t *);
95int	arc_sparse_bus_space_dispose_handle(bus_space_tag_t,
96	    bus_space_handle_t, bus_size_t);
97int	arc_sparse_bus_space_paddr(bus_space_tag_t,
98	    bus_space_handle_t, paddr_t *);
99
100int	arc_bus_space_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
101	    bus_space_handle_t *);
102void	arc_bus_space_unmap(bus_space_tag_t, bus_space_handle_t,
103	    bus_size_t);
104int	arc_bus_space_subregion(bus_space_tag_t, bus_space_handle_t,
105	    bus_size_t, bus_size_t, bus_space_handle_t *);
106paddr_t	arc_bus_space_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
107int	arc_bus_space_alloc(bus_space_tag_t, bus_addr_t, bus_addr_t,
108	    bus_size_t, bus_size_t, bus_size_t, int, bus_addr_t *,
109	    bus_space_handle_t *);
110#define arc_bus_space_free	arc_bus_space_unmap
111
112/*
113 *	int bus_space_compose_handle(bus_space_tag_t t, bus_addr_t addr,
114 *	    bus_size_t size, int flags, bus_space_handle_t *bshp);
115 *
116 * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
117 * Compose a bus_space handle from tag/handle/addr/size/flags.
118 * A helper function for bus_space_map()/bus_space_alloc() implementation.
119 */
120#define bus_space_compose_handle(bst, addr, size, flags, bshp)		\
121	(*(bst)->bs_compose_handle)(bst, addr, size, flags, bshp)
122
123/*
124 *	int bus_space_dispose_handle(bus_space_tag_t t, bus_addr_t addr,
125 *	    bus_space_handle_t bsh, bus_size_t size);
126 *
127 * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
128 * Dispose a bus_space handle.
129 * A helper function for bus_space_unmap()/bus_space_free() implementation.
130 */
131#define bus_space_dispose_handle(bst, bsh, size)			\
132	(*(bst)->bs_dispose_handle)(bst, bsh, size)
133
134/*
135 *	int bus_space_paddr(bus_space_tag_t tag,
136 *	    bus_space_handle_t bsh, paddr_t *pap);
137 *
138 * MACHINE DEPENDENT, NOT PORTABLE INTERFACE:
139 * (cannot be implemented on e.g. I/O space on i386, non-linear space on alpha)
140 * Return physical address of a region.
141 * A helper function for machine-dependent device mmap entry.
142 */
143#define bus_space_paddr(bst, bsh, pap)					\
144	(*(bst)->bs_paddr)(bst, bsh, pap)
145
146/*
147 *	void *bus_space_vaddr(bus_space_tag_t, bus_space_handle_t);
148 *
149 * Get the kernel virtual address for the mapped bus space.
150 * Only allowed for regions mapped with BUS_SPACE_MAP_LINEAR.
151 *  (XXX not enforced)
152 */
153#define bus_space_vaddr(bst, bsh)					\
154	((void *)(bsh))
155
156/*
157 *	paddr_t bus_space_mmap(bus_space_tag_t, bus_addr_t, off_t,
158 *	    int, int);
159 *
160 * Mmap bus space on behalf of the user.
161 */
162#define	bus_space_mmap(bst, addr, off, prot, flags)			\
163	(*(bst)->bs_mmap)((bst), (addr), (off), (prot), (flags))
164
165/*
166 *	int bus_space_map(bus_space_tag_t t, bus_addr_t addr,
167 *	    bus_size_t size, int flags, bus_space_handle_t *bshp);
168 *
169 * Map a region of bus space.
170 */
171
172#define bus_space_map(t, a, s, f, hp)					\
173	(*(t)->bs_map)((t), (a), (s), (f), (hp))
174
175/*
176 *	void bus_space_unmap(bus_space_tag_t t,
177 *	    bus_space_handle_t bsh, bus_size_t size);
178 *
179 * Unmap a region of bus space.
180 */
181
182#define bus_space_unmap(t, h, s)					\
183	(*(t)->bs_unmap)((t), (h), (s))
184
185/*
186 *	int bus_space_subregion(bus_space_tag_t t,
187 *	    bus_space_handle_t bsh, bus_size_t offset, bus_size_t size,
188 *	    bus_space_handle_t *nbshp);
189 *
190 * Get a new handle for a subregion of an already-mapped area of bus space.
191 */
192
193#define bus_space_subregion(t, h, o, s, hp)				\
194	(*(t)->bs_subregion)((t), (h), (o), (s), (hp))
195
196/*
197 *	int bus_space_alloc(bus_space_tag_t t, bus_addr_t, rstart,
198 *	    bus_addr_t rend, bus_size_t size, bus_size_t align,
199 *	    bus_size_t boundary, int flags, bus_addr_t *addrp,
200 *	    bus_space_handle_t *bshp);
201 *
202 * Allocate a region of bus space.
203 */
204
205#define bus_space_alloc(t, rs, re, s, a, b, f, ap, hp)			\
206	(*(t)->bs_alloc)((t), (rs), (re), (s), (a), (b), (f), (ap), (hp))
207
208/*
209 *	int bus_space_free(bus_space_tag_t t,
210 *	    bus_space_handle_t bsh, bus_size_t size);
211 *
212 * Free a region of bus space.
213 */
214
215#define bus_space_free(t, h, s)						\
216	(*(t)->bs_free)((t), (h), (s))
217
218/*
219 * Operations which handle byte stream data on word access.
220 *
221 * These functions are defined to resolve endian mismatch, by either
222 * - When normal (i.e. stream-less) operations perform byte swap
223 *   to resolve endian mismatch, these functions bypass the byte swap.
224 * or
225 * - When bus bridge performs automatic byte swap, these functions
226 *   perform byte swap once more, to cancel the bridge's behavior.
227 *
228 * Currently these are just same as normal operations, since all
229 * supported buses are same endian with CPU (i.e. little-endian).
230 *
231 */
232#define __BUS_SPACE_HAS_STREAM_METHODS
233#define bus_space_read_stream_2(tag, bsh, offset)			\
234	bus_space_read_2(tag, bsh, offset)
235#define bus_space_read_stream_4(tag, bsh, offset)			\
236	bus_space_read_4(tag, bsh, offset)
237#define bus_space_read_stream_8(tag, bsh, offset)			\
238	bus_space_read_8(tag, bsh, offset)
239#define bus_space_read_multi_stream_2(tag, bsh, offset, datap, count)	\
240	bus_space_read_multi_2(tag, bsh, offset, datap, count)
241#define bus_space_read_multi_stream_4(tag, bsh, offset, datap, count)	\
242	bus_space_read_multi_4(tag, bsh, offset, datap, count)
243#define bus_space_read_multi_stream_8(tag, bsh, offset, datap, count)	\
244	bus_space_read_multi_8(tag, bsh, offset, datap, count)
245#define bus_space_read_region_stream_2(tag, bsh, offset, datap, count)	\
246	bus_space_read_region_2(tag, bsh, offset, datap, count)
247#define bus_space_read_region_stream_4(tag, bsh, offset, datap, count)	\
248	bus_space_read_region_4(tag, bsh, offset, datap, count)
249#define bus_space_read_region_stream_8(tag, bsh, offset, datap, count)	\
250	bus_space_read_region_8(tag, bsh, offset, datap, count)
251#define bus_space_write_stream_2(tag, bsh, offset, data)		\
252	bus_space_write_2(tag, bsh, offset, data)
253#define bus_space_write_stream_4(tag, bsh, offset, data)		\
254	bus_space_write_4(tag, bsh, offset, data)
255#define bus_space_write_stream_8(tag, bsh, offset, data)		\
256	bus_space_write_8(tag, bsh, offset, data)
257#define bus_space_write_multi_stream_2(tag, bsh, offset, datap, count)	\
258	bus_space_write_multi_2(tag, bsh, offset, datap, count)
259#define bus_space_write_multi_stream_4(tag, bsh, offset, datap, count)	\
260	bus_space_write_multi_4(tag, bsh, offset, datap, count)
261#define bus_space_write_multi_stream_8(tag, bsh, offset, datap, count)	\
262	bus_space_write_multi_8(tag, bsh, offset, datap, count)
263#define bus_space_write_region_stream_2(tag, bsh, offset, datap, count)	\
264	bus_space_write_region_2(tag, bsh, offset, datap, count)
265#define bus_space_write_region_stream_4(tag, bsh, offset, datap, count)	\
266	bus_space_write_region_4(tag, bsh, offset, datap, count)
267#define bus_space_write_region_stream_8(tag, bsh, offset, datap, count)	\
268	bus_space_write_region_8(tag, bsh, offset, datap, count)
269#define bus_space_write_region_stream_2(tag, bsh, offset, datap, count)	\
270	bus_space_write_region_2(tag, bsh, offset, datap, count)
271#define bus_space_write_region_stream_4(tag, bsh, offset, datap, count)	\
272	bus_space_write_region_4(tag, bsh, offset, datap, count)
273#define bus_space_write_region_stream_8(tag, bsh, offset, datap, count)	\
274	bus_space_write_region_8(tag, bsh, offset, datap, count)
275#define bus_space_set_multi_stream_2(tag, bsh, offset, data, count)	\
276	bus_space_set_multi_2(tag, bsh, offset, data, count)
277#define bus_space_set_multi_stream_4(tag, bsh, offset, data, count)	\
278	bus_space_set_multi_4(tag, bsh, offset, data, count)
279#define bus_space_set_multi_stream_8(tag, bsh, offset, data, count)	\
280	bus_space_set_multi_8(tag, bsh, offset, data, count)
281#define bus_space_set_region_stream_2(tag, bsh, offset, data, count)	\
282	bus_space_set_region_2(tag, bsh, offset, data, count)
283#define bus_space_set_region_stream_4(tag, bsh, offset, data, count)	\
284	bus_space_set_region_4(tag, bsh, offset, data, count)
285#define bus_space_set_region_stream_8(tag, bsh, offset, data, count)	\
286	bus_space_set_region_8(tag, bsh, offset, data, count)
287
288/*
289 * Bus read/write barrier methods.
290 *
291 *	void bus_space_barrier(bus_space_tag_t tag,
292 *	    bus_space_handle_t bsh, bus_size_t offset,
293 *	    bus_size_t len, int flags);
294 *
295 * On the MIPS, we just flush the write buffer.
296 */
297#define bus_space_barrier(t, h, o, l, f)				\
298	((void)((void)(t), (void)(h), (void)(o), (void)(l), (void)(f),	\
299	 wbflush()))
300
301/* Forwards needed by prototypes below. */
302struct mbuf;
303struct uio;
304
305#define bus_dmamap_create(t, s, n, m, b, f, p)			\
306	(*(t)->_dmamap_create)((t), (s), (n), (m), (b), (f), (p))
307#define bus_dmamap_destroy(t, p)				\
308	(*(t)->_dmamap_destroy)((t), (p))
309#define bus_dmamap_load(t, m, b, s, p, f)			\
310	(*(t)->_dmamap_load)((t), (m), (b), (s), (p), (f))
311#define bus_dmamap_load_mbuf(t, m, b, f)			\
312	(*(t)->_dmamap_load_mbuf)((t), (m), (b), (f))
313#define bus_dmamap_load_uio(t, m, u, f)				\
314	(*(t)->_dmamap_load_uio)((t), (m), (u), (f))
315#define bus_dmamap_load_raw(t, m, sg, n, s, f)			\
316	(*(t)->_dmamap_load_raw)((t), (m), (sg), (n), (s), (f))
317#define bus_dmamap_unload(t, p)					\
318	(*(t)->_dmamap_unload)((t), (p))
319#define bus_dmamap_sync(t, p, o, l, ops)			\
320	(*(t)->_dmamap_sync)((t), (p), (o), (l), (ops))
321#define bus_dmamem_alloc(t, s, a, b, sg, n, r, f)		\
322	(*(t)->_dmamem_alloc)((t), (s), (a), (b), (sg), (n), (r), (f))
323#define bus_dmamem_free(t, sg, n)				\
324	(*(t)->_dmamem_free)((t), (sg), (n))
325#define bus_dmamem_map(t, sg, n, s, k, f)			\
326	(*(t)->_dmamem_map)((t), (sg), (n), (s), (k), (f))
327#define bus_dmamem_unmap(t, k, s)				\
328	(*(t)->_dmamem_unmap)((t), (k), (s))
329#define bus_dmamem_mmap(t, sg, n, o, p, f)			\
330	(*(t)->_dmamem_mmap)((t), (sg), (n), (o), (p), (f))
331
332#define bus_dmatag_subregion(t, mna, mxa, nt, f) EOPNOTSUPP
333#define bus_dmatag_destroy(t)
334
335#ifdef _ARC_BUS_DMA_PRIVATE
336int	_bus_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
337	    bus_size_t, int, bus_dmamap_t *);
338void	_bus_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
339int	_bus_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *,
340	    bus_size_t, struct proc *, int);
341int	_bus_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t,
342	    struct mbuf *, int);
343int	_bus_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t,
344	    struct uio *, int);
345int	_bus_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t,
346	    bus_dma_segment_t *, int, bus_size_t, int);
347void	_bus_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
348void	_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
349	    bus_size_t, int);
350
351int	_bus_dmamem_alloc(bus_dma_tag_t tag, bus_size_t size,
352	    bus_size_t alignment, bus_size_t boundary,
353	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags);
354int	_bus_dmamem_alloc_range(bus_dma_tag_t tag, bus_size_t size,
355	    bus_size_t alignment, bus_size_t boundary,
356	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
357	    paddr_t low, paddr_t high);
358void	_bus_dmamem_free(bus_dma_tag_t tag, bus_dma_segment_t *segs,
359	    int nsegs);
360int	_bus_dmamem_map(bus_dma_tag_t tag, bus_dma_segment_t *segs,
361	    int nsegs, size_t size, void **kvap, int flags);
362void	_bus_dmamem_unmap(bus_dma_tag_t tag, void *kva,
363	    size_t size);
364paddr_t	_bus_dmamem_mmap(bus_dma_tag_t tag, bus_dma_segment_t *segs,
365	    int nsegs, off_t off, int prot, int flags);
366
367int	_bus_dmamem_alloc_range(bus_dma_tag_t tag, bus_size_t size,
368	    bus_size_t alignment, bus_size_t boundary,
369	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
370	    paddr_t low, paddr_t high);
371#endif /* _ARC_BUS_DMA_PRIVATE */
372
373void	_bus_dma_tag_init(bus_dma_tag_t tag);
374void	jazz_bus_dma_tag_init(bus_dma_tag_t tag);
375void	isadma_bounce_tag_init(bus_dma_tag_t tag);
376
377#endif /* _KERNEL */
378#endif /* _ARC_BUS_FUNCS_H_ */
379