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