Home | History | Annotate | Line # | Download | only in dev
iommu.c revision 1.81.44.2
      1  1.81.44.2      yamt /*	$NetBSD: iommu.c,v 1.81.44.2 2010/03/11 15:03:00 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.2      yamt __KERNEL_RCSID(0, "$NetBSD: iommu.c,v 1.81.44.2 2010/03/11 15:03:00 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.58       chs 	printf("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.58       chs 	printf("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.1       mrg }
    217        1.1       mrg 
    218        1.8       mrg /*
    219        1.8       mrg  * Streaming buffers don't exist on the UltraSPARC IIi; we should have
    220        1.8       mrg  * detected that already and disabled them.  If not, we will notice that
    221        1.8       mrg  * they aren't there when the STRBUF_EN bit does not remain.
    222        1.8       mrg  */
    223        1.1       mrg void
    224       1.78       cdi iommu_reset(struct iommu_state *is)
    225        1.1       mrg {
    226       1.45       eeh 	int i;
    227       1.55       eeh 	struct strbuf_ctl *sb;
    228        1.1       mrg 
    229        1.1       mrg 	/* Need to do 64-bit stores */
    230       1.58       chs 	bus_space_write_8(is->is_bustag, is->is_iommu, IOMMUREG(iommu_tsb),
    231       1.50       eeh 		is->is_ptsb);
    232       1.50       eeh 
    233       1.11       eeh 	/* Enable IOMMU in diagnostic mode */
    234       1.50       eeh 	bus_space_write_8(is->is_bustag, is->is_iommu, IOMMUREG(iommu_cr),
    235       1.50       eeh 		is->is_cr|IOMMUCR_DE);
    236       1.11       eeh 
    237       1.58       chs 	for (i = 0; i < 2; i++) {
    238       1.55       eeh 		if ((sb = is->is_sb[i])) {
    239        1.5       mrg 
    240       1.45       eeh 			/* Enable diagnostics mode? */
    241       1.58       chs 			bus_space_write_8(is->is_bustag, is->is_sb[i]->sb_sb,
    242       1.50       eeh 				STRBUFREG(strbuf_ctl), STRBUF_EN);
    243       1.45       eeh 
    244       1.45       eeh 			/* No streaming buffers? Disable them */
    245       1.58       chs 			if (bus_space_read_8(is->is_bustag,
    246       1.58       chs 				is->is_sb[i]->sb_sb,
    247       1.55       eeh 				STRBUFREG(strbuf_ctl)) == 0) {
    248       1.55       eeh 				is->is_sb[i]->sb_flush = NULL;
    249       1.55       eeh 			} else {
    250       1.58       chs 
    251       1.55       eeh 				/*
    252       1.55       eeh 				 * locate the pa of the flush buffer.
    253       1.55       eeh 				 */
    254       1.55       eeh 				(void)pmap_extract(pmap_kernel(),
    255       1.55       eeh 					(vaddr_t)is->is_sb[i]->sb_flush,
    256       1.55       eeh 					&is->is_sb[i]->sb_flushpa);
    257       1.55       eeh 			}
    258       1.45       eeh 		}
    259       1.42       eeh 	}
    260        1.2       eeh }
    261        1.2       eeh 
    262        1.2       eeh /*
    263       1.58       chs  * Here are the iommu control routines.
    264        1.2       eeh  */
    265        1.2       eeh void
    266       1.78       cdi iommu_enter(struct strbuf_ctl *sb, vaddr_t va, int64_t pa, int flags)
    267        1.2       eeh {
    268       1.55       eeh 	struct iommu_state *is = sb->sb_is;
    269       1.55       eeh 	int strbuf = (flags & BUS_DMA_STREAMING);
    270        1.2       eeh 	int64_t tte;
    271        1.2       eeh 
    272        1.2       eeh #ifdef DIAGNOSTIC
    273       1.45       eeh 	if (va < is->is_dvmabase || va > is->is_dvmaend)
    274       1.13       mrg 		panic("iommu_enter: va %#lx not in DVMA space", va);
    275        1.2       eeh #endif
    276        1.2       eeh 
    277       1.55       eeh 	/* Is the streamcache flush really needed? */
    278  1.81.44.2      yamt 	if (sb->sb_flush)
    279       1.55       eeh 		iommu_strbuf_flush(sb, va);
    280  1.81.44.2      yamt 	else
    281       1.55       eeh 		/* If we can't flush the strbuf don't enable it. */
    282       1.55       eeh 		strbuf = 0;
    283       1.55       eeh 
    284       1.58       chs 	tte = MAKEIOTTE(pa, !(flags & BUS_DMA_NOWRITE),
    285       1.55       eeh 		!(flags & BUS_DMA_NOCACHE), (strbuf));
    286       1.50       eeh #ifdef DEBUG
    287       1.50       eeh 	tte |= (flags & 0xff000LL)<<(4*8);
    288       1.50       eeh #endif
    289       1.58       chs 
    290       1.58       chs 	DPRINTF(IDB_IOMMU, ("Clearing TSB slot %d for va %p\n",
    291       1.25       mrg 		       (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va));
    292        1.2       eeh 	is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
    293       1.58       chs 	bus_space_write_8(is->is_bustag, is->is_iommu,
    294       1.50       eeh 		IOMMUREG(iommu_flush), va);
    295       1.22       mrg 	DPRINTF(IDB_IOMMU, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
    296       1.50       eeh 		va, (long)pa, (u_long)IOTSBSLOT(va,is->is_tsbsize),
    297       1.50       eeh 		(void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
    298       1.50       eeh 		(u_long)tte));
    299       1.39       eeh }
    300       1.39       eeh 
    301       1.39       eeh /*
    302       1.39       eeh  * Find the value of a DVMA address (debug routine).
    303       1.39       eeh  */
    304       1.39       eeh paddr_t
    305       1.78       cdi iommu_extract(struct iommu_state *is, vaddr_t dva)
    306       1.39       eeh {
    307       1.39       eeh 	int64_t tte = 0;
    308       1.58       chs 
    309  1.81.44.2      yamt 	if (dva >= is->is_dvmabase && dva <= is->is_dvmaend)
    310       1.55       eeh 		tte = is->is_tsb[IOTSBSLOT(dva, is->is_tsbsize)];
    311       1.39       eeh 
    312       1.54       eeh 	if ((tte & IOTTE_V) == 0)
    313       1.39       eeh 		return ((paddr_t)-1L);
    314       1.54       eeh 	return (tte & IOTTE_PAMASK);
    315        1.2       eeh }
    316        1.2       eeh 
    317        1.2       eeh /*
    318        1.2       eeh  * iommu_remove: removes mappings created by iommu_enter
    319        1.2       eeh  *
    320        1.2       eeh  * Only demap from IOMMU if flag is set.
    321        1.8       mrg  *
    322        1.8       mrg  * XXX: this function needs better internal error checking.
    323        1.2       eeh  */
    324        1.2       eeh void
    325       1.78       cdi iommu_remove(struct iommu_state *is, vaddr_t va, size_t len)
    326        1.2       eeh {
    327        1.2       eeh 
    328        1.2       eeh #ifdef DIAGNOSTIC
    329       1.45       eeh 	if (va < is->is_dvmabase || va > is->is_dvmaend)
    330       1.25       mrg 		panic("iommu_remove: va 0x%lx not in DVMA space", (u_long)va);
    331        1.2       eeh 	if ((long)(va + len) < (long)va)
    332       1.58       chs 		panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
    333        1.2       eeh 		      (long) va, (long) len);
    334       1.58       chs 	if (len & ~0xfffffff)
    335       1.72       snj 		panic("iommu_remove: ridiculous len 0x%lx", (u_long)len);
    336        1.2       eeh #endif
    337        1.2       eeh 
    338        1.2       eeh 	va = trunc_page(va);
    339       1.22       mrg 	DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx TSB[%lx]@%p\n",
    340       1.50       eeh 		va, (u_long)IOTSBSLOT(va, is->is_tsbsize),
    341       1.50       eeh 		&is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)]));
    342        1.2       eeh 	while (len > 0) {
    343       1.50       eeh 		DPRINTF(IDB_IOMMU, ("iommu_remove: clearing TSB slot %d "
    344       1.50       eeh 			"for va %p size %lx\n",
    345       1.50       eeh 			(int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va,
    346       1.50       eeh 			(u_long)len));
    347       1.64   thorpej 		if (len <= PAGE_SIZE)
    348       1.10       mrg 			len = 0;
    349       1.10       mrg 		else
    350       1.64   thorpej 			len -= PAGE_SIZE;
    351        1.8       mrg 
    352  1.81.44.2      yamt 		/*
    353  1.81.44.2      yamt 		 * XXX Zero-ing the entry would not require RMW
    354  1.81.44.2      yamt 		 *
    355  1.81.44.2      yamt 		 * Disabling valid bit while a page is used by a device
    356  1.81.44.2      yamt 		 * causes an uncorrectable DMA error.
    357  1.81.44.2      yamt 		 * Workaround to avoid an uncorrectable DMA error is
    358  1.81.44.2      yamt 		 * eliminating the next line, but the page is mapped
    359  1.81.44.2      yamt 		 * until the next iommu_enter call.
    360  1.81.44.2      yamt 		 */
    361       1.47       eeh 		is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] &= ~IOTTE_V;
    362  1.81.44.2      yamt 		membar_storestore();
    363       1.58       chs 		bus_space_write_8(is->is_bustag, is->is_iommu,
    364       1.50       eeh 			IOMMUREG(iommu_flush), va);
    365       1.64   thorpej 		va += PAGE_SIZE;
    366        1.2       eeh 	}
    367        1.2       eeh }
    368        1.2       eeh 
    369       1.58       chs static int
    370       1.78       cdi iommu_strbuf_flush_done(struct strbuf_ctl *sb)
    371        1.2       eeh {
    372       1.55       eeh 	struct iommu_state *is = sb->sb_is;
    373        1.2       eeh 	struct timeval cur, flushtimeout;
    374        1.2       eeh 
    375        1.2       eeh #define BUMPTIME(t, usec) { \
    376        1.2       eeh 	register volatile struct timeval *tp = (t); \
    377        1.2       eeh 	register long us; \
    378        1.2       eeh  \
    379        1.2       eeh 	tp->tv_usec = us = tp->tv_usec + (usec); \
    380        1.2       eeh 	if (us >= 1000000) { \
    381        1.2       eeh 		tp->tv_usec = us - 1000000; \
    382        1.2       eeh 		tp->tv_sec++; \
    383        1.2       eeh 	} \
    384        1.2       eeh }
    385        1.5       mrg 
    386       1.55       eeh 	if (!sb->sb_flush)
    387        1.5       mrg 		return (0);
    388       1.58       chs 
    389        1.7       mrg 	/*
    390        1.7       mrg 	 * Streaming buffer flushes:
    391       1.58       chs 	 *
    392        1.7       mrg 	 *   1 Tell strbuf to flush by storing va to strbuf_pgflush.  If
    393        1.7       mrg 	 *     we're not on a cache line boundary (64-bits):
    394        1.7       mrg 	 *   2 Store 0 in flag
    395        1.7       mrg 	 *   3 Store pointer to flag in flushsync
    396        1.7       mrg 	 *   4 wait till flushsync becomes 0x1
    397        1.7       mrg 	 *
    398        1.7       mrg 	 * If it takes more than .5 sec, something
    399        1.7       mrg 	 * went wrong.
    400        1.7       mrg 	 */
    401        1.2       eeh 
    402       1.55       eeh 	*sb->sb_flush = 0;
    403       1.58       chs 	bus_space_write_8(is->is_bustag, sb->sb_sb,
    404       1.55       eeh 		STRBUFREG(strbuf_flushsync), sb->sb_flushpa);
    405        1.2       eeh 
    406       1.58       chs 	microtime(&flushtimeout);
    407        1.2       eeh 	cur = flushtimeout;
    408        1.2       eeh 	BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
    409       1.58       chs 
    410       1.55       eeh 	DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flush = %lx "
    411  1.81.44.1      yamt 		"at va = %lx pa = %lx now=%"PRIx64":%"PRIx32" until = %"PRIx64":%"PRIx32"\n",
    412       1.58       chs 		(long)*sb->sb_flush, (long)sb->sb_flush, (long)sb->sb_flushpa,
    413       1.42       eeh 		cur.tv_sec, cur.tv_usec,
    414       1.42       eeh 		flushtimeout.tv_sec, flushtimeout.tv_usec));
    415       1.42       eeh 
    416        1.2       eeh 	/* Bypass non-coherent D$ */
    417       1.55       eeh 	while ((!ldxa(sb->sb_flushpa, ASI_PHYS_CACHED)) &&
    418       1.59    martin 		timercmp(&cur, &flushtimeout, <=))
    419        1.2       eeh 		microtime(&cur);
    420        1.2       eeh 
    421        1.2       eeh #ifdef DIAGNOSTIC
    422       1.55       eeh 	if (!ldxa(sb->sb_flushpa, ASI_PHYS_CACHED)) {
    423       1.55       eeh 		printf("iommu_strbuf_flush_done: flush timeout %p, at %p\n",
    424       1.55       eeh 			(void *)(u_long)*sb->sb_flush,
    425       1.55       eeh 			(void *)(u_long)sb->sb_flushpa); /* panic? */
    426        1.2       eeh #ifdef DDB
    427        1.2       eeh 		Debugger();
    428        1.2       eeh #endif
    429        1.2       eeh 	}
    430        1.2       eeh #endif
    431       1.31       eeh 	DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flushed\n"));
    432       1.55       eeh 	return (*sb->sb_flush);
    433        1.7       mrg }
    434        1.7       mrg 
    435        1.7       mrg /*
    436        1.7       mrg  * IOMMU DVMA operations, common to SBUS and PCI.
    437        1.7       mrg  */
    438        1.7       mrg int
    439  1.81.44.1      yamt iommu_dvmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
    440  1.81.44.1      yamt 	bus_size_t buflen, struct proc *p, int flags)
    441        1.7       mrg {
    442  1.81.44.1      yamt 	struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
    443       1.55       eeh 	struct iommu_state *is = sb->sb_is;
    444        1.7       mrg 	int s;
    445  1.81.44.2      yamt 	int err, needsflush;
    446        1.7       mrg 	bus_size_t sgsize;
    447        1.7       mrg 	paddr_t curaddr;
    448  1.81.44.2      yamt 	u_long dvmaddr, sgstart, sgend, bmask;
    449       1.71   tsutsui 	bus_size_t align, boundary, len;
    450        1.7       mrg 	vaddr_t vaddr = (vaddr_t)buf;
    451       1.40       eeh 	int seg;
    452       1.58       chs 	struct pmap *pmap;
    453        1.7       mrg 
    454        1.7       mrg 	if (map->dm_nsegs) {
    455        1.7       mrg 		/* Already in use?? */
    456        1.7       mrg #ifdef DIAGNOSTIC
    457        1.7       mrg 		printf("iommu_dvmamap_load: map still in use\n");
    458        1.7       mrg #endif
    459        1.7       mrg 		bus_dmamap_unload(t, map);
    460        1.7       mrg 	}
    461       1.58       chs 
    462        1.7       mrg 	/*
    463        1.7       mrg 	 * Make sure that on error condition we return "no valid mappings".
    464        1.7       mrg 	 */
    465        1.7       mrg 	map->dm_nsegs = 0;
    466  1.81.44.2      yamt 	KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    467  1.81.44.2      yamt 
    468        1.7       mrg 	if (buflen > map->_dm_size) {
    469       1.22       mrg 		DPRINTF(IDB_BUSDMA,
    470        1.7       mrg 		    ("iommu_dvmamap_load(): error %d > %d -- "
    471       1.25       mrg 		     "map size exceeded!\n", (int)buflen, (int)map->_dm_size));
    472        1.7       mrg 		return (EINVAL);
    473        1.7       mrg 	}
    474        1.7       mrg 
    475        1.7       mrg 	sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
    476       1.20       mrg 
    477        1.7       mrg 	/*
    478       1.21       eeh 	 * A boundary presented to bus_dmamem_alloc() takes precedence
    479       1.21       eeh 	 * over boundary in the map.
    480        1.7       mrg 	 */
    481       1.21       eeh 	if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0)
    482       1.21       eeh 		boundary = map->_dm_boundary;
    483       1.64   thorpej 	align = max(map->dm_segs[0]._ds_align, PAGE_SIZE);
    484       1.58       chs 
    485       1.58       chs 	/*
    486       1.58       chs 	 * If our segment size is larger than the boundary we need to
    487       1.40       eeh 	 * split the transfer up int little pieces ourselves.
    488       1.40       eeh 	 */
    489       1.58       chs 	s = splhigh();
    490       1.58       chs 	err = extent_alloc(is->is_dvmamap, sgsize, align,
    491       1.71   tsutsui 	    (sgsize > boundary) ? 0 : boundary,
    492       1.71   tsutsui 	    EX_NOWAIT|EX_BOUNDZERO, &dvmaddr);
    493        1.7       mrg 	splx(s);
    494        1.7       mrg 
    495        1.7       mrg #ifdef DEBUG
    496       1.71   tsutsui 	if (err || (dvmaddr == (u_long)-1)) {
    497        1.7       mrg 		printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
    498       1.25       mrg 		    (int)sgsize, flags);
    499       1.40       eeh #ifdef DDB
    500        1.7       mrg 		Debugger();
    501       1.40       eeh #endif
    502       1.58       chs 	}
    503       1.58       chs #endif
    504       1.11       eeh 	if (err != 0)
    505       1.11       eeh 		return (err);
    506       1.11       eeh 
    507       1.65  nakayama 	if (dvmaddr == (u_long)-1)
    508        1.7       mrg 		return (ENOMEM);
    509        1.7       mrg 
    510       1.40       eeh 	/* Set the active DVMA map */
    511       1.40       eeh 	map->_dm_dvmastart = dvmaddr;
    512       1.40       eeh 	map->_dm_dvmasize = sgsize;
    513       1.40       eeh 
    514       1.40       eeh 	/*
    515       1.40       eeh 	 * Now split the DVMA range into segments, not crossing
    516       1.40       eeh 	 * the boundary.
    517       1.40       eeh 	 */
    518       1.40       eeh 	seg = 0;
    519       1.40       eeh 	sgstart = dvmaddr + (vaddr & PGOFSET);
    520       1.40       eeh 	sgend = sgstart + buflen - 1;
    521       1.40       eeh 	map->dm_segs[seg].ds_addr = sgstart;
    522       1.71   tsutsui 	DPRINTF(IDB_INFO, ("iommu_dvmamap_load: boundary %lx boundary - 1 %lx "
    523       1.71   tsutsui 	    "~(boundary - 1) %lx\n", (long)boundary, (long)(boundary - 1),
    524       1.71   tsutsui 	    (long)~(boundary - 1)));
    525  1.81.44.2      yamt 	bmask = ~(boundary - 1);
    526  1.81.44.2      yamt 	while ((sgstart & bmask) != (sgend & bmask) ||
    527  1.81.44.2      yamt 	       sgend - sgstart + 1 > map->dm_maxsegsz) {
    528  1.81.44.2      yamt 		/* Oops. We crossed a boundary or large seg. Split the xfer. */
    529  1.81.44.2      yamt 		len = map->dm_maxsegsz;
    530  1.81.44.2      yamt 		if ((sgstart & bmask) != (sgend & bmask))
    531  1.81.44.2      yamt 			len = min(len, boundary - (sgstart & (boundary - 1)));
    532       1.71   tsutsui 		map->dm_segs[seg].ds_len = len;
    533       1.40       eeh 		DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
    534       1.71   tsutsui 		    "seg %d start %lx size %lx\n", seg,
    535       1.71   tsutsui 		    (long)map->dm_segs[seg].ds_addr,
    536       1.71   tsutsui 		    (long)map->dm_segs[seg].ds_len));
    537       1.53       eeh 		if (++seg >= map->_dm_segcnt) {
    538       1.40       eeh 			/* Too many segments.  Fail the operation. */
    539       1.40       eeh 			DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
    540       1.71   tsutsui 			    "too many segments %d\n", seg));
    541       1.40       eeh 			s = splhigh();
    542       1.40       eeh 			err = extent_free(is->is_dvmamap,
    543       1.71   tsutsui 			    dvmaddr, sgsize, EX_NOWAIT);
    544       1.40       eeh 			map->_dm_dvmastart = 0;
    545       1.40       eeh 			map->_dm_dvmasize = 0;
    546       1.43       eeh 			splx(s);
    547  1.81.44.2      yamt 			if (err != 0)
    548  1.81.44.2      yamt 				printf("warning: %s: %" PRId64
    549  1.81.44.2      yamt 				    " of DVMA space lost\n", __func__, sgsize);
    550       1.80       mrg 			return (EFBIG);
    551       1.40       eeh 		}
    552       1.71   tsutsui 		sgstart += len;
    553       1.40       eeh 		map->dm_segs[seg].ds_addr = sgstart;
    554       1.40       eeh 	}
    555       1.40       eeh 	map->dm_segs[seg].ds_len = sgend - sgstart + 1;
    556       1.40       eeh 	DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
    557       1.71   tsutsui 	    "seg %d start %lx size %lx\n", seg,
    558       1.71   tsutsui 	    (long)map->dm_segs[seg].ds_addr, (long)map->dm_segs[seg].ds_len));
    559       1.71   tsutsui 	map->dm_nsegs = seg + 1;
    560        1.7       mrg 	map->dm_mapsize = buflen;
    561        1.7       mrg 
    562        1.7       mrg 	if (p != NULL)
    563        1.7       mrg 		pmap = p->p_vmspace->vm_map.pmap;
    564        1.7       mrg 	else
    565        1.7       mrg 		pmap = pmap_kernel();
    566        1.7       mrg 
    567  1.81.44.2      yamt 	needsflush = 0;
    568        1.7       mrg 	for (; buflen > 0; ) {
    569       1.58       chs 
    570        1.7       mrg 		/*
    571        1.7       mrg 		 * Get the physical address for this page.
    572        1.7       mrg 		 */
    573        1.7       mrg 		if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
    574       1.74    petrov #ifdef DIAGNOSTIC
    575       1.74    petrov 			printf("iommu_dvmamap_load: pmap_extract failed %lx\n", vaddr);
    576       1.74    petrov #endif
    577        1.7       mrg 			bus_dmamap_unload(t, map);
    578        1.7       mrg 			return (-1);
    579        1.7       mrg 		}
    580        1.7       mrg 
    581        1.7       mrg 		/*
    582        1.7       mrg 		 * Compute the segment size, and adjust counts.
    583        1.7       mrg 		 */
    584       1.64   thorpej 		sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
    585        1.7       mrg 		if (buflen < sgsize)
    586        1.7       mrg 			sgsize = buflen;
    587        1.7       mrg 
    588       1.22       mrg 		DPRINTF(IDB_BUSDMA,
    589       1.36       eeh 		    ("iommu_dvmamap_load: map %p loading va %p "
    590       1.71   tsutsui 		    "dva %lx at pa %lx\n",
    591       1.71   tsutsui 		    map, (void *)vaddr, (long)dvmaddr,
    592  1.81.44.2      yamt 		    (long)trunc_page(curaddr)));
    593       1.55       eeh 		iommu_enter(sb, trunc_page(dvmaddr), trunc_page(curaddr),
    594  1.81.44.2      yamt 		    flags | IOTTE_DEBUG(0x4000));
    595  1.81.44.2      yamt 		needsflush = 1;
    596       1.58       chs 
    597        1.7       mrg 		dvmaddr += PAGE_SIZE;
    598        1.7       mrg 		vaddr += sgsize;
    599        1.7       mrg 		buflen -= sgsize;
    600        1.7       mrg 	}
    601  1.81.44.2      yamt 	if (needsflush)
    602  1.81.44.2      yamt 		iommu_strbuf_flush_done(sb);
    603       1.45       eeh #ifdef DIAGNOSTIC
    604       1.45       eeh 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    605       1.45       eeh 		if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
    606       1.45       eeh 			map->dm_segs[seg].ds_addr > is->is_dvmaend) {
    607       1.45       eeh 			printf("seg %d dvmaddr %lx out of range %x - %x\n",
    608       1.71   tsutsui 			    seg, (long)map->dm_segs[seg].ds_addr,
    609       1.71   tsutsui 			    is->is_dvmabase, is->is_dvmaend);
    610       1.57       chs #ifdef DDB
    611       1.45       eeh 			Debugger();
    612       1.57       chs #endif
    613       1.45       eeh 		}
    614       1.45       eeh 	}
    615       1.45       eeh #endif
    616        1.7       mrg 	return (0);
    617        1.7       mrg }
    618        1.7       mrg 
    619        1.7       mrg 
    620        1.7       mrg void
    621  1.81.44.1      yamt iommu_dvmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    622        1.7       mrg {
    623  1.81.44.1      yamt 	struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
    624       1.55       eeh 	struct iommu_state *is = sb->sb_is;
    625       1.40       eeh 	int error, s;
    626       1.70  christos 	bus_size_t sgsize = map->_dm_dvmasize;
    627        1.7       mrg 
    628       1.40       eeh 	/* Flush the iommu */
    629       1.40       eeh #ifdef DEBUG
    630       1.40       eeh 	if (!map->_dm_dvmastart) {
    631       1.40       eeh 		printf("iommu_dvmamap_unload: No dvmastart is zero\n");
    632       1.40       eeh #ifdef DDB
    633       1.40       eeh 		Debugger();
    634       1.40       eeh #endif
    635       1.40       eeh 	}
    636       1.40       eeh #endif
    637       1.40       eeh 	iommu_remove(is, map->_dm_dvmastart, map->_dm_dvmasize);
    638        1.7       mrg 
    639       1.23       eeh 	/* Flush the caches */
    640       1.23       eeh 	bus_dmamap_unload(t->_parent, map);
    641       1.23       eeh 
    642        1.7       mrg 	/* Mark the mappings as invalid. */
    643        1.7       mrg 	map->dm_mapsize = 0;
    644        1.7       mrg 	map->dm_nsegs = 0;
    645       1.58       chs 
    646        1.7       mrg 	s = splhigh();
    647       1.58       chs 	error = extent_free(is->is_dvmamap, map->_dm_dvmastart,
    648       1.40       eeh 		map->_dm_dvmasize, EX_NOWAIT);
    649       1.43       eeh 	map->_dm_dvmastart = 0;
    650       1.43       eeh 	map->_dm_dvmasize = 0;
    651        1.7       mrg 	splx(s);
    652        1.7       mrg 	if (error != 0)
    653  1.81.44.2      yamt 		printf("warning: %s: %" PRId64 " of DVMA space lost\n",
    654  1.81.44.2      yamt 		    __func__, sgsize);
    655       1.40       eeh 
    656       1.40       eeh 	/* Clear the map */
    657        1.9       eeh }
    658        1.9       eeh 
    659        1.9       eeh 
    660        1.9       eeh int
    661  1.81.44.1      yamt iommu_dvmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
    662  1.81.44.1      yamt 	bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    663        1.9       eeh {
    664  1.81.44.1      yamt 	struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
    665       1.55       eeh 	struct iommu_state *is = sb->sb_is;
    666       1.58       chs 	struct vm_page *pg;
    667       1.40       eeh 	int i, j, s;
    668       1.26    martin 	int left;
    669  1.81.44.2      yamt 	int err, needsflush;
    670        1.9       eeh 	bus_size_t sgsize;
    671        1.9       eeh 	paddr_t pa;
    672       1.21       eeh 	bus_size_t boundary, align;
    673  1.81.44.2      yamt 	u_long dvmaddr, sgstart, sgend, bmask;
    674       1.58       chs 	struct pglist *pglist;
    675  1.81.44.2      yamt 	const int pagesz = PAGE_SIZE;
    676  1.81.44.2      yamt #ifdef DEBUG
    677  1.81.44.2      yamt 	int npg = 0;
    678  1.81.44.2      yamt #endif
    679        1.9       eeh 
    680        1.9       eeh 	if (map->dm_nsegs) {
    681        1.9       eeh 		/* Already in use?? */
    682        1.9       eeh #ifdef DIAGNOSTIC
    683        1.9       eeh 		printf("iommu_dvmamap_load_raw: map still in use\n");
    684        1.9       eeh #endif
    685        1.9       eeh 		bus_dmamap_unload(t, map);
    686        1.9       eeh 	}
    687       1.40       eeh 
    688       1.40       eeh 	/*
    689       1.40       eeh 	 * A boundary presented to bus_dmamem_alloc() takes precedence
    690       1.40       eeh 	 * over boundary in the map.
    691       1.40       eeh 	 */
    692       1.40       eeh 	if ((boundary = segs[0]._ds_boundary) == 0)
    693       1.40       eeh 		boundary = map->_dm_boundary;
    694       1.40       eeh 
    695       1.45       eeh 	align = max(segs[0]._ds_align, pagesz);
    696       1.40       eeh 
    697        1.9       eeh 	/*
    698        1.9       eeh 	 * Make sure that on error condition we return "no valid mappings".
    699        1.9       eeh 	 */
    700        1.9       eeh 	map->dm_nsegs = 0;
    701       1.26    martin 	/* Count up the total number of pages we need */
    702  1.81.44.2      yamt 	pa = trunc_page(segs[0].ds_addr);
    703       1.26    martin 	sgsize = 0;
    704       1.40       eeh 	left = size;
    705  1.81.44.2      yamt 	for (i = 0; left > 0 && i < nsegs; i++) {
    706       1.26    martin 		if (round_page(pa) != round_page(segs[i].ds_addr))
    707  1.81.44.2      yamt 			sgsize = round_page(sgsize) +
    708  1.81.44.2      yamt 			    (segs[i].ds_addr & PGOFSET);
    709       1.40       eeh 		sgsize += min(left, segs[i].ds_len);
    710       1.40       eeh 		left -= segs[i].ds_len;
    711       1.26    martin 		pa = segs[i].ds_addr + segs[i].ds_len;
    712       1.26    martin 	}
    713  1.81.44.2      yamt 	sgsize = round_page(sgsize);
    714        1.9       eeh 
    715       1.40       eeh 	s = splhigh();
    716       1.58       chs 	/*
    717       1.58       chs 	 * If our segment size is larger than the boundary we need to
    718       1.45       eeh 	 * split the transfer up into little pieces ourselves.
    719        1.9       eeh 	 */
    720       1.40       eeh 	err = extent_alloc(is->is_dvmamap, sgsize, align,
    721       1.40       eeh 		(sgsize > boundary) ? 0 : boundary,
    722       1.40       eeh 		((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT) |
    723       1.54       eeh 		EX_BOUNDZERO, &dvmaddr);
    724        1.9       eeh 	splx(s);
    725        1.9       eeh 
    726        1.9       eeh 	if (err != 0)
    727        1.9       eeh 		return (err);
    728        1.9       eeh 
    729        1.9       eeh #ifdef DEBUG
    730       1.65  nakayama 	if (dvmaddr == (u_long)-1)
    731       1.58       chs 	{
    732        1.9       eeh 		printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
    733       1.25       mrg 		    (int)sgsize, flags);
    734       1.57       chs #ifdef DDB
    735        1.9       eeh 		Debugger();
    736       1.57       chs #endif
    737       1.58       chs 	}
    738       1.58       chs #endif
    739       1.65  nakayama 	if (dvmaddr == (u_long)-1)
    740        1.9       eeh 		return (ENOMEM);
    741        1.9       eeh 
    742       1.40       eeh 	/* Set the active DVMA map */
    743       1.40       eeh 	map->_dm_dvmastart = dvmaddr;
    744       1.40       eeh 	map->_dm_dvmasize = sgsize;
    745       1.40       eeh 
    746  1.81.44.2      yamt 	bmask = ~(boundary - 1);
    747       1.58       chs 	if ((pglist = segs[0]._ds_mlist) == NULL) {
    748  1.81.44.2      yamt 		u_long prev_va = 0UL, last_va = dvmaddr;
    749       1.45       eeh 		paddr_t prev_pa = 0;
    750       1.45       eeh 		int end = 0, offset;
    751  1.81.44.2      yamt 		bus_size_t len = size;
    752       1.45       eeh 
    753       1.26    martin 		/*
    754       1.45       eeh 		 * This segs is made up of individual physical
    755       1.58       chs 		 *  segments, probably by _bus_dmamap_load_uio() or
    756       1.26    martin 		 * _bus_dmamap_load_mbuf().  Ignore the mlist and
    757       1.45       eeh 		 * load each one individually.
    758       1.26    martin 		 */
    759       1.45       eeh 		j = 0;
    760  1.81.44.2      yamt 		needsflush = 0;
    761       1.45       eeh 		for (i = 0; i < nsegs ; i++) {
    762       1.40       eeh 
    763       1.45       eeh 			pa = segs[i].ds_addr;
    764       1.45       eeh 			offset = (pa & PGOFSET);
    765       1.45       eeh 			pa = trunc_page(pa);
    766       1.45       eeh 			dvmaddr = trunc_page(dvmaddr);
    767  1.81.44.2      yamt 			left = min(len, segs[i].ds_len);
    768       1.45       eeh 
    769       1.45       eeh 			DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: converting "
    770       1.58       chs 				"physseg %d start %lx size %lx\n", i,
    771       1.61    martin 				(long)segs[i].ds_addr, (long)segs[i].ds_len));
    772       1.26    martin 
    773       1.58       chs 			if ((pa == prev_pa) &&
    774       1.47       eeh 				((offset != 0) || (end != offset))) {
    775       1.45       eeh 				/* We can re-use this mapping */
    776       1.45       eeh 				dvmaddr = prev_va;
    777       1.45       eeh 			}
    778       1.29    martin 
    779       1.45       eeh 			sgstart = dvmaddr + offset;
    780       1.45       eeh 			sgend = sgstart + left - 1;
    781       1.26    martin 
    782       1.45       eeh 			/* Are the segments virtually adjacent? */
    783       1.58       chs 			if ((j > 0) && (end == offset) &&
    784  1.81.44.2      yamt 			    ((offset == 0) || (pa == prev_pa)) &&
    785  1.81.44.2      yamt 			    (map->dm_segs[j-1].ds_len + left <=
    786  1.81.44.2      yamt 			     map->dm_maxsegsz)) {
    787       1.45       eeh 				/* Just append to the previous segment. */
    788       1.45       eeh 				map->dm_segs[--j].ds_len += left;
    789  1.81.44.2      yamt 				/* Restore sgstart for boundary check */
    790  1.81.44.2      yamt 				sgstart = map->dm_segs[j].ds_addr;
    791       1.45       eeh 				DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    792       1.45       eeh 					"appending seg %d start %lx size %lx\n", j,
    793       1.58       chs 					(long)map->dm_segs[j].ds_addr,
    794       1.61    martin 					(long)map->dm_segs[j].ds_len));
    795       1.45       eeh 			} else {
    796       1.53       eeh 				if (j >= map->_dm_segcnt) {
    797  1.81.44.2      yamt 					iommu_remove(is, map->_dm_dvmastart,
    798  1.81.44.2      yamt 					    last_va - map->_dm_dvmastart);
    799  1.81.44.2      yamt 					goto fail;
    800       1.53       eeh 				}
    801       1.45       eeh 				map->dm_segs[j].ds_addr = sgstart;
    802       1.45       eeh 				map->dm_segs[j].ds_len = left;
    803       1.45       eeh 				DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    804       1.45       eeh 					"seg %d start %lx size %lx\n", j,
    805       1.48       eeh 					(long)map->dm_segs[j].ds_addr,
    806       1.61    martin 					(long)map->dm_segs[j].ds_len));
    807       1.40       eeh 			}
    808       1.45       eeh 			end = (offset + left) & PGOFSET;
    809       1.40       eeh 
    810       1.40       eeh 			/* Check for boundary issues */
    811  1.81.44.2      yamt 			while ((sgstart & bmask) != (sgend & bmask)) {
    812       1.40       eeh 				/* Need a new segment. */
    813       1.40       eeh 				map->dm_segs[j].ds_len =
    814       1.53       eeh 					boundary - (sgstart & (boundary - 1));
    815       1.40       eeh 				DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    816       1.40       eeh 					"seg %d start %lx size %lx\n", j,
    817       1.58       chs 					(long)map->dm_segs[j].ds_addr,
    818       1.61    martin 					(long)map->dm_segs[j].ds_len));
    819       1.53       eeh 				if (++j >= map->_dm_segcnt) {
    820  1.81.44.2      yamt 					iommu_remove(is, map->_dm_dvmastart,
    821  1.81.44.2      yamt 					    last_va - map->_dm_dvmastart);
    822  1.81.44.2      yamt 					goto fail;
    823       1.40       eeh 				}
    824  1.81.44.2      yamt 				sgstart += map->dm_segs[j-1].ds_len;
    825       1.40       eeh 				map->dm_segs[j].ds_addr = sgstart;
    826       1.40       eeh 				map->dm_segs[j].ds_len = sgend - sgstart + 1;
    827       1.40       eeh 			}
    828       1.40       eeh 
    829       1.26    martin 			if (sgsize == 0)
    830       1.26    martin 				panic("iommu_dmamap_load_raw: size botch");
    831       1.40       eeh 
    832       1.45       eeh 			/* Now map a series of pages. */
    833       1.51       eeh 			while (dvmaddr <= sgend) {
    834       1.45       eeh 				DPRINTF(IDB_BUSDMA,
    835       1.45       eeh 					("iommu_dvmamap_load_raw: map %p "
    836       1.45       eeh 						"loading va %lx at pa %lx\n",
    837       1.45       eeh 						map, (long)dvmaddr,
    838       1.45       eeh 						(long)(pa)));
    839       1.45       eeh 				/* Enter it if we haven't before. */
    840  1.81.44.2      yamt 				if (prev_va != dvmaddr) {
    841       1.55       eeh 					iommu_enter(sb, prev_va = dvmaddr,
    842  1.81.44.2      yamt 					    prev_pa = pa,
    843  1.81.44.2      yamt 					    flags | IOTTE_DEBUG(++npg << 12));
    844  1.81.44.2      yamt 					needsflush = 1;
    845  1.81.44.2      yamt 				}
    846       1.45       eeh 				dvmaddr += pagesz;
    847       1.45       eeh 				pa += pagesz;
    848  1.81.44.2      yamt 				last_va = dvmaddr;
    849       1.45       eeh 			}
    850       1.45       eeh 
    851  1.81.44.2      yamt 			len -= left;
    852       1.45       eeh 			++j;
    853       1.26    martin 		}
    854  1.81.44.2      yamt 		if (needsflush)
    855  1.81.44.2      yamt 			iommu_strbuf_flush_done(sb);
    856       1.45       eeh 
    857  1.81.44.2      yamt 		map->dm_mapsize = size;
    858       1.45       eeh 		map->dm_nsegs = j;
    859       1.45       eeh #ifdef DIAGNOSTIC
    860       1.45       eeh 		{ int seg;
    861       1.45       eeh 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    862       1.45       eeh 		if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
    863       1.45       eeh 			map->dm_segs[seg].ds_addr > is->is_dvmaend) {
    864       1.45       eeh 			printf("seg %d dvmaddr %lx out of range %x - %x\n",
    865       1.58       chs 				seg, (long)map->dm_segs[seg].ds_addr,
    866       1.45       eeh 				is->is_dvmabase, is->is_dvmaend);
    867       1.57       chs #ifdef DDB
    868       1.45       eeh 			Debugger();
    869       1.57       chs #endif
    870       1.45       eeh 		}
    871       1.45       eeh 	}
    872       1.45       eeh 		}
    873       1.45       eeh #endif
    874       1.26    martin 		return (0);
    875       1.26    martin 	}
    876       1.58       chs 
    877        1.9       eeh 	/*
    878       1.40       eeh 	 * This was allocated with bus_dmamem_alloc.
    879       1.58       chs 	 * The pages are on a `pglist'.
    880        1.9       eeh 	 */
    881       1.26    martin 	i = 0;
    882       1.40       eeh 	sgstart = dvmaddr;
    883       1.40       eeh 	sgend = sgstart + size - 1;
    884       1.40       eeh 	map->dm_segs[i].ds_addr = sgstart;
    885  1.81.44.2      yamt 	while ((sgstart & bmask) != (sgend & bmask)) {
    886       1.40       eeh 		/* Oops.  We crossed a boundary.  Split the xfer. */
    887       1.53       eeh 		map->dm_segs[i].ds_len = boundary - (sgstart & (boundary - 1));
    888       1.40       eeh 		DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    889       1.40       eeh 			"seg %d start %lx size %lx\n", i,
    890       1.48       eeh 			(long)map->dm_segs[i].ds_addr,
    891       1.61    martin 			(long)map->dm_segs[i].ds_len));
    892       1.53       eeh 		if (++i >= map->_dm_segcnt) {
    893       1.40       eeh 			/* Too many segments.  Fail the operation. */
    894  1.81.44.2      yamt 			goto fail;
    895       1.40       eeh 		}
    896  1.81.44.2      yamt 		sgstart += map->dm_segs[i-1].ds_len;
    897       1.40       eeh 		map->dm_segs[i].ds_addr = sgstart;
    898       1.40       eeh 	}
    899       1.40       eeh 	DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
    900       1.40       eeh 			"seg %d start %lx size %lx\n", i,
    901       1.61    martin 			(long)map->dm_segs[i].ds_addr, (long)map->dm_segs[i].ds_len));
    902       1.40       eeh 	map->dm_segs[i].ds_len = sgend - sgstart + 1;
    903        1.9       eeh 
    904  1.81.44.2      yamt 	needsflush = 0;
    905  1.81.44.1      yamt 	TAILQ_FOREACH(pg, pglist, pageq.queue) {
    906        1.9       eeh 		if (sgsize == 0)
    907        1.9       eeh 			panic("iommu_dmamap_load_raw: size botch");
    908       1.58       chs 		pa = VM_PAGE_TO_PHYS(pg);
    909        1.9       eeh 
    910       1.22       mrg 		DPRINTF(IDB_BUSDMA,
    911        1.9       eeh 		    ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
    912        1.9       eeh 		    map, (long)dvmaddr, (long)(pa)));
    913  1.81.44.2      yamt 		iommu_enter(sb, dvmaddr, pa, flags | IOTTE_DEBUG(0x8000));
    914  1.81.44.2      yamt 		needsflush = 1;
    915       1.58       chs 
    916        1.9       eeh 		dvmaddr += pagesz;
    917        1.9       eeh 		sgsize -= pagesz;
    918        1.9       eeh 	}
    919  1.81.44.2      yamt 	if (needsflush)
    920  1.81.44.2      yamt 		iommu_strbuf_flush_done(sb);
    921       1.40       eeh 	map->dm_mapsize = size;
    922       1.40       eeh 	map->dm_nsegs = i+1;
    923       1.45       eeh #ifdef DIAGNOSTIC
    924       1.45       eeh 	{ int seg;
    925       1.45       eeh 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    926       1.45       eeh 		if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
    927       1.45       eeh 			map->dm_segs[seg].ds_addr > is->is_dvmaend) {
    928       1.45       eeh 			printf("seg %d dvmaddr %lx out of range %x - %x\n",
    929       1.58       chs 				seg, (long)map->dm_segs[seg].ds_addr,
    930       1.45       eeh 				is->is_dvmabase, is->is_dvmaend);
    931       1.57       chs #ifdef DDB
    932       1.45       eeh 			Debugger();
    933       1.57       chs #endif
    934       1.45       eeh 		}
    935       1.45       eeh 	}
    936       1.45       eeh 	}
    937       1.45       eeh #endif
    938        1.9       eeh 	return (0);
    939  1.81.44.2      yamt 
    940  1.81.44.2      yamt fail:
    941  1.81.44.2      yamt 	s = splhigh();
    942  1.81.44.2      yamt 	err = extent_free(is->is_dvmamap, map->_dm_dvmastart, sgsize,
    943  1.81.44.2      yamt 	    EX_NOWAIT);
    944  1.81.44.2      yamt 	map->_dm_dvmastart = 0;
    945  1.81.44.2      yamt 	map->_dm_dvmasize = 0;
    946  1.81.44.2      yamt 	splx(s);
    947  1.81.44.2      yamt 	if (err != 0)
    948  1.81.44.2      yamt 		printf("warning: %s: %" PRId64 " of DVMA space lost\n",
    949  1.81.44.2      yamt 		    __func__, sgsize);
    950  1.81.44.2      yamt 	return (EFBIG);
    951        1.7       mrg }
    952        1.7       mrg 
    953       1.67    petrov 
    954       1.67    petrov /*
    955       1.67    petrov  * Flush an individual dma segment, returns non-zero if the streaming buffers
    956       1.67    petrov  * need flushing afterwards.
    957       1.67    petrov  */
    958       1.67    petrov static int
    959       1.67    petrov iommu_dvmamap_sync_range(struct strbuf_ctl *sb, vaddr_t va, bus_size_t len)
    960       1.67    petrov {
    961       1.67    petrov 	vaddr_t vaend;
    962       1.67    petrov 	struct iommu_state *is = sb->sb_is;
    963       1.67    petrov 
    964       1.67    petrov #ifdef DIAGNOSTIC
    965       1.67    petrov 	if (va < is->is_dvmabase || va > is->is_dvmaend)
    966       1.67    petrov 		panic("invalid va: %llx", (long long)va);
    967       1.67    petrov #endif
    968       1.67    petrov 
    969       1.67    petrov 	if ((is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)] & IOTTE_STREAM) == 0) {
    970       1.67    petrov 		DPRINTF(IDB_BUSDMA,
    971       1.67    petrov 			("iommu_dvmamap_sync_range: attempting to flush "
    972       1.67    petrov 			 "non-streaming entry\n"));
    973       1.67    petrov 		return (0);
    974       1.67    petrov 	}
    975       1.67    petrov 
    976  1.81.44.2      yamt 	vaend = round_page(va + len) - 1;
    977  1.81.44.2      yamt 	va = trunc_page(va);
    978       1.67    petrov 
    979       1.67    petrov #ifdef DIAGNOSTIC
    980       1.67    petrov 	if (va < is->is_dvmabase || vaend > is->is_dvmaend)
    981       1.67    petrov 		panic("invalid va range: %llx to %llx (%x to %x)",
    982       1.67    petrov 		    (long long)va, (long long)vaend,
    983       1.67    petrov 		    is->is_dvmabase,
    984       1.67    petrov 		    is->is_dvmaend);
    985       1.67    petrov #endif
    986       1.67    petrov 
    987       1.67    petrov 	for ( ; va <= vaend; va += PAGE_SIZE) {
    988       1.67    petrov 		DPRINTF(IDB_BUSDMA,
    989       1.67    petrov 		    ("iommu_dvmamap_sync_range: flushing va %p\n",
    990       1.67    petrov 		    (void *)(u_long)va));
    991       1.67    petrov 		iommu_strbuf_flush(sb, va);
    992       1.67    petrov 	}
    993       1.67    petrov 
    994       1.67    petrov 	return (1);
    995       1.67    petrov }
    996       1.67    petrov 
    997  1.81.44.1      yamt static void
    998  1.81.44.1      yamt _iommu_dvmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    999  1.81.44.1      yamt 	bus_size_t len, int ops)
   1000        1.7       mrg {
   1001  1.81.44.1      yamt 	struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
   1002       1.67    petrov 	bus_size_t count;
   1003       1.67    petrov 	int i, needsflush = 0;
   1004       1.63    petrov 
   1005       1.63    petrov 	if (!sb->sb_flush)
   1006       1.63    petrov 		return;
   1007        1.7       mrg 
   1008       1.67    petrov 	for (i = 0; i < map->dm_nsegs; i++) {
   1009       1.67    petrov 		if (offset < map->dm_segs[i].ds_len)
   1010       1.67    petrov 			break;
   1011       1.67    petrov 		offset -= map->dm_segs[i].ds_len;
   1012       1.67    petrov 	}
   1013       1.60    petrov 
   1014       1.67    petrov 	if (i == map->dm_nsegs)
   1015       1.68    martin 		panic("iommu_dvmamap_sync: segment too short %llu",
   1016       1.68    martin 		    (unsigned long long)offset);
   1017       1.60    petrov 
   1018       1.62    petrov 	if (ops & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_POSTWRITE)) {
   1019       1.60    petrov 		/* Nothing to do */;
   1020       1.60    petrov 	}
   1021       1.60    petrov 
   1022       1.62    petrov 	if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_PREWRITE)) {
   1023       1.67    petrov 
   1024       1.67    petrov 		for (; len > 0 && i < map->dm_nsegs; i++) {
   1025       1.67    petrov 			count = MIN(map->dm_segs[i].ds_len - offset, len);
   1026       1.67    petrov 			if (count > 0 &&
   1027       1.67    petrov 			    iommu_dvmamap_sync_range(sb,
   1028       1.67    petrov 				map->dm_segs[i].ds_addr + offset, count))
   1029       1.67    petrov 				needsflush = 1;
   1030       1.67    petrov 			offset = 0;
   1031       1.67    petrov 			len -= count;
   1032       1.67    petrov 		}
   1033       1.60    petrov #ifdef DIAGNOSTIC
   1034       1.67    petrov 		if (i == map->dm_nsegs && len > 0)
   1035       1.73  nakayama 			panic("iommu_dvmamap_sync: leftover %llu",
   1036       1.73  nakayama 			    (unsigned long long)len);
   1037       1.60    petrov #endif
   1038       1.55       eeh 
   1039       1.67    petrov 		if (needsflush)
   1040       1.58       chs 			iommu_strbuf_flush_done(sb);
   1041        1.7       mrg 	}
   1042        1.7       mrg }
   1043        1.7       mrg 
   1044  1.81.44.1      yamt void
   1045  1.81.44.1      yamt iommu_dvmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
   1046  1.81.44.1      yamt 	bus_size_t len, int ops)
   1047  1.81.44.1      yamt {
   1048  1.81.44.1      yamt 
   1049  1.81.44.2      yamt 	/* If len is 0, then there is nothing to do */
   1050  1.81.44.2      yamt 	if (len == 0)
   1051  1.81.44.2      yamt 		return;
   1052  1.81.44.2      yamt 
   1053  1.81.44.1      yamt 	if (ops & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE)) {
   1054  1.81.44.1      yamt 		/* Flush the CPU then the IOMMU */
   1055  1.81.44.1      yamt 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1056  1.81.44.1      yamt 		_iommu_dvmamap_sync(t, map, offset, len, ops);
   1057  1.81.44.1      yamt 	}
   1058  1.81.44.1      yamt 	if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE)) {
   1059  1.81.44.1      yamt 		/* Flush the IOMMU then the CPU */
   1060  1.81.44.1      yamt 		_iommu_dvmamap_sync(t, map, offset, len, ops);
   1061  1.81.44.1      yamt 		bus_dmamap_sync(t->_parent, map, offset, len, ops);
   1062  1.81.44.1      yamt 	}
   1063  1.81.44.1      yamt }
   1064  1.81.44.1      yamt 
   1065        1.7       mrg int
   1066  1.81.44.1      yamt iommu_dvmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
   1067  1.81.44.1      yamt 	bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
   1068  1.81.44.1      yamt 	int flags)
   1069        1.7       mrg {
   1070        1.7       mrg 
   1071       1.25       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx "
   1072       1.25       mrg 	   "segp %p flags %d\n", (unsigned long long)size,
   1073       1.25       mrg 	   (unsigned long long)alignment, (unsigned long long)boundary,
   1074       1.25       mrg 	   segs, flags));
   1075        1.7       mrg 	return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
   1076       1.21       eeh 	    segs, nsegs, rsegs, flags|BUS_DMA_DVMA));
   1077        1.7       mrg }
   1078        1.7       mrg 
   1079        1.7       mrg void
   1080  1.81.44.1      yamt iommu_dvmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
   1081        1.7       mrg {
   1082        1.7       mrg 
   1083       1.22       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
   1084        1.7       mrg 	    segs, nsegs));
   1085        1.7       mrg 	bus_dmamem_free(t->_parent, segs, nsegs);
   1086        1.7       mrg }
   1087        1.7       mrg 
   1088        1.7       mrg /*
   1089        1.7       mrg  * Map the DVMA mappings into the kernel pmap.
   1090        1.7       mrg  * Check the flags to see whether we're streaming or coherent.
   1091        1.7       mrg  */
   1092        1.7       mrg int
   1093  1.81.44.1      yamt iommu_dvmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
   1094  1.81.44.1      yamt 	size_t size, void **kvap, int flags)
   1095        1.7       mrg {
   1096       1.58       chs 	struct vm_page *pg;
   1097        1.7       mrg 	vaddr_t va;
   1098        1.7       mrg 	bus_addr_t addr;
   1099       1.58       chs 	struct pglist *pglist;
   1100        1.8       mrg 	int cbit;
   1101       1.77      yamt 	const uvm_flag_t kmflags =
   1102       1.77      yamt 	    (flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0;
   1103        1.7       mrg 
   1104       1.22       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
   1105        1.7       mrg 	    segs, nsegs, size));
   1106        1.7       mrg 
   1107        1.7       mrg 	/*
   1108        1.8       mrg 	 * Allocate some space in the kernel map, and then map these pages
   1109        1.8       mrg 	 * into this space.
   1110        1.7       mrg 	 */
   1111        1.8       mrg 	size = round_page(size);
   1112       1.77      yamt 	va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY | kmflags);
   1113        1.8       mrg 	if (va == 0)
   1114        1.8       mrg 		return (ENOMEM);
   1115        1.7       mrg 
   1116       1.81  christos 	*kvap = (void *)va;
   1117        1.7       mrg 
   1118       1.58       chs 	/*
   1119        1.7       mrg 	 * digest flags:
   1120        1.7       mrg 	 */
   1121        1.7       mrg 	cbit = 0;
   1122        1.7       mrg 	if (flags & BUS_DMA_COHERENT)	/* Disable vcache */
   1123        1.7       mrg 		cbit |= PMAP_NVC;
   1124  1.81.44.2      yamt 	if (flags & BUS_DMA_NOCACHE)	/* side effects */
   1125        1.7       mrg 		cbit |= PMAP_NC;
   1126        1.7       mrg 
   1127        1.7       mrg 	/*
   1128        1.8       mrg 	 * Now take this and map it into the CPU.
   1129        1.7       mrg 	 */
   1130       1.58       chs 	pglist = segs[0]._ds_mlist;
   1131  1.81.44.1      yamt 	TAILQ_FOREACH(pg, pglist, pageq.queue) {
   1132        1.8       mrg #ifdef DIAGNOSTIC
   1133        1.7       mrg 		if (size == 0)
   1134        1.7       mrg 			panic("iommu_dvmamem_map: size botch");
   1135        1.8       mrg #endif
   1136       1.58       chs 		addr = VM_PAGE_TO_PHYS(pg);
   1137       1.22       mrg 		DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: "
   1138       1.25       mrg 		    "mapping va %lx at %llx\n", va, (unsigned long long)addr | cbit));
   1139  1.81.44.2      yamt 		pmap_kenter_pa(va, addr | cbit,
   1140  1.81.44.2      yamt 		    VM_PROT_READ | VM_PROT_WRITE, 0);
   1141        1.7       mrg 		va += PAGE_SIZE;
   1142        1.7       mrg 		size -= PAGE_SIZE;
   1143        1.7       mrg 	}
   1144       1.38     chris 	pmap_update(pmap_kernel());
   1145        1.7       mrg 	return (0);
   1146        1.7       mrg }
   1147        1.7       mrg 
   1148        1.7       mrg /*
   1149        1.7       mrg  * Unmap DVMA mappings from kernel
   1150        1.7       mrg  */
   1151        1.7       mrg void
   1152  1.81.44.1      yamt iommu_dvmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
   1153        1.7       mrg {
   1154       1.58       chs 
   1155       1.22       mrg 	DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
   1156        1.7       mrg 	    kva, size));
   1157       1.58       chs 
   1158        1.7       mrg #ifdef DIAGNOSTIC
   1159        1.7       mrg 	if ((u_long)kva & PGOFSET)
   1160        1.7       mrg 		panic("iommu_dvmamem_unmap");
   1161        1.7       mrg #endif
   1162       1.58       chs 
   1163        1.7       mrg 	size = round_page(size);
   1164       1.58       chs 	pmap_kremove((vaddr_t)kva, size);
   1165       1.38     chris 	pmap_update(pmap_kernel());
   1166       1.76      yamt 	uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
   1167        1.1       mrg }
   1168