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