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