xref: /openbsd/sys/arch/riscv64/include/bus.h (revision a3fd9ccd)
1*a3fd9ccdSkettenis /*	$OpenBSD: bus.h,v 1.6 2024/03/27 23:10:18 kettenis Exp $	*/
2380aa7b9Sjsg 
3baed8f06Sdrahn /*
4baed8f06Sdrahn  * Copyright (c) 2003-2004 Opsycon AB Sweden.  All rights reserved.
5baed8f06Sdrahn  *
6baed8f06Sdrahn  * Redistribution and use in source and binary forms, with or without
7baed8f06Sdrahn  * modification, are permitted provided that the following conditions
8baed8f06Sdrahn  * are met:
9baed8f06Sdrahn  * 1. Redistributions of source code must retain the above copyright
10baed8f06Sdrahn  *    notice, this list of conditions and the following disclaimer.
11baed8f06Sdrahn  * 2. Redistributions in binary form must reproduce the above copyright
12baed8f06Sdrahn  *    notice, this list of conditions and the following disclaimer in the
13baed8f06Sdrahn  *    documentation and/or other materials provided with the distribution.
14baed8f06Sdrahn  *
15baed8f06Sdrahn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16baed8f06Sdrahn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17baed8f06Sdrahn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18baed8f06Sdrahn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19baed8f06Sdrahn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20baed8f06Sdrahn  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21baed8f06Sdrahn  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22baed8f06Sdrahn  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23baed8f06Sdrahn  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24baed8f06Sdrahn  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25baed8f06Sdrahn  */
26baed8f06Sdrahn 
27baed8f06Sdrahn #ifndef _MACHINE_BUS_H_
28baed8f06Sdrahn #define _MACHINE_BUS_H_
29baed8f06Sdrahn 
30baed8f06Sdrahn #ifdef __STDC__
31baed8f06Sdrahn #define CAT(a,b)	a##b
32baed8f06Sdrahn #define CAT3(a,b,c)	a##b##c
33baed8f06Sdrahn #else
34baed8f06Sdrahn #define CAT(a,b)	a/**/b
35baed8f06Sdrahn #define CAT3(a,b,c)	a/**/b/**/c
36baed8f06Sdrahn #endif
37baed8f06Sdrahn 
38baed8f06Sdrahn /*
39baed8f06Sdrahn  * Bus access types.
40baed8f06Sdrahn  */
41baed8f06Sdrahn struct bus_space;
42baed8f06Sdrahn typedef u_long bus_addr_t;
43baed8f06Sdrahn typedef u_long bus_size_t;
44baed8f06Sdrahn typedef u_long bus_space_handle_t;
45baed8f06Sdrahn typedef struct bus_space *bus_space_tag_t;
46baed8f06Sdrahn typedef struct bus_space bus_space_t;
47baed8f06Sdrahn 
48baed8f06Sdrahn struct bus_space {
49baed8f06Sdrahn 	bus_addr_t	bus_base;
50baed8f06Sdrahn 	void		*bus_private;
51baed8f06Sdrahn 	u_int8_t	(*_space_read_1)(bus_space_tag_t , bus_space_handle_t,
52baed8f06Sdrahn 			  bus_size_t);
53baed8f06Sdrahn 	void		(*_space_write_1)(bus_space_tag_t , bus_space_handle_t,
54baed8f06Sdrahn 			  bus_size_t, u_int8_t);
55baed8f06Sdrahn 	u_int16_t	(*_space_read_2)(bus_space_tag_t , bus_space_handle_t,
56baed8f06Sdrahn 			  bus_size_t);
57baed8f06Sdrahn 	void		(*_space_write_2)(bus_space_tag_t , bus_space_handle_t,
58baed8f06Sdrahn 			  bus_size_t, u_int16_t);
59baed8f06Sdrahn 	u_int32_t	(*_space_read_4)(bus_space_tag_t , bus_space_handle_t,
60baed8f06Sdrahn 			  bus_size_t);
61baed8f06Sdrahn 	void		(*_space_write_4)(bus_space_tag_t , bus_space_handle_t,
62baed8f06Sdrahn 			  bus_size_t, u_int32_t);
63baed8f06Sdrahn 	u_int64_t	(*_space_read_8)(bus_space_tag_t , bus_space_handle_t,
64baed8f06Sdrahn 			  bus_size_t);
65baed8f06Sdrahn 	void		(*_space_write_8)(bus_space_tag_t , bus_space_handle_t,
66baed8f06Sdrahn 			  bus_size_t, u_int64_t);
67baed8f06Sdrahn 	void		(*_space_read_raw_2)(bus_space_tag_t, bus_space_handle_t,
68baed8f06Sdrahn 			  bus_addr_t, u_int8_t *, bus_size_t);
69baed8f06Sdrahn 	void		(*_space_write_raw_2)(bus_space_tag_t, bus_space_handle_t,
70baed8f06Sdrahn 			  bus_addr_t, const u_int8_t *, bus_size_t);
71baed8f06Sdrahn 	void		(*_space_read_raw_4)(bus_space_tag_t, bus_space_handle_t,
72baed8f06Sdrahn 			  bus_addr_t, u_int8_t *, bus_size_t);
73baed8f06Sdrahn 	void		(*_space_write_raw_4)(bus_space_tag_t, bus_space_handle_t,
74baed8f06Sdrahn 			  bus_addr_t, const u_int8_t *, bus_size_t);
75baed8f06Sdrahn 	void		(*_space_read_raw_8)(bus_space_tag_t, bus_space_handle_t,
76baed8f06Sdrahn 			  bus_addr_t, u_int8_t *, bus_size_t);
77baed8f06Sdrahn 	void		(*_space_write_raw_8)(bus_space_tag_t, bus_space_handle_t,
78baed8f06Sdrahn 			  bus_addr_t, const u_int8_t *, bus_size_t);
79baed8f06Sdrahn 	int		(*_space_map)(bus_space_tag_t , bus_addr_t,
80baed8f06Sdrahn 			  bus_size_t, int, bus_space_handle_t *);
81baed8f06Sdrahn 	void		(*_space_unmap)(bus_space_tag_t, bus_space_handle_t,
82baed8f06Sdrahn 			  bus_size_t);
83baed8f06Sdrahn 	int		(*_space_subregion)(bus_space_tag_t, bus_space_handle_t,
84baed8f06Sdrahn 			  bus_size_t, bus_size_t, bus_space_handle_t *);
85baed8f06Sdrahn 	void *		(*_space_vaddr)(bus_space_tag_t, bus_space_handle_t);
86baed8f06Sdrahn 	paddr_t		(*_space_mmap)(bus_space_tag_t, bus_addr_t, off_t,
87baed8f06Sdrahn 			  int, int);
88baed8f06Sdrahn };
89baed8f06Sdrahn 
90baed8f06Sdrahn #define	bus_space_read_1(t, h, o) (*(t)->_space_read_1)((t), (h), (o))
91baed8f06Sdrahn #define	bus_space_read_2(t, h, o) (*(t)->_space_read_2)((t), (h), (o))
92baed8f06Sdrahn #define	bus_space_read_4(t, h, o) (*(t)->_space_read_4)((t), (h), (o))
93baed8f06Sdrahn #define	bus_space_read_8(t, h, o) (*(t)->_space_read_8)((t), (h), (o))
94baed8f06Sdrahn 
95baed8f06Sdrahn #define	bus_space_write_1(t, h, o, v) (*(t)->_space_write_1)((t), (h), (o), (v))
96baed8f06Sdrahn #define	bus_space_write_2(t, h, o, v) (*(t)->_space_write_2)((t), (h), (o), (v))
97baed8f06Sdrahn #define	bus_space_write_4(t, h, o, v) (*(t)->_space_write_4)((t), (h), (o), (v))
98baed8f06Sdrahn #define	bus_space_write_8(t, h, o, v) (*(t)->_space_write_8)((t), (h), (o), (v))
99baed8f06Sdrahn 
100baed8f06Sdrahn #define	bus_space_read_raw_2(t, h, o) \
101baed8f06Sdrahn 	(*(t)->_space_read_2)((t), (h), (o))
102baed8f06Sdrahn #define	bus_space_read_raw_4(t, h, o) \
103baed8f06Sdrahn 	(*(t)->_space_read_4)((t), (h), (o))
104baed8f06Sdrahn #define	bus_space_read_raw_8(t, h, o) \
105baed8f06Sdrahn 	(*(t)->_space_read_8)((t), (h), (o))
106baed8f06Sdrahn 
107baed8f06Sdrahn #define	bus_space_write_raw_2(t, h, o, v) \
108baed8f06Sdrahn 	(*(t)->_space_write_2)((t), (h), (o), (v))
109baed8f06Sdrahn #define	bus_space_write_raw_4(t, h, o, v) \
110baed8f06Sdrahn 	(*(t)->_space_write_4)((t), (h), (o), (v))
111baed8f06Sdrahn #define	bus_space_write_raw_8(t, h, o, v) \
112baed8f06Sdrahn 	(*(t)->_space_write_8)((t), (h), (o), (v))
113baed8f06Sdrahn 
114baed8f06Sdrahn #define	bus_space_read_raw_multi_2(t, h, a, b, l) \
115baed8f06Sdrahn 	(*(t)->_space_read_raw_2)((t), (h), (a), (b), (l))
116baed8f06Sdrahn #define	bus_space_read_raw_multi_4(t, h, a, b, l) \
117baed8f06Sdrahn 	(*(t)->_space_read_raw_4)((t), (h), (a), (b), (l))
118baed8f06Sdrahn #define	bus_space_read_raw_multi_8(t, h, a, b, l) \
119baed8f06Sdrahn 	(*(t)->_space_read_raw_8)((t), (h), (a), (b), (l))
120baed8f06Sdrahn 
121baed8f06Sdrahn #define	bus_space_write_raw_multi_2(t, h, a, b, l) \
122baed8f06Sdrahn 	(*(t)->_space_write_raw_2)((t), (h), (a), (b), (l))
123baed8f06Sdrahn #define	bus_space_write_raw_multi_4(t, h, a, b, l) \
124baed8f06Sdrahn 	(*(t)->_space_write_raw_4)((t), (h), (a), (b), (l))
125baed8f06Sdrahn #define	bus_space_write_raw_multi_8(t, h, a, b, l) \
126baed8f06Sdrahn 	(*(t)->_space_write_raw_8)((t), (h), (a), (b), (l))
127baed8f06Sdrahn 
128baed8f06Sdrahn #define	bus_space_map(t, o, s, c, p) (*(t)->_space_map)((t), (o), (s), (c), (p))
129baed8f06Sdrahn #define	bus_space_unmap(t, h, s) (*(t)->_space_unmap)((t), (h), (s))
130baed8f06Sdrahn #define	bus_space_subregion(t, h, o, s, p) \
131baed8f06Sdrahn     (*(t)->_space_subregion)((t), (h), (o), (s), (p))
132baed8f06Sdrahn 
133baed8f06Sdrahn #define BUS_SPACE_MAP_CACHEABLE		0x01
1341dfe7d0dSkettenis #define BUS_SPACE_MAP_LINEAR		0x02
1351dfe7d0dSkettenis #define BUS_SPACE_MAP_PREFETCHABLE	0x04
136baed8f06Sdrahn 
137baed8f06Sdrahn #define	bus_space_vaddr(t, h)	(*(t)->_space_vaddr)((t), (h))
138baed8f06Sdrahn #define	bus_space_mmap(t, a, o, p, f) \
139baed8f06Sdrahn     (*(t)->_space_mmap)((t), (a), (o), (p), (f))
140baed8f06Sdrahn 
141baed8f06Sdrahn /*----------------------------------------------------------------------------*/
142baed8f06Sdrahn #define bus_space_read_multi(n,m)					      \
143baed8f06Sdrahn static __inline void							      \
144baed8f06Sdrahn CAT(bus_space_read_multi_,n)(bus_space_tag_t bst, bus_space_handle_t bsh,     \
145baed8f06Sdrahn      bus_size_t o, CAT3(u_int,m,_t) *x, size_t cnt)			      \
146baed8f06Sdrahn {									      \
147baed8f06Sdrahn 	while (cnt--)							      \
148baed8f06Sdrahn 		*x++ = CAT(bus_space_read_,n)(bst, bsh, o);		      \
149baed8f06Sdrahn }
150baed8f06Sdrahn 
151baed8f06Sdrahn bus_space_read_multi(1,8)
152baed8f06Sdrahn bus_space_read_multi(2,16)
153baed8f06Sdrahn bus_space_read_multi(4,32)
154baed8f06Sdrahn bus_space_read_multi(8,64)
155baed8f06Sdrahn 
156baed8f06Sdrahn /*----------------------------------------------------------------------------*/
157baed8f06Sdrahn #define bus_space_read_region(n,m)					      \
158baed8f06Sdrahn static __inline void							      \
159baed8f06Sdrahn CAT(bus_space_read_region_,n)(bus_space_tag_t bst, bus_space_handle_t bsh,    \
160baed8f06Sdrahn      bus_addr_t ba, CAT3(u_int,m,_t) *x, size_t cnt)			      \
161baed8f06Sdrahn {									      \
1621dfe7d0dSkettenis 	while (cnt--) {							      \
1631dfe7d0dSkettenis 		*x++ = CAT(bus_space_read_,n)(bst, bsh, ba);		      \
1641dfe7d0dSkettenis 		ba += (n);						      \
1651dfe7d0dSkettenis 	}								      \
166baed8f06Sdrahn }
167baed8f06Sdrahn 
168baed8f06Sdrahn bus_space_read_region(1,8)
169baed8f06Sdrahn bus_space_read_region(2,16)
170baed8f06Sdrahn bus_space_read_region(4,32)
171baed8f06Sdrahn bus_space_read_region(8,64)
172baed8f06Sdrahn 
173baed8f06Sdrahn /*----------------------------------------------------------------------------*/
174baed8f06Sdrahn #define bus_space_read_raw_region(n,m)					      \
175baed8f06Sdrahn static __inline void							      \
176baed8f06Sdrahn CAT(bus_space_read_raw_region_,n)(bus_space_tag_t bst,			      \
177baed8f06Sdrahn      bus_space_handle_t bsh,						      \
178baed8f06Sdrahn      bus_addr_t ba, u_int8_t *x, size_t cnt)				      \
179baed8f06Sdrahn {									      \
180baed8f06Sdrahn 	cnt >>= ((n) >> 1);						      \
181baed8f06Sdrahn 	while (cnt--) {							      \
182baed8f06Sdrahn 		CAT(bus_space_read_raw_multi_,n)(bst, bsh, ba, x, (n));	      \
183baed8f06Sdrahn 		ba += (n);						      \
184baed8f06Sdrahn 		x += (n);						      \
185baed8f06Sdrahn 	}								      \
186baed8f06Sdrahn }
187baed8f06Sdrahn 
188baed8f06Sdrahn bus_space_read_raw_region(2,16)
189baed8f06Sdrahn bus_space_read_raw_region(4,32)
190baed8f06Sdrahn bus_space_read_raw_region(8,64)
191baed8f06Sdrahn 
192baed8f06Sdrahn /*----------------------------------------------------------------------------*/
193baed8f06Sdrahn #define bus_space_write_multi(n,m)					      \
194baed8f06Sdrahn static __inline void							      \
195baed8f06Sdrahn CAT(bus_space_write_multi_,n)(bus_space_tag_t bst, bus_space_handle_t bsh,    \
196baed8f06Sdrahn      bus_size_t o, const CAT3(u_int,m,_t) *x, size_t cnt)		      \
197baed8f06Sdrahn {									      \
1981dfe7d0dSkettenis 	while (cnt--)							      \
199baed8f06Sdrahn 		CAT(bus_space_write_,n)(bst, bsh, o, *x++);		      \
200baed8f06Sdrahn }
201baed8f06Sdrahn 
202baed8f06Sdrahn bus_space_write_multi(1,8)
203baed8f06Sdrahn bus_space_write_multi(2,16)
204baed8f06Sdrahn bus_space_write_multi(4,32)
205baed8f06Sdrahn bus_space_write_multi(8,64)
206baed8f06Sdrahn 
207baed8f06Sdrahn /*----------------------------------------------------------------------------*/
208baed8f06Sdrahn #define bus_space_write_region(n,m)					      \
209baed8f06Sdrahn static __inline void							      \
210baed8f06Sdrahn CAT(bus_space_write_region_,n)(bus_space_tag_t bst, bus_space_handle_t bsh,   \
211baed8f06Sdrahn      bus_addr_t ba, const CAT3(u_int,m,_t) *x, size_t cnt)		      \
212baed8f06Sdrahn {									      \
213baed8f06Sdrahn 	while (cnt--) {							      \
214baed8f06Sdrahn 		CAT(bus_space_write_,n)(bst, bsh, ba, *x++);		      \
2151dfe7d0dSkettenis 		ba += (n);						      \
216baed8f06Sdrahn 	}								      \
217baed8f06Sdrahn }
218baed8f06Sdrahn 
219baed8f06Sdrahn bus_space_write_region(1,8)
220baed8f06Sdrahn bus_space_write_region(2,16)
221baed8f06Sdrahn bus_space_write_region(4,32)
222baed8f06Sdrahn bus_space_write_region(8,64)
223baed8f06Sdrahn 
224baed8f06Sdrahn /*----------------------------------------------------------------------------*/
225baed8f06Sdrahn #define bus_space_write_raw_region(n,m)					      \
226baed8f06Sdrahn static __inline void							      \
227baed8f06Sdrahn CAT(bus_space_write_raw_region_,n)(bus_space_tag_t bst,			      \
228baed8f06Sdrahn      bus_space_handle_t bsh,						      \
229baed8f06Sdrahn      bus_addr_t ba, const u_int8_t *x, size_t cnt)		              \
230baed8f06Sdrahn {									      \
231baed8f06Sdrahn 	cnt >>= ((n) >> 1);						      \
232baed8f06Sdrahn 	while (cnt--) {							      \
233baed8f06Sdrahn 		CAT(bus_space_write_raw_multi_,n)(bst, bsh, ba, x, (n));      \
234baed8f06Sdrahn 		ba += (n);						      \
235baed8f06Sdrahn 		x += (n);						      \
236baed8f06Sdrahn 	}								      \
237baed8f06Sdrahn }
238baed8f06Sdrahn 
239baed8f06Sdrahn bus_space_write_raw_region(2,16)
240baed8f06Sdrahn bus_space_write_raw_region(4,32)
241baed8f06Sdrahn bus_space_write_raw_region(8,64)
242baed8f06Sdrahn 
243baed8f06Sdrahn /*----------------------------------------------------------------------------*/
244baed8f06Sdrahn #define bus_space_set_region(n,m)					      \
245baed8f06Sdrahn static __inline void							      \
246baed8f06Sdrahn CAT(bus_space_set_region_,n)(bus_space_tag_t bst, bus_space_handle_t bsh,     \
247baed8f06Sdrahn      bus_addr_t ba, CAT3(u_int,m,_t) x, size_t cnt)			      \
248baed8f06Sdrahn {									      \
249baed8f06Sdrahn 	while (cnt--) {							      \
250baed8f06Sdrahn 		CAT(bus_space_write_,n)(bst, bsh, ba, x);		      \
2511dfe7d0dSkettenis 		ba += (n);						      \
252baed8f06Sdrahn 	}								      \
253baed8f06Sdrahn }
254baed8f06Sdrahn 
255baed8f06Sdrahn bus_space_set_region(1,8)
256baed8f06Sdrahn bus_space_set_region(2,16)
257baed8f06Sdrahn bus_space_set_region(4,32)
258baed8f06Sdrahn bus_space_set_region(8,64)
259baed8f06Sdrahn 
260baed8f06Sdrahn /*----------------------------------------------------------------------------*/
261baed8f06Sdrahn static __inline void
bus_space_copy_1(void * v,bus_space_handle_t h1,bus_size_t o1,bus_space_handle_t h2,bus_size_t o2,bus_size_t c)262baed8f06Sdrahn bus_space_copy_1(void *v, bus_space_handle_t h1, bus_size_t o1,
263baed8f06Sdrahn 	bus_space_handle_t h2, bus_size_t o2, bus_size_t c)
264baed8f06Sdrahn {
265baed8f06Sdrahn 	char *s = (char *)(h1 + o1);
266baed8f06Sdrahn 	char *d = (char *)(h2 + o2);
267baed8f06Sdrahn 
268*a3fd9ccdSkettenis 	__asm volatile ("fence w,o" ::: "memory");
269baed8f06Sdrahn 	while (c--)
270baed8f06Sdrahn 		*d++ = *s++;
271*a3fd9ccdSkettenis 	__asm volatile ("fence io,iwr" ::: "memory");
272baed8f06Sdrahn }
273baed8f06Sdrahn 
274baed8f06Sdrahn 
275baed8f06Sdrahn static __inline void
bus_space_copy_2(void * v,bus_space_handle_t h1,bus_size_t o1,bus_space_handle_t h2,bus_size_t o2,bus_size_t c)276baed8f06Sdrahn bus_space_copy_2(void *v, bus_space_handle_t h1, bus_size_t o1,
277baed8f06Sdrahn 	bus_space_handle_t h2, bus_size_t o2, bus_size_t c)
278baed8f06Sdrahn {
279baed8f06Sdrahn 	short *s = (short *)(h1 + o1);
280baed8f06Sdrahn 	short *d = (short *)(h2 + o2);
281baed8f06Sdrahn 
282*a3fd9ccdSkettenis 	__asm volatile ("fence w,o" ::: "memory");
283baed8f06Sdrahn 	while (c--)
284baed8f06Sdrahn 		*d++ = *s++;
285*a3fd9ccdSkettenis 	__asm volatile ("fence io,iwr" ::: "memory");
286baed8f06Sdrahn }
287baed8f06Sdrahn 
288baed8f06Sdrahn static __inline void
bus_space_copy_4(void * v,bus_space_handle_t h1,bus_size_t o1,bus_space_handle_t h2,bus_size_t o2,bus_size_t c)289baed8f06Sdrahn bus_space_copy_4(void *v, bus_space_handle_t h1, bus_size_t o1,
290baed8f06Sdrahn 	bus_space_handle_t h2, bus_size_t o2, bus_size_t c)
291baed8f06Sdrahn {
292baed8f06Sdrahn 	int *s = (int *)(h1 + o1);
293baed8f06Sdrahn 	int *d = (int *)(h2 + o2);
294baed8f06Sdrahn 
295*a3fd9ccdSkettenis 	__asm volatile ("fence w,o" ::: "memory");
296baed8f06Sdrahn 	while (c--)
297baed8f06Sdrahn 		*d++ = *s++;
298*a3fd9ccdSkettenis 	__asm volatile ("fence io,iwr" ::: "memory");
299baed8f06Sdrahn }
300baed8f06Sdrahn 
301baed8f06Sdrahn static __inline void
bus_space_copy_8(void * v,bus_space_handle_t h1,bus_size_t o1,bus_space_handle_t h2,bus_size_t o2,bus_size_t c)302baed8f06Sdrahn bus_space_copy_8(void *v, bus_space_handle_t h1, bus_size_t o1,
303baed8f06Sdrahn 	bus_space_handle_t h2, bus_size_t o2, bus_size_t c)
304baed8f06Sdrahn {
305baed8f06Sdrahn 	int64_t *s = (int64_t *)(h1 + o1);
306baed8f06Sdrahn 	int64_t *d = (int64_t *)(h2 + o2);
307baed8f06Sdrahn 
308*a3fd9ccdSkettenis 	__asm volatile ("fence w,o" ::: "memory");
309baed8f06Sdrahn 	while (c--)
310baed8f06Sdrahn 		*d++ = *s++;
311*a3fd9ccdSkettenis 	__asm volatile ("fence io,iwr" ::: "memory");
312baed8f06Sdrahn }
313baed8f06Sdrahn 
314baed8f06Sdrahn /*----------------------------------------------------------------------------*/
315baed8f06Sdrahn /*
316baed8f06Sdrahn  * Bus read/write barrier methods.
317baed8f06Sdrahn  *
318baed8f06Sdrahn  *	void bus_space_barrier(bus_space_tag_t tag,
319baed8f06Sdrahn  *	    bus_space_handle_t bsh, bus_size_t offset,
320baed8f06Sdrahn  *	    bus_size_t len, int flags);
321baed8f06Sdrahn  *
322baed8f06Sdrahn  */
323baed8f06Sdrahn static inline void
bus_space_barrier(bus_space_tag_t t,bus_space_handle_t h,bus_size_t offset,bus_size_t length,int flags)324baed8f06Sdrahn bus_space_barrier(bus_space_tag_t t, bus_space_handle_t h, bus_size_t offset,
325baed8f06Sdrahn     bus_size_t length, int flags)
326baed8f06Sdrahn {
32768b10e9fSjsg 	__asm__ volatile ("fence iorw,iorw" ::: "memory");
328baed8f06Sdrahn }
329baed8f06Sdrahn #define BUS_SPACE_BARRIER_READ  0x01		/* force read barrier */
330baed8f06Sdrahn #define BUS_SPACE_BARRIER_WRITE 0x02		/* force write barrier */
331baed8f06Sdrahn 
332baed8f06Sdrahn #define	BUS_DMA_WAITOK		0x0000
333baed8f06Sdrahn #define	BUS_DMA_NOWAIT		0x0001
334baed8f06Sdrahn #define	BUS_DMA_ALLOCNOW	0x0002
335baed8f06Sdrahn #define	BUS_DMA_COHERENT	0x0008
336baed8f06Sdrahn #define	BUS_DMA_BUS1		0x0010	/* placeholders for bus functions... */
337baed8f06Sdrahn #define	BUS_DMA_BUS2		0x0020
338baed8f06Sdrahn #define	BUS_DMA_BUS3		0x0040
339baed8f06Sdrahn #define	BUS_DMA_BUS4		0x0080
340baed8f06Sdrahn #define	BUS_DMA_READ		0x0100	/* mapping is device -> memory only */
341baed8f06Sdrahn #define	BUS_DMA_WRITE		0x0200	/* mapping is memory -> device only */
342baed8f06Sdrahn #define	BUS_DMA_STREAMING	0x0400	/* hint: sequential, unidirectional */
343baed8f06Sdrahn #define	BUS_DMA_ZERO		0x0800	/* zero memory in dmamem_alloc */
344baed8f06Sdrahn #define	BUS_DMA_NOCACHE		0x1000
345baed8f06Sdrahn #define	BUS_DMA_64BIT		0x2000	/* device handles 64bit dva */
346baed8f06Sdrahn 
347baed8f06Sdrahn /* Forwards needed by prototypes below. */
348baed8f06Sdrahn struct mbuf;
349baed8f06Sdrahn struct proc;
350baed8f06Sdrahn struct uio;
351baed8f06Sdrahn 
352baed8f06Sdrahn #define	BUS_DMASYNC_POSTREAD	0x0001
353baed8f06Sdrahn #define BUS_DMASYNC_POSTWRITE	0x0002
354baed8f06Sdrahn #define BUS_DMASYNC_PREREAD	0x0004
355baed8f06Sdrahn #define BUS_DMASYNC_PREWRITE	0x0008
356baed8f06Sdrahn 
357baed8f06Sdrahn typedef struct machine_bus_dma_tag	*bus_dma_tag_t;
358baed8f06Sdrahn typedef struct machine_bus_dmamap	*bus_dmamap_t;
359baed8f06Sdrahn 
360baed8f06Sdrahn /*
361baed8f06Sdrahn  *	bus_dma_segment_t
362baed8f06Sdrahn  *
363baed8f06Sdrahn  *	Describes a single contiguous DMA transaction.  Values
364baed8f06Sdrahn  *	are suitable for programming into DMA registers.
365baed8f06Sdrahn  */
366baed8f06Sdrahn struct machine_bus_dma_segment {
367baed8f06Sdrahn 	bus_addr_t	ds_addr;	/* DMA address */
368baed8f06Sdrahn 	bus_size_t	ds_len;		/* length of transfer */
369baed8f06Sdrahn 
370baed8f06Sdrahn 	paddr_t		_ds_paddr;	/* CPU address */
371baed8f06Sdrahn 	vaddr_t		_ds_vaddr;	/* CPU address */
372d48e5b04Skettenis 	int		_ds_coherent;	/* Coherently mapped */
373baed8f06Sdrahn };
374baed8f06Sdrahn typedef struct machine_bus_dma_segment	bus_dma_segment_t;
375baed8f06Sdrahn 
376baed8f06Sdrahn /*
377baed8f06Sdrahn  *	bus_dma_tag_t
378baed8f06Sdrahn  *
379baed8f06Sdrahn  *	A machine-dependent opaque type describing the implementation of
380baed8f06Sdrahn  *	DMA for a given bus.
381baed8f06Sdrahn  */
382baed8f06Sdrahn 
383baed8f06Sdrahn struct machine_bus_dma_tag {
384baed8f06Sdrahn 	void	*_cookie;		/* cookie used in the guts */
385baed8f06Sdrahn 	int	_flags;			/* misc. flags */
386baed8f06Sdrahn 
387baed8f06Sdrahn 	/*
388baed8f06Sdrahn 	 * DMA mapping methods.
389baed8f06Sdrahn 	 */
390baed8f06Sdrahn 	int	(*_dmamap_create)(bus_dma_tag_t , bus_size_t, int,
391baed8f06Sdrahn 		    bus_size_t, bus_size_t, int, bus_dmamap_t *);
392baed8f06Sdrahn 	void	(*_dmamap_destroy)(bus_dma_tag_t , bus_dmamap_t);
393baed8f06Sdrahn 	int	(*_dmamap_load)(bus_dma_tag_t , bus_dmamap_t, void *,
394baed8f06Sdrahn 		    bus_size_t, struct proc *, int);
395baed8f06Sdrahn 	int	(*_dmamap_load_mbuf)(bus_dma_tag_t , bus_dmamap_t,
396baed8f06Sdrahn 		    struct mbuf *, int);
397baed8f06Sdrahn 	int	(*_dmamap_load_uio)(bus_dma_tag_t , bus_dmamap_t,
398baed8f06Sdrahn 		    struct uio *, int);
399baed8f06Sdrahn 	int	(*_dmamap_load_raw)(bus_dma_tag_t , bus_dmamap_t,
400baed8f06Sdrahn 		    bus_dma_segment_t *, int, bus_size_t, int);
401baed8f06Sdrahn 	int	(*_dmamap_load_buffer)(bus_dma_tag_t, bus_dmamap_t, void *,
402baed8f06Sdrahn 		    bus_size_t, struct proc *, int, paddr_t *, int *, int);
403baed8f06Sdrahn 	void	(*_dmamap_unload)(bus_dma_tag_t , bus_dmamap_t);
404baed8f06Sdrahn 	void	(*_dmamap_sync)(bus_dma_tag_t , bus_dmamap_t,
405baed8f06Sdrahn 		    bus_addr_t, bus_size_t, int);
406baed8f06Sdrahn 
407baed8f06Sdrahn 	/*
408baed8f06Sdrahn 	 * DMA memory utility functions.
409baed8f06Sdrahn 	 */
410baed8f06Sdrahn 	int	(*_dmamem_alloc)(bus_dma_tag_t, bus_size_t, bus_size_t,
411baed8f06Sdrahn 		    bus_size_t, bus_dma_segment_t *, int, int *, int);
412baed8f06Sdrahn 	void	(*_dmamem_free)(bus_dma_tag_t, bus_dma_segment_t *, int);
413baed8f06Sdrahn 	int	(*_dmamem_map)(bus_dma_tag_t, bus_dma_segment_t *,
414baed8f06Sdrahn 		    int, size_t, caddr_t *, int);
415baed8f06Sdrahn 	void	(*_dmamem_unmap)(bus_dma_tag_t, caddr_t, size_t);
416baed8f06Sdrahn 	paddr_t	(*_dmamem_mmap)(bus_dma_tag_t, bus_dma_segment_t *,
417baed8f06Sdrahn 		    int, off_t, int, int);
418baed8f06Sdrahn 
419baed8f06Sdrahn 	/*
420baed8f06Sdrahn 	 * internal memory address translation information.
421baed8f06Sdrahn 	 */
422baed8f06Sdrahn 	bus_addr_t _dma_mask;
423baed8f06Sdrahn };
424baed8f06Sdrahn 
425baed8f06Sdrahn #define	bus_dmamap_create(t, s, n, m, b, f, p)			\
426baed8f06Sdrahn 	(*(t)->_dmamap_create)((t), (s), (n), (m), (b), (f), (p))
427baed8f06Sdrahn #define	bus_dmamap_destroy(t, p)				\
428baed8f06Sdrahn 	(*(t)->_dmamap_destroy)((t), (p))
429baed8f06Sdrahn #define	bus_dmamap_load(t, m, b, s, p, f)			\
430baed8f06Sdrahn 	(*(t)->_dmamap_load)((t), (m), (b), (s), (p), (f))
431baed8f06Sdrahn #define	bus_dmamap_load_mbuf(t, m, b, f)			\
432baed8f06Sdrahn 	(*(t)->_dmamap_load_mbuf)((t), (m), (b), (f))
433baed8f06Sdrahn #define	bus_dmamap_load_uio(t, m, u, f)				\
434baed8f06Sdrahn 	(*(t)->_dmamap_load_uio)((t), (m), (u), (f))
435baed8f06Sdrahn #define	bus_dmamap_load_raw(t, m, sg, n, s, f)			\
436baed8f06Sdrahn 	(*(t)->_dmamap_load_raw)((t), (m), (sg), (n), (s), (f))
437baed8f06Sdrahn #define	bus_dmamap_unload(t, p)					\
438baed8f06Sdrahn 	(*(t)->_dmamap_unload)((t), (p))
439baed8f06Sdrahn #define	bus_dmamap_sync(t, p, a, l, o)				\
440baed8f06Sdrahn 	(void)((t)->_dmamap_sync ?				\
441baed8f06Sdrahn 	    (*(t)->_dmamap_sync)((t), (p), (a), (l), (o)) : (void)0)
442baed8f06Sdrahn 
443baed8f06Sdrahn #define	bus_dmamem_alloc(t, s, a, b, sg, n, r, f)		\
444baed8f06Sdrahn 	(*(t)->_dmamem_alloc)((t), (s), (a), (b), (sg), (n), (r), (f))
445baed8f06Sdrahn #define	bus_dmamem_free(t, sg, n)				\
446baed8f06Sdrahn 	(*(t)->_dmamem_free)((t), (sg), (n))
447baed8f06Sdrahn #define	bus_dmamem_map(t, sg, n, s, k, f)			\
448baed8f06Sdrahn 	(*(t)->_dmamem_map)((t), (sg), (n), (s), (k), (f))
449baed8f06Sdrahn #define	bus_dmamem_unmap(t, k, s)				\
450baed8f06Sdrahn 	(*(t)->_dmamem_unmap)((t), (k), (s))
451baed8f06Sdrahn #define	bus_dmamem_mmap(t, sg, n, o, p, f)			\
452baed8f06Sdrahn 	(*(t)->_dmamem_mmap)((t), (sg), (n), (o), (p), (f))
453baed8f06Sdrahn 
454baed8f06Sdrahn int	_dmamap_create(bus_dma_tag_t, bus_size_t, int,
455baed8f06Sdrahn 	    bus_size_t, bus_size_t, int, bus_dmamap_t *);
456baed8f06Sdrahn void	_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
457baed8f06Sdrahn int	_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *,
458baed8f06Sdrahn 	    bus_size_t, struct proc *, int);
459baed8f06Sdrahn int	_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t, struct mbuf *, int);
460baed8f06Sdrahn int	_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t, struct uio *, int);
461baed8f06Sdrahn int	_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t,
462baed8f06Sdrahn 	    bus_dma_segment_t *, int, bus_size_t, int);
463baed8f06Sdrahn int	_dmamap_load_buffer(bus_dma_tag_t, bus_dmamap_t, void *,
464baed8f06Sdrahn 	    bus_size_t, struct proc *, int, paddr_t *, int *, int);
465baed8f06Sdrahn void	_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
466baed8f06Sdrahn void	_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
467baed8f06Sdrahn 	    bus_size_t, int);
468baed8f06Sdrahn 
469baed8f06Sdrahn int	_dmamem_alloc(bus_dma_tag_t, bus_size_t, bus_size_t,
470baed8f06Sdrahn 	    bus_size_t, bus_dma_segment_t *, int, int *, int);
471baed8f06Sdrahn void	_dmamem_free(bus_dma_tag_t, bus_dma_segment_t *, int);
472baed8f06Sdrahn int	_dmamem_map(bus_dma_tag_t, bus_dma_segment_t *,
473baed8f06Sdrahn 	    int, size_t, caddr_t *, int);
474baed8f06Sdrahn void	_dmamem_unmap(bus_dma_tag_t, caddr_t, size_t);
475baed8f06Sdrahn paddr_t	_dmamem_mmap(bus_dma_tag_t, bus_dma_segment_t *, int, off_t, int, int);
476baed8f06Sdrahn int	_dmamem_alloc_range(bus_dma_tag_t, bus_size_t, bus_size_t, bus_size_t,
477baed8f06Sdrahn 	    bus_dma_segment_t *, int, int *, int, paddr_t, paddr_t);
478baed8f06Sdrahn 
479baed8f06Sdrahn /*
480baed8f06Sdrahn  *	bus_dmamap_t
481baed8f06Sdrahn  *
482baed8f06Sdrahn  *	Describes a DMA mapping.
483baed8f06Sdrahn  */
484baed8f06Sdrahn struct machine_bus_dmamap {
485baed8f06Sdrahn 	/*
486baed8f06Sdrahn 	 * PRIVATE MEMBERS: not for use by machine-independent code.
487baed8f06Sdrahn 	 */
488baed8f06Sdrahn 	bus_size_t	_dm_size;	/* largest DMA transfer mappable */
489baed8f06Sdrahn 	int		_dm_segcnt;	/* number of segs this map can map */
490baed8f06Sdrahn 	bus_size_t	_dm_maxsegsz;	/* largest possible segment */
491baed8f06Sdrahn 	bus_size_t	_dm_boundary;	/* don't cross this */
492baed8f06Sdrahn 	int		_dm_flags;	/* misc. flags */
493baed8f06Sdrahn 
494baed8f06Sdrahn 	void		*_dm_cookie;	/* cookie for bus-specific functions */
495baed8f06Sdrahn 
496baed8f06Sdrahn 	/*
497baed8f06Sdrahn 	 * PUBLIC MEMBERS: these are used by machine-independent code.
498baed8f06Sdrahn 	 */
499baed8f06Sdrahn 	bus_size_t	dm_mapsize;	/* size of the mapping */
500baed8f06Sdrahn 	int		dm_nsegs;	/* # valid segments in mapping */
501baed8f06Sdrahn 	bus_dma_segment_t dm_segs[1];	/* segments; variable length */
502baed8f06Sdrahn };
503baed8f06Sdrahn 
504baed8f06Sdrahn int	generic_space_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
505baed8f06Sdrahn 	    bus_space_handle_t *);
506baed8f06Sdrahn void	generic_space_unmap(bus_space_tag_t, bus_space_handle_t, bus_size_t);
507baed8f06Sdrahn int	generic_space_region(bus_space_tag_t, bus_space_handle_t, bus_size_t,
508baed8f06Sdrahn 	    bus_size_t, bus_space_handle_t *);
509baed8f06Sdrahn void	*generic_space_vaddr(bus_space_tag_t, bus_space_handle_t);
510baed8f06Sdrahn paddr_t	generic_space_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
511baed8f06Sdrahn uint8_t generic_space_read_1(bus_space_tag_t, bus_space_handle_t, bus_size_t);
512baed8f06Sdrahn uint16_t generic_space_read_2(bus_space_tag_t, bus_space_handle_t, bus_size_t);
513baed8f06Sdrahn uint32_t generic_space_read_4(bus_space_tag_t, bus_space_handle_t, bus_size_t);
514baed8f06Sdrahn uint64_t generic_space_read_8(bus_space_tag_t, bus_space_handle_t, bus_size_t);
515baed8f06Sdrahn void	generic_space_read_raw_2(bus_space_tag_t, bus_space_handle_t,
516baed8f06Sdrahn 	    bus_addr_t, uint8_t *, bus_size_t);
517baed8f06Sdrahn void	generic_space_write_1(bus_space_tag_t, bus_space_handle_t, bus_size_t,
518baed8f06Sdrahn 	    uint8_t);
519baed8f06Sdrahn void	generic_space_write_2(bus_space_tag_t, bus_space_handle_t, bus_size_t,
520baed8f06Sdrahn 	    uint16_t);
521baed8f06Sdrahn void	generic_space_write_4(bus_space_tag_t, bus_space_handle_t, bus_size_t,
522baed8f06Sdrahn 	    uint32_t);
523baed8f06Sdrahn void	generic_space_write_8(bus_space_tag_t, bus_space_handle_t, bus_size_t,
524baed8f06Sdrahn 	    uint64_t);
525baed8f06Sdrahn void	generic_space_write_raw_2(bus_space_tag_t, bus_space_handle_t,
526baed8f06Sdrahn 	    bus_addr_t, const uint8_t *, bus_size_t);
527baed8f06Sdrahn void	generic_space_read_raw_4(bus_space_tag_t, bus_space_handle_t,
528baed8f06Sdrahn 	    bus_addr_t, uint8_t *, bus_size_t);
529baed8f06Sdrahn void	generic_space_write_raw_4(bus_space_tag_t, bus_space_handle_t,
530baed8f06Sdrahn 	    bus_addr_t, const uint8_t *, bus_size_t);
531baed8f06Sdrahn void	generic_space_read_raw_8(bus_space_tag_t, bus_space_handle_t,
532baed8f06Sdrahn 	    bus_addr_t, uint8_t *, bus_size_t);
533baed8f06Sdrahn void	generic_space_write_raw_8(bus_space_tag_t, bus_space_handle_t,
534baed8f06Sdrahn 	    bus_addr_t, const uint8_t *, bus_size_t);
535baed8f06Sdrahn 
536baed8f06Sdrahn #endif /* _MACHINE_BUS_H_ */
537