xref: /netbsd-src/sys/arch/alpha/include/bus_defs.h (revision 8585484ef87f5a04d32332313cdb799625f4faf8)
1 /* $NetBSD: bus_defs.h,v 1.3 2012/02/06 02:14:13 matt 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 _ALPHA_BUS_DEFS_H_
61 #define	_ALPHA_BUS_DEFS_H_
62 
63 #include <sys/types.h>
64 #include <machine/bus_user.h>
65 
66 #if !defined(_KERNEL) && !defined(_STANDALONE)
67 #include <stdbool.h>
68 #endif
69 #include <sys/stdint.h>
70 
71 #ifdef _KERNEL
72 
73 /*
74  * Turn on BUS_SPACE_DEBUG if the global DEBUG option is enabled.
75  */
76 #if defined(DEBUG) && !defined(BUS_SPACE_DEBUG)
77 #define	BUS_SPACE_DEBUG
78 #endif
79 
80 #ifdef BUS_SPACE_DEBUG
81 #include <sys/systm.h> /* for printf() prototype */
82 /*
83  * Macros for checking the aligned-ness of pointers passed to bus
84  * space ops.  Strict alignment is required by the Alpha architecture,
85  * and a trap will occur if unaligned access is performed.  These
86  * may aid in the debugging of a broken device driver by displaying
87  * useful information about the problem.
88  */
89 #define	__BUS_SPACE_ALIGNED_ADDRESS(p, t)				\
90 	((((u_long)(p)) & (sizeof(t)-1)) == 0)
91 
92 #define	__BUS_SPACE_ADDRESS_SANITY(p, t, d)				\
93 ({									\
94 	if (__BUS_SPACE_ALIGNED_ADDRESS((p), t) == 0) {			\
95 		printf("%s 0x%lx not aligned to %lu bytes %s:%d\n",	\
96 		    d, (u_long)(p), sizeof(t), __FILE__, __LINE__);	\
97 	}								\
98 	(void) 0;							\
99 })
100 
101 #define BUS_SPACE_ALIGNED_POINTER(p, t) __BUS_SPACE_ALIGNED_ADDRESS(p, t)
102 #else
103 #define	__BUS_SPACE_ADDRESS_SANITY(p, t, d)	(void) 0
104 #define BUS_SPACE_ALIGNED_POINTER(p, t) ALIGNED_POINTER(p, t)
105 #endif /* BUS_SPACE_DEBUG */
106 #endif /* _KERNEL */
107 
108 struct alpha_bus_space_translation;
109 
110 /*
111  * Access methods for bus space.
112  */
113 typedef struct alpha_bus_space *bus_space_tag_t;
114 typedef u_long bus_space_handle_t;
115 
116 struct alpha_bus_space {
117 	/* cookie */
118 	void		*abs_cookie;
119 
120 	/* mapping/unmapping */
121 	int		(*abs_map)(void *, bus_addr_t, bus_size_t,
122 			    int, bus_space_handle_t *, int);
123 	void		(*abs_unmap)(void *, bus_space_handle_t,
124 			    bus_size_t, int);
125 	int		(*abs_subregion)(void *, bus_space_handle_t,
126 			    bus_size_t, bus_size_t, bus_space_handle_t *);
127 
128 	/* ALPHA SPECIFIC MAPPING METHOD */
129 	int		(*abs_translate)(void *, bus_addr_t, bus_size_t,
130 			    int, struct alpha_bus_space_translation *);
131 	int		(*abs_get_window)(void *, int,
132 			    struct alpha_bus_space_translation *);
133 
134 	/* allocation/deallocation */
135 	int		(*abs_alloc)(void *, bus_addr_t, bus_addr_t,
136 			    bus_size_t, bus_size_t, bus_size_t, int,
137 			    bus_addr_t *, bus_space_handle_t *);
138 	void		(*abs_free)(void *, bus_space_handle_t,
139 			    bus_size_t);
140 
141 	/* get kernel virtual address */
142 	void *		(*abs_vaddr)(void *, bus_space_handle_t);
143 
144 	/* mmap bus space for user */
145 	paddr_t		(*abs_mmap)(void *, bus_addr_t, off_t, int, int);
146 
147 	/* barrier */
148 	void		(*abs_barrier)(void *, bus_space_handle_t,
149 			    bus_size_t, bus_size_t, int);
150 
151 	/* read (single) */
152 	uint8_t	(*abs_r_1)(void *, bus_space_handle_t,
153 			    bus_size_t);
154 	uint16_t	(*abs_r_2)(void *, bus_space_handle_t,
155 			    bus_size_t);
156 	uint32_t	(*abs_r_4)(void *, bus_space_handle_t,
157 			    bus_size_t);
158 	uint64_t	(*abs_r_8)(void *, bus_space_handle_t,
159 			    bus_size_t);
160 
161 	/* read multiple */
162 	void		(*abs_rm_1)(void *, bus_space_handle_t,
163 			    bus_size_t, uint8_t *, bus_size_t);
164 	void		(*abs_rm_2)(void *, bus_space_handle_t,
165 			    bus_size_t, uint16_t *, bus_size_t);
166 	void		(*abs_rm_4)(void *, bus_space_handle_t,
167 			    bus_size_t, uint32_t *, bus_size_t);
168 	void		(*abs_rm_8)(void *, bus_space_handle_t,
169 			    bus_size_t, uint64_t *, bus_size_t);
170 
171 	/* read region */
172 	void		(*abs_rr_1)(void *, bus_space_handle_t,
173 			    bus_size_t, uint8_t *, bus_size_t);
174 	void		(*abs_rr_2)(void *, bus_space_handle_t,
175 			    bus_size_t, uint16_t *, bus_size_t);
176 	void		(*abs_rr_4)(void *, bus_space_handle_t,
177 			    bus_size_t, uint32_t *, bus_size_t);
178 	void		(*abs_rr_8)(void *, bus_space_handle_t,
179 			    bus_size_t, uint64_t *, bus_size_t);
180 
181 	/* write (single) */
182 	void		(*abs_w_1)(void *, bus_space_handle_t,
183 			    bus_size_t, uint8_t);
184 	void		(*abs_w_2)(void *, bus_space_handle_t,
185 			    bus_size_t, uint16_t);
186 	void		(*abs_w_4)(void *, bus_space_handle_t,
187 			    bus_size_t, uint32_t);
188 	void		(*abs_w_8)(void *, bus_space_handle_t,
189 			    bus_size_t, uint64_t);
190 
191 	/* write multiple */
192 	void		(*abs_wm_1)(void *, bus_space_handle_t,
193 			    bus_size_t, const uint8_t *, bus_size_t);
194 	void		(*abs_wm_2)(void *, bus_space_handle_t,
195 			    bus_size_t, const uint16_t *, bus_size_t);
196 	void		(*abs_wm_4)(void *, bus_space_handle_t,
197 			    bus_size_t, const uint32_t *, bus_size_t);
198 	void		(*abs_wm_8)(void *, bus_space_handle_t,
199 			    bus_size_t, const uint64_t *, bus_size_t);
200 
201 	/* write region */
202 	void		(*abs_wr_1)(void *, bus_space_handle_t,
203 			    bus_size_t, const uint8_t *, bus_size_t);
204 	void		(*abs_wr_2)(void *, bus_space_handle_t,
205 			    bus_size_t, const uint16_t *, bus_size_t);
206 	void		(*abs_wr_4)(void *, bus_space_handle_t,
207 			    bus_size_t, const uint32_t *, bus_size_t);
208 	void		(*abs_wr_8)(void *, bus_space_handle_t,
209 			    bus_size_t, const uint64_t *, bus_size_t);
210 
211 	/* set multiple */
212 	void		(*abs_sm_1)(void *, bus_space_handle_t,
213 			    bus_size_t, uint8_t, bus_size_t);
214 	void		(*abs_sm_2)(void *, bus_space_handle_t,
215 			    bus_size_t, uint16_t, bus_size_t);
216 	void		(*abs_sm_4)(void *, bus_space_handle_t,
217 			    bus_size_t, uint32_t, bus_size_t);
218 	void		(*abs_sm_8)(void *, bus_space_handle_t,
219 			    bus_size_t, uint64_t, bus_size_t);
220 
221 	/* set region */
222 	void		(*abs_sr_1)(void *, bus_space_handle_t,
223 			    bus_size_t, uint8_t, bus_size_t);
224 	void		(*abs_sr_2)(void *, bus_space_handle_t,
225 			    bus_size_t, uint16_t, bus_size_t);
226 	void		(*abs_sr_4)(void *, bus_space_handle_t,
227 			    bus_size_t, uint32_t, bus_size_t);
228 	void		(*abs_sr_8)(void *, bus_space_handle_t,
229 			    bus_size_t, uint64_t, bus_size_t);
230 
231 	/* copy */
232 	void		(*abs_c_1)(void *, bus_space_handle_t, bus_size_t,
233 			    bus_space_handle_t, bus_size_t, bus_size_t);
234 	void		(*abs_c_2)(void *, bus_space_handle_t, bus_size_t,
235 			    bus_space_handle_t, bus_size_t, bus_size_t);
236 	void		(*abs_c_4)(void *, bus_space_handle_t, bus_size_t,
237 			    bus_space_handle_t, bus_size_t, bus_size_t);
238 	void		(*abs_c_8)(void *, bus_space_handle_t, bus_size_t,
239 			    bus_space_handle_t, bus_size_t, bus_size_t);
240 };
241 
242 #define	BUS_SPACE_MAP_CACHEABLE		0x01
243 
244 #ifdef _KERNEL
245 
246 #define	BUS_SPACE_BARRIER_READ	0x01
247 #define	BUS_SPACE_BARRIER_WRITE	0x02
248 /*
249  * Bus stream operations--defined in terms of non-stream counterparts
250  */
251 #define __BUS_SPACE_HAS_STREAM_METHODS 1
252 
253 /*
254  * Flags used in various bus DMA methods.
255  */
256 #define	BUS_DMA_WAITOK		0x000	/* safe to sleep (pseudo-flag) */
257 #define	BUS_DMA_NOWAIT		0x001	/* not safe to sleep */
258 #define	BUS_DMA_ALLOCNOW	0x002	/* perform resource allocation now */
259 #define	BUS_DMA_COHERENT	0x004	/* hint: map memory DMA coherent */
260 #define	BUS_DMA_STREAMING	0x008	/* hint: sequential, unidirectional */
261 #define	BUS_DMA_BUS1		0x010	/* placeholders for bus functions... */
262 #define	BUS_DMA_BUS2		0x020
263 #define	BUS_DMA_BUS3		0x040
264 #define	BUS_DMA_BUS4		0x080
265 #define	BUS_DMA_READ		0x100	/* mapping is device -> memory only */
266 #define	BUS_DMA_WRITE		0x200	/* mapping is memory -> device only */
267 #define	BUS_DMA_NOCACHE		0x400	/* hint: map non-cached memory */
268 
269 /*
270  * Private flags stored in the DMA map.
271  */
272 #define	DMAMAP_NO_COALESCE	0x40000000	/* don't coalesce adjacent
273 						   segments */
274 
275 /* Forwards needed by prototypes below. */
276 struct mbuf;
277 struct uio;
278 struct alpha_sgmap;
279 
280 /*
281  * Operations performed by bus_dmamap_sync().
282  */
283 #define	BUS_DMASYNC_PREREAD	0x01	/* pre-read synchronization */
284 #define	BUS_DMASYNC_POSTREAD	0x02	/* post-read synchronization */
285 #define	BUS_DMASYNC_PREWRITE	0x04	/* pre-write synchronization */
286 #define	BUS_DMASYNC_POSTWRITE	0x08	/* post-write synchronization */
287 
288 /*
289  *	alpha_bus_t
290  *
291  *	Busses supported by NetBSD/alpha, used by internal
292  *	utility functions.  NOT TO BE USED BY MACHINE-INDEPENDENT
293  *	CODE!
294  */
295 typedef enum {
296 	ALPHA_BUS_TURBOCHANNEL,
297 	ALPHA_BUS_PCI,
298 	ALPHA_BUS_EISA,
299 	ALPHA_BUS_ISA,
300 	ALPHA_BUS_TLSB,
301 } alpha_bus_t;
302 
303 typedef struct alpha_bus_dma_tag	*bus_dma_tag_t;
304 typedef struct alpha_bus_dmamap		*bus_dmamap_t;
305 
306 #define BUS_DMA_TAG_VALID(t)    ((t) != (bus_dma_tag_t)0)
307 
308 /*
309  *	bus_dma_segment_t
310  *
311  *	Describes a single contiguous DMA transaction.  Values
312  *	are suitable for programming into DMA registers.
313  */
314 struct alpha_bus_dma_segment {
315 	bus_addr_t	ds_addr;	/* DMA address */
316 	bus_size_t	ds_len;		/* length of transfer */
317 };
318 typedef struct alpha_bus_dma_segment	bus_dma_segment_t;
319 
320 /*
321  *	bus_dma_tag_t
322  *
323  *	A machine-dependent opaque type describing the implementation of
324  *	DMA for a given bus.
325  */
326 struct alpha_bus_dma_tag {
327 	void	*_cookie;		/* cookie used in the guts */
328 	bus_addr_t _wbase;		/* DMA window base */
329 
330 	/*
331 	 * The following two members are used to chain DMA windows
332 	 * together.  If, during the course of a map load, the
333 	 * resulting physical memory address is too large to
334 	 * be addressed by the window, the next window will be
335 	 * attempted.  These would be chained together like so:
336 	 *
337 	 *	direct -> sgmap -> NULL
338 	 *  or
339 	 *	sgmap -> NULL
340 	 *  or
341 	 *	direct -> NULL
342 	 *
343 	 * If the window size is 0, it will not be checked (e.g.
344 	 * TurboChannel DMA).
345 	 */
346 	bus_size_t _wsize;
347 	struct alpha_bus_dma_tag *_next_window;
348 
349 	/*
350 	 * Some chipsets have a built-in boundary constraint, independent
351 	 * of what the device requests.  This allows that boundary to
352 	 * be specified.  If the device has a more restrictive constraint,
353 	 * the map will use that, otherwise this boundary will be used.
354 	 * This value is ignored if 0.
355 	 */
356 	bus_size_t _boundary;
357 
358 	/*
359 	 * A chipset may have more than one SGMAP window, so SGMAP
360 	 * windows also get a pointer to their SGMAP state.
361 	 */
362 	struct alpha_sgmap *_sgmap;
363 
364 	/*
365 	 * The SGMAP MMU implements a prefetch FIFO to keep data
366 	 * moving down the pipe, when doing host->bus DMA writes.
367 	 * The threshold (distance until the next page) used to
368 	 * trigger the prefetch is differnet on different chipsets,
369 	 * and we need to know what it is in order to know whether
370 	 * or not to allocate a spill page.
371 	 */
372 	bus_size_t _pfthresh;
373 
374 	/*
375 	 * Internal-use only utility methods.  NOT TO BE USED BY
376 	 * MACHINE-INDEPENDENT CODE!
377 	 */
378 	bus_dma_tag_t (*_get_tag)(bus_dma_tag_t, alpha_bus_t);
379 
380 	/*
381 	 * DMA mapping methods.
382 	 */
383 	int	(*_dmamap_create)(bus_dma_tag_t, bus_size_t, int,
384 		    bus_size_t, bus_size_t, int, bus_dmamap_t *);
385 	void	(*_dmamap_destroy)(bus_dma_tag_t, bus_dmamap_t);
386 	int	(*_dmamap_load)(bus_dma_tag_t, bus_dmamap_t, void *,
387 		    bus_size_t, struct proc *, int);
388 	int	(*_dmamap_load_mbuf)(bus_dma_tag_t, bus_dmamap_t,
389 		    struct mbuf *, int);
390 	int	(*_dmamap_load_uio)(bus_dma_tag_t, bus_dmamap_t,
391 		    struct uio *, int);
392 	int	(*_dmamap_load_raw)(bus_dma_tag_t, bus_dmamap_t,
393 		    bus_dma_segment_t *, int, bus_size_t, int);
394 	void	(*_dmamap_unload)(bus_dma_tag_t, bus_dmamap_t);
395 	void	(*_dmamap_sync)(bus_dma_tag_t, bus_dmamap_t,
396 		    bus_addr_t, bus_size_t, int);
397 
398 	/*
399 	 * DMA memory utility functions.
400 	 */
401 	int	(*_dmamem_alloc)(bus_dma_tag_t, bus_size_t, bus_size_t,
402 		    bus_size_t, bus_dma_segment_t *, int, int *, int);
403 	void	(*_dmamem_free)(bus_dma_tag_t,
404 		    bus_dma_segment_t *, int);
405 	int	(*_dmamem_map)(bus_dma_tag_t, bus_dma_segment_t *,
406 		    int, size_t, void **, int);
407 	void	(*_dmamem_unmap)(bus_dma_tag_t, void *, size_t);
408 	paddr_t	(*_dmamem_mmap)(bus_dma_tag_t, bus_dma_segment_t *,
409 		    int, off_t, int, int);
410 };
411 
412 #define	alphabus_dma_get_tag(t, b)				\
413 	(*(t)->_get_tag)(t, b)
414 
415 /*
416  *	bus_dmamap_t
417  *
418  *	Describes a DMA mapping.
419  */
420 struct alpha_bus_dmamap {
421 	/*
422 	 * PRIVATE MEMBERS: not for use my machine-independent code.
423 	 */
424 	bus_size_t	_dm_size;	/* largest DMA transfer mappable */
425 	int		_dm_segcnt;	/* number of segs this map can map */
426 	bus_size_t	_dm_maxmaxsegsz; /* fixed largest possible segment */
427 	bus_size_t	_dm_boundary;	/* don't cross this */
428 	int		_dm_flags;	/* misc. flags */
429 
430 	/*
431 	 * Private cookie to be used by the DMA back-end.
432 	 */
433 	void		*_dm_cookie;
434 
435 	/*
436 	 * The DMA window that we ended up being mapped in.
437 	 */
438 	bus_dma_tag_t	_dm_window;
439 
440 	/*
441 	 * PUBLIC MEMBERS: these are used by machine-independent code.
442 	 */
443 	bus_size_t	dm_maxsegsz;	/* largest possible segment */
444 	bus_size_t	dm_mapsize;	/* size of the mapping */
445 	int		dm_nsegs;	/* # valid segments in mapping */
446 	bus_dma_segment_t dm_segs[1];	/* segments; variable length */
447 };
448 
449 #endif /* _KERNEL */
450 
451 #endif /* _ALPHA_BUS_DEFS_H_ */
452