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