bus_funcs.h revision 1.1
1/*	$NetBSD: bus_funcs.h,v 1.1 2011/07/01 17:09:58 dyoung Exp $	*/
2
3/*-
4 * Copyright (c) 1997, 1998, 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Copyright (c) 1996 Carnegie-Mellon University.
35 * All rights reserved.
36 *
37 * Author: Chris G. Demetriou
38 *
39 * Permission to use, copy, modify and distribute this software and
40 * its documentation is hereby granted, provided that both the copyright
41 * notice and this permission notice appear in all copies of the
42 * software, derivative works or modified versions, and any portions
43 * thereof, and that both notices appear in supporting documentation.
44 *
45 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
47 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 *
49 * Carnegie Mellon requests users of this software to return to
50 *
51 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
52 *  School of Computer Science
53 *  Carnegie Mellon University
54 *  Pittsburgh PA 15213-3890
55 *
56 * any improvements or extensions that they make and grant Carnegie the
57 * rights to redistribute these changes.
58 */
59
60#ifndef _ALGOR_BUS_FUNCS_H_
61#define	_ALGOR_BUS_FUNCS_H_
62
63#ifdef _KERNEL
64/*
65 * Access methods for bus space.
66 */
67
68/*
69 * Utility macros; INTERNAL USE ONLY.
70 */
71#define	__bs_c(a,b)		__CONCAT(a,b)
72#define	__bs_opname(op,size)	__bs_c(__bs_c(__bs_c(bs_,op),_),size)
73
74#define	__bs_rs(sz, tn, t, h, o)					\
75	(__BUS_SPACE_ADDRESS_SANITY((h) + (o), tn, "bus addr"),		\
76	 (*(t)->__bs_opname(r,sz))((t)->bs_cookie, h, o))
77
78#define	__bs_ws(sz, tn, t, h, o, v)					\
79do {									\
80	__BUS_SPACE_ADDRESS_SANITY((h) + (o), tn, "bus addr");		\
81	(*(t)->__bs_opname(w,sz))((t)->bs_cookie, h, o, v);		\
82} while (0)
83
84#define	__bs_nonsingle(type, sz, tn, t, h, o, a, c)			\
85do {									\
86	__BUS_SPACE_ADDRESS_SANITY((a), tn, "buffer");			\
87	__BUS_SPACE_ADDRESS_SANITY((h) + (o), tn, "bus addr");		\
88	(*(t)->__bs_opname(type,sz))((t)->bs_cookie, h, o, a, c);	\
89} while (0)
90
91#define	__bs_set(type, sz, tn, t, h, o, v, c)				\
92do {									\
93	__BUS_SPACE_ADDRESS_SANITY((h) + (o), tn, "bus addr");		\
94	(*(t)->__bs_opname(type,sz))((t)->bs_cookie, h, o, v, c);	\
95} while (0)
96
97#define	__bs_copy(sz, tn, t, h1, o1, h2, o2, cnt)			\
98do {									\
99	__BUS_SPACE_ADDRESS_SANITY((h1) + (o1), tn, "bus addr 1");	\
100	__BUS_SPACE_ADDRESS_SANITY((h2) + (o2), tn, "bus addr 2");	\
101	(*(t)->__bs_opname(c,sz))((t)->bs_cookie, h1, o1, h2, o2, cnt); \
102} while (0)
103
104
105/*
106 * Mapping and unmapping operations.
107 */
108#define	bus_space_map(t, a, s, f, hp)					\
109	(*(t)->bs_map)((t)->bs_cookie, (a), (s), (f), (hp), 1)
110#define	algor_bus_space_map_noacct(t, a, s, f, hp)			\
111	(*(t)->bs_map)((t)->bs_cookie, (a), (s), (f), (hp), 0)
112#define	bus_space_unmap(t, h, s)					\
113	(*(t)->bs_unmap)((t)->bs_cookie, (h), (s), 1)
114#define	algor_bus_space_unmap_noacct(t, h, s)				\
115	(*(t)->bs_unmap)((t)->bs_cookie, (h), (s), 0)
116#define	bus_space_subregion(t, h, o, s, hp)				\
117	(*(t)->bs_subregion)((t)->bs_cookie, (h), (o), (s), (hp))
118
119#define	algor_bus_space_translate(t, a, s, f, bst)			\
120	(*(t)->bs_translate)((t)->bs_cookie, (a), (s), (f), (bst))
121#define	algor_bus_space_get_window(t, w, bst)				\
122	(*(t)->bs_get_window)((t)->bs_cookie, (w), (bst))
123
124/*
125 * Allocation and deallocation operations.
126 */
127#define	bus_space_alloc(t, rs, re, s, a, b, f, ap, hp)			\
128	(*(t)->bs_alloc)((t)->bs_cookie, (rs), (re), (s), (a), (b),	\
129	    (f), (ap), (hp))
130#define	bus_space_free(t, h, s)						\
131	(*(t)->bs_free)((t)->bs_cookie, (h), (s))
132
133/*
134 * Get kernel virtual address for ranges mapped BUS_SPACE_MAP_LINEAR.
135 */
136#define bus_space_vaddr(t, h) \
137	(*(t)->bs_vaddr)((t)->bs_cookie, (h))
138
139/*
140 * Mmap for user.
141 */
142#define	bus_space_mmap(t, a, o, p, f)					\
143	(*(t)->bs_mmap)((t)->bs_cookie, (a), (o), (p), (f))
144
145/*
146 * Bus barrier operations.
147 */
148#define	bus_space_barrier(t, h, o, l, f)				\
149	(*(t)->bs_barrier)((t)->bs_cookie, (h), (o), (l), (f))
150
151/*
152 * Bus read (single) operations.
153 */
154#define	bus_space_read_1(t, h, o)	__bs_rs(1,u_int8_t,(t),(h),(o))
155#define	bus_space_read_2(t, h, o)	__bs_rs(2,u_int16_t,(t),(h),(o))
156#define	bus_space_read_4(t, h, o)	__bs_rs(4,u_int32_t,(t),(h),(o))
157#define	bus_space_read_8(t, h, o)	__bs_rs(8,u_int64_t,(t),(h),(o))
158
159
160/*
161 * Bus read multiple operations.
162 */
163#define	bus_space_read_multi_1(t, h, o, a, c)				\
164	__bs_nonsingle(rm,1,u_int8_t,(t),(h),(o),(a),(c))
165#define	bus_space_read_multi_2(t, h, o, a, c)				\
166	__bs_nonsingle(rm,2,u_int16_t,(t),(h),(o),(a),(c))
167#define	bus_space_read_multi_4(t, h, o, a, c)				\
168	__bs_nonsingle(rm,4,u_int32_t,(t),(h),(o),(a),(c))
169#define	bus_space_read_multi_8(t, h, o, a, c)				\
170	__bs_nonsingle(rm,8,u_int64_t,(t),(h),(o),(a),(c))
171
172
173/*
174 * Bus read region operations.
175 */
176#define	bus_space_read_region_1(t, h, o, a, c)				\
177	__bs_nonsingle(rr,1,u_int8_t,(t),(h),(o),(a),(c))
178#define	bus_space_read_region_2(t, h, o, a, c)				\
179	__bs_nonsingle(rr,2,u_int16_t,(t),(h),(o),(a),(c))
180#define	bus_space_read_region_4(t, h, o, a, c)				\
181	__bs_nonsingle(rr,4,u_int32_t,(t),(h),(o),(a),(c))
182#define	bus_space_read_region_8(t, h, o, a, c)				\
183	__bs_nonsingle(rr,8,u_int64_t,(t),(h),(o),(a),(c))
184
185
186/*
187 * Bus write (single) operations.
188 */
189#define	bus_space_write_1(t, h, o, v)	__bs_ws(1,u_int8_t,(t),(h),(o),(v))
190#define	bus_space_write_2(t, h, o, v)	__bs_ws(2,u_int16_t,(t),(h),(o),(v))
191#define	bus_space_write_4(t, h, o, v)	__bs_ws(4,u_int32_t,(t),(h),(o),(v))
192#define	bus_space_write_8(t, h, o, v)	__bs_ws(8,u_int64_t,(t),(h),(o),(v))
193
194
195/*
196 * Bus write multiple operations.
197 */
198#define	bus_space_write_multi_1(t, h, o, a, c)				\
199	__bs_nonsingle(wm,1,u_int8_t,(t),(h),(o),(a),(c))
200#define	bus_space_write_multi_2(t, h, o, a, c)				\
201	__bs_nonsingle(wm,2,u_int16_t,(t),(h),(o),(a),(c))
202#define	bus_space_write_multi_4(t, h, o, a, c)				\
203	__bs_nonsingle(wm,4,u_int32_t,(t),(h),(o),(a),(c))
204#define	bus_space_write_multi_8(t, h, o, a, c)				\
205	__bs_nonsingle(wm,8,u_int64_t,(t),(h),(o),(a),(c))
206
207
208/*
209 * Bus write region operations.
210 */
211#define	bus_space_write_region_1(t, h, o, a, c)				\
212	__bs_nonsingle(wr,1,u_int8_t,(t),(h),(o),(a),(c))
213#define	bus_space_write_region_2(t, h, o, a, c)				\
214	__bs_nonsingle(wr,2,u_int16_t,(t),(h),(o),(a),(c))
215#define	bus_space_write_region_4(t, h, o, a, c)				\
216	__bs_nonsingle(wr,4,u_int32_t,(t),(h),(o),(a),(c))
217#define	bus_space_write_region_8(t, h, o, a, c)				\
218	__bs_nonsingle(wr,8,u_int64_t,(t),(h),(o),(a),(c))
219
220
221/*
222 * Set multiple operations.
223 */
224#define	bus_space_set_multi_1(t, h, o, v, c)				\
225	__bs_set(sm,1,u_int8_t,(t),(h),(o),(v),(c))
226#define	bus_space_set_multi_2(t, h, o, v, c)				\
227	__bs_set(sm,2,u_int16_t,(t),(h),(o),(v),(c))
228#define	bus_space_set_multi_4(t, h, o, v, c)				\
229	__bs_set(sm,4,u_int32_t,(t),(h),(o),(v),(c))
230#define	bus_space_set_multi_8(t, h, o, v, c)				\
231	__bs_set(sm,8,u_int64_t,(t),(h),(o),(v),(c))
232
233
234/*
235 * Set region operations.
236 */
237#define	bus_space_set_region_1(t, h, o, v, c)				\
238	__bs_set(sr,1,u_int8_t,(t),(h),(o),(v),(c))
239#define	bus_space_set_region_2(t, h, o, v, c)				\
240	__bs_set(sr,2,u_int16_t,(t),(h),(o),(v),(c))
241#define	bus_space_set_region_4(t, h, o, v, c)				\
242	__bs_set(sr,4,u_int32_t,(t),(h),(o),(v),(c))
243#define	bus_space_set_region_8(t, h, o, v, c)				\
244	__bs_set(sr,8,u_int64_t,(t),(h),(o),(v),(c))
245
246
247/*
248 * Copy region operations.
249 */
250#define	bus_space_copy_region_1(t, h1, o1, h2, o2, c)			\
251	__bs_copy(1, u_int8_t, (t), (h1), (o1), (h2), (o2), (c))
252#define	bus_space_copy_region_2(t, h1, o1, h2, o2, c)			\
253	__bs_copy(2, u_int16_t, (t), (h1), (o1), (h2), (o2), (c))
254#define	bus_space_copy_region_4(t, h1, o1, h2, o2, c)			\
255	__bs_copy(4, u_int32_t, (t), (h1), (o1), (h2), (o2), (c))
256#define	bus_space_copy_region_8(t, h1, o1, h2, o2, c)			\
257	__bs_copy(8, u_int64_t, (t), (h1), (o1), (h2), (o2), (c))
258
259/*
260 * Bus stream operations--defined in terms of non-stream counterparts
261 */
262#define bus_space_read_stream_1 bus_space_read_1
263#define bus_space_read_stream_2 bus_space_read_2
264#define bus_space_read_stream_4 bus_space_read_4
265#define	bus_space_read_stream_8 bus_space_read_8
266#define bus_space_read_multi_stream_1 bus_space_read_multi_1
267#define bus_space_read_multi_stream_2 bus_space_read_multi_2
268#define bus_space_read_multi_stream_4 bus_space_read_multi_4
269#define	bus_space_read_multi_stream_8 bus_space_read_multi_8
270#define bus_space_read_region_stream_1 bus_space_read_region_1
271#define bus_space_read_region_stream_2 bus_space_read_region_2
272#define bus_space_read_region_stream_4 bus_space_read_region_4
273#define	bus_space_read_region_stream_8 bus_space_read_region_8
274#define bus_space_write_stream_1 bus_space_write_1
275#define bus_space_write_stream_2 bus_space_write_2
276#define bus_space_write_stream_4 bus_space_write_4
277#define	bus_space_write_stream_8 bus_space_write_8
278#define bus_space_write_multi_stream_1 bus_space_write_multi_1
279#define bus_space_write_multi_stream_2 bus_space_write_multi_2
280#define bus_space_write_multi_stream_4 bus_space_write_multi_4
281#define	bus_space_write_multi_stream_8 bus_space_write_multi_8
282#define bus_space_write_region_stream_1 bus_space_write_region_1
283#define bus_space_write_region_stream_2 bus_space_write_region_2
284#define bus_space_write_region_stream_4 bus_space_write_region_4
285#define	bus_space_write_region_stream_8	bus_space_write_region_8
286
287
288/*
289 * Bus DMA methods.
290 */
291
292/* Forwards needed by prototypes below. */
293struct mbuf;
294struct uio;
295
296#define	bus_dmamap_create(t, s, n, m, b, f, p)			\
297	(*(t)->_dmamap_create)((t), (s), (n), (m), (b), (f), (p))
298#define	bus_dmamap_destroy(t, p)				\
299	(*(t)->_dmamap_destroy)((t), (p))
300#define	bus_dmamap_load(t, m, b, s, p, f)			\
301	(*(t)->_dmamap_load)((t), (m), (b), (s), (p), (f))
302#define	bus_dmamap_load_mbuf(t, m, b, f)			\
303	(*(t)->_dmamap_load_mbuf)((t), (m), (b), (f))
304#define	bus_dmamap_load_uio(t, m, u, f)				\
305	(*(t)->_dmamap_load_uio)((t), (m), (u), (f))
306#define	bus_dmamap_load_raw(t, m, sg, n, s, f)			\
307	(*(t)->_dmamap_load_raw)((t), (m), (sg), (n), (s), (f))
308#define	bus_dmamap_unload(t, p)					\
309	(*(t)->_dmamap_unload)((t), (p))
310#define	bus_dmamap_sync(t, p, o, l, ops)			\
311	(*(t)->_dmamap_sync)((t), (p), (o), (l), (ops))
312#define	bus_dmamem_alloc(t, s, a, b, sg, n, r, f)		\
313	(*(t)->_dmamem_alloc)((t), (s), (a), (b), (sg), (n), (r), (f))
314#define	bus_dmamem_free(t, sg, n)				\
315	(*(t)->_dmamem_free)((t), (sg), (n))
316#define	bus_dmamem_map(t, sg, n, s, k, f)			\
317	(*(t)->_dmamem_map)((t), (sg), (n), (s), (k), (f))
318#define	bus_dmamem_unmap(t, k, s)				\
319	(*(t)->_dmamem_unmap)((t), (k), (s))
320#define	bus_dmamem_mmap(t, sg, n, o, p, f)			\
321	(*(t)->_dmamem_mmap)((t), (sg), (n), (o), (p), (f))
322
323#define bus_dmatag_subregion(t, mna, mxa, nt, f) EOPNOTSUPP
324#define bus_dmatag_destroy(t)
325
326#ifdef _ALGOR_BUS_DMA_PRIVATE
327int	_bus_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
328	    bus_size_t, int, bus_dmamap_t *);
329void	_bus_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
330
331int	_bus_dmamap_load(bus_dma_tag_t, bus_dmamap_t,
332	    void *, bus_size_t, struct proc *, int);
333int	_bus_dmamap_load_mbuf(bus_dma_tag_t,
334	    bus_dmamap_t, struct mbuf *, int);
335int	_bus_dmamap_load_uio(bus_dma_tag_t,
336	    bus_dmamap_t, struct uio *, int);
337int	_bus_dmamap_load_raw(bus_dma_tag_t,
338	    bus_dmamap_t, bus_dma_segment_t *, int, bus_size_t, int);
339
340void	_bus_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
341void	_bus_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
342	    bus_size_t, int);
343
344int	_bus_dmamem_alloc(bus_dma_tag_t tag, bus_size_t size,
345	    bus_size_t alignment, bus_size_t boundary,
346	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags);
347int	_bus_dmamem_alloc_range(bus_dma_tag_t tag, bus_size_t size,
348	    bus_size_t alignment, bus_size_t boundary,
349	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags,
350	    paddr_t low, paddr_t high);
351void	_bus_dmamem_free(bus_dma_tag_t tag, bus_dma_segment_t *segs,
352	    int nsegs);
353int	_bus_dmamem_map(bus_dma_tag_t tag, bus_dma_segment_t *segs,
354	    int nsegs, size_t size, void **kvap, int flags);
355void	_bus_dmamem_unmap(bus_dma_tag_t tag, void *kva,
356	    size_t size);
357paddr_t	_bus_dmamem_mmap(bus_dma_tag_t tag, bus_dma_segment_t *segs,
358	    int nsegs, off_t off, int prot, int flags);
359#endif /* _ALGOR_BUS_DMA_PRIVATE */
360
361#endif /* _KERNEL */
362
363#endif /* _ALGOR_BUS_FUNCS_H_ */
364