Home | History | Annotate | Line # | Download | only in sparc
iommu.c revision 1.34
      1 /*	$NetBSD: iommu.c,v 1.34 1999/05/25 23:14:07 thorpej 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/extent.h>
     41 #include <sys/malloc.h>
     42 #include <sys/queue.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 #include <vm/vm.h>
     46 #include <vm/vm_kern.h>
     47 
     48 #include <uvm/uvm_extern.h>
     49 #include <uvm/uvm.h>
     50 
     51 #define _SPARC_BUS_DMA_PRIVATE
     52 #include <machine/bus.h>
     53 #include <machine/autoconf.h>
     54 #include <machine/ctlreg.h>
     55 #include <sparc/sparc/asm.h>
     56 #include <sparc/sparc/vaddrs.h>
     57 #include <sparc/sparc/cpuvar.h>
     58 #include <sparc/sparc/iommureg.h>
     59 #include <sparc/sparc/iommuvar.h>
     60 
     61 struct iommu_softc {
     62 	struct device	sc_dev;		/* base device */
     63 	struct iommureg	*sc_reg;
     64 	u_int		sc_pagesize;
     65 	u_int		sc_range;
     66 	bus_addr_t	sc_dvmabase;
     67 	iopte_t		*sc_ptes;
     68 	int		sc_hasiocache;
     69 };
     70 struct	iommu_softc *iommu_sc;/*XXX*/
     71 int	has_iocache;
     72 u_long	dvma_cachealign;
     73 
     74 /*
     75  * Note: operations on the extent map are being protected with
     76  * splhigh(), since we cannot predict at which interrupt priority
     77  * our clients will run.
     78  */
     79 struct extent *iommu_dvmamap;
     80 
     81 
     82 /* autoconfiguration driver */
     83 int	iommu_print __P((void *, const char *));
     84 void	iommu_attach __P((struct device *, struct device *, void *));
     85 int	iommu_match __P((struct device *, struct cfdata *, void *));
     86 
     87 struct cfattach iommu_ca = {
     88 	sizeof(struct iommu_softc), iommu_match, iommu_attach
     89 };
     90 
     91 /* IOMMU DMA map functions */
     92 int	iommu_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
     93 	    bus_size_t, struct proc *, int));
     94 int	iommu_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
     95 	    struct mbuf *, int));
     96 int	iommu_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
     97 	    struct uio *, int));
     98 int	iommu_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
     99 	    bus_dma_segment_t *, int, bus_size_t, int));
    100 void	iommu_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
    101 void	iommu_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    102 	    bus_size_t, int));
    103 
    104 int	iommu_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
    105 	    bus_size_t alignment, bus_size_t boundary,
    106 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags));
    107 void	iommu_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    108 	    int nsegs));
    109 int	iommu_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    110 	    int nsegs, size_t size, caddr_t *kvap, int flags));
    111 int	iommu_dmamem_mmap __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    112 	    int nsegs, int off, int prot, int flags));
    113 
    114 
    115 struct sparc_bus_dma_tag iommu_dma_tag = {
    116 	NULL,
    117 	_bus_dmamap_create,
    118 	_bus_dmamap_destroy,
    119 	iommu_dmamap_load,
    120 	iommu_dmamap_load_mbuf,
    121 	iommu_dmamap_load_uio,
    122 	iommu_dmamap_load_raw,
    123 	iommu_dmamap_unload,
    124 	iommu_dmamap_sync,
    125 
    126 	iommu_dmamem_alloc,
    127 	iommu_dmamem_free,
    128 	iommu_dmamem_map,
    129 	_bus_dmamem_unmap,
    130 	iommu_dmamem_mmap
    131 };
    132 /*
    133  * Print the location of some iommu-attached device (called just
    134  * before attaching that device).  If `iommu' is not NULL, the
    135  * device was found but not configured; print the iommu as well.
    136  * Return UNCONF (config_find ignores this if the device was configured).
    137  */
    138 int
    139 iommu_print(args, iommu)
    140 	void *args;
    141 	const char *iommu;
    142 {
    143 	struct iommu_attach_args *ia = args;
    144 
    145 	if (iommu)
    146 		printf("%s at %s", ia->iom_name, iommu);
    147 	return (UNCONF);
    148 }
    149 
    150 int
    151 iommu_match(parent, cf, aux)
    152 	struct device *parent;
    153 	struct cfdata *cf;
    154 	void *aux;
    155 {
    156 	struct mainbus_attach_args *ma = aux;
    157 
    158 	if (CPU_ISSUN4OR4C)
    159 		return (0);
    160 	return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
    161 }
    162 
    163 /*
    164  * Attach the iommu.
    165  */
    166 void
    167 iommu_attach(parent, self, aux)
    168 	struct device *parent;
    169 	struct device *self;
    170 	void *aux;
    171 {
    172 #if defined(SUN4M)
    173 	struct iommu_softc *sc = (struct iommu_softc *)self;
    174 	struct mainbus_attach_args *ma = aux;
    175 	int node;
    176 	struct bootpath *bp;
    177 	bus_space_handle_t bh;
    178 	u_int pbase, pa;
    179 	int i, mmupcrsave, s;
    180 	iopte_t *tpte_p;
    181 	extern u_int *kernel_iopte_table;
    182 	extern u_int kernel_iopte_table_pa;
    183 
    184 /*XXX-GCC!*/mmupcrsave=0;
    185 	iommu_sc = sc;
    186 	/*
    187 	 * XXX there is only one iommu, for now -- do not know how to
    188 	 * address children on others
    189 	 */
    190 	if (sc->sc_dev.dv_unit > 0) {
    191 		printf(" unsupported\n");
    192 		return;
    193 	}
    194 	node = ma->ma_node;
    195 
    196 #if 0
    197 	if (ra->ra_vaddr)
    198 		sc->sc_reg = (struct iommureg *)ca->ca_ra.ra_vaddr;
    199 #else
    200 	/*
    201 	 * Map registers into our space. The PROM may have done this
    202 	 * already, but I feel better if we have our own copy. Plus, the
    203 	 * prom doesn't map the entire register set
    204 	 *
    205 	 * XXX struct iommureg is bigger than ra->ra_len; what are the
    206 	 *     other fields for?
    207 	 */
    208 	if (bus_space_map2(
    209 			ma->ma_bustag,
    210 			ma->ma_iospace,
    211 			ma->ma_paddr,
    212 			sizeof(struct iommureg),
    213 			0,
    214 			0,
    215 			&bh) != 0) {
    216 		printf("iommu_attach: cannot map registers\n");
    217 		return;
    218 	}
    219 	sc->sc_reg = (struct iommureg *)bh;
    220 #endif
    221 
    222 	sc->sc_hasiocache = node_has_property(node, "cache-coherence?");
    223 	if (CACHEINFO.c_enabled == 0) /* XXX - is this correct? */
    224 		sc->sc_hasiocache = 0;
    225 	has_iocache = sc->sc_hasiocache; /* Set global flag */
    226 
    227 	sc->sc_pagesize = getpropint(node, "page-size", NBPG),
    228 	sc->sc_range = (1 << 24) <<
    229 	    ((sc->sc_reg->io_cr & IOMMU_CTL_RANGE) >> IOMMU_CTL_RANGESHFT);
    230 #if 0
    231 	sc->sc_dvmabase = (0 - sc->sc_range);
    232 #endif
    233 	pbase = (sc->sc_reg->io_bar & IOMMU_BAR_IBA) <<
    234 			(14 - IOMMU_BAR_IBASHFT);
    235 
    236 	/*
    237 	 * Now we build our own copy of the IOMMU page tables. We need to
    238 	 * do this since we're going to change the range to give us 64M of
    239 	 * mappings, and thus we can move DVMA space down to 0xfd000000 to
    240 	 * give us lots of space and to avoid bumping into the PROM, etc.
    241 	 *
    242 	 * XXX Note that this is rather messy.
    243 	 */
    244 	sc->sc_ptes = (iopte_t *) kernel_iopte_table;
    245 
    246 	/*
    247 	 * Now discache the page tables so that the IOMMU sees our
    248 	 * changes.
    249 	 */
    250 	kvm_uncache((caddr_t)sc->sc_ptes,
    251 	    (((0 - IOMMU_DVMA_BASE)/sc->sc_pagesize) * sizeof(iopte_t)) / NBPG);
    252 
    253 	/*
    254 	 * Ok. We've got to read in the original table using MMU bypass,
    255 	 * and copy all of its entries to the appropriate place in our
    256 	 * new table, even if the sizes are different.
    257 	 * This is pretty easy since we know DVMA ends at 0xffffffff.
    258 	 *
    259 	 * XXX: PGOFSET, NBPG assume same page size as SRMMU
    260 	 */
    261 	if (cpuinfo.cpu_impl == 4 && cpuinfo.mxcc) {
    262 		/* set MMU AC bit */
    263 		sta(SRMMU_PCR, ASI_SRMMU,
    264 		    ((mmupcrsave = lda(SRMMU_PCR, ASI_SRMMU)) | VIKING_PCR_AC));
    265 	}
    266 
    267 	for (tpte_p = &sc->sc_ptes[((0 - IOMMU_DVMA_BASE)/NBPG) - 1],
    268 	     pa = (u_int)pbase - sizeof(iopte_t) +
    269 		   ((u_int)sc->sc_range/NBPG)*sizeof(iopte_t);
    270 	     tpte_p >= &sc->sc_ptes[0] && pa >= (u_int)pbase;
    271 	     tpte_p--, pa -= sizeof(iopte_t)) {
    272 
    273 		IOMMU_FLUSHPAGE(sc,
    274 			     (tpte_p - &sc->sc_ptes[0])*NBPG + IOMMU_DVMA_BASE);
    275 		*tpte_p = lda(pa, ASI_BYPASS);
    276 	}
    277 	if (cpuinfo.cpu_impl == 4 && cpuinfo.mxcc) {
    278 		/* restore mmu after bug-avoidance */
    279 		sta(SRMMU_PCR, ASI_SRMMU, mmupcrsave);
    280 	}
    281 
    282 	/*
    283 	 * Now we can install our new pagetable into the IOMMU
    284 	 */
    285 	sc->sc_range = 0 - IOMMU_DVMA_BASE;
    286 	sc->sc_dvmabase = IOMMU_DVMA_BASE;
    287 
    288 	/* calculate log2(sc->sc_range/16MB) */
    289 	i = ffs(sc->sc_range/(1 << 24)) - 1;
    290 	if ((1 << i) != (sc->sc_range/(1 << 24)))
    291 		panic("bad iommu range: %d\n",i);
    292 
    293 	s = splhigh();
    294 	IOMMU_FLUSHALL(sc);
    295 
    296 	sc->sc_reg->io_cr = (sc->sc_reg->io_cr & ~IOMMU_CTL_RANGE) |
    297 			  (i << IOMMU_CTL_RANGESHFT) | IOMMU_CTL_ME;
    298 	sc->sc_reg->io_bar = (kernel_iopte_table_pa >> 4) & IOMMU_BAR_IBA;
    299 
    300 	IOMMU_FLUSHALL(sc);
    301 	splx(s);
    302 
    303 	printf(": version 0x%x/0x%x, page-size %d, range %dMB\n",
    304 		(sc->sc_reg->io_cr & IOMMU_CTL_VER) >> 24,
    305 		(sc->sc_reg->io_cr & IOMMU_CTL_IMPL) >> 28,
    306 		sc->sc_pagesize,
    307 		sc->sc_range >> 20);
    308 
    309 	/* Propagate bootpath */
    310 	if (ma->ma_bp != NULL && strcmp(ma->ma_bp->name, "iommu") == 0)
    311 		bp = ma->ma_bp + 1;
    312 	else
    313 		bp = NULL;
    314 
    315 	iommu_dvmamap = extent_create("iommudvma",
    316 					IOMMU_DVMA_BASE, IOMMU_DVMA_END,
    317 					M_DEVBUF, 0, 0, EX_NOWAIT);
    318 	if (iommu_dvmamap == NULL)
    319 		panic("iommu: unable to allocate DVMA map");
    320 
    321 	/*
    322 	 * Loop through ROM children (expect Sbus among them).
    323 	 */
    324 	for (node = firstchild(node); node; node = nextsibling(node)) {
    325 		struct iommu_attach_args ia;
    326 
    327 		bzero(&ia, sizeof ia);
    328 		ia.iom_name = getpropstring(node, "name");
    329 
    330 		/* Propagate BUS & DMA tags */
    331 		ia.iom_bustag = ma->ma_bustag;
    332 		ia.iom_dmatag = &iommu_dma_tag;
    333 
    334 		ia.iom_node = node;
    335 		ia.iom_bp = bp;
    336 
    337 		ia.iom_reg = NULL;
    338 		getprop(node, "reg", sizeof(struct sbus_reg),
    339 			&ia.iom_nreg, (void **)&ia.iom_reg);
    340 
    341 		(void) config_found(&sc->sc_dev, (void *)&ia, iommu_print);
    342 		if (ia.iom_reg != NULL)
    343 			free(ia.iom_reg, M_DEVBUF);
    344 	}
    345 #endif
    346 }
    347 
    348 void
    349 iommu_enter(va, pa)
    350 	bus_addr_t va;
    351 	paddr_t pa;
    352 {
    353 	struct iommu_softc *sc = iommu_sc;
    354 	int pte;
    355 
    356 #ifdef DEBUG
    357 	if (va < sc->sc_dvmabase)
    358 		panic("iommu_enter: va 0x%lx not in DVMA space", (long)va);
    359 #endif
    360 
    361 	pte = atop(pa) << IOPTE_PPNSHFT;
    362 	pte &= IOPTE_PPN;
    363 	pte |= IOPTE_V | IOPTE_W | (has_iocache ? IOPTE_C : 0);
    364 	sc->sc_ptes[atop(va - sc->sc_dvmabase)] = pte;
    365 	IOMMU_FLUSHPAGE(sc, va);
    366 }
    367 
    368 /*
    369  * iommu_clear: clears mappings created by iommu_enter
    370  */
    371 void
    372 iommu_remove(va, len)
    373 	bus_addr_t va;
    374 	bus_size_t len;
    375 {
    376 	struct iommu_softc *sc = iommu_sc;
    377 	u_int pagesz = sc->sc_pagesize;
    378 	bus_addr_t base = sc->sc_dvmabase;
    379 
    380 #ifdef DEBUG
    381 	if (va < base)
    382 		panic("iommu_enter: va 0x%lx not in DVMA space", (long)va);
    383 #endif
    384 
    385 	while ((long)len > 0) {
    386 #ifdef notyet
    387 #ifdef DEBUG
    388 		if ((sc->sc_ptes[atop(va - base)] & IOPTE_V) == 0)
    389 			panic("iommu_clear: clearing invalid pte at va 0x%lx",
    390 			      (long)va);
    391 #endif
    392 #endif
    393 		sc->sc_ptes[atop(va - base)] = 0;
    394 		IOMMU_FLUSHPAGE(sc, va);
    395 		len -= pagesz;
    396 		va += pagesz;
    397 	}
    398 }
    399 
    400 #if 0	/* These registers aren't there??? */
    401 void
    402 iommu_error()
    403 {
    404 	struct iommu_softc *sc = X;
    405 	struct iommureg *iop = sc->sc_reg;
    406 
    407 	printf("iommu: afsr 0x%x, afar 0x%x\n", iop->io_afsr, iop->io_afar);
    408 	printf("iommu: mfsr 0x%x, mfar 0x%x\n", iop->io_mfsr, iop->io_mfar);
    409 }
    410 int
    411 iommu_alloc(va, len)
    412 	u_int va, len;
    413 {
    414 	struct iommu_softc *sc = X;
    415 	int off, tva, pa, iovaddr, pte;
    416 
    417 	off = (int)va & PGOFSET;
    418 	len = round_page(len + off);
    419 	va -= off;
    420 
    421 if ((int)sc->sc_dvmacur + len > 0)
    422 	sc->sc_dvmacur = sc->sc_dvmabase;
    423 
    424 	iovaddr = tva = sc->sc_dvmacur;
    425 	sc->sc_dvmacur += len;
    426 	while (len) {
    427 		pa = pmap_extract(pmap_kernel(), va);
    428 
    429 #define IOMMU_PPNSHIFT	8
    430 #define IOMMU_V		0x00000002
    431 #define IOMMU_W		0x00000004
    432 
    433 		pte = atop(pa) << IOMMU_PPNSHIFT;
    434 		pte |= IOMMU_V | IOMMU_W;
    435 		sta(sc->sc_ptes + atop(tva - sc->sc_dvmabase), ASI_BYPASS, pte);
    436 		sc->sc_reg->io_flushpage = tva;
    437 		len -= NBPG;
    438 		va += NBPG;
    439 		tva += NBPG;
    440 	}
    441 	return iovaddr + off;
    442 }
    443 #endif
    444 
    445 
    446 /*
    447  * IOMMU DMA map functions.
    448  */
    449 int
    450 iommu_dmamap_load(t, map, buf, buflen, p, flags)
    451 	bus_dma_tag_t t;
    452 	bus_dmamap_t map;
    453 	void *buf;
    454 	bus_size_t buflen;
    455 	struct proc *p;
    456 	int flags;
    457 {
    458 	bus_size_t sgsize;
    459 	bus_addr_t dva;
    460 	bus_addr_t boundary;
    461 	vaddr_t va = (vaddr_t)buf;
    462 	u_long align, voff;
    463 	pmap_t pmap;
    464 	int s, error;
    465 
    466 	/*
    467 	 * Remember page offset, then truncate the buffer address to
    468 	 * a page boundary.
    469 	 */
    470 	voff = va & PGOFSET;
    471 	va &= ~PGOFSET;
    472 
    473 	/*
    474 	 * Make sure that on error condition we return "no valid mappings".
    475 	 */
    476 	map->dm_nsegs = 0;
    477 
    478 	if (buflen > map->_dm_size)
    479 		return (EINVAL);
    480 
    481 	sgsize = (buflen + voff + PGOFSET) & ~PGOFSET;
    482 	align = dvma_cachealign ? dvma_cachealign : NBPG;
    483 	boundary = map->_dm_boundary;
    484 
    485 	s = splhigh();
    486 	error = extent_alloc1(iommu_dvmamap, sgsize, align, va & (align-1),
    487 			  boundary, EX_NOWAIT, (u_long *)&dva);
    488 	splx(s);
    489 
    490 	if (error != 0)
    491 		return (error);
    492 
    493 	cpuinfo.cache_flush(buf, buflen);
    494 
    495 	/*
    496 	 * We always use just one segment.
    497 	 */
    498 	map->dm_mapsize = buflen;
    499 	map->dm_nsegs = 1;
    500 	map->dm_segs[0].ds_addr = dva + voff;
    501 	map->dm_segs[0].ds_len = buflen;
    502 
    503 	if (p != NULL)
    504 		pmap = p->p_vmspace->vm_map.pmap;
    505 	else
    506 		pmap = pmap_kernel();
    507 
    508 	for (; sgsize != 0; ) {
    509 		/*
    510 		 * Get the physical address for this page.
    511 		 */
    512 		paddr_t pa = pmap_extract(pmap, va);
    513 
    514 		iommu_enter(dva, pa);
    515 
    516 		dva += NBPG;
    517 		va += NBPG;
    518 		sgsize -= NBPG;
    519 	}
    520 
    521 	return (0);
    522 }
    523 
    524 /*
    525  * Like _bus_dmamap_load(), but for mbufs.
    526  */
    527 int
    528 iommu_dmamap_load_mbuf(t, map, m, flags)
    529 	bus_dma_tag_t t;
    530 	bus_dmamap_t map;
    531 	struct mbuf *m;
    532 	int flags;
    533 {
    534 
    535 	panic("_bus_dmamap_load: not implemented");
    536 }
    537 
    538 /*
    539  * Like _bus_dmamap_load(), but for uios.
    540  */
    541 int
    542 iommu_dmamap_load_uio(t, map, uio, flags)
    543 	bus_dma_tag_t t;
    544 	bus_dmamap_t map;
    545 	struct uio *uio;
    546 	int flags;
    547 {
    548 
    549 	panic("_bus_dmamap_load_uio: not implemented");
    550 }
    551 
    552 /*
    553  * Like _bus_dmamap_load(), but for raw memory allocated with
    554  * bus_dmamem_alloc().
    555  */
    556 int
    557 iommu_dmamap_load_raw(t, map, segs, nsegs, size, flags)
    558 	bus_dma_tag_t t;
    559 	bus_dmamap_t map;
    560 	bus_dma_segment_t *segs;
    561 	int nsegs;
    562 	bus_size_t size;
    563 	int flags;
    564 {
    565 
    566 	panic("_bus_dmamap_load_raw: not implemented");
    567 }
    568 
    569 /*
    570  * Common function for unloading a DMA map.  May be called by
    571  * bus-specific DMA map unload functions.
    572  */
    573 void
    574 iommu_dmamap_unload(t, map)
    575 	bus_dma_tag_t t;
    576 	bus_dmamap_t map;
    577 {
    578 	bus_addr_t addr;
    579 	bus_size_t len;
    580 	int s, error;
    581 
    582 	if (map->dm_nsegs != 1)
    583 		panic("_bus_dmamap_unload: nsegs = %d", map->dm_nsegs);
    584 
    585 	addr = map->dm_segs[0].ds_addr;
    586 	len = map->dm_segs[0].ds_len;
    587 	len = ((addr & PGOFSET) + len + PGOFSET) & ~PGOFSET;
    588 	addr &= ~PGOFSET;
    589 
    590 	iommu_remove(addr, len);
    591 	s = splhigh();
    592 	error = extent_free(iommu_dvmamap, addr, len, EX_NOWAIT);
    593 	splx(s);
    594 	if (error != 0)
    595 		printf("warning: %ld of DVMA space lost\n", (long)len);
    596 
    597 	/* Mark the mappings as invalid. */
    598 	map->dm_mapsize = 0;
    599 	map->dm_nsegs = 0;
    600 }
    601 
    602 /*
    603  * Common function for DMA map synchronization.  May be called
    604  * by bus-specific DMA map synchronization functions.
    605  */
    606 void
    607 iommu_dmamap_sync(t, map, offset, len, ops)
    608 	bus_dma_tag_t t;
    609 	bus_dmamap_t map;
    610 	bus_addr_t offset;
    611 	bus_size_t len;
    612 	int ops;
    613 {
    614 
    615 	/*
    616 	 * XXX Should flush CPU write buffers.
    617 	 */
    618 }
    619 
    620 /*
    621  * Common function for DMA-safe memory allocation.  May be called
    622  * by bus-specific DMA memory allocation functions.
    623  */
    624 int
    625 iommu_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
    626 	bus_dma_tag_t t;
    627 	bus_size_t size, alignment, boundary;
    628 	bus_dma_segment_t *segs;
    629 	int nsegs;
    630 	int *rsegs;
    631 	int flags;
    632 {
    633 	paddr_t pa;
    634 	bus_addr_t dva;
    635 	vm_page_t m;
    636 	int s, error;
    637 	struct pglist *mlist;
    638 
    639 	size = round_page(size);
    640 	error = _bus_dmamem_alloc_common(t, size, alignment, boundary,
    641 					 segs, nsegs, rsegs, flags);
    642 	if (error != 0)
    643 		return (error);
    644 
    645 	s = splhigh();
    646 	error = extent_alloc(iommu_dvmamap, size, alignment, boundary,
    647 			     (flags & BUS_DMA_NOWAIT) == 0
    648 				? EX_WAITOK : EX_NOWAIT,
    649 			     (u_long *)&dva);
    650 	splx(s);
    651 	if (error != 0)
    652 		return (error);
    653 
    654 	/*
    655 	 * Compute the location, size, and number of segments actually
    656 	 * returned by the VM code.
    657 	 */
    658 	segs[0].ds_addr = dva;
    659 	segs[0].ds_len = size;
    660 	*rsegs = 1;
    661 
    662 	mlist = segs[0]._ds_mlist;
    663 	/* Map memory into DVMA space */
    664 	for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
    665 		pa = VM_PAGE_TO_PHYS(m);
    666 
    667 		iommu_enter(dva, pa);
    668 		dva += PAGE_SIZE;
    669 	}
    670 
    671 	return (0);
    672 }
    673 
    674 /*
    675  * Common function for freeing DMA-safe memory.  May be called by
    676  * bus-specific DMA memory free functions.
    677  */
    678 void
    679 iommu_dmamem_free(t, segs, nsegs)
    680 	bus_dma_tag_t t;
    681 	bus_dma_segment_t *segs;
    682 	int nsegs;
    683 {
    684 	bus_addr_t addr;
    685 	bus_size_t len;
    686 	int s, error;
    687 
    688 	if (nsegs != 1)
    689 		panic("bus_dmamem_free: nsegs = %d", nsegs);
    690 
    691 	addr = segs[0].ds_addr;
    692 	len = segs[0].ds_len;
    693 
    694 	iommu_remove(addr, len);
    695 	s = splhigh();
    696 	error = extent_free(iommu_dvmamap, addr, len, EX_NOWAIT);
    697 	splx(s);
    698 	if (error != 0)
    699 		printf("warning: %ld of DVMA space lost\n", (long)len);
    700 	/*
    701 	 * Return the list of pages back to the VM system.
    702 	 */
    703 	_bus_dmamem_free_common(t, segs, nsegs);
    704 }
    705 
    706 /*
    707  * Common function for mapping DMA-safe memory.  May be called by
    708  * bus-specific DMA memory map functions.
    709  */
    710 int
    711 iommu_dmamem_map(t, segs, nsegs, size, kvap, flags)
    712 	bus_dma_tag_t t;
    713 	bus_dma_segment_t *segs;
    714 	int nsegs;
    715 	size_t size;
    716 	caddr_t *kvap;
    717 	int flags;
    718 {
    719 	vm_page_t m;
    720 	vaddr_t va, sva;
    721 	bus_addr_t addr;
    722 	struct pglist *mlist;
    723 	int cbit;
    724 	size_t oversize;
    725 	u_long align;
    726 
    727 	if (nsegs != 1)
    728 		panic("iommu_dmamem_map: nsegs = %d", nsegs);
    729 
    730 	cbit = has_iocache ? 0 : PMAP_NC;
    731 	align = dvma_cachealign ? dvma_cachealign : PAGE_SIZE;
    732 
    733 	size = round_page(size);
    734 
    735 	/*
    736 	 * Find a region of kernel virtual addresses that can accomodate
    737 	 * our aligment requirements.
    738 	 */
    739 	oversize = size + align - PAGE_SIZE;
    740 	sva = uvm_km_valloc(kernel_map, oversize);
    741 	if (sva == 0)
    742 		return (ENOMEM);
    743 
    744 	/* Compute start of aligned region */
    745 	va = sva;
    746 	va += ((segs[0].ds_addr & (align - 1)) + align - va) & (align - 1);
    747 
    748 	/* Return excess virtual addresses */
    749 	if (va != sva)
    750 		(void)uvm_unmap(kernel_map, sva, va);
    751 	if (va + size != sva + oversize)
    752 		(void)uvm_unmap(kernel_map, va + size, sva + oversize);
    753 
    754 
    755 	*kvap = (caddr_t)va;
    756 	mlist = segs[0]._ds_mlist;
    757 
    758 	for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
    759 
    760 		if (size == 0)
    761 			panic("iommu_dmamem_map: size botch");
    762 
    763 		addr = VM_PAGE_TO_PHYS(m);
    764 		pmap_enter(pmap_kernel(), va, addr | cbit,
    765 		    VM_PROT_READ | VM_PROT_WRITE, TRUE,
    766 		    VM_PROT_READ | VM_PROT_WRITE);
    767 #if 0
    768 			if (flags & BUS_DMA_COHERENT)
    769 				/* XXX */;
    770 #endif
    771 		va += PAGE_SIZE;
    772 		size -= PAGE_SIZE;
    773 	}
    774 
    775 	return (0);
    776 }
    777 
    778 /*
    779  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
    780  * bus-specific DMA mmap(2)'ing functions.
    781  */
    782 int
    783 iommu_dmamem_mmap(t, segs, nsegs, off, prot, flags)
    784 	bus_dma_tag_t t;
    785 	bus_dma_segment_t *segs;
    786 	int nsegs, off, prot, flags;
    787 {
    788 
    789 	panic("_bus_dmamem_mmap: not implemented");
    790 }
    791