Home | History | Annotate | Line # | Download | only in sparc
iommu.c revision 1.21
      1 /*	$NetBSD: iommu.c,v 1.21 1998/08/21 14:13:54 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 	bus_addr_t	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 	struct iommu_softc *sc = (struct iommu_softc *)self;
    167 	struct mainbus_attach_args *ma = aux;
    168 	int node;
    169 	struct bootpath *bp;
    170 	bus_space_handle_t bh;
    171 	u_int pbase, pa;
    172 	int i, mmupcrsave, s;
    173 	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 	bus_addr_t va;
    333 	paddr_t pa;
    334 {
    335 	struct iommu_softc *sc = iommu_sc;
    336 	int pte;
    337 
    338 #ifdef DEBUG
    339 	if (va < sc->sc_dvmabase)
    340 		panic("iommu_enter: va 0x%lx not in DVMA space", (long)va);
    341 #endif
    342 
    343 	pte = atop(pa) << IOPTE_PPNSHFT;
    344 	pte &= IOPTE_PPN;
    345 	pte |= IOPTE_V | IOPTE_W | (has_iocache ? IOPTE_C : 0);
    346 	sc->sc_ptes[atop(va - sc->sc_dvmabase)] = pte;
    347 	IOMMU_FLUSHPAGE(sc, va);
    348 }
    349 
    350 /*
    351  * iommu_clear: clears mappings created by iommu_enter
    352  */
    353 void
    354 iommu_remove(va, len)
    355 	bus_addr_t va;
    356 	bus_size_t len;
    357 {
    358 	struct iommu_softc *sc = iommu_sc;
    359 	u_int pagesz = sc->sc_pagesize;
    360 	bus_addr_t base = sc->sc_dvmabase;
    361 
    362 #ifdef DEBUG
    363 	if (va < base)
    364 		panic("iommu_enter: va 0x%lx not in DVMA space", (long)va);
    365 #endif
    366 
    367 	while ((long)len > 0) {
    368 #ifdef notyet
    369 #ifdef DEBUG
    370 		if ((sc->sc_ptes[atop(va - base)] & IOPTE_V) == 0)
    371 			panic("iommu_clear: clearing invalid pte at va 0x%lx",
    372 			      (long)va);
    373 #endif
    374 #endif
    375 		sc->sc_ptes[atop(va - base)] = 0;
    376 		IOMMU_FLUSHPAGE(sc, va);
    377 		len -= pagesz;
    378 		va += pagesz;
    379 	}
    380 }
    381 
    382 #if 0	/* These registers aren't there??? */
    383 void
    384 iommu_error()
    385 {
    386 	struct iommu_softc *sc = X;
    387 	struct iommureg *iop = sc->sc_reg;
    388 
    389 	printf("iommu: afsr 0x%x, afar 0x%x\n", iop->io_afsr, iop->io_afar);
    390 	printf("iommu: mfsr 0x%x, mfar 0x%x\n", iop->io_mfsr, iop->io_mfar);
    391 }
    392 int
    393 iommu_alloc(va, len)
    394 	u_int va, len;
    395 {
    396 	struct iommu_softc *sc = X;
    397 	int off, tva, pa, iovaddr, pte;
    398 
    399 	off = (int)va & PGOFSET;
    400 	len = round_page(len + off);
    401 	va -= off;
    402 
    403 if ((int)sc->sc_dvmacur + len > 0)
    404 	sc->sc_dvmacur = sc->sc_dvmabase;
    405 
    406 	iovaddr = tva = sc->sc_dvmacur;
    407 	sc->sc_dvmacur += len;
    408 	while (len) {
    409 		pa = pmap_extract(pmap_kernel(), va);
    410 
    411 #define IOMMU_PPNSHIFT	8
    412 #define IOMMU_V		0x00000002
    413 #define IOMMU_W		0x00000004
    414 
    415 		pte = atop(pa) << IOMMU_PPNSHIFT;
    416 		pte |= IOMMU_V | IOMMU_W;
    417 		sta(sc->sc_ptes + atop(tva - sc->sc_dvmabase), ASI_BYPASS, pte);
    418 		sc->sc_reg->io_flushpage = tva;
    419 		len -= NBPG;
    420 		va += NBPG;
    421 		tva += NBPG;
    422 	}
    423 	return iovaddr + off;
    424 }
    425 #endif
    426 
    427 
    428 /*
    429  * IOMMU DMA map functions.
    430  */
    431 int
    432 iommu_dmamap_load(t, map, buf, buflen, p, flags)
    433 	bus_dma_tag_t t;
    434 	bus_dmamap_t map;
    435 	void *buf;
    436 	bus_size_t buflen;
    437 	struct proc *p;
    438 	int flags;
    439 {
    440 	bus_size_t sgsize;
    441 	bus_addr_t dvmaddr, curaddr;
    442 	vaddr_t vaddr = (vaddr_t)buf;
    443 	pmap_t pmap;
    444 
    445 	/*
    446 	 * Make sure that on error condition we return "no valid mappings".
    447 	 */
    448 	map->dm_nsegs = 0;
    449 
    450 	if (buflen > map->_dm_size)
    451 		return (EINVAL);
    452 
    453 	sgsize = round_page(buflen + (vaddr & PGOFSET));
    454 
    455 	/*
    456 	 * XXX Need to implement "don't dma across this boundry".
    457 	 */
    458 	if (map->_dm_boundary != 0)
    459 		panic("bus_dmamap_load: boundaries not implemented");
    460 
    461 	if (extent_alloc(iommu_dvmamap, sgsize, NBPG, EX_NOBOUNDARY,
    462             EX_NOWAIT, (u_long *)&dvmaddr) != 0)
    463 		return (ENOMEM);
    464 
    465 	cpuinfo.cache_flush(buf, buflen);
    466 
    467 	/*
    468 	 * We always use just one segment.
    469 	 */
    470 	map->dm_mapsize = buflen;
    471 	map->dm_nsegs = 1;
    472 	map->dm_segs[0].ds_addr = dvmaddr + (vaddr & PGOFSET);
    473 	map->dm_segs[0].ds_len = sgsize /*was:buflen*/;
    474 
    475 	if (p != NULL)
    476 		pmap = p->p_vmspace->vm_map.pmap;
    477 	else
    478 		pmap = pmap_kernel();
    479 
    480 	for (; buflen > 0; ) {
    481 		/*
    482 		 * Get the physical address for this page.
    483 		 */
    484 		curaddr = (bus_addr_t)pmap_extract(pmap, vaddr);
    485 
    486 		/*
    487 		 * Compute the segment size, and adjust counts.
    488 		 */
    489 		sgsize = NBPG - (vaddr & PGOFSET);
    490 		if (buflen < sgsize)
    491 			sgsize = buflen;
    492 
    493 		iommu_enter(dvmaddr, curaddr & ~PGOFSET);
    494 
    495 		dvmaddr += NBPG;
    496 		vaddr += sgsize;
    497 		buflen -= sgsize;
    498 	}
    499 	return (0);
    500 }
    501 
    502 /*
    503  * Like _bus_dmamap_load(), but for mbufs.
    504  */
    505 int
    506 iommu_dmamap_load_mbuf(t, map, m, flags)
    507 	bus_dma_tag_t t;
    508 	bus_dmamap_t map;
    509 	struct mbuf *m;
    510 	int flags;
    511 {
    512 
    513 	panic("_bus_dmamap_load: not implemented");
    514 }
    515 
    516 /*
    517  * Like _bus_dmamap_load(), but for uios.
    518  */
    519 int
    520 iommu_dmamap_load_uio(t, map, uio, flags)
    521 	bus_dma_tag_t t;
    522 	bus_dmamap_t map;
    523 	struct uio *uio;
    524 	int flags;
    525 {
    526 
    527 	panic("_bus_dmamap_load_uio: not implemented");
    528 }
    529 
    530 /*
    531  * Like _bus_dmamap_load(), but for raw memory allocated with
    532  * bus_dmamem_alloc().
    533  */
    534 int
    535 iommu_dmamap_load_raw(t, map, segs, nsegs, size, flags)
    536 	bus_dma_tag_t t;
    537 	bus_dmamap_t map;
    538 	bus_dma_segment_t *segs;
    539 	int nsegs;
    540 	bus_size_t size;
    541 	int flags;
    542 {
    543 
    544 	panic("_bus_dmamap_load_raw: not implemented");
    545 }
    546 
    547 /*
    548  * Common function for unloading a DMA map.  May be called by
    549  * bus-specific DMA map unload functions.
    550  */
    551 void
    552 iommu_dmamap_unload(t, map)
    553 	bus_dma_tag_t t;
    554 	bus_dmamap_t map;
    555 {
    556 	bus_addr_t addr;
    557 	bus_size_t len;
    558 
    559 	if (map->dm_nsegs != 1)
    560 		panic("_bus_dmamap_unload: nsegs = %d", map->dm_nsegs);
    561 
    562 	addr = map->dm_segs[0].ds_addr & ~PGOFSET;
    563 	len = map->dm_segs[0].ds_len;
    564 
    565 	iommu_remove(addr, len);
    566 	if (extent_free(iommu_dvmamap, addr, len, EX_NOWAIT) != 0)
    567 		printf("warning: %ld of DVMA space lost\n", (long)len);
    568 
    569 	/* Mark the mappings as invalid. */
    570 	map->dm_mapsize = 0;
    571 	map->dm_nsegs = 0;
    572 }
    573 
    574 /*
    575  * Common function for DMA map synchronization.  May be called
    576  * by bus-specific DMA map synchronization functions.
    577  */
    578 void
    579 iommu_dmamap_sync(t, map, offset, len, ops)
    580 	bus_dma_tag_t t;
    581 	bus_dmamap_t map;
    582 	bus_addr_t offset;
    583 	bus_size_t len;
    584 	int ops;
    585 {
    586 
    587 	/*
    588 	 * XXX Should flush CPU write buffers.
    589 	 */
    590 }
    591 
    592 /*
    593  * Common function for DMA-safe memory allocation.  May be called
    594  * by bus-specific DMA memory allocation functions.
    595  */
    596 int
    597 iommu_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
    598 	bus_dma_tag_t t;
    599 	bus_size_t size, alignment, boundary;
    600 	bus_dma_segment_t *segs;
    601 	int nsegs;
    602 	int *rsegs;
    603 	int flags;
    604 {
    605 	paddr_t pa;
    606 	bus_addr_t dvmaddr;
    607 	vm_page_t m;
    608 	int error;
    609 	struct pglist *mlist;
    610 
    611 	size = round_page(size);
    612 	error = _bus_dmamem_alloc_common(t, size, alignment, boundary,
    613 					 segs, nsegs, rsegs, flags);
    614 	if (error != 0)
    615 		return (error);
    616 
    617 	if (extent_alloc(iommu_dvmamap, size, NBPG, boundary,
    618 			 (flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT,
    619 			 (u_long *)&dvmaddr) != 0)
    620 		return (ENOMEM);
    621 
    622 	/*
    623 	 * Compute the location, size, and number of segments actually
    624 	 * returned by the VM code.
    625 	 */
    626 	segs[0].ds_addr = dvmaddr;
    627 	segs[0].ds_len = size;
    628 	*rsegs = 1;
    629 
    630 	mlist = segs[0]._ds_mlist;
    631 	/* Map memory into DVMA space */
    632 	for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
    633 		pa = VM_PAGE_TO_PHYS(m);
    634 
    635 		iommu_enter(dvmaddr, pa);
    636 		dvmaddr += PAGE_SIZE;
    637 	}
    638 
    639 	return (0);
    640 }
    641 
    642 /*
    643  * Common function for freeing DMA-safe memory.  May be called by
    644  * bus-specific DMA memory free functions.
    645  */
    646 void
    647 iommu_dmamem_free(t, segs, nsegs)
    648 	bus_dma_tag_t t;
    649 	bus_dma_segment_t *segs;
    650 	int nsegs;
    651 {
    652 	bus_addr_t addr;
    653 	bus_size_t len;
    654 
    655 	if (nsegs != 1)
    656 		panic("bus_dmamem_free: nsegs = %d", nsegs);
    657 
    658 	addr = segs[0].ds_addr;
    659 	len = segs[0].ds_len;
    660 
    661 	iommu_remove(addr, len);
    662 	if (extent_free(iommu_dvmamap, addr, len, EX_NOWAIT) != 0)
    663 		printf("warning: %ld of DVMA space lost\n", (long)len);
    664 	/*
    665 	 * Return the list of pages back to the VM system.
    666 	 */
    667 	_bus_dmamem_free_common(t, segs, nsegs);
    668 }
    669 
    670 /*
    671  * Common function for mapping DMA-safe memory.  May be called by
    672  * bus-specific DMA memory map functions.
    673  */
    674 int
    675 iommu_dmamem_map(t, segs, nsegs, size, kvap, flags)
    676 	bus_dma_tag_t t;
    677 	bus_dma_segment_t *segs;
    678 	int nsegs;
    679 	size_t size;
    680 	caddr_t *kvap;
    681 	int flags;
    682 {
    683 	vm_page_t m;
    684 	vaddr_t va, sva;
    685 	bus_addr_t addr;
    686 	struct pglist *mlist;
    687 	int cbit;
    688 	size_t oversize;
    689 	u_long align;
    690 	extern int has_iocache;
    691 	extern u_long dvma_cachealign;
    692 
    693 	if (nsegs != 1)
    694 		panic("iommu_dmamem_map: nsegs = %d", nsegs);
    695 
    696 	cbit = has_iocache ? 0 : PMAP_NC;
    697 	align = dvma_cachealign ? dvma_cachealign : PAGE_SIZE;
    698 
    699 	size = round_page(size);
    700 
    701 	/*
    702 	 * Find a region of kernel virtual addresses that can accomodate
    703 	 * our aligment requirements.
    704 	 */
    705 	oversize = size + align - PAGE_SIZE;
    706 	sva = uvm_km_valloc(kernel_map, oversize);
    707 	if (sva == 0)
    708 		return (ENOMEM);
    709 
    710 	/* Compute start of aligned region */
    711 	va = sva;
    712 	va += ((segs[0].ds_addr & (align - 1)) + align - va) & (align - 1);
    713 
    714 	/* Return excess virtual addresses */
    715 	if (va != sva)
    716 		(void)uvm_unmap(kernel_map, sva, va, 0);
    717 	if (va + size != sva + oversize)
    718 		(void)uvm_unmap(kernel_map, va + size, sva + oversize, 0);
    719 
    720 
    721 	*kvap = (caddr_t)va;
    722 	mlist = segs[0]._ds_mlist;
    723 
    724 	for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
    725 
    726 		if (size == 0)
    727 			panic("iommu_dmamem_map: size botch");
    728 
    729 		addr = VM_PAGE_TO_PHYS(m);
    730 		pmap_enter(pmap_kernel(), va, addr | cbit,
    731 			   VM_PROT_READ | VM_PROT_WRITE, TRUE);
    732 #if 0
    733 			if (flags & BUS_DMA_COHERENT)
    734 				/* XXX */;
    735 #endif
    736 		va += PAGE_SIZE;
    737 		size -= PAGE_SIZE;
    738 	}
    739 
    740 	return (0);
    741 }
    742 
    743 /*
    744  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
    745  * bus-specific DMA mmap(2)'ing functions.
    746  */
    747 int
    748 iommu_dmamem_mmap(t, segs, nsegs, off, prot, flags)
    749 	bus_dma_tag_t t;
    750 	bus_dma_segment_t *segs;
    751 	int nsegs, off, prot, flags;
    752 {
    753 
    754 	panic("_bus_dmamem_mmap: not implemented");
    755 }
    756