iommu.c revision 1.17 1 /* $NetBSD: iommu.c,v 1.17 1998/03/30 14:21:39 pk Exp $ */
2
3 /*
4 * Copyright (c) 1996
5 * The President and Fellows of Harvard College. All rights reserved.
6 * Copyright (c) 1995 Paul Kranenburg
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Aaron Brown and
19 * Harvard University.
20 * This product includes software developed by Paul Kranenburg.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <vm/vm.h>
43
44 #include <machine/autoconf.h>
45 #include <machine/ctlreg.h>
46 #include <sparc/sparc/asm.h>
47 #include <sparc/sparc/vaddrs.h>
48 #include <sparc/sparc/cpuvar.h>
49 #include <sparc/sparc/iommureg.h>
50 #include <sparc/sparc/iommuvar.h>
51
52 struct iommu_softc {
53 struct device sc_dev; /* base device */
54 struct iommureg *sc_reg;
55 u_int sc_pagesize;
56 u_int sc_range;
57 u_int sc_dvmabase;
58 iopte_t *sc_ptes;
59 int sc_hasiocache;
60 };
61 struct iommu_softc *iommu_sc;/*XXX*/
62 int has_iocache;
63
64
65 /* autoconfiguration driver */
66 int iommu_print __P((void *, const char *));
67 void iommu_attach __P((struct device *, struct device *, void *));
68 int iommu_match __P((struct device *, struct cfdata *, void *));
69
70 struct cfattach iommu_ca = {
71 sizeof(struct iommu_softc), iommu_match, iommu_attach
72 };
73
74 /*
75 * Print the location of some iommu-attached device (called just
76 * before attaching that device). If `iommu' is not NULL, the
77 * device was found but not configured; print the iommu as well.
78 * Return UNCONF (config_find ignores this if the device was configured).
79 */
80 int
81 iommu_print(args, iommu)
82 void *args;
83 const char *iommu;
84 {
85 struct iommu_attach_args *ia = args;
86
87 if (iommu)
88 printf("%s at %s", ia->iom_name, iommu);
89 return (UNCONF);
90 }
91
92 int
93 iommu_match(parent, cf, aux)
94 struct device *parent;
95 struct cfdata *cf;
96 void *aux;
97 {
98 struct mainbus_attach_args *ma = aux;
99
100 if (CPU_ISSUN4OR4C)
101 return (0);
102 return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
103 }
104
105 /*
106 * Attach the iommu.
107 */
108 void
109 iommu_attach(parent, self, aux)
110 struct device *parent;
111 struct device *self;
112 void *aux;
113 {
114 #if defined(SUN4M)
115 register struct iommu_softc *sc = (struct iommu_softc *)self;
116 struct mainbus_attach_args *ma = aux;
117 register int node;
118 struct bootpath *bp;
119 bus_space_handle_t bh;
120 register u_int pbase, pa;
121 register int i, mmupcrsave, s;
122 register iopte_t *tpte_p;
123 extern u_int *kernel_iopte_table;
124 extern u_int kernel_iopte_table_pa;
125
126 /*XXX-GCC!*/mmupcrsave=0;
127 iommu_sc = sc;
128 /*
129 * XXX there is only one iommu, for now -- do not know how to
130 * address children on others
131 */
132 if (sc->sc_dev.dv_unit > 0) {
133 printf(" unsupported\n");
134 return;
135 }
136 node = ma->ma_node;
137
138 #if 0
139 if (ra->ra_vaddr)
140 sc->sc_reg = (struct iommureg *)ca->ca_ra.ra_vaddr;
141 #else
142 /*
143 * Map registers into our space. The PROM may have done this
144 * already, but I feel better if we have our own copy. Plus, the
145 * prom doesn't map the entire register set
146 *
147 * XXX struct iommureg is bigger than ra->ra_len; what are the
148 * other fields for?
149 */
150 if (bus_space_map2(
151 ma->ma_bustag,
152 ma->ma_iospace,
153 ma->ma_paddr,
154 sizeof(struct iommureg),
155 0,
156 0,
157 &bh) != 0) {
158 printf("iommu_attach: cannot map registers\n");
159 return;
160 }
161 sc->sc_reg = (struct iommureg *)bh;
162 #endif
163
164 sc->sc_hasiocache = node_has_property(node, "cache-coherence?");
165 if (CACHEINFO.c_enabled == 0) /* XXX - is this correct? */
166 sc->sc_hasiocache = 0;
167 has_iocache = sc->sc_hasiocache; /* Set global flag */
168
169 sc->sc_pagesize = getpropint(node, "page-size", NBPG),
170 sc->sc_range = (1 << 24) <<
171 ((sc->sc_reg->io_cr & IOMMU_CTL_RANGE) >> IOMMU_CTL_RANGESHFT);
172 #if 0
173 sc->sc_dvmabase = (0 - sc->sc_range);
174 #endif
175 pbase = (sc->sc_reg->io_bar & IOMMU_BAR_IBA) <<
176 (14 - IOMMU_BAR_IBASHFT);
177
178 /*
179 * Now we build our own copy of the IOMMU page tables. We need to
180 * do this since we're going to change the range to give us 64M of
181 * mappings, and thus we can move DVMA space down to 0xfd000000 to
182 * give us lots of space and to avoid bumping into the PROM, etc.
183 *
184 * XXX Note that this is rather messy.
185 */
186 sc->sc_ptes = (iopte_t *) kernel_iopte_table;
187
188 /*
189 * Now discache the page tables so that the IOMMU sees our
190 * changes.
191 */
192 kvm_uncache((caddr_t)sc->sc_ptes,
193 (((0 - DVMA4M_BASE)/sc->sc_pagesize) * sizeof(iopte_t)) / NBPG);
194
195 /*
196 * Ok. We've got to read in the original table using MMU bypass,
197 * and copy all of its entries to the appropriate place in our
198 * new table, even if the sizes are different.
199 * This is pretty easy since we know DVMA ends at 0xffffffff.
200 *
201 * XXX: PGOFSET, NBPG assume same page size as SRMMU
202 */
203 if (cpuinfo.cpu_impl == 4 && cpuinfo.mxcc) {
204 /* set MMU AC bit */
205 sta(SRMMU_PCR, ASI_SRMMU,
206 ((mmupcrsave = lda(SRMMU_PCR, ASI_SRMMU)) | VIKING_PCR_AC));
207 }
208
209 for (tpte_p = &sc->sc_ptes[((0 - DVMA4M_BASE)/NBPG) - 1],
210 pa = (u_int)pbase - sizeof(iopte_t) +
211 ((u_int)sc->sc_range/NBPG)*sizeof(iopte_t);
212 tpte_p >= &sc->sc_ptes[0] && pa >= (u_int)pbase;
213 tpte_p--, pa -= sizeof(iopte_t)) {
214
215 IOMMU_FLUSHPAGE(sc,
216 (tpte_p - &sc->sc_ptes[0])*NBPG + DVMA4M_BASE);
217 *tpte_p = lda(pa, ASI_BYPASS);
218 }
219 if (cpuinfo.cpu_impl == 4 && cpuinfo.mxcc) {
220 /* restore mmu after bug-avoidance */
221 sta(SRMMU_PCR, ASI_SRMMU, mmupcrsave);
222 }
223
224 /*
225 * Now we can install our new pagetable into the IOMMU
226 */
227 sc->sc_range = 0 - DVMA4M_BASE;
228 sc->sc_dvmabase = DVMA4M_BASE;
229
230 /* calculate log2(sc->sc_range/16MB) */
231 i = ffs(sc->sc_range/(1 << 24)) - 1;
232 if ((1 << i) != (sc->sc_range/(1 << 24)))
233 panic("bad iommu range: %d\n",i);
234
235 s = splhigh();
236 IOMMU_FLUSHALL(sc);
237
238 sc->sc_reg->io_cr = (sc->sc_reg->io_cr & ~IOMMU_CTL_RANGE) |
239 (i << IOMMU_CTL_RANGESHFT) | IOMMU_CTL_ME;
240 sc->sc_reg->io_bar = (kernel_iopte_table_pa >> 4) & IOMMU_BAR_IBA;
241
242 IOMMU_FLUSHALL(sc);
243 splx(s);
244
245 printf(": version 0x%x/0x%x, page-size %d, range %dMB\n",
246 (sc->sc_reg->io_cr & IOMMU_CTL_VER) >> 24,
247 (sc->sc_reg->io_cr & IOMMU_CTL_IMPL) >> 28,
248 sc->sc_pagesize,
249 sc->sc_range >> 20);
250
251 /* Propagate bootpath */
252 if (ma->ma_bp != NULL && strcmp(ma->ma_bp->name, "iommu") == 0)
253 bp = ma->ma_bp + 1;
254 else
255 bp = NULL;
256 printf("[[iommu: bootpath component: %s]]\n", bp?bp->name:"NULL!");
257
258 /*
259 * Loop through ROM children (expect Sbus among them).
260 */
261 for (node = firstchild(node); node; node = nextsibling(node)) {
262 struct iommu_attach_args ia;
263
264 bzero(&ia, sizeof ia);
265 ia.iom_name = getpropstring(node, "name");
266
267 /* Propagate BUS & DMA tags */
268 ia.iom_bustag = ma->ma_bustag;
269 ia.iom_dmatag = ma->ma_dmatag;
270 ia.iom_node = node;
271 ia.iom_bp = bp;
272 (void) config_found(&sc->sc_dev, (void *)&ia, iommu_print);
273 }
274 #endif
275 }
276
277 void
278 iommu_enter(va, pa)
279 u_int va, pa;
280 {
281 struct iommu_softc *sc = iommu_sc;
282 int pte;
283
284 #ifdef DEBUG
285 if (va < sc->sc_dvmabase)
286 panic("iommu_enter: va 0x%x not in DVMA space",va);
287 #endif
288
289 pte = atop(pa) << IOPTE_PPNSHFT;
290 pte &= IOPTE_PPN;
291 pte |= IOPTE_V | IOPTE_W | (has_iocache ? IOPTE_C : 0);
292 sc->sc_ptes[atop(va - sc->sc_dvmabase)] = pte;
293 IOMMU_FLUSHPAGE(sc, va);
294 }
295
296 /*
297 * iommu_clear: clears mappings created by iommu_enter
298 */
299 void
300 iommu_remove(va, len)
301 register u_int va, len;
302 {
303 register struct iommu_softc *sc = iommu_sc;
304
305 #ifdef DEBUG
306 if (va < sc->sc_dvmabase)
307 panic("iommu_enter: va 0x%x not in DVMA space", va);
308 #endif
309
310 while (len > 0) {
311 #ifdef notyet
312 #ifdef DEBUG
313 if ((sc->sc_ptes[atop(va - sc->sc_dvmabase)] & IOPTE_V) == 0)
314 panic("iommu_clear: clearing invalid pte at va 0x%x",
315 va);
316 #endif
317 #endif
318 sc->sc_ptes[atop(va - sc->sc_dvmabase)] = 0;
319 IOMMU_FLUSHPAGE(sc, va);
320 len -= sc->sc_pagesize;
321 va += sc->sc_pagesize;
322 }
323 }
324
325 #if 0 /* These registers aren't there??? */
326 void
327 iommu_error()
328 {
329 struct iommu_softc *sc = X;
330 struct iommureg *iop = sc->sc_reg;
331
332 printf("iommu: afsr 0x%x, afar 0x%x\n", iop->io_afsr, iop->io_afar);
333 printf("iommu: mfsr 0x%x, mfar 0x%x\n", iop->io_mfsr, iop->io_mfar);
334 }
335 int
336 iommu_alloc(va, len)
337 u_int va, len;
338 {
339 struct iommu_softc *sc = X;
340 int off, tva, pa, iovaddr, pte;
341
342 off = (int)va & PGOFSET;
343 len = round_page(len + off);
344 va -= off;
345
346 if ((int)sc->sc_dvmacur + len > 0)
347 sc->sc_dvmacur = sc->sc_dvmabase;
348
349 iovaddr = tva = sc->sc_dvmacur;
350 sc->sc_dvmacur += len;
351 while (len) {
352 pa = pmap_extract(pmap_kernel(), va);
353
354 #define IOMMU_PPNSHIFT 8
355 #define IOMMU_V 0x00000002
356 #define IOMMU_W 0x00000004
357
358 pte = atop(pa) << IOMMU_PPNSHIFT;
359 pte |= IOMMU_V | IOMMU_W;
360 sta(sc->sc_ptes + atop(tva - sc->sc_dvmabase), ASI_BYPASS, pte);
361 sc->sc_reg->io_flushpage = tva;
362 len -= NBPG;
363 va += NBPG;
364 tva += NBPG;
365 }
366 return iovaddr + off;
367 }
368 #endif
369