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