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