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