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