Home | History | Annotate | Line # | Download | only in sparc
iommu.c revision 1.18
      1 /*	$NetBSD: iommu.c,v 1.18 1998/07/30 22:28:44 pk Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996
      5  * 	The President and Fellows of Harvard College. All rights reserved.
      6  * Copyright (c) 1995 	Paul Kranenburg
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Aaron Brown and
     19  *	Harvard University.
     20  *	This product includes software developed by Paul Kranenburg.
     21  * 4. Neither the name of the University nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/extent.h>
     41 #include <sys/malloc.h>
     42 #include <sys/queue.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 #include <vm/vm.h>
     46 #include <vm/vm_kern.h>
     47 #include <uvm/uvm.h>
     48 
     49 #define _SPARC_BUS_DMA_PRIVATE
     50 #include <machine/bus.h>
     51 #include <machine/autoconf.h>
     52 #include <machine/ctlreg.h>
     53 #include <sparc/sparc/asm.h>
     54 #include <sparc/sparc/vaddrs.h>
     55 #include <sparc/sparc/cpuvar.h>
     56 #include <sparc/sparc/iommureg.h>
     57 #include <sparc/sparc/iommuvar.h>
     58 
     59 struct iommu_softc {
     60 	struct device	sc_dev;		/* base device */
     61 	struct iommureg	*sc_reg;
     62 	u_int		sc_pagesize;
     63 	u_int		sc_range;
     64 	u_int		sc_dvmabase;
     65 	iopte_t		*sc_ptes;
     66 	int		sc_hasiocache;
     67 };
     68 struct	iommu_softc *iommu_sc;/*XXX*/
     69 int	has_iocache;
     70 
     71 struct extent *iommu_dvmamap;
     72 
     73 
     74 /* autoconfiguration driver */
     75 int	iommu_print __P((void *, const char *));
     76 void	iommu_attach __P((struct device *, struct device *, void *));
     77 int	iommu_match __P((struct device *, struct cfdata *, void *));
     78 
     79 struct cfattach iommu_ca = {
     80 	sizeof(struct iommu_softc), iommu_match, iommu_attach
     81 };
     82 
     83 /* IOMMU DMA map functions */
     84 int	iommu_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
     85 	    bus_size_t, struct proc *, int));
     86 int	iommu_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
     87 	    struct mbuf *, int));
     88 int	iommu_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
     89 	    struct uio *, int));
     90 int	iommu_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
     91 	    bus_dma_segment_t *, int, bus_size_t, int));
     92 void	iommu_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
     93 void	iommu_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
     94 	    bus_size_t, int));
     95 
     96 int	iommu_dmamem_alloc __P((bus_dma_tag_t tag, bus_size_t size,
     97 	    bus_size_t alignment, bus_size_t boundary,
     98 	    bus_dma_segment_t *segs, int nsegs, int *rsegs, int flags));
     99 void	iommu_dmamem_free __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    100 	    int nsegs));
    101 int	iommu_dmamem_map __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    102 	    int nsegs, size_t size, caddr_t *kvap, int flags));
    103 int	iommu_dmamem_mmap __P((bus_dma_tag_t tag, bus_dma_segment_t *segs,
    104 	    int nsegs, int off, int prot, int flags));
    105 
    106 
    107 struct sparc_bus_dma_tag iommu_dma_tag = {
    108 	NULL,
    109 	_bus_dmamap_create,
    110 	_bus_dmamap_destroy,
    111 	iommu_dmamap_load,
    112 	iommu_dmamap_load_mbuf,
    113 	iommu_dmamap_load_uio,
    114 	iommu_dmamap_load_raw,
    115 	iommu_dmamap_unload,
    116 	iommu_dmamap_sync,
    117 
    118 	iommu_dmamem_alloc,
    119 	iommu_dmamem_free,
    120 	iommu_dmamem_map,
    121 	_bus_dmamem_unmap,
    122 	iommu_dmamem_mmap
    123 };
    124 /*
    125  * Print the location of some iommu-attached device (called just
    126  * before attaching that device).  If `iommu' is not NULL, the
    127  * device was found but not configured; print the iommu as well.
    128  * Return UNCONF (config_find ignores this if the device was configured).
    129  */
    130 int
    131 iommu_print(args, iommu)
    132 	void *args;
    133 	const char *iommu;
    134 {
    135 	struct iommu_attach_args *ia = args;
    136 
    137 	if (iommu)
    138 		printf("%s at %s", ia->iom_name, iommu);
    139 	return (UNCONF);
    140 }
    141 
    142 int
    143 iommu_match(parent, cf, aux)
    144 	struct device *parent;
    145 	struct cfdata *cf;
    146 	void *aux;
    147 {
    148 	struct mainbus_attach_args *ma = aux;
    149 
    150 	if (CPU_ISSUN4OR4C)
    151 		return (0);
    152 	return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
    153 }
    154 
    155 /*
    156  * Attach the iommu.
    157  */
    158 void
    159 iommu_attach(parent, self, aux)
    160 	struct device *parent;
    161 	struct device *self;
    162 	void *aux;
    163 {
    164 #if defined(SUN4M)
    165 	register struct iommu_softc *sc = (struct iommu_softc *)self;
    166 	struct mainbus_attach_args *ma = aux;
    167 	register int node;
    168 	struct bootpath *bp;
    169 	bus_space_handle_t bh;
    170 	register u_int pbase, pa;
    171 	register int i, mmupcrsave, s;
    172 	register iopte_t *tpte_p;
    173 	extern u_int *kernel_iopte_table;
    174 	extern u_int kernel_iopte_table_pa;
    175 
    176 /*XXX-GCC!*/mmupcrsave=0;
    177 	iommu_sc = sc;
    178 	/*
    179 	 * XXX there is only one iommu, for now -- do not know how to
    180 	 * address children on others
    181 	 */
    182 	if (sc->sc_dev.dv_unit > 0) {
    183 		printf(" unsupported\n");
    184 		return;
    185 	}
    186 	node = ma->ma_node;
    187 
    188 #if 0
    189 	if (ra->ra_vaddr)
    190 		sc->sc_reg = (struct iommureg *)ca->ca_ra.ra_vaddr;
    191 #else
    192 	/*
    193 	 * Map registers into our space. The PROM may have done this
    194 	 * already, but I feel better if we have our own copy. Plus, the
    195 	 * prom doesn't map the entire register set
    196 	 *
    197 	 * XXX struct iommureg is bigger than ra->ra_len; what are the
    198 	 *     other fields for?
    199 	 */
    200 	if (bus_space_map2(
    201 			ma->ma_bustag,
    202 			ma->ma_iospace,
    203 			ma->ma_paddr,
    204 			sizeof(struct iommureg),
    205 			0,
    206 			0,
    207 			&bh) != 0) {
    208 		printf("iommu_attach: cannot map registers\n");
    209 		return;
    210 	}
    211 	sc->sc_reg = (struct iommureg *)bh;
    212 #endif
    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 	sc->sc_range = (1 << 24) <<
    221 	    ((sc->sc_reg->io_cr & IOMMU_CTL_RANGE) >> IOMMU_CTL_RANGESHFT);
    222 #if 0
    223 	sc->sc_dvmabase = (0 - sc->sc_range);
    224 #endif
    225 	pbase = (sc->sc_reg->io_bar & IOMMU_BAR_IBA) <<
    226 			(14 - IOMMU_BAR_IBASHFT);
    227 
    228 	/*
    229 	 * Now we build our own copy of the IOMMU page tables. We need to
    230 	 * do this since we're going to change the range to give us 64M of
    231 	 * mappings, and thus we can move DVMA space down to 0xfd000000 to
    232 	 * give us lots of space and to avoid bumping into the PROM, etc.
    233 	 *
    234 	 * XXX Note that this is rather messy.
    235 	 */
    236 	sc->sc_ptes = (iopte_t *) kernel_iopte_table;
    237 
    238 	/*
    239 	 * Now discache the page tables so that the IOMMU sees our
    240 	 * changes.
    241 	 */
    242 	kvm_uncache((caddr_t)sc->sc_ptes,
    243 		(((0 - DVMA4M_BASE)/sc->sc_pagesize) * sizeof(iopte_t)) / NBPG);
    244 
    245 	/*
    246 	 * Ok. We've got to read in the original table using MMU bypass,
    247 	 * and copy all of its entries to the appropriate place in our
    248 	 * new table, even if the sizes are different.
    249 	 * This is pretty easy since we know DVMA ends at 0xffffffff.
    250 	 *
    251 	 * XXX: PGOFSET, NBPG assume same page size as SRMMU
    252 	 */
    253 	if (cpuinfo.cpu_impl == 4 && cpuinfo.mxcc) {
    254 		/* set MMU AC bit */
    255 		sta(SRMMU_PCR, ASI_SRMMU,
    256 		    ((mmupcrsave = lda(SRMMU_PCR, ASI_SRMMU)) | VIKING_PCR_AC));
    257 	}
    258 
    259 	for (tpte_p = &sc->sc_ptes[((0 - DVMA4M_BASE)/NBPG) - 1],
    260 	     pa = (u_int)pbase - sizeof(iopte_t) +
    261 		   ((u_int)sc->sc_range/NBPG)*sizeof(iopte_t);
    262 	     tpte_p >= &sc->sc_ptes[0] && pa >= (u_int)pbase;
    263 	     tpte_p--, pa -= sizeof(iopte_t)) {
    264 
    265 		IOMMU_FLUSHPAGE(sc,
    266 			        (tpte_p - &sc->sc_ptes[0])*NBPG + DVMA4M_BASE);
    267 		*tpte_p = lda(pa, ASI_BYPASS);
    268 	}
    269 	if (cpuinfo.cpu_impl == 4 && cpuinfo.mxcc) {
    270 		/* restore mmu after bug-avoidance */
    271 		sta(SRMMU_PCR, ASI_SRMMU, mmupcrsave);
    272 	}
    273 
    274 	/*
    275 	 * Now we can install our new pagetable into the IOMMU
    276 	 */
    277 	sc->sc_range = 0 - DVMA4M_BASE;
    278 	sc->sc_dvmabase = DVMA4M_BASE;
    279 
    280 	/* calculate log2(sc->sc_range/16MB) */
    281 	i = ffs(sc->sc_range/(1 << 24)) - 1;
    282 	if ((1 << i) != (sc->sc_range/(1 << 24)))
    283 		panic("bad iommu range: %d\n",i);
    284 
    285 	s = splhigh();
    286 	IOMMU_FLUSHALL(sc);
    287 
    288 	sc->sc_reg->io_cr = (sc->sc_reg->io_cr & ~IOMMU_CTL_RANGE) |
    289 			  (i << IOMMU_CTL_RANGESHFT) | IOMMU_CTL_ME;
    290 	sc->sc_reg->io_bar = (kernel_iopte_table_pa >> 4) & IOMMU_BAR_IBA;
    291 
    292 	IOMMU_FLUSHALL(sc);
    293 	splx(s);
    294 
    295 	printf(": version 0x%x/0x%x, page-size %d, range %dMB\n",
    296 		(sc->sc_reg->io_cr & IOMMU_CTL_VER) >> 24,
    297 		(sc->sc_reg->io_cr & IOMMU_CTL_IMPL) >> 28,
    298 		sc->sc_pagesize,
    299 		sc->sc_range >> 20);
    300 
    301 	/* Propagate bootpath */
    302 	if (ma->ma_bp != NULL && strcmp(ma->ma_bp->name, "iommu") == 0)
    303 		bp = ma->ma_bp + 1;
    304 	else
    305 		bp = NULL;
    306 
    307 	iommu_dvmamap = extent_create("iommudvma", DVMA4M_BASE, DVMA4M_END,
    308 					M_DEVBUF, 0, 0, EX_NOWAIT);
    309 
    310 	/*
    311 	 * Loop through ROM children (expect Sbus among them).
    312 	 */
    313 	for (node = firstchild(node); node; node = nextsibling(node)) {
    314 		struct iommu_attach_args ia;
    315 
    316 		bzero(&ia, sizeof ia);
    317 		ia.iom_name = getpropstring(node, "name");
    318 
    319 		/* Propagate BUS & DMA tags */
    320 		ia.iom_bustag = ma->ma_bustag;
    321 		ia.iom_dmatag = &iommu_dma_tag;
    322 		ia.iom_node = node;
    323 		ia.iom_bp = bp;
    324 		(void) config_found(&sc->sc_dev, (void *)&ia, iommu_print);
    325 	}
    326 #endif
    327 }
    328 
    329 void
    330 iommu_enter(va, pa)
    331 	u_int va, pa;
    332 {
    333 	struct iommu_softc *sc = iommu_sc;
    334 	int pte;
    335 
    336 #ifdef DEBUG
    337 	if (va < sc->sc_dvmabase)
    338 		panic("iommu_enter: va 0x%x not in DVMA space",va);
    339 #endif
    340 
    341 	pte = atop(pa) << IOPTE_PPNSHFT;
    342 	pte &= IOPTE_PPN;
    343 	pte |= IOPTE_V | IOPTE_W | (has_iocache ? IOPTE_C : 0);
    344 	sc->sc_ptes[atop(va - sc->sc_dvmabase)] = pte;
    345 	IOMMU_FLUSHPAGE(sc, va);
    346 }
    347 
    348 /*
    349  * iommu_clear: clears mappings created by iommu_enter
    350  */
    351 void
    352 iommu_remove(va, len)
    353 	register u_int va, len;
    354 {
    355 	register struct iommu_softc *sc = iommu_sc;
    356 
    357 #ifdef DEBUG
    358 	if (va < sc->sc_dvmabase)
    359 		panic("iommu_enter: va 0x%x not in DVMA space", va);
    360 #endif
    361 
    362 	while (len > 0) {
    363 #ifdef notyet
    364 #ifdef DEBUG
    365 		if ((sc->sc_ptes[atop(va - sc->sc_dvmabase)] & IOPTE_V) == 0)
    366 			panic("iommu_clear: clearing invalid pte at va 0x%x",
    367 				va);
    368 #endif
    369 #endif
    370 		sc->sc_ptes[atop(va - sc->sc_dvmabase)] = 0;
    371 		IOMMU_FLUSHPAGE(sc, va);
    372 		len -= sc->sc_pagesize;
    373 		va += sc->sc_pagesize;
    374 	}
    375 }
    376 
    377 #if 0	/* These registers aren't there??? */
    378 void
    379 iommu_error()
    380 {
    381 	struct iommu_softc *sc = X;
    382 	struct iommureg *iop = sc->sc_reg;
    383 
    384 	printf("iommu: afsr 0x%x, afar 0x%x\n", iop->io_afsr, iop->io_afar);
    385 	printf("iommu: mfsr 0x%x, mfar 0x%x\n", iop->io_mfsr, iop->io_mfar);
    386 }
    387 int
    388 iommu_alloc(va, len)
    389 	u_int va, len;
    390 {
    391 	struct iommu_softc *sc = X;
    392 	int off, tva, pa, iovaddr, pte;
    393 
    394 	off = (int)va & PGOFSET;
    395 	len = round_page(len + off);
    396 	va -= off;
    397 
    398 if ((int)sc->sc_dvmacur + len > 0)
    399 	sc->sc_dvmacur = sc->sc_dvmabase;
    400 
    401 	iovaddr = tva = sc->sc_dvmacur;
    402 	sc->sc_dvmacur += len;
    403 	while (len) {
    404 		pa = pmap_extract(pmap_kernel(), va);
    405 
    406 #define IOMMU_PPNSHIFT	8
    407 #define IOMMU_V		0x00000002
    408 #define IOMMU_W		0x00000004
    409 
    410 		pte = atop(pa) << IOMMU_PPNSHIFT;
    411 		pte |= IOMMU_V | IOMMU_W;
    412 		sta(sc->sc_ptes + atop(tva - sc->sc_dvmabase), ASI_BYPASS, pte);
    413 		sc->sc_reg->io_flushpage = tva;
    414 		len -= NBPG;
    415 		va += NBPG;
    416 		tva += NBPG;
    417 	}
    418 	return iovaddr + off;
    419 }
    420 #endif
    421 
    422 
    423 /*
    424  * IOMMU DMA map functions.
    425  */
    426 int
    427 iommu_dmamap_load(t, map, buf, buflen, p, flags)
    428 	bus_dma_tag_t t;
    429 	bus_dmamap_t map;
    430 	void *buf;
    431 	bus_size_t buflen;
    432 	struct proc *p;
    433 	int flags;
    434 {
    435 	bus_size_t sgsize;
    436 	bus_addr_t dvmaddr, curaddr;
    437 	vm_offset_t vaddr = (vm_offset_t)buf;
    438 	pmap_t pmap;
    439 
    440 	/*
    441 	 * Make sure that on error condition we return "no valid mappings".
    442 	 */
    443 	map->dm_nsegs = 0;
    444 
    445 	if (buflen > map->_dm_size)
    446 		return (EINVAL);
    447 
    448 	sgsize = round_page(buflen + (vaddr & PGOFSET));
    449 
    450 	/*
    451 	 * XXX Need to implement "don't dma across this boundry".
    452 	 */
    453 	if (map->_dm_boundary != 0)
    454 		panic("bus_dmamap_load: boundaries not implemented");
    455 
    456 	if (extent_alloc(iommu_dvmamap, sgsize, NBPG, EX_NOBOUNDARY,
    457             EX_NOWAIT, (u_long *)&dvmaddr) != 0)
    458 		return (ENOMEM);
    459 
    460 	cpuinfo.cache_flush(buf, buflen);
    461 
    462 	/*
    463 	 * We always use just one segment.
    464 	 */
    465 	map->dm_mapsize = buflen;
    466 	map->dm_nsegs = 1;
    467 	map->dm_segs[0].ds_addr = dvmaddr + (vaddr & PGOFSET);
    468 	map->dm_segs[0].ds_len = sgsize /*was:buflen*/;
    469 
    470 	if (p != NULL)
    471 		pmap = p->p_vmspace->vm_map.pmap;
    472 	else
    473 		pmap = pmap_kernel();
    474 
    475 	for (; buflen > 0; ) {
    476 		/*
    477 		 * Get the physical address for this page.
    478 		 */
    479 		curaddr = (bus_addr_t)pmap_extract(pmap, vaddr);
    480 
    481 		/*
    482 		 * Compute the segment size, and adjust counts.
    483 		 */
    484 		sgsize = NBPG - (vaddr & PGOFSET);
    485 		if (buflen < sgsize)
    486 			sgsize = buflen;
    487 
    488 		iommu_enter(dvmaddr, curaddr & ~PGOFSET);
    489 
    490 		dvmaddr += NBPG;
    491 		vaddr += sgsize;
    492 		buflen -= sgsize;
    493 	}
    494 	return (0);
    495 }
    496 
    497 /*
    498  * Like _bus_dmamap_load(), but for mbufs.
    499  */
    500 int
    501 iommu_dmamap_load_mbuf(t, map, m, flags)
    502 	bus_dma_tag_t t;
    503 	bus_dmamap_t map;
    504 	struct mbuf *m;
    505 	int flags;
    506 {
    507 
    508 	panic("_bus_dmamap_load: not implemented");
    509 }
    510 
    511 /*
    512  * Like _bus_dmamap_load(), but for uios.
    513  */
    514 int
    515 iommu_dmamap_load_uio(t, map, uio, flags)
    516 	bus_dma_tag_t t;
    517 	bus_dmamap_t map;
    518 	struct uio *uio;
    519 	int flags;
    520 {
    521 
    522 	panic("_bus_dmamap_load_uio: not implemented");
    523 }
    524 
    525 /*
    526  * Like _bus_dmamap_load(), but for raw memory allocated with
    527  * bus_dmamem_alloc().
    528  */
    529 int
    530 iommu_dmamap_load_raw(t, map, segs, nsegs, size, flags)
    531 	bus_dma_tag_t t;
    532 	bus_dmamap_t map;
    533 	bus_dma_segment_t *segs;
    534 	int nsegs;
    535 	bus_size_t size;
    536 	int flags;
    537 {
    538 
    539 	panic("_bus_dmamap_load_raw: not implemented");
    540 }
    541 
    542 /*
    543  * Common function for unloading a DMA map.  May be called by
    544  * bus-specific DMA map unload functions.
    545  */
    546 void
    547 iommu_dmamap_unload(t, map)
    548 	bus_dma_tag_t t;
    549 	bus_dmamap_t map;
    550 {
    551 	bus_addr_t addr;
    552 	bus_size_t len;
    553 
    554 	if (map->dm_nsegs != 1)
    555 		panic("_bus_dmamap_unload: nsegs = %d", map->dm_nsegs);
    556 
    557 	addr = map->dm_segs[0].ds_addr & ~PGOFSET;
    558 	len = map->dm_segs[0].ds_len;
    559 
    560 	iommu_remove(addr, len);
    561 	if (extent_free(iommu_dvmamap, addr, len, EX_NOWAIT) != 0)
    562 		printf("warning: %ld of DVMA space lost\n", len);
    563 
    564 	/* Mark the mappings as invalid. */
    565 	map->dm_mapsize = 0;
    566 	map->dm_nsegs = 0;
    567 }
    568 
    569 /*
    570  * Common function for DMA map synchronization.  May be called
    571  * by bus-specific DMA map synchronization functions.
    572  */
    573 void
    574 iommu_dmamap_sync(t, map, offset, len, ops)
    575 	bus_dma_tag_t t;
    576 	bus_dmamap_t map;
    577 	bus_addr_t offset;
    578 	bus_size_t len;
    579 	int ops;
    580 {
    581 
    582 	/*
    583 	 * XXX Should flush CPU write buffers.
    584 	 */
    585 }
    586 
    587 /*
    588  * Common function for DMA-safe memory allocation.  May be called
    589  * by bus-specific DMA memory allocation functions.
    590  */
    591 int
    592 iommu_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
    593 	bus_dma_tag_t t;
    594 	bus_size_t size, alignment, boundary;
    595 	bus_dma_segment_t *segs;
    596 	int nsegs;
    597 	int *rsegs;
    598 	int flags;
    599 {
    600 	vm_offset_t curaddr;
    601 	bus_addr_t dvmaddr;
    602 	vm_page_t m;
    603 	int error;
    604 	struct pglist *mlist;
    605 
    606 	size = round_page(size);
    607 	error = _bus_dmamem_alloc_common(t, size, alignment, boundary,
    608 					 segs, nsegs, rsegs, flags);
    609 	if (error != 0)
    610 		return (error);
    611 
    612 	if (extent_alloc(iommu_dvmamap, size, NBPG, EX_NOBOUNDARY,
    613             EX_NOWAIT, (u_long *)&dvmaddr) != 0)
    614 		return (ENOMEM);
    615 
    616 	/*
    617 	 * Compute the location, size, and number of segments actually
    618 	 * returned by the VM code.
    619 	 */
    620 	segs[0].ds_addr = dvmaddr;
    621 	segs[0].ds_len = size;
    622 	*rsegs = 1;
    623 
    624 	mlist = segs[0]._ds_mlist;
    625 	/* Map memory into DVMA space */
    626 	for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
    627 		curaddr = VM_PAGE_TO_PHYS(m);
    628 
    629 		iommu_enter(dvmaddr, curaddr);
    630 		dvmaddr += PAGE_SIZE;
    631 	}
    632 
    633 	return (0);
    634 }
    635 
    636 /*
    637  * Common function for freeing DMA-safe memory.  May be called by
    638  * bus-specific DMA memory free functions.
    639  */
    640 void
    641 iommu_dmamem_free(t, segs, nsegs)
    642 	bus_dma_tag_t t;
    643 	bus_dma_segment_t *segs;
    644 	int nsegs;
    645 {
    646 	bus_addr_t addr;
    647 	bus_size_t len;
    648 
    649 	if (nsegs != 1)
    650 		panic("bus_dmamem_free: nsegs = %d", nsegs);
    651 
    652 	addr = segs[0].ds_addr;
    653 	len = segs[0].ds_len;
    654 
    655 	iommu_remove(addr, len);
    656 	if (extent_free(iommu_dvmamap, addr, len, EX_NOWAIT) != 0)
    657 		printf("warning: %ld of DVMA space lost\n", len);
    658 	/*
    659 	 * Return the list of pages back to the VM system.
    660 	 */
    661 	_bus_dmamem_free_common(t, segs, nsegs);
    662 }
    663 
    664 /*
    665  * Common function for mapping DMA-safe memory.  May be called by
    666  * bus-specific DMA memory map functions.
    667  */
    668 int
    669 iommu_dmamem_map(t, segs, nsegs, size, kvap, flags)
    670 	bus_dma_tag_t t;
    671 	bus_dma_segment_t *segs;
    672 	int nsegs;
    673 	size_t size;
    674 	caddr_t *kvap;
    675 	int flags;
    676 {
    677 	vm_page_t m;
    678 	vm_offset_t va, sva;
    679 	bus_addr_t addr;
    680 	struct pglist *mlist;
    681 	int cbit;
    682 	size_t oversize;
    683 	u_long align;
    684 	extern int has_iocache;
    685 	extern u_long dvma_cachealign;
    686 
    687 	if (nsegs != 1)
    688 		panic("iommu_dmamem_map: nsegs = %d", nsegs);
    689 
    690 	cbit = has_iocache ? 0 : PMAP_NC;
    691 	align = dvma_cachealign ? dvma_cachealign : PAGE_SIZE;
    692 
    693 	size = round_page(size);
    694 
    695 	/*
    696 	 * Find a region of kernel virtual addresses that can accomodate
    697 	 * our aligment requirements.
    698 	 */
    699 	oversize = size + align - PAGE_SIZE;
    700 	sva = uvm_km_valloc(kernel_map, oversize);
    701 	if (sva == 0)
    702 		return (ENOMEM);
    703 
    704 	/* Compute start of aligned region */
    705 	va = sva;
    706 	va += ((segs[0].ds_addr & (align - 1)) + align - va) & (align - 1);
    707 
    708 	/* Return excess virtual addresses */
    709 	if (va != sva)
    710 		(void)uvm_unmap(kernel_map, sva, va, 0);
    711 	if (va + size != sva + oversize)
    712 		(void)uvm_unmap(kernel_map, va + size, sva + oversize, 0);
    713 
    714 
    715 	*kvap = (caddr_t)va;
    716 	mlist = segs[0]._ds_mlist;
    717 
    718 	for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
    719 
    720 		if (size == 0)
    721 			panic("iommu_dmamem_map: size botch");
    722 
    723 		addr = VM_PAGE_TO_PHYS(m);
    724 		pmap_enter(pmap_kernel(), va, addr | cbit,
    725 			   VM_PROT_READ | VM_PROT_WRITE, TRUE);
    726 #if 0
    727 			if (flags & BUS_DMA_COHERENT)
    728 				/* XXX */;
    729 #endif
    730 		va += PAGE_SIZE;
    731 		size -= PAGE_SIZE;
    732 	}
    733 
    734 	return (0);
    735 }
    736 
    737 /*
    738  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
    739  * bus-specific DMA mmap(2)'ing functions.
    740  */
    741 int
    742 iommu_dmamem_mmap(t, segs, nsegs, off, prot, flags)
    743 	bus_dma_tag_t t;
    744 	bus_dma_segment_t *segs;
    745 	int nsegs, off, prot, flags;
    746 {
    747 
    748 	panic("_bus_dmamem_mmap: not implemented");
    749 }
    750