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