iommuvar.h revision 1.11
1/*	$OpenBSD: iommuvar.h,v 1.11 2007/12/05 21:15:46 deraadt Exp $	*/
2/*	$NetBSD: iommuvar.h,v 1.9 2001/10/07 20:30:41 eeh Exp $	*/
3
4/*
5 * Copyright (c) 2003 Henric Jungheim
6 * Copyright (c) 1999 Matthew R. Green
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef _SPARC64_DEV_IOMMUVAR_H_
34#define _SPARC64_DEV_IOMMUVAR_H_
35
36#ifndef _SYS_TREE_H_
37#include <sys/tree.h>
38#endif
39
40#include <sys/mutex.h>
41
42/*
43 * per-Streaming Buffer state
44 */
45struct strbuf_ctl {
46	bus_space_tag_t		sb_bustag;	/* streaming buffer registers */
47	bus_space_handle_t	sb_sb;		/* Handle for our regs */
48	struct iommu_state	*sb_iommu;	/* Associated IOMMU */
49	/*
50	 * Since implementing the per-map IOMMU state, these per-STC
51	 * flush areas are not used other than as a boolean flag to indicate
52	 * the presence of a working and enabled STC.  For inconsistency's
53	 * sake, the "sb" pointers of iommu_state are sometimes used for the
54	 * same purpose.  This should be consolidated.  DEFINATELY, since
55	 * mutex operations must happen at this level.
56	 */
57	struct mutex		sb_mtx;		/* one flush at a time */
58
59	paddr_t			sb_flushpa;	/* to flush streaming buffers */
60	volatile int64_t	*sb_flush;
61};
62
63/*
64 * per-map STC flush area
65 */
66struct strbuf_flush {
67	char	sbf_area[0x80];		/* Holds 64-byte long/aligned buffer */
68	void	*sbf_flush;		/* Kernel virtual address of buffer */
69	paddr_t	sbf_flushpa;		/* Physical address of buffer area */
70};
71
72/*
73 * per-map DVMA page table
74 */
75struct iommu_page_entry {
76	SPLAY_ENTRY(iommu_page_entry) ipe_node;
77	paddr_t	ipe_pa;
78	vaddr_t	ipe_va;
79};
80struct iommu_page_map {
81	SPLAY_HEAD(iommu_page_tree, iommu_page_entry) ipm_tree;
82	int ipm_maxpage;	/* Size of allocated page map */
83	int ipm_pagecnt;	/* Number of entries in use */
84	struct iommu_page_entry	ipm_map[1];
85};
86
87/*
88 * per-map IOMMU state
89 *
90 * This is what bus_dvmamap_t'c _dm_cookie should be pointing to.
91 */
92struct iommu_map_state {
93	struct strbuf_flush ims_flush;	/* flush should be first (alignment) */
94	struct strbuf_ctl *ims_sb;	/* Link to parent */
95	int ims_flags;
96	struct iommu_page_map ims_map;	/* map must be last (array at end) */
97};
98#define IOMMU_MAP_STREAM	1
99
100/*
101 * per-IOMMU state
102 */
103struct iommu_state {
104	paddr_t			is_ptsb;	/* TSB physical address */
105	int64_t			*is_tsb;	/* TSB virtual address */
106	int			is_tsbsize;	/* 0 = 8K, ... */
107	u_int			is_dvmabase;
108	u_int			is_dvmaend;
109	int64_t			is_cr;		/* Control register value */
110	struct mutex		is_mtx;
111	struct extent		*is_dvmamap;	/* DVMA map for this instance */
112
113	struct strbuf_ctl	*is_sb[2];	/* Streaming buffers if any */
114
115	/* copies of our parents state, to allow us to be self contained */
116	bus_space_tag_t		is_bustag;	/* our bus tag */
117	bus_space_handle_t	is_iommu;	/* IOMMU registers */
118};
119
120/* interfaces for PCI/SBus code */
121void	iommu_init(char *, struct iommu_state *, int, u_int32_t);
122void	iommu_reset(struct iommu_state *);
123paddr_t iommu_extract(struct iommu_state *, vaddr_t);
124int64_t iommu_lookup_tte(struct iommu_state *, vaddr_t);
125int64_t iommu_fetch_tte(struct iommu_state *, paddr_t);
126/* bus_dma_tag_t implementation functions */
127int	iommu_dvmamap_create(bus_dma_tag_t, bus_dma_tag_t, struct strbuf_ctl *,
128	    bus_size_t, int, bus_size_t, bus_size_t, int, bus_dmamap_t *);
129void	iommu_dvmamap_destroy(bus_dma_tag_t, bus_dma_tag_t, bus_dmamap_t);
130int	iommu_dvmamap_load(bus_dma_tag_t, bus_dma_tag_t, bus_dmamap_t, void *,
131	    bus_size_t, struct proc *, int);
132void	iommu_dvmamap_unload(bus_dma_tag_t, bus_dma_tag_t, bus_dmamap_t);
133int	iommu_dvmamap_load_raw(bus_dma_tag_t, bus_dma_tag_t, bus_dmamap_t,
134	    bus_dma_segment_t *, int, bus_size_t, int);
135void	iommu_dvmamap_sync(bus_dma_tag_t, bus_dma_tag_t, bus_dmamap_t,
136	    bus_addr_t, bus_size_t, int);
137int	iommu_dvmamem_alloc(bus_dma_tag_t, bus_dma_tag_t, bus_size_t,
138	    bus_size_t, bus_size_t, bus_dma_segment_t *, int, int *, int);
139void	iommu_dvmamem_free(bus_dma_tag_t, bus_dma_tag_t, bus_dma_segment_t *,
140	    int);
141int	iommu_dvmamem_map(bus_dma_tag_t, bus_dma_tag_t, bus_dma_segment_t *,
142	    int, size_t, caddr_t *, int);
143void	iommu_dvmamem_unmap(bus_dma_tag_t, bus_dma_tag_t, caddr_t, size_t);
144
145#define IOMMUREG_READ(is, reg)				\
146	bus_space_read_8((is)->is_bustag,		\
147		(is)->is_iommu,				\
148		IOMMUREG(reg))
149
150#define IOMMUREG_WRITE(is, reg, v)			\
151	bus_space_write_8((is)->is_bustag,		\
152		(is)->is_iommu,				\
153		IOMMUREG(reg),				\
154		(v))
155
156#endif /* _SPARC64_DEV_IOMMUVAR_H_ */
157
158