iommu.c revision 1.11 1 /* $NetBSD: iommu.c,v 1.11 1997/05/24 20:16:01 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
51 struct iommu_softc {
52 struct device sc_dev; /* base device */
53 struct iommureg *sc_reg;
54 u_int sc_pagesize;
55 u_int sc_range;
56 u_int sc_dvmabase;
57 iopte_t *sc_ptes;
58 int sc_hasiocache;
59 };
60 struct iommu_softc *iommu_sc;/*XXX*/
61 int has_iocache;
62
63
64 /* autoconfiguration driver */
65 int iommu_print __P((void *, const char *));
66 void iommu_attach __P((struct device *, struct device *, void *));
67 int iommu_match __P((struct device *, struct cfdata *, void *));
68
69 struct cfattach iommu_ca = {
70 sizeof(struct iommu_softc), iommu_match, iommu_attach
71 };
72
73 struct cfdriver iommu_cd = {
74 NULL, "iommu", DV_DULL
75 };
76
77 /*
78 * Print the location of some iommu-attached device (called just
79 * before attaching that device). If `iommu' is not NULL, the
80 * device was found but not configured; print the iommu as well.
81 * Return UNCONF (config_find ignores this if the device was configured).
82 */
83 int
84 iommu_print(args, iommu)
85 void *args;
86 const char *iommu;
87 {
88 register struct confargs *ca = args;
89
90 if (iommu)
91 printf("%s at %s", ca->ca_ra.ra_name, iommu);
92 return (UNCONF);
93 }
94
95 int
96 iommu_match(parent, cf, aux)
97 struct device *parent;
98 struct cfdata *cf;
99 void *aux;
100 {
101 register struct confargs *ca = aux;
102 register struct romaux *ra = &ca->ca_ra;
103
104 if (CPU_ISSUN4OR4C)
105 return (0);
106 return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
107 }
108
109 /*
110 * Attach the iommu.
111 */
112 void
113 iommu_attach(parent, self, aux)
114 struct device *parent;
115 struct device *self;
116 void *aux;
117 {
118 #if defined(SUN4M)
119 register struct iommu_softc *sc = (struct iommu_softc *)self;
120 struct confargs oca, *ca = aux;
121 register struct romaux *ra = &ca->ca_ra;
122 register int node;
123 register char *name;
124 register u_int pbase, pa;
125 register int i, mmupcrsave, s;
126 register iopte_t *tpte_p;
127 extern u_int *kernel_iopte_table;
128 extern u_int kernel_iopte_table_pa;
129
130 /*XXX-GCC!*/mmupcrsave=0;
131 iommu_sc = sc;
132 /*
133 * XXX there is only one iommu, for now -- do not know how to
134 * address children on others
135 */
136 if (sc->sc_dev.dv_unit > 0) {
137 printf(" unsupported\n");
138 return;
139 }
140 node = ra->ra_node;
141
142 #if 0
143 if (ra->ra_vaddr)
144 sc->sc_reg = (struct iommureg *)ca->ca_ra.ra_vaddr;
145 #else
146 /*
147 * Map registers into our space. The PROM may have done this
148 * already, but I feel better if we have our own copy. Plus, the
149 * prom doesn't map the entire register set
150 *
151 * XXX struct iommureg is bigger than ra->ra_len; what are the
152 * other fields for?
153 */
154 sc->sc_reg = (struct iommureg *)
155 mapiodev(ra->ra_reg, 0, ra->ra_len);
156 #endif
157
158 sc->sc_hasiocache = node_has_property(node, "cache-coherence?");
159 if (CACHEINFO.c_enabled == 0) /* XXX - is this correct? */
160 sc->sc_hasiocache = 0;
161 has_iocache = sc->sc_hasiocache; /* Set global flag */
162
163 sc->sc_pagesize = getpropint(node, "page-size", NBPG),
164 sc->sc_range = (1 << 24) <<
165 ((sc->sc_reg->io_cr & IOMMU_CTL_RANGE) >> IOMMU_CTL_RANGESHFT);
166 #if 0
167 sc->sc_dvmabase = (0 - sc->sc_range);
168 #endif
169 pbase = (sc->sc_reg->io_bar & IOMMU_BAR_IBA) <<
170 (14 - IOMMU_BAR_IBASHFT);
171
172 /*
173 * Now we build our own copy of the IOMMU page tables. We need to
174 * do this since we're going to change the range to give us 64M of
175 * mappings, and thus we can move DVMA space down to 0xfd000000 to
176 * give us lots of space and to avoid bumping into the PROM, etc.
177 *
178 * XXX Note that this is rather messy.
179 */
180 sc->sc_ptes = (iopte_t *) kernel_iopte_table;
181
182 /*
183 * Now discache the page tables so that the IOMMU sees our
184 * changes.
185 */
186 kvm_uncache((caddr_t)sc->sc_ptes,
187 (((0 - DVMA4M_BASE)/sc->sc_pagesize) * sizeof(iopte_t)) / NBPG);
188
189 /*
190 * Ok. We've got to read in the original table using MMU bypass,
191 * and copy all of its entries to the appropriate place in our
192 * new table, even if the sizes are different.
193 * This is pretty easy since we know DVMA ends at 0xffffffff.
194 *
195 * XXX: PGOFSET, NBPG assume same page size as SRMMU
196 */
197 if (cpuinfo.cpu_vers == 4 && cpuinfo.mxcc) {
198 /* set MMU AC bit */
199 sta(SRMMU_PCR, ASI_SRMMU,
200 ((mmupcrsave = lda(SRMMU_PCR, ASI_SRMMU)) | VIKING_PCR_AC));
201 }
202
203 for (tpte_p = &sc->sc_ptes[((0 - DVMA4M_BASE)/NBPG) - 1],
204 pa = (u_int)pbase - sizeof(iopte_t) +
205 ((u_int)sc->sc_range/NBPG)*sizeof(iopte_t);
206 tpte_p >= &sc->sc_ptes[0] && pa >= (u_int)pbase;
207 tpte_p--, pa -= sizeof(iopte_t)) {
208
209 IOMMU_FLUSHPAGE(sc,
210 (tpte_p - &sc->sc_ptes[0])*NBPG + DVMA4M_BASE);
211 *tpte_p = lda(pa, ASI_BYPASS);
212 }
213 if (cpuinfo.cpu_vers == 4 && cpuinfo.mxcc) {
214 /* restore mmu after bug-avoidance */
215 sta(SRMMU_PCR, ASI_SRMMU, mmupcrsave);
216 }
217
218 /*
219 * Now we can install our new pagetable into the IOMMU
220 */
221 sc->sc_range = 0 - DVMA4M_BASE;
222 sc->sc_dvmabase = DVMA4M_BASE;
223
224 /* calculate log2(sc->sc_range/16MB) */
225 i = ffs(sc->sc_range/(1 << 24)) - 1;
226 if ((1 << i) != (sc->sc_range/(1 << 24)))
227 panic("bad iommu range: %d\n",i);
228
229 s = splhigh();
230 IOMMU_FLUSHALL(sc);
231
232 sc->sc_reg->io_cr = (sc->sc_reg->io_cr & ~IOMMU_CTL_RANGE) |
233 (i << IOMMU_CTL_RANGESHFT) | IOMMU_CTL_ME;
234 sc->sc_reg->io_bar = (kernel_iopte_table_pa >> 4) & IOMMU_BAR_IBA;
235
236 IOMMU_FLUSHALL(sc);
237 splx(s);
238
239 printf(": version %x/%x, page-size %d, range %dMB\n",
240 (sc->sc_reg->io_cr & IOMMU_CTL_VER) >> 24,
241 (sc->sc_reg->io_cr & IOMMU_CTL_IMPL) >> 28,
242 sc->sc_pagesize,
243 sc->sc_range >> 20);
244
245 /* Propagate bootpath */
246 if (ra->ra_bp != NULL && strcmp(ra->ra_bp->name, "iommu") == 0)
247 oca.ca_ra.ra_bp = ra->ra_bp + 1;
248 else
249 oca.ca_ra.ra_bp = NULL;
250
251 /*
252 * Loop through ROM children (expect Sbus among them).
253 */
254 for (node = firstchild(node); node; node = nextsibling(node)) {
255 name = getpropstring(node, "name");
256 if (!romprop(&oca.ca_ra, name, node))
257 continue;
258 oca.ca_bustype = BUS_MAIN; /* ??? */
259 (void) config_found(&sc->sc_dev, (void *)&oca, iommu_print);
260 }
261 #endif
262 }
263
264 void
265 iommu_enter(va, pa)
266 u_int va, pa;
267 {
268 struct iommu_softc *sc = iommu_sc;
269 int pte;
270
271 #ifdef DEBUG
272 if (va < sc->sc_dvmabase)
273 panic("iommu_enter: va 0x%x not in DVMA space",va);
274 #endif
275
276 pte = atop(pa) << IOPTE_PPNSHFT;
277 pte &= IOPTE_PPN;
278 pte |= IOPTE_V | IOPTE_W | (has_iocache ? IOPTE_C : 0);
279 sc->sc_ptes[atop(va - sc->sc_dvmabase)] = pte;
280 IOMMU_FLUSHPAGE(sc, va);
281 }
282
283 /*
284 * iommu_clear: clears mappings created by iommu_enter
285 */
286 void
287 iommu_remove(va, len)
288 register u_int va, len;
289 {
290 register struct iommu_softc *sc = iommu_sc;
291
292 #ifdef DEBUG
293 if (va < sc->sc_dvmabase)
294 panic("iommu_enter: va 0x%x not in DVMA space", va);
295 #endif
296
297 while (len > 0) {
298 #ifdef notyet
299 #ifdef DEBUG
300 if ((sc->sc_ptes[atop(va - sc->sc_dvmabase)] & IOPTE_V) == 0)
301 panic("iommu_clear: clearing invalid pte at va 0x%x",
302 va);
303 #endif
304 #endif
305 sc->sc_ptes[atop(va - sc->sc_dvmabase)] = 0;
306 sta(sc->sc_ptes + atop(va - sc->sc_dvmabase), ASI_BYPASS, 0);
307 IOMMU_FLUSHPAGE(sc, va);
308 len -= sc->sc_pagesize;
309 va += sc->sc_pagesize;
310 }
311 }
312
313 #if 0 /* These registers aren't there??? */
314 void
315 iommu_error()
316 {
317 struct iommu_softc *sc = X;
318 struct iommureg *iop = sc->sc_reg;
319
320 printf("iommu: afsr %x, afar %x\n", iop->io_afsr, iop->io_afar);
321 printf("iommu: mfsr %x, mfar %x\n", iop->io_mfsr, iop->io_mfar);
322 }
323 int
324 iommu_alloc(va, len)
325 u_int va, len;
326 {
327 struct iommu_softc *sc = X;
328 int off, tva, pa, iovaddr, pte;
329
330 off = (int)va & PGOFSET;
331 len = round_page(len + off);
332 va -= off;
333
334 if ((int)sc->sc_dvmacur + len > 0)
335 sc->sc_dvmacur = sc->sc_dvmabase;
336
337 iovaddr = tva = sc->sc_dvmacur;
338 sc->sc_dvmacur += len;
339 while (len) {
340 pa = pmap_extract(pmap_kernel(), va);
341
342 #define IOMMU_PPNSHIFT 8
343 #define IOMMU_V 0x00000002
344 #define IOMMU_W 0x00000004
345
346 pte = atop(pa) << IOMMU_PPNSHIFT;
347 pte |= IOMMU_V | IOMMU_W;
348 sta(sc->sc_ptes + atop(tva - sc->sc_dvmabase), ASI_BYPASS, pte);
349 sc->sc_reg->io_flushpage = tva;
350 len -= NBPG;
351 va += NBPG;
352 tva += NBPG;
353 }
354 return iovaddr + off;
355 }
356 #endif
357