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