Home | History | Annotate | Line # | Download | only in dev
iommu.c revision 1.81.44.3
      1  1.81.44.3      yamt /*	$NetBSD: iommu.c,v 1.81.44.3 2010/08/11 22:52:47 yamt Exp $	*/
      2        1.7       mrg 
      3        1.7       mrg /*
      4        1.7       mrg  * Copyright (c) 1999, 2000 Matthew R. Green
      5        1.7       mrg  * All rights reserved.
      6        1.7       mrg  *
      7        1.7       mrg  * Redistribution and use in source and binary forms, with or without
      8        1.7       mrg  * modification, are permitted provided that the following conditions
      9        1.7       mrg  * are met:
     10        1.7       mrg  * 1. Redistributions of source code must retain the above copyright
     11        1.7       mrg  *    notice, this list of conditions and the following disclaimer.
     12        1.7       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.7       mrg  *    notice, this list of conditions and the following disclaimer in the
     14        1.7       mrg  *    documentation and/or other materials provided with the distribution.
     15  1.81.44.1      yamt  *
     16  1.81.44.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.81.44.1      yamt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.81.44.1      yamt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.81.44.1      yamt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.81.44.1      yamt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.81.44.1      yamt  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.81.44.1      yamt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.81.44.1      yamt  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.81.44.1      yamt  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.81.44.1      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.81.44.1      yamt  * SUCH DAMAGE.
     27  1.81.44.1      yamt  */
     28  1.81.44.1      yamt 
     29  1.81.44.1      yamt /*
     30  1.81.44.1      yamt  * Copyright (c) 2001, 2002 Eduardo Horvath
     31  1.81.44.1      yamt  * All rights reserved.
     32  1.81.44.1      yamt  *
     33  1.81.44.1      yamt  * Redistribution and use in source and binary forms, with or without
     34  1.81.44.1      yamt  * modification, are permitted provided that the following conditions
     35  1.81.44.1      yamt  * are met:
     36  1.81.44.1      yamt  * 1. Redistributions of source code must retain the above copyright
     37  1.81.44.1      yamt  *    notice, this list of conditions and the following disclaimer.
     38  1.81.44.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     39  1.81.44.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     40  1.81.44.1      yamt  *    documentation and/or other materials provided with the distribution.
     41        1.7       mrg  * 3. The name of the author may not be used to endorse or promote products
     42        1.7       mrg  *    derived from this software without specific prior written permission.
     43        1.7       mrg  *
     44        1.7       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     45        1.7       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     46        1.7       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     47        1.7       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     48        1.7       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     49        1.7       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     50        1.7       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     51        1.7       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     52        1.7       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     53        1.7       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     54        1.7       mrg  * SUCH DAMAGE.
     55        1.7       mrg  */
     56        1.1       mrg 
     57        1.7       mrg /*
     58        1.7       mrg  * UltraSPARC IOMMU support; used by both the sbus and pci code.
     59        1.7       mrg  */
     60       1.66     lukem 
     61       1.66     lukem #include <sys/cdefs.h>
     62  1.81.44.3      yamt __KERNEL_RCSID(0, "$NetBSD: iommu.c,v 1.81.44.3 2010/08/11 22:52:47 yamt Exp $");
     63       1.66     lukem 
     64        1.4       mrg #include "opt_ddb.h"
     65        1.4       mrg 
     66        1.1       mrg #include <sys/param.h>
     67        1.1       mrg #include <sys/extent.h>
     68        1.1       mrg #include <sys/malloc.h>
     69        1.1       mrg #include <sys/systm.h>
     70        1.1       mrg #include <sys/device.h>
     71       1.41       chs #include <sys/proc.h>
     72       1.18       mrg 
     73       1.18       mrg #include <uvm/uvm_extern.h>
     74        1.1       mrg 
     75        1.1       mrg #include <machine/bus.h>
     76        1.1       mrg #include <sparc64/dev/iommureg.h>
     77        1.1       mrg #include <sparc64/dev/iommuvar.h>
     78        1.1       mrg 
     79        1.1       mrg #include <machine/autoconf.h>
     80        1.1       mrg #include <machine/cpu.h>
     81        1.1       mrg 
     82        1.1       mrg #ifdef DEBUG
     83       1.22       mrg #define IDB_BUSDMA	0x1
     84       1.22       mrg #define IDB_IOMMU	0x2
     85       1.22       mrg #define IDB_INFO	0x4
     86       1.36       eeh #define	IDB_SYNC	0x8
     87       1.10       mrg int iommudebug = 0x0;
     88        1.4       mrg #define DPRINTF(l, s)   do { if (iommudebug & l) printf s; } while (0)
     89  1.81.44.2      yamt #define IOTTE_DEBUG(n)	(n)
     90        1.4       mrg #else
     91        1.4       mrg #define DPRINTF(l, s)
     92  1.81.44.2      yamt #define IOTTE_DEBUG(n)	0
     93        1.1       mrg #endif
     94        1.1       mrg 
     95       1.55       eeh #define iommu_strbuf_flush(i, v) do {					\
     96       1.55       eeh 	if ((i)->sb_flush)						\
     97       1.55       eeh 		bus_space_write_8((i)->sb_is->is_bustag, (i)->sb_sb,	\
     98       1.50       eeh 			STRBUFREG(strbuf_pgflush), (v));		\
     99       1.42       eeh 	} while (0)
    100       1.42       eeh 
    101       1.78       cdi static	int iommu_strbuf_flush_done(struct strbuf_ctl *);
    102  1.81.44.1      yamt static	void _iommu_dvmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
    103  1.81.44.1      yamt 		bus_size_t, int);
    104       1.11       eeh 
    105        1.1       mrg /*
    106        1.1       mrg  * initialise the UltraSPARC IOMMU (SBUS or PCI):
    107        1.1       mrg  *	- allocate and setup the iotsb.
    108        1.1       mrg  *	- enable the IOMMU
    109        1.7       mrg  *	- initialise the streaming buffers (if they exist)
    110        1.1       mrg  *	- create a private DVMA map.
    111        1.1       mrg  */
    112        1.1       mrg void
    113       1.79       cdi iommu_init(char *name, struct iommu_state *is, int tsbsize, uint32_t iovabase)
    114        1.1       mrg {
    115       1.11       eeh 	psize_t size;
    116       1.11       eeh 	vaddr_t va;
    117       1.11       eeh 	paddr_t pa;
    118       1.58       chs 	struct vm_page *pg;
    119       1.58       chs 	struct pglist pglist;
    120        1.1       mrg 
    121        1.1       mrg 	/*
    122        1.1       mrg 	 * Setup the iommu.
    123        1.1       mrg 	 *
    124       1.45       eeh 	 * The sun4u iommu is part of the SBUS or PCI controller so we will
    125       1.45       eeh 	 * deal with it here..
    126        1.1       mrg 	 *
    127       1.45       eeh 	 * For sysio and psycho/psycho+ the IOMMU address space always ends at
    128       1.45       eeh 	 * 0xffffe000, but the starting address depends on the size of the
    129       1.45       eeh 	 * map.  The map size is 1024 * 2 ^ is->is_tsbsize entries, where each
    130       1.45       eeh 	 * entry is 8 bytes.  The start of the map can be calculated by
    131       1.45       eeh 	 * (0xffffe000 << (8 + is->is_tsbsize)).
    132       1.45       eeh 	 *
    133       1.45       eeh 	 * But sabre and hummingbird use a different scheme that seems to
    134       1.45       eeh 	 * be hard-wired, so we read the start and size from the PROM and
    135       1.45       eeh 	 * just use those values.
    136        1.2       eeh 	 */
    137       1.11       eeh 	is->is_cr = (tsbsize << 16) | IOMMUCR_EN;
    138       1.11       eeh 	is->is_tsbsize = tsbsize;
    139       1.45       eeh 	if (iovabase == -1) {
    140       1.45       eeh 		is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize);
    141  1.81.44.2      yamt 		is->is_dvmaend = IOTSB_VEND - 1;
    142       1.45       eeh 	} else {
    143       1.45       eeh 		is->is_dvmabase = iovabase;
    144  1.81.44.2      yamt 		is->is_dvmaend = iovabase + IOTSB_VSIZE(tsbsize) - 1;
    145       1.45       eeh 	}
    146       1.11       eeh 
    147       1.11       eeh 	/*
    148       1.15       eeh 	 * Allocate memory for I/O pagetables.  They need to be physically
    149       1.15       eeh 	 * contiguous.
    150       1.11       eeh 	 */
    151       1.11       eeh 
    152       1.64   thorpej 	size = PAGE_SIZE << is->is_tsbsize;
    153       1.11       eeh 	if (uvm_pglistalloc((psize_t)size, (paddr_t)0, (paddr_t)-1,
    154       1.64   thorpej 		(paddr_t)PAGE_SIZE, (paddr_t)0, &pglist, 1, 0) != 0)
    155       1.11       eeh 		panic("iommu_init: no memory");
    156       1.11       eeh 
    157       1.76      yamt 	va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY);
    158       1.11       eeh 	if (va == 0)
    159       1.11       eeh 		panic("iommu_init: no memory");
    160       1.11       eeh 	is->is_tsb = (int64_t *)va;
    161       1.11       eeh 
    162       1.58       chs 	is->is_ptsb = VM_PAGE_TO_PHYS(TAILQ_FIRST(&pglist));
    163       1.11       eeh 
    164       1.11       eeh 	/* Map the pages */
    165  1.81.44.1      yamt 	TAILQ_FOREACH(pg, &pglist, pageq.queue) {
    166       1.58       chs 		pa = VM_PAGE_TO_PHYS(pg);
    167  1.81.44.2      yamt 		pmap_kenter_pa(va, pa | PMAP_NVC,
    168  1.81.44.2      yamt 		    VM_PROT_READ | VM_PROT_WRITE, 0);
    169       1.64   thorpej 		va += PAGE_SIZE;
    170       1.11       eeh 	}
    171       1.38     chris 	pmap_update(pmap_kernel());
    172       1.58       chs 	memset(is->is_tsb, 0, size);
    173        1.1       mrg 
    174        1.1       mrg #ifdef DEBUG
    175       1.22       mrg 	if (iommudebug & IDB_INFO)
    176        1.1       mrg 	{
    177        1.1       mrg 		/* Probe the iommu */
    178        1.1       mrg 
    179       1.25       mrg 		printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n",
    180       1.50       eeh 			(u_long)bus_space_read_8(is->is_bustag, is->is_iommu,
    181       1.50       eeh 				offsetof (struct iommureg, iommu_cr)),
    182       1.50       eeh 			(u_long)bus_space_read_8(is->is_bustag, is->is_iommu,
    183       1.50       eeh 				offsetof (struct iommureg, iommu_tsb)),
    184       1.50       eeh 			(u_long)bus_space_read_8(is->is_bustag, is->is_iommu,
    185       1.50       eeh 				offsetof (struct iommureg, iommu_flush)));
    186       1.50       eeh 		printf("iommu cr=%llx tsb=%llx\n",
    187       1.50       eeh 			(unsigned long long)bus_space_read_8(is->is_bustag,
    188       1.50       eeh 				is->is_iommu,
    189       1.50       eeh 				offsetof (struct iommureg, iommu_cr)),
    190       1.50       eeh 			(unsigned long long)bus_space_read_8(is->is_bustag,
    191       1.50       eeh 				is->is_iommu,
    192       1.50       eeh 				offsetof (struct iommureg, iommu_tsb)));
    193       1.58       chs 		printf("TSB base %p phys %llx\n", (void *)is->is_tsb,
    194       1.50       eeh 			(unsigned long long)is->is_ptsb);
    195        1.1       mrg 		delay(1000000); /* 1 s */
    196        1.1       mrg 	}
    197        1.1       mrg #endif
    198        1.1       mrg 
    199        1.1       mrg 	/*
    200        1.1       mrg 	 * now actually start up the IOMMU
    201        1.1       mrg 	 */
    202        1.1       mrg 	iommu_reset(is);
    203        1.1       mrg 
    204        1.1       mrg 	/*
    205        1.1       mrg 	 * Now all the hardware's working we need to allocate a dvma map.
    206        1.1       mrg 	 */
    207  1.81.44.3      yamt 	aprint_debug("DVMA map: %x to %x\n",
    208       1.11       eeh 		(unsigned int)is->is_dvmabase,
    209       1.45       eeh 		(unsigned int)is->is_dvmaend);
    210  1.81.44.3      yamt 	aprint_debug("IOTSB: %llx to %llx\n",
    211       1.47       eeh 		(unsigned long long)is->is_ptsb,
    212  1.81.44.2      yamt 		(unsigned long long)(is->is_ptsb + size - 1));
    213        1.1       mrg 	is->is_dvmamap = extent_create(name,
    214  1.81.44.2      yamt 	    is->is_dvmabase, is->is_dvmaend,
    215       1.64   thorpej 	    M_DEVBUF, 0, 0, EX_NOWAIT);
    216  1.81.44.3      yamt 	/* XXXMRG Check is_dvmamap is valid. */
    217        1.1       mrg }
    218        1.1       mrg 
    219        1.8       mrg /*
    220        1.8       mrg  * Streaming buffers don't exist on the UltraSPARC IIi; we should have
    221        1.8       mrg  * detected that already and disabled them.  If not, we will notice that
    222        1.8       mrg  * they aren't there when the STRBUF_EN bit does not remain.
    223        1.8       mrg  */
    224        1.1       mrg void
    225       1.78       cdi iommu_reset(struct iommu_state *is)
    226        1.1       mrg {
    227       1.45       eeh 	int i;
    228       1.55       eeh 	struct strbuf_ctl *sb;
    229        1.1       mrg 
    230        1.1       mrg 	/* Need to do 64-bit stores */
    231       1.58       chs 	bus_space_write_8(is->is_bustag, is->is_iommu, IOMMUREG(iommu_tsb),
    232       1.50       eeh 		is->is_ptsb);
    233       1.50       eeh 
    234       1.11       eeh 	/* Enable IOMMU in diagnostic mode */
    235       1.50       eeh 	bus_space_write_8(is->is_bustag, is->is_iommu, IOMMUREG(iommu_cr),
    236       1.50       eeh 		is->is_cr|IOMMUCR_DE);
    237       1.11       eeh 
    238       1.58       chs 	for (i = 0; i < 2; i++) {
    239       1.55       eeh 		if ((sb = is->is_sb[i])) {
    240        1.5       mrg 
    241       1.45       eeh 			/* Enable diagnostics mode? */
    242       1.58       chs 			bus_space_write_8(is->is_bustag, is->is_sb[i]->sb_sb,
    243       1.50       eeh 				STRBUFREG(strbuf_ctl), STRBUF_EN);
    244       1.45       eeh 
    245       1.45       eeh 			/* No streaming buffers? Disable them */
    246       1.58       chs 			if (bus_space_read_8(is->is_bustag,
    247       1.58       chs 				is->is_sb[i]->sb_sb,
    248       1.55       eeh 				STRBUFREG(strbuf_ctl)) == 0) {
    249       1.55       eeh 				is->is_sb[i]->sb_flush = NULL;
    250       1.55       eeh 			} else {
    251       1.58       chs 
    252       1.55       eeh 				/*
    253       1.55       eeh 				 * locate the pa of the flush buffer.
    254       1.55       eeh 				 */
    255       1.55       eeh 				(void)pmap_extract(pmap_kernel(),
    256       1.55       eeh 					(vaddr_t)is->is_sb[i]->sb_flush,
    257       1.55       eeh 					&is->is_sb[i]->sb_flushpa);
    258       1.55       eeh 			}
    259       1.45       eeh 		}
    260       1.42       eeh 	}
    261        1.2       eeh }
    262        1.2       eeh 
    263        1.2       eeh /*
    264       1.58       chs  * Here are the iommu control routines.
    265        1.2       eeh  */
    266        1.2       eeh void
    267       1.78       cdi iommu_enter(struct strbuf_ctl *sb, vaddr_t va, int64_t pa, int flags)
    268        1.2       eeh {
    269       1.55       eeh 	struct iommu_state *is = sb->sb_is;
    270       1.55       eeh 	int strbuf = (flags & BUS_DMA_STREAMING);
    271        1.2       eeh 	int64_t tte;
    272        1.2       eeh 
    273        1.2       eeh #ifdef DIAGNOSTIC
    274       1.45       eeh 	if (va < is->is_dvmabase || va > is->is_dvmaend)
    275       1.13       mrg 		panic("iommu_enter: va %#lx not in DVMA space", va);
    276        1.2       eeh #endif
    277        1.2       eeh 
    278       1.55       eeh 	/* Is the streamcache flush really needed? */
    279  1.81.44.2      yamt 	if (sb->sb_flush)
    280       1.55       eeh 		iommu_strbuf_flush(sb, va);
    281  1.81.44.2      yamt 	else
    282       1.55       eeh 		/* If we can't flush the strbuf don't enable it. */
    283       1.55       eeh 		strbuf = 0;
    284       1.55       eeh 
    285       1.58       chs 	tte = MAKEIOTTE(pa, !(flags & BUS_DMA_NOWRITE),
    286       1.55       eeh 		!(flags & BUS_DMA_NOCACHE), (strbuf));
    287       1.50       eeh #ifdef DEBUG
    288       1.50       eeh 	tte |= (flags & 0xff000LL)<<(4*8);
    289       1.50       eeh #endif
    290       1.58       chs 
    291       1.58       chs 	DPRINTF(IDB_IOMMU, ("Clearing TSB slot %d for va %p\n",
    292       1.25       mrg 		       (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va));
    293        1.2       eeh 	is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
    294       1.58       chs 	bus_space_write_8(is->is_bustag, is->is_iommu,
    295       1.50       eeh 		IOMMUREG(iommu_flush), va);
    296       1.22       mrg 	DPRINTF(IDB_IOMMU, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
    297       1.50       eeh 		va, (long)pa, (u_long)IOTSBSLOT(va,is->is_tsbsize),
    298       1.50       eeh 		(void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    299       1.50       eeh 		(u_long)tte));
    300       1.39       eeh }
    301       1.39       eeh 
    302       1.39       eeh /*
    303       1.39       eeh  * Find the value of a DVMA address (debug routine).
    304       1.39       eeh  */
    305       1.39       eeh paddr_t
    306       1.78       cdi iommu_extract(struct iommu_state *is, vaddr_t dva)
    307       1.39       eeh {
    308       1.39       eeh 	int64_t tte = 0;
    309       1.58       chs 
    310  1.81.44.2      yamt 	if (dva >= is->is_dvmabase && dva <= is->is_dvmaend)
    311       1.55       eeh 		tte = is->is_tsb[IOTSBSLOT(dva, is->is_tsbsize)];
    312       1.39       eeh 
    313       1.54       eeh 	if ((tte & IOTTE_V) == 0)
    314       1.39       eeh 		return ((paddr_t)-1L);
    315       1.54       eeh 	return (tte & IOTTE_PAMASK);
    316        1.2       eeh }
    317        1.2       eeh 
    318        1.2       eeh /*
    319        1.2       eeh  * iommu_remove: removes mappings created by iommu_enter
    320        1.2       eeh  *
    321        1.2       eeh  * Only demap from IOMMU if flag is set.
    322        1.8       mrg  *
    323        1.8       mrg  * XXX: this function needs better internal error checking.
    324        1.2       eeh  */
    325        1.2       eeh void
    326       1.78       cdi iommu_remove(struct iommu_state *is, vaddr_t va, size_t len)
    327        1.2       eeh {
    328        1.2       eeh 
    329        1.2       eeh #ifdef DIAGNOSTIC
    330       1.45       eeh 	if (va < is->is_dvmabase || va > is->is_dvmaend)
    331       1.25       mrg 		panic("iommu_remove: va 0x%lx not in DVMA space", (u_long)va);
    332        1.2       eeh 	if ((long)(va + len) < (long)va)
    333       1.58       chs 		panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
    334        1.2       eeh 		      (long) va, (long) len);
    335       1.58       chs 	if (len & ~0xfffffff)
    336       1.72       snj 		panic("iommu_remove: ridiculous len 0x%lx", (u_long)len);
    337        1.2       eeh #endif
    338        1.2       eeh 
    339        1.2       eeh 	va = trunc_page(va);
    340       1.22       mrg 	DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx TSB[%lx]@%p\n",
    341       1.50       eeh 		va, (u_long)IOTSBSLOT(va, is->is_tsbsize),
    342       1.50       eeh 		&is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)]));
    343        1.2       eeh 	while (len > 0) {
    344       1.50       eeh 		DPRINTF(IDB_IOMMU, ("iommu_remove: clearing TSB slot %d "
    345       1.50       eeh 			"for va %p size %lx\n",
    346       1.50       eeh 			(int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va,
    347       1.50       eeh 			(u_long)len));
    348       1.64   thorpej 		if (len <= PAGE_SIZE)
    349       1.10       mrg 			len = 0;
    350       1.10       mrg 		else
    351       1.64   thorpej 			len -= PAGE_SIZE;
    352        1.8       mrg 
    353  1.81.44.3      yamt #if 0
    354  1.81.44.2      yamt 		/*
    355  1.81.44.2      yamt 		 * XXX Zero-ing the entry would not require RMW
    356  1.81.44.2      yamt 		 *
    357  1.81.44.2      yamt 		 * Disabling valid bit while a page is used by a device
    358  1.81.44.2      yamt 		 * causes an uncorrectable DMA error.
    359  1.81.44.2      yamt 		 * Workaround to avoid an uncorrectable DMA error is
    360  1.81.44.2      yamt 		 * eliminating the next line, but the page is mapped
    361  1.81.44.2      yamt 		 * until the next iommu_enter call.
    362  1.81.44.2      yamt 		 */
    363       1.47       eeh 		is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] &= ~IOTTE_V;
    364  1.81.44.2      yamt 		membar_storestore();
    365  1.81.44.3      yamt #endif
    366       1.58       chs 		bus_space_write_8(is->is_bustag, is->is_iommu,
    367       1.50       eeh 			IOMMUREG(iommu_flush), va);
    368       1.64   thorpej 		va += PAGE_SIZE;
    369        1.2       eeh 	}
    370        1.2       eeh }
    371        1.2       eeh 
    372       1.58       chs static int
    373       1.78       cdi iommu_strbuf_flush_done(struct strbuf_ctl *sb)
    374        1.2       eeh {
    375       1.55       eeh 	struct iommu_state *is = sb->sb_is;
    376        1.2       eeh 	struct timeval cur, flushtimeout;
    377        1.2       eeh 
    378        1.2       eeh #define BUMPTIME(t, usec) { \
    379        1.2       eeh 	register volatile struct timeval *tp = (t); \
    380        1.2       eeh 	register long us; \
    381        1.2       eeh  \
    382        1.2       eeh 	tp->tv_usec = us = tp->tv_usec + (usec); \
    383        1.2       eeh 	if (us >= 1000000) { \
    384        1.2       eeh 		tp->tv_usec = us - 1000000; \
    385        1.2       eeh 		tp->tv_sec++; \
    386        1.2       eeh 	} \
    387        1.2       eeh }
    388        1.5       mrg 
    389       1.55       eeh 	if (!sb->sb_flush)
    390        1.5       mrg 		return (0);
    391       1.58       chs 
    392        1.7       mrg 	/*
    393        1.7       mrg 	 * Streaming buffer flushes:
    394       1.58       chs 	 *
    395        1.7       mrg 	 *   1 Tell strbuf to flush by storing va to strbuf_pgflush.  If
    396        1.7       mrg 	 *     we're not on a cache line boundary (64-bits):
    397        1.7       mrg 	 *   2 Store 0 in flag
    398        1.7       mrg 	 *   3 Store pointer to flag in flushsync
    399        1.7       mrg 	 *   4 wait till flushsync becomes 0x1
    400        1.7       mrg 	 *
    401        1.7       mrg 	 * If it takes more than .5 sec, something
    402        1.7       mrg 	 * went wrong.
    403        1.7       mrg 	 */
    404        1.2       eeh 
    405       1.55       eeh 	*sb->sb_flush = 0;
    406       1.58       chs 	bus_space_write_8(is->is_bustag, sb->sb_sb,
    407       1.55       eeh 		STRBUFREG(strbuf_flushsync), sb->sb_flushpa);
    408        1.2       eeh 
    409       1.58       chs 	microtime(&flushtimeout);
    410        1.2       eeh 	cur = flushtimeout;
    411        1.2       eeh 	BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
    412       1.58       chs 
    413       1.55       eeh 	DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flush = %lx "
    414  1.81.44.1      yamt 		"at va = %lx pa = %lx now=%"PRIx64":%"PRIx32" until = %"PRIx64":%"PRIx32"\n",
    415       1.58       chs 		(long)*sb->sb_flush, (long)sb->sb_flush, (long)sb->sb_flushpa,
    416       1.42       eeh 		cur.tv_sec, cur.tv_usec,
    417       1.42       eeh 		flushtimeout.tv_sec, flushtimeout.tv_usec));
    418       1.42       eeh 
    419        1.2       eeh 	/* Bypass non-coherent D$ */
    420       1.55       eeh 	while ((!ldxa(sb->sb_flushpa, ASI_PHYS_CACHED)) &&
    421  1.81.44.3      yamt 	       timercmp(&cur, &flushtimeout, <=))
    422        1.2       eeh 		microtime(&cur);
    423        1.2       eeh 
    424        1.2       eeh #ifdef DIAGNOSTIC
    425       1.55       eeh 	if (!ldxa(sb->sb_flushpa, ASI_PHYS_CACHED)) {
    426       1.55       eeh 		printf("iommu_strbuf_flush_done: flush timeout %p, at %p\n",
    427       1.55       eeh 			(void *)(u_long)*sb->sb_flush,
    428       1.55       eeh 			(void *)(u_long)sb->sb_flushpa); /* panic? */
    429        1.2       eeh #ifdef DDB
    430        1.2       eeh 		Debugger();
    431        1.2       eeh #endif
    432        1.2       eeh 	}
    433        1.2       eeh #endif
    434       1.31       eeh 	DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flushed\n"));
    435       1.55       eeh 	return (*sb->sb_flush);
    436        1.7       mrg }
    437        1.7       mrg 
    438        1.7       mrg /*
    439        1.7       mrg  * IOMMU DVMA operations, common to SBUS and PCI.
    440        1.7       mrg  */
    441        1.7       mrg int
    442  1.81.44.1      yamt iommu_dvmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    443  1.81.44.1      yamt 	bus_size_t buflen, struct proc *p, int flags)
    444        1.7       mrg {
    445  1.81.44.1      yamt 	struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
    446       1.55       eeh 	struct iommu_state *is = sb->sb_is;
    447        1.7       mrg 	int s;
    448  1.81.44.2      yamt 	int err, needsflush;
    449        1.7       mrg 	bus_size_t sgsize;
    450        1.7       mrg 	paddr_t curaddr;
    451  1.81.44.2      yamt 	u_long dvmaddr, sgstart, sgend, bmask;
    452       1.71   tsutsui 	bus_size_t align, boundary, len;
    453        1.7       mrg 	vaddr_t vaddr = (vaddr_t)buf;
    454       1.40       eeh 	int seg;
    455       1.58       chs 	struct pmap *pmap;
    456        1.7       mrg 
    457        1.7       mrg 	if (map->dm_nsegs) {
    458        1.7       mrg 		/* Already in use?? */
    459        1.7       mrg #ifdef DIAGNOSTIC
    460        1.7       mrg 		printf("iommu_dvmamap_load: map still in use\n");
    461        1.7       mrg #endif
    462        1.7       mrg 		bus_dmamap_unload(t, map);
    463        1.7       mrg 	}
    464       1.58       chs 
    465        1.7       mrg 	/*
    466        1.7       mrg 	 * Make sure that on error condition we return "no valid mappings".
    467        1.7       mrg 	 */
    468        1.7       mrg 	map->dm_nsegs = 0;
    469  1.81.44.2      yamt 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    470  1.81.44.2      yamt 
    471        1.7       mrg 	if (buflen > map->_dm_size) {
    472       1.22       mrg 		DPRINTF(IDB_BUSDMA,
    473        1.7       mrg 		    ("iommu_dvmamap_load(): error %d > %d -- "
    474       1.25       mrg 		     "map size exceeded!\n", (int)buflen, (int)map->_dm_size));
    475        1.7       mrg 		return (EINVAL);
    476        1.7       mrg 	}
    477        1.7       mrg 
    478        1.7       mrg 	sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
    479       1.20       mrg 
    480        1.7       mrg 	/*
    481       1.21       eeh 	 * A boundary presented to bus_dmamem_alloc() takes precedence
    482       1.21       eeh 	 * over boundary in the map.
    483        1.7       mrg 	 */
    484       1.21       eeh 	if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0)
    485       1.21       eeh 		boundary = map->_dm_boundary;
    486       1.64   thorpej 	align = max(map->dm_segs[0]._ds_align, PAGE_SIZE);
    487       1.58       chs 
    488       1.58       chs 	/*
    489       1.58       chs 	 * If our segment size is larger than the boundary we need to
    490       1.40       eeh 	 * split the transfer up int little pieces ourselves.
    491       1.40       eeh 	 */
    492       1.58       chs 	s = splhigh();
    493       1.58       chs 	err = extent_alloc(is->is_dvmamap, sgsize, align,
    494       1.71   tsutsui 	    (sgsize > boundary) ? 0 : boundary,
    495       1.71   tsutsui 	    EX_NOWAIT|EX_BOUNDZERO, &dvmaddr);
    496        1.7       mrg 	splx(s);
    497        1.7       mrg 
    498        1.7       mrg #ifdef DEBUG
    499       1.71   tsutsui 	if (err || (dvmaddr == (u_long)-1)) {
    500        1.7       mrg 		printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
    501       1.25       mrg 		    (int)sgsize, flags);
    502       1.40       eeh #ifdef DDB
    503        1.7       mrg 		Debugger();
    504       1.40       eeh #endif
    505       1.58       chs 	}
    506       1.58       chs #endif
    507       1.11       eeh 	if (err != 0)
    508       1.11       eeh 		return (err);
    509       1.11       eeh 
    510       1.65  nakayama 	if (dvmaddr == (u_long)-1)
    511        1.7       mrg 		return (ENOMEM);
    512        1.7       mrg 
    513       1.40       eeh 	/* Set the active DVMA map */
    514       1.40       eeh 	map->_dm_dvmastart = dvmaddr;
    515       1.40       eeh 	map->_dm_dvmasize = sgsize;
    516       1.40       eeh 
    517       1.40       eeh 	/*
    518       1.40       eeh 	 * Now split the DVMA range into segments, not crossing
    519       1.40       eeh 	 * the boundary.
    520       1.40       eeh 	 */
    521       1.40       eeh 	seg = 0;
    522       1.40       eeh 	sgstart = dvmaddr + (vaddr & PGOFSET);
    523       1.40       eeh 	sgend = sgstart + buflen - 1;
    524       1.40       eeh 	map->dm_segs[seg].ds_addr = sgstart;
    525       1.71   tsutsui 	DPRINTF(IDB_INFO, ("iommu_dvmamap_load: boundary %lx boundary - 1 %lx "
    526       1.71   tsutsui 	    "~(boundary - 1) %lx\n", (long)boundary, (long)(boundary - 1),
    527       1.71   tsutsui 	    (long)~(boundary - 1)));
    528  1.81.44.2      yamt 	bmask = ~(boundary - 1);
    529  1.81.44.2      yamt 	while ((sgstart & bmask) != (sgend & bmask) ||
    530  1.81.44.2      yamt 	       sgend - sgstart + 1 > map->dm_maxsegsz) {
    531  1.81.44.2      yamt 		/* Oops. We crossed a boundary or large seg. Split the xfer. */
    532  1.81.44.2      yamt 		len = map->dm_maxsegsz;
    533  1.81.44.2      yamt 		if ((sgstart & bmask) != (sgend & bmask))
    534  1.81.44.2      yamt 			len = min(len, boundary - (sgstart & (boundary - 1)));
    535       1.71   tsutsui 		map->dm_segs[seg].ds_len = len;
    536       1.40       eeh 		DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
    537       1.71   tsutsui 		    "seg %d start %lx size %lx\n", seg,
    538       1.71   tsutsui 		    (long)map->dm_segs[seg].ds_addr,
    539       1.71   tsutsui 		    (long)map->dm_segs[seg].ds_len));
    540       1.53       eeh 		if (++seg >= map->_dm_segcnt) {
    541       1.40       eeh 			/* Too many segments.  Fail the operation. */
    542       1.40       eeh 			DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
    543       1.71   tsutsui 			    "too many segments %d\n", seg));
    544       1.40       eeh 			s = splhigh();
    545       1.40       eeh 			err = extent_free(is->is_dvmamap,
    546       1.71   tsutsui 			    dvmaddr, sgsize, EX_NOWAIT);
    547       1.40       eeh 			map->_dm_dvmastart = 0;
    548       1.40       eeh 			map->_dm_dvmasize = 0;
    549       1.43       eeh 			splx(s);
    550  1.81.44.2      yamt 			if (err != 0)
    551  1.81.44.2      yamt 				printf("warning: %s: %" PRId64
    552  1.81.44.2      yamt 				    " of DVMA space lost\n", __func__, sgsize);
    553       1.80       mrg 			return (EFBIG);
    554       1.40       eeh 		}
    555       1.71   tsutsui 		sgstart += len;
    556       1.40       eeh 		map->dm_segs[seg].ds_addr = sgstart;
    557       1.40       eeh 	}
    558       1.40       eeh 	map->dm_segs[seg].ds_len = sgend - sgstart + 1;
    559       1.40       eeh 	DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
    560       1.71   tsutsui 	    "seg %d start %lx size %lx\n", seg,
    561       1.71   tsutsui 	    (long)map->dm_segs[seg].ds_addr, (long)map->dm_segs[seg].ds_len));
    562       1.71   tsutsui 	map->dm_nsegs = seg + 1;
    563        1.7       mrg 	map->dm_mapsize = buflen;
    564        1.7       mrg 
    565        1.7       mrg 	if (p != NULL)
    566        1.7       mrg 		pmap = p->p_vmspace->vm_map.pmap;
    567        1.7       mrg 	else
    568        1.7       mrg 		pmap = pmap_kernel();
    569        1.7       mrg 
    570  1.81.44.2      yamt 	needsflush = 0;
    571        1.7       mrg 	for (; buflen > 0; ) {
    572       1.58       chs 
    573        1.7       mrg 		/*
    574        1.7       mrg 		 * Get the physical address for this page.
    575        1.7       mrg 		 */
    576        1.7       mrg 		if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
    577       1.74    petrov #ifdef DIAGNOSTIC
    578       1.74    petrov 			printf("iommu_dvmamap_load: pmap_extract failed %lx\n", vaddr);
    579       1.74    petrov #endif
    580        1.7       mrg 			bus_dmamap_unload(t, map);
    581        1.7       mrg 			return (-1);
    582        1.7       mrg 		}
    583        1.7       mrg 
    584        1.7       mrg 		/*
    585        1.7       mrg 		 * Compute the segment size, and adjust counts.
    586        1.7       mrg 		 */
    587       1.64   thorpej 		sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
    588        1.7       mrg 		if (buflen < sgsize)
    589        1.7       mrg 			sgsize = buflen;
    590        1.7       mrg 
    591       1.22       mrg 		DPRINTF(IDB_BUSDMA,
    592       1.36       eeh 		    ("iommu_dvmamap_load: map %p loading va %p "
    593       1.71   tsutsui 		    "dva %lx at pa %lx\n",
    594       1.71   tsutsui 		    map, (void *)vaddr, (long)dvmaddr,
    595  1.81.44.2      yamt 		    (long)trunc_page(curaddr)));
    596       1.55       eeh 		iommu_enter(sb, trunc_page(dvmaddr), trunc_page(curaddr),
    597  1.81.44.2      yamt 		    flags | IOTTE_DEBUG(0x4000));
    598  1.81.44.2      yamt 		needsflush = 1;
    599       1.58       chs 
    600        1.7       mrg 		dvmaddr += PAGE_SIZE;
    601        1.7       mrg 		vaddr += sgsize;
    602        1.7       mrg 		buflen -= sgsize;
    603        1.7       mrg 	}
    604  1.81.44.2      yamt 	if (needsflush)
    605  1.81.44.2      yamt 		iommu_strbuf_flush_done(sb);
    606       1.45       eeh #ifdef DIAGNOSTIC
    607       1.45       eeh 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    608       1.45       eeh 		if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
    609       1.45       eeh 			map->dm_segs[seg].ds_addr > is->is_dvmaend) {
    610       1.45       eeh 			printf("seg %d dvmaddr %lx out of range %x - %x\n",
    611       1.71   tsutsui 			    seg, (long)map->dm_segs[seg].ds_addr,
    612       1.71   tsutsui 			    is->is_dvmabase, is->is_dvmaend);
    613       1.57       chs #ifdef DDB
    614       1.45       eeh 			Debugger();
    615       1.57       chs #endif
    616       1.45       eeh 		}
    617       1.45       eeh 	}
    618       1.45       eeh #endif
    619        1.7       mrg 	return (0);
    620        1.7       mrg }
    621        1.7       mrg 
    622        1.7       mrg 
    623        1.7       mrg void
    624  1.81.44.1      yamt iommu_dvmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    625        1.7       mrg {
    626  1.81.44.1      yamt 	struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
    627       1.55       eeh 	struct iommu_state *is = sb->sb_is;
    628       1.40       eeh 	int error, s;
    629       1.70  christos 	bus_size_t sgsize = map->_dm_dvmasize;
    630        1.7       mrg 
    631       1.40       eeh 	/* Flush the iommu */
    632       1.40       eeh #ifdef DEBUG
    633       1.40       eeh 	if (!map->_dm_dvmastart) {
    634       1.40       eeh 		printf("iommu_dvmamap_unload: No dvmastart is zero\n");
    635       1.40       eeh #ifdef DDB
    636       1.40       eeh 		Debugger();
    637       1.40       eeh #endif
    638       1.40       eeh 	}
    639       1.40       eeh #endif
    640       1.40       eeh 	iommu_remove(is, map->_dm_dvmastart, map->_dm_dvmasize);
    641        1.7       mrg 
    642       1.23       eeh 	/* Flush the caches */
    643       1.23       eeh 	bus_dmamap_unload(t->_parent, map);
    644       1.23       eeh 
    645        1.7       mrg 	s = splhigh();
    646       1.58       chs 	error = extent_free(is->is_dvmamap, map->_dm_dvmastart,
    647       1.40       eeh 		map->_dm_dvmasize, EX_NOWAIT);
    648       1.43       eeh 	map->_dm_dvmastart = 0;
    649       1.43       eeh 	map->_dm_dvmasize = 0;
    650        1.7       mrg 	splx(s);
    651        1.7       mrg 	if (error != 0)
    652  1.81.44.2      yamt 		printf("warning: %s: %" PRId64 " of DVMA space lost\n",
    653  1.81.44.2      yamt 		    __func__, sgsize);
    654       1.40       eeh 
    655       1.40       eeh 	/* Clear the map */
    656        1.9       eeh }
    657        1.9       eeh 
    658        1.9       eeh 
    659        1.9       eeh int
    660  1.81.44.1      yamt iommu_dvmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
    661  1.81.44.1      yamt 	bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    662        1.9       eeh {
    663  1.81.44.1      yamt 	struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
    664       1.55       eeh 	struct iommu_state *is = sb->sb_is;
    665       1.58       chs 	struct vm_page *pg;
    666       1.40       eeh 	int i, j, s;
    667       1.26    martin 	int left;
    668  1.81.44.2      yamt 	int err, needsflush;
    669        1.9       eeh 	bus_size_t sgsize;
    670        1.9       eeh 	paddr_t pa;
    671       1.21       eeh 	bus_size_t boundary, align;
    672  1.81.44.2      yamt 	u_long dvmaddr, sgstart, sgend, bmask;
    673       1.58       chs 	struct pglist *pglist;
    674  1.81.44.2      yamt 	const int pagesz = PAGE_SIZE;
    675  1.81.44.2      yamt #ifdef DEBUG
    676  1.81.44.2      yamt 	int npg = 0;
    677  1.81.44.2      yamt #endif
    678        1.9       eeh 
    679        1.9       eeh 	if (map->dm_nsegs) {
    680        1.9       eeh 		/* Already in use?? */
    681        1.9       eeh #ifdef DIAGNOSTIC
    682        1.9       eeh 		printf("iommu_dvmamap_load_raw: map still in use\n");
    683        1.9       eeh #endif
    684        1.9       eeh 		bus_dmamap_unload(t, map);
    685        1.9       eeh 	}
    686       1.40       eeh 
    687       1.40       eeh 	/*
    688       1.40       eeh 	 * A boundary presented to bus_dmamem_alloc() takes precedence
    689       1.40       eeh 	 * over boundary in the map.
    690       1.40       eeh 	 */
    691       1.40       eeh 	if ((boundary = segs[0]._ds_boundary) == 0)
    692       1.40       eeh 		boundary = map->_dm_boundary;
    693       1.40       eeh 
    694       1.45       eeh 	align = max(segs[0]._ds_align, pagesz);
    695       1.40       eeh 
    696        1.9       eeh 	/*
    697        1.9       eeh 	 * Make sure that on error condition we return "no valid mappings".
    698        1.9       eeh 	 */
    699        1.9       eeh 	map->dm_nsegs = 0;
    700       1.26    martin 	/* Count up the total number of pages we need */
    701  1.81.44.2      yamt 	pa = trunc_page(segs[0].ds_addr);
    702       1.26    martin 	sgsize = 0;
    703       1.40       eeh 	left = size;
    704  1.81.44.2      yamt 	for (i = 0; left > 0 && i < nsegs; i++) {
    705       1.26    martin 		if (round_page(pa) != round_page(segs[i].ds_addr))
    706  1.81.44.2      yamt 			sgsize = round_page(sgsize) +
    707  1.81.44.2      yamt 			    (segs[i].ds_addr & PGOFSET);
    708       1.40       eeh 		sgsize += min(left, segs[i].ds_len);
    709       1.40       eeh 		left -= segs[i].ds_len;
    710       1.26    martin 		pa = segs[i].ds_addr + segs[i].ds_len;
    711       1.26    martin 	}
    712  1.81.44.2      yamt 	sgsize = round_page(sgsize);
    713        1.9       eeh 
    714       1.40       eeh 	s = splhigh();
    715       1.58       chs 	/*
    716       1.58       chs 	 * If our segment size is larger than the boundary we need to
    717       1.45       eeh 	 * split the transfer up into little pieces ourselves.
    718        1.9       eeh 	 */
    719       1.40       eeh 	err = extent_alloc(is->is_dvmamap, sgsize, align,
    720       1.40       eeh 		(sgsize > boundary) ? 0 : boundary,
    721       1.40       eeh 		((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT) |
    722       1.54       eeh 		EX_BOUNDZERO, &dvmaddr);
    723        1.9       eeh 	splx(s);
    724        1.9       eeh 
    725        1.9       eeh 	if (err != 0)
    726        1.9       eeh 		return (err);
    727        1.9       eeh 
    728        1.9       eeh #ifdef DEBUG
    729       1.65  nakayama 	if (dvmaddr == (u_long)-1)
    730       1.58       chs 	{
    731        1.9       eeh 		printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
    732       1.25       mrg 		    (int)sgsize, flags);
    733       1.57       chs #ifdef DDB
    734        1.9       eeh 		Debugger();
    735       1.57       chs #endif
    736       1.58       chs 	}
    737       1.58       chs #endif
    738       1.65  nakayama 	if (dvmaddr == (u_long)-1)
    739        1.9       eeh 		return (ENOMEM);
    740        1.9       eeh 
    741       1.40       eeh 	/* Set the active DVMA map */
    742       1.40       eeh 	map->_dm_dvmastart = dvmaddr;
    743       1.40       eeh 	map->_dm_dvmasize = sgsize;
    744       1.40       eeh 
    745  1.81.44.2      yamt 	bmask = ~(boundary - 1);
    746       1.58       chs 	if ((pglist = segs[0]._ds_mlist) == NULL) {
    747  1.81.44.2      yamt 		u_long prev_va = 0UL, last_va = dvmaddr;
    748       1.45       eeh 		paddr_t prev_pa = 0;
    749       1.45       eeh 		int end = 0, offset;
    750  1.81.44.2      yamt 		bus_size_t len = size;
    751       1.45       eeh 
    752       1.26    martin 		/*
    753       1.45       eeh 		 * This segs is made up of individual physical
    754       1.58       chs 		 *  segments, probably by _bus_dmamap_load_uio() or
    755       1.26    martin 		 * _bus_dmamap_load_mbuf().  Ignore the mlist and
    756       1.45       eeh 		 * load each one individually.
    757       1.26    martin 		 */
    758       1.45       eeh 		j = 0;
    759  1.81.44.2      yamt 		needsflush = 0;
    760       1.45       eeh 		for (i = 0; i < nsegs ; i++) {
    761       1.40       eeh 
    762       1.45       eeh 			pa = segs[i].ds_addr;
    763       1.45       eeh 			offset = (pa & PGOFSET);
    764       1.45       eeh 			pa = trunc_page(pa);
    765       1.45       eeh 			dvmaddr = trunc_page(dvmaddr);
    766  1.81.44.2      yamt 			left = min(len, segs[i].ds_len);
    767       1.45       eeh 
    768       1.45       eeh 			DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: converting "
    769       1.58       chs 				"physseg %d start %lx size %lx\n", i,
    770       1.61    martin 				(long)segs[i].ds_addr, (long)segs[i].ds_len));
    771       1.26    martin 
    772       1.58       chs 			if ((pa == prev_pa) &&
    773       1.47       eeh 				((offset != 0) || (end != offset))) {
    774       1.45       eeh 				/* We can re-use this mapping */
    775       1.45       eeh 				dvmaddr = prev_va;
    776       1.45       eeh 			}
    777       1.29    martin 
    778       1.45       eeh 			sgstart = dvmaddr + offset;
    779       1.45       eeh 			sgend = sgstart + left - 1;
    780       1.26    martin 
    781       1.45       eeh 			/* Are the segments virtually adjacent? */
    782       1.58       chs 			if ((j > 0) && (end == offset) &&
    783  1.81.44.2      yamt 			    ((offset == 0) || (pa == prev_pa)) &&
    784  1.81.44.2      yamt 			    (map->dm_segs[j-1].ds_len + left <=
    785  1.81.44.2      yamt 			     map->dm_maxsegsz)) {
    786       1.45       eeh 				/* Just append to the previous segment. */
    787       1.45       eeh 				map->dm_segs[--j].ds_len += left;
    788  1.81.44.2      yamt 				/* Restore sgstart for boundary check */
    789  1.81.44.2      yamt 				sgstart = map->dm_segs[j].ds_addr;
    790       1.45       eeh 				DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    791       1.45       eeh 					"appending seg %d start %lx size %lx\n", j,
    792       1.58       chs 					(long)map->dm_segs[j].ds_addr,
    793       1.61    martin 					(long)map->dm_segs[j].ds_len));
    794       1.45       eeh 			} else {
    795       1.53       eeh 				if (j >= map->_dm_segcnt) {
    796  1.81.44.2      yamt 					iommu_remove(is, map->_dm_dvmastart,
    797  1.81.44.2      yamt 					    last_va - map->_dm_dvmastart);
    798  1.81.44.2      yamt 					goto fail;
    799       1.53       eeh 				}
    800       1.45       eeh 				map->dm_segs[j].ds_addr = sgstart;
    801       1.45       eeh 				map->dm_segs[j].ds_len = left;
    802       1.45       eeh 				DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    803       1.45       eeh 					"seg %d start %lx size %lx\n", j,
    804       1.48       eeh 					(long)map->dm_segs[j].ds_addr,
    805       1.61    martin 					(long)map->dm_segs[j].ds_len));
    806       1.40       eeh 			}
    807       1.45       eeh 			end = (offset + left) & PGOFSET;
    808       1.40       eeh 
    809       1.40       eeh 			/* Check for boundary issues */
    810  1.81.44.2      yamt 			while ((sgstart & bmask) != (sgend & bmask)) {
    811       1.40       eeh 				/* Need a new segment. */
    812       1.40       eeh 				map->dm_segs[j].ds_len =
    813       1.53       eeh 					boundary - (sgstart & (boundary - 1));
    814       1.40       eeh 				DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    815       1.40       eeh 					"seg %d start %lx size %lx\n", j,
    816       1.58       chs 					(long)map->dm_segs[j].ds_addr,
    817       1.61    martin 					(long)map->dm_segs[j].ds_len));
    818       1.53       eeh 				if (++j >= map->_dm_segcnt) {
    819  1.81.44.2      yamt 					iommu_remove(is, map->_dm_dvmastart,
    820  1.81.44.2      yamt 					    last_va - map->_dm_dvmastart);
    821  1.81.44.2      yamt 					goto fail;
    822       1.40       eeh 				}
    823  1.81.44.2      yamt 				sgstart += map->dm_segs[j-1].ds_len;
    824       1.40       eeh 				map->dm_segs[j].ds_addr = sgstart;
    825       1.40       eeh 				map->dm_segs[j].ds_len = sgend - sgstart + 1;
    826       1.40       eeh 			}
    827       1.40       eeh 
    828       1.26    martin 			if (sgsize == 0)
    829       1.26    martin 				panic("iommu_dmamap_load_raw: size botch");
    830       1.40       eeh 
    831       1.45       eeh 			/* Now map a series of pages. */
    832       1.51       eeh 			while (dvmaddr <= sgend) {
    833       1.45       eeh 				DPRINTF(IDB_BUSDMA,
    834       1.45       eeh 					("iommu_dvmamap_load_raw: map %p "
    835       1.45       eeh 						"loading va %lx at pa %lx\n",
    836       1.45       eeh 						map, (long)dvmaddr,
    837       1.45       eeh 						(long)(pa)));
    838       1.45       eeh 				/* Enter it if we haven't before. */
    839  1.81.44.2      yamt 				if (prev_va != dvmaddr) {
    840       1.55       eeh 					iommu_enter(sb, prev_va = dvmaddr,
    841  1.81.44.2      yamt 					    prev_pa = pa,
    842  1.81.44.2      yamt 					    flags | IOTTE_DEBUG(++npg << 12));
    843  1.81.44.2      yamt 					needsflush = 1;
    844  1.81.44.2      yamt 				}
    845       1.45       eeh 				dvmaddr += pagesz;
    846       1.45       eeh 				pa += pagesz;
    847  1.81.44.2      yamt 				last_va = dvmaddr;
    848       1.45       eeh 			}
    849       1.45       eeh 
    850  1.81.44.2      yamt 			len -= left;
    851       1.45       eeh 			++j;
    852       1.26    martin 		}
    853  1.81.44.2      yamt 		if (needsflush)
    854  1.81.44.2      yamt 			iommu_strbuf_flush_done(sb);
    855       1.45       eeh 
    856  1.81.44.2      yamt 		map->dm_mapsize = size;
    857       1.45       eeh 		map->dm_nsegs = j;
    858       1.45       eeh #ifdef DIAGNOSTIC
    859       1.45       eeh 		{ int seg;
    860       1.45       eeh 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    861       1.45       eeh 		if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
    862       1.45       eeh 			map->dm_segs[seg].ds_addr > is->is_dvmaend) {
    863       1.45       eeh 			printf("seg %d dvmaddr %lx out of range %x - %x\n",
    864       1.58       chs 				seg, (long)map->dm_segs[seg].ds_addr,
    865       1.45       eeh 				is->is_dvmabase, is->is_dvmaend);
    866       1.57       chs #ifdef DDB
    867       1.45       eeh 			Debugger();
    868       1.57       chs #endif
    869       1.45       eeh 		}
    870       1.45       eeh 	}
    871       1.45       eeh 		}
    872       1.45       eeh #endif
    873       1.26    martin 		return (0);
    874       1.26    martin 	}
    875       1.58       chs 
    876        1.9       eeh 	/*
    877       1.40       eeh 	 * This was allocated with bus_dmamem_alloc.
    878       1.58       chs 	 * The pages are on a `pglist'.
    879        1.9       eeh 	 */
    880       1.26    martin 	i = 0;
    881       1.40       eeh 	sgstart = dvmaddr;
    882       1.40       eeh 	sgend = sgstart + size - 1;
    883       1.40       eeh 	map->dm_segs[i].ds_addr = sgstart;
    884  1.81.44.2      yamt 	while ((sgstart & bmask) != (sgend & bmask)) {
    885       1.40       eeh 		/* Oops.  We crossed a boundary.  Split the xfer. */
    886       1.53       eeh 		map->dm_segs[i].ds_len = boundary - (sgstart & (boundary - 1));
    887       1.40       eeh 		DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    888       1.40       eeh 			"seg %d start %lx size %lx\n", i,
    889       1.48       eeh 			(long)map->dm_segs[i].ds_addr,
    890       1.61    martin 			(long)map->dm_segs[i].ds_len));
    891       1.53       eeh 		if (++i >= map->_dm_segcnt) {
    892       1.40       eeh 			/* Too many segments.  Fail the operation. */
    893  1.81.44.2      yamt 			goto fail;
    894       1.40       eeh 		}
    895  1.81.44.2      yamt 		sgstart += map->dm_segs[i-1].ds_len;
    896       1.40       eeh 		map->dm_segs[i].ds_addr = sgstart;
    897       1.40       eeh 	}
    898       1.40       eeh 	DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    899       1.40       eeh 			"seg %d start %lx size %lx\n", i,
    900       1.61    martin 			(long)map->dm_segs[i].ds_addr, (long)map->dm_segs[i].ds_len));
    901       1.40       eeh 	map->dm_segs[i].ds_len = sgend - sgstart + 1;
    902        1.9       eeh 
    903  1.81.44.2      yamt 	needsflush = 0;
    904  1.81.44.1      yamt 	TAILQ_FOREACH(pg, pglist, pageq.queue) {
    905        1.9       eeh 		if (sgsize == 0)
    906        1.9       eeh 			panic("iommu_dmamap_load_raw: size botch");
    907       1.58       chs 		pa = VM_PAGE_TO_PHYS(pg);
    908        1.9       eeh 
    909       1.22       mrg 		DPRINTF(IDB_BUSDMA,
    910        1.9       eeh 		    ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
    911        1.9       eeh 		    map, (long)dvmaddr, (long)(pa)));
    912  1.81.44.2      yamt 		iommu_enter(sb, dvmaddr, pa, flags | IOTTE_DEBUG(0x8000));
    913  1.81.44.2      yamt 		needsflush = 1;
    914       1.58       chs 
    915        1.9       eeh 		dvmaddr += pagesz;
    916        1.9       eeh 		sgsize -= pagesz;
    917        1.9       eeh 	}
    918  1.81.44.2      yamt 	if (needsflush)
    919  1.81.44.2      yamt 		iommu_strbuf_flush_done(sb);
    920       1.40       eeh 	map->dm_mapsize = size;
    921       1.40       eeh 	map->dm_nsegs = i+1;
    922       1.45       eeh #ifdef DIAGNOSTIC
    923       1.45       eeh 	{ int seg;
    924       1.45       eeh 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    925       1.45       eeh 		if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
    926       1.45       eeh 			map->dm_segs[seg].ds_addr > is->is_dvmaend) {
    927       1.45       eeh 			printf("seg %d dvmaddr %lx out of range %x - %x\n",
    928       1.58       chs 				seg, (long)map->dm_segs[seg].ds_addr,
    929       1.45       eeh 				is->is_dvmabase, is->is_dvmaend);
    930       1.57       chs #ifdef DDB
    931       1.45       eeh 			Debugger();
    932       1.57       chs #endif
    933       1.45       eeh 		}
    934       1.45       eeh 	}
    935       1.45       eeh 	}
    936       1.45       eeh #endif
    937        1.9       eeh 	return (0);
    938  1.81.44.2      yamt 
    939  1.81.44.2      yamt fail:
    940  1.81.44.2      yamt 	s = splhigh();
    941  1.81.44.2      yamt 	err = extent_free(is->is_dvmamap, map->_dm_dvmastart, sgsize,
    942  1.81.44.2      yamt 	    EX_NOWAIT);
    943  1.81.44.2      yamt 	map->_dm_dvmastart = 0;
    944  1.81.44.2      yamt 	map->_dm_dvmasize = 0;
    945  1.81.44.2      yamt 	splx(s);
    946  1.81.44.2      yamt 	if (err != 0)
    947  1.81.44.2      yamt 		printf("warning: %s: %" PRId64 " of DVMA space lost\n",
    948  1.81.44.2      yamt 		    __func__, sgsize);
    949  1.81.44.2      yamt 	return (EFBIG);
    950        1.7       mrg }
    951        1.7       mrg 
    952       1.67    petrov 
    953       1.67    petrov /*
    954       1.67    petrov  * Flush an individual dma segment, returns non-zero if the streaming buffers
    955       1.67    petrov  * need flushing afterwards.
    956       1.67    petrov  */
    957       1.67    petrov static int
    958       1.67    petrov iommu_dvmamap_sync_range(struct strbuf_ctl *sb, vaddr_t va, bus_size_t len)
    959       1.67    petrov {
    960       1.67    petrov 	vaddr_t vaend;
    961       1.67    petrov 	struct iommu_state *is = sb->sb_is;
    962       1.67    petrov 
    963       1.67    petrov #ifdef DIAGNOSTIC
    964       1.67    petrov 	if (va < is->is_dvmabase || va > is->is_dvmaend)
    965       1.67    petrov 		panic("invalid va: %llx", (long long)va);
    966       1.67    petrov #endif
    967       1.67    petrov 
    968       1.67    petrov 	if ((is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)] & IOTTE_STREAM) == 0) {
    969       1.67    petrov 		DPRINTF(IDB_BUSDMA,
    970       1.67    petrov 			("iommu_dvmamap_sync_range: attempting to flush "
    971       1.67    petrov 			 "non-streaming entry\n"));
    972       1.67    petrov 		return (0);
    973       1.67    petrov 	}
    974       1.67    petrov 
    975  1.81.44.2      yamt 	vaend = round_page(va + len) - 1;
    976  1.81.44.2      yamt 	va = trunc_page(va);
    977       1.67    petrov 
    978       1.67    petrov #ifdef DIAGNOSTIC
    979       1.67    petrov 	if (va < is->is_dvmabase || vaend > is->is_dvmaend)
    980       1.67    petrov 		panic("invalid va range: %llx to %llx (%x to %x)",
    981       1.67    petrov 		    (long long)va, (long long)vaend,
    982       1.67    petrov 		    is->is_dvmabase,
    983       1.67    petrov 		    is->is_dvmaend);
    984       1.67    petrov #endif
    985       1.67    petrov 
    986       1.67    petrov 	for ( ; va <= vaend; va += PAGE_SIZE) {
    987       1.67    petrov 		DPRINTF(IDB_BUSDMA,
    988       1.67    petrov 		    ("iommu_dvmamap_sync_range: flushing va %p\n",
    989       1.67    petrov 		    (void *)(u_long)va));
    990       1.67    petrov 		iommu_strbuf_flush(sb, va);
    991       1.67    petrov 	}
    992       1.67    petrov 
    993       1.67    petrov 	return (1);
    994       1.67    petrov }
    995       1.67    petrov 
    996  1.81.44.1      yamt static void
    997  1.81.44.1      yamt _iommu_dvmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    998  1.81.44.1      yamt 	bus_size_t len, int ops)
    999        1.7       mrg {
   1000  1.81.44.1      yamt 	struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
   1001       1.67    petrov 	bus_size_t count;
   1002       1.67    petrov 	int i, needsflush = 0;
   1003       1.63    petrov 
   1004       1.63    petrov 	if (!sb->sb_flush)
   1005       1.63    petrov 		return;
   1006        1.7       mrg 
   1007       1.67    petrov 	for (i = 0; i < map->dm_nsegs; i++) {
   1008       1.67    petrov 		if (offset < map->dm_segs[i].ds_len)
   1009       1.67    petrov 			break;
   1010       1.67    petrov 		offset -= map->dm_segs[i].ds_len;
   1011       1.67    petrov 	}
   1012       1.60    petrov 
   1013       1.67    petrov 	if (i == map->dm_nsegs)
   1014       1.68    martin 		panic("iommu_dvmamap_sync: segment too short %llu",
   1015       1.68    martin 		    (unsigned long long)offset);
   1016       1.60    petrov 
   1017       1.62    petrov 	if (ops & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_POSTWRITE)) {
   1018       1.60    petrov 		/* Nothing to do */;
   1019       1.60    petrov 	}
   1020       1.60    petrov 
   1021       1.62    petrov 	if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_PREWRITE)) {
   1022       1.67    petrov 
   1023       1.67    petrov 		for (; len > 0 && i < map->dm_nsegs; i++) {
   1024       1.67    petrov 			count = MIN(map->dm_segs[i].ds_len - offset, len);
   1025       1.67    petrov 			if (count > 0 &&
   1026       1.67    petrov 			    iommu_dvmamap_sync_range(sb,
   1027       1.67    petrov 				map->dm_segs[i].ds_addr + offset, count))
   1028       1.67    petrov 				needsflush = 1;
   1029       1.67    petrov 			offset = 0;
   1030       1.67    petrov 			len -= count;
   1031       1.67    petrov 		}
   1032       1.60    petrov #ifdef DIAGNOSTIC
   1033       1.67    petrov 		if (i == map->dm_nsegs && len > 0)
   1034       1.73  nakayama 			panic("iommu_dvmamap_sync: leftover %llu",
   1035       1.73  nakayama 			    (unsigned long long)len);
   1036       1.60    petrov #endif
   1037       1.55       eeh 
   1038       1.67    petrov 		if (needsflush)
   1039       1.58       chs 			iommu_strbuf_flush_done(sb);
   1040        1.7       mrg 	}
   1041        1.7       mrg }
   1042        1.7       mrg 
   1043  1.81.44.1      yamt void
   1044  1.81.44.1      yamt iommu_dvmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
   1045  1.81.44.1      yamt 	bus_size_t len, int ops)
   1046  1.81.44.1      yamt {
   1047  1.81.44.1      yamt 
   1048  1.81.44.2      yamt 	/* If len is 0, then there is nothing to do */
   1049  1.81.44.2      yamt 	if (len == 0)
   1050  1.81.44.2      yamt 		return;
   1051  1.81.44.2      yamt 
   1052  1.81.44.1      yamt 	if (ops & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE)) {
   1053  1.81.44.1      yamt 		/* Flush the CPU then the IOMMU */
   1054  1.81.44.1      yamt 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1055  1.81.44.1      yamt 		_iommu_dvmamap_sync(t, map, offset, len, ops);
   1056  1.81.44.1      yamt 	}
   1057  1.81.44.1      yamt 	if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE)) {
   1058  1.81.44.1      yamt 		/* Flush the IOMMU then the CPU */
   1059  1.81.44.1      yamt 		_iommu_dvmamap_sync(t, map, offset, len, ops);
   1060  1.81.44.1      yamt 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1061  1.81.44.1      yamt 	}
   1062  1.81.44.1      yamt }
   1063  1.81.44.1      yamt 
   1064        1.7       mrg int
   1065  1.81.44.1      yamt iommu_dvmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
   1066  1.81.44.1      yamt 	bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
   1067  1.81.44.1      yamt 	int flags)
   1068        1.7       mrg {
   1069        1.7       mrg 
   1070       1.25       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx "
   1071       1.25       mrg 	   "segp %p flags %d\n", (unsigned long long)size,
   1072       1.25       mrg 	   (unsigned long long)alignment, (unsigned long long)boundary,
   1073       1.25       mrg 	   segs, flags));
   1074        1.7       mrg 	return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
   1075       1.21       eeh 	    segs, nsegs, rsegs, flags|BUS_DMA_DVMA));
   1076        1.7       mrg }
   1077        1.7       mrg 
   1078        1.7       mrg void
   1079  1.81.44.1      yamt iommu_dvmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
   1080        1.7       mrg {
   1081        1.7       mrg 
   1082       1.22       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
   1083        1.7       mrg 	    segs, nsegs));
   1084        1.7       mrg 	bus_dmamem_free(t->_parent, segs, nsegs);
   1085        1.7       mrg }
   1086        1.7       mrg 
   1087        1.7       mrg /*
   1088        1.7       mrg  * Map the DVMA mappings into the kernel pmap.
   1089        1.7       mrg  * Check the flags to see whether we're streaming or coherent.
   1090        1.7       mrg  */
   1091        1.7       mrg int
   1092  1.81.44.1      yamt iommu_dvmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
   1093  1.81.44.1      yamt 	size_t size, void **kvap, int flags)
   1094        1.7       mrg {
   1095       1.58       chs 	struct vm_page *pg;
   1096        1.7       mrg 	vaddr_t va;
   1097        1.7       mrg 	bus_addr_t addr;
   1098       1.58       chs 	struct pglist *pglist;
   1099        1.8       mrg 	int cbit;
   1100       1.77      yamt 	const uvm_flag_t kmflags =
   1101       1.77      yamt 	    (flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0;
   1102        1.7       mrg 
   1103       1.22       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
   1104        1.7       mrg 	    segs, nsegs, size));
   1105        1.7       mrg 
   1106        1.7       mrg 	/*
   1107        1.8       mrg 	 * Allocate some space in the kernel map, and then map these pages
   1108        1.8       mrg 	 * into this space.
   1109        1.7       mrg 	 */
   1110        1.8       mrg 	size = round_page(size);
   1111       1.77      yamt 	va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY | kmflags);
   1112        1.8       mrg 	if (va == 0)
   1113        1.8       mrg 		return (ENOMEM);
   1114        1.7       mrg 
   1115       1.81  christos 	*kvap = (void *)va;
   1116        1.7       mrg 
   1117       1.58       chs 	/*
   1118        1.7       mrg 	 * digest flags:
   1119        1.7       mrg 	 */
   1120        1.7       mrg 	cbit = 0;
   1121        1.7       mrg 	if (flags & BUS_DMA_COHERENT)	/* Disable vcache */
   1122        1.7       mrg 		cbit |= PMAP_NVC;
   1123  1.81.44.2      yamt 	if (flags & BUS_DMA_NOCACHE)	/* side effects */
   1124        1.7       mrg 		cbit |= PMAP_NC;
   1125        1.7       mrg 
   1126        1.7       mrg 	/*
   1127        1.8       mrg 	 * Now take this and map it into the CPU.
   1128        1.7       mrg 	 */
   1129       1.58       chs 	pglist = segs[0]._ds_mlist;
   1130  1.81.44.1      yamt 	TAILQ_FOREACH(pg, pglist, pageq.queue) {
   1131        1.8       mrg #ifdef DIAGNOSTIC
   1132        1.7       mrg 		if (size == 0)
   1133        1.7       mrg 			panic("iommu_dvmamem_map: size botch");
   1134        1.8       mrg #endif
   1135       1.58       chs 		addr = VM_PAGE_TO_PHYS(pg);
   1136       1.22       mrg 		DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: "
   1137       1.25       mrg 		    "mapping va %lx at %llx\n", va, (unsigned long long)addr | cbit));
   1138  1.81.44.2      yamt 		pmap_kenter_pa(va, addr | cbit,
   1139  1.81.44.2      yamt 		    VM_PROT_READ | VM_PROT_WRITE, 0);
   1140        1.7       mrg 		va += PAGE_SIZE;
   1141        1.7       mrg 		size -= PAGE_SIZE;
   1142        1.7       mrg 	}
   1143       1.38     chris 	pmap_update(pmap_kernel());
   1144        1.7       mrg 	return (0);
   1145        1.7       mrg }
   1146        1.7       mrg 
   1147        1.7       mrg /*
   1148        1.7       mrg  * Unmap DVMA mappings from kernel
   1149        1.7       mrg  */
   1150        1.7       mrg void
   1151  1.81.44.1      yamt iommu_dvmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
   1152        1.7       mrg {
   1153       1.58       chs 
   1154       1.22       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
   1155        1.7       mrg 	    kva, size));
   1156       1.58       chs 
   1157        1.7       mrg #ifdef DIAGNOSTIC
   1158        1.7       mrg 	if ((u_long)kva & PGOFSET)
   1159        1.7       mrg 		panic("iommu_dvmamem_unmap");
   1160        1.7       mrg #endif
   1161       1.58       chs 
   1162        1.7       mrg 	size = round_page(size);
   1163       1.58       chs 	pmap_kremove((vaddr_t)kva, size);
   1164       1.38     chris 	pmap_update(pmap_kernel());
   1165       1.76      yamt 	uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
   1166        1.1       mrg }
   1167