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