iommu.c revision 1.1 1 1.1 mrg /* $NetBSD: iommu.c,v 1.1 1999/06/04 13:48:48 mrg Exp $ */
2 1.1 mrg
3 1.1 mrg /*-
4 1.1 mrg * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * This code is derived from software contributed to The NetBSD Foundation
8 1.1 mrg * by Paul Kranenburg.
9 1.1 mrg *
10 1.1 mrg * Redistribution and use in source and binary forms, with or without
11 1.1 mrg * modification, are permitted provided that the following conditions
12 1.1 mrg * are met:
13 1.1 mrg * 1. Redistributions of source code must retain the above copyright
14 1.1 mrg * notice, this list of conditions and the following disclaimer.
15 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mrg * notice, this list of conditions and the following disclaimer in the
17 1.1 mrg * documentation and/or other materials provided with the distribution.
18 1.1 mrg * 3. All advertising materials mentioning features or use of this software
19 1.1 mrg * must display the following acknowledgement:
20 1.1 mrg * This product includes software developed by the NetBSD
21 1.1 mrg * Foundation, Inc. and its contributors.
22 1.1 mrg * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 mrg * contributors may be used to endorse or promote products derived
24 1.1 mrg * from this software without specific prior written permission.
25 1.1 mrg *
26 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 mrg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 mrg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 mrg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 mrg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 mrg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 mrg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 mrg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 mrg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 mrg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 mrg * POSSIBILITY OF SUCH DAMAGE.
37 1.1 mrg */
38 1.1 mrg
39 1.1 mrg /*
40 1.1 mrg * Copyright (c) 1992, 1993
41 1.1 mrg * The Regents of the University of California. All rights reserved.
42 1.1 mrg *
43 1.1 mrg * This software was developed by the Computer Systems Engineering group
44 1.1 mrg * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
45 1.1 mrg * contributed to Berkeley.
46 1.1 mrg *
47 1.1 mrg * All advertising materials mentioning features or use of this software
48 1.1 mrg * must display the following acknowledgement:
49 1.1 mrg * This product includes software developed by the University of
50 1.1 mrg * California, Lawrence Berkeley Laboratory.
51 1.1 mrg *
52 1.1 mrg * Redistribution and use in source and binary forms, with or without
53 1.1 mrg * modification, are permitted provided that the following conditions
54 1.1 mrg * are met:
55 1.1 mrg * 1. Redistributions of source code must retain the above copyright
56 1.1 mrg * notice, this list of conditions and the following disclaimer.
57 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
58 1.1 mrg * notice, this list of conditions and the following disclaimer in the
59 1.1 mrg * documentation and/or other materials provided with the distribution.
60 1.1 mrg * 3. All advertising materials mentioning features or use of this software
61 1.1 mrg * must display the following acknowledgement:
62 1.1 mrg * This product includes software developed by the University of
63 1.1 mrg * California, Berkeley and its contributors.
64 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
65 1.1 mrg * may be used to endorse or promote products derived from this software
66 1.1 mrg * without specific prior written permission.
67 1.1 mrg *
68 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 1.1 mrg * SUCH DAMAGE.
79 1.1 mrg *
80 1.1 mrg * from: NetBSD: sbus.c,v 1.13 1999/05/23 07:24:02 mrg Exp
81 1.1 mrg * from: @(#)sbus.c 8.1 (Berkeley) 6/11/93
82 1.1 mrg */
83 1.1 mrg
84 1.1 mrg #include <sys/param.h>
85 1.1 mrg #include <sys/extent.h>
86 1.1 mrg #include <sys/malloc.h>
87 1.1 mrg #include <sys/systm.h>
88 1.1 mrg #include <sys/device.h>
89 1.1 mrg #include <vm/vm.h>
90 1.1 mrg
91 1.1 mrg #include <machine/bus.h>
92 1.1 mrg #include <sparc64/sparc64/vaddrs.h>
93 1.1 mrg #include <sparc64/dev/iommureg.h>
94 1.1 mrg #include <sparc64/dev/iommuvar.h>
95 1.1 mrg
96 1.1 mrg #include <machine/autoconf.h>
97 1.1 mrg #include <machine/ctlreg.h>
98 1.1 mrg #include <machine/cpu.h>
99 1.1 mrg
100 1.1 mrg #ifdef DEBUG
101 1.1 mrg #define IDB_DVMA 0x1
102 1.1 mrg #define IDB_INTR 0x2
103 1.1 mrg int iommudebug = 0;
104 1.1 mrg #endif
105 1.1 mrg
106 1.1 mrg /*
107 1.1 mrg * initialise the UltraSPARC IOMMU (SBUS or PCI):
108 1.1 mrg * - allocate and setup the iotsb.
109 1.1 mrg * - enable the IOMMU
110 1.1 mrg * - initialise the streaming buffers
111 1.1 mrg * - create a private DVMA map.
112 1.1 mrg *
113 1.1 mrg */
114 1.1 mrg void
115 1.1 mrg iommu_init(name, is, tsbsize)
116 1.1 mrg char *name;
117 1.1 mrg struct iommu_state *is;
118 1.1 mrg int tsbsize;
119 1.1 mrg {
120 1.1 mrg
121 1.1 mrg /*
122 1.1 mrg * Setup the iommu.
123 1.1 mrg *
124 1.1 mrg * The sun4u iommu is part of the SBUS controller so we will
125 1.1 mrg * deal with it here. We could try to fake a device node so
126 1.1 mrg * we can eventually share it with the PCI bus run by psycho,
127 1.1 mrg * but I don't want to get into that sort of cruft.
128 1.1 mrg *
129 1.1 mrg * First we need to allocate a IOTSB. Problem is that the IOMMU
130 1.1 mrg * can only access the IOTSB by physical address, so all the
131 1.1 mrg * pages must be contiguous. Luckily, the smallest IOTSB size
132 1.1 mrg * is one 8K page.
133 1.1 mrg */
134 1.1 mrg if (tsbsize != 0)
135 1.1 mrg panic("tsbsize != 0; FIX ME"); /* XXX */
136 1.1 mrg
137 1.1 mrg /* we want 8K pages */
138 1.1 mrg is->is_cr = IOMMUCR_8KPG | IOMMUCR_EN;
139 1.1 mrg
140 1.1 mrg /* set the tsbsize and get the dvmabase */
141 1.1 mrg switch (tsbsize) {
142 1.1 mrg case 0:
143 1.1 mrg is->is_dvmabase = 0x1fffffff;
144 1.1 mrg is->is_cr |= IOMMUCR_TSB1K;
145 1.1 mrg break;
146 1.1 mrg
147 1.1 mrg #if 0
148 1.1 mrg /* XXX are these right? got the values from the linux driver */
149 1.1 mrg case 1:
150 1.1 mrg is->is_dvmabase = 0x3fffffff;
151 1.1 mrg is->is_cr |= IOMMUCR_TSB2K;
152 1.1 mrg break;
153 1.1 mrg
154 1.1 mrg case 2:
155 1.1 mrg is->is_dvmabase = 0x7fffffff;
156 1.1 mrg is->is_cr |= IOMMUCR_TSB4K;
157 1.1 mrg break;
158 1.1 mrg #endif
159 1.1 mrg
160 1.1 mrg default:
161 1.1 mrg panic("unknown tsbsize, fix me");
162 1.1 mrg }
163 1.1 mrg
164 1.1 mrg is->is_tsbsize = tsbsize;
165 1.1 mrg is->is_tsb = malloc(NBPG, M_DMAMAP, M_WAITOK); /* XXX */
166 1.1 mrg is->is_ptsb = pmap_extract(pmap_kernel(), (vaddr_t)is->is_tsb);
167 1.1 mrg
168 1.1 mrg #ifdef DEBUG
169 1.1 mrg if (iommudebug & IDB_DVMA)
170 1.1 mrg {
171 1.1 mrg /* Probe the iommu */
172 1.1 mrg struct iommureg *regs = is->is_iommu;
173 1.1 mrg int64_t cr, tsb;
174 1.1 mrg
175 1.1 mrg printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n", ®s->iommu_cr,
176 1.1 mrg ®s->iommu_tsb, ®s->iommu_flush);
177 1.1 mrg cr = regs->iommu_cr;
178 1.1 mrg tsb = regs->iommu_tsb;
179 1.1 mrg printf("iommu cr=%lx tsb=%lx\n", (long)cr, (long)tsb);
180 1.1 mrg printf("TSB base %p phys %p\n", (long)is->is_tsb, (long)is->is_ptsb);
181 1.1 mrg delay(1000000); /* 1 s */
182 1.1 mrg }
183 1.1 mrg #endif
184 1.1 mrg
185 1.1 mrg /*
186 1.1 mrg * Initialize streaming buffer.
187 1.1 mrg */
188 1.1 mrg is->is_flushpa = pmap_extract(pmap_kernel(), (vaddr_t)&is->is_flush);
189 1.1 mrg
190 1.1 mrg /*
191 1.1 mrg * now actually start up the IOMMU
192 1.1 mrg */
193 1.1 mrg iommu_reset(is);
194 1.1 mrg
195 1.1 mrg /*
196 1.1 mrg * Now all the hardware's working we need to allocate a dvma map.
197 1.1 mrg *
198 1.1 mrg * The IOMMU address space always ends at 0xffffe000, but the starting
199 1.1 mrg * address depends on the size of the map. The map size is 1024 * 2 ^
200 1.1 mrg * is->is_tsbsize entries, where each entry is 8 bytes. The start of
201 1.1 mrg * the map can be calculated by (0xffffe000 << (8 + is->is_tsbsize)).
202 1.1 mrg *
203 1.1 mrg * Note: the stupid IOMMU ignores the high bits of an address, so a
204 1.1 mrg * NULL DMA pointer will be translated by the first page of the IOTSB.
205 1.1 mrg * To trap bugs we'll skip the first entry in the IOTSB.
206 1.1 mrg */
207 1.1 mrg is->is_dvmamap = extent_create(name,
208 1.1 mrg IOTSB_VSTART(is->is_tsbsize) + NBPG, IOTSB_VEND,
209 1.1 mrg M_DEVBUF, 0, 0, EX_NOWAIT);
210 1.1 mrg }
211 1.1 mrg
212 1.1 mrg void
213 1.1 mrg iommu_reset(is)
214 1.1 mrg struct iommu_state *is;
215 1.1 mrg {
216 1.1 mrg
217 1.1 mrg /* Need to do 64-bit stores */
218 1.1 mrg bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_cr, 0, is->is_cr);
219 1.1 mrg bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_tsb, 0, is->is_ptsb);
220 1.1 mrg /* Enable diagnostics mode? */
221 1.1 mrg bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_ctl, 0, STRBUF_EN);
222 1.1 mrg }
223