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