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