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